smcinvoke.c 76 KB

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