adsprpc.c 246 KB

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