p6.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include <linux/perf_event.h>
  2. #include <linux/types.h>
  3. #include "../perf_event.h"
  4. /*
  5. * Not sure about some of these
  6. */
  7. static const u64 p6_perfmon_event_map[] =
  8. {
  9. [PERF_COUNT_HW_CPU_CYCLES] = 0x0079, /* CPU_CLK_UNHALTED */
  10. [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0, /* INST_RETIRED */
  11. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0f2e, /* L2_RQSTS:M:E:S:I */
  12. [PERF_COUNT_HW_CACHE_MISSES] = 0x012e, /* L2_RQSTS:I */
  13. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4, /* BR_INST_RETIRED */
  14. [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5, /* BR_MISS_PRED_RETIRED */
  15. [PERF_COUNT_HW_BUS_CYCLES] = 0x0062, /* BUS_DRDY_CLOCKS */
  16. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 0x00a2, /* RESOURCE_STALLS */
  17. };
  18. static const u64 __initconst p6_hw_cache_event_ids
  19. [PERF_COUNT_HW_CACHE_MAX]
  20. [PERF_COUNT_HW_CACHE_OP_MAX]
  21. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  22. {
  23. [ C(L1D) ] = {
  24. [ C(OP_READ) ] = {
  25. [ C(RESULT_ACCESS) ] = 0x0043, /* DATA_MEM_REFS */
  26. [ C(RESULT_MISS) ] = 0x0045, /* DCU_LINES_IN */
  27. },
  28. [ C(OP_WRITE) ] = {
  29. [ C(RESULT_ACCESS) ] = 0,
  30. [ C(RESULT_MISS) ] = 0x0f29, /* L2_LD:M:E:S:I */
  31. },
  32. [ C(OP_PREFETCH) ] = {
  33. [ C(RESULT_ACCESS) ] = 0,
  34. [ C(RESULT_MISS) ] = 0,
  35. },
  36. },
  37. [ C(L1I ) ] = {
  38. [ C(OP_READ) ] = {
  39. [ C(RESULT_ACCESS) ] = 0x0080, /* IFU_IFETCH */
  40. [ C(RESULT_MISS) ] = 0x0f28, /* L2_IFETCH:M:E:S:I */
  41. },
  42. [ C(OP_WRITE) ] = {
  43. [ C(RESULT_ACCESS) ] = -1,
  44. [ C(RESULT_MISS) ] = -1,
  45. },
  46. [ C(OP_PREFETCH) ] = {
  47. [ C(RESULT_ACCESS) ] = 0,
  48. [ C(RESULT_MISS) ] = 0,
  49. },
  50. },
  51. [ C(LL ) ] = {
  52. [ C(OP_READ) ] = {
  53. [ C(RESULT_ACCESS) ] = 0,
  54. [ C(RESULT_MISS) ] = 0,
  55. },
  56. [ C(OP_WRITE) ] = {
  57. [ C(RESULT_ACCESS) ] = 0,
  58. [ C(RESULT_MISS) ] = 0x0025, /* L2_M_LINES_INM */
  59. },
  60. [ C(OP_PREFETCH) ] = {
  61. [ C(RESULT_ACCESS) ] = 0,
  62. [ C(RESULT_MISS) ] = 0,
  63. },
  64. },
  65. [ C(DTLB) ] = {
  66. [ C(OP_READ) ] = {
  67. [ C(RESULT_ACCESS) ] = 0x0043, /* DATA_MEM_REFS */
  68. [ C(RESULT_MISS) ] = 0,
  69. },
  70. [ C(OP_WRITE) ] = {
  71. [ C(RESULT_ACCESS) ] = 0,
  72. [ C(RESULT_MISS) ] = 0,
  73. },
  74. [ C(OP_PREFETCH) ] = {
  75. [ C(RESULT_ACCESS) ] = 0,
  76. [ C(RESULT_MISS) ] = 0,
  77. },
  78. },
  79. [ C(ITLB) ] = {
  80. [ C(OP_READ) ] = {
  81. [ C(RESULT_ACCESS) ] = 0x0080, /* IFU_IFETCH */
  82. [ C(RESULT_MISS) ] = 0x0085, /* ITLB_MISS */
  83. },
  84. [ C(OP_WRITE) ] = {
  85. [ C(RESULT_ACCESS) ] = -1,
  86. [ C(RESULT_MISS) ] = -1,
  87. },
  88. [ C(OP_PREFETCH) ] = {
  89. [ C(RESULT_ACCESS) ] = -1,
  90. [ C(RESULT_MISS) ] = -1,
  91. },
  92. },
  93. [ C(BPU ) ] = {
  94. [ C(OP_READ) ] = {
  95. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED */
  96. [ C(RESULT_MISS) ] = 0x00c5, /* BR_MISS_PRED_RETIRED */
  97. },
  98. [ C(OP_WRITE) ] = {
  99. [ C(RESULT_ACCESS) ] = -1,
  100. [ C(RESULT_MISS) ] = -1,
  101. },
  102. [ C(OP_PREFETCH) ] = {
  103. [ C(RESULT_ACCESS) ] = -1,
  104. [ C(RESULT_MISS) ] = -1,
  105. },
  106. },
  107. };
  108. static u64 p6_pmu_event_map(int hw_event)
  109. {
  110. return p6_perfmon_event_map[hw_event];
  111. }
  112. /*
  113. * Event setting that is specified not to count anything.
  114. * We use this to effectively disable a counter.
  115. *
  116. * L2_RQSTS with 0 MESI unit mask.
  117. */
  118. #define P6_NOP_EVENT 0x0000002EULL
  119. static struct event_constraint p6_event_constraints[] =
  120. {
  121. INTEL_EVENT_CONSTRAINT(0xc1, 0x1), /* FLOPS */
  122. INTEL_EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */
  123. INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
  124. INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
  125. INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
  126. INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
  127. EVENT_CONSTRAINT_END
  128. };
  129. static void p6_pmu_disable_all(void)
  130. {
  131. u64 val;
  132. /* p6 only has one enable register */
  133. rdmsrl(MSR_P6_EVNTSEL0, val);
  134. val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  135. wrmsrl(MSR_P6_EVNTSEL0, val);
  136. }
  137. static void p6_pmu_enable_all(int added)
  138. {
  139. unsigned long val;
  140. /* p6 only has one enable register */
  141. rdmsrl(MSR_P6_EVNTSEL0, val);
  142. val |= ARCH_PERFMON_EVENTSEL_ENABLE;
  143. wrmsrl(MSR_P6_EVNTSEL0, val);
  144. }
  145. static inline void
  146. p6_pmu_disable_event(struct perf_event *event)
  147. {
  148. struct hw_perf_event *hwc = &event->hw;
  149. u64 val = P6_NOP_EVENT;
  150. (void)wrmsrl_safe(hwc->config_base, val);
  151. }
  152. static void p6_pmu_enable_event(struct perf_event *event)
  153. {
  154. struct hw_perf_event *hwc = &event->hw;
  155. u64 val;
  156. val = hwc->config;
  157. /*
  158. * p6 only has a global event enable, set on PerfEvtSel0
  159. * We "disable" events by programming P6_NOP_EVENT
  160. * and we rely on p6_pmu_enable_all() being called
  161. * to actually enable the events.
  162. */
  163. (void)wrmsrl_safe(hwc->config_base, val);
  164. }
  165. PMU_FORMAT_ATTR(event, "config:0-7" );
  166. PMU_FORMAT_ATTR(umask, "config:8-15" );
  167. PMU_FORMAT_ATTR(edge, "config:18" );
  168. PMU_FORMAT_ATTR(pc, "config:19" );
  169. PMU_FORMAT_ATTR(inv, "config:23" );
  170. PMU_FORMAT_ATTR(cmask, "config:24-31" );
  171. static struct attribute *intel_p6_formats_attr[] = {
  172. &format_attr_event.attr,
  173. &format_attr_umask.attr,
  174. &format_attr_edge.attr,
  175. &format_attr_pc.attr,
  176. &format_attr_inv.attr,
  177. &format_attr_cmask.attr,
  178. NULL,
  179. };
  180. static __initconst const struct x86_pmu p6_pmu = {
  181. .name = "p6",
  182. .handle_irq = x86_pmu_handle_irq,
  183. .disable_all = p6_pmu_disable_all,
  184. .enable_all = p6_pmu_enable_all,
  185. .enable = p6_pmu_enable_event,
  186. .disable = p6_pmu_disable_event,
  187. .hw_config = x86_pmu_hw_config,
  188. .schedule_events = x86_schedule_events,
  189. .eventsel = MSR_P6_EVNTSEL0,
  190. .perfctr = MSR_P6_PERFCTR0,
  191. .event_map = p6_pmu_event_map,
  192. .max_events = ARRAY_SIZE(p6_perfmon_event_map),
  193. .apic = 1,
  194. .max_period = (1ULL << 31) - 1,
  195. .version = 0,
  196. .num_counters = 2,
  197. /*
  198. * Events have 40 bits implemented. However they are designed such
  199. * that bits [32-39] are sign extensions of bit 31. As such the
  200. * effective width of a event for P6-like PMU is 32 bits only.
  201. *
  202. * See IA-32 Intel Architecture Software developer manual Vol 3B
  203. */
  204. .cntval_bits = 32,
  205. .cntval_mask = (1ULL << 32) - 1,
  206. .get_event_constraints = x86_get_event_constraints,
  207. .event_constraints = p6_event_constraints,
  208. .format_attrs = intel_p6_formats_attr,
  209. .events_sysfs_show = intel_event_sysfs_show,
  210. };
  211. static __init void p6_pmu_rdpmc_quirk(void)
  212. {
  213. if (boot_cpu_data.x86_stepping < 9) {
  214. /*
  215. * PPro erratum 26; fixed in stepping 9 and above.
  216. */
  217. pr_warn("Userspace RDPMC support disabled due to a CPU erratum\n");
  218. x86_pmu.attr_rdpmc_broken = 1;
  219. x86_pmu.attr_rdpmc = 0;
  220. }
  221. }
  222. __init int p6_pmu_init(void)
  223. {
  224. x86_pmu = p6_pmu;
  225. switch (boot_cpu_data.x86_model) {
  226. case 1: /* Pentium Pro */
  227. x86_add_quirk(p6_pmu_rdpmc_quirk);
  228. break;
  229. case 3: /* Pentium II - Klamath */
  230. case 5: /* Pentium II - Deschutes */
  231. case 6: /* Pentium II - Mendocino */
  232. break;
  233. case 7: /* Pentium III - Katmai */
  234. case 8: /* Pentium III - Coppermine */
  235. case 10: /* Pentium III Xeon */
  236. case 11: /* Pentium III - Tualatin */
  237. break;
  238. case 9: /* Pentium M - Banias */
  239. case 13: /* Pentium M - Dothan */
  240. break;
  241. default:
  242. pr_cont("unsupported p6 CPU model %d ", boot_cpu_data.x86_model);
  243. return -ENODEV;
  244. }
  245. memcpy(hw_cache_event_ids, p6_hw_cache_event_ids,
  246. sizeof(hw_cache_event_ids));
  247. return 0;
  248. }