adsprpc.c 223 KB

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