adsprpc.c 235 KB

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