adsprpc.c 243 KB

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