adsprpc.c 225 KB

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