cpu_mf.h 9.0 KB

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