adsprpc.c 226 KB

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