adsprpc.c 228 KB

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