adsprpc.c 228 KB

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