perf_event_intel.c 65 KB

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