perf_event_intel.c 101 KB

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