adsprpc.c 222 KB

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