op_model_athlon.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * @file op_model_athlon.h
  3. * athlon / K7 / K8 / Family 10h 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/nmi.h>
  16. #include "op_x86_model.h"
  17. #include "op_counter.h"
  18. #define NUM_COUNTERS 4
  19. #define NUM_CONTROLS 4
  20. #define CTR_IS_RESERVED(msrs, c) (msrs->counters[(c)].addr ? 1 : 0)
  21. #define CTR_READ(l, h, msrs, c) do {rdmsr(msrs->counters[(c)].addr, (l), (h)); } while (0)
  22. #define CTR_WRITE(l, msrs, c) do {wrmsr(msrs->counters[(c)].addr, -(unsigned int)(l), -1); } while (0)
  23. #define CTR_OVERFLOWED(n) (!((n) & (1U<<31)))
  24. #define CTRL_IS_RESERVED(msrs, c) (msrs->controls[(c)].addr ? 1 : 0)
  25. #define CTRL_READ(l, h, msrs, c) do {rdmsr(msrs->controls[(c)].addr, (l), (h)); } while (0)
  26. #define CTRL_WRITE(l, h, msrs, c) do {wrmsr(msrs->controls[(c)].addr, (l), (h)); } while (0)
  27. #define CTRL_SET_ACTIVE(n) (n |= (1<<22))
  28. #define CTRL_SET_INACTIVE(n) (n &= ~(1<<22))
  29. #define CTRL_CLEAR_LO(x) (x &= (1<<21))
  30. #define CTRL_CLEAR_HI(x) (x &= 0xfffffcf0)
  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_LOW(val, e) (val |= (e & 0xff))
  36. #define CTRL_SET_EVENT_HIGH(val, e) (val |= ((e >> 8) & 0xf))
  37. #define CTRL_SET_HOST_ONLY(val, h) (val |= ((h & 1) << 9))
  38. #define CTRL_SET_GUEST_ONLY(val, h) (val |= ((h & 1) << 8))
  39. static unsigned long reset_value[NUM_COUNTERS];
  40. static void athlon_fill_in_addresses(struct op_msrs * const msrs)
  41. {
  42. int i;
  43. for (i = 0; i < NUM_COUNTERS; i++) {
  44. if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
  45. msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
  46. else
  47. msrs->counters[i].addr = 0;
  48. }
  49. for (i = 0; i < NUM_CONTROLS; i++) {
  50. if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
  51. msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
  52. else
  53. msrs->controls[i].addr = 0;
  54. }
  55. }
  56. static void athlon_setup_ctrs(struct op_msrs const * const msrs)
  57. {
  58. unsigned int low, high;
  59. int i;
  60. /* clear all counters */
  61. for (i = 0 ; i < NUM_CONTROLS; ++i) {
  62. if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
  63. continue;
  64. CTRL_READ(low, high, msrs, i);
  65. CTRL_CLEAR_LO(low);
  66. CTRL_CLEAR_HI(high);
  67. CTRL_WRITE(low, high, msrs, i);
  68. }
  69. /* avoid a false detection of ctr overflows in NMI handler */
  70. for (i = 0; i < NUM_COUNTERS; ++i) {
  71. if (unlikely(!CTR_IS_RESERVED(msrs, i)))
  72. continue;
  73. CTR_WRITE(1, msrs, i);
  74. }
  75. /* enable active counters */
  76. for (i = 0; i < NUM_COUNTERS; ++i) {
  77. if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
  78. reset_value[i] = counter_config[i].count;
  79. CTR_WRITE(counter_config[i].count, msrs, i);
  80. CTRL_READ(low, high, msrs, i);
  81. CTRL_CLEAR_LO(low);
  82. CTRL_CLEAR_HI(high);
  83. CTRL_SET_ENABLE(low);
  84. CTRL_SET_USR(low, counter_config[i].user);
  85. CTRL_SET_KERN(low, counter_config[i].kernel);
  86. CTRL_SET_UM(low, counter_config[i].unit_mask);
  87. CTRL_SET_EVENT_LOW(low, counter_config[i].event);
  88. CTRL_SET_EVENT_HIGH(high, counter_config[i].event);
  89. CTRL_SET_HOST_ONLY(high, 0);
  90. CTRL_SET_GUEST_ONLY(high, 0);
  91. CTRL_WRITE(low, high, msrs, i);
  92. } else {
  93. reset_value[i] = 0;
  94. }
  95. }
  96. }
  97. static int athlon_check_ctrs(struct pt_regs * const regs,
  98. struct op_msrs const * const msrs)
  99. {
  100. unsigned int low, high;
  101. int i;
  102. for (i = 0 ; i < NUM_COUNTERS; ++i) {
  103. if (!reset_value[i])
  104. continue;
  105. CTR_READ(low, high, msrs, i);
  106. if (CTR_OVERFLOWED(low)) {
  107. oprofile_add_sample(regs, i);
  108. CTR_WRITE(reset_value[i], msrs, i);
  109. }
  110. }
  111. /* See op_model_ppro.c */
  112. return 1;
  113. }
  114. static void athlon_start(struct op_msrs const * const msrs)
  115. {
  116. unsigned int low, high;
  117. int i;
  118. for (i = 0 ; i < NUM_COUNTERS ; ++i) {
  119. if (reset_value[i]) {
  120. CTRL_READ(low, high, msrs, i);
  121. CTRL_SET_ACTIVE(low);
  122. CTRL_WRITE(low, high, msrs, i);
  123. }
  124. }
  125. }
  126. static void athlon_stop(struct op_msrs const * const msrs)
  127. {
  128. unsigned int low, high;
  129. int i;
  130. /* Subtle: stop on all counters to avoid race with
  131. * setting our pm callback */
  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 athlon_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_K7_PERFCTR0 + i);
  146. }
  147. for (i = 0 ; i < NUM_CONTROLS ; ++i) {
  148. if (CTRL_IS_RESERVED(msrs, i))
  149. release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  150. }
  151. }
  152. struct op_x86_model_spec const op_athlon_spec = {
  153. .num_counters = NUM_COUNTERS,
  154. .num_controls = NUM_CONTROLS,
  155. .fill_in_addresses = &athlon_fill_in_addresses,
  156. .setup_ctrs = &athlon_setup_ctrs,
  157. .check_ctrs = &athlon_check_ctrs,
  158. .start = &athlon_start,
  159. .stop = &athlon_stop,
  160. .shutdown = &athlon_shutdown
  161. };