perf_event_intel.c 60 KB

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