core.c 128 KB

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