perf_event_intel.c 60 KB

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