op_model_athlon.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * @file op_model_athlon.h
  3. * athlon / K7 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(x) (x &= (1<<21))
  30. #define CTRL_SET_ENABLE(val) (val |= 1<<20)
  31. #define CTRL_SET_USR(val,u) (val |= ((u & 1) << 16))
  32. #define CTRL_SET_KERN(val,k) (val |= ((k & 1) << 17))
  33. #define CTRL_SET_UM(val, m) (val |= (m << 8))
  34. #define CTRL_SET_EVENT(val, e) (val |= e)
  35. static unsigned long reset_value[NUM_COUNTERS];
  36. static void athlon_fill_in_addresses(struct op_msrs * const msrs)
  37. {
  38. int i;
  39. for (i=0; i < NUM_COUNTERS; i++) {
  40. if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
  41. msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
  42. else
  43. msrs->counters[i].addr = 0;
  44. }
  45. for (i=0; i < NUM_CONTROLS; i++) {
  46. if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
  47. msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
  48. else
  49. msrs->controls[i].addr = 0;
  50. }
  51. }
  52. static void athlon_setup_ctrs(struct op_msrs const * const msrs)
  53. {
  54. unsigned int low, high;
  55. int i;
  56. /* clear all counters */
  57. for (i = 0 ; i < NUM_CONTROLS; ++i) {
  58. if (unlikely(!CTRL_IS_RESERVED(msrs,i)))
  59. continue;
  60. CTRL_READ(low, high, msrs, i);
  61. CTRL_CLEAR(low);
  62. CTRL_WRITE(low, high, msrs, i);
  63. }
  64. /* avoid a false detection of ctr overflows in NMI handler */
  65. for (i = 0; i < NUM_COUNTERS; ++i) {
  66. if (unlikely(!CTR_IS_RESERVED(msrs,i)))
  67. continue;
  68. CTR_WRITE(1, msrs, i);
  69. }
  70. /* enable active counters */
  71. for (i = 0; i < NUM_COUNTERS; ++i) {
  72. if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs,i))) {
  73. reset_value[i] = counter_config[i].count;
  74. CTR_WRITE(counter_config[i].count, msrs, i);
  75. CTRL_READ(low, high, msrs, i);
  76. CTRL_CLEAR(low);
  77. CTRL_SET_ENABLE(low);
  78. CTRL_SET_USR(low, counter_config[i].user);
  79. CTRL_SET_KERN(low, counter_config[i].kernel);
  80. CTRL_SET_UM(low, counter_config[i].unit_mask);
  81. CTRL_SET_EVENT(low, counter_config[i].event);
  82. CTRL_WRITE(low, high, msrs, i);
  83. } else {
  84. reset_value[i] = 0;
  85. }
  86. }
  87. }
  88. static int athlon_check_ctrs(struct pt_regs * const regs,
  89. struct op_msrs const * const msrs)
  90. {
  91. unsigned int low, high;
  92. int i;
  93. for (i = 0 ; i < NUM_COUNTERS; ++i) {
  94. if (!reset_value[i])
  95. continue;
  96. CTR_READ(low, high, msrs, i);
  97. if (CTR_OVERFLOWED(low)) {
  98. oprofile_add_sample(regs, i);
  99. CTR_WRITE(reset_value[i], msrs, i);
  100. }
  101. }
  102. /* See op_model_ppro.c */
  103. return 1;
  104. }
  105. static void athlon_start(struct op_msrs const * const msrs)
  106. {
  107. unsigned int low, high;
  108. int i;
  109. for (i = 0 ; i < NUM_COUNTERS ; ++i) {
  110. if (reset_value[i]) {
  111. CTRL_READ(low, high, msrs, i);
  112. CTRL_SET_ACTIVE(low);
  113. CTRL_WRITE(low, high, msrs, i);
  114. }
  115. }
  116. }
  117. static void athlon_stop(struct op_msrs const * const msrs)
  118. {
  119. unsigned int low,high;
  120. int i;
  121. /* Subtle: stop on all counters to avoid race with
  122. * setting our pm callback */
  123. for (i = 0 ; i < NUM_COUNTERS ; ++i) {
  124. if (!reset_value[i])
  125. continue;
  126. CTRL_READ(low, high, msrs, i);
  127. CTRL_SET_INACTIVE(low);
  128. CTRL_WRITE(low, high, msrs, i);
  129. }
  130. }
  131. static void athlon_shutdown(struct op_msrs const * const msrs)
  132. {
  133. int i;
  134. for (i = 0 ; i < NUM_COUNTERS ; ++i) {
  135. if (CTR_IS_RESERVED(msrs,i))
  136. release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
  137. }
  138. for (i = 0 ; i < NUM_CONTROLS ; ++i) {
  139. if (CTRL_IS_RESERVED(msrs,i))
  140. release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
  141. }
  142. }
  143. struct op_x86_model_spec const op_athlon_spec = {
  144. .num_counters = NUM_COUNTERS,
  145. .num_controls = NUM_CONTROLS,
  146. .fill_in_addresses = &athlon_fill_in_addresses,
  147. .setup_ctrs = &athlon_setup_ctrs,
  148. .check_ctrs = &athlon_check_ctrs,
  149. .start = &athlon_start,
  150. .stop = &athlon_stop,
  151. .shutdown = &athlon_shutdown
  152. };