perf_event_intel.c 25 KB

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