pcr.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* pcr.c: Generic sparc64 performance counter infrastructure.
  3. *
  4. * Copyright (C) 2009 David S. Miller ([email protected])
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/export.h>
  8. #include <linux/init.h>
  9. #include <linux/irq.h>
  10. #include <linux/irq_work.h>
  11. #include <linux/ftrace.h>
  12. #include <asm/pil.h>
  13. #include <asm/pcr.h>
  14. #include <asm/nmi.h>
  15. #include <asm/asi.h>
  16. #include <asm/spitfire.h>
  17. /* This code is shared between various users of the performance
  18. * counters. Users will be oprofile, pseudo-NMI watchdog, and the
  19. * perf_event support layer.
  20. */
  21. /* Performance counter interrupts run unmasked at PIL level 15.
  22. * Therefore we can't do things like wakeups and other work
  23. * that expects IRQ disabling to be adhered to in locking etc.
  24. *
  25. * Therefore in such situations we defer the work by signalling
  26. * a lower level cpu IRQ.
  27. */
  28. void __irq_entry deferred_pcr_work_irq(int irq, struct pt_regs *regs)
  29. {
  30. struct pt_regs *old_regs;
  31. clear_softint(1 << PIL_DEFERRED_PCR_WORK);
  32. old_regs = set_irq_regs(regs);
  33. irq_enter();
  34. #ifdef CONFIG_IRQ_WORK
  35. irq_work_run();
  36. #endif
  37. irq_exit();
  38. set_irq_regs(old_regs);
  39. }
  40. void arch_irq_work_raise(void)
  41. {
  42. set_softint(1 << PIL_DEFERRED_PCR_WORK);
  43. }
  44. const struct pcr_ops *pcr_ops;
  45. EXPORT_SYMBOL_GPL(pcr_ops);
  46. static u64 direct_pcr_read(unsigned long reg_num)
  47. {
  48. u64 val;
  49. WARN_ON_ONCE(reg_num != 0);
  50. __asm__ __volatile__("rd %%pcr, %0" : "=r" (val));
  51. return val;
  52. }
  53. static void direct_pcr_write(unsigned long reg_num, u64 val)
  54. {
  55. WARN_ON_ONCE(reg_num != 0);
  56. __asm__ __volatile__("wr %0, 0x0, %%pcr" : : "r" (val));
  57. }
  58. static u64 direct_pic_read(unsigned long reg_num)
  59. {
  60. u64 val;
  61. WARN_ON_ONCE(reg_num != 0);
  62. __asm__ __volatile__("rd %%pic, %0" : "=r" (val));
  63. return val;
  64. }
  65. static void direct_pic_write(unsigned long reg_num, u64 val)
  66. {
  67. WARN_ON_ONCE(reg_num != 0);
  68. /* Blackbird errata workaround. See commentary in
  69. * arch/sparc64/kernel/smp.c:smp_percpu_timer_interrupt()
  70. * for more information.
  71. */
  72. __asm__ __volatile__("ba,pt %%xcc, 99f\n\t"
  73. " nop\n\t"
  74. ".align 64\n"
  75. "99:wr %0, 0x0, %%pic\n\t"
  76. "rd %%pic, %%g0" : : "r" (val));
  77. }
  78. static u64 direct_picl_value(unsigned int nmi_hz)
  79. {
  80. u32 delta = local_cpu_data().clock_tick / nmi_hz;
  81. return ((u64)((0 - delta) & 0xffffffff)) << 32;
  82. }
  83. static const struct pcr_ops direct_pcr_ops = {
  84. .read_pcr = direct_pcr_read,
  85. .write_pcr = direct_pcr_write,
  86. .read_pic = direct_pic_read,
  87. .write_pic = direct_pic_write,
  88. .nmi_picl_value = direct_picl_value,
  89. .pcr_nmi_enable = (PCR_PIC_PRIV | PCR_STRACE | PCR_UTRACE),
  90. .pcr_nmi_disable = PCR_PIC_PRIV,
  91. };
  92. static void n2_pcr_write(unsigned long reg_num, u64 val)
  93. {
  94. unsigned long ret;
  95. WARN_ON_ONCE(reg_num != 0);
  96. if (val & PCR_N2_HTRACE) {
  97. ret = sun4v_niagara2_setperf(HV_N2_PERF_SPARC_CTL, val);
  98. if (ret != HV_EOK)
  99. direct_pcr_write(reg_num, val);
  100. } else
  101. direct_pcr_write(reg_num, val);
  102. }
  103. static u64 n2_picl_value(unsigned int nmi_hz)
  104. {
  105. u32 delta = local_cpu_data().clock_tick / (nmi_hz << 2);
  106. return ((u64)((0 - delta) & 0xffffffff)) << 32;
  107. }
  108. static const struct pcr_ops n2_pcr_ops = {
  109. .read_pcr = direct_pcr_read,
  110. .write_pcr = n2_pcr_write,
  111. .read_pic = direct_pic_read,
  112. .write_pic = direct_pic_write,
  113. .nmi_picl_value = n2_picl_value,
  114. .pcr_nmi_enable = (PCR_PIC_PRIV | PCR_STRACE | PCR_UTRACE |
  115. PCR_N2_TOE_OV1 |
  116. (2 << PCR_N2_SL1_SHIFT) |
  117. (0xff << PCR_N2_MASK1_SHIFT)),
  118. .pcr_nmi_disable = PCR_PIC_PRIV,
  119. };
  120. static u64 n4_pcr_read(unsigned long reg_num)
  121. {
  122. unsigned long val;
  123. (void) sun4v_vt_get_perfreg(reg_num, &val);
  124. return val;
  125. }
  126. static void n4_pcr_write(unsigned long reg_num, u64 val)
  127. {
  128. (void) sun4v_vt_set_perfreg(reg_num, val);
  129. }
  130. static u64 n4_pic_read(unsigned long reg_num)
  131. {
  132. unsigned long val;
  133. __asm__ __volatile__("ldxa [%1] %2, %0"
  134. : "=r" (val)
  135. : "r" (reg_num * 0x8UL), "i" (ASI_PIC));
  136. return val;
  137. }
  138. static void n4_pic_write(unsigned long reg_num, u64 val)
  139. {
  140. __asm__ __volatile__("stxa %0, [%1] %2"
  141. : /* no outputs */
  142. : "r" (val), "r" (reg_num * 0x8UL), "i" (ASI_PIC));
  143. }
  144. static u64 n4_picl_value(unsigned int nmi_hz)
  145. {
  146. u32 delta = local_cpu_data().clock_tick / (nmi_hz << 2);
  147. return ((u64)((0 - delta) & 0xffffffff));
  148. }
  149. static const struct pcr_ops n4_pcr_ops = {
  150. .read_pcr = n4_pcr_read,
  151. .write_pcr = n4_pcr_write,
  152. .read_pic = n4_pic_read,
  153. .write_pic = n4_pic_write,
  154. .nmi_picl_value = n4_picl_value,
  155. .pcr_nmi_enable = (PCR_N4_PICNPT | PCR_N4_STRACE |
  156. PCR_N4_UTRACE | PCR_N4_TOE |
  157. (26 << PCR_N4_SL_SHIFT)),
  158. .pcr_nmi_disable = PCR_N4_PICNPT,
  159. };
  160. static u64 n5_pcr_read(unsigned long reg_num)
  161. {
  162. unsigned long val;
  163. (void) sun4v_t5_get_perfreg(reg_num, &val);
  164. return val;
  165. }
  166. static void n5_pcr_write(unsigned long reg_num, u64 val)
  167. {
  168. (void) sun4v_t5_set_perfreg(reg_num, val);
  169. }
  170. static const struct pcr_ops n5_pcr_ops = {
  171. .read_pcr = n5_pcr_read,
  172. .write_pcr = n5_pcr_write,
  173. .read_pic = n4_pic_read,
  174. .write_pic = n4_pic_write,
  175. .nmi_picl_value = n4_picl_value,
  176. .pcr_nmi_enable = (PCR_N4_PICNPT | PCR_N4_STRACE |
  177. PCR_N4_UTRACE | PCR_N4_TOE |
  178. (26 << PCR_N4_SL_SHIFT)),
  179. .pcr_nmi_disable = PCR_N4_PICNPT,
  180. };
  181. static u64 m7_pcr_read(unsigned long reg_num)
  182. {
  183. unsigned long val;
  184. (void) sun4v_m7_get_perfreg(reg_num, &val);
  185. return val;
  186. }
  187. static void m7_pcr_write(unsigned long reg_num, u64 val)
  188. {
  189. (void) sun4v_m7_set_perfreg(reg_num, val);
  190. }
  191. static const struct pcr_ops m7_pcr_ops = {
  192. .read_pcr = m7_pcr_read,
  193. .write_pcr = m7_pcr_write,
  194. .read_pic = n4_pic_read,
  195. .write_pic = n4_pic_write,
  196. .nmi_picl_value = n4_picl_value,
  197. .pcr_nmi_enable = (PCR_N4_PICNPT | PCR_N4_STRACE |
  198. PCR_N4_UTRACE | PCR_N4_TOE |
  199. (26 << PCR_N4_SL_SHIFT)),
  200. .pcr_nmi_disable = PCR_N4_PICNPT,
  201. };
  202. static unsigned long perf_hsvc_group;
  203. static unsigned long perf_hsvc_major;
  204. static unsigned long perf_hsvc_minor;
  205. static int __init register_perf_hsvc(void)
  206. {
  207. unsigned long hverror;
  208. if (tlb_type == hypervisor) {
  209. switch (sun4v_chip_type) {
  210. case SUN4V_CHIP_NIAGARA1:
  211. perf_hsvc_group = HV_GRP_NIAG_PERF;
  212. break;
  213. case SUN4V_CHIP_NIAGARA2:
  214. perf_hsvc_group = HV_GRP_N2_CPU;
  215. break;
  216. case SUN4V_CHIP_NIAGARA3:
  217. perf_hsvc_group = HV_GRP_KT_CPU;
  218. break;
  219. case SUN4V_CHIP_NIAGARA4:
  220. perf_hsvc_group = HV_GRP_VT_CPU;
  221. break;
  222. case SUN4V_CHIP_NIAGARA5:
  223. perf_hsvc_group = HV_GRP_T5_CPU;
  224. break;
  225. case SUN4V_CHIP_SPARC_M7:
  226. perf_hsvc_group = HV_GRP_M7_PERF;
  227. break;
  228. default:
  229. return -ENODEV;
  230. }
  231. perf_hsvc_major = 1;
  232. perf_hsvc_minor = 0;
  233. hverror = sun4v_hvapi_register(perf_hsvc_group,
  234. perf_hsvc_major,
  235. &perf_hsvc_minor);
  236. if (hverror) {
  237. pr_err("perfmon: Could not register hvapi(0x%lx).\n",
  238. hverror);
  239. return -ENODEV;
  240. }
  241. }
  242. return 0;
  243. }
  244. static void __init unregister_perf_hsvc(void)
  245. {
  246. if (tlb_type != hypervisor)
  247. return;
  248. sun4v_hvapi_unregister(perf_hsvc_group);
  249. }
  250. static int __init setup_sun4v_pcr_ops(void)
  251. {
  252. int ret = 0;
  253. switch (sun4v_chip_type) {
  254. case SUN4V_CHIP_NIAGARA1:
  255. case SUN4V_CHIP_NIAGARA2:
  256. case SUN4V_CHIP_NIAGARA3:
  257. pcr_ops = &n2_pcr_ops;
  258. break;
  259. case SUN4V_CHIP_NIAGARA4:
  260. pcr_ops = &n4_pcr_ops;
  261. break;
  262. case SUN4V_CHIP_NIAGARA5:
  263. pcr_ops = &n5_pcr_ops;
  264. break;
  265. case SUN4V_CHIP_SPARC_M7:
  266. pcr_ops = &m7_pcr_ops;
  267. break;
  268. default:
  269. ret = -ENODEV;
  270. break;
  271. }
  272. return ret;
  273. }
  274. int __init pcr_arch_init(void)
  275. {
  276. int err = register_perf_hsvc();
  277. if (err)
  278. return err;
  279. switch (tlb_type) {
  280. case hypervisor:
  281. err = setup_sun4v_pcr_ops();
  282. if (err)
  283. goto out_unregister;
  284. break;
  285. case cheetah:
  286. case cheetah_plus:
  287. pcr_ops = &direct_pcr_ops;
  288. break;
  289. case spitfire:
  290. /* UltraSPARC-I/II and derivatives lack a profile
  291. * counter overflow interrupt so we can't make use of
  292. * their hardware currently.
  293. */
  294. fallthrough;
  295. default:
  296. err = -ENODEV;
  297. goto out_unregister;
  298. }
  299. return nmi_init();
  300. out_unregister:
  301. unregister_perf_hsvc();
  302. return err;
  303. }