adsprpc.c 233 KB

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