adsprpc.c 242 KB

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