adsprpc.c 229 KB

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