perf_event_intel.c 61 KB

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