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_CACA	(1 <<  7)	/* counter auth. change alert */
24#define CPU_MF_INT_CF_LCDA	(1 <<  6)	/* loss of counter data alert */
25#define CPU_MF_INT_RI_HALTED	(1 <<  5)	/* run-time instr. halted */
26#define CPU_MF_INT_RI_BUF_FULL	(1 <<  4)	/* run-time instr. program
27						   buffer full */
28
29#define CPU_MF_INT_CF_MASK	(CPU_MF_INT_CF_CACA|CPU_MF_INT_CF_LCDA)
30#define CPU_MF_INT_SF_MASK	(CPU_MF_INT_SF_IAE|CPU_MF_INT_SF_ISE|	\
31				 CPU_MF_INT_SF_PRA|CPU_MF_INT_SF_SACA|	\
32				 CPU_MF_INT_SF_LSDA)
33#define CPU_MF_INT_RI_MASK	(CPU_MF_INT_RI_HALTED|CPU_MF_INT_RI_BUF_FULL)
34
35/* CPU measurement facility support */
36static inline int cpum_cf_avail(void)
37{
38	return MACHINE_HAS_LPP && test_facility(67);
39}
40
41static inline int cpum_sf_avail(void)
42{
43	return MACHINE_HAS_LPP && test_facility(68);
44}
45
46
47struct cpumf_ctr_info {
48	u16   cfvn;
49	u16   auth_ctl;
50	u16   enable_ctl;
51	u16   act_ctl;
52	u16   max_cpu;
53	u16   csvn;
54	u16   max_cg;
55	u16   reserved1;
56	u32   reserved2[12];
57} __packed;
58
59/* QUERY SAMPLING INFORMATION block */
60struct hws_qsi_info_block {	    /* Bit(s) */
61	unsigned int b0_13:14;	    /* 0-13: zeros			 */
62	unsigned int as:1;	    /* 14: basic-sampling authorization	 */
63	unsigned int ad:1;	    /* 15: diag-sampling authorization	 */
64	unsigned int b16_21:6;	    /* 16-21: zeros			 */
65	unsigned int es:1;	    /* 22: basic-sampling enable control */
66	unsigned int ed:1;	    /* 23: diag-sampling enable control	 */
67	unsigned int b24_29:6;	    /* 24-29: zeros			 */
68	unsigned int cs:1;	    /* 30: basic-sampling activation control */
69	unsigned int cd:1;	    /* 31: diag-sampling activation control */
70	unsigned int bsdes:16;	    /* 4-5: size of basic sampling entry */
71	unsigned int dsdes:16;	    /* 6-7: size of diagnostic sampling entry */
72	unsigned long min_sampl_rate; /* 8-15: minimum sampling interval */
73	unsigned long max_sampl_rate; /* 16-23: maximum sampling interval*/
74	unsigned long tear;	    /* 24-31: TEAR contents		 */
75	unsigned long dear;	    /* 32-39: DEAR contents		 */
76	unsigned int rsvrd0;	    /* 40-43: reserved			 */
77	unsigned int cpu_speed;     /* 44-47: CPU speed 		 */
78	unsigned long long rsvrd1;  /* 48-55: reserved			 */
79	unsigned long long rsvrd2;  /* 56-63: reserved			 */
80} __packed;
81
82/* SET SAMPLING CONTROLS request block */
83struct hws_lsctl_request_block {
84	unsigned int s:1;	    /* 0: maximum buffer indicator	 */
85	unsigned int h:1;	    /* 1: part. level reserved for VM use*/
86	unsigned long long b2_53:52;/* 2-53: zeros			 */
87	unsigned int es:1;	    /* 54: basic-sampling enable control */
88	unsigned int ed:1;	    /* 55: diag-sampling enable control	 */
89	unsigned int b56_61:6;	    /* 56-61: - zeros			 */
90	unsigned int cs:1;	    /* 62: basic-sampling activation control */
91	unsigned int cd:1;	    /* 63: diag-sampling activation control  */
92	unsigned long interval;     /* 8-15: sampling interval		 */
93	unsigned long tear;	    /* 16-23: TEAR contents		 */
94	unsigned long dear;	    /* 24-31: DEAR contents		 */
95	/* 32-63:							 */
96	unsigned long rsvrd1;	    /* reserved 			 */
97	unsigned long rsvrd2;	    /* reserved 			 */
98	unsigned long rsvrd3;	    /* reserved 			 */
99	unsigned long rsvrd4;	    /* reserved 			 */
100} __packed;
101
102struct hws_basic_entry {
103	unsigned int def:16;	    /* 0-15  Data Entry Format		 */
104	unsigned int R:4;	    /* 16-19 reserved			 */
105	unsigned int U:4;	    /* 20-23 Number of unique instruct.  */
106	unsigned int z:2;	    /* zeros				 */
107	unsigned int T:1;	    /* 26 PSW DAT mode			 */
108	unsigned int W:1;	    /* 27 PSW wait state		 */
109	unsigned int P:1;	    /* 28 PSW Problem state		 */
110	unsigned int AS:2;	    /* 29-30 PSW address-space control	 */
111	unsigned int I:1;	    /* 31 entry valid or invalid	 */
112	unsigned int:16;
113	unsigned int prim_asn:16;   /* primary ASN			 */
114	unsigned long long ia;	    /* Instruction Address		 */
115	unsigned long long gpp;     /* Guest Program Parameter		 */
116	unsigned long long hpp;     /* Host Program Parameter		 */
117} __packed;
118
119struct hws_diag_entry {
120	unsigned int def:16;	    /* 0-15  Data Entry Format		 */
121	unsigned int R:14;	    /* 16-19 and 20-30 reserved		 */
122	unsigned int I:1;	    /* 31 entry valid or invalid	 */
123	u8	     data[];	    /* Machine-dependent sample data	 */
124} __packed;
125
126struct hws_combined_entry {
127	struct hws_basic_entry	basic;	/* Basic-sampling data entry */
128	struct hws_diag_entry	diag;	/* Diagnostic-sampling data entry */
129} __packed;
130
131struct hws_trailer_entry {
132	union {
133		struct {
134			unsigned int f:1;	/* 0 - Block Full Indicator   */
135			unsigned int a:1;	/* 1 - Alert request control  */
136			unsigned int t:1;	/* 2 - Timestamp format	      */
137			unsigned long long:61;	/* 3 - 63: Reserved	      */
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 */
150static 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 */
164static 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 */
177static inline int ecctr(u64 ctr, u64 *val)
178{
179	register u64 content asm("4") = 0;
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	if (!cc)
188		*val = content;
189	return cc;
190}
191
192/* Query sampling information */
193static inline int qsi(struct hws_qsi_info_block *info)
194{
195	int cc;
196	cc = 1;
197
198	asm volatile(
199		"0:	.insn	s,0xb2860000,0(%1)\n"
200		"1:	lhi	%0,0\n"
201		"2:\n"
202		EX_TABLE(0b, 2b) EX_TABLE(1b, 2b)
203		: "=d" (cc), "+a" (info)
204		: "m" (*info)
205		: "cc", "memory");
206
207	return cc ? -EINVAL : 0;
208}
209
210/* Load sampling controls */
211static inline int lsctl(struct hws_lsctl_request_block *req)
212{
213	int cc;
214
215	cc = 1;
216	asm volatile(
217		"0:	.insn	s,0xb2870000,0(%1)\n"
218		"1:	ipm	%0\n"
219		"	srl	%0,28\n"
220		"2:\n"
221		EX_TABLE(0b, 2b) EX_TABLE(1b, 2b)
222		: "+d" (cc), "+a" (req)
223		: "m" (*req)
224		: "cc", "memory");
225
226	return cc ? -EINVAL : 0;
227}
228
229/* Sampling control helper functions */
230
231#include <linux/time.h>
232
233static inline unsigned long freq_to_sample_rate(struct hws_qsi_info_block *qsi,
234						unsigned long freq)
235{
236	return (USEC_PER_SEC / freq) * qsi->cpu_speed;
237}
238
239static inline unsigned long sample_rate_to_freq(struct hws_qsi_info_block *qsi,
240						unsigned long rate)
241{
242	return USEC_PER_SEC * qsi->cpu_speed / rate;
243}
244
245#define SDB_TE_ALERT_REQ_MASK	0x4000000000000000UL
246#define SDB_TE_BUFFER_FULL_MASK 0x8000000000000000UL
247
248/* Return TOD timestamp contained in an trailer entry */
249static inline unsigned long long trailer_timestamp(struct hws_trailer_entry *te)
250{
251	/* TOD in STCKE format */
252	if (te->t)
253		return *((unsigned long long *) &te->timestamp[1]);
254
255	/* TOD in STCK format */
256	return *((unsigned long long *) &te->timestamp[0]);
257}
258
259/* Return pointer to trailer entry of an sample data block */
260static inline unsigned long *trailer_entry_ptr(unsigned long v)
261{
262	void *ret;
263
264	ret = (void *) v;
265	ret += PAGE_SIZE;
266	ret -= sizeof(struct hws_trailer_entry);
267
268	return (unsigned long *) ret;
269}
270
271/* Return if the entry in the sample data block table (sdbt)
272 * is a link to the next sdbt */
273static inline int is_link_entry(unsigned long *s)
274{
275	return *s & 0x1ul ? 1 : 0;
276}
277
278/* Return pointer to the linked sdbt */
279static inline unsigned long *get_next_sdbt(unsigned long *s)
280{
281	return (unsigned long *) (*s & ~0x1ul);
282}
283#endif /* _ASM_S390_CPU_MF_H */
284