adsprpc.c 229 KB

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