smcinvoke.c 76 KB

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