adsprpc.c 243 KB

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