smcinvoke.c 93 KB

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