qseecom.c 133 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066
  1. /*Qualcomm Secure Execution Environment Communicator (QSEECOM) driver
  2. *
  3. * Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #define pr_fmt(fmt) "QSEECOM: %s: " fmt, __func__
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. #include <linux/module.h>
  18. #include <linux/fs.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/cdev.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/sched.h>
  24. #include <linux/list.h>
  25. #include <linux/mutex.h>
  26. #include <linux/io.h>
  27. #include <linux/msm_ion.h>
  28. #include <linux/types.h>
  29. #include <linux/clk.h>
  30. #include <linux/qseecom.h>
  31. #include <linux/elf.h>
  32. #include <linux/firmware.h>
  33. #include <linux/freezer.h>
  34. #include <linux/scatterlist.h>
  35. #include <mach/board.h>
  36. #include <mach/msm_bus.h>
  37. #include <mach/msm_bus_board.h>
  38. #include <mach/scm.h>
  39. #include <mach/subsystem_restart.h>
  40. #include <mach/socinfo.h>
  41. #include <mach/qseecomi.h>
  42. #include <asm/cacheflush.h>
  43. #ifdef CONFIG_SEC_DEBUG
  44. #include <mach/sec_debug.h>
  45. #endif
  46. #include "qseecom_legacy.h"
  47. #include "qseecom_kernel.h"
  48. #define QSEECOM_DEV "qseecom"
  49. #define QSEOS_VERSION_14 0x14
  50. #define QSEEE_VERSION_00 0x400000
  51. #define QSEE_VERSION_01 0x401000
  52. #define QSEE_VERSION_02 0x402000
  53. #define QSEE_VERSION_03 0x403000
  54. #define QSEE_VERSION_04 0x404000
  55. #define QSEE_VERSION_05 0x405000
  56. #define QSEOS_CHECK_VERSION_CMD 0x00001803
  57. #define QSEE_CE_CLK_100MHZ 100000000
  58. #define QSEECOM_MAX_SG_ENTRY 512
  59. #define QSEECOM_INVALID_KEY_ID 0xff
  60. /* Save partition image hash for authentication check */
  61. #define SCM_SAVE_PARTITION_HASH_ID 0x01
  62. /* Check if enterprise security is activate */
  63. #define SCM_IS_ACTIVATED_ID 0x02
  64. #define RPMB_SERVICE 0x2000
  65. #define QSEECOM_SEND_CMD_CRYPTO_TIMEOUT 2000
  66. #define QSEECOM_LOAD_APP_CRYPTO_TIMEOUT 2000
  67. #define TWO 2
  68. #define U32_MAX ((u32)~0U)
  69. enum qseecom_clk_definitions {
  70. CLK_DFAB = 0,
  71. CLK_SFPB,
  72. };
  73. enum qseecom_client_handle_type {
  74. QSEECOM_CLIENT_APP = 1,
  75. QSEECOM_LISTENER_SERVICE,
  76. QSEECOM_SECURE_SERVICE,
  77. QSEECOM_GENERIC,
  78. QSEECOM_UNAVAILABLE_CLIENT_APP,
  79. };
  80. enum qseecom_ce_hw_instance {
  81. CLK_QSEE = 0,
  82. CLK_CE_DRV,
  83. };
  84. static struct class *driver_class;
  85. static dev_t qseecom_device_no;
  86. static DEFINE_MUTEX(qsee_bw_mutex);
  87. static DEFINE_MUTEX(app_access_lock);
  88. static DEFINE_MUTEX(clk_access_lock);
  89. struct qseecom_registered_listener_list {
  90. struct list_head list;
  91. struct qseecom_register_listener_req svc;
  92. uint32_t user_virt_sb_base;
  93. u8 *sb_virt;
  94. s32 sb_phys;
  95. size_t sb_length;
  96. struct ion_handle *ihandle; /* Retrieve phy addr */
  97. wait_queue_head_t rcv_req_wq;
  98. int rcv_req_flag;
  99. };
  100. struct qseecom_registered_app_list {
  101. struct list_head list;
  102. u32 app_id;
  103. u32 ref_cnt;
  104. char app_name[MAX_APP_NAME_SIZE];
  105. };
  106. struct qseecom_registered_kclient_list {
  107. struct list_head list;
  108. struct qseecom_handle *handle;
  109. };
  110. struct ce_hw_usage_info {
  111. uint32_t qsee_ce_hw_instance;
  112. uint32_t hlos_ce_hw_instance;
  113. uint32_t disk_encrypt_pipe;
  114. uint32_t file_encrypt_pipe;
  115. };
  116. struct qseecom_clk {
  117. enum qseecom_ce_hw_instance instance;
  118. struct clk *ce_core_clk;
  119. struct clk *ce_clk;
  120. struct clk *ce_core_src_clk;
  121. struct clk *ce_bus_clk;
  122. uint32_t clk_access_cnt;
  123. };
  124. struct qseecom_control {
  125. struct ion_client *ion_clnt; /* Ion client */
  126. struct list_head registered_listener_list_head;
  127. spinlock_t registered_listener_list_lock;
  128. struct list_head registered_app_list_head;
  129. spinlock_t registered_app_list_lock;
  130. struct list_head registered_kclient_list_head;
  131. spinlock_t registered_kclient_list_lock;
  132. wait_queue_head_t send_resp_wq;
  133. int send_resp_flag;
  134. uint32_t qseos_version;
  135. uint32_t qsee_version;
  136. struct device *pdev;
  137. bool commonlib_loaded;
  138. struct ion_handle *cmnlib_ion_handle;
  139. struct ce_hw_usage_info ce_info;
  140. int qsee_bw_count;
  141. int qsee_sfpb_bw_count;
  142. uint32_t qsee_perf_client;
  143. struct qseecom_clk qsee;
  144. struct qseecom_clk ce_drv;
  145. bool support_bus_scaling;
  146. bool support_fde;
  147. bool support_pfe;
  148. uint32_t cumulative_mode;
  149. enum qseecom_bandwidth_request_mode current_mode;
  150. struct timer_list bw_scale_down_timer;
  151. struct work_struct bw_inactive_req_ws;
  152. struct cdev cdev;
  153. bool timer_running;
  154. bool appsbl_qseecom_support;
  155. };
  156. struct qseecom_client_handle {
  157. u32 app_id;
  158. u8 *sb_virt;
  159. s32 sb_phys;
  160. uint32_t user_virt_sb_base;
  161. size_t sb_length;
  162. struct ion_handle *ihandle; /* Retrieve phy addr */
  163. char app_name[MAX_APP_NAME_SIZE];
  164. };
  165. struct qseecom_listener_handle {
  166. u32 id;
  167. };
  168. static struct qseecom_control qseecom;
  169. struct qseecom_dev_handle {
  170. enum qseecom_client_handle_type type;
  171. union {
  172. struct qseecom_client_handle client;
  173. struct qseecom_listener_handle listener;
  174. };
  175. bool released;
  176. int abort;
  177. wait_queue_head_t abort_wq;
  178. atomic_t ioctl_count;
  179. bool perf_enabled;
  180. bool fast_load_enabled;
  181. enum qseecom_bandwidth_request_mode mode;
  182. };
  183. enum qseecom_set_clear_key_flag {
  184. QSEECOM_CLEAR_CE_KEY_CMD = 0,
  185. QSEECOM_SET_CE_KEY_CMD,
  186. };
  187. struct qseecom_set_key_parameter {
  188. uint32_t ce_hw;
  189. uint32_t pipe;
  190. uint32_t flags;
  191. uint8_t key_id[QSEECOM_KEY_ID_SIZE];
  192. unsigned char hash32[QSEECOM_HASH_SIZE];
  193. enum qseecom_set_clear_key_flag set_clear_key_flag;
  194. };
  195. struct qseecom_sg_entry {
  196. uint32_t phys_addr;
  197. uint32_t len;
  198. };
  199. struct qseecom_key_id_usage_desc {
  200. uint8_t desc[QSEECOM_KEY_ID_SIZE];
  201. };
  202. static struct qseecom_key_id_usage_desc key_id_array[] = {
  203. {
  204. .desc = "Undefined Usage Index",
  205. },
  206. {
  207. .desc = "Full Disk Encryption",
  208. },
  209. {
  210. .desc = "Per File Encryption",
  211. },
  212. };
  213. /* Function proto types */
  214. static int qsee_vote_for_clock(struct qseecom_dev_handle *, int32_t);
  215. static void qsee_disable_clock_vote(struct qseecom_dev_handle *, int32_t);
  216. static int __qseecom_enable_clk(enum qseecom_ce_hw_instance ce);
  217. static void __qseecom_disable_clk(enum qseecom_ce_hw_instance ce);
  218. static int __qseecom_is_svc_unique(struct qseecom_dev_handle *data,
  219. struct qseecom_register_listener_req *svc)
  220. {
  221. struct qseecom_registered_listener_list *ptr;
  222. int unique = 1;
  223. unsigned long flags;
  224. spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags);
  225. list_for_each_entry(ptr, &qseecom.registered_listener_list_head, list) {
  226. if (ptr->svc.listener_id == svc->listener_id) {
  227. pr_err("Service id: %u is already registered\n",
  228. ptr->svc.listener_id);
  229. unique = 0;
  230. break;
  231. }
  232. }
  233. spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags);
  234. return unique;
  235. }
  236. static struct qseecom_registered_listener_list *__qseecom_find_svc(
  237. int32_t listener_id)
  238. {
  239. struct qseecom_registered_listener_list *entry = NULL;
  240. unsigned long flags;
  241. spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags);
  242. list_for_each_entry(entry, &qseecom.registered_listener_list_head, list)
  243. {
  244. if (entry->svc.listener_id == listener_id)
  245. break;
  246. }
  247. spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags);
  248. if ((entry != NULL) && (entry->svc.listener_id != listener_id)) {
  249. pr_err("Service id: %u is not found\n", listener_id);
  250. return NULL;
  251. }
  252. return entry;
  253. }
  254. static int __qseecom_set_sb_memory(struct qseecom_registered_listener_list *svc,
  255. struct qseecom_dev_handle *handle,
  256. struct qseecom_register_listener_req *listener)
  257. {
  258. int ret = 0;
  259. struct qseecom_register_listener_ireq req;
  260. struct qseecom_command_scm_resp resp;
  261. ion_phys_addr_t pa;
  262. /* Get the handle of the shared fd */
  263. svc->ihandle = ion_import_dma_buf(qseecom.ion_clnt,
  264. listener->ifd_data_fd);
  265. if (IS_ERR_OR_NULL(svc->ihandle)) {
  266. pr_err("Ion client could not retrieve the handle\n");
  267. return -ENOMEM;
  268. }
  269. /* Get the physical address of the ION BUF */
  270. ret = ion_phys(qseecom.ion_clnt, svc->ihandle, &pa, &svc->sb_length);
  271. if (ret) {
  272. pr_err("Cannot get phys_addr for the Ion Client, ret = %d\n",
  273. ret);
  274. return ret;
  275. }
  276. /* Populate the structure for sending scm call to load image */
  277. svc->sb_virt = (char *) ion_map_kernel(qseecom.ion_clnt, svc->ihandle);
  278. svc->sb_phys = pa;
  279. req.qsee_cmd_id = QSEOS_REGISTER_LISTENER;
  280. req.listener_id = svc->svc.listener_id;
  281. req.sb_len = svc->sb_length;
  282. req.sb_ptr = (void *)svc->sb_phys;
  283. resp.result = QSEOS_RESULT_INCOMPLETE;
  284. ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req,
  285. sizeof(req), &resp, sizeof(resp));
  286. if (ret) {
  287. pr_err("qseecom_scm_call failed with err: %d\n", ret);
  288. return -EINVAL;
  289. }
  290. if (resp.result != QSEOS_RESULT_SUCCESS) {
  291. pr_err("Error SB registration req: resp.result = %d\n",
  292. resp.result);
  293. return -EPERM;
  294. }
  295. return 0;
  296. }
  297. static int qseecom_register_listener(struct qseecom_dev_handle *data,
  298. void __user *argp)
  299. {
  300. int ret = 0;
  301. unsigned long flags;
  302. struct qseecom_register_listener_req rcvd_lstnr;
  303. struct qseecom_registered_listener_list *new_entry;
  304. ret = copy_from_user(&rcvd_lstnr, argp, sizeof(rcvd_lstnr));
  305. if (ret) {
  306. pr_err("copy_from_user failed\n");
  307. return ret;
  308. }
  309. if (!access_ok(VERIFY_WRITE, (void __user *)rcvd_lstnr.virt_sb_base,
  310. rcvd_lstnr.sb_size))
  311. return -EFAULT;
  312. data->listener.id = 0;
  313. if (!__qseecom_is_svc_unique(data, &rcvd_lstnr)) {
  314. pr_err("Service is not unique and is already registered\n");
  315. data->released = true;
  316. return -EBUSY;
  317. }
  318. new_entry = kmalloc(sizeof(*new_entry), GFP_KERNEL);
  319. if (!new_entry) {
  320. pr_err("kmalloc failed\n");
  321. return -ENOMEM;
  322. }
  323. memcpy(&new_entry->svc, &rcvd_lstnr, sizeof(rcvd_lstnr));
  324. new_entry->rcv_req_flag = 0;
  325. new_entry->svc.listener_id = rcvd_lstnr.listener_id;
  326. new_entry->sb_length = rcvd_lstnr.sb_size;
  327. new_entry->user_virt_sb_base = rcvd_lstnr.virt_sb_base;
  328. if (__qseecom_set_sb_memory(new_entry, data, &rcvd_lstnr)) {
  329. pr_err("qseecom_set_sb_memoryfailed\n");
  330. kzfree(new_entry);
  331. return -ENOMEM;
  332. }
  333. init_waitqueue_head(&new_entry->rcv_req_wq);
  334. spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags);
  335. list_add_tail(&new_entry->list, &qseecom.registered_listener_list_head);
  336. spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags);
  337. data->listener.id = rcvd_lstnr.listener_id;
  338. return ret;
  339. }
  340. static int qseecom_unregister_listener(struct qseecom_dev_handle *data)
  341. {
  342. int ret = 0;
  343. unsigned long flags;
  344. uint32_t unmap_mem = 0;
  345. struct qseecom_register_listener_ireq req;
  346. struct qseecom_registered_listener_list *ptr_svc = NULL;
  347. struct qseecom_command_scm_resp resp;
  348. struct ion_handle *ihandle = NULL; /* Retrieve phy addr */
  349. if (data->released) {
  350. pr_err("Don't unregister lsnr %d\n", data->listener.id);
  351. return -EINVAL;
  352. }
  353. req.qsee_cmd_id = QSEOS_DEREGISTER_LISTENER;
  354. req.listener_id = data->listener.id;
  355. resp.result = QSEOS_RESULT_INCOMPLETE;
  356. ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req,
  357. sizeof(req), &resp, sizeof(resp));
  358. if (ret) {
  359. pr_err("scm_call() failed with err: %d (lstnr id=%d)\n",
  360. ret, data->listener.id);
  361. return ret;
  362. }
  363. if (resp.result != QSEOS_RESULT_SUCCESS) {
  364. pr_err("Failed resp.result=%d,(lstnr id=%d)\n",
  365. resp.result, data->listener.id);
  366. return -EPERM;
  367. }
  368. data->abort = 1;
  369. spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags);
  370. list_for_each_entry(ptr_svc, &qseecom.registered_listener_list_head,
  371. list) {
  372. if (ptr_svc->svc.listener_id == data->listener.id) {
  373. wake_up_all(&ptr_svc->rcv_req_wq);
  374. break;
  375. }
  376. }
  377. spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags);
  378. while (atomic_read(&data->ioctl_count) > 1) {
  379. if (wait_event_freezable(data->abort_wq,
  380. atomic_read(&data->ioctl_count) <= 1)) {
  381. pr_err("Interrupted from abort\n");
  382. ret = -ERESTARTSYS;
  383. break;
  384. }
  385. }
  386. spin_lock_irqsave(&qseecom.registered_listener_list_lock, flags);
  387. list_for_each_entry(ptr_svc,
  388. &qseecom.registered_listener_list_head,
  389. list)
  390. {
  391. if (ptr_svc->svc.listener_id == data->listener.id) {
  392. if (ptr_svc->sb_virt) {
  393. unmap_mem = 1;
  394. ihandle = ptr_svc->ihandle;
  395. }
  396. list_del(&ptr_svc->list);
  397. kzfree(ptr_svc);
  398. break;
  399. }
  400. }
  401. spin_unlock_irqrestore(&qseecom.registered_listener_list_lock, flags);
  402. /* Unmap the memory */
  403. if (unmap_mem) {
  404. if (!IS_ERR_OR_NULL(ihandle)) {
  405. ion_unmap_kernel(qseecom.ion_clnt, ihandle);
  406. ion_free(qseecom.ion_clnt, ihandle);
  407. }
  408. }
  409. data->released = true;
  410. return ret;
  411. }
  412. static int __qseecom_set_msm_bus_request(uint32_t mode)
  413. {
  414. int ret = 0;
  415. struct qseecom_clk *qclk;
  416. qclk = &qseecom.qsee;
  417. if (qclk->ce_core_src_clk != NULL) {
  418. if (mode == INACTIVE) {
  419. __qseecom_disable_clk(CLK_QSEE);
  420. } else {
  421. ret = __qseecom_enable_clk(CLK_QSEE);
  422. if (ret)
  423. pr_err("CLK enabling failed (%d) MODE (%d)\n",
  424. ret, mode);
  425. }
  426. }
  427. if ((!ret) && (qseecom.current_mode != mode)) {
  428. ret = msm_bus_scale_client_update_request(
  429. qseecom.qsee_perf_client, mode);
  430. if (ret) {
  431. pr_err("Bandwidth req failed(%d) MODE (%d)\n",
  432. ret, mode);
  433. if (qclk->ce_core_src_clk != NULL) {
  434. if (mode == INACTIVE)
  435. __qseecom_enable_clk(CLK_QSEE);
  436. else
  437. __qseecom_disable_clk(CLK_QSEE);
  438. }
  439. }
  440. qseecom.current_mode = mode;
  441. }
  442. return ret;
  443. }
  444. static void qseecom_bw_inactive_req_work(struct work_struct *work)
  445. {
  446. mutex_lock(&app_access_lock);
  447. mutex_lock(&qsee_bw_mutex);
  448. __qseecom_set_msm_bus_request(INACTIVE);
  449. pr_debug("current_mode = %d, cumulative_mode = %d\n",
  450. qseecom.current_mode, qseecom.cumulative_mode);
  451. qseecom.timer_running = false;
  452. mutex_unlock(&qsee_bw_mutex);
  453. mutex_unlock(&app_access_lock);
  454. return;
  455. }
  456. static void qseecom_scale_bus_bandwidth_timer_callback(unsigned long data)
  457. {
  458. schedule_work(&qseecom.bw_inactive_req_ws);
  459. return;
  460. }
  461. static int __qseecom_decrease_clk_ref_count(enum qseecom_ce_hw_instance ce)
  462. {
  463. struct qseecom_clk *qclk;
  464. int ret = 0;
  465. mutex_lock(&clk_access_lock);
  466. if (ce == CLK_QSEE)
  467. qclk = &qseecom.qsee;
  468. else
  469. qclk = &qseecom.ce_drv;
  470. if (qclk->clk_access_cnt > 2) {
  471. pr_err("Invalid clock ref count %d\n", qclk->clk_access_cnt);
  472. ret = -EINVAL;
  473. goto err_dec_ref_cnt;
  474. }
  475. if (qclk->clk_access_cnt == 2)
  476. qclk->clk_access_cnt--;
  477. err_dec_ref_cnt:
  478. mutex_unlock(&clk_access_lock);
  479. return ret;
  480. }
  481. static int qseecom_scale_bus_bandwidth_timer(uint32_t mode)
  482. {
  483. int32_t ret = 0;
  484. int32_t request_mode = INACTIVE;
  485. mutex_lock(&qsee_bw_mutex);
  486. if (mode == 0) {
  487. if (qseecom.cumulative_mode > MEDIUM)
  488. request_mode = HIGH;
  489. else
  490. request_mode = qseecom.cumulative_mode;
  491. } else {
  492. request_mode = mode;
  493. }
  494. ret = __qseecom_set_msm_bus_request(request_mode);
  495. if (ret) {
  496. pr_err("set msm bus request failed (%d),request_mode (%d)\n",
  497. ret, request_mode);
  498. goto err_scale_timer;
  499. }
  500. if (qseecom.timer_running) {
  501. ret = __qseecom_decrease_clk_ref_count(CLK_QSEE);
  502. if (ret) {
  503. pr_err("Failed to decrease clk ref count.\n");
  504. goto err_scale_timer;
  505. }
  506. del_timer_sync(&(qseecom.bw_scale_down_timer));
  507. qseecom.timer_running = false;
  508. }
  509. err_scale_timer:
  510. mutex_unlock(&qsee_bw_mutex);
  511. return ret;
  512. }
  513. static int qseecom_unregister_bus_bandwidth_needs(
  514. struct qseecom_dev_handle *data)
  515. {
  516. int32_t ret = 0;
  517. qseecom.cumulative_mode -= data->mode;
  518. data->mode = INACTIVE;
  519. return ret;
  520. }
  521. static int __qseecom_register_bus_bandwidth_needs(
  522. struct qseecom_dev_handle *data, uint32_t request_mode)
  523. {
  524. int32_t ret = 0;
  525. if (data->mode == INACTIVE) {
  526. qseecom.cumulative_mode += request_mode;
  527. data->mode = request_mode;
  528. } else {
  529. if (data->mode != request_mode) {
  530. qseecom.cumulative_mode -= data->mode;
  531. qseecom.cumulative_mode += request_mode;
  532. data->mode = request_mode;
  533. }
  534. }
  535. return ret;
  536. }
  537. static int qseecom_perf_enable(struct qseecom_dev_handle *data)
  538. {
  539. int ret = 0;
  540. ret = qsee_vote_for_clock(data, CLK_DFAB);
  541. if (ret) {
  542. pr_err("Failed to vote for DFAB clock with err %d\n", ret);
  543. goto perf_enable_exit;
  544. }
  545. ret = qsee_vote_for_clock(data, CLK_SFPB);
  546. if (ret) {
  547. qsee_disable_clock_vote(data, CLK_DFAB);
  548. pr_err("Failed to vote for SFPB clock with err %d\n", ret);
  549. goto perf_enable_exit;
  550. }
  551. perf_enable_exit:
  552. return ret;
  553. }
  554. static int qseecom_scale_bus_bandwidth(struct qseecom_dev_handle *data,
  555. void __user *argp)
  556. {
  557. int32_t ret = 0;
  558. int32_t req_mode;
  559. ret = copy_from_user(&req_mode, argp, sizeof(req_mode));
  560. if (ret) {
  561. pr_err("copy_from_user failed\n");
  562. return ret;
  563. }
  564. if (req_mode > HIGH) {
  565. pr_err("Invalid bandwidth mode (%d)\n", req_mode);
  566. return -EINVAL;
  567. }
  568. /*
  569. * Register bus bandwidth needs if bus scaling feature is enabled;
  570. * otherwise, qseecom enable/disable clocks for the client directly.
  571. */
  572. if (qseecom.support_bus_scaling) {
  573. mutex_lock(&qsee_bw_mutex);
  574. ret = __qseecom_register_bus_bandwidth_needs(data, req_mode);
  575. mutex_unlock(&qsee_bw_mutex);
  576. } else {
  577. pr_debug("Bus scaling feature is NOT enabled\n");
  578. pr_debug("request bandwidth mode %d for the client\n",
  579. req_mode);
  580. if (req_mode != INACTIVE) {
  581. ret = qseecom_perf_enable(data);
  582. if (ret)
  583. pr_err("Failed to vote for clock with err %d\n",
  584. ret);
  585. } else {
  586. qsee_disable_clock_vote(data, CLK_DFAB);
  587. qsee_disable_clock_vote(data, CLK_SFPB);
  588. }
  589. }
  590. return ret;
  591. }
  592. static void __qseecom_add_bw_scale_down_timer(uint32_t duration)
  593. {
  594. mutex_lock(&qsee_bw_mutex);
  595. qseecom.bw_scale_down_timer.expires = jiffies +
  596. msecs_to_jiffies(duration);
  597. mod_timer(&(qseecom.bw_scale_down_timer),
  598. qseecom.bw_scale_down_timer.expires);
  599. qseecom.timer_running = true;
  600. mutex_unlock(&qsee_bw_mutex);
  601. }
  602. static void __qseecom_disable_clk_scale_down(struct qseecom_dev_handle *data)
  603. {
  604. if (!qseecom.support_bus_scaling)
  605. qsee_disable_clock_vote(data, CLK_SFPB);
  606. else
  607. __qseecom_add_bw_scale_down_timer(
  608. QSEECOM_LOAD_APP_CRYPTO_TIMEOUT);
  609. return;
  610. }
  611. static int __qseecom_enable_clk_scale_up(struct qseecom_dev_handle *data)
  612. {
  613. int ret = 0;
  614. if (qseecom.support_bus_scaling) {
  615. ret = qseecom_scale_bus_bandwidth_timer(MEDIUM);
  616. if (ret)
  617. pr_err("Failed to set bw MEDIUM.\n");
  618. } else {
  619. ret = qsee_vote_for_clock(data, CLK_SFPB);
  620. if (ret)
  621. pr_err("Fail vote for clk SFPB ret %d\n", ret);
  622. }
  623. return ret;
  624. }
  625. static int qseecom_set_client_mem_param(struct qseecom_dev_handle *data,
  626. void __user *argp)
  627. {
  628. ion_phys_addr_t pa;
  629. int32_t ret;
  630. struct qseecom_set_sb_mem_param_req req;
  631. uint32_t len;
  632. /* Copy the relevant information needed for loading the image */
  633. if (copy_from_user(&req, (void __user *)argp, sizeof(req)))
  634. return -EFAULT;
  635. if ((req.ifd_data_fd <= 0) || (req.virt_sb_base == 0) ||
  636. (req.sb_len == 0)) {
  637. pr_err("Inavlid input(s)ion_fd(%d), sb_len(%d), vaddr(0x%pK)\n",
  638. req.ifd_data_fd, req.sb_len, (void *)req.virt_sb_base);
  639. return -EFAULT;
  640. }
  641. if (!access_ok(VERIFY_WRITE, (void __user *)req.virt_sb_base,
  642. req.sb_len))
  643. return -EFAULT;
  644. /* Get the handle of the shared fd */
  645. data->client.ihandle = ion_import_dma_buf(qseecom.ion_clnt,
  646. req.ifd_data_fd);
  647. if (IS_ERR_OR_NULL(data->client.ihandle)) {
  648. pr_err("Ion client could not retrieve the handle\n");
  649. return -ENOMEM;
  650. }
  651. /* Get the physical address of the ION BUF */
  652. ret = ion_phys(qseecom.ion_clnt, data->client.ihandle, &pa, &len);
  653. if (ret) {
  654. pr_err("Cannot get phys_addr for the Ion Client, ret = %d\n",
  655. ret);
  656. return ret;
  657. }
  658. /* Populate the structure for sending scm call to load image */
  659. data->client.sb_virt = (char *) ion_map_kernel(qseecom.ion_clnt,
  660. data->client.ihandle);
  661. data->client.sb_phys = pa;
  662. data->client.sb_length = req.sb_len;
  663. data->client.user_virt_sb_base = req.virt_sb_base;
  664. return 0;
  665. }
  666. static int __qseecom_listener_has_sent_rsp(struct qseecom_dev_handle *data)
  667. {
  668. int ret;
  669. ret = (qseecom.send_resp_flag != 0);
  670. return ret || data->abort;
  671. }
  672. static int __qseecom_process_incomplete_cmd(struct qseecom_dev_handle *data,
  673. struct qseecom_command_scm_resp *resp)
  674. {
  675. int ret = 0;
  676. int rc = 0;
  677. uint32_t lstnr;
  678. unsigned long flags;
  679. struct qseecom_client_listener_data_irsp send_data_rsp;
  680. struct qseecom_registered_listener_list *ptr_svc = NULL;
  681. sigset_t new_sigset;
  682. sigset_t old_sigset;
  683. while (resp->result == QSEOS_RESULT_INCOMPLETE) {
  684. lstnr = resp->data;
  685. /*
  686. * Wake up blocking lsitener service with the lstnr id
  687. */
  688. spin_lock_irqsave(&qseecom.registered_listener_list_lock,
  689. flags);
  690. list_for_each_entry(ptr_svc,
  691. &qseecom.registered_listener_list_head, list) {
  692. if (ptr_svc->svc.listener_id == lstnr) {
  693. ptr_svc->rcv_req_flag = 1;
  694. wake_up_interruptible(&ptr_svc->rcv_req_wq);
  695. break;
  696. }
  697. }
  698. spin_unlock_irqrestore(&qseecom.registered_listener_list_lock,
  699. flags);
  700. if (ptr_svc == NULL) {
  701. pr_err("Listener Svc %d does not exist\n", lstnr);
  702. return -EINVAL;
  703. }
  704. if (ptr_svc->svc.listener_id != lstnr) {
  705. pr_warning("Service requested for does on exist\n");
  706. return -ERESTARTSYS;
  707. }
  708. pr_debug("waking up rcv_req_wq and "
  709. "waiting for send_resp_wq\n");
  710. /* initialize the new signal mask with all signals*/
  711. sigfillset(&new_sigset);
  712. /* block all signals */
  713. sigprocmask(SIG_SETMASK, &new_sigset, &old_sigset);
  714. do {
  715. if (!wait_event_freezable(qseecom.send_resp_wq,
  716. __qseecom_listener_has_sent_rsp(data)))
  717. break;
  718. } while (1);
  719. /* restore signal mask */
  720. sigprocmask(SIG_SETMASK, &old_sigset, NULL);
  721. if (data->abort) {
  722. pr_err("Abort clnt %d waiting on lstnr svc %d, ret %d",
  723. data->client.app_id, lstnr, ret);
  724. rc = -ENODEV;
  725. send_data_rsp.status = QSEOS_RESULT_FAILURE;
  726. } else {
  727. send_data_rsp.status = QSEOS_RESULT_SUCCESS;
  728. }
  729. qseecom.send_resp_flag = 0;
  730. send_data_rsp.qsee_cmd_id = QSEOS_LISTENER_DATA_RSP_COMMAND;
  731. send_data_rsp.listener_id = lstnr ;
  732. if (ptr_svc)
  733. msm_ion_do_cache_op(qseecom.ion_clnt, ptr_svc->ihandle,
  734. ptr_svc->sb_virt, ptr_svc->sb_length,
  735. ION_IOC_CLEAN_INV_CACHES);
  736. if (lstnr == RPMB_SERVICE)
  737. __qseecom_enable_clk(CLK_QSEE);
  738. ret = scm_call(SCM_SVC_TZSCHEDULER, 1,
  739. (const void *)&send_data_rsp,
  740. sizeof(send_data_rsp), resp,
  741. sizeof(*resp));
  742. if (ret) {
  743. pr_err("scm_call() failed with err: %d (app_id = %d)\n",
  744. ret, data->client.app_id);
  745. if (lstnr == RPMB_SERVICE)
  746. __qseecom_disable_clk(CLK_QSEE);
  747. return ret;
  748. }
  749. if ((resp->result != QSEOS_RESULT_SUCCESS) &&
  750. (resp->result != QSEOS_RESULT_INCOMPLETE)) {
  751. pr_err("fail:resp res= %d,app_id = %d,lstr = %d\n",
  752. resp->result, data->client.app_id, lstnr);
  753. ret = -EINVAL;
  754. }
  755. if (lstnr == RPMB_SERVICE)
  756. __qseecom_disable_clk(CLK_QSEE);
  757. }
  758. if (rc)
  759. return rc;
  760. return ret;
  761. }
  762. static int __qseecom_check_app_exists(struct qseecom_check_app_ireq req)
  763. {
  764. int32_t ret;
  765. struct qseecom_command_scm_resp resp;
  766. /* SCM_CALL to check if app_id for the mentioned app exists */
  767. ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req,
  768. sizeof(struct qseecom_check_app_ireq),
  769. &resp, sizeof(resp));
  770. if (ret) {
  771. pr_err("scm_call to check if app is already loaded failed\n");
  772. return -EINVAL;
  773. }
  774. if (resp.result == QSEOS_RESULT_FAILURE) {
  775. return 0;
  776. } else {
  777. switch (resp.resp_type) {
  778. /*qsee returned listener type response */
  779. case QSEOS_LISTENER_ID:
  780. pr_err("resp type is of listener type instead of app");
  781. return -EINVAL;
  782. break;
  783. case QSEOS_APP_ID:
  784. return resp.data;
  785. default:
  786. pr_err("invalid resp type (%d) from qsee",
  787. resp.resp_type);
  788. return -ENODEV;
  789. break;
  790. }
  791. }
  792. }
  793. static int qseecom_load_app(struct qseecom_dev_handle *data, void __user *argp)
  794. {
  795. struct qseecom_registered_app_list *entry = NULL;
  796. unsigned long flags = 0;
  797. u32 app_id = 0;
  798. struct ion_handle *ihandle; /* Ion handle */
  799. struct qseecom_load_img_req load_img_req;
  800. int32_t ret = 0;
  801. ion_phys_addr_t pa = 0;
  802. uint32_t len;
  803. struct qseecom_command_scm_resp resp;
  804. struct qseecom_check_app_ireq req;
  805. struct qseecom_load_app_ireq load_req;
  806. bool first_time = false;
  807. /* Copy the relevant information needed for loading the image */
  808. if (copy_from_user(&load_img_req,
  809. (void __user *)argp,
  810. sizeof(struct qseecom_load_img_req))) {
  811. pr_err("copy_from_user failed\n");
  812. return -EFAULT;
  813. }
  814. if (qseecom.support_bus_scaling) {
  815. mutex_lock(&qsee_bw_mutex);
  816. ret = __qseecom_register_bus_bandwidth_needs(data, MEDIUM);
  817. mutex_unlock(&qsee_bw_mutex);
  818. if (ret)
  819. return ret;
  820. }
  821. /* Vote for the SFPB clock */
  822. ret = __qseecom_enable_clk_scale_up(data);
  823. if (ret)
  824. goto enable_clk_err;
  825. req.qsee_cmd_id = QSEOS_APP_LOOKUP_COMMAND;
  826. load_img_req.img_name[MAX_APP_NAME_SIZE-1] = '\0';
  827. strlcpy(req.app_name, load_img_req.img_name, MAX_APP_NAME_SIZE);
  828. ret = __qseecom_check_app_exists(req);
  829. if (ret < 0)
  830. goto loadapp_err;
  831. app_id = ret;
  832. if (app_id) {
  833. pr_debug("App id %d (%s) already exists\n", app_id,
  834. (char *)(req.app_name));
  835. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
  836. list_for_each_entry(entry,
  837. &qseecom.registered_app_list_head, list){
  838. if (entry->app_id == app_id) {
  839. entry->ref_cnt++;
  840. break;
  841. }
  842. }
  843. spin_unlock_irqrestore(
  844. &qseecom.registered_app_list_lock, flags);
  845. ret = 0;
  846. } else {
  847. first_time = true;
  848. pr_warn("App (%s) does'nt exist, loading apps for first time\n",
  849. (char *)(load_img_req.img_name));
  850. /* Get the handle of the shared fd */
  851. ihandle = ion_import_dma_buf(qseecom.ion_clnt,
  852. load_img_req.ifd_data_fd);
  853. if (IS_ERR_OR_NULL(ihandle)) {
  854. pr_err("Ion client could not retrieve the handle\n");
  855. ret = -ENOMEM;
  856. goto loadapp_err;
  857. }
  858. /* Get the physical address of the ION BUF */
  859. ret = ion_phys(qseecom.ion_clnt, ihandle, &pa, &len);
  860. if (ret) {
  861. pr_err("Cannot get phys_addr for the Ion Client, ret = %d\n",
  862. ret);
  863. goto loadapp_err;
  864. }
  865. /* Populate the structure for sending scm call to load image */
  866. strlcpy(load_req.app_name, load_img_req.img_name,
  867. MAX_APP_NAME_SIZE);
  868. load_req.qsee_cmd_id = QSEOS_APP_START_COMMAND;
  869. load_req.mdt_len = load_img_req.mdt_len;
  870. load_req.img_len = load_img_req.img_len;
  871. load_req.phy_addr = pa;
  872. msm_ion_do_cache_op(qseecom.ion_clnt, ihandle, NULL, len,
  873. ION_IOC_CLEAN_INV_CACHES);
  874. /* SCM_CALL to load the app and get the app_id back */
  875. ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &load_req,
  876. sizeof(struct qseecom_load_app_ireq),
  877. &resp, sizeof(resp));
  878. if (ret) {
  879. pr_err("scm_call to load app failed\n");
  880. if (!IS_ERR_OR_NULL(ihandle))
  881. ion_free(qseecom.ion_clnt, ihandle);
  882. ret = -EINVAL;
  883. goto loadapp_err;
  884. }
  885. if (resp.result == QSEOS_RESULT_FAILURE) {
  886. pr_err("scm_call rsp.result is QSEOS_RESULT_FAILURE\n");
  887. if (!IS_ERR_OR_NULL(ihandle))
  888. ion_free(qseecom.ion_clnt, ihandle);
  889. ret = -EFAULT;
  890. goto loadapp_err;
  891. }
  892. if (resp.result == QSEOS_RESULT_INCOMPLETE) {
  893. ret = __qseecom_process_incomplete_cmd(data, &resp);
  894. if (ret) {
  895. pr_err("process_incomplete_cmd failed err: %d\n",
  896. ret);
  897. if (!IS_ERR_OR_NULL(ihandle))
  898. ion_free(qseecom.ion_clnt, ihandle);
  899. ret = -EFAULT;
  900. goto loadapp_err;
  901. }
  902. }
  903. if (resp.result != QSEOS_RESULT_SUCCESS) {
  904. pr_err("scm_call failed resp.result unknown, %d\n",
  905. resp.result);
  906. if (!IS_ERR_OR_NULL(ihandle))
  907. ion_free(qseecom.ion_clnt, ihandle);
  908. ret = -EFAULT;
  909. goto loadapp_err;
  910. }
  911. app_id = resp.data;
  912. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  913. if (!entry) {
  914. pr_err("kmalloc failed\n");
  915. ret = -ENOMEM;
  916. goto loadapp_err;
  917. }
  918. entry->app_id = app_id;
  919. entry->ref_cnt = 1;
  920. strlcpy(entry->app_name, load_img_req.img_name,
  921. MAX_APP_NAME_SIZE);
  922. /* Deallocate the handle */
  923. if (!IS_ERR_OR_NULL(ihandle))
  924. ion_free(qseecom.ion_clnt, ihandle);
  925. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
  926. list_add_tail(&entry->list, &qseecom.registered_app_list_head);
  927. spin_unlock_irqrestore(&qseecom.registered_app_list_lock,
  928. flags);
  929. pr_warn("App with id %d (%s) now loaded\n", app_id,
  930. (char *)(load_img_req.img_name));
  931. }
  932. data->client.app_id = app_id;
  933. strlcpy(data->client.app_name, load_img_req.img_name,
  934. MAX_APP_NAME_SIZE);
  935. load_img_req.app_id = app_id;
  936. if (copy_to_user(argp, &load_img_req, sizeof(load_img_req))) {
  937. pr_err("copy_to_user failed\n");
  938. ret = -EFAULT;
  939. if (first_time == true) {
  940. spin_lock_irqsave(
  941. &qseecom.registered_app_list_lock, flags);
  942. list_del(&entry->list);
  943. spin_unlock_irqrestore(
  944. &qseecom.registered_app_list_lock, flags);
  945. kzfree(entry);
  946. }
  947. }
  948. loadapp_err:
  949. __qseecom_disable_clk_scale_down(data);
  950. enable_clk_err:
  951. if (qseecom.support_bus_scaling) {
  952. mutex_lock(&qsee_bw_mutex);
  953. qseecom_unregister_bus_bandwidth_needs(data);
  954. mutex_unlock(&qsee_bw_mutex);
  955. }
  956. return ret;
  957. }
  958. static int __qseecom_cleanup_app(struct qseecom_dev_handle *data)
  959. {
  960. wake_up_all(&qseecom.send_resp_wq);
  961. while (atomic_read(&data->ioctl_count) > 1) {
  962. if (wait_event_freezable(data->abort_wq,
  963. atomic_read(&data->ioctl_count) <= 1)) {
  964. pr_err("Interrupted from abort\n");
  965. return -ERESTARTSYS;
  966. break;
  967. }
  968. }
  969. /* Set unload app */
  970. return 1;
  971. }
  972. static int qseecom_unmap_ion_allocated_memory(struct qseecom_dev_handle *data)
  973. {
  974. int ret = 0;
  975. if (!IS_ERR_OR_NULL(data->client.ihandle)) {
  976. ion_unmap_kernel(qseecom.ion_clnt, data->client.ihandle);
  977. ion_free(qseecom.ion_clnt, data->client.ihandle);
  978. memset((void *)&data->client,
  979. 0, sizeof(struct qseecom_client_handle));
  980. }
  981. return ret;
  982. }
  983. static int qseecom_unload_app(struct qseecom_dev_handle *data,
  984. bool app_crash)
  985. {
  986. unsigned long flags;
  987. unsigned long flags1;
  988. int ret = 0;
  989. struct qseecom_command_scm_resp resp;
  990. struct qseecom_registered_app_list *ptr_app = NULL;
  991. bool unload = false;
  992. bool found_app = false;
  993. bool found_dead_app = false;
  994. if (!memcmp(data->client.app_name, "keymaste", strlen("keymaste"))) {
  995. pr_debug("Do not unload keymaster app from tz\n");
  996. goto unload_exit;
  997. }
  998. if (data->client.app_id > 0) {
  999. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
  1000. list_for_each_entry(ptr_app, &qseecom.registered_app_list_head,
  1001. list) {
  1002. if (ptr_app->app_id == data->client.app_id) {
  1003. if (!memcmp((void *)ptr_app->app_name,
  1004. (void *)data->client.app_name,
  1005. strlen(data->client.app_name))) {
  1006. found_app = true;
  1007. if (app_crash || ptr_app->ref_cnt == 1)
  1008. unload = true;
  1009. break;
  1010. } else {
  1011. found_dead_app = true;
  1012. break;
  1013. }
  1014. }
  1015. }
  1016. spin_unlock_irqrestore(&qseecom.registered_app_list_lock,
  1017. flags);
  1018. if (found_app == false && found_dead_app == false) {
  1019. pr_err("Cannot find app with id = %d (%s)\n",
  1020. data->client.app_id,
  1021. (char *)data->client.app_name);
  1022. return -EINVAL;
  1023. }
  1024. }
  1025. if (found_dead_app) {
  1026. pr_warn("cleanup app_id %d(%s)\n", data->client.app_id,
  1027. (char *)data->client.app_name);
  1028. __qseecom_cleanup_app(data);
  1029. }
  1030. if (unload) {
  1031. struct qseecom_unload_app_ireq req;
  1032. /* Populate the structure for sending scm call to load image */
  1033. req.qsee_cmd_id = QSEOS_APP_SHUTDOWN_COMMAND;
  1034. req.app_id = data->client.app_id;
  1035. /* SCM_CALL to unload the app */
  1036. ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req,
  1037. sizeof(struct qseecom_unload_app_ireq),
  1038. &resp, sizeof(resp));
  1039. if (ret) {
  1040. pr_err("scm_call to unload app (id = %d) failed\n",
  1041. req.app_id);
  1042. ret = -EFAULT;
  1043. goto unload_exit;
  1044. } else {
  1045. pr_warn("App id %d now unloaded\n", req.app_id);
  1046. }
  1047. if (resp.result == QSEOS_RESULT_FAILURE) {
  1048. pr_err("app (%d) unload_failed!!\n",
  1049. data->client.app_id);
  1050. ret = -EFAULT;
  1051. goto unload_exit;
  1052. }
  1053. if (resp.result == QSEOS_RESULT_SUCCESS)
  1054. pr_debug("App (%d) is unloaded!!\n",
  1055. data->client.app_id);
  1056. __qseecom_cleanup_app(data);
  1057. if (resp.result == QSEOS_RESULT_INCOMPLETE) {
  1058. ret = __qseecom_process_incomplete_cmd(data, &resp);
  1059. if (ret) {
  1060. pr_err("process_incomplete_cmd fail err: %d\n",
  1061. ret);
  1062. goto unload_exit;
  1063. }
  1064. }
  1065. }
  1066. if (found_app) {
  1067. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags1);
  1068. if (app_crash) {
  1069. ptr_app->ref_cnt = 0;
  1070. pr_debug("app_crash: ref_count = 0\n");
  1071. } else {
  1072. if (ptr_app->ref_cnt == 1) {
  1073. ptr_app->ref_cnt = 0;
  1074. pr_debug("ref_count set to 0\n");
  1075. } else {
  1076. ptr_app->ref_cnt--;
  1077. pr_debug("Can't unload app(%d) inuse\n",
  1078. ptr_app->app_id);
  1079. }
  1080. }
  1081. if (unload) {
  1082. list_del(&ptr_app->list);
  1083. kzfree(ptr_app);
  1084. }
  1085. spin_unlock_irqrestore(&qseecom.registered_app_list_lock,
  1086. flags1);
  1087. }
  1088. unload_exit:
  1089. qseecom_unmap_ion_allocated_memory(data);
  1090. data->released = true;
  1091. return ret;
  1092. }
  1093. static uint32_t __qseecom_uvirt_to_kphys(struct qseecom_dev_handle *data,
  1094. uint32_t virt)
  1095. {
  1096. return data->client.sb_phys + (virt - data->client.user_virt_sb_base);
  1097. }
  1098. static uint32_t __qseecom_uvirt_to_kvirt(struct qseecom_dev_handle *data,
  1099. uint32_t virt)
  1100. {
  1101. return (uint32_t)data->client.sb_virt +
  1102. (virt - data->client.user_virt_sb_base);
  1103. }
  1104. int __qseecom_process_rpmb_svc_cmd(struct qseecom_dev_handle *data_ptr,
  1105. struct qseecom_send_svc_cmd_req *req_ptr,
  1106. struct qseecom_client_send_service_ireq *send_svc_ireq_ptr)
  1107. {
  1108. int ret = 0;
  1109. void *req_buf = NULL;
  1110. if ((req_ptr == NULL) || (send_svc_ireq_ptr == NULL)) {
  1111. pr_err("Error with pointer: req_ptr = %pK, send_svc_ptr = %pK\n",
  1112. req_ptr, send_svc_ireq_ptr);
  1113. return -EINVAL;
  1114. }
  1115. /* Clients need to ensure req_buf is at base offset of shared buffer */
  1116. if ((uint32_t)req_ptr->cmd_req_buf !=
  1117. data_ptr->client.user_virt_sb_base) {
  1118. pr_err("cmd buf not pointing to base offset of shared buffer\n");
  1119. return -EINVAL;
  1120. }
  1121. if (((uint32_t)req_ptr->cmd_req_buf <
  1122. data_ptr->client.user_virt_sb_base)
  1123. || ((uint32_t)req_ptr->cmd_req_buf >=
  1124. (data_ptr->client.user_virt_sb_base +
  1125. data_ptr->client.sb_length))) {
  1126. pr_err("cmd buffer address not within shared bufffer\n");
  1127. return -EINVAL;
  1128. }
  1129. if (data_ptr->client.sb_length <
  1130. sizeof(struct qseecom_rpmb_provision_key)) {
  1131. pr_err("shared buffer is too small to hold key type\n");
  1132. return -EINVAL;
  1133. }
  1134. req_buf = data_ptr->client.sb_virt;
  1135. send_svc_ireq_ptr->qsee_cmd_id = req_ptr->cmd_id;
  1136. send_svc_ireq_ptr->key_type =
  1137. ((struct qseecom_rpmb_provision_key *)req_buf)->key_type;
  1138. send_svc_ireq_ptr->req_len = req_ptr->cmd_req_len;
  1139. send_svc_ireq_ptr->rsp_ptr = (void *)(__qseecom_uvirt_to_kphys(data_ptr,
  1140. (uint32_t)req_ptr->resp_buf));
  1141. send_svc_ireq_ptr->rsp_len = req_ptr->resp_len;
  1142. return ret;
  1143. }
  1144. static int __validate_send_service_cmd_inputs(struct qseecom_dev_handle *data,
  1145. struct qseecom_send_svc_cmd_req *req)
  1146. {
  1147. if (!req || !req->resp_buf || !req->cmd_req_buf) {
  1148. pr_err("req or cmd buffer or response buffer is null\n");
  1149. return -EINVAL;
  1150. }
  1151. if (data->client.sb_virt == NULL) {
  1152. pr_err("sb_virt null\n");
  1153. return -EINVAL;
  1154. }
  1155. if (data->client.user_virt_sb_base == 0) {
  1156. pr_err("user_virt_sb_base is null\n");
  1157. return -EINVAL;
  1158. }
  1159. if (data->client.sb_length == 0) {
  1160. pr_err("sb_length is 0\n");
  1161. return -EINVAL;
  1162. }
  1163. if (((uintptr_t)req->cmd_req_buf <
  1164. data->client.user_virt_sb_base) ||
  1165. ((uintptr_t)req->cmd_req_buf >=
  1166. (data->client.user_virt_sb_base + data->client.sb_length))) {
  1167. pr_err("cmd buffer address not within shared bufffer\n");
  1168. return -EINVAL;
  1169. }
  1170. if (((uintptr_t)req->resp_buf <
  1171. data->client.user_virt_sb_base) ||
  1172. ((uintptr_t)req->resp_buf >=
  1173. (data->client.user_virt_sb_base + data->client.sb_length))) {
  1174. pr_err("response buffer address not within shared bufffer\n");
  1175. return -EINVAL;
  1176. }
  1177. if ((req->cmd_req_len == 0) || (req->resp_len == 0) ||
  1178. (req->cmd_req_len > data->client.sb_length) ||
  1179. (req->resp_len > data->client.sb_length)) {
  1180. pr_err("cmd buf length or response buf length not valid\n");
  1181. return -EINVAL;
  1182. }
  1183. if (req->cmd_req_len > UINT_MAX - req->resp_len) {
  1184. pr_err("Integer overflow detected in req_len & rsp_len\n");
  1185. return -EINVAL;
  1186. }
  1187. if ((req->cmd_req_len + req->resp_len) > data->client.sb_length) {
  1188. pr_debug("Not enough memory to fit cmd_buf.\n");
  1189. pr_debug("resp_buf. Required: %u, Available: %zu\n",
  1190. (req->cmd_req_len + req->resp_len),
  1191. data->client.sb_length);
  1192. return -ENOMEM;
  1193. }
  1194. if ((uintptr_t)req->cmd_req_buf > (ULONG_MAX - req->cmd_req_len)) {
  1195. pr_err("Integer overflow in req_len & cmd_req_buf\n");
  1196. return -EINVAL;
  1197. }
  1198. if ((uintptr_t)req->resp_buf > (ULONG_MAX - req->resp_len)) {
  1199. pr_err("Integer overflow in resp_len & resp_buf\n");
  1200. return -EINVAL;
  1201. }
  1202. if (data->client.user_virt_sb_base >
  1203. (ULONG_MAX - data->client.sb_length)) {
  1204. pr_err("Integer overflow in user_virt_sb_base & sb_length\n");
  1205. return -EINVAL;
  1206. }
  1207. if ((((uintptr_t)req->cmd_req_buf + req->cmd_req_len) >
  1208. ((uintptr_t)data->client.user_virt_sb_base +
  1209. data->client.sb_length)) ||
  1210. (((uintptr_t)req->resp_buf + req->resp_len) >
  1211. ((uintptr_t)data->client.user_virt_sb_base +
  1212. data->client.sb_length))) {
  1213. pr_err("cmd buf or resp buf is out of shared buffer region\n");
  1214. return -EINVAL;
  1215. }
  1216. return 0;
  1217. }
  1218. static int qseecom_send_service_cmd(struct qseecom_dev_handle *data,
  1219. void __user *argp)
  1220. {
  1221. int ret = 0;
  1222. struct qseecom_client_send_service_ireq send_svc_ireq;
  1223. struct qseecom_command_scm_resp resp;
  1224. struct qseecom_send_svc_cmd_req req;
  1225. /*struct qseecom_command_scm_resp resp;*/
  1226. if (copy_from_user(&req,
  1227. (void __user *)argp,
  1228. sizeof(req))) {
  1229. pr_err("copy_from_user failed\n");
  1230. return -EFAULT;
  1231. }
  1232. if (__validate_send_service_cmd_inputs(data, &req))
  1233. return -EINVAL;
  1234. data->type = QSEECOM_SECURE_SERVICE;
  1235. switch (req.cmd_id) {
  1236. case QSEOS_RPMB_PROVISION_KEY_COMMAND:
  1237. case QSEOS_RPMB_ERASE_COMMAND:
  1238. if (__qseecom_process_rpmb_svc_cmd(data, &req,
  1239. &send_svc_ireq))
  1240. return -EINVAL;
  1241. break;
  1242. default:
  1243. pr_err("Unsupported cmd_id %d\n", req.cmd_id);
  1244. return -EINVAL;
  1245. }
  1246. if (qseecom.support_bus_scaling) {
  1247. ret = qseecom_scale_bus_bandwidth_timer(HIGH);
  1248. if (ret) {
  1249. pr_err("Fail to set bw HIGH\n");
  1250. return ret;
  1251. }
  1252. } else {
  1253. ret = qseecom_perf_enable(data);
  1254. if (ret) {
  1255. pr_err("Failed to vote for clocks with err %d\n", ret);
  1256. goto exit;
  1257. }
  1258. }
  1259. msm_ion_do_cache_op(qseecom.ion_clnt, data->client.ihandle,
  1260. data->client.sb_virt, data->client.sb_length,
  1261. ION_IOC_CLEAN_INV_CACHES);
  1262. ret = scm_call(SCM_SVC_TZSCHEDULER, 1, (const void *) &send_svc_ireq,
  1263. sizeof(send_svc_ireq),
  1264. &resp, sizeof(resp));
  1265. msm_ion_do_cache_op(qseecom.ion_clnt, data->client.ihandle,
  1266. data->client.sb_virt, data->client.sb_length,
  1267. ION_IOC_INV_CACHES);
  1268. if (ret) {
  1269. pr_err("qseecom_scm_call failed with err: %d\n", ret);
  1270. if (!qseecom.support_bus_scaling) {
  1271. qsee_disable_clock_vote(data, CLK_DFAB);
  1272. qsee_disable_clock_vote(data, CLK_SFPB);
  1273. } else {
  1274. __qseecom_add_bw_scale_down_timer(
  1275. QSEECOM_SEND_CMD_CRYPTO_TIMEOUT);
  1276. }
  1277. goto exit;
  1278. }
  1279. switch (resp.result) {
  1280. case QSEOS_RESULT_SUCCESS:
  1281. break;
  1282. case QSEOS_RESULT_INCOMPLETE:
  1283. pr_err("qseos_result_incomplete\n");
  1284. ret = __qseecom_process_incomplete_cmd(data, &resp);
  1285. if (ret) {
  1286. pr_err("process_incomplete_cmd fail: err: %d\n",
  1287. ret);
  1288. }
  1289. break;
  1290. case QSEOS_RESULT_FAILURE:
  1291. pr_err("process_incomplete_cmd failed err: %d\n", ret);
  1292. break;
  1293. default:
  1294. pr_err("Response result %d not supported\n",
  1295. resp.result);
  1296. ret = -EINVAL;
  1297. break;
  1298. }
  1299. if (!qseecom.support_bus_scaling) {
  1300. qsee_disable_clock_vote(data, CLK_DFAB);
  1301. qsee_disable_clock_vote(data, CLK_SFPB);
  1302. } else {
  1303. __qseecom_add_bw_scale_down_timer(
  1304. QSEECOM_SEND_CMD_CRYPTO_TIMEOUT);
  1305. }
  1306. exit:
  1307. return ret;
  1308. }
  1309. static int __validate_send_cmd_inputs(struct qseecom_dev_handle *data,
  1310. struct qseecom_send_cmd_req *req)
  1311. {
  1312. if (!data || !data->client.ihandle) {
  1313. pr_err("Client or client handle is not initialized\n");
  1314. return -EINVAL;
  1315. }
  1316. if (((req->resp_buf == NULL) && (req->resp_len != 0)) ||
  1317. (req->cmd_req_buf == NULL)) {
  1318. pr_err("cmd buffer or response buffer is null\n");
  1319. return -EINVAL;
  1320. }
  1321. if (((uint32_t)req->cmd_req_buf < data->client.user_virt_sb_base) ||
  1322. ((uint32_t)req->cmd_req_buf >= (data->client.user_virt_sb_base +
  1323. data->client.sb_length))) {
  1324. pr_err("cmd buffer address not within shared bufffer\n");
  1325. return -EINVAL;
  1326. }
  1327. if (((uintptr_t)req->resp_buf <
  1328. data->client.user_virt_sb_base) ||
  1329. ((uintptr_t)req->resp_buf >=
  1330. (data->client.user_virt_sb_base + data->client.sb_length))) {
  1331. pr_err("response buffer address not within shared bufffer\n");
  1332. return -EINVAL;
  1333. }
  1334. if ((req->cmd_req_len == 0) ||
  1335. (req->cmd_req_len > data->client.sb_length) ||
  1336. (req->resp_len > data->client.sb_length)) {
  1337. pr_err("cmd buf length or response buf length not valid\n");
  1338. return -EINVAL;
  1339. }
  1340. if (req->cmd_req_len > UINT_MAX - req->resp_len) {
  1341. pr_err("Integer overflow detected in req_len & rsp_len\n");
  1342. return -EINVAL;
  1343. }
  1344. if ((req->cmd_req_len + req->resp_len) > data->client.sb_length) {
  1345. pr_debug("Not enough memory to fit cmd_buf.\n");
  1346. pr_debug("resp_buf. Required: %u, Available: %zu\n",
  1347. (req->cmd_req_len + req->resp_len),
  1348. data->client.sb_length);
  1349. return -ENOMEM;
  1350. }
  1351. if ((uintptr_t)req->cmd_req_buf > (ULONG_MAX - req->cmd_req_len)) {
  1352. pr_err("Integer overflow in req_len & cmd_req_buf\n");
  1353. return -EINVAL;
  1354. }
  1355. if ((uintptr_t)req->resp_buf > (ULONG_MAX - req->resp_len)) {
  1356. pr_err("Integer overflow in resp_len & resp_buf\n");
  1357. return -EINVAL;
  1358. }
  1359. if (data->client.user_virt_sb_base >
  1360. (ULONG_MAX - data->client.sb_length)) {
  1361. pr_err("Integer overflow in user_virt_sb_base & sb_length\n");
  1362. return -EINVAL;
  1363. }
  1364. if ((((uintptr_t)req->cmd_req_buf + req->cmd_req_len) >
  1365. ((uintptr_t)data->client.user_virt_sb_base +
  1366. data->client.sb_length)) ||
  1367. (((uintptr_t)req->resp_buf + req->resp_len) >
  1368. ((uintptr_t)data->client.user_virt_sb_base +
  1369. data->client.sb_length))) {
  1370. pr_err("cmd buf or resp buf is out of shared buffer region\n");
  1371. return -EINVAL;
  1372. }
  1373. return 0;
  1374. }
  1375. static int __qseecom_send_cmd(struct qseecom_dev_handle *data,
  1376. struct qseecom_send_cmd_req *req)
  1377. {
  1378. int ret = 0;
  1379. u32 reqd_len_sb_in = 0;
  1380. struct qseecom_client_send_data_ireq send_data_req;
  1381. struct qseecom_command_scm_resp resp;
  1382. unsigned long flags;
  1383. struct qseecom_registered_app_list *ptr_app;
  1384. bool found_app = false;
  1385. int name_len = 0;
  1386. reqd_len_sb_in = req->cmd_req_len + req->resp_len;
  1387. /* find app_id & img_name from list */
  1388. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
  1389. list_for_each_entry(ptr_app, &qseecom.registered_app_list_head,
  1390. list) {
  1391. name_len = min(strlen(data->client.app_name),
  1392. strlen(ptr_app->app_name));
  1393. if ((ptr_app->app_id == data->client.app_id) &&
  1394. (!memcmp(ptr_app->app_name,
  1395. data->client.app_name, name_len))) {
  1396. found_app = true;
  1397. break;
  1398. }
  1399. }
  1400. spin_unlock_irqrestore(&qseecom.registered_app_list_lock, flags);
  1401. if (!found_app) {
  1402. pr_err("app_id %d (%s) is not found\n", data->client.app_id,
  1403. (char *)data->client.app_name);
  1404. return -EINVAL;
  1405. }
  1406. if (req->cmd_req_buf == NULL || req->resp_buf == NULL) {
  1407. pr_err("cmd buffer or response buffer is null\n");
  1408. return -EINVAL;
  1409. }
  1410. if (((uint32_t)req->cmd_req_buf < data->client.user_virt_sb_base) ||
  1411. ((uint32_t)req->cmd_req_buf >= (data->client.user_virt_sb_base +
  1412. data->client.sb_length))) {
  1413. pr_err("cmd buffer address not within shared bufffer\n");
  1414. return -EINVAL;
  1415. }
  1416. if (((uint32_t)req->resp_buf < data->client.user_virt_sb_base) ||
  1417. ((uint32_t)req->resp_buf >= (data->client.user_virt_sb_base +
  1418. data->client.sb_length))){
  1419. pr_err("response buffer address not within shared bufffer\n");
  1420. return -EINVAL;
  1421. }
  1422. if ((req->cmd_req_len == 0) || (req->resp_len == 0) ||
  1423. req->cmd_req_len > data->client.sb_length ||
  1424. req->resp_len > data->client.sb_length) {
  1425. pr_err("cmd buffer length or "
  1426. "response buffer length not valid\n");
  1427. return -EINVAL;
  1428. }
  1429. if (req->cmd_req_len > UINT_MAX - req->resp_len) {
  1430. pr_err("Integer overflow detected in req_len & rsp_len, exiting now\n");
  1431. return -EINVAL;
  1432. }
  1433. send_data_req.qsee_cmd_id = QSEOS_CLIENT_SEND_DATA_COMMAND;
  1434. send_data_req.app_id = data->client.app_id;
  1435. send_data_req.req_ptr = (void *)(__qseecom_uvirt_to_kphys(data,
  1436. (uint32_t)req->cmd_req_buf));
  1437. send_data_req.req_len = req->cmd_req_len;
  1438. send_data_req.rsp_ptr = (void *)(__qseecom_uvirt_to_kphys(data,
  1439. (uint32_t)req->resp_buf));
  1440. send_data_req.rsp_len = req->resp_len;
  1441. msm_ion_do_cache_op(qseecom.ion_clnt, data->client.ihandle,
  1442. data->client.sb_virt,
  1443. reqd_len_sb_in,
  1444. ION_IOC_CLEAN_INV_CACHES);
  1445. ret = scm_call(SCM_SVC_TZSCHEDULER, 1, (const void *) &send_data_req,
  1446. sizeof(send_data_req),
  1447. &resp, sizeof(resp));
  1448. if (ret) {
  1449. pr_err("scm_call() failed with err: %d (app_id = %d)\n",
  1450. ret, data->client.app_id);
  1451. return ret;
  1452. }
  1453. if (resp.result == QSEOS_RESULT_INCOMPLETE) {
  1454. ret = __qseecom_process_incomplete_cmd(data, &resp);
  1455. if (ret) {
  1456. pr_err("process_incomplete_cmd failed err: %d\n", ret);
  1457. return ret;
  1458. }
  1459. } else {
  1460. if (resp.result != QSEOS_RESULT_SUCCESS) {
  1461. pr_err("Response result %d not supported\n",
  1462. resp.result);
  1463. ret = -EINVAL;
  1464. }
  1465. }
  1466. msm_ion_do_cache_op(qseecom.ion_clnt, data->client.ihandle,
  1467. data->client.sb_virt, data->client.sb_length,
  1468. ION_IOC_INV_CACHES);
  1469. return ret;
  1470. }
  1471. static int qseecom_send_cmd(struct qseecom_dev_handle *data, void __user *argp)
  1472. {
  1473. int ret = 0;
  1474. struct qseecom_send_cmd_req req;
  1475. ret = copy_from_user(&req, argp, sizeof(req));
  1476. if (ret) {
  1477. pr_err("copy_from_user failed\n");
  1478. return ret;
  1479. }
  1480. if (__validate_send_cmd_inputs(data, &req))
  1481. return -EINVAL;
  1482. ret = __qseecom_send_cmd(data, &req);
  1483. if (ret)
  1484. return ret;
  1485. return ret;
  1486. }
  1487. int __boundary_checks_offset(struct qseecom_send_modfd_cmd_req *req,
  1488. struct qseecom_send_modfd_listener_resp *lstnr_resp,
  1489. struct qseecom_dev_handle *data, bool qteec,
  1490. int i) {
  1491. if ((data->type != QSEECOM_LISTENER_SERVICE) &&
  1492. (req->ifd_data[i].fd > 0)) {
  1493. if (qteec) {
  1494. if ((req->cmd_req_len < (TWO * sizeof(uint32_t))) ||
  1495. (req->ifd_data[i].cmd_buf_offset >
  1496. req->cmd_req_len - (TWO * sizeof(uint32_t)))) {
  1497. pr_err("Invalid offset (QTEEC req len) 0x%x\n",
  1498. req->ifd_data[i].cmd_buf_offset);
  1499. return -EINVAL;
  1500. }
  1501. } else {
  1502. if ((req->cmd_req_len < sizeof(uint32_t)) ||
  1503. (req->ifd_data[i].cmd_buf_offset >
  1504. req->cmd_req_len - sizeof(uint32_t))) {
  1505. pr_err("Invalid offset (req len) 0x%x\n",
  1506. req->ifd_data[i].cmd_buf_offset);
  1507. return -EINVAL;
  1508. }
  1509. }
  1510. } else if ((data->type == QSEECOM_LISTENER_SERVICE) &&
  1511. (lstnr_resp->ifd_data[i].fd > 0)) {
  1512. if (qteec) {
  1513. if ((lstnr_resp->resp_len < TWO * sizeof(uint32_t)) ||
  1514. (lstnr_resp->ifd_data[i].cmd_buf_offset >
  1515. lstnr_resp->resp_len - TWO*sizeof(uint32_t))) {
  1516. pr_err("Invalid offset (QTEEC resp len) 0x%x\n",
  1517. lstnr_resp->ifd_data[i].cmd_buf_offset);
  1518. return -EINVAL;
  1519. }
  1520. } else {
  1521. if ((lstnr_resp->resp_len < sizeof(uint32_t)) ||
  1522. (lstnr_resp->ifd_data[i].cmd_buf_offset >
  1523. lstnr_resp->resp_len - sizeof(uint32_t))) {
  1524. pr_err("Invalid offset (lstnr resp len) 0x%x\n",
  1525. lstnr_resp->ifd_data[i].cmd_buf_offset);
  1526. return -EINVAL;
  1527. }
  1528. }
  1529. }
  1530. return 0;
  1531. }
  1532. #define SG_ENTRY_SZ sizeof(struct qseecom_sg_entry)
  1533. static int __qseecom_update_cmd_buf(void *msg, bool cleanup,
  1534. struct qseecom_dev_handle *data,
  1535. bool listener_svc)
  1536. {
  1537. struct ion_handle *ihandle;
  1538. char *field;
  1539. int ret = 0;
  1540. int i = 0;
  1541. uint32_t len = 0;
  1542. struct scatterlist *sg;
  1543. struct qseecom_send_modfd_cmd_req *cmd_req = NULL;
  1544. struct qseecom_send_modfd_listener_resp *lstnr_resp = NULL;
  1545. struct qseecom_registered_listener_list *this_lstnr = NULL;
  1546. if (msg == NULL) {
  1547. pr_err("Invalid address\n");
  1548. return -EINVAL;
  1549. }
  1550. if (listener_svc) {
  1551. lstnr_resp = (struct qseecom_send_modfd_listener_resp *)msg;
  1552. this_lstnr = __qseecom_find_svc(data->listener.id);
  1553. if (IS_ERR_OR_NULL(this_lstnr)) {
  1554. pr_err("Invalid listener ID\n");
  1555. return -ENOMEM;
  1556. }
  1557. } else {
  1558. cmd_req = (struct qseecom_send_modfd_cmd_req *)msg;
  1559. }
  1560. for (i = 0; i < MAX_ION_FD; i++) {
  1561. struct sg_table *sg_ptr = NULL;
  1562. if ((!listener_svc) && (cmd_req->ifd_data[i].fd > 0)) {
  1563. ihandle = ion_import_dma_buf(qseecom.ion_clnt,
  1564. cmd_req->ifd_data[i].fd);
  1565. if (IS_ERR_OR_NULL(ihandle)) {
  1566. pr_err("Ion client can't retrieve the handle\n");
  1567. return -ENOMEM;
  1568. }
  1569. field = (char *) cmd_req->cmd_req_buf +
  1570. cmd_req->ifd_data[i].cmd_buf_offset;
  1571. } else if ((listener_svc) &&
  1572. (lstnr_resp->ifd_data[i].fd > 0)) {
  1573. ihandle = ion_import_dma_buf(qseecom.ion_clnt,
  1574. lstnr_resp->ifd_data[i].fd);
  1575. if (IS_ERR_OR_NULL(ihandle)) {
  1576. pr_err("Ion client can't retrieve the handle\n");
  1577. return -ENOMEM;
  1578. }
  1579. field = lstnr_resp->resp_buf_ptr +
  1580. lstnr_resp->ifd_data[i].cmd_buf_offset;
  1581. } else {
  1582. return ret;
  1583. }
  1584. /* Populate the cmd data structure with the phys_addr */
  1585. sg_ptr = ion_sg_table(qseecom.ion_clnt, ihandle);
  1586. if (sg_ptr == NULL) {
  1587. pr_err("IOn client could not retrieve sg table\n");
  1588. goto err;
  1589. }
  1590. if (sg_ptr->nents == 0) {
  1591. pr_err("Num of scattered entries is 0\n");
  1592. goto err;
  1593. }
  1594. if (sg_ptr->nents > QSEECOM_MAX_SG_ENTRY) {
  1595. pr_err("Num of scattered entries");
  1596. pr_err(" (%d) is greater than max supported %d\n",
  1597. sg_ptr->nents, QSEECOM_MAX_SG_ENTRY);
  1598. goto err;
  1599. }
  1600. sg = sg_ptr->sgl;
  1601. if (sg_ptr->nents == 1) {
  1602. uint32_t *update;
  1603. update = (uint32_t *) field;
  1604. if (__boundary_checks_offset(cmd_req, lstnr_resp, data,
  1605. false, i))
  1606. goto err;
  1607. if (cleanup)
  1608. *update = 0;
  1609. else
  1610. *update = (uint32_t)sg_dma_address(
  1611. sg_ptr->sgl);
  1612. len += (uint32_t)sg->length;
  1613. } else {
  1614. struct qseecom_sg_entry *update;
  1615. int j = 0;
  1616. if ((data->type != QSEECOM_LISTENER_SERVICE) &&
  1617. (cmd_req->ifd_data[i].fd > 0)) {
  1618. if ((cmd_req->cmd_req_len <
  1619. SG_ENTRY_SZ * sg_ptr->nents) ||
  1620. (cmd_req->ifd_data[i].cmd_buf_offset >
  1621. (cmd_req->cmd_req_len -
  1622. SG_ENTRY_SZ * sg_ptr->nents))) {
  1623. pr_err("Invalid offset = 0x%x\n",
  1624. cmd_req->ifd_data[i].
  1625. cmd_buf_offset);
  1626. goto err;
  1627. }
  1628. } else if ((data->type == QSEECOM_LISTENER_SERVICE) &&
  1629. (lstnr_resp->ifd_data[i].fd > 0)) {
  1630. if ((lstnr_resp->resp_len <
  1631. SG_ENTRY_SZ * sg_ptr->nents) ||
  1632. (lstnr_resp->ifd_data[i].cmd_buf_offset >
  1633. (lstnr_resp->resp_len -
  1634. SG_ENTRY_SZ * sg_ptr->nents))) {
  1635. pr_err("Invalid offset = 0x%x\n",
  1636. lstnr_resp->ifd_data[i].
  1637. cmd_buf_offset);
  1638. goto err;
  1639. }
  1640. }
  1641. update = (struct qseecom_sg_entry *) field;
  1642. for (j = 0; j < sg_ptr->nents; j++) {
  1643. if (cleanup) {
  1644. update->phys_addr = 0;
  1645. update->len = 0;
  1646. } else {
  1647. update->phys_addr = (uint32_t)
  1648. sg_dma_address(sg);
  1649. update->len = sg->length;
  1650. }
  1651. len += sg->length;
  1652. update++;
  1653. sg = sg_next(sg);
  1654. }
  1655. }
  1656. if (cleanup)
  1657. msm_ion_do_cache_op(qseecom.ion_clnt,
  1658. ihandle, NULL, len,
  1659. ION_IOC_INV_CACHES);
  1660. else
  1661. msm_ion_do_cache_op(qseecom.ion_clnt,
  1662. ihandle, NULL, len,
  1663. ION_IOC_CLEAN_INV_CACHES);
  1664. /* Deallocate the handle */
  1665. if (!IS_ERR_OR_NULL(ihandle))
  1666. ion_free(qseecom.ion_clnt, ihandle);
  1667. }
  1668. return ret;
  1669. err:
  1670. if (!IS_ERR_OR_NULL(ihandle))
  1671. ion_free(qseecom.ion_clnt, ihandle);
  1672. return -ENOMEM;
  1673. }
  1674. static int qseecom_send_modfd_cmd(struct qseecom_dev_handle *data,
  1675. void __user *argp)
  1676. {
  1677. int ret = 0;
  1678. int i;
  1679. struct qseecom_send_modfd_cmd_req req;
  1680. struct qseecom_send_cmd_req send_cmd_req;
  1681. ret = copy_from_user(&req, argp, sizeof(req));
  1682. if (ret) {
  1683. pr_err("copy_from_user failed\n");
  1684. return ret;
  1685. }
  1686. if (req.cmd_req_len == 0 || req.cmd_req_len > data->client.sb_length ||
  1687. req.resp_len > data->client.sb_length) {
  1688. pr_err("cmd or response buffer length not valid\n");
  1689. return -EINVAL;
  1690. }
  1691. if (req.cmd_req_buf == NULL || req.resp_buf == NULL) {
  1692. pr_err("cmd buffer or response buffer is null\n");
  1693. return -EINVAL;
  1694. }
  1695. if (((uint32_t)req.cmd_req_buf < data->client.user_virt_sb_base) ||
  1696. ((uint32_t)req.cmd_req_buf >= (data->client.user_virt_sb_base +
  1697. data->client.sb_length))) {
  1698. pr_err("cmd buffer address not within shared bufffer\n");
  1699. return -EINVAL;
  1700. }
  1701. if (((uint32_t)req.resp_buf < data->client.user_virt_sb_base) ||
  1702. ((uint32_t)req.resp_buf >= (data->client.user_virt_sb_base +
  1703. data->client.sb_length))){
  1704. pr_err("response buffer address not within shared bufffer\n");
  1705. return -EINVAL;
  1706. }
  1707. send_cmd_req.cmd_req_buf = req.cmd_req_buf;
  1708. send_cmd_req.cmd_req_len = req.cmd_req_len;
  1709. send_cmd_req.resp_buf = req.resp_buf;
  1710. send_cmd_req.resp_len = req.resp_len;
  1711. if (__validate_send_cmd_inputs(data, &send_cmd_req))
  1712. return -EINVAL;
  1713. /* validate offsets */
  1714. for (i = 0; i < MAX_ION_FD; i++) {
  1715. if (req.ifd_data[i].cmd_buf_offset >= req.cmd_req_len) {
  1716. pr_err("Invalid offset %d = 0x%x\n",
  1717. i, req.ifd_data[i].cmd_buf_offset);
  1718. return -EINVAL;
  1719. }
  1720. }
  1721. req.cmd_req_buf = (void *)__qseecom_uvirt_to_kvirt(data,
  1722. (uint32_t)req.cmd_req_buf);
  1723. req.resp_buf = (void *)__qseecom_uvirt_to_kvirt(data,
  1724. (uint32_t)req.resp_buf);
  1725. ret = __qseecom_update_cmd_buf(&req, false, data, false);
  1726. if (ret)
  1727. return ret;
  1728. ret = __qseecom_send_cmd(data, &send_cmd_req);
  1729. if (ret)
  1730. return ret;
  1731. ret = __qseecom_update_cmd_buf(&req, true, data, false);
  1732. if (ret)
  1733. return ret;
  1734. return ret;
  1735. }
  1736. static int __qseecom_listener_has_rcvd_req(struct qseecom_dev_handle *data,
  1737. struct qseecom_registered_listener_list *svc)
  1738. {
  1739. int ret;
  1740. ret = (svc->rcv_req_flag != 0);
  1741. return ret || data->abort;
  1742. }
  1743. static int qseecom_receive_req(struct qseecom_dev_handle *data)
  1744. {
  1745. int ret = 0;
  1746. struct qseecom_registered_listener_list *this_lstnr;
  1747. this_lstnr = __qseecom_find_svc(data->listener.id);
  1748. if (!this_lstnr) {
  1749. pr_err("Invalid listener ID\n");
  1750. return -ENODATA;
  1751. }
  1752. while (1) {
  1753. if (wait_event_freezable(this_lstnr->rcv_req_wq,
  1754. __qseecom_listener_has_rcvd_req(data,
  1755. this_lstnr))) {
  1756. pr_debug("Interrupted: exiting Listener Service = %d\n",
  1757. (uint32_t)data->listener.id);
  1758. /* woken up for different reason */
  1759. return -ERESTARTSYS;
  1760. }
  1761. if (data->abort) {
  1762. pr_err("Aborting Listener Service = %d\n",
  1763. (uint32_t)data->listener.id);
  1764. return -ENODEV;
  1765. }
  1766. this_lstnr->rcv_req_flag = 0;
  1767. break;
  1768. }
  1769. return ret;
  1770. }
  1771. static bool __qseecom_is_fw_image_valid(const struct firmware *fw_entry)
  1772. {
  1773. struct elf32_hdr *ehdr;
  1774. if (fw_entry->size < sizeof(*ehdr)) {
  1775. pr_err("%s: Not big enough to be an elf header\n",
  1776. qseecom.pdev->init_name);
  1777. return false;
  1778. }
  1779. ehdr = (struct elf32_hdr *)fw_entry->data;
  1780. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
  1781. pr_err("%s: Not an elf header\n",
  1782. qseecom.pdev->init_name);
  1783. return false;
  1784. }
  1785. if (ehdr->e_phnum == 0) {
  1786. pr_err("%s: No loadable segments\n",
  1787. qseecom.pdev->init_name);
  1788. return false;
  1789. }
  1790. if (sizeof(struct elf32_phdr) * ehdr->e_phnum +
  1791. sizeof(struct elf32_hdr) > fw_entry->size) {
  1792. pr_err("%s: Program headers not within mdt\n",
  1793. qseecom.pdev->init_name);
  1794. return false;
  1795. }
  1796. return true;
  1797. }
  1798. static int __qseecom_get_fw_size(const char *appname, uint32_t *fw_size)
  1799. {
  1800. int ret = -1;
  1801. int i = 0, rc = 0;
  1802. const struct firmware *fw_entry = NULL;
  1803. struct elf32_phdr *phdr;
  1804. char fw_name[MAX_APP_NAME_SIZE];
  1805. struct elf32_hdr *ehdr;
  1806. int num_images = 0;
  1807. snprintf(fw_name, sizeof(fw_name), "%s.mdt", appname);
  1808. rc = request_firmware(&fw_entry, fw_name, qseecom.pdev);
  1809. if (rc) {
  1810. pr_err("error with request_firmware\n");
  1811. ret = -EIO;
  1812. goto err;
  1813. }
  1814. if (!__qseecom_is_fw_image_valid(fw_entry)) {
  1815. ret = -EIO;
  1816. goto err;
  1817. }
  1818. *fw_size = fw_entry->size;
  1819. phdr = (struct elf32_phdr *)(fw_entry->data + sizeof(struct elf32_hdr));
  1820. ehdr = (struct elf32_hdr *)fw_entry->data;
  1821. num_images = ehdr->e_phnum;
  1822. release_firmware(fw_entry);
  1823. fw_entry = NULL;
  1824. for (i = 0; i < num_images; i++, phdr++) {
  1825. memset(fw_name, 0, sizeof(fw_name));
  1826. snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d", appname, i);
  1827. ret = request_firmware(&fw_entry, fw_name, qseecom.pdev);
  1828. if (ret)
  1829. goto err;
  1830. if (*fw_size > U32_MAX - fw_entry->size) {
  1831. pr_err("QSEE %s app file size overflow\n", appname);
  1832. ret = -EINVAL;
  1833. goto err;
  1834. }
  1835. *fw_size += fw_entry->size;
  1836. release_firmware(fw_entry);
  1837. fw_entry = NULL;
  1838. }
  1839. return ret;
  1840. err:
  1841. if (fw_entry)
  1842. release_firmware(fw_entry);
  1843. *fw_size = 0;
  1844. return ret;
  1845. }
  1846. static int __qseecom_get_fw_data(const char *appname, u8 *img_data,
  1847. uint32_t fw_size,
  1848. struct qseecom_load_app_ireq *load_req)
  1849. {
  1850. int ret = -1;
  1851. int i = 0, rc = 0;
  1852. const struct firmware *fw_entry = NULL;
  1853. char fw_name[MAX_APP_NAME_SIZE];
  1854. u8 *img_data_ptr = img_data;
  1855. struct elf32_hdr *ehdr;
  1856. int num_images = 0;
  1857. snprintf(fw_name, sizeof(fw_name), "%s.mdt", appname);
  1858. rc = request_firmware(&fw_entry, fw_name, qseecom.pdev);
  1859. if (rc) {
  1860. ret = -EIO;
  1861. goto err;
  1862. }
  1863. load_req->img_len = fw_entry->size;
  1864. if (load_req->img_len > fw_size) {
  1865. pr_err("app %s size %zu is larger than buf size %u\n",
  1866. appname, fw_entry->size, fw_size);
  1867. ret = -EINVAL;
  1868. goto err;
  1869. }
  1870. memcpy(img_data_ptr, fw_entry->data, fw_entry->size);
  1871. img_data_ptr = img_data_ptr + fw_entry->size;
  1872. load_req->mdt_len = fw_entry->size; /*Get MDT LEN*/
  1873. ehdr = (struct elf32_hdr *)fw_entry->data;
  1874. num_images = ehdr->e_phnum;
  1875. release_firmware(fw_entry);
  1876. fw_entry = NULL;
  1877. for (i = 0; i < num_images; i++) {
  1878. snprintf(fw_name, ARRAY_SIZE(fw_name), "%s.b%02d", appname, i);
  1879. ret = request_firmware(&fw_entry, fw_name, qseecom.pdev);
  1880. if (ret) {
  1881. pr_err("Failed to locate blob %s\n", fw_name);
  1882. goto err;
  1883. }
  1884. if ((fw_entry->size > U32_MAX - load_req->img_len) ||
  1885. (fw_entry->size + load_req->img_len > fw_size)) {
  1886. pr_err("Invalid file size for %s\n", fw_name);
  1887. ret = -EINVAL;
  1888. goto err;
  1889. }
  1890. memcpy(img_data_ptr, fw_entry->data, fw_entry->size);
  1891. img_data_ptr = img_data_ptr + fw_entry->size;
  1892. load_req->img_len += fw_entry->size;
  1893. release_firmware(fw_entry);
  1894. fw_entry = NULL;
  1895. }
  1896. load_req->phy_addr = virt_to_phys(img_data);
  1897. return ret;
  1898. err:
  1899. release_firmware(fw_entry);
  1900. return ret;
  1901. }
  1902. static int __qseecom_allocate_img_data(struct ion_handle **pihandle,
  1903. u8 **data, uint32_t fw_size, ion_phys_addr_t *paddr)
  1904. {
  1905. size_t len = 0;
  1906. int ret = 0;
  1907. ion_phys_addr_t pa;
  1908. struct ion_handle *ihandle = NULL;
  1909. u8 *img_data = NULL;
  1910. ihandle = ion_alloc(qseecom.ion_clnt, fw_size,
  1911. SZ_4K, ION_HEAP(ION_QSECOM_HEAP_ID), 0);
  1912. if (IS_ERR_OR_NULL(ihandle)) {
  1913. pr_err("ION alloc failed\n");
  1914. return -ENOMEM;
  1915. }
  1916. img_data = (u8 *)ion_map_kernel(qseecom.ion_clnt,
  1917. ihandle);
  1918. if (IS_ERR_OR_NULL(img_data)) {
  1919. pr_err("ION memory mapping for image loading failed\n");
  1920. ret = -ENOMEM;
  1921. goto exit_ion_free;
  1922. }
  1923. /* Get the physical address of the ION BUF */
  1924. ret = ion_phys(qseecom.ion_clnt, ihandle, &pa, &len);
  1925. if (ret) {
  1926. pr_err("physical memory retrieval failure\n");
  1927. ret = -EIO;
  1928. goto exit_ion_unmap_kernel;
  1929. }
  1930. *pihandle = ihandle;
  1931. *data = img_data;
  1932. *paddr = pa;
  1933. return ret;
  1934. exit_ion_unmap_kernel:
  1935. ion_unmap_kernel(qseecom.ion_clnt, ihandle);
  1936. exit_ion_free:
  1937. ion_free(qseecom.ion_clnt, ihandle);
  1938. ihandle = NULL;
  1939. return ret;
  1940. }
  1941. static void __qseecom_free_img_data(struct ion_handle **ihandle)
  1942. {
  1943. ion_unmap_kernel(qseecom.ion_clnt, *ihandle);
  1944. ion_free(qseecom.ion_clnt, *ihandle);
  1945. *ihandle = NULL;
  1946. }
  1947. static int __qseecom_load_fw(struct qseecom_dev_handle *data, char *appname)
  1948. {
  1949. int ret = -1;
  1950. uint32_t fw_size = 0;
  1951. struct qseecom_load_app_ireq load_req = {0, 0, 0, 0};
  1952. struct qseecom_command_scm_resp resp;
  1953. u8 *img_data = NULL;
  1954. ion_phys_addr_t pa = 0;
  1955. struct ion_handle *ihandle = NULL;
  1956. if (__qseecom_get_fw_size(appname, &fw_size))
  1957. return -EIO;
  1958. ret = __qseecom_allocate_img_data(&ihandle, &img_data, fw_size, &pa);
  1959. if (ret)
  1960. return ret;
  1961. ret = __qseecom_get_fw_data(appname, img_data, fw_size, &load_req);
  1962. if (ret) {
  1963. ret = -EIO;
  1964. goto exit_free_img_data;
  1965. }
  1966. /* Populate the remaining parameters */
  1967. load_req.phy_addr = (uint32_t)pa;
  1968. load_req.qsee_cmd_id = QSEOS_APP_START_COMMAND;
  1969. strlcpy(load_req.app_name, appname, MAX_APP_NAME_SIZE);
  1970. if (qseecom.support_bus_scaling) {
  1971. mutex_lock(&qsee_bw_mutex);
  1972. ret = __qseecom_register_bus_bandwidth_needs(data, MEDIUM);
  1973. mutex_unlock(&qsee_bw_mutex);
  1974. if (ret) {
  1975. ret = -EIO;
  1976. goto exit_free_img_data;
  1977. }
  1978. }
  1979. ret = __qseecom_enable_clk_scale_up(data);
  1980. if (ret) {
  1981. ret = -EIO;
  1982. goto exit_unregister_bus_bw_need;
  1983. }
  1984. __cpuc_flush_dcache_area((void *)img_data, fw_size);
  1985. /* SCM_CALL to load the image */
  1986. ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &load_req,
  1987. sizeof(struct qseecom_load_app_ireq),
  1988. &resp, sizeof(resp));
  1989. if (ret) {
  1990. pr_err("scm_call to load failed : ret %d\n", ret);
  1991. ret = -EIO;
  1992. goto exit_disable_clk_vote;
  1993. }
  1994. switch (resp.result) {
  1995. case QSEOS_RESULT_SUCCESS:
  1996. ret = resp.data;
  1997. break;
  1998. case QSEOS_RESULT_INCOMPLETE:
  1999. ret = __qseecom_process_incomplete_cmd(data, &resp);
  2000. if (ret)
  2001. pr_err("process_incomplete_cmd FAILED\n");
  2002. else
  2003. ret = resp.data;
  2004. break;
  2005. case QSEOS_RESULT_FAILURE:
  2006. pr_err("scm call failed with response QSEOS_RESULT FAILURE\n");
  2007. break;
  2008. default:
  2009. pr_err("scm call return unknown response %d\n", resp.result);
  2010. ret = -EINVAL;
  2011. break;
  2012. }
  2013. exit_disable_clk_vote:
  2014. __qseecom_disable_clk_scale_down(data);
  2015. exit_unregister_bus_bw_need:
  2016. if (qseecom.support_bus_scaling) {
  2017. mutex_lock(&qsee_bw_mutex);
  2018. qseecom_unregister_bus_bandwidth_needs(data);
  2019. mutex_unlock(&qsee_bw_mutex);
  2020. }
  2021. exit_free_img_data:
  2022. __qseecom_free_img_data(&ihandle);
  2023. return ret;
  2024. }
  2025. static int qseecom_load_commonlib_image(struct qseecom_dev_handle *data)
  2026. {
  2027. int ret = 0;
  2028. uint32_t fw_size = 0;
  2029. struct qseecom_load_app_ireq load_req = {0, 0, 0, 0};
  2030. struct qseecom_command_scm_resp resp;
  2031. u8 *img_data = NULL;
  2032. ion_phys_addr_t pa = 0;
  2033. if (__qseecom_get_fw_size("cmnlib", &fw_size))
  2034. return -EIO;
  2035. ret = __qseecom_allocate_img_data(&qseecom.cmnlib_ion_handle,
  2036. &img_data, fw_size, &pa);
  2037. if (ret)
  2038. return -EIO;
  2039. ret = __qseecom_get_fw_data("cmnlib", img_data, fw_size, &load_req);
  2040. if (ret) {
  2041. ret = -EIO;
  2042. goto exit_free_img_data;
  2043. }
  2044. load_req.phy_addr = (uint32_t)pa;
  2045. load_req.qsee_cmd_id = QSEOS_LOAD_SERV_IMAGE_COMMAND;
  2046. if (qseecom.support_bus_scaling) {
  2047. mutex_lock(&qsee_bw_mutex);
  2048. ret = __qseecom_register_bus_bandwidth_needs(data, MEDIUM);
  2049. mutex_unlock(&qsee_bw_mutex);
  2050. if (ret) {
  2051. ret = -EIO;
  2052. goto exit_free_img_data;
  2053. }
  2054. }
  2055. /* Vote for the SFPB clock */
  2056. ret = __qseecom_enable_clk_scale_up(data);
  2057. if (ret) {
  2058. ret = -EIO;
  2059. goto exit_unregister_bus_bw_need;
  2060. }
  2061. __cpuc_flush_dcache_area((void *)img_data, fw_size);
  2062. /* SCM_CALL to load the image */
  2063. ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &load_req,
  2064. sizeof(struct qseecom_load_lib_image_ireq),
  2065. &resp, sizeof(resp));
  2066. if (ret) {
  2067. pr_err("scm_call to load failed : ret %d\n", ret);
  2068. ret = -EIO;
  2069. goto exit_disable_clk_vote;
  2070. }
  2071. switch (resp.result) {
  2072. case QSEOS_RESULT_SUCCESS:
  2073. break;
  2074. case QSEOS_RESULT_FAILURE:
  2075. pr_err("scm call failed w/response result%d\n", resp.result);
  2076. ret = -EINVAL;
  2077. goto exit_disable_clk_vote;
  2078. case QSEOS_RESULT_INCOMPLETE:
  2079. ret = __qseecom_process_incomplete_cmd(data, &resp);
  2080. if (ret) {
  2081. pr_err("process_incomplete_cmd failed err: %d\n", ret);
  2082. goto exit_disable_clk_vote;
  2083. }
  2084. break;
  2085. default:
  2086. pr_err("scm call return unknown response %d\n", resp.result);
  2087. ret = -EINVAL;
  2088. goto exit_disable_clk_vote;
  2089. }
  2090. __qseecom_disable_clk_scale_down(data);
  2091. if (qseecom.support_bus_scaling) {
  2092. mutex_lock(&qsee_bw_mutex);
  2093. qseecom_unregister_bus_bandwidth_needs(data);
  2094. mutex_unlock(&qsee_bw_mutex);
  2095. }
  2096. return ret;
  2097. exit_disable_clk_vote:
  2098. __qseecom_disable_clk_scale_down(data);
  2099. exit_unregister_bus_bw_need:
  2100. if (qseecom.support_bus_scaling) {
  2101. mutex_lock(&qsee_bw_mutex);
  2102. qseecom_unregister_bus_bandwidth_needs(data);
  2103. mutex_unlock(&qsee_bw_mutex);
  2104. }
  2105. exit_free_img_data:
  2106. __qseecom_free_img_data(&qseecom.cmnlib_ion_handle);
  2107. return ret;
  2108. }
  2109. static int qseecom_unload_commonlib_image(void)
  2110. {
  2111. int ret = -EINVAL;
  2112. struct qseecom_unload_lib_image_ireq unload_req = {0};
  2113. struct qseecom_command_scm_resp resp;
  2114. /* Populate the remaining parameters */
  2115. unload_req.qsee_cmd_id = QSEOS_UNLOAD_SERV_IMAGE_COMMAND;
  2116. /* SCM_CALL to load the image */
  2117. ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &unload_req,
  2118. sizeof(struct qseecom_unload_lib_image_ireq),
  2119. &resp, sizeof(resp));
  2120. if (ret) {
  2121. pr_err("scm_call to unload lib failed : ret %d\n", ret);
  2122. ret = -EIO;
  2123. } else {
  2124. switch (resp.result) {
  2125. case QSEOS_RESULT_SUCCESS:
  2126. break;
  2127. case QSEOS_RESULT_FAILURE:
  2128. pr_err("scm fail resp.result QSEOS_RESULT FAILURE\n");
  2129. break;
  2130. default:
  2131. pr_err("scm call return unknown response %d\n",
  2132. resp.result);
  2133. ret = -EINVAL;
  2134. break;
  2135. }
  2136. }
  2137. __qseecom_free_img_data(&qseecom.cmnlib_ion_handle);
  2138. return ret;
  2139. }
  2140. int qseecom_start_app(struct qseecom_handle **handle,
  2141. char *app_name, uint32_t size)
  2142. {
  2143. int32_t ret = 0;
  2144. unsigned long flags = 0;
  2145. struct qseecom_dev_handle *data = NULL;
  2146. struct qseecom_check_app_ireq app_ireq;
  2147. struct qseecom_registered_app_list *entry = NULL;
  2148. struct qseecom_registered_kclient_list *kclient_entry = NULL;
  2149. bool found_app = false;
  2150. uint32_t len;
  2151. ion_phys_addr_t pa;
  2152. if (!app_name || strlen(app_name) >= MAX_APP_NAME_SIZE) {
  2153. pr_err("The app_name (%s) is not valid\n", app_name);
  2154. return -EINVAL;
  2155. }
  2156. *handle = kzalloc(sizeof(struct qseecom_handle), GFP_KERNEL);
  2157. if (!(*handle)) {
  2158. pr_err("failed to allocate memory for kernel client handle\n");
  2159. return -ENOMEM;
  2160. }
  2161. data = kzalloc(sizeof(*data), GFP_KERNEL);
  2162. if (!data) {
  2163. pr_err("kmalloc failed\n");
  2164. if (ret == 0) {
  2165. kfree(*handle);
  2166. *handle = NULL;
  2167. }
  2168. return -ENOMEM;
  2169. }
  2170. data->abort = 0;
  2171. data->type = QSEECOM_CLIENT_APP;
  2172. data->released = false;
  2173. data->client.sb_length = size;
  2174. data->client.user_virt_sb_base = 0;
  2175. data->client.ihandle = NULL;
  2176. init_waitqueue_head(&data->abort_wq);
  2177. atomic_set(&data->ioctl_count, 0);
  2178. data->client.ihandle = ion_alloc(qseecom.ion_clnt, size, 4096,
  2179. ION_HEAP(ION_QSECOM_HEAP_ID), 0);
  2180. if (IS_ERR_OR_NULL(data->client.ihandle)) {
  2181. pr_err("Ion client could not retrieve the handle\n");
  2182. kfree(data);
  2183. kfree(*handle);
  2184. *handle = NULL;
  2185. return -EINVAL;
  2186. }
  2187. mutex_lock(&app_access_lock);
  2188. if (qseecom.qsee_version > QSEEE_VERSION_00) {
  2189. if (qseecom.commonlib_loaded == false) {
  2190. ret = qseecom_load_commonlib_image(data);
  2191. if (ret == 0)
  2192. qseecom.commonlib_loaded = true;
  2193. }
  2194. }
  2195. if (ret) {
  2196. pr_err("Failed to load commonlib image\n");
  2197. ret = -EIO;
  2198. goto err;
  2199. }
  2200. app_ireq.qsee_cmd_id = QSEOS_APP_LOOKUP_COMMAND;
  2201. strlcpy(app_ireq.app_name, app_name, MAX_APP_NAME_SIZE);
  2202. ret = __qseecom_check_app_exists(app_ireq);
  2203. if (ret < 0)
  2204. goto err;
  2205. data->client.app_id = ret;
  2206. if (ret > 0) {
  2207. pr_warn("App id %d for [%s] app exists\n", ret,
  2208. (char *)app_ireq.app_name);
  2209. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
  2210. list_for_each_entry(entry,
  2211. &qseecom.registered_app_list_head, list){
  2212. if (entry->app_id == ret) {
  2213. entry->ref_cnt++;
  2214. found_app = true;
  2215. break;
  2216. }
  2217. }
  2218. spin_unlock_irqrestore(
  2219. &qseecom.registered_app_list_lock, flags);
  2220. if (!found_app)
  2221. pr_warn("App_id %d [%s] was loaded but not registered\n",
  2222. ret, (char *)app_ireq.app_name);
  2223. } else {
  2224. /* load the app and get the app_id */
  2225. pr_debug("%s: Loading app for the first time'\n",
  2226. qseecom.pdev->init_name);
  2227. ret = __qseecom_load_fw(data, app_name);
  2228. if (ret < 0)
  2229. goto err;
  2230. data->client.app_id = ret;
  2231. strlcpy(data->client.app_name, app_name, MAX_APP_NAME_SIZE);
  2232. }
  2233. if (!found_app) {
  2234. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  2235. if (!entry) {
  2236. pr_err("kmalloc for app entry failed\n");
  2237. ret = -ENOMEM;
  2238. goto err;
  2239. }
  2240. entry->app_id = ret;
  2241. entry->ref_cnt = 1;
  2242. strlcpy(entry->app_name, app_name, MAX_APP_NAME_SIZE);
  2243. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
  2244. list_add_tail(&entry->list, &qseecom.registered_app_list_head);
  2245. spin_unlock_irqrestore(&qseecom.registered_app_list_lock,
  2246. flags);
  2247. }
  2248. /* Get the physical address of the ION BUF */
  2249. ret = ion_phys(qseecom.ion_clnt, data->client.ihandle, &pa, &len);
  2250. if (ret) {
  2251. pr_err("Cannot get phys_addr for the Ion Client, ret = %d\n",
  2252. ret);
  2253. goto err;
  2254. }
  2255. /* Populate the structure for sending scm call to load image */
  2256. data->client.sb_virt = (char *) ion_map_kernel(qseecom.ion_clnt,
  2257. data->client.ihandle);
  2258. data->client.user_virt_sb_base = (uint32_t)data->client.sb_virt;
  2259. data->client.sb_phys = pa;
  2260. (*handle)->dev = (void *)data;
  2261. (*handle)->sbuf = (unsigned char *)data->client.sb_virt;
  2262. (*handle)->sbuf_len = data->client.sb_length;
  2263. kclient_entry = kzalloc(sizeof(*kclient_entry), GFP_KERNEL);
  2264. if (!kclient_entry) {
  2265. pr_err("kmalloc failed\n");
  2266. ret = -ENOMEM;
  2267. goto err;
  2268. }
  2269. kclient_entry->handle = *handle;
  2270. spin_lock_irqsave(&qseecom.registered_kclient_list_lock, flags);
  2271. list_add_tail(&kclient_entry->list,
  2272. &qseecom.registered_kclient_list_head);
  2273. spin_unlock_irqrestore(&qseecom.registered_kclient_list_lock, flags);
  2274. mutex_unlock(&app_access_lock);
  2275. return 0;
  2276. err:
  2277. kfree(data);
  2278. kfree(*handle);
  2279. *handle = NULL;
  2280. mutex_unlock(&app_access_lock);
  2281. return ret;
  2282. }
  2283. EXPORT_SYMBOL(qseecom_start_app);
  2284. int qseecom_shutdown_app(struct qseecom_handle **handle)
  2285. {
  2286. int ret = -EINVAL;
  2287. struct qseecom_dev_handle *data;
  2288. struct qseecom_registered_kclient_list *kclient = NULL;
  2289. unsigned long flags = 0;
  2290. bool found_handle = false;
  2291. if ((handle == NULL) || (*handle == NULL)) {
  2292. pr_err("Handle is not initialized\n");
  2293. return -EINVAL;
  2294. }
  2295. data = (struct qseecom_dev_handle *) ((*handle)->dev);
  2296. mutex_lock(&app_access_lock);
  2297. atomic_inc(&data->ioctl_count);
  2298. spin_lock_irqsave(&qseecom.registered_kclient_list_lock, flags);
  2299. list_for_each_entry(kclient, &qseecom.registered_kclient_list_head,
  2300. list) {
  2301. if (kclient->handle == (*handle)) {
  2302. list_del(&kclient->list);
  2303. found_handle = true;
  2304. break;
  2305. }
  2306. }
  2307. spin_unlock_irqrestore(&qseecom.registered_kclient_list_lock, flags);
  2308. if (!found_handle)
  2309. pr_err("Unable to find the handle, exiting\n");
  2310. else
  2311. ret = qseecom_unload_app(data, false);
  2312. if (qseecom.support_bus_scaling) {
  2313. mutex_lock(&qsee_bw_mutex);
  2314. if (data->mode != INACTIVE) {
  2315. qseecom_unregister_bus_bandwidth_needs(data);
  2316. if (qseecom.cumulative_mode == INACTIVE) {
  2317. ret = __qseecom_set_msm_bus_request(INACTIVE);
  2318. if (ret)
  2319. pr_err("Fail to scale down bus\n");
  2320. }
  2321. }
  2322. mutex_unlock(&qsee_bw_mutex);
  2323. } else {
  2324. if (data->fast_load_enabled == true)
  2325. qsee_disable_clock_vote(data, CLK_SFPB);
  2326. if (data->perf_enabled == true)
  2327. qsee_disable_clock_vote(data, CLK_DFAB);
  2328. }
  2329. atomic_dec(&data->ioctl_count);
  2330. mutex_unlock(&app_access_lock);
  2331. if (ret == 0) {
  2332. kzfree(data);
  2333. kzfree(*handle);
  2334. kzfree(kclient);
  2335. *handle = NULL;
  2336. }
  2337. return ret;
  2338. }
  2339. EXPORT_SYMBOL(qseecom_shutdown_app);
  2340. int qseecom_send_command(struct qseecom_handle *handle, void *send_buf,
  2341. uint32_t sbuf_len, void *resp_buf, uint32_t rbuf_len)
  2342. {
  2343. int ret = 0;
  2344. struct qseecom_send_cmd_req req = {0, 0, 0, 0};
  2345. struct qseecom_dev_handle *data;
  2346. bool perf_enabled = false;
  2347. if (handle == NULL) {
  2348. pr_err("Handle is not initialized\n");
  2349. return -EINVAL;
  2350. }
  2351. data = handle->dev;
  2352. req.cmd_req_len = sbuf_len;
  2353. req.resp_len = rbuf_len;
  2354. req.cmd_req_buf = send_buf;
  2355. req.resp_buf = resp_buf;
  2356. if (__validate_send_cmd_inputs(data, &req))
  2357. return -EINVAL;
  2358. mutex_lock(&app_access_lock);
  2359. atomic_inc(&data->ioctl_count);
  2360. if (qseecom.support_bus_scaling) {
  2361. ret = qseecom_scale_bus_bandwidth_timer(INACTIVE);
  2362. if (ret) {
  2363. pr_err("Failed to set bw.\n");
  2364. atomic_dec(&data->ioctl_count);
  2365. mutex_unlock(&app_access_lock);
  2366. return ret;
  2367. }
  2368. }
  2369. /*
  2370. * On targets where crypto clock is handled by HLOS,
  2371. * if clk_access_cnt is zero and perf_enabled is false,
  2372. * then the crypto clock was not enabled before sending cmd
  2373. * to tz, qseecom will enable the clock to avoid service failure.
  2374. */
  2375. if (!qseecom.qsee.clk_access_cnt && !data->perf_enabled) {
  2376. pr_debug("ce clock is not enabled!\n");
  2377. ret = qseecom_perf_enable(data);
  2378. if (ret) {
  2379. pr_err("Failed to vote for clock with err %d\n",
  2380. ret);
  2381. atomic_dec(&data->ioctl_count);
  2382. mutex_unlock(&app_access_lock);
  2383. return -EINVAL;
  2384. }
  2385. perf_enabled = true;
  2386. }
  2387. ret = __qseecom_send_cmd(data, &req);
  2388. if (qseecom.support_bus_scaling)
  2389. __qseecom_add_bw_scale_down_timer(
  2390. QSEECOM_SEND_CMD_CRYPTO_TIMEOUT);
  2391. if (perf_enabled) {
  2392. qsee_disable_clock_vote(data, CLK_DFAB);
  2393. qsee_disable_clock_vote(data, CLK_SFPB);
  2394. }
  2395. atomic_dec(&data->ioctl_count);
  2396. mutex_unlock(&app_access_lock);
  2397. if (ret)
  2398. return ret;
  2399. pr_debug("sending cmd_req->rsp size: %u, ptr: 0x%pK\n",
  2400. req.resp_len, req.resp_buf);
  2401. return ret;
  2402. }
  2403. EXPORT_SYMBOL(qseecom_send_command);
  2404. int qseecom_set_bandwidth(struct qseecom_handle *handle, bool high)
  2405. {
  2406. int ret = 0;
  2407. if ((handle == NULL) || (handle->dev == NULL)) {
  2408. pr_err("No valid kernel client\n");
  2409. return -EINVAL;
  2410. }
  2411. if (high) {
  2412. if (qseecom.support_bus_scaling) {
  2413. mutex_lock(&qsee_bw_mutex);
  2414. __qseecom_register_bus_bandwidth_needs(handle->dev,
  2415. HIGH);
  2416. mutex_unlock(&qsee_bw_mutex);
  2417. if (ret)
  2418. pr_err("Failed to scale bus (med) %d\n", ret);
  2419. } else {
  2420. ret = qseecom_perf_enable(handle->dev);
  2421. if (ret)
  2422. pr_err("Failed to vote for clock with err %d\n",
  2423. ret);
  2424. }
  2425. } else {
  2426. if (!qseecom.support_bus_scaling) {
  2427. qsee_disable_clock_vote(handle->dev, CLK_DFAB);
  2428. qsee_disable_clock_vote(handle->dev, CLK_SFPB);
  2429. } else {
  2430. mutex_lock(&qsee_bw_mutex);
  2431. qseecom_unregister_bus_bandwidth_needs(handle->dev);
  2432. mutex_unlock(&qsee_bw_mutex);
  2433. }
  2434. }
  2435. return ret;
  2436. }
  2437. EXPORT_SYMBOL(qseecom_set_bandwidth);
  2438. static int qseecom_send_resp(void)
  2439. {
  2440. qseecom.send_resp_flag = 1;
  2441. wake_up_interruptible(&qseecom.send_resp_wq);
  2442. return 0;
  2443. }
  2444. static int __validate_send_modfd_resp_inputs(struct qseecom_dev_handle *data,
  2445. struct qseecom_send_modfd_listener_resp *resp,
  2446. struct qseecom_registered_listener_list *this_lstnr)
  2447. {
  2448. int i;
  2449. if (!data || !resp || !this_lstnr) {
  2450. pr_err("listener handle or resp msg is null\n");
  2451. return -EINVAL;
  2452. }
  2453. if (resp->resp_buf_ptr == NULL) {
  2454. pr_err("resp buffer is null\n");
  2455. return -EINVAL;
  2456. }
  2457. /* validate resp buf length */
  2458. if ((resp->resp_len == 0) ||
  2459. (resp->resp_len > this_lstnr->sb_length)) {
  2460. pr_err("resp buf length %d not valid\n", resp->resp_len);
  2461. return -EINVAL;
  2462. }
  2463. if ((uintptr_t)resp->resp_buf_ptr > (ULONG_MAX - resp->resp_len)) {
  2464. pr_err("Integer overflow in resp_len & resp_buf\n");
  2465. return -EINVAL;
  2466. }
  2467. if ((uintptr_t)this_lstnr->user_virt_sb_base >
  2468. (ULONG_MAX - this_lstnr->sb_length)) {
  2469. pr_err("Integer overflow in user_virt_sb_base & sb_length\n");
  2470. return -EINVAL;
  2471. }
  2472. /* validate resp buf */
  2473. if (((uintptr_t)resp->resp_buf_ptr <
  2474. (uintptr_t)this_lstnr->user_virt_sb_base) ||
  2475. ((uintptr_t)resp->resp_buf_ptr >=
  2476. ((uintptr_t)this_lstnr->user_virt_sb_base +
  2477. this_lstnr->sb_length)) ||
  2478. (((uintptr_t)resp->resp_buf_ptr + resp->resp_len) >
  2479. ((uintptr_t)this_lstnr->user_virt_sb_base +
  2480. this_lstnr->sb_length))) {
  2481. pr_err("resp buf is out of shared buffer region\n");
  2482. return -EINVAL;
  2483. }
  2484. /* validate offsets */
  2485. for (i = 0; i < MAX_ION_FD; i++) {
  2486. if (resp->ifd_data[i].cmd_buf_offset >= resp->resp_len) {
  2487. pr_err("Invalid offset %d = 0x%x\n",
  2488. i, resp->ifd_data[i].cmd_buf_offset);
  2489. return -EINVAL;
  2490. }
  2491. }
  2492. return 0;
  2493. }
  2494. static int __qseecom_send_modfd_resp(struct qseecom_dev_handle *data,
  2495. void __user *argp, bool is_64bit_addr)
  2496. {
  2497. struct qseecom_send_modfd_listener_resp resp;
  2498. struct qseecom_registered_listener_list *this_lstnr = NULL;
  2499. if (copy_from_user(&resp, argp, sizeof(resp))) {
  2500. pr_err("copy_from_user failed");
  2501. return -EINVAL;
  2502. }
  2503. this_lstnr = __qseecom_find_svc(data->listener.id);
  2504. if (this_lstnr == NULL)
  2505. return -EINVAL;
  2506. if (__validate_send_modfd_resp_inputs(data, &resp, this_lstnr))
  2507. return -EINVAL;
  2508. resp.resp_buf_ptr = this_lstnr->sb_virt +
  2509. (uintptr_t)(resp.resp_buf_ptr - this_lstnr->user_virt_sb_base);
  2510. __qseecom_update_cmd_buf(&resp, false, data, true);
  2511. qseecom.send_resp_flag = 1;
  2512. wake_up_interruptible(&qseecom.send_resp_wq);
  2513. return 0;
  2514. }
  2515. static int qseecom_send_modfd_resp(struct qseecom_dev_handle *data,
  2516. void __user *argp)
  2517. {
  2518. return __qseecom_send_modfd_resp(data, argp, false);
  2519. }
  2520. static int qseecom_get_qseos_version(struct qseecom_dev_handle *data,
  2521. void __user *argp)
  2522. {
  2523. struct qseecom_qseos_version_req req;
  2524. if (copy_from_user(&req, argp, sizeof(req))) {
  2525. pr_err("copy_from_user failed");
  2526. return -EINVAL;
  2527. }
  2528. req.qseos_version = qseecom.qseos_version;
  2529. if (copy_to_user(argp, &req, sizeof(req))) {
  2530. pr_err("copy_to_user failed");
  2531. return -EINVAL;
  2532. }
  2533. return 0;
  2534. }
  2535. static int __qseecom_enable_clk(enum qseecom_ce_hw_instance ce)
  2536. {
  2537. int rc = 0;
  2538. struct qseecom_clk *qclk;
  2539. if (ce == CLK_QSEE)
  2540. qclk = &qseecom.qsee;
  2541. else
  2542. qclk = &qseecom.ce_drv;
  2543. mutex_lock(&clk_access_lock);
  2544. if (qclk->clk_access_cnt == ULONG_MAX)
  2545. goto err;
  2546. if (qclk->clk_access_cnt > 0) {
  2547. qclk->clk_access_cnt++;
  2548. mutex_unlock(&clk_access_lock);
  2549. return rc;
  2550. }
  2551. /* Enable CE core clk */
  2552. rc = clk_prepare_enable(qclk->ce_core_clk);
  2553. if (rc) {
  2554. pr_err("Unable to enable/prepare CE core clk\n");
  2555. goto err;
  2556. }
  2557. /* Enable CE clk */
  2558. rc = clk_prepare_enable(qclk->ce_clk);
  2559. if (rc) {
  2560. pr_err("Unable to enable/prepare CE iface clk\n");
  2561. goto ce_clk_err;
  2562. }
  2563. /* Enable AXI clk */
  2564. rc = clk_prepare_enable(qclk->ce_bus_clk);
  2565. if (rc) {
  2566. pr_err("Unable to enable/prepare CE bus clk\n");
  2567. goto ce_bus_clk_err;
  2568. }
  2569. qclk->clk_access_cnt++;
  2570. mutex_unlock(&clk_access_lock);
  2571. return 0;
  2572. ce_bus_clk_err:
  2573. clk_disable_unprepare(qclk->ce_clk);
  2574. ce_clk_err:
  2575. clk_disable_unprepare(qclk->ce_core_clk);
  2576. err:
  2577. mutex_unlock(&clk_access_lock);
  2578. return -EIO;
  2579. }
  2580. static void __qseecom_disable_clk(enum qseecom_ce_hw_instance ce)
  2581. {
  2582. struct qseecom_clk *qclk;
  2583. if (ce == CLK_QSEE)
  2584. qclk = &qseecom.qsee;
  2585. else
  2586. qclk = &qseecom.ce_drv;
  2587. mutex_lock(&clk_access_lock);
  2588. if (qclk->clk_access_cnt == 0) {
  2589. mutex_unlock(&clk_access_lock);
  2590. return;
  2591. }
  2592. if (qclk->clk_access_cnt == 1) {
  2593. if (qclk->ce_clk != NULL)
  2594. clk_disable_unprepare(qclk->ce_clk);
  2595. if (qclk->ce_core_clk != NULL)
  2596. clk_disable_unprepare(qclk->ce_core_clk);
  2597. if (qclk->ce_bus_clk != NULL)
  2598. clk_disable_unprepare(qclk->ce_bus_clk);
  2599. }
  2600. qclk->clk_access_cnt--;
  2601. mutex_unlock(&clk_access_lock);
  2602. }
  2603. static int qsee_vote_for_clock(struct qseecom_dev_handle *data,
  2604. int32_t clk_type)
  2605. {
  2606. int ret = 0;
  2607. struct qseecom_clk *qclk;
  2608. qclk = &qseecom.qsee;
  2609. if (!qseecom.qsee_perf_client)
  2610. return ret;
  2611. switch (clk_type) {
  2612. case CLK_DFAB:
  2613. mutex_lock(&qsee_bw_mutex);
  2614. if (!qseecom.qsee_bw_count) {
  2615. if (qseecom.qsee_sfpb_bw_count > 0)
  2616. ret = msm_bus_scale_client_update_request(
  2617. qseecom.qsee_perf_client, 3);
  2618. else {
  2619. if (qclk->ce_core_src_clk != NULL)
  2620. ret = __qseecom_enable_clk(CLK_QSEE);
  2621. if (!ret) {
  2622. ret =
  2623. msm_bus_scale_client_update_request(
  2624. qseecom.qsee_perf_client, 1);
  2625. if ((ret) &&
  2626. (qclk->ce_core_src_clk != NULL))
  2627. __qseecom_disable_clk(CLK_QSEE);
  2628. }
  2629. }
  2630. if (ret)
  2631. pr_err("DFAB Bandwidth req failed (%d)\n",
  2632. ret);
  2633. else {
  2634. qseecom.qsee_bw_count++;
  2635. data->perf_enabled = true;
  2636. }
  2637. } else {
  2638. qseecom.qsee_bw_count++;
  2639. data->perf_enabled = true;
  2640. }
  2641. mutex_unlock(&qsee_bw_mutex);
  2642. break;
  2643. case CLK_SFPB:
  2644. mutex_lock(&qsee_bw_mutex);
  2645. if (!qseecom.qsee_sfpb_bw_count) {
  2646. if (qseecom.qsee_bw_count > 0)
  2647. ret = msm_bus_scale_client_update_request(
  2648. qseecom.qsee_perf_client, 3);
  2649. else {
  2650. if (qclk->ce_core_src_clk != NULL)
  2651. ret = __qseecom_enable_clk(CLK_QSEE);
  2652. if (!ret) {
  2653. ret =
  2654. msm_bus_scale_client_update_request(
  2655. qseecom.qsee_perf_client, 2);
  2656. if ((ret) &&
  2657. (qclk->ce_core_src_clk != NULL))
  2658. __qseecom_disable_clk(CLK_QSEE);
  2659. }
  2660. }
  2661. if (ret)
  2662. pr_err("SFPB Bandwidth req failed (%d)\n",
  2663. ret);
  2664. else {
  2665. qseecom.qsee_sfpb_bw_count++;
  2666. data->fast_load_enabled = true;
  2667. }
  2668. } else {
  2669. qseecom.qsee_sfpb_bw_count++;
  2670. data->fast_load_enabled = true;
  2671. }
  2672. mutex_unlock(&qsee_bw_mutex);
  2673. break;
  2674. default:
  2675. pr_err("Clock type not defined\n");
  2676. break;
  2677. }
  2678. return ret;
  2679. }
  2680. static void qsee_disable_clock_vote(struct qseecom_dev_handle *data,
  2681. int32_t clk_type)
  2682. {
  2683. int32_t ret = 0;
  2684. struct qseecom_clk *qclk;
  2685. qclk = &qseecom.qsee;
  2686. if (!qseecom.qsee_perf_client)
  2687. return;
  2688. switch (clk_type) {
  2689. case CLK_DFAB:
  2690. mutex_lock(&qsee_bw_mutex);
  2691. if (qseecom.qsee_bw_count == 0) {
  2692. pr_err("Client error.Extra call to disable DFAB clk\n");
  2693. mutex_unlock(&qsee_bw_mutex);
  2694. return;
  2695. }
  2696. if (qseecom.qsee_bw_count == 1) {
  2697. if (qseecom.qsee_sfpb_bw_count > 0)
  2698. ret = msm_bus_scale_client_update_request(
  2699. qseecom.qsee_perf_client, 2);
  2700. else {
  2701. ret = msm_bus_scale_client_update_request(
  2702. qseecom.qsee_perf_client, 0);
  2703. if ((!ret) && (qclk->ce_core_src_clk != NULL))
  2704. __qseecom_disable_clk(CLK_QSEE);
  2705. }
  2706. if (ret)
  2707. pr_err("SFPB Bandwidth req fail (%d)\n",
  2708. ret);
  2709. else {
  2710. qseecom.qsee_bw_count--;
  2711. data->perf_enabled = false;
  2712. }
  2713. } else {
  2714. qseecom.qsee_bw_count--;
  2715. data->perf_enabled = false;
  2716. }
  2717. mutex_unlock(&qsee_bw_mutex);
  2718. break;
  2719. case CLK_SFPB:
  2720. mutex_lock(&qsee_bw_mutex);
  2721. if (qseecom.qsee_sfpb_bw_count == 0) {
  2722. pr_err("Client error.Extra call to disable SFPB clk\n");
  2723. mutex_unlock(&qsee_bw_mutex);
  2724. return;
  2725. }
  2726. if (qseecom.qsee_sfpb_bw_count == 1) {
  2727. if (qseecom.qsee_bw_count > 0)
  2728. ret = msm_bus_scale_client_update_request(
  2729. qseecom.qsee_perf_client, 1);
  2730. else {
  2731. ret = msm_bus_scale_client_update_request(
  2732. qseecom.qsee_perf_client, 0);
  2733. if ((!ret) && (qclk->ce_core_src_clk != NULL))
  2734. __qseecom_disable_clk(CLK_QSEE);
  2735. }
  2736. if (ret)
  2737. pr_err("SFPB Bandwidth req fail (%d)\n",
  2738. ret);
  2739. else {
  2740. qseecom.qsee_sfpb_bw_count--;
  2741. data->fast_load_enabled = false;
  2742. }
  2743. } else {
  2744. qseecom.qsee_sfpb_bw_count--;
  2745. data->fast_load_enabled = false;
  2746. }
  2747. mutex_unlock(&qsee_bw_mutex);
  2748. break;
  2749. default:
  2750. pr_err("Clock type not defined\n");
  2751. break;
  2752. }
  2753. }
  2754. static int qseecom_load_external_elf(struct qseecom_dev_handle *data,
  2755. void __user *argp)
  2756. {
  2757. struct ion_handle *ihandle; /* Ion handle */
  2758. struct qseecom_load_img_req load_img_req;
  2759. int ret;
  2760. int set_cpu_ret = 0;
  2761. ion_phys_addr_t pa = 0;
  2762. uint32_t len;
  2763. struct cpumask mask;
  2764. struct qseecom_load_app_ireq load_req;
  2765. struct qseecom_command_scm_resp resp;
  2766. /* Copy the relevant information needed for loading the image */
  2767. if (copy_from_user(&load_img_req,
  2768. (void __user *)argp,
  2769. sizeof(struct qseecom_load_img_req))) {
  2770. pr_err("copy_from_user failed\n");
  2771. return -EFAULT;
  2772. }
  2773. /* Get the handle of the shared fd */
  2774. ihandle = ion_import_dma_buf(qseecom.ion_clnt,
  2775. load_img_req.ifd_data_fd);
  2776. if (IS_ERR_OR_NULL(ihandle)) {
  2777. pr_err("Ion client could not retrieve the handle\n");
  2778. return -ENOMEM;
  2779. }
  2780. /* Get the physical address of the ION BUF */
  2781. ret = ion_phys(qseecom.ion_clnt, ihandle, &pa, &len);
  2782. if (ret) {
  2783. pr_err("Cannot get phys_addr for the Ion Client, ret = %d\n",
  2784. ret);
  2785. return ret;
  2786. }
  2787. /* Populate the structure for sending scm call to load image */
  2788. load_req.qsee_cmd_id = QSEOS_LOAD_EXTERNAL_ELF_COMMAND;
  2789. load_req.mdt_len = load_img_req.mdt_len;
  2790. load_req.img_len = load_img_req.img_len;
  2791. load_req.phy_addr = pa;
  2792. /* SCM_CALL tied to Core0 */
  2793. mask = CPU_MASK_CPU0;
  2794. set_cpu_ret = set_cpus_allowed_ptr(current, &mask);
  2795. if (set_cpu_ret) {
  2796. pr_err("set_cpus_allowed_ptr failed : ret %d\n",
  2797. set_cpu_ret);
  2798. ret = -EFAULT;
  2799. goto exit_ion_free;
  2800. }
  2801. if (qseecom.support_bus_scaling) {
  2802. mutex_lock(&qsee_bw_mutex);
  2803. ret = __qseecom_register_bus_bandwidth_needs(data, MEDIUM);
  2804. mutex_unlock(&qsee_bw_mutex);
  2805. if (ret) {
  2806. ret = -EIO;
  2807. goto exit_cpu_restore;
  2808. }
  2809. }
  2810. /* Vote for the SFPB clock */
  2811. ret = __qseecom_enable_clk_scale_up(data);
  2812. if (ret) {
  2813. ret = -EIO;
  2814. goto exit_register_bus_bandwidth_needs;
  2815. }
  2816. msm_ion_do_cache_op(qseecom.ion_clnt, ihandle, NULL, len,
  2817. ION_IOC_CLEAN_INV_CACHES);
  2818. /* SCM_CALL to load the external elf */
  2819. ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &load_req,
  2820. sizeof(struct qseecom_load_app_ireq),
  2821. &resp, sizeof(resp));
  2822. if (ret) {
  2823. pr_err("scm_call to load failed : ret %d\n",
  2824. ret);
  2825. ret = -EFAULT;
  2826. goto exit_disable_clock;
  2827. }
  2828. switch (resp.result) {
  2829. case QSEOS_RESULT_SUCCESS:
  2830. break;
  2831. case QSEOS_RESULT_INCOMPLETE:
  2832. pr_err("%s: qseos result incomplete\n", __func__);
  2833. ret = __qseecom_process_incomplete_cmd(data, &resp);
  2834. if (ret)
  2835. pr_err("process_incomplete_cmd failed: err: %d\n", ret);
  2836. break;
  2837. case QSEOS_RESULT_FAILURE:
  2838. pr_err("scm_call rsp.result is QSEOS_RESULT_FAILURE\n");
  2839. ret = -EFAULT;
  2840. break;
  2841. default:
  2842. pr_err("scm_call response result %d not supported\n",
  2843. resp.result);
  2844. ret = -EFAULT;
  2845. break;
  2846. }
  2847. exit_disable_clock:
  2848. __qseecom_disable_clk_scale_down(data);
  2849. exit_register_bus_bandwidth_needs:
  2850. if (qseecom.support_bus_scaling) {
  2851. int ret2;
  2852. mutex_lock(&qsee_bw_mutex);
  2853. ret2 = qseecom_unregister_bus_bandwidth_needs(data);
  2854. pr_info("qseecom_unregister_bus_bandwidth_needs returned %d (ret=%d)\n", ret2, ret); // for debug
  2855. if (ret2) {
  2856. pr_err("qseecom_unregister_bus_bandwidth_needs returned %d\n", ret2);
  2857. ret = (ret || ret2);
  2858. }
  2859. mutex_unlock(&qsee_bw_mutex);
  2860. }
  2861. exit_cpu_restore:
  2862. /* Restore the CPU mask */
  2863. mask = CPU_MASK_ALL;
  2864. set_cpu_ret = set_cpus_allowed_ptr(current, &mask);
  2865. if (set_cpu_ret) {
  2866. pr_err("set_cpus_allowed_ptr failed to restore mask: ret %d\n",
  2867. set_cpu_ret);
  2868. ret = -EFAULT;
  2869. }
  2870. exit_ion_free:
  2871. /* Deallocate the handle */
  2872. if (!IS_ERR_OR_NULL(ihandle))
  2873. ion_free(qseecom.ion_clnt, ihandle);
  2874. return ret;
  2875. }
  2876. static int qseecom_unload_external_elf(struct qseecom_dev_handle *data)
  2877. {
  2878. int ret = 0;
  2879. int set_cpu_ret = 0;
  2880. struct qseecom_command_scm_resp resp;
  2881. struct qseecom_unload_app_ireq req;
  2882. struct cpumask mask;
  2883. /* unavailable client app */
  2884. data->type = QSEECOM_UNAVAILABLE_CLIENT_APP;
  2885. /* Populate the structure for sending scm call to unload image */
  2886. req.qsee_cmd_id = QSEOS_UNLOAD_EXTERNAL_ELF_COMMAND;
  2887. /* SCM_CALL tied to Core0 */
  2888. mask = CPU_MASK_CPU0;
  2889. ret = set_cpus_allowed_ptr(current, &mask);
  2890. if (ret) {
  2891. pr_err("set_cpus_allowed_ptr failed : ret %d\n",
  2892. ret);
  2893. return -EFAULT;
  2894. }
  2895. /* SCM_CALL to unload the external elf */
  2896. ret = scm_call(SCM_SVC_TZSCHEDULER, 1, &req,
  2897. sizeof(struct qseecom_unload_app_ireq),
  2898. &resp, sizeof(resp));
  2899. if (ret) {
  2900. pr_err("scm_call to unload failed : ret %d\n",
  2901. ret);
  2902. ret = -EFAULT;
  2903. goto qseecom_unload_external_elf_scm_err;
  2904. }
  2905. if (resp.result == QSEOS_RESULT_INCOMPLETE) {
  2906. ret = __qseecom_process_incomplete_cmd(data, &resp);
  2907. if (ret)
  2908. pr_err("process_incomplete_cmd fail err: %d\n",
  2909. ret);
  2910. } else {
  2911. if (resp.result != QSEOS_RESULT_SUCCESS) {
  2912. pr_err("scm_call to unload image failed resp.result =%d\n",
  2913. resp.result);
  2914. ret = -EFAULT;
  2915. }
  2916. }
  2917. qseecom_unload_external_elf_scm_err:
  2918. /* Restore the CPU mask */
  2919. mask = CPU_MASK_ALL;
  2920. set_cpu_ret = set_cpus_allowed_ptr(current, &mask);
  2921. if (set_cpu_ret) {
  2922. pr_err("set_cpus_allowed_ptr failed to restore mask: ret %d\n",
  2923. set_cpu_ret);
  2924. ret = -EFAULT;
  2925. }
  2926. return ret;
  2927. }
  2928. static int qseecom_query_app_loaded(struct qseecom_dev_handle *data,
  2929. void __user *argp)
  2930. {
  2931. int32_t ret;
  2932. struct qseecom_qseos_app_load_query query_req;
  2933. struct qseecom_check_app_ireq req;
  2934. struct qseecom_registered_app_list *entry = NULL;
  2935. unsigned long flags = 0;
  2936. bool found_app = false;
  2937. /* Copy the relevant information needed for loading the image */
  2938. if (copy_from_user(&query_req,
  2939. (void __user *)argp,
  2940. sizeof(struct qseecom_qseos_app_load_query))) {
  2941. pr_err("copy_from_user failed\n");
  2942. return -EFAULT;
  2943. }
  2944. req.qsee_cmd_id = QSEOS_APP_LOOKUP_COMMAND;
  2945. query_req.app_name[MAX_APP_NAME_SIZE-1] = '\0';
  2946. strlcpy(req.app_name, query_req.app_name, MAX_APP_NAME_SIZE);
  2947. ret = __qseecom_check_app_exists(req);
  2948. if ((ret == -EINVAL) || (ret == -ENODEV)) {
  2949. pr_err(" scm call to check if app is loaded failed");
  2950. return ret; /* scm call failed */
  2951. } else if (ret > 0) {
  2952. pr_debug("App id %d (%s) already exists\n", ret,
  2953. (char *)(req.app_name));
  2954. spin_lock_irqsave(&qseecom.registered_app_list_lock, flags);
  2955. list_for_each_entry(entry,
  2956. &qseecom.registered_app_list_head, list){
  2957. if (entry->app_id == ret) {
  2958. entry->ref_cnt++;
  2959. found_app = true;
  2960. break;
  2961. }
  2962. }
  2963. spin_unlock_irqrestore(
  2964. &qseecom.registered_app_list_lock, flags);
  2965. data->client.app_id = ret;
  2966. query_req.app_id = ret;
  2967. strlcpy(data->client.app_name, query_req.app_name,
  2968. MAX_APP_NAME_SIZE);
  2969. /*
  2970. * If app was loaded by appsbl or kernel client before
  2971. * and was not registered, regiser this app now.
  2972. */
  2973. if (!found_app) {
  2974. pr_debug("Register app %d [%s] which was loaded before\n",
  2975. ret, (char *)query_req.app_name);
  2976. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  2977. if (!entry) {
  2978. pr_err("kmalloc for app entry failed\n");
  2979. return -ENOMEM;
  2980. }
  2981. entry->app_id = ret;
  2982. entry->ref_cnt = 1;
  2983. strlcpy(entry->app_name, data->client.app_name,
  2984. MAX_APP_NAME_SIZE);
  2985. spin_lock_irqsave(&qseecom.registered_app_list_lock,
  2986. flags);
  2987. list_add_tail(&entry->list,
  2988. &qseecom.registered_app_list_head);
  2989. spin_unlock_irqrestore(
  2990. &qseecom.registered_app_list_lock, flags);
  2991. }
  2992. if (copy_to_user(argp, &query_req, sizeof(query_req))) {
  2993. pr_err("copy_to_user failed\n");
  2994. return -EFAULT;
  2995. }
  2996. return -EEXIST; /* app already loaded */
  2997. } else {
  2998. return 0; /* app not loaded */
  2999. }
  3000. }
  3001. static int __qseecom_get_ce_pipe_info(
  3002. enum qseecom_key_management_usage_type usage,
  3003. uint32_t *pipe, uint32_t *ce_hw)
  3004. {
  3005. int ret;
  3006. switch (usage) {
  3007. case QSEOS_KM_USAGE_DISK_ENCRYPTION:
  3008. if (qseecom.support_fde) {
  3009. *pipe = qseecom.ce_info.disk_encrypt_pipe;
  3010. *ce_hw = qseecom.ce_info.hlos_ce_hw_instance;
  3011. ret = 0;
  3012. } else {
  3013. pr_err("info unavailable: disk encr pipe %d ce_hw %d\n",
  3014. qseecom.ce_info.disk_encrypt_pipe,
  3015. qseecom.ce_info.hlos_ce_hw_instance);
  3016. ret = -EINVAL;
  3017. }
  3018. break;
  3019. case QSEOS_KM_USAGE_FILE_ENCRYPTION:
  3020. if (qseecom.support_pfe) {
  3021. *pipe = qseecom.ce_info.file_encrypt_pipe;
  3022. *ce_hw = qseecom.ce_info.hlos_ce_hw_instance;
  3023. ret = 0;
  3024. } else {
  3025. pr_err("info unavailable: file encr pipe %d ce_hw %d\n",
  3026. qseecom.ce_info.file_encrypt_pipe,
  3027. qseecom.ce_info.hlos_ce_hw_instance);
  3028. ret = -EINVAL;
  3029. }
  3030. break;
  3031. default:
  3032. ret = -EINVAL;
  3033. break;
  3034. }
  3035. return ret;
  3036. }
  3037. static int __qseecom_generate_and_save_key(struct qseecom_dev_handle *data,
  3038. enum qseecom_key_management_usage_type usage,
  3039. struct qseecom_key_generate_ireq *ireq)
  3040. {
  3041. struct qseecom_command_scm_resp resp;
  3042. int ret;
  3043. if (usage < QSEOS_KM_USAGE_DISK_ENCRYPTION ||
  3044. usage >= QSEOS_KM_USAGE_MAX) {
  3045. pr_err("Error:: unsupported usage %d\n", usage);
  3046. return -EFAULT;
  3047. }
  3048. __qseecom_enable_clk(CLK_QSEE);
  3049. ret = scm_call(SCM_SVC_TZSCHEDULER, 1,
  3050. ireq, sizeof(struct qseecom_key_generate_ireq),
  3051. &resp, sizeof(resp));
  3052. if (ret) {
  3053. pr_err("scm call to generate key failed : %d\n", ret);
  3054. __qseecom_disable_clk(CLK_QSEE);
  3055. return -EFAULT;
  3056. }
  3057. switch (resp.result) {
  3058. case QSEOS_RESULT_SUCCESS:
  3059. break;
  3060. case QSEOS_RESULT_FAIL_KEY_ID_EXISTS:
  3061. pr_debug("Key ID exists.\n");
  3062. break;
  3063. case QSEOS_RESULT_INCOMPLETE:
  3064. ret = __qseecom_process_incomplete_cmd(data, &resp);
  3065. if (ret) {
  3066. if (resp.result == QSEOS_RESULT_FAIL_KEY_ID_EXISTS) {
  3067. pr_debug("Key ID exists.\n");
  3068. ret = 0;
  3069. } else {
  3070. pr_err("process_incomplete_cmd FAILED, resp.result %d\n",
  3071. resp.result);
  3072. }
  3073. }
  3074. break;
  3075. case QSEOS_RESULT_FAILURE:
  3076. default:
  3077. pr_err("gen key scm call failed resp.result %d\n", resp.result);
  3078. ret = -EINVAL;
  3079. break;
  3080. }
  3081. __qseecom_disable_clk(CLK_QSEE);
  3082. return ret;
  3083. }
  3084. static int __qseecom_delete_saved_key(struct qseecom_dev_handle *data,
  3085. enum qseecom_key_management_usage_type usage,
  3086. struct qseecom_key_delete_ireq *ireq)
  3087. {
  3088. struct qseecom_command_scm_resp resp;
  3089. int ret;
  3090. if (usage < QSEOS_KM_USAGE_DISK_ENCRYPTION ||
  3091. usage >= QSEOS_KM_USAGE_MAX) {
  3092. pr_err("Error:: unsupported usage %d\n", usage);
  3093. return -EFAULT;
  3094. }
  3095. __qseecom_enable_clk(CLK_QSEE);
  3096. ret = scm_call(SCM_SVC_TZSCHEDULER, 1,
  3097. ireq, sizeof(struct qseecom_key_delete_ireq),
  3098. &resp, sizeof(struct qseecom_command_scm_resp));
  3099. if (ret) {
  3100. pr_err("scm call to delete key failed : %d\n", ret);
  3101. __qseecom_disable_clk(CLK_QSEE);
  3102. return -EFAULT;
  3103. }
  3104. switch (resp.result) {
  3105. case QSEOS_RESULT_SUCCESS:
  3106. break;
  3107. case QSEOS_RESULT_INCOMPLETE:
  3108. ret = __qseecom_process_incomplete_cmd(data, &resp);
  3109. if (ret) {
  3110. pr_err("process_incomplete_cmd FAILED, resp.result %d\n",
  3111. resp.result);
  3112. if (resp.result == QSEOS_RESULT_FAIL_MAX_ATTEMPT) {
  3113. pr_debug("Max attempts to input password reached.\n");
  3114. ret = -ERANGE;
  3115. }
  3116. }
  3117. break;
  3118. case QSEOS_RESULT_FAIL_MAX_ATTEMPT:
  3119. pr_debug("Max attempts to input password reached.\n");
  3120. ret = -ERANGE;
  3121. break;
  3122. case QSEOS_RESULT_FAILURE:
  3123. default:
  3124. pr_err("Delete key scm call failed resp.result %d\n",
  3125. resp.result);
  3126. ret = -EINVAL;
  3127. break;
  3128. }
  3129. __qseecom_disable_clk(CLK_QSEE);
  3130. return ret;
  3131. }
  3132. static int __qseecom_set_clear_ce_key(struct qseecom_dev_handle *data,
  3133. enum qseecom_key_management_usage_type usage,
  3134. struct qseecom_key_select_ireq *ireq)
  3135. {
  3136. struct qseecom_command_scm_resp resp;
  3137. int ret;
  3138. if (usage < QSEOS_KM_USAGE_DISK_ENCRYPTION ||
  3139. usage >= QSEOS_KM_USAGE_MAX) {
  3140. pr_err("Error:: unsupported usage %d\n", usage);
  3141. return -EFAULT;
  3142. }
  3143. __qseecom_enable_clk(CLK_QSEE);
  3144. if (qseecom.qsee.instance != qseecom.ce_drv.instance)
  3145. __qseecom_enable_clk(CLK_CE_DRV);
  3146. ret = scm_call(SCM_SVC_TZSCHEDULER, 1,
  3147. ireq, sizeof(struct qseecom_key_select_ireq),
  3148. &resp, sizeof(struct qseecom_command_scm_resp));
  3149. if (ret) {
  3150. pr_err("scm call to set QSEOS_PIPE_ENC key failed : %d\n", ret);
  3151. __qseecom_disable_clk(CLK_QSEE);
  3152. if (qseecom.qsee.instance != qseecom.ce_drv.instance)
  3153. __qseecom_disable_clk(CLK_CE_DRV);
  3154. return -EFAULT;
  3155. }
  3156. switch (resp.result) {
  3157. case QSEOS_RESULT_SUCCESS:
  3158. break;
  3159. case QSEOS_RESULT_INCOMPLETE:
  3160. ret = __qseecom_process_incomplete_cmd(data, &resp);
  3161. if (ret) {
  3162. pr_err("process_incomplete_cmd FAILED, resp.result %d\n",
  3163. resp.result);
  3164. if (resp.result == QSEOS_RESULT_FAIL_MAX_ATTEMPT) {
  3165. pr_debug("Max attempts to input password reached.\n");
  3166. ret = -ERANGE;
  3167. }
  3168. }
  3169. break;
  3170. case QSEOS_RESULT_FAIL_MAX_ATTEMPT:
  3171. pr_debug("Max attempts to input password reached.\n");
  3172. ret = -ERANGE;
  3173. break;
  3174. case QSEOS_RESULT_FAILURE:
  3175. default:
  3176. pr_err("Set key scm call failed resp.result %d\n", resp.result);
  3177. ret = -EINVAL;
  3178. break;
  3179. }
  3180. __qseecom_disable_clk(CLK_QSEE);
  3181. if (qseecom.qsee.instance != qseecom.ce_drv.instance)
  3182. __qseecom_disable_clk(CLK_CE_DRV);
  3183. return ret;
  3184. }
  3185. static int __qseecom_update_current_key_user_info(
  3186. struct qseecom_dev_handle *data,
  3187. enum qseecom_key_management_usage_type usage,
  3188. struct qseecom_key_userinfo_update_ireq *ireq)
  3189. {
  3190. struct qseecom_command_scm_resp resp;
  3191. int ret;
  3192. if (usage < QSEOS_KM_USAGE_DISK_ENCRYPTION ||
  3193. usage >= QSEOS_KM_USAGE_MAX) {
  3194. pr_err("Error:: unsupported usage %d\n", usage);
  3195. return -EFAULT;
  3196. }
  3197. __qseecom_enable_clk(CLK_QSEE);
  3198. ret = scm_call(SCM_SVC_TZSCHEDULER, 1,
  3199. ireq, sizeof(struct qseecom_key_userinfo_update_ireq),
  3200. &resp, sizeof(struct qseecom_command_scm_resp));
  3201. if (ret) {
  3202. pr_err("scm call to update key userinfo failed : %d\n", ret);
  3203. __qseecom_disable_clk(CLK_QSEE);
  3204. return -EFAULT;
  3205. }
  3206. switch (resp.result) {
  3207. case QSEOS_RESULT_SUCCESS:
  3208. break;
  3209. case QSEOS_RESULT_INCOMPLETE:
  3210. ret = __qseecom_process_incomplete_cmd(data, &resp);
  3211. if (ret)
  3212. pr_err("process_incomplete_cmd FAILED, resp.result %d\n",
  3213. resp.result);
  3214. break;
  3215. case QSEOS_RESULT_FAILURE:
  3216. default:
  3217. pr_err("Set key scm call failed resp.result %d\n", resp.result);
  3218. ret = -EINVAL;
  3219. break;
  3220. }
  3221. __qseecom_disable_clk(CLK_QSEE);
  3222. return ret;
  3223. }
  3224. static int qseecom_create_key(struct qseecom_dev_handle *data,
  3225. void __user *argp)
  3226. {
  3227. uint32_t ce_hw = 0;
  3228. uint32_t pipe = 0;
  3229. int ret = 0;
  3230. uint32_t flags = 0;
  3231. struct qseecom_create_key_req create_key_req;
  3232. struct qseecom_key_generate_ireq generate_key_ireq;
  3233. struct qseecom_key_select_ireq set_key_ireq;
  3234. ret = copy_from_user(&create_key_req, argp, sizeof(create_key_req));
  3235. if (ret) {
  3236. pr_err("copy_from_user failed\n");
  3237. return ret;
  3238. }
  3239. if (create_key_req.usage < QSEOS_KM_USAGE_DISK_ENCRYPTION ||
  3240. create_key_req.usage >= QSEOS_KM_USAGE_MAX) {
  3241. pr_err("Error:: unsupported usage %d\n", create_key_req.usage);
  3242. return -EFAULT;
  3243. }
  3244. ret = __qseecom_get_ce_pipe_info(create_key_req.usage, &pipe, &ce_hw);
  3245. if (ret) {
  3246. pr_err("Failed to retrieve pipe/ce_hw info: %d\n", ret);
  3247. return -EINVAL;
  3248. }
  3249. generate_key_ireq.flags = flags;
  3250. generate_key_ireq.qsee_command_id = QSEOS_GENERATE_KEY;
  3251. memset((void *)generate_key_ireq.key_id, 0, QSEECOM_KEY_ID_SIZE);
  3252. memset((void *)generate_key_ireq.hash32, 0, QSEECOM_HASH_SIZE);
  3253. memcpy((void *)generate_key_ireq.key_id,
  3254. (void *)key_id_array[create_key_req.usage].desc,
  3255. QSEECOM_KEY_ID_SIZE);
  3256. memcpy((void *)generate_key_ireq.hash32,
  3257. (void *)create_key_req.hash32, QSEECOM_HASH_SIZE);
  3258. ret = __qseecom_generate_and_save_key(data, create_key_req.usage,
  3259. &generate_key_ireq);
  3260. if (ret) {
  3261. pr_err("Failed to generate key on storage: %d\n", ret);
  3262. return ret;
  3263. }
  3264. set_key_ireq.qsee_command_id = QSEOS_SET_KEY;
  3265. set_key_ireq.ce = ce_hw;
  3266. set_key_ireq.pipe = pipe;
  3267. set_key_ireq.flags = flags;
  3268. /* set both PIPE_ENC and PIPE_ENC_XTS*/
  3269. set_key_ireq.pipe_type = QSEOS_PIPE_ENC|QSEOS_PIPE_ENC_XTS;
  3270. memset((void *)set_key_ireq.key_id, 0, QSEECOM_KEY_ID_SIZE);
  3271. memset((void *)set_key_ireq.hash32, 0, QSEECOM_HASH_SIZE);
  3272. memcpy((void *)set_key_ireq.key_id,
  3273. (void *)key_id_array[create_key_req.usage].desc,
  3274. QSEECOM_KEY_ID_SIZE);
  3275. memcpy((void *)set_key_ireq.hash32, (void *)create_key_req.hash32,
  3276. QSEECOM_HASH_SIZE);
  3277. ret = __qseecom_set_clear_ce_key(data, create_key_req.usage,
  3278. &set_key_ireq);
  3279. if (ret) {
  3280. pr_err("Failed to create key: pipe %d, ce %d: %d\n",
  3281. pipe, ce_hw, ret);
  3282. return ret;
  3283. }
  3284. return ret;
  3285. }
  3286. static int qseecom_wipe_key(struct qseecom_dev_handle *data,
  3287. void __user *argp)
  3288. {
  3289. uint32_t ce_hw = 0;
  3290. uint32_t pipe = 0;
  3291. int ret = 0;
  3292. uint32_t flags = 0;
  3293. int i;
  3294. struct qseecom_wipe_key_req wipe_key_req;
  3295. struct qseecom_key_delete_ireq delete_key_ireq;
  3296. struct qseecom_key_select_ireq clear_key_ireq;
  3297. ret = copy_from_user(&wipe_key_req, argp, sizeof(wipe_key_req));
  3298. if (ret) {
  3299. pr_err("copy_from_user failed\n");
  3300. return ret;
  3301. }
  3302. if (wipe_key_req.usage < QSEOS_KM_USAGE_DISK_ENCRYPTION ||
  3303. wipe_key_req.usage >= QSEOS_KM_USAGE_MAX) {
  3304. pr_err("Error:: unsupported usage %d\n", wipe_key_req.usage);
  3305. return -EFAULT;
  3306. }
  3307. ret = __qseecom_get_ce_pipe_info(wipe_key_req.usage, &pipe, &ce_hw);
  3308. if (ret) {
  3309. pr_err("Failed to retrieve pipe/ce_hw info: %d\n", ret);
  3310. return -EINVAL;
  3311. }
  3312. if (wipe_key_req.wipe_key_flag) {
  3313. delete_key_ireq.flags = flags;
  3314. delete_key_ireq.qsee_command_id = QSEOS_DELETE_KEY;
  3315. memset((void *)delete_key_ireq.key_id, 0, QSEECOM_KEY_ID_SIZE);
  3316. memcpy((void *)delete_key_ireq.key_id,
  3317. (void *)key_id_array[wipe_key_req.usage].desc,
  3318. QSEECOM_KEY_ID_SIZE);
  3319. memset((void *)delete_key_ireq.hash32, 0, QSEECOM_HASH_SIZE);
  3320. ret = __qseecom_delete_saved_key(data, wipe_key_req.usage,
  3321. &delete_key_ireq);
  3322. if (ret) {
  3323. pr_err("Failed to delete key from ssd storage: %d\n",
  3324. ret);
  3325. return -EFAULT;
  3326. }
  3327. }
  3328. clear_key_ireq.qsee_command_id = QSEOS_SET_KEY;
  3329. clear_key_ireq.ce = ce_hw;
  3330. clear_key_ireq.pipe = pipe;
  3331. clear_key_ireq.flags = flags;
  3332. clear_key_ireq.pipe_type = QSEOS_PIPE_ENC|QSEOS_PIPE_ENC_XTS;
  3333. for (i = 0; i < QSEECOM_KEY_ID_SIZE; i++)
  3334. clear_key_ireq.key_id[i] = QSEECOM_INVALID_KEY_ID;
  3335. memset((void *)clear_key_ireq.hash32, 0, QSEECOM_HASH_SIZE);
  3336. ret = __qseecom_set_clear_ce_key(data, wipe_key_req.usage,
  3337. &clear_key_ireq);
  3338. if (ret) {
  3339. pr_err("Failed to wipe key: pipe %d, ce %d: %d\n",
  3340. pipe, ce_hw, ret);
  3341. return -EFAULT;
  3342. }
  3343. return ret;
  3344. }
  3345. static int qseecom_update_key_user_info(struct qseecom_dev_handle *data,
  3346. void __user *argp)
  3347. {
  3348. int ret = 0;
  3349. uint32_t flags = 0;
  3350. struct qseecom_update_key_userinfo_req update_key_req;
  3351. struct qseecom_key_userinfo_update_ireq ireq;
  3352. ret = copy_from_user(&update_key_req, argp, sizeof(update_key_req));
  3353. if (ret) {
  3354. pr_err("copy_from_user failed\n");
  3355. return ret;
  3356. }
  3357. if (update_key_req.usage < QSEOS_KM_USAGE_DISK_ENCRYPTION ||
  3358. update_key_req.usage >= QSEOS_KM_USAGE_MAX) {
  3359. pr_err("Error:: unsupported usage %d\n", update_key_req.usage);
  3360. return -EFAULT;
  3361. }
  3362. ireq.qsee_command_id = QSEOS_UPDATE_KEY_USERINFO;
  3363. ireq.flags = flags;
  3364. memset(ireq.key_id, 0, QSEECOM_KEY_ID_SIZE);
  3365. memset((void *)ireq.current_hash32, 0, QSEECOM_HASH_SIZE);
  3366. memset((void *)ireq.new_hash32, 0, QSEECOM_HASH_SIZE);
  3367. memcpy((void *)ireq.key_id,
  3368. (void *)key_id_array[update_key_req.usage].desc,
  3369. QSEECOM_KEY_ID_SIZE);
  3370. memcpy((void *)ireq.current_hash32,
  3371. (void *)update_key_req.current_hash32, QSEECOM_HASH_SIZE);
  3372. memcpy((void *)ireq.new_hash32,
  3373. (void *)update_key_req.new_hash32, QSEECOM_HASH_SIZE);
  3374. ret = __qseecom_update_current_key_user_info(data, update_key_req.usage,
  3375. &ireq);
  3376. if (ret) {
  3377. pr_err("Failed to update key info: %d\n", ret);
  3378. return ret;
  3379. }
  3380. return ret;
  3381. }
  3382. static int qseecom_is_es_activated(void __user *argp)
  3383. {
  3384. struct qseecom_is_es_activated_req req;
  3385. int ret;
  3386. int resp_buf;
  3387. if (qseecom.qsee_version < QSEE_VERSION_04) {
  3388. pr_err("invalid qsee version");
  3389. return -ENODEV;
  3390. }
  3391. if (argp == NULL) {
  3392. pr_err("arg is null");
  3393. return -EINVAL;
  3394. }
  3395. ret = scm_call(SCM_SVC_ES, SCM_IS_ACTIVATED_ID, NULL, 0,
  3396. (void *) &resp_buf, sizeof(resp_buf));
  3397. if (ret) {
  3398. pr_err("scm_call failed");
  3399. return ret;
  3400. }
  3401. req.is_activated = resp_buf;
  3402. ret = copy_to_user(argp, &req, sizeof(req));
  3403. if (ret) {
  3404. pr_err("copy_to_user failed");
  3405. return ret;
  3406. }
  3407. return 0;
  3408. }
  3409. static int qseecom_save_partition_hash(void __user *argp)
  3410. {
  3411. struct qseecom_save_partition_hash_req req;
  3412. int ret;
  3413. if (qseecom.qsee_version < QSEE_VERSION_04) {
  3414. pr_err("invalid qsee version ");
  3415. return -ENODEV;
  3416. }
  3417. if (argp == NULL) {
  3418. pr_err("arg is null");
  3419. return -EINVAL;
  3420. }
  3421. ret = copy_from_user(&req, argp, sizeof(req));
  3422. if (ret) {
  3423. pr_err("copy_from_user failed");
  3424. return ret;
  3425. }
  3426. ret = scm_call(SCM_SVC_ES, SCM_SAVE_PARTITION_HASH_ID,
  3427. (void *) &req, sizeof(req), NULL, 0);
  3428. if (ret) {
  3429. pr_err("qseecom_scm_call failed");
  3430. return ret;
  3431. }
  3432. return 0;
  3433. }
  3434. static long qseecom_ioctl(struct file *file, unsigned cmd,
  3435. unsigned long arg)
  3436. {
  3437. int ret = 0;
  3438. struct qseecom_dev_handle *data = file->private_data;
  3439. void __user *argp = (void __user *) arg;
  3440. bool perf_enabled = false;
  3441. if (!data) {
  3442. pr_err("Invalid/uninitialized device handle\n");
  3443. return -EINVAL;
  3444. }
  3445. if (data->abort) {
  3446. pr_err("Aborting qseecom driver\n");
  3447. return -ENODEV;
  3448. }
  3449. switch (cmd) {
  3450. case QSEECOM_IOCTL_REGISTER_LISTENER_REQ: {
  3451. if (data->type != QSEECOM_GENERIC) {
  3452. pr_err("reg lstnr req: invalid handle (%d)\n",
  3453. data->type);
  3454. ret = -EINVAL;
  3455. break;
  3456. }
  3457. pr_debug("ioctl register_listener_req()\n");
  3458. atomic_inc(&data->ioctl_count);
  3459. data->type = QSEECOM_LISTENER_SERVICE;
  3460. ret = qseecom_register_listener(data, argp);
  3461. atomic_dec(&data->ioctl_count);
  3462. wake_up_all(&data->abort_wq);
  3463. if (ret)
  3464. pr_err("failed qseecom_register_listener: %d\n", ret);
  3465. break;
  3466. }
  3467. case QSEECOM_IOCTL_UNREGISTER_LISTENER_REQ: {
  3468. if ((data->listener.id == 0) ||
  3469. (data->type != QSEECOM_LISTENER_SERVICE)) {
  3470. pr_err("unreg lstnr req: invalid handle (%d) lid(%d)\n",
  3471. data->type, data->listener.id);
  3472. ret = -EINVAL;
  3473. break;
  3474. }
  3475. pr_debug("ioctl unregister_listener_req()\n");
  3476. atomic_inc(&data->ioctl_count);
  3477. ret = qseecom_unregister_listener(data);
  3478. atomic_dec(&data->ioctl_count);
  3479. wake_up_all(&data->abort_wq);
  3480. if (ret)
  3481. pr_err("failed qseecom_unregister_listener: %d\n", ret);
  3482. break;
  3483. }
  3484. case QSEECOM_IOCTL_SEND_CMD_REQ: {
  3485. pr_debug("qseecom.current_mode %d\n", qseecom.current_mode);
  3486. if ((data->client.app_id == 0) ||
  3487. (data->type != QSEECOM_CLIENT_APP)) {
  3488. pr_err("send cmd req: invalid handle (%d) app_id(%d)\n",
  3489. data->type, data->client.app_id);
  3490. ret = -EINVAL;
  3491. break;
  3492. }
  3493. /* Only one client allowed here at a time */
  3494. mutex_lock(&app_access_lock);
  3495. if (qseecom.support_bus_scaling) {
  3496. /* register bus bw in case the client doesn't do it */
  3497. if (!data->mode) {
  3498. mutex_lock(&qsee_bw_mutex);
  3499. __qseecom_register_bus_bandwidth_needs(
  3500. data, HIGH);
  3501. mutex_unlock(&qsee_bw_mutex);
  3502. }
  3503. ret = qseecom_scale_bus_bandwidth_timer(INACTIVE);
  3504. if (ret) {
  3505. pr_err("Failed to set bw.\n");
  3506. ret = -EINVAL;
  3507. mutex_unlock(&app_access_lock);
  3508. break;
  3509. }
  3510. }
  3511. /*
  3512. * On targets where crypto clock is handled by HLOS,
  3513. * if clk_access_cnt is zero and perf_enabled is false,
  3514. * then the crypto clock was not enabled before sending cmd
  3515. * to tz, qseecom will enable the clock to avoid service failure.
  3516. */
  3517. if (!qseecom.qsee.clk_access_cnt && !data->perf_enabled) {
  3518. pr_debug("ce clock is not enabled!\n");
  3519. ret = qseecom_perf_enable(data);
  3520. if (ret) {
  3521. pr_err("Failed to vote for clock with err %d\n",
  3522. ret);
  3523. mutex_unlock(&app_access_lock);
  3524. ret = -EINVAL;
  3525. break;
  3526. }
  3527. perf_enabled = true;
  3528. }
  3529. atomic_inc(&data->ioctl_count);
  3530. ret = qseecom_send_cmd(data, argp);
  3531. if (qseecom.support_bus_scaling)
  3532. __qseecom_add_bw_scale_down_timer(
  3533. QSEECOM_SEND_CMD_CRYPTO_TIMEOUT);
  3534. if (perf_enabled) {
  3535. qsee_disable_clock_vote(data, CLK_DFAB);
  3536. qsee_disable_clock_vote(data, CLK_SFPB);
  3537. }
  3538. atomic_dec(&data->ioctl_count);
  3539. wake_up_all(&data->abort_wq);
  3540. mutex_unlock(&app_access_lock);
  3541. if (ret)
  3542. pr_err("failed qseecom_send_cmd: %d\n", ret);
  3543. break;
  3544. }
  3545. case QSEECOM_IOCTL_SEND_MODFD_CMD_REQ: {
  3546. pr_debug("qseecom.current_mode %d\n", qseecom.current_mode);
  3547. if ((data->client.app_id == 0) ||
  3548. (data->type != QSEECOM_CLIENT_APP)) {
  3549. pr_err("send mdfd cmd: invalid handle (%d) appid(%d)\n",
  3550. data->type, data->client.app_id);
  3551. ret = -EINVAL;
  3552. break;
  3553. }
  3554. /* Only one client allowed here at a time */
  3555. mutex_lock(&app_access_lock);
  3556. if (qseecom.support_bus_scaling) {
  3557. if (!data->mode) {
  3558. mutex_lock(&qsee_bw_mutex);
  3559. __qseecom_register_bus_bandwidth_needs(
  3560. data, HIGH);
  3561. mutex_unlock(&qsee_bw_mutex);
  3562. }
  3563. ret = qseecom_scale_bus_bandwidth_timer(INACTIVE);
  3564. if (ret) {
  3565. pr_err("Failed to set bw.\n");
  3566. mutex_unlock(&app_access_lock);
  3567. ret = -EINVAL;
  3568. break;
  3569. }
  3570. }
  3571. /*
  3572. * On targets where crypto clock is handled by HLOS,
  3573. * if clk_access_cnt is zero and perf_enabled is false,
  3574. * then the crypto clock was not enabled before sending cmd
  3575. * to tz, qseecom will enable the clock to avoid service failure.
  3576. */
  3577. if (!qseecom.qsee.clk_access_cnt && !data->perf_enabled) {
  3578. pr_debug("ce clock is not enabled!\n");
  3579. ret = qseecom_perf_enable(data);
  3580. if (ret) {
  3581. pr_err("Failed to vote for clock with err %d\n",
  3582. ret);
  3583. mutex_unlock(&app_access_lock);
  3584. ret = -EINVAL;
  3585. break;
  3586. }
  3587. perf_enabled = true;
  3588. }
  3589. atomic_inc(&data->ioctl_count);
  3590. ret = qseecom_send_modfd_cmd(data, argp);
  3591. if (qseecom.support_bus_scaling)
  3592. __qseecom_add_bw_scale_down_timer(
  3593. QSEECOM_SEND_CMD_CRYPTO_TIMEOUT);
  3594. if (perf_enabled) {
  3595. qsee_disable_clock_vote(data, CLK_DFAB);
  3596. qsee_disable_clock_vote(data, CLK_SFPB);
  3597. }
  3598. atomic_dec(&data->ioctl_count);
  3599. wake_up_all(&data->abort_wq);
  3600. mutex_unlock(&app_access_lock);
  3601. if (ret)
  3602. pr_err("failed qseecom_send_cmd: %d\n", ret);
  3603. break;
  3604. }
  3605. case QSEECOM_IOCTL_RECEIVE_REQ: {
  3606. if ((data->listener.id == 0) ||
  3607. (data->type != QSEECOM_LISTENER_SERVICE)) {
  3608. pr_err("receive req: invalid handle (%d), lid(%d)\n",
  3609. data->type, data->listener.id);
  3610. ret = -EINVAL;
  3611. break;
  3612. }
  3613. atomic_inc(&data->ioctl_count);
  3614. ret = qseecom_receive_req(data);
  3615. atomic_dec(&data->ioctl_count);
  3616. wake_up_all(&data->abort_wq);
  3617. if (ret && (ret != -ERESTARTSYS))
  3618. pr_err("failed qseecom_receive_req: %d\n", ret);
  3619. break;
  3620. }
  3621. case QSEECOM_IOCTL_SEND_RESP_REQ: {
  3622. if ((data->listener.id == 0) ||
  3623. (data->type != QSEECOM_LISTENER_SERVICE)) {
  3624. pr_err("send resp req: invalid handle (%d), lid(%d)\n",
  3625. data->type, data->listener.id);
  3626. ret = -EINVAL;
  3627. break;
  3628. }
  3629. atomic_inc(&data->ioctl_count);
  3630. ret = qseecom_send_resp();
  3631. atomic_dec(&data->ioctl_count);
  3632. wake_up_all(&data->abort_wq);
  3633. if (ret)
  3634. pr_err("failed qseecom_send_resp: %d\n", ret);
  3635. break;
  3636. }
  3637. case QSEECOM_IOCTL_SET_MEM_PARAM_REQ: {
  3638. if ((data->type != QSEECOM_CLIENT_APP) &&
  3639. (data->type != QSEECOM_GENERIC) &&
  3640. (data->type != QSEECOM_SECURE_SERVICE)) {
  3641. pr_err("set mem param req: invalid handle (%d)\n",
  3642. data->type);
  3643. ret = -EINVAL;
  3644. break;
  3645. }
  3646. pr_debug("SET_MEM_PARAM: qseecom addr = 0x%pK\n", data);
  3647. mutex_lock(&app_access_lock);
  3648. atomic_inc(&data->ioctl_count);
  3649. ret = qseecom_set_client_mem_param(data, argp);
  3650. atomic_dec(&data->ioctl_count);
  3651. mutex_unlock(&app_access_lock);
  3652. if (ret)
  3653. pr_err("failed Qqseecom_set_mem_param request: %d\n",
  3654. ret);
  3655. break;
  3656. }
  3657. case QSEECOM_IOCTL_LOAD_APP_REQ: {
  3658. if ((data->type != QSEECOM_GENERIC) &&
  3659. (data->type != QSEECOM_CLIENT_APP)) {
  3660. pr_err("load app req: invalid handle (%d)\n",
  3661. data->type);
  3662. ret = -EINVAL;
  3663. break;
  3664. }
  3665. data->type = QSEECOM_CLIENT_APP;
  3666. pr_debug("LOAD_APP_REQ: qseecom_addr = 0x%pK\n", data);
  3667. mutex_lock(&app_access_lock);
  3668. atomic_inc(&data->ioctl_count);
  3669. if (qseecom.qsee_version > QSEEE_VERSION_00) {
  3670. if (qseecom.commonlib_loaded == false) {
  3671. ret = qseecom_load_commonlib_image(data);
  3672. if (ret == 0)
  3673. qseecom.commonlib_loaded = true;
  3674. }
  3675. }
  3676. if (ret == 0)
  3677. ret = qseecom_load_app(data, argp);
  3678. atomic_dec(&data->ioctl_count);
  3679. mutex_unlock(&app_access_lock);
  3680. if (ret)
  3681. pr_err("failed load_app request: %d\n", ret);
  3682. break;
  3683. }
  3684. case QSEECOM_IOCTL_UNLOAD_APP_REQ: {
  3685. if ((data->client.app_id == 0) ||
  3686. (data->type != QSEECOM_CLIENT_APP)) {
  3687. pr_err("unload app req:invalid handle(%d) app_id(%d)\n",
  3688. data->type, data->client.app_id);
  3689. ret = -EINVAL;
  3690. break;
  3691. }
  3692. pr_debug("UNLOAD_APP: qseecom_addr = 0x%pK\n", data);
  3693. mutex_lock(&app_access_lock);
  3694. atomic_inc(&data->ioctl_count);
  3695. ret = qseecom_unload_app(data, false);
  3696. atomic_dec(&data->ioctl_count);
  3697. mutex_unlock(&app_access_lock);
  3698. if (ret)
  3699. pr_err("failed unload_app request: %d\n", ret);
  3700. break;
  3701. }
  3702. case QSEECOM_IOCTL_GET_QSEOS_VERSION_REQ: {
  3703. atomic_inc(&data->ioctl_count);
  3704. ret = qseecom_get_qseos_version(data, argp);
  3705. if (ret)
  3706. pr_err("qseecom_get_qseos_version: %d\n", ret);
  3707. atomic_dec(&data->ioctl_count);
  3708. break;
  3709. }
  3710. case QSEECOM_IOCTL_PERF_ENABLE_REQ:{
  3711. if ((data->type != QSEECOM_GENERIC) &&
  3712. (data->type != QSEECOM_CLIENT_APP)) {
  3713. pr_err("perf enable req: invalid handle (%d)\n",
  3714. data->type);
  3715. ret = -EINVAL;
  3716. break;
  3717. }
  3718. if ((data->type == QSEECOM_CLIENT_APP) &&
  3719. (data->client.app_id == 0)) {
  3720. pr_err("perf enable req:invalid handle(%d) appid(%d)\n",
  3721. data->type, data->client.app_id);
  3722. ret = -EINVAL;
  3723. break;
  3724. }
  3725. pr_debug("%s : Perf Enable ioctl (Process:%s PID:%d)\n", __func__, \
  3726. current->comm, current->pid);
  3727. atomic_inc(&data->ioctl_count);
  3728. if (qseecom.support_bus_scaling) {
  3729. mutex_lock(&qsee_bw_mutex);
  3730. __qseecom_register_bus_bandwidth_needs(data, HIGH);
  3731. mutex_unlock(&qsee_bw_mutex);
  3732. } else {
  3733. ret = qseecom_perf_enable(data);
  3734. if (ret)
  3735. pr_err("Fail to vote for clocks %d\n", ret);
  3736. }
  3737. atomic_dec(&data->ioctl_count);
  3738. break;
  3739. }
  3740. case QSEECOM_IOCTL_PERF_DISABLE_REQ:{
  3741. if ((data->type != QSEECOM_SECURE_SERVICE) &&
  3742. (data->type != QSEECOM_CLIENT_APP)) {
  3743. pr_err("perf disable req: invalid handle (%d)\n",
  3744. data->type);
  3745. ret = -EINVAL;
  3746. break;
  3747. }
  3748. if ((data->type == QSEECOM_CLIENT_APP) &&
  3749. (data->client.app_id == 0)) {
  3750. pr_err("perf disable: invalid handle (%d)app_id(%d)\n",
  3751. data->type, data->client.app_id);
  3752. ret = -EINVAL;
  3753. break;
  3754. }
  3755. pr_debug("%s : Perf Disable ioctl (Process:%s PID:%d)\n", __func__, \
  3756. current->comm, current->pid);
  3757. atomic_inc(&data->ioctl_count);
  3758. if (!qseecom.support_bus_scaling) {
  3759. qsee_disable_clock_vote(data, CLK_DFAB);
  3760. qsee_disable_clock_vote(data, CLK_SFPB);
  3761. } else {
  3762. mutex_lock(&qsee_bw_mutex);
  3763. qseecom_unregister_bus_bandwidth_needs(data);
  3764. mutex_unlock(&qsee_bw_mutex);
  3765. }
  3766. atomic_dec(&data->ioctl_count);
  3767. break;
  3768. }
  3769. case QSEECOM_IOCTL_SET_BUS_SCALING_REQ: {
  3770. if ((data->client.app_id == 0) ||
  3771. (data->type != QSEECOM_CLIENT_APP)) {
  3772. pr_err("set bus scale: invalid handle (%d) appid(%d)\n",
  3773. data->type, data->client.app_id);
  3774. ret = -EINVAL;
  3775. break;
  3776. }
  3777. atomic_inc(&data->ioctl_count);
  3778. ret = qseecom_scale_bus_bandwidth(data, argp);
  3779. atomic_dec(&data->ioctl_count);
  3780. break;
  3781. }
  3782. case QSEECOM_IOCTL_LOAD_EXTERNAL_ELF_REQ: {
  3783. if (data->type != QSEECOM_GENERIC) {
  3784. pr_err("load ext elf req: invalid client handle (%d)\n",
  3785. data->type);
  3786. ret = -EINVAL;
  3787. break;
  3788. }
  3789. data->type = QSEECOM_UNAVAILABLE_CLIENT_APP;
  3790. data->released = true;
  3791. mutex_lock(&app_access_lock);
  3792. atomic_inc(&data->ioctl_count);
  3793. ret = qseecom_load_external_elf(data, argp);
  3794. atomic_dec(&data->ioctl_count);
  3795. mutex_unlock(&app_access_lock);
  3796. if (ret)
  3797. pr_err("failed load_external_elf request: %d\n", ret);
  3798. break;
  3799. }
  3800. case QSEECOM_IOCTL_UNLOAD_EXTERNAL_ELF_REQ: {
  3801. if (data->type != QSEECOM_UNAVAILABLE_CLIENT_APP) {
  3802. pr_err("unload ext elf req: invalid handle (%d)\n",
  3803. data->type);
  3804. ret = -EINVAL;
  3805. break;
  3806. }
  3807. data->released = true;
  3808. mutex_lock(&app_access_lock);
  3809. atomic_inc(&data->ioctl_count);
  3810. ret = qseecom_unload_external_elf(data);
  3811. atomic_dec(&data->ioctl_count);
  3812. mutex_unlock(&app_access_lock);
  3813. if (ret)
  3814. pr_err("failed unload_app request: %d\n", ret);
  3815. break;
  3816. }
  3817. case QSEECOM_IOCTL_APP_LOADED_QUERY_REQ: {
  3818. if ((data->type != QSEECOM_GENERIC) &&
  3819. (data->type != QSEECOM_CLIENT_APP)) {
  3820. pr_err("app loaded query req: invalid handle (%d)\n",
  3821. data->type);
  3822. ret = -EINVAL;
  3823. break;
  3824. }
  3825. data->type = QSEECOM_CLIENT_APP;
  3826. pr_debug("APP_LOAD_QUERY: qseecom_addr = 0x%pK\n", data);
  3827. mutex_lock(&app_access_lock);
  3828. atomic_inc(&data->ioctl_count);
  3829. pr_debug("APP_LOAD_QUERY: qseecom_addr = 0x%pK\n", data);
  3830. ret = qseecom_query_app_loaded(data, argp);
  3831. atomic_dec(&data->ioctl_count);
  3832. mutex_unlock(&app_access_lock);
  3833. break;
  3834. }
  3835. case QSEECOM_IOCTL_SEND_CMD_SERVICE_REQ: {
  3836. if (data->type != QSEECOM_GENERIC) {
  3837. pr_err("send cmd svc req: invalid handle (%d)\n",
  3838. data->type);
  3839. ret = -EINVAL;
  3840. break;
  3841. }
  3842. data->type = QSEECOM_SECURE_SERVICE;
  3843. if (qseecom.qsee_version < QSEE_VERSION_03) {
  3844. pr_err("SEND_CMD_SERVICE_REQ: Invalid qsee ver %u\n",
  3845. qseecom.qsee_version);
  3846. return -EINVAL;
  3847. }
  3848. mutex_lock(&app_access_lock);
  3849. atomic_inc(&data->ioctl_count);
  3850. ret = qseecom_send_service_cmd(data, argp);
  3851. atomic_dec(&data->ioctl_count);
  3852. mutex_unlock(&app_access_lock);
  3853. break;
  3854. }
  3855. case QSEECOM_IOCTL_CREATE_KEY_REQ: {
  3856. if (!(qseecom.support_pfe || qseecom.support_fde))
  3857. pr_err("Features requiring key init not supported\n");
  3858. if (data->type != QSEECOM_GENERIC) {
  3859. pr_err("create key req: invalid handle (%d)\n",
  3860. data->type);
  3861. ret = -EINVAL;
  3862. break;
  3863. }
  3864. if (qseecom.qsee_version < QSEE_VERSION_05) {
  3865. pr_err("Create Key feature unsupported: qsee ver %u\n",
  3866. qseecom.qsee_version);
  3867. return -EINVAL;
  3868. }
  3869. data->released = true;
  3870. atomic_inc(&data->ioctl_count);
  3871. ret = qseecom_create_key(data, argp);
  3872. if (ret)
  3873. pr_err("failed to create encryption key: %d\n", ret);
  3874. atomic_dec(&data->ioctl_count);
  3875. break;
  3876. }
  3877. case QSEECOM_IOCTL_WIPE_KEY_REQ: {
  3878. if (!(qseecom.support_pfe || qseecom.support_fde))
  3879. pr_err("Features requiring key init not supported\n");
  3880. if (data->type != QSEECOM_GENERIC) {
  3881. pr_err("wipe key req: invalid handle (%d)\n",
  3882. data->type);
  3883. ret = -EINVAL;
  3884. break;
  3885. }
  3886. if (qseecom.qsee_version < QSEE_VERSION_05) {
  3887. pr_err("Wipe Key feature unsupported in qsee ver %u\n",
  3888. qseecom.qsee_version);
  3889. return -EINVAL;
  3890. }
  3891. data->released = true;
  3892. atomic_inc(&data->ioctl_count);
  3893. ret = qseecom_wipe_key(data, argp);
  3894. if (ret)
  3895. pr_err("failed to wipe encryption key: %d\n", ret);
  3896. atomic_dec(&data->ioctl_count);
  3897. break;
  3898. }
  3899. case QSEECOM_IOCTL_UPDATE_KEY_USER_INFO_REQ: {
  3900. if (!(qseecom.support_pfe || qseecom.support_fde))
  3901. pr_err("Features requiring key init not supported\n");
  3902. if (data->type != QSEECOM_GENERIC) {
  3903. pr_err("update key req: invalid handle (%d)\n",
  3904. data->type);
  3905. ret = -EINVAL;
  3906. break;
  3907. }
  3908. if (qseecom.qsee_version < QSEE_VERSION_05) {
  3909. pr_err("Update Key feature unsupported in qsee ver %u\n",
  3910. qseecom.qsee_version);
  3911. return -EINVAL;
  3912. }
  3913. data->released = true;
  3914. atomic_inc(&data->ioctl_count);
  3915. ret = qseecom_update_key_user_info(data, argp);
  3916. if (ret)
  3917. pr_err("failed to update key user info: %d\n", ret);
  3918. atomic_dec(&data->ioctl_count);
  3919. break;
  3920. }
  3921. case QSEECOM_IOCTL_SAVE_PARTITION_HASH_REQ: {
  3922. if (data->type != QSEECOM_GENERIC) {
  3923. pr_err("save part hash req: invalid handle (%d)\n",
  3924. data->type);
  3925. ret = -EINVAL;
  3926. break;
  3927. }
  3928. data->released = true;
  3929. mutex_lock(&app_access_lock);
  3930. atomic_inc(&data->ioctl_count);
  3931. ret = qseecom_save_partition_hash(argp);
  3932. atomic_dec(&data->ioctl_count);
  3933. mutex_unlock(&app_access_lock);
  3934. break;
  3935. }
  3936. case QSEECOM_IOCTL_IS_ES_ACTIVATED_REQ: {
  3937. if (data->type != QSEECOM_GENERIC) {
  3938. pr_err("ES activated req: invalid handle (%d)\n",
  3939. data->type);
  3940. ret = -EINVAL;
  3941. break;
  3942. }
  3943. data->released = true;
  3944. mutex_lock(&app_access_lock);
  3945. atomic_inc(&data->ioctl_count);
  3946. ret = qseecom_is_es_activated(argp);
  3947. atomic_dec(&data->ioctl_count);
  3948. mutex_unlock(&app_access_lock);
  3949. break;
  3950. }
  3951. case QSEECOM_IOCTL_SEND_MODFD_RESP: {
  3952. if ((data->listener.id == 0) ||
  3953. (data->type != QSEECOM_LISTENER_SERVICE)) {
  3954. pr_err("receive req: invalid handle (%d), lid(%d)\n",
  3955. data->type, data->listener.id);
  3956. ret = -EINVAL;
  3957. break;
  3958. }
  3959. /* Only one client allowed here at a time */
  3960. atomic_inc(&data->ioctl_count);
  3961. ret = qseecom_send_modfd_resp(data, argp);
  3962. atomic_dec(&data->ioctl_count);
  3963. wake_up_all(&data->abort_wq);
  3964. if (ret)
  3965. pr_err("failed qseecom_send_mod_resp: %d\n", ret);
  3966. break;
  3967. }
  3968. default:
  3969. pr_err("Invalid IOCTL: %d\n", cmd);
  3970. return -EINVAL;
  3971. }
  3972. return ret;
  3973. }
  3974. static int qseecom_open(struct inode *inode, struct file *file)
  3975. {
  3976. int ret = 0;
  3977. struct qseecom_dev_handle *data;
  3978. data = kzalloc(sizeof(*data), GFP_KERNEL);
  3979. if (!data) {
  3980. pr_err("kmalloc failed\n");
  3981. return -ENOMEM;
  3982. }
  3983. file->private_data = data;
  3984. data->abort = 0;
  3985. data->type = QSEECOM_GENERIC;
  3986. data->released = false;
  3987. memset((void *)data->client.app_name, 0, MAX_APP_NAME_SIZE);
  3988. data->mode = INACTIVE;
  3989. init_waitqueue_head(&data->abort_wq);
  3990. atomic_set(&data->ioctl_count, 0);
  3991. return ret;
  3992. }
  3993. static int qseecom_release(struct inode *inode, struct file *file)
  3994. {
  3995. struct qseecom_dev_handle *data = file->private_data;
  3996. int ret = 0;
  3997. if (data->released == false) {
  3998. pr_debug("data: released = false, type = %d, data = 0x%pK\n",
  3999. data->type, data);
  4000. switch (data->type) {
  4001. case QSEECOM_LISTENER_SERVICE:
  4002. ret = qseecom_unregister_listener(data);
  4003. break;
  4004. case QSEECOM_CLIENT_APP:
  4005. mutex_lock(&app_access_lock);
  4006. ret = qseecom_unload_app(data, true);
  4007. mutex_unlock(&app_access_lock);
  4008. break;
  4009. case QSEECOM_SECURE_SERVICE:
  4010. case QSEECOM_GENERIC:
  4011. ret = qseecom_unmap_ion_allocated_memory(data);
  4012. if (ret)
  4013. pr_err("Ion Unmap failed\n");
  4014. break;
  4015. case QSEECOM_UNAVAILABLE_CLIENT_APP:
  4016. break;
  4017. default:
  4018. pr_err("Unsupported clnt_handle_type %d",
  4019. data->type);
  4020. break;
  4021. }
  4022. }
  4023. if (qseecom.support_bus_scaling) {
  4024. mutex_lock(&qsee_bw_mutex);
  4025. if (data->mode != INACTIVE) {
  4026. qseecom_unregister_bus_bandwidth_needs(data);
  4027. if (qseecom.cumulative_mode == INACTIVE) {
  4028. ret = __qseecom_set_msm_bus_request(INACTIVE);
  4029. if (ret)
  4030. pr_err("Fail to scale down bus\n");
  4031. }
  4032. }
  4033. mutex_unlock(&qsee_bw_mutex);
  4034. } else {
  4035. if (data->fast_load_enabled == true)
  4036. qsee_disable_clock_vote(data, CLK_SFPB);
  4037. if (data->perf_enabled == true)
  4038. qsee_disable_clock_vote(data, CLK_DFAB);
  4039. }
  4040. kfree(data);
  4041. return ret;
  4042. }
  4043. static const struct file_operations qseecom_fops = {
  4044. .owner = THIS_MODULE,
  4045. .unlocked_ioctl = qseecom_ioctl,
  4046. .open = qseecom_open,
  4047. .release = qseecom_release
  4048. };
  4049. static int __qseecom_init_clk(enum qseecom_ce_hw_instance ce)
  4050. {
  4051. int rc = 0;
  4052. struct device *pdev;
  4053. struct qseecom_clk *qclk;
  4054. char *core_clk_src = NULL;
  4055. char *core_clk = NULL;
  4056. char *iface_clk = NULL;
  4057. char *bus_clk = NULL;
  4058. switch (ce) {
  4059. case CLK_QSEE: {
  4060. core_clk_src = "core_clk_src";
  4061. core_clk = "core_clk";
  4062. iface_clk = "iface_clk";
  4063. bus_clk = "bus_clk";
  4064. qclk = &qseecom.qsee;
  4065. qclk->instance = CLK_QSEE;
  4066. break;
  4067. };
  4068. case CLK_CE_DRV: {
  4069. core_clk_src = "ce_drv_core_clk_src";
  4070. core_clk = "ce_drv_core_clk";
  4071. iface_clk = "ce_drv_iface_clk";
  4072. bus_clk = "ce_drv_bus_clk";
  4073. qclk = &qseecom.ce_drv;
  4074. qclk->instance = CLK_CE_DRV;
  4075. break;
  4076. };
  4077. default:
  4078. pr_err("Invalid ce hw instance: %d!\n", ce);
  4079. return -EIO;
  4080. }
  4081. pdev = qseecom.pdev;
  4082. /* Get CE3 src core clk. */
  4083. qclk->ce_core_src_clk = clk_get(pdev, core_clk_src);
  4084. if (!IS_ERR(qclk->ce_core_src_clk)) {
  4085. /* Set the core src clk @100Mhz */
  4086. rc = clk_set_rate(qclk->ce_core_src_clk, QSEE_CE_CLK_100MHZ);
  4087. if (rc) {
  4088. clk_put(qclk->ce_core_src_clk);
  4089. pr_err("Unable to set the core src clk @100Mhz.\n");
  4090. return -EIO;
  4091. }
  4092. } else {
  4093. pr_warn("Unable to get CE core src clk, set to NULL\n");
  4094. qclk->ce_core_src_clk = NULL;
  4095. }
  4096. /* Get CE core clk */
  4097. qclk->ce_core_clk = clk_get(pdev, core_clk);
  4098. if (IS_ERR(qclk->ce_core_clk)) {
  4099. rc = PTR_ERR(qclk->ce_core_clk);
  4100. pr_err("Unable to get CE core clk\n");
  4101. if (qclk->ce_core_src_clk != NULL)
  4102. clk_put(qclk->ce_core_src_clk);
  4103. return -EIO;
  4104. }
  4105. /* Get CE Interface clk */
  4106. qclk->ce_clk = clk_get(pdev, iface_clk);
  4107. if (IS_ERR(qclk->ce_clk)) {
  4108. rc = PTR_ERR(qclk->ce_clk);
  4109. pr_err("Unable to get CE interface clk\n");
  4110. if (qclk->ce_core_src_clk != NULL)
  4111. clk_put(qclk->ce_core_src_clk);
  4112. clk_put(qclk->ce_core_clk);
  4113. return -EIO;
  4114. }
  4115. /* Get CE AXI clk */
  4116. qclk->ce_bus_clk = clk_get(pdev, bus_clk);
  4117. if (IS_ERR(qclk->ce_bus_clk)) {
  4118. rc = PTR_ERR(qclk->ce_bus_clk);
  4119. pr_err("Unable to get CE BUS interface clk\n");
  4120. if (qclk->ce_core_src_clk != NULL)
  4121. clk_put(qclk->ce_core_src_clk);
  4122. clk_put(qclk->ce_core_clk);
  4123. clk_put(qclk->ce_clk);
  4124. return -EIO;
  4125. }
  4126. return rc;
  4127. }
  4128. static void __qseecom_deinit_clk(enum qseecom_ce_hw_instance ce)
  4129. {
  4130. struct qseecom_clk *qclk;
  4131. if (ce == CLK_QSEE)
  4132. qclk = &qseecom.qsee;
  4133. else
  4134. qclk = &qseecom.ce_drv;
  4135. if (qclk->ce_clk != NULL) {
  4136. clk_put(qclk->ce_clk);
  4137. qclk->ce_clk = NULL;
  4138. }
  4139. if (qclk->ce_core_clk != NULL) {
  4140. clk_put(qclk->ce_core_clk);
  4141. qclk->ce_clk = NULL;
  4142. }
  4143. if (qclk->ce_bus_clk != NULL) {
  4144. clk_put(qclk->ce_bus_clk);
  4145. qclk->ce_clk = NULL;
  4146. }
  4147. if (qclk->ce_core_src_clk != NULL) {
  4148. clk_put(qclk->ce_core_src_clk);
  4149. qclk->ce_core_src_clk = NULL;
  4150. }
  4151. }
  4152. static int __devinit qseecom_probe(struct platform_device *pdev)
  4153. {
  4154. int rc;
  4155. int ret = 0;
  4156. struct device *class_dev;
  4157. char qsee_not_legacy = 0;
  4158. struct msm_bus_scale_pdata *qseecom_platform_support = NULL;
  4159. uint32_t system_call_id = QSEOS_CHECK_VERSION_CMD;
  4160. qseecom.qsee_bw_count = 0;
  4161. qseecom.qsee_perf_client = 0;
  4162. qseecom.qsee_sfpb_bw_count = 0;
  4163. qseecom.qsee.ce_core_clk = NULL;
  4164. qseecom.qsee.ce_clk = NULL;
  4165. qseecom.qsee.ce_core_src_clk = NULL;
  4166. qseecom.qsee.ce_bus_clk = NULL;
  4167. qseecom.cumulative_mode = 0;
  4168. qseecom.current_mode = INACTIVE;
  4169. qseecom.support_bus_scaling = false;
  4170. qseecom.support_fde = false;
  4171. qseecom.support_pfe = false;
  4172. qseecom.ce_drv.ce_core_clk = NULL;
  4173. qseecom.ce_drv.ce_clk = NULL;
  4174. qseecom.ce_drv.ce_core_src_clk = NULL;
  4175. qseecom.ce_drv.ce_bus_clk = NULL;
  4176. rc = alloc_chrdev_region(&qseecom_device_no, 0, 1, QSEECOM_DEV);
  4177. if (rc < 0) {
  4178. pr_err("alloc_chrdev_region failed %d\n", rc);
  4179. return rc;
  4180. }
  4181. driver_class = class_create(THIS_MODULE, QSEECOM_DEV);
  4182. if (IS_ERR(driver_class)) {
  4183. rc = -ENOMEM;
  4184. pr_err("class_create failed %d\n", rc);
  4185. goto exit_unreg_chrdev_region;
  4186. }
  4187. class_dev = device_create(driver_class, NULL, qseecom_device_no, NULL,
  4188. QSEECOM_DEV);
  4189. if (!class_dev) {
  4190. pr_err("class_device_create failed %d\n", rc);
  4191. rc = -ENOMEM;
  4192. goto exit_destroy_class;
  4193. }
  4194. cdev_init(&qseecom.cdev, &qseecom_fops);
  4195. qseecom.cdev.owner = THIS_MODULE;
  4196. rc = cdev_add(&qseecom.cdev, MKDEV(MAJOR(qseecom_device_no), 0), 1);
  4197. if (rc < 0) {
  4198. pr_err("cdev_add failed %d\n", rc);
  4199. goto exit_destroy_device;
  4200. }
  4201. INIT_LIST_HEAD(&qseecom.registered_listener_list_head);
  4202. spin_lock_init(&qseecom.registered_listener_list_lock);
  4203. INIT_LIST_HEAD(&qseecom.registered_app_list_head);
  4204. spin_lock_init(&qseecom.registered_app_list_lock);
  4205. INIT_LIST_HEAD(&qseecom.registered_kclient_list_head);
  4206. spin_lock_init(&qseecom.registered_kclient_list_lock);
  4207. init_waitqueue_head(&qseecom.send_resp_wq);
  4208. qseecom.send_resp_flag = 0;
  4209. rc = scm_call(6, 1, &system_call_id, sizeof(system_call_id),
  4210. &qsee_not_legacy, sizeof(qsee_not_legacy));
  4211. if (rc) {
  4212. pr_err("Failed to retrieve QSEOS version information %d\n", rc);
  4213. goto exit_del_cdev;
  4214. }
  4215. if (qsee_not_legacy) {
  4216. uint32_t feature = 10;
  4217. qseecom.qsee_version = QSEEE_VERSION_00;
  4218. rc = scm_call(6, 3, &feature, sizeof(feature),
  4219. &qseecom.qsee_version, sizeof(qseecom.qsee_version));
  4220. if (rc) {
  4221. pr_err("Failed to get QSEE version info %d\n", rc);
  4222. goto exit_del_cdev;
  4223. }
  4224. qseecom.qseos_version = QSEOS_VERSION_14;
  4225. } else {
  4226. pr_err("QSEE legacy version is not supported:");
  4227. pr_err("Support for TZ1.3 and earlier is deprecated\n");
  4228. rc = -EINVAL;
  4229. goto exit_del_cdev;
  4230. }
  4231. qseecom.commonlib_loaded = false;
  4232. qseecom.pdev = class_dev;
  4233. /* Create ION msm client */
  4234. qseecom.ion_clnt = msm_ion_client_create(-1, "qseecom-kernel");
  4235. if (qseecom.ion_clnt == NULL) {
  4236. pr_err("Ion client cannot be created\n");
  4237. rc = -ENOMEM;
  4238. goto exit_del_cdev;
  4239. }
  4240. /* register client for bus scaling */
  4241. if (pdev->dev.of_node) {
  4242. qseecom.pdev->of_node = pdev->dev.of_node;
  4243. qseecom.support_bus_scaling =
  4244. of_property_read_bool((&pdev->dev)->of_node,
  4245. "qcom,support-bus-scaling");
  4246. pr_warn("support_bus_scaling=0x%x",
  4247. qseecom.support_bus_scaling);
  4248. qseecom.support_fde =
  4249. of_property_read_bool((&pdev->dev)->of_node,
  4250. "qcom,support-fde");
  4251. if (qseecom.support_fde) {
  4252. if (of_property_read_u32((&pdev->dev)->of_node,
  4253. "qcom,disk-encrypt-pipe-pair",
  4254. &qseecom.ce_info.disk_encrypt_pipe)) {
  4255. pr_err("Fail to get FDE pipe information.\n");
  4256. rc = -EINVAL;
  4257. goto exit_destroy_ion_client;
  4258. } else {
  4259. pr_warn("disk-encrypt-pipe-pair=0x%x",
  4260. qseecom.ce_info.disk_encrypt_pipe);
  4261. }
  4262. } else {
  4263. pr_warn("Device does not support FDE");
  4264. qseecom.ce_info.disk_encrypt_pipe = 0xff;
  4265. }
  4266. qseecom.support_pfe =
  4267. of_property_read_bool((&pdev->dev)->of_node,
  4268. "qcom,support-pfe");
  4269. if (qseecom.support_pfe) {
  4270. if (of_property_read_u32((&pdev->dev)->of_node,
  4271. "qcom,file-encrypt-pipe-pair",
  4272. &qseecom.ce_info.disk_encrypt_pipe)) {
  4273. pr_err("Fail to get PFE pipe information.\n");
  4274. rc = -EINVAL;
  4275. goto exit_destroy_ion_client;
  4276. } else {
  4277. pr_warn("file-encrypt-pipe-pair=0x%x",
  4278. qseecom.ce_info.file_encrypt_pipe);
  4279. }
  4280. } else {
  4281. pr_warn("Device does not support PFE");
  4282. qseecom.ce_info.file_encrypt_pipe = 0xff;
  4283. }
  4284. if (qseecom.support_pfe || qseecom.support_fde) {
  4285. if (of_property_read_u32((&pdev->dev)->of_node,
  4286. "qcom,hlos-ce-hw-instance",
  4287. &qseecom.ce_info.hlos_ce_hw_instance)) {
  4288. pr_err("Fail: get hlos ce hw instanc info\n");
  4289. rc = -EINVAL;
  4290. goto exit_destroy_ion_client;
  4291. } else {
  4292. pr_warn("hlos-ce-hw-instance=0x%x",
  4293. qseecom.ce_info.hlos_ce_hw_instance);
  4294. }
  4295. } else {
  4296. pr_warn("Device does not support PFE/FDE");
  4297. qseecom.ce_info.hlos_ce_hw_instance = 0xff;
  4298. }
  4299. if (of_property_read_u32((&pdev->dev)->of_node,
  4300. "qcom,qsee-ce-hw-instance",
  4301. &qseecom.ce_info.qsee_ce_hw_instance)) {
  4302. pr_err("Fail to get qsee ce hw instance information.\n");
  4303. rc = -EINVAL;
  4304. goto exit_destroy_ion_client;
  4305. } else {
  4306. pr_warn("qsee-ce-hw-instance=0x%x",
  4307. qseecom.ce_info.qsee_ce_hw_instance);
  4308. }
  4309. qseecom.appsbl_qseecom_support =
  4310. of_property_read_bool((&pdev->dev)->of_node,
  4311. "qcom,appsbl-qseecom-support");
  4312. pr_info("qseecom.appsbl_qseecom_support = 0x%x",
  4313. qseecom.appsbl_qseecom_support);
  4314. qseecom.qsee.instance = qseecom.ce_info.qsee_ce_hw_instance;
  4315. qseecom.ce_drv.instance = qseecom.ce_info.hlos_ce_hw_instance;
  4316. ret = __qseecom_init_clk(CLK_QSEE);
  4317. if (ret)
  4318. goto exit_destroy_ion_client;
  4319. if ((qseecom.qsee.instance != qseecom.ce_drv.instance) &&
  4320. (qseecom.support_pfe || qseecom.support_fde)) {
  4321. ret = __qseecom_init_clk(CLK_CE_DRV);
  4322. if (ret) {
  4323. __qseecom_deinit_clk(CLK_QSEE);
  4324. goto exit_destroy_ion_client;
  4325. }
  4326. } else {
  4327. struct qseecom_clk *qclk;
  4328. qclk = &qseecom.qsee;
  4329. qseecom.ce_drv.ce_core_clk = qclk->ce_core_clk;
  4330. qseecom.ce_drv.ce_clk = qclk->ce_clk;
  4331. qseecom.ce_drv.ce_core_src_clk = qclk->ce_core_src_clk;
  4332. qseecom.ce_drv.ce_bus_clk = qclk->ce_bus_clk;
  4333. }
  4334. qseecom_platform_support = (struct msm_bus_scale_pdata *)
  4335. msm_bus_cl_get_pdata(pdev);
  4336. if (qseecom.qsee_version >= (QSEE_VERSION_02) &&
  4337. !qseecom.appsbl_qseecom_support) {
  4338. struct resource *resource = NULL;
  4339. struct qsee_apps_region_info_ireq req;
  4340. struct qseecom_command_scm_resp resp;
  4341. resource = platform_get_resource_byname(pdev,
  4342. IORESOURCE_MEM, "secapp-region");
  4343. if (resource) {
  4344. req.qsee_cmd_id = QSEOS_APP_REGION_NOTIFICATION;
  4345. req.addr = resource->start;
  4346. req.size = resource_size(resource);
  4347. pr_warn("secure app region addr=0x%x size=0x%x",
  4348. req.addr, req.size);
  4349. #ifdef CONFIG_SEC_DEBUG
  4350. sec_debug_secure_app_addr_size(req.addr, req.size);
  4351. #endif
  4352. } else {
  4353. pr_err("Fail to get secure app region info\n");
  4354. rc = -EINVAL;
  4355. goto exit_destroy_ion_client;
  4356. }
  4357. rc = scm_call(SCM_SVC_TZSCHEDULER, 1, &req, sizeof(req),
  4358. &resp, sizeof(resp));
  4359. if (rc || (resp.result != QSEOS_RESULT_SUCCESS)) {
  4360. pr_err("send secapp reg fail %d resp.res %d\n",
  4361. rc, resp.result);
  4362. rc = -EINVAL;
  4363. goto exit_destroy_ion_client;
  4364. }
  4365. }
  4366. } else {
  4367. qseecom_platform_support = (struct msm_bus_scale_pdata *)
  4368. pdev->dev.platform_data;
  4369. }
  4370. if (qseecom.support_bus_scaling) {
  4371. init_timer(&(qseecom.bw_scale_down_timer));
  4372. INIT_WORK(&qseecom.bw_inactive_req_ws,
  4373. qseecom_bw_inactive_req_work);
  4374. qseecom.bw_scale_down_timer.function =
  4375. qseecom_scale_bus_bandwidth_timer_callback;
  4376. }
  4377. qseecom.timer_running = false;
  4378. qseecom.qsee_perf_client = msm_bus_scale_register_client(
  4379. qseecom_platform_support);
  4380. if (!qseecom.qsee_perf_client)
  4381. pr_err("Unable to register bus client\n");
  4382. return 0;
  4383. exit_destroy_ion_client:
  4384. ion_client_destroy(qseecom.ion_clnt);
  4385. exit_del_cdev:
  4386. cdev_del(&qseecom.cdev);
  4387. exit_destroy_device:
  4388. device_destroy(driver_class, qseecom_device_no);
  4389. exit_destroy_class:
  4390. class_destroy(driver_class);
  4391. exit_unreg_chrdev_region:
  4392. unregister_chrdev_region(qseecom_device_no, 1);
  4393. return rc;
  4394. }
  4395. static int __devinit qseecom_remove(struct platform_device *pdev)
  4396. {
  4397. struct qseecom_registered_kclient_list *kclient = NULL;
  4398. struct qseecom_registered_kclient_list *kclient_tmp = NULL;
  4399. unsigned long flags = 0;
  4400. int ret = 0;
  4401. spin_lock_irqsave(&qseecom.registered_kclient_list_lock, flags);
  4402. list_for_each_entry_safe(kclient, kclient_tmp,
  4403. &qseecom.registered_kclient_list_head, list) {
  4404. /* Break the loop if client handle is NULL */
  4405. if (!kclient->handle)
  4406. goto exit_free_kclient;
  4407. if (list_empty(&kclient->list))
  4408. goto exit_free_kc_handle;
  4409. list_del(&kclient->list);
  4410. ret = qseecom_unload_app(kclient->handle->dev, false);
  4411. if (!ret) {
  4412. kzfree(kclient->handle->dev);
  4413. kzfree(kclient->handle);
  4414. kzfree(kclient);
  4415. }
  4416. }
  4417. exit_free_kc_handle:
  4418. kzfree(kclient->handle);
  4419. exit_free_kclient:
  4420. kzfree(kclient);
  4421. spin_unlock_irqrestore(&qseecom.registered_kclient_list_lock, flags);
  4422. if (qseecom.qseos_version > QSEEE_VERSION_00)
  4423. qseecom_unload_commonlib_image();
  4424. if (qseecom.qsee_perf_client)
  4425. msm_bus_scale_client_update_request(qseecom.qsee_perf_client,
  4426. 0);
  4427. if (pdev->dev.platform_data != NULL)
  4428. msm_bus_scale_unregister_client(qseecom.qsee_perf_client);
  4429. if (qseecom.support_bus_scaling) {
  4430. cancel_work_sync(&qseecom.bw_inactive_req_ws);
  4431. del_timer_sync(&qseecom.bw_scale_down_timer);
  4432. }
  4433. /* register client for bus scaling */
  4434. if (pdev->dev.of_node) {
  4435. __qseecom_deinit_clk(CLK_QSEE);
  4436. if ((qseecom.qsee.instance != qseecom.ce_drv.instance) &&
  4437. (qseecom.support_pfe || qseecom.support_fde))
  4438. __qseecom_deinit_clk(CLK_CE_DRV);
  4439. }
  4440. ion_client_destroy(qseecom.ion_clnt);
  4441. cdev_del(&qseecom.cdev);
  4442. device_destroy(driver_class, qseecom_device_no);
  4443. class_destroy(driver_class);
  4444. unregister_chrdev_region(qseecom_device_no, 1);
  4445. return ret;
  4446. }
  4447. static int qseecom_suspend(struct platform_device *pdev, pm_message_t state)
  4448. {
  4449. int ret = 0;
  4450. struct qseecom_clk *qclk;
  4451. qclk = &qseecom.qsee;
  4452. mutex_lock(&qsee_bw_mutex);
  4453. mutex_lock(&clk_access_lock);
  4454. if (qseecom.current_mode != INACTIVE) {
  4455. ret = msm_bus_scale_client_update_request(
  4456. qseecom.qsee_perf_client, INACTIVE);
  4457. if (ret)
  4458. pr_err("Fail to scale down bus\n");
  4459. else
  4460. qseecom.current_mode = INACTIVE;
  4461. }
  4462. if (qclk->clk_access_cnt) {
  4463. if (qclk->ce_clk != NULL)
  4464. clk_disable_unprepare(qclk->ce_clk);
  4465. if (qclk->ce_core_clk != NULL)
  4466. clk_disable_unprepare(qclk->ce_core_clk);
  4467. if (qclk->ce_bus_clk != NULL)
  4468. clk_disable_unprepare(qclk->ce_bus_clk);
  4469. }
  4470. del_timer_sync(&(qseecom.bw_scale_down_timer));
  4471. qseecom.timer_running = false;
  4472. mutex_unlock(&clk_access_lock);
  4473. mutex_unlock(&qsee_bw_mutex);
  4474. return 0;
  4475. }
  4476. static int qseecom_resume(struct platform_device *pdev)
  4477. {
  4478. int mode = 0;
  4479. int ret = 0;
  4480. struct qseecom_clk *qclk;
  4481. qclk = &qseecom.qsee;
  4482. mutex_lock(&qsee_bw_mutex);
  4483. mutex_lock(&clk_access_lock);
  4484. if (qseecom.cumulative_mode >= HIGH)
  4485. mode = HIGH;
  4486. else
  4487. mode = qseecom.cumulative_mode;
  4488. if (qseecom.cumulative_mode != INACTIVE) {
  4489. ret = msm_bus_scale_client_update_request(
  4490. qseecom.qsee_perf_client, mode);
  4491. if (ret)
  4492. pr_err("Fail to scale up bus to %d\n", mode);
  4493. else
  4494. qseecom.current_mode = mode;
  4495. }
  4496. if (qclk->clk_access_cnt) {
  4497. ret = clk_prepare_enable(qclk->ce_core_clk);
  4498. if (ret) {
  4499. pr_err("Unable to enable/prepare CE core clk\n");
  4500. qclk->clk_access_cnt = 0;
  4501. goto err;
  4502. }
  4503. ret = clk_prepare_enable(qclk->ce_clk);
  4504. if (ret) {
  4505. pr_err("Unable to enable/prepare CE iface clk\n");
  4506. qclk->clk_access_cnt = 0;
  4507. goto ce_clk_err;
  4508. }
  4509. ret = clk_prepare_enable(qclk->ce_bus_clk);
  4510. if (ret) {
  4511. pr_err("Unable to enable/prepare CE bus clk\n");
  4512. qclk->clk_access_cnt = 0;
  4513. goto ce_bus_clk_err;
  4514. }
  4515. }
  4516. if (qclk->clk_access_cnt || qseecom.cumulative_mode) {
  4517. qseecom.bw_scale_down_timer.expires = jiffies +
  4518. msecs_to_jiffies(QSEECOM_SEND_CMD_CRYPTO_TIMEOUT);
  4519. mod_timer(&(qseecom.bw_scale_down_timer),
  4520. qseecom.bw_scale_down_timer.expires);
  4521. qseecom.timer_running = true;
  4522. }
  4523. mutex_unlock(&clk_access_lock);
  4524. mutex_unlock(&qsee_bw_mutex);
  4525. return 0;
  4526. ce_bus_clk_err:
  4527. clk_disable_unprepare(qclk->ce_clk);
  4528. ce_clk_err:
  4529. clk_disable_unprepare(qclk->ce_core_clk);
  4530. err:
  4531. mutex_unlock(&clk_access_lock);
  4532. mutex_unlock(&qsee_bw_mutex);
  4533. return -EIO;
  4534. }
  4535. static struct of_device_id qseecom_match[] = {
  4536. {
  4537. .compatible = "qcom,qseecom",
  4538. },
  4539. {}
  4540. };
  4541. static struct platform_driver qseecom_plat_driver = {
  4542. .probe = qseecom_probe,
  4543. .remove = qseecom_remove,
  4544. .suspend = qseecom_suspend,
  4545. .resume = qseecom_resume,
  4546. .driver = {
  4547. .name = "qseecom",
  4548. .owner = THIS_MODULE,
  4549. .of_match_table = qseecom_match,
  4550. },
  4551. };
  4552. static int __devinit qseecom_init(void)
  4553. {
  4554. return platform_driver_register(&qseecom_plat_driver);
  4555. }
  4556. static void __devexit qseecom_exit(void)
  4557. {
  4558. platform_driver_unregister(&qseecom_plat_driver);
  4559. }
  4560. MODULE_LICENSE("GPL v2");
  4561. MODULE_DESCRIPTION("Qualcomm Secure Execution Environment Communicator");
  4562. module_init(qseecom_init);
  4563. module_exit(qseecom_exit);