adsprpc.c 238 KB

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