cpu_mf.h 9.3 KB

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