• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * CPU-measurement facilities
3  *
4  *  Copyright IBM Corp. 2012
5  *  Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
6  *	       Jan Glauber <jang@linux.vnet.ibm.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License (version 2 only)
10  * as published by the Free Software Foundation.
11  */
12 #ifndef _ASM_S390_CPU_MF_H
13 #define _ASM_S390_CPU_MF_H
14 
15 #include <linux/errno.h>
16 #include <asm/facility.h>
17 
18 #define CPU_MF_INT_SF_IAE	(1 << 31)	/* invalid entry address */
19 #define CPU_MF_INT_SF_ISE	(1 << 30)	/* incorrect SDBT entry */
20 #define CPU_MF_INT_SF_PRA	(1 << 29)	/* program request alert */
21 #define CPU_MF_INT_SF_SACA	(1 << 23)	/* sampler auth. change alert */
22 #define CPU_MF_INT_SF_LSDA	(1 << 22)	/* loss of sample data alert */
23 #define CPU_MF_INT_CF_MTDA	(1 << 15)	/* loss of MT ctr. data alert */
24 #define CPU_MF_INT_CF_CACA	(1 <<  7)	/* counter auth. change alert */
25 #define CPU_MF_INT_CF_LCDA	(1 <<  6)	/* loss of counter data alert */
26 #define CPU_MF_INT_CF_MASK	(CPU_MF_INT_CF_MTDA|CPU_MF_INT_CF_CACA| \
27 				 CPU_MF_INT_CF_LCDA)
28 #define CPU_MF_INT_SF_MASK	(CPU_MF_INT_SF_IAE|CPU_MF_INT_SF_ISE|	\
29 				 CPU_MF_INT_SF_PRA|CPU_MF_INT_SF_SACA|	\
30 				 CPU_MF_INT_SF_LSDA)
31 
32 /* CPU measurement facility support */
cpum_cf_avail(void)33 static inline int cpum_cf_avail(void)
34 {
35 	return MACHINE_HAS_LPP && test_facility(67);
36 }
37 
cpum_sf_avail(void)38 static inline int cpum_sf_avail(void)
39 {
40 	return MACHINE_HAS_LPP && test_facility(68);
41 }
42 
43 
44 struct cpumf_ctr_info {
45 	u16   cfvn;
46 	u16   auth_ctl;
47 	u16   enable_ctl;
48 	u16   act_ctl;
49 	u16   max_cpu;
50 	u16   csvn;
51 	u16   max_cg;
52 	u16   reserved1;
53 	u32   reserved2[12];
54 } __packed;
55 
56 /* QUERY SAMPLING INFORMATION block */
57 struct hws_qsi_info_block {	    /* Bit(s) */
58 	unsigned int b0_13:14;	    /* 0-13: zeros			 */
59 	unsigned int as:1;	    /* 14: basic-sampling authorization	 */
60 	unsigned int ad:1;	    /* 15: diag-sampling authorization	 */
61 	unsigned int b16_21:6;	    /* 16-21: zeros			 */
62 	unsigned int es:1;	    /* 22: basic-sampling enable control */
63 	unsigned int ed:1;	    /* 23: diag-sampling enable control	 */
64 	unsigned int b24_29:6;	    /* 24-29: zeros			 */
65 	unsigned int cs:1;	    /* 30: basic-sampling activation control */
66 	unsigned int cd:1;	    /* 31: diag-sampling activation control */
67 	unsigned int bsdes:16;	    /* 4-5: size of basic sampling entry */
68 	unsigned int dsdes:16;	    /* 6-7: size of diagnostic sampling entry */
69 	unsigned long min_sampl_rate; /* 8-15: minimum sampling interval */
70 	unsigned long max_sampl_rate; /* 16-23: maximum sampling interval*/
71 	unsigned long tear;	    /* 24-31: TEAR contents		 */
72 	unsigned long dear;	    /* 32-39: DEAR contents		 */
73 	unsigned int rsvrd0;	    /* 40-43: reserved			 */
74 	unsigned int cpu_speed;     /* 44-47: CPU speed 		 */
75 	unsigned long long rsvrd1;  /* 48-55: reserved			 */
76 	unsigned long long rsvrd2;  /* 56-63: reserved			 */
77 } __packed;
78 
79 /* SET SAMPLING CONTROLS request block */
80 struct hws_lsctl_request_block {
81 	unsigned int s:1;	    /* 0: maximum buffer indicator	 */
82 	unsigned int h:1;	    /* 1: part. level reserved for VM use*/
83 	unsigned long long b2_53:52;/* 2-53: zeros			 */
84 	unsigned int es:1;	    /* 54: basic-sampling enable control */
85 	unsigned int ed:1;	    /* 55: diag-sampling enable control	 */
86 	unsigned int b56_61:6;	    /* 56-61: - zeros			 */
87 	unsigned int cs:1;	    /* 62: basic-sampling activation control */
88 	unsigned int cd:1;	    /* 63: diag-sampling activation control  */
89 	unsigned long interval;     /* 8-15: sampling interval		 */
90 	unsigned long tear;	    /* 16-23: TEAR contents		 */
91 	unsigned long dear;	    /* 24-31: DEAR contents		 */
92 	/* 32-63:							 */
93 	unsigned long rsvrd1;	    /* reserved 			 */
94 	unsigned long rsvrd2;	    /* reserved 			 */
95 	unsigned long rsvrd3;	    /* reserved 			 */
96 	unsigned long rsvrd4;	    /* reserved 			 */
97 } __packed;
98 
99 struct hws_basic_entry {
100 	unsigned int def:16;	    /* 0-15  Data Entry Format		 */
101 	unsigned int R:4;	    /* 16-19 reserved			 */
102 	unsigned int U:4;	    /* 20-23 Number of unique instruct.  */
103 	unsigned int z:2;	    /* zeros				 */
104 	unsigned int T:1;	    /* 26 PSW DAT mode			 */
105 	unsigned int W:1;	    /* 27 PSW wait state		 */
106 	unsigned int P:1;	    /* 28 PSW Problem state		 */
107 	unsigned int AS:2;	    /* 29-30 PSW address-space control	 */
108 	unsigned int I:1;	    /* 31 entry valid or invalid	 */
109 	unsigned int CL:2;	    /* 32-33 Configuration Level	 */
110 	unsigned int:14;
111 	unsigned int prim_asn:16;   /* primary ASN			 */
112 	unsigned long long ia;	    /* Instruction Address		 */
113 	unsigned long long gpp;     /* Guest Program Parameter		 */
114 	unsigned long long hpp;     /* Host Program Parameter		 */
115 } __packed;
116 
117 struct hws_diag_entry {
118 	unsigned int def:16;	    /* 0-15  Data Entry Format		 */
119 	unsigned int R:15;	    /* 16-19 and 20-30 reserved		 */
120 	unsigned int I:1;	    /* 31 entry valid or invalid	 */
121 	u8	     data[];	    /* Machine-dependent sample data	 */
122 } __packed;
123 
124 struct hws_combined_entry {
125 	struct hws_basic_entry	basic;	/* Basic-sampling data entry */
126 	struct hws_diag_entry	diag;	/* Diagnostic-sampling data entry */
127 } __packed;
128 
129 struct hws_trailer_entry {
130 	union {
131 		struct {
132 			unsigned int f:1;	/* 0 - Block Full Indicator   */
133 			unsigned int a:1;	/* 1 - Alert request control  */
134 			unsigned int t:1;	/* 2 - Timestamp format	      */
135 			unsigned int :29;	/* 3 - 31: Reserved	      */
136 			unsigned int bsdes:16;	/* 32-47: size of basic SDE   */
137 			unsigned int dsdes:16;	/* 48-63: size of diagnostic SDE */
138 		};
139 		unsigned long long flags;	/* 0 - 63: All indicators     */
140 	};
141 	unsigned long long overflow;	 /* 64 - sample Overflow count	      */
142 	unsigned char timestamp[16];	 /* 16 - 31 timestamp		      */
143 	unsigned long long reserved1;	 /* 32 -Reserved		      */
144 	unsigned long long reserved2;	 /*				      */
145 	unsigned long long progusage1;	 /* 48 - reserved for programming use */
146 	unsigned long long progusage2;	 /*				      */
147 } __packed;
148 
149 /* Query counter information */
qctri(struct cpumf_ctr_info * info)150 static inline int qctri(struct cpumf_ctr_info *info)
151 {
152 	int rc = -EINVAL;
153 
154 	asm volatile (
155 		"0:	.insn	s,0xb28e0000,%1\n"
156 		"1:	lhi	%0,0\n"
157 		"2:\n"
158 		EX_TABLE(1b, 2b)
159 		: "+d" (rc), "=Q" (*info));
160 	return rc;
161 }
162 
163 /* Load CPU-counter-set controls */
lcctl(u64 ctl)164 static inline int lcctl(u64 ctl)
165 {
166 	int cc;
167 
168 	asm volatile (
169 		"	.insn	s,0xb2840000,%1\n"
170 		"	ipm	%0\n"
171 		"	srl	%0,28\n"
172 		: "=d" (cc) : "m" (ctl) : "cc");
173 	return cc;
174 }
175 
176 /* Extract CPU counter */
__ecctr(u64 ctr,u64 * content)177 static inline int __ecctr(u64 ctr, u64 *content)
178 {
179 	u64 _content;
180 	int cc;
181 
182 	asm volatile (
183 		"	.insn	rre,0xb2e40000,%0,%2\n"
184 		"	ipm	%1\n"
185 		"	srl	%1,28\n"
186 		: "=d" (_content), "=d" (cc) : "d" (ctr) : "cc");
187 	*content = _content;
188 	return cc;
189 }
190 
191 /* Extract CPU counter */
ecctr(u64 ctr,u64 * val)192 static inline int ecctr(u64 ctr, u64 *val)
193 {
194 	u64 content;
195 	int cc;
196 
197 	cc = __ecctr(ctr, &content);
198 	if (!cc)
199 		*val = content;
200 	return cc;
201 }
202 
203 /* Store CPU counter multiple for the MT utilization counter set */
stcctm5(u64 num,u64 * val)204 static inline int stcctm5(u64 num, u64 *val)
205 {
206 	int cc;
207 
208 	asm volatile (
209 		"	.insn	rsy,0xeb0000000017,%2,5,%1\n"
210 		"	ipm	%0\n"
211 		"	srl	%0,28\n"
212 		: "=d" (cc)
213 		: "Q" (*val), "d" (num)
214 		: "cc", "memory");
215 	return cc;
216 }
217 
218 /* Query sampling information */
qsi(struct hws_qsi_info_block * info)219 static inline int qsi(struct hws_qsi_info_block *info)
220 {
221 	int cc = 1;
222 
223 	asm volatile(
224 		"0:	.insn	s,0xb2860000,%1\n"
225 		"1:	lhi	%0,0\n"
226 		"2:\n"
227 		EX_TABLE(0b, 2b) EX_TABLE(1b, 2b)
228 		: "+d" (cc), "+Q" (*info));
229 	return cc ? -EINVAL : 0;
230 }
231 
232 /* Load sampling controls */
lsctl(struct hws_lsctl_request_block * req)233 static inline int lsctl(struct hws_lsctl_request_block *req)
234 {
235 	int cc;
236 
237 	cc = 1;
238 	asm volatile(
239 		"0:	.insn	s,0xb2870000,0(%1)\n"
240 		"1:	ipm	%0\n"
241 		"	srl	%0,28\n"
242 		"2:\n"
243 		EX_TABLE(0b, 2b) EX_TABLE(1b, 2b)
244 		: "+d" (cc), "+a" (req)
245 		: "m" (*req)
246 		: "cc", "memory");
247 
248 	return cc ? -EINVAL : 0;
249 }
250 
251 /* Sampling control helper functions */
252 
253 #include <linux/time.h>
254 
freq_to_sample_rate(struct hws_qsi_info_block * qsi,unsigned long freq)255 static inline unsigned long freq_to_sample_rate(struct hws_qsi_info_block *qsi,
256 						unsigned long freq)
257 {
258 	return (USEC_PER_SEC / freq) * qsi->cpu_speed;
259 }
260 
sample_rate_to_freq(struct hws_qsi_info_block * qsi,unsigned long rate)261 static inline unsigned long sample_rate_to_freq(struct hws_qsi_info_block *qsi,
262 						unsigned long rate)
263 {
264 	return USEC_PER_SEC * qsi->cpu_speed / rate;
265 }
266 
267 #define SDB_TE_ALERT_REQ_MASK	0x4000000000000000UL
268 #define SDB_TE_BUFFER_FULL_MASK 0x8000000000000000UL
269 
270 /* Return TOD timestamp contained in an trailer entry */
trailer_timestamp(struct hws_trailer_entry * te)271 static inline unsigned long long trailer_timestamp(struct hws_trailer_entry *te)
272 {
273 	/* TOD in STCKE format */
274 	if (te->t)
275 		return *((unsigned long long *) &te->timestamp[1]);
276 
277 	/* TOD in STCK format */
278 	return *((unsigned long long *) &te->timestamp[0]);
279 }
280 
281 /* Return pointer to trailer entry of an sample data block */
trailer_entry_ptr(unsigned long v)282 static inline unsigned long *trailer_entry_ptr(unsigned long v)
283 {
284 	void *ret;
285 
286 	ret = (void *) v;
287 	ret += PAGE_SIZE;
288 	ret -= sizeof(struct hws_trailer_entry);
289 
290 	return (unsigned long *) ret;
291 }
292 
293 /* Return if the entry in the sample data block table (sdbt)
294  * is a link to the next sdbt */
is_link_entry(unsigned long * s)295 static inline int is_link_entry(unsigned long *s)
296 {
297 	return *s & 0x1ul ? 1 : 0;
298 }
299 
300 /* Return pointer to the linked sdbt */
get_next_sdbt(unsigned long * s)301 static inline unsigned long *get_next_sdbt(unsigned long *s)
302 {
303 	return (unsigned long *) (*s & ~0x1ul);
304 }
305 #endif /* _ASM_S390_CPU_MF_H */
306