adsprpc.c 222 KB

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