adsprpc.c 235 KB

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