adsprpc.c 244 KB

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