perf_event_intel.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  1. /*
  2. * Per core/cpu state
  3. *
  4. * Used to coordinate shared registers between HT threads or
  5. * among events on a single PMU.
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/stddef.h>
  9. #include <linux/types.h>
  10. #include <linux/init.h>
  11. #include <linux/slab.h>
  12. #include <linux/export.h>
  13. #include <asm/hardirq.h>
  14. #include <asm/apic.h>
  15. #include "perf_event.h"
  16. /*
  17. * Intel PerfMon, used on Core and later.
  18. */
  19. static u64 intel_perfmon_event_map[PERF_COUNT_HW_MAX] __read_mostly =
  20. {
  21. [PERF_COUNT_HW_CPU_CYCLES] = 0x003c,
  22. [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
  23. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x4f2e,
  24. [PERF_COUNT_HW_CACHE_MISSES] = 0x412e,
  25. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
  26. [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
  27. [PERF_COUNT_HW_BUS_CYCLES] = 0x013c,
  28. [PERF_COUNT_HW_REF_CPU_CYCLES] = 0x0300, /* pseudo-encoding */
  29. };
  30. static struct event_constraint intel_core_event_constraints[] __read_mostly =
  31. {
  32. INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
  33. INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
  34. INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
  35. INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
  36. INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
  37. INTEL_EVENT_CONSTRAINT(0xc1, 0x1), /* FP_COMP_INSTR_RET */
  38. EVENT_CONSTRAINT_END
  39. };
  40. static struct event_constraint intel_core2_event_constraints[] __read_mostly =
  41. {
  42. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  43. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  44. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  45. INTEL_EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */
  46. INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
  47. INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
  48. INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
  49. INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
  50. INTEL_EVENT_CONSTRAINT(0x18, 0x1), /* IDLE_DURING_DIV */
  51. INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
  52. INTEL_EVENT_CONSTRAINT(0xa1, 0x1), /* RS_UOPS_DISPATCH_CYCLES */
  53. INTEL_EVENT_CONSTRAINT(0xc9, 0x1), /* ITLB_MISS_RETIRED (T30-9) */
  54. INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED */
  55. EVENT_CONSTRAINT_END
  56. };
  57. static struct event_constraint intel_nehalem_event_constraints[] __read_mostly =
  58. {
  59. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  60. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  61. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  62. INTEL_EVENT_CONSTRAINT(0x40, 0x3), /* L1D_CACHE_LD */
  63. INTEL_EVENT_CONSTRAINT(0x41, 0x3), /* L1D_CACHE_ST */
  64. INTEL_EVENT_CONSTRAINT(0x42, 0x3), /* L1D_CACHE_LOCK */
  65. INTEL_EVENT_CONSTRAINT(0x43, 0x3), /* L1D_ALL_REF */
  66. INTEL_EVENT_CONSTRAINT(0x48, 0x3), /* L1D_PEND_MISS */
  67. INTEL_EVENT_CONSTRAINT(0x4e, 0x3), /* L1D_PREFETCH */
  68. INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
  69. INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
  70. EVENT_CONSTRAINT_END
  71. };
  72. static struct extra_reg intel_nehalem_extra_regs[] __read_mostly =
  73. {
  74. INTEL_EVENT_EXTRA_REG(0xb7, MSR_OFFCORE_RSP_0, 0xffff, RSP_0),
  75. EVENT_EXTRA_END
  76. };
  77. static struct event_constraint intel_westmere_event_constraints[] __read_mostly =
  78. {
  79. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  80. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  81. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  82. INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
  83. INTEL_EVENT_CONSTRAINT(0x60, 0x1), /* OFFCORE_REQUESTS_OUTSTANDING */
  84. INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
  85. INTEL_EVENT_CONSTRAINT(0xb3, 0x1), /* SNOOPQ_REQUEST_OUTSTANDING */
  86. EVENT_CONSTRAINT_END
  87. };
  88. static struct event_constraint intel_snb_event_constraints[] __read_mostly =
  89. {
  90. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  91. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  92. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  93. INTEL_EVENT_CONSTRAINT(0x48, 0x4), /* L1D_PEND_MISS.PENDING */
  94. INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
  95. INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.LOAD_LATENCY */
  96. EVENT_CONSTRAINT_END
  97. };
  98. static struct extra_reg intel_westmere_extra_regs[] __read_mostly =
  99. {
  100. INTEL_EVENT_EXTRA_REG(0xb7, MSR_OFFCORE_RSP_0, 0xffff, RSP_0),
  101. INTEL_EVENT_EXTRA_REG(0xbb, MSR_OFFCORE_RSP_1, 0xffff, RSP_1),
  102. EVENT_EXTRA_END
  103. };
  104. static struct event_constraint intel_v1_event_constraints[] __read_mostly =
  105. {
  106. EVENT_CONSTRAINT_END
  107. };
  108. static struct event_constraint intel_gen_event_constraints[] __read_mostly =
  109. {
  110. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  111. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  112. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  113. EVENT_CONSTRAINT_END
  114. };
  115. static struct extra_reg intel_snb_extra_regs[] __read_mostly = {
  116. INTEL_EVENT_EXTRA_REG(0xb7, MSR_OFFCORE_RSP_0, 0x3fffffffffull, RSP_0),
  117. INTEL_EVENT_EXTRA_REG(0xbb, MSR_OFFCORE_RSP_1, 0x3fffffffffull, RSP_1),
  118. EVENT_EXTRA_END
  119. };
  120. static u64 intel_pmu_event_map(int hw_event)
  121. {
  122. return intel_perfmon_event_map[hw_event];
  123. }
  124. static __initconst const u64 snb_hw_cache_event_ids
  125. [PERF_COUNT_HW_CACHE_MAX]
  126. [PERF_COUNT_HW_CACHE_OP_MAX]
  127. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  128. {
  129. [ C(L1D) ] = {
  130. [ C(OP_READ) ] = {
  131. [ C(RESULT_ACCESS) ] = 0xf1d0, /* MEM_UOP_RETIRED.LOADS */
  132. [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPLACEMENT */
  133. },
  134. [ C(OP_WRITE) ] = {
  135. [ C(RESULT_ACCESS) ] = 0xf2d0, /* MEM_UOP_RETIRED.STORES */
  136. [ C(RESULT_MISS) ] = 0x0851, /* L1D.ALL_M_REPLACEMENT */
  137. },
  138. [ C(OP_PREFETCH) ] = {
  139. [ C(RESULT_ACCESS) ] = 0x0,
  140. [ C(RESULT_MISS) ] = 0x024e, /* HW_PRE_REQ.DL1_MISS */
  141. },
  142. },
  143. [ C(L1I ) ] = {
  144. [ C(OP_READ) ] = {
  145. [ C(RESULT_ACCESS) ] = 0x0,
  146. [ C(RESULT_MISS) ] = 0x0280, /* ICACHE.MISSES */
  147. },
  148. [ C(OP_WRITE) ] = {
  149. [ C(RESULT_ACCESS) ] = -1,
  150. [ C(RESULT_MISS) ] = -1,
  151. },
  152. [ C(OP_PREFETCH) ] = {
  153. [ C(RESULT_ACCESS) ] = 0x0,
  154. [ C(RESULT_MISS) ] = 0x0,
  155. },
  156. },
  157. [ C(LL ) ] = {
  158. [ C(OP_READ) ] = {
  159. /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
  160. [ C(RESULT_ACCESS) ] = 0x01b7,
  161. /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
  162. [ C(RESULT_MISS) ] = 0x01b7,
  163. },
  164. [ C(OP_WRITE) ] = {
  165. /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
  166. [ C(RESULT_ACCESS) ] = 0x01b7,
  167. /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
  168. [ C(RESULT_MISS) ] = 0x01b7,
  169. },
  170. [ C(OP_PREFETCH) ] = {
  171. /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
  172. [ C(RESULT_ACCESS) ] = 0x01b7,
  173. /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
  174. [ C(RESULT_MISS) ] = 0x01b7,
  175. },
  176. },
  177. [ C(DTLB) ] = {
  178. [ C(OP_READ) ] = {
  179. [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOP_RETIRED.ALL_LOADS */
  180. [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.CAUSES_A_WALK */
  181. },
  182. [ C(OP_WRITE) ] = {
  183. [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOP_RETIRED.ALL_STORES */
  184. [ C(RESULT_MISS) ] = 0x0149, /* DTLB_STORE_MISSES.MISS_CAUSES_A_WALK */
  185. },
  186. [ C(OP_PREFETCH) ] = {
  187. [ C(RESULT_ACCESS) ] = 0x0,
  188. [ C(RESULT_MISS) ] = 0x0,
  189. },
  190. },
  191. [ C(ITLB) ] = {
  192. [ C(OP_READ) ] = {
  193. [ C(RESULT_ACCESS) ] = 0x1085, /* ITLB_MISSES.STLB_HIT */
  194. [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.CAUSES_A_WALK */
  195. },
  196. [ C(OP_WRITE) ] = {
  197. [ C(RESULT_ACCESS) ] = -1,
  198. [ C(RESULT_MISS) ] = -1,
  199. },
  200. [ C(OP_PREFETCH) ] = {
  201. [ C(RESULT_ACCESS) ] = -1,
  202. [ C(RESULT_MISS) ] = -1,
  203. },
  204. },
  205. [ C(BPU ) ] = {
  206. [ C(OP_READ) ] = {
  207. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
  208. [ C(RESULT_MISS) ] = 0x00c5, /* BR_MISP_RETIRED.ALL_BRANCHES */
  209. },
  210. [ C(OP_WRITE) ] = {
  211. [ C(RESULT_ACCESS) ] = -1,
  212. [ C(RESULT_MISS) ] = -1,
  213. },
  214. [ C(OP_PREFETCH) ] = {
  215. [ C(RESULT_ACCESS) ] = -1,
  216. [ C(RESULT_MISS) ] = -1,
  217. },
  218. },
  219. [ C(NODE) ] = {
  220. [ C(OP_READ) ] = {
  221. [ C(RESULT_ACCESS) ] = -1,
  222. [ C(RESULT_MISS) ] = -1,
  223. },
  224. [ C(OP_WRITE) ] = {
  225. [ C(RESULT_ACCESS) ] = -1,
  226. [ C(RESULT_MISS) ] = -1,
  227. },
  228. [ C(OP_PREFETCH) ] = {
  229. [ C(RESULT_ACCESS) ] = -1,
  230. [ C(RESULT_MISS) ] = -1,
  231. },
  232. },
  233. };
  234. static __initconst const u64 westmere_hw_cache_event_ids
  235. [PERF_COUNT_HW_CACHE_MAX]
  236. [PERF_COUNT_HW_CACHE_OP_MAX]
  237. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  238. {
  239. [ C(L1D) ] = {
  240. [ C(OP_READ) ] = {
  241. [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
  242. [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */
  243. },
  244. [ C(OP_WRITE) ] = {
  245. [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
  246. [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */
  247. },
  248. [ C(OP_PREFETCH) ] = {
  249. [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
  250. [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
  251. },
  252. },
  253. [ C(L1I ) ] = {
  254. [ C(OP_READ) ] = {
  255. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  256. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  257. },
  258. [ C(OP_WRITE) ] = {
  259. [ C(RESULT_ACCESS) ] = -1,
  260. [ C(RESULT_MISS) ] = -1,
  261. },
  262. [ C(OP_PREFETCH) ] = {
  263. [ C(RESULT_ACCESS) ] = 0x0,
  264. [ C(RESULT_MISS) ] = 0x0,
  265. },
  266. },
  267. [ C(LL ) ] = {
  268. [ C(OP_READ) ] = {
  269. /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
  270. [ C(RESULT_ACCESS) ] = 0x01b7,
  271. /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
  272. [ C(RESULT_MISS) ] = 0x01b7,
  273. },
  274. /*
  275. * Use RFO, not WRITEBACK, because a write miss would typically occur
  276. * on RFO.
  277. */
  278. [ C(OP_WRITE) ] = {
  279. /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
  280. [ C(RESULT_ACCESS) ] = 0x01b7,
  281. /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
  282. [ C(RESULT_MISS) ] = 0x01b7,
  283. },
  284. [ C(OP_PREFETCH) ] = {
  285. /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
  286. [ C(RESULT_ACCESS) ] = 0x01b7,
  287. /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
  288. [ C(RESULT_MISS) ] = 0x01b7,
  289. },
  290. },
  291. [ C(DTLB) ] = {
  292. [ C(OP_READ) ] = {
  293. [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
  294. [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
  295. },
  296. [ C(OP_WRITE) ] = {
  297. [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
  298. [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
  299. },
  300. [ C(OP_PREFETCH) ] = {
  301. [ C(RESULT_ACCESS) ] = 0x0,
  302. [ C(RESULT_MISS) ] = 0x0,
  303. },
  304. },
  305. [ C(ITLB) ] = {
  306. [ C(OP_READ) ] = {
  307. [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
  308. [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.ANY */
  309. },
  310. [ C(OP_WRITE) ] = {
  311. [ C(RESULT_ACCESS) ] = -1,
  312. [ C(RESULT_MISS) ] = -1,
  313. },
  314. [ C(OP_PREFETCH) ] = {
  315. [ C(RESULT_ACCESS) ] = -1,
  316. [ C(RESULT_MISS) ] = -1,
  317. },
  318. },
  319. [ C(BPU ) ] = {
  320. [ C(OP_READ) ] = {
  321. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
  322. [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
  323. },
  324. [ C(OP_WRITE) ] = {
  325. [ C(RESULT_ACCESS) ] = -1,
  326. [ C(RESULT_MISS) ] = -1,
  327. },
  328. [ C(OP_PREFETCH) ] = {
  329. [ C(RESULT_ACCESS) ] = -1,
  330. [ C(RESULT_MISS) ] = -1,
  331. },
  332. },
  333. [ C(NODE) ] = {
  334. [ C(OP_READ) ] = {
  335. [ C(RESULT_ACCESS) ] = 0x01b7,
  336. [ C(RESULT_MISS) ] = 0x01b7,
  337. },
  338. [ C(OP_WRITE) ] = {
  339. [ C(RESULT_ACCESS) ] = 0x01b7,
  340. [ C(RESULT_MISS) ] = 0x01b7,
  341. },
  342. [ C(OP_PREFETCH) ] = {
  343. [ C(RESULT_ACCESS) ] = 0x01b7,
  344. [ C(RESULT_MISS) ] = 0x01b7,
  345. },
  346. },
  347. };
  348. /*
  349. * Nehalem/Westmere MSR_OFFCORE_RESPONSE bits;
  350. * See IA32 SDM Vol 3B 30.6.1.3
  351. */
  352. #define NHM_DMND_DATA_RD (1 << 0)
  353. #define NHM_DMND_RFO (1 << 1)
  354. #define NHM_DMND_IFETCH (1 << 2)
  355. #define NHM_DMND_WB (1 << 3)
  356. #define NHM_PF_DATA_RD (1 << 4)
  357. #define NHM_PF_DATA_RFO (1 << 5)
  358. #define NHM_PF_IFETCH (1 << 6)
  359. #define NHM_OFFCORE_OTHER (1 << 7)
  360. #define NHM_UNCORE_HIT (1 << 8)
  361. #define NHM_OTHER_CORE_HIT_SNP (1 << 9)
  362. #define NHM_OTHER_CORE_HITM (1 << 10)
  363. /* reserved */
  364. #define NHM_REMOTE_CACHE_FWD (1 << 12)
  365. #define NHM_REMOTE_DRAM (1 << 13)
  366. #define NHM_LOCAL_DRAM (1 << 14)
  367. #define NHM_NON_DRAM (1 << 15)
  368. #define NHM_LOCAL (NHM_LOCAL_DRAM|NHM_REMOTE_CACHE_FWD)
  369. #define NHM_REMOTE (NHM_REMOTE_DRAM)
  370. #define NHM_DMND_READ (NHM_DMND_DATA_RD)
  371. #define NHM_DMND_WRITE (NHM_DMND_RFO|NHM_DMND_WB)
  372. #define NHM_DMND_PREFETCH (NHM_PF_DATA_RD|NHM_PF_DATA_RFO)
  373. #define NHM_L3_HIT (NHM_UNCORE_HIT|NHM_OTHER_CORE_HIT_SNP|NHM_OTHER_CORE_HITM)
  374. #define NHM_L3_MISS (NHM_NON_DRAM|NHM_LOCAL_DRAM|NHM_REMOTE_DRAM|NHM_REMOTE_CACHE_FWD)
  375. #define NHM_L3_ACCESS (NHM_L3_HIT|NHM_L3_MISS)
  376. static __initconst const u64 nehalem_hw_cache_extra_regs
  377. [PERF_COUNT_HW_CACHE_MAX]
  378. [PERF_COUNT_HW_CACHE_OP_MAX]
  379. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  380. {
  381. [ C(LL ) ] = {
  382. [ C(OP_READ) ] = {
  383. [ C(RESULT_ACCESS) ] = NHM_DMND_READ|NHM_L3_ACCESS,
  384. [ C(RESULT_MISS) ] = NHM_DMND_READ|NHM_L3_MISS,
  385. },
  386. [ C(OP_WRITE) ] = {
  387. [ C(RESULT_ACCESS) ] = NHM_DMND_WRITE|NHM_L3_ACCESS,
  388. [ C(RESULT_MISS) ] = NHM_DMND_WRITE|NHM_L3_MISS,
  389. },
  390. [ C(OP_PREFETCH) ] = {
  391. [ C(RESULT_ACCESS) ] = NHM_DMND_PREFETCH|NHM_L3_ACCESS,
  392. [ C(RESULT_MISS) ] = NHM_DMND_PREFETCH|NHM_L3_MISS,
  393. },
  394. },
  395. [ C(NODE) ] = {
  396. [ C(OP_READ) ] = {
  397. [ C(RESULT_ACCESS) ] = NHM_DMND_READ|NHM_LOCAL|NHM_REMOTE,
  398. [ C(RESULT_MISS) ] = NHM_DMND_READ|NHM_REMOTE,
  399. },
  400. [ C(OP_WRITE) ] = {
  401. [ C(RESULT_ACCESS) ] = NHM_DMND_WRITE|NHM_LOCAL|NHM_REMOTE,
  402. [ C(RESULT_MISS) ] = NHM_DMND_WRITE|NHM_REMOTE,
  403. },
  404. [ C(OP_PREFETCH) ] = {
  405. [ C(RESULT_ACCESS) ] = NHM_DMND_PREFETCH|NHM_LOCAL|NHM_REMOTE,
  406. [ C(RESULT_MISS) ] = NHM_DMND_PREFETCH|NHM_REMOTE,
  407. },
  408. },
  409. };
  410. static __initconst const u64 nehalem_hw_cache_event_ids
  411. [PERF_COUNT_HW_CACHE_MAX]
  412. [PERF_COUNT_HW_CACHE_OP_MAX]
  413. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  414. {
  415. [ C(L1D) ] = {
  416. [ C(OP_READ) ] = {
  417. [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
  418. [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */
  419. },
  420. [ C(OP_WRITE) ] = {
  421. [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
  422. [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */
  423. },
  424. [ C(OP_PREFETCH) ] = {
  425. [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
  426. [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
  427. },
  428. },
  429. [ C(L1I ) ] = {
  430. [ C(OP_READ) ] = {
  431. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  432. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  433. },
  434. [ C(OP_WRITE) ] = {
  435. [ C(RESULT_ACCESS) ] = -1,
  436. [ C(RESULT_MISS) ] = -1,
  437. },
  438. [ C(OP_PREFETCH) ] = {
  439. [ C(RESULT_ACCESS) ] = 0x0,
  440. [ C(RESULT_MISS) ] = 0x0,
  441. },
  442. },
  443. [ C(LL ) ] = {
  444. [ C(OP_READ) ] = {
  445. /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
  446. [ C(RESULT_ACCESS) ] = 0x01b7,
  447. /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
  448. [ C(RESULT_MISS) ] = 0x01b7,
  449. },
  450. /*
  451. * Use RFO, not WRITEBACK, because a write miss would typically occur
  452. * on RFO.
  453. */
  454. [ C(OP_WRITE) ] = {
  455. /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
  456. [ C(RESULT_ACCESS) ] = 0x01b7,
  457. /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
  458. [ C(RESULT_MISS) ] = 0x01b7,
  459. },
  460. [ C(OP_PREFETCH) ] = {
  461. /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
  462. [ C(RESULT_ACCESS) ] = 0x01b7,
  463. /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
  464. [ C(RESULT_MISS) ] = 0x01b7,
  465. },
  466. },
  467. [ C(DTLB) ] = {
  468. [ C(OP_READ) ] = {
  469. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
  470. [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
  471. },
  472. [ C(OP_WRITE) ] = {
  473. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
  474. [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
  475. },
  476. [ C(OP_PREFETCH) ] = {
  477. [ C(RESULT_ACCESS) ] = 0x0,
  478. [ C(RESULT_MISS) ] = 0x0,
  479. },
  480. },
  481. [ C(ITLB) ] = {
  482. [ C(OP_READ) ] = {
  483. [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
  484. [ C(RESULT_MISS) ] = 0x20c8, /* ITLB_MISS_RETIRED */
  485. },
  486. [ C(OP_WRITE) ] = {
  487. [ C(RESULT_ACCESS) ] = -1,
  488. [ C(RESULT_MISS) ] = -1,
  489. },
  490. [ C(OP_PREFETCH) ] = {
  491. [ C(RESULT_ACCESS) ] = -1,
  492. [ C(RESULT_MISS) ] = -1,
  493. },
  494. },
  495. [ C(BPU ) ] = {
  496. [ C(OP_READ) ] = {
  497. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
  498. [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
  499. },
  500. [ C(OP_WRITE) ] = {
  501. [ C(RESULT_ACCESS) ] = -1,
  502. [ C(RESULT_MISS) ] = -1,
  503. },
  504. [ C(OP_PREFETCH) ] = {
  505. [ C(RESULT_ACCESS) ] = -1,
  506. [ C(RESULT_MISS) ] = -1,
  507. },
  508. },
  509. [ C(NODE) ] = {
  510. [ C(OP_READ) ] = {
  511. [ C(RESULT_ACCESS) ] = 0x01b7,
  512. [ C(RESULT_MISS) ] = 0x01b7,
  513. },
  514. [ C(OP_WRITE) ] = {
  515. [ C(RESULT_ACCESS) ] = 0x01b7,
  516. [ C(RESULT_MISS) ] = 0x01b7,
  517. },
  518. [ C(OP_PREFETCH) ] = {
  519. [ C(RESULT_ACCESS) ] = 0x01b7,
  520. [ C(RESULT_MISS) ] = 0x01b7,
  521. },
  522. },
  523. };
  524. static __initconst const u64 core2_hw_cache_event_ids
  525. [PERF_COUNT_HW_CACHE_MAX]
  526. [PERF_COUNT_HW_CACHE_OP_MAX]
  527. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  528. {
  529. [ C(L1D) ] = {
  530. [ C(OP_READ) ] = {
  531. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
  532. [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
  533. },
  534. [ C(OP_WRITE) ] = {
  535. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
  536. [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
  537. },
  538. [ C(OP_PREFETCH) ] = {
  539. [ C(RESULT_ACCESS) ] = 0x104e, /* L1D_PREFETCH.REQUESTS */
  540. [ C(RESULT_MISS) ] = 0,
  541. },
  542. },
  543. [ C(L1I ) ] = {
  544. [ C(OP_READ) ] = {
  545. [ C(RESULT_ACCESS) ] = 0x0080, /* L1I.READS */
  546. [ C(RESULT_MISS) ] = 0x0081, /* L1I.MISSES */
  547. },
  548. [ C(OP_WRITE) ] = {
  549. [ C(RESULT_ACCESS) ] = -1,
  550. [ C(RESULT_MISS) ] = -1,
  551. },
  552. [ C(OP_PREFETCH) ] = {
  553. [ C(RESULT_ACCESS) ] = 0,
  554. [ C(RESULT_MISS) ] = 0,
  555. },
  556. },
  557. [ C(LL ) ] = {
  558. [ C(OP_READ) ] = {
  559. [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
  560. [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
  561. },
  562. [ C(OP_WRITE) ] = {
  563. [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
  564. [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
  565. },
  566. [ C(OP_PREFETCH) ] = {
  567. [ C(RESULT_ACCESS) ] = 0,
  568. [ C(RESULT_MISS) ] = 0,
  569. },
  570. },
  571. [ C(DTLB) ] = {
  572. [ C(OP_READ) ] = {
  573. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
  574. [ C(RESULT_MISS) ] = 0x0208, /* DTLB_MISSES.MISS_LD */
  575. },
  576. [ C(OP_WRITE) ] = {
  577. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
  578. [ C(RESULT_MISS) ] = 0x0808, /* DTLB_MISSES.MISS_ST */
  579. },
  580. [ C(OP_PREFETCH) ] = {
  581. [ C(RESULT_ACCESS) ] = 0,
  582. [ C(RESULT_MISS) ] = 0,
  583. },
  584. },
  585. [ C(ITLB) ] = {
  586. [ C(OP_READ) ] = {
  587. [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
  588. [ C(RESULT_MISS) ] = 0x1282, /* ITLBMISSES */
  589. },
  590. [ C(OP_WRITE) ] = {
  591. [ C(RESULT_ACCESS) ] = -1,
  592. [ C(RESULT_MISS) ] = -1,
  593. },
  594. [ C(OP_PREFETCH) ] = {
  595. [ C(RESULT_ACCESS) ] = -1,
  596. [ C(RESULT_MISS) ] = -1,
  597. },
  598. },
  599. [ C(BPU ) ] = {
  600. [ C(OP_READ) ] = {
  601. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
  602. [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
  603. },
  604. [ C(OP_WRITE) ] = {
  605. [ C(RESULT_ACCESS) ] = -1,
  606. [ C(RESULT_MISS) ] = -1,
  607. },
  608. [ C(OP_PREFETCH) ] = {
  609. [ C(RESULT_ACCESS) ] = -1,
  610. [ C(RESULT_MISS) ] = -1,
  611. },
  612. },
  613. };
  614. static __initconst const u64 atom_hw_cache_event_ids
  615. [PERF_COUNT_HW_CACHE_MAX]
  616. [PERF_COUNT_HW_CACHE_OP_MAX]
  617. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  618. {
  619. [ C(L1D) ] = {
  620. [ C(OP_READ) ] = {
  621. [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE.LD */
  622. [ C(RESULT_MISS) ] = 0,
  623. },
  624. [ C(OP_WRITE) ] = {
  625. [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE.ST */
  626. [ C(RESULT_MISS) ] = 0,
  627. },
  628. [ C(OP_PREFETCH) ] = {
  629. [ C(RESULT_ACCESS) ] = 0x0,
  630. [ C(RESULT_MISS) ] = 0,
  631. },
  632. },
  633. [ C(L1I ) ] = {
  634. [ C(OP_READ) ] = {
  635. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  636. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  637. },
  638. [ C(OP_WRITE) ] = {
  639. [ C(RESULT_ACCESS) ] = -1,
  640. [ C(RESULT_MISS) ] = -1,
  641. },
  642. [ C(OP_PREFETCH) ] = {
  643. [ C(RESULT_ACCESS) ] = 0,
  644. [ C(RESULT_MISS) ] = 0,
  645. },
  646. },
  647. [ C(LL ) ] = {
  648. [ C(OP_READ) ] = {
  649. [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
  650. [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
  651. },
  652. [ C(OP_WRITE) ] = {
  653. [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
  654. [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
  655. },
  656. [ C(OP_PREFETCH) ] = {
  657. [ C(RESULT_ACCESS) ] = 0,
  658. [ C(RESULT_MISS) ] = 0,
  659. },
  660. },
  661. [ C(DTLB) ] = {
  662. [ C(OP_READ) ] = {
  663. [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE_LD.MESI (alias) */
  664. [ C(RESULT_MISS) ] = 0x0508, /* DTLB_MISSES.MISS_LD */
  665. },
  666. [ C(OP_WRITE) ] = {
  667. [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE_ST.MESI (alias) */
  668. [ C(RESULT_MISS) ] = 0x0608, /* DTLB_MISSES.MISS_ST */
  669. },
  670. [ C(OP_PREFETCH) ] = {
  671. [ C(RESULT_ACCESS) ] = 0,
  672. [ C(RESULT_MISS) ] = 0,
  673. },
  674. },
  675. [ C(ITLB) ] = {
  676. [ C(OP_READ) ] = {
  677. [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
  678. [ C(RESULT_MISS) ] = 0x0282, /* ITLB.MISSES */
  679. },
  680. [ C(OP_WRITE) ] = {
  681. [ C(RESULT_ACCESS) ] = -1,
  682. [ C(RESULT_MISS) ] = -1,
  683. },
  684. [ C(OP_PREFETCH) ] = {
  685. [ C(RESULT_ACCESS) ] = -1,
  686. [ C(RESULT_MISS) ] = -1,
  687. },
  688. },
  689. [ C(BPU ) ] = {
  690. [ C(OP_READ) ] = {
  691. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
  692. [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
  693. },
  694. [ C(OP_WRITE) ] = {
  695. [ C(RESULT_ACCESS) ] = -1,
  696. [ C(RESULT_MISS) ] = -1,
  697. },
  698. [ C(OP_PREFETCH) ] = {
  699. [ C(RESULT_ACCESS) ] = -1,
  700. [ C(RESULT_MISS) ] = -1,
  701. },
  702. },
  703. };
  704. static inline bool intel_pmu_needs_lbr_smpl(struct perf_event *event)
  705. {
  706. /* user explicitly requested branch sampling */
  707. if (has_branch_stack(event))
  708. return true;
  709. /* implicit branch sampling to correct PEBS skid */
  710. if (x86_pmu.intel_cap.pebs_trap && event->attr.precise_ip > 1)
  711. return true;
  712. return false;
  713. }
  714. static void intel_pmu_disable_all(void)
  715. {
  716. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  717. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  718. if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask))
  719. intel_pmu_disable_bts();
  720. intel_pmu_pebs_disable_all();
  721. intel_pmu_lbr_disable_all();
  722. }
  723. static void intel_pmu_enable_all(int added)
  724. {
  725. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  726. intel_pmu_pebs_enable_all();
  727. intel_pmu_lbr_enable_all();
  728. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL,
  729. x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_guest_mask);
  730. if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask)) {
  731. struct perf_event *event =
  732. cpuc->events[X86_PMC_IDX_FIXED_BTS];
  733. if (WARN_ON_ONCE(!event))
  734. return;
  735. intel_pmu_enable_bts(event->hw.config);
  736. }
  737. }
  738. /*
  739. * Workaround for:
  740. * Intel Errata AAK100 (model 26)
  741. * Intel Errata AAP53 (model 30)
  742. * Intel Errata BD53 (model 44)
  743. *
  744. * The official story:
  745. * These chips need to be 'reset' when adding counters by programming the
  746. * magic three (non-counting) events 0x4300B5, 0x4300D2, and 0x4300B1 either
  747. * in sequence on the same PMC or on different PMCs.
  748. *
  749. * In practise it appears some of these events do in fact count, and
  750. * we need to programm all 4 events.
  751. */
  752. static void intel_pmu_nhm_workaround(void)
  753. {
  754. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  755. static const unsigned long nhm_magic[4] = {
  756. 0x4300B5,
  757. 0x4300D2,
  758. 0x4300B1,
  759. 0x4300B1
  760. };
  761. struct perf_event *event;
  762. int i;
  763. /*
  764. * The Errata requires below steps:
  765. * 1) Clear MSR_IA32_PEBS_ENABLE and MSR_CORE_PERF_GLOBAL_CTRL;
  766. * 2) Configure 4 PERFEVTSELx with the magic events and clear
  767. * the corresponding PMCx;
  768. * 3) set bit0~bit3 of MSR_CORE_PERF_GLOBAL_CTRL;
  769. * 4) Clear MSR_CORE_PERF_GLOBAL_CTRL;
  770. * 5) Clear 4 pairs of ERFEVTSELx and PMCx;
  771. */
  772. /*
  773. * The real steps we choose are a little different from above.
  774. * A) To reduce MSR operations, we don't run step 1) as they
  775. * are already cleared before this function is called;
  776. * B) Call x86_perf_event_update to save PMCx before configuring
  777. * PERFEVTSELx with magic number;
  778. * C) With step 5), we do clear only when the PERFEVTSELx is
  779. * not used currently.
  780. * D) Call x86_perf_event_set_period to restore PMCx;
  781. */
  782. /* We always operate 4 pairs of PERF Counters */
  783. for (i = 0; i < 4; i++) {
  784. event = cpuc->events[i];
  785. if (event)
  786. x86_perf_event_update(event);
  787. }
  788. for (i = 0; i < 4; i++) {
  789. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + i, nhm_magic[i]);
  790. wrmsrl(MSR_ARCH_PERFMON_PERFCTR0 + i, 0x0);
  791. }
  792. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0xf);
  793. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0x0);
  794. for (i = 0; i < 4; i++) {
  795. event = cpuc->events[i];
  796. if (event) {
  797. x86_perf_event_set_period(event);
  798. __x86_pmu_enable_event(&event->hw,
  799. ARCH_PERFMON_EVENTSEL_ENABLE);
  800. } else
  801. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + i, 0x0);
  802. }
  803. }
  804. static void intel_pmu_nhm_enable_all(int added)
  805. {
  806. if (added)
  807. intel_pmu_nhm_workaround();
  808. intel_pmu_enable_all(added);
  809. }
  810. static inline u64 intel_pmu_get_status(void)
  811. {
  812. u64 status;
  813. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  814. return status;
  815. }
  816. static inline void intel_pmu_ack_status(u64 ack)
  817. {
  818. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  819. }
  820. static void intel_pmu_disable_fixed(struct hw_perf_event *hwc)
  821. {
  822. int idx = hwc->idx - X86_PMC_IDX_FIXED;
  823. u64 ctrl_val, mask;
  824. mask = 0xfULL << (idx * 4);
  825. rdmsrl(hwc->config_base, ctrl_val);
  826. ctrl_val &= ~mask;
  827. wrmsrl(hwc->config_base, ctrl_val);
  828. }
  829. static void intel_pmu_disable_event(struct perf_event *event)
  830. {
  831. struct hw_perf_event *hwc = &event->hw;
  832. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  833. if (unlikely(hwc->idx == X86_PMC_IDX_FIXED_BTS)) {
  834. intel_pmu_disable_bts();
  835. intel_pmu_drain_bts_buffer();
  836. return;
  837. }
  838. cpuc->intel_ctrl_guest_mask &= ~(1ull << hwc->idx);
  839. cpuc->intel_ctrl_host_mask &= ~(1ull << hwc->idx);
  840. /*
  841. * must disable before any actual event
  842. * because any event may be combined with LBR
  843. */
  844. if (intel_pmu_needs_lbr_smpl(event))
  845. intel_pmu_lbr_disable(event);
  846. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  847. intel_pmu_disable_fixed(hwc);
  848. return;
  849. }
  850. x86_pmu_disable_event(event);
  851. if (unlikely(event->attr.precise_ip))
  852. intel_pmu_pebs_disable(event);
  853. }
  854. static void intel_pmu_enable_fixed(struct hw_perf_event *hwc)
  855. {
  856. int idx = hwc->idx - X86_PMC_IDX_FIXED;
  857. u64 ctrl_val, bits, mask;
  858. /*
  859. * Enable IRQ generation (0x8),
  860. * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
  861. * if requested:
  862. */
  863. bits = 0x8ULL;
  864. if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
  865. bits |= 0x2;
  866. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  867. bits |= 0x1;
  868. /*
  869. * ANY bit is supported in v3 and up
  870. */
  871. if (x86_pmu.version > 2 && hwc->config & ARCH_PERFMON_EVENTSEL_ANY)
  872. bits |= 0x4;
  873. bits <<= (idx * 4);
  874. mask = 0xfULL << (idx * 4);
  875. rdmsrl(hwc->config_base, ctrl_val);
  876. ctrl_val &= ~mask;
  877. ctrl_val |= bits;
  878. wrmsrl(hwc->config_base, ctrl_val);
  879. }
  880. static void intel_pmu_enable_event(struct perf_event *event)
  881. {
  882. struct hw_perf_event *hwc = &event->hw;
  883. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  884. if (unlikely(hwc->idx == X86_PMC_IDX_FIXED_BTS)) {
  885. if (!__this_cpu_read(cpu_hw_events.enabled))
  886. return;
  887. intel_pmu_enable_bts(hwc->config);
  888. return;
  889. }
  890. /*
  891. * must enabled before any actual event
  892. * because any event may be combined with LBR
  893. */
  894. if (intel_pmu_needs_lbr_smpl(event))
  895. intel_pmu_lbr_enable(event);
  896. if (event->attr.exclude_host)
  897. cpuc->intel_ctrl_guest_mask |= (1ull << hwc->idx);
  898. if (event->attr.exclude_guest)
  899. cpuc->intel_ctrl_host_mask |= (1ull << hwc->idx);
  900. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  901. intel_pmu_enable_fixed(hwc);
  902. return;
  903. }
  904. if (unlikely(event->attr.precise_ip))
  905. intel_pmu_pebs_enable(event);
  906. __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
  907. }
  908. /*
  909. * Save and restart an expired event. Called by NMI contexts,
  910. * so it has to be careful about preempting normal event ops:
  911. */
  912. int intel_pmu_save_and_restart(struct perf_event *event)
  913. {
  914. x86_perf_event_update(event);
  915. return x86_perf_event_set_period(event);
  916. }
  917. static void intel_pmu_reset(void)
  918. {
  919. struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
  920. unsigned long flags;
  921. int idx;
  922. if (!x86_pmu.num_counters)
  923. return;
  924. local_irq_save(flags);
  925. pr_info("clearing PMU state on CPU#%d\n", smp_processor_id());
  926. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  927. checking_wrmsrl(x86_pmu_config_addr(idx), 0ull);
  928. checking_wrmsrl(x86_pmu_event_addr(idx), 0ull);
  929. }
  930. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++)
  931. checking_wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull);
  932. if (ds)
  933. ds->bts_index = ds->bts_buffer_base;
  934. local_irq_restore(flags);
  935. }
  936. /*
  937. * This handler is triggered by the local APIC, so the APIC IRQ handling
  938. * rules apply:
  939. */
  940. static int intel_pmu_handle_irq(struct pt_regs *regs)
  941. {
  942. struct perf_sample_data data;
  943. struct cpu_hw_events *cpuc;
  944. int bit, loops;
  945. u64 status;
  946. int handled;
  947. cpuc = &__get_cpu_var(cpu_hw_events);
  948. /*
  949. * Some chipsets need to unmask the LVTPC in a particular spot
  950. * inside the nmi handler. As a result, the unmasking was pushed
  951. * into all the nmi handlers.
  952. *
  953. * This handler doesn't seem to have any issues with the unmasking
  954. * so it was left at the top.
  955. */
  956. apic_write(APIC_LVTPC, APIC_DM_NMI);
  957. intel_pmu_disable_all();
  958. handled = intel_pmu_drain_bts_buffer();
  959. status = intel_pmu_get_status();
  960. if (!status) {
  961. intel_pmu_enable_all(0);
  962. return handled;
  963. }
  964. loops = 0;
  965. again:
  966. intel_pmu_ack_status(status);
  967. if (++loops > 100) {
  968. WARN_ONCE(1, "perfevents: irq loop stuck!\n");
  969. perf_event_print_debug();
  970. intel_pmu_reset();
  971. goto done;
  972. }
  973. inc_irq_stat(apic_perf_irqs);
  974. intel_pmu_lbr_read();
  975. /*
  976. * PEBS overflow sets bit 62 in the global status register
  977. */
  978. if (__test_and_clear_bit(62, (unsigned long *)&status)) {
  979. handled++;
  980. x86_pmu.drain_pebs(regs);
  981. }
  982. for_each_set_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  983. struct perf_event *event = cpuc->events[bit];
  984. handled++;
  985. if (!test_bit(bit, cpuc->active_mask))
  986. continue;
  987. if (!intel_pmu_save_and_restart(event))
  988. continue;
  989. perf_sample_data_init(&data, 0, event->hw.last_period);
  990. if (has_branch_stack(event))
  991. data.br_stack = &cpuc->lbr_stack;
  992. if (perf_event_overflow(event, &data, regs))
  993. x86_pmu_stop(event, 0);
  994. }
  995. /*
  996. * Repeat if there is more work to be done:
  997. */
  998. status = intel_pmu_get_status();
  999. if (status)
  1000. goto again;
  1001. done:
  1002. intel_pmu_enable_all(0);
  1003. return handled;
  1004. }
  1005. static struct event_constraint *
  1006. intel_bts_constraints(struct perf_event *event)
  1007. {
  1008. struct hw_perf_event *hwc = &event->hw;
  1009. unsigned int hw_event, bts_event;
  1010. if (event->attr.freq)
  1011. return NULL;
  1012. hw_event = hwc->config & INTEL_ARCH_EVENT_MASK;
  1013. bts_event = x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS);
  1014. if (unlikely(hw_event == bts_event && hwc->sample_period == 1))
  1015. return &bts_constraint;
  1016. return NULL;
  1017. }
  1018. static int intel_alt_er(int idx)
  1019. {
  1020. if (!(x86_pmu.er_flags & ERF_HAS_RSP_1))
  1021. return idx;
  1022. if (idx == EXTRA_REG_RSP_0)
  1023. return EXTRA_REG_RSP_1;
  1024. if (idx == EXTRA_REG_RSP_1)
  1025. return EXTRA_REG_RSP_0;
  1026. return idx;
  1027. }
  1028. static void intel_fixup_er(struct perf_event *event, int idx)
  1029. {
  1030. event->hw.extra_reg.idx = idx;
  1031. if (idx == EXTRA_REG_RSP_0) {
  1032. event->hw.config &= ~INTEL_ARCH_EVENT_MASK;
  1033. event->hw.config |= 0x01b7;
  1034. event->hw.extra_reg.reg = MSR_OFFCORE_RSP_0;
  1035. } else if (idx == EXTRA_REG_RSP_1) {
  1036. event->hw.config &= ~INTEL_ARCH_EVENT_MASK;
  1037. event->hw.config |= 0x01bb;
  1038. event->hw.extra_reg.reg = MSR_OFFCORE_RSP_1;
  1039. }
  1040. }
  1041. /*
  1042. * manage allocation of shared extra msr for certain events
  1043. *
  1044. * sharing can be:
  1045. * per-cpu: to be shared between the various events on a single PMU
  1046. * per-core: per-cpu + shared by HT threads
  1047. */
  1048. static struct event_constraint *
  1049. __intel_shared_reg_get_constraints(struct cpu_hw_events *cpuc,
  1050. struct perf_event *event,
  1051. struct hw_perf_event_extra *reg)
  1052. {
  1053. struct event_constraint *c = &emptyconstraint;
  1054. struct er_account *era;
  1055. unsigned long flags;
  1056. int idx = reg->idx;
  1057. /*
  1058. * reg->alloc can be set due to existing state, so for fake cpuc we
  1059. * need to ignore this, otherwise we might fail to allocate proper fake
  1060. * state for this extra reg constraint. Also see the comment below.
  1061. */
  1062. if (reg->alloc && !cpuc->is_fake)
  1063. return NULL; /* call x86_get_event_constraint() */
  1064. again:
  1065. era = &cpuc->shared_regs->regs[idx];
  1066. /*
  1067. * we use spin_lock_irqsave() to avoid lockdep issues when
  1068. * passing a fake cpuc
  1069. */
  1070. raw_spin_lock_irqsave(&era->lock, flags);
  1071. if (!atomic_read(&era->ref) || era->config == reg->config) {
  1072. /*
  1073. * If its a fake cpuc -- as per validate_{group,event}() we
  1074. * shouldn't touch event state and we can avoid doing so
  1075. * since both will only call get_event_constraints() once
  1076. * on each event, this avoids the need for reg->alloc.
  1077. *
  1078. * Not doing the ER fixup will only result in era->reg being
  1079. * wrong, but since we won't actually try and program hardware
  1080. * this isn't a problem either.
  1081. */
  1082. if (!cpuc->is_fake) {
  1083. if (idx != reg->idx)
  1084. intel_fixup_er(event, idx);
  1085. /*
  1086. * x86_schedule_events() can call get_event_constraints()
  1087. * multiple times on events in the case of incremental
  1088. * scheduling(). reg->alloc ensures we only do the ER
  1089. * allocation once.
  1090. */
  1091. reg->alloc = 1;
  1092. }
  1093. /* lock in msr value */
  1094. era->config = reg->config;
  1095. era->reg = reg->reg;
  1096. /* one more user */
  1097. atomic_inc(&era->ref);
  1098. /*
  1099. * need to call x86_get_event_constraint()
  1100. * to check if associated event has constraints
  1101. */
  1102. c = NULL;
  1103. } else {
  1104. idx = intel_alt_er(idx);
  1105. if (idx != reg->idx) {
  1106. raw_spin_unlock_irqrestore(&era->lock, flags);
  1107. goto again;
  1108. }
  1109. }
  1110. raw_spin_unlock_irqrestore(&era->lock, flags);
  1111. return c;
  1112. }
  1113. static void
  1114. __intel_shared_reg_put_constraints(struct cpu_hw_events *cpuc,
  1115. struct hw_perf_event_extra *reg)
  1116. {
  1117. struct er_account *era;
  1118. /*
  1119. * Only put constraint if extra reg was actually allocated. Also takes
  1120. * care of event which do not use an extra shared reg.
  1121. *
  1122. * Also, if this is a fake cpuc we shouldn't touch any event state
  1123. * (reg->alloc) and we don't care about leaving inconsistent cpuc state
  1124. * either since it'll be thrown out.
  1125. */
  1126. if (!reg->alloc || cpuc->is_fake)
  1127. return;
  1128. era = &cpuc->shared_regs->regs[reg->idx];
  1129. /* one fewer user */
  1130. atomic_dec(&era->ref);
  1131. /* allocate again next time */
  1132. reg->alloc = 0;
  1133. }
  1134. static struct event_constraint *
  1135. intel_shared_regs_constraints(struct cpu_hw_events *cpuc,
  1136. struct perf_event *event)
  1137. {
  1138. struct event_constraint *c = NULL, *d;
  1139. struct hw_perf_event_extra *xreg, *breg;
  1140. xreg = &event->hw.extra_reg;
  1141. if (xreg->idx != EXTRA_REG_NONE) {
  1142. c = __intel_shared_reg_get_constraints(cpuc, event, xreg);
  1143. if (c == &emptyconstraint)
  1144. return c;
  1145. }
  1146. breg = &event->hw.branch_reg;
  1147. if (breg->idx != EXTRA_REG_NONE) {
  1148. d = __intel_shared_reg_get_constraints(cpuc, event, breg);
  1149. if (d == &emptyconstraint) {
  1150. __intel_shared_reg_put_constraints(cpuc, xreg);
  1151. c = d;
  1152. }
  1153. }
  1154. return c;
  1155. }
  1156. struct event_constraint *
  1157. x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  1158. {
  1159. struct event_constraint *c;
  1160. if (x86_pmu.event_constraints) {
  1161. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1162. if ((event->hw.config & c->cmask) == c->code)
  1163. return c;
  1164. }
  1165. }
  1166. return &unconstrained;
  1167. }
  1168. static struct event_constraint *
  1169. intel_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
  1170. {
  1171. struct event_constraint *c;
  1172. c = intel_bts_constraints(event);
  1173. if (c)
  1174. return c;
  1175. c = intel_pebs_constraints(event);
  1176. if (c)
  1177. return c;
  1178. c = intel_shared_regs_constraints(cpuc, event);
  1179. if (c)
  1180. return c;
  1181. return x86_get_event_constraints(cpuc, event);
  1182. }
  1183. static void
  1184. intel_put_shared_regs_event_constraints(struct cpu_hw_events *cpuc,
  1185. struct perf_event *event)
  1186. {
  1187. struct hw_perf_event_extra *reg;
  1188. reg = &event->hw.extra_reg;
  1189. if (reg->idx != EXTRA_REG_NONE)
  1190. __intel_shared_reg_put_constraints(cpuc, reg);
  1191. reg = &event->hw.branch_reg;
  1192. if (reg->idx != EXTRA_REG_NONE)
  1193. __intel_shared_reg_put_constraints(cpuc, reg);
  1194. }
  1195. static void intel_put_event_constraints(struct cpu_hw_events *cpuc,
  1196. struct perf_event *event)
  1197. {
  1198. intel_put_shared_regs_event_constraints(cpuc, event);
  1199. }
  1200. static void intel_pebs_aliases_core2(struct perf_event *event)
  1201. {
  1202. if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
  1203. /*
  1204. * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
  1205. * (0x003c) so that we can use it with PEBS.
  1206. *
  1207. * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
  1208. * PEBS capable. However we can use INST_RETIRED.ANY_P
  1209. * (0x00c0), which is a PEBS capable event, to get the same
  1210. * count.
  1211. *
  1212. * INST_RETIRED.ANY_P counts the number of cycles that retires
  1213. * CNTMASK instructions. By setting CNTMASK to a value (16)
  1214. * larger than the maximum number of instructions that can be
  1215. * retired per cycle (4) and then inverting the condition, we
  1216. * count all cycles that retire 16 or less instructions, which
  1217. * is every cycle.
  1218. *
  1219. * Thereby we gain a PEBS capable cycle counter.
  1220. */
  1221. u64 alt_config = X86_CONFIG(.event=0xc0, .inv=1, .cmask=16);
  1222. alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
  1223. event->hw.config = alt_config;
  1224. }
  1225. }
  1226. static void intel_pebs_aliases_snb(struct perf_event *event)
  1227. {
  1228. if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
  1229. /*
  1230. * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
  1231. * (0x003c) so that we can use it with PEBS.
  1232. *
  1233. * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
  1234. * PEBS capable. However we can use UOPS_RETIRED.ALL
  1235. * (0x01c2), which is a PEBS capable event, to get the same
  1236. * count.
  1237. *
  1238. * UOPS_RETIRED.ALL counts the number of cycles that retires
  1239. * CNTMASK micro-ops. By setting CNTMASK to a value (16)
  1240. * larger than the maximum number of micro-ops that can be
  1241. * retired per cycle (4) and then inverting the condition, we
  1242. * count all cycles that retire 16 or less micro-ops, which
  1243. * is every cycle.
  1244. *
  1245. * Thereby we gain a PEBS capable cycle counter.
  1246. */
  1247. u64 alt_config = X86_CONFIG(.event=0xc2, .umask=0x01, .inv=1, .cmask=16);
  1248. alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
  1249. event->hw.config = alt_config;
  1250. }
  1251. }
  1252. static int intel_pmu_hw_config(struct perf_event *event)
  1253. {
  1254. int ret = x86_pmu_hw_config(event);
  1255. if (ret)
  1256. return ret;
  1257. if (event->attr.precise_ip && x86_pmu.pebs_aliases)
  1258. x86_pmu.pebs_aliases(event);
  1259. if (intel_pmu_needs_lbr_smpl(event)) {
  1260. ret = intel_pmu_setup_lbr_filter(event);
  1261. if (ret)
  1262. return ret;
  1263. }
  1264. if (event->attr.type != PERF_TYPE_RAW)
  1265. return 0;
  1266. if (!(event->attr.config & ARCH_PERFMON_EVENTSEL_ANY))
  1267. return 0;
  1268. if (x86_pmu.version < 3)
  1269. return -EINVAL;
  1270. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  1271. return -EACCES;
  1272. event->hw.config |= ARCH_PERFMON_EVENTSEL_ANY;
  1273. return 0;
  1274. }
  1275. struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr)
  1276. {
  1277. if (x86_pmu.guest_get_msrs)
  1278. return x86_pmu.guest_get_msrs(nr);
  1279. *nr = 0;
  1280. return NULL;
  1281. }
  1282. EXPORT_SYMBOL_GPL(perf_guest_get_msrs);
  1283. static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr)
  1284. {
  1285. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1286. struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;
  1287. arr[0].msr = MSR_CORE_PERF_GLOBAL_CTRL;
  1288. arr[0].host = x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_guest_mask;
  1289. arr[0].guest = x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_host_mask;
  1290. *nr = 1;
  1291. return arr;
  1292. }
  1293. static struct perf_guest_switch_msr *core_guest_get_msrs(int *nr)
  1294. {
  1295. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1296. struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;
  1297. int idx;
  1298. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1299. struct perf_event *event = cpuc->events[idx];
  1300. arr[idx].msr = x86_pmu_config_addr(idx);
  1301. arr[idx].host = arr[idx].guest = 0;
  1302. if (!test_bit(idx, cpuc->active_mask))
  1303. continue;
  1304. arr[idx].host = arr[idx].guest =
  1305. event->hw.config | ARCH_PERFMON_EVENTSEL_ENABLE;
  1306. if (event->attr.exclude_host)
  1307. arr[idx].host &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  1308. else if (event->attr.exclude_guest)
  1309. arr[idx].guest &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  1310. }
  1311. *nr = x86_pmu.num_counters;
  1312. return arr;
  1313. }
  1314. static void core_pmu_enable_event(struct perf_event *event)
  1315. {
  1316. if (!event->attr.exclude_host)
  1317. x86_pmu_enable_event(event);
  1318. }
  1319. static void core_pmu_enable_all(int added)
  1320. {
  1321. struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
  1322. int idx;
  1323. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1324. struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
  1325. if (!test_bit(idx, cpuc->active_mask) ||
  1326. cpuc->events[idx]->attr.exclude_host)
  1327. continue;
  1328. __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
  1329. }
  1330. }
  1331. PMU_FORMAT_ATTR(event, "config:0-7" );
  1332. PMU_FORMAT_ATTR(umask, "config:8-15" );
  1333. PMU_FORMAT_ATTR(edge, "config:18" );
  1334. PMU_FORMAT_ATTR(pc, "config:19" );
  1335. PMU_FORMAT_ATTR(any, "config:21" ); /* v3 + */
  1336. PMU_FORMAT_ATTR(inv, "config:23" );
  1337. PMU_FORMAT_ATTR(cmask, "config:24-31" );
  1338. static struct attribute *intel_arch_formats_attr[] = {
  1339. &format_attr_event.attr,
  1340. &format_attr_umask.attr,
  1341. &format_attr_edge.attr,
  1342. &format_attr_pc.attr,
  1343. &format_attr_inv.attr,
  1344. &format_attr_cmask.attr,
  1345. NULL,
  1346. };
  1347. static __initconst const struct x86_pmu core_pmu = {
  1348. .name = "core",
  1349. .handle_irq = x86_pmu_handle_irq,
  1350. .disable_all = x86_pmu_disable_all,
  1351. .enable_all = core_pmu_enable_all,
  1352. .enable = core_pmu_enable_event,
  1353. .disable = x86_pmu_disable_event,
  1354. .hw_config = x86_pmu_hw_config,
  1355. .schedule_events = x86_schedule_events,
  1356. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  1357. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  1358. .event_map = intel_pmu_event_map,
  1359. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  1360. .apic = 1,
  1361. /*
  1362. * Intel PMCs cannot be accessed sanely above 32 bit width,
  1363. * so we install an artificial 1<<31 period regardless of
  1364. * the generic event period:
  1365. */
  1366. .max_period = (1ULL << 31) - 1,
  1367. .get_event_constraints = intel_get_event_constraints,
  1368. .put_event_constraints = intel_put_event_constraints,
  1369. .event_constraints = intel_core_event_constraints,
  1370. .guest_get_msrs = core_guest_get_msrs,
  1371. .format_attrs = intel_arch_formats_attr,
  1372. };
  1373. struct intel_shared_regs *allocate_shared_regs(int cpu)
  1374. {
  1375. struct intel_shared_regs *regs;
  1376. int i;
  1377. regs = kzalloc_node(sizeof(struct intel_shared_regs),
  1378. GFP_KERNEL, cpu_to_node(cpu));
  1379. if (regs) {
  1380. /*
  1381. * initialize the locks to keep lockdep happy
  1382. */
  1383. for (i = 0; i < EXTRA_REG_MAX; i++)
  1384. raw_spin_lock_init(&regs->regs[i].lock);
  1385. regs->core_id = -1;
  1386. }
  1387. return regs;
  1388. }
  1389. static int intel_pmu_cpu_prepare(int cpu)
  1390. {
  1391. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  1392. if (!(x86_pmu.extra_regs || x86_pmu.lbr_sel_map))
  1393. return NOTIFY_OK;
  1394. cpuc->shared_regs = allocate_shared_regs(cpu);
  1395. if (!cpuc->shared_regs)
  1396. return NOTIFY_BAD;
  1397. return NOTIFY_OK;
  1398. }
  1399. static void intel_pmu_cpu_starting(int cpu)
  1400. {
  1401. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  1402. int core_id = topology_core_id(cpu);
  1403. int i;
  1404. init_debug_store_on_cpu(cpu);
  1405. /*
  1406. * Deal with CPUs that don't clear their LBRs on power-up.
  1407. */
  1408. intel_pmu_lbr_reset();
  1409. cpuc->lbr_sel = NULL;
  1410. if (!cpuc->shared_regs)
  1411. return;
  1412. if (!(x86_pmu.er_flags & ERF_NO_HT_SHARING)) {
  1413. for_each_cpu(i, topology_thread_cpumask(cpu)) {
  1414. struct intel_shared_regs *pc;
  1415. pc = per_cpu(cpu_hw_events, i).shared_regs;
  1416. if (pc && pc->core_id == core_id) {
  1417. cpuc->kfree_on_online = cpuc->shared_regs;
  1418. cpuc->shared_regs = pc;
  1419. break;
  1420. }
  1421. }
  1422. cpuc->shared_regs->core_id = core_id;
  1423. cpuc->shared_regs->refcnt++;
  1424. }
  1425. if (x86_pmu.lbr_sel_map)
  1426. cpuc->lbr_sel = &cpuc->shared_regs->regs[EXTRA_REG_LBR];
  1427. }
  1428. static void intel_pmu_cpu_dying(int cpu)
  1429. {
  1430. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  1431. struct intel_shared_regs *pc;
  1432. pc = cpuc->shared_regs;
  1433. if (pc) {
  1434. if (pc->core_id == -1 || --pc->refcnt == 0)
  1435. kfree(pc);
  1436. cpuc->shared_regs = NULL;
  1437. }
  1438. fini_debug_store_on_cpu(cpu);
  1439. }
  1440. static void intel_pmu_flush_branch_stack(void)
  1441. {
  1442. /*
  1443. * Intel LBR does not tag entries with the
  1444. * PID of the current task, then we need to
  1445. * flush it on ctxsw
  1446. * For now, we simply reset it
  1447. */
  1448. if (x86_pmu.lbr_nr)
  1449. intel_pmu_lbr_reset();
  1450. }
  1451. PMU_FORMAT_ATTR(offcore_rsp, "config1:0-63");
  1452. static struct attribute *intel_arch3_formats_attr[] = {
  1453. &format_attr_event.attr,
  1454. &format_attr_umask.attr,
  1455. &format_attr_edge.attr,
  1456. &format_attr_pc.attr,
  1457. &format_attr_any.attr,
  1458. &format_attr_inv.attr,
  1459. &format_attr_cmask.attr,
  1460. &format_attr_offcore_rsp.attr, /* XXX do NHM/WSM + SNB breakout */
  1461. NULL,
  1462. };
  1463. static __initconst const struct x86_pmu intel_pmu = {
  1464. .name = "Intel",
  1465. .handle_irq = intel_pmu_handle_irq,
  1466. .disable_all = intel_pmu_disable_all,
  1467. .enable_all = intel_pmu_enable_all,
  1468. .enable = intel_pmu_enable_event,
  1469. .disable = intel_pmu_disable_event,
  1470. .hw_config = intel_pmu_hw_config,
  1471. .schedule_events = x86_schedule_events,
  1472. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  1473. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  1474. .event_map = intel_pmu_event_map,
  1475. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  1476. .apic = 1,
  1477. /*
  1478. * Intel PMCs cannot be accessed sanely above 32 bit width,
  1479. * so we install an artificial 1<<31 period regardless of
  1480. * the generic event period:
  1481. */
  1482. .max_period = (1ULL << 31) - 1,
  1483. .get_event_constraints = intel_get_event_constraints,
  1484. .put_event_constraints = intel_put_event_constraints,
  1485. .pebs_aliases = intel_pebs_aliases_core2,
  1486. .format_attrs = intel_arch3_formats_attr,
  1487. .cpu_prepare = intel_pmu_cpu_prepare,
  1488. .cpu_starting = intel_pmu_cpu_starting,
  1489. .cpu_dying = intel_pmu_cpu_dying,
  1490. .guest_get_msrs = intel_guest_get_msrs,
  1491. .flush_branch_stack = intel_pmu_flush_branch_stack,
  1492. };
  1493. static __init void intel_clovertown_quirk(void)
  1494. {
  1495. /*
  1496. * PEBS is unreliable due to:
  1497. *
  1498. * AJ67 - PEBS may experience CPL leaks
  1499. * AJ68 - PEBS PMI may be delayed by one event
  1500. * AJ69 - GLOBAL_STATUS[62] will only be set when DEBUGCTL[12]
  1501. * AJ106 - FREEZE_LBRS_ON_PMI doesn't work in combination with PEBS
  1502. *
  1503. * AJ67 could be worked around by restricting the OS/USR flags.
  1504. * AJ69 could be worked around by setting PMU_FREEZE_ON_PMI.
  1505. *
  1506. * AJ106 could possibly be worked around by not allowing LBR
  1507. * usage from PEBS, including the fixup.
  1508. * AJ68 could possibly be worked around by always programming
  1509. * a pebs_event_reset[0] value and coping with the lost events.
  1510. *
  1511. * But taken together it might just make sense to not enable PEBS on
  1512. * these chips.
  1513. */
  1514. pr_warn("PEBS disabled due to CPU errata\n");
  1515. x86_pmu.pebs = 0;
  1516. x86_pmu.pebs_constraints = NULL;
  1517. }
  1518. static __init void intel_sandybridge_quirk(void)
  1519. {
  1520. pr_warn("PEBS disabled due to CPU errata\n");
  1521. x86_pmu.pebs = 0;
  1522. x86_pmu.pebs_constraints = NULL;
  1523. }
  1524. static const struct { int id; char *name; } intel_arch_events_map[] __initconst = {
  1525. { PERF_COUNT_HW_CPU_CYCLES, "cpu cycles" },
  1526. { PERF_COUNT_HW_INSTRUCTIONS, "instructions" },
  1527. { PERF_COUNT_HW_BUS_CYCLES, "bus cycles" },
  1528. { PERF_COUNT_HW_CACHE_REFERENCES, "cache references" },
  1529. { PERF_COUNT_HW_CACHE_MISSES, "cache misses" },
  1530. { PERF_COUNT_HW_BRANCH_INSTRUCTIONS, "branch instructions" },
  1531. { PERF_COUNT_HW_BRANCH_MISSES, "branch misses" },
  1532. };
  1533. static __init void intel_arch_events_quirk(void)
  1534. {
  1535. int bit;
  1536. /* disable event that reported as not presend by cpuid */
  1537. for_each_set_bit(bit, x86_pmu.events_mask, ARRAY_SIZE(intel_arch_events_map)) {
  1538. intel_perfmon_event_map[intel_arch_events_map[bit].id] = 0;
  1539. pr_warn("CPUID marked event: \'%s\' unavailable\n",
  1540. intel_arch_events_map[bit].name);
  1541. }
  1542. }
  1543. static __init void intel_nehalem_quirk(void)
  1544. {
  1545. union cpuid10_ebx ebx;
  1546. ebx.full = x86_pmu.events_maskl;
  1547. if (ebx.split.no_branch_misses_retired) {
  1548. /*
  1549. * Erratum AAJ80 detected, we work it around by using
  1550. * the BR_MISP_EXEC.ANY event. This will over-count
  1551. * branch-misses, but it's still much better than the
  1552. * architectural event which is often completely bogus:
  1553. */
  1554. intel_perfmon_event_map[PERF_COUNT_HW_BRANCH_MISSES] = 0x7f89;
  1555. ebx.split.no_branch_misses_retired = 0;
  1556. x86_pmu.events_maskl = ebx.full;
  1557. pr_info("CPU erratum AAJ80 worked around\n");
  1558. }
  1559. }
  1560. __init int intel_pmu_init(void)
  1561. {
  1562. union cpuid10_edx edx;
  1563. union cpuid10_eax eax;
  1564. union cpuid10_ebx ebx;
  1565. unsigned int unused;
  1566. int version;
  1567. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
  1568. switch (boot_cpu_data.x86) {
  1569. case 0x6:
  1570. return p6_pmu_init();
  1571. case 0xf:
  1572. return p4_pmu_init();
  1573. }
  1574. return -ENODEV;
  1575. }
  1576. /*
  1577. * Check whether the Architectural PerfMon supports
  1578. * Branch Misses Retired hw_event or not.
  1579. */
  1580. cpuid(10, &eax.full, &ebx.full, &unused, &edx.full);
  1581. if (eax.split.mask_length < ARCH_PERFMON_EVENTS_COUNT)
  1582. return -ENODEV;
  1583. version = eax.split.version_id;
  1584. if (version < 2)
  1585. x86_pmu = core_pmu;
  1586. else
  1587. x86_pmu = intel_pmu;
  1588. x86_pmu.version = version;
  1589. x86_pmu.num_counters = eax.split.num_counters;
  1590. x86_pmu.cntval_bits = eax.split.bit_width;
  1591. x86_pmu.cntval_mask = (1ULL << eax.split.bit_width) - 1;
  1592. x86_pmu.events_maskl = ebx.full;
  1593. x86_pmu.events_mask_len = eax.split.mask_length;
  1594. /*
  1595. * Quirk: v2 perfmon does not report fixed-purpose events, so
  1596. * assume at least 3 events:
  1597. */
  1598. if (version > 1)
  1599. x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3);
  1600. /*
  1601. * v2 and above have a perf capabilities MSR
  1602. */
  1603. if (version > 1) {
  1604. u64 capabilities;
  1605. rdmsrl(MSR_IA32_PERF_CAPABILITIES, capabilities);
  1606. x86_pmu.intel_cap.capabilities = capabilities;
  1607. }
  1608. intel_ds_init();
  1609. x86_add_quirk(intel_arch_events_quirk); /* Install first, so it runs last */
  1610. /*
  1611. * Install the hw-cache-events table:
  1612. */
  1613. switch (boot_cpu_data.x86_model) {
  1614. case 14: /* 65 nm core solo/duo, "Yonah" */
  1615. pr_cont("Core events, ");
  1616. break;
  1617. case 15: /* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */
  1618. x86_add_quirk(intel_clovertown_quirk);
  1619. case 22: /* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */
  1620. case 23: /* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */
  1621. case 29: /* six-core 45 nm xeon "Dunnington" */
  1622. memcpy(hw_cache_event_ids, core2_hw_cache_event_ids,
  1623. sizeof(hw_cache_event_ids));
  1624. intel_pmu_lbr_init_core();
  1625. x86_pmu.event_constraints = intel_core2_event_constraints;
  1626. x86_pmu.pebs_constraints = intel_core2_pebs_event_constraints;
  1627. pr_cont("Core2 events, ");
  1628. break;
  1629. case 26: /* 45 nm nehalem, "Bloomfield" */
  1630. case 30: /* 45 nm nehalem, "Lynnfield" */
  1631. case 46: /* 45 nm nehalem-ex, "Beckton" */
  1632. memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids,
  1633. sizeof(hw_cache_event_ids));
  1634. memcpy(hw_cache_extra_regs, nehalem_hw_cache_extra_regs,
  1635. sizeof(hw_cache_extra_regs));
  1636. intel_pmu_lbr_init_nhm();
  1637. x86_pmu.event_constraints = intel_nehalem_event_constraints;
  1638. x86_pmu.pebs_constraints = intel_nehalem_pebs_event_constraints;
  1639. x86_pmu.enable_all = intel_pmu_nhm_enable_all;
  1640. x86_pmu.extra_regs = intel_nehalem_extra_regs;
  1641. /* UOPS_ISSUED.STALLED_CYCLES */
  1642. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
  1643. X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
  1644. /* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */
  1645. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
  1646. X86_CONFIG(.event=0xb1, .umask=0x3f, .inv=1, .cmask=1);
  1647. x86_add_quirk(intel_nehalem_quirk);
  1648. pr_cont("Nehalem events, ");
  1649. break;
  1650. case 28: /* Atom */
  1651. memcpy(hw_cache_event_ids, atom_hw_cache_event_ids,
  1652. sizeof(hw_cache_event_ids));
  1653. intel_pmu_lbr_init_atom();
  1654. x86_pmu.event_constraints = intel_gen_event_constraints;
  1655. x86_pmu.pebs_constraints = intel_atom_pebs_event_constraints;
  1656. pr_cont("Atom events, ");
  1657. break;
  1658. case 37: /* 32 nm nehalem, "Clarkdale" */
  1659. case 44: /* 32 nm nehalem, "Gulftown" */
  1660. case 47: /* 32 nm Xeon E7 */
  1661. memcpy(hw_cache_event_ids, westmere_hw_cache_event_ids,
  1662. sizeof(hw_cache_event_ids));
  1663. memcpy(hw_cache_extra_regs, nehalem_hw_cache_extra_regs,
  1664. sizeof(hw_cache_extra_regs));
  1665. intel_pmu_lbr_init_nhm();
  1666. x86_pmu.event_constraints = intel_westmere_event_constraints;
  1667. x86_pmu.enable_all = intel_pmu_nhm_enable_all;
  1668. x86_pmu.pebs_constraints = intel_westmere_pebs_event_constraints;
  1669. x86_pmu.extra_regs = intel_westmere_extra_regs;
  1670. x86_pmu.er_flags |= ERF_HAS_RSP_1;
  1671. /* UOPS_ISSUED.STALLED_CYCLES */
  1672. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
  1673. X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
  1674. /* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */
  1675. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
  1676. X86_CONFIG(.event=0xb1, .umask=0x3f, .inv=1, .cmask=1);
  1677. pr_cont("Westmere events, ");
  1678. break;
  1679. case 42: /* SandyBridge */
  1680. case 45: /* SandyBridge, "Romely-EP" */
  1681. x86_add_quirk(intel_sandybridge_quirk);
  1682. case 58: /* IvyBridge */
  1683. memcpy(hw_cache_event_ids, snb_hw_cache_event_ids,
  1684. sizeof(hw_cache_event_ids));
  1685. intel_pmu_lbr_init_snb();
  1686. x86_pmu.event_constraints = intel_snb_event_constraints;
  1687. x86_pmu.pebs_constraints = intel_snb_pebs_event_constraints;
  1688. x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
  1689. x86_pmu.extra_regs = intel_snb_extra_regs;
  1690. /* all extra regs are per-cpu when HT is on */
  1691. x86_pmu.er_flags |= ERF_HAS_RSP_1;
  1692. x86_pmu.er_flags |= ERF_NO_HT_SHARING;
  1693. /* UOPS_ISSUED.ANY,c=1,i=1 to count stall cycles */
  1694. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
  1695. X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
  1696. /* UOPS_DISPATCHED.THREAD,c=1,i=1 to count stall cycles*/
  1697. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
  1698. X86_CONFIG(.event=0xb1, .umask=0x01, .inv=1, .cmask=1);
  1699. pr_cont("SandyBridge events, ");
  1700. break;
  1701. default:
  1702. switch (x86_pmu.version) {
  1703. case 1:
  1704. x86_pmu.event_constraints = intel_v1_event_constraints;
  1705. pr_cont("generic architected perfmon v1, ");
  1706. break;
  1707. default:
  1708. /*
  1709. * default constraints for v2 and up
  1710. */
  1711. x86_pmu.event_constraints = intel_gen_event_constraints;
  1712. pr_cont("generic architected perfmon, ");
  1713. break;
  1714. }
  1715. }
  1716. return 0;
  1717. }