pcr.c 3.3 KB

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