op_model_athlon.c 3.7 KB

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