adsprpc.c 210 KB

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