adsprpc.c 224 KB

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