adsprpc.c 247 KB

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