op_model_athlon.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "op_x86_model.h"
  16. #include "op_counter.h"
  17. #define NUM_COUNTERS 4
  18. #define NUM_CONTROLS 4
  19. #define CTR_READ(l,h,msrs,c) do {rdmsr(msrs->counters[(c)].addr, (l), (h));} while (0)
  20. #define CTR_WRITE(l,msrs,c) do {wrmsr(msrs->counters[(c)].addr, -(unsigned int)(l), -1);} while (0)
  21. #define CTR_OVERFLOWED(n) (!((n) & (1U<<31)))
  22. #define CTRL_READ(l,h,msrs,c) do {rdmsr(msrs->controls[(c)].addr, (l), (h));} while (0)
  23. #define CTRL_WRITE(l,h,msrs,c) do {wrmsr(msrs->controls[(c)].addr, (l), (h));} while (0)
  24. #define CTRL_SET_ACTIVE(n) (n |= (1<<22))
  25. #define CTRL_SET_INACTIVE(n) (n &= ~(1<<22))
  26. #define CTRL_CLEAR(x) (x &= (1<<21))
  27. #define CTRL_SET_ENABLE(val) (val |= 1<<20)
  28. #define CTRL_SET_USR(val,u) (val |= ((u & 1) << 16))
  29. #define CTRL_SET_KERN(val,k) (val |= ((k & 1) << 17))
  30. #define CTRL_SET_UM(val, m) (val |= (m << 8))
  31. #define CTRL_SET_EVENT(val, e) (val |= e)
  32. static unsigned long reset_value[NUM_COUNTERS];
  33. static void athlon_fill_in_addresses(struct op_msrs * const msrs)
  34. {
  35. msrs->counters[0].addr = MSR_K7_PERFCTR0;
  36. msrs->counters[1].addr = MSR_K7_PERFCTR1;
  37. msrs->counters[2].addr = MSR_K7_PERFCTR2;
  38. msrs->counters[3].addr = MSR_K7_PERFCTR3;
  39. msrs->controls[0].addr = MSR_K7_EVNTSEL0;
  40. msrs->controls[1].addr = MSR_K7_EVNTSEL1;
  41. msrs->controls[2].addr = MSR_K7_EVNTSEL2;
  42. msrs->controls[3].addr = MSR_K7_EVNTSEL3;
  43. }
  44. static void athlon_setup_ctrs(struct op_msrs const * const msrs)
  45. {
  46. unsigned int low, high;
  47. int i;
  48. /* clear all counters */
  49. for (i = 0 ; i < NUM_CONTROLS; ++i) {
  50. CTRL_READ(low, high, msrs, i);
  51. CTRL_CLEAR(low);
  52. CTRL_WRITE(low, high, msrs, i);
  53. }
  54. /* avoid a false detection of ctr overflows in NMI handler */
  55. for (i = 0; i < NUM_COUNTERS; ++i) {
  56. CTR_WRITE(1, msrs, i);
  57. }
  58. /* enable active counters */
  59. for (i = 0; i < NUM_COUNTERS; ++i) {
  60. if (counter_config[i].enabled) {
  61. reset_value[i] = counter_config[i].count;
  62. CTR_WRITE(counter_config[i].count, msrs, i);
  63. CTRL_READ(low, high, msrs, i);
  64. CTRL_CLEAR(low);
  65. CTRL_SET_ENABLE(low);
  66. CTRL_SET_USR(low, counter_config[i].user);
  67. CTRL_SET_KERN(low, counter_config[i].kernel);
  68. CTRL_SET_UM(low, counter_config[i].unit_mask);
  69. CTRL_SET_EVENT(low, counter_config[i].event);
  70. CTRL_WRITE(low, high, msrs, i);
  71. } else {
  72. reset_value[i] = 0;
  73. }
  74. }
  75. }
  76. static int athlon_check_ctrs(struct pt_regs * const regs,
  77. struct op_msrs const * const msrs)
  78. {
  79. unsigned int low, high;
  80. int i;
  81. for (i = 0 ; i < NUM_COUNTERS; ++i) {
  82. CTR_READ(low, high, msrs, i);
  83. if (CTR_OVERFLOWED(low)) {
  84. oprofile_add_sample(regs, i);
  85. CTR_WRITE(reset_value[i], msrs, i);
  86. }
  87. }
  88. /* See op_model_ppro.c */
  89. return 1;
  90. }
  91. static void athlon_start(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. CTRL_READ(low, high, msrs, i);
  98. CTRL_SET_ACTIVE(low);
  99. CTRL_WRITE(low, high, msrs, i);
  100. }
  101. }
  102. }
  103. static void athlon_stop(struct op_msrs const * const msrs)
  104. {
  105. unsigned int low,high;
  106. int i;
  107. /* Subtle: stop on all counters to avoid race with
  108. * setting our pm callback */
  109. for (i = 0 ; i < NUM_COUNTERS ; ++i) {
  110. CTRL_READ(low, high, msrs, i);
  111. CTRL_SET_INACTIVE(low);
  112. CTRL_WRITE(low, high, msrs, i);
  113. }
  114. }
  115. struct op_x86_model_spec const op_athlon_spec = {
  116. .num_counters = NUM_COUNTERS,
  117. .num_controls = NUM_CONTROLS,
  118. .fill_in_addresses = &athlon_fill_in_addresses,
  119. .setup_ctrs = &athlon_setup_ctrs,
  120. .check_ctrs = &athlon_check_ctrs,
  121. .start = &athlon_start,
  122. .stop = &athlon_stop
  123. };