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