op_model_athlon.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * @file op_model_athlon.c
  3. * athlon / K7 / K8 / Family 10h model-specific MSR operations
  4. *
  5. * @remark Copyright 2002-2008 OProfile authors
  6. * @remark Read the file COPYING
  7. *
  8. * @author John Levon
  9. * @author Philippe Elie
  10. * @author Graydon Hoare
  11. * @author Robert Richter <robert.richter@amd.com>
  12. */
  13. #include <linux/oprofile.h>
  14. #include <asm/ptrace.h>
  15. #include <asm/msr.h>
  16. #include <asm/nmi.h>
  17. #include "op_x86_model.h"
  18. #include "op_counter.h"
  19. #define NUM_COUNTERS 4
  20. #define NUM_CONTROLS 4
  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, -(unsigned int)(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_LO(x) (x &= (1<<21))
  31. #define CTRL_CLEAR_HI(x) (x &= 0xfffffcf0)
  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_LOW(val, e) (val |= (e & 0xff))
  37. #define CTRL_SET_EVENT_HIGH(val, e) (val |= ((e >> 8) & 0xf))
  38. #define CTRL_SET_HOST_ONLY(val, h) (val |= ((h & 1) << 9))
  39. #define CTRL_SET_GUEST_ONLY(val, h) (val |= ((h & 1) << 8))
  40. static unsigned long reset_value[NUM_COUNTERS];
  41. static void athlon_fill_in_addresses(struct op_msrs * const msrs)
  42. {
  43. int i;
  44. for (i = 0; i < NUM_COUNTERS; i++) {
  45. if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
  46. msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
  47. else
  48. msrs->counters[i].addr = 0;
  49. }
  50. for (i = 0; i < NUM_CONTROLS; i++) {
  51. if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
  52. msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
  53. else
  54. msrs->controls[i].addr = 0;
  55. }
  56. }
  57. static void athlon_setup_ctrs(struct op_msrs const * const msrs)
  58. {
  59. unsigned int low, high;
  60. int i;
  61. /* clear all counters */
  62. for (i = 0 ; i < NUM_CONTROLS; ++i) {
  63. if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
  64. continue;
  65. CTRL_READ(low, high, msrs, i);
  66. CTRL_CLEAR_LO(low);
  67. CTRL_CLEAR_HI(high);
  68. CTRL_WRITE(low, high, msrs, i);
  69. }
  70. /* avoid a false detection of ctr overflows in NMI handler */
  71. for (i = 0; i < NUM_COUNTERS; ++i) {
  72. if (unlikely(!CTR_IS_RESERVED(msrs, i)))
  73. continue;
  74. CTR_WRITE(1, msrs, i);
  75. }
  76. /* enable active counters */
  77. for (i = 0; i < NUM_COUNTERS; ++i) {
  78. if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
  79. reset_value[i] = counter_config[i].count;
  80. CTR_WRITE(counter_config[i].count, msrs, i);
  81. CTRL_READ(low, high, msrs, i);
  82. CTRL_CLEAR_LO(low);
  83. CTRL_CLEAR_HI(high);
  84. CTRL_SET_ENABLE(low);
  85. CTRL_SET_USR(low, counter_config[i].user);
  86. CTRL_SET_KERN(low, counter_config[i].kernel);
  87. CTRL_SET_UM(low, counter_config[i].unit_mask);
  88. CTRL_SET_EVENT_LOW(low, counter_config[i].event);
  89. CTRL_SET_EVENT_HIGH(high, counter_config[i].event);
  90. CTRL_SET_HOST_ONLY(high, 0);
  91. CTRL_SET_GUEST_ONLY(high, 0);
  92. CTRL_WRITE(low, high, msrs, i);
  93. } else {
  94. reset_value[i] = 0;
  95. }
  96. }
  97. }
  98. static int athlon_check_ctrs(struct pt_regs * const regs,
  99. struct op_msrs const * const msrs)
  100. {
  101. unsigned int low, high;
  102. int i;
  103. for (i = 0 ; i < NUM_COUNTERS; ++i) {
  104. if (!reset_value[i])
  105. continue;
  106. CTR_READ(low, high, msrs, i);
  107. if (CTR_OVERFLOWED(low)) {
  108. oprofile_add_sample(regs, i);
  109. CTR_WRITE(reset_value[i], msrs, i);
  110. }
  111. }
  112. /* See op_model_ppro.c */
  113. return 1;
  114. }
  115. static void athlon_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 athlon_stop(struct op_msrs const * const msrs)
  128. {
  129. unsigned int low, high;
  130. int i;
  131. /* Subtle: stop on all counters to avoid race with
  132. * setting our pm callback */
  133. for (i = 0 ; i < NUM_COUNTERS ; ++i) {
  134. if (!reset_value[i])
  135. continue;
  136. CTRL_READ(low, high, msrs, i);
  137. CTRL_SET_INACTIVE(low);
  138. CTRL_WRITE(low, high, msrs, i);
  139. }
  140. }
  141. static void athlon_shutdown(struct op_msrs const * const msrs)
  142. {
  143. int i;
  144. for (i = 0 ; i < NUM_COUNTERS ; ++i) {
  145. if (CTR_IS_RESERVED(msrs, i))
  146. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  147. }
  148. for (i = 0 ; i < NUM_CONTROLS ; ++i) {
  149. if (CTRL_IS_RESERVED(msrs, i))
  150. release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  151. }
  152. }
  153. static int op_amd_init(struct oprofile_operations *ops)
  154. {
  155. return 0;
  156. }
  157. static void op_amd_exit(void)
  158. {
  159. }
  160. struct op_x86_model_spec const op_athlon_spec = {
  161. .init = op_amd_init,
  162. .exit = op_amd_exit,
  163. .num_counters = NUM_COUNTERS,
  164. .num_controls = NUM_CONTROLS,
  165. .fill_in_addresses = &athlon_fill_in_addresses,
  166. .setup_ctrs = &athlon_setup_ctrs,
  167. .check_ctrs = &athlon_check_ctrs,
  168. .start = &athlon_start,
  169. .stop = &athlon_stop,
  170. .shutdown = &athlon_shutdown
  171. };