op_model_ppro.c 4.8 KB

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