adsprpc.c 228 KB

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