adsprpc.c 242 KB

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