op_model_ppro.c 3.8 KB

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