perf_event_intel.c 69 KB

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