smcinvoke.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "smcinvoke: %s: " fmt, __func__
  7. #include <linux/module.h>
  8. #include <linux/mod_devicetable.h>
  9. #include <linux/device.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/slab.h>
  12. #include <linux/file.h>
  13. #include <linux/fs.h>
  14. #include <linux/anon_inodes.h>
  15. #include <linux/hashtable.h>
  16. #include <linux/cdev.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/dma-buf.h>
  19. #include <linux/delay.h>
  20. #include <linux/kref.h>
  21. #include <linux/signal.h>
  22. #include <linux/msm_ion.h>
  23. #include <linux/mem-buf.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/firmware.h>
  26. #include <linux/version.h>
  27. #if (KERNEL_VERSION(6, 3, 0) <= LINUX_VERSION_CODE)
  28. #include <linux/firmware/qcom/qcom_scm.h>
  29. #else
  30. #include <linux/qcom_scm.h>
  31. #endif
  32. #include <linux/freezer.h>
  33. #include <linux/ratelimit.h>
  34. #include <asm/cacheflush.h>
  35. #include <linux/qtee_shmbridge.h>
  36. #include <linux/kthread.h>
  37. #include "smcinvoke.h"
  38. #include "smcinvoke_object.h"
  39. #include "IClientEnv.h"
  40. #if IS_ENABLED(CONFIG_QSEECOM_PROXY)
  41. #include <linux/qseecom_kernel.h>
  42. #include "misc/qseecom_priv.h"
  43. #else
  44. #include "misc/qseecom_kernel.h"
  45. #endif
  46. #include "misc/qseecomi.h"
  47. #define CREATE_TRACE_POINTS
  48. #include "trace_smcinvoke.h"
  49. #define SMCINVOKE_DEV "smcinvoke"
  50. #define SMCINVOKE_TZ_ROOT_OBJ 1
  51. #define SMCINVOKE_TZ_OBJ_NULL 0
  52. #define SMCINVOKE_ARGS_ALIGN_SIZE (sizeof(uint64_t))
  53. #define SMCINVOKE_NEXT_AVAILABLE_TXN 0
  54. #define SMCINVOKE_REQ_PLACED 1
  55. #define SMCINVOKE_REQ_PROCESSING 2
  56. #define SMCINVOKE_REQ_PROCESSED 3
  57. #define SMCINVOKE_INCREMENT 1
  58. #define SMCINVOKE_DECREMENT 0
  59. #define SMCINVOKE_OBJ_TYPE_TZ_OBJ 0
  60. #define SMCINVOKE_OBJ_TYPE_SERVER 1
  61. #define SMCINVOKE_OBJ_TYPE_TZ_OBJ_FOR_KERNEL 2
  62. #define SMCINVOKE_MEM_MAP_OBJ 0
  63. #define SMCINVOKE_MEM_RGN_OBJ 1
  64. #define SMCINVOKE_MEM_PERM_RW 6
  65. #define SMCINVOKE_SCM_EBUSY_WAIT_MS 30
  66. #define SMCINVOKE_SCM_EBUSY_MAX_RETRY 200
  67. #define TZCB_ERR_RATELIMIT_INTERVAL (1*HZ)
  68. #define TZCB_ERR_RATELIMIT_BURST 1
  69. //print tzcb err per sec
  70. #define tzcb_err_ratelimited(fmt, ...) do { \
  71. static DEFINE_RATELIMIT_STATE(_rs, \
  72. TZCB_ERR_RATELIMIT_INTERVAL, \
  73. TZCB_ERR_RATELIMIT_BURST); \
  74. if (__ratelimit(&_rs)) \
  75. pr_err(fmt, ##__VA_ARGS__); \
  76. } while(0)
  77. /* TZ defined values - Start */
  78. #define SMCINVOKE_INVOKE_PARAM_ID 0x224
  79. #define SMCINVOKE_CB_RSP_PARAM_ID 0x22
  80. #define SMCINVOKE_INVOKE_CMD_LEGACY 0x32000600
  81. #define SMCINVOKE_INVOKE_CMD 0x32000602
  82. #define SMCINVOKE_CB_RSP_CMD 0x32000601
  83. #define SMCINVOKE_RESULT_INBOUND_REQ_NEEDED 3
  84. /* TZ defined values - End */
  85. /* Asynchronous protocol values */
  86. /* Driver async version is set to match the minimal TZ version that supports async memory object */
  87. #define SMCINVOKE_ASYNC_VERSION (0x00010002)
  88. #define SMCINVOKE_ASYNC_OP_MEMORY_OBJECT (0x00000003)
  89. /*
  90. * This is the state when server FD has been closed but
  91. * TZ still has refs of CBOBjs served by this server
  92. */
  93. #define SMCINVOKE_SERVER_STATE_DEFUNCT 1
  94. #define CBOBJ_MAX_RETRIES 50
  95. #define FOR_ARGS(ndxvar, counts, section) \
  96. for (ndxvar = OBJECT_COUNTS_INDEX_##section(counts); \
  97. ndxvar < (OBJECT_COUNTS_INDEX_##section(counts) \
  98. + OBJECT_COUNTS_NUM_##section(counts)); \
  99. ++ndxvar)
  100. #define TZCB_BUF_OFFSET(tzcb_req) (sizeof(tzcb_req->result) + \
  101. sizeof(struct smcinvoke_msg_hdr) + \
  102. sizeof(union smcinvoke_tz_args) * \
  103. OBJECT_COUNTS_TOTAL(tzcb_req->hdr.counts))
  104. /*
  105. * +ve uhandle : either remote obj or mem obj, decided by f_ops
  106. * -ve uhandle : either Obj NULL or CBObj
  107. * - -1: OBJ NULL
  108. * - < -1: CBObj
  109. */
  110. #define UHANDLE_IS_FD(h) ((h) >= 0)
  111. #define UHANDLE_IS_NULL(h) ((h) == SMCINVOKE_USERSPACE_OBJ_NULL)
  112. #define UHANDLE_IS_CB_OBJ(h) (h < SMCINVOKE_USERSPACE_OBJ_NULL)
  113. #define UHANDLE_NULL (SMCINVOKE_USERSPACE_OBJ_NULL)
  114. /*
  115. * MAKE => create handle for other domain i.e. TZ or userspace
  116. * GET => retrieve obj from incoming handle
  117. */
  118. #define UHANDLE_GET_CB_OBJ(h) (-2-(h))
  119. #define UHANDLE_MAKE_CB_OBJ(o) (-2-(o))
  120. #define UHANDLE_GET_FD(h) (h)
  121. /*
  122. * +ve tzhandle : remote object i.e. owned by TZ
  123. * -ve tzhandle : local object i.e. owned by linux
  124. * --------------------------------------------------
  125. *| 1 (1 bit) | Obj Id (15 bits) | srvr id (16 bits) |
  126. * ---------------------------------------------------
  127. * Server ids are defined below for various local objects
  128. * server id 0 : Kernel Obj
  129. * server id 1 : Memory region Obj
  130. * server id 2 : Memory map Obj
  131. * server id 3-15: Reserverd
  132. * server id 16 & up: Callback Objs
  133. */
  134. #define KRNL_SRVR_ID 0
  135. #define MEM_RGN_SRVR_ID 1
  136. #define MEM_MAP_SRVR_ID 2
  137. #define CBOBJ_SERVER_ID_START 0x10
  138. #define CBOBJ_SERVER_ID_END ((1<<16) - 1)
  139. /* local obj id is represented by 15 bits */
  140. #define MAX_LOCAL_OBJ_ID ((1<<15) - 1)
  141. /* CBOBJs will be served by server id 0x10 onwards */
  142. #define TZHANDLE_GET_SERVER(h) ((uint16_t)((h) & 0xFFFF))
  143. #define TZHANDLE_GET_OBJID(h) (((h) >> 16) & 0x7FFF)
  144. #define TZHANDLE_MAKE_LOCAL(s, o) (((0x8000 | (o)) << 16) | s)
  145. #define SET_BIT(s,b) (s | (1 << b))
  146. #define UNSET_BIT(s,b) (s & (~ (1 << b)))
  147. #define TZHANDLE_IS_NULL(h) ((h) == SMCINVOKE_TZ_OBJ_NULL)
  148. #define TZHANDLE_IS_LOCAL(h) ((h) & 0x80000000)
  149. #define TZHANDLE_IS_REMOTE(h) (!TZHANDLE_IS_NULL(h) && !TZHANDLE_IS_LOCAL(h))
  150. #define TZHANDLE_IS_KERNEL_OBJ(h) (TZHANDLE_IS_LOCAL(h) && \
  151. TZHANDLE_GET_SERVER(h) == KRNL_SRVR_ID)
  152. #define TZHANDLE_IS_MEM_RGN_OBJ(h) (TZHANDLE_IS_LOCAL(h) && \
  153. TZHANDLE_GET_SERVER(h) == MEM_RGN_SRVR_ID)
  154. #define TZHANDLE_IS_MEM_MAP_OBJ(h) (TZHANDLE_IS_LOCAL(h) && \
  155. TZHANDLE_GET_SERVER(h) == MEM_MAP_SRVR_ID)
  156. #define TZHANDLE_IS_MEM_OBJ(h) (TZHANDLE_IS_MEM_RGN_OBJ(h) || \
  157. TZHANDLE_IS_MEM_MAP_OBJ(h))
  158. #define TZHANDLE_IS_CB_OBJ(h) (TZHANDLE_IS_LOCAL(h) && \
  159. TZHANDLE_GET_SERVER(h) >= CBOBJ_SERVER_ID_START)
  160. #define FILE_IS_REMOTE_OBJ(f) ((f)->f_op && (f)->f_op == &g_smcinvoke_fops)
  161. static DEFINE_MUTEX(g_smcinvoke_lock);
  162. #define NO_LOCK 0
  163. #define TAKE_LOCK 1
  164. #define MUTEX_LOCK(x) { if (x) mutex_lock(&g_smcinvoke_lock); }
  165. #define MUTEX_UNLOCK(x) { if (x) mutex_unlock(&g_smcinvoke_lock); }
  166. #define POST_KT_SLEEP 0
  167. #define POST_KT_WAKEUP 1
  168. #define MAX_CHAR_NAME 50
  169. enum worker_thread_type {
  170. SHMB_WORKER_THREAD = 0,
  171. OBJECT_WORKER_THREAD,
  172. ADCI_WORKER_THREAD,
  173. MAX_THREAD_NUMBER
  174. };
  175. static DEFINE_HASHTABLE(g_cb_servers, 8);
  176. static LIST_HEAD(g_mem_objs);
  177. static uint16_t g_last_cb_server_id = CBOBJ_SERVER_ID_START;
  178. static uint16_t g_last_mem_rgn_id, g_last_mem_map_obj_id;
  179. static size_t g_max_cb_buf_size = SMCINVOKE_TZ_MIN_BUF_SIZE;
  180. static unsigned int cb_reqs_inflight;
  181. static bool legacy_smc_call;
  182. static int invoke_cmd;
  183. static long smcinvoke_ioctl(struct file *, unsigned int, unsigned long);
  184. static int smcinvoke_open(struct inode *, struct file *);
  185. static int smcinvoke_release(struct inode *, struct file *);
  186. static int release_cb_server(uint16_t);
  187. static const struct file_operations g_smcinvoke_fops = {
  188. .owner = THIS_MODULE,
  189. .unlocked_ioctl = smcinvoke_ioctl,
  190. .compat_ioctl = smcinvoke_ioctl,
  191. .open = smcinvoke_open,
  192. .release = smcinvoke_release,
  193. };
  194. static dev_t smcinvoke_device_no;
  195. static struct cdev smcinvoke_cdev;
  196. static struct class *driver_class;
  197. struct device *class_dev;
  198. static struct platform_device *smcinvoke_pdev;
  199. /* We disable async memory object support by default,
  200. * until we receive the first message from TZ over the
  201. * async channel and can determine TZ async version.
  202. */
  203. static bool mem_obj_async_support = false;
  204. static uint32_t tz_async_version = 0x0;
  205. struct smcinvoke_buf_hdr {
  206. uint32_t offset;
  207. uint32_t size;
  208. };
  209. union smcinvoke_tz_args {
  210. struct smcinvoke_buf_hdr b;
  211. int32_t handle;
  212. };
  213. struct smcinvoke_msg_hdr {
  214. uint32_t tzhandle;
  215. uint32_t op;
  216. uint32_t counts;
  217. };
  218. /* Inbound reqs from TZ */
  219. struct smcinvoke_tzcb_req {
  220. int32_t result;
  221. struct smcinvoke_msg_hdr hdr;
  222. union smcinvoke_tz_args args[0];
  223. };
  224. struct smcinvoke_file_data {
  225. uint32_t context_type;
  226. union {
  227. uint32_t tzhandle;
  228. uint16_t server_id;
  229. };
  230. };
  231. struct smcinvoke_piggyback_msg {
  232. uint32_t version;
  233. uint32_t op;
  234. uint32_t counts;
  235. int32_t objs[0];
  236. };
  237. /* Mapped memory object data
  238. *
  239. * memObjRef Handle reference for the memory object
  240. * mapObjRef Handle reference for the map object
  241. * addr Mapped memory address
  242. * size Size of mapped memory
  243. * perm Access rights for the memory
  244. */
  245. struct smcinvoke_mem_obj_info {
  246. uint32_t memObjRef;
  247. uint32_t mapObjRef;
  248. uint64_t addr;
  249. uint64_t size;
  250. uint32_t perm;
  251. };
  252. /* Memory object info to be written into the async buffer
  253. *
  254. * version Async protocol version
  255. * op Async protocol operation
  256. * count Number of memory objects passed
  257. * mo Mapped memory object data
  258. */
  259. struct smcinvoke_mem_obj_msg {
  260. uint32_t version;
  261. uint32_t op;
  262. uint32_t count;
  263. struct smcinvoke_mem_obj_info mo[];
  264. };
  265. struct smcinvoke_mem_obj_pending_async {
  266. struct smcinvoke_mem_obj *mem_obj;
  267. struct list_head list;
  268. };
  269. /* Data structure to hold request coming from TZ */
  270. struct smcinvoke_cb_txn {
  271. uint32_t txn_id;
  272. int32_t state;
  273. struct smcinvoke_tzcb_req *cb_req;
  274. size_t cb_req_bytes;
  275. struct file **filp_to_release;
  276. struct hlist_node hash;
  277. struct kref ref_cnt;
  278. };
  279. struct smcinvoke_server_info {
  280. uint16_t server_id;
  281. uint16_t state;
  282. uint32_t txn_id;
  283. struct kref ref_cnt;
  284. wait_queue_head_t req_wait_q;
  285. wait_queue_head_t rsp_wait_q;
  286. size_t cb_buf_size;
  287. DECLARE_HASHTABLE(reqs_table, 4);
  288. DECLARE_HASHTABLE(responses_table, 4);
  289. struct hlist_node hash;
  290. struct list_head pending_cbobjs;
  291. uint8_t is_server_suspended;
  292. };
  293. struct smcinvoke_cbobj {
  294. uint16_t cbobj_id;
  295. struct kref ref_cnt;
  296. struct smcinvoke_server_info *server;
  297. struct list_head list;
  298. };
  299. /*
  300. * We require couple of objects, one for mem region & another
  301. * for mapped mem_obj once mem region has been mapped. It is
  302. * possible that TZ can release either independent of other.
  303. */
  304. struct smcinvoke_mem_obj {
  305. /* these ids are objid part of tzhandle */
  306. uint16_t mem_region_id;
  307. uint16_t mem_map_obj_id;
  308. struct dma_buf *dma_buf;
  309. struct dma_buf_attachment *buf_attach;
  310. struct sg_table *sgt;
  311. struct kref mem_regn_ref_cnt;
  312. struct kref mem_map_obj_ref_cnt;
  313. uint64_t p_addr;
  314. size_t p_addr_len;
  315. struct list_head list;
  316. uint64_t shmbridge_handle;
  317. struct smcinvoke_server_info *server;
  318. int32_t mem_obj_user_fd;
  319. };
  320. static LIST_HEAD(g_bridge_postprocess);
  321. DEFINE_MUTEX(bridge_postprocess_lock);
  322. static LIST_HEAD(g_object_postprocess);
  323. DEFINE_MUTEX(object_postprocess_lock);
  324. struct bridge_deregister {
  325. uint64_t shmbridge_handle;
  326. struct dma_buf *dmabuf_to_free;
  327. };
  328. struct object_release {
  329. uint32_t tzhandle;
  330. uint32_t context_type;
  331. };
  332. struct smcinvoke_shmbridge_deregister_pending_list {
  333. struct list_head list;
  334. struct bridge_deregister data;
  335. };
  336. struct smcinvoke_object_release_pending_list {
  337. struct list_head list;
  338. struct object_release data;
  339. };
  340. struct smcinvoke_worker_thread {
  341. enum worker_thread_type type;
  342. atomic_t postprocess_kthread_state;
  343. wait_queue_head_t postprocess_kthread_wq;
  344. struct task_struct *postprocess_kthread_task;
  345. };
  346. static struct smcinvoke_worker_thread smcinvoke[MAX_THREAD_NUMBER];
  347. static const char thread_name[MAX_THREAD_NUMBER][MAX_CHAR_NAME] = {
  348. "smcinvoke_shmbridge_postprocess", "smcinvoke_object_postprocess", "smcinvoke_adci_thread"};
  349. static struct Object adci_rootEnv = Object_NULL;
  350. extern int get_root_obj(struct Object *rootObj);
  351. static int prepare_send_scm_msg(const uint8_t *in_buf, phys_addr_t in_paddr,
  352. size_t in_buf_len,
  353. uint8_t *out_buf, phys_addr_t out_paddr,
  354. size_t out_buf_len,
  355. struct smcinvoke_cmd_req *req,
  356. union smcinvoke_arg *args_buf,
  357. bool *tz_acked, uint32_t context_type,
  358. struct qtee_shm *in_shm, struct qtee_shm *out_shm, bool retry);
  359. static void process_piggyback_data(void *buf, size_t buf_size);
  360. static void add_mem_obj_info_to_async_side_channel_locked(void *buf, size_t buf_size, struct list_head *l_pending_mem_obj);
  361. static void delete_pending_async_list_locked(struct list_head *l_pending_mem_obj);
  362. static void destroy_cb_server(struct kref *kref)
  363. {
  364. struct smcinvoke_server_info *server = container_of(kref,
  365. struct smcinvoke_server_info, ref_cnt);
  366. if (server) {
  367. hash_del(&server->hash);
  368. kfree(server);
  369. }
  370. }
  371. /*
  372. * A separate find func is reqd mainly for couple of cases:
  373. * next_cb_server_id_locked which checks if server id had been utilized or not.
  374. * - It would be overhead if we do ref_cnt for this case
  375. * smcinvoke_release: which is called when server is closed from userspace.
  376. * - During server creation we init ref count, now put it back
  377. */
  378. static struct smcinvoke_server_info *find_cb_server_locked(uint16_t server_id)
  379. {
  380. struct smcinvoke_server_info *data = NULL;
  381. hash_for_each_possible(g_cb_servers, data, hash, server_id) {
  382. if (data->server_id == server_id)
  383. return data;
  384. }
  385. return NULL;
  386. }
  387. static struct smcinvoke_server_info *get_cb_server_locked(uint16_t server_id)
  388. {
  389. struct smcinvoke_server_info *server = find_cb_server_locked(server_id);
  390. if (server)
  391. kref_get(&server->ref_cnt);
  392. return server;
  393. }
  394. static uint16_t next_cb_server_id_locked(void)
  395. {
  396. if (g_last_cb_server_id == CBOBJ_SERVER_ID_END)
  397. g_last_cb_server_id = CBOBJ_SERVER_ID_START;
  398. while (find_cb_server_locked(++g_last_cb_server_id))
  399. ;
  400. return g_last_cb_server_id;
  401. }
  402. static inline void release_filp(struct file **filp_to_release, size_t arr_len)
  403. {
  404. size_t i = 0;
  405. for (i = 0; i < arr_len; i++) {
  406. if (filp_to_release[i]) {
  407. fput(filp_to_release[i]);
  408. filp_to_release[i] = NULL;
  409. }
  410. }
  411. }
  412. static struct smcinvoke_mem_obj *find_mem_obj_locked(uint16_t mem_obj_id,
  413. bool is_mem_rgn_obj)
  414. {
  415. struct smcinvoke_mem_obj *mem_obj = NULL;
  416. if (list_empty(&g_mem_objs))
  417. return NULL;
  418. list_for_each_entry(mem_obj, &g_mem_objs, list) {
  419. if ((is_mem_rgn_obj &&
  420. (mem_obj->mem_region_id == mem_obj_id)) ||
  421. (!is_mem_rgn_obj &&
  422. (mem_obj->mem_map_obj_id == mem_obj_id)))
  423. return mem_obj;
  424. }
  425. return NULL;
  426. }
  427. static uint32_t next_mem_region_obj_id_locked(void)
  428. {
  429. if (g_last_mem_rgn_id == MAX_LOCAL_OBJ_ID)
  430. g_last_mem_rgn_id = 0;
  431. while (find_mem_obj_locked(++g_last_mem_rgn_id, SMCINVOKE_MEM_RGN_OBJ))
  432. ;
  433. return g_last_mem_rgn_id;
  434. }
  435. static uint32_t next_mem_map_obj_id_locked(void)
  436. {
  437. if (g_last_mem_map_obj_id == MAX_LOCAL_OBJ_ID)
  438. g_last_mem_map_obj_id = 0;
  439. while (find_mem_obj_locked(++g_last_mem_map_obj_id,
  440. SMCINVOKE_MEM_MAP_OBJ))
  441. ;
  442. return g_last_mem_map_obj_id;
  443. }
  444. static void smcinvoke_shmbridge_post_process(void)
  445. {
  446. struct smcinvoke_shmbridge_deregister_pending_list *entry = NULL;
  447. struct list_head *pos;
  448. int ret = 0;
  449. uint64_t handle = 0;
  450. struct dma_buf *dmabuf_to_free = NULL;
  451. do {
  452. mutex_lock(&bridge_postprocess_lock);
  453. if (list_empty(&g_bridge_postprocess)) {
  454. mutex_unlock(&bridge_postprocess_lock);
  455. break;
  456. }
  457. pos = g_bridge_postprocess.next;
  458. entry = list_entry(pos,
  459. struct smcinvoke_shmbridge_deregister_pending_list,
  460. list);
  461. if (entry) {
  462. handle = entry->data.shmbridge_handle;
  463. dmabuf_to_free = entry->data.dmabuf_to_free;
  464. } else {
  465. pr_err("entry is NULL, pos:%#llx\n", (uint64_t)pos);
  466. }
  467. list_del(pos);
  468. kfree_sensitive(entry);
  469. mutex_unlock(&bridge_postprocess_lock);
  470. if (entry) {
  471. do {
  472. ret = qtee_shmbridge_deregister(handle);
  473. if (unlikely(ret)) {
  474. pr_err_ratelimited("SHM failed: ret:%d ptr:0x%p h:%#llx\n",
  475. ret,
  476. dmabuf_to_free,
  477. handle);
  478. } else {
  479. pr_debug("SHM deletion: Handle:%#llx\n",
  480. handle);
  481. dma_buf_put(dmabuf_to_free);
  482. }
  483. } while (-EBUSY == ret);
  484. }
  485. } while (1);
  486. }
  487. static int smcinvoke_release_tz_object(struct qtee_shm *in_shm, struct qtee_shm *out_shm,
  488. uint32_t tzhandle, uint32_t context_type)
  489. {
  490. int ret = 0;
  491. bool release_handles;
  492. uint8_t *in_buf = NULL;
  493. uint8_t *out_buf = NULL;
  494. struct smcinvoke_msg_hdr hdr = {0};
  495. struct smcinvoke_cmd_req req = {0};
  496. in_buf = in_shm->vaddr;
  497. out_buf = out_shm->vaddr;
  498. hdr.tzhandle = tzhandle;
  499. hdr.op = OBJECT_OP_RELEASE;
  500. hdr.counts = 0;
  501. *(struct smcinvoke_msg_hdr *)in_buf = hdr;
  502. ret = prepare_send_scm_msg(in_buf, in_shm->paddr,
  503. SMCINVOKE_TZ_MIN_BUF_SIZE, out_buf, out_shm->paddr,
  504. SMCINVOKE_TZ_MIN_BUF_SIZE, &req, NULL,
  505. &release_handles, context_type, in_shm, out_shm, false);
  506. process_piggyback_data(out_buf, SMCINVOKE_TZ_MIN_BUF_SIZE);
  507. if (ret) {
  508. pr_err_ratelimited("Failed to release object(0x%x), ret:%d\n",
  509. hdr.tzhandle, ret);
  510. } else {
  511. pr_debug("Released object(0x%x) successfully.\n",
  512. hdr.tzhandle);
  513. }
  514. return ret;
  515. }
  516. static int smcinvoke_object_post_process(void)
  517. {
  518. struct smcinvoke_object_release_pending_list *entry = NULL;
  519. struct list_head *pos;
  520. int ret = 0;
  521. struct qtee_shm in_shm = {0}, out_shm = {0};
  522. ret = qtee_shmbridge_allocate_shm(SMCINVOKE_TZ_MIN_BUF_SIZE, &in_shm);
  523. if (ret) {
  524. ret = -ENOMEM;
  525. pr_err("shmbridge alloc failed for in msg in object release\n");
  526. goto out;
  527. }
  528. ret = qtee_shmbridge_allocate_shm(SMCINVOKE_TZ_MIN_BUF_SIZE, &out_shm);
  529. if (ret) {
  530. ret = -ENOMEM;
  531. pr_err("shmbridge alloc failed for out msg in object release\n");
  532. goto out;
  533. }
  534. do {
  535. mutex_lock(&object_postprocess_lock);
  536. if (list_empty(&g_object_postprocess)) {
  537. mutex_unlock(&object_postprocess_lock);
  538. break;
  539. }
  540. pos = g_object_postprocess.next;
  541. entry = list_entry(pos, struct smcinvoke_object_release_pending_list, list);
  542. list_del(pos);
  543. mutex_unlock(&object_postprocess_lock);
  544. if (entry) {
  545. do {
  546. ret = smcinvoke_release_tz_object(&in_shm, &out_shm,
  547. entry->data.tzhandle, entry->data.context_type);
  548. } while (-EBUSY == ret);
  549. } else {
  550. pr_err("entry is NULL, pos:%#llx\n", (uint64_t)pos);
  551. }
  552. kfree_sensitive(entry);
  553. } while (1);
  554. out:
  555. qtee_shmbridge_free_shm(&in_shm);
  556. qtee_shmbridge_free_shm(&out_shm);
  557. return ret;
  558. }
  559. static void smcinvoke_start_adci_thread(void)
  560. {
  561. int32_t ret = OBJECT_ERROR;
  562. int retry_count = 0;
  563. ret = get_root_obj(&adci_rootEnv);
  564. if (ret) {
  565. pr_err("failed to get rootEnv for ADCI invoke thread. ret = %d\n", ret);
  566. /* Marking it Object_NULL in case of failure scenario in order to avoid
  567. * undefined behavior while relasing garbage adci_rootEnv object. */
  568. adci_rootEnv = Object_NULL;
  569. goto out;
  570. }
  571. /* Invoke call to QTEE which should never return if ADCI is supported */
  572. pr_debug("Invoking adciAccept method in QTEE\n");
  573. do {
  574. ret = IClientEnv_adciAccept(adci_rootEnv);
  575. if (ret == OBJECT_ERROR_BUSY) {
  576. pr_err_ratelimited("Secure side is busy,will retry after 5 ms, retry_count = %d\n",retry_count);
  577. msleep(SMCINVOKE_INTERFACE_BUSY_WAIT_MS);
  578. }
  579. } while ((ret == OBJECT_ERROR_BUSY) && (retry_count++ < SMCINVOKE_INTERFACE_MAX_RETRY));
  580. if (ret == OBJECT_ERROR_INVALID)
  581. pr_err("ADCI feature is not supported on this chipsets, ret = %d\n", ret);
  582. else
  583. pr_debug("Received response from QTEE, ret = %d\n", ret);
  584. out:
  585. /* Control should reach to this point only if ADCI feature is not supported by QTEE
  586. (or) ADCI thread held in QTEE is released. */
  587. Object_ASSIGN_NULL(adci_rootEnv);
  588. }
  589. static void __wakeup_postprocess_kthread(struct smcinvoke_worker_thread *smcinvoke)
  590. {
  591. if (smcinvoke) {
  592. atomic_set(&smcinvoke->postprocess_kthread_state,
  593. POST_KT_WAKEUP);
  594. wake_up_interruptible(&smcinvoke->postprocess_kthread_wq);
  595. } else {
  596. pr_err("Invalid smcinvoke pointer.\n");
  597. }
  598. }
  599. static int smcinvoke_postprocess_kthread_func(void *data)
  600. {
  601. struct smcinvoke_worker_thread *smcinvoke_wrk_trd = data;
  602. static const char *const tag[] = {"shmbridge","object","adci","invalid"};
  603. if (!smcinvoke_wrk_trd) {
  604. pr_err("Bad input.\n");
  605. return -EINVAL;
  606. }
  607. while (!kthread_should_stop()) {
  608. wait_event_interruptible(
  609. smcinvoke_wrk_trd->postprocess_kthread_wq,
  610. kthread_should_stop() ||
  611. (atomic_read(&smcinvoke_wrk_trd->postprocess_kthread_state)
  612. == POST_KT_WAKEUP));
  613. switch (smcinvoke_wrk_trd->type) {
  614. case SHMB_WORKER_THREAD:
  615. pr_debug("kthread to %s postprocess is called %d\n",
  616. tag[SHMB_WORKER_THREAD], atomic_read(&smcinvoke_wrk_trd->postprocess_kthread_state));
  617. smcinvoke_shmbridge_post_process();
  618. break;
  619. case OBJECT_WORKER_THREAD:
  620. pr_debug("kthread to %s postprocess is called %d\n",
  621. tag[OBJECT_WORKER_THREAD], atomic_read(&smcinvoke_wrk_trd->postprocess_kthread_state));
  622. smcinvoke_object_post_process();
  623. break;
  624. case ADCI_WORKER_THREAD:
  625. pr_debug("kthread to %s postprocess is called %d\n",
  626. tag[ADCI_WORKER_THREAD], atomic_read(&smcinvoke_wrk_trd->postprocess_kthread_state));
  627. smcinvoke_start_adci_thread();
  628. break;
  629. default:
  630. pr_err("Invalid thread type(%d), do nothing.\n",
  631. (int)smcinvoke_wrk_trd->type);
  632. break;
  633. }
  634. /* For ADCI thread, if control reaches here, that indicates either ADCI
  635. * thread is not supported (or) released by QTEE. Since ADCI thread is
  636. * getting signaled only during the smcinvoke driver initialization,
  637. * there is no point of putting the thread into sleep state again. All the
  638. * required post-processing will be taken care by object and shmbridge threads.
  639. */
  640. if(smcinvoke_wrk_trd->type == ADCI_WORKER_THREAD) {
  641. break;
  642. }
  643. atomic_set(&smcinvoke_wrk_trd->postprocess_kthread_state,
  644. POST_KT_SLEEP);
  645. }
  646. pr_warn("kthread(worker_thread) processed, worker_thread type is %d \n", smcinvoke_wrk_trd->type);
  647. return 0;
  648. }
  649. static int smcinvoke_create_kthreads(void)
  650. {
  651. int i, rc = 0;
  652. const enum worker_thread_type thread_type[MAX_THREAD_NUMBER] = {
  653. SHMB_WORKER_THREAD, OBJECT_WORKER_THREAD, ADCI_WORKER_THREAD};
  654. for (i = 0; i < MAX_THREAD_NUMBER; i++) {
  655. init_waitqueue_head(&smcinvoke[i].postprocess_kthread_wq);
  656. smcinvoke[i].type = thread_type[i];
  657. smcinvoke[i].postprocess_kthread_task = kthread_run(
  658. smcinvoke_postprocess_kthread_func,
  659. &smcinvoke[i], thread_name[i]);
  660. if (IS_ERR(smcinvoke[i].postprocess_kthread_task)) {
  661. rc = PTR_ERR(smcinvoke[i].postprocess_kthread_task);
  662. pr_err("fail to create kthread to postprocess, rc = %x\n",
  663. rc);
  664. return rc;
  665. }
  666. atomic_set(&smcinvoke[i].postprocess_kthread_state,
  667. POST_KT_SLEEP);
  668. }
  669. return rc;
  670. }
  671. static void smcinvoke_destroy_kthreads(void)
  672. {
  673. int i;
  674. int32_t ret = OBJECT_ERROR;
  675. int retry_count = 0;
  676. if (!Object_isNull(adci_rootEnv)) {
  677. pr_debug("Invoking adciShutdown method in QTEE\n");
  678. do {
  679. ret = IClientEnv_adciShutdown(adci_rootEnv);
  680. if (ret == OBJECT_ERROR_BUSY) {
  681. pr_err_ratelimited("Secure side is busy,will retry after 5 ms, retry_count = %d\n",retry_count);
  682. msleep(SMCINVOKE_INTERFACE_BUSY_WAIT_MS);
  683. }
  684. } while ((ret == OBJECT_ERROR_BUSY) && (retry_count++ < SMCINVOKE_INTERFACE_MAX_RETRY));
  685. if (OBJECT_isERROR(ret)) {
  686. pr_err("adciShutdown in QTEE failed with error = %d\n", ret);
  687. }
  688. Object_ASSIGN_NULL(adci_rootEnv);
  689. }
  690. for (i = 0; i < MAX_THREAD_NUMBER; i++) {
  691. kthread_stop(smcinvoke[i].postprocess_kthread_task);
  692. }
  693. }
  694. /* Queue newly created memory object to l_pending_mem_obj list.
  695. * Later, the mapping information for objects in this list will be sent to TZ
  696. * over the async side channel.
  697. *
  698. * No return value as TZ is always able to explicitly ask for this information
  699. * in case this function fails and the memory object is not added to this list.
  700. */
  701. static void queue_mem_obj_pending_async_locked(struct smcinvoke_mem_obj *mem_obj, struct list_head *l_pending_mem_obj)
  702. {
  703. struct smcinvoke_mem_obj_pending_async *t_mem_obj_pending =
  704. kzalloc(sizeof(*t_mem_obj_pending), GFP_KERNEL);
  705. /*
  706. * We are not failing execution in case of a failure here,
  707. * since TZ can always ask for this information explicitly
  708. * if it's not available in the side channel.
  709. */
  710. if (!t_mem_obj_pending) {
  711. pr_err("Unable to allocate memory\n");
  712. return;
  713. }
  714. t_mem_obj_pending->mem_obj = mem_obj;
  715. list_add(&t_mem_obj_pending->list, l_pending_mem_obj);
  716. }
  717. static inline void free_mem_obj_locked(struct smcinvoke_mem_obj *mem_obj)
  718. {
  719. int ret = 0;
  720. struct dma_buf *dmabuf_to_free = mem_obj->dma_buf;
  721. uint64_t shmbridge_handle = mem_obj->shmbridge_handle;
  722. struct smcinvoke_shmbridge_deregister_pending_list *entry = NULL;
  723. list_del(&mem_obj->list);
  724. kfree(mem_obj->server);
  725. kfree(mem_obj);
  726. mem_obj = NULL;
  727. mutex_unlock(&g_smcinvoke_lock);
  728. if (shmbridge_handle)
  729. ret = qtee_shmbridge_deregister(shmbridge_handle);
  730. if (ret) {
  731. pr_err("Error:%d delete bridge failed leaking memory 0x%p\n",
  732. ret, dmabuf_to_free);
  733. if (ret == -EBUSY) {
  734. pr_err("EBUSY: we postpone it 0x%p\n",
  735. dmabuf_to_free);
  736. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  737. if (entry) {
  738. entry->data.shmbridge_handle = shmbridge_handle;
  739. entry->data.dmabuf_to_free = dmabuf_to_free;
  740. mutex_lock(&bridge_postprocess_lock);
  741. list_add_tail(&entry->list, &g_bridge_postprocess);
  742. mutex_unlock(&bridge_postprocess_lock);
  743. pr_debug("SHMBridge list: added a Handle:%#llx\n",
  744. shmbridge_handle);
  745. __wakeup_postprocess_kthread(
  746. &smcinvoke[SHMB_WORKER_THREAD]);
  747. }
  748. }
  749. } else {
  750. dma_buf_put(dmabuf_to_free);
  751. }
  752. mutex_lock(&g_smcinvoke_lock);
  753. }
  754. static void del_mem_regn_obj_locked(struct kref *kref)
  755. {
  756. struct smcinvoke_mem_obj *mem_obj = container_of(kref,
  757. struct smcinvoke_mem_obj, mem_regn_ref_cnt);
  758. /*
  759. * mem_regn obj and mem_map obj are held into mem_obj structure which
  760. * can't be released until both kinds of objs have been released.
  761. * So check whether mem_map iobj has ref 0 and only then release mem_obj
  762. */
  763. if (kref_read(&mem_obj->mem_map_obj_ref_cnt) == 0)
  764. free_mem_obj_locked(mem_obj);
  765. }
  766. static void del_mem_map_obj_locked(struct kref *kref)
  767. {
  768. struct smcinvoke_mem_obj *mem_obj = container_of(kref,
  769. struct smcinvoke_mem_obj, mem_map_obj_ref_cnt);
  770. mem_obj->p_addr_len = 0;
  771. mem_obj->p_addr = 0;
  772. if (mem_obj->sgt)
  773. dma_buf_unmap_attachment(mem_obj->buf_attach,
  774. mem_obj->sgt, DMA_BIDIRECTIONAL);
  775. if (mem_obj->buf_attach)
  776. dma_buf_detach(mem_obj->dma_buf, mem_obj->buf_attach);
  777. /*
  778. * mem_regn obj and mem_map obj are held into mem_obj structure which
  779. * can't be released until both kinds of objs have been released.
  780. * So check if mem_regn obj has ref 0 and only then release mem_obj
  781. */
  782. if (kref_read(&mem_obj->mem_regn_ref_cnt) == 0)
  783. free_mem_obj_locked(mem_obj);
  784. }
  785. static int release_mem_obj_locked(int32_t tzhandle)
  786. {
  787. int is_mem_regn_obj = TZHANDLE_IS_MEM_RGN_OBJ(tzhandle);
  788. struct smcinvoke_mem_obj *mem_obj = find_mem_obj_locked(
  789. TZHANDLE_GET_OBJID(tzhandle), is_mem_regn_obj);
  790. if (!mem_obj) {
  791. pr_err("memory object not found\n");
  792. return OBJECT_ERROR_BADOBJ;
  793. }
  794. if (is_mem_regn_obj)
  795. kref_put(&mem_obj->mem_regn_ref_cnt, del_mem_regn_obj_locked);
  796. else
  797. kref_put(&mem_obj->mem_map_obj_ref_cnt, del_mem_map_obj_locked);
  798. return OBJECT_OK;
  799. }
  800. static void free_pending_cbobj_locked(struct kref *kref)
  801. {
  802. struct smcinvoke_server_info *server = NULL;
  803. struct smcinvoke_cbobj *obj = container_of(kref,
  804. struct smcinvoke_cbobj, ref_cnt);
  805. list_del(&obj->list);
  806. server = obj->server;
  807. kfree(obj);
  808. if (server)
  809. kref_put(&server->ref_cnt, destroy_cb_server);
  810. }
  811. static int get_pending_cbobj_locked(uint16_t srvr_id, int16_t obj_id)
  812. {
  813. int ret = 0;
  814. bool release_server = true;
  815. struct list_head *head = NULL;
  816. struct smcinvoke_cbobj *cbobj = NULL;
  817. struct smcinvoke_cbobj *obj = NULL;
  818. struct smcinvoke_server_info *server = get_cb_server_locked(srvr_id);
  819. if (!server) {
  820. pr_err("%s, server id : %u not found\n", __func__, srvr_id);
  821. return OBJECT_ERROR_BADOBJ;
  822. }
  823. head = &server->pending_cbobjs;
  824. list_for_each_entry(cbobj, head, list)
  825. if (cbobj->cbobj_id == obj_id) {
  826. kref_get(&cbobj->ref_cnt);
  827. goto out;
  828. }
  829. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  830. if (!obj) {
  831. ret = OBJECT_ERROR_KMEM;
  832. goto out;
  833. }
  834. obj->cbobj_id = obj_id;
  835. kref_init(&obj->ref_cnt);
  836. obj->server = server;
  837. /*
  838. * we are holding server ref in cbobj; we will
  839. * release server ref when cbobj is destroyed
  840. */
  841. release_server = false;
  842. list_add_tail(&obj->list, head);
  843. out:
  844. if (release_server)
  845. kref_put(&server->ref_cnt, destroy_cb_server);
  846. return ret;
  847. }
  848. static int put_pending_cbobj_locked(uint16_t srvr_id, int16_t obj_id)
  849. {
  850. int ret = -EINVAL;
  851. struct smcinvoke_server_info *srvr_info =
  852. get_cb_server_locked(srvr_id);
  853. struct list_head *head = NULL;
  854. struct smcinvoke_cbobj *cbobj = NULL;
  855. if (!srvr_info) {
  856. pr_err("%s, server id : %u not found\n", __func__, srvr_id);
  857. return ret;
  858. }
  859. trace_put_pending_cbobj_locked(srvr_id, obj_id);
  860. head = &srvr_info->pending_cbobjs;
  861. list_for_each_entry(cbobj, head, list)
  862. if (cbobj->cbobj_id == obj_id) {
  863. kref_put(&cbobj->ref_cnt, free_pending_cbobj_locked);
  864. ret = 0;
  865. break;
  866. }
  867. kref_put(&srvr_info->ref_cnt, destroy_cb_server);
  868. return ret;
  869. }
  870. static int release_tzhandle_locked(int32_t tzhandle)
  871. {
  872. if (TZHANDLE_IS_MEM_OBJ(tzhandle))
  873. return release_mem_obj_locked(tzhandle);
  874. else if (TZHANDLE_IS_CB_OBJ(tzhandle))
  875. return put_pending_cbobj_locked(TZHANDLE_GET_SERVER(tzhandle),
  876. TZHANDLE_GET_OBJID(tzhandle));
  877. return OBJECT_ERROR;
  878. }
  879. static void release_tzhandles(const int32_t *tzhandles, size_t len)
  880. {
  881. size_t i;
  882. mutex_lock(&g_smcinvoke_lock);
  883. for (i = 0; i < len; i++)
  884. release_tzhandle_locked(tzhandles[i]);
  885. mutex_unlock(&g_smcinvoke_lock);
  886. }
  887. static void delete_cb_txn_locked(struct kref *kref)
  888. {
  889. struct smcinvoke_cb_txn *cb_txn = container_of(kref,
  890. struct smcinvoke_cb_txn, ref_cnt);
  891. if (OBJECT_OP_METHODID(cb_txn->cb_req->hdr.op) == OBJECT_OP_RELEASE)
  892. release_tzhandle_locked(cb_txn->cb_req->hdr.tzhandle);
  893. kfree(cb_txn->cb_req);
  894. hash_del(&cb_txn->hash);
  895. kfree(cb_txn);
  896. }
  897. static struct smcinvoke_cb_txn *find_cbtxn_locked(
  898. struct smcinvoke_server_info *server,
  899. uint32_t txn_id, int32_t state)
  900. {
  901. int i = 0;
  902. struct smcinvoke_cb_txn *cb_txn = NULL;
  903. /*
  904. * Since HASH_BITS() does not work on pointers, we can't select hash
  905. * table using state and loop over it.
  906. */
  907. if (state == SMCINVOKE_REQ_PLACED) {
  908. /* pick up 1st req */
  909. hash_for_each(server->reqs_table, i, cb_txn, hash) {
  910. kref_get(&cb_txn->ref_cnt);
  911. hash_del(&cb_txn->hash);
  912. return cb_txn;
  913. }
  914. } else if (state == SMCINVOKE_REQ_PROCESSING) {
  915. hash_for_each_possible(
  916. server->responses_table, cb_txn, hash, txn_id) {
  917. if (cb_txn->txn_id == txn_id) {
  918. kref_get(&cb_txn->ref_cnt);
  919. hash_del(&cb_txn->hash);
  920. return cb_txn;
  921. }
  922. }
  923. }
  924. return NULL;
  925. }
  926. /*
  927. * size_add_ saturates at SIZE_MAX. If integer overflow is detected,
  928. * this function would return SIZE_MAX otherwise normal a+b is returned.
  929. */
  930. static inline size_t size_add_(size_t a, size_t b)
  931. {
  932. return (b > (SIZE_MAX - a)) ? SIZE_MAX : a + b;
  933. }
  934. /*
  935. * pad_size is used along with size_align to define a buffer overflow
  936. * protected version of ALIGN
  937. */
  938. static inline size_t pad_size(size_t a, size_t b)
  939. {
  940. return (~a + 1) % b;
  941. }
  942. /*
  943. * size_align saturates at SIZE_MAX. If integer overflow is detected, this
  944. * function would return SIZE_MAX otherwise next aligned size is returned.
  945. */
  946. static inline size_t size_align(size_t a, size_t b)
  947. {
  948. return size_add_(a, pad_size(a, b));
  949. }
  950. static uint16_t get_server_id(int cb_server_fd)
  951. {
  952. uint16_t server_id = 0;
  953. struct smcinvoke_file_data *svr_cxt = NULL;
  954. struct file *tmp_filp = fget(cb_server_fd);
  955. if (!tmp_filp || !FILE_IS_REMOTE_OBJ(tmp_filp))
  956. return server_id;
  957. svr_cxt = tmp_filp->private_data;
  958. if (svr_cxt && svr_cxt->context_type == SMCINVOKE_OBJ_TYPE_SERVER)
  959. server_id = svr_cxt->server_id;
  960. fput(tmp_filp);
  961. return server_id;
  962. }
  963. static bool is_dma_fd(int32_t uhandle, struct dma_buf **dma_buf)
  964. {
  965. *dma_buf = dma_buf_get(uhandle);
  966. return IS_ERR_OR_NULL(*dma_buf) ? false : true;
  967. }
  968. static bool is_remote_obj(int32_t uhandle, struct smcinvoke_file_data **tzobj,
  969. struct file **filp)
  970. {
  971. bool ret = false;
  972. struct file *tmp_filp = fget(uhandle);
  973. if (!tmp_filp)
  974. return ret;
  975. if (FILE_IS_REMOTE_OBJ(tmp_filp)) {
  976. *tzobj = tmp_filp->private_data;
  977. if ((*tzobj)->context_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ) {
  978. *filp = tmp_filp;
  979. tmp_filp = NULL;
  980. ret = true;
  981. }
  982. }
  983. if (tmp_filp)
  984. fput(tmp_filp);
  985. return ret;
  986. }
  987. static int smcinvoke_create_bridge(struct smcinvoke_mem_obj *mem_obj)
  988. {
  989. int ret = 0;
  990. int tz_perm = PERM_READ|PERM_WRITE;
  991. uint32_t *vmid_list;
  992. uint32_t *perms_list;
  993. uint32_t nelems = 0;
  994. struct dma_buf *dmabuf = mem_obj->dma_buf;
  995. phys_addr_t phys = mem_obj->p_addr;
  996. size_t size = mem_obj->p_addr_len;
  997. if (!qtee_shmbridge_is_enabled())
  998. return 0;
  999. ret = mem_buf_dma_buf_copy_vmperm(dmabuf, (int **)&vmid_list,
  1000. (int **)&perms_list, (int *)&nelems);
  1001. if (ret) {
  1002. pr_err("mem_buf_dma_buf_copy_vmperm failure, err=%d\n", ret);
  1003. return ret;
  1004. }
  1005. if (mem_buf_dma_buf_exclusive_owner(dmabuf))
  1006. perms_list[0] = PERM_READ | PERM_WRITE;
  1007. ret = qtee_shmbridge_register(phys, size, vmid_list, perms_list, nelems,
  1008. tz_perm, &mem_obj->shmbridge_handle);
  1009. if (ret) {
  1010. pr_err("creation of shm bridge for mem_region_id %d failed ret %d\n",
  1011. mem_obj->mem_region_id, ret);
  1012. goto exit;
  1013. }
  1014. trace_smcinvoke_create_bridge(mem_obj->shmbridge_handle, mem_obj->mem_region_id);
  1015. exit:
  1016. kfree(perms_list);
  1017. kfree(vmid_list);
  1018. return ret;
  1019. }
  1020. /* Map memory region for a given memory object.
  1021. * Mapping information will be saved as part of the memory object structure.
  1022. */
  1023. static int32_t smcinvoke_map_mem_region_locked(struct smcinvoke_mem_obj* mem_obj)
  1024. {
  1025. int ret = OBJECT_OK;
  1026. struct dma_buf_attachment *buf_attach = NULL;
  1027. struct sg_table *sgt = NULL;
  1028. if (!mem_obj) {
  1029. pr_err("Invalid memory object\n");
  1030. return OBJECT_ERROR_BADOBJ;
  1031. }
  1032. if (!mem_obj->p_addr) {
  1033. kref_init(&mem_obj->mem_map_obj_ref_cnt);
  1034. buf_attach = dma_buf_attach(mem_obj->dma_buf,
  1035. &smcinvoke_pdev->dev);
  1036. if (IS_ERR(buf_attach)) {
  1037. ret = OBJECT_ERROR_KMEM;
  1038. pr_err("dma buf attach failed, ret: %d\n", ret);
  1039. goto out;
  1040. }
  1041. mem_obj->buf_attach = buf_attach;
  1042. sgt = dma_buf_map_attachment(buf_attach, DMA_BIDIRECTIONAL);
  1043. if (IS_ERR(sgt)) {
  1044. pr_err("mapping dma buffers failed, ret: %ld\n",
  1045. PTR_ERR(sgt));
  1046. ret = OBJECT_ERROR_KMEM;
  1047. goto out;
  1048. }
  1049. mem_obj->sgt = sgt;
  1050. /* contiguous only => nents=1 */
  1051. if (sgt->nents != 1) {
  1052. ret = OBJECT_ERROR_INVALID;
  1053. pr_err("sg enries are not contigous, ret: %d\n", ret);
  1054. goto out;
  1055. }
  1056. mem_obj->p_addr = sg_dma_address(sgt->sgl);
  1057. mem_obj->p_addr_len = sgt->sgl->length;
  1058. if (!mem_obj->p_addr) {
  1059. ret = OBJECT_ERROR_INVALID;
  1060. pr_err("invalid physical address, ret: %d\n", ret);
  1061. goto out;
  1062. }
  1063. /* Increase reference count as we are feeding the memobj to
  1064. * smcinvoke and unlock the mutex. No need to hold the mutex in
  1065. * case of shmbridge creation.
  1066. */
  1067. kref_get(&mem_obj->mem_map_obj_ref_cnt);
  1068. mutex_unlock(&g_smcinvoke_lock);
  1069. ret = smcinvoke_create_bridge(mem_obj);
  1070. /* Take lock again and decrease the reference count which we
  1071. * increased for shmbridge but before proceeding further we
  1072. * have to check again if the memobj is still valid or not
  1073. * after decreasing the reference.
  1074. */
  1075. mutex_lock(&g_smcinvoke_lock);
  1076. kref_put(&mem_obj->mem_map_obj_ref_cnt, del_mem_map_obj_locked);
  1077. if (ret) {
  1078. ret = OBJECT_ERROR_INVALID;
  1079. pr_err("Unable to create shm bridge, ret: %d\n", ret);
  1080. goto out;
  1081. }
  1082. if (!find_mem_obj_locked(mem_obj->mem_region_id,
  1083. SMCINVOKE_MEM_RGN_OBJ)) {
  1084. mutex_unlock(&g_smcinvoke_lock);
  1085. pr_err("Memory object not found\n");
  1086. return OBJECT_ERROR_BADOBJ;
  1087. }
  1088. mem_obj->mem_map_obj_id = next_mem_map_obj_id_locked();
  1089. }
  1090. out:
  1091. if (ret != OBJECT_OK)
  1092. kref_put(&mem_obj->mem_map_obj_ref_cnt, del_mem_map_obj_locked);
  1093. return ret;
  1094. }
  1095. static int create_mem_obj(struct dma_buf *dma_buf, int32_t *tzhandle,
  1096. struct smcinvoke_mem_obj **mem_obj, int32_t server_id, int32_t user_handle)
  1097. {
  1098. struct smcinvoke_mem_obj *t_mem_obj = NULL;
  1099. struct smcinvoke_server_info *server_i = NULL;
  1100. t_mem_obj = kzalloc(sizeof(struct smcinvoke_mem_obj), GFP_KERNEL);
  1101. if (!t_mem_obj) {
  1102. dma_buf_put(dma_buf);
  1103. return -ENOMEM;
  1104. }
  1105. server_i = kzalloc(sizeof(struct smcinvoke_server_info),GFP_KERNEL);
  1106. if (!server_i) {
  1107. kfree(t_mem_obj);
  1108. dma_buf_put(dma_buf);
  1109. return -ENOMEM;
  1110. }
  1111. kref_init(&t_mem_obj->mem_regn_ref_cnt);
  1112. t_mem_obj->dma_buf = dma_buf;
  1113. mutex_lock(&g_smcinvoke_lock);
  1114. t_mem_obj->mem_region_id = next_mem_region_obj_id_locked();
  1115. server_i->server_id = server_id;
  1116. t_mem_obj->server = server_i;
  1117. t_mem_obj->mem_obj_user_fd = user_handle;
  1118. list_add_tail(&t_mem_obj->list, &g_mem_objs);
  1119. mutex_unlock(&g_smcinvoke_lock);
  1120. *mem_obj = t_mem_obj;
  1121. *tzhandle = TZHANDLE_MAKE_LOCAL(MEM_RGN_SRVR_ID,
  1122. t_mem_obj->mem_region_id);
  1123. return 0;
  1124. }
  1125. /*
  1126. * This function retrieves file pointer corresponding to FD provided. It stores
  1127. * retrieved file pointer until IOCTL call is concluded. Once call is completed,
  1128. * all stored file pointers are released. file pointers are stored to prevent
  1129. * other threads from releasing that FD while IOCTL is in progress.
  1130. */
  1131. static int get_tzhandle_from_uhandle(int32_t uhandle, int32_t server_fd,
  1132. struct file **filp, uint32_t *tzhandle, struct list_head *l_pending_mem_obj)
  1133. {
  1134. int ret = -EBADF;
  1135. uint16_t server_id = 0;
  1136. struct smcinvoke_mem_obj *mem_obj = NULL;
  1137. if (UHANDLE_IS_NULL(uhandle)) {
  1138. *tzhandle = SMCINVOKE_TZ_OBJ_NULL;
  1139. ret = 0;
  1140. } else if (UHANDLE_IS_CB_OBJ(uhandle)) {
  1141. server_id = get_server_id(server_fd);
  1142. if (server_id < CBOBJ_SERVER_ID_START)
  1143. goto out;
  1144. mutex_lock(&g_smcinvoke_lock);
  1145. ret = get_pending_cbobj_locked(server_id,
  1146. UHANDLE_GET_CB_OBJ(uhandle));
  1147. mutex_unlock(&g_smcinvoke_lock);
  1148. if (ret)
  1149. goto out;
  1150. *tzhandle = TZHANDLE_MAKE_LOCAL(server_id,
  1151. UHANDLE_GET_CB_OBJ(uhandle));
  1152. ret = 0;
  1153. } else if (UHANDLE_IS_FD(uhandle)) {
  1154. struct dma_buf *dma_buf = NULL;
  1155. struct smcinvoke_file_data *tzobj = NULL;
  1156. if (is_dma_fd(UHANDLE_GET_FD(uhandle), &dma_buf)) {
  1157. server_id = get_server_id(server_fd);
  1158. ret = create_mem_obj(dma_buf, tzhandle, &mem_obj, server_id, uhandle);
  1159. if (!ret && mem_obj_async_support && l_pending_mem_obj) {
  1160. mutex_lock(&g_smcinvoke_lock);
  1161. /* Map the newly created memory object and add it
  1162. * to l_pending_mem_obj list.
  1163. * Before returning to TZ, add the mapping data
  1164. * to the async side channel so it's available to TZ
  1165. * together with the memory object.
  1166. */
  1167. if (!smcinvoke_map_mem_region_locked(mem_obj)) {
  1168. queue_mem_obj_pending_async_locked(mem_obj, l_pending_mem_obj);
  1169. } else {
  1170. pr_err("Failed to map memory region\n");
  1171. }
  1172. mutex_unlock(&g_smcinvoke_lock);
  1173. }
  1174. } else if (is_remote_obj(UHANDLE_GET_FD(uhandle),
  1175. &tzobj, filp)) {
  1176. *tzhandle = tzobj->tzhandle;
  1177. ret = 0;
  1178. }
  1179. }
  1180. out:
  1181. return ret;
  1182. }
  1183. static int get_fd_for_obj(uint32_t obj_type, uint32_t obj, int32_t *fd)
  1184. {
  1185. int unused_fd = -1, ret = -EINVAL;
  1186. struct file *f = NULL;
  1187. struct smcinvoke_file_data *cxt = NULL;
  1188. cxt = kzalloc(sizeof(*cxt), GFP_KERNEL);
  1189. if (!cxt) {
  1190. ret = -ENOMEM;
  1191. goto out;
  1192. }
  1193. if (obj_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ ||
  1194. obj_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ_FOR_KERNEL) {
  1195. cxt->context_type = obj_type;
  1196. cxt->tzhandle = obj;
  1197. } else if (obj_type == SMCINVOKE_OBJ_TYPE_SERVER) {
  1198. cxt->context_type = SMCINVOKE_OBJ_TYPE_SERVER;
  1199. cxt->server_id = obj;
  1200. } else {
  1201. goto out;
  1202. }
  1203. unused_fd = get_unused_fd_flags(O_RDWR);
  1204. if (unused_fd < 0)
  1205. goto out;
  1206. if (fd == NULL)
  1207. goto out;
  1208. f = anon_inode_getfile(SMCINVOKE_DEV, &g_smcinvoke_fops, cxt, O_RDWR);
  1209. if (IS_ERR(f))
  1210. goto out;
  1211. *fd = unused_fd;
  1212. fd_install(*fd, f);
  1213. return 0;
  1214. out:
  1215. if (unused_fd >= 0)
  1216. put_unused_fd(unused_fd);
  1217. kfree(cxt);
  1218. return ret;
  1219. }
  1220. static int get_uhandle_from_tzhandle(int32_t tzhandle, int32_t srvr_id,
  1221. int32_t *uhandle, bool lock, uint32_t context_type)
  1222. {
  1223. int ret = -1;
  1224. if (TZHANDLE_IS_NULL(tzhandle)) {
  1225. *uhandle = UHANDLE_NULL;
  1226. ret = 0;
  1227. } else if (TZHANDLE_IS_CB_OBJ(tzhandle)) {
  1228. if (srvr_id != TZHANDLE_GET_SERVER(tzhandle))
  1229. goto out;
  1230. *uhandle = UHANDLE_MAKE_CB_OBJ(TZHANDLE_GET_OBJID(tzhandle));
  1231. MUTEX_LOCK(lock)
  1232. ret = get_pending_cbobj_locked(TZHANDLE_GET_SERVER(tzhandle),
  1233. TZHANDLE_GET_OBJID(tzhandle));
  1234. MUTEX_UNLOCK(lock)
  1235. } else if (TZHANDLE_IS_MEM_RGN_OBJ(tzhandle)) {
  1236. struct smcinvoke_mem_obj *mem_obj = NULL;
  1237. MUTEX_LOCK(lock)
  1238. mem_obj = find_mem_obj_locked(TZHANDLE_GET_OBJID(tzhandle),
  1239. SMCINVOKE_MEM_RGN_OBJ);
  1240. if (mem_obj != NULL) {
  1241. int fd;
  1242. fd = mem_obj->mem_obj_user_fd;
  1243. if (fd < 0)
  1244. goto exit_lock;
  1245. *uhandle = fd;
  1246. ret = 0;
  1247. }
  1248. exit_lock:
  1249. MUTEX_UNLOCK(lock)
  1250. } else if (TZHANDLE_IS_REMOTE(tzhandle)) {
  1251. /* if execution comes here => tzhandle is an unsigned int */
  1252. ret = get_fd_for_obj(context_type,
  1253. (uint32_t)tzhandle, uhandle);
  1254. }
  1255. out:
  1256. return ret;
  1257. }
  1258. static int32_t smcinvoke_release_mem_obj_locked(void *buf, size_t buf_len)
  1259. {
  1260. struct smcinvoke_tzcb_req *msg = buf;
  1261. if (msg->hdr.counts != OBJECT_COUNTS_PACK(0, 0, 0, 0)) {
  1262. pr_err("Invalid object count in %s\n", __func__);
  1263. return OBJECT_ERROR_INVALID;
  1264. }
  1265. trace_release_mem_obj_locked(msg->hdr.tzhandle, buf_len);
  1266. return release_tzhandle_locked(msg->hdr.tzhandle);
  1267. }
  1268. static int32_t smcinvoke_process_map_mem_region_req(void *buf, size_t buf_len)
  1269. {
  1270. int ret = OBJECT_OK;
  1271. struct smcinvoke_tzcb_req *msg = buf;
  1272. struct {
  1273. uint64_t p_addr;
  1274. uint64_t len;
  1275. uint32_t perms;
  1276. } *ob = NULL;
  1277. int32_t *oo = NULL;
  1278. struct smcinvoke_mem_obj *mem_obj = NULL;
  1279. if (msg->hdr.counts != OBJECT_COUNTS_PACK(0, 1, 1, 1) ||
  1280. (buf_len - msg->args[0].b.offset < msg->args[0].b.size)) {
  1281. pr_err("Invalid counts received for mapping mem obj\n");
  1282. return OBJECT_ERROR_INVALID;
  1283. }
  1284. /* args[0] = BO, args[1] = OI, args[2] = OO */
  1285. ob = buf + msg->args[0].b.offset;
  1286. oo = &msg->args[2].handle;
  1287. mutex_lock(&g_smcinvoke_lock);
  1288. mem_obj = find_mem_obj_locked(TZHANDLE_GET_OBJID(msg->args[1].handle),
  1289. SMCINVOKE_MEM_RGN_OBJ);
  1290. if (!mem_obj) {
  1291. mutex_unlock(&g_smcinvoke_lock);
  1292. pr_err("Memory object not found\n");
  1293. return OBJECT_ERROR_BADOBJ;
  1294. }
  1295. if (!mem_obj->p_addr) {
  1296. ret = smcinvoke_map_mem_region_locked(mem_obj);
  1297. } else {
  1298. kref_get(&mem_obj->mem_map_obj_ref_cnt);
  1299. }
  1300. if (!ret) {
  1301. ob->p_addr = mem_obj->p_addr;
  1302. ob->len = mem_obj->p_addr_len;
  1303. ob->perms = SMCINVOKE_MEM_PERM_RW;
  1304. *oo = TZHANDLE_MAKE_LOCAL(MEM_MAP_SRVR_ID, mem_obj->mem_map_obj_id);
  1305. }
  1306. mutex_unlock(&g_smcinvoke_lock);
  1307. return ret;
  1308. }
  1309. static int32_t smcinvoke_sleep(void *buf, size_t buf_len)
  1310. {
  1311. struct smcinvoke_tzcb_req *msg = buf;
  1312. uint32_t sleepTimeMs_val = 0;
  1313. if (msg->hdr.counts != OBJECT_COUNTS_PACK(1, 0, 0, 0) ||
  1314. (buf_len - msg->args[0].b.offset < msg->args[0].b.size)) {
  1315. pr_err("Invalid counts received for sleeping in hlos\n");
  1316. return OBJECT_ERROR_INVALID;
  1317. }
  1318. /* Time in miliseconds is expected from tz */
  1319. sleepTimeMs_val = *((uint32_t *)(buf + msg->args[0].b.offset));
  1320. msleep(sleepTimeMs_val);
  1321. return OBJECT_OK;
  1322. }
  1323. static void process_kernel_obj(void *buf, size_t buf_len)
  1324. {
  1325. struct smcinvoke_tzcb_req *cb_req = buf;
  1326. switch (cb_req->hdr.op) {
  1327. case OBJECT_OP_MAP_REGION:
  1328. if (mem_obj_async_support) {
  1329. /* Mapping requests are not supposed to come
  1330. * from TZ once memory object async support
  1331. * is enabled.
  1332. * If they are still coming, we would like to
  1333. * know about it.
  1334. */
  1335. pr_info("Received a request to map memory region\n");
  1336. }
  1337. cb_req->result = smcinvoke_process_map_mem_region_req(buf, buf_len);
  1338. break;
  1339. case OBJECT_OP_YIELD:
  1340. cb_req->result = OBJECT_OK;
  1341. break;
  1342. case OBJECT_OP_SLEEP:
  1343. cb_req->result = smcinvoke_sleep(buf, buf_len);
  1344. break;
  1345. default:
  1346. pr_err(" invalid operation for tz kernel object\n");
  1347. cb_req->result = OBJECT_ERROR_INVALID;
  1348. break;
  1349. }
  1350. }
  1351. static void process_mem_obj(void *buf, size_t buf_len)
  1352. {
  1353. struct smcinvoke_tzcb_req *cb_req = buf;
  1354. mutex_lock(&g_smcinvoke_lock);
  1355. cb_req->result = (cb_req->hdr.op == OBJECT_OP_RELEASE) ?
  1356. smcinvoke_release_mem_obj_locked(buf, buf_len) :
  1357. OBJECT_ERROR_INVALID;
  1358. mutex_unlock(&g_smcinvoke_lock);
  1359. }
  1360. static int invoke_cmd_handler(int cmd, phys_addr_t in_paddr, size_t in_buf_len,
  1361. uint8_t *out_buf, phys_addr_t out_paddr,
  1362. size_t out_buf_len, int32_t *result, u64 *response_type,
  1363. unsigned int *data, struct qtee_shm *in_shm,
  1364. struct qtee_shm *out_shm)
  1365. {
  1366. int ret = 0;
  1367. switch (cmd) {
  1368. case SMCINVOKE_INVOKE_CMD_LEGACY:
  1369. qtee_shmbridge_flush_shm_buf(in_shm);
  1370. qtee_shmbridge_flush_shm_buf(out_shm);
  1371. ret = qcom_scm_invoke_smc_legacy(in_paddr, in_buf_len, out_paddr, out_buf_len,
  1372. result, response_type, data);
  1373. qtee_shmbridge_inv_shm_buf(in_shm);
  1374. qtee_shmbridge_inv_shm_buf(out_shm);
  1375. break;
  1376. case SMCINVOKE_INVOKE_CMD:
  1377. ret = qcom_scm_invoke_smc(in_paddr, in_buf_len, out_paddr, out_buf_len,
  1378. result, response_type, data);
  1379. break;
  1380. case SMCINVOKE_CB_RSP_CMD:
  1381. if (legacy_smc_call)
  1382. qtee_shmbridge_flush_shm_buf(out_shm);
  1383. ret = qcom_scm_invoke_callback_response(virt_to_phys(out_buf), out_buf_len,
  1384. result, response_type, data);
  1385. if (legacy_smc_call) {
  1386. qtee_shmbridge_inv_shm_buf(in_shm);
  1387. qtee_shmbridge_inv_shm_buf(out_shm);
  1388. }
  1389. break;
  1390. default:
  1391. ret = -EINVAL;
  1392. break;
  1393. }
  1394. trace_invoke_cmd_handler(cmd, *response_type, *result, ret);
  1395. return ret;
  1396. }
  1397. /*
  1398. * Buf should be aligned to struct smcinvoke_tzcb_req
  1399. */
  1400. static void process_tzcb_req(void *buf, size_t buf_len, struct file **arr_filp)
  1401. {
  1402. /* ret is going to TZ. Provide values from OBJECT_ERROR_<> */
  1403. int ret = OBJECT_ERROR_DEFUNCT;
  1404. int cbobj_retries = 0;
  1405. long timeout_jiff;
  1406. bool wait_interrupted = false;
  1407. struct smcinvoke_cb_txn *cb_txn = NULL;
  1408. struct smcinvoke_tzcb_req *cb_req = NULL, *tmp_cb_req = NULL;
  1409. struct smcinvoke_server_info *srvr_info = NULL;
  1410. struct smcinvoke_mem_obj *mem_obj = NULL;
  1411. uint16_t server_id = 0;
  1412. if (buf_len < sizeof(struct smcinvoke_tzcb_req)) {
  1413. pr_err("smaller buffer length : %zu\n", buf_len);
  1414. return;
  1415. }
  1416. cb_req = buf;
  1417. /* check whether it is to be served by kernel or userspace */
  1418. if (TZHANDLE_IS_KERNEL_OBJ(cb_req->hdr.tzhandle)) {
  1419. return process_kernel_obj(buf, buf_len);
  1420. } else if (TZHANDLE_IS_MEM_MAP_OBJ(cb_req->hdr.tzhandle)) {
  1421. /*
  1422. * MEM_MAP memory object is created and owned by kernel,
  1423. * hence its processing(handling deletion) is done in
  1424. * kernel context.
  1425. */
  1426. return process_mem_obj(buf, buf_len);
  1427. } else if(TZHANDLE_IS_MEM_RGN_OBJ(cb_req->hdr.tzhandle)) {
  1428. /*
  1429. * MEM_RGN memory objects are created and owned by userspace,
  1430. * and hence their deletion/handling requires going back to the
  1431. * userspace, similar to that of callback objects. If we enter
  1432. * this 'if' condition, its no-op here, and proceed similar to
  1433. * case of callback objects.
  1434. */
  1435. } else if (!TZHANDLE_IS_CB_OBJ(cb_req->hdr.tzhandle)) {
  1436. pr_err("Request object is not a callback object %x\n",
  1437. cb_req->hdr.tzhandle);
  1438. cb_req->result = OBJECT_ERROR_INVALID;
  1439. return;
  1440. }
  1441. /*
  1442. * We need a copy of req that could be sent to server. Otherwise, if
  1443. * someone kills invoke caller, buf would go away and server would be
  1444. * working on already freed buffer, causing a device crash.
  1445. */
  1446. tmp_cb_req = kmemdup(buf, buf_len, GFP_KERNEL);
  1447. if (!tmp_cb_req) {
  1448. /* we need to return error to caller so fill up result */
  1449. cb_req->result = OBJECT_ERROR_KMEM;
  1450. pr_err("failed to create copy of request, set result: %d\n",
  1451. cb_req->result);
  1452. return;
  1453. }
  1454. cb_txn = kzalloc(sizeof(*cb_txn), GFP_KERNEL);
  1455. if (!cb_txn) {
  1456. cb_req->result = OBJECT_ERROR_KMEM;
  1457. pr_err("failed to allocate memory for request, result: %d\n",
  1458. cb_req->result);
  1459. kfree(tmp_cb_req);
  1460. return;
  1461. }
  1462. /* no need for memcpy as we did kmemdup() above */
  1463. cb_req = tmp_cb_req;
  1464. trace_process_tzcb_req_handle(cb_req->hdr.tzhandle, cb_req->hdr.op, cb_req->hdr.counts);
  1465. cb_txn->state = SMCINVOKE_REQ_PLACED;
  1466. cb_txn->cb_req = cb_req;
  1467. cb_txn->cb_req_bytes = buf_len;
  1468. cb_txn->filp_to_release = arr_filp;
  1469. kref_init(&cb_txn->ref_cnt);
  1470. mutex_lock(&g_smcinvoke_lock);
  1471. ++cb_reqs_inflight;
  1472. if(TZHANDLE_IS_MEM_RGN_OBJ(cb_req->hdr.tzhandle)) {
  1473. mem_obj= find_mem_obj_locked(TZHANDLE_GET_OBJID(cb_req->hdr.tzhandle),SMCINVOKE_MEM_RGN_OBJ);
  1474. if(!mem_obj) {
  1475. pr_err("mem obj with tzhandle : %d not found",cb_req->hdr.tzhandle);
  1476. mutex_unlock(&g_smcinvoke_lock);
  1477. goto out;
  1478. }
  1479. server_id = mem_obj->server->server_id;
  1480. } else {
  1481. server_id = TZHANDLE_GET_SERVER(cb_req->hdr.tzhandle);
  1482. }
  1483. srvr_info = get_cb_server_locked(server_id);
  1484. if (!srvr_info || srvr_info->state == SMCINVOKE_SERVER_STATE_DEFUNCT) {
  1485. /* ret equals Object_ERROR_DEFUNCT, at this point go to out */
  1486. if (!srvr_info)
  1487. pr_err("server is invalid\n");
  1488. else {
  1489. pr_err("server is defunct, state= %d tzhandle = %d\n",
  1490. srvr_info->state, cb_req->hdr.tzhandle);
  1491. }
  1492. mutex_unlock(&g_smcinvoke_lock);
  1493. goto out;
  1494. }
  1495. cb_txn->txn_id = ++srvr_info->txn_id;
  1496. hash_add(srvr_info->reqs_table, &cb_txn->hash, cb_txn->txn_id);
  1497. mutex_unlock(&g_smcinvoke_lock);
  1498. trace_process_tzcb_req_wait(cb_req->hdr.tzhandle, cbobj_retries, cb_txn->txn_id,
  1499. current->pid, current->tgid, srvr_info->state, srvr_info->server_id,
  1500. cb_reqs_inflight);
  1501. /*
  1502. * we need not worry that server_info will be deleted because as long
  1503. * as this CBObj is served by this server, srvr_info will be valid.
  1504. */
  1505. wake_up_interruptible_all(&srvr_info->req_wait_q);
  1506. /* timeout before 1s otherwise tzbusy would come */
  1507. timeout_jiff = msecs_to_jiffies(100);
  1508. while (cbobj_retries < CBOBJ_MAX_RETRIES) {
  1509. if (wait_interrupted) {
  1510. ret = wait_event_timeout(srvr_info->rsp_wait_q,
  1511. (cb_txn->state == SMCINVOKE_REQ_PROCESSED) ||
  1512. (srvr_info->state == SMCINVOKE_SERVER_STATE_DEFUNCT),
  1513. timeout_jiff);
  1514. } else {
  1515. ret = wait_event_interruptible_timeout(srvr_info->rsp_wait_q,
  1516. (cb_txn->state == SMCINVOKE_REQ_PROCESSED) ||
  1517. (srvr_info->state == SMCINVOKE_SERVER_STATE_DEFUNCT),
  1518. timeout_jiff);
  1519. }
  1520. if (ret == 0) {
  1521. if (srvr_info->is_server_suspended == 0) {
  1522. tzcb_err_ratelimited("CBobj timed out waiting on cbtxn :%d,cb-tzhandle:%d, retry:%d, op:%d counts :%d\n",
  1523. cb_txn->txn_id,cb_req->hdr.tzhandle, cbobj_retries,
  1524. cb_req->hdr.op, cb_req->hdr.counts);
  1525. tzcb_err_ratelimited("CBobj %d timedout pid %x,tid %x, srvr state=%d, srvr id:%u\n",
  1526. cb_req->hdr.tzhandle, current->pid,
  1527. current->tgid, srvr_info->state,
  1528. srvr_info->server_id);
  1529. }
  1530. } else {
  1531. /* wait_event returned due to a signal */
  1532. if (srvr_info->state != SMCINVOKE_SERVER_STATE_DEFUNCT &&
  1533. cb_txn->state != SMCINVOKE_REQ_PROCESSED) {
  1534. wait_interrupted = true;
  1535. } else {
  1536. break;
  1537. }
  1538. }
  1539. /*
  1540. * If bit corresponding to any accept thread is set, invoke threads
  1541. * should wait infinitely for the accept thread to come back with
  1542. * response.
  1543. */
  1544. if (srvr_info->is_server_suspended > 0) {
  1545. cbobj_retries = 0;
  1546. } else {
  1547. cbobj_retries++;
  1548. }
  1549. }
  1550. out:
  1551. /*
  1552. * we could be here because of either:
  1553. * a. Req is PROCESSED
  1554. * b. Server was killed
  1555. * c. Invoke thread is killed
  1556. * sometime invoke thread and server are part of same process.
  1557. */
  1558. mutex_lock(&g_smcinvoke_lock);
  1559. hash_del(&cb_txn->hash);
  1560. if (ret == 0) {
  1561. pr_err("CBObj timed out! No more retries\n");
  1562. cb_req->result = Object_ERROR_TIMEOUT;
  1563. } else if (ret == -ERESTARTSYS) {
  1564. pr_err("wait event interruped, ret: %d\n", ret);
  1565. cb_req->result = OBJECT_ERROR_ABORT;
  1566. } else {
  1567. if (cb_txn->state == SMCINVOKE_REQ_PROCESSED) {
  1568. /*
  1569. * it is possible that server was killed immediately
  1570. * after CB Req was processed but who cares now!
  1571. */
  1572. } else if (!srvr_info ||
  1573. srvr_info->state == SMCINVOKE_SERVER_STATE_DEFUNCT) {
  1574. cb_req->result = OBJECT_ERROR_DEFUNCT;
  1575. pr_err("server invalid, res: %d\n", cb_req->result);
  1576. } else {
  1577. pr_err("%s: unexpected event happened, ret:%d\n", __func__, ret);
  1578. cb_req->result = OBJECT_ERROR_ABORT;
  1579. }
  1580. }
  1581. --cb_reqs_inflight;
  1582. trace_process_tzcb_req_result(cb_req->result, cb_req->hdr.tzhandle, cb_req->hdr.op,
  1583. cb_req->hdr.counts, cb_reqs_inflight);
  1584. memcpy(buf, cb_req, buf_len);
  1585. kref_put(&cb_txn->ref_cnt, delete_cb_txn_locked);
  1586. if (srvr_info)
  1587. kref_put(&srvr_info->ref_cnt, destroy_cb_server);
  1588. mutex_unlock(&g_smcinvoke_lock);
  1589. }
  1590. static int marshal_out_invoke_req(const uint8_t *buf, uint32_t buf_size,
  1591. struct smcinvoke_cmd_req *req,
  1592. union smcinvoke_arg *args_buf,
  1593. uint32_t context_type)
  1594. {
  1595. int ret = -EINVAL, i = 0;
  1596. int32_t temp_fd = UHANDLE_NULL;
  1597. union smcinvoke_tz_args *tz_args = NULL;
  1598. size_t offset = sizeof(struct smcinvoke_msg_hdr) +
  1599. OBJECT_COUNTS_TOTAL(req->counts) *
  1600. sizeof(union smcinvoke_tz_args);
  1601. if (offset > buf_size)
  1602. goto out;
  1603. tz_args = (union smcinvoke_tz_args *)
  1604. (buf + sizeof(struct smcinvoke_msg_hdr));
  1605. tz_args += OBJECT_COUNTS_NUM_BI(req->counts);
  1606. if (args_buf == NULL)
  1607. return 0;
  1608. FOR_ARGS(i, req->counts, BO) {
  1609. args_buf[i].b.size = tz_args->b.size;
  1610. if ((buf_size - tz_args->b.offset < tz_args->b.size) ||
  1611. tz_args->b.offset > buf_size) {
  1612. pr_err("%s: buffer overflow detected\n", __func__);
  1613. goto out;
  1614. }
  1615. if (context_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ) {
  1616. if (copy_to_user((void __user *)
  1617. (uintptr_t)(args_buf[i].b.addr),
  1618. (uint8_t *)(buf) + tz_args->b.offset,
  1619. tz_args->b.size)) {
  1620. pr_err("Error %d copying ctxt to user\n", ret);
  1621. goto out;
  1622. }
  1623. } else {
  1624. memcpy((uint8_t *)(args_buf[i].b.addr),
  1625. (uint8_t *)(buf) + tz_args->b.offset,
  1626. tz_args->b.size);
  1627. }
  1628. tz_args++;
  1629. }
  1630. tz_args += OBJECT_COUNTS_NUM_OI(req->counts);
  1631. FOR_ARGS(i, req->counts, OO) {
  1632. /*
  1633. * create a new FD and assign to output object's context.
  1634. * We are passing cb_server_fd from output param in case OO
  1635. * is a CBObj. For CBObj, we have to ensure that it is sent
  1636. * to server who serves it and that info comes from USpace.
  1637. */
  1638. temp_fd = UHANDLE_NULL;
  1639. ret = get_uhandle_from_tzhandle(tz_args->handle,
  1640. TZHANDLE_GET_SERVER(tz_args->handle),
  1641. &temp_fd, NO_LOCK, context_type);
  1642. args_buf[i].o.fd = temp_fd;
  1643. if (ret)
  1644. goto out;
  1645. trace_marshal_out_invoke_req(i, tz_args->handle,
  1646. TZHANDLE_GET_SERVER(tz_args->handle), temp_fd);
  1647. tz_args++;
  1648. }
  1649. ret = 0;
  1650. out:
  1651. return ret;
  1652. }
  1653. static bool is_inbound_req(int val)
  1654. {
  1655. return (val == SMCINVOKE_RESULT_INBOUND_REQ_NEEDED ||
  1656. val == QSEOS_RESULT_INCOMPLETE ||
  1657. val == QSEOS_RESULT_BLOCKED_ON_LISTENER);
  1658. }
  1659. static void process_piggyback_cb_data(uint8_t *outbuf, size_t buf_len)
  1660. {
  1661. struct smcinvoke_tzcb_req *msg = NULL;
  1662. uint32_t max_offset = 0;
  1663. uint32_t buffer_size_max_offset = 0;
  1664. void *piggyback_buf = NULL;
  1665. size_t piggyback_buf_size;
  1666. size_t piggyback_offset = 0;
  1667. int i = 0;
  1668. if (outbuf == NULL) {
  1669. pr_err("%s: outbuf is NULL\n", __func__);
  1670. return;
  1671. }
  1672. msg = (void *) outbuf;
  1673. if ((buf_len < msg->args[0].b.offset) ||
  1674. (buf_len - msg->args[0].b.offset < msg->args[0].b.size)) {
  1675. pr_err("%s: invalid scenario\n", __func__);
  1676. return;
  1677. }
  1678. FOR_ARGS(i, msg->hdr.counts, BI)
  1679. {
  1680. if (msg->args[i].b.offset > max_offset) {
  1681. max_offset = msg->args[i].b.offset;
  1682. buffer_size_max_offset = msg->args[i].b.size;
  1683. }
  1684. }
  1685. FOR_ARGS(i, msg->hdr.counts, BO)
  1686. {
  1687. if (msg->args[i].b.offset > max_offset) {
  1688. max_offset = msg->args[i].b.offset;
  1689. buffer_size_max_offset = msg->args[i].b.size;
  1690. }
  1691. }
  1692. //Take out the offset after BI and BO objects end
  1693. if (max_offset)
  1694. piggyback_offset = max_offset + buffer_size_max_offset;
  1695. else
  1696. piggyback_offset = TZCB_BUF_OFFSET(msg);
  1697. piggyback_offset = size_align(piggyback_offset, SMCINVOKE_ARGS_ALIGN_SIZE);
  1698. // Jump to piggy back data offset
  1699. piggyback_buf = (uint8_t *)msg + piggyback_offset;
  1700. piggyback_buf_size = g_max_cb_buf_size - piggyback_offset;
  1701. process_piggyback_data(piggyback_buf, piggyback_buf_size);
  1702. }
  1703. static int prepare_send_scm_msg(const uint8_t *in_buf, phys_addr_t in_paddr,
  1704. size_t in_buf_len,
  1705. uint8_t *out_buf, phys_addr_t out_paddr,
  1706. size_t out_buf_len,
  1707. struct smcinvoke_cmd_req *req,
  1708. union smcinvoke_arg *args_buf,
  1709. bool *tz_acked, uint32_t context_type,
  1710. struct qtee_shm *in_shm, struct qtee_shm *out_shm,
  1711. bool retry)
  1712. {
  1713. int ret = 0, cmd, retry_count = 0;
  1714. u64 response_type;
  1715. unsigned int data;
  1716. struct file *arr_filp[OBJECT_COUNTS_MAX_OO] = {NULL};
  1717. *tz_acked = false;
  1718. /* buf size should be page aligned */
  1719. if ((in_buf_len % PAGE_SIZE) != 0 || (out_buf_len % PAGE_SIZE) != 0)
  1720. return -EINVAL;
  1721. cmd = invoke_cmd;
  1722. while (1) {
  1723. do {
  1724. ret = invoke_cmd_handler(cmd, in_paddr, in_buf_len, out_buf,
  1725. out_paddr, out_buf_len, &req->result,
  1726. &response_type, &data, in_shm, out_shm);
  1727. if (ret == -EBUSY) {
  1728. pr_err_ratelimited("Secure side is busy,will retry after 30 ms, retry_count = %d\n",retry_count);
  1729. msleep(SMCINVOKE_SCM_EBUSY_WAIT_MS);
  1730. }
  1731. } while (retry && (ret == -EBUSY) &&
  1732. (retry_count++ < SMCINVOKE_SCM_EBUSY_MAX_RETRY));
  1733. if (!ret && !is_inbound_req(response_type)) {
  1734. /* dont marshal if Obj returns an error */
  1735. if (!req->result) {
  1736. if (args_buf != NULL)
  1737. ret = marshal_out_invoke_req(in_buf,
  1738. in_buf_len, req, args_buf,
  1739. context_type);
  1740. }
  1741. *tz_acked = true;
  1742. }
  1743. if (cmd == SMCINVOKE_CB_RSP_CMD)
  1744. release_filp(arr_filp, OBJECT_COUNTS_MAX_OO);
  1745. if (ret || !is_inbound_req(response_type))
  1746. break;
  1747. /* process listener request */
  1748. if (response_type == QSEOS_RESULT_INCOMPLETE ||
  1749. response_type == QSEOS_RESULT_BLOCKED_ON_LISTENER) {
  1750. ret = qseecom_process_listener_from_smcinvoke(
  1751. &req->result, &response_type, &data);
  1752. trace_prepare_send_scm_msg(response_type, req->result);
  1753. if (!req->result &&
  1754. response_type != SMCINVOKE_RESULT_INBOUND_REQ_NEEDED) {
  1755. ret = marshal_out_invoke_req(in_buf,
  1756. in_buf_len, req, args_buf,
  1757. context_type);
  1758. }
  1759. *tz_acked = true;
  1760. }
  1761. /*
  1762. * qseecom does not understand smcinvoke's callback object &&
  1763. * erringly sets ret value as -EINVAL :( We need to handle it.
  1764. */
  1765. if (response_type != SMCINVOKE_RESULT_INBOUND_REQ_NEEDED)
  1766. break;
  1767. if (response_type == SMCINVOKE_RESULT_INBOUND_REQ_NEEDED) {
  1768. trace_status(__func__, "looks like inbnd req reqd");
  1769. process_piggyback_cb_data(out_buf, out_buf_len);
  1770. process_tzcb_req(out_buf, out_buf_len, arr_filp);
  1771. cmd = SMCINVOKE_CB_RSP_CMD;
  1772. }
  1773. }
  1774. return ret;
  1775. }
  1776. /*
  1777. * SMC expects arguments in following format
  1778. * ---------------------------------------------------------------------------
  1779. * | cxt | op | counts | ptr|size |ptr|size...|ORef|ORef|...| rest of payload |
  1780. * ---------------------------------------------------------------------------
  1781. * cxt: target, op: operation, counts: total arguments
  1782. * offset: offset is from beginning of buffer i.e. cxt
  1783. * size: size is 8 bytes aligned value
  1784. */
  1785. static size_t compute_in_msg_size(const struct smcinvoke_cmd_req *req,
  1786. const union smcinvoke_arg *args_buf)
  1787. {
  1788. uint32_t i = 0;
  1789. size_t total_size = sizeof(struct smcinvoke_msg_hdr) +
  1790. OBJECT_COUNTS_TOTAL(req->counts) *
  1791. sizeof(union smcinvoke_tz_args);
  1792. /* Computed total_size should be 8 bytes aligned from start of buf */
  1793. total_size = ALIGN(total_size, SMCINVOKE_ARGS_ALIGN_SIZE);
  1794. /* each buffer has to be 8 bytes aligned */
  1795. while (i < OBJECT_COUNTS_NUM_buffers(req->counts))
  1796. total_size = size_add_(total_size,
  1797. size_align(args_buf[i++].b.size,
  1798. SMCINVOKE_ARGS_ALIGN_SIZE));
  1799. return PAGE_ALIGN(total_size);
  1800. }
  1801. static int marshal_in_invoke_req(const struct smcinvoke_cmd_req *req,
  1802. const union smcinvoke_arg *args_buf, uint32_t tzhandle,
  1803. uint8_t *buf, size_t buf_size, struct file **arr_filp,
  1804. int32_t *tzhandles_to_release, uint32_t context_type,
  1805. struct list_head *l_pending_mem_obj)
  1806. {
  1807. int ret = -EINVAL, i = 0, j = 0, k = 0;
  1808. const struct smcinvoke_msg_hdr msg_hdr = {
  1809. tzhandle, req->op, req->counts};
  1810. uint32_t offset = sizeof(struct smcinvoke_msg_hdr) +
  1811. sizeof(union smcinvoke_tz_args) *
  1812. OBJECT_COUNTS_TOTAL(req->counts);
  1813. union smcinvoke_tz_args *tz_args = NULL;
  1814. if (buf_size < offset)
  1815. goto out;
  1816. *(struct smcinvoke_msg_hdr *)buf = msg_hdr;
  1817. tz_args = (union smcinvoke_tz_args *)(buf +
  1818. sizeof(struct smcinvoke_msg_hdr));
  1819. if (args_buf == NULL)
  1820. return 0;
  1821. FOR_ARGS(i, req->counts, BI) {
  1822. offset = size_align(offset, SMCINVOKE_ARGS_ALIGN_SIZE);
  1823. if ((offset > buf_size) ||
  1824. (args_buf[i].b.size > (buf_size - offset)))
  1825. goto out;
  1826. tz_args[i].b.offset = offset;
  1827. tz_args[i].b.size = args_buf[i].b.size;
  1828. if (context_type != SMCINVOKE_OBJ_TYPE_TZ_OBJ_FOR_KERNEL) {
  1829. if (copy_from_user(buf + offset,
  1830. (void __user *)(uintptr_t)(args_buf[i].b.addr),
  1831. args_buf[i].b.size))
  1832. goto out;
  1833. } else {
  1834. memcpy(buf + offset, (void *)(args_buf[i].b.addr),
  1835. args_buf[i].b.size);
  1836. }
  1837. offset += args_buf[i].b.size;
  1838. }
  1839. FOR_ARGS(i, req->counts, BO) {
  1840. offset = size_align(offset, SMCINVOKE_ARGS_ALIGN_SIZE);
  1841. if ((offset > buf_size) ||
  1842. (args_buf[i].b.size > (buf_size - offset)))
  1843. goto out;
  1844. tz_args[i].b.offset = offset;
  1845. tz_args[i].b.size = args_buf[i].b.size;
  1846. offset += args_buf[i].b.size;
  1847. }
  1848. FOR_ARGS(i, req->counts, OI) {
  1849. ret = get_tzhandle_from_uhandle(args_buf[i].o.fd,
  1850. args_buf[i].o.cb_server_fd, &arr_filp[j++],
  1851. &(tz_args[i].handle), l_pending_mem_obj);
  1852. if (ret)
  1853. goto out;
  1854. trace_marshal_in_invoke_req(i, args_buf[i].o.fd,
  1855. args_buf[i].o.cb_server_fd, tz_args[i].handle);
  1856. tzhandles_to_release[k++] = tz_args[i].handle;
  1857. }
  1858. ret = 0;
  1859. out:
  1860. return ret;
  1861. }
  1862. static int marshal_in_tzcb_req(const struct smcinvoke_cb_txn *cb_txn,
  1863. struct smcinvoke_accept *user_req, int srvr_id)
  1864. {
  1865. int ret = 0, i = 0;
  1866. int32_t temp_fd = UHANDLE_NULL;
  1867. union smcinvoke_arg tmp_arg;
  1868. struct smcinvoke_tzcb_req *tzcb_req = cb_txn->cb_req;
  1869. union smcinvoke_tz_args *tz_args = tzcb_req->args;
  1870. size_t tzcb_req_len = cb_txn->cb_req_bytes;
  1871. size_t tz_buf_offset = TZCB_BUF_OFFSET(tzcb_req);
  1872. size_t user_req_buf_offset = sizeof(union smcinvoke_arg) *
  1873. OBJECT_COUNTS_TOTAL(tzcb_req->hdr.counts);
  1874. if (tz_buf_offset > tzcb_req_len) {
  1875. ret = -EINVAL;
  1876. goto out;
  1877. }
  1878. user_req->txn_id = cb_txn->txn_id;
  1879. if (get_uhandle_from_tzhandle(tzcb_req->hdr.tzhandle, srvr_id,
  1880. (int32_t*)(&user_req->cbobj_id), TAKE_LOCK,
  1881. SMCINVOKE_OBJ_TYPE_TZ_OBJ)) {
  1882. ret = -EINVAL;
  1883. goto out;
  1884. }
  1885. user_req->op = tzcb_req->hdr.op;
  1886. user_req->counts = tzcb_req->hdr.counts;
  1887. user_req->argsize = sizeof(union smcinvoke_arg);
  1888. trace_marshal_in_tzcb_req_handle(tzcb_req->hdr.tzhandle, srvr_id,
  1889. user_req->cbobj_id, user_req->op, user_req->counts);
  1890. FOR_ARGS(i, tzcb_req->hdr.counts, BI) {
  1891. user_req_buf_offset = size_align(user_req_buf_offset,
  1892. SMCINVOKE_ARGS_ALIGN_SIZE);
  1893. tmp_arg.b.size = tz_args[i].b.size;
  1894. if ((tz_args[i].b.offset > tzcb_req_len) ||
  1895. (tz_args[i].b.size > tzcb_req_len - tz_args[i].b.offset) ||
  1896. (user_req_buf_offset > user_req->buf_len) ||
  1897. (tmp_arg.b.size >
  1898. user_req->buf_len - user_req_buf_offset)) {
  1899. ret = -EINVAL;
  1900. pr_err("%s: buffer overflow detected\n", __func__);
  1901. goto out;
  1902. }
  1903. tmp_arg.b.addr = user_req->buf_addr + user_req_buf_offset;
  1904. if (copy_to_user(u64_to_user_ptr
  1905. (user_req->buf_addr + i * sizeof(tmp_arg)),
  1906. &tmp_arg, sizeof(tmp_arg)) ||
  1907. copy_to_user(u64_to_user_ptr(tmp_arg.b.addr),
  1908. (uint8_t *)(tzcb_req) + tz_args[i].b.offset,
  1909. tz_args[i].b.size)) {
  1910. ret = -EFAULT;
  1911. goto out;
  1912. }
  1913. user_req_buf_offset += tmp_arg.b.size;
  1914. }
  1915. FOR_ARGS(i, tzcb_req->hdr.counts, BO) {
  1916. user_req_buf_offset = size_align(user_req_buf_offset,
  1917. SMCINVOKE_ARGS_ALIGN_SIZE);
  1918. tmp_arg.b.size = tz_args[i].b.size;
  1919. if ((user_req_buf_offset > user_req->buf_len) ||
  1920. (tmp_arg.b.size >
  1921. user_req->buf_len - user_req_buf_offset)) {
  1922. ret = -EINVAL;
  1923. pr_err("%s: buffer overflow detected\n", __func__);
  1924. goto out;
  1925. }
  1926. tmp_arg.b.addr = user_req->buf_addr + user_req_buf_offset;
  1927. if (copy_to_user(u64_to_user_ptr
  1928. (user_req->buf_addr + i * sizeof(tmp_arg)),
  1929. &tmp_arg, sizeof(tmp_arg))) {
  1930. ret = -EFAULT;
  1931. goto out;
  1932. }
  1933. user_req_buf_offset += tmp_arg.b.size;
  1934. }
  1935. FOR_ARGS(i, tzcb_req->hdr.counts, OI) {
  1936. /*
  1937. * create a new FD and assign to output object's
  1938. * context
  1939. */
  1940. temp_fd = UHANDLE_NULL;
  1941. ret = get_uhandle_from_tzhandle(tz_args[i].handle, srvr_id,
  1942. &temp_fd, TAKE_LOCK, SMCINVOKE_OBJ_TYPE_TZ_OBJ);
  1943. tmp_arg.o.fd = temp_fd;
  1944. if (ret) {
  1945. ret = -EINVAL;
  1946. goto out;
  1947. }
  1948. if (copy_to_user(u64_to_user_ptr
  1949. (user_req->buf_addr + i * sizeof(tmp_arg)),
  1950. &tmp_arg, sizeof(tmp_arg))) {
  1951. ret = -EFAULT;
  1952. goto out;
  1953. }
  1954. trace_marshal_in_tzcb_req_fd(i, tz_args[i].handle, srvr_id, temp_fd);
  1955. }
  1956. out:
  1957. return ret;
  1958. }
  1959. static int marshal_out_tzcb_req(const struct smcinvoke_accept *user_req,
  1960. struct smcinvoke_cb_txn *cb_txn,
  1961. struct file **arr_filp)
  1962. {
  1963. int ret = -EINVAL, i = 0;
  1964. int32_t tzhandles_to_release[OBJECT_COUNTS_MAX_OO] = {0};
  1965. struct smcinvoke_tzcb_req *tzcb_req = cb_txn->cb_req;
  1966. union smcinvoke_tz_args *tz_args = tzcb_req->args;
  1967. size_t tz_buf_offset = TZCB_BUF_OFFSET(tzcb_req);
  1968. LIST_HEAD(l_mem_objs_pending_async); /* Holds new memory objects, to be later sent to TZ */
  1969. uint32_t max_offset = 0;
  1970. uint32_t buffer_size_max_offset = 0;
  1971. void* async_buf_begin;
  1972. size_t async_buf_size;
  1973. uint32_t offset = 0;
  1974. /* We assume 'marshal_in_tzcb_req' increases the ref-count of the CBOBJs.
  1975. * It should be the case for mem-obj as well. However, it does not do that.
  1976. * It is easier to filter out the 'release_tzhandles' for mem-obj here rather
  1977. * than increases its ref-count on 'marshal_in_tzcb_req'. Because, there is no
  1978. * reliable error handling and cleanup in 'marshal_in_tzcb_req'. So if it fails
  1979. * mem-obj may not get released. **/
  1980. if (!TZHANDLE_IS_MEM_OBJ(cb_txn->cb_req->hdr.tzhandle))
  1981. release_tzhandles(&cb_txn->cb_req->hdr.tzhandle, 1);
  1982. tzcb_req->result = user_req->result;
  1983. /* Return without marshaling user args if destination callback invocation was
  1984. unsuccessful. */
  1985. if (tzcb_req->result != 0) {
  1986. ret = 0;
  1987. goto out;
  1988. }
  1989. FOR_ARGS(i, tzcb_req->hdr.counts, BI) {
  1990. /* Find the max offset and the size of the buffer in that offset */
  1991. if (tz_args[i].b.offset > max_offset) {
  1992. max_offset = tz_args[i].b.offset;
  1993. buffer_size_max_offset = tz_args[i].b.size;
  1994. }
  1995. }
  1996. FOR_ARGS(i, tzcb_req->hdr.counts, BO) {
  1997. union smcinvoke_arg tmp_arg;
  1998. if (copy_from_user((uint8_t *)&tmp_arg, u64_to_user_ptr(
  1999. user_req->buf_addr + i * sizeof(union smcinvoke_arg)),
  2000. sizeof(union smcinvoke_arg))) {
  2001. ret = -EFAULT;
  2002. goto out;
  2003. }
  2004. if (tmp_arg.b.size > tz_args[i].b.size)
  2005. goto out;
  2006. if (copy_from_user((uint8_t *)(tzcb_req) + tz_args[i].b.offset,
  2007. u64_to_user_ptr(tmp_arg.b.addr),
  2008. tmp_arg.b.size)) {
  2009. ret = -EFAULT;
  2010. goto out;
  2011. }
  2012. /* Find the max offset and the size of the buffer in that offset */
  2013. if (tz_args[i].b.offset > max_offset) {
  2014. max_offset = tz_args[i].b.offset;
  2015. buffer_size_max_offset = tz_args[i].b.size;
  2016. }
  2017. }
  2018. FOR_ARGS(i, tzcb_req->hdr.counts, OO) {
  2019. union smcinvoke_arg tmp_arg;
  2020. if (copy_from_user((uint8_t *)&tmp_arg, u64_to_user_ptr(
  2021. user_req->buf_addr + i * sizeof(union smcinvoke_arg)),
  2022. sizeof(union smcinvoke_arg))) {
  2023. ret = -EFAULT;
  2024. goto out;
  2025. }
  2026. ret = get_tzhandle_from_uhandle(tmp_arg.o.fd,
  2027. tmp_arg.o.cb_server_fd, &arr_filp[i],
  2028. &(tz_args[i].handle), &l_mem_objs_pending_async);
  2029. if (ret)
  2030. goto out;
  2031. tzhandles_to_release[i] = tz_args[i].handle;
  2032. trace_marshal_out_tzcb_req(i, tmp_arg.o.fd,
  2033. tmp_arg.o.cb_server_fd, tz_args[i].handle);
  2034. }
  2035. ret = 0;
  2036. out:
  2037. FOR_ARGS(i, tzcb_req->hdr.counts, OI) {
  2038. if (TZHANDLE_IS_CB_OBJ(tz_args[i].handle))
  2039. release_tzhandles(&tz_args[i].handle, 1);
  2040. }
  2041. do {
  2042. if (mem_obj_async_support) {
  2043. /* We will be able to add the async information to the buffer beyond the data in the max offset, if exists.
  2044. * If doesn't exist, we can add the async information after the header and the args. */
  2045. offset = (max_offset ? (max_offset + buffer_size_max_offset) : tz_buf_offset);
  2046. offset = size_align(offset, SMCINVOKE_ARGS_ALIGN_SIZE);
  2047. async_buf_begin = (uint8_t *)tzcb_req + offset;
  2048. if (async_buf_begin - (void *)tzcb_req > g_max_cb_buf_size) {
  2049. pr_err("Unable to add memory object info to the async channel\n");
  2050. break;
  2051. } else {
  2052. async_buf_size = g_max_cb_buf_size - (async_buf_begin - (void *)tzcb_req);
  2053. }
  2054. mutex_lock(&g_smcinvoke_lock);
  2055. add_mem_obj_info_to_async_side_channel_locked(async_buf_begin, async_buf_size, &l_mem_objs_pending_async);
  2056. delete_pending_async_list_locked(&l_mem_objs_pending_async);
  2057. mutex_unlock(&g_smcinvoke_lock);
  2058. }
  2059. } while (0);
  2060. if (ret)
  2061. release_tzhandles(tzhandles_to_release, OBJECT_COUNTS_MAX_OO);
  2062. return ret;
  2063. }
  2064. static void set_tz_version (uint32_t tz_version)
  2065. {
  2066. tz_async_version = tz_version;
  2067. /* We enable async memory object support when TZ async
  2068. * version is equal or larger than the driver version.
  2069. * It is expected that if the protocol changes in later
  2070. * TZ versions, TZ will support backward compatibility
  2071. * so this condition should still be valid.
  2072. */
  2073. if (tz_version >= SMCINVOKE_ASYNC_VERSION) {
  2074. mem_obj_async_support = true;
  2075. pr_debug("Enabled asynchronous memory object support\n");
  2076. }
  2077. }
  2078. static void process_piggyback_data(void *buf, size_t buf_size)
  2079. {
  2080. int i;
  2081. struct smcinvoke_tzcb_req req = {0};
  2082. struct smcinvoke_piggyback_msg *msg = buf;
  2083. int32_t *objs = msg->objs;
  2084. if (msg->version == 0) {
  2085. /* QTEE reset the buffer if it is unused. */
  2086. return;
  2087. }
  2088. for (i = 0; i < msg->counts; i++) {
  2089. if (msg->op != OBJECT_OP_RELEASE) {
  2090. /* We only support release handler. */
  2091. break;
  2092. }
  2093. req.hdr.op = msg->op;
  2094. req.hdr.counts = 0; /* release op does not require any args */
  2095. req.hdr.tzhandle = objs[i];
  2096. if (tz_async_version == 0)
  2097. set_tz_version(msg->version);
  2098. process_tzcb_req(&req, sizeof(struct smcinvoke_tzcb_req), NULL);
  2099. /* cbobjs_in_flight will be adjusted during CB processing */
  2100. }
  2101. /* Reset output buffer after processing.*/
  2102. memset(buf, 0, buf_size);
  2103. }
  2104. /* Add memory object mapped data to the async side channel, so it's available to TZ
  2105. * together with the memory object.
  2106. *
  2107. * No return value as TZ is always able to explicitly ask for this information
  2108. * in case this function fails.
  2109. */
  2110. static void add_mem_obj_info_to_async_side_channel_locked(void *buf, size_t buf_size, struct list_head *l_pending_mem_obj)
  2111. {
  2112. struct smcinvoke_mem_obj_msg *msg = buf;
  2113. struct smcinvoke_mem_obj_pending_async *mem_obj_pending = NULL;
  2114. size_t header_size = 0;
  2115. size_t mo_size = 0;
  2116. size_t used = 0;
  2117. size_t index = 0;
  2118. if (list_empty(l_pending_mem_obj))
  2119. return;
  2120. header_size = sizeof(struct smcinvoke_mem_obj_msg);
  2121. mo_size = sizeof(struct smcinvoke_mem_obj_info);
  2122. /* Minimal size required is the header data + one mem obj info */
  2123. if (buf_size < header_size + mo_size) {
  2124. pr_err("Unable to add memory object info to async channel\n");
  2125. return;
  2126. }
  2127. msg->version = SMCINVOKE_ASYNC_VERSION;
  2128. msg->op = SMCINVOKE_ASYNC_OP_MEMORY_OBJECT;
  2129. msg->count = 0;
  2130. used = header_size;
  2131. index = 0;
  2132. list_for_each_entry(mem_obj_pending, l_pending_mem_obj, list) {
  2133. if (NULL == mem_obj_pending->mem_obj) {
  2134. pr_err("Memory object is no longer valid\n");
  2135. continue;
  2136. }
  2137. if (used + mo_size > buf_size) {
  2138. pr_err("Not all memory object info was added to the async channel\n");
  2139. break;
  2140. }
  2141. msg->mo[index].memObjRef = TZHANDLE_MAKE_LOCAL(MEM_RGN_SRVR_ID, mem_obj_pending->mem_obj->mem_region_id);
  2142. msg->mo[index].mapObjRef = TZHANDLE_MAKE_LOCAL(MEM_MAP_SRVR_ID, mem_obj_pending->mem_obj->mem_map_obj_id);
  2143. msg->mo[index].addr = mem_obj_pending->mem_obj->p_addr;
  2144. msg->mo[index].size = mem_obj_pending->mem_obj->p_addr_len;
  2145. msg->mo[index].perm = SMCINVOKE_MEM_PERM_RW;
  2146. used += sizeof(msg->mo[index]);
  2147. index++;
  2148. }
  2149. msg->count = index;
  2150. pr_debug("Added %lu memory objects to the side channel, total size = %zu\n", index, used);
  2151. return;
  2152. }
  2153. /*
  2154. * Delete entire pending async list.
  2155. */
  2156. static void delete_pending_async_list_locked(struct list_head *l_pending_mem_obj)
  2157. {
  2158. struct smcinvoke_mem_obj_pending_async *mem_obj_pending = NULL;
  2159. struct smcinvoke_mem_obj_pending_async *temp = NULL;
  2160. if (list_empty(l_pending_mem_obj))
  2161. return;
  2162. list_for_each_entry_safe(mem_obj_pending, temp, l_pending_mem_obj, list) {
  2163. mem_obj_pending->mem_obj = NULL;
  2164. list_del(&mem_obj_pending->list);
  2165. kfree(mem_obj_pending);
  2166. }
  2167. }
  2168. /*
  2169. * Unmap/release the mapped objects from pending async list.
  2170. */
  2171. static void release_map_obj_pending_async_list_locked(struct list_head *l_pending_mem_obj)
  2172. {
  2173. struct smcinvoke_mem_obj_pending_async *mem_obj_pending = NULL;
  2174. struct smcinvoke_mem_obj_pending_async *temp = NULL;
  2175. if (list_empty(l_pending_mem_obj))
  2176. return;
  2177. list_for_each_entry_safe(mem_obj_pending, temp, l_pending_mem_obj, list) {
  2178. kref_put(&mem_obj_pending->mem_obj->mem_map_obj_ref_cnt, del_mem_map_obj_locked);
  2179. }
  2180. }
  2181. static long process_ack_local_obj(struct file *filp, unsigned int cmd,
  2182. unsigned long arg)
  2183. {
  2184. int ret = -1;
  2185. int32_t local_obj = SMCINVOKE_USERSPACE_OBJ_NULL;
  2186. struct smcinvoke_file_data *filp_data = filp->private_data;
  2187. if (_IOC_SIZE(cmd) != sizeof(int32_t))
  2188. return -EINVAL;
  2189. ret = copy_from_user(&local_obj, (void __user *)(uintptr_t)arg,
  2190. sizeof(int32_t));
  2191. if (ret)
  2192. return -EFAULT;
  2193. mutex_lock(&g_smcinvoke_lock);
  2194. if (UHANDLE_IS_CB_OBJ(local_obj))
  2195. ret = put_pending_cbobj_locked(filp_data->server_id,
  2196. UHANDLE_GET_CB_OBJ(local_obj));
  2197. mutex_unlock(&g_smcinvoke_lock);
  2198. return ret;
  2199. }
  2200. static long process_server_req(struct file *filp, unsigned int cmd,
  2201. unsigned long arg)
  2202. {
  2203. int ret = -1;
  2204. int32_t server_fd = -1;
  2205. struct smcinvoke_server server_req = {0};
  2206. struct smcinvoke_server_info *server_info = NULL;
  2207. if (_IOC_SIZE(cmd) != sizeof(server_req)) {
  2208. pr_err("invalid command size received for server request\n");
  2209. return -EINVAL;
  2210. }
  2211. ret = copy_from_user(&server_req, (void __user *)(uintptr_t)arg,
  2212. sizeof(server_req));
  2213. if (ret) {
  2214. pr_err("copying server request from user failed\n");
  2215. return -EFAULT;
  2216. }
  2217. server_info = kzalloc(sizeof(*server_info), GFP_KERNEL);
  2218. if (!server_info)
  2219. return -ENOMEM;
  2220. kref_init(&server_info->ref_cnt);
  2221. init_waitqueue_head(&server_info->req_wait_q);
  2222. init_waitqueue_head(&server_info->rsp_wait_q);
  2223. server_info->cb_buf_size = server_req.cb_buf_size;
  2224. hash_init(server_info->reqs_table);
  2225. hash_init(server_info->responses_table);
  2226. INIT_LIST_HEAD(&server_info->pending_cbobjs);
  2227. server_info->is_server_suspended = 0;
  2228. mutex_lock(&g_smcinvoke_lock);
  2229. server_info->server_id = next_cb_server_id_locked();
  2230. hash_add(g_cb_servers, &server_info->hash,
  2231. server_info->server_id);
  2232. if (g_max_cb_buf_size < server_req.cb_buf_size)
  2233. g_max_cb_buf_size = server_req.cb_buf_size;
  2234. mutex_unlock(&g_smcinvoke_lock);
  2235. ret = get_fd_for_obj(SMCINVOKE_OBJ_TYPE_SERVER,
  2236. server_info->server_id, &server_fd);
  2237. if (ret)
  2238. release_cb_server(server_info->server_id);
  2239. return server_fd;
  2240. }
  2241. static long process_accept_req(struct file *filp, unsigned int cmd,
  2242. unsigned long arg)
  2243. {
  2244. int ret = -1;
  2245. struct smcinvoke_file_data *server_obj = filp->private_data;
  2246. struct smcinvoke_accept user_args = {0};
  2247. struct smcinvoke_cb_txn *cb_txn = NULL;
  2248. struct smcinvoke_server_info *server_info = NULL;
  2249. if (_IOC_SIZE(cmd) != sizeof(struct smcinvoke_accept)) {
  2250. pr_err("command size invalid for accept request\n");
  2251. return -EINVAL;
  2252. }
  2253. if (copy_from_user(&user_args, (void __user *)arg,
  2254. sizeof(struct smcinvoke_accept))) {
  2255. pr_err("copying accept request from user failed\n");
  2256. return -EFAULT;
  2257. }
  2258. if (user_args.argsize != sizeof(union smcinvoke_arg)) {
  2259. pr_err("arguments size is invalid for accept thread\n");
  2260. return -EINVAL;
  2261. }
  2262. /* ACCEPT is available only on server obj */
  2263. if (server_obj->context_type != SMCINVOKE_OBJ_TYPE_SERVER) {
  2264. pr_err("invalid object type received for accept req\n");
  2265. return -EPERM;
  2266. }
  2267. mutex_lock(&g_smcinvoke_lock);
  2268. server_info = get_cb_server_locked(server_obj->server_id);
  2269. if (!server_info) {
  2270. pr_err("No matching server with server id : %u found\n",
  2271. server_obj->server_id);
  2272. mutex_unlock(&g_smcinvoke_lock);
  2273. return -EINVAL;
  2274. }
  2275. if (server_info->state == SMCINVOKE_SERVER_STATE_DEFUNCT)
  2276. server_info->state = 0;
  2277. server_info->is_server_suspended = UNSET_BIT(server_info->is_server_suspended,
  2278. (current->pid)%DEFAULT_CB_OBJ_THREAD_CNT);
  2279. mutex_unlock(&g_smcinvoke_lock);
  2280. /* First check if it has response otherwise wait for req */
  2281. if (user_args.has_resp) {
  2282. trace_process_accept_req_has_response(current->pid, current->tgid);
  2283. mutex_lock(&g_smcinvoke_lock);
  2284. cb_txn = find_cbtxn_locked(server_info, user_args.txn_id,
  2285. SMCINVOKE_REQ_PROCESSING);
  2286. mutex_unlock(&g_smcinvoke_lock);
  2287. /*
  2288. * cb_txn can be null if userspace provides wrong txn id OR
  2289. * invoke thread died while server was processing cb req.
  2290. * if invoke thread dies, it would remove req from Q. So
  2291. * no matching cb_txn would be on Q and hence NULL cb_txn.
  2292. * In this case, we want this thread to start waiting
  2293. * new cb requests.
  2294. */
  2295. if (!cb_txn) {
  2296. pr_err_ratelimited("%s txn %llu either invalid or removed from Q\n",
  2297. __func__, user_args.txn_id);
  2298. goto start_waiting_for_requests;
  2299. }
  2300. ret = marshal_out_tzcb_req(&user_args, cb_txn,
  2301. cb_txn->filp_to_release);
  2302. /*
  2303. * if client did not set error and we get error locally,
  2304. * we return local error to TA
  2305. */
  2306. if (ret && cb_txn->cb_req->result == 0)
  2307. cb_txn->cb_req->result = OBJECT_ERROR_UNAVAIL;
  2308. cb_txn->state = SMCINVOKE_REQ_PROCESSED;
  2309. mutex_lock(&g_smcinvoke_lock);
  2310. kref_put(&cb_txn->ref_cnt, delete_cb_txn_locked);
  2311. mutex_unlock(&g_smcinvoke_lock);
  2312. wake_up(&server_info->rsp_wait_q);
  2313. /*
  2314. * if marshal_out fails, we should let userspace release
  2315. * any ref/obj it created for CB processing
  2316. */
  2317. if (ret && OBJECT_COUNTS_NUM_OO(user_args.counts))
  2318. goto out;
  2319. }
  2320. start_waiting_for_requests:
  2321. /*
  2322. * Once response has been delivered, thread will wait for another
  2323. * callback req to process.
  2324. */
  2325. do {
  2326. ret = wait_event_interruptible(server_info->req_wait_q,
  2327. !hash_empty(server_info->reqs_table));
  2328. if (ret) {
  2329. trace_process_accept_req_ret(current->pid, current->tgid, ret);
  2330. /*
  2331. * Ideally, we should destroy server if accept threads
  2332. * are returning due to client being killed or device
  2333. * going down (Shutdown/Reboot) but that would make
  2334. * server_info invalid. Other accept/invoke threads are
  2335. * using server_info and would crash. So dont do that.
  2336. */
  2337. mutex_lock(&g_smcinvoke_lock);
  2338. if(freezing(current)) {
  2339. pr_err_ratelimited("Server id :%d interrupted probaby due to suspend, pid:%d\n",
  2340. server_info->server_id, current->pid);
  2341. /*
  2342. * Each accept thread is identified by bits ranging from
  2343. * 0 to DEFAULT_CBOBJ_THREAD_CNT-1. When an accept thread is
  2344. * interrupted by a signal other than SIGUSR1,SIGKILL,SIGTERM,
  2345. * set the corresponding bit of accept thread, indicating that
  2346. * current accept thread's state to be "suspended"/ or something
  2347. * that needs infinite timeout for invoke thread.
  2348. */
  2349. server_info->is_server_suspended =
  2350. SET_BIT(server_info->is_server_suspended,
  2351. (current->pid)%DEFAULT_CB_OBJ_THREAD_CNT);
  2352. } else {
  2353. pr_err_ratelimited("Setting pid:%d, server id : %d state to defunct\n",
  2354. current->pid, server_info->server_id);
  2355. server_info->state = SMCINVOKE_SERVER_STATE_DEFUNCT;
  2356. }
  2357. mutex_unlock(&g_smcinvoke_lock);
  2358. wake_up_interruptible(&server_info->rsp_wait_q);
  2359. goto out;
  2360. }
  2361. mutex_lock(&g_smcinvoke_lock);
  2362. cb_txn = find_cbtxn_locked(server_info,
  2363. SMCINVOKE_NEXT_AVAILABLE_TXN,
  2364. SMCINVOKE_REQ_PLACED);
  2365. mutex_unlock(&g_smcinvoke_lock);
  2366. if (cb_txn) {
  2367. cb_txn->state = SMCINVOKE_REQ_PROCESSING;
  2368. ret = marshal_in_tzcb_req(cb_txn, &user_args,
  2369. server_obj->server_id);
  2370. if (ret) {
  2371. pr_err("failed to marshal in the callback request\n");
  2372. cb_txn->cb_req->result = OBJECT_ERROR_UNAVAIL;
  2373. cb_txn->state = SMCINVOKE_REQ_PROCESSED;
  2374. mutex_lock(&g_smcinvoke_lock);
  2375. kref_put(&cb_txn->ref_cnt, delete_cb_txn_locked);
  2376. mutex_unlock(&g_smcinvoke_lock);
  2377. wake_up_interruptible(&server_info->rsp_wait_q);
  2378. continue;
  2379. }
  2380. mutex_lock(&g_smcinvoke_lock);
  2381. hash_add(server_info->responses_table, &cb_txn->hash,
  2382. cb_txn->txn_id);
  2383. kref_put(&cb_txn->ref_cnt, delete_cb_txn_locked);
  2384. mutex_unlock(&g_smcinvoke_lock);
  2385. trace_process_accept_req_placed(current->pid, current->tgid);
  2386. ret = copy_to_user((void __user *)arg, &user_args,
  2387. sizeof(struct smcinvoke_accept));
  2388. }
  2389. } while (!cb_txn);
  2390. out:
  2391. if (server_info)
  2392. kref_put(&server_info->ref_cnt, destroy_cb_server);
  2393. if (ret && ret != -ERESTARTSYS)
  2394. pr_err("accept thread returning with ret: %d\n", ret);
  2395. return ret;
  2396. }
  2397. static long process_invoke_req(struct file *filp, unsigned int cmd,
  2398. unsigned long arg)
  2399. {
  2400. int ret = -1, nr_args = 0;
  2401. struct smcinvoke_cmd_req req = {0};
  2402. void *in_msg = NULL, *out_msg = NULL;
  2403. size_t inmsg_size = 0, outmsg_size = SMCINVOKE_TZ_MIN_BUF_SIZE;
  2404. union smcinvoke_arg *args_buf = NULL;
  2405. struct smcinvoke_file_data *tzobj = filp->private_data;
  2406. struct qtee_shm in_shm = {0}, out_shm = {0};
  2407. LIST_HEAD(l_mem_objs_pending_async); /* Holds new memory objects, to be later sent to TZ */
  2408. /*
  2409. * Hold reference to remote object until invoke op is not
  2410. * completed. Release once invoke is done.
  2411. */
  2412. struct file *filp_to_release[OBJECT_COUNTS_MAX_OO] = {NULL};
  2413. /*
  2414. * If anything goes wrong, release alloted tzhandles for
  2415. * local objs which could be either CBObj or MemObj.
  2416. */
  2417. int32_t tzhandles_to_release[OBJECT_COUNTS_MAX_OO] = {0};
  2418. bool tz_acked = false;
  2419. uint32_t context_type = tzobj->context_type;
  2420. if (context_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ &&
  2421. _IOC_SIZE(cmd) != sizeof(req)) {
  2422. pr_err("command size for invoke req is invalid\n");
  2423. return -EINVAL;
  2424. }
  2425. if (context_type != SMCINVOKE_OBJ_TYPE_TZ_OBJ &&
  2426. context_type != SMCINVOKE_OBJ_TYPE_TZ_OBJ_FOR_KERNEL) {
  2427. pr_err("invalid context_type %d\n", context_type);
  2428. return -EPERM;
  2429. }
  2430. if (context_type != SMCINVOKE_OBJ_TYPE_TZ_OBJ_FOR_KERNEL) {
  2431. ret = copy_from_user(&req, (void __user *)arg, sizeof(req));
  2432. if (ret) {
  2433. pr_err("copying invoke req failed\n");
  2434. return -EFAULT;
  2435. }
  2436. } else {
  2437. req = *(struct smcinvoke_cmd_req *)arg;
  2438. }
  2439. if (req.argsize != sizeof(union smcinvoke_arg)) {
  2440. pr_err("arguments size for invoke req is invalid\n");
  2441. return -EINVAL;
  2442. }
  2443. if (context_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ &&
  2444. tzobj->tzhandle == SMCINVOKE_TZ_ROOT_OBJ &&
  2445. (req.op == IClientEnv_OP_notifyDomainChange ||
  2446. req.op == IClientEnv_OP_registerWithCredentials ||
  2447. req.op == IClientEnv_OP_adciAccept ||
  2448. req.op == IClientEnv_OP_adciShutdown)) {
  2449. pr_err("invalid rootenv op\n");
  2450. return -EINVAL;
  2451. }
  2452. nr_args = OBJECT_COUNTS_NUM_buffers(req.counts) +
  2453. OBJECT_COUNTS_NUM_objects(req.counts);
  2454. if (nr_args) {
  2455. args_buf = kcalloc(nr_args, req.argsize, GFP_KERNEL);
  2456. if (!args_buf)
  2457. return -ENOMEM;
  2458. if (context_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ) {
  2459. ret = copy_from_user(args_buf,
  2460. u64_to_user_ptr(req.args),
  2461. nr_args * req.argsize);
  2462. if (ret) {
  2463. ret = -EFAULT;
  2464. goto out;
  2465. }
  2466. } else {
  2467. memcpy(args_buf, (void *)(req.args),
  2468. nr_args * req.argsize);
  2469. }
  2470. }
  2471. inmsg_size = compute_in_msg_size(&req, args_buf);
  2472. ret = qtee_shmbridge_allocate_shm(inmsg_size, &in_shm);
  2473. if (ret) {
  2474. ret = -ENOMEM;
  2475. pr_err("shmbridge alloc failed for in msg in invoke req\n");
  2476. goto out;
  2477. }
  2478. in_msg = in_shm.vaddr;
  2479. mutex_lock(&g_smcinvoke_lock);
  2480. outmsg_size = PAGE_ALIGN(g_max_cb_buf_size);
  2481. mutex_unlock(&g_smcinvoke_lock);
  2482. ret = qtee_shmbridge_allocate_shm(outmsg_size, &out_shm);
  2483. if (ret) {
  2484. ret = -ENOMEM;
  2485. pr_err("shmbridge alloc failed for out msg in invoke req\n");
  2486. goto out;
  2487. }
  2488. out_msg = out_shm.vaddr;
  2489. trace_process_invoke_req_tzhandle(tzobj->tzhandle, req.op, req.counts);
  2490. ret = marshal_in_invoke_req(&req, args_buf, tzobj->tzhandle, in_msg,
  2491. inmsg_size, filp_to_release, tzhandles_to_release,
  2492. context_type, &l_mem_objs_pending_async);
  2493. if (ret) {
  2494. pr_err("failed to marshal in invoke req, ret :%d\n", ret);
  2495. goto out;
  2496. }
  2497. if (mem_obj_async_support) {
  2498. mutex_lock(&g_smcinvoke_lock);
  2499. add_mem_obj_info_to_async_side_channel_locked(out_msg, outmsg_size, &l_mem_objs_pending_async);
  2500. mutex_unlock(&g_smcinvoke_lock);
  2501. }
  2502. ret = prepare_send_scm_msg(in_msg, in_shm.paddr, inmsg_size,
  2503. out_msg, out_shm.paddr, outmsg_size,
  2504. &req, args_buf, &tz_acked, context_type,
  2505. &in_shm, &out_shm, true);
  2506. /*
  2507. * If scm_call is success, TZ owns responsibility to release
  2508. * refs for local objs.
  2509. */
  2510. if (!tz_acked) {
  2511. trace_status(__func__, "scm call successful");
  2512. goto out;
  2513. }
  2514. memset(tzhandles_to_release, 0, sizeof(tzhandles_to_release));
  2515. /*
  2516. * if invoke op results in an err, no need to marshal_out and
  2517. * copy args buf to user space
  2518. */
  2519. if (!req.result) {
  2520. /*
  2521. * Dont check ret of marshal_out because there might be a
  2522. * FD for OO which userspace must release even if an error
  2523. * occurs. Releasing FD from user space is much simpler than
  2524. * doing here. ORing of ret is reqd not to miss past error
  2525. */
  2526. if (context_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ)
  2527. ret |= copy_to_user(u64_to_user_ptr(req.args),
  2528. args_buf, nr_args * req.argsize);
  2529. else
  2530. memcpy((void *)(req.args), args_buf,
  2531. nr_args * req.argsize);
  2532. }
  2533. /* copy result of invoke op */
  2534. if (context_type == SMCINVOKE_OBJ_TYPE_TZ_OBJ) {
  2535. ret |= copy_to_user((void __user *)arg, &req, sizeof(req));
  2536. if (ret)
  2537. goto out;
  2538. } else {
  2539. memcpy((void *)arg, (void *)&req, sizeof(req));
  2540. }
  2541. /* Outbuf could be carrying local objs to be released. */
  2542. process_piggyback_data(out_msg, outmsg_size);
  2543. out:
  2544. trace_process_invoke_req_result(ret, req.result, tzobj->tzhandle,
  2545. req.op, req.counts);
  2546. release_filp(filp_to_release, OBJECT_COUNTS_MAX_OO);
  2547. if (ret) {
  2548. release_map_obj_pending_async_list_locked(&l_mem_objs_pending_async);
  2549. release_tzhandles(tzhandles_to_release, OBJECT_COUNTS_MAX_OO);
  2550. }
  2551. delete_pending_async_list_locked(&l_mem_objs_pending_async);
  2552. qtee_shmbridge_free_shm(&in_shm);
  2553. qtee_shmbridge_free_shm(&out_shm);
  2554. kfree(args_buf);
  2555. if (ret)
  2556. pr_err("invoke thread returning with ret = %d\n", ret);
  2557. return ret;
  2558. }
  2559. static long process_log_info(struct file *filp, unsigned int cmd,
  2560. unsigned long arg)
  2561. {
  2562. int ret = 0;
  2563. char buf[SMCINVOKE_LOG_BUF_SIZE];
  2564. struct smcinvoke_file_data *tzobj = filp->private_data;
  2565. ret = copy_from_user(buf, (void __user *)arg, SMCINVOKE_LOG_BUF_SIZE);
  2566. if (ret) {
  2567. pr_err("logging HLOS info copy failed\n");
  2568. return -EFAULT;
  2569. }
  2570. buf[SMCINVOKE_LOG_BUF_SIZE - 1] = '\0';
  2571. trace_process_log_info(buf, tzobj->context_type, tzobj->tzhandle);
  2572. return ret;
  2573. }
  2574. static long smcinvoke_ioctl(struct file *filp, unsigned int cmd,
  2575. unsigned long arg)
  2576. {
  2577. long ret = 0;
  2578. switch (cmd) {
  2579. case SMCINVOKE_IOCTL_INVOKE_REQ:
  2580. ret = process_invoke_req(filp, cmd, arg);
  2581. break;
  2582. case SMCINVOKE_IOCTL_ACCEPT_REQ:
  2583. ret = process_accept_req(filp, cmd, arg);
  2584. break;
  2585. case SMCINVOKE_IOCTL_SERVER_REQ:
  2586. ret = process_server_req(filp, cmd, arg);
  2587. break;
  2588. case SMCINVOKE_IOCTL_ACK_LOCAL_OBJ:
  2589. ret = process_ack_local_obj(filp, cmd, arg);
  2590. break;
  2591. case SMCINVOKE_IOCTL_LOG:
  2592. ret = process_log_info(filp, cmd, arg);
  2593. break;
  2594. default:
  2595. ret = -ENOIOCTLCMD;
  2596. break;
  2597. }
  2598. trace_smcinvoke_ioctl(cmd, ret);
  2599. return ret;
  2600. }
  2601. int get_root_fd(int *root_fd)
  2602. {
  2603. if (!root_fd)
  2604. return -EINVAL;
  2605. else
  2606. return get_fd_for_obj(SMCINVOKE_OBJ_TYPE_TZ_OBJ_FOR_KERNEL,
  2607. SMCINVOKE_TZ_ROOT_OBJ, root_fd);
  2608. }
  2609. int process_invoke_request_from_kernel_client(int fd,
  2610. struct smcinvoke_cmd_req *req)
  2611. {
  2612. struct file *filp = NULL;
  2613. int ret = 0;
  2614. if (!req) {
  2615. pr_err("NULL req\n");
  2616. return -EINVAL;
  2617. }
  2618. filp = fget(fd);
  2619. if (!filp) {
  2620. pr_err("Invalid fd %d\n", fd);
  2621. return -EINVAL;
  2622. }
  2623. ret = process_invoke_req(filp, 0, (uintptr_t)req);
  2624. fput(filp);
  2625. trace_process_invoke_request_from_kernel_client(fd, filp, file_count(filp));
  2626. return ret;
  2627. }
  2628. static int smcinvoke_open(struct inode *nodp, struct file *filp)
  2629. {
  2630. struct smcinvoke_file_data *tzcxt = NULL;
  2631. tzcxt = kzalloc(sizeof(*tzcxt), GFP_KERNEL);
  2632. if (!tzcxt)
  2633. return -ENOMEM;
  2634. tzcxt->tzhandle = SMCINVOKE_TZ_ROOT_OBJ;
  2635. tzcxt->context_type = SMCINVOKE_OBJ_TYPE_TZ_OBJ;
  2636. filp->private_data = tzcxt;
  2637. return 0;
  2638. }
  2639. static int release_cb_server(uint16_t server_id)
  2640. {
  2641. struct smcinvoke_server_info *server = NULL;
  2642. mutex_lock(&g_smcinvoke_lock);
  2643. server = find_cb_server_locked(server_id);
  2644. if (server)
  2645. kref_put(&server->ref_cnt, destroy_cb_server);
  2646. mutex_unlock(&g_smcinvoke_lock);
  2647. return 0;
  2648. }
  2649. int smcinvoke_release_filp(struct file *filp)
  2650. {
  2651. int ret = 0;
  2652. struct smcinvoke_file_data *file_data = filp->private_data;
  2653. uint32_t tzhandle = 0;
  2654. struct smcinvoke_object_release_pending_list *entry = NULL;
  2655. struct qtee_shm in_shm = {0}, out_shm = {0};
  2656. trace_smcinvoke_release_filp(current->files, filp,
  2657. file_count(filp), file_data->context_type);
  2658. if (file_data->context_type == SMCINVOKE_OBJ_TYPE_SERVER) {
  2659. ret = release_cb_server(file_data->server_id);
  2660. goto out;
  2661. }
  2662. tzhandle = file_data->tzhandle;
  2663. /* Root object is special in sense it is indestructible */
  2664. if (!tzhandle || tzhandle == SMCINVOKE_TZ_ROOT_OBJ) {
  2665. if (!tzhandle)
  2666. pr_err("tzhandle not valid in object release\n");
  2667. goto out;
  2668. }
  2669. ret = qtee_shmbridge_allocate_shm(SMCINVOKE_TZ_MIN_BUF_SIZE, &in_shm);
  2670. if (ret) {
  2671. pr_err("shmbridge alloc failed for in msg in object release"
  2672. "with ret %d\n", ret);
  2673. goto out;
  2674. }
  2675. ret = qtee_shmbridge_allocate_shm(SMCINVOKE_TZ_MIN_BUF_SIZE, &out_shm);
  2676. if (ret) {
  2677. pr_err("shmbridge alloc failed for out msg in object release"
  2678. "with ret:%d\n", ret);
  2679. goto out;
  2680. }
  2681. ret = smcinvoke_release_tz_object(&in_shm, &out_shm,
  2682. tzhandle, file_data->context_type);
  2683. if (-EBUSY == ret) {
  2684. pr_debug("failed to release handle in sync adding to list\n");
  2685. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  2686. if (!entry) {
  2687. ret = -ENOMEM;
  2688. goto out;
  2689. }
  2690. ret = 0;
  2691. entry->data.tzhandle = tzhandle;
  2692. entry->data.context_type = file_data->context_type;
  2693. mutex_lock(&object_postprocess_lock);
  2694. list_add_tail(&entry->list, &g_object_postprocess);
  2695. mutex_unlock(&object_postprocess_lock);
  2696. pr_debug("Object release list: added a handle:%u\n", tzhandle);
  2697. __wakeup_postprocess_kthread(&smcinvoke[OBJECT_WORKER_THREAD]);
  2698. }
  2699. out:
  2700. qtee_shmbridge_free_shm(&in_shm);
  2701. qtee_shmbridge_free_shm(&out_shm);
  2702. kfree(filp->private_data);
  2703. filp->private_data = NULL;
  2704. if (ret != 0)
  2705. pr_err ("Object release failed with ret %d\n", ret);
  2706. return ret;
  2707. }
  2708. int smcinvoke_release_from_kernel_client(int fd)
  2709. {
  2710. struct file *filp = NULL;
  2711. /* use fget() to get filp, but this will increase file ref_cnt to 1,
  2712. * then decrease file ref_cnt to 0 with fput().
  2713. */
  2714. filp = fget(fd);
  2715. if (!filp) {
  2716. pr_err("invalid fd %d to release\n", fd);
  2717. return -EINVAL;
  2718. }
  2719. trace_smcinvoke_release_from_kernel_client(current->files, filp,
  2720. file_count(filp));
  2721. /* free filp, notify TZ to release object */
  2722. smcinvoke_release_filp(filp);
  2723. fput(filp);
  2724. return 0;
  2725. }
  2726. static int smcinvoke_release(struct inode *nodp, struct file *filp)
  2727. {
  2728. trace_smcinvoke_release(current->files, filp, file_count(filp),
  2729. filp->private_data);
  2730. if (filp->private_data)
  2731. return smcinvoke_release_filp(filp);
  2732. else
  2733. return 0;
  2734. }
  2735. static int smcinvoke_probe(struct platform_device *pdev)
  2736. {
  2737. unsigned int baseminor = 0;
  2738. unsigned int count = 1;
  2739. int rc = 0;
  2740. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  2741. if (rc) {
  2742. pr_err("dma_set_mask_and_coherent failed %d\n", rc);
  2743. return rc;
  2744. }
  2745. legacy_smc_call = of_property_read_bool((&pdev->dev)->of_node,
  2746. "qcom,support-legacy_smc");
  2747. invoke_cmd = legacy_smc_call ? SMCINVOKE_INVOKE_CMD_LEGACY : SMCINVOKE_INVOKE_CMD;
  2748. rc = smcinvoke_create_kthreads();
  2749. if (rc) {
  2750. pr_err("smcinvoke_create_kthreads failed %d\n", rc);
  2751. return rc;
  2752. }
  2753. rc = alloc_chrdev_region(&smcinvoke_device_no, baseminor, count,
  2754. SMCINVOKE_DEV);
  2755. if (rc < 0) {
  2756. pr_err("chrdev_region failed %d for %s\n", rc, SMCINVOKE_DEV);
  2757. goto exit_destroy_wkthread;
  2758. }
  2759. #if (KERNEL_VERSION(6, 3, 0) <= LINUX_VERSION_CODE)
  2760. driver_class = class_create(SMCINVOKE_DEV);
  2761. #else
  2762. driver_class = class_create(THIS_MODULE, SMCINVOKE_DEV);
  2763. #endif
  2764. if (IS_ERR(driver_class)) {
  2765. rc = -ENOMEM;
  2766. pr_err("class_create failed %d\n", rc);
  2767. goto exit_unreg_chrdev_region;
  2768. }
  2769. class_dev = device_create(driver_class, NULL, smcinvoke_device_no,
  2770. NULL, SMCINVOKE_DEV);
  2771. if (!class_dev) {
  2772. pr_err("class_device_create failed %d\n", rc);
  2773. rc = -ENOMEM;
  2774. goto exit_destroy_class;
  2775. }
  2776. cdev_init(&smcinvoke_cdev, &g_smcinvoke_fops);
  2777. smcinvoke_cdev.owner = THIS_MODULE;
  2778. rc = cdev_add(&smcinvoke_cdev, MKDEV(MAJOR(smcinvoke_device_no), 0),
  2779. count);
  2780. if (rc < 0) {
  2781. pr_err("cdev_add failed %d for %s\n", rc, SMCINVOKE_DEV);
  2782. goto exit_destroy_device;
  2783. }
  2784. smcinvoke_pdev = pdev;
  2785. #if IS_ENABLED(CONFIG_QSEECOM_COMPAT) && IS_ENABLED(CONFIG_QSEECOM_PROXY)
  2786. /*If the api fails to get the func ops, print the error and continue
  2787. * Do not treat it as fatal*/
  2788. rc = get_qseecom_kernel_fun_ops();
  2789. if (rc) {
  2790. pr_err("failed to get qseecom kernel func ops %d", rc);
  2791. }
  2792. #endif
  2793. __wakeup_postprocess_kthread(&smcinvoke[ADCI_WORKER_THREAD]);
  2794. return 0;
  2795. exit_destroy_device:
  2796. device_destroy(driver_class, smcinvoke_device_no);
  2797. exit_destroy_class:
  2798. class_destroy(driver_class);
  2799. exit_unreg_chrdev_region:
  2800. unregister_chrdev_region(smcinvoke_device_no, count);
  2801. exit_destroy_wkthread:
  2802. smcinvoke_destroy_kthreads();
  2803. return rc;
  2804. }
  2805. static int smcinvoke_remove(struct platform_device *pdev)
  2806. {
  2807. int count = 1;
  2808. smcinvoke_destroy_kthreads();
  2809. cdev_del(&smcinvoke_cdev);
  2810. device_destroy(driver_class, smcinvoke_device_no);
  2811. class_destroy(driver_class);
  2812. unregister_chrdev_region(smcinvoke_device_no, count);
  2813. return 0;
  2814. }
  2815. static int __maybe_unused smcinvoke_suspend(struct platform_device *pdev,
  2816. pm_message_t state)
  2817. {
  2818. int ret = 0;
  2819. mutex_lock(&g_smcinvoke_lock);
  2820. if (cb_reqs_inflight) {
  2821. pr_err("Failed to suspend smcinvoke driver\n");
  2822. ret = -EIO;
  2823. }
  2824. mutex_unlock(&g_smcinvoke_lock);
  2825. return ret;
  2826. }
  2827. static int __maybe_unused smcinvoke_resume(struct platform_device *pdev)
  2828. {
  2829. return 0;
  2830. }
  2831. static const struct of_device_id smcinvoke_match[] = {
  2832. {
  2833. .compatible = "qcom,smcinvoke",
  2834. },
  2835. {},
  2836. };
  2837. static struct platform_driver smcinvoke_plat_driver = {
  2838. .probe = smcinvoke_probe,
  2839. .remove = smcinvoke_remove,
  2840. .suspend = smcinvoke_suspend,
  2841. .resume = smcinvoke_resume,
  2842. .driver = {
  2843. .name = "smcinvoke",
  2844. .of_match_table = smcinvoke_match,
  2845. },
  2846. };
  2847. static int smcinvoke_init(void)
  2848. {
  2849. return platform_driver_register(&smcinvoke_plat_driver);
  2850. }
  2851. static void smcinvoke_exit(void)
  2852. {
  2853. platform_driver_unregister(&smcinvoke_plat_driver);
  2854. }
  2855. module_init(smcinvoke_init);
  2856. module_exit(smcinvoke_exit);
  2857. MODULE_LICENSE("GPL v2");
  2858. MODULE_DESCRIPTION("SMC Invoke driver");
  2859. MODULE_IMPORT_NS(VFS_internal_I_am_really_a_filesystem_and_am_NOT_a_driver);
  2860. MODULE_IMPORT_NS(DMA_BUF);