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