adsprpc.c 235 KB

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