adsprpc.c 240 KB

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