adsprpc.c 228 KB

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