smcinvoke.c 92 KB

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