pcr.c 3.5 KB

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