adsprpc.c 238 KB

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