adsprpc.c 235 KB

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