perf_event_intel.c 58 KB

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