Greg Kroah-Hartman | 0b73214 | 2017-11-24 15:00:38 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Hendrik Brueckner | 212188a | 2012-03-23 11:13:06 +0100 | [diff] [blame] | 2 | /* |
| 3 | * CPU-measurement facilities |
| 4 | * |
Hendrik Brueckner | 5223c67 | 2018-07-16 11:08:06 +0200 | [diff] [blame] | 5 | * Copyright IBM Corp. 2012, 2018 |
Hendrik Brueckner | 212188a | 2012-03-23 11:13:06 +0100 | [diff] [blame] | 6 | * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com> |
| 7 | * Jan Glauber <jang@linux.vnet.ibm.com> |
Hendrik Brueckner | 212188a | 2012-03-23 11:13:06 +0100 | [diff] [blame] | 8 | */ |
Jan Glauber | b03d541a | 2012-03-23 11:13:05 +0100 | [diff] [blame] | 9 | #ifndef _ASM_S390_CPU_MF_H |
| 10 | #define _ASM_S390_CPU_MF_H |
| 11 | |
Li Zefan | cb16b91 | 2013-03-07 17:35:53 +0800 | [diff] [blame] | 12 | #include <linux/errno.h> |
Heiko Carstens | 1e3cab2 | 2012-03-30 09:40:55 +0200 | [diff] [blame] | 13 | #include <asm/facility.h> |
| 14 | |
Hendrik Brueckner | 778fb10 | 2018-08-29 17:46:06 +0200 | [diff] [blame] | 15 | asm(".include \"asm/cpu_mf-insn.h\"\n"); |
| 16 | |
Jan Glauber | b03d541a | 2012-03-23 11:13:05 +0100 | [diff] [blame] | 17 | #define CPU_MF_INT_SF_IAE (1 << 31) /* invalid entry address */ |
| 18 | #define CPU_MF_INT_SF_ISE (1 << 30) /* incorrect SDBT entry */ |
| 19 | #define CPU_MF_INT_SF_PRA (1 << 29) /* program request alert */ |
| 20 | #define CPU_MF_INT_SF_SACA (1 << 23) /* sampler auth. change alert */ |
| 21 | #define CPU_MF_INT_SF_LSDA (1 << 22) /* loss of sample data alert */ |
Hendrik Brueckner | ee699f3 | 2016-06-03 16:55:03 +0200 | [diff] [blame] | 22 | #define CPU_MF_INT_CF_MTDA (1 << 15) /* loss of MT ctr. data alert */ |
Hendrik Brueckner | 212188a | 2012-03-23 11:13:06 +0100 | [diff] [blame] | 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 */ |
Hendrik Brueckner | ee699f3 | 2016-06-03 16:55:03 +0200 | [diff] [blame] | 25 | #define CPU_MF_INT_CF_MASK (CPU_MF_INT_CF_MTDA|CPU_MF_INT_CF_CACA| \ |
| 26 | CPU_MF_INT_CF_LCDA) |
Hendrik Brueckner | 212188a | 2012-03-23 11:13:06 +0100 | [diff] [blame] | 27 | #define CPU_MF_INT_SF_MASK (CPU_MF_INT_SF_IAE|CPU_MF_INT_SF_ISE| \ |
| 28 | CPU_MF_INT_SF_PRA|CPU_MF_INT_SF_SACA| \ |
| 29 | CPU_MF_INT_SF_LSDA) |
| 30 | |
Thomas Richter | 932bfc5 | 2019-09-20 11:57:43 +0200 | [diff] [blame] | 31 | #define CPU_MF_SF_RIBM_NOTAV 0x1 /* Sampling unavailable */ |
| 32 | |
Hendrik Brueckner | 212188a | 2012-03-23 11:13:06 +0100 | [diff] [blame] | 33 | /* CPU measurement facility support */ |
| 34 | static inline int cpum_cf_avail(void) |
| 35 | { |
Martin Schwidefsky | e5b9819 | 2018-03-26 15:23:33 +0200 | [diff] [blame] | 36 | return test_facility(40) && test_facility(67); |
Hendrik Brueckner | 212188a | 2012-03-23 11:13:06 +0100 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static inline int cpum_sf_avail(void) |
| 40 | { |
Martin Schwidefsky | e5b9819 | 2018-03-26 15:23:33 +0200 | [diff] [blame] | 41 | return test_facility(40) && test_facility(68); |
Hendrik Brueckner | 212188a | 2012-03-23 11:13:06 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | |
| 45 | struct 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 Brueckner | cf48ad8 | 2013-12-11 12:15:52 +0100 | [diff] [blame] | 57 | /* QUERY SAMPLING INFORMATION block */ |
| 58 | struct hws_qsi_info_block { /* Bit(s) */ |
| 59 | unsigned int b0_13:14; /* 0-13: zeros */ |
Hendrik Brueckner | 7e75fc3 | 2013-12-13 11:42:44 +0100 | [diff] [blame] | 60 | 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 Brueckner | cf48ad8 | 2013-12-11 12:15:52 +0100 | [diff] [blame] | 69 | 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 */ |
Thomas Richter | 932bfc5 | 2019-09-20 11:57:43 +0200 | [diff] [blame] | 74 | unsigned int rsvrd0:24; /* 40-42: reserved */ |
| 75 | unsigned int ribm:8; /* 43: Reserved by IBM */ |
Thomas Richter | 93426ca | 2019-09-16 11:31:43 +0200 | [diff] [blame] | 76 | unsigned int cpu_speed; /* 44-47: CPU speed */ |
Hendrik Brueckner | cf48ad8 | 2013-12-11 12:15:52 +0100 | [diff] [blame] | 77 | unsigned long long rsvrd1; /* 48-55: reserved */ |
| 78 | unsigned long long rsvrd2; /* 56-63: reserved */ |
| 79 | } __packed; |
| 80 | |
| 81 | /* SET SAMPLING CONTROLS request block */ |
| 82 | struct hws_lsctl_request_block { |
| 83 | unsigned int s:1; /* 0: maximum buffer indicator */ |
| 84 | unsigned int h:1; /* 1: part. level reserved for VM use*/ |
| 85 | unsigned long long b2_53:52;/* 2-53: zeros */ |
Hendrik Brueckner | 7e75fc3 | 2013-12-13 11:42:44 +0100 | [diff] [blame] | 86 | unsigned int es:1; /* 54: basic-sampling enable control */ |
| 87 | unsigned int ed:1; /* 55: diag-sampling enable control */ |
| 88 | unsigned int b56_61:6; /* 56-61: - zeros */ |
| 89 | unsigned int cs:1; /* 62: basic-sampling activation control */ |
| 90 | unsigned int cd:1; /* 63: diag-sampling activation control */ |
Hendrik Brueckner | cf48ad8 | 2013-12-11 12:15:52 +0100 | [diff] [blame] | 91 | unsigned long interval; /* 8-15: sampling interval */ |
| 92 | unsigned long tear; /* 16-23: TEAR contents */ |
| 93 | unsigned long dear; /* 24-31: DEAR contents */ |
| 94 | /* 32-63: */ |
Thomas Richter | 93426ca | 2019-09-16 11:31:43 +0200 | [diff] [blame] | 95 | unsigned long rsvrd1; /* reserved */ |
| 96 | unsigned long rsvrd2; /* reserved */ |
| 97 | unsigned long rsvrd3; /* reserved */ |
| 98 | unsigned long rsvrd4; /* reserved */ |
Hendrik Brueckner | cf48ad8 | 2013-12-11 12:15:52 +0100 | [diff] [blame] | 99 | } __packed; |
| 100 | |
Hendrik Brueckner | 7e75fc3 | 2013-12-13 11:42:44 +0100 | [diff] [blame] | 101 | struct hws_basic_entry { |
Hendrik Brueckner | cf48ad8 | 2013-12-11 12:15:52 +0100 | [diff] [blame] | 102 | unsigned int def:16; /* 0-15 Data Entry Format */ |
| 103 | unsigned int R:4; /* 16-19 reserved */ |
| 104 | unsigned int U:4; /* 20-23 Number of unique instruct. */ |
| 105 | unsigned int z:2; /* zeros */ |
| 106 | unsigned int T:1; /* 26 PSW DAT mode */ |
| 107 | unsigned int W:1; /* 27 PSW wait state */ |
| 108 | unsigned int P:1; /* 28 PSW Problem state */ |
| 109 | unsigned int AS:2; /* 29-30 PSW address-space control */ |
| 110 | unsigned int I:1; /* 31 entry valid or invalid */ |
Christian Borntraeger | c19805f | 2016-11-08 09:53:34 +0100 | [diff] [blame] | 111 | unsigned int CL:2; /* 32-33 Configuration Level */ |
Thomas Richter | 745f5d2 | 2022-01-13 12:51:56 +0100 | [diff] [blame] | 112 | unsigned int H:1; /* 34 Host Indicator */ |
| 113 | unsigned int LS:1; /* 35 Limited Sampling */ |
| 114 | unsigned int:12; |
Hendrik Brueckner | cf48ad8 | 2013-12-11 12:15:52 +0100 | [diff] [blame] | 115 | unsigned int prim_asn:16; /* primary ASN */ |
| 116 | unsigned long long ia; /* Instruction Address */ |
| 117 | unsigned long long gpp; /* Guest Program Parameter */ |
| 118 | unsigned long long hpp; /* Host Program Parameter */ |
| 119 | } __packed; |
| 120 | |
Hendrik Brueckner | 7e75fc3 | 2013-12-13 11:42:44 +0100 | [diff] [blame] | 121 | struct hws_diag_entry { |
| 122 | unsigned int def:16; /* 0-15 Data Entry Format */ |
Thomas Richter | 3c0a83b | 2018-05-08 07:53:39 +0200 | [diff] [blame] | 123 | unsigned int R:15; /* 16-19 and 20-30 reserved */ |
Hendrik Brueckner | 7e75fc3 | 2013-12-13 11:42:44 +0100 | [diff] [blame] | 124 | unsigned int I:1; /* 31 entry valid or invalid */ |
| 125 | u8 data[]; /* Machine-dependent sample data */ |
| 126 | } __packed; |
| 127 | |
| 128 | struct hws_combined_entry { |
| 129 | struct hws_basic_entry basic; /* Basic-sampling data entry */ |
| 130 | struct hws_diag_entry diag; /* Diagnostic-sampling data entry */ |
| 131 | } __packed; |
| 132 | |
Hendrik Brueckner | cf48ad8 | 2013-12-11 12:15:52 +0100 | [diff] [blame] | 133 | struct hws_trailer_entry { |
Hendrik Brueckner | fcc77f5 | 2013-12-12 17:26:51 +0100 | [diff] [blame] | 134 | union { |
| 135 | struct { |
| 136 | unsigned int f:1; /* 0 - Block Full Indicator */ |
| 137 | unsigned int a:1; /* 1 - Alert request control */ |
| 138 | unsigned int t:1; /* 2 - Timestamp format */ |
Thomas Richter | 77715b7 | 2018-05-08 10:18:39 +0200 | [diff] [blame] | 139 | unsigned int :29; /* 3 - 31: Reserved */ |
| 140 | unsigned int bsdes:16; /* 32-47: size of basic SDE */ |
| 141 | unsigned int dsdes:16; /* 48-63: size of diagnostic SDE */ |
Hendrik Brueckner | fcc77f5 | 2013-12-12 17:26:51 +0100 | [diff] [blame] | 142 | }; |
| 143 | unsigned long long flags; /* 0 - 63: All indicators */ |
| 144 | }; |
Hendrik Brueckner | cf48ad8 | 2013-12-11 12:15:52 +0100 | [diff] [blame] | 145 | unsigned long long overflow; /* 64 - sample Overflow count */ |
Hendrik Brueckner | 443d4be | 2013-12-12 17:38:50 +0100 | [diff] [blame] | 146 | unsigned char timestamp[16]; /* 16 - 31 timestamp */ |
Hendrik Brueckner | cf48ad8 | 2013-12-11 12:15:52 +0100 | [diff] [blame] | 147 | unsigned long long reserved1; /* 32 -Reserved */ |
| 148 | unsigned long long reserved2; /* */ |
Hendrik Brueckner | 5223c67 | 2018-07-16 11:08:06 +0200 | [diff] [blame] | 149 | union { /* 48 - reserved for programming use */ |
| 150 | struct { |
| 151 | unsigned int clock_base:1; /* in progusage2 */ |
| 152 | unsigned long long progusage1:63; |
| 153 | unsigned long long progusage2; |
| 154 | }; |
| 155 | unsigned long long progusage[2]; |
| 156 | }; |
Hendrik Brueckner | cf48ad8 | 2013-12-11 12:15:52 +0100 | [diff] [blame] | 157 | } __packed; |
| 158 | |
Hendrik Brueckner | d4c7e64 | 2017-10-27 15:45:19 +0200 | [diff] [blame] | 159 | /* Load program parameter */ |
| 160 | static inline void lpp(void *pp) |
| 161 | { |
| 162 | asm volatile(".insn s,0xb2800000,0(%0)\n":: "a" (pp) : "memory"); |
| 163 | } |
| 164 | |
Hendrik Brueckner | 212188a | 2012-03-23 11:13:06 +0100 | [diff] [blame] | 165 | /* Query counter information */ |
| 166 | static inline int qctri(struct cpumf_ctr_info *info) |
| 167 | { |
| 168 | int rc = -EINVAL; |
| 169 | |
| 170 | asm volatile ( |
| 171 | "0: .insn s,0xb28e0000,%1\n" |
| 172 | "1: lhi %0,0\n" |
| 173 | "2:\n" |
| 174 | EX_TABLE(1b, 2b) |
| 175 | : "+d" (rc), "=Q" (*info)); |
| 176 | return rc; |
| 177 | } |
| 178 | |
| 179 | /* Load CPU-counter-set controls */ |
| 180 | static inline int lcctl(u64 ctl) |
| 181 | { |
| 182 | int cc; |
| 183 | |
| 184 | asm volatile ( |
| 185 | " .insn s,0xb2840000,%1\n" |
| 186 | " ipm %0\n" |
| 187 | " srl %0,28\n" |
Vasily Gorbik | 11776ea | 2017-11-13 16:37:33 +0100 | [diff] [blame] | 188 | : "=d" (cc) : "Q" (ctl) : "cc"); |
Hendrik Brueckner | 212188a | 2012-03-23 11:13:06 +0100 | [diff] [blame] | 189 | return cc; |
| 190 | } |
| 191 | |
| 192 | /* Extract CPU counter */ |
Heiko Carstens | e238c15 | 2016-06-20 14:06:54 +0200 | [diff] [blame] | 193 | static inline int __ecctr(u64 ctr, u64 *content) |
Hendrik Brueckner | 212188a | 2012-03-23 11:13:06 +0100 | [diff] [blame] | 194 | { |
Hendrik Brueckner | 26f268a | 2017-02-20 16:07:43 +0100 | [diff] [blame] | 195 | u64 _content; |
Hendrik Brueckner | 212188a | 2012-03-23 11:13:06 +0100 | [diff] [blame] | 196 | int cc; |
| 197 | |
| 198 | asm volatile ( |
| 199 | " .insn rre,0xb2e40000,%0,%2\n" |
| 200 | " ipm %1\n" |
| 201 | " srl %1,28\n" |
Heiko Carstens | e238c15 | 2016-06-20 14:06:54 +0200 | [diff] [blame] | 202 | : "=d" (_content), "=d" (cc) : "d" (ctr) : "cc"); |
| 203 | *content = _content; |
| 204 | return cc; |
| 205 | } |
| 206 | |
| 207 | /* Extract CPU counter */ |
| 208 | static inline int ecctr(u64 ctr, u64 *val) |
| 209 | { |
| 210 | u64 content; |
| 211 | int cc; |
| 212 | |
| 213 | cc = __ecctr(ctr, &content); |
Hendrik Brueckner | 212188a | 2012-03-23 11:13:06 +0100 | [diff] [blame] | 214 | if (!cc) |
| 215 | *val = content; |
| 216 | return cc; |
| 217 | } |
| 218 | |
Hendrik Brueckner | 778fb10 | 2018-08-29 17:46:06 +0200 | [diff] [blame] | 219 | /* Store CPU counter multiple for a particular counter set */ |
Hendrik Brueckner | 346d034 | 2018-08-29 18:12:17 +0200 | [diff] [blame] | 220 | enum stcctm_ctr_set { |
| 221 | EXTENDED = 0, |
| 222 | BASIC = 1, |
| 223 | PROBLEM_STATE = 2, |
| 224 | CRYPTO_ACTIVITY = 3, |
| 225 | MT_DIAG = 5, |
| 226 | MT_DIAG_CLEARING = 9, /* clears loss-of-MT-ctr-data alert */ |
| 227 | }; |
Heiko Carstens | 44967bfb | 2019-10-04 12:29:00 +0200 | [diff] [blame] | 228 | |
| 229 | static __always_inline int stcctm(enum stcctm_ctr_set set, u64 range, u64 *dest) |
Hendrik Brueckner | 778fb10 | 2018-08-29 17:46:06 +0200 | [diff] [blame] | 230 | { |
| 231 | int cc; |
| 232 | |
| 233 | asm volatile ( |
| 234 | " STCCTM %2,%3,%1\n" |
| 235 | " ipm %0\n" |
| 236 | " srl %0,28\n" |
| 237 | : "=d" (cc) |
| 238 | : "Q" (*dest), "d" (range), "i" (set) |
| 239 | : "cc", "memory"); |
| 240 | return cc; |
| 241 | } |
| 242 | |
Hendrik Brueckner | cf48ad8 | 2013-12-11 12:15:52 +0100 | [diff] [blame] | 243 | /* Query sampling information */ |
| 244 | static inline int qsi(struct hws_qsi_info_block *info) |
| 245 | { |
Heiko Carstens | 259acc5 | 2016-12-10 10:37:32 +0100 | [diff] [blame] | 246 | int cc = 1; |
Hendrik Brueckner | cf48ad8 | 2013-12-11 12:15:52 +0100 | [diff] [blame] | 247 | |
| 248 | asm volatile( |
Heiko Carstens | 259acc5 | 2016-12-10 10:37:32 +0100 | [diff] [blame] | 249 | "0: .insn s,0xb2860000,%1\n" |
Hendrik Brueckner | cf48ad8 | 2013-12-11 12:15:52 +0100 | [diff] [blame] | 250 | "1: lhi %0,0\n" |
| 251 | "2:\n" |
| 252 | EX_TABLE(0b, 2b) EX_TABLE(1b, 2b) |
Heiko Carstens | 259acc5 | 2016-12-10 10:37:32 +0100 | [diff] [blame] | 253 | : "+d" (cc), "+Q" (*info)); |
Hendrik Brueckner | cf48ad8 | 2013-12-11 12:15:52 +0100 | [diff] [blame] | 254 | return cc ? -EINVAL : 0; |
| 255 | } |
| 256 | |
| 257 | /* Load sampling controls */ |
| 258 | static inline int lsctl(struct hws_lsctl_request_block *req) |
| 259 | { |
| 260 | int cc; |
| 261 | |
| 262 | cc = 1; |
| 263 | asm volatile( |
| 264 | "0: .insn s,0xb2870000,0(%1)\n" |
| 265 | "1: ipm %0\n" |
| 266 | " srl %0,28\n" |
| 267 | "2:\n" |
| 268 | EX_TABLE(0b, 2b) EX_TABLE(1b, 2b) |
| 269 | : "+d" (cc), "+a" (req) |
| 270 | : "m" (*req) |
| 271 | : "cc", "memory"); |
| 272 | |
| 273 | return cc ? -EINVAL : 0; |
| 274 | } |
| 275 | |
| 276 | /* Sampling control helper functions */ |
| 277 | |
Hendrik Brueckner | 8c069ff | 2013-12-12 16:32:47 +0100 | [diff] [blame] | 278 | #include <linux/time.h> |
| 279 | |
| 280 | static inline unsigned long freq_to_sample_rate(struct hws_qsi_info_block *qsi, |
| 281 | unsigned long freq) |
| 282 | { |
| 283 | return (USEC_PER_SEC / freq) * qsi->cpu_speed; |
| 284 | } |
| 285 | |
| 286 | static inline unsigned long sample_rate_to_freq(struct hws_qsi_info_block *qsi, |
| 287 | unsigned long rate) |
| 288 | { |
| 289 | return USEC_PER_SEC * qsi->cpu_speed / rate; |
| 290 | } |
| 291 | |
Hendrik Brueckner | cf48ad8 | 2013-12-11 12:15:52 +0100 | [diff] [blame] | 292 | #define SDB_TE_ALERT_REQ_MASK 0x4000000000000000UL |
| 293 | #define SDB_TE_BUFFER_FULL_MASK 0x8000000000000000UL |
| 294 | |
Hendrik Brueckner | 443d4be | 2013-12-12 17:38:50 +0100 | [diff] [blame] | 295 | /* Return TOD timestamp contained in an trailer entry */ |
| 296 | static inline unsigned long long trailer_timestamp(struct hws_trailer_entry *te) |
| 297 | { |
| 298 | /* TOD in STCKE format */ |
| 299 | if (te->t) |
| 300 | return *((unsigned long long *) &te->timestamp[1]); |
| 301 | |
| 302 | /* TOD in STCK format */ |
| 303 | return *((unsigned long long *) &te->timestamp[0]); |
| 304 | } |
| 305 | |
Hendrik Brueckner | cf48ad8 | 2013-12-11 12:15:52 +0100 | [diff] [blame] | 306 | /* Return pointer to trailer entry of an sample data block */ |
| 307 | static inline unsigned long *trailer_entry_ptr(unsigned long v) |
| 308 | { |
| 309 | void *ret; |
| 310 | |
| 311 | ret = (void *) v; |
| 312 | ret += PAGE_SIZE; |
| 313 | ret -= sizeof(struct hws_trailer_entry); |
| 314 | |
| 315 | return (unsigned long *) ret; |
| 316 | } |
| 317 | |
Thomas Richter | 532da3d | 2019-11-21 15:46:02 +0100 | [diff] [blame] | 318 | /* Return true if the entry in the sample data block table (sdbt) |
Hendrik Brueckner | cf48ad8 | 2013-12-11 12:15:52 +0100 | [diff] [blame] | 319 | * is a link to the next sdbt */ |
| 320 | static inline int is_link_entry(unsigned long *s) |
| 321 | { |
| 322 | return *s & 0x1ul ? 1 : 0; |
| 323 | } |
| 324 | |
| 325 | /* Return pointer to the linked sdbt */ |
| 326 | static inline unsigned long *get_next_sdbt(unsigned long *s) |
| 327 | { |
| 328 | return (unsigned long *) (*s & ~0x1ul); |
| 329 | } |
Hendrik Brueckner | 212188a | 2012-03-23 11:13:06 +0100 | [diff] [blame] | 330 | #endif /* _ASM_S390_CPU_MF_H */ |