adsprpc.c 210 KB

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