ffa_abi.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021, 2023 Linaro Limited
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/arm_ffa.h>
  7. #include <linux/errno.h>
  8. #include <linux/rpmb.h>
  9. #include <linux/scatterlist.h>
  10. #include <linux/sched.h>
  11. #include <linux/slab.h>
  12. #include <linux/string.h>
  13. #include <linux/tee_core.h>
  14. #include <linux/types.h>
  15. #include "optee_private.h"
  16. #include "optee_ffa.h"
  17. #include "optee_rpc_cmd.h"
  18. /*
  19. * This file implement the FF-A ABI used when communicating with secure world
  20. * OP-TEE OS via FF-A.
  21. * This file is divided into the following sections:
  22. * 1. Maintain a hash table for lookup of a global FF-A memory handle
  23. * 2. Convert between struct tee_param and struct optee_msg_param
  24. * 3. Low level support functions to register shared memory in secure world
  25. * 4. Dynamic shared memory pool based on alloc_pages()
  26. * 5. Do a normal scheduled call into secure world
  27. * 6. Driver initialization.
  28. */
  29. /*
  30. * 1. Maintain a hash table for lookup of a global FF-A memory handle
  31. *
  32. * FF-A assigns a global memory handle for each piece shared memory.
  33. * This handle is then used when communicating with secure world.
  34. *
  35. * Main functions are optee_shm_add_ffa_handle() and optee_shm_rem_ffa_handle()
  36. */
  37. struct shm_rhash {
  38. struct tee_shm *shm;
  39. u64 global_id;
  40. struct rhash_head linkage;
  41. };
  42. static void rh_free_fn(void *ptr, void *arg)
  43. {
  44. kfree(ptr);
  45. }
  46. static const struct rhashtable_params shm_rhash_params = {
  47. .head_offset = offsetof(struct shm_rhash, linkage),
  48. .key_len = sizeof(u64),
  49. .key_offset = offsetof(struct shm_rhash, global_id),
  50. .automatic_shrinking = true,
  51. };
  52. static struct tee_shm *optee_shm_from_ffa_handle(struct optee *optee,
  53. u64 global_id)
  54. {
  55. struct tee_shm *shm = NULL;
  56. struct shm_rhash *r;
  57. mutex_lock(&optee->ffa.mutex);
  58. r = rhashtable_lookup_fast(&optee->ffa.global_ids, &global_id,
  59. shm_rhash_params);
  60. if (r)
  61. shm = r->shm;
  62. mutex_unlock(&optee->ffa.mutex);
  63. return shm;
  64. }
  65. static int optee_shm_add_ffa_handle(struct optee *optee, struct tee_shm *shm,
  66. u64 global_id)
  67. {
  68. struct shm_rhash *r;
  69. int rc;
  70. r = kmalloc(sizeof(*r), GFP_KERNEL);
  71. if (!r)
  72. return -ENOMEM;
  73. r->shm = shm;
  74. r->global_id = global_id;
  75. mutex_lock(&optee->ffa.mutex);
  76. rc = rhashtable_lookup_insert_fast(&optee->ffa.global_ids, &r->linkage,
  77. shm_rhash_params);
  78. mutex_unlock(&optee->ffa.mutex);
  79. if (rc)
  80. kfree(r);
  81. return rc;
  82. }
  83. static int optee_shm_rem_ffa_handle(struct optee *optee, u64 global_id)
  84. {
  85. struct shm_rhash *r;
  86. int rc = -ENOENT;
  87. mutex_lock(&optee->ffa.mutex);
  88. r = rhashtable_lookup_fast(&optee->ffa.global_ids, &global_id,
  89. shm_rhash_params);
  90. if (r)
  91. rc = rhashtable_remove_fast(&optee->ffa.global_ids,
  92. &r->linkage, shm_rhash_params);
  93. mutex_unlock(&optee->ffa.mutex);
  94. if (!rc)
  95. kfree(r);
  96. return rc;
  97. }
  98. /*
  99. * 2. Convert between struct tee_param and struct optee_msg_param
  100. *
  101. * optee_ffa_from_msg_param() and optee_ffa_to_msg_param() are the main
  102. * functions.
  103. */
  104. static void from_msg_param_ffa_mem(struct optee *optee, struct tee_param *p,
  105. u32 attr, const struct optee_msg_param *mp)
  106. {
  107. struct tee_shm *shm = NULL;
  108. u64 offs_high = 0;
  109. u64 offs_low = 0;
  110. p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT +
  111. attr - OPTEE_MSG_ATTR_TYPE_FMEM_INPUT;
  112. p->u.memref.size = mp->u.fmem.size;
  113. if (mp->u.fmem.global_id != OPTEE_MSG_FMEM_INVALID_GLOBAL_ID)
  114. shm = optee_shm_from_ffa_handle(optee, mp->u.fmem.global_id);
  115. p->u.memref.shm = shm;
  116. if (shm) {
  117. offs_low = mp->u.fmem.offs_low;
  118. offs_high = mp->u.fmem.offs_high;
  119. }
  120. p->u.memref.shm_offs = offs_low | offs_high << 32;
  121. }
  122. /**
  123. * optee_ffa_from_msg_param() - convert from OPTEE_MSG parameters to
  124. * struct tee_param
  125. * @optee: main service struct
  126. * @params: subsystem internal parameter representation
  127. * @num_params: number of elements in the parameter arrays
  128. * @msg_params: OPTEE_MSG parameters
  129. *
  130. * Returns 0 on success or <0 on failure
  131. */
  132. static int optee_ffa_from_msg_param(struct optee *optee,
  133. struct tee_param *params, size_t num_params,
  134. const struct optee_msg_param *msg_params)
  135. {
  136. size_t n;
  137. for (n = 0; n < num_params; n++) {
  138. struct tee_param *p = params + n;
  139. const struct optee_msg_param *mp = msg_params + n;
  140. u32 attr = mp->attr & OPTEE_MSG_ATTR_TYPE_MASK;
  141. switch (attr) {
  142. case OPTEE_MSG_ATTR_TYPE_NONE:
  143. p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
  144. memset(&p->u, 0, sizeof(p->u));
  145. break;
  146. case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT:
  147. case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT:
  148. case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT:
  149. optee_from_msg_param_value(p, attr, mp);
  150. break;
  151. case OPTEE_MSG_ATTR_TYPE_FMEM_INPUT:
  152. case OPTEE_MSG_ATTR_TYPE_FMEM_OUTPUT:
  153. case OPTEE_MSG_ATTR_TYPE_FMEM_INOUT:
  154. from_msg_param_ffa_mem(optee, p, attr, mp);
  155. break;
  156. default:
  157. return -EINVAL;
  158. }
  159. }
  160. return 0;
  161. }
  162. static int to_msg_param_ffa_mem(struct optee_msg_param *mp,
  163. const struct tee_param *p)
  164. {
  165. struct tee_shm *shm = p->u.memref.shm;
  166. mp->attr = OPTEE_MSG_ATTR_TYPE_FMEM_INPUT + p->attr -
  167. TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
  168. if (shm) {
  169. u64 shm_offs = p->u.memref.shm_offs;
  170. mp->u.fmem.internal_offs = shm->offset;
  171. mp->u.fmem.offs_low = shm_offs;
  172. mp->u.fmem.offs_high = shm_offs >> 32;
  173. /* Check that the entire offset could be stored. */
  174. if (mp->u.fmem.offs_high != shm_offs >> 32)
  175. return -EINVAL;
  176. mp->u.fmem.global_id = shm->sec_world_id;
  177. } else {
  178. memset(&mp->u, 0, sizeof(mp->u));
  179. mp->u.fmem.global_id = OPTEE_MSG_FMEM_INVALID_GLOBAL_ID;
  180. }
  181. mp->u.fmem.size = p->u.memref.size;
  182. return 0;
  183. }
  184. /**
  185. * optee_ffa_to_msg_param() - convert from struct tee_params to OPTEE_MSG
  186. * parameters
  187. * @optee: main service struct
  188. * @msg_params: OPTEE_MSG parameters
  189. * @num_params: number of elements in the parameter arrays
  190. * @params: subsystem itnernal parameter representation
  191. * Returns 0 on success or <0 on failure
  192. */
  193. static int optee_ffa_to_msg_param(struct optee *optee,
  194. struct optee_msg_param *msg_params,
  195. size_t num_params,
  196. const struct tee_param *params)
  197. {
  198. size_t n;
  199. for (n = 0; n < num_params; n++) {
  200. const struct tee_param *p = params + n;
  201. struct optee_msg_param *mp = msg_params + n;
  202. switch (p->attr) {
  203. case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
  204. mp->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
  205. memset(&mp->u, 0, sizeof(mp->u));
  206. break;
  207. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
  208. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
  209. case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
  210. optee_to_msg_param_value(mp, p);
  211. break;
  212. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
  213. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
  214. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
  215. if (to_msg_param_ffa_mem(mp, p))
  216. return -EINVAL;
  217. break;
  218. default:
  219. return -EINVAL;
  220. }
  221. }
  222. return 0;
  223. }
  224. /*
  225. * 3. Low level support functions to register shared memory in secure world
  226. *
  227. * Functions to register and unregister shared memory both for normal
  228. * clients and for tee-supplicant.
  229. */
  230. static int optee_ffa_shm_register(struct tee_context *ctx, struct tee_shm *shm,
  231. struct page **pages, size_t num_pages,
  232. unsigned long start)
  233. {
  234. struct optee *optee = tee_get_drvdata(ctx->teedev);
  235. struct ffa_device *ffa_dev = optee->ffa.ffa_dev;
  236. const struct ffa_mem_ops *mem_ops = ffa_dev->ops->mem_ops;
  237. struct ffa_mem_region_attributes mem_attr = {
  238. .receiver = ffa_dev->vm_id,
  239. .attrs = FFA_MEM_RW,
  240. };
  241. struct ffa_mem_ops_args args = {
  242. .use_txbuf = true,
  243. .attrs = &mem_attr,
  244. .nattrs = 1,
  245. };
  246. struct sg_table sgt;
  247. int rc;
  248. rc = optee_check_mem_type(start, num_pages);
  249. if (rc)
  250. return rc;
  251. rc = sg_alloc_table_from_pages(&sgt, pages, num_pages, 0,
  252. num_pages * PAGE_SIZE, GFP_KERNEL);
  253. if (rc)
  254. return rc;
  255. args.sg = sgt.sgl;
  256. rc = mem_ops->memory_share(&args);
  257. sg_free_table(&sgt);
  258. if (rc)
  259. return rc;
  260. rc = optee_shm_add_ffa_handle(optee, shm, args.g_handle);
  261. if (rc) {
  262. mem_ops->memory_reclaim(args.g_handle, 0);
  263. return rc;
  264. }
  265. shm->sec_world_id = args.g_handle;
  266. return 0;
  267. }
  268. static int optee_ffa_shm_unregister(struct tee_context *ctx,
  269. struct tee_shm *shm)
  270. {
  271. struct optee *optee = tee_get_drvdata(ctx->teedev);
  272. struct ffa_device *ffa_dev = optee->ffa.ffa_dev;
  273. const struct ffa_msg_ops *msg_ops = ffa_dev->ops->msg_ops;
  274. const struct ffa_mem_ops *mem_ops = ffa_dev->ops->mem_ops;
  275. u64 global_handle = shm->sec_world_id;
  276. struct ffa_send_direct_data data = {
  277. .data0 = OPTEE_FFA_UNREGISTER_SHM,
  278. .data1 = (u32)global_handle,
  279. .data2 = (u32)(global_handle >> 32)
  280. };
  281. int rc;
  282. optee_shm_rem_ffa_handle(optee, global_handle);
  283. shm->sec_world_id = 0;
  284. rc = msg_ops->sync_send_receive(ffa_dev, &data);
  285. if (rc)
  286. pr_err("Unregister SHM id 0x%llx rc %d\n", global_handle, rc);
  287. rc = mem_ops->memory_reclaim(global_handle, 0);
  288. if (rc)
  289. pr_err("mem_reclaim: 0x%llx %d", global_handle, rc);
  290. return rc;
  291. }
  292. static int optee_ffa_shm_unregister_supp(struct tee_context *ctx,
  293. struct tee_shm *shm)
  294. {
  295. struct optee *optee = tee_get_drvdata(ctx->teedev);
  296. const struct ffa_mem_ops *mem_ops;
  297. u64 global_handle = shm->sec_world_id;
  298. int rc;
  299. /*
  300. * We're skipping the OPTEE_FFA_YIELDING_CALL_UNREGISTER_SHM call
  301. * since this is OP-TEE freeing via RPC so it has already retired
  302. * this ID.
  303. */
  304. optee_shm_rem_ffa_handle(optee, global_handle);
  305. mem_ops = optee->ffa.ffa_dev->ops->mem_ops;
  306. rc = mem_ops->memory_reclaim(global_handle, 0);
  307. if (rc)
  308. pr_err("mem_reclaim: 0x%llx %d", global_handle, rc);
  309. shm->sec_world_id = 0;
  310. return rc;
  311. }
  312. /*
  313. * 4. Dynamic shared memory pool based on alloc_pages()
  314. *
  315. * Implements an OP-TEE specific shared memory pool.
  316. * The main function is optee_ffa_shm_pool_alloc_pages().
  317. */
  318. static int pool_ffa_op_alloc(struct tee_shm_pool *pool,
  319. struct tee_shm *shm, size_t size, size_t align)
  320. {
  321. return tee_dyn_shm_alloc_helper(shm, size, align,
  322. optee_ffa_shm_register);
  323. }
  324. static void pool_ffa_op_free(struct tee_shm_pool *pool,
  325. struct tee_shm *shm)
  326. {
  327. tee_dyn_shm_free_helper(shm, optee_ffa_shm_unregister);
  328. }
  329. static void pool_ffa_op_destroy_pool(struct tee_shm_pool *pool)
  330. {
  331. kfree(pool);
  332. }
  333. static const struct tee_shm_pool_ops pool_ffa_ops = {
  334. .alloc = pool_ffa_op_alloc,
  335. .free = pool_ffa_op_free,
  336. .destroy_pool = pool_ffa_op_destroy_pool,
  337. };
  338. /**
  339. * optee_ffa_shm_pool_alloc_pages() - create page-based allocator pool
  340. *
  341. * This pool is used with OP-TEE over FF-A. In this case command buffers
  342. * and such are allocated from kernel's own memory.
  343. */
  344. static struct tee_shm_pool *optee_ffa_shm_pool_alloc_pages(void)
  345. {
  346. struct tee_shm_pool *pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  347. if (!pool)
  348. return ERR_PTR(-ENOMEM);
  349. pool->ops = &pool_ffa_ops;
  350. return pool;
  351. }
  352. /*
  353. * 5. Do a normal scheduled call into secure world
  354. *
  355. * The function optee_ffa_do_call_with_arg() performs a normal scheduled
  356. * call into secure world. During this call may normal world request help
  357. * from normal world using RPCs, Remote Procedure Calls. This includes
  358. * delivery of non-secure interrupts to for instance allow rescheduling of
  359. * the current task.
  360. */
  361. static void handle_ffa_rpc_func_cmd_shm_alloc(struct tee_context *ctx,
  362. struct optee *optee,
  363. struct optee_msg_arg *arg)
  364. {
  365. struct tee_shm *shm;
  366. if (arg->num_params != 1 ||
  367. arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
  368. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  369. return;
  370. }
  371. switch (arg->params[0].u.value.a) {
  372. case OPTEE_RPC_SHM_TYPE_APPL:
  373. shm = optee_rpc_cmd_alloc_suppl(ctx, arg->params[0].u.value.b);
  374. break;
  375. case OPTEE_RPC_SHM_TYPE_KERNEL:
  376. shm = tee_shm_alloc_priv_buf(optee->ctx,
  377. arg->params[0].u.value.b);
  378. break;
  379. default:
  380. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  381. return;
  382. }
  383. if (IS_ERR(shm)) {
  384. arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
  385. return;
  386. }
  387. arg->params[0] = (struct optee_msg_param){
  388. .attr = OPTEE_MSG_ATTR_TYPE_FMEM_OUTPUT,
  389. .u.fmem.size = tee_shm_get_size(shm),
  390. .u.fmem.global_id = shm->sec_world_id,
  391. .u.fmem.internal_offs = shm->offset,
  392. };
  393. arg->ret = TEEC_SUCCESS;
  394. }
  395. static void handle_ffa_rpc_func_cmd_shm_free(struct tee_context *ctx,
  396. struct optee *optee,
  397. struct optee_msg_arg *arg)
  398. {
  399. struct tee_shm *shm;
  400. if (arg->num_params != 1 ||
  401. arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT)
  402. goto err_bad_param;
  403. shm = optee_shm_from_ffa_handle(optee, arg->params[0].u.value.b);
  404. if (!shm)
  405. goto err_bad_param;
  406. switch (arg->params[0].u.value.a) {
  407. case OPTEE_RPC_SHM_TYPE_APPL:
  408. optee_rpc_cmd_free_suppl(ctx, shm);
  409. break;
  410. case OPTEE_RPC_SHM_TYPE_KERNEL:
  411. tee_shm_free(shm);
  412. break;
  413. default:
  414. goto err_bad_param;
  415. }
  416. arg->ret = TEEC_SUCCESS;
  417. return;
  418. err_bad_param:
  419. arg->ret = TEEC_ERROR_BAD_PARAMETERS;
  420. }
  421. static void handle_ffa_rpc_func_cmd(struct tee_context *ctx,
  422. struct optee *optee,
  423. struct optee_msg_arg *arg)
  424. {
  425. arg->ret_origin = TEEC_ORIGIN_COMMS;
  426. switch (arg->cmd) {
  427. case OPTEE_RPC_CMD_SHM_ALLOC:
  428. handle_ffa_rpc_func_cmd_shm_alloc(ctx, optee, arg);
  429. break;
  430. case OPTEE_RPC_CMD_SHM_FREE:
  431. handle_ffa_rpc_func_cmd_shm_free(ctx, optee, arg);
  432. break;
  433. default:
  434. optee_rpc_cmd(ctx, optee, arg);
  435. }
  436. }
  437. static void optee_handle_ffa_rpc(struct tee_context *ctx, struct optee *optee,
  438. u32 cmd, struct optee_msg_arg *arg)
  439. {
  440. switch (cmd) {
  441. case OPTEE_FFA_YIELDING_CALL_RETURN_RPC_CMD:
  442. handle_ffa_rpc_func_cmd(ctx, optee, arg);
  443. break;
  444. case OPTEE_FFA_YIELDING_CALL_RETURN_INTERRUPT:
  445. /* Interrupt delivered by now */
  446. break;
  447. default:
  448. pr_warn("Unknown RPC func 0x%x\n", cmd);
  449. break;
  450. }
  451. }
  452. static int optee_ffa_yielding_call(struct tee_context *ctx,
  453. struct ffa_send_direct_data *data,
  454. struct optee_msg_arg *rpc_arg,
  455. bool system_thread)
  456. {
  457. struct optee *optee = tee_get_drvdata(ctx->teedev);
  458. struct ffa_device *ffa_dev = optee->ffa.ffa_dev;
  459. const struct ffa_msg_ops *msg_ops = ffa_dev->ops->msg_ops;
  460. struct optee_call_waiter w;
  461. u32 cmd = data->data0;
  462. u32 w4 = data->data1;
  463. u32 w5 = data->data2;
  464. u32 w6 = data->data3;
  465. int rc;
  466. /* Initialize waiter */
  467. optee_cq_wait_init(&optee->call_queue, &w, system_thread);
  468. while (true) {
  469. rc = msg_ops->sync_send_receive(ffa_dev, data);
  470. if (rc)
  471. goto done;
  472. switch ((int)data->data0) {
  473. case TEEC_SUCCESS:
  474. break;
  475. case TEEC_ERROR_BUSY:
  476. if (cmd == OPTEE_FFA_YIELDING_CALL_RESUME) {
  477. rc = -EIO;
  478. goto done;
  479. }
  480. /*
  481. * Out of threads in secure world, wait for a thread
  482. * become available.
  483. */
  484. optee_cq_wait_for_completion(&optee->call_queue, &w);
  485. data->data0 = cmd;
  486. data->data1 = w4;
  487. data->data2 = w5;
  488. data->data3 = w6;
  489. continue;
  490. default:
  491. rc = -EIO;
  492. goto done;
  493. }
  494. if (data->data1 == OPTEE_FFA_YIELDING_CALL_RETURN_DONE)
  495. goto done;
  496. /*
  497. * OP-TEE has returned with a RPC request.
  498. *
  499. * Note that data->data4 (passed in register w7) is already
  500. * filled in by ffa_mem_ops->sync_send_receive() returning
  501. * above.
  502. */
  503. cond_resched();
  504. optee_handle_ffa_rpc(ctx, optee, data->data1, rpc_arg);
  505. cmd = OPTEE_FFA_YIELDING_CALL_RESUME;
  506. data->data0 = cmd;
  507. data->data1 = 0;
  508. data->data2 = 0;
  509. data->data3 = 0;
  510. }
  511. done:
  512. /*
  513. * We're done with our thread in secure world, if there's any
  514. * thread waiters wake up one.
  515. */
  516. optee_cq_wait_final(&optee->call_queue, &w);
  517. return rc;
  518. }
  519. /**
  520. * optee_ffa_do_call_with_arg() - Do a FF-A call to enter OP-TEE in secure world
  521. * @ctx: calling context
  522. * @shm: shared memory holding the message to pass to secure world
  523. * @offs: offset of the message in @shm
  524. * @system_thread: true if caller requests TEE system thread support
  525. *
  526. * Does a FF-A call to OP-TEE in secure world and handles eventual resulting
  527. * Remote Procedure Calls (RPC) from OP-TEE.
  528. *
  529. * Returns return code from FF-A, 0 is OK
  530. */
  531. static int optee_ffa_do_call_with_arg(struct tee_context *ctx,
  532. struct tee_shm *shm, u_int offs,
  533. bool system_thread)
  534. {
  535. struct ffa_send_direct_data data = {
  536. .data0 = OPTEE_FFA_YIELDING_CALL_WITH_ARG,
  537. .data1 = (u32)shm->sec_world_id,
  538. .data2 = (u32)(shm->sec_world_id >> 32),
  539. .data3 = offs,
  540. };
  541. struct optee_msg_arg *arg;
  542. unsigned int rpc_arg_offs;
  543. struct optee_msg_arg *rpc_arg;
  544. /*
  545. * The shared memory object has to start on a page when passed as
  546. * an argument struct. This is also what the shm pool allocator
  547. * returns, but check this before calling secure world to catch
  548. * eventual errors early in case something changes.
  549. */
  550. if (shm->offset)
  551. return -EINVAL;
  552. arg = tee_shm_get_va(shm, offs);
  553. if (IS_ERR(arg))
  554. return PTR_ERR(arg);
  555. rpc_arg_offs = OPTEE_MSG_GET_ARG_SIZE(arg->num_params);
  556. rpc_arg = tee_shm_get_va(shm, offs + rpc_arg_offs);
  557. if (IS_ERR(rpc_arg))
  558. return PTR_ERR(rpc_arg);
  559. return optee_ffa_yielding_call(ctx, &data, rpc_arg, system_thread);
  560. }
  561. /*
  562. * 6. Driver initialization
  563. *
  564. * During driver inititialization is the OP-TEE Secure Partition is probed
  565. * to find out which features it supports so the driver can be initialized
  566. * with a matching configuration.
  567. */
  568. static bool optee_ffa_api_is_compatible(struct ffa_device *ffa_dev,
  569. const struct ffa_ops *ops)
  570. {
  571. const struct ffa_msg_ops *msg_ops = ops->msg_ops;
  572. struct ffa_send_direct_data data = {
  573. .data0 = OPTEE_FFA_GET_API_VERSION,
  574. };
  575. int rc;
  576. msg_ops->mode_32bit_set(ffa_dev);
  577. rc = msg_ops->sync_send_receive(ffa_dev, &data);
  578. if (rc) {
  579. pr_err("Unexpected error %d\n", rc);
  580. return false;
  581. }
  582. if (data.data0 != OPTEE_FFA_VERSION_MAJOR ||
  583. data.data1 < OPTEE_FFA_VERSION_MINOR) {
  584. pr_err("Incompatible OP-TEE API version %lu.%lu",
  585. data.data0, data.data1);
  586. return false;
  587. }
  588. data = (struct ffa_send_direct_data){
  589. .data0 = OPTEE_FFA_GET_OS_VERSION,
  590. };
  591. rc = msg_ops->sync_send_receive(ffa_dev, &data);
  592. if (rc) {
  593. pr_err("Unexpected error %d\n", rc);
  594. return false;
  595. }
  596. if (data.data2)
  597. pr_info("revision %lu.%lu (%08lx)",
  598. data.data0, data.data1, data.data2);
  599. else
  600. pr_info("revision %lu.%lu", data.data0, data.data1);
  601. return true;
  602. }
  603. static bool optee_ffa_exchange_caps(struct ffa_device *ffa_dev,
  604. const struct ffa_ops *ops,
  605. u32 *sec_caps,
  606. unsigned int *rpc_param_count,
  607. unsigned int *max_notif_value)
  608. {
  609. struct ffa_send_direct_data data = {
  610. .data0 = OPTEE_FFA_EXCHANGE_CAPABILITIES,
  611. };
  612. int rc;
  613. rc = ops->msg_ops->sync_send_receive(ffa_dev, &data);
  614. if (rc) {
  615. pr_err("Unexpected error %d", rc);
  616. return false;
  617. }
  618. if (data.data0) {
  619. pr_err("Unexpected exchange error %lu", data.data0);
  620. return false;
  621. }
  622. *rpc_param_count = (u8)data.data1;
  623. *sec_caps = data.data2;
  624. if (data.data3)
  625. *max_notif_value = data.data3;
  626. else
  627. *max_notif_value = OPTEE_DEFAULT_MAX_NOTIF_VALUE;
  628. return true;
  629. }
  630. static void notif_work_fn(struct work_struct *work)
  631. {
  632. struct optee_ffa *optee_ffa = container_of(work, struct optee_ffa,
  633. notif_work);
  634. struct optee *optee = container_of(optee_ffa, struct optee, ffa);
  635. optee_do_bottom_half(optee->ctx);
  636. }
  637. static void notif_callback(int notify_id, void *cb_data)
  638. {
  639. struct optee *optee = cb_data;
  640. if (notify_id == optee->ffa.bottom_half_value)
  641. queue_work(optee->ffa.notif_wq, &optee->ffa.notif_work);
  642. else
  643. optee_notif_send(optee, notify_id);
  644. }
  645. static int enable_async_notif(struct optee *optee)
  646. {
  647. struct ffa_device *ffa_dev = optee->ffa.ffa_dev;
  648. struct ffa_send_direct_data data = {
  649. .data0 = OPTEE_FFA_ENABLE_ASYNC_NOTIF,
  650. .data1 = optee->ffa.bottom_half_value,
  651. };
  652. int rc;
  653. rc = ffa_dev->ops->msg_ops->sync_send_receive(ffa_dev, &data);
  654. if (rc)
  655. return rc;
  656. return data.data0;
  657. }
  658. static void optee_ffa_get_version(struct tee_device *teedev,
  659. struct tee_ioctl_version_data *vers)
  660. {
  661. struct tee_ioctl_version_data v = {
  662. .impl_id = TEE_IMPL_ID_OPTEE,
  663. .impl_caps = TEE_OPTEE_CAP_TZ,
  664. .gen_caps = TEE_GEN_CAP_GP | TEE_GEN_CAP_REG_MEM |
  665. TEE_GEN_CAP_MEMREF_NULL,
  666. };
  667. *vers = v;
  668. }
  669. static int optee_ffa_open(struct tee_context *ctx)
  670. {
  671. return optee_open(ctx, true);
  672. }
  673. static const struct tee_driver_ops optee_ffa_clnt_ops = {
  674. .get_version = optee_ffa_get_version,
  675. .open = optee_ffa_open,
  676. .release = optee_release,
  677. .open_session = optee_open_session,
  678. .close_session = optee_close_session,
  679. .invoke_func = optee_invoke_func,
  680. .cancel_req = optee_cancel_req,
  681. .shm_register = optee_ffa_shm_register,
  682. .shm_unregister = optee_ffa_shm_unregister,
  683. };
  684. static const struct tee_desc optee_ffa_clnt_desc = {
  685. .name = DRIVER_NAME "-ffa-clnt",
  686. .ops = &optee_ffa_clnt_ops,
  687. .owner = THIS_MODULE,
  688. };
  689. static const struct tee_driver_ops optee_ffa_supp_ops = {
  690. .get_version = optee_ffa_get_version,
  691. .open = optee_ffa_open,
  692. .release = optee_release_supp,
  693. .supp_recv = optee_supp_recv,
  694. .supp_send = optee_supp_send,
  695. .shm_register = optee_ffa_shm_register, /* same as for clnt ops */
  696. .shm_unregister = optee_ffa_shm_unregister_supp,
  697. };
  698. static const struct tee_desc optee_ffa_supp_desc = {
  699. .name = DRIVER_NAME "-ffa-supp",
  700. .ops = &optee_ffa_supp_ops,
  701. .owner = THIS_MODULE,
  702. .flags = TEE_DESC_PRIVILEGED,
  703. };
  704. static const struct optee_ops optee_ffa_ops = {
  705. .do_call_with_arg = optee_ffa_do_call_with_arg,
  706. .to_msg_param = optee_ffa_to_msg_param,
  707. .from_msg_param = optee_ffa_from_msg_param,
  708. };
  709. static void optee_ffa_remove(struct ffa_device *ffa_dev)
  710. {
  711. struct optee *optee = ffa_dev_get_drvdata(ffa_dev);
  712. u32 bottom_half_id = optee->ffa.bottom_half_value;
  713. if (bottom_half_id != U32_MAX) {
  714. ffa_dev->ops->notifier_ops->notify_relinquish(ffa_dev,
  715. bottom_half_id);
  716. destroy_workqueue(optee->ffa.notif_wq);
  717. }
  718. optee_remove_common(optee);
  719. mutex_destroy(&optee->ffa.mutex);
  720. rhashtable_free_and_destroy(&optee->ffa.global_ids, rh_free_fn, NULL);
  721. kfree(optee);
  722. }
  723. static int optee_ffa_async_notif_init(struct ffa_device *ffa_dev,
  724. struct optee *optee)
  725. {
  726. bool is_per_vcpu = false;
  727. u32 notif_id = 0;
  728. int rc;
  729. INIT_WORK(&optee->ffa.notif_work, notif_work_fn);
  730. optee->ffa.notif_wq = create_workqueue("optee_notification");
  731. if (!optee->ffa.notif_wq) {
  732. rc = -EINVAL;
  733. goto err;
  734. }
  735. while (true) {
  736. rc = ffa_dev->ops->notifier_ops->notify_request(ffa_dev,
  737. is_per_vcpu,
  738. notif_callback,
  739. optee,
  740. notif_id);
  741. if (!rc)
  742. break;
  743. /*
  744. * -EACCES means that the notification ID was
  745. * already bound, try the next one as long as we
  746. * haven't reached the max. Any other error is a
  747. * permanent error, so skip asynchronous
  748. * notifications in that case.
  749. */
  750. if (rc != -EACCES)
  751. goto err_wq;
  752. notif_id++;
  753. if (notif_id >= OPTEE_FFA_MAX_ASYNC_NOTIF_VALUE)
  754. goto err_wq;
  755. }
  756. optee->ffa.bottom_half_value = notif_id;
  757. rc = enable_async_notif(optee);
  758. if (rc < 0)
  759. goto err_rel;
  760. return 0;
  761. err_rel:
  762. ffa_dev->ops->notifier_ops->notify_relinquish(ffa_dev, notif_id);
  763. err_wq:
  764. destroy_workqueue(optee->ffa.notif_wq);
  765. err:
  766. optee->ffa.bottom_half_value = U32_MAX;
  767. return rc;
  768. }
  769. static int optee_ffa_probe(struct ffa_device *ffa_dev)
  770. {
  771. const struct ffa_notifier_ops *notif_ops;
  772. const struct ffa_ops *ffa_ops;
  773. unsigned int max_notif_value;
  774. unsigned int rpc_param_count;
  775. struct tee_shm_pool *pool;
  776. struct tee_device *teedev;
  777. struct tee_context *ctx;
  778. u32 arg_cache_flags = 0;
  779. struct optee *optee;
  780. u32 sec_caps;
  781. int rc;
  782. ffa_ops = ffa_dev->ops;
  783. notif_ops = ffa_ops->notifier_ops;
  784. if (!optee_ffa_api_is_compatible(ffa_dev, ffa_ops))
  785. return -EINVAL;
  786. if (!optee_ffa_exchange_caps(ffa_dev, ffa_ops, &sec_caps,
  787. &rpc_param_count, &max_notif_value))
  788. return -EINVAL;
  789. if (sec_caps & OPTEE_FFA_SEC_CAP_ARG_OFFSET)
  790. arg_cache_flags |= OPTEE_SHM_ARG_SHARED;
  791. optee = kzalloc(sizeof(*optee), GFP_KERNEL);
  792. if (!optee)
  793. return -ENOMEM;
  794. pool = optee_ffa_shm_pool_alloc_pages();
  795. if (IS_ERR(pool)) {
  796. rc = PTR_ERR(pool);
  797. goto err_free_optee;
  798. }
  799. optee->pool = pool;
  800. optee->ops = &optee_ffa_ops;
  801. optee->ffa.ffa_dev = ffa_dev;
  802. optee->ffa.bottom_half_value = U32_MAX;
  803. optee->rpc_param_count = rpc_param_count;
  804. if (IS_REACHABLE(CONFIG_RPMB) &&
  805. (sec_caps & OPTEE_FFA_SEC_CAP_RPMB_PROBE))
  806. optee->in_kernel_rpmb_routing = true;
  807. teedev = tee_device_alloc(&optee_ffa_clnt_desc, NULL, optee->pool,
  808. optee);
  809. if (IS_ERR(teedev)) {
  810. rc = PTR_ERR(teedev);
  811. goto err_free_pool;
  812. }
  813. optee->teedev = teedev;
  814. teedev = tee_device_alloc(&optee_ffa_supp_desc, NULL, optee->pool,
  815. optee);
  816. if (IS_ERR(teedev)) {
  817. rc = PTR_ERR(teedev);
  818. goto err_unreg_teedev;
  819. }
  820. optee->supp_teedev = teedev;
  821. optee_set_dev_group(optee);
  822. rc = tee_device_register(optee->teedev);
  823. if (rc)
  824. goto err_unreg_supp_teedev;
  825. rc = tee_device_register(optee->supp_teedev);
  826. if (rc)
  827. goto err_unreg_supp_teedev;
  828. rc = rhashtable_init(&optee->ffa.global_ids, &shm_rhash_params);
  829. if (rc)
  830. goto err_unreg_supp_teedev;
  831. mutex_init(&optee->ffa.mutex);
  832. optee_cq_init(&optee->call_queue, 0);
  833. optee_supp_init(&optee->supp);
  834. optee_shm_arg_cache_init(optee, arg_cache_flags);
  835. mutex_init(&optee->rpmb_dev_mutex);
  836. ffa_dev_set_drvdata(ffa_dev, optee);
  837. ctx = teedev_open(optee->teedev);
  838. if (IS_ERR(ctx)) {
  839. rc = PTR_ERR(ctx);
  840. goto err_rhashtable_free;
  841. }
  842. optee->ctx = ctx;
  843. rc = optee_notif_init(optee, OPTEE_DEFAULT_MAX_NOTIF_VALUE);
  844. if (rc)
  845. goto err_close_ctx;
  846. if (sec_caps & OPTEE_FFA_SEC_CAP_ASYNC_NOTIF) {
  847. rc = optee_ffa_async_notif_init(ffa_dev, optee);
  848. if (rc < 0)
  849. pr_err("Failed to initialize async notifications: %d",
  850. rc);
  851. }
  852. rc = optee_enumerate_devices(PTA_CMD_GET_DEVICES);
  853. if (rc)
  854. goto err_unregister_devices;
  855. INIT_WORK(&optee->rpmb_scan_bus_work, optee_bus_scan_rpmb);
  856. optee->rpmb_intf.notifier_call = optee_rpmb_intf_rdev;
  857. blocking_notifier_chain_register(&optee_rpmb_intf_added,
  858. &optee->rpmb_intf);
  859. pr_info("initialized driver\n");
  860. return 0;
  861. err_unregister_devices:
  862. optee_unregister_devices();
  863. if (optee->ffa.bottom_half_value != U32_MAX)
  864. notif_ops->notify_relinquish(ffa_dev,
  865. optee->ffa.bottom_half_value);
  866. optee_notif_uninit(optee);
  867. err_close_ctx:
  868. teedev_close_context(ctx);
  869. err_rhashtable_free:
  870. rhashtable_free_and_destroy(&optee->ffa.global_ids, rh_free_fn, NULL);
  871. rpmb_dev_put(optee->rpmb_dev);
  872. mutex_destroy(&optee->rpmb_dev_mutex);
  873. optee_supp_uninit(&optee->supp);
  874. mutex_destroy(&optee->call_queue.mutex);
  875. mutex_destroy(&optee->ffa.mutex);
  876. err_unreg_supp_teedev:
  877. tee_device_unregister(optee->supp_teedev);
  878. err_unreg_teedev:
  879. tee_device_unregister(optee->teedev);
  880. err_free_pool:
  881. tee_shm_pool_free(pool);
  882. err_free_optee:
  883. kfree(optee);
  884. return rc;
  885. }
  886. static const struct ffa_device_id optee_ffa_device_id[] = {
  887. /* 486178e0-e7f8-11e3-bc5e0002a5d5c51b */
  888. { UUID_INIT(0x486178e0, 0xe7f8, 0x11e3,
  889. 0xbc, 0x5e, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b) },
  890. {}
  891. };
  892. static struct ffa_driver optee_ffa_driver = {
  893. .name = "optee",
  894. .probe = optee_ffa_probe,
  895. .remove = optee_ffa_remove,
  896. .id_table = optee_ffa_device_id,
  897. };
  898. int optee_ffa_abi_register(void)
  899. {
  900. if (IS_REACHABLE(CONFIG_ARM_FFA_TRANSPORT))
  901. return ffa_register(&optee_ffa_driver);
  902. else
  903. return -EOPNOTSUPP;
  904. }
  905. void optee_ffa_abi_unregister(void)
  906. {
  907. if (IS_REACHABLE(CONFIG_ARM_FFA_TRANSPORT))
  908. ffa_unregister(&optee_ffa_driver);
  909. }