perf_event_intel.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. #ifdef CONFIG_CPU_SUP_INTEL
  2. /*
  3. * Intel PerfMon, used on Core and later.
  4. */
  5. static const u64 intel_perfmon_event_map[] =
  6. {
  7. [PERF_COUNT_HW_CPU_CYCLES] = 0x003c,
  8. [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
  9. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x4f2e,
  10. [PERF_COUNT_HW_CACHE_MISSES] = 0x412e,
  11. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
  12. [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
  13. [PERF_COUNT_HW_BUS_CYCLES] = 0x013c,
  14. };
  15. static struct event_constraint intel_core_event_constraints[] =
  16. {
  17. INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
  18. INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
  19. INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
  20. INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
  21. INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
  22. INTEL_EVENT_CONSTRAINT(0xc1, 0x1), /* FP_COMP_INSTR_RET */
  23. EVENT_CONSTRAINT_END
  24. };
  25. static struct event_constraint intel_core2_event_constraints[] =
  26. {
  27. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  28. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  29. /*
  30. * Core2 has Fixed Counter 2 listed as CPU_CLK_UNHALTED.REF and event
  31. * 0x013c as CPU_CLK_UNHALTED.BUS and specifies there is a fixed
  32. * ratio between these counters.
  33. */
  34. /* FIXED_EVENT_CONSTRAINT(0x013c, 2), CPU_CLK_UNHALTED.REF */
  35. INTEL_EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */
  36. INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
  37. INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
  38. INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
  39. INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
  40. INTEL_EVENT_CONSTRAINT(0x18, 0x1), /* IDLE_DURING_DIV */
  41. INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
  42. INTEL_EVENT_CONSTRAINT(0xa1, 0x1), /* RS_UOPS_DISPATCH_CYCLES */
  43. INTEL_EVENT_CONSTRAINT(0xc9, 0x1), /* ITLB_MISS_RETIRED (T30-9) */
  44. INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED */
  45. EVENT_CONSTRAINT_END
  46. };
  47. static struct event_constraint intel_nehalem_event_constraints[] =
  48. {
  49. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  50. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  51. /* FIXED_EVENT_CONSTRAINT(0x013c, 2), CPU_CLK_UNHALTED.REF */
  52. INTEL_EVENT_CONSTRAINT(0x40, 0x3), /* L1D_CACHE_LD */
  53. INTEL_EVENT_CONSTRAINT(0x41, 0x3), /* L1D_CACHE_ST */
  54. INTEL_EVENT_CONSTRAINT(0x42, 0x3), /* L1D_CACHE_LOCK */
  55. INTEL_EVENT_CONSTRAINT(0x43, 0x3), /* L1D_ALL_REF */
  56. INTEL_EVENT_CONSTRAINT(0x48, 0x3), /* L1D_PEND_MISS */
  57. INTEL_EVENT_CONSTRAINT(0x4e, 0x3), /* L1D_PREFETCH */
  58. INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
  59. INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
  60. EVENT_CONSTRAINT_END
  61. };
  62. static struct event_constraint intel_westmere_event_constraints[] =
  63. {
  64. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  65. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  66. /* FIXED_EVENT_CONSTRAINT(0x013c, 2), CPU_CLK_UNHALTED.REF */
  67. INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
  68. INTEL_EVENT_CONSTRAINT(0x60, 0x1), /* OFFCORE_REQUESTS_OUTSTANDING */
  69. INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
  70. EVENT_CONSTRAINT_END
  71. };
  72. static struct event_constraint intel_gen_event_constraints[] =
  73. {
  74. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  75. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  76. /* FIXED_EVENT_CONSTRAINT(0x013c, 2), CPU_CLK_UNHALTED.REF */
  77. EVENT_CONSTRAINT_END
  78. };
  79. static u64 intel_pmu_event_map(int hw_event)
  80. {
  81. return intel_perfmon_event_map[hw_event];
  82. }
  83. static __initconst const u64 westmere_hw_cache_event_ids
  84. [PERF_COUNT_HW_CACHE_MAX]
  85. [PERF_COUNT_HW_CACHE_OP_MAX]
  86. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  87. {
  88. [ C(L1D) ] = {
  89. [ C(OP_READ) ] = {
  90. [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
  91. [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */
  92. },
  93. [ C(OP_WRITE) ] = {
  94. [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
  95. [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */
  96. },
  97. [ C(OP_PREFETCH) ] = {
  98. [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
  99. [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
  100. },
  101. },
  102. [ C(L1I ) ] = {
  103. [ C(OP_READ) ] = {
  104. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  105. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  106. },
  107. [ C(OP_WRITE) ] = {
  108. [ C(RESULT_ACCESS) ] = -1,
  109. [ C(RESULT_MISS) ] = -1,
  110. },
  111. [ C(OP_PREFETCH) ] = {
  112. [ C(RESULT_ACCESS) ] = 0x0,
  113. [ C(RESULT_MISS) ] = 0x0,
  114. },
  115. },
  116. [ C(LL ) ] = {
  117. [ C(OP_READ) ] = {
  118. [ C(RESULT_ACCESS) ] = 0x0324, /* L2_RQSTS.LOADS */
  119. [ C(RESULT_MISS) ] = 0x0224, /* L2_RQSTS.LD_MISS */
  120. },
  121. [ C(OP_WRITE) ] = {
  122. [ C(RESULT_ACCESS) ] = 0x0c24, /* L2_RQSTS.RFOS */
  123. [ C(RESULT_MISS) ] = 0x0824, /* L2_RQSTS.RFO_MISS */
  124. },
  125. [ C(OP_PREFETCH) ] = {
  126. [ C(RESULT_ACCESS) ] = 0x4f2e, /* LLC Reference */
  127. [ C(RESULT_MISS) ] = 0x412e, /* LLC Misses */
  128. },
  129. },
  130. [ C(DTLB) ] = {
  131. [ C(OP_READ) ] = {
  132. [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
  133. [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
  134. },
  135. [ C(OP_WRITE) ] = {
  136. [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
  137. [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
  138. },
  139. [ C(OP_PREFETCH) ] = {
  140. [ C(RESULT_ACCESS) ] = 0x0,
  141. [ C(RESULT_MISS) ] = 0x0,
  142. },
  143. },
  144. [ C(ITLB) ] = {
  145. [ C(OP_READ) ] = {
  146. [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
  147. [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.ANY */
  148. },
  149. [ C(OP_WRITE) ] = {
  150. [ C(RESULT_ACCESS) ] = -1,
  151. [ C(RESULT_MISS) ] = -1,
  152. },
  153. [ C(OP_PREFETCH) ] = {
  154. [ C(RESULT_ACCESS) ] = -1,
  155. [ C(RESULT_MISS) ] = -1,
  156. },
  157. },
  158. [ C(BPU ) ] = {
  159. [ C(OP_READ) ] = {
  160. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
  161. [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
  162. },
  163. [ C(OP_WRITE) ] = {
  164. [ C(RESULT_ACCESS) ] = -1,
  165. [ C(RESULT_MISS) ] = -1,
  166. },
  167. [ C(OP_PREFETCH) ] = {
  168. [ C(RESULT_ACCESS) ] = -1,
  169. [ C(RESULT_MISS) ] = -1,
  170. },
  171. },
  172. };
  173. static __initconst const u64 nehalem_hw_cache_event_ids
  174. [PERF_COUNT_HW_CACHE_MAX]
  175. [PERF_COUNT_HW_CACHE_OP_MAX]
  176. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  177. {
  178. [ C(L1D) ] = {
  179. [ C(OP_READ) ] = {
  180. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
  181. [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
  182. },
  183. [ C(OP_WRITE) ] = {
  184. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
  185. [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
  186. },
  187. [ C(OP_PREFETCH) ] = {
  188. [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
  189. [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
  190. },
  191. },
  192. [ C(L1I ) ] = {
  193. [ C(OP_READ) ] = {
  194. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  195. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  196. },
  197. [ C(OP_WRITE) ] = {
  198. [ C(RESULT_ACCESS) ] = -1,
  199. [ C(RESULT_MISS) ] = -1,
  200. },
  201. [ C(OP_PREFETCH) ] = {
  202. [ C(RESULT_ACCESS) ] = 0x0,
  203. [ C(RESULT_MISS) ] = 0x0,
  204. },
  205. },
  206. [ C(LL ) ] = {
  207. [ C(OP_READ) ] = {
  208. [ C(RESULT_ACCESS) ] = 0x0324, /* L2_RQSTS.LOADS */
  209. [ C(RESULT_MISS) ] = 0x0224, /* L2_RQSTS.LD_MISS */
  210. },
  211. [ C(OP_WRITE) ] = {
  212. [ C(RESULT_ACCESS) ] = 0x0c24, /* L2_RQSTS.RFOS */
  213. [ C(RESULT_MISS) ] = 0x0824, /* L2_RQSTS.RFO_MISS */
  214. },
  215. [ C(OP_PREFETCH) ] = {
  216. [ C(RESULT_ACCESS) ] = 0x4f2e, /* LLC Reference */
  217. [ C(RESULT_MISS) ] = 0x412e, /* LLC Misses */
  218. },
  219. },
  220. [ C(DTLB) ] = {
  221. [ C(OP_READ) ] = {
  222. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
  223. [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
  224. },
  225. [ C(OP_WRITE) ] = {
  226. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
  227. [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
  228. },
  229. [ C(OP_PREFETCH) ] = {
  230. [ C(RESULT_ACCESS) ] = 0x0,
  231. [ C(RESULT_MISS) ] = 0x0,
  232. },
  233. },
  234. [ C(ITLB) ] = {
  235. [ C(OP_READ) ] = {
  236. [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
  237. [ C(RESULT_MISS) ] = 0x20c8, /* ITLB_MISS_RETIRED */
  238. },
  239. [ C(OP_WRITE) ] = {
  240. [ C(RESULT_ACCESS) ] = -1,
  241. [ C(RESULT_MISS) ] = -1,
  242. },
  243. [ C(OP_PREFETCH) ] = {
  244. [ C(RESULT_ACCESS) ] = -1,
  245. [ C(RESULT_MISS) ] = -1,
  246. },
  247. },
  248. [ C(BPU ) ] = {
  249. [ C(OP_READ) ] = {
  250. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
  251. [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
  252. },
  253. [ C(OP_WRITE) ] = {
  254. [ C(RESULT_ACCESS) ] = -1,
  255. [ C(RESULT_MISS) ] = -1,
  256. },
  257. [ C(OP_PREFETCH) ] = {
  258. [ C(RESULT_ACCESS) ] = -1,
  259. [ C(RESULT_MISS) ] = -1,
  260. },
  261. },
  262. };
  263. static __initconst const u64 core2_hw_cache_event_ids
  264. [PERF_COUNT_HW_CACHE_MAX]
  265. [PERF_COUNT_HW_CACHE_OP_MAX]
  266. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  267. {
  268. [ C(L1D) ] = {
  269. [ C(OP_READ) ] = {
  270. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
  271. [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
  272. },
  273. [ C(OP_WRITE) ] = {
  274. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
  275. [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
  276. },
  277. [ C(OP_PREFETCH) ] = {
  278. [ C(RESULT_ACCESS) ] = 0x104e, /* L1D_PREFETCH.REQUESTS */
  279. [ C(RESULT_MISS) ] = 0,
  280. },
  281. },
  282. [ C(L1I ) ] = {
  283. [ C(OP_READ) ] = {
  284. [ C(RESULT_ACCESS) ] = 0x0080, /* L1I.READS */
  285. [ C(RESULT_MISS) ] = 0x0081, /* L1I.MISSES */
  286. },
  287. [ C(OP_WRITE) ] = {
  288. [ C(RESULT_ACCESS) ] = -1,
  289. [ C(RESULT_MISS) ] = -1,
  290. },
  291. [ C(OP_PREFETCH) ] = {
  292. [ C(RESULT_ACCESS) ] = 0,
  293. [ C(RESULT_MISS) ] = 0,
  294. },
  295. },
  296. [ C(LL ) ] = {
  297. [ C(OP_READ) ] = {
  298. [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
  299. [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
  300. },
  301. [ C(OP_WRITE) ] = {
  302. [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
  303. [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
  304. },
  305. [ C(OP_PREFETCH) ] = {
  306. [ C(RESULT_ACCESS) ] = 0,
  307. [ C(RESULT_MISS) ] = 0,
  308. },
  309. },
  310. [ C(DTLB) ] = {
  311. [ C(OP_READ) ] = {
  312. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
  313. [ C(RESULT_MISS) ] = 0x0208, /* DTLB_MISSES.MISS_LD */
  314. },
  315. [ C(OP_WRITE) ] = {
  316. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
  317. [ C(RESULT_MISS) ] = 0x0808, /* DTLB_MISSES.MISS_ST */
  318. },
  319. [ C(OP_PREFETCH) ] = {
  320. [ C(RESULT_ACCESS) ] = 0,
  321. [ C(RESULT_MISS) ] = 0,
  322. },
  323. },
  324. [ C(ITLB) ] = {
  325. [ C(OP_READ) ] = {
  326. [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
  327. [ C(RESULT_MISS) ] = 0x1282, /* ITLBMISSES */
  328. },
  329. [ C(OP_WRITE) ] = {
  330. [ C(RESULT_ACCESS) ] = -1,
  331. [ C(RESULT_MISS) ] = -1,
  332. },
  333. [ C(OP_PREFETCH) ] = {
  334. [ C(RESULT_ACCESS) ] = -1,
  335. [ C(RESULT_MISS) ] = -1,
  336. },
  337. },
  338. [ C(BPU ) ] = {
  339. [ C(OP_READ) ] = {
  340. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
  341. [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
  342. },
  343. [ C(OP_WRITE) ] = {
  344. [ C(RESULT_ACCESS) ] = -1,
  345. [ C(RESULT_MISS) ] = -1,
  346. },
  347. [ C(OP_PREFETCH) ] = {
  348. [ C(RESULT_ACCESS) ] = -1,
  349. [ C(RESULT_MISS) ] = -1,
  350. },
  351. },
  352. };
  353. static __initconst const u64 atom_hw_cache_event_ids
  354. [PERF_COUNT_HW_CACHE_MAX]
  355. [PERF_COUNT_HW_CACHE_OP_MAX]
  356. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  357. {
  358. [ C(L1D) ] = {
  359. [ C(OP_READ) ] = {
  360. [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE.LD */
  361. [ C(RESULT_MISS) ] = 0,
  362. },
  363. [ C(OP_WRITE) ] = {
  364. [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE.ST */
  365. [ C(RESULT_MISS) ] = 0,
  366. },
  367. [ C(OP_PREFETCH) ] = {
  368. [ C(RESULT_ACCESS) ] = 0x0,
  369. [ C(RESULT_MISS) ] = 0,
  370. },
  371. },
  372. [ C(L1I ) ] = {
  373. [ C(OP_READ) ] = {
  374. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  375. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  376. },
  377. [ C(OP_WRITE) ] = {
  378. [ C(RESULT_ACCESS) ] = -1,
  379. [ C(RESULT_MISS) ] = -1,
  380. },
  381. [ C(OP_PREFETCH) ] = {
  382. [ C(RESULT_ACCESS) ] = 0,
  383. [ C(RESULT_MISS) ] = 0,
  384. },
  385. },
  386. [ C(LL ) ] = {
  387. [ C(OP_READ) ] = {
  388. [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
  389. [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
  390. },
  391. [ C(OP_WRITE) ] = {
  392. [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
  393. [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
  394. },
  395. [ C(OP_PREFETCH) ] = {
  396. [ C(RESULT_ACCESS) ] = 0,
  397. [ C(RESULT_MISS) ] = 0,
  398. },
  399. },
  400. [ C(DTLB) ] = {
  401. [ C(OP_READ) ] = {
  402. [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE_LD.MESI (alias) */
  403. [ C(RESULT_MISS) ] = 0x0508, /* DTLB_MISSES.MISS_LD */
  404. },
  405. [ C(OP_WRITE) ] = {
  406. [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE_ST.MESI (alias) */
  407. [ C(RESULT_MISS) ] = 0x0608, /* DTLB_MISSES.MISS_ST */
  408. },
  409. [ C(OP_PREFETCH) ] = {
  410. [ C(RESULT_ACCESS) ] = 0,
  411. [ C(RESULT_MISS) ] = 0,
  412. },
  413. },
  414. [ C(ITLB) ] = {
  415. [ C(OP_READ) ] = {
  416. [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
  417. [ C(RESULT_MISS) ] = 0x0282, /* ITLB.MISSES */
  418. },
  419. [ C(OP_WRITE) ] = {
  420. [ C(RESULT_ACCESS) ] = -1,
  421. [ C(RESULT_MISS) ] = -1,
  422. },
  423. [ C(OP_PREFETCH) ] = {
  424. [ C(RESULT_ACCESS) ] = -1,
  425. [ C(RESULT_MISS) ] = -1,
  426. },
  427. },
  428. [ C(BPU ) ] = {
  429. [ C(OP_READ) ] = {
  430. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
  431. [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
  432. },
  433. [ C(OP_WRITE) ] = {
  434. [ C(RESULT_ACCESS) ] = -1,
  435. [ C(RESULT_MISS) ] = -1,
  436. },
  437. [ C(OP_PREFETCH) ] = {
  438. [ C(RESULT_ACCESS) ] = -1,
  439. [ C(RESULT_MISS) ] = -1,
  440. },
  441. },
  442. };
  443. static void intel_pmu_disable_all(void)
  444. {
  445. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  446. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  447. if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask))
  448. intel_pmu_disable_bts();
  449. intel_pmu_pebs_disable_all();
  450. intel_pmu_lbr_disable_all();
  451. }
  452. static void intel_pmu_enable_all(int added)
  453. {
  454. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  455. intel_pmu_pebs_enable_all();
  456. intel_pmu_lbr_enable_all();
  457. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl);
  458. if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask)) {
  459. struct perf_event *event =
  460. cpuc->events[X86_PMC_IDX_FIXED_BTS];
  461. if (WARN_ON_ONCE(!event))
  462. return;
  463. intel_pmu_enable_bts(event->hw.config);
  464. }
  465. }
  466. /*
  467. * Workaround for:
  468. * Intel Errata AAK100 (model 26)
  469. * Intel Errata AAP53 (model 30)
  470. * Intel Errata BD53 (model 44)
  471. *
  472. * These chips need to be 'reset' when adding counters by programming
  473. * the magic three (non counting) events 0x4300D2, 0x4300B1 and 0x4300B5
  474. * either in sequence on the same PMC or on different PMCs.
  475. */
  476. static void intel_pmu_nhm_enable_all(int added)
  477. {
  478. if (added) {
  479. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  480. int i;
  481. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + 0, 0x4300D2);
  482. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + 1, 0x4300B1);
  483. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + 2, 0x4300B5);
  484. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0x3);
  485. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0x0);
  486. for (i = 0; i < 3; i++) {
  487. struct perf_event *event = cpuc->events[i];
  488. if (!event)
  489. continue;
  490. __x86_pmu_enable_event(&event->hw);
  491. }
  492. }
  493. intel_pmu_enable_all(added);
  494. }
  495. static inline u64 intel_pmu_get_status(void)
  496. {
  497. u64 status;
  498. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  499. return status;
  500. }
  501. static inline void intel_pmu_ack_status(u64 ack)
  502. {
  503. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  504. }
  505. static void intel_pmu_disable_fixed(struct hw_perf_event *hwc)
  506. {
  507. int idx = hwc->idx - X86_PMC_IDX_FIXED;
  508. u64 ctrl_val, mask;
  509. mask = 0xfULL << (idx * 4);
  510. rdmsrl(hwc->config_base, ctrl_val);
  511. ctrl_val &= ~mask;
  512. wrmsrl(hwc->config_base, ctrl_val);
  513. }
  514. static void intel_pmu_disable_event(struct perf_event *event)
  515. {
  516. struct hw_perf_event *hwc = &event->hw;
  517. if (unlikely(hwc->idx == X86_PMC_IDX_FIXED_BTS)) {
  518. intel_pmu_disable_bts();
  519. intel_pmu_drain_bts_buffer();
  520. return;
  521. }
  522. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  523. intel_pmu_disable_fixed(hwc);
  524. return;
  525. }
  526. x86_pmu_disable_event(event);
  527. if (unlikely(event->attr.precise))
  528. intel_pmu_pebs_disable(event);
  529. }
  530. static void intel_pmu_enable_fixed(struct hw_perf_event *hwc)
  531. {
  532. int idx = hwc->idx - X86_PMC_IDX_FIXED;
  533. u64 ctrl_val, bits, mask;
  534. /*
  535. * Enable IRQ generation (0x8),
  536. * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
  537. * if requested:
  538. */
  539. bits = 0x8ULL;
  540. if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
  541. bits |= 0x2;
  542. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  543. bits |= 0x1;
  544. /*
  545. * ANY bit is supported in v3 and up
  546. */
  547. if (x86_pmu.version > 2 && hwc->config & ARCH_PERFMON_EVENTSEL_ANY)
  548. bits |= 0x4;
  549. bits <<= (idx * 4);
  550. mask = 0xfULL << (idx * 4);
  551. rdmsrl(hwc->config_base, ctrl_val);
  552. ctrl_val &= ~mask;
  553. ctrl_val |= bits;
  554. wrmsrl(hwc->config_base, ctrl_val);
  555. }
  556. static void intel_pmu_enable_event(struct perf_event *event)
  557. {
  558. struct hw_perf_event *hwc = &event->hw;
  559. if (unlikely(hwc->idx == X86_PMC_IDX_FIXED_BTS)) {
  560. if (!__get_cpu_var(cpu_hw_events).enabled)
  561. return;
  562. intel_pmu_enable_bts(hwc->config);
  563. return;
  564. }
  565. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  566. intel_pmu_enable_fixed(hwc);
  567. return;
  568. }
  569. if (unlikely(event->attr.precise))
  570. intel_pmu_pebs_enable(event);
  571. __x86_pmu_enable_event(hwc);
  572. }
  573. /*
  574. * Save and restart an expired event. Called by NMI contexts,
  575. * so it has to be careful about preempting normal event ops:
  576. */
  577. static int intel_pmu_save_and_restart(struct perf_event *event)
  578. {
  579. x86_perf_event_update(event);
  580. return x86_perf_event_set_period(event);
  581. }
  582. static void intel_pmu_reset(void)
  583. {
  584. struct debug_store *ds = __get_cpu_var(cpu_hw_events).ds;
  585. unsigned long flags;
  586. int idx;
  587. if (!x86_pmu.num_counters)
  588. return;
  589. local_irq_save(flags);
  590. printk("clearing PMU state on CPU#%d\n", smp_processor_id());
  591. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  592. checking_wrmsrl(x86_pmu.eventsel + idx, 0ull);
  593. checking_wrmsrl(x86_pmu.perfctr + idx, 0ull);
  594. }
  595. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++)
  596. checking_wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull);
  597. if (ds)
  598. ds->bts_index = ds->bts_buffer_base;
  599. local_irq_restore(flags);
  600. }
  601. /*
  602. * This handler is triggered by the local APIC, so the APIC IRQ handling
  603. * rules apply:
  604. */
  605. static int intel_pmu_handle_irq(struct pt_regs *regs)
  606. {
  607. struct perf_sample_data data;
  608. struct cpu_hw_events *cpuc;
  609. int bit, loops;
  610. u64 ack, status;
  611. perf_sample_data_init(&data, 0);
  612. cpuc = &__get_cpu_var(cpu_hw_events);
  613. intel_pmu_disable_all();
  614. intel_pmu_drain_bts_buffer();
  615. status = intel_pmu_get_status();
  616. if (!status) {
  617. intel_pmu_enable_all(0);
  618. return 0;
  619. }
  620. loops = 0;
  621. again:
  622. if (++loops > 100) {
  623. WARN_ONCE(1, "perfevents: irq loop stuck!\n");
  624. perf_event_print_debug();
  625. intel_pmu_reset();
  626. goto done;
  627. }
  628. inc_irq_stat(apic_perf_irqs);
  629. ack = status;
  630. intel_pmu_lbr_read();
  631. /*
  632. * PEBS overflow sets bit 62 in the global status register
  633. */
  634. if (__test_and_clear_bit(62, (unsigned long *)&status))
  635. x86_pmu.drain_pebs(regs);
  636. for_each_set_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  637. struct perf_event *event = cpuc->events[bit];
  638. if (!test_bit(bit, cpuc->active_mask))
  639. continue;
  640. if (!intel_pmu_save_and_restart(event))
  641. continue;
  642. data.period = event->hw.last_period;
  643. if (perf_event_overflow(event, 1, &data, regs))
  644. x86_pmu_stop(event);
  645. }
  646. intel_pmu_ack_status(ack);
  647. /*
  648. * Repeat if there is more work to be done:
  649. */
  650. status = intel_pmu_get_status();
  651. if (status)
  652. goto again;
  653. done:
  654. intel_pmu_enable_all(0);
  655. return 1;
  656. }
  657. static struct event_constraint *
  658. intel_bts_constraints(struct perf_event *event)
  659. {
  660. struct hw_perf_event *hwc = &event->hw;
  661. unsigned int hw_event, bts_event;
  662. hw_event = hwc->config & INTEL_ARCH_EVENT_MASK;
  663. bts_event = x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS);
  664. if (unlikely(hw_event == bts_event && hwc->sample_period == 1))
  665. return &bts_constraint;
  666. return NULL;
  667. }
  668. static struct event_constraint *
  669. intel_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  670. {
  671. struct event_constraint *c;
  672. c = intel_bts_constraints(event);
  673. if (c)
  674. return c;
  675. c = intel_pebs_constraints(event);
  676. if (c)
  677. return c;
  678. return x86_get_event_constraints(cpuc, event);
  679. }
  680. static int intel_pmu_hw_config(struct perf_event *event)
  681. {
  682. int ret = x86_pmu_hw_config(event);
  683. if (ret)
  684. return ret;
  685. if (event->attr.type != PERF_TYPE_RAW)
  686. return 0;
  687. if (!(event->attr.config & ARCH_PERFMON_EVENTSEL_ANY))
  688. return 0;
  689. if (x86_pmu.version < 3)
  690. return -EINVAL;
  691. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  692. return -EACCES;
  693. event->hw.config |= ARCH_PERFMON_EVENTSEL_ANY;
  694. return 0;
  695. }
  696. static __initconst const struct x86_pmu core_pmu = {
  697. .name = "core",
  698. .handle_irq = x86_pmu_handle_irq,
  699. .disable_all = x86_pmu_disable_all,
  700. .enable_all = x86_pmu_enable_all,
  701. .enable = x86_pmu_enable_event,
  702. .disable = x86_pmu_disable_event,
  703. .hw_config = x86_pmu_hw_config,
  704. .schedule_events = x86_schedule_events,
  705. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  706. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  707. .event_map = intel_pmu_event_map,
  708. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  709. .apic = 1,
  710. /*
  711. * Intel PMCs cannot be accessed sanely above 32 bit width,
  712. * so we install an artificial 1<<31 period regardless of
  713. * the generic event period:
  714. */
  715. .max_period = (1ULL << 31) - 1,
  716. .get_event_constraints = intel_get_event_constraints,
  717. .event_constraints = intel_core_event_constraints,
  718. };
  719. static void intel_pmu_cpu_starting(int cpu)
  720. {
  721. init_debug_store_on_cpu(cpu);
  722. /*
  723. * Deal with CPUs that don't clear their LBRs on power-up.
  724. */
  725. intel_pmu_lbr_reset();
  726. }
  727. static void intel_pmu_cpu_dying(int cpu)
  728. {
  729. fini_debug_store_on_cpu(cpu);
  730. }
  731. static __initconst const struct x86_pmu intel_pmu = {
  732. .name = "Intel",
  733. .handle_irq = intel_pmu_handle_irq,
  734. .disable_all = intel_pmu_disable_all,
  735. .enable_all = intel_pmu_enable_all,
  736. .enable = intel_pmu_enable_event,
  737. .disable = intel_pmu_disable_event,
  738. .hw_config = intel_pmu_hw_config,
  739. .schedule_events = x86_schedule_events,
  740. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  741. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  742. .event_map = intel_pmu_event_map,
  743. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  744. .apic = 1,
  745. /*
  746. * Intel PMCs cannot be accessed sanely above 32 bit width,
  747. * so we install an artificial 1<<31 period regardless of
  748. * the generic event period:
  749. */
  750. .max_period = (1ULL << 31) - 1,
  751. .get_event_constraints = intel_get_event_constraints,
  752. .cpu_starting = intel_pmu_cpu_starting,
  753. .cpu_dying = intel_pmu_cpu_dying,
  754. };
  755. static void intel_clovertown_quirks(void)
  756. {
  757. /*
  758. * PEBS is unreliable due to:
  759. *
  760. * AJ67 - PEBS may experience CPL leaks
  761. * AJ68 - PEBS PMI may be delayed by one event
  762. * AJ69 - GLOBAL_STATUS[62] will only be set when DEBUGCTL[12]
  763. * AJ106 - FREEZE_LBRS_ON_PMI doesn't work in combination with PEBS
  764. *
  765. * AJ67 could be worked around by restricting the OS/USR flags.
  766. * AJ69 could be worked around by setting PMU_FREEZE_ON_PMI.
  767. *
  768. * AJ106 could possibly be worked around by not allowing LBR
  769. * usage from PEBS, including the fixup.
  770. * AJ68 could possibly be worked around by always programming
  771. * a pebs_event_reset[0] value and coping with the lost events.
  772. *
  773. * But taken together it might just make sense to not enable PEBS on
  774. * these chips.
  775. */
  776. printk(KERN_WARNING "PEBS disabled due to CPU errata.\n");
  777. x86_pmu.pebs = 0;
  778. x86_pmu.pebs_constraints = NULL;
  779. }
  780. static __init int intel_pmu_init(void)
  781. {
  782. union cpuid10_edx edx;
  783. union cpuid10_eax eax;
  784. unsigned int unused;
  785. unsigned int ebx;
  786. int version;
  787. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
  788. switch (boot_cpu_data.x86) {
  789. case 0x6:
  790. return p6_pmu_init();
  791. case 0xf:
  792. return p4_pmu_init();
  793. }
  794. return -ENODEV;
  795. }
  796. /*
  797. * Check whether the Architectural PerfMon supports
  798. * Branch Misses Retired hw_event or not.
  799. */
  800. cpuid(10, &eax.full, &ebx, &unused, &edx.full);
  801. if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
  802. return -ENODEV;
  803. version = eax.split.version_id;
  804. if (version < 2)
  805. x86_pmu = core_pmu;
  806. else
  807. x86_pmu = intel_pmu;
  808. x86_pmu.version = version;
  809. x86_pmu.num_counters = eax.split.num_counters;
  810. x86_pmu.cntval_bits = eax.split.bit_width;
  811. x86_pmu.cntval_mask = (1ULL << eax.split.bit_width) - 1;
  812. /*
  813. * Quirk: v2 perfmon does not report fixed-purpose events, so
  814. * assume at least 3 events:
  815. */
  816. if (version > 1)
  817. x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3);
  818. /*
  819. * v2 and above have a perf capabilities MSR
  820. */
  821. if (version > 1) {
  822. u64 capabilities;
  823. rdmsrl(MSR_IA32_PERF_CAPABILITIES, capabilities);
  824. x86_pmu.intel_cap.capabilities = capabilities;
  825. }
  826. intel_ds_init();
  827. /*
  828. * Install the hw-cache-events table:
  829. */
  830. switch (boot_cpu_data.x86_model) {
  831. case 14: /* 65 nm core solo/duo, "Yonah" */
  832. pr_cont("Core events, ");
  833. break;
  834. case 15: /* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */
  835. x86_pmu.quirks = intel_clovertown_quirks;
  836. case 22: /* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */
  837. case 23: /* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */
  838. case 29: /* six-core 45 nm xeon "Dunnington" */
  839. memcpy(hw_cache_event_ids, core2_hw_cache_event_ids,
  840. sizeof(hw_cache_event_ids));
  841. intel_pmu_lbr_init_core();
  842. x86_pmu.event_constraints = intel_core2_event_constraints;
  843. pr_cont("Core2 events, ");
  844. break;
  845. case 26: /* 45 nm nehalem, "Bloomfield" */
  846. case 30: /* 45 nm nehalem, "Lynnfield" */
  847. case 46: /* 45 nm nehalem-ex, "Beckton" */
  848. memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids,
  849. sizeof(hw_cache_event_ids));
  850. intel_pmu_lbr_init_nhm();
  851. x86_pmu.event_constraints = intel_nehalem_event_constraints;
  852. x86_pmu.enable_all = intel_pmu_nhm_enable_all;
  853. pr_cont("Nehalem events, ");
  854. break;
  855. case 28: /* Atom */
  856. memcpy(hw_cache_event_ids, atom_hw_cache_event_ids,
  857. sizeof(hw_cache_event_ids));
  858. intel_pmu_lbr_init_atom();
  859. x86_pmu.event_constraints = intel_gen_event_constraints;
  860. pr_cont("Atom events, ");
  861. break;
  862. case 37: /* 32 nm nehalem, "Clarkdale" */
  863. case 44: /* 32 nm nehalem, "Gulftown" */
  864. memcpy(hw_cache_event_ids, westmere_hw_cache_event_ids,
  865. sizeof(hw_cache_event_ids));
  866. intel_pmu_lbr_init_nhm();
  867. x86_pmu.event_constraints = intel_westmere_event_constraints;
  868. x86_pmu.enable_all = intel_pmu_nhm_enable_all;
  869. pr_cont("Westmere events, ");
  870. break;
  871. default:
  872. /*
  873. * default constraints for v2 and up
  874. */
  875. x86_pmu.event_constraints = intel_gen_event_constraints;
  876. pr_cont("generic architected perfmon, ");
  877. }
  878. return 0;
  879. }
  880. #else /* CONFIG_CPU_SUP_INTEL */
  881. static int intel_pmu_init(void)
  882. {
  883. return 0;
  884. }
  885. #endif /* CONFIG_CPU_SUP_INTEL */