adsprpc.c 246 KB

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