adsprpc.c 226 KB

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