adsprpc.c 241 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012
  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, irq_flags = 0;
  3598. remote_arg_t ra[6];
  3599. int fds[6];
  3600. struct fastrpc_apps *me = &gfa;
  3601. struct hlist_node *n = NULL;
  3602. struct fastrpc_file *fl_curr = NULL;
  3603. unsigned int gid = 0, one_mb = 1024*1024;
  3604. unsigned int dsp_userpd_memlen = 0;
  3605. struct fastrpc_buf *init_mem;
  3606. struct fastrpc_mmap *sharedbuf_map = NULL;
  3607. struct {
  3608. int pgid;
  3609. unsigned int namelen;
  3610. unsigned int filelen;
  3611. unsigned int pageslen;
  3612. int attrs;
  3613. int siglen;
  3614. } inbuf;
  3615. spin_lock(&fl->hlock);
  3616. if (fl->dsp_process_state) {
  3617. err = -EALREADY;
  3618. ADSPRPC_ERR("Already in create dynamic process\n");
  3619. spin_unlock(&fl->hlock);
  3620. return err;
  3621. }
  3622. fl->dsp_process_state = PROCESS_CREATE_IS_INPROGRESS;
  3623. if (init->memlen) {
  3624. if(init->memlen > INIT_MEMLEN_MAX_DYNAMIC || init->memlen < INIT_MEMLEN_MIN_DYNAMIC) {
  3625. ADSPRPC_ERR(
  3626. "init memory for process %d should be between %d and %d\n",
  3627. init->memlen, INIT_MEMLEN_MIN_DYNAMIC, INIT_MEMLEN_MAX_DYNAMIC);
  3628. err = -EINVAL;
  3629. spin_unlock(&fl->hlock);
  3630. goto bail;
  3631. }
  3632. dsp_userpd_memlen = init->memlen;
  3633. } else {
  3634. dsp_userpd_memlen = 3*one_mb;
  3635. }
  3636. spin_unlock(&fl->hlock);
  3637. inbuf.pgid = fl->tgid_frpc;
  3638. inbuf.namelen = strlen(current->comm) + 1;
  3639. inbuf.filelen = init->filelen;
  3640. fl->pd = FASTRPC_USER_PD;
  3641. if (uproc->attrs & FASTRPC_MODE_UNSIGNED_MODULE)
  3642. fl->is_unsigned_pd = true;
  3643. /* Validate that any existing sessions of process are of same pd type */
  3644. spin_lock_irqsave(&me->hlock, irq_flags);
  3645. hlist_for_each_entry_safe(fl_curr, n, &me->drivers, hn) {
  3646. if ((fl != fl_curr) && (fl->tgid == fl_curr->tgid) && (fl->cid == fl_curr->cid)) {
  3647. err = (fl->is_unsigned_pd != fl_curr->is_unsigned_pd) ? -ECONNREFUSED : 0;
  3648. break;
  3649. }
  3650. }
  3651. spin_unlock_irqrestore(&me->hlock, irq_flags);
  3652. if (err) {
  3653. ADSPRPC_ERR("existing session pd type %u not same as requested pd type %u \n",
  3654. fl_curr->is_unsigned_pd, fl->is_unsigned_pd);
  3655. goto bail;
  3656. }
  3657. /* Check if file memory passed by userspace is valid */
  3658. VERIFY(err, access_ok((void __user *)init->file, init->filelen));
  3659. if (err)
  3660. goto bail;
  3661. if (init->filelen) {
  3662. /* Map the shell file buffer to remote subsystem */
  3663. mutex_lock(&fl->map_mutex);
  3664. err = fastrpc_mmap_create(fl, init->filefd, NULL, 0,
  3665. init->file, init->filelen, mflags, &file);
  3666. if (file)
  3667. file->is_filemap = true;
  3668. mutex_unlock(&fl->map_mutex);
  3669. if (err)
  3670. goto bail;
  3671. }
  3672. inbuf.pageslen = 1;
  3673. /* Disregard any system unsigned PD attribute from userspace */
  3674. uproc->attrs &= (~FASTRPC_MODE_SYSTEM_UNSIGNED_PD);
  3675. /* Untrusted apps are not allowed to offload to signedPD on DSP. */
  3676. if (fl->untrusted_process) {
  3677. VERIFY(err, fl->is_unsigned_pd);
  3678. if (err) {
  3679. err = -ECONNREFUSED;
  3680. ADSPRPC_ERR(
  3681. "untrusted app trying to offload to signed remote process\n");
  3682. goto bail;
  3683. }
  3684. } else {
  3685. /* Trusted apps will be launched as system unsigned PDs */
  3686. if (fl->is_unsigned_pd)
  3687. uproc->attrs |= FASTRPC_MODE_SYSTEM_UNSIGNED_PD;
  3688. }
  3689. /* Disregard any privilege bits from userspace */
  3690. uproc->attrs &= (~FASTRPC_MODE_PRIVILEGED);
  3691. /*
  3692. * Check if the primary or supplementary group(s) of the process is
  3693. * one of the 'privileged' fastrpc GIDs stored in the device-tree.
  3694. */
  3695. gid = sorted_lists_intersection(fl->gidlist.gids,
  3696. fl->gidlist.gidcount, gfa.gidlist.gids, gfa.gidlist.gidcount);
  3697. if (gid) {
  3698. ADSPRPC_INFO("PID %d, GID %u is a privileged process\n",
  3699. fl->tgid, gid);
  3700. uproc->attrs |= FASTRPC_MODE_PRIVILEGED;
  3701. }
  3702. /*
  3703. * Userspace client should try to allocate the initial memory donated
  3704. * to remote subsystem as only the kernel and DSP should have access
  3705. * to that memory.
  3706. */
  3707. VERIFY(err, !init->mem);
  3708. if (err) {
  3709. err = -EINVAL;
  3710. ADSPRPC_ERR("donated memory allocated in userspace\n");
  3711. goto bail;
  3712. }
  3713. /* Free any previous donated memory */
  3714. spin_lock(&fl->hlock);
  3715. locked = 1;
  3716. if (fl->init_mem) {
  3717. init_mem = fl->init_mem;
  3718. fl->init_mem = NULL;
  3719. spin_unlock(&fl->hlock);
  3720. locked = 0;
  3721. fastrpc_buf_free(init_mem, 0);
  3722. }
  3723. if (locked) {
  3724. spin_unlock(&fl->hlock);
  3725. locked = 0;
  3726. }
  3727. /* Allocate DMA buffer in kernel for donating to remote process
  3728. * Unsigned PD requires additional memory because of the
  3729. * additional static heap initialized within the process.
  3730. */
  3731. if (fl->is_unsigned_pd)
  3732. dsp_userpd_memlen = 5*one_mb;
  3733. memlen = ALIGN(max(dsp_userpd_memlen, init->filelen * 4), one_mb);
  3734. imem_dma_attr = DMA_ATTR_DELAYED_UNMAP | DMA_ATTR_NO_KERNEL_MAPPING;
  3735. err = fastrpc_buf_alloc(fl, memlen, imem_dma_attr, 0,
  3736. INITMEM_BUF, &imem);
  3737. if (err)
  3738. goto bail;
  3739. fl->init_mem = imem;
  3740. inbuf.pageslen = 1;
  3741. if ((fl->sharedbuf_info.buf_fd != -1) && fl->sharedbuf_info.buf_size) {
  3742. mutex_lock(&fl->map_mutex);
  3743. err = fastrpc_mmap_create(fl, fl->sharedbuf_info.buf_fd, NULL, 0,
  3744. 0, fl->sharedbuf_info.buf_size, mflags, &sharedbuf_map);
  3745. mutex_unlock(&fl->map_mutex);
  3746. if (err)
  3747. goto bail;
  3748. /* if shared buff is available send this as the second page and set pageslen as 2 */
  3749. inbuf.pageslen = PAGESLEN_WITH_SHAREDBUF;
  3750. }
  3751. /*
  3752. * Prepare remote arguments for dynamic process create
  3753. * call to remote subsystem.
  3754. */
  3755. ra[0].buf.pv = (void *)&inbuf;
  3756. ra[0].buf.len = sizeof(inbuf);
  3757. fds[0] = -1;
  3758. ra[1].buf.pv = (void *)current->comm;
  3759. ra[1].buf.len = inbuf.namelen;
  3760. fds[1] = -1;
  3761. ra[2].buf.pv = (void *)init->file;
  3762. ra[2].buf.len = inbuf.filelen;
  3763. fds[2] = init->filefd;
  3764. pages[0].addr = imem->phys;
  3765. pages[0].size = imem->size;
  3766. /* Update IOVA of second page shared with DSP */
  3767. if (inbuf.pageslen > 1) {
  3768. pages[1].addr = sharedbuf_map->phys;
  3769. pages[1].size = sharedbuf_map->size;
  3770. }
  3771. ra[3].buf.pv = (void *)pages;
  3772. ra[3].buf.len = (inbuf.pageslen) * sizeof(*pages);
  3773. fds[3] = -1;
  3774. inbuf.attrs = uproc->attrs;
  3775. ra[4].buf.pv = (void *)&(inbuf.attrs);
  3776. ra[4].buf.len = sizeof(inbuf.attrs);
  3777. fds[4] = -1;
  3778. inbuf.siglen = uproc->siglen;
  3779. ra[5].buf.pv = (void *)&(inbuf.siglen);
  3780. ra[5].buf.len = sizeof(inbuf.siglen);
  3781. fds[5] = -1;
  3782. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
  3783. /*
  3784. * Choose appropriate remote method ID depending on whether the
  3785. * HLOS process has any attributes enabled (like unsignedPD,
  3786. * critical process, adaptive QoS, CRC checks etc).
  3787. */
  3788. ioctl.inv.sc = REMOTE_SCALARS_MAKE(6, 4, 0);
  3789. if (uproc->attrs)
  3790. ioctl.inv.sc = REMOTE_SCALARS_MAKE(7, 4, 0);
  3791. ioctl.inv.pra = ra;
  3792. ioctl.fds = fds;
  3793. ioctl.attrs = NULL;
  3794. ioctl.crc = NULL;
  3795. ioctl.perf_kernel = NULL;
  3796. ioctl.perf_dsp = NULL;
  3797. ioctl.job = NULL;
  3798. err = fastrpc_internal_invoke(fl, FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl);
  3799. if (err)
  3800. goto bail;
  3801. bail:
  3802. /*
  3803. * Shell is loaded into the donated memory on remote subsystem. So, the
  3804. * original file buffer can be DMA unmapped. In case of a failure also,
  3805. * the mapping needs to be removed.
  3806. */
  3807. if (file) {
  3808. mutex_lock(&fl->map_mutex);
  3809. fastrpc_mmap_free(file, 0);
  3810. mutex_unlock(&fl->map_mutex);
  3811. }
  3812. spin_lock(&fl->hlock);
  3813. locked = 1;
  3814. if (err) {
  3815. ADSPRPC_ERR("failed with err %d\n", err);
  3816. fl->dsp_process_state = PROCESS_CREATE_DEFAULT;
  3817. if (!IS_ERR_OR_NULL(fl->init_mem)) {
  3818. init_mem = fl->init_mem;
  3819. fl->init_mem = NULL;
  3820. spin_unlock(&fl->hlock);
  3821. locked = 0;
  3822. fastrpc_buf_free(init_mem, 0);
  3823. }
  3824. } else {
  3825. fl->dsp_process_state = PROCESS_CREATE_SUCCESS;
  3826. }
  3827. if (locked) {
  3828. spin_unlock(&fl->hlock);
  3829. locked = 0;
  3830. }
  3831. return err;
  3832. }
  3833. /*
  3834. * This function makes a call to create a thread group in the static
  3835. * process on the remote subsystem.
  3836. * Example: audio daemon 'adsprpcd' on audioPD on ADSP
  3837. */
  3838. static int fastrpc_init_create_static_process(struct fastrpc_file *fl,
  3839. struct fastrpc_ioctl_init *init)
  3840. {
  3841. int err = 0, rh_hyp_done = 0;
  3842. struct fastrpc_apps *me = &gfa;
  3843. struct fastrpc_ioctl_invoke_async ioctl;
  3844. struct smq_phy_page pages[1];
  3845. struct fastrpc_mmap *mem = NULL;
  3846. char *proc_name = NULL;
  3847. remote_arg_t ra[3];
  3848. uint64_t phys = 0;
  3849. size_t size = 0;
  3850. int fds[3];
  3851. struct secure_vm *rhvm = &me->channel[fl->cid].rhvm;
  3852. struct {
  3853. int pgid;
  3854. unsigned int namelen;
  3855. unsigned int pageslen;
  3856. } inbuf;
  3857. unsigned long irq_flags = 0;
  3858. if (fl->dev_minor == MINOR_NUM_DEV) {
  3859. err = -ECONNREFUSED;
  3860. ADSPRPC_ERR(
  3861. "untrusted app trying to attach to audio PD\n");
  3862. return err;
  3863. }
  3864. VERIFY(err, init->memlen <= INIT_MEMLEN_MAX_STATIC);
  3865. if (err) {
  3866. ADSPRPC_ERR(
  3867. "init memory for static process %d is more than max allowed init len %d\n",
  3868. init->memlen, INIT_MEMLEN_MAX_STATIC);
  3869. err = -EFBIG;
  3870. goto bail;
  3871. }
  3872. if (!init->filelen)
  3873. goto bail;
  3874. proc_name = kzalloc(init->filelen + 1, GFP_KERNEL);
  3875. VERIFY(err, !IS_ERR_OR_NULL(proc_name));
  3876. if (err) {
  3877. err = -ENOMEM;
  3878. goto bail;
  3879. }
  3880. err = copy_from_user((void *)proc_name,
  3881. (void __user *)init->file, init->filelen);
  3882. if (err) {
  3883. err = -EFAULT;
  3884. goto bail;
  3885. }
  3886. fl->pd = FASTRPC_USER_PD;
  3887. inbuf.pgid = fl->tgid_frpc;
  3888. inbuf.namelen = init->filelen;
  3889. inbuf.pageslen = 0;
  3890. if (!strcmp(proc_name, "audiopd")) {
  3891. /*
  3892. * Remove any previous mappings in case process is trying
  3893. * to reconnect after a PD restart on remote subsystem.
  3894. */
  3895. err = fastrpc_mmap_remove_pdr(fl);
  3896. if (err)
  3897. goto bail;
  3898. } else {
  3899. ADSPRPC_ERR(
  3900. "Create static process is failed for proc_name %s",
  3901. proc_name);
  3902. goto bail;
  3903. }
  3904. if ((!me->staticpd_flags && !me->legacy_remote_heap)) {
  3905. inbuf.pageslen = 1;
  3906. if (!fastrpc_get_persistent_map(init->memlen, &mem)) {
  3907. mutex_lock(&fl->map_mutex);
  3908. err = fastrpc_mmap_create(fl, -1, NULL, 0, init->mem,
  3909. init->memlen, ADSP_MMAP_REMOTE_HEAP_ADDR, &mem);
  3910. mutex_unlock(&fl->map_mutex);
  3911. if (err || (!mem))
  3912. goto bail;
  3913. spin_lock_irqsave(&me->hlock, irq_flags);
  3914. mem->in_use = true;
  3915. spin_unlock_irqrestore(&me->hlock, irq_flags);
  3916. }
  3917. VERIFY(err, mem);
  3918. if (err)
  3919. goto bail;
  3920. phys = mem->phys;
  3921. size = mem->size;
  3922. /*
  3923. * If remote-heap VMIDs are defined in DTSI, then do
  3924. * hyp_assign from HLOS to those VMs (LPASS, ADSP).
  3925. */
  3926. if (rhvm->vmid && mem->refs == 1 && size) {
  3927. u64 src_perms = BIT(QCOM_SCM_VMID_HLOS);
  3928. struct qcom_scm_vmperm *dst_perms;
  3929. uint32_t i = 0;
  3930. VERIFY(err, NULL != (dst_perms = kcalloc(rhvm->vmcount,
  3931. sizeof(struct qcom_scm_vmperm), GFP_KERNEL)));
  3932. if (err)
  3933. goto bail;
  3934. for (i = 0; i < rhvm->vmcount; i++) {
  3935. dst_perms[i].vmid = rhvm->vmid[i];
  3936. dst_perms[i].perm = rhvm->vmperm[i];
  3937. }
  3938. err = qcom_scm_assign_mem(phys, (uint64_t)size,
  3939. &src_perms, dst_perms, rhvm->vmcount);
  3940. kfree(dst_perms);
  3941. if (err) {
  3942. ADSPRPC_ERR(
  3943. "rh hyp assign failed with %d for phys 0x%llx, size %zu\n",
  3944. err, phys, size);
  3945. err = -EADDRNOTAVAIL;
  3946. goto bail;
  3947. }
  3948. rh_hyp_done = 1;
  3949. }
  3950. me->staticpd_flags = 1;
  3951. mem->is_persistent = true;
  3952. }
  3953. /*
  3954. * Prepare remote arguments for static process create
  3955. * call to remote subsystem.
  3956. */
  3957. ra[0].buf.pv = (void *)&inbuf;
  3958. ra[0].buf.len = sizeof(inbuf);
  3959. fds[0] = -1;
  3960. ra[1].buf.pv = (void *)proc_name;
  3961. ra[1].buf.len = inbuf.namelen;
  3962. fds[1] = -1;
  3963. pages[0].addr = phys;
  3964. pages[0].size = size;
  3965. ra[2].buf.pv = (void *)pages;
  3966. ra[2].buf.len = sizeof(*pages);
  3967. fds[2] = -1;
  3968. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
  3969. ioctl.inv.sc = REMOTE_SCALARS_MAKE(8, 3, 0);
  3970. ioctl.inv.pra = ra;
  3971. ioctl.fds = NULL;
  3972. ioctl.attrs = NULL;
  3973. ioctl.crc = NULL;
  3974. ioctl.perf_kernel = NULL;
  3975. ioctl.perf_dsp = NULL;
  3976. ioctl.job = NULL;
  3977. err = fastrpc_internal_invoke(fl, FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl);
  3978. if (err)
  3979. goto bail;
  3980. bail:
  3981. kfree(proc_name);
  3982. if (err) {
  3983. me->staticpd_flags = 0;
  3984. if (rh_hyp_done) {
  3985. int hyp_err = 0;
  3986. u64 src_perms = 0;
  3987. struct qcom_scm_vmperm dst_perms;
  3988. uint32_t i = 0;
  3989. for (i = 0; i < rhvm->vmcount; i++) {
  3990. src_perms |= BIT(rhvm->vmid[i]);
  3991. }
  3992. dst_perms.vmid = QCOM_SCM_VMID_HLOS;
  3993. dst_perms.perm = QCOM_SCM_PERM_RWX;
  3994. /* Assign memory back to HLOS in case of errors */
  3995. hyp_err = qcom_scm_assign_mem(phys, (uint64_t)size,
  3996. &src_perms, &dst_perms, 1);
  3997. if (hyp_err)
  3998. ADSPRPC_WARN(
  3999. "rh hyp unassign failed with %d for phys 0x%llx of size %zu\n",
  4000. hyp_err, phys, size);
  4001. }
  4002. mutex_lock(&fl->map_mutex);
  4003. fastrpc_mmap_free(mem, 0);
  4004. mutex_unlock(&fl->map_mutex);
  4005. }
  4006. return err;
  4007. }
  4008. /*
  4009. * This function sets fastrpc service location name
  4010. * based on ioctl init flags.
  4011. */
  4012. static void fastrpc_set_servloc(struct fastrpc_file *fl,
  4013. struct fastrpc_ioctl_init *init)
  4014. {
  4015. char *proc_name = NULL;
  4016. int err = 0;
  4017. if (init->flags == FASTRPC_INIT_ATTACH_SENSORS) {
  4018. if (fl->cid == ADSP_DOMAIN_ID)
  4019. fl->servloc_name =
  4020. SENSORS_PDR_ADSP_SERVICE_LOCATION_CLIENT_NAME;
  4021. else if (fl->cid == SDSP_DOMAIN_ID)
  4022. fl->servloc_name =
  4023. SENSORS_PDR_SLPI_SERVICE_LOCATION_CLIENT_NAME;
  4024. } else if (init->flags == FASTRPC_INIT_CREATE_STATIC) {
  4025. if (!init->filelen)
  4026. goto bail;
  4027. proc_name = kzalloc(init->filelen + 1, GFP_KERNEL);
  4028. VERIFY(err, !IS_ERR_OR_NULL(proc_name));
  4029. if (err) {
  4030. err = -ENOMEM;
  4031. goto bail;
  4032. }
  4033. err = copy_from_user((void *)proc_name,
  4034. (void __user *)init->file, init->filelen);
  4035. if (err) {
  4036. err = -EFAULT;
  4037. goto bail;
  4038. }
  4039. if (!strcmp(proc_name, "audiopd"))
  4040. fl->servloc_name = AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME;
  4041. }
  4042. bail:
  4043. kfree(proc_name);
  4044. }
  4045. int fastrpc_init_process(struct fastrpc_file *fl,
  4046. struct fastrpc_ioctl_init_attrs *uproc)
  4047. {
  4048. int err = 0;
  4049. struct fastrpc_ioctl_init *init = &uproc->init;
  4050. int cid = fl->cid;
  4051. struct fastrpc_apps *me = &gfa;
  4052. struct fastrpc_channel_ctx *chan = NULL;
  4053. VERIFY(err, init->filelen < INIT_FILELEN_MAX
  4054. && init->memlen <= INIT_MEMLEN_MAX_DYNAMIC);
  4055. if (err) {
  4056. ADSPRPC_ERR(
  4057. "file size 0x%x or init memory 0x%x is more than max allowed file size 0x%x or init len 0x%x\n",
  4058. init->filelen, init->memlen,
  4059. INIT_FILELEN_MAX, INIT_MEMLEN_MAX_DYNAMIC);
  4060. err = -EFBIG;
  4061. goto bail;
  4062. }
  4063. VERIFY(err, VALID_FASTRPC_CID(cid));
  4064. if (err) {
  4065. err = -ECHRNG;
  4066. goto bail;
  4067. }
  4068. chan = &me->channel[cid];
  4069. if (chan->unsigned_support && fl->dev_minor == MINOR_NUM_DEV) {
  4070. /* Make sure third party applications */
  4071. /* can spawn only unsigned PD when */
  4072. /* channel configured as secure. */
  4073. if (chan->secure && !(fl->is_unsigned_pd)) {
  4074. err = -ECONNREFUSED;
  4075. goto bail;
  4076. }
  4077. }
  4078. if (fl->sharedcb == 1) {
  4079. // Only attach sensors pd use cases can share CB
  4080. VERIFY(err, init->flags == FASTRPC_INIT_ATTACH_SENSORS);
  4081. if (err) {
  4082. err = -EACCES;
  4083. goto bail;
  4084. }
  4085. }
  4086. fastrpc_set_servloc(fl, init);
  4087. err = fastrpc_set_tvm_remote_domain(fl, init);
  4088. if (err)
  4089. goto bail;
  4090. err = fastrpc_channel_open(fl, init->flags);
  4091. if (err)
  4092. goto bail;
  4093. fl->proc_flags = init->flags;
  4094. switch (init->flags) {
  4095. case FASTRPC_INIT_ATTACH:
  4096. case FASTRPC_INIT_ATTACH_SENSORS:
  4097. err = fastrpc_init_attach_process(fl, init);
  4098. break;
  4099. case FASTRPC_INIT_CREATE:
  4100. err = fastrpc_init_create_dynamic_process(fl, uproc);
  4101. break;
  4102. case FASTRPC_INIT_CREATE_STATIC:
  4103. err = fastrpc_init_create_static_process(fl, init);
  4104. break;
  4105. default:
  4106. err = -ENOTTY;
  4107. break;
  4108. }
  4109. if (err)
  4110. goto bail;
  4111. fl->dsp_proc_init = 1;
  4112. VERIFY(err, 0 == (err = fastrpc_device_create(fl)));
  4113. if (err)
  4114. goto bail;
  4115. bail:
  4116. return err;
  4117. }
  4118. static int fastrpc_send_cpuinfo_to_dsp(struct fastrpc_file *fl)
  4119. {
  4120. int err = 0;
  4121. uint64_t cpuinfo = 0;
  4122. struct fastrpc_apps *me = &gfa;
  4123. struct fastrpc_ioctl_invoke_async ioctl;
  4124. remote_arg_t ra[1];
  4125. int cid = -1;
  4126. if (!fl) {
  4127. err = -EBADF;
  4128. goto bail;
  4129. }
  4130. cid = fl->cid;
  4131. VERIFY(err, VALID_FASTRPC_CID(cid));
  4132. if (err) {
  4133. err = -ECHRNG;
  4134. ADSPRPC_ERR(
  4135. "invalid channel 0x%zx set for session\n",
  4136. cid);
  4137. goto bail;
  4138. }
  4139. cpuinfo = me->channel[cid].cpuinfo_todsp;
  4140. /* return success if already updated to remote processor */
  4141. if (me->channel[cid].cpuinfo_status)
  4142. return 0;
  4143. ra[0].buf.pv = (void *)&cpuinfo;
  4144. ra[0].buf.len = sizeof(cpuinfo);
  4145. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_DSP_UTILITIES;
  4146. ioctl.inv.sc = REMOTE_SCALARS_MAKE(1, 1, 0);
  4147. ioctl.inv.pra = ra;
  4148. ioctl.fds = NULL;
  4149. ioctl.attrs = NULL;
  4150. ioctl.crc = NULL;
  4151. ioctl.perf_kernel = NULL;
  4152. ioctl.perf_dsp = NULL;
  4153. ioctl.job = NULL;
  4154. err = fastrpc_internal_invoke(fl, FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl);
  4155. if (!err)
  4156. me->channel[cid].cpuinfo_status = true;
  4157. bail:
  4158. return err;
  4159. }
  4160. int fastrpc_get_info_from_dsp(struct fastrpc_file *fl,
  4161. uint32_t *dsp_attr_buf,
  4162. uint32_t dsp_attr_buf_len,
  4163. uint32_t domain)
  4164. {
  4165. int err = 0;
  4166. struct fastrpc_ioctl_invoke_async ioctl;
  4167. remote_arg_t ra[2];
  4168. dsp_attr_buf[0] = 0; // Capability filled in userspace
  4169. // Fastrpc to modem not supported
  4170. if (domain == MDSP_DOMAIN_ID)
  4171. goto bail;
  4172. err = fastrpc_channel_open(fl, FASTRPC_INIT_NO_CREATE);
  4173. if (err)
  4174. goto bail;
  4175. ra[0].buf.pv = (void *)&dsp_attr_buf_len;
  4176. ra[0].buf.len = sizeof(dsp_attr_buf_len);
  4177. ra[1].buf.pv = (void *)(&dsp_attr_buf[1]);
  4178. ra[1].buf.len = dsp_attr_buf_len * sizeof(uint32_t);
  4179. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_DSP_UTILITIES;
  4180. ioctl.inv.sc = REMOTE_SCALARS_MAKE(0, 1, 1);
  4181. ioctl.inv.pra = ra;
  4182. ioctl.fds = NULL;
  4183. ioctl.attrs = NULL;
  4184. ioctl.crc = NULL;
  4185. ioctl.perf_kernel = NULL;
  4186. ioctl.perf_dsp = NULL;
  4187. ioctl.job = NULL;
  4188. err = fastrpc_internal_invoke(fl, FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl);
  4189. bail:
  4190. if (err)
  4191. ADSPRPC_ERR("could not obtain dsp information, err val %d\n",
  4192. err);
  4193. return err;
  4194. }
  4195. int fastrpc_get_info_from_kernel(
  4196. struct fastrpc_ioctl_capability *cap,
  4197. struct fastrpc_file *fl)
  4198. {
  4199. int err = 0;
  4200. uint32_t domain = cap->domain, attribute_ID = cap->attribute_ID;
  4201. uint32_t async_capability = 0;
  4202. struct fastrpc_dsp_capabilities *dsp_cap_ptr = NULL;
  4203. VERIFY(err, domain < NUM_CHANNELS);
  4204. if (err) {
  4205. err = -ECHRNG;
  4206. goto bail;
  4207. }
  4208. /*
  4209. * Check if number of attribute IDs obtained from userspace
  4210. * is less than the number of attribute IDs supported by
  4211. * kernel
  4212. */
  4213. if (attribute_ID >= FASTRPC_MAX_ATTRIBUTES) {
  4214. err = -EOVERFLOW;
  4215. goto bail;
  4216. }
  4217. dsp_cap_ptr = &gcinfo[domain].dsp_cap_kernel;
  4218. if (attribute_ID >= FASTRPC_MAX_DSP_ATTRIBUTES) {
  4219. // Driver capability, pass it to user
  4220. memcpy(&cap->capability,
  4221. &kernel_capabilities[attribute_ID -
  4222. FASTRPC_MAX_DSP_ATTRIBUTES],
  4223. sizeof(cap->capability));
  4224. } else if (!dsp_cap_ptr->is_cached) {
  4225. /*
  4226. * Information not on kernel, query device for information
  4227. * and cache on kernel
  4228. */
  4229. err = fastrpc_get_info_from_dsp(fl,
  4230. dsp_cap_ptr->dsp_attributes,
  4231. FASTRPC_MAX_DSP_ATTRIBUTES - 1,
  4232. domain);
  4233. if (err)
  4234. goto bail;
  4235. /* Async capability support depends on both kernel and DSP */
  4236. async_capability = IS_ASYNC_FASTRPC_AVAILABLE &&
  4237. dsp_cap_ptr->dsp_attributes[ASYNC_FASTRPC_CAP];
  4238. dsp_cap_ptr->dsp_attributes[ASYNC_FASTRPC_CAP]
  4239. = async_capability;
  4240. memcpy(&cap->capability,
  4241. &dsp_cap_ptr->dsp_attributes[attribute_ID],
  4242. sizeof(cap->capability));
  4243. dsp_cap_ptr->is_cached = 1;
  4244. } else {
  4245. // Information on Kernel, pass it to user
  4246. memcpy(&cap->capability,
  4247. &dsp_cap_ptr->dsp_attributes[attribute_ID],
  4248. sizeof(cap->capability));
  4249. }
  4250. bail:
  4251. return err;
  4252. }
  4253. static int fastrpc_release_current_dsp_process(struct fastrpc_file *fl)
  4254. {
  4255. int err = 0;
  4256. struct fastrpc_ioctl_invoke_async ioctl;
  4257. remote_arg_t ra[1];
  4258. int tgid = 0;
  4259. int cid = -1;
  4260. unsigned long irq_flags = 0;
  4261. if (!fl) {
  4262. err = -EBADF;
  4263. goto bail;
  4264. }
  4265. cid = fl->cid;
  4266. VERIFY(err, VALID_FASTRPC_CID(cid));
  4267. if (err) {
  4268. err = -ECHRNG;
  4269. goto bail;
  4270. }
  4271. VERIFY(err, fl->sctx != NULL);
  4272. if (err) {
  4273. err = -EBADR;
  4274. goto bail;
  4275. }
  4276. err = verify_transport_device(cid, fl->tvm_remote_domain);
  4277. if (err)
  4278. goto bail;
  4279. VERIFY(err, fl->apps->channel[cid].subsystemstate != SUBSYSTEM_RESTARTING);
  4280. if (err) {
  4281. wait_for_completion(&fl->shutdown);
  4282. err = -ECONNRESET;
  4283. goto bail;
  4284. }
  4285. /* Send unique fastrpc process ID to dsp */
  4286. tgid = fl->tgid_frpc;
  4287. ra[0].buf.pv = (void *)&tgid;
  4288. ra[0].buf.len = sizeof(tgid);
  4289. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
  4290. ioctl.inv.sc = REMOTE_SCALARS_MAKE(1, 1, 0);
  4291. ioctl.inv.pra = ra;
  4292. ioctl.fds = NULL;
  4293. ioctl.attrs = NULL;
  4294. ioctl.crc = NULL;
  4295. ioctl.perf_kernel = NULL;
  4296. ioctl.perf_dsp = NULL;
  4297. ioctl.job = NULL;
  4298. spin_lock_irqsave(&fl->apps->hlock, irq_flags);
  4299. fl->file_close = FASTRPC_PROCESS_DSP_EXIT_INIT;
  4300. spin_unlock_irqrestore(&fl->apps->hlock, irq_flags);
  4301. /*
  4302. * Pass 2 for "kernel" arg to send kernel msg to DSP
  4303. * with non-zero msg PID for the DSP to directly use
  4304. * that info to kill the remote process.
  4305. */
  4306. VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
  4307. FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_NONZERO_PID, &ioctl)));
  4308. spin_lock_irqsave(&fl->apps->hlock, irq_flags);
  4309. fl->file_close = FASTRPC_PROCESS_DSP_EXIT_COMPLETE;
  4310. spin_unlock_irqrestore(&fl->apps->hlock, irq_flags);
  4311. if (err && fl->dsp_proc_init)
  4312. ADSPRPC_ERR(
  4313. "releasing DSP process failed with %d (0x%x) for %s\n",
  4314. err, err, current->comm);
  4315. bail:
  4316. if (err && fl && fl->apps) {
  4317. spin_lock_irqsave(&fl->apps->hlock, irq_flags);
  4318. fl->file_close = FASTRPC_PROCESS_DSP_EXIT_ERROR;
  4319. spin_unlock_irqrestore(&fl->apps->hlock, irq_flags);
  4320. }
  4321. return err;
  4322. }
  4323. static int fastrpc_mem_map_to_dsp(struct fastrpc_file *fl, int fd, int offset,
  4324. uint32_t flags, uintptr_t va, uint64_t phys,
  4325. size_t size, uintptr_t *raddr)
  4326. {
  4327. struct fastrpc_ioctl_invoke_async ioctl;
  4328. struct smq_phy_page page;
  4329. remote_arg_t ra[4];
  4330. int err = 0;
  4331. struct {
  4332. int pid;
  4333. int fd;
  4334. int offset;
  4335. uint32_t flags;
  4336. uint64_t vaddrin;
  4337. int num;
  4338. int data_len;
  4339. } inargs;
  4340. struct {
  4341. uint64_t vaddrout;
  4342. } routargs;
  4343. /* Send unique fastrpc process ID to dsp */
  4344. inargs.pid = fl->tgid_frpc;
  4345. inargs.fd = fd;
  4346. inargs.offset = offset;
  4347. inargs.vaddrin = (uintptr_t)va;
  4348. inargs.flags = flags;
  4349. inargs.num = sizeof(page);
  4350. inargs.data_len = 0;
  4351. ra[0].buf.pv = (void *)&inargs;
  4352. ra[0].buf.len = sizeof(inargs);
  4353. page.addr = phys;
  4354. page.size = size;
  4355. ra[1].buf.pv = (void *)&page;
  4356. ra[1].buf.len = sizeof(page);
  4357. ra[2].buf.pv = (void *)&page;
  4358. ra[2].buf.len = 0;
  4359. ra[3].buf.pv = (void *)&routargs;
  4360. ra[3].buf.len = sizeof(routargs);
  4361. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
  4362. ioctl.inv.sc = REMOTE_SCALARS_MAKE(10, 3, 1);
  4363. ioctl.inv.pra = ra;
  4364. ioctl.fds = NULL;
  4365. ioctl.attrs = NULL;
  4366. ioctl.crc = NULL;
  4367. ioctl.perf_kernel = NULL;
  4368. ioctl.perf_dsp = NULL;
  4369. ioctl.job = NULL;
  4370. VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
  4371. FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl)));
  4372. if (err)
  4373. goto bail;
  4374. if (raddr)
  4375. *raddr = (uintptr_t)routargs.vaddrout;
  4376. bail:
  4377. return err;
  4378. }
  4379. static int fastrpc_mem_unmap_to_dsp(struct fastrpc_file *fl, int fd,
  4380. uint32_t flags, uintptr_t va,
  4381. uint64_t phys, size_t size)
  4382. {
  4383. struct fastrpc_ioctl_invoke_async ioctl;
  4384. remote_arg_t ra[1];
  4385. int err = 0;
  4386. struct {
  4387. int pid;
  4388. int fd;
  4389. uint64_t vaddrin;
  4390. uint64_t len;
  4391. } inargs;
  4392. /* Send unique fastrpc process ID to dsp */
  4393. inargs.pid = fl->tgid_frpc;
  4394. inargs.fd = fd;
  4395. inargs.vaddrin = (uint64_t)va;
  4396. inargs.len = (uint64_t)size;
  4397. ra[0].buf.pv = (void *)&inargs;
  4398. ra[0].buf.len = sizeof(inargs);
  4399. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
  4400. ioctl.inv.sc = REMOTE_SCALARS_MAKE(11, 1, 0);
  4401. ioctl.inv.pra = ra;
  4402. ioctl.fds = NULL;
  4403. ioctl.attrs = NULL;
  4404. ioctl.crc = NULL;
  4405. ioctl.perf_kernel = NULL;
  4406. ioctl.perf_dsp = NULL;
  4407. ioctl.job = NULL;
  4408. VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
  4409. FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl)));
  4410. if (err)
  4411. goto bail;
  4412. bail:
  4413. return err;
  4414. }
  4415. static int fastrpc_unmap_on_dsp(struct fastrpc_file *fl,
  4416. uintptr_t raddr, uint64_t phys, size_t size, uint32_t flags)
  4417. {
  4418. struct fastrpc_ioctl_invoke_async ioctl;
  4419. remote_arg_t ra[1] = {};
  4420. int err = 0;
  4421. struct {
  4422. int pid;
  4423. uintptr_t vaddrout;
  4424. size_t size;
  4425. } inargs;
  4426. /* Send unique fastrpc process ID to dsp */
  4427. inargs.pid = fl->tgid_frpc;
  4428. inargs.size = size;
  4429. inargs.vaddrout = raddr;
  4430. ra[0].buf.pv = (void *)&inargs;
  4431. ra[0].buf.len = sizeof(inargs);
  4432. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
  4433. if (fl->apps->compat)
  4434. ioctl.inv.sc = REMOTE_SCALARS_MAKE(5, 1, 0);
  4435. else
  4436. ioctl.inv.sc = REMOTE_SCALARS_MAKE(3, 1, 0);
  4437. ioctl.inv.pra = ra;
  4438. ioctl.fds = NULL;
  4439. ioctl.attrs = NULL;
  4440. ioctl.crc = NULL;
  4441. ioctl.perf_kernel = NULL;
  4442. ioctl.perf_dsp = NULL;
  4443. ioctl.job = NULL;
  4444. VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
  4445. FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl)));
  4446. if (err)
  4447. goto bail;
  4448. bail:
  4449. return err;
  4450. }
  4451. static int fastrpc_mmap_on_dsp(struct fastrpc_file *fl, uint32_t flags,
  4452. uintptr_t va, uint64_t phys,
  4453. size_t size, int refs, uintptr_t *raddr)
  4454. {
  4455. struct fastrpc_ioctl_invoke_async ioctl;
  4456. struct fastrpc_apps *me = &gfa;
  4457. struct smq_phy_page page;
  4458. int num = 1;
  4459. remote_arg_t ra[3];
  4460. int err = 0;
  4461. struct {
  4462. int pid;
  4463. uint32_t flags;
  4464. uintptr_t vaddrin;
  4465. int num;
  4466. } inargs;
  4467. struct {
  4468. uintptr_t vaddrout;
  4469. } routargs;
  4470. int cid = -1;
  4471. if (!fl) {
  4472. err = -EBADF;
  4473. goto bail;
  4474. }
  4475. cid = fl->cid;
  4476. /* Send unique fastrpc process ID to dsp */
  4477. inargs.pid = fl->tgid_frpc;
  4478. inargs.vaddrin = (uintptr_t)va;
  4479. inargs.flags = flags;
  4480. inargs.num = fl->apps->compat ? num * sizeof(page) : num;
  4481. ra[0].buf.pv = (void *)&inargs;
  4482. ra[0].buf.len = sizeof(inargs);
  4483. page.addr = phys;
  4484. page.size = size;
  4485. ra[1].buf.pv = (void *)&page;
  4486. ra[1].buf.len = num * sizeof(page);
  4487. ra[2].buf.pv = (void *)&routargs;
  4488. ra[2].buf.len = sizeof(routargs);
  4489. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
  4490. if (fl->apps->compat)
  4491. ioctl.inv.sc = REMOTE_SCALARS_MAKE(4, 2, 1);
  4492. else
  4493. ioctl.inv.sc = REMOTE_SCALARS_MAKE(2, 2, 1);
  4494. ioctl.inv.pra = ra;
  4495. ioctl.fds = NULL;
  4496. ioctl.attrs = NULL;
  4497. ioctl.crc = NULL;
  4498. ioctl.perf_kernel = NULL;
  4499. ioctl.perf_dsp = NULL;
  4500. ioctl.job = NULL;
  4501. VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
  4502. FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl)));
  4503. *raddr = (uintptr_t)routargs.vaddrout;
  4504. if (err)
  4505. goto bail;
  4506. if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
  4507. VERIFY(err, VALID_FASTRPC_CID(cid));
  4508. if (err) {
  4509. err = -ECHRNG;
  4510. ADSPRPC_ERR(
  4511. "invalid channel 0x%zx set for session\n",
  4512. cid);
  4513. goto bail;
  4514. }
  4515. }
  4516. if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR
  4517. && me->channel[cid].rhvm.vmid && refs == 1) {
  4518. struct secure_vm *rhvm = &me->channel[cid].rhvm;
  4519. u64 src_perms = BIT(QCOM_SCM_VMID_HLOS);
  4520. struct qcom_scm_vmperm *dst_perms;
  4521. uint32_t i = 0;
  4522. VERIFY(err, NULL != (dst_perms = kcalloc(rhvm->vmcount,
  4523. sizeof(struct qcom_scm_vmperm), GFP_KERNEL)));
  4524. if (err)
  4525. goto bail;
  4526. for (i = 0; i < rhvm->vmcount; i++) {
  4527. dst_perms[i].vmid = rhvm->vmid[i];
  4528. dst_perms[i].perm = rhvm->vmperm[i];
  4529. }
  4530. err = qcom_scm_assign_mem(phys, (uint64_t)size,
  4531. &src_perms, dst_perms, rhvm->vmcount);
  4532. kfree(dst_perms);
  4533. if (err) {
  4534. int unmap_err = 0;
  4535. ADSPRPC_ERR(
  4536. "rh hyp assign failed with %d for phys 0x%llx, size %zu\n",
  4537. err, phys, size);
  4538. err = -EADDRNOTAVAIL;
  4539. unmap_err = fastrpc_unmap_on_dsp(fl,
  4540. *raddr, phys, size, flags);
  4541. if (unmap_err) {
  4542. ADSPRPC_ERR(
  4543. "failed to unmap %d for phys 0x%llx, size %zd\n",
  4544. unmap_err, phys, size);
  4545. }
  4546. goto bail;
  4547. }
  4548. }
  4549. bail:
  4550. return err;
  4551. }
  4552. static int fastrpc_munmap_on_dsp_rh(struct fastrpc_file *fl, uint64_t phys,
  4553. size_t size, uint32_t flags, int locked)
  4554. {
  4555. int err = 0;
  4556. int tgid = 0;
  4557. struct fastrpc_apps *me = &gfa;
  4558. int cid = -1;
  4559. struct fastrpc_ioctl_invoke_async ioctl;
  4560. remote_arg_t ra[2];
  4561. struct {
  4562. uint8_t skey;
  4563. } routargs;
  4564. if (!fl) {
  4565. err = -EBADF;
  4566. goto bail;
  4567. }
  4568. cid = fl->cid;
  4569. VERIFY(err, VALID_FASTRPC_CID(cid));
  4570. if (err) {
  4571. err = -ECHRNG;
  4572. ADSPRPC_ERR(
  4573. "invalid channel 0x%zx set for session\n",
  4574. cid);
  4575. goto bail;
  4576. }
  4577. /* Send unique fastrpc process ID to dsp */
  4578. tgid = fl->tgid_frpc;
  4579. ra[0].buf.pv = (void *)&tgid;
  4580. ra[0].buf.len = sizeof(tgid);
  4581. ra[1].buf.pv = (void *)&routargs;
  4582. ra[1].buf.len = sizeof(routargs);
  4583. ioctl.inv.handle = FASTRPC_STATIC_HANDLE_PROCESS_GROUP;
  4584. ioctl.inv.sc = REMOTE_SCALARS_MAKE(9, 1, 1);
  4585. ioctl.inv.pra = ra;
  4586. ioctl.fds = NULL;
  4587. ioctl.attrs = NULL;
  4588. ioctl.crc = NULL;
  4589. ioctl.perf_kernel = NULL;
  4590. ioctl.perf_dsp = NULL;
  4591. ioctl.job = NULL;
  4592. if (locked) {
  4593. mutex_unlock(&fl->map_mutex);
  4594. mutex_unlock(&me->channel[cid].smd_mutex);
  4595. }
  4596. VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl,
  4597. FASTRPC_MODE_PARALLEL, KERNEL_MSG_WITH_ZERO_PID, &ioctl)));
  4598. if (locked) {
  4599. mutex_lock(&me->channel[cid].smd_mutex);
  4600. mutex_lock(&fl->map_mutex);
  4601. }
  4602. if (err)
  4603. goto bail;
  4604. bail:
  4605. return err;
  4606. }
  4607. static int fastrpc_munmap_rh(uint64_t phys, size_t size,
  4608. uint32_t flags)
  4609. {
  4610. int err = 0;
  4611. struct fastrpc_apps *me = &gfa;
  4612. struct secure_vm *rhvm = &me->channel[RH_CID].rhvm;
  4613. if ((rhvm->vmid)
  4614. && (me->channel[RH_CID].in_hib == 0)) {
  4615. u64 src_perms = 0;
  4616. struct qcom_scm_vmperm dst_perms = {0};
  4617. uint32_t i = 0;
  4618. for (i = 0; i < rhvm->vmcount; i++) {
  4619. src_perms |= BIT(rhvm->vmid[i]);
  4620. }
  4621. dst_perms.vmid = QCOM_SCM_VMID_HLOS;
  4622. dst_perms.perm = QCOM_SCM_PERM_RWX;
  4623. err = qcom_scm_assign_mem(phys,
  4624. (uint64_t)size, &src_perms, &dst_perms, 1);
  4625. if (err) {
  4626. ADSPRPC_ERR(
  4627. "rh hyp unassign failed with %d for phys 0x%llx, size %zu\n",
  4628. err, phys, size);
  4629. err = -EADDRNOTAVAIL;
  4630. return err;
  4631. }
  4632. }
  4633. return err;
  4634. }
  4635. static int fastrpc_munmap_on_dsp(struct fastrpc_file *fl, uintptr_t raddr,
  4636. uint64_t phys, size_t size, uint32_t flags)
  4637. {
  4638. int err = 0;
  4639. VERIFY(err, 0 == (err = fastrpc_unmap_on_dsp(fl, raddr, phys,
  4640. size, flags)));
  4641. if (err)
  4642. goto bail;
  4643. if (flags == ADSP_MMAP_HEAP_ADDR) {
  4644. VERIFY(err, !(err = fastrpc_munmap_on_dsp_rh(fl, phys,
  4645. size, flags, 0)));
  4646. if (err)
  4647. goto bail;
  4648. } else if (flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
  4649. VERIFY(err, !(err = fastrpc_munmap_rh(phys,
  4650. size, flags)));
  4651. if (err)
  4652. goto bail;
  4653. }
  4654. bail:
  4655. return err;
  4656. }
  4657. static int fastrpc_mmap_remove_ssr(struct fastrpc_file *fl, int locked)
  4658. {
  4659. struct fastrpc_mmap *match = NULL, *map = NULL;
  4660. struct hlist_node *n = NULL;
  4661. int err = 0, ret = 0, lock = 0;
  4662. struct fastrpc_apps *me = &gfa;
  4663. struct qcom_dump_segment ramdump_segments_rh;
  4664. struct list_head head;
  4665. unsigned long irq_flags = 0;
  4666. INIT_LIST_HEAD(&head);
  4667. if (fl) {
  4668. VERIFY(err, fl->cid == RH_CID);
  4669. if (err) {
  4670. err = -EBADR;
  4671. goto bail;
  4672. }
  4673. }
  4674. spin_lock_irqsave(&me->hlock, irq_flags);
  4675. lock = 1;
  4676. hlist_for_each_entry_safe(map, n, &me->maps, hn) {
  4677. if (!lock) {
  4678. spin_lock_irqsave(&me->hlock, irq_flags);
  4679. lock = 1;
  4680. }
  4681. /* In hibernation suspend case fl is NULL, check !fl to cleanup */
  4682. if (!fl || (fl && map->servloc_name && fl->servloc_name
  4683. && !strcmp(map->servloc_name, fl->servloc_name))) {
  4684. match = map;
  4685. if (map->is_persistent && map->in_use) {
  4686. struct secure_vm *rhvm = &me->channel[RH_CID].rhvm;
  4687. uint64_t phys = map->phys;
  4688. size_t size = map->size;
  4689. if (lock) {
  4690. spin_unlock_irqrestore(&me->hlock, irq_flags);
  4691. lock = 0;
  4692. }
  4693. //scm assign it back to HLOS
  4694. if (rhvm->vmid) {
  4695. u64 src_perms = 0;
  4696. struct qcom_scm_vmperm dst_perms = {0};
  4697. uint32_t i = 0;
  4698. for (i = 0; i < rhvm->vmcount; i++) {
  4699. src_perms |= BIT(rhvm->vmid[i]);
  4700. }
  4701. dst_perms.vmid = QCOM_SCM_VMID_HLOS;
  4702. dst_perms.perm = QCOM_SCM_PERM_RWX;
  4703. err = qcom_scm_assign_mem(phys, (uint64_t)size,
  4704. &src_perms, &dst_perms, 1);
  4705. }
  4706. if (err) {
  4707. ADSPRPC_ERR(
  4708. "rh hyp unassign failed with %d for phys 0x%llx, size %zu\n",
  4709. err, phys, size);
  4710. err = -EADDRNOTAVAIL;
  4711. goto bail;
  4712. }
  4713. if (!lock) {
  4714. spin_lock_irqsave(&me->hlock, irq_flags);
  4715. lock = 1;
  4716. }
  4717. map->in_use = false;
  4718. /*
  4719. * decrementing refcount for persistent mappings
  4720. * as incrementing it in fastrpc_get_persistent_map
  4721. */
  4722. map->refs--;
  4723. }
  4724. if (!match->is_persistent)
  4725. hlist_del_init(&map->hn);
  4726. }
  4727. if (lock) {
  4728. spin_unlock_irqrestore(&me->hlock, irq_flags);
  4729. lock = 0;
  4730. }
  4731. if (match) {
  4732. if (!match->is_persistent) {
  4733. if (match->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
  4734. err = fastrpc_munmap_rh(match->phys,
  4735. match->size, match->flags);
  4736. } else if (match->flags == ADSP_MMAP_HEAP_ADDR) {
  4737. if (fl)
  4738. err = fastrpc_munmap_on_dsp_rh(fl, match->phys,
  4739. match->size, match->flags, 0);
  4740. else {
  4741. pr_err("Cannot communicate with DSP, ADSP is down\n");
  4742. fastrpc_mmap_add(match);
  4743. }
  4744. }
  4745. }
  4746. memset(&ramdump_segments_rh, 0, sizeof(ramdump_segments_rh));
  4747. ramdump_segments_rh.da = match->phys;
  4748. ramdump_segments_rh.va = (void *)page_address((struct page *)match->va);
  4749. ramdump_segments_rh.size = match->size;
  4750. INIT_LIST_HEAD(&head);
  4751. list_add(&ramdump_segments_rh.node, &head);
  4752. if (me->dev && dump_enabled()) {
  4753. ret = qcom_elf_dump(&head, me->dev, ELF_CLASS);
  4754. if (ret < 0)
  4755. pr_err("adsprpc: %s: unable to dump heap (err %d)\n",
  4756. __func__, ret);
  4757. }
  4758. if (!match->is_persistent) {
  4759. if (!locked)
  4760. mutex_lock(&fl->map_mutex);
  4761. fastrpc_mmap_free(match, 0);
  4762. if (!locked)
  4763. mutex_unlock(&fl->map_mutex);
  4764. }
  4765. }
  4766. }
  4767. bail:
  4768. if (lock) {
  4769. spin_unlock_irqrestore(&me->hlock, irq_flags);
  4770. lock = 0;
  4771. }
  4772. if (err && match) {
  4773. if (!locked)
  4774. mutex_lock(&fl->map_mutex);
  4775. fastrpc_mmap_add(match);
  4776. if (!locked)
  4777. mutex_unlock(&fl->map_mutex);
  4778. }
  4779. return err;
  4780. }
  4781. static int fastrpc_mmap_remove_pdr(struct fastrpc_file *fl)
  4782. {
  4783. struct fastrpc_apps *me = &gfa;
  4784. int session = 0, err = 0, cid = -1;
  4785. if (!fl) {
  4786. err = -EBADF;
  4787. goto bail;
  4788. }
  4789. err = fastrpc_get_spd_session(fl->servloc_name,
  4790. &session, &cid);
  4791. if (err)
  4792. goto bail;
  4793. VERIFY(err, cid == fl->cid);
  4794. if (err) {
  4795. err = -EBADR;
  4796. goto bail;
  4797. }
  4798. if (atomic_read(&me->channel[cid].spd[session].ispdup) == 0) {
  4799. err = -ENOTCONN;
  4800. goto bail;
  4801. }
  4802. if (me->channel[cid].spd[session].pdrcount !=
  4803. me->channel[cid].spd[session].prevpdrcount) {
  4804. err = fastrpc_mmap_remove_ssr(fl, 0);
  4805. if (err)
  4806. ADSPRPC_WARN("failed to unmap remote heap (err %d)\n",
  4807. err);
  4808. me->channel[cid].spd[session].prevpdrcount =
  4809. me->channel[cid].spd[session].pdrcount;
  4810. }
  4811. bail:
  4812. return err;
  4813. }
  4814. static inline void get_fastrpc_ioctl_mmap_64(
  4815. struct fastrpc_ioctl_mmap_64 *mmap64,
  4816. struct fastrpc_ioctl_mmap *immap)
  4817. {
  4818. immap->fd = mmap64->fd;
  4819. immap->flags = mmap64->flags;
  4820. immap->vaddrin = (uintptr_t)mmap64->vaddrin;
  4821. immap->size = mmap64->size;
  4822. }
  4823. static inline void put_fastrpc_ioctl_mmap_64(
  4824. struct fastrpc_ioctl_mmap_64 *mmap64,
  4825. struct fastrpc_ioctl_mmap *immap)
  4826. {
  4827. mmap64->vaddrout = (uint64_t)immap->vaddrout;
  4828. }
  4829. static inline void get_fastrpc_ioctl_munmap_64(
  4830. struct fastrpc_ioctl_munmap_64 *munmap64,
  4831. struct fastrpc_ioctl_munmap *imunmap)
  4832. {
  4833. imunmap->vaddrout = (uintptr_t)munmap64->vaddrout;
  4834. imunmap->size = munmap64->size;
  4835. }
  4836. int fastrpc_internal_munmap(struct fastrpc_file *fl,
  4837. struct fastrpc_ioctl_munmap *ud)
  4838. {
  4839. int err = 0;
  4840. struct fastrpc_mmap *map = NULL;
  4841. struct fastrpc_buf *rbuf = NULL, *free = NULL;
  4842. struct hlist_node *n;
  4843. VERIFY(err, fl->dsp_proc_init == 1);
  4844. if (err) {
  4845. ADSPRPC_ERR(
  4846. "user application %s trying to unmap without initialization\n",
  4847. current->comm);
  4848. err = -EHOSTDOWN;
  4849. return err;
  4850. }
  4851. mutex_lock(&fl->internal_map_mutex);
  4852. spin_lock(&fl->hlock);
  4853. hlist_for_each_entry_safe(rbuf, n, &fl->remote_bufs, hn_rem) {
  4854. if (rbuf->raddr && ((rbuf->flags == ADSP_MMAP_ADD_PAGES) ||
  4855. (rbuf->flags == ADSP_MMAP_ADD_PAGES_LLC))) {
  4856. if ((rbuf->raddr == ud->vaddrout) &&
  4857. (rbuf->size == ud->size)) {
  4858. free = rbuf;
  4859. break;
  4860. }
  4861. }
  4862. }
  4863. spin_unlock(&fl->hlock);
  4864. if (free) {
  4865. VERIFY(err, !(err = fastrpc_munmap_on_dsp(fl, free->raddr,
  4866. free->phys, free->size, free->flags)));
  4867. if (err)
  4868. goto bail;
  4869. fastrpc_buf_free(rbuf, 0);
  4870. mutex_unlock(&fl->internal_map_mutex);
  4871. return err;
  4872. }
  4873. mutex_lock(&fl->map_mutex);
  4874. VERIFY(err, !(err = fastrpc_mmap_remove(fl, -1, ud->vaddrout,
  4875. ud->size, &map)));
  4876. mutex_unlock(&fl->map_mutex);
  4877. if (err)
  4878. goto bail;
  4879. VERIFY(err, map != NULL);
  4880. if (err) {
  4881. err = -EINVAL;
  4882. goto bail;
  4883. }
  4884. if (!map->is_persistent) {
  4885. VERIFY(err, !(err = fastrpc_munmap_on_dsp(fl, map->raddr,
  4886. map->phys, map->size, map->flags)));
  4887. }
  4888. if (err)
  4889. goto bail;
  4890. mutex_lock(&fl->map_mutex);
  4891. fastrpc_mmap_free(map, 0);
  4892. mutex_unlock(&fl->map_mutex);
  4893. bail:
  4894. if (err && map) {
  4895. mutex_lock(&fl->map_mutex);
  4896. fastrpc_mmap_add(map);
  4897. mutex_unlock(&fl->map_mutex);
  4898. }
  4899. mutex_unlock(&fl->internal_map_mutex);
  4900. return err;
  4901. }
  4902. /*
  4903. * fastrpc_internal_munmap_fd can only be used for buffers
  4904. * mapped with persist attributes. This can only be called
  4905. * once for any persist buffer
  4906. */
  4907. static int fastrpc_internal_munmap_fd(struct fastrpc_file *fl,
  4908. struct fastrpc_ioctl_munmap_fd *ud)
  4909. {
  4910. int err = 0;
  4911. struct fastrpc_mmap *map = NULL;
  4912. VERIFY(err, (fl && ud));
  4913. if (err) {
  4914. err = -EINVAL;
  4915. return err;
  4916. }
  4917. VERIFY(err, fl->dsp_proc_init == 1);
  4918. if (err) {
  4919. ADSPRPC_ERR(
  4920. "user application %s trying to unmap without initialization\n",
  4921. current->comm);
  4922. err = -EHOSTDOWN;
  4923. return err;
  4924. }
  4925. mutex_lock(&fl->internal_map_mutex);
  4926. mutex_lock(&fl->map_mutex);
  4927. err = fastrpc_mmap_find(fl, ud->fd, NULL, ud->va, ud->len, 0, 0, &map);
  4928. if (err) {
  4929. ADSPRPC_ERR(
  4930. "mapping not found to unmap fd 0x%x, va 0x%llx, len 0x%x, err %d\n",
  4931. ud->fd, (unsigned long long)ud->va,
  4932. (unsigned int)ud->len, err);
  4933. mutex_unlock(&fl->map_mutex);
  4934. goto bail;
  4935. }
  4936. if (map && (map->attr & FASTRPC_ATTR_KEEP_MAP)) {
  4937. map->attr = map->attr & (~FASTRPC_ATTR_KEEP_MAP);
  4938. fastrpc_mmap_free(map, 0);
  4939. }
  4940. mutex_unlock(&fl->map_mutex);
  4941. bail:
  4942. mutex_unlock(&fl->internal_map_mutex);
  4943. return err;
  4944. }
  4945. int fastrpc_internal_mem_map(struct fastrpc_file *fl,
  4946. struct fastrpc_ioctl_mem_map *ud)
  4947. {
  4948. int err = 0;
  4949. struct fastrpc_mmap *map = NULL;
  4950. VERIFY(err, fl->dsp_proc_init == 1);
  4951. if (err) {
  4952. pr_err("adsprpc: ERROR: %s: user application %s trying to map without initialization\n",
  4953. __func__, current->comm);
  4954. err = EBADR;
  4955. goto bail;
  4956. }
  4957. /* create SMMU mapping */
  4958. mutex_lock(&fl->map_mutex);
  4959. VERIFY(err, !(err = fastrpc_mmap_create(fl, ud->m.fd, NULL, ud->m.attrs,
  4960. ud->m.vaddrin, ud->m.length,
  4961. ud->m.flags, &map)));
  4962. mutex_unlock(&fl->map_mutex);
  4963. if (err)
  4964. goto bail;
  4965. if (map->raddr) {
  4966. err = -EEXIST;
  4967. goto bail;
  4968. }
  4969. /* create DSP mapping */
  4970. VERIFY(err, !(err = fastrpc_mem_map_to_dsp(fl, ud->m.fd, ud->m.offset,
  4971. ud->m.flags, map->va, map->phys, map->size, &map->raddr)));
  4972. if (err)
  4973. goto bail;
  4974. ud->m.vaddrout = map->raddr;
  4975. bail:
  4976. if (err) {
  4977. ADSPRPC_ERR("failed to map fd %d, len 0x%x, flags %d, map %pK, err %d\n",
  4978. ud->m.fd, ud->m.length, ud->m.flags, map, err);
  4979. if (map) {
  4980. mutex_lock(&fl->map_mutex);
  4981. fastrpc_mmap_free(map, 0);
  4982. mutex_unlock(&fl->map_mutex);
  4983. }
  4984. }
  4985. return err;
  4986. }
  4987. int fastrpc_internal_mem_unmap(struct fastrpc_file *fl,
  4988. struct fastrpc_ioctl_mem_unmap *ud)
  4989. {
  4990. int err = 0;
  4991. struct fastrpc_mmap *map = NULL;
  4992. size_t map_size = 0;
  4993. VERIFY(err, fl->dsp_proc_init == 1);
  4994. if (err) {
  4995. pr_err("adsprpc: ERROR: %s: user application %s trying to map without initialization\n",
  4996. __func__, current->comm);
  4997. err = EBADR;
  4998. goto bail;
  4999. }
  5000. mutex_lock(&fl->map_mutex);
  5001. VERIFY(err, !(err = fastrpc_mmap_remove(fl, ud->um.fd,
  5002. (uintptr_t)ud->um.vaddr, ud->um.length, &map)));
  5003. mutex_unlock(&fl->map_mutex);
  5004. if (err)
  5005. goto bail;
  5006. VERIFY(err, map->flags == FASTRPC_MAP_FD ||
  5007. map->flags == FASTRPC_MAP_FD_DELAYED ||
  5008. map->flags == FASTRPC_MAP_STATIC);
  5009. if (err) {
  5010. err = -EBADMSG;
  5011. goto bail;
  5012. }
  5013. map_size = map->size;
  5014. /* remove mapping on DSP */
  5015. VERIFY(err, !(err = fastrpc_mem_unmap_to_dsp(fl, map->fd, map->flags,
  5016. map->raddr, map->phys, map->size)));
  5017. if (err)
  5018. goto bail;
  5019. /* remove SMMU mapping */
  5020. mutex_lock(&fl->map_mutex);
  5021. fastrpc_mmap_free(map, 0);
  5022. mutex_unlock(&fl->map_mutex);
  5023. map = NULL;
  5024. bail:
  5025. if (err) {
  5026. ADSPRPC_ERR(
  5027. "failed to unmap fd %d addr 0x%llx length %zu map size %zu err 0x%x\n",
  5028. ud->um.fd, ud->um.vaddr, ud->um.length, map_size, err);
  5029. /* Add back to map list in case of error to unmap on DSP */
  5030. if (map) {
  5031. mutex_lock(&fl->map_mutex);
  5032. fastrpc_mmap_add(map);
  5033. mutex_unlock(&fl->map_mutex);
  5034. }
  5035. }
  5036. return err;
  5037. }
  5038. int fastrpc_internal_mmap(struct fastrpc_file *fl,
  5039. struct fastrpc_ioctl_mmap *ud)
  5040. {
  5041. struct fastrpc_mmap *map = NULL;
  5042. struct fastrpc_buf *rbuf = NULL;
  5043. unsigned long dma_attr = 0;
  5044. uintptr_t raddr = 0;
  5045. int err = 0;
  5046. VERIFY(err, fl->dsp_proc_init == 1);
  5047. if (err) {
  5048. ADSPRPC_ERR(
  5049. "user application %s trying to map without initialization\n",
  5050. current->comm);
  5051. err = -EHOSTDOWN;
  5052. return err;
  5053. }
  5054. mutex_lock(&fl->internal_map_mutex);
  5055. /* Pages for unsigned PD's user-heap should be allocated in userspace */
  5056. if (((ud->flags == ADSP_MMAP_ADD_PAGES) ||
  5057. (ud->flags == ADSP_MMAP_ADD_PAGES_LLC)) && !fl->is_unsigned_pd) {
  5058. if (ud->vaddrin) {
  5059. err = -EINVAL;
  5060. ADSPRPC_ERR(
  5061. "adding user allocated pages is not supported\n");
  5062. goto bail;
  5063. }
  5064. dma_attr = DMA_ATTR_DELAYED_UNMAP | DMA_ATTR_NO_KERNEL_MAPPING;
  5065. if (ud->flags == ADSP_MMAP_ADD_PAGES_LLC)
  5066. dma_attr |= DMA_ATTR_SYS_CACHE_ONLY;
  5067. err = fastrpc_buf_alloc(fl, ud->size, dma_attr, ud->flags,
  5068. USERHEAP_BUF, &rbuf);
  5069. if (err)
  5070. goto bail;
  5071. err = fastrpc_mmap_on_dsp(fl, ud->flags, 0,
  5072. rbuf->phys, rbuf->size, 0, &raddr);
  5073. if (err)
  5074. goto bail;
  5075. rbuf->raddr = raddr;
  5076. } else {
  5077. uintptr_t va_to_dsp;
  5078. if (fl->is_unsigned_pd && ud->flags == ADSP_MMAP_REMOTE_HEAP_ADDR) {
  5079. err = -EINVAL;
  5080. ADSPRPC_ERR(
  5081. "Secure memory allocation is not supported in unsigned PD");
  5082. goto bail;
  5083. }
  5084. mutex_lock(&fl->map_mutex);
  5085. VERIFY(err, !(err = fastrpc_mmap_create(fl, ud->fd, NULL, 0,
  5086. (uintptr_t)ud->vaddrin, ud->size,
  5087. ud->flags, &map)));
  5088. mutex_unlock(&fl->map_mutex);
  5089. if (err)
  5090. goto bail;
  5091. if (ud->flags == ADSP_MMAP_HEAP_ADDR ||
  5092. ud->flags == ADSP_MMAP_REMOTE_HEAP_ADDR)
  5093. va_to_dsp = 0;
  5094. else
  5095. va_to_dsp = (uintptr_t)map->va;
  5096. VERIFY(err, 0 == (err = fastrpc_mmap_on_dsp(fl, ud->flags,
  5097. va_to_dsp, map->phys, map->size, map->refs, &raddr)));
  5098. if (err)
  5099. goto bail;
  5100. map->raddr = raddr;
  5101. }
  5102. ud->vaddrout = raddr;
  5103. bail:
  5104. if (err) {
  5105. if (map) {
  5106. mutex_lock(&fl->map_mutex);
  5107. fastrpc_mmap_free(map, 0);
  5108. mutex_unlock(&fl->map_mutex);
  5109. }
  5110. if (!IS_ERR_OR_NULL(rbuf))
  5111. fastrpc_buf_free(rbuf, 0);
  5112. }
  5113. mutex_unlock(&fl->internal_map_mutex);
  5114. return err;
  5115. }
  5116. static void fastrpc_context_list_dtor(struct fastrpc_file *fl);
  5117. static int fastrpc_session_alloc_locked(struct fastrpc_channel_ctx *chan,
  5118. int secure, int sharedcb, int pd_type, struct fastrpc_session_ctx **session)
  5119. {
  5120. struct fastrpc_apps *me = &gfa;
  5121. uint64_t idx = 0;
  5122. int err = 0;
  5123. /*
  5124. * PD type can be either unused(DEFAULT_UNUSED) (or) if PD type
  5125. * is used, choose the context bank with matching PD type.
  5126. */
  5127. if (chan->sesscount) {
  5128. for (idx = 0; idx < chan->sesscount; ++idx) {
  5129. if (!chan->session[idx].used &&
  5130. chan->session[idx].smmu.secure == secure &&
  5131. chan->session[idx].smmu.sharedcb == sharedcb &&
  5132. (pd_type == DEFAULT_UNUSED ||
  5133. chan->session[idx].smmu.pd_type == pd_type)) {
  5134. chan->session[idx].used = 1;
  5135. break;
  5136. }
  5137. }
  5138. if (idx >= chan->sesscount) {
  5139. err = -EUSERS;
  5140. goto bail;
  5141. }
  5142. chan->session[idx].smmu.faults = 0;
  5143. } else {
  5144. VERIFY(err, me->dev != NULL);
  5145. if (err) {
  5146. err = -ENODEV;
  5147. goto bail;
  5148. }
  5149. chan->session[0].dev = me->dev;
  5150. chan->session[0].smmu.dev = me->dev;
  5151. }
  5152. *session = &chan->session[idx];
  5153. bail:
  5154. return err;
  5155. }
  5156. static void handle_remote_signal(uint64_t msg, int cid)
  5157. {
  5158. struct fastrpc_apps *me = &gfa;
  5159. uint32_t pid = msg >> 32;
  5160. uint32_t signal_id = msg & 0xffffffff;
  5161. struct fastrpc_file *fl = NULL;
  5162. struct hlist_node *n = NULL;
  5163. unsigned long irq_flags = 0;
  5164. DSPSIGNAL_VERBOSE("Received queue signal %llx: PID %u, signal %u\n", msg, pid, signal_id);
  5165. if (signal_id >= DSPSIGNAL_NUM_SIGNALS) {
  5166. ADSPRPC_ERR("Received bad signal %u for PID %u\n", signal_id, pid);
  5167. return;
  5168. }
  5169. spin_lock_irqsave(&me->hlock, irq_flags);
  5170. hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
  5171. /* Response from DSP contains unique fastrpc process id, use unique fastrpc process ID to compare */
  5172. if ((fl->tgid_frpc == pid) && (fl->cid == cid)) {
  5173. unsigned long fflags = 0;
  5174. spin_lock_irqsave(&fl->dspsignals_lock, fflags);
  5175. if (fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE]) {
  5176. struct fastrpc_dspsignal *group =
  5177. fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE];
  5178. struct fastrpc_dspsignal *sig =
  5179. &group[signal_id % DSPSIGNAL_GROUP_SIZE];
  5180. if ((sig->state == DSPSIGNAL_STATE_PENDING) ||
  5181. (sig->state == DSPSIGNAL_STATE_SIGNALED)) {
  5182. DSPSIGNAL_VERBOSE("Signaling signal %u for PID %u\n",
  5183. signal_id, pid);
  5184. trace_fastrpc_dspsignal("complete", signal_id, sig->state, 0);
  5185. complete(&sig->comp);
  5186. sig->state = DSPSIGNAL_STATE_SIGNALED;
  5187. } else if (sig->state == DSPSIGNAL_STATE_UNUSED) {
  5188. ADSPRPC_ERR("Received unknown signal %u for PID %u\n",
  5189. signal_id, pid);
  5190. }
  5191. } else {
  5192. ADSPRPC_ERR("Received unknown signal %u for PID %u\n",
  5193. signal_id, pid);
  5194. }
  5195. spin_unlock_irqrestore(&fl->dspsignals_lock, fflags);
  5196. break;
  5197. }
  5198. }
  5199. spin_unlock_irqrestore(&me->hlock, irq_flags);
  5200. }
  5201. int fastrpc_handle_rpc_response(void *data, int len, int cid)
  5202. {
  5203. struct smq_invoke_rsp *rsp = (struct smq_invoke_rsp *)data;
  5204. struct smq_notif_rspv3 *notif = (struct smq_notif_rspv3 *)data;
  5205. struct smq_invoke_rspv2 *rspv2 = NULL;
  5206. struct smq_invoke_ctx *ctx = NULL;
  5207. struct fastrpc_apps *me = &gfa;
  5208. uint32_t index, rsp_flags = 0, early_wake_time = 0, ver = 0;
  5209. int err = 0, ignore_rsp_err = 0;
  5210. struct fastrpc_channel_ctx *chan = NULL;
  5211. unsigned long irq_flags = 0;
  5212. int64_t ns = 0;
  5213. uint64_t xo_time_in_us = 0, dspsig_msg = 0;
  5214. xo_time_in_us = CONVERT_CNT_TO_US(__arch_counter_get_cntvct());
  5215. if (len == sizeof(uint64_t)) {
  5216. /*
  5217. * dspsignal message from the DSP
  5218. */
  5219. dspsig_msg = *((uint64_t *)data);
  5220. trace_fastrpc_transport_response(cid, dspsig_msg, 0, 0, 0);
  5221. handle_remote_signal(dspsig_msg, cid);
  5222. goto bail;
  5223. }
  5224. chan = &me->channel[cid];
  5225. VERIFY(err, (rsp && len >= sizeof(*rsp)));
  5226. if (err) {
  5227. err = -EINVAL;
  5228. goto bail;
  5229. }
  5230. if (notif->ctx == FASTRPC_NOTIF_CTX_RESERVED) {
  5231. VERIFY(err, (notif->type == STATUS_RESPONSE &&
  5232. len >= sizeof(*notif)));
  5233. if (err)
  5234. goto bail;
  5235. fastrpc_notif_find_process(cid, notif);
  5236. goto bail;
  5237. }
  5238. if (len >= sizeof(struct smq_invoke_rspv2))
  5239. rspv2 = (struct smq_invoke_rspv2 *)data;
  5240. if (rspv2) {
  5241. early_wake_time = rspv2->early_wake_time;
  5242. rsp_flags = rspv2->flags;
  5243. ver = rspv2->version;
  5244. }
  5245. trace_fastrpc_transport_response(cid, rsp->ctx,
  5246. rsp->retval, rsp_flags, early_wake_time);
  5247. ns = get_timestamp_in_ns();
  5248. fastrpc_update_rxmsg_buf(chan, rsp->ctx, rsp->retval,
  5249. rsp_flags, early_wake_time, ver, ns, xo_time_in_us);
  5250. index = (uint32_t)GET_TABLE_IDX_FROM_CTXID(rsp->ctx);
  5251. VERIFY(err, index < FASTRPC_CTX_MAX);
  5252. if (err)
  5253. goto bail;
  5254. spin_lock_irqsave(&chan->ctxlock, irq_flags);
  5255. ctx = chan->ctxtable[index];
  5256. VERIFY(err, !IS_ERR_OR_NULL(ctx) &&
  5257. (ctx->ctxid == GET_CTXID_FROM_RSP_CTX(rsp->ctx)) &&
  5258. ctx->magic == FASTRPC_CTX_MAGIC);
  5259. if (err) {
  5260. /*
  5261. * Received an anticipatory COMPLETE_SIGNAL from DSP for a
  5262. * context after CPU successfully polling on memory and
  5263. * completed processing of context. Ignore the message.
  5264. * Also ignore response for a call which was already
  5265. * completed by update of poll memory and the context was
  5266. * removed from the table and possibly reused for another call.
  5267. */
  5268. ignore_rsp_err = ((rsp_flags == COMPLETE_SIGNAL) || !ctx ||
  5269. (ctx && (ctx->ctxid != GET_CTXID_FROM_RSP_CTX(rsp->ctx)))) ? 1 : 0;
  5270. goto bail_unlock;
  5271. }
  5272. if (rspv2) {
  5273. VERIFY(err, rspv2->version == FASTRPC_RSP_VERSION2);
  5274. if (err)
  5275. goto bail_unlock;
  5276. }
  5277. VERIFY(err, VALID_FASTRPC_CID(ctx->fl->cid));
  5278. if (err) {
  5279. err = -ECHRNG;
  5280. goto bail_unlock;
  5281. }
  5282. context_notify_user(ctx, rsp->retval, rsp_flags, early_wake_time);
  5283. bail_unlock:
  5284. spin_unlock_irqrestore(&chan->ctxlock, irq_flags);
  5285. bail:
  5286. if (err) {
  5287. err = -ENOKEY;
  5288. if (!ignore_rsp_err)
  5289. ADSPRPC_ERR(
  5290. "invalid response data %pK, len %d from remote subsystem err %d\n",
  5291. data, len, err);
  5292. else {
  5293. err = 0;
  5294. me->duplicate_rsp_err_cnt++;
  5295. }
  5296. }
  5297. return err;
  5298. }
  5299. static int fastrpc_session_alloc_secure_memory(
  5300. struct fastrpc_channel_ctx *chan, int secure,
  5301. int sharedcb, int pd_type, struct fastrpc_session_ctx **session)
  5302. {
  5303. int err = 0;
  5304. struct fastrpc_apps *me = &gfa;
  5305. /*
  5306. * If PD type is configured for context banks,
  5307. * Use CPZ_USERPD, to allocate secure context bank type.
  5308. */
  5309. if (pd_type != DEFAULT_UNUSED && me->cb_pd_type)
  5310. pd_type = CPZ_USERPD;
  5311. mutex_lock(&chan->smd_mutex);
  5312. if (!*session)
  5313. err = fastrpc_session_alloc_locked(chan, secure, sharedcb, pd_type, session);
  5314. mutex_unlock(&chan->smd_mutex);
  5315. if (err == -EUSERS) {
  5316. ADSPRPC_WARN(
  5317. "max concurrent sessions limit (%d) already reached on %s err %d\n",
  5318. chan->sesscount, chan->subsys, err);
  5319. }
  5320. return err;
  5321. }
  5322. static void fastrpc_session_free(struct fastrpc_channel_ctx *chan,
  5323. struct fastrpc_session_ctx *session)
  5324. {
  5325. mutex_lock(&chan->smd_mutex);
  5326. session->used = 0;
  5327. mutex_unlock(&chan->smd_mutex);
  5328. }
  5329. static int fastrpc_file_free(struct fastrpc_file *fl)
  5330. {
  5331. struct hlist_node *n = NULL;
  5332. struct fastrpc_mmap *map = NULL, *lmap = NULL;
  5333. unsigned long flags;
  5334. int cid;
  5335. struct fastrpc_apps *me = &gfa;
  5336. bool is_driver_closed = false;
  5337. int err = 0;
  5338. unsigned long irq_flags = 0;
  5339. bool is_locked = false;
  5340. int i;
  5341. if (!fl)
  5342. return 0;
  5343. cid = fl->cid;
  5344. spin_lock_irqsave(&me->hlock, irq_flags);
  5345. if (fl->device) {
  5346. fl->device->dev_close = true;
  5347. if (fl->device->refs == 0) {
  5348. is_driver_closed = true;
  5349. hlist_del_init(&fl->device->hn);
  5350. }
  5351. }
  5352. fl->file_close = FASTRPC_PROCESS_EXIT_START;
  5353. spin_unlock_irqrestore(&me->hlock, irq_flags);
  5354. (void)fastrpc_release_current_dsp_process(fl);
  5355. spin_lock_irqsave(&fl->apps->hlock, irq_flags);
  5356. is_locked = true;
  5357. if (!fl->is_dma_invoke_pend)
  5358. goto skip_dmainvoke_wait;
  5359. is_locked = false;
  5360. spin_unlock_irqrestore(&fl->apps->hlock, irq_flags);
  5361. wait_for_completion(&fl->dma_invoke);
  5362. skip_dmainvoke_wait:
  5363. if (!is_locked) {
  5364. spin_lock_irqsave(&fl->apps->hlock, irq_flags);
  5365. is_locked = true;
  5366. }
  5367. if (!fl->is_ramdump_pend)
  5368. goto skip_dump_wait;
  5369. is_locked = false;
  5370. spin_unlock_irqrestore(&fl->apps->hlock, irq_flags);
  5371. wait_for_completion(&fl->work);
  5372. skip_dump_wait:
  5373. if (!is_locked) {
  5374. spin_lock_irqsave(&fl->apps->hlock, irq_flags);
  5375. is_locked = true;
  5376. }
  5377. hlist_del_init(&fl->hn);
  5378. fl->is_ramdump_pend = false;
  5379. fl->is_dma_invoke_pend = false;
  5380. fl->dsp_process_state = PROCESS_CREATE_DEFAULT;
  5381. /* Reset the tgid usage to false */
  5382. if (fl->tgid_frpc != -1)
  5383. frpc_tgid_usage_array[fl->tgid_frpc] = false;
  5384. is_locked = false;
  5385. spin_unlock_irqrestore(&fl->apps->hlock, irq_flags);
  5386. if (!fl->sctx) {
  5387. kfree(fl);
  5388. return 0;
  5389. }
  5390. //Dummy wake up to exit Async worker thread
  5391. spin_lock_irqsave(&fl->aqlock, flags);
  5392. atomic_add(1, &fl->async_queue_job_count);
  5393. wake_up_interruptible(&fl->async_wait_queue);
  5394. spin_unlock_irqrestore(&fl->aqlock, flags);
  5395. // Dummy wake up to exit notification worker thread
  5396. spin_lock_irqsave(&fl->proc_state_notif.nqlock, flags);
  5397. atomic_add(1, &fl->proc_state_notif.notif_queue_count);
  5398. wake_up_interruptible(&fl->proc_state_notif.notif_wait_queue);
  5399. spin_unlock_irqrestore(&fl->proc_state_notif.nqlock, flags);
  5400. if (!IS_ERR_OR_NULL(fl->init_mem))
  5401. fastrpc_buf_free(fl->init_mem, 0);
  5402. fastrpc_context_list_dtor(fl);
  5403. fastrpc_cached_buf_list_free(fl);
  5404. if (!IS_ERR_OR_NULL(fl->hdr_bufs))
  5405. kfree(fl->hdr_bufs);
  5406. if (!IS_ERR_OR_NULL(fl->pers_hdr_buf))
  5407. fastrpc_buf_free(fl->pers_hdr_buf, 0);
  5408. mutex_lock(&fl->internal_map_mutex);
  5409. mutex_lock(&fl->map_mutex);
  5410. do {
  5411. lmap = NULL;
  5412. hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
  5413. hlist_del_init(&map->hn);
  5414. lmap = map;
  5415. break;
  5416. }
  5417. fastrpc_mmap_free(lmap, 1);
  5418. } while (lmap);
  5419. mutex_unlock(&fl->map_mutex);
  5420. mutex_unlock(&fl->internal_map_mutex);
  5421. fastrpc_pm_relax(fl, gcinfo[fl->cid].secure);
  5422. if (fl->device && is_driver_closed)
  5423. device_unregister(&fl->device->dev);
  5424. VERIFY(err, VALID_FASTRPC_CID(cid));
  5425. if (!err && fl->sctx)
  5426. fastrpc_session_free(&fl->apps->channel[cid], fl->sctx);
  5427. if (!err && fl->secsctx)
  5428. fastrpc_session_free(&fl->apps->channel[cid], fl->secsctx);
  5429. for (i = 0; i < (DSPSIGNAL_NUM_SIGNALS / DSPSIGNAL_GROUP_SIZE); i++)
  5430. kfree(fl->signal_groups[i]);
  5431. mutex_destroy(&fl->signal_create_mutex);
  5432. fastrpc_remote_buf_list_free(fl);
  5433. mutex_destroy(&fl->map_mutex);
  5434. mutex_destroy(&fl->internal_map_mutex);
  5435. kfree(fl->dev_pm_qos_req);
  5436. kfree(fl->gidlist.gids);
  5437. kfree(fl);
  5438. return 0;
  5439. }
  5440. static int fastrpc_device_release(struct inode *inode, struct file *file)
  5441. {
  5442. struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data;
  5443. struct fastrpc_apps *me = &gfa;
  5444. unsigned int ii;
  5445. if (!fl)
  5446. return 0;
  5447. if (fl->qos_request && fl->dev_pm_qos_req) {
  5448. for (ii = 0; ii < me->lowest_capacity_core_count; ii++) {
  5449. if (!dev_pm_qos_request_active(&fl->dev_pm_qos_req[ii]))
  5450. continue;
  5451. dev_pm_qos_remove_request(&fl->dev_pm_qos_req[ii]);
  5452. }
  5453. }
  5454. debugfs_remove(fl->debugfs_file);
  5455. fastrpc_file_free(fl);
  5456. file->private_data = NULL;
  5457. return 0;
  5458. }
  5459. static ssize_t fastrpc_debugfs_read(struct file *filp, char __user *buffer,
  5460. size_t count, loff_t *position)
  5461. {
  5462. struct fastrpc_apps *me = &gfa;
  5463. struct fastrpc_file *fl = filp->private_data;
  5464. struct hlist_node *n;
  5465. struct fastrpc_buf *buf = NULL;
  5466. struct fastrpc_mmap *map = NULL;
  5467. struct fastrpc_mmap *gmaps = NULL;
  5468. struct smq_invoke_ctx *ictx = NULL;
  5469. struct fastrpc_channel_ctx *chan = NULL;
  5470. unsigned int len = 0;
  5471. int i, j, sess_used = 0, ret = 0;
  5472. char *fileinfo = NULL;
  5473. char single_line[] = "----------------";
  5474. char title[] = "=========================";
  5475. unsigned long irq_flags = 0;
  5476. fileinfo = kzalloc(DEBUGFS_SIZE, GFP_KERNEL);
  5477. if (!fileinfo) {
  5478. ret = -ENOMEM;
  5479. goto bail;
  5480. }
  5481. if (fl == NULL) {
  5482. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5483. "\n%s %s %s\n", title, " CHANNEL INFO ", title);
  5484. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5485. "%-7s|%-10s|%-15s|%-9s|%-13s\n",
  5486. "subsys", "sesscount", "subsystemstate",
  5487. "ssrcount", "session_used");
  5488. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5489. "-%s%s%s%s-\n", single_line, single_line,
  5490. single_line, single_line);
  5491. for (i = 0; i < NUM_CHANNELS; i++) {
  5492. sess_used = 0;
  5493. chan = &gcinfo[i];
  5494. len += scnprintf(fileinfo + len,
  5495. DEBUGFS_SIZE - len, "%-7s", chan->subsys);
  5496. len += scnprintf(fileinfo + len,
  5497. DEBUGFS_SIZE - len, "|%-10u",
  5498. chan->sesscount);
  5499. len += scnprintf(fileinfo + len,
  5500. DEBUGFS_SIZE - len, "|%-15d",
  5501. chan->subsystemstate);
  5502. len += scnprintf(fileinfo + len,
  5503. DEBUGFS_SIZE - len, "|%-9u",
  5504. chan->ssrcount);
  5505. for (j = 0; j < chan->sesscount; j++)
  5506. sess_used += chan->session[j].used;
  5507. len += scnprintf(fileinfo + len,
  5508. DEBUGFS_SIZE - len, "|%-13d\n", sess_used);
  5509. }
  5510. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5511. "\n%s%s%s\n", "=============",
  5512. " CMA HEAP ", "==============");
  5513. len += scnprintf(fileinfo + len,
  5514. DEBUGFS_SIZE - len, "%-20s|%-20s\n", "addr", "size");
  5515. len += scnprintf(fileinfo + len,
  5516. DEBUGFS_SIZE - len, "--%s%s---\n",
  5517. single_line, single_line);
  5518. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5519. "\n==========%s %s %s===========\n",
  5520. title, " GMAPS ", title);
  5521. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5522. "%-20s|%-20s|%-20s|%-20s\n",
  5523. "fd", "phys", "size", "va");
  5524. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5525. "%s%s%s%s%s\n", single_line, single_line,
  5526. single_line, single_line, single_line);
  5527. spin_lock_irqsave(&me->hlock, irq_flags);
  5528. hlist_for_each_entry_safe(gmaps, n, &me->maps, hn) {
  5529. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5530. "%-20d|0x%-18llX|0x%-18X|0x%-20lX\n\n",
  5531. gmaps->fd, gmaps->phys,
  5532. (uint32_t)gmaps->size,
  5533. gmaps->va);
  5534. }
  5535. spin_unlock_irqrestore(&me->hlock, irq_flags);
  5536. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5537. "%-20s|%-20s|%-20s|%-20s\n",
  5538. "len", "refs", "raddr", "flags");
  5539. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5540. "%s%s%s%s%s\n", single_line, single_line,
  5541. single_line, single_line, single_line);
  5542. spin_lock_irqsave(&me->hlock, irq_flags);
  5543. hlist_for_each_entry_safe(gmaps, n, &me->maps, hn) {
  5544. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5545. "0x%-18X|%-20d|%-20lu|%-20u\n",
  5546. (uint32_t)gmaps->len, gmaps->refs,
  5547. gmaps->raddr, gmaps->flags);
  5548. }
  5549. spin_unlock_irqrestore(&me->hlock, irq_flags);
  5550. } else {
  5551. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5552. "\n%s %13s %d\n", "cid", ":", fl->cid);
  5553. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5554. "%s %12s %d\n", "tgid", ":", fl->tgid);
  5555. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5556. "%s %14s %d\n", "tgid_frpc", ":", fl->tgid_frpc);
  5557. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5558. "%s %7s %d\n", "sessionid", ":", fl->sessionid);
  5559. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5560. "%s %8s %u\n", "ssrcount", ":", fl->ssrcount);
  5561. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5562. "%s %14s %d\n", "pd", ":", fl->pd);
  5563. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5564. "%s %9s %s\n", "servloc_name", ":", fl->servloc_name);
  5565. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5566. "%s %6s %d\n", "file_close", ":", fl->file_close);
  5567. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5568. "%s %9s %d\n", "profile", ":", fl->profile);
  5569. if (fl->sctx) {
  5570. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5571. "%s %3s %d\n", "smmu.coherent", ":",
  5572. fl->sctx->smmu.coherent);
  5573. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5574. "%s %4s %d\n", "smmu.enabled", ":",
  5575. fl->sctx->smmu.enabled);
  5576. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5577. "%s %9s %d\n", "smmu.cb", ":", fl->sctx->smmu.cb);
  5578. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5579. "%s %5s %d\n", "smmu.secure", ":",
  5580. fl->sctx->smmu.secure);
  5581. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5582. "%s %5s %d\n", "smmu.faults", ":",
  5583. fl->sctx->smmu.faults);
  5584. }
  5585. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5586. "\n=======%s %s %s======\n", title,
  5587. " LIST OF MAPS ", title);
  5588. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5589. "%-20s|%-20s|%-20s|%-20s\n", "va", "phys", "size", "flags");
  5590. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5591. "%s%s%s%s%s\n",
  5592. single_line, single_line, single_line,
  5593. single_line, single_line);
  5594. mutex_lock(&fl->map_mutex);
  5595. hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
  5596. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5597. "0x%-20lX|0x%-20llX|0x%-20zu|0x%-17llX\n\n",
  5598. map->va, map->phys,
  5599. map->size, map->flags);
  5600. }
  5601. mutex_unlock(&fl->map_mutex);
  5602. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5603. "%-20s|%-20s|%-20s\n",
  5604. "len", "refs",
  5605. "raddr");
  5606. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5607. "%s%s%s%s%s\n",
  5608. single_line, single_line, single_line,
  5609. single_line, single_line);
  5610. mutex_lock(&fl->map_mutex);
  5611. hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
  5612. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5613. "%-20zu|%-20d|0x%-20lX\n\n",
  5614. map->len, map->refs, map->raddr);
  5615. }
  5616. mutex_unlock(&fl->map_mutex);
  5617. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5618. "%-20s|%-20s\n", "secure", "attr");
  5619. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5620. "%s%s%s%s%s\n",
  5621. single_line, single_line, single_line,
  5622. single_line, single_line);
  5623. mutex_lock(&fl->map_mutex);
  5624. hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
  5625. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5626. "%-20d|0x%-20lX\n\n",
  5627. map->secure, map->attr);
  5628. }
  5629. mutex_unlock(&fl->map_mutex);
  5630. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5631. "\n======%s %s %s======\n", title,
  5632. " LIST OF BUFS ", title);
  5633. spin_lock(&fl->hlock);
  5634. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5635. "%-19s|%-19s|%-19s|%-19s\n",
  5636. "virt", "phys", "size", "flags");
  5637. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5638. "%s%s%s%s%s\n", single_line, single_line,
  5639. single_line, single_line, single_line);
  5640. hlist_for_each_entry_safe(buf, n, &fl->cached_bufs, hn) {
  5641. len += scnprintf(fileinfo + len,
  5642. DEBUGFS_SIZE - len,
  5643. "0x%-17p|0x%-17llX|%-19zu|0x%-17llX\n",
  5644. buf->virt, (uint64_t)buf->phys, buf->size, buf->flags);
  5645. }
  5646. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5647. "\n======%s %s %s======\n", title,
  5648. " LIST OF REMOTE BUFS ", title);
  5649. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5650. "%-19s|%-19s|%-19s|%-19s\n",
  5651. "virt", "phys", "size", "flags");
  5652. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5653. "%s%s%s%s%s\n", single_line, single_line,
  5654. single_line, single_line, single_line);
  5655. hlist_for_each_entry_safe(buf, n, &fl->remote_bufs, hn_rem) {
  5656. len += scnprintf(fileinfo + len,
  5657. DEBUGFS_SIZE - len,
  5658. "0x%-17p|0x%-17llX|%-19zu|0x%-17llX\n",
  5659. buf->virt, (uint64_t)buf->phys, buf->size, buf->flags);
  5660. }
  5661. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5662. "\n%s %s %s\n", title,
  5663. " LIST OF PENDING SMQCONTEXTS ", title);
  5664. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5665. "%-20s|%-10s|%-10s|%-10s|%-20s\n",
  5666. "sc", "pid", "tgid", "used", "ctxid");
  5667. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5668. "%s%s%s%s%s\n", single_line, single_line,
  5669. single_line, single_line, single_line);
  5670. hlist_for_each_entry_safe(ictx, n, &fl->clst.pending, hn) {
  5671. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5672. "0x%-18X|%-10d|%-10d|%-10zu|0x%-20llX\n\n",
  5673. ictx->sc, ictx->pid, ictx->tgid,
  5674. ictx->used, ictx->ctxid);
  5675. }
  5676. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5677. "\n%s %s %s\n", title,
  5678. " LIST OF INTERRUPTED SMQCONTEXTS ", title);
  5679. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5680. "%-20s|%-10s|%-10s|%-10s|%-20s\n",
  5681. "sc", "pid", "tgid", "used", "ctxid");
  5682. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5683. "%s%s%s%s%s\n", single_line, single_line,
  5684. single_line, single_line, single_line);
  5685. hlist_for_each_entry_safe(ictx, n, &fl->clst.interrupted, hn) {
  5686. len += scnprintf(fileinfo + len, DEBUGFS_SIZE - len,
  5687. "%-20u|%-20d|%-20d|%-20zu|0x%-20llX\n\n",
  5688. ictx->sc, ictx->pid, ictx->tgid,
  5689. ictx->used, ictx->ctxid);
  5690. }
  5691. spin_unlock(&fl->hlock);
  5692. }
  5693. if (len > DEBUGFS_SIZE)
  5694. len = DEBUGFS_SIZE;
  5695. ret = simple_read_from_buffer(buffer, count, position, fileinfo, len);
  5696. kfree(fileinfo);
  5697. bail:
  5698. return ret;
  5699. }
  5700. static const struct file_operations debugfs_fops = {
  5701. .open = simple_open,
  5702. .read = fastrpc_debugfs_read,
  5703. };
  5704. static int fastrpc_channel_open(struct fastrpc_file *fl, uint32_t flags)
  5705. {
  5706. struct fastrpc_apps *me = &gfa;
  5707. int cid = -1, err = 0;
  5708. VERIFY(err, fl && fl->sctx && fl->cid >= 0 && fl->cid < NUM_CHANNELS);
  5709. if (err) {
  5710. ADSPRPC_ERR("kernel session not initialized yet for %s\n",
  5711. current->comm);
  5712. err = -EBADR;
  5713. return err;
  5714. }
  5715. cid = fl->cid;
  5716. err = fastrpc_wait_for_transport_interrupt(cid, flags);
  5717. if (err)
  5718. goto bail;
  5719. err = verify_transport_device(cid, fl->tvm_remote_domain);
  5720. if (err)
  5721. goto bail;
  5722. mutex_lock(&me->channel[cid].smd_mutex);
  5723. if (me->channel[cid].ssrcount !=
  5724. me->channel[cid].prevssrcount) {
  5725. if (me->channel[cid].subsystemstate != SUBSYSTEM_UP) {
  5726. err = -ECONNREFUSED;
  5727. mutex_unlock(&me->channel[cid].smd_mutex);
  5728. goto bail;
  5729. }
  5730. }
  5731. fl->ssrcount = me->channel[cid].ssrcount;
  5732. if (cid == ADSP_DOMAIN_ID && me->channel[cid].ssrcount !=
  5733. me->channel[cid].prevssrcount) {
  5734. mutex_unlock(&me->channel[cid].smd_mutex);
  5735. mutex_lock(&fl->map_mutex);
  5736. err = fastrpc_mmap_remove_ssr(fl, 1);
  5737. mutex_unlock(&fl->map_mutex);
  5738. if (err)
  5739. ADSPRPC_WARN(
  5740. "failed to unmap remote heap for %s (err %d)\n",
  5741. me->channel[cid].subsys, err);
  5742. mutex_lock(&me->channel[cid].smd_mutex);
  5743. me->channel[cid].prevssrcount =
  5744. me->channel[cid].ssrcount;
  5745. }
  5746. me->channel[cid].in_hib = 0;
  5747. mutex_unlock(&me->channel[cid].smd_mutex);
  5748. bail:
  5749. return err;
  5750. }
  5751. static inline void fastrpc_register_wakeup_source(struct device *dev,
  5752. const char *client_name, struct wakeup_source **device_wake_source)
  5753. {
  5754. struct wakeup_source *wake_source = NULL;
  5755. wake_source = wakeup_source_register(dev, client_name);
  5756. if (IS_ERR_OR_NULL(wake_source)) {
  5757. ADSPRPC_ERR(
  5758. "wakeup_source_register failed for dev %s, client %s with err %ld\n",
  5759. dev_name(dev), client_name, PTR_ERR(wake_source));
  5760. return;
  5761. }
  5762. *device_wake_source = wake_source;
  5763. }
  5764. static int fastrpc_device_open(struct inode *inode, struct file *filp)
  5765. {
  5766. int err = 0;
  5767. struct fastrpc_file *fl = NULL;
  5768. struct fastrpc_apps *me = &gfa;
  5769. unsigned long irq_flags = 0;
  5770. /*
  5771. * Indicates the device node opened
  5772. * MINOR_NUM_DEV or MINOR_NUM_SECURE_DEV
  5773. */
  5774. int dev_minor = MINOR(inode->i_rdev);
  5775. VERIFY(err, ((dev_minor == MINOR_NUM_DEV) ||
  5776. (dev_minor == MINOR_NUM_SECURE_DEV)));
  5777. if (err) {
  5778. ADSPRPC_ERR("Invalid dev minor num %d\n",
  5779. dev_minor);
  5780. return err;
  5781. }
  5782. VERIFY(err, NULL != (fl = kzalloc(sizeof(*fl), GFP_KERNEL)));
  5783. if (err) {
  5784. err = -ENOMEM;
  5785. return err;
  5786. }
  5787. context_list_ctor(&fl->clst);
  5788. spin_lock_init(&fl->hlock);
  5789. spin_lock_init(&fl->aqlock);
  5790. spin_lock_init(&fl->proc_state_notif.nqlock);
  5791. INIT_HLIST_HEAD(&fl->maps);
  5792. INIT_HLIST_HEAD(&fl->cached_bufs);
  5793. fl->num_cached_buf = 0;
  5794. INIT_HLIST_HEAD(&fl->remote_bufs);
  5795. init_waitqueue_head(&fl->async_wait_queue);
  5796. init_waitqueue_head(&fl->proc_state_notif.notif_wait_queue);
  5797. INIT_HLIST_NODE(&fl->hn);
  5798. fl->sessionid = 0;
  5799. fl->tgid_open = current->tgid;
  5800. /* PD type is not known, when device is opened */
  5801. fl->pd_type = DEFAULT_UNUSED;
  5802. fl->apps = me;
  5803. fl->mode = FASTRPC_MODE_SERIAL;
  5804. fl->cid = -1;
  5805. fl->tgid_frpc = -1;
  5806. fl->tvm_remote_domain = -1;
  5807. fl->dev_minor = dev_minor;
  5808. fl->init_mem = NULL;
  5809. fl->qos_request = 0;
  5810. fl->dsp_proc_init = 0;
  5811. fl->is_ramdump_pend = false;
  5812. fl->dsp_process_state = PROCESS_CREATE_DEFAULT;
  5813. fl->is_unsigned_pd = false;
  5814. fl->is_compat = false;
  5815. fl->exit_notif = false;
  5816. fl->exit_async = false;
  5817. fl->multi_session_support = false;
  5818. fl->set_session_info = false;
  5819. init_completion(&fl->work);
  5820. init_completion(&fl->dma_invoke);
  5821. fl->file_close = FASTRPC_PROCESS_DEFAULT_STATE;
  5822. filp->private_data = fl;
  5823. fl->sharedbuf_info.buf_fd = -1;
  5824. mutex_init(&fl->internal_map_mutex);
  5825. mutex_init(&fl->map_mutex);
  5826. spin_lock_irqsave(&me->hlock, irq_flags);
  5827. hlist_add_head(&fl->hn, &me->drivers);
  5828. spin_unlock_irqrestore(&me->hlock, irq_flags);
  5829. if (me->lowest_capacity_core_count)
  5830. fl->dev_pm_qos_req = kzalloc((me->lowest_capacity_core_count) *
  5831. sizeof(struct dev_pm_qos_request),
  5832. GFP_KERNEL);
  5833. spin_lock_init(&fl->dspsignals_lock);
  5834. mutex_init(&fl->signal_create_mutex);
  5835. init_completion(&fl->shutdown);
  5836. return 0;
  5837. }
  5838. static int fastrpc_get_process_gids(struct gid_list *gidlist)
  5839. {
  5840. struct group_info *group_info = get_current_groups();
  5841. int i = 0, err = 0, num_gids = group_info->ngroups + 1;
  5842. unsigned int *gids = NULL;
  5843. gids = kcalloc(num_gids, sizeof(unsigned int), GFP_KERNEL);
  5844. if (!gids) {
  5845. err = -ENOMEM;
  5846. goto bail;
  5847. }
  5848. /* Get the real GID */
  5849. gids[0] = __kgid_val(current_gid());
  5850. /* Get the supplemental GIDs */
  5851. for (i = 1; i < num_gids; i++)
  5852. gids[i] = __kgid_val(group_info->gid[i - 1]);
  5853. sort(gids, num_gids, sizeof(*gids), uint_cmp_func, NULL);
  5854. gidlist->gids = gids;
  5855. gidlist->gidcount = num_gids;
  5856. bail:
  5857. if (err)
  5858. kfree(gids);
  5859. return err;
  5860. }
  5861. // Generate a unique process ID to DSP process
  5862. static int get_unique_hlos_process_id(void)
  5863. {
  5864. int tgid_frpc = -1, tgid_index = 1;
  5865. struct fastrpc_apps *me = &gfa;
  5866. unsigned long irq_flags = 0;
  5867. spin_lock_irqsave(&me->hlock, irq_flags);
  5868. for (tgid_index = 1; tgid_index < MAX_FRPC_TGID; tgid_index++) {
  5869. if (!frpc_tgid_usage_array[tgid_index]) {
  5870. tgid_frpc = tgid_index;
  5871. /* Set the tgid usage to false */
  5872. frpc_tgid_usage_array[tgid_index] = true;
  5873. break;
  5874. }
  5875. }
  5876. spin_unlock_irqrestore(&me->hlock, irq_flags);
  5877. return tgid_frpc;
  5878. }
  5879. static int fastrpc_set_process_info(struct fastrpc_file *fl, uint32_t cid)
  5880. {
  5881. int err = 0, buf_size = 0;
  5882. char strpid[PID_SIZE];
  5883. char cur_comm[TASK_COMM_LEN];
  5884. memcpy(cur_comm, current->comm, TASK_COMM_LEN);
  5885. cur_comm[TASK_COMM_LEN-1] = '\0';
  5886. fl->tgid = current->tgid;
  5887. fl->tgid_frpc = get_unique_hlos_process_id();
  5888. VERIFY(err, fl->tgid_frpc != -1);
  5889. if (err) {
  5890. ADSPRPC_ERR("too many fastrpc clients, max %u allowed\n", MAX_FRPC_TGID);
  5891. err = -EUSERS;
  5892. goto bail;
  5893. }
  5894. ADSPRPC_INFO("HLOS pid %d, cid %d is mapped to unique sessions pid %d",
  5895. fl->tgid, cid, fl->tgid_frpc);
  5896. /*
  5897. * Third-party apps don't have permission to open the fastrpc device, so
  5898. * it is opened on their behalf by DSP HAL. This is detected by
  5899. * comparing current PID with the one stored during device open.
  5900. */
  5901. if (current->tgid != fl->tgid_open)
  5902. fl->untrusted_process = true;
  5903. snprintf(strpid, PID_SIZE, "%d", current->pid);
  5904. if (debugfs_root) {
  5905. VERIFY(err, VALID_FASTRPC_CID(cid));
  5906. if (err) {
  5907. err = -ECHRNG;
  5908. goto bail;
  5909. }
  5910. buf_size = strlen(cur_comm) + strlen("_") + strlen(strpid)
  5911. + strlen("_") + strlen(__TOSTR__(NUM_CHANNELS)) + 1;
  5912. spin_lock(&fl->hlock);
  5913. if (fl->debug_buf_alloced_attempted) {
  5914. spin_unlock(&fl->hlock);
  5915. return err;
  5916. }
  5917. fl->debug_buf_alloced_attempted = 1;
  5918. spin_unlock(&fl->hlock);
  5919. fl->debug_buf = kzalloc(buf_size, GFP_KERNEL);
  5920. if (!fl->debug_buf) {
  5921. err = -ENOMEM;
  5922. return err;
  5923. }
  5924. /* Use HLOS PID, unique fastrpc PID, CID in debugfs filename,
  5925. * for better ability to debug
  5926. */
  5927. snprintf(fl->debug_buf, buf_size, "%.10s%s%d%s%d%s%d",
  5928. cur_comm, "_", current->pid, "_", fl->tgid_frpc, "_", cid);
  5929. fl->debugfs_file = debugfs_create_file(fl->debug_buf, 0644,
  5930. debugfs_root, fl, &debugfs_fops);
  5931. if (IS_ERR_OR_NULL(fl->debugfs_file)) {
  5932. pr_warn("Error: %s: %s: failed to create debugfs file %s\n",
  5933. cur_comm, __func__, fl->debug_buf);
  5934. fl->debugfs_file = NULL;
  5935. }
  5936. kfree(fl->debug_buf);
  5937. fl->debug_buf = NULL;
  5938. }
  5939. bail:
  5940. return err;
  5941. }
  5942. int fastrpc_get_info(struct fastrpc_file *fl, uint32_t *info)
  5943. {
  5944. int err = 0;
  5945. uint32_t cid = *info;
  5946. struct fastrpc_apps *me = &gfa;
  5947. VERIFY(err, fl != NULL);
  5948. if (err) {
  5949. err = -EBADF;
  5950. goto bail;
  5951. }
  5952. spin_lock(&fl->hlock);
  5953. if (fl->set_session_info) {
  5954. spin_unlock(&fl->hlock);
  5955. ADSPRPC_ERR("Set session info invoked multiple times\n");
  5956. err = -EBADR;
  5957. goto bail;
  5958. }
  5959. // Set set_session_info to true
  5960. fl->set_session_info = true;
  5961. spin_unlock(&fl->hlock);
  5962. VERIFY(err, VALID_FASTRPC_CID(cid));
  5963. if (err) {
  5964. err = -ECHRNG;
  5965. goto bail;
  5966. }
  5967. fastrpc_get_process_gids(&fl->gidlist);
  5968. err = fastrpc_set_process_info(fl, cid);
  5969. if (err)
  5970. goto bail;
  5971. if (fl->cid == -1) {
  5972. struct fastrpc_channel_ctx *chan = NULL;
  5973. chan = &me->channel[cid];
  5974. /* Check to see if the device node is non-secure */
  5975. if (fl->dev_minor == MINOR_NUM_DEV) {
  5976. /*
  5977. * If an app is trying to offload to a secure remote
  5978. * channel by opening the non-secure device node, allow
  5979. * the access if the subsystem supports unsigned
  5980. * offload. Untrusted apps will be restricted from
  5981. * offloading to signed PD using DSP HAL.
  5982. */
  5983. if (chan->secure == SECURE_CHANNEL
  5984. && !chan->unsigned_support) {
  5985. ADSPRPC_ERR(
  5986. "cannot use domain %d with non-secure device\n",
  5987. cid);
  5988. err = -EACCES;
  5989. goto bail;
  5990. }
  5991. }
  5992. fl->cid = cid;
  5993. fl->ssrcount = fl->apps->channel[cid].ssrcount;
  5994. mutex_lock(&fl->apps->channel[cid].smd_mutex);
  5995. err = fastrpc_session_alloc_locked(&fl->apps->channel[cid],
  5996. 0, fl->sharedcb, fl->pd_type, &fl->sctx);
  5997. mutex_unlock(&fl->apps->channel[cid].smd_mutex);
  5998. if (err == -EUSERS) {
  5999. ADSPRPC_WARN(
  6000. "max concurrent sessions limit (%d) already reached on %s err %d\n",
  6001. chan->sesscount, chan->subsys, err);
  6002. }
  6003. if (err)
  6004. goto bail;
  6005. }
  6006. VERIFY(err, fl->sctx != NULL);
  6007. if (err) {
  6008. err = -EBADR;
  6009. goto bail;
  6010. }
  6011. *info = (fl->sctx->smmu.enabled ? 1 : 0);
  6012. bail:
  6013. return err;
  6014. }
  6015. static int fastrpc_manage_poll_mode(struct fastrpc_file *fl, uint32_t enable, uint32_t timeout)
  6016. {
  6017. int err = 0;
  6018. const unsigned int MAX_POLL_TIMEOUT_US = 10000;
  6019. if ((fl->cid != CDSP_DOMAIN_ID) || (fl->proc_flags != FASTRPC_INIT_CREATE)) {
  6020. err = -EPERM;
  6021. ADSPRPC_ERR("flags %d, cid %d, poll mode allowed only for dynamic CDSP process\n",
  6022. fl->proc_flags, fl->cid);
  6023. goto bail;
  6024. }
  6025. if (timeout > MAX_POLL_TIMEOUT_US) {
  6026. err = -EBADMSG;
  6027. ADSPRPC_ERR("poll timeout %u is greater than max allowed value %u\n",
  6028. timeout, MAX_POLL_TIMEOUT_US);
  6029. goto bail;
  6030. }
  6031. spin_lock(&fl->hlock);
  6032. if (enable) {
  6033. fl->poll_mode = true;
  6034. fl->poll_timeout = timeout;
  6035. } else {
  6036. fl->poll_mode = false;
  6037. fl->poll_timeout = 0;
  6038. }
  6039. spin_unlock(&fl->hlock);
  6040. ADSPRPC_INFO("updated poll mode to %d, timeout %u\n", enable, timeout);
  6041. bail:
  6042. return err;
  6043. }
  6044. int fastrpc_internal_control(struct fastrpc_file *fl,
  6045. struct fastrpc_ioctl_control *cp)
  6046. {
  6047. int err = 0;
  6048. unsigned int latency;
  6049. struct fastrpc_apps *me = &gfa;
  6050. unsigned int cpu;
  6051. unsigned long flags = 0;
  6052. VERIFY(err, !IS_ERR_OR_NULL(fl) && !IS_ERR_OR_NULL(fl->apps));
  6053. if (err) {
  6054. err = -EBADF;
  6055. goto bail;
  6056. }
  6057. VERIFY(err, !IS_ERR_OR_NULL(cp));
  6058. if (err) {
  6059. err = -EINVAL;
  6060. goto bail;
  6061. }
  6062. switch (cp->req) {
  6063. case FASTRPC_CONTROL_LATENCY:
  6064. latency = cp->lp.enable == FASTRPC_LATENCY_CTRL_ENB ?
  6065. fl->apps->latency : PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
  6066. VERIFY(err, latency != 0);
  6067. if (err) {
  6068. err = -EINVAL;
  6069. goto bail;
  6070. }
  6071. VERIFY(err, (me->lowest_capacity_core_count && fl->dev_pm_qos_req));
  6072. if (err) {
  6073. ADSPRPC_INFO("Skipping PM QoS latency voting, core count: %u\n",
  6074. me->lowest_capacity_core_count);
  6075. err = -EINVAL;
  6076. goto bail;
  6077. }
  6078. /*
  6079. * Add voting request for all possible cores corresponding to cluster
  6080. * id 0. If DT property 'qcom,single-core-latency-vote' is enabled
  6081. * then add voting request for only one core of cluster id 0.
  6082. */
  6083. for (cpu = 0; cpu < me->lowest_capacity_core_count; cpu++) {
  6084. if (!fl->qos_request) {
  6085. err = dev_pm_qos_add_request(
  6086. get_cpu_device(cpu),
  6087. &fl->dev_pm_qos_req[cpu],
  6088. DEV_PM_QOS_RESUME_LATENCY,
  6089. latency);
  6090. } else {
  6091. err = dev_pm_qos_update_request(
  6092. &fl->dev_pm_qos_req[cpu],
  6093. latency);
  6094. }
  6095. /* PM QoS request APIs return 0 or 1 on success */
  6096. if (err < 0) {
  6097. ADSPRPC_WARN("QoS with lat %u failed for CPU %d, err %d, req %d\n",
  6098. latency, cpu, err, fl->qos_request);
  6099. break;
  6100. }
  6101. }
  6102. if (err >= 0) {
  6103. fl->qos_request = 1;
  6104. err = 0;
  6105. }
  6106. /* Ensure CPU feature map updated to DSP for early WakeUp */
  6107. fastrpc_send_cpuinfo_to_dsp(fl);
  6108. break;
  6109. case FASTRPC_CONTROL_KALLOC:
  6110. cp->kalloc.kalloc_support = 1;
  6111. break;
  6112. case FASTRPC_CONTROL_WAKELOCK:
  6113. if (fl->dev_minor != MINOR_NUM_SECURE_DEV) {
  6114. ADSPRPC_ERR(
  6115. "PM voting not allowed for non-secure device node %d\n",
  6116. fl->dev_minor);
  6117. err = -EPERM;
  6118. goto bail;
  6119. }
  6120. fl->wake_enable = cp->wp.enable;
  6121. break;
  6122. case FASTRPC_CONTROL_PM:
  6123. if (!fl->wake_enable) {
  6124. /* Kernel PM voting not requested by this application */
  6125. err = -EACCES;
  6126. goto bail;
  6127. }
  6128. if (cp->pm.timeout > MAX_PM_TIMEOUT_MS)
  6129. fl->ws_timeout = MAX_PM_TIMEOUT_MS;
  6130. else
  6131. fl->ws_timeout = cp->pm.timeout;
  6132. VERIFY(err, VALID_FASTRPC_CID(fl->cid));
  6133. if (err) {
  6134. err = -ECHRNG;
  6135. goto bail;
  6136. }
  6137. fastrpc_pm_awake(fl, gcinfo[fl->cid].secure);
  6138. break;
  6139. case FASTRPC_CONTROL_DSPPROCESS_CLEAN:
  6140. (void)fastrpc_release_current_dsp_process(fl);
  6141. fastrpc_queue_pd_status(fl, fl->cid, FASTRPC_USER_PD_FORCE_KILL, fl->sessionid);
  6142. break;
  6143. case FASTRPC_CONTROL_RPC_POLL:
  6144. err = fastrpc_manage_poll_mode(fl, cp->lp.enable, cp->lp.latency);
  6145. if (err)
  6146. goto bail;
  6147. break;
  6148. case FASTRPC_CONTROL_SMMU:
  6149. fl->sharedcb = cp->smmu.sharedcb;
  6150. break;
  6151. case FASTRPC_CONTROL_ASYNC_WAKE:
  6152. fl->exit_async = true;
  6153. spin_lock_irqsave(&fl->aqlock, flags);
  6154. atomic_add(1, &fl->async_queue_job_count);
  6155. wake_up_interruptible(&fl->async_wait_queue);
  6156. spin_unlock_irqrestore(&fl->aqlock, flags);
  6157. break;
  6158. case FASTRPC_CONTROL_NOTIF_WAKE:
  6159. fl->exit_notif = true;
  6160. spin_lock_irqsave(&fl->proc_state_notif.nqlock, flags);
  6161. atomic_add(1, &fl->proc_state_notif.notif_queue_count);
  6162. wake_up_interruptible(&fl->proc_state_notif.notif_wait_queue);
  6163. spin_unlock_irqrestore(&fl->proc_state_notif.nqlock, flags);
  6164. break;
  6165. default:
  6166. err = -EBADRQC;
  6167. break;
  6168. }
  6169. bail:
  6170. return err;
  6171. }
  6172. /* Wait for PD to be up before audio or sensors daemons try connecting */
  6173. static int fastrpc_check_pd_status(struct fastrpc_file *fl, char *sloc_name)
  6174. {
  6175. int err = 0, session = -1, cid = -1;
  6176. struct fastrpc_apps *me = &gfa;
  6177. if (fl->servloc_name && sloc_name
  6178. && !strcmp(fl->servloc_name, sloc_name)) {
  6179. err = fastrpc_get_spd_session(sloc_name, &session, &cid);
  6180. if (err || cid != fl->cid)
  6181. goto bail;
  6182. #if IS_ENABLED(CONFIG_QCOM_PDR_HELPERS)
  6183. if (!strcmp(fl->servloc_name,
  6184. AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME) || !strcmp(fl->servloc_name,
  6185. SENSORS_PDR_ADSP_SERVICE_LOCATION_CLIENT_NAME) ||
  6186. !strcmp(fl->servloc_name,
  6187. SENSORS_PDR_SLPI_SERVICE_LOCATION_CLIENT_NAME)) {
  6188. err = wait_event_interruptible(
  6189. me->channel[cid].spd[session].wait_for_pdup,
  6190. atomic_read(&me->channel[cid].spd[session].ispdup));
  6191. goto bail;
  6192. }
  6193. #else
  6194. (void)me;
  6195. #endif
  6196. }
  6197. bail:
  6198. return err;
  6199. }
  6200. int fastrpc_setmode(unsigned long ioctl_param,
  6201. struct fastrpc_file *fl)
  6202. {
  6203. int err = 0;
  6204. switch ((uint32_t)ioctl_param) {
  6205. case FASTRPC_MODE_PARALLEL:
  6206. case FASTRPC_MODE_SERIAL:
  6207. fl->mode = (uint32_t)ioctl_param;
  6208. break;
  6209. case FASTRPC_MODE_PROFILE:
  6210. fl->profile = (uint32_t)ioctl_param;
  6211. break;
  6212. case FASTRPC_MODE_SESSION:
  6213. if (!fl->multi_session_support)
  6214. fl->sessionid = 1;
  6215. break;
  6216. default:
  6217. err = -ENOTTY;
  6218. break;
  6219. }
  6220. return err;
  6221. }
  6222. int fastrpc_control(struct fastrpc_ioctl_control *cp,
  6223. void *param, struct fastrpc_file *fl)
  6224. {
  6225. int err = 0;
  6226. K_COPY_FROM_USER(err, 0, cp, param,
  6227. sizeof(*cp));
  6228. if (err) {
  6229. err = -EFAULT;
  6230. goto bail;
  6231. }
  6232. VERIFY(err, 0 == (err = fastrpc_internal_control(fl, cp)));
  6233. if (err)
  6234. goto bail;
  6235. if (cp->req == FASTRPC_CONTROL_KALLOC) {
  6236. K_COPY_TO_USER(err, 0, param, cp, sizeof(*cp));
  6237. if (err) {
  6238. err = -EFAULT;
  6239. goto bail;
  6240. }
  6241. }
  6242. bail:
  6243. return err;
  6244. }
  6245. static int fastrpc_get_dsp_info(
  6246. struct fastrpc_ioctl_capability *cap,
  6247. void *param, struct fastrpc_file *fl)
  6248. {
  6249. int err = 0;
  6250. K_COPY_FROM_USER(err, 0, cap, param,
  6251. sizeof(struct fastrpc_ioctl_capability));
  6252. VERIFY(err, cap->domain < NUM_CHANNELS);
  6253. if (err) {
  6254. err = -ECHRNG;
  6255. goto bail;
  6256. }
  6257. cap->capability = 0;
  6258. err = fastrpc_get_info_from_kernel(cap, fl);
  6259. if (err)
  6260. goto bail;
  6261. K_COPY_TO_USER(err, 0, &((struct fastrpc_ioctl_capability *)
  6262. param)->capability, &cap->capability, sizeof(cap->capability));
  6263. bail:
  6264. return err;
  6265. }
  6266. int fastrpc_dspsignal_signal(struct fastrpc_file *fl,
  6267. struct fastrpc_ioctl_dspsignal_signal *sig)
  6268. {
  6269. int err = 0, cid = -1;
  6270. struct fastrpc_channel_ctx *channel_ctx = NULL;
  6271. uint64_t msg = 0;
  6272. // We don't check if the signal has even been allocated since we don't
  6273. // track outgoing signals in the driver. The userspace library does a
  6274. // basic sanity check and any security validation needs to be done by
  6275. // the recipient.
  6276. DSPSIGNAL_VERBOSE("Send signal PID %u, unique fastrpc pid %u signal %u\n",
  6277. (unsigned int)fl->tgid, (unsigned int)fl->tgid_frpc,
  6278. (unsigned int)sig->signal_id);
  6279. VERIFY(err, sig->signal_id < DSPSIGNAL_NUM_SIGNALS);
  6280. if (err) {
  6281. ADSPRPC_ERR("Sending bad signal %u for PID %u",
  6282. sig->signal_id, (unsigned int)fl->tgid);
  6283. err = -EBADR;
  6284. goto bail;
  6285. }
  6286. cid = fl->cid;
  6287. VERIFY(err, VALID_FASTRPC_CID(cid) && fl->sctx != NULL);
  6288. if (err) {
  6289. err = -EBADR;
  6290. goto bail;
  6291. }
  6292. channel_ctx = &fl->apps->channel[cid];
  6293. mutex_lock(&channel_ctx->smd_mutex);
  6294. if (fl->ssrcount != channel_ctx->ssrcount) {
  6295. err = -ECONNRESET;
  6296. mutex_unlock(&channel_ctx->smd_mutex);
  6297. goto bail;
  6298. }
  6299. /* Use unique fastrpc pid, to signal DSP process */
  6300. msg = (((uint64_t)fl->tgid_frpc) << 32) | ((uint64_t)sig->signal_id);
  6301. err = fastrpc_transport_send(cid, (void *)&msg, sizeof(msg), fl->tvm_remote_domain);
  6302. mutex_unlock(&channel_ctx->smd_mutex);
  6303. trace_fastrpc_dspsignal("signal", sig->signal_id, 0, 0);
  6304. bail:
  6305. return err;
  6306. }
  6307. int fastrpc_dspsignal_wait(struct fastrpc_file *fl,
  6308. struct fastrpc_ioctl_dspsignal_wait *wait)
  6309. {
  6310. int err = 0, cid = -1;
  6311. unsigned long timeout = usecs_to_jiffies(wait->timeout_usec);
  6312. uint32_t signal_id = wait->signal_id;
  6313. struct fastrpc_dspsignal *s = NULL;
  6314. long ret = 0;
  6315. unsigned long irq_flags = 0;
  6316. DSPSIGNAL_VERBOSE("Wait for signal %u\n", signal_id);
  6317. VERIFY(err, signal_id < DSPSIGNAL_NUM_SIGNALS);
  6318. if (err) {
  6319. ADSPRPC_ERR("Waiting on bad signal %u", signal_id);
  6320. err = -EINVAL;
  6321. goto bail;
  6322. }
  6323. cid = fl->cid;
  6324. VERIFY(err, VALID_FASTRPC_CID(cid) && fl->sctx != NULL);
  6325. if (err) {
  6326. err = -EBADR;
  6327. goto bail;
  6328. }
  6329. spin_lock_irqsave(&fl->dspsignals_lock, irq_flags);
  6330. if (fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE] != NULL) {
  6331. struct fastrpc_dspsignal *group =
  6332. fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE];
  6333. s = &group[signal_id % DSPSIGNAL_GROUP_SIZE];
  6334. }
  6335. if ((s == NULL) || (s->state == DSPSIGNAL_STATE_UNUSED)) {
  6336. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6337. ADSPRPC_ERR("Unknown signal id %u\n", signal_id);
  6338. err = -ENOENT;
  6339. goto bail;
  6340. }
  6341. if (s->state != DSPSIGNAL_STATE_PENDING) {
  6342. if ((s->state == DSPSIGNAL_STATE_CANCELED) || (s->state == DSPSIGNAL_STATE_UNUSED))
  6343. err = -EINTR;
  6344. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6345. DSPSIGNAL_VERBOSE("Signal %u in state %u, complete wait immediately",
  6346. signal_id, s->state);
  6347. goto bail;
  6348. }
  6349. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6350. trace_fastrpc_dspsignal("wait", signal_id, s->state, wait->timeout_usec);
  6351. if (timeout != 0xffffffff)
  6352. ret = wait_for_completion_interruptible_timeout(&s->comp, timeout);
  6353. else
  6354. ret = wait_for_completion_interruptible(&s->comp);
  6355. trace_fastrpc_dspsignal("wakeup", signal_id, s->state, wait->timeout_usec);
  6356. if (ret == 0) {
  6357. DSPSIGNAL_VERBOSE("Wait for signal %u timed out\n", signal_id);
  6358. err = -ETIMEDOUT;
  6359. goto bail;
  6360. } else if (ret < 0) {
  6361. ADSPRPC_ERR("Wait for signal %u failed %d\n", signal_id, (int)ret);
  6362. err = ret;
  6363. goto bail;
  6364. }
  6365. spin_lock_irqsave(&fl->dspsignals_lock, irq_flags);
  6366. if (s->state == DSPSIGNAL_STATE_SIGNALED) {
  6367. s->state = DSPSIGNAL_STATE_PENDING;
  6368. DSPSIGNAL_VERBOSE("Signal %u completed\n", signal_id);
  6369. } else if ((s->state == DSPSIGNAL_STATE_CANCELED) || (s->state == DSPSIGNAL_STATE_UNUSED)) {
  6370. DSPSIGNAL_VERBOSE("Signal %u cancelled or destroyed\n", signal_id);
  6371. err = -EINTR;
  6372. }
  6373. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6374. bail:
  6375. return err;
  6376. }
  6377. int fastrpc_dspsignal_create(struct fastrpc_file *fl,
  6378. struct fastrpc_ioctl_dspsignal_create *create)
  6379. {
  6380. int err = 0, cid = -1;
  6381. uint32_t signal_id = create->signal_id;
  6382. struct fastrpc_dspsignal *group, *sig;
  6383. unsigned long irq_flags = 0;
  6384. VERIFY(err, signal_id < DSPSIGNAL_NUM_SIGNALS);
  6385. if (err) {
  6386. err = -EINVAL;
  6387. goto bail;
  6388. }
  6389. cid = fl->cid;
  6390. VERIFY(err, VALID_FASTRPC_CID(cid) && fl->sctx != NULL);
  6391. if (err) {
  6392. err = -EBADR;
  6393. goto bail;
  6394. }
  6395. // Use a separate mutex for creating signals. This avoids holding on
  6396. // to a spinlock if we need to allocate a whole group of signals. The
  6397. // mutex ensures nobody else will allocate the same group.
  6398. mutex_lock(&fl->signal_create_mutex);
  6399. spin_lock_irqsave(&fl->dspsignals_lock, irq_flags);
  6400. group = fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE];
  6401. if (group == NULL) {
  6402. int i;
  6403. // Release the spinlock while we allocate a new group but take
  6404. // it back before taking the group into use. No other code
  6405. // allocates groups so the mutex is sufficient.
  6406. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6407. VERIFY(err, (group = kzalloc(DSPSIGNAL_GROUP_SIZE * sizeof(*group),
  6408. GFP_KERNEL)) != NULL);
  6409. if (err) {
  6410. ADSPRPC_ERR("Unable to allocate signal group\n");
  6411. err = -ENOMEM;
  6412. mutex_unlock(&fl->signal_create_mutex);
  6413. goto bail;
  6414. }
  6415. for (i = 0; i < DSPSIGNAL_GROUP_SIZE; i++) {
  6416. sig = &group[i];
  6417. init_completion(&sig->comp);
  6418. sig->state = DSPSIGNAL_STATE_UNUSED;
  6419. }
  6420. spin_lock_irqsave(&fl->dspsignals_lock, irq_flags);
  6421. fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE] = group;
  6422. }
  6423. sig = &group[signal_id % DSPSIGNAL_GROUP_SIZE];
  6424. if (sig->state != DSPSIGNAL_STATE_UNUSED) {
  6425. err = -EBUSY;
  6426. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6427. mutex_unlock(&fl->signal_create_mutex);
  6428. ADSPRPC_ERR("Attempting to create signal %u already in use (state %u)\n",
  6429. signal_id, sig->state);
  6430. goto bail;
  6431. }
  6432. sig->state = DSPSIGNAL_STATE_PENDING;
  6433. reinit_completion(&sig->comp);
  6434. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6435. mutex_unlock(&fl->signal_create_mutex);
  6436. DSPSIGNAL_VERBOSE("Signal %u created\n", signal_id);
  6437. bail:
  6438. return err;
  6439. }
  6440. int fastrpc_dspsignal_destroy(struct fastrpc_file *fl,
  6441. struct fastrpc_ioctl_dspsignal_destroy *destroy)
  6442. {
  6443. int err = 0, cid = -1;
  6444. uint32_t signal_id = destroy->signal_id;
  6445. struct fastrpc_dspsignal *s = NULL;
  6446. unsigned long irq_flags = 0;
  6447. DSPSIGNAL_VERBOSE("Destroy signal %u\n", signal_id);
  6448. VERIFY(err, signal_id < DSPSIGNAL_NUM_SIGNALS);
  6449. if (err) {
  6450. err = -EINVAL;
  6451. goto bail;
  6452. }
  6453. cid = fl->cid;
  6454. VERIFY(err, VALID_FASTRPC_CID(cid) && fl->sctx != NULL);
  6455. if (err) {
  6456. err = -EBADR;
  6457. goto bail;
  6458. }
  6459. spin_lock_irqsave(&fl->dspsignals_lock, irq_flags);
  6460. if (fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE] != NULL) {
  6461. struct fastrpc_dspsignal *group =
  6462. fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE];
  6463. s = &group[signal_id % DSPSIGNAL_GROUP_SIZE];
  6464. }
  6465. if ((s == NULL) || (s->state == DSPSIGNAL_STATE_UNUSED)) {
  6466. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6467. ADSPRPC_ERR("Attempting to destroy unused signal %u\n", signal_id);
  6468. err = -ENOENT;
  6469. goto bail;
  6470. }
  6471. s->state = DSPSIGNAL_STATE_UNUSED;
  6472. complete_all(&s->comp);
  6473. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6474. DSPSIGNAL_VERBOSE("Signal %u destroyed\n", signal_id);
  6475. bail:
  6476. return err;
  6477. }
  6478. int fastrpc_dspsignal_cancel_wait(struct fastrpc_file *fl,
  6479. struct fastrpc_ioctl_dspsignal_cancel_wait *cancel)
  6480. {
  6481. int err = 0, cid = -1;
  6482. uint32_t signal_id = cancel->signal_id;
  6483. struct fastrpc_dspsignal *s = NULL;
  6484. unsigned long irq_flags = 0;
  6485. DSPSIGNAL_VERBOSE("Cancel wait for signal %u\n", signal_id);
  6486. VERIFY(err, signal_id < DSPSIGNAL_NUM_SIGNALS);
  6487. if (err) {
  6488. err = -EINVAL;
  6489. goto bail;
  6490. }
  6491. cid = fl->cid;
  6492. VERIFY(err, VALID_FASTRPC_CID(cid) && fl->sctx != NULL);
  6493. if (err) {
  6494. err = -EBADR;
  6495. goto bail;
  6496. }
  6497. spin_lock_irqsave(&fl->dspsignals_lock, irq_flags);
  6498. if (fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE] != NULL) {
  6499. struct fastrpc_dspsignal *group =
  6500. fl->signal_groups[signal_id / DSPSIGNAL_GROUP_SIZE];
  6501. s = &group[signal_id % DSPSIGNAL_GROUP_SIZE];
  6502. }
  6503. if ((s == NULL) || (s->state == DSPSIGNAL_STATE_UNUSED)) {
  6504. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6505. ADSPRPC_ERR("Attempting to cancel unused signal %u\n", signal_id);
  6506. err = -ENOENT;
  6507. goto bail;
  6508. }
  6509. if (s->state != DSPSIGNAL_STATE_CANCELED) {
  6510. s->state = DSPSIGNAL_STATE_CANCELED;
  6511. trace_fastrpc_dspsignal("cancel", signal_id, s->state, 0);
  6512. complete_all(&s->comp);
  6513. }
  6514. spin_unlock_irqrestore(&fl->dspsignals_lock, irq_flags);
  6515. DSPSIGNAL_VERBOSE("Signal %u cancelled\n", signal_id);
  6516. bail:
  6517. return err;
  6518. }
  6519. static inline int fastrpc_mmap_device_ioctl(struct fastrpc_file *fl,
  6520. unsigned int ioctl_num, union fastrpc_ioctl_param *p,
  6521. void *param)
  6522. {
  6523. union {
  6524. struct fastrpc_ioctl_mmap mmap;
  6525. struct fastrpc_ioctl_munmap munmap;
  6526. } i;
  6527. int err = 0;
  6528. switch (ioctl_num) {
  6529. case FASTRPC_IOCTL_MEM_MAP:
  6530. K_COPY_FROM_USER(err, 0, &p->mem_map, param,
  6531. sizeof(p->mem_map));
  6532. if (err) {
  6533. err = -EFAULT;
  6534. goto bail;
  6535. }
  6536. VERIFY(err, 0 == (err = fastrpc_internal_mem_map(fl,
  6537. &p->mem_map)));
  6538. if (err)
  6539. goto bail;
  6540. K_COPY_TO_USER(err, 0, param, &p->mem_map, sizeof(p->mem_map));
  6541. if (err) {
  6542. err = -EFAULT;
  6543. goto bail;
  6544. }
  6545. break;
  6546. case FASTRPC_IOCTL_MEM_UNMAP:
  6547. K_COPY_FROM_USER(err, 0, &p->mem_unmap, param,
  6548. sizeof(p->mem_unmap));
  6549. if (err) {
  6550. err = -EFAULT;
  6551. goto bail;
  6552. }
  6553. VERIFY(err, 0 == (err = fastrpc_internal_mem_unmap(fl,
  6554. &p->mem_unmap)));
  6555. if (err)
  6556. goto bail;
  6557. K_COPY_TO_USER(err, 0, param, &p->mem_unmap,
  6558. sizeof(p->mem_unmap));
  6559. if (err) {
  6560. err = -EFAULT;
  6561. goto bail;
  6562. }
  6563. break;
  6564. case FASTRPC_IOCTL_MMAP:
  6565. K_COPY_FROM_USER(err, 0, &p->mmap, param,
  6566. sizeof(p->mmap));
  6567. if (err) {
  6568. err = -EFAULT;
  6569. goto bail;
  6570. }
  6571. VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &p->mmap)));
  6572. if (err)
  6573. goto bail;
  6574. K_COPY_TO_USER(err, 0, param, &p->mmap, sizeof(p->mmap));
  6575. if (err) {
  6576. err = -EFAULT;
  6577. goto bail;
  6578. }
  6579. break;
  6580. case FASTRPC_IOCTL_MUNMAP:
  6581. K_COPY_FROM_USER(err, 0, &p->munmap, param,
  6582. sizeof(p->munmap));
  6583. if (err) {
  6584. err = -EFAULT;
  6585. goto bail;
  6586. }
  6587. VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl,
  6588. &p->munmap)));
  6589. if (err)
  6590. goto bail;
  6591. break;
  6592. case FASTRPC_IOCTL_MMAP_64:
  6593. K_COPY_FROM_USER(err, 0, &p->mmap64, param,
  6594. sizeof(p->mmap64));
  6595. if (err) {
  6596. err = -EFAULT;
  6597. goto bail;
  6598. }
  6599. get_fastrpc_ioctl_mmap_64(&p->mmap64, &i.mmap);
  6600. VERIFY(err, 0 == (err = fastrpc_internal_mmap(fl, &i.mmap)));
  6601. if (err)
  6602. goto bail;
  6603. put_fastrpc_ioctl_mmap_64(&p->mmap64, &i.mmap);
  6604. K_COPY_TO_USER(err, 0, param, &p->mmap64, sizeof(p->mmap64));
  6605. if (err) {
  6606. err = -EFAULT;
  6607. goto bail;
  6608. }
  6609. break;
  6610. case FASTRPC_IOCTL_MUNMAP_64:
  6611. K_COPY_FROM_USER(err, 0, &p->munmap64, param,
  6612. sizeof(p->munmap64));
  6613. if (err) {
  6614. err = -EFAULT;
  6615. goto bail;
  6616. }
  6617. get_fastrpc_ioctl_munmap_64(&p->munmap64, &i.munmap);
  6618. VERIFY(err, 0 == (err = fastrpc_internal_munmap(fl,
  6619. &i.munmap)));
  6620. if (err)
  6621. goto bail;
  6622. break;
  6623. case FASTRPC_IOCTL_MUNMAP_FD:
  6624. K_COPY_FROM_USER(err, 0, &p->munmap_fd, param,
  6625. sizeof(p->munmap_fd));
  6626. if (err) {
  6627. err = -EFAULT;
  6628. goto bail;
  6629. }
  6630. VERIFY(err, 0 == (err = fastrpc_internal_munmap_fd(fl,
  6631. &p->munmap_fd)));
  6632. if (err)
  6633. goto bail;
  6634. break;
  6635. default:
  6636. err = -ENOTTY;
  6637. pr_info("bad ioctl: %d\n", ioctl_num);
  6638. break;
  6639. }
  6640. bail:
  6641. return err;
  6642. }
  6643. static long fastrpc_device_ioctl(struct file *file, unsigned int ioctl_num,
  6644. unsigned long ioctl_param)
  6645. {
  6646. union fastrpc_ioctl_param p;
  6647. void *param = (char *)ioctl_param;
  6648. struct fastrpc_file *fl = (struct fastrpc_file *)file->private_data;
  6649. int size = 0, err = 0;
  6650. uint32_t info;
  6651. p.inv.fds = NULL;
  6652. p.inv.attrs = NULL;
  6653. p.inv.crc = NULL;
  6654. p.inv.perf_kernel = NULL;
  6655. p.inv.perf_dsp = NULL;
  6656. p.inv.job = NULL;
  6657. spin_lock(&fl->hlock);
  6658. if (fl->file_close >= FASTRPC_PROCESS_EXIT_START) {
  6659. err = -ESHUTDOWN;
  6660. pr_warn("adsprpc: fastrpc_device_release is happening, So not sending any new requests to DSP\n");
  6661. spin_unlock(&fl->hlock);
  6662. goto bail;
  6663. }
  6664. spin_unlock(&fl->hlock);
  6665. switch (ioctl_num) {
  6666. case FASTRPC_IOCTL_INVOKE:
  6667. size = sizeof(struct fastrpc_ioctl_invoke);
  6668. fallthrough;
  6669. case FASTRPC_IOCTL_INVOKE_FD:
  6670. if (!size)
  6671. size = sizeof(struct fastrpc_ioctl_invoke_fd);
  6672. fallthrough;
  6673. case FASTRPC_IOCTL_INVOKE_ATTRS:
  6674. if (!size)
  6675. size = sizeof(struct fastrpc_ioctl_invoke_attrs);
  6676. fallthrough;
  6677. case FASTRPC_IOCTL_INVOKE_CRC:
  6678. if (!size)
  6679. size = sizeof(struct fastrpc_ioctl_invoke_crc);
  6680. fallthrough;
  6681. case FASTRPC_IOCTL_INVOKE_PERF:
  6682. if (!size)
  6683. size = sizeof(struct fastrpc_ioctl_invoke_perf);
  6684. trace_fastrpc_msg("invoke: begin");
  6685. K_COPY_FROM_USER(err, 0, &p.inv, param, size);
  6686. if (err) {
  6687. err = -EFAULT;
  6688. goto bail;
  6689. }
  6690. VERIFY(err, 0 == (err = fastrpc_internal_invoke(fl, fl->mode,
  6691. USER_MSG, &p.inv)));
  6692. trace_fastrpc_msg("invoke: end");
  6693. if (err)
  6694. goto bail;
  6695. break;
  6696. case FASTRPC_IOCTL_INVOKE2:
  6697. K_COPY_FROM_USER(err, 0, &p.inv2, param,
  6698. sizeof(struct fastrpc_ioctl_invoke2));
  6699. if (err) {
  6700. err = -EFAULT;
  6701. goto bail;
  6702. }
  6703. VERIFY(err, 0 == (err = fastrpc_internal_invoke2(fl, &p.inv2)));
  6704. if (err)
  6705. goto bail;
  6706. break;
  6707. case FASTRPC_IOCTL_SETMODE:
  6708. err = fastrpc_setmode(ioctl_param, fl);
  6709. break;
  6710. case FASTRPC_IOCTL_CONTROL:
  6711. err = fastrpc_control(&p.cp, param, fl);
  6712. break;
  6713. case FASTRPC_IOCTL_GETINFO:
  6714. K_COPY_FROM_USER(err, 0, &info, param, sizeof(info));
  6715. if (err) {
  6716. err = -EFAULT;
  6717. goto bail;
  6718. }
  6719. VERIFY(err, 0 == (err = fastrpc_get_info(fl, &info)));
  6720. if (err)
  6721. goto bail;
  6722. K_COPY_TO_USER(err, 0, param, &info, sizeof(info));
  6723. if (err) {
  6724. err = -EFAULT;
  6725. goto bail;
  6726. }
  6727. break;
  6728. case FASTRPC_IOCTL_INIT:
  6729. p.init.attrs = 0;
  6730. p.init.siglen = 0;
  6731. size = sizeof(struct fastrpc_ioctl_init);
  6732. fallthrough;
  6733. case FASTRPC_IOCTL_INIT_ATTRS:
  6734. if (!size)
  6735. size = sizeof(struct fastrpc_ioctl_init_attrs);
  6736. K_COPY_FROM_USER(err, 0, &p.init, param, size);
  6737. if (err) {
  6738. err = -EFAULT;
  6739. goto bail;
  6740. }
  6741. VERIFY(err, 0 == (err = fastrpc_init_process(fl, &p.init)));
  6742. if (err)
  6743. goto bail;
  6744. break;
  6745. case FASTRPC_IOCTL_GET_DSP_INFO:
  6746. err = fastrpc_get_dsp_info(&p.cap, param, fl);
  6747. break;
  6748. case FASTRPC_IOCTL_MEM_MAP:
  6749. fallthrough;
  6750. case FASTRPC_IOCTL_MEM_UNMAP:
  6751. fallthrough;
  6752. case FASTRPC_IOCTL_MMAP:
  6753. fallthrough;
  6754. case FASTRPC_IOCTL_MUNMAP:
  6755. fallthrough;
  6756. case FASTRPC_IOCTL_MMAP_64:
  6757. fallthrough;
  6758. case FASTRPC_IOCTL_MUNMAP_64:
  6759. fallthrough;
  6760. case FASTRPC_IOCTL_MUNMAP_FD:
  6761. err = fastrpc_mmap_device_ioctl(fl, ioctl_num, &p, param);
  6762. break;
  6763. case FASTRPC_IOCTL_DSPSIGNAL_SIGNAL:
  6764. K_COPY_FROM_USER(err, 0, &p.sig, param,
  6765. sizeof(struct fastrpc_ioctl_dspsignal_signal));
  6766. if (err) {
  6767. err = -EFAULT;
  6768. goto bail;
  6769. }
  6770. VERIFY(err, 0 == (err = fastrpc_dspsignal_signal(fl, &p.sig)));
  6771. if (err)
  6772. goto bail;
  6773. break;
  6774. case FASTRPC_IOCTL_DSPSIGNAL_WAIT:
  6775. K_COPY_FROM_USER(err, 0, &p.wait, param,
  6776. sizeof(struct fastrpc_ioctl_dspsignal_wait));
  6777. if (err) {
  6778. err = -EFAULT;
  6779. goto bail;
  6780. }
  6781. VERIFY(err, 0 == (err = fastrpc_dspsignal_wait(fl, &p.wait)));
  6782. if (err)
  6783. goto bail;
  6784. break;
  6785. case FASTRPC_IOCTL_DSPSIGNAL_CREATE:
  6786. K_COPY_FROM_USER(err, 0, &p.cre, param,
  6787. sizeof(struct fastrpc_ioctl_dspsignal_create));
  6788. if (err) {
  6789. err = -EFAULT;
  6790. goto bail;
  6791. }
  6792. VERIFY(err, 0 == (err = fastrpc_dspsignal_create(fl, &p.cre)));
  6793. if (err)
  6794. goto bail;
  6795. break;
  6796. case FASTRPC_IOCTL_DSPSIGNAL_DESTROY:
  6797. K_COPY_FROM_USER(err, 0, &p.des, param,
  6798. sizeof(struct fastrpc_ioctl_dspsignal_destroy));
  6799. if (err) {
  6800. err = -EFAULT;
  6801. goto bail;
  6802. }
  6803. VERIFY(err, 0 == (err = fastrpc_dspsignal_destroy(fl, &p.des)));
  6804. if (err)
  6805. goto bail;
  6806. break;
  6807. case FASTRPC_IOCTL_DSPSIGNAL_CANCEL_WAIT:
  6808. K_COPY_FROM_USER(err, 0, &p.canc, param,
  6809. sizeof(struct fastrpc_ioctl_dspsignal_cancel_wait));
  6810. if (err) {
  6811. err = -EFAULT;
  6812. goto bail;
  6813. }
  6814. VERIFY(err, 0 == (err = fastrpc_dspsignal_cancel_wait(fl, &p.canc)));
  6815. if (err)
  6816. goto bail;
  6817. break;
  6818. default:
  6819. err = -ENOTTY;
  6820. pr_info("bad ioctl: %d\n", ioctl_num);
  6821. break;
  6822. }
  6823. bail:
  6824. return err;
  6825. }
  6826. /*
  6827. * fastrpc_smq_ctx_detail : Store smq_invoke_ctx structure parameter.
  6828. * Input :
  6829. * structure smq_invoke_ctx
  6830. * void* mini_dump_buff
  6831. */
  6832. static void fastrpc_smq_ctx_detail(struct smq_invoke_ctx *smq_ctx, int cid, void *mini_dump_buff)
  6833. {
  6834. int i = 0;
  6835. remote_arg64_t *rpra = NULL;
  6836. struct fastrpc_mmap *map = NULL;
  6837. if (!smq_ctx)
  6838. return;
  6839. if (smq_ctx->buf && smq_ctx->buf->virt)
  6840. rpra = smq_ctx->buf->virt;
  6841. for (i = 0; rpra &&
  6842. i < (REMOTE_SCALARS_INBUFS(smq_ctx->sc) + REMOTE_SCALARS_OUTBUFS(smq_ctx->sc));
  6843. ++i) {
  6844. map = smq_ctx->maps[i];
  6845. if (map) {
  6846. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  6847. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6848. smq_invoke_ctx_params,
  6849. smq_ctx->pid, smq_ctx->tgid, smq_ctx->handle,
  6850. smq_ctx->sc, smq_ctx->fl, smq_ctx->fds,
  6851. smq_ctx->magic);
  6852. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  6853. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6854. fastrpc_mmap_params,
  6855. map->fd, map->flags, map->buf,
  6856. map->phys, map->size, map->va,
  6857. map->raddr, map->len, map->refs,
  6858. map->secure);
  6859. } else {
  6860. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  6861. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6862. smq_invoke_ctx_params, smq_ctx->pid, smq_ctx->tgid,
  6863. smq_ctx->handle, smq_ctx->sc, smq_ctx->fl, smq_ctx->fds,
  6864. smq_ctx->magic);
  6865. }
  6866. break;
  6867. }
  6868. }
  6869. /*
  6870. * fastrpc_print_fastrpcbuf : Print fastrpc_buf structure parameter.
  6871. * Input :
  6872. * structure fastrpc_buf
  6873. * void* buffer
  6874. */
  6875. static void fastrpc_print_fastrpcbuf(struct fastrpc_buf *buf, void *buffer)
  6876. {
  6877. if (!buf || !buffer)
  6878. return;
  6879. scnprintf(buffer + strlen(buffer),
  6880. MINI_DUMP_DBG_SIZE - strlen(buffer),
  6881. fastrpc_buf_params, buf->fl, buf->phys,
  6882. buf->virt, buf->size, buf->dma_attr, buf->raddr,
  6883. buf->flags, buf->type, buf->in_use);
  6884. }
  6885. /*
  6886. * fastrpc_print_debug_data : Print debug structure variable in CMA memory.
  6887. * Input cid: Channel id
  6888. */
  6889. static void fastrpc_print_debug_data(int cid)
  6890. {
  6891. unsigned int i = 0, count = 0, gmsg_log_iter = 3, err = 0, len = 0;
  6892. unsigned int tx_index = 0, rx_index = 0;
  6893. unsigned long flags = 0;
  6894. char *gmsg_log_tx = NULL;
  6895. char *gmsg_log_rx = NULL;
  6896. void *mini_dump_buff = NULL;
  6897. struct fastrpc_apps *me = &gfa;
  6898. struct smq_invoke_rspv2 *rsp = NULL;
  6899. struct fastrpc_file *fl = NULL;
  6900. struct fastrpc_channel_ctx *chan = NULL;
  6901. struct hlist_node *n = NULL;
  6902. struct smq_invoke_ctx *ictx = NULL;
  6903. struct fastrpc_tx_msg *tx_msg = NULL;
  6904. struct fastrpc_buf *buf = NULL;
  6905. struct fastrpc_mmap *map = NULL;
  6906. unsigned long irq_flags = 0;
  6907. VERIFY(err, NULL != (gmsg_log_tx = kzalloc(MD_GMSG_BUFFER, GFP_KERNEL)));
  6908. if (err) {
  6909. err = -ENOMEM;
  6910. return;
  6911. }
  6912. VERIFY(err, NULL != (gmsg_log_rx = kzalloc(MD_GMSG_BUFFER, GFP_KERNEL)));
  6913. if (err) {
  6914. err = -ENOMEM;
  6915. return;
  6916. }
  6917. chan = &me->channel[cid];
  6918. if ((!chan) || (!chan->buf))
  6919. return;
  6920. mini_dump_buff = chan->buf->virt;
  6921. if (!mini_dump_buff)
  6922. return;
  6923. if (chan) {
  6924. tx_index = chan->gmsg_log.tx_index;
  6925. rx_index = chan->gmsg_log.rx_index;
  6926. }
  6927. spin_lock_irqsave(&me->hlock, irq_flags);
  6928. hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
  6929. if (fl->cid == cid) {
  6930. scnprintf(mini_dump_buff +
  6931. strlen(mini_dump_buff),
  6932. MINI_DUMP_DBG_SIZE -
  6933. strlen(mini_dump_buff),
  6934. "\nfastrpc_file : %p\n", fl);
  6935. scnprintf(mini_dump_buff +
  6936. strlen(mini_dump_buff),
  6937. MINI_DUMP_DBG_SIZE -
  6938. strlen(mini_dump_buff),
  6939. fastrpc_file_params, fl->tgid,
  6940. fl->cid, fl->ssrcount, fl->pd,
  6941. fl->profile, fl->mode,
  6942. fl->tgid_open, fl->num_cached_buf,
  6943. fl->num_pers_hdrs, fl->sessionid,
  6944. fl->servloc_name, fl->file_close,
  6945. fl->dsp_proc_init, fl->apps,
  6946. fl->qos_request, fl->dev_minor,
  6947. fl->debug_buf,
  6948. fl->debug_buf_alloced_attempted,
  6949. fl->wake_enable,
  6950. fl->ws_timeout,
  6951. fl->untrusted_process);
  6952. scnprintf(mini_dump_buff +
  6953. strlen(mini_dump_buff),
  6954. MINI_DUMP_DBG_SIZE -
  6955. strlen(mini_dump_buff),
  6956. "\nSession Maps\n");
  6957. hlist_for_each_entry_safe(map, n, &fl->maps, hn) {
  6958. scnprintf(mini_dump_buff +
  6959. strlen(mini_dump_buff),
  6960. MINI_DUMP_DBG_SIZE -
  6961. strlen(mini_dump_buff),
  6962. fastrpc_mmap_params,
  6963. map->fd,
  6964. map->flags, map->buf,
  6965. map->phys, map->size,
  6966. map->va, map->raddr,
  6967. map->len, map->refs,
  6968. map->secure);
  6969. }
  6970. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  6971. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6972. "\ncached_bufs\n");
  6973. hlist_for_each_entry_safe(buf, n, &fl->cached_bufs, hn) {
  6974. fastrpc_print_fastrpcbuf(buf, mini_dump_buff);
  6975. }
  6976. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  6977. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6978. "\ninit_mem: %p\n", fl->init_mem);
  6979. fastrpc_print_fastrpcbuf(fl->init_mem, mini_dump_buff);
  6980. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  6981. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6982. "\npers_hdr_buf: %p\n", fl->pers_hdr_buf);
  6983. fastrpc_print_fastrpcbuf(fl->pers_hdr_buf, mini_dump_buff);
  6984. snprintf(mini_dump_buff + strlen(mini_dump_buff),
  6985. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6986. "\nhdr_bufs: %p\n", fl->hdr_bufs);
  6987. fastrpc_print_fastrpcbuf(fl->hdr_bufs, mini_dump_buff);
  6988. if (fl->debugfs_file) {
  6989. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  6990. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6991. "\nfl->debugfs_file.d_iname : %s\n",
  6992. fl->debugfs_file->d_iname);
  6993. }
  6994. if (fl->sctx) {
  6995. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  6996. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  6997. "\nfl->sctx->smmu.cb : %d\n",
  6998. fl->sctx->smmu.cb);
  6999. }
  7000. if (fl->secsctx) {
  7001. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  7002. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  7003. "\nfl->secsctx->smmu.cb : %d\n",
  7004. fl->secsctx->smmu.cb);
  7005. }
  7006. spin_lock(&fl->hlock);
  7007. scnprintf(mini_dump_buff +
  7008. strlen(mini_dump_buff),
  7009. MINI_DUMP_DBG_SIZE -
  7010. strlen(mini_dump_buff),
  7011. "\nPending Ctx:\n");
  7012. hlist_for_each_entry_safe(ictx, n, &fl->clst.pending, hn) {
  7013. fastrpc_smq_ctx_detail(ictx,
  7014. cid, mini_dump_buff);
  7015. }
  7016. scnprintf(mini_dump_buff +
  7017. strlen(mini_dump_buff),
  7018. MINI_DUMP_DBG_SIZE -
  7019. strlen(mini_dump_buff),
  7020. "\nInterrupted Ctx:\n");
  7021. hlist_for_each_entry_safe(ictx, n,
  7022. &fl->clst.interrupted,
  7023. hn) {
  7024. fastrpc_smq_ctx_detail(ictx,
  7025. cid, mini_dump_buff);
  7026. }
  7027. spin_unlock(&fl->hlock);
  7028. }
  7029. }
  7030. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7031. spin_lock_irqsave(&chan->gmsg_log.lock, flags);
  7032. if (rx_index) {
  7033. for (i = rx_index, count = 0, len = 0 ; i > 0 &&
  7034. count <= gmsg_log_iter; i--, count++) {
  7035. rsp = &chan->gmsg_log.rx_msgs[i].rsp;
  7036. len += scnprintf(gmsg_log_rx + len, MD_GMSG_BUFFER - len,
  7037. "ctx: 0x%x, retval: %d, flags: %d, early_wake_time: %d, version: %d\n",
  7038. rsp->ctx, rsp->retval, rsp->flags,
  7039. rsp->early_wake_time, rsp->version);
  7040. }
  7041. }
  7042. if (tx_index) {
  7043. for (i = tx_index, count = 0, len = 0;
  7044. i > 0 && count <= gmsg_log_iter;
  7045. i--, count++) {
  7046. tx_msg = &chan->gmsg_log.tx_msgs[i];
  7047. len += scnprintf(gmsg_log_tx + len, MD_GMSG_BUFFER - len,
  7048. "pid: %d, tid: %d, ctx: 0x%x, handle: 0x%x, sc: 0x%x, addr: 0x%x, size:%d\n",
  7049. tx_msg->msg.pid,
  7050. tx_msg->msg.tid,
  7051. tx_msg->msg.invoke.header.ctx,
  7052. tx_msg->msg.invoke.header.handle,
  7053. tx_msg->msg.invoke.header.sc,
  7054. tx_msg->msg.invoke.page.addr,
  7055. tx_msg->msg.invoke.page.size);
  7056. }
  7057. }
  7058. spin_unlock_irqrestore(&chan->gmsg_log.lock, flags);
  7059. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  7060. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  7061. "gmsg_log_tx:\n%s\n", gmsg_log_tx);
  7062. scnprintf(mini_dump_buff + strlen(mini_dump_buff),
  7063. MINI_DUMP_DBG_SIZE - strlen(mini_dump_buff),
  7064. "gmsg_log_rx:\n %s\n", gmsg_log_rx);
  7065. if (chan && chan->buf)
  7066. chan->buf->size = strlen(mini_dump_buff);
  7067. kfree(gmsg_log_tx);
  7068. kfree(gmsg_log_rx);
  7069. }
  7070. void fastrpc_restart_drivers(int cid)
  7071. {
  7072. struct fastrpc_apps *me = &gfa;
  7073. fastrpc_notify_drivers(me, cid);
  7074. me->channel[cid].ssrcount++;
  7075. }
  7076. static int fastrpc_restart_notifier_cb(struct notifier_block *nb,
  7077. unsigned long code,
  7078. void *data)
  7079. {
  7080. struct fastrpc_apps *me = &gfa;
  7081. struct fastrpc_channel_ctx *ctx;
  7082. struct fastrpc_file *fl;
  7083. struct hlist_node *n;
  7084. int cid = -1;
  7085. struct timespec64 startT = {0};
  7086. unsigned long irq_flags = 0;
  7087. ctx = container_of(nb, struct fastrpc_channel_ctx, nb);
  7088. cid = ctx - &me->channel[0];
  7089. switch (code) {
  7090. case QCOM_SSR_BEFORE_SHUTDOWN:
  7091. fastrpc_rproc_trace_events(gcinfo[cid].subsys,
  7092. "QCOM_SSR_BEFORE_SHUTDOWN", "fastrpc_restart_notifier-enter");
  7093. pr_info("adsprpc: %s: %s subsystem is restarting\n",
  7094. __func__, gcinfo[cid].subsys);
  7095. mutex_lock(&me->channel[cid].smd_mutex);
  7096. ctx->ssrcount++;
  7097. ctx->subsystemstate = SUBSYSTEM_RESTARTING;
  7098. mutex_unlock(&me->channel[cid].smd_mutex);
  7099. if (cid == RH_CID)
  7100. me->staticpd_flags = 0;
  7101. break;
  7102. case QCOM_SSR_AFTER_SHUTDOWN:
  7103. fastrpc_rproc_trace_events(gcinfo[cid].subsys,
  7104. "QCOM_SSR_AFTER_SHUTDOWN", "fastrpc_restart_notifier-enter");
  7105. spin_lock_irqsave(&me->hlock, irq_flags);
  7106. hlist_for_each_entry_safe(fl, n, &me->drivers, hn) {
  7107. if (fl->cid != cid)
  7108. continue;
  7109. complete(&fl->shutdown);
  7110. }
  7111. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7112. ctx->subsystemstate = SUBSYSTEM_DOWN;
  7113. pr_info("adsprpc: %s: received RAMDUMP notification for %s\n",
  7114. __func__, gcinfo[cid].subsys);
  7115. break;
  7116. case QCOM_SSR_BEFORE_POWERUP:
  7117. fastrpc_rproc_trace_events(gcinfo[cid].subsys,
  7118. "QCOM_SSR_BEFORE_POWERUP", "fastrpc_restart_notifier-enter");
  7119. pr_info("adsprpc: %s: subsystem %s is about to start\n",
  7120. __func__, gcinfo[cid].subsys);
  7121. if (cid == CDSP_DOMAIN_ID && dump_enabled() &&
  7122. ctx->ssrcount)
  7123. fastrpc_update_ramdump_status(cid);
  7124. fastrpc_notify_drivers(me, cid);
  7125. /* Skip ram dump collection in first boot */
  7126. if (cid == CDSP_DOMAIN_ID && dump_enabled() &&
  7127. ctx->ssrcount) {
  7128. mutex_lock(&me->channel[cid].smd_mutex);
  7129. fastrpc_print_debug_data(cid);
  7130. mutex_unlock(&me->channel[cid].smd_mutex);
  7131. ktime_get_real_ts64(&startT);
  7132. fastrpc_ramdump_collection(cid);
  7133. pr_info("adsprpc: %s: fastrpc ramdump finished in %lu (us)\n",
  7134. __func__, getnstimediff(&startT));
  7135. }
  7136. break;
  7137. case QCOM_SSR_AFTER_POWERUP:
  7138. fastrpc_rproc_trace_events(gcinfo[cid].subsys,
  7139. "QCOM_SSR_AFTER_POWERUP", "fastrpc_restart_notifier-enter");
  7140. pr_info("adsprpc: %s: %s subsystem is up\n",
  7141. __func__, gcinfo[cid].subsys);
  7142. ctx->subsystemstate = SUBSYSTEM_UP;
  7143. break;
  7144. default:
  7145. break;
  7146. }
  7147. fastrpc_rproc_trace_events(dev_name(me->dev), "fastrpc_restart_notifier", "exit");
  7148. return NOTIFY_DONE;
  7149. }
  7150. static void fastrpc_pdr_cb(int state, char *service_path, void *priv)
  7151. {
  7152. struct fastrpc_apps *me = &gfa;
  7153. struct fastrpc_static_pd *spd;
  7154. int err = 0;
  7155. spd = priv;
  7156. VERIFY(err, spd);
  7157. if (err)
  7158. goto bail;
  7159. switch (state) {
  7160. case SERVREG_SERVICE_STATE_DOWN:
  7161. pr_info("adsprpc: %s: %s (%s) is down for PDR on %s\n",
  7162. __func__, spd->spdname,
  7163. spd->servloc_name,
  7164. gcinfo[spd->cid].subsys);
  7165. mutex_lock(&me->channel[spd->cid].smd_mutex);
  7166. spd->pdrcount++;
  7167. atomic_set(&spd->ispdup, 0);
  7168. mutex_unlock(&me->channel[spd->cid].smd_mutex);
  7169. if (!strcmp(spd->servloc_name,
  7170. AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME))
  7171. me->staticpd_flags = 0;
  7172. fastrpc_notify_pdr_drivers(me, spd->servloc_name);
  7173. break;
  7174. case SERVREG_SERVICE_STATE_UP:
  7175. pr_info("adsprpc: %s: %s (%s) is up for PDR on %s\n",
  7176. __func__, spd->spdname,
  7177. spd->servloc_name,
  7178. gcinfo[spd->cid].subsys);
  7179. atomic_set(&spd->ispdup, 1);
  7180. wake_up_interruptible(&spd->wait_for_pdup);
  7181. break;
  7182. default:
  7183. break;
  7184. }
  7185. bail:
  7186. if (err) {
  7187. pr_err("adsprpc: %s: failed for path %s, state %d, spd %pK\n",
  7188. __func__, service_path, state, spd);
  7189. }
  7190. }
  7191. static const struct file_operations fops = {
  7192. .open = fastrpc_device_open,
  7193. .release = fastrpc_device_release,
  7194. .unlocked_ioctl = fastrpc_device_ioctl,
  7195. /* Only DSP service 64-bit app will interface with fastrpc TVM driver.
  7196. * There is not need to support 32-bit fastrpc driver on TVM.
  7197. */
  7198. #if IS_ENABLED(CONFIG_MSM_ADSPRPC_TRUSTED)
  7199. .compat_ioctl = NULL,
  7200. #else
  7201. .compat_ioctl = compat_fastrpc_device_ioctl,
  7202. #endif
  7203. };
  7204. static const struct of_device_id fastrpc_match_table[] = {
  7205. { .compatible = "qcom,msm-fastrpc-adsp", },
  7206. { .compatible = "qcom,msm-fastrpc-compute", },
  7207. { .compatible = "qcom,msm-fastrpc-compute-cb", },
  7208. { .compatible = "qcom,msm-adsprpc-mem-region", },
  7209. {}
  7210. };
  7211. static int fastrpc_cb_probe(struct device *dev)
  7212. {
  7213. struct fastrpc_channel_ctx *chan = NULL;
  7214. struct fastrpc_session_ctx *sess = NULL;
  7215. struct of_phandle_args iommuspec;
  7216. struct fastrpc_apps *me = &gfa;
  7217. struct fastrpc_buf *buf = NULL;
  7218. struct gen_pool *gen_pool = NULL;
  7219. struct iommu_domain *domain = NULL;
  7220. const char *name;
  7221. int err = 0, cid = -1, i = 0;
  7222. u32 sharedcb_count = 0, j = 0;
  7223. uint32_t dma_addr_pool[2] = {0, 0};
  7224. VERIFY(err, NULL != (name = of_get_property(dev->of_node,
  7225. "label", NULL)));
  7226. if (err) {
  7227. err = -EINVAL;
  7228. goto bail;
  7229. }
  7230. for (i = 0; i < NUM_CHANNELS; i++) {
  7231. if (!gcinfo[i].name)
  7232. continue;
  7233. if (!strcmp(name, gcinfo[i].name))
  7234. break;
  7235. }
  7236. VERIFY(err, i < NUM_CHANNELS);
  7237. if (err) {
  7238. err = -ECHRNG;
  7239. goto bail;
  7240. }
  7241. cid = i;
  7242. chan = &gcinfo[i];
  7243. VERIFY(err, chan->sesscount < NUM_SESSIONS);
  7244. if (err) {
  7245. err = -EINVAL;
  7246. goto bail;
  7247. }
  7248. err = of_parse_phandle_with_args(dev->of_node, "iommus",
  7249. "#iommu-cells", 0, &iommuspec);
  7250. if (err) {
  7251. pr_err("Error: adsprpc: %s: parsing iommu arguments failed for %s with err %d\n",
  7252. __func__, dev_name(dev), err);
  7253. goto bail;
  7254. }
  7255. sess = &chan->session[chan->sesscount];
  7256. sess->used = 0;
  7257. sess->smmu.coherent = of_property_read_bool(dev->of_node,
  7258. "dma-coherent");
  7259. sess->smmu.secure = of_property_read_bool(dev->of_node,
  7260. "qcom,secure-context-bank");
  7261. sess->smmu.cb = iommuspec.args[0] & 0xf;
  7262. sess->smmu.dev = dev;
  7263. sess->smmu.dev_name = dev_name(dev);
  7264. sess->smmu.enabled = 1;
  7265. if (!sess->smmu.dev->dma_parms)
  7266. sess->smmu.dev->dma_parms = devm_kzalloc(sess->smmu.dev,
  7267. sizeof(*sess->smmu.dev->dma_parms), GFP_KERNEL);
  7268. dma_set_max_seg_size(sess->smmu.dev, DMA_BIT_MASK(32));
  7269. dma_set_seg_boundary(sess->smmu.dev, (unsigned long)DMA_BIT_MASK(64));
  7270. of_property_read_u32_array(dev->of_node, "qcom,iommu-dma-addr-pool",
  7271. dma_addr_pool, 2);
  7272. me->max_size_limit = (dma_addr_pool[1] == 0 ? 0x78000000 :
  7273. dma_addr_pool[1]);
  7274. if (of_get_property(dev->of_node, "pd-type", NULL) != NULL) {
  7275. err = of_property_read_u32(dev->of_node, "pd-type",
  7276. &(sess->smmu.pd_type));
  7277. /* Set cb_pd_type, if the process type is set for context banks */
  7278. me->cb_pd_type = true;
  7279. if (err)
  7280. goto bail;
  7281. }
  7282. if (of_get_property(dev->of_node, "shared-cb", NULL) != NULL) {
  7283. sess->smmu.sharedcb = 1;
  7284. // Set share_securecb, if the secure context bank is shared
  7285. if (sess->smmu.secure)
  7286. me->share_securecb = 1;
  7287. err = of_property_read_u32(dev->of_node, "shared-cb",
  7288. &sharedcb_count);
  7289. if (err)
  7290. goto bail;
  7291. if (sharedcb_count > 0) {
  7292. struct fastrpc_session_ctx *dup_sess;
  7293. for (j = 1; j < sharedcb_count &&
  7294. chan->sesscount < NUM_SESSIONS; j++) {
  7295. chan->sesscount++;
  7296. dup_sess = &chan->session[chan->sesscount];
  7297. memcpy(dup_sess, sess,
  7298. sizeof(struct fastrpc_session_ctx));
  7299. }
  7300. }
  7301. }
  7302. if (of_get_property(dev->of_node, "qrtr-gen-pool", NULL) != NULL) {
  7303. u32 frpc_gen_addr_pool[2] = {0, 0};
  7304. struct sg_table sgt;
  7305. err = of_property_read_u32_array(dev->of_node, "frpc-gen-addr-pool",
  7306. frpc_gen_addr_pool, 2);
  7307. if (err) {
  7308. pr_err("Error: adsprpc: %s: parsing frpc-gen-addr-pool arguments failed for %s with err %d\n",
  7309. __func__, dev_name(dev), err);
  7310. goto bail;
  7311. }
  7312. sess->smmu.genpool_iova = frpc_gen_addr_pool[0];
  7313. sess->smmu.genpool_size = frpc_gen_addr_pool[1];
  7314. VERIFY(err, NULL != (buf = kzalloc(sizeof(*buf), GFP_KERNEL)));
  7315. if (err) {
  7316. err = -ENOMEM;
  7317. ADSPRPC_ERR(
  7318. "allocation failed for size 0x%zx\n", sizeof(*buf));
  7319. goto bail;
  7320. }
  7321. INIT_HLIST_NODE(&buf->hn);
  7322. buf->virt = NULL;
  7323. buf->phys = 0;
  7324. buf->size = frpc_gen_addr_pool[1];
  7325. buf->dma_attr = DMA_ATTR_DELAYED_UNMAP;
  7326. /* Allocate memory for adding to genpool */
  7327. buf->virt = dma_alloc_attrs(sess->smmu.dev, buf->size,
  7328. (dma_addr_t *)&buf->phys,
  7329. GFP_KERNEL, buf->dma_attr);
  7330. if (IS_ERR_OR_NULL(buf->virt)) {
  7331. ADSPRPC_ERR(
  7332. "dma_alloc_attrs failed for size 0x%zx, returned %pK\n",
  7333. buf->size, buf->virt);
  7334. err = -ENOBUFS;
  7335. goto dma_alloc_bail;
  7336. }
  7337. err = dma_get_sgtable_attrs(sess->smmu.dev, &sgt, buf->virt,
  7338. buf->phys, buf->size, buf->dma_attr);
  7339. if (err) {
  7340. ADSPRPC_ERR("dma_get_sgtable_attrs failed with err %d", err);
  7341. goto iommu_map_bail;
  7342. }
  7343. domain = iommu_get_domain_for_dev(sess->smmu.dev);
  7344. if (!domain) {
  7345. ADSPRPC_ERR("iommu_get_domain_for_dev failed ");
  7346. goto iommu_map_bail;
  7347. }
  7348. /* Map the allocated memory with fixed IOVA and is shared to remote subsystem */
  7349. err = iommu_map_sg(domain, frpc_gen_addr_pool[0], sgt.sgl,
  7350. sgt.nents, IOMMU_READ | IOMMU_WRITE | IOMMU_CACHE);
  7351. if (err < 0) {
  7352. ADSPRPC_ERR("iommu_map_sg failed with err %d", err);
  7353. goto iommu_map_bail;
  7354. }
  7355. /* Create genpool using SMMU device */
  7356. gen_pool = devm_gen_pool_create(sess->smmu.dev, 0,
  7357. NUMA_NO_NODE, NULL);
  7358. if (IS_ERR(gen_pool)) {
  7359. err = PTR_ERR(gen_pool);
  7360. ADSPRPC_ERR("devm_gen_pool_create failed with err %d", err);
  7361. goto genpool_create_bail;
  7362. }
  7363. /* Add allocated memory to genpool */
  7364. err = gen_pool_add_virt(gen_pool, (unsigned long)buf->virt,
  7365. buf->phys, buf->size, NUMA_NO_NODE);
  7366. if (err) {
  7367. ADSPRPC_ERR("gen_pool_add_virt failed with err %d", err);
  7368. goto genpool_add_bail;
  7369. }
  7370. sess->smmu.frpc_genpool = gen_pool;
  7371. sess->smmu.frpc_genpool_buf = buf;
  7372. }
  7373. chan->sesscount++;
  7374. if (debugfs_root && !debugfs_global_file) {
  7375. debugfs_global_file = debugfs_create_file("global", 0644,
  7376. debugfs_root, NULL, &debugfs_fops);
  7377. if (IS_ERR_OR_NULL(debugfs_global_file)) {
  7378. pr_warn("Error: %s: %s: failed to create debugfs global file\n",
  7379. current->comm, __func__);
  7380. debugfs_global_file = NULL;
  7381. }
  7382. }
  7383. bail:
  7384. return err;
  7385. genpool_add_bail:
  7386. gen_pool_destroy(gen_pool);
  7387. genpool_create_bail:
  7388. iommu_unmap(domain, sess->smmu.genpool_iova,
  7389. sess->smmu.genpool_size);
  7390. iommu_map_bail:
  7391. dma_free_attrs(sess->smmu.dev, buf->size, buf->virt,
  7392. buf->phys, buf->dma_attr);
  7393. dma_alloc_bail:
  7394. kfree(buf);
  7395. return err;
  7396. }
  7397. static void init_secure_vmid_list(struct device *dev, char *prop_name,
  7398. struct secure_vm *destvm)
  7399. {
  7400. int err = 0;
  7401. u32 len = 0, i = 0;
  7402. u32 *rhvmlist = NULL;
  7403. u32 *rhvmpermlist = NULL;
  7404. if (!of_find_property(dev->of_node, prop_name, &len))
  7405. goto bail;
  7406. if (len == 0)
  7407. goto bail;
  7408. len /= sizeof(u32);
  7409. VERIFY(err, NULL != (rhvmlist = kcalloc(len, sizeof(u32), GFP_KERNEL)));
  7410. if (err)
  7411. goto bail;
  7412. VERIFY(err, NULL != (rhvmpermlist = kcalloc(len, sizeof(u32),
  7413. GFP_KERNEL)));
  7414. if (err)
  7415. goto bail;
  7416. for (i = 0; i < len; i++) {
  7417. err = of_property_read_u32_index(dev->of_node, prop_name, i,
  7418. &rhvmlist[i]);
  7419. if (err) {
  7420. pr_err("Error: adsprpc: %s: failed to read VMID\n",
  7421. __func__);
  7422. goto bail;
  7423. }
  7424. ADSPRPC_INFO("secure VMID = %d\n",
  7425. rhvmlist[i]);
  7426. rhvmpermlist[i] = QCOM_SCM_PERM_RWX;
  7427. }
  7428. destvm->vmid = rhvmlist;
  7429. destvm->vmperm = rhvmpermlist;
  7430. destvm->vmcount = len;
  7431. bail:
  7432. if (err) {
  7433. kfree(rhvmlist);
  7434. kfree(rhvmpermlist);
  7435. }
  7436. }
  7437. static void fastrpc_init_privileged_gids(struct device *dev, char *prop_name,
  7438. struct gid_list *gidlist)
  7439. {
  7440. int err = 0;
  7441. u32 len = 0, i = 0;
  7442. u32 *gids = NULL;
  7443. if (!of_find_property(dev->of_node, prop_name, &len))
  7444. goto bail;
  7445. if (len == 0)
  7446. goto bail;
  7447. len /= sizeof(u32);
  7448. gids = kcalloc(len, sizeof(u32), GFP_KERNEL);
  7449. if (!gids) {
  7450. err = ENOMEM;
  7451. goto bail;
  7452. }
  7453. for (i = 0; i < len; i++) {
  7454. err = of_property_read_u32_index(dev->of_node, prop_name,
  7455. i, &gids[i]);
  7456. if (err) {
  7457. pr_err("Error: adsprpc: %s: failed to read GID %u\n",
  7458. __func__, i);
  7459. goto bail;
  7460. }
  7461. pr_info("adsprpc: %s: privileged GID: %u\n", __func__, gids[i]);
  7462. }
  7463. sort(gids, len, sizeof(*gids), uint_cmp_func, NULL);
  7464. gidlist->gids = gids;
  7465. gidlist->gidcount = len;
  7466. bail:
  7467. if (err)
  7468. kfree(gids);
  7469. }
  7470. static void configure_secure_channels(uint32_t secure_domains)
  7471. {
  7472. struct fastrpc_apps *me = &gfa;
  7473. int ii = 0;
  7474. /*
  7475. * secure_domains contains the bitmask of the secure channels
  7476. * Bit 0 - ADSP
  7477. * Bit 1 - MDSP
  7478. * Bit 2 - SLPI
  7479. * Bit 3 - CDSP
  7480. */
  7481. for (ii = ADSP_DOMAIN_ID; ii <= CDSP_DOMAIN_ID; ++ii) {
  7482. int secure = (secure_domains >> ii) & 0x01;
  7483. me->channel[ii].secure = secure;
  7484. ADSPRPC_INFO("domain %d configured as secure %d\n", ii, secure);
  7485. }
  7486. }
  7487. /*
  7488. * This function is used to create the service locator required for
  7489. * registering for remote process restart (PDR) notifications if that
  7490. * PDR property has been enabled in the fastrpc node on the DTSI.
  7491. */
  7492. static int fastrpc_setup_service_locator(struct device *dev,
  7493. const char *propname,
  7494. char *client_name, char *service_name,
  7495. char *service_path)
  7496. {
  7497. int err = 0, session = -1, cid = -1;
  7498. struct fastrpc_apps *me = &gfa;
  7499. struct pdr_handle *handle = NULL;
  7500. struct pdr_service *service = NULL;
  7501. if (of_property_read_bool(dev->of_node, propname)) {
  7502. err = fastrpc_get_spd_session(client_name, &session, &cid);
  7503. if (err)
  7504. goto bail;
  7505. /* Register the service locator's callback function */
  7506. handle = pdr_handle_alloc(fastrpc_pdr_cb, &me->channel[cid].spd[session]);
  7507. if (IS_ERR_OR_NULL(handle)) {
  7508. err = PTR_ERR(handle);
  7509. goto bail;
  7510. }
  7511. me->channel[cid].spd[session].pdrhandle = handle;
  7512. service = pdr_add_lookup(handle, service_name, service_path);
  7513. if (IS_ERR_OR_NULL(service)) {
  7514. err = PTR_ERR(service);
  7515. goto bail;
  7516. }
  7517. pr_info("adsprpc: %s: pdr_add_lookup enabled for %s (%s, %s), DTSI (%s)\n",
  7518. __func__, service_name, client_name, service_path, propname);
  7519. }
  7520. bail:
  7521. if (err) {
  7522. pr_warn("adsprpc: %s: failed for %s (%s, %s), DTSI (%s) with err %d\n",
  7523. __func__, service_name, client_name, service_path, propname, err);
  7524. }
  7525. return err;
  7526. }
  7527. /*
  7528. * fastrpc_nsp_status_show() - Updates the buffer with remote nsp status
  7529. * by reading the fastrpc node.
  7530. * @dev : pointer to device node.
  7531. * @attr: pointer to device attribute.
  7532. * @buf : Output parameter to be updated with remote nsp status.
  7533. * Return : bytes written to buffer.
  7534. */
  7535. static ssize_t fastrpc_nsp_status_show(struct device *dev,
  7536. struct device_attribute *attr, char *buf)
  7537. {
  7538. struct fastrpc_apps *me = &gfa;
  7539. /*
  7540. * Default remote DSP status: 0
  7541. * driver possibly not probed yet or not the main device.
  7542. */
  7543. if (!dev || !dev->driver ||
  7544. !of_device_is_compatible(dev->of_node, "qcom,msm-fastrpc-compute")) {
  7545. ADSPRPC_ERR("Driver not probed yet or not the main device\n");
  7546. return 0;
  7547. }
  7548. return scnprintf(buf, PAGE_SIZE, "%d",
  7549. me->fastrpc_nsp_status);
  7550. }
  7551. /* Remote nsp status attribute declaration as read only */
  7552. static DEVICE_ATTR_RO(fastrpc_nsp_status);
  7553. /* Declaring attribute for remote dsp */
  7554. static struct attribute *msm_remote_dsp_attrs[] = {
  7555. &dev_attr_fastrpc_nsp_status.attr,
  7556. NULL
  7557. };
  7558. /* Defining remote dsp attributes in attributes group */
  7559. static struct attribute_group msm_remote_dsp_attr_group = {
  7560. .attrs = msm_remote_dsp_attrs,
  7561. };
  7562. static int fastrpc_probe(struct platform_device *pdev)
  7563. {
  7564. int err = 0;
  7565. struct fastrpc_apps *me = &gfa;
  7566. struct device *dev = &pdev->dev;
  7567. int ret = 0;
  7568. uint32_t secure_domains = 0;
  7569. if (of_device_is_compatible(dev->of_node,
  7570. "qcom,msm-fastrpc-compute")) {
  7571. err = sysfs_create_group(&pdev->dev.kobj, &msm_remote_dsp_attr_group);
  7572. if (err) {
  7573. ADSPRPC_ERR(
  7574. "Initialization of sysfs create group failed with %d\n",
  7575. err);
  7576. goto bail;
  7577. }
  7578. init_secure_vmid_list(dev, "qcom,adsp-remoteheap-vmid",
  7579. &gcinfo[0].rhvm);
  7580. fastrpc_init_privileged_gids(dev, "qcom,fastrpc-gids",
  7581. &me->gidlist);
  7582. /*
  7583. * Check if latency voting for only one core
  7584. * is enabled for the platform
  7585. */
  7586. me->single_core_latency_vote = of_property_read_bool(dev->of_node,
  7587. "qcom,single-core-latency-vote");
  7588. if (me->single_core_latency_vote)
  7589. me->lowest_capacity_core_count = 1;
  7590. of_property_read_u32(dev->of_node, "qcom,rpc-latency-us",
  7591. &me->latency);
  7592. of_property_read_u32(dev->of_node, "qcom,max-sessions",
  7593. &me->max_sess_per_proc);
  7594. if (of_get_property(dev->of_node,
  7595. "qcom,secure-domains", NULL) != NULL) {
  7596. VERIFY(err, !of_property_read_u32(dev->of_node,
  7597. "qcom,secure-domains",
  7598. &secure_domains));
  7599. if (!err)
  7600. configure_secure_channels(secure_domains);
  7601. else
  7602. pr_info("adsprpc: unable to read the domain configuration from dts\n");
  7603. }
  7604. }
  7605. if (of_device_is_compatible(dev->of_node,
  7606. "qcom,msm-fastrpc-compute-cb"))
  7607. return fastrpc_cb_probe(dev);
  7608. if (of_device_is_compatible(dev->of_node,
  7609. "qcom,msm-adsprpc-mem-region")) {
  7610. me->dev = dev;
  7611. ret = of_reserved_mem_device_init_by_idx(dev, dev->of_node, 0);
  7612. if (ret) {
  7613. pr_warn("adsprpc: Error: %s: initialization of memory region adsp_mem failed with %d\n",
  7614. __func__, ret);
  7615. }
  7616. goto bail;
  7617. }
  7618. me->legacy_remote_heap = of_property_read_bool(dev->of_node,
  7619. "qcom,fastrpc-legacy-remote-heap");
  7620. err = fastrpc_setup_service_locator(dev, AUDIO_PDR_ADSP_DTSI_PROPERTY_NAME,
  7621. AUDIO_PDR_SERVICE_LOCATION_CLIENT_NAME,
  7622. AUDIO_PDR_ADSP_SERVICE_NAME, ADSP_AUDIOPD_NAME);
  7623. if (err)
  7624. goto bail;
  7625. err = fastrpc_setup_service_locator(dev, SENSORS_PDR_ADSP_DTSI_PROPERTY_NAME,
  7626. SENSORS_PDR_ADSP_SERVICE_LOCATION_CLIENT_NAME,
  7627. SENSORS_PDR_ADSP_SERVICE_NAME, ADSP_SENSORPD_NAME);
  7628. if (err)
  7629. goto bail;
  7630. err = fastrpc_setup_service_locator(dev, SENSORS_PDR_SLPI_DTSI_PROPERTY_NAME,
  7631. SENSORS_PDR_SLPI_SERVICE_LOCATION_CLIENT_NAME,
  7632. SENSORS_PDR_SLPI_SERVICE_NAME, SLPI_SENSORPD_NAME);
  7633. if (err)
  7634. goto bail;
  7635. err = of_platform_populate(pdev->dev.of_node,
  7636. fastrpc_match_table,
  7637. NULL, &pdev->dev);
  7638. if (err)
  7639. goto bail;
  7640. bail:
  7641. return err;
  7642. }
  7643. /*
  7644. * Function to free fastrpc genpool buffer
  7645. */
  7646. static void fastrpc_genpool_free(struct fastrpc_session_ctx *sess)
  7647. {
  7648. struct fastrpc_buf *buf = NULL;
  7649. struct iommu_domain *domain = NULL;
  7650. if (!sess)
  7651. goto bail;
  7652. buf = sess->smmu.frpc_genpool_buf;
  7653. if (sess->smmu.frpc_genpool) {
  7654. gen_pool_destroy(sess->smmu.frpc_genpool);
  7655. sess->smmu.frpc_genpool = NULL;
  7656. }
  7657. if (buf && sess->smmu.dev) {
  7658. domain = iommu_get_domain_for_dev(sess->smmu.dev);
  7659. iommu_unmap(domain, sess->smmu.genpool_iova,
  7660. sess->smmu.genpool_size);
  7661. if (buf->phys)
  7662. dma_free_attrs(sess->smmu.dev, buf->size, buf->virt,
  7663. buf->phys, buf->dma_attr);
  7664. kfree(buf);
  7665. sess->smmu.frpc_genpool_buf = NULL;
  7666. }
  7667. bail:
  7668. return;
  7669. }
  7670. static void fastrpc_deinit(void)
  7671. {
  7672. struct fastrpc_channel_ctx *chan = gcinfo;
  7673. struct fastrpc_apps *me = &gfa;
  7674. int i, j;
  7675. for (i = 0; i < NUM_CHANNELS; i++, chan++) {
  7676. for (j = 0; j < NUM_SESSIONS; j++) {
  7677. struct fastrpc_session_ctx *sess = &chan->session[j];
  7678. fastrpc_genpool_free(sess);
  7679. if (sess->smmu.dev)
  7680. sess->smmu.dev = NULL;
  7681. }
  7682. kfree(chan->rhvm.vmid);
  7683. kfree(chan->rhvm.vmperm);
  7684. fastrpc_transport_session_deinit(i);
  7685. mutex_destroy(&chan->smd_mutex);
  7686. }
  7687. if (me->transport_initialized)
  7688. fastrpc_transport_deinit();
  7689. me->transport_initialized = 0;
  7690. mutex_destroy(&me->mut_uid);
  7691. }
  7692. #ifdef CONFIG_HIBERNATION
  7693. static bool hibernation;
  7694. static int fastrpc_hibernation_notifier(struct notifier_block *nb,
  7695. unsigned long event, void *dummy)
  7696. {
  7697. if (event == PM_HIBERNATION_PREPARE)
  7698. hibernation = true;
  7699. else if (event == PM_POST_HIBERNATION)
  7700. hibernation = false;
  7701. return NOTIFY_OK;
  7702. }
  7703. static struct notifier_block fastrpc_notif_block = {
  7704. .notifier_call = fastrpc_hibernation_notifier,
  7705. };
  7706. #endif
  7707. #ifdef CONFIG_PM_SLEEP
  7708. static int fastrpc_hibernation_suspend(struct device *dev)
  7709. {
  7710. int err = 0;
  7711. if (of_device_is_compatible(dev->of_node,
  7712. "qcom,msm-fastrpc-compute")) {
  7713. err = fastrpc_mmap_remove_ssr(NULL, 0);
  7714. if (err)
  7715. ADSPRPC_WARN("failed to unmap remote heap (err %d)\n",
  7716. err);
  7717. }
  7718. return err;
  7719. }
  7720. static int fastrpc_restore(struct device *dev)
  7721. {
  7722. struct fastrpc_apps *me = &gfa;
  7723. int cid;
  7724. pr_info("adsprpc: restore enter\n");
  7725. for (cid = 0; cid < NUM_CHANNELS; cid++)
  7726. me->channel[cid].in_hib = 1;
  7727. pr_info("adsprpc: restore exit\n");
  7728. return 0;
  7729. }
  7730. static const struct dev_pm_ops fastrpc_pm = {
  7731. .freeze = fastrpc_hibernation_suspend,
  7732. .restore = fastrpc_restore,
  7733. };
  7734. #endif
  7735. static struct platform_driver fastrpc_driver = {
  7736. .probe = fastrpc_probe,
  7737. .driver = {
  7738. .name = "fastrpc",
  7739. .of_match_table = fastrpc_match_table,
  7740. .suppress_bind_attrs = true,
  7741. #ifdef CONFIG_PM_SLEEP
  7742. .pm = &fastrpc_pm,
  7743. #endif
  7744. },
  7745. };
  7746. union fastrpc_dev_param {
  7747. struct fastrpc_dev_map_dma *map;
  7748. struct fastrpc_dev_unmap_dma *unmap;
  7749. struct fastrpc_dev_get_hlos_pid *hpid;
  7750. };
  7751. long fastrpc_dev_map_dma(struct fastrpc_device *dev, unsigned long invoke_param)
  7752. {
  7753. int err = 0;
  7754. union fastrpc_dev_param p;
  7755. struct fastrpc_file *fl = NULL;
  7756. struct fastrpc_mmap *map = NULL;
  7757. struct fastrpc_apps *me = &gfa;
  7758. uintptr_t raddr = 0;
  7759. unsigned long irq_flags = 0;
  7760. p.map = (struct fastrpc_dev_map_dma *)invoke_param;
  7761. spin_lock_irqsave(&me->hlock, irq_flags);
  7762. /* Verify if fastrpc device is closed*/
  7763. VERIFY(err, dev && !dev->dev_close);
  7764. if (err) {
  7765. err = -ESRCH;
  7766. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7767. return err;
  7768. }
  7769. fl = dev->fl;
  7770. /* Verify if fastrpc file is not NULL*/
  7771. if (!fl) {
  7772. err = -EBADF;
  7773. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7774. return err;
  7775. }
  7776. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7777. mutex_lock(&fl->internal_map_mutex);
  7778. spin_lock_irqsave(&me->hlock, irq_flags);
  7779. /* Verify if fastrpc file is being closed, holding device lock*/
  7780. if (fl->file_close) {
  7781. err = -ESRCH;
  7782. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7783. goto bail;
  7784. }
  7785. fl->is_dma_invoke_pend = true;
  7786. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7787. mutex_lock(&fl->map_mutex);
  7788. /* Map DMA buffer on SMMU device*/
  7789. err = fastrpc_mmap_create(fl, -1, p.map->buf,
  7790. p.map->attrs, 0, p.map->size,
  7791. ADSP_MMAP_DMA_BUFFER, &map);
  7792. mutex_unlock(&fl->map_mutex);
  7793. if (err)
  7794. goto bail;
  7795. /* Map DMA buffer on DSP*/
  7796. VERIFY(err, 0 == (err = fastrpc_mmap_on_dsp(fl,
  7797. map->flags, 0, map->phys, map->size, map->refs, &raddr)));
  7798. if (err)
  7799. goto bail;
  7800. map->raddr = raddr;
  7801. p.map->v_dsp_addr = raddr;
  7802. bail:
  7803. if (err && map) {
  7804. mutex_lock(&fl->map_mutex);
  7805. fastrpc_mmap_free(map, 0);
  7806. mutex_unlock(&fl->map_mutex);
  7807. }
  7808. if (fl) {
  7809. spin_lock_irqsave(&me->hlock, irq_flags);
  7810. if (fl->file_close && fl->is_dma_invoke_pend)
  7811. complete(&fl->dma_invoke);
  7812. fl->is_dma_invoke_pend = false;
  7813. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7814. }
  7815. mutex_unlock(&fl->internal_map_mutex);
  7816. return err;
  7817. }
  7818. long fastrpc_dev_unmap_dma(struct fastrpc_device *dev, unsigned long invoke_param)
  7819. {
  7820. int err = 0;
  7821. union fastrpc_dev_param p;
  7822. struct fastrpc_file *fl = NULL;
  7823. struct fastrpc_mmap *map = NULL;
  7824. struct fastrpc_apps *me = &gfa;
  7825. unsigned long irq_flags = 0;
  7826. p.unmap = (struct fastrpc_dev_unmap_dma *)invoke_param;
  7827. spin_lock_irqsave(&me->hlock, irq_flags);
  7828. /* Verify if fastrpc device is closed*/
  7829. VERIFY(err, dev && !dev->dev_close);
  7830. if (err) {
  7831. err = -ESRCH;
  7832. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7833. return err;
  7834. }
  7835. fl = dev->fl;
  7836. /* Verify if fastrpc file is not NULL*/
  7837. if (!fl) {
  7838. err = -EBADF;
  7839. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7840. return err;
  7841. }
  7842. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7843. mutex_lock(&fl->internal_map_mutex);
  7844. spin_lock_irqsave(&me->hlock, irq_flags);
  7845. /* Verify if fastrpc file is being closed, holding device lock*/
  7846. if (fl->file_close) {
  7847. err = -ESRCH;
  7848. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7849. goto bail;
  7850. }
  7851. fl->is_dma_invoke_pend = true;
  7852. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7853. mutex_lock(&fl->map_mutex);
  7854. if (!fastrpc_mmap_find(fl, -1, p.unmap->buf, 0, 0, ADSP_MMAP_DMA_BUFFER, 0, &map)) {
  7855. mutex_unlock(&fl->map_mutex);
  7856. if (err)
  7857. goto bail;
  7858. /* Un-map DMA buffer on DSP*/
  7859. VERIFY(err, !(err = fastrpc_munmap_on_dsp(fl, map->raddr,
  7860. map->phys, map->size, map->flags)));
  7861. if (err)
  7862. goto bail;
  7863. mutex_lock(&fl->map_mutex);
  7864. fastrpc_mmap_free(map, 0);
  7865. }
  7866. mutex_unlock(&fl->map_mutex);
  7867. bail:
  7868. if (fl) {
  7869. spin_lock_irqsave(&me->hlock, irq_flags);
  7870. if (fl->file_close && fl->is_dma_invoke_pend)
  7871. complete(&fl->dma_invoke);
  7872. fl->is_dma_invoke_pend = false;
  7873. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7874. }
  7875. mutex_unlock(&fl->internal_map_mutex);
  7876. return err;
  7877. }
  7878. long fastrpc_dev_get_hlos_pid(struct fastrpc_device *dev, unsigned long invoke_param)
  7879. {
  7880. int err = 0;
  7881. union fastrpc_dev_param p;
  7882. struct fastrpc_file *fl = NULL;
  7883. struct fastrpc_apps *me = &gfa;
  7884. unsigned long irq_flags = 0;
  7885. p.hpid = (struct fastrpc_dev_get_hlos_pid *)invoke_param;
  7886. spin_lock_irqsave(&me->hlock, irq_flags);
  7887. /* Verify if fastrpc device is closed*/
  7888. VERIFY(err, dev && !dev->dev_close);
  7889. if (err) {
  7890. err = -ESRCH;
  7891. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7892. return err;
  7893. }
  7894. fl = dev->fl;
  7895. /* Verify if fastrpc file is not NULL*/
  7896. if (!fl) {
  7897. err = -EBADF;
  7898. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7899. return err;
  7900. }
  7901. p.hpid->hlos_pid = fl->tgid;
  7902. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7903. return err;
  7904. }
  7905. long fastrpc_driver_invoke(struct fastrpc_device *dev, unsigned int invoke_num,
  7906. unsigned long invoke_param)
  7907. {
  7908. int err = 0;
  7909. switch (invoke_num) {
  7910. case FASTRPC_DEV_MAP_DMA:
  7911. err = fastrpc_dev_map_dma(dev, invoke_param);
  7912. break;
  7913. case FASTRPC_DEV_UNMAP_DMA:
  7914. err = fastrpc_dev_unmap_dma(dev, invoke_param);
  7915. break;
  7916. case FASTRPC_DEV_GET_HLOS_PID:
  7917. err = fastrpc_dev_get_hlos_pid(dev, invoke_param);
  7918. break;
  7919. default:
  7920. err = -ENOTTY;
  7921. break;
  7922. }
  7923. return err;
  7924. }
  7925. EXPORT_SYMBOL(fastrpc_driver_invoke);
  7926. static struct device fastrpc_bus = {
  7927. .init_name = "fastrpc"
  7928. };
  7929. static int fastrpc_bus_match(struct device *dev, struct device_driver *driver)
  7930. {
  7931. struct fastrpc_apps *me = &gfa;
  7932. struct fastrpc_driver *frpc_driver = to_fastrpc_driver(driver);
  7933. struct fastrpc_device *frpc_device = to_fastrpc_device(dev);
  7934. unsigned long irq_flags = 0;
  7935. if (frpc_device->handle == frpc_driver->handle) {
  7936. spin_lock_irqsave(&me->hlock, irq_flags);
  7937. /* If device is being closed, fail the match */
  7938. if (frpc_device->dev_close) {
  7939. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7940. return 0;
  7941. }
  7942. frpc_device->refs++;
  7943. frpc_driver->device = dev;
  7944. spin_unlock_irqrestore(&me->hlock, irq_flags);
  7945. return 1;
  7946. }
  7947. return 0;
  7948. }
  7949. static int fastrpc_bus_probe(struct device *dev)
  7950. {
  7951. struct fastrpc_device *frpc_dev = to_fastrpc_device(dev);
  7952. struct fastrpc_driver *frpc_drv = to_fastrpc_driver(dev->driver);
  7953. if (frpc_drv && frpc_drv->probe)
  7954. return frpc_drv->probe(frpc_dev);
  7955. return 0;
  7956. }
  7957. static void fastrpc_bus_remove(struct device *dev)
  7958. {
  7959. struct fastrpc_driver *frpc_drv = to_fastrpc_driver(dev->driver);
  7960. if (frpc_drv && frpc_drv->callback)
  7961. frpc_drv->callback(to_fastrpc_device(dev), FASTRPC_PROC_DOWN);
  7962. }
  7963. static struct bus_type fastrpc_bus_type = {
  7964. .name = "fastrpc",
  7965. .match = fastrpc_bus_match,
  7966. .probe = fastrpc_bus_probe,
  7967. .remove = fastrpc_bus_remove,
  7968. };
  7969. static void fastrpc_dev_release(struct device *dev)
  7970. {
  7971. kfree(to_fastrpc_device(dev));
  7972. }
  7973. static int fastrpc_device_create(struct fastrpc_file *fl)
  7974. {
  7975. int err = 0;
  7976. struct fastrpc_device *frpc_dev;
  7977. struct fastrpc_apps *me = &gfa;
  7978. unsigned long irq_flags = 0;
  7979. frpc_dev = kzalloc(sizeof(*frpc_dev), GFP_KERNEL);
  7980. if (!frpc_dev) {
  7981. err = -ENOMEM;
  7982. goto bail;
  7983. }
  7984. frpc_dev->dev.parent = &fastrpc_bus;
  7985. frpc_dev->dev.bus = &fastrpc_bus_type;
  7986. /* Use HLOS PID, unique fastrpc process ID and CID to create device file,
  7987. * Else names would conflict for multiple sessions
  7988. * And also for better ability to debug
  7989. */
  7990. dev_set_name(&frpc_dev->dev, "%s-%d-%d-%d",
  7991. dev_name(frpc_dev->dev.parent), fl->tgid, fl->tgid_frpc, fl->cid);
  7992. frpc_dev->dev.release = fastrpc_dev_release;
  7993. frpc_dev->fl = fl;
  7994. /* Use unique fastrpc tgid as handle */
  7995. frpc_dev->handle = fl->tgid_frpc;
  7996. err = device_register(&frpc_dev->dev);
  7997. if (err) {
  7998. put_device(&frpc_dev->dev);
  7999. ADSPRPC_ERR(
  8000. "fastrpc device register failed for process %d unique fastrpc tgid %d session %d with error %d\n",
  8001. fl->tgid, fl->tgid_frpc, fl->sessionid, err);
  8002. goto bail;
  8003. }
  8004. fl->device = frpc_dev;
  8005. spin_lock_irqsave(&me->hlock, irq_flags);
  8006. hlist_add_head(&frpc_dev->hn, &me->frpc_devices);
  8007. spin_unlock_irqrestore(&me->hlock, irq_flags);
  8008. bail:
  8009. return err;
  8010. }
  8011. void fastrpc_driver_unregister(struct fastrpc_driver *frpc_driver)
  8012. {
  8013. struct fastrpc_apps *me = &gfa;
  8014. struct device *dev = NULL;
  8015. struct fastrpc_device *frpc_dev = NULL;
  8016. bool is_device_closed = false;
  8017. unsigned long irq_flags = 0;
  8018. spin_lock_irqsave(&me->hlock, irq_flags);
  8019. dev = frpc_driver->device;
  8020. if (dev) {
  8021. frpc_dev = to_fastrpc_device(dev);
  8022. if (frpc_dev->refs > 0)
  8023. frpc_dev->refs--;
  8024. else
  8025. ADSPRPC_ERR("Fastrpc device for driver %s is already freed\n",
  8026. frpc_driver->driver.name);
  8027. if (frpc_dev->dev_close) {
  8028. hlist_del_init(&frpc_dev->hn);
  8029. is_device_closed = true;
  8030. }
  8031. }
  8032. hlist_del_init(&frpc_driver->hn);
  8033. spin_unlock_irqrestore(&me->hlock, irq_flags);
  8034. if (is_device_closed) {
  8035. ADSPRPC_INFO("un-registering fastrpc device with handle %d\n",
  8036. frpc_dev->handle);
  8037. device_unregister(dev);
  8038. }
  8039. driver_unregister(&frpc_driver->driver);
  8040. ADSPRPC_INFO("Un-registering fastrpc driver %s with handle %d\n",
  8041. frpc_driver->driver.name, frpc_driver->handle);
  8042. }
  8043. EXPORT_SYMBOL(fastrpc_driver_unregister);
  8044. int fastrpc_driver_register(struct fastrpc_driver *frpc_driver)
  8045. {
  8046. int err = 0;
  8047. struct fastrpc_apps *me = &gfa;
  8048. unsigned long irq_flags = 0;
  8049. frpc_driver->driver.bus = &fastrpc_bus_type;
  8050. frpc_driver->driver.owner = THIS_MODULE;
  8051. err = driver_register(&frpc_driver->driver);
  8052. if (err) {
  8053. ADSPRPC_ERR("fastrpc driver %s failed to register with error %d\n",
  8054. frpc_driver->driver.name, err);
  8055. goto bail;
  8056. }
  8057. ADSPRPC_INFO("fastrpc driver %s registered with handle %d\n",
  8058. frpc_driver->driver.name, frpc_driver->handle);
  8059. spin_lock_irqsave(&me->hlock, irq_flags);
  8060. hlist_add_head(&frpc_driver->hn, &me->frpc_drivers);
  8061. spin_unlock_irqrestore(&me->hlock, irq_flags);
  8062. bail:
  8063. return err;
  8064. }
  8065. EXPORT_SYMBOL(fastrpc_driver_register);
  8066. static int __init fastrpc_device_init(void)
  8067. {
  8068. struct fastrpc_apps *me = &gfa;
  8069. int err = 0, i;
  8070. uintptr_t attr = 0;
  8071. dma_addr_t region_phys = 0;
  8072. void *region_vaddr = NULL;
  8073. struct fastrpc_buf *buf = NULL;
  8074. debugfs_root = debugfs_create_dir("adsprpc", NULL);
  8075. if (IS_ERR_OR_NULL(debugfs_root)) {
  8076. pr_warn("Error: %s: %s: failed to create debugfs root dir\n",
  8077. current->comm, __func__);
  8078. debugfs_remove_recursive(debugfs_root);
  8079. debugfs_root = NULL;
  8080. }
  8081. memset(me, 0, sizeof(*me));
  8082. fastrpc_init(me);
  8083. fastrpc_get_nsp_status(me);
  8084. me->dev = NULL;
  8085. me->legacy_remote_heap = false;
  8086. err = bus_register(&fastrpc_bus_type);
  8087. if (err) {
  8088. ADSPRPC_ERR("fastrpc bus register failed with err %d\n",
  8089. err);
  8090. goto bus_register_bail;
  8091. }
  8092. err = device_register(&fastrpc_bus);
  8093. if (err) {
  8094. ADSPRPC_ERR("fastrpc bus device register failed with err %d\n",
  8095. err);
  8096. goto bus_device_register_bail;
  8097. }
  8098. me->fastrpc_bus_register = true;
  8099. fastrpc_lowest_capacity_corecount(me);
  8100. VERIFY(err, 0 == platform_driver_register(&fastrpc_driver));
  8101. if (err)
  8102. goto register_bail;
  8103. VERIFY(err, 0 == alloc_chrdev_region(&me->dev_no, 0, NUM_CHANNELS,
  8104. DEVICE_NAME));
  8105. if (err)
  8106. goto alloc_chrdev_bail;
  8107. cdev_init(&me->cdev, &fops);
  8108. me->cdev.owner = THIS_MODULE;
  8109. VERIFY(err, 0 == cdev_add(&me->cdev, MKDEV(MAJOR(me->dev_no), 0),
  8110. NUM_DEVICES));
  8111. if (err)
  8112. goto cdev_init_bail;
  8113. me->class = class_create(THIS_MODULE, "fastrpc");
  8114. VERIFY(err, !IS_ERR(me->class));
  8115. if (err)
  8116. goto class_create_bail;
  8117. #if IS_ENABLED(CONFIG_MSM_ADSPRPC_TRUSTED)
  8118. me->compat = 1;
  8119. #else
  8120. me->compat = (fops.compat_ioctl == NULL) ? 0 : 1;
  8121. #endif
  8122. /*
  8123. * Create devices and register with sysfs
  8124. * Create first device with minor number 0
  8125. */
  8126. me->non_secure_dev = device_create(me->class, NULL,
  8127. MKDEV(MAJOR(me->dev_no), MINOR_NUM_DEV),
  8128. NULL, DEVICE_NAME);
  8129. VERIFY(err, !IS_ERR_OR_NULL(me->non_secure_dev));
  8130. if (err) {
  8131. err = -ENODEV;
  8132. goto device_create_bail;
  8133. }
  8134. /* Create secure device with minor number for secure device */
  8135. me->secure_dev = device_create(me->class, NULL,
  8136. MKDEV(MAJOR(me->dev_no), MINOR_NUM_SECURE_DEV),
  8137. NULL, DEVICE_NAME_SECURE);
  8138. VERIFY(err, !IS_ERR_OR_NULL(me->secure_dev));
  8139. if (err)
  8140. goto device_create_bail;
  8141. for (i = 0; i < NUM_CHANNELS; i++) {
  8142. me->jobid[i] = 1;
  8143. me->channel[i].dev = me->secure_dev;
  8144. me->channel[i].ssrcount = 0;
  8145. me->channel[i].in_hib = 0;
  8146. me->channel[i].prevssrcount = 0;
  8147. me->channel[i].subsystemstate = SUBSYSTEM_UP;
  8148. me->channel[i].rh_dump_dev = NULL;
  8149. me->channel[i].nb.notifier_call = fastrpc_restart_notifier_cb;
  8150. me->channel[i].handle = qcom_register_ssr_notifier(
  8151. gcinfo[i].subsys,
  8152. &me->channel[i].nb);
  8153. if (i == CDSP_DOMAIN_ID) {
  8154. me->channel[i].dev = me->non_secure_dev;
  8155. #if !IS_ENABLED(CONFIG_MSM_ADSPRPC_TRUSTED)
  8156. err = fastrpc_alloc_cma_memory(&region_phys,
  8157. &region_vaddr,
  8158. MINI_DUMP_DBG_SIZE,
  8159. (unsigned long)attr);
  8160. #endif
  8161. if (err)
  8162. ADSPRPC_WARN("%s: CMA alloc failed err 0x%x\n",
  8163. __func__, err);
  8164. VERIFY(err, NULL != (buf = kzalloc(sizeof(*buf), GFP_KERNEL)));
  8165. if (err) {
  8166. err = -ENOMEM;
  8167. ADSPRPC_ERR("%s: CMA alloc failed err 0x%x\n",
  8168. __func__, err);
  8169. goto device_create_bail;
  8170. }
  8171. INIT_HLIST_NODE(&buf->hn);
  8172. buf->virt = region_vaddr;
  8173. buf->phys = (uintptr_t)region_phys;
  8174. buf->size = MINI_DUMP_DBG_SIZE;
  8175. buf->dma_attr = attr;
  8176. buf->raddr = 0;
  8177. ktime_get_real_ts64(&buf->buf_start_time);
  8178. me->channel[i].buf = buf;
  8179. }
  8180. if (IS_ERR_OR_NULL(me->channel[i].handle))
  8181. pr_warn("adsprpc: %s: SSR notifier register failed for %s with err %d\n",
  8182. __func__, gcinfo[i].subsys,
  8183. PTR_ERR(me->channel[i].handle));
  8184. else
  8185. pr_info("adsprpc: %s: SSR notifier registered for %s\n",
  8186. __func__, gcinfo[i].subsys);
  8187. }
  8188. err = fastrpc_transport_init();
  8189. if (err)
  8190. goto device_create_bail;
  8191. me->transport_initialized = 1;
  8192. #ifdef CONFIG_HIBERNATION
  8193. err = register_pm_notifier(&fastrpc_notif_block);
  8194. if (err)
  8195. goto device_create_bail;
  8196. #endif
  8197. fastrpc_register_wakeup_source(me->non_secure_dev,
  8198. FASTRPC_NON_SECURE_WAKE_SOURCE_CLIENT_NAME,
  8199. &me->wake_source);
  8200. fastrpc_register_wakeup_source(me->secure_dev,
  8201. FASTRPC_SECURE_WAKE_SOURCE_CLIENT_NAME,
  8202. &me->wake_source_secure);
  8203. return 0;
  8204. device_create_bail:
  8205. for (i = 0; i < NUM_CHANNELS; i++) {
  8206. if (me->channel[i].handle)
  8207. qcom_unregister_ssr_notifier(me->channel[i].handle,
  8208. &me->channel[i].nb);
  8209. }
  8210. if (!IS_ERR_OR_NULL(me->non_secure_dev))
  8211. device_destroy(me->class, MKDEV(MAJOR(me->dev_no),
  8212. MINOR_NUM_DEV));
  8213. if (!IS_ERR_OR_NULL(me->secure_dev))
  8214. device_destroy(me->class, MKDEV(MAJOR(me->dev_no),
  8215. MINOR_NUM_SECURE_DEV));
  8216. class_destroy(me->class);
  8217. class_create_bail:
  8218. cdev_del(&me->cdev);
  8219. cdev_init_bail:
  8220. unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
  8221. alloc_chrdev_bail:
  8222. platform_driver_unregister(&fastrpc_driver);
  8223. register_bail:
  8224. device_unregister(&fastrpc_bus);
  8225. bus_device_register_bail:
  8226. bus_unregister(&fastrpc_bus_type);
  8227. bus_register_bail:
  8228. fastrpc_deinit();
  8229. return err;
  8230. }
  8231. static void __exit fastrpc_device_exit(void)
  8232. {
  8233. struct fastrpc_apps *me = &gfa;
  8234. int i;
  8235. fastrpc_file_list_dtor(me);
  8236. fastrpc_deinit();
  8237. wakeup_source_unregister(me->wake_source);
  8238. wakeup_source_unregister(me->wake_source_secure);
  8239. for (i = 0; i < NUM_CHANNELS; i++) {
  8240. if (i == CDSP_DOMAIN_ID)
  8241. kfree(me->channel[i].buf);
  8242. if (!gcinfo[i].name)
  8243. continue;
  8244. qcom_unregister_ssr_notifier(me->channel[i].handle,
  8245. &me->channel[i].nb);
  8246. }
  8247. /* Destroy the secure and non secure devices */
  8248. device_destroy(me->class, MKDEV(MAJOR(me->dev_no), MINOR_NUM_DEV));
  8249. device_destroy(me->class, MKDEV(MAJOR(me->dev_no),
  8250. MINOR_NUM_SECURE_DEV));
  8251. of_reserved_mem_device_release(me->dev);
  8252. class_destroy(me->class);
  8253. cdev_del(&me->cdev);
  8254. unregister_chrdev_region(me->dev_no, NUM_CHANNELS);
  8255. if (me->transport_initialized)
  8256. fastrpc_transport_deinit();
  8257. me->transport_initialized = 0;
  8258. if (me->fastrpc_bus_register) {
  8259. bus_unregister(&fastrpc_bus_type);
  8260. device_unregister(&fastrpc_bus);
  8261. }
  8262. kfree(me->gidlist.gids);
  8263. debugfs_remove_recursive(debugfs_root);
  8264. }
  8265. module_init(fastrpc_device_init);
  8266. module_exit(fastrpc_device_exit);
  8267. MODULE_LICENSE("GPL v2");