adsprpc.c 238 KB

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