perf_event_intel.c 93 KB

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