qseecom.c 266 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * QTI Secure Execution Environment Communicator (QSEECOM) driver
  4. *
  5. * Copyright (c) 2012-2021, The Linux Foundation. All rights reserved.
  6. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  7. */
  8. #define pr_fmt(fmt) "QSEECOM: %s: " fmt, __func__
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/fs.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/cdev.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/sched.h>
  18. #include <linux/list.h>
  19. #include <linux/mutex.h>
  20. #include <linux/io.h>
  21. #include <linux/dma-buf.h>
  22. #include <linux/ion.h>
  23. #include <linux/msm_ion.h>
  24. #include <linux/types.h>
  25. #include <linux/clk.h>
  26. #include <linux/qseecom.h>
  27. #include <linux/elf.h>
  28. #include <linux/firmware.h>
  29. #include <linux/freezer.h>
  30. #include <linux/scatterlist.h>
  31. #include <linux/regulator/consumer.h>
  32. #include <linux/dma-mapping.h>
  33. #include <soc/qcom/qseecom_scm.h>
  34. #include <soc/qcom/qseecomi.h>
  35. #include <asm/cacheflush.h>
  36. #include <linux/delay.h>
  37. #include <linux/signal.h>
  38. #include <linux/compat.h>
  39. #include <linux/kthread.h>
  40. #include <linux/dma-map-ops.h>
  41. #include <linux/cma.h>
  42. #include <linux/of_platform.h>
  43. #include <linux/interconnect.h>
  44. #include <linux/of_reserved_mem.h>
  45. #include <linux/qtee_shmbridge.h>
  46. #include <linux/mem-buf.h>
  47. #include <linux/version.h>
  48. #include "ice.h"
  49. #if IS_ENABLED(CONFIG_QSEECOM_PROXY)
  50. #include <linux/qseecom_kernel.h>
  51. #include "misc/qseecom_priv.h"
  52. #else
  53. #include "misc/qseecom_kernel.h"
  54. #endif
  55. #if (LINUX_VERSION_CODE <= KERNEL_VERSION(6,0,0))
  56. #define KERNEL_VERSION_LEGACY
  57. #endif
  58. #define QSEECOM_DEV "qseecom"
  59. #define QSEOS_VERSION_14 0x14
  60. #define QSEEE_VERSION_00 0x400000
  61. #define QSEE_VERSION_01 0x401000
  62. #define QSEE_VERSION_02 0x402000
  63. #define QSEE_VERSION_03 0x403000
  64. #define QSEE_VERSION_04 0x404000
  65. #define QSEE_VERSION_05 0x405000
  66. #define QSEE_VERSION_20 0x800000
  67. #define QSEE_VERSION_40 0x1000000 /* TZ.BF.4.0 */
  68. #define QSEE_CE_CLK_100MHZ 100000000
  69. #define CE_CLK_DIV 1000000
  70. #define QSEECOM_MAX_SG_ENTRY 4096
  71. #define QSEECOM_SG_ENTRY_MSG_BUF_SZ_64BIT \
  72. (QSEECOM_MAX_SG_ENTRY * SG_ENTRY_SZ_64BIT)
  73. #define QSEECOM_INVALID_KEY_ID 0xff
  74. /* Save partition image hash for authentication check */
  75. #define SCM_SAVE_PARTITION_HASH_ID 0x01
  76. /* Check if enterprise security is activate */
  77. #define SCM_IS_ACTIVATED_ID 0x02
  78. /* Encrypt/Decrypt Data Integrity Partition (DIP) for MDTP */
  79. #define SCM_MDTP_CIPHER_DIP 0x01
  80. /* Maximum Allowed Size (128K) of Data Integrity Partition (DIP) for MDTP */
  81. #define MAX_DIP 0x20000
  82. #define RPMB_SERVICE 0x2000
  83. #define SSD_SERVICE 0x3000
  84. #define QSEECOM_SEND_CMD_CRYPTO_TIMEOUT 2000
  85. #define QSEECOM_LOAD_APP_CRYPTO_TIMEOUT 2000
  86. #define TWO 2
  87. #define QSEECOM_UFS_ICE_CE_NUM 10
  88. #define QSEECOM_SDCC_ICE_CE_NUM 20
  89. #define QSEECOM_ICE_FDE_KEY_INDEX 0
  90. #define PHY_ADDR_4G (1ULL<<32)
  91. #define QSEECOM_STATE_NOT_READY 0
  92. #define QSEECOM_STATE_SUSPEND 1
  93. #define QSEECOM_STATE_READY 2
  94. #define QSEECOM_ICE_FDE_KEY_SIZE_MASK 2
  95. /*
  96. * default ce info unit to 0 for
  97. * services which
  98. * support only single instance.
  99. * Most of services are in this category.
  100. */
  101. #define DEFAULT_CE_INFO_UNIT 0
  102. #define DEFAULT_NUM_CE_INFO_UNIT 1
  103. #define FDE_FLAG_POS 4
  104. #define ENABLE_KEY_WRAP_IN_KS (1 << FDE_FLAG_POS)
  105. enum qseecom_clk_definitions {
  106. CLK_DFAB = 0,
  107. CLK_SFPB,
  108. };
  109. enum qseecom_ice_key_size_type {
  110. QSEECOM_ICE_FDE_KEY_SIZE_16_BYTE =
  111. (0 << QSEECOM_ICE_FDE_KEY_SIZE_MASK),
  112. QSEECOM_ICE_FDE_KEY_SIZE_32_BYTE =
  113. (1 << QSEECOM_ICE_FDE_KEY_SIZE_MASK),
  114. QSEE_ICE_FDE_KEY_SIZE_UNDEFINED =
  115. (0xF << QSEECOM_ICE_FDE_KEY_SIZE_MASK),
  116. };
  117. enum qseecom_client_handle_type {
  118. QSEECOM_CLIENT_APP = 1,
  119. QSEECOM_LISTENER_SERVICE,
  120. QSEECOM_SECURE_SERVICE,
  121. QSEECOM_GENERIC,
  122. QSEECOM_UNAVAILABLE_CLIENT_APP,
  123. };
  124. enum qseecom_ce_hw_instance {
  125. CLK_QSEE = 0,
  126. CLK_CE_DRV,
  127. CLK_INVALID,
  128. };
  129. enum qseecom_cache_ops {
  130. QSEECOM_CACHE_CLEAN,
  131. QSEECOM_CACHE_INVALIDATE,
  132. };
  133. enum qseecom_listener_unregister_kthread_state {
  134. LSNR_UNREG_KT_SLEEP = 0,
  135. LSNR_UNREG_KT_WAKEUP,
  136. };
  137. enum qseecom_unload_app_kthread_state {
  138. UNLOAD_APP_KT_SLEEP = 0,
  139. UNLOAD_APP_KT_WAKEUP,
  140. };
  141. static DEFINE_MUTEX(qsee_bw_mutex);
  142. static DEFINE_MUTEX(app_access_lock);
  143. static DEFINE_MUTEX(clk_access_lock);
  144. static DEFINE_MUTEX(listener_access_lock);
  145. static DEFINE_MUTEX(unload_app_pending_list_lock);
  146. struct sglist_info {
  147. uint32_t indexAndFlags;
  148. uint32_t sizeOrCount;
  149. };
  150. /*
  151. * The 31st bit indicates only one or multiple physical address inside
  152. * the request buffer. If it is set, the index locates a single physical addr
  153. * inside the request buffer, and `sizeOrCount` is the size of the memory being
  154. * shared at that physical address.
  155. * Otherwise, the index locates an array of {start, len} pairs (a
  156. * "scatter/gather list"), and `sizeOrCount` gives the number of entries in
  157. * that array.
  158. *
  159. * The 30th bit indicates 64 or 32bit address; when it is set, physical addr
  160. * and scatter gather entry sizes are 64-bit values. Otherwise, 32-bit values.
  161. *
  162. * The bits [0:29] of `indexAndFlags` hold an offset into the request buffer.
  163. */
  164. #define SGLISTINFO_SET_INDEX_FLAG(c, s, i) \
  165. ((uint32_t)(((c & 1) << 31) | ((s & 1) << 30) | (i & 0x3fffffff)))
  166. #define SGLISTINFO_TABLE_SIZE (sizeof(struct sglist_info) * MAX_ION_FD)
  167. #define FEATURE_ID_WHITELIST 15 /*whitelist feature id*/
  168. #define MAKE_WHITELIST_VERSION(major, minor, patch) \
  169. (((major & 0x3FF) << 22) | ((minor & 0x3FF) << 12) | (patch & 0xFFF))
  170. #define MAKE_NULL(sgt, attach, dmabuf) do {\
  171. sgt = NULL;\
  172. attach = NULL;\
  173. dmabuf = NULL;\
  174. } while (0)
  175. struct qseecom_registered_listener_list {
  176. struct list_head list;
  177. struct qseecom_register_listener_req svc;
  178. void *user_virt_sb_base;
  179. struct dma_buf *dmabuf;
  180. struct dma_buf_attachment *attach;
  181. struct sg_table *sgt;
  182. u8 *sb_virt;
  183. phys_addr_t sb_phys;
  184. size_t sb_length;
  185. wait_queue_head_t rcv_req_wq;
  186. /* rcv_req_flag: 0: ready and empty; 1: received req */
  187. int rcv_req_flag;
  188. int send_resp_flag;
  189. bool listener_in_use;
  190. /* wq for thread blocked on this listener*/
  191. wait_queue_head_t listener_block_app_wq;
  192. struct sglist_info *sglistinfo_ptr;
  193. struct qtee_shm sglistinfo_shm;
  194. uint32_t sglist_cnt;
  195. int abort;
  196. bool unregister_pending;
  197. };
  198. struct qseecom_unregister_pending_list {
  199. struct list_head list;
  200. struct qseecom_dev_handle *data;
  201. };
  202. struct qseecom_registered_app_list {
  203. struct list_head list;
  204. u32 app_id;
  205. u32 ref_cnt;
  206. char app_name[MAX_APP_NAME_SIZE];
  207. u32 app_arch;
  208. bool app_blocked;
  209. u32 check_block;
  210. u32 blocked_on_listener_id;
  211. };
  212. struct qseecom_registered_kclient_list {
  213. struct list_head list;
  214. struct qseecom_handle *handle;
  215. };
  216. struct qseecom_ce_info_use {
  217. unsigned char handle[MAX_CE_INFO_HANDLE_SIZE];
  218. unsigned int unit_num;
  219. unsigned int num_ce_pipe_entries;
  220. struct qseecom_ce_pipe_entry *ce_pipe_entry;
  221. bool alloc;
  222. uint32_t type;
  223. };
  224. struct ce_hw_usage_info {
  225. uint32_t qsee_ce_hw_instance;
  226. uint32_t num_fde;
  227. struct qseecom_ce_info_use *fde;
  228. uint32_t num_pfe;
  229. struct qseecom_ce_info_use *pfe;
  230. };
  231. struct qseecom_clk {
  232. enum qseecom_ce_hw_instance instance;
  233. struct clk *ce_core_clk;
  234. struct clk *ce_clk;
  235. struct clk *ce_core_src_clk;
  236. struct clk *ce_bus_clk;
  237. uint32_t clk_access_cnt;
  238. };
  239. struct qseecom_control {
  240. struct list_head registered_listener_list_head;
  241. struct list_head registered_app_list_head;
  242. spinlock_t registered_app_list_lock;
  243. struct list_head registered_kclient_list_head;
  244. spinlock_t registered_kclient_list_lock;
  245. wait_queue_head_t send_resp_wq;
  246. int send_resp_flag;
  247. uint32_t qseos_version;
  248. uint32_t qsee_version;
  249. struct device *pdev; /* class_dev */
  250. struct device *dev; /* platform_dev->dev */
  251. struct class *driver_class;
  252. dev_t qseecom_device_no;
  253. bool whitelist_support;
  254. bool commonlib_loaded;
  255. bool commonlib64_loaded;
  256. struct ce_hw_usage_info ce_info;
  257. int qsee_bw_count;
  258. int qsee_sfpb_bw_count;
  259. uint32_t qsee_perf_client;
  260. struct icc_path *icc_path;
  261. uint32_t avg_bw;
  262. uint32_t peak_bw;
  263. struct qseecom_clk qsee;
  264. struct qseecom_clk ce_drv;
  265. bool support_bus_scaling;
  266. bool support_fde;
  267. bool support_pfe;
  268. bool fde_key_size;
  269. uint32_t cumulative_mode;
  270. enum qseecom_bandwidth_request_mode current_mode;
  271. struct timer_list bw_scale_down_timer;
  272. struct work_struct bw_inactive_req_ws;
  273. struct cdev cdev;
  274. bool timer_running;
  275. bool no_clock_support;
  276. unsigned int ce_opp_freq_hz;
  277. bool appsbl_qseecom_support;
  278. uint32_t qsee_reentrancy_support;
  279. bool enable_key_wrap_in_ks;
  280. uint32_t app_block_ref_cnt;
  281. wait_queue_head_t app_block_wq;
  282. atomic_t qseecom_state;
  283. int is_apps_region_protected;
  284. bool smcinvoke_support;
  285. uint64_t qseecom_bridge_handle;
  286. uint64_t ta_bridge_handle;
  287. uint64_t user_contig_bridge_handle;
  288. struct list_head unregister_lsnr_pending_list_head;
  289. wait_queue_head_t register_lsnr_pending_wq;
  290. struct task_struct *unregister_lsnr_kthread_task;
  291. wait_queue_head_t unregister_lsnr_kthread_wq;
  292. atomic_t unregister_lsnr_kthread_state;
  293. struct list_head unload_app_pending_list_head;
  294. struct task_struct *unload_app_kthread_task;
  295. wait_queue_head_t unload_app_kthread_wq;
  296. atomic_t unload_app_kthread_state;
  297. };
  298. struct qseecom_unload_app_pending_list {
  299. struct list_head list;
  300. struct qseecom_dev_handle *data;
  301. };
  302. struct qseecom_sec_buf_fd_info {
  303. bool is_sec_buf_fd;
  304. size_t size;
  305. void *vbase;
  306. phys_addr_t pbase;
  307. struct qtee_shm shm;
  308. };
  309. struct qseecom_param_memref {
  310. uint32_t buffer;
  311. uint32_t size;
  312. };
  313. struct qseecom_client_handle {
  314. u32 app_id;
  315. struct dma_buf *dmabuf;
  316. struct dma_buf_attachment *attach;
  317. struct sg_table *sgt;
  318. u8 *sb_virt;
  319. phys_addr_t sb_phys;
  320. size_t sb_length;
  321. unsigned long user_virt_sb_base;
  322. char app_name[MAX_APP_NAME_SIZE];
  323. u32 app_arch;
  324. struct qseecom_sec_buf_fd_info sec_buf_fd[MAX_ION_FD];
  325. bool from_smcinvoke;
  326. struct qtee_shm shm; /* kernel client's shm for req/rsp buf */
  327. bool unload_pending;
  328. bool from_loadapp;
  329. };
  330. struct qseecom_listener_handle {
  331. u32 id;
  332. bool unregister_pending;
  333. bool release_called;
  334. };
  335. static struct qseecom_control qseecom;
  336. struct qseecom_dev_handle {
  337. enum qseecom_client_handle_type type;
  338. union {
  339. struct qseecom_client_handle client;
  340. struct qseecom_listener_handle listener;
  341. };
  342. bool released;
  343. int abort;
  344. wait_queue_head_t abort_wq;
  345. atomic_t ioctl_count;
  346. bool perf_enabled;
  347. bool fast_load_enabled;
  348. enum qseecom_bandwidth_request_mode mode;
  349. struct sglist_info *sglistinfo_ptr;
  350. struct qtee_shm sglistinfo_shm;
  351. uint32_t sglist_cnt;
  352. bool use_legacy_cmd;
  353. };
  354. struct qseecom_key_id_usage_desc {
  355. uint8_t desc[QSEECOM_KEY_ID_SIZE];
  356. };
  357. struct qseecom_crypto_info {
  358. unsigned int unit_num;
  359. unsigned int ce;
  360. unsigned int pipe_pair;
  361. };
  362. static struct qseecom_key_id_usage_desc key_id_array[] = {
  363. {
  364. .desc = "Undefined Usage Index",
  365. },
  366. {
  367. .desc = "Full Disk Encryption",
  368. },
  369. {
  370. .desc = "Per File Encryption",
  371. },
  372. {
  373. .desc = "UFS ICE Full Disk Encryption",
  374. },
  375. {
  376. .desc = "SDCC ICE Full Disk Encryption",
  377. },
  378. };
  379. /* Function proto types */
  380. static int qsee_vote_for_clock(struct qseecom_dev_handle *, int32_t);
  381. static void qsee_disable_clock_vote(struct qseecom_dev_handle *, int32_t);
  382. static int __qseecom_enable_clk(enum qseecom_ce_hw_instance ce);
  383. static void __qseecom_disable_clk(enum qseecom_ce_hw_instance ce);
  384. static int __qseecom_init_clk(enum qseecom_ce_hw_instance ce);
  385. static int qseecom_load_commonlib_image(struct qseecom_dev_handle *data,
  386. char *cmnlib_name);
  387. static int qseecom_enable_ice_setup(int usage);
  388. static int qseecom_disable_ice_setup(int usage);
  389. static void __qseecom_reentrancy_check_if_no_app_blocked(uint32_t smc_id);
  390. static int qseecom_get_ce_info(struct qseecom_dev_handle *data,
  391. void __user *argp);
  392. static int qseecom_free_ce_info(struct qseecom_dev_handle *data,
  393. void __user *argp);
  394. static int qseecom_query_ce_info(struct qseecom_dev_handle *data,
  395. void __user *argp);
  396. static int __qseecom_unload_app(struct qseecom_dev_handle *data,
  397. uint32_t app_id);
  398. static int __maybe_unused get_qseecom_keymaster_status(char *str)
  399. {
  400. get_option(&str, &qseecom.is_apps_region_protected);
  401. return 1;
  402. }
  403. __setup("androidboot.keymaster=", get_qseecom_keymaster_status);
  404. static int __qseecom_alloc_coherent_buf(
  405. uint32_t size, u8 **vaddr, phys_addr_t *paddr);
  406. static void __qseecom_free_coherent_buf(uint32_t size,
  407. u8 *vaddr, phys_addr_t paddr);
  408. #define QSEECOM_SCM_EBUSY_WAIT_MS 30
  409. #define QSEECOM_SCM_EBUSY_MAX_RETRY 67
  410. #define QSEE_RESULT_FAIL_APP_BUSY 315
  411. static int __qseecom_scm_call2_locked(uint32_t smc_id, struct qseecom_scm_desc *desc)
  412. {
  413. int ret = 0;
  414. int retry_count = 0;
  415. do {
  416. ret = qcom_scm_qseecom_call(smc_id, desc, false);
  417. if ((ret == -EBUSY) || (desc && (desc->ret[0] == -QSEE_RESULT_FAIL_APP_BUSY))) {
  418. mutex_unlock(&app_access_lock);
  419. msleep(QSEECOM_SCM_EBUSY_WAIT_MS);
  420. mutex_lock(&app_access_lock);
  421. }
  422. if (retry_count == 33)
  423. pr_warn("secure world has been busy for 1 second!\n");
  424. } while (((ret == -EBUSY) || (desc && (desc->ret[0] == -QSEE_RESULT_FAIL_APP_BUSY))) &&
  425. (retry_count++ < QSEECOM_SCM_EBUSY_MAX_RETRY));
  426. return ret;
  427. }
  428. static char *__qseecom_alloc_tzbuf(uint32_t size,
  429. phys_addr_t *pa, struct qtee_shm *shm)
  430. {
  431. char *tzbuf = NULL;
  432. int ret = qtee_shmbridge_allocate_shm(size, shm);
  433. if (ret)
  434. return NULL;
  435. tzbuf = shm->vaddr;
  436. memset(tzbuf, 0, size);
  437. *pa = shm->paddr;
  438. return tzbuf;
  439. }
  440. static void __qseecom_free_tzbuf(struct qtee_shm *shm)
  441. {
  442. qtee_shmbridge_free_shm(shm);
  443. }
  444. static int qseecom_scm_call2(uint32_t svc_id, uint32_t tz_cmd_id,
  445. const void *req_buf, void *resp_buf)
  446. {
  447. int ret = 0;
  448. uint32_t smc_id = 0;
  449. uint32_t qseos_cmd_id = 0;
  450. struct qseecom_scm_desc desc = {0};
  451. struct qseecom_command_scm_resp *scm_resp = NULL;
  452. struct qtee_shm shm = {0};
  453. phys_addr_t pa;
  454. if (!req_buf || !resp_buf) {
  455. pr_err("Invalid buffer pointer\n");
  456. return -EINVAL;
  457. }
  458. qseos_cmd_id = *(uint32_t *)req_buf;
  459. scm_resp = (struct qseecom_command_scm_resp *)resp_buf;
  460. switch (svc_id) {
  461. case SCM_SVC_INFO: {
  462. if (tz_cmd_id == 3) {
  463. smc_id = TZ_INFO_GET_FEATURE_VERSION_ID;
  464. desc.arginfo = TZ_INFO_GET_FEATURE_VERSION_ID_PARAM_ID;
  465. desc.args[0] = *(uint32_t *)req_buf;
  466. } else {
  467. pr_err("Unsupported svc_id %d, tz_cmd_id %d\n",
  468. svc_id, tz_cmd_id);
  469. return -EINVAL;
  470. }
  471. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  472. break;
  473. }
  474. case SCM_SVC_ES: {
  475. switch (tz_cmd_id) {
  476. case SCM_SAVE_PARTITION_HASH_ID: {
  477. u32 tzbuflen = PAGE_ALIGN(SHA256_DIGEST_LENGTH);
  478. struct qseecom_save_partition_hash_req *p_hash_req =
  479. (struct qseecom_save_partition_hash_req *)
  480. req_buf;
  481. char *tzbuf = __qseecom_alloc_tzbuf(
  482. tzbuflen, &pa, &shm);
  483. if (!tzbuf)
  484. return -ENOMEM;
  485. memset(tzbuf, 0, tzbuflen);
  486. memcpy(tzbuf, p_hash_req->digest,
  487. SHA256_DIGEST_LENGTH);
  488. qtee_shmbridge_flush_shm_buf(&shm);
  489. smc_id = TZ_ES_SAVE_PARTITION_HASH_ID;
  490. desc.arginfo = TZ_ES_SAVE_PARTITION_HASH_ID_PARAM_ID;
  491. desc.args[0] = p_hash_req->partition_id;
  492. desc.args[1] = pa;
  493. desc.args[2] = SHA256_DIGEST_LENGTH;
  494. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  495. __qseecom_free_tzbuf(&shm);
  496. break;
  497. }
  498. default: {
  499. pr_err("tz_cmd_id %d is not supported\n", tz_cmd_id);
  500. ret = -EINVAL;
  501. break;
  502. }
  503. } /* end of switch (tz_cmd_id) */
  504. break;
  505. } /* end of case SCM_SVC_ES */
  506. case SCM_SVC_TZSCHEDULER: {
  507. switch (qseos_cmd_id) {
  508. case QSEOS_APP_START_COMMAND: {
  509. struct qseecom_load_app_ireq *req;
  510. struct qseecom_load_app_64bit_ireq *req_64bit;
  511. smc_id = TZ_OS_APP_START_ID;
  512. desc.arginfo = TZ_OS_APP_START_ID_PARAM_ID;
  513. if (qseecom.qsee_version < QSEE_VERSION_40) {
  514. req = (struct qseecom_load_app_ireq *)req_buf;
  515. desc.args[0] = req->mdt_len;
  516. desc.args[1] = req->img_len;
  517. desc.args[2] = req->phy_addr;
  518. } else {
  519. req_64bit =
  520. (struct qseecom_load_app_64bit_ireq *)
  521. req_buf;
  522. desc.args[0] = req_64bit->mdt_len;
  523. desc.args[1] = req_64bit->img_len;
  524. desc.args[2] = req_64bit->phy_addr;
  525. }
  526. __qseecom_reentrancy_check_if_no_app_blocked(smc_id);
  527. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  528. break;
  529. }
  530. case QSEOS_APP_SHUTDOWN_COMMAND: {
  531. struct qseecom_unload_app_ireq *req;
  532. req = (struct qseecom_unload_app_ireq *)req_buf;
  533. smc_id = TZ_OS_APP_SHUTDOWN_ID;
  534. desc.arginfo = TZ_OS_APP_SHUTDOWN_ID_PARAM_ID;
  535. desc.args[0] = req->app_id;
  536. ret = qcom_scm_qseecom_call(smc_id, &desc, true);
  537. break;
  538. }
  539. case QSEOS_APP_LOOKUP_COMMAND: {
  540. struct qseecom_check_app_ireq *req;
  541. u32 tzbuflen = PAGE_ALIGN(sizeof(req->app_name));
  542. char *tzbuf = __qseecom_alloc_tzbuf(
  543. tzbuflen, &pa, &shm);
  544. if (!tzbuf)
  545. return -ENOMEM;
  546. req = (struct qseecom_check_app_ireq *)req_buf;
  547. pr_debug("Lookup app_name = %s\n", req->app_name);
  548. strlcpy(tzbuf, req->app_name, sizeof(req->app_name));
  549. qtee_shmbridge_flush_shm_buf(&shm);
  550. smc_id = TZ_OS_APP_LOOKUP_ID;
  551. desc.arginfo = TZ_OS_APP_LOOKUP_ID_PARAM_ID;
  552. desc.args[0] = pa;
  553. desc.args[1] = strlen(req->app_name);
  554. __qseecom_reentrancy_check_if_no_app_blocked(smc_id);
  555. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  556. __qseecom_free_tzbuf(&shm);
  557. break;
  558. }
  559. case QSEOS_APP_REGION_NOTIFICATION: {
  560. struct qsee_apps_region_info_ireq *req;
  561. struct qsee_apps_region_info_64bit_ireq *req_64bit;
  562. smc_id = TZ_OS_APP_REGION_NOTIFICATION_ID;
  563. desc.arginfo =
  564. TZ_OS_APP_REGION_NOTIFICATION_ID_PARAM_ID;
  565. if (qseecom.qsee_version < QSEE_VERSION_40) {
  566. req = (struct qsee_apps_region_info_ireq *)
  567. req_buf;
  568. desc.args[0] = req->addr;
  569. desc.args[1] = req->size;
  570. } else {
  571. req_64bit =
  572. (struct qsee_apps_region_info_64bit_ireq *)
  573. req_buf;
  574. desc.args[0] = req_64bit->addr;
  575. desc.args[1] = req_64bit->size;
  576. }
  577. __qseecom_reentrancy_check_if_no_app_blocked(smc_id);
  578. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  579. break;
  580. }
  581. case QSEOS_LOAD_SERV_IMAGE_COMMAND: {
  582. struct qseecom_load_lib_image_ireq *req;
  583. struct qseecom_load_lib_image_64bit_ireq *req_64bit;
  584. smc_id = TZ_OS_LOAD_SERVICES_IMAGE_ID;
  585. desc.arginfo = TZ_OS_LOAD_SERVICES_IMAGE_ID_PARAM_ID;
  586. if (qseecom.qsee_version < QSEE_VERSION_40) {
  587. req = (struct qseecom_load_lib_image_ireq *)
  588. req_buf;
  589. desc.args[0] = req->mdt_len;
  590. desc.args[1] = req->img_len;
  591. desc.args[2] = req->phy_addr;
  592. } else {
  593. req_64bit =
  594. (struct qseecom_load_lib_image_64bit_ireq *)
  595. req_buf;
  596. desc.args[0] = req_64bit->mdt_len;
  597. desc.args[1] = req_64bit->img_len;
  598. desc.args[2] = req_64bit->phy_addr;
  599. }
  600. __qseecom_reentrancy_check_if_no_app_blocked(smc_id);
  601. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  602. break;
  603. }
  604. case QSEOS_UNLOAD_SERV_IMAGE_COMMAND: {
  605. smc_id = TZ_OS_UNLOAD_SERVICES_IMAGE_ID;
  606. desc.arginfo = TZ_OS_UNLOAD_SERVICES_IMAGE_ID_PARAM_ID;
  607. __qseecom_reentrancy_check_if_no_app_blocked(smc_id);
  608. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  609. break;
  610. }
  611. case QSEOS_REGISTER_LISTENER: {
  612. struct qseecom_register_listener_ireq *req;
  613. struct qseecom_register_listener_64bit_ireq *req_64bit;
  614. desc.arginfo =
  615. TZ_OS_REGISTER_LISTENER_ID_PARAM_ID;
  616. if (qseecom.qsee_version < QSEE_VERSION_40) {
  617. req = (struct qseecom_register_listener_ireq *)
  618. req_buf;
  619. desc.args[0] = req->listener_id;
  620. desc.args[1] = req->sb_ptr;
  621. desc.args[2] = req->sb_len;
  622. } else {
  623. req_64bit =
  624. (struct qseecom_register_listener_64bit_ireq *)
  625. req_buf;
  626. desc.args[0] = req_64bit->listener_id;
  627. desc.args[1] = req_64bit->sb_ptr;
  628. desc.args[2] = req_64bit->sb_len;
  629. }
  630. qseecom.smcinvoke_support = true;
  631. smc_id = TZ_OS_REGISTER_LISTENER_SMCINVOKE_ID;
  632. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  633. if (ret == -EIO) {
  634. /* smcinvoke is not supported */
  635. qseecom.smcinvoke_support = false;
  636. smc_id = TZ_OS_REGISTER_LISTENER_ID;
  637. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  638. }
  639. break;
  640. }
  641. case QSEOS_DEREGISTER_LISTENER: {
  642. struct qseecom_unregister_listener_ireq *req;
  643. req = (struct qseecom_unregister_listener_ireq *)
  644. req_buf;
  645. smc_id = TZ_OS_DEREGISTER_LISTENER_ID;
  646. desc.arginfo = TZ_OS_DEREGISTER_LISTENER_ID_PARAM_ID;
  647. desc.args[0] = req->listener_id;
  648. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  649. break;
  650. }
  651. case QSEOS_LISTENER_DATA_RSP_COMMAND: {
  652. struct qseecom_client_listener_data_irsp *req;
  653. req = (struct qseecom_client_listener_data_irsp *)
  654. req_buf;
  655. smc_id = TZ_OS_LISTENER_RESPONSE_HANDLER_ID;
  656. desc.arginfo =
  657. TZ_OS_LISTENER_RESPONSE_HANDLER_ID_PARAM_ID;
  658. desc.args[0] = req->listener_id;
  659. desc.args[1] = req->status;
  660. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  661. break;
  662. }
  663. case QSEOS_LISTENER_DATA_RSP_COMMAND_WHITELIST: {
  664. struct qseecom_client_listener_data_irsp *req;
  665. struct qseecom_client_listener_data_64bit_irsp *req_64;
  666. smc_id =
  667. TZ_OS_LISTENER_RESPONSE_HANDLER_WITH_WHITELIST_ID;
  668. desc.arginfo =
  669. TZ_OS_LISTENER_RESPONSE_HANDLER_WITH_WHITELIST_PARAM_ID;
  670. if (qseecom.qsee_version < QSEE_VERSION_40) {
  671. req =
  672. (struct qseecom_client_listener_data_irsp *)
  673. req_buf;
  674. desc.args[0] = req->listener_id;
  675. desc.args[1] = req->status;
  676. desc.args[2] = req->sglistinfo_ptr;
  677. desc.args[3] = req->sglistinfo_len;
  678. } else {
  679. req_64 =
  680. (struct qseecom_client_listener_data_64bit_irsp *)
  681. req_buf;
  682. desc.args[0] = req_64->listener_id;
  683. desc.args[1] = req_64->status;
  684. desc.args[2] = req_64->sglistinfo_ptr;
  685. desc.args[3] = req_64->sglistinfo_len;
  686. }
  687. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  688. break;
  689. }
  690. case QSEOS_LOAD_EXTERNAL_ELF_COMMAND: {
  691. struct qseecom_load_app_ireq *req;
  692. struct qseecom_load_app_64bit_ireq *req_64bit;
  693. smc_id = TZ_OS_LOAD_EXTERNAL_IMAGE_ID;
  694. desc.arginfo = TZ_OS_LOAD_SERVICES_IMAGE_ID_PARAM_ID;
  695. if (qseecom.qsee_version < QSEE_VERSION_40) {
  696. req = (struct qseecom_load_app_ireq *)req_buf;
  697. desc.args[0] = req->mdt_len;
  698. desc.args[1] = req->img_len;
  699. desc.args[2] = req->phy_addr;
  700. } else {
  701. req_64bit =
  702. (struct qseecom_load_app_64bit_ireq *)req_buf;
  703. desc.args[0] = req_64bit->mdt_len;
  704. desc.args[1] = req_64bit->img_len;
  705. desc.args[2] = req_64bit->phy_addr;
  706. }
  707. __qseecom_reentrancy_check_if_no_app_blocked(smc_id);
  708. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  709. break;
  710. }
  711. case QSEOS_UNLOAD_EXTERNAL_ELF_COMMAND: {
  712. smc_id = TZ_OS_UNLOAD_EXTERNAL_IMAGE_ID;
  713. desc.arginfo = TZ_OS_UNLOAD_SERVICES_IMAGE_ID_PARAM_ID;
  714. __qseecom_reentrancy_check_if_no_app_blocked(smc_id);
  715. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  716. break;
  717. }
  718. case QSEOS_CLIENT_SEND_DATA_COMMAND: {
  719. struct qseecom_client_send_data_ireq *req;
  720. struct qseecom_client_send_data_64bit_ireq *req_64bit;
  721. smc_id = TZ_APP_QSAPP_SEND_DATA_ID;
  722. desc.arginfo = TZ_APP_QSAPP_SEND_DATA_ID_PARAM_ID;
  723. if (qseecom.qsee_version < QSEE_VERSION_40) {
  724. req = (struct qseecom_client_send_data_ireq *)
  725. req_buf;
  726. desc.args[0] = req->app_id;
  727. desc.args[1] = req->req_ptr;
  728. desc.args[2] = req->req_len;
  729. desc.args[3] = req->rsp_ptr;
  730. desc.args[4] = req->rsp_len;
  731. } else {
  732. req_64bit =
  733. (struct qseecom_client_send_data_64bit_ireq *)
  734. req_buf;
  735. desc.args[0] = req_64bit->app_id;
  736. desc.args[1] = req_64bit->req_ptr;
  737. desc.args[2] = req_64bit->req_len;
  738. desc.args[3] = req_64bit->rsp_ptr;
  739. desc.args[4] = req_64bit->rsp_len;
  740. }
  741. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  742. break;
  743. }
  744. case QSEOS_CLIENT_SEND_DATA_COMMAND_WHITELIST: {
  745. struct qseecom_client_send_data_ireq *req;
  746. struct qseecom_client_send_data_64bit_ireq *req_64bit;
  747. smc_id = TZ_APP_QSAPP_SEND_DATA_WITH_WHITELIST_ID;
  748. desc.arginfo =
  749. TZ_APP_QSAPP_SEND_DATA_WITH_WHITELIST_ID_PARAM_ID;
  750. if (qseecom.qsee_version < QSEE_VERSION_40) {
  751. req = (struct qseecom_client_send_data_ireq *)
  752. req_buf;
  753. desc.args[0] = req->app_id;
  754. desc.args[1] = req->req_ptr;
  755. desc.args[2] = req->req_len;
  756. desc.args[3] = req->rsp_ptr;
  757. desc.args[4] = req->rsp_len;
  758. desc.args[5] = req->sglistinfo_ptr;
  759. desc.args[6] = req->sglistinfo_len;
  760. } else {
  761. req_64bit =
  762. (struct qseecom_client_send_data_64bit_ireq *)
  763. req_buf;
  764. desc.args[0] = req_64bit->app_id;
  765. desc.args[1] = req_64bit->req_ptr;
  766. desc.args[2] = req_64bit->req_len;
  767. desc.args[3] = req_64bit->rsp_ptr;
  768. desc.args[4] = req_64bit->rsp_len;
  769. desc.args[5] = req_64bit->sglistinfo_ptr;
  770. desc.args[6] = req_64bit->sglistinfo_len;
  771. }
  772. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  773. break;
  774. }
  775. case QSEOS_RPMB_PROVISION_KEY_COMMAND: {
  776. struct qseecom_client_send_service_ireq *req;
  777. req = (struct qseecom_client_send_service_ireq *)
  778. req_buf;
  779. smc_id = TZ_OS_RPMB_PROVISION_KEY_ID;
  780. desc.arginfo = TZ_OS_RPMB_PROVISION_KEY_ID_PARAM_ID;
  781. desc.args[0] = req->key_type;
  782. __qseecom_reentrancy_check_if_no_app_blocked(smc_id);
  783. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  784. break;
  785. }
  786. case QSEOS_RPMB_ERASE_COMMAND: {
  787. smc_id = TZ_OS_RPMB_ERASE_ID;
  788. desc.arginfo = TZ_OS_RPMB_ERASE_ID_PARAM_ID;
  789. __qseecom_reentrancy_check_if_no_app_blocked(smc_id);
  790. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  791. break;
  792. }
  793. case QSEOS_RPMB_CHECK_PROV_STATUS_COMMAND: {
  794. smc_id = TZ_OS_RPMB_CHECK_PROV_STATUS_ID;
  795. desc.arginfo = TZ_OS_RPMB_CHECK_PROV_STATUS_ID_PARAM_ID;
  796. __qseecom_reentrancy_check_if_no_app_blocked(smc_id);
  797. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  798. break;
  799. }
  800. case QSEOS_DIAG_FUSE_REQ_CMD:
  801. case QSEOS_DIAG_FUSE_REQ_RSP_CMD: {
  802. struct qseecom_client_send_fsm_diag_req *req;
  803. smc_id = TZ_SECBOOT_GET_FUSE_INFO;
  804. desc.arginfo = TZ_SECBOOT_GET_FUSE_INFO_PARAM_ID;
  805. req = (struct qseecom_client_send_fsm_diag_req *) req_buf;
  806. desc.args[0] = req->req_ptr;
  807. desc.args[1] = req->req_len;
  808. desc.args[2] = req->rsp_ptr;
  809. desc.args[3] = req->rsp_len;
  810. __qseecom_reentrancy_check_if_no_app_blocked(smc_id);
  811. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  812. break;
  813. }
  814. case QSEOS_GENERATE_KEY: {
  815. u32 tzbuflen = PAGE_ALIGN(sizeof
  816. (struct qseecom_key_generate_ireq) -
  817. sizeof(uint32_t));
  818. char *tzbuf = __qseecom_alloc_tzbuf(
  819. tzbuflen, &pa, &shm);
  820. if (!tzbuf)
  821. return -ENOMEM;
  822. memset(tzbuf, 0, tzbuflen);
  823. memcpy(tzbuf, req_buf + sizeof(uint32_t),
  824. (sizeof(struct qseecom_key_generate_ireq) -
  825. sizeof(uint32_t)));
  826. qtee_shmbridge_flush_shm_buf(&shm);
  827. smc_id = TZ_OS_KS_GEN_KEY_ID;
  828. desc.arginfo = TZ_OS_KS_GEN_KEY_ID_PARAM_ID;
  829. desc.args[0] = pa;
  830. desc.args[1] = tzbuflen;
  831. __qseecom_reentrancy_check_if_no_app_blocked(smc_id);
  832. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  833. __qseecom_free_tzbuf(&shm);
  834. break;
  835. }
  836. case QSEOS_DELETE_KEY: {
  837. u32 tzbuflen = PAGE_ALIGN(sizeof
  838. (struct qseecom_key_delete_ireq) -
  839. sizeof(uint32_t));
  840. char *tzbuf = __qseecom_alloc_tzbuf(
  841. tzbuflen, &pa, &shm);
  842. if (!tzbuf)
  843. return -ENOMEM;
  844. memset(tzbuf, 0, tzbuflen);
  845. memcpy(tzbuf, req_buf + sizeof(uint32_t),
  846. (sizeof(struct qseecom_key_delete_ireq) -
  847. sizeof(uint32_t)));
  848. qtee_shmbridge_flush_shm_buf(&shm);
  849. smc_id = TZ_OS_KS_DEL_KEY_ID;
  850. desc.arginfo = TZ_OS_KS_DEL_KEY_ID_PARAM_ID;
  851. desc.args[0] = pa;
  852. desc.args[1] = tzbuflen;
  853. __qseecom_reentrancy_check_if_no_app_blocked(smc_id);
  854. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  855. __qseecom_free_tzbuf(&shm);
  856. break;
  857. }
  858. case QSEOS_SET_KEY: {
  859. u32 tzbuflen = PAGE_ALIGN(sizeof
  860. (struct qseecom_key_select_ireq) -
  861. sizeof(uint32_t));
  862. char *tzbuf = __qseecom_alloc_tzbuf(
  863. tzbuflen, &pa, &shm);
  864. if (!tzbuf)
  865. return -ENOMEM;
  866. memset(tzbuf, 0, tzbuflen);
  867. memcpy(tzbuf, req_buf + sizeof(uint32_t),
  868. (sizeof(struct qseecom_key_select_ireq) -
  869. sizeof(uint32_t)));
  870. qtee_shmbridge_flush_shm_buf(&shm);
  871. smc_id = TZ_OS_KS_SET_PIPE_KEY_ID;
  872. desc.arginfo = TZ_OS_KS_SET_PIPE_KEY_ID_PARAM_ID;
  873. desc.args[0] = pa;
  874. desc.args[1] = tzbuflen;
  875. __qseecom_reentrancy_check_if_no_app_blocked(smc_id);
  876. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  877. __qseecom_free_tzbuf(&shm);
  878. break;
  879. }
  880. case QSEOS_UPDATE_KEY_USERINFO: {
  881. u32 tzbuflen = PAGE_ALIGN(sizeof
  882. (struct qseecom_key_userinfo_update_ireq) -
  883. sizeof(uint32_t));
  884. char *tzbuf = __qseecom_alloc_tzbuf(
  885. tzbuflen, &pa, &shm);
  886. if (!tzbuf)
  887. return -ENOMEM;
  888. memset(tzbuf, 0, tzbuflen);
  889. memcpy(tzbuf, req_buf + sizeof(uint32_t), (sizeof
  890. (struct qseecom_key_userinfo_update_ireq) -
  891. sizeof(uint32_t)));
  892. qtee_shmbridge_flush_shm_buf(&shm);
  893. smc_id = TZ_OS_KS_UPDATE_KEY_ID;
  894. desc.arginfo = TZ_OS_KS_UPDATE_KEY_ID_PARAM_ID;
  895. desc.args[0] = pa;
  896. desc.args[1] = tzbuflen;
  897. __qseecom_reentrancy_check_if_no_app_blocked(smc_id);
  898. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  899. __qseecom_free_tzbuf(&shm);
  900. break;
  901. }
  902. case QSEOS_TEE_OPEN_SESSION: {
  903. struct qseecom_qteec_ireq *req;
  904. struct qseecom_qteec_64bit_ireq *req_64bit;
  905. smc_id = TZ_APP_GPAPP_OPEN_SESSION_ID;
  906. desc.arginfo = TZ_APP_GPAPP_OPEN_SESSION_ID_PARAM_ID;
  907. if (qseecom.qsee_version < QSEE_VERSION_40) {
  908. req = (struct qseecom_qteec_ireq *)req_buf;
  909. desc.args[0] = req->app_id;
  910. desc.args[1] = req->req_ptr;
  911. desc.args[2] = req->req_len;
  912. desc.args[3] = req->resp_ptr;
  913. desc.args[4] = req->resp_len;
  914. } else {
  915. req_64bit = (struct qseecom_qteec_64bit_ireq *)
  916. req_buf;
  917. desc.args[0] = req_64bit->app_id;
  918. desc.args[1] = req_64bit->req_ptr;
  919. desc.args[2] = req_64bit->req_len;
  920. desc.args[3] = req_64bit->resp_ptr;
  921. desc.args[4] = req_64bit->resp_len;
  922. }
  923. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  924. break;
  925. }
  926. case QSEOS_TEE_OPEN_SESSION_WHITELIST: {
  927. struct qseecom_qteec_ireq *req;
  928. struct qseecom_qteec_64bit_ireq *req_64bit;
  929. smc_id = TZ_APP_GPAPP_OPEN_SESSION_WITH_WHITELIST_ID;
  930. desc.arginfo =
  931. TZ_APP_GPAPP_OPEN_SESSION_WITH_WHITELIST_ID_PARAM_ID;
  932. if (qseecom.qsee_version < QSEE_VERSION_40) {
  933. req = (struct qseecom_qteec_ireq *)req_buf;
  934. desc.args[0] = req->app_id;
  935. desc.args[1] = req->req_ptr;
  936. desc.args[2] = req->req_len;
  937. desc.args[3] = req->resp_ptr;
  938. desc.args[4] = req->resp_len;
  939. desc.args[5] = req->sglistinfo_ptr;
  940. desc.args[6] = req->sglistinfo_len;
  941. } else {
  942. req_64bit = (struct qseecom_qteec_64bit_ireq *)
  943. req_buf;
  944. desc.args[0] = req_64bit->app_id;
  945. desc.args[1] = req_64bit->req_ptr;
  946. desc.args[2] = req_64bit->req_len;
  947. desc.args[3] = req_64bit->resp_ptr;
  948. desc.args[4] = req_64bit->resp_len;
  949. desc.args[5] = req_64bit->sglistinfo_ptr;
  950. desc.args[6] = req_64bit->sglistinfo_len;
  951. }
  952. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  953. break;
  954. }
  955. case QSEOS_TEE_INVOKE_COMMAND: {
  956. struct qseecom_qteec_ireq *req;
  957. struct qseecom_qteec_64bit_ireq *req_64bit;
  958. smc_id = TZ_APP_GPAPP_INVOKE_COMMAND_ID;
  959. desc.arginfo = TZ_APP_GPAPP_INVOKE_COMMAND_ID_PARAM_ID;
  960. if (qseecom.qsee_version < QSEE_VERSION_40) {
  961. req = (struct qseecom_qteec_ireq *)req_buf;
  962. desc.args[0] = req->app_id;
  963. desc.args[1] = req->req_ptr;
  964. desc.args[2] = req->req_len;
  965. desc.args[3] = req->resp_ptr;
  966. desc.args[4] = req->resp_len;
  967. } else {
  968. req_64bit = (struct qseecom_qteec_64bit_ireq *)
  969. req_buf;
  970. desc.args[0] = req_64bit->app_id;
  971. desc.args[1] = req_64bit->req_ptr;
  972. desc.args[2] = req_64bit->req_len;
  973. desc.args[3] = req_64bit->resp_ptr;
  974. desc.args[4] = req_64bit->resp_len;
  975. }
  976. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  977. break;
  978. }
  979. case QSEOS_TEE_INVOKE_COMMAND_WHITELIST: {
  980. struct qseecom_qteec_ireq *req;
  981. struct qseecom_qteec_64bit_ireq *req_64bit;
  982. smc_id = TZ_APP_GPAPP_INVOKE_COMMAND_WITH_WHITELIST_ID;
  983. desc.arginfo =
  984. TZ_APP_GPAPP_INVOKE_COMMAND_WITH_WHITELIST_ID_PARAM_ID;
  985. if (qseecom.qsee_version < QSEE_VERSION_40) {
  986. req = (struct qseecom_qteec_ireq *)req_buf;
  987. desc.args[0] = req->app_id;
  988. desc.args[1] = req->req_ptr;
  989. desc.args[2] = req->req_len;
  990. desc.args[3] = req->resp_ptr;
  991. desc.args[4] = req->resp_len;
  992. desc.args[5] = req->sglistinfo_ptr;
  993. desc.args[6] = req->sglistinfo_len;
  994. } else {
  995. req_64bit = (struct qseecom_qteec_64bit_ireq *)
  996. req_buf;
  997. desc.args[0] = req_64bit->app_id;
  998. desc.args[1] = req_64bit->req_ptr;
  999. desc.args[2] = req_64bit->req_len;
  1000. desc.args[3] = req_64bit->resp_ptr;
  1001. desc.args[4] = req_64bit->resp_len;
  1002. desc.args[5] = req_64bit->sglistinfo_ptr;
  1003. desc.args[6] = req_64bit->sglistinfo_len;
  1004. }
  1005. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  1006. break;
  1007. }
  1008. case QSEOS_TEE_CLOSE_SESSION: {
  1009. struct qseecom_qteec_ireq *req;
  1010. struct qseecom_qteec_64bit_ireq *req_64bit;
  1011. smc_id = TZ_APP_GPAPP_CLOSE_SESSION_ID;
  1012. desc.arginfo = TZ_APP_GPAPP_CLOSE_SESSION_ID_PARAM_ID;
  1013. if (qseecom.qsee_version < QSEE_VERSION_40) {
  1014. req = (struct qseecom_qteec_ireq *)req_buf;
  1015. desc.args[0] = req->app_id;
  1016. desc.args[1] = req->req_ptr;
  1017. desc.args[2] = req->req_len;
  1018. desc.args[3] = req->resp_ptr;
  1019. desc.args[4] = req->resp_len;
  1020. } else {
  1021. req_64bit = (struct qseecom_qteec_64bit_ireq *)
  1022. req_buf;
  1023. desc.args[0] = req_64bit->app_id;
  1024. desc.args[1] = req_64bit->req_ptr;
  1025. desc.args[2] = req_64bit->req_len;
  1026. desc.args[3] = req_64bit->resp_ptr;
  1027. desc.args[4] = req_64bit->resp_len;
  1028. }
  1029. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  1030. break;
  1031. }
  1032. case QSEOS_TEE_REQUEST_CANCELLATION: {
  1033. struct qseecom_qteec_ireq *req;
  1034. struct qseecom_qteec_64bit_ireq *req_64bit;
  1035. smc_id = TZ_APP_GPAPP_REQUEST_CANCELLATION_ID;
  1036. desc.arginfo =
  1037. TZ_APP_GPAPP_REQUEST_CANCELLATION_ID_PARAM_ID;
  1038. if (qseecom.qsee_version < QSEE_VERSION_40) {
  1039. req = (struct qseecom_qteec_ireq *)req_buf;
  1040. desc.args[0] = req->app_id;
  1041. desc.args[1] = req->req_ptr;
  1042. desc.args[2] = req->req_len;
  1043. desc.args[3] = req->resp_ptr;
  1044. desc.args[4] = req->resp_len;
  1045. } else {
  1046. req_64bit = (struct qseecom_qteec_64bit_ireq *)
  1047. req_buf;
  1048. desc.args[0] = req_64bit->app_id;
  1049. desc.args[1] = req_64bit->req_ptr;
  1050. desc.args[2] = req_64bit->req_len;
  1051. desc.args[3] = req_64bit->resp_ptr;
  1052. desc.args[4] = req_64bit->resp_len;
  1053. }
  1054. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  1055. break;
  1056. }
  1057. case QSEOS_CONTINUE_BLOCKED_REQ_COMMAND: {
  1058. struct qseecom_continue_blocked_request_ireq *req =
  1059. (struct qseecom_continue_blocked_request_ireq *)
  1060. req_buf;
  1061. if (qseecom.smcinvoke_support)
  1062. smc_id =
  1063. TZ_OS_CONTINUE_BLOCKED_REQUEST_SMCINVOKE_ID;
  1064. else
  1065. smc_id = TZ_OS_CONTINUE_BLOCKED_REQUEST_ID;
  1066. desc.arginfo =
  1067. TZ_OS_CONTINUE_BLOCKED_REQUEST_ID_PARAM_ID;
  1068. desc.args[0] = req->app_or_session_id;
  1069. ret = __qseecom_scm_call2_locked(smc_id, &desc);
  1070. break;
  1071. }
  1072. default: {
  1073. pr_err("qseos_cmd_id %d is not supported.\n",
  1074. qseos_cmd_id);
  1075. ret = -EINVAL;
  1076. break;
  1077. }
  1078. } /*end of switch (qsee_cmd_id) */
  1079. break;
  1080. } /*end of case SCM_SVC_TZSCHEDULER*/
  1081. default: {
  1082. pr_err("svc_id 0x%x is not supported.\n", svc_id);
  1083. ret = -EINVAL;
  1084. break;
  1085. }
  1086. } /*end of switch svc_id */
  1087. scm_resp->result = desc.ret[0];
  1088. scm_resp->resp_type = desc.ret[1];
  1089. scm_resp->data = desc.ret[2];
  1090. pr_debug("svc_id = 0x%x, tz_cmd_id = 0x%x, qseos_cmd_id = 0x%x, smc_id = 0x%x, param_id = 0x%x\n",
  1091. svc_id, tz_cmd_id, qseos_cmd_id, smc_id, desc.arginfo);
  1092. pr_debug("scm_resp->result = 0x%x, scm_resp->resp_type = 0x%x, scm_resp->data = 0x%x\n",
  1093. scm_resp->result, scm_resp->resp_type, scm_resp->data);
  1094. return ret;
  1095. }
  1096. static int qseecom_scm_call(u32 svc_id, u32 tz_cmd_id, const void *cmd_buf,
  1097. size_t cmd_len, void *resp_buf, size_t resp_len)
  1098. {
  1099. return qseecom_scm_call2(svc_id, tz_cmd_id, cmd_buf, resp_buf);
  1100. }
  1101. static struct qseecom_registered_listener_list *__qseecom_find_svc(
  1102. int32_t listener_id)
  1103. {
  1104. struct qseecom_registered_listener_list *entry = NULL;
  1105. list_for_each_entry(entry,
  1106. &qseecom.registered_listener_list_head, list) {
  1107. if (entry->svc.listener_id == listener_id)
  1108. break;
  1109. }
  1110. if ((entry != NULL) && (entry->svc.listener_id != listener_id)) {
  1111. pr_debug("Service id: %u is not found\n", listener_id);
  1112. return NULL;
  1113. }
  1114. return entry;
  1115. }
  1116. static int qseecom_dmabuf_cache_operations(struct dma_buf *dmabuf,
  1117. enum qseecom_cache_ops cache_op)
  1118. {
  1119. int ret = 0;
  1120. if (!dmabuf) {
  1121. pr_err("dmabuf is NULL\n");
  1122. ret = -EINVAL;
  1123. goto exit;
  1124. }
  1125. switch (cache_op) {
  1126. case QSEECOM_CACHE_CLEAN: /* Doing CLEAN and INVALIDATE */
  1127. dma_buf_begin_cpu_access(dmabuf, DMA_BIDIRECTIONAL);
  1128. dma_buf_end_cpu_access(dmabuf, DMA_BIDIRECTIONAL);
  1129. break;
  1130. case QSEECOM_CACHE_INVALIDATE:
  1131. dma_buf_begin_cpu_access(dmabuf, DMA_TO_DEVICE);
  1132. dma_buf_end_cpu_access(dmabuf, DMA_FROM_DEVICE);
  1133. break;
  1134. default:
  1135. pr_err("cache (%d) operation not supported\n",
  1136. cache_op);
  1137. ret = -EINVAL;
  1138. goto exit;
  1139. }
  1140. exit:
  1141. return ret;
  1142. }
  1143. static int qseecom_destroy_bridge_callback(void *dtor_data)
  1144. {
  1145. int ret = 0;
  1146. uint64_t handle = (uint64_t)dtor_data;
  1147. pr_debug("to destroy shm bridge %lld\n", handle);
  1148. ret = qtee_shmbridge_deregister(handle);
  1149. if (ret) {
  1150. pr_err("failed to destroy shm bridge %lld\n", handle);
  1151. return ret;
  1152. }
  1153. return ret;
  1154. }
  1155. static int qseecom_create_bridge_for_secbuf(int ion_fd, struct dma_buf *dmabuf,
  1156. struct sg_table *sgt)
  1157. {
  1158. int ret = 0;
  1159. phys_addr_t phys;
  1160. size_t size = 0;
  1161. uint64_t handle = 0;
  1162. int tz_perm = PERM_READ|PERM_WRITE;
  1163. uint32_t *vmid_list;
  1164. uint32_t *perms_list;
  1165. uint32_t nelems = 0;
  1166. struct scatterlist *sg = sgt->sgl;
  1167. if (!qtee_shmbridge_is_enabled())
  1168. return 0;
  1169. phys = sg_phys(sg);
  1170. size = sg->length;
  1171. ret = qtee_shmbridge_query(phys);
  1172. if (ret) {
  1173. pr_debug("bridge exists\n");
  1174. return 0;
  1175. }
  1176. if (mem_buf_dma_buf_exclusive_owner(dmabuf) || (sgt->nents != 1)) {
  1177. pr_debug("just create bridge for contiguous secure buf\n");
  1178. return 0;
  1179. }
  1180. ret = mem_buf_dma_buf_copy_vmperm(dmabuf, (int **)&vmid_list,
  1181. (int **)&perms_list, (int *)&nelems);
  1182. if (ret) {
  1183. pr_err("mem_buf_dma_buf_copy_vmperm failure, err=%d\n", ret);
  1184. return ret;
  1185. }
  1186. ret = qtee_shmbridge_register(phys, size, vmid_list, perms_list, nelems,
  1187. tz_perm, &handle);
  1188. if (ret && ret != -EEXIST) {
  1189. pr_err("creation of shm bridge failed with ret: %d\n",
  1190. ret);
  1191. goto exit;
  1192. }
  1193. pr_debug("created shm bridge %lld\n", handle);
  1194. mem_buf_dma_buf_set_destructor(dmabuf, qseecom_destroy_bridge_callback,
  1195. (void *)handle);
  1196. exit:
  1197. kfree(perms_list);
  1198. kfree(vmid_list);
  1199. return ret;
  1200. }
  1201. static int qseecom_dmabuf_map(int ion_fd, struct sg_table **sgt,
  1202. struct dma_buf_attachment **attach,
  1203. struct dma_buf **dmabuf)
  1204. {
  1205. struct dma_buf *new_dma_buf = NULL;
  1206. struct dma_buf_attachment *new_attach = NULL;
  1207. struct sg_table *new_sgt = NULL;
  1208. int ret = 0;
  1209. new_dma_buf = dma_buf_get(ion_fd);
  1210. if (IS_ERR_OR_NULL(new_dma_buf)) {
  1211. pr_err("dma_buf_get() for ion_fd %d failed\n", ion_fd);
  1212. ret = -ENOMEM;
  1213. goto err;
  1214. }
  1215. new_attach = dma_buf_attach(new_dma_buf, qseecom.dev);
  1216. if (IS_ERR_OR_NULL(new_attach)) {
  1217. pr_err("dma_buf_attach() for ion_fd %d failed\n", ion_fd);
  1218. ret = -ENOMEM;
  1219. goto err_put;
  1220. }
  1221. new_sgt = dma_buf_map_attachment(new_attach, DMA_BIDIRECTIONAL);
  1222. if (IS_ERR_OR_NULL(new_sgt)) {
  1223. ret = PTR_ERR(new_sgt);
  1224. pr_err("dma_buf_map_attachment for ion_fd %d failed ret = %d\n",
  1225. ion_fd, ret);
  1226. goto err_detach;
  1227. }
  1228. ret = qseecom_create_bridge_for_secbuf(ion_fd, new_dma_buf, new_sgt);
  1229. if (ret) {
  1230. pr_err("failed to create bridge for fd %d\n", ion_fd);
  1231. goto err_unmap_attachment;
  1232. }
  1233. *sgt = new_sgt;
  1234. *attach = new_attach;
  1235. *dmabuf = new_dma_buf;
  1236. return ret;
  1237. err_unmap_attachment:
  1238. dma_buf_unmap_attachment(new_attach, new_sgt, DMA_BIDIRECTIONAL);
  1239. err_detach:
  1240. dma_buf_detach(new_dma_buf, new_attach);
  1241. err_put:
  1242. dma_buf_put(new_dma_buf);
  1243. err:
  1244. return ret;
  1245. }
  1246. static void qseecom_dmabuf_unmap(struct sg_table *sgt,
  1247. struct dma_buf_attachment *attach,
  1248. struct dma_buf *dmabuf)
  1249. {
  1250. dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
  1251. dma_buf_detach(dmabuf, attach);
  1252. dma_buf_put(dmabuf);
  1253. }
  1254. /* convert ion_fd to phys_adds and virt_addr*/
  1255. static int qseecom_vaddr_map(int ion_fd,
  1256. phys_addr_t *paddr, void **vaddr,
  1257. struct sg_table **sgt,
  1258. struct dma_buf_attachment **attach,
  1259. size_t *sb_length, struct dma_buf **dmabuf)
  1260. {
  1261. struct dma_buf *new_dma_buf = NULL;
  1262. struct dma_buf_attachment *new_attach = NULL;
  1263. #ifdef KERNEL_VERSION_LEGACY
  1264. struct dma_buf_map new_dma_buf_map = {0};
  1265. #else
  1266. struct iosys_map new_dma_buf_map = {0};
  1267. #endif
  1268. struct sg_table *new_sgt = NULL;
  1269. void *new_va = NULL;
  1270. int ret = 0;
  1271. ret = qseecom_dmabuf_map(ion_fd, &new_sgt, &new_attach, &new_dma_buf);
  1272. if (ret) {
  1273. pr_err("qseecom_dmabuf_map for ion_fd %d failed ret = %d\n",
  1274. ion_fd, ret);
  1275. goto err;
  1276. }
  1277. ret = 0;
  1278. *paddr = sg_dma_address(new_sgt->sgl);
  1279. *sb_length = new_sgt->sgl->length;
  1280. dma_buf_begin_cpu_access(new_dma_buf, DMA_BIDIRECTIONAL);
  1281. ret = dma_buf_vmap(new_dma_buf, &new_dma_buf_map);
  1282. new_va = ret ? NULL : new_dma_buf_map.vaddr;
  1283. if (!new_va) {
  1284. pr_err("dma_buf_vmap failed\n");
  1285. ret = -ENOMEM;
  1286. goto err_unmap;
  1287. }
  1288. *dmabuf = new_dma_buf;
  1289. *attach = new_attach;
  1290. *sgt = new_sgt;
  1291. *vaddr = new_va;
  1292. return ret;
  1293. err_unmap:
  1294. dma_buf_end_cpu_access(new_dma_buf, DMA_BIDIRECTIONAL);
  1295. qseecom_dmabuf_unmap(new_sgt, new_attach, new_dma_buf);
  1296. MAKE_NULL(*sgt, *attach, *dmabuf);
  1297. err:
  1298. return ret;
  1299. }
  1300. static void qseecom_vaddr_unmap(void *vaddr, struct sg_table *sgt,
  1301. struct dma_buf_attachment *attach,
  1302. struct dma_buf *dmabuf)
  1303. {
  1304. #ifdef KERNEL_VERSION_LEGACY
  1305. struct dma_buf_map dmabufmap = DMA_BUF_MAP_INIT_VADDR(vaddr);
  1306. #else
  1307. struct iosys_map dmabufmap = IOSYS_MAP_INIT_VADDR(vaddr);
  1308. #endif
  1309. if (!dmabuf || !vaddr || !sgt || !attach)
  1310. return;
  1311. pr_err("Trying to unmap vaddr");
  1312. dma_buf_vunmap(dmabuf, &dmabufmap);
  1313. dma_buf_end_cpu_access(dmabuf, DMA_BIDIRECTIONAL);
  1314. qseecom_dmabuf_unmap(sgt, attach, dmabuf);
  1315. }
  1316. static int __qseecom_set_sb_memory(struct qseecom_registered_listener_list *svc,
  1317. struct qseecom_dev_handle *handle,
  1318. struct qseecom_register_listener_req *listener)
  1319. {
  1320. int ret = 0;
  1321. struct qseecom_register_listener_ireq req;
  1322. struct qseecom_register_listener_64bit_ireq req_64bit;
  1323. struct qseecom_command_scm_resp resp;
  1324. void *cmd_buf = NULL;
  1325. size_t cmd_len;
  1326. ret = qseecom_vaddr_map(listener->ifd_data_fd,
  1327. &svc->sb_phys, (void **)&svc->sb_virt,
  1328. &svc->sgt, &svc->attach,
  1329. &svc->sb_length, &svc->dmabuf);
  1330. if (ret) {
  1331. pr_err("failed to convert ion_fd %d for lsnr %d with err: %d\n",
  1332. listener->ifd_data_fd, svc->svc.listener_id, ret);
  1333. return -EINVAL;
  1334. }
  1335. if (qseecom.qsee_version < QSEE_VERSION_40) {
  1336. req.qsee_cmd_id = QSEOS_REGISTER_LISTENER;
  1337. req.listener_id = svc->svc.listener_id;
  1338. req.sb_len = svc->sb_length;
  1339. req.sb_ptr = (uint32_t)svc->sb_phys;
  1340. cmd_buf = (void *)&req;
  1341. cmd_len = sizeof(struct qseecom_register_listener_ireq);
  1342. } else {
  1343. req_64bit.qsee_cmd_id = QSEOS_REGISTER_LISTENER;
  1344. req_64bit.listener_id = svc->svc.listener_id;
  1345. req_64bit.sb_len = svc->sb_length;
  1346. req_64bit.sb_ptr = (uint64_t)svc->sb_phys;
  1347. cmd_buf = (void *)&req_64bit;
  1348. cmd_len = sizeof(struct qseecom_register_listener_64bit_ireq);
  1349. }
  1350. resp.result = QSEOS_RESULT_INCOMPLETE;
  1351. mutex_unlock(&listener_access_lock);
  1352. mutex_lock(&app_access_lock);
  1353. __qseecom_reentrancy_check_if_no_app_blocked(
  1354. TZ_OS_REGISTER_LISTENER_SMCINVOKE_ID);
  1355. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1, cmd_buf, cmd_len,
  1356. &resp, sizeof(resp));
  1357. mutex_unlock(&app_access_lock);
  1358. mutex_lock(&listener_access_lock);
  1359. if (ret) {
  1360. pr_err("qseecom_scm_call failed with err: %d\n", ret);
  1361. ret = -EINVAL;
  1362. goto err;
  1363. }
  1364. if (resp.result != QSEOS_RESULT_SUCCESS) {
  1365. pr_err("Error SB registration req: resp.result = %d\n",
  1366. resp.result);
  1367. ret = -EPERM;
  1368. goto err;
  1369. }
  1370. return 0;
  1371. err:
  1372. if (svc->dmabuf) {
  1373. qseecom_vaddr_unmap(svc->sb_virt, svc->sgt, svc->attach,
  1374. svc->dmabuf);
  1375. MAKE_NULL(svc->sgt, svc->attach, svc->dmabuf);
  1376. }
  1377. return ret;
  1378. }
  1379. static int qseecom_register_listener(struct qseecom_dev_handle *data,
  1380. void __user *argp)
  1381. {
  1382. int ret = 0;
  1383. struct qseecom_register_listener_req rcvd_lstnr;
  1384. struct qseecom_registered_listener_list *new_entry;
  1385. struct qseecom_registered_listener_list *ptr_svc;
  1386. ret = copy_from_user(&rcvd_lstnr, argp, sizeof(rcvd_lstnr));
  1387. if (ret) {
  1388. pr_err("copy_from_user failed\n");
  1389. return ret;
  1390. }
  1391. if (!access_ok((void __user *)rcvd_lstnr.virt_sb_base,
  1392. rcvd_lstnr.sb_size))
  1393. return -EFAULT;
  1394. ptr_svc = __qseecom_find_svc(rcvd_lstnr.listener_id);
  1395. if (ptr_svc) {
  1396. if (!ptr_svc->unregister_pending) {
  1397. pr_err("Service %d is not unique\n",
  1398. rcvd_lstnr.listener_id);
  1399. data->released = true;
  1400. return -EBUSY;
  1401. } else {
  1402. /*wait until listener is unregistered*/
  1403. pr_debug("register %d has to wait\n",
  1404. rcvd_lstnr.listener_id);
  1405. mutex_unlock(&listener_access_lock);
  1406. ret = wait_event_interruptible(
  1407. qseecom.register_lsnr_pending_wq,
  1408. list_empty(
  1409. &qseecom.unregister_lsnr_pending_list_head));
  1410. if (ret) {
  1411. pr_err("interrupted register_pending_wq %d\n",
  1412. rcvd_lstnr.listener_id);
  1413. mutex_lock(&listener_access_lock);
  1414. return -ERESTARTSYS;
  1415. }
  1416. mutex_lock(&listener_access_lock);
  1417. }
  1418. }
  1419. new_entry = kzalloc(sizeof(*new_entry), GFP_KERNEL);
  1420. if (!new_entry)
  1421. return -ENOMEM;
  1422. memcpy(&new_entry->svc, &rcvd_lstnr, sizeof(rcvd_lstnr));
  1423. new_entry->rcv_req_flag = 0;
  1424. new_entry->sglistinfo_ptr =
  1425. (struct sglist_info *)__qseecom_alloc_tzbuf(
  1426. sizeof(struct sglist_info) * MAX_ION_FD,
  1427. &new_entry->sglistinfo_shm.paddr,
  1428. &new_entry->sglistinfo_shm);
  1429. if (!new_entry->sglistinfo_ptr) {
  1430. kfree(new_entry);
  1431. return -ENOMEM;
  1432. }
  1433. new_entry->svc.listener_id = rcvd_lstnr.listener_id;
  1434. new_entry->sb_length = rcvd_lstnr.sb_size;
  1435. new_entry->user_virt_sb_base = rcvd_lstnr.virt_sb_base;
  1436. if (__qseecom_set_sb_memory(new_entry, data, &rcvd_lstnr)) {
  1437. pr_err("qseecom_set_sb_memory failed for listener %d, size %d\n",
  1438. rcvd_lstnr.listener_id, rcvd_lstnr.sb_size);
  1439. __qseecom_free_tzbuf(&new_entry->sglistinfo_shm);
  1440. kfree_sensitive(new_entry);
  1441. return -ENOMEM;
  1442. }
  1443. init_waitqueue_head(&new_entry->rcv_req_wq);
  1444. init_waitqueue_head(&new_entry->listener_block_app_wq);
  1445. new_entry->send_resp_flag = 0;
  1446. new_entry->listener_in_use = false;
  1447. list_add_tail(&new_entry->list, &qseecom.registered_listener_list_head);
  1448. data->listener.id = rcvd_lstnr.listener_id;
  1449. pr_debug("Service %d is registered\n", rcvd_lstnr.listener_id);
  1450. return ret;
  1451. }
  1452. static int __qseecom_unregister_listener(struct qseecom_dev_handle *data,
  1453. struct qseecom_registered_listener_list *ptr_svc)
  1454. {
  1455. int ret = 0;
  1456. struct qseecom_register_listener_ireq req;
  1457. struct qseecom_command_scm_resp resp;
  1458. req.qsee_cmd_id = QSEOS_DEREGISTER_LISTENER;
  1459. req.listener_id = data->listener.id;
  1460. resp.result = QSEOS_RESULT_INCOMPLETE;
  1461. mutex_unlock(&listener_access_lock);
  1462. mutex_lock(&app_access_lock);
  1463. __qseecom_reentrancy_check_if_no_app_blocked(
  1464. TZ_OS_DEREGISTER_LISTENER_ID);
  1465. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1, &req,
  1466. sizeof(req), &resp, sizeof(resp));
  1467. mutex_unlock(&app_access_lock);
  1468. mutex_lock(&listener_access_lock);
  1469. if (ret) {
  1470. pr_err("scm_call() failed with err: %d (lstnr id=%d)\n",
  1471. ret, data->listener.id);
  1472. return ret;
  1473. }
  1474. if (resp.result != QSEOS_RESULT_SUCCESS) {
  1475. pr_err("Failed resp.result=%d,(lstnr id=%d)\n",
  1476. resp.result, data->listener.id);
  1477. ret = -EPERM;
  1478. goto exit;
  1479. }
  1480. while (atomic_read(&data->ioctl_count) > 1) {
  1481. if (wait_event_interruptible(data->abort_wq,
  1482. atomic_read(&data->ioctl_count) <= 1)) {
  1483. pr_err("Interrupted from abort\n");
  1484. ret = -ERESTARTSYS;
  1485. }
  1486. }
  1487. exit:
  1488. if (ptr_svc->dmabuf) {
  1489. qseecom_vaddr_unmap(ptr_svc->sb_virt,
  1490. ptr_svc->sgt, ptr_svc->attach, ptr_svc->dmabuf);
  1491. MAKE_NULL(ptr_svc->sgt, ptr_svc->attach, ptr_svc->dmabuf);
  1492. }
  1493. __qseecom_free_tzbuf(&ptr_svc->sglistinfo_shm);
  1494. list_del(&ptr_svc->list);
  1495. kfree_sensitive(ptr_svc);
  1496. data->released = true;
  1497. pr_debug("Service %d is unregistered\n", data->listener.id);
  1498. return ret;
  1499. }
  1500. static int qseecom_unregister_listener(struct qseecom_dev_handle *data)
  1501. {
  1502. struct qseecom_registered_listener_list *ptr_svc = NULL;
  1503. struct qseecom_unregister_pending_list *entry = NULL;
  1504. if (data->released) {
  1505. pr_err("Don't unregister lsnr %d\n", data->listener.id);
  1506. return -EINVAL;
  1507. }
  1508. ptr_svc = __qseecom_find_svc(data->listener.id);
  1509. if (!ptr_svc) {
  1510. pr_err("Unregiser invalid listener ID %d\n", data->listener.id);
  1511. return -ENODATA;
  1512. }
  1513. /* stop CA thread waiting for listener response */
  1514. ptr_svc->abort = 1;
  1515. wake_up_interruptible_all(&qseecom.send_resp_wq);
  1516. /* stop listener thread waiting for listener request */
  1517. data->abort = 1;
  1518. wake_up_all(&ptr_svc->rcv_req_wq);
  1519. /* return directly if pending*/
  1520. if (ptr_svc->unregister_pending)
  1521. return 0;
  1522. /*add unregistration into pending list*/
  1523. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  1524. if (!entry)
  1525. return -ENOMEM;
  1526. entry->data = data;
  1527. list_add_tail(&entry->list,
  1528. &qseecom.unregister_lsnr_pending_list_head);
  1529. ptr_svc->unregister_pending = true;
  1530. pr_debug("unregister %d pending\n", data->listener.id);
  1531. return 0;
  1532. }
  1533. static void __qseecom_processing_pending_lsnr_unregister(void)
  1534. {
  1535. struct qseecom_unregister_pending_list *entry = NULL;
  1536. struct qseecom_registered_listener_list *ptr_svc = NULL;
  1537. struct list_head *pos;
  1538. int ret = 0;
  1539. mutex_lock(&listener_access_lock);
  1540. while (!list_empty(&qseecom.unregister_lsnr_pending_list_head)) {
  1541. pos = qseecom.unregister_lsnr_pending_list_head.next;
  1542. entry = list_entry(pos,
  1543. struct qseecom_unregister_pending_list, list);
  1544. if (entry && entry->data) {
  1545. pr_debug("process pending unregister %d\n",
  1546. entry->data->listener.id);
  1547. /* don't process the entry if qseecom_release is not called*/
  1548. if (!entry->data->listener.release_called) {
  1549. list_del(pos);
  1550. list_add_tail(&entry->list,
  1551. &qseecom.unregister_lsnr_pending_list_head);
  1552. break;
  1553. }
  1554. ptr_svc = __qseecom_find_svc(
  1555. entry->data->listener.id);
  1556. if (ptr_svc) {
  1557. ret = __qseecom_unregister_listener(
  1558. entry->data, ptr_svc);
  1559. if (ret) {
  1560. pr_debug("unregister %d pending again\n",
  1561. entry->data->listener.id);
  1562. mutex_unlock(&listener_access_lock);
  1563. return;
  1564. }
  1565. } else
  1566. pr_err("invalid listener %d\n",
  1567. entry->data->listener.id);
  1568. __qseecom_free_tzbuf(&entry->data->sglistinfo_shm);
  1569. kfree_sensitive(entry->data);
  1570. }
  1571. list_del(pos);
  1572. kfree_sensitive(entry);
  1573. }
  1574. mutex_unlock(&listener_access_lock);
  1575. wake_up_interruptible(&qseecom.register_lsnr_pending_wq);
  1576. }
  1577. static void __wakeup_unregister_listener_kthread(void)
  1578. {
  1579. atomic_set(&qseecom.unregister_lsnr_kthread_state,
  1580. LSNR_UNREG_KT_WAKEUP);
  1581. wake_up_interruptible(&qseecom.unregister_lsnr_kthread_wq);
  1582. }
  1583. static int __qseecom_unregister_listener_kthread_func(void *data)
  1584. {
  1585. while (!kthread_should_stop()) {
  1586. wait_event_interruptible(
  1587. qseecom.unregister_lsnr_kthread_wq,
  1588. atomic_read(&qseecom.unregister_lsnr_kthread_state)
  1589. == LSNR_UNREG_KT_WAKEUP);
  1590. pr_debug("kthread to unregister listener is called %d\n",
  1591. atomic_read(&qseecom.unregister_lsnr_kthread_state));
  1592. __qseecom_processing_pending_lsnr_unregister();
  1593. atomic_set(&qseecom.unregister_lsnr_kthread_state,
  1594. LSNR_UNREG_KT_SLEEP);
  1595. }
  1596. pr_warn("kthread to unregister listener stopped\n");
  1597. return 0;
  1598. }
  1599. static int qseecom_bus_scale_update_request(
  1600. int client, int mode)
  1601. {
  1602. pr_debug("client %d, mode %d\n", client, mode);
  1603. /*TODO: get ab/ib from device tree for different mode*/
  1604. if (!mode)
  1605. return icc_set_bw(qseecom.icc_path, 0, 0);
  1606. else
  1607. return icc_set_bw(qseecom.icc_path,
  1608. qseecom.avg_bw, qseecom.peak_bw);
  1609. }
  1610. static int __qseecom_set_msm_bus_request(uint32_t mode)
  1611. {
  1612. int ret = 0;
  1613. struct qseecom_clk *qclk;
  1614. qclk = &qseecom.qsee;
  1615. if (qclk->ce_core_src_clk != NULL) {
  1616. if (mode == INACTIVE) {
  1617. __qseecom_disable_clk(CLK_QSEE);
  1618. } else {
  1619. ret = __qseecom_enable_clk(CLK_QSEE);
  1620. if (ret)
  1621. pr_err("CLK enabling failed (%d) MODE (%d)\n",
  1622. ret, mode);
  1623. }
  1624. }
  1625. if ((!ret) && (qseecom.current_mode != mode)) {
  1626. ret = qseecom_bus_scale_update_request(
  1627. qseecom.qsee_perf_client, mode);
  1628. if (ret) {
  1629. pr_err("Bandwidth req failed(%d) MODE (%d)\n",
  1630. ret, mode);
  1631. if (qclk->ce_core_src_clk != NULL) {
  1632. if (mode == INACTIVE) {
  1633. ret = __qseecom_enable_clk(CLK_QSEE);
  1634. if (ret)
  1635. pr_err("CLK enable failed\n");
  1636. } else
  1637. __qseecom_disable_clk(CLK_QSEE);
  1638. }
  1639. }
  1640. qseecom.current_mode = mode;
  1641. }
  1642. return ret;
  1643. }
  1644. static void qseecom_bw_inactive_req_work(struct work_struct *work)
  1645. {
  1646. mutex_lock(&app_access_lock);
  1647. mutex_lock(&qsee_bw_mutex);
  1648. if (qseecom.timer_running)
  1649. __qseecom_set_msm_bus_request(INACTIVE);
  1650. pr_debug("current_mode = %d, cumulative_mode = %d\n",
  1651. qseecom.current_mode, qseecom.cumulative_mode);
  1652. qseecom.timer_running = false;
  1653. mutex_unlock(&qsee_bw_mutex);
  1654. mutex_unlock(&app_access_lock);
  1655. }
  1656. static void qseecom_scale_bus_bandwidth_timer_callback(struct timer_list *data)
  1657. {
  1658. schedule_work(&qseecom.bw_inactive_req_ws);
  1659. }
  1660. static int __qseecom_decrease_clk_ref_count(enum qseecom_ce_hw_instance ce)
  1661. {
  1662. struct qseecom_clk *qclk;
  1663. int ret = 0;
  1664. mutex_lock(&clk_access_lock);
  1665. if (ce == CLK_QSEE)
  1666. qclk = &qseecom.qsee;
  1667. else
  1668. qclk = &qseecom.ce_drv;
  1669. if (qclk->clk_access_cnt > 0) {
  1670. qclk->clk_access_cnt--;
  1671. } else {
  1672. pr_err("Invalid clock ref count %d\n", qclk->clk_access_cnt);
  1673. ret = -EINVAL;
  1674. }
  1675. mutex_unlock(&clk_access_lock);
  1676. return ret;
  1677. }
  1678. static int qseecom_scale_bus_bandwidth_timer(uint32_t mode)
  1679. {
  1680. int32_t ret = 0;
  1681. int32_t request_mode = INACTIVE;
  1682. mutex_lock(&qsee_bw_mutex);
  1683. if (mode == 0) {
  1684. if (qseecom.cumulative_mode > MEDIUM)
  1685. request_mode = HIGH;
  1686. else
  1687. request_mode = qseecom.cumulative_mode;
  1688. } else {
  1689. request_mode = mode;
  1690. }
  1691. ret = __qseecom_set_msm_bus_request(request_mode);
  1692. if (ret) {
  1693. pr_err("set msm bus request failed (%d),request_mode (%d)\n",
  1694. ret, request_mode);
  1695. goto err_scale_timer;
  1696. }
  1697. if (qseecom.timer_running) {
  1698. ret = __qseecom_decrease_clk_ref_count(CLK_QSEE);
  1699. if (ret) {
  1700. pr_err("Failed to decrease clk ref count.\n");
  1701. goto err_scale_timer;
  1702. }
  1703. del_timer_sync(&(qseecom.bw_scale_down_timer));
  1704. qseecom.timer_running = false;
  1705. }
  1706. err_scale_timer:
  1707. mutex_unlock(&qsee_bw_mutex);
  1708. return ret;
  1709. }
  1710. static int qseecom_unregister_bus_bandwidth_needs(
  1711. struct qseecom_dev_handle *data)
  1712. {
  1713. qseecom.cumulative_mode -= data->mode;
  1714. data->mode = INACTIVE;
  1715. return 0;
  1716. }
  1717. static int __qseecom_register_bus_bandwidth_needs(
  1718. struct qseecom_dev_handle *data, uint32_t request_mode)
  1719. {
  1720. if (data->mode == INACTIVE) {
  1721. qseecom.cumulative_mode += request_mode;
  1722. data->mode = request_mode;
  1723. } else {
  1724. if (data->mode != request_mode) {
  1725. qseecom.cumulative_mode -= data->mode;
  1726. qseecom.cumulative_mode += request_mode;
  1727. data->mode = request_mode;
  1728. }
  1729. }
  1730. return 0;
  1731. }
  1732. static int qseecom_perf_enable(struct qseecom_dev_handle *data)
  1733. {
  1734. int ret = 0;
  1735. ret = qsee_vote_for_clock(data, CLK_DFAB);
  1736. if (ret) {
  1737. pr_err("Failed to vote for DFAB clock with err %d\n", ret);
  1738. goto perf_enable_exit;
  1739. }
  1740. ret = qsee_vote_for_clock(data, CLK_SFPB);
  1741. if (ret) {
  1742. qsee_disable_clock_vote(data, CLK_DFAB);
  1743. pr_err("Failed to vote for SFPB clock with err %d\n", ret);
  1744. goto perf_enable_exit;
  1745. }
  1746. perf_enable_exit:
  1747. return ret;
  1748. }
  1749. static int qseecom_scale_bus_bandwidth(struct qseecom_dev_handle *data,
  1750. void __user *argp)
  1751. {
  1752. int32_t ret = 0;
  1753. int32_t req_mode;
  1754. if (qseecom.no_clock_support)
  1755. return 0;
  1756. ret = copy_from_user(&req_mode, argp, sizeof(req_mode));
  1757. if (ret) {
  1758. pr_err("copy_from_user failed\n");
  1759. return ret;
  1760. }
  1761. if (req_mode > HIGH) {
  1762. pr_err("Invalid bandwidth mode (%d)\n", req_mode);
  1763. return -EINVAL;
  1764. }
  1765. /*
  1766. * Register bus bandwidth needs if bus scaling feature is enabled;
  1767. * otherwise, qseecom enable/disable clocks for the client directly.
  1768. */
  1769. if (qseecom.support_bus_scaling) {
  1770. mutex_lock(&qsee_bw_mutex);
  1771. ret = __qseecom_register_bus_bandwidth_needs(data, req_mode);
  1772. mutex_unlock(&qsee_bw_mutex);
  1773. } else {
  1774. pr_debug("Bus scaling feature is NOT enabled\n");
  1775. pr_debug("request bandwidth mode %d for the client\n",
  1776. req_mode);
  1777. if (req_mode != INACTIVE) {
  1778. ret = qseecom_perf_enable(data);
  1779. if (ret)
  1780. pr_err("Failed to vote for clock with err %d\n",
  1781. ret);
  1782. } else {
  1783. qsee_disable_clock_vote(data, CLK_DFAB);
  1784. qsee_disable_clock_vote(data, CLK_SFPB);
  1785. }
  1786. }
  1787. return ret;
  1788. }
  1789. static void __qseecom_add_bw_scale_down_timer(uint32_t duration)
  1790. {
  1791. if (qseecom.no_clock_support)
  1792. return;
  1793. mutex_lock(&qsee_bw_mutex);
  1794. qseecom.bw_scale_down_timer.expires = jiffies +
  1795. msecs_to_jiffies(duration);
  1796. mod_timer(&(qseecom.bw_scale_down_timer),
  1797. qseecom.bw_scale_down_timer.expires);
  1798. qseecom.timer_running = true;
  1799. mutex_unlock(&qsee_bw_mutex);
  1800. }
  1801. static void __qseecom_disable_clk_scale_down(struct qseecom_dev_handle *data)
  1802. {
  1803. if (!qseecom.support_bus_scaling)
  1804. qsee_disable_clock_vote(data, CLK_SFPB);
  1805. else
  1806. __qseecom_add_bw_scale_down_timer(
  1807. QSEECOM_LOAD_APP_CRYPTO_TIMEOUT);
  1808. }
  1809. static int __qseecom_enable_clk_scale_up(struct qseecom_dev_handle *data)
  1810. {
  1811. int ret = 0;
  1812. if (qseecom.support_bus_scaling) {
  1813. ret = qseecom_scale_bus_bandwidth_timer(MEDIUM);
  1814. if (ret)
  1815. pr_err("Failed to set bw MEDIUM.\n");
  1816. } else {
  1817. ret = qsee_vote_for_clock(data, CLK_SFPB);
  1818. if (ret)
  1819. pr_err("Fail vote for clk SFPB ret %d\n", ret);
  1820. }
  1821. return ret;
  1822. }
  1823. static int qseecom_set_client_mem_param(struct qseecom_dev_handle *data,
  1824. void __user *argp)
  1825. {
  1826. int32_t ret;
  1827. struct qseecom_set_sb_mem_param_req req;
  1828. size_t len;
  1829. /* Copy the relevant information needed for loading the image */
  1830. if (copy_from_user(&req, (void __user *)argp, sizeof(req)))
  1831. return -EFAULT;
  1832. if ((req.ifd_data_fd <= 0) || (req.virt_sb_base == NULL) ||
  1833. (req.sb_len == 0)) {
  1834. pr_err("Invalid input(s)ion_fd(%d), sb_len(%d), vaddr(0x%pK)\n",
  1835. req.ifd_data_fd, req.sb_len, req.virt_sb_base);
  1836. return -EFAULT;
  1837. }
  1838. if (!access_ok((void __user *)req.virt_sb_base,
  1839. req.sb_len))
  1840. return -EFAULT;
  1841. ret = qseecom_vaddr_map(req.ifd_data_fd, &data->client.sb_phys,
  1842. (void **)&data->client.sb_virt,
  1843. &data->client.sgt, &data->client.attach,
  1844. &len, &data->client.dmabuf);
  1845. if (ret) {
  1846. pr_err("failed to convert ion_fd %d for lsnr %d with err: %d\n",
  1847. req.ifd_data_fd, data->client.app_id, ret);
  1848. return -EINVAL;
  1849. }
  1850. if (len < req.sb_len) {
  1851. pr_err("Requested length (0x%x) is > allocated (%zu)\n",
  1852. req.sb_len, len);
  1853. ret = -EINVAL;
  1854. goto exit;
  1855. }
  1856. data->client.sb_length = req.sb_len;
  1857. data->client.user_virt_sb_base = (uintptr_t)req.virt_sb_base;
  1858. return ret;
  1859. exit:
  1860. if (data->client.dmabuf) {
  1861. qseecom_vaddr_unmap(data->client.sb_virt, data->client.sgt,
  1862. data->client.attach, data->client.dmabuf);
  1863. MAKE_NULL(data->client.sgt,
  1864. data->client.attach, data->client.dmabuf);
  1865. }
  1866. return ret;
  1867. }
  1868. static int __qseecom_listener_has_sent_rsp(struct qseecom_dev_handle *data,
  1869. struct qseecom_registered_listener_list *ptr_svc)
  1870. {
  1871. int ret;
  1872. ret = (qseecom.send_resp_flag != 0);
  1873. return ret || data->abort || ptr_svc->abort;
  1874. }
  1875. static int __qseecom_reentrancy_listener_has_sent_rsp(
  1876. struct qseecom_dev_handle *data,
  1877. struct qseecom_registered_listener_list *ptr_svc)
  1878. {
  1879. int ret;
  1880. ret = (ptr_svc->send_resp_flag != 0);
  1881. return ret || data->abort || ptr_svc->abort;
  1882. }
  1883. static void __qseecom_clean_listener_sglistinfo(
  1884. struct qseecom_registered_listener_list *ptr_svc)
  1885. {
  1886. if (ptr_svc->sglist_cnt) {
  1887. memset(ptr_svc->sglistinfo_ptr, 0,
  1888. SGLISTINFO_TABLE_SIZE);
  1889. ptr_svc->sglist_cnt = 0;
  1890. }
  1891. }
  1892. static int __qseecom_process_incomplete_cmd(struct qseecom_dev_handle *data,
  1893. struct qseecom_command_scm_resp *resp)
  1894. {
  1895. int ret = 0;
  1896. int rc = 0;
  1897. uint32_t lstnr;
  1898. struct qseecom_client_listener_data_irsp send_data_rsp = {0};
  1899. struct qseecom_client_listener_data_64bit_irsp send_data_rsp_64bit
  1900. = {0};
  1901. struct qseecom_registered_listener_list *ptr_svc = NULL;
  1902. sigset_t new_sigset;
  1903. uint32_t status;
  1904. void *cmd_buf = NULL;
  1905. size_t cmd_len;
  1906. struct sglist_info *table = NULL;
  1907. qseecom.app_block_ref_cnt++;
  1908. while (resp->result == QSEOS_RESULT_INCOMPLETE) {
  1909. lstnr = resp->data;
  1910. /*
  1911. * Wake up blocking lsitener service with the lstnr id
  1912. */
  1913. mutex_lock(&listener_access_lock);
  1914. list_for_each_entry(ptr_svc,
  1915. &qseecom.registered_listener_list_head, list) {
  1916. if (ptr_svc->svc.listener_id == lstnr) {
  1917. ptr_svc->listener_in_use = true;
  1918. ptr_svc->rcv_req_flag = 1;
  1919. ret = qseecom_dmabuf_cache_operations(
  1920. ptr_svc->dmabuf,
  1921. QSEECOM_CACHE_INVALIDATE);
  1922. if (ret) {
  1923. rc = -EINVAL;
  1924. status = QSEOS_RESULT_FAILURE;
  1925. goto err_resp;
  1926. }
  1927. wake_up_interruptible(&ptr_svc->rcv_req_wq);
  1928. break;
  1929. }
  1930. }
  1931. if (ptr_svc == NULL) {
  1932. pr_err("Listener Svc %d does not exist\n", lstnr);
  1933. rc = -EINVAL;
  1934. status = QSEOS_RESULT_FAILURE;
  1935. goto err_resp;
  1936. }
  1937. if (!ptr_svc->dmabuf) {
  1938. pr_err("Client dmabuf is not initialized\n");
  1939. rc = -EINVAL;
  1940. status = QSEOS_RESULT_FAILURE;
  1941. goto err_resp;
  1942. }
  1943. if (ptr_svc->svc.listener_id != lstnr) {
  1944. pr_err("Service %d does not exist\n",
  1945. lstnr);
  1946. rc = -ERESTARTSYS;
  1947. ptr_svc = NULL;
  1948. status = QSEOS_RESULT_FAILURE;
  1949. goto err_resp;
  1950. }
  1951. if (ptr_svc->abort == 1) {
  1952. pr_debug("Service %d abort %d\n",
  1953. lstnr, ptr_svc->abort);
  1954. rc = -ENODEV;
  1955. status = QSEOS_RESULT_FAILURE;
  1956. goto err_resp;
  1957. }
  1958. pr_debug("waking up rcv_req_wq and waiting for send_resp_wq\n");
  1959. /* initialize the new signal mask with all signals*/
  1960. sigfillset(&new_sigset);
  1961. /* block all signals */
  1962. mutex_unlock(&listener_access_lock);
  1963. do {
  1964. /*
  1965. * When reentrancy is not supported, check global
  1966. * send_resp_flag; otherwise, check this listener's
  1967. * send_resp_flag.
  1968. */
  1969. if (!qseecom.qsee_reentrancy_support &&
  1970. !wait_event_interruptible(qseecom.send_resp_wq,
  1971. __qseecom_listener_has_sent_rsp(
  1972. data, ptr_svc))) {
  1973. break;
  1974. }
  1975. if (qseecom.qsee_reentrancy_support &&
  1976. !wait_event_interruptible(qseecom.send_resp_wq,
  1977. __qseecom_reentrancy_listener_has_sent_rsp(
  1978. data, ptr_svc))) {
  1979. break;
  1980. }
  1981. } while (1);
  1982. mutex_lock(&listener_access_lock);
  1983. /* restore signal mask */
  1984. if (data->abort || ptr_svc->abort) {
  1985. pr_err("Abort clnt %d waiting on lstnr svc %d, ret %d\n",
  1986. data->client.app_id, lstnr, ret);
  1987. rc = -ENODEV;
  1988. status = QSEOS_RESULT_FAILURE;
  1989. } else {
  1990. status = QSEOS_RESULT_SUCCESS;
  1991. }
  1992. err_resp:
  1993. qseecom.send_resp_flag = 0;
  1994. if (ptr_svc) {
  1995. ptr_svc->send_resp_flag = 0;
  1996. table = ptr_svc->sglistinfo_ptr;
  1997. }
  1998. if (qseecom.qsee_version < QSEE_VERSION_40) {
  1999. send_data_rsp.listener_id = lstnr;
  2000. send_data_rsp.status = status;
  2001. if (table) {
  2002. send_data_rsp.sglistinfo_ptr =
  2003. (uint32_t)virt_to_phys(table);
  2004. send_data_rsp.sglistinfo_len =
  2005. SGLISTINFO_TABLE_SIZE;
  2006. qtee_shmbridge_flush_shm_buf(
  2007. &ptr_svc->sglistinfo_shm);
  2008. }
  2009. cmd_buf = (void *)&send_data_rsp;
  2010. cmd_len = sizeof(send_data_rsp);
  2011. } else {
  2012. send_data_rsp_64bit.listener_id = lstnr;
  2013. send_data_rsp_64bit.status = status;
  2014. if (table) {
  2015. send_data_rsp_64bit.sglistinfo_ptr =
  2016. virt_to_phys(table);
  2017. send_data_rsp_64bit.sglistinfo_len =
  2018. SGLISTINFO_TABLE_SIZE;
  2019. qtee_shmbridge_flush_shm_buf(
  2020. &ptr_svc->sglistinfo_shm);
  2021. }
  2022. cmd_buf = (void *)&send_data_rsp_64bit;
  2023. cmd_len = sizeof(send_data_rsp_64bit);
  2024. }
  2025. if (!qseecom.whitelist_support || table == NULL)
  2026. *(uint32_t *)cmd_buf = QSEOS_LISTENER_DATA_RSP_COMMAND;
  2027. else
  2028. *(uint32_t *)cmd_buf =
  2029. QSEOS_LISTENER_DATA_RSP_COMMAND_WHITELIST;
  2030. if ((lstnr == RPMB_SERVICE) || (lstnr == SSD_SERVICE)) {
  2031. ret = __qseecom_enable_clk(CLK_QSEE);
  2032. if (ret)
  2033. goto exit;
  2034. }
  2035. if (ptr_svc) {
  2036. ret = qseecom_dmabuf_cache_operations(ptr_svc->dmabuf,
  2037. QSEECOM_CACHE_CLEAN);
  2038. if (ret)
  2039. goto exit;
  2040. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1,
  2041. cmd_buf, cmd_len, resp, sizeof(*resp));
  2042. ptr_svc->listener_in_use = false;
  2043. __qseecom_clean_listener_sglistinfo(ptr_svc);
  2044. if (ret) {
  2045. pr_err("scm_call() failed with err: %d (app_id = %d)\n",
  2046. ret, data->client.app_id);
  2047. goto exit;
  2048. }
  2049. } else {
  2050. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1,
  2051. cmd_buf, cmd_len, resp, sizeof(*resp));
  2052. if (ret) {
  2053. pr_err("scm_call() failed with err: %d (app_id = %d)\n",
  2054. ret, data->client.app_id);
  2055. goto exit;
  2056. }
  2057. }
  2058. pr_debug("resp status %d, res= %d, app_id = %d, lstr = %d\n",
  2059. status, resp->result, data->client.app_id, lstnr);
  2060. if ((resp->result != QSEOS_RESULT_SUCCESS) &&
  2061. (resp->result != QSEOS_RESULT_INCOMPLETE)) {
  2062. pr_err("fail:resp res= %d,app_id = %d,lstr = %d\n",
  2063. resp->result, data->client.app_id, lstnr);
  2064. ret = -EINVAL;
  2065. }
  2066. exit:
  2067. mutex_unlock(&listener_access_lock);
  2068. if ((lstnr == RPMB_SERVICE) || (lstnr == SSD_SERVICE))
  2069. __qseecom_disable_clk(CLK_QSEE);
  2070. }
  2071. qseecom.app_block_ref_cnt--;
  2072. wake_up_interruptible_all(&qseecom.app_block_wq);
  2073. if (rc)
  2074. return rc;
  2075. return ret;
  2076. }
  2077. static int __qseecom_process_reentrancy_blocked_on_listener(
  2078. struct qseecom_command_scm_resp *resp,
  2079. struct qseecom_registered_app_list *ptr_app,
  2080. struct qseecom_dev_handle *data)
  2081. {
  2082. struct qseecom_registered_listener_list *list_ptr;
  2083. int ret = 0;
  2084. struct qseecom_continue_blocked_request_ireq ireq;
  2085. struct qseecom_command_scm_resp continue_resp;
  2086. unsigned int session_id;
  2087. sigset_t new_sigset;
  2088. unsigned long flags;
  2089. bool found_app = false;
  2090. struct qseecom_registered_app_list dummy_app_entry = { {NULL} };
  2091. if (!resp || !data) {
  2092. pr_err("invalid resp or data pointer\n");
  2093. ret = -EINVAL;
  2094. goto exit;
  2095. }
  2096. /* find app_id & img_name from list */
  2097. if (!ptr_app) {
  2098. if (data->client.from_smcinvoke || data->client.from_loadapp) {
  2099. pr_debug("This request is from %s\n",
  2100. (data->client.from_smcinvoke ? "smcinvoke" : "load_app"));
  2101. ptr_app = &dummy_app_entry;
  2102. ptr_app->app_id = data->client.app_id;
  2103. } else {
  2104. spin_lock_irqsave(&qseecom.registered_app_list_lock,
  2105. flags);
  2106. list_for_each_entry(ptr_app,
  2107. &qseecom.registered_app_list_head, list) {
  2108. if ((ptr_app->app_id == data->client.app_id) &&
  2109. (!strcmp(ptr_app->app_name,
  2110. data->client.app_name))) {
  2111. found_app = true;
  2112. break;
  2113. }
  2114. }
  2115. spin_unlock_irqrestore(
  2116. &qseecom.registered_app_list_lock, flags);
  2117. if (!found_app) {
  2118. pr_err("app_id %d (%s) is not found\n",
  2119. data->client.app_id,
  2120. (char *)data->client.app_name);
  2121. ret = -ENOENT;
  2122. goto exit;
  2123. }
  2124. }
  2125. }
  2126. do {
  2127. session_id = resp->resp_type;
  2128. mutex_lock(&listener_access_lock);
  2129. list_ptr = __qseecom_find_svc(resp->data);
  2130. if (!list_ptr) {
  2131. pr_err("Invalid listener ID %d\n", resp->data);
  2132. ret = -ENODATA;
  2133. mutex_unlock(&listener_access_lock);
  2134. goto exit;
  2135. }
  2136. ptr_app->blocked_on_listener_id = resp->data;
  2137. pr_warn("Lsntr %d in_use %d, block session(%d) app(%d)\n",
  2138. resp->data, list_ptr->listener_in_use,
  2139. session_id, data->client.app_id);
  2140. /* sleep until listener is available */
  2141. sigfillset(&new_sigset);
  2142. do {
  2143. qseecom.app_block_ref_cnt++;
  2144. ptr_app->app_blocked = true;
  2145. mutex_unlock(&listener_access_lock);
  2146. mutex_unlock(&app_access_lock);
  2147. wait_event_interruptible(
  2148. list_ptr->listener_block_app_wq,
  2149. !list_ptr->listener_in_use);
  2150. mutex_lock(&app_access_lock);
  2151. mutex_lock(&listener_access_lock);
  2152. ptr_app->app_blocked = false;
  2153. qseecom.app_block_ref_cnt--;
  2154. } while (list_ptr->listener_in_use);
  2155. ptr_app->blocked_on_listener_id = 0;
  2156. pr_warn("Lsntr %d is available, unblock session(%d) app(%d)\n",
  2157. resp->data, session_id, data->client.app_id);
  2158. /* notify TZ that listener is available */
  2159. ireq.qsee_cmd_id = QSEOS_CONTINUE_BLOCKED_REQ_COMMAND;
  2160. if (qseecom.smcinvoke_support)
  2161. ireq.app_or_session_id = session_id;
  2162. else
  2163. ireq.app_or_session_id = data->client.app_id;
  2164. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1,
  2165. &ireq, sizeof(ireq),
  2166. &continue_resp, sizeof(continue_resp));
  2167. if (ret && qseecom.smcinvoke_support) {
  2168. /* retry with legacy cmd */
  2169. pr_warn("falling back to legacy method\n");
  2170. qseecom.smcinvoke_support = false;
  2171. ireq.app_or_session_id = data->client.app_id;
  2172. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1,
  2173. &ireq, sizeof(ireq),
  2174. &continue_resp, sizeof(continue_resp));
  2175. qseecom.smcinvoke_support = true;
  2176. if (ret) {
  2177. pr_err("unblock app %d or session %d fail\n",
  2178. data->client.app_id, session_id);
  2179. mutex_unlock(&listener_access_lock);
  2180. goto exit;
  2181. }
  2182. }
  2183. mutex_unlock(&listener_access_lock);
  2184. resp->result = continue_resp.result;
  2185. resp->resp_type = continue_resp.resp_type;
  2186. resp->data = continue_resp.data;
  2187. pr_err("unblock resp = %d\n", resp->result);
  2188. } while (resp->result == QSEOS_RESULT_BLOCKED_ON_LISTENER);
  2189. if (resp->result != QSEOS_RESULT_INCOMPLETE) {
  2190. pr_err("Unexpected unblock resp %d\n", resp->result);
  2191. ret = -EINVAL;
  2192. }
  2193. exit:
  2194. return ret;
  2195. }
  2196. static int __qseecom_reentrancy_process_incomplete_cmd(
  2197. struct qseecom_dev_handle *data,
  2198. struct qseecom_command_scm_resp *resp)
  2199. {
  2200. int ret = 0;
  2201. int rc = 0;
  2202. uint32_t lstnr;
  2203. struct qseecom_client_listener_data_irsp send_data_rsp = {0};
  2204. struct qseecom_client_listener_data_64bit_irsp send_data_rsp_64bit
  2205. = {0};
  2206. struct qseecom_registered_listener_list *ptr_svc = NULL;
  2207. sigset_t new_sigset;
  2208. uint32_t status;
  2209. void *cmd_buf = NULL;
  2210. size_t cmd_len;
  2211. struct sglist_info *table = NULL;
  2212. while (ret == 0 && resp->result == QSEOS_RESULT_INCOMPLETE) {
  2213. lstnr = resp->data;
  2214. /*
  2215. * Wake up blocking lsitener service with the lstnr id
  2216. */
  2217. mutex_lock(&listener_access_lock);
  2218. list_for_each_entry(ptr_svc,
  2219. &qseecom.registered_listener_list_head, list) {
  2220. if (ptr_svc->svc.listener_id == lstnr) {
  2221. ptr_svc->listener_in_use = true;
  2222. ptr_svc->rcv_req_flag = 1;
  2223. ret = qseecom_dmabuf_cache_operations(
  2224. ptr_svc->dmabuf,
  2225. QSEECOM_CACHE_INVALIDATE);
  2226. if (ret) {
  2227. rc = -EINVAL;
  2228. status = QSEOS_RESULT_FAILURE;
  2229. goto err_resp;
  2230. }
  2231. wake_up_interruptible(&ptr_svc->rcv_req_wq);
  2232. break;
  2233. }
  2234. }
  2235. if (ptr_svc == NULL) {
  2236. pr_err("Listener Svc %d does not exist\n", lstnr);
  2237. rc = -EINVAL;
  2238. status = QSEOS_RESULT_FAILURE;
  2239. goto err_resp;
  2240. }
  2241. if (!ptr_svc->dmabuf) {
  2242. pr_err("Client dmabuf is not initialized\n");
  2243. rc = -EINVAL;
  2244. status = QSEOS_RESULT_FAILURE;
  2245. goto err_resp;
  2246. }
  2247. if (ptr_svc->svc.listener_id != lstnr) {
  2248. pr_err("Service %d does not exist\n",
  2249. lstnr);
  2250. rc = -ERESTARTSYS;
  2251. ptr_svc = NULL;
  2252. table = NULL;
  2253. status = QSEOS_RESULT_FAILURE;
  2254. goto err_resp;
  2255. }
  2256. if (ptr_svc->abort == 1) {
  2257. pr_debug("Service %d abort %d\n",
  2258. lstnr, ptr_svc->abort);
  2259. rc = -ENODEV;
  2260. status = QSEOS_RESULT_FAILURE;
  2261. goto err_resp;
  2262. }
  2263. pr_debug("waking up rcv_req_wq and waiting for send_resp_wq\n");
  2264. /* initialize the new signal mask with all signals*/
  2265. sigfillset(&new_sigset);
  2266. /* block all signals */
  2267. /* unlock mutex btw waking listener and sleep-wait */
  2268. mutex_unlock(&listener_access_lock);
  2269. mutex_unlock(&app_access_lock);
  2270. do {
  2271. if (!wait_event_interruptible(qseecom.send_resp_wq,
  2272. __qseecom_reentrancy_listener_has_sent_rsp(
  2273. data, ptr_svc))) {
  2274. break;
  2275. }
  2276. } while (1);
  2277. /* lock mutex again after resp sent */
  2278. mutex_lock(&app_access_lock);
  2279. mutex_lock(&listener_access_lock);
  2280. ptr_svc->send_resp_flag = 0;
  2281. qseecom.send_resp_flag = 0;
  2282. /* restore signal mask */
  2283. if (data->abort || ptr_svc->abort) {
  2284. pr_err("Abort clnt %d waiting on lstnr svc %d, ret %d\n",
  2285. data->client.app_id, lstnr, ret);
  2286. rc = -ENODEV;
  2287. status = QSEOS_RESULT_FAILURE;
  2288. } else {
  2289. status = QSEOS_RESULT_SUCCESS;
  2290. }
  2291. err_resp:
  2292. if (ptr_svc)
  2293. table = ptr_svc->sglistinfo_ptr;
  2294. if (qseecom.qsee_version < QSEE_VERSION_40) {
  2295. send_data_rsp.listener_id = lstnr;
  2296. send_data_rsp.status = status;
  2297. if (table) {
  2298. send_data_rsp.sglistinfo_ptr =
  2299. (uint32_t)virt_to_phys(table);
  2300. send_data_rsp.sglistinfo_len =
  2301. SGLISTINFO_TABLE_SIZE;
  2302. qtee_shmbridge_flush_shm_buf(
  2303. &ptr_svc->sglistinfo_shm);
  2304. }
  2305. cmd_buf = (void *)&send_data_rsp;
  2306. cmd_len = sizeof(send_data_rsp);
  2307. } else {
  2308. send_data_rsp_64bit.listener_id = lstnr;
  2309. send_data_rsp_64bit.status = status;
  2310. if (table) {
  2311. send_data_rsp_64bit.sglistinfo_ptr =
  2312. virt_to_phys(table);
  2313. send_data_rsp_64bit.sglistinfo_len =
  2314. SGLISTINFO_TABLE_SIZE;
  2315. qtee_shmbridge_flush_shm_buf(
  2316. &ptr_svc->sglistinfo_shm);
  2317. }
  2318. cmd_buf = (void *)&send_data_rsp_64bit;
  2319. cmd_len = sizeof(send_data_rsp_64bit);
  2320. }
  2321. if (!qseecom.whitelist_support || table == NULL)
  2322. *(uint32_t *)cmd_buf = QSEOS_LISTENER_DATA_RSP_COMMAND;
  2323. else
  2324. *(uint32_t *)cmd_buf =
  2325. QSEOS_LISTENER_DATA_RSP_COMMAND_WHITELIST;
  2326. if (lstnr == RPMB_SERVICE) {
  2327. ret = __qseecom_enable_clk(CLK_QSEE);
  2328. if (ret)
  2329. goto exit;
  2330. }
  2331. if (ptr_svc) {
  2332. ret = qseecom_dmabuf_cache_operations(ptr_svc->dmabuf,
  2333. QSEECOM_CACHE_CLEAN);
  2334. if (ret)
  2335. goto exit;
  2336. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1,
  2337. cmd_buf, cmd_len, resp, sizeof(*resp));
  2338. ptr_svc->listener_in_use = false;
  2339. __qseecom_clean_listener_sglistinfo(ptr_svc);
  2340. wake_up_interruptible(&ptr_svc->listener_block_app_wq);
  2341. if (ret) {
  2342. pr_err("scm_call() failed with err: %d (app_id = %d)\n",
  2343. ret, data->client.app_id);
  2344. goto exit;
  2345. }
  2346. } else {
  2347. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1,
  2348. cmd_buf, cmd_len, resp, sizeof(*resp));
  2349. if (ret) {
  2350. pr_err("scm_call() failed with err: %d (app_id = %d)\n",
  2351. ret, data->client.app_id);
  2352. goto exit;
  2353. }
  2354. }
  2355. switch (resp->result) {
  2356. case QSEOS_RESULT_BLOCKED_ON_LISTENER:
  2357. pr_warn("send lsr %d rsp, but app %d block on lsr %d\n",
  2358. lstnr, data->client.app_id, resp->data);
  2359. if (lstnr == resp->data) {
  2360. pr_err("lstnr %d should not be blocked!\n",
  2361. lstnr);
  2362. ret = -EINVAL;
  2363. goto exit;
  2364. }
  2365. mutex_unlock(&listener_access_lock);
  2366. ret = __qseecom_process_reentrancy_blocked_on_listener(
  2367. resp, NULL, data);
  2368. mutex_lock(&listener_access_lock);
  2369. if (ret) {
  2370. pr_err("failed to process App(%d) %s blocked on listener %d\n",
  2371. data->client.app_id,
  2372. data->client.app_name, resp->data);
  2373. goto exit;
  2374. }
  2375. fallthrough;
  2376. case QSEOS_RESULT_SUCCESS:
  2377. break;
  2378. case QSEOS_RESULT_INCOMPLETE:
  2379. break;
  2380. case QSEOS_RESULT_CBACK_REQUEST:
  2381. pr_warn("get cback req app_id = %d, resp->data = %d\n",
  2382. data->client.app_id, resp->data);
  2383. resp->resp_type = SMCINVOKE_RESULT_INBOUND_REQ_NEEDED;
  2384. break;
  2385. default:
  2386. pr_err("fail:resp res= %d,app_id = %d,lstr = %d\n",
  2387. resp->result, data->client.app_id, lstnr);
  2388. ret = -EINVAL;
  2389. goto exit;
  2390. }
  2391. exit:
  2392. mutex_unlock(&listener_access_lock);
  2393. if (lstnr == RPMB_SERVICE)
  2394. __qseecom_disable_clk(CLK_QSEE);
  2395. }
  2396. if (rc)
  2397. return rc;
  2398. return ret;
  2399. }
  2400. /*
  2401. * QSEE doesn't support OS level cmds reentrancy until RE phase-3,
  2402. * and QSEE OS level scm_call cmds will fail if there is any blocked TZ app.
  2403. * So, needs to first check if no app blocked before sending OS level scm call,
  2404. * then wait until all apps are unblocked.
  2405. */
  2406. static void __qseecom_reentrancy_check_if_no_app_blocked(uint32_t smc_id)
  2407. {
  2408. if (qseecom.qsee_reentrancy_support > QSEE_REENTRANCY_PHASE_0 &&
  2409. qseecom.qsee_reentrancy_support < QSEE_REENTRANCY_PHASE_3 &&
  2410. IS_OWNER_TRUSTED_OS(TZ_SYSCALL_OWNER_ID(smc_id))) {
  2411. /* thread sleep until this app unblocked */
  2412. while (qseecom.app_block_ref_cnt > 0) {
  2413. mutex_unlock(&app_access_lock);
  2414. wait_event_interruptible(qseecom.app_block_wq,
  2415. (!qseecom.app_block_ref_cnt));
  2416. mutex_lock(&app_access_lock);
  2417. }
  2418. }
  2419. }
  2420. /*
  2421. * scm_call of send data will fail if this TA is blocked or there are more
  2422. * than one TA requesting listener services; So, first check to see if need
  2423. * to wait.
  2424. */
  2425. static void __qseecom_reentrancy_check_if_this_app_blocked(
  2426. struct qseecom_registered_app_list *ptr_app)
  2427. {
  2428. if (qseecom.qsee_reentrancy_support) {
  2429. ptr_app->check_block++;
  2430. while (ptr_app->app_blocked || qseecom.app_block_ref_cnt > 1) {
  2431. /* thread sleep until this app unblocked */
  2432. mutex_unlock(&app_access_lock);
  2433. wait_event_interruptible(qseecom.app_block_wq,
  2434. (!ptr_app->app_blocked &&
  2435. qseecom.app_block_ref_cnt <= 1));
  2436. mutex_lock(&app_access_lock);
  2437. }
  2438. ptr_app->check_block--;
  2439. }
  2440. }
  2441. static int __qseecom_check_app_exists(struct qseecom_check_app_ireq req,
  2442. uint32_t *app_id)
  2443. {
  2444. int32_t ret;
  2445. struct qseecom_command_scm_resp resp;
  2446. bool found_app = false;
  2447. struct qseecom_registered_app_list *entry = NULL;
  2448. unsigned long flags = 0;
  2449. if (!app_id) {
  2450. pr_err("Null pointer to app_id\n");
  2451. return -EINVAL;
  2452. }
  2453. *app_id = 0;
  2454. /* check if app exists and has been registered locally */
  2455. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
  2456. list_for_each_entry(entry,
  2457. &qseecom.registered_app_list_head, list) {
  2458. if (!strcmp(entry->app_name, req.app_name)) {
  2459. found_app = true;
  2460. break;
  2461. }
  2462. }
  2463. spin_unlock_irqrestore(&qseecom.registered_app_list_lock, flags);
  2464. if (found_app) {
  2465. pr_debug("Found app with id %d\n", entry->app_id);
  2466. *app_id = entry->app_id;
  2467. return 0;
  2468. }
  2469. memset((void *)&resp, 0, sizeof(resp));
  2470. /* SCM_CALL to check if app_id for the mentioned app exists */
  2471. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1, &req,
  2472. sizeof(struct qseecom_check_app_ireq),
  2473. &resp, sizeof(resp));
  2474. if (ret) {
  2475. pr_err("scm_call to check if app is already loaded failed\n");
  2476. return -EINVAL;
  2477. }
  2478. if (resp.result == QSEOS_RESULT_FAILURE)
  2479. return 0;
  2480. switch (resp.resp_type) {
  2481. /*qsee returned listener type response */
  2482. case QSEOS_LISTENER_ID:
  2483. pr_err("resp type is of listener type instead of app\n");
  2484. return -EINVAL;
  2485. case QSEOS_APP_ID:
  2486. *app_id = resp.data;
  2487. return 0;
  2488. default:
  2489. pr_err("invalid resp type (%d) from qsee\n",
  2490. resp.resp_type);
  2491. return -ENODEV;
  2492. }
  2493. }
  2494. static int qseecom_load_app(struct qseecom_dev_handle *data, void __user *argp)
  2495. {
  2496. struct qseecom_registered_app_list *entry = NULL;
  2497. unsigned long flags = 0;
  2498. u32 app_id = 0;
  2499. struct qseecom_load_img_req load_img_req;
  2500. int32_t ret = 0;
  2501. phys_addr_t pa = 0;
  2502. void *vaddr = NULL;
  2503. struct dma_buf_attachment *attach = NULL;
  2504. struct dma_buf *dmabuf = NULL;
  2505. struct sg_table *sgt = NULL;
  2506. size_t len;
  2507. struct qseecom_command_scm_resp resp;
  2508. struct qseecom_check_app_ireq req;
  2509. struct qseecom_load_app_ireq load_req;
  2510. struct qseecom_load_app_64bit_ireq load_req_64bit;
  2511. void *cmd_buf = NULL;
  2512. size_t cmd_len;
  2513. bool first_time = false;
  2514. /* Copy the relevant information needed for loading the image */
  2515. if (copy_from_user(&load_img_req,
  2516. (void __user *)argp,
  2517. sizeof(struct qseecom_load_img_req))) {
  2518. pr_err("copy_from_user failed\n");
  2519. return -EFAULT;
  2520. }
  2521. /* Check and load cmnlib */
  2522. if (qseecom.qsee_version > QSEEE_VERSION_00) {
  2523. if (!qseecom.commonlib_loaded &&
  2524. load_img_req.app_arch == ELFCLASS32) {
  2525. ret = qseecom_load_commonlib_image(data, "cmnlib");
  2526. if (ret) {
  2527. pr_err("failed to load cmnlib\n");
  2528. return -EIO;
  2529. }
  2530. qseecom.commonlib_loaded = true;
  2531. pr_debug("cmnlib is loaded\n");
  2532. }
  2533. if (!qseecom.commonlib64_loaded &&
  2534. load_img_req.app_arch == ELFCLASS64) {
  2535. ret = qseecom_load_commonlib_image(data, "cmnlib64");
  2536. if (ret) {
  2537. pr_err("failed to load cmnlib64\n");
  2538. return -EIO;
  2539. }
  2540. qseecom.commonlib64_loaded = true;
  2541. pr_debug("cmnlib64 is loaded\n");
  2542. }
  2543. }
  2544. if (qseecom.support_bus_scaling) {
  2545. mutex_lock(&qsee_bw_mutex);
  2546. ret = __qseecom_register_bus_bandwidth_needs(data, MEDIUM);
  2547. mutex_unlock(&qsee_bw_mutex);
  2548. if (ret)
  2549. return ret;
  2550. }
  2551. /* Vote for the SFPB clock */
  2552. ret = __qseecom_enable_clk_scale_up(data);
  2553. if (ret)
  2554. goto enable_clk_err;
  2555. req.qsee_cmd_id = QSEOS_APP_LOOKUP_COMMAND;
  2556. load_img_req.img_name[MAX_APP_NAME_SIZE-1] = '\0';
  2557. strlcpy(req.app_name, load_img_req.img_name, MAX_APP_NAME_SIZE);
  2558. ret = __qseecom_check_app_exists(req, &app_id);
  2559. if (ret < 0)
  2560. goto checkapp_err;
  2561. if (app_id) {
  2562. pr_debug("App id %d (%s) already exists\n", app_id,
  2563. (char *)(req.app_name));
  2564. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
  2565. list_for_each_entry(entry,
  2566. &qseecom.registered_app_list_head, list){
  2567. if (entry->app_id == app_id) {
  2568. if (entry->ref_cnt == U32_MAX) {
  2569. pr_err("App %d (%s) ref_cnt overflow\n",
  2570. app_id, req.app_name);
  2571. ret = -EINVAL;
  2572. goto loadapp_err;
  2573. }
  2574. entry->ref_cnt++;
  2575. break;
  2576. }
  2577. }
  2578. spin_unlock_irqrestore(
  2579. &qseecom.registered_app_list_lock, flags);
  2580. ret = 0;
  2581. } else {
  2582. first_time = true;
  2583. pr_warn("App (%s) does'nt exist, loading apps for first time\n",
  2584. (char *)(load_img_req.img_name));
  2585. ret = qseecom_vaddr_map(load_img_req.ifd_data_fd,
  2586. &pa, &vaddr, &sgt, &attach, &len, &dmabuf);
  2587. if (ret) {
  2588. pr_err("Ion client could not retrieve the handle\n");
  2589. ret = -ENOMEM;
  2590. goto loadapp_err;
  2591. }
  2592. if (load_img_req.mdt_len > len || load_img_req.img_len > len) {
  2593. pr_err("ion len %zu is smaller than mdt_len %u or img_len %u\n",
  2594. len, load_img_req.mdt_len,
  2595. load_img_req.img_len);
  2596. ret = -EINVAL;
  2597. goto loadapp_err;
  2598. }
  2599. /* Populate the structure for sending scm call to load image */
  2600. if (qseecom.qsee_version < QSEE_VERSION_40) {
  2601. load_req.qsee_cmd_id = QSEOS_APP_START_COMMAND;
  2602. load_req.mdt_len = load_img_req.mdt_len;
  2603. load_req.img_len = load_img_req.img_len;
  2604. strlcpy(load_req.app_name, load_img_req.img_name,
  2605. MAX_APP_NAME_SIZE);
  2606. load_req.phy_addr = (uint32_t)pa;
  2607. cmd_buf = (void *)&load_req;
  2608. cmd_len = sizeof(struct qseecom_load_app_ireq);
  2609. } else {
  2610. load_req_64bit.qsee_cmd_id = QSEOS_APP_START_COMMAND;
  2611. load_req_64bit.mdt_len = load_img_req.mdt_len;
  2612. load_req_64bit.img_len = load_img_req.img_len;
  2613. strlcpy(load_req_64bit.app_name, load_img_req.img_name,
  2614. MAX_APP_NAME_SIZE);
  2615. load_req_64bit.phy_addr = (uint64_t)pa;
  2616. cmd_buf = (void *)&load_req_64bit;
  2617. cmd_len = sizeof(struct qseecom_load_app_64bit_ireq);
  2618. }
  2619. ret = qseecom_dmabuf_cache_operations(dmabuf,
  2620. QSEECOM_CACHE_CLEAN);
  2621. if (ret) {
  2622. pr_err("cache operation failed %d\n", ret);
  2623. goto loadapp_err;
  2624. }
  2625. /* SCM_CALL to load the app and get the app_id back */
  2626. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1, cmd_buf,
  2627. cmd_len, &resp, sizeof(resp));
  2628. if (ret) {
  2629. pr_err("scm_call to load app failed\n");
  2630. ret = -EINVAL;
  2631. goto loadapp_err;
  2632. }
  2633. ret = qseecom_dmabuf_cache_operations(dmabuf,
  2634. QSEECOM_CACHE_INVALIDATE);
  2635. if (ret) {
  2636. pr_err("cache operation failed %d\n", ret);
  2637. goto loadapp_err;
  2638. }
  2639. do {
  2640. if (resp.result == QSEOS_RESULT_FAILURE) {
  2641. pr_err("scm_call rsp.result is QSEOS_RESULT_FAILURE\n");
  2642. ret = -EFAULT;
  2643. goto loadapp_err;
  2644. }
  2645. if (resp.result == QSEOS_RESULT_INCOMPLETE) {
  2646. ret = __qseecom_process_incomplete_cmd(data, &resp);
  2647. if (ret) {
  2648. /* TZ has created app_id, need to unload it */
  2649. pr_err("incomp_cmd err %d, %d, unload %d %s\n",
  2650. ret, resp.result, resp.data,
  2651. load_img_req.img_name);
  2652. __qseecom_unload_app(data, resp.data);
  2653. ret = -EFAULT;
  2654. goto loadapp_err;
  2655. }
  2656. }
  2657. if (resp.result == QSEOS_RESULT_BLOCKED_ON_LISTENER) {
  2658. pr_err("load app blocked on listener\n");
  2659. data->client.app_id = resp.result;
  2660. data->client.from_loadapp = true;
  2661. ret = __qseecom_process_reentrancy_blocked_on_listener(&resp,
  2662. NULL, data);
  2663. if (ret) {
  2664. pr_err("load app fail proc block on listener,ret :%d\n",
  2665. ret);
  2666. ret = -EFAULT;
  2667. goto loadapp_err;
  2668. }
  2669. }
  2670. } while ((resp.result == QSEOS_RESULT_BLOCKED_ON_LISTENER) ||
  2671. (resp.result == QSEOS_RESULT_INCOMPLETE));
  2672. if (resp.result != QSEOS_RESULT_SUCCESS) {
  2673. pr_err("scm_call failed resp.result unknown, %d\n",
  2674. resp.result);
  2675. ret = -EFAULT;
  2676. goto loadapp_err;
  2677. }
  2678. app_id = resp.data;
  2679. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  2680. if (!entry) {
  2681. ret = -ENOMEM;
  2682. goto loadapp_err;
  2683. }
  2684. entry->app_id = app_id;
  2685. entry->ref_cnt = 1;
  2686. entry->app_arch = load_img_req.app_arch;
  2687. /*
  2688. * keymaster app may be first loaded as "keymaste" by qseecomd,
  2689. * and then used as "keymaster" on some targets. To avoid app
  2690. * name checking error, register "keymaster" into app_list and
  2691. * thread private data.
  2692. */
  2693. if (!strcmp(load_img_req.img_name, "keymaste"))
  2694. strlcpy(entry->app_name, "keymaster",
  2695. MAX_APP_NAME_SIZE);
  2696. else
  2697. strlcpy(entry->app_name, load_img_req.img_name,
  2698. MAX_APP_NAME_SIZE);
  2699. entry->app_blocked = false;
  2700. entry->blocked_on_listener_id = 0;
  2701. entry->check_block = 0;
  2702. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
  2703. list_add_tail(&entry->list, &qseecom.registered_app_list_head);
  2704. spin_unlock_irqrestore(&qseecom.registered_app_list_lock,
  2705. flags);
  2706. pr_warn("App with id %u (%s) now loaded\n", app_id,
  2707. (char *)(load_img_req.img_name));
  2708. }
  2709. data->client.app_id = app_id;
  2710. data->client.app_arch = load_img_req.app_arch;
  2711. if (!strcmp(load_img_req.img_name, "keymaste"))
  2712. strlcpy(data->client.app_name, "keymaster", MAX_APP_NAME_SIZE);
  2713. else
  2714. strlcpy(data->client.app_name, load_img_req.img_name,
  2715. MAX_APP_NAME_SIZE);
  2716. load_img_req.app_id = app_id;
  2717. if (copy_to_user(argp, &load_img_req, sizeof(load_img_req))) {
  2718. pr_err("copy_to_user failed\n");
  2719. ret = -EFAULT;
  2720. if (first_time) {
  2721. spin_lock_irqsave(
  2722. &qseecom.registered_app_list_lock, flags);
  2723. list_del(&entry->list);
  2724. spin_unlock_irqrestore(
  2725. &qseecom.registered_app_list_lock, flags);
  2726. kfree_sensitive(entry);
  2727. }
  2728. }
  2729. loadapp_err:
  2730. if (dmabuf) {
  2731. qseecom_vaddr_unmap(vaddr, sgt, attach, dmabuf);
  2732. MAKE_NULL(sgt, attach, dmabuf);
  2733. }
  2734. checkapp_err:
  2735. __qseecom_disable_clk_scale_down(data);
  2736. enable_clk_err:
  2737. if (qseecom.support_bus_scaling) {
  2738. mutex_lock(&qsee_bw_mutex);
  2739. qseecom_unregister_bus_bandwidth_needs(data);
  2740. mutex_unlock(&qsee_bw_mutex);
  2741. }
  2742. return ret;
  2743. }
  2744. static int __qseecom_cleanup_app(struct qseecom_dev_handle *data)
  2745. {
  2746. int ret = 0; /* Set unload app */
  2747. wake_up_all(&qseecom.send_resp_wq);
  2748. if (qseecom.qsee_reentrancy_support)
  2749. mutex_unlock(&app_access_lock);
  2750. while (atomic_read(&data->ioctl_count) > 1) {
  2751. if (wait_event_interruptible(data->abort_wq,
  2752. atomic_read(&data->ioctl_count) <= 1)) {
  2753. pr_err("Interrupted from abort\n");
  2754. ret = -ERESTARTSYS;
  2755. break;
  2756. }
  2757. }
  2758. if (qseecom.qsee_reentrancy_support)
  2759. mutex_lock(&app_access_lock);
  2760. return ret;
  2761. }
  2762. static int __qseecom_unload_app(struct qseecom_dev_handle *data,
  2763. uint32_t app_id)
  2764. {
  2765. struct qseecom_unload_app_ireq req;
  2766. struct qseecom_command_scm_resp resp;
  2767. int ret = 0;
  2768. /* Populate the structure for sending scm call to load image */
  2769. req.qsee_cmd_id = QSEOS_APP_SHUTDOWN_COMMAND;
  2770. req.app_id = app_id;
  2771. /* SCM_CALL to unload the app */
  2772. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1, &req,
  2773. sizeof(struct qseecom_unload_app_ireq),
  2774. &resp, sizeof(resp));
  2775. if (ret) {
  2776. pr_err("scm_call to unload app (id = %d) failed ret: %d\n",
  2777. app_id, ret);
  2778. return ret;
  2779. }
  2780. do {
  2781. switch (resp.result) {
  2782. case QSEOS_RESULT_SUCCESS:
  2783. pr_warn("App (%d) is unloaded\n", app_id);
  2784. break;
  2785. case QSEOS_RESULT_INCOMPLETE:
  2786. ret = __qseecom_process_incomplete_cmd(data, &resp);
  2787. if (ret)
  2788. pr_err("unload app %d fail proc incom cmd: %d,%d,%d\n",
  2789. app_id, ret, resp.result, resp.data);
  2790. else
  2791. pr_warn("App (%d) is unloaded\n", app_id);
  2792. break;
  2793. case QSEOS_RESULT_FAILURE:
  2794. pr_err("app (%d) unload_failed!!\n", app_id);
  2795. ret = -EFAULT;
  2796. break;
  2797. case QSEOS_RESULT_BLOCKED_ON_LISTENER:
  2798. pr_err("unload app (%d) blocked on listener\n", app_id);
  2799. ret = __qseecom_process_reentrancy_blocked_on_listener(&resp, NULL, data);
  2800. if (ret) {
  2801. pr_err("unload app fail proc block on listener cmd,ret :%d\n",
  2802. ret);
  2803. ret = -EFAULT;
  2804. }
  2805. break;
  2806. default:
  2807. pr_err("unload app %d get unknown resp.result %d\n",
  2808. app_id, resp.result);
  2809. ret = -EFAULT;
  2810. break;
  2811. }
  2812. } while ((resp.result == QSEOS_RESULT_INCOMPLETE) ||
  2813. (resp.result == QSEOS_RESULT_BLOCKED_ON_LISTENER));
  2814. return ret;
  2815. }
  2816. static int qseecom_unload_app(struct qseecom_dev_handle *data,
  2817. bool app_crash)
  2818. {
  2819. unsigned long flags;
  2820. int ret = 0;
  2821. struct qseecom_registered_app_list *ptr_app = NULL;
  2822. bool found_app = false;
  2823. if (!data) {
  2824. pr_err("Invalid/uninitialized device handle\n");
  2825. return -EINVAL;
  2826. }
  2827. pr_debug("unload app %d(%s), app_crash flag %d\n", data->client.app_id,
  2828. data->client.app_name, app_crash);
  2829. if (!memcmp(data->client.app_name, "keymaste", strlen("keymaste"))) {
  2830. pr_debug("Do not unload keymaster app from tz\n");
  2831. goto unload_exit;
  2832. }
  2833. ret = __qseecom_cleanup_app(data);
  2834. if (ret && !app_crash) {
  2835. pr_err("cleanup app failed, pending ioctl:%d\n", data->ioctl_count);
  2836. return ret;
  2837. }
  2838. __qseecom_reentrancy_check_if_no_app_blocked(TZ_OS_APP_SHUTDOWN_ID);
  2839. /* ignore app_id 0, it happens when close qseecom_fd if load app fail*/
  2840. if (!data->client.app_id)
  2841. goto unload_exit;
  2842. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
  2843. list_for_each_entry(ptr_app, &qseecom.registered_app_list_head,
  2844. list) {
  2845. if ((ptr_app->app_id == data->client.app_id) &&
  2846. (!strcmp(ptr_app->app_name, data->client.app_name))) {
  2847. pr_debug("unload app %d (%s), ref_cnt %d\n",
  2848. ptr_app->app_id, ptr_app->app_name,
  2849. ptr_app->ref_cnt);
  2850. ptr_app->ref_cnt--;
  2851. found_app = true;
  2852. break;
  2853. }
  2854. }
  2855. spin_unlock_irqrestore(&qseecom.registered_app_list_lock,
  2856. flags);
  2857. if (!found_app) {
  2858. pr_err("Cannot find app with id = %d (%s)\n",
  2859. data->client.app_id, data->client.app_name);
  2860. ret = -EINVAL;
  2861. goto unload_exit;
  2862. }
  2863. if (!ptr_app->ref_cnt) {
  2864. ret = __qseecom_unload_app(data, data->client.app_id);
  2865. if (ret == -EBUSY) {
  2866. /*
  2867. * If unload failed due to EBUSY, don't free mem
  2868. * just restore app ref_cnt and return -EBUSY
  2869. */
  2870. pr_warn("unload ta %d(%s) EBUSY\n",
  2871. data->client.app_id, data->client.app_name);
  2872. ptr_app->ref_cnt++;
  2873. return ret;
  2874. }
  2875. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
  2876. list_del(&ptr_app->list);
  2877. spin_unlock_irqrestore(&qseecom.registered_app_list_lock,
  2878. flags);
  2879. kfree_sensitive(ptr_app);
  2880. }
  2881. unload_exit:
  2882. if (data->client.dmabuf) {
  2883. qseecom_vaddr_unmap(data->client.sb_virt, data->client.sgt,
  2884. data->client.attach, data->client.dmabuf);
  2885. MAKE_NULL(data->client.sgt,
  2886. data->client.attach, data->client.dmabuf);
  2887. }
  2888. data->released = true;
  2889. return ret;
  2890. }
  2891. static int qseecom_prepare_unload_app(struct qseecom_dev_handle *data)
  2892. {
  2893. struct qseecom_unload_app_pending_list *entry = NULL;
  2894. pr_debug("prepare to unload app(%d)(%s), pending %d\n",
  2895. data->client.app_id, data->client.app_name,
  2896. data->client.unload_pending);
  2897. if (data->client.unload_pending)
  2898. return 0;
  2899. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  2900. if (!entry)
  2901. return -ENOMEM;
  2902. entry->data = data;
  2903. list_add_tail(&entry->list,
  2904. &qseecom.unload_app_pending_list_head);
  2905. data->client.unload_pending = true;
  2906. pr_debug("unload ta %d pending\n", data->client.app_id);
  2907. return 0;
  2908. }
  2909. static void __wakeup_unload_app_kthread(void)
  2910. {
  2911. atomic_set(&qseecom.unload_app_kthread_state,
  2912. UNLOAD_APP_KT_WAKEUP);
  2913. wake_up_interruptible(&qseecom.unload_app_kthread_wq);
  2914. }
  2915. static bool __qseecom_find_pending_unload_app(uint32_t app_id, char *app_name)
  2916. {
  2917. struct qseecom_unload_app_pending_list *entry = NULL;
  2918. bool found = false;
  2919. mutex_lock(&unload_app_pending_list_lock);
  2920. list_for_each_entry(entry, &qseecom.unload_app_pending_list_head,
  2921. list) {
  2922. if ((entry->data->client.app_id == app_id) &&
  2923. (!strcmp(entry->data->client.app_name, app_name))) {
  2924. found = true;
  2925. break;
  2926. }
  2927. }
  2928. mutex_unlock(&unload_app_pending_list_lock);
  2929. return found;
  2930. }
  2931. static void __qseecom_processing_pending_unload_app(void)
  2932. {
  2933. struct qseecom_unload_app_pending_list *entry = NULL;
  2934. struct list_head *pos;
  2935. int ret = 0;
  2936. mutex_lock(&unload_app_pending_list_lock);
  2937. while (!list_empty(&qseecom.unload_app_pending_list_head)) {
  2938. pos = qseecom.unload_app_pending_list_head.next;
  2939. entry = list_entry(pos,
  2940. struct qseecom_unload_app_pending_list, list);
  2941. if (entry && entry->data) {
  2942. pr_debug("process pending unload app %d (%s)\n",
  2943. entry->data->client.app_id,
  2944. entry->data->client.app_name);
  2945. mutex_unlock(&unload_app_pending_list_lock);
  2946. mutex_lock(&app_access_lock);
  2947. ret = qseecom_unload_app(entry->data, true);
  2948. if (ret)
  2949. pr_err("unload app %d pending failed %d\n",
  2950. entry->data->client.app_id, ret);
  2951. mutex_unlock(&app_access_lock);
  2952. mutex_lock(&unload_app_pending_list_lock);
  2953. __qseecom_free_tzbuf(&entry->data->sglistinfo_shm);
  2954. kfree_sensitive(entry->data);
  2955. }
  2956. list_del(pos);
  2957. kfree_sensitive(entry);
  2958. }
  2959. mutex_unlock(&unload_app_pending_list_lock);
  2960. }
  2961. static int __qseecom_unload_app_kthread_func(void *data)
  2962. {
  2963. while (!kthread_should_stop()) {
  2964. wait_event_interruptible(
  2965. qseecom.unload_app_kthread_wq,
  2966. atomic_read(&qseecom.unload_app_kthread_state)
  2967. == UNLOAD_APP_KT_WAKEUP);
  2968. pr_debug("kthread to unload app is called, state %d\n",
  2969. atomic_read(&qseecom.unload_app_kthread_state));
  2970. __qseecom_processing_pending_unload_app();
  2971. atomic_set(&qseecom.unload_app_kthread_state,
  2972. UNLOAD_APP_KT_SLEEP);
  2973. }
  2974. pr_warn("kthread to unload app stopped\n");
  2975. return 0;
  2976. }
  2977. static phys_addr_t __qseecom_uvirt_to_kphys(struct qseecom_dev_handle *data,
  2978. unsigned long virt)
  2979. {
  2980. return data->client.sb_phys + (virt - data->client.user_virt_sb_base);
  2981. }
  2982. static uintptr_t __qseecom_uvirt_to_kvirt(struct qseecom_dev_handle *data,
  2983. unsigned long virt)
  2984. {
  2985. return (uintptr_t)data->client.sb_virt +
  2986. (virt - data->client.user_virt_sb_base);
  2987. }
  2988. static int __qseecom_process_rpmb_svc_cmd(struct qseecom_dev_handle *data_ptr,
  2989. struct qseecom_send_svc_cmd_req *req_ptr,
  2990. struct qseecom_client_send_service_ireq *send_svc_ireq_ptr)
  2991. {
  2992. int ret = 0;
  2993. void *req_buf = NULL;
  2994. if ((req_ptr == NULL) || (send_svc_ireq_ptr == NULL)) {
  2995. pr_err("Error with pointer: req_ptr = %pK, send_svc_ptr = %pK\n",
  2996. req_ptr, send_svc_ireq_ptr);
  2997. return -EINVAL;
  2998. }
  2999. /* Clients need to ensure req_buf is at base offset of shared buffer */
  3000. if ((uintptr_t)req_ptr->cmd_req_buf !=
  3001. data_ptr->client.user_virt_sb_base) {
  3002. pr_err("cmd buf not pointing to base offset of shared buffer\n");
  3003. return -EINVAL;
  3004. }
  3005. if (data_ptr->client.sb_length <
  3006. sizeof(struct qseecom_rpmb_provision_key)) {
  3007. pr_err("shared buffer is too small to hold key type\n");
  3008. return -EINVAL;
  3009. }
  3010. req_buf = data_ptr->client.sb_virt;
  3011. send_svc_ireq_ptr->qsee_cmd_id = req_ptr->cmd_id;
  3012. send_svc_ireq_ptr->key_type =
  3013. ((struct qseecom_rpmb_provision_key *)req_buf)->key_type;
  3014. send_svc_ireq_ptr->req_len = req_ptr->cmd_req_len;
  3015. send_svc_ireq_ptr->rsp_ptr = (uint32_t)(__qseecom_uvirt_to_kphys(
  3016. data_ptr, (uintptr_t)req_ptr->resp_buf));
  3017. send_svc_ireq_ptr->rsp_len = req_ptr->resp_len;
  3018. return ret;
  3019. }
  3020. static int __qseecom_process_fsm_key_svc_cmd(
  3021. struct qseecom_dev_handle *data_ptr,
  3022. struct qseecom_send_svc_cmd_req *req_ptr,
  3023. struct qseecom_client_send_fsm_diag_req *send_svc_ireq_ptr)
  3024. {
  3025. int ret = 0;
  3026. uint32_t reqd_len_sb_in = 0;
  3027. if ((req_ptr == NULL) || (send_svc_ireq_ptr == NULL)) {
  3028. pr_err("Error with pointer: req_ptr = %pK, send_svc_ptr = %pK\n",
  3029. req_ptr, send_svc_ireq_ptr);
  3030. return -EINVAL;
  3031. }
  3032. reqd_len_sb_in = req_ptr->cmd_req_len + req_ptr->resp_len;
  3033. if (reqd_len_sb_in > data_ptr->client.sb_length) {
  3034. pr_err("Not enough memory to fit cmd_buf and resp_buf.\n");
  3035. pr_err("Required: %u, Available: %zu\n",
  3036. reqd_len_sb_in, data_ptr->client.sb_length);
  3037. return -ENOMEM;
  3038. }
  3039. send_svc_ireq_ptr->qsee_cmd_id = req_ptr->cmd_id;
  3040. send_svc_ireq_ptr->req_len = req_ptr->cmd_req_len;
  3041. send_svc_ireq_ptr->rsp_ptr = (uint32_t)(__qseecom_uvirt_to_kphys(
  3042. data_ptr, (uintptr_t)req_ptr->resp_buf));
  3043. send_svc_ireq_ptr->rsp_len = req_ptr->resp_len;
  3044. send_svc_ireq_ptr->req_ptr = (uint32_t)(__qseecom_uvirt_to_kphys(
  3045. data_ptr, (uintptr_t)req_ptr->cmd_req_buf));
  3046. return ret;
  3047. }
  3048. static int __validate_send_service_cmd_inputs(struct qseecom_dev_handle *data,
  3049. struct qseecom_send_svc_cmd_req *req)
  3050. {
  3051. if (!req || !req->resp_buf || !req->cmd_req_buf) {
  3052. pr_err("req or cmd buffer or response buffer is null\n");
  3053. return -EINVAL;
  3054. }
  3055. if (!data || !data->client.sb_virt) {
  3056. pr_err("Client or client buf is not initialized\n");
  3057. return -EINVAL;
  3058. }
  3059. if (data->client.sb_virt == NULL) {
  3060. pr_err("sb_virt null\n");
  3061. return -EINVAL;
  3062. }
  3063. if (data->client.user_virt_sb_base == 0) {
  3064. pr_err("user_virt_sb_base is null\n");
  3065. return -EINVAL;
  3066. }
  3067. if (data->client.sb_length == 0) {
  3068. pr_err("sb_length is 0\n");
  3069. return -EINVAL;
  3070. }
  3071. if (((uintptr_t)req->cmd_req_buf <
  3072. data->client.user_virt_sb_base) ||
  3073. ((uintptr_t)req->cmd_req_buf >=
  3074. (data->client.user_virt_sb_base + data->client.sb_length))) {
  3075. pr_err("cmd buffer address not within shared bufffer\n");
  3076. return -EINVAL;
  3077. }
  3078. if (((uintptr_t)req->resp_buf <
  3079. data->client.user_virt_sb_base) ||
  3080. ((uintptr_t)req->resp_buf >=
  3081. (data->client.user_virt_sb_base + data->client.sb_length))) {
  3082. pr_err("response buffer address not within shared bufffer\n");
  3083. return -EINVAL;
  3084. }
  3085. if ((req->cmd_req_len == 0) || (req->resp_len == 0) ||
  3086. (req->cmd_req_len > data->client.sb_length) ||
  3087. (req->resp_len > data->client.sb_length)) {
  3088. pr_err("cmd buf length or response buf length not valid\n");
  3089. return -EINVAL;
  3090. }
  3091. if (req->cmd_req_len > UINT_MAX - req->resp_len) {
  3092. pr_err("Integer overflow detected in req_len & rsp_len\n");
  3093. return -EINVAL;
  3094. }
  3095. if ((req->cmd_req_len + req->resp_len) > data->client.sb_length) {
  3096. pr_debug("Not enough memory to fit cmd_buf.\n");
  3097. pr_debug("resp_buf. Required: %u, Available: %zu\n",
  3098. (req->cmd_req_len + req->resp_len),
  3099. data->client.sb_length);
  3100. return -ENOMEM;
  3101. }
  3102. if ((uintptr_t)req->cmd_req_buf > (ULONG_MAX - req->cmd_req_len)) {
  3103. pr_err("Integer overflow in req_len & cmd_req_buf\n");
  3104. return -EINVAL;
  3105. }
  3106. if ((uintptr_t)req->resp_buf > (ULONG_MAX - req->resp_len)) {
  3107. pr_err("Integer overflow in resp_len & resp_buf\n");
  3108. return -EINVAL;
  3109. }
  3110. if (data->client.user_virt_sb_base >
  3111. (ULONG_MAX - data->client.sb_length)) {
  3112. pr_err("Integer overflow in user_virt_sb_base & sb_length\n");
  3113. return -EINVAL;
  3114. }
  3115. if ((((uintptr_t)req->cmd_req_buf + req->cmd_req_len) >
  3116. ((uintptr_t)data->client.user_virt_sb_base +
  3117. data->client.sb_length)) ||
  3118. (((uintptr_t)req->resp_buf + req->resp_len) >
  3119. ((uintptr_t)data->client.user_virt_sb_base +
  3120. data->client.sb_length))) {
  3121. pr_err("cmd buf or resp buf is out of shared buffer region\n");
  3122. return -EINVAL;
  3123. }
  3124. return 0;
  3125. }
  3126. static int qseecom_send_service_cmd(struct qseecom_dev_handle *data,
  3127. void __user *argp)
  3128. {
  3129. int ret = 0;
  3130. struct qseecom_client_send_service_ireq send_svc_ireq;
  3131. struct qseecom_client_send_fsm_diag_req send_fsm_diag_svc_ireq;
  3132. struct qseecom_command_scm_resp resp;
  3133. struct qseecom_send_svc_cmd_req req;
  3134. void *send_req_ptr;
  3135. size_t req_buf_size;
  3136. /*struct qseecom_command_scm_resp resp;*/
  3137. if (copy_from_user(&req,
  3138. (void __user *)argp,
  3139. sizeof(req))) {
  3140. pr_err("copy_from_user failed\n");
  3141. return -EFAULT;
  3142. }
  3143. if (__validate_send_service_cmd_inputs(data, &req))
  3144. return -EINVAL;
  3145. data->type = QSEECOM_SECURE_SERVICE;
  3146. switch (req.cmd_id) {
  3147. case QSEOS_RPMB_PROVISION_KEY_COMMAND:
  3148. case QSEOS_RPMB_ERASE_COMMAND:
  3149. case QSEOS_RPMB_CHECK_PROV_STATUS_COMMAND:
  3150. send_req_ptr = &send_svc_ireq;
  3151. req_buf_size = sizeof(send_svc_ireq);
  3152. if (__qseecom_process_rpmb_svc_cmd(data, &req,
  3153. send_req_ptr))
  3154. return -EINVAL;
  3155. break;
  3156. case QSEOS_FSM_LTEOTA_REQ_CMD:
  3157. case QSEOS_FSM_LTEOTA_REQ_RSP_CMD:
  3158. case QSEOS_FSM_IKE_REQ_CMD:
  3159. case QSEOS_FSM_IKE_REQ_RSP_CMD:
  3160. case QSEOS_FSM_OEM_FUSE_WRITE_ROW:
  3161. case QSEOS_FSM_OEM_FUSE_READ_ROW:
  3162. case QSEOS_FSM_ENCFS_REQ_CMD:
  3163. case QSEOS_FSM_ENCFS_REQ_RSP_CMD:
  3164. case QSEOS_DIAG_FUSE_REQ_CMD:
  3165. case QSEOS_DIAG_FUSE_REQ_RSP_CMD:
  3166. send_req_ptr = &send_fsm_diag_svc_ireq;
  3167. req_buf_size = sizeof(send_fsm_diag_svc_ireq);
  3168. if (__qseecom_process_fsm_key_svc_cmd(data, &req,
  3169. send_req_ptr))
  3170. return -EINVAL;
  3171. break;
  3172. default:
  3173. pr_err("Unsupported cmd_id %d\n", req.cmd_id);
  3174. return -EINVAL;
  3175. }
  3176. ret = qseecom_dmabuf_cache_operations(data->client.dmabuf,
  3177. QSEECOM_CACHE_CLEAN);
  3178. if (ret) {
  3179. pr_err("cache operation failed %d\n", ret);
  3180. return ret;
  3181. }
  3182. if (qseecom.support_bus_scaling) {
  3183. ret = qseecom_scale_bus_bandwidth_timer(HIGH);
  3184. if (ret) {
  3185. pr_err("Fail to set bw HIGH\n");
  3186. return ret;
  3187. }
  3188. } else {
  3189. ret = qseecom_perf_enable(data);
  3190. if (ret) {
  3191. pr_err("Failed to vote for clocks with err %d\n", ret);
  3192. return ret;
  3193. }
  3194. }
  3195. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1,
  3196. (const void *)send_req_ptr,
  3197. req_buf_size, &resp, sizeof(resp));
  3198. if (ret) {
  3199. pr_err("qseecom_scm_call failed with err: %d\n", ret);
  3200. goto exit;
  3201. }
  3202. ret = qseecom_dmabuf_cache_operations(data->client.dmabuf,
  3203. QSEECOM_CACHE_INVALIDATE);
  3204. if (ret) {
  3205. pr_err("cache operation failed %d\n", ret);
  3206. goto exit;
  3207. }
  3208. switch (resp.result) {
  3209. case QSEOS_RESULT_SUCCESS:
  3210. break;
  3211. case QSEOS_RESULT_INCOMPLETE:
  3212. pr_debug("qseos_result_incomplete\n");
  3213. ret = __qseecom_process_incomplete_cmd(data, &resp);
  3214. if (ret) {
  3215. pr_err("process_incomplete_cmd fail with result: %d\n",
  3216. resp.result);
  3217. }
  3218. if (req.cmd_id == QSEOS_RPMB_CHECK_PROV_STATUS_COMMAND) {
  3219. pr_warn("RPMB key status is 0x%x\n", resp.result);
  3220. if (put_user(resp.result,
  3221. (uint32_t __user *)req.resp_buf)) {
  3222. ret = -EINVAL;
  3223. goto exit;
  3224. }
  3225. ret = 0;
  3226. }
  3227. break;
  3228. case QSEOS_RESULT_FAILURE:
  3229. pr_err("scm call failed with resp.result: %d\n", resp.result);
  3230. ret = -EINVAL;
  3231. break;
  3232. default:
  3233. pr_err("Response result %d not supported\n",
  3234. resp.result);
  3235. ret = -EINVAL;
  3236. break;
  3237. }
  3238. exit:
  3239. if (!qseecom.support_bus_scaling) {
  3240. qsee_disable_clock_vote(data, CLK_DFAB);
  3241. qsee_disable_clock_vote(data, CLK_SFPB);
  3242. } else {
  3243. __qseecom_add_bw_scale_down_timer(
  3244. QSEECOM_SEND_CMD_CRYPTO_TIMEOUT);
  3245. }
  3246. return ret;
  3247. }
  3248. static int __validate_send_cmd_inputs(struct qseecom_dev_handle *data,
  3249. struct qseecom_send_cmd_req *req)
  3250. {
  3251. if (!data || !data->client.sb_virt) {
  3252. pr_err("Client or client buf is not initialized\n");
  3253. return -EINVAL;
  3254. }
  3255. if (((req->resp_buf == NULL) && (req->resp_len != 0)) ||
  3256. (req->cmd_req_buf == NULL)) {
  3257. pr_err("cmd buffer or response buffer is null\n");
  3258. return -EINVAL;
  3259. }
  3260. if (((uintptr_t)req->cmd_req_buf <
  3261. data->client.user_virt_sb_base) ||
  3262. ((uintptr_t)req->cmd_req_buf >=
  3263. (data->client.user_virt_sb_base + data->client.sb_length))) {
  3264. pr_err("cmd buffer address not within shared bufffer\n");
  3265. return -EINVAL;
  3266. }
  3267. if (((uintptr_t)req->resp_buf <
  3268. data->client.user_virt_sb_base) ||
  3269. ((uintptr_t)req->resp_buf >=
  3270. (data->client.user_virt_sb_base + data->client.sb_length))) {
  3271. pr_err("response buffer address not within shared bufffer\n");
  3272. return -EINVAL;
  3273. }
  3274. if ((req->cmd_req_len == 0) ||
  3275. (req->cmd_req_len > data->client.sb_length) ||
  3276. (req->resp_len > data->client.sb_length)) {
  3277. pr_err("cmd buf length or response buf length not valid\n");
  3278. return -EINVAL;
  3279. }
  3280. if (req->cmd_req_len > UINT_MAX - req->resp_len) {
  3281. pr_err("Integer overflow detected in req_len & rsp_len\n");
  3282. return -EINVAL;
  3283. }
  3284. if ((req->cmd_req_len + req->resp_len) > data->client.sb_length) {
  3285. pr_debug("Not enough memory to fit cmd_buf.\n");
  3286. pr_debug("resp_buf. Required: %u, Available: %zu\n",
  3287. (req->cmd_req_len + req->resp_len),
  3288. data->client.sb_length);
  3289. return -ENOMEM;
  3290. }
  3291. if ((uintptr_t)req->cmd_req_buf > (ULONG_MAX - req->cmd_req_len)) {
  3292. pr_err("Integer overflow in req_len & cmd_req_buf\n");
  3293. return -EINVAL;
  3294. }
  3295. if ((uintptr_t)req->resp_buf > (ULONG_MAX - req->resp_len)) {
  3296. pr_err("Integer overflow in resp_len & resp_buf\n");
  3297. return -EINVAL;
  3298. }
  3299. if (data->client.user_virt_sb_base >
  3300. (ULONG_MAX - data->client.sb_length)) {
  3301. pr_err("Integer overflow in user_virt_sb_base & sb_length\n");
  3302. return -EINVAL;
  3303. }
  3304. if ((((uintptr_t)req->cmd_req_buf + req->cmd_req_len) >
  3305. ((uintptr_t)data->client.user_virt_sb_base +
  3306. data->client.sb_length)) ||
  3307. (((uintptr_t)req->resp_buf + req->resp_len) >
  3308. ((uintptr_t)data->client.user_virt_sb_base +
  3309. data->client.sb_length))) {
  3310. pr_err("cmd buf or resp buf is out of shared buffer region\n");
  3311. return -EINVAL;
  3312. }
  3313. return 0;
  3314. }
  3315. static int __qseecom_process_reentrancy(struct qseecom_command_scm_resp *resp,
  3316. struct qseecom_registered_app_list *ptr_app,
  3317. struct qseecom_dev_handle *data)
  3318. {
  3319. int ret = 0;
  3320. switch (resp->result) {
  3321. case QSEOS_RESULT_BLOCKED_ON_LISTENER:
  3322. pr_warn("App(%d) %s is blocked on listener %d\n",
  3323. data->client.app_id, data->client.app_name,
  3324. resp->data);
  3325. ret = __qseecom_process_reentrancy_blocked_on_listener(
  3326. resp, ptr_app, data);
  3327. if (ret) {
  3328. pr_err("failed to process App(%d) %s is blocked on listener %d\n",
  3329. data->client.app_id, data->client.app_name, resp->data);
  3330. return ret;
  3331. }
  3332. fallthrough;
  3333. /* fall through to process incomplete request */
  3334. case QSEOS_RESULT_INCOMPLETE:
  3335. qseecom.app_block_ref_cnt++;
  3336. ptr_app->app_blocked = true;
  3337. ret = __qseecom_reentrancy_process_incomplete_cmd(data, resp);
  3338. ptr_app->app_blocked = false;
  3339. qseecom.app_block_ref_cnt--;
  3340. wake_up_interruptible_all(&qseecom.app_block_wq);
  3341. if (ret)
  3342. pr_err("process_incomplete_cmd failed err: %d\n",
  3343. ret);
  3344. return ret;
  3345. case QSEOS_RESULT_SUCCESS:
  3346. return ret;
  3347. default:
  3348. pr_err("Response result %d not supported\n",
  3349. resp->result);
  3350. return -EINVAL;
  3351. }
  3352. return ret;
  3353. }
  3354. static int __qseecom_send_cmd(struct qseecom_dev_handle *data,
  3355. struct qseecom_send_cmd_req *req,
  3356. bool is_phys_adr)
  3357. {
  3358. int ret = 0;
  3359. u32 reqd_len_sb_in = 0;
  3360. struct qseecom_client_send_data_ireq send_data_req = {0};
  3361. struct qseecom_client_send_data_64bit_ireq send_data_req_64bit = {0};
  3362. struct qseecom_command_scm_resp resp;
  3363. unsigned long flags;
  3364. struct qseecom_registered_app_list *ptr_app;
  3365. bool found_app = false;
  3366. void *cmd_buf = NULL;
  3367. size_t cmd_len;
  3368. reqd_len_sb_in = req->cmd_req_len + req->resp_len;
  3369. /* find app_id & img_name from list */
  3370. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
  3371. list_for_each_entry(ptr_app, &qseecom.registered_app_list_head,
  3372. list) {
  3373. if ((ptr_app->app_id == data->client.app_id) &&
  3374. (!strcmp(ptr_app->app_name, data->client.app_name))) {
  3375. found_app = true;
  3376. break;
  3377. }
  3378. }
  3379. spin_unlock_irqrestore(&qseecom.registered_app_list_lock, flags);
  3380. if (!found_app) {
  3381. pr_err("app_id %d (%s) is not found\n", data->client.app_id,
  3382. (char *)data->client.app_name);
  3383. return -ENOENT;
  3384. }
  3385. if (__qseecom_find_pending_unload_app(data->client.app_id,
  3386. data->client.app_name)) {
  3387. pr_err("app %d (%s) unload is pending\n",
  3388. data->client.app_id, data->client.app_name);
  3389. return -ENOENT;
  3390. }
  3391. if (qseecom.qsee_version < QSEE_VERSION_40) {
  3392. send_data_req.app_id = data->client.app_id;
  3393. if (!is_phys_adr) {
  3394. send_data_req.req_ptr =
  3395. (uint32_t)(__qseecom_uvirt_to_kphys
  3396. (data, (uintptr_t)req->cmd_req_buf));
  3397. send_data_req.rsp_ptr =
  3398. (uint32_t)(__qseecom_uvirt_to_kphys(
  3399. data, (uintptr_t)req->resp_buf));
  3400. } else {
  3401. send_data_req.req_ptr = (uint32_t)(uintptr_t)req->cmd_req_buf;
  3402. send_data_req.rsp_ptr = (uint32_t)(uintptr_t)req->resp_buf;
  3403. }
  3404. send_data_req.req_len = req->cmd_req_len;
  3405. send_data_req.rsp_len = req->resp_len;
  3406. send_data_req.sglistinfo_ptr =
  3407. (uint32_t)data->sglistinfo_shm.paddr;
  3408. send_data_req.sglistinfo_len = SGLISTINFO_TABLE_SIZE;
  3409. qtee_shmbridge_flush_shm_buf(&data->sglistinfo_shm);
  3410. cmd_buf = (void *)&send_data_req;
  3411. cmd_len = sizeof(struct qseecom_client_send_data_ireq);
  3412. } else {
  3413. send_data_req_64bit.app_id = data->client.app_id;
  3414. if (!is_phys_adr) {
  3415. send_data_req_64bit.req_ptr =
  3416. __qseecom_uvirt_to_kphys(data,
  3417. (uintptr_t)req->cmd_req_buf);
  3418. send_data_req_64bit.rsp_ptr =
  3419. __qseecom_uvirt_to_kphys(data,
  3420. (uintptr_t)req->resp_buf);
  3421. } else {
  3422. send_data_req_64bit.req_ptr =
  3423. (uintptr_t)req->cmd_req_buf;
  3424. send_data_req_64bit.rsp_ptr =
  3425. (uintptr_t)req->resp_buf;
  3426. }
  3427. send_data_req_64bit.req_len = req->cmd_req_len;
  3428. send_data_req_64bit.rsp_len = req->resp_len;
  3429. /* check if 32bit app's phys_addr region is under 4GB.*/
  3430. if ((data->client.app_arch == ELFCLASS32) &&
  3431. ((send_data_req_64bit.req_ptr >=
  3432. PHY_ADDR_4G - send_data_req_64bit.req_len) ||
  3433. (send_data_req_64bit.rsp_ptr >=
  3434. PHY_ADDR_4G - send_data_req_64bit.rsp_len))){
  3435. pr_err("32bit app %s PA exceeds 4G: req_ptr=%llx, req_len=%x, rsp_ptr=%llx, rsp_len=%x\n",
  3436. data->client.app_name,
  3437. send_data_req_64bit.req_ptr,
  3438. send_data_req_64bit.req_len,
  3439. send_data_req_64bit.rsp_ptr,
  3440. send_data_req_64bit.rsp_len);
  3441. return -EFAULT;
  3442. }
  3443. send_data_req_64bit.sglistinfo_ptr =
  3444. (uint64_t)data->sglistinfo_shm.paddr;
  3445. send_data_req_64bit.sglistinfo_len = SGLISTINFO_TABLE_SIZE;
  3446. qtee_shmbridge_flush_shm_buf(&data->sglistinfo_shm);
  3447. cmd_buf = (void *)&send_data_req_64bit;
  3448. cmd_len = sizeof(struct qseecom_client_send_data_64bit_ireq);
  3449. }
  3450. if (!qseecom.whitelist_support || data->use_legacy_cmd)
  3451. *(uint32_t *)cmd_buf = QSEOS_CLIENT_SEND_DATA_COMMAND;
  3452. else
  3453. *(uint32_t *)cmd_buf = QSEOS_CLIENT_SEND_DATA_COMMAND_WHITELIST;
  3454. if (data->client.dmabuf) {
  3455. ret = qseecom_dmabuf_cache_operations(data->client.dmabuf,
  3456. QSEECOM_CACHE_CLEAN);
  3457. if (ret) {
  3458. pr_err("cache operation failed %d\n", ret);
  3459. return ret;
  3460. }
  3461. }
  3462. __qseecom_reentrancy_check_if_this_app_blocked(ptr_app);
  3463. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1,
  3464. cmd_buf, cmd_len,
  3465. &resp, sizeof(resp));
  3466. if (ret) {
  3467. pr_err("scm_call() failed with err: %d (app_id = %d)\n",
  3468. ret, data->client.app_id);
  3469. goto exit;
  3470. }
  3471. if (qseecom.qsee_reentrancy_support) {
  3472. ret = __qseecom_process_reentrancy(&resp, ptr_app, data);
  3473. if (ret)
  3474. goto exit;
  3475. } else {
  3476. if (resp.result == QSEOS_RESULT_INCOMPLETE) {
  3477. ret = __qseecom_process_incomplete_cmd(data, &resp);
  3478. if (ret) {
  3479. pr_err("process_incomplete_cmd failed err: %d\n",
  3480. ret);
  3481. goto exit;
  3482. }
  3483. } else {
  3484. if (resp.result != QSEOS_RESULT_SUCCESS) {
  3485. pr_err("Response result %d not supported\n",
  3486. resp.result);
  3487. ret = -EINVAL;
  3488. goto exit;
  3489. }
  3490. }
  3491. }
  3492. if (data->client.dmabuf) {
  3493. ret = qseecom_dmabuf_cache_operations(data->client.dmabuf,
  3494. QSEECOM_CACHE_INVALIDATE);
  3495. if (ret) {
  3496. pr_err("cache operation failed %d\n", ret);
  3497. goto exit;
  3498. }
  3499. }
  3500. exit:
  3501. return ret;
  3502. }
  3503. static int qseecom_send_cmd(struct qseecom_dev_handle *data, void __user *argp)
  3504. {
  3505. int ret = 0;
  3506. struct qseecom_send_cmd_req req;
  3507. ret = copy_from_user(&req, argp, sizeof(req));
  3508. if (ret) {
  3509. pr_err("copy_from_user failed\n");
  3510. return ret;
  3511. }
  3512. if (__validate_send_cmd_inputs(data, &req))
  3513. return -EINVAL;
  3514. ret = __qseecom_send_cmd(data, &req, false);
  3515. return ret;
  3516. }
  3517. static int __boundary_checks_offset(struct qseecom_send_modfd_cmd_req *req,
  3518. struct qseecom_send_modfd_listener_resp *lstnr_resp,
  3519. struct qseecom_dev_handle *data, int i, size_t size)
  3520. {
  3521. char *curr_field = NULL;
  3522. char *temp_field = NULL;
  3523. int j = 0;
  3524. if ((data->type != QSEECOM_LISTENER_SERVICE) &&
  3525. (req->ifd_data[i].fd > 0)) {
  3526. if ((req->cmd_req_len < size) ||
  3527. (req->ifd_data[i].cmd_buf_offset >
  3528. req->cmd_req_len - size)) {
  3529. pr_err("Invalid offset (req len) 0x%x\n",
  3530. req->ifd_data[i].cmd_buf_offset);
  3531. return -EINVAL;
  3532. }
  3533. curr_field = (char *) (req->cmd_req_buf +
  3534. req->ifd_data[i].cmd_buf_offset);
  3535. for (j = 0; j < MAX_ION_FD; j++) {
  3536. if ((req->ifd_data[j].fd > 0) && i != j) {
  3537. temp_field = (char *) (req->cmd_req_buf +
  3538. req->ifd_data[j].cmd_buf_offset);
  3539. if (temp_field >= curr_field && temp_field <
  3540. (curr_field + size)) {
  3541. pr_err("Invalid field offset 0x%x\n",
  3542. req->ifd_data[i].cmd_buf_offset);
  3543. return -EINVAL;
  3544. }
  3545. }
  3546. }
  3547. } else if ((data->type == QSEECOM_LISTENER_SERVICE) &&
  3548. (lstnr_resp->ifd_data[i].fd > 0)) {
  3549. if ((lstnr_resp->resp_len < size) ||
  3550. (lstnr_resp->ifd_data[i].cmd_buf_offset >
  3551. lstnr_resp->resp_len - size)) {
  3552. pr_err("Invalid offset (lstnr resp len) 0x%x\n",
  3553. lstnr_resp->ifd_data[i].cmd_buf_offset);
  3554. return -EINVAL;
  3555. }
  3556. curr_field = (char *) (lstnr_resp->resp_buf_ptr +
  3557. lstnr_resp->ifd_data[i].cmd_buf_offset);
  3558. for (j = 0; j < MAX_ION_FD; j++) {
  3559. if ((lstnr_resp->ifd_data[j].fd > 0) && i != j) {
  3560. temp_field = (char *) lstnr_resp->resp_buf_ptr +
  3561. lstnr_resp->ifd_data[j].cmd_buf_offset;
  3562. if (temp_field >= curr_field && temp_field <
  3563. (curr_field + size)) {
  3564. pr_err("Invalid lstnr field offset 0x%x\n",
  3565. lstnr_resp->ifd_data[i].cmd_buf_offset);
  3566. return -EINVAL;
  3567. }
  3568. }
  3569. }
  3570. }
  3571. return 0;
  3572. }
  3573. static int __qseecom_update_cmd_buf(void *msg, bool cleanup,
  3574. struct qseecom_dev_handle *data)
  3575. {
  3576. char *field;
  3577. int ret = 0;
  3578. int i = 0;
  3579. uint32_t len = 0;
  3580. struct scatterlist *sg;
  3581. struct qseecom_send_modfd_cmd_req *req = NULL;
  3582. struct qseecom_send_modfd_listener_resp *lstnr_resp = NULL;
  3583. struct qseecom_registered_listener_list *this_lstnr = NULL;
  3584. uint32_t offset;
  3585. struct sg_table *sg_ptr = NULL;
  3586. int ion_fd = -1;
  3587. struct dma_buf *dmabuf = NULL;
  3588. struct dma_buf_attachment *attach = NULL;
  3589. if ((data->type != QSEECOM_LISTENER_SERVICE) &&
  3590. (data->type != QSEECOM_CLIENT_APP))
  3591. return -EFAULT;
  3592. if (msg == NULL) {
  3593. pr_err("Invalid address\n");
  3594. return -EINVAL;
  3595. }
  3596. if (data->type == QSEECOM_LISTENER_SERVICE) {
  3597. lstnr_resp = (struct qseecom_send_modfd_listener_resp *)msg;
  3598. this_lstnr = __qseecom_find_svc(data->listener.id);
  3599. if (IS_ERR_OR_NULL(this_lstnr)) {
  3600. pr_err("Invalid listener ID\n");
  3601. return -ENOMEM;
  3602. }
  3603. } else {
  3604. req = (struct qseecom_send_modfd_cmd_req *)msg;
  3605. }
  3606. for (i = 0; i < MAX_ION_FD; i++) {
  3607. if ((data->type != QSEECOM_LISTENER_SERVICE) &&
  3608. (req->ifd_data[i].fd > 0)) {
  3609. ion_fd = req->ifd_data[i].fd;
  3610. field = (char *) req->cmd_req_buf +
  3611. req->ifd_data[i].cmd_buf_offset;
  3612. } else if ((data->type == QSEECOM_LISTENER_SERVICE) &&
  3613. (lstnr_resp->ifd_data[i].fd > 0)) {
  3614. ion_fd = lstnr_resp->ifd_data[i].fd;
  3615. field = lstnr_resp->resp_buf_ptr +
  3616. lstnr_resp->ifd_data[i].cmd_buf_offset;
  3617. } else {
  3618. continue;
  3619. }
  3620. /* Populate the cmd data structure with the phys_addr */
  3621. ret = qseecom_dmabuf_map(ion_fd, &sg_ptr, &attach, &dmabuf);
  3622. if (ret) {
  3623. pr_err("IOn client could not retrieve sg table\n");
  3624. goto err;
  3625. }
  3626. if (sg_ptr->nents == 0) {
  3627. pr_err("Num of scattered entries is 0\n");
  3628. goto err;
  3629. }
  3630. if (sg_ptr->nents > QSEECOM_MAX_SG_ENTRY) {
  3631. pr_err("Num of scattered entries\n");
  3632. pr_err(" (%d) is greater than max supported %d\n",
  3633. sg_ptr->nents, QSEECOM_MAX_SG_ENTRY);
  3634. goto err;
  3635. }
  3636. sg = sg_ptr->sgl;
  3637. if (sg_ptr->nents == 1) {
  3638. uint32_t *update;
  3639. if (__boundary_checks_offset(req, lstnr_resp, data, i, sizeof(uint32_t)))
  3640. goto err;
  3641. if ((data->type == QSEECOM_CLIENT_APP &&
  3642. (data->client.app_arch == ELFCLASS32 ||
  3643. data->client.app_arch == ELFCLASS64)) ||
  3644. (data->type == QSEECOM_LISTENER_SERVICE)) {
  3645. /*
  3646. * Check if sg list phy add region is under 4GB
  3647. */
  3648. if ((qseecom.qsee_version >= QSEE_VERSION_40) &&
  3649. (!cleanup) &&
  3650. ((uint64_t)sg_dma_address(sg_ptr->sgl)
  3651. >= PHY_ADDR_4G - sg->length)) {
  3652. pr_err("App %s sgl PA exceeds 4G: phy_addr=%pKad, len=%x\n",
  3653. data->client.app_name,
  3654. &(sg_dma_address(sg_ptr->sgl)),
  3655. sg->length);
  3656. goto err;
  3657. }
  3658. update = (uint32_t *) field;
  3659. *update = cleanup ? 0 :
  3660. (uint32_t)sg_dma_address(sg_ptr->sgl);
  3661. } else {
  3662. pr_err("QSEE app arch %u is not supported\n",
  3663. data->client.app_arch);
  3664. goto err;
  3665. }
  3666. len += (uint32_t)sg->length;
  3667. } else {
  3668. struct qseecom_sg_entry *update;
  3669. int j = 0;
  3670. if (__boundary_checks_offset(req, lstnr_resp, data, i,
  3671. (SG_ENTRY_SZ * sg_ptr->nents)))
  3672. goto err;
  3673. if ((data->type == QSEECOM_CLIENT_APP &&
  3674. (data->client.app_arch == ELFCLASS32 ||
  3675. data->client.app_arch == ELFCLASS64)) ||
  3676. (data->type == QSEECOM_LISTENER_SERVICE)) {
  3677. update = (struct qseecom_sg_entry *)field;
  3678. for (j = 0; j < sg_ptr->nents; j++) {
  3679. /*
  3680. * Check if sg list PA is under 4GB
  3681. */
  3682. if ((qseecom.qsee_version >=
  3683. QSEE_VERSION_40) &&
  3684. (!cleanup) &&
  3685. ((uint64_t)(sg_dma_address(sg))
  3686. >= PHY_ADDR_4G - sg->length)) {
  3687. pr_err("App %s sgl PA exceeds 4G: phy_addr=%pKad, len=%x\n",
  3688. data->client.app_name,
  3689. &(sg_dma_address(sg)),
  3690. sg->length);
  3691. goto err;
  3692. }
  3693. update->phys_addr = cleanup ? 0 :
  3694. (uint32_t)sg_dma_address(sg);
  3695. update->len = cleanup ? 0 : sg->length;
  3696. update++;
  3697. len += sg->length;
  3698. sg = sg_next(sg);
  3699. }
  3700. } else {
  3701. pr_err("QSEE app arch %u is not supported\n",
  3702. data->client.app_arch);
  3703. goto err;
  3704. }
  3705. }
  3706. if (cleanup) {
  3707. ret = qseecom_dmabuf_cache_operations(dmabuf,
  3708. QSEECOM_CACHE_INVALIDATE);
  3709. if (ret) {
  3710. pr_err("cache operation failed %d\n", ret);
  3711. goto err;
  3712. }
  3713. } else {
  3714. ret = qseecom_dmabuf_cache_operations(dmabuf,
  3715. QSEECOM_CACHE_CLEAN);
  3716. if (ret) {
  3717. pr_err("cache operation failed %d\n", ret);
  3718. goto err;
  3719. }
  3720. if (data->type == QSEECOM_CLIENT_APP) {
  3721. offset = req->ifd_data[i].cmd_buf_offset;
  3722. data->sglistinfo_ptr[i].indexAndFlags =
  3723. SGLISTINFO_SET_INDEX_FLAG(
  3724. (sg_ptr->nents == 1), 0, offset);
  3725. data->sglistinfo_ptr[i].sizeOrCount =
  3726. (sg_ptr->nents == 1) ?
  3727. sg->length : sg_ptr->nents;
  3728. data->sglist_cnt = i + 1;
  3729. } else {
  3730. offset = (lstnr_resp->ifd_data[i].cmd_buf_offset
  3731. + (uintptr_t)lstnr_resp->resp_buf_ptr -
  3732. (uintptr_t)this_lstnr->sb_virt);
  3733. this_lstnr->sglistinfo_ptr[i].indexAndFlags =
  3734. SGLISTINFO_SET_INDEX_FLAG(
  3735. (sg_ptr->nents == 1), 0, offset);
  3736. this_lstnr->sglistinfo_ptr[i].sizeOrCount =
  3737. (sg_ptr->nents == 1) ?
  3738. sg->length : sg_ptr->nents;
  3739. this_lstnr->sglist_cnt = i + 1;
  3740. }
  3741. }
  3742. /* Deallocate the kbuf */
  3743. qseecom_dmabuf_unmap(sg_ptr, attach, dmabuf);
  3744. sg_ptr = NULL;
  3745. dmabuf = NULL;
  3746. attach = NULL;
  3747. }
  3748. return ret;
  3749. err:
  3750. if (!IS_ERR_OR_NULL(sg_ptr)) {
  3751. qseecom_dmabuf_unmap(sg_ptr, attach, dmabuf);
  3752. MAKE_NULL(sg_ptr, attach, dmabuf);
  3753. }
  3754. return -ENOMEM;
  3755. }
  3756. static int __qseecom_allocate_sg_list_buffer(struct qseecom_dev_handle *data,
  3757. char *field, uint32_t fd_idx, struct sg_table *sg_ptr)
  3758. {
  3759. struct scatterlist *sg = sg_ptr->sgl;
  3760. struct qseecom_sg_entry_64bit *sg_entry;
  3761. struct qseecom_sg_list_buf_hdr_64bit *buf_hdr;
  3762. void *buf;
  3763. uint i;
  3764. size_t size;
  3765. dma_addr_t coh_pmem;
  3766. if (fd_idx >= MAX_ION_FD) {
  3767. pr_err("fd_idx [%d] is invalid\n", fd_idx);
  3768. return -ENOMEM;
  3769. }
  3770. buf_hdr = (struct qseecom_sg_list_buf_hdr_64bit *)field;
  3771. memset((void *)buf_hdr, 0, QSEECOM_SG_LIST_BUF_HDR_SZ_64BIT);
  3772. /* Allocate a contiguous kernel buffer */
  3773. size = sg_ptr->nents * SG_ENTRY_SZ_64BIT;
  3774. size = (size + PAGE_SIZE) & PAGE_MASK;
  3775. buf = dma_alloc_coherent(qseecom.dev,
  3776. size, &coh_pmem, GFP_KERNEL);
  3777. if (buf == NULL)
  3778. return -ENOMEM;
  3779. /* update qseecom_sg_list_buf_hdr_64bit */
  3780. buf_hdr->version = QSEECOM_SG_LIST_BUF_FORMAT_VERSION_2;
  3781. buf_hdr->new_buf_phys_addr = coh_pmem;
  3782. buf_hdr->nents_total = sg_ptr->nents;
  3783. /* save the left sg entries into new allocated buf */
  3784. sg_entry = (struct qseecom_sg_entry_64bit *)buf;
  3785. for (i = 0; i < sg_ptr->nents; i++) {
  3786. sg_entry->phys_addr = (uint64_t)sg_dma_address(sg);
  3787. sg_entry->len = sg->length;
  3788. sg_entry++;
  3789. sg = sg_next(sg);
  3790. }
  3791. data->client.sec_buf_fd[fd_idx].is_sec_buf_fd = true;
  3792. data->client.sec_buf_fd[fd_idx].vbase = buf;
  3793. data->client.sec_buf_fd[fd_idx].pbase = coh_pmem;
  3794. data->client.sec_buf_fd[fd_idx].size = size;
  3795. return 0;
  3796. }
  3797. static int __qseecom_update_cmd_buf_64(void *msg, bool cleanup,
  3798. struct qseecom_dev_handle *data)
  3799. {
  3800. char *field;
  3801. int ret = 0;
  3802. int i = 0;
  3803. uint32_t len = 0;
  3804. struct scatterlist *sg;
  3805. struct qseecom_send_modfd_cmd_req *req = NULL;
  3806. struct qseecom_send_modfd_listener_resp *lstnr_resp = NULL;
  3807. struct qseecom_registered_listener_list *this_lstnr = NULL;
  3808. uint32_t offset;
  3809. struct sg_table *sg_ptr;
  3810. int ion_fd = -1;
  3811. struct dma_buf *dmabuf = NULL;
  3812. struct dma_buf_attachment *attach = NULL;
  3813. if ((data->type != QSEECOM_LISTENER_SERVICE) &&
  3814. (data->type != QSEECOM_CLIENT_APP))
  3815. return -EFAULT;
  3816. if (msg == NULL) {
  3817. pr_err("Invalid address\n");
  3818. return -EINVAL;
  3819. }
  3820. if (data->type == QSEECOM_LISTENER_SERVICE) {
  3821. lstnr_resp = (struct qseecom_send_modfd_listener_resp *)msg;
  3822. this_lstnr = __qseecom_find_svc(data->listener.id);
  3823. if (IS_ERR_OR_NULL(this_lstnr)) {
  3824. pr_err("Invalid listener ID\n");
  3825. return -ENOMEM;
  3826. }
  3827. } else {
  3828. req = (struct qseecom_send_modfd_cmd_req *)msg;
  3829. }
  3830. for (i = 0; i < MAX_ION_FD; i++) {
  3831. if ((data->type != QSEECOM_LISTENER_SERVICE) &&
  3832. (req->ifd_data[i].fd > 0)) {
  3833. ion_fd = req->ifd_data[i].fd;
  3834. field = (char *) req->cmd_req_buf +
  3835. req->ifd_data[i].cmd_buf_offset;
  3836. } else if ((data->type == QSEECOM_LISTENER_SERVICE) &&
  3837. (lstnr_resp->ifd_data[i].fd > 0)) {
  3838. ion_fd = lstnr_resp->ifd_data[i].fd;
  3839. field = lstnr_resp->resp_buf_ptr +
  3840. lstnr_resp->ifd_data[i].cmd_buf_offset;
  3841. } else {
  3842. continue;
  3843. }
  3844. /* Populate the cmd data structure with the phys_addr */
  3845. ret = qseecom_dmabuf_map(ion_fd, &sg_ptr, &attach, &dmabuf);
  3846. if (ret) {
  3847. pr_err("IOn client could not retrieve sg table\n");
  3848. goto err;
  3849. }
  3850. if (sg_ptr->nents == 0) {
  3851. pr_err("Num of scattered entries is 0\n");
  3852. goto err;
  3853. }
  3854. if (sg_ptr->nents > QSEECOM_MAX_SG_ENTRY) {
  3855. pr_warn("Num of scattered entries\n");
  3856. pr_warn(" (%d) is greater than %d\n",
  3857. sg_ptr->nents, QSEECOM_MAX_SG_ENTRY);
  3858. if (cleanup) {
  3859. if (data->client.sec_buf_fd[i].is_sec_buf_fd &&
  3860. data->client.sec_buf_fd[i].vbase)
  3861. dma_free_coherent(qseecom.dev,
  3862. data->client.sec_buf_fd[i].size,
  3863. data->client.sec_buf_fd[i].vbase,
  3864. data->client.sec_buf_fd[i].pbase);
  3865. } else {
  3866. ret = __qseecom_allocate_sg_list_buffer(data,
  3867. field, i, sg_ptr);
  3868. if (ret) {
  3869. pr_err("Failed to allocate sg list buffer\n");
  3870. goto err;
  3871. }
  3872. }
  3873. len = QSEECOM_SG_LIST_BUF_HDR_SZ_64BIT;
  3874. sg = sg_ptr->sgl;
  3875. goto cleanup;
  3876. }
  3877. sg = sg_ptr->sgl;
  3878. if (sg_ptr->nents == 1) {
  3879. uint64_t *update_64bit;
  3880. if (__boundary_checks_offset(req, lstnr_resp, data, i, sizeof(uint64_t)))
  3881. goto err;
  3882. /* 64bit app uses 64bit address */
  3883. update_64bit = (uint64_t *) field;
  3884. *update_64bit = cleanup ? 0 :
  3885. (uint64_t)sg_dma_address(sg_ptr->sgl);
  3886. len += (uint32_t)sg->length;
  3887. } else {
  3888. struct qseecom_sg_entry_64bit *update_64bit;
  3889. int j = 0;
  3890. if (__boundary_checks_offset(req, lstnr_resp, data, i,
  3891. (SG_ENTRY_SZ_64BIT * sg_ptr->nents)))
  3892. goto err;
  3893. /* 64bit app uses 64bit address */
  3894. update_64bit = (struct qseecom_sg_entry_64bit *)field;
  3895. for (j = 0; j < sg_ptr->nents; j++) {
  3896. update_64bit->phys_addr = cleanup ? 0 :
  3897. (uint64_t)sg_dma_address(sg);
  3898. update_64bit->len = cleanup ? 0 :
  3899. (uint32_t)sg->length;
  3900. update_64bit++;
  3901. len += sg->length;
  3902. sg = sg_next(sg);
  3903. }
  3904. }
  3905. cleanup:
  3906. if (cleanup) {
  3907. ret = qseecom_dmabuf_cache_operations(dmabuf,
  3908. QSEECOM_CACHE_INVALIDATE);
  3909. if (ret) {
  3910. pr_err("cache operation failed %d\n", ret);
  3911. goto err;
  3912. }
  3913. } else {
  3914. ret = qseecom_dmabuf_cache_operations(dmabuf,
  3915. QSEECOM_CACHE_CLEAN);
  3916. if (ret) {
  3917. pr_err("cache operation failed %d\n", ret);
  3918. goto err;
  3919. }
  3920. if (data->type == QSEECOM_CLIENT_APP) {
  3921. offset = req->ifd_data[i].cmd_buf_offset;
  3922. data->sglistinfo_ptr[i].indexAndFlags =
  3923. SGLISTINFO_SET_INDEX_FLAG(
  3924. (sg_ptr->nents == 1), 1, offset);
  3925. data->sglistinfo_ptr[i].sizeOrCount =
  3926. (sg_ptr->nents == 1) ?
  3927. sg->length : sg_ptr->nents;
  3928. data->sglist_cnt = i + 1;
  3929. } else {
  3930. offset = (lstnr_resp->ifd_data[i].cmd_buf_offset
  3931. + (uintptr_t)lstnr_resp->resp_buf_ptr -
  3932. (uintptr_t)this_lstnr->sb_virt);
  3933. this_lstnr->sglistinfo_ptr[i].indexAndFlags =
  3934. SGLISTINFO_SET_INDEX_FLAG(
  3935. (sg_ptr->nents == 1), 1, offset);
  3936. this_lstnr->sglistinfo_ptr[i].sizeOrCount =
  3937. (sg_ptr->nents == 1) ?
  3938. sg->length : sg_ptr->nents;
  3939. this_lstnr->sglist_cnt = i + 1;
  3940. }
  3941. }
  3942. /* unmap the dmabuf */
  3943. qseecom_dmabuf_unmap(sg_ptr, attach, dmabuf);
  3944. sg_ptr = NULL;
  3945. dmabuf = NULL;
  3946. attach = NULL;
  3947. }
  3948. return ret;
  3949. err:
  3950. for (i = 0; i < MAX_ION_FD; i++)
  3951. if (data->client.sec_buf_fd[i].is_sec_buf_fd &&
  3952. data->client.sec_buf_fd[i].vbase)
  3953. dma_free_coherent(qseecom.dev,
  3954. data->client.sec_buf_fd[i].size,
  3955. data->client.sec_buf_fd[i].vbase,
  3956. data->client.sec_buf_fd[i].pbase);
  3957. if (!IS_ERR_OR_NULL(sg_ptr)) {
  3958. qseecom_dmabuf_unmap(sg_ptr, attach, dmabuf);
  3959. MAKE_NULL(sg_ptr, attach, dmabuf);
  3960. }
  3961. return -ENOMEM;
  3962. }
  3963. static int __qseecom_send_modfd_cmd(struct qseecom_dev_handle *data,
  3964. void __user *argp,
  3965. bool is_64bit_addr)
  3966. {
  3967. int ret = 0;
  3968. int i;
  3969. struct qseecom_send_modfd_cmd_req req;
  3970. struct qseecom_send_cmd_req send_cmd_req;
  3971. void *origin_req_buf_kvirt, *origin_rsp_buf_kvirt;
  3972. phys_addr_t pa;
  3973. u8 *va = NULL;
  3974. ret = copy_from_user(&req, argp, sizeof(req));
  3975. if (ret) {
  3976. pr_err("copy_from_user failed\n");
  3977. return ret;
  3978. }
  3979. send_cmd_req.cmd_req_buf = req.cmd_req_buf;
  3980. send_cmd_req.cmd_req_len = req.cmd_req_len;
  3981. send_cmd_req.resp_buf = req.resp_buf;
  3982. send_cmd_req.resp_len = req.resp_len;
  3983. if (__validate_send_cmd_inputs(data, &send_cmd_req))
  3984. return -EINVAL;
  3985. /* validate offsets */
  3986. for (i = 0; i < MAX_ION_FD; i++) {
  3987. if (req.ifd_data[i].cmd_buf_offset >= req.cmd_req_len) {
  3988. pr_err("Invalid offset %d = 0x%x\n",
  3989. i, req.ifd_data[i].cmd_buf_offset);
  3990. return -EINVAL;
  3991. }
  3992. }
  3993. /*Back up original address */
  3994. origin_req_buf_kvirt = (void *)__qseecom_uvirt_to_kvirt(data,
  3995. (uintptr_t)req.cmd_req_buf);
  3996. origin_rsp_buf_kvirt = (void *)__qseecom_uvirt_to_kvirt(data,
  3997. (uintptr_t)req.resp_buf);
  3998. /* Allocate kernel buffer for request and response*/
  3999. ret = __qseecom_alloc_coherent_buf(req.cmd_req_len + req.resp_len,
  4000. &va, &pa);
  4001. if (ret) {
  4002. pr_err("Failed to allocate coherent buf, ret %d\n", ret);
  4003. return ret;
  4004. }
  4005. req.cmd_req_buf = va;
  4006. send_cmd_req.cmd_req_buf = (void *)pa;
  4007. req.resp_buf = va + req.cmd_req_len;
  4008. send_cmd_req.resp_buf = (void *)pa + req.cmd_req_len;
  4009. /* Copy the data to kernel request and response buffers*/
  4010. memcpy(req.cmd_req_buf, origin_req_buf_kvirt, req.cmd_req_len);
  4011. memcpy(req.resp_buf, origin_rsp_buf_kvirt, req.resp_len);
  4012. if (!is_64bit_addr) {
  4013. ret = __qseecom_update_cmd_buf(&req, false, data);
  4014. if (ret)
  4015. goto out;
  4016. ret = __qseecom_send_cmd(data, &send_cmd_req, true);
  4017. if (ret)
  4018. goto out;
  4019. ret = __qseecom_update_cmd_buf(&req, true, data);
  4020. if (ret)
  4021. goto out;
  4022. } else {
  4023. ret = __qseecom_update_cmd_buf_64(&req, false, data);
  4024. if (ret)
  4025. goto out;
  4026. ret = __qseecom_send_cmd(data, &send_cmd_req, true);
  4027. if (ret)
  4028. goto out;
  4029. ret = __qseecom_update_cmd_buf_64(&req, true, data);
  4030. if (ret)
  4031. goto out;
  4032. }
  4033. /*Copy the response back to the userspace buffer*/
  4034. memcpy(origin_rsp_buf_kvirt, req.resp_buf, req.resp_len);
  4035. memcpy(origin_req_buf_kvirt, req.cmd_req_buf, req.cmd_req_len);
  4036. out:
  4037. if (req.cmd_req_buf)
  4038. __qseecom_free_coherent_buf(req.cmd_req_len + req.resp_len,
  4039. req.cmd_req_buf, (phys_addr_t)send_cmd_req.cmd_req_buf);
  4040. return ret;
  4041. }
  4042. static int qseecom_send_modfd_cmd(struct qseecom_dev_handle *data,
  4043. void __user *argp)
  4044. {
  4045. return __qseecom_send_modfd_cmd(data, argp, false);
  4046. }
  4047. static int qseecom_send_modfd_cmd_64(struct qseecom_dev_handle *data,
  4048. void __user *argp)
  4049. {
  4050. return __qseecom_send_modfd_cmd(data, argp, true);
  4051. }
  4052. static int __qseecom_listener_has_rcvd_req(struct qseecom_dev_handle *data,
  4053. struct qseecom_registered_listener_list *svc)
  4054. {
  4055. int ret;
  4056. ret = (svc->rcv_req_flag == 1);
  4057. return ret || data->abort;
  4058. }
  4059. static int qseecom_receive_req(struct qseecom_dev_handle *data)
  4060. {
  4061. int ret = 0;
  4062. struct qseecom_registered_listener_list *this_lstnr;
  4063. mutex_lock(&listener_access_lock);
  4064. this_lstnr = __qseecom_find_svc(data->listener.id);
  4065. if (!this_lstnr) {
  4066. pr_err("Invalid listener ID\n");
  4067. mutex_unlock(&listener_access_lock);
  4068. return -ENODATA;
  4069. }
  4070. mutex_unlock(&listener_access_lock);
  4071. while (1) {
  4072. if (wait_event_interruptible(this_lstnr->rcv_req_wq,
  4073. __qseecom_listener_has_rcvd_req(data,
  4074. this_lstnr))) {
  4075. pr_debug("Interrupted: exiting Listener Service = %d\n",
  4076. (uint32_t)data->listener.id);
  4077. /* woken up for different reason */
  4078. return -ERESTARTSYS;
  4079. }
  4080. if (data->abort) {
  4081. pr_err("Aborting Listener Service = %d\n",
  4082. (uint32_t)data->listener.id);
  4083. return -ENODEV;
  4084. }
  4085. mutex_lock(&listener_access_lock);
  4086. this_lstnr->rcv_req_flag = 0;
  4087. mutex_unlock(&listener_access_lock);
  4088. break;
  4089. }
  4090. return ret;
  4091. }
  4092. static bool __qseecom_is_fw_image_valid(const struct firmware *fw_entry)
  4093. {
  4094. unsigned char app_arch = 0;
  4095. struct elf32_hdr *ehdr;
  4096. struct elf64_hdr *ehdr64;
  4097. app_arch = *(unsigned char *)(fw_entry->data + EI_CLASS);
  4098. switch (app_arch) {
  4099. case ELFCLASS32: {
  4100. ehdr = (struct elf32_hdr *)fw_entry->data;
  4101. if (fw_entry->size < sizeof(*ehdr)) {
  4102. pr_err("%s: Not big enough to be an elf32 header\n",
  4103. qseecom.pdev->init_name);
  4104. return false;
  4105. }
  4106. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
  4107. pr_err("%s: Not an elf32 header\n",
  4108. qseecom.pdev->init_name);
  4109. return false;
  4110. }
  4111. if (ehdr->e_phnum == 0) {
  4112. pr_err("%s: No loadable segments\n",
  4113. qseecom.pdev->init_name);
  4114. return false;
  4115. }
  4116. if (sizeof(struct elf32_phdr) * ehdr->e_phnum +
  4117. sizeof(struct elf32_hdr) > fw_entry->size) {
  4118. pr_err("%s: Program headers not within mdt\n",
  4119. qseecom.pdev->init_name);
  4120. return false;
  4121. }
  4122. break;
  4123. }
  4124. case ELFCLASS64: {
  4125. ehdr64 = (struct elf64_hdr *)fw_entry->data;
  4126. if (fw_entry->size < sizeof(*ehdr64)) {
  4127. pr_err("%s: Not big enough to be an elf64 header\n",
  4128. qseecom.pdev->init_name);
  4129. return false;
  4130. }
  4131. if (memcmp(ehdr64->e_ident, ELFMAG, SELFMAG)) {
  4132. pr_err("%s: Not an elf64 header\n",
  4133. qseecom.pdev->init_name);
  4134. return false;
  4135. }
  4136. if (ehdr64->e_phnum == 0) {
  4137. pr_err("%s: No loadable segments\n",
  4138. qseecom.pdev->init_name);
  4139. return false;
  4140. }
  4141. if (sizeof(struct elf64_phdr) * ehdr64->e_phnum +
  4142. sizeof(struct elf64_hdr) > fw_entry->size) {
  4143. pr_err("%s: Program headers not within mdt\n",
  4144. qseecom.pdev->init_name);
  4145. return false;
  4146. }
  4147. break;
  4148. }
  4149. default: {
  4150. pr_err("QSEE app arch %u is not supported\n", app_arch);
  4151. return false;
  4152. }
  4153. }
  4154. return true;
  4155. }
  4156. static int __qseecom_get_fw_size(const char *appname, uint32_t *fw_size,
  4157. uint32_t *app_arch)
  4158. {
  4159. int ret = -1;
  4160. int i = 0, rc = 0;
  4161. const struct firmware *fw_entry = NULL;
  4162. char fw_name[MAX_APP_NAME_SIZE];
  4163. struct elf32_hdr *ehdr;
  4164. struct elf64_hdr *ehdr64;
  4165. int num_images = 0;
  4166. snprintf(fw_name, sizeof(fw_name), "%s.mdt", appname);
  4167. rc = firmware_request_nowarn(&fw_entry, fw_name, qseecom.pdev);
  4168. if (rc) {
  4169. pr_err("error with firmware_request_nowarn, rc = %d\n", rc);
  4170. ret = -EIO;
  4171. goto err;
  4172. }
  4173. if (!__qseecom_is_fw_image_valid(fw_entry)) {
  4174. ret = -EIO;
  4175. goto err;
  4176. }
  4177. *app_arch = *(unsigned char *)(fw_entry->data + EI_CLASS);
  4178. *fw_size = fw_entry->size;
  4179. if (*app_arch == ELFCLASS32) {
  4180. ehdr = (struct elf32_hdr *)fw_entry->data;
  4181. num_images = ehdr->e_phnum;
  4182. } else if (*app_arch == ELFCLASS64) {
  4183. ehdr64 = (struct elf64_hdr *)fw_entry->data;
  4184. num_images = ehdr64->e_phnum;
  4185. } else {
  4186. pr_err("QSEE %s app, arch %u is not supported\n",
  4187. appname, *app_arch);
  4188. ret = -EIO;
  4189. goto err;
  4190. }
  4191. pr_debug("QSEE %s app, arch %u\n", appname, *app_arch);
  4192. release_firmware(fw_entry);
  4193. fw_entry = NULL;
  4194. for (i = 0; i < num_images; i++) {
  4195. memset(fw_name, 0, sizeof(fw_name));
  4196. snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d", appname, i);
  4197. ret = firmware_request_nowarn(&fw_entry, fw_name, qseecom.pdev);
  4198. if (ret)
  4199. goto err;
  4200. if (*fw_size > U32_MAX - fw_entry->size) {
  4201. pr_err("QSEE %s app file size overflow\n", appname);
  4202. ret = -EINVAL;
  4203. goto err;
  4204. }
  4205. *fw_size += fw_entry->size;
  4206. release_firmware(fw_entry);
  4207. fw_entry = NULL;
  4208. }
  4209. return ret;
  4210. err:
  4211. if (fw_entry)
  4212. release_firmware(fw_entry);
  4213. *fw_size = 0;
  4214. return ret;
  4215. }
  4216. static int __qseecom_get_fw_data(const char *appname, u8 *img_data,
  4217. uint32_t fw_size,
  4218. struct qseecom_load_app_ireq *load_req)
  4219. {
  4220. int ret = -1;
  4221. int i = 0, rc = 0;
  4222. const struct firmware *fw_entry = NULL;
  4223. char fw_name[MAX_APP_NAME_SIZE];
  4224. u8 *img_data_ptr = img_data;
  4225. struct elf32_hdr *ehdr;
  4226. struct elf64_hdr *ehdr64;
  4227. int num_images = 0;
  4228. unsigned char app_arch = 0;
  4229. snprintf(fw_name, sizeof(fw_name), "%s.mdt", appname);
  4230. rc = firmware_request_nowarn(&fw_entry, fw_name, qseecom.pdev);
  4231. if (rc) {
  4232. ret = -EIO;
  4233. goto err;
  4234. }
  4235. load_req->img_len = fw_entry->size;
  4236. if (load_req->img_len > fw_size) {
  4237. pr_err("app %s size %zu is larger than buf size %u\n",
  4238. appname, fw_entry->size, fw_size);
  4239. ret = -EINVAL;
  4240. goto err;
  4241. }
  4242. memcpy(img_data_ptr, fw_entry->data, fw_entry->size);
  4243. img_data_ptr = img_data_ptr + fw_entry->size;
  4244. load_req->mdt_len = fw_entry->size; /*Get MDT LEN*/
  4245. app_arch = *(unsigned char *)(fw_entry->data + EI_CLASS);
  4246. if (app_arch == ELFCLASS32) {
  4247. ehdr = (struct elf32_hdr *)fw_entry->data;
  4248. num_images = ehdr->e_phnum;
  4249. } else if (app_arch == ELFCLASS64) {
  4250. ehdr64 = (struct elf64_hdr *)fw_entry->data;
  4251. num_images = ehdr64->e_phnum;
  4252. } else {
  4253. pr_err("QSEE %s app, arch %u is not supported\n",
  4254. appname, app_arch);
  4255. ret = -EIO;
  4256. goto err;
  4257. }
  4258. release_firmware(fw_entry);
  4259. fw_entry = NULL;
  4260. for (i = 0; i < num_images; i++) {
  4261. snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d", appname, i);
  4262. ret = firmware_request_nowarn(&fw_entry, fw_name, qseecom.pdev);
  4263. if (ret) {
  4264. pr_err("Failed to locate blob %s\n", fw_name);
  4265. goto err;
  4266. }
  4267. if ((fw_entry->size > U32_MAX - load_req->img_len) ||
  4268. (fw_entry->size + load_req->img_len > fw_size)) {
  4269. pr_err("Invalid file size for %s\n", fw_name);
  4270. ret = -EINVAL;
  4271. goto err;
  4272. }
  4273. memcpy(img_data_ptr, fw_entry->data, fw_entry->size);
  4274. img_data_ptr = img_data_ptr + fw_entry->size;
  4275. load_req->img_len += fw_entry->size;
  4276. release_firmware(fw_entry);
  4277. fw_entry = NULL;
  4278. }
  4279. return ret;
  4280. err:
  4281. release_firmware(fw_entry);
  4282. return ret;
  4283. }
  4284. static int __qseecom_alloc_coherent_buf(
  4285. uint32_t size, u8 **vaddr, phys_addr_t *paddr)
  4286. {
  4287. dma_addr_t coh_pmem;
  4288. void *buf = NULL;
  4289. /* Allocate a contiguous kernel buffer */
  4290. size = (size + PAGE_SIZE) & PAGE_MASK;
  4291. buf = dma_alloc_coherent(qseecom.dev,
  4292. size, &coh_pmem, GFP_KERNEL);
  4293. if (buf == NULL)
  4294. return -ENOMEM;
  4295. *vaddr = buf;
  4296. *paddr = coh_pmem;
  4297. return 0;
  4298. }
  4299. static void __qseecom_free_coherent_buf(uint32_t size,
  4300. u8 *vaddr, phys_addr_t paddr)
  4301. {
  4302. if (!vaddr)
  4303. return;
  4304. size = (size + PAGE_SIZE) & PAGE_MASK;
  4305. dma_free_coherent(qseecom.dev, size, vaddr, paddr);
  4306. }
  4307. static int __qseecom_load_fw(struct qseecom_dev_handle *data, char *appname,
  4308. uint32_t *app_id)
  4309. {
  4310. int ret = -1;
  4311. uint32_t fw_size = 0;
  4312. struct qseecom_load_app_ireq load_req = {0, 0, 0, 0};
  4313. struct qseecom_load_app_64bit_ireq load_req_64bit = {0, 0, 0, 0};
  4314. struct qseecom_command_scm_resp resp;
  4315. u8 *img_data = NULL;
  4316. phys_addr_t pa = 0;
  4317. void *cmd_buf = NULL;
  4318. size_t cmd_len;
  4319. uint32_t app_arch = 0;
  4320. if (!data || !appname || !app_id) {
  4321. pr_err("Null pointer to data or appname or appid\n");
  4322. return -EINVAL;
  4323. }
  4324. *app_id = 0;
  4325. if (__qseecom_get_fw_size(appname, &fw_size, &app_arch))
  4326. return -EIO;
  4327. data->client.app_arch = app_arch;
  4328. /* Check and load cmnlib */
  4329. if (qseecom.qsee_version > QSEEE_VERSION_00) {
  4330. if (!qseecom.commonlib_loaded && app_arch == ELFCLASS32) {
  4331. ret = qseecom_load_commonlib_image(data, "cmnlib");
  4332. if (ret) {
  4333. pr_err("failed to load cmnlib\n");
  4334. return -EIO;
  4335. }
  4336. qseecom.commonlib_loaded = true;
  4337. pr_debug("cmnlib is loaded\n");
  4338. }
  4339. if (!qseecom.commonlib64_loaded && app_arch == ELFCLASS64) {
  4340. ret = qseecom_load_commonlib_image(data, "cmnlib64");
  4341. if (ret) {
  4342. pr_err("failed to load cmnlib64\n");
  4343. return -EIO;
  4344. }
  4345. qseecom.commonlib64_loaded = true;
  4346. pr_debug("cmnlib64 is loaded\n");
  4347. }
  4348. }
  4349. ret = __qseecom_alloc_coherent_buf(fw_size, &img_data, &pa);
  4350. if (ret)
  4351. return ret;
  4352. ret = __qseecom_get_fw_data(appname, img_data, fw_size, &load_req);
  4353. if (ret) {
  4354. ret = -EIO;
  4355. goto exit_free_img_data;
  4356. }
  4357. /* Populate the load_req parameters */
  4358. if (qseecom.qsee_version < QSEE_VERSION_40) {
  4359. load_req.qsee_cmd_id = QSEOS_APP_START_COMMAND;
  4360. load_req.mdt_len = load_req.mdt_len;
  4361. load_req.img_len = load_req.img_len;
  4362. strlcpy(load_req.app_name, appname, MAX_APP_NAME_SIZE);
  4363. load_req.phy_addr = (uint32_t)pa;
  4364. cmd_buf = (void *)&load_req;
  4365. cmd_len = sizeof(struct qseecom_load_app_ireq);
  4366. } else {
  4367. load_req_64bit.qsee_cmd_id = QSEOS_APP_START_COMMAND;
  4368. load_req_64bit.mdt_len = load_req.mdt_len;
  4369. load_req_64bit.img_len = load_req.img_len;
  4370. strlcpy(load_req_64bit.app_name, appname, MAX_APP_NAME_SIZE);
  4371. load_req_64bit.phy_addr = (uint64_t)pa;
  4372. cmd_buf = (void *)&load_req_64bit;
  4373. cmd_len = sizeof(struct qseecom_load_app_64bit_ireq);
  4374. }
  4375. if (qseecom.support_bus_scaling) {
  4376. mutex_lock(&qsee_bw_mutex);
  4377. ret = __qseecom_register_bus_bandwidth_needs(data, MEDIUM);
  4378. mutex_unlock(&qsee_bw_mutex);
  4379. if (ret) {
  4380. ret = -EIO;
  4381. goto exit_free_img_data;
  4382. }
  4383. }
  4384. ret = __qseecom_enable_clk_scale_up(data);
  4385. if (ret) {
  4386. ret = -EIO;
  4387. goto exit_unregister_bus_bw_need;
  4388. }
  4389. /* SCM_CALL to load the image */
  4390. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1, cmd_buf, cmd_len,
  4391. &resp, sizeof(resp));
  4392. if (ret) {
  4393. pr_err("scm_call to load failed : ret %d\n", ret);
  4394. ret = -EIO;
  4395. goto exit_disable_clk_vote;
  4396. }
  4397. switch (resp.result) {
  4398. case QSEOS_RESULT_SUCCESS:
  4399. *app_id = resp.data;
  4400. break;
  4401. case QSEOS_RESULT_INCOMPLETE:
  4402. ret = __qseecom_process_incomplete_cmd(data, &resp);
  4403. if (ret) {
  4404. pr_err("incomp_cmd err %d, %d, unload %d %s\n",
  4405. ret, resp.result, resp.data, appname);
  4406. __qseecom_unload_app(data, resp.data);
  4407. ret = -EFAULT;
  4408. } else {
  4409. *app_id = resp.data;
  4410. }
  4411. break;
  4412. case QSEOS_RESULT_FAILURE:
  4413. pr_err("scm call failed with response QSEOS_RESULT FAILURE\n");
  4414. break;
  4415. default:
  4416. pr_err("scm call return unknown response %d\n", resp.result);
  4417. ret = -EINVAL;
  4418. break;
  4419. }
  4420. exit_disable_clk_vote:
  4421. __qseecom_disable_clk_scale_down(data);
  4422. exit_unregister_bus_bw_need:
  4423. if (qseecom.support_bus_scaling) {
  4424. mutex_lock(&qsee_bw_mutex);
  4425. qseecom_unregister_bus_bandwidth_needs(data);
  4426. mutex_unlock(&qsee_bw_mutex);
  4427. }
  4428. exit_free_img_data:
  4429. if (img_data)
  4430. __qseecom_free_coherent_buf(fw_size, img_data, pa);
  4431. return ret;
  4432. }
  4433. static int qseecom_load_commonlib_image(struct qseecom_dev_handle *data,
  4434. char *cmnlib_name)
  4435. {
  4436. int ret = 0;
  4437. uint32_t fw_size = 0;
  4438. struct qseecom_load_app_ireq load_req = {0, 0, 0, 0};
  4439. struct qseecom_load_app_64bit_ireq load_req_64bit = {0, 0, 0, 0};
  4440. struct qseecom_command_scm_resp resp;
  4441. u8 *img_data = NULL;
  4442. phys_addr_t pa = 0;
  4443. void *cmd_buf = NULL;
  4444. size_t cmd_len;
  4445. uint32_t app_arch = 0;
  4446. if (!cmnlib_name) {
  4447. pr_err("cmnlib_name is NULL\n");
  4448. return -EINVAL;
  4449. }
  4450. if (strlen(cmnlib_name) >= MAX_APP_NAME_SIZE) {
  4451. pr_err("The cmnlib_name (%s) with length %zu is not valid\n",
  4452. cmnlib_name, strlen(cmnlib_name));
  4453. return -EINVAL;
  4454. }
  4455. if (__qseecom_get_fw_size(cmnlib_name, &fw_size, &app_arch))
  4456. return -EIO;
  4457. ret = __qseecom_alloc_coherent_buf(fw_size, &img_data, &pa);
  4458. if (ret)
  4459. return -EIO;
  4460. ret = __qseecom_get_fw_data(cmnlib_name, img_data, fw_size, &load_req);
  4461. if (ret) {
  4462. ret = -EIO;
  4463. goto exit_free_img_data;
  4464. }
  4465. if (qseecom.qsee_version < QSEE_VERSION_40) {
  4466. load_req.phy_addr = (uint32_t)pa;
  4467. load_req.qsee_cmd_id = QSEOS_LOAD_SERV_IMAGE_COMMAND;
  4468. cmd_buf = (void *)&load_req;
  4469. cmd_len = sizeof(struct qseecom_load_lib_image_ireq);
  4470. } else {
  4471. load_req_64bit.phy_addr = (uint64_t)pa;
  4472. load_req_64bit.qsee_cmd_id = QSEOS_LOAD_SERV_IMAGE_COMMAND;
  4473. load_req_64bit.img_len = load_req.img_len;
  4474. load_req_64bit.mdt_len = load_req.mdt_len;
  4475. cmd_buf = (void *)&load_req_64bit;
  4476. cmd_len = sizeof(struct qseecom_load_lib_image_64bit_ireq);
  4477. }
  4478. if (qseecom.support_bus_scaling) {
  4479. mutex_lock(&qsee_bw_mutex);
  4480. ret = __qseecom_register_bus_bandwidth_needs(data, MEDIUM);
  4481. mutex_unlock(&qsee_bw_mutex);
  4482. if (ret) {
  4483. ret = -EIO;
  4484. goto exit_free_img_data;
  4485. }
  4486. }
  4487. /* Vote for the SFPB clock */
  4488. ret = __qseecom_enable_clk_scale_up(data);
  4489. if (ret) {
  4490. ret = -EIO;
  4491. goto exit_unregister_bus_bw_need;
  4492. }
  4493. /* SCM_CALL to load the image */
  4494. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1, cmd_buf, cmd_len,
  4495. &resp, sizeof(resp));
  4496. if (ret) {
  4497. pr_err("scm_call to load failed : ret %d\n", ret);
  4498. ret = -EIO;
  4499. goto exit_disable_clk_vote;
  4500. }
  4501. switch (resp.result) {
  4502. case QSEOS_RESULT_SUCCESS:
  4503. break;
  4504. case QSEOS_RESULT_FAILURE:
  4505. pr_err("scm call failed w/response result%d\n", resp.result);
  4506. ret = -EINVAL;
  4507. goto exit_disable_clk_vote;
  4508. case QSEOS_RESULT_INCOMPLETE:
  4509. ret = __qseecom_process_incomplete_cmd(data, &resp);
  4510. if (ret) {
  4511. pr_err("process_incomplete_cmd failed err: %d\n", ret);
  4512. goto exit_disable_clk_vote;
  4513. }
  4514. break;
  4515. default:
  4516. pr_err("scm call return unknown response %d\n", resp.result);
  4517. ret = -EINVAL;
  4518. goto exit_disable_clk_vote;
  4519. }
  4520. exit_disable_clk_vote:
  4521. __qseecom_disable_clk_scale_down(data);
  4522. exit_unregister_bus_bw_need:
  4523. if (qseecom.support_bus_scaling) {
  4524. mutex_lock(&qsee_bw_mutex);
  4525. qseecom_unregister_bus_bandwidth_needs(data);
  4526. mutex_unlock(&qsee_bw_mutex);
  4527. }
  4528. exit_free_img_data:
  4529. if (img_data)
  4530. __qseecom_free_coherent_buf(fw_size, img_data, pa);
  4531. return ret;
  4532. }
  4533. static int qseecom_unload_commonlib_image(void)
  4534. {
  4535. int ret = -EINVAL;
  4536. struct qseecom_unload_lib_image_ireq unload_req = {0};
  4537. struct qseecom_command_scm_resp resp;
  4538. /* Populate the remaining parameters */
  4539. unload_req.qsee_cmd_id = QSEOS_UNLOAD_SERV_IMAGE_COMMAND;
  4540. /* SCM_CALL to load the image */
  4541. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1, &unload_req,
  4542. sizeof(struct qseecom_unload_lib_image_ireq),
  4543. &resp, sizeof(resp));
  4544. if (ret) {
  4545. pr_err("scm_call to unload lib failed : ret %d\n", ret);
  4546. ret = -EIO;
  4547. } else {
  4548. switch (resp.result) {
  4549. case QSEOS_RESULT_SUCCESS:
  4550. break;
  4551. case QSEOS_RESULT_FAILURE:
  4552. pr_err("scm fail resp.result QSEOS_RESULT FAILURE\n");
  4553. break;
  4554. default:
  4555. pr_err("scm call return unknown response %d\n",
  4556. resp.result);
  4557. ret = -EINVAL;
  4558. break;
  4559. }
  4560. }
  4561. return ret;
  4562. }
  4563. static int __qseecom_start_app(struct qseecom_handle **handle,
  4564. char *app_name, uint32_t size)
  4565. {
  4566. int32_t ret = 0;
  4567. unsigned long flags = 0;
  4568. struct qseecom_dev_handle *data = NULL;
  4569. struct qseecom_check_app_ireq app_ireq;
  4570. struct qseecom_registered_app_list *entry = NULL;
  4571. struct qseecom_registered_kclient_list *kclient_entry = NULL;
  4572. bool found_app = false;
  4573. phys_addr_t pa = 0;
  4574. u8 *va = NULL;
  4575. uint32_t fw_size, app_arch;
  4576. uint32_t app_id = 0;
  4577. __wakeup_unregister_listener_kthread();
  4578. __wakeup_unload_app_kthread();
  4579. if (atomic_read(&qseecom.qseecom_state) != QSEECOM_STATE_READY) {
  4580. pr_err("Not allowed to be called in %d state\n",
  4581. atomic_read(&qseecom.qseecom_state));
  4582. return -EPERM;
  4583. }
  4584. if (!app_name) {
  4585. pr_err("failed to get the app name\n");
  4586. return -EINVAL;
  4587. }
  4588. if (strnlen(app_name, MAX_APP_NAME_SIZE) == MAX_APP_NAME_SIZE) {
  4589. pr_err("The app_name (%s) with length %zu is not valid\n",
  4590. app_name, strnlen(app_name, MAX_APP_NAME_SIZE));
  4591. return -EINVAL;
  4592. }
  4593. *handle = kzalloc(sizeof(struct qseecom_handle), GFP_KERNEL);
  4594. if (!(*handle))
  4595. return -ENOMEM;
  4596. data = kzalloc(sizeof(*data), GFP_KERNEL);
  4597. if (!data) {
  4598. kfree(*handle);
  4599. *handle = NULL;
  4600. return -ENOMEM;
  4601. }
  4602. mutex_lock(&app_access_lock);
  4603. data->abort = 0;
  4604. data->type = QSEECOM_CLIENT_APP;
  4605. data->released = false;
  4606. data->client.sb_length = size;
  4607. data->client.user_virt_sb_base = 0;
  4608. data->sglistinfo_ptr = (struct sglist_info *)__qseecom_alloc_tzbuf(
  4609. sizeof(struct sglist_info) * MAX_ION_FD,
  4610. &data->sglistinfo_shm.paddr,
  4611. &data->sglistinfo_shm);
  4612. if (!data->sglistinfo_ptr) {
  4613. ret = -ENOMEM;
  4614. goto err;
  4615. }
  4616. init_waitqueue_head(&data->abort_wq);
  4617. app_ireq.qsee_cmd_id = QSEOS_APP_LOOKUP_COMMAND;
  4618. strlcpy(app_ireq.app_name, app_name, MAX_APP_NAME_SIZE);
  4619. ret = __qseecom_check_app_exists(app_ireq, &app_id);
  4620. if (ret)
  4621. goto err;
  4622. strlcpy(data->client.app_name, app_name, MAX_APP_NAME_SIZE);
  4623. if (app_id) {
  4624. pr_warn("App id %d for [%s] app exists\n", app_id,
  4625. (char *)app_ireq.app_name);
  4626. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
  4627. list_for_each_entry(entry,
  4628. &qseecom.registered_app_list_head, list){
  4629. if (entry->app_id == app_id) {
  4630. if (entry->ref_cnt == U32_MAX) {
  4631. pr_err("App %d (%s) ref_cnt overflow\n",
  4632. app_id, app_ireq.app_name);
  4633. ret = -EINVAL;
  4634. goto err;
  4635. }
  4636. entry->ref_cnt++;
  4637. found_app = true;
  4638. break;
  4639. }
  4640. }
  4641. spin_unlock_irqrestore(
  4642. &qseecom.registered_app_list_lock, flags);
  4643. if (!found_app)
  4644. pr_warn("App_id %d [%s] was loaded but not registered\n",
  4645. ret, (char *)app_ireq.app_name);
  4646. } else {
  4647. /* load the app and get the app_id */
  4648. pr_debug("%s: Loading app for the first time'\n",
  4649. qseecom.pdev->init_name);
  4650. ret = __qseecom_load_fw(data, app_name, &app_id);
  4651. if (ret < 0)
  4652. goto err;
  4653. }
  4654. data->client.app_id = app_id;
  4655. if (!found_app) {
  4656. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  4657. if (!entry) {
  4658. ret = -ENOMEM;
  4659. goto err;
  4660. }
  4661. entry->app_id = app_id;
  4662. entry->ref_cnt = 1;
  4663. strlcpy(entry->app_name, app_name, MAX_APP_NAME_SIZE);
  4664. if (__qseecom_get_fw_size(app_name, &fw_size, &app_arch)) {
  4665. ret = -EIO;
  4666. kfree(entry);
  4667. goto err;
  4668. }
  4669. entry->app_arch = app_arch;
  4670. entry->app_blocked = false;
  4671. entry->blocked_on_listener_id = 0;
  4672. entry->check_block = 0;
  4673. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
  4674. list_add_tail(&entry->list, &qseecom.registered_app_list_head);
  4675. spin_unlock_irqrestore(&qseecom.registered_app_list_lock,
  4676. flags);
  4677. }
  4678. /* Get the physical address of the req/resp buffer */
  4679. ret = __qseecom_alloc_coherent_buf(size, &va, &pa);
  4680. if (ret) {
  4681. pr_err("Cannot get phys_addr for the Ion Client, ret = %d\n",
  4682. ret);
  4683. goto err;
  4684. }
  4685. /* Populate the structure for sending scm call to load image */
  4686. data->client.sb_virt = va;
  4687. data->client.user_virt_sb_base = (uintptr_t)data->client.sb_virt;
  4688. data->client.sb_phys = (phys_addr_t)pa;
  4689. (*handle)->dev = (void *)data;
  4690. (*handle)->sbuf = (unsigned char *)data->client.sb_virt;
  4691. (*handle)->sbuf_len = data->client.sb_length;
  4692. kclient_entry = kzalloc(sizeof(*kclient_entry), GFP_KERNEL);
  4693. if (!kclient_entry) {
  4694. ret = -ENOMEM;
  4695. goto err;
  4696. }
  4697. kclient_entry->handle = *handle;
  4698. spin_lock_irqsave(&qseecom.registered_kclient_list_lock, flags);
  4699. list_add_tail(&kclient_entry->list,
  4700. &qseecom.registered_kclient_list_head);
  4701. spin_unlock_irqrestore(&qseecom.registered_kclient_list_lock, flags);
  4702. mutex_unlock(&app_access_lock);
  4703. __wakeup_unload_app_kthread();
  4704. return 0;
  4705. err:
  4706. __qseecom_free_coherent_buf(size, va, pa);
  4707. __qseecom_free_tzbuf(&data->sglistinfo_shm);
  4708. kfree(data);
  4709. kfree(*handle);
  4710. *handle = NULL;
  4711. mutex_unlock(&app_access_lock);
  4712. __wakeup_unload_app_kthread();
  4713. return ret;
  4714. }
  4715. static int __qseecom_shutdown_app(struct qseecom_handle **handle)
  4716. {
  4717. int ret = -EINVAL;
  4718. struct qseecom_dev_handle *data;
  4719. struct qseecom_registered_kclient_list *kclient = NULL;
  4720. unsigned long flags = 0;
  4721. bool found_handle = false;
  4722. __wakeup_unregister_listener_kthread();
  4723. __wakeup_unload_app_kthread();
  4724. if (atomic_read(&qseecom.qseecom_state) != QSEECOM_STATE_READY) {
  4725. pr_err("Not allowed to be called in %d state\n",
  4726. atomic_read(&qseecom.qseecom_state));
  4727. return -EPERM;
  4728. }
  4729. if ((handle == NULL) || (*handle == NULL)) {
  4730. pr_err("Handle is not initialized\n");
  4731. return -EINVAL;
  4732. }
  4733. data = (struct qseecom_dev_handle *) ((*handle)->dev);
  4734. mutex_lock(&app_access_lock);
  4735. spin_lock_irqsave(&qseecom.registered_kclient_list_lock, flags);
  4736. list_for_each_entry(kclient, &qseecom.registered_kclient_list_head,
  4737. list) {
  4738. if (kclient->handle == (*handle)) {
  4739. list_del(&kclient->list);
  4740. found_handle = true;
  4741. break;
  4742. }
  4743. }
  4744. spin_unlock_irqrestore(&qseecom.registered_kclient_list_lock, flags);
  4745. if (!found_handle)
  4746. pr_err("Unable to find the handle, exiting\n");
  4747. else
  4748. ret = qseecom_unload_app(data, false);
  4749. mutex_unlock(&app_access_lock);
  4750. if (ret == 0) {
  4751. if (data->client.sb_virt)
  4752. __qseecom_free_coherent_buf(data->client.sb_length,
  4753. data->client.sb_virt, data->client.sb_phys);
  4754. __qseecom_free_tzbuf(&data->sglistinfo_shm);
  4755. kfree_sensitive(data);
  4756. kfree_sensitive(*handle);
  4757. kfree_sensitive(kclient);
  4758. *handle = NULL;
  4759. }
  4760. __wakeup_unload_app_kthread();
  4761. return ret;
  4762. }
  4763. static int __qseecom_send_command(struct qseecom_handle *handle, void *send_buf,
  4764. uint32_t sbuf_len, void *resp_buf, uint32_t rbuf_len)
  4765. {
  4766. int ret = 0;
  4767. struct qseecom_send_cmd_req req = {NULL, 0, NULL, 0};
  4768. struct qseecom_dev_handle *data;
  4769. bool perf_enabled = false;
  4770. __wakeup_unregister_listener_kthread();
  4771. __wakeup_unload_app_kthread();
  4772. if (atomic_read(&qseecom.qseecom_state) != QSEECOM_STATE_READY) {
  4773. pr_err("Not allowed to be called in %d state\n",
  4774. atomic_read(&qseecom.qseecom_state));
  4775. return -EPERM;
  4776. }
  4777. if (handle == NULL) {
  4778. pr_err("Handle is not initialized\n");
  4779. return -EINVAL;
  4780. }
  4781. data = handle->dev;
  4782. req.cmd_req_len = sbuf_len;
  4783. req.resp_len = rbuf_len;
  4784. req.cmd_req_buf = send_buf;
  4785. req.resp_buf = resp_buf;
  4786. if (__validate_send_cmd_inputs(data, &req))
  4787. return -EINVAL;
  4788. mutex_lock(&app_access_lock);
  4789. if (qseecom.support_bus_scaling) {
  4790. ret = qseecom_scale_bus_bandwidth_timer(INACTIVE);
  4791. if (ret) {
  4792. pr_err("Failed to set bw.\n");
  4793. mutex_unlock(&app_access_lock);
  4794. return ret;
  4795. }
  4796. }
  4797. /*
  4798. * On targets where crypto clock is handled by HLOS,
  4799. * if clk_access_cnt is zero and perf_enabled is false,
  4800. * then the crypto clock was not enabled before sending cmd
  4801. * to tz, qseecom will enable the clock to avoid service failure.
  4802. */
  4803. if (!qseecom.no_clock_support &&
  4804. !qseecom.qsee.clk_access_cnt && !data->perf_enabled) {
  4805. pr_debug("ce clock is not enabled!\n");
  4806. ret = qseecom_perf_enable(data);
  4807. if (ret) {
  4808. pr_err("Failed to vote for clock with err %d\n",
  4809. ret);
  4810. mutex_unlock(&app_access_lock);
  4811. return -EINVAL;
  4812. }
  4813. perf_enabled = true;
  4814. }
  4815. if (!strcmp(data->client.app_name, "securemm"))
  4816. data->use_legacy_cmd = true;
  4817. ret = __qseecom_send_cmd(data, &req, false);
  4818. data->use_legacy_cmd = false;
  4819. if (qseecom.support_bus_scaling)
  4820. __qseecom_add_bw_scale_down_timer(
  4821. QSEECOM_SEND_CMD_CRYPTO_TIMEOUT);
  4822. if (perf_enabled) {
  4823. qsee_disable_clock_vote(data, CLK_DFAB);
  4824. qsee_disable_clock_vote(data, CLK_SFPB);
  4825. }
  4826. mutex_unlock(&app_access_lock);
  4827. if (ret)
  4828. return ret;
  4829. pr_debug("sending cmd_req->rsp size: %u, ptr: 0x%pK\n",
  4830. req.resp_len, req.resp_buf);
  4831. return ret;
  4832. }
  4833. #if IS_ENABLED(CONFIG_QSEECOM_PROXY)
  4834. const static struct qseecom_drv_ops qseecom_driver_ops = {
  4835. .qseecom_send_command = __qseecom_send_command,
  4836. .qseecom_start_app = __qseecom_start_app,
  4837. .qseecom_shutdown_app = __qseecom_shutdown_app,
  4838. };
  4839. int get_qseecom_kernel_fun_ops(void)
  4840. {
  4841. return provide_qseecom_kernel_fun_ops(&qseecom_driver_ops);
  4842. }
  4843. #else
  4844. int qseecom_start_app(struct qseecom_handle **handle,
  4845. char *app_name, uint32_t size)
  4846. {
  4847. return __qseecom_start_app(handle, app_name, size);
  4848. }
  4849. EXPORT_SYMBOL(qseecom_start_app);
  4850. int qseecom_shutdown_app(struct qseecom_handle **handle)
  4851. {
  4852. return __qseecom_shutdown_app(handle);
  4853. }
  4854. EXPORT_SYMBOL(qseecom_shutdown_app);
  4855. int qseecom_send_command(struct qseecom_handle *handle, void *send_buf,
  4856. uint32_t sbuf_len, void *resp_buf, uint32_t rbuf_len)
  4857. {
  4858. return __qseecom_send_command(handle, send_buf, sbuf_len,
  4859. resp_buf, rbuf_len);
  4860. }
  4861. EXPORT_SYMBOL(qseecom_send_command);
  4862. #endif
  4863. int qseecom_set_bandwidth(struct qseecom_handle *handle, bool high)
  4864. {
  4865. int ret = 0;
  4866. if ((handle == NULL) || (handle->dev == NULL)) {
  4867. pr_err("No valid kernel client\n");
  4868. return -EINVAL;
  4869. }
  4870. if (high) {
  4871. if (qseecom.support_bus_scaling) {
  4872. mutex_lock(&qsee_bw_mutex);
  4873. __qseecom_register_bus_bandwidth_needs(handle->dev,
  4874. HIGH);
  4875. mutex_unlock(&qsee_bw_mutex);
  4876. } else {
  4877. ret = qseecom_perf_enable(handle->dev);
  4878. if (ret)
  4879. pr_err("Failed to vote for clock with err %d\n",
  4880. ret);
  4881. }
  4882. } else {
  4883. if (!qseecom.support_bus_scaling) {
  4884. qsee_disable_clock_vote(handle->dev, CLK_DFAB);
  4885. qsee_disable_clock_vote(handle->dev, CLK_SFPB);
  4886. } else {
  4887. mutex_lock(&qsee_bw_mutex);
  4888. qseecom_unregister_bus_bandwidth_needs(handle->dev);
  4889. mutex_unlock(&qsee_bw_mutex);
  4890. }
  4891. }
  4892. return ret;
  4893. }
  4894. EXPORT_SYMBOL(qseecom_set_bandwidth);
  4895. int qseecom_process_listener_from_smcinvoke(uint32_t *result,
  4896. u64 *response_type, unsigned int *data)
  4897. {
  4898. struct qseecom_registered_app_list dummy_app_entry;
  4899. struct qseecom_dev_handle dummy_private_data = {0};
  4900. struct qseecom_command_scm_resp resp;
  4901. int ret = 0;
  4902. if (!result || !response_type || !data) {
  4903. pr_err("input parameter NULL\n");
  4904. return -EINVAL;
  4905. }
  4906. memset((void *)&dummy_app_entry, 0, sizeof(dummy_app_entry));
  4907. /*
  4908. * smcinvoke expects result in scm call resp.ret[1] and type in ret[0],
  4909. * while qseecom expects result in ret[0] and type in ret[1].
  4910. * To simplify API interface and code changes in smcinvoke, here
  4911. * internally switch result and resp_type to let qseecom work with
  4912. * smcinvoke and upstream scm driver protocol.
  4913. */
  4914. resp.result = *response_type;
  4915. resp.resp_type = *result;
  4916. resp.data = *data;
  4917. dummy_private_data.client.app_id = *response_type;
  4918. dummy_private_data.client.from_smcinvoke = true;
  4919. dummy_app_entry.app_id = *response_type;
  4920. mutex_lock(&app_access_lock);
  4921. if (qseecom.qsee_reentrancy_support)
  4922. ret = __qseecom_process_reentrancy(&resp, &dummy_app_entry,
  4923. &dummy_private_data);
  4924. else
  4925. ret = __qseecom_process_incomplete_cmd(&dummy_private_data,
  4926. &resp);
  4927. mutex_unlock(&app_access_lock);
  4928. if (ret)
  4929. pr_err("Failed on cmd %d for lsnr %d session %d, ret = %d\n",
  4930. resp.result, resp.data, resp.resp_type, ret);
  4931. *result = resp.resp_type;
  4932. *response_type = resp.result;
  4933. *data = resp.data;
  4934. return ret;
  4935. }
  4936. EXPORT_SYMBOL(qseecom_process_listener_from_smcinvoke);
  4937. static int qseecom_send_resp(void)
  4938. {
  4939. qseecom.send_resp_flag = 1;
  4940. wake_up_interruptible(&qseecom.send_resp_wq);
  4941. return 0;
  4942. }
  4943. static int qseecom_reentrancy_send_resp(struct qseecom_dev_handle *data)
  4944. {
  4945. struct qseecom_registered_listener_list *this_lstnr = NULL;
  4946. pr_debug("lstnr %d send resp, wakeup\n", data->listener.id);
  4947. this_lstnr = __qseecom_find_svc(data->listener.id);
  4948. if (this_lstnr == NULL)
  4949. return -EINVAL;
  4950. qseecom.send_resp_flag = 1;
  4951. this_lstnr->send_resp_flag = 1;
  4952. wake_up_interruptible(&qseecom.send_resp_wq);
  4953. return 0;
  4954. }
  4955. static int __validate_send_modfd_resp_inputs(struct qseecom_dev_handle *data,
  4956. struct qseecom_send_modfd_listener_resp *resp,
  4957. struct qseecom_registered_listener_list *this_lstnr)
  4958. {
  4959. int i;
  4960. if (!data || !resp || !this_lstnr) {
  4961. pr_err("listener handle or resp msg is null\n");
  4962. return -EINVAL;
  4963. }
  4964. if (resp->resp_buf_ptr == NULL) {
  4965. pr_err("resp buffer is null\n");
  4966. return -EINVAL;
  4967. }
  4968. /* validate resp buf length */
  4969. if ((resp->resp_len == 0) ||
  4970. (resp->resp_len > this_lstnr->sb_length)) {
  4971. pr_err("resp buf length %d not valid\n", resp->resp_len);
  4972. return -EINVAL;
  4973. }
  4974. if ((uintptr_t)resp->resp_buf_ptr > (ULONG_MAX - resp->resp_len)) {
  4975. pr_err("Integer overflow in resp_len & resp_buf\n");
  4976. return -EINVAL;
  4977. }
  4978. if ((uintptr_t)this_lstnr->user_virt_sb_base >
  4979. (ULONG_MAX - this_lstnr->sb_length)) {
  4980. pr_err("Integer overflow in user_virt_sb_base & sb_length\n");
  4981. return -EINVAL;
  4982. }
  4983. /* validate resp buf */
  4984. if (((uintptr_t)resp->resp_buf_ptr <
  4985. (uintptr_t)this_lstnr->user_virt_sb_base) ||
  4986. ((uintptr_t)resp->resp_buf_ptr >=
  4987. ((uintptr_t)this_lstnr->user_virt_sb_base +
  4988. this_lstnr->sb_length)) ||
  4989. (((uintptr_t)resp->resp_buf_ptr + resp->resp_len) >
  4990. ((uintptr_t)this_lstnr->user_virt_sb_base +
  4991. this_lstnr->sb_length))) {
  4992. pr_err("resp buf is out of shared buffer region\n");
  4993. return -EINVAL;
  4994. }
  4995. /* validate offsets */
  4996. for (i = 0; i < MAX_ION_FD; i++) {
  4997. if (resp->ifd_data[i].cmd_buf_offset >= resp->resp_len) {
  4998. pr_err("Invalid offset %d = 0x%x\n",
  4999. i, resp->ifd_data[i].cmd_buf_offset);
  5000. return -EINVAL;
  5001. }
  5002. }
  5003. return 0;
  5004. }
  5005. static int __qseecom_send_modfd_resp(struct qseecom_dev_handle *data,
  5006. void __user *argp, bool is_64bit_addr)
  5007. {
  5008. struct qseecom_send_modfd_listener_resp resp;
  5009. struct qseecom_registered_listener_list *this_lstnr = NULL;
  5010. if (copy_from_user(&resp, argp, sizeof(resp))) {
  5011. pr_err("copy_from_user failed\n");
  5012. return -EINVAL;
  5013. }
  5014. this_lstnr = __qseecom_find_svc(data->listener.id);
  5015. if (this_lstnr == NULL)
  5016. return -EINVAL;
  5017. if (__validate_send_modfd_resp_inputs(data, &resp, this_lstnr))
  5018. return -EINVAL;
  5019. resp.resp_buf_ptr = this_lstnr->sb_virt +
  5020. (uintptr_t)(resp.resp_buf_ptr - this_lstnr->user_virt_sb_base);
  5021. if (!is_64bit_addr)
  5022. __qseecom_update_cmd_buf(&resp, false, data);
  5023. else
  5024. __qseecom_update_cmd_buf_64(&resp, false, data);
  5025. qseecom.send_resp_flag = 1;
  5026. this_lstnr->send_resp_flag = 1;
  5027. wake_up_interruptible(&qseecom.send_resp_wq);
  5028. return 0;
  5029. }
  5030. static int qseecom_send_modfd_resp(struct qseecom_dev_handle *data,
  5031. void __user *argp)
  5032. {
  5033. return __qseecom_send_modfd_resp(data, argp, false);
  5034. }
  5035. static int qseecom_send_modfd_resp_64(struct qseecom_dev_handle *data,
  5036. void __user *argp)
  5037. {
  5038. return __qseecom_send_modfd_resp(data, argp, true);
  5039. }
  5040. static int qseecom_get_qseos_version(struct qseecom_dev_handle *data,
  5041. void __user *argp)
  5042. {
  5043. struct qseecom_qseos_version_req req;
  5044. if (copy_from_user(&req, argp, sizeof(req))) {
  5045. pr_err("copy_from_user failed\n");
  5046. return -EINVAL;
  5047. }
  5048. req.qseos_version = qseecom.qseos_version;
  5049. if (copy_to_user(argp, &req, sizeof(req))) {
  5050. pr_err("copy_to_user failed\n");
  5051. return -EINVAL;
  5052. }
  5053. return 0;
  5054. }
  5055. static int __qseecom_enable_clk(enum qseecom_ce_hw_instance ce)
  5056. {
  5057. int rc = 0;
  5058. struct qseecom_clk *qclk = NULL;
  5059. if (qseecom.no_clock_support)
  5060. return 0;
  5061. if (ce == CLK_QSEE)
  5062. qclk = &qseecom.qsee;
  5063. if (ce == CLK_CE_DRV)
  5064. qclk = &qseecom.ce_drv;
  5065. if (qclk == NULL) {
  5066. pr_err("CLK type not supported\n");
  5067. return -EINVAL;
  5068. }
  5069. mutex_lock(&clk_access_lock);
  5070. if (qclk->clk_access_cnt == ULONG_MAX) {
  5071. pr_err("clk_access_cnt beyond limitation\n");
  5072. goto err;
  5073. }
  5074. if (qclk->clk_access_cnt > 0) {
  5075. qclk->clk_access_cnt++;
  5076. mutex_unlock(&clk_access_lock);
  5077. return rc;
  5078. }
  5079. /* Enable CE core clk */
  5080. if (qclk->ce_core_clk != NULL) {
  5081. rc = clk_prepare_enable(qclk->ce_core_clk);
  5082. if (rc) {
  5083. pr_err("Unable to enable/prepare CE core clk\n");
  5084. goto err;
  5085. }
  5086. }
  5087. /* Enable CE clk */
  5088. if (qclk->ce_clk != NULL) {
  5089. rc = clk_prepare_enable(qclk->ce_clk);
  5090. if (rc) {
  5091. pr_err("Unable to enable/prepare CE iface clk\n");
  5092. goto ce_clk_err;
  5093. }
  5094. }
  5095. /* Enable AXI clk */
  5096. if (qclk->ce_bus_clk != NULL) {
  5097. rc = clk_prepare_enable(qclk->ce_bus_clk);
  5098. if (rc) {
  5099. pr_err("Unable to enable/prepare CE bus clk\n");
  5100. goto ce_bus_clk_err;
  5101. }
  5102. }
  5103. qclk->clk_access_cnt++;
  5104. mutex_unlock(&clk_access_lock);
  5105. return 0;
  5106. ce_bus_clk_err:
  5107. if (qclk->ce_clk != NULL)
  5108. clk_disable_unprepare(qclk->ce_clk);
  5109. ce_clk_err:
  5110. if (qclk->ce_core_clk != NULL)
  5111. clk_disable_unprepare(qclk->ce_core_clk);
  5112. err:
  5113. mutex_unlock(&clk_access_lock);
  5114. return -EIO;
  5115. }
  5116. static void __qseecom_disable_clk(enum qseecom_ce_hw_instance ce)
  5117. {
  5118. struct qseecom_clk *qclk;
  5119. if (qseecom.no_clock_support)
  5120. return;
  5121. if (ce == CLK_QSEE)
  5122. qclk = &qseecom.qsee;
  5123. else
  5124. qclk = &qseecom.ce_drv;
  5125. mutex_lock(&clk_access_lock);
  5126. if (qclk->clk_access_cnt == 0) {
  5127. mutex_unlock(&clk_access_lock);
  5128. return;
  5129. }
  5130. if (qclk->clk_access_cnt == 1) {
  5131. if (qclk->ce_clk != NULL)
  5132. clk_disable_unprepare(qclk->ce_clk);
  5133. if (qclk->ce_core_clk != NULL)
  5134. clk_disable_unprepare(qclk->ce_core_clk);
  5135. if (qclk->ce_bus_clk != NULL)
  5136. clk_disable_unprepare(qclk->ce_bus_clk);
  5137. }
  5138. qclk->clk_access_cnt--;
  5139. mutex_unlock(&clk_access_lock);
  5140. }
  5141. static int qsee_vote_for_clock(struct qseecom_dev_handle *data,
  5142. int32_t clk_type)
  5143. {
  5144. int ret = 0;
  5145. struct qseecom_clk *qclk;
  5146. if (qseecom.no_clock_support)
  5147. return 0;
  5148. qclk = &qseecom.qsee;
  5149. if (!qseecom.qsee_perf_client)
  5150. return ret;
  5151. switch (clk_type) {
  5152. case CLK_DFAB:
  5153. mutex_lock(&qsee_bw_mutex);
  5154. if (!qseecom.qsee_bw_count) {
  5155. if (qseecom.qsee_sfpb_bw_count > 0)
  5156. ret = qseecom_bus_scale_update_request(
  5157. qseecom.qsee_perf_client, 3);
  5158. else {
  5159. if (qclk->ce_core_src_clk != NULL)
  5160. ret = __qseecom_enable_clk(CLK_QSEE);
  5161. if (!ret) {
  5162. ret =
  5163. qseecom_bus_scale_update_request(
  5164. qseecom.qsee_perf_client, 1);
  5165. if ((ret) &&
  5166. (qclk->ce_core_src_clk != NULL))
  5167. __qseecom_disable_clk(CLK_QSEE);
  5168. }
  5169. }
  5170. if (ret)
  5171. pr_err("DFAB Bandwidth req failed (%d)\n",
  5172. ret);
  5173. else {
  5174. qseecom.qsee_bw_count++;
  5175. data->perf_enabled = true;
  5176. }
  5177. } else {
  5178. qseecom.qsee_bw_count++;
  5179. data->perf_enabled = true;
  5180. }
  5181. mutex_unlock(&qsee_bw_mutex);
  5182. break;
  5183. case CLK_SFPB:
  5184. mutex_lock(&qsee_bw_mutex);
  5185. if (!qseecom.qsee_sfpb_bw_count) {
  5186. if (qseecom.qsee_bw_count > 0)
  5187. ret = qseecom_bus_scale_update_request(
  5188. qseecom.qsee_perf_client, 3);
  5189. else {
  5190. if (qclk->ce_core_src_clk != NULL)
  5191. ret = __qseecom_enable_clk(CLK_QSEE);
  5192. if (!ret) {
  5193. ret =
  5194. qseecom_bus_scale_update_request(
  5195. qseecom.qsee_perf_client, 2);
  5196. if ((ret) &&
  5197. (qclk->ce_core_src_clk != NULL))
  5198. __qseecom_disable_clk(CLK_QSEE);
  5199. }
  5200. }
  5201. if (ret)
  5202. pr_err("SFPB Bandwidth req failed (%d)\n",
  5203. ret);
  5204. else {
  5205. qseecom.qsee_sfpb_bw_count++;
  5206. data->fast_load_enabled = true;
  5207. }
  5208. } else {
  5209. qseecom.qsee_sfpb_bw_count++;
  5210. data->fast_load_enabled = true;
  5211. }
  5212. mutex_unlock(&qsee_bw_mutex);
  5213. break;
  5214. default:
  5215. pr_err("Clock type not defined\n");
  5216. break;
  5217. }
  5218. return ret;
  5219. }
  5220. static void qsee_disable_clock_vote(struct qseecom_dev_handle *data,
  5221. int32_t clk_type)
  5222. {
  5223. int32_t ret = 0;
  5224. struct qseecom_clk *qclk;
  5225. qclk = &qseecom.qsee;
  5226. if (qseecom.no_clock_support)
  5227. return;
  5228. if (!qseecom.qsee_perf_client)
  5229. return;
  5230. switch (clk_type) {
  5231. case CLK_DFAB:
  5232. mutex_lock(&qsee_bw_mutex);
  5233. if (qseecom.qsee_bw_count == 0) {
  5234. pr_err("Client error.Extra call to disable DFAB clk\n");
  5235. mutex_unlock(&qsee_bw_mutex);
  5236. return;
  5237. }
  5238. if (qseecom.qsee_bw_count == 1) {
  5239. if (qseecom.qsee_sfpb_bw_count > 0)
  5240. ret = qseecom_bus_scale_update_request(
  5241. qseecom.qsee_perf_client, 2);
  5242. else {
  5243. ret = qseecom_bus_scale_update_request(
  5244. qseecom.qsee_perf_client, 0);
  5245. if ((!ret) && (qclk->ce_core_src_clk != NULL))
  5246. __qseecom_disable_clk(CLK_QSEE);
  5247. }
  5248. if (ret)
  5249. pr_err("SFPB Bandwidth req fail (%d)\n",
  5250. ret);
  5251. else {
  5252. qseecom.qsee_bw_count--;
  5253. data->perf_enabled = false;
  5254. }
  5255. } else {
  5256. qseecom.qsee_bw_count--;
  5257. data->perf_enabled = false;
  5258. }
  5259. mutex_unlock(&qsee_bw_mutex);
  5260. break;
  5261. case CLK_SFPB:
  5262. mutex_lock(&qsee_bw_mutex);
  5263. if (qseecom.qsee_sfpb_bw_count == 0) {
  5264. pr_err("Client error.Extra call to disable SFPB clk\n");
  5265. mutex_unlock(&qsee_bw_mutex);
  5266. return;
  5267. }
  5268. if (qseecom.qsee_sfpb_bw_count == 1) {
  5269. if (qseecom.qsee_bw_count > 0)
  5270. ret = qseecom_bus_scale_update_request(
  5271. qseecom.qsee_perf_client, 1);
  5272. else {
  5273. ret = qseecom_bus_scale_update_request(
  5274. qseecom.qsee_perf_client, 0);
  5275. if ((!ret) && (qclk->ce_core_src_clk != NULL))
  5276. __qseecom_disable_clk(CLK_QSEE);
  5277. }
  5278. if (ret)
  5279. pr_err("SFPB Bandwidth req fail (%d)\n",
  5280. ret);
  5281. else {
  5282. qseecom.qsee_sfpb_bw_count--;
  5283. data->fast_load_enabled = false;
  5284. }
  5285. } else {
  5286. qseecom.qsee_sfpb_bw_count--;
  5287. data->fast_load_enabled = false;
  5288. }
  5289. mutex_unlock(&qsee_bw_mutex);
  5290. break;
  5291. default:
  5292. pr_err("Clock type not defined\n");
  5293. break;
  5294. }
  5295. }
  5296. static int qseecom_load_external_elf(struct qseecom_dev_handle *data,
  5297. void __user *argp)
  5298. {
  5299. struct qseecom_load_img_req load_img_req;
  5300. int uret = 0;
  5301. int ret = 0;
  5302. phys_addr_t pa = 0;
  5303. size_t len;
  5304. struct qseecom_load_app_ireq load_req;
  5305. struct qseecom_load_app_64bit_ireq load_req_64bit;
  5306. struct qseecom_command_scm_resp resp;
  5307. void *cmd_buf = NULL;
  5308. size_t cmd_len;
  5309. struct sg_table *sgt = NULL;
  5310. struct dma_buf_attachment *attach = NULL;
  5311. struct dma_buf *dmabuf = NULL;
  5312. void *va = NULL;
  5313. /* Copy the relevant information needed for loading the image */
  5314. if (copy_from_user(&load_img_req,
  5315. (void __user *)argp,
  5316. sizeof(struct qseecom_load_img_req))) {
  5317. pr_err("copy_from_user failed\n");
  5318. return -EFAULT;
  5319. }
  5320. /* Get the handle of the shared fd */
  5321. ret = qseecom_vaddr_map(load_img_req.ifd_data_fd, &pa, &va,
  5322. &sgt, &attach, &len, &dmabuf);
  5323. if (ret) {
  5324. pr_err("Failed to map vaddr for ion_fd %d\n",
  5325. load_img_req.ifd_data_fd);
  5326. return -ENOMEM;
  5327. }
  5328. if (load_img_req.mdt_len > len || load_img_req.img_len > len) {
  5329. pr_err("ion len %zu is smaller than mdt_len %u or img_len %u\n",
  5330. len, load_img_req.mdt_len,
  5331. load_img_req.img_len);
  5332. ret = -EINVAL;
  5333. goto exit_cpu_restore;
  5334. }
  5335. /* Populate the structure for sending scm call to load image */
  5336. if (qseecom.qsee_version < QSEE_VERSION_40) {
  5337. load_req.qsee_cmd_id = QSEOS_LOAD_EXTERNAL_ELF_COMMAND;
  5338. load_req.mdt_len = load_img_req.mdt_len;
  5339. load_req.img_len = load_img_req.img_len;
  5340. load_req.phy_addr = (uint32_t)pa;
  5341. cmd_buf = (void *)&load_req;
  5342. cmd_len = sizeof(struct qseecom_load_app_ireq);
  5343. } else {
  5344. load_req_64bit.qsee_cmd_id = QSEOS_LOAD_EXTERNAL_ELF_COMMAND;
  5345. load_req_64bit.mdt_len = load_img_req.mdt_len;
  5346. load_req_64bit.img_len = load_img_req.img_len;
  5347. load_req_64bit.phy_addr = (uint64_t)pa;
  5348. cmd_buf = (void *)&load_req_64bit;
  5349. cmd_len = sizeof(struct qseecom_load_app_64bit_ireq);
  5350. }
  5351. if (qseecom.support_bus_scaling) {
  5352. mutex_lock(&qsee_bw_mutex);
  5353. ret = __qseecom_register_bus_bandwidth_needs(data, MEDIUM);
  5354. mutex_unlock(&qsee_bw_mutex);
  5355. if (ret) {
  5356. ret = -EIO;
  5357. goto exit_cpu_restore;
  5358. }
  5359. }
  5360. /* Vote for the SFPB clock */
  5361. ret = __qseecom_enable_clk_scale_up(data);
  5362. if (ret) {
  5363. ret = -EIO;
  5364. goto exit_register_bus_bandwidth_needs;
  5365. }
  5366. ret = qseecom_dmabuf_cache_operations(dmabuf,
  5367. QSEECOM_CACHE_CLEAN);
  5368. if (ret) {
  5369. pr_err("cache operation failed %d\n", ret);
  5370. goto exit_disable_clock;
  5371. }
  5372. /* SCM_CALL to load the external elf */
  5373. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1, cmd_buf, cmd_len,
  5374. &resp, sizeof(resp));
  5375. if (ret) {
  5376. pr_err("scm_call to load failed : ret %d\n",
  5377. ret);
  5378. ret = -EFAULT;
  5379. goto exit_disable_clock;
  5380. }
  5381. ret = qseecom_dmabuf_cache_operations(dmabuf,
  5382. QSEECOM_CACHE_INVALIDATE);
  5383. if (ret) {
  5384. pr_err("cache operation failed %d\n", ret);
  5385. goto exit_disable_clock;
  5386. }
  5387. switch (resp.result) {
  5388. case QSEOS_RESULT_SUCCESS:
  5389. break;
  5390. case QSEOS_RESULT_INCOMPLETE:
  5391. pr_err("%s: qseos result incomplete\n", __func__);
  5392. ret = __qseecom_process_incomplete_cmd(data, &resp);
  5393. if (ret)
  5394. pr_err("process_incomplete_cmd failed: err: %d\n", ret);
  5395. break;
  5396. case QSEOS_RESULT_FAILURE:
  5397. pr_err("scm_call rsp.result is QSEOS_RESULT_FAILURE\n");
  5398. ret = -EFAULT;
  5399. break;
  5400. default:
  5401. pr_err("scm_call response result %d not supported\n",
  5402. resp.result);
  5403. ret = -EFAULT;
  5404. break;
  5405. }
  5406. exit_disable_clock:
  5407. __qseecom_disable_clk_scale_down(data);
  5408. exit_register_bus_bandwidth_needs:
  5409. if (qseecom.support_bus_scaling) {
  5410. mutex_lock(&qsee_bw_mutex);
  5411. uret = qseecom_unregister_bus_bandwidth_needs(data);
  5412. mutex_unlock(&qsee_bw_mutex);
  5413. if (uret)
  5414. pr_err("Failed to unregister bus bw needs %d, scm_call ret %d\n",
  5415. uret, ret);
  5416. }
  5417. exit_cpu_restore:
  5418. if (dmabuf) {
  5419. qseecom_vaddr_unmap(va, sgt, attach, dmabuf);
  5420. MAKE_NULL(sgt, attach, dmabuf);
  5421. }
  5422. return ret;
  5423. }
  5424. static int qseecom_unload_external_elf(struct qseecom_dev_handle *data)
  5425. {
  5426. int ret = 0;
  5427. struct qseecom_command_scm_resp resp;
  5428. struct qseecom_unload_app_ireq req;
  5429. /* unavailable client app */
  5430. data->type = QSEECOM_UNAVAILABLE_CLIENT_APP;
  5431. /* Populate the structure for sending scm call to unload image */
  5432. req.qsee_cmd_id = QSEOS_UNLOAD_EXTERNAL_ELF_COMMAND;
  5433. /* SCM_CALL to unload the external elf */
  5434. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1, &req,
  5435. sizeof(struct qseecom_unload_app_ireq),
  5436. &resp, sizeof(resp));
  5437. if (ret) {
  5438. pr_err("scm_call to unload failed : ret %d\n",
  5439. ret);
  5440. ret = -EFAULT;
  5441. goto qseecom_unload_external_elf_scm_err;
  5442. }
  5443. if (resp.result == QSEOS_RESULT_INCOMPLETE) {
  5444. ret = __qseecom_process_incomplete_cmd(data, &resp);
  5445. if (ret)
  5446. pr_err("process_incomplete_cmd fail err: %d\n",
  5447. ret);
  5448. } else {
  5449. if (resp.result != QSEOS_RESULT_SUCCESS) {
  5450. pr_err("scm_call to unload image failed resp.result =%d\n",
  5451. resp.result);
  5452. ret = -EFAULT;
  5453. }
  5454. }
  5455. qseecom_unload_external_elf_scm_err:
  5456. return ret;
  5457. }
  5458. static int qseecom_query_app_loaded(struct qseecom_dev_handle *data,
  5459. void __user *argp)
  5460. {
  5461. int32_t ret = 0;
  5462. struct qseecom_qseos_app_load_query query_req = { {0} };
  5463. struct qseecom_check_app_ireq req;
  5464. struct qseecom_registered_app_list *entry = NULL;
  5465. unsigned long flags = 0;
  5466. uint32_t app_arch = 0, app_id = 0;
  5467. bool found_app = false;
  5468. /* Copy the relevant information needed for loading the image */
  5469. if (copy_from_user(&query_req, (void __user *)argp,
  5470. sizeof(struct qseecom_qseos_app_load_query))) {
  5471. pr_err("copy_from_user failed\n");
  5472. ret = -EFAULT;
  5473. goto exit_free;
  5474. }
  5475. req.qsee_cmd_id = QSEOS_APP_LOOKUP_COMMAND;
  5476. query_req.app_name[MAX_APP_NAME_SIZE-1] = '\0';
  5477. strlcpy(req.app_name, query_req.app_name, MAX_APP_NAME_SIZE);
  5478. ret = __qseecom_check_app_exists(req, &app_id);
  5479. if (ret) {
  5480. pr_err(" scm call to check if app is loaded failed\n");
  5481. goto exit_free;
  5482. }
  5483. if (app_id) {
  5484. pr_debug("App id %d (%s) already exists\n", app_id,
  5485. (char *)(req.app_name));
  5486. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
  5487. list_for_each_entry(entry,
  5488. &qseecom.registered_app_list_head, list){
  5489. if (entry->app_id == app_id) {
  5490. app_arch = entry->app_arch;
  5491. if (entry->ref_cnt == U32_MAX) {
  5492. pr_err("App %d (%s) ref_cnt overflow\n",
  5493. app_id, req.app_name);
  5494. ret = -EINVAL;
  5495. spin_unlock_irqrestore(
  5496. &qseecom.registered_app_list_lock,
  5497. flags);
  5498. goto exit_free;
  5499. }
  5500. entry->ref_cnt++;
  5501. found_app = true;
  5502. break;
  5503. }
  5504. }
  5505. spin_unlock_irqrestore(
  5506. &qseecom.registered_app_list_lock, flags);
  5507. data->client.app_id = app_id;
  5508. query_req.app_id = app_id;
  5509. if (app_arch) {
  5510. data->client.app_arch = app_arch;
  5511. query_req.app_arch = app_arch;
  5512. } else {
  5513. data->client.app_arch = 0;
  5514. query_req.app_arch = 0;
  5515. }
  5516. strlcpy(data->client.app_name, query_req.app_name,
  5517. MAX_APP_NAME_SIZE);
  5518. /*
  5519. * If app was loaded by appsbl before and was not registered,
  5520. * regiser this app now.
  5521. */
  5522. if (!found_app) {
  5523. pr_debug("Register app %d [%s] which was loaded before\n",
  5524. ret, (char *)query_req.app_name);
  5525. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  5526. if (!entry) {
  5527. ret = -ENOMEM;
  5528. goto exit_free;
  5529. }
  5530. entry->app_id = app_id;
  5531. entry->ref_cnt = 1;
  5532. entry->app_arch = data->client.app_arch;
  5533. strlcpy(entry->app_name, data->client.app_name,
  5534. MAX_APP_NAME_SIZE);
  5535. entry->app_blocked = false;
  5536. entry->blocked_on_listener_id = 0;
  5537. entry->check_block = 0;
  5538. spin_lock_irqsave(&qseecom.registered_app_list_lock,
  5539. flags);
  5540. list_add_tail(&entry->list,
  5541. &qseecom.registered_app_list_head);
  5542. spin_unlock_irqrestore(
  5543. &qseecom.registered_app_list_lock, flags);
  5544. }
  5545. if (copy_to_user(argp, &query_req, sizeof(query_req))) {
  5546. pr_err("copy_to_user failed\n");
  5547. ret = -EFAULT;
  5548. goto exit_free;
  5549. }
  5550. ret = -EEXIST; /* app already loaded */
  5551. goto exit_free;
  5552. }
  5553. exit_free:
  5554. return ret; /* app not loaded */
  5555. }
  5556. static int __qseecom_get_ce_pipe_info(
  5557. enum qseecom_key_management_usage_type usage,
  5558. uint32_t *pipe, uint32_t **ce_hw, uint32_t unit)
  5559. {
  5560. int ret = -EINVAL;
  5561. int i, j;
  5562. struct qseecom_ce_info_use *p = NULL;
  5563. int total = 0;
  5564. struct qseecom_ce_pipe_entry *pcepipe;
  5565. switch (usage) {
  5566. case QSEOS_KM_USAGE_DISK_ENCRYPTION:
  5567. case QSEOS_KM_USAGE_UFS_ICE_DISK_ENCRYPTION:
  5568. case QSEOS_KM_USAGE_SDCC_ICE_DISK_ENCRYPTION:
  5569. if (qseecom.support_fde) {
  5570. p = qseecom.ce_info.fde;
  5571. total = qseecom.ce_info.num_fde;
  5572. } else {
  5573. pr_err("system does not support fde\n");
  5574. return -EINVAL;
  5575. }
  5576. break;
  5577. case QSEOS_KM_USAGE_FILE_ENCRYPTION:
  5578. if (qseecom.support_pfe) {
  5579. p = qseecom.ce_info.pfe;
  5580. total = qseecom.ce_info.num_pfe;
  5581. } else {
  5582. pr_err("system does not support pfe\n");
  5583. return -EINVAL;
  5584. }
  5585. break;
  5586. default:
  5587. pr_err("unsupported usage %d\n", usage);
  5588. return -EINVAL;
  5589. }
  5590. for (j = 0; j < total; j++) {
  5591. if (p->unit_num == unit) {
  5592. pcepipe = p->ce_pipe_entry;
  5593. for (i = 0; i < p->num_ce_pipe_entries; i++) {
  5594. (*ce_hw)[i] = pcepipe->ce_num;
  5595. *pipe = pcepipe->ce_pipe_pair;
  5596. pcepipe++;
  5597. }
  5598. ret = 0;
  5599. break;
  5600. }
  5601. p++;
  5602. }
  5603. return ret;
  5604. }
  5605. static int __qseecom_generate_and_save_key(struct qseecom_dev_handle *data,
  5606. enum qseecom_key_management_usage_type usage,
  5607. struct qseecom_key_generate_ireq *ireq)
  5608. {
  5609. struct qseecom_command_scm_resp resp;
  5610. int ret;
  5611. if (usage < QSEOS_KM_USAGE_DISK_ENCRYPTION ||
  5612. usage >= QSEOS_KM_USAGE_MAX) {
  5613. pr_err("Error:: unsupported usage %d\n", usage);
  5614. return -EFAULT;
  5615. }
  5616. ret = __qseecom_enable_clk(CLK_QSEE);
  5617. if (ret)
  5618. return ret;
  5619. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1,
  5620. ireq, sizeof(struct qseecom_key_generate_ireq),
  5621. &resp, sizeof(resp));
  5622. if (ret) {
  5623. if (ret == -EINVAL &&
  5624. resp.result == QSEOS_RESULT_FAIL_KEY_ID_EXISTS) {
  5625. pr_debug("Key ID exists.\n");
  5626. ret = 0;
  5627. } else {
  5628. pr_err("scm call to generate key failed : %d\n", ret);
  5629. ret = -EFAULT;
  5630. }
  5631. goto generate_key_exit;
  5632. }
  5633. switch (resp.result) {
  5634. case QSEOS_RESULT_SUCCESS:
  5635. break;
  5636. case QSEOS_RESULT_FAIL_KEY_ID_EXISTS:
  5637. pr_debug("Key ID exists.\n");
  5638. break;
  5639. case QSEOS_RESULT_INCOMPLETE:
  5640. ret = __qseecom_process_incomplete_cmd(data, &resp);
  5641. if (ret) {
  5642. if (resp.result == QSEOS_RESULT_FAIL_KEY_ID_EXISTS) {
  5643. pr_debug("Key ID exists.\n");
  5644. ret = 0;
  5645. } else {
  5646. pr_err("process_incomplete_cmd FAILED, resp.result %d\n",
  5647. resp.result);
  5648. }
  5649. }
  5650. break;
  5651. case QSEOS_RESULT_FAILURE:
  5652. default:
  5653. pr_err("gen key scm call failed resp.result %d\n", resp.result);
  5654. ret = -EINVAL;
  5655. break;
  5656. }
  5657. generate_key_exit:
  5658. __qseecom_disable_clk(CLK_QSEE);
  5659. return ret;
  5660. }
  5661. static int __qseecom_delete_saved_key(struct qseecom_dev_handle *data,
  5662. enum qseecom_key_management_usage_type usage,
  5663. struct qseecom_key_delete_ireq *ireq)
  5664. {
  5665. struct qseecom_command_scm_resp resp;
  5666. int ret;
  5667. if (usage < QSEOS_KM_USAGE_DISK_ENCRYPTION ||
  5668. usage >= QSEOS_KM_USAGE_MAX) {
  5669. pr_err("Error:: unsupported usage %d\n", usage);
  5670. return -EFAULT;
  5671. }
  5672. ret = __qseecom_enable_clk(CLK_QSEE);
  5673. if (ret)
  5674. return ret;
  5675. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1,
  5676. ireq, sizeof(struct qseecom_key_delete_ireq),
  5677. &resp, sizeof(struct qseecom_command_scm_resp));
  5678. if (ret) {
  5679. if (ret == -EINVAL &&
  5680. resp.result == QSEOS_RESULT_FAIL_MAX_ATTEMPT) {
  5681. pr_debug("Max attempts to input password reached.\n");
  5682. ret = -ERANGE;
  5683. } else {
  5684. pr_err("scm call to delete key failed : %d\n", ret);
  5685. ret = -EFAULT;
  5686. }
  5687. goto del_key_exit;
  5688. }
  5689. switch (resp.result) {
  5690. case QSEOS_RESULT_SUCCESS:
  5691. break;
  5692. case QSEOS_RESULT_INCOMPLETE:
  5693. ret = __qseecom_process_incomplete_cmd(data, &resp);
  5694. if (ret) {
  5695. pr_err("process_incomplete_cmd FAILED, resp.result %d\n",
  5696. resp.result);
  5697. if (resp.result == QSEOS_RESULT_FAIL_MAX_ATTEMPT) {
  5698. pr_debug("Max attempts to input password reached.\n");
  5699. ret = -ERANGE;
  5700. }
  5701. }
  5702. break;
  5703. case QSEOS_RESULT_FAIL_MAX_ATTEMPT:
  5704. pr_debug("Max attempts to input password reached.\n");
  5705. ret = -ERANGE;
  5706. break;
  5707. case QSEOS_RESULT_FAILURE:
  5708. default:
  5709. pr_err("Delete key scm call failed resp.result %d\n",
  5710. resp.result);
  5711. ret = -EINVAL;
  5712. break;
  5713. }
  5714. del_key_exit:
  5715. __qseecom_disable_clk(CLK_QSEE);
  5716. return ret;
  5717. }
  5718. static int __qseecom_set_clear_ce_key(struct qseecom_dev_handle *data,
  5719. enum qseecom_key_management_usage_type usage,
  5720. struct qseecom_key_select_ireq *ireq)
  5721. {
  5722. struct qseecom_command_scm_resp resp;
  5723. int ret;
  5724. if (usage < QSEOS_KM_USAGE_DISK_ENCRYPTION ||
  5725. usage >= QSEOS_KM_USAGE_MAX) {
  5726. pr_err("Error:: unsupported usage %d\n", usage);
  5727. return -EFAULT;
  5728. }
  5729. ret = __qseecom_enable_clk(CLK_QSEE);
  5730. if (ret)
  5731. return ret;
  5732. if (qseecom.qsee.instance != qseecom.ce_drv.instance) {
  5733. ret = __qseecom_enable_clk(CLK_CE_DRV);
  5734. if (ret)
  5735. return ret;
  5736. }
  5737. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1,
  5738. ireq, sizeof(struct qseecom_key_select_ireq),
  5739. &resp, sizeof(struct qseecom_command_scm_resp));
  5740. if (ret) {
  5741. if (ret == -EINVAL &&
  5742. resp.result == QSEOS_RESULT_FAIL_MAX_ATTEMPT) {
  5743. pr_debug("Max attempts to input password reached.\n");
  5744. ret = -ERANGE;
  5745. } else if (ret == -EINVAL &&
  5746. resp.result == QSEOS_RESULT_FAIL_PENDING_OPERATION) {
  5747. pr_debug("Set Key operation under processing...\n");
  5748. ret = QSEOS_RESULT_FAIL_PENDING_OPERATION;
  5749. } else {
  5750. pr_err("scm call to set QSEOS_PIPE_ENC key failed : %d\n",
  5751. ret);
  5752. ret = -EFAULT;
  5753. }
  5754. goto set_key_exit;
  5755. }
  5756. switch (resp.result) {
  5757. case QSEOS_RESULT_SUCCESS:
  5758. break;
  5759. case QSEOS_RESULT_INCOMPLETE:
  5760. ret = __qseecom_process_incomplete_cmd(data, &resp);
  5761. if (ret) {
  5762. pr_err("process_incomplete_cmd FAILED, resp.result %d\n",
  5763. resp.result);
  5764. if (resp.result ==
  5765. QSEOS_RESULT_FAIL_PENDING_OPERATION) {
  5766. pr_debug("Set Key operation under processing...\n");
  5767. ret = QSEOS_RESULT_FAIL_PENDING_OPERATION;
  5768. }
  5769. if (resp.result == QSEOS_RESULT_FAIL_MAX_ATTEMPT) {
  5770. pr_debug("Max attempts to input password reached.\n");
  5771. ret = -ERANGE;
  5772. }
  5773. }
  5774. break;
  5775. case QSEOS_RESULT_FAIL_MAX_ATTEMPT:
  5776. pr_debug("Max attempts to input password reached.\n");
  5777. ret = -ERANGE;
  5778. break;
  5779. case QSEOS_RESULT_FAIL_PENDING_OPERATION:
  5780. pr_debug("Set Key operation under processing...\n");
  5781. ret = QSEOS_RESULT_FAIL_PENDING_OPERATION;
  5782. break;
  5783. case QSEOS_RESULT_FAILURE:
  5784. default:
  5785. pr_err("Set key scm call failed resp.result %d\n", resp.result);
  5786. ret = -EINVAL;
  5787. break;
  5788. }
  5789. set_key_exit:
  5790. __qseecom_disable_clk(CLK_QSEE);
  5791. if (qseecom.qsee.instance != qseecom.ce_drv.instance)
  5792. __qseecom_disable_clk(CLK_CE_DRV);
  5793. return ret;
  5794. }
  5795. static int __qseecom_update_current_key_user_info(
  5796. struct qseecom_dev_handle *data,
  5797. enum qseecom_key_management_usage_type usage,
  5798. struct qseecom_key_userinfo_update_ireq *ireq)
  5799. {
  5800. struct qseecom_command_scm_resp resp;
  5801. int ret;
  5802. if (usage < QSEOS_KM_USAGE_DISK_ENCRYPTION ||
  5803. usage >= QSEOS_KM_USAGE_MAX) {
  5804. pr_err("Error:: unsupported usage %d\n", usage);
  5805. return -EFAULT;
  5806. }
  5807. ret = __qseecom_enable_clk(CLK_QSEE);
  5808. if (ret)
  5809. return ret;
  5810. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1,
  5811. ireq, sizeof(struct qseecom_key_userinfo_update_ireq),
  5812. &resp, sizeof(struct qseecom_command_scm_resp));
  5813. if (ret) {
  5814. if (ret == -EINVAL &&
  5815. resp.result == QSEOS_RESULT_FAIL_PENDING_OPERATION) {
  5816. pr_debug("Set Key operation under processing...\n");
  5817. ret = QSEOS_RESULT_FAIL_PENDING_OPERATION;
  5818. } else {
  5819. pr_err("scm call to update key userinfo failed: %d\n",
  5820. ret);
  5821. __qseecom_disable_clk(CLK_QSEE);
  5822. return -EFAULT;
  5823. }
  5824. }
  5825. switch (resp.result) {
  5826. case QSEOS_RESULT_SUCCESS:
  5827. break;
  5828. case QSEOS_RESULT_INCOMPLETE:
  5829. ret = __qseecom_process_incomplete_cmd(data, &resp);
  5830. if (resp.result ==
  5831. QSEOS_RESULT_FAIL_PENDING_OPERATION) {
  5832. pr_debug("Set Key operation under processing...\n");
  5833. ret = QSEOS_RESULT_FAIL_PENDING_OPERATION;
  5834. }
  5835. if (ret)
  5836. pr_err("process_incomplete_cmd FAILED, resp.result %d\n",
  5837. resp.result);
  5838. break;
  5839. case QSEOS_RESULT_FAIL_PENDING_OPERATION:
  5840. pr_debug("Update Key operation under processing...\n");
  5841. ret = QSEOS_RESULT_FAIL_PENDING_OPERATION;
  5842. break;
  5843. case QSEOS_RESULT_FAILURE:
  5844. default:
  5845. pr_err("Set key scm call failed resp.result %d\n", resp.result);
  5846. ret = -EINVAL;
  5847. break;
  5848. }
  5849. __qseecom_disable_clk(CLK_QSEE);
  5850. return ret;
  5851. }
  5852. static int qseecom_enable_ice_setup(int usage)
  5853. {
  5854. int ret = 0;
  5855. if (usage == QSEOS_KM_USAGE_UFS_ICE_DISK_ENCRYPTION)
  5856. ret = qcom_ice_setup_ice_hw("ufs", true);
  5857. else if (usage == QSEOS_KM_USAGE_SDCC_ICE_DISK_ENCRYPTION)
  5858. ret = qcom_ice_setup_ice_hw("sdcc", true);
  5859. return ret;
  5860. }
  5861. static int qseecom_disable_ice_setup(int usage)
  5862. {
  5863. int ret = 0;
  5864. if (usage == QSEOS_KM_USAGE_UFS_ICE_DISK_ENCRYPTION)
  5865. ret = qcom_ice_setup_ice_hw("ufs", false);
  5866. else if (usage == QSEOS_KM_USAGE_SDCC_ICE_DISK_ENCRYPTION)
  5867. ret = qcom_ice_setup_ice_hw("sdcc", false);
  5868. return ret;
  5869. }
  5870. static int qseecom_get_ce_hw_instance(uint32_t unit, uint32_t usage)
  5871. {
  5872. struct qseecom_ce_info_use *pce_info_use, *p;
  5873. int total = 0;
  5874. int i;
  5875. switch (usage) {
  5876. case QSEOS_KM_USAGE_DISK_ENCRYPTION:
  5877. case QSEOS_KM_USAGE_UFS_ICE_DISK_ENCRYPTION:
  5878. case QSEOS_KM_USAGE_SDCC_ICE_DISK_ENCRYPTION:
  5879. p = qseecom.ce_info.fde;
  5880. total = qseecom.ce_info.num_fde;
  5881. break;
  5882. case QSEOS_KM_USAGE_FILE_ENCRYPTION:
  5883. p = qseecom.ce_info.pfe;
  5884. total = qseecom.ce_info.num_pfe;
  5885. break;
  5886. default:
  5887. pr_err("unsupported usage %d\n", usage);
  5888. return -EINVAL;
  5889. }
  5890. pce_info_use = NULL;
  5891. for (i = 0; i < total; i++) {
  5892. if (p->unit_num == unit) {
  5893. pce_info_use = p;
  5894. break;
  5895. }
  5896. p++;
  5897. }
  5898. if (!pce_info_use) {
  5899. pr_err("can not find %d\n", unit);
  5900. return -EINVAL;
  5901. }
  5902. return pce_info_use->num_ce_pipe_entries;
  5903. }
  5904. static int qseecom_create_key(struct qseecom_dev_handle *data,
  5905. void __user *argp)
  5906. {
  5907. int i;
  5908. uint32_t *ce_hw = NULL;
  5909. uint32_t pipe = 0;
  5910. int ret = 0;
  5911. uint32_t flags = 0;
  5912. struct qseecom_create_key_req create_key_req;
  5913. struct qseecom_key_generate_ireq generate_key_ireq;
  5914. struct qseecom_key_select_ireq set_key_ireq;
  5915. uint32_t entries = 0;
  5916. ret = copy_from_user(&create_key_req, argp, sizeof(create_key_req));
  5917. if (ret) {
  5918. pr_err("copy_from_user failed\n");
  5919. return ret;
  5920. }
  5921. if (create_key_req.usage < QSEOS_KM_USAGE_DISK_ENCRYPTION ||
  5922. create_key_req.usage >= QSEOS_KM_USAGE_MAX) {
  5923. pr_err("unsupported usage %d\n", create_key_req.usage);
  5924. ret = -EFAULT;
  5925. return ret;
  5926. }
  5927. entries = qseecom_get_ce_hw_instance(DEFAULT_CE_INFO_UNIT,
  5928. create_key_req.usage);
  5929. if (entries <= 0) {
  5930. pr_err("no ce instance for usage %d instance %d\n",
  5931. DEFAULT_CE_INFO_UNIT, create_key_req.usage);
  5932. ret = -EINVAL;
  5933. return ret;
  5934. }
  5935. ce_hw = kcalloc(entries, sizeof(*ce_hw), GFP_KERNEL);
  5936. if (!ce_hw) {
  5937. ret = -ENOMEM;
  5938. return ret;
  5939. }
  5940. ret = __qseecom_get_ce_pipe_info(create_key_req.usage, &pipe, &ce_hw,
  5941. DEFAULT_CE_INFO_UNIT);
  5942. if (ret) {
  5943. pr_err("Failed to retrieve pipe/ce_hw info: %d\n", ret);
  5944. ret = -EINVAL;
  5945. goto free_buf;
  5946. }
  5947. if (qseecom.fde_key_size)
  5948. flags |= QSEECOM_ICE_FDE_KEY_SIZE_32_BYTE;
  5949. else
  5950. flags |= QSEECOM_ICE_FDE_KEY_SIZE_16_BYTE;
  5951. if (qseecom.enable_key_wrap_in_ks)
  5952. flags |= ENABLE_KEY_WRAP_IN_KS;
  5953. generate_key_ireq.flags = flags;
  5954. generate_key_ireq.qsee_command_id = QSEOS_GENERATE_KEY;
  5955. memset((void *)generate_key_ireq.key_id,
  5956. 0, QSEECOM_KEY_ID_SIZE);
  5957. memset((void *)generate_key_ireq.hash32,
  5958. 0, QSEECOM_HASH_SIZE);
  5959. memcpy((void *)generate_key_ireq.key_id,
  5960. (void *)key_id_array[create_key_req.usage].desc,
  5961. QSEECOM_KEY_ID_SIZE);
  5962. memcpy((void *)generate_key_ireq.hash32,
  5963. (void *)create_key_req.hash32,
  5964. QSEECOM_HASH_SIZE);
  5965. ret = __qseecom_generate_and_save_key(data,
  5966. create_key_req.usage, &generate_key_ireq);
  5967. if (ret) {
  5968. pr_err("Failed to generate key on storage: %d\n", ret);
  5969. goto free_buf;
  5970. }
  5971. for (i = 0; i < entries; i++) {
  5972. set_key_ireq.qsee_command_id = QSEOS_SET_KEY;
  5973. if (create_key_req.usage ==
  5974. QSEOS_KM_USAGE_UFS_ICE_DISK_ENCRYPTION) {
  5975. set_key_ireq.ce = QSEECOM_UFS_ICE_CE_NUM;
  5976. set_key_ireq.pipe = QSEECOM_ICE_FDE_KEY_INDEX;
  5977. } else if (create_key_req.usage ==
  5978. QSEOS_KM_USAGE_SDCC_ICE_DISK_ENCRYPTION) {
  5979. set_key_ireq.ce = QSEECOM_SDCC_ICE_CE_NUM;
  5980. set_key_ireq.pipe = QSEECOM_ICE_FDE_KEY_INDEX;
  5981. } else {
  5982. set_key_ireq.ce = ce_hw[i];
  5983. set_key_ireq.pipe = pipe;
  5984. }
  5985. set_key_ireq.flags = flags;
  5986. /* set both PIPE_ENC and PIPE_ENC_XTS*/
  5987. set_key_ireq.pipe_type = QSEOS_PIPE_ENC|QSEOS_PIPE_ENC_XTS;
  5988. memset((void *)set_key_ireq.key_id, 0, QSEECOM_KEY_ID_SIZE);
  5989. memset((void *)set_key_ireq.hash32, 0, QSEECOM_HASH_SIZE);
  5990. memcpy((void *)set_key_ireq.key_id,
  5991. (void *)key_id_array[create_key_req.usage].desc,
  5992. QSEECOM_KEY_ID_SIZE);
  5993. memcpy((void *)set_key_ireq.hash32,
  5994. (void *)create_key_req.hash32,
  5995. QSEECOM_HASH_SIZE);
  5996. /*
  5997. * It will return false if it is GPCE based crypto instance or
  5998. * ICE is setup properly
  5999. */
  6000. ret = qseecom_enable_ice_setup(create_key_req.usage);
  6001. if (ret)
  6002. goto free_buf;
  6003. do {
  6004. ret = __qseecom_set_clear_ce_key(data,
  6005. create_key_req.usage,
  6006. &set_key_ireq);
  6007. /*
  6008. * wait a little before calling scm again to let other
  6009. * processes run
  6010. */
  6011. if (ret == QSEOS_RESULT_FAIL_PENDING_OPERATION)
  6012. msleep(50);
  6013. } while (ret == QSEOS_RESULT_FAIL_PENDING_OPERATION);
  6014. qseecom_disable_ice_setup(create_key_req.usage);
  6015. if (ret) {
  6016. pr_err("Failed to create key: pipe %d, ce %d: %d\n",
  6017. pipe, ce_hw[i], ret);
  6018. goto free_buf;
  6019. } else {
  6020. pr_err("Set the key successfully\n");
  6021. if ((create_key_req.usage ==
  6022. QSEOS_KM_USAGE_UFS_ICE_DISK_ENCRYPTION) ||
  6023. (create_key_req.usage ==
  6024. QSEOS_KM_USAGE_SDCC_ICE_DISK_ENCRYPTION))
  6025. goto free_buf;
  6026. }
  6027. }
  6028. free_buf:
  6029. kfree_sensitive(ce_hw);
  6030. return ret;
  6031. }
  6032. static int qseecom_wipe_key(struct qseecom_dev_handle *data,
  6033. void __user *argp)
  6034. {
  6035. uint32_t *ce_hw = NULL;
  6036. uint32_t pipe = 0;
  6037. int ret = 0;
  6038. uint32_t flags = 0;
  6039. int i, j;
  6040. struct qseecom_wipe_key_req wipe_key_req;
  6041. struct qseecom_key_delete_ireq delete_key_ireq;
  6042. struct qseecom_key_select_ireq clear_key_ireq;
  6043. uint32_t entries = 0;
  6044. ret = copy_from_user(&wipe_key_req, argp, sizeof(wipe_key_req));
  6045. if (ret) {
  6046. pr_err("copy_from_user failed\n");
  6047. return ret;
  6048. }
  6049. if (wipe_key_req.usage < QSEOS_KM_USAGE_DISK_ENCRYPTION ||
  6050. wipe_key_req.usage >= QSEOS_KM_USAGE_MAX) {
  6051. pr_err("unsupported usage %d\n", wipe_key_req.usage);
  6052. ret = -EFAULT;
  6053. return ret;
  6054. }
  6055. entries = qseecom_get_ce_hw_instance(DEFAULT_CE_INFO_UNIT,
  6056. wipe_key_req.usage);
  6057. if (entries <= 0) {
  6058. pr_err("no ce instance for usage %d instance %d\n",
  6059. DEFAULT_CE_INFO_UNIT, wipe_key_req.usage);
  6060. ret = -EINVAL;
  6061. return ret;
  6062. }
  6063. ce_hw = kcalloc(entries, sizeof(*ce_hw), GFP_KERNEL);
  6064. if (!ce_hw) {
  6065. ret = -ENOMEM;
  6066. return ret;
  6067. }
  6068. ret = __qseecom_get_ce_pipe_info(wipe_key_req.usage, &pipe, &ce_hw,
  6069. DEFAULT_CE_INFO_UNIT);
  6070. if (ret) {
  6071. pr_err("Failed to retrieve pipe/ce_hw info: %d\n", ret);
  6072. ret = -EINVAL;
  6073. goto free_buf;
  6074. }
  6075. if (wipe_key_req.wipe_key_flag) {
  6076. delete_key_ireq.flags = flags;
  6077. delete_key_ireq.qsee_command_id = QSEOS_DELETE_KEY;
  6078. memset((void *)delete_key_ireq.key_id, 0, QSEECOM_KEY_ID_SIZE);
  6079. memcpy((void *)delete_key_ireq.key_id,
  6080. (void *)key_id_array[wipe_key_req.usage].desc,
  6081. QSEECOM_KEY_ID_SIZE);
  6082. memset((void *)delete_key_ireq.hash32, 0, QSEECOM_HASH_SIZE);
  6083. ret = __qseecom_delete_saved_key(data, wipe_key_req.usage,
  6084. &delete_key_ireq);
  6085. if (ret) {
  6086. pr_err("Failed to delete key from ssd storage: %d\n",
  6087. ret);
  6088. ret = -EFAULT;
  6089. goto free_buf;
  6090. }
  6091. }
  6092. for (j = 0; j < entries; j++) {
  6093. clear_key_ireq.qsee_command_id = QSEOS_SET_KEY;
  6094. if (wipe_key_req.usage ==
  6095. QSEOS_KM_USAGE_UFS_ICE_DISK_ENCRYPTION) {
  6096. clear_key_ireq.ce = QSEECOM_UFS_ICE_CE_NUM;
  6097. clear_key_ireq.pipe = QSEECOM_ICE_FDE_KEY_INDEX;
  6098. } else if (wipe_key_req.usage ==
  6099. QSEOS_KM_USAGE_SDCC_ICE_DISK_ENCRYPTION) {
  6100. clear_key_ireq.ce = QSEECOM_SDCC_ICE_CE_NUM;
  6101. clear_key_ireq.pipe = QSEECOM_ICE_FDE_KEY_INDEX;
  6102. } else {
  6103. clear_key_ireq.ce = ce_hw[j];
  6104. clear_key_ireq.pipe = pipe;
  6105. }
  6106. clear_key_ireq.flags = flags;
  6107. clear_key_ireq.pipe_type = QSEOS_PIPE_ENC|QSEOS_PIPE_ENC_XTS;
  6108. for (i = 0; i < QSEECOM_KEY_ID_SIZE; i++)
  6109. clear_key_ireq.key_id[i] = QSEECOM_INVALID_KEY_ID;
  6110. memset((void *)clear_key_ireq.hash32, 0, QSEECOM_HASH_SIZE);
  6111. /*
  6112. * It will return false if it is GPCE based crypto instance or
  6113. * ICE is setup properly
  6114. */
  6115. ret = qseecom_enable_ice_setup(wipe_key_req.usage);
  6116. if (ret)
  6117. goto free_buf;
  6118. ret = __qseecom_set_clear_ce_key(data, wipe_key_req.usage,
  6119. &clear_key_ireq);
  6120. qseecom_disable_ice_setup(wipe_key_req.usage);
  6121. if (ret) {
  6122. pr_err("Failed to wipe key: pipe %d, ce %d: %d\n",
  6123. pipe, ce_hw[j], ret);
  6124. ret = -EFAULT;
  6125. goto free_buf;
  6126. }
  6127. }
  6128. free_buf:
  6129. kfree_sensitive(ce_hw);
  6130. return ret;
  6131. }
  6132. static int qseecom_update_key_user_info(struct qseecom_dev_handle *data,
  6133. void __user *argp)
  6134. {
  6135. int ret = 0;
  6136. uint32_t flags = 0;
  6137. struct qseecom_update_key_userinfo_req update_key_req;
  6138. struct qseecom_key_userinfo_update_ireq ireq;
  6139. ret = copy_from_user(&update_key_req, argp, sizeof(update_key_req));
  6140. if (ret) {
  6141. pr_err("copy_from_user failed\n");
  6142. return ret;
  6143. }
  6144. if (update_key_req.usage < QSEOS_KM_USAGE_DISK_ENCRYPTION ||
  6145. update_key_req.usage >= QSEOS_KM_USAGE_MAX) {
  6146. pr_err("Error:: unsupported usage %d\n", update_key_req.usage);
  6147. return -EFAULT;
  6148. }
  6149. ireq.qsee_command_id = QSEOS_UPDATE_KEY_USERINFO;
  6150. if (qseecom.fde_key_size)
  6151. flags |= QSEECOM_ICE_FDE_KEY_SIZE_32_BYTE;
  6152. else
  6153. flags |= QSEECOM_ICE_FDE_KEY_SIZE_16_BYTE;
  6154. ireq.flags = flags;
  6155. memset(ireq.key_id, 0, QSEECOM_KEY_ID_SIZE);
  6156. memset((void *)ireq.current_hash32, 0, QSEECOM_HASH_SIZE);
  6157. memset((void *)ireq.new_hash32, 0, QSEECOM_HASH_SIZE);
  6158. memcpy((void *)ireq.key_id,
  6159. (void *)key_id_array[update_key_req.usage].desc,
  6160. QSEECOM_KEY_ID_SIZE);
  6161. memcpy((void *)ireq.current_hash32,
  6162. (void *)update_key_req.current_hash32, QSEECOM_HASH_SIZE);
  6163. memcpy((void *)ireq.new_hash32,
  6164. (void *)update_key_req.new_hash32, QSEECOM_HASH_SIZE);
  6165. do {
  6166. ret = __qseecom_update_current_key_user_info(data,
  6167. update_key_req.usage,
  6168. &ireq);
  6169. /*
  6170. * wait a little before calling scm again to let other
  6171. * processes run
  6172. */
  6173. if (ret == QSEOS_RESULT_FAIL_PENDING_OPERATION)
  6174. msleep(50);
  6175. } while (ret == QSEOS_RESULT_FAIL_PENDING_OPERATION);
  6176. if (ret) {
  6177. pr_err("Failed to update key info: %d\n", ret);
  6178. return ret;
  6179. }
  6180. return ret;
  6181. }
  6182. static int qseecom_is_es_activated(void __user *argp)
  6183. {
  6184. struct qseecom_is_es_activated_req req = {0};
  6185. struct qseecom_command_scm_resp resp;
  6186. int ret;
  6187. if (qseecom.qsee_version < QSEE_VERSION_04) {
  6188. pr_err("invalid qsee version\n");
  6189. return -ENODEV;
  6190. }
  6191. if (argp == NULL) {
  6192. pr_err("arg is null\n");
  6193. return -EINVAL;
  6194. }
  6195. ret = qseecom_scm_call(SCM_SVC_ES, SCM_IS_ACTIVATED_ID,
  6196. &req, sizeof(req), &resp, sizeof(resp));
  6197. if (ret) {
  6198. pr_err("scm_call failed\n");
  6199. return ret;
  6200. }
  6201. req.is_activated = resp.result;
  6202. ret = copy_to_user(argp, &req, sizeof(req));
  6203. if (ret) {
  6204. pr_err("copy_to_user failed\n");
  6205. return ret;
  6206. }
  6207. return 0;
  6208. }
  6209. static int qseecom_save_partition_hash(void __user *argp)
  6210. {
  6211. struct qseecom_save_partition_hash_req req;
  6212. struct qseecom_command_scm_resp resp;
  6213. int ret;
  6214. memset(&resp, 0x00, sizeof(resp));
  6215. if (qseecom.qsee_version < QSEE_VERSION_04) {
  6216. pr_err("invalid qsee version\n");
  6217. return -ENODEV;
  6218. }
  6219. if (argp == NULL) {
  6220. pr_err("arg is null\n");
  6221. return -EINVAL;
  6222. }
  6223. ret = copy_from_user(&req, argp, sizeof(req));
  6224. if (ret) {
  6225. pr_err("copy_from_user failed\n");
  6226. return ret;
  6227. }
  6228. ret = qseecom_scm_call(SCM_SVC_ES, SCM_SAVE_PARTITION_HASH_ID,
  6229. (void *)&req, sizeof(req), (void *)&resp, sizeof(resp));
  6230. if (ret) {
  6231. pr_err("qseecom_scm_call failed\n");
  6232. return ret;
  6233. }
  6234. return 0;
  6235. }
  6236. static int qseecom_mdtp_cipher_dip(void __user *argp)
  6237. {
  6238. struct qseecom_mdtp_cipher_dip_req req;
  6239. u32 tzbuflenin, tzbuflenout;
  6240. char *tzbufin = NULL, *tzbufout = NULL;
  6241. struct qseecom_scm_desc desc = {0};
  6242. int ret;
  6243. phys_addr_t pain, paout;
  6244. struct qtee_shm shmin = {0}, shmout = {0};
  6245. do {
  6246. /* Copy the parameters from userspace */
  6247. if (argp == NULL) {
  6248. pr_err("arg is null\n");
  6249. ret = -EINVAL;
  6250. break;
  6251. }
  6252. ret = copy_from_user(&req, argp, sizeof(req));
  6253. if (ret) {
  6254. pr_err("copy_from_user failed, ret= %d\n", ret);
  6255. break;
  6256. }
  6257. if (req.in_buf == NULL || req.out_buf == NULL ||
  6258. req.in_buf_size == 0 || req.in_buf_size > MAX_DIP ||
  6259. req.out_buf_size == 0 || req.out_buf_size > MAX_DIP ||
  6260. req.direction > 1) {
  6261. pr_err("invalid parameters\n");
  6262. ret = -EINVAL;
  6263. break;
  6264. }
  6265. /* Copy the input buffer from userspace to kernel space */
  6266. tzbuflenin = PAGE_ALIGN(req.in_buf_size);
  6267. tzbufin = __qseecom_alloc_tzbuf(tzbuflenin, &pain, &shmin);
  6268. if (!tzbufin) {
  6269. pr_err("error allocating in buffer\n");
  6270. ret = -ENOMEM;
  6271. break;
  6272. }
  6273. ret = copy_from_user(tzbufin, (void __user *)req.in_buf,
  6274. req.in_buf_size);
  6275. if (ret) {
  6276. pr_err("copy_from_user failed, ret=%d\n", ret);
  6277. break;
  6278. }
  6279. qtee_shmbridge_flush_shm_buf(&shmin);
  6280. /* Prepare the output buffer in kernel space */
  6281. tzbuflenout = PAGE_ALIGN(req.out_buf_size);
  6282. tzbufout = __qseecom_alloc_tzbuf(tzbuflenout, &paout, &shmout);
  6283. if (!tzbufout) {
  6284. pr_err("error allocating out buffer\n");
  6285. ret = -ENOMEM;
  6286. break;
  6287. }
  6288. qtee_shmbridge_flush_shm_buf(&shmout);
  6289. /* Send the command to TZ */
  6290. desc.arginfo = TZ_MDTP_CIPHER_DIP_ID_PARAM_ID;
  6291. desc.args[0] = pain;
  6292. desc.args[1] = req.in_buf_size;
  6293. desc.args[2] = paout;
  6294. desc.args[3] = req.out_buf_size;
  6295. desc.args[4] = req.direction;
  6296. ret = __qseecom_enable_clk(CLK_QSEE);
  6297. if (ret)
  6298. break;
  6299. ret = __qseecom_scm_call2_locked(TZ_MDTP_CIPHER_DIP_ID, &desc);
  6300. __qseecom_disable_clk(CLK_QSEE);
  6301. if (ret) {
  6302. pr_err("failed for SCM_SVC_MDTP, ret=%d\n",
  6303. ret);
  6304. break;
  6305. }
  6306. /* Copy the output buffer from kernel space to userspace */
  6307. qtee_shmbridge_flush_shm_buf(&shmout);
  6308. ret = copy_to_user((void __user *)req.out_buf,
  6309. tzbufout, req.out_buf_size);
  6310. if (ret) {
  6311. pr_err("copy_to_user failed, ret=%d\n", ret);
  6312. break;
  6313. }
  6314. } while (0);
  6315. __qseecom_free_tzbuf(&shmin);
  6316. __qseecom_free_tzbuf(&shmout);
  6317. return ret;
  6318. }
  6319. static int __qseecom_qteec_validate_msg(struct qseecom_dev_handle *data,
  6320. struct qseecom_qteec_req *req)
  6321. {
  6322. if (!data || !data->client.sb_virt) {
  6323. pr_err("Client or client buf is not initialized\n");
  6324. return -EINVAL;
  6325. }
  6326. if (data->type != QSEECOM_CLIENT_APP)
  6327. return -EFAULT;
  6328. if (req->req_len > UINT_MAX - req->resp_len) {
  6329. pr_err("Integer overflow detected in req_len & rsp_len\n");
  6330. return -EINVAL;
  6331. }
  6332. if (req->req_len + req->resp_len > data->client.sb_length) {
  6333. pr_debug("Not enough memory to fit cmd_buf.\n");
  6334. pr_debug("resp_buf. Required: %u, Available: %zu\n",
  6335. (req->req_len + req->resp_len), data->client.sb_length);
  6336. return -ENOMEM;
  6337. }
  6338. if (req->req_ptr == NULL || req->resp_ptr == NULL) {
  6339. pr_err("cmd buffer or response buffer is null\n");
  6340. return -EINVAL;
  6341. }
  6342. if (((uintptr_t)req->req_ptr <
  6343. data->client.user_virt_sb_base) ||
  6344. ((uintptr_t)req->req_ptr >=
  6345. (data->client.user_virt_sb_base + data->client.sb_length))) {
  6346. pr_err("cmd buffer address not within shared bufffer\n");
  6347. return -EINVAL;
  6348. }
  6349. if (((uintptr_t)req->resp_ptr <
  6350. data->client.user_virt_sb_base) ||
  6351. ((uintptr_t)req->resp_ptr >=
  6352. (data->client.user_virt_sb_base + data->client.sb_length))) {
  6353. pr_err("response buffer address not within shared bufffer\n");
  6354. return -EINVAL;
  6355. }
  6356. if ((req->req_len == 0) || (req->resp_len == 0)) {
  6357. pr_err("cmd buf lengtgh/response buf length not valid\n");
  6358. return -EINVAL;
  6359. }
  6360. if ((uintptr_t)req->req_ptr > (ULONG_MAX - req->req_len)) {
  6361. pr_err("Integer overflow in req_len & req_ptr\n");
  6362. return -EINVAL;
  6363. }
  6364. if ((uintptr_t)req->resp_ptr > (ULONG_MAX - req->resp_len)) {
  6365. pr_err("Integer overflow in resp_len & resp_ptr\n");
  6366. return -EINVAL;
  6367. }
  6368. if (data->client.user_virt_sb_base >
  6369. (ULONG_MAX - data->client.sb_length)) {
  6370. pr_err("Integer overflow in user_virt_sb_base & sb_length\n");
  6371. return -EINVAL;
  6372. }
  6373. if ((((uintptr_t)req->req_ptr + req->req_len) >
  6374. ((uintptr_t)data->client.user_virt_sb_base +
  6375. data->client.sb_length)) ||
  6376. (((uintptr_t)req->resp_ptr + req->resp_len) >
  6377. ((uintptr_t)data->client.user_virt_sb_base +
  6378. data->client.sb_length))) {
  6379. pr_err("cmd buf or resp buf is out of shared buffer region\n");
  6380. return -EINVAL;
  6381. }
  6382. return 0;
  6383. }
  6384. static int __qseecom_qteec_handle_pre_alc_fd(struct qseecom_dev_handle *data,
  6385. uint32_t fd_idx, struct sg_table *sg_ptr)
  6386. {
  6387. struct scatterlist *sg = sg_ptr->sgl;
  6388. struct qseecom_sg_entry *sg_entry;
  6389. void *buf;
  6390. uint i;
  6391. size_t size;
  6392. dma_addr_t coh_pmem;
  6393. if (fd_idx >= MAX_ION_FD) {
  6394. pr_err("fd_idx [%d] is invalid\n", fd_idx);
  6395. return -ENOMEM;
  6396. }
  6397. /*
  6398. * Allocate a buffer, populate it with number of entry plus
  6399. * each sg entry's phy addr and length; then return the
  6400. * phy_addr of the buffer.
  6401. */
  6402. size = sizeof(uint32_t) +
  6403. sizeof(struct qseecom_sg_entry) * sg_ptr->nents;
  6404. size = (size + PAGE_SIZE) & PAGE_MASK;
  6405. buf = dma_alloc_coherent(qseecom.dev,
  6406. size, &coh_pmem, GFP_KERNEL);
  6407. if (buf == NULL)
  6408. return -ENOMEM;
  6409. *(uint32_t *)buf = sg_ptr->nents;
  6410. sg_entry = (struct qseecom_sg_entry *) (buf + sizeof(uint32_t));
  6411. for (i = 0; i < sg_ptr->nents; i++) {
  6412. sg_entry->phys_addr = (uint32_t)sg_dma_address(sg);
  6413. sg_entry->len = sg->length;
  6414. sg_entry++;
  6415. sg = sg_next(sg);
  6416. }
  6417. data->client.sec_buf_fd[fd_idx].is_sec_buf_fd = true;
  6418. data->client.sec_buf_fd[fd_idx].vbase = buf;
  6419. data->client.sec_buf_fd[fd_idx].pbase = coh_pmem;
  6420. data->client.sec_buf_fd[fd_idx].size = size;
  6421. return 0;
  6422. }
  6423. static int __qseecom_update_qteec_req_buf(struct qseecom_qteec_modfd_req *req,
  6424. struct qseecom_dev_handle *data, bool cleanup)
  6425. {
  6426. int ret = 0;
  6427. int i = 0;
  6428. uint32_t *update;
  6429. struct sg_table *sg_ptr = NULL;
  6430. struct scatterlist *sg;
  6431. struct qseecom_param_memref *memref;
  6432. int ion_fd = -1;
  6433. struct dma_buf *dmabuf = NULL;
  6434. struct dma_buf_attachment *attach = NULL;
  6435. if (req == NULL) {
  6436. pr_err("Invalid address\n");
  6437. return -EINVAL;
  6438. }
  6439. for (i = 0; i < MAX_ION_FD; i++) {
  6440. if (req->ifd_data[i].fd > 0) {
  6441. ion_fd = req->ifd_data[i].fd;
  6442. if ((req->req_len <
  6443. sizeof(struct qseecom_param_memref)) ||
  6444. (req->ifd_data[i].cmd_buf_offset >
  6445. req->req_len -
  6446. sizeof(struct qseecom_param_memref))) {
  6447. pr_err("Invalid offset/req len 0x%x/0x%x\n",
  6448. req->req_len,
  6449. req->ifd_data[i].cmd_buf_offset);
  6450. return -EINVAL;
  6451. }
  6452. update = (uint32_t *)((char *) req->req_ptr +
  6453. req->ifd_data[i].cmd_buf_offset);
  6454. if (!update) {
  6455. pr_err("update pointer is NULL\n");
  6456. return -EINVAL;
  6457. }
  6458. } else {
  6459. continue;
  6460. }
  6461. /* Populate the cmd data structure with the phys_addr */
  6462. ret = qseecom_dmabuf_map(ion_fd, &sg_ptr, &attach, &dmabuf);
  6463. if (ret) {
  6464. pr_err("IOn client could not retrieve sg table\n");
  6465. goto err;
  6466. }
  6467. sg = sg_ptr->sgl;
  6468. if (sg == NULL) {
  6469. pr_err("sg is NULL\n");
  6470. goto err;
  6471. }
  6472. if ((sg_ptr->nents == 0) || (sg->length == 0)) {
  6473. pr_err("Num of scat entr (%d)or length(%d) invalid\n",
  6474. sg_ptr->nents, sg->length);
  6475. goto err;
  6476. }
  6477. /* clean up buf for pre-allocated fd */
  6478. if (cleanup && data->client.sec_buf_fd[i].is_sec_buf_fd &&
  6479. (*update)) {
  6480. if (data->client.sec_buf_fd[i].vbase)
  6481. dma_free_coherent(qseecom.dev,
  6482. data->client.sec_buf_fd[i].size,
  6483. data->client.sec_buf_fd[i].vbase,
  6484. data->client.sec_buf_fd[i].pbase);
  6485. memset((void *)update, 0,
  6486. sizeof(struct qseecom_param_memref));
  6487. memset(&(data->client.sec_buf_fd[i]), 0,
  6488. sizeof(struct qseecom_sec_buf_fd_info));
  6489. goto clean;
  6490. }
  6491. if (*update == 0) {
  6492. /* update buf for pre-allocated fd from secure heap*/
  6493. ret = __qseecom_qteec_handle_pre_alc_fd(data, i,
  6494. sg_ptr);
  6495. if (ret) {
  6496. pr_err("Failed to handle buf for fd[%d]\n", i);
  6497. goto err;
  6498. }
  6499. memref = (struct qseecom_param_memref *)update;
  6500. memref->buffer =
  6501. (uint32_t)(data->client.sec_buf_fd[i].pbase);
  6502. memref->size =
  6503. (uint32_t)(data->client.sec_buf_fd[i].size);
  6504. } else {
  6505. /* update buf for fd from non-secure qseecom heap */
  6506. if (sg_ptr->nents != 1) {
  6507. pr_err("Num of scat entr (%d) invalid\n",
  6508. sg_ptr->nents);
  6509. goto err;
  6510. }
  6511. if (cleanup)
  6512. *update = 0;
  6513. else
  6514. *update = (uint32_t)sg_dma_address(sg_ptr->sgl);
  6515. }
  6516. clean:
  6517. if (cleanup) {
  6518. ret = qseecom_dmabuf_cache_operations(dmabuf,
  6519. QSEECOM_CACHE_INVALIDATE);
  6520. if (ret) {
  6521. pr_err("cache operation failed %d\n", ret);
  6522. goto err;
  6523. }
  6524. } else {
  6525. ret = qseecom_dmabuf_cache_operations(dmabuf,
  6526. QSEECOM_CACHE_CLEAN);
  6527. if (ret) {
  6528. pr_err("cache operation failed %d\n", ret);
  6529. goto err;
  6530. }
  6531. data->sglistinfo_ptr[i].indexAndFlags =
  6532. SGLISTINFO_SET_INDEX_FLAG(
  6533. (sg_ptr->nents == 1), 0,
  6534. req->ifd_data[i].cmd_buf_offset);
  6535. data->sglistinfo_ptr[i].sizeOrCount =
  6536. (sg_ptr->nents == 1) ?
  6537. sg->length : sg_ptr->nents;
  6538. data->sglist_cnt = i + 1;
  6539. }
  6540. /* unmap the dmabuf */
  6541. qseecom_dmabuf_unmap(sg_ptr, attach, dmabuf);
  6542. sg_ptr = NULL;
  6543. dmabuf = NULL;
  6544. attach = NULL;
  6545. }
  6546. return ret;
  6547. err:
  6548. if (!IS_ERR_OR_NULL(sg_ptr)) {
  6549. qseecom_dmabuf_unmap(sg_ptr, attach, dmabuf);
  6550. MAKE_NULL(sg_ptr, attach, dmabuf);
  6551. }
  6552. return -ENOMEM;
  6553. }
  6554. static int __qseecom_qteec_issue_cmd(struct qseecom_dev_handle *data,
  6555. struct qseecom_qteec_req *req, uint32_t cmd_id)
  6556. {
  6557. struct qseecom_command_scm_resp resp;
  6558. struct qseecom_qteec_ireq ireq;
  6559. struct qseecom_qteec_64bit_ireq ireq_64bit;
  6560. struct qseecom_registered_app_list *ptr_app;
  6561. bool found_app = false;
  6562. unsigned long flags;
  6563. int ret = 0;
  6564. int ret2 = 0;
  6565. uint32_t reqd_len_sb_in = 0;
  6566. void *cmd_buf = NULL;
  6567. size_t cmd_len;
  6568. struct sglist_info *table = data->sglistinfo_ptr;
  6569. void *req_ptr = NULL;
  6570. void *resp_ptr = NULL;
  6571. ret = __qseecom_qteec_validate_msg(data, req);
  6572. if (ret)
  6573. return ret;
  6574. req_ptr = req->req_ptr;
  6575. resp_ptr = req->resp_ptr;
  6576. /* find app_id & img_name from list */
  6577. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
  6578. list_for_each_entry(ptr_app, &qseecom.registered_app_list_head,
  6579. list) {
  6580. if ((ptr_app->app_id == data->client.app_id) &&
  6581. (!strcmp(ptr_app->app_name, data->client.app_name))) {
  6582. found_app = true;
  6583. break;
  6584. }
  6585. }
  6586. spin_unlock_irqrestore(&qseecom.registered_app_list_lock, flags);
  6587. if (!found_app) {
  6588. pr_err("app_id %d (%s) is not found\n", data->client.app_id,
  6589. (char *)data->client.app_name);
  6590. return -ENOENT;
  6591. }
  6592. if (__qseecom_find_pending_unload_app(data->client.app_id,
  6593. data->client.app_name)) {
  6594. pr_err("app %d (%s) unload is pending\n",
  6595. data->client.app_id, data->client.app_name);
  6596. return -ENOENT;
  6597. }
  6598. req->req_ptr = (void *)__qseecom_uvirt_to_kvirt(data,
  6599. (uintptr_t)req->req_ptr);
  6600. req->resp_ptr = (void *)__qseecom_uvirt_to_kvirt(data,
  6601. (uintptr_t)req->resp_ptr);
  6602. if ((cmd_id == QSEOS_TEE_OPEN_SESSION) ||
  6603. (cmd_id == QSEOS_TEE_REQUEST_CANCELLATION)) {
  6604. ret = __qseecom_update_qteec_req_buf(
  6605. (struct qseecom_qteec_modfd_req *)req, data, false);
  6606. if (ret)
  6607. return ret;
  6608. }
  6609. if (qseecom.qsee_version < QSEE_VERSION_40) {
  6610. ireq.app_id = data->client.app_id;
  6611. ireq.req_ptr = (uint32_t)__qseecom_uvirt_to_kphys(data,
  6612. (uintptr_t)req_ptr);
  6613. ireq.req_len = req->req_len;
  6614. ireq.resp_ptr = (uint32_t)__qseecom_uvirt_to_kphys(data,
  6615. (uintptr_t)resp_ptr);
  6616. ireq.resp_len = req->resp_len;
  6617. ireq.sglistinfo_ptr = (uint32_t)virt_to_phys(table);
  6618. ireq.sglistinfo_len = SGLISTINFO_TABLE_SIZE;
  6619. qtee_shmbridge_flush_shm_buf(&data->sglistinfo_shm);
  6620. cmd_buf = (void *)&ireq;
  6621. cmd_len = sizeof(struct qseecom_qteec_ireq);
  6622. } else {
  6623. ireq_64bit.app_id = data->client.app_id;
  6624. ireq_64bit.req_ptr = (uint64_t)__qseecom_uvirt_to_kphys(data,
  6625. (uintptr_t)req_ptr);
  6626. ireq_64bit.req_len = req->req_len;
  6627. ireq_64bit.resp_ptr = (uint64_t)__qseecom_uvirt_to_kphys(data,
  6628. (uintptr_t)resp_ptr);
  6629. ireq_64bit.resp_len = req->resp_len;
  6630. if ((data->client.app_arch == ELFCLASS32) &&
  6631. ((ireq_64bit.req_ptr >=
  6632. PHY_ADDR_4G - ireq_64bit.req_len) ||
  6633. (ireq_64bit.resp_ptr >=
  6634. PHY_ADDR_4G - ireq_64bit.resp_len))){
  6635. pr_err("32bit app %s (id: %d): phy_addr exceeds 4G\n",
  6636. data->client.app_name, data->client.app_id);
  6637. pr_err("req_ptr:%llx,req_len:%x,rsp_ptr:%llx,rsp_len:%x\n",
  6638. ireq_64bit.req_ptr, ireq_64bit.req_len,
  6639. ireq_64bit.resp_ptr, ireq_64bit.resp_len);
  6640. return -EFAULT;
  6641. }
  6642. ireq_64bit.sglistinfo_ptr = (uint64_t)virt_to_phys(table);
  6643. ireq_64bit.sglistinfo_len = SGLISTINFO_TABLE_SIZE;
  6644. qtee_shmbridge_flush_shm_buf(&data->sglistinfo_shm);
  6645. cmd_buf = (void *)&ireq_64bit;
  6646. cmd_len = sizeof(struct qseecom_qteec_64bit_ireq);
  6647. }
  6648. if (qseecom.whitelist_support
  6649. && cmd_id == QSEOS_TEE_OPEN_SESSION)
  6650. *(uint32_t *)cmd_buf = QSEOS_TEE_OPEN_SESSION_WHITELIST;
  6651. else
  6652. *(uint32_t *)cmd_buf = cmd_id;
  6653. reqd_len_sb_in = req->req_len + req->resp_len;
  6654. ret = qseecom_dmabuf_cache_operations(data->client.dmabuf,
  6655. QSEECOM_CACHE_CLEAN);
  6656. if (ret) {
  6657. pr_err("cache operation failed %d\n", ret);
  6658. return ret;
  6659. }
  6660. __qseecom_reentrancy_check_if_this_app_blocked(ptr_app);
  6661. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1,
  6662. cmd_buf, cmd_len,
  6663. &resp, sizeof(resp));
  6664. if (ret) {
  6665. pr_err("scm_call() failed with err: %d (app_id = %d)\n",
  6666. ret, data->client.app_id);
  6667. goto exit;
  6668. }
  6669. ret = qseecom_dmabuf_cache_operations(data->client.dmabuf,
  6670. QSEECOM_CACHE_INVALIDATE);
  6671. if (ret) {
  6672. pr_err("cache operation failed %d\n", ret);
  6673. return ret;
  6674. }
  6675. if (qseecom.qsee_reentrancy_support) {
  6676. ret = __qseecom_process_reentrancy(&resp, ptr_app, data);
  6677. if (ret)
  6678. goto exit;
  6679. } else {
  6680. if (resp.result == QSEOS_RESULT_INCOMPLETE) {
  6681. ret = __qseecom_process_incomplete_cmd(data, &resp);
  6682. if (ret) {
  6683. pr_err("process_incomplete_cmd failed err: %d\n",
  6684. ret);
  6685. goto exit;
  6686. }
  6687. } else {
  6688. if (resp.result != QSEOS_RESULT_SUCCESS) {
  6689. pr_err("Response result %d not supported\n",
  6690. resp.result);
  6691. ret = -EINVAL;
  6692. goto exit;
  6693. }
  6694. }
  6695. }
  6696. exit:
  6697. if ((cmd_id == QSEOS_TEE_OPEN_SESSION) ||
  6698. (cmd_id == QSEOS_TEE_REQUEST_CANCELLATION)) {
  6699. ret2 = __qseecom_update_qteec_req_buf(
  6700. (struct qseecom_qteec_modfd_req *)req, data, true);
  6701. if (ret2)
  6702. return ret2;
  6703. }
  6704. return ret;
  6705. }
  6706. static int qseecom_qteec_open_session(struct qseecom_dev_handle *data,
  6707. void __user *argp)
  6708. {
  6709. struct qseecom_qteec_modfd_req req;
  6710. int ret = 0;
  6711. ret = copy_from_user(&req, argp,
  6712. sizeof(struct qseecom_qteec_modfd_req));
  6713. if (ret) {
  6714. pr_err("copy_from_user failed\n");
  6715. return ret;
  6716. }
  6717. ret = __qseecom_qteec_issue_cmd(data, (struct qseecom_qteec_req *)&req,
  6718. QSEOS_TEE_OPEN_SESSION);
  6719. return ret;
  6720. }
  6721. static int qseecom_qteec_close_session(struct qseecom_dev_handle *data,
  6722. void __user *argp)
  6723. {
  6724. struct qseecom_qteec_req req;
  6725. int ret = 0;
  6726. ret = copy_from_user(&req, argp, sizeof(struct qseecom_qteec_req));
  6727. if (ret) {
  6728. pr_err("copy_from_user failed\n");
  6729. return ret;
  6730. }
  6731. ret = __qseecom_qteec_issue_cmd(data, &req, QSEOS_TEE_CLOSE_SESSION);
  6732. return ret;
  6733. }
  6734. static int qseecom_qteec_invoke_modfd_cmd(struct qseecom_dev_handle *data,
  6735. void __user *argp)
  6736. {
  6737. struct qseecom_qteec_modfd_req req;
  6738. struct qseecom_command_scm_resp resp;
  6739. struct qseecom_qteec_ireq ireq;
  6740. struct qseecom_qteec_64bit_ireq ireq_64bit;
  6741. struct qseecom_registered_app_list *ptr_app;
  6742. bool found_app = false;
  6743. unsigned long flags;
  6744. int ret = 0;
  6745. int i = 0;
  6746. uint32_t reqd_len_sb_in = 0;
  6747. void *cmd_buf = NULL;
  6748. size_t cmd_len;
  6749. struct sglist_info *table = data->sglistinfo_ptr;
  6750. void *req_ptr = NULL;
  6751. void *resp_ptr = NULL;
  6752. ret = copy_from_user(&req, argp,
  6753. sizeof(struct qseecom_qteec_modfd_req));
  6754. if (ret) {
  6755. pr_err("copy_from_user failed\n");
  6756. return ret;
  6757. }
  6758. ret = __qseecom_qteec_validate_msg(data,
  6759. (struct qseecom_qteec_req *)(&req));
  6760. if (ret)
  6761. return ret;
  6762. req_ptr = req.req_ptr;
  6763. resp_ptr = req.resp_ptr;
  6764. /* find app_id & img_name from list */
  6765. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
  6766. list_for_each_entry(ptr_app, &qseecom.registered_app_list_head,
  6767. list) {
  6768. if ((ptr_app->app_id == data->client.app_id) &&
  6769. (!strcmp(ptr_app->app_name, data->client.app_name))) {
  6770. found_app = true;
  6771. break;
  6772. }
  6773. }
  6774. spin_unlock_irqrestore(&qseecom.registered_app_list_lock, flags);
  6775. if (!found_app) {
  6776. pr_err("app_id %d (%s) is not found\n", data->client.app_id,
  6777. (char *)data->client.app_name);
  6778. return -ENOENT;
  6779. }
  6780. if (__qseecom_find_pending_unload_app(data->client.app_id,
  6781. data->client.app_name)) {
  6782. pr_err("app %d (%s) unload is pending\n",
  6783. data->client.app_id, data->client.app_name);
  6784. return -ENOENT;
  6785. }
  6786. /* validate offsets */
  6787. for (i = 0; i < MAX_ION_FD; i++) {
  6788. if (req.ifd_data[i].fd) {
  6789. if (req.ifd_data[i].cmd_buf_offset >= req.req_len)
  6790. return -EINVAL;
  6791. }
  6792. }
  6793. req.req_ptr = (void *)__qseecom_uvirt_to_kvirt(data,
  6794. (uintptr_t)req.req_ptr);
  6795. req.resp_ptr = (void *)__qseecom_uvirt_to_kvirt(data,
  6796. (uintptr_t)req.resp_ptr);
  6797. ret = __qseecom_update_qteec_req_buf(&req, data, false);
  6798. if (ret)
  6799. return ret;
  6800. if (qseecom.qsee_version < QSEE_VERSION_40) {
  6801. ireq.app_id = data->client.app_id;
  6802. ireq.req_ptr = (uint32_t)__qseecom_uvirt_to_kphys(data,
  6803. (uintptr_t)req_ptr);
  6804. ireq.req_len = req.req_len;
  6805. ireq.resp_ptr = (uint32_t)__qseecom_uvirt_to_kphys(data,
  6806. (uintptr_t)resp_ptr);
  6807. ireq.resp_len = req.resp_len;
  6808. cmd_buf = (void *)&ireq;
  6809. cmd_len = sizeof(struct qseecom_qteec_ireq);
  6810. ireq.sglistinfo_ptr = (uint32_t)virt_to_phys(table);
  6811. ireq.sglistinfo_len = SGLISTINFO_TABLE_SIZE;
  6812. qtee_shmbridge_flush_shm_buf(&data->sglistinfo_shm);
  6813. } else {
  6814. ireq_64bit.app_id = data->client.app_id;
  6815. ireq_64bit.req_ptr = (uint64_t)__qseecom_uvirt_to_kphys(data,
  6816. (uintptr_t)req_ptr);
  6817. ireq_64bit.req_len = req.req_len;
  6818. ireq_64bit.resp_ptr = (uint64_t)__qseecom_uvirt_to_kphys(data,
  6819. (uintptr_t)resp_ptr);
  6820. ireq_64bit.resp_len = req.resp_len;
  6821. cmd_buf = (void *)&ireq_64bit;
  6822. cmd_len = sizeof(struct qseecom_qteec_64bit_ireq);
  6823. ireq_64bit.sglistinfo_ptr = (uint64_t)virt_to_phys(table);
  6824. ireq_64bit.sglistinfo_len = SGLISTINFO_TABLE_SIZE;
  6825. qtee_shmbridge_flush_shm_buf(&data->sglistinfo_shm);
  6826. }
  6827. reqd_len_sb_in = req.req_len + req.resp_len;
  6828. if (qseecom.whitelist_support)
  6829. *(uint32_t *)cmd_buf = QSEOS_TEE_INVOKE_COMMAND_WHITELIST;
  6830. else
  6831. *(uint32_t *)cmd_buf = QSEOS_TEE_INVOKE_COMMAND;
  6832. ret = qseecom_dmabuf_cache_operations(data->client.dmabuf,
  6833. QSEECOM_CACHE_CLEAN);
  6834. if (ret) {
  6835. pr_err("cache operation failed %d\n", ret);
  6836. return ret;
  6837. }
  6838. __qseecom_reentrancy_check_if_this_app_blocked(ptr_app);
  6839. ret = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1,
  6840. cmd_buf, cmd_len,
  6841. &resp, sizeof(resp));
  6842. if (ret) {
  6843. pr_err("scm_call() failed with err: %d (app_id = %d)\n",
  6844. ret, data->client.app_id);
  6845. return ret;
  6846. }
  6847. ret = qseecom_dmabuf_cache_operations(data->client.dmabuf,
  6848. QSEECOM_CACHE_INVALIDATE);
  6849. if (ret) {
  6850. pr_err("cache operation failed %d\n", ret);
  6851. return ret;
  6852. }
  6853. if (qseecom.qsee_reentrancy_support) {
  6854. ret = __qseecom_process_reentrancy(&resp, ptr_app, data);
  6855. } else {
  6856. if (resp.result == QSEOS_RESULT_INCOMPLETE) {
  6857. ret = __qseecom_process_incomplete_cmd(data, &resp);
  6858. if (ret) {
  6859. pr_err("process_incomplete_cmd failed err: %d\n",
  6860. ret);
  6861. return ret;
  6862. }
  6863. } else {
  6864. if (resp.result != QSEOS_RESULT_SUCCESS) {
  6865. pr_err("Response result %d not supported\n",
  6866. resp.result);
  6867. ret = -EINVAL;
  6868. }
  6869. }
  6870. }
  6871. ret = __qseecom_update_qteec_req_buf(&req, data, true);
  6872. if (ret)
  6873. return ret;
  6874. return 0;
  6875. }
  6876. static int qseecom_qteec_request_cancellation(struct qseecom_dev_handle *data,
  6877. void __user *argp)
  6878. {
  6879. struct qseecom_qteec_modfd_req req;
  6880. int ret = 0;
  6881. ret = copy_from_user(&req, argp,
  6882. sizeof(struct qseecom_qteec_modfd_req));
  6883. if (ret) {
  6884. pr_err("copy_from_user failed\n");
  6885. return ret;
  6886. }
  6887. ret = __qseecom_qteec_issue_cmd(data, (struct qseecom_qteec_req *)&req,
  6888. QSEOS_TEE_REQUEST_CANCELLATION);
  6889. return ret;
  6890. }
  6891. static void __qseecom_clean_data_sglistinfo(struct qseecom_dev_handle *data)
  6892. {
  6893. if (data->sglist_cnt) {
  6894. memset(data->sglistinfo_ptr, 0,
  6895. SGLISTINFO_TABLE_SIZE);
  6896. data->sglist_cnt = 0;
  6897. }
  6898. }
  6899. long qseecom_ioctl(struct file *file,
  6900. unsigned int cmd, unsigned long arg)
  6901. {
  6902. int ret = 0;
  6903. struct qseecom_dev_handle *data = file->private_data;
  6904. void __user *argp = (void __user *) arg;
  6905. bool perf_enabled = false;
  6906. if (!data) {
  6907. pr_err("Invalid/uninitialized device handle\n");
  6908. return -EINVAL;
  6909. }
  6910. if (data->abort) {
  6911. pr_err("Aborting qseecom driver\n");
  6912. return -ENODEV;
  6913. }
  6914. if (cmd != QSEECOM_IOCTL_RECEIVE_REQ &&
  6915. cmd != QSEECOM_IOCTL_SEND_RESP_REQ &&
  6916. cmd != QSEECOM_IOCTL_SEND_MODFD_RESP &&
  6917. cmd != QSEECOM_IOCTL_SEND_MODFD_RESP_64)
  6918. __wakeup_unregister_listener_kthread();
  6919. __wakeup_unload_app_kthread();
  6920. switch (cmd) {
  6921. case QSEECOM_IOCTL_REGISTER_LISTENER_REQ: {
  6922. if (data->type != QSEECOM_GENERIC) {
  6923. pr_err("reg lstnr req: invalid handle (%d)\n",
  6924. data->type);
  6925. ret = -EINVAL;
  6926. break;
  6927. }
  6928. pr_debug("ioctl register_listener_req()\n");
  6929. mutex_lock(&listener_access_lock);
  6930. atomic_inc(&data->ioctl_count);
  6931. data->type = QSEECOM_LISTENER_SERVICE;
  6932. ret = qseecom_register_listener(data, argp);
  6933. atomic_dec(&data->ioctl_count);
  6934. wake_up_all(&data->abort_wq);
  6935. mutex_unlock(&listener_access_lock);
  6936. if (ret)
  6937. pr_err("failed qseecom_register_listener: %d\n", ret);
  6938. break;
  6939. }
  6940. case QSEECOM_IOCTL_UNREGISTER_LISTENER_REQ: {
  6941. if ((data->listener.id == 0) ||
  6942. (data->type != QSEECOM_LISTENER_SERVICE)) {
  6943. pr_err("unreg lstnr req: invalid handle (%d) lid(%d)\n",
  6944. data->type, data->listener.id);
  6945. ret = -EINVAL;
  6946. break;
  6947. }
  6948. pr_debug("ioctl unregister_listener_req()\n");
  6949. mutex_lock(&listener_access_lock);
  6950. atomic_inc(&data->ioctl_count);
  6951. ret = qseecom_unregister_listener(data);
  6952. atomic_dec(&data->ioctl_count);
  6953. wake_up_all(&data->abort_wq);
  6954. mutex_unlock(&listener_access_lock);
  6955. if (ret)
  6956. pr_err("failed qseecom_unregister_listener: %d\n", ret);
  6957. break;
  6958. }
  6959. case QSEECOM_IOCTL_SEND_CMD_REQ: {
  6960. if ((data->client.app_id == 0) ||
  6961. (data->type != QSEECOM_CLIENT_APP)) {
  6962. pr_err("send cmd req: invalid handle (%d) app_id(%d)\n",
  6963. data->type, data->client.app_id);
  6964. ret = -EINVAL;
  6965. break;
  6966. }
  6967. /* Only one client allowed here at a time */
  6968. mutex_lock(&app_access_lock);
  6969. if (qseecom.support_bus_scaling) {
  6970. /* register bus bw in case the client doesn't do it */
  6971. if (!data->mode) {
  6972. mutex_lock(&qsee_bw_mutex);
  6973. __qseecom_register_bus_bandwidth_needs(
  6974. data, HIGH);
  6975. mutex_unlock(&qsee_bw_mutex);
  6976. }
  6977. ret = qseecom_scale_bus_bandwidth_timer(INACTIVE);
  6978. if (ret) {
  6979. pr_err("Failed to set bw.\n");
  6980. ret = -EINVAL;
  6981. mutex_unlock(&app_access_lock);
  6982. break;
  6983. }
  6984. }
  6985. /*
  6986. * On targets where crypto clock is handled by HLOS,
  6987. * if clk_access_cnt is zero and perf_enabled is false,
  6988. * then the crypto clock was not enabled before sending cmd to
  6989. * tz, qseecom will enable the clock to avoid service failure.
  6990. */
  6991. if (!qseecom.no_clock_support &&
  6992. !qseecom.qsee.clk_access_cnt && !data->perf_enabled) {
  6993. pr_debug("ce clock is not enabled!\n");
  6994. ret = qseecom_perf_enable(data);
  6995. if (ret) {
  6996. pr_err("Failed to vote for clock with err %d\n",
  6997. ret);
  6998. mutex_unlock(&app_access_lock);
  6999. ret = -EINVAL;
  7000. break;
  7001. }
  7002. perf_enabled = true;
  7003. }
  7004. atomic_inc(&data->ioctl_count);
  7005. ret = qseecom_send_cmd(data, argp);
  7006. if (qseecom.support_bus_scaling)
  7007. __qseecom_add_bw_scale_down_timer(
  7008. QSEECOM_SEND_CMD_CRYPTO_TIMEOUT);
  7009. if (perf_enabled) {
  7010. qsee_disable_clock_vote(data, CLK_DFAB);
  7011. qsee_disable_clock_vote(data, CLK_SFPB);
  7012. }
  7013. atomic_dec(&data->ioctl_count);
  7014. wake_up_all(&data->abort_wq);
  7015. mutex_unlock(&app_access_lock);
  7016. if (ret)
  7017. pr_err("failed qseecom_send_cmd: %d\n", ret);
  7018. break;
  7019. }
  7020. case QSEECOM_IOCTL_SEND_MODFD_CMD_REQ:
  7021. case QSEECOM_IOCTL_SEND_MODFD_CMD_64_REQ: {
  7022. if ((data->client.app_id == 0) ||
  7023. (data->type != QSEECOM_CLIENT_APP)) {
  7024. pr_err("send mdfd cmd: invalid handle (%d) appid(%d)\n",
  7025. data->type, data->client.app_id);
  7026. ret = -EINVAL;
  7027. break;
  7028. }
  7029. /* Only one client allowed here at a time */
  7030. mutex_lock(&app_access_lock);
  7031. if (qseecom.support_bus_scaling) {
  7032. if (!data->mode) {
  7033. mutex_lock(&qsee_bw_mutex);
  7034. __qseecom_register_bus_bandwidth_needs(
  7035. data, HIGH);
  7036. mutex_unlock(&qsee_bw_mutex);
  7037. }
  7038. ret = qseecom_scale_bus_bandwidth_timer(INACTIVE);
  7039. if (ret) {
  7040. pr_err("Failed to set bw.\n");
  7041. mutex_unlock(&app_access_lock);
  7042. ret = -EINVAL;
  7043. break;
  7044. }
  7045. }
  7046. /*
  7047. * On targets where crypto clock is handled by HLOS,
  7048. * if clk_access_cnt is zero and perf_enabled is false,
  7049. * then the crypto clock was not enabled before sending cmd to
  7050. * tz, qseecom will enable the clock to avoid service failure.
  7051. */
  7052. if (!qseecom.no_clock_support &&
  7053. !qseecom.qsee.clk_access_cnt && !data->perf_enabled) {
  7054. pr_debug("ce clock is not enabled!\n");
  7055. ret = qseecom_perf_enable(data);
  7056. if (ret) {
  7057. pr_err("Failed to vote for clock with err %d\n",
  7058. ret);
  7059. mutex_unlock(&app_access_lock);
  7060. ret = -EINVAL;
  7061. break;
  7062. }
  7063. perf_enabled = true;
  7064. }
  7065. atomic_inc(&data->ioctl_count);
  7066. if (cmd == QSEECOM_IOCTL_SEND_MODFD_CMD_REQ)
  7067. ret = qseecom_send_modfd_cmd(data, argp);
  7068. else
  7069. ret = qseecom_send_modfd_cmd_64(data, argp);
  7070. if (qseecom.support_bus_scaling)
  7071. __qseecom_add_bw_scale_down_timer(
  7072. QSEECOM_SEND_CMD_CRYPTO_TIMEOUT);
  7073. if (perf_enabled) {
  7074. qsee_disable_clock_vote(data, CLK_DFAB);
  7075. qsee_disable_clock_vote(data, CLK_SFPB);
  7076. }
  7077. atomic_dec(&data->ioctl_count);
  7078. wake_up_all(&data->abort_wq);
  7079. mutex_unlock(&app_access_lock);
  7080. if (ret)
  7081. pr_err("failed qseecom_send_cmd: %d\n", ret);
  7082. __qseecom_clean_data_sglistinfo(data);
  7083. break;
  7084. }
  7085. case QSEECOM_IOCTL_RECEIVE_REQ: {
  7086. if ((data->listener.id == 0) ||
  7087. (data->type != QSEECOM_LISTENER_SERVICE)) {
  7088. pr_err("receive req: invalid handle (%d), lid(%d)\n",
  7089. data->type, data->listener.id);
  7090. ret = -EINVAL;
  7091. break;
  7092. }
  7093. atomic_inc(&data->ioctl_count);
  7094. ret = qseecom_receive_req(data);
  7095. atomic_dec(&data->ioctl_count);
  7096. wake_up_all(&data->abort_wq);
  7097. if (ret && (ret != -ERESTARTSYS))
  7098. pr_err("failed qseecom_receive_req: %d\n", ret);
  7099. break;
  7100. }
  7101. case QSEECOM_IOCTL_SEND_RESP_REQ: {
  7102. if ((data->listener.id == 0) ||
  7103. (data->type != QSEECOM_LISTENER_SERVICE)) {
  7104. pr_err("send resp req: invalid handle (%d), lid(%d)\n",
  7105. data->type, data->listener.id);
  7106. ret = -EINVAL;
  7107. break;
  7108. }
  7109. mutex_lock(&listener_access_lock);
  7110. atomic_inc(&data->ioctl_count);
  7111. if (!qseecom.qsee_reentrancy_support)
  7112. ret = qseecom_send_resp();
  7113. else
  7114. ret = qseecom_reentrancy_send_resp(data);
  7115. atomic_dec(&data->ioctl_count);
  7116. wake_up_all(&data->abort_wq);
  7117. mutex_unlock(&listener_access_lock);
  7118. if (ret)
  7119. pr_err("failed qseecom_send_resp: %d\n", ret);
  7120. break;
  7121. }
  7122. case QSEECOM_IOCTL_SET_MEM_PARAM_REQ: {
  7123. if ((data->type != QSEECOM_CLIENT_APP) &&
  7124. (data->type != QSEECOM_GENERIC) &&
  7125. (data->type != QSEECOM_SECURE_SERVICE)) {
  7126. pr_err("set mem param req: invalid handle (%d)\n",
  7127. data->type);
  7128. ret = -EINVAL;
  7129. break;
  7130. }
  7131. pr_debug("SET_MEM_PARAM: qseecom addr = 0x%pK\n", data);
  7132. mutex_lock(&app_access_lock);
  7133. atomic_inc(&data->ioctl_count);
  7134. ret = qseecom_set_client_mem_param(data, argp);
  7135. atomic_dec(&data->ioctl_count);
  7136. mutex_unlock(&app_access_lock);
  7137. if (ret)
  7138. pr_err("failed Qqseecom_set_mem_param request: %d\n",
  7139. ret);
  7140. break;
  7141. }
  7142. case QSEECOM_IOCTL_LOAD_APP_REQ: {
  7143. if ((data->type != QSEECOM_GENERIC) &&
  7144. (data->type != QSEECOM_CLIENT_APP)) {
  7145. pr_err("load app req: invalid handle (%d)\n",
  7146. data->type);
  7147. ret = -EINVAL;
  7148. break;
  7149. }
  7150. data->type = QSEECOM_CLIENT_APP;
  7151. pr_debug("LOAD_APP_REQ: qseecom_addr = 0x%pK\n", data);
  7152. mutex_lock(&app_access_lock);
  7153. atomic_inc(&data->ioctl_count);
  7154. ret = qseecom_load_app(data, argp);
  7155. atomic_dec(&data->ioctl_count);
  7156. mutex_unlock(&app_access_lock);
  7157. if (ret)
  7158. pr_err("failed load_app request: %d\n", ret);
  7159. __wakeup_unload_app_kthread();
  7160. break;
  7161. }
  7162. case QSEECOM_IOCTL_UNLOAD_APP_REQ: {
  7163. if ((data->client.app_id == 0) ||
  7164. (data->type != QSEECOM_CLIENT_APP)) {
  7165. pr_err("unload app req:invalid handle(%d) app_id(%d)\n",
  7166. data->type, data->client.app_id);
  7167. ret = -EINVAL;
  7168. break;
  7169. }
  7170. pr_debug("UNLOAD_APP: qseecom_addr = 0x%pK\n", data);
  7171. mutex_lock(&app_access_lock);
  7172. atomic_inc(&data->ioctl_count);
  7173. ret = qseecom_unload_app(data, false);
  7174. atomic_dec(&data->ioctl_count);
  7175. mutex_unlock(&app_access_lock);
  7176. if (ret)
  7177. pr_err("failed unload_app request: %d\n", ret);
  7178. __wakeup_unload_app_kthread();
  7179. break;
  7180. }
  7181. case QSEECOM_IOCTL_GET_QSEOS_VERSION_REQ: {
  7182. atomic_inc(&data->ioctl_count);
  7183. ret = qseecom_get_qseos_version(data, argp);
  7184. if (ret)
  7185. pr_err("qseecom_get_qseos_version: %d\n", ret);
  7186. atomic_dec(&data->ioctl_count);
  7187. break;
  7188. }
  7189. case QSEECOM_IOCTL_PERF_ENABLE_REQ:{
  7190. if ((data->type != QSEECOM_GENERIC) &&
  7191. (data->type != QSEECOM_CLIENT_APP)) {
  7192. pr_err("perf enable req: invalid handle (%d)\n",
  7193. data->type);
  7194. ret = -EINVAL;
  7195. break;
  7196. }
  7197. if ((data->type == QSEECOM_CLIENT_APP) &&
  7198. (data->client.app_id == 0)) {
  7199. pr_err("perf enable req:invalid handle(%d) appid(%d)\n",
  7200. data->type, data->client.app_id);
  7201. ret = -EINVAL;
  7202. break;
  7203. }
  7204. atomic_inc(&data->ioctl_count);
  7205. if (qseecom.support_bus_scaling) {
  7206. mutex_lock(&qsee_bw_mutex);
  7207. __qseecom_register_bus_bandwidth_needs(data, HIGH);
  7208. mutex_unlock(&qsee_bw_mutex);
  7209. } else {
  7210. ret = qseecom_perf_enable(data);
  7211. if (ret)
  7212. pr_err("Fail to vote for clocks %d\n", ret);
  7213. }
  7214. atomic_dec(&data->ioctl_count);
  7215. break;
  7216. }
  7217. case QSEECOM_IOCTL_PERF_DISABLE_REQ:{
  7218. if ((data->type != QSEECOM_SECURE_SERVICE) &&
  7219. (data->type != QSEECOM_CLIENT_APP)) {
  7220. pr_err("perf disable req: invalid handle (%d)\n",
  7221. data->type);
  7222. ret = -EINVAL;
  7223. break;
  7224. }
  7225. if ((data->type == QSEECOM_CLIENT_APP) &&
  7226. (data->client.app_id == 0)) {
  7227. pr_err("perf disable: invalid handle (%d)app_id(%d)\n",
  7228. data->type, data->client.app_id);
  7229. ret = -EINVAL;
  7230. break;
  7231. }
  7232. atomic_inc(&data->ioctl_count);
  7233. if (!qseecom.support_bus_scaling) {
  7234. qsee_disable_clock_vote(data, CLK_DFAB);
  7235. qsee_disable_clock_vote(data, CLK_SFPB);
  7236. } else {
  7237. mutex_lock(&qsee_bw_mutex);
  7238. qseecom_unregister_bus_bandwidth_needs(data);
  7239. mutex_unlock(&qsee_bw_mutex);
  7240. }
  7241. atomic_dec(&data->ioctl_count);
  7242. break;
  7243. }
  7244. case QSEECOM_IOCTL_SET_BUS_SCALING_REQ: {
  7245. /* If crypto clock is not handled by HLOS, return directly. */
  7246. if (qseecom.no_clock_support) {
  7247. pr_debug("crypto clock is not handled by HLOS\n");
  7248. break;
  7249. }
  7250. if ((data->client.app_id == 0) ||
  7251. (data->type != QSEECOM_CLIENT_APP)) {
  7252. pr_err("set bus scale: invalid handle (%d) appid(%d)\n",
  7253. data->type, data->client.app_id);
  7254. ret = -EINVAL;
  7255. break;
  7256. }
  7257. atomic_inc(&data->ioctl_count);
  7258. ret = qseecom_scale_bus_bandwidth(data, argp);
  7259. atomic_dec(&data->ioctl_count);
  7260. break;
  7261. }
  7262. case QSEECOM_IOCTL_LOAD_EXTERNAL_ELF_REQ: {
  7263. if (data->type != QSEECOM_GENERIC) {
  7264. pr_err("load ext elf req: invalid client handle (%d)\n",
  7265. data->type);
  7266. ret = -EINVAL;
  7267. break;
  7268. }
  7269. data->type = QSEECOM_UNAVAILABLE_CLIENT_APP;
  7270. data->released = true;
  7271. mutex_lock(&app_access_lock);
  7272. atomic_inc(&data->ioctl_count);
  7273. ret = qseecom_load_external_elf(data, argp);
  7274. atomic_dec(&data->ioctl_count);
  7275. mutex_unlock(&app_access_lock);
  7276. if (ret)
  7277. pr_err("failed load_external_elf request: %d\n", ret);
  7278. break;
  7279. }
  7280. case QSEECOM_IOCTL_UNLOAD_EXTERNAL_ELF_REQ: {
  7281. if (data->type != QSEECOM_UNAVAILABLE_CLIENT_APP) {
  7282. pr_err("unload ext elf req: invalid handle (%d)\n",
  7283. data->type);
  7284. ret = -EINVAL;
  7285. break;
  7286. }
  7287. data->released = true;
  7288. mutex_lock(&app_access_lock);
  7289. atomic_inc(&data->ioctl_count);
  7290. ret = qseecom_unload_external_elf(data);
  7291. atomic_dec(&data->ioctl_count);
  7292. mutex_unlock(&app_access_lock);
  7293. if (ret)
  7294. pr_err("failed unload_app request: %d\n", ret);
  7295. break;
  7296. }
  7297. case QSEECOM_IOCTL_APP_LOADED_QUERY_REQ: {
  7298. if ((data->type != QSEECOM_GENERIC) &&
  7299. (data->type != QSEECOM_CLIENT_APP)) {
  7300. pr_err("app loaded query req: invalid handle (%d)\n",
  7301. data->type);
  7302. ret = -EINVAL;
  7303. break;
  7304. }
  7305. data->type = QSEECOM_CLIENT_APP;
  7306. mutex_lock(&app_access_lock);
  7307. atomic_inc(&data->ioctl_count);
  7308. pr_debug("APP_LOAD_QUERY: qseecom_addr = 0x%pK\n", data);
  7309. ret = qseecom_query_app_loaded(data, argp);
  7310. atomic_dec(&data->ioctl_count);
  7311. mutex_unlock(&app_access_lock);
  7312. break;
  7313. }
  7314. case QSEECOM_IOCTL_SEND_CMD_SERVICE_REQ: {
  7315. if (data->type != QSEECOM_GENERIC) {
  7316. pr_err("send cmd svc req: invalid handle (%d)\n",
  7317. data->type);
  7318. ret = -EINVAL;
  7319. break;
  7320. }
  7321. data->type = QSEECOM_SECURE_SERVICE;
  7322. if (qseecom.qsee_version < QSEE_VERSION_03) {
  7323. pr_err("SEND_CMD_SERVICE_REQ: Invalid qsee ver %u\n",
  7324. qseecom.qsee_version);
  7325. return -EINVAL;
  7326. }
  7327. mutex_lock(&app_access_lock);
  7328. atomic_inc(&data->ioctl_count);
  7329. ret = qseecom_send_service_cmd(data, argp);
  7330. atomic_dec(&data->ioctl_count);
  7331. mutex_unlock(&app_access_lock);
  7332. break;
  7333. }
  7334. case QSEECOM_IOCTL_CREATE_KEY_REQ: {
  7335. if (!(qseecom.support_pfe || qseecom.support_fde))
  7336. pr_err("Features requiring key init not supported\n");
  7337. if (data->type != QSEECOM_GENERIC) {
  7338. pr_err("create key req: invalid handle (%d)\n",
  7339. data->type);
  7340. ret = -EINVAL;
  7341. break;
  7342. }
  7343. if (qseecom.qsee_version < QSEE_VERSION_05) {
  7344. pr_err("Create Key feature unsupported: qsee ver %u\n",
  7345. qseecom.qsee_version);
  7346. return -EINVAL;
  7347. }
  7348. data->released = true;
  7349. mutex_lock(&app_access_lock);
  7350. atomic_inc(&data->ioctl_count);
  7351. ret = qseecom_create_key(data, argp);
  7352. if (ret)
  7353. pr_err("failed to create encryption key: %d\n", ret);
  7354. atomic_dec(&data->ioctl_count);
  7355. mutex_unlock(&app_access_lock);
  7356. break;
  7357. }
  7358. case QSEECOM_IOCTL_WIPE_KEY_REQ: {
  7359. if (!(qseecom.support_pfe || qseecom.support_fde))
  7360. pr_err("Features requiring key init not supported\n");
  7361. if (data->type != QSEECOM_GENERIC) {
  7362. pr_err("wipe key req: invalid handle (%d)\n",
  7363. data->type);
  7364. ret = -EINVAL;
  7365. break;
  7366. }
  7367. if (qseecom.qsee_version < QSEE_VERSION_05) {
  7368. pr_err("Wipe Key feature unsupported in qsee ver %u\n",
  7369. qseecom.qsee_version);
  7370. return -EINVAL;
  7371. }
  7372. data->released = true;
  7373. mutex_lock(&app_access_lock);
  7374. atomic_inc(&data->ioctl_count);
  7375. ret = qseecom_wipe_key(data, argp);
  7376. if (ret)
  7377. pr_err("failed to wipe encryption key: %d\n", ret);
  7378. atomic_dec(&data->ioctl_count);
  7379. mutex_unlock(&app_access_lock);
  7380. break;
  7381. }
  7382. case QSEECOM_IOCTL_UPDATE_KEY_USER_INFO_REQ: {
  7383. if (!(qseecom.support_pfe || qseecom.support_fde))
  7384. pr_err("Features requiring key init not supported\n");
  7385. if (data->type != QSEECOM_GENERIC) {
  7386. pr_err("update key req: invalid handle (%d)\n",
  7387. data->type);
  7388. ret = -EINVAL;
  7389. break;
  7390. }
  7391. if (qseecom.qsee_version < QSEE_VERSION_05) {
  7392. pr_err("Update Key feature unsupported in qsee ver %u\n",
  7393. qseecom.qsee_version);
  7394. return -EINVAL;
  7395. }
  7396. data->released = true;
  7397. mutex_lock(&app_access_lock);
  7398. atomic_inc(&data->ioctl_count);
  7399. ret = qseecom_update_key_user_info(data, argp);
  7400. if (ret)
  7401. pr_err("failed to update key user info: %d\n", ret);
  7402. atomic_dec(&data->ioctl_count);
  7403. mutex_unlock(&app_access_lock);
  7404. break;
  7405. }
  7406. case QSEECOM_IOCTL_SAVE_PARTITION_HASH_REQ: {
  7407. if (data->type != QSEECOM_GENERIC) {
  7408. pr_err("save part hash req: invalid handle (%d)\n",
  7409. data->type);
  7410. ret = -EINVAL;
  7411. break;
  7412. }
  7413. data->released = true;
  7414. mutex_lock(&app_access_lock);
  7415. atomic_inc(&data->ioctl_count);
  7416. ret = qseecom_save_partition_hash(argp);
  7417. atomic_dec(&data->ioctl_count);
  7418. mutex_unlock(&app_access_lock);
  7419. break;
  7420. }
  7421. case QSEECOM_IOCTL_IS_ES_ACTIVATED_REQ: {
  7422. if (data->type != QSEECOM_GENERIC) {
  7423. pr_err("ES activated req: invalid handle (%d)\n",
  7424. data->type);
  7425. ret = -EINVAL;
  7426. break;
  7427. }
  7428. data->released = true;
  7429. mutex_lock(&app_access_lock);
  7430. atomic_inc(&data->ioctl_count);
  7431. ret = qseecom_is_es_activated(argp);
  7432. atomic_dec(&data->ioctl_count);
  7433. mutex_unlock(&app_access_lock);
  7434. break;
  7435. }
  7436. case QSEECOM_IOCTL_MDTP_CIPHER_DIP_REQ: {
  7437. if (data->type != QSEECOM_GENERIC) {
  7438. pr_err("MDTP cipher DIP req: invalid handle (%d)\n",
  7439. data->type);
  7440. ret = -EINVAL;
  7441. break;
  7442. }
  7443. data->released = true;
  7444. mutex_lock(&app_access_lock);
  7445. atomic_inc(&data->ioctl_count);
  7446. ret = qseecom_mdtp_cipher_dip(argp);
  7447. atomic_dec(&data->ioctl_count);
  7448. mutex_unlock(&app_access_lock);
  7449. break;
  7450. }
  7451. case QSEECOM_IOCTL_SEND_MODFD_RESP:
  7452. case QSEECOM_IOCTL_SEND_MODFD_RESP_64: {
  7453. if ((data->listener.id == 0) ||
  7454. (data->type != QSEECOM_LISTENER_SERVICE)) {
  7455. pr_err("receive req: invalid handle (%d), lid(%d)\n",
  7456. data->type, data->listener.id);
  7457. ret = -EINVAL;
  7458. break;
  7459. }
  7460. mutex_lock(&listener_access_lock);
  7461. atomic_inc(&data->ioctl_count);
  7462. if (cmd == QSEECOM_IOCTL_SEND_MODFD_RESP)
  7463. ret = qseecom_send_modfd_resp(data, argp);
  7464. else
  7465. ret = qseecom_send_modfd_resp_64(data, argp);
  7466. atomic_dec(&data->ioctl_count);
  7467. wake_up_all(&data->abort_wq);
  7468. mutex_unlock(&listener_access_lock);
  7469. if (ret)
  7470. pr_err("failed qseecom_send_mod_resp: %d\n", ret);
  7471. __qseecom_clean_data_sglistinfo(data);
  7472. break;
  7473. }
  7474. case QSEECOM_QTEEC_IOCTL_OPEN_SESSION_REQ: {
  7475. if ((data->client.app_id == 0) ||
  7476. (data->type != QSEECOM_CLIENT_APP)) {
  7477. pr_err("Open session: invalid handle (%d) appid(%d)\n",
  7478. data->type, data->client.app_id);
  7479. ret = -EINVAL;
  7480. break;
  7481. }
  7482. if (qseecom.qsee_version < QSEE_VERSION_40) {
  7483. pr_err("GP feature unsupported: qsee ver %u\n",
  7484. qseecom.qsee_version);
  7485. return -EINVAL;
  7486. }
  7487. /* Only one client allowed here at a time */
  7488. mutex_lock(&app_access_lock);
  7489. atomic_inc(&data->ioctl_count);
  7490. ret = qseecom_qteec_open_session(data, argp);
  7491. atomic_dec(&data->ioctl_count);
  7492. wake_up_all(&data->abort_wq);
  7493. mutex_unlock(&app_access_lock);
  7494. if (ret)
  7495. pr_err("failed open_session_cmd: %d\n", ret);
  7496. __qseecom_clean_data_sglistinfo(data);
  7497. break;
  7498. }
  7499. case QSEECOM_QTEEC_IOCTL_CLOSE_SESSION_REQ: {
  7500. if ((data->client.app_id == 0) ||
  7501. (data->type != QSEECOM_CLIENT_APP)) {
  7502. pr_err("Close session: invalid handle (%d) appid(%d)\n",
  7503. data->type, data->client.app_id);
  7504. ret = -EINVAL;
  7505. break;
  7506. }
  7507. if (qseecom.qsee_version < QSEE_VERSION_40) {
  7508. pr_err("GP feature unsupported: qsee ver %u\n",
  7509. qseecom.qsee_version);
  7510. return -EINVAL;
  7511. }
  7512. /* Only one client allowed here at a time */
  7513. mutex_lock(&app_access_lock);
  7514. atomic_inc(&data->ioctl_count);
  7515. ret = qseecom_qteec_close_session(data, argp);
  7516. atomic_dec(&data->ioctl_count);
  7517. wake_up_all(&data->abort_wq);
  7518. mutex_unlock(&app_access_lock);
  7519. if (ret)
  7520. pr_err("failed close_session_cmd: %d\n", ret);
  7521. break;
  7522. }
  7523. case QSEECOM_QTEEC_IOCTL_INVOKE_MODFD_CMD_REQ: {
  7524. if ((data->client.app_id == 0) ||
  7525. (data->type != QSEECOM_CLIENT_APP)) {
  7526. pr_err("Invoke cmd: invalid handle (%d) appid(%d)\n",
  7527. data->type, data->client.app_id);
  7528. ret = -EINVAL;
  7529. break;
  7530. }
  7531. if (qseecom.qsee_version < QSEE_VERSION_40) {
  7532. pr_err("GP feature unsupported: qsee ver %u\n",
  7533. qseecom.qsee_version);
  7534. return -EINVAL;
  7535. }
  7536. /* Only one client allowed here at a time */
  7537. mutex_lock(&app_access_lock);
  7538. atomic_inc(&data->ioctl_count);
  7539. ret = qseecom_qteec_invoke_modfd_cmd(data, argp);
  7540. atomic_dec(&data->ioctl_count);
  7541. wake_up_all(&data->abort_wq);
  7542. mutex_unlock(&app_access_lock);
  7543. if (ret)
  7544. pr_err("failed Invoke cmd: %d\n", ret);
  7545. __qseecom_clean_data_sglistinfo(data);
  7546. break;
  7547. }
  7548. case QSEECOM_QTEEC_IOCTL_REQUEST_CANCELLATION_REQ: {
  7549. if ((data->client.app_id == 0) ||
  7550. (data->type != QSEECOM_CLIENT_APP)) {
  7551. pr_err("Cancel req: invalid handle (%d) appid(%d)\n",
  7552. data->type, data->client.app_id);
  7553. ret = -EINVAL;
  7554. break;
  7555. }
  7556. if (qseecom.qsee_version < QSEE_VERSION_40) {
  7557. pr_err("GP feature unsupported: qsee ver %u\n",
  7558. qseecom.qsee_version);
  7559. return -EINVAL;
  7560. }
  7561. /* Only one client allowed here at a time */
  7562. mutex_lock(&app_access_lock);
  7563. atomic_inc(&data->ioctl_count);
  7564. ret = qseecom_qteec_request_cancellation(data, argp);
  7565. atomic_dec(&data->ioctl_count);
  7566. wake_up_all(&data->abort_wq);
  7567. mutex_unlock(&app_access_lock);
  7568. if (ret)
  7569. pr_err("failed request_cancellation: %d\n", ret);
  7570. break;
  7571. }
  7572. case QSEECOM_IOCTL_GET_CE_PIPE_INFO: {
  7573. atomic_inc(&data->ioctl_count);
  7574. ret = qseecom_get_ce_info(data, argp);
  7575. if (ret)
  7576. pr_err("failed get fde ce pipe info: %d\n", ret);
  7577. atomic_dec(&data->ioctl_count);
  7578. break;
  7579. }
  7580. case QSEECOM_IOCTL_FREE_CE_PIPE_INFO: {
  7581. atomic_inc(&data->ioctl_count);
  7582. ret = qseecom_free_ce_info(data, argp);
  7583. if (ret)
  7584. pr_err("failed get fde ce pipe info: %d\n", ret);
  7585. atomic_dec(&data->ioctl_count);
  7586. break;
  7587. }
  7588. case QSEECOM_IOCTL_QUERY_CE_PIPE_INFO: {
  7589. atomic_inc(&data->ioctl_count);
  7590. ret = qseecom_query_ce_info(data, argp);
  7591. if (ret)
  7592. pr_err("failed get fde ce pipe info: %d\n", ret);
  7593. atomic_dec(&data->ioctl_count);
  7594. break;
  7595. }
  7596. case QSEECOM_IOCTL_SET_ICE_INFO: {
  7597. struct qseecom_ice_data_t ice_data;
  7598. ret = copy_from_user(&ice_data, argp, sizeof(ice_data));
  7599. if (ret) {
  7600. pr_err("copy_from_user failed\n");
  7601. return -EFAULT;
  7602. }
  7603. qcom_ice_set_fde_flag(ice_data.flag);
  7604. break;
  7605. }
  7606. case QSEECOM_IOCTL_FBE_CLEAR_KEY: {
  7607. pr_err("QSEECOM_IOCTL_FBE_CLEAR_KEY IOCTL is deprecated\n");
  7608. return -EINVAL;
  7609. }
  7610. default:
  7611. pr_err("Invalid IOCTL: 0x%x\n", cmd);
  7612. return -ENOIOCTLCMD;
  7613. }
  7614. return ret;
  7615. }
  7616. static int qseecom_open(struct inode *inode, struct file *file)
  7617. {
  7618. int ret = 0;
  7619. struct qseecom_dev_handle *data;
  7620. data = kzalloc(sizeof(*data), GFP_KERNEL);
  7621. if (!data)
  7622. {
  7623. return -ENOMEM;
  7624. }
  7625. file->private_data = data;
  7626. data->abort = 0;
  7627. data->type = QSEECOM_GENERIC;
  7628. data->released = false;
  7629. memset((void *)data->client.app_name, 0, MAX_APP_NAME_SIZE);
  7630. data->mode = INACTIVE;
  7631. init_waitqueue_head(&data->abort_wq);
  7632. atomic_set(&data->ioctl_count, 0);
  7633. data->sglistinfo_ptr = (struct sglist_info *)__qseecom_alloc_tzbuf(
  7634. sizeof(struct sglist_info) * MAX_ION_FD,
  7635. &data->sglistinfo_shm.paddr,
  7636. &data->sglistinfo_shm);
  7637. if (!data->sglistinfo_ptr)
  7638. {
  7639. return -ENOMEM;
  7640. }
  7641. return ret;
  7642. }
  7643. static void __qseecom_release_disable_clk(struct qseecom_dev_handle *data)
  7644. {
  7645. if (qseecom.no_clock_support)
  7646. return;
  7647. if (qseecom.support_bus_scaling) {
  7648. mutex_lock(&qsee_bw_mutex);
  7649. if (data->mode != INACTIVE) {
  7650. qseecom_unregister_bus_bandwidth_needs(data);
  7651. if (qseecom.cumulative_mode == INACTIVE)
  7652. __qseecom_set_msm_bus_request(INACTIVE);
  7653. }
  7654. mutex_unlock(&qsee_bw_mutex);
  7655. } else {
  7656. if (data->fast_load_enabled)
  7657. qsee_disable_clock_vote(data, CLK_SFPB);
  7658. if (data->perf_enabled)
  7659. qsee_disable_clock_vote(data, CLK_DFAB);
  7660. }
  7661. }
  7662. static int qseecom_release(struct inode *inode, struct file *file)
  7663. {
  7664. struct qseecom_dev_handle *data = file->private_data;
  7665. int ret = 0;
  7666. bool free_private_data = true;
  7667. __qseecom_release_disable_clk(data);
  7668. if (!data->released) {
  7669. pr_debug("data: released=false, type=%d, mode=%d, data=0x%pK\n",
  7670. data->type, data->mode, data);
  7671. switch (data->type) {
  7672. case QSEECOM_LISTENER_SERVICE:
  7673. pr_debug("release lsnr svc %d\n", data->listener.id);
  7674. mutex_lock(&listener_access_lock);
  7675. ret = qseecom_unregister_listener(data);
  7676. if (!ret)
  7677. free_private_data = false;
  7678. data->listener.release_called = true;
  7679. mutex_unlock(&listener_access_lock);
  7680. __wakeup_unregister_listener_kthread();
  7681. break;
  7682. case QSEECOM_CLIENT_APP:
  7683. pr_debug("release app %d (%s)\n",
  7684. data->client.app_id, data->client.app_name);
  7685. if (data->client.app_id) {
  7686. free_private_data = false;
  7687. mutex_lock(&unload_app_pending_list_lock);
  7688. ret = qseecom_prepare_unload_app(data);
  7689. mutex_unlock(&unload_app_pending_list_lock);
  7690. __wakeup_unload_app_kthread();
  7691. }
  7692. break;
  7693. case QSEECOM_SECURE_SERVICE:
  7694. case QSEECOM_GENERIC:
  7695. if (data->client.dmabuf) {
  7696. qseecom_vaddr_unmap(data->client.sb_virt,
  7697. data->client.sgt, data->client.attach,
  7698. data->client.dmabuf);
  7699. MAKE_NULL(data->client.sgt, data->client.attach,
  7700. data->client.dmabuf);
  7701. }
  7702. break;
  7703. case QSEECOM_UNAVAILABLE_CLIENT_APP:
  7704. break;
  7705. default:
  7706. pr_err("Unsupported clnt_handle_type %d\n",
  7707. data->type);
  7708. break;
  7709. }
  7710. }
  7711. if (free_private_data) {
  7712. __qseecom_free_tzbuf(&data->sglistinfo_shm);
  7713. kfree(data);
  7714. }
  7715. return ret;
  7716. }
  7717. static const struct file_operations qseecom_fops = {
  7718. .owner = THIS_MODULE,
  7719. .unlocked_ioctl = qseecom_ioctl,
  7720. .open = qseecom_open,
  7721. .release = qseecom_release
  7722. };
  7723. static int __qseecom_init_clk(enum qseecom_ce_hw_instance ce)
  7724. {
  7725. int rc = 0;
  7726. struct device *pdev;
  7727. struct qseecom_clk *qclk;
  7728. char *core_clk_src = NULL;
  7729. char *core_clk = NULL;
  7730. char *iface_clk = NULL;
  7731. char *bus_clk = NULL;
  7732. switch (ce) {
  7733. case CLK_QSEE: {
  7734. core_clk_src = "core_clk_src";
  7735. core_clk = "core_clk";
  7736. iface_clk = "iface_clk";
  7737. bus_clk = "bus_clk";
  7738. qclk = &qseecom.qsee;
  7739. qclk->instance = CLK_QSEE;
  7740. break;
  7741. };
  7742. case CLK_CE_DRV: {
  7743. core_clk_src = "ce_drv_core_clk_src";
  7744. core_clk = "ce_drv_core_clk";
  7745. iface_clk = "ce_drv_iface_clk";
  7746. bus_clk = "ce_drv_bus_clk";
  7747. qclk = &qseecom.ce_drv;
  7748. qclk->instance = CLK_CE_DRV;
  7749. break;
  7750. };
  7751. default:
  7752. pr_err("Invalid ce hw instance: %d!\n", ce);
  7753. return -EIO;
  7754. }
  7755. if (qseecom.no_clock_support) {
  7756. qclk->ce_core_clk = NULL;
  7757. qclk->ce_clk = NULL;
  7758. qclk->ce_bus_clk = NULL;
  7759. qclk->ce_core_src_clk = NULL;
  7760. return 0;
  7761. }
  7762. pdev = qseecom.pdev;
  7763. /* Get CE3 src core clk. */
  7764. qclk->ce_core_src_clk = clk_get(pdev, core_clk_src);
  7765. if (!IS_ERR(qclk->ce_core_src_clk)) {
  7766. rc = clk_set_rate(qclk->ce_core_src_clk,
  7767. qseecom.ce_opp_freq_hz);
  7768. if (rc) {
  7769. clk_put(qclk->ce_core_src_clk);
  7770. qclk->ce_core_src_clk = NULL;
  7771. pr_err("Unable to set the core src clk @%uMhz.\n",
  7772. qseecom.ce_opp_freq_hz/CE_CLK_DIV);
  7773. return -EIO;
  7774. }
  7775. } else {
  7776. pr_warn("Unable to get CE core src clk, set to NULL\n");
  7777. qclk->ce_core_src_clk = NULL;
  7778. }
  7779. /* Get CE core clk */
  7780. qclk->ce_core_clk = clk_get(pdev, core_clk);
  7781. if (IS_ERR(qclk->ce_core_clk)) {
  7782. rc = PTR_ERR(qclk->ce_core_clk);
  7783. pr_err("Unable to get CE core clk\n");
  7784. if (qclk->ce_core_src_clk != NULL)
  7785. clk_put(qclk->ce_core_src_clk);
  7786. return -EIO;
  7787. }
  7788. /* Get CE Interface clk */
  7789. qclk->ce_clk = clk_get(pdev, iface_clk);
  7790. if (IS_ERR(qclk->ce_clk)) {
  7791. rc = PTR_ERR(qclk->ce_clk);
  7792. pr_err("Unable to get CE interface clk\n");
  7793. if (qclk->ce_core_src_clk != NULL)
  7794. clk_put(qclk->ce_core_src_clk);
  7795. clk_put(qclk->ce_core_clk);
  7796. return -EIO;
  7797. }
  7798. /* Get CE AXI clk */
  7799. qclk->ce_bus_clk = clk_get(pdev, bus_clk);
  7800. if (IS_ERR(qclk->ce_bus_clk)) {
  7801. rc = PTR_ERR(qclk->ce_bus_clk);
  7802. pr_err("Unable to get CE BUS interface clk\n");
  7803. if (qclk->ce_core_src_clk != NULL)
  7804. clk_put(qclk->ce_core_src_clk);
  7805. clk_put(qclk->ce_core_clk);
  7806. clk_put(qclk->ce_clk);
  7807. return -EIO;
  7808. }
  7809. return rc;
  7810. }
  7811. static void __qseecom_deinit_clk(enum qseecom_ce_hw_instance ce)
  7812. {
  7813. struct qseecom_clk *qclk;
  7814. if (ce == CLK_QSEE)
  7815. qclk = &qseecom.qsee;
  7816. else
  7817. qclk = &qseecom.ce_drv;
  7818. if (qclk->ce_clk != NULL) {
  7819. clk_put(qclk->ce_clk);
  7820. qclk->ce_clk = NULL;
  7821. }
  7822. if (qclk->ce_core_clk != NULL) {
  7823. clk_put(qclk->ce_core_clk);
  7824. qclk->ce_core_clk = NULL;
  7825. }
  7826. if (qclk->ce_bus_clk != NULL) {
  7827. clk_put(qclk->ce_bus_clk);
  7828. qclk->ce_bus_clk = NULL;
  7829. }
  7830. if (qclk->ce_core_src_clk != NULL) {
  7831. clk_put(qclk->ce_core_src_clk);
  7832. qclk->ce_core_src_clk = NULL;
  7833. }
  7834. qclk->instance = CLK_INVALID;
  7835. }
  7836. static int qseecom_retrieve_ce_data(struct platform_device *pdev)
  7837. {
  7838. int rc = 0;
  7839. uint32_t hlos_num_ce_hw_instances;
  7840. uint32_t disk_encrypt_pipe;
  7841. uint32_t file_encrypt_pipe;
  7842. uint32_t hlos_ce_hw_instance[MAX_CE_PIPE_PAIR_PER_UNIT] = {0};
  7843. int i;
  7844. const int *tbl;
  7845. int size;
  7846. int entry;
  7847. struct qseecom_crypto_info *pfde_tbl = NULL;
  7848. struct qseecom_crypto_info *p;
  7849. int tbl_size;
  7850. int j;
  7851. bool old_db = true;
  7852. struct qseecom_ce_info_use *pce_info_use;
  7853. uint32_t *unit_tbl = NULL;
  7854. int total_units = 0;
  7855. struct qseecom_ce_pipe_entry *pce_entry;
  7856. qseecom.ce_info.fde = qseecom.ce_info.pfe = NULL;
  7857. qseecom.ce_info.num_fde = qseecom.ce_info.num_pfe = 0;
  7858. if (of_property_read_u32((&pdev->dev)->of_node,
  7859. "qcom,qsee-ce-hw-instance",
  7860. &qseecom.ce_info.qsee_ce_hw_instance)) {
  7861. pr_err("Fail to get qsee ce hw instance information.\n");
  7862. rc = -EINVAL;
  7863. goto out;
  7864. } else {
  7865. pr_debug("qsee-ce-hw-instance=0x%x\n",
  7866. qseecom.ce_info.qsee_ce_hw_instance);
  7867. }
  7868. qseecom.support_fde = of_property_read_bool((&pdev->dev)->of_node,
  7869. "qcom,support-fde");
  7870. qseecom.support_pfe = of_property_read_bool((&pdev->dev)->of_node,
  7871. "qcom,support-pfe");
  7872. if (!qseecom.support_pfe && !qseecom.support_fde) {
  7873. pr_warn("Device does not support PFE/FDE\n");
  7874. goto out;
  7875. }
  7876. if (qseecom.support_fde)
  7877. tbl = of_get_property((&pdev->dev)->of_node,
  7878. "qcom,full-disk-encrypt-info", &size);
  7879. else
  7880. tbl = NULL;
  7881. if (tbl) {
  7882. old_db = false;
  7883. if (size % sizeof(struct qseecom_crypto_info)) {
  7884. pr_err("full-disk-encrypt-info tbl size(%d)\n",
  7885. size);
  7886. rc = -EINVAL;
  7887. goto out;
  7888. }
  7889. tbl_size = size / sizeof
  7890. (struct qseecom_crypto_info);
  7891. pfde_tbl = kzalloc(size, GFP_KERNEL);
  7892. unit_tbl = kcalloc(tbl_size, sizeof(int), GFP_KERNEL);
  7893. total_units = 0;
  7894. if (!pfde_tbl || !unit_tbl) {
  7895. rc = -ENOMEM;
  7896. goto out;
  7897. }
  7898. if (of_property_read_u32_array((&pdev->dev)->of_node,
  7899. "qcom,full-disk-encrypt-info",
  7900. (u32 *)pfde_tbl, size/sizeof(u32))) {
  7901. pr_err("failed to read full-disk-encrypt-info tbl\n");
  7902. rc = -EINVAL;
  7903. goto out;
  7904. }
  7905. for (i = 0, p = pfde_tbl; i < tbl_size; i++, p++) {
  7906. for (j = 0; j < total_units; j++) {
  7907. if (p->unit_num == *(unit_tbl + j))
  7908. break;
  7909. }
  7910. if (j == total_units) {
  7911. *(unit_tbl + total_units) = p->unit_num;
  7912. total_units++;
  7913. }
  7914. }
  7915. qseecom.ce_info.num_fde = total_units;
  7916. pce_info_use = qseecom.ce_info.fde = kcalloc(
  7917. total_units, sizeof(struct qseecom_ce_info_use),
  7918. GFP_KERNEL);
  7919. if (!pce_info_use) {
  7920. rc = -ENOMEM;
  7921. goto out;
  7922. }
  7923. for (j = 0; j < total_units; j++, pce_info_use++) {
  7924. pce_info_use->unit_num = *(unit_tbl + j);
  7925. pce_info_use->alloc = false;
  7926. pce_info_use->type = CE_PIPE_PAIR_USE_TYPE_FDE;
  7927. pce_info_use->num_ce_pipe_entries = 0;
  7928. pce_info_use->ce_pipe_entry = NULL;
  7929. for (i = 0, p = pfde_tbl; i < tbl_size; i++, p++) {
  7930. if (p->unit_num == pce_info_use->unit_num)
  7931. pce_info_use->num_ce_pipe_entries++;
  7932. }
  7933. entry = pce_info_use->num_ce_pipe_entries;
  7934. pce_entry = pce_info_use->ce_pipe_entry =
  7935. kcalloc(entry,
  7936. sizeof(struct qseecom_ce_pipe_entry),
  7937. GFP_KERNEL);
  7938. if (pce_entry == NULL) {
  7939. rc = -ENOMEM;
  7940. goto out;
  7941. }
  7942. for (i = 0, p = pfde_tbl; i < tbl_size; i++, p++) {
  7943. if (p->unit_num == pce_info_use->unit_num) {
  7944. pce_entry->ce_num = p->ce;
  7945. pce_entry->ce_pipe_pair =
  7946. p->pipe_pair;
  7947. pce_entry->valid = true;
  7948. pce_entry++;
  7949. }
  7950. }
  7951. }
  7952. kfree(unit_tbl);
  7953. unit_tbl = NULL;
  7954. kfree(pfde_tbl);
  7955. pfde_tbl = NULL;
  7956. }
  7957. if (qseecom.support_pfe)
  7958. tbl = of_get_property((&pdev->dev)->of_node,
  7959. "qcom,per-file-encrypt-info", &size);
  7960. else
  7961. tbl = NULL;
  7962. if (tbl) {
  7963. old_db = false;
  7964. if (size % sizeof(struct qseecom_crypto_info)) {
  7965. pr_err("per-file-encrypt-info tbl size(%d)\n",
  7966. size);
  7967. rc = -EINVAL;
  7968. goto out;
  7969. }
  7970. tbl_size = size / sizeof
  7971. (struct qseecom_crypto_info);
  7972. pfde_tbl = kzalloc(size, GFP_KERNEL);
  7973. unit_tbl = kcalloc(tbl_size, sizeof(int), GFP_KERNEL);
  7974. total_units = 0;
  7975. if (!pfde_tbl || !unit_tbl) {
  7976. rc = -ENOMEM;
  7977. goto out;
  7978. }
  7979. if (of_property_read_u32_array((&pdev->dev)->of_node,
  7980. "qcom,per-file-encrypt-info",
  7981. (u32 *)pfde_tbl, size/sizeof(u32))) {
  7982. pr_err("failed to read per-file-encrypt-info tbl\n");
  7983. rc = -EINVAL;
  7984. goto out;
  7985. }
  7986. for (i = 0, p = pfde_tbl; i < tbl_size; i++, p++) {
  7987. for (j = 0; j < total_units; j++) {
  7988. if (p->unit_num == *(unit_tbl + j))
  7989. break;
  7990. }
  7991. if (j == total_units) {
  7992. *(unit_tbl + total_units) = p->unit_num;
  7993. total_units++;
  7994. }
  7995. }
  7996. qseecom.ce_info.num_pfe = total_units;
  7997. pce_info_use = qseecom.ce_info.pfe = kcalloc(
  7998. total_units, sizeof(struct qseecom_ce_info_use),
  7999. GFP_KERNEL);
  8000. if (!pce_info_use) {
  8001. rc = -ENOMEM;
  8002. goto out;
  8003. }
  8004. for (j = 0; j < total_units; j++, pce_info_use++) {
  8005. pce_info_use->unit_num = *(unit_tbl + j);
  8006. pce_info_use->alloc = false;
  8007. pce_info_use->type = CE_PIPE_PAIR_USE_TYPE_PFE;
  8008. pce_info_use->num_ce_pipe_entries = 0;
  8009. pce_info_use->ce_pipe_entry = NULL;
  8010. for (i = 0, p = pfde_tbl; i < tbl_size; i++, p++) {
  8011. if (p->unit_num == pce_info_use->unit_num)
  8012. pce_info_use->num_ce_pipe_entries++;
  8013. }
  8014. entry = pce_info_use->num_ce_pipe_entries;
  8015. pce_entry = pce_info_use->ce_pipe_entry =
  8016. kcalloc(entry,
  8017. sizeof(struct qseecom_ce_pipe_entry),
  8018. GFP_KERNEL);
  8019. if (pce_entry == NULL) {
  8020. rc = -ENOMEM;
  8021. goto out;
  8022. }
  8023. for (i = 0, p = pfde_tbl; i < tbl_size; i++, p++) {
  8024. if (p->unit_num == pce_info_use->unit_num) {
  8025. pce_entry->ce_num = p->ce;
  8026. pce_entry->ce_pipe_pair =
  8027. p->pipe_pair;
  8028. pce_entry->valid = true;
  8029. pce_entry++;
  8030. }
  8031. }
  8032. }
  8033. kfree(unit_tbl);
  8034. unit_tbl = NULL;
  8035. kfree(pfde_tbl);
  8036. pfde_tbl = NULL;
  8037. }
  8038. if (!old_db)
  8039. goto out1;
  8040. if (of_property_read_bool((&pdev->dev)->of_node,
  8041. "qcom,support-multiple-ce-hw-instance")) {
  8042. if (of_property_read_u32((&pdev->dev)->of_node,
  8043. "qcom,hlos-num-ce-hw-instances",
  8044. &hlos_num_ce_hw_instances)) {
  8045. pr_err("Fail: get hlos number of ce hw instance\n");
  8046. rc = -EINVAL;
  8047. goto out;
  8048. }
  8049. } else {
  8050. hlos_num_ce_hw_instances = 1;
  8051. }
  8052. if (hlos_num_ce_hw_instances > MAX_CE_PIPE_PAIR_PER_UNIT) {
  8053. pr_err("Fail: hlos number of ce hw instance exceeds %d\n",
  8054. MAX_CE_PIPE_PAIR_PER_UNIT);
  8055. rc = -EINVAL;
  8056. goto out;
  8057. }
  8058. if (of_property_read_u32_array((&pdev->dev)->of_node,
  8059. "qcom,hlos-ce-hw-instance", hlos_ce_hw_instance,
  8060. hlos_num_ce_hw_instances)) {
  8061. pr_err("Fail: get hlos ce hw instance info\n");
  8062. rc = -EINVAL;
  8063. goto out;
  8064. }
  8065. if (qseecom.support_fde) {
  8066. pce_info_use = qseecom.ce_info.fde =
  8067. kzalloc(sizeof(struct qseecom_ce_info_use), GFP_KERNEL);
  8068. if (!pce_info_use) {
  8069. rc = -ENOMEM;
  8070. goto out;
  8071. }
  8072. /* by default for old db */
  8073. qseecom.ce_info.num_fde = DEFAULT_NUM_CE_INFO_UNIT;
  8074. pce_info_use->unit_num = DEFAULT_CE_INFO_UNIT;
  8075. pce_info_use->alloc = false;
  8076. pce_info_use->type = CE_PIPE_PAIR_USE_TYPE_FDE;
  8077. pce_info_use->ce_pipe_entry = NULL;
  8078. if (of_property_read_u32((&pdev->dev)->of_node,
  8079. "qcom,disk-encrypt-pipe-pair",
  8080. &disk_encrypt_pipe)) {
  8081. pr_err("Fail to get FDE pipe information.\n");
  8082. rc = -EINVAL;
  8083. goto out;
  8084. } else {
  8085. pr_debug("disk-encrypt-pipe-pair=0x%x\n",
  8086. disk_encrypt_pipe);
  8087. }
  8088. entry = pce_info_use->num_ce_pipe_entries =
  8089. hlos_num_ce_hw_instances;
  8090. pce_entry = pce_info_use->ce_pipe_entry =
  8091. kcalloc(entry,
  8092. sizeof(struct qseecom_ce_pipe_entry),
  8093. GFP_KERNEL);
  8094. if (pce_entry == NULL) {
  8095. rc = -ENOMEM;
  8096. goto out;
  8097. }
  8098. for (i = 0; i < entry; i++) {
  8099. pce_entry->ce_num = hlos_ce_hw_instance[i];
  8100. pce_entry->ce_pipe_pair = disk_encrypt_pipe;
  8101. pce_entry->valid = 1;
  8102. pce_entry++;
  8103. }
  8104. } else {
  8105. pr_warn("Device does not support FDE\n");
  8106. disk_encrypt_pipe = 0xff;
  8107. }
  8108. if (qseecom.support_pfe) {
  8109. pce_info_use = qseecom.ce_info.pfe =
  8110. kzalloc(sizeof(struct qseecom_ce_info_use), GFP_KERNEL);
  8111. if (!pce_info_use) {
  8112. rc = -ENOMEM;
  8113. goto out;
  8114. }
  8115. /* by default for old db */
  8116. qseecom.ce_info.num_pfe = DEFAULT_NUM_CE_INFO_UNIT;
  8117. pce_info_use->unit_num = DEFAULT_CE_INFO_UNIT;
  8118. pce_info_use->alloc = false;
  8119. pce_info_use->type = CE_PIPE_PAIR_USE_TYPE_PFE;
  8120. pce_info_use->ce_pipe_entry = NULL;
  8121. if (of_property_read_u32((&pdev->dev)->of_node,
  8122. "qcom,file-encrypt-pipe-pair",
  8123. &file_encrypt_pipe)) {
  8124. pr_err("Fail to get PFE pipe information.\n");
  8125. rc = -EINVAL;
  8126. goto out;
  8127. } else {
  8128. pr_debug("file-encrypt-pipe-pair=0x%x\n",
  8129. file_encrypt_pipe);
  8130. }
  8131. entry = pce_info_use->num_ce_pipe_entries =
  8132. hlos_num_ce_hw_instances;
  8133. pce_entry = pce_info_use->ce_pipe_entry =
  8134. kcalloc(entry,
  8135. sizeof(struct qseecom_ce_pipe_entry),
  8136. GFP_KERNEL);
  8137. if (pce_entry == NULL) {
  8138. rc = -ENOMEM;
  8139. goto out;
  8140. }
  8141. for (i = 0; i < entry; i++) {
  8142. pce_entry->ce_num = hlos_ce_hw_instance[i];
  8143. pce_entry->ce_pipe_pair = file_encrypt_pipe;
  8144. pce_entry->valid = 1;
  8145. pce_entry++;
  8146. }
  8147. } else {
  8148. pr_warn("Device does not support PFE\n");
  8149. file_encrypt_pipe = 0xff;
  8150. }
  8151. out1:
  8152. qseecom.qsee.instance = qseecom.ce_info.qsee_ce_hw_instance;
  8153. qseecom.ce_drv.instance = hlos_ce_hw_instance[0];
  8154. out:
  8155. if (rc) {
  8156. if (qseecom.ce_info.fde) {
  8157. pce_info_use = qseecom.ce_info.fde;
  8158. for (i = 0; i < qseecom.ce_info.num_fde; i++) {
  8159. pce_entry = pce_info_use->ce_pipe_entry;
  8160. kfree(pce_entry);
  8161. pce_info_use++;
  8162. }
  8163. }
  8164. kfree(qseecom.ce_info.fde);
  8165. qseecom.ce_info.fde = NULL;
  8166. if (qseecom.ce_info.pfe) {
  8167. pce_info_use = qseecom.ce_info.pfe;
  8168. for (i = 0; i < qseecom.ce_info.num_pfe; i++) {
  8169. pce_entry = pce_info_use->ce_pipe_entry;
  8170. kfree(pce_entry);
  8171. pce_info_use++;
  8172. }
  8173. }
  8174. kfree(qseecom.ce_info.pfe);
  8175. qseecom.ce_info.pfe = NULL;
  8176. }
  8177. kfree(unit_tbl);
  8178. kfree(pfde_tbl);
  8179. return rc;
  8180. }
  8181. static int qseecom_get_ce_info(struct qseecom_dev_handle *data,
  8182. void __user *argp)
  8183. {
  8184. struct qseecom_ce_info_req req;
  8185. struct qseecom_ce_info_req *pinfo = &req;
  8186. int ret = 0;
  8187. int i;
  8188. unsigned int entries;
  8189. struct qseecom_ce_info_use *pce_info_use, *p;
  8190. int total = 0;
  8191. bool found = false;
  8192. struct qseecom_ce_pipe_entry *pce_entry;
  8193. ret = copy_from_user(pinfo, argp,
  8194. sizeof(struct qseecom_ce_info_req));
  8195. if (ret) {
  8196. pr_err("copy_from_user failed\n");
  8197. return ret;
  8198. }
  8199. switch (pinfo->usage) {
  8200. case QSEOS_KM_USAGE_DISK_ENCRYPTION:
  8201. case QSEOS_KM_USAGE_UFS_ICE_DISK_ENCRYPTION:
  8202. case QSEOS_KM_USAGE_SDCC_ICE_DISK_ENCRYPTION:
  8203. if (qseecom.support_fde) {
  8204. p = qseecom.ce_info.fde;
  8205. total = qseecom.ce_info.num_fde;
  8206. } else {
  8207. pr_err("system does not support fde\n");
  8208. return -EINVAL;
  8209. }
  8210. break;
  8211. case QSEOS_KM_USAGE_FILE_ENCRYPTION:
  8212. if (qseecom.support_pfe) {
  8213. p = qseecom.ce_info.pfe;
  8214. total = qseecom.ce_info.num_pfe;
  8215. } else {
  8216. pr_err("system does not support pfe\n");
  8217. return -EINVAL;
  8218. }
  8219. break;
  8220. default:
  8221. pr_err("unsupported usage %d\n", pinfo->usage);
  8222. return -EINVAL;
  8223. }
  8224. pce_info_use = NULL;
  8225. for (i = 0; i < total; i++) {
  8226. if (!p->alloc)
  8227. pce_info_use = p;
  8228. else if (!memcmp(p->handle, pinfo->handle,
  8229. MAX_CE_INFO_HANDLE_SIZE)) {
  8230. pce_info_use = p;
  8231. found = true;
  8232. break;
  8233. }
  8234. p++;
  8235. }
  8236. if (pce_info_use == NULL)
  8237. return -EBUSY;
  8238. pinfo->unit_num = pce_info_use->unit_num;
  8239. if (!pce_info_use->alloc) {
  8240. pce_info_use->alloc = true;
  8241. memcpy(pce_info_use->handle,
  8242. pinfo->handle, MAX_CE_INFO_HANDLE_SIZE);
  8243. }
  8244. if (pce_info_use->num_ce_pipe_entries >
  8245. MAX_CE_PIPE_PAIR_PER_UNIT)
  8246. entries = MAX_CE_PIPE_PAIR_PER_UNIT;
  8247. else
  8248. entries = pce_info_use->num_ce_pipe_entries;
  8249. pinfo->num_ce_pipe_entries = entries;
  8250. pce_entry = pce_info_use->ce_pipe_entry;
  8251. for (i = 0; i < entries; i++, pce_entry++)
  8252. pinfo->ce_pipe_entry[i] = *pce_entry;
  8253. for (; i < MAX_CE_PIPE_PAIR_PER_UNIT; i++)
  8254. pinfo->ce_pipe_entry[i].valid = 0;
  8255. if (copy_to_user(argp, pinfo, sizeof(struct qseecom_ce_info_req))) {
  8256. pr_err("copy_to_user failed\n");
  8257. ret = -EFAULT;
  8258. }
  8259. return ret;
  8260. }
  8261. static int qseecom_free_ce_info(struct qseecom_dev_handle *data,
  8262. void __user *argp)
  8263. {
  8264. struct qseecom_ce_info_req req;
  8265. struct qseecom_ce_info_req *pinfo = &req;
  8266. int ret = 0;
  8267. struct qseecom_ce_info_use *p;
  8268. int total = 0;
  8269. int i;
  8270. bool found = false;
  8271. ret = copy_from_user(pinfo, argp,
  8272. sizeof(struct qseecom_ce_info_req));
  8273. if (ret)
  8274. return ret;
  8275. switch (pinfo->usage) {
  8276. case QSEOS_KM_USAGE_DISK_ENCRYPTION:
  8277. case QSEOS_KM_USAGE_UFS_ICE_DISK_ENCRYPTION:
  8278. case QSEOS_KM_USAGE_SDCC_ICE_DISK_ENCRYPTION:
  8279. if (qseecom.support_fde) {
  8280. p = qseecom.ce_info.fde;
  8281. total = qseecom.ce_info.num_fde;
  8282. } else {
  8283. pr_err("system does not support fde\n");
  8284. return -EINVAL;
  8285. }
  8286. break;
  8287. case QSEOS_KM_USAGE_FILE_ENCRYPTION:
  8288. if (qseecom.support_pfe) {
  8289. p = qseecom.ce_info.pfe;
  8290. total = qseecom.ce_info.num_pfe;
  8291. } else {
  8292. pr_err("system does not support pfe\n");
  8293. return -EINVAL;
  8294. }
  8295. break;
  8296. default:
  8297. pr_err("unsupported usage %d\n", pinfo->usage);
  8298. return -EINVAL;
  8299. }
  8300. for (i = 0; i < total; i++) {
  8301. if (p->alloc &&
  8302. !memcmp(p->handle, pinfo->handle,
  8303. MAX_CE_INFO_HANDLE_SIZE)) {
  8304. memset(p->handle, 0, MAX_CE_INFO_HANDLE_SIZE);
  8305. p->alloc = false;
  8306. found = true;
  8307. break;
  8308. }
  8309. p++;
  8310. }
  8311. return ret;
  8312. }
  8313. static int qseecom_query_ce_info(struct qseecom_dev_handle *data,
  8314. void __user *argp)
  8315. {
  8316. struct qseecom_ce_info_req req;
  8317. struct qseecom_ce_info_req *pinfo = &req;
  8318. int ret = 0;
  8319. int i;
  8320. unsigned int entries;
  8321. struct qseecom_ce_info_use *pce_info_use, *p;
  8322. int total = 0;
  8323. bool found = false;
  8324. struct qseecom_ce_pipe_entry *pce_entry;
  8325. ret = copy_from_user(pinfo, argp,
  8326. sizeof(struct qseecom_ce_info_req));
  8327. if (ret)
  8328. return ret;
  8329. switch (pinfo->usage) {
  8330. case QSEOS_KM_USAGE_DISK_ENCRYPTION:
  8331. case QSEOS_KM_USAGE_UFS_ICE_DISK_ENCRYPTION:
  8332. case QSEOS_KM_USAGE_SDCC_ICE_DISK_ENCRYPTION:
  8333. if (qseecom.support_fde) {
  8334. p = qseecom.ce_info.fde;
  8335. total = qseecom.ce_info.num_fde;
  8336. } else {
  8337. pr_err("system does not support fde\n");
  8338. return -EINVAL;
  8339. }
  8340. break;
  8341. case QSEOS_KM_USAGE_FILE_ENCRYPTION:
  8342. if (qseecom.support_pfe) {
  8343. p = qseecom.ce_info.pfe;
  8344. total = qseecom.ce_info.num_pfe;
  8345. } else {
  8346. pr_err("system does not support pfe\n");
  8347. return -EINVAL;
  8348. }
  8349. break;
  8350. default:
  8351. pr_err("unsupported usage %d\n", pinfo->usage);
  8352. return -EINVAL;
  8353. }
  8354. pce_info_use = NULL;
  8355. pinfo->unit_num = INVALID_CE_INFO_UNIT_NUM;
  8356. pinfo->num_ce_pipe_entries = 0;
  8357. for (i = 0; i < MAX_CE_PIPE_PAIR_PER_UNIT; i++)
  8358. pinfo->ce_pipe_entry[i].valid = 0;
  8359. for (i = 0; i < total; i++) {
  8360. if (p->alloc && !memcmp(p->handle,
  8361. pinfo->handle, MAX_CE_INFO_HANDLE_SIZE)) {
  8362. pce_info_use = p;
  8363. found = true;
  8364. break;
  8365. }
  8366. p++;
  8367. }
  8368. if (!pce_info_use)
  8369. goto out;
  8370. pinfo->unit_num = pce_info_use->unit_num;
  8371. if (pce_info_use->num_ce_pipe_entries >
  8372. MAX_CE_PIPE_PAIR_PER_UNIT)
  8373. entries = MAX_CE_PIPE_PAIR_PER_UNIT;
  8374. else
  8375. entries = pce_info_use->num_ce_pipe_entries;
  8376. pinfo->num_ce_pipe_entries = entries;
  8377. pce_entry = pce_info_use->ce_pipe_entry;
  8378. for (i = 0; i < entries; i++, pce_entry++)
  8379. pinfo->ce_pipe_entry[i] = *pce_entry;
  8380. for (; i < MAX_CE_PIPE_PAIR_PER_UNIT; i++)
  8381. pinfo->ce_pipe_entry[i].valid = 0;
  8382. out:
  8383. if (copy_to_user(argp, pinfo, sizeof(struct qseecom_ce_info_req))) {
  8384. pr_err("copy_to_user failed\n");
  8385. ret = -EFAULT;
  8386. }
  8387. return ret;
  8388. }
  8389. /*
  8390. * Check whitelist feature, and if TZ feature version is < 1.0.0,
  8391. * then whitelist feature is not supported.
  8392. */
  8393. #define GET_FEAT_VERSION_CMD 3
  8394. static int qseecom_check_whitelist_feature(void)
  8395. {
  8396. struct qseecom_scm_desc desc = {0};
  8397. int version = 0;
  8398. int ret = 0;
  8399. desc.args[0] = FEATURE_ID_WHITELIST;
  8400. desc.arginfo = SCM_ARGS(1);
  8401. mutex_lock(&app_access_lock);
  8402. ret = __qseecom_scm_call2_locked(SCM_SIP_FNID(SCM_SVC_INFO,
  8403. GET_FEAT_VERSION_CMD), &desc);
  8404. mutex_unlock(&app_access_lock);
  8405. if (!ret)
  8406. version = desc.ret[0];
  8407. return version >= MAKE_WHITELIST_VERSION(1, 0, 0);
  8408. }
  8409. static int qseecom_init_clk(void)
  8410. {
  8411. int rc;
  8412. if (qseecom.no_clock_support)
  8413. return 0;
  8414. rc = __qseecom_init_clk(CLK_QSEE);
  8415. if (rc)
  8416. return rc;
  8417. if ((qseecom.qsee.instance != qseecom.ce_drv.instance) &&
  8418. (qseecom.support_pfe || qseecom.support_fde)) {
  8419. rc = __qseecom_init_clk(CLK_CE_DRV);
  8420. if (rc) {
  8421. __qseecom_deinit_clk(CLK_QSEE);
  8422. return rc;
  8423. }
  8424. } else {
  8425. qseecom.ce_drv.ce_core_clk = qseecom.qsee.ce_core_clk;
  8426. qseecom.ce_drv.ce_clk = qseecom.qsee.ce_clk;
  8427. qseecom.ce_drv.ce_core_src_clk = qseecom.qsee.ce_core_src_clk;
  8428. qseecom.ce_drv.ce_bus_clk = qseecom.qsee.ce_bus_clk;
  8429. }
  8430. return rc;
  8431. }
  8432. static void qseecom_deinit_clk(void)
  8433. {
  8434. if (qseecom.no_clock_support)
  8435. return;
  8436. __qseecom_deinit_clk(CLK_QSEE);
  8437. if ((qseecom.qsee.instance != qseecom.ce_drv.instance) &&
  8438. (qseecom.support_pfe || qseecom.support_fde))
  8439. __qseecom_deinit_clk(CLK_CE_DRV);
  8440. }
  8441. static int qseecom_init_bus(struct platform_device *pdev)
  8442. {
  8443. int ret = 0;
  8444. if (!qseecom.support_bus_scaling)
  8445. return 0;
  8446. if (qseecom.no_clock_support) {
  8447. pr_err("Can not support bus_scalling if no clock support\n");
  8448. return -EINVAL;
  8449. }
  8450. timer_setup(&(qseecom.bw_scale_down_timer),
  8451. qseecom_scale_bus_bandwidth_timer_callback, 0);
  8452. INIT_WORK(&qseecom.bw_inactive_req_ws,
  8453. qseecom_bw_inactive_req_work);
  8454. qseecom.timer_running = false;
  8455. qseecom.icc_path = of_icc_get(&pdev->dev, "data_path");
  8456. if (IS_ERR(qseecom.icc_path)) {
  8457. ret = PTR_ERR(qseecom.icc_path);
  8458. if (ret != -EPROBE_DEFER)
  8459. pr_err("Unable to get Interconnect path\n");
  8460. return ret;
  8461. }
  8462. return 0;
  8463. }
  8464. static void qseecom_deinit_bus(void)
  8465. {
  8466. if (!qseecom.support_bus_scaling || qseecom.no_clock_support)
  8467. return;
  8468. qseecom_bus_scale_update_request(qseecom.qsee_perf_client, 0);
  8469. icc_put(qseecom.icc_path);
  8470. cancel_work_sync(&qseecom.bw_inactive_req_ws);
  8471. del_timer_sync(&qseecom.bw_scale_down_timer);
  8472. }
  8473. static int qseecom_send_app_region(struct platform_device *pdev)
  8474. {
  8475. struct resource *resource = NULL;
  8476. struct qsee_apps_region_info_64bit_ireq req_64bit;
  8477. struct qseecom_command_scm_resp resp;
  8478. void *cmd_buf = NULL;
  8479. size_t cmd_len;
  8480. int rc = 0;
  8481. if (qseecom.qsee_version < QSEE_VERSION_02 ||
  8482. qseecom.is_apps_region_protected ||
  8483. qseecom.appsbl_qseecom_support)
  8484. return 0;
  8485. resource = platform_get_resource_byname(pdev,
  8486. IORESOURCE_MEM, "secapp-region");
  8487. if (!resource) {
  8488. pr_err("Fail to get secure app region info\n");
  8489. return -ENOMEM;
  8490. }
  8491. req_64bit.qsee_cmd_id = QSEOS_APP_REGION_NOTIFICATION;
  8492. req_64bit.addr = resource->start;
  8493. req_64bit.size = resource_size(resource);
  8494. cmd_buf = (void *)&req_64bit;
  8495. cmd_len = sizeof(struct qsee_apps_region_info_64bit_ireq);
  8496. pr_warn("secure app region addr=0x%llx size=0x%x\n",
  8497. req_64bit.addr, req_64bit.size);
  8498. rc = __qseecom_enable_clk(CLK_QSEE);
  8499. if (rc) {
  8500. pr_err("CLK_QSEE enabling failed (%d)\n", rc);
  8501. return rc;
  8502. }
  8503. mutex_lock(&app_access_lock);
  8504. rc = qseecom_scm_call(SCM_SVC_TZSCHEDULER, 1,
  8505. cmd_buf, cmd_len,
  8506. &resp, sizeof(resp));
  8507. mutex_unlock(&app_access_lock);
  8508. __qseecom_disable_clk(CLK_QSEE);
  8509. if (rc || (resp.result != QSEOS_RESULT_SUCCESS)) {
  8510. pr_err("send secapp reg fail %d resp.res %d\n",
  8511. rc, resp.result);
  8512. return -EINVAL;
  8513. }
  8514. return rc;
  8515. }
  8516. static void qseecom_release_ce_data(void)
  8517. {
  8518. int i;
  8519. struct qseecom_ce_info_use *pce_info_use = NULL;
  8520. if (qseecom.ce_info.fde) {
  8521. pce_info_use = qseecom.ce_info.fde;
  8522. for (i = 0; i < qseecom.ce_info.num_fde; i++) {
  8523. kfree_sensitive(pce_info_use->ce_pipe_entry);
  8524. pce_info_use++;
  8525. }
  8526. kfree(qseecom.ce_info.fde);
  8527. }
  8528. if (qseecom.ce_info.pfe) {
  8529. pce_info_use = qseecom.ce_info.pfe;
  8530. for (i = 0; i < qseecom.ce_info.num_pfe; i++) {
  8531. kfree_sensitive(pce_info_use->ce_pipe_entry);
  8532. pce_info_use++;
  8533. }
  8534. kfree(qseecom.ce_info.pfe);
  8535. }
  8536. }
  8537. static int qseecom_init_dev(struct platform_device *pdev)
  8538. {
  8539. int rc = 0;
  8540. rc = alloc_chrdev_region(&qseecom.qseecom_device_no,
  8541. 0, 1, QSEECOM_DEV);
  8542. if (rc < 0) {
  8543. pr_err("alloc_chrdev_region failed %d\n", rc);
  8544. return rc;
  8545. }
  8546. qseecom.driver_class = class_create(THIS_MODULE, QSEECOM_DEV);
  8547. if (IS_ERR(qseecom.driver_class)) {
  8548. rc = PTR_ERR(qseecom.driver_class);
  8549. pr_err("class_create failed %x\n", rc);
  8550. goto exit_unreg_chrdev_region;
  8551. }
  8552. qseecom.pdev = device_create(qseecom.driver_class, NULL,
  8553. qseecom.qseecom_device_no, NULL,
  8554. QSEECOM_DEV);
  8555. if (IS_ERR(qseecom.pdev)) {
  8556. pr_err("class_device_create failed %d\n", rc);
  8557. rc = PTR_ERR(qseecom.pdev);
  8558. goto exit_destroy_class;
  8559. }
  8560. cdev_init(&qseecom.cdev, &qseecom_fops);
  8561. qseecom.cdev.owner = THIS_MODULE;
  8562. rc = cdev_add(&qseecom.cdev,
  8563. MKDEV(MAJOR(qseecom.qseecom_device_no), 0), 1);
  8564. if (rc < 0) {
  8565. pr_err("cdev_add failed %d\n", rc);
  8566. goto exit_destroy_device;
  8567. }
  8568. qseecom.dev = &pdev->dev;
  8569. rc = dma_set_mask(qseecom.dev, DMA_BIT_MASK(64));
  8570. if (rc) {
  8571. pr_err("qseecom failed to set dma mask %d\n", rc);
  8572. goto exit_del_cdev;
  8573. }
  8574. if (!qseecom.dev->dma_parms) {
  8575. qseecom.dev->dma_parms =
  8576. kzalloc(sizeof(*qseecom.dev->dma_parms), GFP_KERNEL);
  8577. if (!qseecom.dev->dma_parms) {
  8578. rc = -ENOMEM;
  8579. goto exit_del_cdev;
  8580. }
  8581. }
  8582. dma_set_max_seg_size(qseecom.dev, DMA_BIT_MASK(32));
  8583. rc = of_reserved_mem_device_init_by_idx(&pdev->dev,
  8584. (&pdev->dev)->of_node, 0);
  8585. if (rc) {
  8586. pr_err("Failed to initialize reserved mem, ret %d\n", rc);
  8587. goto exit_del_cdev;
  8588. }
  8589. return 0;
  8590. exit_del_cdev:
  8591. cdev_del(&qseecom.cdev);
  8592. exit_destroy_device:
  8593. device_destroy(qseecom.driver_class, qseecom.qseecom_device_no);
  8594. exit_destroy_class:
  8595. class_destroy(qseecom.driver_class);
  8596. exit_unreg_chrdev_region:
  8597. unregister_chrdev_region(qseecom.qseecom_device_no, 1);
  8598. return rc;
  8599. }
  8600. static void qseecom_deinit_dev(void)
  8601. {
  8602. kfree(qseecom.dev->dma_parms);
  8603. qseecom.dev->dma_parms = NULL;
  8604. cdev_del(&qseecom.cdev);
  8605. device_destroy(qseecom.driver_class, qseecom.qseecom_device_no);
  8606. class_destroy(qseecom.driver_class);
  8607. unregister_chrdev_region(qseecom.qseecom_device_no, 1);
  8608. }
  8609. static int qseecom_init_control(void)
  8610. {
  8611. uint32_t feature = 10;
  8612. struct qseecom_command_scm_resp resp;
  8613. int rc = 0;
  8614. qseecom.qsee_version = QSEEE_VERSION_00;
  8615. mutex_lock(&app_access_lock);
  8616. rc = qseecom_scm_call(6, 3, &feature, sizeof(feature),
  8617. &resp, sizeof(resp));
  8618. mutex_unlock(&app_access_lock);
  8619. pr_info("qseecom.qsee_version = 0x%x\n", resp.result);
  8620. if (rc) {
  8621. pr_err("Failed to get QSEE version info %d\n", rc);
  8622. return rc;
  8623. }
  8624. qseecom.qsee_version = resp.result;
  8625. atomic_set(&qseecom.qseecom_state, QSEECOM_STATE_NOT_READY);
  8626. init_waitqueue_head(&qseecom.app_block_wq);
  8627. qseecom.whitelist_support = true;
  8628. INIT_LIST_HEAD(&qseecom.registered_listener_list_head);
  8629. INIT_LIST_HEAD(&qseecom.registered_app_list_head);
  8630. spin_lock_init(&qseecom.registered_app_list_lock);
  8631. INIT_LIST_HEAD(&qseecom.unregister_lsnr_pending_list_head);
  8632. INIT_LIST_HEAD(&qseecom.registered_kclient_list_head);
  8633. spin_lock_init(&qseecom.registered_kclient_list_lock);
  8634. init_waitqueue_head(&qseecom.send_resp_wq);
  8635. init_waitqueue_head(&qseecom.register_lsnr_pending_wq);
  8636. init_waitqueue_head(&qseecom.unregister_lsnr_kthread_wq);
  8637. INIT_LIST_HEAD(&qseecom.unload_app_pending_list_head);
  8638. init_waitqueue_head(&qseecom.unload_app_kthread_wq);
  8639. qseecom.send_resp_flag = 0;
  8640. qseecom.qseos_version = QSEOS_VERSION_14;
  8641. qseecom.commonlib_loaded = false;
  8642. qseecom.commonlib64_loaded = false;
  8643. qseecom.whitelist_support = qseecom_check_whitelist_feature();
  8644. return rc;
  8645. }
  8646. static int qseecom_parse_dt(struct platform_device *pdev)
  8647. {
  8648. if (!pdev->dev.of_node) {
  8649. pr_err("NULL of_node\n");
  8650. return -ENODEV;
  8651. }
  8652. qseecom.pdev->of_node = pdev->dev.of_node;
  8653. qseecom.support_bus_scaling =
  8654. of_property_read_bool((&pdev->dev)->of_node,
  8655. "qcom,support-bus-scaling");
  8656. qseecom.appsbl_qseecom_support =
  8657. of_property_read_bool((&pdev->dev)->of_node,
  8658. "qcom,appsbl-qseecom-support");
  8659. qseecom.commonlib64_loaded =
  8660. of_property_read_bool((&pdev->dev)->of_node,
  8661. "qcom,commonlib64-loaded-by-uefi");
  8662. qseecom.fde_key_size =
  8663. of_property_read_bool((&pdev->dev)->of_node,
  8664. "qcom,fde-key-size");
  8665. qseecom.no_clock_support =
  8666. of_property_read_bool((&pdev->dev)->of_node,
  8667. "qcom,no-clock-support");
  8668. qseecom.enable_key_wrap_in_ks =
  8669. of_property_read_bool((&pdev->dev)->of_node,
  8670. "qcom,enable-key-wrap-in-ks");
  8671. if (of_property_read_u32((&pdev->dev)->of_node,
  8672. "qcom,qsee-reentrancy-support",
  8673. &qseecom.qsee_reentrancy_support)) {
  8674. pr_warn("qsee reentrancy support phase is not defined, setting to default 0\n");
  8675. qseecom.qsee_reentrancy_support = 0;
  8676. }
  8677. if (of_property_read_u32((&pdev->dev)->of_node,
  8678. "qcom,ce-opp-freq", &qseecom.ce_opp_freq_hz)) {
  8679. pr_debug("CE operating frequency is not defined, setting to default 100MHZ\n");
  8680. qseecom.ce_opp_freq_hz = QSEE_CE_CLK_100MHZ;
  8681. }
  8682. /*
  8683. * By default, appsbl only loads cmnlib. If OEM changes appsbl to
  8684. * load cmnlib64 too, while cmnlib64 img is not present in non_hlos.bin,
  8685. * Pls add "qseecom.commonlib64_loaded = true" here too.
  8686. */
  8687. if (qseecom.is_apps_region_protected ||
  8688. qseecom.appsbl_qseecom_support)
  8689. qseecom.commonlib_loaded = true;
  8690. return 0;
  8691. }
  8692. static int qseecom_create_kthreads(void)
  8693. {
  8694. int rc = 0;
  8695. qseecom.unregister_lsnr_kthread_task = kthread_run(
  8696. __qseecom_unregister_listener_kthread_func,
  8697. NULL, "qseecom-unreg-lsnr");
  8698. if (IS_ERR(qseecom.unregister_lsnr_kthread_task)) {
  8699. rc = PTR_ERR(qseecom.unregister_lsnr_kthread_task);
  8700. pr_err("fail to create kthread to unreg lsnr, rc = %x\n", rc);
  8701. return rc;
  8702. }
  8703. atomic_set(&qseecom.unregister_lsnr_kthread_state,
  8704. LSNR_UNREG_KT_SLEEP);
  8705. /*create a kthread to process pending ta unloading task */
  8706. qseecom.unload_app_kthread_task = kthread_run(
  8707. __qseecom_unload_app_kthread_func,
  8708. NULL, "qseecom-unload-ta");
  8709. if (IS_ERR(qseecom.unload_app_kthread_task)) {
  8710. rc = PTR_ERR(qseecom.unload_app_kthread_task);
  8711. pr_err("failed to create kthread to unload ta, rc = %x\n", rc);
  8712. kthread_stop(qseecom.unregister_lsnr_kthread_task);
  8713. return rc;
  8714. }
  8715. atomic_set(&qseecom.unload_app_kthread_state,
  8716. UNLOAD_APP_KT_SLEEP);
  8717. return 0;
  8718. }
  8719. static int qseecom_register_heap_shmbridge(struct platform_device *pdev,
  8720. char *heap_mem_region_name,
  8721. uint64_t *handle)
  8722. {
  8723. phys_addr_t heap_pa = 0;
  8724. size_t heap_size = 0;
  8725. struct device_node *node = NULL;
  8726. struct reserved_mem *rmem = NULL;
  8727. uint32_t ns_vmids[] = {VMID_HLOS};
  8728. uint32_t ns_vm_perms[] = {PERM_READ | PERM_WRITE};
  8729. node = of_parse_phandle(pdev->dev.of_node, heap_mem_region_name, 0);
  8730. if (!node) {
  8731. pr_err("unable to parse memory-region of heap %d\n", heap_mem_region_name);
  8732. return -EINVAL;
  8733. }
  8734. rmem = of_reserved_mem_lookup(node);
  8735. if (!rmem) {
  8736. pr_err("unable to acquire memory-region of heap %d\n", heap_mem_region_name);
  8737. return -EINVAL;
  8738. }
  8739. heap_pa = rmem->base;
  8740. heap_size = (size_t)rmem->size;
  8741. pr_debug("get heap %d info: shmbridge created\n", heap_mem_region_name);
  8742. return qtee_shmbridge_register(heap_pa,
  8743. heap_size, ns_vmids, ns_vm_perms, 1,
  8744. PERM_READ | PERM_WRITE, handle);
  8745. }
  8746. static int qseecom_register_shmbridge(struct platform_device *pdev)
  8747. {
  8748. int ret = 0;
  8749. if (!qtee_shmbridge_is_enabled())
  8750. return 0;
  8751. ret = qseecom_register_heap_shmbridge(pdev, "qseecom_ta_mem",
  8752. &qseecom.ta_bridge_handle);
  8753. if (ret)
  8754. return ret;
  8755. ret = qseecom_register_heap_shmbridge(pdev, "qseecom_mem",
  8756. &qseecom.qseecom_bridge_handle);
  8757. if (ret) {
  8758. qtee_shmbridge_deregister(qseecom.ta_bridge_handle);
  8759. return ret;
  8760. }
  8761. ret = qseecom_register_heap_shmbridge(pdev, "user_contig_mem",
  8762. &qseecom.user_contig_bridge_handle);
  8763. if (ret) {
  8764. qtee_shmbridge_deregister(qseecom.qseecom_bridge_handle);
  8765. qtee_shmbridge_deregister(qseecom.ta_bridge_handle);
  8766. return ret;
  8767. }
  8768. return 0;
  8769. }
  8770. static void qseecom_deregister_shmbridge(void)
  8771. {
  8772. qtee_shmbridge_deregister(qseecom.user_contig_bridge_handle);
  8773. qtee_shmbridge_deregister(qseecom.qseecom_bridge_handle);
  8774. qtee_shmbridge_deregister(qseecom.ta_bridge_handle);
  8775. }
  8776. static int qseecom_probe(struct platform_device *pdev)
  8777. {
  8778. int rc;
  8779. rc = qseecom_register_shmbridge(pdev);
  8780. if (rc)
  8781. return rc;
  8782. rc = qseecom_init_dev(pdev);
  8783. if (rc)
  8784. goto exit_unregister_bridge;
  8785. rc = qseecom_init_control();
  8786. if (rc)
  8787. goto exit_deinit_dev;
  8788. rc = qseecom_parse_dt(pdev);
  8789. if (rc)
  8790. goto exit_deinit_dev;
  8791. rc = qseecom_retrieve_ce_data(pdev);
  8792. if (rc)
  8793. goto exit_deinit_dev;
  8794. rc = qseecom_init_clk();
  8795. if (rc)
  8796. goto exit_release_ce_data;
  8797. rc = qseecom_init_bus(pdev);
  8798. if (rc)
  8799. goto exit_deinit_clock;
  8800. rc = qseecom_send_app_region(pdev);
  8801. if (rc)
  8802. goto exit_deinit_bus;
  8803. rc = qseecom_create_kthreads();
  8804. if (rc)
  8805. goto exit_deinit_bus;
  8806. #if IS_ENABLED(CONFIG_QSEECOM_PROXY)
  8807. /*If the api fails to get the func ops, print the error and continue
  8808. * Do not treat it as fatal*/
  8809. rc = get_qseecom_kernel_fun_ops();
  8810. if (rc)
  8811. pr_err("failed to provide qseecom ops %d", rc);
  8812. #endif
  8813. atomic_set(&qseecom.qseecom_state, QSEECOM_STATE_READY);
  8814. return 0;
  8815. exit_deinit_bus:
  8816. qseecom_deinit_bus();
  8817. exit_deinit_clock:
  8818. qseecom_deinit_clk();
  8819. exit_release_ce_data:
  8820. qseecom_release_ce_data();
  8821. exit_deinit_dev:
  8822. qseecom_deinit_dev();
  8823. exit_unregister_bridge:
  8824. qseecom_deregister_shmbridge();
  8825. return rc;
  8826. }
  8827. static int qseecom_remove(struct platform_device *pdev)
  8828. {
  8829. struct qseecom_registered_kclient_list *kclient = NULL;
  8830. struct qseecom_registered_kclient_list *kclient_tmp = NULL;
  8831. unsigned long flags = 0;
  8832. int ret = 0;
  8833. atomic_set(&qseecom.qseecom_state, QSEECOM_STATE_NOT_READY);
  8834. spin_lock_irqsave(&qseecom.registered_kclient_list_lock, flags);
  8835. list_for_each_entry_safe(kclient, kclient_tmp,
  8836. &qseecom.registered_kclient_list_head, list) {
  8837. /* Break the loop if client handle is NULL */
  8838. if (!kclient->handle) {
  8839. list_del(&kclient->list);
  8840. kfree_sensitive(kclient);
  8841. break;
  8842. }
  8843. list_del(&kclient->list);
  8844. mutex_lock(&app_access_lock);
  8845. ret = qseecom_unload_app(kclient->handle->dev, false);
  8846. mutex_unlock(&app_access_lock);
  8847. if (!ret) {
  8848. kfree_sensitive(kclient->handle->dev);
  8849. kfree_sensitive(kclient->handle);
  8850. kfree_sensitive(kclient);
  8851. }
  8852. }
  8853. spin_unlock_irqrestore(&qseecom.registered_kclient_list_lock, flags);
  8854. if (qseecom.qseos_version > QSEEE_VERSION_00)
  8855. qseecom_unload_commonlib_image();
  8856. qseecom_deregister_shmbridge();
  8857. kthread_stop(qseecom.unload_app_kthread_task);
  8858. kthread_stop(qseecom.unregister_lsnr_kthread_task);
  8859. qseecom_deinit_bus();
  8860. qseecom_deinit_clk();
  8861. qseecom_release_ce_data();
  8862. qseecom_deinit_dev();
  8863. return ret;
  8864. }
  8865. static int qseecom_suspend(struct platform_device *pdev, pm_message_t state)
  8866. {
  8867. int ret = 0;
  8868. struct qseecom_clk *qclk;
  8869. qclk = &qseecom.qsee;
  8870. atomic_set(&qseecom.qseecom_state, QSEECOM_STATE_SUSPEND);
  8871. if (qseecom.no_clock_support)
  8872. return 0;
  8873. mutex_lock(&qsee_bw_mutex);
  8874. mutex_lock(&clk_access_lock);
  8875. if (qseecom.current_mode != INACTIVE) {
  8876. ret = qseecom_bus_scale_update_request(
  8877. qseecom.qsee_perf_client, INACTIVE);
  8878. if (ret)
  8879. pr_err("Fail to scale down bus\n");
  8880. else
  8881. qseecom.current_mode = INACTIVE;
  8882. }
  8883. if (qclk->clk_access_cnt) {
  8884. if (qclk->ce_clk != NULL)
  8885. clk_disable_unprepare(qclk->ce_clk);
  8886. if (qclk->ce_core_clk != NULL)
  8887. clk_disable_unprepare(qclk->ce_core_clk);
  8888. if (qclk->ce_bus_clk != NULL)
  8889. clk_disable_unprepare(qclk->ce_bus_clk);
  8890. }
  8891. del_timer_sync(&(qseecom.bw_scale_down_timer));
  8892. qseecom.timer_running = false;
  8893. mutex_unlock(&clk_access_lock);
  8894. mutex_unlock(&qsee_bw_mutex);
  8895. cancel_work_sync(&qseecom.bw_inactive_req_ws);
  8896. return 0;
  8897. }
  8898. static int qseecom_resume(struct platform_device *pdev)
  8899. {
  8900. int mode = 0;
  8901. int ret = 0;
  8902. struct qseecom_clk *qclk;
  8903. qclk = &qseecom.qsee;
  8904. if (qseecom.no_clock_support)
  8905. goto exit;
  8906. mutex_lock(&qsee_bw_mutex);
  8907. mutex_lock(&clk_access_lock);
  8908. if (qseecom.cumulative_mode >= HIGH)
  8909. mode = HIGH;
  8910. else
  8911. mode = qseecom.cumulative_mode;
  8912. if (qseecom.cumulative_mode != INACTIVE) {
  8913. ret = qseecom_bus_scale_update_request(
  8914. qseecom.qsee_perf_client, mode);
  8915. if (ret)
  8916. pr_err("Fail to scale up bus to %d\n", mode);
  8917. else
  8918. qseecom.current_mode = mode;
  8919. }
  8920. if (qclk->clk_access_cnt) {
  8921. if (qclk->ce_core_clk != NULL) {
  8922. ret = clk_prepare_enable(qclk->ce_core_clk);
  8923. if (ret) {
  8924. pr_err("Unable to enable/prep CE core clk\n");
  8925. qclk->clk_access_cnt = 0;
  8926. goto err;
  8927. }
  8928. }
  8929. if (qclk->ce_clk != NULL) {
  8930. ret = clk_prepare_enable(qclk->ce_clk);
  8931. if (ret) {
  8932. pr_err("Unable to enable/prep CE iface clk\n");
  8933. qclk->clk_access_cnt = 0;
  8934. goto ce_clk_err;
  8935. }
  8936. }
  8937. if (qclk->ce_bus_clk != NULL) {
  8938. ret = clk_prepare_enable(qclk->ce_bus_clk);
  8939. if (ret) {
  8940. pr_err("Unable to enable/prep CE bus clk\n");
  8941. qclk->clk_access_cnt = 0;
  8942. goto ce_bus_clk_err;
  8943. }
  8944. }
  8945. }
  8946. if (qclk->clk_access_cnt || qseecom.cumulative_mode) {
  8947. qseecom.bw_scale_down_timer.expires = jiffies +
  8948. msecs_to_jiffies(QSEECOM_SEND_CMD_CRYPTO_TIMEOUT);
  8949. mod_timer(&(qseecom.bw_scale_down_timer),
  8950. qseecom.bw_scale_down_timer.expires);
  8951. qseecom.timer_running = true;
  8952. }
  8953. mutex_unlock(&clk_access_lock);
  8954. mutex_unlock(&qsee_bw_mutex);
  8955. goto exit;
  8956. ce_bus_clk_err:
  8957. if (qclk->ce_clk)
  8958. clk_disable_unprepare(qclk->ce_clk);
  8959. ce_clk_err:
  8960. if (qclk->ce_core_clk)
  8961. clk_disable_unprepare(qclk->ce_core_clk);
  8962. err:
  8963. mutex_unlock(&clk_access_lock);
  8964. mutex_unlock(&qsee_bw_mutex);
  8965. ret = -EIO;
  8966. exit:
  8967. atomic_set(&qseecom.qseecom_state, QSEECOM_STATE_READY);
  8968. return ret;
  8969. }
  8970. static const struct of_device_id qseecom_match[] = {
  8971. {
  8972. .compatible = "qcom,qseecom",
  8973. },
  8974. {}
  8975. };
  8976. static struct platform_driver qseecom_plat_driver = {
  8977. .probe = qseecom_probe,
  8978. .remove = qseecom_remove,
  8979. .suspend = qseecom_suspend,
  8980. .resume = qseecom_resume,
  8981. .driver = {
  8982. .name = "qseecom",
  8983. .of_match_table = qseecom_match,
  8984. },
  8985. };
  8986. static int qseecom_init(void)
  8987. {
  8988. return platform_driver_register(&qseecom_plat_driver);
  8989. }
  8990. static void qseecom_exit(void)
  8991. {
  8992. platform_driver_unregister(&qseecom_plat_driver);
  8993. }
  8994. MODULE_LICENSE("GPL v2");
  8995. MODULE_DESCRIPTION("QTI Secure Execution Environment Communicator");
  8996. MODULE_IMPORT_NS(DMA_BUF);
  8997. module_init(qseecom_init);
  8998. module_exit(qseecom_exit);