adsprpc.c 243 KB

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