adsprpc.c 247 KB

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