smcinvoke.c 82 KB

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