core.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*
  2. * Copyright (c) 2015, Linaro Limited
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/arm-smccc.h>
  16. #include <linux/errno.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include <linux/string.h>
  24. #include <linux/tee_drv.h>
  25. #include <linux/types.h>
  26. #include <linux/uaccess.h>
  27. #include "optee_private.h"
  28. #include "optee_smc.h"
  29. #include "shm_pool.h"
  30. #define DRIVER_NAME "optee"
  31. #define OPTEE_SHM_NUM_PRIV_PAGES CONFIG_OPTEE_SHM_NUM_PRIV_PAGES
  32. /**
  33. * optee_from_msg_param() - convert from OPTEE_MSG parameters to
  34. * struct tee_param
  35. * @params: subsystem internal parameter representation
  36. * @num_params: number of elements in the parameter arrays
  37. * @msg_params: OPTEE_MSG parameters
  38. * Returns 0 on success or <0 on failure
  39. */
  40. int optee_from_msg_param(struct tee_param *params, size_t num_params,
  41. const struct optee_msg_param *msg_params)
  42. {
  43. int rc;
  44. size_t n;
  45. struct tee_shm *shm;
  46. phys_addr_t pa;
  47. for (n = 0; n < num_params; n++) {
  48. struct tee_param *p = params + n;
  49. const struct optee_msg_param *mp = msg_params + n;
  50. u32 attr = mp->attr & OPTEE_MSG_ATTR_TYPE_MASK;
  51. switch (attr) {
  52. case OPTEE_MSG_ATTR_TYPE_NONE:
  53. p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
  54. memset(&p->u, 0, sizeof(p->u));
  55. break;
  56. case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT:
  57. case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT:
  58. case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT:
  59. p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT +
  60. attr - OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
  61. p->u.value.a = mp->u.value.a;
  62. p->u.value.b = mp->u.value.b;
  63. p->u.value.c = mp->u.value.c;
  64. break;
  65. case OPTEE_MSG_ATTR_TYPE_TMEM_INPUT:
  66. case OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT:
  67. case OPTEE_MSG_ATTR_TYPE_TMEM_INOUT:
  68. p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT +
  69. attr - OPTEE_MSG_ATTR_TYPE_TMEM_INPUT;
  70. p->u.memref.size = mp->u.tmem.size;
  71. shm = (struct tee_shm *)(unsigned long)
  72. mp->u.tmem.shm_ref;
  73. if (!shm) {
  74. p->u.memref.shm_offs = 0;
  75. p->u.memref.shm = NULL;
  76. break;
  77. }
  78. rc = tee_shm_get_pa(shm, 0, &pa);
  79. if (rc)
  80. return rc;
  81. p->u.memref.shm_offs = mp->u.tmem.buf_ptr - pa;
  82. p->u.memref.shm = shm;
  83. /* Check that the memref is covered by the shm object */
  84. if (p->u.memref.size) {
  85. size_t o = p->u.memref.shm_offs +
  86. p->u.memref.size - 1;
  87. rc = tee_shm_get_pa(shm, o, NULL);
  88. if (rc)
  89. return rc;
  90. }
  91. break;
  92. case OPTEE_MSG_ATTR_TYPE_RMEM_INPUT:
  93. case OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT:
  94. case OPTEE_MSG_ATTR_TYPE_RMEM_INOUT:
  95. p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT +
  96. attr - OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
  97. p->u.memref.size = mp->u.rmem.size;
  98. shm = (struct tee_shm *)(unsigned long)
  99. mp->u.rmem.shm_ref;
  100. if (!shm) {
  101. p->u.memref.shm_offs = 0;
  102. p->u.memref.shm = NULL;
  103. break;
  104. }
  105. p->u.memref.shm_offs = mp->u.rmem.offs;
  106. p->u.memref.shm = shm;
  107. break;
  108. default:
  109. return -EINVAL;
  110. }
  111. }
  112. return 0;
  113. }
  114. static int to_msg_param_tmp_mem(struct optee_msg_param *mp,
  115. const struct tee_param *p)
  116. {
  117. int rc;
  118. phys_addr_t pa;
  119. mp->attr = OPTEE_MSG_ATTR_TYPE_TMEM_INPUT + p->attr -
  120. TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
  121. mp->u.tmem.shm_ref = (unsigned long)p->u.memref.shm;
  122. mp->u.tmem.size = p->u.memref.size;
  123. if (!p->u.memref.shm) {
  124. mp->u.tmem.buf_ptr = 0;
  125. return 0;
  126. }
  127. rc = tee_shm_get_pa(p->u.memref.shm, p->u.memref.shm_offs, &pa);
  128. if (rc)
  129. return rc;
  130. mp->u.tmem.buf_ptr = pa;
  131. mp->attr |= OPTEE_MSG_ATTR_CACHE_PREDEFINED <<
  132. OPTEE_MSG_ATTR_CACHE_SHIFT;
  133. return 0;
  134. }
  135. static int to_msg_param_reg_mem(struct optee_msg_param *mp,
  136. const struct tee_param *p)
  137. {
  138. mp->attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT + p->attr -
  139. TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
  140. mp->u.rmem.shm_ref = (unsigned long)p->u.memref.shm;
  141. mp->u.rmem.size = p->u.memref.size;
  142. mp->u.rmem.offs = p->u.memref.shm_offs;
  143. return 0;
  144. }
  145. /**
  146. * optee_to_msg_param() - convert from struct tee_params to OPTEE_MSG parameters
  147. * @msg_params: OPTEE_MSG parameters
  148. * @num_params: number of elements in the parameter arrays
  149. * @params: subsystem itnernal parameter representation
  150. * Returns 0 on success or <0 on failure
  151. */
  152. int optee_to_msg_param(struct optee_msg_param *msg_params, size_t num_params,
  153. const struct tee_param *params)
  154. {
  155. int rc;
  156. size_t n;
  157. for (n = 0; n < num_params; n++) {
  158. const struct tee_param *p = params + n;
  159. struct optee_msg_param *mp = msg_params + n;
  160. switch (p->attr) {
  161. case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
  162. mp->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
  163. memset(&mp->u, 0, sizeof(mp->u));
  164. break;
  165. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
  166. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
  167. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
  168. mp->attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr -
  169. TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
  170. mp->u.value.a = p->u.value.a;
  171. mp->u.value.b = p->u.value.b;
  172. mp->u.value.c = p->u.value.c;
  173. break;
  174. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
  175. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
  176. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
  177. if (tee_shm_is_registered(p->u.memref.shm))
  178. rc = to_msg_param_reg_mem(mp, p);
  179. else
  180. rc = to_msg_param_tmp_mem(mp, p);
  181. if (rc)
  182. return rc;
  183. break;
  184. default:
  185. return -EINVAL;
  186. }
  187. }
  188. return 0;
  189. }
  190. static void optee_get_version(struct tee_device *teedev,
  191. struct tee_ioctl_version_data *vers)
  192. {
  193. struct tee_ioctl_version_data v = {
  194. .impl_id = TEE_IMPL_ID_OPTEE,
  195. .impl_caps = TEE_OPTEE_CAP_TZ,
  196. .gen_caps = TEE_GEN_CAP_GP,
  197. };
  198. struct optee *optee = tee_get_drvdata(teedev);
  199. if (optee->sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
  200. v.gen_caps |= TEE_GEN_CAP_REG_MEM;
  201. *vers = v;
  202. }
  203. static int optee_open(struct tee_context *ctx)
  204. {
  205. struct optee_context_data *ctxdata;
  206. struct tee_device *teedev = ctx->teedev;
  207. struct optee *optee = tee_get_drvdata(teedev);
  208. ctxdata = kzalloc(sizeof(*ctxdata), GFP_KERNEL);
  209. if (!ctxdata)
  210. return -ENOMEM;
  211. if (teedev == optee->supp_teedev) {
  212. bool busy = true;
  213. mutex_lock(&optee->supp.mutex);
  214. if (!optee->supp.ctx) {
  215. busy = false;
  216. optee->supp.ctx = ctx;
  217. }
  218. mutex_unlock(&optee->supp.mutex);
  219. if (busy) {
  220. kfree(ctxdata);
  221. return -EBUSY;
  222. }
  223. }
  224. mutex_init(&ctxdata->mutex);
  225. INIT_LIST_HEAD(&ctxdata->sess_list);
  226. ctx->data = ctxdata;
  227. return 0;
  228. }
  229. static void optee_release(struct tee_context *ctx)
  230. {
  231. struct optee_context_data *ctxdata = ctx->data;
  232. struct tee_device *teedev = ctx->teedev;
  233. struct optee *optee = tee_get_drvdata(teedev);
  234. struct tee_shm *shm;
  235. struct optee_msg_arg *arg = NULL;
  236. phys_addr_t parg;
  237. struct optee_session *sess;
  238. struct optee_session *sess_tmp;
  239. if (!ctxdata)
  240. return;
  241. shm = tee_shm_alloc(ctx, sizeof(struct optee_msg_arg), TEE_SHM_MAPPED);
  242. if (!IS_ERR(shm)) {
  243. arg = tee_shm_get_va(shm, 0);
  244. /*
  245. * If va2pa fails for some reason, we can't call into
  246. * secure world, only free the memory. Secure OS will leak
  247. * sessions and finally refuse more sessions, but we will
  248. * at least let normal world reclaim its memory.
  249. */
  250. if (!IS_ERR(arg))
  251. if (tee_shm_va2pa(shm, arg, &parg))
  252. arg = NULL; /* prevent usage of parg below */
  253. }
  254. list_for_each_entry_safe(sess, sess_tmp, &ctxdata->sess_list,
  255. list_node) {
  256. list_del(&sess->list_node);
  257. if (!IS_ERR_OR_NULL(arg)) {
  258. memset(arg, 0, sizeof(*arg));
  259. arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
  260. arg->session = sess->session_id;
  261. optee_do_call_with_arg(ctx, parg);
  262. }
  263. kfree(sess);
  264. }
  265. kfree(ctxdata);
  266. if (!IS_ERR(shm))
  267. tee_shm_free(shm);
  268. ctx->data = NULL;
  269. if (teedev == optee->supp_teedev)
  270. optee_supp_release(&optee->supp);
  271. }
  272. static const struct tee_driver_ops optee_ops = {
  273. .get_version = optee_get_version,
  274. .open = optee_open,
  275. .release = optee_release,
  276. .open_session = optee_open_session,
  277. .close_session = optee_close_session,
  278. .invoke_func = optee_invoke_func,
  279. .cancel_req = optee_cancel_req,
  280. .shm_register = optee_shm_register,
  281. .shm_unregister = optee_shm_unregister,
  282. };
  283. static const struct tee_desc optee_desc = {
  284. .name = DRIVER_NAME "-clnt",
  285. .ops = &optee_ops,
  286. .owner = THIS_MODULE,
  287. };
  288. static const struct tee_driver_ops optee_supp_ops = {
  289. .get_version = optee_get_version,
  290. .open = optee_open,
  291. .release = optee_release,
  292. .supp_recv = optee_supp_recv,
  293. .supp_send = optee_supp_send,
  294. .shm_register = optee_shm_register_supp,
  295. .shm_unregister = optee_shm_unregister_supp,
  296. };
  297. static const struct tee_desc optee_supp_desc = {
  298. .name = DRIVER_NAME "-supp",
  299. .ops = &optee_supp_ops,
  300. .owner = THIS_MODULE,
  301. .flags = TEE_DESC_PRIVILEGED,
  302. };
  303. static bool optee_msg_api_uid_is_optee_api(optee_invoke_fn *invoke_fn)
  304. {
  305. struct arm_smccc_res res;
  306. invoke_fn(OPTEE_SMC_CALLS_UID, 0, 0, 0, 0, 0, 0, 0, &res);
  307. if (res.a0 == OPTEE_MSG_UID_0 && res.a1 == OPTEE_MSG_UID_1 &&
  308. res.a2 == OPTEE_MSG_UID_2 && res.a3 == OPTEE_MSG_UID_3)
  309. return true;
  310. return false;
  311. }
  312. static void optee_msg_get_os_revision(optee_invoke_fn *invoke_fn)
  313. {
  314. union {
  315. struct arm_smccc_res smccc;
  316. struct optee_smc_call_get_os_revision_result result;
  317. } res = {
  318. .result = {
  319. .build_id = 0
  320. }
  321. };
  322. invoke_fn(OPTEE_SMC_CALL_GET_OS_REVISION, 0, 0, 0, 0, 0, 0, 0,
  323. &res.smccc);
  324. if (res.result.build_id)
  325. pr_info("revision %lu.%lu (%08lx)", res.result.major,
  326. res.result.minor, res.result.build_id);
  327. else
  328. pr_info("revision %lu.%lu", res.result.major, res.result.minor);
  329. }
  330. static bool optee_msg_api_revision_is_compatible(optee_invoke_fn *invoke_fn)
  331. {
  332. union {
  333. struct arm_smccc_res smccc;
  334. struct optee_smc_calls_revision_result result;
  335. } res;
  336. invoke_fn(OPTEE_SMC_CALLS_REVISION, 0, 0, 0, 0, 0, 0, 0, &res.smccc);
  337. if (res.result.major == OPTEE_MSG_REVISION_MAJOR &&
  338. (int)res.result.minor >= OPTEE_MSG_REVISION_MINOR)
  339. return true;
  340. return false;
  341. }
  342. static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn,
  343. u32 *sec_caps)
  344. {
  345. union {
  346. struct arm_smccc_res smccc;
  347. struct optee_smc_exchange_capabilities_result result;
  348. } res;
  349. u32 a1 = 0;
  350. /*
  351. * TODO This isn't enough to tell if it's UP system (from kernel
  352. * point of view) or not, is_smp() returns the the information
  353. * needed, but can't be called directly from here.
  354. */
  355. if (!IS_ENABLED(CONFIG_SMP) || nr_cpu_ids == 1)
  356. a1 |= OPTEE_SMC_NSEC_CAP_UNIPROCESSOR;
  357. invoke_fn(OPTEE_SMC_EXCHANGE_CAPABILITIES, a1, 0, 0, 0, 0, 0, 0,
  358. &res.smccc);
  359. if (res.result.status != OPTEE_SMC_RETURN_OK)
  360. return false;
  361. *sec_caps = res.result.capabilities;
  362. return true;
  363. }
  364. static struct tee_shm_pool *
  365. optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm,
  366. u32 sec_caps)
  367. {
  368. union {
  369. struct arm_smccc_res smccc;
  370. struct optee_smc_get_shm_config_result result;
  371. } res;
  372. unsigned long vaddr;
  373. phys_addr_t paddr;
  374. size_t size;
  375. phys_addr_t begin;
  376. phys_addr_t end;
  377. void *va;
  378. struct tee_shm_pool_mgr *priv_mgr;
  379. struct tee_shm_pool_mgr *dmabuf_mgr;
  380. void *rc;
  381. invoke_fn(OPTEE_SMC_GET_SHM_CONFIG, 0, 0, 0, 0, 0, 0, 0, &res.smccc);
  382. if (res.result.status != OPTEE_SMC_RETURN_OK) {
  383. pr_info("shm service not available\n");
  384. return ERR_PTR(-ENOENT);
  385. }
  386. if (res.result.settings != OPTEE_SMC_SHM_CACHED) {
  387. pr_err("only normal cached shared memory supported\n");
  388. return ERR_PTR(-EINVAL);
  389. }
  390. begin = roundup(res.result.start, PAGE_SIZE);
  391. end = rounddown(res.result.start + res.result.size, PAGE_SIZE);
  392. paddr = begin;
  393. size = end - begin;
  394. if (size < 2 * OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE) {
  395. pr_err("too small shared memory area\n");
  396. return ERR_PTR(-EINVAL);
  397. }
  398. va = memremap(paddr, size, MEMREMAP_WB);
  399. if (!va) {
  400. pr_err("shared memory ioremap failed\n");
  401. return ERR_PTR(-EINVAL);
  402. }
  403. vaddr = (unsigned long)va;
  404. /*
  405. * If OP-TEE can work with unregistered SHM, we will use own pool
  406. * for private shm
  407. */
  408. if (sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM) {
  409. rc = optee_shm_pool_alloc_pages();
  410. if (IS_ERR(rc))
  411. goto err_memunmap;
  412. priv_mgr = rc;
  413. } else {
  414. const size_t sz = OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE;
  415. rc = tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, sz,
  416. 3 /* 8 bytes aligned */);
  417. if (IS_ERR(rc))
  418. goto err_memunmap;
  419. priv_mgr = rc;
  420. vaddr += sz;
  421. paddr += sz;
  422. size -= sz;
  423. }
  424. rc = tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, size, PAGE_SHIFT);
  425. if (IS_ERR(rc))
  426. goto err_free_priv_mgr;
  427. dmabuf_mgr = rc;
  428. rc = tee_shm_pool_alloc(priv_mgr, dmabuf_mgr);
  429. if (IS_ERR(rc))
  430. goto err_free_dmabuf_mgr;
  431. *memremaped_shm = va;
  432. return rc;
  433. err_free_dmabuf_mgr:
  434. tee_shm_pool_mgr_destroy(dmabuf_mgr);
  435. err_free_priv_mgr:
  436. tee_shm_pool_mgr_destroy(priv_mgr);
  437. err_memunmap:
  438. memunmap(va);
  439. return rc;
  440. }
  441. /* Simple wrapper functions to be able to use a function pointer */
  442. static void optee_smccc_smc(unsigned long a0, unsigned long a1,
  443. unsigned long a2, unsigned long a3,
  444. unsigned long a4, unsigned long a5,
  445. unsigned long a6, unsigned long a7,
  446. struct arm_smccc_res *res)
  447. {
  448. arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res);
  449. }
  450. static void optee_smccc_hvc(unsigned long a0, unsigned long a1,
  451. unsigned long a2, unsigned long a3,
  452. unsigned long a4, unsigned long a5,
  453. unsigned long a6, unsigned long a7,
  454. struct arm_smccc_res *res)
  455. {
  456. arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
  457. }
  458. static optee_invoke_fn *get_invoke_func(struct device_node *np)
  459. {
  460. const char *method;
  461. pr_info("probing for conduit method from DT.\n");
  462. if (of_property_read_string(np, "method", &method)) {
  463. pr_warn("missing \"method\" property\n");
  464. return ERR_PTR(-ENXIO);
  465. }
  466. if (!strcmp("hvc", method))
  467. return optee_smccc_hvc;
  468. else if (!strcmp("smc", method))
  469. return optee_smccc_smc;
  470. pr_warn("invalid \"method\" property: %s\n", method);
  471. return ERR_PTR(-EINVAL);
  472. }
  473. static struct optee *optee_probe(struct device_node *np)
  474. {
  475. optee_invoke_fn *invoke_fn;
  476. struct tee_shm_pool *pool;
  477. struct optee *optee = NULL;
  478. void *memremaped_shm = NULL;
  479. struct tee_device *teedev;
  480. u32 sec_caps;
  481. int rc;
  482. invoke_fn = get_invoke_func(np);
  483. if (IS_ERR(invoke_fn))
  484. return (void *)invoke_fn;
  485. if (!optee_msg_api_uid_is_optee_api(invoke_fn)) {
  486. pr_warn("api uid mismatch\n");
  487. return ERR_PTR(-EINVAL);
  488. }
  489. optee_msg_get_os_revision(invoke_fn);
  490. if (!optee_msg_api_revision_is_compatible(invoke_fn)) {
  491. pr_warn("api revision mismatch\n");
  492. return ERR_PTR(-EINVAL);
  493. }
  494. if (!optee_msg_exchange_capabilities(invoke_fn, &sec_caps)) {
  495. pr_warn("capabilities mismatch\n");
  496. return ERR_PTR(-EINVAL);
  497. }
  498. /*
  499. * We have no other option for shared memory, if secure world
  500. * doesn't have any reserved memory we can use we can't continue.
  501. */
  502. if (!(sec_caps & OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM))
  503. return ERR_PTR(-EINVAL);
  504. pool = optee_config_shm_memremap(invoke_fn, &memremaped_shm, sec_caps);
  505. if (IS_ERR(pool))
  506. return (void *)pool;
  507. optee = kzalloc(sizeof(*optee), GFP_KERNEL);
  508. if (!optee) {
  509. rc = -ENOMEM;
  510. goto err;
  511. }
  512. optee->invoke_fn = invoke_fn;
  513. optee->sec_caps = sec_caps;
  514. teedev = tee_device_alloc(&optee_desc, NULL, pool, optee);
  515. if (IS_ERR(teedev)) {
  516. rc = PTR_ERR(teedev);
  517. goto err;
  518. }
  519. optee->teedev = teedev;
  520. teedev = tee_device_alloc(&optee_supp_desc, NULL, pool, optee);
  521. if (IS_ERR(teedev)) {
  522. rc = PTR_ERR(teedev);
  523. goto err;
  524. }
  525. optee->supp_teedev = teedev;
  526. rc = tee_device_register(optee->teedev);
  527. if (rc)
  528. goto err;
  529. rc = tee_device_register(optee->supp_teedev);
  530. if (rc)
  531. goto err;
  532. mutex_init(&optee->call_queue.mutex);
  533. INIT_LIST_HEAD(&optee->call_queue.waiters);
  534. optee_wait_queue_init(&optee->wait_queue);
  535. optee_supp_init(&optee->supp);
  536. optee->memremaped_shm = memremaped_shm;
  537. optee->pool = pool;
  538. optee_enable_shm_cache(optee);
  539. pr_info("initialized driver\n");
  540. return optee;
  541. err:
  542. if (optee) {
  543. /*
  544. * tee_device_unregister() is safe to call even if the
  545. * devices hasn't been registered with
  546. * tee_device_register() yet.
  547. */
  548. tee_device_unregister(optee->supp_teedev);
  549. tee_device_unregister(optee->teedev);
  550. kfree(optee);
  551. }
  552. if (pool)
  553. tee_shm_pool_free(pool);
  554. if (memremaped_shm)
  555. memunmap(memremaped_shm);
  556. return ERR_PTR(rc);
  557. }
  558. static void optee_remove(struct optee *optee)
  559. {
  560. /*
  561. * Ask OP-TEE to free all cached shared memory objects to decrease
  562. * reference counters and also avoid wild pointers in secure world
  563. * into the old shared memory range.
  564. */
  565. optee_disable_shm_cache(optee);
  566. /*
  567. * The two devices has to be unregistered before we can free the
  568. * other resources.
  569. */
  570. tee_device_unregister(optee->supp_teedev);
  571. tee_device_unregister(optee->teedev);
  572. tee_shm_pool_free(optee->pool);
  573. if (optee->memremaped_shm)
  574. memunmap(optee->memremaped_shm);
  575. optee_wait_queue_exit(&optee->wait_queue);
  576. optee_supp_uninit(&optee->supp);
  577. mutex_destroy(&optee->call_queue.mutex);
  578. kfree(optee);
  579. }
  580. static const struct of_device_id optee_match[] = {
  581. { .compatible = "linaro,optee-tz" },
  582. {},
  583. };
  584. static struct optee *optee_svc;
  585. static int __init optee_driver_init(void)
  586. {
  587. struct device_node *fw_np;
  588. struct device_node *np;
  589. struct optee *optee;
  590. /* Node is supposed to be below /firmware */
  591. fw_np = of_find_node_by_name(NULL, "firmware");
  592. if (!fw_np)
  593. return -ENODEV;
  594. np = of_find_matching_node(fw_np, optee_match);
  595. if (!np || !of_device_is_available(np))
  596. return -ENODEV;
  597. optee = optee_probe(np);
  598. of_node_put(np);
  599. if (IS_ERR(optee))
  600. return PTR_ERR(optee);
  601. optee_svc = optee;
  602. return 0;
  603. }
  604. module_init(optee_driver_init);
  605. static void __exit optee_driver_exit(void)
  606. {
  607. struct optee *optee = optee_svc;
  608. optee_svc = NULL;
  609. if (optee)
  610. optee_remove(optee);
  611. }
  612. module_exit(optee_driver_exit);
  613. MODULE_AUTHOR("Linaro");
  614. MODULE_DESCRIPTION("OP-TEE driver");
  615. MODULE_SUPPORTED_DEVICE("");
  616. MODULE_VERSION("1.0");
  617. MODULE_LICENSE("GPL v2");