op_model_ppro.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @file op_model_ppro.h
  3. * pentium pro / P6 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/apic.h>
  16. #include "op_x86_model.h"
  17. #include "op_counter.h"
  18. #define NUM_COUNTERS 2
  19. #define NUM_CONTROLS 2
  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, -(u32)(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 ppro_fill_in_addresses(struct op_msrs * const msrs)
  35. {
  36. msrs->counters[0].addr = MSR_P6_PERFCTR0;
  37. msrs->counters[1].addr = MSR_P6_PERFCTR1;
  38. msrs->controls[0].addr = MSR_P6_EVNTSEL0;
  39. msrs->controls[1].addr = MSR_P6_EVNTSEL1;
  40. }
  41. static void ppro_setup_ctrs(struct op_msrs const * const msrs)
  42. {
  43. unsigned int low, high;
  44. int i;
  45. /* clear all counters */
  46. for (i = 0 ; i < NUM_CONTROLS; ++i) {
  47. CTRL_READ(low, high, msrs, i);
  48. CTRL_CLEAR(low);
  49. CTRL_WRITE(low, high, msrs, i);
  50. }
  51. /* avoid a false detection of ctr overflows in NMI handler */
  52. for (i = 0; i < NUM_COUNTERS; ++i) {
  53. CTR_WRITE(1, msrs, i);
  54. }
  55. /* enable active counters */
  56. for (i = 0; i < NUM_COUNTERS; ++i) {
  57. if (counter_config[i].enabled) {
  58. reset_value[i] = counter_config[i].count;
  59. CTR_WRITE(counter_config[i].count, msrs, i);
  60. CTRL_READ(low, high, msrs, i);
  61. CTRL_CLEAR(low);
  62. CTRL_SET_ENABLE(low);
  63. CTRL_SET_USR(low, counter_config[i].user);
  64. CTRL_SET_KERN(low, counter_config[i].kernel);
  65. CTRL_SET_UM(low, counter_config[i].unit_mask);
  66. CTRL_SET_EVENT(low, counter_config[i].event);
  67. CTRL_WRITE(low, high, msrs, i);
  68. }
  69. }
  70. }
  71. static int ppro_check_ctrs(struct pt_regs * const regs,
  72. struct op_msrs const * const msrs)
  73. {
  74. unsigned int low, high;
  75. int i;
  76. for (i = 0 ; i < NUM_COUNTERS; ++i) {
  77. CTR_READ(low, high, msrs, i);
  78. if (CTR_OVERFLOWED(low)) {
  79. oprofile_add_sample(regs, i);
  80. CTR_WRITE(reset_value[i], msrs, i);
  81. }
  82. }
  83. /* Only P6 based Pentium M need to re-unmask the apic vector but it
  84. * doesn't hurt other P6 variant */
  85. apic_write(APIC_LVTPC, apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED);
  86. /* We can't work out if we really handled an interrupt. We
  87. * might have caught a *second* counter just after overflowing
  88. * the interrupt for this counter then arrives
  89. * and we don't find a counter that's overflowed, so we
  90. * would return 0 and get dazed + confused. Instead we always
  91. * assume we found an overflow. This sucks.
  92. */
  93. return 1;
  94. }
  95. static void ppro_start(struct op_msrs const * const msrs)
  96. {
  97. unsigned int low,high;
  98. CTRL_READ(low, high, msrs, 0);
  99. CTRL_SET_ACTIVE(low);
  100. CTRL_WRITE(low, high, msrs, 0);
  101. }
  102. static void ppro_stop(struct op_msrs const * const msrs)
  103. {
  104. unsigned int low,high;
  105. CTRL_READ(low, high, msrs, 0);
  106. CTRL_SET_INACTIVE(low);
  107. CTRL_WRITE(low, high, msrs, 0);
  108. }
  109. struct op_x86_model_spec const op_ppro_spec = {
  110. .num_counters = NUM_COUNTERS,
  111. .num_controls = NUM_CONTROLS,
  112. .fill_in_addresses = &ppro_fill_in_addresses,
  113. .setup_ctrs = &ppro_setup_ctrs,
  114. .check_ctrs = &ppro_check_ctrs,
  115. .start = &ppro_start,
  116. .stop = &ppro_stop
  117. };