core.c 129 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673
  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/intel-family.h>
  17. #include <asm/apic.h>
  18. #include "../perf_event.h"
  19. /*
  20. * Intel PerfMon, used on Core and later.
  21. */
  22. static u64 intel_perfmon_event_map[PERF_COUNT_HW_MAX] __read_mostly =
  23. {
  24. [PERF_COUNT_HW_CPU_CYCLES] = 0x003c,
  25. [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
  26. [PERF_COUNT_HW_CACHE_REFERENCES] = 0x4f2e,
  27. [PERF_COUNT_HW_CACHE_MISSES] = 0x412e,
  28. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
  29. [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
  30. [PERF_COUNT_HW_BUS_CYCLES] = 0x013c,
  31. [PERF_COUNT_HW_REF_CPU_CYCLES] = 0x0300, /* pseudo-encoding */
  32. };
  33. static struct event_constraint intel_core_event_constraints[] __read_mostly =
  34. {
  35. INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
  36. INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
  37. INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
  38. INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
  39. INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
  40. INTEL_EVENT_CONSTRAINT(0xc1, 0x1), /* FP_COMP_INSTR_RET */
  41. EVENT_CONSTRAINT_END
  42. };
  43. static struct event_constraint intel_core2_event_constraints[] __read_mostly =
  44. {
  45. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  46. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  47. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  48. INTEL_EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */
  49. INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
  50. INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
  51. INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
  52. INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
  53. INTEL_EVENT_CONSTRAINT(0x18, 0x1), /* IDLE_DURING_DIV */
  54. INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
  55. INTEL_EVENT_CONSTRAINT(0xa1, 0x1), /* RS_UOPS_DISPATCH_CYCLES */
  56. INTEL_EVENT_CONSTRAINT(0xc9, 0x1), /* ITLB_MISS_RETIRED (T30-9) */
  57. INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED */
  58. EVENT_CONSTRAINT_END
  59. };
  60. static struct event_constraint intel_nehalem_event_constraints[] __read_mostly =
  61. {
  62. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  63. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  64. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  65. INTEL_EVENT_CONSTRAINT(0x40, 0x3), /* L1D_CACHE_LD */
  66. INTEL_EVENT_CONSTRAINT(0x41, 0x3), /* L1D_CACHE_ST */
  67. INTEL_EVENT_CONSTRAINT(0x42, 0x3), /* L1D_CACHE_LOCK */
  68. INTEL_EVENT_CONSTRAINT(0x43, 0x3), /* L1D_ALL_REF */
  69. INTEL_EVENT_CONSTRAINT(0x48, 0x3), /* L1D_PEND_MISS */
  70. INTEL_EVENT_CONSTRAINT(0x4e, 0x3), /* L1D_PREFETCH */
  71. INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
  72. INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
  73. EVENT_CONSTRAINT_END
  74. };
  75. static struct extra_reg intel_nehalem_extra_regs[] __read_mostly =
  76. {
  77. /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
  78. INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0xffff, RSP_0),
  79. INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x100b),
  80. EVENT_EXTRA_END
  81. };
  82. static struct event_constraint intel_westmere_event_constraints[] __read_mostly =
  83. {
  84. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  85. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  86. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  87. INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
  88. INTEL_EVENT_CONSTRAINT(0x60, 0x1), /* OFFCORE_REQUESTS_OUTSTANDING */
  89. INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
  90. INTEL_EVENT_CONSTRAINT(0xb3, 0x1), /* SNOOPQ_REQUEST_OUTSTANDING */
  91. EVENT_CONSTRAINT_END
  92. };
  93. static struct event_constraint intel_snb_event_constraints[] __read_mostly =
  94. {
  95. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  96. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  97. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  98. INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_DISPATCH */
  99. INTEL_UEVENT_CONSTRAINT(0x05a3, 0xf), /* CYCLE_ACTIVITY.STALLS_L2_PENDING */
  100. INTEL_UEVENT_CONSTRAINT(0x02a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
  101. INTEL_UEVENT_CONSTRAINT(0x06a3, 0x4), /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
  102. INTEL_EVENT_CONSTRAINT(0x48, 0x4), /* L1D_PEND_MISS.PENDING */
  103. INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
  104. INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.LOAD_LATENCY */
  105. INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_DISPATCH */
  106. INTEL_UEVENT_CONSTRAINT(0x02a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
  107. /*
  108. * When HT is off these events can only run on the bottom 4 counters
  109. * When HT is on, they are impacted by the HT bug and require EXCL access
  110. */
  111. INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
  112. INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  113. INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  114. INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
  115. EVENT_CONSTRAINT_END
  116. };
  117. static struct event_constraint intel_ivb_event_constraints[] __read_mostly =
  118. {
  119. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  120. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  121. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  122. INTEL_UEVENT_CONSTRAINT(0x0148, 0x4), /* L1D_PEND_MISS.PENDING */
  123. INTEL_UEVENT_CONSTRAINT(0x0279, 0xf), /* IDQ.EMTPY */
  124. INTEL_UEVENT_CONSTRAINT(0x019c, 0xf), /* IDQ_UOPS_NOT_DELIVERED.CORE */
  125. INTEL_UEVENT_CONSTRAINT(0x02a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_LDM_PENDING */
  126. INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf), /* CYCLE_ACTIVITY.CYCLES_NO_EXECUTE */
  127. INTEL_UEVENT_CONSTRAINT(0x05a3, 0xf), /* CYCLE_ACTIVITY.STALLS_L2_PENDING */
  128. INTEL_UEVENT_CONSTRAINT(0x06a3, 0xf), /* CYCLE_ACTIVITY.STALLS_LDM_PENDING */
  129. INTEL_UEVENT_CONSTRAINT(0x08a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
  130. INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4), /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
  131. INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
  132. /*
  133. * When HT is off these events can only run on the bottom 4 counters
  134. * When HT is on, they are impacted by the HT bug and require EXCL access
  135. */
  136. INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
  137. INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  138. INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  139. INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
  140. EVENT_CONSTRAINT_END
  141. };
  142. static struct extra_reg intel_westmere_extra_regs[] __read_mostly =
  143. {
  144. /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
  145. INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0xffff, RSP_0),
  146. INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0xffff, RSP_1),
  147. INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x100b),
  148. EVENT_EXTRA_END
  149. };
  150. static struct event_constraint intel_v1_event_constraints[] __read_mostly =
  151. {
  152. EVENT_CONSTRAINT_END
  153. };
  154. static struct event_constraint intel_gen_event_constraints[] __read_mostly =
  155. {
  156. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  157. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  158. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  159. EVENT_CONSTRAINT_END
  160. };
  161. static struct event_constraint intel_slm_event_constraints[] __read_mostly =
  162. {
  163. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  164. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  165. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* pseudo CPU_CLK_UNHALTED.REF */
  166. EVENT_CONSTRAINT_END
  167. };
  168. static struct event_constraint intel_skl_event_constraints[] = {
  169. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  170. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  171. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  172. INTEL_UEVENT_CONSTRAINT(0x1c0, 0x2), /* INST_RETIRED.PREC_DIST */
  173. /*
  174. * when HT is off, these can only run on the bottom 4 counters
  175. */
  176. INTEL_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_INST_RETIRED.* */
  177. INTEL_EVENT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */
  178. INTEL_EVENT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */
  179. INTEL_EVENT_CONSTRAINT(0xcd, 0xf), /* MEM_TRANS_RETIRED.* */
  180. INTEL_EVENT_CONSTRAINT(0xc6, 0xf), /* FRONTEND_RETIRED.* */
  181. EVENT_CONSTRAINT_END
  182. };
  183. static struct extra_reg intel_knl_extra_regs[] __read_mostly = {
  184. INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x799ffbb6e7ull, RSP_0),
  185. INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0x399ffbffe7ull, RSP_1),
  186. EVENT_EXTRA_END
  187. };
  188. static struct extra_reg intel_snb_extra_regs[] __read_mostly = {
  189. /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
  190. INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3f807f8fffull, RSP_0),
  191. INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0x3f807f8fffull, RSP_1),
  192. INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd),
  193. EVENT_EXTRA_END
  194. };
  195. static struct extra_reg intel_snbep_extra_regs[] __read_mostly = {
  196. /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
  197. INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3fffff8fffull, RSP_0),
  198. INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0x3fffff8fffull, RSP_1),
  199. INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd),
  200. EVENT_EXTRA_END
  201. };
  202. static struct extra_reg intel_skl_extra_regs[] __read_mostly = {
  203. INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x3fffff8fffull, RSP_0),
  204. INTEL_UEVENT_EXTRA_REG(0x01bb, MSR_OFFCORE_RSP_1, 0x3fffff8fffull, RSP_1),
  205. INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(0x01cd),
  206. /*
  207. * Note the low 8 bits eventsel code is not a continuous field, containing
  208. * some #GPing bits. These are masked out.
  209. */
  210. INTEL_UEVENT_EXTRA_REG(0x01c6, MSR_PEBS_FRONTEND, 0x7fff17, FE),
  211. EVENT_EXTRA_END
  212. };
  213. EVENT_ATTR_STR(mem-loads, mem_ld_nhm, "event=0x0b,umask=0x10,ldlat=3");
  214. EVENT_ATTR_STR(mem-loads, mem_ld_snb, "event=0xcd,umask=0x1,ldlat=3");
  215. EVENT_ATTR_STR(mem-stores, mem_st_snb, "event=0xcd,umask=0x2");
  216. static struct attribute *nhm_mem_events_attrs[] = {
  217. EVENT_PTR(mem_ld_nhm),
  218. NULL,
  219. };
  220. /*
  221. * topdown events for Intel Core CPUs.
  222. *
  223. * The events are all in slots, which is a free slot in a 4 wide
  224. * pipeline. Some events are already reported in slots, for cycle
  225. * events we multiply by the pipeline width (4).
  226. *
  227. * With Hyper Threading on, topdown metrics are either summed or averaged
  228. * between the threads of a core: (count_t0 + count_t1).
  229. *
  230. * For the average case the metric is always scaled to pipeline width,
  231. * so we use factor 2 ((count_t0 + count_t1) / 2 * 4)
  232. */
  233. EVENT_ATTR_STR_HT(topdown-total-slots, td_total_slots,
  234. "event=0x3c,umask=0x0", /* cpu_clk_unhalted.thread */
  235. "event=0x3c,umask=0x0,any=1"); /* cpu_clk_unhalted.thread_any */
  236. EVENT_ATTR_STR_HT(topdown-total-slots.scale, td_total_slots_scale, "4", "2");
  237. EVENT_ATTR_STR(topdown-slots-issued, td_slots_issued,
  238. "event=0xe,umask=0x1"); /* uops_issued.any */
  239. EVENT_ATTR_STR(topdown-slots-retired, td_slots_retired,
  240. "event=0xc2,umask=0x2"); /* uops_retired.retire_slots */
  241. EVENT_ATTR_STR(topdown-fetch-bubbles, td_fetch_bubbles,
  242. "event=0x9c,umask=0x1"); /* idq_uops_not_delivered_core */
  243. EVENT_ATTR_STR_HT(topdown-recovery-bubbles, td_recovery_bubbles,
  244. "event=0xd,umask=0x3,cmask=1", /* int_misc.recovery_cycles */
  245. "event=0xd,umask=0x3,cmask=1,any=1"); /* int_misc.recovery_cycles_any */
  246. EVENT_ATTR_STR_HT(topdown-recovery-bubbles.scale, td_recovery_bubbles_scale,
  247. "4", "2");
  248. static struct attribute *snb_events_attrs[] = {
  249. EVENT_PTR(td_slots_issued),
  250. EVENT_PTR(td_slots_retired),
  251. EVENT_PTR(td_fetch_bubbles),
  252. EVENT_PTR(td_total_slots),
  253. EVENT_PTR(td_total_slots_scale),
  254. EVENT_PTR(td_recovery_bubbles),
  255. EVENT_PTR(td_recovery_bubbles_scale),
  256. NULL,
  257. };
  258. static struct attribute *snb_mem_events_attrs[] = {
  259. EVENT_PTR(mem_ld_snb),
  260. EVENT_PTR(mem_st_snb),
  261. NULL,
  262. };
  263. static struct event_constraint intel_hsw_event_constraints[] = {
  264. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  265. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  266. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  267. INTEL_UEVENT_CONSTRAINT(0x148, 0x4), /* L1D_PEND_MISS.PENDING */
  268. INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PREC_DIST */
  269. INTEL_EVENT_CONSTRAINT(0xcd, 0x8), /* MEM_TRANS_RETIRED.LOAD_LATENCY */
  270. /* CYCLE_ACTIVITY.CYCLES_L1D_PENDING */
  271. INTEL_UEVENT_CONSTRAINT(0x08a3, 0x4),
  272. /* CYCLE_ACTIVITY.STALLS_L1D_PENDING */
  273. INTEL_UEVENT_CONSTRAINT(0x0ca3, 0x4),
  274. /* CYCLE_ACTIVITY.CYCLES_NO_EXECUTE */
  275. INTEL_UEVENT_CONSTRAINT(0x04a3, 0xf),
  276. /*
  277. * When HT is off these events can only run on the bottom 4 counters
  278. * When HT is on, they are impacted by the HT bug and require EXCL access
  279. */
  280. INTEL_EXCLEVT_CONSTRAINT(0xd0, 0xf), /* MEM_UOPS_RETIRED.* */
  281. INTEL_EXCLEVT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_UOPS_RETIRED.* */
  282. INTEL_EXCLEVT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.* */
  283. INTEL_EXCLEVT_CONSTRAINT(0xd3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.* */
  284. EVENT_CONSTRAINT_END
  285. };
  286. static struct event_constraint intel_bdw_event_constraints[] = {
  287. FIXED_EVENT_CONSTRAINT(0x00c0, 0), /* INST_RETIRED.ANY */
  288. FIXED_EVENT_CONSTRAINT(0x003c, 1), /* CPU_CLK_UNHALTED.CORE */
  289. FIXED_EVENT_CONSTRAINT(0x0300, 2), /* CPU_CLK_UNHALTED.REF */
  290. INTEL_UEVENT_CONSTRAINT(0x148, 0x4), /* L1D_PEND_MISS.PENDING */
  291. INTEL_UBIT_EVENT_CONSTRAINT(0x8a3, 0x4), /* CYCLE_ACTIVITY.CYCLES_L1D_MISS */
  292. /*
  293. * when HT is off, these can only run on the bottom 4 counters
  294. */
  295. INTEL_EVENT_CONSTRAINT(0xd0, 0xf), /* MEM_INST_RETIRED.* */
  296. INTEL_EVENT_CONSTRAINT(0xd1, 0xf), /* MEM_LOAD_RETIRED.* */
  297. INTEL_EVENT_CONSTRAINT(0xd2, 0xf), /* MEM_LOAD_L3_HIT_RETIRED.* */
  298. INTEL_EVENT_CONSTRAINT(0xcd, 0xf), /* MEM_TRANS_RETIRED.* */
  299. EVENT_CONSTRAINT_END
  300. };
  301. static u64 intel_pmu_event_map(int hw_event)
  302. {
  303. return intel_perfmon_event_map[hw_event];
  304. }
  305. /*
  306. * Notes on the events:
  307. * - data reads do not include code reads (comparable to earlier tables)
  308. * - data counts include speculative execution (except L1 write, dtlb, bpu)
  309. * - remote node access includes remote memory, remote cache, remote mmio.
  310. * - prefetches are not included in the counts.
  311. * - icache miss does not include decoded icache
  312. */
  313. #define SKL_DEMAND_DATA_RD BIT_ULL(0)
  314. #define SKL_DEMAND_RFO BIT_ULL(1)
  315. #define SKL_ANY_RESPONSE BIT_ULL(16)
  316. #define SKL_SUPPLIER_NONE BIT_ULL(17)
  317. #define SKL_L3_MISS_LOCAL_DRAM BIT_ULL(26)
  318. #define SKL_L3_MISS_REMOTE_HOP0_DRAM BIT_ULL(27)
  319. #define SKL_L3_MISS_REMOTE_HOP1_DRAM BIT_ULL(28)
  320. #define SKL_L3_MISS_REMOTE_HOP2P_DRAM BIT_ULL(29)
  321. #define SKL_L3_MISS (SKL_L3_MISS_LOCAL_DRAM| \
  322. SKL_L3_MISS_REMOTE_HOP0_DRAM| \
  323. SKL_L3_MISS_REMOTE_HOP1_DRAM| \
  324. SKL_L3_MISS_REMOTE_HOP2P_DRAM)
  325. #define SKL_SPL_HIT BIT_ULL(30)
  326. #define SKL_SNOOP_NONE BIT_ULL(31)
  327. #define SKL_SNOOP_NOT_NEEDED BIT_ULL(32)
  328. #define SKL_SNOOP_MISS BIT_ULL(33)
  329. #define SKL_SNOOP_HIT_NO_FWD BIT_ULL(34)
  330. #define SKL_SNOOP_HIT_WITH_FWD BIT_ULL(35)
  331. #define SKL_SNOOP_HITM BIT_ULL(36)
  332. #define SKL_SNOOP_NON_DRAM BIT_ULL(37)
  333. #define SKL_ANY_SNOOP (SKL_SPL_HIT|SKL_SNOOP_NONE| \
  334. SKL_SNOOP_NOT_NEEDED|SKL_SNOOP_MISS| \
  335. SKL_SNOOP_HIT_NO_FWD|SKL_SNOOP_HIT_WITH_FWD| \
  336. SKL_SNOOP_HITM|SKL_SNOOP_NON_DRAM)
  337. #define SKL_DEMAND_READ SKL_DEMAND_DATA_RD
  338. #define SKL_SNOOP_DRAM (SKL_SNOOP_NONE| \
  339. SKL_SNOOP_NOT_NEEDED|SKL_SNOOP_MISS| \
  340. SKL_SNOOP_HIT_NO_FWD|SKL_SNOOP_HIT_WITH_FWD| \
  341. SKL_SNOOP_HITM|SKL_SPL_HIT)
  342. #define SKL_DEMAND_WRITE SKL_DEMAND_RFO
  343. #define SKL_LLC_ACCESS SKL_ANY_RESPONSE
  344. #define SKL_L3_MISS_REMOTE (SKL_L3_MISS_REMOTE_HOP0_DRAM| \
  345. SKL_L3_MISS_REMOTE_HOP1_DRAM| \
  346. SKL_L3_MISS_REMOTE_HOP2P_DRAM)
  347. static __initconst const u64 skl_hw_cache_event_ids
  348. [PERF_COUNT_HW_CACHE_MAX]
  349. [PERF_COUNT_HW_CACHE_OP_MAX]
  350. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  351. {
  352. [ C(L1D ) ] = {
  353. [ C(OP_READ) ] = {
  354. [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_INST_RETIRED.ALL_LOADS */
  355. [ C(RESULT_MISS) ] = 0x151, /* L1D.REPLACEMENT */
  356. },
  357. [ C(OP_WRITE) ] = {
  358. [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_INST_RETIRED.ALL_STORES */
  359. [ C(RESULT_MISS) ] = 0x0,
  360. },
  361. [ C(OP_PREFETCH) ] = {
  362. [ C(RESULT_ACCESS) ] = 0x0,
  363. [ C(RESULT_MISS) ] = 0x0,
  364. },
  365. },
  366. [ C(L1I ) ] = {
  367. [ C(OP_READ) ] = {
  368. [ C(RESULT_ACCESS) ] = 0x0,
  369. [ C(RESULT_MISS) ] = 0x283, /* ICACHE_64B.MISS */
  370. },
  371. [ C(OP_WRITE) ] = {
  372. [ C(RESULT_ACCESS) ] = -1,
  373. [ C(RESULT_MISS) ] = -1,
  374. },
  375. [ C(OP_PREFETCH) ] = {
  376. [ C(RESULT_ACCESS) ] = 0x0,
  377. [ C(RESULT_MISS) ] = 0x0,
  378. },
  379. },
  380. [ C(LL ) ] = {
  381. [ C(OP_READ) ] = {
  382. [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  383. [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  384. },
  385. [ C(OP_WRITE) ] = {
  386. [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  387. [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  388. },
  389. [ C(OP_PREFETCH) ] = {
  390. [ C(RESULT_ACCESS) ] = 0x0,
  391. [ C(RESULT_MISS) ] = 0x0,
  392. },
  393. },
  394. [ C(DTLB) ] = {
  395. [ C(OP_READ) ] = {
  396. [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_INST_RETIRED.ALL_LOADS */
  397. [ C(RESULT_MISS) ] = 0xe08, /* DTLB_LOAD_MISSES.WALK_COMPLETED */
  398. },
  399. [ C(OP_WRITE) ] = {
  400. [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_INST_RETIRED.ALL_STORES */
  401. [ C(RESULT_MISS) ] = 0xe49, /* DTLB_STORE_MISSES.WALK_COMPLETED */
  402. },
  403. [ C(OP_PREFETCH) ] = {
  404. [ C(RESULT_ACCESS) ] = 0x0,
  405. [ C(RESULT_MISS) ] = 0x0,
  406. },
  407. },
  408. [ C(ITLB) ] = {
  409. [ C(OP_READ) ] = {
  410. [ C(RESULT_ACCESS) ] = 0x2085, /* ITLB_MISSES.STLB_HIT */
  411. [ C(RESULT_MISS) ] = 0xe85, /* ITLB_MISSES.WALK_COMPLETED */
  412. },
  413. [ C(OP_WRITE) ] = {
  414. [ C(RESULT_ACCESS) ] = -1,
  415. [ C(RESULT_MISS) ] = -1,
  416. },
  417. [ C(OP_PREFETCH) ] = {
  418. [ C(RESULT_ACCESS) ] = -1,
  419. [ C(RESULT_MISS) ] = -1,
  420. },
  421. },
  422. [ C(BPU ) ] = {
  423. [ C(OP_READ) ] = {
  424. [ C(RESULT_ACCESS) ] = 0xc4, /* BR_INST_RETIRED.ALL_BRANCHES */
  425. [ C(RESULT_MISS) ] = 0xc5, /* BR_MISP_RETIRED.ALL_BRANCHES */
  426. },
  427. [ C(OP_WRITE) ] = {
  428. [ C(RESULT_ACCESS) ] = -1,
  429. [ C(RESULT_MISS) ] = -1,
  430. },
  431. [ C(OP_PREFETCH) ] = {
  432. [ C(RESULT_ACCESS) ] = -1,
  433. [ C(RESULT_MISS) ] = -1,
  434. },
  435. },
  436. [ C(NODE) ] = {
  437. [ C(OP_READ) ] = {
  438. [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  439. [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  440. },
  441. [ C(OP_WRITE) ] = {
  442. [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  443. [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  444. },
  445. [ C(OP_PREFETCH) ] = {
  446. [ C(RESULT_ACCESS) ] = 0x0,
  447. [ C(RESULT_MISS) ] = 0x0,
  448. },
  449. },
  450. };
  451. static __initconst const u64 skl_hw_cache_extra_regs
  452. [PERF_COUNT_HW_CACHE_MAX]
  453. [PERF_COUNT_HW_CACHE_OP_MAX]
  454. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  455. {
  456. [ C(LL ) ] = {
  457. [ C(OP_READ) ] = {
  458. [ C(RESULT_ACCESS) ] = SKL_DEMAND_READ|
  459. SKL_LLC_ACCESS|SKL_ANY_SNOOP,
  460. [ C(RESULT_MISS) ] = SKL_DEMAND_READ|
  461. SKL_L3_MISS|SKL_ANY_SNOOP|
  462. SKL_SUPPLIER_NONE,
  463. },
  464. [ C(OP_WRITE) ] = {
  465. [ C(RESULT_ACCESS) ] = SKL_DEMAND_WRITE|
  466. SKL_LLC_ACCESS|SKL_ANY_SNOOP,
  467. [ C(RESULT_MISS) ] = SKL_DEMAND_WRITE|
  468. SKL_L3_MISS|SKL_ANY_SNOOP|
  469. SKL_SUPPLIER_NONE,
  470. },
  471. [ C(OP_PREFETCH) ] = {
  472. [ C(RESULT_ACCESS) ] = 0x0,
  473. [ C(RESULT_MISS) ] = 0x0,
  474. },
  475. },
  476. [ C(NODE) ] = {
  477. [ C(OP_READ) ] = {
  478. [ C(RESULT_ACCESS) ] = SKL_DEMAND_READ|
  479. SKL_L3_MISS_LOCAL_DRAM|SKL_SNOOP_DRAM,
  480. [ C(RESULT_MISS) ] = SKL_DEMAND_READ|
  481. SKL_L3_MISS_REMOTE|SKL_SNOOP_DRAM,
  482. },
  483. [ C(OP_WRITE) ] = {
  484. [ C(RESULT_ACCESS) ] = SKL_DEMAND_WRITE|
  485. SKL_L3_MISS_LOCAL_DRAM|SKL_SNOOP_DRAM,
  486. [ C(RESULT_MISS) ] = SKL_DEMAND_WRITE|
  487. SKL_L3_MISS_REMOTE|SKL_SNOOP_DRAM,
  488. },
  489. [ C(OP_PREFETCH) ] = {
  490. [ C(RESULT_ACCESS) ] = 0x0,
  491. [ C(RESULT_MISS) ] = 0x0,
  492. },
  493. },
  494. };
  495. #define SNB_DMND_DATA_RD (1ULL << 0)
  496. #define SNB_DMND_RFO (1ULL << 1)
  497. #define SNB_DMND_IFETCH (1ULL << 2)
  498. #define SNB_DMND_WB (1ULL << 3)
  499. #define SNB_PF_DATA_RD (1ULL << 4)
  500. #define SNB_PF_RFO (1ULL << 5)
  501. #define SNB_PF_IFETCH (1ULL << 6)
  502. #define SNB_LLC_DATA_RD (1ULL << 7)
  503. #define SNB_LLC_RFO (1ULL << 8)
  504. #define SNB_LLC_IFETCH (1ULL << 9)
  505. #define SNB_BUS_LOCKS (1ULL << 10)
  506. #define SNB_STRM_ST (1ULL << 11)
  507. #define SNB_OTHER (1ULL << 15)
  508. #define SNB_RESP_ANY (1ULL << 16)
  509. #define SNB_NO_SUPP (1ULL << 17)
  510. #define SNB_LLC_HITM (1ULL << 18)
  511. #define SNB_LLC_HITE (1ULL << 19)
  512. #define SNB_LLC_HITS (1ULL << 20)
  513. #define SNB_LLC_HITF (1ULL << 21)
  514. #define SNB_LOCAL (1ULL << 22)
  515. #define SNB_REMOTE (0xffULL << 23)
  516. #define SNB_SNP_NONE (1ULL << 31)
  517. #define SNB_SNP_NOT_NEEDED (1ULL << 32)
  518. #define SNB_SNP_MISS (1ULL << 33)
  519. #define SNB_NO_FWD (1ULL << 34)
  520. #define SNB_SNP_FWD (1ULL << 35)
  521. #define SNB_HITM (1ULL << 36)
  522. #define SNB_NON_DRAM (1ULL << 37)
  523. #define SNB_DMND_READ (SNB_DMND_DATA_RD|SNB_LLC_DATA_RD)
  524. #define SNB_DMND_WRITE (SNB_DMND_RFO|SNB_LLC_RFO)
  525. #define SNB_DMND_PREFETCH (SNB_PF_DATA_RD|SNB_PF_RFO)
  526. #define SNB_SNP_ANY (SNB_SNP_NONE|SNB_SNP_NOT_NEEDED| \
  527. SNB_SNP_MISS|SNB_NO_FWD|SNB_SNP_FWD| \
  528. SNB_HITM)
  529. #define SNB_DRAM_ANY (SNB_LOCAL|SNB_REMOTE|SNB_SNP_ANY)
  530. #define SNB_DRAM_REMOTE (SNB_REMOTE|SNB_SNP_ANY)
  531. #define SNB_L3_ACCESS SNB_RESP_ANY
  532. #define SNB_L3_MISS (SNB_DRAM_ANY|SNB_NON_DRAM)
  533. static __initconst const u64 snb_hw_cache_extra_regs
  534. [PERF_COUNT_HW_CACHE_MAX]
  535. [PERF_COUNT_HW_CACHE_OP_MAX]
  536. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  537. {
  538. [ C(LL ) ] = {
  539. [ C(OP_READ) ] = {
  540. [ C(RESULT_ACCESS) ] = SNB_DMND_READ|SNB_L3_ACCESS,
  541. [ C(RESULT_MISS) ] = SNB_DMND_READ|SNB_L3_MISS,
  542. },
  543. [ C(OP_WRITE) ] = {
  544. [ C(RESULT_ACCESS) ] = SNB_DMND_WRITE|SNB_L3_ACCESS,
  545. [ C(RESULT_MISS) ] = SNB_DMND_WRITE|SNB_L3_MISS,
  546. },
  547. [ C(OP_PREFETCH) ] = {
  548. [ C(RESULT_ACCESS) ] = SNB_DMND_PREFETCH|SNB_L3_ACCESS,
  549. [ C(RESULT_MISS) ] = SNB_DMND_PREFETCH|SNB_L3_MISS,
  550. },
  551. },
  552. [ C(NODE) ] = {
  553. [ C(OP_READ) ] = {
  554. [ C(RESULT_ACCESS) ] = SNB_DMND_READ|SNB_DRAM_ANY,
  555. [ C(RESULT_MISS) ] = SNB_DMND_READ|SNB_DRAM_REMOTE,
  556. },
  557. [ C(OP_WRITE) ] = {
  558. [ C(RESULT_ACCESS) ] = SNB_DMND_WRITE|SNB_DRAM_ANY,
  559. [ C(RESULT_MISS) ] = SNB_DMND_WRITE|SNB_DRAM_REMOTE,
  560. },
  561. [ C(OP_PREFETCH) ] = {
  562. [ C(RESULT_ACCESS) ] = SNB_DMND_PREFETCH|SNB_DRAM_ANY,
  563. [ C(RESULT_MISS) ] = SNB_DMND_PREFETCH|SNB_DRAM_REMOTE,
  564. },
  565. },
  566. };
  567. static __initconst const u64 snb_hw_cache_event_ids
  568. [PERF_COUNT_HW_CACHE_MAX]
  569. [PERF_COUNT_HW_CACHE_OP_MAX]
  570. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  571. {
  572. [ C(L1D) ] = {
  573. [ C(OP_READ) ] = {
  574. [ C(RESULT_ACCESS) ] = 0xf1d0, /* MEM_UOP_RETIRED.LOADS */
  575. [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPLACEMENT */
  576. },
  577. [ C(OP_WRITE) ] = {
  578. [ C(RESULT_ACCESS) ] = 0xf2d0, /* MEM_UOP_RETIRED.STORES */
  579. [ C(RESULT_MISS) ] = 0x0851, /* L1D.ALL_M_REPLACEMENT */
  580. },
  581. [ C(OP_PREFETCH) ] = {
  582. [ C(RESULT_ACCESS) ] = 0x0,
  583. [ C(RESULT_MISS) ] = 0x024e, /* HW_PRE_REQ.DL1_MISS */
  584. },
  585. },
  586. [ C(L1I ) ] = {
  587. [ C(OP_READ) ] = {
  588. [ C(RESULT_ACCESS) ] = 0x0,
  589. [ C(RESULT_MISS) ] = 0x0280, /* ICACHE.MISSES */
  590. },
  591. [ C(OP_WRITE) ] = {
  592. [ C(RESULT_ACCESS) ] = -1,
  593. [ C(RESULT_MISS) ] = -1,
  594. },
  595. [ C(OP_PREFETCH) ] = {
  596. [ C(RESULT_ACCESS) ] = 0x0,
  597. [ C(RESULT_MISS) ] = 0x0,
  598. },
  599. },
  600. [ C(LL ) ] = {
  601. [ C(OP_READ) ] = {
  602. /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
  603. [ C(RESULT_ACCESS) ] = 0x01b7,
  604. /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
  605. [ C(RESULT_MISS) ] = 0x01b7,
  606. },
  607. [ C(OP_WRITE) ] = {
  608. /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
  609. [ C(RESULT_ACCESS) ] = 0x01b7,
  610. /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
  611. [ C(RESULT_MISS) ] = 0x01b7,
  612. },
  613. [ C(OP_PREFETCH) ] = {
  614. /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
  615. [ C(RESULT_ACCESS) ] = 0x01b7,
  616. /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
  617. [ C(RESULT_MISS) ] = 0x01b7,
  618. },
  619. },
  620. [ C(DTLB) ] = {
  621. [ C(OP_READ) ] = {
  622. [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOP_RETIRED.ALL_LOADS */
  623. [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.CAUSES_A_WALK */
  624. },
  625. [ C(OP_WRITE) ] = {
  626. [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOP_RETIRED.ALL_STORES */
  627. [ C(RESULT_MISS) ] = 0x0149, /* DTLB_STORE_MISSES.MISS_CAUSES_A_WALK */
  628. },
  629. [ C(OP_PREFETCH) ] = {
  630. [ C(RESULT_ACCESS) ] = 0x0,
  631. [ C(RESULT_MISS) ] = 0x0,
  632. },
  633. },
  634. [ C(ITLB) ] = {
  635. [ C(OP_READ) ] = {
  636. [ C(RESULT_ACCESS) ] = 0x1085, /* ITLB_MISSES.STLB_HIT */
  637. [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.CAUSES_A_WALK */
  638. },
  639. [ C(OP_WRITE) ] = {
  640. [ C(RESULT_ACCESS) ] = -1,
  641. [ C(RESULT_MISS) ] = -1,
  642. },
  643. [ C(OP_PREFETCH) ] = {
  644. [ C(RESULT_ACCESS) ] = -1,
  645. [ C(RESULT_MISS) ] = -1,
  646. },
  647. },
  648. [ C(BPU ) ] = {
  649. [ C(OP_READ) ] = {
  650. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
  651. [ C(RESULT_MISS) ] = 0x00c5, /* BR_MISP_RETIRED.ALL_BRANCHES */
  652. },
  653. [ C(OP_WRITE) ] = {
  654. [ C(RESULT_ACCESS) ] = -1,
  655. [ C(RESULT_MISS) ] = -1,
  656. },
  657. [ C(OP_PREFETCH) ] = {
  658. [ C(RESULT_ACCESS) ] = -1,
  659. [ C(RESULT_MISS) ] = -1,
  660. },
  661. },
  662. [ C(NODE) ] = {
  663. [ C(OP_READ) ] = {
  664. [ C(RESULT_ACCESS) ] = 0x01b7,
  665. [ C(RESULT_MISS) ] = 0x01b7,
  666. },
  667. [ C(OP_WRITE) ] = {
  668. [ C(RESULT_ACCESS) ] = 0x01b7,
  669. [ C(RESULT_MISS) ] = 0x01b7,
  670. },
  671. [ C(OP_PREFETCH) ] = {
  672. [ C(RESULT_ACCESS) ] = 0x01b7,
  673. [ C(RESULT_MISS) ] = 0x01b7,
  674. },
  675. },
  676. };
  677. /*
  678. * Notes on the events:
  679. * - data reads do not include code reads (comparable to earlier tables)
  680. * - data counts include speculative execution (except L1 write, dtlb, bpu)
  681. * - remote node access includes remote memory, remote cache, remote mmio.
  682. * - prefetches are not included in the counts because they are not
  683. * reliably counted.
  684. */
  685. #define HSW_DEMAND_DATA_RD BIT_ULL(0)
  686. #define HSW_DEMAND_RFO BIT_ULL(1)
  687. #define HSW_ANY_RESPONSE BIT_ULL(16)
  688. #define HSW_SUPPLIER_NONE BIT_ULL(17)
  689. #define HSW_L3_MISS_LOCAL_DRAM BIT_ULL(22)
  690. #define HSW_L3_MISS_REMOTE_HOP0 BIT_ULL(27)
  691. #define HSW_L3_MISS_REMOTE_HOP1 BIT_ULL(28)
  692. #define HSW_L3_MISS_REMOTE_HOP2P BIT_ULL(29)
  693. #define HSW_L3_MISS (HSW_L3_MISS_LOCAL_DRAM| \
  694. HSW_L3_MISS_REMOTE_HOP0|HSW_L3_MISS_REMOTE_HOP1| \
  695. HSW_L3_MISS_REMOTE_HOP2P)
  696. #define HSW_SNOOP_NONE BIT_ULL(31)
  697. #define HSW_SNOOP_NOT_NEEDED BIT_ULL(32)
  698. #define HSW_SNOOP_MISS BIT_ULL(33)
  699. #define HSW_SNOOP_HIT_NO_FWD BIT_ULL(34)
  700. #define HSW_SNOOP_HIT_WITH_FWD BIT_ULL(35)
  701. #define HSW_SNOOP_HITM BIT_ULL(36)
  702. #define HSW_SNOOP_NON_DRAM BIT_ULL(37)
  703. #define HSW_ANY_SNOOP (HSW_SNOOP_NONE| \
  704. HSW_SNOOP_NOT_NEEDED|HSW_SNOOP_MISS| \
  705. HSW_SNOOP_HIT_NO_FWD|HSW_SNOOP_HIT_WITH_FWD| \
  706. HSW_SNOOP_HITM|HSW_SNOOP_NON_DRAM)
  707. #define HSW_SNOOP_DRAM (HSW_ANY_SNOOP & ~HSW_SNOOP_NON_DRAM)
  708. #define HSW_DEMAND_READ HSW_DEMAND_DATA_RD
  709. #define HSW_DEMAND_WRITE HSW_DEMAND_RFO
  710. #define HSW_L3_MISS_REMOTE (HSW_L3_MISS_REMOTE_HOP0|\
  711. HSW_L3_MISS_REMOTE_HOP1|HSW_L3_MISS_REMOTE_HOP2P)
  712. #define HSW_LLC_ACCESS HSW_ANY_RESPONSE
  713. #define BDW_L3_MISS_LOCAL BIT(26)
  714. #define BDW_L3_MISS (BDW_L3_MISS_LOCAL| \
  715. HSW_L3_MISS_REMOTE_HOP0|HSW_L3_MISS_REMOTE_HOP1| \
  716. HSW_L3_MISS_REMOTE_HOP2P)
  717. static __initconst const u64 hsw_hw_cache_event_ids
  718. [PERF_COUNT_HW_CACHE_MAX]
  719. [PERF_COUNT_HW_CACHE_OP_MAX]
  720. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  721. {
  722. [ C(L1D ) ] = {
  723. [ C(OP_READ) ] = {
  724. [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
  725. [ C(RESULT_MISS) ] = 0x151, /* L1D.REPLACEMENT */
  726. },
  727. [ C(OP_WRITE) ] = {
  728. [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
  729. [ C(RESULT_MISS) ] = 0x0,
  730. },
  731. [ C(OP_PREFETCH) ] = {
  732. [ C(RESULT_ACCESS) ] = 0x0,
  733. [ C(RESULT_MISS) ] = 0x0,
  734. },
  735. },
  736. [ C(L1I ) ] = {
  737. [ C(OP_READ) ] = {
  738. [ C(RESULT_ACCESS) ] = 0x0,
  739. [ C(RESULT_MISS) ] = 0x280, /* ICACHE.MISSES */
  740. },
  741. [ C(OP_WRITE) ] = {
  742. [ C(RESULT_ACCESS) ] = -1,
  743. [ C(RESULT_MISS) ] = -1,
  744. },
  745. [ C(OP_PREFETCH) ] = {
  746. [ C(RESULT_ACCESS) ] = 0x0,
  747. [ C(RESULT_MISS) ] = 0x0,
  748. },
  749. },
  750. [ C(LL ) ] = {
  751. [ C(OP_READ) ] = {
  752. [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  753. [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  754. },
  755. [ C(OP_WRITE) ] = {
  756. [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  757. [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  758. },
  759. [ C(OP_PREFETCH) ] = {
  760. [ C(RESULT_ACCESS) ] = 0x0,
  761. [ C(RESULT_MISS) ] = 0x0,
  762. },
  763. },
  764. [ C(DTLB) ] = {
  765. [ C(OP_READ) ] = {
  766. [ C(RESULT_ACCESS) ] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
  767. [ C(RESULT_MISS) ] = 0x108, /* DTLB_LOAD_MISSES.MISS_CAUSES_A_WALK */
  768. },
  769. [ C(OP_WRITE) ] = {
  770. [ C(RESULT_ACCESS) ] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
  771. [ C(RESULT_MISS) ] = 0x149, /* DTLB_STORE_MISSES.MISS_CAUSES_A_WALK */
  772. },
  773. [ C(OP_PREFETCH) ] = {
  774. [ C(RESULT_ACCESS) ] = 0x0,
  775. [ C(RESULT_MISS) ] = 0x0,
  776. },
  777. },
  778. [ C(ITLB) ] = {
  779. [ C(OP_READ) ] = {
  780. [ C(RESULT_ACCESS) ] = 0x6085, /* ITLB_MISSES.STLB_HIT */
  781. [ C(RESULT_MISS) ] = 0x185, /* ITLB_MISSES.MISS_CAUSES_A_WALK */
  782. },
  783. [ C(OP_WRITE) ] = {
  784. [ C(RESULT_ACCESS) ] = -1,
  785. [ C(RESULT_MISS) ] = -1,
  786. },
  787. [ C(OP_PREFETCH) ] = {
  788. [ C(RESULT_ACCESS) ] = -1,
  789. [ C(RESULT_MISS) ] = -1,
  790. },
  791. },
  792. [ C(BPU ) ] = {
  793. [ C(OP_READ) ] = {
  794. [ C(RESULT_ACCESS) ] = 0xc4, /* BR_INST_RETIRED.ALL_BRANCHES */
  795. [ C(RESULT_MISS) ] = 0xc5, /* BR_MISP_RETIRED.ALL_BRANCHES */
  796. },
  797. [ C(OP_WRITE) ] = {
  798. [ C(RESULT_ACCESS) ] = -1,
  799. [ C(RESULT_MISS) ] = -1,
  800. },
  801. [ C(OP_PREFETCH) ] = {
  802. [ C(RESULT_ACCESS) ] = -1,
  803. [ C(RESULT_MISS) ] = -1,
  804. },
  805. },
  806. [ C(NODE) ] = {
  807. [ C(OP_READ) ] = {
  808. [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  809. [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  810. },
  811. [ C(OP_WRITE) ] = {
  812. [ C(RESULT_ACCESS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  813. [ C(RESULT_MISS) ] = 0x1b7, /* OFFCORE_RESPONSE */
  814. },
  815. [ C(OP_PREFETCH) ] = {
  816. [ C(RESULT_ACCESS) ] = 0x0,
  817. [ C(RESULT_MISS) ] = 0x0,
  818. },
  819. },
  820. };
  821. static __initconst const u64 hsw_hw_cache_extra_regs
  822. [PERF_COUNT_HW_CACHE_MAX]
  823. [PERF_COUNT_HW_CACHE_OP_MAX]
  824. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  825. {
  826. [ C(LL ) ] = {
  827. [ C(OP_READ) ] = {
  828. [ C(RESULT_ACCESS) ] = HSW_DEMAND_READ|
  829. HSW_LLC_ACCESS,
  830. [ C(RESULT_MISS) ] = HSW_DEMAND_READ|
  831. HSW_L3_MISS|HSW_ANY_SNOOP,
  832. },
  833. [ C(OP_WRITE) ] = {
  834. [ C(RESULT_ACCESS) ] = HSW_DEMAND_WRITE|
  835. HSW_LLC_ACCESS,
  836. [ C(RESULT_MISS) ] = HSW_DEMAND_WRITE|
  837. HSW_L3_MISS|HSW_ANY_SNOOP,
  838. },
  839. [ C(OP_PREFETCH) ] = {
  840. [ C(RESULT_ACCESS) ] = 0x0,
  841. [ C(RESULT_MISS) ] = 0x0,
  842. },
  843. },
  844. [ C(NODE) ] = {
  845. [ C(OP_READ) ] = {
  846. [ C(RESULT_ACCESS) ] = HSW_DEMAND_READ|
  847. HSW_L3_MISS_LOCAL_DRAM|
  848. HSW_SNOOP_DRAM,
  849. [ C(RESULT_MISS) ] = HSW_DEMAND_READ|
  850. HSW_L3_MISS_REMOTE|
  851. HSW_SNOOP_DRAM,
  852. },
  853. [ C(OP_WRITE) ] = {
  854. [ C(RESULT_ACCESS) ] = HSW_DEMAND_WRITE|
  855. HSW_L3_MISS_LOCAL_DRAM|
  856. HSW_SNOOP_DRAM,
  857. [ C(RESULT_MISS) ] = HSW_DEMAND_WRITE|
  858. HSW_L3_MISS_REMOTE|
  859. HSW_SNOOP_DRAM,
  860. },
  861. [ C(OP_PREFETCH) ] = {
  862. [ C(RESULT_ACCESS) ] = 0x0,
  863. [ C(RESULT_MISS) ] = 0x0,
  864. },
  865. },
  866. };
  867. static __initconst const u64 westmere_hw_cache_event_ids
  868. [PERF_COUNT_HW_CACHE_MAX]
  869. [PERF_COUNT_HW_CACHE_OP_MAX]
  870. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  871. {
  872. [ C(L1D) ] = {
  873. [ C(OP_READ) ] = {
  874. [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
  875. [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */
  876. },
  877. [ C(OP_WRITE) ] = {
  878. [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
  879. [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */
  880. },
  881. [ C(OP_PREFETCH) ] = {
  882. [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
  883. [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
  884. },
  885. },
  886. [ C(L1I ) ] = {
  887. [ C(OP_READ) ] = {
  888. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  889. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  890. },
  891. [ C(OP_WRITE) ] = {
  892. [ C(RESULT_ACCESS) ] = -1,
  893. [ C(RESULT_MISS) ] = -1,
  894. },
  895. [ C(OP_PREFETCH) ] = {
  896. [ C(RESULT_ACCESS) ] = 0x0,
  897. [ C(RESULT_MISS) ] = 0x0,
  898. },
  899. },
  900. [ C(LL ) ] = {
  901. [ C(OP_READ) ] = {
  902. /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
  903. [ C(RESULT_ACCESS) ] = 0x01b7,
  904. /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
  905. [ C(RESULT_MISS) ] = 0x01b7,
  906. },
  907. /*
  908. * Use RFO, not WRITEBACK, because a write miss would typically occur
  909. * on RFO.
  910. */
  911. [ C(OP_WRITE) ] = {
  912. /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
  913. [ C(RESULT_ACCESS) ] = 0x01b7,
  914. /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
  915. [ C(RESULT_MISS) ] = 0x01b7,
  916. },
  917. [ C(OP_PREFETCH) ] = {
  918. /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
  919. [ C(RESULT_ACCESS) ] = 0x01b7,
  920. /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
  921. [ C(RESULT_MISS) ] = 0x01b7,
  922. },
  923. },
  924. [ C(DTLB) ] = {
  925. [ C(OP_READ) ] = {
  926. [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
  927. [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
  928. },
  929. [ C(OP_WRITE) ] = {
  930. [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
  931. [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
  932. },
  933. [ C(OP_PREFETCH) ] = {
  934. [ C(RESULT_ACCESS) ] = 0x0,
  935. [ C(RESULT_MISS) ] = 0x0,
  936. },
  937. },
  938. [ C(ITLB) ] = {
  939. [ C(OP_READ) ] = {
  940. [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
  941. [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.ANY */
  942. },
  943. [ C(OP_WRITE) ] = {
  944. [ C(RESULT_ACCESS) ] = -1,
  945. [ C(RESULT_MISS) ] = -1,
  946. },
  947. [ C(OP_PREFETCH) ] = {
  948. [ C(RESULT_ACCESS) ] = -1,
  949. [ C(RESULT_MISS) ] = -1,
  950. },
  951. },
  952. [ C(BPU ) ] = {
  953. [ C(OP_READ) ] = {
  954. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
  955. [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
  956. },
  957. [ C(OP_WRITE) ] = {
  958. [ C(RESULT_ACCESS) ] = -1,
  959. [ C(RESULT_MISS) ] = -1,
  960. },
  961. [ C(OP_PREFETCH) ] = {
  962. [ C(RESULT_ACCESS) ] = -1,
  963. [ C(RESULT_MISS) ] = -1,
  964. },
  965. },
  966. [ C(NODE) ] = {
  967. [ C(OP_READ) ] = {
  968. [ C(RESULT_ACCESS) ] = 0x01b7,
  969. [ C(RESULT_MISS) ] = 0x01b7,
  970. },
  971. [ C(OP_WRITE) ] = {
  972. [ C(RESULT_ACCESS) ] = 0x01b7,
  973. [ C(RESULT_MISS) ] = 0x01b7,
  974. },
  975. [ C(OP_PREFETCH) ] = {
  976. [ C(RESULT_ACCESS) ] = 0x01b7,
  977. [ C(RESULT_MISS) ] = 0x01b7,
  978. },
  979. },
  980. };
  981. /*
  982. * Nehalem/Westmere MSR_OFFCORE_RESPONSE bits;
  983. * See IA32 SDM Vol 3B 30.6.1.3
  984. */
  985. #define NHM_DMND_DATA_RD (1 << 0)
  986. #define NHM_DMND_RFO (1 << 1)
  987. #define NHM_DMND_IFETCH (1 << 2)
  988. #define NHM_DMND_WB (1 << 3)
  989. #define NHM_PF_DATA_RD (1 << 4)
  990. #define NHM_PF_DATA_RFO (1 << 5)
  991. #define NHM_PF_IFETCH (1 << 6)
  992. #define NHM_OFFCORE_OTHER (1 << 7)
  993. #define NHM_UNCORE_HIT (1 << 8)
  994. #define NHM_OTHER_CORE_HIT_SNP (1 << 9)
  995. #define NHM_OTHER_CORE_HITM (1 << 10)
  996. /* reserved */
  997. #define NHM_REMOTE_CACHE_FWD (1 << 12)
  998. #define NHM_REMOTE_DRAM (1 << 13)
  999. #define NHM_LOCAL_DRAM (1 << 14)
  1000. #define NHM_NON_DRAM (1 << 15)
  1001. #define NHM_LOCAL (NHM_LOCAL_DRAM|NHM_REMOTE_CACHE_FWD)
  1002. #define NHM_REMOTE (NHM_REMOTE_DRAM)
  1003. #define NHM_DMND_READ (NHM_DMND_DATA_RD)
  1004. #define NHM_DMND_WRITE (NHM_DMND_RFO|NHM_DMND_WB)
  1005. #define NHM_DMND_PREFETCH (NHM_PF_DATA_RD|NHM_PF_DATA_RFO)
  1006. #define NHM_L3_HIT (NHM_UNCORE_HIT|NHM_OTHER_CORE_HIT_SNP|NHM_OTHER_CORE_HITM)
  1007. #define NHM_L3_MISS (NHM_NON_DRAM|NHM_LOCAL_DRAM|NHM_REMOTE_DRAM|NHM_REMOTE_CACHE_FWD)
  1008. #define NHM_L3_ACCESS (NHM_L3_HIT|NHM_L3_MISS)
  1009. static __initconst const u64 nehalem_hw_cache_extra_regs
  1010. [PERF_COUNT_HW_CACHE_MAX]
  1011. [PERF_COUNT_HW_CACHE_OP_MAX]
  1012. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  1013. {
  1014. [ C(LL ) ] = {
  1015. [ C(OP_READ) ] = {
  1016. [ C(RESULT_ACCESS) ] = NHM_DMND_READ|NHM_L3_ACCESS,
  1017. [ C(RESULT_MISS) ] = NHM_DMND_READ|NHM_L3_MISS,
  1018. },
  1019. [ C(OP_WRITE) ] = {
  1020. [ C(RESULT_ACCESS) ] = NHM_DMND_WRITE|NHM_L3_ACCESS,
  1021. [ C(RESULT_MISS) ] = NHM_DMND_WRITE|NHM_L3_MISS,
  1022. },
  1023. [ C(OP_PREFETCH) ] = {
  1024. [ C(RESULT_ACCESS) ] = NHM_DMND_PREFETCH|NHM_L3_ACCESS,
  1025. [ C(RESULT_MISS) ] = NHM_DMND_PREFETCH|NHM_L3_MISS,
  1026. },
  1027. },
  1028. [ C(NODE) ] = {
  1029. [ C(OP_READ) ] = {
  1030. [ C(RESULT_ACCESS) ] = NHM_DMND_READ|NHM_LOCAL|NHM_REMOTE,
  1031. [ C(RESULT_MISS) ] = NHM_DMND_READ|NHM_REMOTE,
  1032. },
  1033. [ C(OP_WRITE) ] = {
  1034. [ C(RESULT_ACCESS) ] = NHM_DMND_WRITE|NHM_LOCAL|NHM_REMOTE,
  1035. [ C(RESULT_MISS) ] = NHM_DMND_WRITE|NHM_REMOTE,
  1036. },
  1037. [ C(OP_PREFETCH) ] = {
  1038. [ C(RESULT_ACCESS) ] = NHM_DMND_PREFETCH|NHM_LOCAL|NHM_REMOTE,
  1039. [ C(RESULT_MISS) ] = NHM_DMND_PREFETCH|NHM_REMOTE,
  1040. },
  1041. },
  1042. };
  1043. static __initconst const u64 nehalem_hw_cache_event_ids
  1044. [PERF_COUNT_HW_CACHE_MAX]
  1045. [PERF_COUNT_HW_CACHE_OP_MAX]
  1046. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  1047. {
  1048. [ C(L1D) ] = {
  1049. [ C(OP_READ) ] = {
  1050. [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
  1051. [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */
  1052. },
  1053. [ C(OP_WRITE) ] = {
  1054. [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
  1055. [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */
  1056. },
  1057. [ C(OP_PREFETCH) ] = {
  1058. [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
  1059. [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
  1060. },
  1061. },
  1062. [ C(L1I ) ] = {
  1063. [ C(OP_READ) ] = {
  1064. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  1065. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  1066. },
  1067. [ C(OP_WRITE) ] = {
  1068. [ C(RESULT_ACCESS) ] = -1,
  1069. [ C(RESULT_MISS) ] = -1,
  1070. },
  1071. [ C(OP_PREFETCH) ] = {
  1072. [ C(RESULT_ACCESS) ] = 0x0,
  1073. [ C(RESULT_MISS) ] = 0x0,
  1074. },
  1075. },
  1076. [ C(LL ) ] = {
  1077. [ C(OP_READ) ] = {
  1078. /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
  1079. [ C(RESULT_ACCESS) ] = 0x01b7,
  1080. /* OFFCORE_RESPONSE.ANY_DATA.ANY_LLC_MISS */
  1081. [ C(RESULT_MISS) ] = 0x01b7,
  1082. },
  1083. /*
  1084. * Use RFO, not WRITEBACK, because a write miss would typically occur
  1085. * on RFO.
  1086. */
  1087. [ C(OP_WRITE) ] = {
  1088. /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
  1089. [ C(RESULT_ACCESS) ] = 0x01b7,
  1090. /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
  1091. [ C(RESULT_MISS) ] = 0x01b7,
  1092. },
  1093. [ C(OP_PREFETCH) ] = {
  1094. /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
  1095. [ C(RESULT_ACCESS) ] = 0x01b7,
  1096. /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
  1097. [ C(RESULT_MISS) ] = 0x01b7,
  1098. },
  1099. },
  1100. [ C(DTLB) ] = {
  1101. [ C(OP_READ) ] = {
  1102. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
  1103. [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
  1104. },
  1105. [ C(OP_WRITE) ] = {
  1106. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
  1107. [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
  1108. },
  1109. [ C(OP_PREFETCH) ] = {
  1110. [ C(RESULT_ACCESS) ] = 0x0,
  1111. [ C(RESULT_MISS) ] = 0x0,
  1112. },
  1113. },
  1114. [ C(ITLB) ] = {
  1115. [ C(OP_READ) ] = {
  1116. [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
  1117. [ C(RESULT_MISS) ] = 0x20c8, /* ITLB_MISS_RETIRED */
  1118. },
  1119. [ C(OP_WRITE) ] = {
  1120. [ C(RESULT_ACCESS) ] = -1,
  1121. [ C(RESULT_MISS) ] = -1,
  1122. },
  1123. [ C(OP_PREFETCH) ] = {
  1124. [ C(RESULT_ACCESS) ] = -1,
  1125. [ C(RESULT_MISS) ] = -1,
  1126. },
  1127. },
  1128. [ C(BPU ) ] = {
  1129. [ C(OP_READ) ] = {
  1130. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
  1131. [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
  1132. },
  1133. [ C(OP_WRITE) ] = {
  1134. [ C(RESULT_ACCESS) ] = -1,
  1135. [ C(RESULT_MISS) ] = -1,
  1136. },
  1137. [ C(OP_PREFETCH) ] = {
  1138. [ C(RESULT_ACCESS) ] = -1,
  1139. [ C(RESULT_MISS) ] = -1,
  1140. },
  1141. },
  1142. [ C(NODE) ] = {
  1143. [ C(OP_READ) ] = {
  1144. [ C(RESULT_ACCESS) ] = 0x01b7,
  1145. [ C(RESULT_MISS) ] = 0x01b7,
  1146. },
  1147. [ C(OP_WRITE) ] = {
  1148. [ C(RESULT_ACCESS) ] = 0x01b7,
  1149. [ C(RESULT_MISS) ] = 0x01b7,
  1150. },
  1151. [ C(OP_PREFETCH) ] = {
  1152. [ C(RESULT_ACCESS) ] = 0x01b7,
  1153. [ C(RESULT_MISS) ] = 0x01b7,
  1154. },
  1155. },
  1156. };
  1157. static __initconst const u64 core2_hw_cache_event_ids
  1158. [PERF_COUNT_HW_CACHE_MAX]
  1159. [PERF_COUNT_HW_CACHE_OP_MAX]
  1160. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  1161. {
  1162. [ C(L1D) ] = {
  1163. [ C(OP_READ) ] = {
  1164. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
  1165. [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
  1166. },
  1167. [ C(OP_WRITE) ] = {
  1168. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
  1169. [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
  1170. },
  1171. [ C(OP_PREFETCH) ] = {
  1172. [ C(RESULT_ACCESS) ] = 0x104e, /* L1D_PREFETCH.REQUESTS */
  1173. [ C(RESULT_MISS) ] = 0,
  1174. },
  1175. },
  1176. [ C(L1I ) ] = {
  1177. [ C(OP_READ) ] = {
  1178. [ C(RESULT_ACCESS) ] = 0x0080, /* L1I.READS */
  1179. [ C(RESULT_MISS) ] = 0x0081, /* L1I.MISSES */
  1180. },
  1181. [ C(OP_WRITE) ] = {
  1182. [ C(RESULT_ACCESS) ] = -1,
  1183. [ C(RESULT_MISS) ] = -1,
  1184. },
  1185. [ C(OP_PREFETCH) ] = {
  1186. [ C(RESULT_ACCESS) ] = 0,
  1187. [ C(RESULT_MISS) ] = 0,
  1188. },
  1189. },
  1190. [ C(LL ) ] = {
  1191. [ C(OP_READ) ] = {
  1192. [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
  1193. [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
  1194. },
  1195. [ C(OP_WRITE) ] = {
  1196. [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
  1197. [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
  1198. },
  1199. [ C(OP_PREFETCH) ] = {
  1200. [ C(RESULT_ACCESS) ] = 0,
  1201. [ C(RESULT_MISS) ] = 0,
  1202. },
  1203. },
  1204. [ C(DTLB) ] = {
  1205. [ C(OP_READ) ] = {
  1206. [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
  1207. [ C(RESULT_MISS) ] = 0x0208, /* DTLB_MISSES.MISS_LD */
  1208. },
  1209. [ C(OP_WRITE) ] = {
  1210. [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
  1211. [ C(RESULT_MISS) ] = 0x0808, /* DTLB_MISSES.MISS_ST */
  1212. },
  1213. [ C(OP_PREFETCH) ] = {
  1214. [ C(RESULT_ACCESS) ] = 0,
  1215. [ C(RESULT_MISS) ] = 0,
  1216. },
  1217. },
  1218. [ C(ITLB) ] = {
  1219. [ C(OP_READ) ] = {
  1220. [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
  1221. [ C(RESULT_MISS) ] = 0x1282, /* ITLBMISSES */
  1222. },
  1223. [ C(OP_WRITE) ] = {
  1224. [ C(RESULT_ACCESS) ] = -1,
  1225. [ C(RESULT_MISS) ] = -1,
  1226. },
  1227. [ C(OP_PREFETCH) ] = {
  1228. [ C(RESULT_ACCESS) ] = -1,
  1229. [ C(RESULT_MISS) ] = -1,
  1230. },
  1231. },
  1232. [ C(BPU ) ] = {
  1233. [ C(OP_READ) ] = {
  1234. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
  1235. [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
  1236. },
  1237. [ C(OP_WRITE) ] = {
  1238. [ C(RESULT_ACCESS) ] = -1,
  1239. [ C(RESULT_MISS) ] = -1,
  1240. },
  1241. [ C(OP_PREFETCH) ] = {
  1242. [ C(RESULT_ACCESS) ] = -1,
  1243. [ C(RESULT_MISS) ] = -1,
  1244. },
  1245. },
  1246. };
  1247. static __initconst const u64 atom_hw_cache_event_ids
  1248. [PERF_COUNT_HW_CACHE_MAX]
  1249. [PERF_COUNT_HW_CACHE_OP_MAX]
  1250. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  1251. {
  1252. [ C(L1D) ] = {
  1253. [ C(OP_READ) ] = {
  1254. [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE.LD */
  1255. [ C(RESULT_MISS) ] = 0,
  1256. },
  1257. [ C(OP_WRITE) ] = {
  1258. [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE.ST */
  1259. [ C(RESULT_MISS) ] = 0,
  1260. },
  1261. [ C(OP_PREFETCH) ] = {
  1262. [ C(RESULT_ACCESS) ] = 0x0,
  1263. [ C(RESULT_MISS) ] = 0,
  1264. },
  1265. },
  1266. [ C(L1I ) ] = {
  1267. [ C(OP_READ) ] = {
  1268. [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
  1269. [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
  1270. },
  1271. [ C(OP_WRITE) ] = {
  1272. [ C(RESULT_ACCESS) ] = -1,
  1273. [ C(RESULT_MISS) ] = -1,
  1274. },
  1275. [ C(OP_PREFETCH) ] = {
  1276. [ C(RESULT_ACCESS) ] = 0,
  1277. [ C(RESULT_MISS) ] = 0,
  1278. },
  1279. },
  1280. [ C(LL ) ] = {
  1281. [ C(OP_READ) ] = {
  1282. [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
  1283. [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
  1284. },
  1285. [ C(OP_WRITE) ] = {
  1286. [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
  1287. [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
  1288. },
  1289. [ C(OP_PREFETCH) ] = {
  1290. [ C(RESULT_ACCESS) ] = 0,
  1291. [ C(RESULT_MISS) ] = 0,
  1292. },
  1293. },
  1294. [ C(DTLB) ] = {
  1295. [ C(OP_READ) ] = {
  1296. [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE_LD.MESI (alias) */
  1297. [ C(RESULT_MISS) ] = 0x0508, /* DTLB_MISSES.MISS_LD */
  1298. },
  1299. [ C(OP_WRITE) ] = {
  1300. [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE_ST.MESI (alias) */
  1301. [ C(RESULT_MISS) ] = 0x0608, /* DTLB_MISSES.MISS_ST */
  1302. },
  1303. [ C(OP_PREFETCH) ] = {
  1304. [ C(RESULT_ACCESS) ] = 0,
  1305. [ C(RESULT_MISS) ] = 0,
  1306. },
  1307. },
  1308. [ C(ITLB) ] = {
  1309. [ C(OP_READ) ] = {
  1310. [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
  1311. [ C(RESULT_MISS) ] = 0x0282, /* ITLB.MISSES */
  1312. },
  1313. [ C(OP_WRITE) ] = {
  1314. [ C(RESULT_ACCESS) ] = -1,
  1315. [ C(RESULT_MISS) ] = -1,
  1316. },
  1317. [ C(OP_PREFETCH) ] = {
  1318. [ C(RESULT_ACCESS) ] = -1,
  1319. [ C(RESULT_MISS) ] = -1,
  1320. },
  1321. },
  1322. [ C(BPU ) ] = {
  1323. [ C(OP_READ) ] = {
  1324. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
  1325. [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
  1326. },
  1327. [ C(OP_WRITE) ] = {
  1328. [ C(RESULT_ACCESS) ] = -1,
  1329. [ C(RESULT_MISS) ] = -1,
  1330. },
  1331. [ C(OP_PREFETCH) ] = {
  1332. [ C(RESULT_ACCESS) ] = -1,
  1333. [ C(RESULT_MISS) ] = -1,
  1334. },
  1335. },
  1336. };
  1337. EVENT_ATTR_STR(topdown-total-slots, td_total_slots_slm, "event=0x3c");
  1338. EVENT_ATTR_STR(topdown-total-slots.scale, td_total_slots_scale_slm, "2");
  1339. /* no_alloc_cycles.not_delivered */
  1340. EVENT_ATTR_STR(topdown-fetch-bubbles, td_fetch_bubbles_slm,
  1341. "event=0xca,umask=0x50");
  1342. EVENT_ATTR_STR(topdown-fetch-bubbles.scale, td_fetch_bubbles_scale_slm, "2");
  1343. /* uops_retired.all */
  1344. EVENT_ATTR_STR(topdown-slots-issued, td_slots_issued_slm,
  1345. "event=0xc2,umask=0x10");
  1346. /* uops_retired.all */
  1347. EVENT_ATTR_STR(topdown-slots-retired, td_slots_retired_slm,
  1348. "event=0xc2,umask=0x10");
  1349. static struct attribute *slm_events_attrs[] = {
  1350. EVENT_PTR(td_total_slots_slm),
  1351. EVENT_PTR(td_total_slots_scale_slm),
  1352. EVENT_PTR(td_fetch_bubbles_slm),
  1353. EVENT_PTR(td_fetch_bubbles_scale_slm),
  1354. EVENT_PTR(td_slots_issued_slm),
  1355. EVENT_PTR(td_slots_retired_slm),
  1356. NULL
  1357. };
  1358. static struct extra_reg intel_slm_extra_regs[] __read_mostly =
  1359. {
  1360. /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
  1361. INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x768005ffffull, RSP_0),
  1362. INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0x368005ffffull, RSP_1),
  1363. EVENT_EXTRA_END
  1364. };
  1365. #define SLM_DMND_READ SNB_DMND_DATA_RD
  1366. #define SLM_DMND_WRITE SNB_DMND_RFO
  1367. #define SLM_DMND_PREFETCH (SNB_PF_DATA_RD|SNB_PF_RFO)
  1368. #define SLM_SNP_ANY (SNB_SNP_NONE|SNB_SNP_MISS|SNB_NO_FWD|SNB_HITM)
  1369. #define SLM_LLC_ACCESS SNB_RESP_ANY
  1370. #define SLM_LLC_MISS (SLM_SNP_ANY|SNB_NON_DRAM)
  1371. static __initconst const u64 slm_hw_cache_extra_regs
  1372. [PERF_COUNT_HW_CACHE_MAX]
  1373. [PERF_COUNT_HW_CACHE_OP_MAX]
  1374. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  1375. {
  1376. [ C(LL ) ] = {
  1377. [ C(OP_READ) ] = {
  1378. [ C(RESULT_ACCESS) ] = SLM_DMND_READ|SLM_LLC_ACCESS,
  1379. [ C(RESULT_MISS) ] = 0,
  1380. },
  1381. [ C(OP_WRITE) ] = {
  1382. [ C(RESULT_ACCESS) ] = SLM_DMND_WRITE|SLM_LLC_ACCESS,
  1383. [ C(RESULT_MISS) ] = SLM_DMND_WRITE|SLM_LLC_MISS,
  1384. },
  1385. [ C(OP_PREFETCH) ] = {
  1386. [ C(RESULT_ACCESS) ] = SLM_DMND_PREFETCH|SLM_LLC_ACCESS,
  1387. [ C(RESULT_MISS) ] = SLM_DMND_PREFETCH|SLM_LLC_MISS,
  1388. },
  1389. },
  1390. };
  1391. static __initconst const u64 slm_hw_cache_event_ids
  1392. [PERF_COUNT_HW_CACHE_MAX]
  1393. [PERF_COUNT_HW_CACHE_OP_MAX]
  1394. [PERF_COUNT_HW_CACHE_RESULT_MAX] =
  1395. {
  1396. [ C(L1D) ] = {
  1397. [ C(OP_READ) ] = {
  1398. [ C(RESULT_ACCESS) ] = 0,
  1399. [ C(RESULT_MISS) ] = 0x0104, /* LD_DCU_MISS */
  1400. },
  1401. [ C(OP_WRITE) ] = {
  1402. [ C(RESULT_ACCESS) ] = 0,
  1403. [ C(RESULT_MISS) ] = 0,
  1404. },
  1405. [ C(OP_PREFETCH) ] = {
  1406. [ C(RESULT_ACCESS) ] = 0,
  1407. [ C(RESULT_MISS) ] = 0,
  1408. },
  1409. },
  1410. [ C(L1I ) ] = {
  1411. [ C(OP_READ) ] = {
  1412. [ C(RESULT_ACCESS) ] = 0x0380, /* ICACHE.ACCESSES */
  1413. [ C(RESULT_MISS) ] = 0x0280, /* ICACGE.MISSES */
  1414. },
  1415. [ C(OP_WRITE) ] = {
  1416. [ C(RESULT_ACCESS) ] = -1,
  1417. [ C(RESULT_MISS) ] = -1,
  1418. },
  1419. [ C(OP_PREFETCH) ] = {
  1420. [ C(RESULT_ACCESS) ] = 0,
  1421. [ C(RESULT_MISS) ] = 0,
  1422. },
  1423. },
  1424. [ C(LL ) ] = {
  1425. [ C(OP_READ) ] = {
  1426. /* OFFCORE_RESPONSE.ANY_DATA.LOCAL_CACHE */
  1427. [ C(RESULT_ACCESS) ] = 0x01b7,
  1428. [ C(RESULT_MISS) ] = 0,
  1429. },
  1430. [ C(OP_WRITE) ] = {
  1431. /* OFFCORE_RESPONSE.ANY_RFO.LOCAL_CACHE */
  1432. [ C(RESULT_ACCESS) ] = 0x01b7,
  1433. /* OFFCORE_RESPONSE.ANY_RFO.ANY_LLC_MISS */
  1434. [ C(RESULT_MISS) ] = 0x01b7,
  1435. },
  1436. [ C(OP_PREFETCH) ] = {
  1437. /* OFFCORE_RESPONSE.PREFETCH.LOCAL_CACHE */
  1438. [ C(RESULT_ACCESS) ] = 0x01b7,
  1439. /* OFFCORE_RESPONSE.PREFETCH.ANY_LLC_MISS */
  1440. [ C(RESULT_MISS) ] = 0x01b7,
  1441. },
  1442. },
  1443. [ C(DTLB) ] = {
  1444. [ C(OP_READ) ] = {
  1445. [ C(RESULT_ACCESS) ] = 0,
  1446. [ C(RESULT_MISS) ] = 0x0804, /* LD_DTLB_MISS */
  1447. },
  1448. [ C(OP_WRITE) ] = {
  1449. [ C(RESULT_ACCESS) ] = 0,
  1450. [ C(RESULT_MISS) ] = 0,
  1451. },
  1452. [ C(OP_PREFETCH) ] = {
  1453. [ C(RESULT_ACCESS) ] = 0,
  1454. [ C(RESULT_MISS) ] = 0,
  1455. },
  1456. },
  1457. [ C(ITLB) ] = {
  1458. [ C(OP_READ) ] = {
  1459. [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
  1460. [ C(RESULT_MISS) ] = 0x40205, /* PAGE_WALKS.I_SIDE_WALKS */
  1461. },
  1462. [ C(OP_WRITE) ] = {
  1463. [ C(RESULT_ACCESS) ] = -1,
  1464. [ C(RESULT_MISS) ] = -1,
  1465. },
  1466. [ C(OP_PREFETCH) ] = {
  1467. [ C(RESULT_ACCESS) ] = -1,
  1468. [ C(RESULT_MISS) ] = -1,
  1469. },
  1470. },
  1471. [ C(BPU ) ] = {
  1472. [ C(OP_READ) ] = {
  1473. [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
  1474. [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
  1475. },
  1476. [ C(OP_WRITE) ] = {
  1477. [ C(RESULT_ACCESS) ] = -1,
  1478. [ C(RESULT_MISS) ] = -1,
  1479. },
  1480. [ C(OP_PREFETCH) ] = {
  1481. [ C(RESULT_ACCESS) ] = -1,
  1482. [ C(RESULT_MISS) ] = -1,
  1483. },
  1484. },
  1485. };
  1486. EVENT_ATTR_STR(topdown-total-slots, td_total_slots_glm, "event=0x3c");
  1487. EVENT_ATTR_STR(topdown-total-slots.scale, td_total_slots_scale_glm, "3");
  1488. /* UOPS_NOT_DELIVERED.ANY */
  1489. EVENT_ATTR_STR(topdown-fetch-bubbles, td_fetch_bubbles_glm, "event=0x9c");
  1490. /* ISSUE_SLOTS_NOT_CONSUMED.RECOVERY */
  1491. EVENT_ATTR_STR(topdown-recovery-bubbles, td_recovery_bubbles_glm, "event=0xca,umask=0x02");
  1492. /* UOPS_RETIRED.ANY */
  1493. EVENT_ATTR_STR(topdown-slots-retired, td_slots_retired_glm, "event=0xc2");
  1494. /* UOPS_ISSUED.ANY */
  1495. EVENT_ATTR_STR(topdown-slots-issued, td_slots_issued_glm, "event=0x0e");
  1496. static struct attribute *glm_events_attrs[] = {
  1497. EVENT_PTR(td_total_slots_glm),
  1498. EVENT_PTR(td_total_slots_scale_glm),
  1499. EVENT_PTR(td_fetch_bubbles_glm),
  1500. EVENT_PTR(td_recovery_bubbles_glm),
  1501. EVENT_PTR(td_slots_issued_glm),
  1502. EVENT_PTR(td_slots_retired_glm),
  1503. NULL
  1504. };
  1505. static struct extra_reg intel_glm_extra_regs[] __read_mostly = {
  1506. /* must define OFFCORE_RSP_X first, see intel_fixup_er() */
  1507. INTEL_UEVENT_EXTRA_REG(0x01b7, MSR_OFFCORE_RSP_0, 0x760005ffbfull, RSP_0),
  1508. INTEL_UEVENT_EXTRA_REG(0x02b7, MSR_OFFCORE_RSP_1, 0x360005ffbfull, RSP_1),
  1509. EVENT_EXTRA_END
  1510. };
  1511. #define GLM_DEMAND_DATA_RD BIT_ULL(0)
  1512. #define GLM_DEMAND_RFO BIT_ULL(1)
  1513. #define GLM_ANY_RESPONSE BIT_ULL(16)
  1514. #define GLM_SNP_NONE_OR_MISS BIT_ULL(33)
  1515. #define GLM_DEMAND_READ GLM_DEMAND_DATA_RD
  1516. #define GLM_DEMAND_WRITE GLM_DEMAND_RFO
  1517. #define GLM_DEMAND_PREFETCH (SNB_PF_DATA_RD|SNB_PF_RFO)
  1518. #define GLM_LLC_ACCESS GLM_ANY_RESPONSE
  1519. #define GLM_SNP_ANY (GLM_SNP_NONE_OR_MISS|SNB_NO_FWD|SNB_HITM)
  1520. #define GLM_LLC_MISS (GLM_SNP_ANY|SNB_NON_DRAM)
  1521. static __initconst const u64 glm_hw_cache_event_ids
  1522. [PERF_COUNT_HW_CACHE_MAX]
  1523. [PERF_COUNT_HW_CACHE_OP_MAX]
  1524. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  1525. [C(L1D)] = {
  1526. [C(OP_READ)] = {
  1527. [C(RESULT_ACCESS)] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
  1528. [C(RESULT_MISS)] = 0x0,
  1529. },
  1530. [C(OP_WRITE)] = {
  1531. [C(RESULT_ACCESS)] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
  1532. [C(RESULT_MISS)] = 0x0,
  1533. },
  1534. [C(OP_PREFETCH)] = {
  1535. [C(RESULT_ACCESS)] = 0x0,
  1536. [C(RESULT_MISS)] = 0x0,
  1537. },
  1538. },
  1539. [C(L1I)] = {
  1540. [C(OP_READ)] = {
  1541. [C(RESULT_ACCESS)] = 0x0380, /* ICACHE.ACCESSES */
  1542. [C(RESULT_MISS)] = 0x0280, /* ICACHE.MISSES */
  1543. },
  1544. [C(OP_WRITE)] = {
  1545. [C(RESULT_ACCESS)] = -1,
  1546. [C(RESULT_MISS)] = -1,
  1547. },
  1548. [C(OP_PREFETCH)] = {
  1549. [C(RESULT_ACCESS)] = 0x0,
  1550. [C(RESULT_MISS)] = 0x0,
  1551. },
  1552. },
  1553. [C(LL)] = {
  1554. [C(OP_READ)] = {
  1555. [C(RESULT_ACCESS)] = 0x1b7, /* OFFCORE_RESPONSE */
  1556. [C(RESULT_MISS)] = 0x1b7, /* OFFCORE_RESPONSE */
  1557. },
  1558. [C(OP_WRITE)] = {
  1559. [C(RESULT_ACCESS)] = 0x1b7, /* OFFCORE_RESPONSE */
  1560. [C(RESULT_MISS)] = 0x1b7, /* OFFCORE_RESPONSE */
  1561. },
  1562. [C(OP_PREFETCH)] = {
  1563. [C(RESULT_ACCESS)] = 0x1b7, /* OFFCORE_RESPONSE */
  1564. [C(RESULT_MISS)] = 0x1b7, /* OFFCORE_RESPONSE */
  1565. },
  1566. },
  1567. [C(DTLB)] = {
  1568. [C(OP_READ)] = {
  1569. [C(RESULT_ACCESS)] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
  1570. [C(RESULT_MISS)] = 0x0,
  1571. },
  1572. [C(OP_WRITE)] = {
  1573. [C(RESULT_ACCESS)] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
  1574. [C(RESULT_MISS)] = 0x0,
  1575. },
  1576. [C(OP_PREFETCH)] = {
  1577. [C(RESULT_ACCESS)] = 0x0,
  1578. [C(RESULT_MISS)] = 0x0,
  1579. },
  1580. },
  1581. [C(ITLB)] = {
  1582. [C(OP_READ)] = {
  1583. [C(RESULT_ACCESS)] = 0x00c0, /* INST_RETIRED.ANY_P */
  1584. [C(RESULT_MISS)] = 0x0481, /* ITLB.MISS */
  1585. },
  1586. [C(OP_WRITE)] = {
  1587. [C(RESULT_ACCESS)] = -1,
  1588. [C(RESULT_MISS)] = -1,
  1589. },
  1590. [C(OP_PREFETCH)] = {
  1591. [C(RESULT_ACCESS)] = -1,
  1592. [C(RESULT_MISS)] = -1,
  1593. },
  1594. },
  1595. [C(BPU)] = {
  1596. [C(OP_READ)] = {
  1597. [C(RESULT_ACCESS)] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
  1598. [C(RESULT_MISS)] = 0x00c5, /* BR_MISP_RETIRED.ALL_BRANCHES */
  1599. },
  1600. [C(OP_WRITE)] = {
  1601. [C(RESULT_ACCESS)] = -1,
  1602. [C(RESULT_MISS)] = -1,
  1603. },
  1604. [C(OP_PREFETCH)] = {
  1605. [C(RESULT_ACCESS)] = -1,
  1606. [C(RESULT_MISS)] = -1,
  1607. },
  1608. },
  1609. };
  1610. static __initconst const u64 glm_hw_cache_extra_regs
  1611. [PERF_COUNT_HW_CACHE_MAX]
  1612. [PERF_COUNT_HW_CACHE_OP_MAX]
  1613. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  1614. [C(LL)] = {
  1615. [C(OP_READ)] = {
  1616. [C(RESULT_ACCESS)] = GLM_DEMAND_READ|
  1617. GLM_LLC_ACCESS,
  1618. [C(RESULT_MISS)] = GLM_DEMAND_READ|
  1619. GLM_LLC_MISS,
  1620. },
  1621. [C(OP_WRITE)] = {
  1622. [C(RESULT_ACCESS)] = GLM_DEMAND_WRITE|
  1623. GLM_LLC_ACCESS,
  1624. [C(RESULT_MISS)] = GLM_DEMAND_WRITE|
  1625. GLM_LLC_MISS,
  1626. },
  1627. [C(OP_PREFETCH)] = {
  1628. [C(RESULT_ACCESS)] = GLM_DEMAND_PREFETCH|
  1629. GLM_LLC_ACCESS,
  1630. [C(RESULT_MISS)] = GLM_DEMAND_PREFETCH|
  1631. GLM_LLC_MISS,
  1632. },
  1633. },
  1634. };
  1635. static __initconst const u64 glp_hw_cache_event_ids
  1636. [PERF_COUNT_HW_CACHE_MAX]
  1637. [PERF_COUNT_HW_CACHE_OP_MAX]
  1638. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  1639. [C(L1D)] = {
  1640. [C(OP_READ)] = {
  1641. [C(RESULT_ACCESS)] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
  1642. [C(RESULT_MISS)] = 0x0,
  1643. },
  1644. [C(OP_WRITE)] = {
  1645. [C(RESULT_ACCESS)] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
  1646. [C(RESULT_MISS)] = 0x0,
  1647. },
  1648. [C(OP_PREFETCH)] = {
  1649. [C(RESULT_ACCESS)] = 0x0,
  1650. [C(RESULT_MISS)] = 0x0,
  1651. },
  1652. },
  1653. [C(L1I)] = {
  1654. [C(OP_READ)] = {
  1655. [C(RESULT_ACCESS)] = 0x0380, /* ICACHE.ACCESSES */
  1656. [C(RESULT_MISS)] = 0x0280, /* ICACHE.MISSES */
  1657. },
  1658. [C(OP_WRITE)] = {
  1659. [C(RESULT_ACCESS)] = -1,
  1660. [C(RESULT_MISS)] = -1,
  1661. },
  1662. [C(OP_PREFETCH)] = {
  1663. [C(RESULT_ACCESS)] = 0x0,
  1664. [C(RESULT_MISS)] = 0x0,
  1665. },
  1666. },
  1667. [C(LL)] = {
  1668. [C(OP_READ)] = {
  1669. [C(RESULT_ACCESS)] = 0x1b7, /* OFFCORE_RESPONSE */
  1670. [C(RESULT_MISS)] = 0x1b7, /* OFFCORE_RESPONSE */
  1671. },
  1672. [C(OP_WRITE)] = {
  1673. [C(RESULT_ACCESS)] = 0x1b7, /* OFFCORE_RESPONSE */
  1674. [C(RESULT_MISS)] = 0x1b7, /* OFFCORE_RESPONSE */
  1675. },
  1676. [C(OP_PREFETCH)] = {
  1677. [C(RESULT_ACCESS)] = 0x0,
  1678. [C(RESULT_MISS)] = 0x0,
  1679. },
  1680. },
  1681. [C(DTLB)] = {
  1682. [C(OP_READ)] = {
  1683. [C(RESULT_ACCESS)] = 0x81d0, /* MEM_UOPS_RETIRED.ALL_LOADS */
  1684. [C(RESULT_MISS)] = 0xe08, /* DTLB_LOAD_MISSES.WALK_COMPLETED */
  1685. },
  1686. [C(OP_WRITE)] = {
  1687. [C(RESULT_ACCESS)] = 0x82d0, /* MEM_UOPS_RETIRED.ALL_STORES */
  1688. [C(RESULT_MISS)] = 0xe49, /* DTLB_STORE_MISSES.WALK_COMPLETED */
  1689. },
  1690. [C(OP_PREFETCH)] = {
  1691. [C(RESULT_ACCESS)] = 0x0,
  1692. [C(RESULT_MISS)] = 0x0,
  1693. },
  1694. },
  1695. [C(ITLB)] = {
  1696. [C(OP_READ)] = {
  1697. [C(RESULT_ACCESS)] = 0x00c0, /* INST_RETIRED.ANY_P */
  1698. [C(RESULT_MISS)] = 0x0481, /* ITLB.MISS */
  1699. },
  1700. [C(OP_WRITE)] = {
  1701. [C(RESULT_ACCESS)] = -1,
  1702. [C(RESULT_MISS)] = -1,
  1703. },
  1704. [C(OP_PREFETCH)] = {
  1705. [C(RESULT_ACCESS)] = -1,
  1706. [C(RESULT_MISS)] = -1,
  1707. },
  1708. },
  1709. [C(BPU)] = {
  1710. [C(OP_READ)] = {
  1711. [C(RESULT_ACCESS)] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
  1712. [C(RESULT_MISS)] = 0x00c5, /* BR_MISP_RETIRED.ALL_BRANCHES */
  1713. },
  1714. [C(OP_WRITE)] = {
  1715. [C(RESULT_ACCESS)] = -1,
  1716. [C(RESULT_MISS)] = -1,
  1717. },
  1718. [C(OP_PREFETCH)] = {
  1719. [C(RESULT_ACCESS)] = -1,
  1720. [C(RESULT_MISS)] = -1,
  1721. },
  1722. },
  1723. };
  1724. static __initconst const u64 glp_hw_cache_extra_regs
  1725. [PERF_COUNT_HW_CACHE_MAX]
  1726. [PERF_COUNT_HW_CACHE_OP_MAX]
  1727. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  1728. [C(LL)] = {
  1729. [C(OP_READ)] = {
  1730. [C(RESULT_ACCESS)] = GLM_DEMAND_READ|
  1731. GLM_LLC_ACCESS,
  1732. [C(RESULT_MISS)] = GLM_DEMAND_READ|
  1733. GLM_LLC_MISS,
  1734. },
  1735. [C(OP_WRITE)] = {
  1736. [C(RESULT_ACCESS)] = GLM_DEMAND_WRITE|
  1737. GLM_LLC_ACCESS,
  1738. [C(RESULT_MISS)] = GLM_DEMAND_WRITE|
  1739. GLM_LLC_MISS,
  1740. },
  1741. [C(OP_PREFETCH)] = {
  1742. [C(RESULT_ACCESS)] = 0x0,
  1743. [C(RESULT_MISS)] = 0x0,
  1744. },
  1745. },
  1746. };
  1747. #define KNL_OT_L2_HITE BIT_ULL(19) /* Other Tile L2 Hit */
  1748. #define KNL_OT_L2_HITF BIT_ULL(20) /* Other Tile L2 Hit */
  1749. #define KNL_MCDRAM_LOCAL BIT_ULL(21)
  1750. #define KNL_MCDRAM_FAR BIT_ULL(22)
  1751. #define KNL_DDR_LOCAL BIT_ULL(23)
  1752. #define KNL_DDR_FAR BIT_ULL(24)
  1753. #define KNL_DRAM_ANY (KNL_MCDRAM_LOCAL | KNL_MCDRAM_FAR | \
  1754. KNL_DDR_LOCAL | KNL_DDR_FAR)
  1755. #define KNL_L2_READ SLM_DMND_READ
  1756. #define KNL_L2_WRITE SLM_DMND_WRITE
  1757. #define KNL_L2_PREFETCH SLM_DMND_PREFETCH
  1758. #define KNL_L2_ACCESS SLM_LLC_ACCESS
  1759. #define KNL_L2_MISS (KNL_OT_L2_HITE | KNL_OT_L2_HITF | \
  1760. KNL_DRAM_ANY | SNB_SNP_ANY | \
  1761. SNB_NON_DRAM)
  1762. static __initconst const u64 knl_hw_cache_extra_regs
  1763. [PERF_COUNT_HW_CACHE_MAX]
  1764. [PERF_COUNT_HW_CACHE_OP_MAX]
  1765. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  1766. [C(LL)] = {
  1767. [C(OP_READ)] = {
  1768. [C(RESULT_ACCESS)] = KNL_L2_READ | KNL_L2_ACCESS,
  1769. [C(RESULT_MISS)] = 0,
  1770. },
  1771. [C(OP_WRITE)] = {
  1772. [C(RESULT_ACCESS)] = KNL_L2_WRITE | KNL_L2_ACCESS,
  1773. [C(RESULT_MISS)] = KNL_L2_WRITE | KNL_L2_MISS,
  1774. },
  1775. [C(OP_PREFETCH)] = {
  1776. [C(RESULT_ACCESS)] = KNL_L2_PREFETCH | KNL_L2_ACCESS,
  1777. [C(RESULT_MISS)] = KNL_L2_PREFETCH | KNL_L2_MISS,
  1778. },
  1779. },
  1780. };
  1781. /*
  1782. * Used from PMIs where the LBRs are already disabled.
  1783. *
  1784. * This function could be called consecutively. It is required to remain in
  1785. * disabled state if called consecutively.
  1786. *
  1787. * During consecutive calls, the same disable value will be written to related
  1788. * registers, so the PMU state remains unchanged.
  1789. *
  1790. * intel_bts events don't coexist with intel PMU's BTS events because of
  1791. * x86_add_exclusive(x86_lbr_exclusive_lbr); there's no need to keep them
  1792. * disabled around intel PMU's event batching etc, only inside the PMI handler.
  1793. */
  1794. static void __intel_pmu_disable_all(void)
  1795. {
  1796. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1797. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  1798. if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask))
  1799. intel_pmu_disable_bts();
  1800. intel_pmu_pebs_disable_all();
  1801. }
  1802. static void intel_pmu_disable_all(void)
  1803. {
  1804. __intel_pmu_disable_all();
  1805. intel_pmu_lbr_disable_all();
  1806. }
  1807. static void __intel_pmu_enable_all(int added, bool pmi)
  1808. {
  1809. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1810. intel_pmu_pebs_enable_all();
  1811. intel_pmu_lbr_enable_all(pmi);
  1812. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL,
  1813. x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_guest_mask);
  1814. if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask)) {
  1815. struct perf_event *event =
  1816. cpuc->events[INTEL_PMC_IDX_FIXED_BTS];
  1817. if (WARN_ON_ONCE(!event))
  1818. return;
  1819. intel_pmu_enable_bts(event->hw.config);
  1820. }
  1821. }
  1822. static void intel_pmu_enable_all(int added)
  1823. {
  1824. __intel_pmu_enable_all(added, false);
  1825. }
  1826. /*
  1827. * Workaround for:
  1828. * Intel Errata AAK100 (model 26)
  1829. * Intel Errata AAP53 (model 30)
  1830. * Intel Errata BD53 (model 44)
  1831. *
  1832. * The official story:
  1833. * These chips need to be 'reset' when adding counters by programming the
  1834. * magic three (non-counting) events 0x4300B5, 0x4300D2, and 0x4300B1 either
  1835. * in sequence on the same PMC or on different PMCs.
  1836. *
  1837. * In practise it appears some of these events do in fact count, and
  1838. * we need to programm all 4 events.
  1839. */
  1840. static void intel_pmu_nhm_workaround(void)
  1841. {
  1842. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1843. static const unsigned long nhm_magic[4] = {
  1844. 0x4300B5,
  1845. 0x4300D2,
  1846. 0x4300B1,
  1847. 0x4300B1
  1848. };
  1849. struct perf_event *event;
  1850. int i;
  1851. /*
  1852. * The Errata requires below steps:
  1853. * 1) Clear MSR_IA32_PEBS_ENABLE and MSR_CORE_PERF_GLOBAL_CTRL;
  1854. * 2) Configure 4 PERFEVTSELx with the magic events and clear
  1855. * the corresponding PMCx;
  1856. * 3) set bit0~bit3 of MSR_CORE_PERF_GLOBAL_CTRL;
  1857. * 4) Clear MSR_CORE_PERF_GLOBAL_CTRL;
  1858. * 5) Clear 4 pairs of ERFEVTSELx and PMCx;
  1859. */
  1860. /*
  1861. * The real steps we choose are a little different from above.
  1862. * A) To reduce MSR operations, we don't run step 1) as they
  1863. * are already cleared before this function is called;
  1864. * B) Call x86_perf_event_update to save PMCx before configuring
  1865. * PERFEVTSELx with magic number;
  1866. * C) With step 5), we do clear only when the PERFEVTSELx is
  1867. * not used currently.
  1868. * D) Call x86_perf_event_set_period to restore PMCx;
  1869. */
  1870. /* We always operate 4 pairs of PERF Counters */
  1871. for (i = 0; i < 4; i++) {
  1872. event = cpuc->events[i];
  1873. if (event)
  1874. x86_perf_event_update(event);
  1875. }
  1876. for (i = 0; i < 4; i++) {
  1877. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + i, nhm_magic[i]);
  1878. wrmsrl(MSR_ARCH_PERFMON_PERFCTR0 + i, 0x0);
  1879. }
  1880. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0xf);
  1881. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0x0);
  1882. for (i = 0; i < 4; i++) {
  1883. event = cpuc->events[i];
  1884. if (event) {
  1885. x86_perf_event_set_period(event);
  1886. __x86_pmu_enable_event(&event->hw,
  1887. ARCH_PERFMON_EVENTSEL_ENABLE);
  1888. } else
  1889. wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + i, 0x0);
  1890. }
  1891. }
  1892. static void intel_pmu_nhm_enable_all(int added)
  1893. {
  1894. if (added)
  1895. intel_pmu_nhm_workaround();
  1896. intel_pmu_enable_all(added);
  1897. }
  1898. static void enable_counter_freeze(void)
  1899. {
  1900. update_debugctlmsr(get_debugctlmsr() |
  1901. DEBUGCTLMSR_FREEZE_PERFMON_ON_PMI);
  1902. }
  1903. static void disable_counter_freeze(void)
  1904. {
  1905. update_debugctlmsr(get_debugctlmsr() &
  1906. ~DEBUGCTLMSR_FREEZE_PERFMON_ON_PMI);
  1907. }
  1908. static inline u64 intel_pmu_get_status(void)
  1909. {
  1910. u64 status;
  1911. rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
  1912. return status;
  1913. }
  1914. static inline void intel_pmu_ack_status(u64 ack)
  1915. {
  1916. wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
  1917. }
  1918. static void intel_pmu_disable_fixed(struct hw_perf_event *hwc)
  1919. {
  1920. int idx = hwc->idx - INTEL_PMC_IDX_FIXED;
  1921. u64 ctrl_val, mask;
  1922. mask = 0xfULL << (idx * 4);
  1923. rdmsrl(hwc->config_base, ctrl_val);
  1924. ctrl_val &= ~mask;
  1925. wrmsrl(hwc->config_base, ctrl_val);
  1926. }
  1927. static inline bool event_is_checkpointed(struct perf_event *event)
  1928. {
  1929. return (event->hw.config & HSW_IN_TX_CHECKPOINTED) != 0;
  1930. }
  1931. static void intel_pmu_disable_event(struct perf_event *event)
  1932. {
  1933. struct hw_perf_event *hwc = &event->hw;
  1934. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1935. if (unlikely(hwc->idx == INTEL_PMC_IDX_FIXED_BTS)) {
  1936. intel_pmu_disable_bts();
  1937. intel_pmu_drain_bts_buffer();
  1938. return;
  1939. }
  1940. cpuc->intel_ctrl_guest_mask &= ~(1ull << hwc->idx);
  1941. cpuc->intel_ctrl_host_mask &= ~(1ull << hwc->idx);
  1942. cpuc->intel_cp_status &= ~(1ull << hwc->idx);
  1943. if (unlikely(event->attr.precise_ip))
  1944. intel_pmu_pebs_disable(event);
  1945. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  1946. intel_pmu_disable_fixed(hwc);
  1947. return;
  1948. }
  1949. x86_pmu_disable_event(event);
  1950. }
  1951. static void intel_pmu_del_event(struct perf_event *event)
  1952. {
  1953. if (needs_branch_stack(event))
  1954. intel_pmu_lbr_del(event);
  1955. if (event->attr.precise_ip)
  1956. intel_pmu_pebs_del(event);
  1957. }
  1958. static void intel_pmu_read_event(struct perf_event *event)
  1959. {
  1960. if (event->hw.flags & PERF_X86_EVENT_AUTO_RELOAD)
  1961. intel_pmu_auto_reload_read(event);
  1962. else
  1963. x86_perf_event_update(event);
  1964. }
  1965. static void intel_pmu_enable_fixed(struct perf_event *event)
  1966. {
  1967. struct hw_perf_event *hwc = &event->hw;
  1968. int idx = hwc->idx - INTEL_PMC_IDX_FIXED;
  1969. u64 ctrl_val, mask, bits = 0;
  1970. /*
  1971. * Enable IRQ generation (0x8), if not PEBS,
  1972. * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
  1973. * if requested:
  1974. */
  1975. if (!event->attr.precise_ip)
  1976. bits |= 0x8;
  1977. if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
  1978. bits |= 0x2;
  1979. if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
  1980. bits |= 0x1;
  1981. /*
  1982. * ANY bit is supported in v3 and up
  1983. */
  1984. if (x86_pmu.version > 2 && hwc->config & ARCH_PERFMON_EVENTSEL_ANY)
  1985. bits |= 0x4;
  1986. bits <<= (idx * 4);
  1987. mask = 0xfULL << (idx * 4);
  1988. rdmsrl(hwc->config_base, ctrl_val);
  1989. ctrl_val &= ~mask;
  1990. ctrl_val |= bits;
  1991. wrmsrl(hwc->config_base, ctrl_val);
  1992. }
  1993. static void intel_pmu_enable_event(struct perf_event *event)
  1994. {
  1995. struct hw_perf_event *hwc = &event->hw;
  1996. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  1997. if (unlikely(hwc->idx == INTEL_PMC_IDX_FIXED_BTS)) {
  1998. if (!__this_cpu_read(cpu_hw_events.enabled))
  1999. return;
  2000. intel_pmu_enable_bts(hwc->config);
  2001. return;
  2002. }
  2003. if (event->attr.exclude_host)
  2004. cpuc->intel_ctrl_guest_mask |= (1ull << hwc->idx);
  2005. if (event->attr.exclude_guest)
  2006. cpuc->intel_ctrl_host_mask |= (1ull << hwc->idx);
  2007. if (unlikely(event_is_checkpointed(event)))
  2008. cpuc->intel_cp_status |= (1ull << hwc->idx);
  2009. if (unlikely(event->attr.precise_ip))
  2010. intel_pmu_pebs_enable(event);
  2011. if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
  2012. intel_pmu_enable_fixed(event);
  2013. return;
  2014. }
  2015. __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
  2016. }
  2017. static void intel_pmu_add_event(struct perf_event *event)
  2018. {
  2019. if (event->attr.precise_ip)
  2020. intel_pmu_pebs_add(event);
  2021. if (needs_branch_stack(event))
  2022. intel_pmu_lbr_add(event);
  2023. }
  2024. /*
  2025. * Save and restart an expired event. Called by NMI contexts,
  2026. * so it has to be careful about preempting normal event ops:
  2027. */
  2028. int intel_pmu_save_and_restart(struct perf_event *event)
  2029. {
  2030. x86_perf_event_update(event);
  2031. /*
  2032. * For a checkpointed counter always reset back to 0. This
  2033. * avoids a situation where the counter overflows, aborts the
  2034. * transaction and is then set back to shortly before the
  2035. * overflow, and overflows and aborts again.
  2036. */
  2037. if (unlikely(event_is_checkpointed(event))) {
  2038. /* No race with NMIs because the counter should not be armed */
  2039. wrmsrl(event->hw.event_base, 0);
  2040. local64_set(&event->hw.prev_count, 0);
  2041. }
  2042. return x86_perf_event_set_period(event);
  2043. }
  2044. static void intel_pmu_reset(void)
  2045. {
  2046. struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
  2047. unsigned long flags;
  2048. int idx;
  2049. if (!x86_pmu.num_counters)
  2050. return;
  2051. local_irq_save(flags);
  2052. pr_info("clearing PMU state on CPU#%d\n", smp_processor_id());
  2053. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  2054. wrmsrl_safe(x86_pmu_config_addr(idx), 0ull);
  2055. wrmsrl_safe(x86_pmu_event_addr(idx), 0ull);
  2056. }
  2057. for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++)
  2058. wrmsrl_safe(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull);
  2059. if (ds)
  2060. ds->bts_index = ds->bts_buffer_base;
  2061. /* Ack all overflows and disable fixed counters */
  2062. if (x86_pmu.version >= 2) {
  2063. intel_pmu_ack_status(intel_pmu_get_status());
  2064. wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
  2065. }
  2066. /* Reset LBRs and LBR freezing */
  2067. if (x86_pmu.lbr_nr) {
  2068. update_debugctlmsr(get_debugctlmsr() &
  2069. ~(DEBUGCTLMSR_FREEZE_LBRS_ON_PMI|DEBUGCTLMSR_LBR));
  2070. }
  2071. local_irq_restore(flags);
  2072. }
  2073. static int handle_pmi_common(struct pt_regs *regs, u64 status)
  2074. {
  2075. struct perf_sample_data data;
  2076. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  2077. int bit;
  2078. int handled = 0;
  2079. inc_irq_stat(apic_perf_irqs);
  2080. /*
  2081. * Ignore a range of extra bits in status that do not indicate
  2082. * overflow by themselves.
  2083. */
  2084. status &= ~(GLOBAL_STATUS_COND_CHG |
  2085. GLOBAL_STATUS_ASIF |
  2086. GLOBAL_STATUS_LBRS_FROZEN);
  2087. if (!status)
  2088. return 0;
  2089. /*
  2090. * In case multiple PEBS events are sampled at the same time,
  2091. * it is possible to have GLOBAL_STATUS bit 62 set indicating
  2092. * PEBS buffer overflow and also seeing at most 3 PEBS counters
  2093. * having their bits set in the status register. This is a sign
  2094. * that there was at least one PEBS record pending at the time
  2095. * of the PMU interrupt. PEBS counters must only be processed
  2096. * via the drain_pebs() calls and not via the regular sample
  2097. * processing loop coming after that the function, otherwise
  2098. * phony regular samples may be generated in the sampling buffer
  2099. * not marked with the EXACT tag. Another possibility is to have
  2100. * one PEBS event and at least one non-PEBS event whic hoverflows
  2101. * while PEBS has armed. In this case, bit 62 of GLOBAL_STATUS will
  2102. * not be set, yet the overflow status bit for the PEBS counter will
  2103. * be on Skylake.
  2104. *
  2105. * To avoid this problem, we systematically ignore the PEBS-enabled
  2106. * counters from the GLOBAL_STATUS mask and we always process PEBS
  2107. * events via drain_pebs().
  2108. */
  2109. if (x86_pmu.flags & PMU_FL_PEBS_ALL)
  2110. status &= ~cpuc->pebs_enabled;
  2111. else
  2112. status &= ~(cpuc->pebs_enabled & PEBS_COUNTER_MASK);
  2113. /*
  2114. * PEBS overflow sets bit 62 in the global status register
  2115. */
  2116. if (__test_and_clear_bit(62, (unsigned long *)&status)) {
  2117. handled++;
  2118. x86_pmu.drain_pebs(regs);
  2119. status &= x86_pmu.intel_ctrl | GLOBAL_STATUS_TRACE_TOPAPMI;
  2120. }
  2121. /*
  2122. * Intel PT
  2123. */
  2124. if (__test_and_clear_bit(55, (unsigned long *)&status)) {
  2125. handled++;
  2126. intel_pt_interrupt();
  2127. }
  2128. /*
  2129. * Checkpointed counters can lead to 'spurious' PMIs because the
  2130. * rollback caused by the PMI will have cleared the overflow status
  2131. * bit. Therefore always force probe these counters.
  2132. */
  2133. status |= cpuc->intel_cp_status;
  2134. for_each_set_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
  2135. struct perf_event *event = cpuc->events[bit];
  2136. handled++;
  2137. if (!test_bit(bit, cpuc->active_mask))
  2138. continue;
  2139. if (!intel_pmu_save_and_restart(event))
  2140. continue;
  2141. perf_sample_data_init(&data, 0, event->hw.last_period);
  2142. if (has_branch_stack(event))
  2143. data.br_stack = &cpuc->lbr_stack;
  2144. if (perf_event_overflow(event, &data, regs))
  2145. x86_pmu_stop(event, 0);
  2146. }
  2147. return handled;
  2148. }
  2149. static bool disable_counter_freezing;
  2150. static int __init intel_perf_counter_freezing_setup(char *s)
  2151. {
  2152. disable_counter_freezing = true;
  2153. pr_info("Intel PMU Counter freezing feature disabled\n");
  2154. return 1;
  2155. }
  2156. __setup("disable_counter_freezing", intel_perf_counter_freezing_setup);
  2157. /*
  2158. * Simplified handler for Arch Perfmon v4:
  2159. * - We rely on counter freezing/unfreezing to enable/disable the PMU.
  2160. * This is done automatically on PMU ack.
  2161. * - Ack the PMU only after the APIC.
  2162. */
  2163. static int intel_pmu_handle_irq_v4(struct pt_regs *regs)
  2164. {
  2165. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  2166. int handled = 0;
  2167. bool bts = false;
  2168. u64 status;
  2169. int pmu_enabled = cpuc->enabled;
  2170. int loops = 0;
  2171. /* PMU has been disabled because of counter freezing */
  2172. cpuc->enabled = 0;
  2173. if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask)) {
  2174. bts = true;
  2175. intel_bts_disable_local();
  2176. handled = intel_pmu_drain_bts_buffer();
  2177. handled += intel_bts_interrupt();
  2178. }
  2179. status = intel_pmu_get_status();
  2180. if (!status)
  2181. goto done;
  2182. again:
  2183. intel_pmu_lbr_read();
  2184. if (++loops > 100) {
  2185. static bool warned;
  2186. if (!warned) {
  2187. WARN(1, "perfevents: irq loop stuck!\n");
  2188. perf_event_print_debug();
  2189. warned = true;
  2190. }
  2191. intel_pmu_reset();
  2192. goto done;
  2193. }
  2194. handled += handle_pmi_common(regs, status);
  2195. done:
  2196. /* Ack the PMI in the APIC */
  2197. apic_write(APIC_LVTPC, APIC_DM_NMI);
  2198. /*
  2199. * The counters start counting immediately while ack the status.
  2200. * Make it as close as possible to IRET. This avoids bogus
  2201. * freezing on Skylake CPUs.
  2202. */
  2203. if (status) {
  2204. intel_pmu_ack_status(status);
  2205. } else {
  2206. /*
  2207. * CPU may issues two PMIs very close to each other.
  2208. * When the PMI handler services the first one, the
  2209. * GLOBAL_STATUS is already updated to reflect both.
  2210. * When it IRETs, the second PMI is immediately
  2211. * handled and it sees clear status. At the meantime,
  2212. * there may be a third PMI, because the freezing bit
  2213. * isn't set since the ack in first PMI handlers.
  2214. * Double check if there is more work to be done.
  2215. */
  2216. status = intel_pmu_get_status();
  2217. if (status)
  2218. goto again;
  2219. }
  2220. if (bts)
  2221. intel_bts_enable_local();
  2222. cpuc->enabled = pmu_enabled;
  2223. return handled;
  2224. }
  2225. /*
  2226. * This handler is triggered by the local APIC, so the APIC IRQ handling
  2227. * rules apply:
  2228. */
  2229. static int intel_pmu_handle_irq(struct pt_regs *regs)
  2230. {
  2231. struct cpu_hw_events *cpuc;
  2232. int loops;
  2233. u64 status;
  2234. int handled;
  2235. int pmu_enabled;
  2236. cpuc = this_cpu_ptr(&cpu_hw_events);
  2237. /*
  2238. * Save the PMU state.
  2239. * It needs to be restored when leaving the handler.
  2240. */
  2241. pmu_enabled = cpuc->enabled;
  2242. /*
  2243. * No known reason to not always do late ACK,
  2244. * but just in case do it opt-in.
  2245. */
  2246. if (!x86_pmu.late_ack)
  2247. apic_write(APIC_LVTPC, APIC_DM_NMI);
  2248. intel_bts_disable_local();
  2249. cpuc->enabled = 0;
  2250. __intel_pmu_disable_all();
  2251. handled = intel_pmu_drain_bts_buffer();
  2252. handled += intel_bts_interrupt();
  2253. status = intel_pmu_get_status();
  2254. if (!status)
  2255. goto done;
  2256. loops = 0;
  2257. again:
  2258. intel_pmu_lbr_read();
  2259. intel_pmu_ack_status(status);
  2260. if (++loops > 100) {
  2261. static bool warned;
  2262. if (!warned) {
  2263. WARN(1, "perfevents: irq loop stuck!\n");
  2264. perf_event_print_debug();
  2265. warned = true;
  2266. }
  2267. intel_pmu_reset();
  2268. goto done;
  2269. }
  2270. handled += handle_pmi_common(regs, status);
  2271. /*
  2272. * Repeat if there is more work to be done:
  2273. */
  2274. status = intel_pmu_get_status();
  2275. if (status)
  2276. goto again;
  2277. done:
  2278. /* Only restore PMU state when it's active. See x86_pmu_disable(). */
  2279. cpuc->enabled = pmu_enabled;
  2280. if (pmu_enabled)
  2281. __intel_pmu_enable_all(0, true);
  2282. intel_bts_enable_local();
  2283. /*
  2284. * Only unmask the NMI after the overflow counters
  2285. * have been reset. This avoids spurious NMIs on
  2286. * Haswell CPUs.
  2287. */
  2288. if (x86_pmu.late_ack)
  2289. apic_write(APIC_LVTPC, APIC_DM_NMI);
  2290. return handled;
  2291. }
  2292. static struct event_constraint *
  2293. intel_bts_constraints(struct perf_event *event)
  2294. {
  2295. struct hw_perf_event *hwc = &event->hw;
  2296. unsigned int hw_event, bts_event;
  2297. if (event->attr.freq)
  2298. return NULL;
  2299. hw_event = hwc->config & INTEL_ARCH_EVENT_MASK;
  2300. bts_event = x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS);
  2301. if (unlikely(hw_event == bts_event && hwc->sample_period == 1))
  2302. return &bts_constraint;
  2303. return NULL;
  2304. }
  2305. static int intel_alt_er(int idx, u64 config)
  2306. {
  2307. int alt_idx = idx;
  2308. if (!(x86_pmu.flags & PMU_FL_HAS_RSP_1))
  2309. return idx;
  2310. if (idx == EXTRA_REG_RSP_0)
  2311. alt_idx = EXTRA_REG_RSP_1;
  2312. if (idx == EXTRA_REG_RSP_1)
  2313. alt_idx = EXTRA_REG_RSP_0;
  2314. if (config & ~x86_pmu.extra_regs[alt_idx].valid_mask)
  2315. return idx;
  2316. return alt_idx;
  2317. }
  2318. static void intel_fixup_er(struct perf_event *event, int idx)
  2319. {
  2320. event->hw.extra_reg.idx = idx;
  2321. if (idx == EXTRA_REG_RSP_0) {
  2322. event->hw.config &= ~INTEL_ARCH_EVENT_MASK;
  2323. event->hw.config |= x86_pmu.extra_regs[EXTRA_REG_RSP_0].event;
  2324. event->hw.extra_reg.reg = MSR_OFFCORE_RSP_0;
  2325. } else if (idx == EXTRA_REG_RSP_1) {
  2326. event->hw.config &= ~INTEL_ARCH_EVENT_MASK;
  2327. event->hw.config |= x86_pmu.extra_regs[EXTRA_REG_RSP_1].event;
  2328. event->hw.extra_reg.reg = MSR_OFFCORE_RSP_1;
  2329. }
  2330. }
  2331. /*
  2332. * manage allocation of shared extra msr for certain events
  2333. *
  2334. * sharing can be:
  2335. * per-cpu: to be shared between the various events on a single PMU
  2336. * per-core: per-cpu + shared by HT threads
  2337. */
  2338. static struct event_constraint *
  2339. __intel_shared_reg_get_constraints(struct cpu_hw_events *cpuc,
  2340. struct perf_event *event,
  2341. struct hw_perf_event_extra *reg)
  2342. {
  2343. struct event_constraint *c = &emptyconstraint;
  2344. struct er_account *era;
  2345. unsigned long flags;
  2346. int idx = reg->idx;
  2347. /*
  2348. * reg->alloc can be set due to existing state, so for fake cpuc we
  2349. * need to ignore this, otherwise we might fail to allocate proper fake
  2350. * state for this extra reg constraint. Also see the comment below.
  2351. */
  2352. if (reg->alloc && !cpuc->is_fake)
  2353. return NULL; /* call x86_get_event_constraint() */
  2354. again:
  2355. era = &cpuc->shared_regs->regs[idx];
  2356. /*
  2357. * we use spin_lock_irqsave() to avoid lockdep issues when
  2358. * passing a fake cpuc
  2359. */
  2360. raw_spin_lock_irqsave(&era->lock, flags);
  2361. if (!atomic_read(&era->ref) || era->config == reg->config) {
  2362. /*
  2363. * If its a fake cpuc -- as per validate_{group,event}() we
  2364. * shouldn't touch event state and we can avoid doing so
  2365. * since both will only call get_event_constraints() once
  2366. * on each event, this avoids the need for reg->alloc.
  2367. *
  2368. * Not doing the ER fixup will only result in era->reg being
  2369. * wrong, but since we won't actually try and program hardware
  2370. * this isn't a problem either.
  2371. */
  2372. if (!cpuc->is_fake) {
  2373. if (idx != reg->idx)
  2374. intel_fixup_er(event, idx);
  2375. /*
  2376. * x86_schedule_events() can call get_event_constraints()
  2377. * multiple times on events in the case of incremental
  2378. * scheduling(). reg->alloc ensures we only do the ER
  2379. * allocation once.
  2380. */
  2381. reg->alloc = 1;
  2382. }
  2383. /* lock in msr value */
  2384. era->config = reg->config;
  2385. era->reg = reg->reg;
  2386. /* one more user */
  2387. atomic_inc(&era->ref);
  2388. /*
  2389. * need to call x86_get_event_constraint()
  2390. * to check if associated event has constraints
  2391. */
  2392. c = NULL;
  2393. } else {
  2394. idx = intel_alt_er(idx, reg->config);
  2395. if (idx != reg->idx) {
  2396. raw_spin_unlock_irqrestore(&era->lock, flags);
  2397. goto again;
  2398. }
  2399. }
  2400. raw_spin_unlock_irqrestore(&era->lock, flags);
  2401. return c;
  2402. }
  2403. static void
  2404. __intel_shared_reg_put_constraints(struct cpu_hw_events *cpuc,
  2405. struct hw_perf_event_extra *reg)
  2406. {
  2407. struct er_account *era;
  2408. /*
  2409. * Only put constraint if extra reg was actually allocated. Also takes
  2410. * care of event which do not use an extra shared reg.
  2411. *
  2412. * Also, if this is a fake cpuc we shouldn't touch any event state
  2413. * (reg->alloc) and we don't care about leaving inconsistent cpuc state
  2414. * either since it'll be thrown out.
  2415. */
  2416. if (!reg->alloc || cpuc->is_fake)
  2417. return;
  2418. era = &cpuc->shared_regs->regs[reg->idx];
  2419. /* one fewer user */
  2420. atomic_dec(&era->ref);
  2421. /* allocate again next time */
  2422. reg->alloc = 0;
  2423. }
  2424. static struct event_constraint *
  2425. intel_shared_regs_constraints(struct cpu_hw_events *cpuc,
  2426. struct perf_event *event)
  2427. {
  2428. struct event_constraint *c = NULL, *d;
  2429. struct hw_perf_event_extra *xreg, *breg;
  2430. xreg = &event->hw.extra_reg;
  2431. if (xreg->idx != EXTRA_REG_NONE) {
  2432. c = __intel_shared_reg_get_constraints(cpuc, event, xreg);
  2433. if (c == &emptyconstraint)
  2434. return c;
  2435. }
  2436. breg = &event->hw.branch_reg;
  2437. if (breg->idx != EXTRA_REG_NONE) {
  2438. d = __intel_shared_reg_get_constraints(cpuc, event, breg);
  2439. if (d == &emptyconstraint) {
  2440. __intel_shared_reg_put_constraints(cpuc, xreg);
  2441. c = d;
  2442. }
  2443. }
  2444. return c;
  2445. }
  2446. struct event_constraint *
  2447. x86_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
  2448. struct perf_event *event)
  2449. {
  2450. struct event_constraint *c;
  2451. if (x86_pmu.event_constraints) {
  2452. for_each_event_constraint(c, x86_pmu.event_constraints) {
  2453. if ((event->hw.config & c->cmask) == c->code) {
  2454. event->hw.flags |= c->flags;
  2455. return c;
  2456. }
  2457. }
  2458. }
  2459. return &unconstrained;
  2460. }
  2461. static struct event_constraint *
  2462. __intel_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
  2463. struct perf_event *event)
  2464. {
  2465. struct event_constraint *c;
  2466. c = intel_bts_constraints(event);
  2467. if (c)
  2468. return c;
  2469. c = intel_shared_regs_constraints(cpuc, event);
  2470. if (c)
  2471. return c;
  2472. c = intel_pebs_constraints(event);
  2473. if (c)
  2474. return c;
  2475. return x86_get_event_constraints(cpuc, idx, event);
  2476. }
  2477. static void
  2478. intel_start_scheduling(struct cpu_hw_events *cpuc)
  2479. {
  2480. struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
  2481. struct intel_excl_states *xl;
  2482. int tid = cpuc->excl_thread_id;
  2483. /*
  2484. * nothing needed if in group validation mode
  2485. */
  2486. if (cpuc->is_fake || !is_ht_workaround_enabled())
  2487. return;
  2488. /*
  2489. * no exclusion needed
  2490. */
  2491. if (WARN_ON_ONCE(!excl_cntrs))
  2492. return;
  2493. xl = &excl_cntrs->states[tid];
  2494. xl->sched_started = true;
  2495. /*
  2496. * lock shared state until we are done scheduling
  2497. * in stop_event_scheduling()
  2498. * makes scheduling appear as a transaction
  2499. */
  2500. raw_spin_lock(&excl_cntrs->lock);
  2501. }
  2502. static void intel_commit_scheduling(struct cpu_hw_events *cpuc, int idx, int cntr)
  2503. {
  2504. struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
  2505. struct event_constraint *c = cpuc->event_constraint[idx];
  2506. struct intel_excl_states *xl;
  2507. int tid = cpuc->excl_thread_id;
  2508. if (cpuc->is_fake || !is_ht_workaround_enabled())
  2509. return;
  2510. if (WARN_ON_ONCE(!excl_cntrs))
  2511. return;
  2512. if (!(c->flags & PERF_X86_EVENT_DYNAMIC))
  2513. return;
  2514. xl = &excl_cntrs->states[tid];
  2515. lockdep_assert_held(&excl_cntrs->lock);
  2516. if (c->flags & PERF_X86_EVENT_EXCL)
  2517. xl->state[cntr] = INTEL_EXCL_EXCLUSIVE;
  2518. else
  2519. xl->state[cntr] = INTEL_EXCL_SHARED;
  2520. }
  2521. static void
  2522. intel_stop_scheduling(struct cpu_hw_events *cpuc)
  2523. {
  2524. struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
  2525. struct intel_excl_states *xl;
  2526. int tid = cpuc->excl_thread_id;
  2527. /*
  2528. * nothing needed if in group validation mode
  2529. */
  2530. if (cpuc->is_fake || !is_ht_workaround_enabled())
  2531. return;
  2532. /*
  2533. * no exclusion needed
  2534. */
  2535. if (WARN_ON_ONCE(!excl_cntrs))
  2536. return;
  2537. xl = &excl_cntrs->states[tid];
  2538. xl->sched_started = false;
  2539. /*
  2540. * release shared state lock (acquired in intel_start_scheduling())
  2541. */
  2542. raw_spin_unlock(&excl_cntrs->lock);
  2543. }
  2544. static struct event_constraint *
  2545. intel_get_excl_constraints(struct cpu_hw_events *cpuc, struct perf_event *event,
  2546. int idx, struct event_constraint *c)
  2547. {
  2548. struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
  2549. struct intel_excl_states *xlo;
  2550. int tid = cpuc->excl_thread_id;
  2551. int is_excl, i;
  2552. /*
  2553. * validating a group does not require
  2554. * enforcing cross-thread exclusion
  2555. */
  2556. if (cpuc->is_fake || !is_ht_workaround_enabled())
  2557. return c;
  2558. /*
  2559. * no exclusion needed
  2560. */
  2561. if (WARN_ON_ONCE(!excl_cntrs))
  2562. return c;
  2563. /*
  2564. * because we modify the constraint, we need
  2565. * to make a copy. Static constraints come
  2566. * from static const tables.
  2567. *
  2568. * only needed when constraint has not yet
  2569. * been cloned (marked dynamic)
  2570. */
  2571. if (!(c->flags & PERF_X86_EVENT_DYNAMIC)) {
  2572. struct event_constraint *cx;
  2573. /*
  2574. * grab pre-allocated constraint entry
  2575. */
  2576. cx = &cpuc->constraint_list[idx];
  2577. /*
  2578. * initialize dynamic constraint
  2579. * with static constraint
  2580. */
  2581. *cx = *c;
  2582. /*
  2583. * mark constraint as dynamic, so we
  2584. * can free it later on
  2585. */
  2586. cx->flags |= PERF_X86_EVENT_DYNAMIC;
  2587. c = cx;
  2588. }
  2589. /*
  2590. * From here on, the constraint is dynamic.
  2591. * Either it was just allocated above, or it
  2592. * was allocated during a earlier invocation
  2593. * of this function
  2594. */
  2595. /*
  2596. * state of sibling HT
  2597. */
  2598. xlo = &excl_cntrs->states[tid ^ 1];
  2599. /*
  2600. * event requires exclusive counter access
  2601. * across HT threads
  2602. */
  2603. is_excl = c->flags & PERF_X86_EVENT_EXCL;
  2604. if (is_excl && !(event->hw.flags & PERF_X86_EVENT_EXCL_ACCT)) {
  2605. event->hw.flags |= PERF_X86_EVENT_EXCL_ACCT;
  2606. if (!cpuc->n_excl++)
  2607. WRITE_ONCE(excl_cntrs->has_exclusive[tid], 1);
  2608. }
  2609. /*
  2610. * Modify static constraint with current dynamic
  2611. * state of thread
  2612. *
  2613. * EXCLUSIVE: sibling counter measuring exclusive event
  2614. * SHARED : sibling counter measuring non-exclusive event
  2615. * UNUSED : sibling counter unused
  2616. */
  2617. for_each_set_bit(i, c->idxmsk, X86_PMC_IDX_MAX) {
  2618. /*
  2619. * exclusive event in sibling counter
  2620. * our corresponding counter cannot be used
  2621. * regardless of our event
  2622. */
  2623. if (xlo->state[i] == INTEL_EXCL_EXCLUSIVE)
  2624. __clear_bit(i, c->idxmsk);
  2625. /*
  2626. * if measuring an exclusive event, sibling
  2627. * measuring non-exclusive, then counter cannot
  2628. * be used
  2629. */
  2630. if (is_excl && xlo->state[i] == INTEL_EXCL_SHARED)
  2631. __clear_bit(i, c->idxmsk);
  2632. }
  2633. /*
  2634. * recompute actual bit weight for scheduling algorithm
  2635. */
  2636. c->weight = hweight64(c->idxmsk64);
  2637. /*
  2638. * if we return an empty mask, then switch
  2639. * back to static empty constraint to avoid
  2640. * the cost of freeing later on
  2641. */
  2642. if (c->weight == 0)
  2643. c = &emptyconstraint;
  2644. return c;
  2645. }
  2646. static struct event_constraint *
  2647. intel_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
  2648. struct perf_event *event)
  2649. {
  2650. struct event_constraint *c1 = NULL;
  2651. struct event_constraint *c2;
  2652. if (idx >= 0) /* fake does < 0 */
  2653. c1 = cpuc->event_constraint[idx];
  2654. /*
  2655. * first time only
  2656. * - static constraint: no change across incremental scheduling calls
  2657. * - dynamic constraint: handled by intel_get_excl_constraints()
  2658. */
  2659. c2 = __intel_get_event_constraints(cpuc, idx, event);
  2660. if (c1 && (c1->flags & PERF_X86_EVENT_DYNAMIC)) {
  2661. bitmap_copy(c1->idxmsk, c2->idxmsk, X86_PMC_IDX_MAX);
  2662. c1->weight = c2->weight;
  2663. c2 = c1;
  2664. }
  2665. if (cpuc->excl_cntrs)
  2666. return intel_get_excl_constraints(cpuc, event, idx, c2);
  2667. return c2;
  2668. }
  2669. static void intel_put_excl_constraints(struct cpu_hw_events *cpuc,
  2670. struct perf_event *event)
  2671. {
  2672. struct hw_perf_event *hwc = &event->hw;
  2673. struct intel_excl_cntrs *excl_cntrs = cpuc->excl_cntrs;
  2674. int tid = cpuc->excl_thread_id;
  2675. struct intel_excl_states *xl;
  2676. /*
  2677. * nothing needed if in group validation mode
  2678. */
  2679. if (cpuc->is_fake)
  2680. return;
  2681. if (WARN_ON_ONCE(!excl_cntrs))
  2682. return;
  2683. if (hwc->flags & PERF_X86_EVENT_EXCL_ACCT) {
  2684. hwc->flags &= ~PERF_X86_EVENT_EXCL_ACCT;
  2685. if (!--cpuc->n_excl)
  2686. WRITE_ONCE(excl_cntrs->has_exclusive[tid], 0);
  2687. }
  2688. /*
  2689. * If event was actually assigned, then mark the counter state as
  2690. * unused now.
  2691. */
  2692. if (hwc->idx >= 0) {
  2693. xl = &excl_cntrs->states[tid];
  2694. /*
  2695. * put_constraint may be called from x86_schedule_events()
  2696. * which already has the lock held so here make locking
  2697. * conditional.
  2698. */
  2699. if (!xl->sched_started)
  2700. raw_spin_lock(&excl_cntrs->lock);
  2701. xl->state[hwc->idx] = INTEL_EXCL_UNUSED;
  2702. if (!xl->sched_started)
  2703. raw_spin_unlock(&excl_cntrs->lock);
  2704. }
  2705. }
  2706. static void
  2707. intel_put_shared_regs_event_constraints(struct cpu_hw_events *cpuc,
  2708. struct perf_event *event)
  2709. {
  2710. struct hw_perf_event_extra *reg;
  2711. reg = &event->hw.extra_reg;
  2712. if (reg->idx != EXTRA_REG_NONE)
  2713. __intel_shared_reg_put_constraints(cpuc, reg);
  2714. reg = &event->hw.branch_reg;
  2715. if (reg->idx != EXTRA_REG_NONE)
  2716. __intel_shared_reg_put_constraints(cpuc, reg);
  2717. }
  2718. static void intel_put_event_constraints(struct cpu_hw_events *cpuc,
  2719. struct perf_event *event)
  2720. {
  2721. intel_put_shared_regs_event_constraints(cpuc, event);
  2722. /*
  2723. * is PMU has exclusive counter restrictions, then
  2724. * all events are subject to and must call the
  2725. * put_excl_constraints() routine
  2726. */
  2727. if (cpuc->excl_cntrs)
  2728. intel_put_excl_constraints(cpuc, event);
  2729. }
  2730. static void intel_pebs_aliases_core2(struct perf_event *event)
  2731. {
  2732. if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
  2733. /*
  2734. * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
  2735. * (0x003c) so that we can use it with PEBS.
  2736. *
  2737. * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
  2738. * PEBS capable. However we can use INST_RETIRED.ANY_P
  2739. * (0x00c0), which is a PEBS capable event, to get the same
  2740. * count.
  2741. *
  2742. * INST_RETIRED.ANY_P counts the number of cycles that retires
  2743. * CNTMASK instructions. By setting CNTMASK to a value (16)
  2744. * larger than the maximum number of instructions that can be
  2745. * retired per cycle (4) and then inverting the condition, we
  2746. * count all cycles that retire 16 or less instructions, which
  2747. * is every cycle.
  2748. *
  2749. * Thereby we gain a PEBS capable cycle counter.
  2750. */
  2751. u64 alt_config = X86_CONFIG(.event=0xc0, .inv=1, .cmask=16);
  2752. alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
  2753. event->hw.config = alt_config;
  2754. }
  2755. }
  2756. static void intel_pebs_aliases_snb(struct perf_event *event)
  2757. {
  2758. if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
  2759. /*
  2760. * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
  2761. * (0x003c) so that we can use it with PEBS.
  2762. *
  2763. * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
  2764. * PEBS capable. However we can use UOPS_RETIRED.ALL
  2765. * (0x01c2), which is a PEBS capable event, to get the same
  2766. * count.
  2767. *
  2768. * UOPS_RETIRED.ALL counts the number of cycles that retires
  2769. * CNTMASK micro-ops. By setting CNTMASK to a value (16)
  2770. * larger than the maximum number of micro-ops that can be
  2771. * retired per cycle (4) and then inverting the condition, we
  2772. * count all cycles that retire 16 or less micro-ops, which
  2773. * is every cycle.
  2774. *
  2775. * Thereby we gain a PEBS capable cycle counter.
  2776. */
  2777. u64 alt_config = X86_CONFIG(.event=0xc2, .umask=0x01, .inv=1, .cmask=16);
  2778. alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
  2779. event->hw.config = alt_config;
  2780. }
  2781. }
  2782. static void intel_pebs_aliases_precdist(struct perf_event *event)
  2783. {
  2784. if ((event->hw.config & X86_RAW_EVENT_MASK) == 0x003c) {
  2785. /*
  2786. * Use an alternative encoding for CPU_CLK_UNHALTED.THREAD_P
  2787. * (0x003c) so that we can use it with PEBS.
  2788. *
  2789. * The regular CPU_CLK_UNHALTED.THREAD_P event (0x003c) isn't
  2790. * PEBS capable. However we can use INST_RETIRED.PREC_DIST
  2791. * (0x01c0), which is a PEBS capable event, to get the same
  2792. * count.
  2793. *
  2794. * The PREC_DIST event has special support to minimize sample
  2795. * shadowing effects. One drawback is that it can be
  2796. * only programmed on counter 1, but that seems like an
  2797. * acceptable trade off.
  2798. */
  2799. u64 alt_config = X86_CONFIG(.event=0xc0, .umask=0x01, .inv=1, .cmask=16);
  2800. alt_config |= (event->hw.config & ~X86_RAW_EVENT_MASK);
  2801. event->hw.config = alt_config;
  2802. }
  2803. }
  2804. static void intel_pebs_aliases_ivb(struct perf_event *event)
  2805. {
  2806. if (event->attr.precise_ip < 3)
  2807. return intel_pebs_aliases_snb(event);
  2808. return intel_pebs_aliases_precdist(event);
  2809. }
  2810. static void intel_pebs_aliases_skl(struct perf_event *event)
  2811. {
  2812. if (event->attr.precise_ip < 3)
  2813. return intel_pebs_aliases_core2(event);
  2814. return intel_pebs_aliases_precdist(event);
  2815. }
  2816. static unsigned long intel_pmu_large_pebs_flags(struct perf_event *event)
  2817. {
  2818. unsigned long flags = x86_pmu.large_pebs_flags;
  2819. if (event->attr.use_clockid)
  2820. flags &= ~PERF_SAMPLE_TIME;
  2821. if (!event->attr.exclude_kernel)
  2822. flags &= ~PERF_SAMPLE_REGS_USER;
  2823. if (event->attr.sample_regs_user & ~PEBS_REGS)
  2824. flags &= ~(PERF_SAMPLE_REGS_USER | PERF_SAMPLE_REGS_INTR);
  2825. return flags;
  2826. }
  2827. static int intel_pmu_hw_config(struct perf_event *event)
  2828. {
  2829. int ret = x86_pmu_hw_config(event);
  2830. if (ret)
  2831. return ret;
  2832. if (event->attr.precise_ip) {
  2833. if (!event->attr.freq) {
  2834. event->hw.flags |= PERF_X86_EVENT_AUTO_RELOAD;
  2835. if (!(event->attr.sample_type &
  2836. ~intel_pmu_large_pebs_flags(event)))
  2837. event->hw.flags |= PERF_X86_EVENT_LARGE_PEBS;
  2838. }
  2839. if (x86_pmu.pebs_aliases)
  2840. x86_pmu.pebs_aliases(event);
  2841. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
  2842. event->attr.sample_type |= __PERF_SAMPLE_CALLCHAIN_EARLY;
  2843. }
  2844. if (needs_branch_stack(event)) {
  2845. ret = intel_pmu_setup_lbr_filter(event);
  2846. if (ret)
  2847. return ret;
  2848. /*
  2849. * BTS is set up earlier in this path, so don't account twice
  2850. */
  2851. if (!intel_pmu_has_bts(event)) {
  2852. /* disallow lbr if conflicting events are present */
  2853. if (x86_add_exclusive(x86_lbr_exclusive_lbr))
  2854. return -EBUSY;
  2855. event->destroy = hw_perf_lbr_event_destroy;
  2856. }
  2857. }
  2858. if (event->attr.type != PERF_TYPE_RAW)
  2859. return 0;
  2860. if (!(event->attr.config & ARCH_PERFMON_EVENTSEL_ANY))
  2861. return 0;
  2862. if (x86_pmu.version < 3)
  2863. return -EINVAL;
  2864. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  2865. return -EACCES;
  2866. event->hw.config |= ARCH_PERFMON_EVENTSEL_ANY;
  2867. return 0;
  2868. }
  2869. struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr)
  2870. {
  2871. if (x86_pmu.guest_get_msrs)
  2872. return x86_pmu.guest_get_msrs(nr);
  2873. *nr = 0;
  2874. return NULL;
  2875. }
  2876. EXPORT_SYMBOL_GPL(perf_guest_get_msrs);
  2877. static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr)
  2878. {
  2879. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  2880. struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;
  2881. arr[0].msr = MSR_CORE_PERF_GLOBAL_CTRL;
  2882. arr[0].host = x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_guest_mask;
  2883. arr[0].guest = x86_pmu.intel_ctrl & ~cpuc->intel_ctrl_host_mask;
  2884. /*
  2885. * If PMU counter has PEBS enabled it is not enough to disable counter
  2886. * on a guest entry since PEBS memory write can overshoot guest entry
  2887. * and corrupt guest memory. Disabling PEBS solves the problem.
  2888. */
  2889. arr[1].msr = MSR_IA32_PEBS_ENABLE;
  2890. arr[1].host = cpuc->pebs_enabled;
  2891. arr[1].guest = 0;
  2892. *nr = 2;
  2893. return arr;
  2894. }
  2895. static struct perf_guest_switch_msr *core_guest_get_msrs(int *nr)
  2896. {
  2897. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  2898. struct perf_guest_switch_msr *arr = cpuc->guest_switch_msrs;
  2899. int idx;
  2900. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  2901. struct perf_event *event = cpuc->events[idx];
  2902. arr[idx].msr = x86_pmu_config_addr(idx);
  2903. arr[idx].host = arr[idx].guest = 0;
  2904. if (!test_bit(idx, cpuc->active_mask))
  2905. continue;
  2906. arr[idx].host = arr[idx].guest =
  2907. event->hw.config | ARCH_PERFMON_EVENTSEL_ENABLE;
  2908. if (event->attr.exclude_host)
  2909. arr[idx].host &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  2910. else if (event->attr.exclude_guest)
  2911. arr[idx].guest &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
  2912. }
  2913. *nr = x86_pmu.num_counters;
  2914. return arr;
  2915. }
  2916. static void core_pmu_enable_event(struct perf_event *event)
  2917. {
  2918. if (!event->attr.exclude_host)
  2919. x86_pmu_enable_event(event);
  2920. }
  2921. static void core_pmu_enable_all(int added)
  2922. {
  2923. struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
  2924. int idx;
  2925. for (idx = 0; idx < x86_pmu.num_counters; idx++) {
  2926. struct hw_perf_event *hwc = &cpuc->events[idx]->hw;
  2927. if (!test_bit(idx, cpuc->active_mask) ||
  2928. cpuc->events[idx]->attr.exclude_host)
  2929. continue;
  2930. __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE);
  2931. }
  2932. }
  2933. static int hsw_hw_config(struct perf_event *event)
  2934. {
  2935. int ret = intel_pmu_hw_config(event);
  2936. if (ret)
  2937. return ret;
  2938. if (!boot_cpu_has(X86_FEATURE_RTM) && !boot_cpu_has(X86_FEATURE_HLE))
  2939. return 0;
  2940. event->hw.config |= event->attr.config & (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED);
  2941. /*
  2942. * IN_TX/IN_TX-CP filters are not supported by the Haswell PMU with
  2943. * PEBS or in ANY thread mode. Since the results are non-sensical forbid
  2944. * this combination.
  2945. */
  2946. if ((event->hw.config & (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED)) &&
  2947. ((event->hw.config & ARCH_PERFMON_EVENTSEL_ANY) ||
  2948. event->attr.precise_ip > 0))
  2949. return -EOPNOTSUPP;
  2950. if (event_is_checkpointed(event)) {
  2951. /*
  2952. * Sampling of checkpointed events can cause situations where
  2953. * the CPU constantly aborts because of a overflow, which is
  2954. * then checkpointed back and ignored. Forbid checkpointing
  2955. * for sampling.
  2956. *
  2957. * But still allow a long sampling period, so that perf stat
  2958. * from KVM works.
  2959. */
  2960. if (event->attr.sample_period > 0 &&
  2961. event->attr.sample_period < 0x7fffffff)
  2962. return -EOPNOTSUPP;
  2963. }
  2964. return 0;
  2965. }
  2966. static struct event_constraint counter0_constraint =
  2967. INTEL_ALL_EVENT_CONSTRAINT(0, 0x1);
  2968. static struct event_constraint counter2_constraint =
  2969. EVENT_CONSTRAINT(0, 0x4, 0);
  2970. static struct event_constraint *
  2971. hsw_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
  2972. struct perf_event *event)
  2973. {
  2974. struct event_constraint *c;
  2975. c = intel_get_event_constraints(cpuc, idx, event);
  2976. /* Handle special quirk on in_tx_checkpointed only in counter 2 */
  2977. if (event->hw.config & HSW_IN_TX_CHECKPOINTED) {
  2978. if (c->idxmsk64 & (1U << 2))
  2979. return &counter2_constraint;
  2980. return &emptyconstraint;
  2981. }
  2982. return c;
  2983. }
  2984. static struct event_constraint *
  2985. glp_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
  2986. struct perf_event *event)
  2987. {
  2988. struct event_constraint *c;
  2989. /* :ppp means to do reduced skid PEBS which is PMC0 only. */
  2990. if (event->attr.precise_ip == 3)
  2991. return &counter0_constraint;
  2992. c = intel_get_event_constraints(cpuc, idx, event);
  2993. return c;
  2994. }
  2995. /*
  2996. * Broadwell:
  2997. *
  2998. * The INST_RETIRED.ALL period always needs to have lowest 6 bits cleared
  2999. * (BDM55) and it must not use a period smaller than 100 (BDM11). We combine
  3000. * the two to enforce a minimum period of 128 (the smallest value that has bits
  3001. * 0-5 cleared and >= 100).
  3002. *
  3003. * Because of how the code in x86_perf_event_set_period() works, the truncation
  3004. * of the lower 6 bits is 'harmless' as we'll occasionally add a longer period
  3005. * to make up for the 'lost' events due to carrying the 'error' in period_left.
  3006. *
  3007. * Therefore the effective (average) period matches the requested period,
  3008. * despite coarser hardware granularity.
  3009. */
  3010. static u64 bdw_limit_period(struct perf_event *event, u64 left)
  3011. {
  3012. if ((event->hw.config & INTEL_ARCH_EVENT_MASK) ==
  3013. X86_CONFIG(.event=0xc0, .umask=0x01)) {
  3014. if (left < 128)
  3015. left = 128;
  3016. left &= ~0x3fULL;
  3017. }
  3018. return left;
  3019. }
  3020. PMU_FORMAT_ATTR(event, "config:0-7" );
  3021. PMU_FORMAT_ATTR(umask, "config:8-15" );
  3022. PMU_FORMAT_ATTR(edge, "config:18" );
  3023. PMU_FORMAT_ATTR(pc, "config:19" );
  3024. PMU_FORMAT_ATTR(any, "config:21" ); /* v3 + */
  3025. PMU_FORMAT_ATTR(inv, "config:23" );
  3026. PMU_FORMAT_ATTR(cmask, "config:24-31" );
  3027. PMU_FORMAT_ATTR(in_tx, "config:32");
  3028. PMU_FORMAT_ATTR(in_tx_cp, "config:33");
  3029. static struct attribute *intel_arch_formats_attr[] = {
  3030. &format_attr_event.attr,
  3031. &format_attr_umask.attr,
  3032. &format_attr_edge.attr,
  3033. &format_attr_pc.attr,
  3034. &format_attr_inv.attr,
  3035. &format_attr_cmask.attr,
  3036. NULL,
  3037. };
  3038. ssize_t intel_event_sysfs_show(char *page, u64 config)
  3039. {
  3040. u64 event = (config & ARCH_PERFMON_EVENTSEL_EVENT);
  3041. return x86_event_sysfs_show(page, config, event);
  3042. }
  3043. struct intel_shared_regs *allocate_shared_regs(int cpu)
  3044. {
  3045. struct intel_shared_regs *regs;
  3046. int i;
  3047. regs = kzalloc_node(sizeof(struct intel_shared_regs),
  3048. GFP_KERNEL, cpu_to_node(cpu));
  3049. if (regs) {
  3050. /*
  3051. * initialize the locks to keep lockdep happy
  3052. */
  3053. for (i = 0; i < EXTRA_REG_MAX; i++)
  3054. raw_spin_lock_init(&regs->regs[i].lock);
  3055. regs->core_id = -1;
  3056. }
  3057. return regs;
  3058. }
  3059. static struct intel_excl_cntrs *allocate_excl_cntrs(int cpu)
  3060. {
  3061. struct intel_excl_cntrs *c;
  3062. c = kzalloc_node(sizeof(struct intel_excl_cntrs),
  3063. GFP_KERNEL, cpu_to_node(cpu));
  3064. if (c) {
  3065. raw_spin_lock_init(&c->lock);
  3066. c->core_id = -1;
  3067. }
  3068. return c;
  3069. }
  3070. static int intel_pmu_cpu_prepare(int cpu)
  3071. {
  3072. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  3073. if (x86_pmu.extra_regs || x86_pmu.lbr_sel_map) {
  3074. cpuc->shared_regs = allocate_shared_regs(cpu);
  3075. if (!cpuc->shared_regs)
  3076. goto err;
  3077. }
  3078. if (x86_pmu.flags & PMU_FL_EXCL_CNTRS) {
  3079. size_t sz = X86_PMC_IDX_MAX * sizeof(struct event_constraint);
  3080. cpuc->constraint_list = kzalloc(sz, GFP_KERNEL);
  3081. if (!cpuc->constraint_list)
  3082. goto err_shared_regs;
  3083. cpuc->excl_cntrs = allocate_excl_cntrs(cpu);
  3084. if (!cpuc->excl_cntrs)
  3085. goto err_constraint_list;
  3086. cpuc->excl_thread_id = 0;
  3087. }
  3088. return 0;
  3089. err_constraint_list:
  3090. kfree(cpuc->constraint_list);
  3091. cpuc->constraint_list = NULL;
  3092. err_shared_regs:
  3093. kfree(cpuc->shared_regs);
  3094. cpuc->shared_regs = NULL;
  3095. err:
  3096. return -ENOMEM;
  3097. }
  3098. static void flip_smm_bit(void *data)
  3099. {
  3100. unsigned long set = *(unsigned long *)data;
  3101. if (set > 0) {
  3102. msr_set_bit(MSR_IA32_DEBUGCTLMSR,
  3103. DEBUGCTLMSR_FREEZE_IN_SMM_BIT);
  3104. } else {
  3105. msr_clear_bit(MSR_IA32_DEBUGCTLMSR,
  3106. DEBUGCTLMSR_FREEZE_IN_SMM_BIT);
  3107. }
  3108. }
  3109. static void intel_pmu_cpu_starting(int cpu)
  3110. {
  3111. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  3112. int core_id = topology_core_id(cpu);
  3113. int i;
  3114. init_debug_store_on_cpu(cpu);
  3115. /*
  3116. * Deal with CPUs that don't clear their LBRs on power-up.
  3117. */
  3118. intel_pmu_lbr_reset();
  3119. cpuc->lbr_sel = NULL;
  3120. if (x86_pmu.version > 1)
  3121. flip_smm_bit(&x86_pmu.attr_freeze_on_smi);
  3122. if (x86_pmu.counter_freezing)
  3123. enable_counter_freeze();
  3124. if (!cpuc->shared_regs)
  3125. return;
  3126. if (!(x86_pmu.flags & PMU_FL_NO_HT_SHARING)) {
  3127. for_each_cpu(i, topology_sibling_cpumask(cpu)) {
  3128. struct intel_shared_regs *pc;
  3129. pc = per_cpu(cpu_hw_events, i).shared_regs;
  3130. if (pc && pc->core_id == core_id) {
  3131. cpuc->kfree_on_online[0] = cpuc->shared_regs;
  3132. cpuc->shared_regs = pc;
  3133. break;
  3134. }
  3135. }
  3136. cpuc->shared_regs->core_id = core_id;
  3137. cpuc->shared_regs->refcnt++;
  3138. }
  3139. if (x86_pmu.lbr_sel_map)
  3140. cpuc->lbr_sel = &cpuc->shared_regs->regs[EXTRA_REG_LBR];
  3141. if (x86_pmu.flags & PMU_FL_EXCL_CNTRS) {
  3142. for_each_cpu(i, topology_sibling_cpumask(cpu)) {
  3143. struct cpu_hw_events *sibling;
  3144. struct intel_excl_cntrs *c;
  3145. sibling = &per_cpu(cpu_hw_events, i);
  3146. c = sibling->excl_cntrs;
  3147. if (c && c->core_id == core_id) {
  3148. cpuc->kfree_on_online[1] = cpuc->excl_cntrs;
  3149. cpuc->excl_cntrs = c;
  3150. if (!sibling->excl_thread_id)
  3151. cpuc->excl_thread_id = 1;
  3152. break;
  3153. }
  3154. }
  3155. cpuc->excl_cntrs->core_id = core_id;
  3156. cpuc->excl_cntrs->refcnt++;
  3157. }
  3158. }
  3159. static void free_excl_cntrs(int cpu)
  3160. {
  3161. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  3162. struct intel_excl_cntrs *c;
  3163. c = cpuc->excl_cntrs;
  3164. if (c) {
  3165. if (c->core_id == -1 || --c->refcnt == 0)
  3166. kfree(c);
  3167. cpuc->excl_cntrs = NULL;
  3168. kfree(cpuc->constraint_list);
  3169. cpuc->constraint_list = NULL;
  3170. }
  3171. }
  3172. static void intel_pmu_cpu_dying(int cpu)
  3173. {
  3174. struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
  3175. struct intel_shared_regs *pc;
  3176. pc = cpuc->shared_regs;
  3177. if (pc) {
  3178. if (pc->core_id == -1 || --pc->refcnt == 0)
  3179. kfree(pc);
  3180. cpuc->shared_regs = NULL;
  3181. }
  3182. free_excl_cntrs(cpu);
  3183. fini_debug_store_on_cpu(cpu);
  3184. if (x86_pmu.counter_freezing)
  3185. disable_counter_freeze();
  3186. }
  3187. static void intel_pmu_sched_task(struct perf_event_context *ctx,
  3188. bool sched_in)
  3189. {
  3190. intel_pmu_pebs_sched_task(ctx, sched_in);
  3191. intel_pmu_lbr_sched_task(ctx, sched_in);
  3192. }
  3193. PMU_FORMAT_ATTR(offcore_rsp, "config1:0-63");
  3194. PMU_FORMAT_ATTR(ldlat, "config1:0-15");
  3195. PMU_FORMAT_ATTR(frontend, "config1:0-23");
  3196. static struct attribute *intel_arch3_formats_attr[] = {
  3197. &format_attr_event.attr,
  3198. &format_attr_umask.attr,
  3199. &format_attr_edge.attr,
  3200. &format_attr_pc.attr,
  3201. &format_attr_any.attr,
  3202. &format_attr_inv.attr,
  3203. &format_attr_cmask.attr,
  3204. NULL,
  3205. };
  3206. static struct attribute *hsw_format_attr[] = {
  3207. &format_attr_in_tx.attr,
  3208. &format_attr_in_tx_cp.attr,
  3209. &format_attr_offcore_rsp.attr,
  3210. &format_attr_ldlat.attr,
  3211. NULL
  3212. };
  3213. static struct attribute *nhm_format_attr[] = {
  3214. &format_attr_offcore_rsp.attr,
  3215. &format_attr_ldlat.attr,
  3216. NULL
  3217. };
  3218. static struct attribute *slm_format_attr[] = {
  3219. &format_attr_offcore_rsp.attr,
  3220. NULL
  3221. };
  3222. static struct attribute *skl_format_attr[] = {
  3223. &format_attr_frontend.attr,
  3224. NULL,
  3225. };
  3226. static __initconst const struct x86_pmu core_pmu = {
  3227. .name = "core",
  3228. .handle_irq = x86_pmu_handle_irq,
  3229. .disable_all = x86_pmu_disable_all,
  3230. .enable_all = core_pmu_enable_all,
  3231. .enable = core_pmu_enable_event,
  3232. .disable = x86_pmu_disable_event,
  3233. .hw_config = x86_pmu_hw_config,
  3234. .schedule_events = x86_schedule_events,
  3235. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  3236. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  3237. .event_map = intel_pmu_event_map,
  3238. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  3239. .apic = 1,
  3240. .large_pebs_flags = LARGE_PEBS_FLAGS,
  3241. /*
  3242. * Intel PMCs cannot be accessed sanely above 32-bit width,
  3243. * so we install an artificial 1<<31 period regardless of
  3244. * the generic event period:
  3245. */
  3246. .max_period = (1ULL<<31) - 1,
  3247. .get_event_constraints = intel_get_event_constraints,
  3248. .put_event_constraints = intel_put_event_constraints,
  3249. .event_constraints = intel_core_event_constraints,
  3250. .guest_get_msrs = core_guest_get_msrs,
  3251. .format_attrs = intel_arch_formats_attr,
  3252. .events_sysfs_show = intel_event_sysfs_show,
  3253. /*
  3254. * Virtual (or funny metal) CPU can define x86_pmu.extra_regs
  3255. * together with PMU version 1 and thus be using core_pmu with
  3256. * shared_regs. We need following callbacks here to allocate
  3257. * it properly.
  3258. */
  3259. .cpu_prepare = intel_pmu_cpu_prepare,
  3260. .cpu_starting = intel_pmu_cpu_starting,
  3261. .cpu_dying = intel_pmu_cpu_dying,
  3262. };
  3263. static struct attribute *intel_pmu_attrs[];
  3264. static __initconst const struct x86_pmu intel_pmu = {
  3265. .name = "Intel",
  3266. .handle_irq = intel_pmu_handle_irq,
  3267. .disable_all = intel_pmu_disable_all,
  3268. .enable_all = intel_pmu_enable_all,
  3269. .enable = intel_pmu_enable_event,
  3270. .disable = intel_pmu_disable_event,
  3271. .add = intel_pmu_add_event,
  3272. .del = intel_pmu_del_event,
  3273. .read = intel_pmu_read_event,
  3274. .hw_config = intel_pmu_hw_config,
  3275. .schedule_events = x86_schedule_events,
  3276. .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
  3277. .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
  3278. .event_map = intel_pmu_event_map,
  3279. .max_events = ARRAY_SIZE(intel_perfmon_event_map),
  3280. .apic = 1,
  3281. .large_pebs_flags = LARGE_PEBS_FLAGS,
  3282. /*
  3283. * Intel PMCs cannot be accessed sanely above 32 bit width,
  3284. * so we install an artificial 1<<31 period regardless of
  3285. * the generic event period:
  3286. */
  3287. .max_period = (1ULL << 31) - 1,
  3288. .get_event_constraints = intel_get_event_constraints,
  3289. .put_event_constraints = intel_put_event_constraints,
  3290. .pebs_aliases = intel_pebs_aliases_core2,
  3291. .format_attrs = intel_arch3_formats_attr,
  3292. .events_sysfs_show = intel_event_sysfs_show,
  3293. .attrs = intel_pmu_attrs,
  3294. .cpu_prepare = intel_pmu_cpu_prepare,
  3295. .cpu_starting = intel_pmu_cpu_starting,
  3296. .cpu_dying = intel_pmu_cpu_dying,
  3297. .guest_get_msrs = intel_guest_get_msrs,
  3298. .sched_task = intel_pmu_sched_task,
  3299. };
  3300. static __init void intel_clovertown_quirk(void)
  3301. {
  3302. /*
  3303. * PEBS is unreliable due to:
  3304. *
  3305. * AJ67 - PEBS may experience CPL leaks
  3306. * AJ68 - PEBS PMI may be delayed by one event
  3307. * AJ69 - GLOBAL_STATUS[62] will only be set when DEBUGCTL[12]
  3308. * AJ106 - FREEZE_LBRS_ON_PMI doesn't work in combination with PEBS
  3309. *
  3310. * AJ67 could be worked around by restricting the OS/USR flags.
  3311. * AJ69 could be worked around by setting PMU_FREEZE_ON_PMI.
  3312. *
  3313. * AJ106 could possibly be worked around by not allowing LBR
  3314. * usage from PEBS, including the fixup.
  3315. * AJ68 could possibly be worked around by always programming
  3316. * a pebs_event_reset[0] value and coping with the lost events.
  3317. *
  3318. * But taken together it might just make sense to not enable PEBS on
  3319. * these chips.
  3320. */
  3321. pr_warn("PEBS disabled due to CPU errata\n");
  3322. x86_pmu.pebs = 0;
  3323. x86_pmu.pebs_constraints = NULL;
  3324. }
  3325. static int intel_snb_pebs_broken(int cpu)
  3326. {
  3327. u32 rev = UINT_MAX; /* default to broken for unknown models */
  3328. switch (cpu_data(cpu).x86_model) {
  3329. case INTEL_FAM6_SANDYBRIDGE:
  3330. rev = 0x28;
  3331. break;
  3332. case INTEL_FAM6_SANDYBRIDGE_X:
  3333. switch (cpu_data(cpu).x86_stepping) {
  3334. case 6: rev = 0x618; break;
  3335. case 7: rev = 0x70c; break;
  3336. }
  3337. }
  3338. return (cpu_data(cpu).microcode < rev);
  3339. }
  3340. static void intel_snb_check_microcode(void)
  3341. {
  3342. int pebs_broken = 0;
  3343. int cpu;
  3344. for_each_online_cpu(cpu) {
  3345. if ((pebs_broken = intel_snb_pebs_broken(cpu)))
  3346. break;
  3347. }
  3348. if (pebs_broken == x86_pmu.pebs_broken)
  3349. return;
  3350. /*
  3351. * Serialized by the microcode lock..
  3352. */
  3353. if (x86_pmu.pebs_broken) {
  3354. pr_info("PEBS enabled due to microcode update\n");
  3355. x86_pmu.pebs_broken = 0;
  3356. } else {
  3357. pr_info("PEBS disabled due to CPU errata, please upgrade microcode\n");
  3358. x86_pmu.pebs_broken = 1;
  3359. }
  3360. }
  3361. static bool is_lbr_from(unsigned long msr)
  3362. {
  3363. unsigned long lbr_from_nr = x86_pmu.lbr_from + x86_pmu.lbr_nr;
  3364. return x86_pmu.lbr_from <= msr && msr < lbr_from_nr;
  3365. }
  3366. /*
  3367. * Under certain circumstances, access certain MSR may cause #GP.
  3368. * The function tests if the input MSR can be safely accessed.
  3369. */
  3370. static bool check_msr(unsigned long msr, u64 mask)
  3371. {
  3372. u64 val_old, val_new, val_tmp;
  3373. /*
  3374. * Read the current value, change it and read it back to see if it
  3375. * matches, this is needed to detect certain hardware emulators
  3376. * (qemu/kvm) that don't trap on the MSR access and always return 0s.
  3377. */
  3378. if (rdmsrl_safe(msr, &val_old))
  3379. return false;
  3380. /*
  3381. * Only change the bits which can be updated by wrmsrl.
  3382. */
  3383. val_tmp = val_old ^ mask;
  3384. if (is_lbr_from(msr))
  3385. val_tmp = lbr_from_signext_quirk_wr(val_tmp);
  3386. if (wrmsrl_safe(msr, val_tmp) ||
  3387. rdmsrl_safe(msr, &val_new))
  3388. return false;
  3389. /*
  3390. * Quirk only affects validation in wrmsr(), so wrmsrl()'s value
  3391. * should equal rdmsrl()'s even with the quirk.
  3392. */
  3393. if (val_new != val_tmp)
  3394. return false;
  3395. if (is_lbr_from(msr))
  3396. val_old = lbr_from_signext_quirk_wr(val_old);
  3397. /* Here it's sure that the MSR can be safely accessed.
  3398. * Restore the old value and return.
  3399. */
  3400. wrmsrl(msr, val_old);
  3401. return true;
  3402. }
  3403. static __init void intel_sandybridge_quirk(void)
  3404. {
  3405. x86_pmu.check_microcode = intel_snb_check_microcode;
  3406. cpus_read_lock();
  3407. intel_snb_check_microcode();
  3408. cpus_read_unlock();
  3409. }
  3410. static const struct { int id; char *name; } intel_arch_events_map[] __initconst = {
  3411. { PERF_COUNT_HW_CPU_CYCLES, "cpu cycles" },
  3412. { PERF_COUNT_HW_INSTRUCTIONS, "instructions" },
  3413. { PERF_COUNT_HW_BUS_CYCLES, "bus cycles" },
  3414. { PERF_COUNT_HW_CACHE_REFERENCES, "cache references" },
  3415. { PERF_COUNT_HW_CACHE_MISSES, "cache misses" },
  3416. { PERF_COUNT_HW_BRANCH_INSTRUCTIONS, "branch instructions" },
  3417. { PERF_COUNT_HW_BRANCH_MISSES, "branch misses" },
  3418. };
  3419. static __init void intel_arch_events_quirk(void)
  3420. {
  3421. int bit;
  3422. /* disable event that reported as not presend by cpuid */
  3423. for_each_set_bit(bit, x86_pmu.events_mask, ARRAY_SIZE(intel_arch_events_map)) {
  3424. intel_perfmon_event_map[intel_arch_events_map[bit].id] = 0;
  3425. pr_warn("CPUID marked event: \'%s\' unavailable\n",
  3426. intel_arch_events_map[bit].name);
  3427. }
  3428. }
  3429. static __init void intel_nehalem_quirk(void)
  3430. {
  3431. union cpuid10_ebx ebx;
  3432. ebx.full = x86_pmu.events_maskl;
  3433. if (ebx.split.no_branch_misses_retired) {
  3434. /*
  3435. * Erratum AAJ80 detected, we work it around by using
  3436. * the BR_MISP_EXEC.ANY event. This will over-count
  3437. * branch-misses, but it's still much better than the
  3438. * architectural event which is often completely bogus:
  3439. */
  3440. intel_perfmon_event_map[PERF_COUNT_HW_BRANCH_MISSES] = 0x7f89;
  3441. ebx.split.no_branch_misses_retired = 0;
  3442. x86_pmu.events_maskl = ebx.full;
  3443. pr_info("CPU erratum AAJ80 worked around\n");
  3444. }
  3445. }
  3446. static bool intel_glp_counter_freezing_broken(int cpu)
  3447. {
  3448. u32 rev = UINT_MAX; /* default to broken for unknown stepping */
  3449. switch (cpu_data(cpu).x86_stepping) {
  3450. case 1:
  3451. rev = 0x28;
  3452. break;
  3453. case 8:
  3454. rev = 0x6;
  3455. break;
  3456. }
  3457. return (cpu_data(cpu).microcode < rev);
  3458. }
  3459. static __init void intel_glp_counter_freezing_quirk(void)
  3460. {
  3461. /* Check if it's already disabled */
  3462. if (disable_counter_freezing)
  3463. return;
  3464. /*
  3465. * If the system starts with the wrong ucode, leave the
  3466. * counter-freezing feature permanently disabled.
  3467. */
  3468. if (intel_glp_counter_freezing_broken(raw_smp_processor_id())) {
  3469. pr_info("PMU counter freezing disabled due to CPU errata,"
  3470. "please upgrade microcode\n");
  3471. x86_pmu.counter_freezing = false;
  3472. x86_pmu.handle_irq = intel_pmu_handle_irq;
  3473. }
  3474. }
  3475. /*
  3476. * enable software workaround for errata:
  3477. * SNB: BJ122
  3478. * IVB: BV98
  3479. * HSW: HSD29
  3480. *
  3481. * Only needed when HT is enabled. However detecting
  3482. * if HT is enabled is difficult (model specific). So instead,
  3483. * we enable the workaround in the early boot, and verify if
  3484. * it is needed in a later initcall phase once we have valid
  3485. * topology information to check if HT is actually enabled
  3486. */
  3487. static __init void intel_ht_bug(void)
  3488. {
  3489. x86_pmu.flags |= PMU_FL_EXCL_CNTRS | PMU_FL_EXCL_ENABLED;
  3490. x86_pmu.start_scheduling = intel_start_scheduling;
  3491. x86_pmu.commit_scheduling = intel_commit_scheduling;
  3492. x86_pmu.stop_scheduling = intel_stop_scheduling;
  3493. }
  3494. EVENT_ATTR_STR(mem-loads, mem_ld_hsw, "event=0xcd,umask=0x1,ldlat=3");
  3495. EVENT_ATTR_STR(mem-stores, mem_st_hsw, "event=0xd0,umask=0x82")
  3496. /* Haswell special events */
  3497. EVENT_ATTR_STR(tx-start, tx_start, "event=0xc9,umask=0x1");
  3498. EVENT_ATTR_STR(tx-commit, tx_commit, "event=0xc9,umask=0x2");
  3499. EVENT_ATTR_STR(tx-abort, tx_abort, "event=0xc9,umask=0x4");
  3500. EVENT_ATTR_STR(tx-capacity, tx_capacity, "event=0x54,umask=0x2");
  3501. EVENT_ATTR_STR(tx-conflict, tx_conflict, "event=0x54,umask=0x1");
  3502. EVENT_ATTR_STR(el-start, el_start, "event=0xc8,umask=0x1");
  3503. EVENT_ATTR_STR(el-commit, el_commit, "event=0xc8,umask=0x2");
  3504. EVENT_ATTR_STR(el-abort, el_abort, "event=0xc8,umask=0x4");
  3505. EVENT_ATTR_STR(el-capacity, el_capacity, "event=0x54,umask=0x2");
  3506. EVENT_ATTR_STR(el-conflict, el_conflict, "event=0x54,umask=0x1");
  3507. EVENT_ATTR_STR(cycles-t, cycles_t, "event=0x3c,in_tx=1");
  3508. EVENT_ATTR_STR(cycles-ct, cycles_ct, "event=0x3c,in_tx=1,in_tx_cp=1");
  3509. static struct attribute *hsw_events_attrs[] = {
  3510. EVENT_PTR(td_slots_issued),
  3511. EVENT_PTR(td_slots_retired),
  3512. EVENT_PTR(td_fetch_bubbles),
  3513. EVENT_PTR(td_total_slots),
  3514. EVENT_PTR(td_total_slots_scale),
  3515. EVENT_PTR(td_recovery_bubbles),
  3516. EVENT_PTR(td_recovery_bubbles_scale),
  3517. NULL
  3518. };
  3519. static struct attribute *hsw_mem_events_attrs[] = {
  3520. EVENT_PTR(mem_ld_hsw),
  3521. EVENT_PTR(mem_st_hsw),
  3522. NULL,
  3523. };
  3524. static struct attribute *hsw_tsx_events_attrs[] = {
  3525. EVENT_PTR(tx_start),
  3526. EVENT_PTR(tx_commit),
  3527. EVENT_PTR(tx_abort),
  3528. EVENT_PTR(tx_capacity),
  3529. EVENT_PTR(tx_conflict),
  3530. EVENT_PTR(el_start),
  3531. EVENT_PTR(el_commit),
  3532. EVENT_PTR(el_abort),
  3533. EVENT_PTR(el_capacity),
  3534. EVENT_PTR(el_conflict),
  3535. EVENT_PTR(cycles_t),
  3536. EVENT_PTR(cycles_ct),
  3537. NULL
  3538. };
  3539. static ssize_t freeze_on_smi_show(struct device *cdev,
  3540. struct device_attribute *attr,
  3541. char *buf)
  3542. {
  3543. return sprintf(buf, "%lu\n", x86_pmu.attr_freeze_on_smi);
  3544. }
  3545. static DEFINE_MUTEX(freeze_on_smi_mutex);
  3546. static ssize_t freeze_on_smi_store(struct device *cdev,
  3547. struct device_attribute *attr,
  3548. const char *buf, size_t count)
  3549. {
  3550. unsigned long val;
  3551. ssize_t ret;
  3552. ret = kstrtoul(buf, 0, &val);
  3553. if (ret)
  3554. return ret;
  3555. if (val > 1)
  3556. return -EINVAL;
  3557. mutex_lock(&freeze_on_smi_mutex);
  3558. if (x86_pmu.attr_freeze_on_smi == val)
  3559. goto done;
  3560. x86_pmu.attr_freeze_on_smi = val;
  3561. get_online_cpus();
  3562. on_each_cpu(flip_smm_bit, &val, 1);
  3563. put_online_cpus();
  3564. done:
  3565. mutex_unlock(&freeze_on_smi_mutex);
  3566. return count;
  3567. }
  3568. static DEVICE_ATTR_RW(freeze_on_smi);
  3569. static ssize_t branches_show(struct device *cdev,
  3570. struct device_attribute *attr,
  3571. char *buf)
  3572. {
  3573. return snprintf(buf, PAGE_SIZE, "%d\n", x86_pmu.lbr_nr);
  3574. }
  3575. static DEVICE_ATTR_RO(branches);
  3576. static struct attribute *lbr_attrs[] = {
  3577. &dev_attr_branches.attr,
  3578. NULL
  3579. };
  3580. static char pmu_name_str[30];
  3581. static ssize_t pmu_name_show(struct device *cdev,
  3582. struct device_attribute *attr,
  3583. char *buf)
  3584. {
  3585. return snprintf(buf, PAGE_SIZE, "%s\n", pmu_name_str);
  3586. }
  3587. static DEVICE_ATTR_RO(pmu_name);
  3588. static struct attribute *intel_pmu_caps_attrs[] = {
  3589. &dev_attr_pmu_name.attr,
  3590. NULL
  3591. };
  3592. static struct attribute *intel_pmu_attrs[] = {
  3593. &dev_attr_freeze_on_smi.attr,
  3594. NULL,
  3595. };
  3596. static __init struct attribute **
  3597. get_events_attrs(struct attribute **base,
  3598. struct attribute **mem,
  3599. struct attribute **tsx)
  3600. {
  3601. struct attribute **attrs = base;
  3602. struct attribute **old;
  3603. if (mem && x86_pmu.pebs)
  3604. attrs = merge_attr(attrs, mem);
  3605. if (tsx && boot_cpu_has(X86_FEATURE_RTM)) {
  3606. old = attrs;
  3607. attrs = merge_attr(attrs, tsx);
  3608. if (old != base)
  3609. kfree(old);
  3610. }
  3611. return attrs;
  3612. }
  3613. __init int intel_pmu_init(void)
  3614. {
  3615. struct attribute **extra_attr = NULL;
  3616. struct attribute **mem_attr = NULL;
  3617. struct attribute **tsx_attr = NULL;
  3618. struct attribute **to_free = NULL;
  3619. union cpuid10_edx edx;
  3620. union cpuid10_eax eax;
  3621. union cpuid10_ebx ebx;
  3622. struct event_constraint *c;
  3623. unsigned int unused;
  3624. struct extra_reg *er;
  3625. int version, i;
  3626. char *name;
  3627. if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
  3628. switch (boot_cpu_data.x86) {
  3629. case 0x6:
  3630. return p6_pmu_init();
  3631. case 0xb:
  3632. return knc_pmu_init();
  3633. case 0xf:
  3634. return p4_pmu_init();
  3635. }
  3636. return -ENODEV;
  3637. }
  3638. /*
  3639. * Check whether the Architectural PerfMon supports
  3640. * Branch Misses Retired hw_event or not.
  3641. */
  3642. cpuid(10, &eax.full, &ebx.full, &unused, &edx.full);
  3643. if (eax.split.mask_length < ARCH_PERFMON_EVENTS_COUNT)
  3644. return -ENODEV;
  3645. version = eax.split.version_id;
  3646. if (version < 2)
  3647. x86_pmu = core_pmu;
  3648. else
  3649. x86_pmu = intel_pmu;
  3650. x86_pmu.version = version;
  3651. x86_pmu.num_counters = eax.split.num_counters;
  3652. x86_pmu.cntval_bits = eax.split.bit_width;
  3653. x86_pmu.cntval_mask = (1ULL << eax.split.bit_width) - 1;
  3654. x86_pmu.events_maskl = ebx.full;
  3655. x86_pmu.events_mask_len = eax.split.mask_length;
  3656. x86_pmu.max_pebs_events = min_t(unsigned, MAX_PEBS_EVENTS, x86_pmu.num_counters);
  3657. /*
  3658. * Quirk: v2 perfmon does not report fixed-purpose events, so
  3659. * assume at least 3 events, when not running in a hypervisor:
  3660. */
  3661. if (version > 1) {
  3662. int assume = 3 * !boot_cpu_has(X86_FEATURE_HYPERVISOR);
  3663. x86_pmu.num_counters_fixed =
  3664. max((int)edx.split.num_counters_fixed, assume);
  3665. }
  3666. if (version >= 4)
  3667. x86_pmu.counter_freezing = !disable_counter_freezing;
  3668. if (boot_cpu_has(X86_FEATURE_PDCM)) {
  3669. u64 capabilities;
  3670. rdmsrl(MSR_IA32_PERF_CAPABILITIES, capabilities);
  3671. x86_pmu.intel_cap.capabilities = capabilities;
  3672. }
  3673. intel_ds_init();
  3674. x86_add_quirk(intel_arch_events_quirk); /* Install first, so it runs last */
  3675. /*
  3676. * Install the hw-cache-events table:
  3677. */
  3678. switch (boot_cpu_data.x86_model) {
  3679. case INTEL_FAM6_CORE_YONAH:
  3680. pr_cont("Core events, ");
  3681. name = "core";
  3682. break;
  3683. case INTEL_FAM6_CORE2_MEROM:
  3684. x86_add_quirk(intel_clovertown_quirk);
  3685. case INTEL_FAM6_CORE2_MEROM_L:
  3686. case INTEL_FAM6_CORE2_PENRYN:
  3687. case INTEL_FAM6_CORE2_DUNNINGTON:
  3688. memcpy(hw_cache_event_ids, core2_hw_cache_event_ids,
  3689. sizeof(hw_cache_event_ids));
  3690. intel_pmu_lbr_init_core();
  3691. x86_pmu.event_constraints = intel_core2_event_constraints;
  3692. x86_pmu.pebs_constraints = intel_core2_pebs_event_constraints;
  3693. pr_cont("Core2 events, ");
  3694. name = "core2";
  3695. break;
  3696. case INTEL_FAM6_NEHALEM:
  3697. case INTEL_FAM6_NEHALEM_EP:
  3698. case INTEL_FAM6_NEHALEM_EX:
  3699. memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids,
  3700. sizeof(hw_cache_event_ids));
  3701. memcpy(hw_cache_extra_regs, nehalem_hw_cache_extra_regs,
  3702. sizeof(hw_cache_extra_regs));
  3703. intel_pmu_lbr_init_nhm();
  3704. x86_pmu.event_constraints = intel_nehalem_event_constraints;
  3705. x86_pmu.pebs_constraints = intel_nehalem_pebs_event_constraints;
  3706. x86_pmu.enable_all = intel_pmu_nhm_enable_all;
  3707. x86_pmu.extra_regs = intel_nehalem_extra_regs;
  3708. mem_attr = nhm_mem_events_attrs;
  3709. /* UOPS_ISSUED.STALLED_CYCLES */
  3710. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
  3711. X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
  3712. /* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */
  3713. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
  3714. X86_CONFIG(.event=0xb1, .umask=0x3f, .inv=1, .cmask=1);
  3715. intel_pmu_pebs_data_source_nhm();
  3716. x86_add_quirk(intel_nehalem_quirk);
  3717. x86_pmu.pebs_no_tlb = 1;
  3718. extra_attr = nhm_format_attr;
  3719. pr_cont("Nehalem events, ");
  3720. name = "nehalem";
  3721. break;
  3722. case INTEL_FAM6_ATOM_BONNELL:
  3723. case INTEL_FAM6_ATOM_BONNELL_MID:
  3724. case INTEL_FAM6_ATOM_SALTWELL:
  3725. case INTEL_FAM6_ATOM_SALTWELL_MID:
  3726. case INTEL_FAM6_ATOM_SALTWELL_TABLET:
  3727. memcpy(hw_cache_event_ids, atom_hw_cache_event_ids,
  3728. sizeof(hw_cache_event_ids));
  3729. intel_pmu_lbr_init_atom();
  3730. x86_pmu.event_constraints = intel_gen_event_constraints;
  3731. x86_pmu.pebs_constraints = intel_atom_pebs_event_constraints;
  3732. x86_pmu.pebs_aliases = intel_pebs_aliases_core2;
  3733. pr_cont("Atom events, ");
  3734. name = "bonnell";
  3735. break;
  3736. case INTEL_FAM6_ATOM_SILVERMONT:
  3737. case INTEL_FAM6_ATOM_SILVERMONT_X:
  3738. case INTEL_FAM6_ATOM_SILVERMONT_MID:
  3739. case INTEL_FAM6_ATOM_AIRMONT:
  3740. case INTEL_FAM6_ATOM_AIRMONT_MID:
  3741. memcpy(hw_cache_event_ids, slm_hw_cache_event_ids,
  3742. sizeof(hw_cache_event_ids));
  3743. memcpy(hw_cache_extra_regs, slm_hw_cache_extra_regs,
  3744. sizeof(hw_cache_extra_regs));
  3745. intel_pmu_lbr_init_slm();
  3746. x86_pmu.event_constraints = intel_slm_event_constraints;
  3747. x86_pmu.pebs_constraints = intel_slm_pebs_event_constraints;
  3748. x86_pmu.extra_regs = intel_slm_extra_regs;
  3749. x86_pmu.flags |= PMU_FL_HAS_RSP_1;
  3750. x86_pmu.cpu_events = slm_events_attrs;
  3751. extra_attr = slm_format_attr;
  3752. pr_cont("Silvermont events, ");
  3753. name = "silvermont";
  3754. break;
  3755. case INTEL_FAM6_ATOM_GOLDMONT:
  3756. case INTEL_FAM6_ATOM_GOLDMONT_X:
  3757. memcpy(hw_cache_event_ids, glm_hw_cache_event_ids,
  3758. sizeof(hw_cache_event_ids));
  3759. memcpy(hw_cache_extra_regs, glm_hw_cache_extra_regs,
  3760. sizeof(hw_cache_extra_regs));
  3761. intel_pmu_lbr_init_skl();
  3762. x86_pmu.event_constraints = intel_slm_event_constraints;
  3763. x86_pmu.pebs_constraints = intel_glm_pebs_event_constraints;
  3764. x86_pmu.extra_regs = intel_glm_extra_regs;
  3765. /*
  3766. * It's recommended to use CPU_CLK_UNHALTED.CORE_P + NPEBS
  3767. * for precise cycles.
  3768. * :pp is identical to :ppp
  3769. */
  3770. x86_pmu.pebs_aliases = NULL;
  3771. x86_pmu.pebs_prec_dist = true;
  3772. x86_pmu.lbr_pt_coexist = true;
  3773. x86_pmu.flags |= PMU_FL_HAS_RSP_1;
  3774. x86_pmu.cpu_events = glm_events_attrs;
  3775. extra_attr = slm_format_attr;
  3776. pr_cont("Goldmont events, ");
  3777. name = "goldmont";
  3778. break;
  3779. case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
  3780. x86_add_quirk(intel_glp_counter_freezing_quirk);
  3781. memcpy(hw_cache_event_ids, glp_hw_cache_event_ids,
  3782. sizeof(hw_cache_event_ids));
  3783. memcpy(hw_cache_extra_regs, glp_hw_cache_extra_regs,
  3784. sizeof(hw_cache_extra_regs));
  3785. intel_pmu_lbr_init_skl();
  3786. x86_pmu.event_constraints = intel_slm_event_constraints;
  3787. x86_pmu.extra_regs = intel_glm_extra_regs;
  3788. /*
  3789. * It's recommended to use CPU_CLK_UNHALTED.CORE_P + NPEBS
  3790. * for precise cycles.
  3791. */
  3792. x86_pmu.pebs_aliases = NULL;
  3793. x86_pmu.pebs_prec_dist = true;
  3794. x86_pmu.lbr_pt_coexist = true;
  3795. x86_pmu.flags |= PMU_FL_HAS_RSP_1;
  3796. x86_pmu.flags |= PMU_FL_PEBS_ALL;
  3797. x86_pmu.get_event_constraints = glp_get_event_constraints;
  3798. x86_pmu.cpu_events = glm_events_attrs;
  3799. /* Goldmont Plus has 4-wide pipeline */
  3800. event_attr_td_total_slots_scale_glm.event_str = "4";
  3801. extra_attr = slm_format_attr;
  3802. pr_cont("Goldmont plus events, ");
  3803. name = "goldmont_plus";
  3804. break;
  3805. case INTEL_FAM6_WESTMERE:
  3806. case INTEL_FAM6_WESTMERE_EP:
  3807. case INTEL_FAM6_WESTMERE_EX:
  3808. memcpy(hw_cache_event_ids, westmere_hw_cache_event_ids,
  3809. sizeof(hw_cache_event_ids));
  3810. memcpy(hw_cache_extra_regs, nehalem_hw_cache_extra_regs,
  3811. sizeof(hw_cache_extra_regs));
  3812. intel_pmu_lbr_init_nhm();
  3813. x86_pmu.event_constraints = intel_westmere_event_constraints;
  3814. x86_pmu.enable_all = intel_pmu_nhm_enable_all;
  3815. x86_pmu.pebs_constraints = intel_westmere_pebs_event_constraints;
  3816. x86_pmu.extra_regs = intel_westmere_extra_regs;
  3817. x86_pmu.flags |= PMU_FL_HAS_RSP_1;
  3818. mem_attr = nhm_mem_events_attrs;
  3819. /* UOPS_ISSUED.STALLED_CYCLES */
  3820. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
  3821. X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
  3822. /* UOPS_EXECUTED.CORE_ACTIVE_CYCLES,c=1,i=1 */
  3823. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
  3824. X86_CONFIG(.event=0xb1, .umask=0x3f, .inv=1, .cmask=1);
  3825. intel_pmu_pebs_data_source_nhm();
  3826. extra_attr = nhm_format_attr;
  3827. pr_cont("Westmere events, ");
  3828. name = "westmere";
  3829. break;
  3830. case INTEL_FAM6_SANDYBRIDGE:
  3831. case INTEL_FAM6_SANDYBRIDGE_X:
  3832. x86_add_quirk(intel_sandybridge_quirk);
  3833. x86_add_quirk(intel_ht_bug);
  3834. memcpy(hw_cache_event_ids, snb_hw_cache_event_ids,
  3835. sizeof(hw_cache_event_ids));
  3836. memcpy(hw_cache_extra_regs, snb_hw_cache_extra_regs,
  3837. sizeof(hw_cache_extra_regs));
  3838. intel_pmu_lbr_init_snb();
  3839. x86_pmu.event_constraints = intel_snb_event_constraints;
  3840. x86_pmu.pebs_constraints = intel_snb_pebs_event_constraints;
  3841. x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
  3842. if (boot_cpu_data.x86_model == INTEL_FAM6_SANDYBRIDGE_X)
  3843. x86_pmu.extra_regs = intel_snbep_extra_regs;
  3844. else
  3845. x86_pmu.extra_regs = intel_snb_extra_regs;
  3846. /* all extra regs are per-cpu when HT is on */
  3847. x86_pmu.flags |= PMU_FL_HAS_RSP_1;
  3848. x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
  3849. x86_pmu.cpu_events = snb_events_attrs;
  3850. mem_attr = snb_mem_events_attrs;
  3851. /* UOPS_ISSUED.ANY,c=1,i=1 to count stall cycles */
  3852. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
  3853. X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
  3854. /* UOPS_DISPATCHED.THREAD,c=1,i=1 to count stall cycles*/
  3855. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] =
  3856. X86_CONFIG(.event=0xb1, .umask=0x01, .inv=1, .cmask=1);
  3857. extra_attr = nhm_format_attr;
  3858. pr_cont("SandyBridge events, ");
  3859. name = "sandybridge";
  3860. break;
  3861. case INTEL_FAM6_IVYBRIDGE:
  3862. case INTEL_FAM6_IVYBRIDGE_X:
  3863. x86_add_quirk(intel_ht_bug);
  3864. memcpy(hw_cache_event_ids, snb_hw_cache_event_ids,
  3865. sizeof(hw_cache_event_ids));
  3866. /* dTLB-load-misses on IVB is different than SNB */
  3867. hw_cache_event_ids[C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = 0x8108; /* DTLB_LOAD_MISSES.DEMAND_LD_MISS_CAUSES_A_WALK */
  3868. memcpy(hw_cache_extra_regs, snb_hw_cache_extra_regs,
  3869. sizeof(hw_cache_extra_regs));
  3870. intel_pmu_lbr_init_snb();
  3871. x86_pmu.event_constraints = intel_ivb_event_constraints;
  3872. x86_pmu.pebs_constraints = intel_ivb_pebs_event_constraints;
  3873. x86_pmu.pebs_aliases = intel_pebs_aliases_ivb;
  3874. x86_pmu.pebs_prec_dist = true;
  3875. if (boot_cpu_data.x86_model == INTEL_FAM6_IVYBRIDGE_X)
  3876. x86_pmu.extra_regs = intel_snbep_extra_regs;
  3877. else
  3878. x86_pmu.extra_regs = intel_snb_extra_regs;
  3879. /* all extra regs are per-cpu when HT is on */
  3880. x86_pmu.flags |= PMU_FL_HAS_RSP_1;
  3881. x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
  3882. x86_pmu.cpu_events = snb_events_attrs;
  3883. mem_attr = snb_mem_events_attrs;
  3884. /* UOPS_ISSUED.ANY,c=1,i=1 to count stall cycles */
  3885. intel_perfmon_event_map[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] =
  3886. X86_CONFIG(.event=0x0e, .umask=0x01, .inv=1, .cmask=1);
  3887. extra_attr = nhm_format_attr;
  3888. pr_cont("IvyBridge events, ");
  3889. name = "ivybridge";
  3890. break;
  3891. case INTEL_FAM6_HASWELL_CORE:
  3892. case INTEL_FAM6_HASWELL_X:
  3893. case INTEL_FAM6_HASWELL_ULT:
  3894. case INTEL_FAM6_HASWELL_GT3E:
  3895. x86_add_quirk(intel_ht_bug);
  3896. x86_pmu.late_ack = true;
  3897. memcpy(hw_cache_event_ids, hsw_hw_cache_event_ids, sizeof(hw_cache_event_ids));
  3898. memcpy(hw_cache_extra_regs, hsw_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
  3899. intel_pmu_lbr_init_hsw();
  3900. x86_pmu.event_constraints = intel_hsw_event_constraints;
  3901. x86_pmu.pebs_constraints = intel_hsw_pebs_event_constraints;
  3902. x86_pmu.extra_regs = intel_snbep_extra_regs;
  3903. x86_pmu.pebs_aliases = intel_pebs_aliases_ivb;
  3904. x86_pmu.pebs_prec_dist = true;
  3905. /* all extra regs are per-cpu when HT is on */
  3906. x86_pmu.flags |= PMU_FL_HAS_RSP_1;
  3907. x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
  3908. x86_pmu.hw_config = hsw_hw_config;
  3909. x86_pmu.get_event_constraints = hsw_get_event_constraints;
  3910. x86_pmu.cpu_events = hsw_events_attrs;
  3911. x86_pmu.lbr_double_abort = true;
  3912. extra_attr = boot_cpu_has(X86_FEATURE_RTM) ?
  3913. hsw_format_attr : nhm_format_attr;
  3914. mem_attr = hsw_mem_events_attrs;
  3915. tsx_attr = hsw_tsx_events_attrs;
  3916. pr_cont("Haswell events, ");
  3917. name = "haswell";
  3918. break;
  3919. case INTEL_FAM6_BROADWELL_CORE:
  3920. case INTEL_FAM6_BROADWELL_XEON_D:
  3921. case INTEL_FAM6_BROADWELL_GT3E:
  3922. case INTEL_FAM6_BROADWELL_X:
  3923. x86_pmu.late_ack = true;
  3924. memcpy(hw_cache_event_ids, hsw_hw_cache_event_ids, sizeof(hw_cache_event_ids));
  3925. memcpy(hw_cache_extra_regs, hsw_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
  3926. /* L3_MISS_LOCAL_DRAM is BIT(26) in Broadwell */
  3927. hw_cache_extra_regs[C(LL)][C(OP_READ)][C(RESULT_MISS)] = HSW_DEMAND_READ |
  3928. BDW_L3_MISS|HSW_SNOOP_DRAM;
  3929. hw_cache_extra_regs[C(LL)][C(OP_WRITE)][C(RESULT_MISS)] = HSW_DEMAND_WRITE|BDW_L3_MISS|
  3930. HSW_SNOOP_DRAM;
  3931. hw_cache_extra_regs[C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = HSW_DEMAND_READ|
  3932. BDW_L3_MISS_LOCAL|HSW_SNOOP_DRAM;
  3933. hw_cache_extra_regs[C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = HSW_DEMAND_WRITE|
  3934. BDW_L3_MISS_LOCAL|HSW_SNOOP_DRAM;
  3935. intel_pmu_lbr_init_hsw();
  3936. x86_pmu.event_constraints = intel_bdw_event_constraints;
  3937. x86_pmu.pebs_constraints = intel_bdw_pebs_event_constraints;
  3938. x86_pmu.extra_regs = intel_snbep_extra_regs;
  3939. x86_pmu.pebs_aliases = intel_pebs_aliases_ivb;
  3940. x86_pmu.pebs_prec_dist = true;
  3941. /* all extra regs are per-cpu when HT is on */
  3942. x86_pmu.flags |= PMU_FL_HAS_RSP_1;
  3943. x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
  3944. x86_pmu.hw_config = hsw_hw_config;
  3945. x86_pmu.get_event_constraints = hsw_get_event_constraints;
  3946. x86_pmu.cpu_events = hsw_events_attrs;
  3947. x86_pmu.limit_period = bdw_limit_period;
  3948. extra_attr = boot_cpu_has(X86_FEATURE_RTM) ?
  3949. hsw_format_attr : nhm_format_attr;
  3950. mem_attr = hsw_mem_events_attrs;
  3951. tsx_attr = hsw_tsx_events_attrs;
  3952. pr_cont("Broadwell events, ");
  3953. name = "broadwell";
  3954. break;
  3955. case INTEL_FAM6_XEON_PHI_KNL:
  3956. case INTEL_FAM6_XEON_PHI_KNM:
  3957. memcpy(hw_cache_event_ids,
  3958. slm_hw_cache_event_ids, sizeof(hw_cache_event_ids));
  3959. memcpy(hw_cache_extra_regs,
  3960. knl_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
  3961. intel_pmu_lbr_init_knl();
  3962. x86_pmu.event_constraints = intel_slm_event_constraints;
  3963. x86_pmu.pebs_constraints = intel_slm_pebs_event_constraints;
  3964. x86_pmu.extra_regs = intel_knl_extra_regs;
  3965. /* all extra regs are per-cpu when HT is on */
  3966. x86_pmu.flags |= PMU_FL_HAS_RSP_1;
  3967. x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
  3968. extra_attr = slm_format_attr;
  3969. pr_cont("Knights Landing/Mill events, ");
  3970. name = "knights-landing";
  3971. break;
  3972. case INTEL_FAM6_SKYLAKE_MOBILE:
  3973. case INTEL_FAM6_SKYLAKE_DESKTOP:
  3974. case INTEL_FAM6_SKYLAKE_X:
  3975. case INTEL_FAM6_KABYLAKE_MOBILE:
  3976. case INTEL_FAM6_KABYLAKE_DESKTOP:
  3977. x86_pmu.late_ack = true;
  3978. memcpy(hw_cache_event_ids, skl_hw_cache_event_ids, sizeof(hw_cache_event_ids));
  3979. memcpy(hw_cache_extra_regs, skl_hw_cache_extra_regs, sizeof(hw_cache_extra_regs));
  3980. intel_pmu_lbr_init_skl();
  3981. /* INT_MISC.RECOVERY_CYCLES has umask 1 in Skylake */
  3982. event_attr_td_recovery_bubbles.event_str_noht =
  3983. "event=0xd,umask=0x1,cmask=1";
  3984. event_attr_td_recovery_bubbles.event_str_ht =
  3985. "event=0xd,umask=0x1,cmask=1,any=1";
  3986. x86_pmu.event_constraints = intel_skl_event_constraints;
  3987. x86_pmu.pebs_constraints = intel_skl_pebs_event_constraints;
  3988. x86_pmu.extra_regs = intel_skl_extra_regs;
  3989. x86_pmu.pebs_aliases = intel_pebs_aliases_skl;
  3990. x86_pmu.pebs_prec_dist = true;
  3991. /* all extra regs are per-cpu when HT is on */
  3992. x86_pmu.flags |= PMU_FL_HAS_RSP_1;
  3993. x86_pmu.flags |= PMU_FL_NO_HT_SHARING;
  3994. x86_pmu.hw_config = hsw_hw_config;
  3995. x86_pmu.get_event_constraints = hsw_get_event_constraints;
  3996. extra_attr = boot_cpu_has(X86_FEATURE_RTM) ?
  3997. hsw_format_attr : nhm_format_attr;
  3998. extra_attr = merge_attr(extra_attr, skl_format_attr);
  3999. to_free = extra_attr;
  4000. x86_pmu.cpu_events = hsw_events_attrs;
  4001. mem_attr = hsw_mem_events_attrs;
  4002. tsx_attr = hsw_tsx_events_attrs;
  4003. intel_pmu_pebs_data_source_skl(
  4004. boot_cpu_data.x86_model == INTEL_FAM6_SKYLAKE_X);
  4005. pr_cont("Skylake events, ");
  4006. name = "skylake";
  4007. break;
  4008. default:
  4009. switch (x86_pmu.version) {
  4010. case 1:
  4011. x86_pmu.event_constraints = intel_v1_event_constraints;
  4012. pr_cont("generic architected perfmon v1, ");
  4013. name = "generic_arch_v1";
  4014. break;
  4015. default:
  4016. /*
  4017. * default constraints for v2 and up
  4018. */
  4019. x86_pmu.event_constraints = intel_gen_event_constraints;
  4020. pr_cont("generic architected perfmon, ");
  4021. name = "generic_arch_v2+";
  4022. break;
  4023. }
  4024. }
  4025. snprintf(pmu_name_str, sizeof pmu_name_str, "%s", name);
  4026. if (version >= 2 && extra_attr) {
  4027. x86_pmu.format_attrs = merge_attr(intel_arch3_formats_attr,
  4028. extra_attr);
  4029. WARN_ON(!x86_pmu.format_attrs);
  4030. }
  4031. x86_pmu.cpu_events = get_events_attrs(x86_pmu.cpu_events,
  4032. mem_attr, tsx_attr);
  4033. if (x86_pmu.num_counters > INTEL_PMC_MAX_GENERIC) {
  4034. WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
  4035. x86_pmu.num_counters, INTEL_PMC_MAX_GENERIC);
  4036. x86_pmu.num_counters = INTEL_PMC_MAX_GENERIC;
  4037. }
  4038. x86_pmu.intel_ctrl = (1ULL << x86_pmu.num_counters) - 1;
  4039. if (x86_pmu.num_counters_fixed > INTEL_PMC_MAX_FIXED) {
  4040. WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
  4041. x86_pmu.num_counters_fixed, INTEL_PMC_MAX_FIXED);
  4042. x86_pmu.num_counters_fixed = INTEL_PMC_MAX_FIXED;
  4043. }
  4044. x86_pmu.intel_ctrl |=
  4045. ((1LL << x86_pmu.num_counters_fixed)-1) << INTEL_PMC_IDX_FIXED;
  4046. if (x86_pmu.event_constraints) {
  4047. /*
  4048. * event on fixed counter2 (REF_CYCLES) only works on this
  4049. * counter, so do not extend mask to generic counters
  4050. */
  4051. for_each_event_constraint(c, x86_pmu.event_constraints) {
  4052. if (c->cmask == FIXED_EVENT_FLAGS
  4053. && c->idxmsk64 != INTEL_PMC_MSK_FIXED_REF_CYCLES) {
  4054. c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
  4055. }
  4056. c->idxmsk64 &=
  4057. ~(~0ULL << (INTEL_PMC_IDX_FIXED + x86_pmu.num_counters_fixed));
  4058. c->weight = hweight64(c->idxmsk64);
  4059. }
  4060. }
  4061. /*
  4062. * Access LBR MSR may cause #GP under certain circumstances.
  4063. * E.g. KVM doesn't support LBR MSR
  4064. * Check all LBT MSR here.
  4065. * Disable LBR access if any LBR MSRs can not be accessed.
  4066. */
  4067. if (x86_pmu.lbr_nr && !check_msr(x86_pmu.lbr_tos, 0x3UL))
  4068. x86_pmu.lbr_nr = 0;
  4069. for (i = 0; i < x86_pmu.lbr_nr; i++) {
  4070. if (!(check_msr(x86_pmu.lbr_from + i, 0xffffUL) &&
  4071. check_msr(x86_pmu.lbr_to + i, 0xffffUL)))
  4072. x86_pmu.lbr_nr = 0;
  4073. }
  4074. x86_pmu.caps_attrs = intel_pmu_caps_attrs;
  4075. if (x86_pmu.lbr_nr) {
  4076. x86_pmu.caps_attrs = merge_attr(x86_pmu.caps_attrs, lbr_attrs);
  4077. pr_cont("%d-deep LBR, ", x86_pmu.lbr_nr);
  4078. }
  4079. /*
  4080. * Access extra MSR may cause #GP under certain circumstances.
  4081. * E.g. KVM doesn't support offcore event
  4082. * Check all extra_regs here.
  4083. */
  4084. if (x86_pmu.extra_regs) {
  4085. for (er = x86_pmu.extra_regs; er->msr; er++) {
  4086. er->extra_msr_access = check_msr(er->msr, 0x11UL);
  4087. /* Disable LBR select mapping */
  4088. if ((er->idx == EXTRA_REG_LBR) && !er->extra_msr_access)
  4089. x86_pmu.lbr_sel_map = NULL;
  4090. }
  4091. }
  4092. /* Support full width counters using alternative MSR range */
  4093. if (x86_pmu.intel_cap.full_width_write) {
  4094. x86_pmu.max_period = x86_pmu.cntval_mask >> 1;
  4095. x86_pmu.perfctr = MSR_IA32_PMC0;
  4096. pr_cont("full-width counters, ");
  4097. }
  4098. /*
  4099. * For arch perfmon 4 use counter freezing to avoid
  4100. * several MSR accesses in the PMI.
  4101. */
  4102. if (x86_pmu.counter_freezing)
  4103. x86_pmu.handle_irq = intel_pmu_handle_irq_v4;
  4104. kfree(to_free);
  4105. return 0;
  4106. }
  4107. /*
  4108. * HT bug: phase 2 init
  4109. * Called once we have valid topology information to check
  4110. * whether or not HT is enabled
  4111. * If HT is off, then we disable the workaround
  4112. */
  4113. static __init int fixup_ht_bug(void)
  4114. {
  4115. int c;
  4116. /*
  4117. * problem not present on this CPU model, nothing to do
  4118. */
  4119. if (!(x86_pmu.flags & PMU_FL_EXCL_ENABLED))
  4120. return 0;
  4121. if (topology_max_smt_threads() > 1) {
  4122. pr_info("PMU erratum BJ122, BV98, HSD29 worked around, HT is on\n");
  4123. return 0;
  4124. }
  4125. cpus_read_lock();
  4126. hardlockup_detector_perf_stop();
  4127. x86_pmu.flags &= ~(PMU_FL_EXCL_CNTRS | PMU_FL_EXCL_ENABLED);
  4128. x86_pmu.start_scheduling = NULL;
  4129. x86_pmu.commit_scheduling = NULL;
  4130. x86_pmu.stop_scheduling = NULL;
  4131. hardlockup_detector_perf_restart();
  4132. for_each_online_cpu(c)
  4133. free_excl_cntrs(c);
  4134. cpus_read_unlock();
  4135. pr_info("PMU erratum BJ122, BV98, HSD29 workaround disabled, HT off\n");
  4136. return 0;
  4137. }
  4138. subsys_initcall(fixup_ht_bug)