adsprpc.c 221 KB

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