adsprpc.c 210 KB

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