adsprpc.c 245 KB

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