perfctr-watchdog.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * local apic based NMI watchdog for various CPUs.
  3. *
  4. * This file also handles reservation of performance counters for coordination
  5. * with other users (like oprofile).
  6. *
  7. * Note that these events normally don't tick when the CPU idles. This means
  8. * the frequency varies with CPU load.
  9. *
  10. * Original code for K7/P6 written by Keith Owens
  11. *
  12. */
  13. #include <linux/percpu.h>
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/bitops.h>
  17. #include <linux/smp.h>
  18. #include <asm/nmi.h>
  19. #include <linux/kprobes.h>
  20. #include <asm/apic.h>
  21. #include <asm/perf_event.h>
  22. /*
  23. * this number is calculated from Intel's MSR_P4_CRU_ESCR5 register and it's
  24. * offset from MSR_P4_BSU_ESCR0.
  25. *
  26. * It will be the max for all platforms (for now)
  27. */
  28. #define NMI_MAX_COUNTER_BITS 66
  29. /*
  30. * perfctr_nmi_owner tracks the ownership of the perfctr registers:
  31. * evtsel_nmi_owner tracks the ownership of the event selection
  32. * - different performance counters/ event selection may be reserved for
  33. * different subsystems this reservation system just tries to coordinate
  34. * things a little
  35. */
  36. static DECLARE_BITMAP(perfctr_nmi_owner, NMI_MAX_COUNTER_BITS);
  37. static DECLARE_BITMAP(evntsel_nmi_owner, NMI_MAX_COUNTER_BITS);
  38. /* converts an msr to an appropriate reservation bit */
  39. static inline unsigned int nmi_perfctr_msr_to_bit(unsigned int msr)
  40. {
  41. /* returns the bit offset of the performance counter register */
  42. switch (boot_cpu_data.x86_vendor) {
  43. case X86_VENDOR_AMD:
  44. return msr - MSR_K7_PERFCTR0;
  45. case X86_VENDOR_INTEL:
  46. if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  47. return msr - MSR_ARCH_PERFMON_PERFCTR0;
  48. switch (boot_cpu_data.x86) {
  49. case 6:
  50. return msr - MSR_P6_PERFCTR0;
  51. case 15:
  52. return msr - MSR_P4_BPU_PERFCTR0;
  53. }
  54. }
  55. return 0;
  56. }
  57. /*
  58. * converts an msr to an appropriate reservation bit
  59. * returns the bit offset of the event selection register
  60. */
  61. static inline unsigned int nmi_evntsel_msr_to_bit(unsigned int msr)
  62. {
  63. /* returns the bit offset of the event selection register */
  64. switch (boot_cpu_data.x86_vendor) {
  65. case X86_VENDOR_AMD:
  66. return msr - MSR_K7_EVNTSEL0;
  67. case X86_VENDOR_INTEL:
  68. if (cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
  69. return msr - MSR_ARCH_PERFMON_EVENTSEL0;
  70. switch (boot_cpu_data.x86) {
  71. case 6:
  72. return msr - MSR_P6_EVNTSEL0;
  73. case 15:
  74. return msr - MSR_P4_BSU_ESCR0;
  75. }
  76. }
  77. return 0;
  78. }
  79. /* checks for a bit availability (hack for oprofile) */
  80. int avail_to_resrv_perfctr_nmi_bit(unsigned int counter)
  81. {
  82. BUG_ON(counter > NMI_MAX_COUNTER_BITS);
  83. return !test_bit(counter, perfctr_nmi_owner);
  84. }
  85. EXPORT_SYMBOL(avail_to_resrv_perfctr_nmi_bit);
  86. int reserve_perfctr_nmi(unsigned int msr)
  87. {
  88. unsigned int counter;
  89. counter = nmi_perfctr_msr_to_bit(msr);
  90. /* register not managed by the allocator? */
  91. if (counter > NMI_MAX_COUNTER_BITS)
  92. return 1;
  93. if (!test_and_set_bit(counter, perfctr_nmi_owner))
  94. return 1;
  95. return 0;
  96. }
  97. EXPORT_SYMBOL(reserve_perfctr_nmi);
  98. void release_perfctr_nmi(unsigned int msr)
  99. {
  100. unsigned int counter;
  101. counter = nmi_perfctr_msr_to_bit(msr);
  102. /* register not managed by the allocator? */
  103. if (counter > NMI_MAX_COUNTER_BITS)
  104. return;
  105. clear_bit(counter, perfctr_nmi_owner);
  106. }
  107. EXPORT_SYMBOL(release_perfctr_nmi);
  108. int reserve_evntsel_nmi(unsigned int msr)
  109. {
  110. unsigned int counter;
  111. counter = nmi_evntsel_msr_to_bit(msr);
  112. /* register not managed by the allocator? */
  113. if (counter > NMI_MAX_COUNTER_BITS)
  114. return 1;
  115. if (!test_and_set_bit(counter, evntsel_nmi_owner))
  116. return 1;
  117. return 0;
  118. }
  119. EXPORT_SYMBOL(reserve_evntsel_nmi);
  120. void release_evntsel_nmi(unsigned int msr)
  121. {
  122. unsigned int counter;
  123. counter = nmi_evntsel_msr_to_bit(msr);
  124. /* register not managed by the allocator? */
  125. if (counter > NMI_MAX_COUNTER_BITS)
  126. return;
  127. clear_bit(counter, evntsel_nmi_owner);
  128. }
  129. EXPORT_SYMBOL(release_evntsel_nmi);