perf_event_intel.c 101 KB

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