perf_event_intel.c 61 KB

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