adsprpc.c 238 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. /* Uncomment this block to log an error on every VERIFY failure */
  7. /*
  8. * #ifndef VERIFY_PRINT_ERROR
  9. * #define VERIFY_PRINT_ERROR
  10. * #endif
  11. */
  12. #include <linux/dma-buf.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/qcom-dma-mapping.h>
  15. #include <linux/slab.h>
  16. #include <linux/completion.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/mm.h>
  19. #include <linux/wait.h>
  20. #include <linux/sched.h>
  21. #include <linux/module.h>
  22. #include <linux/list.h>
  23. #include <linux/arch_topology.h>
  24. #include <linux/hash.h>
  25. #include <linux/msm_ion.h>
  26. #include <linux/qcom_scm.h>
  27. #include <linux/ipc_logging.h>
  28. #include <linux/remoteproc/qcom_rproc.h>
  29. #include <linux/scatterlist.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/device.h>
  32. #include <linux/of.h>
  33. #include <linux/of_address.h>
  34. #include <linux/of_platform.h>
  35. #include <linux/dma-map-ops.h>
  36. #include <linux/cma.h>
  37. #include <linux/sort.h>
  38. #include <linux/cred.h>
  39. #include <linux/msm_dma_iommu_mapping.h>
  40. #include "adsprpc_compat.h"
  41. #include "adsprpc_shared.h"
  42. #include <soc/qcom/qcom_ramdump.h>
  43. #include <soc/qcom/minidump.h>
  44. #include <linux/delay.h>
  45. #include <linux/debugfs.h>
  46. #include <linux/pm_qos.h>
  47. #include <linux/stat.h>
  48. #include <linux/preempt.h>
  49. #include <linux/of_reserved_mem.h>
  50. #include <linux/soc/qcom/pdr.h>
  51. #include <linux/soc/qcom/qmi.h>
  52. #include <linux/mem-buf.h>
  53. #include <linux/iommu.h>
  54. #include <asm/arch_timer.h>
  55. #include <linux/genalloc.h>
  56. #include <soc/qcom/socinfo.h>
  57. #ifdef CONFIG_HIBERNATION
  58. #include <linux/suspend.h>
  59. #include <linux/notifier.h>
  60. #endif
  61. #define CREATE_TRACE_POINTS
  62. #include "fastrpc_trace.h"
  63. #ifdef CONFIG_MSM_ADSPRPC_TRUSTED
  64. #include "../include/linux/fastrpc.h"
  65. #else
  66. #include "fastrpc.h"
  67. #endif
  68. #define TZ_PIL_PROTECT_MEM_SUBSYS_ID 0x0C
  69. #define TZ_PIL_CLEAR_PROTECT_MEM_SUBSYS_ID 0x0D
  70. #define TZ_PIL_AUTH_QDSP6_PROC 1
  71. #define FASTRPC_ENOSUCH 39
  72. #define VMID_SSC_Q6 5
  73. #define VMID_ADSP_Q6 6
  74. #define DEBUGFS_SIZE 3072
  75. #define PID_SIZE 10
  76. #define AUDIO_PDR_ADSP_DTSI_PROPERTY_NAME "qcom,fastrpc-adsp-audio-pdr"
  77. #define AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME "audio_pdr_adsprpc"
  78. #define AUDIO_PDR_ADSP_SERVICE_NAME "avs/audio"
  79. #define ADSP_AUDIOPD_NAME "msm/adsp/audio_pd"
  80. #define SENSORS_PDR_ADSP_DTSI_PROPERTY_NAME "qcom,fastrpc-adsp-sensors-pdr"
  81. #define SENSORS_PDR_ADSP_SERVICE_LOCATION_CLIENT_NAME "sensors_pdr_adsprpc"
  82. #define SENSORS_PDR_ADSP_SERVICE_NAME "tms/servreg"
  83. #define ADSP_SENSORPD_NAME "msm/adsp/sensor_pd"
  84. #define SENSORS_PDR_SLPI_DTSI_PROPERTY_NAME "qcom,fastrpc-slpi-sensors-pdr"
  85. #define SENSORS_PDR_SLPI_SERVICE_LOCATION_CLIENT_NAME "sensors_pdr_sdsprpc"
  86. #define SENSORS_PDR_SLPI_SERVICE_NAME SENSORS_PDR_ADSP_SERVICE_NAME
  87. #define SLPI_SENSORPD_NAME "msm/slpi/sensor_pd"
  88. #define FASTRPC_SECURE_WAKE_SOURCE_CLIENT_NAME "adsprpc-secure"
  89. #define FASTRPC_NON_SECURE_WAKE_SOURCE_CLIENT_NAME "adsprpc-non_secure"
  90. #define RPC_TIMEOUT (5 * HZ)
  91. #define BALIGN 128
  92. #define M_FDLIST (16)
  93. #define M_CRCLIST (64)
  94. #define M_KERNEL_PERF_LIST (PERF_KEY_MAX)
  95. #define M_DSP_PERF_LIST (12)
  96. #define FASTRPC_CTX_MAGIC (0xbeeddeed)
  97. /* Process status notifications from DSP will be sent with this unique context */
  98. #define FASTRPC_NOTIF_CTX_RESERVED 0xABCDABCD
  99. #define FASTRPC_CTX_JOB_TYPE_POS (4)
  100. #define FASTRPC_CTX_TABLE_IDX_POS (6)
  101. #define FASTRPC_CTX_JOBID_POS (16)
  102. #define FASTRPC_CTX_TABLE_IDX_MASK \
  103. ((FASTRPC_CTX_MAX - 1) << FASTRPC_CTX_TABLE_IDX_POS)
  104. #define FASTRPC_ASYNC_JOB_MASK (1)
  105. #define GET_TABLE_IDX_FROM_CTXID(ctxid) \
  106. ((ctxid & FASTRPC_CTX_TABLE_IDX_MASK) >> FASTRPC_CTX_TABLE_IDX_POS)
  107. /* Reserve few entries in context table for critical kernel and static RPC
  108. * calls to avoid user invocations from exhausting all entries.
  109. */
  110. #define NUM_KERNEL_AND_STATIC_ONLY_CONTEXTS (70)
  111. /* Maximum number of pending contexts per remote session */
  112. #define MAX_PENDING_CTX_PER_SESSION (64)
  113. #define NUM_DEVICES 2 /* adsprpc-smd, adsprpc-smd-secure */
  114. #define MINOR_NUM_DEV 0
  115. #define MINOR_NUM_SECURE_DEV 1
  116. #define NON_SECURE_CHANNEL 0
  117. #define SECURE_CHANNEL 1
  118. #define IS_CACHE_ALIGNED(x) (((x) & ((L1_CACHE_BYTES)-1)) == 0)
  119. #ifndef ION_FLAG_CACHED
  120. #define ION_FLAG_CACHED (1)
  121. #endif
  122. #ifndef topology_cluster_id
  123. #define topology_cluster_id(cpu) topology_physical_package_id(cpu)
  124. #endif
  125. /*
  126. * ctxid of every message is OR-ed with fastrpc_remote_pd_type before
  127. * it is sent to DSP. So mask 2 LSBs to retrieve actual context
  128. */
  129. #define CONTEXT_PD_CHECK (3)
  130. #define GET_CTXID_FROM_RSP_CTX(rsp_ctx) (rsp_ctx & ~CONTEXT_PD_CHECK)
  131. #define RH_CID ADSP_DOMAIN_ID
  132. #define FASTRPC_STATIC_HANDLE_PROCESS_GROUP (1)
  133. #define FASTRPC_STATIC_HANDLE_DSP_UTILITIES (2)
  134. #define FASTRPC_STATIC_HANDLE_LISTENER (3)
  135. #define FASTRPC_STATIC_HANDLE_MAX (20)
  136. #define FASTRPC_LATENCY_CTRL_ENB (1)
  137. /* Maximum PM timeout that can be voted through fastrpc */
  138. #define MAX_PM_TIMEOUT_MS 50
  139. /* timeout in us for busy polling after early response from remote processor */
  140. #define FASTRPC_POLL_TIME (4000)
  141. /* timeout in us for polling until memory barrier */
  142. #define FASTRPC_POLL_TIME_MEM_UPDATE (500)
  143. /* timeout in us for polling completion signal after user early hint */
  144. #define FASTRPC_USER_EARLY_HINT_TIMEOUT (500)
  145. /* Early wake up poll completion number received from remote processor */
  146. #define FASTRPC_EARLY_WAKEUP_POLL (0xabbccdde)
  147. /* Poll response number from remote processor for call completion */
  148. #define FASTRPC_POLL_RESPONSE (0xdecaf)
  149. /* latency in us, early wake up signal used below this value */
  150. #define FASTRPC_EARLY_WAKEUP_LATENCY (200)
  151. /* response version number */
  152. #define FASTRPC_RSP_VERSION2 (2)
  153. /* CPU feature information to DSP */
  154. #define FASTRPC_CPUINFO_DEFAULT (0)
  155. #define FASTRPC_CPUINFO_EARLY_WAKEUP (1)
  156. #define INIT_FILELEN_MAX (2*1024*1024)
  157. #define INIT_MEMLEN_MAX_STATIC (8*1024*1024)
  158. #define INIT_MEMLEN_MAX_DYNAMIC (200*1024*1024)
  159. #define INIT_MEMLEN_MIN_DYNAMIC (3*1024*1024)
  160. #define MAX_CACHE_BUF_SIZE (8*1024*1024)
  161. /* Maximum buffers cached in cached buffer list */
  162. #define MAX_CACHED_BUFS (32)
  163. /* Max no. of persistent headers pre-allocated per process */
  164. #define MAX_PERSISTENT_HEADERS (25)
  165. /* Max value of unique fastrpc tgid */
  166. #define MAX_FRPC_TGID 65
  167. #define PERF_CAPABILITY_SUPPORT (1 << 1)
  168. #define KERNEL_ERROR_CODE_V1_SUPPORT 1
  169. #define USERSPACE_ALLOCATION_SUPPORT 1
  170. #define DSPSIGNAL_SUPPORT 1
  171. #define MD_GMSG_BUFFER (1000)
  172. #define MINI_DUMP_DBG_SIZE (200*1024)
  173. /* Max number of region supported */
  174. #define MAX_UNIQUE_ID 5
  175. /* Convert the 19.2MHz clock count to micro-seconds */
  176. #define CONVERT_CNT_TO_US(CNT) (CNT * 10ull / 192ull)
  177. #define FASTRPC_USER_PD_FORCE_KILL 2
  178. /*
  179. * No. of pages shared with DSP during process init
  180. * First page for init-mem and second page for proc-attrs
  181. */
  182. #define PAGESLEN_WITH_SHAREDBUF 2
  183. /* Unique index flag used for mini dump */
  184. static int md_unique_index_flag[MAX_UNIQUE_ID] = { 0, 0, 0, 0, 0 };
  185. /* Array to keep track unique tgid_frpc usage */
  186. static bool frpc_tgid_usage_array[NUM_CHANNELS][MAX_FRPC_TGID] = {0};
  187. /* Fastrpc remote process attributes */
  188. enum fastrpc_proc_attr {
  189. /* Macro for Debug attr */
  190. FASTRPC_MODE_DEBUG = 1 << 0,
  191. /* Macro for Ptrace */
  192. FASTRPC_MODE_PTRACE = 1 << 1,
  193. /* Macro for CRC Check */
  194. FASTRPC_MODE_CRC = 1 << 2,
  195. /* Macro for Unsigned PD */
  196. FASTRPC_MODE_UNSIGNED_MODULE = 1 << 3,
  197. /* Macro for Adaptive QoS */
  198. FASTRPC_MODE_ADAPTIVE_QOS = 1 << 4,
  199. /* Macro for System Process */
  200. FASTRPC_MODE_SYSTEM_PROCESS = 1 << 5,
  201. /* Macro for Prvileged Process */
  202. FASTRPC_MODE_PRIVILEGED = (1 << 6),
  203. /* Macro for system unsigned PD */
  204. FASTRPC_MODE_SYSTEM_UNSIGNED_PD = 1 << 17,
  205. };
  206. /* FastRPC remote subsystem state*/
  207. enum fastrpc_remote_subsys_state {
  208. SUBSYSTEM_RESTARTING = 0,
  209. SUBSYSTEM_DOWN,
  210. SUBSYSTEM_UP,
  211. };
  212. #define PERF_END ((void)0)
  213. #define PERF(enb, cnt, ff) \
  214. {\
  215. struct timespec64 startT = {0};\
  216. uint64_t *counter = cnt;\
  217. if (enb && counter) {\
  218. ktime_get_real_ts64(&startT);\
  219. } \
  220. ff ;\
  221. if (enb && counter) {\
  222. *counter += getnstimediff(&startT);\
  223. } \
  224. }
  225. #define GET_COUNTER(perf_ptr, offset) \
  226. (perf_ptr != NULL ?\
  227. (((offset >= 0) && (offset < PERF_KEY_MAX)) ?\
  228. (uint64_t *)(perf_ptr + offset)\
  229. : (uint64_t *)NULL) : (uint64_t *)NULL)
  230. /* Macro for comparing local client and PD names with those from callback */
  231. #define COMPARE_SERVICE_LOCATOR_NAMES(cb_client, local_client, \
  232. cb_pdname, local_pdname) \
  233. ((!strcmp(cb_client, local_client)) \
  234. && (!strcmp(cb_pdname, local_pdname)))
  235. #define IS_ASYNC_FASTRPC_AVAILABLE (1)
  236. /* Use the second definition to enable additional dspsignal debug logging */
  237. #define DSPSIGNAL_VERBOSE(x, ...)
  238. /*#define DSPSIGNAL_VERBOSE ADSPRPC_INFO*/
  239. MODULE_IMPORT_NS(DMA_BUF);
  240. static struct dentry *debugfs_root;
  241. static struct dentry *debugfs_global_file;
  242. static inline uint64_t buf_page_start(uint64_t buf)
  243. {
  244. uint64_t start = (uint64_t) buf & PAGE_MASK;
  245. return start;
  246. }
  247. static inline uint64_t buf_page_offset(uint64_t buf)
  248. {
  249. uint64_t offset = (uint64_t) buf & (PAGE_SIZE - 1);
  250. return offset;
  251. }
  252. static inline uint64_t buf_num_pages(uint64_t buf, size_t len)
  253. {
  254. uint64_t start = buf_page_start(buf) >> PAGE_SHIFT;
  255. uint64_t end = (((uint64_t) buf + len - 1) & PAGE_MASK) >> PAGE_SHIFT;
  256. uint64_t nPages = end - start + 1;
  257. return nPages;
  258. }
  259. static inline uint64_t buf_page_size(uint32_t size)
  260. {
  261. uint64_t sz = (size + (PAGE_SIZE - 1)) & PAGE_MASK;
  262. return sz > PAGE_SIZE ? sz : PAGE_SIZE;
  263. }
  264. static inline void *uint64_to_ptr(uint64_t addr)
  265. {
  266. void *ptr = (void *)((uintptr_t)addr);
  267. return ptr;
  268. }
  269. static inline uint64_t ptr_to_uint64(void *ptr)
  270. {
  271. uint64_t addr = (uint64_t)((uintptr_t)ptr);
  272. return addr;
  273. }
  274. static struct fastrpc_apps gfa;
  275. static struct fastrpc_channel_ctx gcinfo[NUM_CHANNELS] = {
  276. {
  277. .name = "adsprpc-smd",
  278. .subsys = "lpass",
  279. .spd = {
  280. {
  281. .servloc_name =
  282. AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME,
  283. .spdname = ADSP_AUDIOPD_NAME,
  284. .cid = ADSP_DOMAIN_ID,
  285. },
  286. {
  287. .servloc_name =
  288. SENSORS_PDR_ADSP_SERVICE_LOCATION_CLIENT_NAME,
  289. .spdname = ADSP_SENSORPD_NAME,
  290. .cid = ADSP_DOMAIN_ID,
  291. }
  292. },
  293. .cpuinfo_todsp = FASTRPC_CPUINFO_DEFAULT,
  294. .cpuinfo_status = false,
  295. },
  296. {
  297. .name = "mdsprpc-smd",
  298. .subsys = "mpss",
  299. .spd = {
  300. {
  301. .cid = MDSP_DOMAIN_ID,
  302. }
  303. },
  304. .cpuinfo_todsp = FASTRPC_CPUINFO_DEFAULT,
  305. .cpuinfo_status = false,
  306. },
  307. {
  308. .name = "sdsprpc-smd",
  309. .subsys = "dsps",
  310. .spd = {
  311. {
  312. .servloc_name =
  313. SENSORS_PDR_SLPI_SERVICE_LOCATION_CLIENT_NAME,
  314. .spdname = SLPI_SENSORPD_NAME,
  315. .cid = SDSP_DOMAIN_ID,
  316. }
  317. },
  318. .cpuinfo_todsp = FASTRPC_CPUINFO_DEFAULT,
  319. .cpuinfo_status = false,
  320. },
  321. {
  322. .name = "cdsprpc-smd",
  323. .subsys = "cdsp",
  324. .spd = {
  325. {
  326. .cid = CDSP_DOMAIN_ID,
  327. }
  328. },
  329. .cpuinfo_todsp = FASTRPC_CPUINFO_EARLY_WAKEUP,
  330. .cpuinfo_status = false,
  331. },
  332. };
  333. static uint32_t kernel_capabilities[FASTRPC_MAX_ATTRIBUTES -
  334. FASTRPC_MAX_DSP_ATTRIBUTES] = {
  335. PERF_CAPABILITY_SUPPORT,
  336. /* PERF_LOGGING_V2_SUPPORT feature is supported, unsupported = 0 */
  337. KERNEL_ERROR_CODE_V1_SUPPORT,
  338. /* Fastrpc Driver error code changes present */
  339. USERSPACE_ALLOCATION_SUPPORT,
  340. /* Userspace allocation allowed for DSP memory request*/
  341. DSPSIGNAL_SUPPORT
  342. /* Lightweight driver-based signaling */
  343. };
  344. static inline void fastrpc_pm_awake(struct fastrpc_file *fl, int channel_type);
  345. static int fastrpc_mem_map_to_dsp(struct fastrpc_file *fl, int fd, int offset,
  346. uint32_t flags, uintptr_t va, uint64_t phys,
  347. size_t size, uintptr_t *raddr);
  348. static inline void fastrpc_update_rxmsg_buf(struct fastrpc_channel_ctx *chan,
  349. uint64_t ctx, int retval, uint32_t rsp_flags,
  350. uint32_t early_wake_time, uint32_t ver, int64_t ns, uint64_t xo_time_in_us);
  351. /**
  352. * fastrpc_device_create - Create device for the fastrpc process file
  353. * @fl : Fastrpc process file
  354. * Returns: 0 on Success
  355. */
  356. static int fastrpc_device_create(struct fastrpc_file *fl);
  357. static inline int64_t getnstimediff(struct timespec64 *start)
  358. {
  359. int64_t ns;
  360. struct timespec64 ts, b;
  361. ktime_get_real_ts64(&ts);
  362. b = timespec64_sub(ts, *start);
  363. ns = timespec64_to_ns(&b);
  364. return ns;
  365. }
  366. /**
  367. * get_timestamp_in_ns - Gets time of day in nanoseconds
  368. *
  369. * Returns: Timestamp in nanoseconds
  370. */
  371. static inline int64_t get_timestamp_in_ns(void)
  372. {
  373. int64_t ns = 0;
  374. struct timespec64 ts;
  375. ktime_get_real_ts64(&ts);
  376. ns = timespec64_to_ns(&ts);
  377. return ns;
  378. }
  379. static inline int poll_for_remote_response(struct smq_invoke_ctx *ctx, uint32_t timeout)
  380. {
  381. int err = -EIO;
  382. uint32_t sc = ctx->sc, ii = 0, jj = 0;
  383. struct smq_invoke_buf *list;
  384. struct smq_phy_page *pages;
  385. uint64_t *fdlist = NULL;
  386. uint32_t *crclist = NULL, *poll = NULL;
  387. unsigned int inbufs, outbufs, handles;
  388. /* calculate poll memory location */
  389. inbufs = REMOTE_SCALARS_INBUFS(sc);
  390. outbufs = REMOTE_SCALARS_OUTBUFS(sc);
  391. handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc);
  392. list = smq_invoke_buf_start(ctx->rpra, sc);
  393. pages = smq_phy_page_start(sc, list);
  394. fdlist = (uint64_t *)(pages + inbufs + outbufs + handles);
  395. crclist = (uint32_t *)(fdlist + M_FDLIST);
  396. poll = (uint32_t *)(crclist + M_CRCLIST);
  397. /* poll on memory for DSP response. Return failure on timeout */
  398. for (ii = 0, jj = 0; ii < timeout; ii++, jj++) {
  399. if (*poll == FASTRPC_EARLY_WAKEUP_POLL) {
  400. /* Remote processor sent early response */
  401. err = 0;
  402. break;
  403. } else if (*poll == FASTRPC_POLL_RESPONSE) {
  404. /* Remote processor sent poll response to complete the call */
  405. err = 0;
  406. ctx->is_work_done = true;
  407. ctx->retval = 0;
  408. /* Update DSP response history */
  409. fastrpc_update_rxmsg_buf(&gfa.channel[ctx->fl->cid],
  410. ctx->msg.invoke.header.ctx, 0, POLL_MODE, 0,
  411. FASTRPC_RSP_VERSION2, get_timestamp_in_ns(),
  412. CONVERT_CNT_TO_US(__arch_counter_get_cntvct()));
  413. break;
  414. }
  415. if (jj == FASTRPC_POLL_TIME_MEM_UPDATE) {
  416. /* Wait for DSP to finish updating poll memory */
  417. rmb();
  418. jj = 0;
  419. }
  420. udelay(1);
  421. }
  422. return err;
  423. }
  424. enum interrupted_state {
  425. DEFAULT_STATE = 0,
  426. INTERRUPTED_STATE = 1,
  427. RESTORED_STATE = 2,
  428. };
  429. /**
  430. * fastrpc_update_txmsg_buf - Update history of sent glink messages
  431. * @msg : Pointer to RPC message to remote subsystem
  432. * @transport_send_err : Error from transport
  433. * @ns : Timestamp (in ns) of sent message
  434. * @xo_time_in_us : XO Timestamp (in us) of sent message
  435. * @ctx : invoke ctx
  436. * @interrupted : 0/1/2 (default/interrupted/restored)
  437. *
  438. * Returns none
  439. */
  440. static inline void fastrpc_update_txmsg_buf(struct smq_msg *msg,
  441. int transport_send_err, int64_t ns, uint64_t xo_time_in_us,
  442. struct smq_invoke_ctx *ctx, enum interrupted_state interrupted)
  443. {
  444. unsigned long flags = 0;
  445. unsigned int tx_index = 0;
  446. struct fastrpc_tx_msg *tx_msg = NULL;
  447. struct fastrpc_channel_ctx *chan = NULL;
  448. struct fastrpc_file *fl = ctx->fl;
  449. int err = 0, cid = -1;
  450. if (!fl) {
  451. err = -EBADF;
  452. goto bail;
  453. }
  454. cid = fl->cid;
  455. VERIFY(err, VALID_FASTRPC_CID(cid));
  456. if (err) {
  457. err = -ECHRNG;
  458. goto bail;
  459. }
  460. chan = &fl->apps->channel[cid];
  461. spin_lock_irqsave(&chan->gmsg_log.lock, flags);
  462. if (interrupted){
  463. if (ctx->tx_index >= 0 && ctx->tx_index < GLINK_MSG_HISTORY_LEN) {
  464. tx_msg = &chan->gmsg_log.tx_msgs[ctx->tx_index];
  465. if (tx_msg->msg.invoke.header.ctx == ctx->msg.invoke.header.ctx) {
  466. tx_msg->xo_time_in_us_interrupted = ctx->xo_time_in_us_interrupted;
  467. tx_msg->xo_time_in_us_restored = ctx->xo_time_in_us_restored;
  468. }
  469. }
  470. } else {
  471. tx_index = chan->gmsg_log.tx_index;
  472. ctx->tx_index = tx_index;
  473. tx_msg = &chan->gmsg_log.tx_msgs[tx_index];
  474. memcpy(&tx_msg->msg, msg, sizeof(struct smq_msg));
  475. tx_msg->transport_send_err = transport_send_err;
  476. tx_msg->ns = ns;
  477. tx_msg->xo_time_in_us = xo_time_in_us;
  478. tx_index++;
  479. chan->gmsg_log.tx_index =
  480. (tx_index > (GLINK_MSG_HISTORY_LEN - 1)) ? 0 : tx_index;
  481. }
  482. spin_unlock_irqrestore(&chan->gmsg_log.lock, flags);
  483. bail:
  484. if (err)
  485. ADSPRPC_ERR("adsprpc: %s: unable to update txmsg buf (err %d) for ctx: 0x%x\n",
  486. __func__, err, ctx->msg.invoke.header.ctx);
  487. }
  488. /**
  489. * fastrpc_update_rxmsg_buf - Update history of received glink responses
  490. * @chan : Channel context
  491. * @ctx : Context of received response from DSP
  492. * @retval : Return value for RPC call
  493. * @rsp_flags : Response type
  494. * @early_wake_time : Poll time for early wakeup
  495. * @ver : Version of response
  496. * @ns : Timestamp (in ns) of response
  497. * @xo_time_in_us : XO Timestamp (in us) of response
  498. *
  499. * Returns none
  500. */
  501. static inline void fastrpc_update_rxmsg_buf(struct fastrpc_channel_ctx *chan,
  502. uint64_t ctx, int retval, uint32_t rsp_flags,
  503. uint32_t early_wake_time, uint32_t ver, int64_t ns, uint64_t xo_time_in_us)
  504. {
  505. unsigned long flags = 0;
  506. unsigned int rx_index = 0;
  507. struct fastrpc_rx_msg *rx_msg = NULL;
  508. struct smq_invoke_rspv2 *rsp = NULL;
  509. spin_lock_irqsave(&chan->gmsg_log.lock, flags);
  510. rx_index = chan->gmsg_log.rx_index;
  511. rx_msg = &chan->gmsg_log.rx_msgs[rx_index];
  512. rsp = &rx_msg->rsp;
  513. rsp->ctx = ctx;
  514. rsp->retval = retval;
  515. rsp->flags = rsp_flags;
  516. rsp->early_wake_time = early_wake_time;
  517. rsp->version = ver;
  518. rx_msg->ns = ns;
  519. rx_msg->xo_time_in_us = xo_time_in_us;
  520. rx_index++;
  521. chan->gmsg_log.rx_index =
  522. (rx_index > (GLINK_MSG_HISTORY_LEN - 1)) ? 0 : rx_index;
  523. spin_unlock_irqrestore(&chan->gmsg_log.lock, flags);
  524. }
  525. static inline int get_unique_index(void)
  526. {
  527. int index = -1;
  528. mutex_lock(&gfa.mut_uid);
  529. for (index = 0; index < MAX_UNIQUE_ID; index++) {
  530. if (md_unique_index_flag[index] == 0) {
  531. md_unique_index_flag[index] = 1;
  532. mutex_unlock(&gfa.mut_uid);
  533. return index;
  534. }
  535. }
  536. mutex_unlock(&gfa.mut_uid);
  537. return index;
  538. }
  539. static inline void reset_unique_index(int index)
  540. {
  541. mutex_lock(&gfa.mut_uid);
  542. if (index > -1 && index < MAX_UNIQUE_ID)
  543. md_unique_index_flag[index] = 0;
  544. mutex_unlock(&gfa.mut_uid);
  545. }
  546. /**
  547. * fastrpc_minidump_add_region - Add mini dump region
  548. * @fastrpc_mmap : Input structure mmap
  549. *
  550. * Returns int
  551. */
  552. static int fastrpc_minidump_add_region(struct fastrpc_mmap *map)
  553. {
  554. int err = 0, ret_val = 0, md_index = 0;
  555. struct md_region md_entry;
  556. md_index = get_unique_index();
  557. if (md_index > -1 && md_index < MAX_UNIQUE_ID) {
  558. scnprintf(md_entry.name, MAX_NAME_LENGTH, "FRPC_%d", md_index);
  559. md_entry.virt_addr = map->va;
  560. md_entry.phys_addr = map->phys;
  561. md_entry.size = map->size;
  562. ret_val = msm_minidump_add_region(&md_entry);
  563. if (ret_val < 0) {
  564. ADSPRPC_ERR(
  565. "Failed to add/update CMA to Minidump for phys: 0x%llx, size: %zu, md_index %d, md_entry.name %s\n",
  566. map->phys,
  567. map->size, md_index,
  568. md_entry.name);
  569. reset_unique_index(md_index);
  570. err = ret_val;
  571. } else {
  572. map->frpc_md_index = md_index;
  573. }
  574. } else {
  575. pr_warn("failed to generate valid unique id for mini dump : %d\n", md_index);
  576. }
  577. return err;
  578. }
  579. /**
  580. * fastrpc_minidump_remove_region - Remove mini dump region if added
  581. * @fastrpc_mmap : Input structure mmap
  582. *
  583. * Returns int
  584. */
  585. static int fastrpc_minidump_remove_region(struct fastrpc_mmap *map)
  586. {
  587. int err = -EINVAL;
  588. struct md_region md_entry;
  589. if (map->frpc_md_index > -1 && map->frpc_md_index < MAX_UNIQUE_ID) {
  590. scnprintf(md_entry.name, MAX_NAME_LENGTH, "FRPC_%d",
  591. map->frpc_md_index);
  592. md_entry.virt_addr = map->va;
  593. md_entry.phys_addr = map->phys;
  594. md_entry.size = map->size;
  595. err = msm_minidump_remove_region(&md_entry);
  596. if (err < 0) {
  597. ADSPRPC_ERR(
  598. "Failed to remove CMA from Minidump for phys: 0x%llx, size: %zu index = %d\n",
  599. map->phys, map->size, map->frpc_md_index);
  600. } else {
  601. reset_unique_index(map->frpc_md_index);
  602. map->frpc_md_index = -1;
  603. }
  604. } else {
  605. ADSPRPC_WARN("mini-dump enabled with invalid unique id: %d\n", map->frpc_md_index);
  606. }
  607. return err;
  608. }
  609. static void fastrpc_buf_free(struct fastrpc_buf *buf, int cache)
  610. {
  611. struct fastrpc_file *fl = buf == NULL ? NULL : buf->fl;
  612. int vmid, err = 0, cid = -1;
  613. if (!fl)
  614. return;
  615. if (buf->in_use) {
  616. /* Don't free persistent header buf. Just mark as available */
  617. spin_lock(&fl->hlock);
  618. buf->in_use = false;
  619. spin_unlock(&fl->hlock);
  620. return;
  621. }
  622. if (cache && buf->size < MAX_CACHE_BUF_SIZE) {
  623. spin_lock(&fl->hlock);
  624. if (fl->num_cached_buf > MAX_CACHED_BUFS) {
  625. spin_unlock(&fl->hlock);
  626. goto skip_buf_cache;
  627. }
  628. hlist_add_head(&buf->hn, &fl->cached_bufs);
  629. fl->num_cached_buf++;
  630. buf->type = -1;
  631. spin_unlock(&fl->hlock);
  632. return;
  633. }
  634. skip_buf_cache:
  635. if (buf->type == USERHEAP_BUF) {
  636. spin_lock(&fl->hlock);
  637. hlist_del_init(&buf->hn_rem);
  638. spin_unlock(&fl->hlock);
  639. buf->raddr = 0;
  640. }
  641. if (!IS_ERR_OR_NULL(buf->virt)) {
  642. VERIFY(err, fl->sctx != NULL);
  643. if (err)
  644. goto bail;
  645. if (fl->sctx->smmu.cb)
  646. buf->phys &= ~((uint64_t)fl->sctx->smmu.cb << 32);
  647. cid = fl->cid;
  648. VERIFY(err, VALID_FASTRPC_CID(cid));
  649. if (err) {
  650. err = -ECHRNG;
  651. ADSPRPC_ERR(
  652. "invalid channel 0x%zx set for session\n",
  653. cid);
  654. goto bail;
  655. }
  656. vmid = fl->apps->channel[cid].vmid;
  657. if ((vmid) && (fl->apps->channel[cid].in_hib == 0)) {
  658. u64 src_perms = BIT(QCOM_SCM_VMID_HLOS)| BIT(vmid);
  659. struct qcom_scm_vmperm dest_perms = {0};
  660. int hyp_err = 0;
  661. dest_perms.vmid = QCOM_SCM_VMID_HLOS;
  662. dest_perms.perm = QCOM_SCM_PERM_RWX;
  663. hyp_err = qcom_scm_assign_mem(buf->phys,
  664. buf_page_size(buf->size),
  665. &src_perms, &dest_perms, 1);
  666. if (hyp_err) {
  667. ADSPRPC_ERR(
  668. "rh hyp unassign failed with %d for phys 0x%llx, size %zu\n",
  669. hyp_err, buf->phys, buf->size);
  670. }
  671. }
  672. trace_fastrpc_dma_free(cid, buf->phys, buf->size);
  673. dma_free_attrs(fl->sctx->smmu.dev, buf->size, buf->virt,
  674. buf->phys, buf->dma_attr);
  675. }
  676. bail:
  677. kfree(buf);
  678. }
  679. static void fastrpc_cached_buf_list_free(struct fastrpc_file *fl)
  680. {
  681. struct fastrpc_buf *buf, *free;
  682. do {
  683. struct hlist_node *n;
  684. free = NULL;
  685. spin_lock(&fl->hlock);
  686. hlist_for_each_entry_safe(buf, n, &fl->cached_bufs, hn) {
  687. hlist_del_init(&buf->hn);
  688. fl->num_cached_buf--;
  689. free = buf;
  690. break;
  691. }
  692. spin_unlock(&fl->hlock);
  693. if (free)
  694. fastrpc_buf_free(free, 0);
  695. } while (free);
  696. }
  697. static void fastrpc_remote_buf_list_free(struct fastrpc_file *fl)
  698. {
  699. struct fastrpc_buf *buf, *free;
  700. do {
  701. struct hlist_node *n;
  702. free = NULL;
  703. spin_lock(&fl->hlock);
  704. hlist_for_each_entry_safe(buf, n, &fl->remote_bufs, hn_rem) {
  705. free = buf;
  706. break;
  707. }
  708. spin_unlock(&fl->hlock);
  709. if (free)
  710. fastrpc_buf_free(free, 0);
  711. } while (free);
  712. }
  713. static void fastrpc_mmap_add(struct fastrpc_mmap *map)
  714. {
  715. if (map->flags == ADSP_MMAP_HEAP_ADDR ||
  716. map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
  717. struct fastrpc_apps *me = &gfa;
  718. unsigned long irq_flags = 0;
  719. spin_lock_irqsave(&me->hlock, irq_flags);
  720. hlist_add_head(&map->hn, &me->maps);
  721. spin_unlock_irqrestore(&me->hlock, irq_flags);
  722. } else {
  723. struct fastrpc_file *fl = map->fl;
  724. hlist_add_head(&map->hn, &fl->maps);
  725. }
  726. }
  727. static int fastrpc_mmap_find(struct fastrpc_file *fl, int fd,
  728. struct dma_buf *buf, uintptr_t va, size_t len, int mflags, int refs,
  729. struct fastrpc_mmap **ppmap)
  730. {
  731. struct fastrpc_apps *me = &gfa;
  732. struct fastrpc_mmap *match = NULL, *map = NULL;
  733. struct hlist_node *n;
  734. unsigned long irq_flags = 0;
  735. if ((va + len) < va)
  736. return -EFAULT;
  737. if (mflags == ADSP_MMAP_HEAP_ADDR ||
  738. mflags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
  739. spin_lock_irqsave(&me->hlock, irq_flags);
  740. hlist_for_each_entry_safe(map, n, &me->maps, hn) {
  741. if (va >= map->va &&
  742. va + len <= map->va + map->len &&
  743. map->fd == fd) {
  744. if (refs) {
  745. if (map->refs + 1 == INT_MAX) {
  746. spin_unlock_irqrestore(&me->hlock, irq_flags);
  747. return -ETOOMANYREFS;
  748. }
  749. map->refs++;
  750. }
  751. match = map;
  752. break;
  753. }
  754. }
  755. spin_unlock_irqrestore(&me->hlock, irq_flags);
  756. } else if (mflags == ADSP_MMAP_DMA_BUFFER) {
  757. hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
  758. if (map->buf == buf) {
  759. if (refs) {
  760. if (map->refs + 1 == INT_MAX)
  761. return -ETOOMANYREFS;
  762. map->refs++;
  763. }
  764. match = map;
  765. break;
  766. }
  767. }
  768. } else {
  769. hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
  770. if (va >= map->va &&
  771. va + len <= map->va + map->len &&
  772. map->fd == fd) {
  773. if (refs) {
  774. if (map->refs + 1 == INT_MAX)
  775. return -ETOOMANYREFS;
  776. map->refs++;
  777. }
  778. match = map;
  779. break;
  780. }
  781. }
  782. }
  783. if (match) {
  784. *ppmap = match;
  785. return 0;
  786. }
  787. return -ENXIO;
  788. }
  789. static int fastrpc_alloc_cma_memory(dma_addr_t *region_phys, void **vaddr,
  790. size_t size, unsigned long dma_attr)
  791. {
  792. int err = 0;
  793. struct fastrpc_apps *me = &gfa;
  794. if (me->dev == NULL) {
  795. ADSPRPC_ERR(
  796. "failed to allocate CMA memory, device adsprpc-mem is not initialized\n");
  797. return -ENODEV;
  798. }
  799. VERIFY(err, size > 0 && size < me->max_size_limit);
  800. if (err) {
  801. err = -EFAULT;
  802. pr_err("adsprpc: %s: invalid allocation size 0x%zx\n",
  803. __func__, size);
  804. return err;
  805. }
  806. *vaddr = dma_alloc_attrs(me->dev, size, region_phys,
  807. GFP_KERNEL, dma_attr);
  808. if (IS_ERR_OR_NULL(*vaddr)) {
  809. ADSPRPC_ERR(
  810. "dma_alloc_attrs failed for device %s size 0x%zx dma_attr %lu, returned %ld\n",
  811. dev_name(me->dev), size, dma_attr, PTR_ERR(*vaddr));
  812. return -ENOBUFS;
  813. }
  814. return 0;
  815. }
  816. static int fastrpc_mmap_remove(struct fastrpc_file *fl, int fd, uintptr_t va,
  817. size_t len, struct fastrpc_mmap **ppmap)
  818. {
  819. struct fastrpc_mmap *match = NULL, *map;
  820. struct hlist_node *n;
  821. struct fastrpc_apps *me = &gfa;
  822. unsigned long irq_flags = 0;
  823. /*
  824. * Search for a mapping by matching fd, remote address and length.
  825. * For backward compatibility, search for a mapping by matching is
  826. * limited to remote address and length when passed fd < 0.
  827. */
  828. spin_lock_irqsave(&me->hlock, irq_flags);
  829. hlist_for_each_entry_safe(map, n, &me->maps, hn) {
  830. if ((fd < 0 || map->fd == fd) && map->raddr == va &&
  831. map->raddr + map->len == va + len &&
  832. map->refs == 1 && !map->is_persistent &&
  833. /* Skip unmap if it is fastrpc shell memory */
  834. !map->is_filemap) {
  835. match = map;
  836. hlist_del_init(&map->hn);
  837. break;
  838. }
  839. }
  840. spin_unlock_irqrestore(&me->hlock, irq_flags);
  841. if (match) {
  842. *ppmap = match;
  843. return 0;
  844. }
  845. hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
  846. if ((fd < 0 || map->fd == fd) && map->raddr == va &&
  847. map->raddr + map->len == va + len &&
  848. map->refs == 1 &&
  849. /* Remove map only if it isn't being used in any pending RPC calls */
  850. !map->ctx_refs &&
  851. /* Skip unmap if it is fastrpc shell memory */
  852. !map->is_filemap) {
  853. match = map;
  854. hlist_del_init(&map->hn);
  855. break;
  856. }
  857. }
  858. if (match) {
  859. *ppmap = match;
  860. return 0;
  861. }
  862. return -ETOOMANYREFS;
  863. }
  864. static void fastrpc_mmap_free(struct fastrpc_mmap *map, uint32_t flags)
  865. {
  866. struct fastrpc_apps *me = &gfa;
  867. struct fastrpc_file *fl;
  868. int vmid, cid = -1, err = 0;
  869. struct fastrpc_session_ctx *sess;
  870. unsigned long irq_flags = 0;
  871. if (!map)
  872. return;
  873. fl = map->fl;
  874. if (fl && !(map->flags == ADSP_MMAP_HEAP_ADDR ||
  875. map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR)) {
  876. cid = fl->cid;
  877. VERIFY(err, VALID_FASTRPC_CID(cid));
  878. if (err) {
  879. err = -ECHRNG;
  880. pr_err("adsprpc: ERROR:%s, Invalid channel id: %d, err:%d\n",
  881. __func__, cid, err);
  882. return;
  883. }
  884. }
  885. if (map->flags == ADSP_MMAP_HEAP_ADDR ||
  886. map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
  887. spin_lock_irqsave(&me->hlock, irq_flags);
  888. map->refs--;
  889. if (!map->refs && !map->is_persistent && !map->ctx_refs)
  890. hlist_del_init(&map->hn);
  891. spin_unlock_irqrestore(&me->hlock, irq_flags);
  892. if (map->refs > 0) {
  893. ADSPRPC_WARN(
  894. "multiple references for remote heap size %zu va 0x%lx ref count is %d\n",
  895. map->size, map->va, map->refs);
  896. return;
  897. }
  898. spin_lock_irqsave(&me->hlock, irq_flags);
  899. if (map->is_persistent && map->in_use)
  900. map->in_use = false;
  901. spin_unlock_irqrestore(&me->hlock, irq_flags);
  902. } else {
  903. map->refs--;
  904. if (!map->refs && !map->ctx_refs)
  905. hlist_del_init(&map->hn);
  906. if (map->refs > 0 && !flags)
  907. return;
  908. }
  909. if (map->flags == ADSP_MMAP_HEAP_ADDR ||
  910. map->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
  911. if (me->dev == NULL) {
  912. ADSPRPC_ERR(
  913. "failed to free remote heap allocation, device is not initialized\n");
  914. return;
  915. }
  916. if (msm_minidump_enabled() && !map->is_persistent)
  917. err = fastrpc_minidump_remove_region(map);
  918. if (map->phys && !map->is_persistent) {
  919. trace_fastrpc_dma_free(-1, map->phys, map->size);
  920. dma_free_attrs(me->dev, map->size, (void *)map->va,
  921. (dma_addr_t)map->phys, (unsigned long)map->attr);
  922. }
  923. } else if (map->flags == FASTRPC_MAP_FD_NOMAP) {
  924. trace_fastrpc_dma_unmap(cid, map->phys, map->size);
  925. if (!IS_ERR_OR_NULL(map->table))
  926. dma_buf_unmap_attachment(map->attach, map->table,
  927. DMA_BIDIRECTIONAL);
  928. if (!IS_ERR_OR_NULL(map->attach))
  929. dma_buf_detach(map->buf, map->attach);
  930. if (!IS_ERR_OR_NULL(map->buf))
  931. dma_buf_put(map->buf);
  932. } else {
  933. if (!fl)
  934. goto bail;
  935. if (map->secure)
  936. sess = fl->secsctx;
  937. else
  938. sess = fl->sctx;
  939. vmid = fl->apps->channel[cid].vmid;
  940. if (vmid && map->phys && (me->channel[cid].in_hib == 0)) {
  941. int hyp_err = 0;
  942. u64 src_perms = BIT(QCOM_SCM_VMID_HLOS) | BIT(vmid);
  943. struct qcom_scm_vmperm dst_perms = {0};
  944. dst_perms.vmid = QCOM_SCM_VMID_HLOS;
  945. dst_perms.perm = QCOM_SCM_PERM_RWX;
  946. hyp_err = qcom_scm_assign_mem(map->phys,
  947. buf_page_size(map->size),
  948. &src_perms, &dst_perms, 1);
  949. if (hyp_err) {
  950. ADSPRPC_ERR(
  951. "rh hyp unassign failed with %d for phys 0x%llx, size %zu\n",
  952. hyp_err, map->phys, map->size);
  953. }
  954. }
  955. trace_fastrpc_dma_unmap(cid, map->phys, map->size);
  956. if (!IS_ERR_OR_NULL(map->table))
  957. dma_buf_unmap_attachment(map->attach, map->table,
  958. DMA_BIDIRECTIONAL);
  959. if (!IS_ERR_OR_NULL(map->attach))
  960. dma_buf_detach(map->buf, map->attach);
  961. if (!IS_ERR_OR_NULL(map->buf))
  962. dma_buf_put(map->buf);
  963. }
  964. bail:
  965. if (!map->is_persistent)
  966. kfree(map);
  967. }
  968. static int fastrpc_session_alloc_secure_memory(
  969. struct fastrpc_channel_ctx *chan, int secure,
  970. int sharedcb, int pd_type, struct fastrpc_session_ctx **session);
  971. static inline bool fastrpc_get_persistent_map(size_t len, struct fastrpc_mmap **pers_map)
  972. {
  973. struct fastrpc_apps *me = &gfa;
  974. struct fastrpc_mmap *map = NULL;
  975. struct hlist_node *n = NULL;
  976. bool found = false;
  977. unsigned long irq_flags = 0;
  978. spin_lock_irqsave(&me->hlock, irq_flags);
  979. hlist_for_each_entry_safe(map, n, &me->maps, hn) {
  980. if (len == map->len &&
  981. map->is_persistent && !map->in_use) {
  982. *pers_map = map;
  983. map->in_use = true;
  984. /*
  985. * Incrementing map reference count when getting
  986. * the map to avoid negative reference count when
  987. * freeing the map.
  988. */
  989. map->refs++;
  990. found = true;
  991. break;
  992. }
  993. }
  994. spin_unlock_irqrestore(&me->hlock, irq_flags);
  995. return found;
  996. }
  997. static int fastrpc_mmap_create_remote_heap(struct fastrpc_file *fl,
  998. struct fastrpc_mmap *map, size_t len, int mflags)
  999. {
  1000. int err = 0;
  1001. struct fastrpc_apps *me = &gfa;
  1002. dma_addr_t region_phys = 0;
  1003. void *region_vaddr = NULL;
  1004. map->apps = me;
  1005. map->fl = NULL;
  1006. map->attr |= DMA_ATTR_NO_KERNEL_MAPPING;
  1007. err = fastrpc_alloc_cma_memory(&region_phys, &region_vaddr,
  1008. len, (unsigned long) map->attr);
  1009. if (err)
  1010. goto bail;
  1011. trace_fastrpc_dma_alloc(fl->cid, (uint64_t)region_phys, len,
  1012. (unsigned long)map->attr, mflags);
  1013. map->phys = (uintptr_t)region_phys;
  1014. map->size = len;
  1015. map->va = (uintptr_t)region_vaddr;
  1016. map->servloc_name = fl->servloc_name;
  1017. bail:
  1018. return err;
  1019. }
  1020. static int fastrpc_mmap_create(struct fastrpc_file *fl, int fd, struct dma_buf *buf,
  1021. unsigned int attr, uintptr_t va, size_t len, int mflags,
  1022. struct fastrpc_mmap **ppmap)
  1023. {
  1024. struct fastrpc_apps *me = &gfa;
  1025. struct fastrpc_session_ctx *sess;
  1026. struct fastrpc_apps *apps = NULL;
  1027. int cid = -1;
  1028. struct fastrpc_channel_ctx *chan = NULL;
  1029. struct fastrpc_mmap *map = NULL;
  1030. int err = 0, vmid, sgl_index = 0;
  1031. struct scatterlist *sgl = NULL;
  1032. if (!fl) {
  1033. err = -EBADF;
  1034. goto bail;
  1035. }
  1036. apps = fl->apps;
  1037. cid = fl->cid;
  1038. VERIFY(err, VALID_FASTRPC_CID(cid));
  1039. if (err) {
  1040. err = -ECHRNG;
  1041. goto bail;
  1042. }
  1043. chan = &apps->channel[cid];
  1044. if (!fastrpc_mmap_find(fl, fd, NULL, va, len, mflags, 1, ppmap))
  1045. return 0;
  1046. map = kzalloc(sizeof(*map), GFP_KERNEL);
  1047. VERIFY(err, !IS_ERR_OR_NULL(map));
  1048. if (err) {
  1049. err = -ENOMEM;
  1050. goto bail;
  1051. }
  1052. INIT_HLIST_NODE(&map->hn);
  1053. map->flags = mflags;
  1054. map->refs = 1;
  1055. map->fl = fl;
  1056. map->fd = fd;
  1057. map->attr = attr;
  1058. map->buf = buf;
  1059. map->frpc_md_index = -1;
  1060. map->is_filemap = false;
  1061. ktime_get_real_ts64(&map->map_start_time);
  1062. if (mflags == ADSP_MMAP_HEAP_ADDR ||
  1063. mflags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
  1064. VERIFY(err, 0 == (err = fastrpc_mmap_create_remote_heap(fl, map,
  1065. len, mflags)));
  1066. if (err)
  1067. goto bail;
  1068. if (msm_minidump_enabled()) {
  1069. err = fastrpc_minidump_add_region(map);
  1070. if (err)
  1071. goto bail;
  1072. }
  1073. } else if (mflags == FASTRPC_MAP_FD_NOMAP) {
  1074. VERIFY(err, !IS_ERR_OR_NULL(map->buf = dma_buf_get(fd)));
  1075. if (err) {
  1076. ADSPRPC_ERR("dma_buf_get failed for fd %d ret %ld\n",
  1077. fd, PTR_ERR(map->buf));
  1078. err = -EBADFD;
  1079. goto bail;
  1080. }
  1081. #if !IS_ENABLED(CONFIG_MSM_ADSPRPC_TRUSTED)
  1082. map->secure = (mem_buf_dma_buf_exclusive_owner(map->buf)) ? 0 : 1;
  1083. #endif
  1084. map->va = 0;
  1085. map->phys = 0;
  1086. VERIFY(err, !IS_ERR_OR_NULL(map->attach =
  1087. dma_buf_attach(map->buf, me->dev)));
  1088. if (err) {
  1089. ADSPRPC_ERR(
  1090. "dma_buf_attach for fd %d for len 0x%zx failed to map buffer on SMMU device %s ret %ld\n",
  1091. fd, len, dev_name(me->dev), PTR_ERR(map->attach));
  1092. err = -EFAULT;
  1093. goto bail;
  1094. }
  1095. map->attach->dma_map_attrs |= DMA_ATTR_SKIP_CPU_SYNC;
  1096. VERIFY(err, !IS_ERR_OR_NULL(map->table =
  1097. dma_buf_map_attachment(map->attach,
  1098. DMA_BIDIRECTIONAL)));
  1099. if (err) {
  1100. ADSPRPC_ERR(
  1101. "dma_buf_map_attachment for fd %d for len 0x%zx failed on device %s ret %ld\n",
  1102. fd, len, dev_name(me->dev), PTR_ERR(map->table));
  1103. err = -EFAULT;
  1104. goto bail;
  1105. }
  1106. VERIFY(err, map->table->nents == 1);
  1107. if (err) {
  1108. ADSPRPC_ERR(
  1109. "multiple scatter-gather entries (%u) present for NOMAP fd %d\n",
  1110. map->table->nents, fd);
  1111. err = -EFAULT;
  1112. goto bail;
  1113. }
  1114. map->phys = sg_dma_address(map->table->sgl);
  1115. map->size = len;
  1116. map->flags = FASTRPC_MAP_FD_DELAYED;
  1117. trace_fastrpc_dma_map(cid, fd, map->phys, map->size,
  1118. len, map->attach->dma_map_attrs, mflags);
  1119. } else {
  1120. if (map->attr && (map->attr & FASTRPC_ATTR_KEEP_MAP)) {
  1121. ADSPRPC_INFO("buffer mapped with persist attr 0x%x\n",
  1122. (unsigned int)map->attr);
  1123. map->refs = 2;
  1124. }
  1125. if (mflags == ADSP_MMAP_DMA_BUFFER) {
  1126. VERIFY(err, !IS_ERR_OR_NULL(map->buf));
  1127. if (err) {
  1128. ADSPRPC_ERR("Invalid DMA buffer address %pK\n",
  1129. map->buf);
  1130. err = -EFAULT;
  1131. goto bail;
  1132. }
  1133. /* Increment DMA buffer ref count,
  1134. * so that client cannot unmap DMA buffer, before freeing buffer
  1135. */
  1136. get_dma_buf(map->buf);
  1137. } else {
  1138. VERIFY(err, !IS_ERR_OR_NULL(map->buf = dma_buf_get(fd)));
  1139. if (err) {
  1140. ADSPRPC_ERR("dma_buf_get failed for fd %d ret %ld\n",
  1141. fd, PTR_ERR(map->buf));
  1142. err = -EBADFD;
  1143. goto bail;
  1144. }
  1145. }
  1146. #if !IS_ENABLED(CONFIG_MSM_ADSPRPC_TRUSTED)
  1147. map->secure = (mem_buf_dma_buf_exclusive_owner(map->buf)) ? 0 : 1;
  1148. #endif
  1149. if (map->secure) {
  1150. if (!fl->secsctx)
  1151. err = fastrpc_session_alloc_secure_memory(chan, 1,
  1152. me->share_securecb, fl->pd_type, &fl->secsctx);
  1153. if (err) {
  1154. ADSPRPC_ERR(
  1155. "fastrpc_session_alloc_secure_memory failed for fd %d ret %d\n",
  1156. fd, err);
  1157. err = -ENOSR;
  1158. goto bail;
  1159. }
  1160. }
  1161. if (map->secure)
  1162. sess = fl->secsctx;
  1163. else
  1164. sess = fl->sctx;
  1165. VERIFY(err, !IS_ERR_OR_NULL(sess));
  1166. if (err) {
  1167. ADSPRPC_ERR(
  1168. "session is invalid for fd %d, secure flag %d\n",
  1169. fd, map->secure);
  1170. err = -EBADR;
  1171. goto bail;
  1172. }
  1173. VERIFY(err, !IS_ERR_OR_NULL(map->attach =
  1174. dma_buf_attach(map->buf, sess->smmu.dev)));
  1175. if (err) {
  1176. ADSPRPC_ERR(
  1177. "dma_buf_attach for fd %d failed for len 0x%zx to map buffer on SMMU device %s ret %ld\n",
  1178. fd, len, dev_name(sess->smmu.dev),
  1179. PTR_ERR(map->attach));
  1180. err = -EFAULT;
  1181. goto bail;
  1182. }
  1183. map->attach->dma_map_attrs |= DMA_ATTR_DELAYED_UNMAP;
  1184. /*
  1185. * Skip CPU sync if IO Cohernecy is not supported
  1186. */
  1187. if (!sess->smmu.coherent)
  1188. map->attach->dma_map_attrs |= DMA_ATTR_SKIP_CPU_SYNC;
  1189. VERIFY(err, !IS_ERR_OR_NULL(map->table =
  1190. dma_buf_map_attachment(map->attach,
  1191. DMA_BIDIRECTIONAL)));
  1192. if (err) {
  1193. ADSPRPC_ERR(
  1194. "dma_buf_map_attachment for fd %d failed for len 0x%zx on device %s ret %ld\n",
  1195. fd, len, dev_name(sess->smmu.dev),
  1196. PTR_ERR(map->table));
  1197. err = -EFAULT;
  1198. goto bail;
  1199. }
  1200. if (!sess->smmu.enabled) {
  1201. VERIFY(err, map->table->nents == 1);
  1202. if (err) {
  1203. ADSPRPC_ERR(
  1204. "multiple scatter-gather entries (%u) present for fd %d mapped on SMMU disabled device\n",
  1205. map->table->nents, fd);
  1206. err = -EFAULT;
  1207. goto bail;
  1208. }
  1209. }
  1210. map->phys = sg_dma_address(map->table->sgl);
  1211. if (sess->smmu.cb) {
  1212. map->phys += ((uint64_t)sess->smmu.cb << 32);
  1213. for_each_sg(map->table->sgl, sgl, map->table->nents,
  1214. sgl_index)
  1215. map->size += sg_dma_len(sgl);
  1216. } else {
  1217. map->size = buf_page_size(len);
  1218. }
  1219. trace_fastrpc_dma_map(cid, fd, map->phys, map->size,
  1220. len, map->attach->dma_map_attrs, mflags);
  1221. VERIFY(err, map->size >= len && map->size < me->max_size_limit);
  1222. if (err) {
  1223. err = -EFAULT;
  1224. pr_err("adsprpc: %s: invalid map size 0x%zx len 0x%zx\n",
  1225. __func__, map->size, len);
  1226. goto bail;
  1227. }
  1228. vmid = fl->apps->channel[cid].vmid;
  1229. if (vmid) {
  1230. u64 src_perms = BIT(QCOM_SCM_VMID_HLOS);
  1231. struct qcom_scm_vmperm dst_perms[2] = {0};
  1232. dst_perms[0].vmid = QCOM_SCM_VMID_HLOS;
  1233. dst_perms[0].perm = QCOM_SCM_PERM_RW;
  1234. dst_perms[1].vmid = vmid;
  1235. dst_perms[1].perm = QCOM_SCM_PERM_RWX;
  1236. err = qcom_scm_assign_mem(map->phys,
  1237. buf_page_size(map->size),
  1238. &src_perms, dst_perms, 2);
  1239. if (err) {
  1240. ADSPRPC_ERR(
  1241. "rh hyp assign failed with %d for phys 0x%llx, size %zu\n",
  1242. err, map->phys, map->size);
  1243. err = -EADDRNOTAVAIL;
  1244. goto bail;
  1245. }
  1246. }
  1247. map->va = va;
  1248. }
  1249. map->len = len;
  1250. fastrpc_mmap_add(map);
  1251. *ppmap = map;
  1252. bail:
  1253. if (map)
  1254. ktime_get_real_ts64(&map->map_end_time);
  1255. if (err && map)
  1256. fastrpc_mmap_free(map, 0);
  1257. return err;
  1258. }
  1259. static inline bool fastrpc_get_cached_buf(struct fastrpc_file *fl,
  1260. size_t size, int buf_type, struct fastrpc_buf **obuf)
  1261. {
  1262. bool found = false;
  1263. struct fastrpc_buf *buf = NULL, *fr = NULL;
  1264. struct hlist_node *n = NULL;
  1265. if (buf_type == USERHEAP_BUF)
  1266. goto bail;
  1267. /* find the smallest buffer that fits in the cache */
  1268. spin_lock(&fl->hlock);
  1269. hlist_for_each_entry_safe(buf, n, &fl->cached_bufs, hn) {
  1270. if (buf->size >= size && (!fr || fr->size > buf->size))
  1271. fr = buf;
  1272. }
  1273. if (fr) {
  1274. hlist_del_init(&fr->hn);
  1275. fl->num_cached_buf--;
  1276. }
  1277. spin_unlock(&fl->hlock);
  1278. if (fr) {
  1279. fr->type = buf_type;
  1280. *obuf = fr;
  1281. found = true;
  1282. }
  1283. bail:
  1284. return found;
  1285. }
  1286. static inline bool fastrpc_get_persistent_buf(struct fastrpc_file *fl,
  1287. size_t size, int buf_type, struct fastrpc_buf **obuf)
  1288. {
  1289. unsigned int i = 0;
  1290. bool found = false;
  1291. struct fastrpc_buf *buf = NULL;
  1292. spin_lock(&fl->hlock);
  1293. if (!fl->num_pers_hdrs)
  1294. goto bail;
  1295. /*
  1296. * Persistent header buffer can be used only if
  1297. * metadata length is less than 1 page size.
  1298. */
  1299. if (buf_type != METADATA_BUF || size > PAGE_SIZE)
  1300. goto bail;
  1301. for (i = 0; i < fl->num_pers_hdrs; i++) {
  1302. buf = &fl->hdr_bufs[i];
  1303. /* If buffer not in use, then assign it for requested alloc */
  1304. if (!buf->in_use) {
  1305. buf->in_use = true;
  1306. *obuf = buf;
  1307. found = true;
  1308. break;
  1309. }
  1310. }
  1311. bail:
  1312. spin_unlock(&fl->hlock);
  1313. return found;
  1314. }
  1315. static int fastrpc_buf_alloc(struct fastrpc_file *fl, size_t size,
  1316. unsigned long dma_attr, uint32_t rflags,
  1317. int buf_type, struct fastrpc_buf **obuf)
  1318. {
  1319. int err = 0, vmid;
  1320. struct fastrpc_apps *me = &gfa;
  1321. struct fastrpc_buf *buf = NULL;
  1322. int cid = -1;
  1323. VERIFY(err, fl && fl->sctx != NULL);
  1324. if (err) {
  1325. err = -EBADR;
  1326. goto bail;
  1327. }
  1328. cid = fl->cid;
  1329. VERIFY(err, VALID_FASTRPC_CID(cid));
  1330. if (err) {
  1331. err = -ECHRNG;
  1332. goto bail;
  1333. }
  1334. VERIFY(err, size > 0 && size < me->max_size_limit);
  1335. if (err) {
  1336. err = -EFAULT;
  1337. pr_err("adsprpc: %s: invalid allocation size 0x%zx\n",
  1338. __func__, size);
  1339. goto bail;
  1340. }
  1341. VERIFY(err, size > 0 && fl->sctx->smmu.dev);
  1342. if (err) {
  1343. err = (fl->sctx->smmu.dev == NULL) ? -ENODEV : err;
  1344. goto bail;
  1345. }
  1346. if (fastrpc_get_persistent_buf(fl, size, buf_type, obuf))
  1347. return err;
  1348. if (fastrpc_get_cached_buf(fl, size, buf_type, obuf))
  1349. return err;
  1350. /* If unable to get persistent or cached buf, allocate new buffer */
  1351. VERIFY(err, NULL != (buf = kzalloc(sizeof(*buf), GFP_KERNEL)));
  1352. if (err) {
  1353. err = -ENOMEM;
  1354. goto bail;
  1355. }
  1356. INIT_HLIST_NODE(&buf->hn);
  1357. buf->fl = fl;
  1358. buf->virt = NULL;
  1359. buf->phys = 0;
  1360. buf->size = size;
  1361. buf->dma_attr = dma_attr;
  1362. buf->flags = rflags;
  1363. buf->raddr = 0;
  1364. buf->type = buf_type;
  1365. ktime_get_real_ts64(&buf->buf_start_time);
  1366. buf->virt = dma_alloc_attrs(fl->sctx->smmu.dev, buf->size,
  1367. (dma_addr_t *)&buf->phys,
  1368. GFP_KERNEL, buf->dma_attr);
  1369. if (IS_ERR_OR_NULL(buf->virt)) {
  1370. /* free cache and retry */
  1371. fastrpc_cached_buf_list_free(fl);
  1372. buf->virt = dma_alloc_attrs(fl->sctx->smmu.dev, buf->size,
  1373. (dma_addr_t *)&buf->phys, GFP_KERNEL,
  1374. buf->dma_attr);
  1375. VERIFY(err, !IS_ERR_OR_NULL(buf->virt));
  1376. }
  1377. if (err) {
  1378. ADSPRPC_ERR(
  1379. "dma_alloc_attrs failed for size 0x%zx, returned %pK\n",
  1380. size, buf->virt);
  1381. err = -ENOBUFS;
  1382. goto bail;
  1383. }
  1384. if (fl->sctx->smmu.cb)
  1385. buf->phys += ((uint64_t)fl->sctx->smmu.cb << 32);
  1386. trace_fastrpc_dma_alloc(cid, buf->phys, size,
  1387. dma_attr, (int)rflags);
  1388. vmid = fl->apps->channel[cid].vmid;
  1389. if (vmid) {
  1390. u64 src_perms = BIT(QCOM_SCM_VMID_HLOS);
  1391. struct qcom_scm_vmperm dst_perms[2] = {0};
  1392. dst_perms[0].vmid = QCOM_SCM_VMID_HLOS;
  1393. dst_perms[0].perm = QCOM_SCM_PERM_RW;
  1394. dst_perms[1].vmid = vmid;
  1395. dst_perms[1].perm = QCOM_SCM_PERM_RWX;
  1396. err = qcom_scm_assign_mem(buf->phys, buf_page_size(size),
  1397. &src_perms, dst_perms, 2);
  1398. if (err) {
  1399. ADSPRPC_DEBUG(
  1400. "rh hyp assign failed with %d for phys 0x%llx, size %zu\n",
  1401. err, buf->phys, size);
  1402. err = -EADDRNOTAVAIL;
  1403. goto bail;
  1404. }
  1405. }
  1406. if (buf_type == USERHEAP_BUF) {
  1407. INIT_HLIST_NODE(&buf->hn_rem);
  1408. spin_lock(&fl->hlock);
  1409. hlist_add_head(&buf->hn_rem, &fl->remote_bufs);
  1410. spin_unlock(&fl->hlock);
  1411. }
  1412. *obuf = buf;
  1413. bail:
  1414. if (buf)
  1415. ktime_get_real_ts64(&buf->buf_end_time);
  1416. if (err && buf)
  1417. fastrpc_buf_free(buf, 0);
  1418. return err;
  1419. }
  1420. static int context_restore_interrupted(struct fastrpc_file *fl,
  1421. struct fastrpc_ioctl_invoke_async *inv,
  1422. struct smq_invoke_ctx **po)
  1423. {
  1424. int err = 0;
  1425. struct smq_invoke_ctx *ctx = NULL, *ictx = NULL;
  1426. struct hlist_node *n;
  1427. struct fastrpc_ioctl_invoke *invoke = &inv->inv;
  1428. spin_lock(&fl->hlock);
  1429. hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) {
  1430. if (ictx->pid == current->pid) {
  1431. if (invoke->sc != ictx->sc || ictx->fl != fl) {
  1432. err = -EINVAL;
  1433. ictx->sc_interrupted = invoke->sc;
  1434. ictx->fl_interrupted = fl;
  1435. ictx->handle_interrupted = invoke->handle;
  1436. ADSPRPC_ERR(
  1437. "interrupted sc (0x%x) or fl (%pK) does not match with invoke sc (0x%x) or fl (%pK)\n",
  1438. ictx->sc, ictx->fl, invoke->sc, fl);
  1439. } else {
  1440. ictx->xo_time_in_us_restored = CONVERT_CNT_TO_US(__arch_counter_get_cntvct());
  1441. fastrpc_update_txmsg_buf(NULL, 0, 0, 0, ictx, RESTORED_STATE);
  1442. ADSPRPC_DEBUG(
  1443. "restored sc (0x%x) of fl (%pK), interrupt ts 0x%llx, restore ts 0x%llx \n",
  1444. ictx->sc, ictx->fl, ictx->xo_time_in_us_interrupted, ictx->xo_time_in_us_restored);
  1445. ctx = ictx;
  1446. hlist_del_init(&ctx->hn);
  1447. hlist_add_head(&ctx->hn, &fl->clst.pending);
  1448. }
  1449. break;
  1450. }
  1451. }
  1452. spin_unlock(&fl->hlock);
  1453. if (ctx)
  1454. *po = ctx;
  1455. return err;
  1456. }
  1457. static unsigned int sorted_lists_intersection(unsigned int *listA,
  1458. unsigned int lenA, unsigned int *listB, unsigned int lenB)
  1459. {
  1460. unsigned int i = 0, j = 0;
  1461. while (i < lenA && j < lenB) {
  1462. if (listA[i] < listB[j])
  1463. i++;
  1464. else if (listA[i] > listB[j])
  1465. j++;
  1466. else
  1467. return listA[i];
  1468. }
  1469. return 0;
  1470. }
  1471. #define CMP(aa, bb) ((aa) == (bb) ? 0 : (aa) < (bb) ? -1 : 1)
  1472. static int uint_cmp_func(const void *p1, const void *p2)
  1473. {
  1474. unsigned int a1 = *((unsigned int *)p1);
  1475. unsigned int a2 = *((unsigned int *)p2);
  1476. return CMP(a1, a2);
  1477. }
  1478. static int overlap_ptr_cmp(const void *a, const void *b)
  1479. {
  1480. struct overlap *pa = *((struct overlap **)a);
  1481. struct overlap *pb = *((struct overlap **)b);
  1482. /* sort with lowest starting buffer first */
  1483. int st = CMP(pa->start, pb->start);
  1484. /* sort with highest ending buffer first */
  1485. int ed = CMP(pb->end, pa->end);
  1486. return st == 0 ? ed : st;
  1487. }
  1488. static int context_build_overlap(struct smq_invoke_ctx *ctx)
  1489. {
  1490. int i, err = 0;
  1491. remote_arg_t *lpra = ctx->lpra;
  1492. int inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
  1493. int outbufs = REMOTE_SCALARS_OUTBUFS(ctx->sc);
  1494. int nbufs = inbufs + outbufs;
  1495. struct overlap max;
  1496. for (i = 0; i < nbufs; ++i) {
  1497. ctx->overs[i].start = (uintptr_t)lpra[i].buf.pv;
  1498. ctx->overs[i].end = ctx->overs[i].start + lpra[i].buf.len;
  1499. if (lpra[i].buf.len) {
  1500. VERIFY(err, ctx->overs[i].end > ctx->overs[i].start);
  1501. if (err) {
  1502. err = -EFAULT;
  1503. ADSPRPC_ERR(
  1504. "Invalid address 0x%llx and size %zu\n",
  1505. (uintptr_t)lpra[i].buf.pv,
  1506. lpra[i].buf.len);
  1507. goto bail;
  1508. }
  1509. }
  1510. ctx->overs[i].raix = i;
  1511. ctx->overps[i] = &ctx->overs[i];
  1512. }
  1513. sort(ctx->overps, nbufs, sizeof(*ctx->overps), overlap_ptr_cmp, NULL);
  1514. max.start = 0;
  1515. max.end = 0;
  1516. for (i = 0; i < nbufs; ++i) {
  1517. if (ctx->overps[i]->start < max.end) {
  1518. ctx->overps[i]->mstart = max.end;
  1519. ctx->overps[i]->mend = ctx->overps[i]->end;
  1520. ctx->overps[i]->offset = max.end -
  1521. ctx->overps[i]->start;
  1522. if (ctx->overps[i]->end > max.end) {
  1523. max.end = ctx->overps[i]->end;
  1524. } else {
  1525. if ((max.raix < inbufs &&
  1526. ctx->overps[i]->raix + 1 > inbufs) ||
  1527. (ctx->overps[i]->raix < inbufs &&
  1528. max.raix + 1 > inbufs))
  1529. ctx->overps[i]->do_cmo = 1;
  1530. ctx->overps[i]->mend = 0;
  1531. ctx->overps[i]->mstart = 0;
  1532. }
  1533. } else {
  1534. ctx->overps[i]->mend = ctx->overps[i]->end;
  1535. ctx->overps[i]->mstart = ctx->overps[i]->start;
  1536. ctx->overps[i]->offset = 0;
  1537. max = *ctx->overps[i];
  1538. }
  1539. }
  1540. bail:
  1541. return err;
  1542. }
  1543. #define K_COPY_FROM_USER(err, kernel, dst, src, size) \
  1544. do {\
  1545. if (!(kernel))\
  1546. err = copy_from_user((dst),\
  1547. (void const __user *)(src),\
  1548. (size));\
  1549. else\
  1550. memmove((dst), (src), (size));\
  1551. } while (0)
  1552. #define K_COPY_TO_USER(err, kernel, dst, src, size) \
  1553. do {\
  1554. if (!(kernel))\
  1555. err = copy_to_user((void __user *)(dst),\
  1556. (src), (size));\
  1557. else\
  1558. memmove((dst), (src), (size));\
  1559. } while (0)
  1560. static void context_free(struct smq_invoke_ctx *ctx);
  1561. static int context_alloc(struct fastrpc_file *fl, uint32_t kernel,
  1562. struct fastrpc_ioctl_invoke_async *invokefd,
  1563. struct smq_invoke_ctx **po)
  1564. {
  1565. struct fastrpc_apps *me = &gfa;
  1566. int err = 0, bufs, ii, size = 0, cid = fl->cid;
  1567. struct smq_invoke_ctx *ctx = NULL;
  1568. struct fastrpc_ctx_lst *clst = &fl->clst;
  1569. struct fastrpc_ioctl_invoke *invoke = &invokefd->inv;
  1570. struct fastrpc_channel_ctx *chan = NULL;
  1571. unsigned long irq_flags = 0;
  1572. uint32_t is_kernel_memory = 0;
  1573. spin_lock(&fl->hlock);
  1574. if (fl->clst.num_active_ctxs > MAX_PENDING_CTX_PER_SESSION &&
  1575. !(kernel || invoke->handle < FASTRPC_STATIC_HANDLE_MAX)) {
  1576. err = -EDQUOT;
  1577. spin_unlock(&fl->hlock);
  1578. goto bail;
  1579. }
  1580. spin_unlock(&fl->hlock);
  1581. bufs = REMOTE_SCALARS_LENGTH(invoke->sc);
  1582. size = bufs * sizeof(*ctx->lpra) + bufs * sizeof(*ctx->maps) +
  1583. sizeof(*ctx->fds) * (bufs) +
  1584. sizeof(*ctx->attrs) * (bufs) +
  1585. sizeof(*ctx->overs) * (bufs) +
  1586. sizeof(*ctx->overps) * (bufs);
  1587. VERIFY(err, NULL != (ctx = kzalloc(sizeof(*ctx) + size, GFP_KERNEL)));
  1588. if (err) {
  1589. err = -ENOMEM;
  1590. goto bail;
  1591. }
  1592. INIT_HLIST_NODE(&ctx->hn);
  1593. INIT_LIST_HEAD(&ctx->asyncn);
  1594. hlist_add_fake(&ctx->hn);
  1595. ctx->fl = fl;
  1596. ctx->maps = (struct fastrpc_mmap **)(&ctx[1]);
  1597. ctx->lpra = (remote_arg_t *)(&ctx->maps[bufs]);
  1598. ctx->fds = (int *)(&ctx->lpra[bufs]);
  1599. ctx->attrs = (unsigned int *)(&ctx->fds[bufs]);
  1600. ctx->overs = (struct overlap *)(&ctx->attrs[bufs]);
  1601. ctx->overps = (struct overlap **)(&ctx->overs[bufs]);
  1602. /* If user message, do not use copy_from_user to copy buffers for
  1603. * compat driver,as memory is already copied to kernel memory
  1604. * for compat driver
  1605. */
  1606. is_kernel_memory = ((kernel == USER_MSG) ? (fl->is_compat) : kernel);
  1607. K_COPY_FROM_USER(err, is_kernel_memory, (void *)ctx->lpra, invoke->pra,
  1608. bufs * sizeof(*ctx->lpra));
  1609. if (err) {
  1610. ADSPRPC_ERR(
  1611. "copy from user failed with %d for remote arguments list\n",
  1612. err);
  1613. err = -EFAULT;
  1614. goto bail;
  1615. }
  1616. if (invokefd->fds) {
  1617. K_COPY_FROM_USER(err, kernel, ctx->fds, invokefd->fds,
  1618. bufs * sizeof(*ctx->fds));
  1619. if (err) {
  1620. ADSPRPC_ERR(
  1621. "copy from user failed with %d for fd list\n",
  1622. err);
  1623. err = -EFAULT;
  1624. goto bail;
  1625. }
  1626. } else {
  1627. ctx->fds = NULL;
  1628. }
  1629. if (invokefd->attrs) {
  1630. K_COPY_FROM_USER(err, kernel, ctx->attrs, invokefd->attrs,
  1631. bufs * sizeof(*ctx->attrs));
  1632. if (err) {
  1633. ADSPRPC_ERR(
  1634. "copy from user failed with %d for attribute list\n",
  1635. err);
  1636. err = -EFAULT;
  1637. goto bail;
  1638. }
  1639. }
  1640. ctx->crc = (uint32_t *)invokefd->crc;
  1641. ctx->perf_dsp = (uint64_t *)invokefd->perf_dsp;
  1642. ctx->perf_kernel = (uint64_t *)invokefd->perf_kernel;
  1643. ctx->handle = invoke->handle;
  1644. ctx->sc = invoke->sc;
  1645. if (bufs) {
  1646. VERIFY(err, 0 == (err = context_build_overlap(ctx)));
  1647. if (err)
  1648. goto bail;
  1649. }
  1650. ctx->retval = -1;
  1651. ctx->pid = current->pid;
  1652. /* Store HLOS PID in context, it is not being sent to DSP */
  1653. ctx->tgid = fl->tgid;
  1654. init_completion(&ctx->work);
  1655. ctx->magic = FASTRPC_CTX_MAGIC;
  1656. ctx->rsp_flags = NORMAL_RESPONSE;
  1657. ctx->is_work_done = false;
  1658. ctx->copybuf = NULL;
  1659. ctx->is_early_wakeup = false;
  1660. ctx->is_job_sent_to_remote_ss = false;
  1661. if (ctx->fl->profile) {
  1662. ctx->perf = kzalloc(sizeof(*(ctx->perf)), GFP_KERNEL);
  1663. VERIFY(err, !IS_ERR_OR_NULL(ctx->perf));
  1664. if (err) {
  1665. kfree(ctx->perf);
  1666. err = -ENOMEM;
  1667. goto bail;
  1668. }
  1669. memset(ctx->perf, 0, sizeof(*(ctx->perf)));
  1670. /* Use HLOS PID, as perf tid is not being sent to DSP and is used to log in traces */
  1671. ctx->perf->tid = fl->tgid;
  1672. }
  1673. if (invokefd->job) {
  1674. K_COPY_FROM_USER(err, kernel, &ctx->asyncjob, invokefd->job,
  1675. sizeof(ctx->asyncjob));
  1676. if (err)
  1677. goto bail;
  1678. }
  1679. VERIFY(err, VALID_FASTRPC_CID(cid));
  1680. if (err) {
  1681. err = -ECHRNG;
  1682. goto bail;
  1683. }
  1684. chan = &me->channel[cid];
  1685. spin_lock_irqsave(&chan->ctxlock, irq_flags);
  1686. me->jobid[cid]++;
  1687. for (ii = ((kernel || ctx->handle < FASTRPC_STATIC_HANDLE_MAX)
  1688. ? 0 : NUM_KERNEL_AND_STATIC_ONLY_CONTEXTS);
  1689. ii < FASTRPC_CTX_MAX; ii++) {
  1690. if (!chan->ctxtable[ii]) {
  1691. chan->ctxtable[ii] = ctx;
  1692. ctx->ctxid = (me->jobid[cid] << FASTRPC_CTX_JOBID_POS)
  1693. | (ii << FASTRPC_CTX_TABLE_IDX_POS)
  1694. | ((ctx->asyncjob.isasyncjob &&
  1695. FASTRPC_ASYNC_JOB_MASK) << FASTRPC_CTX_JOB_TYPE_POS);
  1696. break;
  1697. }
  1698. }
  1699. spin_unlock_irqrestore(&chan->ctxlock, irq_flags);
  1700. VERIFY(err, ii < FASTRPC_CTX_MAX);
  1701. if (err) {
  1702. ADSPRPC_ERR(
  1703. "adsprpc: out of context table entries for handle 0x%x, sc 0x%x\n",
  1704. ctx->handle, ctx->sc);
  1705. err = -ENOKEY;
  1706. goto bail;
  1707. }
  1708. ctx->xo_time_in_us_created = CONVERT_CNT_TO_US(__arch_counter_get_cntvct());
  1709. spin_lock(&fl->hlock);
  1710. hlist_add_head(&ctx->hn, &clst->pending);
  1711. clst->num_active_ctxs++;
  1712. spin_unlock(&fl->hlock);
  1713. trace_fastrpc_context_alloc((uint64_t)ctx,
  1714. ctx->ctxid | fl->pd, ctx->handle, ctx->sc);
  1715. *po = ctx;
  1716. bail:
  1717. if (ctx && err)
  1718. context_free(ctx);
  1719. return err;
  1720. }
  1721. static void context_save_interrupted(struct smq_invoke_ctx *ctx)
  1722. {
  1723. struct fastrpc_ctx_lst *clst = &ctx->fl->clst;
  1724. ctx->xo_time_in_us_interrupted = CONVERT_CNT_TO_US(__arch_counter_get_cntvct());
  1725. fastrpc_update_txmsg_buf(NULL, 0, 0, 0, ctx, INTERRUPTED_STATE);
  1726. spin_lock(&ctx->fl->hlock);
  1727. hlist_del_init(&ctx->hn);
  1728. hlist_add_head(&ctx->hn, &clst->interrupted);
  1729. spin_unlock(&ctx->fl->hlock);
  1730. }
  1731. static void context_free(struct smq_invoke_ctx *ctx)
  1732. {
  1733. uint32_t i = 0;
  1734. struct fastrpc_apps *me = &gfa;
  1735. int nbufs = REMOTE_SCALARS_INBUFS(ctx->sc) +
  1736. REMOTE_SCALARS_OUTBUFS(ctx->sc);
  1737. int cid = ctx->fl->cid;
  1738. struct fastrpc_channel_ctx *chan = NULL;
  1739. unsigned long irq_flags = 0;
  1740. int err = 0;
  1741. VERIFY(err, VALID_FASTRPC_CID(cid));
  1742. if (err) {
  1743. ADSPRPC_ERR(
  1744. "invalid channel 0x%zx set for session\n",
  1745. cid);
  1746. return;
  1747. }
  1748. chan = &me->channel[cid];
  1749. i = (uint32_t)GET_TABLE_IDX_FROM_CTXID(ctx->ctxid);
  1750. spin_lock_irqsave(&chan->ctxlock, irq_flags);
  1751. if (i < FASTRPC_CTX_MAX && chan->ctxtable[i] == ctx) {
  1752. chan->ctxtable[i] = NULL;
  1753. } else {
  1754. for (i = 0; i < FASTRPC_CTX_MAX; i++) {
  1755. if (chan->ctxtable[i] == ctx) {
  1756. chan->ctxtable[i] = NULL;
  1757. break;
  1758. }
  1759. }
  1760. }
  1761. spin_unlock_irqrestore(&chan->ctxlock, irq_flags);
  1762. spin_lock(&ctx->fl->hlock);
  1763. if (!hlist_unhashed(&ctx->hn)) {
  1764. hlist_del_init(&ctx->hn);
  1765. ctx->fl->clst.num_active_ctxs--;
  1766. }
  1767. spin_unlock(&ctx->fl->hlock);
  1768. mutex_lock(&ctx->fl->map_mutex);
  1769. for (i = 0; i < nbufs; ++i) {
  1770. /*
  1771. * Decrement ctx refs count before mmap free,
  1772. * indicate remote call no longer using it
  1773. */
  1774. if (ctx->maps[i] && ctx->maps[i]->ctx_refs)
  1775. ctx->maps[i]->ctx_refs--;
  1776. fastrpc_mmap_free(ctx->maps[i], 0);
  1777. }
  1778. mutex_unlock(&ctx->fl->map_mutex);
  1779. fastrpc_buf_free(ctx->buf, 1);
  1780. if (ctx->copybuf != ctx->buf)
  1781. fastrpc_buf_free(ctx->copybuf, 1);
  1782. kfree(ctx->lrpra);
  1783. ctx->lrpra = NULL;
  1784. ctx->magic = 0;
  1785. ctx->ctxid = 0;
  1786. if (ctx->fl->profile)
  1787. kfree(ctx->perf);
  1788. trace_fastrpc_context_free((uint64_t)ctx,
  1789. ctx->msg.invoke.header.ctx, ctx->handle, ctx->sc);
  1790. kfree(ctx);
  1791. }
  1792. static void fastrpc_queue_completed_async_job(struct smq_invoke_ctx *ctx)
  1793. {
  1794. struct fastrpc_file *fl = ctx->fl;
  1795. unsigned long flags;
  1796. spin_lock_irqsave(&fl->aqlock, flags);
  1797. if (ctx->is_early_wakeup)
  1798. goto bail;
  1799. list_add_tail(&ctx->asyncn, &fl->clst.async_queue);
  1800. atomic_add(1, &fl->async_queue_job_count);
  1801. ctx->is_early_wakeup = true;
  1802. wake_up_interruptible(&fl->async_wait_queue);
  1803. bail:
  1804. spin_unlock_irqrestore(&fl->aqlock, flags);
  1805. }
  1806. static void fastrpc_queue_pd_status(struct fastrpc_file *fl, int domain, int status, int sessionid)
  1807. {
  1808. struct smq_notif_rsp *notif_rsp = NULL;
  1809. unsigned long flags;
  1810. int err = 0;
  1811. VERIFY(err, NULL != (notif_rsp = kzalloc(sizeof(*notif_rsp), GFP_ATOMIC)));
  1812. if (err) {
  1813. ADSPRPC_ERR(
  1814. "allocation failed for size 0x%zx\n",
  1815. sizeof(*notif_rsp));
  1816. return;
  1817. }
  1818. notif_rsp->status = status;
  1819. notif_rsp->domain = domain;
  1820. notif_rsp->session = sessionid;
  1821. spin_lock_irqsave(&fl->proc_state_notif.nqlock, flags);
  1822. list_add_tail(&notif_rsp->notifn, &fl->clst.notif_queue);
  1823. atomic_add(1, &fl->proc_state_notif.notif_queue_count);
  1824. wake_up_interruptible(&fl->proc_state_notif.notif_wait_queue);
  1825. spin_unlock_irqrestore(&fl->proc_state_notif.nqlock, flags);
  1826. }
  1827. static void fastrpc_notif_find_process(int domain, struct smq_notif_rspv3 *notif)
  1828. {
  1829. struct fastrpc_apps *me = &gfa;
  1830. struct fastrpc_file *fl = NULL;
  1831. struct hlist_node *n;
  1832. bool is_process_found = false;
  1833. unsigned long irq_flags = 0;
  1834. spin_lock_irqsave(&me->hlock, irq_flags);
  1835. hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
  1836. if (fl->tgid_frpc == notif->pid) {
  1837. is_process_found = true;
  1838. break;
  1839. }
  1840. }
  1841. spin_unlock_irqrestore(&me->hlock, irq_flags);
  1842. if (!is_process_found)
  1843. return;
  1844. fastrpc_queue_pd_status(fl, domain, notif->status, fl->sessionid);
  1845. }
  1846. static void context_notify_user(struct smq_invoke_ctx *ctx,
  1847. int retval, uint32_t rsp_flags, uint32_t early_wake_time)
  1848. {
  1849. fastrpc_pm_awake(ctx->fl, gcinfo[ctx->fl->cid].secure);
  1850. ctx->retval = retval;
  1851. ctx->rsp_flags = (enum fastrpc_response_flags)rsp_flags;
  1852. trace_fastrpc_context_complete(ctx->fl->cid, (uint64_t)ctx, retval,
  1853. ctx->msg.invoke.header.ctx, ctx->handle, ctx->sc);
  1854. switch (rsp_flags) {
  1855. case NORMAL_RESPONSE:
  1856. fallthrough;
  1857. case COMPLETE_SIGNAL:
  1858. /* normal and complete response with return value */
  1859. ctx->is_work_done = true;
  1860. if (ctx->asyncjob.isasyncjob)
  1861. fastrpc_queue_completed_async_job(ctx);
  1862. trace_fastrpc_msg("wakeup_task: begin");
  1863. complete(&ctx->work);
  1864. trace_fastrpc_msg("wakeup_task: end");
  1865. break;
  1866. case USER_EARLY_SIGNAL:
  1867. /* user hint of approximate time of completion */
  1868. ctx->early_wake_time = early_wake_time;
  1869. if (ctx->asyncjob.isasyncjob)
  1870. break;
  1871. fallthrough;
  1872. case EARLY_RESPONSE:
  1873. /* rpc framework early response with return value */
  1874. if (ctx->asyncjob.isasyncjob)
  1875. fastrpc_queue_completed_async_job(ctx);
  1876. else {
  1877. trace_fastrpc_msg("wakeup_task: begin");
  1878. complete(&ctx->work);
  1879. trace_fastrpc_msg("wakeup_task: end");
  1880. }
  1881. break;
  1882. default:
  1883. break;
  1884. }
  1885. }
  1886. static void fastrpc_notify_users(struct fastrpc_file *me)
  1887. {
  1888. struct smq_invoke_ctx *ictx;
  1889. struct hlist_node *n;
  1890. unsigned long irq_flags = 0;
  1891. spin_lock_irqsave(&me->hlock, irq_flags);
  1892. hlist_for_each_entry_safe(ictx, n, &me->clst.pending, hn) {
  1893. ictx->is_work_done = true;
  1894. ictx->retval = -ECONNRESET;
  1895. trace_fastrpc_context_complete(me->cid, (uint64_t)ictx,
  1896. ictx->retval, ictx->msg.invoke.header.ctx,
  1897. ictx->handle, ictx->sc);
  1898. if (ictx->asyncjob.isasyncjob && ictx->is_job_sent_to_remote_ss)
  1899. fastrpc_queue_completed_async_job(ictx);
  1900. else
  1901. complete(&ictx->work);
  1902. }
  1903. hlist_for_each_entry_safe(ictx, n, &me->clst.interrupted, hn) {
  1904. ictx->is_work_done = true;
  1905. ictx->retval = -ECONNRESET;
  1906. trace_fastrpc_context_complete(me->cid, (uint64_t)ictx,
  1907. ictx->retval, ictx->msg.invoke.header.ctx,
  1908. ictx->handle, ictx->sc);
  1909. complete(&ictx->work);
  1910. }
  1911. spin_unlock_irqrestore(&me->hlock, irq_flags);
  1912. }
  1913. static void fastrpc_notify_users_staticpd_pdr(struct fastrpc_file *me)
  1914. {
  1915. struct smq_invoke_ctx *ictx;
  1916. struct hlist_node *n;
  1917. unsigned long irq_flags = 0;
  1918. spin_lock_irqsave(&me->hlock, irq_flags);
  1919. hlist_for_each_entry_safe(ictx, n, &me->clst.pending, hn) {
  1920. if (ictx->msg.pid) {
  1921. ictx->is_work_done = true;
  1922. ictx->retval = -ECONNRESET;
  1923. trace_fastrpc_context_complete(me->cid, (uint64_t)ictx,
  1924. ictx->retval, ictx->msg.invoke.header.ctx,
  1925. ictx->handle, ictx->sc);
  1926. if (ictx->asyncjob.isasyncjob && ictx->is_job_sent_to_remote_ss)
  1927. fastrpc_queue_completed_async_job(ictx);
  1928. else
  1929. complete(&ictx->work);
  1930. }
  1931. }
  1932. hlist_for_each_entry_safe(ictx, n, &me->clst.interrupted, hn) {
  1933. if (ictx->msg.pid) {
  1934. ictx->is_work_done = true;
  1935. ictx->retval = -ECONNRESET;
  1936. trace_fastrpc_context_complete(me->cid, (uint64_t)ictx,
  1937. ictx->retval, ictx->msg.invoke.header.ctx,
  1938. ictx->handle, ictx->sc);
  1939. complete(&ictx->work);
  1940. }
  1941. }
  1942. spin_unlock_irqrestore(&me->hlock, irq_flags);
  1943. }
  1944. static void fastrpc_update_ramdump_status(int cid)
  1945. {
  1946. struct fastrpc_file *fl = NULL;
  1947. struct hlist_node *n = NULL;
  1948. struct fastrpc_apps *me = &gfa;
  1949. struct fastrpc_channel_ctx *chan = &me->channel[cid];
  1950. unsigned long irq_flags = 0;
  1951. spin_lock_irqsave(&me->hlock, irq_flags);
  1952. hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
  1953. if (fl->cid == cid && fl->init_mem &&
  1954. fl->file_close < FASTRPC_PROCESS_DSP_EXIT_COMPLETE &&
  1955. fl->dsp_proc_init) {
  1956. hlist_add_head(&fl->init_mem->hn_init, &chan->initmems);
  1957. fl->is_ramdump_pend = true;
  1958. }
  1959. }
  1960. if (chan->buf)
  1961. hlist_add_head(&chan->buf->hn_init, &chan->initmems);
  1962. spin_unlock_irqrestore(&me->hlock, irq_flags);
  1963. }
  1964. static void fastrpc_ramdump_collection(int cid)
  1965. {
  1966. struct fastrpc_file *fl = NULL;
  1967. struct hlist_node *n = NULL;
  1968. struct fastrpc_apps *me = &gfa;
  1969. struct fastrpc_channel_ctx *chan = &me->channel[cid];
  1970. struct qcom_dump_segment ramdump_entry;
  1971. struct fastrpc_buf *buf = NULL;
  1972. int ret = 0;
  1973. unsigned long irq_flags = 0;
  1974. struct list_head head;
  1975. hlist_for_each_entry_safe(buf, n, &chan->initmems, hn_init) {
  1976. fl = buf->fl;
  1977. memset(&ramdump_entry, 0, sizeof(ramdump_entry));
  1978. ramdump_entry.da = buf->phys;
  1979. ramdump_entry.va = (void *)buf->virt;
  1980. ramdump_entry.size = buf->size;
  1981. INIT_LIST_HEAD(&head);
  1982. list_add(&ramdump_entry.node, &head);
  1983. if (fl && fl->sctx && fl->sctx->smmu.dev)
  1984. ret = qcom_elf_dump(&head, fl->sctx->smmu.dev, ELF_CLASS);
  1985. else {
  1986. if (me->dev != NULL)
  1987. ret = qcom_elf_dump(&head, me->dev, ELF_CLASS);
  1988. }
  1989. if (ret < 0)
  1990. ADSPRPC_ERR("adsprpc: %s: unable to dump PD memory (err %d)\n",
  1991. __func__, ret);
  1992. hlist_del_init(&buf->hn_init);
  1993. if (fl) {
  1994. spin_lock_irqsave(&me->hlock, irq_flags);
  1995. if (fl->file_close)
  1996. complete(&fl->work);
  1997. fl->is_ramdump_pend = false;
  1998. spin_unlock_irqrestore(&me->hlock, irq_flags);
  1999. }
  2000. }
  2001. }
  2002. static void fastrpc_notify_drivers(struct fastrpc_apps *me, int cid)
  2003. {
  2004. struct fastrpc_file *fl;
  2005. struct hlist_node *n;
  2006. unsigned long irq_flags = 0;
  2007. spin_lock_irqsave(&me->hlock, irq_flags);
  2008. hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
  2009. if (fl->cid == cid) {
  2010. fastrpc_queue_pd_status(fl, cid, FASTRPC_DSP_SSR, 0);
  2011. fastrpc_notify_users(fl);
  2012. }
  2013. }
  2014. spin_unlock_irqrestore(&me->hlock, irq_flags);
  2015. }
  2016. static void fastrpc_notify_pdr_drivers(struct fastrpc_apps *me,
  2017. char *servloc_name)
  2018. {
  2019. struct fastrpc_file *fl;
  2020. struct hlist_node *n;
  2021. unsigned long irq_flags = 0;
  2022. spin_lock_irqsave(&me->hlock, irq_flags);
  2023. hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
  2024. if (fl->servloc_name && !strcmp(servloc_name, fl->servloc_name))
  2025. fastrpc_notify_users_staticpd_pdr(fl);
  2026. }
  2027. spin_unlock_irqrestore(&me->hlock, irq_flags);
  2028. }
  2029. static void context_list_ctor(struct fastrpc_ctx_lst *me)
  2030. {
  2031. INIT_HLIST_HEAD(&me->interrupted);
  2032. INIT_HLIST_HEAD(&me->pending);
  2033. me->num_active_ctxs = 0;
  2034. INIT_LIST_HEAD(&me->async_queue);
  2035. INIT_LIST_HEAD(&me->notif_queue);
  2036. }
  2037. static void fastrpc_context_list_dtor(struct fastrpc_file *fl)
  2038. {
  2039. struct fastrpc_ctx_lst *clst = &fl->clst;
  2040. struct smq_invoke_ctx *ictx = NULL, *ctxfree;
  2041. struct hlist_node *n;
  2042. do {
  2043. ctxfree = NULL;
  2044. spin_lock(&fl->hlock);
  2045. hlist_for_each_entry_safe(ictx, n, &clst->interrupted, hn) {
  2046. hlist_del_init(&ictx->hn);
  2047. clst->num_active_ctxs--;
  2048. ctxfree = ictx;
  2049. break;
  2050. }
  2051. spin_unlock(&fl->hlock);
  2052. if (ctxfree)
  2053. context_free(ctxfree);
  2054. } while (ctxfree);
  2055. do {
  2056. ctxfree = NULL;
  2057. spin_lock(&fl->hlock);
  2058. hlist_for_each_entry_safe(ictx, n, &clst->pending, hn) {
  2059. hlist_del_init(&ictx->hn);
  2060. clst->num_active_ctxs--;
  2061. ctxfree = ictx;
  2062. break;
  2063. }
  2064. spin_unlock(&fl->hlock);
  2065. if (ctxfree)
  2066. context_free(ctxfree);
  2067. } while (ctxfree);
  2068. }
  2069. static int fastrpc_file_free(struct fastrpc_file *fl);
  2070. static void fastrpc_file_list_dtor(struct fastrpc_apps *me)
  2071. {
  2072. struct fastrpc_file *fl, *free;
  2073. struct hlist_node *n;
  2074. unsigned long irq_flags = 0;
  2075. do {
  2076. free = NULL;
  2077. spin_lock_irqsave(&me->hlock, irq_flags);
  2078. hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
  2079. hlist_del_init(&fl->hn);
  2080. free = fl;
  2081. break;
  2082. }
  2083. spin_unlock_irqrestore(&me->hlock, irq_flags);
  2084. if (free)
  2085. fastrpc_file_free(free);
  2086. } while (free);
  2087. }
  2088. static int get_args(uint32_t kernel, struct smq_invoke_ctx *ctx)
  2089. {
  2090. remote_arg64_t *rpra, *lrpra;
  2091. remote_arg_t *lpra = ctx->lpra;
  2092. struct smq_invoke_buf *list;
  2093. struct smq_phy_page *pages, *ipage;
  2094. uint32_t sc = ctx->sc;
  2095. int inbufs = REMOTE_SCALARS_INBUFS(sc);
  2096. int outbufs = REMOTE_SCALARS_OUTBUFS(sc);
  2097. int handles, bufs = inbufs + outbufs;
  2098. uintptr_t args = 0;
  2099. size_t rlen = 0, copylen = 0, metalen = 0, lrpralen = 0, templen = 0;
  2100. size_t totallen = 0; //header and non ion copy buf len
  2101. int i, oix;
  2102. int err = 0, j = 0;
  2103. int mflags = 0;
  2104. uint64_t *fdlist = NULL;
  2105. uint32_t *crclist = NULL;
  2106. uint32_t early_hint;
  2107. uint64_t *perf_counter = NULL;
  2108. struct fastrpc_dsp_capabilities *dsp_cap_ptr = NULL;
  2109. if (ctx->fl->profile)
  2110. perf_counter = (uint64_t *)ctx->perf + PERF_COUNT;
  2111. /* calculate size of the metadata */
  2112. rpra = NULL;
  2113. lrpra = NULL;
  2114. list = smq_invoke_buf_start(rpra, sc);
  2115. pages = smq_phy_page_start(sc, list);
  2116. ipage = pages;
  2117. PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_MAP),
  2118. for (i = 0; i < bufs; ++i) {
  2119. uintptr_t buf = (uintptr_t)lpra[i].buf.pv;
  2120. size_t len = lpra[i].buf.len;
  2121. mutex_lock(&ctx->fl->map_mutex);
  2122. if (ctx->fds && (ctx->fds[i] != -1))
  2123. err = fastrpc_mmap_create(ctx->fl, ctx->fds[i], NULL,
  2124. ctx->attrs[i], buf, len,
  2125. mflags, &ctx->maps[i]);
  2126. /*
  2127. * Increment ctx refs count for in/out buffer if map created,
  2128. * indicate map under use in remote call
  2129. */
  2130. if (ctx->maps[i])
  2131. ctx->maps[i]->ctx_refs++;
  2132. mutex_unlock(&ctx->fl->map_mutex);
  2133. if (err)
  2134. goto bail;
  2135. ipage += 1;
  2136. }
  2137. PERF_END);
  2138. handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc);
  2139. mutex_lock(&ctx->fl->map_mutex);
  2140. for (i = bufs; i < bufs + handles; i++) {
  2141. int dmaflags = 0;
  2142. if (ctx->attrs && (ctx->attrs[i] & FASTRPC_ATTR_NOMAP))
  2143. dmaflags = FASTRPC_MAP_FD_NOMAP;
  2144. VERIFY(err, VALID_FASTRPC_CID(ctx->fl->cid));
  2145. if (err) {
  2146. err = -ECHRNG;
  2147. mutex_unlock(&ctx->fl->map_mutex);
  2148. goto bail;
  2149. }
  2150. dsp_cap_ptr = &gcinfo[ctx->fl->cid].dsp_cap_kernel;
  2151. // Skip cpu mapping if DMA_HANDLE_REVERSE_RPC_CAP is true.
  2152. if (!dsp_cap_ptr->dsp_attributes[DMA_HANDLE_REVERSE_RPC_CAP] &&
  2153. ctx->fds && (ctx->fds[i] != -1))
  2154. err = fastrpc_mmap_create(ctx->fl, ctx->fds[i], NULL,
  2155. FASTRPC_ATTR_NOVA, 0, 0, dmaflags,
  2156. &ctx->maps[i]);
  2157. if (err) {
  2158. for (j = bufs; j < i; j++) {
  2159. /*
  2160. * Due to error decrement ctx refs count before mmap free
  2161. * for each in/out handle, if map created
  2162. */
  2163. if (ctx->maps[j] && ctx->maps[j]->ctx_refs)
  2164. ctx->maps[j]->ctx_refs--;
  2165. fastrpc_mmap_free(ctx->maps[j], 0);
  2166. }
  2167. mutex_unlock(&ctx->fl->map_mutex);
  2168. goto bail;
  2169. } else if (ctx->maps[i]) {
  2170. /*
  2171. * Increment ctx refs count for in/out handle if map created
  2172. * and no error, indicate map under use in remote call
  2173. */
  2174. ctx->maps[i]->ctx_refs++;
  2175. }
  2176. ipage += 1;
  2177. }
  2178. mutex_unlock(&ctx->fl->map_mutex);
  2179. /* metalen includes meta data, fds, crc, dsp perf and early wakeup hint */
  2180. metalen = totallen = (size_t)&ipage[0] + (sizeof(uint64_t) * M_FDLIST) +
  2181. (sizeof(uint32_t) * M_CRCLIST) + (sizeof(uint64_t) * M_DSP_PERF_LIST) +
  2182. sizeof(early_hint);
  2183. if (metalen) {
  2184. err = fastrpc_buf_alloc(ctx->fl, metalen, 0, 0,
  2185. METADATA_BUF, &ctx->buf);
  2186. if (err)
  2187. goto bail;
  2188. VERIFY(err, !IS_ERR_OR_NULL(ctx->buf->virt));
  2189. if (err)
  2190. goto bail;
  2191. memset(ctx->buf->virt, 0, ctx->buf->size);
  2192. }
  2193. ctx->used = metalen;
  2194. /* allocate new local rpra buffer */
  2195. lrpralen = (size_t)&list[0];
  2196. if (lrpralen) {
  2197. lrpra = kzalloc(lrpralen, GFP_KERNEL);
  2198. VERIFY(err, !IS_ERR_OR_NULL(lrpra));
  2199. if (err) {
  2200. err = -ENOMEM;
  2201. goto bail;
  2202. }
  2203. }
  2204. ctx->lrpra = lrpra;
  2205. /* calculate len required for copying */
  2206. for (oix = 0; oix < inbufs + outbufs; ++oix) {
  2207. int i = ctx->overps[oix]->raix;
  2208. uintptr_t mstart, mend;
  2209. size_t len = lpra[i].buf.len;
  2210. if (!len)
  2211. continue;
  2212. if (ctx->maps[i])
  2213. continue;
  2214. if (ctx->overps[oix]->offset == 0)
  2215. copylen = ALIGN(copylen, BALIGN);
  2216. mstart = ctx->overps[oix]->mstart;
  2217. mend = ctx->overps[oix]->mend;
  2218. templen = mend - mstart;
  2219. VERIFY(err, ((templen <= LONG_MAX) && (copylen <= (LONG_MAX - templen))));
  2220. if (err) {
  2221. err = -EFAULT;
  2222. goto bail;
  2223. }
  2224. copylen += templen;
  2225. }
  2226. totallen = ALIGN(totallen, BALIGN) + copylen;
  2227. /* allocate non -ion copy buffer */
  2228. /* Checking if copylen can be accomodated in metalen*/
  2229. /*if not allocating new buffer */
  2230. if (totallen <= (size_t)buf_page_size(metalen)) {
  2231. args = (uintptr_t)ctx->buf->virt + metalen;
  2232. ctx->copybuf = ctx->buf;
  2233. rlen = totallen - metalen;
  2234. } else if (copylen) {
  2235. err = fastrpc_buf_alloc(ctx->fl, copylen, 0, 0, COPYDATA_BUF,
  2236. &ctx->copybuf);
  2237. if (err)
  2238. goto bail;
  2239. memset(ctx->copybuf->virt, 0, copylen);
  2240. args = (uintptr_t)ctx->copybuf->virt;
  2241. rlen = copylen;
  2242. totallen = copylen;
  2243. }
  2244. /* copy metadata */
  2245. rpra = ctx->buf->virt;
  2246. ctx->rpra = rpra;
  2247. list = smq_invoke_buf_start(rpra, sc);
  2248. pages = smq_phy_page_start(sc, list);
  2249. ipage = pages;
  2250. for (i = 0; i < bufs + handles; ++i) {
  2251. if (lpra[i].buf.len)
  2252. list[i].num = 1;
  2253. else
  2254. list[i].num = 0;
  2255. list[i].pgidx = ipage - pages;
  2256. ipage++;
  2257. }
  2258. /* map ion buffers */
  2259. PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_MAP),
  2260. for (i = 0; rpra && i < inbufs + outbufs; ++i) {
  2261. struct fastrpc_mmap *map = ctx->maps[i];
  2262. uint64_t buf = ptr_to_uint64(lpra[i].buf.pv);
  2263. size_t len = lpra[i].buf.len;
  2264. rpra[i].buf.pv = 0;
  2265. rpra[i].buf.len = len;
  2266. if (!len)
  2267. continue;
  2268. if (map) {
  2269. struct vm_area_struct *vma;
  2270. uintptr_t offset;
  2271. uint64_t num = buf_num_pages(buf, len);
  2272. int idx = list[i].pgidx;
  2273. if (map->attr & FASTRPC_ATTR_NOVA) {
  2274. offset = 0;
  2275. } else {
  2276. down_read(&current->mm->mmap_lock);
  2277. VERIFY(err, NULL != (vma = find_vma(current->mm,
  2278. map->va)));
  2279. if (err) {
  2280. up_read(&current->mm->mmap_lock);
  2281. goto bail;
  2282. }
  2283. offset = buf_page_start(buf) - vma->vm_start;
  2284. up_read(&current->mm->mmap_lock);
  2285. VERIFY(err, offset + len <= (uintptr_t)map->size);
  2286. if (err) {
  2287. ADSPRPC_ERR(
  2288. "buffer address is invalid for the fd passed for %d address 0x%llx and size %zu\n",
  2289. i, (uintptr_t)lpra[i].buf.pv,
  2290. lpra[i].buf.len);
  2291. err = -EFAULT;
  2292. goto bail;
  2293. }
  2294. }
  2295. pages[idx].addr = map->phys + offset;
  2296. pages[idx].size = num << PAGE_SHIFT;
  2297. }
  2298. rpra[i].buf.pv = buf;
  2299. }
  2300. PERF_END);
  2301. for (i = bufs; i < bufs + handles; ++i) {
  2302. struct fastrpc_mmap *map = ctx->maps[i];
  2303. if (map) {
  2304. pages[i].addr = map->phys;
  2305. pages[i].size = map->size;
  2306. }
  2307. }
  2308. fdlist = (uint64_t *)&pages[bufs + handles];
  2309. crclist = (uint32_t *)&fdlist[M_FDLIST];
  2310. /* reset fds, crc and early wakeup hint memory */
  2311. /* remote process updates these values before responding */
  2312. memset(fdlist, 0, sizeof(uint64_t)*M_FDLIST + sizeof(uint32_t)*M_CRCLIST +
  2313. (sizeof(uint64_t) * M_DSP_PERF_LIST) + sizeof(early_hint));
  2314. /* copy non ion buffers */
  2315. PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_COPY),
  2316. for (oix = 0; rpra && oix < inbufs + outbufs; ++oix) {
  2317. int i = ctx->overps[oix]->raix;
  2318. struct fastrpc_mmap *map = ctx->maps[i];
  2319. size_t mlen;
  2320. uint64_t buf;
  2321. size_t len = lpra[i].buf.len;
  2322. if (!len)
  2323. continue;
  2324. if (map)
  2325. continue;
  2326. if (ctx->overps[oix]->offset == 0) {
  2327. rlen -= ALIGN(args, BALIGN) - args;
  2328. args = ALIGN(args, BALIGN);
  2329. }
  2330. mlen = ctx->overps[oix]->mend - ctx->overps[oix]->mstart;
  2331. VERIFY(err, rlen >= mlen);
  2332. if (err) {
  2333. err = -EFAULT;
  2334. goto bail;
  2335. }
  2336. rpra[i].buf.pv =
  2337. (args - ctx->overps[oix]->offset);
  2338. pages[list[i].pgidx].addr = ctx->copybuf->phys -
  2339. ctx->overps[oix]->offset +
  2340. (totallen - rlen);
  2341. pages[list[i].pgidx].addr =
  2342. buf_page_start(pages[list[i].pgidx].addr);
  2343. buf = rpra[i].buf.pv;
  2344. pages[list[i].pgidx].size = buf_num_pages(buf, len) * PAGE_SIZE;
  2345. if (i < inbufs) {
  2346. K_COPY_FROM_USER(err, kernel, uint64_to_ptr(buf),
  2347. lpra[i].buf.pv, len);
  2348. if (err) {
  2349. ADSPRPC_ERR(
  2350. "copy from user failed with %d for dst 0x%llx, src %pK, size 0x%zx, arg %d\n",
  2351. err, buf, lpra[i].buf.pv, len, i+1);
  2352. err = -EFAULT;
  2353. goto bail;
  2354. }
  2355. }
  2356. if (len > DEBUG_PRINT_SIZE_LIMIT)
  2357. ADSPRPC_DEBUG(
  2358. "copied non ion buffer sc 0x%x pv 0x%llx, mend 0x%llx mstart 0x%llx, len %zu\n",
  2359. sc, rpra[i].buf.pv,
  2360. ctx->overps[oix]->mend,
  2361. ctx->overps[oix]->mstart, len);
  2362. args = args + mlen;
  2363. rlen -= mlen;
  2364. }
  2365. PERF_END);
  2366. PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_FLUSH),
  2367. for (oix = 0; oix < inbufs + outbufs; ++oix) {
  2368. int i = ctx->overps[oix]->raix;
  2369. struct fastrpc_mmap *map = ctx->maps[i];
  2370. if (i+1 > inbufs) // Avoiding flush for outbufs
  2371. continue;
  2372. if (ctx->fl->sctx && ctx->fl->sctx->smmu.coherent)
  2373. continue;
  2374. if (map && (map->attr & FASTRPC_ATTR_FORCE_NOFLUSH))
  2375. continue;
  2376. if (rpra && rpra[i].buf.len && (ctx->overps[oix]->mstart ||
  2377. ctx->overps[oix]->do_cmo == 1)) {
  2378. if (map && map->buf) {
  2379. if (((buf_page_size(ctx->overps[oix]->mend -
  2380. ctx->overps[oix]->mstart)) == map->size) ||
  2381. ctx->overps[oix]->do_cmo) {
  2382. dma_buf_begin_cpu_access(map->buf,
  2383. DMA_TO_DEVICE);
  2384. dma_buf_end_cpu_access(map->buf,
  2385. DMA_TO_DEVICE);
  2386. ADSPRPC_DEBUG(
  2387. "sc 0x%x pv 0x%llx, mend 0x%llx mstart 0x%llx, len %zu size %zu\n",
  2388. sc, rpra[i].buf.pv,
  2389. ctx->overps[oix]->mend,
  2390. ctx->overps[oix]->mstart,
  2391. rpra[i].buf.len, map->size);
  2392. } else {
  2393. uintptr_t offset;
  2394. uint64_t flush_len;
  2395. struct vm_area_struct *vma;
  2396. down_read(&current->mm->mmap_lock);
  2397. VERIFY(err, NULL != (vma = find_vma(
  2398. current->mm, rpra[i].buf.pv)));
  2399. if (err) {
  2400. up_read(&current->mm->mmap_lock);
  2401. goto bail;
  2402. }
  2403. if (ctx->overps[oix]->do_cmo) {
  2404. offset = rpra[i].buf.pv -
  2405. vma->vm_start;
  2406. flush_len = rpra[i].buf.len;
  2407. } else {
  2408. offset =
  2409. ctx->overps[oix]->mstart
  2410. - vma->vm_start;
  2411. flush_len =
  2412. ctx->overps[oix]->mend -
  2413. ctx->overps[oix]->mstart;
  2414. }
  2415. up_read(&current->mm->mmap_lock);
  2416. dma_buf_begin_cpu_access_partial(
  2417. map->buf, DMA_TO_DEVICE, offset,
  2418. flush_len);
  2419. dma_buf_end_cpu_access_partial(
  2420. map->buf, DMA_TO_DEVICE, offset,
  2421. flush_len);
  2422. ADSPRPC_DEBUG(
  2423. "sc 0x%x vm_start 0x%llx pv 0x%llx, offset 0x%llx, mend 0x%llx mstart 0x%llx, len %zu size %zu\n",
  2424. sc, vma->vm_start,
  2425. rpra[i].buf.pv, offset,
  2426. ctx->overps[oix]->mend,
  2427. ctx->overps[oix]->mstart,
  2428. rpra[i].buf.len, map->size);
  2429. }
  2430. }
  2431. }
  2432. }
  2433. PERF_END);
  2434. for (i = bufs; ctx->fds && rpra && i < bufs + handles; i++) {
  2435. rpra[i].dma.fd = ctx->fds[i];
  2436. rpra[i].dma.len = (uint32_t)lpra[i].buf.len;
  2437. rpra[i].dma.offset =
  2438. (uint32_t)(uintptr_t)lpra[i].buf.pv;
  2439. }
  2440. /* Copy rpra to local buffer */
  2441. if (ctx->lrpra && rpra && lrpralen > 0)
  2442. memcpy(ctx->lrpra, rpra, lrpralen);
  2443. bail:
  2444. return err;
  2445. }
  2446. static int put_args(uint32_t kernel, struct smq_invoke_ctx *ctx,
  2447. remote_arg_t *upra)
  2448. {
  2449. uint32_t sc = ctx->sc;
  2450. struct smq_invoke_buf *list;
  2451. struct smq_phy_page *pages;
  2452. struct fastrpc_mmap *mmap;
  2453. uint64_t *fdlist;
  2454. uint32_t *crclist = NULL, *poll = NULL;
  2455. uint64_t *perf_dsp_list = NULL;
  2456. remote_arg64_t *rpra = ctx->lrpra;
  2457. int i, inbufs, outbufs, handles;
  2458. int err = 0, perfErr = 0;
  2459. inbufs = REMOTE_SCALARS_INBUFS(sc);
  2460. outbufs = REMOTE_SCALARS_OUTBUFS(sc);
  2461. handles = REMOTE_SCALARS_INHANDLES(sc) + REMOTE_SCALARS_OUTHANDLES(sc);
  2462. list = smq_invoke_buf_start(ctx->rpra, sc);
  2463. pages = smq_phy_page_start(sc, list);
  2464. fdlist = (uint64_t *)(pages + inbufs + outbufs + handles);
  2465. crclist = (uint32_t *)(fdlist + M_FDLIST);
  2466. poll = (uint32_t *)(crclist + M_CRCLIST);
  2467. perf_dsp_list = (uint64_t *)(poll + 1);
  2468. for (i = inbufs; i < inbufs + outbufs; ++i) {
  2469. if (!ctx->maps[i]) {
  2470. K_COPY_TO_USER(err, kernel,
  2471. ctx->lpra[i].buf.pv,
  2472. uint64_to_ptr(rpra[i].buf.pv),
  2473. rpra[i].buf.len);
  2474. if (err) {
  2475. ADSPRPC_ERR(
  2476. "Invalid size 0x%llx for output argument %d ret %ld\n",
  2477. rpra[i].buf.len, i+1, err);
  2478. err = -EFAULT;
  2479. goto bail;
  2480. }
  2481. } else {
  2482. mutex_lock(&ctx->fl->map_mutex);
  2483. /*
  2484. * Decrement ctx refs count before mmap free,
  2485. * indicate remote call no longer using it
  2486. */
  2487. if (ctx->maps[i]->ctx_refs)
  2488. ctx->maps[i]->ctx_refs--;
  2489. fastrpc_mmap_free(ctx->maps[i], 0);
  2490. mutex_unlock(&ctx->fl->map_mutex);
  2491. ctx->maps[i] = NULL;
  2492. }
  2493. }
  2494. mutex_lock(&ctx->fl->map_mutex);
  2495. for (i = 0; i < M_FDLIST; i++) {
  2496. if (!fdlist[i])
  2497. break;
  2498. if (!fastrpc_mmap_find(ctx->fl, (int)fdlist[i], NULL, 0, 0,
  2499. 0, 0, &mmap)) {
  2500. /*
  2501. * Decrement ctx refs count before mmap free,
  2502. * indicate remote call no longer using it
  2503. */
  2504. if (mmap && mmap->ctx_refs)
  2505. mmap->ctx_refs--;
  2506. fastrpc_mmap_free(mmap, 0);
  2507. }
  2508. }
  2509. mutex_unlock(&ctx->fl->map_mutex);
  2510. if (ctx->crc && crclist && rpra)
  2511. K_COPY_TO_USER(err, kernel, ctx->crc,
  2512. crclist, M_CRCLIST*sizeof(uint32_t));
  2513. if (ctx->perf_dsp && perf_dsp_list) {
  2514. K_COPY_TO_USER(perfErr, kernel, ctx->perf_dsp,
  2515. perf_dsp_list, M_DSP_PERF_LIST*sizeof(uint64_t));
  2516. if (perfErr)
  2517. ADSPRPC_WARN("failed to copy perf data err %d\n", perfErr);
  2518. }
  2519. bail:
  2520. return err;
  2521. }
  2522. static void inv_args(struct smq_invoke_ctx *ctx)
  2523. {
  2524. int i, inbufs, outbufs;
  2525. uint32_t sc = ctx->sc;
  2526. remote_arg64_t *rpra = ctx->lrpra;
  2527. int err = 0;
  2528. inbufs = REMOTE_SCALARS_INBUFS(sc);
  2529. outbufs = REMOTE_SCALARS_OUTBUFS(sc);
  2530. for (i = 0; i < inbufs + outbufs; ++i) {
  2531. int over = ctx->overps[i]->raix;
  2532. struct fastrpc_mmap *map = ctx->maps[over];
  2533. if ((over + 1 <= inbufs))
  2534. continue;
  2535. if (!rpra[over].buf.len)
  2536. continue;
  2537. if (ctx->fl && ctx->fl->sctx && ctx->fl->sctx->smmu.coherent)
  2538. continue;
  2539. if (map && (map->attr & FASTRPC_ATTR_FORCE_NOINVALIDATE))
  2540. continue;
  2541. if (buf_page_start(ptr_to_uint64((void *)rpra)) ==
  2542. buf_page_start(rpra[over].buf.pv)) {
  2543. continue;
  2544. }
  2545. if (ctx->overps[i]->mstart || ctx->overps[i]->do_cmo == 1) {
  2546. if (map && map->buf) {
  2547. if (((buf_page_size(ctx->overps[i]->mend -
  2548. ctx->overps[i]->mstart)) == map->size) ||
  2549. ctx->overps[i]->do_cmo) {
  2550. dma_buf_begin_cpu_access(map->buf,
  2551. DMA_TO_DEVICE);
  2552. dma_buf_end_cpu_access(map->buf,
  2553. DMA_FROM_DEVICE);
  2554. ADSPRPC_DEBUG(
  2555. "sc 0x%x pv 0x%llx, mend 0x%llx mstart 0x%llx, len %zu size %zu\n",
  2556. sc, rpra[over].buf.pv,
  2557. ctx->overps[i]->mend,
  2558. ctx->overps[i]->mstart,
  2559. rpra[over].buf.len, map->size);
  2560. } else {
  2561. uintptr_t offset;
  2562. uint64_t inv_len;
  2563. struct vm_area_struct *vma;
  2564. down_read(&current->mm->mmap_lock);
  2565. VERIFY(err, NULL != (vma = find_vma(
  2566. current->mm,
  2567. rpra[over].buf.pv)));
  2568. if (err) {
  2569. up_read(&current->mm->mmap_lock);
  2570. goto bail;
  2571. }
  2572. if (ctx->overps[i]->do_cmo) {
  2573. offset = rpra[over].buf.pv -
  2574. vma->vm_start;
  2575. inv_len = rpra[over].buf.len;
  2576. } else {
  2577. offset =
  2578. ctx->overps[i]->mstart -
  2579. vma->vm_start;
  2580. inv_len =
  2581. ctx->overps[i]->mend -
  2582. ctx->overps[i]->mstart;
  2583. }
  2584. up_read(&current->mm->mmap_lock);
  2585. dma_buf_begin_cpu_access_partial(
  2586. map->buf, DMA_TO_DEVICE, offset,
  2587. inv_len);
  2588. dma_buf_end_cpu_access_partial(map->buf,
  2589. DMA_FROM_DEVICE, offset,
  2590. inv_len);
  2591. ADSPRPC_DEBUG(
  2592. "sc 0x%x vm_start 0x%llx pv 0x%llx, offset 0x%llx, mend 0x%llx mstart 0x%llx, len %zu size %zu\n",
  2593. sc, vma->vm_start,
  2594. rpra[over].buf.pv,
  2595. offset, ctx->overps[i]->mend,
  2596. ctx->overps[i]->mstart,
  2597. rpra[over].buf.len, map->size);
  2598. }
  2599. }
  2600. }
  2601. }
  2602. bail:
  2603. return;
  2604. }
  2605. static int fastrpc_invoke_send(struct smq_invoke_ctx *ctx,
  2606. uint32_t kernel, uint32_t handle)
  2607. {
  2608. struct smq_msg *msg = &ctx->msg;
  2609. struct smq_msg msg_temp;
  2610. struct smq_invoke_ctx ctx_temp;
  2611. struct fastrpc_file *fl = ctx->fl;
  2612. struct fastrpc_channel_ctx *channel_ctx = NULL;
  2613. int err = 0, cid = -1;
  2614. uint32_t sc = ctx->sc;
  2615. int64_t ns = 0;
  2616. uint64_t xo_time_in_us = 0;
  2617. int isasync = (ctx->asyncjob.isasyncjob ? true : false);
  2618. unsigned long irq_flags = 0;
  2619. uint32_t index = 0;
  2620. if (!fl) {
  2621. err = -EBADF;
  2622. goto bail;
  2623. }
  2624. cid = fl->cid;
  2625. VERIFY(err, VALID_FASTRPC_CID(cid));
  2626. if (err) {
  2627. err = -ECHRNG;
  2628. goto bail;
  2629. }
  2630. channel_ctx = &fl->apps->channel[cid];
  2631. mutex_lock(&channel_ctx->smd_mutex);
  2632. /* Send unique fastrpc process ID to dsp */
  2633. msg->pid = fl->tgid_frpc;
  2634. msg->tid = current->pid;
  2635. if (kernel == KERNEL_MSG_WITH_ZERO_PID)
  2636. msg->pid = 0;
  2637. msg->invoke.header.ctx = ctx->ctxid | fl->pd;
  2638. msg->invoke.header.handle = handle;
  2639. msg->invoke.header.sc = sc;
  2640. msg->invoke.page.addr = ctx->buf ? ctx->buf->phys : 0;
  2641. msg->invoke.page.size = buf_page_size(ctx->used);
  2642. if (fl->ssrcount != channel_ctx->ssrcount) {
  2643. err = -ECONNRESET;
  2644. mutex_unlock(&channel_ctx->smd_mutex);
  2645. goto bail;
  2646. }
  2647. mutex_unlock(&channel_ctx->smd_mutex);
  2648. xo_time_in_us = CONVERT_CNT_TO_US(__arch_counter_get_cntvct());
  2649. if (isasync) {
  2650. /*
  2651. * After message is sent to DSP, async response thread could immediately
  2652. * get the response and free context, which will result in a use-after-free
  2653. * in this function. So use a local variable for message and context.
  2654. */
  2655. memcpy(&msg_temp, msg, sizeof(struct smq_msg));
  2656. msg = &msg_temp;
  2657. memcpy(&ctx_temp, ctx, sizeof(struct smq_invoke_ctx));
  2658. index = (uint32_t)GET_TABLE_IDX_FROM_CTXID(ctx->ctxid);
  2659. }
  2660. err = fastrpc_transport_send(cid, (void *)msg, sizeof(*msg), fl->tvm_remote_domain);
  2661. if (isasync) {
  2662. if (!err) {
  2663. /*
  2664. * Validate the ctx as this could have been already
  2665. * freed by async response.
  2666. */
  2667. spin_lock_irqsave(&channel_ctx->ctxlock, irq_flags);
  2668. if (index < FASTRPC_CTX_MAX && channel_ctx->ctxtable[index] == ctx)
  2669. ctx->is_job_sent_to_remote_ss = true;
  2670. spin_unlock_irqrestore(&channel_ctx->ctxlock, irq_flags);
  2671. }
  2672. ctx = &ctx_temp;
  2673. }
  2674. trace_fastrpc_transport_send(cid, (uint64_t)ctx, msg->invoke.header.ctx,
  2675. handle, sc, msg->invoke.page.addr, msg->invoke.page.size);
  2676. ns = get_timestamp_in_ns();
  2677. fastrpc_update_txmsg_buf(msg, err, ns, xo_time_in_us, ctx, DEFAULT_STATE);
  2678. bail:
  2679. return err;
  2680. }
  2681. /* fastrpc_get_nsp_status() - Reads the property string from soc_info
  2682. * denoted for nsp part, and updates the nsp device avialbility status
  2683. * if the nsp is not defective.
  2684. * @me : pointer to fastrpc_apps.
  2685. */
  2686. static void fastrpc_get_nsp_status(struct fastrpc_apps *me)
  2687. {
  2688. if (socinfo_get_part_info(PART_NSP)) {
  2689. me->fastrpc_nsp_status = 0;
  2690. ADSPRPC_ERR(
  2691. "nsp part defective with status:%x\n", me->fastrpc_nsp_status);
  2692. } else {
  2693. me->fastrpc_nsp_status = 1;
  2694. ADSPRPC_INFO("nsp available with status: %x\n", me->fastrpc_nsp_status);
  2695. }
  2696. }
  2697. /*
  2698. * Counts number of cores corresponding
  2699. * to cluster id 0. If a core is defective or unavailable, skip counting
  2700. * that core.
  2701. * @me : pointer to fastrpc_apps.
  2702. */
  2703. static void fastrpc_lowest_capacity_corecount(struct fastrpc_apps *me)
  2704. {
  2705. unsigned int cpu = 0;
  2706. cpu = cpumask_first(cpu_possible_mask);
  2707. for_each_cpu(cpu, cpu_possible_mask) {
  2708. if (topology_cluster_id(cpu) == 0)
  2709. me->lowest_capacity_core_count++;
  2710. }
  2711. ADSPRPC_INFO("lowest capacity core count: %u\n",
  2712. me->lowest_capacity_core_count);
  2713. }
  2714. static void fastrpc_init(struct fastrpc_apps *me)
  2715. {
  2716. int i, jj;
  2717. INIT_HLIST_HEAD(&me->drivers);
  2718. INIT_HLIST_HEAD(&me->maps);
  2719. spin_lock_init(&me->hlock);
  2720. me->channel = &gcinfo[0];
  2721. mutex_init(&me->mut_uid);
  2722. me->max_sess_per_proc = DEFAULT_MAX_SESS_PER_PROC;
  2723. for (i = 0; i < NUM_CHANNELS; i++) {
  2724. init_completion(&me->channel[i].work);
  2725. init_completion(&me->channel[i].workport);
  2726. me->channel[i].sesscount = 0;
  2727. /* All channels are secure by default except CDSP */
  2728. me->channel[i].secure = SECURE_CHANNEL;
  2729. me->channel[i].unsigned_support = false;
  2730. mutex_init(&me->channel[i].smd_mutex);
  2731. fastrpc_transport_session_init(i, me->channel[i].subsys);
  2732. spin_lock_init(&me->channel[i].ctxlock);
  2733. spin_lock_init(&me->channel[i].gmsg_log.lock);
  2734. INIT_HLIST_HEAD(&me->channel[i].initmems);
  2735. for (jj = 0; jj < NUM_SESSIONS; jj++)
  2736. init_waitqueue_head(&me->channel[i].spd[jj].wait_for_pdup);
  2737. }
  2738. /* Set CDSP channel to non secure */
  2739. me->channel[CDSP_DOMAIN_ID].secure = NON_SECURE_CHANNEL;
  2740. me->channel[CDSP_DOMAIN_ID].unsigned_support = true;
  2741. }
  2742. static inline void fastrpc_pm_awake(struct fastrpc_file *fl, int channel_type)
  2743. {
  2744. struct fastrpc_apps *me = &gfa;
  2745. struct wakeup_source *wake_source = NULL;
  2746. if (!fl->wake_enable)
  2747. return;
  2748. /*
  2749. * Vote with PM to abort any suspend in progress and
  2750. * keep system awake for specified timeout
  2751. */
  2752. if (channel_type == SECURE_CHANNEL)
  2753. wake_source = me->wake_source_secure;
  2754. else if (channel_type == NON_SECURE_CHANNEL)
  2755. wake_source = me->wake_source;
  2756. if (wake_source)
  2757. pm_wakeup_ws_event(wake_source, fl->ws_timeout, true);
  2758. }
  2759. static inline int fastrpc_wait_for_response(struct smq_invoke_ctx *ctx,
  2760. uint32_t kernel)
  2761. {
  2762. int interrupted = 0;
  2763. if (kernel)
  2764. wait_for_completion(&ctx->work);
  2765. else
  2766. interrupted = wait_for_completion_interruptible(&ctx->work);
  2767. return interrupted;
  2768. }
  2769. static void fastrpc_wait_for_completion(struct smq_invoke_ctx *ctx,
  2770. int *ptr_interrupted, uint32_t kernel, uint32_t async,
  2771. bool *ptr_isworkdone)
  2772. {
  2773. int interrupted = 0, err = 0;
  2774. int jj;
  2775. bool wait_resp;
  2776. uint32_t wTimeout = FASTRPC_USER_EARLY_HINT_TIMEOUT;
  2777. uint32_t wakeTime = 0;
  2778. unsigned long flags;
  2779. if (!ctx) {
  2780. /* This failure is not expected */
  2781. err = *ptr_interrupted = EFAULT;
  2782. *ptr_isworkdone = false;
  2783. ADSPRPC_ERR("ctx is NULL, cannot wait for response err %d\n",
  2784. err);
  2785. return;
  2786. }
  2787. wakeTime = ctx->early_wake_time;
  2788. do {
  2789. switch (ctx->rsp_flags) {
  2790. /* try polling on completion with timeout */
  2791. case USER_EARLY_SIGNAL:
  2792. /* try wait if completion time is less than timeout */
  2793. /* disable preempt to avoid context switch latency */
  2794. preempt_disable();
  2795. jj = 0;
  2796. wait_resp = false;
  2797. for (; wakeTime < wTimeout && jj < wTimeout; jj++) {
  2798. wait_resp = try_wait_for_completion(&ctx->work);
  2799. if (wait_resp)
  2800. break;
  2801. udelay(1);
  2802. }
  2803. preempt_enable();
  2804. if (async) {
  2805. spin_lock_irqsave(&ctx->fl->aqlock, flags);
  2806. if (!ctx->is_work_done) {
  2807. ctx->is_early_wakeup = false;
  2808. *ptr_isworkdone = false;
  2809. } else
  2810. *ptr_isworkdone = true;
  2811. spin_unlock_irqrestore(&ctx->fl->aqlock, flags);
  2812. goto bail;
  2813. } else if (!wait_resp) {
  2814. interrupted = fastrpc_wait_for_response(ctx,
  2815. kernel);
  2816. *ptr_interrupted = interrupted;
  2817. if (interrupted || ctx->is_work_done)
  2818. goto bail;
  2819. }
  2820. break;
  2821. /* busy poll on memory for actual job done */
  2822. case EARLY_RESPONSE:
  2823. trace_fastrpc_msg("early_response: poll_begin");
  2824. err = poll_for_remote_response(ctx, FASTRPC_POLL_TIME);
  2825. /* Mark job done if poll on memory successful */
  2826. /* Wait for completion if poll on memory timoeut */
  2827. if (!err) {
  2828. ctx->is_work_done = true;
  2829. *ptr_isworkdone = true;
  2830. goto bail;
  2831. }
  2832. trace_fastrpc_msg("early_response: poll_timeout");
  2833. ADSPRPC_INFO("early rsp poll timeout (%u us) for handle 0x%x, sc 0x%x\n",
  2834. FASTRPC_POLL_TIME, ctx->handle, ctx->sc);
  2835. if (async) {
  2836. spin_lock_irqsave(&ctx->fl->aqlock, flags);
  2837. if (!ctx->is_work_done) {
  2838. ctx->is_early_wakeup = false;
  2839. *ptr_isworkdone = false;
  2840. } else
  2841. *ptr_isworkdone = true;
  2842. spin_unlock_irqrestore(&ctx->fl->aqlock, flags);
  2843. goto bail;
  2844. } else if (!ctx->is_work_done) {
  2845. interrupted = fastrpc_wait_for_response(ctx,
  2846. kernel);
  2847. *ptr_interrupted = interrupted;
  2848. if (interrupted || ctx->is_work_done)
  2849. goto bail;
  2850. }
  2851. break;
  2852. case COMPLETE_SIGNAL:
  2853. case NORMAL_RESPONSE:
  2854. if (!async) {
  2855. interrupted = fastrpc_wait_for_response(ctx,
  2856. kernel);
  2857. *ptr_interrupted = interrupted;
  2858. if (interrupted || ctx->is_work_done)
  2859. goto bail;
  2860. } else {
  2861. spin_lock_irqsave(&ctx->fl->aqlock, flags);
  2862. if (!ctx->is_work_done) {
  2863. ctx->is_early_wakeup = false;
  2864. *ptr_isworkdone = false;
  2865. } else
  2866. *ptr_isworkdone = true;
  2867. spin_unlock_irqrestore(&ctx->fl->aqlock, flags);
  2868. goto bail;
  2869. }
  2870. break;
  2871. case POLL_MODE:
  2872. trace_fastrpc_msg("poll_mode: begin");
  2873. err = poll_for_remote_response(ctx, ctx->fl->poll_timeout);
  2874. /* If polling timed out, move to normal response state */
  2875. if (err) {
  2876. trace_fastrpc_msg("poll_mode: timeout");
  2877. ADSPRPC_INFO("poll mode timeout (%u us) for handle 0x%x, sc 0x%x\n",
  2878. ctx->fl->poll_timeout, ctx->handle, ctx->sc);
  2879. ctx->rsp_flags = NORMAL_RESPONSE;
  2880. } else {
  2881. *ptr_interrupted = 0;
  2882. *ptr_isworkdone = true;
  2883. }
  2884. break;
  2885. default:
  2886. *ptr_interrupted = EBADR;
  2887. *ptr_isworkdone = false;
  2888. ADSPRPC_ERR(
  2889. "unsupported response flags 0x%x for handle 0x%x, sc 0x%x\n",
  2890. ctx->rsp_flags, ctx->handle, ctx->sc);
  2891. goto bail;
  2892. } /* end of switch */
  2893. } while (!ctx->is_work_done);
  2894. bail:
  2895. return;
  2896. }
  2897. static void fastrpc_update_invoke_count(uint32_t handle, uint64_t *perf_counter,
  2898. struct timespec64 *invoket)
  2899. {
  2900. /* update invoke count for dynamic handles */
  2901. if (handle != FASTRPC_STATIC_HANDLE_LISTENER) {
  2902. uint64_t *count = GET_COUNTER(perf_counter, PERF_INVOKE);
  2903. if (count)
  2904. *count += getnstimediff(invoket);
  2905. }
  2906. if (handle > FASTRPC_STATIC_HANDLE_MAX) {
  2907. uint64_t *count = GET_COUNTER(perf_counter, PERF_COUNT);
  2908. if (count)
  2909. *count += 1;
  2910. }
  2911. }
  2912. static int fastrpc_check_pd_status(struct fastrpc_file *fl, char *sloc_name);
  2913. int fastrpc_internal_invoke(struct fastrpc_file *fl, uint32_t mode,
  2914. uint32_t kernel,
  2915. struct fastrpc_ioctl_invoke_async *inv)
  2916. {
  2917. struct smq_invoke_ctx *ctx = NULL;
  2918. struct fastrpc_ioctl_invoke *invoke = &inv->inv;
  2919. int err = 0, interrupted = 0, cid = -1, perfErr = 0;
  2920. struct timespec64 invoket = {0};
  2921. uint64_t *perf_counter = NULL;
  2922. bool isasyncinvoke = false, isworkdone = false;
  2923. cid = fl->cid;
  2924. VERIFY(err, VALID_FASTRPC_CID(cid) &&
  2925. fl->sctx != NULL);
  2926. if (err) {
  2927. ADSPRPC_ERR("kernel session not initialized yet for %s\n",
  2928. current->comm);
  2929. err = -EBADR;
  2930. goto bail;
  2931. }
  2932. if (fl->profile)
  2933. ktime_get_real_ts64(&invoket);
  2934. if (!kernel) {
  2935. VERIFY(err, invoke->handle !=
  2936. FASTRPC_STATIC_HANDLE_PROCESS_GROUP);
  2937. VERIFY(err, invoke->handle !=
  2938. FASTRPC_STATIC_HANDLE_DSP_UTILITIES);
  2939. if (err) {
  2940. err = -EINVAL;
  2941. ADSPRPC_ERR(
  2942. "user application trying to send a kernel RPC message to channel %d, handle 0x%x\n",
  2943. cid, invoke->handle);
  2944. goto bail;
  2945. }
  2946. }
  2947. if (!kernel) {
  2948. VERIFY(err, 0 == (err = context_restore_interrupted(fl,
  2949. inv, &ctx)));
  2950. if (err)
  2951. goto bail;
  2952. if (fl->sctx->smmu.faults)
  2953. err = -FASTRPC_ENOSUCH;
  2954. if (err)
  2955. goto bail;
  2956. if (ctx) {
  2957. trace_fastrpc_context_restore(cid, (uint64_t)ctx,
  2958. ctx->msg.invoke.header.ctx,
  2959. ctx->handle, ctx->sc);
  2960. goto wait;
  2961. }
  2962. }
  2963. trace_fastrpc_msg("context_alloc: begin");
  2964. VERIFY(err, 0 == (err = context_alloc(fl, kernel, inv, &ctx)));
  2965. trace_fastrpc_msg("context_alloc: end");
  2966. if (err)
  2967. goto bail;
  2968. if (fl->servloc_name) {
  2969. err = fastrpc_check_pd_status(fl,
  2970. AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME);
  2971. err |= fastrpc_check_pd_status(fl,
  2972. SENSORS_PDR_ADSP_SERVICE_LOCATION_CLIENT_NAME);
  2973. err |= fastrpc_check_pd_status(fl,
  2974. SENSORS_PDR_SLPI_SERVICE_LOCATION_CLIENT_NAME);
  2975. if (err)
  2976. goto bail;
  2977. }
  2978. isasyncinvoke = (ctx->asyncjob.isasyncjob ? true : false);
  2979. if (fl->profile)
  2980. perf_counter = (uint64_t *)ctx->perf + PERF_COUNT;
  2981. PERF(fl->profile, GET_COUNTER(perf_counter, PERF_GETARGS),
  2982. VERIFY(err, 0 == (err = get_args(kernel, ctx)));
  2983. PERF_END);
  2984. trace_fastrpc_msg("get_args: end");
  2985. if (err)
  2986. goto bail;
  2987. PERF(fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS),
  2988. inv_args(ctx);
  2989. PERF_END);
  2990. trace_fastrpc_msg("inv_args_1: end");
  2991. PERF(fl->profile, GET_COUNTER(perf_counter, PERF_LINK),
  2992. VERIFY(err, 0 == (err = fastrpc_invoke_send(ctx,
  2993. kernel, invoke->handle)));
  2994. PERF_END);
  2995. trace_fastrpc_msg("invoke_send: end");
  2996. if (err)
  2997. goto bail;
  2998. if (isasyncinvoke)
  2999. goto invoke_end;
  3000. wait:
  3001. /* Poll mode allowed only for non-static handle calls to dynamic CDSP process */
  3002. if (fl->poll_mode && (invoke->handle > FASTRPC_STATIC_HANDLE_MAX)
  3003. && (cid == CDSP_DOMAIN_ID)
  3004. && (fl->proc_flags == FASTRPC_INIT_CREATE))
  3005. ctx->rsp_flags = POLL_MODE;
  3006. fastrpc_wait_for_completion(ctx, &interrupted, kernel, 0, &isworkdone);
  3007. trace_fastrpc_msg("wait_for_completion: end");
  3008. VERIFY(err, 0 == (err = interrupted));
  3009. if (err)
  3010. goto bail;
  3011. if (!ctx->is_work_done) {
  3012. err = -ETIMEDOUT;
  3013. ADSPRPC_ERR(
  3014. "WorkDone state is invalid for handle 0x%x, sc 0x%x\n",
  3015. invoke->handle, ctx->sc);
  3016. goto bail;
  3017. }
  3018. PERF(fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS),
  3019. inv_args(ctx);
  3020. PERF_END);
  3021. trace_fastrpc_msg("inv_args_2: end");
  3022. PERF(fl->profile, GET_COUNTER(perf_counter, PERF_PUTARGS),
  3023. VERIFY(err, 0 == (err = put_args(kernel, ctx, invoke->pra)));
  3024. PERF_END);
  3025. trace_fastrpc_msg("put_args: end");
  3026. if (err)
  3027. goto bail;
  3028. VERIFY(err, 0 == (err = ctx->retval));
  3029. if (err)
  3030. goto bail;
  3031. bail:
  3032. if (ctx && interrupted == -ERESTARTSYS) {
  3033. trace_fastrpc_context_interrupt(cid, (uint64_t)ctx,
  3034. ctx->msg.invoke.header.ctx, ctx->handle, ctx->sc);
  3035. context_save_interrupted(ctx);
  3036. } else if (ctx) {
  3037. if (fl->profile && !interrupted)
  3038. fastrpc_update_invoke_count(invoke->handle,
  3039. perf_counter, &invoket);
  3040. if (fl->profile && ctx->perf && ctx->handle > FASTRPC_STATIC_HANDLE_MAX) {
  3041. trace_fastrpc_perf_counters(ctx->handle, ctx->sc,
  3042. ctx->perf->count, ctx->perf->flush, ctx->perf->map,
  3043. ctx->perf->copy, ctx->perf->link, ctx->perf->getargs,
  3044. ctx->perf->putargs, ctx->perf->invargs,
  3045. ctx->perf->invoke, ctx->perf->tid);
  3046. if (ctx->perf_kernel) {
  3047. K_COPY_TO_USER(perfErr, kernel, ctx->perf_kernel,
  3048. ctx->perf, M_KERNEL_PERF_LIST*sizeof(uint64_t));
  3049. if (perfErr)
  3050. ADSPRPC_WARN("failed to copy perf data err %d\n", perfErr);
  3051. }
  3052. }
  3053. context_free(ctx);
  3054. trace_fastrpc_msg("context_free: end");
  3055. }
  3056. if (!kernel) {
  3057. if (VALID_FASTRPC_CID(cid)
  3058. && (fl->ssrcount != fl->apps->channel[cid].ssrcount))
  3059. err = -ECONNRESET;
  3060. }
  3061. invoke_end:
  3062. if (fl->profile && !interrupted && isasyncinvoke)
  3063. fastrpc_update_invoke_count(invoke->handle, perf_counter,
  3064. &invoket);
  3065. return err;
  3066. }
  3067. static int fastrpc_wait_on_async_queue(
  3068. struct fastrpc_ioctl_async_response *async_res,
  3069. struct fastrpc_file *fl)
  3070. {
  3071. int err = 0, ierr = 0, interrupted = 0, perfErr = 0;
  3072. struct smq_invoke_ctx *ctx = NULL, *ictx = NULL, *n = NULL;
  3073. unsigned long flags;
  3074. uint64_t *perf_counter = NULL;
  3075. bool isworkdone = false;
  3076. read_async_job:
  3077. interrupted = wait_event_interruptible(fl->async_wait_queue,
  3078. atomic_read(&fl->async_queue_job_count));
  3079. if (!fl || fl->file_close >= FASTRPC_PROCESS_EXIT_START) {
  3080. err = -EBADF;
  3081. goto bail;
  3082. }
  3083. if (fl->exit_async) {
  3084. err = -EFAULT;
  3085. goto bail;
  3086. }
  3087. VERIFY(err, 0 == (err = interrupted));
  3088. if (err)
  3089. goto bail;
  3090. spin_lock_irqsave(&fl->aqlock, flags);
  3091. list_for_each_entry_safe(ictx, n, &fl->clst.async_queue, asyncn) {
  3092. list_del_init(&ictx->asyncn);
  3093. atomic_sub(1, &fl->async_queue_job_count);
  3094. ctx = ictx;
  3095. break;
  3096. }
  3097. spin_unlock_irqrestore(&fl->aqlock, flags);
  3098. if (ctx) {
  3099. if (fl->profile)
  3100. perf_counter = (uint64_t *)ctx->perf + PERF_COUNT;
  3101. fastrpc_wait_for_completion(ctx, &interrupted, 0, 1,
  3102. &isworkdone);
  3103. if (!isworkdone) {//In valid workdone state
  3104. ADSPRPC_DEBUG(
  3105. "Async early wake response did not reach on time for thread %d handle 0x%x, sc 0x%x\n",
  3106. ctx->pid, ctx->handle, ctx->sc);
  3107. goto read_async_job;
  3108. }
  3109. async_res->jobid = ctx->asyncjob.jobid;
  3110. async_res->result = ctx->retval;
  3111. async_res->handle = ctx->handle;
  3112. async_res->sc = ctx->sc;
  3113. async_res->perf_dsp = (uint64_t *)ctx->perf_dsp;
  3114. async_res->perf_kernel = (uint64_t *)ctx->perf_kernel;
  3115. PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_INVARGS),
  3116. inv_args(ctx);
  3117. PERF_END);
  3118. if (ctx->retval != 0)
  3119. goto bail;
  3120. PERF(ctx->fl->profile, GET_COUNTER(perf_counter, PERF_PUTARGS),
  3121. VERIFY(ierr, 0 == (ierr = put_args(0, ctx, NULL)));
  3122. PERF_END);
  3123. if (ierr)
  3124. goto bail;
  3125. } else { // Go back to wait if ctx is invalid
  3126. ADSPRPC_ERR("Invalid async job wake up\n");
  3127. goto read_async_job;
  3128. }
  3129. bail:
  3130. if (ierr)
  3131. async_res->result = ierr;
  3132. if (ctx) {
  3133. if (fl->profile && ctx->perf && ctx->handle > FASTRPC_STATIC_HANDLE_MAX) {
  3134. trace_fastrpc_perf_counters(ctx->handle, ctx->sc,
  3135. ctx->perf->count, ctx->perf->flush, ctx->perf->map,
  3136. ctx->perf->copy, ctx->perf->link, ctx->perf->getargs,
  3137. ctx->perf->putargs, ctx->perf->invargs,
  3138. ctx->perf->invoke, ctx->perf->tid);
  3139. if (ctx->perf_kernel) {
  3140. K_COPY_TO_USER(perfErr, 0, ctx->perf_kernel,
  3141. ctx->perf, M_KERNEL_PERF_LIST*sizeof(uint64_t));
  3142. if (perfErr)
  3143. ADSPRPC_WARN("failed to copy perf data err %d\n", perfErr);
  3144. }
  3145. }
  3146. context_free(ctx);
  3147. }
  3148. return err;
  3149. }
  3150. static int fastrpc_wait_on_notif_queue(
  3151. struct fastrpc_ioctl_notif_rsp *notif_rsp,
  3152. struct fastrpc_file *fl)
  3153. {
  3154. int err = 0, interrupted = 0;
  3155. unsigned long flags;
  3156. struct smq_notif_rsp *notif = NULL, *inotif = NULL, *n = NULL;
  3157. read_notif_status:
  3158. interrupted = wait_event_interruptible(fl->proc_state_notif.notif_wait_queue,
  3159. atomic_read(&fl->proc_state_notif.notif_queue_count));
  3160. if (!fl) {
  3161. err = -EBADF;
  3162. goto bail;
  3163. }
  3164. if (fl->exit_notif) {
  3165. err = -EFAULT;
  3166. goto bail;
  3167. }
  3168. VERIFY(err, 0 == (err = interrupted));
  3169. if (err)
  3170. goto bail;
  3171. spin_lock_irqsave(&fl->proc_state_notif.nqlock, flags);
  3172. list_for_each_entry_safe(inotif, n, &fl->clst.notif_queue, notifn) {
  3173. list_del_init(&inotif->notifn);
  3174. atomic_sub(1, &fl->proc_state_notif.notif_queue_count);
  3175. notif = inotif;
  3176. break;
  3177. }
  3178. spin_unlock_irqrestore(&fl->proc_state_notif.nqlock, flags);
  3179. if (notif) {
  3180. notif_rsp->status = notif->status;
  3181. notif_rsp->domain = notif->domain;
  3182. notif_rsp->session = notif->session;
  3183. } else {// Go back to wait if ctx is invalid
  3184. ADSPRPC_ERR("Invalid status notification response\n");
  3185. goto read_notif_status;
  3186. }
  3187. bail:
  3188. kfree(notif);
  3189. return err;
  3190. }
  3191. static int fastrpc_get_async_response(
  3192. struct fastrpc_ioctl_async_response *async_res,
  3193. void *param, struct fastrpc_file *fl)
  3194. {
  3195. int err = 0;
  3196. err = fastrpc_wait_on_async_queue(async_res, fl);
  3197. if (err)
  3198. goto bail;
  3199. K_COPY_TO_USER(err, 0, param, async_res,
  3200. sizeof(struct fastrpc_ioctl_async_response));
  3201. bail:
  3202. return err;
  3203. }
  3204. static int fastrpc_get_notif_response(
  3205. struct fastrpc_ioctl_notif_rsp *notif,
  3206. void *param, struct fastrpc_file *fl)
  3207. {
  3208. int err = 0;
  3209. err = fastrpc_wait_on_notif_queue(notif, fl);
  3210. if (err)
  3211. goto bail;
  3212. K_COPY_TO_USER(err, 0, param, notif,
  3213. sizeof(struct fastrpc_ioctl_notif_rsp));
  3214. bail:
  3215. return err;
  3216. }
  3217. static int fastrpc_set_session_info(
  3218. struct fastrpc_proc_sess_info *sess_info,
  3219. void *param, struct fastrpc_file *fl)
  3220. {
  3221. int err = 0;
  3222. struct fastrpc_apps *me = &gfa;
  3223. if (fl->set_session_info) {
  3224. ADSPRPC_ERR("Set session info invoked multiple times\n");
  3225. err = -EBADR;
  3226. goto bail;
  3227. }
  3228. /*
  3229. * Third-party apps don't have permission to open the fastrpc device, so
  3230. * it is opened on their behalf by DSP HAL. This is detected by
  3231. * comparing current PID with the one stored during device open.
  3232. */
  3233. if (current->tgid != fl->tgid_open)
  3234. fl->untrusted_process = true;
  3235. VERIFY(err, sess_info->pd_type > DEFAULT_UNUSED &&
  3236. sess_info->pd_type < MAX_PD_TYPE);
  3237. if (err) {
  3238. ADSPRPC_ERR(
  3239. "Session PD type %u is invalid for the process\n",
  3240. sess_info->pd_type);
  3241. err = -EBADR;
  3242. goto bail;
  3243. }
  3244. if (fl->untrusted_process && sess_info->pd_type != USERPD) {
  3245. ADSPRPC_ERR(
  3246. "Session PD type %u not allowed for untrusted process\n",
  3247. sess_info->pd_type);
  3248. err = -EBADR;
  3249. goto bail;
  3250. }
  3251. /*
  3252. * If PD type is not configured for context banks,
  3253. * ignore PD type passed by the user, leave pd_type set to DEFAULT_UNUSED(0)
  3254. */
  3255. if (me->cb_pd_type)
  3256. fl->pd_type = sess_info->pd_type;
  3257. // Processes attaching to Sensor Static PD, share context bank.
  3258. if (sess_info->pd_type == SENSORS_STATICPD)
  3259. fl->sharedcb = 1;
  3260. if (sess_info->session_id >= me->max_sess_per_proc) {
  3261. ADSPRPC_ERR(
  3262. "Session ID %u cannot be beyond %u\n",
  3263. sess_info->session_id, me->max_sess_per_proc);
  3264. err = -EBADR;
  3265. goto bail;
  3266. }
  3267. fl->sessionid = sess_info->session_id;
  3268. // Set multi_session_support, to disable old way of setting session_id
  3269. fl->multi_session_support = true;
  3270. VERIFY(err, 0 == (err = fastrpc_get_info(fl, &(sess_info->domain_id))));
  3271. if (err)
  3272. goto bail;
  3273. K_COPY_TO_USER(err, 0, param, sess_info,
  3274. sizeof(struct fastrpc_proc_sess_info));
  3275. bail:
  3276. return err;
  3277. }
  3278. static int fastrpc_create_persistent_headers(struct fastrpc_file *fl,
  3279. uint32_t user_concurrency)
  3280. {
  3281. int err = 0, i = 0;
  3282. uint64_t virtb = 0;
  3283. struct fastrpc_buf *pers_hdr_buf = NULL, *hdr_bufs = NULL, *buf = NULL;
  3284. unsigned int num_pers_hdrs = 0;
  3285. size_t hdr_buf_alloc_len = 0;
  3286. if (fl->pers_hdr_buf || !user_concurrency)
  3287. goto bail;
  3288. /*
  3289. * Pre-allocate memory for persistent header buffers based
  3290. * on concurrency info passed by user. Upper limit enforced.
  3291. */
  3292. num_pers_hdrs = (user_concurrency > MAX_PERSISTENT_HEADERS) ?
  3293. MAX_PERSISTENT_HEADERS : user_concurrency;
  3294. hdr_buf_alloc_len = num_pers_hdrs*PAGE_SIZE;
  3295. err = fastrpc_buf_alloc(fl, hdr_buf_alloc_len, 0, 0,
  3296. METADATA_BUF, &pers_hdr_buf);
  3297. if (err)
  3298. goto bail;
  3299. virtb = ptr_to_uint64(pers_hdr_buf->virt);
  3300. /* Map entire buffer on remote subsystem in single RPC call */
  3301. err = fastrpc_mem_map_to_dsp(fl, -1, 0, ADSP_MMAP_PERSIST_HDR, 0,
  3302. pers_hdr_buf->phys, pers_hdr_buf->size,
  3303. &pers_hdr_buf->raddr);
  3304. if (err)
  3305. goto bail;
  3306. /* Divide and store as N chunks, each of 1 page size */
  3307. hdr_bufs = kcalloc(num_pers_hdrs, sizeof(struct fastrpc_buf),
  3308. GFP_KERNEL);
  3309. if (!hdr_bufs) {
  3310. err = -ENOMEM;
  3311. goto bail;
  3312. }
  3313. spin_lock(&fl->hlock);
  3314. fl->pers_hdr_buf = pers_hdr_buf;
  3315. fl->num_pers_hdrs = num_pers_hdrs;
  3316. fl->hdr_bufs = hdr_bufs;
  3317. for (i = 0; i < num_pers_hdrs; i++) {
  3318. buf = &fl->hdr_bufs[i];
  3319. buf->fl = fl;
  3320. buf->virt = uint64_to_ptr(virtb + (i*PAGE_SIZE));
  3321. buf->phys = pers_hdr_buf->phys + (i*PAGE_SIZE);
  3322. buf->size = PAGE_SIZE;
  3323. buf->dma_attr = pers_hdr_buf->dma_attr;
  3324. buf->flags = pers_hdr_buf->flags;
  3325. buf->type = pers_hdr_buf->type;
  3326. buf->in_use = false;
  3327. }
  3328. spin_unlock(&fl->hlock);
  3329. bail:
  3330. if (err) {
  3331. ADSPRPC_ERR(
  3332. "failed to map len %zu, flags %d, user concurrency %u, num headers %u with err %d\n",
  3333. hdr_buf_alloc_len, ADSP_MMAP_PERSIST_HDR,
  3334. user_concurrency, num_pers_hdrs, err);
  3335. fl->pers_hdr_buf = NULL;
  3336. fl->hdr_bufs = NULL;
  3337. fl->num_pers_hdrs = 0;
  3338. if (!IS_ERR_OR_NULL(pers_hdr_buf))
  3339. fastrpc_buf_free(pers_hdr_buf, 0);
  3340. if (!IS_ERR_OR_NULL(hdr_bufs))
  3341. kfree(hdr_bufs);
  3342. }
  3343. return err;
  3344. }
  3345. int fastrpc_internal_invoke2(struct fastrpc_file *fl,
  3346. struct fastrpc_ioctl_invoke2 *inv2)
  3347. {
  3348. union {
  3349. struct fastrpc_ioctl_invoke_async inv;
  3350. struct fastrpc_ioctl_invoke_async_no_perf inv3;
  3351. struct fastrpc_ioctl_async_response async_res;
  3352. uint32_t user_concurrency;
  3353. struct fastrpc_ioctl_notif_rsp notif;
  3354. struct fastrpc_proc_sharedbuf_info buff_info;
  3355. struct fastrpc_proc_sess_info sess_info;
  3356. } p;
  3357. struct fastrpc_dsp_capabilities *dsp_cap_ptr = NULL;
  3358. uint32_t size = 0;
  3359. int err = 0, domain = fl->cid;
  3360. if (inv2->req == FASTRPC_INVOKE2_ASYNC ||
  3361. inv2->req == FASTRPC_INVOKE2_ASYNC_RESPONSE) {
  3362. VERIFY(err, domain == CDSP_DOMAIN_ID && fl->sctx != NULL);
  3363. if (err) {
  3364. err = -EBADR;
  3365. goto bail;
  3366. }
  3367. dsp_cap_ptr = &gcinfo[domain].dsp_cap_kernel;
  3368. VERIFY(err,
  3369. dsp_cap_ptr->dsp_attributes[ASYNC_FASTRPC_CAP] == 1);
  3370. if (err) {
  3371. err = -EPROTONOSUPPORT;
  3372. goto bail;
  3373. }
  3374. }
  3375. switch (inv2->req) {
  3376. case FASTRPC_INVOKE2_ASYNC:
  3377. size = sizeof(struct fastrpc_ioctl_invoke_async);
  3378. VERIFY(err, size >= inv2->size);
  3379. if (err) {
  3380. err = -EBADE;
  3381. goto bail;
  3382. }
  3383. if (size > inv2->size) {
  3384. K_COPY_FROM_USER(err, fl->is_compat, &p.inv3, (void *)inv2->invparam,
  3385. sizeof(struct fastrpc_ioctl_invoke_async_no_perf));
  3386. if (err)
  3387. goto bail;
  3388. memcpy(&p.inv, &p.inv3, sizeof(struct fastrpc_ioctl_invoke_crc));
  3389. memcpy(&p.inv.job, &p.inv3.job, sizeof(p.inv.job));
  3390. } else {
  3391. K_COPY_FROM_USER(err, fl->is_compat, &p.inv, (void *)inv2->invparam, size);
  3392. if (err)
  3393. goto bail;
  3394. }
  3395. VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, fl->mode,
  3396. USER_MSG, &p.inv)));
  3397. if (err)
  3398. goto bail;
  3399. break;
  3400. case FASTRPC_INVOKE2_ASYNC_RESPONSE:
  3401. VERIFY(err,
  3402. sizeof(struct fastrpc_ioctl_async_response) >= inv2->size);
  3403. if (err) {
  3404. err = -EBADE;
  3405. goto bail;
  3406. }
  3407. err = fastrpc_get_async_response(&p.async_res,
  3408. (void *)inv2->invparam, fl);
  3409. break;
  3410. case FASTRPC_INVOKE2_KERNEL_OPTIMIZATIONS:
  3411. size = sizeof(uint32_t);
  3412. if (inv2->size != size) {
  3413. err = -EBADE;
  3414. goto bail;
  3415. }
  3416. K_COPY_FROM_USER(err, 0, &p.user_concurrency,
  3417. (void *)inv2->invparam, size);
  3418. if (err)
  3419. goto bail;
  3420. err = fastrpc_create_persistent_headers(fl,
  3421. p.user_concurrency);
  3422. break;
  3423. case FASTRPC_INVOKE2_STATUS_NOTIF:
  3424. VERIFY(err,
  3425. sizeof(struct fastrpc_ioctl_notif_rsp) >= inv2->size);
  3426. if (err) {
  3427. err = -EBADE;
  3428. goto bail;
  3429. }
  3430. err = fastrpc_get_notif_response(&p.notif,
  3431. (void *)inv2->invparam, fl);
  3432. break;
  3433. case FASTRPC_INVOKE2_PROC_SHAREDBUF_INFO:
  3434. VERIFY(err,
  3435. sizeof(struct fastrpc_proc_sharedbuf_info) >= inv2->size);
  3436. if (err) {
  3437. err = -EBADE;
  3438. goto bail;
  3439. }
  3440. K_COPY_FROM_USER(err, fl->is_compat, &p.buff_info,
  3441. (void *)inv2->invparam, inv2->size);
  3442. if (err)
  3443. goto bail;
  3444. fl->sharedbuf_info.buf_fd = p.buff_info.buf_fd;
  3445. fl->sharedbuf_info.buf_size = p.buff_info.buf_size;
  3446. break;
  3447. case FASTRPC_INVOKE2_SESS_INFO:
  3448. VERIFY(err,
  3449. sizeof(struct fastrpc_proc_sess_info) >= inv2->size);
  3450. if (err) {
  3451. err = -EBADE;
  3452. goto bail;
  3453. }
  3454. K_COPY_FROM_USER(err, fl->is_compat, &p.sess_info,
  3455. (void *)inv2->invparam, inv2->size);
  3456. if (err)
  3457. goto bail;
  3458. err = fastrpc_set_session_info(&p.sess_info,
  3459. (void *)inv2->invparam, fl);
  3460. break;
  3461. default:
  3462. err = -ENOTTY;
  3463. break;
  3464. }
  3465. bail:
  3466. return err;
  3467. }
  3468. static int fastrpc_get_spd_session(char *name, int *session, int *cid)
  3469. {
  3470. struct fastrpc_apps *me = &gfa;
  3471. int err = 0, i, j, match = 0;
  3472. for (i = 0; i < NUM_CHANNELS; i++) {
  3473. for (j = 0; j < NUM_SESSIONS; j++) {
  3474. if (!me->channel[i].spd[j].servloc_name)
  3475. continue;
  3476. if (!strcmp(name, me->channel[i].spd[j].servloc_name)) {
  3477. match = 1;
  3478. break;
  3479. }
  3480. }
  3481. if (match)
  3482. break;
  3483. }
  3484. VERIFY(err, i < NUM_CHANNELS && j < NUM_SESSIONS);
  3485. if (err) {
  3486. err = -EUSERS;
  3487. goto bail;
  3488. }
  3489. *cid = i;
  3490. *session = j;
  3491. bail:
  3492. return err;
  3493. }
  3494. static int fastrpc_mmap_remove_pdr(struct fastrpc_file *fl);
  3495. static int fastrpc_channel_open(struct fastrpc_file *fl, uint32_t flags);
  3496. static int fastrpc_mmap_remove_ssr(struct fastrpc_file *fl, int locked);
  3497. /*
  3498. * This function makes a call to create a thread group in the root
  3499. * process or static process on the remote subsystem.
  3500. * Examples:
  3501. * - guestOS daemons on all DSPs
  3502. * - sensors daemon on sensorsPD on SLPI/ADSP
  3503. */
  3504. static int fastrpc_init_attach_process(struct fastrpc_file *fl,
  3505. struct fastrpc_ioctl_init *init)
  3506. {
  3507. int err = 0, tgid = fl->tgid_frpc;
  3508. remote_arg_t ra[1];
  3509. struct fastrpc_ioctl_invoke_async ioctl;
  3510. if (fl->dev_minor == MINOR_NUM_DEV) {
  3511. err = -ECONNREFUSED;
  3512. ADSPRPC_ERR(
  3513. "untrusted app trying to attach to privileged DSP PD\n");
  3514. return err;
  3515. }
  3516. /*
  3517. * Prepare remote arguments for creating thread group
  3518. * in guestOS/staticPD on the remote subsystem.
  3519. * Send unique fastrpc id to dsp
  3520. */
  3521. ra[0].buf.pv = (void *)&tgid;
  3522. ra[0].buf.len = sizeof(tgid);
  3523. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
  3524. ioctl.inv.sc = REMOTE_SCALARS_MAKE(0, 1, 0);
  3525. ioctl.inv.pra = ra;
  3526. ioctl.fds = NULL;
  3527. ioctl.attrs = NULL;
  3528. ioctl.crc = NULL;
  3529. ioctl.perf_kernel = NULL;
  3530. ioctl.perf_dsp = NULL;
  3531. ioctl.job = NULL;
  3532. if (init->flags == FASTRPC_INIT_ATTACH)
  3533. fl->pd = FASTRPC_ROOT_PD;
  3534. else if (init->flags == FASTRPC_INIT_ATTACH_SENSORS)
  3535. /* Setting to 2 will route the message to sensorsPD */
  3536. fl->pd = FASTRPC_SENSORS_PD;
  3537. err = fastrpc_internal_invoke(fl, FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl);
  3538. if (err)
  3539. goto bail;
  3540. bail:
  3541. return err;
  3542. }
  3543. /*
  3544. * This function makes a call to spawn a dynamic process
  3545. * on the remote subsystem.
  3546. * Example: all compute offloads to CDSP
  3547. */
  3548. static int fastrpc_init_create_dynamic_process(struct fastrpc_file *fl,
  3549. struct fastrpc_ioctl_init_attrs *uproc)
  3550. {
  3551. int err = 0, memlen = 0, mflags = 0, locked = 0;
  3552. struct fastrpc_ioctl_invoke_async ioctl;
  3553. struct fastrpc_ioctl_init *init = &uproc->init;
  3554. /* First page for init-mem and second page for proc-attrs */
  3555. struct smq_phy_page pages[PAGESLEN_WITH_SHAREDBUF];
  3556. struct fastrpc_mmap *file = NULL;
  3557. struct fastrpc_buf *imem = NULL;
  3558. unsigned long imem_dma_attr = 0;
  3559. remote_arg_t ra[6];
  3560. int fds[6];
  3561. unsigned int gid = 0, one_mb = 1024*1024;
  3562. unsigned int dsp_userpd_memlen = 0;
  3563. struct fastrpc_buf *init_mem;
  3564. struct fastrpc_mmap *sharedbuf_map = NULL;
  3565. struct {
  3566. int pgid;
  3567. unsigned int namelen;
  3568. unsigned int filelen;
  3569. unsigned int pageslen;
  3570. int attrs;
  3571. int siglen;
  3572. } inbuf;
  3573. spin_lock(&fl->hlock);
  3574. if (fl->dsp_process_state) {
  3575. err = -EALREADY;
  3576. ADSPRPC_ERR("Already in create dynamic process\n");
  3577. spin_unlock(&fl->hlock);
  3578. return err;
  3579. }
  3580. fl->dsp_process_state = PROCESS_CREATE_IS_INPROGRESS;
  3581. if (init->memlen) {
  3582. if(init->memlen > INIT_MEMLEN_MAX_DYNAMIC || init->memlen < INIT_MEMLEN_MIN_DYNAMIC) {
  3583. ADSPRPC_ERR(
  3584. "init memory for process %d should be between %d and %d\n",
  3585. init->memlen, INIT_MEMLEN_MIN_DYNAMIC, INIT_MEMLEN_MAX_DYNAMIC);
  3586. err = -EINVAL;
  3587. spin_unlock(&fl->hlock);
  3588. goto bail;
  3589. }
  3590. dsp_userpd_memlen = init->memlen;
  3591. } else {
  3592. dsp_userpd_memlen = 3*one_mb;
  3593. }
  3594. spin_unlock(&fl->hlock);
  3595. inbuf.pgid = fl->tgid_frpc;
  3596. inbuf.namelen = strlen(current->comm) + 1;
  3597. inbuf.filelen = init->filelen;
  3598. fl->pd = FASTRPC_USER_PD;
  3599. if (uproc->attrs & FASTRPC_MODE_UNSIGNED_MODULE)
  3600. fl->is_unsigned_pd = true;
  3601. /* Check if file memory passed by userspace is valid */
  3602. VERIFY(err, access_ok((void __user *)init->file, init->filelen));
  3603. if (err)
  3604. goto bail;
  3605. if (init->filelen) {
  3606. /* Map the shell file buffer to remote subsystem */
  3607. mutex_lock(&fl->map_mutex);
  3608. err = fastrpc_mmap_create(fl, init->filefd, NULL, 0,
  3609. init->file, init->filelen, mflags, &file);
  3610. if (file)
  3611. file->is_filemap = true;
  3612. mutex_unlock(&fl->map_mutex);
  3613. if (err)
  3614. goto bail;
  3615. }
  3616. inbuf.pageslen = 1;
  3617. /* Disregard any system unsigned PD attribute from userspace */
  3618. uproc->attrs &= (~FASTRPC_MODE_SYSTEM_UNSIGNED_PD);
  3619. /* Untrusted apps are not allowed to offload to signedPD on DSP. */
  3620. if (fl->untrusted_process) {
  3621. VERIFY(err, fl->is_unsigned_pd);
  3622. if (err) {
  3623. err = -ECONNREFUSED;
  3624. ADSPRPC_ERR(
  3625. "untrusted app trying to offload to signed remote process\n");
  3626. goto bail;
  3627. }
  3628. } else {
  3629. /* Trusted apps will be launched as system unsigned PDs */
  3630. if (fl->is_unsigned_pd)
  3631. uproc->attrs |= FASTRPC_MODE_SYSTEM_UNSIGNED_PD;
  3632. }
  3633. /* Disregard any privilege bits from userspace */
  3634. uproc->attrs &= (~FASTRPC_MODE_PRIVILEGED);
  3635. /*
  3636. * Check if the primary or supplementary group(s) of the process is
  3637. * one of the 'privileged' fastrpc GIDs stored in the device-tree.
  3638. */
  3639. gid = sorted_lists_intersection(fl->gidlist.gids,
  3640. fl->gidlist.gidcount, gfa.gidlist.gids, gfa.gidlist.gidcount);
  3641. if (gid) {
  3642. ADSPRPC_INFO("PID %d, GID %u is a privileged process\n",
  3643. fl->tgid, gid);
  3644. uproc->attrs |= FASTRPC_MODE_PRIVILEGED;
  3645. }
  3646. /*
  3647. * Userspace client should try to allocate the initial memory donated
  3648. * to remote subsystem as only the kernel and DSP should have access
  3649. * to that memory.
  3650. */
  3651. VERIFY(err, !init->mem);
  3652. if (err) {
  3653. err = -EINVAL;
  3654. ADSPRPC_ERR("donated memory allocated in userspace\n");
  3655. goto bail;
  3656. }
  3657. /* Free any previous donated memory */
  3658. spin_lock(&fl->hlock);
  3659. locked = 1;
  3660. if (fl->init_mem) {
  3661. init_mem = fl->init_mem;
  3662. fl->init_mem = NULL;
  3663. spin_unlock(&fl->hlock);
  3664. locked = 0;
  3665. fastrpc_buf_free(init_mem, 0);
  3666. }
  3667. if (locked) {
  3668. spin_unlock(&fl->hlock);
  3669. locked = 0;
  3670. }
  3671. /* Allocate DMA buffer in kernel for donating to remote process
  3672. * Unsigned PD requires additional memory because of the
  3673. * additional static heap initialized within the process.
  3674. */
  3675. if (fl->is_unsigned_pd)
  3676. dsp_userpd_memlen = 5*one_mb;
  3677. memlen = ALIGN(max(dsp_userpd_memlen, init->filelen * 4), one_mb);
  3678. imem_dma_attr = DMA_ATTR_DELAYED_UNMAP | DMA_ATTR_NO_KERNEL_MAPPING;
  3679. err = fastrpc_buf_alloc(fl, memlen, imem_dma_attr, 0,
  3680. INITMEM_BUF, &imem);
  3681. if (err)
  3682. goto bail;
  3683. fl->init_mem = imem;
  3684. inbuf.pageslen = 1;
  3685. if ((fl->sharedbuf_info.buf_fd != -1) && fl->sharedbuf_info.buf_size) {
  3686. mutex_lock(&fl->map_mutex);
  3687. err = fastrpc_mmap_create(fl, fl->sharedbuf_info.buf_fd, NULL, 0,
  3688. 0, fl->sharedbuf_info.buf_size, mflags, &sharedbuf_map);
  3689. mutex_unlock(&fl->map_mutex);
  3690. if (err)
  3691. goto bail;
  3692. /* if shared buff is available send this as the second page and set pageslen as 2 */
  3693. inbuf.pageslen = PAGESLEN_WITH_SHAREDBUF;
  3694. }
  3695. /*
  3696. * Prepare remote arguments for dynamic process create
  3697. * call to remote subsystem.
  3698. */
  3699. ra[0].buf.pv = (void *)&inbuf;
  3700. ra[0].buf.len = sizeof(inbuf);
  3701. fds[0] = -1;
  3702. ra[1].buf.pv = (void *)current->comm;
  3703. ra[1].buf.len = inbuf.namelen;
  3704. fds[1] = -1;
  3705. ra[2].buf.pv = (void *)init->file;
  3706. ra[2].buf.len = inbuf.filelen;
  3707. fds[2] = init->filefd;
  3708. pages[0].addr = imem->phys;
  3709. pages[0].size = imem->size;
  3710. /* Update IOVA of second page shared with DSP */
  3711. if (inbuf.pageslen > 1) {
  3712. pages[1].addr = sharedbuf_map->phys;
  3713. pages[1].size = sharedbuf_map->size;
  3714. }
  3715. ra[3].buf.pv = (void *)pages;
  3716. ra[3].buf.len = (inbuf.pageslen) * sizeof(*pages);
  3717. fds[3] = -1;
  3718. inbuf.attrs = uproc->attrs;
  3719. ra[4].buf.pv = (void *)&(inbuf.attrs);
  3720. ra[4].buf.len = sizeof(inbuf.attrs);
  3721. fds[4] = -1;
  3722. inbuf.siglen = uproc->siglen;
  3723. ra[5].buf.pv = (void *)&(inbuf.siglen);
  3724. ra[5].buf.len = sizeof(inbuf.siglen);
  3725. fds[5] = -1;
  3726. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
  3727. /*
  3728. * Choose appropriate remote method ID depending on whether the
  3729. * HLOS process has any attributes enabled (like unsignedPD,
  3730. * critical process, adaptive QoS, CRC checks etc).
  3731. */
  3732. ioctl.inv.sc = REMOTE_SCALARS_MAKE(6, 4, 0);
  3733. if (uproc->attrs)
  3734. ioctl.inv.sc = REMOTE_SCALARS_MAKE(7, 4, 0);
  3735. ioctl.inv.pra = ra;
  3736. ioctl.fds = fds;
  3737. ioctl.attrs = NULL;
  3738. ioctl.crc = NULL;
  3739. ioctl.perf_kernel = NULL;
  3740. ioctl.perf_dsp = NULL;
  3741. ioctl.job = NULL;
  3742. err = fastrpc_internal_invoke(fl, FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl);
  3743. if (err)
  3744. goto bail;
  3745. bail:
  3746. /*
  3747. * Shell is loaded into the donated memory on remote subsystem. So, the
  3748. * original file buffer can be DMA unmapped. In case of a failure also,
  3749. * the mapping needs to be removed.
  3750. */
  3751. if (file) {
  3752. mutex_lock(&fl->map_mutex);
  3753. fastrpc_mmap_free(file, 0);
  3754. mutex_unlock(&fl->map_mutex);
  3755. }
  3756. spin_lock(&fl->hlock);
  3757. locked = 1;
  3758. if (err) {
  3759. ADSPRPC_ERR("failed with err %d\n", err);
  3760. fl->dsp_process_state = PROCESS_CREATE_DEFAULT;
  3761. if (!IS_ERR_OR_NULL(fl->init_mem)) {
  3762. init_mem = fl->init_mem;
  3763. fl->init_mem = NULL;
  3764. spin_unlock(&fl->hlock);
  3765. locked = 0;
  3766. fastrpc_buf_free(init_mem, 0);
  3767. }
  3768. } else {
  3769. fl->dsp_process_state = PROCESS_CREATE_SUCCESS;
  3770. }
  3771. if (locked) {
  3772. spin_unlock(&fl->hlock);
  3773. locked = 0;
  3774. }
  3775. return err;
  3776. }
  3777. /*
  3778. * This function makes a call to create a thread group in the static
  3779. * process on the remote subsystem.
  3780. * Example: audio daemon 'adsprpcd' on audioPD on ADSP
  3781. */
  3782. static int fastrpc_init_create_static_process(struct fastrpc_file *fl,
  3783. struct fastrpc_ioctl_init *init)
  3784. {
  3785. int err = 0, rh_hyp_done = 0;
  3786. struct fastrpc_apps *me = &gfa;
  3787. struct fastrpc_ioctl_invoke_async ioctl;
  3788. struct smq_phy_page pages[1];
  3789. struct fastrpc_mmap *mem = NULL;
  3790. char *proc_name = NULL;
  3791. remote_arg_t ra[3];
  3792. uint64_t phys = 0;
  3793. size_t size = 0;
  3794. int fds[3];
  3795. struct secure_vm *rhvm = &me->channel[fl->cid].rhvm;
  3796. struct {
  3797. int pgid;
  3798. unsigned int namelen;
  3799. unsigned int pageslen;
  3800. } inbuf;
  3801. unsigned long irq_flags = 0;
  3802. if (fl->dev_minor == MINOR_NUM_DEV) {
  3803. err = -ECONNREFUSED;
  3804. ADSPRPC_ERR(
  3805. "untrusted app trying to attach to audio PD\n");
  3806. return err;
  3807. }
  3808. VERIFY(err, init->memlen <= INIT_MEMLEN_MAX_STATIC);
  3809. if (err) {
  3810. ADSPRPC_ERR(
  3811. "init memory for static process %d is more than max allowed init len %d\n",
  3812. init->memlen, INIT_MEMLEN_MAX_STATIC);
  3813. err = -EFBIG;
  3814. goto bail;
  3815. }
  3816. if (!init->filelen)
  3817. goto bail;
  3818. proc_name = kzalloc(init->filelen + 1, GFP_KERNEL);
  3819. VERIFY(err, !IS_ERR_OR_NULL(proc_name));
  3820. if (err) {
  3821. err = -ENOMEM;
  3822. goto bail;
  3823. }
  3824. err = copy_from_user((void *)proc_name,
  3825. (void __user *)init->file, init->filelen);
  3826. if (err) {
  3827. err = -EFAULT;
  3828. goto bail;
  3829. }
  3830. fl->pd = FASTRPC_USER_PD;
  3831. inbuf.pgid = fl->tgid_frpc;
  3832. inbuf.namelen = init->filelen;
  3833. inbuf.pageslen = 0;
  3834. if (!strcmp(proc_name, "audiopd")) {
  3835. /*
  3836. * Remove any previous mappings in case process is trying
  3837. * to reconnect after a PD restart on remote subsystem.
  3838. */
  3839. err = fastrpc_mmap_remove_pdr(fl);
  3840. if (err)
  3841. goto bail;
  3842. } else {
  3843. ADSPRPC_ERR(
  3844. "Create static process is failed for proc_name %s",
  3845. proc_name);
  3846. goto bail;
  3847. }
  3848. if ((!me->staticpd_flags && !me->legacy_remote_heap)) {
  3849. inbuf.pageslen = 1;
  3850. if (!fastrpc_get_persistent_map(init->memlen, &mem)) {
  3851. mutex_lock(&fl->map_mutex);
  3852. err = fastrpc_mmap_create(fl, -1, NULL, 0, init->mem,
  3853. init->memlen, ADSP_MMAP_REMOTE_HEAP_ADDR, &mem);
  3854. mutex_unlock(&fl->map_mutex);
  3855. if (err || (!mem))
  3856. goto bail;
  3857. spin_lock_irqsave(&me->hlock, irq_flags);
  3858. mem->in_use = true;
  3859. spin_unlock_irqrestore(&me->hlock, irq_flags);
  3860. }
  3861. VERIFY(err, mem);
  3862. if (err)
  3863. goto bail;
  3864. phys = mem->phys;
  3865. size = mem->size;
  3866. /*
  3867. * If remote-heap VMIDs are defined in DTSI, then do
  3868. * hyp_assign from HLOS to those VMs (LPASS, ADSP).
  3869. */
  3870. if (rhvm->vmid && mem->refs == 1 && size) {
  3871. u64 src_perms = BIT(QCOM_SCM_VMID_HLOS);
  3872. struct qcom_scm_vmperm *dst_perms;
  3873. uint32_t i = 0;
  3874. VERIFY(err, NULL != (dst_perms = kcalloc(rhvm->vmcount,
  3875. sizeof(struct qcom_scm_vmperm), GFP_KERNEL)));
  3876. if (err)
  3877. goto bail;
  3878. for (i = 0; i < rhvm->vmcount; i++) {
  3879. dst_perms[i].vmid = rhvm->vmid[i];
  3880. dst_perms[i].perm = rhvm->vmperm[i];
  3881. }
  3882. err = qcom_scm_assign_mem(phys, (uint64_t)size,
  3883. &src_perms, dst_perms, rhvm->vmcount);
  3884. kfree(dst_perms);
  3885. if (err) {
  3886. ADSPRPC_ERR(
  3887. "rh hyp assign failed with %d for phys 0x%llx, size %zu\n",
  3888. err, phys, size);
  3889. err = -EADDRNOTAVAIL;
  3890. goto bail;
  3891. }
  3892. rh_hyp_done = 1;
  3893. }
  3894. me->staticpd_flags = 1;
  3895. mem->is_persistent = true;
  3896. }
  3897. /*
  3898. * Prepare remote arguments for static process create
  3899. * call to remote subsystem.
  3900. */
  3901. ra[0].buf.pv = (void *)&inbuf;
  3902. ra[0].buf.len = sizeof(inbuf);
  3903. fds[0] = -1;
  3904. ra[1].buf.pv = (void *)proc_name;
  3905. ra[1].buf.len = inbuf.namelen;
  3906. fds[1] = -1;
  3907. pages[0].addr = phys;
  3908. pages[0].size = size;
  3909. ra[2].buf.pv = (void *)pages;
  3910. ra[2].buf.len = sizeof(*pages);
  3911. fds[2] = -1;
  3912. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
  3913. ioctl.inv.sc = REMOTE_SCALARS_MAKE(8, 3, 0);
  3914. ioctl.inv.pra = ra;
  3915. ioctl.fds = NULL;
  3916. ioctl.attrs = NULL;
  3917. ioctl.crc = NULL;
  3918. ioctl.perf_kernel = NULL;
  3919. ioctl.perf_dsp = NULL;
  3920. ioctl.job = NULL;
  3921. err = fastrpc_internal_invoke(fl, FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl);
  3922. if (err)
  3923. goto bail;
  3924. bail:
  3925. kfree(proc_name);
  3926. if (err) {
  3927. me->staticpd_flags = 0;
  3928. if (rh_hyp_done) {
  3929. int hyp_err = 0;
  3930. u64 src_perms = 0;
  3931. struct qcom_scm_vmperm dst_perms;
  3932. uint32_t i = 0;
  3933. for (i = 0; i < rhvm->vmcount; i++) {
  3934. src_perms |= BIT(rhvm->vmid[i]);
  3935. }
  3936. dst_perms.vmid = QCOM_SCM_VMID_HLOS;
  3937. dst_perms.perm = QCOM_SCM_PERM_RWX;
  3938. /* Assign memory back to HLOS in case of errors */
  3939. hyp_err = qcom_scm_assign_mem(phys, (uint64_t)size,
  3940. &src_perms, &dst_perms, 1);
  3941. if (hyp_err)
  3942. ADSPRPC_WARN(
  3943. "rh hyp unassign failed with %d for phys 0x%llx of size %zu\n",
  3944. hyp_err, phys, size);
  3945. }
  3946. mutex_lock(&fl->map_mutex);
  3947. fastrpc_mmap_free(mem, 0);
  3948. mutex_unlock(&fl->map_mutex);
  3949. }
  3950. return err;
  3951. }
  3952. /*
  3953. * This function sets fastrpc service location name
  3954. * based on ioctl init flags.
  3955. */
  3956. static void fastrpc_set_servloc(struct fastrpc_file *fl,
  3957. struct fastrpc_ioctl_init *init)
  3958. {
  3959. char *proc_name = NULL;
  3960. int err = 0;
  3961. if (init->flags == FASTRPC_INIT_ATTACH_SENSORS) {
  3962. if (fl->cid == ADSP_DOMAIN_ID)
  3963. fl->servloc_name =
  3964. SENSORS_PDR_ADSP_SERVICE_LOCATION_CLIENT_NAME;
  3965. else if (fl->cid == SDSP_DOMAIN_ID)
  3966. fl->servloc_name =
  3967. SENSORS_PDR_SLPI_SERVICE_LOCATION_CLIENT_NAME;
  3968. } else if (init->flags == FASTRPC_INIT_CREATE_STATIC) {
  3969. if (!init->filelen)
  3970. goto bail;
  3971. proc_name = kzalloc(init->filelen + 1, GFP_KERNEL);
  3972. VERIFY(err, !IS_ERR_OR_NULL(proc_name));
  3973. if (err) {
  3974. err = -ENOMEM;
  3975. goto bail;
  3976. }
  3977. err = copy_from_user((void *)proc_name,
  3978. (void __user *)init->file, init->filelen);
  3979. if (err) {
  3980. err = -EFAULT;
  3981. goto bail;
  3982. }
  3983. if (!strcmp(proc_name, "audiopd"))
  3984. fl->servloc_name = AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME;
  3985. }
  3986. bail:
  3987. kfree(proc_name);
  3988. }
  3989. int fastrpc_init_process(struct fastrpc_file *fl,
  3990. struct fastrpc_ioctl_init_attrs *uproc)
  3991. {
  3992. int err = 0;
  3993. struct fastrpc_ioctl_init *init = &uproc->init;
  3994. int cid = fl->cid;
  3995. struct fastrpc_apps *me = &gfa;
  3996. struct fastrpc_channel_ctx *chan = NULL;
  3997. VERIFY(err, init->filelen < INIT_FILELEN_MAX
  3998. && init->memlen <= INIT_MEMLEN_MAX_DYNAMIC);
  3999. if (err) {
  4000. ADSPRPC_ERR(
  4001. "file size 0x%x or init memory 0x%x is more than max allowed file size 0x%x or init len 0x%x\n",
  4002. init->filelen, init->memlen,
  4003. INIT_FILELEN_MAX, INIT_MEMLEN_MAX_DYNAMIC);
  4004. err = -EFBIG;
  4005. goto bail;
  4006. }
  4007. VERIFY(err, VALID_FASTRPC_CID(cid));
  4008. if (err) {
  4009. err = -ECHRNG;
  4010. goto bail;
  4011. }
  4012. chan = &me->channel[cid];
  4013. if (chan->unsigned_support && fl->dev_minor == MINOR_NUM_DEV) {
  4014. /* Make sure third party applications */
  4015. /* can spawn only unsigned PD when */
  4016. /* channel configured as secure. */
  4017. if (chan->secure && !(fl->is_unsigned_pd)) {
  4018. err = -ECONNREFUSED;
  4019. goto bail;
  4020. }
  4021. }
  4022. if (fl->sharedcb == 1) {
  4023. // Only attach sensors pd use cases can share CB
  4024. VERIFY(err, init->flags == FASTRPC_INIT_ATTACH_SENSORS);
  4025. if (err) {
  4026. err = -EACCES;
  4027. goto bail;
  4028. }
  4029. }
  4030. fastrpc_set_servloc(fl, init);
  4031. err = fastrpc_set_tvm_remote_domain(fl, init);
  4032. if (err)
  4033. goto bail;
  4034. err = fastrpc_channel_open(fl, init->flags);
  4035. if (err)
  4036. goto bail;
  4037. fl->proc_flags = init->flags;
  4038. switch (init->flags) {
  4039. case FASTRPC_INIT_ATTACH:
  4040. case FASTRPC_INIT_ATTACH_SENSORS:
  4041. err = fastrpc_init_attach_process(fl, init);
  4042. break;
  4043. case FASTRPC_INIT_CREATE:
  4044. err = fastrpc_init_create_dynamic_process(fl, uproc);
  4045. break;
  4046. case FASTRPC_INIT_CREATE_STATIC:
  4047. err = fastrpc_init_create_static_process(fl, init);
  4048. break;
  4049. default:
  4050. err = -ENOTTY;
  4051. break;
  4052. }
  4053. if (err)
  4054. goto bail;
  4055. fl->dsp_proc_init = 1;
  4056. VERIFY(err, 0 == (err = fastrpc_device_create(fl)));
  4057. if (err)
  4058. goto bail;
  4059. bail:
  4060. return err;
  4061. }
  4062. static int fastrpc_send_cpuinfo_to_dsp(struct fastrpc_file *fl)
  4063. {
  4064. int err = 0;
  4065. uint64_t cpuinfo = 0;
  4066. struct fastrpc_apps *me = &gfa;
  4067. struct fastrpc_ioctl_invoke_async ioctl;
  4068. remote_arg_t ra[1];
  4069. int cid = -1;
  4070. if (!fl) {
  4071. err = -EBADF;
  4072. goto bail;
  4073. }
  4074. cid = fl->cid;
  4075. VERIFY(err, VALID_FASTRPC_CID(cid));
  4076. if (err) {
  4077. err = -ECHRNG;
  4078. ADSPRPC_ERR(
  4079. "invalid channel 0x%zx set for session\n",
  4080. cid);
  4081. goto bail;
  4082. }
  4083. cpuinfo = me->channel[cid].cpuinfo_todsp;
  4084. /* return success if already updated to remote processor */
  4085. if (me->channel[cid].cpuinfo_status)
  4086. return 0;
  4087. ra[0].buf.pv = (void *)&cpuinfo;
  4088. ra[0].buf.len = sizeof(cpuinfo);
  4089. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_DSP_UTILITIES;
  4090. ioctl.inv.sc = REMOTE_SCALARS_MAKE(1, 1, 0);
  4091. ioctl.inv.pra = ra;
  4092. ioctl.fds = NULL;
  4093. ioctl.attrs = NULL;
  4094. ioctl.crc = NULL;
  4095. ioctl.perf_kernel = NULL;
  4096. ioctl.perf_dsp = NULL;
  4097. ioctl.job = NULL;
  4098. err = fastrpc_internal_invoke(fl, FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl);
  4099. if (!err)
  4100. me->channel[cid].cpuinfo_status = true;
  4101. bail:
  4102. return err;
  4103. }
  4104. int fastrpc_get_info_from_dsp(struct fastrpc_file *fl,
  4105. uint32_t *dsp_attr_buf,
  4106. uint32_t dsp_attr_buf_len,
  4107. uint32_t domain)
  4108. {
  4109. int err = 0;
  4110. struct fastrpc_ioctl_invoke_async ioctl;
  4111. remote_arg_t ra[2];
  4112. dsp_attr_buf[0] = 0; // Capability filled in userspace
  4113. // Fastrpc to modem not supported
  4114. if (domain == MDSP_DOMAIN_ID)
  4115. goto bail;
  4116. err = fastrpc_channel_open(fl, FASTRPC_INIT_NO_CREATE);
  4117. if (err)
  4118. goto bail;
  4119. ra[0].buf.pv = (void *)&dsp_attr_buf_len;
  4120. ra[0].buf.len = sizeof(dsp_attr_buf_len);
  4121. ra[1].buf.pv = (void *)(&dsp_attr_buf[1]);
  4122. ra[1].buf.len = dsp_attr_buf_len * sizeof(uint32_t);
  4123. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_DSP_UTILITIES;
  4124. ioctl.inv.sc = REMOTE_SCALARS_MAKE(0, 1, 1);
  4125. ioctl.inv.pra = ra;
  4126. ioctl.fds = NULL;
  4127. ioctl.attrs = NULL;
  4128. ioctl.crc = NULL;
  4129. ioctl.perf_kernel = NULL;
  4130. ioctl.perf_dsp = NULL;
  4131. ioctl.job = NULL;
  4132. err = fastrpc_internal_invoke(fl, FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl);
  4133. bail:
  4134. if (err)
  4135. ADSPRPC_ERR("could not obtain dsp information, err val %d\n",
  4136. err);
  4137. return err;
  4138. }
  4139. int fastrpc_get_info_from_kernel(
  4140. struct fastrpc_ioctl_capability *cap,
  4141. struct fastrpc_file *fl)
  4142. {
  4143. int err = 0;
  4144. uint32_t domain = cap->domain, attribute_ID = cap->attribute_ID;
  4145. uint32_t async_capability = 0;
  4146. struct fastrpc_dsp_capabilities *dsp_cap_ptr = NULL;
  4147. VERIFY(err, domain < NUM_CHANNELS);
  4148. if (err) {
  4149. err = -ECHRNG;
  4150. goto bail;
  4151. }
  4152. /*
  4153. * Check if number of attribute IDs obtained from userspace
  4154. * is less than the number of attribute IDs supported by
  4155. * kernel
  4156. */
  4157. if (attribute_ID >= FASTRPC_MAX_ATTRIBUTES) {
  4158. err = -EOVERFLOW;
  4159. goto bail;
  4160. }
  4161. dsp_cap_ptr = &gcinfo[domain].dsp_cap_kernel;
  4162. if (attribute_ID >= FASTRPC_MAX_DSP_ATTRIBUTES) {
  4163. // Driver capability, pass it to user
  4164. memcpy(&cap->capability,
  4165. &kernel_capabilities[attribute_ID -
  4166. FASTRPC_MAX_DSP_ATTRIBUTES],
  4167. sizeof(cap->capability));
  4168. } else if (!dsp_cap_ptr->is_cached) {
  4169. /*
  4170. * Information not on kernel, query device for information
  4171. * and cache on kernel
  4172. */
  4173. err = fastrpc_get_info_from_dsp(fl,
  4174. dsp_cap_ptr->dsp_attributes,
  4175. FASTRPC_MAX_DSP_ATTRIBUTES - 1,
  4176. domain);
  4177. if (err)
  4178. goto bail;
  4179. /* Async capability support depends on both kernel and DSP */
  4180. async_capability = IS_ASYNC_FASTRPC_AVAILABLE &&
  4181. dsp_cap_ptr->dsp_attributes[ASYNC_FASTRPC_CAP];
  4182. dsp_cap_ptr->dsp_attributes[ASYNC_FASTRPC_CAP]
  4183. = async_capability;
  4184. memcpy(&cap->capability,
  4185. &dsp_cap_ptr->dsp_attributes[attribute_ID],
  4186. sizeof(cap->capability));
  4187. dsp_cap_ptr->is_cached = 1;
  4188. } else {
  4189. // Information on Kernel, pass it to user
  4190. memcpy(&cap->capability,
  4191. &dsp_cap_ptr->dsp_attributes[attribute_ID],
  4192. sizeof(cap->capability));
  4193. }
  4194. bail:
  4195. return err;
  4196. }
  4197. static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl)
  4198. {
  4199. int err = 0;
  4200. struct fastrpc_ioctl_invoke_async ioctl;
  4201. remote_arg_t ra[1];
  4202. int tgid = 0;
  4203. int cid = -1;
  4204. unsigned long irq_flags = 0;
  4205. if (!fl) {
  4206. err = -EBADF;
  4207. goto bail;
  4208. }
  4209. cid = fl->cid;
  4210. VERIFY(err, VALID_FASTRPC_CID(cid));
  4211. if (err) {
  4212. err = -ECHRNG;
  4213. goto bail;
  4214. }
  4215. VERIFY(err, fl->sctx != NULL);
  4216. if (err) {
  4217. err = -EBADR;
  4218. goto bail;
  4219. }
  4220. err = verify_transport_device(cid, fl->tvm_remote_domain);
  4221. if (err)
  4222. goto bail;
  4223. VERIFY(err, fl->apps->channel[cid].subsystemstate != SUBSYSTEM_RESTARTING);
  4224. if (err) {
  4225. wait_for_completion(&fl->shutdown);
  4226. err = -ECONNRESET;
  4227. goto bail;
  4228. }
  4229. /* Send unique fastrpc process ID to dsp */
  4230. tgid = fl->tgid_frpc;
  4231. ra[0].buf.pv = (void *)&tgid;
  4232. ra[0].buf.len = sizeof(tgid);
  4233. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
  4234. ioctl.inv.sc = REMOTE_SCALARS_MAKE(1, 1, 0);
  4235. ioctl.inv.pra = ra;
  4236. ioctl.fds = NULL;
  4237. ioctl.attrs = NULL;
  4238. ioctl.crc = NULL;
  4239. ioctl.perf_kernel = NULL;
  4240. ioctl.perf_dsp = NULL;
  4241. ioctl.job = NULL;
  4242. spin_lock_irqsave(&fl->apps->hlock, irq_flags);
  4243. fl->file_close = FASTRPC_PROCESS_DSP_EXIT_INIT;
  4244. spin_unlock_irqrestore(&fl->apps->hlock, irq_flags);
  4245. /*
  4246. * Pass 2 for "kernel" arg to send kernel msg to DSP
  4247. * with non-zero msg PID for the DSP to directly use
  4248. * that info to kill the remote process.
  4249. */
  4250. VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
  4251. FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_NONZERO_PID, &ioctl)));
  4252. spin_lock_irqsave(&fl->apps->hlock, irq_flags);
  4253. fl->file_close = FASTRPC_PROCESS_DSP_EXIT_COMPLETE;
  4254. spin_unlock_irqrestore(&fl->apps->hlock, irq_flags);
  4255. if (err && fl->dsp_proc_init)
  4256. ADSPRPC_ERR(
  4257. "releasing DSP process failed with %d (0x%x) for %s\n",
  4258. err, err, current->comm);
  4259. bail:
  4260. if (err && fl && fl->apps) {
  4261. spin_lock_irqsave(&fl->apps->hlock, irq_flags);
  4262. fl->file_close = FASTRPC_PROCESS_DSP_EXIT_ERROR;
  4263. spin_unlock_irqrestore(&fl->apps->hlock, irq_flags);
  4264. }
  4265. return err;
  4266. }
  4267. static int fastrpc_mem_map_to_dsp(struct fastrpc_file *fl, int fd, int offset,
  4268. uint32_t flags, uintptr_t va, uint64_t phys,
  4269. size_t size, uintptr_t *raddr)
  4270. {
  4271. struct fastrpc_ioctl_invoke_async ioctl;
  4272. struct smq_phy_page page;
  4273. remote_arg_t ra[4];
  4274. int err = 0;
  4275. struct {
  4276. int pid;
  4277. int fd;
  4278. int offset;
  4279. uint32_t flags;
  4280. uint64_t vaddrin;
  4281. int num;
  4282. int data_len;
  4283. } inargs;
  4284. struct {
  4285. uint64_t vaddrout;
  4286. } routargs;
  4287. /* Send unique fastrpc process ID to dsp */
  4288. inargs.pid = fl->tgid_frpc;
  4289. inargs.fd = fd;
  4290. inargs.offset = offset;
  4291. inargs.vaddrin = (uintptr_t)va;
  4292. inargs.flags = flags;
  4293. inargs.num = sizeof(page);
  4294. inargs.data_len = 0;
  4295. ra[0].buf.pv = (void *)&inargs;
  4296. ra[0].buf.len = sizeof(inargs);
  4297. page.addr = phys;
  4298. page.size = size;
  4299. ra[1].buf.pv = (void *)&page;
  4300. ra[1].buf.len = sizeof(page);
  4301. ra[2].buf.pv = (void *)&page;
  4302. ra[2].buf.len = 0;
  4303. ra[3].buf.pv = (void *)&routargs;
  4304. ra[3].buf.len = sizeof(routargs);
  4305. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
  4306. ioctl.inv.sc = REMOTE_SCALARS_MAKE(10, 3, 1);
  4307. ioctl.inv.pra = ra;
  4308. ioctl.fds = NULL;
  4309. ioctl.attrs = NULL;
  4310. ioctl.crc = NULL;
  4311. ioctl.perf_kernel = NULL;
  4312. ioctl.perf_dsp = NULL;
  4313. ioctl.job = NULL;
  4314. VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
  4315. FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl)));
  4316. if (err)
  4317. goto bail;
  4318. if (raddr)
  4319. *raddr = (uintptr_t)routargs.vaddrout;
  4320. bail:
  4321. return err;
  4322. }
  4323. static int fastrpc_mem_unmap_to_dsp(struct fastrpc_file *fl, int fd,
  4324. uint32_t flags, uintptr_t va,
  4325. uint64_t phys, size_t size)
  4326. {
  4327. struct fastrpc_ioctl_invoke_async ioctl;
  4328. remote_arg_t ra[1];
  4329. int err = 0;
  4330. struct {
  4331. int pid;
  4332. int fd;
  4333. uint64_t vaddrin;
  4334. uint64_t len;
  4335. } inargs;
  4336. /* Send unique fastrpc process ID to dsp */
  4337. inargs.pid = fl->tgid_frpc;
  4338. inargs.fd = fd;
  4339. inargs.vaddrin = (uint64_t)va;
  4340. inargs.len = (uint64_t)size;
  4341. ra[0].buf.pv = (void *)&inargs;
  4342. ra[0].buf.len = sizeof(inargs);
  4343. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
  4344. ioctl.inv.sc = REMOTE_SCALARS_MAKE(11, 1, 0);
  4345. ioctl.inv.pra = ra;
  4346. ioctl.fds = NULL;
  4347. ioctl.attrs = NULL;
  4348. ioctl.crc = NULL;
  4349. ioctl.perf_kernel = NULL;
  4350. ioctl.perf_dsp = NULL;
  4351. ioctl.job = NULL;
  4352. VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
  4353. FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl)));
  4354. if (err)
  4355. goto bail;
  4356. bail:
  4357. return err;
  4358. }
  4359. static int fastrpc_unmap_on_dsp(struct fastrpc_file *fl,
  4360. uintptr_t raddr, uint64_t phys, size_t size, uint32_t flags)
  4361. {
  4362. struct fastrpc_ioctl_invoke_async ioctl;
  4363. remote_arg_t ra[1] = {};
  4364. int err = 0;
  4365. struct {
  4366. int pid;
  4367. uintptr_t vaddrout;
  4368. size_t size;
  4369. } inargs;
  4370. /* Send unique fastrpc process ID to dsp */
  4371. inargs.pid = fl->tgid_frpc;
  4372. inargs.size = size;
  4373. inargs.vaddrout = raddr;
  4374. ra[0].buf.pv = (void *)&inargs;
  4375. ra[0].buf.len = sizeof(inargs);
  4376. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
  4377. if (fl->apps->compat)
  4378. ioctl.inv.sc = REMOTE_SCALARS_MAKE(5, 1, 0);
  4379. else
  4380. ioctl.inv.sc = REMOTE_SCALARS_MAKE(3, 1, 0);
  4381. ioctl.inv.pra = ra;
  4382. ioctl.fds = NULL;
  4383. ioctl.attrs = NULL;
  4384. ioctl.crc = NULL;
  4385. ioctl.perf_kernel = NULL;
  4386. ioctl.perf_dsp = NULL;
  4387. ioctl.job = NULL;
  4388. VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
  4389. FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl)));
  4390. if (err)
  4391. goto bail;
  4392. bail:
  4393. return err;
  4394. }
  4395. static int fastrpc_mmap_on_dsp(struct fastrpc_file *fl, uint32_t flags,
  4396. uintptr_t va, uint64_t phys,
  4397. size_t size, int refs, uintptr_t *raddr)
  4398. {
  4399. struct fastrpc_ioctl_invoke_async ioctl;
  4400. struct fastrpc_apps *me = &gfa;
  4401. struct smq_phy_page page;
  4402. int num = 1;
  4403. remote_arg_t ra[3];
  4404. int err = 0;
  4405. struct {
  4406. int pid;
  4407. uint32_t flags;
  4408. uintptr_t vaddrin;
  4409. int num;
  4410. } inargs;
  4411. struct {
  4412. uintptr_t vaddrout;
  4413. } routargs;
  4414. int cid = -1;
  4415. if (!fl) {
  4416. err = -EBADF;
  4417. goto bail;
  4418. }
  4419. cid = fl->cid;
  4420. /* Send unique fastrpc process ID to dsp */
  4421. inargs.pid = fl->tgid_frpc;
  4422. inargs.vaddrin = (uintptr_t)va;
  4423. inargs.flags = flags;
  4424. inargs.num = fl->apps->compat ? num * sizeof(page) : num;
  4425. ra[0].buf.pv = (void *)&inargs;
  4426. ra[0].buf.len = sizeof(inargs);
  4427. page.addr = phys;
  4428. page.size = size;
  4429. ra[1].buf.pv = (void *)&page;
  4430. ra[1].buf.len = num * sizeof(page);
  4431. ra[2].buf.pv = (void *)&routargs;
  4432. ra[2].buf.len = sizeof(routargs);
  4433. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
  4434. if (fl->apps->compat)
  4435. ioctl.inv.sc = REMOTE_SCALARS_MAKE(4, 2, 1);
  4436. else
  4437. ioctl.inv.sc = REMOTE_SCALARS_MAKE(2, 2, 1);
  4438. ioctl.inv.pra = ra;
  4439. ioctl.fds = NULL;
  4440. ioctl.attrs = NULL;
  4441. ioctl.crc = NULL;
  4442. ioctl.perf_kernel = NULL;
  4443. ioctl.perf_dsp = NULL;
  4444. ioctl.job = NULL;
  4445. VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
  4446. FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl)));
  4447. *raddr = (uintptr_t)routargs.vaddrout;
  4448. if (err)
  4449. goto bail;
  4450. if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
  4451. VERIFY(err, VALID_FASTRPC_CID(cid));
  4452. if (err) {
  4453. err = -ECHRNG;
  4454. ADSPRPC_ERR(
  4455. "invalid channel 0x%zx set for session\n",
  4456. cid);
  4457. goto bail;
  4458. }
  4459. }
  4460. if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR
  4461. && me->channel[cid].rhvm.vmid && refs == 1) {
  4462. struct secure_vm *rhvm = &me->channel[cid].rhvm;
  4463. u64 src_perms = BIT(QCOM_SCM_VMID_HLOS);
  4464. struct qcom_scm_vmperm *dst_perms;
  4465. uint32_t i = 0;
  4466. VERIFY(err, NULL != (dst_perms = kcalloc(rhvm->vmcount,
  4467. sizeof(struct qcom_scm_vmperm), GFP_KERNEL)));
  4468. if (err)
  4469. goto bail;
  4470. for (i = 0; i < rhvm->vmcount; i++) {
  4471. dst_perms[i].vmid = rhvm->vmid[i];
  4472. dst_perms[i].perm = rhvm->vmperm[i];
  4473. }
  4474. err = qcom_scm_assign_mem(phys, (uint64_t)size,
  4475. &src_perms, dst_perms, rhvm->vmcount);
  4476. kfree(dst_perms);
  4477. if (err) {
  4478. ADSPRPC_ERR(
  4479. "rh hyp assign failed with %d for phys 0x%llx, size %zu\n",
  4480. err, phys, size);
  4481. err = -EADDRNOTAVAIL;
  4482. err = fastrpc_unmap_on_dsp(fl,
  4483. *raddr, phys, size, flags);
  4484. if (err) {
  4485. ADSPRPC_ERR(
  4486. "failed to unmap %d for phys 0x%llx, size %zd\n",
  4487. err, phys, size);
  4488. }
  4489. goto bail;
  4490. }
  4491. }
  4492. bail:
  4493. return err;
  4494. }
  4495. static int fastrpc_munmap_on_dsp_rh(struct fastrpc_file *fl, uint64_t phys,
  4496. size_t size, uint32_t flags, int locked)
  4497. {
  4498. int err = 0;
  4499. int tgid = 0;
  4500. struct fastrpc_apps *me = &gfa;
  4501. int cid = -1;
  4502. struct fastrpc_ioctl_invoke_async ioctl;
  4503. remote_arg_t ra[2];
  4504. struct {
  4505. uint8_t skey;
  4506. } routargs;
  4507. if (!fl) {
  4508. err = -EBADF;
  4509. goto bail;
  4510. }
  4511. cid = fl->cid;
  4512. VERIFY(err, VALID_FASTRPC_CID(cid));
  4513. if (err) {
  4514. err = -ECHRNG;
  4515. ADSPRPC_ERR(
  4516. "invalid channel 0x%zx set for session\n",
  4517. cid);
  4518. goto bail;
  4519. }
  4520. /* Send unique fastrpc process ID to dsp */
  4521. tgid = fl->tgid_frpc;
  4522. ra[0].buf.pv = (void *)&tgid;
  4523. ra[0].buf.len = sizeof(tgid);
  4524. ra[1].buf.pv = (void *)&routargs;
  4525. ra[1].buf.len = sizeof(routargs);
  4526. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
  4527. ioctl.inv.sc = REMOTE_SCALARS_MAKE(9, 1, 1);
  4528. ioctl.inv.pra = ra;
  4529. ioctl.fds = NULL;
  4530. ioctl.attrs = NULL;
  4531. ioctl.crc = NULL;
  4532. ioctl.perf_kernel = NULL;
  4533. ioctl.perf_dsp = NULL;
  4534. ioctl.job = NULL;
  4535. if (locked) {
  4536. mutex_unlock(&fl->map_mutex);
  4537. mutex_unlock(&me->channel[cid].smd_mutex);
  4538. }
  4539. VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
  4540. FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl)));
  4541. if (locked) {
  4542. mutex_lock(&me->channel[cid].smd_mutex);
  4543. mutex_lock(&fl->map_mutex);
  4544. }
  4545. if (err)
  4546. goto bail;
  4547. bail:
  4548. return err;
  4549. }
  4550. static int fastrpc_munmap_rh(uint64_t phys, size_t size,
  4551. uint32_t flags)
  4552. {
  4553. int err = 0;
  4554. struct fastrpc_apps *me = &gfa;
  4555. struct secure_vm *rhvm = &me->channel[RH_CID].rhvm;
  4556. if ((rhvm->vmid)
  4557. && (me->channel[RH_CID].in_hib == 0)) {
  4558. u64 src_perms = 0;
  4559. struct qcom_scm_vmperm dst_perms = {0};
  4560. uint32_t i = 0;
  4561. for (i = 0; i < rhvm->vmcount; i++) {
  4562. src_perms |= BIT(rhvm->vmid[i]);
  4563. }
  4564. dst_perms.vmid = QCOM_SCM_VMID_HLOS;
  4565. dst_perms.perm = QCOM_SCM_PERM_RWX;
  4566. err = qcom_scm_assign_mem(phys,
  4567. (uint64_t)size, &src_perms, &dst_perms, 1);
  4568. if (err) {
  4569. ADSPRPC_ERR(
  4570. "rh hyp unassign failed with %d for phys 0x%llx, size %zu\n",
  4571. err, phys, size);
  4572. err = -EADDRNOTAVAIL;
  4573. return err;
  4574. }
  4575. }
  4576. return err;
  4577. }
  4578. static int fastrpc_munmap_on_dsp(struct fastrpc_file *fl, uintptr_t raddr,
  4579. uint64_t phys, size_t size, uint32_t flags)
  4580. {
  4581. int err = 0;
  4582. VERIFY(err, 0 == (err = fastrpc_unmap_on_dsp(fl, raddr, phys,
  4583. size, flags)));
  4584. if (err)
  4585. goto bail;
  4586. if (flags == ADSP_MMAP_HEAP_ADDR) {
  4587. VERIFY(err, !(err = fastrpc_munmap_on_dsp_rh(fl, phys,
  4588. size, flags, 0)));
  4589. if (err)
  4590. goto bail;
  4591. } else if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
  4592. VERIFY(err, !(err = fastrpc_munmap_rh(phys,
  4593. size, flags)));
  4594. if (err)
  4595. goto bail;
  4596. }
  4597. bail:
  4598. return err;
  4599. }
  4600. static int fastrpc_mmap_remove_ssr(struct fastrpc_file *fl, int locked)
  4601. {
  4602. struct fastrpc_mmap *match = NULL, *map = NULL;
  4603. struct hlist_node *n = NULL;
  4604. int err = 0, ret = 0, lock = 0;
  4605. struct fastrpc_apps *me = &gfa;
  4606. struct qcom_dump_segment ramdump_segments_rh;
  4607. struct list_head head;
  4608. unsigned long irq_flags = 0;
  4609. INIT_LIST_HEAD(&head);
  4610. if (fl) {
  4611. VERIFY(err, fl->cid == RH_CID);
  4612. if (err) {
  4613. err = -EBADR;
  4614. goto bail;
  4615. }
  4616. }
  4617. spin_lock_irqsave(&me->hlock, irq_flags);
  4618. lock = 1;
  4619. hlist_for_each_entry_safe(map, n, &me->maps, hn) {
  4620. if (!lock) {
  4621. spin_lock_irqsave(&me->hlock, irq_flags);
  4622. lock = 1;
  4623. }
  4624. /* In hibernation suspend case fl is NULL, check !fl to cleanup */
  4625. if (!fl || (fl && map->servloc_name && fl->servloc_name
  4626. && !strcmp(map->servloc_name, fl->servloc_name))) {
  4627. match = map;
  4628. if (map->is_persistent && map->in_use) {
  4629. struct secure_vm *rhvm = &me->channel[RH_CID].rhvm;
  4630. uint64_t phys = map->phys;
  4631. size_t size = map->size;
  4632. if (lock) {
  4633. spin_unlock_irqrestore(&me->hlock, irq_flags);
  4634. lock = 0;
  4635. }
  4636. //scm assign it back to HLOS
  4637. if (rhvm->vmid) {
  4638. u64 src_perms = 0;
  4639. struct qcom_scm_vmperm dst_perms = {0};
  4640. uint32_t i = 0;
  4641. for (i = 0; i < rhvm->vmcount; i++) {
  4642. src_perms |= BIT(rhvm->vmid[i]);
  4643. }
  4644. dst_perms.vmid = QCOM_SCM_VMID_HLOS;
  4645. dst_perms.perm = QCOM_SCM_PERM_RWX;
  4646. err = qcom_scm_assign_mem(phys, (uint64_t)size,
  4647. &src_perms, &dst_perms, 1);
  4648. }
  4649. if (err) {
  4650. ADSPRPC_ERR(
  4651. "rh hyp unassign failed with %d for phys 0x%llx, size %zu\n",
  4652. err, phys, size);
  4653. err = -EADDRNOTAVAIL;
  4654. goto bail;
  4655. }
  4656. if (!lock) {
  4657. spin_lock_irqsave(&me->hlock, irq_flags);
  4658. lock = 1;
  4659. }
  4660. map->in_use = false;
  4661. /*
  4662. * decrementing refcount for persistent mappings
  4663. * as incrementing it in fastrpc_get_persistent_map
  4664. */
  4665. map->refs--;
  4666. }
  4667. if (!match->is_persistent)
  4668. hlist_del_init(&map->hn);
  4669. }
  4670. if (lock) {
  4671. spin_unlock_irqrestore(&me->hlock, irq_flags);
  4672. lock = 0;
  4673. }
  4674. if (match) {
  4675. if (!match->is_persistent) {
  4676. if (match->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
  4677. err = fastrpc_munmap_rh(match->phys,
  4678. match->size, match->flags);
  4679. } else if (match->flags == ADSP_MMAP_HEAP_ADDR) {
  4680. if (fl)
  4681. err = fastrpc_munmap_on_dsp_rh(fl, match->phys,
  4682. match->size, match->flags, 0);
  4683. else {
  4684. pr_err("Cannot communicate with DSP, ADSP is down\n");
  4685. fastrpc_mmap_add(match);
  4686. }
  4687. }
  4688. }
  4689. memset(&ramdump_segments_rh, 0, sizeof(ramdump_segments_rh));
  4690. ramdump_segments_rh.da = match->phys;
  4691. ramdump_segments_rh.va = (void *)page_address((struct page *)match->va);
  4692. ramdump_segments_rh.size = match->size;
  4693. INIT_LIST_HEAD(&head);
  4694. list_add(&ramdump_segments_rh.node, &head);
  4695. if (me->dev && dump_enabled()) {
  4696. ret = qcom_elf_dump(&head, me->dev, ELF_CLASS);
  4697. if (ret < 0)
  4698. pr_err("adsprpc: %s: unable to dump heap (err %d)\n",
  4699. __func__, ret);
  4700. }
  4701. if (!match->is_persistent) {
  4702. if (!locked)
  4703. mutex_lock(&fl->map_mutex);
  4704. fastrpc_mmap_free(match, 0);
  4705. if (!locked)
  4706. mutex_unlock(&fl->map_mutex);
  4707. }
  4708. }
  4709. }
  4710. bail:
  4711. if (lock) {
  4712. spin_unlock_irqrestore(&me->hlock, irq_flags);
  4713. lock = 0;
  4714. }
  4715. if (err && match) {
  4716. if (!locked)
  4717. mutex_lock(&fl->map_mutex);
  4718. fastrpc_mmap_add(match);
  4719. if (!locked)
  4720. mutex_unlock(&fl->map_mutex);
  4721. }
  4722. return err;
  4723. }
  4724. static int fastrpc_mmap_remove_pdr(struct fastrpc_file *fl)
  4725. {
  4726. struct fastrpc_apps *me = &gfa;
  4727. int session = 0, err = 0, cid = -1;
  4728. if (!fl) {
  4729. err = -EBADF;
  4730. goto bail;
  4731. }
  4732. err = fastrpc_get_spd_session(fl->servloc_name,
  4733. &session, &cid);
  4734. if (err)
  4735. goto bail;
  4736. VERIFY(err, cid == fl->cid);
  4737. if (err) {
  4738. err = -EBADR;
  4739. goto bail;
  4740. }
  4741. if (atomic_read(&me->channel[cid].spd[session].ispdup) == 0) {
  4742. err = -ENOTCONN;
  4743. goto bail;
  4744. }
  4745. if (me->channel[cid].spd[session].pdrcount !=
  4746. me->channel[cid].spd[session].prevpdrcount) {
  4747. err = fastrpc_mmap_remove_ssr(fl, 0);
  4748. if (err)
  4749. ADSPRPC_WARN("failed to unmap remote heap (err %d)\n",
  4750. err);
  4751. me->channel[cid].spd[session].prevpdrcount =
  4752. me->channel[cid].spd[session].pdrcount;
  4753. }
  4754. bail:
  4755. return err;
  4756. }
  4757. static inline void get_fastrpc_ioctl_mmap_64(
  4758. struct fastrpc_ioctl_mmap_64 *mmap64,
  4759. struct fastrpc_ioctl_mmap *immap)
  4760. {
  4761. immap->fd = mmap64->fd;
  4762. immap->flags = mmap64->flags;
  4763. immap->vaddrin = (uintptr_t)mmap64->vaddrin;
  4764. immap->size = mmap64->size;
  4765. }
  4766. static inline void put_fastrpc_ioctl_mmap_64(
  4767. struct fastrpc_ioctl_mmap_64 *mmap64,
  4768. struct fastrpc_ioctl_mmap *immap)
  4769. {
  4770. mmap64->vaddrout = (uint64_t)immap->vaddrout;
  4771. }
  4772. static inline void get_fastrpc_ioctl_munmap_64(
  4773. struct fastrpc_ioctl_munmap_64 *munmap64,
  4774. struct fastrpc_ioctl_munmap *imunmap)
  4775. {
  4776. imunmap->vaddrout = (uintptr_t)munmap64->vaddrout;
  4777. imunmap->size = munmap64->size;
  4778. }
  4779. int fastrpc_internal_munmap(struct fastrpc_file *fl,
  4780. struct fastrpc_ioctl_munmap *ud)
  4781. {
  4782. int err = 0;
  4783. struct fastrpc_mmap *map = NULL;
  4784. struct fastrpc_buf *rbuf = NULL, *free = NULL;
  4785. struct hlist_node *n;
  4786. VERIFY(err, fl->dsp_proc_init == 1);
  4787. if (err) {
  4788. ADSPRPC_ERR(
  4789. "user application %s trying to unmap without initialization\n",
  4790. current->comm);
  4791. err = -EHOSTDOWN;
  4792. return err;
  4793. }
  4794. mutex_lock(&fl->internal_map_mutex);
  4795. spin_lock(&fl->hlock);
  4796. hlist_for_each_entry_safe(rbuf, n, &fl->remote_bufs, hn_rem) {
  4797. if (rbuf->raddr && ((rbuf->flags == ADSP_MMAP_ADD_PAGES) ||
  4798. (rbuf->flags == ADSP_MMAP_ADD_PAGES_LLC))) {
  4799. if ((rbuf->raddr == ud->vaddrout) &&
  4800. (rbuf->size == ud->size)) {
  4801. free = rbuf;
  4802. break;
  4803. }
  4804. }
  4805. }
  4806. spin_unlock(&fl->hlock);
  4807. if (free) {
  4808. VERIFY(err, !(err = fastrpc_munmap_on_dsp(fl, free->raddr,
  4809. free->phys, free->size, free->flags)));
  4810. if (err)
  4811. goto bail;
  4812. fastrpc_buf_free(rbuf, 0);
  4813. mutex_unlock(&fl->internal_map_mutex);
  4814. return err;
  4815. }
  4816. mutex_lock(&fl->map_mutex);
  4817. VERIFY(err, !(err = fastrpc_mmap_remove(fl, -1, ud->vaddrout,
  4818. ud->size, &map)));
  4819. mutex_unlock(&fl->map_mutex);
  4820. if (err)
  4821. goto bail;
  4822. VERIFY(err, map != NULL);
  4823. if (err) {
  4824. err = -EINVAL;
  4825. goto bail;
  4826. }
  4827. if (!map->is_persistent) {
  4828. VERIFY(err, !(err = fastrpc_munmap_on_dsp(fl, map->raddr,
  4829. map->phys, map->size, map->flags)));
  4830. }
  4831. if (err)
  4832. goto bail;
  4833. mutex_lock(&fl->map_mutex);
  4834. fastrpc_mmap_free(map, 0);
  4835. mutex_unlock(&fl->map_mutex);
  4836. bail:
  4837. if (err && map) {
  4838. mutex_lock(&fl->map_mutex);
  4839. fastrpc_mmap_add(map);
  4840. mutex_unlock(&fl->map_mutex);
  4841. }
  4842. mutex_unlock(&fl->internal_map_mutex);
  4843. return err;
  4844. }
  4845. /*
  4846. * fastrpc_internal_munmap_fd can only be used for buffers
  4847. * mapped with persist attributes. This can only be called
  4848. * once for any persist buffer
  4849. */
  4850. static int fastrpc_internal_munmap_fd(struct fastrpc_file *fl,
  4851. struct fastrpc_ioctl_munmap_fd *ud)
  4852. {
  4853. int err = 0;
  4854. struct fastrpc_mmap *map = NULL;
  4855. VERIFY(err, (fl && ud));
  4856. if (err) {
  4857. err = -EINVAL;
  4858. return err;
  4859. }
  4860. VERIFY(err, fl->dsp_proc_init == 1);
  4861. if (err) {
  4862. ADSPRPC_ERR(
  4863. "user application %s trying to unmap without initialization\n",
  4864. current->comm);
  4865. err = -EHOSTDOWN;
  4866. return err;
  4867. }
  4868. mutex_lock(&fl->internal_map_mutex);
  4869. mutex_lock(&fl->map_mutex);
  4870. err = fastrpc_mmap_find(fl, ud->fd, NULL, ud->va, ud->len, 0, 0, &map);
  4871. if (err) {
  4872. ADSPRPC_ERR(
  4873. "mapping not found to unmap fd 0x%x, va 0x%llx, len 0x%x, err %d\n",
  4874. ud->fd, (unsigned long long)ud->va,
  4875. (unsigned int)ud->len, err);
  4876. mutex_unlock(&fl->map_mutex);
  4877. goto bail;
  4878. }
  4879. if (map && (map->attr & FASTRPC_ATTR_KEEP_MAP)) {
  4880. map->attr = map->attr & (~FASTRPC_ATTR_KEEP_MAP);
  4881. fastrpc_mmap_free(map, 0);
  4882. }
  4883. mutex_unlock(&fl->map_mutex);
  4884. bail:
  4885. mutex_unlock(&fl->internal_map_mutex);
  4886. return err;
  4887. }
  4888. int fastrpc_internal_mem_map(struct fastrpc_file *fl,
  4889. struct fastrpc_ioctl_mem_map *ud)
  4890. {
  4891. int err = 0;
  4892. struct fastrpc_mmap *map = NULL;
  4893. VERIFY(err, fl->dsp_proc_init == 1);
  4894. if (err) {
  4895. pr_err("adsprpc: ERROR: %s: user application %s trying to map without initialization\n",
  4896. __func__, current->comm);
  4897. err = EBADR;
  4898. goto bail;
  4899. }
  4900. /* create SMMU mapping */
  4901. mutex_lock(&fl->map_mutex);
  4902. VERIFY(err, !(err = fastrpc_mmap_create(fl, ud->m.fd, NULL, ud->m.attrs,
  4903. ud->m.vaddrin, ud->m.length,
  4904. ud->m.flags, &map)));
  4905. mutex_unlock(&fl->map_mutex);
  4906. if (err)
  4907. goto bail;
  4908. if (map->raddr) {
  4909. err = -EEXIST;
  4910. goto bail;
  4911. }
  4912. /* create DSP mapping */
  4913. VERIFY(err, !(err = fastrpc_mem_map_to_dsp(fl, ud->m.fd, ud->m.offset,
  4914. ud->m.flags, map->va, map->phys, map->size, &map->raddr)));
  4915. if (err)
  4916. goto bail;
  4917. ud->m.vaddrout = map->raddr;
  4918. bail:
  4919. if (err) {
  4920. ADSPRPC_ERR("failed to map fd %d, len 0x%x, flags %d, map %pK, err %d\n",
  4921. ud->m.fd, ud->m.length, ud->m.flags, map, err);
  4922. if (map) {
  4923. mutex_lock(&fl->map_mutex);
  4924. fastrpc_mmap_free(map, 0);
  4925. mutex_unlock(&fl->map_mutex);
  4926. }
  4927. }
  4928. return err;
  4929. }
  4930. int fastrpc_internal_mem_unmap(struct fastrpc_file *fl,
  4931. struct fastrpc_ioctl_mem_unmap *ud)
  4932. {
  4933. int err = 0;
  4934. struct fastrpc_mmap *map = NULL;
  4935. size_t map_size = 0;
  4936. VERIFY(err, fl->dsp_proc_init == 1);
  4937. if (err) {
  4938. pr_err("adsprpc: ERROR: %s: user application %s trying to map without initialization\n",
  4939. __func__, current->comm);
  4940. err = EBADR;
  4941. goto bail;
  4942. }
  4943. mutex_lock(&fl->map_mutex);
  4944. VERIFY(err, !(err = fastrpc_mmap_remove(fl, ud->um.fd,
  4945. (uintptr_t)ud->um.vaddr, ud->um.length, &map)));
  4946. mutex_unlock(&fl->map_mutex);
  4947. if (err)
  4948. goto bail;
  4949. VERIFY(err, map->flags == FASTRPC_MAP_FD ||
  4950. map->flags == FASTRPC_MAP_FD_DELAYED ||
  4951. map->flags == FASTRPC_MAP_STATIC);
  4952. if (err) {
  4953. err = -EBADMSG;
  4954. goto bail;
  4955. }
  4956. map_size = map->size;
  4957. /* remove mapping on DSP */
  4958. VERIFY(err, !(err = fastrpc_mem_unmap_to_dsp(fl, map->fd, map->flags,
  4959. map->raddr, map->phys, map->size)));
  4960. if (err)
  4961. goto bail;
  4962. /* remove SMMU mapping */
  4963. mutex_lock(&fl->map_mutex);
  4964. fastrpc_mmap_free(map, 0);
  4965. mutex_unlock(&fl->map_mutex);
  4966. map = NULL;
  4967. bail:
  4968. if (err) {
  4969. ADSPRPC_ERR(
  4970. "failed to unmap fd %d addr 0x%llx length %zu map size %zu err 0x%x\n",
  4971. ud->um.fd, ud->um.vaddr, ud->um.length, map_size, err);
  4972. /* Add back to map list in case of error to unmap on DSP */
  4973. if (map) {
  4974. mutex_lock(&fl->map_mutex);
  4975. fastrpc_mmap_add(map);
  4976. mutex_unlock(&fl->map_mutex);
  4977. }
  4978. }
  4979. return err;
  4980. }
  4981. int fastrpc_internal_mmap(struct fastrpc_file *fl,
  4982. struct fastrpc_ioctl_mmap *ud)
  4983. {
  4984. struct fastrpc_mmap *map = NULL;
  4985. struct fastrpc_buf *rbuf = NULL;
  4986. unsigned long dma_attr = 0;
  4987. uintptr_t raddr = 0;
  4988. int err = 0;
  4989. VERIFY(err, fl->dsp_proc_init == 1);
  4990. if (err) {
  4991. ADSPRPC_ERR(
  4992. "user application %s trying to map without initialization\n",
  4993. current->comm);
  4994. err = -EHOSTDOWN;
  4995. return err;
  4996. }
  4997. mutex_lock(&fl->internal_map_mutex);
  4998. /* Pages for unsigned PD's user-heap should be allocated in userspace */
  4999. if (((ud->flags == ADSP_MMAP_ADD_PAGES) ||
  5000. (ud->flags == ADSP_MMAP_ADD_PAGES_LLC)) && !fl->is_unsigned_pd) {
  5001. if (ud->vaddrin) {
  5002. err = -EINVAL;
  5003. ADSPRPC_ERR(
  5004. "adding user allocated pages is not supported\n");
  5005. goto bail;
  5006. }
  5007. dma_attr = DMA_ATTR_DELAYED_UNMAP | DMA_ATTR_NO_KERNEL_MAPPING;
  5008. if (ud->flags == ADSP_MMAP_ADD_PAGES_LLC)
  5009. dma_attr |= DMA_ATTR_SYS_CACHE_ONLY;
  5010. err = fastrpc_buf_alloc(fl, ud->size, dma_attr, ud->flags,
  5011. USERHEAP_BUF, &rbuf);
  5012. if (err)
  5013. goto bail;
  5014. err = fastrpc_mmap_on_dsp(fl, ud->flags, 0,
  5015. rbuf->phys, rbuf->size, 0, &raddr);
  5016. if (err)
  5017. goto bail;
  5018. rbuf->raddr = raddr;
  5019. } else {
  5020. uintptr_t va_to_dsp;
  5021. if (fl->is_unsigned_pd && ud->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
  5022. err = -EINVAL;
  5023. ADSPRPC_ERR(
  5024. "Secure memory allocation is not supported in unsigned PD");
  5025. goto bail;
  5026. }
  5027. mutex_lock(&fl->map_mutex);
  5028. VERIFY(err, !(err = fastrpc_mmap_create(fl, ud->fd, NULL, 0,
  5029. (uintptr_t)ud->vaddrin, ud->size,
  5030. ud->flags, &map)));
  5031. mutex_unlock(&fl->map_mutex);
  5032. if (err)
  5033. goto bail;
  5034. if (ud->flags == ADSP_MMAP_HEAP_ADDR ||
  5035. ud->flags == ADSP_MMAP_REMOTE_HEAP_ADDR)
  5036. va_to_dsp = 0;
  5037. else
  5038. va_to_dsp = (uintptr_t)map->va;
  5039. VERIFY(err, 0 == (err = fastrpc_mmap_on_dsp(fl, ud->flags,
  5040. va_to_dsp, map->phys, map->size, map->refs, &raddr)));
  5041. if (err)
  5042. goto bail;
  5043. map->raddr = raddr;
  5044. }
  5045. ud->vaddrout = raddr;
  5046. bail:
  5047. if (err) {
  5048. if (map) {
  5049. mutex_lock(&fl->map_mutex);
  5050. fastrpc_mmap_free(map, 0);
  5051. mutex_unlock(&fl->map_mutex);
  5052. }
  5053. if (!IS_ERR_OR_NULL(rbuf))
  5054. fastrpc_buf_free(rbuf, 0);
  5055. }
  5056. mutex_unlock(&fl->internal_map_mutex);
  5057. return err;
  5058. }
  5059. static void fastrpc_context_list_dtor(struct fastrpc_file *fl);
  5060. static int fastrpc_session_alloc_locked(struct fastrpc_channel_ctx *chan,
  5061. int secure, int sharedcb, int pd_type, struct fastrpc_session_ctx **session)
  5062. {
  5063. struct fastrpc_apps *me = &gfa;
  5064. uint64_t idx = 0;
  5065. int err = 0;
  5066. /*
  5067. * PD type can be either unused(DEFAULT_UNUSED) (or) if PD type
  5068. * is used, choose the context bank with matching PD type.
  5069. */
  5070. if (chan->sesscount) {
  5071. for (idx = 0; idx < chan->sesscount; ++idx) {
  5072. if (!chan->session[idx].used &&
  5073. chan->session[idx].smmu.secure == secure &&
  5074. chan->session[idx].smmu.sharedcb == sharedcb &&
  5075. (pd_type == DEFAULT_UNUSED ||
  5076. chan->session[idx].smmu.pd_type == pd_type)) {
  5077. chan->session[idx].used = 1;
  5078. break;
  5079. }
  5080. }
  5081. if (idx >= chan->sesscount) {
  5082. err = -EUSERS;
  5083. goto bail;
  5084. }
  5085. chan->session[idx].smmu.faults = 0;
  5086. } else {
  5087. VERIFY(err, me->dev != NULL);
  5088. if (err) {
  5089. err = -ENODEV;
  5090. goto bail;
  5091. }
  5092. chan->session[0].dev = me->dev;
  5093. chan->session[0].smmu.dev = me->dev;
  5094. }
  5095. *session = &chan->session[idx];
  5096. bail:
  5097. return err;
  5098. }
  5099. static void handle_remote_signal(uint64_t msg, int cid)
  5100. {
  5101. struct fastrpc_apps *me = &gfa;
  5102. uint32_t pid = msg >> 32;
  5103. uint32_t signal_id = msg & 0xffffffff;
  5104. struct fastrpc_file *fl = NULL;
  5105. struct hlist_node *n = NULL;
  5106. unsigned long irq_flags = 0;
  5107. DSPSIGNAL_VERBOSE("Received queue signal %llx: PID %u, signal %u\n", msg, pid, signal_id);
  5108. if (signal_id >= DSPSIGNAL_NUM_SIGNALS) {
  5109. ADSPRPC_ERR("Received bad signal %u for PID %u\n", signal_id, pid);
  5110. return;
  5111. }
  5112. spin_lock_irqsave(&me->hlock, irq_flags);
  5113. hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
  5114. /* Response from DSP contains unique fastrpc process id, use unique fastrpc process ID to compare */
  5115. if ((fl->tgid_frpc == pid) && (fl->cid == cid)) {
  5116. unsigned long fflags = 0;
  5117. spin_lock_irqsave(&fl->dspsignals_lock, fflags);
  5118. if (fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE]) {
  5119. struct fastrpc_dspsignal *group =
  5120. fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE];
  5121. struct fastrpc_dspsignal *sig =
  5122. &group[signal_id % DSPSIGNAL_GROUP_SIZE];
  5123. if ((sig->state == DSPSIGNAL_STATE_PENDING) ||
  5124. (sig->state == DSPSIGNAL_STATE_SIGNALED)) {
  5125. DSPSIGNAL_VERBOSE("Signaling signal %u for PID %u\n",
  5126. signal_id, pid);
  5127. complete(&sig->comp);
  5128. sig->state = DSPSIGNAL_STATE_SIGNALED;
  5129. } else if (sig->state == DSPSIGNAL_STATE_UNUSED) {
  5130. ADSPRPC_ERR("Received unknown signal %u for PID %u\n",
  5131. signal_id, pid);
  5132. }
  5133. } else {
  5134. ADSPRPC_ERR("Received unknown signal %u for PID %u\n",
  5135. signal_id, pid);
  5136. }
  5137. spin_unlock_irqrestore(&fl->dspsignals_lock, fflags);
  5138. break;
  5139. }
  5140. }
  5141. spin_unlock_irqrestore(&me->hlock, irq_flags);
  5142. }
  5143. int fastrpc_handle_rpc_response(void *data, int len, int cid)
  5144. {
  5145. struct smq_invoke_rsp *rsp = (struct smq_invoke_rsp *)data;
  5146. struct smq_notif_rspv3 *notif = (struct smq_notif_rspv3 *)data;
  5147. struct smq_invoke_rspv2 *rspv2 = NULL;
  5148. struct smq_invoke_ctx *ctx = NULL;
  5149. struct fastrpc_apps *me = &gfa;
  5150. uint32_t index, rsp_flags = 0, early_wake_time = 0, ver = 0;
  5151. int err = 0, ignore_rsp_err = 0;
  5152. struct fastrpc_channel_ctx *chan = NULL;
  5153. unsigned long irq_flags = 0;
  5154. int64_t ns = 0;
  5155. uint64_t xo_time_in_us = 0;
  5156. xo_time_in_us = CONVERT_CNT_TO_US(__arch_counter_get_cntvct());
  5157. if (len == sizeof(uint64_t)) {
  5158. /*
  5159. * dspsignal message from the DSP
  5160. */
  5161. handle_remote_signal(*((uint64_t *)data), cid);
  5162. goto bail;
  5163. }
  5164. chan = &me->channel[cid];
  5165. VERIFY(err, (rsp && len >= sizeof(*rsp)));
  5166. if (err) {
  5167. err = -EINVAL;
  5168. goto bail;
  5169. }
  5170. if (notif->ctx == FASTRPC_NOTIF_CTX_RESERVED) {
  5171. VERIFY(err, (notif->type == STATUS_RESPONSE &&
  5172. len >= sizeof(*notif)));
  5173. if (err)
  5174. goto bail;
  5175. fastrpc_notif_find_process(cid, notif);
  5176. goto bail;
  5177. }
  5178. if (len >= sizeof(struct smq_invoke_rspv2))
  5179. rspv2 = (struct smq_invoke_rspv2 *)data;
  5180. if (rspv2) {
  5181. early_wake_time = rspv2->early_wake_time;
  5182. rsp_flags = rspv2->flags;
  5183. ver = rspv2->version;
  5184. }
  5185. trace_fastrpc_transport_response(cid, rsp->ctx,
  5186. rsp->retval, rsp_flags, early_wake_time);
  5187. ns = get_timestamp_in_ns();
  5188. fastrpc_update_rxmsg_buf(chan, rsp->ctx, rsp->retval,
  5189. rsp_flags, early_wake_time, ver, ns, xo_time_in_us);
  5190. index = (uint32_t)GET_TABLE_IDX_FROM_CTXID(rsp->ctx);
  5191. VERIFY(err, index < FASTRPC_CTX_MAX);
  5192. if (err)
  5193. goto bail;
  5194. spin_lock_irqsave(&chan->ctxlock, irq_flags);
  5195. ctx = chan->ctxtable[index];
  5196. VERIFY(err, !IS_ERR_OR_NULL(ctx) &&
  5197. (ctx->ctxid == GET_CTXID_FROM_RSP_CTX(rsp->ctx)) &&
  5198. ctx->magic == FASTRPC_CTX_MAGIC);
  5199. if (err) {
  5200. /*
  5201. * Received an anticipatory COMPLETE_SIGNAL from DSP for a
  5202. * context after CPU successfully polling on memory and
  5203. * completed processing of context. Ignore the message.
  5204. * Also ignore response for a call which was already
  5205. * completed by update of poll memory and the context was
  5206. * removed from the table and possibly reused for another call.
  5207. */
  5208. ignore_rsp_err = ((rsp_flags == COMPLETE_SIGNAL) || !ctx ||
  5209. (ctx && (ctx->ctxid != GET_CTXID_FROM_RSP_CTX(rsp->ctx)))) ? 1 : 0;
  5210. goto bail_unlock;
  5211. }
  5212. if (rspv2) {
  5213. VERIFY(err, rspv2->version == FASTRPC_RSP_VERSION2);
  5214. if (err)
  5215. goto bail_unlock;
  5216. }
  5217. VERIFY(err, VALID_FASTRPC_CID(ctx->fl->cid));
  5218. if (err) {
  5219. err = -ECHRNG;
  5220. goto bail_unlock;
  5221. }
  5222. context_notify_user(ctx, rsp->retval, rsp_flags, early_wake_time);
  5223. bail_unlock:
  5224. spin_unlock_irqrestore(&chan->ctxlock, irq_flags);
  5225. bail:
  5226. if (err) {
  5227. err = -ENOKEY;
  5228. if (!ignore_rsp_err)
  5229. ADSPRPC_ERR(
  5230. "invalid response data %pK, len %d from remote subsystem err %d\n",
  5231. data, len, err);
  5232. else {
  5233. err = 0;
  5234. me->duplicate_rsp_err_cnt++;
  5235. }
  5236. }
  5237. return err;
  5238. }
  5239. static int fastrpc_session_alloc_secure_memory(
  5240. struct fastrpc_channel_ctx *chan, int secure,
  5241. int sharedcb, int pd_type, struct fastrpc_session_ctx **session)
  5242. {
  5243. int err = 0;
  5244. struct fastrpc_apps *me = &gfa;
  5245. /*
  5246. * If PD type is configured for context banks,
  5247. * Use CPZ_USERPD, to allocate secure context bank type.
  5248. */
  5249. if (pd_type != DEFAULT_UNUSED && me->cb_pd_type)
  5250. pd_type = CPZ_USERPD;
  5251. mutex_lock(&chan->smd_mutex);
  5252. if (!*session)
  5253. err = fastrpc_session_alloc_locked(chan, secure, sharedcb, pd_type, session);
  5254. mutex_unlock(&chan->smd_mutex);
  5255. if (err == -EUSERS) {
  5256. ADSPRPC_WARN(
  5257. "max concurrent sessions limit (%d) already reached on %s err %d\n",
  5258. chan->sesscount, chan->subsys, err);
  5259. }
  5260. return err;
  5261. }
  5262. static void fastrpc_session_free(struct fastrpc_channel_ctx *chan,
  5263. struct fastrpc_session_ctx *session)
  5264. {
  5265. mutex_lock(&chan->smd_mutex);
  5266. session->used = 0;
  5267. mutex_unlock(&chan->smd_mutex);
  5268. }
  5269. static int fastrpc_file_free(struct fastrpc_file *fl)
  5270. {
  5271. struct hlist_node *n = NULL;
  5272. struct fastrpc_mmap *map = NULL, *lmap = NULL;
  5273. unsigned long flags;
  5274. int cid;
  5275. struct fastrpc_apps *me = &gfa;
  5276. bool is_driver_closed = false;
  5277. int err = 0;
  5278. unsigned long irq_flags = 0;
  5279. bool is_locked = false;
  5280. int i;
  5281. if (!fl)
  5282. return 0;
  5283. cid = fl->cid;
  5284. spin_lock_irqsave(&me->hlock, irq_flags);
  5285. if (fl->device) {
  5286. fl->device->dev_close = true;
  5287. if (fl->device->refs == 0) {
  5288. is_driver_closed = true;
  5289. hlist_del_init(&fl->device->hn);
  5290. }
  5291. }
  5292. fl->file_close = FASTRPC_PROCESS_EXIT_START;
  5293. spin_unlock_irqrestore(&me->hlock, irq_flags);
  5294. (void)fastrpc_release_current_dsp_process(fl);
  5295. spin_lock_irqsave(&fl->apps->hlock, irq_flags);
  5296. is_locked = true;
  5297. if (!fl->is_dma_invoke_pend)
  5298. goto skip_dmainvoke_wait;
  5299. is_locked = false;
  5300. spin_unlock_irqrestore(&fl->apps->hlock, irq_flags);
  5301. wait_for_completion(&fl->dma_invoke);
  5302. skip_dmainvoke_wait:
  5303. if (!is_locked) {
  5304. spin_lock_irqsave(&fl->apps->hlock, irq_flags);
  5305. is_locked = true;
  5306. }
  5307. if (!fl->is_ramdump_pend)
  5308. goto skip_dump_wait;
  5309. is_locked = false;
  5310. spin_unlock_irqrestore(&fl->apps->hlock, irq_flags);
  5311. wait_for_completion(&fl->work);
  5312. skip_dump_wait:
  5313. if (!is_locked) {
  5314. spin_lock_irqsave(&fl->apps->hlock, irq_flags);
  5315. is_locked = true;
  5316. }
  5317. hlist_del_init(&fl->hn);
  5318. fl->is_ramdump_pend = false;
  5319. fl->is_dma_invoke_pend = false;
  5320. fl->dsp_process_state = PROCESS_CREATE_DEFAULT;
  5321. /* Reset the tgid usage to false */
  5322. if (VALID_FASTRPC_CID(cid) && fl->tgid_frpc != -1)
  5323. frpc_tgid_usage_array[cid][fl->tgid_frpc] = false;
  5324. is_locked = false;
  5325. spin_unlock_irqrestore(&fl->apps->hlock, irq_flags);
  5326. if (!fl->sctx) {
  5327. kfree(fl);
  5328. return 0;
  5329. }
  5330. //Dummy wake up to exit Async worker thread
  5331. spin_lock_irqsave(&fl->aqlock, flags);
  5332. atomic_add(1, &fl->async_queue_job_count);
  5333. wake_up_interruptible(&fl->async_wait_queue);
  5334. spin_unlock_irqrestore(&fl->aqlock, flags);
  5335. // Dummy wake up to exit notification worker thread
  5336. spin_lock_irqsave(&fl->proc_state_notif.nqlock, flags);
  5337. atomic_add(1, &fl->proc_state_notif.notif_queue_count);
  5338. wake_up_interruptible(&fl->proc_state_notif.notif_wait_queue);
  5339. spin_unlock_irqrestore(&fl->proc_state_notif.nqlock, flags);
  5340. if (!IS_ERR_OR_NULL(fl->init_mem))
  5341. fastrpc_buf_free(fl->init_mem, 0);
  5342. fastrpc_context_list_dtor(fl);
  5343. fastrpc_cached_buf_list_free(fl);
  5344. if (!IS_ERR_OR_NULL(fl->hdr_bufs))
  5345. kfree(fl->hdr_bufs);
  5346. if (!IS_ERR_OR_NULL(fl->pers_hdr_buf))
  5347. fastrpc_buf_free(fl->pers_hdr_buf, 0);
  5348. mutex_lock(&fl->internal_map_mutex);
  5349. mutex_lock(&fl->map_mutex);
  5350. do {
  5351. lmap = NULL;
  5352. hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
  5353. hlist_del_init(&map->hn);
  5354. lmap = map;
  5355. break;
  5356. }
  5357. fastrpc_mmap_free(lmap, 1);
  5358. } while (lmap);
  5359. mutex_unlock(&fl->map_mutex);
  5360. mutex_unlock(&fl->internal_map_mutex);
  5361. if (fl->device && is_driver_closed)
  5362. device_unregister(&fl->device->dev);
  5363. VERIFY(err, VALID_FASTRPC_CID(cid));
  5364. if (!err && fl->sctx)
  5365. fastrpc_session_free(&fl->apps->channel[cid], fl->sctx);
  5366. if (!err && fl->secsctx)
  5367. fastrpc_session_free(&fl->apps->channel[cid], fl->secsctx);
  5368. for (i = 0; i < (DSPSIGNAL_NUM_SIGNALS / DSPSIGNAL_GROUP_SIZE); i++)
  5369. kfree(fl->signal_groups[i]);
  5370. mutex_destroy(&fl->signal_create_mutex);
  5371. fastrpc_remote_buf_list_free(fl);
  5372. mutex_destroy(&fl->map_mutex);
  5373. mutex_destroy(&fl->internal_map_mutex);
  5374. kfree(fl->dev_pm_qos_req);
  5375. kfree(fl->gidlist.gids);
  5376. kfree(fl);
  5377. return 0;
  5378. }
  5379. static int fastrpc_device_release(struct inode *inode, struct file *file)
  5380. {
  5381. struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data;
  5382. struct fastrpc_apps *me = &gfa;
  5383. unsigned int ii;
  5384. if (!fl)
  5385. return 0;
  5386. if (fl->qos_request && fl->dev_pm_qos_req) {
  5387. for (ii = 0; ii < me->lowest_capacity_core_count; ii++) {
  5388. if (!dev_pm_qos_request_active(&fl->dev_pm_qos_req[ii]))
  5389. continue;
  5390. dev_pm_qos_remove_request(&fl->dev_pm_qos_req[ii]);
  5391. }
  5392. }
  5393. debugfs_remove(fl->debugfs_file);
  5394. fastrpc_file_free(fl);
  5395. file->private_data = NULL;
  5396. return 0;
  5397. }
  5398. static ssize_t fastrpc_debugfs_read(struct file *filp, char __user *buffer,
  5399. size_t count, loff_t *position)
  5400. {
  5401. struct fastrpc_apps *me = &gfa;
  5402. struct fastrpc_file *fl = filp->private_data;
  5403. struct hlist_node *n;
  5404. struct fastrpc_buf *buf = NULL;
  5405. struct fastrpc_mmap *map = NULL;
  5406. struct fastrpc_mmap *gmaps = NULL;
  5407. struct smq_invoke_ctx *ictx = NULL;
  5408. struct fastrpc_channel_ctx *chan = NULL;
  5409. unsigned int len = 0;
  5410. int i, j, sess_used = 0, ret = 0;
  5411. char *fileinfo = NULL;
  5412. char single_line[] = "----------------";
  5413. char title[] = "=========================";
  5414. unsigned long irq_flags = 0;
  5415. fileinfo = kzalloc(DEBUGFS_SIZE, GFP_KERNEL);
  5416. if (!fileinfo) {
  5417. ret = -ENOMEM;
  5418. goto bail;
  5419. }
  5420. if (fl == NULL) {
  5421. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5422. "\n%s %s %s\n", title, " CHANNEL INFO ", title);
  5423. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5424. "%-7s|%-10s|%-15s|%-9s|%-13s\n",
  5425. "subsys", "sesscount", "subsystemstate",
  5426. "ssrcount", "session_used");
  5427. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5428. "-%s%s%s%s-\n", single_line, single_line,
  5429. single_line, single_line);
  5430. for (i = 0; i < NUM_CHANNELS; i++) {
  5431. sess_used = 0;
  5432. chan = &gcinfo[i];
  5433. len += scnprintf(fileinfo + len,
  5434. DEBUGFS_SIZE - len, "%-7s", chan->subsys);
  5435. len += scnprintf(fileinfo + len,
  5436. DEBUGFS_SIZE - len, "|%-10u",
  5437. chan->sesscount);
  5438. len += scnprintf(fileinfo + len,
  5439. DEBUGFS_SIZE - len, "|%-15d",
  5440. chan->subsystemstate);
  5441. len += scnprintf(fileinfo + len,
  5442. DEBUGFS_SIZE - len, "|%-9u",
  5443. chan->ssrcount);
  5444. for (j = 0; j < chan->sesscount; j++)
  5445. sess_used += chan->session[j].used;
  5446. len += scnprintf(fileinfo + len,
  5447. DEBUGFS_SIZE - len, "|%-13d\n", sess_used);
  5448. }
  5449. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5450. "\n%s%s%s\n", "=============",
  5451. " CMA HEAP ", "==============");
  5452. len += scnprintf(fileinfo + len,
  5453. DEBUGFS_SIZE - len, "%-20s|%-20s\n", "addr", "size");
  5454. len += scnprintf(fileinfo + len,
  5455. DEBUGFS_SIZE - len, "--%s%s---\n",
  5456. single_line, single_line);
  5457. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5458. "\n==========%s %s %s===========\n",
  5459. title, " GMAPS ", title);
  5460. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5461. "%-20s|%-20s|%-20s|%-20s\n",
  5462. "fd", "phys", "size", "va");
  5463. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5464. "%s%s%s%s%s\n", single_line, single_line,
  5465. single_line, single_line, single_line);
  5466. spin_lock_irqsave(&me->hlock, irq_flags);
  5467. hlist_for_each_entry_safe(gmaps, n, &me->maps, hn) {
  5468. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5469. "%-20d|0x%-18llX|0x%-18X|0x%-20lX\n\n",
  5470. gmaps->fd, gmaps->phys,
  5471. (uint32_t)gmaps->size,
  5472. gmaps->va);
  5473. }
  5474. spin_unlock_irqrestore(&me->hlock, irq_flags);
  5475. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5476. "%-20s|%-20s|%-20s|%-20s\n",
  5477. "len", "refs", "raddr", "flags");
  5478. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5479. "%s%s%s%s%s\n", single_line, single_line,
  5480. single_line, single_line, single_line);
  5481. spin_lock_irqsave(&me->hlock, irq_flags);
  5482. hlist_for_each_entry_safe(gmaps, n, &me->maps, hn) {
  5483. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5484. "0x%-18X|%-20d|%-20lu|%-20u\n",
  5485. (uint32_t)gmaps->len, gmaps->refs,
  5486. gmaps->raddr, gmaps->flags);
  5487. }
  5488. spin_unlock_irqrestore(&me->hlock, irq_flags);
  5489. } else {
  5490. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5491. "\n%s %13s %d\n", "cid", ":", fl->cid);
  5492. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5493. "%s %12s %d\n", "tgid", ":", fl->tgid);
  5494. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5495. "%s %7s %d\n", "sessionid", ":", fl->sessionid);
  5496. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5497. "%s %8s %u\n", "ssrcount", ":", fl->ssrcount);
  5498. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5499. "%s %14s %d\n", "pd", ":", fl->pd);
  5500. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5501. "%s %9s %s\n", "servloc_name", ":", fl->servloc_name);
  5502. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5503. "%s %6s %d\n", "file_close", ":", fl->file_close);
  5504. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5505. "%s %9s %d\n", "profile", ":", fl->profile);
  5506. if (fl->sctx) {
  5507. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5508. "%s %3s %d\n", "smmu.coherent", ":",
  5509. fl->sctx->smmu.coherent);
  5510. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5511. "%s %4s %d\n", "smmu.enabled", ":",
  5512. fl->sctx->smmu.enabled);
  5513. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5514. "%s %9s %d\n", "smmu.cb", ":", fl->sctx->smmu.cb);
  5515. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5516. "%s %5s %d\n", "smmu.secure", ":",
  5517. fl->sctx->smmu.secure);
  5518. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5519. "%s %5s %d\n", "smmu.faults", ":",
  5520. fl->sctx->smmu.faults);
  5521. }
  5522. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5523. "\n=======%s %s %s======\n", title,
  5524. " LIST OF MAPS ", title);
  5525. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5526. "%-20s|%-20s|%-20s|%-20s\n", "va", "phys", "size", "flags");
  5527. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5528. "%s%s%s%s%s\n",
  5529. single_line, single_line, single_line,
  5530. single_line, single_line);
  5531. mutex_lock(&fl->map_mutex);
  5532. hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
  5533. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5534. "0x%-20lX|0x%-20llX|0x%-20zu|0x%-17llX\n\n",
  5535. map->va, map->phys,
  5536. map->size, map->flags);
  5537. }
  5538. mutex_unlock(&fl->map_mutex);
  5539. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5540. "%-20s|%-20s|%-20s\n",
  5541. "len", "refs",
  5542. "raddr");
  5543. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5544. "%s%s%s%s%s\n",
  5545. single_line, single_line, single_line,
  5546. single_line, single_line);
  5547. mutex_lock(&fl->map_mutex);
  5548. hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
  5549. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5550. "%-20zu|%-20d|0x%-20lX\n\n",
  5551. map->len, map->refs, map->raddr);
  5552. }
  5553. mutex_unlock(&fl->map_mutex);
  5554. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5555. "%-20s|%-20s\n", "secure", "attr");
  5556. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5557. "%s%s%s%s%s\n",
  5558. single_line, single_line, single_line,
  5559. single_line, single_line);
  5560. mutex_lock(&fl->map_mutex);
  5561. hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
  5562. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5563. "%-20d|0x%-20lX\n\n",
  5564. map->secure, map->attr);
  5565. }
  5566. mutex_unlock(&fl->map_mutex);
  5567. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5568. "\n======%s %s %s======\n", title,
  5569. " LIST OF BUFS ", title);
  5570. spin_lock(&fl->hlock);
  5571. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5572. "%-19s|%-19s|%-19s|%-19s\n",
  5573. "virt", "phys", "size", "flags");
  5574. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5575. "%s%s%s%s%s\n", single_line, single_line,
  5576. single_line, single_line, single_line);
  5577. hlist_for_each_entry_safe(buf, n, &fl->cached_bufs, hn) {
  5578. len += scnprintf(fileinfo + len,
  5579. DEBUGFS_SIZE - len,
  5580. "0x%-17p|0x%-17llX|%-19zu|0x%-17llX\n",
  5581. buf->virt, (uint64_t)buf->phys, buf->size, buf->flags);
  5582. }
  5583. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5584. "\n======%s %s %s======\n", title,
  5585. " LIST OF REMOTE BUFS ", title);
  5586. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5587. "%-19s|%-19s|%-19s|%-19s\n",
  5588. "virt", "phys", "size", "flags");
  5589. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5590. "%s%s%s%s%s\n", single_line, single_line,
  5591. single_line, single_line, single_line);
  5592. hlist_for_each_entry_safe(buf, n, &fl->remote_bufs, hn_rem) {
  5593. len += scnprintf(fileinfo + len,
  5594. DEBUGFS_SIZE - len,
  5595. "0x%-17p|0x%-17llX|%-19zu|0x%-17llX\n",
  5596. buf->virt, (uint64_t)buf->phys, buf->size, buf->flags);
  5597. }
  5598. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5599. "\n%s %s %s\n", title,
  5600. " LIST OF PENDING SMQCONTEXTS ", title);
  5601. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5602. "%-20s|%-10s|%-10s|%-10s|%-20s\n",
  5603. "sc", "pid", "tgid", "used", "ctxid");
  5604. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5605. "%s%s%s%s%s\n", single_line, single_line,
  5606. single_line, single_line, single_line);
  5607. hlist_for_each_entry_safe(ictx, n, &fl->clst.pending, hn) {
  5608. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5609. "0x%-18X|%-10d|%-10d|%-10zu|0x%-20llX\n\n",
  5610. ictx->sc, ictx->pid, ictx->tgid,
  5611. ictx->used, ictx->ctxid);
  5612. }
  5613. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5614. "\n%s %s %s\n", title,
  5615. " LIST OF INTERRUPTED SMQCONTEXTS ", title);
  5616. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5617. "%-20s|%-10s|%-10s|%-10s|%-20s\n",
  5618. "sc", "pid", "tgid", "used", "ctxid");
  5619. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5620. "%s%s%s%s%s\n", single_line, single_line,
  5621. single_line, single_line, single_line);
  5622. hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) {
  5623. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5624. "%-20u|%-20d|%-20d|%-20zu|0x%-20llX\n\n",
  5625. ictx->sc, ictx->pid, ictx->tgid,
  5626. ictx->used, ictx->ctxid);
  5627. }
  5628. spin_unlock(&fl->hlock);
  5629. }
  5630. if (len > DEBUGFS_SIZE)
  5631. len = DEBUGFS_SIZE;
  5632. ret = simple_read_from_buffer(buffer, count, position, fileinfo, len);
  5633. kfree(fileinfo);
  5634. bail:
  5635. return ret;
  5636. }
  5637. static const struct file_operations debugfs_fops = {
  5638. .open = simple_open,
  5639. .read = fastrpc_debugfs_read,
  5640. };
  5641. static int fastrpc_channel_open(struct fastrpc_file *fl, uint32_t flags)
  5642. {
  5643. struct fastrpc_apps *me = &gfa;
  5644. int cid = -1, err = 0;
  5645. VERIFY(err, fl && fl->sctx && fl->cid >= 0 && fl->cid < NUM_CHANNELS);
  5646. if (err) {
  5647. ADSPRPC_ERR("kernel session not initialized yet for %s\n",
  5648. current->comm);
  5649. err = -EBADR;
  5650. return err;
  5651. }
  5652. cid = fl->cid;
  5653. err = fastrpc_wait_for_transport_interrupt(cid, flags);
  5654. if (err)
  5655. goto bail;
  5656. err = verify_transport_device(cid, fl->tvm_remote_domain);
  5657. if (err)
  5658. goto bail;
  5659. mutex_lock(&me->channel[cid].smd_mutex);
  5660. if (me->channel[cid].ssrcount !=
  5661. me->channel[cid].prevssrcount) {
  5662. if (me->channel[cid].subsystemstate != SUBSYSTEM_UP) {
  5663. err = -ECONNREFUSED;
  5664. mutex_unlock(&me->channel[cid].smd_mutex);
  5665. goto bail;
  5666. }
  5667. }
  5668. fl->ssrcount = me->channel[cid].ssrcount;
  5669. if (cid == ADSP_DOMAIN_ID && me->channel[cid].ssrcount !=
  5670. me->channel[cid].prevssrcount) {
  5671. mutex_unlock(&me->channel[cid].smd_mutex);
  5672. mutex_lock(&fl->map_mutex);
  5673. err = fastrpc_mmap_remove_ssr(fl, 1);
  5674. mutex_unlock(&fl->map_mutex);
  5675. if (err)
  5676. ADSPRPC_WARN(
  5677. "failed to unmap remote heap for %s (err %d)\n",
  5678. me->channel[cid].subsys, err);
  5679. mutex_lock(&me->channel[cid].smd_mutex);
  5680. me->channel[cid].prevssrcount =
  5681. me->channel[cid].ssrcount;
  5682. }
  5683. me->channel[cid].in_hib = 0;
  5684. mutex_unlock(&me->channel[cid].smd_mutex);
  5685. bail:
  5686. return err;
  5687. }
  5688. static inline void fastrpc_register_wakeup_source(struct device *dev,
  5689. const char *client_name, struct wakeup_source **device_wake_source)
  5690. {
  5691. struct wakeup_source *wake_source = NULL;
  5692. wake_source = wakeup_source_register(dev, client_name);
  5693. if (IS_ERR_OR_NULL(wake_source)) {
  5694. ADSPRPC_ERR(
  5695. "wakeup_source_register failed for dev %s, client %s with err %ld\n",
  5696. dev_name(dev), client_name, PTR_ERR(wake_source));
  5697. return;
  5698. }
  5699. *device_wake_source = wake_source;
  5700. }
  5701. static int fastrpc_device_open(struct inode *inode, struct file *filp)
  5702. {
  5703. int err = 0;
  5704. struct fastrpc_file *fl = NULL;
  5705. struct fastrpc_apps *me = &gfa;
  5706. unsigned long irq_flags = 0;
  5707. /*
  5708. * Indicates the device node opened
  5709. * MINOR_NUM_DEV or MINOR_NUM_SECURE_DEV
  5710. */
  5711. int dev_minor = MINOR(inode->i_rdev);
  5712. VERIFY(err, ((dev_minor == MINOR_NUM_DEV) ||
  5713. (dev_minor == MINOR_NUM_SECURE_DEV)));
  5714. if (err) {
  5715. ADSPRPC_ERR("Invalid dev minor num %d\n",
  5716. dev_minor);
  5717. return err;
  5718. }
  5719. VERIFY(err, NULL != (fl = kzalloc(sizeof(*fl), GFP_KERNEL)));
  5720. if (err) {
  5721. err = -ENOMEM;
  5722. return err;
  5723. }
  5724. context_list_ctor(&fl->clst);
  5725. spin_lock_init(&fl->hlock);
  5726. spin_lock_init(&fl->aqlock);
  5727. spin_lock_init(&fl->proc_state_notif.nqlock);
  5728. INIT_HLIST_HEAD(&fl->maps);
  5729. INIT_HLIST_HEAD(&fl->cached_bufs);
  5730. fl->num_cached_buf = 0;
  5731. INIT_HLIST_HEAD(&fl->remote_bufs);
  5732. init_waitqueue_head(&fl->async_wait_queue);
  5733. init_waitqueue_head(&fl->proc_state_notif.notif_wait_queue);
  5734. INIT_HLIST_NODE(&fl->hn);
  5735. fl->sessionid = 0;
  5736. fl->tgid_open = current->tgid;
  5737. /* PD type is not known, when device is opened */
  5738. fl->pd_type = DEFAULT_UNUSED;
  5739. fl->apps = me;
  5740. fl->mode = FASTRPC_MODE_SERIAL;
  5741. fl->cid = -1;
  5742. fl->tgid_frpc = -1;
  5743. fl->tvm_remote_domain = -1;
  5744. fl->dev_minor = dev_minor;
  5745. fl->init_mem = NULL;
  5746. fl->qos_request = 0;
  5747. fl->dsp_proc_init = 0;
  5748. fl->is_ramdump_pend = false;
  5749. fl->dsp_process_state = PROCESS_CREATE_DEFAULT;
  5750. fl->is_unsigned_pd = false;
  5751. fl->is_compat = false;
  5752. fl->exit_notif = false;
  5753. fl->exit_async = false;
  5754. fl->multi_session_support = false;
  5755. fl->set_session_info = false;
  5756. init_completion(&fl->work);
  5757. init_completion(&fl->dma_invoke);
  5758. fl->file_close = FASTRPC_PROCESS_DEFAULT_STATE;
  5759. filp->private_data = fl;
  5760. fl->sharedbuf_info.buf_fd = -1;
  5761. mutex_init(&fl->internal_map_mutex);
  5762. mutex_init(&fl->map_mutex);
  5763. spin_lock_irqsave(&me->hlock, irq_flags);
  5764. hlist_add_head(&fl->hn, &me->drivers);
  5765. spin_unlock_irqrestore(&me->hlock, irq_flags);
  5766. if (me->lowest_capacity_core_count)
  5767. fl->dev_pm_qos_req = kzalloc((me->lowest_capacity_core_count) *
  5768. sizeof(struct dev_pm_qos_request),
  5769. GFP_KERNEL);
  5770. spin_lock_init(&fl->dspsignals_lock);
  5771. mutex_init(&fl->signal_create_mutex);
  5772. init_completion(&fl->shutdown);
  5773. return 0;
  5774. }
  5775. static int fastrpc_get_process_gids(struct gid_list *gidlist)
  5776. {
  5777. struct group_info *group_info = get_current_groups();
  5778. int i = 0, err = 0, num_gids = group_info->ngroups + 1;
  5779. unsigned int *gids = NULL;
  5780. gids = kcalloc(num_gids, sizeof(unsigned int), GFP_KERNEL);
  5781. if (!gids) {
  5782. err = -ENOMEM;
  5783. goto bail;
  5784. }
  5785. /* Get the real GID */
  5786. gids[0] = __kgid_val(current_gid());
  5787. /* Get the supplemental GIDs */
  5788. for (i = 1; i < num_gids; i++)
  5789. gids[i] = __kgid_val(group_info->gid[i - 1]);
  5790. sort(gids, num_gids, sizeof(*gids), uint_cmp_func, NULL);
  5791. gidlist->gids = gids;
  5792. gidlist->gidcount = num_gids;
  5793. bail:
  5794. if (err)
  5795. kfree(gids);
  5796. return err;
  5797. }
  5798. // Generate a unique process ID to DSP process
  5799. static int get_unique_hlos_process_id(uint32_t cid)
  5800. {
  5801. int tgid_frpc = -1, tgid_index = 1;
  5802. struct fastrpc_apps *me = &gfa;
  5803. unsigned long irq_flags = 0;
  5804. spin_lock_irqsave(&me->hlock, irq_flags);
  5805. for (tgid_index = 1; tgid_index < MAX_FRPC_TGID; tgid_index++) {
  5806. if (!frpc_tgid_usage_array[cid][tgid_index]) {
  5807. tgid_frpc = tgid_index;
  5808. /* Set the tgid usage to false */
  5809. frpc_tgid_usage_array[cid][tgid_index] = true;
  5810. break;
  5811. }
  5812. }
  5813. spin_unlock_irqrestore(&me->hlock, irq_flags);
  5814. return tgid_frpc;
  5815. }
  5816. static int fastrpc_set_process_info(struct fastrpc_file *fl, uint32_t cid)
  5817. {
  5818. int err = 0, buf_size = 0;
  5819. char strpid[PID_SIZE];
  5820. char cur_comm[TASK_COMM_LEN];
  5821. memcpy(cur_comm, current->comm, TASK_COMM_LEN);
  5822. cur_comm[TASK_COMM_LEN-1] = '\0';
  5823. fl->tgid = current->tgid;
  5824. fl->tgid_frpc = get_unique_hlos_process_id(cid);
  5825. VERIFY(err, fl->tgid_frpc != -1);
  5826. if (err) {
  5827. ADSPRPC_ERR("too many fastrpc clients, max %u allowed\n", MAX_FRPC_TGID);
  5828. err = -EUSERS;
  5829. goto bail;
  5830. }
  5831. /*
  5832. * Third-party apps don't have permission to open the fastrpc device, so
  5833. * it is opened on their behalf by DSP HAL. This is detected by
  5834. * comparing current PID with the one stored during device open.
  5835. */
  5836. if (current->tgid != fl->tgid_open)
  5837. fl->untrusted_process = true;
  5838. snprintf(strpid, PID_SIZE, "%d", current->pid);
  5839. if (debugfs_root) {
  5840. VERIFY(err, VALID_FASTRPC_CID(cid));
  5841. if (err) {
  5842. err = -ECHRNG;
  5843. goto bail;
  5844. }
  5845. buf_size = strlen(cur_comm) + strlen("_") + strlen(strpid)
  5846. + strlen("_") + strlen(__TOSTR__(NUM_CHANNELS)) + 1;
  5847. spin_lock(&fl->hlock);
  5848. if (fl->debug_buf_alloced_attempted) {
  5849. spin_unlock(&fl->hlock);
  5850. return err;
  5851. }
  5852. fl->debug_buf_alloced_attempted = 1;
  5853. spin_unlock(&fl->hlock);
  5854. fl->debug_buf = kzalloc(buf_size, GFP_KERNEL);
  5855. if (!fl->debug_buf) {
  5856. err = -ENOMEM;
  5857. return err;
  5858. }
  5859. /* Use HLOS PID, unique fastrpc PID, CID in debugfs filename,
  5860. * for better ability to debug
  5861. */
  5862. snprintf(fl->debug_buf, buf_size, "%.10s%s%d%s%d%s%d",
  5863. cur_comm, "_", current->pid, "_", fl->tgid_frpc, "_", cid);
  5864. fl->debugfs_file = debugfs_create_file(fl->debug_buf, 0644,
  5865. debugfs_root, fl, &debugfs_fops);
  5866. if (IS_ERR_OR_NULL(fl->debugfs_file)) {
  5867. pr_warn("Error: %s: %s: failed to create debugfs file %s\n",
  5868. cur_comm, __func__, fl->debug_buf);
  5869. fl->debugfs_file = NULL;
  5870. }
  5871. kfree(fl->debug_buf);
  5872. fl->debug_buf = NULL;
  5873. }
  5874. bail:
  5875. return err;
  5876. }
  5877. int fastrpc_get_info(struct fastrpc_file *fl, uint32_t *info)
  5878. {
  5879. int err = 0;
  5880. uint32_t cid = *info;
  5881. struct fastrpc_apps *me = &gfa;
  5882. VERIFY(err, fl != NULL);
  5883. if (err) {
  5884. err = -EBADF;
  5885. goto bail;
  5886. }
  5887. spin_lock(&fl->hlock);
  5888. if (fl->set_session_info) {
  5889. spin_unlock(&fl->hlock);
  5890. ADSPRPC_ERR("Set session info invoked multiple times\n");
  5891. err = -EBADR;
  5892. goto bail;
  5893. }
  5894. // Set set_session_info to true
  5895. fl->set_session_info = true;
  5896. spin_unlock(&fl->hlock);
  5897. VERIFY(err, VALID_FASTRPC_CID(cid));
  5898. if (err) {
  5899. err = -ECHRNG;
  5900. goto bail;
  5901. }
  5902. fastrpc_get_process_gids(&fl->gidlist);
  5903. err = fastrpc_set_process_info(fl, cid);
  5904. if (err)
  5905. goto bail;
  5906. if (fl->cid == -1) {
  5907. struct fastrpc_channel_ctx *chan = NULL;
  5908. chan = &me->channel[cid];
  5909. /* Check to see if the device node is non-secure */
  5910. if (fl->dev_minor == MINOR_NUM_DEV) {
  5911. /*
  5912. * If an app is trying to offload to a secure remote
  5913. * channel by opening the non-secure device node, allow
  5914. * the access if the subsystem supports unsigned
  5915. * offload. Untrusted apps will be restricted from
  5916. * offloading to signed PD using DSP HAL.
  5917. */
  5918. if (chan->secure == SECURE_CHANNEL
  5919. && !chan->unsigned_support) {
  5920. ADSPRPC_ERR(
  5921. "cannot use domain %d with non-secure device\n",
  5922. cid);
  5923. err = -EACCES;
  5924. goto bail;
  5925. }
  5926. }
  5927. fl->cid = cid;
  5928. fl->ssrcount = fl->apps->channel[cid].ssrcount;
  5929. mutex_lock(&fl->apps->channel[cid].smd_mutex);
  5930. err = fastrpc_session_alloc_locked(&fl->apps->channel[cid],
  5931. 0, fl->sharedcb, fl->pd_type, &fl->sctx);
  5932. mutex_unlock(&fl->apps->channel[cid].smd_mutex);
  5933. if (err == -EUSERS) {
  5934. ADSPRPC_WARN(
  5935. "max concurrent sessions limit (%d) already reached on %s err %d\n",
  5936. chan->sesscount, chan->subsys, err);
  5937. }
  5938. if (err)
  5939. goto bail;
  5940. }
  5941. VERIFY(err, fl->sctx != NULL);
  5942. if (err) {
  5943. err = -EBADR;
  5944. goto bail;
  5945. }
  5946. *info = (fl->sctx->smmu.enabled ? 1 : 0);
  5947. bail:
  5948. return err;
  5949. }
  5950. static int fastrpc_manage_poll_mode(struct fastrpc_file *fl, uint32_t enable, uint32_t timeout)
  5951. {
  5952. int err = 0;
  5953. const unsigned int MAX_POLL_TIMEOUT_US = 10000;
  5954. if ((fl->cid != CDSP_DOMAIN_ID) || (fl->proc_flags != FASTRPC_INIT_CREATE)) {
  5955. err = -EPERM;
  5956. ADSPRPC_ERR("flags %d, cid %d, poll mode allowed only for dynamic CDSP process\n",
  5957. fl->proc_flags, fl->cid);
  5958. goto bail;
  5959. }
  5960. if (timeout > MAX_POLL_TIMEOUT_US) {
  5961. err = -EBADMSG;
  5962. ADSPRPC_ERR("poll timeout %u is greater than max allowed value %u\n",
  5963. timeout, MAX_POLL_TIMEOUT_US);
  5964. goto bail;
  5965. }
  5966. spin_lock(&fl->hlock);
  5967. if (enable) {
  5968. fl->poll_mode = true;
  5969. fl->poll_timeout = timeout;
  5970. } else {
  5971. fl->poll_mode = false;
  5972. fl->poll_timeout = 0;
  5973. }
  5974. spin_unlock(&fl->hlock);
  5975. ADSPRPC_INFO("updated poll mode to %d, timeout %u\n", enable, timeout);
  5976. bail:
  5977. return err;
  5978. }
  5979. int fastrpc_internal_control(struct fastrpc_file *fl,
  5980. struct fastrpc_ioctl_control *cp)
  5981. {
  5982. int err = 0;
  5983. unsigned int latency;
  5984. struct fastrpc_apps *me = &gfa;
  5985. unsigned int cpu;
  5986. unsigned long flags = 0;
  5987. VERIFY(err, !IS_ERR_OR_NULL(fl) && !IS_ERR_OR_NULL(fl->apps));
  5988. if (err) {
  5989. err = -EBADF;
  5990. goto bail;
  5991. }
  5992. VERIFY(err, !IS_ERR_OR_NULL(cp));
  5993. if (err) {
  5994. err = -EINVAL;
  5995. goto bail;
  5996. }
  5997. switch (cp->req) {
  5998. case FASTRPC_CONTROL_LATENCY:
  5999. latency = cp->lp.enable == FASTRPC_LATENCY_CTRL_ENB ?
  6000. fl->apps->latency : PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
  6001. VERIFY(err, latency != 0);
  6002. if (err) {
  6003. err = -EINVAL;
  6004. goto bail;
  6005. }
  6006. VERIFY(err, (me->lowest_capacity_core_count && fl->dev_pm_qos_req));
  6007. if (err) {
  6008. ADSPRPC_INFO("Skipping PM QoS latency voting, core count: %u\n",
  6009. me->lowest_capacity_core_count);
  6010. err = -EINVAL;
  6011. goto bail;
  6012. }
  6013. /*
  6014. * Add voting request for all possible cores corresponding to cluster
  6015. * id 0. If DT property 'qcom,single-core-latency-vote' is enabled
  6016. * then add voting request for only one core of cluster id 0.
  6017. */
  6018. for (cpu = 0; cpu < me->lowest_capacity_core_count; cpu++) {
  6019. if (!fl->qos_request) {
  6020. err = dev_pm_qos_add_request(
  6021. get_cpu_device(cpu),
  6022. &fl->dev_pm_qos_req[cpu],
  6023. DEV_PM_QOS_RESUME_LATENCY,
  6024. latency);
  6025. } else {
  6026. err = dev_pm_qos_update_request(
  6027. &fl->dev_pm_qos_req[cpu],
  6028. latency);
  6029. }
  6030. /* PM QoS request APIs return 0 or 1 on success */
  6031. if (err < 0) {
  6032. ADSPRPC_WARN("QoS with lat %u failed for CPU %d, err %d, req %d\n",
  6033. latency, cpu, err, fl->qos_request);
  6034. break;
  6035. }
  6036. }
  6037. if (err >= 0) {
  6038. fl->qos_request = 1;
  6039. err = 0;
  6040. }
  6041. /* Ensure CPU feature map updated to DSP for early WakeUp */
  6042. fastrpc_send_cpuinfo_to_dsp(fl);
  6043. break;
  6044. case FASTRPC_CONTROL_KALLOC:
  6045. cp->kalloc.kalloc_support = 1;
  6046. break;
  6047. case FASTRPC_CONTROL_WAKELOCK:
  6048. if (fl->dev_minor != MINOR_NUM_SECURE_DEV) {
  6049. ADSPRPC_ERR(
  6050. "PM voting not allowed for non-secure device node %d\n",
  6051. fl->dev_minor);
  6052. err = -EPERM;
  6053. goto bail;
  6054. }
  6055. fl->wake_enable = cp->wp.enable;
  6056. break;
  6057. case FASTRPC_CONTROL_PM:
  6058. if (!fl->wake_enable) {
  6059. /* Kernel PM voting not requested by this application */
  6060. err = -EACCES;
  6061. goto bail;
  6062. }
  6063. if (cp->pm.timeout > MAX_PM_TIMEOUT_MS)
  6064. fl->ws_timeout = MAX_PM_TIMEOUT_MS;
  6065. else
  6066. fl->ws_timeout = cp->pm.timeout;
  6067. VERIFY(err, VALID_FASTRPC_CID(fl->cid));
  6068. if (err) {
  6069. err = -ECHRNG;
  6070. goto bail;
  6071. }
  6072. fastrpc_pm_awake(fl, gcinfo[fl->cid].secure);
  6073. break;
  6074. case FASTRPC_CONTROL_DSPPROCESS_CLEAN:
  6075. (void)fastrpc_release_current_dsp_process(fl);
  6076. fastrpc_queue_pd_status(fl, fl->cid, FASTRPC_USER_PD_FORCE_KILL, fl->sessionid);
  6077. break;
  6078. case FASTRPC_CONTROL_RPC_POLL:
  6079. err = fastrpc_manage_poll_mode(fl, cp->lp.enable, cp->lp.latency);
  6080. if (err)
  6081. goto bail;
  6082. break;
  6083. case FASTRPC_CONTROL_SMMU:
  6084. fl->sharedcb = cp->smmu.sharedcb;
  6085. break;
  6086. case FASTRPC_CONTROL_ASYNC_WAKE:
  6087. fl->exit_async = true;
  6088. spin_lock_irqsave(&fl->aqlock, flags);
  6089. atomic_add(1, &fl->async_queue_job_count);
  6090. wake_up_interruptible(&fl->async_wait_queue);
  6091. spin_unlock_irqrestore(&fl->aqlock, flags);
  6092. break;
  6093. case FASTRPC_CONTROL_NOTIF_WAKE:
  6094. fl->exit_notif = true;
  6095. spin_lock_irqsave(&fl->proc_state_notif.nqlock, flags);
  6096. atomic_add(1, &fl->proc_state_notif.notif_queue_count);
  6097. wake_up_interruptible(&fl->proc_state_notif.notif_wait_queue);
  6098. spin_unlock_irqrestore(&fl->proc_state_notif.nqlock, flags);
  6099. break;
  6100. default:
  6101. err = -EBADRQC;
  6102. break;
  6103. }
  6104. bail:
  6105. return err;
  6106. }
  6107. /* Wait for PD to be up before audio or sensors daemons try connecting */
  6108. static int fastrpc_check_pd_status(struct fastrpc_file *fl, char *sloc_name)
  6109. {
  6110. int err = 0, session = -1, cid = -1;
  6111. struct fastrpc_apps *me = &gfa;
  6112. if (fl->servloc_name && sloc_name
  6113. && !strcmp(fl->servloc_name, sloc_name)) {
  6114. err = fastrpc_get_spd_session(sloc_name, &session, &cid);
  6115. if (err || cid != fl->cid)
  6116. goto bail;
  6117. #if IS_ENABLED(CONFIG_QCOM_PDR_HELPERS)
  6118. if (!strcmp(fl->servloc_name,
  6119. AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME) || !strcmp(fl->servloc_name,
  6120. SENSORS_PDR_ADSP_SERVICE_LOCATION_CLIENT_NAME) ||
  6121. !strcmp(fl->servloc_name,
  6122. SENSORS_PDR_SLPI_SERVICE_LOCATION_CLIENT_NAME)) {
  6123. err = wait_event_interruptible(
  6124. me->channel[cid].spd[session].wait_for_pdup,
  6125. atomic_read(&me->channel[cid].spd[session].ispdup));
  6126. goto bail;
  6127. }
  6128. #else
  6129. (void)me;
  6130. #endif
  6131. }
  6132. bail:
  6133. return err;
  6134. }
  6135. int fastrpc_setmode(unsigned long ioctl_param,
  6136. struct fastrpc_file *fl)
  6137. {
  6138. int err = 0;
  6139. switch ((uint32_t)ioctl_param) {
  6140. case FASTRPC_MODE_PARALLEL:
  6141. case FASTRPC_MODE_SERIAL:
  6142. fl->mode = (uint32_t)ioctl_param;
  6143. break;
  6144. case FASTRPC_MODE_PROFILE:
  6145. fl->profile = (uint32_t)ioctl_param;
  6146. break;
  6147. case FASTRPC_MODE_SESSION:
  6148. if (fl->untrusted_process) {
  6149. err = -EPERM;
  6150. ADSPRPC_ERR(
  6151. "multiple sessions not allowed for untrusted apps\n");
  6152. goto bail;
  6153. }
  6154. if (!fl->multi_session_support)
  6155. fl->sessionid = 1;
  6156. break;
  6157. default:
  6158. err = -ENOTTY;
  6159. break;
  6160. }
  6161. bail:
  6162. return err;
  6163. }
  6164. int fastrpc_control(struct fastrpc_ioctl_control *cp,
  6165. void *param, struct fastrpc_file *fl)
  6166. {
  6167. int err = 0;
  6168. K_COPY_FROM_USER(err, 0, cp, param,
  6169. sizeof(*cp));
  6170. if (err) {
  6171. err = -EFAULT;
  6172. goto bail;
  6173. }
  6174. VERIFY(err, 0 == (err = fastrpc_internal_control(fl, cp)));
  6175. if (err)
  6176. goto bail;
  6177. if (cp->req == FASTRPC_CONTROL_KALLOC) {
  6178. K_COPY_TO_USER(err, 0, param, cp, sizeof(*cp));
  6179. if (err) {
  6180. err = -EFAULT;
  6181. goto bail;
  6182. }
  6183. }
  6184. bail:
  6185. return err;
  6186. }
  6187. static int fastrpc_get_dsp_info(
  6188. struct fastrpc_ioctl_capability *cap,
  6189. void *param, struct fastrpc_file *fl)
  6190. {
  6191. int err = 0;
  6192. K_COPY_FROM_USER(err, 0, cap, param,
  6193. sizeof(struct fastrpc_ioctl_capability));
  6194. VERIFY(err, cap->domain < NUM_CHANNELS);
  6195. if (err) {
  6196. err = -ECHRNG;
  6197. goto bail;
  6198. }
  6199. cap->capability = 0;
  6200. err = fastrpc_get_info_from_kernel(cap, fl);
  6201. if (err)
  6202. goto bail;
  6203. K_COPY_TO_USER(err, 0, &((struct fastrpc_ioctl_capability *)
  6204. param)->capability, &cap->capability, sizeof(cap->capability));
  6205. bail:
  6206. return err;
  6207. }
  6208. int fastrpc_dspsignal_signal(struct fastrpc_file *fl,
  6209. struct fastrpc_ioctl_dspsignal_signal *sig)
  6210. {
  6211. int err = 0, cid = -1;
  6212. struct fastrpc_channel_ctx *channel_ctx = NULL;
  6213. uint64_t msg = 0;
  6214. // We don't check if the signal has even been allocated since we don't
  6215. // track outgoing signals in the driver. The userspace library does a
  6216. // basic sanity check and any security validation needs to be done by
  6217. // the recipient.
  6218. DSPSIGNAL_VERBOSE("Send signal PID %u, unique fastrpc pid %u signal %u\n",
  6219. (unsigned int)fl->tgid, (unsigned int)fl->tgid_frpc,
  6220. (unsigned int)sig->signal_id);
  6221. VERIFY(err, sig->signal_id < DSPSIGNAL_NUM_SIGNALS);
  6222. if (err) {
  6223. ADSPRPC_ERR("Sending bad signal %u for PID %u",
  6224. sig->signal_id, (unsigned int)fl->tgid);
  6225. err = -EBADR;
  6226. goto bail;
  6227. }
  6228. cid = fl->cid;
  6229. VERIFY(err, VALID_FASTRPC_CID(cid) && fl->sctx != NULL);
  6230. if (err) {
  6231. err = -EBADR;
  6232. goto bail;
  6233. }
  6234. channel_ctx = &fl->apps->channel[cid];
  6235. mutex_lock(&channel_ctx->smd_mutex);
  6236. if (fl->ssrcount != channel_ctx->ssrcount) {
  6237. err = -ECONNRESET;
  6238. mutex_unlock(&channel_ctx->smd_mutex);
  6239. goto bail;
  6240. }
  6241. /* Use unique fastrpc pid, to signal DSP process */
  6242. msg = (((uint64_t)fl->tgid_frpc) << 32) | ((uint64_t)sig->signal_id);
  6243. err = fastrpc_transport_send(cid, (void *)&msg, sizeof(msg), fl->tvm_remote_domain);
  6244. mutex_unlock(&channel_ctx->smd_mutex);
  6245. bail:
  6246. return err;
  6247. }
  6248. int fastrpc_dspsignal_wait(struct fastrpc_file *fl,
  6249. struct fastrpc_ioctl_dspsignal_wait *wait)
  6250. {
  6251. int err = 0, cid = -1;
  6252. unsigned long timeout = usecs_to_jiffies(wait->timeout_usec);
  6253. uint32_t signal_id = wait->signal_id;
  6254. struct fastrpc_dspsignal *s = NULL;
  6255. long ret = 0;
  6256. unsigned long irq_flags = 0;
  6257. DSPSIGNAL_VERBOSE("Wait for signal %u\n", signal_id);
  6258. VERIFY(err, signal_id < DSPSIGNAL_NUM_SIGNALS);
  6259. if (err) {
  6260. ADSPRPC_ERR("Waiting on bad signal %u", signal_id);
  6261. err = -EINVAL;
  6262. goto bail;
  6263. }
  6264. cid = fl->cid;
  6265. VERIFY(err, VALID_FASTRPC_CID(cid) && fl->sctx != NULL);
  6266. if (err) {
  6267. err = -EBADR;
  6268. goto bail;
  6269. }
  6270. spin_lock_irqsave(&fl->dspsignals_lock, irq_flags);
  6271. if (fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE] != NULL) {
  6272. struct fastrpc_dspsignal *group =
  6273. fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE];
  6274. s = &group[signal_id % DSPSIGNAL_GROUP_SIZE];
  6275. }
  6276. if ((s == NULL) || (s->state == DSPSIGNAL_STATE_UNUSED)) {
  6277. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6278. ADSPRPC_ERR("Unknown signal id %u\n", signal_id);
  6279. err = -ENOENT;
  6280. goto bail;
  6281. }
  6282. if (s->state != DSPSIGNAL_STATE_PENDING) {
  6283. if ((s->state == DSPSIGNAL_STATE_CANCELED) || (s->state == DSPSIGNAL_STATE_UNUSED))
  6284. err = -EINTR;
  6285. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6286. DSPSIGNAL_VERBOSE("Signal %u in state %u, complete wait immediately",
  6287. signal_id, s->state);
  6288. goto bail;
  6289. }
  6290. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6291. if (timeout != 0xffffffff)
  6292. ret = wait_for_completion_interruptible_timeout(&s->comp, timeout);
  6293. else
  6294. ret = wait_for_completion_interruptible(&s->comp);
  6295. if (ret == 0) {
  6296. DSPSIGNAL_VERBOSE("Wait for signal %u timed out\n", signal_id);
  6297. err = -ETIMEDOUT;
  6298. goto bail;
  6299. } else if (ret < 0) {
  6300. ADSPRPC_ERR("Wait for signal %u failed %d\n", signal_id, (int)ret);
  6301. err = ret;
  6302. goto bail;
  6303. }
  6304. spin_lock_irqsave(&fl->dspsignals_lock, irq_flags);
  6305. if (s->state == DSPSIGNAL_STATE_SIGNALED) {
  6306. s->state = DSPSIGNAL_STATE_PENDING;
  6307. DSPSIGNAL_VERBOSE("Signal %u completed\n", signal_id);
  6308. } else if ((s->state == DSPSIGNAL_STATE_CANCELED) || (s->state == DSPSIGNAL_STATE_UNUSED)) {
  6309. DSPSIGNAL_VERBOSE("Signal %u cancelled or destroyed\n", signal_id);
  6310. err = -EINTR;
  6311. }
  6312. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6313. bail:
  6314. return err;
  6315. }
  6316. int fastrpc_dspsignal_create(struct fastrpc_file *fl,
  6317. struct fastrpc_ioctl_dspsignal_create *create)
  6318. {
  6319. int err = 0, cid = -1;
  6320. uint32_t signal_id = create->signal_id;
  6321. struct fastrpc_dspsignal *group, *sig;
  6322. unsigned long irq_flags = 0;
  6323. VERIFY(err, signal_id < DSPSIGNAL_NUM_SIGNALS);
  6324. if (err) {
  6325. err = -EINVAL;
  6326. goto bail;
  6327. }
  6328. cid = fl->cid;
  6329. VERIFY(err, VALID_FASTRPC_CID(cid) && fl->sctx != NULL);
  6330. if (err) {
  6331. err = -EBADR;
  6332. goto bail;
  6333. }
  6334. // Use a separate mutex for creating signals. This avoids holding on
  6335. // to a spinlock if we need to allocate a whole group of signals. The
  6336. // mutex ensures nobody else will allocate the same group.
  6337. mutex_lock(&fl->signal_create_mutex);
  6338. spin_lock_irqsave(&fl->dspsignals_lock, irq_flags);
  6339. group = fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE];
  6340. if (group == NULL) {
  6341. int i;
  6342. // Release the spinlock while we allocate a new group but take
  6343. // it back before taking the group into use. No other code
  6344. // allocates groups so the mutex is sufficient.
  6345. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6346. VERIFY(err, (group = kzalloc(DSPSIGNAL_GROUP_SIZE * sizeof(*group),
  6347. GFP_KERNEL)) != NULL);
  6348. if (err) {
  6349. ADSPRPC_ERR("Unable to allocate signal group\n");
  6350. err = -ENOMEM;
  6351. mutex_unlock(&fl->signal_create_mutex);
  6352. goto bail;
  6353. }
  6354. for (i = 0; i < DSPSIGNAL_GROUP_SIZE; i++) {
  6355. sig = &group[i];
  6356. init_completion(&sig->comp);
  6357. sig->state = DSPSIGNAL_STATE_UNUSED;
  6358. }
  6359. spin_lock_irqsave(&fl->dspsignals_lock, irq_flags);
  6360. fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE] = group;
  6361. }
  6362. sig = &group[signal_id % DSPSIGNAL_GROUP_SIZE];
  6363. if (sig->state != DSPSIGNAL_STATE_UNUSED) {
  6364. err = -EBUSY;
  6365. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6366. mutex_unlock(&fl->signal_create_mutex);
  6367. ADSPRPC_ERR("Attempting to create signal %u already in use (state %u)\n",
  6368. signal_id, sig->state);
  6369. goto bail;
  6370. }
  6371. sig->state = DSPSIGNAL_STATE_PENDING;
  6372. reinit_completion(&sig->comp);
  6373. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6374. mutex_unlock(&fl->signal_create_mutex);
  6375. DSPSIGNAL_VERBOSE("Signal %u created\n", signal_id);
  6376. bail:
  6377. return err;
  6378. }
  6379. int fastrpc_dspsignal_destroy(struct fastrpc_file *fl,
  6380. struct fastrpc_ioctl_dspsignal_destroy *destroy)
  6381. {
  6382. int err = 0, cid = -1;
  6383. uint32_t signal_id = destroy->signal_id;
  6384. struct fastrpc_dspsignal *s = NULL;
  6385. unsigned long irq_flags = 0;
  6386. DSPSIGNAL_VERBOSE("Destroy signal %u\n", signal_id);
  6387. VERIFY(err, signal_id < DSPSIGNAL_NUM_SIGNALS);
  6388. if (err) {
  6389. err = -EINVAL;
  6390. goto bail;
  6391. }
  6392. cid = fl->cid;
  6393. VERIFY(err, VALID_FASTRPC_CID(cid) && fl->sctx != NULL);
  6394. if (err) {
  6395. err = -EBADR;
  6396. goto bail;
  6397. }
  6398. spin_lock_irqsave(&fl->dspsignals_lock, irq_flags);
  6399. if (fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE] != NULL) {
  6400. struct fastrpc_dspsignal *group =
  6401. fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE];
  6402. s = &group[signal_id % DSPSIGNAL_GROUP_SIZE];
  6403. }
  6404. if ((s == NULL) || (s->state == DSPSIGNAL_STATE_UNUSED)) {
  6405. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6406. ADSPRPC_ERR("Attempting to destroy unused signal %u\n", signal_id);
  6407. err = -ENOENT;
  6408. goto bail;
  6409. }
  6410. s->state = DSPSIGNAL_STATE_UNUSED;
  6411. complete_all(&s->comp);
  6412. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6413. DSPSIGNAL_VERBOSE("Signal %u destroyed\n", signal_id);
  6414. bail:
  6415. return err;
  6416. }
  6417. int fastrpc_dspsignal_cancel_wait(struct fastrpc_file *fl,
  6418. struct fastrpc_ioctl_dspsignal_cancel_wait *cancel)
  6419. {
  6420. int err = 0, cid = -1;
  6421. uint32_t signal_id = cancel->signal_id;
  6422. struct fastrpc_dspsignal *s = NULL;
  6423. unsigned long irq_flags = 0;
  6424. DSPSIGNAL_VERBOSE("Cancel wait for signal %u\n", signal_id);
  6425. VERIFY(err, signal_id < DSPSIGNAL_NUM_SIGNALS);
  6426. if (err) {
  6427. err = -EINVAL;
  6428. goto bail;
  6429. }
  6430. cid = fl->cid;
  6431. VERIFY(err, VALID_FASTRPC_CID(cid) && fl->sctx != NULL);
  6432. if (err) {
  6433. err = -EBADR;
  6434. goto bail;
  6435. }
  6436. spin_lock_irqsave(&fl->dspsignals_lock, irq_flags);
  6437. if (fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE] != NULL) {
  6438. struct fastrpc_dspsignal *group =
  6439. fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE];
  6440. s = &group[signal_id % DSPSIGNAL_GROUP_SIZE];
  6441. }
  6442. if ((s == NULL) || (s->state == DSPSIGNAL_STATE_UNUSED)) {
  6443. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6444. ADSPRPC_ERR("Attempting to cancel unused signal %u\n", signal_id);
  6445. err = -ENOENT;
  6446. goto bail;
  6447. }
  6448. if (s->state != DSPSIGNAL_STATE_CANCELED) {
  6449. s->state = DSPSIGNAL_STATE_CANCELED;
  6450. complete_all(&s->comp);
  6451. }
  6452. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6453. DSPSIGNAL_VERBOSE("Signal %u cancelled\n", signal_id);
  6454. bail:
  6455. return err;
  6456. }
  6457. static inline int fastrpc_mmap_device_ioctl(struct fastrpc_file *fl,
  6458. unsigned int ioctl_num, union fastrpc_ioctl_param *p,
  6459. void *param)
  6460. {
  6461. union {
  6462. struct fastrpc_ioctl_mmap mmap;
  6463. struct fastrpc_ioctl_munmap munmap;
  6464. } i;
  6465. int err = 0;
  6466. switch (ioctl_num) {
  6467. case FASTRPC_IOCTL_MEM_MAP:
  6468. K_COPY_FROM_USER(err, 0, &p->mem_map, param,
  6469. sizeof(p->mem_map));
  6470. if (err) {
  6471. err = -EFAULT;
  6472. goto bail;
  6473. }
  6474. VERIFY(err, 0 == (err = fastrpc_internal_mem_map(fl,
  6475. &p->mem_map)));
  6476. if (err)
  6477. goto bail;
  6478. K_COPY_TO_USER(err, 0, param, &p->mem_map, sizeof(p->mem_map));
  6479. if (err) {
  6480. err = -EFAULT;
  6481. goto bail;
  6482. }
  6483. break;
  6484. case FASTRPC_IOCTL_MEM_UNMAP:
  6485. K_COPY_FROM_USER(err, 0, &p->mem_unmap, param,
  6486. sizeof(p->mem_unmap));
  6487. if (err) {
  6488. err = -EFAULT;
  6489. goto bail;
  6490. }
  6491. VERIFY(err, 0 == (err = fastrpc_internal_mem_unmap(fl,
  6492. &p->mem_unmap)));
  6493. if (err)
  6494. goto bail;
  6495. K_COPY_TO_USER(err, 0, param, &p->mem_unmap,
  6496. sizeof(p->mem_unmap));
  6497. if (err) {
  6498. err = -EFAULT;
  6499. goto bail;
  6500. }
  6501. break;
  6502. case FASTRPC_IOCTL_MMAP:
  6503. K_COPY_FROM_USER(err, 0, &p->mmap, param,
  6504. sizeof(p->mmap));
  6505. if (err) {
  6506. err = -EFAULT;
  6507. goto bail;
  6508. }
  6509. VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &p->mmap)));
  6510. if (err)
  6511. goto bail;
  6512. K_COPY_TO_USER(err, 0, param, &p->mmap, sizeof(p->mmap));
  6513. if (err) {
  6514. err = -EFAULT;
  6515. goto bail;
  6516. }
  6517. break;
  6518. case FASTRPC_IOCTL_MUNMAP:
  6519. K_COPY_FROM_USER(err, 0, &p->munmap, param,
  6520. sizeof(p->munmap));
  6521. if (err) {
  6522. err = -EFAULT;
  6523. goto bail;
  6524. }
  6525. VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl,
  6526. &p->munmap)));
  6527. if (err)
  6528. goto bail;
  6529. break;
  6530. case FASTRPC_IOCTL_MMAP_64:
  6531. K_COPY_FROM_USER(err, 0, &p->mmap64, param,
  6532. sizeof(p->mmap64));
  6533. if (err) {
  6534. err = -EFAULT;
  6535. goto bail;
  6536. }
  6537. get_fastrpc_ioctl_mmap_64(&p->mmap64, &i.mmap);
  6538. VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &i.mmap)));
  6539. if (err)
  6540. goto bail;
  6541. put_fastrpc_ioctl_mmap_64(&p->mmap64, &i.mmap);
  6542. K_COPY_TO_USER(err, 0, param, &p->mmap64, sizeof(p->mmap64));
  6543. if (err) {
  6544. err = -EFAULT;
  6545. goto bail;
  6546. }
  6547. break;
  6548. case FASTRPC_IOCTL_MUNMAP_64:
  6549. K_COPY_FROM_USER(err, 0, &p->munmap64, param,
  6550. sizeof(p->munmap64));
  6551. if (err) {
  6552. err = -EFAULT;
  6553. goto bail;
  6554. }
  6555. get_fastrpc_ioctl_munmap_64(&p->munmap64, &i.munmap);
  6556. VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl,
  6557. &i.munmap)));
  6558. if (err)
  6559. goto bail;
  6560. break;
  6561. case FASTRPC_IOCTL_MUNMAP_FD:
  6562. K_COPY_FROM_USER(err, 0, &p->munmap_fd, param,
  6563. sizeof(p->munmap_fd));
  6564. if (err) {
  6565. err = -EFAULT;
  6566. goto bail;
  6567. }
  6568. VERIFY(err, 0 == (err = fastrpc_internal_munmap_fd(fl,
  6569. &p->munmap_fd)));
  6570. if (err)
  6571. goto bail;
  6572. break;
  6573. default:
  6574. err = -ENOTTY;
  6575. pr_info("bad ioctl: %d\n", ioctl_num);
  6576. break;
  6577. }
  6578. bail:
  6579. return err;
  6580. }
  6581. static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num,
  6582. unsigned long ioctl_param)
  6583. {
  6584. union fastrpc_ioctl_param p;
  6585. void *param = (char *)ioctl_param;
  6586. struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data;
  6587. int size = 0, err = 0;
  6588. uint32_t info;
  6589. p.inv.fds = NULL;
  6590. p.inv.attrs = NULL;
  6591. p.inv.crc = NULL;
  6592. p.inv.perf_kernel = NULL;
  6593. p.inv.perf_dsp = NULL;
  6594. p.inv.job = NULL;
  6595. spin_lock(&fl->hlock);
  6596. if (fl->file_close >= FASTRPC_PROCESS_EXIT_START) {
  6597. err = -ESHUTDOWN;
  6598. pr_warn("adsprpc: fastrpc_device_release is happening, So not sending any new requests to DSP\n");
  6599. spin_unlock(&fl->hlock);
  6600. goto bail;
  6601. }
  6602. spin_unlock(&fl->hlock);
  6603. switch (ioctl_num) {
  6604. case FASTRPC_IOCTL_INVOKE:
  6605. size = sizeof(struct fastrpc_ioctl_invoke);
  6606. fallthrough;
  6607. case FASTRPC_IOCTL_INVOKE_FD:
  6608. if (!size)
  6609. size = sizeof(struct fastrpc_ioctl_invoke_fd);
  6610. fallthrough;
  6611. case FASTRPC_IOCTL_INVOKE_ATTRS:
  6612. if (!size)
  6613. size = sizeof(struct fastrpc_ioctl_invoke_attrs);
  6614. fallthrough;
  6615. case FASTRPC_IOCTL_INVOKE_CRC:
  6616. if (!size)
  6617. size = sizeof(struct fastrpc_ioctl_invoke_crc);
  6618. fallthrough;
  6619. case FASTRPC_IOCTL_INVOKE_PERF:
  6620. if (!size)
  6621. size = sizeof(struct fastrpc_ioctl_invoke_perf);
  6622. trace_fastrpc_msg("invoke: begin");
  6623. K_COPY_FROM_USER(err, 0, &p.inv, param, size);
  6624. if (err) {
  6625. err = -EFAULT;
  6626. goto bail;
  6627. }
  6628. VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, fl->mode,
  6629. USER_MSG, &p.inv)));
  6630. trace_fastrpc_msg("invoke: end");
  6631. if (err)
  6632. goto bail;
  6633. break;
  6634. case FASTRPC_IOCTL_INVOKE2:
  6635. K_COPY_FROM_USER(err, 0, &p.inv2, param,
  6636. sizeof(struct fastrpc_ioctl_invoke2));
  6637. if (err) {
  6638. err = -EFAULT;
  6639. goto bail;
  6640. }
  6641. VERIFY(err, 0 == (err = fastrpc_internal_invoke2(fl, &p.inv2)));
  6642. if (err)
  6643. goto bail;
  6644. break;
  6645. case FASTRPC_IOCTL_SETMODE:
  6646. err = fastrpc_setmode(ioctl_param, fl);
  6647. break;
  6648. case FASTRPC_IOCTL_CONTROL:
  6649. err = fastrpc_control(&p.cp, param, fl);
  6650. break;
  6651. case FASTRPC_IOCTL_GETINFO:
  6652. K_COPY_FROM_USER(err, 0, &info, param, sizeof(info));
  6653. if (err) {
  6654. err = -EFAULT;
  6655. goto bail;
  6656. }
  6657. VERIFY(err, 0 == (err = fastrpc_get_info(fl, &info)));
  6658. if (err)
  6659. goto bail;
  6660. K_COPY_TO_USER(err, 0, param, &info, sizeof(info));
  6661. if (err) {
  6662. err = -EFAULT;
  6663. goto bail;
  6664. }
  6665. break;
  6666. case FASTRPC_IOCTL_INIT:
  6667. p.init.attrs = 0;
  6668. p.init.siglen = 0;
  6669. size = sizeof(struct fastrpc_ioctl_init);
  6670. fallthrough;
  6671. case FASTRPC_IOCTL_INIT_ATTRS:
  6672. if (!size)
  6673. size = sizeof(struct fastrpc_ioctl_init_attrs);
  6674. K_COPY_FROM_USER(err, 0, &p.init, param, size);
  6675. if (err) {
  6676. err = -EFAULT;
  6677. goto bail;
  6678. }
  6679. VERIFY(err, 0 == (err = fastrpc_init_process(fl, &p.init)));
  6680. if (err)
  6681. goto bail;
  6682. break;
  6683. case FASTRPC_IOCTL_GET_DSP_INFO:
  6684. err = fastrpc_get_dsp_info(&p.cap, param, fl);
  6685. break;
  6686. case FASTRPC_IOCTL_MEM_MAP:
  6687. fallthrough;
  6688. case FASTRPC_IOCTL_MEM_UNMAP:
  6689. fallthrough;
  6690. case FASTRPC_IOCTL_MMAP:
  6691. fallthrough;
  6692. case FASTRPC_IOCTL_MUNMAP:
  6693. fallthrough;
  6694. case FASTRPC_IOCTL_MMAP_64:
  6695. fallthrough;
  6696. case FASTRPC_IOCTL_MUNMAP_64:
  6697. fallthrough;
  6698. case FASTRPC_IOCTL_MUNMAP_FD:
  6699. err = fastrpc_mmap_device_ioctl(fl, ioctl_num, &p, param);
  6700. break;
  6701. case FASTRPC_IOCTL_DSPSIGNAL_SIGNAL:
  6702. K_COPY_FROM_USER(err, 0, &p.sig, param,
  6703. sizeof(struct fastrpc_ioctl_dspsignal_signal));
  6704. if (err) {
  6705. err = -EFAULT;
  6706. goto bail;
  6707. }
  6708. VERIFY(err, 0 == (err = fastrpc_dspsignal_signal(fl, &p.sig)));
  6709. if (err)
  6710. goto bail;
  6711. break;
  6712. case FASTRPC_IOCTL_DSPSIGNAL_WAIT:
  6713. K_COPY_FROM_USER(err, 0, &p.wait, param,
  6714. sizeof(struct fastrpc_ioctl_dspsignal_wait));
  6715. if (err) {
  6716. err = -EFAULT;
  6717. goto bail;
  6718. }
  6719. VERIFY(err, 0 == (err = fastrpc_dspsignal_wait(fl, &p.wait)));
  6720. if (err)
  6721. goto bail;
  6722. break;
  6723. case FASTRPC_IOCTL_DSPSIGNAL_CREATE:
  6724. K_COPY_FROM_USER(err, 0, &p.cre, param,
  6725. sizeof(struct fastrpc_ioctl_dspsignal_create));
  6726. if (err) {
  6727. err = -EFAULT;
  6728. goto bail;
  6729. }
  6730. VERIFY(err, 0 == (err = fastrpc_dspsignal_create(fl, &p.cre)));
  6731. if (err)
  6732. goto bail;
  6733. break;
  6734. case FASTRPC_IOCTL_DSPSIGNAL_DESTROY:
  6735. K_COPY_FROM_USER(err, 0, &p.des, param,
  6736. sizeof(struct fastrpc_ioctl_dspsignal_destroy));
  6737. if (err) {
  6738. err = -EFAULT;
  6739. goto bail;
  6740. }
  6741. VERIFY(err, 0 == (err = fastrpc_dspsignal_destroy(fl, &p.des)));
  6742. if (err)
  6743. goto bail;
  6744. break;
  6745. case FASTRPC_IOCTL_DSPSIGNAL_CANCEL_WAIT:
  6746. K_COPY_FROM_USER(err, 0, &p.canc, param,
  6747. sizeof(struct fastrpc_ioctl_dspsignal_cancel_wait));
  6748. if (err) {
  6749. err = -EFAULT;
  6750. goto bail;
  6751. }
  6752. VERIFY(err, 0 == (err = fastrpc_dspsignal_cancel_wait(fl, &p.canc)));
  6753. if (err)
  6754. goto bail;
  6755. break;
  6756. default:
  6757. err = -ENOTTY;
  6758. pr_info("bad ioctl: %d\n", ioctl_num);
  6759. break;
  6760. }
  6761. bail:
  6762. return err;
  6763. }
  6764. /*
  6765. * fastrpc_smq_ctx_detail : Store smq_invoke_ctx structure parameter.
  6766. * Input :
  6767. * structure smq_invoke_ctx
  6768. * void* mini_dump_buff
  6769. */
  6770. static void fastrpc_smq_ctx_detail(struct smq_invoke_ctx *smq_ctx, int cid, void *mini_dump_buff)
  6771. {
  6772. int i = 0;
  6773. remote_arg64_t *rpra = NULL;
  6774. struct fastrpc_mmap *map = NULL;
  6775. if (!smq_ctx)
  6776. return;
  6777. if (smq_ctx->buf && smq_ctx->buf->virt)
  6778. rpra = smq_ctx->buf->virt;
  6779. for (i = 0; rpra &&
  6780. i < (REMOTE_SCALARS_INBUFS(smq_ctx->sc) + REMOTE_SCALARS_OUTBUFS(smq_ctx->sc));
  6781. ++i) {
  6782. map = smq_ctx->maps[i];
  6783. if (map) {
  6784. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  6785. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6786. smq_invoke_ctx_params,
  6787. smq_ctx->pid, smq_ctx->tgid, smq_ctx->handle,
  6788. smq_ctx->sc, smq_ctx->fl, smq_ctx->fds,
  6789. smq_ctx->magic);
  6790. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  6791. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6792. fastrpc_mmap_params,
  6793. map->fd, map->flags, map->buf,
  6794. map->phys, map->size, map->va,
  6795. map->raddr, map->len, map->refs,
  6796. map->secure);
  6797. } else {
  6798. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  6799. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6800. smq_invoke_ctx_params, smq_ctx->pid, smq_ctx->tgid,
  6801. smq_ctx->handle, smq_ctx->sc, smq_ctx->fl, smq_ctx->fds,
  6802. smq_ctx->magic);
  6803. }
  6804. break;
  6805. }
  6806. }
  6807. /*
  6808. * fastrpc_print_fastrpcbuf : Print fastrpc_buf structure parameter.
  6809. * Input :
  6810. * structure fastrpc_buf
  6811. * void* buffer
  6812. */
  6813. static void fastrpc_print_fastrpcbuf(struct fastrpc_buf *buf, void *buffer)
  6814. {
  6815. if (!buf || !buffer)
  6816. return;
  6817. scnprintf(buffer + strlen(buffer),
  6818. MINI_DUMP_DBG_SIZE - strlen(buffer),
  6819. fastrpc_buf_params, buf->fl, buf->phys,
  6820. buf->virt, buf->size, buf->dma_attr, buf->raddr,
  6821. buf->flags, buf->type, buf->in_use);
  6822. }
  6823. /*
  6824. * fastrpc_print_debug_data : Print debug structure variable in CMA memory.
  6825. * Input cid: Channel id
  6826. */
  6827. static void fastrpc_print_debug_data(int cid)
  6828. {
  6829. unsigned int i = 0, count = 0, gmsg_log_iter = 3, err = 0, len = 0;
  6830. unsigned int tx_index = 0, rx_index = 0;
  6831. unsigned long flags = 0;
  6832. char *gmsg_log_tx = NULL;
  6833. char *gmsg_log_rx = NULL;
  6834. void *mini_dump_buff = NULL;
  6835. struct fastrpc_apps *me = &gfa;
  6836. struct smq_invoke_rspv2 *rsp = NULL;
  6837. struct fastrpc_file *fl = NULL;
  6838. struct fastrpc_channel_ctx *chan = NULL;
  6839. struct hlist_node *n = NULL;
  6840. struct smq_invoke_ctx *ictx = NULL;
  6841. struct fastrpc_tx_msg *tx_msg = NULL;
  6842. struct fastrpc_buf *buf = NULL;
  6843. struct fastrpc_mmap *map = NULL;
  6844. unsigned long irq_flags = 0;
  6845. VERIFY(err, NULL != (gmsg_log_tx = kzalloc(MD_GMSG_BUFFER, GFP_KERNEL)));
  6846. if (err) {
  6847. err = -ENOMEM;
  6848. return;
  6849. }
  6850. VERIFY(err, NULL != (gmsg_log_rx = kzalloc(MD_GMSG_BUFFER, GFP_KERNEL)));
  6851. if (err) {
  6852. err = -ENOMEM;
  6853. return;
  6854. }
  6855. chan = &me->channel[cid];
  6856. if ((!chan) || (!chan->buf))
  6857. return;
  6858. mini_dump_buff = chan->buf->virt;
  6859. if (!mini_dump_buff)
  6860. return;
  6861. if (chan) {
  6862. tx_index = chan->gmsg_log.tx_index;
  6863. rx_index = chan->gmsg_log.rx_index;
  6864. }
  6865. spin_lock_irqsave(&me->hlock, irq_flags);
  6866. hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
  6867. if (fl->cid == cid) {
  6868. scnprintf(mini_dump_buff +
  6869. strlen(mini_dump_buff),
  6870. MINI_DUMP_DBG_SIZE -
  6871. strlen(mini_dump_buff),
  6872. "\nfastrpc_file : %p\n", fl);
  6873. scnprintf(mini_dump_buff +
  6874. strlen(mini_dump_buff),
  6875. MINI_DUMP_DBG_SIZE -
  6876. strlen(mini_dump_buff),
  6877. fastrpc_file_params, fl->tgid,
  6878. fl->cid, fl->ssrcount, fl->pd,
  6879. fl->profile, fl->mode,
  6880. fl->tgid_open, fl->num_cached_buf,
  6881. fl->num_pers_hdrs, fl->sessionid,
  6882. fl->servloc_name, fl->file_close,
  6883. fl->dsp_proc_init, fl->apps,
  6884. fl->qos_request, fl->dev_minor,
  6885. fl->debug_buf,
  6886. fl->debug_buf_alloced_attempted,
  6887. fl->wake_enable,
  6888. fl->ws_timeout,
  6889. fl->untrusted_process);
  6890. scnprintf(mini_dump_buff +
  6891. strlen(mini_dump_buff),
  6892. MINI_DUMP_DBG_SIZE -
  6893. strlen(mini_dump_buff),
  6894. "\nSession Maps\n");
  6895. hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
  6896. scnprintf(mini_dump_buff +
  6897. strlen(mini_dump_buff),
  6898. MINI_DUMP_DBG_SIZE -
  6899. strlen(mini_dump_buff),
  6900. fastrpc_mmap_params,
  6901. map->fd,
  6902. map->flags, map->buf,
  6903. map->phys, map->size,
  6904. map->va, map->raddr,
  6905. map->len, map->refs,
  6906. map->secure);
  6907. }
  6908. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  6909. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6910. "\ncached_bufs\n");
  6911. hlist_for_each_entry_safe(buf, n, &fl->cached_bufs, hn) {
  6912. fastrpc_print_fastrpcbuf(buf, mini_dump_buff);
  6913. }
  6914. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  6915. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6916. "\ninit_mem: %p\n", fl->init_mem);
  6917. fastrpc_print_fastrpcbuf(fl->init_mem, mini_dump_buff);
  6918. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  6919. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6920. "\npers_hdr_buf: %p\n", fl->pers_hdr_buf);
  6921. fastrpc_print_fastrpcbuf(fl->pers_hdr_buf, mini_dump_buff);
  6922. snprintf(mini_dump_buff + strlen(mini_dump_buff),
  6923. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6924. "\nhdr_bufs: %p\n", fl->hdr_bufs);
  6925. fastrpc_print_fastrpcbuf(fl->hdr_bufs, mini_dump_buff);
  6926. if (fl->debugfs_file) {
  6927. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  6928. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6929. "\nfl->debugfs_file.d_iname : %s\n",
  6930. fl->debugfs_file->d_iname);
  6931. }
  6932. if (fl->sctx) {
  6933. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  6934. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6935. "\nfl->sctx->smmu.cb : %d\n",
  6936. fl->sctx->smmu.cb);
  6937. }
  6938. if (fl->secsctx) {
  6939. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  6940. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6941. "\nfl->secsctx->smmu.cb : %d\n",
  6942. fl->secsctx->smmu.cb);
  6943. }
  6944. spin_lock(&fl->hlock);
  6945. scnprintf(mini_dump_buff +
  6946. strlen(mini_dump_buff),
  6947. MINI_DUMP_DBG_SIZE -
  6948. strlen(mini_dump_buff),
  6949. "\nPending Ctx:\n");
  6950. hlist_for_each_entry_safe(ictx, n, &fl->clst.pending, hn) {
  6951. fastrpc_smq_ctx_detail(ictx,
  6952. cid, mini_dump_buff);
  6953. }
  6954. scnprintf(mini_dump_buff +
  6955. strlen(mini_dump_buff),
  6956. MINI_DUMP_DBG_SIZE -
  6957. strlen(mini_dump_buff),
  6958. "\nInterrupted Ctx:\n");
  6959. hlist_for_each_entry_safe(ictx, n,
  6960. &fl->clst.interrupted,
  6961. hn) {
  6962. fastrpc_smq_ctx_detail(ictx,
  6963. cid, mini_dump_buff);
  6964. }
  6965. spin_unlock(&fl->hlock);
  6966. }
  6967. }
  6968. spin_unlock_irqrestore(&me->hlock, irq_flags);
  6969. spin_lock_irqsave(&chan->gmsg_log.lock, flags);
  6970. if (rx_index) {
  6971. for (i = rx_index, count = 0, len = 0 ; i > 0 &&
  6972. count <= gmsg_log_iter; i--, count++) {
  6973. rsp = &chan->gmsg_log.rx_msgs[i].rsp;
  6974. len += scnprintf(gmsg_log_rx + len, MD_GMSG_BUFFER - len,
  6975. "ctx: 0x%x, retval: %d, flags: %d, early_wake_time: %d, version: %d\n",
  6976. rsp->ctx, rsp->retval, rsp->flags,
  6977. rsp->early_wake_time, rsp->version);
  6978. }
  6979. }
  6980. if (tx_index) {
  6981. for (i = tx_index, count = 0, len = 0;
  6982. i > 0 && count <= gmsg_log_iter;
  6983. i--, count++) {
  6984. tx_msg = &chan->gmsg_log.tx_msgs[i];
  6985. len += scnprintf(gmsg_log_tx + len, MD_GMSG_BUFFER - len,
  6986. "pid: %d, tid: %d, ctx: 0x%x, handle: 0x%x, sc: 0x%x, addr: 0x%x, size:%d\n",
  6987. tx_msg->msg.pid,
  6988. tx_msg->msg.tid,
  6989. tx_msg->msg.invoke.header.ctx,
  6990. tx_msg->msg.invoke.header.handle,
  6991. tx_msg->msg.invoke.header.sc,
  6992. tx_msg->msg.invoke.page.addr,
  6993. tx_msg->msg.invoke.page.size);
  6994. }
  6995. }
  6996. spin_unlock_irqrestore(&chan->gmsg_log.lock, flags);
  6997. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  6998. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6999. "gmsg_log_tx:\n%s\n", gmsg_log_tx);
  7000. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  7001. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  7002. "gmsg_log_rx:\n %s\n", gmsg_log_rx);
  7003. if (chan && chan->buf)
  7004. chan->buf->size = strlen(mini_dump_buff);
  7005. kfree(gmsg_log_tx);
  7006. kfree(gmsg_log_rx);
  7007. }
  7008. void fastrpc_restart_drivers(int cid)
  7009. {
  7010. struct fastrpc_apps *me = &gfa;
  7011. fastrpc_notify_drivers(me, cid);
  7012. me->channel[cid].ssrcount++;
  7013. }
  7014. static int fastrpc_restart_notifier_cb(struct notifier_block *nb,
  7015. unsigned long code,
  7016. void *data)
  7017. {
  7018. struct fastrpc_apps *me = &gfa;
  7019. struct fastrpc_channel_ctx *ctx;
  7020. struct fastrpc_file *fl;
  7021. struct hlist_node *n;
  7022. int cid = -1;
  7023. struct timespec64 startT = {0};
  7024. unsigned long irq_flags = 0;
  7025. ctx = container_of(nb, struct fastrpc_channel_ctx, nb);
  7026. cid = ctx - &me->channel[0];
  7027. switch (code) {
  7028. case QCOM_SSR_BEFORE_SHUTDOWN:
  7029. fastrpc_rproc_trace_events(gcinfo[cid].subsys,
  7030. "QCOM_SSR_BEFORE_SHUTDOWN", "fastrpc_restart_notifier-enter");
  7031. pr_info("adsprpc: %s: %s subsystem is restarting\n",
  7032. __func__, gcinfo[cid].subsys);
  7033. mutex_lock(&me->channel[cid].smd_mutex);
  7034. ctx->ssrcount++;
  7035. ctx->subsystemstate = SUBSYSTEM_RESTARTING;
  7036. mutex_unlock(&me->channel[cid].smd_mutex);
  7037. if (cid == RH_CID)
  7038. me->staticpd_flags = 0;
  7039. break;
  7040. case QCOM_SSR_AFTER_SHUTDOWN:
  7041. fastrpc_rproc_trace_events(gcinfo[cid].subsys,
  7042. "QCOM_SSR_AFTER_SHUTDOWN", "fastrpc_restart_notifier-enter");
  7043. spin_lock_irqsave(&me->hlock, irq_flags);
  7044. hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
  7045. if (fl->cid != cid)
  7046. continue;
  7047. complete(&fl->shutdown);
  7048. }
  7049. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7050. ctx->subsystemstate = SUBSYSTEM_DOWN;
  7051. pr_info("adsprpc: %s: received RAMDUMP notification for %s\n",
  7052. __func__, gcinfo[cid].subsys);
  7053. break;
  7054. case QCOM_SSR_BEFORE_POWERUP:
  7055. fastrpc_rproc_trace_events(gcinfo[cid].subsys,
  7056. "QCOM_SSR_BEFORE_POWERUP", "fastrpc_restart_notifier-enter");
  7057. pr_info("adsprpc: %s: subsystem %s is about to start\n",
  7058. __func__, gcinfo[cid].subsys);
  7059. if (cid == CDSP_DOMAIN_ID && dump_enabled() &&
  7060. ctx->ssrcount)
  7061. fastrpc_update_ramdump_status(cid);
  7062. fastrpc_notify_drivers(me, cid);
  7063. /* Skip ram dump collection in first boot */
  7064. if (cid == CDSP_DOMAIN_ID && dump_enabled() &&
  7065. ctx->ssrcount) {
  7066. mutex_lock(&me->channel[cid].smd_mutex);
  7067. fastrpc_print_debug_data(cid);
  7068. mutex_unlock(&me->channel[cid].smd_mutex);
  7069. ktime_get_real_ts64(&startT);
  7070. fastrpc_ramdump_collection(cid);
  7071. pr_info("adsprpc: %s: fastrpc ramdump finished in %lu (us)\n",
  7072. __func__, getnstimediff(&startT));
  7073. }
  7074. break;
  7075. case QCOM_SSR_AFTER_POWERUP:
  7076. fastrpc_rproc_trace_events(gcinfo[cid].subsys,
  7077. "QCOM_SSR_AFTER_POWERUP", "fastrpc_restart_notifier-enter");
  7078. pr_info("adsprpc: %s: %s subsystem is up\n",
  7079. __func__, gcinfo[cid].subsys);
  7080. ctx->subsystemstate = SUBSYSTEM_UP;
  7081. break;
  7082. default:
  7083. break;
  7084. }
  7085. fastrpc_rproc_trace_events(dev_name(me->dev), "fastrpc_restart_notifier", "exit");
  7086. return NOTIFY_DONE;
  7087. }
  7088. static void fastrpc_pdr_cb(int state, char *service_path, void *priv)
  7089. {
  7090. struct fastrpc_apps *me = &gfa;
  7091. struct fastrpc_static_pd *spd;
  7092. int err = 0;
  7093. spd = priv;
  7094. VERIFY(err, spd);
  7095. if (err)
  7096. goto bail;
  7097. switch (state) {
  7098. case SERVREG_SERVICE_STATE_DOWN:
  7099. pr_info("adsprpc: %s: %s (%s) is down for PDR on %s\n",
  7100. __func__, spd->spdname,
  7101. spd->servloc_name,
  7102. gcinfo[spd->cid].subsys);
  7103. mutex_lock(&me->channel[spd->cid].smd_mutex);
  7104. spd->pdrcount++;
  7105. atomic_set(&spd->ispdup, 0);
  7106. mutex_unlock(&me->channel[spd->cid].smd_mutex);
  7107. if (!strcmp(spd->servloc_name,
  7108. AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME))
  7109. me->staticpd_flags = 0;
  7110. fastrpc_notify_pdr_drivers(me, spd->servloc_name);
  7111. break;
  7112. case SERVREG_SERVICE_STATE_UP:
  7113. pr_info("adsprpc: %s: %s (%s) is up for PDR on %s\n",
  7114. __func__, spd->spdname,
  7115. spd->servloc_name,
  7116. gcinfo[spd->cid].subsys);
  7117. atomic_set(&spd->ispdup, 1);
  7118. wake_up_interruptible(&spd->wait_for_pdup);
  7119. break;
  7120. default:
  7121. break;
  7122. }
  7123. bail:
  7124. if (err) {
  7125. pr_err("adsprpc: %s: failed for path %s, state %d, spd %pK\n",
  7126. __func__, service_path, state, spd);
  7127. }
  7128. }
  7129. static const struct file_operations fops = {
  7130. .open = fastrpc_device_open,
  7131. .release = fastrpc_device_release,
  7132. .unlocked_ioctl = fastrpc_device_ioctl,
  7133. /* Only DSP service 64-bit app will interface with fastrpc TVM driver.
  7134. * There is not need to support 32-bit fastrpc driver on TVM.
  7135. */
  7136. #if IS_ENABLED(CONFIG_MSM_ADSPRPC_TRUSTED)
  7137. .compat_ioctl = NULL,
  7138. #else
  7139. .compat_ioctl = compat_fastrpc_device_ioctl,
  7140. #endif
  7141. };
  7142. static const struct of_device_id fastrpc_match_table[] = {
  7143. { .compatible = "qcom,msm-fastrpc-adsp", },
  7144. { .compatible = "qcom,msm-fastrpc-compute", },
  7145. { .compatible = "qcom,msm-fastrpc-compute-cb", },
  7146. { .compatible = "qcom,msm-adsprpc-mem-region", },
  7147. {}
  7148. };
  7149. static int fastrpc_cb_probe(struct device *dev)
  7150. {
  7151. struct fastrpc_channel_ctx *chan = NULL;
  7152. struct fastrpc_session_ctx *sess = NULL;
  7153. struct of_phandle_args iommuspec;
  7154. struct fastrpc_apps *me = &gfa;
  7155. struct fastrpc_buf *buf = NULL;
  7156. struct gen_pool *gen_pool = NULL;
  7157. struct iommu_domain *domain = NULL;
  7158. const char *name;
  7159. int err = 0, cid = -1, i = 0;
  7160. u32 sharedcb_count = 0, j = 0;
  7161. uint32_t dma_addr_pool[2] = {0, 0};
  7162. VERIFY(err, NULL != (name = of_get_property(dev->of_node,
  7163. "label", NULL)));
  7164. if (err) {
  7165. err = -EINVAL;
  7166. goto bail;
  7167. }
  7168. for (i = 0; i < NUM_CHANNELS; i++) {
  7169. if (!gcinfo[i].name)
  7170. continue;
  7171. if (!strcmp(name, gcinfo[i].name))
  7172. break;
  7173. }
  7174. VERIFY(err, i < NUM_CHANNELS);
  7175. if (err) {
  7176. err = -ECHRNG;
  7177. goto bail;
  7178. }
  7179. cid = i;
  7180. chan = &gcinfo[i];
  7181. VERIFY(err, chan->sesscount < NUM_SESSIONS);
  7182. if (err) {
  7183. err = -EINVAL;
  7184. goto bail;
  7185. }
  7186. err = of_parse_phandle_with_args(dev->of_node, "iommus",
  7187. "#iommu-cells", 0, &iommuspec);
  7188. if (err) {
  7189. pr_err("Error: adsprpc: %s: parsing iommu arguments failed for %s with err %d\n",
  7190. __func__, dev_name(dev), err);
  7191. goto bail;
  7192. }
  7193. sess = &chan->session[chan->sesscount];
  7194. sess->used = 0;
  7195. sess->smmu.coherent = of_property_read_bool(dev->of_node,
  7196. "dma-coherent");
  7197. sess->smmu.secure = of_property_read_bool(dev->of_node,
  7198. "qcom,secure-context-bank");
  7199. sess->smmu.cb = iommuspec.args[0] & 0xf;
  7200. sess->smmu.dev = dev;
  7201. sess->smmu.dev_name = dev_name(dev);
  7202. sess->smmu.enabled = 1;
  7203. if (!sess->smmu.dev->dma_parms)
  7204. sess->smmu.dev->dma_parms = devm_kzalloc(sess->smmu.dev,
  7205. sizeof(*sess->smmu.dev->dma_parms), GFP_KERNEL);
  7206. dma_set_max_seg_size(sess->smmu.dev, DMA_BIT_MASK(32));
  7207. dma_set_seg_boundary(sess->smmu.dev, (unsigned long)DMA_BIT_MASK(64));
  7208. of_property_read_u32_array(dev->of_node, "qcom,iommu-dma-addr-pool",
  7209. dma_addr_pool, 2);
  7210. me->max_size_limit = (dma_addr_pool[1] == 0 ? 0x78000000 :
  7211. dma_addr_pool[1]);
  7212. if (of_get_property(dev->of_node, "pd-type", NULL) != NULL) {
  7213. err = of_property_read_u32(dev->of_node, "pd-type",
  7214. &(sess->smmu.pd_type));
  7215. /* Set cb_pd_type, if the process type is set for context banks */
  7216. me->cb_pd_type = true;
  7217. if (err)
  7218. goto bail;
  7219. }
  7220. if (of_get_property(dev->of_node, "shared-cb", NULL) != NULL) {
  7221. sess->smmu.sharedcb = 1;
  7222. // Set share_securecb, if the secure context bank is shared
  7223. if (sess->smmu.secure)
  7224. me->share_securecb = 1;
  7225. err = of_property_read_u32(dev->of_node, "shared-cb",
  7226. &sharedcb_count);
  7227. if (err)
  7228. goto bail;
  7229. if (sharedcb_count > 0) {
  7230. struct fastrpc_session_ctx *dup_sess;
  7231. for (j = 1; j < sharedcb_count &&
  7232. chan->sesscount < NUM_SESSIONS; j++) {
  7233. chan->sesscount++;
  7234. dup_sess = &chan->session[chan->sesscount];
  7235. memcpy(dup_sess, sess,
  7236. sizeof(struct fastrpc_session_ctx));
  7237. }
  7238. }
  7239. }
  7240. if (of_get_property(dev->of_node, "qrtr-gen-pool", NULL) != NULL) {
  7241. u32 frpc_gen_addr_pool[2] = {0, 0};
  7242. struct sg_table sgt;
  7243. err = of_property_read_u32_array(dev->of_node, "frpc-gen-addr-pool",
  7244. frpc_gen_addr_pool, 2);
  7245. if (err) {
  7246. pr_err("Error: adsprpc: %s: parsing frpc-gen-addr-pool arguments failed for %s with err %d\n",
  7247. __func__, dev_name(dev), err);
  7248. goto bail;
  7249. }
  7250. sess->smmu.genpool_iova = frpc_gen_addr_pool[0];
  7251. sess->smmu.genpool_size = frpc_gen_addr_pool[1];
  7252. VERIFY(err, NULL != (buf = kzalloc(sizeof(*buf), GFP_KERNEL)));
  7253. if (err) {
  7254. err = -ENOMEM;
  7255. ADSPRPC_ERR(
  7256. "allocation failed for size 0x%zx\n", sizeof(*buf));
  7257. goto bail;
  7258. }
  7259. INIT_HLIST_NODE(&buf->hn);
  7260. buf->virt = NULL;
  7261. buf->phys = 0;
  7262. buf->size = frpc_gen_addr_pool[1];
  7263. buf->dma_attr = DMA_ATTR_DELAYED_UNMAP;
  7264. /* Allocate memory for adding to genpool */
  7265. buf->virt = dma_alloc_attrs(sess->smmu.dev, buf->size,
  7266. (dma_addr_t *)&buf->phys,
  7267. GFP_KERNEL, buf->dma_attr);
  7268. if (IS_ERR_OR_NULL(buf->virt)) {
  7269. ADSPRPC_ERR(
  7270. "dma_alloc_attrs failed for size 0x%zx, returned %pK\n",
  7271. buf->size, buf->virt);
  7272. err = -ENOBUFS;
  7273. goto dma_alloc_bail;
  7274. }
  7275. err = dma_get_sgtable_attrs(sess->smmu.dev, &sgt, buf->virt,
  7276. buf->phys, buf->size, buf->dma_attr);
  7277. if (err) {
  7278. ADSPRPC_ERR("dma_get_sgtable_attrs failed with err %d", err);
  7279. goto iommu_map_bail;
  7280. }
  7281. domain = iommu_get_domain_for_dev(sess->smmu.dev);
  7282. if (!domain) {
  7283. ADSPRPC_ERR("iommu_get_domain_for_dev failed ");
  7284. goto iommu_map_bail;
  7285. }
  7286. /* Map the allocated memory with fixed IOVA and is shared to remote subsystem */
  7287. err = iommu_map_sg(domain, frpc_gen_addr_pool[0], sgt.sgl,
  7288. sgt.nents, IOMMU_READ | IOMMU_WRITE | IOMMU_CACHE);
  7289. if (err < 0) {
  7290. ADSPRPC_ERR("iommu_map_sg failed with err %d", err);
  7291. goto iommu_map_bail;
  7292. }
  7293. /* Create genpool using SMMU device */
  7294. gen_pool = devm_gen_pool_create(sess->smmu.dev, 0,
  7295. NUMA_NO_NODE, NULL);
  7296. if (IS_ERR(gen_pool)) {
  7297. err = PTR_ERR(gen_pool);
  7298. ADSPRPC_ERR("devm_gen_pool_create failed with err %d", err);
  7299. goto genpool_create_bail;
  7300. }
  7301. /* Add allocated memory to genpool */
  7302. err = gen_pool_add_virt(gen_pool, (unsigned long)buf->virt,
  7303. buf->phys, buf->size, NUMA_NO_NODE);
  7304. if (err) {
  7305. ADSPRPC_ERR("gen_pool_add_virt failed with err %d", err);
  7306. goto genpool_add_bail;
  7307. }
  7308. sess->smmu.frpc_genpool = gen_pool;
  7309. sess->smmu.frpc_genpool_buf = buf;
  7310. }
  7311. chan->sesscount++;
  7312. if (debugfs_root && !debugfs_global_file) {
  7313. debugfs_global_file = debugfs_create_file("global", 0644,
  7314. debugfs_root, NULL, &debugfs_fops);
  7315. if (IS_ERR_OR_NULL(debugfs_global_file)) {
  7316. pr_warn("Error: %s: %s: failed to create debugfs global file\n",
  7317. current->comm, __func__);
  7318. debugfs_global_file = NULL;
  7319. }
  7320. }
  7321. bail:
  7322. return err;
  7323. genpool_add_bail:
  7324. gen_pool_destroy(gen_pool);
  7325. genpool_create_bail:
  7326. iommu_unmap(domain, sess->smmu.genpool_iova,
  7327. sess->smmu.genpool_size);
  7328. iommu_map_bail:
  7329. dma_free_attrs(sess->smmu.dev, buf->size, buf->virt,
  7330. buf->phys, buf->dma_attr);
  7331. dma_alloc_bail:
  7332. kfree(buf);
  7333. return err;
  7334. }
  7335. static void init_secure_vmid_list(struct device *dev, char *prop_name,
  7336. struct secure_vm *destvm)
  7337. {
  7338. int err = 0;
  7339. u32 len = 0, i = 0;
  7340. u32 *rhvmlist = NULL;
  7341. u32 *rhvmpermlist = NULL;
  7342. if (!of_find_property(dev->of_node, prop_name, &len))
  7343. goto bail;
  7344. if (len == 0)
  7345. goto bail;
  7346. len /= sizeof(u32);
  7347. VERIFY(err, NULL != (rhvmlist = kcalloc(len, sizeof(u32), GFP_KERNEL)));
  7348. if (err)
  7349. goto bail;
  7350. VERIFY(err, NULL != (rhvmpermlist = kcalloc(len, sizeof(u32),
  7351. GFP_KERNEL)));
  7352. if (err)
  7353. goto bail;
  7354. for (i = 0; i < len; i++) {
  7355. err = of_property_read_u32_index(dev->of_node, prop_name, i,
  7356. &rhvmlist[i]);
  7357. if (err) {
  7358. pr_err("Error: adsprpc: %s: failed to read VMID\n",
  7359. __func__);
  7360. goto bail;
  7361. }
  7362. ADSPRPC_INFO("secure VMID = %d\n",
  7363. rhvmlist[i]);
  7364. rhvmpermlist[i] = QCOM_SCM_PERM_RWX;
  7365. }
  7366. destvm->vmid = rhvmlist;
  7367. destvm->vmperm = rhvmpermlist;
  7368. destvm->vmcount = len;
  7369. bail:
  7370. if (err) {
  7371. kfree(rhvmlist);
  7372. kfree(rhvmpermlist);
  7373. }
  7374. }
  7375. static void fastrpc_init_privileged_gids(struct device *dev, char *prop_name,
  7376. struct gid_list *gidlist)
  7377. {
  7378. int err = 0;
  7379. u32 len = 0, i = 0;
  7380. u32 *gids = NULL;
  7381. if (!of_find_property(dev->of_node, prop_name, &len))
  7382. goto bail;
  7383. if (len == 0)
  7384. goto bail;
  7385. len /= sizeof(u32);
  7386. gids = kcalloc(len, sizeof(u32), GFP_KERNEL);
  7387. if (!gids) {
  7388. err = ENOMEM;
  7389. goto bail;
  7390. }
  7391. for (i = 0; i < len; i++) {
  7392. err = of_property_read_u32_index(dev->of_node, prop_name,
  7393. i, &gids[i]);
  7394. if (err) {
  7395. pr_err("Error: adsprpc: %s: failed to read GID %u\n",
  7396. __func__, i);
  7397. goto bail;
  7398. }
  7399. pr_info("adsprpc: %s: privileged GID: %u\n", __func__, gids[i]);
  7400. }
  7401. sort(gids, len, sizeof(*gids), uint_cmp_func, NULL);
  7402. gidlist->gids = gids;
  7403. gidlist->gidcount = len;
  7404. bail:
  7405. if (err)
  7406. kfree(gids);
  7407. }
  7408. static void configure_secure_channels(uint32_t secure_domains)
  7409. {
  7410. struct fastrpc_apps *me = &gfa;
  7411. int ii = 0;
  7412. /*
  7413. * secure_domains contains the bitmask of the secure channels
  7414. * Bit 0 - ADSP
  7415. * Bit 1 - MDSP
  7416. * Bit 2 - SLPI
  7417. * Bit 3 - CDSP
  7418. */
  7419. for (ii = ADSP_DOMAIN_ID; ii <= CDSP_DOMAIN_ID; ++ii) {
  7420. int secure = (secure_domains >> ii) & 0x01;
  7421. me->channel[ii].secure = secure;
  7422. ADSPRPC_INFO("domain %d configured as secure %d\n", ii, secure);
  7423. }
  7424. }
  7425. /*
  7426. * This function is used to create the service locator required for
  7427. * registering for remote process restart (PDR) notifications if that
  7428. * PDR property has been enabled in the fastrpc node on the DTSI.
  7429. */
  7430. static int fastrpc_setup_service_locator(struct device *dev,
  7431. const char *propname,
  7432. char *client_name, char *service_name,
  7433. char *service_path)
  7434. {
  7435. int err = 0, session = -1, cid = -1;
  7436. struct fastrpc_apps *me = &gfa;
  7437. struct pdr_handle *handle = NULL;
  7438. struct pdr_service *service = NULL;
  7439. if (of_property_read_bool(dev->of_node, propname)) {
  7440. err = fastrpc_get_spd_session(client_name, &session, &cid);
  7441. if (err)
  7442. goto bail;
  7443. /* Register the service locator's callback function */
  7444. handle = pdr_handle_alloc(fastrpc_pdr_cb, &me->channel[cid].spd[session]);
  7445. if (IS_ERR_OR_NULL(handle)) {
  7446. err = PTR_ERR(handle);
  7447. goto bail;
  7448. }
  7449. me->channel[cid].spd[session].pdrhandle = handle;
  7450. service = pdr_add_lookup(handle, service_name, service_path);
  7451. if (IS_ERR_OR_NULL(service)) {
  7452. err = PTR_ERR(service);
  7453. goto bail;
  7454. }
  7455. pr_info("adsprpc: %s: pdr_add_lookup enabled for %s (%s, %s), DTSI (%s)\n",
  7456. __func__, service_name, client_name, service_path, propname);
  7457. }
  7458. bail:
  7459. if (err) {
  7460. pr_warn("adsprpc: %s: failed for %s (%s, %s), DTSI (%s) with err %d\n",
  7461. __func__, service_name, client_name, service_path, propname, err);
  7462. }
  7463. return err;
  7464. }
  7465. /*
  7466. * fastrpc_nsp_status_show() - Updates the buffer with remote nsp status
  7467. * by reading the fastrpc node.
  7468. * @dev : pointer to device node.
  7469. * @attr: pointer to device attribute.
  7470. * @buf : Output parameter to be updated with remote nsp status.
  7471. * Return : bytes written to buffer.
  7472. */
  7473. static ssize_t fastrpc_nsp_status_show(struct device *dev,
  7474. struct device_attribute *attr, char *buf)
  7475. {
  7476. struct fastrpc_apps *me = &gfa;
  7477. /*
  7478. * Default remote DSP status: 0
  7479. * driver possibly not probed yet or not the main device.
  7480. */
  7481. if (!dev || !dev->driver ||
  7482. !of_device_is_compatible(dev->of_node, "qcom,msm-fastrpc-compute")) {
  7483. ADSPRPC_ERR("Driver not probed yet or not the main device\n");
  7484. return 0;
  7485. }
  7486. return scnprintf(buf, PAGE_SIZE, "%d",
  7487. me->fastrpc_nsp_status);
  7488. }
  7489. /* Remote nsp status attribute declaration as read only */
  7490. static DEVICE_ATTR_RO(fastrpc_nsp_status);
  7491. /* Declaring attribute for remote dsp */
  7492. static struct attribute *msm_remote_dsp_attrs[] = {
  7493. &dev_attr_fastrpc_nsp_status.attr,
  7494. NULL
  7495. };
  7496. /* Defining remote dsp attributes in attributes group */
  7497. static struct attribute_group msm_remote_dsp_attr_group = {
  7498. .attrs = msm_remote_dsp_attrs,
  7499. };
  7500. static int fastrpc_probe(struct platform_device *pdev)
  7501. {
  7502. int err = 0;
  7503. struct fastrpc_apps *me = &gfa;
  7504. struct device *dev = &pdev->dev;
  7505. int ret = 0;
  7506. uint32_t secure_domains = 0;
  7507. if (of_device_is_compatible(dev->of_node,
  7508. "qcom,msm-fastrpc-compute")) {
  7509. err = sysfs_create_group(&pdev->dev.kobj, &msm_remote_dsp_attr_group);
  7510. if (err) {
  7511. ADSPRPC_ERR(
  7512. "Initialization of sysfs create group failed with %d\n",
  7513. err);
  7514. goto bail;
  7515. }
  7516. init_secure_vmid_list(dev, "qcom,adsp-remoteheap-vmid",
  7517. &gcinfo[0].rhvm);
  7518. fastrpc_init_privileged_gids(dev, "qcom,fastrpc-gids",
  7519. &me->gidlist);
  7520. /*
  7521. * Check if latency voting for only one core
  7522. * is enabled for the platform
  7523. */
  7524. me->single_core_latency_vote = of_property_read_bool(dev->of_node,
  7525. "qcom,single-core-latency-vote");
  7526. if (me->single_core_latency_vote)
  7527. me->lowest_capacity_core_count = 1;
  7528. of_property_read_u32(dev->of_node, "qcom,rpc-latency-us",
  7529. &me->latency);
  7530. of_property_read_u32(dev->of_node, "qcom,max-sessions",
  7531. &me->max_sess_per_proc);
  7532. if (of_get_property(dev->of_node,
  7533. "qcom,secure-domains", NULL) != NULL) {
  7534. VERIFY(err, !of_property_read_u32(dev->of_node,
  7535. "qcom,secure-domains",
  7536. &secure_domains));
  7537. if (!err)
  7538. configure_secure_channels(secure_domains);
  7539. else
  7540. pr_info("adsprpc: unable to read the domain configuration from dts\n");
  7541. }
  7542. }
  7543. if (of_device_is_compatible(dev->of_node,
  7544. "qcom,msm-fastrpc-compute-cb"))
  7545. return fastrpc_cb_probe(dev);
  7546. if (of_device_is_compatible(dev->of_node,
  7547. "qcom,msm-adsprpc-mem-region")) {
  7548. me->dev = dev;
  7549. ret = of_reserved_mem_device_init_by_idx(dev, dev->of_node, 0);
  7550. if (ret) {
  7551. pr_warn("adsprpc: Error: %s: initialization of memory region adsp_mem failed with %d\n",
  7552. __func__, ret);
  7553. }
  7554. goto bail;
  7555. }
  7556. me->legacy_remote_heap = of_property_read_bool(dev->of_node,
  7557. "qcom,fastrpc-legacy-remote-heap");
  7558. err = fastrpc_setup_service_locator(dev, AUDIO_PDR_ADSP_DTSI_PROPERTY_NAME,
  7559. AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME,
  7560. AUDIO_PDR_ADSP_SERVICE_NAME, ADSP_AUDIOPD_NAME);
  7561. if (err)
  7562. goto bail;
  7563. err = fastrpc_setup_service_locator(dev, SENSORS_PDR_ADSP_DTSI_PROPERTY_NAME,
  7564. SENSORS_PDR_ADSP_SERVICE_LOCATION_CLIENT_NAME,
  7565. SENSORS_PDR_ADSP_SERVICE_NAME, ADSP_SENSORPD_NAME);
  7566. if (err)
  7567. goto bail;
  7568. err = fastrpc_setup_service_locator(dev, SENSORS_PDR_SLPI_DTSI_PROPERTY_NAME,
  7569. SENSORS_PDR_SLPI_SERVICE_LOCATION_CLIENT_NAME,
  7570. SENSORS_PDR_SLPI_SERVICE_NAME, SLPI_SENSORPD_NAME);
  7571. if (err)
  7572. goto bail;
  7573. err = of_platform_populate(pdev->dev.of_node,
  7574. fastrpc_match_table,
  7575. NULL, &pdev->dev);
  7576. if (err)
  7577. goto bail;
  7578. bail:
  7579. return err;
  7580. }
  7581. /*
  7582. * Function to free fastrpc genpool buffer
  7583. */
  7584. static void fastrpc_genpool_free(struct fastrpc_session_ctx *sess)
  7585. {
  7586. struct fastrpc_buf *buf = NULL;
  7587. struct iommu_domain *domain = NULL;
  7588. if (!sess)
  7589. goto bail;
  7590. buf = sess->smmu.frpc_genpool_buf;
  7591. if (sess->smmu.frpc_genpool) {
  7592. gen_pool_destroy(sess->smmu.frpc_genpool);
  7593. sess->smmu.frpc_genpool = NULL;
  7594. }
  7595. if (buf && sess->smmu.dev) {
  7596. domain = iommu_get_domain_for_dev(sess->smmu.dev);
  7597. iommu_unmap(domain, sess->smmu.genpool_iova,
  7598. sess->smmu.genpool_size);
  7599. if (buf->phys)
  7600. dma_free_attrs(sess->smmu.dev, buf->size, buf->virt,
  7601. buf->phys, buf->dma_attr);
  7602. kfree(buf);
  7603. sess->smmu.frpc_genpool_buf = NULL;
  7604. }
  7605. bail:
  7606. return;
  7607. }
  7608. static void fastrpc_deinit(void)
  7609. {
  7610. struct fastrpc_channel_ctx *chan = gcinfo;
  7611. struct fastrpc_apps *me = &gfa;
  7612. int i, j;
  7613. for (i = 0; i < NUM_CHANNELS; i++, chan++) {
  7614. for (j = 0; j < NUM_SESSIONS; j++) {
  7615. struct fastrpc_session_ctx *sess = &chan->session[j];
  7616. fastrpc_genpool_free(sess);
  7617. if (sess->smmu.dev)
  7618. sess->smmu.dev = NULL;
  7619. }
  7620. kfree(chan->rhvm.vmid);
  7621. kfree(chan->rhvm.vmperm);
  7622. fastrpc_transport_session_deinit(i);
  7623. mutex_destroy(&chan->smd_mutex);
  7624. }
  7625. if (me->transport_initialized)
  7626. fastrpc_transport_deinit();
  7627. me->transport_initialized = 0;
  7628. mutex_destroy(&me->mut_uid);
  7629. }
  7630. #ifdef CONFIG_HIBERNATION
  7631. static bool hibernation;
  7632. static int fastrpc_hibernation_notifier(struct notifier_block *nb,
  7633. unsigned long event, void *dummy)
  7634. {
  7635. if (event == PM_HIBERNATION_PREPARE)
  7636. hibernation = true;
  7637. else if (event == PM_POST_HIBERNATION)
  7638. hibernation = false;
  7639. return NOTIFY_OK;
  7640. }
  7641. static struct notifier_block fastrpc_notif_block = {
  7642. .notifier_call = fastrpc_hibernation_notifier,
  7643. };
  7644. #endif
  7645. #ifdef CONFIG_PM_SLEEP
  7646. static int fastrpc_hibernation_suspend(struct device *dev)
  7647. {
  7648. int err = 0;
  7649. if (of_device_is_compatible(dev->of_node,
  7650. "qcom,msm-fastrpc-compute")) {
  7651. err = fastrpc_mmap_remove_ssr(NULL, 0);
  7652. if (err)
  7653. ADSPRPC_WARN("failed to unmap remote heap (err %d)\n",
  7654. err);
  7655. }
  7656. return err;
  7657. }
  7658. static int fastrpc_restore(struct device *dev)
  7659. {
  7660. struct fastrpc_apps *me = &gfa;
  7661. int cid;
  7662. pr_info("adsprpc: restore enter\n");
  7663. for (cid = 0; cid < NUM_CHANNELS; cid++)
  7664. me->channel[cid].in_hib = 1;
  7665. pr_info("adsprpc: restore exit\n");
  7666. return 0;
  7667. }
  7668. static const struct dev_pm_ops fastrpc_pm = {
  7669. .freeze = fastrpc_hibernation_suspend,
  7670. .restore = fastrpc_restore,
  7671. };
  7672. #endif
  7673. static struct platform_driver fastrpc_driver = {
  7674. .probe = fastrpc_probe,
  7675. .driver = {
  7676. .name = "fastrpc",
  7677. .of_match_table = fastrpc_match_table,
  7678. .suppress_bind_attrs = true,
  7679. #ifdef CONFIG_PM_SLEEP
  7680. .pm = &fastrpc_pm,
  7681. #endif
  7682. },
  7683. };
  7684. union fastrpc_dev_param {
  7685. struct fastrpc_dev_map_dma *map;
  7686. struct fastrpc_dev_unmap_dma *unmap;
  7687. struct fastrpc_dev_get_hlos_pid *hpid;
  7688. };
  7689. long fastrpc_dev_map_dma(struct fastrpc_device *dev, unsigned long invoke_param)
  7690. {
  7691. int err = 0;
  7692. union fastrpc_dev_param p;
  7693. struct fastrpc_file *fl = NULL;
  7694. struct fastrpc_mmap *map = NULL;
  7695. struct fastrpc_apps *me = &gfa;
  7696. uintptr_t raddr = 0;
  7697. unsigned long irq_flags = 0;
  7698. p.map = (struct fastrpc_dev_map_dma *)invoke_param;
  7699. spin_lock_irqsave(&me->hlock, irq_flags);
  7700. /* Verify if fastrpc device is closed*/
  7701. VERIFY(err, dev && !dev->dev_close);
  7702. if (err) {
  7703. err = -ESRCH;
  7704. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7705. return err;
  7706. }
  7707. fl = dev->fl;
  7708. /* Verify if fastrpc file is not NULL*/
  7709. if (!fl) {
  7710. err = -EBADF;
  7711. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7712. return err;
  7713. }
  7714. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7715. mutex_lock(&fl->internal_map_mutex);
  7716. spin_lock_irqsave(&me->hlock, irq_flags);
  7717. /* Verify if fastrpc file is being closed, holding device lock*/
  7718. if (fl->file_close) {
  7719. err = -ESRCH;
  7720. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7721. goto bail;
  7722. }
  7723. fl->is_dma_invoke_pend = true;
  7724. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7725. mutex_lock(&fl->map_mutex);
  7726. /* Map DMA buffer on SMMU device*/
  7727. err = fastrpc_mmap_create(fl, -1, p.map->buf,
  7728. p.map->attrs, 0, p.map->size,
  7729. ADSP_MMAP_DMA_BUFFER, &map);
  7730. mutex_unlock(&fl->map_mutex);
  7731. if (err)
  7732. goto bail;
  7733. /* Map DMA buffer on DSP*/
  7734. VERIFY(err, 0 == (err = fastrpc_mmap_on_dsp(fl,
  7735. map->flags, 0, map->phys, map->size, map->refs, &raddr)));
  7736. if (err)
  7737. goto bail;
  7738. map->raddr = raddr;
  7739. p.map->v_dsp_addr = raddr;
  7740. bail:
  7741. if (err && map) {
  7742. mutex_lock(&fl->map_mutex);
  7743. fastrpc_mmap_free(map, 0);
  7744. mutex_unlock(&fl->map_mutex);
  7745. }
  7746. if (fl) {
  7747. spin_lock_irqsave(&me->hlock, irq_flags);
  7748. if (fl->file_close && fl->is_dma_invoke_pend)
  7749. complete(&fl->dma_invoke);
  7750. fl->is_dma_invoke_pend = false;
  7751. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7752. }
  7753. mutex_unlock(&fl->internal_map_mutex);
  7754. return err;
  7755. }
  7756. long fastrpc_dev_unmap_dma(struct fastrpc_device *dev, unsigned long invoke_param)
  7757. {
  7758. int err = 0;
  7759. union fastrpc_dev_param p;
  7760. struct fastrpc_file *fl = NULL;
  7761. struct fastrpc_mmap *map = NULL;
  7762. struct fastrpc_apps *me = &gfa;
  7763. unsigned long irq_flags = 0;
  7764. p.unmap = (struct fastrpc_dev_unmap_dma *)invoke_param;
  7765. spin_lock_irqsave(&me->hlock, irq_flags);
  7766. /* Verify if fastrpc device is closed*/
  7767. VERIFY(err, dev && !dev->dev_close);
  7768. if (err) {
  7769. err = -ESRCH;
  7770. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7771. return err;
  7772. }
  7773. fl = dev->fl;
  7774. /* Verify if fastrpc file is not NULL*/
  7775. if (!fl) {
  7776. err = -EBADF;
  7777. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7778. return err;
  7779. }
  7780. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7781. mutex_lock(&fl->internal_map_mutex);
  7782. spin_lock_irqsave(&me->hlock, irq_flags);
  7783. /* Verify if fastrpc file is being closed, holding device lock*/
  7784. if (fl->file_close) {
  7785. err = -ESRCH;
  7786. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7787. goto bail;
  7788. }
  7789. fl->is_dma_invoke_pend = true;
  7790. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7791. mutex_lock(&fl->map_mutex);
  7792. if (!fastrpc_mmap_find(fl, -1, p.unmap->buf, 0, 0, ADSP_MMAP_DMA_BUFFER, 0, &map)) {
  7793. mutex_unlock(&fl->map_mutex);
  7794. if (err)
  7795. goto bail;
  7796. /* Un-map DMA buffer on DSP*/
  7797. VERIFY(err, !(err = fastrpc_munmap_on_dsp(fl, map->raddr,
  7798. map->phys, map->size, map->flags)));
  7799. if (err)
  7800. goto bail;
  7801. mutex_lock(&fl->map_mutex);
  7802. fastrpc_mmap_free(map, 0);
  7803. }
  7804. mutex_unlock(&fl->map_mutex);
  7805. bail:
  7806. if (fl) {
  7807. spin_lock_irqsave(&me->hlock, irq_flags);
  7808. if (fl->file_close && fl->is_dma_invoke_pend)
  7809. complete(&fl->dma_invoke);
  7810. fl->is_dma_invoke_pend = false;
  7811. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7812. }
  7813. mutex_unlock(&fl->internal_map_mutex);
  7814. return err;
  7815. }
  7816. long fastrpc_dev_get_hlos_pid(struct fastrpc_device *dev, unsigned long invoke_param)
  7817. {
  7818. int err = 0;
  7819. union fastrpc_dev_param p;
  7820. struct fastrpc_file *fl = NULL;
  7821. struct fastrpc_apps *me = &gfa;
  7822. unsigned long irq_flags = 0;
  7823. p.hpid = (struct fastrpc_dev_get_hlos_pid *)invoke_param;
  7824. spin_lock_irqsave(&me->hlock, irq_flags);
  7825. /* Verify if fastrpc device is closed*/
  7826. VERIFY(err, dev && !dev->dev_close);
  7827. if (err) {
  7828. err = -ESRCH;
  7829. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7830. return err;
  7831. }
  7832. fl = dev->fl;
  7833. /* Verify if fastrpc file is not NULL*/
  7834. if (!fl) {
  7835. err = -EBADF;
  7836. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7837. return err;
  7838. }
  7839. p.hpid->hlos_pid = fl->tgid;
  7840. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7841. return err;
  7842. }
  7843. long fastrpc_driver_invoke(struct fastrpc_device *dev, unsigned int invoke_num,
  7844. unsigned long invoke_param)
  7845. {
  7846. int err = 0;
  7847. switch (invoke_num) {
  7848. case FASTRPC_DEV_MAP_DMA:
  7849. err = fastrpc_dev_map_dma(dev, invoke_param);
  7850. break;
  7851. case FASTRPC_DEV_UNMAP_DMA:
  7852. err = fastrpc_dev_unmap_dma(dev, invoke_param);
  7853. break;
  7854. case FASTRPC_DEV_GET_HLOS_PID:
  7855. err = fastrpc_dev_get_hlos_pid(dev, invoke_param);
  7856. break;
  7857. default:
  7858. err = -ENOTTY;
  7859. break;
  7860. }
  7861. return err;
  7862. }
  7863. EXPORT_SYMBOL(fastrpc_driver_invoke);
  7864. static struct device fastrpc_bus = {
  7865. .init_name = "fastrpc"
  7866. };
  7867. static int fastrpc_bus_match(struct device *dev, struct device_driver *driver)
  7868. {
  7869. struct fastrpc_apps *me = &gfa;
  7870. struct fastrpc_driver *frpc_driver = to_fastrpc_driver(driver);
  7871. struct fastrpc_device *frpc_device = to_fastrpc_device(dev);
  7872. unsigned long irq_flags = 0;
  7873. if (frpc_device->handle == frpc_driver->handle) {
  7874. spin_lock_irqsave(&me->hlock, irq_flags);
  7875. /* If device is being closed, fail the match */
  7876. if (frpc_device->dev_close) {
  7877. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7878. return 0;
  7879. }
  7880. frpc_device->refs++;
  7881. frpc_driver->device = dev;
  7882. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7883. return 1;
  7884. }
  7885. return 0;
  7886. }
  7887. static int fastrpc_bus_probe(struct device *dev)
  7888. {
  7889. struct fastrpc_device *frpc_dev = to_fastrpc_device(dev);
  7890. struct fastrpc_driver *frpc_drv = to_fastrpc_driver(dev->driver);
  7891. if (frpc_drv && frpc_drv->probe)
  7892. return frpc_drv->probe(frpc_dev);
  7893. return 0;
  7894. }
  7895. static void fastrpc_bus_remove(struct device *dev)
  7896. {
  7897. struct fastrpc_driver *frpc_drv = to_fastrpc_driver(dev->driver);
  7898. if (frpc_drv && frpc_drv->callback)
  7899. frpc_drv->callback(to_fastrpc_device(dev), FASTRPC_PROC_DOWN);
  7900. }
  7901. static struct bus_type fastrpc_bus_type = {
  7902. .name = "fastrpc",
  7903. .match = fastrpc_bus_match,
  7904. .probe = fastrpc_bus_probe,
  7905. .remove = fastrpc_bus_remove,
  7906. };
  7907. static void fastrpc_dev_release(struct device *dev)
  7908. {
  7909. kfree(to_fastrpc_device(dev));
  7910. }
  7911. static int fastrpc_device_create(struct fastrpc_file *fl)
  7912. {
  7913. int err = 0;
  7914. struct fastrpc_device *frpc_dev;
  7915. struct fastrpc_apps *me = &gfa;
  7916. unsigned long irq_flags = 0;
  7917. frpc_dev = kzalloc(sizeof(*frpc_dev), GFP_KERNEL);
  7918. if (!frpc_dev) {
  7919. err = -ENOMEM;
  7920. goto bail;
  7921. }
  7922. frpc_dev->dev.parent = &fastrpc_bus;
  7923. frpc_dev->dev.bus = &fastrpc_bus_type;
  7924. /* Use HLOS PID, unique fastrpc process ID and CID to create device file,
  7925. * Else names would conflict for multiple sessions
  7926. * And also for better ability to debug
  7927. */
  7928. dev_set_name(&frpc_dev->dev, "%s-%d-%d-%d",
  7929. dev_name(frpc_dev->dev.parent), fl->tgid, fl->tgid_frpc, fl->cid);
  7930. frpc_dev->dev.release = fastrpc_dev_release;
  7931. frpc_dev->fl = fl;
  7932. /* Use unique fastrpc tgid as handle */
  7933. frpc_dev->handle = fl->tgid_frpc;
  7934. err = device_register(&frpc_dev->dev);
  7935. if (err) {
  7936. put_device(&frpc_dev->dev);
  7937. ADSPRPC_ERR(
  7938. "fastrpc device register failed for process %d unique fastrpc tgid %d session %d with error %d\n",
  7939. fl->tgid, fl->tgid_frpc, fl->sessionid, err);
  7940. goto bail;
  7941. }
  7942. fl->device = frpc_dev;
  7943. spin_lock_irqsave(&me->hlock, irq_flags);
  7944. hlist_add_head(&frpc_dev->hn, &me->frpc_devices);
  7945. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7946. bail:
  7947. return err;
  7948. }
  7949. void fastrpc_driver_unregister(struct fastrpc_driver *frpc_driver)
  7950. {
  7951. struct fastrpc_apps *me = &gfa;
  7952. struct device *dev = NULL;
  7953. struct fastrpc_device *frpc_dev = NULL;
  7954. bool is_device_closed = false;
  7955. unsigned long irq_flags = 0;
  7956. spin_lock_irqsave(&me->hlock, irq_flags);
  7957. dev = frpc_driver->device;
  7958. if (dev) {
  7959. frpc_dev = to_fastrpc_device(dev);
  7960. if (frpc_dev->refs > 0)
  7961. frpc_dev->refs--;
  7962. else
  7963. ADSPRPC_ERR("Fastrpc device for driver %s is already freed\n",
  7964. frpc_driver->driver.name);
  7965. if (frpc_dev->dev_close) {
  7966. hlist_del_init(&frpc_dev->hn);
  7967. is_device_closed = true;
  7968. }
  7969. }
  7970. hlist_del_init(&frpc_driver->hn);
  7971. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7972. if (is_device_closed) {
  7973. ADSPRPC_INFO("un-registering fastrpc device with handle %d\n",
  7974. frpc_dev->handle);
  7975. device_unregister(dev);
  7976. }
  7977. driver_unregister(&frpc_driver->driver);
  7978. ADSPRPC_INFO("Un-registering fastrpc driver %s with handle %d\n",
  7979. frpc_driver->driver.name, frpc_driver->handle);
  7980. }
  7981. EXPORT_SYMBOL(fastrpc_driver_unregister);
  7982. int fastrpc_driver_register(struct fastrpc_driver *frpc_driver)
  7983. {
  7984. int err = 0;
  7985. struct fastrpc_apps *me = &gfa;
  7986. unsigned long irq_flags = 0;
  7987. frpc_driver->driver.bus = &fastrpc_bus_type;
  7988. frpc_driver->driver.owner = THIS_MODULE;
  7989. err = driver_register(&frpc_driver->driver);
  7990. if (err) {
  7991. ADSPRPC_ERR("fastrpc driver %s failed to register with error %d\n",
  7992. frpc_driver->driver.name, err);
  7993. goto bail;
  7994. }
  7995. ADSPRPC_INFO("fastrpc driver %s registered with handle %d\n",
  7996. frpc_driver->driver.name, frpc_driver->handle);
  7997. spin_lock_irqsave(&me->hlock, irq_flags);
  7998. hlist_add_head(&frpc_driver->hn, &me->frpc_drivers);
  7999. spin_unlock_irqrestore(&me->hlock, irq_flags);
  8000. bail:
  8001. return err;
  8002. }
  8003. EXPORT_SYMBOL(fastrpc_driver_register);
  8004. static int __init fastrpc_device_init(void)
  8005. {
  8006. struct fastrpc_apps *me = &gfa;
  8007. int err = 0, i;
  8008. uintptr_t attr = 0;
  8009. dma_addr_t region_phys = 0;
  8010. void *region_vaddr = NULL;
  8011. struct fastrpc_buf *buf = NULL;
  8012. debugfs_root = debugfs_create_dir("adsprpc", NULL);
  8013. if (IS_ERR_OR_NULL(debugfs_root)) {
  8014. pr_warn("Error: %s: %s: failed to create debugfs root dir\n",
  8015. current->comm, __func__);
  8016. debugfs_remove_recursive(debugfs_root);
  8017. debugfs_root = NULL;
  8018. }
  8019. memset(me, 0, sizeof(*me));
  8020. fastrpc_init(me);
  8021. fastrpc_get_nsp_status(me);
  8022. me->dev = NULL;
  8023. me->legacy_remote_heap = false;
  8024. err = bus_register(&fastrpc_bus_type);
  8025. if (err) {
  8026. ADSPRPC_ERR("fastrpc bus register failed with err %d\n",
  8027. err);
  8028. goto bus_register_bail;
  8029. }
  8030. err = device_register(&fastrpc_bus);
  8031. if (err) {
  8032. ADSPRPC_ERR("fastrpc bus device register failed with err %d\n",
  8033. err);
  8034. goto bus_device_register_bail;
  8035. }
  8036. me->fastrpc_bus_register = true;
  8037. fastrpc_lowest_capacity_corecount(me);
  8038. VERIFY(err, 0 == platform_driver_register(&fastrpc_driver));
  8039. if (err)
  8040. goto register_bail;
  8041. VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, NUM_CHANNELS,
  8042. DEVICE_NAME));
  8043. if (err)
  8044. goto alloc_chrdev_bail;
  8045. cdev_init(&me->cdev, &fops);
  8046. me->cdev.owner = THIS_MODULE;
  8047. VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0),
  8048. NUM_DEVICES));
  8049. if (err)
  8050. goto cdev_init_bail;
  8051. me->class = class_create(THIS_MODULE, "fastrpc");
  8052. VERIFY(err, !IS_ERR(me->class));
  8053. if (err)
  8054. goto class_create_bail;
  8055. me->compat = (fops.compat_ioctl == NULL) ? 0 : 1;
  8056. /*
  8057. * Create devices and register with sysfs
  8058. * Create first device with minor number 0
  8059. */
  8060. me->non_secure_dev = device_create(me->class, NULL,
  8061. MKDEV(MAJOR(me->dev_no), MINOR_NUM_DEV),
  8062. NULL, DEVICE_NAME);
  8063. VERIFY(err, !IS_ERR_OR_NULL(me->non_secure_dev));
  8064. if (err) {
  8065. err = -ENODEV;
  8066. goto device_create_bail;
  8067. }
  8068. /* Create secure device with minor number for secure device */
  8069. me->secure_dev = device_create(me->class, NULL,
  8070. MKDEV(MAJOR(me->dev_no), MINOR_NUM_SECURE_DEV),
  8071. NULL, DEVICE_NAME_SECURE);
  8072. VERIFY(err, !IS_ERR_OR_NULL(me->secure_dev));
  8073. if (err)
  8074. goto device_create_bail;
  8075. for (i = 0; i < NUM_CHANNELS; i++) {
  8076. me->jobid[i] = 1;
  8077. me->channel[i].dev = me->secure_dev;
  8078. me->channel[i].ssrcount = 0;
  8079. me->channel[i].in_hib = 0;
  8080. me->channel[i].prevssrcount = 0;
  8081. me->channel[i].subsystemstate = SUBSYSTEM_UP;
  8082. me->channel[i].rh_dump_dev = NULL;
  8083. me->channel[i].nb.notifier_call = fastrpc_restart_notifier_cb;
  8084. me->channel[i].handle = qcom_register_ssr_notifier(
  8085. gcinfo[i].subsys,
  8086. &me->channel[i].nb);
  8087. if (i == CDSP_DOMAIN_ID) {
  8088. me->channel[i].dev = me->non_secure_dev;
  8089. #if !IS_ENABLED(CONFIG_MSM_ADSPRPC_TRUSTED)
  8090. err = fastrpc_alloc_cma_memory(&region_phys,
  8091. &region_vaddr,
  8092. MINI_DUMP_DBG_SIZE,
  8093. (unsigned long)attr);
  8094. #endif
  8095. if (err)
  8096. ADSPRPC_WARN("%s: CMA alloc failed err 0x%x\n",
  8097. __func__, err);
  8098. VERIFY(err, NULL != (buf = kzalloc(sizeof(*buf), GFP_KERNEL)));
  8099. if (err) {
  8100. err = -ENOMEM;
  8101. ADSPRPC_ERR("%s: CMA alloc failed err 0x%x\n",
  8102. __func__, err);
  8103. goto device_create_bail;
  8104. }
  8105. INIT_HLIST_NODE(&buf->hn);
  8106. buf->virt = region_vaddr;
  8107. buf->phys = (uintptr_t)region_phys;
  8108. buf->size = MINI_DUMP_DBG_SIZE;
  8109. buf->dma_attr = attr;
  8110. buf->raddr = 0;
  8111. ktime_get_real_ts64(&buf->buf_start_time);
  8112. me->channel[i].buf = buf;
  8113. }
  8114. if (IS_ERR_OR_NULL(me->channel[i].handle))
  8115. pr_warn("adsprpc: %s: SSR notifier register failed for %s with err %d\n",
  8116. __func__, gcinfo[i].subsys,
  8117. PTR_ERR(me->channel[i].handle));
  8118. else
  8119. pr_info("adsprpc: %s: SSR notifier registered for %s\n",
  8120. __func__, gcinfo[i].subsys);
  8121. }
  8122. err = fastrpc_transport_init();
  8123. if (err)
  8124. goto device_create_bail;
  8125. me->transport_initialized = 1;
  8126. #ifdef CONFIG_HIBERNATION
  8127. err = register_pm_notifier(&fastrpc_notif_block);
  8128. if (err)
  8129. goto device_create_bail;
  8130. #endif
  8131. fastrpc_register_wakeup_source(me->non_secure_dev,
  8132. FASTRPC_NON_SECURE_WAKE_SOURCE_CLIENT_NAME,
  8133. &me->wake_source);
  8134. fastrpc_register_wakeup_source(me->secure_dev,
  8135. FASTRPC_SECURE_WAKE_SOURCE_CLIENT_NAME,
  8136. &me->wake_source_secure);
  8137. return 0;
  8138. device_create_bail:
  8139. for (i = 0; i < NUM_CHANNELS; i++) {
  8140. if (me->channel[i].handle)
  8141. qcom_unregister_ssr_notifier(me->channel[i].handle,
  8142. &me->channel[i].nb);
  8143. }
  8144. if (!IS_ERR_OR_NULL(me->non_secure_dev))
  8145. device_destroy(me->class, MKDEV(MAJOR(me->dev_no),
  8146. MINOR_NUM_DEV));
  8147. if (!IS_ERR_OR_NULL(me->secure_dev))
  8148. device_destroy(me->class, MKDEV(MAJOR(me->dev_no),
  8149. MINOR_NUM_SECURE_DEV));
  8150. class_destroy(me->class);
  8151. class_create_bail:
  8152. cdev_del(&me->cdev);
  8153. cdev_init_bail:
  8154. unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
  8155. alloc_chrdev_bail:
  8156. platform_driver_unregister(&fastrpc_driver);
  8157. register_bail:
  8158. device_unregister(&fastrpc_bus);
  8159. bus_device_register_bail:
  8160. bus_unregister(&fastrpc_bus_type);
  8161. bus_register_bail:
  8162. fastrpc_deinit();
  8163. return err;
  8164. }
  8165. static void __exit fastrpc_device_exit(void)
  8166. {
  8167. struct fastrpc_apps *me = &gfa;
  8168. int i;
  8169. fastrpc_file_list_dtor(me);
  8170. fastrpc_deinit();
  8171. wakeup_source_unregister(me->wake_source);
  8172. wakeup_source_unregister(me->wake_source_secure);
  8173. for (i = 0; i < NUM_CHANNELS; i++) {
  8174. if (i == CDSP_DOMAIN_ID)
  8175. kfree(me->channel[i].buf);
  8176. if (!gcinfo[i].name)
  8177. continue;
  8178. qcom_unregister_ssr_notifier(me->channel[i].handle,
  8179. &me->channel[i].nb);
  8180. }
  8181. /* Destroy the secure and non secure devices */
  8182. device_destroy(me->class, MKDEV(MAJOR(me->dev_no), MINOR_NUM_DEV));
  8183. device_destroy(me->class, MKDEV(MAJOR(me->dev_no),
  8184. MINOR_NUM_SECURE_DEV));
  8185. of_reserved_mem_device_release(me->dev);
  8186. class_destroy(me->class);
  8187. cdev_del(&me->cdev);
  8188. unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
  8189. if (me->transport_initialized)
  8190. fastrpc_transport_deinit();
  8191. me->transport_initialized = 0;
  8192. if (me->fastrpc_bus_register) {
  8193. bus_unregister(&fastrpc_bus_type);
  8194. device_unregister(&fastrpc_bus);
  8195. }
  8196. kfree(me->gidlist.gids);
  8197. debugfs_remove_recursive(debugfs_root);
  8198. }
  8199. module_init(fastrpc_device_init);
  8200. module_exit(fastrpc_device_exit);
  8201. MODULE_LICENSE("GPL v2");