op_model_ppro.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * @file op_model_ppro.h
  3. * pentium pro / P6 model-specific MSR operations
  4. *
  5. * @remark Copyright 2002 OProfile authors
  6. * @remark Read the file COPYING
  7. *
  8. * @author John Levon
  9. * @author Philippe Elie
  10. * @author Graydon Hoare
  11. */
  12. #include <linux/oprofile.h>
  13. #include <asm/ptrace.h>
  14. #include <asm/msr.h>
  15. #include <asm/apic.h>
  16. #include <asm/nmi.h>
  17. #include "op_x86_model.h"
  18. #include "op_counter.h"
  19. #define NUM_COUNTERS 2
  20. #define NUM_CONTROLS 2
  21. #define CTR_IS_RESERVED(msrs, c) (msrs->counters[(c)].addr ? 1 : 0)
  22. #define CTR_READ(l, h, msrs, c) do {rdmsr(msrs->counters[(c)].addr, (l), (h)); } while (0)
  23. #define CTR_32BIT_WRITE(l, msrs, c) \
  24. do {wrmsr(msrs->counters[(c)].addr, -(u32)(l), 0); } while (0)
  25. #define CTR_OVERFLOWED(n) (!((n) & (1U<<31)))
  26. #define CTRL_IS_RESERVED(msrs, c) (msrs->controls[(c)].addr ? 1 : 0)
  27. #define CTRL_READ(l, h, msrs, c) do {rdmsr((msrs->controls[(c)].addr), (l), (h)); } while (0)
  28. #define CTRL_WRITE(l, h, msrs, c) do {wrmsr((msrs->controls[(c)].addr), (l), (h)); } while (0)
  29. #define CTRL_SET_ACTIVE(n) (n |= (1<<22))
  30. #define CTRL_SET_INACTIVE(n) (n &= ~(1<<22))
  31. #define CTRL_CLEAR(x) (x &= (1<<21))
  32. #define CTRL_SET_ENABLE(val) (val |= 1<<20)
  33. #define CTRL_SET_USR(val, u) (val |= ((u & 1) << 16))
  34. #define CTRL_SET_KERN(val, k) (val |= ((k & 1) << 17))
  35. #define CTRL_SET_UM(val, m) (val |= (m << 8))
  36. #define CTRL_SET_EVENT(val, e) (val |= e)
  37. static unsigned long reset_value[NUM_COUNTERS];
  38. static void ppro_fill_in_addresses(struct op_msrs * const msrs)
  39. {
  40. int i;
  41. for (i = 0; i < NUM_COUNTERS; i++) {
  42. if (reserve_perfctr_nmi(MSR_P6_PERFCTR0 + i))
  43. msrs->counters[i].addr = MSR_P6_PERFCTR0 + i;
  44. else
  45. msrs->counters[i].addr = 0;
  46. }
  47. for (i = 0; i < NUM_CONTROLS; i++) {
  48. if (reserve_evntsel_nmi(MSR_P6_EVNTSEL0 + i))
  49. msrs->controls[i].addr = MSR_P6_EVNTSEL0 + i;
  50. else
  51. msrs->controls[i].addr = 0;
  52. }
  53. }
  54. static void ppro_setup_ctrs(struct op_msrs const * const msrs)
  55. {
  56. unsigned int low, high;
  57. int i;
  58. /* clear all counters */
  59. for (i = 0 ; i < NUM_CONTROLS; ++i) {
  60. if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
  61. continue;
  62. CTRL_READ(low, high, msrs, i);
  63. CTRL_CLEAR(low);
  64. CTRL_WRITE(low, high, msrs, i);
  65. }
  66. /* avoid a false detection of ctr overflows in NMI handler */
  67. for (i = 0; i < NUM_COUNTERS; ++i) {
  68. if (unlikely(!CTR_IS_RESERVED(msrs, i)))
  69. continue;
  70. CTR_32BIT_WRITE(1, msrs, i);
  71. }
  72. /* enable active counters */
  73. for (i = 0; i < NUM_COUNTERS; ++i) {
  74. if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
  75. reset_value[i] = counter_config[i].count;
  76. CTR_32BIT_WRITE(counter_config[i].count, msrs, i);
  77. CTRL_READ(low, high, msrs, i);
  78. CTRL_CLEAR(low);
  79. CTRL_SET_ENABLE(low);
  80. CTRL_SET_USR(low, counter_config[i].user);
  81. CTRL_SET_KERN(low, counter_config[i].kernel);
  82. CTRL_SET_UM(low, counter_config[i].unit_mask);
  83. CTRL_SET_EVENT(low, counter_config[i].event);
  84. CTRL_WRITE(low, high, msrs, i);
  85. } else {
  86. reset_value[i] = 0;
  87. }
  88. }
  89. }
  90. static int ppro_check_ctrs(struct pt_regs * const regs,
  91. struct op_msrs const * const msrs)
  92. {
  93. unsigned int low, high;
  94. int i;
  95. for (i = 0 ; i < NUM_COUNTERS; ++i) {
  96. if (!reset_value[i])
  97. continue;
  98. CTR_READ(low, high, msrs, i);
  99. if (CTR_OVERFLOWED(low)) {
  100. oprofile_add_sample(regs, i);
  101. CTR_32BIT_WRITE(reset_value[i], msrs, i);
  102. }
  103. }
  104. /* Only P6 based Pentium M need to re-unmask the apic vector but it
  105. * doesn't hurt other P6 variant */
  106. apic_write(APIC_LVTPC, apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED);
  107. /* We can't work out if we really handled an interrupt. We
  108. * might have caught a *second* counter just after overflowing
  109. * the interrupt for this counter then arrives
  110. * and we don't find a counter that's overflowed, so we
  111. * would return 0 and get dazed + confused. Instead we always
  112. * assume we found an overflow. This sucks.
  113. */
  114. return 1;
  115. }
  116. static void ppro_start(struct op_msrs const * const msrs)
  117. {
  118. unsigned int low, high;
  119. int i;
  120. for (i = 0; i < NUM_COUNTERS; ++i) {
  121. if (reset_value[i]) {
  122. CTRL_READ(low, high, msrs, i);
  123. CTRL_SET_ACTIVE(low);
  124. CTRL_WRITE(low, high, msrs, i);
  125. }
  126. }
  127. }
  128. static void ppro_stop(struct op_msrs const * const msrs)
  129. {
  130. unsigned int low, high;
  131. int i;
  132. for (i = 0; i < NUM_COUNTERS; ++i) {
  133. if (!reset_value[i])
  134. continue;
  135. CTRL_READ(low, high, msrs, i);
  136. CTRL_SET_INACTIVE(low);
  137. CTRL_WRITE(low, high, msrs, i);
  138. }
  139. }
  140. static void ppro_shutdown(struct op_msrs const * const msrs)
  141. {
  142. int i;
  143. for (i = 0 ; i < NUM_COUNTERS ; ++i) {
  144. if (CTR_IS_RESERVED(msrs, i))
  145. release_perfctr_nmi(MSR_P6_PERFCTR0 + i);
  146. }
  147. for (i = 0 ; i < NUM_CONTROLS ; ++i) {
  148. if (CTRL_IS_RESERVED(msrs, i))
  149. release_evntsel_nmi(MSR_P6_EVNTSEL0 + i);
  150. }
  151. }
  152. struct op_x86_model_spec const op_ppro_spec = {
  153. .num_counters = NUM_COUNTERS,
  154. .num_controls = NUM_CONTROLS,
  155. .fill_in_addresses = &ppro_fill_in_addresses,
  156. .setup_ctrs = &ppro_setup_ctrs,
  157. .check_ctrs = &ppro_check_ctrs,
  158. .start = &ppro_start,
  159. .stop = &ppro_stop,
  160. .shutdown = &ppro_shutdown
  161. };