adsprpc.c 248 KB

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