blob: a9ba5e322bf38ddf23e4e9793e4b441e86c7f9c8 [file] [log] [blame]
Greg Kroah-Hartman0b732142017-11-24 15:00:38 +01001/* SPDX-License-Identifier: GPL-2.0 */
Hendrik Brueckner212188a2012-03-23 11:13:06 +01002/*
3 * CPU-measurement facilities
4 *
5 * Copyright IBM Corp. 2012
6 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
7 * Jan Glauber <jang@linux.vnet.ibm.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License (version 2 only)
11 * as published by the Free Software Foundation.
12 */
Jan Glauberb03d541a2012-03-23 11:13:05 +010013#ifndef _ASM_S390_CPU_MF_H
14#define _ASM_S390_CPU_MF_H
15
Li Zefancb16b912013-03-07 17:35:53 +080016#include <linux/errno.h>
Heiko Carstens1e3cab22012-03-30 09:40:55 +020017#include <asm/facility.h>
18
Jan Glauberb03d541a2012-03-23 11:13:05 +010019#define CPU_MF_INT_SF_IAE (1 << 31) /* invalid entry address */
20#define CPU_MF_INT_SF_ISE (1 << 30) /* incorrect SDBT entry */
21#define CPU_MF_INT_SF_PRA (1 << 29) /* program request alert */
22#define CPU_MF_INT_SF_SACA (1 << 23) /* sampler auth. change alert */
23#define CPU_MF_INT_SF_LSDA (1 << 22) /* loss of sample data alert */
Hendrik Brueckneree699f32016-06-03 16:55:03 +020024#define CPU_MF_INT_CF_MTDA (1 << 15) /* loss of MT ctr. data alert */
Hendrik Brueckner212188a2012-03-23 11:13:06 +010025#define CPU_MF_INT_CF_CACA (1 << 7) /* counter auth. change alert */
26#define CPU_MF_INT_CF_LCDA (1 << 6) /* loss of counter data alert */
Hendrik Brueckneree699f32016-06-03 16:55:03 +020027#define CPU_MF_INT_CF_MASK (CPU_MF_INT_CF_MTDA|CPU_MF_INT_CF_CACA| \
28 CPU_MF_INT_CF_LCDA)
Hendrik Brueckner212188a2012-03-23 11:13:06 +010029#define CPU_MF_INT_SF_MASK (CPU_MF_INT_SF_IAE|CPU_MF_INT_SF_ISE| \
30 CPU_MF_INT_SF_PRA|CPU_MF_INT_SF_SACA| \
31 CPU_MF_INT_SF_LSDA)
32
33/* CPU measurement facility support */
34static inline int cpum_cf_avail(void)
35{
Hendrik Brueckner23d18e82013-02-11 18:11:09 +010036 return MACHINE_HAS_LPP && test_facility(67);
Hendrik Brueckner212188a2012-03-23 11:13:06 +010037}
38
39static inline int cpum_sf_avail(void)
40{
Hendrik Brueckner23d18e82013-02-11 18:11:09 +010041 return MACHINE_HAS_LPP && test_facility(68);
Hendrik Brueckner212188a2012-03-23 11:13:06 +010042}
43
44
45struct cpumf_ctr_info {
46 u16 cfvn;
47 u16 auth_ctl;
48 u16 enable_ctl;
49 u16 act_ctl;
50 u16 max_cpu;
51 u16 csvn;
52 u16 max_cg;
53 u16 reserved1;
54 u32 reserved2[12];
55} __packed;
56
Hendrik Bruecknercf48ad82013-12-11 12:15:52 +010057/* QUERY SAMPLING INFORMATION block */
58struct hws_qsi_info_block { /* Bit(s) */
59 unsigned int b0_13:14; /* 0-13: zeros */
Hendrik Brueckner7e75fc32013-12-13 11:42:44 +010060 unsigned int as:1; /* 14: basic-sampling authorization */
61 unsigned int ad:1; /* 15: diag-sampling authorization */
62 unsigned int b16_21:6; /* 16-21: zeros */
63 unsigned int es:1; /* 22: basic-sampling enable control */
64 unsigned int ed:1; /* 23: diag-sampling enable control */
65 unsigned int b24_29:6; /* 24-29: zeros */
66 unsigned int cs:1; /* 30: basic-sampling activation control */
67 unsigned int cd:1; /* 31: diag-sampling activation control */
68 unsigned int bsdes:16; /* 4-5: size of basic sampling entry */
Hendrik Bruecknercf48ad82013-12-11 12:15:52 +010069 unsigned int dsdes:16; /* 6-7: size of diagnostic sampling entry */
70 unsigned long min_sampl_rate; /* 8-15: minimum sampling interval */
71 unsigned long max_sampl_rate; /* 16-23: maximum sampling interval*/
72 unsigned long tear; /* 24-31: TEAR contents */
73 unsigned long dear; /* 32-39: DEAR contents */
74 unsigned int rsvrd0; /* 40-43: reserved */
75 unsigned int cpu_speed; /* 44-47: CPU speed */
76 unsigned long long rsvrd1; /* 48-55: reserved */
77 unsigned long long rsvrd2; /* 56-63: reserved */
78} __packed;
79
80/* SET SAMPLING CONTROLS request block */
81struct hws_lsctl_request_block {
82 unsigned int s:1; /* 0: maximum buffer indicator */
83 unsigned int h:1; /* 1: part. level reserved for VM use*/
84 unsigned long long b2_53:52;/* 2-53: zeros */
Hendrik Brueckner7e75fc32013-12-13 11:42:44 +010085 unsigned int es:1; /* 54: basic-sampling enable control */
86 unsigned int ed:1; /* 55: diag-sampling enable control */
87 unsigned int b56_61:6; /* 56-61: - zeros */
88 unsigned int cs:1; /* 62: basic-sampling activation control */
89 unsigned int cd:1; /* 63: diag-sampling activation control */
Hendrik Bruecknercf48ad82013-12-11 12:15:52 +010090 unsigned long interval; /* 8-15: sampling interval */
91 unsigned long tear; /* 16-23: TEAR contents */
92 unsigned long dear; /* 24-31: DEAR contents */
93 /* 32-63: */
94 unsigned long rsvrd1; /* reserved */
95 unsigned long rsvrd2; /* reserved */
96 unsigned long rsvrd3; /* reserved */
97 unsigned long rsvrd4; /* reserved */
98} __packed;
99
Hendrik Brueckner7e75fc32013-12-13 11:42:44 +0100100struct hws_basic_entry {
Hendrik Bruecknercf48ad82013-12-11 12:15:52 +0100101 unsigned int def:16; /* 0-15 Data Entry Format */
102 unsigned int R:4; /* 16-19 reserved */
103 unsigned int U:4; /* 20-23 Number of unique instruct. */
104 unsigned int z:2; /* zeros */
105 unsigned int T:1; /* 26 PSW DAT mode */
106 unsigned int W:1; /* 27 PSW wait state */
107 unsigned int P:1; /* 28 PSW Problem state */
108 unsigned int AS:2; /* 29-30 PSW address-space control */
109 unsigned int I:1; /* 31 entry valid or invalid */
Christian Borntraegerc19805f2016-11-08 09:53:34 +0100110 unsigned int CL:2; /* 32-33 Configuration Level */
111 unsigned int:14;
Hendrik Bruecknercf48ad82013-12-11 12:15:52 +0100112 unsigned int prim_asn:16; /* primary ASN */
113 unsigned long long ia; /* Instruction Address */
114 unsigned long long gpp; /* Guest Program Parameter */
115 unsigned long long hpp; /* Host Program Parameter */
116} __packed;
117
Hendrik Brueckner7e75fc32013-12-13 11:42:44 +0100118struct hws_diag_entry {
119 unsigned int def:16; /* 0-15 Data Entry Format */
120 unsigned int R:14; /* 16-19 and 20-30 reserved */
121 unsigned int I:1; /* 31 entry valid or invalid */
122 u8 data[]; /* Machine-dependent sample data */
123} __packed;
124
125struct hws_combined_entry {
126 struct hws_basic_entry basic; /* Basic-sampling data entry */
127 struct hws_diag_entry diag; /* Diagnostic-sampling data entry */
128} __packed;
129
Hendrik Bruecknercf48ad82013-12-11 12:15:52 +0100130struct hws_trailer_entry {
Hendrik Bruecknerfcc77f52013-12-12 17:26:51 +0100131 union {
132 struct {
133 unsigned int f:1; /* 0 - Block Full Indicator */
134 unsigned int a:1; /* 1 - Alert request control */
135 unsigned int t:1; /* 2 - Timestamp format */
136 unsigned long long:61; /* 3 - 63: Reserved */
137 };
138 unsigned long long flags; /* 0 - 63: All indicators */
139 };
Hendrik Bruecknercf48ad82013-12-11 12:15:52 +0100140 unsigned long long overflow; /* 64 - sample Overflow count */
Hendrik Brueckner443d4be2013-12-12 17:38:50 +0100141 unsigned char timestamp[16]; /* 16 - 31 timestamp */
Hendrik Bruecknercf48ad82013-12-11 12:15:52 +0100142 unsigned long long reserved1; /* 32 -Reserved */
143 unsigned long long reserved2; /* */
144 unsigned long long progusage1; /* 48 - reserved for programming use */
145 unsigned long long progusage2; /* */
146} __packed;
147
Hendrik Bruecknerd4c7e642017-10-27 15:45:19 +0200148/* Load program parameter */
149static inline void lpp(void *pp)
150{
151 asm volatile(".insn s,0xb2800000,0(%0)\n":: "a" (pp) : "memory");
152}
153
Hendrik Brueckner212188a2012-03-23 11:13:06 +0100154/* Query counter information */
155static inline int qctri(struct cpumf_ctr_info *info)
156{
157 int rc = -EINVAL;
158
159 asm volatile (
160 "0: .insn s,0xb28e0000,%1\n"
161 "1: lhi %0,0\n"
162 "2:\n"
163 EX_TABLE(1b, 2b)
164 : "+d" (rc), "=Q" (*info));
165 return rc;
166}
167
168/* Load CPU-counter-set controls */
169static inline int lcctl(u64 ctl)
170{
171 int cc;
172
173 asm volatile (
174 " .insn s,0xb2840000,%1\n"
175 " ipm %0\n"
176 " srl %0,28\n"
Vasily Gorbik11776ea2017-11-13 16:37:33 +0100177 : "=d" (cc) : "Q" (ctl) : "cc");
Hendrik Brueckner212188a2012-03-23 11:13:06 +0100178 return cc;
179}
180
181/* Extract CPU counter */
Heiko Carstense238c152016-06-20 14:06:54 +0200182static inline int __ecctr(u64 ctr, u64 *content)
Hendrik Brueckner212188a2012-03-23 11:13:06 +0100183{
Hendrik Brueckner26f268a2017-02-20 16:07:43 +0100184 u64 _content;
Hendrik Brueckner212188a2012-03-23 11:13:06 +0100185 int cc;
186
187 asm volatile (
188 " .insn rre,0xb2e40000,%0,%2\n"
189 " ipm %1\n"
190 " srl %1,28\n"
Heiko Carstense238c152016-06-20 14:06:54 +0200191 : "=d" (_content), "=d" (cc) : "d" (ctr) : "cc");
192 *content = _content;
193 return cc;
194}
195
196/* Extract CPU counter */
197static inline int ecctr(u64 ctr, u64 *val)
198{
199 u64 content;
200 int cc;
201
202 cc = __ecctr(ctr, &content);
Hendrik Brueckner212188a2012-03-23 11:13:06 +0100203 if (!cc)
204 *val = content;
205 return cc;
206}
207
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100208/* Store CPU counter multiple for the MT utilization counter set */
209static inline int stcctm5(u64 num, u64 *val)
210{
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100211 int cc;
212
213 asm volatile (
214 " .insn rsy,0xeb0000000017,%2,5,%1\n"
215 " ipm %0\n"
216 " srl %0,28\n"
Heiko Carstense3850ec2016-12-14 13:45:07 +0100217 : "=d" (cc)
218 : "Q" (*val), "d" (num)
219 : "cc", "memory");
Martin Schwidefsky10ad34b2015-01-14 17:52:10 +0100220 return cc;
221}
222
Hendrik Bruecknercf48ad82013-12-11 12:15:52 +0100223/* Query sampling information */
224static inline int qsi(struct hws_qsi_info_block *info)
225{
Heiko Carstens259acc52016-12-10 10:37:32 +0100226 int cc = 1;
Hendrik Bruecknercf48ad82013-12-11 12:15:52 +0100227
228 asm volatile(
Heiko Carstens259acc52016-12-10 10:37:32 +0100229 "0: .insn s,0xb2860000,%1\n"
Hendrik Bruecknercf48ad82013-12-11 12:15:52 +0100230 "1: lhi %0,0\n"
231 "2:\n"
232 EX_TABLE(0b, 2b) EX_TABLE(1b, 2b)
Heiko Carstens259acc52016-12-10 10:37:32 +0100233 : "+d" (cc), "+Q" (*info));
Hendrik Bruecknercf48ad82013-12-11 12:15:52 +0100234 return cc ? -EINVAL : 0;
235}
236
237/* Load sampling controls */
238static inline int lsctl(struct hws_lsctl_request_block *req)
239{
240 int cc;
241
242 cc = 1;
243 asm volatile(
244 "0: .insn s,0xb2870000,0(%1)\n"
245 "1: ipm %0\n"
246 " srl %0,28\n"
247 "2:\n"
248 EX_TABLE(0b, 2b) EX_TABLE(1b, 2b)
249 : "+d" (cc), "+a" (req)
250 : "m" (*req)
251 : "cc", "memory");
252
253 return cc ? -EINVAL : 0;
254}
255
256/* Sampling control helper functions */
257
Hendrik Brueckner8c069ff2013-12-12 16:32:47 +0100258#include <linux/time.h>
259
260static inline unsigned long freq_to_sample_rate(struct hws_qsi_info_block *qsi,
261 unsigned long freq)
262{
263 return (USEC_PER_SEC / freq) * qsi->cpu_speed;
264}
265
266static inline unsigned long sample_rate_to_freq(struct hws_qsi_info_block *qsi,
267 unsigned long rate)
268{
269 return USEC_PER_SEC * qsi->cpu_speed / rate;
270}
271
Hendrik Bruecknercf48ad82013-12-11 12:15:52 +0100272#define SDB_TE_ALERT_REQ_MASK 0x4000000000000000UL
273#define SDB_TE_BUFFER_FULL_MASK 0x8000000000000000UL
274
Hendrik Brueckner443d4be2013-12-12 17:38:50 +0100275/* Return TOD timestamp contained in an trailer entry */
276static inline unsigned long long trailer_timestamp(struct hws_trailer_entry *te)
277{
278 /* TOD in STCKE format */
279 if (te->t)
280 return *((unsigned long long *) &te->timestamp[1]);
281
282 /* TOD in STCK format */
283 return *((unsigned long long *) &te->timestamp[0]);
284}
285
Hendrik Bruecknercf48ad82013-12-11 12:15:52 +0100286/* Return pointer to trailer entry of an sample data block */
287static inline unsigned long *trailer_entry_ptr(unsigned long v)
288{
289 void *ret;
290
291 ret = (void *) v;
292 ret += PAGE_SIZE;
293 ret -= sizeof(struct hws_trailer_entry);
294
295 return (unsigned long *) ret;
296}
297
298/* Return if the entry in the sample data block table (sdbt)
299 * is a link to the next sdbt */
300static inline int is_link_entry(unsigned long *s)
301{
302 return *s & 0x1ul ? 1 : 0;
303}
304
305/* Return pointer to the linked sdbt */
306static inline unsigned long *get_next_sdbt(unsigned long *s)
307{
308 return (unsigned long *) (*s & ~0x1ul);
309}
Hendrik Brueckner212188a2012-03-23 11:13:06 +0100310#endif /* _ASM_S390_CPU_MF_H */