smc_abi.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2021, 2023 Linaro Limited
  4. * Copyright (c) 2016, EPAM Systems
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/arm-smccc.h>
  8. #include <linux/cpuhotplug.h>
  9. #include <linux/errno.h>
  10. #include <linux/firmware.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/irqdomain.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/rpmb.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <linux/string.h>
  25. #include <linux/tee_core.h>
  26. #include <linux/types.h>
  27. #include <linux/workqueue.h>
  28. #include "optee_private.h"
  29. #include "optee_smc.h"
  30. #include "optee_rpc_cmd.h"
  31. #include <linux/kmemleak.h>
  32. #define CREATE_TRACE_POINTS
  33. #include "optee_trace.h"
  34. /*
  35. * This file implement the SMC ABI used when communicating with secure world
  36. * OP-TEE OS via raw SMCs.
  37. * This file is divided into the following sections:
  38. * 1. Convert between struct tee_param and struct optee_msg_param
  39. * 2. Low level support functions to register shared memory in secure world
  40. * 3. Dynamic shared memory pool based on alloc_pages()
  41. * 4. Do a normal scheduled call into secure world
  42. * 5. Asynchronous notification
  43. * 6. Driver initialization.
  44. */
  45. /*
  46. * A typical OP-TEE private shm allocation is 224 bytes (argument struct
  47. * with 6 parameters, needed for open session). So with an alignment of 512
  48. * we'll waste a bit more than 50%. However, it's only expected that we'll
  49. * have a handful of these structs allocated at a time. Most memory will
  50. * be allocated aligned to the page size, So all in all this should scale
  51. * up and down quite well.
  52. */
  53. #define OPTEE_MIN_STATIC_POOL_ALIGN 9 /* 512 bytes aligned */
  54. /* SMC ABI considers at most a single TEE firmware */
  55. static unsigned int pcpu_irq_num;
  56. static int optee_cpuhp_enable_pcpu_irq(unsigned int cpu)
  57. {
  58. enable_percpu_irq(pcpu_irq_num, IRQ_TYPE_NONE);
  59. return 0;
  60. }
  61. static int optee_cpuhp_disable_pcpu_irq(unsigned int cpu)
  62. {
  63. disable_percpu_irq(pcpu_irq_num);
  64. return 0;
  65. }
  66. /*
  67. * 1. Convert between struct tee_param and struct optee_msg_param
  68. *
  69. * optee_from_msg_param() and optee_to_msg_param() are the main
  70. * functions.
  71. */
  72. static int from_msg_param_tmp_mem(struct tee_param *p, u32 attr,
  73. const struct optee_msg_param *mp)
  74. {
  75. struct tee_shm *shm;
  76. phys_addr_t pa;
  77. int rc;
  78. p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT +
  79. attr - OPTEE_MSG_ATTR_TYPE_TMEM_INPUT;
  80. p->u.memref.size = mp->u.tmem.size;
  81. shm = (struct tee_shm *)(unsigned long)mp->u.tmem.shm_ref;
  82. if (!shm) {
  83. p->u.memref.shm_offs = 0;
  84. p->u.memref.shm = NULL;
  85. return 0;
  86. }
  87. rc = tee_shm_get_pa(shm, 0, &pa);
  88. if (rc)
  89. return rc;
  90. p->u.memref.shm_offs = mp->u.tmem.buf_ptr - pa;
  91. p->u.memref.shm = shm;
  92. return 0;
  93. }
  94. static void from_msg_param_reg_mem(struct tee_param *p, u32 attr,
  95. const struct optee_msg_param *mp)
  96. {
  97. struct tee_shm *shm;
  98. p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT +
  99. attr - OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
  100. p->u.memref.size = mp->u.rmem.size;
  101. shm = (struct tee_shm *)(unsigned long)mp->u.rmem.shm_ref;
  102. if (shm) {
  103. p->u.memref.shm_offs = mp->u.rmem.offs;
  104. p->u.memref.shm = shm;
  105. } else {
  106. p->u.memref.shm_offs = 0;
  107. p->u.memref.shm = NULL;
  108. }
  109. }
  110. /**
  111. * optee_from_msg_param() - convert from OPTEE_MSG parameters to
  112. * struct tee_param
  113. * @optee: main service struct
  114. * @params: subsystem internal parameter representation
  115. * @num_params: number of elements in the parameter arrays
  116. * @msg_params: OPTEE_MSG parameters
  117. * Returns 0 on success or <0 on failure
  118. */
  119. static int optee_from_msg_param(struct optee *optee, struct tee_param *params,
  120. size_t num_params,
  121. const struct optee_msg_param *msg_params)
  122. {
  123. int rc;
  124. size_t n;
  125. for (n = 0; n < num_params; n++) {
  126. struct tee_param *p = params + n;
  127. const struct optee_msg_param *mp = msg_params + n;
  128. u32 attr = mp->attr & OPTEE_MSG_ATTR_TYPE_MASK;
  129. switch (attr) {
  130. case OPTEE_MSG_ATTR_TYPE_NONE:
  131. p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
  132. memset(&p->u, 0, sizeof(p->u));
  133. break;
  134. case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT:
  135. case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT:
  136. case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT:
  137. optee_from_msg_param_value(p, attr, mp);
  138. break;
  139. case OPTEE_MSG_ATTR_TYPE_TMEM_INPUT:
  140. case OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT:
  141. case OPTEE_MSG_ATTR_TYPE_TMEM_INOUT:
  142. rc = from_msg_param_tmp_mem(p, attr, mp);
  143. if (rc)
  144. return rc;
  145. break;
  146. case OPTEE_MSG_ATTR_TYPE_RMEM_INPUT:
  147. case OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT:
  148. case OPTEE_MSG_ATTR_TYPE_RMEM_INOUT:
  149. from_msg_param_reg_mem(p, attr, mp);
  150. break;
  151. default:
  152. return -EINVAL;
  153. }
  154. }
  155. return 0;
  156. }
  157. static int to_msg_param_tmp_mem(struct optee_msg_param *mp,
  158. const struct tee_param *p)
  159. {
  160. int rc;
  161. phys_addr_t pa;
  162. mp->attr = OPTEE_MSG_ATTR_TYPE_TMEM_INPUT + p->attr -
  163. TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
  164. mp->u.tmem.shm_ref = (unsigned long)p->u.memref.shm;
  165. mp->u.tmem.size = p->u.memref.size;
  166. if (!p->u.memref.shm) {
  167. mp->u.tmem.buf_ptr = 0;
  168. return 0;
  169. }
  170. rc = tee_shm_get_pa(p->u.memref.shm, p->u.memref.shm_offs, &pa);
  171. if (rc)
  172. return rc;
  173. mp->u.tmem.buf_ptr = pa;
  174. mp->attr |= OPTEE_MSG_ATTR_CACHE_PREDEFINED <<
  175. OPTEE_MSG_ATTR_CACHE_SHIFT;
  176. return 0;
  177. }
  178. static int to_msg_param_reg_mem(struct optee_msg_param *mp,
  179. const struct tee_param *p)
  180. {
  181. mp->attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT + p->attr -
  182. TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
  183. mp->u.rmem.shm_ref = (unsigned long)p->u.memref.shm;
  184. mp->u.rmem.size = p->u.memref.size;
  185. mp->u.rmem.offs = p->u.memref.shm_offs;
  186. return 0;
  187. }
  188. /**
  189. * optee_to_msg_param() - convert from struct tee_params to OPTEE_MSG parameters
  190. * @optee: main service struct
  191. * @msg_params: OPTEE_MSG parameters
  192. * @num_params: number of elements in the parameter arrays
  193. * @params: subsystem itnernal parameter representation
  194. * Returns 0 on success or <0 on failure
  195. */
  196. static int optee_to_msg_param(struct optee *optee,
  197. struct optee_msg_param *msg_params,
  198. size_t num_params, const struct tee_param *params)
  199. {
  200. int rc;
  201. size_t n;
  202. for (n = 0; n < num_params; n++) {
  203. const struct tee_param *p = params + n;
  204. struct optee_msg_param *mp = msg_params + n;
  205. switch (p->attr) {
  206. case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
  207. mp->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
  208. memset(&mp->u, 0, sizeof(mp->u));
  209. break;
  210. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
  211. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
  212. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
  213. optee_to_msg_param_value(mp, p);
  214. break;
  215. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
  216. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
  217. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
  218. if (tee_shm_is_dynamic(p->u.memref.shm))
  219. rc = to_msg_param_reg_mem(mp, p);
  220. else
  221. rc = to_msg_param_tmp_mem(mp, p);
  222. if (rc)
  223. return rc;
  224. break;
  225. default:
  226. return -EINVAL;
  227. }
  228. }
  229. return 0;
  230. }
  231. /*
  232. * 2. Low level support functions to register shared memory in secure world
  233. *
  234. * Functions to enable/disable shared memory caching in secure world, that
  235. * is, lazy freeing of previously allocated shared memory. Freeing is
  236. * performed when a request has been compled.
  237. *
  238. * Functions to register and unregister shared memory both for normal
  239. * clients and for tee-supplicant.
  240. */
  241. /**
  242. * optee_enable_shm_cache() - Enables caching of some shared memory allocation
  243. * in OP-TEE
  244. * @optee: main service struct
  245. */
  246. static void optee_enable_shm_cache(struct optee *optee)
  247. {
  248. struct optee_call_waiter w;
  249. /* We need to retry until secure world isn't busy. */
  250. optee_cq_wait_init(&optee->call_queue, &w, false);
  251. while (true) {
  252. struct arm_smccc_res res;
  253. optee->smc.invoke_fn(OPTEE_SMC_ENABLE_SHM_CACHE,
  254. 0, 0, 0, 0, 0, 0, 0, &res);
  255. if (res.a0 == OPTEE_SMC_RETURN_OK)
  256. break;
  257. optee_cq_wait_for_completion(&optee->call_queue, &w);
  258. }
  259. optee_cq_wait_final(&optee->call_queue, &w);
  260. }
  261. /**
  262. * __optee_disable_shm_cache() - Disables caching of some shared memory
  263. * allocation in OP-TEE
  264. * @optee: main service struct
  265. * @is_mapped: true if the cached shared memory addresses were mapped by this
  266. * kernel, are safe to dereference, and should be freed
  267. */
  268. static void __optee_disable_shm_cache(struct optee *optee, bool is_mapped)
  269. {
  270. struct optee_call_waiter w;
  271. /* We need to retry until secure world isn't busy. */
  272. optee_cq_wait_init(&optee->call_queue, &w, false);
  273. while (true) {
  274. union {
  275. struct arm_smccc_res smccc;
  276. struct optee_smc_disable_shm_cache_result result;
  277. } res;
  278. optee->smc.invoke_fn(OPTEE_SMC_DISABLE_SHM_CACHE,
  279. 0, 0, 0, 0, 0, 0, 0, &res.smccc);
  280. if (res.result.status == OPTEE_SMC_RETURN_ENOTAVAIL)
  281. break; /* All shm's freed */
  282. if (res.result.status == OPTEE_SMC_RETURN_OK) {
  283. struct tee_shm *shm;
  284. /*
  285. * Shared memory references that were not mapped by
  286. * this kernel must be ignored to prevent a crash.
  287. */
  288. if (!is_mapped)
  289. continue;
  290. shm = reg_pair_to_ptr(res.result.shm_upper32,
  291. res.result.shm_lower32);
  292. tee_shm_free(shm);
  293. } else {
  294. optee_cq_wait_for_completion(&optee->call_queue, &w);
  295. }
  296. }
  297. optee_cq_wait_final(&optee->call_queue, &w);
  298. }
  299. /**
  300. * optee_disable_shm_cache() - Disables caching of mapped shared memory
  301. * allocations in OP-TEE
  302. * @optee: main service struct
  303. */
  304. static void optee_disable_shm_cache(struct optee *optee)
  305. {
  306. return __optee_disable_shm_cache(optee, true);
  307. }
  308. /**
  309. * optee_disable_unmapped_shm_cache() - Disables caching of shared memory
  310. * allocations in OP-TEE which are not
  311. * currently mapped
  312. * @optee: main service struct
  313. */
  314. static void optee_disable_unmapped_shm_cache(struct optee *optee)
  315. {
  316. return __optee_disable_shm_cache(optee, false);
  317. }
  318. #define PAGELIST_ENTRIES_PER_PAGE \
  319. ((OPTEE_MSG_NONCONTIG_PAGE_SIZE / sizeof(u64)) - 1)
  320. /*
  321. * The final entry in each pagelist page is a pointer to the next
  322. * pagelist page.
  323. */
  324. static size_t get_pages_list_size(size_t num_entries)
  325. {
  326. int pages = DIV_ROUND_UP(num_entries, PAGELIST_ENTRIES_PER_PAGE);
  327. return pages * OPTEE_MSG_NONCONTIG_PAGE_SIZE;
  328. }
  329. static u64 *optee_allocate_pages_list(size_t num_entries)
  330. {
  331. return alloc_pages_exact(get_pages_list_size(num_entries), GFP_KERNEL);
  332. }
  333. static void optee_free_pages_list(void *list, size_t num_entries)
  334. {
  335. free_pages_exact(list, get_pages_list_size(num_entries));
  336. }
  337. /**
  338. * optee_fill_pages_list() - write list of user pages to given shared
  339. * buffer.
  340. *
  341. * @dst: page-aligned buffer where list of pages will be stored
  342. * @pages: array of pages that represents shared buffer
  343. * @num_pages: number of entries in @pages
  344. * @page_offset: offset of user buffer from page start
  345. *
  346. * @dst should be big enough to hold list of user page addresses and
  347. * links to the next pages of buffer
  348. */
  349. static void optee_fill_pages_list(u64 *dst, struct page **pages, int num_pages,
  350. size_t page_offset)
  351. {
  352. int n = 0;
  353. phys_addr_t optee_page;
  354. /*
  355. * Refer to OPTEE_MSG_ATTR_NONCONTIG description in optee_msg.h
  356. * for details.
  357. */
  358. struct {
  359. u64 pages_list[PAGELIST_ENTRIES_PER_PAGE];
  360. u64 next_page_data;
  361. } *pages_data;
  362. /*
  363. * Currently OP-TEE uses 4k page size and it does not looks
  364. * like this will change in the future. On other hand, there are
  365. * no know ARM architectures with page size < 4k.
  366. * Thus the next built assert looks redundant. But the following
  367. * code heavily relies on this assumption, so it is better be
  368. * safe than sorry.
  369. */
  370. BUILD_BUG_ON(PAGE_SIZE < OPTEE_MSG_NONCONTIG_PAGE_SIZE);
  371. pages_data = (void *)dst;
  372. /*
  373. * If linux page is bigger than 4k, and user buffer offset is
  374. * larger than 4k/8k/12k/etc this will skip first 4k pages,
  375. * because they bear no value data for OP-TEE.
  376. */
  377. optee_page = page_to_phys(*pages) +
  378. round_down(page_offset, OPTEE_MSG_NONCONTIG_PAGE_SIZE);
  379. while (true) {
  380. pages_data->pages_list[n++] = optee_page;
  381. if (n == PAGELIST_ENTRIES_PER_PAGE) {
  382. pages_data->next_page_data =
  383. virt_to_phys(pages_data + 1);
  384. pages_data++;
  385. n = 0;
  386. }
  387. optee_page += OPTEE_MSG_NONCONTIG_PAGE_SIZE;
  388. if (!(optee_page & ~PAGE_MASK)) {
  389. if (!--num_pages)
  390. break;
  391. pages++;
  392. optee_page = page_to_phys(*pages);
  393. }
  394. }
  395. }
  396. static int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm,
  397. struct page **pages, size_t num_pages,
  398. unsigned long start)
  399. {
  400. struct optee *optee = tee_get_drvdata(ctx->teedev);
  401. struct optee_msg_arg *msg_arg;
  402. struct tee_shm *shm_arg;
  403. u64 *pages_list;
  404. size_t sz;
  405. int rc;
  406. if (!num_pages)
  407. return -EINVAL;
  408. rc = optee_check_mem_type(start, num_pages);
  409. if (rc)
  410. return rc;
  411. pages_list = optee_allocate_pages_list(num_pages);
  412. if (!pages_list)
  413. return -ENOMEM;
  414. /*
  415. * We're about to register shared memory we can't register shared
  416. * memory for this request or there's a catch-22.
  417. *
  418. * So in this we'll have to do the good old temporary private
  419. * allocation instead of using optee_get_msg_arg().
  420. */
  421. sz = optee_msg_arg_size(optee->rpc_param_count);
  422. shm_arg = tee_shm_alloc_priv_buf(ctx, sz);
  423. if (IS_ERR(shm_arg)) {
  424. rc = PTR_ERR(shm_arg);
  425. goto out;
  426. }
  427. msg_arg = tee_shm_get_va(shm_arg, 0);
  428. if (IS_ERR(msg_arg)) {
  429. rc = PTR_ERR(msg_arg);
  430. goto out;
  431. }
  432. optee_fill_pages_list(pages_list, pages, num_pages,
  433. tee_shm_get_page_offset(shm));
  434. memset(msg_arg, 0, OPTEE_MSG_GET_ARG_SIZE(1));
  435. msg_arg->num_params = 1;
  436. msg_arg->cmd = OPTEE_MSG_CMD_REGISTER_SHM;
  437. msg_arg->params->attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
  438. OPTEE_MSG_ATTR_NONCONTIG;
  439. msg_arg->params->u.tmem.shm_ref = (unsigned long)shm;
  440. msg_arg->params->u.tmem.size = tee_shm_get_size(shm);
  441. /*
  442. * In the least bits of msg_arg->params->u.tmem.buf_ptr we
  443. * store buffer offset from 4k page, as described in OP-TEE ABI.
  444. */
  445. msg_arg->params->u.tmem.buf_ptr = virt_to_phys(pages_list) |
  446. (tee_shm_get_page_offset(shm) & (OPTEE_MSG_NONCONTIG_PAGE_SIZE - 1));
  447. if (optee->ops->do_call_with_arg(ctx, shm_arg, 0, false) ||
  448. msg_arg->ret != TEEC_SUCCESS)
  449. rc = -EINVAL;
  450. tee_shm_free(shm_arg);
  451. out:
  452. optee_free_pages_list(pages_list, num_pages);
  453. return rc;
  454. }
  455. static int optee_shm_unregister(struct tee_context *ctx, struct tee_shm *shm)
  456. {
  457. struct optee *optee = tee_get_drvdata(ctx->teedev);
  458. struct optee_msg_arg *msg_arg;
  459. struct tee_shm *shm_arg;
  460. int rc = 0;
  461. size_t sz;
  462. /*
  463. * We're about to unregister shared memory and we may not be able
  464. * register shared memory for this request in case we're called
  465. * from optee_shm_arg_cache_uninit().
  466. *
  467. * So in order to keep things simple in this function just as in
  468. * optee_shm_register() we'll use temporary private allocation
  469. * instead of using optee_get_msg_arg().
  470. */
  471. sz = optee_msg_arg_size(optee->rpc_param_count);
  472. shm_arg = tee_shm_alloc_priv_buf(ctx, sz);
  473. if (IS_ERR(shm_arg))
  474. return PTR_ERR(shm_arg);
  475. msg_arg = tee_shm_get_va(shm_arg, 0);
  476. if (IS_ERR(msg_arg)) {
  477. rc = PTR_ERR(msg_arg);
  478. goto out;
  479. }
  480. memset(msg_arg, 0, sz);
  481. msg_arg->num_params = 1;
  482. msg_arg->cmd = OPTEE_MSG_CMD_UNREGISTER_SHM;
  483. msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
  484. msg_arg->params[0].u.rmem.shm_ref = (unsigned long)shm;
  485. if (optee->ops->do_call_with_arg(ctx, shm_arg, 0, false) ||
  486. msg_arg->ret != TEEC_SUCCESS)
  487. rc = -EINVAL;
  488. out:
  489. tee_shm_free(shm_arg);
  490. return rc;
  491. }
  492. static int optee_shm_register_supp(struct tee_context *ctx, struct tee_shm *shm,
  493. struct page **pages, size_t num_pages,
  494. unsigned long start)
  495. {
  496. /*
  497. * We don't want to register supplicant memory in OP-TEE.
  498. * Instead information about it will be passed in RPC code.
  499. */
  500. return optee_check_mem_type(start, num_pages);
  501. }
  502. static int optee_shm_unregister_supp(struct tee_context *ctx,
  503. struct tee_shm *shm)
  504. {
  505. return 0;
  506. }
  507. /*
  508. * 3. Dynamic shared memory pool based on alloc_pages()
  509. *
  510. * Implements an OP-TEE specific shared memory pool which is used
  511. * when dynamic shared memory is supported by secure world.
  512. *
  513. * The main function is optee_shm_pool_alloc_pages().
  514. */
  515. static int pool_op_alloc(struct tee_shm_pool *pool,
  516. struct tee_shm *shm, size_t size, size_t align)
  517. {
  518. /*
  519. * Shared memory private to the OP-TEE driver doesn't need
  520. * to be registered with OP-TEE.
  521. */
  522. if (shm->flags & TEE_SHM_PRIV)
  523. return tee_dyn_shm_alloc_helper(shm, size, align, NULL);
  524. return tee_dyn_shm_alloc_helper(shm, size, align, optee_shm_register);
  525. }
  526. static void pool_op_free(struct tee_shm_pool *pool,
  527. struct tee_shm *shm)
  528. {
  529. if (!(shm->flags & TEE_SHM_PRIV))
  530. tee_dyn_shm_free_helper(shm, optee_shm_unregister);
  531. else
  532. tee_dyn_shm_free_helper(shm, NULL);
  533. }
  534. static void pool_op_destroy_pool(struct tee_shm_pool *pool)
  535. {
  536. kfree(pool);
  537. }
  538. static const struct tee_shm_pool_ops pool_ops = {
  539. .alloc = pool_op_alloc,
  540. .free = pool_op_free,
  541. .destroy_pool = pool_op_destroy_pool,
  542. };
  543. /**
  544. * optee_shm_pool_alloc_pages() - create page-based allocator pool
  545. *
  546. * This pool is used when OP-TEE supports dymanic SHM. In this case
  547. * command buffers and such are allocated from kernel's own memory.
  548. */
  549. static struct tee_shm_pool *optee_shm_pool_alloc_pages(void)
  550. {
  551. struct tee_shm_pool *pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  552. if (!pool)
  553. return ERR_PTR(-ENOMEM);
  554. pool->ops = &pool_ops;
  555. return pool;
  556. }
  557. /*
  558. * 4. Do a normal scheduled call into secure world
  559. *
  560. * The function optee_smc_do_call_with_arg() performs a normal scheduled
  561. * call into secure world. During this call may normal world request help
  562. * from normal world using RPCs, Remote Procedure Calls. This includes
  563. * delivery of non-secure interrupts to for instance allow rescheduling of
  564. * the current task.
  565. */
  566. static void handle_rpc_func_cmd_shm_free(struct tee_context *ctx,
  567. struct optee_msg_arg *arg)
  568. {
  569. struct tee_shm *shm;
  570. arg->ret_origin = TEEC_ORIGIN_COMMS;
  571. if (arg->num_params != 1 ||
  572. arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
  573. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  574. return;
  575. }
  576. shm = (struct tee_shm *)(unsigned long)arg->params[0].u.value.b;
  577. switch (arg->params[0].u.value.a) {
  578. case OPTEE_RPC_SHM_TYPE_APPL:
  579. optee_rpc_cmd_free_suppl(ctx, shm);
  580. break;
  581. case OPTEE_RPC_SHM_TYPE_KERNEL:
  582. tee_shm_free(shm);
  583. break;
  584. default:
  585. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  586. }
  587. arg->ret = TEEC_SUCCESS;
  588. }
  589. static void handle_rpc_func_cmd_shm_alloc(struct tee_context *ctx,
  590. struct optee *optee,
  591. struct optee_msg_arg *arg,
  592. struct optee_call_ctx *call_ctx)
  593. {
  594. struct tee_shm *shm;
  595. size_t sz;
  596. size_t n;
  597. struct page **pages;
  598. size_t page_count;
  599. arg->ret_origin = TEEC_ORIGIN_COMMS;
  600. if (!arg->num_params ||
  601. arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
  602. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  603. return;
  604. }
  605. for (n = 1; n < arg->num_params; n++) {
  606. if (arg->params[n].attr != OPTEE_MSG_ATTR_TYPE_NONE) {
  607. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  608. return;
  609. }
  610. }
  611. sz = arg->params[0].u.value.b;
  612. switch (arg->params[0].u.value.a) {
  613. case OPTEE_RPC_SHM_TYPE_APPL:
  614. shm = optee_rpc_cmd_alloc_suppl(ctx, sz);
  615. break;
  616. case OPTEE_RPC_SHM_TYPE_KERNEL:
  617. shm = tee_shm_alloc_priv_buf(optee->ctx, sz);
  618. break;
  619. default:
  620. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  621. return;
  622. }
  623. if (IS_ERR(shm)) {
  624. arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
  625. return;
  626. }
  627. /*
  628. * If there are pages it's dynamically allocated shared memory (not
  629. * from the reserved shared memory pool) and needs to be
  630. * registered.
  631. */
  632. pages = tee_shm_get_pages(shm, &page_count);
  633. if (pages) {
  634. u64 *pages_list;
  635. pages_list = optee_allocate_pages_list(page_count);
  636. if (!pages_list) {
  637. arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
  638. goto bad;
  639. }
  640. call_ctx->pages_list = pages_list;
  641. call_ctx->num_entries = page_count;
  642. arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
  643. OPTEE_MSG_ATTR_NONCONTIG;
  644. /*
  645. * In the least bits of u.tmem.buf_ptr we store buffer offset
  646. * from 4k page, as described in OP-TEE ABI.
  647. */
  648. arg->params[0].u.tmem.buf_ptr = virt_to_phys(pages_list) |
  649. (tee_shm_get_page_offset(shm) &
  650. (OPTEE_MSG_NONCONTIG_PAGE_SIZE - 1));
  651. optee_fill_pages_list(pages_list, pages, page_count,
  652. tee_shm_get_page_offset(shm));
  653. } else {
  654. phys_addr_t pa;
  655. if (tee_shm_get_pa(shm, 0, &pa)) {
  656. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  657. goto bad;
  658. }
  659. arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT;
  660. arg->params[0].u.tmem.buf_ptr = pa;
  661. }
  662. arg->params[0].u.tmem.size = tee_shm_get_size(shm);
  663. arg->params[0].u.tmem.shm_ref = (unsigned long)shm;
  664. arg->ret = TEEC_SUCCESS;
  665. return;
  666. bad:
  667. tee_shm_free(shm);
  668. }
  669. static void free_pages_list(struct optee_call_ctx *call_ctx)
  670. {
  671. if (call_ctx->pages_list) {
  672. optee_free_pages_list(call_ctx->pages_list,
  673. call_ctx->num_entries);
  674. call_ctx->pages_list = NULL;
  675. call_ctx->num_entries = 0;
  676. }
  677. }
  678. static void optee_rpc_finalize_call(struct optee_call_ctx *call_ctx)
  679. {
  680. free_pages_list(call_ctx);
  681. }
  682. static void handle_rpc_func_cmd(struct tee_context *ctx, struct optee *optee,
  683. struct optee_msg_arg *arg,
  684. struct optee_call_ctx *call_ctx)
  685. {
  686. switch (arg->cmd) {
  687. case OPTEE_RPC_CMD_SHM_ALLOC:
  688. free_pages_list(call_ctx);
  689. handle_rpc_func_cmd_shm_alloc(ctx, optee, arg, call_ctx);
  690. break;
  691. case OPTEE_RPC_CMD_SHM_FREE:
  692. handle_rpc_func_cmd_shm_free(ctx, arg);
  693. break;
  694. default:
  695. optee_rpc_cmd(ctx, optee, arg);
  696. }
  697. }
  698. /**
  699. * optee_handle_rpc() - handle RPC from secure world
  700. * @ctx: context doing the RPC
  701. * @rpc_arg: pointer to RPC arguments if any, or NULL if none
  702. * @param: value of registers for the RPC
  703. * @call_ctx: call context. Preserved during one OP-TEE invocation
  704. *
  705. * Result of RPC is written back into @param.
  706. */
  707. static void optee_handle_rpc(struct tee_context *ctx,
  708. struct optee_msg_arg *rpc_arg,
  709. struct optee_rpc_param *param,
  710. struct optee_call_ctx *call_ctx)
  711. {
  712. struct tee_device *teedev = ctx->teedev;
  713. struct optee *optee = tee_get_drvdata(teedev);
  714. struct optee_msg_arg *arg;
  715. struct tee_shm *shm;
  716. phys_addr_t pa;
  717. switch (OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0)) {
  718. case OPTEE_SMC_RPC_FUNC_ALLOC:
  719. shm = tee_shm_alloc_priv_buf(optee->ctx, param->a1);
  720. if (!IS_ERR(shm) && !tee_shm_get_pa(shm, 0, &pa)) {
  721. reg_pair_from_64(&param->a1, &param->a2, pa);
  722. reg_pair_from_64(&param->a4, &param->a5,
  723. (unsigned long)shm);
  724. } else {
  725. param->a1 = 0;
  726. param->a2 = 0;
  727. param->a4 = 0;
  728. param->a5 = 0;
  729. }
  730. kmemleak_not_leak(shm);
  731. break;
  732. case OPTEE_SMC_RPC_FUNC_FREE:
  733. shm = reg_pair_to_ptr(param->a1, param->a2);
  734. tee_shm_free(shm);
  735. break;
  736. case OPTEE_SMC_RPC_FUNC_FOREIGN_INTR:
  737. /*
  738. * A foreign interrupt was raised while secure world was
  739. * executing, since they are handled in Linux a dummy RPC is
  740. * performed to let Linux take the interrupt through the normal
  741. * vector.
  742. */
  743. break;
  744. case OPTEE_SMC_RPC_FUNC_CMD:
  745. if (rpc_arg) {
  746. arg = rpc_arg;
  747. } else {
  748. shm = reg_pair_to_ptr(param->a1, param->a2);
  749. arg = tee_shm_get_va(shm, 0);
  750. if (IS_ERR(arg)) {
  751. pr_err("%s: tee_shm_get_va %p failed\n",
  752. __func__, shm);
  753. break;
  754. }
  755. }
  756. handle_rpc_func_cmd(ctx, optee, arg, call_ctx);
  757. break;
  758. default:
  759. pr_warn("Unknown RPC func 0x%x\n",
  760. (u32)OPTEE_SMC_RETURN_GET_RPC_FUNC(param->a0));
  761. break;
  762. }
  763. param->a0 = OPTEE_SMC_CALL_RETURN_FROM_RPC;
  764. }
  765. /**
  766. * optee_smc_do_call_with_arg() - Do an SMC to OP-TEE in secure world
  767. * @ctx: calling context
  768. * @shm: shared memory holding the message to pass to secure world
  769. * @offs: offset of the message in @shm
  770. * @system_thread: true if caller requests TEE system thread support
  771. *
  772. * Does and SMC to OP-TEE in secure world and handles eventual resulting
  773. * Remote Procedure Calls (RPC) from OP-TEE.
  774. *
  775. * Returns return code from secure world, 0 is OK
  776. */
  777. static int optee_smc_do_call_with_arg(struct tee_context *ctx,
  778. struct tee_shm *shm, u_int offs,
  779. bool system_thread)
  780. {
  781. struct optee *optee = tee_get_drvdata(ctx->teedev);
  782. struct optee_call_waiter w;
  783. struct optee_rpc_param param = { };
  784. struct optee_call_ctx call_ctx = { };
  785. struct optee_msg_arg *rpc_arg = NULL;
  786. int rc;
  787. if (optee->rpc_param_count) {
  788. struct optee_msg_arg *arg;
  789. unsigned int rpc_arg_offs;
  790. arg = tee_shm_get_va(shm, offs);
  791. if (IS_ERR(arg))
  792. return PTR_ERR(arg);
  793. rpc_arg_offs = OPTEE_MSG_GET_ARG_SIZE(arg->num_params);
  794. rpc_arg = tee_shm_get_va(shm, offs + rpc_arg_offs);
  795. if (IS_ERR(rpc_arg))
  796. return PTR_ERR(rpc_arg);
  797. }
  798. if (rpc_arg && tee_shm_is_dynamic(shm)) {
  799. param.a0 = OPTEE_SMC_CALL_WITH_REGD_ARG;
  800. reg_pair_from_64(&param.a1, &param.a2, (u_long)shm);
  801. param.a3 = offs;
  802. } else {
  803. phys_addr_t parg;
  804. rc = tee_shm_get_pa(shm, offs, &parg);
  805. if (rc)
  806. return rc;
  807. if (rpc_arg)
  808. param.a0 = OPTEE_SMC_CALL_WITH_RPC_ARG;
  809. else
  810. param.a0 = OPTEE_SMC_CALL_WITH_ARG;
  811. reg_pair_from_64(&param.a1, &param.a2, parg);
  812. }
  813. /* Initialize waiter */
  814. optee_cq_wait_init(&optee->call_queue, &w, system_thread);
  815. while (true) {
  816. struct arm_smccc_res res;
  817. trace_optee_invoke_fn_begin(&param);
  818. optee->smc.invoke_fn(param.a0, param.a1, param.a2, param.a3,
  819. param.a4, param.a5, param.a6, param.a7,
  820. &res);
  821. trace_optee_invoke_fn_end(&param, &res);
  822. if (res.a0 == OPTEE_SMC_RETURN_ETHREAD_LIMIT) {
  823. /*
  824. * Out of threads in secure world, wait for a thread
  825. * become available.
  826. */
  827. optee_cq_wait_for_completion(&optee->call_queue, &w);
  828. } else if (OPTEE_SMC_RETURN_IS_RPC(res.a0)) {
  829. cond_resched();
  830. param.a0 = res.a0;
  831. param.a1 = res.a1;
  832. param.a2 = res.a2;
  833. param.a3 = res.a3;
  834. optee_handle_rpc(ctx, rpc_arg, &param, &call_ctx);
  835. } else {
  836. rc = res.a0;
  837. break;
  838. }
  839. }
  840. optee_rpc_finalize_call(&call_ctx);
  841. /*
  842. * We're done with our thread in secure world, if there's any
  843. * thread waiters wake up one.
  844. */
  845. optee_cq_wait_final(&optee->call_queue, &w);
  846. return rc;
  847. }
  848. /*
  849. * 5. Asynchronous notification
  850. */
  851. static u32 get_async_notif_value(optee_invoke_fn *invoke_fn, bool *value_valid,
  852. bool *value_pending)
  853. {
  854. struct arm_smccc_res res;
  855. invoke_fn(OPTEE_SMC_GET_ASYNC_NOTIF_VALUE, 0, 0, 0, 0, 0, 0, 0, &res);
  856. if (res.a0) {
  857. *value_valid = false;
  858. return 0;
  859. }
  860. *value_valid = (res.a2 & OPTEE_SMC_ASYNC_NOTIF_VALUE_VALID);
  861. *value_pending = (res.a2 & OPTEE_SMC_ASYNC_NOTIF_VALUE_PENDING);
  862. return res.a1;
  863. }
  864. static irqreturn_t irq_handler(struct optee *optee)
  865. {
  866. bool do_bottom_half = false;
  867. bool value_valid;
  868. bool value_pending;
  869. u32 value;
  870. do {
  871. value = get_async_notif_value(optee->smc.invoke_fn,
  872. &value_valid, &value_pending);
  873. if (!value_valid)
  874. break;
  875. if (value == OPTEE_SMC_ASYNC_NOTIF_VALUE_DO_BOTTOM_HALF)
  876. do_bottom_half = true;
  877. else
  878. optee_notif_send(optee, value);
  879. } while (value_pending);
  880. if (do_bottom_half)
  881. return IRQ_WAKE_THREAD;
  882. return IRQ_HANDLED;
  883. }
  884. static irqreturn_t notif_irq_handler(int irq, void *dev_id)
  885. {
  886. struct optee *optee = dev_id;
  887. return irq_handler(optee);
  888. }
  889. static irqreturn_t notif_irq_thread_fn(int irq, void *dev_id)
  890. {
  891. struct optee *optee = dev_id;
  892. optee_do_bottom_half(optee->ctx);
  893. return IRQ_HANDLED;
  894. }
  895. static int init_irq(struct optee *optee, u_int irq)
  896. {
  897. int rc;
  898. rc = request_threaded_irq(irq, notif_irq_handler,
  899. notif_irq_thread_fn,
  900. 0, "optee_notification", optee);
  901. if (rc)
  902. return rc;
  903. optee->smc.notif_irq = irq;
  904. return 0;
  905. }
  906. static irqreturn_t notif_pcpu_irq_handler(int irq, void *dev_id)
  907. {
  908. struct optee_pcpu *pcpu = dev_id;
  909. struct optee *optee = pcpu->optee;
  910. if (irq_handler(optee) == IRQ_WAKE_THREAD)
  911. queue_work(optee->smc.notif_pcpu_wq,
  912. &optee->smc.notif_pcpu_work);
  913. return IRQ_HANDLED;
  914. }
  915. static void notif_pcpu_irq_work_fn(struct work_struct *work)
  916. {
  917. struct optee_smc *optee_smc = container_of(work, struct optee_smc,
  918. notif_pcpu_work);
  919. struct optee *optee = container_of(optee_smc, struct optee, smc);
  920. optee_do_bottom_half(optee->ctx);
  921. }
  922. static int init_pcpu_irq(struct optee *optee, u_int irq)
  923. {
  924. struct optee_pcpu __percpu *optee_pcpu;
  925. int cpu, rc;
  926. optee_pcpu = alloc_percpu(struct optee_pcpu);
  927. if (!optee_pcpu)
  928. return -ENOMEM;
  929. for_each_present_cpu(cpu)
  930. per_cpu_ptr(optee_pcpu, cpu)->optee = optee;
  931. rc = request_percpu_irq(irq, notif_pcpu_irq_handler,
  932. "optee_pcpu_notification", optee_pcpu);
  933. if (rc)
  934. goto err_free_pcpu;
  935. INIT_WORK(&optee->smc.notif_pcpu_work, notif_pcpu_irq_work_fn);
  936. optee->smc.notif_pcpu_wq = create_workqueue("optee_pcpu_notification");
  937. if (!optee->smc.notif_pcpu_wq) {
  938. rc = -EINVAL;
  939. goto err_free_pcpu_irq;
  940. }
  941. optee->smc.optee_pcpu = optee_pcpu;
  942. optee->smc.notif_irq = irq;
  943. pcpu_irq_num = irq;
  944. rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "optee/pcpu-notif:starting",
  945. optee_cpuhp_enable_pcpu_irq,
  946. optee_cpuhp_disable_pcpu_irq);
  947. if (!rc)
  948. rc = -EINVAL;
  949. if (rc < 0)
  950. goto err_free_pcpu_irq;
  951. optee->smc.notif_cpuhp_state = rc;
  952. return 0;
  953. err_free_pcpu_irq:
  954. free_percpu_irq(irq, optee_pcpu);
  955. err_free_pcpu:
  956. free_percpu(optee_pcpu);
  957. return rc;
  958. }
  959. static int optee_smc_notif_init_irq(struct optee *optee, u_int irq)
  960. {
  961. if (irq_is_percpu_devid(irq))
  962. return init_pcpu_irq(optee, irq);
  963. else
  964. return init_irq(optee, irq);
  965. }
  966. static void uninit_pcpu_irq(struct optee *optee)
  967. {
  968. cpuhp_remove_state(optee->smc.notif_cpuhp_state);
  969. destroy_workqueue(optee->smc.notif_pcpu_wq);
  970. free_percpu_irq(optee->smc.notif_irq, optee->smc.optee_pcpu);
  971. free_percpu(optee->smc.optee_pcpu);
  972. }
  973. static void optee_smc_notif_uninit_irq(struct optee *optee)
  974. {
  975. if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_ASYNC_NOTIF) {
  976. optee_stop_async_notif(optee->ctx);
  977. if (optee->smc.notif_irq) {
  978. if (irq_is_percpu_devid(optee->smc.notif_irq))
  979. uninit_pcpu_irq(optee);
  980. else
  981. free_irq(optee->smc.notif_irq, optee);
  982. irq_dispose_mapping(optee->smc.notif_irq);
  983. }
  984. }
  985. }
  986. /*
  987. * 6. Driver initialization
  988. *
  989. * During driver initialization is secure world probed to find out which
  990. * features it supports so the driver can be initialized with a matching
  991. * configuration. This involves for instance support for dynamic shared
  992. * memory instead of a static memory carvout.
  993. */
  994. static void optee_get_version(struct tee_device *teedev,
  995. struct tee_ioctl_version_data *vers)
  996. {
  997. struct tee_ioctl_version_data v = {
  998. .impl_id = TEE_IMPL_ID_OPTEE,
  999. .impl_caps = TEE_OPTEE_CAP_TZ,
  1000. .gen_caps = TEE_GEN_CAP_GP,
  1001. };
  1002. struct optee *optee = tee_get_drvdata(teedev);
  1003. if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
  1004. v.gen_caps |= TEE_GEN_CAP_REG_MEM;
  1005. if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_MEMREF_NULL)
  1006. v.gen_caps |= TEE_GEN_CAP_MEMREF_NULL;
  1007. *vers = v;
  1008. }
  1009. static int optee_smc_open(struct tee_context *ctx)
  1010. {
  1011. struct optee *optee = tee_get_drvdata(ctx->teedev);
  1012. u32 sec_caps = optee->smc.sec_caps;
  1013. return optee_open(ctx, sec_caps & OPTEE_SMC_SEC_CAP_MEMREF_NULL);
  1014. }
  1015. static const struct tee_driver_ops optee_clnt_ops = {
  1016. .get_version = optee_get_version,
  1017. .open = optee_smc_open,
  1018. .release = optee_release,
  1019. .open_session = optee_open_session,
  1020. .close_session = optee_close_session,
  1021. .system_session = optee_system_session,
  1022. .invoke_func = optee_invoke_func,
  1023. .cancel_req = optee_cancel_req,
  1024. .shm_register = optee_shm_register,
  1025. .shm_unregister = optee_shm_unregister,
  1026. };
  1027. static const struct tee_desc optee_clnt_desc = {
  1028. .name = DRIVER_NAME "-clnt",
  1029. .ops = &optee_clnt_ops,
  1030. .owner = THIS_MODULE,
  1031. };
  1032. static const struct tee_driver_ops optee_supp_ops = {
  1033. .get_version = optee_get_version,
  1034. .open = optee_smc_open,
  1035. .release = optee_release_supp,
  1036. .supp_recv = optee_supp_recv,
  1037. .supp_send = optee_supp_send,
  1038. .shm_register = optee_shm_register_supp,
  1039. .shm_unregister = optee_shm_unregister_supp,
  1040. };
  1041. static const struct tee_desc optee_supp_desc = {
  1042. .name = DRIVER_NAME "-supp",
  1043. .ops = &optee_supp_ops,
  1044. .owner = THIS_MODULE,
  1045. .flags = TEE_DESC_PRIVILEGED,
  1046. };
  1047. static const struct optee_ops optee_ops = {
  1048. .do_call_with_arg = optee_smc_do_call_with_arg,
  1049. .to_msg_param = optee_to_msg_param,
  1050. .from_msg_param = optee_from_msg_param,
  1051. };
  1052. static int enable_async_notif(optee_invoke_fn *invoke_fn)
  1053. {
  1054. struct arm_smccc_res res;
  1055. invoke_fn(OPTEE_SMC_ENABLE_ASYNC_NOTIF, 0, 0, 0, 0, 0, 0, 0, &res);
  1056. if (res.a0)
  1057. return -EINVAL;
  1058. return 0;
  1059. }
  1060. static bool optee_msg_api_uid_is_optee_api(optee_invoke_fn *invoke_fn)
  1061. {
  1062. struct arm_smccc_res res;
  1063. invoke_fn(OPTEE_SMC_CALLS_UID, 0, 0, 0, 0, 0, 0, 0, &res);
  1064. if (res.a0 == OPTEE_MSG_UID_0 && res.a1 == OPTEE_MSG_UID_1 &&
  1065. res.a2 == OPTEE_MSG_UID_2 && res.a3 == OPTEE_MSG_UID_3)
  1066. return true;
  1067. return false;
  1068. }
  1069. #ifdef CONFIG_OPTEE_INSECURE_LOAD_IMAGE
  1070. static bool optee_msg_api_uid_is_optee_image_load(optee_invoke_fn *invoke_fn)
  1071. {
  1072. struct arm_smccc_res res;
  1073. invoke_fn(OPTEE_SMC_CALLS_UID, 0, 0, 0, 0, 0, 0, 0, &res);
  1074. if (res.a0 == OPTEE_MSG_IMAGE_LOAD_UID_0 &&
  1075. res.a1 == OPTEE_MSG_IMAGE_LOAD_UID_1 &&
  1076. res.a2 == OPTEE_MSG_IMAGE_LOAD_UID_2 &&
  1077. res.a3 == OPTEE_MSG_IMAGE_LOAD_UID_3)
  1078. return true;
  1079. return false;
  1080. }
  1081. #endif
  1082. static void optee_msg_get_os_revision(optee_invoke_fn *invoke_fn)
  1083. {
  1084. union {
  1085. struct arm_smccc_res smccc;
  1086. struct optee_smc_call_get_os_revision_result result;
  1087. } res = {
  1088. .result = {
  1089. .build_id = 0
  1090. }
  1091. };
  1092. invoke_fn(OPTEE_SMC_CALL_GET_OS_REVISION, 0, 0, 0, 0, 0, 0, 0,
  1093. &res.smccc);
  1094. if (res.result.build_id)
  1095. pr_info("revision %lu.%lu (%08lx)", res.result.major,
  1096. res.result.minor, res.result.build_id);
  1097. else
  1098. pr_info("revision %lu.%lu", res.result.major, res.result.minor);
  1099. }
  1100. static bool optee_msg_api_revision_is_compatible(optee_invoke_fn *invoke_fn)
  1101. {
  1102. union {
  1103. struct arm_smccc_res smccc;
  1104. struct optee_smc_calls_revision_result result;
  1105. } res;
  1106. invoke_fn(OPTEE_SMC_CALLS_REVISION, 0, 0, 0, 0, 0, 0, 0, &res.smccc);
  1107. if (res.result.major == OPTEE_MSG_REVISION_MAJOR &&
  1108. (int)res.result.minor >= OPTEE_MSG_REVISION_MINOR)
  1109. return true;
  1110. return false;
  1111. }
  1112. static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn,
  1113. u32 *sec_caps, u32 *max_notif_value,
  1114. unsigned int *rpc_param_count)
  1115. {
  1116. union {
  1117. struct arm_smccc_res smccc;
  1118. struct optee_smc_exchange_capabilities_result result;
  1119. } res;
  1120. u32 a1 = 0;
  1121. /*
  1122. * TODO This isn't enough to tell if it's UP system (from kernel
  1123. * point of view) or not, is_smp() returns the information
  1124. * needed, but can't be called directly from here.
  1125. */
  1126. if (!IS_ENABLED(CONFIG_SMP) || nr_cpu_ids == 1)
  1127. a1 |= OPTEE_SMC_NSEC_CAP_UNIPROCESSOR;
  1128. invoke_fn(OPTEE_SMC_EXCHANGE_CAPABILITIES, a1, 0, 0, 0, 0, 0, 0,
  1129. &res.smccc);
  1130. if (res.result.status != OPTEE_SMC_RETURN_OK)
  1131. return false;
  1132. *sec_caps = res.result.capabilities;
  1133. if (*sec_caps & OPTEE_SMC_SEC_CAP_ASYNC_NOTIF)
  1134. *max_notif_value = res.result.max_notif_value;
  1135. else
  1136. *max_notif_value = OPTEE_DEFAULT_MAX_NOTIF_VALUE;
  1137. if (*sec_caps & OPTEE_SMC_SEC_CAP_RPC_ARG)
  1138. *rpc_param_count = (u8)res.result.data;
  1139. else
  1140. *rpc_param_count = 0;
  1141. return true;
  1142. }
  1143. static unsigned int optee_msg_get_thread_count(optee_invoke_fn *invoke_fn)
  1144. {
  1145. struct arm_smccc_res res;
  1146. invoke_fn(OPTEE_SMC_GET_THREAD_COUNT, 0, 0, 0, 0, 0, 0, 0, &res);
  1147. if (res.a0)
  1148. return 0;
  1149. return res.a1;
  1150. }
  1151. static struct tee_shm_pool *
  1152. optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm)
  1153. {
  1154. union {
  1155. struct arm_smccc_res smccc;
  1156. struct optee_smc_get_shm_config_result result;
  1157. } res;
  1158. unsigned long vaddr;
  1159. phys_addr_t paddr;
  1160. size_t size;
  1161. phys_addr_t begin;
  1162. phys_addr_t end;
  1163. void *va;
  1164. void *rc;
  1165. invoke_fn(OPTEE_SMC_GET_SHM_CONFIG, 0, 0, 0, 0, 0, 0, 0, &res.smccc);
  1166. if (res.result.status != OPTEE_SMC_RETURN_OK) {
  1167. pr_err("static shm service not available\n");
  1168. return ERR_PTR(-ENOENT);
  1169. }
  1170. if (res.result.settings != OPTEE_SMC_SHM_CACHED) {
  1171. pr_err("only normal cached shared memory supported\n");
  1172. return ERR_PTR(-EINVAL);
  1173. }
  1174. begin = roundup(res.result.start, PAGE_SIZE);
  1175. end = rounddown(res.result.start + res.result.size, PAGE_SIZE);
  1176. paddr = begin;
  1177. size = end - begin;
  1178. va = memremap(paddr, size, MEMREMAP_WB);
  1179. if (!va) {
  1180. pr_err("shared memory ioremap failed\n");
  1181. return ERR_PTR(-EINVAL);
  1182. }
  1183. vaddr = (unsigned long)va;
  1184. rc = tee_shm_pool_alloc_res_mem(vaddr, paddr, size,
  1185. OPTEE_MIN_STATIC_POOL_ALIGN);
  1186. if (IS_ERR(rc))
  1187. memunmap(va);
  1188. else
  1189. *memremaped_shm = va;
  1190. return rc;
  1191. }
  1192. /* Simple wrapper functions to be able to use a function pointer */
  1193. static void optee_smccc_smc(unsigned long a0, unsigned long a1,
  1194. unsigned long a2, unsigned long a3,
  1195. unsigned long a4, unsigned long a5,
  1196. unsigned long a6, unsigned long a7,
  1197. struct arm_smccc_res *res)
  1198. {
  1199. arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res);
  1200. }
  1201. static void optee_smccc_hvc(unsigned long a0, unsigned long a1,
  1202. unsigned long a2, unsigned long a3,
  1203. unsigned long a4, unsigned long a5,
  1204. unsigned long a6, unsigned long a7,
  1205. struct arm_smccc_res *res)
  1206. {
  1207. arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
  1208. }
  1209. static optee_invoke_fn *get_invoke_func(struct device *dev)
  1210. {
  1211. const char *method;
  1212. pr_info("probing for conduit method.\n");
  1213. if (device_property_read_string(dev, "method", &method)) {
  1214. pr_warn("missing \"method\" property\n");
  1215. return ERR_PTR(-ENXIO);
  1216. }
  1217. if (!strcmp("hvc", method))
  1218. return optee_smccc_hvc;
  1219. else if (!strcmp("smc", method))
  1220. return optee_smccc_smc;
  1221. pr_warn("invalid \"method\" property: %s\n", method);
  1222. return ERR_PTR(-EINVAL);
  1223. }
  1224. /* optee_remove - Device Removal Routine
  1225. * @pdev: platform device information struct
  1226. *
  1227. * optee_remove is called by platform subsystem to alert the driver
  1228. * that it should release the device
  1229. */
  1230. static void optee_smc_remove(struct platform_device *pdev)
  1231. {
  1232. struct optee *optee = platform_get_drvdata(pdev);
  1233. /*
  1234. * Ask OP-TEE to free all cached shared memory objects to decrease
  1235. * reference counters and also avoid wild pointers in secure world
  1236. * into the old shared memory range.
  1237. */
  1238. if (!optee->rpc_param_count)
  1239. optee_disable_shm_cache(optee);
  1240. optee_smc_notif_uninit_irq(optee);
  1241. optee_remove_common(optee);
  1242. if (optee->smc.memremaped_shm)
  1243. memunmap(optee->smc.memremaped_shm);
  1244. kfree(optee);
  1245. }
  1246. /* optee_shutdown - Device Removal Routine
  1247. * @pdev: platform device information struct
  1248. *
  1249. * platform_shutdown is called by the platform subsystem to alert
  1250. * the driver that a shutdown, reboot, or kexec is happening and
  1251. * device must be disabled.
  1252. */
  1253. static void optee_shutdown(struct platform_device *pdev)
  1254. {
  1255. struct optee *optee = platform_get_drvdata(pdev);
  1256. if (!optee->rpc_param_count)
  1257. optee_disable_shm_cache(optee);
  1258. }
  1259. #ifdef CONFIG_OPTEE_INSECURE_LOAD_IMAGE
  1260. #define OPTEE_FW_IMAGE "optee/tee.bin"
  1261. static optee_invoke_fn *cpuhp_invoke_fn;
  1262. static int optee_cpuhp_probe(unsigned int cpu)
  1263. {
  1264. /*
  1265. * Invoking a call on a CPU will cause OP-TEE to perform the required
  1266. * setup for that CPU. Just invoke the call to get the UID since that
  1267. * has no side effects.
  1268. */
  1269. if (optee_msg_api_uid_is_optee_api(cpuhp_invoke_fn))
  1270. return 0;
  1271. else
  1272. return -EINVAL;
  1273. }
  1274. static int optee_load_fw(struct platform_device *pdev,
  1275. optee_invoke_fn *invoke_fn)
  1276. {
  1277. const struct firmware *fw = NULL;
  1278. struct arm_smccc_res res;
  1279. phys_addr_t data_pa;
  1280. u8 *data_buf = NULL;
  1281. u64 data_size;
  1282. u32 data_pa_high, data_pa_low;
  1283. u32 data_size_high, data_size_low;
  1284. int rc;
  1285. int hp_state;
  1286. if (!optee_msg_api_uid_is_optee_image_load(invoke_fn))
  1287. return 0;
  1288. rc = request_firmware(&fw, OPTEE_FW_IMAGE, &pdev->dev);
  1289. if (rc) {
  1290. /*
  1291. * The firmware in the rootfs will not be accessible until we
  1292. * are in the SYSTEM_RUNNING state, so return EPROBE_DEFER until
  1293. * that point.
  1294. */
  1295. if (system_state < SYSTEM_RUNNING)
  1296. return -EPROBE_DEFER;
  1297. goto fw_err;
  1298. }
  1299. data_size = fw->size;
  1300. /*
  1301. * This uses the GFP_DMA flag to ensure we are allocated memory in the
  1302. * 32-bit space since TF-A cannot map memory beyond the 32-bit boundary.
  1303. */
  1304. data_buf = kmemdup(fw->data, fw->size, GFP_KERNEL | GFP_DMA);
  1305. if (!data_buf) {
  1306. rc = -ENOMEM;
  1307. goto fw_err;
  1308. }
  1309. data_pa = virt_to_phys(data_buf);
  1310. reg_pair_from_64(&data_pa_high, &data_pa_low, data_pa);
  1311. reg_pair_from_64(&data_size_high, &data_size_low, data_size);
  1312. goto fw_load;
  1313. fw_err:
  1314. pr_warn("image loading failed\n");
  1315. data_pa_high = 0;
  1316. data_pa_low = 0;
  1317. data_size_high = 0;
  1318. data_size_low = 0;
  1319. fw_load:
  1320. /*
  1321. * Always invoke the SMC, even if loading the image fails, to indicate
  1322. * to EL3 that we have passed the point where it should allow invoking
  1323. * this SMC.
  1324. */
  1325. pr_warn("OP-TEE image loaded from kernel, this can be insecure");
  1326. invoke_fn(OPTEE_SMC_CALL_LOAD_IMAGE, data_size_high, data_size_low,
  1327. data_pa_high, data_pa_low, 0, 0, 0, &res);
  1328. if (!rc)
  1329. rc = res.a0;
  1330. if (fw)
  1331. release_firmware(fw);
  1332. kfree(data_buf);
  1333. if (!rc) {
  1334. /*
  1335. * We need to initialize OP-TEE on all other running cores as
  1336. * well. Any cores that aren't running yet will get initialized
  1337. * when they are brought up by the power management functions in
  1338. * TF-A which are registered by the OP-TEE SPD. Due to that we
  1339. * can un-register the callback right after registering it.
  1340. */
  1341. cpuhp_invoke_fn = invoke_fn;
  1342. hp_state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "optee:probe",
  1343. optee_cpuhp_probe, NULL);
  1344. if (hp_state < 0) {
  1345. pr_warn("Failed with CPU hotplug setup for OP-TEE");
  1346. return -EINVAL;
  1347. }
  1348. cpuhp_remove_state(hp_state);
  1349. cpuhp_invoke_fn = NULL;
  1350. }
  1351. return rc;
  1352. }
  1353. #else
  1354. static inline int optee_load_fw(struct platform_device *pdev,
  1355. optee_invoke_fn *invoke_fn)
  1356. {
  1357. return 0;
  1358. }
  1359. #endif
  1360. static int optee_probe(struct platform_device *pdev)
  1361. {
  1362. optee_invoke_fn *invoke_fn;
  1363. struct tee_shm_pool *pool = ERR_PTR(-EINVAL);
  1364. struct optee *optee = NULL;
  1365. void *memremaped_shm = NULL;
  1366. unsigned int rpc_param_count;
  1367. unsigned int thread_count;
  1368. struct tee_device *teedev;
  1369. struct tee_context *ctx;
  1370. u32 max_notif_value;
  1371. u32 arg_cache_flags;
  1372. u32 sec_caps;
  1373. int rc;
  1374. invoke_fn = get_invoke_func(&pdev->dev);
  1375. if (IS_ERR(invoke_fn))
  1376. return PTR_ERR(invoke_fn);
  1377. rc = optee_load_fw(pdev, invoke_fn);
  1378. if (rc)
  1379. return rc;
  1380. if (!optee_msg_api_uid_is_optee_api(invoke_fn)) {
  1381. pr_warn("api uid mismatch\n");
  1382. return -EINVAL;
  1383. }
  1384. optee_msg_get_os_revision(invoke_fn);
  1385. if (!optee_msg_api_revision_is_compatible(invoke_fn)) {
  1386. pr_warn("api revision mismatch\n");
  1387. return -EINVAL;
  1388. }
  1389. thread_count = optee_msg_get_thread_count(invoke_fn);
  1390. if (!optee_msg_exchange_capabilities(invoke_fn, &sec_caps,
  1391. &max_notif_value,
  1392. &rpc_param_count)) {
  1393. pr_warn("capabilities mismatch\n");
  1394. return -EINVAL;
  1395. }
  1396. /*
  1397. * Try to use dynamic shared memory if possible
  1398. */
  1399. if (sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM) {
  1400. /*
  1401. * If we have OPTEE_SMC_SEC_CAP_RPC_ARG we can ask
  1402. * optee_get_msg_arg() to pre-register (by having
  1403. * OPTEE_SHM_ARG_ALLOC_PRIV cleared) the page used to pass
  1404. * an argument struct.
  1405. *
  1406. * With the page is pre-registered we can use a non-zero
  1407. * offset for argument struct, this is indicated with
  1408. * OPTEE_SHM_ARG_SHARED.
  1409. *
  1410. * This means that optee_smc_do_call_with_arg() will use
  1411. * OPTEE_SMC_CALL_WITH_REGD_ARG for pre-registered pages.
  1412. */
  1413. if (sec_caps & OPTEE_SMC_SEC_CAP_RPC_ARG)
  1414. arg_cache_flags = OPTEE_SHM_ARG_SHARED;
  1415. else
  1416. arg_cache_flags = OPTEE_SHM_ARG_ALLOC_PRIV;
  1417. pool = optee_shm_pool_alloc_pages();
  1418. }
  1419. /*
  1420. * If dynamic shared memory is not available or failed - try static one
  1421. */
  1422. if (IS_ERR(pool) && (sec_caps & OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM)) {
  1423. /*
  1424. * The static memory pool can use non-zero page offsets so
  1425. * let optee_get_msg_arg() know that with OPTEE_SHM_ARG_SHARED.
  1426. *
  1427. * optee_get_msg_arg() should not pre-register the
  1428. * allocated page used to pass an argument struct, this is
  1429. * indicated with OPTEE_SHM_ARG_ALLOC_PRIV.
  1430. *
  1431. * This means that optee_smc_do_call_with_arg() will use
  1432. * OPTEE_SMC_CALL_WITH_ARG if rpc_param_count is 0, else
  1433. * OPTEE_SMC_CALL_WITH_RPC_ARG.
  1434. */
  1435. arg_cache_flags = OPTEE_SHM_ARG_SHARED |
  1436. OPTEE_SHM_ARG_ALLOC_PRIV;
  1437. pool = optee_config_shm_memremap(invoke_fn, &memremaped_shm);
  1438. }
  1439. if (IS_ERR(pool))
  1440. return PTR_ERR(pool);
  1441. optee = kzalloc(sizeof(*optee), GFP_KERNEL);
  1442. if (!optee) {
  1443. rc = -ENOMEM;
  1444. goto err_free_pool;
  1445. }
  1446. optee->ops = &optee_ops;
  1447. optee->smc.invoke_fn = invoke_fn;
  1448. optee->smc.sec_caps = sec_caps;
  1449. optee->rpc_param_count = rpc_param_count;
  1450. if (IS_REACHABLE(CONFIG_RPMB) &&
  1451. (sec_caps & OPTEE_SMC_SEC_CAP_RPMB_PROBE))
  1452. optee->in_kernel_rpmb_routing = true;
  1453. teedev = tee_device_alloc(&optee_clnt_desc, NULL, pool, optee);
  1454. if (IS_ERR(teedev)) {
  1455. rc = PTR_ERR(teedev);
  1456. goto err_free_optee;
  1457. }
  1458. optee->teedev = teedev;
  1459. teedev = tee_device_alloc(&optee_supp_desc, NULL, pool, optee);
  1460. if (IS_ERR(teedev)) {
  1461. rc = PTR_ERR(teedev);
  1462. goto err_unreg_teedev;
  1463. }
  1464. optee->supp_teedev = teedev;
  1465. optee_set_dev_group(optee);
  1466. rc = tee_device_register(optee->teedev);
  1467. if (rc)
  1468. goto err_unreg_supp_teedev;
  1469. rc = tee_device_register(optee->supp_teedev);
  1470. if (rc)
  1471. goto err_unreg_supp_teedev;
  1472. optee_cq_init(&optee->call_queue, thread_count);
  1473. optee_supp_init(&optee->supp);
  1474. optee->smc.memremaped_shm = memremaped_shm;
  1475. optee->pool = pool;
  1476. optee_shm_arg_cache_init(optee, arg_cache_flags);
  1477. mutex_init(&optee->rpmb_dev_mutex);
  1478. platform_set_drvdata(pdev, optee);
  1479. ctx = teedev_open(optee->teedev);
  1480. if (IS_ERR(ctx)) {
  1481. rc = PTR_ERR(ctx);
  1482. goto err_supp_uninit;
  1483. }
  1484. optee->ctx = ctx;
  1485. rc = optee_notif_init(optee, max_notif_value);
  1486. if (rc)
  1487. goto err_close_ctx;
  1488. if (sec_caps & OPTEE_SMC_SEC_CAP_ASYNC_NOTIF) {
  1489. unsigned int irq;
  1490. rc = platform_get_irq(pdev, 0);
  1491. if (rc < 0) {
  1492. pr_err("platform_get_irq: ret %d\n", rc);
  1493. goto err_notif_uninit;
  1494. }
  1495. irq = rc;
  1496. rc = optee_smc_notif_init_irq(optee, irq);
  1497. if (rc) {
  1498. irq_dispose_mapping(irq);
  1499. goto err_notif_uninit;
  1500. }
  1501. enable_async_notif(optee->smc.invoke_fn);
  1502. pr_info("Asynchronous notifications enabled\n");
  1503. }
  1504. /*
  1505. * Ensure that there are no pre-existing shm objects before enabling
  1506. * the shm cache so that there's no chance of receiving an invalid
  1507. * address during shutdown. This could occur, for example, if we're
  1508. * kexec booting from an older kernel that did not properly cleanup the
  1509. * shm cache.
  1510. */
  1511. optee_disable_unmapped_shm_cache(optee);
  1512. /*
  1513. * Only enable the shm cache in case we're not able to pass the RPC
  1514. * arg struct right after the normal arg struct.
  1515. */
  1516. if (!optee->rpc_param_count)
  1517. optee_enable_shm_cache(optee);
  1518. if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
  1519. pr_info("dynamic shared memory is enabled\n");
  1520. rc = optee_enumerate_devices(PTA_CMD_GET_DEVICES);
  1521. if (rc)
  1522. goto err_disable_shm_cache;
  1523. INIT_WORK(&optee->rpmb_scan_bus_work, optee_bus_scan_rpmb);
  1524. optee->rpmb_intf.notifier_call = optee_rpmb_intf_rdev;
  1525. blocking_notifier_chain_register(&optee_rpmb_intf_added,
  1526. &optee->rpmb_intf);
  1527. pr_info("initialized driver\n");
  1528. return 0;
  1529. err_disable_shm_cache:
  1530. if (!optee->rpc_param_count)
  1531. optee_disable_shm_cache(optee);
  1532. optee_smc_notif_uninit_irq(optee);
  1533. optee_unregister_devices();
  1534. err_notif_uninit:
  1535. optee_notif_uninit(optee);
  1536. err_close_ctx:
  1537. teedev_close_context(ctx);
  1538. err_supp_uninit:
  1539. rpmb_dev_put(optee->rpmb_dev);
  1540. mutex_destroy(&optee->rpmb_dev_mutex);
  1541. optee_shm_arg_cache_uninit(optee);
  1542. optee_supp_uninit(&optee->supp);
  1543. mutex_destroy(&optee->call_queue.mutex);
  1544. err_unreg_supp_teedev:
  1545. tee_device_unregister(optee->supp_teedev);
  1546. err_unreg_teedev:
  1547. tee_device_unregister(optee->teedev);
  1548. err_free_optee:
  1549. kfree(optee);
  1550. err_free_pool:
  1551. tee_shm_pool_free(pool);
  1552. if (memremaped_shm)
  1553. memunmap(memremaped_shm);
  1554. return rc;
  1555. }
  1556. static const struct of_device_id optee_dt_match[] = {
  1557. { .compatible = "linaro,optee-tz" },
  1558. {},
  1559. };
  1560. MODULE_DEVICE_TABLE(of, optee_dt_match);
  1561. static struct platform_driver optee_driver = {
  1562. .probe = optee_probe,
  1563. .remove_new = optee_smc_remove,
  1564. .shutdown = optee_shutdown,
  1565. .driver = {
  1566. .name = "optee",
  1567. .of_match_table = optee_dt_match,
  1568. },
  1569. };
  1570. int optee_smc_abi_register(void)
  1571. {
  1572. return platform_driver_register(&optee_driver);
  1573. }
  1574. void optee_smc_abi_unregister(void)
  1575. {
  1576. platform_driver_unregister(&optee_driver);
  1577. }