perf_event_intel.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408
  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 <linux/watchdog.h>
  14. #include <asm/cpufeature.h>
  15. #include <asm/hardirq.h>
  16. #include <asm/apic.h>
  17. #include "perf_event.h"
  18. /*
  19. * Intel PerfMon, used on Core and later.
  20. */
  21. static u64 intel_perfmon_event_map[PERF_COUNT_HW_MAX] __read_mostly =
  22. {
  23. [PERF_COUNT_HW_CPU_CYCLES] = 0x003c,
  24. [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
  25. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x4f2e,
  26. [PERF_COUNT_HW_CACHE_MISSES] = 0x412e,
  27. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
  28. [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
  29. [PERF_COUNT_HW_BUS_CYCLES] = 0x013c,
  30. [PERF_COUNT_HW_REF_CPU_CYCLES] = 0x0300, /* pseudo-encoding */
  31. };
  32. static struct event_constraint intel_core_event_constraints[] __read_mostly =
  33. {
  34. INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
  35. INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
  36. INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
  37. INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
  38. INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
  39. INTEL_EVENT_CONSTRAINT(0xc1, 0x1), /* FP_COMP_INSTR_RET */
  40. EVENT_CONSTRAINT_END
  41. };
  42. static struct event_constraint intel_core2_event_constraints[] __read_mostly =
  43. {
  44. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  45. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  46. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  47. INTEL_EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */
  48. INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
  49. INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
  50. INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
  51. INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
  52. INTEL_EVENT_CONSTRAINT(0x18, 0x1), /* IDLE_DURING_DIV */
  53. INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
  54. INTEL_EVENT_CONSTRAINT(0xa1, 0x1), /* RS_UOPS_DISPATCH_CYCLES */
  55. INTEL_EVENT_CONSTRAINT(0xc9, 0x1), /* ITLB_MISS_RETIRED (T30-9) */
  56. INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED */
  57. EVENT_CONSTRAINT_END
  58. };
  59. static struct event_constraint intel_nehalem_event_constraints[] __read_mostly =
  60. {
  61. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  62. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  63. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  64. INTEL_EVENT_CONSTRAINT(0x40, 0x3), /* L1D_CACHE_LD */
  65. INTEL_EVENT_CONSTRAINT(0x41, 0x3), /* L1D_CACHE_ST */
  66. INTEL_EVENT_CONSTRAINT(0x42, 0x3), /* L1D_CACHE_LOCK */
  67. INTEL_EVENT_CONSTRAINT(0x43, 0x3), /* L1D_ALL_REF */
  68. INTEL_EVENT_CONSTRAINT(0x48, 0x3), /* L1D_PEND_MISS */
  69. INTEL_EVENT_CONSTRAINT(0x4e, 0x3), /* L1D_PREFETCH */
  70. INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
  71. INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
  72. EVENT_CONSTRAINT_END
  73. };
  74. static struct extra_reg intel_nehalem_extra_regs[] __read_mostly =
  75. {
  76. /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
  77. INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0xffff, RSP_0),
  78. INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x100b),
  79. EVENT_EXTRA_END
  80. };
  81. static struct event_constraint intel_westmere_event_constraints[] __read_mostly =
  82. {
  83. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  84. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  85. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  86. INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
  87. INTEL_EVENT_CONSTRAINT(0x60, 0x1), /* OFFCORE_REQUESTS_OUTSTANDING */
  88. INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
  89. INTEL_EVENT_CONSTRAINT(0xb3, 0x1), /* SNOOPQ_REQUEST_OUTSTANDING */
  90. EVENT_CONSTRAINT_END
  91. };
  92. static struct event_constraint intel_snb_event_constraints[] __read_mostly =
  93. {
  94. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  95. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  96. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  97. INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_DISPATCH */
  98. INTEL_UEVENT_CONSTRAINT(0x05a3, 0xf), /* CYCLE_ACTIVITY.STALLS_L2_PENDING */
  99. INTEL_UEVENT_CONSTRAINT(0x02a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
  100. INTEL_UEVENT_CONSTRAINT(0x06a3, 0x4), /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
  101. INTEL_EVENT_CONSTRAINT(0x48, 0x4), /* L1D_PEND_MISS.PENDING */
  102. INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
  103. INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.LOAD_LATENCY */
  104. INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_DISPATCH */
  105. INTEL_UEVENT_CONSTRAINT(0x02a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
  106. INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
  107. INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  108. INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  109. INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
  110. EVENT_CONSTRAINT_END
  111. };
  112. static struct event_constraint intel_ivb_event_constraints[] __read_mostly =
  113. {
  114. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  115. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  116. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  117. INTEL_UEVENT_CONSTRAINT(0x0148, 0x4), /* L1D_PEND_MISS.PENDING */
  118. INTEL_UEVENT_CONSTRAINT(0x0279, 0xf), /* IDQ.EMTPY */
  119. INTEL_UEVENT_CONSTRAINT(0x019c, 0xf), /* IDQ_UOPS_NOT_DELIVERED.CORE */
  120. INTEL_UEVENT_CONSTRAINT(0x02a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_LDM_PENDING */
  121. INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_EXECUTE */
  122. INTEL_UEVENT_CONSTRAINT(0x05a3, 0xf), /* CYCLE_ACTIVITY.STALLS_L2_PENDING */
  123. INTEL_UEVENT_CONSTRAINT(0x06a3, 0xf), /* CYCLE_ACTIVITY.STALLS_LDM_PENDING */
  124. INTEL_UEVENT_CONSTRAINT(0x08a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
  125. INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4), /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
  126. INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
  127. INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
  128. INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  129. INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  130. INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* 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_UEVENT_CONSTRAINT(0x08a3, 0x4),
  194. /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
  195. INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4),
  196. /* CYCLE_ACTIVITY.CYCLES_NO_EXECUTE */
  197. INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf),
  198. INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
  199. INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  200. INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  201. INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
  202. EVENT_CONSTRAINT_END
  203. };
  204. struct event_constraint intel_bdw_event_constraints[] = {
  205. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  206. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  207. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  208. INTEL_UEVENT_CONSTRAINT(0x148, 0x4), /* L1D_PEND_MISS.PENDING */
  209. INTEL_EVENT_CONSTRAINT(0xa3, 0x4), /* CYCLE_ACTIVITY.* */
  210. EVENT_CONSTRAINT_END
  211. };
  212. static u64 intel_pmu_event_map(int hw_event)
  213. {
  214. return intel_perfmon_event_map[hw_event];
  215. }
  216. #define SNB_DMND_DATA_RD (1ULL << 0)
  217. #define SNB_DMND_RFO (1ULL << 1)
  218. #define SNB_DMND_IFETCH (1ULL << 2)
  219. #define SNB_DMND_WB (1ULL << 3)
  220. #define SNB_PF_DATA_RD (1ULL << 4)
  221. #define SNB_PF_RFO (1ULL << 5)
  222. #define SNB_PF_IFETCH (1ULL << 6)
  223. #define SNB_LLC_DATA_RD (1ULL << 7)
  224. #define SNB_LLC_RFO (1ULL << 8)
  225. #define SNB_LLC_IFETCH (1ULL << 9)
  226. #define SNB_BUS_LOCKS (1ULL << 10)
  227. #define SNB_STRM_ST (1ULL << 11)
  228. #define SNB_OTHER (1ULL << 15)
  229. #define SNB_RESP_ANY (1ULL << 16)
  230. #define SNB_NO_SUPP (1ULL << 17)
  231. #define SNB_LLC_HITM (1ULL << 18)
  232. #define SNB_LLC_HITE (1ULL << 19)
  233. #define SNB_LLC_HITS (1ULL << 20)
  234. #define SNB_LLC_HITF (1ULL << 21)
  235. #define SNB_LOCAL (1ULL << 22)
  236. #define SNB_REMOTE (0xffULL << 23)
  237. #define SNB_SNP_NONE (1ULL << 31)
  238. #define SNB_SNP_NOT_NEEDED (1ULL << 32)
  239. #define SNB_SNP_MISS (1ULL << 33)
  240. #define SNB_NO_FWD (1ULL << 34)
  241. #define SNB_SNP_FWD (1ULL << 35)
  242. #define SNB_HITM (1ULL << 36)
  243. #define SNB_NON_DRAM (1ULL << 37)
  244. #define SNB_DMND_READ (SNB_DMND_DATA_RD|SNB_LLC_DATA_RD)
  245. #define SNB_DMND_WRITE (SNB_DMND_RFO|SNB_LLC_RFO)
  246. #define SNB_DMND_PREFETCH (SNB_PF_DATA_RD|SNB_PF_RFO)
  247. #define SNB_SNP_ANY (SNB_SNP_NONE|SNB_SNP_NOT_NEEDED| \
  248. SNB_SNP_MISS|SNB_NO_FWD|SNB_SNP_FWD| \
  249. SNB_HITM)
  250. #define SNB_DRAM_ANY (SNB_LOCAL|SNB_REMOTE|SNB_SNP_ANY)
  251. #define SNB_DRAM_REMOTE (SNB_REMOTE|SNB_SNP_ANY)
  252. #define SNB_L3_ACCESS SNB_RESP_ANY
  253. #define SNB_L3_MISS (SNB_DRAM_ANY|SNB_NON_DRAM)
  254. static __initconst const u64 snb_hw_cache_extra_regs
  255. [PERF_COUNT_HW_CACHE_MAX]
  256. [PERF_COUNT_HW_CACHE_OP_MAX]
  257. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  258. {
  259. [ C(LL ) ] = {
  260. [ C(OP_READ) ] = {
  261. [ C(RESULT_ACCESS) ] = SNB_DMND_READ|SNB_L3_ACCESS,
  262. [ C(RESULT_MISS) ] = SNB_DMND_READ|SNB_L3_MISS,
  263. },
  264. [ C(OP_WRITE) ] = {
  265. [ C(RESULT_ACCESS) ] = SNB_DMND_WRITE|SNB_L3_ACCESS,
  266. [ C(RESULT_MISS) ] = SNB_DMND_WRITE|SNB_L3_MISS,
  267. },
  268. [ C(OP_PREFETCH) ] = {
  269. [ C(RESULT_ACCESS) ] = SNB_DMND_PREFETCH|SNB_L3_ACCESS,
  270. [ C(RESULT_MISS) ] = SNB_DMND_PREFETCH|SNB_L3_MISS,
  271. },
  272. },
  273. [ C(NODE) ] = {
  274. [ C(OP_READ) ] = {
  275. [ C(RESULT_ACCESS) ] = SNB_DMND_READ|SNB_DRAM_ANY,
  276. [ C(RESULT_MISS) ] = SNB_DMND_READ|SNB_DRAM_REMOTE,
  277. },
  278. [ C(OP_WRITE) ] = {
  279. [ C(RESULT_ACCESS) ] = SNB_DMND_WRITE|SNB_DRAM_ANY,
  280. [ C(RESULT_MISS) ] = SNB_DMND_WRITE|SNB_DRAM_REMOTE,
  281. },
  282. [ C(OP_PREFETCH) ] = {
  283. [ C(RESULT_ACCESS) ] = SNB_DMND_PREFETCH|SNB_DRAM_ANY,
  284. [ C(RESULT_MISS) ] = SNB_DMND_PREFETCH|SNB_DRAM_REMOTE,
  285. },
  286. },
  287. };
  288. static __initconst const u64 snb_hw_cache_event_ids
  289. [PERF_COUNT_HW_CACHE_MAX]
  290. [PERF_COUNT_HW_CACHE_OP_MAX]
  291. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  292. {
  293. [ C(L1D) ] = {
  294. [ C(OP_READ) ] = {
  295. [ C(RESULT_ACCESS) ] = 0xf1d0, /* MEM_UOP_RETIRED.LOADS */
  296. [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPLACEMENT */
  297. },
  298. [ C(OP_WRITE) ] = {
  299. [ C(RESULT_ACCESS) ] = 0xf2d0, /* MEM_UOP_RETIRED.STORES */
  300. [ C(RESULT_MISS) ] = 0x0851, /* L1D.ALL_M_REPLACEMENT */
  301. },
  302. [ C(OP_PREFETCH) ] = {
  303. [ C(RESULT_ACCESS) ] = 0x0,
  304. [ C(RESULT_MISS) ] = 0x024e, /* HW_PRE_REQ.DL1_MISS */
  305. },
  306. },
  307. [ C(L1I ) ] = {
  308. [ C(OP_READ) ] = {
  309. [ C(RESULT_ACCESS) ] = 0x0,
  310. [ C(RESULT_MISS) ] = 0x0280, /* ICACHE.MISSES */
  311. },
  312. [ C(OP_WRITE) ] = {
  313. [ C(RESULT_ACCESS) ] = -1,
  314. [ C(RESULT_MISS) ] = -1,
  315. },
  316. [ C(OP_PREFETCH) ] = {
  317. [ C(RESULT_ACCESS) ] = 0x0,
  318. [ C(RESULT_MISS) ] = 0x0,
  319. },
  320. },
  321. [ C(LL ) ] = {
  322. [ C(OP_READ) ] = {
  323. /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
  324. [ C(RESULT_ACCESS) ] = 0x01b7,
  325. /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
  326. [ C(RESULT_MISS) ] = 0x01b7,
  327. },
  328. [ C(OP_WRITE) ] = {
  329. /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
  330. [ C(RESULT_ACCESS) ] = 0x01b7,
  331. /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
  332. [ C(RESULT_MISS) ] = 0x01b7,
  333. },
  334. [ C(OP_PREFETCH) ] = {
  335. /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
  336. [ C(RESULT_ACCESS) ] = 0x01b7,
  337. /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
  338. [ C(RESULT_MISS) ] = 0x01b7,
  339. },
  340. },
  341. [ C(DTLB) ] = {
  342. [ C(OP_READ) ] = {
  343. [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOP_RETIRED.ALL_LOADS */
  344. [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.CAUSES_A_WALK */
  345. },
  346. [ C(OP_WRITE) ] = {
  347. [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOP_RETIRED.ALL_STORES */
  348. [ C(RESULT_MISS) ] = 0x0149, /* DTLB_STORE_MISSES.MISS_CAUSES_A_WALK */
  349. },
  350. [ C(OP_PREFETCH) ] = {
  351. [ C(RESULT_ACCESS) ] = 0x0,
  352. [ C(RESULT_MISS) ] = 0x0,
  353. },
  354. },
  355. [ C(ITLB) ] = {
  356. [ C(OP_READ) ] = {
  357. [ C(RESULT_ACCESS) ] = 0x1085, /* ITLB_MISSES.STLB_HIT */
  358. [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.CAUSES_A_WALK */
  359. },
  360. [ C(OP_WRITE) ] = {
  361. [ C(RESULT_ACCESS) ] = -1,
  362. [ C(RESULT_MISS) ] = -1,
  363. },
  364. [ C(OP_PREFETCH) ] = {
  365. [ C(RESULT_ACCESS) ] = -1,
  366. [ C(RESULT_MISS) ] = -1,
  367. },
  368. },
  369. [ C(BPU ) ] = {
  370. [ C(OP_READ) ] = {
  371. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
  372. [ C(RESULT_MISS) ] = 0x00c5, /* BR_MISP_RETIRED.ALL_BRANCHES */
  373. },
  374. [ C(OP_WRITE) ] = {
  375. [ C(RESULT_ACCESS) ] = -1,
  376. [ C(RESULT_MISS) ] = -1,
  377. },
  378. [ C(OP_PREFETCH) ] = {
  379. [ C(RESULT_ACCESS) ] = -1,
  380. [ C(RESULT_MISS) ] = -1,
  381. },
  382. },
  383. [ C(NODE) ] = {
  384. [ C(OP_READ) ] = {
  385. [ C(RESULT_ACCESS) ] = 0x01b7,
  386. [ C(RESULT_MISS) ] = 0x01b7,
  387. },
  388. [ C(OP_WRITE) ] = {
  389. [ C(RESULT_ACCESS) ] = 0x01b7,
  390. [ C(RESULT_MISS) ] = 0x01b7,
  391. },
  392. [ C(OP_PREFETCH) ] = {
  393. [ C(RESULT_ACCESS) ] = 0x01b7,
  394. [ C(RESULT_MISS) ] = 0x01b7,
  395. },
  396. },
  397. };
  398. /*
  399. * Notes on the events:
  400. * - data reads do not include code reads (comparable to earlier tables)
  401. * - data counts include speculative execution (except L1 write, dtlb, bpu)
  402. * - remote node access includes remote memory, remote cache, remote mmio.
  403. * - prefetches are not included in the counts because they are not
  404. * reliably counted.
  405. */
  406. #define HSW_DEMAND_DATA_RD BIT_ULL(0)
  407. #define HSW_DEMAND_RFO BIT_ULL(1)
  408. #define HSW_ANY_RESPONSE BIT_ULL(16)
  409. #define HSW_SUPPLIER_NONE BIT_ULL(17)
  410. #define HSW_L3_MISS_LOCAL_DRAM BIT_ULL(22)
  411. #define HSW_L3_MISS_REMOTE_HOP0 BIT_ULL(27)
  412. #define HSW_L3_MISS_REMOTE_HOP1 BIT_ULL(28)
  413. #define HSW_L3_MISS_REMOTE_HOP2P BIT_ULL(29)
  414. #define HSW_L3_MISS (HSW_L3_MISS_LOCAL_DRAM| \
  415. HSW_L3_MISS_REMOTE_HOP0|HSW_L3_MISS_REMOTE_HOP1| \
  416. HSW_L3_MISS_REMOTE_HOP2P)
  417. #define HSW_SNOOP_NONE BIT_ULL(31)
  418. #define HSW_SNOOP_NOT_NEEDED BIT_ULL(32)
  419. #define HSW_SNOOP_MISS BIT_ULL(33)
  420. #define HSW_SNOOP_HIT_NO_FWD BIT_ULL(34)
  421. #define HSW_SNOOP_HIT_WITH_FWD BIT_ULL(35)
  422. #define HSW_SNOOP_HITM BIT_ULL(36)
  423. #define HSW_SNOOP_NON_DRAM BIT_ULL(37)
  424. #define HSW_ANY_SNOOP (HSW_SNOOP_NONE| \
  425. HSW_SNOOP_NOT_NEEDED|HSW_SNOOP_MISS| \
  426. HSW_SNOOP_HIT_NO_FWD|HSW_SNOOP_HIT_WITH_FWD| \
  427. HSW_SNOOP_HITM|HSW_SNOOP_NON_DRAM)
  428. #define HSW_SNOOP_DRAM (HSW_ANY_SNOOP & ~HSW_SNOOP_NON_DRAM)
  429. #define HSW_DEMAND_READ HSW_DEMAND_DATA_RD
  430. #define HSW_DEMAND_WRITE HSW_DEMAND_RFO
  431. #define HSW_L3_MISS_REMOTE (HSW_L3_MISS_REMOTE_HOP0|\
  432. HSW_L3_MISS_REMOTE_HOP1|HSW_L3_MISS_REMOTE_HOP2P)
  433. #define HSW_LLC_ACCESS HSW_ANY_RESPONSE
  434. #define BDW_L3_MISS_LOCAL BIT(26)
  435. #define BDW_L3_MISS (BDW_L3_MISS_LOCAL| \
  436. HSW_L3_MISS_REMOTE_HOP0|HSW_L3_MISS_REMOTE_HOP1| \
  437. HSW_L3_MISS_REMOTE_HOP2P)
  438. static __initconst const u64 hsw_hw_cache_event_ids
  439. [PERF_COUNT_HW_CACHE_MAX]
  440. [PERF_COUNT_HW_CACHE_OP_MAX]
  441. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  442. {
  443. [ C(L1D ) ] = {
  444. [ C(OP_READ) ] = {
  445. [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
  446. [ C(RESULT_MISS) ] = 0x151, /* L1D.REPLACEMENT */
  447. },
  448. [ C(OP_WRITE) ] = {
  449. [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
  450. [ C(RESULT_MISS) ] = 0x0,
  451. },
  452. [ C(OP_PREFETCH) ] = {
  453. [ C(RESULT_ACCESS) ] = 0x0,
  454. [ C(RESULT_MISS) ] = 0x0,
  455. },
  456. },
  457. [ C(L1I ) ] = {
  458. [ C(OP_READ) ] = {
  459. [ C(RESULT_ACCESS) ] = 0x0,
  460. [ C(RESULT_MISS) ] = 0x280, /* ICACHE.MISSES */
  461. },
  462. [ C(OP_WRITE) ] = {
  463. [ C(RESULT_ACCESS) ] = -1,
  464. [ C(RESULT_MISS) ] = -1,
  465. },
  466. [ C(OP_PREFETCH) ] = {
  467. [ C(RESULT_ACCESS) ] = 0x0,
  468. [ C(RESULT_MISS) ] = 0x0,
  469. },
  470. },
  471. [ C(LL ) ] = {
  472. [ C(OP_READ) ] = {
  473. [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  474. [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  475. },
  476. [ C(OP_WRITE) ] = {
  477. [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  478. [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  479. },
  480. [ C(OP_PREFETCH) ] = {
  481. [ C(RESULT_ACCESS) ] = 0x0,
  482. [ C(RESULT_MISS) ] = 0x0,
  483. },
  484. },
  485. [ C(DTLB) ] = {
  486. [ C(OP_READ) ] = {
  487. [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
  488. [ C(RESULT_MISS) ] = 0x108, /* DTLB_LOAD_MISSES.MISS_CAUSES_A_WALK */
  489. },
  490. [ C(OP_WRITE) ] = {
  491. [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
  492. [ C(RESULT_MISS) ] = 0x149, /* DTLB_STORE_MISSES.MISS_CAUSES_A_WALK */
  493. },
  494. [ C(OP_PREFETCH) ] = {
  495. [ C(RESULT_ACCESS) ] = 0x0,
  496. [ C(RESULT_MISS) ] = 0x0,
  497. },
  498. },
  499. [ C(ITLB) ] = {
  500. [ C(OP_READ) ] = {
  501. [ C(RESULT_ACCESS) ] = 0x6085, /* ITLB_MISSES.STLB_HIT */
  502. [ C(RESULT_MISS) ] = 0x185, /* ITLB_MISSES.MISS_CAUSES_A_WALK */
  503. },
  504. [ C(OP_WRITE) ] = {
  505. [ C(RESULT_ACCESS) ] = -1,
  506. [ C(RESULT_MISS) ] = -1,
  507. },
  508. [ C(OP_PREFETCH) ] = {
  509. [ C(RESULT_ACCESS) ] = -1,
  510. [ C(RESULT_MISS) ] = -1,
  511. },
  512. },
  513. [ C(BPU ) ] = {
  514. [ C(OP_READ) ] = {
  515. [ C(RESULT_ACCESS) ] = 0xc4, /* BR_INST_RETIRED.ALL_BRANCHES */
  516. [ C(RESULT_MISS) ] = 0xc5, /* BR_MISP_RETIRED.ALL_BRANCHES */
  517. },
  518. [ C(OP_WRITE) ] = {
  519. [ C(RESULT_ACCESS) ] = -1,
  520. [ C(RESULT_MISS) ] = -1,
  521. },
  522. [ C(OP_PREFETCH) ] = {
  523. [ C(RESULT_ACCESS) ] = -1,
  524. [ C(RESULT_MISS) ] = -1,
  525. },
  526. },
  527. [ C(NODE) ] = {
  528. [ C(OP_READ) ] = {
  529. [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  530. [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  531. },
  532. [ C(OP_WRITE) ] = {
  533. [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  534. [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  535. },
  536. [ C(OP_PREFETCH) ] = {
  537. [ C(RESULT_ACCESS) ] = 0x0,
  538. [ C(RESULT_MISS) ] = 0x0,
  539. },
  540. },
  541. };
  542. static __initconst const u64 hsw_hw_cache_extra_regs
  543. [PERF_COUNT_HW_CACHE_MAX]
  544. [PERF_COUNT_HW_CACHE_OP_MAX]
  545. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  546. {
  547. [ C(LL ) ] = {
  548. [ C(OP_READ) ] = {
  549. [ C(RESULT_ACCESS) ] = HSW_DEMAND_READ|
  550. HSW_LLC_ACCESS,
  551. [ C(RESULT_MISS) ] = HSW_DEMAND_READ|
  552. HSW_L3_MISS|HSW_ANY_SNOOP,
  553. },
  554. [ C(OP_WRITE) ] = {
  555. [ C(RESULT_ACCESS) ] = HSW_DEMAND_WRITE|
  556. HSW_LLC_ACCESS,
  557. [ C(RESULT_MISS) ] = HSW_DEMAND_WRITE|
  558. HSW_L3_MISS|HSW_ANY_SNOOP,
  559. },
  560. [ C(OP_PREFETCH) ] = {
  561. [ C(RESULT_ACCESS) ] = 0x0,
  562. [ C(RESULT_MISS) ] = 0x0,
  563. },
  564. },
  565. [ C(NODE) ] = {
  566. [ C(OP_READ) ] = {
  567. [ C(RESULT_ACCESS) ] = HSW_DEMAND_READ|
  568. HSW_L3_MISS_LOCAL_DRAM|
  569. HSW_SNOOP_DRAM,
  570. [ C(RESULT_MISS) ] = HSW_DEMAND_READ|
  571. HSW_L3_MISS_REMOTE|
  572. HSW_SNOOP_DRAM,
  573. },
  574. [ C(OP_WRITE) ] = {
  575. [ C(RESULT_ACCESS) ] = HSW_DEMAND_WRITE|
  576. HSW_L3_MISS_LOCAL_DRAM|
  577. HSW_SNOOP_DRAM,
  578. [ C(RESULT_MISS) ] = HSW_DEMAND_WRITE|
  579. HSW_L3_MISS_REMOTE|
  580. HSW_SNOOP_DRAM,
  581. },
  582. [ C(OP_PREFETCH) ] = {
  583. [ C(RESULT_ACCESS) ] = 0x0,
  584. [ C(RESULT_MISS) ] = 0x0,
  585. },
  586. },
  587. };
  588. static __initconst const u64 westmere_hw_cache_event_ids
  589. [PERF_COUNT_HW_CACHE_MAX]
  590. [PERF_COUNT_HW_CACHE_OP_MAX]
  591. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  592. {
  593. [ C(L1D) ] = {
  594. [ C(OP_READ) ] = {
  595. [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
  596. [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */
  597. },
  598. [ C(OP_WRITE) ] = {
  599. [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
  600. [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */
  601. },
  602. [ C(OP_PREFETCH) ] = {
  603. [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
  604. [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
  605. },
  606. },
  607. [ C(L1I ) ] = {
  608. [ C(OP_READ) ] = {
  609. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  610. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  611. },
  612. [ C(OP_WRITE) ] = {
  613. [ C(RESULT_ACCESS) ] = -1,
  614. [ C(RESULT_MISS) ] = -1,
  615. },
  616. [ C(OP_PREFETCH) ] = {
  617. [ C(RESULT_ACCESS) ] = 0x0,
  618. [ C(RESULT_MISS) ] = 0x0,
  619. },
  620. },
  621. [ C(LL ) ] = {
  622. [ C(OP_READ) ] = {
  623. /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
  624. [ C(RESULT_ACCESS) ] = 0x01b7,
  625. /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
  626. [ C(RESULT_MISS) ] = 0x01b7,
  627. },
  628. /*
  629. * Use RFO, not WRITEBACK, because a write miss would typically occur
  630. * on RFO.
  631. */
  632. [ C(OP_WRITE) ] = {
  633. /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
  634. [ C(RESULT_ACCESS) ] = 0x01b7,
  635. /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
  636. [ C(RESULT_MISS) ] = 0x01b7,
  637. },
  638. [ C(OP_PREFETCH) ] = {
  639. /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
  640. [ C(RESULT_ACCESS) ] = 0x01b7,
  641. /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
  642. [ C(RESULT_MISS) ] = 0x01b7,
  643. },
  644. },
  645. [ C(DTLB) ] = {
  646. [ C(OP_READ) ] = {
  647. [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
  648. [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
  649. },
  650. [ C(OP_WRITE) ] = {
  651. [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
  652. [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
  653. },
  654. [ C(OP_PREFETCH) ] = {
  655. [ C(RESULT_ACCESS) ] = 0x0,
  656. [ C(RESULT_MISS) ] = 0x0,
  657. },
  658. },
  659. [ C(ITLB) ] = {
  660. [ C(OP_READ) ] = {
  661. [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
  662. [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.ANY */
  663. },
  664. [ C(OP_WRITE) ] = {
  665. [ C(RESULT_ACCESS) ] = -1,
  666. [ C(RESULT_MISS) ] = -1,
  667. },
  668. [ C(OP_PREFETCH) ] = {
  669. [ C(RESULT_ACCESS) ] = -1,
  670. [ C(RESULT_MISS) ] = -1,
  671. },
  672. },
  673. [ C(BPU ) ] = {
  674. [ C(OP_READ) ] = {
  675. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
  676. [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
  677. },
  678. [ C(OP_WRITE) ] = {
  679. [ C(RESULT_ACCESS) ] = -1,
  680. [ C(RESULT_MISS) ] = -1,
  681. },
  682. [ C(OP_PREFETCH) ] = {
  683. [ C(RESULT_ACCESS) ] = -1,
  684. [ C(RESULT_MISS) ] = -1,
  685. },
  686. },
  687. [ C(NODE) ] = {
  688. [ C(OP_READ) ] = {
  689. [ C(RESULT_ACCESS) ] = 0x01b7,
  690. [ C(RESULT_MISS) ] = 0x01b7,
  691. },
  692. [ C(OP_WRITE) ] = {
  693. [ C(RESULT_ACCESS) ] = 0x01b7,
  694. [ C(RESULT_MISS) ] = 0x01b7,
  695. },
  696. [ C(OP_PREFETCH) ] = {
  697. [ C(RESULT_ACCESS) ] = 0x01b7,
  698. [ C(RESULT_MISS) ] = 0x01b7,
  699. },
  700. },
  701. };
  702. /*
  703. * Nehalem/Westmere MSR_OFFCORE_RESPONSE bits;
  704. * See IA32 SDM Vol 3B 30.6.1.3
  705. */
  706. #define NHM_DMND_DATA_RD (1 << 0)
  707. #define NHM_DMND_RFO (1 << 1)
  708. #define NHM_DMND_IFETCH (1 << 2)
  709. #define NHM_DMND_WB (1 << 3)
  710. #define NHM_PF_DATA_RD (1 << 4)
  711. #define NHM_PF_DATA_RFO (1 << 5)
  712. #define NHM_PF_IFETCH (1 << 6)
  713. #define NHM_OFFCORE_OTHER (1 << 7)
  714. #define NHM_UNCORE_HIT (1 << 8)
  715. #define NHM_OTHER_CORE_HIT_SNP (1 << 9)
  716. #define NHM_OTHER_CORE_HITM (1 << 10)
  717. /* reserved */
  718. #define NHM_REMOTE_CACHE_FWD (1 << 12)
  719. #define NHM_REMOTE_DRAM (1 << 13)
  720. #define NHM_LOCAL_DRAM (1 << 14)
  721. #define NHM_NON_DRAM (1 << 15)
  722. #define NHM_LOCAL (NHM_LOCAL_DRAM|NHM_REMOTE_CACHE_FWD)
  723. #define NHM_REMOTE (NHM_REMOTE_DRAM)
  724. #define NHM_DMND_READ (NHM_DMND_DATA_RD)
  725. #define NHM_DMND_WRITE (NHM_DMND_RFO|NHM_DMND_WB)
  726. #define NHM_DMND_PREFETCH (NHM_PF_DATA_RD|NHM_PF_DATA_RFO)
  727. #define NHM_L3_HIT (NHM_UNCORE_HIT|NHM_OTHER_CORE_HIT_SNP|NHM_OTHER_CORE_HITM)
  728. #define NHM_L3_MISS (NHM_NON_DRAM|NHM_LOCAL_DRAM|NHM_REMOTE_DRAM|NHM_REMOTE_CACHE_FWD)
  729. #define NHM_L3_ACCESS (NHM_L3_HIT|NHM_L3_MISS)
  730. static __initconst const u64 nehalem_hw_cache_extra_regs
  731. [PERF_COUNT_HW_CACHE_MAX]
  732. [PERF_COUNT_HW_CACHE_OP_MAX]
  733. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  734. {
  735. [ C(LL ) ] = {
  736. [ C(OP_READ) ] = {
  737. [ C(RESULT_ACCESS) ] = NHM_DMND_READ|NHM_L3_ACCESS,
  738. [ C(RESULT_MISS) ] = NHM_DMND_READ|NHM_L3_MISS,
  739. },
  740. [ C(OP_WRITE) ] = {
  741. [ C(RESULT_ACCESS) ] = NHM_DMND_WRITE|NHM_L3_ACCESS,
  742. [ C(RESULT_MISS) ] = NHM_DMND_WRITE|NHM_L3_MISS,
  743. },
  744. [ C(OP_PREFETCH) ] = {
  745. [ C(RESULT_ACCESS) ] = NHM_DMND_PREFETCH|NHM_L3_ACCESS,
  746. [ C(RESULT_MISS) ] = NHM_DMND_PREFETCH|NHM_L3_MISS,
  747. },
  748. },
  749. [ C(NODE) ] = {
  750. [ C(OP_READ) ] = {
  751. [ C(RESULT_ACCESS) ] = NHM_DMND_READ|NHM_LOCAL|NHM_REMOTE,
  752. [ C(RESULT_MISS) ] = NHM_DMND_READ|NHM_REMOTE,
  753. },
  754. [ C(OP_WRITE) ] = {
  755. [ C(RESULT_ACCESS) ] = NHM_DMND_WRITE|NHM_LOCAL|NHM_REMOTE,
  756. [ C(RESULT_MISS) ] = NHM_DMND_WRITE|NHM_REMOTE,
  757. },
  758. [ C(OP_PREFETCH) ] = {
  759. [ C(RESULT_ACCESS) ] = NHM_DMND_PREFETCH|NHM_LOCAL|NHM_REMOTE,
  760. [ C(RESULT_MISS) ] = NHM_DMND_PREFETCH|NHM_REMOTE,
  761. },
  762. },
  763. };
  764. static __initconst const u64 nehalem_hw_cache_event_ids
  765. [PERF_COUNT_HW_CACHE_MAX]
  766. [PERF_COUNT_HW_CACHE_OP_MAX]
  767. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  768. {
  769. [ C(L1D) ] = {
  770. [ C(OP_READ) ] = {
  771. [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
  772. [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */
  773. },
  774. [ C(OP_WRITE) ] = {
  775. [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
  776. [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */
  777. },
  778. [ C(OP_PREFETCH) ] = {
  779. [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
  780. [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
  781. },
  782. },
  783. [ C(L1I ) ] = {
  784. [ C(OP_READ) ] = {
  785. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  786. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  787. },
  788. [ C(OP_WRITE) ] = {
  789. [ C(RESULT_ACCESS) ] = -1,
  790. [ C(RESULT_MISS) ] = -1,
  791. },
  792. [ C(OP_PREFETCH) ] = {
  793. [ C(RESULT_ACCESS) ] = 0x0,
  794. [ C(RESULT_MISS) ] = 0x0,
  795. },
  796. },
  797. [ C(LL ) ] = {
  798. [ C(OP_READ) ] = {
  799. /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
  800. [ C(RESULT_ACCESS) ] = 0x01b7,
  801. /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
  802. [ C(RESULT_MISS) ] = 0x01b7,
  803. },
  804. /*
  805. * Use RFO, not WRITEBACK, because a write miss would typically occur
  806. * on RFO.
  807. */
  808. [ C(OP_WRITE) ] = {
  809. /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
  810. [ C(RESULT_ACCESS) ] = 0x01b7,
  811. /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
  812. [ C(RESULT_MISS) ] = 0x01b7,
  813. },
  814. [ C(OP_PREFETCH) ] = {
  815. /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
  816. [ C(RESULT_ACCESS) ] = 0x01b7,
  817. /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
  818. [ C(RESULT_MISS) ] = 0x01b7,
  819. },
  820. },
  821. [ C(DTLB) ] = {
  822. [ C(OP_READ) ] = {
  823. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
  824. [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
  825. },
  826. [ C(OP_WRITE) ] = {
  827. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
  828. [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
  829. },
  830. [ C(OP_PREFETCH) ] = {
  831. [ C(RESULT_ACCESS) ] = 0x0,
  832. [ C(RESULT_MISS) ] = 0x0,
  833. },
  834. },
  835. [ C(ITLB) ] = {
  836. [ C(OP_READ) ] = {
  837. [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
  838. [ C(RESULT_MISS) ] = 0x20c8, /* ITLB_MISS_RETIRED */
  839. },
  840. [ C(OP_WRITE) ] = {
  841. [ C(RESULT_ACCESS) ] = -1,
  842. [ C(RESULT_MISS) ] = -1,
  843. },
  844. [ C(OP_PREFETCH) ] = {
  845. [ C(RESULT_ACCESS) ] = -1,
  846. [ C(RESULT_MISS) ] = -1,
  847. },
  848. },
  849. [ C(BPU ) ] = {
  850. [ C(OP_READ) ] = {
  851. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
  852. [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
  853. },
  854. [ C(OP_WRITE) ] = {
  855. [ C(RESULT_ACCESS) ] = -1,
  856. [ C(RESULT_MISS) ] = -1,
  857. },
  858. [ C(OP_PREFETCH) ] = {
  859. [ C(RESULT_ACCESS) ] = -1,
  860. [ C(RESULT_MISS) ] = -1,
  861. },
  862. },
  863. [ C(NODE) ] = {
  864. [ C(OP_READ) ] = {
  865. [ C(RESULT_ACCESS) ] = 0x01b7,
  866. [ C(RESULT_MISS) ] = 0x01b7,
  867. },
  868. [ C(OP_WRITE) ] = {
  869. [ C(RESULT_ACCESS) ] = 0x01b7,
  870. [ C(RESULT_MISS) ] = 0x01b7,
  871. },
  872. [ C(OP_PREFETCH) ] = {
  873. [ C(RESULT_ACCESS) ] = 0x01b7,
  874. [ C(RESULT_MISS) ] = 0x01b7,
  875. },
  876. },
  877. };
  878. static __initconst const u64 core2_hw_cache_event_ids
  879. [PERF_COUNT_HW_CACHE_MAX]
  880. [PERF_COUNT_HW_CACHE_OP_MAX]
  881. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  882. {
  883. [ C(L1D) ] = {
  884. [ C(OP_READ) ] = {
  885. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
  886. [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
  887. },
  888. [ C(OP_WRITE) ] = {
  889. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
  890. [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
  891. },
  892. [ C(OP_PREFETCH) ] = {
  893. [ C(RESULT_ACCESS) ] = 0x104e, /* L1D_PREFETCH.REQUESTS */
  894. [ C(RESULT_MISS) ] = 0,
  895. },
  896. },
  897. [ C(L1I ) ] = {
  898. [ C(OP_READ) ] = {
  899. [ C(RESULT_ACCESS) ] = 0x0080, /* L1I.READS */
  900. [ C(RESULT_MISS) ] = 0x0081, /* L1I.MISSES */
  901. },
  902. [ C(OP_WRITE) ] = {
  903. [ C(RESULT_ACCESS) ] = -1,
  904. [ C(RESULT_MISS) ] = -1,
  905. },
  906. [ C(OP_PREFETCH) ] = {
  907. [ C(RESULT_ACCESS) ] = 0,
  908. [ C(RESULT_MISS) ] = 0,
  909. },
  910. },
  911. [ C(LL ) ] = {
  912. [ C(OP_READ) ] = {
  913. [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
  914. [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
  915. },
  916. [ C(OP_WRITE) ] = {
  917. [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
  918. [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
  919. },
  920. [ C(OP_PREFETCH) ] = {
  921. [ C(RESULT_ACCESS) ] = 0,
  922. [ C(RESULT_MISS) ] = 0,
  923. },
  924. },
  925. [ C(DTLB) ] = {
  926. [ C(OP_READ) ] = {
  927. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
  928. [ C(RESULT_MISS) ] = 0x0208, /* DTLB_MISSES.MISS_LD */
  929. },
  930. [ C(OP_WRITE) ] = {
  931. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
  932. [ C(RESULT_MISS) ] = 0x0808, /* DTLB_MISSES.MISS_ST */
  933. },
  934. [ C(OP_PREFETCH) ] = {
  935. [ C(RESULT_ACCESS) ] = 0,
  936. [ C(RESULT_MISS) ] = 0,
  937. },
  938. },
  939. [ C(ITLB) ] = {
  940. [ C(OP_READ) ] = {
  941. [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
  942. [ C(RESULT_MISS) ] = 0x1282, /* ITLBMISSES */
  943. },
  944. [ C(OP_WRITE) ] = {
  945. [ C(RESULT_ACCESS) ] = -1,
  946. [ C(RESULT_MISS) ] = -1,
  947. },
  948. [ C(OP_PREFETCH) ] = {
  949. [ C(RESULT_ACCESS) ] = -1,
  950. [ C(RESULT_MISS) ] = -1,
  951. },
  952. },
  953. [ C(BPU ) ] = {
  954. [ C(OP_READ) ] = {
  955. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
  956. [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
  957. },
  958. [ C(OP_WRITE) ] = {
  959. [ C(RESULT_ACCESS) ] = -1,
  960. [ C(RESULT_MISS) ] = -1,
  961. },
  962. [ C(OP_PREFETCH) ] = {
  963. [ C(RESULT_ACCESS) ] = -1,
  964. [ C(RESULT_MISS) ] = -1,
  965. },
  966. },
  967. };
  968. static __initconst const u64 atom_hw_cache_event_ids
  969. [PERF_COUNT_HW_CACHE_MAX]
  970. [PERF_COUNT_HW_CACHE_OP_MAX]
  971. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  972. {
  973. [ C(L1D) ] = {
  974. [ C(OP_READ) ] = {
  975. [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE.LD */
  976. [ C(RESULT_MISS) ] = 0,
  977. },
  978. [ C(OP_WRITE) ] = {
  979. [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE.ST */
  980. [ C(RESULT_MISS) ] = 0,
  981. },
  982. [ C(OP_PREFETCH) ] = {
  983. [ C(RESULT_ACCESS) ] = 0x0,
  984. [ C(RESULT_MISS) ] = 0,
  985. },
  986. },
  987. [ C(L1I ) ] = {
  988. [ C(OP_READ) ] = {
  989. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  990. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  991. },
  992. [ C(OP_WRITE) ] = {
  993. [ C(RESULT_ACCESS) ] = -1,
  994. [ C(RESULT_MISS) ] = -1,
  995. },
  996. [ C(OP_PREFETCH) ] = {
  997. [ C(RESULT_ACCESS) ] = 0,
  998. [ C(RESULT_MISS) ] = 0,
  999. },
  1000. },
  1001. [ C(LL ) ] = {
  1002. [ C(OP_READ) ] = {
  1003. [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
  1004. [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
  1005. },
  1006. [ C(OP_WRITE) ] = {
  1007. [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
  1008. [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
  1009. },
  1010. [ C(OP_PREFETCH) ] = {
  1011. [ C(RESULT_ACCESS) ] = 0,
  1012. [ C(RESULT_MISS) ] = 0,
  1013. },
  1014. },
  1015. [ C(DTLB) ] = {
  1016. [ C(OP_READ) ] = {
  1017. [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE_LD.MESI (alias) */
  1018. [ C(RESULT_MISS) ] = 0x0508, /* DTLB_MISSES.MISS_LD */
  1019. },
  1020. [ C(OP_WRITE) ] = {
  1021. [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE_ST.MESI (alias) */
  1022. [ C(RESULT_MISS) ] = 0x0608, /* DTLB_MISSES.MISS_ST */
  1023. },
  1024. [ C(OP_PREFETCH) ] = {
  1025. [ C(RESULT_ACCESS) ] = 0,
  1026. [ C(RESULT_MISS) ] = 0,
  1027. },
  1028. },
  1029. [ C(ITLB) ] = {
  1030. [ C(OP_READ) ] = {
  1031. [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
  1032. [ C(RESULT_MISS) ] = 0x0282, /* ITLB.MISSES */
  1033. },
  1034. [ C(OP_WRITE) ] = {
  1035. [ C(RESULT_ACCESS) ] = -1,
  1036. [ C(RESULT_MISS) ] = -1,
  1037. },
  1038. [ C(OP_PREFETCH) ] = {
  1039. [ C(RESULT_ACCESS) ] = -1,
  1040. [ C(RESULT_MISS) ] = -1,
  1041. },
  1042. },
  1043. [ C(BPU ) ] = {
  1044. [ C(OP_READ) ] = {
  1045. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
  1046. [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
  1047. },
  1048. [ C(OP_WRITE) ] = {
  1049. [ C(RESULT_ACCESS) ] = -1,
  1050. [ C(RESULT_MISS) ] = -1,
  1051. },
  1052. [ C(OP_PREFETCH) ] = {
  1053. [ C(RESULT_ACCESS) ] = -1,
  1054. [ C(RESULT_MISS) ] = -1,
  1055. },
  1056. },
  1057. };
  1058. static struct extra_reg intel_slm_extra_regs[] __read_mostly =
  1059. {
  1060. /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
  1061. INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x768005ffffull, RSP_0),
  1062. INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0x368005ffffull, RSP_1),
  1063. EVENT_EXTRA_END
  1064. };
  1065. #define SLM_DMND_READ SNB_DMND_DATA_RD
  1066. #define SLM_DMND_WRITE SNB_DMND_RFO
  1067. #define SLM_DMND_PREFETCH (SNB_PF_DATA_RD|SNB_PF_RFO)
  1068. #define SLM_SNP_ANY (SNB_SNP_NONE|SNB_SNP_MISS|SNB_NO_FWD|SNB_HITM)
  1069. #define SLM_LLC_ACCESS SNB_RESP_ANY
  1070. #define SLM_LLC_MISS (SLM_SNP_ANY|SNB_NON_DRAM)
  1071. static __initconst const u64 slm_hw_cache_extra_regs
  1072. [PERF_COUNT_HW_CACHE_MAX]
  1073. [PERF_COUNT_HW_CACHE_OP_MAX]
  1074. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  1075. {
  1076. [ C(LL ) ] = {
  1077. [ C(OP_READ) ] = {
  1078. [ C(RESULT_ACCESS) ] = SLM_DMND_READ|SLM_LLC_ACCESS,
  1079. [ C(RESULT_MISS) ] = 0,
  1080. },
  1081. [ C(OP_WRITE) ] = {
  1082. [ C(RESULT_ACCESS) ] = SLM_DMND_WRITE|SLM_LLC_ACCESS,
  1083. [ C(RESULT_MISS) ] = SLM_DMND_WRITE|SLM_LLC_MISS,
  1084. },
  1085. [ C(OP_PREFETCH) ] = {
  1086. [ C(RESULT_ACCESS) ] = SLM_DMND_PREFETCH|SLM_LLC_ACCESS,
  1087. [ C(RESULT_MISS) ] = SLM_DMND_PREFETCH|SLM_LLC_MISS,
  1088. },
  1089. },
  1090. };
  1091. static __initconst const u64 slm_hw_cache_event_ids
  1092. [PERF_COUNT_HW_CACHE_MAX]
  1093. [PERF_COUNT_HW_CACHE_OP_MAX]
  1094. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  1095. {
  1096. [ C(L1D) ] = {
  1097. [ C(OP_READ) ] = {
  1098. [ C(RESULT_ACCESS) ] = 0,
  1099. [ C(RESULT_MISS) ] = 0x0104, /* LD_DCU_MISS */
  1100. },
  1101. [ C(OP_WRITE) ] = {
  1102. [ C(RESULT_ACCESS) ] = 0,
  1103. [ C(RESULT_MISS) ] = 0,
  1104. },
  1105. [ C(OP_PREFETCH) ] = {
  1106. [ C(RESULT_ACCESS) ] = 0,
  1107. [ C(RESULT_MISS) ] = 0,
  1108. },
  1109. },
  1110. [ C(L1I ) ] = {
  1111. [ C(OP_READ) ] = {
  1112. [ C(RESULT_ACCESS) ] = 0x0380, /* ICACHE.ACCESSES */
  1113. [ C(RESULT_MISS) ] = 0x0280, /* ICACGE.MISSES */
  1114. },
  1115. [ C(OP_WRITE) ] = {
  1116. [ C(RESULT_ACCESS) ] = -1,
  1117. [ C(RESULT_MISS) ] = -1,
  1118. },
  1119. [ C(OP_PREFETCH) ] = {
  1120. [ C(RESULT_ACCESS) ] = 0,
  1121. [ C(RESULT_MISS) ] = 0,
  1122. },
  1123. },
  1124. [ C(LL ) ] = {
  1125. [ C(OP_READ) ] = {
  1126. /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
  1127. [ C(RESULT_ACCESS) ] = 0x01b7,
  1128. [ C(RESULT_MISS) ] = 0,
  1129. },
  1130. [ C(OP_WRITE) ] = {
  1131. /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
  1132. [ C(RESULT_ACCESS) ] = 0x01b7,
  1133. /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
  1134. [ C(RESULT_MISS) ] = 0x01b7,
  1135. },
  1136. [ C(OP_PREFETCH) ] = {
  1137. /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
  1138. [ C(RESULT_ACCESS) ] = 0x01b7,
  1139. /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
  1140. [ C(RESULT_MISS) ] = 0x01b7,
  1141. },
  1142. },
  1143. [ C(DTLB) ] = {
  1144. [ C(OP_READ) ] = {
  1145. [ C(RESULT_ACCESS) ] = 0,
  1146. [ C(RESULT_MISS) ] = 0x0804, /* LD_DTLB_MISS */
  1147. },
  1148. [ C(OP_WRITE) ] = {
  1149. [ C(RESULT_ACCESS) ] = 0,
  1150. [ C(RESULT_MISS) ] = 0,
  1151. },
  1152. [ C(OP_PREFETCH) ] = {
  1153. [ C(RESULT_ACCESS) ] = 0,
  1154. [ C(RESULT_MISS) ] = 0,
  1155. },
  1156. },
  1157. [ C(ITLB) ] = {
  1158. [ C(OP_READ) ] = {
  1159. [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
  1160. [ C(RESULT_MISS) ] = 0x40205, /* PAGE_WALKS.I_SIDE_WALKS */
  1161. },
  1162. [ C(OP_WRITE) ] = {
  1163. [ C(RESULT_ACCESS) ] = -1,
  1164. [ C(RESULT_MISS) ] = -1,
  1165. },
  1166. [ C(OP_PREFETCH) ] = {
  1167. [ C(RESULT_ACCESS) ] = -1,
  1168. [ C(RESULT_MISS) ] = -1,
  1169. },
  1170. },
  1171. [ C(BPU ) ] = {
  1172. [ C(OP_READ) ] = {
  1173. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
  1174. [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
  1175. },
  1176. [ C(OP_WRITE) ] = {
  1177. [ C(RESULT_ACCESS) ] = -1,
  1178. [ C(RESULT_MISS) ] = -1,
  1179. },
  1180. [ C(OP_PREFETCH) ] = {
  1181. [ C(RESULT_ACCESS) ] = -1,
  1182. [ C(RESULT_MISS) ] = -1,
  1183. },
  1184. },
  1185. };
  1186. /*
  1187. * Use from PMIs where the LBRs are already disabled.
  1188. */
  1189. static void __intel_pmu_disable_all(void)
  1190. {
  1191. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1192. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  1193. if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask))
  1194. intel_pmu_disable_bts();
  1195. else
  1196. intel_bts_disable_local();
  1197. intel_pmu_pebs_disable_all();
  1198. }
  1199. static void intel_pmu_disable_all(void)
  1200. {
  1201. __intel_pmu_disable_all();
  1202. intel_pmu_lbr_disable_all();
  1203. }
  1204. static void __intel_pmu_enable_all(int added, bool pmi)
  1205. {
  1206. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1207. intel_pmu_pebs_enable_all();
  1208. intel_pmu_lbr_enable_all(pmi);
  1209. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL,
  1210. x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_guest_mask);
  1211. if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask)) {
  1212. struct perf_event *event =
  1213. cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
  1214. if (WARN_ON_ONCE(!event))
  1215. return;
  1216. intel_pmu_enable_bts(event->hw.config);
  1217. } else
  1218. intel_bts_enable_local();
  1219. }
  1220. static void intel_pmu_enable_all(int added)
  1221. {
  1222. __intel_pmu_enable_all(added, false);
  1223. }
  1224. /*
  1225. * Workaround for:
  1226. * Intel Errata AAK100 (model 26)
  1227. * Intel Errata AAP53 (model 30)
  1228. * Intel Errata BD53 (model 44)
  1229. *
  1230. * The official story:
  1231. * These chips need to be 'reset' when adding counters by programming the
  1232. * magic three (non-counting) events 0x4300B5, 0x4300D2, and 0x4300B1 either
  1233. * in sequence on the same PMC or on different PMCs.
  1234. *
  1235. * In practise it appears some of these events do in fact count, and
  1236. * we need to programm all 4 events.
  1237. */
  1238. static void intel_pmu_nhm_workaround(void)
  1239. {
  1240. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1241. static const unsigned long nhm_magic[4] = {
  1242. 0x4300B5,
  1243. 0x4300D2,
  1244. 0x4300B1,
  1245. 0x4300B1
  1246. };
  1247. struct perf_event *event;
  1248. int i;
  1249. /*
  1250. * The Errata requires below steps:
  1251. * 1) Clear MSR_IA32_PEBS_ENABLE and MSR_CORE_PERF_GLOBAL_CTRL;
  1252. * 2) Configure 4 PERFEVTSELx with the magic events and clear
  1253. * the corresponding PMCx;
  1254. * 3) set bit0~bit3 of MSR_CORE_PERF_GLOBAL_CTRL;
  1255. * 4) Clear MSR_CORE_PERF_GLOBAL_CTRL;
  1256. * 5) Clear 4 pairs of ERFEVTSELx and PMCx;
  1257. */
  1258. /*
  1259. * The real steps we choose are a little different from above.
  1260. * A) To reduce MSR operations, we don't run step 1) as they
  1261. * are already cleared before this function is called;
  1262. * B) Call x86_perf_event_update to save PMCx before configuring
  1263. * PERFEVTSELx with magic number;
  1264. * C) With step 5), we do clear only when the PERFEVTSELx is
  1265. * not used currently.
  1266. * D) Call x86_perf_event_set_period to restore PMCx;
  1267. */
  1268. /* We always operate 4 pairs of PERF Counters */
  1269. for (i = 0; i < 4; i++) {
  1270. event = cpuc->events[i];
  1271. if (event)
  1272. x86_perf_event_update(event);
  1273. }
  1274. for (i = 0; i < 4; i++) {
  1275. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + i, nhm_magic[i]);
  1276. wrmsrl(MSR_ARCH_PERFMON_PERFCTR0 + i, 0x0);
  1277. }
  1278. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0xf);
  1279. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0x0);
  1280. for (i = 0; i < 4; i++) {
  1281. event = cpuc->events[i];
  1282. if (event) {
  1283. x86_perf_event_set_period(event);
  1284. __x86_pmu_enable_event(&event->hw,
  1285. ARCH_PERFMON_EVENTSEL_ENABLE);
  1286. } else
  1287. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + i, 0x0);
  1288. }
  1289. }
  1290. static void intel_pmu_nhm_enable_all(int added)
  1291. {
  1292. if (added)
  1293. intel_pmu_nhm_workaround();
  1294. intel_pmu_enable_all(added);
  1295. }
  1296. static inline u64 intel_pmu_get_status(void)
  1297. {
  1298. u64 status;
  1299. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  1300. return status;
  1301. }
  1302. static inline void intel_pmu_ack_status(u64 ack)
  1303. {
  1304. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  1305. }
  1306. static void intel_pmu_disable_fixed(struct hw_perf_event *hwc)
  1307. {
  1308. int idx = hwc->idx - INTEL_PMC_IDX_FIXED;
  1309. u64 ctrl_val, mask;
  1310. mask = 0xfULL << (idx * 4);
  1311. rdmsrl(hwc->config_base, ctrl_val);
  1312. ctrl_val &= ~mask;
  1313. wrmsrl(hwc->config_base, ctrl_val);
  1314. }
  1315. static inline bool event_is_checkpointed(struct perf_event *event)
  1316. {
  1317. return (event->hw.config & HSW_IN_TX_CHECKPOINTED) != 0;
  1318. }
  1319. static void intel_pmu_disable_event(struct perf_event *event)
  1320. {
  1321. struct hw_perf_event *hwc = &event->hw;
  1322. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1323. if (unlikely(hwc->idx == INTEL_PMC_IDX_FIXED_BTS)) {
  1324. intel_pmu_disable_bts();
  1325. intel_pmu_drain_bts_buffer();
  1326. return;
  1327. }
  1328. cpuc->intel_ctrl_guest_mask &= ~(1ull << hwc->idx);
  1329. cpuc->intel_ctrl_host_mask &= ~(1ull << hwc->idx);
  1330. cpuc->intel_cp_status &= ~(1ull << hwc->idx);
  1331. /*
  1332. * must disable before any actual event
  1333. * because any event may be combined with LBR
  1334. */
  1335. if (needs_branch_stack(event))
  1336. intel_pmu_lbr_disable(event);
  1337. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  1338. intel_pmu_disable_fixed(hwc);
  1339. return;
  1340. }
  1341. x86_pmu_disable_event(event);
  1342. if (unlikely(event->attr.precise_ip))
  1343. intel_pmu_pebs_disable(event);
  1344. }
  1345. static void intel_pmu_enable_fixed(struct hw_perf_event *hwc)
  1346. {
  1347. int idx = hwc->idx - INTEL_PMC_IDX_FIXED;
  1348. u64 ctrl_val, bits, mask;
  1349. /*
  1350. * Enable IRQ generation (0x8),
  1351. * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
  1352. * if requested:
  1353. */
  1354. bits = 0x8ULL;
  1355. if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
  1356. bits |= 0x2;
  1357. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  1358. bits |= 0x1;
  1359. /*
  1360. * ANY bit is supported in v3 and up
  1361. */
  1362. if (x86_pmu.version > 2 && hwc->config & ARCH_PERFMON_EVENTSEL_ANY)
  1363. bits |= 0x4;
  1364. bits <<= (idx * 4);
  1365. mask = 0xfULL << (idx * 4);
  1366. rdmsrl(hwc->config_base, ctrl_val);
  1367. ctrl_val &= ~mask;
  1368. ctrl_val |= bits;
  1369. wrmsrl(hwc->config_base, ctrl_val);
  1370. }
  1371. static void intel_pmu_enable_event(struct perf_event *event)
  1372. {
  1373. struct hw_perf_event *hwc = &event->hw;
  1374. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1375. if (unlikely(hwc->idx == INTEL_PMC_IDX_FIXED_BTS)) {
  1376. if (!__this_cpu_read(cpu_hw_events.enabled))
  1377. return;
  1378. intel_pmu_enable_bts(hwc->config);
  1379. return;
  1380. }
  1381. /*
  1382. * must enabled before any actual event
  1383. * because any event may be combined with LBR
  1384. */
  1385. if (needs_branch_stack(event))
  1386. intel_pmu_lbr_enable(event);
  1387. if (event->attr.exclude_host)
  1388. cpuc->intel_ctrl_guest_mask |= (1ull << hwc->idx);
  1389. if (event->attr.exclude_guest)
  1390. cpuc->intel_ctrl_host_mask |= (1ull << hwc->idx);
  1391. if (unlikely(event_is_checkpointed(event)))
  1392. cpuc->intel_cp_status |= (1ull << hwc->idx);
  1393. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  1394. intel_pmu_enable_fixed(hwc);
  1395. return;
  1396. }
  1397. if (unlikely(event->attr.precise_ip))
  1398. intel_pmu_pebs_enable(event);
  1399. __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
  1400. }
  1401. /*
  1402. * Save and restart an expired event. Called by NMI contexts,
  1403. * so it has to be careful about preempting normal event ops:
  1404. */
  1405. int intel_pmu_save_and_restart(struct perf_event *event)
  1406. {
  1407. x86_perf_event_update(event);
  1408. /*
  1409. * For a checkpointed counter always reset back to 0. This
  1410. * avoids a situation where the counter overflows, aborts the
  1411. * transaction and is then set back to shortly before the
  1412. * overflow, and overflows and aborts again.
  1413. */
  1414. if (unlikely(event_is_checkpointed(event))) {
  1415. /* No race with NMIs because the counter should not be armed */
  1416. wrmsrl(event->hw.event_base, 0);
  1417. local64_set(&event->hw.prev_count, 0);
  1418. }
  1419. return x86_perf_event_set_period(event);
  1420. }
  1421. static void intel_pmu_reset(void)
  1422. {
  1423. struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
  1424. unsigned long flags;
  1425. int idx;
  1426. if (!x86_pmu.num_counters)
  1427. return;
  1428. local_irq_save(flags);
  1429. pr_info("clearing PMU state on CPU#%d\n", smp_processor_id());
  1430. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  1431. wrmsrl_safe(x86_pmu_config_addr(idx), 0ull);
  1432. wrmsrl_safe(x86_pmu_event_addr(idx), 0ull);
  1433. }
  1434. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++)
  1435. wrmsrl_safe(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull);
  1436. if (ds)
  1437. ds->bts_index = ds->bts_buffer_base;
  1438. /* Ack all overflows and disable fixed counters */
  1439. if (x86_pmu.version >= 2) {
  1440. intel_pmu_ack_status(intel_pmu_get_status());
  1441. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  1442. }
  1443. /* Reset LBRs and LBR freezing */
  1444. if (x86_pmu.lbr_nr) {
  1445. update_debugctlmsr(get_debugctlmsr() &
  1446. ~(DEBUGCTLMSR_FREEZE_LBRS_ON_PMI|DEBUGCTLMSR_LBR));
  1447. }
  1448. local_irq_restore(flags);
  1449. }
  1450. /*
  1451. * This handler is triggered by the local APIC, so the APIC IRQ handling
  1452. * rules apply:
  1453. */
  1454. static int intel_pmu_handle_irq(struct pt_regs *regs)
  1455. {
  1456. struct perf_sample_data data;
  1457. struct cpu_hw_events *cpuc;
  1458. int bit, loops;
  1459. u64 status;
  1460. int handled;
  1461. cpuc = this_cpu_ptr(&cpu_hw_events);
  1462. /*
  1463. * No known reason to not always do late ACK,
  1464. * but just in case do it opt-in.
  1465. */
  1466. if (!x86_pmu.late_ack)
  1467. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1468. __intel_pmu_disable_all();
  1469. handled = intel_pmu_drain_bts_buffer();
  1470. handled += intel_bts_interrupt();
  1471. status = intel_pmu_get_status();
  1472. if (!status)
  1473. goto done;
  1474. loops = 0;
  1475. again:
  1476. intel_pmu_ack_status(status);
  1477. if (++loops > 100) {
  1478. static bool warned = false;
  1479. if (!warned) {
  1480. WARN(1, "perfevents: irq loop stuck!\n");
  1481. perf_event_print_debug();
  1482. warned = true;
  1483. }
  1484. intel_pmu_reset();
  1485. goto done;
  1486. }
  1487. inc_irq_stat(apic_perf_irqs);
  1488. intel_pmu_lbr_read();
  1489. /*
  1490. * CondChgd bit 63 doesn't mean any overflow status. Ignore
  1491. * and clear the bit.
  1492. */
  1493. if (__test_and_clear_bit(63, (unsigned long *)&status)) {
  1494. if (!status)
  1495. goto done;
  1496. }
  1497. /*
  1498. * PEBS overflow sets bit 62 in the global status register
  1499. */
  1500. if (__test_and_clear_bit(62, (unsigned long *)&status)) {
  1501. handled++;
  1502. x86_pmu.drain_pebs(regs);
  1503. }
  1504. /*
  1505. * Intel PT
  1506. */
  1507. if (__test_and_clear_bit(55, (unsigned long *)&status)) {
  1508. handled++;
  1509. intel_pt_interrupt();
  1510. }
  1511. /*
  1512. * Checkpointed counters can lead to 'spurious' PMIs because the
  1513. * rollback caused by the PMI will have cleared the overflow status
  1514. * bit. Therefore always force probe these counters.
  1515. */
  1516. status |= cpuc->intel_cp_status;
  1517. for_each_set_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  1518. struct perf_event *event = cpuc->events[bit];
  1519. handled++;
  1520. if (!test_bit(bit, cpuc->active_mask))
  1521. continue;
  1522. if (!intel_pmu_save_and_restart(event))
  1523. continue;
  1524. perf_sample_data_init(&data, 0, event->hw.last_period);
  1525. if (has_branch_stack(event))
  1526. data.br_stack = &cpuc->lbr_stack;
  1527. if (perf_event_overflow(event, &data, regs))
  1528. x86_pmu_stop(event, 0);
  1529. }
  1530. /*
  1531. * Repeat if there is more work to be done:
  1532. */
  1533. status = intel_pmu_get_status();
  1534. if (status)
  1535. goto again;
  1536. done:
  1537. __intel_pmu_enable_all(0, true);
  1538. /*
  1539. * Only unmask the NMI after the overflow counters
  1540. * have been reset. This avoids spurious NMIs on
  1541. * Haswell CPUs.
  1542. */
  1543. if (x86_pmu.late_ack)
  1544. apic_write(APIC_LVTPC, APIC_DM_NMI);
  1545. return handled;
  1546. }
  1547. static struct event_constraint *
  1548. intel_bts_constraints(struct perf_event *event)
  1549. {
  1550. struct hw_perf_event *hwc = &event->hw;
  1551. unsigned int hw_event, bts_event;
  1552. if (event->attr.freq)
  1553. return NULL;
  1554. hw_event = hwc->config & INTEL_ARCH_EVENT_MASK;
  1555. bts_event = x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS);
  1556. if (unlikely(hw_event == bts_event && hwc->sample_period == 1))
  1557. return &bts_constraint;
  1558. return NULL;
  1559. }
  1560. static int intel_alt_er(int idx, u64 config)
  1561. {
  1562. int alt_idx;
  1563. if (!(x86_pmu.flags & PMU_FL_HAS_RSP_1))
  1564. return idx;
  1565. if (idx == EXTRA_REG_RSP_0)
  1566. alt_idx = EXTRA_REG_RSP_1;
  1567. if (idx == EXTRA_REG_RSP_1)
  1568. alt_idx = EXTRA_REG_RSP_0;
  1569. if (config & ~x86_pmu.extra_regs[alt_idx].valid_mask)
  1570. return idx;
  1571. return alt_idx;
  1572. }
  1573. static void intel_fixup_er(struct perf_event *event, int idx)
  1574. {
  1575. event->hw.extra_reg.idx = idx;
  1576. if (idx == EXTRA_REG_RSP_0) {
  1577. event->hw.config &= ~INTEL_ARCH_EVENT_MASK;
  1578. event->hw.config |= x86_pmu.extra_regs[EXTRA_REG_RSP_0].event;
  1579. event->hw.extra_reg.reg = MSR_OFFCORE_RSP_0;
  1580. } else if (idx == EXTRA_REG_RSP_1) {
  1581. event->hw.config &= ~INTEL_ARCH_EVENT_MASK;
  1582. event->hw.config |= x86_pmu.extra_regs[EXTRA_REG_RSP_1].event;
  1583. event->hw.extra_reg.reg = MSR_OFFCORE_RSP_1;
  1584. }
  1585. }
  1586. /*
  1587. * manage allocation of shared extra msr for certain events
  1588. *
  1589. * sharing can be:
  1590. * per-cpu: to be shared between the various events on a single PMU
  1591. * per-core: per-cpu + shared by HT threads
  1592. */
  1593. static struct event_constraint *
  1594. __intel_shared_reg_get_constraints(struct cpu_hw_events *cpuc,
  1595. struct perf_event *event,
  1596. struct hw_perf_event_extra *reg)
  1597. {
  1598. struct event_constraint *c = &emptyconstraint;
  1599. struct er_account *era;
  1600. unsigned long flags;
  1601. int idx = reg->idx;
  1602. /*
  1603. * reg->alloc can be set due to existing state, so for fake cpuc we
  1604. * need to ignore this, otherwise we might fail to allocate proper fake
  1605. * state for this extra reg constraint. Also see the comment below.
  1606. */
  1607. if (reg->alloc && !cpuc->is_fake)
  1608. return NULL; /* call x86_get_event_constraint() */
  1609. again:
  1610. era = &cpuc->shared_regs->regs[idx];
  1611. /*
  1612. * we use spin_lock_irqsave() to avoid lockdep issues when
  1613. * passing a fake cpuc
  1614. */
  1615. raw_spin_lock_irqsave(&era->lock, flags);
  1616. if (!atomic_read(&era->ref) || era->config == reg->config) {
  1617. /*
  1618. * If its a fake cpuc -- as per validate_{group,event}() we
  1619. * shouldn't touch event state and we can avoid doing so
  1620. * since both will only call get_event_constraints() once
  1621. * on each event, this avoids the need for reg->alloc.
  1622. *
  1623. * Not doing the ER fixup will only result in era->reg being
  1624. * wrong, but since we won't actually try and program hardware
  1625. * this isn't a problem either.
  1626. */
  1627. if (!cpuc->is_fake) {
  1628. if (idx != reg->idx)
  1629. intel_fixup_er(event, idx);
  1630. /*
  1631. * x86_schedule_events() can call get_event_constraints()
  1632. * multiple times on events in the case of incremental
  1633. * scheduling(). reg->alloc ensures we only do the ER
  1634. * allocation once.
  1635. */
  1636. reg->alloc = 1;
  1637. }
  1638. /* lock in msr value */
  1639. era->config = reg->config;
  1640. era->reg = reg->reg;
  1641. /* one more user */
  1642. atomic_inc(&era->ref);
  1643. /*
  1644. * need to call x86_get_event_constraint()
  1645. * to check if associated event has constraints
  1646. */
  1647. c = NULL;
  1648. } else {
  1649. idx = intel_alt_er(idx, reg->config);
  1650. if (idx != reg->idx) {
  1651. raw_spin_unlock_irqrestore(&era->lock, flags);
  1652. goto again;
  1653. }
  1654. }
  1655. raw_spin_unlock_irqrestore(&era->lock, flags);
  1656. return c;
  1657. }
  1658. static void
  1659. __intel_shared_reg_put_constraints(struct cpu_hw_events *cpuc,
  1660. struct hw_perf_event_extra *reg)
  1661. {
  1662. struct er_account *era;
  1663. /*
  1664. * Only put constraint if extra reg was actually allocated. Also takes
  1665. * care of event which do not use an extra shared reg.
  1666. *
  1667. * Also, if this is a fake cpuc we shouldn't touch any event state
  1668. * (reg->alloc) and we don't care about leaving inconsistent cpuc state
  1669. * either since it'll be thrown out.
  1670. */
  1671. if (!reg->alloc || cpuc->is_fake)
  1672. return;
  1673. era = &cpuc->shared_regs->regs[reg->idx];
  1674. /* one fewer user */
  1675. atomic_dec(&era->ref);
  1676. /* allocate again next time */
  1677. reg->alloc = 0;
  1678. }
  1679. static struct event_constraint *
  1680. intel_shared_regs_constraints(struct cpu_hw_events *cpuc,
  1681. struct perf_event *event)
  1682. {
  1683. struct event_constraint *c = NULL, *d;
  1684. struct hw_perf_event_extra *xreg, *breg;
  1685. xreg = &event->hw.extra_reg;
  1686. if (xreg->idx != EXTRA_REG_NONE) {
  1687. c = __intel_shared_reg_get_constraints(cpuc, event, xreg);
  1688. if (c == &emptyconstraint)
  1689. return c;
  1690. }
  1691. breg = &event->hw.branch_reg;
  1692. if (breg->idx != EXTRA_REG_NONE) {
  1693. d = __intel_shared_reg_get_constraints(cpuc, event, breg);
  1694. if (d == &emptyconstraint) {
  1695. __intel_shared_reg_put_constraints(cpuc, xreg);
  1696. c = d;
  1697. }
  1698. }
  1699. return c;
  1700. }
  1701. struct event_constraint *
  1702. x86_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
  1703. struct perf_event *event)
  1704. {
  1705. struct event_constraint *c;
  1706. if (x86_pmu.event_constraints) {
  1707. for_each_event_constraint(c, x86_pmu.event_constraints) {
  1708. if ((event->hw.config & c->cmask) == c->code) {
  1709. event->hw.flags |= c->flags;
  1710. return c;
  1711. }
  1712. }
  1713. }
  1714. return &unconstrained;
  1715. }
  1716. static struct event_constraint *
  1717. __intel_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
  1718. struct perf_event *event)
  1719. {
  1720. struct event_constraint *c;
  1721. c = intel_bts_constraints(event);
  1722. if (c)
  1723. return c;
  1724. c = intel_shared_regs_constraints(cpuc, event);
  1725. if (c)
  1726. return c;
  1727. c = intel_pebs_constraints(event);
  1728. if (c)
  1729. return c;
  1730. return x86_get_event_constraints(cpuc, idx, event);
  1731. }
  1732. static void
  1733. intel_start_scheduling(struct cpu_hw_events *cpuc)
  1734. {
  1735. struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
  1736. struct intel_excl_states *xl;
  1737. int tid = cpuc->excl_thread_id;
  1738. /*
  1739. * nothing needed if in group validation mode
  1740. */
  1741. if (cpuc->is_fake || !is_ht_workaround_enabled())
  1742. return;
  1743. /*
  1744. * no exclusion needed
  1745. */
  1746. if (WARN_ON_ONCE(!excl_cntrs))
  1747. return;
  1748. xl = &excl_cntrs->states[tid];
  1749. xl->sched_started = true;
  1750. /*
  1751. * lock shared state until we are done scheduling
  1752. * in stop_event_scheduling()
  1753. * makes scheduling appear as a transaction
  1754. */
  1755. raw_spin_lock(&excl_cntrs->lock);
  1756. }
  1757. static void intel_commit_scheduling(struct cpu_hw_events *cpuc, int idx, int cntr)
  1758. {
  1759. struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
  1760. struct event_constraint *c = cpuc->event_constraint[idx];
  1761. struct intel_excl_states *xl;
  1762. int tid = cpuc->excl_thread_id;
  1763. if (cpuc->is_fake || !is_ht_workaround_enabled())
  1764. return;
  1765. if (WARN_ON_ONCE(!excl_cntrs))
  1766. return;
  1767. if (!(c->flags & PERF_X86_EVENT_DYNAMIC))
  1768. return;
  1769. xl = &excl_cntrs->states[tid];
  1770. lockdep_assert_held(&excl_cntrs->lock);
  1771. if (c->flags & PERF_X86_EVENT_EXCL)
  1772. xl->state[cntr] = INTEL_EXCL_EXCLUSIVE;
  1773. else
  1774. xl->state[cntr] = INTEL_EXCL_SHARED;
  1775. }
  1776. static void
  1777. intel_stop_scheduling(struct cpu_hw_events *cpuc)
  1778. {
  1779. struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
  1780. struct intel_excl_states *xl;
  1781. int tid = cpuc->excl_thread_id;
  1782. /*
  1783. * nothing needed if in group validation mode
  1784. */
  1785. if (cpuc->is_fake || !is_ht_workaround_enabled())
  1786. return;
  1787. /*
  1788. * no exclusion needed
  1789. */
  1790. if (WARN_ON_ONCE(!excl_cntrs))
  1791. return;
  1792. xl = &excl_cntrs->states[tid];
  1793. xl->sched_started = false;
  1794. /*
  1795. * release shared state lock (acquired in intel_start_scheduling())
  1796. */
  1797. raw_spin_unlock(&excl_cntrs->lock);
  1798. }
  1799. static struct event_constraint *
  1800. intel_get_excl_constraints(struct cpu_hw_events *cpuc, struct perf_event *event,
  1801. int idx, struct event_constraint *c)
  1802. {
  1803. struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
  1804. struct intel_excl_states *xlo;
  1805. int tid = cpuc->excl_thread_id;
  1806. int is_excl, i;
  1807. /*
  1808. * validating a group does not require
  1809. * enforcing cross-thread exclusion
  1810. */
  1811. if (cpuc->is_fake || !is_ht_workaround_enabled())
  1812. return c;
  1813. /*
  1814. * no exclusion needed
  1815. */
  1816. if (WARN_ON_ONCE(!excl_cntrs))
  1817. return c;
  1818. /*
  1819. * because we modify the constraint, we need
  1820. * to make a copy. Static constraints come
  1821. * from static const tables.
  1822. *
  1823. * only needed when constraint has not yet
  1824. * been cloned (marked dynamic)
  1825. */
  1826. if (!(c->flags & PERF_X86_EVENT_DYNAMIC)) {
  1827. struct event_constraint *cx;
  1828. /*
  1829. * grab pre-allocated constraint entry
  1830. */
  1831. cx = &cpuc->constraint_list[idx];
  1832. /*
  1833. * initialize dynamic constraint
  1834. * with static constraint
  1835. */
  1836. *cx = *c;
  1837. /*
  1838. * mark constraint as dynamic, so we
  1839. * can free it later on
  1840. */
  1841. cx->flags |= PERF_X86_EVENT_DYNAMIC;
  1842. c = cx;
  1843. }
  1844. /*
  1845. * From here on, the constraint is dynamic.
  1846. * Either it was just allocated above, or it
  1847. * was allocated during a earlier invocation
  1848. * of this function
  1849. */
  1850. /*
  1851. * state of sibling HT
  1852. */
  1853. xlo = &excl_cntrs->states[tid ^ 1];
  1854. /*
  1855. * event requires exclusive counter access
  1856. * across HT threads
  1857. */
  1858. is_excl = c->flags & PERF_X86_EVENT_EXCL;
  1859. if (is_excl && !(event->hw.flags & PERF_X86_EVENT_EXCL_ACCT)) {
  1860. event->hw.flags |= PERF_X86_EVENT_EXCL_ACCT;
  1861. if (!cpuc->n_excl++)
  1862. WRITE_ONCE(excl_cntrs->has_exclusive[tid], 1);
  1863. }
  1864. /*
  1865. * Modify static constraint with current dynamic
  1866. * state of thread
  1867. *
  1868. * EXCLUSIVE: sibling counter measuring exclusive event
  1869. * SHARED : sibling counter measuring non-exclusive event
  1870. * UNUSED : sibling counter unused
  1871. */
  1872. for_each_set_bit(i, c->idxmsk, X86_PMC_IDX_MAX) {
  1873. /*
  1874. * exclusive event in sibling counter
  1875. * our corresponding counter cannot be used
  1876. * regardless of our event
  1877. */
  1878. if (xlo->state[i] == INTEL_EXCL_EXCLUSIVE)
  1879. __clear_bit(i, c->idxmsk);
  1880. /*
  1881. * if measuring an exclusive event, sibling
  1882. * measuring non-exclusive, then counter cannot
  1883. * be used
  1884. */
  1885. if (is_excl && xlo->state[i] == INTEL_EXCL_SHARED)
  1886. __clear_bit(i, c->idxmsk);
  1887. }
  1888. /*
  1889. * recompute actual bit weight for scheduling algorithm
  1890. */
  1891. c->weight = hweight64(c->idxmsk64);
  1892. /*
  1893. * if we return an empty mask, then switch
  1894. * back to static empty constraint to avoid
  1895. * the cost of freeing later on
  1896. */
  1897. if (c->weight == 0)
  1898. c = &emptyconstraint;
  1899. return c;
  1900. }
  1901. static struct event_constraint *
  1902. intel_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
  1903. struct perf_event *event)
  1904. {
  1905. struct event_constraint *c1 = cpuc->event_constraint[idx];
  1906. struct event_constraint *c2;
  1907. /*
  1908. * first time only
  1909. * - static constraint: no change across incremental scheduling calls
  1910. * - dynamic constraint: handled by intel_get_excl_constraints()
  1911. */
  1912. c2 = __intel_get_event_constraints(cpuc, idx, event);
  1913. if (c1 && (c1->flags & PERF_X86_EVENT_DYNAMIC)) {
  1914. bitmap_copy(c1->idxmsk, c2->idxmsk, X86_PMC_IDX_MAX);
  1915. c1->weight = c2->weight;
  1916. c2 = c1;
  1917. }
  1918. if (cpuc->excl_cntrs)
  1919. return intel_get_excl_constraints(cpuc, event, idx, c2);
  1920. return c2;
  1921. }
  1922. static void intel_put_excl_constraints(struct cpu_hw_events *cpuc,
  1923. struct perf_event *event)
  1924. {
  1925. struct hw_perf_event *hwc = &event->hw;
  1926. struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
  1927. int tid = cpuc->excl_thread_id;
  1928. struct intel_excl_states *xl;
  1929. /*
  1930. * nothing needed if in group validation mode
  1931. */
  1932. if (cpuc->is_fake)
  1933. return;
  1934. if (WARN_ON_ONCE(!excl_cntrs))
  1935. return;
  1936. if (hwc->flags & PERF_X86_EVENT_EXCL_ACCT) {
  1937. hwc->flags &= ~PERF_X86_EVENT_EXCL_ACCT;
  1938. if (!--cpuc->n_excl)
  1939. WRITE_ONCE(excl_cntrs->has_exclusive[tid], 0);
  1940. }
  1941. /*
  1942. * If event was actually assigned, then mark the counter state as
  1943. * unused now.
  1944. */
  1945. if (hwc->idx >= 0) {
  1946. xl = &excl_cntrs->states[tid];
  1947. /*
  1948. * put_constraint may be called from x86_schedule_events()
  1949. * which already has the lock held so here make locking
  1950. * conditional.
  1951. */
  1952. if (!xl->sched_started)
  1953. raw_spin_lock(&excl_cntrs->lock);
  1954. xl->state[hwc->idx] = INTEL_EXCL_UNUSED;
  1955. if (!xl->sched_started)
  1956. raw_spin_unlock(&excl_cntrs->lock);
  1957. }
  1958. }
  1959. static void
  1960. intel_put_shared_regs_event_constraints(struct cpu_hw_events *cpuc,
  1961. struct perf_event *event)
  1962. {
  1963. struct hw_perf_event_extra *reg;
  1964. reg = &event->hw.extra_reg;
  1965. if (reg->idx != EXTRA_REG_NONE)
  1966. __intel_shared_reg_put_constraints(cpuc, reg);
  1967. reg = &event->hw.branch_reg;
  1968. if (reg->idx != EXTRA_REG_NONE)
  1969. __intel_shared_reg_put_constraints(cpuc, reg);
  1970. }
  1971. static void intel_put_event_constraints(struct cpu_hw_events *cpuc,
  1972. struct perf_event *event)
  1973. {
  1974. intel_put_shared_regs_event_constraints(cpuc, event);
  1975. /*
  1976. * is PMU has exclusive counter restrictions, then
  1977. * all events are subject to and must call the
  1978. * put_excl_constraints() routine
  1979. */
  1980. if (cpuc->excl_cntrs)
  1981. intel_put_excl_constraints(cpuc, event);
  1982. }
  1983. static void intel_pebs_aliases_core2(struct perf_event *event)
  1984. {
  1985. if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
  1986. /*
  1987. * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
  1988. * (0x003c) so that we can use it with PEBS.
  1989. *
  1990. * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
  1991. * PEBS capable. However we can use INST_RETIRED.ANY_P
  1992. * (0x00c0), which is a PEBS capable event, to get the same
  1993. * count.
  1994. *
  1995. * INST_RETIRED.ANY_P counts the number of cycles that retires
  1996. * CNTMASK instructions. By setting CNTMASK to a value (16)
  1997. * larger than the maximum number of instructions that can be
  1998. * retired per cycle (4) and then inverting the condition, we
  1999. * count all cycles that retire 16 or less instructions, which
  2000. * is every cycle.
  2001. *
  2002. * Thereby we gain a PEBS capable cycle counter.
  2003. */
  2004. u64 alt_config = X86_CONFIG(.event=0xc0, .inv=1, .cmask=16);
  2005. alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
  2006. event->hw.config = alt_config;
  2007. }
  2008. }
  2009. static void intel_pebs_aliases_snb(struct perf_event *event)
  2010. {
  2011. if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
  2012. /*
  2013. * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
  2014. * (0x003c) so that we can use it with PEBS.
  2015. *
  2016. * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
  2017. * PEBS capable. However we can use UOPS_RETIRED.ALL
  2018. * (0x01c2), which is a PEBS capable event, to get the same
  2019. * count.
  2020. *
  2021. * UOPS_RETIRED.ALL counts the number of cycles that retires
  2022. * CNTMASK micro-ops. By setting CNTMASK to a value (16)
  2023. * larger than the maximum number of micro-ops that can be
  2024. * retired per cycle (4) and then inverting the condition, we
  2025. * count all cycles that retire 16 or less micro-ops, which
  2026. * is every cycle.
  2027. *
  2028. * Thereby we gain a PEBS capable cycle counter.
  2029. */
  2030. u64 alt_config = X86_CONFIG(.event=0xc2, .umask=0x01, .inv=1, .cmask=16);
  2031. alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
  2032. event->hw.config = alt_config;
  2033. }
  2034. }
  2035. static unsigned long intel_pmu_free_running_flags(struct perf_event *event)
  2036. {
  2037. unsigned long flags = x86_pmu.free_running_flags;
  2038. if (event->attr.use_clockid)
  2039. flags &= ~PERF_SAMPLE_TIME;
  2040. return flags;
  2041. }
  2042. static int intel_pmu_hw_config(struct perf_event *event)
  2043. {
  2044. int ret = x86_pmu_hw_config(event);
  2045. if (ret)
  2046. return ret;
  2047. if (event->attr.precise_ip) {
  2048. if (!event->attr.freq) {
  2049. event->hw.flags |= PERF_X86_EVENT_AUTO_RELOAD;
  2050. if (!(event->attr.sample_type &
  2051. ~intel_pmu_free_running_flags(event)))
  2052. event->hw.flags |= PERF_X86_EVENT_FREERUNNING;
  2053. }
  2054. if (x86_pmu.pebs_aliases)
  2055. x86_pmu.pebs_aliases(event);
  2056. }
  2057. if (needs_branch_stack(event)) {
  2058. ret = intel_pmu_setup_lbr_filter(event);
  2059. if (ret)
  2060. return ret;
  2061. /*
  2062. * BTS is set up earlier in this path, so don't account twice
  2063. */
  2064. if (!intel_pmu_has_bts(event)) {
  2065. /* disallow lbr if conflicting events are present */
  2066. if (x86_add_exclusive(x86_lbr_exclusive_lbr))
  2067. return -EBUSY;
  2068. event->destroy = hw_perf_lbr_event_destroy;
  2069. }
  2070. }
  2071. if (event->attr.type != PERF_TYPE_RAW)
  2072. return 0;
  2073. if (!(event->attr.config & ARCH_PERFMON_EVENTSEL_ANY))
  2074. return 0;
  2075. if (x86_pmu.version < 3)
  2076. return -EINVAL;
  2077. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  2078. return -EACCES;
  2079. event->hw.config |= ARCH_PERFMON_EVENTSEL_ANY;
  2080. return 0;
  2081. }
  2082. struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr)
  2083. {
  2084. if (x86_pmu.guest_get_msrs)
  2085. return x86_pmu.guest_get_msrs(nr);
  2086. *nr = 0;
  2087. return NULL;
  2088. }
  2089. EXPORT_SYMBOL_GPL(perf_guest_get_msrs);
  2090. static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr)
  2091. {
  2092. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  2093. struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;
  2094. arr[0].msr = MSR_CORE_PERF_GLOBAL_CTRL;
  2095. arr[0].host = x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_guest_mask;
  2096. arr[0].guest = x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_host_mask;
  2097. /*
  2098. * If PMU counter has PEBS enabled it is not enough to disable counter
  2099. * on a guest entry since PEBS memory write can overshoot guest entry
  2100. * and corrupt guest memory. Disabling PEBS solves the problem.
  2101. */
  2102. arr[1].msr = MSR_IA32_PEBS_ENABLE;
  2103. arr[1].host = cpuc->pebs_enabled;
  2104. arr[1].guest = 0;
  2105. *nr = 2;
  2106. return arr;
  2107. }
  2108. static struct perf_guest_switch_msr *core_guest_get_msrs(int *nr)
  2109. {
  2110. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  2111. struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;
  2112. int idx;
  2113. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  2114. struct perf_event *event = cpuc->events[idx];
  2115. arr[idx].msr = x86_pmu_config_addr(idx);
  2116. arr[idx].host = arr[idx].guest = 0;
  2117. if (!test_bit(idx, cpuc->active_mask))
  2118. continue;
  2119. arr[idx].host = arr[idx].guest =
  2120. event->hw.config | ARCH_PERFMON_EVENTSEL_ENABLE;
  2121. if (event->attr.exclude_host)
  2122. arr[idx].host &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  2123. else if (event->attr.exclude_guest)
  2124. arr[idx].guest &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  2125. }
  2126. *nr = x86_pmu.num_counters;
  2127. return arr;
  2128. }
  2129. static void core_pmu_enable_event(struct perf_event *event)
  2130. {
  2131. if (!event->attr.exclude_host)
  2132. x86_pmu_enable_event(event);
  2133. }
  2134. static void core_pmu_enable_all(int added)
  2135. {
  2136. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  2137. int idx;
  2138. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  2139. struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
  2140. if (!test_bit(idx, cpuc->active_mask) ||
  2141. cpuc->events[idx]->attr.exclude_host)
  2142. continue;
  2143. __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
  2144. }
  2145. }
  2146. static int hsw_hw_config(struct perf_event *event)
  2147. {
  2148. int ret = intel_pmu_hw_config(event);
  2149. if (ret)
  2150. return ret;
  2151. if (!boot_cpu_has(X86_FEATURE_RTM) && !boot_cpu_has(X86_FEATURE_HLE))
  2152. return 0;
  2153. event->hw.config |= event->attr.config & (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED);
  2154. /*
  2155. * IN_TX/IN_TX-CP filters are not supported by the Haswell PMU with
  2156. * PEBS or in ANY thread mode. Since the results are non-sensical forbid
  2157. * this combination.
  2158. */
  2159. if ((event->hw.config & (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED)) &&
  2160. ((event->hw.config & ARCH_PERFMON_EVENTSEL_ANY) ||
  2161. event->attr.precise_ip > 0))
  2162. return -EOPNOTSUPP;
  2163. if (event_is_checkpointed(event)) {
  2164. /*
  2165. * Sampling of checkpointed events can cause situations where
  2166. * the CPU constantly aborts because of a overflow, which is
  2167. * then checkpointed back and ignored. Forbid checkpointing
  2168. * for sampling.
  2169. *
  2170. * But still allow a long sampling period, so that perf stat
  2171. * from KVM works.
  2172. */
  2173. if (event->attr.sample_period > 0 &&
  2174. event->attr.sample_period < 0x7fffffff)
  2175. return -EOPNOTSUPP;
  2176. }
  2177. return 0;
  2178. }
  2179. static struct event_constraint counter2_constraint =
  2180. EVENT_CONSTRAINT(0, 0x4, 0);
  2181. static struct event_constraint *
  2182. hsw_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
  2183. struct perf_event *event)
  2184. {
  2185. struct event_constraint *c;
  2186. c = intel_get_event_constraints(cpuc, idx, event);
  2187. /* Handle special quirk on in_tx_checkpointed only in counter 2 */
  2188. if (event->hw.config & HSW_IN_TX_CHECKPOINTED) {
  2189. if (c->idxmsk64 & (1U << 2))
  2190. return &counter2_constraint;
  2191. return &emptyconstraint;
  2192. }
  2193. return c;
  2194. }
  2195. /*
  2196. * Broadwell:
  2197. *
  2198. * The INST_RETIRED.ALL period always needs to have lowest 6 bits cleared
  2199. * (BDM55) and it must not use a period smaller than 100 (BDM11). We combine
  2200. * the two to enforce a minimum period of 128 (the smallest value that has bits
  2201. * 0-5 cleared and >= 100).
  2202. *
  2203. * Because of how the code in x86_perf_event_set_period() works, the truncation
  2204. * of the lower 6 bits is 'harmless' as we'll occasionally add a longer period
  2205. * to make up for the 'lost' events due to carrying the 'error' in period_left.
  2206. *
  2207. * Therefore the effective (average) period matches the requested period,
  2208. * despite coarser hardware granularity.
  2209. */
  2210. static unsigned bdw_limit_period(struct perf_event *event, unsigned left)
  2211. {
  2212. if ((event->hw.config & INTEL_ARCH_EVENT_MASK) ==
  2213. X86_CONFIG(.event=0xc0, .umask=0x01)) {
  2214. if (left < 128)
  2215. left = 128;
  2216. left &= ~0x3fu;
  2217. }
  2218. return left;
  2219. }
  2220. PMU_FORMAT_ATTR(event, "config:0-7" );
  2221. PMU_FORMAT_ATTR(umask, "config:8-15" );
  2222. PMU_FORMAT_ATTR(edge, "config:18" );
  2223. PMU_FORMAT_ATTR(pc, "config:19" );
  2224. PMU_FORMAT_ATTR(any, "config:21" ); /* v3 + */
  2225. PMU_FORMAT_ATTR(inv, "config:23" );
  2226. PMU_FORMAT_ATTR(cmask, "config:24-31" );
  2227. PMU_FORMAT_ATTR(in_tx, "config:32");
  2228. PMU_FORMAT_ATTR(in_tx_cp, "config:33");
  2229. static struct attribute *intel_arch_formats_attr[] = {
  2230. &format_attr_event.attr,
  2231. &format_attr_umask.attr,
  2232. &format_attr_edge.attr,
  2233. &format_attr_pc.attr,
  2234. &format_attr_inv.attr,
  2235. &format_attr_cmask.attr,
  2236. NULL,
  2237. };
  2238. ssize_t intel_event_sysfs_show(char *page, u64 config)
  2239. {
  2240. u64 event = (config & ARCH_PERFMON_EVENTSEL_EVENT);
  2241. return x86_event_sysfs_show(page, config, event);
  2242. }
  2243. struct intel_shared_regs *allocate_shared_regs(int cpu)
  2244. {
  2245. struct intel_shared_regs *regs;
  2246. int i;
  2247. regs = kzalloc_node(sizeof(struct intel_shared_regs),
  2248. GFP_KERNEL, cpu_to_node(cpu));
  2249. if (regs) {
  2250. /*
  2251. * initialize the locks to keep lockdep happy
  2252. */
  2253. for (i = 0; i < EXTRA_REG_MAX; i++)
  2254. raw_spin_lock_init(&regs->regs[i].lock);
  2255. regs->core_id = -1;
  2256. }
  2257. return regs;
  2258. }
  2259. static struct intel_excl_cntrs *allocate_excl_cntrs(int cpu)
  2260. {
  2261. struct intel_excl_cntrs *c;
  2262. c = kzalloc_node(sizeof(struct intel_excl_cntrs),
  2263. GFP_KERNEL, cpu_to_node(cpu));
  2264. if (c) {
  2265. raw_spin_lock_init(&c->lock);
  2266. c->core_id = -1;
  2267. }
  2268. return c;
  2269. }
  2270. static int intel_pmu_cpu_prepare(int cpu)
  2271. {
  2272. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  2273. if (x86_pmu.extra_regs || x86_pmu.lbr_sel_map) {
  2274. cpuc->shared_regs = allocate_shared_regs(cpu);
  2275. if (!cpuc->shared_regs)
  2276. return NOTIFY_BAD;
  2277. }
  2278. if (x86_pmu.flags & PMU_FL_EXCL_CNTRS) {
  2279. size_t sz = X86_PMC_IDX_MAX * sizeof(struct event_constraint);
  2280. cpuc->constraint_list = kzalloc(sz, GFP_KERNEL);
  2281. if (!cpuc->constraint_list)
  2282. return NOTIFY_BAD;
  2283. cpuc->excl_cntrs = allocate_excl_cntrs(cpu);
  2284. if (!cpuc->excl_cntrs) {
  2285. kfree(cpuc->constraint_list);
  2286. kfree(cpuc->shared_regs);
  2287. return NOTIFY_BAD;
  2288. }
  2289. cpuc->excl_thread_id = 0;
  2290. }
  2291. return NOTIFY_OK;
  2292. }
  2293. static void intel_pmu_cpu_starting(int cpu)
  2294. {
  2295. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  2296. int core_id = topology_core_id(cpu);
  2297. int i;
  2298. init_debug_store_on_cpu(cpu);
  2299. /*
  2300. * Deal with CPUs that don't clear their LBRs on power-up.
  2301. */
  2302. intel_pmu_lbr_reset();
  2303. cpuc->lbr_sel = NULL;
  2304. if (!cpuc->shared_regs)
  2305. return;
  2306. if (!(x86_pmu.flags & PMU_FL_NO_HT_SHARING)) {
  2307. void **onln = &cpuc->kfree_on_online[X86_PERF_KFREE_SHARED];
  2308. for_each_cpu(i, topology_sibling_cpumask(cpu)) {
  2309. struct intel_shared_regs *pc;
  2310. pc = per_cpu(cpu_hw_events, i).shared_regs;
  2311. if (pc && pc->core_id == core_id) {
  2312. *onln = cpuc->shared_regs;
  2313. cpuc->shared_regs = pc;
  2314. break;
  2315. }
  2316. }
  2317. cpuc->shared_regs->core_id = core_id;
  2318. cpuc->shared_regs->refcnt++;
  2319. }
  2320. if (x86_pmu.lbr_sel_map)
  2321. cpuc->lbr_sel = &cpuc->shared_regs->regs[EXTRA_REG_LBR];
  2322. if (x86_pmu.flags & PMU_FL_EXCL_CNTRS) {
  2323. for_each_cpu(i, topology_sibling_cpumask(cpu)) {
  2324. struct intel_excl_cntrs *c;
  2325. c = per_cpu(cpu_hw_events, i).excl_cntrs;
  2326. if (c && c->core_id == core_id) {
  2327. cpuc->kfree_on_online[1] = cpuc->excl_cntrs;
  2328. cpuc->excl_cntrs = c;
  2329. cpuc->excl_thread_id = 1;
  2330. break;
  2331. }
  2332. }
  2333. cpuc->excl_cntrs->core_id = core_id;
  2334. cpuc->excl_cntrs->refcnt++;
  2335. }
  2336. }
  2337. static void free_excl_cntrs(int cpu)
  2338. {
  2339. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  2340. struct intel_excl_cntrs *c;
  2341. c = cpuc->excl_cntrs;
  2342. if (c) {
  2343. if (c->core_id == -1 || --c->refcnt == 0)
  2344. kfree(c);
  2345. cpuc->excl_cntrs = NULL;
  2346. kfree(cpuc->constraint_list);
  2347. cpuc->constraint_list = NULL;
  2348. }
  2349. }
  2350. static void intel_pmu_cpu_dying(int cpu)
  2351. {
  2352. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  2353. struct intel_shared_regs *pc;
  2354. pc = cpuc->shared_regs;
  2355. if (pc) {
  2356. if (pc->core_id == -1 || --pc->refcnt == 0)
  2357. kfree(pc);
  2358. cpuc->shared_regs = NULL;
  2359. }
  2360. free_excl_cntrs(cpu);
  2361. fini_debug_store_on_cpu(cpu);
  2362. }
  2363. static void intel_pmu_sched_task(struct perf_event_context *ctx,
  2364. bool sched_in)
  2365. {
  2366. if (x86_pmu.pebs_active)
  2367. intel_pmu_pebs_sched_task(ctx, sched_in);
  2368. if (x86_pmu.lbr_nr)
  2369. intel_pmu_lbr_sched_task(ctx, sched_in);
  2370. }
  2371. PMU_FORMAT_ATTR(offcore_rsp, "config1:0-63");
  2372. PMU_FORMAT_ATTR(ldlat, "config1:0-15");
  2373. static struct attribute *intel_arch3_formats_attr[] = {
  2374. &format_attr_event.attr,
  2375. &format_attr_umask.attr,
  2376. &format_attr_edge.attr,
  2377. &format_attr_pc.attr,
  2378. &format_attr_any.attr,
  2379. &format_attr_inv.attr,
  2380. &format_attr_cmask.attr,
  2381. &format_attr_in_tx.attr,
  2382. &format_attr_in_tx_cp.attr,
  2383. &format_attr_offcore_rsp.attr, /* XXX do NHM/WSM + SNB breakout */
  2384. &format_attr_ldlat.attr, /* PEBS load latency */
  2385. NULL,
  2386. };
  2387. static __initconst const struct x86_pmu core_pmu = {
  2388. .name = "core",
  2389. .handle_irq = x86_pmu_handle_irq,
  2390. .disable_all = x86_pmu_disable_all,
  2391. .enable_all = core_pmu_enable_all,
  2392. .enable = core_pmu_enable_event,
  2393. .disable = x86_pmu_disable_event,
  2394. .hw_config = x86_pmu_hw_config,
  2395. .schedule_events = x86_schedule_events,
  2396. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  2397. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  2398. .event_map = intel_pmu_event_map,
  2399. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  2400. .apic = 1,
  2401. .free_running_flags = PEBS_FREERUNNING_FLAGS,
  2402. /*
  2403. * Intel PMCs cannot be accessed sanely above 32-bit width,
  2404. * so we install an artificial 1<<31 period regardless of
  2405. * the generic event period:
  2406. */
  2407. .max_period = (1ULL<<31) - 1,
  2408. .get_event_constraints = intel_get_event_constraints,
  2409. .put_event_constraints = intel_put_event_constraints,
  2410. .event_constraints = intel_core_event_constraints,
  2411. .guest_get_msrs = core_guest_get_msrs,
  2412. .format_attrs = intel_arch_formats_attr,
  2413. .events_sysfs_show = intel_event_sysfs_show,
  2414. /*
  2415. * Virtual (or funny metal) CPU can define x86_pmu.extra_regs
  2416. * together with PMU version 1 and thus be using core_pmu with
  2417. * shared_regs. We need following callbacks here to allocate
  2418. * it properly.
  2419. */
  2420. .cpu_prepare = intel_pmu_cpu_prepare,
  2421. .cpu_starting = intel_pmu_cpu_starting,
  2422. .cpu_dying = intel_pmu_cpu_dying,
  2423. };
  2424. static __initconst const struct x86_pmu intel_pmu = {
  2425. .name = "Intel",
  2426. .handle_irq = intel_pmu_handle_irq,
  2427. .disable_all = intel_pmu_disable_all,
  2428. .enable_all = intel_pmu_enable_all,
  2429. .enable = intel_pmu_enable_event,
  2430. .disable = intel_pmu_disable_event,
  2431. .hw_config = intel_pmu_hw_config,
  2432. .schedule_events = x86_schedule_events,
  2433. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  2434. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  2435. .event_map = intel_pmu_event_map,
  2436. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  2437. .apic = 1,
  2438. .free_running_flags = PEBS_FREERUNNING_FLAGS,
  2439. /*
  2440. * Intel PMCs cannot be accessed sanely above 32 bit width,
  2441. * so we install an artificial 1<<31 period regardless of
  2442. * the generic event period:
  2443. */
  2444. .max_period = (1ULL << 31) - 1,
  2445. .get_event_constraints = intel_get_event_constraints,
  2446. .put_event_constraints = intel_put_event_constraints,
  2447. .pebs_aliases = intel_pebs_aliases_core2,
  2448. .format_attrs = intel_arch3_formats_attr,
  2449. .events_sysfs_show = intel_event_sysfs_show,
  2450. .cpu_prepare = intel_pmu_cpu_prepare,
  2451. .cpu_starting = intel_pmu_cpu_starting,
  2452. .cpu_dying = intel_pmu_cpu_dying,
  2453. .guest_get_msrs = intel_guest_get_msrs,
  2454. .sched_task = intel_pmu_sched_task,
  2455. };
  2456. static __init void intel_clovertown_quirk(void)
  2457. {
  2458. /*
  2459. * PEBS is unreliable due to:
  2460. *
  2461. * AJ67 - PEBS may experience CPL leaks
  2462. * AJ68 - PEBS PMI may be delayed by one event
  2463. * AJ69 - GLOBAL_STATUS[62] will only be set when DEBUGCTL[12]
  2464. * AJ106 - FREEZE_LBRS_ON_PMI doesn't work in combination with PEBS
  2465. *
  2466. * AJ67 could be worked around by restricting the OS/USR flags.
  2467. * AJ69 could be worked around by setting PMU_FREEZE_ON_PMI.
  2468. *
  2469. * AJ106 could possibly be worked around by not allowing LBR
  2470. * usage from PEBS, including the fixup.
  2471. * AJ68 could possibly be worked around by always programming
  2472. * a pebs_event_reset[0] value and coping with the lost events.
  2473. *
  2474. * But taken together it might just make sense to not enable PEBS on
  2475. * these chips.
  2476. */
  2477. pr_warn("PEBS disabled due to CPU errata\n");
  2478. x86_pmu.pebs = 0;
  2479. x86_pmu.pebs_constraints = NULL;
  2480. }
  2481. static int intel_snb_pebs_broken(int cpu)
  2482. {
  2483. u32 rev = UINT_MAX; /* default to broken for unknown models */
  2484. switch (cpu_data(cpu).x86_model) {
  2485. case 42: /* SNB */
  2486. rev = 0x28;
  2487. break;
  2488. case 45: /* SNB-EP */
  2489. switch (cpu_data(cpu).x86_mask) {
  2490. case 6: rev = 0x618; break;
  2491. case 7: rev = 0x70c; break;
  2492. }
  2493. }
  2494. return (cpu_data(cpu).microcode < rev);
  2495. }
  2496. static void intel_snb_check_microcode(void)
  2497. {
  2498. int pebs_broken = 0;
  2499. int cpu;
  2500. get_online_cpus();
  2501. for_each_online_cpu(cpu) {
  2502. if ((pebs_broken = intel_snb_pebs_broken(cpu)))
  2503. break;
  2504. }
  2505. put_online_cpus();
  2506. if (pebs_broken == x86_pmu.pebs_broken)
  2507. return;
  2508. /*
  2509. * Serialized by the microcode lock..
  2510. */
  2511. if (x86_pmu.pebs_broken) {
  2512. pr_info("PEBS enabled due to microcode update\n");
  2513. x86_pmu.pebs_broken = 0;
  2514. } else {
  2515. pr_info("PEBS disabled due to CPU errata, please upgrade microcode\n");
  2516. x86_pmu.pebs_broken = 1;
  2517. }
  2518. }
  2519. /*
  2520. * Under certain circumstances, access certain MSR may cause #GP.
  2521. * The function tests if the input MSR can be safely accessed.
  2522. */
  2523. static bool check_msr(unsigned long msr, u64 mask)
  2524. {
  2525. u64 val_old, val_new, val_tmp;
  2526. /*
  2527. * Read the current value, change it and read it back to see if it
  2528. * matches, this is needed to detect certain hardware emulators
  2529. * (qemu/kvm) that don't trap on the MSR access and always return 0s.
  2530. */
  2531. if (rdmsrl_safe(msr, &val_old))
  2532. return false;
  2533. /*
  2534. * Only change the bits which can be updated by wrmsrl.
  2535. */
  2536. val_tmp = val_old ^ mask;
  2537. if (wrmsrl_safe(msr, val_tmp) ||
  2538. rdmsrl_safe(msr, &val_new))
  2539. return false;
  2540. if (val_new != val_tmp)
  2541. return false;
  2542. /* Here it's sure that the MSR can be safely accessed.
  2543. * Restore the old value and return.
  2544. */
  2545. wrmsrl(msr, val_old);
  2546. return true;
  2547. }
  2548. static __init void intel_sandybridge_quirk(void)
  2549. {
  2550. x86_pmu.check_microcode = intel_snb_check_microcode;
  2551. intel_snb_check_microcode();
  2552. }
  2553. static const struct { int id; char *name; } intel_arch_events_map[] __initconst = {
  2554. { PERF_COUNT_HW_CPU_CYCLES, "cpu cycles" },
  2555. { PERF_COUNT_HW_INSTRUCTIONS, "instructions" },
  2556. { PERF_COUNT_HW_BUS_CYCLES, "bus cycles" },
  2557. { PERF_COUNT_HW_CACHE_REFERENCES, "cache references" },
  2558. { PERF_COUNT_HW_CACHE_MISSES, "cache misses" },
  2559. { PERF_COUNT_HW_BRANCH_INSTRUCTIONS, "branch instructions" },
  2560. { PERF_COUNT_HW_BRANCH_MISSES, "branch misses" },
  2561. };
  2562. static __init void intel_arch_events_quirk(void)
  2563. {
  2564. int bit;
  2565. /* disable event that reported as not presend by cpuid */
  2566. for_each_set_bit(bit, x86_pmu.events_mask, ARRAY_SIZE(intel_arch_events_map)) {
  2567. intel_perfmon_event_map[intel_arch_events_map[bit].id] = 0;
  2568. pr_warn("CPUID marked event: \'%s\' unavailable\n",
  2569. intel_arch_events_map[bit].name);
  2570. }
  2571. }
  2572. static __init void intel_nehalem_quirk(void)
  2573. {
  2574. union cpuid10_ebx ebx;
  2575. ebx.full = x86_pmu.events_maskl;
  2576. if (ebx.split.no_branch_misses_retired) {
  2577. /*
  2578. * Erratum AAJ80 detected, we work it around by using
  2579. * the BR_MISP_EXEC.ANY event. This will over-count
  2580. * branch-misses, but it's still much better than the
  2581. * architectural event which is often completely bogus:
  2582. */
  2583. intel_perfmon_event_map[PERF_COUNT_HW_BRANCH_MISSES] = 0x7f89;
  2584. ebx.split.no_branch_misses_retired = 0;
  2585. x86_pmu.events_maskl = ebx.full;
  2586. pr_info("CPU erratum AAJ80 worked around\n");
  2587. }
  2588. }
  2589. /*
  2590. * enable software workaround for errata:
  2591. * SNB: BJ122
  2592. * IVB: BV98
  2593. * HSW: HSD29
  2594. *
  2595. * Only needed when HT is enabled. However detecting
  2596. * if HT is enabled is difficult (model specific). So instead,
  2597. * we enable the workaround in the early boot, and verify if
  2598. * it is needed in a later initcall phase once we have valid
  2599. * topology information to check if HT is actually enabled
  2600. */
  2601. static __init void intel_ht_bug(void)
  2602. {
  2603. x86_pmu.flags |= PMU_FL_EXCL_CNTRS | PMU_FL_EXCL_ENABLED;
  2604. x86_pmu.start_scheduling = intel_start_scheduling;
  2605. x86_pmu.commit_scheduling = intel_commit_scheduling;
  2606. x86_pmu.stop_scheduling = intel_stop_scheduling;
  2607. }
  2608. EVENT_ATTR_STR(mem-loads, mem_ld_hsw, "event=0xcd,umask=0x1,ldlat=3");
  2609. EVENT_ATTR_STR(mem-stores, mem_st_hsw, "event=0xd0,umask=0x82")
  2610. /* Haswell special events */
  2611. EVENT_ATTR_STR(tx-start, tx_start, "event=0xc9,umask=0x1");
  2612. EVENT_ATTR_STR(tx-commit, tx_commit, "event=0xc9,umask=0x2");
  2613. EVENT_ATTR_STR(tx-abort, tx_abort, "event=0xc9,umask=0x4");
  2614. EVENT_ATTR_STR(tx-capacity, tx_capacity, "event=0x54,umask=0x2");
  2615. EVENT_ATTR_STR(tx-conflict, tx_conflict, "event=0x54,umask=0x1");
  2616. EVENT_ATTR_STR(el-start, el_start, "event=0xc8,umask=0x1");
  2617. EVENT_ATTR_STR(el-commit, el_commit, "event=0xc8,umask=0x2");
  2618. EVENT_ATTR_STR(el-abort, el_abort, "event=0xc8,umask=0x4");
  2619. EVENT_ATTR_STR(el-capacity, el_capacity, "event=0x54,umask=0x2");
  2620. EVENT_ATTR_STR(el-conflict, el_conflict, "event=0x54,umask=0x1");
  2621. EVENT_ATTR_STR(cycles-t, cycles_t, "event=0x3c,in_tx=1");
  2622. EVENT_ATTR_STR(cycles-ct, cycles_ct, "event=0x3c,in_tx=1,in_tx_cp=1");
  2623. static struct attribute *hsw_events_attrs[] = {
  2624. EVENT_PTR(tx_start),
  2625. EVENT_PTR(tx_commit),
  2626. EVENT_PTR(tx_abort),
  2627. EVENT_PTR(tx_capacity),
  2628. EVENT_PTR(tx_conflict),
  2629. EVENT_PTR(el_start),
  2630. EVENT_PTR(el_commit),
  2631. EVENT_PTR(el_abort),
  2632. EVENT_PTR(el_capacity),
  2633. EVENT_PTR(el_conflict),
  2634. EVENT_PTR(cycles_t),
  2635. EVENT_PTR(cycles_ct),
  2636. EVENT_PTR(mem_ld_hsw),
  2637. EVENT_PTR(mem_st_hsw),
  2638. NULL
  2639. };
  2640. __init int intel_pmu_init(void)
  2641. {
  2642. union cpuid10_edx edx;
  2643. union cpuid10_eax eax;
  2644. union cpuid10_ebx ebx;
  2645. struct event_constraint *c;
  2646. unsigned int unused;
  2647. struct extra_reg *er;
  2648. int version, i;
  2649. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
  2650. switch (boot_cpu_data.x86) {
  2651. case 0x6:
  2652. return p6_pmu_init();
  2653. case 0xb:
  2654. return knc_pmu_init();
  2655. case 0xf:
  2656. return p4_pmu_init();
  2657. }
  2658. return -ENODEV;
  2659. }
  2660. /*
  2661. * Check whether the Architectural PerfMon supports
  2662. * Branch Misses Retired hw_event or not.
  2663. */
  2664. cpuid(10, &eax.full, &ebx.full, &unused, &edx.full);
  2665. if (eax.split.mask_length < ARCH_PERFMON_EVENTS_COUNT)
  2666. return -ENODEV;
  2667. version = eax.split.version_id;
  2668. if (version < 2)
  2669. x86_pmu = core_pmu;
  2670. else
  2671. x86_pmu = intel_pmu;
  2672. x86_pmu.version = version;
  2673. x86_pmu.num_counters = eax.split.num_counters;
  2674. x86_pmu.cntval_bits = eax.split.bit_width;
  2675. x86_pmu.cntval_mask = (1ULL << eax.split.bit_width) - 1;
  2676. x86_pmu.events_maskl = ebx.full;
  2677. x86_pmu.events_mask_len = eax.split.mask_length;
  2678. x86_pmu.max_pebs_events = min_t(unsigned, MAX_PEBS_EVENTS, x86_pmu.num_counters);
  2679. /*
  2680. * Quirk: v2 perfmon does not report fixed-purpose events, so
  2681. * assume at least 3 events:
  2682. */
  2683. if (version > 1)
  2684. x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3);
  2685. if (boot_cpu_has(X86_FEATURE_PDCM)) {
  2686. u64 capabilities;
  2687. rdmsrl(MSR_IA32_PERF_CAPABILITIES, capabilities);
  2688. x86_pmu.intel_cap.capabilities = capabilities;
  2689. }
  2690. intel_ds_init();
  2691. x86_add_quirk(intel_arch_events_quirk); /* Install first, so it runs last */
  2692. /*
  2693. * Install the hw-cache-events table:
  2694. */
  2695. switch (boot_cpu_data.x86_model) {
  2696. case 14: /* 65nm Core "Yonah" */
  2697. pr_cont("Core events, ");
  2698. break;
  2699. case 15: /* 65nm Core2 "Merom" */
  2700. x86_add_quirk(intel_clovertown_quirk);
  2701. case 22: /* 65nm Core2 "Merom-L" */
  2702. case 23: /* 45nm Core2 "Penryn" */
  2703. case 29: /* 45nm Core2 "Dunnington (MP) */
  2704. memcpy(hw_cache_event_ids, core2_hw_cache_event_ids,
  2705. sizeof(hw_cache_event_ids));
  2706. intel_pmu_lbr_init_core();
  2707. x86_pmu.event_constraints = intel_core2_event_constraints;
  2708. x86_pmu.pebs_constraints = intel_core2_pebs_event_constraints;
  2709. pr_cont("Core2 events, ");
  2710. break;
  2711. case 30: /* 45nm Nehalem */
  2712. case 26: /* 45nm Nehalem-EP */
  2713. case 46: /* 45nm Nehalem-EX */
  2714. memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids,
  2715. sizeof(hw_cache_event_ids));
  2716. memcpy(hw_cache_extra_regs, nehalem_hw_cache_extra_regs,
  2717. sizeof(hw_cache_extra_regs));
  2718. intel_pmu_lbr_init_nhm();
  2719. x86_pmu.event_constraints = intel_nehalem_event_constraints;
  2720. x86_pmu.pebs_constraints = intel_nehalem_pebs_event_constraints;
  2721. x86_pmu.enable_all = intel_pmu_nhm_enable_all;
  2722. x86_pmu.extra_regs = intel_nehalem_extra_regs;
  2723. x86_pmu.cpu_events = nhm_events_attrs;
  2724. /* UOPS_ISSUED.STALLED_CYCLES */
  2725. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
  2726. X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
  2727. /* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */
  2728. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
  2729. X86_CONFIG(.event=0xb1, .umask=0x3f, .inv=1, .cmask=1);
  2730. x86_add_quirk(intel_nehalem_quirk);
  2731. pr_cont("Nehalem events, ");
  2732. break;
  2733. case 28: /* 45nm Atom "Pineview" */
  2734. case 38: /* 45nm Atom "Lincroft" */
  2735. case 39: /* 32nm Atom "Penwell" */
  2736. case 53: /* 32nm Atom "Cloverview" */
  2737. case 54: /* 32nm Atom "Cedarview" */
  2738. memcpy(hw_cache_event_ids, atom_hw_cache_event_ids,
  2739. sizeof(hw_cache_event_ids));
  2740. intel_pmu_lbr_init_atom();
  2741. x86_pmu.event_constraints = intel_gen_event_constraints;
  2742. x86_pmu.pebs_constraints = intel_atom_pebs_event_constraints;
  2743. pr_cont("Atom events, ");
  2744. break;
  2745. case 55: /* 22nm Atom "Silvermont" */
  2746. case 76: /* 14nm Atom "Airmont" */
  2747. case 77: /* 22nm Atom "Silvermont Avoton/Rangely" */
  2748. memcpy(hw_cache_event_ids, slm_hw_cache_event_ids,
  2749. sizeof(hw_cache_event_ids));
  2750. memcpy(hw_cache_extra_regs, slm_hw_cache_extra_regs,
  2751. sizeof(hw_cache_extra_regs));
  2752. intel_pmu_lbr_init_atom();
  2753. x86_pmu.event_constraints = intel_slm_event_constraints;
  2754. x86_pmu.pebs_constraints = intel_slm_pebs_event_constraints;
  2755. x86_pmu.extra_regs = intel_slm_extra_regs;
  2756. x86_pmu.flags |= PMU_FL_HAS_RSP_1;
  2757. pr_cont("Silvermont events, ");
  2758. break;
  2759. case 37: /* 32nm Westmere */
  2760. case 44: /* 32nm Westmere-EP */
  2761. case 47: /* 32nm Westmere-EX */
  2762. memcpy(hw_cache_event_ids, westmere_hw_cache_event_ids,
  2763. sizeof(hw_cache_event_ids));
  2764. memcpy(hw_cache_extra_regs, nehalem_hw_cache_extra_regs,
  2765. sizeof(hw_cache_extra_regs));
  2766. intel_pmu_lbr_init_nhm();
  2767. x86_pmu.event_constraints = intel_westmere_event_constraints;
  2768. x86_pmu.enable_all = intel_pmu_nhm_enable_all;
  2769. x86_pmu.pebs_constraints = intel_westmere_pebs_event_constraints;
  2770. x86_pmu.extra_regs = intel_westmere_extra_regs;
  2771. x86_pmu.flags |= PMU_FL_HAS_RSP_1;
  2772. x86_pmu.cpu_events = nhm_events_attrs;
  2773. /* UOPS_ISSUED.STALLED_CYCLES */
  2774. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
  2775. X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
  2776. /* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */
  2777. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
  2778. X86_CONFIG(.event=0xb1, .umask=0x3f, .inv=1, .cmask=1);
  2779. pr_cont("Westmere events, ");
  2780. break;
  2781. case 42: /* 32nm SandyBridge */
  2782. case 45: /* 32nm SandyBridge-E/EN/EP */
  2783. x86_add_quirk(intel_sandybridge_quirk);
  2784. x86_add_quirk(intel_ht_bug);
  2785. memcpy(hw_cache_event_ids, snb_hw_cache_event_ids,
  2786. sizeof(hw_cache_event_ids));
  2787. memcpy(hw_cache_extra_regs, snb_hw_cache_extra_regs,
  2788. sizeof(hw_cache_extra_regs));
  2789. intel_pmu_lbr_init_snb();
  2790. x86_pmu.event_constraints = intel_snb_event_constraints;
  2791. x86_pmu.pebs_constraints = intel_snb_pebs_event_constraints;
  2792. x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
  2793. if (boot_cpu_data.x86_model == 45)
  2794. x86_pmu.extra_regs = intel_snbep_extra_regs;
  2795. else
  2796. x86_pmu.extra_regs = intel_snb_extra_regs;
  2797. /* all extra regs are per-cpu when HT is on */
  2798. x86_pmu.flags |= PMU_FL_HAS_RSP_1;
  2799. x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
  2800. x86_pmu.cpu_events = snb_events_attrs;
  2801. /* UOPS_ISSUED.ANY,c=1,i=1 to count stall cycles */
  2802. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
  2803. X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
  2804. /* UOPS_DISPATCHED.THREAD,c=1,i=1 to count stall cycles*/
  2805. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
  2806. X86_CONFIG(.event=0xb1, .umask=0x01, .inv=1, .cmask=1);
  2807. pr_cont("SandyBridge events, ");
  2808. break;
  2809. case 58: /* 22nm IvyBridge */
  2810. case 62: /* 22nm IvyBridge-EP/EX */
  2811. x86_add_quirk(intel_ht_bug);
  2812. memcpy(hw_cache_event_ids, snb_hw_cache_event_ids,
  2813. sizeof(hw_cache_event_ids));
  2814. /* dTLB-load-misses on IVB is different than SNB */
  2815. hw_cache_event_ids[C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = 0x8108; /* DTLB_LOAD_MISSES.DEMAND_LD_MISS_CAUSES_A_WALK */
  2816. memcpy(hw_cache_extra_regs, snb_hw_cache_extra_regs,
  2817. sizeof(hw_cache_extra_regs));
  2818. intel_pmu_lbr_init_snb();
  2819. x86_pmu.event_constraints = intel_ivb_event_constraints;
  2820. x86_pmu.pebs_constraints = intel_ivb_pebs_event_constraints;
  2821. x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
  2822. if (boot_cpu_data.x86_model == 62)
  2823. x86_pmu.extra_regs = intel_snbep_extra_regs;
  2824. else
  2825. x86_pmu.extra_regs = intel_snb_extra_regs;
  2826. /* all extra regs are per-cpu when HT is on */
  2827. x86_pmu.flags |= PMU_FL_HAS_RSP_1;
  2828. x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
  2829. x86_pmu.cpu_events = snb_events_attrs;
  2830. /* UOPS_ISSUED.ANY,c=1,i=1 to count stall cycles */
  2831. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
  2832. X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
  2833. pr_cont("IvyBridge events, ");
  2834. break;
  2835. case 60: /* 22nm Haswell Core */
  2836. case 63: /* 22nm Haswell Server */
  2837. case 69: /* 22nm Haswell ULT */
  2838. case 70: /* 22nm Haswell + GT3e (Intel Iris Pro graphics) */
  2839. x86_add_quirk(intel_ht_bug);
  2840. x86_pmu.late_ack = true;
  2841. memcpy(hw_cache_event_ids, hsw_hw_cache_event_ids, sizeof(hw_cache_event_ids));
  2842. memcpy(hw_cache_extra_regs, hsw_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
  2843. intel_pmu_lbr_init_hsw();
  2844. x86_pmu.event_constraints = intel_hsw_event_constraints;
  2845. x86_pmu.pebs_constraints = intel_hsw_pebs_event_constraints;
  2846. x86_pmu.extra_regs = intel_snbep_extra_regs;
  2847. x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
  2848. /* all extra regs are per-cpu when HT is on */
  2849. x86_pmu.flags |= PMU_FL_HAS_RSP_1;
  2850. x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
  2851. x86_pmu.hw_config = hsw_hw_config;
  2852. x86_pmu.get_event_constraints = hsw_get_event_constraints;
  2853. x86_pmu.cpu_events = hsw_events_attrs;
  2854. x86_pmu.lbr_double_abort = true;
  2855. pr_cont("Haswell events, ");
  2856. break;
  2857. case 61: /* 14nm Broadwell Core-M */
  2858. case 86: /* 14nm Broadwell Xeon D */
  2859. case 71: /* 14nm Broadwell + GT3e (Intel Iris Pro graphics) */
  2860. case 79: /* 14nm Broadwell Server */
  2861. x86_pmu.late_ack = true;
  2862. memcpy(hw_cache_event_ids, hsw_hw_cache_event_ids, sizeof(hw_cache_event_ids));
  2863. memcpy(hw_cache_extra_regs, hsw_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
  2864. /* L3_MISS_LOCAL_DRAM is BIT(26) in Broadwell */
  2865. hw_cache_extra_regs[C(LL)][C(OP_READ)][C(RESULT_MISS)] = HSW_DEMAND_READ |
  2866. BDW_L3_MISS|HSW_SNOOP_DRAM;
  2867. hw_cache_extra_regs[C(LL)][C(OP_WRITE)][C(RESULT_MISS)] = HSW_DEMAND_WRITE|BDW_L3_MISS|
  2868. HSW_SNOOP_DRAM;
  2869. hw_cache_extra_regs[C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = HSW_DEMAND_READ|
  2870. BDW_L3_MISS_LOCAL|HSW_SNOOP_DRAM;
  2871. hw_cache_extra_regs[C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = HSW_DEMAND_WRITE|
  2872. BDW_L3_MISS_LOCAL|HSW_SNOOP_DRAM;
  2873. intel_pmu_lbr_init_hsw();
  2874. x86_pmu.event_constraints = intel_bdw_event_constraints;
  2875. x86_pmu.pebs_constraints = intel_hsw_pebs_event_constraints;
  2876. x86_pmu.extra_regs = intel_snbep_extra_regs;
  2877. x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
  2878. /* all extra regs are per-cpu when HT is on */
  2879. x86_pmu.flags |= PMU_FL_HAS_RSP_1;
  2880. x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
  2881. x86_pmu.hw_config = hsw_hw_config;
  2882. x86_pmu.get_event_constraints = hsw_get_event_constraints;
  2883. x86_pmu.cpu_events = hsw_events_attrs;
  2884. x86_pmu.limit_period = bdw_limit_period;
  2885. pr_cont("Broadwell events, ");
  2886. break;
  2887. default:
  2888. switch (x86_pmu.version) {
  2889. case 1:
  2890. x86_pmu.event_constraints = intel_v1_event_constraints;
  2891. pr_cont("generic architected perfmon v1, ");
  2892. break;
  2893. default:
  2894. /*
  2895. * default constraints for v2 and up
  2896. */
  2897. x86_pmu.event_constraints = intel_gen_event_constraints;
  2898. pr_cont("generic architected perfmon, ");
  2899. break;
  2900. }
  2901. }
  2902. if (x86_pmu.num_counters > INTEL_PMC_MAX_GENERIC) {
  2903. WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
  2904. x86_pmu.num_counters, INTEL_PMC_MAX_GENERIC);
  2905. x86_pmu.num_counters = INTEL_PMC_MAX_GENERIC;
  2906. }
  2907. x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
  2908. if (x86_pmu.num_counters_fixed > INTEL_PMC_MAX_FIXED) {
  2909. WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
  2910. x86_pmu.num_counters_fixed, INTEL_PMC_MAX_FIXED);
  2911. x86_pmu.num_counters_fixed = INTEL_PMC_MAX_FIXED;
  2912. }
  2913. x86_pmu.intel_ctrl |=
  2914. ((1LL << x86_pmu.num_counters_fixed)-1) << INTEL_PMC_IDX_FIXED;
  2915. if (x86_pmu.event_constraints) {
  2916. /*
  2917. * event on fixed counter2 (REF_CYCLES) only works on this
  2918. * counter, so do not extend mask to generic counters
  2919. */
  2920. for_each_event_constraint(c, x86_pmu.event_constraints) {
  2921. if (c->cmask == FIXED_EVENT_FLAGS
  2922. && c->idxmsk64 != INTEL_PMC_MSK_FIXED_REF_CYCLES) {
  2923. c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
  2924. }
  2925. c->idxmsk64 &=
  2926. ~(~0UL << (INTEL_PMC_IDX_FIXED + x86_pmu.num_counters_fixed));
  2927. c->weight = hweight64(c->idxmsk64);
  2928. }
  2929. }
  2930. /*
  2931. * Access LBR MSR may cause #GP under certain circumstances.
  2932. * E.g. KVM doesn't support LBR MSR
  2933. * Check all LBT MSR here.
  2934. * Disable LBR access if any LBR MSRs can not be accessed.
  2935. */
  2936. if (x86_pmu.lbr_nr && !check_msr(x86_pmu.lbr_tos, 0x3UL))
  2937. x86_pmu.lbr_nr = 0;
  2938. for (i = 0; i < x86_pmu.lbr_nr; i++) {
  2939. if (!(check_msr(x86_pmu.lbr_from + i, 0xffffUL) &&
  2940. check_msr(x86_pmu.lbr_to + i, 0xffffUL)))
  2941. x86_pmu.lbr_nr = 0;
  2942. }
  2943. /*
  2944. * Access extra MSR may cause #GP under certain circumstances.
  2945. * E.g. KVM doesn't support offcore event
  2946. * Check all extra_regs here.
  2947. */
  2948. if (x86_pmu.extra_regs) {
  2949. for (er = x86_pmu.extra_regs; er->msr; er++) {
  2950. er->extra_msr_access = check_msr(er->msr, 0x1ffUL);
  2951. /* Disable LBR select mapping */
  2952. if ((er->idx == EXTRA_REG_LBR) && !er->extra_msr_access)
  2953. x86_pmu.lbr_sel_map = NULL;
  2954. }
  2955. }
  2956. /* Support full width counters using alternative MSR range */
  2957. if (x86_pmu.intel_cap.full_width_write) {
  2958. x86_pmu.max_period = x86_pmu.cntval_mask;
  2959. x86_pmu.perfctr = MSR_IA32_PMC0;
  2960. pr_cont("full-width counters, ");
  2961. }
  2962. return 0;
  2963. }
  2964. /*
  2965. * HT bug: phase 2 init
  2966. * Called once we have valid topology information to check
  2967. * whether or not HT is enabled
  2968. * If HT is off, then we disable the workaround
  2969. */
  2970. static __init int fixup_ht_bug(void)
  2971. {
  2972. int cpu = smp_processor_id();
  2973. int w, c;
  2974. /*
  2975. * problem not present on this CPU model, nothing to do
  2976. */
  2977. if (!(x86_pmu.flags & PMU_FL_EXCL_ENABLED))
  2978. return 0;
  2979. w = cpumask_weight(topology_sibling_cpumask(cpu));
  2980. if (w > 1) {
  2981. pr_info("PMU erratum BJ122, BV98, HSD29 worked around, HT is on\n");
  2982. return 0;
  2983. }
  2984. watchdog_nmi_disable_all();
  2985. x86_pmu.flags &= ~(PMU_FL_EXCL_CNTRS | PMU_FL_EXCL_ENABLED);
  2986. x86_pmu.start_scheduling = NULL;
  2987. x86_pmu.commit_scheduling = NULL;
  2988. x86_pmu.stop_scheduling = NULL;
  2989. watchdog_nmi_enable_all();
  2990. get_online_cpus();
  2991. for_each_online_cpu(c) {
  2992. free_excl_cntrs(c);
  2993. }
  2994. put_online_cpus();
  2995. pr_info("PMU erratum BJ122, BV98, HSD29 workaround disabled, HT off\n");
  2996. return 0;
  2997. }
  2998. subsys_initcall(fixup_ht_bug)