adsprpc.c 221 KB

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