adsprpc.c 222 KB

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