pcr.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* pcr.c: Generic sparc64 performance counter infrastructure.
  2. *
  3. * Copyright (C) 2009 David S. Miller (davem@davemloft.net)
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/irq.h>
  9. #include <linux/irq_work.h>
  10. #include <linux/ftrace.h>
  11. #include <asm/pil.h>
  12. #include <asm/pcr.h>
  13. #include <asm/nmi.h>
  14. /* This code is shared between various users of the performance
  15. * counters. Users will be oprofile, pseudo-NMI watchdog, and the
  16. * perf_event support layer.
  17. */
  18. #define PCR_SUN4U_ENABLE (PCR_PIC_PRIV | PCR_STRACE | PCR_UTRACE)
  19. #define PCR_N2_ENABLE (PCR_PIC_PRIV | PCR_STRACE | PCR_UTRACE | \
  20. PCR_N2_TOE_OV1 | \
  21. (2 << PCR_N2_SL1_SHIFT) | \
  22. (0xff << PCR_N2_MASK1_SHIFT))
  23. u64 pcr_enable;
  24. unsigned int picl_shift;
  25. /* Performance counter interrupts run unmasked at PIL level 15.
  26. * Therefore we can't do things like wakeups and other work
  27. * that expects IRQ disabling to be adhered to in locking etc.
  28. *
  29. * Therefore in such situations we defer the work by signalling
  30. * a lower level cpu IRQ.
  31. */
  32. void __irq_entry deferred_pcr_work_irq(int irq, struct pt_regs *regs)
  33. {
  34. struct pt_regs *old_regs;
  35. clear_softint(1 << PIL_DEFERRED_PCR_WORK);
  36. old_regs = set_irq_regs(regs);
  37. irq_enter();
  38. #ifdef CONFIG_IRQ_WORK
  39. irq_work_run();
  40. #endif
  41. irq_exit();
  42. set_irq_regs(old_regs);
  43. }
  44. void arch_irq_work_raise(void)
  45. {
  46. set_softint(1 << PIL_DEFERRED_PCR_WORK);
  47. }
  48. const struct pcr_ops *pcr_ops;
  49. EXPORT_SYMBOL_GPL(pcr_ops);
  50. static u64 direct_pcr_read(void)
  51. {
  52. u64 val;
  53. read_pcr(val);
  54. return val;
  55. }
  56. static void direct_pcr_write(u64 val)
  57. {
  58. write_pcr(val);
  59. }
  60. static const struct pcr_ops direct_pcr_ops = {
  61. .read = direct_pcr_read,
  62. .write = direct_pcr_write,
  63. };
  64. static void n2_pcr_write(u64 val)
  65. {
  66. unsigned long ret;
  67. if (val & PCR_N2_HTRACE) {
  68. ret = sun4v_niagara2_setperf(HV_N2_PERF_SPARC_CTL, val);
  69. if (ret != HV_EOK)
  70. write_pcr(val);
  71. } else
  72. write_pcr(val);
  73. }
  74. static const struct pcr_ops n2_pcr_ops = {
  75. .read = direct_pcr_read,
  76. .write = n2_pcr_write,
  77. };
  78. static unsigned long perf_hsvc_group;
  79. static unsigned long perf_hsvc_major;
  80. static unsigned long perf_hsvc_minor;
  81. static int __init register_perf_hsvc(void)
  82. {
  83. if (tlb_type == hypervisor) {
  84. switch (sun4v_chip_type) {
  85. case SUN4V_CHIP_NIAGARA1:
  86. perf_hsvc_group = HV_GRP_NIAG_PERF;
  87. break;
  88. case SUN4V_CHIP_NIAGARA2:
  89. perf_hsvc_group = HV_GRP_N2_CPU;
  90. break;
  91. case SUN4V_CHIP_NIAGARA3:
  92. perf_hsvc_group = HV_GRP_KT_CPU;
  93. break;
  94. default:
  95. return -ENODEV;
  96. }
  97. perf_hsvc_major = 1;
  98. perf_hsvc_minor = 0;
  99. if (sun4v_hvapi_register(perf_hsvc_group,
  100. perf_hsvc_major,
  101. &perf_hsvc_minor)) {
  102. printk("perfmon: Could not register hvapi.\n");
  103. return -ENODEV;
  104. }
  105. }
  106. return 0;
  107. }
  108. static void __init unregister_perf_hsvc(void)
  109. {
  110. if (tlb_type != hypervisor)
  111. return;
  112. sun4v_hvapi_unregister(perf_hsvc_group);
  113. }
  114. int __init pcr_arch_init(void)
  115. {
  116. int err = register_perf_hsvc();
  117. if (err)
  118. return err;
  119. switch (tlb_type) {
  120. case hypervisor:
  121. pcr_ops = &n2_pcr_ops;
  122. pcr_enable = PCR_N2_ENABLE;
  123. picl_shift = 2;
  124. break;
  125. case cheetah:
  126. case cheetah_plus:
  127. pcr_ops = &direct_pcr_ops;
  128. pcr_enable = PCR_SUN4U_ENABLE;
  129. break;
  130. case spitfire:
  131. /* UltraSPARC-I/II and derivatives lack a profile
  132. * counter overflow interrupt so we can't make use of
  133. * their hardware currently.
  134. */
  135. /* fallthrough */
  136. default:
  137. err = -ENODEV;
  138. goto out_unregister;
  139. }
  140. return nmi_init();
  141. out_unregister:
  142. unregister_perf_hsvc();
  143. return err;
  144. }