adsprpc.c 247 KB

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