adsprpc.c 226 KB

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