adsprpc.c 222 KB

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