perf_event_intel.c 74 KB

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