smcinvoke.c 79 KB

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