adsprpc.c 239 KB

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