perf_event_intel.c 60 KB

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