adsprpc.c 242 KB

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