gsi.c 162 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/of.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/io.h>
  9. #include <linux/log2.h>
  10. #include <linux/module.h>
  11. #include <linux/msm_gsi.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/delay.h>
  14. #include <linux/msi.h>
  15. #include <linux/smp.h>
  16. #include "gsi.h"
  17. #include "gsi_emulation.h"
  18. #include "gsihal.h"
  19. #include <asm/arch_timer.h>
  20. #include <linux/sched/clock.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/sched.h>
  23. #include <linux/wait.h>
  24. #include <linux/delay.h>
  25. #include <linux/version.h>
  26. #define GSI_CMD_TIMEOUT (5*HZ)
  27. #define GSI_FC_CMD_TIMEOUT (2*GSI_CMD_TIMEOUT)
  28. #define GSI_START_CMD_TIMEOUT_MS 1000
  29. #define GSI_CMD_POLL_CNT 5
  30. #define GSI_STOP_CMD_TIMEOUT_MS 200
  31. #define GSI_MAX_CH_LOW_WEIGHT 15
  32. #define GSI_IRQ_STORM_THR 5
  33. #define GSI_FC_MAX_TIMEOUT 5
  34. #define GSI_STOP_CMD_POLL_CNT 4
  35. #define GSI_STOP_IN_PROC_CMD_POLL_CNT 2
  36. #define GSI_RESET_WA_MIN_SLEEP 1000
  37. #define GSI_RESET_WA_MAX_SLEEP 2000
  38. #define GSI_CHNL_STATE_MAX_RETRYCNT 10
  39. #define GSI_STTS_REG_BITS 32
  40. #define GSI_MSB_MASK 0xFFFFFFFF00000000ULL
  41. #define GSI_LSB_MASK 0x00000000FFFFFFFFULL
  42. #define GSI_MSB(num) ((u32)((num & GSI_MSB_MASK) >> 32))
  43. #define GSI_LSB(num) ((u32)(num & GSI_LSB_MASK))
  44. #define GSI_INST_RAM_FW_VER_OFFSET (0)
  45. #define GSI_INST_RAM_FW_VER_GSI_3_0_OFFSET (64)
  46. #define GSI_INST_RAM_FW_VER_HW_MASK (0xFC00)
  47. #define GSI_INST_RAM_FW_VER_HW_SHIFT (10)
  48. #define GSI_INST_RAM_FW_VER_FLAVOR_MASK (0x380)
  49. #define GSI_INST_RAM_FW_VER_FLAVOR_SHIFT (7)
  50. #define GSI_INST_RAM_FW_VER_FW_MASK (0x7f)
  51. #define GSI_INST_RAM_FW_VER_FW_SHIFT (0)
  52. #define GSI_FC_NUM_WORDS_PER_CHNL_SHRAM (20)
  53. #define GSI_FC_STATE_INDEX_SHRAM (7)
  54. #define GSI_FC_PENDING_MASK (0x00080000)
  55. #define GSI_NTN3_PENDING_DB_AFTER_RB_MASK 18
  56. #define GSI_NTN3_PENDING_DB_AFTER_RB_SHIFT 1
  57. /* FOR_SEQ_HIGH channel scratch: (((8 * (pipe_id * ctx_size + offset_lines)) + 4) / 4) */
  58. #define GSI_GSI_SHRAM_n_EP_FOR_SEQ_HIGH_N_GET(ep_id) (((8 * (ep_id * 10 + 9)) + 4) / 4)
  59. #ifndef CONFIG_DEBUG_FS
  60. void gsi_debugfs_init(void)
  61. {
  62. }
  63. #endif
  64. static const struct of_device_id msm_gsi_match[] = {
  65. { .compatible = "qcom,msm_gsi", },
  66. { },
  67. };
  68. #if defined(CONFIG_IPA_EMULATION)
  69. static bool running_emulation = true;
  70. #else
  71. static bool running_emulation;
  72. #endif
  73. struct gsi_ctx *gsi_ctx;
  74. static union __packed gsi_channel_scratch __gsi_update_mhi_channel_scratch(
  75. unsigned long chan_hdl, struct __packed gsi_mhi_channel_scratch mscr);
  76. static void __gsi_config_type_irq(int ee, uint32_t mask, uint32_t val)
  77. {
  78. uint32_t curr;
  79. curr = gsihal_read_reg_n(GSI_EE_n_CNTXT_TYPE_IRQ_MSK, ee);
  80. gsihal_write_reg_n(GSI_EE_n_CNTXT_TYPE_IRQ_MSK, ee,
  81. (curr & ~mask) | (val & mask));
  82. }
  83. static void __gsi_config_ch_irq(int ee, uint32_t mask, uint32_t val)
  84. {
  85. uint32_t curr;
  86. curr = gsihal_read_reg_n(GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_MSK, ee);
  87. gsihal_write_reg_n(GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_MSK, ee,
  88. (curr & ~mask) | (val & mask));
  89. }
  90. static void __gsi_config_all_ch_irq(int ee, uint32_t mask, uint32_t val)
  91. {
  92. uint32_t curr, k, max_k;
  93. max_k = gsihal_get_bit_map_array_size();
  94. for (k = 0; k < max_k; k++)
  95. {
  96. curr = gsihal_read_reg_nk(GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_MSK_k, ee, k);
  97. gsihal_write_reg_nk(GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_MSK_k, ee, k,
  98. (curr & ~mask) | (val & mask));
  99. }
  100. }
  101. static void __gsi_config_evt_irq(int ee, uint32_t mask, uint32_t val)
  102. {
  103. uint32_t curr;
  104. curr = gsihal_read_reg_n(GSI_EE_n_CNTXT_SRC_EV_CH_IRQ_MSK, ee);
  105. gsihal_write_reg_n(GSI_EE_n_CNTXT_SRC_EV_CH_IRQ_MSK, ee,
  106. (curr & ~mask) | (val & mask));
  107. }
  108. static void __gsi_config_all_evt_irq(int ee, uint32_t mask, uint32_t val)
  109. {
  110. uint32_t curr, k, max_k;
  111. max_k = gsihal_get_bit_map_array_size();
  112. for (k = 0; k < max_k; k++)
  113. {
  114. curr = gsihal_read_reg_nk(GSI_EE_n_CNTXT_SRC_EV_CH_IRQ_MSK_k, ee, k);
  115. gsihal_write_reg_nk(GSI_EE_n_CNTXT_SRC_EV_CH_IRQ_MSK_k, ee, k,
  116. (curr & ~mask) | (val & mask));
  117. }
  118. }
  119. static void __gsi_config_ieob_irq(int ee, uint32_t mask, uint32_t val)
  120. {
  121. uint32_t curr;
  122. curr = gsihal_read_reg_n(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_MSK, ee);
  123. gsihal_write_reg_n(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_MSK, ee,
  124. (curr & ~mask) | (val & mask));
  125. GSIDBG("current IEO_IRQ_MSK: 0x%x, change to: 0x%x\n",
  126. curr, ((curr & ~mask) | (val & mask)));
  127. }
  128. static void __gsi_config_all_ieob_irq(int ee, uint32_t mask, uint32_t val)
  129. {
  130. uint32_t curr, k, max_k;
  131. max_k = gsihal_get_bit_map_array_size();
  132. for (k = 0; k < max_k; k++)
  133. {
  134. curr = gsihal_read_reg_nk(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_MSK_k, ee, k);
  135. gsihal_write_reg_nk(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_MSK_k, ee, k,
  136. (curr & ~mask) | (val & mask));
  137. GSIDBG("current IEO_IRQ_MSK: 0x%x, change to: 0x%x\n",
  138. curr, ((curr & ~mask) | (val & mask)));
  139. }
  140. }
  141. static void __gsi_config_ieob_irq_k(int ee, uint32_t k, uint32_t mask, uint32_t val)
  142. {
  143. uint32_t curr;
  144. curr = gsihal_read_reg_nk(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_MSK_k, ee, k);
  145. gsihal_write_reg_nk(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_MSK_k, ee, k,
  146. (curr & ~mask) | (val & mask));
  147. GSIDBG("current IEO_IRQ_MSK: 0x%x, change to: 0x%x\n",
  148. curr, ((curr & ~mask) | (val & mask)));
  149. }
  150. static void __gsi_config_glob_irq(int ee, uint32_t mask, uint32_t val)
  151. {
  152. uint32_t curr;
  153. curr = gsihal_read_reg_n(GSI_EE_n_CNTXT_GLOB_IRQ_EN, ee);
  154. gsihal_write_reg_n(GSI_EE_n_CNTXT_GLOB_IRQ_EN, ee,
  155. (curr & ~mask) | (val & mask));
  156. }
  157. static void __gsi_config_gen_irq(int ee, uint32_t mask, uint32_t val)
  158. {
  159. uint32_t curr;
  160. curr = gsihal_read_reg_n(GSI_EE_n_CNTXT_GSI_IRQ_EN, ee);
  161. gsihal_write_reg_n(GSI_EE_n_CNTXT_GSI_IRQ_EN, ee,
  162. (curr & ~mask) | (val & mask));
  163. }
  164. static void gsi_channel_state_change_wait(unsigned long chan_hdl,
  165. struct gsi_chan_ctx *ctx,
  166. uint32_t tm, enum gsi_ch_cmd_opcode op)
  167. {
  168. int poll_cnt;
  169. int gsi_pending_intr;
  170. int res;
  171. struct gsihal_reg_ctx_type_irq type;
  172. struct gsihal_reg_ch_k_cntxt_0 ch_k_cntxt_0;
  173. int ee = gsi_ctx->per.ee;
  174. enum gsi_chan_state curr_state = GSI_CHAN_STATE_NOT_ALLOCATED;
  175. int stop_in_proc_retry = 0;
  176. int stop_retry = 0;
  177. /*
  178. * Start polling the GSI channel for
  179. * duration = tm * GSI_CMD_POLL_CNT.
  180. * We need to do polling of gsi state for improving debugability
  181. * of gsi hw state.
  182. */
  183. for (poll_cnt = 0;
  184. poll_cnt < GSI_CMD_POLL_CNT;
  185. poll_cnt++) {
  186. res = wait_for_completion_timeout(&ctx->compl,
  187. msecs_to_jiffies(tm));
  188. /* Interrupt received, return */
  189. if (res != 0)
  190. return;
  191. gsihal_read_reg_n_fields(GSI_EE_n_CNTXT_TYPE_IRQ, ee, &type);
  192. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  193. gsi_pending_intr = gsihal_read_reg_nk(
  194. GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_k,
  195. ee, gsihal_get_ch_reg_idx(chan_hdl));
  196. } else {
  197. gsi_pending_intr = gsihal_read_reg_n(
  198. GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ, ee);
  199. }
  200. if (gsi_ctx->per.ver == GSI_VER_1_0) {
  201. gsihal_read_reg_nk_fields(GSI_EE_n_GSI_CH_k_CNTXT_0,
  202. ee, chan_hdl, &ch_k_cntxt_0);
  203. curr_state = ch_k_cntxt_0.chstate;
  204. }
  205. /* Update the channel state only if interrupt was raised
  206. * on particular channel and also checking global interrupt
  207. * is raised for channel control.
  208. */
  209. if ((type.ch_ctrl) &&
  210. (gsi_pending_intr & gsihal_get_ch_reg_mask(chan_hdl))) {
  211. /*
  212. * Check channel state here in case the channel is
  213. * already started but interrupt is not yet received.
  214. */
  215. gsihal_read_reg_nk_fields(GSI_EE_n_GSI_CH_k_CNTXT_0,
  216. ee, chan_hdl, &ch_k_cntxt_0);
  217. curr_state = ch_k_cntxt_0.chstate;
  218. }
  219. if (op == GSI_CH_START) {
  220. if (curr_state == GSI_CHAN_STATE_STARTED ||
  221. curr_state == GSI_CHAN_STATE_FLOW_CONTROL) {
  222. ctx->state = curr_state;
  223. return;
  224. }
  225. }
  226. if (op == GSI_CH_STOP) {
  227. if (curr_state == GSI_CHAN_STATE_STOPPED)
  228. stop_retry++;
  229. else if (curr_state == GSI_CHAN_STATE_STOP_IN_PROC)
  230. stop_in_proc_retry++;
  231. }
  232. /* if interrupt marked reg after poll count reaching to max
  233. * keep loop to continue reach max stop proc and max stop count.
  234. */
  235. if (stop_retry == 1 || stop_in_proc_retry == 1)
  236. poll_cnt = 0;
  237. /* If stop channel retry reached to max count
  238. * clear the pending interrupt, if channel already stopped.
  239. */
  240. if (stop_retry == GSI_STOP_CMD_POLL_CNT) {
  241. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  242. gsihal_write_reg_nk(GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_CLR_k,
  243. ee, gsihal_get_ch_reg_idx(chan_hdl),
  244. gsi_pending_intr);
  245. }
  246. else {
  247. gsihal_write_reg_n(GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_CLR,
  248. ee,
  249. gsi_pending_intr);
  250. }
  251. ctx->state = curr_state;
  252. return;
  253. }
  254. /* If channel state stop in progress case no need
  255. * to wait for long time.
  256. */
  257. if (stop_in_proc_retry == GSI_STOP_IN_PROC_CMD_POLL_CNT) {
  258. ctx->state = curr_state;
  259. return;
  260. }
  261. GSIDBG("GSI wait on chan_hld=%lu irqtyp=%u state=%u intr=%u\n",
  262. chan_hdl,
  263. type,
  264. ctx->state,
  265. gsi_pending_intr);
  266. }
  267. GSIDBG("invalidating the channel state when timeout happens\n");
  268. ctx->state = curr_state;
  269. }
  270. static void gsi_handle_ch_ctrl(int ee)
  271. {
  272. uint32_t ch;
  273. int i, k, max_k;
  274. uint32_t ch_hdl;
  275. struct gsihal_reg_ch_k_cntxt_0 ch_k_cntxt_0;
  276. struct gsi_chan_ctx *ctx;
  277. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  278. max_k = gsihal_get_bit_map_array_size();
  279. for (k = 0; k < max_k; k++) {
  280. ch = gsihal_read_reg_nk(GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_k, ee, k);
  281. gsihal_write_reg_nk(GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_CLR_k, ee, k, ch);
  282. GSIDBG("ch %x\n", ch);
  283. for (i = 0; i < GSI_STTS_REG_BITS; i++) {
  284. if ((1 << i) & ch) {
  285. ch_hdl = i + (GSI_STTS_REG_BITS * k);
  286. if (ch_hdl >= gsi_ctx->max_ch ||
  287. ch_hdl >= GSI_CHAN_MAX) {
  288. GSIERR("invalid channel %d\n",
  289. ch_hdl);
  290. break;
  291. }
  292. ctx = &gsi_ctx->chan[ch_hdl];
  293. gsihal_read_reg_nk_fields(GSI_EE_n_GSI_CH_k_CNTXT_0,
  294. ee, ch_hdl, &ch_k_cntxt_0);
  295. ctx->state = ch_k_cntxt_0.chstate;
  296. GSIDBG("ch %u state updated to %u\n",
  297. ch_hdl, ctx->state);
  298. complete(&ctx->compl);
  299. gsi_ctx->ch_dbg[ch_hdl].cmd_completed++;
  300. }
  301. }
  302. }
  303. } else {
  304. ch = gsihal_read_reg_n(GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ, ee);
  305. gsihal_write_reg_n(GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_CLR, ee, ch);
  306. GSIDBG("ch %x\n", ch);
  307. for (i = 0; i < GSI_STTS_REG_BITS; i++) {
  308. if ((1 << i) & ch) {
  309. if (i >= gsi_ctx->max_ch ||
  310. i >= GSI_CHAN_MAX) {
  311. GSIERR("invalid channel %d\n", i);
  312. break;
  313. }
  314. ctx = &gsi_ctx->chan[i];
  315. gsihal_read_reg_nk_fields(GSI_EE_n_GSI_CH_k_CNTXT_0,
  316. ee, i, &ch_k_cntxt_0);
  317. ctx->state = ch_k_cntxt_0.chstate;
  318. GSIDBG("ch %u state updated to %u\n", i,
  319. ctx->state);
  320. complete(&ctx->compl);
  321. gsi_ctx->ch_dbg[i].cmd_completed++;
  322. }
  323. }
  324. }
  325. }
  326. static void gsi_handle_ev_ctrl(int ee)
  327. {
  328. uint32_t ch;
  329. int i, k;
  330. uint32_t evt_hdl, max_k;
  331. struct gsi_evt_ctx *ctx;
  332. struct gsihal_reg_ev_ch_k_cntxt_0 ev_ch_k_cntxt_0;
  333. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  334. max_k = gsihal_get_bit_map_array_size();
  335. for (k = 0; k < max_k; k++) {
  336. ch = gsihal_read_reg_nk(GSI_EE_n_CNTXT_SRC_EV_CH_IRQ_k, ee, k);
  337. gsihal_write_reg_nk(GSI_EE_n_CNTXT_SRC_EV_CH_IRQ_CLR_k, ee, k, ch);
  338. GSIDBG("ev %x\n", ch);
  339. for (i = 0; i < GSI_STTS_REG_BITS; i++) {
  340. if ((1 << i) & ch) {
  341. evt_hdl = i + (GSI_STTS_REG_BITS * k);
  342. if (evt_hdl >= gsi_ctx->max_ev ||
  343. evt_hdl >= GSI_EVT_RING_MAX) {
  344. GSIERR("invalid event %d\n",
  345. evt_hdl);
  346. break;
  347. }
  348. ctx = &gsi_ctx->evtr[evt_hdl];
  349. gsihal_read_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_0,
  350. ee, evt_hdl, &ev_ch_k_cntxt_0);
  351. ctx->state = ev_ch_k_cntxt_0.chstate;
  352. GSIDBG("evt %u state updated to %u\n",
  353. evt_hdl, ctx->state);
  354. complete(&ctx->compl);
  355. }
  356. }
  357. }
  358. } else {
  359. ch = gsihal_read_reg_n(GSI_EE_n_CNTXT_SRC_EV_CH_IRQ, ee);
  360. gsihal_write_reg_n(GSI_EE_n_CNTXT_SRC_EV_CH_IRQ_CLR, ee, ch);
  361. GSIDBG("ev %x\n", ch);
  362. for (i = 0; i < GSI_STTS_REG_BITS; i++) {
  363. if ((1 << i) & ch) {
  364. if (i >= gsi_ctx->max_ev ||
  365. i >= GSI_EVT_RING_MAX) {
  366. GSIERR("invalid event %d\n", i);
  367. break;
  368. }
  369. ctx = &gsi_ctx->evtr[i];
  370. gsihal_read_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_0,
  371. ee, i, &ev_ch_k_cntxt_0);
  372. ctx->state = ev_ch_k_cntxt_0.chstate;
  373. GSIDBG("evt %u state updated to %u\n", i,
  374. ctx->state);
  375. complete(&ctx->compl);
  376. }
  377. }
  378. }
  379. }
  380. static void gsi_handle_glob_err(uint32_t err)
  381. {
  382. struct gsi_log_err *log;
  383. struct gsi_chan_ctx *ch;
  384. struct gsi_evt_ctx *ev;
  385. struct gsi_chan_err_notify chan_notify;
  386. struct gsi_evt_err_notify evt_notify;
  387. struct gsi_per_notify per_notify;
  388. enum gsi_err_type err_type;
  389. struct gsihal_reg_ch_k_cntxt_0 ch_k_cntxt_0;
  390. log = (struct gsi_log_err *)&err;
  391. GSIERR("log err_type=%u ee=%u idx=%u\n", log->err_type, log->ee,
  392. log->virt_idx);
  393. GSIERR("code=%u arg1=%u arg2=%u arg3=%u\n", log->code, log->arg1,
  394. log->arg2, log->arg3);
  395. err_type = log->err_type;
  396. /*
  397. * These are errors thrown by hardware. We need
  398. * BUG_ON() to capture the hardware state right
  399. * when it is unexpected.
  400. */
  401. switch (err_type) {
  402. case GSI_ERR_TYPE_GLOB:
  403. per_notify.evt_id = GSI_PER_EVT_GLOB_ERROR;
  404. per_notify.user_data = gsi_ctx->per.user_data;
  405. per_notify.data.err_desc = err & 0xFFFF;
  406. gsi_ctx->per.notify_cb(&per_notify);
  407. break;
  408. case GSI_ERR_TYPE_CHAN:
  409. if (WARN_ON(log->virt_idx >= gsi_ctx->max_ch)) {
  410. GSIERR("Unexpected ch %d\n", log->virt_idx);
  411. return;
  412. }
  413. ch = &gsi_ctx->chan[log->virt_idx];
  414. chan_notify.chan_user_data = ch->props.chan_user_data;
  415. chan_notify.err_desc = err & 0xFFFF;
  416. if (log->code == GSI_INVALID_TRE_ERR) {
  417. if (log->ee != gsi_ctx->per.ee) {
  418. GSIERR("unexpected EE in event %d\n", log->ee);
  419. GSI_ASSERT();
  420. }
  421. gsihal_read_reg_nk_fields(GSI_EE_n_GSI_CH_k_CNTXT_0,
  422. gsi_ctx->per.ee, log->virt_idx, &ch_k_cntxt_0);
  423. ch->state = ch_k_cntxt_0.chstate;
  424. GSIDBG("ch %u state updated to %u\n", log->virt_idx,
  425. ch->state);
  426. ch->stats.invalid_tre_error++;
  427. if (ch->state == GSI_CHAN_STATE_ERROR) {
  428. GSIERR("Unexpected channel state %d\n",
  429. ch->state);
  430. GSI_ASSERT();
  431. }
  432. chan_notify.evt_id = GSI_CHAN_INVALID_TRE_ERR;
  433. } else if (log->code == GSI_OUT_OF_BUFFERS_ERR) {
  434. if (log->ee != gsi_ctx->per.ee) {
  435. GSIERR("unexpected EE in event %d\n", log->ee);
  436. GSI_ASSERT();
  437. }
  438. chan_notify.evt_id = GSI_CHAN_OUT_OF_BUFFERS_ERR;
  439. } else if (log->code == GSI_OUT_OF_RESOURCES_ERR) {
  440. if (log->ee != gsi_ctx->per.ee) {
  441. GSIERR("unexpected EE in event %d\n", log->ee);
  442. GSI_ASSERT();
  443. }
  444. chan_notify.evt_id = GSI_CHAN_OUT_OF_RESOURCES_ERR;
  445. complete(&ch->compl);
  446. } else if (log->code == GSI_UNSUPPORTED_INTER_EE_OP_ERR) {
  447. chan_notify.evt_id =
  448. GSI_CHAN_UNSUPPORTED_INTER_EE_OP_ERR;
  449. } else if (log->code == GSI_NON_ALLOCATED_EVT_ACCESS_ERR) {
  450. if (log->ee != gsi_ctx->per.ee) {
  451. GSIERR("unexpected EE in event %d\n", log->ee);
  452. GSI_ASSERT();
  453. }
  454. chan_notify.evt_id =
  455. GSI_CHAN_NON_ALLOCATED_EVT_ACCESS_ERR;
  456. } else if (log->code == GSI_HWO_1_ERR) {
  457. if (log->ee != gsi_ctx->per.ee) {
  458. GSIERR("unexpected EE in event %d\n", log->ee);
  459. GSI_ASSERT();
  460. }
  461. chan_notify.evt_id = GSI_CHAN_HWO_1_ERR;
  462. } else {
  463. GSIERR("unexpected event log code %d\n", log->code);
  464. GSI_ASSERT();
  465. }
  466. ch->props.err_cb(&chan_notify);
  467. break;
  468. case GSI_ERR_TYPE_EVT:
  469. if (WARN_ON(log->virt_idx >= gsi_ctx->max_ev)) {
  470. GSIERR("Unexpected ev %d\n", log->virt_idx);
  471. return;
  472. }
  473. ev = &gsi_ctx->evtr[log->virt_idx];
  474. evt_notify.user_data = ev->props.user_data;
  475. evt_notify.err_desc = err & 0xFFFF;
  476. if (log->code == GSI_OUT_OF_BUFFERS_ERR) {
  477. if (log->ee != gsi_ctx->per.ee) {
  478. GSIERR("unexpected EE in event %d\n", log->ee);
  479. GSI_ASSERT();
  480. }
  481. evt_notify.evt_id = GSI_EVT_OUT_OF_BUFFERS_ERR;
  482. } else if (log->code == GSI_OUT_OF_RESOURCES_ERR) {
  483. if (log->ee != gsi_ctx->per.ee) {
  484. GSIERR("unexpected EE in event %d\n", log->ee);
  485. GSI_ASSERT();
  486. }
  487. evt_notify.evt_id = GSI_EVT_OUT_OF_RESOURCES_ERR;
  488. complete(&ev->compl);
  489. } else if (log->code == GSI_UNSUPPORTED_INTER_EE_OP_ERR) {
  490. evt_notify.evt_id = GSI_EVT_UNSUPPORTED_INTER_EE_OP_ERR;
  491. } else if (log->code == GSI_EVT_RING_EMPTY_ERR) {
  492. if (log->ee != gsi_ctx->per.ee) {
  493. GSIERR("unexpected EE in event %d\n", log->ee);
  494. GSI_ASSERT();
  495. }
  496. evt_notify.evt_id = GSI_EVT_EVT_RING_EMPTY_ERR;
  497. } else {
  498. GSIERR("unexpected event log code %d\n", log->code);
  499. GSI_ASSERT();
  500. }
  501. ev->props.err_cb(&evt_notify);
  502. break;
  503. }
  504. }
  505. static void gsi_handle_gp_int1(void)
  506. {
  507. complete(&gsi_ctx->gen_ee_cmd_compl);
  508. }
  509. static void gsi_handle_glob_ee(int ee)
  510. {
  511. uint32_t val;
  512. uint32_t err;
  513. struct gsi_per_notify notify;
  514. uint32_t clr = ~0;
  515. struct gsihal_reg_cntxt_glob_irq_stts cntxt_glob_irq_stts;
  516. val = gsihal_read_reg_n_fields(GSI_EE_n_CNTXT_GLOB_IRQ_STTS,
  517. ee, &cntxt_glob_irq_stts);
  518. notify.user_data = gsi_ctx->per.user_data;
  519. if(cntxt_glob_irq_stts.error_int) {
  520. err = gsihal_read_reg_n(GSI_EE_n_ERROR_LOG, ee);
  521. if (gsi_ctx->per.ver >= GSI_VER_1_2)
  522. gsihal_write_reg_n(GSI_EE_n_ERROR_LOG, ee, 0);
  523. gsihal_write_reg_n(GSI_EE_n_ERROR_LOG_CLR, ee, clr);
  524. gsi_handle_glob_err(err);
  525. }
  526. if (cntxt_glob_irq_stts.gp_int1)
  527. gsi_handle_gp_int1();
  528. if (cntxt_glob_irq_stts.gp_int2) {
  529. notify.evt_id = GSI_PER_EVT_GLOB_GP2;
  530. gsi_ctx->per.notify_cb(&notify);
  531. }
  532. if (cntxt_glob_irq_stts.gp_int3) {
  533. notify.evt_id = GSI_PER_EVT_GLOB_GP3;
  534. gsi_ctx->per.notify_cb(&notify);
  535. }
  536. gsihal_write_reg_n(GSI_EE_n_CNTXT_GLOB_IRQ_CLR, ee, val);
  537. }
  538. static void gsi_incr_ring_wp(struct gsi_ring_ctx *ctx)
  539. {
  540. ctx->wp_local += ctx->elem_sz;
  541. if (ctx->wp_local == ctx->end)
  542. ctx->wp_local = ctx->base;
  543. }
  544. static void gsi_incr_ring_rp(struct gsi_ring_ctx *ctx)
  545. {
  546. ctx->rp_local += ctx->elem_sz;
  547. if (ctx->rp_local == ctx->end)
  548. ctx->rp_local = ctx->base;
  549. }
  550. uint16_t gsi_find_idx_from_addr(struct gsi_ring_ctx *ctx, uint64_t addr)
  551. {
  552. WARN_ON(addr < ctx->base || addr >= ctx->end);
  553. return (uint32_t)(addr - ctx->base) / ctx->elem_sz;
  554. }
  555. static uint16_t gsi_get_complete_num(struct gsi_ring_ctx *ctx, uint64_t addr1,
  556. uint64_t addr2)
  557. {
  558. uint32_t addr_diff;
  559. GSIDBG_LOW("gsi base addr 0x%llx end addr 0x%llx\n",
  560. ctx->base, ctx->end);
  561. if (addr1 < ctx->base || addr1 >= ctx->end) {
  562. GSIERR("address = 0x%llx not in range\n", addr1);
  563. GSI_ASSERT();
  564. }
  565. if (addr2 < ctx->base || addr2 >= ctx->end) {
  566. GSIERR("address = 0x%llx not in range\n", addr2);
  567. GSI_ASSERT();
  568. }
  569. addr_diff = (uint32_t)(addr2 - addr1);
  570. if (addr1 < addr2)
  571. return addr_diff / ctx->elem_sz;
  572. else
  573. return (addr_diff + ctx->len) / ctx->elem_sz;
  574. }
  575. static void gsi_process_chan(struct gsi_xfer_compl_evt *evt,
  576. struct gsi_chan_xfer_notify *notify, bool callback)
  577. {
  578. uint32_t ch_id;
  579. struct gsi_chan_ctx *ch_ctx;
  580. uint16_t rp_idx;
  581. uint64_t rp;
  582. ch_id = evt->chid;
  583. if (WARN_ON(ch_id >= gsi_ctx->max_ch)) {
  584. GSIERR("Unexpected ch %d\n", ch_id);
  585. return;
  586. }
  587. ch_ctx = &gsi_ctx->chan[ch_id];
  588. if (WARN_ON(ch_ctx->props.prot != GSI_CHAN_PROT_GPI &&
  589. ch_ctx->props.prot != GSI_CHAN_PROT_GCI))
  590. return;
  591. if (evt->type != GSI_XFER_COMPL_TYPE_GCI) {
  592. rp = evt->xfer_ptr;
  593. if (ch_ctx->ring.rp_local != rp) {
  594. ch_ctx->stats.completed +=
  595. gsi_get_complete_num(&ch_ctx->ring,
  596. ch_ctx->ring.rp_local, rp);
  597. ch_ctx->ring.rp_local = rp;
  598. }
  599. /*
  600. * Increment RP local only in polling context to avoid
  601. * sys len mismatch.
  602. */
  603. if (!callback || (ch_ctx->props.dir == GSI_CHAN_DIR_TO_GSI &&
  604. !ch_ctx->props.tx_poll))
  605. /* the element at RP is also processed */
  606. gsi_incr_ring_rp(&ch_ctx->ring);
  607. ch_ctx->ring.rp = ch_ctx->ring.rp_local;
  608. rp_idx = gsi_find_idx_from_addr(&ch_ctx->ring, rp);
  609. notify->veid = GSI_VEID_DEFAULT;
  610. } else {
  611. rp_idx = evt->cookie;
  612. notify->veid = evt->veid;
  613. }
  614. WARN_ON(!ch_ctx->user_data[rp_idx].valid);
  615. notify->xfer_user_data = ch_ctx->user_data[rp_idx].p;
  616. /*
  617. * In suspend just before stopping the channel possible to receive
  618. * the IEOB interrupt and xfer pointer will not be processed in this
  619. * mode and moving channel poll mode. In resume after starting the
  620. * channel will receive the IEOB interrupt and xfer pointer will be
  621. * overwritten. To avoid this process all data in polling context.
  622. */
  623. if (!callback || (ch_ctx->props.dir == GSI_CHAN_DIR_TO_GSI &&
  624. !ch_ctx->props.tx_poll)) {
  625. ch_ctx->stats.completed++;
  626. ch_ctx->user_data[rp_idx].valid = false;
  627. }
  628. notify->chan_user_data = ch_ctx->props.chan_user_data;
  629. notify->evt_id = evt->code;
  630. notify->bytes_xfered = evt->len;
  631. if (callback) {
  632. if (atomic_read(&ch_ctx->poll_mode)) {
  633. GSIERR("Calling client callback in polling mode\n");
  634. WARN_ON(1);
  635. }
  636. ch_ctx->props.xfer_cb(notify);
  637. }
  638. }
  639. static void gsi_process_evt_re(struct gsi_evt_ctx *ctx,
  640. struct gsi_chan_xfer_notify *notify, bool callback)
  641. {
  642. struct gsi_xfer_compl_evt *evt;
  643. struct gsi_chan_ctx *ch_ctx;
  644. evt = (struct gsi_xfer_compl_evt *)(ctx->ring.base_va +
  645. ctx->ring.rp_local - ctx->ring.base);
  646. gsi_process_chan(evt, notify, callback);
  647. /*
  648. * Increment RP local only in polling context to avoid
  649. * sys len mismatch.
  650. */
  651. ch_ctx = &gsi_ctx->chan[evt->chid];
  652. if (callback && (ch_ctx->props.dir == GSI_CHAN_DIR_FROM_GSI ||
  653. ch_ctx->props.tx_poll))
  654. return;
  655. gsi_incr_ring_rp(&ctx->ring);
  656. /* recycle this element */
  657. gsi_incr_ring_wp(&ctx->ring);
  658. ctx->stats.completed++;
  659. }
  660. static void gsi_ring_evt_doorbell(struct gsi_evt_ctx *ctx)
  661. {
  662. uint32_t val;
  663. ctx->ring.wp = ctx->ring.wp_local;
  664. val = GSI_LSB(ctx->ring.wp_local);
  665. gsihal_write_reg_nk(GSI_EE_n_EV_CH_k_DOORBELL_0,
  666. gsi_ctx->per.ee, ctx->id, val);
  667. }
  668. void gsi_ring_evt_doorbell_polling_mode(unsigned long chan_hdl) {
  669. struct gsi_evt_ctx *ctx;
  670. ctx = gsi_ctx->chan[chan_hdl].evtr;
  671. gsi_ring_evt_doorbell(ctx);
  672. }
  673. EXPORT_SYMBOL(gsi_ring_evt_doorbell_polling_mode);
  674. static void gsi_ring_chan_doorbell(struct gsi_chan_ctx *ctx)
  675. {
  676. uint32_t val;
  677. /*
  678. * allocate new events for this channel first
  679. * before submitting the new TREs.
  680. * for TO_GSI channels the event ring doorbell is rang as part of
  681. * interrupt handling.
  682. */
  683. if (ctx->evtr && ctx->props.dir == GSI_CHAN_DIR_FROM_GSI)
  684. gsi_ring_evt_doorbell(ctx->evtr);
  685. ctx->ring.wp = ctx->ring.wp_local;
  686. val = GSI_LSB(ctx->ring.wp_local);
  687. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_DOORBELL_0,
  688. gsi_ctx->per.ee, ctx->props.ch_id, val);
  689. }
  690. static bool check_channel_polling(struct gsi_evt_ctx* ctx) {
  691. /* For shared event rings both channels will be marked */
  692. return atomic_read(&ctx->chan[0]->poll_mode);
  693. }
  694. static void gsi_handle_ieob(int ee)
  695. {
  696. uint32_t ch, evt_hdl;
  697. int i, k, max_k;
  698. uint64_t rp;
  699. struct gsi_evt_ctx *ctx;
  700. struct gsi_chan_xfer_notify notify;
  701. unsigned long flags;
  702. unsigned long cntr;
  703. uint32_t msk;
  704. bool empty;
  705. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  706. max_k = gsihal_get_bit_map_array_size();
  707. for (k = 0; k < max_k; k++) {
  708. ch = gsihal_read_reg_nk(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_k, ee, k);
  709. msk = gsihal_read_reg_nk(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_MSK_k, ee, k);
  710. gsihal_write_reg_nk(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_k, ee, k, ch & msk);
  711. for (i = 0; i < GSI_STTS_REG_BITS; i++) {
  712. if ((1 << i) & ch & msk) {
  713. evt_hdl = i + (GSI_STTS_REG_BITS * k);
  714. if (evt_hdl >= gsi_ctx->max_ev ||
  715. evt_hdl >= GSI_EVT_RING_MAX) {
  716. GSIERR("invalid event %d\n",
  717. evt_hdl);
  718. break;
  719. }
  720. ctx = &gsi_ctx->evtr[evt_hdl];
  721. /*
  722. * Don't handle MSI interrupts, only handle IEOB
  723. * IRQs
  724. */
  725. if (ctx->props.intr == GSI_INTR_MSI)
  726. continue;
  727. if (ctx->props.intf !=
  728. GSI_EVT_CHTYPE_GPI_EV) {
  729. GSIERR("Unexpected irq intf %d\n",
  730. ctx->props.intf);
  731. GSI_ASSERT();
  732. }
  733. spin_lock_irqsave(&ctx->ring.slock,
  734. flags);
  735. check_again_v3_0:
  736. cntr = 0;
  737. empty = true;
  738. rp = ctx->props.gsi_read_event_ring_rp(
  739. &ctx->props, ctx->id, ee);
  740. rp |= ctx->ring.rp & GSI_MSB_MASK;
  741. ctx->ring.rp = rp;
  742. while (ctx->ring.rp_local != rp) {
  743. ++cntr;
  744. if (check_channel_polling(ctx)) {
  745. cntr = 0;
  746. break;
  747. }
  748. gsi_process_evt_re(ctx, &notify,
  749. true);
  750. empty = false;
  751. }
  752. if (!empty)
  753. gsi_ring_evt_doorbell(ctx);
  754. if (cntr != 0)
  755. goto check_again_v3_0;
  756. spin_unlock_irqrestore(&ctx->ring.slock,
  757. flags);
  758. }
  759. }
  760. }
  761. } else {
  762. ch = gsihal_read_reg_n(GSI_EE_n_CNTXT_SRC_IEOB_IRQ, ee);
  763. msk = gsihal_read_reg_n(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_MSK, ee);
  764. gsihal_write_reg_n(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR, ee, ch & msk);
  765. for (i = 0; i < GSI_STTS_REG_BITS; i++) {
  766. if ((1 << i) & ch & msk) {
  767. if (i >= gsi_ctx->max_ev ||
  768. i >= GSI_EVT_RING_MAX) {
  769. GSIERR("invalid event %d\n", i);
  770. break;
  771. }
  772. ctx = &gsi_ctx->evtr[i];
  773. /*
  774. * Don't handle MSI interrupts, only handle IEOB
  775. * IRQs
  776. */
  777. if (ctx->props.intr == GSI_INTR_MSI)
  778. continue;
  779. if (ctx->props.intf != GSI_EVT_CHTYPE_GPI_EV) {
  780. GSIERR("Unexpected irq intf %d\n",
  781. ctx->props.intf);
  782. GSI_ASSERT();
  783. }
  784. spin_lock_irqsave(&ctx->ring.slock, flags);
  785. check_again:
  786. cntr = 0;
  787. empty = true;
  788. rp = ctx->props.gsi_read_event_ring_rp(
  789. &ctx->props, ctx->id, ee);
  790. rp |= ctx->ring.rp & GSI_MSB_MASK;
  791. ctx->ring.rp = rp;
  792. while (ctx->ring.rp_local != rp) {
  793. ++cntr;
  794. if (check_channel_polling(ctx)) {
  795. cntr = 0;
  796. break;
  797. }
  798. gsi_process_evt_re(ctx, &notify, true);
  799. empty = false;
  800. }
  801. if (!empty)
  802. gsi_ring_evt_doorbell(ctx);
  803. if (cntr != 0)
  804. goto check_again;
  805. spin_unlock_irqrestore(&ctx->ring.slock, flags);
  806. }
  807. }
  808. }
  809. }
  810. static void gsi_handle_inter_ee_ch_ctrl(int ee)
  811. {
  812. uint32_t ch, ch_hdl;
  813. int i, k, max_k;
  814. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  815. max_k = gsihal_get_bit_map_array_size();
  816. for (k = 0; k < max_k; k++) {
  817. ch = gsihal_read_reg_nk(GSI_INTER_EE_n_SRC_GSI_CH_IRQ_k, ee, k);
  818. gsihal_write_reg_nk(GSI_INTER_EE_n_SRC_GSI_CH_IRQ_k, ee, k, ch);
  819. for (i = 0; i < GSI_STTS_REG_BITS; i++) {
  820. if ((1 << i) & ch) {
  821. ch_hdl = i + (GSI_STTS_REG_BITS * k);
  822. /* not currently expected */
  823. GSIERR("ch %u was inter-EE changed\n", ch_hdl);
  824. }
  825. }
  826. }
  827. } else {
  828. ch = gsihal_read_reg_n(GSI_INTER_EE_n_SRC_GSI_CH_IRQ, ee);
  829. gsihal_write_reg_n(GSI_INTER_EE_n_SRC_GSI_CH_IRQ, ee, ch);
  830. for (i = 0; i < GSI_STTS_REG_BITS; i++) {
  831. if ((1 << i) & ch) {
  832. /* not currently expected */
  833. GSIERR("ch %u was inter-EE changed\n", i);
  834. }
  835. }
  836. }
  837. }
  838. static void gsi_handle_inter_ee_ev_ctrl(int ee)
  839. {
  840. uint32_t ch, evt_hdl;
  841. int i, k, max_k;
  842. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  843. max_k = gsihal_get_bit_map_array_size();
  844. for (k = 0; k < max_k; k++) {
  845. ch = gsihal_read_reg_nk(GSI_INTER_EE_n_SRC_EV_CH_IRQ_k, ee, k);
  846. gsihal_write_reg_nk(GSI_INTER_EE_n_SRC_EV_CH_IRQ_CLR_k, ee, k, ch);
  847. for (i = 0; i < GSI_STTS_REG_BITS; i++) {
  848. if ((1 << i) & ch) {
  849. evt_hdl = i + (GSI_STTS_REG_BITS * k);
  850. /* not currently expected */
  851. GSIERR("evt %u was inter-EE changed\n",
  852. evt_hdl);
  853. }
  854. }
  855. }
  856. } else {
  857. ch = gsihal_read_reg_n(GSI_INTER_EE_n_SRC_EV_CH_IRQ, ee);
  858. gsihal_write_reg_n(GSI_INTER_EE_n_SRC_EV_CH_IRQ_CLR, ee, ch);
  859. for (i = 0; i < GSI_STTS_REG_BITS; i++) {
  860. if ((1 << i) & ch) {
  861. /* not currently expected */
  862. GSIERR("evt %u was inter-EE changed\n", i);
  863. }
  864. }
  865. }
  866. }
  867. static void gsi_handle_general(int ee)
  868. {
  869. uint32_t val;
  870. struct gsi_per_notify notify;
  871. struct gsihal_reg_cntxt_gsi_irq_stts gsi_irq_stts;
  872. val = gsihal_read_reg_n_fields(GSI_EE_n_CNTXT_GSI_IRQ_STTS,
  873. ee, &gsi_irq_stts);
  874. notify.user_data = gsi_ctx->per.user_data;
  875. if (gsi_irq_stts.gsi_mcs_stack_ovrflow)
  876. notify.evt_id = GSI_PER_EVT_GENERAL_MCS_STACK_OVERFLOW;
  877. if (gsi_irq_stts.gsi_cmd_fifo_ovrflow)
  878. notify.evt_id = GSI_PER_EVT_GENERAL_CMD_FIFO_OVERFLOW;
  879. if (gsi_irq_stts.gsi_bus_error)
  880. notify.evt_id = GSI_PER_EVT_GENERAL_BUS_ERROR;
  881. if (gsi_irq_stts.gsi_break_point)
  882. notify.evt_id = GSI_PER_EVT_GENERAL_BREAK_POINT;
  883. if (gsi_ctx->per.notify_cb)
  884. gsi_ctx->per.notify_cb(&notify);
  885. gsihal_write_reg_n(GSI_EE_n_CNTXT_GSI_IRQ_CLR, ee, val);
  886. }
  887. static void gsi_handle_irq(void)
  888. {
  889. uint32_t type;
  890. int ee = gsi_ctx->per.ee;
  891. int index;
  892. struct gsihal_reg_ctx_type_irq ctx_type_irq;
  893. while (1) {
  894. if (!gsi_ctx->per.clk_status_cb())
  895. break;
  896. type = gsihal_read_reg_n_fields(GSI_EE_n_CNTXT_TYPE_IRQ,
  897. ee, &ctx_type_irq);
  898. if (!type)
  899. break;
  900. GSIDBG_LOW("type 0x%x\n", type);
  901. index = gsi_ctx->gsi_isr_cache_index;
  902. gsi_ctx->gsi_isr_cache[index].timestamp =
  903. sched_clock();
  904. gsi_ctx->gsi_isr_cache[index].qtimer =
  905. __arch_counter_get_cntvct();
  906. gsi_ctx->gsi_isr_cache[index].interrupt_type = type;
  907. gsi_ctx->gsi_isr_cache_index++;
  908. if (gsi_ctx->gsi_isr_cache_index == GSI_ISR_CACHE_MAX)
  909. gsi_ctx->gsi_isr_cache_index = 0;
  910. if(ctx_type_irq.ch_ctrl) {
  911. gsi_handle_ch_ctrl(ee);
  912. break;
  913. }
  914. if (ctx_type_irq.ev_ctrl) {
  915. gsi_handle_ev_ctrl(ee);
  916. break;
  917. }
  918. if (ctx_type_irq.glob_ee)
  919. gsi_handle_glob_ee(ee);
  920. if (ctx_type_irq.ieob)
  921. gsi_handle_ieob(ee);
  922. if (ctx_type_irq.inter_ee_ch_ctrl)
  923. gsi_handle_inter_ee_ch_ctrl(ee);
  924. if (ctx_type_irq.inter_ee_ev_ctrl)
  925. gsi_handle_inter_ee_ev_ctrl(ee);
  926. if (ctx_type_irq.general)
  927. gsi_handle_general(ee);
  928. }
  929. }
  930. static irqreturn_t gsi_isr(int irq, void *ctxt)
  931. {
  932. if (gsi_ctx->per.req_clk_cb) {
  933. bool granted = false;
  934. gsi_ctx->per.req_clk_cb(gsi_ctx->per.user_data, &granted);
  935. if (granted) {
  936. gsi_handle_irq();
  937. gsi_ctx->per.rel_clk_cb(gsi_ctx->per.user_data);
  938. }
  939. } else if (!gsi_ctx->per.clk_status_cb()) {
  940. /* we only want to capture the gsi isr storm here */
  941. if (atomic_read(&gsi_ctx->num_unclock_irq) ==
  942. GSI_IRQ_STORM_THR)
  943. gsi_ctx->per.enable_clk_bug_on();
  944. atomic_inc(&gsi_ctx->num_unclock_irq);
  945. return IRQ_HANDLED;
  946. } else {
  947. atomic_set(&gsi_ctx->num_unclock_irq, 0);
  948. gsi_handle_irq();
  949. }
  950. return IRQ_HANDLED;
  951. }
  952. static irqreturn_t gsi_msi_isr(int irq, void *ctxt)
  953. {
  954. int ee = gsi_ctx->per.ee;
  955. uint64_t rp;
  956. struct gsi_chan_xfer_notify notify;
  957. unsigned long flags;
  958. unsigned long cntr;
  959. bool empty;
  960. uint8_t evt;
  961. unsigned long msi;
  962. struct gsi_evt_ctx *evt_ctxt;
  963. /* Determine which event channel to handle */
  964. for (msi = 0; msi < gsi_ctx->msi.num; msi++) {
  965. if (gsi_ctx->msi.irq[msi] == irq)
  966. break;
  967. }
  968. evt = gsi_ctx->msi.evt[msi];
  969. evt_ctxt = &gsi_ctx->evtr[evt];
  970. if (evt_ctxt->props.intf != GSI_EVT_CHTYPE_GPI_EV) {
  971. GSIERR("Unexpected irq intf %d\n",
  972. evt_ctxt->props.intf);
  973. GSI_ASSERT();
  974. }
  975. /* Clearing IEOB irq if there are any genereated for MSI channel */
  976. gsihal_write_reg_nk(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_k, ee,
  977. gsihal_get_ch_reg_idx(evt_ctxt->id),
  978. gsihal_get_ch_reg_mask(evt_ctxt->id));
  979. spin_lock_irqsave(&evt_ctxt->ring.slock, flags);
  980. check_again:
  981. cntr = 0;
  982. empty = true;
  983. rp = evt_ctxt->props.gsi_read_event_ring_rp(&evt_ctxt->props,
  984. evt_ctxt->id, ee);
  985. rp |= evt_ctxt->ring.rp & 0xFFFFFFFF00000000;
  986. evt_ctxt->ring.rp = rp;
  987. while (evt_ctxt->ring.rp_local != rp) {
  988. ++cntr;
  989. if (evt_ctxt->props.exclusive &&
  990. atomic_read(&evt_ctxt->chan[0]->poll_mode)) {
  991. cntr = 0;
  992. break;
  993. }
  994. gsi_process_evt_re(evt_ctxt, &notify, true);
  995. empty = false;
  996. }
  997. if (!empty)
  998. gsi_ring_evt_doorbell(evt_ctxt);
  999. if (cntr != 0)
  1000. goto check_again;
  1001. spin_unlock_irqrestore(&evt_ctxt->ring.slock, flags);
  1002. return IRQ_HANDLED;
  1003. }
  1004. static uint32_t gsi_get_max_channels(enum gsi_ver ver)
  1005. {
  1006. uint32_t max_ch = 0;
  1007. struct gsihal_reg_hw_param hw_param;
  1008. struct gsihal_reg_hw_param2 hw_param2;
  1009. switch (ver) {
  1010. case GSI_VER_ERR:
  1011. case GSI_VER_MAX:
  1012. GSIERR("GSI version is not supported %d\n", ver);
  1013. WARN_ON(1);
  1014. break;
  1015. case GSI_VER_1_0:
  1016. gsihal_read_reg_n_fields(GSI_EE_n_GSI_HW_PARAM,
  1017. gsi_ctx->per.ee, &hw_param);
  1018. max_ch = hw_param.gsi_ch_num;
  1019. break;
  1020. case GSI_VER_1_2:
  1021. gsihal_read_reg_n_fields(GSI_EE_n_GSI_HW_PARAM_0,
  1022. gsi_ctx->per.ee, &hw_param);
  1023. max_ch = hw_param.gsi_ch_num;
  1024. break;
  1025. default:
  1026. gsihal_read_reg_n_fields(GSI_EE_n_GSI_HW_PARAM_2,
  1027. gsi_ctx->per.ee, &hw_param2);
  1028. max_ch = hw_param2.gsi_num_ch_per_ee;
  1029. break;
  1030. }
  1031. GSIDBG("max channels %d\n", max_ch);
  1032. return max_ch;
  1033. }
  1034. static uint32_t gsi_get_max_event_rings(enum gsi_ver ver)
  1035. {
  1036. uint32_t max_ev = 0;
  1037. struct gsihal_reg_hw_param hw_param;
  1038. struct gsihal_reg_hw_param2 hw_param2;
  1039. struct gsihal_reg_hw_param4 hw_param4;
  1040. switch (ver) {
  1041. case GSI_VER_ERR:
  1042. case GSI_VER_MAX:
  1043. GSIERR("GSI version is not supported %d\n", ver);
  1044. WARN_ON(1);
  1045. break;
  1046. case GSI_VER_1_0:
  1047. gsihal_read_reg_n_fields(GSI_EE_n_GSI_HW_PARAM,
  1048. gsi_ctx->per.ee, &hw_param);
  1049. max_ev = hw_param.gsi_ev_ch_num;
  1050. break;
  1051. case GSI_VER_1_2:
  1052. gsihal_read_reg_n_fields(GSI_EE_n_GSI_HW_PARAM_0,
  1053. gsi_ctx->per.ee, &hw_param);
  1054. max_ev = hw_param.gsi_ev_ch_num;
  1055. break;
  1056. case GSI_VER_3_0:
  1057. case GSI_VER_5_5:
  1058. gsihal_read_reg_n_fields(GSI_EE_n_GSI_HW_PARAM_4,
  1059. gsi_ctx->per.ee, &hw_param4);
  1060. max_ev = hw_param4.gsi_num_ev_per_ee;
  1061. break;
  1062. default:
  1063. gsihal_read_reg_n_fields(GSI_EE_n_GSI_HW_PARAM_2,
  1064. gsi_ctx->per.ee, &hw_param2);
  1065. max_ev = hw_param2.gsi_num_ev_per_ee;
  1066. break;
  1067. }
  1068. GSIDBG("max event rings %d\n", max_ev);
  1069. return max_ev;
  1070. }
  1071. int gsi_complete_clk_grant(unsigned long dev_hdl)
  1072. {
  1073. unsigned long flags;
  1074. if (!gsi_ctx) {
  1075. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1076. return -GSI_STATUS_NODEV;
  1077. }
  1078. if (!gsi_ctx->per_registered) {
  1079. GSIERR("no client registered\n");
  1080. return -GSI_STATUS_INVALID_PARAMS;
  1081. }
  1082. if (dev_hdl != (uintptr_t)gsi_ctx) {
  1083. GSIERR("bad params dev_hdl=0x%lx gsi_ctx=0x%pK\n", dev_hdl,
  1084. gsi_ctx);
  1085. return -GSI_STATUS_INVALID_PARAMS;
  1086. }
  1087. spin_lock_irqsave(&gsi_ctx->slock, flags);
  1088. gsi_handle_irq();
  1089. gsi_ctx->per.rel_clk_cb(gsi_ctx->per.user_data);
  1090. spin_unlock_irqrestore(&gsi_ctx->slock, flags);
  1091. return GSI_STATUS_SUCCESS;
  1092. }
  1093. EXPORT_SYMBOL(gsi_complete_clk_grant);
  1094. int gsi_map_base(phys_addr_t gsi_base_addr, u32 gsi_size, enum gsi_ver ver)
  1095. {
  1096. if (!gsi_ctx) {
  1097. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1098. return -GSI_STATUS_NODEV;
  1099. }
  1100. gsi_ctx->base = devm_ioremap(
  1101. gsi_ctx->dev, gsi_base_addr, gsi_size);
  1102. if (!gsi_ctx->base) {
  1103. GSIERR("failed to map access to GSI HW\n");
  1104. return -GSI_STATUS_RES_ALLOC_FAILURE;
  1105. }
  1106. GSIDBG("GSI base(%pa) mapped to (%pK) with len (0x%x)\n",
  1107. &gsi_base_addr,
  1108. gsi_ctx->base,
  1109. gsi_size);
  1110. /* initialize HAL before accessing any register */
  1111. gsihal_init(ver, gsi_ctx->base);
  1112. return 0;
  1113. }
  1114. EXPORT_SYMBOL(gsi_map_base);
  1115. int gsi_unmap_base(void)
  1116. {
  1117. if (!gsi_ctx) {
  1118. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1119. return -GSI_STATUS_NODEV;
  1120. }
  1121. if (!gsi_ctx->base) {
  1122. GSIERR("access to GSI HW has not been mapped\n");
  1123. return -GSI_STATUS_INVALID_PARAMS;
  1124. }
  1125. devm_iounmap(gsi_ctx->dev, gsi_ctx->base);
  1126. gsi_ctx->base = NULL;
  1127. return 0;
  1128. }
  1129. EXPORT_SYMBOL(gsi_unmap_base);
  1130. static void __gsi_msi_write_msg(struct msi_desc *desc, struct msi_msg *msg)
  1131. {
  1132. u16 msi = 0;
  1133. if (IS_ERR_OR_NULL(desc) || IS_ERR_OR_NULL(msg) || IS_ERR_OR_NULL(gsi_ctx))
  1134. BUG();
  1135. msi = desc->platform.msi_index;
  1136. /* MSI should be valid and unallocated */
  1137. if ((msi >= gsi_ctx->msi.num) || (test_bit(msi, gsi_ctx->msi.allocated)))
  1138. BUG();
  1139. /* Save the message for later use */
  1140. memcpy(&gsi_ctx->msi.msg[msi], msg, sizeof(*msg));
  1141. dev_notice(gsi_ctx->dev,
  1142. "saved msi %u msg data %u addr 0x%08x%08x\n", msi,
  1143. msg->data, msg->address_hi, msg->address_lo);
  1144. /* Single MSI control is used. So MSI address will be same. */
  1145. if (!gsi_ctx->msi_addr_set) {
  1146. gsi_ctx->msi_addr = gsi_ctx->msi.msg[msi].address_hi;
  1147. gsi_ctx->msi_addr = (gsi_ctx->msi_addr << 32) |
  1148. gsi_ctx->msi.msg[msi].address_lo;
  1149. gsi_ctx->msi_addr_set = true;
  1150. }
  1151. GSIDBG("saved msi %u msg data %u addr 0x%08x%08x, MSI:0x%lx\n", msi,
  1152. msg->data, msg->address_hi, msg->address_lo, gsi_ctx->msi_addr);
  1153. }
  1154. static int __gsi_request_msi_irq(unsigned long msi)
  1155. {
  1156. int result = 0;
  1157. /* Ensure this is not already allocated */
  1158. if (test_bit((int)msi, gsi_ctx->msi.allocated)) {
  1159. GSIERR("MSI %lu already allocated\n", msi);
  1160. return -GSI_STATUS_ERROR;
  1161. }
  1162. /* Request MSI IRQ
  1163. * NOTE: During the call to devm_request_irq, the
  1164. * __gsi_msi_write_msg callback is triggered.
  1165. */
  1166. result = devm_request_irq(gsi_ctx->dev, gsi_ctx->msi.irq[msi],
  1167. (irq_handler_t)gsi_msi_isr, IRQF_TRIGGER_NONE,
  1168. "gsi_msi", gsi_ctx);
  1169. if (result) {
  1170. GSIERR("failed to register msi irq %u idx %lu\n",
  1171. gsi_ctx->msi.irq[msi], msi);
  1172. return -GSI_STATUS_ERROR;
  1173. }
  1174. set_bit(msi, gsi_ctx->msi.allocated);
  1175. return result;
  1176. }
  1177. static int __gsi_allocate_msis(void)
  1178. {
  1179. int result = 0;
  1180. struct msi_desc *desc = NULL;
  1181. size_t size = 0;
  1182. /* Allocate all MSIs */
  1183. GSIDBG("gsi_ctx->dev = %lu, gsi_ctx->msi.num = %d", gsi_ctx->dev, gsi_ctx->msi.num);
  1184. result = platform_msi_domain_alloc_irqs(gsi_ctx->dev, gsi_ctx->msi.num,
  1185. __gsi_msi_write_msg);
  1186. if (result) {
  1187. GSIERR("error allocating platform MSIs - %d\n", result);
  1188. return -GSI_STATUS_ERROR;
  1189. }
  1190. GSIDBG("MSI allocating is succesful\n");
  1191. /* Loop through the allocated MSIs and save the info, then
  1192. * request the IRQ.
  1193. */
  1194. for_each_msi_entry(desc, gsi_ctx->dev) {
  1195. unsigned long msi = desc->platform.msi_index;
  1196. /* Ensure a valid index */
  1197. if (msi >= gsi_ctx->msi.num) {
  1198. GSIERR("error invalid MSI %lu\n", msi);
  1199. result = -GSI_STATUS_ERROR;
  1200. goto err_free_msis;
  1201. }
  1202. /* Save IRQ */
  1203. gsi_ctx->msi.irq[msi] = desc->irq;
  1204. GSIDBG("desc->irq =%d\n", desc->irq);
  1205. /* Request the IRQ */
  1206. if (__gsi_request_msi_irq(msi)) {
  1207. GSIERR("error requesting IRQ for MSI %lu\n",
  1208. msi);
  1209. result = -GSI_STATUS_ERROR;
  1210. goto err_free_msis;
  1211. }
  1212. GSIDBG("Requesting IRQ succesful\n");
  1213. }
  1214. return result;
  1215. err_free_msis:
  1216. size = sizeof(unsigned long) * BITS_TO_LONGS(gsi_ctx->msi.num);
  1217. platform_msi_domain_free_irqs(gsi_ctx->dev);
  1218. memset(gsi_ctx->msi.allocated, 0, size);
  1219. return result;
  1220. }
  1221. int gsi_register_device(struct gsi_per_props *props, unsigned long *dev_hdl)
  1222. {
  1223. int res;
  1224. int result = GSI_STATUS_SUCCESS;
  1225. struct gsihal_reg_gsi_status gsi_status;
  1226. struct gsihal_reg_gsi_ee_n_cntxt_gsi_irq gen_irq;
  1227. if (!gsi_ctx) {
  1228. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1229. return -GSI_STATUS_NODEV;
  1230. }
  1231. if (!props || !dev_hdl) {
  1232. GSIERR("bad params props=%pK dev_hdl=%pK\n", props, dev_hdl);
  1233. return -GSI_STATUS_INVALID_PARAMS;
  1234. }
  1235. if (props->ver <= GSI_VER_ERR || props->ver >= GSI_VER_MAX) {
  1236. GSIERR("bad params gsi_ver=%d\n", props->ver);
  1237. return -GSI_STATUS_INVALID_PARAMS;
  1238. }
  1239. if (!props->notify_cb) {
  1240. GSIERR("notify callback must be provided\n");
  1241. return -GSI_STATUS_INVALID_PARAMS;
  1242. }
  1243. if (props->req_clk_cb && !props->rel_clk_cb) {
  1244. GSIERR("rel callback must be provided\n");
  1245. return -GSI_STATUS_INVALID_PARAMS;
  1246. }
  1247. if (gsi_ctx->per_registered) {
  1248. GSIERR("per already registered\n");
  1249. return -GSI_STATUS_UNSUPPORTED_OP;
  1250. }
  1251. spin_lock_init(&gsi_ctx->slock);
  1252. gsi_ctx->per = *props;
  1253. if (props->intr == GSI_INTR_IRQ) {
  1254. if (!props->irq) {
  1255. GSIERR("bad irq specified %u\n", props->irq);
  1256. return -GSI_STATUS_INVALID_PARAMS;
  1257. }
  1258. /*
  1259. * On a real UE, there are two separate interrupt
  1260. * vectors that get directed toward the GSI/IPA
  1261. * drivers. They are handled by gsi_isr() and
  1262. * (ipa_isr() or ipa3_isr()) respectively. In the
  1263. * emulation environment, this is not the case;
  1264. * instead, interrupt vectors are routed to the
  1265. * emualation hardware's interrupt controller, which
  1266. * in turn, forwards a single interrupt to the GSI/IPA
  1267. * driver. When the new interrupt vector is received,
  1268. * the driver needs to probe the interrupt
  1269. * controller's registers so see if one, the other, or
  1270. * both interrupts have occurred. Given the above, we
  1271. * now need to handle both situations, namely: the
  1272. * emulator's and the real UE.
  1273. */
  1274. if (running_emulation) {
  1275. /*
  1276. * New scheme involving the emulator's
  1277. * interrupt controller.
  1278. */
  1279. res = devm_request_threaded_irq(
  1280. gsi_ctx->dev,
  1281. props->irq,
  1282. /* top half handler to follow */
  1283. emulator_hard_irq_isr,
  1284. /* threaded bottom half handler to follow */
  1285. emulator_soft_irq_isr,
  1286. IRQF_SHARED,
  1287. "emulator_intcntrlr",
  1288. gsi_ctx);
  1289. } else {
  1290. /*
  1291. * Traditional scheme used on the real UE.
  1292. */
  1293. res = devm_request_irq(gsi_ctx->dev, props->irq,
  1294. gsi_isr,
  1295. props->req_clk_cb ? IRQF_TRIGGER_RISING :
  1296. IRQF_TRIGGER_HIGH,
  1297. "gsi",
  1298. gsi_ctx);
  1299. }
  1300. if (res) {
  1301. GSIERR(
  1302. "failed to register isr for %u\n",
  1303. props->irq);
  1304. return -GSI_STATUS_ERROR;
  1305. }
  1306. GSIDBG(
  1307. "succeeded to register isr for %u\n",
  1308. props->irq);
  1309. res = enable_irq_wake(props->irq);
  1310. if (res)
  1311. GSIERR("failed to enable wake irq %u\n", props->irq);
  1312. else
  1313. GSIERR("GSI irq is wake enabled %u\n", props->irq);
  1314. } else {
  1315. GSIERR("do not support interrupt type %u\n", props->intr);
  1316. return -GSI_STATUS_UNSUPPORTED_OP;
  1317. }
  1318. /* If MSIs are enabled, make sure they are set up */
  1319. if (gsi_ctx->msi.num) {
  1320. if (__gsi_allocate_msis()) {
  1321. GSIERR("failed to allocate MSIs\n");
  1322. goto err_free_irq;
  1323. }
  1324. }
  1325. /*
  1326. * If base not previously mapped via gsi_map_base(), map it
  1327. * now...
  1328. */
  1329. if (!gsi_ctx->base) {
  1330. res = gsi_map_base(props->phys_addr, props->size, props->ver);
  1331. if (res) {
  1332. result = res;
  1333. goto err_free_msis;
  1334. }
  1335. }
  1336. if (running_emulation) {
  1337. GSIDBG("GSI SW ver register value 0x%x\n",
  1338. gsihal_read_reg_n(GSI_EE_n_GSI_SW_VERSION, 0));
  1339. gsi_ctx->intcntrlr_mem_size =
  1340. props->emulator_intcntrlr_size;
  1341. gsi_ctx->intcntrlr_base =
  1342. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0))
  1343. devm_ioremap(
  1344. #else
  1345. devm_ioremap_nocache(
  1346. #endif
  1347. gsi_ctx->dev,
  1348. props->emulator_intcntrlr_addr,
  1349. props->emulator_intcntrlr_size);
  1350. if (!gsi_ctx->intcntrlr_base) {
  1351. GSIERR(
  1352. "failed to remap emulator's interrupt controller HW\n");
  1353. gsi_unmap_base();
  1354. devm_free_irq(gsi_ctx->dev, props->irq, gsi_ctx);
  1355. result = -GSI_STATUS_RES_ALLOC_FAILURE;
  1356. goto err_iounmap;
  1357. }
  1358. GSIDBG(
  1359. "Emulator's interrupt controller base(%pa) mapped to (%pK) with len (0x%lx)\n",
  1360. &(props->emulator_intcntrlr_addr),
  1361. gsi_ctx->intcntrlr_base,
  1362. props->emulator_intcntrlr_size);
  1363. gsi_ctx->intcntrlr_gsi_isr = gsi_isr;
  1364. gsi_ctx->intcntrlr_client_isr =
  1365. props->emulator_intcntrlr_client_isr;
  1366. }
  1367. gsi_ctx->per_registered = true;
  1368. mutex_init(&gsi_ctx->mlock);
  1369. atomic_set(&gsi_ctx->num_chan, 0);
  1370. atomic_set(&gsi_ctx->num_evt_ring, 0);
  1371. gsi_ctx->max_ch = gsi_get_max_channels(gsi_ctx->per.ver);
  1372. if (gsi_ctx->max_ch == 0) {
  1373. gsi_unmap_base();
  1374. if (running_emulation)
  1375. devm_iounmap(gsi_ctx->dev, gsi_ctx->intcntrlr_base);
  1376. gsi_ctx->base = gsi_ctx->intcntrlr_base = NULL;
  1377. devm_free_irq(gsi_ctx->dev, props->irq, gsi_ctx);
  1378. GSIERR("failed to get max channels\n");
  1379. result = -GSI_STATUS_ERROR;
  1380. goto err_iounmap;
  1381. }
  1382. gsi_ctx->max_ev = gsi_get_max_event_rings(gsi_ctx->per.ver);
  1383. if (gsi_ctx->max_ev == 0) {
  1384. gsi_unmap_base();
  1385. if (running_emulation)
  1386. devm_iounmap(gsi_ctx->dev, gsi_ctx->intcntrlr_base);
  1387. gsi_ctx->base = gsi_ctx->intcntrlr_base = NULL;
  1388. devm_free_irq(gsi_ctx->dev, props->irq, gsi_ctx);
  1389. GSIERR("failed to get max event rings\n");
  1390. result = -GSI_STATUS_ERROR;
  1391. goto err_iounmap;
  1392. }
  1393. if (gsi_ctx->max_ev > GSI_EVT_RING_MAX) {
  1394. GSIERR("max event rings are beyond absolute maximum\n");
  1395. result = -GSI_STATUS_ERROR;
  1396. goto err_iounmap;
  1397. }
  1398. if (props->mhi_er_id_limits_valid &&
  1399. props->mhi_er_id_limits[0] > (gsi_ctx->max_ev - 1)) {
  1400. gsi_unmap_base();
  1401. if (running_emulation)
  1402. devm_iounmap(gsi_ctx->dev, gsi_ctx->intcntrlr_base);
  1403. gsi_ctx->base = gsi_ctx->intcntrlr_base = NULL;
  1404. devm_free_irq(gsi_ctx->dev, props->irq, gsi_ctx);
  1405. GSIERR("MHI event ring start id %u is beyond max %u\n",
  1406. props->mhi_er_id_limits[0], gsi_ctx->max_ev);
  1407. result = -GSI_STATUS_ERROR;
  1408. goto err_iounmap;
  1409. }
  1410. gsi_ctx->evt_bmap = ~((((unsigned long)1) << gsi_ctx->max_ev) - 1);
  1411. /* exclude reserved mhi events */
  1412. if (props->mhi_er_id_limits_valid)
  1413. gsi_ctx->evt_bmap |=
  1414. ((1 << (props->mhi_er_id_limits[1] + 1)) - 1) ^
  1415. ((1 << (props->mhi_er_id_limits[0])) - 1);
  1416. /*
  1417. * enable all interrupts but GSI_BREAK_POINT.
  1418. * Inter EE commands / interrupt are no supported.
  1419. */
  1420. __gsi_config_type_irq(props->ee, ~0, ~0);
  1421. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  1422. __gsi_config_all_ch_irq(props->ee, ~0, ~0);
  1423. __gsi_config_all_evt_irq(props->ee, ~0, ~0);
  1424. __gsi_config_all_ieob_irq(props->ee, ~0, ~0);
  1425. }
  1426. else {
  1427. __gsi_config_ch_irq(props->ee, ~0, ~0);
  1428. __gsi_config_evt_irq(props->ee, ~0, ~0);
  1429. __gsi_config_ieob_irq(props->ee, ~0, ~0);
  1430. }
  1431. __gsi_config_glob_irq(props->ee, ~0, ~0);
  1432. /*
  1433. * Disabling global INT1 interrupt by default and enable it
  1434. * onlt when sending the generic command.
  1435. */
  1436. __gsi_config_glob_irq(props->ee,
  1437. gsihal_get_glob_irq_en_gp_int1_mask(), 0);
  1438. gen_irq.gsi_mcs_stack_ovrflow = 1;
  1439. gen_irq.gsi_cmd_fifo_ovrflow = 1;
  1440. gen_irq.gsi_bus_error = 1;
  1441. gen_irq.gsi_break_point = 0;
  1442. gsihal_write_reg_n_fields(GSI_EE_n_CNTXT_GSI_IRQ_EN,
  1443. gsi_ctx->per.ee, &gen_irq);
  1444. gsihal_write_reg_n(GSI_EE_n_CNTXT_INTSET, gsi_ctx->per.ee, props->intr);
  1445. /* set GSI_TOP_EE_n_CNTXT_MSI_BASE_LSB/MSB to 0 */
  1446. if ((gsi_ctx->per.ver >= GSI_VER_2_0) &&
  1447. (props->intr != GSI_INTR_MSI)) {
  1448. gsihal_write_reg_n(
  1449. GSI_EE_n_CNTXT_MSI_BASE_LSB, gsi_ctx->per.ee, 0);
  1450. gsihal_write_reg_n(
  1451. GSI_EE_n_CNTXT_MSI_BASE_MSB, gsi_ctx->per.ee, 0);
  1452. }
  1453. gsihal_read_reg_n_fields(GSI_EE_n_GSI_STATUS,
  1454. gsi_ctx->per.ee, &gsi_status);
  1455. if (gsi_status.enabled)
  1456. gsi_ctx->enabled = true;
  1457. else
  1458. GSIERR("Manager EE has not enabled GSI, GSI un-usable\n");
  1459. if (gsi_ctx->per.ver >= GSI_VER_1_2)
  1460. gsihal_write_reg_n(GSI_EE_n_ERROR_LOG, gsi_ctx->per.ee, 0);
  1461. if (running_emulation) {
  1462. /*
  1463. * Set up the emulator's interrupt controller...
  1464. */
  1465. res = setup_emulator_cntrlr(
  1466. gsi_ctx->intcntrlr_base, gsi_ctx->intcntrlr_mem_size);
  1467. if (res != 0) {
  1468. GSIERR("setup_emulator_cntrlr() failed\n");
  1469. result = res;
  1470. goto err_iounmap;
  1471. }
  1472. }
  1473. *dev_hdl = (uintptr_t)gsi_ctx;
  1474. gsi_ctx->gsi_isr_cache_index = 0;
  1475. return result;
  1476. err_iounmap:
  1477. gsi_unmap_base();
  1478. if (running_emulation && gsi_ctx->intcntrlr_base != NULL)
  1479. devm_iounmap(gsi_ctx->dev, gsi_ctx->intcntrlr_base);
  1480. gsi_ctx->base = gsi_ctx->intcntrlr_base = NULL;
  1481. err_free_msis:
  1482. if (gsi_ctx->msi.num) {
  1483. size_t size =
  1484. sizeof(unsigned long) * BITS_TO_LONGS(gsi_ctx->msi.num);
  1485. platform_msi_domain_free_irqs(gsi_ctx->dev);
  1486. memset(gsi_ctx->msi.allocated, 0, size);
  1487. }
  1488. err_free_irq:
  1489. devm_free_irq(gsi_ctx->dev, props->irq, gsi_ctx);
  1490. return result;
  1491. }
  1492. EXPORT_SYMBOL(gsi_register_device);
  1493. int gsi_write_device_scratch(unsigned long dev_hdl,
  1494. struct gsi_device_scratch *val)
  1495. {
  1496. unsigned int max_usb_pkt_size = 0;
  1497. if (!gsi_ctx) {
  1498. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1499. return -GSI_STATUS_NODEV;
  1500. }
  1501. if (!gsi_ctx->per_registered) {
  1502. GSIERR("no client registered\n");
  1503. return -GSI_STATUS_INVALID_PARAMS;
  1504. }
  1505. if (dev_hdl != (uintptr_t)gsi_ctx) {
  1506. GSIERR("bad params dev_hdl=0x%lx gsi_ctx=0x%pK\n", dev_hdl,
  1507. gsi_ctx);
  1508. return -GSI_STATUS_INVALID_PARAMS;
  1509. }
  1510. if (val->max_usb_pkt_size_valid &&
  1511. val->max_usb_pkt_size != 1024 &&
  1512. val->max_usb_pkt_size != 512 &&
  1513. val->max_usb_pkt_size != 64) {
  1514. GSIERR("bad USB max pkt size dev_hdl=0x%lx sz=%u\n", dev_hdl,
  1515. val->max_usb_pkt_size);
  1516. return -GSI_STATUS_INVALID_PARAMS;
  1517. }
  1518. mutex_lock(&gsi_ctx->mlock);
  1519. if (val->mhi_base_chan_idx_valid)
  1520. gsi_ctx->scratch.word0.s.mhi_base_chan_idx =
  1521. val->mhi_base_chan_idx;
  1522. if (val->max_usb_pkt_size_valid) {
  1523. max_usb_pkt_size = 2;
  1524. if (val->max_usb_pkt_size > 64)
  1525. max_usb_pkt_size =
  1526. (val->max_usb_pkt_size == 1024) ? 1 : 0;
  1527. gsi_ctx->scratch.word0.s.max_usb_pkt_size = max_usb_pkt_size;
  1528. }
  1529. gsihal_write_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  1530. gsi_ctx->per.ee, gsi_ctx->scratch.word0.val);
  1531. mutex_unlock(&gsi_ctx->mlock);
  1532. return GSI_STATUS_SUCCESS;
  1533. }
  1534. EXPORT_SYMBOL(gsi_write_device_scratch);
  1535. int gsi_deregister_device(unsigned long dev_hdl, bool force)
  1536. {
  1537. if (!gsi_ctx) {
  1538. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1539. return -GSI_STATUS_NODEV;
  1540. }
  1541. if (!gsi_ctx->per_registered) {
  1542. GSIERR("no client registered\n");
  1543. return -GSI_STATUS_INVALID_PARAMS;
  1544. }
  1545. if (dev_hdl != (uintptr_t)gsi_ctx) {
  1546. GSIERR("bad params dev_hdl=0x%lx gsi_ctx=0x%pK\n", dev_hdl,
  1547. gsi_ctx);
  1548. return -GSI_STATUS_INVALID_PARAMS;
  1549. }
  1550. if (!force && atomic_read(&gsi_ctx->num_chan)) {
  1551. GSIERR("cannot deregister %u channels are still connected\n",
  1552. atomic_read(&gsi_ctx->num_chan));
  1553. return -GSI_STATUS_UNSUPPORTED_OP;
  1554. }
  1555. if (!force && atomic_read(&gsi_ctx->num_evt_ring)) {
  1556. GSIERR("cannot deregister %u events are still connected\n",
  1557. atomic_read(&gsi_ctx->num_evt_ring));
  1558. return -GSI_STATUS_UNSUPPORTED_OP;
  1559. }
  1560. /* disable all interrupts */
  1561. __gsi_config_type_irq(gsi_ctx->per.ee, ~0, 0);
  1562. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  1563. __gsi_config_all_ch_irq(gsi_ctx->per.ee, ~0, 0);
  1564. __gsi_config_all_evt_irq(gsi_ctx->per.ee, ~0, 0);
  1565. __gsi_config_all_ieob_irq(gsi_ctx->per.ee, ~0, 0);
  1566. }
  1567. else {
  1568. __gsi_config_ch_irq(gsi_ctx->per.ee, ~0, 0);
  1569. __gsi_config_evt_irq(gsi_ctx->per.ee, ~0, 0);
  1570. __gsi_config_ieob_irq(gsi_ctx->per.ee, ~0, 0);
  1571. }
  1572. __gsi_config_glob_irq(gsi_ctx->per.ee, ~0, 0);
  1573. __gsi_config_gen_irq(gsi_ctx->per.ee, ~0, 0);
  1574. if (gsi_ctx->msi.num)
  1575. platform_msi_domain_free_irqs(gsi_ctx->dev);
  1576. devm_free_irq(gsi_ctx->dev, gsi_ctx->per.irq, gsi_ctx);
  1577. gsihal_destroy();
  1578. gsi_unmap_base();
  1579. memset(gsi_ctx, 0, sizeof(*gsi_ctx));
  1580. return GSI_STATUS_SUCCESS;
  1581. }
  1582. EXPORT_SYMBOL(gsi_deregister_device);
  1583. static void gsi_program_evt_ring_ctx(struct gsi_evt_ring_props *props,
  1584. uint8_t evt_id, unsigned int ee)
  1585. {
  1586. struct gsihal_reg_ev_ch_k_cntxt_0 ev_ch_k_cntxt_0;
  1587. struct gsihal_reg_ev_ch_k_cntxt_1 ev_ch_k_cntxt_1;
  1588. struct gsihal_reg_ev_ch_k_cntxt_2 ev_ch_k_cntxt_2;
  1589. struct gsihal_reg_ev_ch_k_cntxt_3 ev_ch_k_cntxt_3;
  1590. struct gsihal_reg_ev_ch_k_cntxt_8 ev_ch_k_cntxt_8;
  1591. struct gsihal_reg_ev_ch_k_cntxt_9 ev_ch_k_cntxt_9;
  1592. struct gsihal_reg_ev_ch_k_cntxt_10 ev_ch_k_cntxt_10;
  1593. struct gsihal_reg_ev_ch_k_cntxt_11 ev_ch_k_cntxt_11;
  1594. struct gsihal_reg_ev_ch_k_cntxt_12 ev_ch_k_cntxt_12;
  1595. struct gsihal_reg_ev_ch_k_cntxt_13 ev_ch_k_cntxt_13;
  1596. GSIDBG("intf=%u intr=%u re=%u\n", props->intf, props->intr,
  1597. props->re_size);
  1598. ev_ch_k_cntxt_0.chtype = props->intf;
  1599. ev_ch_k_cntxt_0.intype = props->intr;
  1600. ev_ch_k_cntxt_0.element_size = props->re_size;
  1601. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_0,
  1602. ee, evt_id, &ev_ch_k_cntxt_0);
  1603. ev_ch_k_cntxt_1.r_length = props->ring_len;
  1604. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_1,
  1605. ee, evt_id,
  1606. &ev_ch_k_cntxt_1);
  1607. ev_ch_k_cntxt_2.r_base_addr_lsbs = GSI_LSB(props->ring_base_addr);
  1608. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_2,
  1609. ee, evt_id,
  1610. &ev_ch_k_cntxt_2);
  1611. ev_ch_k_cntxt_3.r_base_addr_msbs = GSI_MSB(props->ring_base_addr);
  1612. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_3,
  1613. ee, evt_id,
  1614. &ev_ch_k_cntxt_3);
  1615. ev_ch_k_cntxt_8.int_modt = props->int_modt;
  1616. ev_ch_k_cntxt_8.int_modc = props->int_modc;
  1617. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_8,
  1618. ee, evt_id,
  1619. &ev_ch_k_cntxt_8);
  1620. ev_ch_k_cntxt_9.intvec = props->intvec;
  1621. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_9,
  1622. ee, evt_id,
  1623. &ev_ch_k_cntxt_9);
  1624. ev_ch_k_cntxt_10.msi_addr_lsb = GSI_LSB(props->msi_addr);
  1625. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_10,
  1626. ee, evt_id,
  1627. &ev_ch_k_cntxt_10);
  1628. ev_ch_k_cntxt_11.msi_addr_msb = GSI_MSB(props->msi_addr);
  1629. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_11,
  1630. ee, evt_id,
  1631. &ev_ch_k_cntxt_11);
  1632. ev_ch_k_cntxt_12.rp_update_addr_lsb = GSI_LSB(props->rp_update_addr);
  1633. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_12,
  1634. ee, evt_id,
  1635. &ev_ch_k_cntxt_12);
  1636. ev_ch_k_cntxt_13.rp_update_addr_msb = GSI_MSB(props->rp_update_addr);
  1637. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_13,
  1638. ee, evt_id,
  1639. &ev_ch_k_cntxt_13);
  1640. }
  1641. static void gsi_init_evt_ring(struct gsi_evt_ring_props *props,
  1642. struct gsi_ring_ctx *ctx)
  1643. {
  1644. ctx->base_va = (uintptr_t)props->ring_base_vaddr;
  1645. ctx->base = props->ring_base_addr;
  1646. ctx->wp = ctx->base;
  1647. ctx->rp = ctx->base;
  1648. ctx->wp_local = ctx->base;
  1649. ctx->rp_local = ctx->base;
  1650. ctx->len = props->ring_len;
  1651. ctx->elem_sz = props->re_size;
  1652. ctx->max_num_elem = ctx->len / ctx->elem_sz - 1;
  1653. ctx->end = ctx->base + (ctx->max_num_elem + 1) * ctx->elem_sz;
  1654. if (props->rp_update_vaddr)
  1655. *(uint64_t *)(props->rp_update_vaddr) = ctx->rp_local;
  1656. }
  1657. static void gsi_prime_evt_ring(struct gsi_evt_ctx *ctx)
  1658. {
  1659. unsigned long flags;
  1660. struct gsihal_reg_gsi_ee_n_ev_ch_k_doorbell_1 db;
  1661. spin_lock_irqsave(&ctx->ring.slock, flags);
  1662. memset((void *)ctx->ring.base_va, 0, ctx->ring.len);
  1663. ctx->ring.wp_local = ctx->ring.base +
  1664. ctx->ring.max_num_elem * ctx->ring.elem_sz;
  1665. /* write order MUST be MSB followed by LSB */
  1666. db.write_ptr_msb = GSI_MSB(ctx->ring.wp_local);
  1667. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_DOORBELL_1,
  1668. gsi_ctx->per.ee, ctx->id, &db);
  1669. gsi_ring_evt_doorbell(ctx);
  1670. spin_unlock_irqrestore(&ctx->ring.slock, flags);
  1671. }
  1672. static void gsi_prime_evt_ring_wdi(struct gsi_evt_ctx *ctx)
  1673. {
  1674. unsigned long flags;
  1675. spin_lock_irqsave(&ctx->ring.slock, flags);
  1676. if (ctx->ring.base_va)
  1677. memset((void *)ctx->ring.base_va, 0, ctx->ring.len);
  1678. ctx->ring.wp_local = ctx->ring.base +
  1679. ((ctx->ring.max_num_elem + 2) * ctx->ring.elem_sz);
  1680. gsi_ring_evt_doorbell(ctx);
  1681. spin_unlock_irqrestore(&ctx->ring.slock, flags);
  1682. }
  1683. static int gsi_validate_evt_ring_props(struct gsi_evt_ring_props *props)
  1684. {
  1685. uint64_t ra;
  1686. if ((props->re_size == GSI_EVT_RING_RE_SIZE_4B &&
  1687. props->ring_len % 4) ||
  1688. (props->re_size == GSI_EVT_RING_RE_SIZE_8B &&
  1689. props->ring_len % 8) ||
  1690. (props->re_size == GSI_EVT_RING_RE_SIZE_16B &&
  1691. props->ring_len % 16) ||
  1692. (props->re_size == GSI_EVT_RING_RE_SIZE_32B &&
  1693. props->ring_len % 32)) {
  1694. GSIERR("bad params ring_len %u not a multiple of RE size %u\n",
  1695. props->ring_len, props->re_size);
  1696. return -GSI_STATUS_INVALID_PARAMS;
  1697. }
  1698. if (!gsihal_check_ring_length_valid(props->ring_len, props->re_size))
  1699. return -GSI_STATUS_INVALID_PARAMS;
  1700. ra = props->ring_base_addr;
  1701. do_div(ra, roundup_pow_of_two(props->ring_len));
  1702. if (props->ring_base_addr != ra * roundup_pow_of_two(props->ring_len)) {
  1703. GSIERR("bad params ring base not aligned 0x%llx align 0x%lx\n",
  1704. props->ring_base_addr,
  1705. roundup_pow_of_two(props->ring_len));
  1706. return -GSI_STATUS_INVALID_PARAMS;
  1707. }
  1708. if (props->intf == GSI_EVT_CHTYPE_GPI_EV &&
  1709. !props->ring_base_vaddr) {
  1710. GSIERR("protocol %u requires ring base VA\n", props->intf);
  1711. return -GSI_STATUS_INVALID_PARAMS;
  1712. }
  1713. if (props->intf == GSI_EVT_CHTYPE_MHI_EV &&
  1714. (!props->evchid_valid ||
  1715. props->evchid > gsi_ctx->per.mhi_er_id_limits[1] ||
  1716. props->evchid < gsi_ctx->per.mhi_er_id_limits[0])) {
  1717. GSIERR("MHI requires evchid valid=%d val=%u\n",
  1718. props->evchid_valid, props->evchid);
  1719. return -GSI_STATUS_INVALID_PARAMS;
  1720. }
  1721. if (props->intf != GSI_EVT_CHTYPE_MHI_EV &&
  1722. props->evchid_valid) {
  1723. GSIERR("protocol %u cannot specify evchid\n", props->intf);
  1724. return -GSI_STATUS_INVALID_PARAMS;
  1725. }
  1726. if (!props->err_cb) {
  1727. GSIERR("err callback must be provided\n");
  1728. return -GSI_STATUS_INVALID_PARAMS;
  1729. }
  1730. return GSI_STATUS_SUCCESS;
  1731. }
  1732. /**
  1733. * gsi_cleanup_xfer_user_data: cleanup the user data array using callback passed
  1734. * by IPA driver. Need to do this in GSI since only GSI knows which TRE
  1735. * are being used or not. However, IPA is the one that does cleaning,
  1736. * therefore we pass a callback from IPA and call it using params from GSI
  1737. *
  1738. * @chan_hdl: hdl of the gsi channel user data array to be cleaned
  1739. * @cleanup_cb: callback used to clean the user data array. takes 2 inputs
  1740. * @chan_user_data: ipa_sys_context of the gsi_channel
  1741. * @xfer_uder_data: user data array element (rx_pkt wrapper)
  1742. *
  1743. * Returns: 0 on success, negative on failure
  1744. */
  1745. static int gsi_cleanup_xfer_user_data(unsigned long chan_hdl,
  1746. void (*cleanup_cb)(void *chan_user_data, void *xfer_user_data))
  1747. {
  1748. struct gsi_chan_ctx *ctx;
  1749. uint64_t i;
  1750. uint16_t rp_idx;
  1751. ctx = &gsi_ctx->chan[chan_hdl];
  1752. if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  1753. GSIERR("bad state %d\n", ctx->state);
  1754. return -GSI_STATUS_UNSUPPORTED_OP;
  1755. }
  1756. /* for coalescing, traverse the whole array */
  1757. if (ctx->props.prot == GSI_CHAN_PROT_GCI) {
  1758. size_t user_data_size =
  1759. ctx->ring.max_num_elem + 1 + GSI_VEID_MAX;
  1760. for (i = 0; i < user_data_size; i++) {
  1761. if (ctx->user_data[i].valid)
  1762. cleanup_cb(ctx->props.chan_user_data,
  1763. ctx->user_data[i].p);
  1764. }
  1765. } else {
  1766. /* for non-coalescing, clean between RP and WP */
  1767. while (ctx->ring.rp_local != ctx->ring.wp_local) {
  1768. rp_idx = gsi_find_idx_from_addr(&ctx->ring,
  1769. ctx->ring.rp_local);
  1770. WARN_ON(!ctx->user_data[rp_idx].valid);
  1771. cleanup_cb(ctx->props.chan_user_data,
  1772. ctx->user_data[rp_idx].p);
  1773. gsi_incr_ring_rp(&ctx->ring);
  1774. }
  1775. }
  1776. return 0;
  1777. }
  1778. /**
  1779. * gsi_read_event_ring_rp_ddr - function returns the RP value of the event
  1780. * ring read from the ring context register.
  1781. *
  1782. * @props: Props structere of the event channel
  1783. * @id: Event channel index
  1784. * @ee: EE
  1785. *
  1786. * @Return pointer to the read pointer
  1787. */
  1788. static inline uint64_t gsi_read_event_ring_rp_ddr(struct gsi_evt_ring_props* props,
  1789. uint8_t id, int ee)
  1790. {
  1791. return readl_relaxed(props->rp_update_vaddr);
  1792. }
  1793. /**
  1794. * gsi_read_event_ring_rp_reg - function returns the RP value of the event ring
  1795. * read from the DDR.
  1796. *
  1797. * @props: Props structere of the event channel
  1798. * @id: Event channel index
  1799. * @ee: EE
  1800. *
  1801. * @Return pointer to the read pointer
  1802. */
  1803. static inline uint64_t gsi_read_event_ring_rp_reg(struct gsi_evt_ring_props* props,
  1804. uint8_t id, int ee)
  1805. {
  1806. return gsihal_read_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_4, ee, id);
  1807. }
  1808. static int __gsi_pair_msi(struct gsi_evt_ctx *ctx,
  1809. struct gsi_evt_ring_props *props)
  1810. {
  1811. int result = GSI_STATUS_SUCCESS;
  1812. unsigned long msi = 0;
  1813. if (IS_ERR_OR_NULL(ctx) || IS_ERR_OR_NULL(props) || IS_ERR_OR_NULL(gsi_ctx))
  1814. BUG();
  1815. /* Find the first unused MSI */
  1816. msi = find_first_zero_bit(gsi_ctx->msi.used, gsi_ctx->msi.num);
  1817. if (msi >= gsi_ctx->msi.num) {
  1818. GSIERR("No free MSIs for evt %u\n", ctx->id);
  1819. return -GSI_STATUS_ERROR;
  1820. }
  1821. /* Ensure it's been allocated */
  1822. if (!test_bit((int)msi, gsi_ctx->msi.allocated)) {
  1823. GSIDBG("MSI %lu not allocated\n", msi);
  1824. return -GSI_STATUS_ERROR;
  1825. }
  1826. /* Save the event ID for later lookup */
  1827. gsi_ctx->msi.evt[msi] = ctx->id;
  1828. /* Add this event to the IRQ mask */
  1829. set_bit((int)ctx->id, &gsi_ctx->msi.mask);
  1830. props->intvec = gsi_ctx->msi.msg[msi].data;
  1831. props->msi_addr = (uint64_t)gsi_ctx->msi.msg[msi].address_hi << 32 |
  1832. (uint64_t)gsi_ctx->msi.msg[msi].address_lo;
  1833. GSIDBG("props->intvec = %d, props->msi_addr = %lu\n", props->intvec, props->msi_addr);
  1834. if (props->msi_addr == 0)
  1835. BUG();
  1836. /* Mark MSI as used */
  1837. set_bit(msi, gsi_ctx->msi.used);
  1838. return result;
  1839. }
  1840. int gsi_alloc_evt_ring(struct gsi_evt_ring_props *props, unsigned long dev_hdl,
  1841. unsigned long *evt_ring_hdl)
  1842. {
  1843. unsigned long evt_id;
  1844. enum gsi_evt_ch_cmd_opcode op = GSI_EVT_ALLOCATE;
  1845. struct gsihal_reg_ee_n_ev_ch_cmd ev_ch_cmd;
  1846. struct gsi_evt_ctx *ctx;
  1847. int res = 0;
  1848. int ee;
  1849. unsigned long flags;
  1850. if (!gsi_ctx) {
  1851. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1852. return -GSI_STATUS_NODEV;
  1853. }
  1854. if (!props || !evt_ring_hdl || dev_hdl != (uintptr_t)gsi_ctx) {
  1855. GSIERR("bad params props=%pK dev_hdl=0x%lx evt_ring_hdl=%pK\n",
  1856. props, dev_hdl, evt_ring_hdl);
  1857. return -GSI_STATUS_INVALID_PARAMS;
  1858. }
  1859. if (gsi_validate_evt_ring_props(props)) {
  1860. GSIERR("invalid params\n");
  1861. return -GSI_STATUS_INVALID_PARAMS;
  1862. }
  1863. if (!props->evchid_valid) {
  1864. mutex_lock(&gsi_ctx->mlock);
  1865. evt_id = find_first_zero_bit(&gsi_ctx->evt_bmap,
  1866. sizeof(unsigned long) * BITS_PER_BYTE);
  1867. if (evt_id == sizeof(unsigned long) * BITS_PER_BYTE) {
  1868. GSIERR("failed to alloc event ID\n");
  1869. mutex_unlock(&gsi_ctx->mlock);
  1870. return -GSI_STATUS_RES_ALLOC_FAILURE;
  1871. }
  1872. set_bit(evt_id, &gsi_ctx->evt_bmap);
  1873. mutex_unlock(&gsi_ctx->mlock);
  1874. } else {
  1875. evt_id = props->evchid;
  1876. }
  1877. GSIDBG("Using %lu as virt evt id\n", evt_id);
  1878. if (props->rp_update_addr != 0) {
  1879. GSIDBG("Using DDR to read event RP for virt evt id: %lu\n",
  1880. evt_id);
  1881. props->gsi_read_event_ring_rp =
  1882. gsi_read_event_ring_rp_ddr;
  1883. }
  1884. else {
  1885. GSIDBG("Using CONTEXT reg to read event RP for virt evt id: %lu\n",
  1886. evt_id);
  1887. props->gsi_read_event_ring_rp =
  1888. gsi_read_event_ring_rp_reg;
  1889. }
  1890. ctx = &gsi_ctx->evtr[evt_id];
  1891. memset(ctx, 0, sizeof(*ctx));
  1892. mutex_init(&ctx->mlock);
  1893. init_completion(&ctx->compl);
  1894. atomic_set(&ctx->chan_ref_cnt, 0);
  1895. ctx->num_of_chan_allocated = 0;
  1896. ctx->id = evt_id;
  1897. mutex_lock(&gsi_ctx->mlock);
  1898. /* Pair an MSI with this event if this is an MSI and GPI event channel
  1899. * NOTE: This modifies props, so must be before props are saved to ctx.
  1900. */
  1901. if (props->intf == GSI_EVT_CHTYPE_GPI_EV &&
  1902. props->intr == GSI_INTR_MSI) {
  1903. if (__gsi_pair_msi(ctx, props)) {
  1904. GSIERR("evt_id=%lu failed to pair MSI\n", evt_id);
  1905. if (!props->evchid_valid)
  1906. clear_bit(evt_id, &gsi_ctx->evt_bmap);
  1907. mutex_unlock(&gsi_ctx->mlock);
  1908. return -GSI_STATUS_NODEV;
  1909. }
  1910. GSIDBG("evt_id=%lu pair MSI succesful\n", evt_id);
  1911. }
  1912. ctx->props = *props;
  1913. ee = gsi_ctx->per.ee;
  1914. ev_ch_cmd.opcode = op;
  1915. ev_ch_cmd.chid = evt_id;
  1916. gsihal_write_reg_n_fields(GSI_EE_n_EV_CH_CMD, ee, &ev_ch_cmd);
  1917. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  1918. if (res == 0) {
  1919. GSIERR("evt_id=%lu timed out\n", evt_id);
  1920. if (!props->evchid_valid)
  1921. clear_bit(evt_id, &gsi_ctx->evt_bmap);
  1922. mutex_unlock(&gsi_ctx->mlock);
  1923. return -GSI_STATUS_TIMED_OUT;
  1924. }
  1925. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  1926. GSIERR("evt_id=%lu allocation failed state=%u\n",
  1927. evt_id, ctx->state);
  1928. if (!props->evchid_valid)
  1929. clear_bit(evt_id, &gsi_ctx->evt_bmap);
  1930. mutex_unlock(&gsi_ctx->mlock);
  1931. return -GSI_STATUS_RES_ALLOC_FAILURE;
  1932. }
  1933. gsi_program_evt_ring_ctx(props, evt_id, gsi_ctx->per.ee);
  1934. spin_lock_init(&ctx->ring.slock);
  1935. gsi_init_evt_ring(props, &ctx->ring);
  1936. ctx->id = evt_id;
  1937. *evt_ring_hdl = evt_id;
  1938. atomic_inc(&gsi_ctx->num_evt_ring);
  1939. if (props->intf == GSI_EVT_CHTYPE_GPI_EV)
  1940. gsi_prime_evt_ring(ctx);
  1941. else if (props->intf == GSI_EVT_CHTYPE_WDI2_EV)
  1942. gsi_prime_evt_ring_wdi(ctx);
  1943. mutex_unlock(&gsi_ctx->mlock);
  1944. spin_lock_irqsave(&gsi_ctx->slock, flags);
  1945. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  1946. gsihal_write_reg_nk(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_k, ee,
  1947. gsihal_get_ch_reg_idx(evt_id), gsihal_get_ch_reg_mask(evt_id));
  1948. }
  1949. else {
  1950. gsihal_write_reg_n(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR, ee, 1 << evt_id);
  1951. }
  1952. /* enable ieob interrupts for GPI, enable MSI interrupts */
  1953. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  1954. if ((props->intf != GSI_EVT_CHTYPE_GPI_EV) &&
  1955. (props->intr != GSI_INTR_MSI))
  1956. __gsi_config_ieob_irq_k(gsi_ctx->per.ee, gsihal_get_ch_reg_idx(evt_id),
  1957. gsihal_get_ch_reg_mask(evt_id),
  1958. 0);
  1959. else
  1960. __gsi_config_ieob_irq_k(gsi_ctx->per.ee, gsihal_get_ch_reg_idx(evt_id),
  1961. gsihal_get_ch_reg_mask(evt_id),
  1962. ~0);
  1963. }
  1964. else {
  1965. if ((props->intf != GSI_EVT_CHTYPE_GPI_EV) &&
  1966. (props->intr != GSI_INTR_MSI))
  1967. __gsi_config_ieob_irq(gsi_ctx->per.ee, 1 << evt_id, 0);
  1968. else
  1969. __gsi_config_ieob_irq(gsi_ctx->per.ee, 1 << ctx->id, ~0);
  1970. }
  1971. spin_unlock_irqrestore(&gsi_ctx->slock, flags);
  1972. return GSI_STATUS_SUCCESS;
  1973. }
  1974. EXPORT_SYMBOL(gsi_alloc_evt_ring);
  1975. static void __gsi_write_evt_ring_scratch(unsigned long evt_ring_hdl,
  1976. union __packed gsi_evt_scratch val)
  1977. {
  1978. gsihal_write_reg_nk(GSI_EE_n_EV_CH_k_SCRATCH_0,
  1979. gsi_ctx->per.ee, evt_ring_hdl, val.data.word1);
  1980. gsihal_write_reg_nk(GSI_EE_n_EV_CH_k_SCRATCH_1,
  1981. gsi_ctx->per.ee, evt_ring_hdl, val.data.word2);
  1982. }
  1983. int gsi_write_evt_ring_scratch(unsigned long evt_ring_hdl,
  1984. union __packed gsi_evt_scratch val)
  1985. {
  1986. struct gsi_evt_ctx *ctx;
  1987. if (!gsi_ctx) {
  1988. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1989. return -GSI_STATUS_NODEV;
  1990. }
  1991. if (evt_ring_hdl >= gsi_ctx->max_ev) {
  1992. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  1993. return -GSI_STATUS_INVALID_PARAMS;
  1994. }
  1995. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  1996. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  1997. GSIERR("bad state %d\n",
  1998. gsi_ctx->evtr[evt_ring_hdl].state);
  1999. return -GSI_STATUS_UNSUPPORTED_OP;
  2000. }
  2001. mutex_lock(&ctx->mlock);
  2002. ctx->scratch = val;
  2003. __gsi_write_evt_ring_scratch(evt_ring_hdl, val);
  2004. mutex_unlock(&ctx->mlock);
  2005. return GSI_STATUS_SUCCESS;
  2006. }
  2007. EXPORT_SYMBOL(gsi_write_evt_ring_scratch);
  2008. int gsi_dealloc_evt_ring(unsigned long evt_ring_hdl)
  2009. {
  2010. struct gsihal_reg_ee_n_ev_ch_cmd ev_ch_cmd;
  2011. enum gsi_evt_ch_cmd_opcode op = GSI_EVT_DE_ALLOC;
  2012. struct gsi_evt_ctx *ctx;
  2013. int res = 0;
  2014. u32 msi;
  2015. if (!gsi_ctx) {
  2016. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2017. return -GSI_STATUS_NODEV;
  2018. }
  2019. if (evt_ring_hdl >= gsi_ctx->max_ev ||
  2020. evt_ring_hdl >= GSI_EVT_RING_MAX) {
  2021. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  2022. return -GSI_STATUS_INVALID_PARAMS;
  2023. }
  2024. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  2025. if (atomic_read(&ctx->chan_ref_cnt)) {
  2026. GSIERR("%d channels still using this event ring\n",
  2027. atomic_read(&ctx->chan_ref_cnt));
  2028. return -GSI_STATUS_UNSUPPORTED_OP;
  2029. }
  2030. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  2031. GSIERR("bad state %d\n", ctx->state);
  2032. return -GSI_STATUS_UNSUPPORTED_OP;
  2033. }
  2034. /* Unpair the MSI */
  2035. if (ctx->props.intf == GSI_EVT_CHTYPE_GPI_EV &&
  2036. ctx->props.intr == GSI_INTR_MSI) {
  2037. GSIERR("Interrupt dereg for msi_irq = %d\n", ctx->props.msi_irq);
  2038. for (msi = 0; msi < gsi_ctx->msi.num; msi++) {
  2039. if (gsi_ctx->msi.msg[msi].data == ctx->props.intvec) {
  2040. mutex_lock(&gsi_ctx->mlock);
  2041. clear_bit(msi, gsi_ctx->msi.used);
  2042. gsi_ctx->msi.evt[msi] = 0;
  2043. clear_bit(evt_ring_hdl, &gsi_ctx->msi.mask);
  2044. mutex_unlock(&gsi_ctx->mlock);
  2045. }
  2046. }
  2047. }
  2048. mutex_lock(&gsi_ctx->mlock);
  2049. reinit_completion(&ctx->compl);
  2050. ev_ch_cmd.chid = evt_ring_hdl;
  2051. ev_ch_cmd.opcode = op;
  2052. gsihal_write_reg_n_fields(GSI_EE_n_EV_CH_CMD,
  2053. gsi_ctx->per.ee, &ev_ch_cmd);
  2054. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  2055. if (res == 0) {
  2056. GSIERR("evt_id=%lu timed out\n", evt_ring_hdl);
  2057. mutex_unlock(&gsi_ctx->mlock);
  2058. return -GSI_STATUS_TIMED_OUT;
  2059. }
  2060. if (ctx->state != GSI_EVT_RING_STATE_NOT_ALLOCATED) {
  2061. GSIERR("evt_id=%lu unexpected state=%u\n", evt_ring_hdl,
  2062. ctx->state);
  2063. /*
  2064. * IPA Hardware returned GSI RING not allocated, which is
  2065. * unexpected hardware state.
  2066. */
  2067. GSI_ASSERT();
  2068. }
  2069. mutex_unlock(&gsi_ctx->mlock);
  2070. if (!ctx->props.evchid_valid) {
  2071. mutex_lock(&gsi_ctx->mlock);
  2072. clear_bit(evt_ring_hdl, &gsi_ctx->evt_bmap);
  2073. mutex_unlock(&gsi_ctx->mlock);
  2074. }
  2075. atomic_dec(&gsi_ctx->num_evt_ring);
  2076. return GSI_STATUS_SUCCESS;
  2077. }
  2078. EXPORT_SYMBOL(gsi_dealloc_evt_ring);
  2079. int gsi_query_evt_ring_db_addr(unsigned long evt_ring_hdl,
  2080. uint32_t *db_addr_wp_lsb, uint32_t *db_addr_wp_msb)
  2081. {
  2082. struct gsi_evt_ctx *ctx;
  2083. if (!gsi_ctx) {
  2084. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2085. return -GSI_STATUS_NODEV;
  2086. }
  2087. if (!db_addr_wp_msb || !db_addr_wp_lsb) {
  2088. GSIERR("bad params msb=%pK lsb=%pK\n", db_addr_wp_msb,
  2089. db_addr_wp_lsb);
  2090. return -GSI_STATUS_INVALID_PARAMS;
  2091. }
  2092. if (evt_ring_hdl >= gsi_ctx->max_ev) {
  2093. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  2094. return -GSI_STATUS_INVALID_PARAMS;
  2095. }
  2096. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  2097. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  2098. GSIERR("bad state %d\n",
  2099. gsi_ctx->evtr[evt_ring_hdl].state);
  2100. return -GSI_STATUS_UNSUPPORTED_OP;
  2101. }
  2102. *db_addr_wp_lsb = gsi_ctx->per.phys_addr + gsihal_get_reg_nk_ofst(
  2103. GSI_EE_n_EV_CH_k_DOORBELL_0, gsi_ctx->per.ee, evt_ring_hdl);
  2104. *db_addr_wp_msb = gsi_ctx->per.phys_addr + gsihal_get_reg_nk_ofst(
  2105. GSI_EE_n_EV_CH_k_DOORBELL_1, gsi_ctx->per.ee, evt_ring_hdl);
  2106. return GSI_STATUS_SUCCESS;
  2107. }
  2108. EXPORT_SYMBOL(gsi_query_evt_ring_db_addr);
  2109. int gsi_ring_evt_ring_db(unsigned long evt_ring_hdl, uint64_t value)
  2110. {
  2111. struct gsi_evt_ctx *ctx;
  2112. if (!gsi_ctx) {
  2113. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2114. return -GSI_STATUS_NODEV;
  2115. }
  2116. if (evt_ring_hdl >= gsi_ctx->max_ev) {
  2117. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  2118. return -GSI_STATUS_INVALID_PARAMS;
  2119. }
  2120. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  2121. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  2122. GSIERR("bad state %d\n",
  2123. gsi_ctx->evtr[evt_ring_hdl].state);
  2124. return -GSI_STATUS_UNSUPPORTED_OP;
  2125. }
  2126. ctx->ring.wp_local = value;
  2127. gsi_ring_evt_doorbell(ctx);
  2128. return GSI_STATUS_SUCCESS;
  2129. }
  2130. EXPORT_SYMBOL(gsi_ring_evt_ring_db);
  2131. int gsi_ring_ch_ring_db(unsigned long chan_hdl, uint64_t value)
  2132. {
  2133. struct gsi_chan_ctx *ctx;
  2134. if (!gsi_ctx) {
  2135. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2136. return -GSI_STATUS_NODEV;
  2137. }
  2138. if (chan_hdl >= gsi_ctx->max_ch) {
  2139. GSIERR("bad chan_hdl=%lu\n", chan_hdl);
  2140. return -GSI_STATUS_INVALID_PARAMS;
  2141. }
  2142. ctx = &gsi_ctx->chan[chan_hdl];
  2143. if (ctx->state != GSI_CHAN_STATE_STARTED) {
  2144. GSIERR("bad state %d\n", ctx->state);
  2145. return -GSI_STATUS_UNSUPPORTED_OP;
  2146. }
  2147. ctx->ring.wp_local = value;
  2148. /* write MSB first */
  2149. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_DOORBELL_1,
  2150. gsi_ctx->per.ee, ctx->props.ch_id, GSI_MSB(ctx->ring.wp_local));
  2151. gsi_ring_chan_doorbell(ctx);
  2152. return GSI_STATUS_SUCCESS;
  2153. }
  2154. EXPORT_SYMBOL(gsi_ring_ch_ring_db);
  2155. int gsi_reset_evt_ring(unsigned long evt_ring_hdl)
  2156. {
  2157. struct gsihal_reg_ee_n_ev_ch_cmd ev_ch_cmd;
  2158. enum gsi_evt_ch_cmd_opcode op = GSI_EVT_RESET;
  2159. struct gsi_evt_ctx *ctx;
  2160. int res;
  2161. if (!gsi_ctx) {
  2162. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2163. return -GSI_STATUS_NODEV;
  2164. }
  2165. if (evt_ring_hdl >= gsi_ctx->max_ev) {
  2166. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  2167. return -GSI_STATUS_INVALID_PARAMS;
  2168. }
  2169. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  2170. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  2171. GSIERR("bad state %d\n", ctx->state);
  2172. return -GSI_STATUS_UNSUPPORTED_OP;
  2173. }
  2174. mutex_lock(&gsi_ctx->mlock);
  2175. reinit_completion(&ctx->compl);
  2176. ev_ch_cmd.chid = evt_ring_hdl;
  2177. ev_ch_cmd.opcode = op;
  2178. gsihal_write_reg_n_fields(GSI_EE_n_EV_CH_CMD,
  2179. gsi_ctx->per.ee, &ev_ch_cmd);
  2180. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  2181. if (res == 0) {
  2182. GSIERR("evt_id=%lu timed out\n", evt_ring_hdl);
  2183. mutex_unlock(&gsi_ctx->mlock);
  2184. return -GSI_STATUS_TIMED_OUT;
  2185. }
  2186. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  2187. GSIERR("evt_id=%lu unexpected state=%u\n", evt_ring_hdl,
  2188. ctx->state);
  2189. /*
  2190. * IPA Hardware returned GSI RING not allocated, which is
  2191. * unexpected. Indicates hardware instability.
  2192. */
  2193. GSI_ASSERT();
  2194. }
  2195. gsi_program_evt_ring_ctx(&ctx->props, evt_ring_hdl, gsi_ctx->per.ee);
  2196. gsi_init_evt_ring(&ctx->props, &ctx->ring);
  2197. /* restore scratch */
  2198. __gsi_write_evt_ring_scratch(evt_ring_hdl, ctx->scratch);
  2199. if (ctx->props.intf == GSI_EVT_CHTYPE_GPI_EV)
  2200. gsi_prime_evt_ring(ctx);
  2201. if (ctx->props.intf == GSI_EVT_CHTYPE_WDI2_EV)
  2202. gsi_prime_evt_ring_wdi(ctx);
  2203. mutex_unlock(&gsi_ctx->mlock);
  2204. return GSI_STATUS_SUCCESS;
  2205. }
  2206. EXPORT_SYMBOL(gsi_reset_evt_ring);
  2207. int gsi_get_evt_ring_cfg(unsigned long evt_ring_hdl,
  2208. struct gsi_evt_ring_props *props, union gsi_evt_scratch *scr)
  2209. {
  2210. struct gsi_evt_ctx *ctx;
  2211. if (!gsi_ctx) {
  2212. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2213. return -GSI_STATUS_NODEV;
  2214. }
  2215. if (!props || !scr) {
  2216. GSIERR("bad params props=%pK scr=%pK\n", props, scr);
  2217. return -GSI_STATUS_INVALID_PARAMS;
  2218. }
  2219. if (evt_ring_hdl >= gsi_ctx->max_ev) {
  2220. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  2221. return -GSI_STATUS_INVALID_PARAMS;
  2222. }
  2223. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  2224. if (ctx->state == GSI_EVT_RING_STATE_NOT_ALLOCATED) {
  2225. GSIERR("bad state %d\n", ctx->state);
  2226. return -GSI_STATUS_UNSUPPORTED_OP;
  2227. }
  2228. mutex_lock(&ctx->mlock);
  2229. *props = ctx->props;
  2230. *scr = ctx->scratch;
  2231. mutex_unlock(&ctx->mlock);
  2232. return GSI_STATUS_SUCCESS;
  2233. }
  2234. EXPORT_SYMBOL(gsi_get_evt_ring_cfg);
  2235. int gsi_set_evt_ring_cfg(unsigned long evt_ring_hdl,
  2236. struct gsi_evt_ring_props *props, union gsi_evt_scratch *scr)
  2237. {
  2238. struct gsi_evt_ctx *ctx;
  2239. if (!gsi_ctx) {
  2240. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2241. return -GSI_STATUS_NODEV;
  2242. }
  2243. if (!props || gsi_validate_evt_ring_props(props)) {
  2244. GSIERR("bad params props=%pK\n", props);
  2245. return -GSI_STATUS_INVALID_PARAMS;
  2246. }
  2247. if (evt_ring_hdl >= gsi_ctx->max_ev) {
  2248. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  2249. return -GSI_STATUS_INVALID_PARAMS;
  2250. }
  2251. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  2252. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  2253. GSIERR("bad state %d\n", ctx->state);
  2254. return -GSI_STATUS_UNSUPPORTED_OP;
  2255. }
  2256. if (ctx->props.exclusive != props->exclusive) {
  2257. GSIERR("changing immutable fields not supported\n");
  2258. return -GSI_STATUS_UNSUPPORTED_OP;
  2259. }
  2260. mutex_lock(&ctx->mlock);
  2261. ctx->props = *props;
  2262. if (scr)
  2263. ctx->scratch = *scr;
  2264. mutex_unlock(&ctx->mlock);
  2265. return gsi_reset_evt_ring(evt_ring_hdl);
  2266. }
  2267. EXPORT_SYMBOL(gsi_set_evt_ring_cfg);
  2268. static void gsi_program_chan_ctx_qos(struct gsi_chan_props *props,
  2269. unsigned int ee)
  2270. {
  2271. struct gsihal_reg_gsi_ee_n_gsi_ch_k_qos ch_k_qos;
  2272. ch_k_qos.wrr_weight = props->low_weight;
  2273. ch_k_qos.max_prefetch = props->max_prefetch;
  2274. ch_k_qos.use_db_eng = props->use_db_eng;
  2275. if (gsi_ctx->per.ver >= GSI_VER_2_0) {
  2276. if (gsi_ctx->per.ver < GSI_VER_2_5) {
  2277. ch_k_qos.use_escape_buf_only = props->prefetch_mode;
  2278. } else {
  2279. ch_k_qos.prefetch_mode = props->prefetch_mode;
  2280. ch_k_qos.empty_lvl_thrshold =
  2281. props->empty_lvl_threshold;
  2282. if (gsi_ctx->per.ver >= GSI_VER_2_9)
  2283. ch_k_qos.db_in_bytes = props->db_in_bytes;
  2284. if (gsi_ctx->per.ver >= GSI_VER_3_0)
  2285. ch_k_qos.low_latency_en = props->low_latency_en;
  2286. }
  2287. }
  2288. gsihal_write_reg_nk_fields(GSI_EE_n_GSI_CH_k_QOS,
  2289. ee, props->ch_id, &ch_k_qos);
  2290. }
  2291. static void gsi_program_chan_ctx(struct gsi_chan_props *props, unsigned int ee,
  2292. uint8_t erindex)
  2293. {
  2294. struct gsihal_reg_ch_k_cntxt_0 ch_k_cntxt_0;
  2295. struct gsihal_reg_ch_k_cntxt_1 ch_k_cntxt_1;
  2296. switch (props->prot) {
  2297. case GSI_CHAN_PROT_MHI:
  2298. case GSI_CHAN_PROT_XHCI:
  2299. case GSI_CHAN_PROT_GPI:
  2300. case GSI_CHAN_PROT_XDCI:
  2301. case GSI_CHAN_PROT_WDI2:
  2302. case GSI_CHAN_PROT_WDI3:
  2303. case GSI_CHAN_PROT_GCI:
  2304. case GSI_CHAN_PROT_MHIP:
  2305. ch_k_cntxt_0.chtype_protocol_msb = 0;
  2306. break;
  2307. case GSI_CHAN_PROT_AQC:
  2308. case GSI_CHAN_PROT_11AD:
  2309. case GSI_CHAN_PROT_RTK:
  2310. case GSI_CHAN_PROT_QDSS:
  2311. case GSI_CHAN_PROT_NTN:
  2312. ch_k_cntxt_0.chtype_protocol_msb = 1;
  2313. break;
  2314. default:
  2315. GSIERR("Unsupported protocol %d\n", props->prot);
  2316. WARN_ON(1);
  2317. return;
  2318. }
  2319. ch_k_cntxt_0.chtype_protocol = props->prot;
  2320. ch_k_cntxt_0.chtype_dir = props->dir;
  2321. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  2322. ch_k_cntxt_1.erindex = erindex;
  2323. } else {
  2324. ch_k_cntxt_0.erindex = erindex;
  2325. }
  2326. ch_k_cntxt_0.element_size = props->re_size;
  2327. gsihal_write_reg_nk_fields(GSI_EE_n_GSI_CH_k_CNTXT_0,
  2328. ee, props->ch_id, &ch_k_cntxt_0);
  2329. ch_k_cntxt_1.r_length = props->ring_len;
  2330. gsihal_write_reg_nk_fields(GSI_EE_n_GSI_CH_k_CNTXT_1,
  2331. ee, props->ch_id, &ch_k_cntxt_1);
  2332. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_2,
  2333. ee, props->ch_id, GSI_LSB(props->ring_base_addr));
  2334. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_3,
  2335. ee, props->ch_id, GSI_MSB(props->ring_base_addr));
  2336. gsi_program_chan_ctx_qos(props, ee);
  2337. }
  2338. static void gsi_init_chan_ring(struct gsi_chan_props *props,
  2339. struct gsi_ring_ctx *ctx)
  2340. {
  2341. ctx->base_va = (uintptr_t)props->ring_base_vaddr;
  2342. ctx->base = props->ring_base_addr;
  2343. ctx->wp = ctx->base;
  2344. ctx->rp = ctx->base;
  2345. ctx->wp_local = ctx->base;
  2346. ctx->rp_local = ctx->base;
  2347. ctx->len = props->ring_len;
  2348. ctx->elem_sz = props->re_size;
  2349. ctx->max_num_elem = ctx->len / ctx->elem_sz - 1;
  2350. ctx->end = ctx->base + (ctx->max_num_elem + 1) *
  2351. ctx->elem_sz;
  2352. }
  2353. static int gsi_validate_channel_props(struct gsi_chan_props *props)
  2354. {
  2355. uint64_t ra;
  2356. uint64_t last;
  2357. if (props->ch_id >= gsi_ctx->max_ch) {
  2358. GSIERR("ch_id %u invalid\n", props->ch_id);
  2359. return -GSI_STATUS_INVALID_PARAMS;
  2360. }
  2361. if ((props->re_size == GSI_CHAN_RE_SIZE_4B &&
  2362. props->ring_len % 4) ||
  2363. (props->re_size == GSI_CHAN_RE_SIZE_8B &&
  2364. props->ring_len % 8) ||
  2365. (props->re_size == GSI_CHAN_RE_SIZE_16B &&
  2366. props->ring_len % 16) ||
  2367. (props->re_size == GSI_CHAN_RE_SIZE_32B &&
  2368. props->ring_len % 32) ||
  2369. (props->re_size == GSI_CHAN_RE_SIZE_64B &&
  2370. props->ring_len % 64)) {
  2371. GSIERR("bad params ring_len %u not a multiple of re size %u\n",
  2372. props->ring_len, props->re_size);
  2373. return -GSI_STATUS_INVALID_PARAMS;
  2374. }
  2375. if (!gsihal_check_ring_length_valid(props->ring_len, props->re_size))
  2376. return -GSI_STATUS_INVALID_PARAMS;
  2377. ra = props->ring_base_addr;
  2378. do_div(ra, roundup_pow_of_two(props->ring_len));
  2379. if (props->ring_base_addr != ra * roundup_pow_of_two(props->ring_len)) {
  2380. GSIERR("bad params ring base not aligned 0x%llx align 0x%lx\n",
  2381. props->ring_base_addr,
  2382. roundup_pow_of_two(props->ring_len));
  2383. return -GSI_STATUS_INVALID_PARAMS;
  2384. }
  2385. last = props->ring_base_addr + props->ring_len - props->re_size;
  2386. /* MSB should stay same within the ring */
  2387. if ((props->ring_base_addr & 0xFFFFFFFF00000000ULL) !=
  2388. (last & 0xFFFFFFFF00000000ULL)) {
  2389. GSIERR("MSB is not fixed on ring base 0x%llx size 0x%x\n",
  2390. props->ring_base_addr,
  2391. props->ring_len);
  2392. return -GSI_STATUS_INVALID_PARAMS;
  2393. }
  2394. if (props->prot == GSI_CHAN_PROT_GPI &&
  2395. !props->ring_base_vaddr) {
  2396. GSIERR("protocol %u requires ring base VA\n", props->prot);
  2397. return -GSI_STATUS_INVALID_PARAMS;
  2398. }
  2399. if (props->low_weight > GSI_MAX_CH_LOW_WEIGHT) {
  2400. GSIERR("invalid channel low weight %u\n", props->low_weight);
  2401. return -GSI_STATUS_INVALID_PARAMS;
  2402. }
  2403. if (props->prot == GSI_CHAN_PROT_GPI && !props->xfer_cb) {
  2404. GSIERR("xfer callback must be provided\n");
  2405. return -GSI_STATUS_INVALID_PARAMS;
  2406. }
  2407. if (!props->err_cb) {
  2408. GSIERR("err callback must be provided\n");
  2409. return -GSI_STATUS_INVALID_PARAMS;
  2410. }
  2411. return GSI_STATUS_SUCCESS;
  2412. }
  2413. int gsi_alloc_channel(struct gsi_chan_props *props, unsigned long dev_hdl,
  2414. unsigned long *chan_hdl)
  2415. {
  2416. struct gsi_chan_ctx *ctx;
  2417. int res;
  2418. int ee;
  2419. enum gsi_ch_cmd_opcode op = GSI_CH_ALLOCATE;
  2420. uint8_t erindex;
  2421. struct gsi_user_data *user_data;
  2422. size_t user_data_size;
  2423. if (!gsi_ctx) {
  2424. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2425. return -GSI_STATUS_NODEV;
  2426. }
  2427. if (!props || !chan_hdl || dev_hdl != (uintptr_t)gsi_ctx) {
  2428. GSIERR("bad params props=%pK dev_hdl=0x%lx chan_hdl=%pK\n",
  2429. props, dev_hdl, chan_hdl);
  2430. return -GSI_STATUS_INVALID_PARAMS;
  2431. }
  2432. if (gsi_validate_channel_props(props)) {
  2433. GSIERR("bad params\n");
  2434. return -GSI_STATUS_INVALID_PARAMS;
  2435. }
  2436. if (props->evt_ring_hdl != ~0) {
  2437. if (props->evt_ring_hdl >= gsi_ctx->max_ev) {
  2438. GSIERR("invalid evt ring=%lu\n", props->evt_ring_hdl);
  2439. return -GSI_STATUS_INVALID_PARAMS;
  2440. }
  2441. if (atomic_read(
  2442. &gsi_ctx->evtr[props->evt_ring_hdl].chan_ref_cnt) &&
  2443. gsi_ctx->evtr[props->evt_ring_hdl].props.exclusive &&
  2444. gsi_ctx->evtr[props->evt_ring_hdl].chan[0]->props.prot !=
  2445. GSI_CHAN_PROT_GCI) {
  2446. GSIERR("evt ring=%lu exclusively used by ch_hdl=%pK\n",
  2447. props->evt_ring_hdl, chan_hdl);
  2448. return -GSI_STATUS_UNSUPPORTED_OP;
  2449. }
  2450. }
  2451. ctx = &gsi_ctx->chan[props->ch_id];
  2452. if (ctx->allocated) {
  2453. GSIERR("chan %d already allocated\n", props->ch_id);
  2454. return -GSI_STATUS_NODEV;
  2455. }
  2456. memset(ctx, 0, sizeof(*ctx));
  2457. /* For IPA offloaded WDI channels not required user_data pointer */
  2458. if (props->prot != GSI_CHAN_PROT_WDI2 &&
  2459. props->prot != GSI_CHAN_PROT_WDI3)
  2460. user_data_size = props->ring_len / props->re_size;
  2461. else
  2462. user_data_size = props->re_size;
  2463. /*
  2464. * GCI channels might have OOO event completions up to GSI_VEID_MAX.
  2465. * user_data needs to be large enough to accommodate those.
  2466. * TODO: increase user data size if GSI_VEID_MAX is not enough
  2467. */
  2468. if (props->prot == GSI_CHAN_PROT_GCI)
  2469. user_data_size += GSI_VEID_MAX;
  2470. user_data = devm_kzalloc(gsi_ctx->dev,
  2471. user_data_size * sizeof(*user_data),
  2472. GFP_KERNEL);
  2473. if (user_data == NULL) {
  2474. GSIERR("context not allocated\n");
  2475. return -GSI_STATUS_RES_ALLOC_FAILURE;
  2476. }
  2477. mutex_init(&ctx->mlock);
  2478. init_completion(&ctx->compl);
  2479. atomic_set(&ctx->poll_mode, GSI_CHAN_MODE_CALLBACK);
  2480. ctx->props = *props;
  2481. if (gsi_ctx->per.ver != GSI_VER_2_2) {
  2482. struct gsihal_reg_ee_n_gsi_ch_cmd ch_cmd;
  2483. mutex_lock(&gsi_ctx->mlock);
  2484. ee = gsi_ctx->per.ee;
  2485. gsi_ctx->ch_dbg[props->ch_id].ch_allocate++;
  2486. ch_cmd.chid = props->ch_id;
  2487. ch_cmd.opcode = op;
  2488. gsihal_write_reg_n_fields(GSI_EE_n_GSI_CH_CMD, ee, &ch_cmd);
  2489. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  2490. if (res == 0) {
  2491. GSIERR("chan_hdl=%u timed out\n", props->ch_id);
  2492. mutex_unlock(&gsi_ctx->mlock);
  2493. devm_kfree(gsi_ctx->dev, user_data);
  2494. return -GSI_STATUS_TIMED_OUT;
  2495. }
  2496. if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  2497. GSIERR("chan_hdl=%u allocation failed state=%d\n",
  2498. props->ch_id, ctx->state);
  2499. mutex_unlock(&gsi_ctx->mlock);
  2500. devm_kfree(gsi_ctx->dev, user_data);
  2501. return -GSI_STATUS_RES_ALLOC_FAILURE;
  2502. }
  2503. mutex_unlock(&gsi_ctx->mlock);
  2504. } else {
  2505. mutex_lock(&gsi_ctx->mlock);
  2506. ctx->state = GSI_CHAN_STATE_ALLOCATED;
  2507. mutex_unlock(&gsi_ctx->mlock);
  2508. }
  2509. erindex = props->evt_ring_hdl != ~0 ? props->evt_ring_hdl :
  2510. GSI_NO_EVT_ERINDEX;
  2511. if (erindex != GSI_NO_EVT_ERINDEX && erindex >= GSI_EVT_RING_MAX) {
  2512. GSIERR("invalid erindex %u\n", erindex);
  2513. devm_kfree(gsi_ctx->dev, user_data);
  2514. return -GSI_STATUS_INVALID_PARAMS;
  2515. }
  2516. if (erindex < GSI_EVT_RING_MAX) {
  2517. ctx->evtr = &gsi_ctx->evtr[erindex];
  2518. if(ctx->evtr->num_of_chan_allocated
  2519. >= MAX_CHANNELS_SHARING_EVENT_RING) {
  2520. GSIERR(
  2521. "too many channels sharing the same event ring %u\n",
  2522. erindex);
  2523. GSI_ASSERT();
  2524. }
  2525. if (props->prot != GSI_CHAN_PROT_GCI) {
  2526. atomic_inc(&ctx->evtr->chan_ref_cnt);
  2527. if (ctx->evtr->props.exclusive) {
  2528. if (atomic_read(&ctx->evtr->chan_ref_cnt) == 1)
  2529. ctx->evtr->chan
  2530. [ctx->evtr->num_of_chan_allocated++] = ctx;
  2531. }
  2532. else {
  2533. ctx->evtr->chan[ctx->evtr->num_of_chan_allocated++]
  2534. = ctx;
  2535. }
  2536. }
  2537. }
  2538. gsi_program_chan_ctx(props, gsi_ctx->per.ee, erindex);
  2539. spin_lock_init(&ctx->ring.slock);
  2540. gsi_init_chan_ring(props, &ctx->ring);
  2541. if (!props->max_re_expected)
  2542. ctx->props.max_re_expected = ctx->ring.max_num_elem;
  2543. ctx->user_data = user_data;
  2544. *chan_hdl = props->ch_id;
  2545. ctx->allocated = true;
  2546. ctx->stats.dp.last_timestamp = jiffies_to_msecs(jiffies);
  2547. atomic_inc(&gsi_ctx->num_chan);
  2548. if (props->prot == GSI_CHAN_PROT_GCI) {
  2549. gsi_ctx->coal_info.ch_id = props->ch_id;
  2550. gsi_ctx->coal_info.evchid = props->evt_ring_hdl;
  2551. }
  2552. return GSI_STATUS_SUCCESS;
  2553. }
  2554. EXPORT_SYMBOL(gsi_alloc_channel);
  2555. static int gsi_alloc_ap_channel(unsigned int chan_hdl)
  2556. {
  2557. struct gsi_chan_ctx *ctx;
  2558. struct gsihal_reg_ee_n_gsi_ch_cmd ch_cmd;
  2559. int res;
  2560. int ee;
  2561. enum gsi_ch_cmd_opcode op = GSI_CH_ALLOCATE;
  2562. if (!gsi_ctx) {
  2563. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2564. return -GSI_STATUS_NODEV;
  2565. }
  2566. ctx = &gsi_ctx->chan[chan_hdl];
  2567. if (ctx->allocated) {
  2568. GSIERR("chan %d already allocated\n", chan_hdl);
  2569. return -GSI_STATUS_NODEV;
  2570. }
  2571. memset(ctx, 0, sizeof(*ctx));
  2572. mutex_init(&ctx->mlock);
  2573. init_completion(&ctx->compl);
  2574. atomic_set(&ctx->poll_mode, GSI_CHAN_MODE_CALLBACK);
  2575. mutex_lock(&gsi_ctx->mlock);
  2576. ee = gsi_ctx->per.ee;
  2577. gsi_ctx->ch_dbg[chan_hdl].ch_allocate++;
  2578. ch_cmd.chid = chan_hdl;
  2579. ch_cmd.opcode = op;
  2580. gsihal_write_reg_n_fields(GSI_EE_n_GSI_CH_CMD, ee, &ch_cmd);
  2581. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  2582. if (res == 0) {
  2583. GSIERR("chan_hdl=%u timed out\n", chan_hdl);
  2584. mutex_unlock(&gsi_ctx->mlock);
  2585. return -GSI_STATUS_TIMED_OUT;
  2586. }
  2587. if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  2588. GSIERR("chan_hdl=%u allocation failed state=%d\n",
  2589. chan_hdl, ctx->state);
  2590. mutex_unlock(&gsi_ctx->mlock);
  2591. return -GSI_STATUS_RES_ALLOC_FAILURE;
  2592. }
  2593. mutex_unlock(&gsi_ctx->mlock);
  2594. return GSI_STATUS_SUCCESS;
  2595. }
  2596. static void __gsi_write_channel_scratch(unsigned long chan_hdl,
  2597. union __packed gsi_channel_scratch val)
  2598. {
  2599. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_0,
  2600. gsi_ctx->per.ee, chan_hdl, val.data.word1);
  2601. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_1,
  2602. gsi_ctx->per.ee, chan_hdl, val.data.word2);
  2603. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_2,
  2604. gsi_ctx->per.ee, chan_hdl, val.data.word3);
  2605. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_3,
  2606. gsi_ctx->per.ee, chan_hdl, val.data.word4);
  2607. }
  2608. static void __gsi_write_wdi3_channel_scratch2_reg(unsigned long chan_hdl,
  2609. union __packed gsi_wdi3_channel_scratch2_reg val)
  2610. {
  2611. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_2,
  2612. gsi_ctx->per.ee, chan_hdl, val.data.word1);
  2613. }
  2614. int gsi_write_channel_scratch3_reg(unsigned long chan_hdl,
  2615. union __packed gsi_wdi_channel_scratch3_reg val)
  2616. {
  2617. struct gsi_chan_ctx *ctx;
  2618. if (!gsi_ctx) {
  2619. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2620. return -GSI_STATUS_NODEV;
  2621. }
  2622. if (chan_hdl >= gsi_ctx->max_ch) {
  2623. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2624. return -GSI_STATUS_INVALID_PARAMS;
  2625. }
  2626. ctx = &gsi_ctx->chan[chan_hdl];
  2627. mutex_lock(&ctx->mlock);
  2628. ctx->scratch.wdi.endp_metadatareg_offset =
  2629. val.wdi.endp_metadatareg_offset;
  2630. ctx->scratch.wdi.qmap_id = val.wdi.qmap_id;
  2631. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_3,
  2632. gsi_ctx->per.ee, chan_hdl, val.data.word1);
  2633. mutex_unlock(&ctx->mlock);
  2634. return GSI_STATUS_SUCCESS;
  2635. }
  2636. EXPORT_SYMBOL(gsi_write_channel_scratch3_reg);
  2637. int gsi_write_channel_scratch2_reg(unsigned long chan_hdl,
  2638. union __packed gsi_wdi2_channel_scratch2_reg val)
  2639. {
  2640. struct gsi_chan_ctx *ctx;
  2641. if (!gsi_ctx) {
  2642. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2643. return -GSI_STATUS_NODEV;
  2644. }
  2645. if (chan_hdl >= gsi_ctx->max_ch) {
  2646. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2647. return -GSI_STATUS_INVALID_PARAMS;
  2648. }
  2649. ctx = &gsi_ctx->chan[chan_hdl];
  2650. mutex_lock(&ctx->mlock);
  2651. ctx->scratch.wdi2_new.endp_metadatareg_offset =
  2652. val.wdi.endp_metadatareg_offset;
  2653. ctx->scratch.wdi2_new.qmap_id = val.wdi.qmap_id;
  2654. val.wdi.update_ri_moderation_threshold =
  2655. ctx->scratch.wdi2_new.update_ri_moderation_threshold;
  2656. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_2,
  2657. gsi_ctx->per.ee, chan_hdl, val.data.word1);
  2658. mutex_unlock(&ctx->mlock);
  2659. return GSI_STATUS_SUCCESS;
  2660. }
  2661. EXPORT_SYMBOL(gsi_write_channel_scratch2_reg);
  2662. static void __gsi_read_channel_scratch(unsigned long chan_hdl,
  2663. union __packed gsi_channel_scratch * val)
  2664. {
  2665. val->data.word1 = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_0,
  2666. gsi_ctx->per.ee, chan_hdl);
  2667. val->data.word2 = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_1,
  2668. gsi_ctx->per.ee, chan_hdl);
  2669. val->data.word3 = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_2,
  2670. gsi_ctx->per.ee, chan_hdl);
  2671. val->data.word4 = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_3,
  2672. gsi_ctx->per.ee, chan_hdl);
  2673. }
  2674. static void __gsi_read_wdi3_channel_scratch2_reg(unsigned long chan_hdl,
  2675. union __packed gsi_wdi3_channel_scratch2_reg * val)
  2676. {
  2677. val->data.word1 = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_2,
  2678. gsi_ctx->per.ee, chan_hdl);
  2679. }
  2680. int gsi_write_channel_scratch(unsigned long chan_hdl,
  2681. union __packed gsi_channel_scratch val)
  2682. {
  2683. struct gsi_chan_ctx *ctx;
  2684. if (!gsi_ctx) {
  2685. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2686. return -GSI_STATUS_NODEV;
  2687. }
  2688. if (chan_hdl >= gsi_ctx->max_ch) {
  2689. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2690. return -GSI_STATUS_INVALID_PARAMS;
  2691. }
  2692. if (gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_ALLOCATED &&
  2693. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STOPPED) {
  2694. GSIERR("bad state %d\n",
  2695. gsi_ctx->chan[chan_hdl].state);
  2696. return -GSI_STATUS_UNSUPPORTED_OP;
  2697. }
  2698. ctx = &gsi_ctx->chan[chan_hdl];
  2699. mutex_lock(&ctx->mlock);
  2700. ctx->scratch = val;
  2701. __gsi_write_channel_scratch(chan_hdl, val);
  2702. mutex_unlock(&ctx->mlock);
  2703. return GSI_STATUS_SUCCESS;
  2704. }
  2705. EXPORT_SYMBOL(gsi_write_channel_scratch);
  2706. int gsi_write_wdi3_channel_scratch2_reg(unsigned long chan_hdl,
  2707. union __packed gsi_wdi3_channel_scratch2_reg val)
  2708. {
  2709. struct gsi_chan_ctx *ctx;
  2710. if (!gsi_ctx) {
  2711. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2712. return -GSI_STATUS_NODEV;
  2713. }
  2714. if (chan_hdl >= gsi_ctx->max_ch) {
  2715. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2716. return -GSI_STATUS_INVALID_PARAMS;
  2717. }
  2718. if (gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_ALLOCATED &&
  2719. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STARTED &&
  2720. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STOPPED) {
  2721. GSIERR("bad state %d\n",
  2722. gsi_ctx->chan[chan_hdl].state);
  2723. return -GSI_STATUS_UNSUPPORTED_OP;
  2724. }
  2725. ctx = &gsi_ctx->chan[chan_hdl];
  2726. mutex_lock(&ctx->mlock);
  2727. ctx->scratch.data.word3 = val.data.word1;
  2728. __gsi_write_wdi3_channel_scratch2_reg(chan_hdl, val);
  2729. mutex_unlock(&ctx->mlock);
  2730. return GSI_STATUS_SUCCESS;
  2731. }
  2732. EXPORT_SYMBOL(gsi_write_wdi3_channel_scratch2_reg);
  2733. int gsi_read_channel_scratch(unsigned long chan_hdl,
  2734. union __packed gsi_channel_scratch *val)
  2735. {
  2736. struct gsi_chan_ctx *ctx;
  2737. if (!gsi_ctx) {
  2738. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2739. return -GSI_STATUS_NODEV;
  2740. }
  2741. if (chan_hdl >= gsi_ctx->max_ch) {
  2742. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2743. return -GSI_STATUS_INVALID_PARAMS;
  2744. }
  2745. if (gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_ALLOCATED &&
  2746. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STARTED &&
  2747. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STOPPED) {
  2748. GSIERR("bad state %d\n",
  2749. gsi_ctx->chan[chan_hdl].state);
  2750. return -GSI_STATUS_UNSUPPORTED_OP;
  2751. }
  2752. ctx = &gsi_ctx->chan[chan_hdl];
  2753. mutex_lock(&ctx->mlock);
  2754. __gsi_read_channel_scratch(chan_hdl, val);
  2755. mutex_unlock(&ctx->mlock);
  2756. return GSI_STATUS_SUCCESS;
  2757. }
  2758. EXPORT_SYMBOL(gsi_read_channel_scratch);
  2759. int gsi_read_wdi3_channel_scratch2_reg(unsigned long chan_hdl,
  2760. union __packed gsi_wdi3_channel_scratch2_reg * val)
  2761. {
  2762. struct gsi_chan_ctx *ctx;
  2763. if (!gsi_ctx) {
  2764. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2765. return -GSI_STATUS_NODEV;
  2766. }
  2767. if (chan_hdl >= gsi_ctx->max_ch) {
  2768. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2769. return -GSI_STATUS_INVALID_PARAMS;
  2770. }
  2771. if (gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_ALLOCATED &&
  2772. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STARTED &&
  2773. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STOPPED) {
  2774. GSIERR("bad state %d\n",
  2775. gsi_ctx->chan[chan_hdl].state);
  2776. return -GSI_STATUS_UNSUPPORTED_OP;
  2777. }
  2778. ctx = &gsi_ctx->chan[chan_hdl];
  2779. mutex_lock(&ctx->mlock);
  2780. __gsi_read_wdi3_channel_scratch2_reg(chan_hdl, val);
  2781. mutex_unlock(&ctx->mlock);
  2782. return GSI_STATUS_SUCCESS;
  2783. }
  2784. EXPORT_SYMBOL(gsi_read_wdi3_channel_scratch2_reg);
  2785. int gsi_update_mhi_channel_scratch(unsigned long chan_hdl,
  2786. struct __packed gsi_mhi_channel_scratch mscr)
  2787. {
  2788. struct gsi_chan_ctx *ctx;
  2789. if (!gsi_ctx) {
  2790. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2791. return -GSI_STATUS_NODEV;
  2792. }
  2793. if (chan_hdl >= gsi_ctx->max_ch) {
  2794. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2795. return -GSI_STATUS_INVALID_PARAMS;
  2796. }
  2797. if (gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_ALLOCATED &&
  2798. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STOPPED) {
  2799. GSIERR("bad state %d\n",
  2800. gsi_ctx->chan[chan_hdl].state);
  2801. return -GSI_STATUS_UNSUPPORTED_OP;
  2802. }
  2803. ctx = &gsi_ctx->chan[chan_hdl];
  2804. mutex_lock(&ctx->mlock);
  2805. ctx->scratch = __gsi_update_mhi_channel_scratch(chan_hdl, mscr);
  2806. mutex_unlock(&ctx->mlock);
  2807. return GSI_STATUS_SUCCESS;
  2808. }
  2809. EXPORT_SYMBOL(gsi_update_mhi_channel_scratch);
  2810. int gsi_query_channel_db_addr(unsigned long chan_hdl,
  2811. uint32_t *db_addr_wp_lsb, uint32_t *db_addr_wp_msb)
  2812. {
  2813. if (!gsi_ctx) {
  2814. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2815. return -GSI_STATUS_NODEV;
  2816. }
  2817. if (!db_addr_wp_msb || !db_addr_wp_lsb) {
  2818. GSIERR("bad params msb=%pK lsb=%pK\n", db_addr_wp_msb,
  2819. db_addr_wp_lsb);
  2820. return -GSI_STATUS_INVALID_PARAMS;
  2821. }
  2822. if (chan_hdl >= gsi_ctx->max_ch) {
  2823. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2824. return -GSI_STATUS_INVALID_PARAMS;
  2825. }
  2826. if (gsi_ctx->chan[chan_hdl].state == GSI_CHAN_STATE_NOT_ALLOCATED) {
  2827. GSIERR("bad state %d\n",
  2828. gsi_ctx->chan[chan_hdl].state);
  2829. return -GSI_STATUS_UNSUPPORTED_OP;
  2830. }
  2831. *db_addr_wp_lsb = gsi_ctx->per.phys_addr +
  2832. gsihal_get_reg_nk_ofst(GSI_EE_n_GSI_CH_k_DOORBELL_0,
  2833. gsi_ctx->per.ee, chan_hdl);
  2834. *db_addr_wp_msb = gsi_ctx->per.phys_addr +
  2835. gsihal_get_reg_nk_ofst(GSI_EE_n_GSI_CH_k_DOORBELL_1,
  2836. gsi_ctx->per.ee, chan_hdl);
  2837. return GSI_STATUS_SUCCESS;
  2838. }
  2839. EXPORT_SYMBOL(gsi_query_channel_db_addr);
  2840. int gsi_pending_irq_type(void)
  2841. {
  2842. int ee = gsi_ctx->per.ee;
  2843. return gsihal_read_reg_n(GSI_EE_n_CNTXT_TYPE_IRQ, ee);
  2844. }
  2845. EXPORT_SYMBOL(gsi_pending_irq_type);
  2846. int gsi_start_channel(unsigned long chan_hdl)
  2847. {
  2848. enum gsi_ch_cmd_opcode op = GSI_CH_START;
  2849. uint32_t val;
  2850. struct gsihal_reg_ee_n_gsi_ch_cmd ch_cmd;
  2851. struct gsi_chan_ctx *ctx;
  2852. if (!gsi_ctx) {
  2853. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2854. return -GSI_STATUS_NODEV;
  2855. }
  2856. if (chan_hdl >= gsi_ctx->max_ch) {
  2857. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2858. return -GSI_STATUS_INVALID_PARAMS;
  2859. }
  2860. ctx = &gsi_ctx->chan[chan_hdl];
  2861. if (ctx->state != GSI_CHAN_STATE_ALLOCATED &&
  2862. ctx->state != GSI_CHAN_STATE_STOP_IN_PROC &&
  2863. ctx->state != GSI_CHAN_STATE_STOPPED) {
  2864. GSIERR("bad state %d\n", ctx->state);
  2865. return -GSI_STATUS_UNSUPPORTED_OP;
  2866. }
  2867. mutex_lock(&gsi_ctx->mlock);
  2868. reinit_completion(&ctx->compl);
  2869. /* check if INTSET is in IRQ mode for GPI channel */
  2870. val = gsihal_read_reg_n(GSI_EE_n_CNTXT_INTSET, gsi_ctx->per.ee);
  2871. if (ctx->evtr &&
  2872. ctx->evtr->props.intf == GSI_EVT_CHTYPE_GPI_EV &&
  2873. val != GSI_INTR_IRQ) {
  2874. GSIERR("GSI_EE_n_CNTXT_INTSET %d\n", val);
  2875. BUG();
  2876. }
  2877. gsi_ctx->ch_dbg[chan_hdl].ch_start++;
  2878. ch_cmd.chid = chan_hdl;
  2879. ch_cmd.opcode = op;
  2880. gsihal_write_reg_n_fields(GSI_EE_n_GSI_CH_CMD,
  2881. gsi_ctx->per.ee, &ch_cmd);
  2882. GSIDBG("GSI Channel Start, waiting for completion\n");
  2883. gsi_channel_state_change_wait(chan_hdl,
  2884. ctx,
  2885. GSI_START_CMD_TIMEOUT_MS, op);
  2886. if (ctx->state != GSI_CHAN_STATE_STARTED &&
  2887. ctx->state != GSI_CHAN_STATE_FLOW_CONTROL) {
  2888. /*
  2889. * Hardware returned unexpected status, unexpected
  2890. * hardware state.
  2891. */
  2892. GSIERR("chan=%lu timed out, unexpected state=%u\n",
  2893. chan_hdl, ctx->state);
  2894. gsi_dump_ch_info(chan_hdl);
  2895. GSI_ASSERT();
  2896. }
  2897. GSIDBG("GSI Channel=%lu Start success\n", chan_hdl);
  2898. /* write order MUST be MSB followed by LSB */
  2899. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_DOORBELL_1,
  2900. gsi_ctx->per.ee, ctx->props.ch_id, GSI_MSB(ctx->ring.wp_local));
  2901. mutex_unlock(&gsi_ctx->mlock);
  2902. return GSI_STATUS_SUCCESS;
  2903. }
  2904. EXPORT_SYMBOL(gsi_start_channel);
  2905. void gsi_dump_ch_info(unsigned long chan_hdl)
  2906. {
  2907. uint32_t val;
  2908. if (!gsi_ctx) {
  2909. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2910. return;
  2911. }
  2912. if (chan_hdl >= gsi_ctx->max_ch) {
  2913. GSIDBG("invalid chan id %u\n", chan_hdl);
  2914. return;
  2915. }
  2916. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_0,
  2917. gsi_ctx->per.ee, chan_hdl);
  2918. GSIERR("CH%2d CTX0 0x%x\n", chan_hdl, val);
  2919. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_1,
  2920. gsi_ctx->per.ee, chan_hdl);
  2921. GSIERR("CH%2d CTX1 0x%x\n", chan_hdl, val);
  2922. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_2,
  2923. gsi_ctx->per.ee, chan_hdl);
  2924. GSIERR("CH%2d CTX2 0x%x\n", chan_hdl, val);
  2925. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_3,
  2926. gsi_ctx->per.ee, chan_hdl);
  2927. GSIERR("CH%2d CTX3 0x%x\n", chan_hdl, val);
  2928. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_4,
  2929. gsi_ctx->per.ee, chan_hdl);
  2930. GSIERR("CH%2d CTX4 0x%x\n", chan_hdl, val);
  2931. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_5,
  2932. gsi_ctx->per.ee, chan_hdl);
  2933. GSIERR("CH%2d CTX5 0x%x\n", chan_hdl, val);
  2934. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_6,
  2935. gsi_ctx->per.ee, chan_hdl);
  2936. GSIERR("CH%2d CTX6 0x%x\n", chan_hdl, val);
  2937. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_7,
  2938. gsi_ctx->per.ee, chan_hdl);
  2939. GSIERR("CH%2d CTX7 0x%x\n", chan_hdl, val);
  2940. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  2941. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_8,
  2942. gsi_ctx->per.ee, chan_hdl);
  2943. GSIERR("CH%2d CTX8 0x%x\n", chan_hdl, val);
  2944. }
  2945. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_RE_FETCH_READ_PTR,
  2946. gsi_ctx->per.ee, chan_hdl);
  2947. GSIERR("CH%2d REFRP 0x%x\n", chan_hdl, val);
  2948. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_RE_FETCH_WRITE_PTR,
  2949. gsi_ctx->per.ee, chan_hdl);
  2950. GSIERR("CH%2d REFWP 0x%x\n", chan_hdl, val);
  2951. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_QOS,
  2952. gsi_ctx->per.ee, chan_hdl);
  2953. GSIERR("CH%2d QOS 0x%x\n", chan_hdl, val);
  2954. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_0,
  2955. gsi_ctx->per.ee, chan_hdl);
  2956. GSIERR("CH%2d SCR0 0x%x\n", chan_hdl, val);
  2957. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_1,
  2958. gsi_ctx->per.ee, chan_hdl);
  2959. GSIERR("CH%2d SCR1 0x%x\n", chan_hdl, val);
  2960. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_2,
  2961. gsi_ctx->per.ee, chan_hdl);
  2962. GSIERR("CH%2d SCR2 0x%x\n", chan_hdl, val);
  2963. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_3,
  2964. gsi_ctx->per.ee, chan_hdl);
  2965. GSIERR("CH%2d SCR3 0x%x\n", chan_hdl, val);
  2966. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  2967. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_4,
  2968. gsi_ctx->per.ee, chan_hdl);
  2969. GSIERR("CH%2d SCR4 0x%x\n", chan_hdl, val);
  2970. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_5,
  2971. gsi_ctx->per.ee, chan_hdl);
  2972. GSIERR("CH%2d SCR5 0x%x\n", chan_hdl, val);
  2973. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_6,
  2974. gsi_ctx->per.ee, chan_hdl);
  2975. GSIERR("CH%2d SCR6 0x%x\n", chan_hdl, val);
  2976. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_7,
  2977. gsi_ctx->per.ee, chan_hdl);
  2978. GSIERR("CH%2d SCR7 0x%x\n", chan_hdl, val);
  2979. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_8,
  2980. gsi_ctx->per.ee, chan_hdl);
  2981. GSIERR("CH%2d SCR8 0x%x\n", chan_hdl, val);
  2982. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_9,
  2983. gsi_ctx->per.ee, chan_hdl);
  2984. GSIERR("CH%2d SCR9 0x%x\n", chan_hdl, val);
  2985. }
  2986. return;
  2987. }
  2988. EXPORT_SYMBOL(gsi_dump_ch_info);
  2989. int gsi_stop_channel(unsigned long chan_hdl)
  2990. {
  2991. enum gsi_ch_cmd_opcode op = GSI_CH_STOP;
  2992. int res;
  2993. uint32_t val;
  2994. struct gsihal_reg_ee_n_gsi_ch_cmd ch_cmd;
  2995. struct gsi_chan_ctx *ctx;
  2996. unsigned long flags;
  2997. if (!gsi_ctx) {
  2998. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2999. return -GSI_STATUS_NODEV;
  3000. }
  3001. if (chan_hdl >= gsi_ctx->max_ch) {
  3002. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3003. return -GSI_STATUS_INVALID_PARAMS;
  3004. }
  3005. ctx = &gsi_ctx->chan[chan_hdl];
  3006. if (ctx->state == GSI_CHAN_STATE_STOPPED) {
  3007. GSIDBG("chan_hdl=%lu already stopped\n", chan_hdl);
  3008. return GSI_STATUS_SUCCESS;
  3009. }
  3010. if (ctx->state != GSI_CHAN_STATE_STARTED &&
  3011. ctx->state != GSI_CHAN_STATE_STOP_IN_PROC &&
  3012. ctx->state != GSI_CHAN_STATE_ERROR) {
  3013. GSIERR("bad state %d\n", ctx->state);
  3014. return -GSI_STATUS_UNSUPPORTED_OP;
  3015. }
  3016. mutex_lock(&gsi_ctx->mlock);
  3017. reinit_completion(&ctx->compl);
  3018. /* check if INTSET is in IRQ mode for GPI channel */
  3019. val = gsihal_read_reg_n(GSI_EE_n_CNTXT_INTSET, gsi_ctx->per.ee);
  3020. if (ctx->evtr &&
  3021. ctx->evtr->props.intf == GSI_EVT_CHTYPE_GPI_EV &&
  3022. val != GSI_INTR_IRQ) {
  3023. GSIERR("GSI_EE_n_CNTXT_INTSET %d\n", val);
  3024. BUG();
  3025. }
  3026. gsi_ctx->ch_dbg[chan_hdl].ch_stop++;
  3027. ch_cmd.chid = chan_hdl;
  3028. ch_cmd.opcode = op;
  3029. gsihal_write_reg_n_fields(GSI_EE_n_GSI_CH_CMD,
  3030. gsi_ctx->per.ee, &ch_cmd);
  3031. GSIDBG("GSI Channel Stop, waiting for completion: 0x%x\n", val);
  3032. gsi_channel_state_change_wait(chan_hdl,
  3033. ctx,
  3034. GSI_STOP_CMD_TIMEOUT_MS, op);
  3035. if (ctx->state != GSI_CHAN_STATE_STOPPED &&
  3036. ctx->state != GSI_CHAN_STATE_STOP_IN_PROC) {
  3037. GSIERR("chan=%lu unexpected state=%u\n", chan_hdl, ctx->state);
  3038. gsi_dump_ch_info(chan_hdl);
  3039. res = -GSI_STATUS_BAD_STATE;
  3040. BUG();
  3041. goto free_lock;
  3042. }
  3043. if (ctx->state == GSI_CHAN_STATE_STOP_IN_PROC) {
  3044. GSIERR("chan=%lu busy try again\n", chan_hdl);
  3045. res = -GSI_STATUS_AGAIN;
  3046. goto free_lock;
  3047. }
  3048. /* If channel is stopped succesfully and has an event with IRQ type MSI
  3049. - clear IEOB */
  3050. if (ctx->evtr && ctx->evtr->props.intr == GSI_INTR_MSI) {
  3051. spin_lock_irqsave(&ctx->evtr->ring.slock, flags);
  3052. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  3053. gsihal_write_reg_nk(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_k,
  3054. gsi_ctx->per.ee, gsihal_get_ch_reg_idx(ctx->evtr->id),
  3055. gsihal_get_ch_reg_mask(ctx->evtr->id));
  3056. } else {
  3057. gsihal_write_reg_n(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR,
  3058. gsi_ctx->per.ee, 1 << ctx->evtr->id);
  3059. }
  3060. spin_unlock_irqrestore(&ctx->evtr->ring.slock, flags);
  3061. }
  3062. res = GSI_STATUS_SUCCESS;
  3063. free_lock:
  3064. mutex_unlock(&gsi_ctx->mlock);
  3065. return res;
  3066. }
  3067. EXPORT_SYMBOL(gsi_stop_channel);
  3068. int gsi_stop_db_channel(unsigned long chan_hdl)
  3069. {
  3070. enum gsi_ch_cmd_opcode op = GSI_CH_DB_STOP;
  3071. int res;
  3072. struct gsihal_reg_ee_n_gsi_ch_cmd ch_cmd;
  3073. struct gsi_chan_ctx *ctx;
  3074. if (!gsi_ctx) {
  3075. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3076. return -GSI_STATUS_NODEV;
  3077. }
  3078. if (chan_hdl >= gsi_ctx->max_ch) {
  3079. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3080. return -GSI_STATUS_INVALID_PARAMS;
  3081. }
  3082. ctx = &gsi_ctx->chan[chan_hdl];
  3083. if (ctx->state == GSI_CHAN_STATE_STOPPED) {
  3084. GSIDBG("chan_hdl=%lu already stopped\n", chan_hdl);
  3085. return GSI_STATUS_SUCCESS;
  3086. }
  3087. if (ctx->state != GSI_CHAN_STATE_STARTED &&
  3088. ctx->state != GSI_CHAN_STATE_STOP_IN_PROC) {
  3089. GSIERR("bad state %d\n", ctx->state);
  3090. return -GSI_STATUS_UNSUPPORTED_OP;
  3091. }
  3092. mutex_lock(&gsi_ctx->mlock);
  3093. reinit_completion(&ctx->compl);
  3094. gsi_ctx->ch_dbg[chan_hdl].ch_db_stop++;
  3095. ch_cmd.chid = chan_hdl;
  3096. ch_cmd.opcode = op;
  3097. gsihal_write_reg_n_fields(GSI_EE_n_GSI_CH_CMD,
  3098. gsi_ctx->per.ee, &ch_cmd);
  3099. res = wait_for_completion_timeout(&ctx->compl,
  3100. msecs_to_jiffies(GSI_STOP_CMD_TIMEOUT_MS));
  3101. if (res == 0) {
  3102. GSIERR("chan_hdl=%lu timed out\n", chan_hdl);
  3103. res = -GSI_STATUS_TIMED_OUT;
  3104. goto free_lock;
  3105. }
  3106. if (ctx->state != GSI_CHAN_STATE_STOPPED &&
  3107. ctx->state != GSI_CHAN_STATE_STOP_IN_PROC) {
  3108. GSIERR("chan=%lu unexpected state=%u\n", chan_hdl, ctx->state);
  3109. res = -GSI_STATUS_BAD_STATE;
  3110. goto free_lock;
  3111. }
  3112. if (ctx->state == GSI_CHAN_STATE_STOP_IN_PROC) {
  3113. GSIERR("chan=%lu busy try again\n", chan_hdl);
  3114. res = -GSI_STATUS_AGAIN;
  3115. goto free_lock;
  3116. }
  3117. res = GSI_STATUS_SUCCESS;
  3118. free_lock:
  3119. mutex_unlock(&gsi_ctx->mlock);
  3120. return res;
  3121. }
  3122. EXPORT_SYMBOL(gsi_stop_db_channel);
  3123. int gsi_reset_channel(unsigned long chan_hdl)
  3124. {
  3125. enum gsi_ch_cmd_opcode op = GSI_CH_RESET;
  3126. int res;
  3127. struct gsihal_reg_ee_n_gsi_ch_cmd ch_cmd;
  3128. struct gsi_chan_ctx *ctx;
  3129. bool reset_done = false;
  3130. uint32_t retry_cnt = 0;
  3131. if (!gsi_ctx) {
  3132. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3133. return -GSI_STATUS_NODEV;
  3134. }
  3135. if (chan_hdl >= gsi_ctx->max_ch) {
  3136. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3137. return -GSI_STATUS_INVALID_PARAMS;
  3138. }
  3139. ctx = &gsi_ctx->chan[chan_hdl];
  3140. /*
  3141. * In WDI3 case, if SAP enabled but no client connected,
  3142. * GSI will be in allocated state. When SAP disabled,
  3143. * gsi_reset_channel will be called and reset is needed.
  3144. */
  3145. if (ctx->state != GSI_CHAN_STATE_STOPPED &&
  3146. ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  3147. GSIERR("bad state %d\n", ctx->state);
  3148. return -GSI_STATUS_UNSUPPORTED_OP;
  3149. }
  3150. mutex_lock(&gsi_ctx->mlock);
  3151. reset:
  3152. reinit_completion(&ctx->compl);
  3153. gsi_ctx->ch_dbg[chan_hdl].ch_reset++;
  3154. ch_cmd.chid = chan_hdl;
  3155. ch_cmd.opcode = op;
  3156. gsihal_write_reg_n_fields(GSI_EE_n_GSI_CH_CMD,
  3157. gsi_ctx->per.ee, &ch_cmd);
  3158. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  3159. if (res == 0) {
  3160. GSIERR("chan_hdl=%lu timed out\n", chan_hdl);
  3161. mutex_unlock(&gsi_ctx->mlock);
  3162. return -GSI_STATUS_TIMED_OUT;
  3163. }
  3164. revrfy_chnlstate:
  3165. if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  3166. GSIERR("chan_hdl=%lu unexpected state=%u\n", chan_hdl,
  3167. ctx->state);
  3168. /* GSI register update state not sync with gsi channel
  3169. * context state not sync, need to wait for 1ms to sync.
  3170. */
  3171. retry_cnt++;
  3172. if (retry_cnt <= GSI_CHNL_STATE_MAX_RETRYCNT) {
  3173. usleep_range(GSI_RESET_WA_MIN_SLEEP,
  3174. GSI_RESET_WA_MAX_SLEEP);
  3175. goto revrfy_chnlstate;
  3176. }
  3177. /*
  3178. * Hardware returned incorrect state, unexpected
  3179. * hardware state.
  3180. */
  3181. GSI_ASSERT();
  3182. }
  3183. /* Hardware issue fixed from GSI 2.0 and no need for the WA */
  3184. if (gsi_ctx->per.ver >= GSI_VER_2_0)
  3185. reset_done = true;
  3186. /* workaround: reset GSI producers again */
  3187. if (ctx->props.dir == GSI_CHAN_DIR_FROM_GSI && !reset_done) {
  3188. usleep_range(GSI_RESET_WA_MIN_SLEEP, GSI_RESET_WA_MAX_SLEEP);
  3189. reset_done = true;
  3190. goto reset;
  3191. }
  3192. if (ctx->props.cleanup_cb)
  3193. gsi_cleanup_xfer_user_data(chan_hdl, ctx->props.cleanup_cb);
  3194. gsi_program_chan_ctx(&ctx->props, gsi_ctx->per.ee,
  3195. ctx->evtr ? ctx->evtr->id : GSI_NO_EVT_ERINDEX);
  3196. gsi_init_chan_ring(&ctx->props, &ctx->ring);
  3197. /* restore scratch */
  3198. __gsi_write_channel_scratch(chan_hdl, ctx->scratch);
  3199. mutex_unlock(&gsi_ctx->mlock);
  3200. return GSI_STATUS_SUCCESS;
  3201. }
  3202. EXPORT_SYMBOL(gsi_reset_channel);
  3203. int gsi_dealloc_channel(unsigned long chan_hdl)
  3204. {
  3205. enum gsi_ch_cmd_opcode op = GSI_CH_DE_ALLOC;
  3206. int res;
  3207. struct gsihal_reg_ee_n_gsi_ch_cmd ch_cmd;
  3208. struct gsi_chan_ctx *ctx;
  3209. if (!gsi_ctx) {
  3210. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3211. return -GSI_STATUS_NODEV;
  3212. }
  3213. if (chan_hdl >= gsi_ctx->max_ch) {
  3214. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3215. return -GSI_STATUS_INVALID_PARAMS;
  3216. }
  3217. ctx = &gsi_ctx->chan[chan_hdl];
  3218. if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  3219. GSIERR("bad state %d\n", ctx->state);
  3220. return -GSI_STATUS_UNSUPPORTED_OP;
  3221. }
  3222. /*In GSI_VER_2_2 version deallocation channel not supported*/
  3223. if (gsi_ctx->per.ver != GSI_VER_2_2) {
  3224. mutex_lock(&gsi_ctx->mlock);
  3225. reinit_completion(&ctx->compl);
  3226. gsi_ctx->ch_dbg[chan_hdl].ch_de_alloc++;
  3227. ch_cmd.chid = chan_hdl;
  3228. ch_cmd.opcode = op;
  3229. gsihal_write_reg_n_fields(GSI_EE_n_GSI_CH_CMD,
  3230. gsi_ctx->per.ee, &ch_cmd);
  3231. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  3232. if (res == 0) {
  3233. GSIERR("chan_hdl=%lu timed out\n", chan_hdl);
  3234. mutex_unlock(&gsi_ctx->mlock);
  3235. return -GSI_STATUS_TIMED_OUT;
  3236. }
  3237. if (ctx->state != GSI_CHAN_STATE_NOT_ALLOCATED) {
  3238. GSIERR("chan_hdl=%lu unexpected state=%u\n", chan_hdl,
  3239. ctx->state);
  3240. /* Hardware returned incorrect value */
  3241. GSI_ASSERT();
  3242. }
  3243. mutex_unlock(&gsi_ctx->mlock);
  3244. } else {
  3245. mutex_lock(&gsi_ctx->mlock);
  3246. GSIDBG("In GSI_VER_2_2 channel deallocation not supported\n");
  3247. ctx->state = GSI_CHAN_STATE_NOT_ALLOCATED;
  3248. GSIDBG("chan_hdl=%lu Channel state = %u\n", chan_hdl,
  3249. ctx->state);
  3250. mutex_unlock(&gsi_ctx->mlock);
  3251. }
  3252. devm_kfree(gsi_ctx->dev, ctx->user_data);
  3253. ctx->allocated = false;
  3254. if (ctx->evtr && (ctx->props.prot != GSI_CHAN_PROT_GCI)) {
  3255. atomic_dec(&ctx->evtr->chan_ref_cnt);
  3256. ctx->evtr->num_of_chan_allocated--;
  3257. }
  3258. atomic_dec(&gsi_ctx->num_chan);
  3259. if (ctx->props.prot == GSI_CHAN_PROT_GCI) {
  3260. gsi_ctx->coal_info.ch_id = GSI_CHAN_MAX;
  3261. gsi_ctx->coal_info.evchid = GSI_EVT_RING_MAX;
  3262. }
  3263. return GSI_STATUS_SUCCESS;
  3264. }
  3265. EXPORT_SYMBOL(gsi_dealloc_channel);
  3266. void gsi_update_ch_dp_stats(struct gsi_chan_ctx *ctx, uint16_t used)
  3267. {
  3268. unsigned long now = jiffies_to_msecs(jiffies);
  3269. unsigned long elapsed;
  3270. if (used == 0) {
  3271. elapsed = now - ctx->stats.dp.last_timestamp;
  3272. if (ctx->stats.dp.empty_time < elapsed)
  3273. ctx->stats.dp.empty_time = elapsed;
  3274. }
  3275. if (used <= ctx->props.max_re_expected / 3)
  3276. ++ctx->stats.dp.ch_below_lo;
  3277. else if (used <= 2 * ctx->props.max_re_expected / 3)
  3278. ++ctx->stats.dp.ch_below_hi;
  3279. else
  3280. ++ctx->stats.dp.ch_above_hi;
  3281. ctx->stats.dp.last_timestamp = now;
  3282. }
  3283. static void __gsi_query_channel_free_re(struct gsi_chan_ctx *ctx,
  3284. uint16_t *num_free_re)
  3285. {
  3286. uint16_t start;
  3287. uint16_t end;
  3288. uint64_t rp;
  3289. int ee = gsi_ctx->per.ee;
  3290. uint16_t used;
  3291. WARN_ON(ctx->props.prot != GSI_CHAN_PROT_GPI);
  3292. if (!ctx->evtr) {
  3293. rp = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_4,
  3294. ee, ctx->props.ch_id);
  3295. rp |= ctx->ring.rp & GSI_MSB_MASK;
  3296. ctx->ring.rp = rp;
  3297. } else {
  3298. rp = ctx->ring.rp_local;
  3299. }
  3300. start = gsi_find_idx_from_addr(&ctx->ring, rp);
  3301. end = gsi_find_idx_from_addr(&ctx->ring, ctx->ring.wp_local);
  3302. if (end >= start)
  3303. used = end - start;
  3304. else
  3305. used = ctx->ring.max_num_elem + 1 - (start - end);
  3306. *num_free_re = ctx->ring.max_num_elem - used;
  3307. }
  3308. int gsi_query_channel_info(unsigned long chan_hdl,
  3309. struct gsi_chan_info *info)
  3310. {
  3311. struct gsi_chan_ctx *ctx;
  3312. spinlock_t *slock;
  3313. unsigned long flags;
  3314. uint64_t rp;
  3315. uint64_t wp;
  3316. int ee;
  3317. if (!gsi_ctx) {
  3318. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3319. return -GSI_STATUS_NODEV;
  3320. }
  3321. if (chan_hdl >= gsi_ctx->max_ch || !info) {
  3322. GSIERR("bad params chan_hdl=%lu info=%pK\n", chan_hdl, info);
  3323. return -GSI_STATUS_INVALID_PARAMS;
  3324. }
  3325. ctx = &gsi_ctx->chan[chan_hdl];
  3326. if (ctx->evtr) {
  3327. slock = &ctx->evtr->ring.slock;
  3328. info->evt_valid = true;
  3329. } else {
  3330. slock = &ctx->ring.slock;
  3331. info->evt_valid = false;
  3332. }
  3333. spin_lock_irqsave(slock, flags);
  3334. ee = gsi_ctx->per.ee;
  3335. rp = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_4,
  3336. ee, ctx->props.ch_id);
  3337. rp |= ((uint64_t)gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_5,
  3338. ee, ctx->props.ch_id)) << 32;
  3339. ctx->ring.rp = rp;
  3340. info->rp = rp;
  3341. wp = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_6,
  3342. ee, ctx->props.ch_id);
  3343. wp |= ((uint64_t)gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_7,
  3344. ee, ctx->props.ch_id)) << 32;
  3345. ctx->ring.wp = wp;
  3346. info->wp = wp;
  3347. if (info->evt_valid) {
  3348. rp = gsihal_read_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_4,
  3349. ee, ctx->evtr->id);
  3350. rp |= ((uint64_t)gsihal_read_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_5,
  3351. ee, ctx->evtr->id)) << 32;
  3352. info->evt_rp = rp;
  3353. wp = gsihal_read_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_6,
  3354. ee, ctx->evtr->id);
  3355. wp |= ((uint64_t)gsihal_read_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_7,
  3356. ee, ctx->evtr->id)) << 32;
  3357. info->evt_wp = wp;
  3358. }
  3359. spin_unlock_irqrestore(slock, flags);
  3360. GSIDBG("ch=%lu RP=0x%llx WP=0x%llx ev_valid=%d ERP=0x%llx EWP=0x%llx\n",
  3361. chan_hdl, info->rp, info->wp,
  3362. info->evt_valid, info->evt_rp, info->evt_wp);
  3363. return GSI_STATUS_SUCCESS;
  3364. }
  3365. EXPORT_SYMBOL(gsi_query_channel_info);
  3366. int gsi_is_channel_empty(unsigned long chan_hdl, bool *is_empty)
  3367. {
  3368. struct gsi_chan_ctx *ctx;
  3369. struct gsi_evt_ctx *ev_ctx;
  3370. spinlock_t *slock;
  3371. unsigned long flags;
  3372. uint64_t rp;
  3373. uint64_t wp;
  3374. uint64_t rp_local;
  3375. int ee;
  3376. if (!gsi_ctx) {
  3377. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3378. return -GSI_STATUS_NODEV;
  3379. }
  3380. if (chan_hdl >= gsi_ctx->max_ch || !is_empty) {
  3381. GSIERR("bad params chan_hdl=%lu is_empty=%pK\n",
  3382. chan_hdl, is_empty);
  3383. return -GSI_STATUS_INVALID_PARAMS;
  3384. }
  3385. ctx = &gsi_ctx->chan[chan_hdl];
  3386. ee = gsi_ctx->per.ee;
  3387. if (ctx->props.prot != GSI_CHAN_PROT_GPI &&
  3388. ctx->props.prot != GSI_CHAN_PROT_GCI) {
  3389. GSIERR("op not supported for protocol %u\n", ctx->props.prot);
  3390. return -GSI_STATUS_UNSUPPORTED_OP;
  3391. }
  3392. if (ctx->evtr)
  3393. slock = &ctx->evtr->ring.slock;
  3394. else
  3395. slock = &ctx->ring.slock;
  3396. spin_lock_irqsave(slock, flags);
  3397. if (ctx->props.dir == GSI_CHAN_DIR_FROM_GSI && ctx->evtr) {
  3398. ev_ctx = &gsi_ctx->evtr[ctx->evtr->id];
  3399. /* Read the event ring rp from DDR to avoid mismatch */
  3400. rp = ev_ctx->props.gsi_read_event_ring_rp(&ev_ctx->props,
  3401. ev_ctx->id, ee);
  3402. rp |= ctx->evtr->ring.rp & GSI_MSB_MASK;
  3403. ctx->evtr->ring.rp = rp;
  3404. wp = gsihal_read_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_6,
  3405. ee, ctx->evtr->id);
  3406. wp |= ctx->evtr->ring.wp & GSI_MSB_MASK;
  3407. ctx->evtr->ring.wp = wp;
  3408. rp_local = ctx->evtr->ring.rp_local;
  3409. } else {
  3410. rp = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_4,
  3411. ee, ctx->props.ch_id);
  3412. rp |= ctx->ring.rp & GSI_MSB_MASK;
  3413. ctx->ring.rp = rp;
  3414. wp = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_6,
  3415. ee, ctx->props.ch_id);
  3416. wp |= ctx->ring.wp & GSI_MSB_MASK;
  3417. ctx->ring.wp = wp;
  3418. rp_local = ctx->ring.rp_local;
  3419. }
  3420. if (ctx->props.dir == GSI_CHAN_DIR_FROM_GSI)
  3421. *is_empty = (rp_local == rp) ? true : false;
  3422. else
  3423. *is_empty = (wp == rp) ? true : false;
  3424. spin_unlock_irqrestore(slock, flags);
  3425. if (ctx->props.dir == GSI_CHAN_DIR_FROM_GSI && ctx->evtr)
  3426. GSIDBG("ch=%ld ev=%d RP=0x%llx WP=0x%llx RP_LOCAL=0x%llx\n",
  3427. chan_hdl, ctx->evtr->id, rp, wp, rp_local);
  3428. else
  3429. GSIDBG("ch=%lu RP=0x%llx WP=0x%llx RP_LOCAL=0x%llx\n",
  3430. chan_hdl, rp, wp, rp_local);
  3431. return GSI_STATUS_SUCCESS;
  3432. }
  3433. EXPORT_SYMBOL(gsi_is_channel_empty);
  3434. bool gsi_is_event_pending(unsigned long chan_hdl) {
  3435. struct gsi_chan_ctx *ctx;
  3436. uint64_t rp;
  3437. uint64_t rp_local;
  3438. int ee;
  3439. if (chan_hdl >= gsi_ctx->max_ch) {
  3440. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3441. return false;
  3442. }
  3443. ctx = &gsi_ctx->chan[chan_hdl];
  3444. ee = gsi_ctx->per.ee;
  3445. /* read only, updating will be handled in NAPI context if needed */
  3446. rp = ctx->evtr->props.gsi_read_event_ring_rp(
  3447. &ctx->evtr->props, ctx->evtr->id, ee);
  3448. rp |= ctx->evtr->ring.rp & GSI_MSB_MASK;
  3449. rp_local = ctx->evtr->ring.rp_local;
  3450. return rp != rp_local;
  3451. }
  3452. EXPORT_SYMBOL(gsi_is_event_pending);
  3453. int __gsi_get_gci_cookie(struct gsi_chan_ctx *ctx, uint16_t idx)
  3454. {
  3455. int i;
  3456. int end;
  3457. if (!ctx->user_data[idx].valid) {
  3458. ctx->user_data[idx].valid = true;
  3459. return idx;
  3460. }
  3461. /*
  3462. * at this point we need to find an "escape buffer" for the cookie
  3463. * as the userdata in this spot is in use. This happens if the TRE at
  3464. * idx is not completed yet and it is getting reused by a new TRE.
  3465. */
  3466. ctx->stats.userdata_in_use++;
  3467. end = ctx->ring.max_num_elem + 1;
  3468. for (i = 0; i < GSI_VEID_MAX; i++) {
  3469. if (!ctx->user_data[end + i].valid) {
  3470. ctx->user_data[end + i].valid = true;
  3471. return end + i;
  3472. }
  3473. }
  3474. /* Go over original userdata when escape buffer is full (costly) */
  3475. GSIDBG("escape buffer is full\n");
  3476. for (i = 0; i < end; i++) {
  3477. if (!ctx->user_data[i].valid) {
  3478. ctx->user_data[i].valid = true;
  3479. return i;
  3480. }
  3481. }
  3482. /* Everything is full (possibly a stall) */
  3483. GSIERR("both userdata array and escape buffer is full\n");
  3484. BUG();
  3485. return 0xFFFF;
  3486. }
  3487. int __gsi_populate_gci_tre(struct gsi_chan_ctx *ctx,
  3488. struct gsi_xfer_elem *xfer)
  3489. {
  3490. struct gsi_gci_tre gci_tre;
  3491. struct gsi_gci_tre *tre_gci_ptr;
  3492. uint16_t idx;
  3493. memset(&gci_tre, 0, sizeof(gci_tre));
  3494. if (xfer->addr & 0xFFFFFF0000000000) {
  3495. GSIERR("chan_hdl=%u add too large=%llx\n",
  3496. ctx->props.ch_id, xfer->addr);
  3497. return -EINVAL;
  3498. }
  3499. if (xfer->type != GSI_XFER_ELEM_DATA) {
  3500. GSIERR("chan_hdl=%u bad RE type=%u\n", ctx->props.ch_id,
  3501. xfer->type);
  3502. return -EINVAL;
  3503. }
  3504. idx = gsi_find_idx_from_addr(&ctx->ring, ctx->ring.wp_local);
  3505. tre_gci_ptr = (struct gsi_gci_tre *)(ctx->ring.base_va +
  3506. idx * ctx->ring.elem_sz);
  3507. gci_tre.buffer_ptr = xfer->addr;
  3508. gci_tre.buf_len = xfer->len;
  3509. gci_tre.re_type = GSI_RE_COAL;
  3510. gci_tre.cookie = __gsi_get_gci_cookie(ctx, idx);
  3511. if (gci_tre.cookie > (ctx->ring.max_num_elem + GSI_VEID_MAX))
  3512. return -EPERM;
  3513. /* write the TRE to ring */
  3514. *tre_gci_ptr = gci_tre;
  3515. ctx->user_data[gci_tre.cookie].p = xfer->xfer_user_data;
  3516. return 0;
  3517. }
  3518. int __gsi_populate_tre(struct gsi_chan_ctx *ctx,
  3519. struct gsi_xfer_elem *xfer)
  3520. {
  3521. struct gsi_tre tre;
  3522. struct gsi_tre *tre_ptr;
  3523. uint16_t idx;
  3524. memset(&tre, 0, sizeof(tre));
  3525. tre.buffer_ptr = xfer->addr;
  3526. tre.buf_len = xfer->len;
  3527. if (xfer->type == GSI_XFER_ELEM_DATA) {
  3528. tre.re_type = GSI_RE_XFER;
  3529. } else if (xfer->type == GSI_XFER_ELEM_IMME_CMD) {
  3530. tre.re_type = GSI_RE_IMMD_CMD;
  3531. } else if (xfer->type == GSI_XFER_ELEM_NOP) {
  3532. tre.re_type = GSI_RE_NOP;
  3533. } else {
  3534. GSIERR("chan_hdl=%u bad RE type=%u\n", ctx->props.ch_id,
  3535. xfer->type);
  3536. return -EINVAL;
  3537. }
  3538. tre.bei = (xfer->flags & GSI_XFER_FLAG_BEI) ? 1 : 0;
  3539. tre.ieot = (xfer->flags & GSI_XFER_FLAG_EOT) ? 1 : 0;
  3540. tre.ieob = (xfer->flags & GSI_XFER_FLAG_EOB) ? 1 : 0;
  3541. tre.chain = (xfer->flags & GSI_XFER_FLAG_CHAIN) ? 1 : 0;
  3542. if (unlikely(ctx->state == GSI_CHAN_STATE_NOT_ALLOCATED)) {
  3543. GSIERR("bad state %d\n", ctx->state);
  3544. return -GSI_STATUS_UNSUPPORTED_OP;
  3545. }
  3546. idx = gsi_find_idx_from_addr(&ctx->ring, ctx->ring.wp_local);
  3547. tre_ptr = (struct gsi_tre *)(ctx->ring.base_va +
  3548. idx * ctx->ring.elem_sz);
  3549. /* write the TRE to ring */
  3550. *tre_ptr = tre;
  3551. ctx->user_data[idx].valid = true;
  3552. ctx->user_data[idx].p = xfer->xfer_user_data;
  3553. return 0;
  3554. }
  3555. int gsi_queue_xfer(unsigned long chan_hdl, uint16_t num_xfers,
  3556. struct gsi_xfer_elem *xfer, bool ring_db)
  3557. {
  3558. struct gsi_chan_ctx *ctx;
  3559. uint16_t free;
  3560. uint64_t wp_rollback;
  3561. int i;
  3562. spinlock_t *slock;
  3563. unsigned long flags;
  3564. if (!gsi_ctx) {
  3565. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3566. return -GSI_STATUS_NODEV;
  3567. }
  3568. if (chan_hdl >= gsi_ctx->max_ch || (num_xfers && !xfer)) {
  3569. GSIERR("bad params chan_hdl=%lu num_xfers=%u xfer=%pK\n",
  3570. chan_hdl, num_xfers, xfer);
  3571. return -GSI_STATUS_INVALID_PARAMS;
  3572. }
  3573. if (unlikely(gsi_ctx->chan[chan_hdl].state
  3574. == GSI_CHAN_STATE_NOT_ALLOCATED)) {
  3575. GSIERR("bad state %d\n",
  3576. gsi_ctx->chan[chan_hdl].state);
  3577. return -GSI_STATUS_UNSUPPORTED_OP;
  3578. }
  3579. ctx = &gsi_ctx->chan[chan_hdl];
  3580. if (ctx->props.prot != GSI_CHAN_PROT_GPI &&
  3581. ctx->props.prot != GSI_CHAN_PROT_GCI) {
  3582. GSIERR("op not supported for protocol %u\n", ctx->props.prot);
  3583. return -GSI_STATUS_UNSUPPORTED_OP;
  3584. }
  3585. if (ctx->evtr)
  3586. slock = &ctx->evtr->ring.slock;
  3587. else
  3588. slock = &ctx->ring.slock;
  3589. spin_lock_irqsave(slock, flags);
  3590. /* allow only ring doorbell */
  3591. if (!num_xfers)
  3592. goto ring_doorbell;
  3593. /*
  3594. * for GCI channels the responsibility is on the caller to make sure
  3595. * there is enough room in the TRE.
  3596. */
  3597. if (ctx->props.prot != GSI_CHAN_PROT_GCI) {
  3598. __gsi_query_channel_free_re(ctx, &free);
  3599. if (num_xfers > free) {
  3600. GSIERR("chan_hdl=%lu num_xfers=%u free=%u\n",
  3601. chan_hdl, num_xfers, free);
  3602. spin_unlock_irqrestore(slock, flags);
  3603. return -GSI_STATUS_RING_INSUFFICIENT_SPACE;
  3604. }
  3605. }
  3606. wp_rollback = ctx->ring.wp_local;
  3607. for (i = 0; i < num_xfers; i++) {
  3608. if (ctx->props.prot == GSI_CHAN_PROT_GCI) {
  3609. if (__gsi_populate_gci_tre(ctx, &xfer[i]))
  3610. break;
  3611. } else {
  3612. if (__gsi_populate_tre(ctx, &xfer[i]))
  3613. break;
  3614. }
  3615. gsi_incr_ring_wp(&ctx->ring);
  3616. }
  3617. if (i != num_xfers) {
  3618. /* reject all the xfers */
  3619. ctx->ring.wp_local = wp_rollback;
  3620. spin_unlock_irqrestore(slock, flags);
  3621. return -GSI_STATUS_INVALID_PARAMS;
  3622. }
  3623. ctx->stats.queued += num_xfers;
  3624. ring_doorbell:
  3625. if (ring_db) {
  3626. /* ensure TRE is set before ringing doorbell */
  3627. wmb();
  3628. gsi_ring_chan_doorbell(ctx);
  3629. }
  3630. spin_unlock_irqrestore(slock, flags);
  3631. return GSI_STATUS_SUCCESS;
  3632. }
  3633. EXPORT_SYMBOL(gsi_queue_xfer);
  3634. int gsi_start_xfer(unsigned long chan_hdl)
  3635. {
  3636. struct gsi_chan_ctx *ctx;
  3637. if (!gsi_ctx) {
  3638. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3639. return -GSI_STATUS_NODEV;
  3640. }
  3641. if (chan_hdl >= gsi_ctx->max_ch) {
  3642. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3643. return -GSI_STATUS_INVALID_PARAMS;
  3644. }
  3645. ctx = &gsi_ctx->chan[chan_hdl];
  3646. if (ctx->props.prot != GSI_CHAN_PROT_GPI &&
  3647. ctx->props.prot != GSI_CHAN_PROT_GCI) {
  3648. GSIERR("op not supported for protocol %u\n", ctx->props.prot);
  3649. return -GSI_STATUS_UNSUPPORTED_OP;
  3650. }
  3651. if (ctx->state == GSI_CHAN_STATE_NOT_ALLOCATED) {
  3652. GSIERR("bad state %d\n", ctx->state);
  3653. return -GSI_STATUS_UNSUPPORTED_OP;
  3654. }
  3655. if (ctx->ring.wp == ctx->ring.wp_local)
  3656. return GSI_STATUS_SUCCESS;
  3657. gsi_ring_chan_doorbell(ctx);
  3658. return GSI_STATUS_SUCCESS;
  3659. };
  3660. EXPORT_SYMBOL(gsi_start_xfer);
  3661. int gsi_poll_channel(unsigned long chan_hdl,
  3662. struct gsi_chan_xfer_notify *notify)
  3663. {
  3664. int unused_var;
  3665. return gsi_poll_n_channel(chan_hdl, notify, 1, &unused_var);
  3666. }
  3667. EXPORT_SYMBOL(gsi_poll_channel);
  3668. int gsi_poll_n_channel(unsigned long chan_hdl,
  3669. struct gsi_chan_xfer_notify *notify,
  3670. int expected_num, int *actual_num)
  3671. {
  3672. struct gsi_chan_ctx *ctx;
  3673. uint64_t rp;
  3674. int ee;
  3675. int i;
  3676. unsigned long flags;
  3677. if (!gsi_ctx) {
  3678. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3679. return -GSI_STATUS_NODEV;
  3680. }
  3681. if (chan_hdl >= gsi_ctx->max_ch || !notify ||
  3682. !actual_num || expected_num <= 0) {
  3683. GSIERR("bad params chan_hdl=%lu notify=%pK\n",
  3684. chan_hdl, notify);
  3685. GSIERR("actual_num=%pK expected_num=%d\n",
  3686. actual_num, expected_num);
  3687. return -GSI_STATUS_INVALID_PARAMS;
  3688. }
  3689. ctx = &gsi_ctx->chan[chan_hdl];
  3690. ee = gsi_ctx->per.ee;
  3691. if (ctx->props.prot != GSI_CHAN_PROT_GPI &&
  3692. ctx->props.prot != GSI_CHAN_PROT_GCI) {
  3693. GSIERR("op not supported for protocol %u\n", ctx->props.prot);
  3694. return -GSI_STATUS_UNSUPPORTED_OP;
  3695. }
  3696. /* Before going to poll packet make sure it was in allocated state */
  3697. if (unlikely(ctx->state == GSI_CHAN_STATE_NOT_ALLOCATED)) {
  3698. GSIERR("bad state %d\n", ctx->state);
  3699. return -GSI_STATUS_UNSUPPORTED_OP;
  3700. }
  3701. if (!ctx->evtr) {
  3702. GSIERR("no event ring associated chan_hdl=%lu\n", chan_hdl);
  3703. return -GSI_STATUS_UNSUPPORTED_OP;
  3704. }
  3705. spin_lock_irqsave(&ctx->evtr->ring.slock, flags);
  3706. if (ctx->evtr->ring.rp == ctx->evtr->ring.rp_local) {
  3707. /* update rp to see of we have anything new to process */
  3708. rp = ctx->evtr->props.gsi_read_event_ring_rp(
  3709. &ctx->evtr->props, ctx->evtr->id, ee);
  3710. rp |= ctx->ring.rp & GSI_MSB_MASK;
  3711. ctx->evtr->ring.rp = rp;
  3712. /* read gsi event ring rp again if last read is empty */
  3713. if (rp == ctx->evtr->ring.rp_local) {
  3714. /* event ring is empty */
  3715. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  3716. gsihal_write_reg_nk(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_k,
  3717. ee, gsihal_get_ch_reg_idx(ctx->evtr->id),
  3718. gsihal_get_ch_reg_mask(ctx->evtr->id));
  3719. }
  3720. else {
  3721. gsihal_write_reg_n(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR,
  3722. ee, 1 << ctx->evtr->id);
  3723. }
  3724. /* do another read to close a small window */
  3725. __iowmb();
  3726. rp = ctx->evtr->props.gsi_read_event_ring_rp(
  3727. &ctx->evtr->props, ctx->evtr->id, ee);
  3728. rp |= ctx->ring.rp & GSI_MSB_MASK;
  3729. ctx->evtr->ring.rp = rp;
  3730. if (rp == ctx->evtr->ring.rp_local) {
  3731. spin_unlock_irqrestore(
  3732. &ctx->evtr->ring.slock,
  3733. flags);
  3734. ctx->stats.poll_empty++;
  3735. return GSI_STATUS_POLL_EMPTY;
  3736. }
  3737. }
  3738. }
  3739. *actual_num = gsi_get_complete_num(&ctx->evtr->ring,
  3740. ctx->evtr->ring.rp_local, ctx->evtr->ring.rp);
  3741. if (*actual_num > expected_num)
  3742. *actual_num = expected_num;
  3743. for (i = 0; i < *actual_num; i++)
  3744. gsi_process_evt_re(ctx->evtr, notify + i, false);
  3745. spin_unlock_irqrestore(&ctx->evtr->ring.slock, flags);
  3746. ctx->stats.poll_ok++;
  3747. return GSI_STATUS_SUCCESS;
  3748. }
  3749. EXPORT_SYMBOL(gsi_poll_n_channel);
  3750. int gsi_config_channel_mode(unsigned long chan_hdl, enum gsi_chan_mode mode)
  3751. {
  3752. struct gsi_chan_ctx *ctx, *coal_ctx;
  3753. enum gsi_chan_mode curr;
  3754. unsigned long flags;
  3755. enum gsi_chan_mode chan_mode;
  3756. int i;
  3757. if (!gsi_ctx) {
  3758. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3759. return -GSI_STATUS_NODEV;
  3760. }
  3761. if (chan_hdl >= gsi_ctx->max_ch) {
  3762. GSIERR("bad params chan_hdl=%lu mode=%u\n", chan_hdl, mode);
  3763. return -GSI_STATUS_INVALID_PARAMS;
  3764. }
  3765. ctx = &gsi_ctx->chan[chan_hdl];
  3766. if (ctx->props.prot != GSI_CHAN_PROT_GPI &&
  3767. ctx->props.prot != GSI_CHAN_PROT_GCI) {
  3768. GSIERR("op not supported for protocol %u\n", ctx->props.prot);
  3769. return -GSI_STATUS_UNSUPPORTED_OP;
  3770. }
  3771. if (!ctx->evtr) {
  3772. GSIERR("cannot configure mode on chan_hdl=%lu\n",
  3773. chan_hdl);
  3774. return -GSI_STATUS_UNSUPPORTED_OP;
  3775. }
  3776. if (atomic_read(&ctx->poll_mode))
  3777. curr = GSI_CHAN_MODE_POLL;
  3778. else
  3779. curr = GSI_CHAN_MODE_CALLBACK;
  3780. if (mode == curr) {
  3781. GSIDBG("already in requested mode %u chan_hdl=%lu\n",
  3782. curr, chan_hdl);
  3783. return -GSI_STATUS_UNSUPPORTED_OP;
  3784. }
  3785. spin_lock_irqsave(&gsi_ctx->slock, flags);
  3786. if (curr == GSI_CHAN_MODE_CALLBACK &&
  3787. mode == GSI_CHAN_MODE_POLL) {
  3788. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  3789. if (ctx->evtr->props.intr != GSI_INTR_MSI) {
  3790. __gsi_config_ieob_irq_k(gsi_ctx->per.ee,
  3791. gsihal_get_ch_reg_idx(ctx->evtr->id),
  3792. gsihal_get_ch_reg_mask(ctx->evtr->id),
  3793. 0);
  3794. }
  3795. }
  3796. else {
  3797. __gsi_config_ieob_irq(gsi_ctx->per.ee, 1 << ctx->evtr->id, 0);
  3798. }
  3799. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  3800. gsihal_write_reg_nk(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_k,
  3801. gsi_ctx->per.ee, gsihal_get_ch_reg_idx(ctx->evtr->id),
  3802. gsihal_get_ch_reg_mask(ctx->evtr->id));
  3803. }
  3804. else {
  3805. gsihal_write_reg_n(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR,
  3806. gsi_ctx->per.ee, 1 << ctx->evtr->id);
  3807. }
  3808. atomic_set(&ctx->poll_mode, mode);
  3809. for(i = 0; i < ctx->evtr->num_of_chan_allocated; i++) {
  3810. atomic_set(&ctx->evtr->chan[i]->poll_mode, mode);
  3811. }
  3812. if ((ctx->props.prot == GSI_CHAN_PROT_GCI) && *ctx->evtr->chan) {
  3813. atomic_set(&ctx->evtr->chan[0]->poll_mode, mode);
  3814. } else if (gsi_ctx->coal_info.evchid == ctx->evtr->id) {
  3815. coal_ctx = &gsi_ctx->chan[gsi_ctx->coal_info.ch_id];
  3816. if (coal_ctx != NULL)
  3817. atomic_set(&coal_ctx->poll_mode, mode);
  3818. }
  3819. GSIDBG("set gsi_ctx evtr_id %d to %d mode\n",
  3820. ctx->evtr->id, mode);
  3821. ctx->stats.callback_to_poll++;
  3822. }
  3823. if (curr == GSI_CHAN_MODE_POLL &&
  3824. mode == GSI_CHAN_MODE_CALLBACK) {
  3825. atomic_set(&ctx->poll_mode, mode);
  3826. for(i = 0; i < ctx->evtr->num_of_chan_allocated; i++) {
  3827. atomic_set(&ctx->evtr->chan[i]->poll_mode, mode);
  3828. }
  3829. if ((ctx->props.prot == GSI_CHAN_PROT_GCI) && *ctx->evtr->chan) {
  3830. atomic_set(&ctx->evtr->chan[0]->poll_mode, mode);
  3831. } else if (gsi_ctx->coal_info.evchid == ctx->evtr->id) {
  3832. coal_ctx = &gsi_ctx->chan[gsi_ctx->coal_info.ch_id];
  3833. if (coal_ctx != NULL)
  3834. atomic_set(&coal_ctx->poll_mode, mode);
  3835. }
  3836. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  3837. if (ctx->evtr->props.intr != GSI_INTR_MSI) {
  3838. __gsi_config_ieob_irq_k(gsi_ctx->per.ee,
  3839. gsihal_get_ch_reg_idx(ctx->evtr->id),
  3840. gsihal_get_ch_reg_mask(ctx->evtr->id),
  3841. ~0);
  3842. }
  3843. }
  3844. else {
  3845. __gsi_config_ieob_irq(gsi_ctx->per.ee, 1 << ctx->evtr->id, ~0);
  3846. }
  3847. GSIDBG("set gsi_ctx evtr_id %d to %d mode\n",
  3848. ctx->evtr->id, mode);
  3849. /*
  3850. * In GSI 2.2 and 2.5 there is a limitation that can lead
  3851. * to losing an interrupt. For these versions an
  3852. * explicit check is needed after enabling the interrupt
  3853. */
  3854. if ((gsi_ctx->per.ver == GSI_VER_2_2 ||
  3855. gsi_ctx->per.ver == GSI_VER_2_5) &&
  3856. !gsi_ctx->per.skip_ieob_mask_wa) {
  3857. u32 src = gsihal_read_reg_n(
  3858. GSI_EE_n_CNTXT_SRC_IEOB_IRQ,
  3859. gsi_ctx->per.ee);
  3860. if (src & (1 << ctx->evtr->id)) {
  3861. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  3862. __gsi_config_ieob_irq_k(gsi_ctx->per.ee,
  3863. gsihal_get_ch_reg_idx(ctx->evtr->id),
  3864. gsihal_get_ch_reg_mask(ctx->evtr->id),
  3865. 0);
  3866. gsihal_write_reg_nk(
  3867. GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_k,
  3868. gsi_ctx->per.ee,
  3869. gsihal_get_ch_reg_idx(ctx->evtr->id),
  3870. gsihal_get_ch_reg_mask(ctx->evtr->id));
  3871. }
  3872. else {
  3873. __gsi_config_ieob_irq(gsi_ctx->per.ee, 1 <<
  3874. ctx->evtr->id, 0);
  3875. gsihal_write_reg_n(
  3876. GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR,
  3877. gsi_ctx->per.ee,
  3878. 1 << ctx->evtr->id);
  3879. }
  3880. spin_unlock_irqrestore(&gsi_ctx->slock, flags);
  3881. spin_lock_irqsave(&ctx->evtr->ring.slock,
  3882. flags);
  3883. chan_mode = atomic_xchg(&ctx->poll_mode,
  3884. GSI_CHAN_MODE_POLL);
  3885. spin_unlock_irqrestore(
  3886. &ctx->evtr->ring.slock, flags);
  3887. ctx->stats.poll_pending_irq++;
  3888. GSIDBG("IEOB WA pnd cnt = %ld prvmode = %d\n",
  3889. ctx->stats.poll_pending_irq,
  3890. chan_mode);
  3891. if (chan_mode == GSI_CHAN_MODE_POLL)
  3892. return GSI_STATUS_SUCCESS;
  3893. else
  3894. return -GSI_STATUS_PENDING_IRQ;
  3895. }
  3896. }
  3897. ctx->stats.poll_to_callback++;
  3898. }
  3899. spin_unlock_irqrestore(&gsi_ctx->slock, flags);
  3900. return GSI_STATUS_SUCCESS;
  3901. }
  3902. EXPORT_SYMBOL(gsi_config_channel_mode);
  3903. int gsi_get_channel_cfg(unsigned long chan_hdl, struct gsi_chan_props *props,
  3904. union gsi_channel_scratch *scr)
  3905. {
  3906. struct gsi_chan_ctx *ctx;
  3907. if (!gsi_ctx) {
  3908. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3909. return -GSI_STATUS_NODEV;
  3910. }
  3911. if (!props || !scr) {
  3912. GSIERR("bad params props=%pK scr=%pK\n", props, scr);
  3913. return -GSI_STATUS_INVALID_PARAMS;
  3914. }
  3915. if (chan_hdl >= gsi_ctx->max_ch) {
  3916. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3917. return -GSI_STATUS_INVALID_PARAMS;
  3918. }
  3919. ctx = &gsi_ctx->chan[chan_hdl];
  3920. if (ctx->state == GSI_CHAN_STATE_NOT_ALLOCATED) {
  3921. GSIERR("bad state %d\n", ctx->state);
  3922. return -GSI_STATUS_UNSUPPORTED_OP;
  3923. }
  3924. mutex_lock(&ctx->mlock);
  3925. *props = ctx->props;
  3926. *scr = ctx->scratch;
  3927. mutex_unlock(&ctx->mlock);
  3928. return GSI_STATUS_SUCCESS;
  3929. }
  3930. EXPORT_SYMBOL(gsi_get_channel_cfg);
  3931. int gsi_set_channel_cfg(unsigned long chan_hdl, struct gsi_chan_props *props,
  3932. union gsi_channel_scratch *scr)
  3933. {
  3934. struct gsi_chan_ctx *ctx;
  3935. if (!gsi_ctx) {
  3936. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3937. return -GSI_STATUS_NODEV;
  3938. }
  3939. if (!props || gsi_validate_channel_props(props)) {
  3940. GSIERR("bad params props=%pK\n", props);
  3941. return -GSI_STATUS_INVALID_PARAMS;
  3942. }
  3943. if (chan_hdl >= gsi_ctx->max_ch) {
  3944. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3945. return -GSI_STATUS_INVALID_PARAMS;
  3946. }
  3947. ctx = &gsi_ctx->chan[chan_hdl];
  3948. if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  3949. GSIERR("bad state %d\n", ctx->state);
  3950. return -GSI_STATUS_UNSUPPORTED_OP;
  3951. }
  3952. if (ctx->props.ch_id != props->ch_id ||
  3953. ctx->props.evt_ring_hdl != props->evt_ring_hdl) {
  3954. GSIERR("changing immutable fields not supported\n");
  3955. return -GSI_STATUS_UNSUPPORTED_OP;
  3956. }
  3957. mutex_lock(&ctx->mlock);
  3958. ctx->props = *props;
  3959. if (scr)
  3960. ctx->scratch = *scr;
  3961. gsi_program_chan_ctx(&ctx->props, gsi_ctx->per.ee,
  3962. ctx->evtr ? ctx->evtr->id : GSI_NO_EVT_ERINDEX);
  3963. gsi_init_chan_ring(&ctx->props, &ctx->ring);
  3964. /* restore scratch */
  3965. __gsi_write_channel_scratch(chan_hdl, ctx->scratch);
  3966. mutex_unlock(&ctx->mlock);
  3967. return GSI_STATUS_SUCCESS;
  3968. }
  3969. EXPORT_SYMBOL(gsi_set_channel_cfg);
  3970. static void gsi_configure_ieps(enum gsi_ver ver)
  3971. {
  3972. gsihal_write_reg(GSI_GSI_IRAM_PTR_CH_CMD, 1);
  3973. gsihal_write_reg(GSI_GSI_IRAM_PTR_CH_DB, 2);
  3974. gsihal_write_reg(GSI_GSI_IRAM_PTR_CH_DIS_COMP, 3);
  3975. gsihal_write_reg(GSI_GSI_IRAM_PTR_CH_EMPTY, 4);
  3976. gsihal_write_reg(GSI_GSI_IRAM_PTR_EE_GENERIC_CMD, 5);
  3977. gsihal_write_reg(GSI_GSI_IRAM_PTR_EVENT_GEN_COMP, 6);
  3978. gsihal_write_reg(GSI_GSI_IRAM_PTR_INT_MOD_STOPPED, 7);
  3979. gsihal_write_reg(GSI_GSI_IRAM_PTR_PERIPH_IF_TLV_IN_0, 8);
  3980. gsihal_write_reg(GSI_GSI_IRAM_PTR_PERIPH_IF_TLV_IN_2, 9);
  3981. gsihal_write_reg(GSI_GSI_IRAM_PTR_PERIPH_IF_TLV_IN_1, 10);
  3982. gsihal_write_reg(GSI_GSI_IRAM_PTR_NEW_RE, 11);
  3983. gsihal_write_reg(GSI_GSI_IRAM_PTR_READ_ENG_COMP, 12);
  3984. gsihal_write_reg(GSI_GSI_IRAM_PTR_TIMER_EXPIRED, 13);
  3985. gsihal_write_reg(GSI_GSI_IRAM_PTR_EV_DB, 14);
  3986. gsihal_write_reg(GSI_GSI_IRAM_PTR_UC_GP_INT, 15);
  3987. gsihal_write_reg(GSI_GSI_IRAM_PTR_WRITE_ENG_COMP, 16);
  3988. if (ver >= GSI_VER_2_5)
  3989. gsihal_write_reg(
  3990. GSI_GSI_IRAM_PTR_TLV_CH_NOT_FULL,
  3991. 17);
  3992. if (ver >= GSI_VER_2_11)
  3993. gsihal_write_reg(
  3994. GSI_GSI_IRAM_PTR_MSI_DB,
  3995. 18);
  3996. if (ver >= GSI_VER_3_0)
  3997. gsihal_write_reg(
  3998. GSI_GSI_IRAM_PTR_INT_NOTIFY_MCS,
  3999. 19);
  4000. }
  4001. static void gsi_configure_bck_prs_matrix(void)
  4002. {
  4003. /*
  4004. * For now, these are default values. In the future, GSI FW image will
  4005. * produce optimized back-pressure values based on the FW image.
  4006. */
  4007. gsihal_write_reg(GSI_IC_DISABLE_CHNL_BCK_PRS_LSB, 0xfffffffe);
  4008. gsihal_write_reg(GSI_IC_DISABLE_CHNL_BCK_PRS_MSB, 0xffffffff);
  4009. gsihal_write_reg(GSI_IC_GEN_EVNT_BCK_PRS_LSB, 0xffffffbf);
  4010. gsihal_write_reg(GSI_IC_GEN_EVNT_BCK_PRS_MSB, 0xffffffff);
  4011. gsihal_write_reg(GSI_IC_GEN_INT_BCK_PRS_LSB, 0xffffefff);
  4012. gsihal_write_reg(GSI_IC_GEN_INT_BCK_PRS_MSB, 0xffffffff);
  4013. gsihal_write_reg(GSI_IC_STOP_INT_MOD_BCK_PRS_LSB, 0xffffefff);
  4014. gsihal_write_reg(GSI_IC_STOP_INT_MOD_BCK_PRS_MSB, 0xffffffff);
  4015. gsihal_write_reg(GSI_IC_PROCESS_DESC_BCK_PRS_LSB, 0x00000000);
  4016. gsihal_write_reg(GSI_IC_PROCESS_DESC_BCK_PRS_MSB, 0x00000000);
  4017. gsihal_write_reg(GSI_IC_TLV_STOP_BCK_PRS_LSB, 0xf9ffffff);
  4018. gsihal_write_reg(GSI_IC_TLV_STOP_BCK_PRS_MSB, 0xffffffff);
  4019. gsihal_write_reg(GSI_IC_TLV_RESET_BCK_PRS_LSB, 0xf9ffffff);
  4020. gsihal_write_reg(GSI_IC_TLV_RESET_BCK_PRS_MSB, 0xffffffff);
  4021. gsihal_write_reg(GSI_IC_RGSTR_TIMER_BCK_PRS_LSB, 0xffffffff);
  4022. gsihal_write_reg(GSI_IC_RGSTR_TIMER_BCK_PRS_MSB, 0xfffffffe);
  4023. gsihal_write_reg(GSI_IC_READ_BCK_PRS_LSB, 0xffffffff);
  4024. gsihal_write_reg(GSI_IC_READ_BCK_PRS_MSB, 0xffffefff);
  4025. gsihal_write_reg(GSI_IC_WRITE_BCK_PRS_LSB, 0xffffffff);
  4026. gsihal_write_reg(GSI_IC_WRITE_BCK_PRS_MSB, 0xffffdfff);
  4027. gsihal_write_reg(GSI_IC_UCONTROLLER_GPR_BCK_PRS_LSB, 0xffffffff);
  4028. gsihal_write_reg(GSI_IC_UCONTROLLER_GPR_BCK_PRS_MSB, 0xff03ffff);
  4029. }
  4030. int gsi_configure_regs(phys_addr_t per_base_addr, enum gsi_ver ver)
  4031. {
  4032. if (!gsi_ctx) {
  4033. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4034. return -GSI_STATUS_NODEV;
  4035. }
  4036. if (!gsi_ctx->base) {
  4037. GSIERR("access to GSI HW has not been mapped\n");
  4038. return -GSI_STATUS_INVALID_PARAMS;
  4039. }
  4040. if (ver <= GSI_VER_ERR || ver >= GSI_VER_MAX) {
  4041. GSIERR("Incorrect version %d\n", ver);
  4042. return -GSI_STATUS_ERROR;
  4043. }
  4044. gsihal_write_reg(GSI_GSI_PERIPH_BASE_ADDR_MSB, 0);
  4045. gsihal_write_reg(GSI_GSI_PERIPH_BASE_ADDR_LSB, per_base_addr);
  4046. gsi_configure_bck_prs_matrix();
  4047. gsi_configure_ieps(ver);
  4048. return 0;
  4049. }
  4050. EXPORT_SYMBOL(gsi_configure_regs);
  4051. int gsi_enable_fw(phys_addr_t gsi_base_addr, u32 gsi_size, enum gsi_ver ver)
  4052. {
  4053. struct gsihal_reg_gsi_cfg gsi_cfg;
  4054. if (ver <= GSI_VER_ERR || ver >= GSI_VER_MAX) {
  4055. GSIERR("Incorrect version %d\n", ver);
  4056. return -GSI_STATUS_ERROR;
  4057. }
  4058. /* Enable the MCS and set to x2 clocks */
  4059. gsi_cfg.gsi_enable = 1;
  4060. gsi_cfg.double_mcs_clk_freq = 1;
  4061. gsi_cfg.uc_is_mcs = 0;
  4062. gsi_cfg.gsi_pwr_clps = 0;
  4063. gsi_cfg.bp_mtrix_disable = 0;
  4064. if (ver >= GSI_VER_1_2) {
  4065. gsihal_write_reg(GSI_GSI_MCS_CFG, 1);
  4066. gsi_cfg.mcs_enable = 0;
  4067. } else {
  4068. gsi_cfg.mcs_enable = 1;
  4069. }
  4070. /* GSI frequency is peripheral frequency divided by 3 (2+1) */
  4071. if (ver >= GSI_VER_2_5)
  4072. gsi_cfg.sleep_clk_div = 2;
  4073. gsihal_write_reg_fields(GSI_GSI_CFG, &gsi_cfg);
  4074. return 0;
  4075. }
  4076. EXPORT_SYMBOL(gsi_enable_fw);
  4077. void gsi_get_inst_ram_offset_and_size(unsigned long *base_offset,
  4078. unsigned long *size, enum gsi_ver ver)
  4079. {
  4080. if (!gsi_ctx) {
  4081. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4082. return;
  4083. }
  4084. if (size)
  4085. *size = gsihal_get_inst_ram_size();
  4086. if (base_offset) {
  4087. *base_offset = gsihal_get_reg_n_ofst(GSI_GSI_INST_RAM_n, 0);
  4088. }
  4089. }
  4090. EXPORT_SYMBOL(gsi_get_inst_ram_offset_and_size);
  4091. /*
  4092. * Dumping the Debug registers for halt issue debugging.
  4093. */
  4094. static void gsi_dump_halt_debug_reg(unsigned int chan_idx, unsigned int ee)
  4095. {
  4096. struct gsihal_reg_ch_k_cntxt_0 ch_k_cntxt_0;
  4097. GSIERR("DEBUG_PC_FOR_DEBUG = 0x%x\n",
  4098. gsihal_read_reg(GSI_EE_n_GSI_DEBUG_PC_FOR_DEBUG));
  4099. GSIERR("GSI_DEBUG_BUSY_REG 0x%x\n",
  4100. gsihal_read_reg(GSI_EE_n_GSI_DEBUG_BUSY_REG));
  4101. GSIERR("GSI_EE_n_CNTXT_GLOB_IRQ_EN_OFFS = 0x%x\n",
  4102. gsihal_read_reg_n(GSI_EE_n_CNTXT_GLOB_IRQ_EN, gsi_ctx->per.ee));
  4103. GSIERR("GSI_EE_n_CNTXT_GLOB_IRQ_STTS_OFFS IRQ type = 0x%x\n",
  4104. gsihal_read_reg_n(GSI_EE_n_CNTXT_GLOB_IRQ_EN, gsi_ctx->per.ee));
  4105. GSIERR("GSI_EE_n_CNTXT_SCRATCH_0_OFFS = 0x%x\n",
  4106. gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0, gsi_ctx->per.ee));
  4107. if (gsi_ctx->per.ver >= GSI_VER_2_9)
  4108. GSIERR("GSI_EE_n_GSI_CH_k_SCRATCH_4 = 0x%x\n",
  4109. gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_4, ee, chan_idx));
  4110. gsihal_read_reg_nk_fields(GSI_EE_n_GSI_CH_k_CNTXT_0, ee, chan_idx, &ch_k_cntxt_0);
  4111. GSIERR("Q6 channel [%d] state = %d\n", chan_idx, ch_k_cntxt_0.chstate);
  4112. }
  4113. int gsi_halt_channel_ee(unsigned int chan_idx, unsigned int ee, int *code)
  4114. {
  4115. enum gsi_generic_ee_cmd_opcode op = GSI_GEN_EE_CMD_HALT_CHANNEL;
  4116. struct gsihal_reg_gsi_ee_generic_cmd cmd;
  4117. int res;
  4118. if (!gsi_ctx) {
  4119. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4120. return -GSI_STATUS_NODEV;
  4121. }
  4122. if (chan_idx >= gsi_ctx->max_ch || !code) {
  4123. GSIERR("bad params chan_idx=%d\n", chan_idx);
  4124. return -GSI_STATUS_INVALID_PARAMS;
  4125. }
  4126. mutex_lock(&gsi_ctx->mlock);
  4127. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4128. gsihal_get_glob_irq_en_gp_int1_mask(), ~0);
  4129. reinit_completion(&gsi_ctx->gen_ee_cmd_compl);
  4130. /* invalidate the response */
  4131. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(
  4132. GSI_EE_n_CNTXT_SCRATCH_0, gsi_ctx->per.ee);
  4133. gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code = 0;
  4134. gsihal_write_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4135. gsi_ctx->per.ee, gsi_ctx->scratch.word0.val);
  4136. gsi_ctx->gen_ee_cmd_dbg.halt_channel++;
  4137. cmd.opcode = op;
  4138. cmd.virt_chan_idx = chan_idx;
  4139. cmd.ee = ee;
  4140. gsihal_write_reg_n_fields(GSI_EE_n_GSI_EE_GENERIC_CMD, gsi_ctx->per.ee, &cmd);
  4141. res = wait_for_completion_timeout(&gsi_ctx->gen_ee_cmd_compl,
  4142. msecs_to_jiffies(GSI_CMD_TIMEOUT));
  4143. if (res == 0) {
  4144. GSIERR("chan_idx=%u ee=%u timed out\n", chan_idx, ee);
  4145. res = -GSI_STATUS_TIMED_OUT;
  4146. goto free_lock;
  4147. }
  4148. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4149. gsi_ctx->per.ee);
  4150. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code ==
  4151. GSI_GEN_EE_CMD_RETURN_CODE_RETRY) {
  4152. GSIDBG("chan_idx=%u ee=%u busy try again\n", chan_idx, ee);
  4153. *code = GSI_GEN_EE_CMD_RETURN_CODE_RETRY;
  4154. res = -GSI_STATUS_AGAIN;
  4155. goto free_lock;
  4156. }
  4157. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code == 0) {
  4158. GSIERR("No response received\n");
  4159. gsi_dump_halt_debug_reg(chan_idx, ee);
  4160. usleep_range(GSI_RESET_WA_MIN_SLEEP, GSI_RESET_WA_MAX_SLEEP);
  4161. GSIERR("Reading after usleep scratch 0 reg\n");
  4162. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4163. gsi_ctx->per.ee);
  4164. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code == 0) {
  4165. GSIERR("No response received second attempt\n");
  4166. gsi_dump_halt_debug_reg(chan_idx, ee);
  4167. res = -GSI_STATUS_ERROR;
  4168. goto free_lock;
  4169. }
  4170. }
  4171. res = GSI_STATUS_SUCCESS;
  4172. *code = gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code;
  4173. free_lock:
  4174. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4175. gsihal_get_glob_irq_en_gp_int1_mask(), 0);
  4176. mutex_unlock(&gsi_ctx->mlock);
  4177. return res;
  4178. }
  4179. EXPORT_SYMBOL(gsi_halt_channel_ee);
  4180. int gsi_alloc_channel_ee(unsigned int chan_idx, unsigned int ee, int *code)
  4181. {
  4182. enum gsi_generic_ee_cmd_opcode op = GSI_GEN_EE_CMD_ALLOC_CHANNEL;
  4183. struct gsi_chan_ctx *ctx;
  4184. struct gsihal_reg_gsi_ee_generic_cmd cmd;
  4185. int res;
  4186. if (chan_idx >= gsi_ctx->max_ch || !code) {
  4187. GSIERR("bad params chan_idx=%d\n", chan_idx);
  4188. return -GSI_STATUS_INVALID_PARAMS;
  4189. }
  4190. if (ee == 0)
  4191. return gsi_alloc_ap_channel(chan_idx);
  4192. mutex_lock(&gsi_ctx->mlock);
  4193. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4194. gsihal_get_glob_irq_en_gp_int1_mask(), ~0);
  4195. reinit_completion(&gsi_ctx->gen_ee_cmd_compl);
  4196. /* invalidate the response */
  4197. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4198. gsi_ctx->per.ee);
  4199. gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code = 0;
  4200. gsihal_write_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4201. gsi_ctx->per.ee, gsi_ctx->scratch.word0.val);
  4202. cmd.opcode = op;
  4203. cmd.virt_chan_idx = chan_idx;
  4204. cmd.ee = ee;
  4205. gsihal_write_reg_n_fields(
  4206. GSI_EE_n_GSI_EE_GENERIC_CMD, gsi_ctx->per.ee, &cmd);
  4207. res = wait_for_completion_timeout(&gsi_ctx->gen_ee_cmd_compl,
  4208. msecs_to_jiffies(GSI_CMD_TIMEOUT));
  4209. if (res == 0) {
  4210. GSIERR("chan_idx=%u ee=%u timed out\n", chan_idx, ee);
  4211. res = -GSI_STATUS_TIMED_OUT;
  4212. goto free_lock;
  4213. }
  4214. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4215. gsi_ctx->per.ee);
  4216. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code ==
  4217. GSI_GEN_EE_CMD_RETURN_CODE_OUT_OF_RESOURCES) {
  4218. GSIDBG("chan_idx=%u ee=%u out of resources\n", chan_idx, ee);
  4219. *code = GSI_GEN_EE_CMD_RETURN_CODE_OUT_OF_RESOURCES;
  4220. res = -GSI_STATUS_RES_ALLOC_FAILURE;
  4221. goto free_lock;
  4222. }
  4223. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code == 0) {
  4224. GSIERR("No response received\n");
  4225. res = -GSI_STATUS_ERROR;
  4226. goto free_lock;
  4227. }
  4228. if (ee == 0) {
  4229. ctx = &gsi_ctx->chan[chan_idx];
  4230. gsi_ctx->ch_dbg[chan_idx].ch_allocate++;
  4231. }
  4232. res = GSI_STATUS_SUCCESS;
  4233. *code = gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code;
  4234. free_lock:
  4235. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4236. gsihal_get_glob_irq_en_gp_int1_mask(), 0);
  4237. mutex_unlock(&gsi_ctx->mlock);
  4238. return res;
  4239. }
  4240. EXPORT_SYMBOL(gsi_alloc_channel_ee);
  4241. int gsi_enable_flow_control_ee(unsigned int chan_idx, unsigned int ee,
  4242. int *code)
  4243. {
  4244. enum gsi_generic_ee_cmd_opcode op = GSI_GEN_EE_CMD_ENABLE_FLOW_CHANNEL;
  4245. struct gsihal_reg_ch_k_cntxt_0 ch_k_cntxt_0;
  4246. struct gsihal_reg_gsi_ee_generic_cmd cmd;
  4247. enum gsi_chan_state curr_state = GSI_CHAN_STATE_NOT_ALLOCATED;
  4248. int res;
  4249. if (!gsi_ctx) {
  4250. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4251. return -GSI_STATUS_NODEV;
  4252. }
  4253. if (chan_idx >= gsi_ctx->max_ch || !code) {
  4254. GSIERR("bad params chan_idx=%d\n", chan_idx);
  4255. return -GSI_STATUS_INVALID_PARAMS;
  4256. }
  4257. mutex_lock(&gsi_ctx->mlock);
  4258. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4259. gsihal_get_glob_irq_en_gp_int1_mask(), ~0);
  4260. reinit_completion(&gsi_ctx->gen_ee_cmd_compl);
  4261. /* invalidate the response */
  4262. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4263. gsi_ctx->per.ee);
  4264. gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code = 0;
  4265. gsihal_write_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4266. gsi_ctx->per.ee, gsi_ctx->scratch.word0.val);
  4267. gsi_ctx->gen_ee_cmd_dbg.flow_ctrl_channel++;
  4268. cmd.opcode = op;
  4269. cmd.virt_chan_idx = chan_idx;
  4270. cmd.ee = ee;
  4271. gsihal_write_reg_n_fields(
  4272. GSI_EE_n_GSI_EE_GENERIC_CMD, gsi_ctx->per.ee, &cmd);
  4273. res = wait_for_completion_timeout(&gsi_ctx->gen_ee_cmd_compl,
  4274. msecs_to_jiffies(GSI_CMD_TIMEOUT));
  4275. if (res == 0) {
  4276. GSIERR("chan_idx=%u ee=%u timed out\n", chan_idx, ee);
  4277. res = -GSI_STATUS_TIMED_OUT;
  4278. goto free_lock;
  4279. }
  4280. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4281. gsi_ctx->per.ee);
  4282. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code ==
  4283. GSI_GEN_EE_CMD_RETURN_CODE_CHANNEL_NOT_RUNNING) {
  4284. GSIDBG("chan_idx=%u ee=%u not in correct state\n",
  4285. chan_idx, ee);
  4286. *code = GSI_GEN_EE_CMD_RETURN_CODE_CHANNEL_NOT_RUNNING;
  4287. res = -GSI_STATUS_RES_ALLOC_FAILURE;
  4288. goto free_lock;
  4289. } else if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code ==
  4290. GSI_GEN_EE_CMD_RETURN_CODE_INCORRECT_CHANNEL_TYPE ||
  4291. gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code ==
  4292. GSI_GEN_EE_CMD_RETURN_CODE_INCORRECT_CHANNEL_INDEX) {
  4293. GSIERR("chan_idx=%u ee=%u not in correct state\n",
  4294. chan_idx, ee);
  4295. GSI_ASSERT();
  4296. }
  4297. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code == 0) {
  4298. GSIERR("No response received\n");
  4299. res = -GSI_STATUS_ERROR;
  4300. goto free_lock;
  4301. }
  4302. /*Reading current channel state*/
  4303. gsihal_read_reg_nk_fields(GSI_EE_n_GSI_CH_k_CNTXT_0,
  4304. gsi_ctx->per.ee, chan_idx, &ch_k_cntxt_0);
  4305. curr_state = ch_k_cntxt_0.chstate;
  4306. if (curr_state == GSI_CHAN_STATE_FLOW_CONTROL) {
  4307. GSIDBG("ch %u state updated to %u\n", chan_idx, curr_state);
  4308. res = GSI_STATUS_SUCCESS;
  4309. } else {
  4310. GSIERR("ch %u state updated to %u incorrect state\n",
  4311. chan_idx, curr_state);
  4312. res = -GSI_STATUS_ERROR;
  4313. }
  4314. *code = gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code;
  4315. free_lock:
  4316. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4317. gsihal_get_glob_irq_en_gp_int1_mask(), 0);
  4318. mutex_unlock(&gsi_ctx->mlock);
  4319. return res;
  4320. }
  4321. EXPORT_SYMBOL(gsi_enable_flow_control_ee);
  4322. int gsi_flow_control_ee(unsigned int chan_idx, int ep_id, unsigned int ee,
  4323. bool enable, bool prmy_scnd_fc, int *code)
  4324. {
  4325. struct gsihal_reg_gsi_ee_generic_cmd cmd;
  4326. enum gsi_generic_ee_cmd_opcode op = enable ?
  4327. GSI_GEN_EE_CMD_ENABLE_FLOW_CHANNEL :
  4328. GSI_GEN_EE_CMD_DISABLE_FLOW_CHANNEL;
  4329. int res;
  4330. int wait_due_pending = 0;
  4331. uint32_t fc_pending = 0;
  4332. if (!gsi_ctx) {
  4333. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4334. return -GSI_STATUS_NODEV;
  4335. }
  4336. if (chan_idx >= gsi_ctx->max_ch || !code) {
  4337. GSIERR("bad params chan_idx=%d\n", chan_idx);
  4338. return -GSI_STATUS_INVALID_PARAMS;
  4339. }
  4340. GSIDBG("GSI flow control opcode=%d, ch_id=%d\n", op, chan_idx);
  4341. mutex_lock(&gsi_ctx->mlock);
  4342. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4343. gsihal_get_glob_irq_en_gp_int1_mask(), ~0);
  4344. reinit_completion(&gsi_ctx->gen_ee_cmd_compl);
  4345. /* invalidate the response */
  4346. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4347. gsi_ctx->per.ee);
  4348. gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code = 0;
  4349. gsihal_write_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4350. gsi_ctx->per.ee, gsi_ctx->scratch.word0.val);
  4351. gsi_ctx->gen_ee_cmd_dbg.flow_ctrl_channel++;
  4352. cmd.opcode = op;
  4353. cmd.virt_chan_idx = chan_idx;
  4354. cmd.ee = ee;
  4355. cmd.prmy_scnd_fc = prmy_scnd_fc;
  4356. gsihal_write_reg_n_fields(
  4357. GSI_EE_n_GSI_EE_GENERIC_CMD, gsi_ctx->per.ee, &cmd);
  4358. wait_again:
  4359. fc_pending = gsihal_read_reg_n(GSI_GSI_SHRAM_n,
  4360. (ep_id * GSI_FC_NUM_WORDS_PER_CHNL_SHRAM) + GSI_FC_STATE_INDEX_SHRAM) &
  4361. GSI_FC_PENDING_MASK;
  4362. res = wait_for_completion_timeout(&gsi_ctx->gen_ee_cmd_compl,
  4363. msecs_to_jiffies(GSI_FC_CMD_TIMEOUT));
  4364. if (res == 0) {
  4365. GSIERR("chan_idx=%u ee=%u timed out\n", chan_idx, ee);
  4366. if (op == GSI_GEN_EE_CMD_ENABLE_FLOW_CHANNEL &&
  4367. wait_due_pending < GSI_FC_MAX_TIMEOUT &&
  4368. fc_pending) {
  4369. wait_due_pending++;
  4370. goto wait_again;
  4371. }
  4372. GSIERR("GSI_EE_n_CNTXT_GLOB_IRQ_EN_OFFS = 0x%x\n",
  4373. gsihal_read_reg_n(GSI_EE_n_CNTXT_GLOB_IRQ_EN, gsi_ctx->per.ee));
  4374. GSIERR("GSI_EE_n_CNTXT_GLOB_IRQ_STTS_OFFS IRQ type = 0x%x\n",
  4375. gsihal_read_reg_n(GSI_EE_n_CNTXT_GLOB_IRQ_STTS, gsi_ctx->per.ee));
  4376. }
  4377. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4378. gsi_ctx->per.ee);
  4379. GSIDBG(
  4380. "Flow control command response GENERIC_CMD_RESPONSE_CODE = %u, val = %u\n",
  4381. gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code,
  4382. gsi_ctx->scratch.word0.val);
  4383. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code ==
  4384. GSI_GEN_EE_CMD_RETURN_CODE_CHANNEL_NOT_RUNNING) {
  4385. GSIDBG("chan_idx=%u ee=%u not in correct state\n",
  4386. chan_idx, ee);
  4387. *code = GSI_GEN_EE_CMD_RETURN_CODE_CHANNEL_NOT_RUNNING;
  4388. res = -GSI_STATUS_RES_ALLOC_FAILURE;
  4389. goto free_lock;
  4390. } else if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code ==
  4391. GSI_GEN_EE_CMD_RETURN_CODE_INCORRECT_CHANNEL_TYPE) {
  4392. GSIERR("chan_idx=%u ee=%u not in correct state\n",
  4393. chan_idx, ee);
  4394. GSI_ASSERT();
  4395. } else if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code ==
  4396. GSI_GEN_EE_CMD_RETURN_CODE_INCORRECT_CHANNEL_INDEX) {
  4397. GSIERR("Channel ID = %u ee = %u not allocated\n", chan_idx, ee);
  4398. }
  4399. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code == 0) {
  4400. GSIERR("No response received\n");
  4401. res = -GSI_STATUS_ERROR;
  4402. GSI_ASSERT();
  4403. goto free_lock;
  4404. }
  4405. *code = gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code;
  4406. res = GSI_STATUS_SUCCESS;
  4407. free_lock:
  4408. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4409. gsihal_get_glob_irq_en_gp_int1_mask(), 0);
  4410. mutex_unlock(&gsi_ctx->mlock);
  4411. return res;
  4412. }
  4413. EXPORT_SYMBOL(gsi_flow_control_ee);
  4414. int gsi_query_flow_control_state_ee(unsigned int chan_idx, unsigned int ee,
  4415. bool prmy_scnd_fc, int *code)
  4416. {
  4417. struct gsihal_reg_gsi_ee_generic_cmd cmd;
  4418. enum gsi_generic_ee_cmd_opcode op = GSI_GEN_EE_CMD_QUERY_FLOW_CHANNEL;
  4419. int res;
  4420. if (!gsi_ctx) {
  4421. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4422. return -GSI_STATUS_NODEV;
  4423. }
  4424. if (chan_idx >= gsi_ctx->max_ch || !code) {
  4425. GSIERR("bad params chan_idx=%d\n", chan_idx);
  4426. return -GSI_STATUS_INVALID_PARAMS;
  4427. }
  4428. mutex_lock(&gsi_ctx->mlock);
  4429. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4430. gsihal_get_glob_irq_en_gp_int1_mask(), ~0);
  4431. reinit_completion(&gsi_ctx->gen_ee_cmd_compl);
  4432. /* invalidate the response */
  4433. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4434. gsi_ctx->per.ee);
  4435. gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code = 0;
  4436. gsihal_write_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4437. gsi_ctx->per.ee, gsi_ctx->scratch.word0.val);
  4438. gsi_ctx->gen_ee_cmd_dbg.flow_ctrl_channel++;
  4439. cmd.opcode = op;
  4440. cmd.virt_chan_idx = chan_idx;
  4441. cmd.ee = ee;
  4442. cmd.prmy_scnd_fc = prmy_scnd_fc;
  4443. gsihal_write_reg_n_fields(
  4444. GSI_EE_n_GSI_EE_GENERIC_CMD, gsi_ctx->per.ee, &cmd);
  4445. res = wait_for_completion_timeout(&gsi_ctx->gen_ee_cmd_compl,
  4446. msecs_to_jiffies(GSI_CMD_TIMEOUT));
  4447. if (res == 0) {
  4448. GSIERR("chan_idx=%u ee=%u timed out\n", chan_idx, ee);
  4449. res = -GSI_STATUS_TIMED_OUT;
  4450. goto free_lock;
  4451. }
  4452. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4453. gsi_ctx->per.ee);
  4454. *code = gsi_ctx->scratch.word0.s.generic_ee_cmd_return_val;
  4455. if (prmy_scnd_fc)
  4456. res = (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_val ==
  4457. GSI_GEN_EE_CMD_RETURN_VAL_FLOW_CONTROL_SECONDARY)?
  4458. GSI_STATUS_SUCCESS:-GSI_STATUS_ERROR;
  4459. else
  4460. res = (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_val ==
  4461. GSI_GEN_EE_CMD_RETURN_VAL_FLOW_CONTROL_PRIMARY)?
  4462. GSI_STATUS_SUCCESS:-GSI_STATUS_ERROR;
  4463. free_lock:
  4464. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4465. gsihal_get_glob_irq_en_gp_int1_mask(), 0);
  4466. mutex_unlock(&gsi_ctx->mlock);
  4467. return res;
  4468. }
  4469. EXPORT_SYMBOL(gsi_query_flow_control_state_ee);
  4470. int gsi_map_virtual_ch_to_per_ep(u32 ee, u32 chan_num, u32 per_ep_index)
  4471. {
  4472. if (!gsi_ctx) {
  4473. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4474. return -GSI_STATUS_NODEV;
  4475. }
  4476. if (!gsi_ctx->base) {
  4477. GSIERR("access to GSI HW has not been mapped\n");
  4478. return -GSI_STATUS_INVALID_PARAMS;
  4479. }
  4480. gsihal_write_reg_nk(GSI_MAP_EE_n_CH_k_VP_TABLE,
  4481. ee, chan_num, per_ep_index);
  4482. return 0;
  4483. }
  4484. EXPORT_SYMBOL(gsi_map_virtual_ch_to_per_ep);
  4485. void gsi_wdi3_write_evt_ring_db(unsigned long evt_ring_hdl,
  4486. uint32_t db_addr_low, uint32_t db_addr_high)
  4487. {
  4488. if (!gsi_ctx) {
  4489. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4490. return;
  4491. }
  4492. if (gsi_ctx->per.ver >= GSI_VER_2_9) {
  4493. gsihal_write_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_10,
  4494. gsi_ctx->per.ee, evt_ring_hdl, db_addr_low);
  4495. gsihal_write_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_11,
  4496. gsi_ctx->per.ee, evt_ring_hdl, db_addr_high);
  4497. } else {
  4498. gsihal_write_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_12,
  4499. gsi_ctx->per.ee, evt_ring_hdl, db_addr_low);
  4500. gsihal_write_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_13,
  4501. gsi_ctx->per.ee, evt_ring_hdl, db_addr_high);
  4502. }
  4503. }
  4504. EXPORT_SYMBOL(gsi_wdi3_write_evt_ring_db);
  4505. int gsi_get_refetch_reg(unsigned long chan_hdl, bool is_rp)
  4506. {
  4507. if (is_rp) {
  4508. return gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_RE_FETCH_READ_PTR,
  4509. gsi_ctx->per.ee, chan_hdl);
  4510. } else {
  4511. return gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_RE_FETCH_WRITE_PTR,
  4512. gsi_ctx->per.ee, chan_hdl);
  4513. }
  4514. }
  4515. EXPORT_SYMBOL(gsi_get_refetch_reg);
  4516. /*
  4517. * ; +------------------------------------------------------+
  4518. * ; | NTN3 Rx Channel Scratch |
  4519. * ; +-------------+--------------------------------+-------+
  4520. * ; | 32-bit word | Field | Bits |
  4521. * ; +-------------+--------------------------------+-------+
  4522. * ; | 4 | NTN_PENDING_DB_AFTER_ROLLBACK | 18-18 |
  4523. * ; +-------------+--------------------------------+-------+
  4524. * ; | 5 | NTN_MSI_DB_INDEX_VALUE | 0-31 |
  4525. * ; +-------------+--------------------------------+-------+
  4526. * ; | 6 | NTN_RX_CHAIN_COUNTER | 0-31 |
  4527. * ; +-------------+--------------------------------+-------+
  4528. * ; | 7 | NTN_RX_ERR_COUNTER | 0-31 |
  4529. * ; +-------------+--------------------------------+-------+
  4530. * ; | 8 | NTN_ACCUMULATED_TRES_HANDLED | 0-31 |
  4531. * ; +-------------+--------------------------------+-------+
  4532. * ; | 9 | NTN_ROLLBACKS_COUNTER | 0-31 |
  4533. * ; +-------------+--------------------------------+-------+
  4534. * ; | FOR_SEQ_HIGH| NTN_MSI_DB_COUNT | 0-31 |
  4535. * ; +-------------+--------------------------------+-------+
  4536. *
  4537. * ; +------------------------------------------------------+
  4538. * ; | NTN3 Tx Channel Scratch |
  4539. * ; +-------------+--------------------------------+-------+
  4540. * ; | 32-bit word | Field | Bits |
  4541. * ; +-------------+--------------------------------+-------+
  4542. * ; | 4 | NTN_PENDING_DB_AFTER_ROLLBACK | 18-18 |
  4543. * ; +-------------+--------------------------------+-------+
  4544. * ; | 5 | NTN_MSI_DB_INDEX_VALUE | 0-31 |
  4545. * ; +-------------+--------------------------------+-------+
  4546. * ; | 6 | TX_DERR_COUNTER | 0-31 |
  4547. * ; +-------------+--------------------------------+-------+
  4548. * ; | 7 | NTN_TX_OOB_COUNTER | 0-31 |
  4549. * ; +-------------+--------------------------------+-------+
  4550. * ; | 8 | NTN_ACCUMULATED_TRES_HANDLED | 0-31 |
  4551. * ; +-------------+--------------------------------+-------+
  4552. * ; | 9 | NTN_ROLLBACKS_COUNTER | 0-31 |
  4553. * ; +-------------+--------------------------------+-------+
  4554. * ; | FOR_SEQ_HIGH| NTN_MSI_DB_COUNT | 0-31 |
  4555. * ; +-------------+--------------------------------+-------+
  4556. */
  4557. int gsi_ntn3_client_stats_get(unsigned ep_id, int scratch_id, unsigned chan_hdl)
  4558. {
  4559. switch (scratch_id) {
  4560. case -1:
  4561. return gsihal_read_reg_n(GSI_GSI_SHRAM_n, GSI_GSI_SHRAM_n_EP_FOR_SEQ_HIGH_N_GET(ep_id));
  4562. case 4:
  4563. return (gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_4, gsi_ctx->per.ee,
  4564. chan_hdl) >> GSI_NTN3_PENDING_DB_AFTER_RB_MASK) &
  4565. GSI_NTN3_PENDING_DB_AFTER_RB_SHIFT;
  4566. break;
  4567. case 5:
  4568. return gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_5, gsi_ctx->per.ee, chan_hdl);
  4569. break;
  4570. case 6:
  4571. return gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_6, gsi_ctx->per.ee, chan_hdl);
  4572. break;
  4573. case 7:
  4574. return gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_7, gsi_ctx->per.ee, chan_hdl);
  4575. break;
  4576. case 8:
  4577. return gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_8, gsi_ctx->per.ee, chan_hdl);
  4578. break;
  4579. case 9:
  4580. return gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_9, gsi_ctx->per.ee, chan_hdl);
  4581. break;
  4582. default:
  4583. GSIERR("invalid scratch id %d\n", scratch_id);
  4584. return 0;
  4585. }
  4586. return 0;
  4587. }
  4588. EXPORT_SYMBOL(gsi_ntn3_client_stats_get);
  4589. int gsi_get_drop_stats(unsigned long ep_id, int scratch_id,
  4590. unsigned long chan_hdl)
  4591. {
  4592. #define GSI_RTK_ERR_STATS_MASK 0xFFFF
  4593. #define GSI_NTN_ERR_STATS_MASK 0xFFFFFFFF
  4594. #define GSI_AQC_RX_STATUS_MASK 0x1FFF
  4595. #define GSI_AQC_RX_STATUS_SHIFT 0
  4596. #define GSI_AQC_RDM_ERR_MASK 0x1FFF0000
  4597. #define GSI_AQC_RDM_ERR_SHIFT 16
  4598. uint16_t rx_status;
  4599. uint16_t rdm_err;
  4600. uint32_t val;
  4601. /* on newer versions we can read the ch scratch directly from reg */
  4602. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  4603. switch (scratch_id) {
  4604. case 5:
  4605. return gsihal_read_reg_nk(
  4606. GSI_EE_n_GSI_CH_k_SCRATCH_5,
  4607. gsi_ctx->per.ee,
  4608. chan_hdl) & GSI_RTK_ERR_STATS_MASK;
  4609. break;
  4610. case 6:
  4611. return gsihal_read_reg_nk(
  4612. GSI_EE_n_GSI_CH_k_SCRATCH_6,
  4613. gsi_ctx->per.ee,
  4614. chan_hdl) & GSI_NTN_ERR_STATS_MASK;
  4615. break;
  4616. case 7:
  4617. val = gsihal_read_reg_nk(
  4618. GSI_EE_n_GSI_CH_k_SCRATCH_7,
  4619. gsi_ctx->per.ee,
  4620. chan_hdl);
  4621. rx_status = (val & GSI_AQC_RX_STATUS_MASK)
  4622. >> GSI_AQC_RX_STATUS_SHIFT;
  4623. rdm_err = (val & GSI_AQC_RDM_ERR_MASK)
  4624. >> (GSI_AQC_RDM_ERR_SHIFT);
  4625. return rx_status + rdm_err;
  4626. break;
  4627. default:
  4628. GSIERR("invalid scratch id %d\n", scratch_id);
  4629. return 0;
  4630. }
  4631. /* on older versions we need to read the scratch from SHRAM */
  4632. } else {
  4633. /* RTK use scratch 5 */
  4634. if (scratch_id == 5) {
  4635. /*
  4636. * each channel context is 6 lines of 8 bytes, but n in
  4637. * SHRAM_n is in 4 bytes offsets, so multiplying ep_id
  4638. * by 6*2=12 will give the beginning of the required
  4639. * channel context, and then need to add 7 since the
  4640. * channel context layout has the ring rbase (8 bytes)
  4641. * + channel scratch 0-4 (20 bytes) so adding
  4642. * additional 28/4 = 7 to get to scratch 5 of the
  4643. * required channel.
  4644. */
  4645. return gsihal_read_reg_n(
  4646. GSI_GSI_SHRAM_n,
  4647. ep_id * 12 + 7) & GSI_RTK_ERR_STATS_MASK;
  4648. }
  4649. }
  4650. return 0;
  4651. }
  4652. EXPORT_SYMBOL(gsi_get_drop_stats);
  4653. int gsi_get_wp(unsigned long chan_hdl)
  4654. {
  4655. return gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_6, gsi_ctx->per.ee,
  4656. chan_hdl);
  4657. }
  4658. EXPORT_SYMBOL(gsi_get_wp);
  4659. void gsi_wdi3_dump_register(unsigned long chan_hdl)
  4660. {
  4661. uint32_t val;
  4662. if (!gsi_ctx) {
  4663. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4664. return;
  4665. }
  4666. GSIDBG("reg dump ch id %ld\n", chan_hdl);
  4667. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_0,
  4668. gsi_ctx->per.ee, chan_hdl);
  4669. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_0 0x%x\n", val);
  4670. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_1,
  4671. gsi_ctx->per.ee, chan_hdl);
  4672. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_1 0x%x\n", val);
  4673. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_2,
  4674. gsi_ctx->per.ee, chan_hdl);
  4675. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_2 0x%x\n", val);
  4676. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_3,
  4677. gsi_ctx->per.ee, chan_hdl);
  4678. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_3 0x%x\n", val);
  4679. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_4,
  4680. gsi_ctx->per.ee, chan_hdl);
  4681. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_4 0x%x\n", val);
  4682. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_5,
  4683. gsi_ctx->per.ee, chan_hdl);
  4684. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_5 0x%x\n", val);
  4685. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_6,
  4686. gsi_ctx->per.ee, chan_hdl);
  4687. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_6 0x%x\n", val);
  4688. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_7,
  4689. gsi_ctx->per.ee, chan_hdl);
  4690. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_7 0x%x\n", val);
  4691. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_RE_FETCH_READ_PTR,
  4692. gsi_ctx->per.ee, chan_hdl);
  4693. GSIDBG("GSI_EE_n_GSI_CH_k_RE_FETCH_READ_PTR 0x%x\n", val);
  4694. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_RE_FETCH_WRITE_PTR,
  4695. gsi_ctx->per.ee, chan_hdl);
  4696. GSIDBG("GSI_EE_n_GSI_CH_k_RE_FETCH_WRITE_PTR 0x%x\n", val);
  4697. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_QOS,
  4698. gsi_ctx->per.ee, chan_hdl);
  4699. GSIDBG("GSI_EE_n_GSI_CH_k_QOS 0x%x\n", val);
  4700. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_0,
  4701. gsi_ctx->per.ee, chan_hdl);
  4702. GSIDBG("GSI_EE_n_GSI_CH_k_SCRATCH_0 0x%x\n", val);
  4703. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_1,
  4704. gsi_ctx->per.ee, chan_hdl);
  4705. GSIDBG("GSI_EE_n_GSI_CH_k_SCRATCH_1 0x%x\n", val);
  4706. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_2,
  4707. gsi_ctx->per.ee, chan_hdl);
  4708. GSIDBG("GSI_EE_n_GSI_CH_k_SCRATCH_2 0x%x\n", val);
  4709. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_3,
  4710. gsi_ctx->per.ee, chan_hdl);
  4711. GSIDBG("GSI_EE_n_GSI_CH_k_SCRATCH_3 0x%x\n", val);
  4712. }
  4713. EXPORT_SYMBOL(gsi_wdi3_dump_register);
  4714. int gsi_query_msi_addr(unsigned long chan_hdl, phys_addr_t *addr)
  4715. {
  4716. if (!gsi_ctx) {
  4717. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4718. return -GSI_STATUS_NODEV;
  4719. }
  4720. if (chan_hdl >= gsi_ctx->max_ch) {
  4721. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  4722. return -GSI_STATUS_INVALID_PARAMS;
  4723. }
  4724. if (gsi_ctx->chan[chan_hdl].state == GSI_CHAN_STATE_NOT_ALLOCATED) {
  4725. GSIERR("bad state %d\n",
  4726. gsi_ctx->chan[chan_hdl].state);
  4727. return -GSI_STATUS_UNSUPPORTED_OP;
  4728. }
  4729. *addr = (phys_addr_t)(gsi_ctx->per.phys_addr +
  4730. gsihal_get_reg_nk_ofst(GSI_EE_n_GSI_CH_k_CNTXT_8,
  4731. gsi_ctx->per.ee, chan_hdl));
  4732. return 0;
  4733. }
  4734. EXPORT_SYMBOL(gsi_query_msi_addr);
  4735. int gsi_query_device_msi_addr(u64 *addr)
  4736. {
  4737. if (!gsi_ctx) {
  4738. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4739. return -GSI_STATUS_NODEV;
  4740. }
  4741. if (gsi_ctx->msi_addr_set)
  4742. *addr = gsi_ctx->msi_addr;
  4743. else
  4744. *addr = 0;
  4745. GSIDBG("Device MSI Addr: 0x%lx", *addr);
  4746. return 0;
  4747. }
  4748. EXPORT_SYMBOL(gsi_query_device_msi_addr);
  4749. uint64_t gsi_read_event_ring_wp(int evtr_id, int ee)
  4750. {
  4751. uint64_t wp;
  4752. wp = gsihal_read_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_6,
  4753. ee, evtr_id);
  4754. wp |= ((uint64_t)gsihal_read_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_7,
  4755. ee, evtr_id)) << 32;
  4756. return wp;
  4757. }
  4758. EXPORT_SYMBOL(gsi_read_event_ring_wp);
  4759. uint64_t gsi_read_event_ring_bp(int evt_hdl)
  4760. {
  4761. return gsi_ctx->evtr[evt_hdl].ring.base;
  4762. }
  4763. EXPORT_SYMBOL(gsi_read_event_ring_bp);
  4764. uint64_t gsi_get_evt_ring_rp(int evt_hdl)
  4765. {
  4766. return gsi_ctx->evtr[evt_hdl].props.gsi_read_event_ring_rp(
  4767. &gsi_ctx->evtr[evt_hdl].props, evt_hdl, gsi_ctx->per.ee);
  4768. }
  4769. EXPORT_SYMBOL(gsi_get_evt_ring_rp);
  4770. uint64_t gsi_read_chan_ring_rp(int chan_id, int ee)
  4771. {
  4772. uint64_t rp;
  4773. rp = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_4,
  4774. ee, chan_id);
  4775. rp |= ((uint64_t)gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_5,
  4776. ee, chan_id)) << 32;
  4777. return rp;
  4778. }
  4779. EXPORT_SYMBOL(gsi_read_chan_ring_rp);
  4780. uint64_t gsi_read_chan_ring_wp(int chan_id, int ee)
  4781. {
  4782. uint64_t wp;
  4783. wp = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_6,
  4784. ee, chan_id);
  4785. wp |= ((uint64_t)gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_7,
  4786. ee, chan_id)) << 32;
  4787. return wp;
  4788. }
  4789. EXPORT_SYMBOL(gsi_read_chan_ring_wp);
  4790. uint64_t gsi_read_chan_ring_bp(int chan_hdl)
  4791. {
  4792. return gsi_ctx->chan[chan_hdl].ring.base;
  4793. }
  4794. EXPORT_SYMBOL(gsi_read_chan_ring_bp);
  4795. uint64_t gsi_read_chan_ring_re_fetch_wp(int chan_id, int ee)
  4796. {
  4797. uint64_t wp;
  4798. wp = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_RE_FETCH_WRITE_PTR,
  4799. ee, chan_id);
  4800. return wp;
  4801. }
  4802. EXPORT_SYMBOL(gsi_read_chan_ring_re_fetch_wp);
  4803. enum gsi_chan_prot gsi_get_chan_prot_type(int chan_hdl)
  4804. {
  4805. return gsi_ctx->chan[chan_hdl].props.prot;
  4806. }
  4807. EXPORT_SYMBOL(gsi_get_chan_prot_type);
  4808. enum gsi_chan_state gsi_get_chan_state(int chan_hdl)
  4809. {
  4810. return gsi_ctx->chan[chan_hdl].state;
  4811. }
  4812. EXPORT_SYMBOL(gsi_get_chan_state);
  4813. int gsi_get_chan_poll_mode(int chan_hdl)
  4814. {
  4815. return atomic_read(&gsi_ctx->chan[chan_hdl].poll_mode);
  4816. }
  4817. EXPORT_SYMBOL(gsi_get_chan_poll_mode);
  4818. uint32_t gsi_get_ring_len(int chan_hdl)
  4819. {
  4820. return gsi_ctx->chan[chan_hdl].ring.len;
  4821. }
  4822. EXPORT_SYMBOL(gsi_get_ring_len);
  4823. uint8_t gsi_get_chan_props_db_in_bytes(int chan_hdl)
  4824. {
  4825. return gsi_ctx->chan[chan_hdl].props.db_in_bytes;
  4826. }
  4827. EXPORT_SYMBOL(gsi_get_chan_props_db_in_bytes);
  4828. int gsi_get_peripheral_ee(void)
  4829. {
  4830. return gsi_ctx->per.ee;
  4831. }
  4832. EXPORT_SYMBOL(gsi_get_peripheral_ee);
  4833. uint32_t gsi_get_chan_stop_stm(int chan_id, int ee)
  4834. {
  4835. uint32_t ch_scratch;
  4836. ch_scratch = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_4, ee, chan_id);
  4837. /* Only bits 28 - 31 for STM */
  4838. return ((ch_scratch & 0xF0000000) >> 24);
  4839. }
  4840. EXPORT_SYMBOL(gsi_get_chan_stop_stm);
  4841. enum gsi_evt_ring_elem_size gsi_get_evt_ring_re_size(int evt_hdl)
  4842. {
  4843. return gsi_ctx->evtr[evt_hdl].props.re_size;
  4844. }
  4845. EXPORT_SYMBOL(gsi_get_evt_ring_re_size);
  4846. uint32_t gsi_get_evt_ring_len(int evt_hdl)
  4847. {
  4848. return gsi_ctx->evtr[evt_hdl].ring.len;
  4849. }
  4850. EXPORT_SYMBOL(gsi_get_evt_ring_len);
  4851. void gsi_update_almst_empty_thrshold(unsigned long chan_hdl, unsigned short threshold)
  4852. {
  4853. gsihal_write_reg_nk(GSI_EE_n_CH_k_CH_ALMST_EMPTY_THRSHOLD,
  4854. gsi_ctx->per.ee, chan_hdl, threshold);
  4855. }
  4856. EXPORT_SYMBOL(gsi_update_almst_empty_thrshold);
  4857. static union __packed gsi_channel_scratch __gsi_update_mhi_channel_scratch(
  4858. unsigned long chan_hdl, struct __packed gsi_mhi_channel_scratch mscr)
  4859. {
  4860. union __packed gsi_channel_scratch scr;
  4861. /* below sequence is not atomic. assumption is sequencer specific fields
  4862. * will remain unchanged across this sequence
  4863. */
  4864. /* READ */
  4865. scr.data.word1 = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_0,
  4866. gsi_ctx->per.ee, chan_hdl);
  4867. scr.data.word2 = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_1,
  4868. gsi_ctx->per.ee, chan_hdl);
  4869. scr.data.word3 = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_2,
  4870. gsi_ctx->per.ee, chan_hdl);
  4871. scr.data.word4 = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_3,
  4872. gsi_ctx->per.ee, chan_hdl);
  4873. /* UPDATE */
  4874. scr.mhi.polling_mode = mscr.polling_mode;
  4875. if (gsi_ctx->per.ver < GSI_VER_2_5) {
  4876. scr.mhi.max_outstanding_tre = mscr.max_outstanding_tre;
  4877. scr.mhi.outstanding_threshold = mscr.outstanding_threshold;
  4878. }
  4879. /* WRITE */
  4880. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_0,
  4881. gsi_ctx->per.ee, chan_hdl, scr.data.word1);
  4882. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_1,
  4883. gsi_ctx->per.ee, chan_hdl, scr.data.word2);
  4884. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_2,
  4885. gsi_ctx->per.ee, chan_hdl, scr.data.word3);
  4886. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_3,
  4887. gsi_ctx->per.ee, chan_hdl, scr.data.word4);
  4888. return scr;
  4889. }
  4890. /**
  4891. * gsi_get_hw_profiling_stats() - Query GSI HW profiling stats
  4892. * @stats: [out] stats blob from client populated by driver
  4893. *
  4894. * Returns: 0 on success, negative on failure
  4895. *
  4896. */
  4897. int gsi_get_hw_profiling_stats(struct gsi_hw_profiling_data *stats)
  4898. {
  4899. if (stats == NULL) {
  4900. GSIERR("bad parms NULL stats == NULL\n");
  4901. return -EINVAL;
  4902. }
  4903. stats->bp_cnt = (u64)gsihal_read_reg(
  4904. GSI_GSI_MCS_PROFILING_BP_CNT_LSB) +
  4905. ((u64)gsihal_read_reg(
  4906. GSI_GSI_MCS_PROFILING_BP_CNT_MSB) << 32);
  4907. stats->bp_and_pending_cnt = (u64)gsihal_read_reg(
  4908. GSI_GSI_MCS_PROFILING_BP_AND_PENDING_CNT_LSB) +
  4909. ((u64)gsihal_read_reg(
  4910. GSI_GSI_MCS_PROFILING_BP_AND_PENDING_CNT_MSB) << 32);
  4911. stats->mcs_busy_cnt = (u64)gsihal_read_reg(
  4912. GSI_GSI_MCS_PROFILING_MCS_BUSY_CNT_LSB) +
  4913. ((u64)gsihal_read_reg(
  4914. GSI_GSI_MCS_PROFILING_MCS_BUSY_CNT_MSB) << 32);
  4915. stats->mcs_idle_cnt = (u64)gsihal_read_reg(
  4916. GSI_GSI_MCS_PROFILING_MCS_IDLE_CNT_LSB) +
  4917. ((u64)gsihal_read_reg(
  4918. GSI_GSI_MCS_PROFILING_MCS_IDLE_CNT_MSB) << 32);
  4919. return 0;
  4920. }
  4921. /**
  4922. * gsi_get_fw_version() - Query GSI FW version
  4923. * @ver: [out] ver blob from client populated by driver
  4924. *
  4925. * Returns: 0 on success, negative on failure
  4926. *
  4927. */
  4928. int gsi_get_fw_version(struct gsi_fw_version *ver)
  4929. {
  4930. u32 raw = 0;
  4931. if (ver == NULL) {
  4932. GSIERR("bad parms: ver == NULL\n");
  4933. return -EINVAL;
  4934. }
  4935. if (gsi_ctx->per.ver < GSI_VER_3_0)
  4936. raw = gsihal_read_reg_n(GSI_GSI_INST_RAM_n,
  4937. GSI_INST_RAM_FW_VER_OFFSET);
  4938. else
  4939. raw = gsihal_read_reg_n(GSI_GSI_INST_RAM_n,
  4940. GSI_INST_RAM_FW_VER_GSI_3_0_OFFSET);
  4941. ver->hw = (raw & GSI_INST_RAM_FW_VER_HW_MASK) >>
  4942. GSI_INST_RAM_FW_VER_HW_SHIFT;
  4943. ver->flavor = (raw & GSI_INST_RAM_FW_VER_FLAVOR_MASK) >>
  4944. GSI_INST_RAM_FW_VER_FLAVOR_SHIFT;
  4945. ver->fw = (raw & GSI_INST_RAM_FW_VER_FW_MASK) >>
  4946. GSI_INST_RAM_FW_VER_FW_SHIFT;
  4947. return 0;
  4948. }
  4949. static int msm_gsi_probe(struct platform_device *pdev)
  4950. {
  4951. struct device *dev = &pdev->dev;
  4952. int result;
  4953. pr_debug("gsi_probe\n");
  4954. gsi_ctx = devm_kzalloc(dev, sizeof(*gsi_ctx), GFP_KERNEL);
  4955. if (!gsi_ctx) {
  4956. dev_err(dev, "failed to allocated gsi context\n");
  4957. return -ENOMEM;
  4958. }
  4959. gsi_ctx->ipc_logbuf = ipc_log_context_create(GSI_IPC_LOG_PAGES,
  4960. "gsi", 0);
  4961. if (gsi_ctx->ipc_logbuf == NULL)
  4962. GSIERR("failed to create IPC log, continue...\n");
  4963. result = of_property_read_u32(pdev->dev.of_node, "qcom,num-msi",
  4964. &gsi_ctx->msi.num);
  4965. if (result)
  4966. GSIERR("No MSIs configured\n");
  4967. else {
  4968. if (gsi_ctx->msi.num > GSI_MAX_NUM_MSI) {
  4969. GSIERR("Num MSIs %u larger than max %u, normalizing\n");
  4970. gsi_ctx->msi.num = GSI_MAX_NUM_MSI;
  4971. } else GSIDBG("Num MSIs=%u\n", gsi_ctx->msi.num);
  4972. }
  4973. gsi_ctx->dev = dev;
  4974. init_completion(&gsi_ctx->gen_ee_cmd_compl);
  4975. gsi_debugfs_init();
  4976. return 0;
  4977. }
  4978. static struct platform_driver msm_gsi_driver = {
  4979. .probe = msm_gsi_probe,
  4980. .driver = {
  4981. .name = "gsi",
  4982. .of_match_table = msm_gsi_match,
  4983. },
  4984. };
  4985. static struct platform_device *pdev;
  4986. /**
  4987. * Module Init.
  4988. */
  4989. static int __init gsi_init(void)
  4990. {
  4991. int ret;
  4992. pr_debug("%s\n", __func__);
  4993. ret = platform_driver_register(&msm_gsi_driver);
  4994. if (ret < 0)
  4995. goto out;
  4996. if (running_emulation) {
  4997. pdev = platform_device_register_simple("gsi", -1, NULL, 0);
  4998. if (IS_ERR(pdev)) {
  4999. ret = PTR_ERR(pdev);
  5000. platform_driver_unregister(&msm_gsi_driver);
  5001. goto out;
  5002. }
  5003. }
  5004. out:
  5005. return ret;
  5006. }
  5007. arch_initcall(gsi_init);
  5008. /*
  5009. * Module exit.
  5010. */
  5011. static void __exit gsi_exit(void)
  5012. {
  5013. if (running_emulation && pdev)
  5014. platform_device_unregister(pdev);
  5015. platform_driver_unregister(&msm_gsi_driver);
  5016. }
  5017. module_exit(gsi_exit);
  5018. MODULE_LICENSE("GPL v2");
  5019. MODULE_DESCRIPTION("Generic Software Interface (GSI)");