adsprpc.c 247 KB

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