blob: 089d1b6abed1f7e0b0f6ebb4fb74bc26477cb0d4 [file] [log] [blame]
Patrick Williams2194f502022-10-16 14:26:09 -05001From 2e26d6cc12475b86cfc0a42d6c21146b5ce1fe4b Mon Sep 17 00:00:00 2001
Brad Bishopbec4ebc2022-08-03 09:55:16 -04002From: Jens Wiklander <jens.wiklander@linaro.org>
Patrick Williams2194f502022-10-16 14:26:09 -05003Date: Wed, 21 Jul 2021 17:45:21 +0200
4Subject: [PATCH 12/40] optee: add FF-A support
Brad Bishopbec4ebc2022-08-03 09:55:16 -04005
6Adds support for using FF-A [1] as transport to the OP-TEE driver.
7
8Introduces struct optee_msg_param_fmem which carries all information
9needed when OP-TEE is calling FFA_MEM_RETRIEVE_REQ to get the shared
10memory reference mapped by the hypervisor in S-EL2. Register usage is
11also updated to include the information needed.
12
13The FF-A part of this driver is enabled if CONFIG_ARM_FFA_TRANSPORT is
14enabled.
15
16[1] https://developer.arm.com/documentation/den0077/latest
Patrick Williams2194f502022-10-16 14:26:09 -050017Acked-by: Sumit Garg <sumit.garg@linaro.org>
Brad Bishopbec4ebc2022-08-03 09:55:16 -040018Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
Brad Bishopbec4ebc2022-08-03 09:55:16 -040019Upstream-Status: Pending [Not submitted to upstream yet]
Patrick Williams2194f502022-10-16 14:26:09 -050020Signed-off-by: Rupinderjit Singh <rupinderjit.singh@arm.com>
Brad Bishopbec4ebc2022-08-03 09:55:16 -040021---
Patrick Williams2194f502022-10-16 14:26:09 -050022 drivers/tee/optee/Makefile | 1 +
23 drivers/tee/optee/call.c | 13 +-
24 drivers/tee/optee/core.c | 16 +-
25 drivers/tee/optee/ffa_abi.c | 911 ++++++++++++++++++++++++++++++
26 drivers/tee/optee/optee_ffa.h | 153 +++++
Brad Bishopbec4ebc2022-08-03 09:55:16 -040027 drivers/tee/optee/optee_msg.h | 27 +-
Patrick Williams2194f502022-10-16 14:26:09 -050028 drivers/tee/optee/optee_private.h | 35 +-
29 7 files changed, 1143 insertions(+), 13 deletions(-)
30 create mode 100644 drivers/tee/optee/ffa_abi.c
Brad Bishopbec4ebc2022-08-03 09:55:16 -040031 create mode 100644 drivers/tee/optee/optee_ffa.h
32
Patrick Williams2194f502022-10-16 14:26:09 -050033diff --git a/drivers/tee/optee/Makefile b/drivers/tee/optee/Makefile
34index 97ac3ab3e1c0..66b8a17f14c4 100644
35--- a/drivers/tee/optee/Makefile
36+++ b/drivers/tee/optee/Makefile
37@@ -6,6 +6,7 @@ optee-objs += rpc.o
38 optee-objs += supp.o
39 optee-objs += device.o
40 optee-objs += smc_abi.o
41+optee-objs += ffa_abi.o
42
43 # for tracing framework to find optee_trace.h
44 CFLAGS_smc_abi.o := -I$(src)
Brad Bishopbec4ebc2022-08-03 09:55:16 -040045diff --git a/drivers/tee/optee/call.c b/drivers/tee/optee/call.c
Patrick Williams2194f502022-10-16 14:26:09 -050046index 9ff4f0812825..b25cc1fac945 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -040047--- a/drivers/tee/optee/call.c
48+++ b/drivers/tee/optee/call.c
Patrick Williams2194f502022-10-16 14:26:09 -050049@@ -107,11 +107,20 @@ static struct optee_session *find_session(struct optee_context_data *ctxdata,
50 struct tee_shm *optee_get_msg_arg(struct tee_context *ctx, size_t num_params,
51 struct optee_msg_arg **msg_arg)
Brad Bishopbec4ebc2022-08-03 09:55:16 -040052 {
53+ struct optee *optee = tee_get_drvdata(ctx->teedev);
54+ size_t sz = OPTEE_MSG_GET_ARG_SIZE(num_params);
55 struct tee_shm *shm;
56 struct optee_msg_arg *ma;
57
58- shm = tee_shm_alloc(ctx, OPTEE_MSG_GET_ARG_SIZE(num_params),
Patrick Williams2194f502022-10-16 14:26:09 -050059- TEE_SHM_MAPPED | TEE_SHM_PRIV);
Brad Bishopbec4ebc2022-08-03 09:55:16 -040060+ /*
61+ * rpc_arg_count is set to the number of allocated parameters in
62+ * the RPC argument struct if a second MSG arg struct is expected.
Patrick Williams2194f502022-10-16 14:26:09 -050063+ * The second arg struct will then be used for RPC.
Brad Bishopbec4ebc2022-08-03 09:55:16 -040064+ */
65+ if (optee->rpc_arg_count)
66+ sz += OPTEE_MSG_GET_ARG_SIZE(optee->rpc_arg_count);
67+
Patrick Williams2194f502022-10-16 14:26:09 -050068+ shm = tee_shm_alloc(ctx, sz, TEE_SHM_MAPPED | TEE_SHM_PRIV);
Brad Bishopbec4ebc2022-08-03 09:55:16 -040069 if (IS_ERR(shm))
70 return shm;
71
Patrick Williams2194f502022-10-16 14:26:09 -050072diff --git a/drivers/tee/optee/core.c b/drivers/tee/optee/core.c
73index 27b855325b33..ab2edfcc6c70 100644
74--- a/drivers/tee/optee/core.c
75+++ b/drivers/tee/optee/core.c
76@@ -172,6 +172,9 @@ void optee_remove_common(struct optee *optee)
77 mutex_destroy(&optee->call_queue.mutex);
Brad Bishopbec4ebc2022-08-03 09:55:16 -040078 }
Patrick Williams2194f502022-10-16 14:26:09 -050079
80+static int smc_abi_rc;
81+static int ffa_abi_rc;
Brad Bishopbec4ebc2022-08-03 09:55:16 -040082+
Patrick Williams2194f502022-10-16 14:26:09 -050083 static int optee_core_init(void)
84 {
85 /*
86@@ -184,13 +187,22 @@ static int optee_core_init(void)
87 if (is_kdump_kernel())
88 return -ENODEV;
89
90- return optee_smc_abi_register();
91+ smc_abi_rc = optee_smc_abi_register();
92+ ffa_abi_rc = optee_ffa_abi_register();
93+
94+ /* If both failed there's no point with this module */
95+ if (smc_abi_rc && ffa_abi_rc)
96+ return smc_abi_rc;
97+ return 0;
98 }
99 module_init(optee_core_init);
100
101 static void optee_core_exit(void)
102 {
103- optee_smc_abi_unregister();
104+ if (!smc_abi_rc)
105+ optee_smc_abi_unregister();
106+ if (!ffa_abi_rc)
107+ optee_ffa_abi_unregister();
108 }
109 module_exit(optee_core_exit);
110
111diff --git a/drivers/tee/optee/ffa_abi.c b/drivers/tee/optee/ffa_abi.c
112new file mode 100644
113index 000000000000..6defd1ec982a
114--- /dev/null
115+++ b/drivers/tee/optee/ffa_abi.c
116@@ -0,0 +1,911 @@
117+// SPDX-License-Identifier: GPL-2.0-only
118+/*
119+ * Copyright (c) 2021, Linaro Limited
120+ */
121+
122+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
123+
124+#include <linux/arm_ffa.h>
125+#include <linux/errno.h>
126+#include <linux/scatterlist.h>
127+#include <linux/sched.h>
128+#include <linux/slab.h>
129+#include <linux/string.h>
130+#include <linux/tee_drv.h>
131+#include <linux/types.h>
132+#include "optee_private.h"
133+#include "optee_ffa.h"
134+#include "optee_rpc_cmd.h"
135+
136+/*
137+ * This file implement the FF-A ABI used when communicating with secure world
138+ * OP-TEE OS via FF-A.
139+ * This file is divided into the following sections:
140+ * 1. Maintain a hash table for lookup of a global FF-A memory handle
141+ * 2. Convert between struct tee_param and struct optee_msg_param
142+ * 3. Low level support functions to register shared memory in secure world
143+ * 4. Dynamic shared memory pool based on alloc_pages()
144+ * 5. Do a normal scheduled call into secure world
145+ * 6. Driver initialization.
146+ */
147+
148+/*
149+ * 1. Maintain a hash table for lookup of a global FF-A memory handle
150+ *
151+ * FF-A assigns a global memory handle for each piece shared memory.
152+ * This handle is then used when communicating with secure world.
153+ *
154+ * Main functions are optee_shm_add_ffa_handle() and optee_shm_rem_ffa_handle()
155+ */
156+struct shm_rhash {
157+ struct tee_shm *shm;
158+ u64 global_id;
159+ struct rhash_head linkage;
160+};
161+
162+static void rh_free_fn(void *ptr, void *arg)
163+{
164+ kfree(ptr);
165+}
166+
167+static const struct rhashtable_params shm_rhash_params = {
168+ .head_offset = offsetof(struct shm_rhash, linkage),
169+ .key_len = sizeof(u64),
170+ .key_offset = offsetof(struct shm_rhash, global_id),
171+ .automatic_shrinking = true,
172+};
173+
174+static struct tee_shm *optee_shm_from_ffa_handle(struct optee *optee,
175+ u64 global_id)
176+{
177+ struct tee_shm *shm = NULL;
178+ struct shm_rhash *r;
179+
180+ mutex_lock(&optee->ffa.mutex);
181+ r = rhashtable_lookup_fast(&optee->ffa.global_ids, &global_id,
182+ shm_rhash_params);
183+ if (r)
184+ shm = r->shm;
185+ mutex_unlock(&optee->ffa.mutex);
186+
187+ return shm;
188+}
189+
190+static int optee_shm_add_ffa_handle(struct optee *optee, struct tee_shm *shm,
191+ u64 global_id)
192+{
193+ struct shm_rhash *r;
194+ int rc;
195+
196+ r = kmalloc(sizeof(*r), GFP_KERNEL);
197+ if (!r)
198+ return -ENOMEM;
199+ r->shm = shm;
200+ r->global_id = global_id;
201+
202+ mutex_lock(&optee->ffa.mutex);
203+ rc = rhashtable_lookup_insert_fast(&optee->ffa.global_ids, &r->linkage,
204+ shm_rhash_params);
205+ mutex_unlock(&optee->ffa.mutex);
206+
207+ if (rc)
208+ kfree(r);
209+
210+ return rc;
211+}
212+
213+static int optee_shm_rem_ffa_handle(struct optee *optee, u64 global_id)
214+{
215+ struct shm_rhash *r;
216+ int rc = -ENOENT;
217+
218+ mutex_lock(&optee->ffa.mutex);
219+ r = rhashtable_lookup_fast(&optee->ffa.global_ids, &global_id,
220+ shm_rhash_params);
221+ if (r)
222+ rc = rhashtable_remove_fast(&optee->ffa.global_ids,
223+ &r->linkage, shm_rhash_params);
224+ mutex_unlock(&optee->ffa.mutex);
225+
226+ if (!rc)
227+ kfree(r);
228+
229+ return rc;
230+}
231+
232+/*
233+ * 2. Convert between struct tee_param and struct optee_msg_param
234+ *
235+ * optee_ffa_from_msg_param() and optee_ffa_to_msg_param() are the main
236+ * functions.
237+ */
238+
239+static void from_msg_param_ffa_mem(struct optee *optee, struct tee_param *p,
240+ u32 attr, const struct optee_msg_param *mp)
241+{
242+ struct tee_shm *shm = NULL;
243+ u64 offs_high = 0;
244+ u64 offs_low = 0;
245+
246+ p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT +
247+ attr - OPTEE_MSG_ATTR_TYPE_FMEM_INPUT;
248+ p->u.memref.size = mp->u.fmem.size;
249+
250+ if (mp->u.fmem.global_id != OPTEE_MSG_FMEM_INVALID_GLOBAL_ID)
251+ shm = optee_shm_from_ffa_handle(optee, mp->u.fmem.global_id);
252+ p->u.memref.shm = shm;
253+
254+ if (shm) {
255+ offs_low = mp->u.fmem.offs_low;
256+ offs_high = mp->u.fmem.offs_high;
257+ }
258+ p->u.memref.shm_offs = offs_low | offs_high << 32;
259+}
260+
261+/**
262+ * optee_ffa_from_msg_param() - convert from OPTEE_MSG parameters to
263+ * struct tee_param
264+ * @optee: main service struct
265+ * @params: subsystem internal parameter representation
266+ * @num_params: number of elements in the parameter arrays
267+ * @msg_params: OPTEE_MSG parameters
268+ *
269+ * Returns 0 on success or <0 on failure
270+ */
271+static int optee_ffa_from_msg_param(struct optee *optee,
272+ struct tee_param *params, size_t num_params,
273+ const struct optee_msg_param *msg_params)
274+{
275+ size_t n;
276+
277+ for (n = 0; n < num_params; n++) {
278+ struct tee_param *p = params + n;
279+ const struct optee_msg_param *mp = msg_params + n;
280+ u32 attr = mp->attr & OPTEE_MSG_ATTR_TYPE_MASK;
281+
282+ switch (attr) {
283+ case OPTEE_MSG_ATTR_TYPE_NONE:
284+ p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
285+ memset(&p->u, 0, sizeof(p->u));
286+ break;
287+ case OPTEE_MSG_ATTR_TYPE_VALUE_INPUT:
288+ case OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT:
289+ case OPTEE_MSG_ATTR_TYPE_VALUE_INOUT:
290+ optee_from_msg_param_value(p, attr, mp);
291+ break;
292+ case OPTEE_MSG_ATTR_TYPE_FMEM_INPUT:
293+ case OPTEE_MSG_ATTR_TYPE_FMEM_OUTPUT:
294+ case OPTEE_MSG_ATTR_TYPE_FMEM_INOUT:
295+ from_msg_param_ffa_mem(optee, p, attr, mp);
296+ break;
297+ default:
298+ return -EINVAL;
299+ }
300+ }
301+
302+ return 0;
303+}
304+
305+static int to_msg_param_ffa_mem(struct optee_msg_param *mp,
306+ const struct tee_param *p)
307+{
308+ struct tee_shm *shm = p->u.memref.shm;
309+
310+ mp->attr = OPTEE_MSG_ATTR_TYPE_FMEM_INPUT + p->attr -
311+ TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
312+
313+ if (shm) {
314+ u64 shm_offs = p->u.memref.shm_offs;
315+
316+ mp->u.fmem.internal_offs = shm->offset;
317+
318+ mp->u.fmem.offs_low = shm_offs;
319+ mp->u.fmem.offs_high = shm_offs >> 32;
320+ /* Check that the entire offset could be stored. */
321+ if (mp->u.fmem.offs_high != shm_offs >> 32)
322+ return -EINVAL;
323+
324+ mp->u.fmem.global_id = shm->sec_world_id;
325+ } else {
326+ memset(&mp->u, 0, sizeof(mp->u));
327+ mp->u.fmem.global_id = OPTEE_MSG_FMEM_INVALID_GLOBAL_ID;
328+ }
329+ mp->u.fmem.size = p->u.memref.size;
330+
331+ return 0;
332+}
333+
334+/**
335+ * optee_ffa_to_msg_param() - convert from struct tee_params to OPTEE_MSG
336+ * parameters
337+ * @optee: main service struct
338+ * @msg_params: OPTEE_MSG parameters
339+ * @num_params: number of elements in the parameter arrays
340+ * @params: subsystem itnernal parameter representation
341+ * Returns 0 on success or <0 on failure
342+ */
343+static int optee_ffa_to_msg_param(struct optee *optee,
344+ struct optee_msg_param *msg_params,
345+ size_t num_params,
346+ const struct tee_param *params)
347+{
348+ size_t n;
349+
350+ for (n = 0; n < num_params; n++) {
351+ const struct tee_param *p = params + n;
352+ struct optee_msg_param *mp = msg_params + n;
353+
354+ switch (p->attr) {
355+ case TEE_IOCTL_PARAM_ATTR_TYPE_NONE:
356+ mp->attr = TEE_IOCTL_PARAM_ATTR_TYPE_NONE;
357+ memset(&mp->u, 0, sizeof(mp->u));
358+ break;
359+ case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT:
360+ case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT:
361+ case TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT:
362+ optee_to_msg_param_value(mp, p);
363+ break;
364+ case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
365+ case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
366+ case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
367+ if (to_msg_param_ffa_mem(mp, p))
368+ return -EINVAL;
369+ break;
370+ default:
371+ return -EINVAL;
372+ }
373+ }
374+
375+ return 0;
376+}
377+
378+/*
379+ * 3. Low level support functions to register shared memory in secure world
380+ *
381+ * Functions to register and unregister shared memory both for normal
382+ * clients and for tee-supplicant.
383+ */
384+
385+static int optee_ffa_shm_register(struct tee_context *ctx, struct tee_shm *shm,
386+ struct page **pages, size_t num_pages,
387+ unsigned long start)
388+{
389+ struct optee *optee = tee_get_drvdata(ctx->teedev);
390+ const struct ffa_dev_ops *ffa_ops = optee->ffa.ffa_ops;
391+ struct ffa_device *ffa_dev = optee->ffa.ffa_dev;
392+ struct ffa_mem_region_attributes mem_attr = {
393+ .receiver = ffa_dev->vm_id,
394+ .attrs = FFA_MEM_RW,
395+ };
396+ struct ffa_mem_ops_args args = {
397+ .use_txbuf = true,
398+ .attrs = &mem_attr,
399+ .nattrs = 1,
400+ };
401+ struct sg_table sgt;
402+ int rc;
403+
404+ rc = optee_check_mem_type(start, num_pages);
405+ if (rc)
406+ return rc;
407+
408+ rc = sg_alloc_table_from_pages(&sgt, pages, num_pages, 0,
409+ num_pages * PAGE_SIZE, GFP_KERNEL);
410+ if (rc)
411+ return rc;
412+ args.sg = sgt.sgl;
413+ rc = ffa_ops->memory_share(ffa_dev, &args);
414+ sg_free_table(&sgt);
415+ if (rc)
416+ return rc;
417+
418+ rc = optee_shm_add_ffa_handle(optee, shm, args.g_handle);
419+ if (rc) {
420+ ffa_ops->memory_reclaim(args.g_handle, 0);
421+ return rc;
422+ }
423+
424+ shm->sec_world_id = args.g_handle;
425+
426+ return 0;
427+}
428+
429+static int optee_ffa_shm_unregister(struct tee_context *ctx,
430+ struct tee_shm *shm)
431+{
432+ struct optee *optee = tee_get_drvdata(ctx->teedev);
433+ const struct ffa_dev_ops *ffa_ops = optee->ffa.ffa_ops;
434+ struct ffa_device *ffa_dev = optee->ffa.ffa_dev;
435+ u64 global_handle = shm->sec_world_id;
436+ struct ffa_send_direct_data data = {
437+ .data0 = OPTEE_FFA_UNREGISTER_SHM,
438+ .data1 = (u32)global_handle,
439+ .data2 = (u32)(global_handle >> 32)
440+ };
441+ int rc;
442+
443+ optee_shm_rem_ffa_handle(optee, global_handle);
444+ shm->sec_world_id = 0;
445+
446+ rc = ffa_ops->sync_send_receive(ffa_dev, &data);
447+ if (rc)
448+ pr_err("Unregister SHM id 0x%llx rc %d\n", global_handle, rc);
449+
450+ rc = ffa_ops->memory_reclaim(global_handle, 0);
451+ if (rc)
452+ pr_err("mem_reclain: 0x%llx %d", global_handle, rc);
453+
454+ return rc;
455+}
456+
457+static int optee_ffa_shm_unregister_supp(struct tee_context *ctx,
458+ struct tee_shm *shm)
459+{
460+ struct optee *optee = tee_get_drvdata(ctx->teedev);
461+ const struct ffa_dev_ops *ffa_ops = optee->ffa.ffa_ops;
462+ u64 global_handle = shm->sec_world_id;
463+ int rc;
464+
465+ /*
466+ * We're skipping the OPTEE_FFA_YIELDING_CALL_UNREGISTER_SHM call
467+ * since this is OP-TEE freeing via RPC so it has already retired
468+ * this ID.
469+ */
470+
471+ optee_shm_rem_ffa_handle(optee, global_handle);
472+ rc = ffa_ops->memory_reclaim(global_handle, 0);
473+ if (rc)
474+ pr_err("mem_reclain: 0x%llx %d", global_handle, rc);
475+
476+ shm->sec_world_id = 0;
477+
478+ return rc;
479+}
480+
481+/*
482+ * 4. Dynamic shared memory pool based on alloc_pages()
483+ *
484+ * Implements an OP-TEE specific shared memory pool.
485+ * The main function is optee_ffa_shm_pool_alloc_pages().
486+ */
487+
488+static int pool_ffa_op_alloc(struct tee_shm_pool_mgr *poolm,
489+ struct tee_shm *shm, size_t size)
490+{
491+ return optee_pool_op_alloc_helper(poolm, shm, size,
492+ optee_ffa_shm_register);
493+}
494+
495+static void pool_ffa_op_free(struct tee_shm_pool_mgr *poolm,
496+ struct tee_shm *shm)
497+{
498+ optee_ffa_shm_unregister(shm->ctx, shm);
499+ free_pages((unsigned long)shm->kaddr, get_order(shm->size));
500+ shm->kaddr = NULL;
501+}
502+
503+static void pool_ffa_op_destroy_poolmgr(struct tee_shm_pool_mgr *poolm)
504+{
505+ kfree(poolm);
506+}
507+
508+static const struct tee_shm_pool_mgr_ops pool_ffa_ops = {
509+ .alloc = pool_ffa_op_alloc,
510+ .free = pool_ffa_op_free,
511+ .destroy_poolmgr = pool_ffa_op_destroy_poolmgr,
512+};
513+
514+/**
515+ * optee_ffa_shm_pool_alloc_pages() - create page-based allocator pool
516+ *
517+ * This pool is used with OP-TEE over FF-A. In this case command buffers
518+ * and such are allocated from kernel's own memory.
519+ */
520+static struct tee_shm_pool_mgr *optee_ffa_shm_pool_alloc_pages(void)
521+{
522+ struct tee_shm_pool_mgr *mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
523+
524+ if (!mgr)
525+ return ERR_PTR(-ENOMEM);
526+
527+ mgr->ops = &pool_ffa_ops;
528+
529+ return mgr;
530+}
531+
532+/*
533+ * 5. Do a normal scheduled call into secure world
534+ *
535+ * The function optee_ffa_do_call_with_arg() performs a normal scheduled
536+ * call into secure world. During this call may normal world request help
537+ * from normal world using RPCs, Remote Procedure Calls. This includes
538+ * delivery of non-secure interrupts to for instance allow rescheduling of
539+ * the current task.
540+ */
541+
542+static void handle_ffa_rpc_func_cmd_shm_alloc(struct tee_context *ctx,
543+ struct optee_msg_arg *arg)
544+{
545+ struct tee_shm *shm;
546+
547+ if (arg->num_params != 1 ||
548+ arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT) {
549+ arg->ret = TEEC_ERROR_BAD_PARAMETERS;
550+ return;
551+ }
552+
553+ switch (arg->params[0].u.value.a) {
554+ case OPTEE_RPC_SHM_TYPE_APPL:
555+ shm = optee_rpc_cmd_alloc_suppl(ctx, arg->params[0].u.value.b);
556+ break;
557+ case OPTEE_RPC_SHM_TYPE_KERNEL:
558+ shm = tee_shm_alloc(ctx, arg->params[0].u.value.b,
559+ TEE_SHM_MAPPED | TEE_SHM_PRIV);
560+ break;
561+ default:
562+ arg->ret = TEEC_ERROR_BAD_PARAMETERS;
563+ return;
564+ }
565+
566+ if (IS_ERR(shm)) {
567+ arg->ret = TEEC_ERROR_OUT_OF_MEMORY;
568+ return;
569+ }
570+
571+ arg->params[0] = (struct optee_msg_param){
572+ .attr = OPTEE_MSG_ATTR_TYPE_FMEM_OUTPUT,
573+ .u.fmem.size = tee_shm_get_size(shm),
574+ .u.fmem.global_id = shm->sec_world_id,
575+ .u.fmem.internal_offs = shm->offset,
576+ };
577+
578+ arg->ret = TEEC_SUCCESS;
579+}
580+
581+static void handle_ffa_rpc_func_cmd_shm_free(struct tee_context *ctx,
582+ struct optee *optee,
583+ struct optee_msg_arg *arg)
584+{
585+ struct tee_shm *shm;
586+
587+ if (arg->num_params != 1 ||
588+ arg->params[0].attr != OPTEE_MSG_ATTR_TYPE_VALUE_INPUT)
589+ goto err_bad_param;
590+
591+ shm = optee_shm_from_ffa_handle(optee, arg->params[0].u.value.b);
592+ if (!shm)
593+ goto err_bad_param;
594+ switch (arg->params[0].u.value.a) {
595+ case OPTEE_RPC_SHM_TYPE_APPL:
596+ optee_rpc_cmd_free_suppl(ctx, shm);
597+ break;
598+ case OPTEE_RPC_SHM_TYPE_KERNEL:
599+ tee_shm_free(shm);
600+ break;
601+ default:
602+ goto err_bad_param;
603+ }
604+ arg->ret = TEEC_SUCCESS;
605+ return;
606+
607+err_bad_param:
608+ arg->ret = TEEC_ERROR_BAD_PARAMETERS;
609+}
610+
611+static void handle_ffa_rpc_func_cmd(struct tee_context *ctx,
612+ struct optee_msg_arg *arg)
613+{
614+ struct optee *optee = tee_get_drvdata(ctx->teedev);
615+
616+ arg->ret_origin = TEEC_ORIGIN_COMMS;
617+ switch (arg->cmd) {
618+ case OPTEE_RPC_CMD_SHM_ALLOC:
619+ handle_ffa_rpc_func_cmd_shm_alloc(ctx, arg);
620+ break;
621+ case OPTEE_RPC_CMD_SHM_FREE:
622+ handle_ffa_rpc_func_cmd_shm_free(ctx, optee, arg);
623+ break;
624+ default:
625+ optee_rpc_cmd(ctx, optee, arg);
626+ }
627+}
628+
629+static void optee_handle_ffa_rpc(struct tee_context *ctx, u32 cmd,
630+ struct optee_msg_arg *arg)
631+{
632+ switch (cmd) {
633+ case OPTEE_FFA_YIELDING_CALL_RETURN_RPC_CMD:
634+ handle_ffa_rpc_func_cmd(ctx, arg);
635+ break;
636+ case OPTEE_FFA_YIELDING_CALL_RETURN_INTERRUPT:
637+ /* Interrupt delivered by now */
638+ break;
639+ default:
640+ pr_warn("Unknown RPC func 0x%x\n", cmd);
641+ break;
642+ }
643+}
644+
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400645+static int optee_ffa_yielding_call(struct tee_context *ctx,
646+ struct ffa_send_direct_data *data,
647+ struct optee_msg_arg *rpc_arg)
648+{
649+ struct optee *optee = tee_get_drvdata(ctx->teedev);
650+ const struct ffa_dev_ops *ffa_ops = optee->ffa.ffa_ops;
651+ struct ffa_device *ffa_dev = optee->ffa.ffa_dev;
652+ struct optee_call_waiter w;
653+ u32 cmd = data->data0;
654+ u32 w4 = data->data1;
655+ u32 w5 = data->data2;
656+ u32 w6 = data->data3;
657+ int rc;
658+
659+ /* Initialize waiter */
660+ optee_cq_wait_init(&optee->call_queue, &w);
661+ while (true) {
662+ rc = ffa_ops->sync_send_receive(ffa_dev, data);
663+ if (rc)
664+ goto done;
665+
666+ switch ((int)data->data0) {
667+ case TEEC_SUCCESS:
668+ break;
669+ case TEEC_ERROR_BUSY:
670+ if (cmd == OPTEE_FFA_YIELDING_CALL_RESUME) {
671+ rc = -EIO;
672+ goto done;
673+ }
674+
675+ /*
676+ * Out of threads in secure world, wait for a thread
677+ * become available.
678+ */
679+ optee_cq_wait_for_completion(&optee->call_queue, &w);
680+ data->data0 = cmd;
681+ data->data1 = w4;
682+ data->data2 = w5;
683+ data->data3 = w6;
684+ continue;
685+ default:
686+ rc = -EIO;
687+ goto done;
688+ }
689+
690+ if (data->data1 == OPTEE_FFA_YIELDING_CALL_RETURN_DONE)
691+ goto done;
692+
693+ /*
694+ * OP-TEE has returned with a RPC request.
695+ *
696+ * Note that data->data4 (passed in register w7) is already
697+ * filled in by ffa_ops->sync_send_receive() returning
698+ * above.
699+ */
700+ cond_resched();
701+ optee_handle_ffa_rpc(ctx, data->data1, rpc_arg);
702+ cmd = OPTEE_FFA_YIELDING_CALL_RESUME;
703+ data->data0 = cmd;
704+ data->data1 = 0;
705+ data->data2 = 0;
706+ data->data3 = 0;
707+ }
708+done:
709+ /*
710+ * We're done with our thread in secure world, if there's any
711+ * thread waiters wake up one.
712+ */
713+ optee_cq_wait_final(&optee->call_queue, &w);
714+
715+ return rc;
716+}
717+
718+/**
719+ * optee_ffa_do_call_with_arg() - Do a FF-A call to enter OP-TEE in secure world
720+ * @ctx: calling context
721+ * @shm: shared memory holding the message to pass to secure world
722+ *
723+ * Does a FF-A call to OP-TEE in secure world and handles eventual resulting
724+ * Remote Procedure Calls (RPC) from OP-TEE.
725+ *
726+ * Returns return code from FF-A, 0 is OK
727+ */
728+
Patrick Williams2194f502022-10-16 14:26:09 -0500729+static int optee_ffa_do_call_with_arg(struct tee_context *ctx,
730+ struct tee_shm *shm)
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400731+{
732+ struct ffa_send_direct_data data = {
733+ .data0 = OPTEE_FFA_YIELDING_CALL_WITH_ARG,
734+ .data1 = (u32)shm->sec_world_id,
735+ .data2 = (u32)(shm->sec_world_id >> 32),
736+ .data3 = shm->offset,
737+ };
738+ struct optee_msg_arg *arg = tee_shm_get_va(shm, 0);
739+ unsigned int rpc_arg_offs = OPTEE_MSG_GET_ARG_SIZE(arg->num_params);
740+ struct optee_msg_arg *rpc_arg = tee_shm_get_va(shm, rpc_arg_offs);
741+
742+ return optee_ffa_yielding_call(ctx, &data, rpc_arg);
743+}
744+
Patrick Williams2194f502022-10-16 14:26:09 -0500745+/*
746+ * 6. Driver initialization
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400747+ *
Patrick Williams2194f502022-10-16 14:26:09 -0500748+ * During driver inititialization is the OP-TEE Secure Partition is probed
749+ * to find out which features it supports so the driver can be initialized
750+ * with a matching configuration.
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400751+ */
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400752+
753+static bool optee_ffa_api_is_compatbile(struct ffa_device *ffa_dev,
754+ const struct ffa_dev_ops *ops)
755+{
756+ struct ffa_send_direct_data data = { OPTEE_FFA_GET_API_VERSION };
757+ int rc;
758+
759+ ops->mode_32bit_set(ffa_dev);
760+
761+ rc = ops->sync_send_receive(ffa_dev, &data);
762+ if (rc) {
763+ pr_err("Unexpected error %d\n", rc);
764+ return false;
765+ }
766+ if (data.data0 != OPTEE_FFA_VERSION_MAJOR ||
767+ data.data1 < OPTEE_FFA_VERSION_MINOR) {
768+ pr_err("Incompatible OP-TEE API version %lu.%lu",
769+ data.data0, data.data1);
770+ return false;
771+ }
772+
773+ data = (struct ffa_send_direct_data){ OPTEE_FFA_GET_OS_VERSION };
774+ rc = ops->sync_send_receive(ffa_dev, &data);
775+ if (rc) {
776+ pr_err("Unexpected error %d\n", rc);
777+ return false;
778+ }
779+ if (data.data2)
780+ pr_info("revision %lu.%lu (%08lx)",
781+ data.data0, data.data1, data.data2);
782+ else
783+ pr_info("revision %lu.%lu", data.data0, data.data1);
784+
785+ return true;
786+}
787+
788+static bool optee_ffa_exchange_caps(struct ffa_device *ffa_dev,
789+ const struct ffa_dev_ops *ops,
Patrick Williams2194f502022-10-16 14:26:09 -0500790+ unsigned int *rpc_arg_count)
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400791+{
792+ struct ffa_send_direct_data data = { OPTEE_FFA_EXCHANGE_CAPABILITIES };
793+ int rc;
794+
795+ rc = ops->sync_send_receive(ffa_dev, &data);
796+ if (rc) {
797+ pr_err("Unexpected error %d", rc);
798+ return false;
799+ }
800+ if (data.data0) {
801+ pr_err("Unexpected exchange error %lu", data.data0);
802+ return false;
803+ }
804+
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400805+ *rpc_arg_count = (u8)data.data1;
806+
807+ return true;
808+}
809+
810+static struct tee_shm_pool *optee_ffa_config_dyn_shm(void)
811+{
812+ struct tee_shm_pool_mgr *priv_mgr;
813+ struct tee_shm_pool_mgr *dmabuf_mgr;
814+ void *rc;
815+
816+ rc = optee_ffa_shm_pool_alloc_pages();
817+ if (IS_ERR(rc))
818+ return rc;
819+ priv_mgr = rc;
820+
821+ rc = optee_ffa_shm_pool_alloc_pages();
822+ if (IS_ERR(rc)) {
823+ tee_shm_pool_mgr_destroy(priv_mgr);
824+ return rc;
825+ }
826+ dmabuf_mgr = rc;
827+
828+ rc = tee_shm_pool_alloc(priv_mgr, dmabuf_mgr);
829+ if (IS_ERR(rc)) {
830+ tee_shm_pool_mgr_destroy(priv_mgr);
831+ tee_shm_pool_mgr_destroy(dmabuf_mgr);
832+ }
833+
834+ return rc;
835+}
836+
Patrick Williams2194f502022-10-16 14:26:09 -0500837+static void optee_ffa_get_version(struct tee_device *teedev,
838+ struct tee_ioctl_version_data *vers)
839+{
840+ struct tee_ioctl_version_data v = {
841+ .impl_id = TEE_IMPL_ID_OPTEE,
842+ .impl_caps = TEE_OPTEE_CAP_TZ,
843+ .gen_caps = TEE_GEN_CAP_GP | TEE_GEN_CAP_REG_MEM |
844+ TEE_GEN_CAP_MEMREF_NULL,
845+ };
846+
847+ *vers = v;
848+}
849+
850+static int optee_ffa_open(struct tee_context *ctx)
851+{
852+ return optee_open(ctx, true);
853+}
854+
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400855+static const struct tee_driver_ops optee_ffa_clnt_ops = {
856+ .get_version = optee_ffa_get_version,
Patrick Williams2194f502022-10-16 14:26:09 -0500857+ .open = optee_ffa_open,
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400858+ .release = optee_release,
859+ .open_session = optee_open_session,
860+ .close_session = optee_close_session,
861+ .invoke_func = optee_invoke_func,
862+ .cancel_req = optee_cancel_req,
863+ .shm_register = optee_ffa_shm_register,
864+ .shm_unregister = optee_ffa_shm_unregister,
865+};
866+
867+static const struct tee_desc optee_ffa_clnt_desc = {
Patrick Williams2194f502022-10-16 14:26:09 -0500868+ .name = DRIVER_NAME "-ffa-clnt",
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400869+ .ops = &optee_ffa_clnt_ops,
870+ .owner = THIS_MODULE,
871+};
872+
873+static const struct tee_driver_ops optee_ffa_supp_ops = {
874+ .get_version = optee_ffa_get_version,
Patrick Williams2194f502022-10-16 14:26:09 -0500875+ .open = optee_ffa_open,
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400876+ .release = optee_release_supp,
877+ .supp_recv = optee_supp_recv,
878+ .supp_send = optee_supp_send,
879+ .shm_register = optee_ffa_shm_register, /* same as for clnt ops */
880+ .shm_unregister = optee_ffa_shm_unregister_supp,
881+};
882+
883+static const struct tee_desc optee_ffa_supp_desc = {
Patrick Williams2194f502022-10-16 14:26:09 -0500884+ .name = DRIVER_NAME "-ffa-supp",
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400885+ .ops = &optee_ffa_supp_ops,
886+ .owner = THIS_MODULE,
887+ .flags = TEE_DESC_PRIVILEGED,
888+};
889+
890+static const struct optee_ops optee_ffa_ops = {
891+ .do_call_with_arg = optee_ffa_do_call_with_arg,
892+ .to_msg_param = optee_ffa_to_msg_param,
893+ .from_msg_param = optee_ffa_from_msg_param,
894+};
895+
896+static void optee_ffa_remove(struct ffa_device *ffa_dev)
897+{
Patrick Williams2194f502022-10-16 14:26:09 -0500898+ struct optee *optee = ffa_dev->dev.driver_data;
899+
900+ optee_remove_common(optee);
901+
902+ mutex_destroy(&optee->ffa.mutex);
903+ rhashtable_free_and_destroy(&optee->ffa.global_ids, rh_free_fn, NULL);
904+
905+ kfree(optee);
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400906+}
907+
908+static int optee_ffa_probe(struct ffa_device *ffa_dev)
909+{
910+ const struct ffa_dev_ops *ffa_ops;
911+ unsigned int rpc_arg_count;
912+ struct tee_device *teedev;
913+ struct optee *optee;
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400914+ int rc;
915+
916+ ffa_ops = ffa_dev_ops_get(ffa_dev);
917+ if (!ffa_ops) {
918+ pr_warn("failed \"method\" init: ffa\n");
919+ return -ENOENT;
920+ }
921+
922+ if (!optee_ffa_api_is_compatbile(ffa_dev, ffa_ops))
923+ return -EINVAL;
924+
Patrick Williams2194f502022-10-16 14:26:09 -0500925+ if (!optee_ffa_exchange_caps(ffa_dev, ffa_ops, &rpc_arg_count))
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400926+ return -EINVAL;
927+
928+ optee = kzalloc(sizeof(*optee), GFP_KERNEL);
929+ if (!optee) {
930+ rc = -ENOMEM;
931+ goto err;
932+ }
933+ optee->pool = optee_ffa_config_dyn_shm();
934+ if (IS_ERR(optee->pool)) {
935+ rc = PTR_ERR(optee->pool);
936+ optee->pool = NULL;
937+ goto err;
938+ }
939+
940+ optee->ops = &optee_ffa_ops;
941+ optee->ffa.ffa_dev = ffa_dev;
942+ optee->ffa.ffa_ops = ffa_ops;
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400943+ optee->rpc_arg_count = rpc_arg_count;
944+
945+ teedev = tee_device_alloc(&optee_ffa_clnt_desc, NULL, optee->pool,
946+ optee);
947+ if (IS_ERR(teedev)) {
948+ rc = PTR_ERR(teedev);
949+ goto err;
950+ }
951+ optee->teedev = teedev;
952+
953+ teedev = tee_device_alloc(&optee_ffa_supp_desc, NULL, optee->pool,
954+ optee);
955+ if (IS_ERR(teedev)) {
956+ rc = PTR_ERR(teedev);
957+ goto err;
958+ }
959+ optee->supp_teedev = teedev;
960+
961+ rc = tee_device_register(optee->teedev);
962+ if (rc)
963+ goto err;
964+
965+ rc = tee_device_register(optee->supp_teedev);
966+ if (rc)
967+ goto err;
968+
969+ rc = rhashtable_init(&optee->ffa.global_ids, &shm_rhash_params);
970+ if (rc)
971+ goto err;
972+ mutex_init(&optee->ffa.mutex);
973+ mutex_init(&optee->call_queue.mutex);
974+ INIT_LIST_HEAD(&optee->call_queue.waiters);
975+ optee_wait_queue_init(&optee->wait_queue);
976+ optee_supp_init(&optee->supp);
977+ ffa_dev_set_drvdata(ffa_dev, optee);
978+
Patrick Williams2194f502022-10-16 14:26:09 -0500979+ rc = optee_enumerate_devices(PTA_CMD_GET_DEVICES);
980+ if (rc) {
981+ optee_ffa_remove(ffa_dev);
982+ return rc;
983+ }
984+
Brad Bishopbec4ebc2022-08-03 09:55:16 -0400985+ pr_info("initialized driver\n");
986+ return 0;
987+err:
988+ /*
989+ * tee_device_unregister() is safe to call even if the
990+ * devices hasn't been registered with
991+ * tee_device_register() yet.
992+ */
993+ tee_device_unregister(optee->supp_teedev);
994+ tee_device_unregister(optee->teedev);
995+ if (optee->pool)
996+ tee_shm_pool_free(optee->pool);
997+ kfree(optee);
998+ return rc;
999+}
1000+
1001+static const struct ffa_device_id optee_ffa_device_id[] = {
1002+ /* 486178e0-e7f8-11e3-bc5e0002a5d5c51b */
1003+ { UUID_INIT(0x486178e0, 0xe7f8, 0x11e3,
1004+ 0xbc, 0x5e, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b) },
1005+ {}
1006+};
1007+
1008+static struct ffa_driver optee_ffa_driver = {
1009+ .name = "optee",
1010+ .probe = optee_ffa_probe,
1011+ .remove = optee_ffa_remove,
1012+ .id_table = optee_ffa_device_id,
1013+};
1014+
Patrick Williams2194f502022-10-16 14:26:09 -05001015+int optee_ffa_abi_register(void)
1016+{
1017+ if (IS_REACHABLE(CONFIG_ARM_FFA_TRANSPORT))
1018+ return ffa_register(&optee_ffa_driver);
1019+ else
1020+ return -EOPNOTSUPP;
1021+}
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001022+
Patrick Williams2194f502022-10-16 14:26:09 -05001023+void optee_ffa_abi_unregister(void)
1024+{
1025+ if (IS_REACHABLE(CONFIG_ARM_FFA_TRANSPORT))
1026+ ffa_unregister(&optee_ffa_driver);
1027+}
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001028diff --git a/drivers/tee/optee/optee_ffa.h b/drivers/tee/optee/optee_ffa.h
1029new file mode 100644
1030index 000000000000..ee3a03fc392c
1031--- /dev/null
1032+++ b/drivers/tee/optee/optee_ffa.h
1033@@ -0,0 +1,153 @@
1034+/* SPDX-License-Identifier: BSD-2-Clause */
1035+/*
1036+ * Copyright (c) 2019-2021, Linaro Limited
1037+ */
1038+
1039+/*
1040+ * This file is exported by OP-TEE and is kept in sync between secure world
1041+ * and normal world drivers. We're using ARM FF-A 1.0 specification.
1042+ */
1043+
1044+#ifndef __OPTEE_FFA_H
1045+#define __OPTEE_FFA_H
1046+
1047+#include <linux/arm_ffa.h>
1048+
1049+/*
1050+ * Normal world sends requests with FFA_MSG_SEND_DIRECT_REQ and
1051+ * responses are returned with FFA_MSG_SEND_DIRECT_RESP for normal
1052+ * messages.
1053+ *
1054+ * All requests with FFA_MSG_SEND_DIRECT_REQ and FFA_MSG_SEND_DIRECT_RESP
1055+ * are using the AArch32 SMC calling convention with register usage as
1056+ * defined in FF-A specification:
1057+ * w0: Function ID (0x8400006F or 0x84000070)
1058+ * w1: Source/Destination IDs
1059+ * w2: Reserved (MBZ)
1060+ * w3-w7: Implementation defined, free to be used below
1061+ */
1062+
1063+#define OPTEE_FFA_VERSION_MAJOR 1
1064+#define OPTEE_FFA_VERSION_MINOR 0
1065+
1066+#define OPTEE_FFA_BLOCKING_CALL(id) (id)
1067+#define OPTEE_FFA_YIELDING_CALL_BIT 31
1068+#define OPTEE_FFA_YIELDING_CALL(id) ((id) | BIT(OPTEE_FFA_YIELDING_CALL_BIT))
1069+
1070+/*
1071+ * Returns the API version implemented, currently follows the FF-A version.
1072+ * Call register usage:
1073+ * w3: Service ID, OPTEE_FFA_GET_API_VERSION
1074+ * w4-w7: Not used (MBZ)
1075+ *
1076+ * Return register usage:
1077+ * w3: OPTEE_FFA_VERSION_MAJOR
1078+ * w4: OPTEE_FFA_VERSION_MINOR
1079+ * w5-w7: Not used (MBZ)
1080+ */
1081+#define OPTEE_FFA_GET_API_VERSION OPTEE_FFA_BLOCKING_CALL(0)
1082+
1083+/*
1084+ * Returns the revision of OP-TEE.
1085+ *
1086+ * Used by non-secure world to figure out which version of the Trusted OS
1087+ * is installed. Note that the returned revision is the revision of the
1088+ * Trusted OS, not of the API.
1089+ *
1090+ * Call register usage:
1091+ * w3: Service ID, OPTEE_FFA_GET_OS_VERSION
1092+ * w4-w7: Unused (MBZ)
1093+ *
1094+ * Return register usage:
1095+ * w3: CFG_OPTEE_REVISION_MAJOR
1096+ * w4: CFG_OPTEE_REVISION_MINOR
1097+ * w5: TEE_IMPL_GIT_SHA1 (or zero if not supported)
1098+ */
1099+#define OPTEE_FFA_GET_OS_VERSION OPTEE_FFA_BLOCKING_CALL(1)
1100+
1101+/*
1102+ * Exchange capabilities between normal world and secure world.
1103+ *
1104+ * Currently there are no defined capabilities. When features are added new
1105+ * capabilities may be added.
1106+ *
1107+ * Call register usage:
1108+ * w3: Service ID, OPTEE_FFA_EXCHANGE_CAPABILITIES
1109+ * w4-w7: Note used (MBZ)
1110+ *
1111+ * Return register usage:
1112+ * w3: Error code, 0 on success
1113+ * w4: Bit[7:0]: Number of parameters needed for RPC to be supplied
1114+ * as the second MSG arg struct for
1115+ * OPTEE_FFA_YIELDING_CALL_WITH_ARG.
1116+ * Bit[31:8]: Reserved (MBZ)
1117+ * w5-w7: Note used (MBZ)
1118+ */
1119+#define OPTEE_FFA_EXCHANGE_CAPABILITIES OPTEE_FFA_BLOCKING_CALL(2)
1120+
1121+/*
1122+ * Unregister shared memory
1123+ *
1124+ * Call register usage:
1125+ * w3: Service ID, OPTEE_FFA_YIELDING_CALL_UNREGISTER_SHM
1126+ * w4: Shared memory handle, lower bits
1127+ * w5: Shared memory handle, higher bits
1128+ * w6-w7: Not used (MBZ)
1129+ *
1130+ * Return register usage:
1131+ * w3: Error code, 0 on success
1132+ * w4-w7: Note used (MBZ)
1133+ */
1134+#define OPTEE_FFA_UNREGISTER_SHM OPTEE_FFA_BLOCKING_CALL(3)
1135+
1136+/*
1137+ * Call with struct optee_msg_arg as argument in the supplied shared memory
1138+ * with a zero internal offset and normal cached memory attributes.
1139+ * Register usage:
1140+ * w3: Service ID, OPTEE_FFA_YIELDING_CALL_WITH_ARG
1141+ * w4: Lower 32 bits of a 64-bit Shared memory handle
1142+ * w5: Upper 32 bits of a 64-bit Shared memory handle
1143+ * w6: Offset into shared memory pointing to a struct optee_msg_arg
1144+ * right after the parameters of this struct (at offset
1145+ * OPTEE_MSG_GET_ARG_SIZE(num_params) follows a struct optee_msg_arg
1146+ * for RPC, this struct has reserved space for the number of RPC
1147+ * parameters as returned by OPTEE_FFA_EXCHANGE_CAPABILITIES.
1148+ * w7: Not used (MBZ)
1149+ * Resume from RPC. Register usage:
1150+ * w3: Service ID, OPTEE_FFA_YIELDING_CALL_RESUME
1151+ * w4-w6: Not used (MBZ)
1152+ * w7: Resume info
1153+ *
1154+ * Normal return (yielding call is completed). Register usage:
1155+ * w3: Error code, 0 on success
1156+ * w4: OPTEE_FFA_YIELDING_CALL_RETURN_DONE
1157+ * w5-w7: Not used (MBZ)
1158+ *
1159+ * RPC interrupt return (RPC from secure world). Register usage:
1160+ * w3: Error code == 0
1161+ * w4: Any defined RPC code but OPTEE_FFA_YIELDING_CALL_RETURN_DONE
1162+ * w5-w6: Not used (MBZ)
1163+ * w7: Resume info
1164+ *
1165+ * Possible error codes in register w3:
1166+ * 0: Success
1167+ * FFA_DENIED: w4 isn't one of OPTEE_FFA_YIELDING_CALL_START
1168+ * OPTEE_FFA_YIELDING_CALL_RESUME
1169+ *
1170+ * Possible error codes for OPTEE_FFA_YIELDING_CALL_START,
1171+ * FFA_BUSY: Number of OP-TEE OS threads exceeded,
1172+ * try again later
1173+ * FFA_DENIED: RPC shared memory object not found
1174+ * FFA_INVALID_PARAMETER: Bad shared memory handle or offset into the memory
1175+ *
1176+ * Possible error codes for OPTEE_FFA_YIELDING_CALL_RESUME
1177+ * FFA_INVALID_PARAMETER: Bad resume info
1178+ */
1179+#define OPTEE_FFA_YIELDING_CALL_WITH_ARG OPTEE_FFA_YIELDING_CALL(0)
1180+#define OPTEE_FFA_YIELDING_CALL_RESUME OPTEE_FFA_YIELDING_CALL(1)
1181+
1182+#define OPTEE_FFA_YIELDING_CALL_RETURN_DONE 0
1183+#define OPTEE_FFA_YIELDING_CALL_RETURN_RPC_CMD 1
1184+#define OPTEE_FFA_YIELDING_CALL_RETURN_INTERRUPT 2
1185+
1186+#endif /*__OPTEE_FFA_H*/
1187diff --git a/drivers/tee/optee/optee_msg.h b/drivers/tee/optee/optee_msg.h
Patrick Williams2194f502022-10-16 14:26:09 -05001188index e3d72d09c484..2422e185d400 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001189--- a/drivers/tee/optee/optee_msg.h
1190+++ b/drivers/tee/optee/optee_msg.h
1191@@ -28,6 +28,9 @@
1192 #define OPTEE_MSG_ATTR_TYPE_RMEM_INPUT 0x5
1193 #define OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT 0x6
1194 #define OPTEE_MSG_ATTR_TYPE_RMEM_INOUT 0x7
1195+#define OPTEE_MSG_ATTR_TYPE_FMEM_INPUT OPTEE_MSG_ATTR_TYPE_RMEM_INPUT
1196+#define OPTEE_MSG_ATTR_TYPE_FMEM_OUTPUT OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT
1197+#define OPTEE_MSG_ATTR_TYPE_FMEM_INOUT OPTEE_MSG_ATTR_TYPE_RMEM_INOUT
1198 #define OPTEE_MSG_ATTR_TYPE_TMEM_INPUT 0x9
1199 #define OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT 0xa
1200 #define OPTEE_MSG_ATTR_TYPE_TMEM_INOUT 0xb
1201@@ -96,6 +99,8 @@
1202 */
1203 #define OPTEE_MSG_NONCONTIG_PAGE_SIZE 4096
1204
1205+#define OPTEE_MSG_FMEM_INVALID_GLOBAL_ID 0xffffffffffffffff
1206+
1207 /**
1208 * struct optee_msg_param_tmem - temporary memory reference parameter
1209 * @buf_ptr: Address of the buffer
1210@@ -127,6 +132,23 @@ struct optee_msg_param_rmem {
1211 u64 shm_ref;
1212 };
1213
1214+/**
1215+ * struct optee_msg_param_fmem - ffa memory reference parameter
1216+ * @offs_lower: Lower bits of offset into shared memory reference
1217+ * @offs_upper: Upper bits of offset into shared memory reference
1218+ * @internal_offs: Internal offset into the first page of shared memory
1219+ * reference
1220+ * @size: Size of the buffer
1221+ * @global_id: Global identifier of Shared memory
1222+ */
1223+struct optee_msg_param_fmem {
1224+ u32 offs_low;
1225+ u16 offs_high;
1226+ u16 internal_offs;
1227+ u64 size;
1228+ u64 global_id;
1229+};
1230+
1231 /**
1232 * struct optee_msg_param_value - opaque value parameter
1233 *
1234@@ -143,13 +165,15 @@ struct optee_msg_param_value {
1235 * @attr: attributes
1236 * @tmem: parameter by temporary memory reference
1237 * @rmem: parameter by registered memory reference
1238+ * @fmem: parameter by ffa registered memory reference
1239 * @value: parameter by opaque value
1240 * @octets: parameter by octet string
1241 *
1242 * @attr & OPTEE_MSG_ATTR_TYPE_MASK indicates if tmem, rmem or value is used in
1243 * the union. OPTEE_MSG_ATTR_TYPE_VALUE_* indicates value or octets,
1244 * OPTEE_MSG_ATTR_TYPE_TMEM_* indicates @tmem and
1245- * OPTEE_MSG_ATTR_TYPE_RMEM_* indicates @rmem,
1246+ * OPTEE_MSG_ATTR_TYPE_RMEM_* or the alias PTEE_MSG_ATTR_TYPE_FMEM_* indicates
1247+ * @rmem or @fmem depending on the conduit.
1248 * OPTEE_MSG_ATTR_TYPE_NONE indicates that none of the members are used.
1249 */
1250 struct optee_msg_param {
1251@@ -157,6 +181,7 @@ struct optee_msg_param {
1252 union {
1253 struct optee_msg_param_tmem tmem;
1254 struct optee_msg_param_rmem rmem;
1255+ struct optee_msg_param_fmem fmem;
1256 struct optee_msg_param_value value;
1257 u8 octets[24];
1258 } u;
1259diff --git a/drivers/tee/optee/optee_private.h b/drivers/tee/optee/optee_private.h
Patrick Williams2194f502022-10-16 14:26:09 -05001260index 40af6b059b20..6660e05298db 100644
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001261--- a/drivers/tee/optee/optee_private.h
1262+++ b/drivers/tee/optee/optee_private.h
1263@@ -7,6 +7,7 @@
1264 #define OPTEE_PRIVATE_H
1265
1266 #include <linux/arm-smccc.h>
1267+#include <linux/rhashtable.h>
1268 #include <linux/semaphore.h>
1269 #include <linux/tee_drv.h>
1270 #include <linux/types.h>
Patrick Williams2194f502022-10-16 14:26:09 -05001271@@ -22,6 +23,7 @@
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001272 #define TEEC_ERROR_NOT_SUPPORTED 0xFFFF000A
1273 #define TEEC_ERROR_COMMUNICATION 0xFFFF000E
1274 #define TEEC_ERROR_OUT_OF_MEMORY 0xFFFF000C
1275+#define TEEC_ERROR_BUSY 0xFFFF000D
1276 #define TEEC_ERROR_SHORT_BUFFER 0xFFFF0010
1277
1278 #define TEEC_ORIGIN_COMMS 0x00000002
Patrick Williams2194f502022-10-16 14:26:09 -05001279@@ -73,19 +75,28 @@ struct optee_supp {
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001280 struct completion reqs_c;
1281 };
1282
Patrick Williams2194f502022-10-16 14:26:09 -05001283-/**
1284- * struct optee_smc - SMC ABI specifics
1285- * @invoke_fn: function to issue smc or hvc
1286- * @memremaped_shm virtual address of memory in shared memory pool
1287- * @sec_caps: secure world capabilities defined by
1288- * OPTEE_SMC_SEC_CAP_* in optee_smc.h
1289- */
1290 struct optee_smc {
1291 optee_invoke_fn *invoke_fn;
1292 void *memremaped_shm;
1293 u32 sec_caps;
1294 };
1295
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001296+/**
1297+ * struct optee_ffa_data - FFA communication struct
1298+ * @ffa_dev FFA device, contains the destination id, the id of
1299+ * OP-TEE in secure world
1300+ * @ffa_ops FFA operations
1301+ * @mutex Serializes access to @global_ids
1302+ * @global_ids FF-A shared memory global handle translation
1303+ */
1304+struct optee_ffa {
1305+ struct ffa_device *ffa_dev;
1306+ const struct ffa_dev_ops *ffa_ops;
1307+ /* Serializes access to @global_ids */
1308+ struct mutex mutex;
1309+ struct rhashtable global_ids;
1310+};
1311+
1312 struct optee;
1313
1314 /**
Patrick Williams2194f502022-10-16 14:26:09 -05001315@@ -116,11 +127,13 @@ struct optee_ops {
1316 * world
1317 * @teedev: client device
1318 * @smc: specific to SMC ABI
1319+ * @ffa: specific to FF-A ABI
1320 * @call_queue: queue of threads waiting to call @invoke_fn
1321 * @wait_queue: queue of threads from secure world waiting for a
1322 * secure world sync object
1323 * @supp: supplicant synchronization struct for RPC to supplicant
1324 * @pool: shared memory pool
1325+ * @rpc_arg_count: If > 0 number of RPC parameters to make room for
1326 * @scan_bus_done flag if device registation was already done.
1327 * @scan_bus_wq workqueue to scan optee bus and register optee drivers
1328 * @scan_bus_work workq to scan optee bus and register optee drivers
1329@@ -129,11 +142,15 @@ struct optee {
1330 struct tee_device *supp_teedev;
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001331 struct tee_device *teedev;
1332 const struct optee_ops *ops;
Patrick Williams2194f502022-10-16 14:26:09 -05001333- struct optee_smc smc;
1334+ union {
1335+ struct optee_smc smc;
1336+ struct optee_ffa ffa;
1337+ };
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001338 struct optee_call_queue call_queue;
1339 struct optee_wait_queue wait_queue;
1340 struct optee_supp supp;
1341 struct tee_shm_pool *pool;
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001342+ unsigned int rpc_arg_count;
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001343 bool scan_bus_done;
1344 struct workqueue_struct *scan_bus_wq;
Patrick Williams2194f502022-10-16 14:26:09 -05001345 struct work_struct scan_bus_work;
1346@@ -266,5 +283,7 @@ static inline void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)
1347 /* Registration of the ABIs */
1348 int optee_smc_abi_register(void);
1349 void optee_smc_abi_unregister(void);
1350+int optee_ffa_abi_register(void);
1351+void optee_ffa_abi_unregister(void);
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001352
Patrick Williams2194f502022-10-16 14:26:09 -05001353 #endif /*OPTEE_PRIVATE_H*/
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001354--
Patrick Williams2194f502022-10-16 14:26:09 -050013552.34.1
Brad Bishopbec4ebc2022-08-03 09:55:16 -04001356