smcinvoke.c 91 KB

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