i915_perf.c 153 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262
  1. /*
  2. * Copyright © 2015-2016 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Robert Bragg <robert@sixbynine.org>
  25. */
  26. /**
  27. * DOC: i915 Perf Overview
  28. *
  29. * Gen graphics supports a large number of performance counters that can help
  30. * driver and application developers understand and optimize their use of the
  31. * GPU.
  32. *
  33. * This i915 perf interface enables userspace to configure and open a file
  34. * descriptor representing a stream of GPU metrics which can then be read() as
  35. * a stream of sample records.
  36. *
  37. * The interface is particularly suited to exposing buffered metrics that are
  38. * captured by DMA from the GPU, unsynchronized with and unrelated to the CPU.
  39. *
  40. * Streams representing a single context are accessible to applications with a
  41. * corresponding drm file descriptor, such that OpenGL can use the interface
  42. * without special privileges. Access to system-wide metrics requires root
  43. * privileges by default, unless changed via the dev.i915.perf_event_paranoid
  44. * sysctl option.
  45. *
  46. */
  47. /**
  48. * DOC: i915 Perf History and Comparison with Core Perf
  49. *
  50. * The interface was initially inspired by the core Perf infrastructure but
  51. * some notable differences are:
  52. *
  53. * i915 perf file descriptors represent a "stream" instead of an "event"; where
  54. * a perf event primarily corresponds to a single 64bit value, while a stream
  55. * might sample sets of tightly-coupled counters, depending on the
  56. * configuration. For example the Gen OA unit isn't designed to support
  57. * orthogonal configurations of individual counters; it's configured for a set
  58. * of related counters. Samples for an i915 perf stream capturing OA metrics
  59. * will include a set of counter values packed in a compact HW specific format.
  60. * The OA unit supports a number of different packing formats which can be
  61. * selected by the user opening the stream. Perf has support for grouping
  62. * events, but each event in the group is configured, validated and
  63. * authenticated individually with separate system calls.
  64. *
  65. * i915 perf stream configurations are provided as an array of u64 (key,value)
  66. * pairs, instead of a fixed struct with multiple miscellaneous config members,
  67. * interleaved with event-type specific members.
  68. *
  69. * i915 perf doesn't support exposing metrics via an mmap'd circular buffer.
  70. * The supported metrics are being written to memory by the GPU unsynchronized
  71. * with the CPU, using HW specific packing formats for counter sets. Sometimes
  72. * the constraints on HW configuration require reports to be filtered before it
  73. * would be acceptable to expose them to unprivileged applications - to hide
  74. * the metrics of other processes/contexts. For these use cases a read() based
  75. * interface is a good fit, and provides an opportunity to filter data as it
  76. * gets copied from the GPU mapped buffers to userspace buffers.
  77. *
  78. *
  79. * Issues hit with first prototype based on Core Perf
  80. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  81. *
  82. * The first prototype of this driver was based on the core perf
  83. * infrastructure, and while we did make that mostly work, with some changes to
  84. * perf, we found we were breaking or working around too many assumptions baked
  85. * into perf's currently cpu centric design.
  86. *
  87. * In the end we didn't see a clear benefit to making perf's implementation and
  88. * interface more complex by changing design assumptions while we knew we still
  89. * wouldn't be able to use any existing perf based userspace tools.
  90. *
  91. * Also considering the Gen specific nature of the Observability hardware and
  92. * how userspace will sometimes need to combine i915 perf OA metrics with
  93. * side-band OA data captured via MI_REPORT_PERF_COUNT commands; we're
  94. * expecting the interface to be used by a platform specific userspace such as
  95. * OpenGL or tools. This is to say; we aren't inherently missing out on having
  96. * a standard vendor/architecture agnostic interface by not using perf.
  97. *
  98. *
  99. * For posterity, in case we might re-visit trying to adapt core perf to be
  100. * better suited to exposing i915 metrics these were the main pain points we
  101. * hit:
  102. *
  103. * - The perf based OA PMU driver broke some significant design assumptions:
  104. *
  105. * Existing perf pmus are used for profiling work on a cpu and we were
  106. * introducing the idea of _IS_DEVICE pmus with different security
  107. * implications, the need to fake cpu-related data (such as user/kernel
  108. * registers) to fit with perf's current design, and adding _DEVICE records
  109. * as a way to forward device-specific status records.
  110. *
  111. * The OA unit writes reports of counters into a circular buffer, without
  112. * involvement from the CPU, making our PMU driver the first of a kind.
  113. *
  114. * Given the way we were periodically forward data from the GPU-mapped, OA
  115. * buffer to perf's buffer, those bursts of sample writes looked to perf like
  116. * we were sampling too fast and so we had to subvert its throttling checks.
  117. *
  118. * Perf supports groups of counters and allows those to be read via
  119. * transactions internally but transactions currently seem designed to be
  120. * explicitly initiated from the cpu (say in response to a userspace read())
  121. * and while we could pull a report out of the OA buffer we can't
  122. * trigger a report from the cpu on demand.
  123. *
  124. * Related to being report based; the OA counters are configured in HW as a
  125. * set while perf generally expects counter configurations to be orthogonal.
  126. * Although counters can be associated with a group leader as they are
  127. * opened, there's no clear precedent for being able to provide group-wide
  128. * configuration attributes (for example we want to let userspace choose the
  129. * OA unit report format used to capture all counters in a set, or specify a
  130. * GPU context to filter metrics on). We avoided using perf's grouping
  131. * feature and forwarded OA reports to userspace via perf's 'raw' sample
  132. * field. This suited our userspace well considering how coupled the counters
  133. * are when dealing with normalizing. It would be inconvenient to split
  134. * counters up into separate events, only to require userspace to recombine
  135. * them. For Mesa it's also convenient to be forwarded raw, periodic reports
  136. * for combining with the side-band raw reports it captures using
  137. * MI_REPORT_PERF_COUNT commands.
  138. *
  139. * - As a side note on perf's grouping feature; there was also some concern
  140. * that using PERF_FORMAT_GROUP as a way to pack together counter values
  141. * would quite drastically inflate our sample sizes, which would likely
  142. * lower the effective sampling resolutions we could use when the available
  143. * memory bandwidth is limited.
  144. *
  145. * With the OA unit's report formats, counters are packed together as 32
  146. * or 40bit values, with the largest report size being 256 bytes.
  147. *
  148. * PERF_FORMAT_GROUP values are 64bit, but there doesn't appear to be a
  149. * documented ordering to the values, implying PERF_FORMAT_ID must also be
  150. * used to add a 64bit ID before each value; giving 16 bytes per counter.
  151. *
  152. * Related to counter orthogonality; we can't time share the OA unit, while
  153. * event scheduling is a central design idea within perf for allowing
  154. * userspace to open + enable more events than can be configured in HW at any
  155. * one time. The OA unit is not designed to allow re-configuration while in
  156. * use. We can't reconfigure the OA unit without losing internal OA unit
  157. * state which we can't access explicitly to save and restore. Reconfiguring
  158. * the OA unit is also relatively slow, involving ~100 register writes. From
  159. * userspace Mesa also depends on a stable OA configuration when emitting
  160. * MI_REPORT_PERF_COUNT commands and importantly the OA unit can't be
  161. * disabled while there are outstanding MI_RPC commands lest we hang the
  162. * command streamer.
  163. *
  164. * The contents of sample records aren't extensible by device drivers (i.e.
  165. * the sample_type bits). As an example; Sourab Gupta had been looking to
  166. * attach GPU timestamps to our OA samples. We were shoehorning OA reports
  167. * into sample records by using the 'raw' field, but it's tricky to pack more
  168. * than one thing into this field because events/core.c currently only lets a
  169. * pmu give a single raw data pointer plus len which will be copied into the
  170. * ring buffer. To include more than the OA report we'd have to copy the
  171. * report into an intermediate larger buffer. I'd been considering allowing a
  172. * vector of data+len values to be specified for copying the raw data, but
  173. * it felt like a kludge to being using the raw field for this purpose.
  174. *
  175. * - It felt like our perf based PMU was making some technical compromises
  176. * just for the sake of using perf:
  177. *
  178. * perf_event_open() requires events to either relate to a pid or a specific
  179. * cpu core, while our device pmu related to neither. Events opened with a
  180. * pid will be automatically enabled/disabled according to the scheduling of
  181. * that process - so not appropriate for us. When an event is related to a
  182. * cpu id, perf ensures pmu methods will be invoked via an inter process
  183. * interrupt on that core. To avoid invasive changes our userspace opened OA
  184. * perf events for a specific cpu. This was workable but it meant the
  185. * majority of the OA driver ran in atomic context, including all OA report
  186. * forwarding, which wasn't really necessary in our case and seems to make
  187. * our locking requirements somewhat complex as we handled the interaction
  188. * with the rest of the i915 driver.
  189. */
  190. #include <linux/anon_inodes.h>
  191. #include <linux/nospec.h>
  192. #include <linux/sizes.h>
  193. #include <linux/uuid.h>
  194. #include "gem/i915_gem_context.h"
  195. #include "gem/i915_gem_internal.h"
  196. #include "gt/intel_engine_pm.h"
  197. #include "gt/intel_engine_regs.h"
  198. #include "gt/intel_engine_user.h"
  199. #include "gt/intel_execlists_submission.h"
  200. #include "gt/intel_gpu_commands.h"
  201. #include "gt/intel_gt.h"
  202. #include "gt/intel_gt_clock_utils.h"
  203. #include "gt/intel_gt_mcr.h"
  204. #include "gt/intel_gt_print.h"
  205. #include "gt/intel_gt_regs.h"
  206. #include "gt/intel_lrc.h"
  207. #include "gt/intel_lrc_reg.h"
  208. #include "gt/intel_rc6.h"
  209. #include "gt/intel_ring.h"
  210. #include "gt/uc/intel_guc_slpc.h"
  211. #include "i915_drv.h"
  212. #include "i915_file_private.h"
  213. #include "i915_perf.h"
  214. #include "i915_perf_oa_regs.h"
  215. #include "i915_reg.h"
  216. #include "i915_mmio_range.h"
  217. /* HW requires this to be a power of two, between 128k and 16M, though driver
  218. * is currently generally designed assuming the largest 16M size is used such
  219. * that the overflow cases are unlikely in normal operation.
  220. */
  221. #define OA_BUFFER_SIZE SZ_16M
  222. #define OA_TAKEN(tail, head) ((tail - head) & (OA_BUFFER_SIZE - 1))
  223. /**
  224. * DOC: OA Tail Pointer Race
  225. *
  226. * There's a HW race condition between OA unit tail pointer register updates and
  227. * writes to memory whereby the tail pointer can sometimes get ahead of what's
  228. * been written out to the OA buffer so far (in terms of what's visible to the
  229. * CPU).
  230. *
  231. * Although this can be observed explicitly while copying reports to userspace
  232. * by checking for a zeroed report-id field in tail reports, we want to account
  233. * for this earlier, as part of the oa_buffer_check_unlocked to avoid lots of
  234. * redundant read() attempts.
  235. *
  236. * We workaround this issue in oa_buffer_check_unlocked() by reading the reports
  237. * in the OA buffer, starting from the tail reported by the HW until we find a
  238. * report with its first 2 dwords not 0 meaning its previous report is
  239. * completely in memory and ready to be read. Those dwords are also set to 0
  240. * once read and the whole buffer is cleared upon OA buffer initialization. The
  241. * first dword is the reason for this report while the second is the timestamp,
  242. * making the chances of having those 2 fields at 0 fairly unlikely. A more
  243. * detailed explanation is available in oa_buffer_check_unlocked().
  244. *
  245. * Most of the implementation details for this workaround are in
  246. * oa_buffer_check_unlocked() and _append_oa_reports()
  247. *
  248. * Note for posterity: previously the driver used to define an effective tail
  249. * pointer that lagged the real pointer by a 'tail margin' measured in bytes
  250. * derived from %OA_TAIL_MARGIN_NSEC and the configured sampling frequency.
  251. * This was flawed considering that the OA unit may also automatically generate
  252. * non-periodic reports (such as on context switch) or the OA unit may be
  253. * enabled without any periodic sampling.
  254. */
  255. #define OA_TAIL_MARGIN_NSEC 100000ULL
  256. #define INVALID_TAIL_PTR 0xffffffff
  257. /* The default frequency for checking whether the OA unit has written new
  258. * reports to the circular OA buffer...
  259. */
  260. #define DEFAULT_POLL_FREQUENCY_HZ 200
  261. #define DEFAULT_POLL_PERIOD_NS (NSEC_PER_SEC / DEFAULT_POLL_FREQUENCY_HZ)
  262. /* for sysctl proc_dointvec_minmax of dev.i915.perf_stream_paranoid */
  263. static u32 i915_perf_stream_paranoid = true;
  264. /* The maximum exponent the hardware accepts is 63 (essentially it selects one
  265. * of the 64bit timestamp bits to trigger reports from) but there's currently
  266. * no known use case for sampling as infrequently as once per 47 thousand years.
  267. *
  268. * Since the timestamps included in OA reports are only 32bits it seems
  269. * reasonable to limit the OA exponent where it's still possible to account for
  270. * overflow in OA report timestamps.
  271. */
  272. #define OA_EXPONENT_MAX 31
  273. #define INVALID_CTX_ID 0xffffffff
  274. /* On Gen8+ automatically triggered OA reports include a 'reason' field... */
  275. #define OAREPORT_REASON_MASK 0x3f
  276. #define OAREPORT_REASON_MASK_EXTENDED 0x7f
  277. #define OAREPORT_REASON_SHIFT 19
  278. #define OAREPORT_REASON_TIMER (1<<0)
  279. #define OAREPORT_REASON_CTX_SWITCH (1<<3)
  280. #define OAREPORT_REASON_CLK_RATIO (1<<5)
  281. #define HAS_MI_SET_PREDICATE(i915) (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 55))
  282. /* For sysctl proc_dointvec_minmax of i915_oa_max_sample_rate
  283. *
  284. * The highest sampling frequency we can theoretically program the OA unit
  285. * with is always half the timestamp frequency: E.g. 6.25Mhz for Haswell.
  286. *
  287. * Initialized just before we register the sysctl parameter.
  288. */
  289. static int oa_sample_rate_hard_limit;
  290. /* Theoretically we can program the OA unit to sample every 160ns but don't
  291. * allow that by default unless root...
  292. *
  293. * The default threshold of 100000Hz is based on perf's similar
  294. * kernel.perf_event_max_sample_rate sysctl parameter.
  295. */
  296. static u32 i915_oa_max_sample_rate = 100000;
  297. /* XXX: beware if future OA HW adds new report formats that the current
  298. * code assumes all reports have a power-of-two size and ~(size - 1) can
  299. * be used as a mask to align the OA tail pointer.
  300. */
  301. static const struct i915_oa_format oa_formats[I915_OA_FORMAT_MAX] = {
  302. [I915_OA_FORMAT_A13] = { 0, 64 },
  303. [I915_OA_FORMAT_A29] = { 1, 128 },
  304. [I915_OA_FORMAT_A13_B8_C8] = { 2, 128 },
  305. /* A29_B8_C8 Disallowed as 192 bytes doesn't factor into buffer size */
  306. [I915_OA_FORMAT_B4_C8] = { 4, 64 },
  307. [I915_OA_FORMAT_A45_B8_C8] = { 5, 256 },
  308. [I915_OA_FORMAT_B4_C8_A16] = { 6, 128 },
  309. [I915_OA_FORMAT_C4_B8] = { 7, 64 },
  310. [I915_OA_FORMAT_A12] = { 0, 64 },
  311. [I915_OA_FORMAT_A12_B8_C8] = { 2, 128 },
  312. [I915_OA_FORMAT_A32u40_A4u32_B8_C8] = { 5, 256 },
  313. [I915_OAR_FORMAT_A32u40_A4u32_B8_C8] = { 5, 256 },
  314. [I915_OA_FORMAT_A24u40_A14u32_B8_C8] = { 5, 256 },
  315. [I915_OAM_FORMAT_MPEC8u64_B8_C8] = { 1, 192, TYPE_OAM, HDR_64_BIT },
  316. [I915_OAM_FORMAT_MPEC8u32_B8_C8] = { 2, 128, TYPE_OAM, HDR_64_BIT },
  317. };
  318. static const u32 mtl_oa_base[] = {
  319. [PERF_GROUP_OAM_SAMEDIA_0] = 0x393000,
  320. };
  321. #define SAMPLE_OA_REPORT (1<<0)
  322. /**
  323. * struct perf_open_properties - for validated properties given to open a stream
  324. * @sample_flags: `DRM_I915_PERF_PROP_SAMPLE_*` properties are tracked as flags
  325. * @single_context: Whether a single or all gpu contexts should be monitored
  326. * @hold_preemption: Whether the preemption is disabled for the filtered
  327. * context
  328. * @ctx_handle: A gem ctx handle for use with @single_context
  329. * @metrics_set: An ID for an OA unit metric set advertised via sysfs
  330. * @oa_format: An OA unit HW report format
  331. * @oa_periodic: Whether to enable periodic OA unit sampling
  332. * @oa_period_exponent: The OA unit sampling period is derived from this
  333. * @engine: The engine (typically rcs0) being monitored by the OA unit
  334. * @has_sseu: Whether @sseu was specified by userspace
  335. * @sseu: internal SSEU configuration computed either from the userspace
  336. * specified configuration in the opening parameters or a default value
  337. * (see get_default_sseu_config())
  338. * @poll_oa_period: The period in nanoseconds at which the CPU will check for OA
  339. * data availability
  340. *
  341. * As read_properties_unlocked() enumerates and validates the properties given
  342. * to open a stream of metrics the configuration is built up in the structure
  343. * which starts out zero initialized.
  344. */
  345. struct perf_open_properties {
  346. u32 sample_flags;
  347. u64 single_context:1;
  348. u64 hold_preemption:1;
  349. u64 ctx_handle;
  350. /* OA sampling state */
  351. int metrics_set;
  352. int oa_format;
  353. bool oa_periodic;
  354. int oa_period_exponent;
  355. struct intel_engine_cs *engine;
  356. bool has_sseu;
  357. struct intel_sseu sseu;
  358. u64 poll_oa_period;
  359. };
  360. struct i915_oa_config_bo {
  361. struct llist_node node;
  362. struct i915_oa_config *oa_config;
  363. struct i915_vma *vma;
  364. };
  365. static struct ctl_table_header *sysctl_header;
  366. static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer);
  367. void i915_oa_config_release(struct kref *ref)
  368. {
  369. struct i915_oa_config *oa_config =
  370. container_of(ref, typeof(*oa_config), ref);
  371. kfree(oa_config->flex_regs);
  372. kfree(oa_config->b_counter_regs);
  373. kfree(oa_config->mux_regs);
  374. kfree_rcu(oa_config, rcu);
  375. }
  376. struct i915_oa_config *
  377. i915_perf_get_oa_config(struct i915_perf *perf, int metrics_set)
  378. {
  379. struct i915_oa_config *oa_config;
  380. rcu_read_lock();
  381. oa_config = idr_find(&perf->metrics_idr, metrics_set);
  382. if (oa_config)
  383. oa_config = i915_oa_config_get(oa_config);
  384. rcu_read_unlock();
  385. return oa_config;
  386. }
  387. static void free_oa_config_bo(struct i915_oa_config_bo *oa_bo)
  388. {
  389. i915_oa_config_put(oa_bo->oa_config);
  390. i915_vma_put(oa_bo->vma);
  391. kfree(oa_bo);
  392. }
  393. static inline const
  394. struct i915_perf_regs *__oa_regs(struct i915_perf_stream *stream)
  395. {
  396. return &stream->engine->oa_group->regs;
  397. }
  398. static u32 gen12_oa_hw_tail_read(struct i915_perf_stream *stream)
  399. {
  400. struct intel_uncore *uncore = stream->uncore;
  401. return intel_uncore_read(uncore, __oa_regs(stream)->oa_tail_ptr) &
  402. GEN12_OAG_OATAILPTR_MASK;
  403. }
  404. static u32 gen8_oa_hw_tail_read(struct i915_perf_stream *stream)
  405. {
  406. struct intel_uncore *uncore = stream->uncore;
  407. return intel_uncore_read(uncore, GEN8_OATAILPTR) & GEN8_OATAILPTR_MASK;
  408. }
  409. static u32 gen7_oa_hw_tail_read(struct i915_perf_stream *stream)
  410. {
  411. struct intel_uncore *uncore = stream->uncore;
  412. u32 oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
  413. return oastatus1 & GEN7_OASTATUS1_TAIL_MASK;
  414. }
  415. #define oa_report_header_64bit(__s) \
  416. ((__s)->oa_buffer.format->header == HDR_64_BIT)
  417. static u64 oa_report_id(struct i915_perf_stream *stream, void *report)
  418. {
  419. return oa_report_header_64bit(stream) ? *(u64 *)report : *(u32 *)report;
  420. }
  421. static u64 oa_report_reason(struct i915_perf_stream *stream, void *report)
  422. {
  423. return (oa_report_id(stream, report) >> OAREPORT_REASON_SHIFT) &
  424. (GRAPHICS_VER(stream->perf->i915) == 12 ?
  425. OAREPORT_REASON_MASK_EXTENDED :
  426. OAREPORT_REASON_MASK);
  427. }
  428. static void oa_report_id_clear(struct i915_perf_stream *stream, u32 *report)
  429. {
  430. if (oa_report_header_64bit(stream))
  431. *(u64 *)report = 0;
  432. else
  433. *report = 0;
  434. }
  435. static bool oa_report_ctx_invalid(struct i915_perf_stream *stream, void *report)
  436. {
  437. return !(oa_report_id(stream, report) &
  438. stream->perf->gen8_valid_ctx_bit);
  439. }
  440. static u64 oa_timestamp(struct i915_perf_stream *stream, void *report)
  441. {
  442. return oa_report_header_64bit(stream) ?
  443. *((u64 *)report + 1) :
  444. *((u32 *)report + 1);
  445. }
  446. static void oa_timestamp_clear(struct i915_perf_stream *stream, u32 *report)
  447. {
  448. if (oa_report_header_64bit(stream))
  449. *(u64 *)&report[2] = 0;
  450. else
  451. report[1] = 0;
  452. }
  453. static u32 oa_context_id(struct i915_perf_stream *stream, u32 *report)
  454. {
  455. u32 ctx_id = oa_report_header_64bit(stream) ? report[4] : report[2];
  456. return ctx_id & stream->specific_ctx_id_mask;
  457. }
  458. static void oa_context_id_squash(struct i915_perf_stream *stream, u32 *report)
  459. {
  460. if (oa_report_header_64bit(stream))
  461. report[4] = INVALID_CTX_ID;
  462. else
  463. report[2] = INVALID_CTX_ID;
  464. }
  465. /**
  466. * oa_buffer_check_unlocked - check for data and update tail ptr state
  467. * @stream: i915 stream instance
  468. *
  469. * This is either called via fops (for blocking reads in user ctx) or the poll
  470. * check hrtimer (atomic ctx) to check the OA buffer tail pointer and check
  471. * if there is data available for userspace to read.
  472. *
  473. * This function is central to providing a workaround for the OA unit tail
  474. * pointer having a race with respect to what data is visible to the CPU.
  475. * It is responsible for reading tail pointers from the hardware and giving
  476. * the pointers time to 'age' before they are made available for reading.
  477. * (See description of OA_TAIL_MARGIN_NSEC above for further details.)
  478. *
  479. * Besides returning true when there is data available to read() this function
  480. * also updates the tail in the oa_buffer object.
  481. *
  482. * Note: It's safe to read OA config state here unlocked, assuming that this is
  483. * only called while the stream is enabled, while the global OA configuration
  484. * can't be modified.
  485. *
  486. * Returns: %true if the OA buffer contains data, else %false
  487. */
  488. static bool oa_buffer_check_unlocked(struct i915_perf_stream *stream)
  489. {
  490. u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
  491. int report_size = stream->oa_buffer.format->size;
  492. u32 tail, hw_tail;
  493. unsigned long flags;
  494. bool pollin;
  495. u32 partial_report_size;
  496. /*
  497. * We have to consider the (unlikely) possibility that read() errors
  498. * could result in an OA buffer reset which might reset the head and
  499. * tail state.
  500. */
  501. spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
  502. hw_tail = stream->perf->ops.oa_hw_tail_read(stream);
  503. hw_tail -= gtt_offset;
  504. /*
  505. * The tail pointer increases in 64 byte increments, not in report_size
  506. * steps. Also the report size may not be a power of 2. Compute
  507. * potentially partially landed report in the OA buffer
  508. */
  509. partial_report_size = OA_TAKEN(hw_tail, stream->oa_buffer.tail);
  510. partial_report_size %= report_size;
  511. /* Subtract partial amount off the tail */
  512. hw_tail = OA_TAKEN(hw_tail, partial_report_size);
  513. tail = hw_tail;
  514. /*
  515. * Walk the stream backward until we find a report with report
  516. * id and timestamp not at 0. Since the circular buffer pointers
  517. * progress by increments of 64 bytes and that reports can be up
  518. * to 256 bytes long, we can't tell whether a report has fully
  519. * landed in memory before the report id and timestamp of the
  520. * following report have effectively landed.
  521. *
  522. * This is assuming that the writes of the OA unit land in
  523. * memory in the order they were written to.
  524. * If not : (╯°□°)╯︵ ┻━┻
  525. */
  526. while (OA_TAKEN(tail, stream->oa_buffer.tail) >= report_size) {
  527. void *report = stream->oa_buffer.vaddr + tail;
  528. if (oa_report_id(stream, report) ||
  529. oa_timestamp(stream, report))
  530. break;
  531. tail = (tail - report_size) & (OA_BUFFER_SIZE - 1);
  532. }
  533. if (OA_TAKEN(hw_tail, tail) > report_size &&
  534. __ratelimit(&stream->perf->tail_pointer_race))
  535. drm_notice(&stream->uncore->i915->drm,
  536. "unlanded report(s) head=0x%x tail=0x%x hw_tail=0x%x\n",
  537. stream->oa_buffer.head, tail, hw_tail);
  538. stream->oa_buffer.tail = tail;
  539. pollin = OA_TAKEN(stream->oa_buffer.tail,
  540. stream->oa_buffer.head) >= report_size;
  541. spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
  542. return pollin;
  543. }
  544. /**
  545. * append_oa_status - Appends a status record to a userspace read() buffer.
  546. * @stream: An i915-perf stream opened for OA metrics
  547. * @buf: destination buffer given by userspace
  548. * @count: the number of bytes userspace wants to read
  549. * @offset: (inout): the current position for writing into @buf
  550. * @type: The kind of status to report to userspace
  551. *
  552. * Writes a status record (such as `DRM_I915_PERF_RECORD_OA_REPORT_LOST`)
  553. * into the userspace read() buffer.
  554. *
  555. * The @buf @offset will only be updated on success.
  556. *
  557. * Returns: 0 on success, negative error code on failure.
  558. */
  559. static int append_oa_status(struct i915_perf_stream *stream,
  560. char __user *buf,
  561. size_t count,
  562. size_t *offset,
  563. enum drm_i915_perf_record_type type)
  564. {
  565. struct drm_i915_perf_record_header header = { type, 0, sizeof(header) };
  566. if ((count - *offset) < header.size)
  567. return -ENOSPC;
  568. if (copy_to_user(buf + *offset, &header, sizeof(header)))
  569. return -EFAULT;
  570. (*offset) += header.size;
  571. return 0;
  572. }
  573. /**
  574. * append_oa_sample - Copies single OA report into userspace read() buffer.
  575. * @stream: An i915-perf stream opened for OA metrics
  576. * @buf: destination buffer given by userspace
  577. * @count: the number of bytes userspace wants to read
  578. * @offset: (inout): the current position for writing into @buf
  579. * @report: A single OA report to (optionally) include as part of the sample
  580. *
  581. * The contents of a sample are configured through `DRM_I915_PERF_PROP_SAMPLE_*`
  582. * properties when opening a stream, tracked as `stream->sample_flags`. This
  583. * function copies the requested components of a single sample to the given
  584. * read() @buf.
  585. *
  586. * The @buf @offset will only be updated on success.
  587. *
  588. * Returns: 0 on success, negative error code on failure.
  589. */
  590. static int append_oa_sample(struct i915_perf_stream *stream,
  591. char __user *buf,
  592. size_t count,
  593. size_t *offset,
  594. const u8 *report)
  595. {
  596. int report_size = stream->oa_buffer.format->size;
  597. struct drm_i915_perf_record_header header;
  598. int report_size_partial;
  599. u8 *oa_buf_end;
  600. header.type = DRM_I915_PERF_RECORD_SAMPLE;
  601. header.pad = 0;
  602. header.size = stream->sample_size;
  603. if ((count - *offset) < header.size)
  604. return -ENOSPC;
  605. buf += *offset;
  606. if (copy_to_user(buf, &header, sizeof(header)))
  607. return -EFAULT;
  608. buf += sizeof(header);
  609. oa_buf_end = stream->oa_buffer.vaddr + OA_BUFFER_SIZE;
  610. report_size_partial = oa_buf_end - report;
  611. if (report_size_partial < report_size) {
  612. if (copy_to_user(buf, report, report_size_partial))
  613. return -EFAULT;
  614. buf += report_size_partial;
  615. if (copy_to_user(buf, stream->oa_buffer.vaddr,
  616. report_size - report_size_partial))
  617. return -EFAULT;
  618. } else if (copy_to_user(buf, report, report_size)) {
  619. return -EFAULT;
  620. }
  621. (*offset) += header.size;
  622. return 0;
  623. }
  624. /**
  625. * gen8_append_oa_reports - Copies all buffered OA reports into
  626. * userspace read() buffer.
  627. * @stream: An i915-perf stream opened for OA metrics
  628. * @buf: destination buffer given by userspace
  629. * @count: the number of bytes userspace wants to read
  630. * @offset: (inout): the current position for writing into @buf
  631. *
  632. * Notably any error condition resulting in a short read (-%ENOSPC or
  633. * -%EFAULT) will be returned even though one or more records may
  634. * have been successfully copied. In this case it's up to the caller
  635. * to decide if the error should be squashed before returning to
  636. * userspace.
  637. *
  638. * Note: reports are consumed from the head, and appended to the
  639. * tail, so the tail chases the head?... If you think that's mad
  640. * and back-to-front you're not alone, but this follows the
  641. * Gen PRM naming convention.
  642. *
  643. * Returns: 0 on success, negative error code on failure.
  644. */
  645. static int gen8_append_oa_reports(struct i915_perf_stream *stream,
  646. char __user *buf,
  647. size_t count,
  648. size_t *offset)
  649. {
  650. struct intel_uncore *uncore = stream->uncore;
  651. int report_size = stream->oa_buffer.format->size;
  652. u8 *oa_buf_base = stream->oa_buffer.vaddr;
  653. u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
  654. u32 mask = (OA_BUFFER_SIZE - 1);
  655. size_t start_offset = *offset;
  656. unsigned long flags;
  657. u32 head, tail;
  658. int ret = 0;
  659. if (drm_WARN_ON(&uncore->i915->drm, !stream->enabled))
  660. return -EIO;
  661. spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
  662. head = stream->oa_buffer.head;
  663. tail = stream->oa_buffer.tail;
  664. spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
  665. /*
  666. * An out of bounds or misaligned head or tail pointer implies a driver
  667. * bug since we validate + align the tail pointers we read from the
  668. * hardware and we are in full control of the head pointer which should
  669. * only be incremented by multiples of the report size.
  670. */
  671. if (drm_WARN_ONCE(&uncore->i915->drm,
  672. head > OA_BUFFER_SIZE ||
  673. tail > OA_BUFFER_SIZE,
  674. "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
  675. head, tail))
  676. return -EIO;
  677. for (/* none */;
  678. OA_TAKEN(tail, head);
  679. head = (head + report_size) & mask) {
  680. u8 *report = oa_buf_base + head;
  681. u32 *report32 = (void *)report;
  682. u32 ctx_id;
  683. u64 reason;
  684. /*
  685. * The reason field includes flags identifying what
  686. * triggered this specific report (mostly timer
  687. * triggered or e.g. due to a context switch).
  688. */
  689. reason = oa_report_reason(stream, report);
  690. ctx_id = oa_context_id(stream, report32);
  691. /*
  692. * Squash whatever is in the CTX_ID field if it's marked as
  693. * invalid to be sure we avoid false-positive, single-context
  694. * filtering below...
  695. *
  696. * Note: that we don't clear the valid_ctx_bit so userspace can
  697. * understand that the ID has been squashed by the kernel.
  698. *
  699. * Update:
  700. *
  701. * On XEHP platforms the behavior of context id valid bit has
  702. * changed compared to prior platforms. To describe this, we
  703. * define a few terms:
  704. *
  705. * context-switch-report: This is a report with the reason type
  706. * being context-switch. It is generated when a context switches
  707. * out.
  708. *
  709. * context-valid-bit: A bit that is set in the report ID field
  710. * to indicate that a valid context has been loaded.
  711. *
  712. * gpu-idle: A condition characterized by a
  713. * context-switch-report with context-valid-bit set to 0.
  714. *
  715. * On prior platforms, context-id-valid bit is set to 0 only
  716. * when GPU goes idle. In all other reports, it is set to 1.
  717. *
  718. * On XEHP platforms, context-valid-bit is set to 1 in a context
  719. * switch report if a new context switched in. For all other
  720. * reports it is set to 0.
  721. *
  722. * This change in behavior causes an issue with MMIO triggered
  723. * reports. MMIO triggered reports have the markers in the
  724. * context ID field and the context-valid-bit is 0. The logic
  725. * below to squash the context ID would render the report
  726. * useless since the user will not be able to find it in the OA
  727. * buffer. Since MMIO triggered reports exist only on XEHP,
  728. * we should avoid squashing these for XEHP platforms.
  729. */
  730. if (oa_report_ctx_invalid(stream, report) &&
  731. GRAPHICS_VER_FULL(stream->engine->i915) < IP_VER(12, 55)) {
  732. ctx_id = INVALID_CTX_ID;
  733. oa_context_id_squash(stream, report32);
  734. }
  735. /*
  736. * NB: For Gen 8 the OA unit no longer supports clock gating
  737. * off for a specific context and the kernel can't securely
  738. * stop the counters from updating as system-wide / global
  739. * values.
  740. *
  741. * Automatic reports now include a context ID so reports can be
  742. * filtered on the cpu but it's not worth trying to
  743. * automatically subtract/hide counter progress for other
  744. * contexts while filtering since we can't stop userspace
  745. * issuing MI_REPORT_PERF_COUNT commands which would still
  746. * provide a side-band view of the real values.
  747. *
  748. * To allow userspace (such as Mesa/GL_INTEL_performance_query)
  749. * to normalize counters for a single filtered context then it
  750. * needs be forwarded bookend context-switch reports so that it
  751. * can track switches in between MI_REPORT_PERF_COUNT commands
  752. * and can itself subtract/ignore the progress of counters
  753. * associated with other contexts. Note that the hardware
  754. * automatically triggers reports when switching to a new
  755. * context which are tagged with the ID of the newly active
  756. * context. To avoid the complexity (and likely fragility) of
  757. * reading ahead while parsing reports to try and minimize
  758. * forwarding redundant context switch reports (i.e. between
  759. * other, unrelated contexts) we simply elect to forward them
  760. * all.
  761. *
  762. * We don't rely solely on the reason field to identify context
  763. * switches since it's not-uncommon for periodic samples to
  764. * identify a switch before any 'context switch' report.
  765. */
  766. if (!stream->ctx ||
  767. stream->specific_ctx_id == ctx_id ||
  768. stream->oa_buffer.last_ctx_id == stream->specific_ctx_id ||
  769. reason & OAREPORT_REASON_CTX_SWITCH) {
  770. /*
  771. * While filtering for a single context we avoid
  772. * leaking the IDs of other contexts.
  773. */
  774. if (stream->ctx &&
  775. stream->specific_ctx_id != ctx_id) {
  776. oa_context_id_squash(stream, report32);
  777. }
  778. ret = append_oa_sample(stream, buf, count, offset,
  779. report);
  780. if (ret)
  781. break;
  782. stream->oa_buffer.last_ctx_id = ctx_id;
  783. }
  784. if (is_power_of_2(report_size)) {
  785. /*
  786. * Clear out the report id and timestamp as a means
  787. * to detect unlanded reports.
  788. */
  789. oa_report_id_clear(stream, report32);
  790. oa_timestamp_clear(stream, report32);
  791. } else {
  792. u8 *oa_buf_end = stream->oa_buffer.vaddr +
  793. OA_BUFFER_SIZE;
  794. u32 part = oa_buf_end - (u8 *)report32;
  795. /* Zero out the entire report */
  796. if (report_size <= part) {
  797. memset(report32, 0, report_size);
  798. } else {
  799. memset(report32, 0, part);
  800. memset(oa_buf_base, 0, report_size - part);
  801. }
  802. }
  803. }
  804. if (start_offset != *offset) {
  805. i915_reg_t oaheadptr;
  806. oaheadptr = GRAPHICS_VER(stream->perf->i915) == 12 ?
  807. __oa_regs(stream)->oa_head_ptr :
  808. GEN8_OAHEADPTR;
  809. spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
  810. /*
  811. * We removed the gtt_offset for the copy loop above, indexing
  812. * relative to oa_buf_base so put back here...
  813. */
  814. intel_uncore_write(uncore, oaheadptr,
  815. (head + gtt_offset) & GEN12_OAG_OAHEADPTR_MASK);
  816. stream->oa_buffer.head = head;
  817. spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
  818. }
  819. return ret;
  820. }
  821. /**
  822. * gen8_oa_read - copy status records then buffered OA reports
  823. * @stream: An i915-perf stream opened for OA metrics
  824. * @buf: destination buffer given by userspace
  825. * @count: the number of bytes userspace wants to read
  826. * @offset: (inout): the current position for writing into @buf
  827. *
  828. * Checks OA unit status registers and if necessary appends corresponding
  829. * status records for userspace (such as for a buffer full condition) and then
  830. * initiate appending any buffered OA reports.
  831. *
  832. * Updates @offset according to the number of bytes successfully copied into
  833. * the userspace buffer.
  834. *
  835. * NB: some data may be successfully copied to the userspace buffer
  836. * even if an error is returned, and this is reflected in the
  837. * updated @offset.
  838. *
  839. * Returns: zero on success or a negative error code
  840. */
  841. static int gen8_oa_read(struct i915_perf_stream *stream,
  842. char __user *buf,
  843. size_t count,
  844. size_t *offset)
  845. {
  846. struct intel_uncore *uncore = stream->uncore;
  847. u32 oastatus;
  848. i915_reg_t oastatus_reg;
  849. int ret;
  850. if (drm_WARN_ON(&uncore->i915->drm, !stream->oa_buffer.vaddr))
  851. return -EIO;
  852. oastatus_reg = GRAPHICS_VER(stream->perf->i915) == 12 ?
  853. __oa_regs(stream)->oa_status :
  854. GEN8_OASTATUS;
  855. oastatus = intel_uncore_read(uncore, oastatus_reg);
  856. /*
  857. * We treat OABUFFER_OVERFLOW as a significant error:
  858. *
  859. * Although theoretically we could handle this more gracefully
  860. * sometimes, some Gens don't correctly suppress certain
  861. * automatically triggered reports in this condition and so we
  862. * have to assume that old reports are now being trampled
  863. * over.
  864. *
  865. * Considering how we don't currently give userspace control
  866. * over the OA buffer size and always configure a large 16MB
  867. * buffer, then a buffer overflow does anyway likely indicate
  868. * that something has gone quite badly wrong.
  869. */
  870. if (oastatus & GEN8_OASTATUS_OABUFFER_OVERFLOW) {
  871. ret = append_oa_status(stream, buf, count, offset,
  872. DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
  873. if (ret)
  874. return ret;
  875. drm_dbg(&stream->perf->i915->drm,
  876. "OA buffer overflow (exponent = %d): force restart\n",
  877. stream->period_exponent);
  878. stream->perf->ops.oa_disable(stream);
  879. stream->perf->ops.oa_enable(stream);
  880. /*
  881. * Note: .oa_enable() is expected to re-init the oabuffer and
  882. * reset GEN8_OASTATUS for us
  883. */
  884. oastatus = intel_uncore_read(uncore, oastatus_reg);
  885. }
  886. if (oastatus & GEN8_OASTATUS_REPORT_LOST) {
  887. ret = append_oa_status(stream, buf, count, offset,
  888. DRM_I915_PERF_RECORD_OA_REPORT_LOST);
  889. if (ret)
  890. return ret;
  891. intel_uncore_rmw(uncore, oastatus_reg,
  892. GEN8_OASTATUS_COUNTER_OVERFLOW |
  893. GEN8_OASTATUS_REPORT_LOST,
  894. IS_GRAPHICS_VER(uncore->i915, 8, 11) ?
  895. (GEN8_OASTATUS_HEAD_POINTER_WRAP |
  896. GEN8_OASTATUS_TAIL_POINTER_WRAP) : 0);
  897. }
  898. return gen8_append_oa_reports(stream, buf, count, offset);
  899. }
  900. /**
  901. * gen7_append_oa_reports - Copies all buffered OA reports into
  902. * userspace read() buffer.
  903. * @stream: An i915-perf stream opened for OA metrics
  904. * @buf: destination buffer given by userspace
  905. * @count: the number of bytes userspace wants to read
  906. * @offset: (inout): the current position for writing into @buf
  907. *
  908. * Notably any error condition resulting in a short read (-%ENOSPC or
  909. * -%EFAULT) will be returned even though one or more records may
  910. * have been successfully copied. In this case it's up to the caller
  911. * to decide if the error should be squashed before returning to
  912. * userspace.
  913. *
  914. * Note: reports are consumed from the head, and appended to the
  915. * tail, so the tail chases the head?... If you think that's mad
  916. * and back-to-front you're not alone, but this follows the
  917. * Gen PRM naming convention.
  918. *
  919. * Returns: 0 on success, negative error code on failure.
  920. */
  921. static int gen7_append_oa_reports(struct i915_perf_stream *stream,
  922. char __user *buf,
  923. size_t count,
  924. size_t *offset)
  925. {
  926. struct intel_uncore *uncore = stream->uncore;
  927. int report_size = stream->oa_buffer.format->size;
  928. u8 *oa_buf_base = stream->oa_buffer.vaddr;
  929. u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
  930. u32 mask = (OA_BUFFER_SIZE - 1);
  931. size_t start_offset = *offset;
  932. unsigned long flags;
  933. u32 head, tail;
  934. int ret = 0;
  935. if (drm_WARN_ON(&uncore->i915->drm, !stream->enabled))
  936. return -EIO;
  937. spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
  938. head = stream->oa_buffer.head;
  939. tail = stream->oa_buffer.tail;
  940. spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
  941. /* An out of bounds or misaligned head or tail pointer implies a driver
  942. * bug since we validate + align the tail pointers we read from the
  943. * hardware and we are in full control of the head pointer which should
  944. * only be incremented by multiples of the report size (notably also
  945. * all a power of two).
  946. */
  947. if (drm_WARN_ONCE(&uncore->i915->drm,
  948. head > OA_BUFFER_SIZE || head % report_size ||
  949. tail > OA_BUFFER_SIZE || tail % report_size,
  950. "Inconsistent OA buffer pointers: head = %u, tail = %u\n",
  951. head, tail))
  952. return -EIO;
  953. for (/* none */;
  954. OA_TAKEN(tail, head);
  955. head = (head + report_size) & mask) {
  956. u8 *report = oa_buf_base + head;
  957. u32 *report32 = (void *)report;
  958. /* All the report sizes factor neatly into the buffer
  959. * size so we never expect to see a report split
  960. * between the beginning and end of the buffer.
  961. *
  962. * Given the initial alignment check a misalignment
  963. * here would imply a driver bug that would result
  964. * in an overrun.
  965. */
  966. if (drm_WARN_ON(&uncore->i915->drm,
  967. (OA_BUFFER_SIZE - head) < report_size)) {
  968. drm_err(&uncore->i915->drm,
  969. "Spurious OA head ptr: non-integral report offset\n");
  970. break;
  971. }
  972. /* The report-ID field for periodic samples includes
  973. * some undocumented flags related to what triggered
  974. * the report and is never expected to be zero so we
  975. * can check that the report isn't invalid before
  976. * copying it to userspace...
  977. */
  978. if (report32[0] == 0) {
  979. if (__ratelimit(&stream->perf->spurious_report_rs))
  980. drm_notice(&uncore->i915->drm,
  981. "Skipping spurious, invalid OA report\n");
  982. continue;
  983. }
  984. ret = append_oa_sample(stream, buf, count, offset, report);
  985. if (ret)
  986. break;
  987. /* Clear out the first 2 dwords as a mean to detect unlanded
  988. * reports.
  989. */
  990. report32[0] = 0;
  991. report32[1] = 0;
  992. }
  993. if (start_offset != *offset) {
  994. spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
  995. intel_uncore_write(uncore, GEN7_OASTATUS2,
  996. ((head + gtt_offset) & GEN7_OASTATUS2_HEAD_MASK) |
  997. GEN7_OASTATUS2_MEM_SELECT_GGTT);
  998. stream->oa_buffer.head = head;
  999. spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
  1000. }
  1001. return ret;
  1002. }
  1003. /**
  1004. * gen7_oa_read - copy status records then buffered OA reports
  1005. * @stream: An i915-perf stream opened for OA metrics
  1006. * @buf: destination buffer given by userspace
  1007. * @count: the number of bytes userspace wants to read
  1008. * @offset: (inout): the current position for writing into @buf
  1009. *
  1010. * Checks Gen 7 specific OA unit status registers and if necessary appends
  1011. * corresponding status records for userspace (such as for a buffer full
  1012. * condition) and then initiate appending any buffered OA reports.
  1013. *
  1014. * Updates @offset according to the number of bytes successfully copied into
  1015. * the userspace buffer.
  1016. *
  1017. * Returns: zero on success or a negative error code
  1018. */
  1019. static int gen7_oa_read(struct i915_perf_stream *stream,
  1020. char __user *buf,
  1021. size_t count,
  1022. size_t *offset)
  1023. {
  1024. struct intel_uncore *uncore = stream->uncore;
  1025. u32 oastatus1;
  1026. int ret;
  1027. if (drm_WARN_ON(&uncore->i915->drm, !stream->oa_buffer.vaddr))
  1028. return -EIO;
  1029. oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
  1030. /* XXX: On Haswell we don't have a safe way to clear oastatus1
  1031. * bits while the OA unit is enabled (while the tail pointer
  1032. * may be updated asynchronously) so we ignore status bits
  1033. * that have already been reported to userspace.
  1034. */
  1035. oastatus1 &= ~stream->perf->gen7_latched_oastatus1;
  1036. /* We treat OABUFFER_OVERFLOW as a significant error:
  1037. *
  1038. * - The status can be interpreted to mean that the buffer is
  1039. * currently full (with a higher precedence than OA_TAKEN()
  1040. * which will start to report a near-empty buffer after an
  1041. * overflow) but it's awkward that we can't clear the status
  1042. * on Haswell, so without a reset we won't be able to catch
  1043. * the state again.
  1044. *
  1045. * - Since it also implies the HW has started overwriting old
  1046. * reports it may also affect our sanity checks for invalid
  1047. * reports when copying to userspace that assume new reports
  1048. * are being written to cleared memory.
  1049. *
  1050. * - In the future we may want to introduce a flight recorder
  1051. * mode where the driver will automatically maintain a safe
  1052. * guard band between head/tail, avoiding this overflow
  1053. * condition, but we avoid the added driver complexity for
  1054. * now.
  1055. */
  1056. if (unlikely(oastatus1 & GEN7_OASTATUS1_OABUFFER_OVERFLOW)) {
  1057. ret = append_oa_status(stream, buf, count, offset,
  1058. DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
  1059. if (ret)
  1060. return ret;
  1061. drm_dbg(&stream->perf->i915->drm,
  1062. "OA buffer overflow (exponent = %d): force restart\n",
  1063. stream->period_exponent);
  1064. stream->perf->ops.oa_disable(stream);
  1065. stream->perf->ops.oa_enable(stream);
  1066. oastatus1 = intel_uncore_read(uncore, GEN7_OASTATUS1);
  1067. }
  1068. if (unlikely(oastatus1 & GEN7_OASTATUS1_REPORT_LOST)) {
  1069. ret = append_oa_status(stream, buf, count, offset,
  1070. DRM_I915_PERF_RECORD_OA_REPORT_LOST);
  1071. if (ret)
  1072. return ret;
  1073. stream->perf->gen7_latched_oastatus1 |=
  1074. GEN7_OASTATUS1_REPORT_LOST;
  1075. }
  1076. return gen7_append_oa_reports(stream, buf, count, offset);
  1077. }
  1078. /**
  1079. * i915_oa_wait_unlocked - handles blocking IO until OA data available
  1080. * @stream: An i915-perf stream opened for OA metrics
  1081. *
  1082. * Called when userspace tries to read() from a blocking stream FD opened
  1083. * for OA metrics. It waits until the hrtimer callback finds a non-empty
  1084. * OA buffer and wakes us.
  1085. *
  1086. * Note: it's acceptable to have this return with some false positives
  1087. * since any subsequent read handling will return -EAGAIN if there isn't
  1088. * really data ready for userspace yet.
  1089. *
  1090. * Returns: zero on success or a negative error code
  1091. */
  1092. static int i915_oa_wait_unlocked(struct i915_perf_stream *stream)
  1093. {
  1094. /* We would wait indefinitely if periodic sampling is not enabled */
  1095. if (!stream->periodic)
  1096. return -EIO;
  1097. return wait_event_interruptible(stream->poll_wq,
  1098. oa_buffer_check_unlocked(stream));
  1099. }
  1100. /**
  1101. * i915_oa_poll_wait - call poll_wait() for an OA stream poll()
  1102. * @stream: An i915-perf stream opened for OA metrics
  1103. * @file: An i915 perf stream file
  1104. * @wait: poll() state table
  1105. *
  1106. * For handling userspace polling on an i915 perf stream opened for OA metrics,
  1107. * this starts a poll_wait with the wait queue that our hrtimer callback wakes
  1108. * when it sees data ready to read in the circular OA buffer.
  1109. */
  1110. static void i915_oa_poll_wait(struct i915_perf_stream *stream,
  1111. struct file *file,
  1112. poll_table *wait)
  1113. {
  1114. poll_wait(file, &stream->poll_wq, wait);
  1115. }
  1116. /**
  1117. * i915_oa_read - just calls through to &i915_oa_ops->read
  1118. * @stream: An i915-perf stream opened for OA metrics
  1119. * @buf: destination buffer given by userspace
  1120. * @count: the number of bytes userspace wants to read
  1121. * @offset: (inout): the current position for writing into @buf
  1122. *
  1123. * Updates @offset according to the number of bytes successfully copied into
  1124. * the userspace buffer.
  1125. *
  1126. * Returns: zero on success or a negative error code
  1127. */
  1128. static int i915_oa_read(struct i915_perf_stream *stream,
  1129. char __user *buf,
  1130. size_t count,
  1131. size_t *offset)
  1132. {
  1133. return stream->perf->ops.read(stream, buf, count, offset);
  1134. }
  1135. static struct intel_context *oa_pin_context(struct i915_perf_stream *stream)
  1136. {
  1137. struct i915_gem_engines_iter it;
  1138. struct i915_gem_context *ctx = stream->ctx;
  1139. struct intel_context *ce;
  1140. struct i915_gem_ww_ctx ww;
  1141. int err = -ENODEV;
  1142. for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
  1143. if (ce->engine != stream->engine) /* first match! */
  1144. continue;
  1145. err = 0;
  1146. break;
  1147. }
  1148. i915_gem_context_unlock_engines(ctx);
  1149. if (err)
  1150. return ERR_PTR(err);
  1151. i915_gem_ww_ctx_init(&ww, true);
  1152. retry:
  1153. /*
  1154. * As the ID is the gtt offset of the context's vma we
  1155. * pin the vma to ensure the ID remains fixed.
  1156. */
  1157. err = intel_context_pin_ww(ce, &ww);
  1158. if (err == -EDEADLK) {
  1159. err = i915_gem_ww_ctx_backoff(&ww);
  1160. if (!err)
  1161. goto retry;
  1162. }
  1163. i915_gem_ww_ctx_fini(&ww);
  1164. if (err)
  1165. return ERR_PTR(err);
  1166. stream->pinned_ctx = ce;
  1167. return stream->pinned_ctx;
  1168. }
  1169. static int
  1170. __store_reg_to_mem(struct i915_request *rq, i915_reg_t reg, u32 ggtt_offset)
  1171. {
  1172. u32 *cs, cmd;
  1173. cmd = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
  1174. if (GRAPHICS_VER(rq->i915) >= 8)
  1175. cmd++;
  1176. cs = intel_ring_begin(rq, 4);
  1177. if (IS_ERR(cs))
  1178. return PTR_ERR(cs);
  1179. *cs++ = cmd;
  1180. *cs++ = i915_mmio_reg_offset(reg);
  1181. *cs++ = ggtt_offset;
  1182. *cs++ = 0;
  1183. intel_ring_advance(rq, cs);
  1184. return 0;
  1185. }
  1186. static int
  1187. __read_reg(struct intel_context *ce, i915_reg_t reg, u32 ggtt_offset)
  1188. {
  1189. struct i915_request *rq;
  1190. int err;
  1191. rq = i915_request_create(ce);
  1192. if (IS_ERR(rq))
  1193. return PTR_ERR(rq);
  1194. i915_request_get(rq);
  1195. err = __store_reg_to_mem(rq, reg, ggtt_offset);
  1196. i915_request_add(rq);
  1197. if (!err && i915_request_wait(rq, 0, HZ / 2) < 0)
  1198. err = -ETIME;
  1199. i915_request_put(rq);
  1200. return err;
  1201. }
  1202. static int
  1203. gen12_guc_sw_ctx_id(struct intel_context *ce, u32 *ctx_id)
  1204. {
  1205. struct i915_vma *scratch;
  1206. u32 *val;
  1207. int err;
  1208. scratch = __vm_create_scratch_for_read_pinned(&ce->engine->gt->ggtt->vm, 4);
  1209. if (IS_ERR(scratch))
  1210. return PTR_ERR(scratch);
  1211. err = i915_vma_sync(scratch);
  1212. if (err)
  1213. goto err_scratch;
  1214. err = __read_reg(ce, RING_EXECLIST_STATUS_HI(ce->engine->mmio_base),
  1215. i915_ggtt_offset(scratch));
  1216. if (err)
  1217. goto err_scratch;
  1218. val = i915_gem_object_pin_map_unlocked(scratch->obj, I915_MAP_WB);
  1219. if (IS_ERR(val)) {
  1220. err = PTR_ERR(val);
  1221. goto err_scratch;
  1222. }
  1223. *ctx_id = *val;
  1224. i915_gem_object_unpin_map(scratch->obj);
  1225. err_scratch:
  1226. i915_vma_unpin_and_release(&scratch, 0);
  1227. return err;
  1228. }
  1229. /*
  1230. * For execlist mode of submission, pick an unused context id
  1231. * 0 - (NUM_CONTEXT_TAG -1) are used by other contexts
  1232. * XXX_MAX_CONTEXT_HW_ID is used by idle context
  1233. *
  1234. * For GuC mode of submission read context id from the upper dword of the
  1235. * EXECLIST_STATUS register. Note that we read this value only once and expect
  1236. * that the value stays fixed for the entire OA use case. There are cases where
  1237. * GuC KMD implementation may deregister a context to reuse it's context id, but
  1238. * we prevent that from happening to the OA context by pinning it.
  1239. */
  1240. static int gen12_get_render_context_id(struct i915_perf_stream *stream)
  1241. {
  1242. u32 ctx_id, mask;
  1243. int ret;
  1244. if (intel_engine_uses_guc(stream->engine)) {
  1245. ret = gen12_guc_sw_ctx_id(stream->pinned_ctx, &ctx_id);
  1246. if (ret)
  1247. return ret;
  1248. mask = ((1U << GEN12_GUC_SW_CTX_ID_WIDTH) - 1) <<
  1249. (GEN12_GUC_SW_CTX_ID_SHIFT - 32);
  1250. } else if (GRAPHICS_VER_FULL(stream->engine->i915) >= IP_VER(12, 55)) {
  1251. ctx_id = (XEHP_MAX_CONTEXT_HW_ID - 1) <<
  1252. (XEHP_SW_CTX_ID_SHIFT - 32);
  1253. mask = ((1U << XEHP_SW_CTX_ID_WIDTH) - 1) <<
  1254. (XEHP_SW_CTX_ID_SHIFT - 32);
  1255. } else {
  1256. ctx_id = (GEN12_MAX_CONTEXT_HW_ID - 1) <<
  1257. (GEN11_SW_CTX_ID_SHIFT - 32);
  1258. mask = ((1U << GEN11_SW_CTX_ID_WIDTH) - 1) <<
  1259. (GEN11_SW_CTX_ID_SHIFT - 32);
  1260. }
  1261. stream->specific_ctx_id = ctx_id & mask;
  1262. stream->specific_ctx_id_mask = mask;
  1263. return 0;
  1264. }
  1265. static bool oa_find_reg_in_lri(u32 *state, u32 reg, u32 *offset, u32 end)
  1266. {
  1267. u32 idx = *offset;
  1268. u32 len = min(MI_LRI_LEN(state[idx]) + idx, end);
  1269. bool found = false;
  1270. idx++;
  1271. for (; idx < len; idx += 2) {
  1272. if (state[idx] == reg) {
  1273. found = true;
  1274. break;
  1275. }
  1276. }
  1277. *offset = idx;
  1278. return found;
  1279. }
  1280. static u32 oa_context_image_offset(struct intel_context *ce, u32 reg)
  1281. {
  1282. u32 offset, len = (ce->engine->context_size - PAGE_SIZE) / 4;
  1283. u32 *state = ce->lrc_reg_state;
  1284. if (drm_WARN_ON(&ce->engine->i915->drm, !state))
  1285. return U32_MAX;
  1286. for (offset = 0; offset < len; ) {
  1287. if (IS_MI_LRI_CMD(state[offset])) {
  1288. /*
  1289. * We expect reg-value pairs in MI_LRI command, so
  1290. * MI_LRI_LEN() should be even, if not, issue a warning.
  1291. */
  1292. drm_WARN_ON(&ce->engine->i915->drm,
  1293. MI_LRI_LEN(state[offset]) & 0x1);
  1294. if (oa_find_reg_in_lri(state, reg, &offset, len))
  1295. break;
  1296. } else {
  1297. offset++;
  1298. }
  1299. }
  1300. return offset < len ? offset : U32_MAX;
  1301. }
  1302. static int set_oa_ctx_ctrl_offset(struct intel_context *ce)
  1303. {
  1304. i915_reg_t reg = GEN12_OACTXCONTROL(ce->engine->mmio_base);
  1305. struct i915_perf *perf = &ce->engine->i915->perf;
  1306. u32 offset = perf->ctx_oactxctrl_offset;
  1307. /* Do this only once. Failure is stored as offset of U32_MAX */
  1308. if (offset)
  1309. goto exit;
  1310. offset = oa_context_image_offset(ce, i915_mmio_reg_offset(reg));
  1311. perf->ctx_oactxctrl_offset = offset;
  1312. drm_dbg(&ce->engine->i915->drm,
  1313. "%s oa ctx control at 0x%08x dword offset\n",
  1314. ce->engine->name, offset);
  1315. exit:
  1316. return offset && offset != U32_MAX ? 0 : -ENODEV;
  1317. }
  1318. static bool engine_supports_mi_query(struct intel_engine_cs *engine)
  1319. {
  1320. return engine->class == RENDER_CLASS;
  1321. }
  1322. /**
  1323. * oa_get_render_ctx_id - determine and hold ctx hw id
  1324. * @stream: An i915-perf stream opened for OA metrics
  1325. *
  1326. * Determine the render context hw id, and ensure it remains fixed for the
  1327. * lifetime of the stream. This ensures that we don't have to worry about
  1328. * updating the context ID in OACONTROL on the fly.
  1329. *
  1330. * Returns: zero on success or a negative error code
  1331. */
  1332. static int oa_get_render_ctx_id(struct i915_perf_stream *stream)
  1333. {
  1334. struct intel_context *ce;
  1335. int ret = 0;
  1336. ce = oa_pin_context(stream);
  1337. if (IS_ERR(ce))
  1338. return PTR_ERR(ce);
  1339. if (engine_supports_mi_query(stream->engine) &&
  1340. HAS_LOGICAL_RING_CONTEXTS(stream->perf->i915)) {
  1341. /*
  1342. * We are enabling perf query here. If we don't find the context
  1343. * offset here, just return an error.
  1344. */
  1345. ret = set_oa_ctx_ctrl_offset(ce);
  1346. if (ret) {
  1347. intel_context_unpin(ce);
  1348. drm_err(&stream->perf->i915->drm,
  1349. "Enabling perf query failed for %s\n",
  1350. stream->engine->name);
  1351. return ret;
  1352. }
  1353. }
  1354. switch (GRAPHICS_VER(ce->engine->i915)) {
  1355. case 7: {
  1356. /*
  1357. * On Haswell we don't do any post processing of the reports
  1358. * and don't need to use the mask.
  1359. */
  1360. stream->specific_ctx_id = i915_ggtt_offset(ce->state);
  1361. stream->specific_ctx_id_mask = 0;
  1362. break;
  1363. }
  1364. case 8:
  1365. case 9:
  1366. if (intel_engine_uses_guc(ce->engine)) {
  1367. /*
  1368. * When using GuC, the context descriptor we write in
  1369. * i915 is read by GuC and rewritten before it's
  1370. * actually written into the hardware. The LRCA is
  1371. * what is put into the context id field of the
  1372. * context descriptor by GuC. Because it's aligned to
  1373. * a page, the lower 12bits are always at 0 and
  1374. * dropped by GuC. They won't be part of the context
  1375. * ID in the OA reports, so squash those lower bits.
  1376. */
  1377. stream->specific_ctx_id = ce->lrc.lrca >> 12;
  1378. /*
  1379. * GuC uses the top bit to signal proxy submission, so
  1380. * ignore that bit.
  1381. */
  1382. stream->specific_ctx_id_mask =
  1383. (1U << (GEN8_CTX_ID_WIDTH - 1)) - 1;
  1384. } else {
  1385. stream->specific_ctx_id_mask =
  1386. (1U << GEN8_CTX_ID_WIDTH) - 1;
  1387. stream->specific_ctx_id = stream->specific_ctx_id_mask;
  1388. }
  1389. break;
  1390. case 11:
  1391. case 12:
  1392. ret = gen12_get_render_context_id(stream);
  1393. break;
  1394. default:
  1395. MISSING_CASE(GRAPHICS_VER(ce->engine->i915));
  1396. }
  1397. ce->tag = stream->specific_ctx_id;
  1398. drm_dbg(&stream->perf->i915->drm,
  1399. "filtering on ctx_id=0x%x ctx_id_mask=0x%x\n",
  1400. stream->specific_ctx_id,
  1401. stream->specific_ctx_id_mask);
  1402. return ret;
  1403. }
  1404. /**
  1405. * oa_put_render_ctx_id - counterpart to oa_get_render_ctx_id releases hold
  1406. * @stream: An i915-perf stream opened for OA metrics
  1407. *
  1408. * In case anything needed doing to ensure the context HW ID would remain valid
  1409. * for the lifetime of the stream, then that can be undone here.
  1410. */
  1411. static void oa_put_render_ctx_id(struct i915_perf_stream *stream)
  1412. {
  1413. struct intel_context *ce;
  1414. ce = fetch_and_zero(&stream->pinned_ctx);
  1415. if (ce) {
  1416. ce->tag = 0; /* recomputed on next submission after parking */
  1417. intel_context_unpin(ce);
  1418. }
  1419. stream->specific_ctx_id = INVALID_CTX_ID;
  1420. stream->specific_ctx_id_mask = 0;
  1421. }
  1422. static void
  1423. free_oa_buffer(struct i915_perf_stream *stream)
  1424. {
  1425. i915_vma_unpin_and_release(&stream->oa_buffer.vma,
  1426. I915_VMA_RELEASE_MAP);
  1427. stream->oa_buffer.vaddr = NULL;
  1428. }
  1429. static void
  1430. free_oa_configs(struct i915_perf_stream *stream)
  1431. {
  1432. struct i915_oa_config_bo *oa_bo, *tmp;
  1433. i915_oa_config_put(stream->oa_config);
  1434. llist_for_each_entry_safe(oa_bo, tmp, stream->oa_config_bos.first, node)
  1435. free_oa_config_bo(oa_bo);
  1436. }
  1437. static void
  1438. free_noa_wait(struct i915_perf_stream *stream)
  1439. {
  1440. i915_vma_unpin_and_release(&stream->noa_wait, 0);
  1441. }
  1442. static bool engine_supports_oa(const struct intel_engine_cs *engine)
  1443. {
  1444. return engine->oa_group;
  1445. }
  1446. static bool engine_supports_oa_format(struct intel_engine_cs *engine, int type)
  1447. {
  1448. return engine->oa_group && engine->oa_group->type == type;
  1449. }
  1450. static void i915_oa_stream_destroy(struct i915_perf_stream *stream)
  1451. {
  1452. struct i915_perf *perf = stream->perf;
  1453. struct intel_gt *gt = stream->engine->gt;
  1454. struct i915_perf_group *g = stream->engine->oa_group;
  1455. int m;
  1456. if (WARN_ON(stream != g->exclusive_stream))
  1457. return;
  1458. /*
  1459. * Unset exclusive_stream first, it will be checked while disabling
  1460. * the metric set on gen8+.
  1461. *
  1462. * See i915_oa_init_reg_state() and lrc_configure_all_contexts()
  1463. */
  1464. WRITE_ONCE(g->exclusive_stream, NULL);
  1465. perf->ops.disable_metric_set(stream);
  1466. free_oa_buffer(stream);
  1467. intel_uncore_forcewake_put(stream->uncore, FORCEWAKE_ALL);
  1468. intel_engine_pm_put(stream->engine);
  1469. if (stream->ctx)
  1470. oa_put_render_ctx_id(stream);
  1471. free_oa_configs(stream);
  1472. free_noa_wait(stream);
  1473. m = ratelimit_state_get_miss(&perf->spurious_report_rs);
  1474. if (m)
  1475. gt_notice(gt, "%d spurious OA report notices suppressed due to ratelimiting\n", m);
  1476. }
  1477. static void gen7_init_oa_buffer(struct i915_perf_stream *stream)
  1478. {
  1479. struct intel_uncore *uncore = stream->uncore;
  1480. u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
  1481. unsigned long flags;
  1482. spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
  1483. /* Pre-DevBDW: OABUFFER must be set with counters off,
  1484. * before OASTATUS1, but after OASTATUS2
  1485. */
  1486. intel_uncore_write(uncore, GEN7_OASTATUS2, /* head */
  1487. gtt_offset | GEN7_OASTATUS2_MEM_SELECT_GGTT);
  1488. stream->oa_buffer.head = 0;
  1489. intel_uncore_write(uncore, GEN7_OABUFFER, gtt_offset);
  1490. intel_uncore_write(uncore, GEN7_OASTATUS1, /* tail */
  1491. gtt_offset | OABUFFER_SIZE_16M);
  1492. /* Mark that we need updated tail pointers to read from... */
  1493. stream->oa_buffer.tail = 0;
  1494. spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
  1495. /* On Haswell we have to track which OASTATUS1 flags we've
  1496. * already seen since they can't be cleared while periodic
  1497. * sampling is enabled.
  1498. */
  1499. stream->perf->gen7_latched_oastatus1 = 0;
  1500. /* NB: although the OA buffer will initially be allocated
  1501. * zeroed via shmfs (and so this memset is redundant when
  1502. * first allocating), we may re-init the OA buffer, either
  1503. * when re-enabling a stream or in error/reset paths.
  1504. *
  1505. * The reason we clear the buffer for each re-init is for the
  1506. * sanity check in gen7_append_oa_reports() that looks at the
  1507. * report-id field to make sure it's non-zero which relies on
  1508. * the assumption that new reports are being written to zeroed
  1509. * memory...
  1510. */
  1511. memset(stream->oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
  1512. }
  1513. static void gen8_init_oa_buffer(struct i915_perf_stream *stream)
  1514. {
  1515. struct intel_uncore *uncore = stream->uncore;
  1516. u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
  1517. unsigned long flags;
  1518. spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
  1519. intel_uncore_write(uncore, GEN8_OASTATUS, 0);
  1520. intel_uncore_write(uncore, GEN8_OAHEADPTR, gtt_offset);
  1521. stream->oa_buffer.head = 0;
  1522. intel_uncore_write(uncore, GEN8_OABUFFER_UDW, 0);
  1523. /*
  1524. * PRM says:
  1525. *
  1526. * "This MMIO must be set before the OATAILPTR
  1527. * register and after the OAHEADPTR register. This is
  1528. * to enable proper functionality of the overflow
  1529. * bit."
  1530. */
  1531. intel_uncore_write(uncore, GEN8_OABUFFER, gtt_offset |
  1532. OABUFFER_SIZE_16M | GEN8_OABUFFER_MEM_SELECT_GGTT);
  1533. intel_uncore_write(uncore, GEN8_OATAILPTR, gtt_offset & GEN8_OATAILPTR_MASK);
  1534. /* Mark that we need updated tail pointers to read from... */
  1535. stream->oa_buffer.tail = 0;
  1536. /*
  1537. * Reset state used to recognise context switches, affecting which
  1538. * reports we will forward to userspace while filtering for a single
  1539. * context.
  1540. */
  1541. stream->oa_buffer.last_ctx_id = INVALID_CTX_ID;
  1542. spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
  1543. /*
  1544. * NB: although the OA buffer will initially be allocated
  1545. * zeroed via shmfs (and so this memset is redundant when
  1546. * first allocating), we may re-init the OA buffer, either
  1547. * when re-enabling a stream or in error/reset paths.
  1548. *
  1549. * The reason we clear the buffer for each re-init is for the
  1550. * sanity check in gen8_append_oa_reports() that looks at the
  1551. * reason field to make sure it's non-zero which relies on
  1552. * the assumption that new reports are being written to zeroed
  1553. * memory...
  1554. */
  1555. memset(stream->oa_buffer.vaddr, 0, OA_BUFFER_SIZE);
  1556. }
  1557. static void gen12_init_oa_buffer(struct i915_perf_stream *stream)
  1558. {
  1559. struct intel_uncore *uncore = stream->uncore;
  1560. u32 gtt_offset = i915_ggtt_offset(stream->oa_buffer.vma);
  1561. unsigned long flags;
  1562. spin_lock_irqsave(&stream->oa_buffer.ptr_lock, flags);
  1563. intel_uncore_write(uncore, __oa_regs(stream)->oa_status, 0);
  1564. intel_uncore_write(uncore, __oa_regs(stream)->oa_head_ptr,
  1565. gtt_offset & GEN12_OAG_OAHEADPTR_MASK);
  1566. stream->oa_buffer.head = 0;
  1567. /*
  1568. * PRM says:
  1569. *
  1570. * "This MMIO must be set before the OATAILPTR
  1571. * register and after the OAHEADPTR register. This is
  1572. * to enable proper functionality of the overflow
  1573. * bit."
  1574. */
  1575. intel_uncore_write(uncore, __oa_regs(stream)->oa_buffer, gtt_offset |
  1576. OABUFFER_SIZE_16M | GEN8_OABUFFER_MEM_SELECT_GGTT);
  1577. intel_uncore_write(uncore, __oa_regs(stream)->oa_tail_ptr,
  1578. gtt_offset & GEN12_OAG_OATAILPTR_MASK);
  1579. /* Mark that we need updated tail pointers to read from... */
  1580. stream->oa_buffer.tail = 0;
  1581. /*
  1582. * Reset state used to recognise context switches, affecting which
  1583. * reports we will forward to userspace while filtering for a single
  1584. * context.
  1585. */
  1586. stream->oa_buffer.last_ctx_id = INVALID_CTX_ID;
  1587. spin_unlock_irqrestore(&stream->oa_buffer.ptr_lock, flags);
  1588. /*
  1589. * NB: although the OA buffer will initially be allocated
  1590. * zeroed via shmfs (and so this memset is redundant when
  1591. * first allocating), we may re-init the OA buffer, either
  1592. * when re-enabling a stream or in error/reset paths.
  1593. *
  1594. * The reason we clear the buffer for each re-init is for the
  1595. * sanity check in gen8_append_oa_reports() that looks at the
  1596. * reason field to make sure it's non-zero which relies on
  1597. * the assumption that new reports are being written to zeroed
  1598. * memory...
  1599. */
  1600. memset(stream->oa_buffer.vaddr, 0,
  1601. stream->oa_buffer.vma->size);
  1602. }
  1603. static int alloc_oa_buffer(struct i915_perf_stream *stream)
  1604. {
  1605. struct drm_i915_private *i915 = stream->perf->i915;
  1606. struct intel_gt *gt = stream->engine->gt;
  1607. struct drm_i915_gem_object *bo;
  1608. struct i915_vma *vma;
  1609. int ret;
  1610. if (drm_WARN_ON(&i915->drm, stream->oa_buffer.vma))
  1611. return -ENODEV;
  1612. BUILD_BUG_ON_NOT_POWER_OF_2(OA_BUFFER_SIZE);
  1613. BUILD_BUG_ON(OA_BUFFER_SIZE < SZ_128K || OA_BUFFER_SIZE > SZ_16M);
  1614. bo = i915_gem_object_create_shmem(stream->perf->i915, OA_BUFFER_SIZE);
  1615. if (IS_ERR(bo)) {
  1616. drm_err(&i915->drm, "Failed to allocate OA buffer\n");
  1617. return PTR_ERR(bo);
  1618. }
  1619. i915_gem_object_set_cache_coherency(bo, I915_CACHE_LLC);
  1620. /* PreHSW required 512K alignment, HSW requires 16M */
  1621. vma = i915_vma_instance(bo, &gt->ggtt->vm, NULL);
  1622. if (IS_ERR(vma)) {
  1623. ret = PTR_ERR(vma);
  1624. goto err_unref;
  1625. }
  1626. /*
  1627. * PreHSW required 512K alignment.
  1628. * HSW and onwards, align to requested size of OA buffer.
  1629. */
  1630. ret = i915_vma_pin(vma, 0, SZ_16M, PIN_GLOBAL | PIN_HIGH);
  1631. if (ret) {
  1632. gt_err(gt, "Failed to pin OA buffer %d\n", ret);
  1633. goto err_unref;
  1634. }
  1635. stream->oa_buffer.vma = vma;
  1636. stream->oa_buffer.vaddr =
  1637. i915_gem_object_pin_map_unlocked(bo, I915_MAP_WB);
  1638. if (IS_ERR(stream->oa_buffer.vaddr)) {
  1639. ret = PTR_ERR(stream->oa_buffer.vaddr);
  1640. goto err_unpin;
  1641. }
  1642. return 0;
  1643. err_unpin:
  1644. __i915_vma_unpin(vma);
  1645. err_unref:
  1646. i915_gem_object_put(bo);
  1647. stream->oa_buffer.vaddr = NULL;
  1648. stream->oa_buffer.vma = NULL;
  1649. return ret;
  1650. }
  1651. static u32 *save_restore_register(struct i915_perf_stream *stream, u32 *cs,
  1652. bool save, i915_reg_t reg, u32 offset,
  1653. u32 dword_count)
  1654. {
  1655. u32 cmd;
  1656. u32 d;
  1657. cmd = save ? MI_STORE_REGISTER_MEM : MI_LOAD_REGISTER_MEM;
  1658. cmd |= MI_SRM_LRM_GLOBAL_GTT;
  1659. if (GRAPHICS_VER(stream->perf->i915) >= 8)
  1660. cmd++;
  1661. for (d = 0; d < dword_count; d++) {
  1662. *cs++ = cmd;
  1663. *cs++ = i915_mmio_reg_offset(reg) + 4 * d;
  1664. *cs++ = i915_ggtt_offset(stream->noa_wait) + offset + 4 * d;
  1665. *cs++ = 0;
  1666. }
  1667. return cs;
  1668. }
  1669. static int alloc_noa_wait(struct i915_perf_stream *stream)
  1670. {
  1671. struct drm_i915_private *i915 = stream->perf->i915;
  1672. struct intel_gt *gt = stream->engine->gt;
  1673. struct drm_i915_gem_object *bo;
  1674. struct i915_vma *vma;
  1675. const u64 delay_ticks = 0xffffffffffffffff -
  1676. intel_gt_ns_to_clock_interval(to_gt(stream->perf->i915),
  1677. atomic64_read(&stream->perf->noa_programming_delay));
  1678. const u32 base = stream->engine->mmio_base;
  1679. #define CS_GPR(x) GEN8_RING_CS_GPR(base, x)
  1680. u32 *batch, *ts0, *cs, *jump;
  1681. struct i915_gem_ww_ctx ww;
  1682. int ret, i;
  1683. enum {
  1684. START_TS,
  1685. NOW_TS,
  1686. DELTA_TS,
  1687. JUMP_PREDICATE,
  1688. DELTA_TARGET,
  1689. N_CS_GPR
  1690. };
  1691. i915_reg_t mi_predicate_result = HAS_MI_SET_PREDICATE(i915) ?
  1692. MI_PREDICATE_RESULT_2_ENGINE(base) :
  1693. MI_PREDICATE_RESULT_1(RENDER_RING_BASE);
  1694. /*
  1695. * gt->scratch was being used to save/restore the GPR registers, but on
  1696. * MTL the scratch uses stolen lmem. An MI_SRM to this memory region
  1697. * causes an engine hang. Instead allocate an additional page here to
  1698. * save/restore GPR registers
  1699. */
  1700. bo = i915_gem_object_create_internal(i915, 8192);
  1701. if (IS_ERR(bo)) {
  1702. drm_err(&i915->drm,
  1703. "Failed to allocate NOA wait batchbuffer\n");
  1704. return PTR_ERR(bo);
  1705. }
  1706. i915_gem_ww_ctx_init(&ww, true);
  1707. retry:
  1708. ret = i915_gem_object_lock(bo, &ww);
  1709. if (ret)
  1710. goto out_ww;
  1711. /*
  1712. * We pin in GGTT because we jump into this buffer now because
  1713. * multiple OA config BOs will have a jump to this address and it
  1714. * needs to be fixed during the lifetime of the i915/perf stream.
  1715. */
  1716. vma = i915_vma_instance(bo, &gt->ggtt->vm, NULL);
  1717. if (IS_ERR(vma)) {
  1718. ret = PTR_ERR(vma);
  1719. goto out_ww;
  1720. }
  1721. ret = i915_vma_pin_ww(vma, &ww, 0, 0, PIN_GLOBAL | PIN_HIGH);
  1722. if (ret)
  1723. goto out_ww;
  1724. batch = cs = i915_gem_object_pin_map(bo, I915_MAP_WB);
  1725. if (IS_ERR(batch)) {
  1726. ret = PTR_ERR(batch);
  1727. goto err_unpin;
  1728. }
  1729. stream->noa_wait = vma;
  1730. #define GPR_SAVE_OFFSET 4096
  1731. #define PREDICATE_SAVE_OFFSET 4160
  1732. /* Save registers. */
  1733. for (i = 0; i < N_CS_GPR; i++)
  1734. cs = save_restore_register(
  1735. stream, cs, true /* save */, CS_GPR(i),
  1736. GPR_SAVE_OFFSET + 8 * i, 2);
  1737. cs = save_restore_register(
  1738. stream, cs, true /* save */, mi_predicate_result,
  1739. PREDICATE_SAVE_OFFSET, 1);
  1740. /* First timestamp snapshot location. */
  1741. ts0 = cs;
  1742. /*
  1743. * Initial snapshot of the timestamp register to implement the wait.
  1744. * We work with 32b values, so clear out the top 32b bits of the
  1745. * register because the ALU works 64bits.
  1746. */
  1747. *cs++ = MI_LOAD_REGISTER_IMM(1);
  1748. *cs++ = i915_mmio_reg_offset(CS_GPR(START_TS)) + 4;
  1749. *cs++ = 0;
  1750. *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
  1751. *cs++ = i915_mmio_reg_offset(RING_TIMESTAMP(base));
  1752. *cs++ = i915_mmio_reg_offset(CS_GPR(START_TS));
  1753. /*
  1754. * This is the location we're going to jump back into until the
  1755. * required amount of time has passed.
  1756. */
  1757. jump = cs;
  1758. /*
  1759. * Take another snapshot of the timestamp register. Take care to clear
  1760. * up the top 32bits of CS_GPR(1) as we're using it for other
  1761. * operations below.
  1762. */
  1763. *cs++ = MI_LOAD_REGISTER_IMM(1);
  1764. *cs++ = i915_mmio_reg_offset(CS_GPR(NOW_TS)) + 4;
  1765. *cs++ = 0;
  1766. *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
  1767. *cs++ = i915_mmio_reg_offset(RING_TIMESTAMP(base));
  1768. *cs++ = i915_mmio_reg_offset(CS_GPR(NOW_TS));
  1769. /*
  1770. * Do a diff between the 2 timestamps and store the result back into
  1771. * CS_GPR(1).
  1772. */
  1773. *cs++ = MI_MATH(5);
  1774. *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCA, MI_MATH_REG(NOW_TS));
  1775. *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCB, MI_MATH_REG(START_TS));
  1776. *cs++ = MI_MATH_SUB;
  1777. *cs++ = MI_MATH_STORE(MI_MATH_REG(DELTA_TS), MI_MATH_REG_ACCU);
  1778. *cs++ = MI_MATH_STORE(MI_MATH_REG(JUMP_PREDICATE), MI_MATH_REG_CF);
  1779. /*
  1780. * Transfer the carry flag (set to 1 if ts1 < ts0, meaning the
  1781. * timestamp have rolled over the 32bits) into the predicate register
  1782. * to be used for the predicated jump.
  1783. */
  1784. *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
  1785. *cs++ = i915_mmio_reg_offset(CS_GPR(JUMP_PREDICATE));
  1786. *cs++ = i915_mmio_reg_offset(mi_predicate_result);
  1787. if (HAS_MI_SET_PREDICATE(i915))
  1788. *cs++ = MI_SET_PREDICATE | 1;
  1789. /* Restart from the beginning if we had timestamps roll over. */
  1790. *cs++ = (GRAPHICS_VER(i915) < 8 ?
  1791. MI_BATCH_BUFFER_START :
  1792. MI_BATCH_BUFFER_START_GEN8) |
  1793. MI_BATCH_PREDICATE;
  1794. *cs++ = i915_ggtt_offset(vma) + (ts0 - batch) * 4;
  1795. *cs++ = 0;
  1796. if (HAS_MI_SET_PREDICATE(i915))
  1797. *cs++ = MI_SET_PREDICATE;
  1798. /*
  1799. * Now add the diff between to previous timestamps and add it to :
  1800. * (((1 * << 64) - 1) - delay_ns)
  1801. *
  1802. * When the Carry Flag contains 1 this means the elapsed time is
  1803. * longer than the expected delay, and we can exit the wait loop.
  1804. */
  1805. *cs++ = MI_LOAD_REGISTER_IMM(2);
  1806. *cs++ = i915_mmio_reg_offset(CS_GPR(DELTA_TARGET));
  1807. *cs++ = lower_32_bits(delay_ticks);
  1808. *cs++ = i915_mmio_reg_offset(CS_GPR(DELTA_TARGET)) + 4;
  1809. *cs++ = upper_32_bits(delay_ticks);
  1810. *cs++ = MI_MATH(4);
  1811. *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCA, MI_MATH_REG(DELTA_TS));
  1812. *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCB, MI_MATH_REG(DELTA_TARGET));
  1813. *cs++ = MI_MATH_ADD;
  1814. *cs++ = MI_MATH_STOREINV(MI_MATH_REG(JUMP_PREDICATE), MI_MATH_REG_CF);
  1815. *cs++ = MI_ARB_CHECK;
  1816. /*
  1817. * Transfer the result into the predicate register to be used for the
  1818. * predicated jump.
  1819. */
  1820. *cs++ = MI_LOAD_REGISTER_REG | (3 - 2);
  1821. *cs++ = i915_mmio_reg_offset(CS_GPR(JUMP_PREDICATE));
  1822. *cs++ = i915_mmio_reg_offset(mi_predicate_result);
  1823. if (HAS_MI_SET_PREDICATE(i915))
  1824. *cs++ = MI_SET_PREDICATE | 1;
  1825. /* Predicate the jump. */
  1826. *cs++ = (GRAPHICS_VER(i915) < 8 ?
  1827. MI_BATCH_BUFFER_START :
  1828. MI_BATCH_BUFFER_START_GEN8) |
  1829. MI_BATCH_PREDICATE;
  1830. *cs++ = i915_ggtt_offset(vma) + (jump - batch) * 4;
  1831. *cs++ = 0;
  1832. if (HAS_MI_SET_PREDICATE(i915))
  1833. *cs++ = MI_SET_PREDICATE;
  1834. /* Restore registers. */
  1835. for (i = 0; i < N_CS_GPR; i++)
  1836. cs = save_restore_register(
  1837. stream, cs, false /* restore */, CS_GPR(i),
  1838. GPR_SAVE_OFFSET + 8 * i, 2);
  1839. cs = save_restore_register(
  1840. stream, cs, false /* restore */, mi_predicate_result,
  1841. PREDICATE_SAVE_OFFSET, 1);
  1842. /* And return to the ring. */
  1843. *cs++ = MI_BATCH_BUFFER_END;
  1844. GEM_BUG_ON(cs - batch > PAGE_SIZE / sizeof(*batch));
  1845. i915_gem_object_flush_map(bo);
  1846. __i915_gem_object_release_map(bo);
  1847. goto out_ww;
  1848. err_unpin:
  1849. i915_vma_unpin_and_release(&vma, 0);
  1850. out_ww:
  1851. if (ret == -EDEADLK) {
  1852. ret = i915_gem_ww_ctx_backoff(&ww);
  1853. if (!ret)
  1854. goto retry;
  1855. }
  1856. i915_gem_ww_ctx_fini(&ww);
  1857. if (ret)
  1858. i915_gem_object_put(bo);
  1859. return ret;
  1860. }
  1861. static u32 *write_cs_mi_lri(u32 *cs,
  1862. const struct i915_oa_reg *reg_data,
  1863. u32 n_regs)
  1864. {
  1865. u32 i;
  1866. for (i = 0; i < n_regs; i++) {
  1867. if ((i % MI_LOAD_REGISTER_IMM_MAX_REGS) == 0) {
  1868. u32 n_lri = min_t(u32,
  1869. n_regs - i,
  1870. MI_LOAD_REGISTER_IMM_MAX_REGS);
  1871. *cs++ = MI_LOAD_REGISTER_IMM(n_lri);
  1872. }
  1873. *cs++ = i915_mmio_reg_offset(reg_data[i].addr);
  1874. *cs++ = reg_data[i].value;
  1875. }
  1876. return cs;
  1877. }
  1878. static int num_lri_dwords(int num_regs)
  1879. {
  1880. int count = 0;
  1881. if (num_regs > 0) {
  1882. count += DIV_ROUND_UP(num_regs, MI_LOAD_REGISTER_IMM_MAX_REGS);
  1883. count += num_regs * 2;
  1884. }
  1885. return count;
  1886. }
  1887. static struct i915_oa_config_bo *
  1888. alloc_oa_config_buffer(struct i915_perf_stream *stream,
  1889. struct i915_oa_config *oa_config)
  1890. {
  1891. struct drm_i915_gem_object *obj;
  1892. struct i915_oa_config_bo *oa_bo;
  1893. struct i915_gem_ww_ctx ww;
  1894. size_t config_length = 0;
  1895. u32 *cs;
  1896. int err;
  1897. oa_bo = kzalloc_obj(*oa_bo);
  1898. if (!oa_bo)
  1899. return ERR_PTR(-ENOMEM);
  1900. config_length += num_lri_dwords(oa_config->mux_regs_len);
  1901. config_length += num_lri_dwords(oa_config->b_counter_regs_len);
  1902. config_length += num_lri_dwords(oa_config->flex_regs_len);
  1903. config_length += 3; /* MI_BATCH_BUFFER_START */
  1904. config_length = ALIGN(sizeof(u32) * config_length, I915_GTT_PAGE_SIZE);
  1905. obj = i915_gem_object_create_shmem(stream->perf->i915, config_length);
  1906. if (IS_ERR(obj)) {
  1907. err = PTR_ERR(obj);
  1908. goto err_free;
  1909. }
  1910. i915_gem_ww_ctx_init(&ww, true);
  1911. retry:
  1912. err = i915_gem_object_lock(obj, &ww);
  1913. if (err)
  1914. goto out_ww;
  1915. cs = i915_gem_object_pin_map(obj, I915_MAP_WB);
  1916. if (IS_ERR(cs)) {
  1917. err = PTR_ERR(cs);
  1918. goto out_ww;
  1919. }
  1920. cs = write_cs_mi_lri(cs,
  1921. oa_config->mux_regs,
  1922. oa_config->mux_regs_len);
  1923. cs = write_cs_mi_lri(cs,
  1924. oa_config->b_counter_regs,
  1925. oa_config->b_counter_regs_len);
  1926. cs = write_cs_mi_lri(cs,
  1927. oa_config->flex_regs,
  1928. oa_config->flex_regs_len);
  1929. /* Jump into the active wait. */
  1930. *cs++ = (GRAPHICS_VER(stream->perf->i915) < 8 ?
  1931. MI_BATCH_BUFFER_START :
  1932. MI_BATCH_BUFFER_START_GEN8);
  1933. *cs++ = i915_ggtt_offset(stream->noa_wait);
  1934. *cs++ = 0;
  1935. i915_gem_object_flush_map(obj);
  1936. __i915_gem_object_release_map(obj);
  1937. oa_bo->vma = i915_vma_instance(obj,
  1938. &stream->engine->gt->ggtt->vm,
  1939. NULL);
  1940. if (IS_ERR(oa_bo->vma)) {
  1941. err = PTR_ERR(oa_bo->vma);
  1942. goto out_ww;
  1943. }
  1944. oa_bo->oa_config = i915_oa_config_get(oa_config);
  1945. llist_add(&oa_bo->node, &stream->oa_config_bos);
  1946. out_ww:
  1947. if (err == -EDEADLK) {
  1948. err = i915_gem_ww_ctx_backoff(&ww);
  1949. if (!err)
  1950. goto retry;
  1951. }
  1952. i915_gem_ww_ctx_fini(&ww);
  1953. if (err)
  1954. i915_gem_object_put(obj);
  1955. err_free:
  1956. if (err) {
  1957. kfree(oa_bo);
  1958. return ERR_PTR(err);
  1959. }
  1960. return oa_bo;
  1961. }
  1962. static struct i915_vma *
  1963. get_oa_vma(struct i915_perf_stream *stream, struct i915_oa_config *oa_config)
  1964. {
  1965. struct i915_oa_config_bo *oa_bo;
  1966. /*
  1967. * Look for the buffer in the already allocated BOs attached
  1968. * to the stream.
  1969. */
  1970. llist_for_each_entry(oa_bo, stream->oa_config_bos.first, node) {
  1971. if (oa_bo->oa_config == oa_config &&
  1972. memcmp(oa_bo->oa_config->uuid,
  1973. oa_config->uuid,
  1974. sizeof(oa_config->uuid)) == 0)
  1975. goto out;
  1976. }
  1977. oa_bo = alloc_oa_config_buffer(stream, oa_config);
  1978. if (IS_ERR(oa_bo))
  1979. return ERR_CAST(oa_bo);
  1980. out:
  1981. return i915_vma_get(oa_bo->vma);
  1982. }
  1983. static int
  1984. emit_oa_config(struct i915_perf_stream *stream,
  1985. struct i915_oa_config *oa_config,
  1986. struct intel_context *ce,
  1987. struct i915_active *active)
  1988. {
  1989. struct i915_request *rq;
  1990. struct i915_vma *vma;
  1991. struct i915_gem_ww_ctx ww;
  1992. int err;
  1993. vma = get_oa_vma(stream, oa_config);
  1994. if (IS_ERR(vma))
  1995. return PTR_ERR(vma);
  1996. i915_gem_ww_ctx_init(&ww, true);
  1997. retry:
  1998. err = i915_gem_object_lock(vma->obj, &ww);
  1999. if (err)
  2000. goto err;
  2001. err = i915_vma_pin_ww(vma, &ww, 0, 0, PIN_GLOBAL | PIN_HIGH);
  2002. if (err)
  2003. goto err;
  2004. intel_engine_pm_get(ce->engine);
  2005. rq = i915_request_create(ce);
  2006. intel_engine_pm_put(ce->engine);
  2007. if (IS_ERR(rq)) {
  2008. err = PTR_ERR(rq);
  2009. goto err_vma_unpin;
  2010. }
  2011. if (!IS_ERR_OR_NULL(active)) {
  2012. /* After all individual context modifications */
  2013. err = i915_request_await_active(rq, active,
  2014. I915_ACTIVE_AWAIT_ACTIVE);
  2015. if (err)
  2016. goto err_add_request;
  2017. err = i915_active_add_request(active, rq);
  2018. if (err)
  2019. goto err_add_request;
  2020. }
  2021. err = i915_vma_move_to_active(vma, rq, 0);
  2022. if (err)
  2023. goto err_add_request;
  2024. err = rq->engine->emit_bb_start(rq,
  2025. i915_vma_offset(vma), 0,
  2026. I915_DISPATCH_SECURE);
  2027. if (err)
  2028. goto err_add_request;
  2029. err_add_request:
  2030. i915_request_add(rq);
  2031. err_vma_unpin:
  2032. i915_vma_unpin(vma);
  2033. err:
  2034. if (err == -EDEADLK) {
  2035. err = i915_gem_ww_ctx_backoff(&ww);
  2036. if (!err)
  2037. goto retry;
  2038. }
  2039. i915_gem_ww_ctx_fini(&ww);
  2040. i915_vma_put(vma);
  2041. return err;
  2042. }
  2043. static struct intel_context *oa_context(struct i915_perf_stream *stream)
  2044. {
  2045. return stream->pinned_ctx ?: stream->engine->kernel_context;
  2046. }
  2047. static int
  2048. hsw_enable_metric_set(struct i915_perf_stream *stream,
  2049. struct i915_active *active)
  2050. {
  2051. struct intel_uncore *uncore = stream->uncore;
  2052. /*
  2053. * PRM:
  2054. *
  2055. * OA unit is using “crclk” for its functionality. When trunk
  2056. * level clock gating takes place, OA clock would be gated,
  2057. * unable to count the events from non-render clock domain.
  2058. * Render clock gating must be disabled when OA is enabled to
  2059. * count the events from non-render domain. Unit level clock
  2060. * gating for RCS should also be disabled.
  2061. */
  2062. intel_uncore_rmw(uncore, GEN7_MISCCPCTL,
  2063. GEN7_DOP_CLOCK_GATE_ENABLE, 0);
  2064. intel_uncore_rmw(uncore, GEN6_UCGCTL1,
  2065. 0, GEN6_CSUNIT_CLOCK_GATE_DISABLE);
  2066. return emit_oa_config(stream,
  2067. stream->oa_config, oa_context(stream),
  2068. active);
  2069. }
  2070. static void hsw_disable_metric_set(struct i915_perf_stream *stream)
  2071. {
  2072. struct intel_uncore *uncore = stream->uncore;
  2073. intel_uncore_rmw(uncore, GEN6_UCGCTL1,
  2074. GEN6_CSUNIT_CLOCK_GATE_DISABLE, 0);
  2075. intel_uncore_rmw(uncore, GEN7_MISCCPCTL,
  2076. 0, GEN7_DOP_CLOCK_GATE_ENABLE);
  2077. intel_uncore_rmw(uncore, GDT_CHICKEN_BITS, GT_NOA_ENABLE, 0);
  2078. }
  2079. static u32 oa_config_flex_reg(const struct i915_oa_config *oa_config,
  2080. i915_reg_t reg)
  2081. {
  2082. u32 mmio = i915_mmio_reg_offset(reg);
  2083. int i;
  2084. /*
  2085. * This arbitrary default will select the 'EU FPU0 Pipeline
  2086. * Active' event. In the future it's anticipated that there
  2087. * will be an explicit 'No Event' we can select, but not yet...
  2088. */
  2089. if (!oa_config)
  2090. return 0;
  2091. for (i = 0; i < oa_config->flex_regs_len; i++) {
  2092. if (i915_mmio_reg_offset(oa_config->flex_regs[i].addr) == mmio)
  2093. return oa_config->flex_regs[i].value;
  2094. }
  2095. return 0;
  2096. }
  2097. /*
  2098. * NB: It must always remain pointer safe to run this even if the OA unit
  2099. * has been disabled.
  2100. *
  2101. * It's fine to put out-of-date values into these per-context registers
  2102. * in the case that the OA unit has been disabled.
  2103. */
  2104. static void
  2105. gen8_update_reg_state_unlocked(const struct intel_context *ce,
  2106. const struct i915_perf_stream *stream)
  2107. {
  2108. u32 ctx_oactxctrl = stream->perf->ctx_oactxctrl_offset;
  2109. u32 ctx_flexeu0 = stream->perf->ctx_flexeu0_offset;
  2110. /* The MMIO offsets for Flex EU registers aren't contiguous */
  2111. static const i915_reg_t flex_regs[] = {
  2112. EU_PERF_CNTL0,
  2113. EU_PERF_CNTL1,
  2114. EU_PERF_CNTL2,
  2115. EU_PERF_CNTL3,
  2116. EU_PERF_CNTL4,
  2117. EU_PERF_CNTL5,
  2118. EU_PERF_CNTL6,
  2119. };
  2120. u32 *reg_state = ce->lrc_reg_state;
  2121. int i;
  2122. reg_state[ctx_oactxctrl + 1] =
  2123. (stream->period_exponent << GEN8_OA_TIMER_PERIOD_SHIFT) |
  2124. (stream->periodic ? GEN8_OA_TIMER_ENABLE : 0) |
  2125. GEN8_OA_COUNTER_RESUME;
  2126. for (i = 0; i < ARRAY_SIZE(flex_regs); i++)
  2127. reg_state[ctx_flexeu0 + i * 2 + 1] =
  2128. oa_config_flex_reg(stream->oa_config, flex_regs[i]);
  2129. }
  2130. struct flex {
  2131. i915_reg_t reg;
  2132. u32 offset;
  2133. u32 value;
  2134. };
  2135. static int
  2136. gen8_store_flex(struct i915_request *rq,
  2137. struct intel_context *ce,
  2138. const struct flex *flex, unsigned int count)
  2139. {
  2140. u32 offset;
  2141. u32 *cs;
  2142. cs = intel_ring_begin(rq, 4 * count);
  2143. if (IS_ERR(cs))
  2144. return PTR_ERR(cs);
  2145. offset = i915_ggtt_offset(ce->state) + LRC_STATE_OFFSET;
  2146. do {
  2147. *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
  2148. *cs++ = offset + flex->offset * sizeof(u32);
  2149. *cs++ = 0;
  2150. *cs++ = flex->value;
  2151. } while (flex++, --count);
  2152. intel_ring_advance(rq, cs);
  2153. return 0;
  2154. }
  2155. static int
  2156. gen8_load_flex(struct i915_request *rq,
  2157. struct intel_context *ce,
  2158. const struct flex *flex, unsigned int count)
  2159. {
  2160. u32 *cs;
  2161. GEM_BUG_ON(!count || count > 63);
  2162. cs = intel_ring_begin(rq, 2 * count + 2);
  2163. if (IS_ERR(cs))
  2164. return PTR_ERR(cs);
  2165. *cs++ = MI_LOAD_REGISTER_IMM(count);
  2166. do {
  2167. *cs++ = i915_mmio_reg_offset(flex->reg);
  2168. *cs++ = flex->value;
  2169. } while (flex++, --count);
  2170. *cs++ = MI_NOOP;
  2171. intel_ring_advance(rq, cs);
  2172. return 0;
  2173. }
  2174. static int gen8_modify_context(struct intel_context *ce,
  2175. const struct flex *flex, unsigned int count)
  2176. {
  2177. struct i915_request *rq;
  2178. int err;
  2179. rq = intel_engine_create_kernel_request(ce->engine);
  2180. if (IS_ERR(rq))
  2181. return PTR_ERR(rq);
  2182. /* Serialise with the remote context */
  2183. err = intel_context_prepare_remote_request(ce, rq);
  2184. if (err == 0)
  2185. err = gen8_store_flex(rq, ce, flex, count);
  2186. i915_request_add(rq);
  2187. return err;
  2188. }
  2189. static int
  2190. gen8_modify_self(struct intel_context *ce,
  2191. const struct flex *flex, unsigned int count,
  2192. struct i915_active *active)
  2193. {
  2194. struct i915_request *rq;
  2195. int err;
  2196. intel_engine_pm_get(ce->engine);
  2197. rq = i915_request_create(ce);
  2198. intel_engine_pm_put(ce->engine);
  2199. if (IS_ERR(rq))
  2200. return PTR_ERR(rq);
  2201. if (!IS_ERR_OR_NULL(active)) {
  2202. err = i915_active_add_request(active, rq);
  2203. if (err)
  2204. goto err_add_request;
  2205. }
  2206. err = gen8_load_flex(rq, ce, flex, count);
  2207. if (err)
  2208. goto err_add_request;
  2209. err_add_request:
  2210. i915_request_add(rq);
  2211. return err;
  2212. }
  2213. static int gen8_configure_context(struct i915_perf_stream *stream,
  2214. struct i915_gem_context *ctx,
  2215. struct flex *flex, unsigned int count)
  2216. {
  2217. struct i915_gem_engines_iter it;
  2218. struct intel_context *ce;
  2219. int err = 0;
  2220. for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
  2221. GEM_BUG_ON(ce == ce->engine->kernel_context);
  2222. if (ce->engine->class != RENDER_CLASS)
  2223. continue;
  2224. /* Otherwise OA settings will be set upon first use */
  2225. if (!intel_context_pin_if_active(ce))
  2226. continue;
  2227. flex->value = intel_sseu_make_rpcs(ce->engine->gt, &ce->sseu);
  2228. err = gen8_modify_context(ce, flex, count);
  2229. intel_context_unpin(ce);
  2230. if (err)
  2231. break;
  2232. }
  2233. i915_gem_context_unlock_engines(ctx);
  2234. return err;
  2235. }
  2236. static int gen12_configure_oar_context(struct i915_perf_stream *stream,
  2237. struct i915_active *active)
  2238. {
  2239. int err;
  2240. struct intel_context *ce = stream->pinned_ctx;
  2241. u32 format = stream->oa_buffer.format->format;
  2242. u32 offset = stream->perf->ctx_oactxctrl_offset;
  2243. struct flex regs_context[] = {
  2244. {
  2245. GEN8_OACTXCONTROL,
  2246. offset + 1,
  2247. active ? GEN8_OA_COUNTER_RESUME : 0,
  2248. },
  2249. };
  2250. /* Offsets in regs_lri are not used since this configuration is only
  2251. * applied using LRI. Initialize the correct offsets for posterity.
  2252. */
  2253. #define GEN12_OAR_OACONTROL_OFFSET 0x5B0
  2254. struct flex regs_lri[] = {
  2255. {
  2256. GEN12_OAR_OACONTROL,
  2257. GEN12_OAR_OACONTROL_OFFSET + 1,
  2258. (format << GEN12_OAR_OACONTROL_COUNTER_FORMAT_SHIFT) |
  2259. (active ? GEN12_OAR_OACONTROL_COUNTER_ENABLE : 0)
  2260. },
  2261. {
  2262. RING_CONTEXT_CONTROL(ce->engine->mmio_base),
  2263. CTX_CONTEXT_CONTROL,
  2264. _MASKED_FIELD(GEN12_CTX_CTRL_OAR_CONTEXT_ENABLE,
  2265. active ?
  2266. GEN12_CTX_CTRL_OAR_CONTEXT_ENABLE :
  2267. 0)
  2268. },
  2269. };
  2270. /* Modify the context image of pinned context with regs_context */
  2271. err = intel_context_lock_pinned(ce);
  2272. if (err)
  2273. return err;
  2274. err = gen8_modify_context(ce, regs_context,
  2275. ARRAY_SIZE(regs_context));
  2276. intel_context_unlock_pinned(ce);
  2277. if (err)
  2278. return err;
  2279. /* Apply regs_lri using LRI with pinned context */
  2280. return gen8_modify_self(ce, regs_lri, ARRAY_SIZE(regs_lri), active);
  2281. }
  2282. /*
  2283. * Manages updating the per-context aspects of the OA stream
  2284. * configuration across all contexts.
  2285. *
  2286. * The awkward consideration here is that OACTXCONTROL controls the
  2287. * exponent for periodic sampling which is primarily used for system
  2288. * wide profiling where we'd like a consistent sampling period even in
  2289. * the face of context switches.
  2290. *
  2291. * Our approach of updating the register state context (as opposed to
  2292. * say using a workaround batch buffer) ensures that the hardware
  2293. * won't automatically reload an out-of-date timer exponent even
  2294. * transiently before a WA BB could be parsed.
  2295. *
  2296. * This function needs to:
  2297. * - Ensure the currently running context's per-context OA state is
  2298. * updated
  2299. * - Ensure that all existing contexts will have the correct per-context
  2300. * OA state if they are scheduled for use.
  2301. * - Ensure any new contexts will be initialized with the correct
  2302. * per-context OA state.
  2303. *
  2304. * Note: it's only the RCS/Render context that has any OA state.
  2305. * Note: the first flex register passed must always be R_PWR_CLK_STATE
  2306. */
  2307. static int
  2308. oa_configure_all_contexts(struct i915_perf_stream *stream,
  2309. struct flex *regs,
  2310. size_t num_regs,
  2311. struct i915_active *active)
  2312. {
  2313. struct drm_i915_private *i915 = stream->perf->i915;
  2314. struct intel_engine_cs *engine;
  2315. struct intel_gt *gt = stream->engine->gt;
  2316. struct i915_gem_context *ctx, *cn;
  2317. int err;
  2318. lockdep_assert_held(&gt->perf.lock);
  2319. /*
  2320. * The OA register config is setup through the context image. This image
  2321. * might be written to by the GPU on context switch (in particular on
  2322. * lite-restore). This means we can't safely update a context's image,
  2323. * if this context is scheduled/submitted to run on the GPU.
  2324. *
  2325. * We could emit the OA register config through the batch buffer but
  2326. * this might leave small interval of time where the OA unit is
  2327. * configured at an invalid sampling period.
  2328. *
  2329. * Note that since we emit all requests from a single ring, there
  2330. * is still an implicit global barrier here that may cause a high
  2331. * priority context to wait for an otherwise independent low priority
  2332. * context. Contexts idle at the time of reconfiguration are not
  2333. * trapped behind the barrier.
  2334. */
  2335. spin_lock(&i915->gem.contexts.lock);
  2336. list_for_each_entry_safe(ctx, cn, &i915->gem.contexts.list, link) {
  2337. if (!kref_get_unless_zero(&ctx->ref))
  2338. continue;
  2339. spin_unlock(&i915->gem.contexts.lock);
  2340. err = gen8_configure_context(stream, ctx, regs, num_regs);
  2341. if (err) {
  2342. i915_gem_context_put(ctx);
  2343. return err;
  2344. }
  2345. spin_lock(&i915->gem.contexts.lock);
  2346. list_safe_reset_next(ctx, cn, link);
  2347. i915_gem_context_put(ctx);
  2348. }
  2349. spin_unlock(&i915->gem.contexts.lock);
  2350. /*
  2351. * After updating all other contexts, we need to modify ourselves.
  2352. * If we don't modify the kernel_context, we do not get events while
  2353. * idle.
  2354. */
  2355. for_each_uabi_engine(engine, i915) {
  2356. struct intel_context *ce = engine->kernel_context;
  2357. if (engine->class != RENDER_CLASS)
  2358. continue;
  2359. regs[0].value = intel_sseu_make_rpcs(engine->gt, &ce->sseu);
  2360. err = gen8_modify_self(ce, regs, num_regs, active);
  2361. if (err)
  2362. return err;
  2363. }
  2364. return 0;
  2365. }
  2366. static int
  2367. lrc_configure_all_contexts(struct i915_perf_stream *stream,
  2368. const struct i915_oa_config *oa_config,
  2369. struct i915_active *active)
  2370. {
  2371. u32 ctx_oactxctrl = stream->perf->ctx_oactxctrl_offset;
  2372. /* The MMIO offsets for Flex EU registers aren't contiguous */
  2373. const u32 ctx_flexeu0 = stream->perf->ctx_flexeu0_offset;
  2374. #define ctx_flexeuN(N) (ctx_flexeu0 + 2 * (N) + 1)
  2375. struct flex regs[] = {
  2376. {
  2377. GEN8_R_PWR_CLK_STATE(RENDER_RING_BASE),
  2378. CTX_R_PWR_CLK_STATE,
  2379. },
  2380. {
  2381. GEN8_OACTXCONTROL,
  2382. ctx_oactxctrl + 1,
  2383. },
  2384. { EU_PERF_CNTL0, ctx_flexeuN(0) },
  2385. { EU_PERF_CNTL1, ctx_flexeuN(1) },
  2386. { EU_PERF_CNTL2, ctx_flexeuN(2) },
  2387. { EU_PERF_CNTL3, ctx_flexeuN(3) },
  2388. { EU_PERF_CNTL4, ctx_flexeuN(4) },
  2389. { EU_PERF_CNTL5, ctx_flexeuN(5) },
  2390. { EU_PERF_CNTL6, ctx_flexeuN(6) },
  2391. };
  2392. #undef ctx_flexeuN
  2393. int i;
  2394. regs[1].value =
  2395. (stream->period_exponent << GEN8_OA_TIMER_PERIOD_SHIFT) |
  2396. (stream->periodic ? GEN8_OA_TIMER_ENABLE : 0) |
  2397. GEN8_OA_COUNTER_RESUME;
  2398. for (i = 2; i < ARRAY_SIZE(regs); i++)
  2399. regs[i].value = oa_config_flex_reg(oa_config, regs[i].reg);
  2400. return oa_configure_all_contexts(stream,
  2401. regs, ARRAY_SIZE(regs),
  2402. active);
  2403. }
  2404. static int
  2405. gen8_enable_metric_set(struct i915_perf_stream *stream,
  2406. struct i915_active *active)
  2407. {
  2408. struct intel_uncore *uncore = stream->uncore;
  2409. struct i915_oa_config *oa_config = stream->oa_config;
  2410. int ret;
  2411. /*
  2412. * We disable slice/unslice clock ratio change reports on SKL since
  2413. * they are too noisy. The HW generates a lot of redundant reports
  2414. * where the ratio hasn't really changed causing a lot of redundant
  2415. * work to processes and increasing the chances we'll hit buffer
  2416. * overruns.
  2417. *
  2418. * Although we don't currently use the 'disable overrun' OABUFFER
  2419. * feature it's worth noting that clock ratio reports have to be
  2420. * disabled before considering to use that feature since the HW doesn't
  2421. * correctly block these reports.
  2422. *
  2423. * Currently none of the high-level metrics we have depend on knowing
  2424. * this ratio to normalize.
  2425. *
  2426. * Note: This register is not power context saved and restored, but
  2427. * that's OK considering that we disable RC6 while the OA unit is
  2428. * enabled.
  2429. *
  2430. * The _INCLUDE_CLK_RATIO bit allows the slice/unslice frequency to
  2431. * be read back from automatically triggered reports, as part of the
  2432. * RPT_ID field.
  2433. */
  2434. if (IS_GRAPHICS_VER(stream->perf->i915, 9, 11)) {
  2435. intel_uncore_write(uncore, GEN8_OA_DEBUG,
  2436. _MASKED_BIT_ENABLE(GEN9_OA_DEBUG_DISABLE_CLK_RATIO_REPORTS |
  2437. GEN9_OA_DEBUG_INCLUDE_CLK_RATIO));
  2438. }
  2439. /*
  2440. * Update all contexts prior writing the mux configurations as we need
  2441. * to make sure all slices/subslices are ON before writing to NOA
  2442. * registers.
  2443. */
  2444. ret = lrc_configure_all_contexts(stream, oa_config, active);
  2445. if (ret)
  2446. return ret;
  2447. return emit_oa_config(stream,
  2448. stream->oa_config, oa_context(stream),
  2449. active);
  2450. }
  2451. static u32 oag_report_ctx_switches(const struct i915_perf_stream *stream)
  2452. {
  2453. return _MASKED_FIELD(GEN12_OAG_OA_DEBUG_DISABLE_CTX_SWITCH_REPORTS,
  2454. (stream->sample_flags & SAMPLE_OA_REPORT) ?
  2455. 0 : GEN12_OAG_OA_DEBUG_DISABLE_CTX_SWITCH_REPORTS);
  2456. }
  2457. static int
  2458. gen12_enable_metric_set(struct i915_perf_stream *stream,
  2459. struct i915_active *active)
  2460. {
  2461. struct drm_i915_private *i915 = stream->perf->i915;
  2462. struct intel_uncore *uncore = stream->uncore;
  2463. bool periodic = stream->periodic;
  2464. u32 period_exponent = stream->period_exponent;
  2465. u32 sqcnt1;
  2466. int ret;
  2467. /*
  2468. * Wa_1508761755
  2469. * EU NOA signals behave incorrectly if EU clock gating is enabled.
  2470. * Disable thread stall DOP gating and EU DOP gating.
  2471. */
  2472. if (IS_DG2(i915)) {
  2473. intel_gt_mcr_multicast_write(uncore->gt, GEN8_ROW_CHICKEN,
  2474. _MASKED_BIT_ENABLE(STALL_DOP_GATING_DISABLE));
  2475. intel_uncore_write(uncore, GEN7_ROW_CHICKEN2,
  2476. _MASKED_BIT_ENABLE(GEN12_DISABLE_DOP_GATING));
  2477. }
  2478. intel_uncore_write(uncore, __oa_regs(stream)->oa_debug,
  2479. /* Disable clk ratio reports, like previous Gens. */
  2480. _MASKED_BIT_ENABLE(GEN12_OAG_OA_DEBUG_DISABLE_CLK_RATIO_REPORTS |
  2481. GEN12_OAG_OA_DEBUG_INCLUDE_CLK_RATIO) |
  2482. /*
  2483. * If the user didn't require OA reports, instruct
  2484. * the hardware not to emit ctx switch reports.
  2485. */
  2486. oag_report_ctx_switches(stream));
  2487. intel_uncore_write(uncore, __oa_regs(stream)->oa_ctx_ctrl, periodic ?
  2488. (GEN12_OAG_OAGLBCTXCTRL_COUNTER_RESUME |
  2489. GEN12_OAG_OAGLBCTXCTRL_TIMER_ENABLE |
  2490. (period_exponent << GEN12_OAG_OAGLBCTXCTRL_TIMER_PERIOD_SHIFT))
  2491. : 0);
  2492. /*
  2493. * Initialize Super Queue Internal Cnt Register
  2494. * Set PMON Enable in order to collect valid metrics.
  2495. * Enable bytes per clock reporting in OA.
  2496. */
  2497. sqcnt1 = GEN12_SQCNT1_PMON_ENABLE |
  2498. (HAS_OA_BPC_REPORTING(i915) ? GEN12_SQCNT1_OABPC : 0);
  2499. intel_uncore_rmw(uncore, GEN12_SQCNT1, 0, sqcnt1);
  2500. /*
  2501. * For Gen12, performance counters are context
  2502. * saved/restored. Only enable it for the context that
  2503. * requested this.
  2504. */
  2505. if (stream->ctx) {
  2506. ret = gen12_configure_oar_context(stream, active);
  2507. if (ret)
  2508. return ret;
  2509. }
  2510. return emit_oa_config(stream,
  2511. stream->oa_config, oa_context(stream),
  2512. active);
  2513. }
  2514. static void gen8_disable_metric_set(struct i915_perf_stream *stream)
  2515. {
  2516. struct intel_uncore *uncore = stream->uncore;
  2517. /* Reset all contexts' slices/subslices configurations. */
  2518. lrc_configure_all_contexts(stream, NULL, NULL);
  2519. intel_uncore_rmw(uncore, GDT_CHICKEN_BITS, GT_NOA_ENABLE, 0);
  2520. }
  2521. static void gen11_disable_metric_set(struct i915_perf_stream *stream)
  2522. {
  2523. struct intel_uncore *uncore = stream->uncore;
  2524. /* Reset all contexts' slices/subslices configurations. */
  2525. lrc_configure_all_contexts(stream, NULL, NULL);
  2526. /* Make sure we disable noa to save power. */
  2527. intel_uncore_rmw(uncore, RPM_CONFIG1, GEN10_GT_NOA_ENABLE, 0);
  2528. }
  2529. static void gen12_disable_metric_set(struct i915_perf_stream *stream)
  2530. {
  2531. struct intel_uncore *uncore = stream->uncore;
  2532. struct drm_i915_private *i915 = stream->perf->i915;
  2533. u32 sqcnt1;
  2534. /*
  2535. * Wa_1508761755: Enable thread stall DOP gating and EU DOP gating.
  2536. */
  2537. if (IS_DG2(i915)) {
  2538. intel_gt_mcr_multicast_write(uncore->gt, GEN8_ROW_CHICKEN,
  2539. _MASKED_BIT_DISABLE(STALL_DOP_GATING_DISABLE));
  2540. intel_uncore_write(uncore, GEN7_ROW_CHICKEN2,
  2541. _MASKED_BIT_DISABLE(GEN12_DISABLE_DOP_GATING));
  2542. }
  2543. /* disable the context save/restore or OAR counters */
  2544. if (stream->ctx)
  2545. gen12_configure_oar_context(stream, NULL);
  2546. /* Make sure we disable noa to save power. */
  2547. intel_uncore_rmw(uncore, RPM_CONFIG1, GEN10_GT_NOA_ENABLE, 0);
  2548. sqcnt1 = GEN12_SQCNT1_PMON_ENABLE |
  2549. (HAS_OA_BPC_REPORTING(i915) ? GEN12_SQCNT1_OABPC : 0);
  2550. /* Reset PMON Enable to save power. */
  2551. intel_uncore_rmw(uncore, GEN12_SQCNT1, sqcnt1, 0);
  2552. }
  2553. static void gen7_oa_enable(struct i915_perf_stream *stream)
  2554. {
  2555. struct intel_uncore *uncore = stream->uncore;
  2556. struct i915_gem_context *ctx = stream->ctx;
  2557. u32 ctx_id = stream->specific_ctx_id;
  2558. bool periodic = stream->periodic;
  2559. u32 period_exponent = stream->period_exponent;
  2560. u32 report_format = stream->oa_buffer.format->format;
  2561. /*
  2562. * Reset buf pointers so we don't forward reports from before now.
  2563. *
  2564. * Think carefully if considering trying to avoid this, since it
  2565. * also ensures status flags and the buffer itself are cleared
  2566. * in error paths, and we have checks for invalid reports based
  2567. * on the assumption that certain fields are written to zeroed
  2568. * memory which this helps maintains.
  2569. */
  2570. gen7_init_oa_buffer(stream);
  2571. intel_uncore_write(uncore, GEN7_OACONTROL,
  2572. (ctx_id & GEN7_OACONTROL_CTX_MASK) |
  2573. (period_exponent <<
  2574. GEN7_OACONTROL_TIMER_PERIOD_SHIFT) |
  2575. (periodic ? GEN7_OACONTROL_TIMER_ENABLE : 0) |
  2576. (report_format << GEN7_OACONTROL_FORMAT_SHIFT) |
  2577. (ctx ? GEN7_OACONTROL_PER_CTX_ENABLE : 0) |
  2578. GEN7_OACONTROL_ENABLE);
  2579. }
  2580. static void gen8_oa_enable(struct i915_perf_stream *stream)
  2581. {
  2582. struct intel_uncore *uncore = stream->uncore;
  2583. u32 report_format = stream->oa_buffer.format->format;
  2584. /*
  2585. * Reset buf pointers so we don't forward reports from before now.
  2586. *
  2587. * Think carefully if considering trying to avoid this, since it
  2588. * also ensures status flags and the buffer itself are cleared
  2589. * in error paths, and we have checks for invalid reports based
  2590. * on the assumption that certain fields are written to zeroed
  2591. * memory which this helps maintains.
  2592. */
  2593. gen8_init_oa_buffer(stream);
  2594. /*
  2595. * Note: we don't rely on the hardware to perform single context
  2596. * filtering and instead filter on the cpu based on the context-id
  2597. * field of reports
  2598. */
  2599. intel_uncore_write(uncore, GEN8_OACONTROL,
  2600. (report_format << GEN8_OA_REPORT_FORMAT_SHIFT) |
  2601. GEN8_OA_COUNTER_ENABLE);
  2602. }
  2603. static void gen12_oa_enable(struct i915_perf_stream *stream)
  2604. {
  2605. const struct i915_perf_regs *regs;
  2606. u32 val;
  2607. /*
  2608. * If we don't want OA reports from the OA buffer, then we don't even
  2609. * need to program the OAG unit.
  2610. */
  2611. if (!(stream->sample_flags & SAMPLE_OA_REPORT))
  2612. return;
  2613. gen12_init_oa_buffer(stream);
  2614. regs = __oa_regs(stream);
  2615. val = (stream->oa_buffer.format->format << regs->oa_ctrl_counter_format_shift) |
  2616. GEN12_OAG_OACONTROL_OA_COUNTER_ENABLE;
  2617. intel_uncore_write(stream->uncore, regs->oa_ctrl, val);
  2618. }
  2619. /**
  2620. * i915_oa_stream_enable - handle `I915_PERF_IOCTL_ENABLE` for OA stream
  2621. * @stream: An i915 perf stream opened for OA metrics
  2622. *
  2623. * [Re]enables hardware periodic sampling according to the period configured
  2624. * when opening the stream. This also starts a hrtimer that will periodically
  2625. * check for data in the circular OA buffer for notifying userspace (e.g.
  2626. * during a read() or poll()).
  2627. */
  2628. static void i915_oa_stream_enable(struct i915_perf_stream *stream)
  2629. {
  2630. stream->pollin = false;
  2631. stream->perf->ops.oa_enable(stream);
  2632. if (stream->sample_flags & SAMPLE_OA_REPORT)
  2633. hrtimer_start(&stream->poll_check_timer,
  2634. ns_to_ktime(stream->poll_oa_period),
  2635. HRTIMER_MODE_REL_PINNED);
  2636. }
  2637. static void gen7_oa_disable(struct i915_perf_stream *stream)
  2638. {
  2639. struct intel_uncore *uncore = stream->uncore;
  2640. intel_uncore_write(uncore, GEN7_OACONTROL, 0);
  2641. if (intel_wait_for_register(uncore,
  2642. GEN7_OACONTROL, GEN7_OACONTROL_ENABLE, 0,
  2643. 50))
  2644. drm_err(&stream->perf->i915->drm,
  2645. "wait for OA to be disabled timed out\n");
  2646. }
  2647. static void gen8_oa_disable(struct i915_perf_stream *stream)
  2648. {
  2649. struct intel_uncore *uncore = stream->uncore;
  2650. intel_uncore_write(uncore, GEN8_OACONTROL, 0);
  2651. if (intel_wait_for_register(uncore,
  2652. GEN8_OACONTROL, GEN8_OA_COUNTER_ENABLE, 0,
  2653. 50))
  2654. drm_err(&stream->perf->i915->drm,
  2655. "wait for OA to be disabled timed out\n");
  2656. }
  2657. static void gen12_oa_disable(struct i915_perf_stream *stream)
  2658. {
  2659. struct intel_uncore *uncore = stream->uncore;
  2660. intel_uncore_write(uncore, __oa_regs(stream)->oa_ctrl, 0);
  2661. if (intel_wait_for_register(uncore,
  2662. __oa_regs(stream)->oa_ctrl,
  2663. GEN12_OAG_OACONTROL_OA_COUNTER_ENABLE, 0,
  2664. 50))
  2665. drm_err(&stream->perf->i915->drm,
  2666. "wait for OA to be disabled timed out\n");
  2667. intel_uncore_write(uncore, GEN12_OA_TLB_INV_CR, 1);
  2668. if (intel_wait_for_register(uncore,
  2669. GEN12_OA_TLB_INV_CR,
  2670. 1, 0,
  2671. 50))
  2672. drm_err(&stream->perf->i915->drm,
  2673. "wait for OA tlb invalidate timed out\n");
  2674. }
  2675. /**
  2676. * i915_oa_stream_disable - handle `I915_PERF_IOCTL_DISABLE` for OA stream
  2677. * @stream: An i915 perf stream opened for OA metrics
  2678. *
  2679. * Stops the OA unit from periodically writing counter reports into the
  2680. * circular OA buffer. This also stops the hrtimer that periodically checks for
  2681. * data in the circular OA buffer, for notifying userspace.
  2682. */
  2683. static void i915_oa_stream_disable(struct i915_perf_stream *stream)
  2684. {
  2685. stream->perf->ops.oa_disable(stream);
  2686. if (stream->sample_flags & SAMPLE_OA_REPORT)
  2687. hrtimer_cancel(&stream->poll_check_timer);
  2688. }
  2689. static const struct i915_perf_stream_ops i915_oa_stream_ops = {
  2690. .destroy = i915_oa_stream_destroy,
  2691. .enable = i915_oa_stream_enable,
  2692. .disable = i915_oa_stream_disable,
  2693. .wait_unlocked = i915_oa_wait_unlocked,
  2694. .poll_wait = i915_oa_poll_wait,
  2695. .read = i915_oa_read,
  2696. };
  2697. static int i915_perf_stream_enable_sync(struct i915_perf_stream *stream)
  2698. {
  2699. struct i915_active *active;
  2700. int err;
  2701. active = i915_active_create();
  2702. if (!active)
  2703. return -ENOMEM;
  2704. err = stream->perf->ops.enable_metric_set(stream, active);
  2705. if (err == 0)
  2706. __i915_active_wait(active, TASK_UNINTERRUPTIBLE);
  2707. i915_active_put(active);
  2708. return err;
  2709. }
  2710. static void
  2711. get_default_sseu_config(struct intel_sseu *out_sseu,
  2712. struct intel_engine_cs *engine)
  2713. {
  2714. const struct sseu_dev_info *devinfo_sseu = &engine->gt->info.sseu;
  2715. *out_sseu = intel_sseu_from_device_info(devinfo_sseu);
  2716. if (GRAPHICS_VER(engine->i915) == 11) {
  2717. /*
  2718. * We only need subslice count so it doesn't matter which ones
  2719. * we select - just turn off low bits in the amount of half of
  2720. * all available subslices per slice.
  2721. */
  2722. out_sseu->subslice_mask =
  2723. ~(~0 << (hweight8(out_sseu->subslice_mask) / 2));
  2724. out_sseu->slice_mask = 0x1;
  2725. }
  2726. }
  2727. static int
  2728. get_sseu_config(struct intel_sseu *out_sseu,
  2729. struct intel_engine_cs *engine,
  2730. const struct drm_i915_gem_context_param_sseu *drm_sseu)
  2731. {
  2732. if (drm_sseu->engine.engine_class != engine->uabi_class ||
  2733. drm_sseu->engine.engine_instance != engine->uabi_instance)
  2734. return -EINVAL;
  2735. return i915_gem_user_to_context_sseu(engine->gt, drm_sseu, out_sseu);
  2736. }
  2737. /*
  2738. * OA timestamp frequency = CS timestamp frequency in most platforms. On some
  2739. * platforms OA unit ignores the CTC_SHIFT and the 2 timestamps differ. In such
  2740. * cases, return the adjusted CS timestamp frequency to the user.
  2741. */
  2742. u32 i915_perf_oa_timestamp_frequency(struct drm_i915_private *i915)
  2743. {
  2744. struct intel_gt *gt = to_gt(i915);
  2745. /* Wa_18013179988 */
  2746. if (IS_DG2(i915) || IS_GFX_GT_IP_RANGE(gt, IP_VER(12, 70), IP_VER(12, 74))) {
  2747. intel_wakeref_t wakeref;
  2748. u32 reg, shift;
  2749. with_intel_runtime_pm(to_gt(i915)->uncore->rpm, wakeref)
  2750. reg = intel_uncore_read(to_gt(i915)->uncore, RPM_CONFIG0);
  2751. shift = REG_FIELD_GET(GEN10_RPM_CONFIG0_CTC_SHIFT_PARAMETER_MASK,
  2752. reg);
  2753. return to_gt(i915)->clock_frequency << (3 - shift);
  2754. }
  2755. return to_gt(i915)->clock_frequency;
  2756. }
  2757. /**
  2758. * i915_oa_stream_init - validate combined props for OA stream and init
  2759. * @stream: An i915 perf stream
  2760. * @param: The open parameters passed to `DRM_I915_PERF_OPEN`
  2761. * @props: The property state that configures stream (individually validated)
  2762. *
  2763. * While read_properties_unlocked() validates properties in isolation it
  2764. * doesn't ensure that the combination necessarily makes sense.
  2765. *
  2766. * At this point it has been determined that userspace wants a stream of
  2767. * OA metrics, but still we need to further validate the combined
  2768. * properties are OK.
  2769. *
  2770. * If the configuration makes sense then we can allocate memory for
  2771. * a circular OA buffer and apply the requested metric set configuration.
  2772. *
  2773. * Returns: zero on success or a negative error code.
  2774. */
  2775. static int i915_oa_stream_init(struct i915_perf_stream *stream,
  2776. struct drm_i915_perf_open_param *param,
  2777. struct perf_open_properties *props)
  2778. {
  2779. struct drm_i915_private *i915 = stream->perf->i915;
  2780. struct i915_perf *perf = stream->perf;
  2781. struct i915_perf_group *g;
  2782. int ret;
  2783. if (!props->engine) {
  2784. drm_dbg(&stream->perf->i915->drm,
  2785. "OA engine not specified\n");
  2786. return -EINVAL;
  2787. }
  2788. g = props->engine->oa_group;
  2789. /*
  2790. * If the sysfs metrics/ directory wasn't registered for some
  2791. * reason then don't let userspace try their luck with config
  2792. * IDs
  2793. */
  2794. if (!perf->metrics_kobj) {
  2795. drm_dbg(&stream->perf->i915->drm,
  2796. "OA metrics weren't advertised via sysfs\n");
  2797. return -EINVAL;
  2798. }
  2799. if (!(props->sample_flags & SAMPLE_OA_REPORT) &&
  2800. (GRAPHICS_VER(perf->i915) < 12 || !stream->ctx)) {
  2801. drm_dbg(&stream->perf->i915->drm,
  2802. "Only OA report sampling supported\n");
  2803. return -EINVAL;
  2804. }
  2805. if (!perf->ops.enable_metric_set) {
  2806. drm_dbg(&stream->perf->i915->drm,
  2807. "OA unit not supported\n");
  2808. return -ENODEV;
  2809. }
  2810. /*
  2811. * To avoid the complexity of having to accurately filter
  2812. * counter reports and marshal to the appropriate client
  2813. * we currently only allow exclusive access
  2814. */
  2815. if (g->exclusive_stream) {
  2816. drm_dbg(&stream->perf->i915->drm,
  2817. "OA unit already in use\n");
  2818. return -EBUSY;
  2819. }
  2820. if (!props->oa_format) {
  2821. drm_dbg(&stream->perf->i915->drm,
  2822. "OA report format not specified\n");
  2823. return -EINVAL;
  2824. }
  2825. stream->engine = props->engine;
  2826. stream->uncore = stream->engine->gt->uncore;
  2827. stream->sample_size = sizeof(struct drm_i915_perf_record_header);
  2828. stream->oa_buffer.format = &perf->oa_formats[props->oa_format];
  2829. if (drm_WARN_ON(&i915->drm, stream->oa_buffer.format->size == 0))
  2830. return -EINVAL;
  2831. stream->sample_flags = props->sample_flags;
  2832. stream->sample_size += stream->oa_buffer.format->size;
  2833. stream->hold_preemption = props->hold_preemption;
  2834. stream->periodic = props->oa_periodic;
  2835. if (stream->periodic)
  2836. stream->period_exponent = props->oa_period_exponent;
  2837. if (stream->ctx) {
  2838. ret = oa_get_render_ctx_id(stream);
  2839. if (ret) {
  2840. drm_dbg(&stream->perf->i915->drm,
  2841. "Invalid context id to filter with\n");
  2842. return ret;
  2843. }
  2844. }
  2845. ret = alloc_noa_wait(stream);
  2846. if (ret) {
  2847. drm_dbg(&stream->perf->i915->drm,
  2848. "Unable to allocate NOA wait batch buffer\n");
  2849. goto err_noa_wait_alloc;
  2850. }
  2851. stream->oa_config = i915_perf_get_oa_config(perf, props->metrics_set);
  2852. if (!stream->oa_config) {
  2853. drm_dbg(&stream->perf->i915->drm,
  2854. "Invalid OA config id=%i\n", props->metrics_set);
  2855. ret = -EINVAL;
  2856. goto err_config;
  2857. }
  2858. /* PRM - observability performance counters:
  2859. *
  2860. * OACONTROL, performance counter enable, note:
  2861. *
  2862. * "When this bit is set, in order to have coherent counts,
  2863. * RC6 power state and trunk clock gating must be disabled.
  2864. * This can be achieved by programming MMIO registers as
  2865. * 0xA094=0 and 0xA090[31]=1"
  2866. *
  2867. * In our case we are expecting that taking pm + FORCEWAKE
  2868. * references will effectively disable RC6.
  2869. */
  2870. intel_engine_pm_get(stream->engine);
  2871. intel_uncore_forcewake_get(stream->uncore, FORCEWAKE_ALL);
  2872. ret = alloc_oa_buffer(stream);
  2873. if (ret)
  2874. goto err_oa_buf_alloc;
  2875. stream->ops = &i915_oa_stream_ops;
  2876. stream->engine->gt->perf.sseu = props->sseu;
  2877. WRITE_ONCE(g->exclusive_stream, stream);
  2878. ret = i915_perf_stream_enable_sync(stream);
  2879. if (ret) {
  2880. drm_dbg(&stream->perf->i915->drm,
  2881. "Unable to enable metric set\n");
  2882. goto err_enable;
  2883. }
  2884. drm_dbg(&stream->perf->i915->drm,
  2885. "opening stream oa config uuid=%s\n",
  2886. stream->oa_config->uuid);
  2887. hrtimer_setup(&stream->poll_check_timer, oa_poll_check_timer_cb, CLOCK_MONOTONIC,
  2888. HRTIMER_MODE_REL);
  2889. init_waitqueue_head(&stream->poll_wq);
  2890. spin_lock_init(&stream->oa_buffer.ptr_lock);
  2891. mutex_init(&stream->lock);
  2892. return 0;
  2893. err_enable:
  2894. WRITE_ONCE(g->exclusive_stream, NULL);
  2895. perf->ops.disable_metric_set(stream);
  2896. free_oa_buffer(stream);
  2897. err_oa_buf_alloc:
  2898. intel_uncore_forcewake_put(stream->uncore, FORCEWAKE_ALL);
  2899. intel_engine_pm_put(stream->engine);
  2900. free_oa_configs(stream);
  2901. err_config:
  2902. free_noa_wait(stream);
  2903. err_noa_wait_alloc:
  2904. if (stream->ctx)
  2905. oa_put_render_ctx_id(stream);
  2906. return ret;
  2907. }
  2908. void i915_oa_init_reg_state(const struct intel_context *ce,
  2909. const struct intel_engine_cs *engine)
  2910. {
  2911. struct i915_perf_stream *stream;
  2912. if (engine->class != RENDER_CLASS)
  2913. return;
  2914. /* perf.exclusive_stream serialised by lrc_configure_all_contexts() */
  2915. stream = READ_ONCE(engine->oa_group->exclusive_stream);
  2916. if (stream && GRAPHICS_VER(stream->perf->i915) < 12)
  2917. gen8_update_reg_state_unlocked(ce, stream);
  2918. }
  2919. /**
  2920. * i915_perf_read - handles read() FOP for i915 perf stream FDs
  2921. * @file: An i915 perf stream file
  2922. * @buf: destination buffer given by userspace
  2923. * @count: the number of bytes userspace wants to read
  2924. * @ppos: (inout) file seek position (unused)
  2925. *
  2926. * The entry point for handling a read() on a stream file descriptor from
  2927. * userspace. Most of the work is left to the i915_perf_read_locked() and
  2928. * &i915_perf_stream_ops->read but to save having stream implementations (of
  2929. * which we might have multiple later) we handle blocking read here.
  2930. *
  2931. * We can also consistently treat trying to read from a disabled stream
  2932. * as an IO error so implementations can assume the stream is enabled
  2933. * while reading.
  2934. *
  2935. * Returns: The number of bytes copied or a negative error code on failure.
  2936. */
  2937. static ssize_t i915_perf_read(struct file *file,
  2938. char __user *buf,
  2939. size_t count,
  2940. loff_t *ppos)
  2941. {
  2942. struct i915_perf_stream *stream = file->private_data;
  2943. size_t offset = 0;
  2944. int ret;
  2945. /* To ensure it's handled consistently we simply treat all reads of a
  2946. * disabled stream as an error. In particular it might otherwise lead
  2947. * to a deadlock for blocking file descriptors...
  2948. */
  2949. if (!stream->enabled || !(stream->sample_flags & SAMPLE_OA_REPORT))
  2950. return -EIO;
  2951. if (!(file->f_flags & O_NONBLOCK)) {
  2952. /* There's the small chance of false positives from
  2953. * stream->ops->wait_unlocked.
  2954. *
  2955. * E.g. with single context filtering since we only wait until
  2956. * oabuffer has >= 1 report we don't immediately know whether
  2957. * any reports really belong to the current context
  2958. */
  2959. do {
  2960. ret = stream->ops->wait_unlocked(stream);
  2961. if (ret)
  2962. return ret;
  2963. mutex_lock(&stream->lock);
  2964. ret = stream->ops->read(stream, buf, count, &offset);
  2965. mutex_unlock(&stream->lock);
  2966. } while (!offset && !ret);
  2967. } else {
  2968. mutex_lock(&stream->lock);
  2969. ret = stream->ops->read(stream, buf, count, &offset);
  2970. mutex_unlock(&stream->lock);
  2971. }
  2972. /* We allow the poll checking to sometimes report false positive EPOLLIN
  2973. * events where we might actually report EAGAIN on read() if there's
  2974. * not really any data available. In this situation though we don't
  2975. * want to enter a busy loop between poll() reporting a EPOLLIN event
  2976. * and read() returning -EAGAIN. Clearing the oa.pollin state here
  2977. * effectively ensures we back off until the next hrtimer callback
  2978. * before reporting another EPOLLIN event.
  2979. * The exception to this is if ops->read() returned -ENOSPC which means
  2980. * that more OA data is available than could fit in the user provided
  2981. * buffer. In this case we want the next poll() call to not block.
  2982. */
  2983. if (ret != -ENOSPC)
  2984. stream->pollin = false;
  2985. /* Possible values for ret are 0, -EFAULT, -ENOSPC, -EIO, ... */
  2986. return offset ?: (ret ?: -EAGAIN);
  2987. }
  2988. static enum hrtimer_restart oa_poll_check_timer_cb(struct hrtimer *hrtimer)
  2989. {
  2990. struct i915_perf_stream *stream =
  2991. container_of(hrtimer, typeof(*stream), poll_check_timer);
  2992. if (oa_buffer_check_unlocked(stream)) {
  2993. stream->pollin = true;
  2994. wake_up(&stream->poll_wq);
  2995. }
  2996. hrtimer_forward_now(hrtimer,
  2997. ns_to_ktime(stream->poll_oa_period));
  2998. return HRTIMER_RESTART;
  2999. }
  3000. /**
  3001. * i915_perf_poll_locked - poll_wait() with a suitable wait queue for stream
  3002. * @stream: An i915 perf stream
  3003. * @file: An i915 perf stream file
  3004. * @wait: poll() state table
  3005. *
  3006. * For handling userspace polling on an i915 perf stream, this calls through to
  3007. * &i915_perf_stream_ops->poll_wait to call poll_wait() with a wait queue that
  3008. * will be woken for new stream data.
  3009. *
  3010. * Returns: any poll events that are ready without sleeping
  3011. */
  3012. static __poll_t i915_perf_poll_locked(struct i915_perf_stream *stream,
  3013. struct file *file,
  3014. poll_table *wait)
  3015. {
  3016. __poll_t events = 0;
  3017. stream->ops->poll_wait(stream, file, wait);
  3018. /* Note: we don't explicitly check whether there's something to read
  3019. * here since this path may be very hot depending on what else
  3020. * userspace is polling, or on the timeout in use. We rely solely on
  3021. * the hrtimer/oa_poll_check_timer_cb to notify us when there are
  3022. * samples to read.
  3023. */
  3024. if (stream->pollin)
  3025. events |= EPOLLIN;
  3026. return events;
  3027. }
  3028. /**
  3029. * i915_perf_poll - call poll_wait() with a suitable wait queue for stream
  3030. * @file: An i915 perf stream file
  3031. * @wait: poll() state table
  3032. *
  3033. * For handling userspace polling on an i915 perf stream, this ensures
  3034. * poll_wait() gets called with a wait queue that will be woken for new stream
  3035. * data.
  3036. *
  3037. * Note: Implementation deferred to i915_perf_poll_locked()
  3038. *
  3039. * Returns: any poll events that are ready without sleeping
  3040. */
  3041. static __poll_t i915_perf_poll(struct file *file, poll_table *wait)
  3042. {
  3043. struct i915_perf_stream *stream = file->private_data;
  3044. __poll_t ret;
  3045. mutex_lock(&stream->lock);
  3046. ret = i915_perf_poll_locked(stream, file, wait);
  3047. mutex_unlock(&stream->lock);
  3048. return ret;
  3049. }
  3050. /**
  3051. * i915_perf_enable_locked - handle `I915_PERF_IOCTL_ENABLE` ioctl
  3052. * @stream: A disabled i915 perf stream
  3053. *
  3054. * [Re]enables the associated capture of data for this stream.
  3055. *
  3056. * If a stream was previously enabled then there's currently no intention
  3057. * to provide userspace any guarantee about the preservation of previously
  3058. * buffered data.
  3059. */
  3060. static void i915_perf_enable_locked(struct i915_perf_stream *stream)
  3061. {
  3062. if (stream->enabled)
  3063. return;
  3064. /* Allow stream->ops->enable() to refer to this */
  3065. stream->enabled = true;
  3066. if (stream->ops->enable)
  3067. stream->ops->enable(stream);
  3068. if (stream->hold_preemption)
  3069. intel_context_set_nopreempt(stream->pinned_ctx);
  3070. }
  3071. /**
  3072. * i915_perf_disable_locked - handle `I915_PERF_IOCTL_DISABLE` ioctl
  3073. * @stream: An enabled i915 perf stream
  3074. *
  3075. * Disables the associated capture of data for this stream.
  3076. *
  3077. * The intention is that disabling an re-enabling a stream will ideally be
  3078. * cheaper than destroying and re-opening a stream with the same configuration,
  3079. * though there are no formal guarantees about what state or buffered data
  3080. * must be retained between disabling and re-enabling a stream.
  3081. *
  3082. * Note: while a stream is disabled it's considered an error for userspace
  3083. * to attempt to read from the stream (-EIO).
  3084. */
  3085. static void i915_perf_disable_locked(struct i915_perf_stream *stream)
  3086. {
  3087. if (!stream->enabled)
  3088. return;
  3089. /* Allow stream->ops->disable() to refer to this */
  3090. stream->enabled = false;
  3091. if (stream->hold_preemption)
  3092. intel_context_clear_nopreempt(stream->pinned_ctx);
  3093. if (stream->ops->disable)
  3094. stream->ops->disable(stream);
  3095. }
  3096. static long i915_perf_config_locked(struct i915_perf_stream *stream,
  3097. unsigned long metrics_set)
  3098. {
  3099. struct i915_oa_config *config;
  3100. long ret = stream->oa_config->id;
  3101. config = i915_perf_get_oa_config(stream->perf, metrics_set);
  3102. if (!config)
  3103. return -EINVAL;
  3104. if (config != stream->oa_config) {
  3105. int err;
  3106. /*
  3107. * If OA is bound to a specific context, emit the
  3108. * reconfiguration inline from that context. The update
  3109. * will then be ordered with respect to submission on that
  3110. * context.
  3111. *
  3112. * When set globally, we use a low priority kernel context,
  3113. * so it will effectively take effect when idle.
  3114. */
  3115. err = emit_oa_config(stream, config, oa_context(stream), NULL);
  3116. if (!err)
  3117. config = xchg(&stream->oa_config, config);
  3118. else
  3119. ret = err;
  3120. }
  3121. i915_oa_config_put(config);
  3122. return ret;
  3123. }
  3124. /**
  3125. * i915_perf_ioctl_locked - support ioctl() usage with i915 perf stream FDs
  3126. * @stream: An i915 perf stream
  3127. * @cmd: the ioctl request
  3128. * @arg: the ioctl data
  3129. *
  3130. * Returns: zero on success or a negative error code. Returns -EINVAL for
  3131. * an unknown ioctl request.
  3132. */
  3133. static long i915_perf_ioctl_locked(struct i915_perf_stream *stream,
  3134. unsigned int cmd,
  3135. unsigned long arg)
  3136. {
  3137. switch (cmd) {
  3138. case I915_PERF_IOCTL_ENABLE:
  3139. i915_perf_enable_locked(stream);
  3140. return 0;
  3141. case I915_PERF_IOCTL_DISABLE:
  3142. i915_perf_disable_locked(stream);
  3143. return 0;
  3144. case I915_PERF_IOCTL_CONFIG:
  3145. return i915_perf_config_locked(stream, arg);
  3146. }
  3147. return -EINVAL;
  3148. }
  3149. /**
  3150. * i915_perf_ioctl - support ioctl() usage with i915 perf stream FDs
  3151. * @file: An i915 perf stream file
  3152. * @cmd: the ioctl request
  3153. * @arg: the ioctl data
  3154. *
  3155. * Implementation deferred to i915_perf_ioctl_locked().
  3156. *
  3157. * Returns: zero on success or a negative error code. Returns -EINVAL for
  3158. * an unknown ioctl request.
  3159. */
  3160. static long i915_perf_ioctl(struct file *file,
  3161. unsigned int cmd,
  3162. unsigned long arg)
  3163. {
  3164. struct i915_perf_stream *stream = file->private_data;
  3165. long ret;
  3166. mutex_lock(&stream->lock);
  3167. ret = i915_perf_ioctl_locked(stream, cmd, arg);
  3168. mutex_unlock(&stream->lock);
  3169. return ret;
  3170. }
  3171. /**
  3172. * i915_perf_destroy_locked - destroy an i915 perf stream
  3173. * @stream: An i915 perf stream
  3174. *
  3175. * Frees all resources associated with the given i915 perf @stream, disabling
  3176. * any associated data capture in the process.
  3177. *
  3178. * Note: The &gt->perf.lock mutex has been taken to serialize
  3179. * with any non-file-operation driver hooks.
  3180. */
  3181. static void i915_perf_destroy_locked(struct i915_perf_stream *stream)
  3182. {
  3183. if (stream->enabled)
  3184. i915_perf_disable_locked(stream);
  3185. if (stream->ops->destroy)
  3186. stream->ops->destroy(stream);
  3187. if (stream->ctx)
  3188. i915_gem_context_put(stream->ctx);
  3189. kfree(stream);
  3190. }
  3191. /**
  3192. * i915_perf_release - handles userspace close() of a stream file
  3193. * @inode: anonymous inode associated with file
  3194. * @file: An i915 perf stream file
  3195. *
  3196. * Cleans up any resources associated with an open i915 perf stream file.
  3197. *
  3198. * NB: close() can't really fail from the userspace point of view.
  3199. *
  3200. * Returns: zero on success or a negative error code.
  3201. */
  3202. static int i915_perf_release(struct inode *inode, struct file *file)
  3203. {
  3204. struct i915_perf_stream *stream = file->private_data;
  3205. struct i915_perf *perf = stream->perf;
  3206. struct intel_gt *gt = stream->engine->gt;
  3207. /*
  3208. * Within this call, we know that the fd is being closed and we have no
  3209. * other user of stream->lock. Use the perf lock to destroy the stream
  3210. * here.
  3211. */
  3212. mutex_lock(&gt->perf.lock);
  3213. i915_perf_destroy_locked(stream);
  3214. mutex_unlock(&gt->perf.lock);
  3215. /* Release the reference the perf stream kept on the driver. */
  3216. drm_dev_put(&perf->i915->drm);
  3217. return 0;
  3218. }
  3219. static const struct file_operations fops = {
  3220. .owner = THIS_MODULE,
  3221. .release = i915_perf_release,
  3222. .poll = i915_perf_poll,
  3223. .read = i915_perf_read,
  3224. .unlocked_ioctl = i915_perf_ioctl,
  3225. /* Our ioctl have no arguments, so it's safe to use the same function
  3226. * to handle 32bits compatibility.
  3227. */
  3228. .compat_ioctl = i915_perf_ioctl,
  3229. };
  3230. /**
  3231. * i915_perf_open_ioctl_locked - DRM ioctl() for userspace to open a stream FD
  3232. * @perf: i915 perf instance
  3233. * @param: The open parameters passed to 'DRM_I915_PERF_OPEN`
  3234. * @props: individually validated u64 property value pairs
  3235. * @file: drm file
  3236. *
  3237. * See i915_perf_ioctl_open() for interface details.
  3238. *
  3239. * Implements further stream config validation and stream initialization on
  3240. * behalf of i915_perf_open_ioctl() with the &gt->perf.lock mutex
  3241. * taken to serialize with any non-file-operation driver hooks.
  3242. *
  3243. * Note: at this point the @props have only been validated in isolation and
  3244. * it's still necessary to validate that the combination of properties makes
  3245. * sense.
  3246. *
  3247. * In the case where userspace is interested in OA unit metrics then further
  3248. * config validation and stream initialization details will be handled by
  3249. * i915_oa_stream_init(). The code here should only validate config state that
  3250. * will be relevant to all stream types / backends.
  3251. *
  3252. * Returns: zero on success or a negative error code.
  3253. */
  3254. static int
  3255. i915_perf_open_ioctl_locked(struct i915_perf *perf,
  3256. struct drm_i915_perf_open_param *param,
  3257. struct perf_open_properties *props,
  3258. struct drm_file *file)
  3259. {
  3260. struct i915_gem_context *specific_ctx = NULL;
  3261. struct i915_perf_stream *stream = NULL;
  3262. unsigned long f_flags = 0;
  3263. bool privileged_op = true;
  3264. int stream_fd;
  3265. int ret;
  3266. if (props->single_context) {
  3267. u32 ctx_handle = props->ctx_handle;
  3268. struct drm_i915_file_private *file_priv = file->driver_priv;
  3269. specific_ctx = i915_gem_context_lookup(file_priv, ctx_handle);
  3270. if (IS_ERR(specific_ctx)) {
  3271. drm_dbg(&perf->i915->drm,
  3272. "Failed to look up context with ID %u for opening perf stream\n",
  3273. ctx_handle);
  3274. ret = PTR_ERR(specific_ctx);
  3275. goto err;
  3276. }
  3277. }
  3278. /*
  3279. * On Haswell the OA unit supports clock gating off for a specific
  3280. * context and in this mode there's no visibility of metrics for the
  3281. * rest of the system, which we consider acceptable for a
  3282. * non-privileged client.
  3283. *
  3284. * For Gen8->11 the OA unit no longer supports clock gating off for a
  3285. * specific context and the kernel can't securely stop the counters
  3286. * from updating as system-wide / global values. Even though we can
  3287. * filter reports based on the included context ID we can't block
  3288. * clients from seeing the raw / global counter values via
  3289. * MI_REPORT_PERF_COUNT commands and so consider it a privileged op to
  3290. * enable the OA unit by default.
  3291. *
  3292. * For Gen12+ we gain a new OAR unit that only monitors the RCS on a
  3293. * per context basis. So we can relax requirements there if the user
  3294. * doesn't request global stream access (i.e. query based sampling
  3295. * using MI_RECORD_PERF_COUNT.
  3296. */
  3297. if (IS_HASWELL(perf->i915) && specific_ctx)
  3298. privileged_op = false;
  3299. else if (GRAPHICS_VER(perf->i915) == 12 && specific_ctx &&
  3300. (props->sample_flags & SAMPLE_OA_REPORT) == 0)
  3301. privileged_op = false;
  3302. if (props->hold_preemption) {
  3303. if (!props->single_context) {
  3304. drm_dbg(&perf->i915->drm,
  3305. "preemption disable with no context\n");
  3306. ret = -EINVAL;
  3307. goto err;
  3308. }
  3309. privileged_op = true;
  3310. }
  3311. /*
  3312. * Asking for SSEU configuration is a privileged operation.
  3313. */
  3314. if (props->has_sseu)
  3315. privileged_op = true;
  3316. else
  3317. get_default_sseu_config(&props->sseu, props->engine);
  3318. /* Similar to perf's kernel.perf_paranoid_cpu sysctl option
  3319. * we check a dev.i915.perf_stream_paranoid sysctl option
  3320. * to determine if it's ok to access system wide OA counters
  3321. * without CAP_PERFMON or CAP_SYS_ADMIN privileges.
  3322. */
  3323. if (privileged_op &&
  3324. i915_perf_stream_paranoid && !perfmon_capable()) {
  3325. drm_dbg(&perf->i915->drm,
  3326. "Insufficient privileges to open i915 perf stream\n");
  3327. ret = -EACCES;
  3328. goto err_ctx;
  3329. }
  3330. stream = kzalloc_obj(*stream);
  3331. if (!stream) {
  3332. ret = -ENOMEM;
  3333. goto err_ctx;
  3334. }
  3335. stream->perf = perf;
  3336. stream->ctx = specific_ctx;
  3337. stream->poll_oa_period = props->poll_oa_period;
  3338. ret = i915_oa_stream_init(stream, param, props);
  3339. if (ret)
  3340. goto err_alloc;
  3341. /* we avoid simply assigning stream->sample_flags = props->sample_flags
  3342. * to have _stream_init check the combination of sample flags more
  3343. * thoroughly, but still this is the expected result at this point.
  3344. */
  3345. if (WARN_ON(stream->sample_flags != props->sample_flags)) {
  3346. ret = -ENODEV;
  3347. goto err_flags;
  3348. }
  3349. if (param->flags & I915_PERF_FLAG_FD_CLOEXEC)
  3350. f_flags |= O_CLOEXEC;
  3351. if (param->flags & I915_PERF_FLAG_FD_NONBLOCK)
  3352. f_flags |= O_NONBLOCK;
  3353. stream_fd = anon_inode_getfd("[i915_perf]", &fops, stream, f_flags);
  3354. if (stream_fd < 0) {
  3355. ret = stream_fd;
  3356. goto err_flags;
  3357. }
  3358. if (!(param->flags & I915_PERF_FLAG_DISABLED))
  3359. i915_perf_enable_locked(stream);
  3360. /* Take a reference on the driver that will be kept with stream_fd
  3361. * until its release.
  3362. */
  3363. drm_dev_get(&perf->i915->drm);
  3364. return stream_fd;
  3365. err_flags:
  3366. if (stream->ops->destroy)
  3367. stream->ops->destroy(stream);
  3368. err_alloc:
  3369. kfree(stream);
  3370. err_ctx:
  3371. if (specific_ctx)
  3372. i915_gem_context_put(specific_ctx);
  3373. err:
  3374. return ret;
  3375. }
  3376. static u64 oa_exponent_to_ns(struct i915_perf *perf, int exponent)
  3377. {
  3378. u64 nom = (2ULL << exponent) * NSEC_PER_SEC;
  3379. u32 den = i915_perf_oa_timestamp_frequency(perf->i915);
  3380. return div_u64(nom + den - 1, den);
  3381. }
  3382. static __always_inline bool
  3383. oa_format_valid(struct i915_perf *perf, enum drm_i915_oa_format format)
  3384. {
  3385. return test_bit(format, perf->format_mask);
  3386. }
  3387. static __always_inline void
  3388. oa_format_add(struct i915_perf *perf, enum drm_i915_oa_format format)
  3389. {
  3390. __set_bit(format, perf->format_mask);
  3391. }
  3392. /**
  3393. * read_properties_unlocked - validate + copy userspace stream open properties
  3394. * @perf: i915 perf instance
  3395. * @uprops: The array of u64 key value pairs given by userspace
  3396. * @n_props: The number of key value pairs expected in @uprops
  3397. * @props: The stream configuration built up while validating properties
  3398. *
  3399. * Note this function only validates properties in isolation it doesn't
  3400. * validate that the combination of properties makes sense or that all
  3401. * properties necessary for a particular kind of stream have been set.
  3402. *
  3403. * Note that there currently aren't any ordering requirements for properties so
  3404. * we shouldn't validate or assume anything about ordering here. This doesn't
  3405. * rule out defining new properties with ordering requirements in the future.
  3406. */
  3407. static int read_properties_unlocked(struct i915_perf *perf,
  3408. u64 __user *uprops,
  3409. u32 n_props,
  3410. struct perf_open_properties *props)
  3411. {
  3412. struct drm_i915_gem_context_param_sseu user_sseu;
  3413. const struct i915_oa_format *f;
  3414. u64 __user *uprop = uprops;
  3415. bool config_instance = false;
  3416. bool config_class = false;
  3417. bool config_sseu = false;
  3418. u8 class, instance;
  3419. u32 i;
  3420. int ret;
  3421. memset(props, 0, sizeof(struct perf_open_properties));
  3422. props->poll_oa_period = DEFAULT_POLL_PERIOD_NS;
  3423. /* Considering that ID = 0 is reserved and assuming that we don't
  3424. * (currently) expect any configurations to ever specify duplicate
  3425. * values for a particular property ID then the last _PROP_MAX value is
  3426. * one greater than the maximum number of properties we expect to get
  3427. * from userspace.
  3428. */
  3429. if (!n_props || n_props >= DRM_I915_PERF_PROP_MAX) {
  3430. drm_dbg(&perf->i915->drm,
  3431. "Invalid number of i915 perf properties given\n");
  3432. return -EINVAL;
  3433. }
  3434. /* Defaults when class:instance is not passed */
  3435. class = I915_ENGINE_CLASS_RENDER;
  3436. instance = 0;
  3437. for (i = 0; i < n_props; i++) {
  3438. u64 oa_period, oa_freq_hz;
  3439. u64 id, value;
  3440. ret = get_user(id, uprop);
  3441. if (ret)
  3442. return ret;
  3443. ret = get_user(value, uprop + 1);
  3444. if (ret)
  3445. return ret;
  3446. if (id == 0 || id >= DRM_I915_PERF_PROP_MAX) {
  3447. drm_dbg(&perf->i915->drm,
  3448. "Unknown i915 perf property ID\n");
  3449. return -EINVAL;
  3450. }
  3451. switch ((enum drm_i915_perf_property_id)id) {
  3452. case DRM_I915_PERF_PROP_CTX_HANDLE:
  3453. props->single_context = 1;
  3454. props->ctx_handle = value;
  3455. break;
  3456. case DRM_I915_PERF_PROP_SAMPLE_OA:
  3457. if (value)
  3458. props->sample_flags |= SAMPLE_OA_REPORT;
  3459. break;
  3460. case DRM_I915_PERF_PROP_OA_METRICS_SET:
  3461. if (value == 0) {
  3462. drm_dbg(&perf->i915->drm,
  3463. "Unknown OA metric set ID\n");
  3464. return -EINVAL;
  3465. }
  3466. props->metrics_set = value;
  3467. break;
  3468. case DRM_I915_PERF_PROP_OA_FORMAT:
  3469. if (value == 0 || value >= I915_OA_FORMAT_MAX) {
  3470. drm_dbg(&perf->i915->drm,
  3471. "Out-of-range OA report format %llu\n",
  3472. value);
  3473. return -EINVAL;
  3474. }
  3475. if (!oa_format_valid(perf, value)) {
  3476. drm_dbg(&perf->i915->drm,
  3477. "Unsupported OA report format %llu\n",
  3478. value);
  3479. return -EINVAL;
  3480. }
  3481. props->oa_format = value;
  3482. break;
  3483. case DRM_I915_PERF_PROP_OA_EXPONENT:
  3484. if (value > OA_EXPONENT_MAX) {
  3485. drm_dbg(&perf->i915->drm,
  3486. "OA timer exponent too high (> %u)\n",
  3487. OA_EXPONENT_MAX);
  3488. return -EINVAL;
  3489. }
  3490. /* Theoretically we can program the OA unit to sample
  3491. * e.g. every 160ns for HSW, 167ns for BDW/SKL or 104ns
  3492. * for BXT. We don't allow such high sampling
  3493. * frequencies by default unless root.
  3494. */
  3495. BUILD_BUG_ON(sizeof(oa_period) != 8);
  3496. oa_period = oa_exponent_to_ns(perf, value);
  3497. /* This check is primarily to ensure that oa_period <=
  3498. * UINT32_MAX (before passing to do_div which only
  3499. * accepts a u32 denominator), but we can also skip
  3500. * checking anything < 1Hz which implicitly can't be
  3501. * limited via an integer oa_max_sample_rate.
  3502. */
  3503. if (oa_period <= NSEC_PER_SEC) {
  3504. u64 tmp = NSEC_PER_SEC;
  3505. do_div(tmp, oa_period);
  3506. oa_freq_hz = tmp;
  3507. } else
  3508. oa_freq_hz = 0;
  3509. if (oa_freq_hz > i915_oa_max_sample_rate && !perfmon_capable()) {
  3510. drm_dbg(&perf->i915->drm,
  3511. "OA exponent would exceed the max sampling frequency (sysctl dev.i915.oa_max_sample_rate) %uHz without CAP_PERFMON or CAP_SYS_ADMIN privileges\n",
  3512. i915_oa_max_sample_rate);
  3513. return -EACCES;
  3514. }
  3515. props->oa_periodic = true;
  3516. props->oa_period_exponent = value;
  3517. break;
  3518. case DRM_I915_PERF_PROP_HOLD_PREEMPTION:
  3519. props->hold_preemption = !!value;
  3520. break;
  3521. case DRM_I915_PERF_PROP_GLOBAL_SSEU: {
  3522. if (GRAPHICS_VER_FULL(perf->i915) >= IP_VER(12, 55)) {
  3523. drm_dbg(&perf->i915->drm,
  3524. "SSEU config not supported on gfx %x\n",
  3525. GRAPHICS_VER_FULL(perf->i915));
  3526. return -ENODEV;
  3527. }
  3528. if (copy_from_user(&user_sseu,
  3529. u64_to_user_ptr(value),
  3530. sizeof(user_sseu))) {
  3531. drm_dbg(&perf->i915->drm,
  3532. "Unable to copy global sseu parameter\n");
  3533. return -EFAULT;
  3534. }
  3535. config_sseu = true;
  3536. break;
  3537. }
  3538. case DRM_I915_PERF_PROP_POLL_OA_PERIOD:
  3539. if (value < 100000 /* 100us */) {
  3540. drm_dbg(&perf->i915->drm,
  3541. "OA availability timer too small (%lluns < 100us)\n",
  3542. value);
  3543. return -EINVAL;
  3544. }
  3545. props->poll_oa_period = value;
  3546. break;
  3547. case DRM_I915_PERF_PROP_OA_ENGINE_CLASS:
  3548. class = (u8)value;
  3549. config_class = true;
  3550. break;
  3551. case DRM_I915_PERF_PROP_OA_ENGINE_INSTANCE:
  3552. instance = (u8)value;
  3553. config_instance = true;
  3554. break;
  3555. default:
  3556. MISSING_CASE(id);
  3557. return -EINVAL;
  3558. }
  3559. uprop += 2;
  3560. }
  3561. if ((config_class && !config_instance) ||
  3562. (config_instance && !config_class)) {
  3563. drm_dbg(&perf->i915->drm,
  3564. "OA engine-class and engine-instance parameters must be passed together\n");
  3565. return -EINVAL;
  3566. }
  3567. props->engine = intel_engine_lookup_user(perf->i915, class, instance);
  3568. if (!props->engine) {
  3569. drm_dbg(&perf->i915->drm,
  3570. "OA engine class and instance invalid %d:%d\n",
  3571. class, instance);
  3572. return -EINVAL;
  3573. }
  3574. if (!engine_supports_oa(props->engine)) {
  3575. drm_dbg(&perf->i915->drm,
  3576. "Engine not supported by OA %d:%d\n",
  3577. class, instance);
  3578. return -EINVAL;
  3579. }
  3580. /*
  3581. * Wa_14017512683: mtl[a0..c0): Use of OAM must be preceded with Media
  3582. * C6 disable in BIOS. Fail if Media C6 is enabled on steppings where OAM
  3583. * does not work as expected.
  3584. */
  3585. if (IS_MEDIA_GT_IP_STEP(props->engine->gt, IP_VER(13, 0), STEP_A0, STEP_C0) &&
  3586. props->engine->oa_group->type == TYPE_OAM &&
  3587. intel_check_bios_c6_setup(&props->engine->gt->rc6)) {
  3588. drm_dbg(&perf->i915->drm,
  3589. "OAM requires media C6 to be disabled in BIOS\n");
  3590. return -EINVAL;
  3591. }
  3592. i = array_index_nospec(props->oa_format, I915_OA_FORMAT_MAX);
  3593. f = &perf->oa_formats[i];
  3594. if (!engine_supports_oa_format(props->engine, f->type)) {
  3595. drm_dbg(&perf->i915->drm,
  3596. "Invalid OA format %d for class %d\n",
  3597. f->type, props->engine->class);
  3598. return -EINVAL;
  3599. }
  3600. if (config_sseu) {
  3601. ret = get_sseu_config(&props->sseu, props->engine, &user_sseu);
  3602. if (ret) {
  3603. drm_dbg(&perf->i915->drm,
  3604. "Invalid SSEU configuration\n");
  3605. return ret;
  3606. }
  3607. props->has_sseu = true;
  3608. }
  3609. return 0;
  3610. }
  3611. /**
  3612. * i915_perf_open_ioctl - DRM ioctl() for userspace to open a stream FD
  3613. * @dev: drm device
  3614. * @data: ioctl data copied from userspace (unvalidated)
  3615. * @file: drm file
  3616. *
  3617. * Validates the stream open parameters given by userspace including flags
  3618. * and an array of u64 key, value pair properties.
  3619. *
  3620. * Very little is assumed up front about the nature of the stream being
  3621. * opened (for instance we don't assume it's for periodic OA unit metrics). An
  3622. * i915-perf stream is expected to be a suitable interface for other forms of
  3623. * buffered data written by the GPU besides periodic OA metrics.
  3624. *
  3625. * Note we copy the properties from userspace outside of the i915 perf
  3626. * mutex to avoid an awkward lockdep with mmap_lock.
  3627. *
  3628. * Most of the implementation details are handled by
  3629. * i915_perf_open_ioctl_locked() after taking the &gt->perf.lock
  3630. * mutex for serializing with any non-file-operation driver hooks.
  3631. *
  3632. * Return: A newly opened i915 Perf stream file descriptor or negative
  3633. * error code on failure.
  3634. */
  3635. int i915_perf_open_ioctl(struct drm_device *dev, void *data,
  3636. struct drm_file *file)
  3637. {
  3638. struct i915_perf *perf = &to_i915(dev)->perf;
  3639. struct drm_i915_perf_open_param *param = data;
  3640. struct intel_gt *gt;
  3641. struct perf_open_properties props;
  3642. u32 known_open_flags;
  3643. int ret;
  3644. if (!perf->i915)
  3645. return -ENOTSUPP;
  3646. known_open_flags = I915_PERF_FLAG_FD_CLOEXEC |
  3647. I915_PERF_FLAG_FD_NONBLOCK |
  3648. I915_PERF_FLAG_DISABLED;
  3649. if (param->flags & ~known_open_flags) {
  3650. drm_dbg(&perf->i915->drm,
  3651. "Unknown drm_i915_perf_open_param flag\n");
  3652. return -EINVAL;
  3653. }
  3654. ret = read_properties_unlocked(perf,
  3655. u64_to_user_ptr(param->properties_ptr),
  3656. param->num_properties,
  3657. &props);
  3658. if (ret)
  3659. return ret;
  3660. gt = props.engine->gt;
  3661. mutex_lock(&gt->perf.lock);
  3662. ret = i915_perf_open_ioctl_locked(perf, param, &props, file);
  3663. mutex_unlock(&gt->perf.lock);
  3664. return ret;
  3665. }
  3666. /**
  3667. * i915_perf_register - exposes i915-perf to userspace
  3668. * @i915: i915 device instance
  3669. *
  3670. * In particular OA metric sets are advertised under a sysfs metrics/
  3671. * directory allowing userspace to enumerate valid IDs that can be
  3672. * used to open an i915-perf stream.
  3673. */
  3674. void i915_perf_register(struct drm_i915_private *i915)
  3675. {
  3676. struct i915_perf *perf = &i915->perf;
  3677. struct intel_gt *gt = to_gt(i915);
  3678. if (!perf->i915)
  3679. return;
  3680. /* To be sure we're synchronized with an attempted
  3681. * i915_perf_open_ioctl(); considering that we register after
  3682. * being exposed to userspace.
  3683. */
  3684. mutex_lock(&gt->perf.lock);
  3685. perf->metrics_kobj =
  3686. kobject_create_and_add("metrics",
  3687. &i915->drm.primary->kdev->kobj);
  3688. mutex_unlock(&gt->perf.lock);
  3689. }
  3690. /**
  3691. * i915_perf_unregister - hide i915-perf from userspace
  3692. * @i915: i915 device instance
  3693. *
  3694. * i915-perf state cleanup is split up into an 'unregister' and
  3695. * 'deinit' phase where the interface is first hidden from
  3696. * userspace by i915_perf_unregister() before cleaning up
  3697. * remaining state in i915_perf_fini().
  3698. */
  3699. void i915_perf_unregister(struct drm_i915_private *i915)
  3700. {
  3701. struct i915_perf *perf = &i915->perf;
  3702. if (!perf->metrics_kobj)
  3703. return;
  3704. kobject_put(perf->metrics_kobj);
  3705. perf->metrics_kobj = NULL;
  3706. }
  3707. static bool gen8_is_valid_flex_addr(struct i915_perf *perf, u32 addr)
  3708. {
  3709. static const i915_reg_t flex_eu_regs[] = {
  3710. EU_PERF_CNTL0,
  3711. EU_PERF_CNTL1,
  3712. EU_PERF_CNTL2,
  3713. EU_PERF_CNTL3,
  3714. EU_PERF_CNTL4,
  3715. EU_PERF_CNTL5,
  3716. EU_PERF_CNTL6,
  3717. };
  3718. int i;
  3719. for (i = 0; i < ARRAY_SIZE(flex_eu_regs); i++) {
  3720. if (i915_mmio_reg_offset(flex_eu_regs[i]) == addr)
  3721. return true;
  3722. }
  3723. return false;
  3724. }
  3725. #define REG_EQUAL(addr, mmio) \
  3726. ((addr) == i915_mmio_reg_offset(mmio))
  3727. static const struct i915_mmio_range gen7_oa_b_counters[] = {
  3728. { .start = 0x2710, .end = 0x272c }, /* OASTARTTRIG[1-8] */
  3729. { .start = 0x2740, .end = 0x275c }, /* OAREPORTTRIG[1-8] */
  3730. { .start = 0x2770, .end = 0x27ac }, /* OACEC[0-7][0-1] */
  3731. {}
  3732. };
  3733. static const struct i915_mmio_range gen12_oa_b_counters[] = {
  3734. { .start = 0x2b2c, .end = 0x2b2c }, /* GEN12_OAG_OA_PESS */
  3735. { .start = 0xd900, .end = 0xd91c }, /* GEN12_OAG_OASTARTTRIG[1-8] */
  3736. { .start = 0xd920, .end = 0xd93c }, /* GEN12_OAG_OAREPORTTRIG1[1-8] */
  3737. { .start = 0xd940, .end = 0xd97c }, /* GEN12_OAG_CEC[0-7][0-1] */
  3738. { .start = 0xdc00, .end = 0xdc3c }, /* GEN12_OAG_SCEC[0-7][0-1] */
  3739. { .start = 0xdc40, .end = 0xdc40 }, /* GEN12_OAG_SPCTR_CNF */
  3740. { .start = 0xdc44, .end = 0xdc44 }, /* GEN12_OAA_DBG_REG */
  3741. {}
  3742. };
  3743. static const struct i915_mmio_range mtl_oam_b_counters[] = {
  3744. { .start = 0x393000, .end = 0x39301c }, /* GEN12_OAM_STARTTRIG1[1-8] */
  3745. { .start = 0x393020, .end = 0x39303c }, /* GEN12_OAM_REPORTTRIG1[1-8] */
  3746. { .start = 0x393040, .end = 0x39307c }, /* GEN12_OAM_CEC[0-7][0-1] */
  3747. { .start = 0x393200, .end = 0x39323C }, /* MPES[0-7] */
  3748. {}
  3749. };
  3750. static const struct i915_mmio_range xehp_oa_b_counters[] = {
  3751. { .start = 0xdc48, .end = 0xdc48 }, /* OAA_ENABLE_REG */
  3752. { .start = 0xdd00, .end = 0xdd48 }, /* OAG_LCE0_0 - OAA_LENABLE_REG */
  3753. {}
  3754. };
  3755. static const struct i915_mmio_range gen7_oa_mux_regs[] = {
  3756. { .start = 0x91b8, .end = 0x91cc }, /* OA_PERFCNT[1-2], OA_PERFMATRIX */
  3757. { .start = 0x9800, .end = 0x9888 }, /* MICRO_BP0_0 - NOA_WRITE */
  3758. { .start = 0xe180, .end = 0xe180 }, /* HALF_SLICE_CHICKEN2 */
  3759. {}
  3760. };
  3761. static const struct i915_mmio_range hsw_oa_mux_regs[] = {
  3762. { .start = 0x09e80, .end = 0x09ea4 }, /* HSW_MBVID2_NOA[0-9] */
  3763. { .start = 0x09ec0, .end = 0x09ec0 }, /* HSW_MBVID2_MISR0 */
  3764. { .start = 0x25100, .end = 0x2ff90 },
  3765. {}
  3766. };
  3767. static const struct i915_mmio_range chv_oa_mux_regs[] = {
  3768. { .start = 0x182300, .end = 0x1823a4 },
  3769. {}
  3770. };
  3771. static const struct i915_mmio_range gen8_oa_mux_regs[] = {
  3772. { .start = 0x0d00, .end = 0x0d2c }, /* RPM_CONFIG[0-1], NOA_CONFIG[0-8] */
  3773. { .start = 0x20cc, .end = 0x20cc }, /* WAIT_FOR_RC6_EXIT */
  3774. {}
  3775. };
  3776. static const struct i915_mmio_range gen11_oa_mux_regs[] = {
  3777. { .start = 0x91c8, .end = 0x91dc }, /* OA_PERFCNT[3-4] */
  3778. {}
  3779. };
  3780. static const struct i915_mmio_range gen12_oa_mux_regs[] = {
  3781. { .start = 0x0d00, .end = 0x0d04 }, /* RPM_CONFIG[0-1] */
  3782. { .start = 0x0d0c, .end = 0x0d2c }, /* NOA_CONFIG[0-8] */
  3783. { .start = 0x9840, .end = 0x9840 }, /* GDT_CHICKEN_BITS */
  3784. { .start = 0x9884, .end = 0x9888 }, /* NOA_WRITE */
  3785. { .start = 0x20cc, .end = 0x20cc }, /* WAIT_FOR_RC6_EXIT */
  3786. {}
  3787. };
  3788. /*
  3789. * Ref: 14010536224:
  3790. * 0x20cc is repurposed on MTL, so use a separate array for MTL.
  3791. */
  3792. static const struct i915_mmio_range mtl_oa_mux_regs[] = {
  3793. { .start = 0x0d00, .end = 0x0d04 }, /* RPM_CONFIG[0-1] */
  3794. { .start = 0x0d0c, .end = 0x0d2c }, /* NOA_CONFIG[0-8] */
  3795. { .start = 0x9840, .end = 0x9840 }, /* GDT_CHICKEN_BITS */
  3796. { .start = 0x9884, .end = 0x9888 }, /* NOA_WRITE */
  3797. { .start = 0x38d100, .end = 0x38d114}, /* VISACTL */
  3798. {}
  3799. };
  3800. static bool gen7_is_valid_b_counter_addr(struct i915_perf *perf, u32 addr)
  3801. {
  3802. return i915_mmio_range_table_contains(addr, gen7_oa_b_counters);
  3803. }
  3804. static bool gen8_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
  3805. {
  3806. return i915_mmio_range_table_contains(addr, gen7_oa_mux_regs) ||
  3807. i915_mmio_range_table_contains(addr, gen8_oa_mux_regs);
  3808. }
  3809. static bool gen11_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
  3810. {
  3811. return i915_mmio_range_table_contains(addr, gen7_oa_mux_regs) ||
  3812. i915_mmio_range_table_contains(addr, gen8_oa_mux_regs) ||
  3813. i915_mmio_range_table_contains(addr, gen11_oa_mux_regs);
  3814. }
  3815. static bool hsw_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
  3816. {
  3817. return i915_mmio_range_table_contains(addr, gen7_oa_mux_regs) ||
  3818. i915_mmio_range_table_contains(addr, hsw_oa_mux_regs);
  3819. }
  3820. static bool chv_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
  3821. {
  3822. return i915_mmio_range_table_contains(addr, gen7_oa_mux_regs) ||
  3823. i915_mmio_range_table_contains(addr, chv_oa_mux_regs);
  3824. }
  3825. static bool gen12_is_valid_b_counter_addr(struct i915_perf *perf, u32 addr)
  3826. {
  3827. return i915_mmio_range_table_contains(addr, gen12_oa_b_counters);
  3828. }
  3829. static bool mtl_is_valid_oam_b_counter_addr(struct i915_perf *perf, u32 addr)
  3830. {
  3831. if (HAS_OAM(perf->i915) &&
  3832. GRAPHICS_VER_FULL(perf->i915) >= IP_VER(12, 70))
  3833. return i915_mmio_range_table_contains(addr, mtl_oam_b_counters);
  3834. return false;
  3835. }
  3836. static bool xehp_is_valid_b_counter_addr(struct i915_perf *perf, u32 addr)
  3837. {
  3838. return i915_mmio_range_table_contains(addr, xehp_oa_b_counters) ||
  3839. i915_mmio_range_table_contains(addr, gen12_oa_b_counters) ||
  3840. mtl_is_valid_oam_b_counter_addr(perf, addr);
  3841. }
  3842. static bool gen12_is_valid_mux_addr(struct i915_perf *perf, u32 addr)
  3843. {
  3844. if (GRAPHICS_VER_FULL(perf->i915) >= IP_VER(12, 70))
  3845. return i915_mmio_range_table_contains(addr, mtl_oa_mux_regs);
  3846. else
  3847. return i915_mmio_range_table_contains(addr, gen12_oa_mux_regs);
  3848. }
  3849. static u32 mask_reg_value(u32 reg, u32 val)
  3850. {
  3851. /*
  3852. * HALF_SLICE_CHICKEN2 is programmed with a the
  3853. * WaDisableSTUnitPowerOptimization workaround. Make sure the value
  3854. * programmed by userspace doesn't change this.
  3855. */
  3856. if (REG_EQUAL(reg, HALF_SLICE_CHICKEN2))
  3857. val = val & ~_MASKED_BIT_ENABLE(GEN8_ST_PO_DISABLE);
  3858. /*
  3859. * WAIT_FOR_RC6_EXIT has only one bit fulfilling the function
  3860. * indicated by its name and a bunch of selection fields used by OA
  3861. * configs.
  3862. */
  3863. if (REG_EQUAL(reg, WAIT_FOR_RC6_EXIT))
  3864. val = val & ~_MASKED_BIT_ENABLE(HSW_WAIT_FOR_RC6_EXIT_ENABLE);
  3865. return val;
  3866. }
  3867. static struct i915_oa_reg *alloc_oa_regs(struct i915_perf *perf,
  3868. bool (*is_valid)(struct i915_perf *perf, u32 addr),
  3869. u32 __user *regs,
  3870. u32 n_regs)
  3871. {
  3872. struct i915_oa_reg *oa_regs;
  3873. int err;
  3874. u32 i;
  3875. if (!n_regs)
  3876. return NULL;
  3877. /* No is_valid function means we're not allowing any register to be programmed. */
  3878. GEM_BUG_ON(!is_valid);
  3879. if (!is_valid)
  3880. return ERR_PTR(-EINVAL);
  3881. oa_regs = kmalloc_objs(*oa_regs, n_regs);
  3882. if (!oa_regs)
  3883. return ERR_PTR(-ENOMEM);
  3884. for (i = 0; i < n_regs; i++) {
  3885. u32 addr, value;
  3886. err = get_user(addr, regs);
  3887. if (err)
  3888. goto addr_err;
  3889. if (!is_valid(perf, addr)) {
  3890. drm_dbg(&perf->i915->drm,
  3891. "Invalid oa_reg address: %X\n", addr);
  3892. err = -EINVAL;
  3893. goto addr_err;
  3894. }
  3895. err = get_user(value, regs + 1);
  3896. if (err)
  3897. goto addr_err;
  3898. oa_regs[i].addr = _MMIO(addr);
  3899. oa_regs[i].value = mask_reg_value(addr, value);
  3900. regs += 2;
  3901. }
  3902. return oa_regs;
  3903. addr_err:
  3904. kfree(oa_regs);
  3905. return ERR_PTR(err);
  3906. }
  3907. static ssize_t show_dynamic_id(struct kobject *kobj,
  3908. struct kobj_attribute *attr,
  3909. char *buf)
  3910. {
  3911. struct i915_oa_config *oa_config =
  3912. container_of(attr, typeof(*oa_config), sysfs_metric_id);
  3913. return sprintf(buf, "%d\n", oa_config->id);
  3914. }
  3915. static int create_dynamic_oa_sysfs_entry(struct i915_perf *perf,
  3916. struct i915_oa_config *oa_config)
  3917. {
  3918. sysfs_attr_init(&oa_config->sysfs_metric_id.attr);
  3919. oa_config->sysfs_metric_id.attr.name = "id";
  3920. oa_config->sysfs_metric_id.attr.mode = S_IRUGO;
  3921. oa_config->sysfs_metric_id.show = show_dynamic_id;
  3922. oa_config->sysfs_metric_id.store = NULL;
  3923. oa_config->attrs[0] = &oa_config->sysfs_metric_id.attr;
  3924. oa_config->attrs[1] = NULL;
  3925. oa_config->sysfs_metric.name = oa_config->uuid;
  3926. oa_config->sysfs_metric.attrs = oa_config->attrs;
  3927. return sysfs_create_group(perf->metrics_kobj,
  3928. &oa_config->sysfs_metric);
  3929. }
  3930. /**
  3931. * i915_perf_add_config_ioctl - DRM ioctl() for userspace to add a new OA config
  3932. * @dev: drm device
  3933. * @data: ioctl data (pointer to struct drm_i915_perf_oa_config) copied from
  3934. * userspace (unvalidated)
  3935. * @file: drm file
  3936. *
  3937. * Validates the submitted OA register to be saved into a new OA config that
  3938. * can then be used for programming the OA unit and its NOA network.
  3939. *
  3940. * Returns: A new allocated config number to be used with the perf open ioctl
  3941. * or a negative error code on failure.
  3942. */
  3943. int i915_perf_add_config_ioctl(struct drm_device *dev, void *data,
  3944. struct drm_file *file)
  3945. {
  3946. struct i915_perf *perf = &to_i915(dev)->perf;
  3947. struct drm_i915_perf_oa_config *args = data;
  3948. struct i915_oa_config *oa_config, *tmp;
  3949. struct i915_oa_reg *regs;
  3950. int err, id;
  3951. if (!perf->i915)
  3952. return -ENOTSUPP;
  3953. if (!perf->metrics_kobj) {
  3954. drm_dbg(&perf->i915->drm,
  3955. "OA metrics weren't advertised via sysfs\n");
  3956. return -EINVAL;
  3957. }
  3958. if (i915_perf_stream_paranoid && !perfmon_capable()) {
  3959. drm_dbg(&perf->i915->drm,
  3960. "Insufficient privileges to add i915 OA config\n");
  3961. return -EACCES;
  3962. }
  3963. if ((!args->mux_regs_ptr || !args->n_mux_regs) &&
  3964. (!args->boolean_regs_ptr || !args->n_boolean_regs) &&
  3965. (!args->flex_regs_ptr || !args->n_flex_regs)) {
  3966. drm_dbg(&perf->i915->drm,
  3967. "No OA registers given\n");
  3968. return -EINVAL;
  3969. }
  3970. oa_config = kzalloc_obj(*oa_config);
  3971. if (!oa_config) {
  3972. drm_dbg(&perf->i915->drm,
  3973. "Failed to allocate memory for the OA config\n");
  3974. return -ENOMEM;
  3975. }
  3976. oa_config->perf = perf;
  3977. kref_init(&oa_config->ref);
  3978. if (!uuid_is_valid(args->uuid)) {
  3979. drm_dbg(&perf->i915->drm,
  3980. "Invalid uuid format for OA config\n");
  3981. err = -EINVAL;
  3982. goto reg_err;
  3983. }
  3984. /* Last character in oa_config->uuid will be 0 because oa_config is
  3985. * kzalloc.
  3986. */
  3987. memcpy(oa_config->uuid, args->uuid, sizeof(args->uuid));
  3988. oa_config->mux_regs_len = args->n_mux_regs;
  3989. regs = alloc_oa_regs(perf,
  3990. perf->ops.is_valid_mux_reg,
  3991. u64_to_user_ptr(args->mux_regs_ptr),
  3992. args->n_mux_regs);
  3993. if (IS_ERR(regs)) {
  3994. drm_dbg(&perf->i915->drm,
  3995. "Failed to create OA config for mux_regs\n");
  3996. err = PTR_ERR(regs);
  3997. goto reg_err;
  3998. }
  3999. oa_config->mux_regs = regs;
  4000. oa_config->b_counter_regs_len = args->n_boolean_regs;
  4001. regs = alloc_oa_regs(perf,
  4002. perf->ops.is_valid_b_counter_reg,
  4003. u64_to_user_ptr(args->boolean_regs_ptr),
  4004. args->n_boolean_regs);
  4005. if (IS_ERR(regs)) {
  4006. drm_dbg(&perf->i915->drm,
  4007. "Failed to create OA config for b_counter_regs\n");
  4008. err = PTR_ERR(regs);
  4009. goto reg_err;
  4010. }
  4011. oa_config->b_counter_regs = regs;
  4012. if (GRAPHICS_VER(perf->i915) < 8) {
  4013. if (args->n_flex_regs != 0) {
  4014. err = -EINVAL;
  4015. goto reg_err;
  4016. }
  4017. } else {
  4018. oa_config->flex_regs_len = args->n_flex_regs;
  4019. regs = alloc_oa_regs(perf,
  4020. perf->ops.is_valid_flex_reg,
  4021. u64_to_user_ptr(args->flex_regs_ptr),
  4022. args->n_flex_regs);
  4023. if (IS_ERR(regs)) {
  4024. drm_dbg(&perf->i915->drm,
  4025. "Failed to create OA config for flex_regs\n");
  4026. err = PTR_ERR(regs);
  4027. goto reg_err;
  4028. }
  4029. oa_config->flex_regs = regs;
  4030. }
  4031. err = mutex_lock_interruptible(&perf->metrics_lock);
  4032. if (err)
  4033. goto reg_err;
  4034. /* We shouldn't have too many configs, so this iteration shouldn't be
  4035. * too costly.
  4036. */
  4037. idr_for_each_entry(&perf->metrics_idr, tmp, id) {
  4038. if (!strcmp(tmp->uuid, oa_config->uuid)) {
  4039. drm_dbg(&perf->i915->drm,
  4040. "OA config already exists with this uuid\n");
  4041. err = -EADDRINUSE;
  4042. goto sysfs_err;
  4043. }
  4044. }
  4045. err = create_dynamic_oa_sysfs_entry(perf, oa_config);
  4046. if (err) {
  4047. drm_dbg(&perf->i915->drm,
  4048. "Failed to create sysfs entry for OA config\n");
  4049. goto sysfs_err;
  4050. }
  4051. /* Config id 0 is invalid, id 1 for kernel stored test config. */
  4052. oa_config->id = idr_alloc(&perf->metrics_idr,
  4053. oa_config, 2,
  4054. 0, GFP_KERNEL);
  4055. if (oa_config->id < 0) {
  4056. drm_dbg(&perf->i915->drm,
  4057. "Failed to create sysfs entry for OA config\n");
  4058. err = oa_config->id;
  4059. goto sysfs_err;
  4060. }
  4061. id = oa_config->id;
  4062. drm_dbg(&perf->i915->drm,
  4063. "Added config %s id=%i\n", oa_config->uuid, oa_config->id);
  4064. mutex_unlock(&perf->metrics_lock);
  4065. return id;
  4066. sysfs_err:
  4067. mutex_unlock(&perf->metrics_lock);
  4068. reg_err:
  4069. i915_oa_config_put(oa_config);
  4070. drm_dbg(&perf->i915->drm,
  4071. "Failed to add new OA config\n");
  4072. return err;
  4073. }
  4074. /**
  4075. * i915_perf_remove_config_ioctl - DRM ioctl() for userspace to remove an OA config
  4076. * @dev: drm device
  4077. * @data: ioctl data (pointer to u64 integer) copied from userspace
  4078. * @file: drm file
  4079. *
  4080. * Configs can be removed while being used, the will stop appearing in sysfs
  4081. * and their content will be freed when the stream using the config is closed.
  4082. *
  4083. * Returns: 0 on success or a negative error code on failure.
  4084. */
  4085. int i915_perf_remove_config_ioctl(struct drm_device *dev, void *data,
  4086. struct drm_file *file)
  4087. {
  4088. struct i915_perf *perf = &to_i915(dev)->perf;
  4089. u64 *arg = data;
  4090. struct i915_oa_config *oa_config;
  4091. int ret;
  4092. if (!perf->i915)
  4093. return -ENOTSUPP;
  4094. if (i915_perf_stream_paranoid && !perfmon_capable()) {
  4095. drm_dbg(&perf->i915->drm,
  4096. "Insufficient privileges to remove i915 OA config\n");
  4097. return -EACCES;
  4098. }
  4099. ret = mutex_lock_interruptible(&perf->metrics_lock);
  4100. if (ret)
  4101. return ret;
  4102. oa_config = idr_find(&perf->metrics_idr, *arg);
  4103. if (!oa_config) {
  4104. drm_dbg(&perf->i915->drm,
  4105. "Failed to remove unknown OA config\n");
  4106. ret = -ENOENT;
  4107. goto err_unlock;
  4108. }
  4109. GEM_BUG_ON(*arg != oa_config->id);
  4110. sysfs_remove_group(perf->metrics_kobj, &oa_config->sysfs_metric);
  4111. idr_remove(&perf->metrics_idr, *arg);
  4112. mutex_unlock(&perf->metrics_lock);
  4113. drm_dbg(&perf->i915->drm,
  4114. "Removed config %s id=%i\n", oa_config->uuid, oa_config->id);
  4115. i915_oa_config_put(oa_config);
  4116. return 0;
  4117. err_unlock:
  4118. mutex_unlock(&perf->metrics_lock);
  4119. return ret;
  4120. }
  4121. static const struct ctl_table oa_table[] = {
  4122. {
  4123. .procname = "perf_stream_paranoid",
  4124. .data = &i915_perf_stream_paranoid,
  4125. .maxlen = sizeof(i915_perf_stream_paranoid),
  4126. .mode = 0644,
  4127. .proc_handler = proc_dointvec_minmax,
  4128. .extra1 = SYSCTL_ZERO,
  4129. .extra2 = SYSCTL_ONE,
  4130. },
  4131. {
  4132. .procname = "oa_max_sample_rate",
  4133. .data = &i915_oa_max_sample_rate,
  4134. .maxlen = sizeof(i915_oa_max_sample_rate),
  4135. .mode = 0644,
  4136. .proc_handler = proc_dointvec_minmax,
  4137. .extra1 = SYSCTL_ZERO,
  4138. .extra2 = &oa_sample_rate_hard_limit,
  4139. },
  4140. };
  4141. static u32 num_perf_groups_per_gt(struct intel_gt *gt)
  4142. {
  4143. return 1;
  4144. }
  4145. static u32 __oam_engine_group(struct intel_engine_cs *engine)
  4146. {
  4147. if (GRAPHICS_VER_FULL(engine->i915) >= IP_VER(12, 70)) {
  4148. /*
  4149. * There's 1 SAMEDIA gt and 1 OAM per SAMEDIA gt. All media slices
  4150. * within the gt use the same OAM. All MTL SKUs list 1 SA MEDIA.
  4151. */
  4152. drm_WARN_ON(&engine->i915->drm,
  4153. engine->gt->type != GT_MEDIA);
  4154. return PERF_GROUP_OAM_SAMEDIA_0;
  4155. }
  4156. return PERF_GROUP_INVALID;
  4157. }
  4158. static u32 __oa_engine_group(struct intel_engine_cs *engine)
  4159. {
  4160. switch (engine->class) {
  4161. case RENDER_CLASS:
  4162. return PERF_GROUP_OAG;
  4163. case VIDEO_DECODE_CLASS:
  4164. case VIDEO_ENHANCEMENT_CLASS:
  4165. return __oam_engine_group(engine);
  4166. default:
  4167. return PERF_GROUP_INVALID;
  4168. }
  4169. }
  4170. static struct i915_perf_regs __oam_regs(u32 base)
  4171. {
  4172. return (struct i915_perf_regs) {
  4173. base,
  4174. GEN12_OAM_HEAD_POINTER(base),
  4175. GEN12_OAM_TAIL_POINTER(base),
  4176. GEN12_OAM_BUFFER(base),
  4177. GEN12_OAM_CONTEXT_CONTROL(base),
  4178. GEN12_OAM_CONTROL(base),
  4179. GEN12_OAM_DEBUG(base),
  4180. GEN12_OAM_STATUS(base),
  4181. GEN12_OAM_CONTROL_COUNTER_FORMAT_SHIFT,
  4182. };
  4183. }
  4184. static struct i915_perf_regs __oag_regs(void)
  4185. {
  4186. return (struct i915_perf_regs) {
  4187. 0,
  4188. GEN12_OAG_OAHEADPTR,
  4189. GEN12_OAG_OATAILPTR,
  4190. GEN12_OAG_OABUFFER,
  4191. GEN12_OAG_OAGLBCTXCTRL,
  4192. GEN12_OAG_OACONTROL,
  4193. GEN12_OAG_OA_DEBUG,
  4194. GEN12_OAG_OASTATUS,
  4195. GEN12_OAG_OACONTROL_OA_COUNTER_FORMAT_SHIFT,
  4196. };
  4197. }
  4198. static void oa_init_groups(struct intel_gt *gt)
  4199. {
  4200. int i, num_groups = gt->perf.num_perf_groups;
  4201. for (i = 0; i < num_groups; i++) {
  4202. struct i915_perf_group *g = &gt->perf.group[i];
  4203. /* Fused off engines can result in a group with num_engines == 0 */
  4204. if (g->num_engines == 0)
  4205. continue;
  4206. if (i == PERF_GROUP_OAG && gt->type != GT_MEDIA) {
  4207. g->regs = __oag_regs();
  4208. g->type = TYPE_OAG;
  4209. } else if (GRAPHICS_VER_FULL(gt->i915) >= IP_VER(12, 70)) {
  4210. g->regs = __oam_regs(mtl_oa_base[i]);
  4211. g->type = TYPE_OAM;
  4212. }
  4213. }
  4214. }
  4215. static int oa_init_gt(struct intel_gt *gt)
  4216. {
  4217. u32 num_groups = num_perf_groups_per_gt(gt);
  4218. struct intel_engine_cs *engine;
  4219. struct i915_perf_group *g;
  4220. intel_engine_mask_t tmp;
  4221. g = kzalloc_objs(*g, num_groups);
  4222. if (!g)
  4223. return -ENOMEM;
  4224. for_each_engine_masked(engine, gt, ALL_ENGINES, tmp) {
  4225. u32 index = __oa_engine_group(engine);
  4226. engine->oa_group = NULL;
  4227. if (index < num_groups) {
  4228. g[index].num_engines++;
  4229. engine->oa_group = &g[index];
  4230. }
  4231. }
  4232. gt->perf.num_perf_groups = num_groups;
  4233. gt->perf.group = g;
  4234. oa_init_groups(gt);
  4235. return 0;
  4236. }
  4237. static int oa_init_engine_groups(struct i915_perf *perf)
  4238. {
  4239. struct intel_gt *gt;
  4240. int i, ret;
  4241. for_each_gt(gt, perf->i915, i) {
  4242. ret = oa_init_gt(gt);
  4243. if (ret)
  4244. return ret;
  4245. }
  4246. return 0;
  4247. }
  4248. static void oa_init_supported_formats(struct i915_perf *perf)
  4249. {
  4250. struct drm_i915_private *i915 = perf->i915;
  4251. enum intel_platform platform = INTEL_INFO(i915)->platform;
  4252. switch (platform) {
  4253. case INTEL_HASWELL:
  4254. oa_format_add(perf, I915_OA_FORMAT_A13);
  4255. oa_format_add(perf, I915_OA_FORMAT_A13);
  4256. oa_format_add(perf, I915_OA_FORMAT_A29);
  4257. oa_format_add(perf, I915_OA_FORMAT_A13_B8_C8);
  4258. oa_format_add(perf, I915_OA_FORMAT_B4_C8);
  4259. oa_format_add(perf, I915_OA_FORMAT_A45_B8_C8);
  4260. oa_format_add(perf, I915_OA_FORMAT_B4_C8_A16);
  4261. oa_format_add(perf, I915_OA_FORMAT_C4_B8);
  4262. break;
  4263. case INTEL_BROADWELL:
  4264. case INTEL_CHERRYVIEW:
  4265. case INTEL_SKYLAKE:
  4266. case INTEL_BROXTON:
  4267. case INTEL_KABYLAKE:
  4268. case INTEL_GEMINILAKE:
  4269. case INTEL_COFFEELAKE:
  4270. case INTEL_COMETLAKE:
  4271. case INTEL_ICELAKE:
  4272. case INTEL_ELKHARTLAKE:
  4273. case INTEL_JASPERLAKE:
  4274. case INTEL_TIGERLAKE:
  4275. case INTEL_ROCKETLAKE:
  4276. case INTEL_DG1:
  4277. case INTEL_ALDERLAKE_S:
  4278. case INTEL_ALDERLAKE_P:
  4279. oa_format_add(perf, I915_OA_FORMAT_A12);
  4280. oa_format_add(perf, I915_OA_FORMAT_A12_B8_C8);
  4281. oa_format_add(perf, I915_OA_FORMAT_A32u40_A4u32_B8_C8);
  4282. oa_format_add(perf, I915_OA_FORMAT_C4_B8);
  4283. break;
  4284. case INTEL_DG2:
  4285. oa_format_add(perf, I915_OAR_FORMAT_A32u40_A4u32_B8_C8);
  4286. oa_format_add(perf, I915_OA_FORMAT_A24u40_A14u32_B8_C8);
  4287. break;
  4288. case INTEL_METEORLAKE:
  4289. oa_format_add(perf, I915_OAR_FORMAT_A32u40_A4u32_B8_C8);
  4290. oa_format_add(perf, I915_OA_FORMAT_A24u40_A14u32_B8_C8);
  4291. oa_format_add(perf, I915_OAM_FORMAT_MPEC8u64_B8_C8);
  4292. oa_format_add(perf, I915_OAM_FORMAT_MPEC8u32_B8_C8);
  4293. break;
  4294. default:
  4295. MISSING_CASE(platform);
  4296. }
  4297. }
  4298. static void i915_perf_init_info(struct drm_i915_private *i915)
  4299. {
  4300. struct i915_perf *perf = &i915->perf;
  4301. switch (GRAPHICS_VER(i915)) {
  4302. case 8:
  4303. perf->ctx_oactxctrl_offset = 0x120;
  4304. perf->ctx_flexeu0_offset = 0x2ce;
  4305. perf->gen8_valid_ctx_bit = BIT(25);
  4306. break;
  4307. case 9:
  4308. perf->ctx_oactxctrl_offset = 0x128;
  4309. perf->ctx_flexeu0_offset = 0x3de;
  4310. perf->gen8_valid_ctx_bit = BIT(16);
  4311. break;
  4312. case 11:
  4313. perf->ctx_oactxctrl_offset = 0x124;
  4314. perf->ctx_flexeu0_offset = 0x78e;
  4315. perf->gen8_valid_ctx_bit = BIT(16);
  4316. break;
  4317. case 12:
  4318. perf->gen8_valid_ctx_bit = BIT(16);
  4319. /*
  4320. * Calculate offset at runtime in oa_pin_context for gen12 and
  4321. * cache the value in perf->ctx_oactxctrl_offset.
  4322. */
  4323. break;
  4324. default:
  4325. MISSING_CASE(GRAPHICS_VER(i915));
  4326. }
  4327. }
  4328. /**
  4329. * i915_perf_init - initialize i915-perf state on module bind
  4330. * @i915: i915 device instance
  4331. *
  4332. * Initializes i915-perf state without exposing anything to userspace.
  4333. *
  4334. * Note: i915-perf initialization is split into an 'init' and 'register'
  4335. * phase with the i915_perf_register() exposing state to userspace.
  4336. */
  4337. int i915_perf_init(struct drm_i915_private *i915)
  4338. {
  4339. struct i915_perf *perf = &i915->perf;
  4340. perf->oa_formats = oa_formats;
  4341. if (IS_HASWELL(i915)) {
  4342. perf->ops.is_valid_b_counter_reg = gen7_is_valid_b_counter_addr;
  4343. perf->ops.is_valid_mux_reg = hsw_is_valid_mux_addr;
  4344. perf->ops.is_valid_flex_reg = NULL;
  4345. perf->ops.enable_metric_set = hsw_enable_metric_set;
  4346. perf->ops.disable_metric_set = hsw_disable_metric_set;
  4347. perf->ops.oa_enable = gen7_oa_enable;
  4348. perf->ops.oa_disable = gen7_oa_disable;
  4349. perf->ops.read = gen7_oa_read;
  4350. perf->ops.oa_hw_tail_read = gen7_oa_hw_tail_read;
  4351. } else if (HAS_LOGICAL_RING_CONTEXTS(i915)) {
  4352. /* Note: that although we could theoretically also support the
  4353. * legacy ringbuffer mode on BDW (and earlier iterations of
  4354. * this driver, before upstreaming did this) it didn't seem
  4355. * worth the complexity to maintain now that BDW+ enable
  4356. * execlist mode by default.
  4357. */
  4358. perf->ops.read = gen8_oa_read;
  4359. i915_perf_init_info(i915);
  4360. if (IS_GRAPHICS_VER(i915, 8, 9)) {
  4361. perf->ops.is_valid_b_counter_reg =
  4362. gen7_is_valid_b_counter_addr;
  4363. perf->ops.is_valid_mux_reg =
  4364. gen8_is_valid_mux_addr;
  4365. perf->ops.is_valid_flex_reg =
  4366. gen8_is_valid_flex_addr;
  4367. if (IS_CHERRYVIEW(i915)) {
  4368. perf->ops.is_valid_mux_reg =
  4369. chv_is_valid_mux_addr;
  4370. }
  4371. perf->ops.oa_enable = gen8_oa_enable;
  4372. perf->ops.oa_disable = gen8_oa_disable;
  4373. perf->ops.enable_metric_set = gen8_enable_metric_set;
  4374. perf->ops.disable_metric_set = gen8_disable_metric_set;
  4375. perf->ops.oa_hw_tail_read = gen8_oa_hw_tail_read;
  4376. } else if (GRAPHICS_VER(i915) == 11) {
  4377. perf->ops.is_valid_b_counter_reg =
  4378. gen7_is_valid_b_counter_addr;
  4379. perf->ops.is_valid_mux_reg =
  4380. gen11_is_valid_mux_addr;
  4381. perf->ops.is_valid_flex_reg =
  4382. gen8_is_valid_flex_addr;
  4383. perf->ops.oa_enable = gen8_oa_enable;
  4384. perf->ops.oa_disable = gen8_oa_disable;
  4385. perf->ops.enable_metric_set = gen8_enable_metric_set;
  4386. perf->ops.disable_metric_set = gen11_disable_metric_set;
  4387. perf->ops.oa_hw_tail_read = gen8_oa_hw_tail_read;
  4388. } else if (GRAPHICS_VER(i915) == 12) {
  4389. perf->ops.is_valid_b_counter_reg =
  4390. HAS_OA_SLICE_CONTRIB_LIMITS(i915) ?
  4391. xehp_is_valid_b_counter_addr :
  4392. gen12_is_valid_b_counter_addr;
  4393. perf->ops.is_valid_mux_reg =
  4394. gen12_is_valid_mux_addr;
  4395. perf->ops.is_valid_flex_reg =
  4396. gen8_is_valid_flex_addr;
  4397. perf->ops.oa_enable = gen12_oa_enable;
  4398. perf->ops.oa_disable = gen12_oa_disable;
  4399. perf->ops.enable_metric_set = gen12_enable_metric_set;
  4400. perf->ops.disable_metric_set = gen12_disable_metric_set;
  4401. perf->ops.oa_hw_tail_read = gen12_oa_hw_tail_read;
  4402. }
  4403. }
  4404. if (perf->ops.enable_metric_set) {
  4405. struct intel_gt *gt;
  4406. int i, ret;
  4407. for_each_gt(gt, i915, i)
  4408. mutex_init(&gt->perf.lock);
  4409. /* Choose a representative limit */
  4410. oa_sample_rate_hard_limit = to_gt(i915)->clock_frequency / 2;
  4411. mutex_init(&perf->metrics_lock);
  4412. idr_init_base(&perf->metrics_idr, 1);
  4413. /* We set up some ratelimit state to potentially throttle any
  4414. * _NOTES about spurious, invalid OA reports which we don't
  4415. * forward to userspace.
  4416. *
  4417. * We print a _NOTE about any throttling when closing the
  4418. * stream instead of waiting until driver _fini which no one
  4419. * would ever see.
  4420. *
  4421. * Using the same limiting factors as printk_ratelimit()
  4422. */
  4423. ratelimit_state_init(&perf->spurious_report_rs, 5 * HZ, 10);
  4424. /* Since we use a DRM_NOTE for spurious reports it would be
  4425. * inconsistent to let __ratelimit() automatically print a
  4426. * warning for throttling.
  4427. */
  4428. ratelimit_set_flags(&perf->spurious_report_rs,
  4429. RATELIMIT_MSG_ON_RELEASE);
  4430. ratelimit_state_init(&perf->tail_pointer_race,
  4431. 5 * HZ, 10);
  4432. ratelimit_set_flags(&perf->tail_pointer_race,
  4433. RATELIMIT_MSG_ON_RELEASE);
  4434. atomic64_set(&perf->noa_programming_delay,
  4435. 500 * 1000 /* 500us */);
  4436. perf->i915 = i915;
  4437. ret = oa_init_engine_groups(perf);
  4438. if (ret) {
  4439. drm_err(&i915->drm,
  4440. "OA initialization failed %d\n", ret);
  4441. return ret;
  4442. }
  4443. oa_init_supported_formats(perf);
  4444. }
  4445. return 0;
  4446. }
  4447. static int destroy_config(int id, void *p, void *data)
  4448. {
  4449. i915_oa_config_put(p);
  4450. return 0;
  4451. }
  4452. int i915_perf_sysctl_register(void)
  4453. {
  4454. sysctl_header = register_sysctl("dev/i915", oa_table);
  4455. return 0;
  4456. }
  4457. void i915_perf_sysctl_unregister(void)
  4458. {
  4459. unregister_sysctl_table(sysctl_header);
  4460. }
  4461. /**
  4462. * i915_perf_fini - Counter part to i915_perf_init()
  4463. * @i915: i915 device instance
  4464. */
  4465. void i915_perf_fini(struct drm_i915_private *i915)
  4466. {
  4467. struct i915_perf *perf = &i915->perf;
  4468. struct intel_gt *gt;
  4469. int i;
  4470. if (!perf->i915)
  4471. return;
  4472. for_each_gt(gt, perf->i915, i)
  4473. kfree(gt->perf.group);
  4474. idr_for_each(&perf->metrics_idr, destroy_config, perf);
  4475. idr_destroy(&perf->metrics_idr);
  4476. memset(&perf->ops, 0, sizeof(perf->ops));
  4477. perf->i915 = NULL;
  4478. }
  4479. /**
  4480. * i915_perf_ioctl_version - Version of the i915-perf subsystem
  4481. * @i915: The i915 device
  4482. *
  4483. * This version number is used by userspace to detect available features.
  4484. */
  4485. int i915_perf_ioctl_version(struct drm_i915_private *i915)
  4486. {
  4487. /*
  4488. * 1: Initial version
  4489. * I915_PERF_IOCTL_ENABLE
  4490. * I915_PERF_IOCTL_DISABLE
  4491. *
  4492. * 2: Added runtime modification of OA config.
  4493. * I915_PERF_IOCTL_CONFIG
  4494. *
  4495. * 3: Add DRM_I915_PERF_PROP_HOLD_PREEMPTION parameter to hold
  4496. * preemption on a particular context so that performance data is
  4497. * accessible from a delta of MI_RPC reports without looking at the
  4498. * OA buffer.
  4499. *
  4500. * 4: Add DRM_I915_PERF_PROP_ALLOWED_SSEU to limit what contexts can
  4501. * be run for the duration of the performance recording based on
  4502. * their SSEU configuration.
  4503. *
  4504. * 5: Add DRM_I915_PERF_PROP_POLL_OA_PERIOD parameter that controls the
  4505. * interval for the hrtimer used to check for OA data.
  4506. *
  4507. * 6: Add DRM_I915_PERF_PROP_OA_ENGINE_CLASS and
  4508. * DRM_I915_PERF_PROP_OA_ENGINE_INSTANCE
  4509. *
  4510. * 7: Add support for video decode and enhancement classes.
  4511. */
  4512. /*
  4513. * Wa_14017512683: mtl[a0..c0): Use of OAM must be preceded with Media
  4514. * C6 disable in BIOS. If Media C6 is enabled in BIOS, return version 6
  4515. * to indicate that OA media is not supported.
  4516. */
  4517. if (IS_MEDIA_GT_IP_STEP(i915->media_gt, IP_VER(13, 0), STEP_A0, STEP_C0) &&
  4518. intel_check_bios_c6_setup(&i915->media_gt->rc6))
  4519. return 6;
  4520. return 7;
  4521. }
  4522. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  4523. #include "selftests/i915_perf.c"
  4524. #endif