adsprpc.c 222 KB

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