adsprpc.c 245 KB

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