irq-gic-v3-its.c 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
  4. * Author: Marc Zyngier <[email protected]>
  5. */
  6. #include <linux/acpi.h>
  7. #include <linux/acpi_iort.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/bitmap.h>
  10. #include <linux/cpu.h>
  11. #include <linux/crash_dump.h>
  12. #include <linux/delay.h>
  13. #include <linux/efi.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/iommu.h>
  16. #include <linux/iopoll.h>
  17. #include <linux/irqdomain.h>
  18. #include <linux/list.h>
  19. #include <linux/log2.h>
  20. #include <linux/memblock.h>
  21. #include <linux/mm.h>
  22. #include <linux/msi.h>
  23. #include <linux/of.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/of_pci.h>
  27. #include <linux/of_platform.h>
  28. #include <linux/percpu.h>
  29. #include <linux/slab.h>
  30. #include <linux/syscore_ops.h>
  31. #include <linux/irqchip.h>
  32. #include <linux/irqchip/arm-gic-v3.h>
  33. #include <linux/irqchip/arm-gic-v4.h>
  34. #include <asm/cputype.h>
  35. #include <asm/exception.h>
  36. #include "irq-gic-common.h"
  37. #define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
  38. #define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
  39. #define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
  40. #define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
  41. #define RDIST_FLAGS_RD_TABLES_PREALLOCATED (1 << 1)
  42. #define RD_LOCAL_LPI_ENABLED BIT(0)
  43. #define RD_LOCAL_PENDTABLE_PREALLOCATED BIT(1)
  44. #define RD_LOCAL_MEMRESERVE_DONE BIT(2)
  45. static u32 lpi_id_bits;
  46. /*
  47. * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
  48. * deal with (one configuration byte per interrupt). PENDBASE has to
  49. * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
  50. */
  51. #define LPI_NRBITS lpi_id_bits
  52. #define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
  53. #define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
  54. #define LPI_PROP_DEFAULT_PRIO GICD_INT_DEF_PRI
  55. /*
  56. * Collection structure - just an ID, and a redistributor address to
  57. * ping. We use one per CPU as a bag of interrupts assigned to this
  58. * CPU.
  59. */
  60. struct its_collection {
  61. u64 target_address;
  62. u16 col_id;
  63. };
  64. /*
  65. * The ITS_BASER structure - contains memory information, cached
  66. * value of BASER register configuration and ITS page size.
  67. */
  68. struct its_baser {
  69. void *base;
  70. u64 val;
  71. u32 order;
  72. u32 psz;
  73. };
  74. struct its_device;
  75. /*
  76. * The ITS structure - contains most of the infrastructure, with the
  77. * top-level MSI domain, the command queue, the collections, and the
  78. * list of devices writing to it.
  79. *
  80. * dev_alloc_lock has to be taken for device allocations, while the
  81. * spinlock must be taken to parse data structures such as the device
  82. * list.
  83. */
  84. struct its_node {
  85. raw_spinlock_t lock;
  86. struct mutex dev_alloc_lock;
  87. struct list_head entry;
  88. void __iomem *base;
  89. void __iomem *sgir_base;
  90. phys_addr_t phys_base;
  91. struct its_cmd_block *cmd_base;
  92. struct its_cmd_block *cmd_write;
  93. struct its_baser tables[GITS_BASER_NR_REGS];
  94. struct its_collection *collections;
  95. struct fwnode_handle *fwnode_handle;
  96. u64 (*get_msi_base)(struct its_device *its_dev);
  97. u64 typer;
  98. u64 cbaser_save;
  99. u32 ctlr_save;
  100. u32 mpidr;
  101. struct list_head its_device_list;
  102. u64 flags;
  103. unsigned long list_nr;
  104. int numa_node;
  105. unsigned int msi_domain_flags;
  106. u32 pre_its_base; /* for Socionext Synquacer */
  107. int vlpi_redist_offset;
  108. };
  109. #define is_v4(its) (!!((its)->typer & GITS_TYPER_VLPIS))
  110. #define is_v4_1(its) (!!((its)->typer & GITS_TYPER_VMAPP))
  111. #define device_ids(its) (FIELD_GET(GITS_TYPER_DEVBITS, (its)->typer) + 1)
  112. #define ITS_ITT_ALIGN SZ_256
  113. /* The maximum number of VPEID bits supported by VLPI commands */
  114. #define ITS_MAX_VPEID_BITS \
  115. ({ \
  116. int nvpeid = 16; \
  117. if (gic_rdists->has_rvpeid && \
  118. gic_rdists->gicd_typer2 & GICD_TYPER2_VIL) \
  119. nvpeid = 1 + (gic_rdists->gicd_typer2 & \
  120. GICD_TYPER2_VID); \
  121. \
  122. nvpeid; \
  123. })
  124. #define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS))
  125. /* Convert page order to size in bytes */
  126. #define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
  127. struct event_lpi_map {
  128. unsigned long *lpi_map;
  129. u16 *col_map;
  130. irq_hw_number_t lpi_base;
  131. int nr_lpis;
  132. raw_spinlock_t vlpi_lock;
  133. struct its_vm *vm;
  134. struct its_vlpi_map *vlpi_maps;
  135. int nr_vlpis;
  136. };
  137. /*
  138. * The ITS view of a device - belongs to an ITS, owns an interrupt
  139. * translation table, and a list of interrupts. If it some of its
  140. * LPIs are injected into a guest (GICv4), the event_map.vm field
  141. * indicates which one.
  142. */
  143. struct its_device {
  144. struct list_head entry;
  145. struct its_node *its;
  146. struct event_lpi_map event_map;
  147. void *itt;
  148. u32 nr_ites;
  149. u32 device_id;
  150. bool shared;
  151. };
  152. static struct {
  153. raw_spinlock_t lock;
  154. struct its_device *dev;
  155. struct its_vpe **vpes;
  156. int next_victim;
  157. } vpe_proxy;
  158. struct cpu_lpi_count {
  159. atomic_t managed;
  160. atomic_t unmanaged;
  161. };
  162. static DEFINE_PER_CPU(struct cpu_lpi_count, cpu_lpi_count);
  163. static LIST_HEAD(its_nodes);
  164. static DEFINE_RAW_SPINLOCK(its_lock);
  165. static struct rdists *gic_rdists;
  166. static struct irq_domain *its_parent;
  167. static unsigned long its_list_map;
  168. static u16 vmovp_seq_num;
  169. static DEFINE_RAW_SPINLOCK(vmovp_lock);
  170. static DEFINE_IDA(its_vpeid_ida);
  171. #define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
  172. #define gic_data_rdist_cpu(cpu) (per_cpu_ptr(gic_rdists->rdist, cpu))
  173. #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
  174. #define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
  175. /*
  176. * Skip ITSs that have no vLPIs mapped, unless we're on GICv4.1, as we
  177. * always have vSGIs mapped.
  178. */
  179. static bool require_its_list_vmovp(struct its_vm *vm, struct its_node *its)
  180. {
  181. return (gic_rdists->has_rvpeid || vm->vlpi_count[its->list_nr]);
  182. }
  183. static u16 get_its_list(struct its_vm *vm)
  184. {
  185. struct its_node *its;
  186. unsigned long its_list = 0;
  187. list_for_each_entry(its, &its_nodes, entry) {
  188. if (!is_v4(its))
  189. continue;
  190. if (require_its_list_vmovp(vm, its))
  191. __set_bit(its->list_nr, &its_list);
  192. }
  193. return (u16)its_list;
  194. }
  195. static inline u32 its_get_event_id(struct irq_data *d)
  196. {
  197. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  198. return d->hwirq - its_dev->event_map.lpi_base;
  199. }
  200. static struct its_collection *dev_event_to_col(struct its_device *its_dev,
  201. u32 event)
  202. {
  203. struct its_node *its = its_dev->its;
  204. return its->collections + its_dev->event_map.col_map[event];
  205. }
  206. static struct its_vlpi_map *dev_event_to_vlpi_map(struct its_device *its_dev,
  207. u32 event)
  208. {
  209. if (WARN_ON_ONCE(event >= its_dev->event_map.nr_lpis))
  210. return NULL;
  211. return &its_dev->event_map.vlpi_maps[event];
  212. }
  213. static struct its_vlpi_map *get_vlpi_map(struct irq_data *d)
  214. {
  215. if (irqd_is_forwarded_to_vcpu(d)) {
  216. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  217. u32 event = its_get_event_id(d);
  218. return dev_event_to_vlpi_map(its_dev, event);
  219. }
  220. return NULL;
  221. }
  222. static int vpe_to_cpuid_lock(struct its_vpe *vpe, unsigned long *flags)
  223. {
  224. raw_spin_lock_irqsave(&vpe->vpe_lock, *flags);
  225. return vpe->col_idx;
  226. }
  227. static void vpe_to_cpuid_unlock(struct its_vpe *vpe, unsigned long flags)
  228. {
  229. raw_spin_unlock_irqrestore(&vpe->vpe_lock, flags);
  230. }
  231. static struct irq_chip its_vpe_irq_chip;
  232. static int irq_to_cpuid_lock(struct irq_data *d, unsigned long *flags)
  233. {
  234. struct its_vpe *vpe = NULL;
  235. int cpu;
  236. if (d->chip == &its_vpe_irq_chip) {
  237. vpe = irq_data_get_irq_chip_data(d);
  238. } else {
  239. struct its_vlpi_map *map = get_vlpi_map(d);
  240. if (map)
  241. vpe = map->vpe;
  242. }
  243. if (vpe) {
  244. cpu = vpe_to_cpuid_lock(vpe, flags);
  245. } else {
  246. /* Physical LPIs are already locked via the irq_desc lock */
  247. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  248. cpu = its_dev->event_map.col_map[its_get_event_id(d)];
  249. /* Keep GCC quiet... */
  250. *flags = 0;
  251. }
  252. return cpu;
  253. }
  254. static void irq_to_cpuid_unlock(struct irq_data *d, unsigned long flags)
  255. {
  256. struct its_vpe *vpe = NULL;
  257. if (d->chip == &its_vpe_irq_chip) {
  258. vpe = irq_data_get_irq_chip_data(d);
  259. } else {
  260. struct its_vlpi_map *map = get_vlpi_map(d);
  261. if (map)
  262. vpe = map->vpe;
  263. }
  264. if (vpe)
  265. vpe_to_cpuid_unlock(vpe, flags);
  266. }
  267. static struct its_collection *valid_col(struct its_collection *col)
  268. {
  269. if (WARN_ON_ONCE(col->target_address & GENMASK_ULL(15, 0)))
  270. return NULL;
  271. return col;
  272. }
  273. static struct its_vpe *valid_vpe(struct its_node *its, struct its_vpe *vpe)
  274. {
  275. if (valid_col(its->collections + vpe->col_idx))
  276. return vpe;
  277. return NULL;
  278. }
  279. /*
  280. * ITS command descriptors - parameters to be encoded in a command
  281. * block.
  282. */
  283. struct its_cmd_desc {
  284. union {
  285. struct {
  286. struct its_device *dev;
  287. u32 event_id;
  288. } its_inv_cmd;
  289. struct {
  290. struct its_device *dev;
  291. u32 event_id;
  292. } its_clear_cmd;
  293. struct {
  294. struct its_device *dev;
  295. u32 event_id;
  296. } its_int_cmd;
  297. struct {
  298. struct its_device *dev;
  299. int valid;
  300. } its_mapd_cmd;
  301. struct {
  302. struct its_collection *col;
  303. int valid;
  304. } its_mapc_cmd;
  305. struct {
  306. struct its_device *dev;
  307. u32 phys_id;
  308. u32 event_id;
  309. } its_mapti_cmd;
  310. struct {
  311. struct its_device *dev;
  312. struct its_collection *col;
  313. u32 event_id;
  314. } its_movi_cmd;
  315. struct {
  316. struct its_device *dev;
  317. u32 event_id;
  318. } its_discard_cmd;
  319. struct {
  320. struct its_collection *col;
  321. } its_invall_cmd;
  322. struct {
  323. struct its_vpe *vpe;
  324. } its_vinvall_cmd;
  325. struct {
  326. struct its_vpe *vpe;
  327. struct its_collection *col;
  328. bool valid;
  329. } its_vmapp_cmd;
  330. struct {
  331. struct its_vpe *vpe;
  332. struct its_device *dev;
  333. u32 virt_id;
  334. u32 event_id;
  335. bool db_enabled;
  336. } its_vmapti_cmd;
  337. struct {
  338. struct its_vpe *vpe;
  339. struct its_device *dev;
  340. u32 event_id;
  341. bool db_enabled;
  342. } its_vmovi_cmd;
  343. struct {
  344. struct its_vpe *vpe;
  345. struct its_collection *col;
  346. u16 seq_num;
  347. u16 its_list;
  348. } its_vmovp_cmd;
  349. struct {
  350. struct its_vpe *vpe;
  351. } its_invdb_cmd;
  352. struct {
  353. struct its_vpe *vpe;
  354. u8 sgi;
  355. u8 priority;
  356. bool enable;
  357. bool group;
  358. bool clear;
  359. } its_vsgi_cmd;
  360. };
  361. };
  362. /*
  363. * The ITS command block, which is what the ITS actually parses.
  364. */
  365. struct its_cmd_block {
  366. union {
  367. u64 raw_cmd[4];
  368. __le64 raw_cmd_le[4];
  369. };
  370. };
  371. #define ITS_CMD_QUEUE_SZ SZ_64K
  372. #define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
  373. typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *,
  374. struct its_cmd_block *,
  375. struct its_cmd_desc *);
  376. typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *,
  377. struct its_cmd_block *,
  378. struct its_cmd_desc *);
  379. static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
  380. {
  381. u64 mask = GENMASK_ULL(h, l);
  382. *raw_cmd &= ~mask;
  383. *raw_cmd |= (val << l) & mask;
  384. }
  385. static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
  386. {
  387. its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
  388. }
  389. static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
  390. {
  391. its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
  392. }
  393. static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
  394. {
  395. its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
  396. }
  397. static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
  398. {
  399. its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
  400. }
  401. static void its_encode_size(struct its_cmd_block *cmd, u8 size)
  402. {
  403. its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
  404. }
  405. static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
  406. {
  407. its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8);
  408. }
  409. static void its_encode_valid(struct its_cmd_block *cmd, int valid)
  410. {
  411. its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
  412. }
  413. static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
  414. {
  415. its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16);
  416. }
  417. static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
  418. {
  419. its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
  420. }
  421. static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
  422. {
  423. its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
  424. }
  425. static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
  426. {
  427. its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
  428. }
  429. static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
  430. {
  431. its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
  432. }
  433. static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
  434. {
  435. its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
  436. }
  437. static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
  438. {
  439. its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
  440. }
  441. static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
  442. {
  443. its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
  444. }
  445. static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
  446. {
  447. its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16);
  448. }
  449. static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
  450. {
  451. its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
  452. }
  453. static void its_encode_vconf_addr(struct its_cmd_block *cmd, u64 vconf_pa)
  454. {
  455. its_mask_encode(&cmd->raw_cmd[0], vconf_pa >> 16, 51, 16);
  456. }
  457. static void its_encode_alloc(struct its_cmd_block *cmd, bool alloc)
  458. {
  459. its_mask_encode(&cmd->raw_cmd[0], alloc, 8, 8);
  460. }
  461. static void its_encode_ptz(struct its_cmd_block *cmd, bool ptz)
  462. {
  463. its_mask_encode(&cmd->raw_cmd[0], ptz, 9, 9);
  464. }
  465. static void its_encode_vmapp_default_db(struct its_cmd_block *cmd,
  466. u32 vpe_db_lpi)
  467. {
  468. its_mask_encode(&cmd->raw_cmd[1], vpe_db_lpi, 31, 0);
  469. }
  470. static void its_encode_vmovp_default_db(struct its_cmd_block *cmd,
  471. u32 vpe_db_lpi)
  472. {
  473. its_mask_encode(&cmd->raw_cmd[3], vpe_db_lpi, 31, 0);
  474. }
  475. static void its_encode_db(struct its_cmd_block *cmd, bool db)
  476. {
  477. its_mask_encode(&cmd->raw_cmd[2], db, 63, 63);
  478. }
  479. static void its_encode_sgi_intid(struct its_cmd_block *cmd, u8 sgi)
  480. {
  481. its_mask_encode(&cmd->raw_cmd[0], sgi, 35, 32);
  482. }
  483. static void its_encode_sgi_priority(struct its_cmd_block *cmd, u8 prio)
  484. {
  485. its_mask_encode(&cmd->raw_cmd[0], prio >> 4, 23, 20);
  486. }
  487. static void its_encode_sgi_group(struct its_cmd_block *cmd, bool grp)
  488. {
  489. its_mask_encode(&cmd->raw_cmd[0], grp, 10, 10);
  490. }
  491. static void its_encode_sgi_clear(struct its_cmd_block *cmd, bool clr)
  492. {
  493. its_mask_encode(&cmd->raw_cmd[0], clr, 9, 9);
  494. }
  495. static void its_encode_sgi_enable(struct its_cmd_block *cmd, bool en)
  496. {
  497. its_mask_encode(&cmd->raw_cmd[0], en, 8, 8);
  498. }
  499. static inline void its_fixup_cmd(struct its_cmd_block *cmd)
  500. {
  501. /* Let's fixup BE commands */
  502. cmd->raw_cmd_le[0] = cpu_to_le64(cmd->raw_cmd[0]);
  503. cmd->raw_cmd_le[1] = cpu_to_le64(cmd->raw_cmd[1]);
  504. cmd->raw_cmd_le[2] = cpu_to_le64(cmd->raw_cmd[2]);
  505. cmd->raw_cmd_le[3] = cpu_to_le64(cmd->raw_cmd[3]);
  506. }
  507. static struct its_collection *its_build_mapd_cmd(struct its_node *its,
  508. struct its_cmd_block *cmd,
  509. struct its_cmd_desc *desc)
  510. {
  511. unsigned long itt_addr;
  512. u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
  513. itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
  514. itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
  515. its_encode_cmd(cmd, GITS_CMD_MAPD);
  516. its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
  517. its_encode_size(cmd, size - 1);
  518. its_encode_itt(cmd, itt_addr);
  519. its_encode_valid(cmd, desc->its_mapd_cmd.valid);
  520. its_fixup_cmd(cmd);
  521. return NULL;
  522. }
  523. static struct its_collection *its_build_mapc_cmd(struct its_node *its,
  524. struct its_cmd_block *cmd,
  525. struct its_cmd_desc *desc)
  526. {
  527. its_encode_cmd(cmd, GITS_CMD_MAPC);
  528. its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
  529. its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
  530. its_encode_valid(cmd, desc->its_mapc_cmd.valid);
  531. its_fixup_cmd(cmd);
  532. return desc->its_mapc_cmd.col;
  533. }
  534. static struct its_collection *its_build_mapti_cmd(struct its_node *its,
  535. struct its_cmd_block *cmd,
  536. struct its_cmd_desc *desc)
  537. {
  538. struct its_collection *col;
  539. col = dev_event_to_col(desc->its_mapti_cmd.dev,
  540. desc->its_mapti_cmd.event_id);
  541. its_encode_cmd(cmd, GITS_CMD_MAPTI);
  542. its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
  543. its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
  544. its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
  545. its_encode_collection(cmd, col->col_id);
  546. its_fixup_cmd(cmd);
  547. return valid_col(col);
  548. }
  549. static struct its_collection *its_build_movi_cmd(struct its_node *its,
  550. struct its_cmd_block *cmd,
  551. struct its_cmd_desc *desc)
  552. {
  553. struct its_collection *col;
  554. col = dev_event_to_col(desc->its_movi_cmd.dev,
  555. desc->its_movi_cmd.event_id);
  556. its_encode_cmd(cmd, GITS_CMD_MOVI);
  557. its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
  558. its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
  559. its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
  560. its_fixup_cmd(cmd);
  561. return valid_col(col);
  562. }
  563. static struct its_collection *its_build_discard_cmd(struct its_node *its,
  564. struct its_cmd_block *cmd,
  565. struct its_cmd_desc *desc)
  566. {
  567. struct its_collection *col;
  568. col = dev_event_to_col(desc->its_discard_cmd.dev,
  569. desc->its_discard_cmd.event_id);
  570. its_encode_cmd(cmd, GITS_CMD_DISCARD);
  571. its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
  572. its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
  573. its_fixup_cmd(cmd);
  574. return valid_col(col);
  575. }
  576. static struct its_collection *its_build_inv_cmd(struct its_node *its,
  577. struct its_cmd_block *cmd,
  578. struct its_cmd_desc *desc)
  579. {
  580. struct its_collection *col;
  581. col = dev_event_to_col(desc->its_inv_cmd.dev,
  582. desc->its_inv_cmd.event_id);
  583. its_encode_cmd(cmd, GITS_CMD_INV);
  584. its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
  585. its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
  586. its_fixup_cmd(cmd);
  587. return valid_col(col);
  588. }
  589. static struct its_collection *its_build_int_cmd(struct its_node *its,
  590. struct its_cmd_block *cmd,
  591. struct its_cmd_desc *desc)
  592. {
  593. struct its_collection *col;
  594. col = dev_event_to_col(desc->its_int_cmd.dev,
  595. desc->its_int_cmd.event_id);
  596. its_encode_cmd(cmd, GITS_CMD_INT);
  597. its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
  598. its_encode_event_id(cmd, desc->its_int_cmd.event_id);
  599. its_fixup_cmd(cmd);
  600. return valid_col(col);
  601. }
  602. static struct its_collection *its_build_clear_cmd(struct its_node *its,
  603. struct its_cmd_block *cmd,
  604. struct its_cmd_desc *desc)
  605. {
  606. struct its_collection *col;
  607. col = dev_event_to_col(desc->its_clear_cmd.dev,
  608. desc->its_clear_cmd.event_id);
  609. its_encode_cmd(cmd, GITS_CMD_CLEAR);
  610. its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
  611. its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
  612. its_fixup_cmd(cmd);
  613. return valid_col(col);
  614. }
  615. static struct its_collection *its_build_invall_cmd(struct its_node *its,
  616. struct its_cmd_block *cmd,
  617. struct its_cmd_desc *desc)
  618. {
  619. its_encode_cmd(cmd, GITS_CMD_INVALL);
  620. its_encode_collection(cmd, desc->its_invall_cmd.col->col_id);
  621. its_fixup_cmd(cmd);
  622. return desc->its_invall_cmd.col;
  623. }
  624. static struct its_vpe *its_build_vinvall_cmd(struct its_node *its,
  625. struct its_cmd_block *cmd,
  626. struct its_cmd_desc *desc)
  627. {
  628. its_encode_cmd(cmd, GITS_CMD_VINVALL);
  629. its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
  630. its_fixup_cmd(cmd);
  631. return valid_vpe(its, desc->its_vinvall_cmd.vpe);
  632. }
  633. static struct its_vpe *its_build_vmapp_cmd(struct its_node *its,
  634. struct its_cmd_block *cmd,
  635. struct its_cmd_desc *desc)
  636. {
  637. unsigned long vpt_addr, vconf_addr;
  638. u64 target;
  639. bool alloc;
  640. its_encode_cmd(cmd, GITS_CMD_VMAPP);
  641. its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
  642. its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
  643. if (!desc->its_vmapp_cmd.valid) {
  644. if (is_v4_1(its)) {
  645. alloc = !atomic_dec_return(&desc->its_vmapp_cmd.vpe->vmapp_count);
  646. its_encode_alloc(cmd, alloc);
  647. }
  648. goto out;
  649. }
  650. vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
  651. target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset;
  652. its_encode_target(cmd, target);
  653. its_encode_vpt_addr(cmd, vpt_addr);
  654. its_encode_vpt_size(cmd, LPI_NRBITS - 1);
  655. if (!is_v4_1(its))
  656. goto out;
  657. vconf_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->its_vm->vprop_page));
  658. alloc = !atomic_fetch_inc(&desc->its_vmapp_cmd.vpe->vmapp_count);
  659. its_encode_alloc(cmd, alloc);
  660. /*
  661. * GICv4.1 provides a way to get the VLPI state, which needs the vPE
  662. * to be unmapped first, and in this case, we may remap the vPE
  663. * back while the VPT is not empty. So we can't assume that the
  664. * VPT is empty on map. This is why we never advertise PTZ.
  665. */
  666. its_encode_ptz(cmd, false);
  667. its_encode_vconf_addr(cmd, vconf_addr);
  668. its_encode_vmapp_default_db(cmd, desc->its_vmapp_cmd.vpe->vpe_db_lpi);
  669. out:
  670. its_fixup_cmd(cmd);
  671. return valid_vpe(its, desc->its_vmapp_cmd.vpe);
  672. }
  673. static struct its_vpe *its_build_vmapti_cmd(struct its_node *its,
  674. struct its_cmd_block *cmd,
  675. struct its_cmd_desc *desc)
  676. {
  677. u32 db;
  678. if (!is_v4_1(its) && desc->its_vmapti_cmd.db_enabled)
  679. db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
  680. else
  681. db = 1023;
  682. its_encode_cmd(cmd, GITS_CMD_VMAPTI);
  683. its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
  684. its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
  685. its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
  686. its_encode_db_phys_id(cmd, db);
  687. its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
  688. its_fixup_cmd(cmd);
  689. return valid_vpe(its, desc->its_vmapti_cmd.vpe);
  690. }
  691. static struct its_vpe *its_build_vmovi_cmd(struct its_node *its,
  692. struct its_cmd_block *cmd,
  693. struct its_cmd_desc *desc)
  694. {
  695. u32 db;
  696. if (!is_v4_1(its) && desc->its_vmovi_cmd.db_enabled)
  697. db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
  698. else
  699. db = 1023;
  700. its_encode_cmd(cmd, GITS_CMD_VMOVI);
  701. its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
  702. its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
  703. its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
  704. its_encode_db_phys_id(cmd, db);
  705. its_encode_db_valid(cmd, true);
  706. its_fixup_cmd(cmd);
  707. return valid_vpe(its, desc->its_vmovi_cmd.vpe);
  708. }
  709. static struct its_vpe *its_build_vmovp_cmd(struct its_node *its,
  710. struct its_cmd_block *cmd,
  711. struct its_cmd_desc *desc)
  712. {
  713. u64 target;
  714. target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset;
  715. its_encode_cmd(cmd, GITS_CMD_VMOVP);
  716. its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
  717. its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
  718. its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
  719. its_encode_target(cmd, target);
  720. if (is_v4_1(its)) {
  721. its_encode_db(cmd, true);
  722. its_encode_vmovp_default_db(cmd, desc->its_vmovp_cmd.vpe->vpe_db_lpi);
  723. }
  724. its_fixup_cmd(cmd);
  725. return valid_vpe(its, desc->its_vmovp_cmd.vpe);
  726. }
  727. static struct its_vpe *its_build_vinv_cmd(struct its_node *its,
  728. struct its_cmd_block *cmd,
  729. struct its_cmd_desc *desc)
  730. {
  731. struct its_vlpi_map *map;
  732. map = dev_event_to_vlpi_map(desc->its_inv_cmd.dev,
  733. desc->its_inv_cmd.event_id);
  734. its_encode_cmd(cmd, GITS_CMD_INV);
  735. its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
  736. its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
  737. its_fixup_cmd(cmd);
  738. return valid_vpe(its, map->vpe);
  739. }
  740. static struct its_vpe *its_build_vint_cmd(struct its_node *its,
  741. struct its_cmd_block *cmd,
  742. struct its_cmd_desc *desc)
  743. {
  744. struct its_vlpi_map *map;
  745. map = dev_event_to_vlpi_map(desc->its_int_cmd.dev,
  746. desc->its_int_cmd.event_id);
  747. its_encode_cmd(cmd, GITS_CMD_INT);
  748. its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
  749. its_encode_event_id(cmd, desc->its_int_cmd.event_id);
  750. its_fixup_cmd(cmd);
  751. return valid_vpe(its, map->vpe);
  752. }
  753. static struct its_vpe *its_build_vclear_cmd(struct its_node *its,
  754. struct its_cmd_block *cmd,
  755. struct its_cmd_desc *desc)
  756. {
  757. struct its_vlpi_map *map;
  758. map = dev_event_to_vlpi_map(desc->its_clear_cmd.dev,
  759. desc->its_clear_cmd.event_id);
  760. its_encode_cmd(cmd, GITS_CMD_CLEAR);
  761. its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
  762. its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
  763. its_fixup_cmd(cmd);
  764. return valid_vpe(its, map->vpe);
  765. }
  766. static struct its_vpe *its_build_invdb_cmd(struct its_node *its,
  767. struct its_cmd_block *cmd,
  768. struct its_cmd_desc *desc)
  769. {
  770. if (WARN_ON(!is_v4_1(its)))
  771. return NULL;
  772. its_encode_cmd(cmd, GITS_CMD_INVDB);
  773. its_encode_vpeid(cmd, desc->its_invdb_cmd.vpe->vpe_id);
  774. its_fixup_cmd(cmd);
  775. return valid_vpe(its, desc->its_invdb_cmd.vpe);
  776. }
  777. static struct its_vpe *its_build_vsgi_cmd(struct its_node *its,
  778. struct its_cmd_block *cmd,
  779. struct its_cmd_desc *desc)
  780. {
  781. if (WARN_ON(!is_v4_1(its)))
  782. return NULL;
  783. its_encode_cmd(cmd, GITS_CMD_VSGI);
  784. its_encode_vpeid(cmd, desc->its_vsgi_cmd.vpe->vpe_id);
  785. its_encode_sgi_intid(cmd, desc->its_vsgi_cmd.sgi);
  786. its_encode_sgi_priority(cmd, desc->its_vsgi_cmd.priority);
  787. its_encode_sgi_group(cmd, desc->its_vsgi_cmd.group);
  788. its_encode_sgi_clear(cmd, desc->its_vsgi_cmd.clear);
  789. its_encode_sgi_enable(cmd, desc->its_vsgi_cmd.enable);
  790. its_fixup_cmd(cmd);
  791. return valid_vpe(its, desc->its_vsgi_cmd.vpe);
  792. }
  793. static u64 its_cmd_ptr_to_offset(struct its_node *its,
  794. struct its_cmd_block *ptr)
  795. {
  796. return (ptr - its->cmd_base) * sizeof(*ptr);
  797. }
  798. static int its_queue_full(struct its_node *its)
  799. {
  800. int widx;
  801. int ridx;
  802. widx = its->cmd_write - its->cmd_base;
  803. ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
  804. /* This is incredibly unlikely to happen, unless the ITS locks up. */
  805. if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
  806. return 1;
  807. return 0;
  808. }
  809. static struct its_cmd_block *its_allocate_entry(struct its_node *its)
  810. {
  811. struct its_cmd_block *cmd;
  812. u32 count = 1000000; /* 1s! */
  813. while (its_queue_full(its)) {
  814. count--;
  815. if (!count) {
  816. pr_err_ratelimited("ITS queue not draining\n");
  817. return NULL;
  818. }
  819. cpu_relax();
  820. udelay(1);
  821. }
  822. cmd = its->cmd_write++;
  823. /* Handle queue wrapping */
  824. if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
  825. its->cmd_write = its->cmd_base;
  826. /* Clear command */
  827. cmd->raw_cmd[0] = 0;
  828. cmd->raw_cmd[1] = 0;
  829. cmd->raw_cmd[2] = 0;
  830. cmd->raw_cmd[3] = 0;
  831. return cmd;
  832. }
  833. static struct its_cmd_block *its_post_commands(struct its_node *its)
  834. {
  835. u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
  836. writel_relaxed(wr, its->base + GITS_CWRITER);
  837. return its->cmd_write;
  838. }
  839. static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
  840. {
  841. /*
  842. * Make sure the commands written to memory are observable by
  843. * the ITS.
  844. */
  845. if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
  846. gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
  847. else
  848. dsb(ishst);
  849. }
  850. static int its_wait_for_range_completion(struct its_node *its,
  851. u64 prev_idx,
  852. struct its_cmd_block *to)
  853. {
  854. u64 rd_idx, to_idx, linear_idx;
  855. u32 count = 1000000; /* 1s! */
  856. /* Linearize to_idx if the command set has wrapped around */
  857. to_idx = its_cmd_ptr_to_offset(its, to);
  858. if (to_idx < prev_idx)
  859. to_idx += ITS_CMD_QUEUE_SZ;
  860. linear_idx = prev_idx;
  861. while (1) {
  862. s64 delta;
  863. rd_idx = readl_relaxed(its->base + GITS_CREADR);
  864. /*
  865. * Compute the read pointer progress, taking the
  866. * potential wrap-around into account.
  867. */
  868. delta = rd_idx - prev_idx;
  869. if (rd_idx < prev_idx)
  870. delta += ITS_CMD_QUEUE_SZ;
  871. linear_idx += delta;
  872. if (linear_idx >= to_idx)
  873. break;
  874. count--;
  875. if (!count) {
  876. pr_err_ratelimited("ITS queue timeout (%llu %llu)\n",
  877. to_idx, linear_idx);
  878. return -1;
  879. }
  880. prev_idx = rd_idx;
  881. cpu_relax();
  882. udelay(1);
  883. }
  884. return 0;
  885. }
  886. /* Warning, macro hell follows */
  887. #define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
  888. void name(struct its_node *its, \
  889. buildtype builder, \
  890. struct its_cmd_desc *desc) \
  891. { \
  892. struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
  893. synctype *sync_obj; \
  894. unsigned long flags; \
  895. u64 rd_idx; \
  896. \
  897. raw_spin_lock_irqsave(&its->lock, flags); \
  898. \
  899. cmd = its_allocate_entry(its); \
  900. if (!cmd) { /* We're soooooo screewed... */ \
  901. raw_spin_unlock_irqrestore(&its->lock, flags); \
  902. return; \
  903. } \
  904. sync_obj = builder(its, cmd, desc); \
  905. its_flush_cmd(its, cmd); \
  906. \
  907. if (sync_obj) { \
  908. sync_cmd = its_allocate_entry(its); \
  909. if (!sync_cmd) \
  910. goto post; \
  911. \
  912. buildfn(its, sync_cmd, sync_obj); \
  913. its_flush_cmd(its, sync_cmd); \
  914. } \
  915. \
  916. post: \
  917. rd_idx = readl_relaxed(its->base + GITS_CREADR); \
  918. next_cmd = its_post_commands(its); \
  919. raw_spin_unlock_irqrestore(&its->lock, flags); \
  920. \
  921. if (its_wait_for_range_completion(its, rd_idx, next_cmd)) \
  922. pr_err_ratelimited("ITS cmd %ps failed\n", builder); \
  923. }
  924. static void its_build_sync_cmd(struct its_node *its,
  925. struct its_cmd_block *sync_cmd,
  926. struct its_collection *sync_col)
  927. {
  928. its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
  929. its_encode_target(sync_cmd, sync_col->target_address);
  930. its_fixup_cmd(sync_cmd);
  931. }
  932. static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
  933. struct its_collection, its_build_sync_cmd)
  934. static void its_build_vsync_cmd(struct its_node *its,
  935. struct its_cmd_block *sync_cmd,
  936. struct its_vpe *sync_vpe)
  937. {
  938. its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
  939. its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
  940. its_fixup_cmd(sync_cmd);
  941. }
  942. static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
  943. struct its_vpe, its_build_vsync_cmd)
  944. static void its_send_int(struct its_device *dev, u32 event_id)
  945. {
  946. struct its_cmd_desc desc;
  947. desc.its_int_cmd.dev = dev;
  948. desc.its_int_cmd.event_id = event_id;
  949. its_send_single_command(dev->its, its_build_int_cmd, &desc);
  950. }
  951. static void its_send_clear(struct its_device *dev, u32 event_id)
  952. {
  953. struct its_cmd_desc desc;
  954. desc.its_clear_cmd.dev = dev;
  955. desc.its_clear_cmd.event_id = event_id;
  956. its_send_single_command(dev->its, its_build_clear_cmd, &desc);
  957. }
  958. static void its_send_inv(struct its_device *dev, u32 event_id)
  959. {
  960. struct its_cmd_desc desc;
  961. desc.its_inv_cmd.dev = dev;
  962. desc.its_inv_cmd.event_id = event_id;
  963. its_send_single_command(dev->its, its_build_inv_cmd, &desc);
  964. }
  965. static void its_send_mapd(struct its_device *dev, int valid)
  966. {
  967. struct its_cmd_desc desc;
  968. desc.its_mapd_cmd.dev = dev;
  969. desc.its_mapd_cmd.valid = !!valid;
  970. its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
  971. }
  972. static void its_send_mapc(struct its_node *its, struct its_collection *col,
  973. int valid)
  974. {
  975. struct its_cmd_desc desc;
  976. desc.its_mapc_cmd.col = col;
  977. desc.its_mapc_cmd.valid = !!valid;
  978. its_send_single_command(its, its_build_mapc_cmd, &desc);
  979. }
  980. static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
  981. {
  982. struct its_cmd_desc desc;
  983. desc.its_mapti_cmd.dev = dev;
  984. desc.its_mapti_cmd.phys_id = irq_id;
  985. desc.its_mapti_cmd.event_id = id;
  986. its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
  987. }
  988. static void its_send_movi(struct its_device *dev,
  989. struct its_collection *col, u32 id)
  990. {
  991. struct its_cmd_desc desc;
  992. desc.its_movi_cmd.dev = dev;
  993. desc.its_movi_cmd.col = col;
  994. desc.its_movi_cmd.event_id = id;
  995. its_send_single_command(dev->its, its_build_movi_cmd, &desc);
  996. }
  997. static void its_send_discard(struct its_device *dev, u32 id)
  998. {
  999. struct its_cmd_desc desc;
  1000. desc.its_discard_cmd.dev = dev;
  1001. desc.its_discard_cmd.event_id = id;
  1002. its_send_single_command(dev->its, its_build_discard_cmd, &desc);
  1003. }
  1004. static void its_send_invall(struct its_node *its, struct its_collection *col)
  1005. {
  1006. struct its_cmd_desc desc;
  1007. desc.its_invall_cmd.col = col;
  1008. its_send_single_command(its, its_build_invall_cmd, &desc);
  1009. }
  1010. static void its_send_vmapti(struct its_device *dev, u32 id)
  1011. {
  1012. struct its_vlpi_map *map = dev_event_to_vlpi_map(dev, id);
  1013. struct its_cmd_desc desc;
  1014. desc.its_vmapti_cmd.vpe = map->vpe;
  1015. desc.its_vmapti_cmd.dev = dev;
  1016. desc.its_vmapti_cmd.virt_id = map->vintid;
  1017. desc.its_vmapti_cmd.event_id = id;
  1018. desc.its_vmapti_cmd.db_enabled = map->db_enabled;
  1019. its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
  1020. }
  1021. static void its_send_vmovi(struct its_device *dev, u32 id)
  1022. {
  1023. struct its_vlpi_map *map = dev_event_to_vlpi_map(dev, id);
  1024. struct its_cmd_desc desc;
  1025. desc.its_vmovi_cmd.vpe = map->vpe;
  1026. desc.its_vmovi_cmd.dev = dev;
  1027. desc.its_vmovi_cmd.event_id = id;
  1028. desc.its_vmovi_cmd.db_enabled = map->db_enabled;
  1029. its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
  1030. }
  1031. static void its_send_vmapp(struct its_node *its,
  1032. struct its_vpe *vpe, bool valid)
  1033. {
  1034. struct its_cmd_desc desc;
  1035. desc.its_vmapp_cmd.vpe = vpe;
  1036. desc.its_vmapp_cmd.valid = valid;
  1037. desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
  1038. its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
  1039. }
  1040. static void its_send_vmovp(struct its_vpe *vpe)
  1041. {
  1042. struct its_cmd_desc desc = {};
  1043. struct its_node *its;
  1044. unsigned long flags;
  1045. int col_id = vpe->col_idx;
  1046. desc.its_vmovp_cmd.vpe = vpe;
  1047. if (!its_list_map) {
  1048. its = list_first_entry(&its_nodes, struct its_node, entry);
  1049. desc.its_vmovp_cmd.col = &its->collections[col_id];
  1050. its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
  1051. return;
  1052. }
  1053. /*
  1054. * Yet another marvel of the architecture. If using the
  1055. * its_list "feature", we need to make sure that all ITSs
  1056. * receive all VMOVP commands in the same order. The only way
  1057. * to guarantee this is to make vmovp a serialization point.
  1058. *
  1059. * Wall <-- Head.
  1060. */
  1061. raw_spin_lock_irqsave(&vmovp_lock, flags);
  1062. desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
  1063. desc.its_vmovp_cmd.its_list = get_its_list(vpe->its_vm);
  1064. /* Emit VMOVPs */
  1065. list_for_each_entry(its, &its_nodes, entry) {
  1066. if (!is_v4(its))
  1067. continue;
  1068. if (!require_its_list_vmovp(vpe->its_vm, its))
  1069. continue;
  1070. desc.its_vmovp_cmd.col = &its->collections[col_id];
  1071. its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
  1072. }
  1073. raw_spin_unlock_irqrestore(&vmovp_lock, flags);
  1074. }
  1075. static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe)
  1076. {
  1077. struct its_cmd_desc desc;
  1078. desc.its_vinvall_cmd.vpe = vpe;
  1079. its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
  1080. }
  1081. static void its_send_vinv(struct its_device *dev, u32 event_id)
  1082. {
  1083. struct its_cmd_desc desc;
  1084. /*
  1085. * There is no real VINV command. This is just a normal INV,
  1086. * with a VSYNC instead of a SYNC.
  1087. */
  1088. desc.its_inv_cmd.dev = dev;
  1089. desc.its_inv_cmd.event_id = event_id;
  1090. its_send_single_vcommand(dev->its, its_build_vinv_cmd, &desc);
  1091. }
  1092. static void its_send_vint(struct its_device *dev, u32 event_id)
  1093. {
  1094. struct its_cmd_desc desc;
  1095. /*
  1096. * There is no real VINT command. This is just a normal INT,
  1097. * with a VSYNC instead of a SYNC.
  1098. */
  1099. desc.its_int_cmd.dev = dev;
  1100. desc.its_int_cmd.event_id = event_id;
  1101. its_send_single_vcommand(dev->its, its_build_vint_cmd, &desc);
  1102. }
  1103. static void its_send_vclear(struct its_device *dev, u32 event_id)
  1104. {
  1105. struct its_cmd_desc desc;
  1106. /*
  1107. * There is no real VCLEAR command. This is just a normal CLEAR,
  1108. * with a VSYNC instead of a SYNC.
  1109. */
  1110. desc.its_clear_cmd.dev = dev;
  1111. desc.its_clear_cmd.event_id = event_id;
  1112. its_send_single_vcommand(dev->its, its_build_vclear_cmd, &desc);
  1113. }
  1114. static void its_send_invdb(struct its_node *its, struct its_vpe *vpe)
  1115. {
  1116. struct its_cmd_desc desc;
  1117. desc.its_invdb_cmd.vpe = vpe;
  1118. its_send_single_vcommand(its, its_build_invdb_cmd, &desc);
  1119. }
  1120. /*
  1121. * irqchip functions - assumes MSI, mostly.
  1122. */
  1123. static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
  1124. {
  1125. struct its_vlpi_map *map = get_vlpi_map(d);
  1126. irq_hw_number_t hwirq;
  1127. void *va;
  1128. u8 *cfg;
  1129. if (map) {
  1130. va = page_address(map->vm->vprop_page);
  1131. hwirq = map->vintid;
  1132. /* Remember the updated property */
  1133. map->properties &= ~clr;
  1134. map->properties |= set | LPI_PROP_GROUP1;
  1135. } else {
  1136. va = gic_rdists->prop_table_va;
  1137. hwirq = d->hwirq;
  1138. }
  1139. cfg = va + hwirq - 8192;
  1140. *cfg &= ~clr;
  1141. *cfg |= set | LPI_PROP_GROUP1;
  1142. /*
  1143. * Make the above write visible to the redistributors.
  1144. * And yes, we're flushing exactly: One. Single. Byte.
  1145. * Humpf...
  1146. */
  1147. if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
  1148. gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
  1149. else
  1150. dsb(ishst);
  1151. }
  1152. static void wait_for_syncr(void __iomem *rdbase)
  1153. {
  1154. while (readl_relaxed(rdbase + GICR_SYNCR) & 1)
  1155. cpu_relax();
  1156. }
  1157. static void __direct_lpi_inv(struct irq_data *d, u64 val)
  1158. {
  1159. void __iomem *rdbase;
  1160. unsigned long flags;
  1161. int cpu;
  1162. /* Target the redistributor this LPI is currently routed to */
  1163. cpu = irq_to_cpuid_lock(d, &flags);
  1164. raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock);
  1165. rdbase = per_cpu_ptr(gic_rdists->rdist, cpu)->rd_base;
  1166. gic_write_lpir(val, rdbase + GICR_INVLPIR);
  1167. wait_for_syncr(rdbase);
  1168. raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock);
  1169. irq_to_cpuid_unlock(d, flags);
  1170. }
  1171. static void direct_lpi_inv(struct irq_data *d)
  1172. {
  1173. struct its_vlpi_map *map = get_vlpi_map(d);
  1174. u64 val;
  1175. if (map) {
  1176. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1177. WARN_ON(!is_v4_1(its_dev->its));
  1178. val = GICR_INVLPIR_V;
  1179. val |= FIELD_PREP(GICR_INVLPIR_VPEID, map->vpe->vpe_id);
  1180. val |= FIELD_PREP(GICR_INVLPIR_INTID, map->vintid);
  1181. } else {
  1182. val = d->hwirq;
  1183. }
  1184. __direct_lpi_inv(d, val);
  1185. }
  1186. static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
  1187. {
  1188. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1189. lpi_write_config(d, clr, set);
  1190. if (gic_rdists->has_direct_lpi &&
  1191. (is_v4_1(its_dev->its) || !irqd_is_forwarded_to_vcpu(d)))
  1192. direct_lpi_inv(d);
  1193. else if (!irqd_is_forwarded_to_vcpu(d))
  1194. its_send_inv(its_dev, its_get_event_id(d));
  1195. else
  1196. its_send_vinv(its_dev, its_get_event_id(d));
  1197. }
  1198. static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
  1199. {
  1200. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1201. u32 event = its_get_event_id(d);
  1202. struct its_vlpi_map *map;
  1203. /*
  1204. * GICv4.1 does away with the per-LPI nonsense, nothing to do
  1205. * here.
  1206. */
  1207. if (is_v4_1(its_dev->its))
  1208. return;
  1209. map = dev_event_to_vlpi_map(its_dev, event);
  1210. if (map->db_enabled == enable)
  1211. return;
  1212. map->db_enabled = enable;
  1213. /*
  1214. * More fun with the architecture:
  1215. *
  1216. * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
  1217. * value or to 1023, depending on the enable bit. But that
  1218. * would be issuing a mapping for an /existing/ DevID+EventID
  1219. * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
  1220. * to the /same/ vPE, using this opportunity to adjust the
  1221. * doorbell. Mouahahahaha. We loves it, Precious.
  1222. */
  1223. its_send_vmovi(its_dev, event);
  1224. }
  1225. static void its_mask_irq(struct irq_data *d)
  1226. {
  1227. if (irqd_is_forwarded_to_vcpu(d))
  1228. its_vlpi_set_doorbell(d, false);
  1229. lpi_update_config(d, LPI_PROP_ENABLED, 0);
  1230. }
  1231. static void its_unmask_irq(struct irq_data *d)
  1232. {
  1233. if (irqd_is_forwarded_to_vcpu(d))
  1234. its_vlpi_set_doorbell(d, true);
  1235. lpi_update_config(d, 0, LPI_PROP_ENABLED);
  1236. }
  1237. static __maybe_unused u32 its_read_lpi_count(struct irq_data *d, int cpu)
  1238. {
  1239. if (irqd_affinity_is_managed(d))
  1240. return atomic_read(&per_cpu_ptr(&cpu_lpi_count, cpu)->managed);
  1241. return atomic_read(&per_cpu_ptr(&cpu_lpi_count, cpu)->unmanaged);
  1242. }
  1243. static void its_inc_lpi_count(struct irq_data *d, int cpu)
  1244. {
  1245. if (irqd_affinity_is_managed(d))
  1246. atomic_inc(&per_cpu_ptr(&cpu_lpi_count, cpu)->managed);
  1247. else
  1248. atomic_inc(&per_cpu_ptr(&cpu_lpi_count, cpu)->unmanaged);
  1249. }
  1250. static void its_dec_lpi_count(struct irq_data *d, int cpu)
  1251. {
  1252. if (irqd_affinity_is_managed(d))
  1253. atomic_dec(&per_cpu_ptr(&cpu_lpi_count, cpu)->managed);
  1254. else
  1255. atomic_dec(&per_cpu_ptr(&cpu_lpi_count, cpu)->unmanaged);
  1256. }
  1257. static unsigned int cpumask_pick_least_loaded(struct irq_data *d,
  1258. const struct cpumask *cpu_mask)
  1259. {
  1260. unsigned int cpu = nr_cpu_ids, tmp;
  1261. int count = S32_MAX;
  1262. for_each_cpu(tmp, cpu_mask) {
  1263. int this_count = its_read_lpi_count(d, tmp);
  1264. if (this_count < count) {
  1265. cpu = tmp;
  1266. count = this_count;
  1267. }
  1268. }
  1269. return cpu;
  1270. }
  1271. /*
  1272. * As suggested by Thomas Gleixner in:
  1273. * https://lore.kernel.org/r/[email protected]
  1274. */
  1275. static int its_select_cpu(struct irq_data *d,
  1276. const struct cpumask *aff_mask)
  1277. {
  1278. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1279. static DEFINE_RAW_SPINLOCK(tmpmask_lock);
  1280. static struct cpumask __tmpmask;
  1281. struct cpumask *tmpmask;
  1282. unsigned long flags;
  1283. int cpu, node;
  1284. node = its_dev->its->numa_node;
  1285. tmpmask = &__tmpmask;
  1286. raw_spin_lock_irqsave(&tmpmask_lock, flags);
  1287. if (!irqd_affinity_is_managed(d)) {
  1288. /* First try the NUMA node */
  1289. if (node != NUMA_NO_NODE) {
  1290. /*
  1291. * Try the intersection of the affinity mask and the
  1292. * node mask (and the online mask, just to be safe).
  1293. */
  1294. cpumask_and(tmpmask, cpumask_of_node(node), aff_mask);
  1295. cpumask_and(tmpmask, tmpmask, cpu_online_mask);
  1296. /*
  1297. * Ideally, we would check if the mask is empty, and
  1298. * try again on the full node here.
  1299. *
  1300. * But it turns out that the way ACPI describes the
  1301. * affinity for ITSs only deals about memory, and
  1302. * not target CPUs, so it cannot describe a single
  1303. * ITS placed next to two NUMA nodes.
  1304. *
  1305. * Instead, just fallback on the online mask. This
  1306. * diverges from Thomas' suggestion above.
  1307. */
  1308. cpu = cpumask_pick_least_loaded(d, tmpmask);
  1309. if (cpu < nr_cpu_ids)
  1310. goto out;
  1311. /* If we can't cross sockets, give up */
  1312. if ((its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144))
  1313. goto out;
  1314. /* If the above failed, expand the search */
  1315. }
  1316. /* Try the intersection of the affinity and online masks */
  1317. cpumask_and(tmpmask, aff_mask, cpu_online_mask);
  1318. /* If that doesn't fly, the online mask is the last resort */
  1319. if (cpumask_empty(tmpmask))
  1320. cpumask_copy(tmpmask, cpu_online_mask);
  1321. cpu = cpumask_pick_least_loaded(d, tmpmask);
  1322. } else {
  1323. cpumask_copy(tmpmask, aff_mask);
  1324. /* If we cannot cross sockets, limit the search to that node */
  1325. if ((its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) &&
  1326. node != NUMA_NO_NODE)
  1327. cpumask_and(tmpmask, tmpmask, cpumask_of_node(node));
  1328. cpu = cpumask_pick_least_loaded(d, tmpmask);
  1329. }
  1330. out:
  1331. raw_spin_unlock_irqrestore(&tmpmask_lock, flags);
  1332. pr_debug("IRQ%d -> %*pbl CPU%d\n", d->irq, cpumask_pr_args(aff_mask), cpu);
  1333. return cpu;
  1334. }
  1335. static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  1336. bool force)
  1337. {
  1338. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1339. struct its_collection *target_col;
  1340. u32 id = its_get_event_id(d);
  1341. int cpu, prev_cpu;
  1342. /* A forwarded interrupt should use irq_set_vcpu_affinity */
  1343. if (irqd_is_forwarded_to_vcpu(d))
  1344. return -EINVAL;
  1345. prev_cpu = its_dev->event_map.col_map[id];
  1346. its_dec_lpi_count(d, prev_cpu);
  1347. if (!force)
  1348. cpu = its_select_cpu(d, mask_val);
  1349. else
  1350. cpu = cpumask_pick_least_loaded(d, mask_val);
  1351. if (cpu < 0 || cpu >= nr_cpu_ids)
  1352. goto err;
  1353. /* don't set the affinity when the target cpu is same as current one */
  1354. if (cpu != prev_cpu) {
  1355. target_col = &its_dev->its->collections[cpu];
  1356. its_send_movi(its_dev, target_col, id);
  1357. its_dev->event_map.col_map[id] = cpu;
  1358. irq_data_update_effective_affinity(d, cpumask_of(cpu));
  1359. }
  1360. its_inc_lpi_count(d, cpu);
  1361. return IRQ_SET_MASK_OK_DONE;
  1362. err:
  1363. its_inc_lpi_count(d, prev_cpu);
  1364. return -EINVAL;
  1365. }
  1366. static u64 its_irq_get_msi_base(struct its_device *its_dev)
  1367. {
  1368. struct its_node *its = its_dev->its;
  1369. return its->phys_base + GITS_TRANSLATER;
  1370. }
  1371. static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
  1372. {
  1373. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1374. struct its_node *its;
  1375. u64 addr;
  1376. its = its_dev->its;
  1377. addr = its->get_msi_base(its_dev);
  1378. msg->address_lo = lower_32_bits(addr);
  1379. msg->address_hi = upper_32_bits(addr);
  1380. msg->data = its_get_event_id(d);
  1381. iommu_dma_compose_msi_msg(irq_data_get_msi_desc(d), msg);
  1382. }
  1383. static int its_irq_set_irqchip_state(struct irq_data *d,
  1384. enum irqchip_irq_state which,
  1385. bool state)
  1386. {
  1387. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1388. u32 event = its_get_event_id(d);
  1389. if (which != IRQCHIP_STATE_PENDING)
  1390. return -EINVAL;
  1391. if (irqd_is_forwarded_to_vcpu(d)) {
  1392. if (state)
  1393. its_send_vint(its_dev, event);
  1394. else
  1395. its_send_vclear(its_dev, event);
  1396. } else {
  1397. if (state)
  1398. its_send_int(its_dev, event);
  1399. else
  1400. its_send_clear(its_dev, event);
  1401. }
  1402. return 0;
  1403. }
  1404. static int its_irq_retrigger(struct irq_data *d)
  1405. {
  1406. return !its_irq_set_irqchip_state(d, IRQCHIP_STATE_PENDING, true);
  1407. }
  1408. /*
  1409. * Two favourable cases:
  1410. *
  1411. * (a) Either we have a GICv4.1, and all vPEs have to be mapped at all times
  1412. * for vSGI delivery
  1413. *
  1414. * (b) Or the ITSs do not use a list map, meaning that VMOVP is cheap enough
  1415. * and we're better off mapping all VPEs always
  1416. *
  1417. * If neither (a) nor (b) is true, then we map vPEs on demand.
  1418. *
  1419. */
  1420. static bool gic_requires_eager_mapping(void)
  1421. {
  1422. if (!its_list_map || gic_rdists->has_rvpeid)
  1423. return true;
  1424. return false;
  1425. }
  1426. static void its_map_vm(struct its_node *its, struct its_vm *vm)
  1427. {
  1428. unsigned long flags;
  1429. if (gic_requires_eager_mapping())
  1430. return;
  1431. raw_spin_lock_irqsave(&vmovp_lock, flags);
  1432. /*
  1433. * If the VM wasn't mapped yet, iterate over the vpes and get
  1434. * them mapped now.
  1435. */
  1436. vm->vlpi_count[its->list_nr]++;
  1437. if (vm->vlpi_count[its->list_nr] == 1) {
  1438. int i;
  1439. for (i = 0; i < vm->nr_vpes; i++) {
  1440. struct its_vpe *vpe = vm->vpes[i];
  1441. struct irq_data *d = irq_get_irq_data(vpe->irq);
  1442. /* Map the VPE to the first possible CPU */
  1443. vpe->col_idx = cpumask_first(cpu_online_mask);
  1444. its_send_vmapp(its, vpe, true);
  1445. its_send_vinvall(its, vpe);
  1446. irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
  1447. }
  1448. }
  1449. raw_spin_unlock_irqrestore(&vmovp_lock, flags);
  1450. }
  1451. static void its_unmap_vm(struct its_node *its, struct its_vm *vm)
  1452. {
  1453. unsigned long flags;
  1454. /* Not using the ITS list? Everything is always mapped. */
  1455. if (gic_requires_eager_mapping())
  1456. return;
  1457. raw_spin_lock_irqsave(&vmovp_lock, flags);
  1458. if (!--vm->vlpi_count[its->list_nr]) {
  1459. int i;
  1460. for (i = 0; i < vm->nr_vpes; i++)
  1461. its_send_vmapp(its, vm->vpes[i], false);
  1462. }
  1463. raw_spin_unlock_irqrestore(&vmovp_lock, flags);
  1464. }
  1465. static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
  1466. {
  1467. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1468. u32 event = its_get_event_id(d);
  1469. int ret = 0;
  1470. if (!info->map)
  1471. return -EINVAL;
  1472. raw_spin_lock(&its_dev->event_map.vlpi_lock);
  1473. if (!its_dev->event_map.vm) {
  1474. struct its_vlpi_map *maps;
  1475. maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps),
  1476. GFP_ATOMIC);
  1477. if (!maps) {
  1478. ret = -ENOMEM;
  1479. goto out;
  1480. }
  1481. its_dev->event_map.vm = info->map->vm;
  1482. its_dev->event_map.vlpi_maps = maps;
  1483. } else if (its_dev->event_map.vm != info->map->vm) {
  1484. ret = -EINVAL;
  1485. goto out;
  1486. }
  1487. /* Get our private copy of the mapping information */
  1488. its_dev->event_map.vlpi_maps[event] = *info->map;
  1489. if (irqd_is_forwarded_to_vcpu(d)) {
  1490. /* Already mapped, move it around */
  1491. its_send_vmovi(its_dev, event);
  1492. } else {
  1493. /* Ensure all the VPEs are mapped on this ITS */
  1494. its_map_vm(its_dev->its, info->map->vm);
  1495. /*
  1496. * Flag the interrupt as forwarded so that we can
  1497. * start poking the virtual property table.
  1498. */
  1499. irqd_set_forwarded_to_vcpu(d);
  1500. /* Write out the property to the prop table */
  1501. lpi_write_config(d, 0xff, info->map->properties);
  1502. /* Drop the physical mapping */
  1503. its_send_discard(its_dev, event);
  1504. /* and install the virtual one */
  1505. its_send_vmapti(its_dev, event);
  1506. /* Increment the number of VLPIs */
  1507. its_dev->event_map.nr_vlpis++;
  1508. }
  1509. out:
  1510. raw_spin_unlock(&its_dev->event_map.vlpi_lock);
  1511. return ret;
  1512. }
  1513. static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
  1514. {
  1515. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1516. struct its_vlpi_map *map;
  1517. int ret = 0;
  1518. raw_spin_lock(&its_dev->event_map.vlpi_lock);
  1519. map = get_vlpi_map(d);
  1520. if (!its_dev->event_map.vm || !map) {
  1521. ret = -EINVAL;
  1522. goto out;
  1523. }
  1524. /* Copy our mapping information to the incoming request */
  1525. *info->map = *map;
  1526. out:
  1527. raw_spin_unlock(&its_dev->event_map.vlpi_lock);
  1528. return ret;
  1529. }
  1530. static int its_vlpi_unmap(struct irq_data *d)
  1531. {
  1532. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1533. u32 event = its_get_event_id(d);
  1534. int ret = 0;
  1535. raw_spin_lock(&its_dev->event_map.vlpi_lock);
  1536. if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
  1537. ret = -EINVAL;
  1538. goto out;
  1539. }
  1540. /* Drop the virtual mapping */
  1541. its_send_discard(its_dev, event);
  1542. /* and restore the physical one */
  1543. irqd_clr_forwarded_to_vcpu(d);
  1544. its_send_mapti(its_dev, d->hwirq, event);
  1545. lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
  1546. LPI_PROP_ENABLED |
  1547. LPI_PROP_GROUP1));
  1548. /* Potentially unmap the VM from this ITS */
  1549. its_unmap_vm(its_dev->its, its_dev->event_map.vm);
  1550. /*
  1551. * Drop the refcount and make the device available again if
  1552. * this was the last VLPI.
  1553. */
  1554. if (!--its_dev->event_map.nr_vlpis) {
  1555. its_dev->event_map.vm = NULL;
  1556. kfree(its_dev->event_map.vlpi_maps);
  1557. }
  1558. out:
  1559. raw_spin_unlock(&its_dev->event_map.vlpi_lock);
  1560. return ret;
  1561. }
  1562. static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
  1563. {
  1564. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1565. if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
  1566. return -EINVAL;
  1567. if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
  1568. lpi_update_config(d, 0xff, info->config);
  1569. else
  1570. lpi_write_config(d, 0xff, info->config);
  1571. its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
  1572. return 0;
  1573. }
  1574. static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
  1575. {
  1576. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  1577. struct its_cmd_info *info = vcpu_info;
  1578. /* Need a v4 ITS */
  1579. if (!is_v4(its_dev->its))
  1580. return -EINVAL;
  1581. /* Unmap request? */
  1582. if (!info)
  1583. return its_vlpi_unmap(d);
  1584. switch (info->cmd_type) {
  1585. case MAP_VLPI:
  1586. return its_vlpi_map(d, info);
  1587. case GET_VLPI:
  1588. return its_vlpi_get(d, info);
  1589. case PROP_UPDATE_VLPI:
  1590. case PROP_UPDATE_AND_INV_VLPI:
  1591. return its_vlpi_prop_update(d, info);
  1592. default:
  1593. return -EINVAL;
  1594. }
  1595. }
  1596. static struct irq_chip its_irq_chip = {
  1597. .name = "ITS",
  1598. .irq_mask = its_mask_irq,
  1599. .irq_unmask = its_unmask_irq,
  1600. .irq_eoi = irq_chip_eoi_parent,
  1601. .irq_set_affinity = its_set_affinity,
  1602. .irq_compose_msi_msg = its_irq_compose_msi_msg,
  1603. .irq_set_irqchip_state = its_irq_set_irqchip_state,
  1604. .irq_retrigger = its_irq_retrigger,
  1605. .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
  1606. };
  1607. /*
  1608. * How we allocate LPIs:
  1609. *
  1610. * lpi_range_list contains ranges of LPIs that are to available to
  1611. * allocate from. To allocate LPIs, just pick the first range that
  1612. * fits the required allocation, and reduce it by the required
  1613. * amount. Once empty, remove the range from the list.
  1614. *
  1615. * To free a range of LPIs, add a free range to the list, sort it and
  1616. * merge the result if the new range happens to be adjacent to an
  1617. * already free block.
  1618. *
  1619. * The consequence of the above is that allocation is cost is low, but
  1620. * freeing is expensive. We assumes that freeing rarely occurs.
  1621. */
  1622. #define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
  1623. static DEFINE_MUTEX(lpi_range_lock);
  1624. static LIST_HEAD(lpi_range_list);
  1625. struct lpi_range {
  1626. struct list_head entry;
  1627. u32 base_id;
  1628. u32 span;
  1629. };
  1630. static struct lpi_range *mk_lpi_range(u32 base, u32 span)
  1631. {
  1632. struct lpi_range *range;
  1633. range = kmalloc(sizeof(*range), GFP_KERNEL);
  1634. if (range) {
  1635. range->base_id = base;
  1636. range->span = span;
  1637. }
  1638. return range;
  1639. }
  1640. static int alloc_lpi_range(u32 nr_lpis, u32 *base)
  1641. {
  1642. struct lpi_range *range, *tmp;
  1643. int err = -ENOSPC;
  1644. mutex_lock(&lpi_range_lock);
  1645. list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
  1646. if (range->span >= nr_lpis) {
  1647. *base = range->base_id;
  1648. range->base_id += nr_lpis;
  1649. range->span -= nr_lpis;
  1650. if (range->span == 0) {
  1651. list_del(&range->entry);
  1652. kfree(range);
  1653. }
  1654. err = 0;
  1655. break;
  1656. }
  1657. }
  1658. mutex_unlock(&lpi_range_lock);
  1659. pr_debug("ITS: alloc %u:%u\n", *base, nr_lpis);
  1660. return err;
  1661. }
  1662. static void merge_lpi_ranges(struct lpi_range *a, struct lpi_range *b)
  1663. {
  1664. if (&a->entry == &lpi_range_list || &b->entry == &lpi_range_list)
  1665. return;
  1666. if (a->base_id + a->span != b->base_id)
  1667. return;
  1668. b->base_id = a->base_id;
  1669. b->span += a->span;
  1670. list_del(&a->entry);
  1671. kfree(a);
  1672. }
  1673. static int free_lpi_range(u32 base, u32 nr_lpis)
  1674. {
  1675. struct lpi_range *new, *old;
  1676. new = mk_lpi_range(base, nr_lpis);
  1677. if (!new)
  1678. return -ENOMEM;
  1679. mutex_lock(&lpi_range_lock);
  1680. list_for_each_entry_reverse(old, &lpi_range_list, entry) {
  1681. if (old->base_id < base)
  1682. break;
  1683. }
  1684. /*
  1685. * old is the last element with ->base_id smaller than base,
  1686. * so new goes right after it. If there are no elements with
  1687. * ->base_id smaller than base, &old->entry ends up pointing
  1688. * at the head of the list, and inserting new it the start of
  1689. * the list is the right thing to do in that case as well.
  1690. */
  1691. list_add(&new->entry, &old->entry);
  1692. /*
  1693. * Now check if we can merge with the preceding and/or
  1694. * following ranges.
  1695. */
  1696. merge_lpi_ranges(old, new);
  1697. merge_lpi_ranges(new, list_next_entry(new, entry));
  1698. mutex_unlock(&lpi_range_lock);
  1699. return 0;
  1700. }
  1701. static int __init its_lpi_init(u32 id_bits)
  1702. {
  1703. u32 lpis = (1UL << id_bits) - 8192;
  1704. u32 numlpis;
  1705. int err;
  1706. numlpis = 1UL << GICD_TYPER_NUM_LPIS(gic_rdists->gicd_typer);
  1707. if (numlpis > 2 && !WARN_ON(numlpis > lpis)) {
  1708. lpis = numlpis;
  1709. pr_info("ITS: Using hypervisor restricted LPI range [%u]\n",
  1710. lpis);
  1711. }
  1712. /*
  1713. * Initializing the allocator is just the same as freeing the
  1714. * full range of LPIs.
  1715. */
  1716. err = free_lpi_range(8192, lpis);
  1717. pr_debug("ITS: Allocator initialized for %u LPIs\n", lpis);
  1718. return err;
  1719. }
  1720. static unsigned long *its_lpi_alloc(int nr_irqs, u32 *base, int *nr_ids)
  1721. {
  1722. unsigned long *bitmap = NULL;
  1723. int err = 0;
  1724. do {
  1725. err = alloc_lpi_range(nr_irqs, base);
  1726. if (!err)
  1727. break;
  1728. nr_irqs /= 2;
  1729. } while (nr_irqs > 0);
  1730. if (!nr_irqs)
  1731. err = -ENOSPC;
  1732. if (err)
  1733. goto out;
  1734. bitmap = bitmap_zalloc(nr_irqs, GFP_ATOMIC);
  1735. if (!bitmap)
  1736. goto out;
  1737. *nr_ids = nr_irqs;
  1738. out:
  1739. if (!bitmap)
  1740. *base = *nr_ids = 0;
  1741. return bitmap;
  1742. }
  1743. static void its_lpi_free(unsigned long *bitmap, u32 base, u32 nr_ids)
  1744. {
  1745. WARN_ON(free_lpi_range(base, nr_ids));
  1746. bitmap_free(bitmap);
  1747. }
  1748. static void gic_reset_prop_table(void *va)
  1749. {
  1750. /* Priority 0xa0, Group-1, disabled */
  1751. memset(va, LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1, LPI_PROPBASE_SZ);
  1752. /* Make sure the GIC will observe the written configuration */
  1753. gic_flush_dcache_to_poc(va, LPI_PROPBASE_SZ);
  1754. }
  1755. static struct page *its_allocate_prop_table(gfp_t gfp_flags)
  1756. {
  1757. struct page *prop_page;
  1758. prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
  1759. if (!prop_page)
  1760. return NULL;
  1761. gic_reset_prop_table(page_address(prop_page));
  1762. return prop_page;
  1763. }
  1764. static void its_free_prop_table(struct page *prop_page)
  1765. {
  1766. free_pages((unsigned long)page_address(prop_page),
  1767. get_order(LPI_PROPBASE_SZ));
  1768. }
  1769. static bool gic_check_reserved_range(phys_addr_t addr, unsigned long size)
  1770. {
  1771. phys_addr_t start, end, addr_end;
  1772. u64 i;
  1773. /*
  1774. * We don't bother checking for a kdump kernel as by
  1775. * construction, the LPI tables are out of this kernel's
  1776. * memory map.
  1777. */
  1778. if (is_kdump_kernel())
  1779. return true;
  1780. addr_end = addr + size - 1;
  1781. for_each_reserved_mem_range(i, &start, &end) {
  1782. if (addr >= start && addr_end <= end)
  1783. return true;
  1784. }
  1785. /* Not found, not a good sign... */
  1786. pr_warn("GICv3: Expected reserved range [%pa:%pa], not found\n",
  1787. &addr, &addr_end);
  1788. add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
  1789. return false;
  1790. }
  1791. static int gic_reserve_range(phys_addr_t addr, unsigned long size)
  1792. {
  1793. if (efi_enabled(EFI_CONFIG_TABLES))
  1794. return efi_mem_reserve_persistent(addr, size);
  1795. return 0;
  1796. }
  1797. static int __init its_setup_lpi_prop_table(void)
  1798. {
  1799. if (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) {
  1800. u64 val;
  1801. val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
  1802. lpi_id_bits = (val & GICR_PROPBASER_IDBITS_MASK) + 1;
  1803. gic_rdists->prop_table_pa = val & GENMASK_ULL(51, 12);
  1804. gic_rdists->prop_table_va = memremap(gic_rdists->prop_table_pa,
  1805. LPI_PROPBASE_SZ,
  1806. MEMREMAP_WB);
  1807. gic_reset_prop_table(gic_rdists->prop_table_va);
  1808. } else {
  1809. struct page *page;
  1810. lpi_id_bits = min_t(u32,
  1811. GICD_TYPER_ID_BITS(gic_rdists->gicd_typer),
  1812. ITS_MAX_LPI_NRBITS);
  1813. page = its_allocate_prop_table(GFP_NOWAIT);
  1814. if (!page) {
  1815. pr_err("Failed to allocate PROPBASE\n");
  1816. return -ENOMEM;
  1817. }
  1818. gic_rdists->prop_table_pa = page_to_phys(page);
  1819. gic_rdists->prop_table_va = page_address(page);
  1820. WARN_ON(gic_reserve_range(gic_rdists->prop_table_pa,
  1821. LPI_PROPBASE_SZ));
  1822. }
  1823. pr_info("GICv3: using LPI property table @%pa\n",
  1824. &gic_rdists->prop_table_pa);
  1825. return its_lpi_init(lpi_id_bits);
  1826. }
  1827. static const char *its_base_type_string[] = {
  1828. [GITS_BASER_TYPE_DEVICE] = "Devices",
  1829. [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
  1830. [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
  1831. [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
  1832. [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
  1833. [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
  1834. [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
  1835. };
  1836. static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
  1837. {
  1838. u32 idx = baser - its->tables;
  1839. return gits_read_baser(its->base + GITS_BASER + (idx << 3));
  1840. }
  1841. static void its_write_baser(struct its_node *its, struct its_baser *baser,
  1842. u64 val)
  1843. {
  1844. u32 idx = baser - its->tables;
  1845. gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
  1846. baser->val = its_read_baser(its, baser);
  1847. }
  1848. static int its_setup_baser(struct its_node *its, struct its_baser *baser,
  1849. u64 cache, u64 shr, u32 order, bool indirect)
  1850. {
  1851. u64 val = its_read_baser(its, baser);
  1852. u64 esz = GITS_BASER_ENTRY_SIZE(val);
  1853. u64 type = GITS_BASER_TYPE(val);
  1854. u64 baser_phys, tmp;
  1855. u32 alloc_pages, psz;
  1856. struct page *page;
  1857. void *base;
  1858. psz = baser->psz;
  1859. alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
  1860. if (alloc_pages > GITS_BASER_PAGES_MAX) {
  1861. pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
  1862. &its->phys_base, its_base_type_string[type],
  1863. alloc_pages, GITS_BASER_PAGES_MAX);
  1864. alloc_pages = GITS_BASER_PAGES_MAX;
  1865. order = get_order(GITS_BASER_PAGES_MAX * psz);
  1866. }
  1867. page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, order);
  1868. if (!page)
  1869. return -ENOMEM;
  1870. base = (void *)page_address(page);
  1871. baser_phys = virt_to_phys(base);
  1872. /* Check if the physical address of the memory is above 48bits */
  1873. if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
  1874. /* 52bit PA is supported only when PageSize=64K */
  1875. if (psz != SZ_64K) {
  1876. pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
  1877. free_pages((unsigned long)base, order);
  1878. return -ENXIO;
  1879. }
  1880. /* Convert 52bit PA to 48bit field */
  1881. baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
  1882. }
  1883. retry_baser:
  1884. val = (baser_phys |
  1885. (type << GITS_BASER_TYPE_SHIFT) |
  1886. ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
  1887. ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
  1888. cache |
  1889. shr |
  1890. GITS_BASER_VALID);
  1891. val |= indirect ? GITS_BASER_INDIRECT : 0x0;
  1892. switch (psz) {
  1893. case SZ_4K:
  1894. val |= GITS_BASER_PAGE_SIZE_4K;
  1895. break;
  1896. case SZ_16K:
  1897. val |= GITS_BASER_PAGE_SIZE_16K;
  1898. break;
  1899. case SZ_64K:
  1900. val |= GITS_BASER_PAGE_SIZE_64K;
  1901. break;
  1902. }
  1903. its_write_baser(its, baser, val);
  1904. tmp = baser->val;
  1905. if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
  1906. /*
  1907. * Shareability didn't stick. Just use
  1908. * whatever the read reported, which is likely
  1909. * to be the only thing this redistributor
  1910. * supports. If that's zero, make it
  1911. * non-cacheable as well.
  1912. */
  1913. shr = tmp & GITS_BASER_SHAREABILITY_MASK;
  1914. if (!shr) {
  1915. cache = GITS_BASER_nC;
  1916. gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
  1917. }
  1918. goto retry_baser;
  1919. }
  1920. if (val != tmp) {
  1921. pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
  1922. &its->phys_base, its_base_type_string[type],
  1923. val, tmp);
  1924. free_pages((unsigned long)base, order);
  1925. return -ENXIO;
  1926. }
  1927. baser->order = order;
  1928. baser->base = base;
  1929. baser->psz = psz;
  1930. tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
  1931. pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
  1932. &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
  1933. its_base_type_string[type],
  1934. (unsigned long)virt_to_phys(base),
  1935. indirect ? "indirect" : "flat", (int)esz,
  1936. psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
  1937. return 0;
  1938. }
  1939. static bool its_parse_indirect_baser(struct its_node *its,
  1940. struct its_baser *baser,
  1941. u32 *order, u32 ids)
  1942. {
  1943. u64 tmp = its_read_baser(its, baser);
  1944. u64 type = GITS_BASER_TYPE(tmp);
  1945. u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
  1946. u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
  1947. u32 new_order = *order;
  1948. u32 psz = baser->psz;
  1949. bool indirect = false;
  1950. /* No need to enable Indirection if memory requirement < (psz*2)bytes */
  1951. if ((esz << ids) > (psz * 2)) {
  1952. /*
  1953. * Find out whether hw supports a single or two-level table by
  1954. * table by reading bit at offset '62' after writing '1' to it.
  1955. */
  1956. its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
  1957. indirect = !!(baser->val & GITS_BASER_INDIRECT);
  1958. if (indirect) {
  1959. /*
  1960. * The size of the lvl2 table is equal to ITS page size
  1961. * which is 'psz'. For computing lvl1 table size,
  1962. * subtract ID bits that sparse lvl2 table from 'ids'
  1963. * which is reported by ITS hardware times lvl1 table
  1964. * entry size.
  1965. */
  1966. ids -= ilog2(psz / (int)esz);
  1967. esz = GITS_LVL1_ENTRY_SIZE;
  1968. }
  1969. }
  1970. /*
  1971. * Allocate as many entries as required to fit the
  1972. * range of device IDs that the ITS can grok... The ID
  1973. * space being incredibly sparse, this results in a
  1974. * massive waste of memory if two-level device table
  1975. * feature is not supported by hardware.
  1976. */
  1977. new_order = max_t(u32, get_order(esz << ids), new_order);
  1978. if (new_order >= MAX_ORDER) {
  1979. new_order = MAX_ORDER - 1;
  1980. ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
  1981. pr_warn("ITS@%pa: %s Table too large, reduce ids %llu->%u\n",
  1982. &its->phys_base, its_base_type_string[type],
  1983. device_ids(its), ids);
  1984. }
  1985. *order = new_order;
  1986. return indirect;
  1987. }
  1988. static u32 compute_common_aff(u64 val)
  1989. {
  1990. u32 aff, clpiaff;
  1991. aff = FIELD_GET(GICR_TYPER_AFFINITY, val);
  1992. clpiaff = FIELD_GET(GICR_TYPER_COMMON_LPI_AFF, val);
  1993. return aff & ~(GENMASK(31, 0) >> (clpiaff * 8));
  1994. }
  1995. static u32 compute_its_aff(struct its_node *its)
  1996. {
  1997. u64 val;
  1998. u32 svpet;
  1999. /*
  2000. * Reencode the ITS SVPET and MPIDR as a GICR_TYPER, and compute
  2001. * the resulting affinity. We then use that to see if this match
  2002. * our own affinity.
  2003. */
  2004. svpet = FIELD_GET(GITS_TYPER_SVPET, its->typer);
  2005. val = FIELD_PREP(GICR_TYPER_COMMON_LPI_AFF, svpet);
  2006. val |= FIELD_PREP(GICR_TYPER_AFFINITY, its->mpidr);
  2007. return compute_common_aff(val);
  2008. }
  2009. static struct its_node *find_sibling_its(struct its_node *cur_its)
  2010. {
  2011. struct its_node *its;
  2012. u32 aff;
  2013. if (!FIELD_GET(GITS_TYPER_SVPET, cur_its->typer))
  2014. return NULL;
  2015. aff = compute_its_aff(cur_its);
  2016. list_for_each_entry(its, &its_nodes, entry) {
  2017. u64 baser;
  2018. if (!is_v4_1(its) || its == cur_its)
  2019. continue;
  2020. if (!FIELD_GET(GITS_TYPER_SVPET, its->typer))
  2021. continue;
  2022. if (aff != compute_its_aff(its))
  2023. continue;
  2024. /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */
  2025. baser = its->tables[2].val;
  2026. if (!(baser & GITS_BASER_VALID))
  2027. continue;
  2028. return its;
  2029. }
  2030. return NULL;
  2031. }
  2032. static void its_free_tables(struct its_node *its)
  2033. {
  2034. int i;
  2035. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  2036. if (its->tables[i].base) {
  2037. free_pages((unsigned long)its->tables[i].base,
  2038. its->tables[i].order);
  2039. its->tables[i].base = NULL;
  2040. }
  2041. }
  2042. }
  2043. static int its_probe_baser_psz(struct its_node *its, struct its_baser *baser)
  2044. {
  2045. u64 psz = SZ_64K;
  2046. while (psz) {
  2047. u64 val, gpsz;
  2048. val = its_read_baser(its, baser);
  2049. val &= ~GITS_BASER_PAGE_SIZE_MASK;
  2050. switch (psz) {
  2051. case SZ_64K:
  2052. gpsz = GITS_BASER_PAGE_SIZE_64K;
  2053. break;
  2054. case SZ_16K:
  2055. gpsz = GITS_BASER_PAGE_SIZE_16K;
  2056. break;
  2057. case SZ_4K:
  2058. default:
  2059. gpsz = GITS_BASER_PAGE_SIZE_4K;
  2060. break;
  2061. }
  2062. gpsz >>= GITS_BASER_PAGE_SIZE_SHIFT;
  2063. val |= FIELD_PREP(GITS_BASER_PAGE_SIZE_MASK, gpsz);
  2064. its_write_baser(its, baser, val);
  2065. if (FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser->val) == gpsz)
  2066. break;
  2067. switch (psz) {
  2068. case SZ_64K:
  2069. psz = SZ_16K;
  2070. break;
  2071. case SZ_16K:
  2072. psz = SZ_4K;
  2073. break;
  2074. case SZ_4K:
  2075. default:
  2076. return -1;
  2077. }
  2078. }
  2079. baser->psz = psz;
  2080. return 0;
  2081. }
  2082. static int its_alloc_tables(struct its_node *its)
  2083. {
  2084. u64 shr = GITS_BASER_InnerShareable;
  2085. u64 cache = GITS_BASER_RaWaWb;
  2086. int err, i;
  2087. if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
  2088. /* erratum 24313: ignore memory access type */
  2089. cache = GITS_BASER_nCnB;
  2090. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  2091. struct its_baser *baser = its->tables + i;
  2092. u64 val = its_read_baser(its, baser);
  2093. u64 type = GITS_BASER_TYPE(val);
  2094. bool indirect = false;
  2095. u32 order;
  2096. if (type == GITS_BASER_TYPE_NONE)
  2097. continue;
  2098. if (its_probe_baser_psz(its, baser)) {
  2099. its_free_tables(its);
  2100. return -ENXIO;
  2101. }
  2102. order = get_order(baser->psz);
  2103. switch (type) {
  2104. case GITS_BASER_TYPE_DEVICE:
  2105. indirect = its_parse_indirect_baser(its, baser, &order,
  2106. device_ids(its));
  2107. break;
  2108. case GITS_BASER_TYPE_VCPU:
  2109. if (is_v4_1(its)) {
  2110. struct its_node *sibling;
  2111. WARN_ON(i != 2);
  2112. if ((sibling = find_sibling_its(its))) {
  2113. *baser = sibling->tables[2];
  2114. its_write_baser(its, baser, baser->val);
  2115. continue;
  2116. }
  2117. }
  2118. indirect = its_parse_indirect_baser(its, baser, &order,
  2119. ITS_MAX_VPEID_BITS);
  2120. break;
  2121. }
  2122. err = its_setup_baser(its, baser, cache, shr, order, indirect);
  2123. if (err < 0) {
  2124. its_free_tables(its);
  2125. return err;
  2126. }
  2127. /* Update settings which will be used for next BASERn */
  2128. cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
  2129. shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
  2130. }
  2131. return 0;
  2132. }
  2133. static u64 inherit_vpe_l1_table_from_its(void)
  2134. {
  2135. struct its_node *its;
  2136. u64 val;
  2137. u32 aff;
  2138. val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
  2139. aff = compute_common_aff(val);
  2140. list_for_each_entry(its, &its_nodes, entry) {
  2141. u64 baser, addr;
  2142. if (!is_v4_1(its))
  2143. continue;
  2144. if (!FIELD_GET(GITS_TYPER_SVPET, its->typer))
  2145. continue;
  2146. if (aff != compute_its_aff(its))
  2147. continue;
  2148. /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */
  2149. baser = its->tables[2].val;
  2150. if (!(baser & GITS_BASER_VALID))
  2151. continue;
  2152. /* We have a winner! */
  2153. gic_data_rdist()->vpe_l1_base = its->tables[2].base;
  2154. val = GICR_VPROPBASER_4_1_VALID;
  2155. if (baser & GITS_BASER_INDIRECT)
  2156. val |= GICR_VPROPBASER_4_1_INDIRECT;
  2157. val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE,
  2158. FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser));
  2159. switch (FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser)) {
  2160. case GIC_PAGE_SIZE_64K:
  2161. addr = GITS_BASER_ADDR_48_to_52(baser);
  2162. break;
  2163. default:
  2164. addr = baser & GENMASK_ULL(47, 12);
  2165. break;
  2166. }
  2167. val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, addr >> 12);
  2168. val |= FIELD_PREP(GICR_VPROPBASER_SHAREABILITY_MASK,
  2169. FIELD_GET(GITS_BASER_SHAREABILITY_MASK, baser));
  2170. val |= FIELD_PREP(GICR_VPROPBASER_INNER_CACHEABILITY_MASK,
  2171. FIELD_GET(GITS_BASER_INNER_CACHEABILITY_MASK, baser));
  2172. val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, GITS_BASER_NR_PAGES(baser) - 1);
  2173. return val;
  2174. }
  2175. return 0;
  2176. }
  2177. static u64 inherit_vpe_l1_table_from_rd(cpumask_t **mask)
  2178. {
  2179. u32 aff;
  2180. u64 val;
  2181. int cpu;
  2182. val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
  2183. aff = compute_common_aff(val);
  2184. for_each_possible_cpu(cpu) {
  2185. void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base;
  2186. if (!base || cpu == smp_processor_id())
  2187. continue;
  2188. val = gic_read_typer(base + GICR_TYPER);
  2189. if (aff != compute_common_aff(val))
  2190. continue;
  2191. /*
  2192. * At this point, we have a victim. This particular CPU
  2193. * has already booted, and has an affinity that matches
  2194. * ours wrt CommonLPIAff. Let's use its own VPROPBASER.
  2195. * Make sure we don't write the Z bit in that case.
  2196. */
  2197. val = gicr_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER);
  2198. val &= ~GICR_VPROPBASER_4_1_Z;
  2199. gic_data_rdist()->vpe_l1_base = gic_data_rdist_cpu(cpu)->vpe_l1_base;
  2200. *mask = gic_data_rdist_cpu(cpu)->vpe_table_mask;
  2201. return val;
  2202. }
  2203. return 0;
  2204. }
  2205. static bool allocate_vpe_l2_table(int cpu, u32 id)
  2206. {
  2207. void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base;
  2208. unsigned int psz, esz, idx, npg, gpsz;
  2209. u64 val;
  2210. struct page *page;
  2211. __le64 *table;
  2212. if (!gic_rdists->has_rvpeid)
  2213. return true;
  2214. /* Skip non-present CPUs */
  2215. if (!base)
  2216. return true;
  2217. val = gicr_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER);
  2218. esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val) + 1;
  2219. gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val);
  2220. npg = FIELD_GET(GICR_VPROPBASER_4_1_SIZE, val) + 1;
  2221. switch (gpsz) {
  2222. default:
  2223. WARN_ON(1);
  2224. fallthrough;
  2225. case GIC_PAGE_SIZE_4K:
  2226. psz = SZ_4K;
  2227. break;
  2228. case GIC_PAGE_SIZE_16K:
  2229. psz = SZ_16K;
  2230. break;
  2231. case GIC_PAGE_SIZE_64K:
  2232. psz = SZ_64K;
  2233. break;
  2234. }
  2235. /* Don't allow vpe_id that exceeds single, flat table limit */
  2236. if (!(val & GICR_VPROPBASER_4_1_INDIRECT))
  2237. return (id < (npg * psz / (esz * SZ_8)));
  2238. /* Compute 1st level table index & check if that exceeds table limit */
  2239. idx = id >> ilog2(psz / (esz * SZ_8));
  2240. if (idx >= (npg * psz / GITS_LVL1_ENTRY_SIZE))
  2241. return false;
  2242. table = gic_data_rdist_cpu(cpu)->vpe_l1_base;
  2243. /* Allocate memory for 2nd level table */
  2244. if (!table[idx]) {
  2245. page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(psz));
  2246. if (!page)
  2247. return false;
  2248. /* Flush Lvl2 table to PoC if hw doesn't support coherency */
  2249. if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK))
  2250. gic_flush_dcache_to_poc(page_address(page), psz);
  2251. table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
  2252. /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
  2253. if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK))
  2254. gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
  2255. /* Ensure updated table contents are visible to RD hardware */
  2256. dsb(sy);
  2257. }
  2258. return true;
  2259. }
  2260. static int allocate_vpe_l1_table(void)
  2261. {
  2262. void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
  2263. u64 val, gpsz, npg, pa;
  2264. unsigned int psz = SZ_64K;
  2265. unsigned int np, epp, esz;
  2266. struct page *page;
  2267. if (!gic_rdists->has_rvpeid)
  2268. return 0;
  2269. /*
  2270. * if VPENDBASER.Valid is set, disable any previously programmed
  2271. * VPE by setting PendingLast while clearing Valid. This has the
  2272. * effect of making sure no doorbell will be generated and we can
  2273. * then safely clear VPROPBASER.Valid.
  2274. */
  2275. if (gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER) & GICR_VPENDBASER_Valid)
  2276. gicr_write_vpendbaser(GICR_VPENDBASER_PendingLast,
  2277. vlpi_base + GICR_VPENDBASER);
  2278. /*
  2279. * If we can inherit the configuration from another RD, let's do
  2280. * so. Otherwise, we have to go through the allocation process. We
  2281. * assume that all RDs have the exact same requirements, as
  2282. * nothing will work otherwise.
  2283. */
  2284. val = inherit_vpe_l1_table_from_rd(&gic_data_rdist()->vpe_table_mask);
  2285. if (val & GICR_VPROPBASER_4_1_VALID)
  2286. goto out;
  2287. gic_data_rdist()->vpe_table_mask = kzalloc(sizeof(cpumask_t), GFP_ATOMIC);
  2288. if (!gic_data_rdist()->vpe_table_mask)
  2289. return -ENOMEM;
  2290. val = inherit_vpe_l1_table_from_its();
  2291. if (val & GICR_VPROPBASER_4_1_VALID)
  2292. goto out;
  2293. /* First probe the page size */
  2294. val = FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, GIC_PAGE_SIZE_64K);
  2295. gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
  2296. val = gicr_read_vpropbaser(vlpi_base + GICR_VPROPBASER);
  2297. gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val);
  2298. esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val);
  2299. switch (gpsz) {
  2300. default:
  2301. gpsz = GIC_PAGE_SIZE_4K;
  2302. fallthrough;
  2303. case GIC_PAGE_SIZE_4K:
  2304. psz = SZ_4K;
  2305. break;
  2306. case GIC_PAGE_SIZE_16K:
  2307. psz = SZ_16K;
  2308. break;
  2309. case GIC_PAGE_SIZE_64K:
  2310. psz = SZ_64K;
  2311. break;
  2312. }
  2313. /*
  2314. * Start populating the register from scratch, including RO fields
  2315. * (which we want to print in debug cases...)
  2316. */
  2317. val = 0;
  2318. val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, gpsz);
  2319. val |= FIELD_PREP(GICR_VPROPBASER_4_1_ENTRY_SIZE, esz);
  2320. /* How many entries per GIC page? */
  2321. esz++;
  2322. epp = psz / (esz * SZ_8);
  2323. /*
  2324. * If we need more than just a single L1 page, flag the table
  2325. * as indirect and compute the number of required L1 pages.
  2326. */
  2327. if (epp < ITS_MAX_VPEID) {
  2328. int nl2;
  2329. val |= GICR_VPROPBASER_4_1_INDIRECT;
  2330. /* Number of L2 pages required to cover the VPEID space */
  2331. nl2 = DIV_ROUND_UP(ITS_MAX_VPEID, epp);
  2332. /* Number of L1 pages to point to the L2 pages */
  2333. npg = DIV_ROUND_UP(nl2 * SZ_8, psz);
  2334. } else {
  2335. npg = 1;
  2336. }
  2337. val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, npg - 1);
  2338. /* Right, that's the number of CPU pages we need for L1 */
  2339. np = DIV_ROUND_UP(npg * psz, PAGE_SIZE);
  2340. pr_debug("np = %d, npg = %lld, psz = %d, epp = %d, esz = %d\n",
  2341. np, npg, psz, epp, esz);
  2342. page = alloc_pages(GFP_ATOMIC | __GFP_ZERO, get_order(np * PAGE_SIZE));
  2343. if (!page)
  2344. return -ENOMEM;
  2345. gic_data_rdist()->vpe_l1_base = page_address(page);
  2346. pa = virt_to_phys(page_address(page));
  2347. WARN_ON(!IS_ALIGNED(pa, psz));
  2348. val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, pa >> 12);
  2349. val |= GICR_VPROPBASER_RaWb;
  2350. val |= GICR_VPROPBASER_InnerShareable;
  2351. val |= GICR_VPROPBASER_4_1_Z;
  2352. val |= GICR_VPROPBASER_4_1_VALID;
  2353. out:
  2354. gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
  2355. cpumask_set_cpu(smp_processor_id(), gic_data_rdist()->vpe_table_mask);
  2356. pr_debug("CPU%d: VPROPBASER = %llx %*pbl\n",
  2357. smp_processor_id(), val,
  2358. cpumask_pr_args(gic_data_rdist()->vpe_table_mask));
  2359. return 0;
  2360. }
  2361. static int its_alloc_collections(struct its_node *its)
  2362. {
  2363. int i;
  2364. its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections),
  2365. GFP_KERNEL);
  2366. if (!its->collections)
  2367. return -ENOMEM;
  2368. for (i = 0; i < nr_cpu_ids; i++)
  2369. its->collections[i].target_address = ~0ULL;
  2370. return 0;
  2371. }
  2372. static struct page *its_allocate_pending_table(gfp_t gfp_flags)
  2373. {
  2374. struct page *pend_page;
  2375. pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
  2376. get_order(LPI_PENDBASE_SZ));
  2377. if (!pend_page)
  2378. return NULL;
  2379. /* Make sure the GIC will observe the zero-ed page */
  2380. gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
  2381. return pend_page;
  2382. }
  2383. static void its_free_pending_table(struct page *pt)
  2384. {
  2385. free_pages((unsigned long)page_address(pt), get_order(LPI_PENDBASE_SZ));
  2386. }
  2387. /*
  2388. * Booting with kdump and LPIs enabled is generally fine. Any other
  2389. * case is wrong in the absence of firmware/EFI support.
  2390. */
  2391. static bool enabled_lpis_allowed(void)
  2392. {
  2393. phys_addr_t addr;
  2394. u64 val;
  2395. /* Check whether the property table is in a reserved region */
  2396. val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
  2397. addr = val & GENMASK_ULL(51, 12);
  2398. return gic_check_reserved_range(addr, LPI_PROPBASE_SZ);
  2399. }
  2400. static int __init allocate_lpi_tables(void)
  2401. {
  2402. u64 val;
  2403. int err, cpu;
  2404. /*
  2405. * If LPIs are enabled while we run this from the boot CPU,
  2406. * flag the RD tables as pre-allocated if the stars do align.
  2407. */
  2408. val = readl_relaxed(gic_data_rdist_rd_base() + GICR_CTLR);
  2409. if ((val & GICR_CTLR_ENABLE_LPIS) && enabled_lpis_allowed()) {
  2410. gic_rdists->flags |= (RDIST_FLAGS_RD_TABLES_PREALLOCATED |
  2411. RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING);
  2412. pr_info("GICv3: Using preallocated redistributor tables\n");
  2413. }
  2414. err = its_setup_lpi_prop_table();
  2415. if (err)
  2416. return err;
  2417. /*
  2418. * We allocate all the pending tables anyway, as we may have a
  2419. * mix of RDs that have had LPIs enabled, and some that
  2420. * don't. We'll free the unused ones as each CPU comes online.
  2421. */
  2422. for_each_possible_cpu(cpu) {
  2423. struct page *pend_page;
  2424. pend_page = its_allocate_pending_table(GFP_NOWAIT);
  2425. if (!pend_page) {
  2426. pr_err("Failed to allocate PENDBASE for CPU%d\n", cpu);
  2427. return -ENOMEM;
  2428. }
  2429. gic_data_rdist_cpu(cpu)->pend_page = pend_page;
  2430. }
  2431. return 0;
  2432. }
  2433. static u64 read_vpend_dirty_clear(void __iomem *vlpi_base)
  2434. {
  2435. u32 count = 1000000; /* 1s! */
  2436. bool clean;
  2437. u64 val;
  2438. do {
  2439. val = gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
  2440. clean = !(val & GICR_VPENDBASER_Dirty);
  2441. if (!clean) {
  2442. count--;
  2443. cpu_relax();
  2444. udelay(1);
  2445. }
  2446. } while (!clean && count);
  2447. if (unlikely(!clean))
  2448. pr_err_ratelimited("ITS virtual pending table not cleaning\n");
  2449. return val;
  2450. }
  2451. static u64 its_clear_vpend_valid(void __iomem *vlpi_base, u64 clr, u64 set)
  2452. {
  2453. u64 val;
  2454. /* Make sure we wait until the RD is done with the initial scan */
  2455. val = read_vpend_dirty_clear(vlpi_base);
  2456. val &= ~GICR_VPENDBASER_Valid;
  2457. val &= ~clr;
  2458. val |= set;
  2459. gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
  2460. val = read_vpend_dirty_clear(vlpi_base);
  2461. if (unlikely(val & GICR_VPENDBASER_Dirty))
  2462. val |= GICR_VPENDBASER_PendingLast;
  2463. return val;
  2464. }
  2465. static void its_cpu_init_lpis(void)
  2466. {
  2467. void __iomem *rbase = gic_data_rdist_rd_base();
  2468. struct page *pend_page;
  2469. phys_addr_t paddr;
  2470. u64 val, tmp;
  2471. if (gic_data_rdist()->flags & RD_LOCAL_LPI_ENABLED)
  2472. return;
  2473. val = readl_relaxed(rbase + GICR_CTLR);
  2474. if ((gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) &&
  2475. (val & GICR_CTLR_ENABLE_LPIS)) {
  2476. /*
  2477. * Check that we get the same property table on all
  2478. * RDs. If we don't, this is hopeless.
  2479. */
  2480. paddr = gicr_read_propbaser(rbase + GICR_PROPBASER);
  2481. paddr &= GENMASK_ULL(51, 12);
  2482. if (WARN_ON(gic_rdists->prop_table_pa != paddr))
  2483. add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
  2484. paddr = gicr_read_pendbaser(rbase + GICR_PENDBASER);
  2485. paddr &= GENMASK_ULL(51, 16);
  2486. WARN_ON(!gic_check_reserved_range(paddr, LPI_PENDBASE_SZ));
  2487. gic_data_rdist()->flags |= RD_LOCAL_PENDTABLE_PREALLOCATED;
  2488. goto out;
  2489. }
  2490. pend_page = gic_data_rdist()->pend_page;
  2491. paddr = page_to_phys(pend_page);
  2492. /* set PROPBASE */
  2493. val = (gic_rdists->prop_table_pa |
  2494. GICR_PROPBASER_InnerShareable |
  2495. GICR_PROPBASER_RaWaWb |
  2496. ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
  2497. gicr_write_propbaser(val, rbase + GICR_PROPBASER);
  2498. tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
  2499. if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
  2500. if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
  2501. /*
  2502. * The HW reports non-shareable, we must
  2503. * remove the cacheability attributes as
  2504. * well.
  2505. */
  2506. val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
  2507. GICR_PROPBASER_CACHEABILITY_MASK);
  2508. val |= GICR_PROPBASER_nC;
  2509. gicr_write_propbaser(val, rbase + GICR_PROPBASER);
  2510. }
  2511. pr_info_once("GIC: using cache flushing for LPI property table\n");
  2512. gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
  2513. }
  2514. /* set PENDBASE */
  2515. val = (page_to_phys(pend_page) |
  2516. GICR_PENDBASER_InnerShareable |
  2517. GICR_PENDBASER_RaWaWb);
  2518. gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
  2519. tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
  2520. if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
  2521. /*
  2522. * The HW reports non-shareable, we must remove the
  2523. * cacheability attributes as well.
  2524. */
  2525. val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
  2526. GICR_PENDBASER_CACHEABILITY_MASK);
  2527. val |= GICR_PENDBASER_nC;
  2528. gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
  2529. }
  2530. /* Enable LPIs */
  2531. val = readl_relaxed(rbase + GICR_CTLR);
  2532. val |= GICR_CTLR_ENABLE_LPIS;
  2533. writel_relaxed(val, rbase + GICR_CTLR);
  2534. if (gic_rdists->has_vlpis && !gic_rdists->has_rvpeid) {
  2535. void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
  2536. /*
  2537. * It's possible for CPU to receive VLPIs before it is
  2538. * scheduled as a vPE, especially for the first CPU, and the
  2539. * VLPI with INTID larger than 2^(IDbits+1) will be considered
  2540. * as out of range and dropped by GIC.
  2541. * So we initialize IDbits to known value to avoid VLPI drop.
  2542. */
  2543. val = (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
  2544. pr_debug("GICv4: CPU%d: Init IDbits to 0x%llx for GICR_VPROPBASER\n",
  2545. smp_processor_id(), val);
  2546. gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
  2547. /*
  2548. * Also clear Valid bit of GICR_VPENDBASER, in case some
  2549. * ancient programming gets left in and has possibility of
  2550. * corrupting memory.
  2551. */
  2552. val = its_clear_vpend_valid(vlpi_base, 0, 0);
  2553. }
  2554. if (allocate_vpe_l1_table()) {
  2555. /*
  2556. * If the allocation has failed, we're in massive trouble.
  2557. * Disable direct injection, and pray that no VM was
  2558. * already running...
  2559. */
  2560. gic_rdists->has_rvpeid = false;
  2561. gic_rdists->has_vlpis = false;
  2562. }
  2563. /* Make sure the GIC has seen the above */
  2564. dsb(sy);
  2565. out:
  2566. gic_data_rdist()->flags |= RD_LOCAL_LPI_ENABLED;
  2567. pr_info("GICv3: CPU%d: using %s LPI pending table @%pa\n",
  2568. smp_processor_id(),
  2569. gic_data_rdist()->flags & RD_LOCAL_PENDTABLE_PREALLOCATED ?
  2570. "reserved" : "allocated",
  2571. &paddr);
  2572. }
  2573. static void its_cpu_init_collection(struct its_node *its)
  2574. {
  2575. int cpu = smp_processor_id();
  2576. u64 target;
  2577. /* avoid cross node collections and its mapping */
  2578. if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
  2579. struct device_node *cpu_node;
  2580. cpu_node = of_get_cpu_node(cpu, NULL);
  2581. if (its->numa_node != NUMA_NO_NODE &&
  2582. its->numa_node != of_node_to_nid(cpu_node))
  2583. return;
  2584. }
  2585. /*
  2586. * We now have to bind each collection to its target
  2587. * redistributor.
  2588. */
  2589. if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
  2590. /*
  2591. * This ITS wants the physical address of the
  2592. * redistributor.
  2593. */
  2594. target = gic_data_rdist()->phys_base;
  2595. } else {
  2596. /* This ITS wants a linear CPU number. */
  2597. target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
  2598. target = GICR_TYPER_CPU_NUMBER(target) << 16;
  2599. }
  2600. /* Perform collection mapping */
  2601. its->collections[cpu].target_address = target;
  2602. its->collections[cpu].col_id = cpu;
  2603. its_send_mapc(its, &its->collections[cpu], 1);
  2604. its_send_invall(its, &its->collections[cpu]);
  2605. }
  2606. static void its_cpu_init_collections(void)
  2607. {
  2608. struct its_node *its;
  2609. raw_spin_lock(&its_lock);
  2610. list_for_each_entry(its, &its_nodes, entry)
  2611. its_cpu_init_collection(its);
  2612. raw_spin_unlock(&its_lock);
  2613. }
  2614. static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
  2615. {
  2616. struct its_device *its_dev = NULL, *tmp;
  2617. unsigned long flags;
  2618. raw_spin_lock_irqsave(&its->lock, flags);
  2619. list_for_each_entry(tmp, &its->its_device_list, entry) {
  2620. if (tmp->device_id == dev_id) {
  2621. its_dev = tmp;
  2622. break;
  2623. }
  2624. }
  2625. raw_spin_unlock_irqrestore(&its->lock, flags);
  2626. return its_dev;
  2627. }
  2628. static struct its_baser *its_get_baser(struct its_node *its, u32 type)
  2629. {
  2630. int i;
  2631. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  2632. if (GITS_BASER_TYPE(its->tables[i].val) == type)
  2633. return &its->tables[i];
  2634. }
  2635. return NULL;
  2636. }
  2637. static bool its_alloc_table_entry(struct its_node *its,
  2638. struct its_baser *baser, u32 id)
  2639. {
  2640. struct page *page;
  2641. u32 esz, idx;
  2642. __le64 *table;
  2643. /* Don't allow device id that exceeds single, flat table limit */
  2644. esz = GITS_BASER_ENTRY_SIZE(baser->val);
  2645. if (!(baser->val & GITS_BASER_INDIRECT))
  2646. return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
  2647. /* Compute 1st level table index & check if that exceeds table limit */
  2648. idx = id >> ilog2(baser->psz / esz);
  2649. if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
  2650. return false;
  2651. table = baser->base;
  2652. /* Allocate memory for 2nd level table */
  2653. if (!table[idx]) {
  2654. page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
  2655. get_order(baser->psz));
  2656. if (!page)
  2657. return false;
  2658. /* Flush Lvl2 table to PoC if hw doesn't support coherency */
  2659. if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
  2660. gic_flush_dcache_to_poc(page_address(page), baser->psz);
  2661. table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
  2662. /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
  2663. if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
  2664. gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
  2665. /* Ensure updated table contents are visible to ITS hardware */
  2666. dsb(sy);
  2667. }
  2668. return true;
  2669. }
  2670. static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
  2671. {
  2672. struct its_baser *baser;
  2673. baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
  2674. /* Don't allow device id that exceeds ITS hardware limit */
  2675. if (!baser)
  2676. return (ilog2(dev_id) < device_ids(its));
  2677. return its_alloc_table_entry(its, baser, dev_id);
  2678. }
  2679. static bool its_alloc_vpe_table(u32 vpe_id)
  2680. {
  2681. struct its_node *its;
  2682. int cpu;
  2683. /*
  2684. * Make sure the L2 tables are allocated on *all* v4 ITSs. We
  2685. * could try and only do it on ITSs corresponding to devices
  2686. * that have interrupts targeted at this VPE, but the
  2687. * complexity becomes crazy (and you have tons of memory
  2688. * anyway, right?).
  2689. */
  2690. list_for_each_entry(its, &its_nodes, entry) {
  2691. struct its_baser *baser;
  2692. if (!is_v4(its))
  2693. continue;
  2694. baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
  2695. if (!baser)
  2696. return false;
  2697. if (!its_alloc_table_entry(its, baser, vpe_id))
  2698. return false;
  2699. }
  2700. /* Non v4.1? No need to iterate RDs and go back early. */
  2701. if (!gic_rdists->has_rvpeid)
  2702. return true;
  2703. /*
  2704. * Make sure the L2 tables are allocated for all copies of
  2705. * the L1 table on *all* v4.1 RDs.
  2706. */
  2707. for_each_possible_cpu(cpu) {
  2708. if (!allocate_vpe_l2_table(cpu, vpe_id))
  2709. return false;
  2710. }
  2711. return true;
  2712. }
  2713. static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
  2714. int nvecs, bool alloc_lpis)
  2715. {
  2716. struct its_device *dev;
  2717. unsigned long *lpi_map = NULL;
  2718. unsigned long flags;
  2719. u16 *col_map = NULL;
  2720. void *itt;
  2721. int lpi_base;
  2722. int nr_lpis;
  2723. int nr_ites;
  2724. int sz;
  2725. if (!its_alloc_device_table(its, dev_id))
  2726. return NULL;
  2727. if (WARN_ON(!is_power_of_2(nvecs)))
  2728. nvecs = roundup_pow_of_two(nvecs);
  2729. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  2730. /*
  2731. * Even if the device wants a single LPI, the ITT must be
  2732. * sized as a power of two (and you need at least one bit...).
  2733. */
  2734. nr_ites = max(2, nvecs);
  2735. sz = nr_ites * (FIELD_GET(GITS_TYPER_ITT_ENTRY_SIZE, its->typer) + 1);
  2736. sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
  2737. itt = kzalloc_node(sz, GFP_KERNEL, its->numa_node);
  2738. if (alloc_lpis) {
  2739. lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis);
  2740. if (lpi_map)
  2741. col_map = kcalloc(nr_lpis, sizeof(*col_map),
  2742. GFP_KERNEL);
  2743. } else {
  2744. col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL);
  2745. nr_lpis = 0;
  2746. lpi_base = 0;
  2747. }
  2748. if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
  2749. kfree(dev);
  2750. kfree(itt);
  2751. bitmap_free(lpi_map);
  2752. kfree(col_map);
  2753. return NULL;
  2754. }
  2755. gic_flush_dcache_to_poc(itt, sz);
  2756. dev->its = its;
  2757. dev->itt = itt;
  2758. dev->nr_ites = nr_ites;
  2759. dev->event_map.lpi_map = lpi_map;
  2760. dev->event_map.col_map = col_map;
  2761. dev->event_map.lpi_base = lpi_base;
  2762. dev->event_map.nr_lpis = nr_lpis;
  2763. raw_spin_lock_init(&dev->event_map.vlpi_lock);
  2764. dev->device_id = dev_id;
  2765. INIT_LIST_HEAD(&dev->entry);
  2766. raw_spin_lock_irqsave(&its->lock, flags);
  2767. list_add(&dev->entry, &its->its_device_list);
  2768. raw_spin_unlock_irqrestore(&its->lock, flags);
  2769. /* Map device to its ITT */
  2770. its_send_mapd(dev, 1);
  2771. return dev;
  2772. }
  2773. static void its_free_device(struct its_device *its_dev)
  2774. {
  2775. unsigned long flags;
  2776. raw_spin_lock_irqsave(&its_dev->its->lock, flags);
  2777. list_del(&its_dev->entry);
  2778. raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
  2779. kfree(its_dev->event_map.col_map);
  2780. kfree(its_dev->itt);
  2781. kfree(its_dev);
  2782. }
  2783. static int its_alloc_device_irq(struct its_device *dev, int nvecs, irq_hw_number_t *hwirq)
  2784. {
  2785. int idx;
  2786. /* Find a free LPI region in lpi_map and allocate them. */
  2787. idx = bitmap_find_free_region(dev->event_map.lpi_map,
  2788. dev->event_map.nr_lpis,
  2789. get_count_order(nvecs));
  2790. if (idx < 0)
  2791. return -ENOSPC;
  2792. *hwirq = dev->event_map.lpi_base + idx;
  2793. return 0;
  2794. }
  2795. static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
  2796. int nvec, msi_alloc_info_t *info)
  2797. {
  2798. struct its_node *its;
  2799. struct its_device *its_dev;
  2800. struct msi_domain_info *msi_info;
  2801. u32 dev_id;
  2802. int err = 0;
  2803. /*
  2804. * We ignore "dev" entirely, and rely on the dev_id that has
  2805. * been passed via the scratchpad. This limits this domain's
  2806. * usefulness to upper layers that definitely know that they
  2807. * are built on top of the ITS.
  2808. */
  2809. dev_id = info->scratchpad[0].ul;
  2810. msi_info = msi_get_domain_info(domain);
  2811. its = msi_info->data;
  2812. if (!gic_rdists->has_direct_lpi &&
  2813. vpe_proxy.dev &&
  2814. vpe_proxy.dev->its == its &&
  2815. dev_id == vpe_proxy.dev->device_id) {
  2816. /* Bad luck. Get yourself a better implementation */
  2817. WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
  2818. dev_id);
  2819. return -EINVAL;
  2820. }
  2821. mutex_lock(&its->dev_alloc_lock);
  2822. its_dev = its_find_device(its, dev_id);
  2823. if (its_dev) {
  2824. /*
  2825. * We already have seen this ID, probably through
  2826. * another alias (PCI bridge of some sort). No need to
  2827. * create the device.
  2828. */
  2829. its_dev->shared = true;
  2830. pr_debug("Reusing ITT for devID %x\n", dev_id);
  2831. goto out;
  2832. }
  2833. its_dev = its_create_device(its, dev_id, nvec, true);
  2834. if (!its_dev) {
  2835. err = -ENOMEM;
  2836. goto out;
  2837. }
  2838. if (info->flags & MSI_ALLOC_FLAGS_PROXY_DEVICE)
  2839. its_dev->shared = true;
  2840. pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
  2841. out:
  2842. mutex_unlock(&its->dev_alloc_lock);
  2843. info->scratchpad[0].ptr = its_dev;
  2844. return err;
  2845. }
  2846. static struct msi_domain_ops its_msi_domain_ops = {
  2847. .msi_prepare = its_msi_prepare,
  2848. };
  2849. static int its_irq_gic_domain_alloc(struct irq_domain *domain,
  2850. unsigned int virq,
  2851. irq_hw_number_t hwirq)
  2852. {
  2853. struct irq_fwspec fwspec;
  2854. if (irq_domain_get_of_node(domain->parent)) {
  2855. fwspec.fwnode = domain->parent->fwnode;
  2856. fwspec.param_count = 3;
  2857. fwspec.param[0] = GIC_IRQ_TYPE_LPI;
  2858. fwspec.param[1] = hwirq;
  2859. fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
  2860. } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
  2861. fwspec.fwnode = domain->parent->fwnode;
  2862. fwspec.param_count = 2;
  2863. fwspec.param[0] = hwirq;
  2864. fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
  2865. } else {
  2866. return -EINVAL;
  2867. }
  2868. return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
  2869. }
  2870. static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  2871. unsigned int nr_irqs, void *args)
  2872. {
  2873. msi_alloc_info_t *info = args;
  2874. struct its_device *its_dev = info->scratchpad[0].ptr;
  2875. struct its_node *its = its_dev->its;
  2876. struct irq_data *irqd;
  2877. irq_hw_number_t hwirq;
  2878. int err;
  2879. int i;
  2880. err = its_alloc_device_irq(its_dev, nr_irqs, &hwirq);
  2881. if (err)
  2882. return err;
  2883. err = iommu_dma_prepare_msi(info->desc, its->get_msi_base(its_dev));
  2884. if (err)
  2885. return err;
  2886. for (i = 0; i < nr_irqs; i++) {
  2887. err = its_irq_gic_domain_alloc(domain, virq + i, hwirq + i);
  2888. if (err)
  2889. return err;
  2890. irq_domain_set_hwirq_and_chip(domain, virq + i,
  2891. hwirq + i, &its_irq_chip, its_dev);
  2892. irqd = irq_get_irq_data(virq + i);
  2893. irqd_set_single_target(irqd);
  2894. irqd_set_affinity_on_activate(irqd);
  2895. pr_debug("ID:%d pID:%d vID:%d\n",
  2896. (int)(hwirq + i - its_dev->event_map.lpi_base),
  2897. (int)(hwirq + i), virq + i);
  2898. }
  2899. return 0;
  2900. }
  2901. static int its_irq_domain_activate(struct irq_domain *domain,
  2902. struct irq_data *d, bool reserve)
  2903. {
  2904. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  2905. u32 event = its_get_event_id(d);
  2906. int cpu;
  2907. cpu = its_select_cpu(d, cpu_online_mask);
  2908. if (cpu < 0 || cpu >= nr_cpu_ids)
  2909. return -EINVAL;
  2910. its_inc_lpi_count(d, cpu);
  2911. its_dev->event_map.col_map[event] = cpu;
  2912. irq_data_update_effective_affinity(d, cpumask_of(cpu));
  2913. /* Map the GIC IRQ and event to the device */
  2914. its_send_mapti(its_dev, d->hwirq, event);
  2915. return 0;
  2916. }
  2917. static void its_irq_domain_deactivate(struct irq_domain *domain,
  2918. struct irq_data *d)
  2919. {
  2920. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  2921. u32 event = its_get_event_id(d);
  2922. its_dec_lpi_count(d, its_dev->event_map.col_map[event]);
  2923. /* Stop the delivery of interrupts */
  2924. its_send_discard(its_dev, event);
  2925. }
  2926. static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  2927. unsigned int nr_irqs)
  2928. {
  2929. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  2930. struct its_device *its_dev = irq_data_get_irq_chip_data(d);
  2931. struct its_node *its = its_dev->its;
  2932. int i;
  2933. bitmap_release_region(its_dev->event_map.lpi_map,
  2934. its_get_event_id(irq_domain_get_irq_data(domain, virq)),
  2935. get_count_order(nr_irqs));
  2936. for (i = 0; i < nr_irqs; i++) {
  2937. struct irq_data *data = irq_domain_get_irq_data(domain,
  2938. virq + i);
  2939. /* Nuke the entry in the domain */
  2940. irq_domain_reset_irq_data(data);
  2941. }
  2942. mutex_lock(&its->dev_alloc_lock);
  2943. /*
  2944. * If all interrupts have been freed, start mopping the
  2945. * floor. This is conditioned on the device not being shared.
  2946. */
  2947. if (!its_dev->shared &&
  2948. bitmap_empty(its_dev->event_map.lpi_map,
  2949. its_dev->event_map.nr_lpis)) {
  2950. its_lpi_free(its_dev->event_map.lpi_map,
  2951. its_dev->event_map.lpi_base,
  2952. its_dev->event_map.nr_lpis);
  2953. /* Unmap device/itt */
  2954. its_send_mapd(its_dev, 0);
  2955. its_free_device(its_dev);
  2956. }
  2957. mutex_unlock(&its->dev_alloc_lock);
  2958. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  2959. }
  2960. static const struct irq_domain_ops its_domain_ops = {
  2961. .alloc = its_irq_domain_alloc,
  2962. .free = its_irq_domain_free,
  2963. .activate = its_irq_domain_activate,
  2964. .deactivate = its_irq_domain_deactivate,
  2965. };
  2966. /*
  2967. * This is insane.
  2968. *
  2969. * If a GICv4.0 doesn't implement Direct LPIs (which is extremely
  2970. * likely), the only way to perform an invalidate is to use a fake
  2971. * device to issue an INV command, implying that the LPI has first
  2972. * been mapped to some event on that device. Since this is not exactly
  2973. * cheap, we try to keep that mapping around as long as possible, and
  2974. * only issue an UNMAP if we're short on available slots.
  2975. *
  2976. * Broken by design(tm).
  2977. *
  2978. * GICv4.1, on the other hand, mandates that we're able to invalidate
  2979. * by writing to a MMIO register. It doesn't implement the whole of
  2980. * DirectLPI, but that's good enough. And most of the time, we don't
  2981. * even have to invalidate anything, as the redistributor can be told
  2982. * whether to generate a doorbell or not (we thus leave it enabled,
  2983. * always).
  2984. */
  2985. static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
  2986. {
  2987. /* GICv4.1 doesn't use a proxy, so nothing to do here */
  2988. if (gic_rdists->has_rvpeid)
  2989. return;
  2990. /* Already unmapped? */
  2991. if (vpe->vpe_proxy_event == -1)
  2992. return;
  2993. its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
  2994. vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
  2995. /*
  2996. * We don't track empty slots at all, so let's move the
  2997. * next_victim pointer if we can quickly reuse that slot
  2998. * instead of nuking an existing entry. Not clear that this is
  2999. * always a win though, and this might just generate a ripple
  3000. * effect... Let's just hope VPEs don't migrate too often.
  3001. */
  3002. if (vpe_proxy.vpes[vpe_proxy.next_victim])
  3003. vpe_proxy.next_victim = vpe->vpe_proxy_event;
  3004. vpe->vpe_proxy_event = -1;
  3005. }
  3006. static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
  3007. {
  3008. /* GICv4.1 doesn't use a proxy, so nothing to do here */
  3009. if (gic_rdists->has_rvpeid)
  3010. return;
  3011. if (!gic_rdists->has_direct_lpi) {
  3012. unsigned long flags;
  3013. raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
  3014. its_vpe_db_proxy_unmap_locked(vpe);
  3015. raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
  3016. }
  3017. }
  3018. static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
  3019. {
  3020. /* GICv4.1 doesn't use a proxy, so nothing to do here */
  3021. if (gic_rdists->has_rvpeid)
  3022. return;
  3023. /* Already mapped? */
  3024. if (vpe->vpe_proxy_event != -1)
  3025. return;
  3026. /* This slot was already allocated. Kick the other VPE out. */
  3027. if (vpe_proxy.vpes[vpe_proxy.next_victim])
  3028. its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
  3029. /* Map the new VPE instead */
  3030. vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
  3031. vpe->vpe_proxy_event = vpe_proxy.next_victim;
  3032. vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
  3033. vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
  3034. its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
  3035. }
  3036. static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
  3037. {
  3038. unsigned long flags;
  3039. struct its_collection *target_col;
  3040. /* GICv4.1 doesn't use a proxy, so nothing to do here */
  3041. if (gic_rdists->has_rvpeid)
  3042. return;
  3043. if (gic_rdists->has_direct_lpi) {
  3044. void __iomem *rdbase;
  3045. rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
  3046. gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
  3047. wait_for_syncr(rdbase);
  3048. return;
  3049. }
  3050. raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
  3051. its_vpe_db_proxy_map_locked(vpe);
  3052. target_col = &vpe_proxy.dev->its->collections[to];
  3053. its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
  3054. vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
  3055. raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
  3056. }
  3057. static int its_vpe_set_affinity(struct irq_data *d,
  3058. const struct cpumask *mask_val,
  3059. bool force)
  3060. {
  3061. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  3062. int from, cpu = cpumask_first(mask_val);
  3063. unsigned long flags;
  3064. /*
  3065. * Changing affinity is mega expensive, so let's be as lazy as
  3066. * we can and only do it if we really have to. Also, if mapped
  3067. * into the proxy device, we need to move the doorbell
  3068. * interrupt to its new location.
  3069. *
  3070. * Another thing is that changing the affinity of a vPE affects
  3071. * *other interrupts* such as all the vLPIs that are routed to
  3072. * this vPE. This means that the irq_desc lock is not enough to
  3073. * protect us, and that we must ensure nobody samples vpe->col_idx
  3074. * during the update, hence the lock below which must also be
  3075. * taken on any vLPI handling path that evaluates vpe->col_idx.
  3076. */
  3077. from = vpe_to_cpuid_lock(vpe, &flags);
  3078. if (from == cpu)
  3079. goto out;
  3080. vpe->col_idx = cpu;
  3081. /*
  3082. * GICv4.1 allows us to skip VMOVP if moving to a cpu whose RD
  3083. * is sharing its VPE table with the current one.
  3084. */
  3085. if (gic_data_rdist_cpu(cpu)->vpe_table_mask &&
  3086. cpumask_test_cpu(from, gic_data_rdist_cpu(cpu)->vpe_table_mask))
  3087. goto out;
  3088. its_send_vmovp(vpe);
  3089. its_vpe_db_proxy_move(vpe, from, cpu);
  3090. out:
  3091. irq_data_update_effective_affinity(d, cpumask_of(cpu));
  3092. vpe_to_cpuid_unlock(vpe, flags);
  3093. return IRQ_SET_MASK_OK_DONE;
  3094. }
  3095. static void its_wait_vpt_parse_complete(void)
  3096. {
  3097. void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
  3098. u64 val;
  3099. if (!gic_rdists->has_vpend_valid_dirty)
  3100. return;
  3101. WARN_ON_ONCE(readq_relaxed_poll_timeout_atomic(vlpi_base + GICR_VPENDBASER,
  3102. val,
  3103. !(val & GICR_VPENDBASER_Dirty),
  3104. 1, 500));
  3105. }
  3106. static void its_vpe_schedule(struct its_vpe *vpe)
  3107. {
  3108. void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
  3109. u64 val;
  3110. /* Schedule the VPE */
  3111. val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
  3112. GENMASK_ULL(51, 12);
  3113. val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
  3114. val |= GICR_VPROPBASER_RaWb;
  3115. val |= GICR_VPROPBASER_InnerShareable;
  3116. gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
  3117. val = virt_to_phys(page_address(vpe->vpt_page)) &
  3118. GENMASK_ULL(51, 16);
  3119. val |= GICR_VPENDBASER_RaWaWb;
  3120. val |= GICR_VPENDBASER_InnerShareable;
  3121. /*
  3122. * There is no good way of finding out if the pending table is
  3123. * empty as we can race against the doorbell interrupt very
  3124. * easily. So in the end, vpe->pending_last is only an
  3125. * indication that the vcpu has something pending, not one
  3126. * that the pending table is empty. A good implementation
  3127. * would be able to read its coarse map pretty quickly anyway,
  3128. * making this a tolerable issue.
  3129. */
  3130. val |= GICR_VPENDBASER_PendingLast;
  3131. val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
  3132. val |= GICR_VPENDBASER_Valid;
  3133. gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
  3134. }
  3135. static void its_vpe_deschedule(struct its_vpe *vpe)
  3136. {
  3137. void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
  3138. u64 val;
  3139. val = its_clear_vpend_valid(vlpi_base, 0, 0);
  3140. vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
  3141. vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
  3142. }
  3143. static void its_vpe_invall(struct its_vpe *vpe)
  3144. {
  3145. struct its_node *its;
  3146. list_for_each_entry(its, &its_nodes, entry) {
  3147. if (!is_v4(its))
  3148. continue;
  3149. if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
  3150. continue;
  3151. /*
  3152. * Sending a VINVALL to a single ITS is enough, as all
  3153. * we need is to reach the redistributors.
  3154. */
  3155. its_send_vinvall(its, vpe);
  3156. return;
  3157. }
  3158. }
  3159. static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
  3160. {
  3161. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  3162. struct its_cmd_info *info = vcpu_info;
  3163. switch (info->cmd_type) {
  3164. case SCHEDULE_VPE:
  3165. its_vpe_schedule(vpe);
  3166. return 0;
  3167. case DESCHEDULE_VPE:
  3168. its_vpe_deschedule(vpe);
  3169. return 0;
  3170. case COMMIT_VPE:
  3171. its_wait_vpt_parse_complete();
  3172. return 0;
  3173. case INVALL_VPE:
  3174. its_vpe_invall(vpe);
  3175. return 0;
  3176. default:
  3177. return -EINVAL;
  3178. }
  3179. }
  3180. static void its_vpe_send_cmd(struct its_vpe *vpe,
  3181. void (*cmd)(struct its_device *, u32))
  3182. {
  3183. unsigned long flags;
  3184. raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
  3185. its_vpe_db_proxy_map_locked(vpe);
  3186. cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
  3187. raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
  3188. }
  3189. static void its_vpe_send_inv(struct irq_data *d)
  3190. {
  3191. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  3192. if (gic_rdists->has_direct_lpi)
  3193. __direct_lpi_inv(d, d->parent_data->hwirq);
  3194. else
  3195. its_vpe_send_cmd(vpe, its_send_inv);
  3196. }
  3197. static void its_vpe_mask_irq(struct irq_data *d)
  3198. {
  3199. /*
  3200. * We need to unmask the LPI, which is described by the parent
  3201. * irq_data. Instead of calling into the parent (which won't
  3202. * exactly do the right thing, let's simply use the
  3203. * parent_data pointer. Yes, I'm naughty.
  3204. */
  3205. lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
  3206. its_vpe_send_inv(d);
  3207. }
  3208. static void its_vpe_unmask_irq(struct irq_data *d)
  3209. {
  3210. /* Same hack as above... */
  3211. lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
  3212. its_vpe_send_inv(d);
  3213. }
  3214. static int its_vpe_set_irqchip_state(struct irq_data *d,
  3215. enum irqchip_irq_state which,
  3216. bool state)
  3217. {
  3218. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  3219. if (which != IRQCHIP_STATE_PENDING)
  3220. return -EINVAL;
  3221. if (gic_rdists->has_direct_lpi) {
  3222. void __iomem *rdbase;
  3223. rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
  3224. if (state) {
  3225. gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
  3226. } else {
  3227. gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
  3228. wait_for_syncr(rdbase);
  3229. }
  3230. } else {
  3231. if (state)
  3232. its_vpe_send_cmd(vpe, its_send_int);
  3233. else
  3234. its_vpe_send_cmd(vpe, its_send_clear);
  3235. }
  3236. return 0;
  3237. }
  3238. static int its_vpe_retrigger(struct irq_data *d)
  3239. {
  3240. return !its_vpe_set_irqchip_state(d, IRQCHIP_STATE_PENDING, true);
  3241. }
  3242. static struct irq_chip its_vpe_irq_chip = {
  3243. .name = "GICv4-vpe",
  3244. .irq_mask = its_vpe_mask_irq,
  3245. .irq_unmask = its_vpe_unmask_irq,
  3246. .irq_eoi = irq_chip_eoi_parent,
  3247. .irq_set_affinity = its_vpe_set_affinity,
  3248. .irq_retrigger = its_vpe_retrigger,
  3249. .irq_set_irqchip_state = its_vpe_set_irqchip_state,
  3250. .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
  3251. };
  3252. static struct its_node *find_4_1_its(void)
  3253. {
  3254. static struct its_node *its = NULL;
  3255. if (!its) {
  3256. list_for_each_entry(its, &its_nodes, entry) {
  3257. if (is_v4_1(its))
  3258. return its;
  3259. }
  3260. /* Oops? */
  3261. its = NULL;
  3262. }
  3263. return its;
  3264. }
  3265. static void its_vpe_4_1_send_inv(struct irq_data *d)
  3266. {
  3267. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  3268. struct its_node *its;
  3269. /*
  3270. * GICv4.1 wants doorbells to be invalidated using the
  3271. * INVDB command in order to be broadcast to all RDs. Send
  3272. * it to the first valid ITS, and let the HW do its magic.
  3273. */
  3274. its = find_4_1_its();
  3275. if (its)
  3276. its_send_invdb(its, vpe);
  3277. }
  3278. static void its_vpe_4_1_mask_irq(struct irq_data *d)
  3279. {
  3280. lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
  3281. its_vpe_4_1_send_inv(d);
  3282. }
  3283. static void its_vpe_4_1_unmask_irq(struct irq_data *d)
  3284. {
  3285. lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
  3286. its_vpe_4_1_send_inv(d);
  3287. }
  3288. static void its_vpe_4_1_schedule(struct its_vpe *vpe,
  3289. struct its_cmd_info *info)
  3290. {
  3291. void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
  3292. u64 val = 0;
  3293. /* Schedule the VPE */
  3294. val |= GICR_VPENDBASER_Valid;
  3295. val |= info->g0en ? GICR_VPENDBASER_4_1_VGRP0EN : 0;
  3296. val |= info->g1en ? GICR_VPENDBASER_4_1_VGRP1EN : 0;
  3297. val |= FIELD_PREP(GICR_VPENDBASER_4_1_VPEID, vpe->vpe_id);
  3298. gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
  3299. }
  3300. static void its_vpe_4_1_deschedule(struct its_vpe *vpe,
  3301. struct its_cmd_info *info)
  3302. {
  3303. void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
  3304. u64 val;
  3305. if (info->req_db) {
  3306. unsigned long flags;
  3307. /*
  3308. * vPE is going to block: make the vPE non-resident with
  3309. * PendingLast clear and DB set. The GIC guarantees that if
  3310. * we read-back PendingLast clear, then a doorbell will be
  3311. * delivered when an interrupt comes.
  3312. *
  3313. * Note the locking to deal with the concurrent update of
  3314. * pending_last from the doorbell interrupt handler that can
  3315. * run concurrently.
  3316. */
  3317. raw_spin_lock_irqsave(&vpe->vpe_lock, flags);
  3318. val = its_clear_vpend_valid(vlpi_base,
  3319. GICR_VPENDBASER_PendingLast,
  3320. GICR_VPENDBASER_4_1_DB);
  3321. vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
  3322. raw_spin_unlock_irqrestore(&vpe->vpe_lock, flags);
  3323. } else {
  3324. /*
  3325. * We're not blocking, so just make the vPE non-resident
  3326. * with PendingLast set, indicating that we'll be back.
  3327. */
  3328. val = its_clear_vpend_valid(vlpi_base,
  3329. 0,
  3330. GICR_VPENDBASER_PendingLast);
  3331. vpe->pending_last = true;
  3332. }
  3333. }
  3334. static void its_vpe_4_1_invall(struct its_vpe *vpe)
  3335. {
  3336. void __iomem *rdbase;
  3337. unsigned long flags;
  3338. u64 val;
  3339. int cpu;
  3340. val = GICR_INVALLR_V;
  3341. val |= FIELD_PREP(GICR_INVALLR_VPEID, vpe->vpe_id);
  3342. /* Target the redistributor this vPE is currently known on */
  3343. cpu = vpe_to_cpuid_lock(vpe, &flags);
  3344. raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock);
  3345. rdbase = per_cpu_ptr(gic_rdists->rdist, cpu)->rd_base;
  3346. gic_write_lpir(val, rdbase + GICR_INVALLR);
  3347. wait_for_syncr(rdbase);
  3348. raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock);
  3349. vpe_to_cpuid_unlock(vpe, flags);
  3350. }
  3351. static int its_vpe_4_1_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
  3352. {
  3353. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  3354. struct its_cmd_info *info = vcpu_info;
  3355. switch (info->cmd_type) {
  3356. case SCHEDULE_VPE:
  3357. its_vpe_4_1_schedule(vpe, info);
  3358. return 0;
  3359. case DESCHEDULE_VPE:
  3360. its_vpe_4_1_deschedule(vpe, info);
  3361. return 0;
  3362. case COMMIT_VPE:
  3363. its_wait_vpt_parse_complete();
  3364. return 0;
  3365. case INVALL_VPE:
  3366. its_vpe_4_1_invall(vpe);
  3367. return 0;
  3368. default:
  3369. return -EINVAL;
  3370. }
  3371. }
  3372. static struct irq_chip its_vpe_4_1_irq_chip = {
  3373. .name = "GICv4.1-vpe",
  3374. .irq_mask = its_vpe_4_1_mask_irq,
  3375. .irq_unmask = its_vpe_4_1_unmask_irq,
  3376. .irq_eoi = irq_chip_eoi_parent,
  3377. .irq_set_affinity = its_vpe_set_affinity,
  3378. .irq_set_vcpu_affinity = its_vpe_4_1_set_vcpu_affinity,
  3379. };
  3380. static void its_configure_sgi(struct irq_data *d, bool clear)
  3381. {
  3382. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  3383. struct its_cmd_desc desc;
  3384. desc.its_vsgi_cmd.vpe = vpe;
  3385. desc.its_vsgi_cmd.sgi = d->hwirq;
  3386. desc.its_vsgi_cmd.priority = vpe->sgi_config[d->hwirq].priority;
  3387. desc.its_vsgi_cmd.enable = vpe->sgi_config[d->hwirq].enabled;
  3388. desc.its_vsgi_cmd.group = vpe->sgi_config[d->hwirq].group;
  3389. desc.its_vsgi_cmd.clear = clear;
  3390. /*
  3391. * GICv4.1 allows us to send VSGI commands to any ITS as long as the
  3392. * destination VPE is mapped there. Since we map them eagerly at
  3393. * activation time, we're pretty sure the first GICv4.1 ITS will do.
  3394. */
  3395. its_send_single_vcommand(find_4_1_its(), its_build_vsgi_cmd, &desc);
  3396. }
  3397. static void its_sgi_mask_irq(struct irq_data *d)
  3398. {
  3399. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  3400. vpe->sgi_config[d->hwirq].enabled = false;
  3401. its_configure_sgi(d, false);
  3402. }
  3403. static void its_sgi_unmask_irq(struct irq_data *d)
  3404. {
  3405. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  3406. vpe->sgi_config[d->hwirq].enabled = true;
  3407. its_configure_sgi(d, false);
  3408. }
  3409. static int its_sgi_set_affinity(struct irq_data *d,
  3410. const struct cpumask *mask_val,
  3411. bool force)
  3412. {
  3413. /*
  3414. * There is no notion of affinity for virtual SGIs, at least
  3415. * not on the host (since they can only be targeting a vPE).
  3416. * Tell the kernel we've done whatever it asked for.
  3417. */
  3418. irq_data_update_effective_affinity(d, mask_val);
  3419. return IRQ_SET_MASK_OK;
  3420. }
  3421. static int its_sgi_set_irqchip_state(struct irq_data *d,
  3422. enum irqchip_irq_state which,
  3423. bool state)
  3424. {
  3425. if (which != IRQCHIP_STATE_PENDING)
  3426. return -EINVAL;
  3427. if (state) {
  3428. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  3429. struct its_node *its = find_4_1_its();
  3430. u64 val;
  3431. val = FIELD_PREP(GITS_SGIR_VPEID, vpe->vpe_id);
  3432. val |= FIELD_PREP(GITS_SGIR_VINTID, d->hwirq);
  3433. writeq_relaxed(val, its->sgir_base + GITS_SGIR - SZ_128K);
  3434. } else {
  3435. its_configure_sgi(d, true);
  3436. }
  3437. return 0;
  3438. }
  3439. static int its_sgi_get_irqchip_state(struct irq_data *d,
  3440. enum irqchip_irq_state which, bool *val)
  3441. {
  3442. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  3443. void __iomem *base;
  3444. unsigned long flags;
  3445. u32 count = 1000000; /* 1s! */
  3446. u32 status;
  3447. int cpu;
  3448. if (which != IRQCHIP_STATE_PENDING)
  3449. return -EINVAL;
  3450. /*
  3451. * Locking galore! We can race against two different events:
  3452. *
  3453. * - Concurrent vPE affinity change: we must make sure it cannot
  3454. * happen, or we'll talk to the wrong redistributor. This is
  3455. * identical to what happens with vLPIs.
  3456. *
  3457. * - Concurrent VSGIPENDR access: As it involves accessing two
  3458. * MMIO registers, this must be made atomic one way or another.
  3459. */
  3460. cpu = vpe_to_cpuid_lock(vpe, &flags);
  3461. raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock);
  3462. base = gic_data_rdist_cpu(cpu)->rd_base + SZ_128K;
  3463. writel_relaxed(vpe->vpe_id, base + GICR_VSGIR);
  3464. do {
  3465. status = readl_relaxed(base + GICR_VSGIPENDR);
  3466. if (!(status & GICR_VSGIPENDR_BUSY))
  3467. goto out;
  3468. count--;
  3469. if (!count) {
  3470. pr_err_ratelimited("Unable to get SGI status\n");
  3471. goto out;
  3472. }
  3473. cpu_relax();
  3474. udelay(1);
  3475. } while (count);
  3476. out:
  3477. raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock);
  3478. vpe_to_cpuid_unlock(vpe, flags);
  3479. if (!count)
  3480. return -ENXIO;
  3481. *val = !!(status & (1 << d->hwirq));
  3482. return 0;
  3483. }
  3484. static int its_sgi_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
  3485. {
  3486. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  3487. struct its_cmd_info *info = vcpu_info;
  3488. switch (info->cmd_type) {
  3489. case PROP_UPDATE_VSGI:
  3490. vpe->sgi_config[d->hwirq].priority = info->priority;
  3491. vpe->sgi_config[d->hwirq].group = info->group;
  3492. its_configure_sgi(d, false);
  3493. return 0;
  3494. default:
  3495. return -EINVAL;
  3496. }
  3497. }
  3498. static struct irq_chip its_sgi_irq_chip = {
  3499. .name = "GICv4.1-sgi",
  3500. .irq_mask = its_sgi_mask_irq,
  3501. .irq_unmask = its_sgi_unmask_irq,
  3502. .irq_set_affinity = its_sgi_set_affinity,
  3503. .irq_set_irqchip_state = its_sgi_set_irqchip_state,
  3504. .irq_get_irqchip_state = its_sgi_get_irqchip_state,
  3505. .irq_set_vcpu_affinity = its_sgi_set_vcpu_affinity,
  3506. };
  3507. static int its_sgi_irq_domain_alloc(struct irq_domain *domain,
  3508. unsigned int virq, unsigned int nr_irqs,
  3509. void *args)
  3510. {
  3511. struct its_vpe *vpe = args;
  3512. int i;
  3513. /* Yes, we do want 16 SGIs */
  3514. WARN_ON(nr_irqs != 16);
  3515. for (i = 0; i < 16; i++) {
  3516. vpe->sgi_config[i].priority = 0;
  3517. vpe->sgi_config[i].enabled = false;
  3518. vpe->sgi_config[i].group = false;
  3519. irq_domain_set_hwirq_and_chip(domain, virq + i, i,
  3520. &its_sgi_irq_chip, vpe);
  3521. irq_set_status_flags(virq + i, IRQ_DISABLE_UNLAZY);
  3522. }
  3523. return 0;
  3524. }
  3525. static void its_sgi_irq_domain_free(struct irq_domain *domain,
  3526. unsigned int virq,
  3527. unsigned int nr_irqs)
  3528. {
  3529. /* Nothing to do */
  3530. }
  3531. static int its_sgi_irq_domain_activate(struct irq_domain *domain,
  3532. struct irq_data *d, bool reserve)
  3533. {
  3534. /* Write out the initial SGI configuration */
  3535. its_configure_sgi(d, false);
  3536. return 0;
  3537. }
  3538. static void its_sgi_irq_domain_deactivate(struct irq_domain *domain,
  3539. struct irq_data *d)
  3540. {
  3541. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  3542. /*
  3543. * The VSGI command is awkward:
  3544. *
  3545. * - To change the configuration, CLEAR must be set to false,
  3546. * leaving the pending bit unchanged.
  3547. * - To clear the pending bit, CLEAR must be set to true, leaving
  3548. * the configuration unchanged.
  3549. *
  3550. * You just can't do both at once, hence the two commands below.
  3551. */
  3552. vpe->sgi_config[d->hwirq].enabled = false;
  3553. its_configure_sgi(d, false);
  3554. its_configure_sgi(d, true);
  3555. }
  3556. static const struct irq_domain_ops its_sgi_domain_ops = {
  3557. .alloc = its_sgi_irq_domain_alloc,
  3558. .free = its_sgi_irq_domain_free,
  3559. .activate = its_sgi_irq_domain_activate,
  3560. .deactivate = its_sgi_irq_domain_deactivate,
  3561. };
  3562. static int its_vpe_id_alloc(void)
  3563. {
  3564. return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL);
  3565. }
  3566. static void its_vpe_id_free(u16 id)
  3567. {
  3568. ida_simple_remove(&its_vpeid_ida, id);
  3569. }
  3570. static int its_vpe_init(struct its_vpe *vpe)
  3571. {
  3572. struct page *vpt_page;
  3573. int vpe_id;
  3574. /* Allocate vpe_id */
  3575. vpe_id = its_vpe_id_alloc();
  3576. if (vpe_id < 0)
  3577. return vpe_id;
  3578. /* Allocate VPT */
  3579. vpt_page = its_allocate_pending_table(GFP_KERNEL);
  3580. if (!vpt_page) {
  3581. its_vpe_id_free(vpe_id);
  3582. return -ENOMEM;
  3583. }
  3584. if (!its_alloc_vpe_table(vpe_id)) {
  3585. its_vpe_id_free(vpe_id);
  3586. its_free_pending_table(vpt_page);
  3587. return -ENOMEM;
  3588. }
  3589. raw_spin_lock_init(&vpe->vpe_lock);
  3590. vpe->vpe_id = vpe_id;
  3591. vpe->vpt_page = vpt_page;
  3592. if (gic_rdists->has_rvpeid)
  3593. atomic_set(&vpe->vmapp_count, 0);
  3594. else
  3595. vpe->vpe_proxy_event = -1;
  3596. return 0;
  3597. }
  3598. static void its_vpe_teardown(struct its_vpe *vpe)
  3599. {
  3600. its_vpe_db_proxy_unmap(vpe);
  3601. its_vpe_id_free(vpe->vpe_id);
  3602. its_free_pending_table(vpe->vpt_page);
  3603. }
  3604. static void its_vpe_irq_domain_free(struct irq_domain *domain,
  3605. unsigned int virq,
  3606. unsigned int nr_irqs)
  3607. {
  3608. struct its_vm *vm = domain->host_data;
  3609. int i;
  3610. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  3611. for (i = 0; i < nr_irqs; i++) {
  3612. struct irq_data *data = irq_domain_get_irq_data(domain,
  3613. virq + i);
  3614. struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
  3615. BUG_ON(vm != vpe->its_vm);
  3616. clear_bit(data->hwirq, vm->db_bitmap);
  3617. its_vpe_teardown(vpe);
  3618. irq_domain_reset_irq_data(data);
  3619. }
  3620. if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
  3621. its_lpi_free(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
  3622. its_free_prop_table(vm->vprop_page);
  3623. }
  3624. }
  3625. static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  3626. unsigned int nr_irqs, void *args)
  3627. {
  3628. struct irq_chip *irqchip = &its_vpe_irq_chip;
  3629. struct its_vm *vm = args;
  3630. unsigned long *bitmap;
  3631. struct page *vprop_page;
  3632. int base, nr_ids, i, err = 0;
  3633. BUG_ON(!vm);
  3634. bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids);
  3635. if (!bitmap)
  3636. return -ENOMEM;
  3637. if (nr_ids < nr_irqs) {
  3638. its_lpi_free(bitmap, base, nr_ids);
  3639. return -ENOMEM;
  3640. }
  3641. vprop_page = its_allocate_prop_table(GFP_KERNEL);
  3642. if (!vprop_page) {
  3643. its_lpi_free(bitmap, base, nr_ids);
  3644. return -ENOMEM;
  3645. }
  3646. vm->db_bitmap = bitmap;
  3647. vm->db_lpi_base = base;
  3648. vm->nr_db_lpis = nr_ids;
  3649. vm->vprop_page = vprop_page;
  3650. if (gic_rdists->has_rvpeid)
  3651. irqchip = &its_vpe_4_1_irq_chip;
  3652. for (i = 0; i < nr_irqs; i++) {
  3653. vm->vpes[i]->vpe_db_lpi = base + i;
  3654. err = its_vpe_init(vm->vpes[i]);
  3655. if (err)
  3656. break;
  3657. err = its_irq_gic_domain_alloc(domain, virq + i,
  3658. vm->vpes[i]->vpe_db_lpi);
  3659. if (err)
  3660. break;
  3661. irq_domain_set_hwirq_and_chip(domain, virq + i, i,
  3662. irqchip, vm->vpes[i]);
  3663. set_bit(i, bitmap);
  3664. }
  3665. if (err) {
  3666. if (i > 0)
  3667. its_vpe_irq_domain_free(domain, virq, i);
  3668. its_lpi_free(bitmap, base, nr_ids);
  3669. its_free_prop_table(vprop_page);
  3670. }
  3671. return err;
  3672. }
  3673. static int its_vpe_irq_domain_activate(struct irq_domain *domain,
  3674. struct irq_data *d, bool reserve)
  3675. {
  3676. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  3677. struct its_node *its;
  3678. /*
  3679. * If we use the list map, we issue VMAPP on demand... Unless
  3680. * we're on a GICv4.1 and we eagerly map the VPE on all ITSs
  3681. * so that VSGIs can work.
  3682. */
  3683. if (!gic_requires_eager_mapping())
  3684. return 0;
  3685. /* Map the VPE to the first possible CPU */
  3686. vpe->col_idx = cpumask_first(cpu_online_mask);
  3687. list_for_each_entry(its, &its_nodes, entry) {
  3688. if (!is_v4(its))
  3689. continue;
  3690. its_send_vmapp(its, vpe, true);
  3691. its_send_vinvall(its, vpe);
  3692. }
  3693. irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
  3694. return 0;
  3695. }
  3696. static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
  3697. struct irq_data *d)
  3698. {
  3699. struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
  3700. struct its_node *its;
  3701. /*
  3702. * If we use the list map on GICv4.0, we unmap the VPE once no
  3703. * VLPIs are associated with the VM.
  3704. */
  3705. if (!gic_requires_eager_mapping())
  3706. return;
  3707. list_for_each_entry(its, &its_nodes, entry) {
  3708. if (!is_v4(its))
  3709. continue;
  3710. its_send_vmapp(its, vpe, false);
  3711. }
  3712. /*
  3713. * There may be a direct read to the VPT after unmapping the
  3714. * vPE, to guarantee the validity of this, we make the VPT
  3715. * memory coherent with the CPU caches here.
  3716. */
  3717. if (find_4_1_its() && !atomic_read(&vpe->vmapp_count))
  3718. gic_flush_dcache_to_poc(page_address(vpe->vpt_page),
  3719. LPI_PENDBASE_SZ);
  3720. }
  3721. static const struct irq_domain_ops its_vpe_domain_ops = {
  3722. .alloc = its_vpe_irq_domain_alloc,
  3723. .free = its_vpe_irq_domain_free,
  3724. .activate = its_vpe_irq_domain_activate,
  3725. .deactivate = its_vpe_irq_domain_deactivate,
  3726. };
  3727. static int its_force_quiescent(void __iomem *base)
  3728. {
  3729. u32 count = 1000000; /* 1s */
  3730. u32 val;
  3731. val = readl_relaxed(base + GITS_CTLR);
  3732. /*
  3733. * GIC architecture specification requires the ITS to be both
  3734. * disabled and quiescent for writes to GITS_BASER<n> or
  3735. * GITS_CBASER to not have UNPREDICTABLE results.
  3736. */
  3737. if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
  3738. return 0;
  3739. /* Disable the generation of all interrupts to this ITS */
  3740. val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
  3741. writel_relaxed(val, base + GITS_CTLR);
  3742. /* Poll GITS_CTLR and wait until ITS becomes quiescent */
  3743. while (1) {
  3744. val = readl_relaxed(base + GITS_CTLR);
  3745. if (val & GITS_CTLR_QUIESCENT)
  3746. return 0;
  3747. count--;
  3748. if (!count)
  3749. return -EBUSY;
  3750. cpu_relax();
  3751. udelay(1);
  3752. }
  3753. }
  3754. static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
  3755. {
  3756. struct its_node *its = data;
  3757. /* erratum 22375: only alloc 8MB table size (20 bits) */
  3758. its->typer &= ~GITS_TYPER_DEVBITS;
  3759. its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, 20 - 1);
  3760. its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
  3761. return true;
  3762. }
  3763. static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
  3764. {
  3765. struct its_node *its = data;
  3766. its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
  3767. return true;
  3768. }
  3769. static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
  3770. {
  3771. struct its_node *its = data;
  3772. /* On QDF2400, the size of the ITE is 16Bytes */
  3773. its->typer &= ~GITS_TYPER_ITT_ENTRY_SIZE;
  3774. its->typer |= FIELD_PREP(GITS_TYPER_ITT_ENTRY_SIZE, 16 - 1);
  3775. return true;
  3776. }
  3777. static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
  3778. {
  3779. struct its_node *its = its_dev->its;
  3780. /*
  3781. * The Socionext Synquacer SoC has a so-called 'pre-ITS',
  3782. * which maps 32-bit writes targeted at a separate window of
  3783. * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
  3784. * with device ID taken from bits [device_id_bits + 1:2] of
  3785. * the window offset.
  3786. */
  3787. return its->pre_its_base + (its_dev->device_id << 2);
  3788. }
  3789. static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
  3790. {
  3791. struct its_node *its = data;
  3792. u32 pre_its_window[2];
  3793. u32 ids;
  3794. if (!fwnode_property_read_u32_array(its->fwnode_handle,
  3795. "socionext,synquacer-pre-its",
  3796. pre_its_window,
  3797. ARRAY_SIZE(pre_its_window))) {
  3798. its->pre_its_base = pre_its_window[0];
  3799. its->get_msi_base = its_irq_get_msi_base_pre_its;
  3800. ids = ilog2(pre_its_window[1]) - 2;
  3801. if (device_ids(its) > ids) {
  3802. its->typer &= ~GITS_TYPER_DEVBITS;
  3803. its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, ids - 1);
  3804. }
  3805. /* the pre-ITS breaks isolation, so disable MSI remapping */
  3806. its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
  3807. return true;
  3808. }
  3809. return false;
  3810. }
  3811. static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
  3812. {
  3813. struct its_node *its = data;
  3814. /*
  3815. * Hip07 insists on using the wrong address for the VLPI
  3816. * page. Trick it into doing the right thing...
  3817. */
  3818. its->vlpi_redist_offset = SZ_128K;
  3819. return true;
  3820. }
  3821. static const struct gic_quirk its_quirks[] = {
  3822. #ifdef CONFIG_CAVIUM_ERRATUM_22375
  3823. {
  3824. .desc = "ITS: Cavium errata 22375, 24313",
  3825. .iidr = 0xa100034c, /* ThunderX pass 1.x */
  3826. .mask = 0xffff0fff,
  3827. .init = its_enable_quirk_cavium_22375,
  3828. },
  3829. #endif
  3830. #ifdef CONFIG_CAVIUM_ERRATUM_23144
  3831. {
  3832. .desc = "ITS: Cavium erratum 23144",
  3833. .iidr = 0xa100034c, /* ThunderX pass 1.x */
  3834. .mask = 0xffff0fff,
  3835. .init = its_enable_quirk_cavium_23144,
  3836. },
  3837. #endif
  3838. #ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
  3839. {
  3840. .desc = "ITS: QDF2400 erratum 0065",
  3841. .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
  3842. .mask = 0xffffffff,
  3843. .init = its_enable_quirk_qdf2400_e0065,
  3844. },
  3845. #endif
  3846. #ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
  3847. {
  3848. /*
  3849. * The Socionext Synquacer SoC incorporates ARM's own GIC-500
  3850. * implementation, but with a 'pre-ITS' added that requires
  3851. * special handling in software.
  3852. */
  3853. .desc = "ITS: Socionext Synquacer pre-ITS",
  3854. .iidr = 0x0001143b,
  3855. .mask = 0xffffffff,
  3856. .init = its_enable_quirk_socionext_synquacer,
  3857. },
  3858. #endif
  3859. #ifdef CONFIG_HISILICON_ERRATUM_161600802
  3860. {
  3861. .desc = "ITS: Hip07 erratum 161600802",
  3862. .iidr = 0x00000004,
  3863. .mask = 0xffffffff,
  3864. .init = its_enable_quirk_hip07_161600802,
  3865. },
  3866. #endif
  3867. {
  3868. }
  3869. };
  3870. static void its_enable_quirks(struct its_node *its)
  3871. {
  3872. u32 iidr = readl_relaxed(its->base + GITS_IIDR);
  3873. gic_enable_quirks(iidr, its_quirks, its);
  3874. }
  3875. static int its_save_disable(void)
  3876. {
  3877. struct its_node *its;
  3878. int err = 0;
  3879. raw_spin_lock(&its_lock);
  3880. list_for_each_entry(its, &its_nodes, entry) {
  3881. void __iomem *base;
  3882. base = its->base;
  3883. its->ctlr_save = readl_relaxed(base + GITS_CTLR);
  3884. err = its_force_quiescent(base);
  3885. if (err) {
  3886. pr_err("ITS@%pa: failed to quiesce: %d\n",
  3887. &its->phys_base, err);
  3888. writel_relaxed(its->ctlr_save, base + GITS_CTLR);
  3889. goto err;
  3890. }
  3891. its->cbaser_save = gits_read_cbaser(base + GITS_CBASER);
  3892. }
  3893. err:
  3894. if (err) {
  3895. list_for_each_entry_continue_reverse(its, &its_nodes, entry) {
  3896. void __iomem *base;
  3897. base = its->base;
  3898. writel_relaxed(its->ctlr_save, base + GITS_CTLR);
  3899. }
  3900. }
  3901. raw_spin_unlock(&its_lock);
  3902. return err;
  3903. }
  3904. static void its_restore_enable(void)
  3905. {
  3906. struct its_node *its;
  3907. int ret;
  3908. raw_spin_lock(&its_lock);
  3909. list_for_each_entry(its, &its_nodes, entry) {
  3910. void __iomem *base;
  3911. int i;
  3912. base = its->base;
  3913. /*
  3914. * Make sure that the ITS is disabled. If it fails to quiesce,
  3915. * don't restore it since writing to CBASER or BASER<n>
  3916. * registers is undefined according to the GIC v3 ITS
  3917. * Specification.
  3918. *
  3919. * Firmware resuming with the ITS enabled is terminally broken.
  3920. */
  3921. WARN_ON(readl_relaxed(base + GITS_CTLR) & GITS_CTLR_ENABLE);
  3922. ret = its_force_quiescent(base);
  3923. if (ret) {
  3924. pr_err("ITS@%pa: failed to quiesce on resume: %d\n",
  3925. &its->phys_base, ret);
  3926. continue;
  3927. }
  3928. gits_write_cbaser(its->cbaser_save, base + GITS_CBASER);
  3929. /*
  3930. * Writing CBASER resets CREADR to 0, so make CWRITER and
  3931. * cmd_write line up with it.
  3932. */
  3933. its->cmd_write = its->cmd_base;
  3934. gits_write_cwriter(0, base + GITS_CWRITER);
  3935. /* Restore GITS_BASER from the value cache. */
  3936. for (i = 0; i < GITS_BASER_NR_REGS; i++) {
  3937. struct its_baser *baser = &its->tables[i];
  3938. if (!(baser->val & GITS_BASER_VALID))
  3939. continue;
  3940. its_write_baser(its, baser, baser->val);
  3941. }
  3942. writel_relaxed(its->ctlr_save, base + GITS_CTLR);
  3943. /*
  3944. * Reinit the collection if it's stored in the ITS. This is
  3945. * indicated by the col_id being less than the HCC field.
  3946. * CID < HCC as specified in the GIC v3 Documentation.
  3947. */
  3948. if (its->collections[smp_processor_id()].col_id <
  3949. GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER)))
  3950. its_cpu_init_collection(its);
  3951. }
  3952. raw_spin_unlock(&its_lock);
  3953. }
  3954. static struct syscore_ops its_syscore_ops = {
  3955. .suspend = its_save_disable,
  3956. .resume = its_restore_enable,
  3957. };
  3958. static void __init __iomem *its_map_one(struct resource *res, int *err)
  3959. {
  3960. void __iomem *its_base;
  3961. u32 val;
  3962. its_base = ioremap(res->start, SZ_64K);
  3963. if (!its_base) {
  3964. pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
  3965. *err = -ENOMEM;
  3966. return NULL;
  3967. }
  3968. val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
  3969. if (val != 0x30 && val != 0x40) {
  3970. pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
  3971. *err = -ENODEV;
  3972. goto out_unmap;
  3973. }
  3974. *err = its_force_quiescent(its_base);
  3975. if (*err) {
  3976. pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
  3977. goto out_unmap;
  3978. }
  3979. return its_base;
  3980. out_unmap:
  3981. iounmap(its_base);
  3982. return NULL;
  3983. }
  3984. static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
  3985. {
  3986. struct irq_domain *inner_domain;
  3987. struct msi_domain_info *info;
  3988. info = kzalloc(sizeof(*info), GFP_KERNEL);
  3989. if (!info)
  3990. return -ENOMEM;
  3991. inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
  3992. if (!inner_domain) {
  3993. kfree(info);
  3994. return -ENOMEM;
  3995. }
  3996. inner_domain->parent = its_parent;
  3997. irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
  3998. inner_domain->flags |= its->msi_domain_flags;
  3999. info->ops = &its_msi_domain_ops;
  4000. info->data = its;
  4001. inner_domain->host_data = info;
  4002. return 0;
  4003. }
  4004. static int its_init_vpe_domain(void)
  4005. {
  4006. struct its_node *its;
  4007. u32 devid;
  4008. int entries;
  4009. if (gic_rdists->has_direct_lpi) {
  4010. pr_info("ITS: Using DirectLPI for VPE invalidation\n");
  4011. return 0;
  4012. }
  4013. /* Any ITS will do, even if not v4 */
  4014. its = list_first_entry(&its_nodes, struct its_node, entry);
  4015. entries = roundup_pow_of_two(nr_cpu_ids);
  4016. vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes),
  4017. GFP_KERNEL);
  4018. if (!vpe_proxy.vpes)
  4019. return -ENOMEM;
  4020. /* Use the last possible DevID */
  4021. devid = GENMASK(device_ids(its) - 1, 0);
  4022. vpe_proxy.dev = its_create_device(its, devid, entries, false);
  4023. if (!vpe_proxy.dev) {
  4024. kfree(vpe_proxy.vpes);
  4025. pr_err("ITS: Can't allocate GICv4 proxy device\n");
  4026. return -ENOMEM;
  4027. }
  4028. BUG_ON(entries > vpe_proxy.dev->nr_ites);
  4029. raw_spin_lock_init(&vpe_proxy.lock);
  4030. vpe_proxy.next_victim = 0;
  4031. pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
  4032. devid, vpe_proxy.dev->nr_ites);
  4033. return 0;
  4034. }
  4035. static int __init its_compute_its_list_map(struct resource *res,
  4036. void __iomem *its_base)
  4037. {
  4038. int its_number;
  4039. u32 ctlr;
  4040. /*
  4041. * This is assumed to be done early enough that we're
  4042. * guaranteed to be single-threaded, hence no
  4043. * locking. Should this change, we should address
  4044. * this.
  4045. */
  4046. its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
  4047. if (its_number >= GICv4_ITS_LIST_MAX) {
  4048. pr_err("ITS@%pa: No ITSList entry available!\n",
  4049. &res->start);
  4050. return -EINVAL;
  4051. }
  4052. ctlr = readl_relaxed(its_base + GITS_CTLR);
  4053. ctlr &= ~GITS_CTLR_ITS_NUMBER;
  4054. ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
  4055. writel_relaxed(ctlr, its_base + GITS_CTLR);
  4056. ctlr = readl_relaxed(its_base + GITS_CTLR);
  4057. if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
  4058. its_number = ctlr & GITS_CTLR_ITS_NUMBER;
  4059. its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
  4060. }
  4061. if (test_and_set_bit(its_number, &its_list_map)) {
  4062. pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
  4063. &res->start, its_number);
  4064. return -EINVAL;
  4065. }
  4066. return its_number;
  4067. }
  4068. static int __init its_probe_one(struct resource *res,
  4069. struct fwnode_handle *handle, int numa_node)
  4070. {
  4071. struct its_node *its;
  4072. void __iomem *its_base;
  4073. u64 baser, tmp, typer;
  4074. struct page *page;
  4075. u32 ctlr;
  4076. int err;
  4077. its_base = its_map_one(res, &err);
  4078. if (!its_base)
  4079. return err;
  4080. pr_info("ITS %pR\n", res);
  4081. its = kzalloc(sizeof(*its), GFP_KERNEL);
  4082. if (!its) {
  4083. err = -ENOMEM;
  4084. goto out_unmap;
  4085. }
  4086. raw_spin_lock_init(&its->lock);
  4087. mutex_init(&its->dev_alloc_lock);
  4088. INIT_LIST_HEAD(&its->entry);
  4089. INIT_LIST_HEAD(&its->its_device_list);
  4090. typer = gic_read_typer(its_base + GITS_TYPER);
  4091. its->typer = typer;
  4092. its->base = its_base;
  4093. its->phys_base = res->start;
  4094. if (is_v4(its)) {
  4095. if (!(typer & GITS_TYPER_VMOVP)) {
  4096. err = its_compute_its_list_map(res, its_base);
  4097. if (err < 0)
  4098. goto out_free_its;
  4099. its->list_nr = err;
  4100. pr_info("ITS@%pa: Using ITS number %d\n",
  4101. &res->start, err);
  4102. } else {
  4103. pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
  4104. }
  4105. if (is_v4_1(its)) {
  4106. u32 svpet = FIELD_GET(GITS_TYPER_SVPET, typer);
  4107. its->sgir_base = ioremap(res->start + SZ_128K, SZ_64K);
  4108. if (!its->sgir_base) {
  4109. err = -ENOMEM;
  4110. goto out_free_its;
  4111. }
  4112. its->mpidr = readl_relaxed(its_base + GITS_MPIDR);
  4113. pr_info("ITS@%pa: Using GICv4.1 mode %08x %08x\n",
  4114. &res->start, its->mpidr, svpet);
  4115. }
  4116. }
  4117. its->numa_node = numa_node;
  4118. page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
  4119. get_order(ITS_CMD_QUEUE_SZ));
  4120. if (!page) {
  4121. err = -ENOMEM;
  4122. goto out_unmap_sgir;
  4123. }
  4124. its->cmd_base = (void *)page_address(page);
  4125. its->cmd_write = its->cmd_base;
  4126. its->fwnode_handle = handle;
  4127. its->get_msi_base = its_irq_get_msi_base;
  4128. its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP;
  4129. its_enable_quirks(its);
  4130. err = its_alloc_tables(its);
  4131. if (err)
  4132. goto out_free_cmd;
  4133. err = its_alloc_collections(its);
  4134. if (err)
  4135. goto out_free_tables;
  4136. baser = (virt_to_phys(its->cmd_base) |
  4137. GITS_CBASER_RaWaWb |
  4138. GITS_CBASER_InnerShareable |
  4139. (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
  4140. GITS_CBASER_VALID);
  4141. gits_write_cbaser(baser, its->base + GITS_CBASER);
  4142. tmp = gits_read_cbaser(its->base + GITS_CBASER);
  4143. if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
  4144. if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
  4145. /*
  4146. * The HW reports non-shareable, we must
  4147. * remove the cacheability attributes as
  4148. * well.
  4149. */
  4150. baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
  4151. GITS_CBASER_CACHEABILITY_MASK);
  4152. baser |= GITS_CBASER_nC;
  4153. gits_write_cbaser(baser, its->base + GITS_CBASER);
  4154. }
  4155. pr_info("ITS: using cache flushing for cmd queue\n");
  4156. its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
  4157. }
  4158. gits_write_cwriter(0, its->base + GITS_CWRITER);
  4159. ctlr = readl_relaxed(its->base + GITS_CTLR);
  4160. ctlr |= GITS_CTLR_ENABLE;
  4161. if (is_v4(its))
  4162. ctlr |= GITS_CTLR_ImDe;
  4163. writel_relaxed(ctlr, its->base + GITS_CTLR);
  4164. err = its_init_domain(handle, its);
  4165. if (err)
  4166. goto out_free_tables;
  4167. raw_spin_lock(&its_lock);
  4168. list_add(&its->entry, &its_nodes);
  4169. raw_spin_unlock(&its_lock);
  4170. return 0;
  4171. out_free_tables:
  4172. its_free_tables(its);
  4173. out_free_cmd:
  4174. free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
  4175. out_unmap_sgir:
  4176. if (its->sgir_base)
  4177. iounmap(its->sgir_base);
  4178. out_free_its:
  4179. kfree(its);
  4180. out_unmap:
  4181. iounmap(its_base);
  4182. pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
  4183. return err;
  4184. }
  4185. static bool gic_rdists_supports_plpis(void)
  4186. {
  4187. return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
  4188. }
  4189. static int redist_disable_lpis(void)
  4190. {
  4191. void __iomem *rbase = gic_data_rdist_rd_base();
  4192. u64 timeout = USEC_PER_SEC;
  4193. u64 val;
  4194. if (!gic_rdists_supports_plpis()) {
  4195. pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
  4196. return -ENXIO;
  4197. }
  4198. val = readl_relaxed(rbase + GICR_CTLR);
  4199. if (!(val & GICR_CTLR_ENABLE_LPIS))
  4200. return 0;
  4201. /*
  4202. * If coming via a CPU hotplug event, we don't need to disable
  4203. * LPIs before trying to re-enable them. They are already
  4204. * configured and all is well in the world.
  4205. *
  4206. * If running with preallocated tables, there is nothing to do.
  4207. */
  4208. if ((gic_data_rdist()->flags & RD_LOCAL_LPI_ENABLED) ||
  4209. (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED))
  4210. return 0;
  4211. /*
  4212. * From that point on, we only try to do some damage control.
  4213. */
  4214. pr_warn("GICv3: CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
  4215. smp_processor_id());
  4216. add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
  4217. /* Disable LPIs */
  4218. val &= ~GICR_CTLR_ENABLE_LPIS;
  4219. writel_relaxed(val, rbase + GICR_CTLR);
  4220. /* Make sure any change to GICR_CTLR is observable by the GIC */
  4221. dsb(sy);
  4222. /*
  4223. * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs
  4224. * from 1 to 0 before programming GICR_PEND{PROP}BASER registers.
  4225. * Error out if we time out waiting for RWP to clear.
  4226. */
  4227. while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) {
  4228. if (!timeout) {
  4229. pr_err("CPU%d: Timeout while disabling LPIs\n",
  4230. smp_processor_id());
  4231. return -ETIMEDOUT;
  4232. }
  4233. udelay(1);
  4234. timeout--;
  4235. }
  4236. /*
  4237. * After it has been written to 1, it is IMPLEMENTATION
  4238. * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be
  4239. * cleared to 0. Error out if clearing the bit failed.
  4240. */
  4241. if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) {
  4242. pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id());
  4243. return -EBUSY;
  4244. }
  4245. return 0;
  4246. }
  4247. int its_cpu_init(void)
  4248. {
  4249. if (!list_empty(&its_nodes)) {
  4250. int ret;
  4251. ret = redist_disable_lpis();
  4252. if (ret)
  4253. return ret;
  4254. its_cpu_init_lpis();
  4255. its_cpu_init_collections();
  4256. }
  4257. return 0;
  4258. }
  4259. static void rdist_memreserve_cpuhp_cleanup_workfn(struct work_struct *work)
  4260. {
  4261. cpuhp_remove_state_nocalls(gic_rdists->cpuhp_memreserve_state);
  4262. gic_rdists->cpuhp_memreserve_state = CPUHP_INVALID;
  4263. }
  4264. static DECLARE_WORK(rdist_memreserve_cpuhp_cleanup_work,
  4265. rdist_memreserve_cpuhp_cleanup_workfn);
  4266. static int its_cpu_memreserve_lpi(unsigned int cpu)
  4267. {
  4268. struct page *pend_page;
  4269. int ret = 0;
  4270. /* This gets to run exactly once per CPU */
  4271. if (gic_data_rdist()->flags & RD_LOCAL_MEMRESERVE_DONE)
  4272. return 0;
  4273. pend_page = gic_data_rdist()->pend_page;
  4274. if (WARN_ON(!pend_page)) {
  4275. ret = -ENOMEM;
  4276. goto out;
  4277. }
  4278. /*
  4279. * If the pending table was pre-programmed, free the memory we
  4280. * preemptively allocated. Otherwise, reserve that memory for
  4281. * later kexecs.
  4282. */
  4283. if (gic_data_rdist()->flags & RD_LOCAL_PENDTABLE_PREALLOCATED) {
  4284. its_free_pending_table(pend_page);
  4285. gic_data_rdist()->pend_page = NULL;
  4286. } else {
  4287. phys_addr_t paddr = page_to_phys(pend_page);
  4288. WARN_ON(gic_reserve_range(paddr, LPI_PENDBASE_SZ));
  4289. }
  4290. out:
  4291. /* Last CPU being brought up gets to issue the cleanup */
  4292. if (!IS_ENABLED(CONFIG_SMP) ||
  4293. cpumask_equal(&cpus_booted_once_mask, cpu_possible_mask))
  4294. schedule_work(&rdist_memreserve_cpuhp_cleanup_work);
  4295. gic_data_rdist()->flags |= RD_LOCAL_MEMRESERVE_DONE;
  4296. return ret;
  4297. }
  4298. /* Mark all the BASER registers as invalid before they get reprogrammed */
  4299. static int __init its_reset_one(struct resource *res)
  4300. {
  4301. void __iomem *its_base;
  4302. int err, i;
  4303. its_base = its_map_one(res, &err);
  4304. if (!its_base)
  4305. return err;
  4306. for (i = 0; i < GITS_BASER_NR_REGS; i++)
  4307. gits_write_baser(0, its_base + GITS_BASER + (i << 3));
  4308. iounmap(its_base);
  4309. return 0;
  4310. }
  4311. static const struct of_device_id its_device_id[] = {
  4312. { .compatible = "arm,gic-v3-its", },
  4313. {},
  4314. };
  4315. static int __init its_of_probe(struct device_node *node)
  4316. {
  4317. struct device_node *np;
  4318. struct resource res;
  4319. /*
  4320. * Make sure *all* the ITS are reset before we probe any, as
  4321. * they may be sharing memory. If any of the ITS fails to
  4322. * reset, don't even try to go any further, as this could
  4323. * result in something even worse.
  4324. */
  4325. for (np = of_find_matching_node(node, its_device_id); np;
  4326. np = of_find_matching_node(np, its_device_id)) {
  4327. int err;
  4328. if (!of_device_is_available(np) ||
  4329. !of_property_read_bool(np, "msi-controller") ||
  4330. of_address_to_resource(np, 0, &res))
  4331. continue;
  4332. err = its_reset_one(&res);
  4333. if (err)
  4334. return err;
  4335. }
  4336. for (np = of_find_matching_node(node, its_device_id); np;
  4337. np = of_find_matching_node(np, its_device_id)) {
  4338. if (!of_device_is_available(np))
  4339. continue;
  4340. if (!of_property_read_bool(np, "msi-controller")) {
  4341. pr_warn("%pOF: no msi-controller property, ITS ignored\n",
  4342. np);
  4343. continue;
  4344. }
  4345. if (of_address_to_resource(np, 0, &res)) {
  4346. pr_warn("%pOF: no regs?\n", np);
  4347. continue;
  4348. }
  4349. its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
  4350. }
  4351. return 0;
  4352. }
  4353. #ifdef CONFIG_ACPI
  4354. #define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
  4355. #ifdef CONFIG_ACPI_NUMA
  4356. struct its_srat_map {
  4357. /* numa node id */
  4358. u32 numa_node;
  4359. /* GIC ITS ID */
  4360. u32 its_id;
  4361. };
  4362. static struct its_srat_map *its_srat_maps __initdata;
  4363. static int its_in_srat __initdata;
  4364. static int __init acpi_get_its_numa_node(u32 its_id)
  4365. {
  4366. int i;
  4367. for (i = 0; i < its_in_srat; i++) {
  4368. if (its_id == its_srat_maps[i].its_id)
  4369. return its_srat_maps[i].numa_node;
  4370. }
  4371. return NUMA_NO_NODE;
  4372. }
  4373. static int __init gic_acpi_match_srat_its(union acpi_subtable_headers *header,
  4374. const unsigned long end)
  4375. {
  4376. return 0;
  4377. }
  4378. static int __init gic_acpi_parse_srat_its(union acpi_subtable_headers *header,
  4379. const unsigned long end)
  4380. {
  4381. int node;
  4382. struct acpi_srat_gic_its_affinity *its_affinity;
  4383. its_affinity = (struct acpi_srat_gic_its_affinity *)header;
  4384. if (!its_affinity)
  4385. return -EINVAL;
  4386. if (its_affinity->header.length < sizeof(*its_affinity)) {
  4387. pr_err("SRAT: Invalid header length %d in ITS affinity\n",
  4388. its_affinity->header.length);
  4389. return -EINVAL;
  4390. }
  4391. /*
  4392. * Note that in theory a new proximity node could be created by this
  4393. * entry as it is an SRAT resource allocation structure.
  4394. * We do not currently support doing so.
  4395. */
  4396. node = pxm_to_node(its_affinity->proximity_domain);
  4397. if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
  4398. pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
  4399. return 0;
  4400. }
  4401. its_srat_maps[its_in_srat].numa_node = node;
  4402. its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
  4403. its_in_srat++;
  4404. pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
  4405. its_affinity->proximity_domain, its_affinity->its_id, node);
  4406. return 0;
  4407. }
  4408. static void __init acpi_table_parse_srat_its(void)
  4409. {
  4410. int count;
  4411. count = acpi_table_parse_entries(ACPI_SIG_SRAT,
  4412. sizeof(struct acpi_table_srat),
  4413. ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
  4414. gic_acpi_match_srat_its, 0);
  4415. if (count <= 0)
  4416. return;
  4417. its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map),
  4418. GFP_KERNEL);
  4419. if (!its_srat_maps)
  4420. return;
  4421. acpi_table_parse_entries(ACPI_SIG_SRAT,
  4422. sizeof(struct acpi_table_srat),
  4423. ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
  4424. gic_acpi_parse_srat_its, 0);
  4425. }
  4426. /* free the its_srat_maps after ITS probing */
  4427. static void __init acpi_its_srat_maps_free(void)
  4428. {
  4429. kfree(its_srat_maps);
  4430. }
  4431. #else
  4432. static void __init acpi_table_parse_srat_its(void) { }
  4433. static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
  4434. static void __init acpi_its_srat_maps_free(void) { }
  4435. #endif
  4436. static int __init gic_acpi_parse_madt_its(union acpi_subtable_headers *header,
  4437. const unsigned long end)
  4438. {
  4439. struct acpi_madt_generic_translator *its_entry;
  4440. struct fwnode_handle *dom_handle;
  4441. struct resource res;
  4442. int err;
  4443. its_entry = (struct acpi_madt_generic_translator *)header;
  4444. memset(&res, 0, sizeof(res));
  4445. res.start = its_entry->base_address;
  4446. res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
  4447. res.flags = IORESOURCE_MEM;
  4448. dom_handle = irq_domain_alloc_fwnode(&res.start);
  4449. if (!dom_handle) {
  4450. pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
  4451. &res.start);
  4452. return -ENOMEM;
  4453. }
  4454. err = iort_register_domain_token(its_entry->translation_id, res.start,
  4455. dom_handle);
  4456. if (err) {
  4457. pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
  4458. &res.start, its_entry->translation_id);
  4459. goto dom_err;
  4460. }
  4461. err = its_probe_one(&res, dom_handle,
  4462. acpi_get_its_numa_node(its_entry->translation_id));
  4463. if (!err)
  4464. return 0;
  4465. iort_deregister_domain_token(its_entry->translation_id);
  4466. dom_err:
  4467. irq_domain_free_fwnode(dom_handle);
  4468. return err;
  4469. }
  4470. static int __init its_acpi_reset(union acpi_subtable_headers *header,
  4471. const unsigned long end)
  4472. {
  4473. struct acpi_madt_generic_translator *its_entry;
  4474. struct resource res;
  4475. its_entry = (struct acpi_madt_generic_translator *)header;
  4476. res = (struct resource) {
  4477. .start = its_entry->base_address,
  4478. .end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1,
  4479. .flags = IORESOURCE_MEM,
  4480. };
  4481. return its_reset_one(&res);
  4482. }
  4483. static void __init its_acpi_probe(void)
  4484. {
  4485. acpi_table_parse_srat_its();
  4486. /*
  4487. * Make sure *all* the ITS are reset before we probe any, as
  4488. * they may be sharing memory. If any of the ITS fails to
  4489. * reset, don't even try to go any further, as this could
  4490. * result in something even worse.
  4491. */
  4492. if (acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
  4493. its_acpi_reset, 0) > 0)
  4494. acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
  4495. gic_acpi_parse_madt_its, 0);
  4496. acpi_its_srat_maps_free();
  4497. }
  4498. #else
  4499. static void __init its_acpi_probe(void) { }
  4500. #endif
  4501. int __init its_lpi_memreserve_init(void)
  4502. {
  4503. int state;
  4504. if (!efi_enabled(EFI_CONFIG_TABLES))
  4505. return 0;
  4506. if (list_empty(&its_nodes))
  4507. return 0;
  4508. gic_rdists->cpuhp_memreserve_state = CPUHP_INVALID;
  4509. state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  4510. "irqchip/arm/gicv3/memreserve:online",
  4511. its_cpu_memreserve_lpi,
  4512. NULL);
  4513. if (state < 0)
  4514. return state;
  4515. gic_rdists->cpuhp_memreserve_state = state;
  4516. return 0;
  4517. }
  4518. int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
  4519. struct irq_domain *parent_domain)
  4520. {
  4521. struct device_node *of_node;
  4522. struct its_node *its;
  4523. bool has_v4 = false;
  4524. bool has_v4_1 = false;
  4525. int err;
  4526. gic_rdists = rdists;
  4527. its_parent = parent_domain;
  4528. of_node = to_of_node(handle);
  4529. if (of_node)
  4530. its_of_probe(of_node);
  4531. else
  4532. its_acpi_probe();
  4533. if (list_empty(&its_nodes)) {
  4534. pr_warn("ITS: No ITS available, not enabling LPIs\n");
  4535. return -ENXIO;
  4536. }
  4537. err = allocate_lpi_tables();
  4538. if (err)
  4539. return err;
  4540. list_for_each_entry(its, &its_nodes, entry) {
  4541. has_v4 |= is_v4(its);
  4542. has_v4_1 |= is_v4_1(its);
  4543. }
  4544. /* Don't bother with inconsistent systems */
  4545. if (WARN_ON(!has_v4_1 && rdists->has_rvpeid))
  4546. rdists->has_rvpeid = false;
  4547. if (has_v4 & rdists->has_vlpis) {
  4548. const struct irq_domain_ops *sgi_ops;
  4549. if (has_v4_1)
  4550. sgi_ops = &its_sgi_domain_ops;
  4551. else
  4552. sgi_ops = NULL;
  4553. if (its_init_vpe_domain() ||
  4554. its_init_v4(parent_domain, &its_vpe_domain_ops, sgi_ops)) {
  4555. rdists->has_vlpis = false;
  4556. pr_err("ITS: Disabling GICv4 support\n");
  4557. }
  4558. }
  4559. register_syscore_ops(&its_syscore_ops);
  4560. return 0;
  4561. }