adsprpc.c 239 KB

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