Skip to content

Commit

Permalink
Handle shared memory offset in optee driver
Browse files Browse the repository at this point in the history
Signed-off-by: Jingdong Lu <jingdong.lu@intel.com>
  • Loading branch information
jingdlu committed Jun 13, 2023
1 parent 7bf2c8d commit 910e6c7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
6 changes: 4 additions & 2 deletions drivers/tee/optee/call.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#include <asm/io.h>
#endif

#if defined(CONFIG_OPTEE_VSOCK)
extern phys_addr_t optee_shm_offset;
#if defined(CONFIG_OPTEE_VSOCK) || defined(CONFIG_OPTEE_IVSHMEM)
extern unsigned long optee_shm_offset;
#endif

struct optee_call_waiter {
Expand Down Expand Up @@ -147,6 +147,8 @@ u32 optee_do_call_with_arg(struct tee_context *ctx, phys_addr_t parg)

param.a3 = arg_size;
parg = parg - optee_shm_offset;
#elif defined(CONFIG_OPTEE_IVSHMEM)
parg = parg - optee_shm_offset;
#endif

param.a0 = OPTEE_SMC_CALL_WITH_ARG;
Expand Down
37 changes: 24 additions & 13 deletions drivers/tee/optee/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

static struct socket *host_smc_sock = NULL;
static struct socket *tee_sock = NULL;
phys_addr_t optee_shm_offset;
unsigned long optee_shm_offset = 0;

static DEFINE_SEMAPHORE(optee_smc_lock);
#endif
Expand All @@ -63,6 +63,8 @@ static DEFINE_SEMAPHORE(optee_smc_lock);

#define OPTEE_HANDLE_DONE 0xa5a5a5a5

#define OPTEE_SHM_SMC_SIZE 0x200000

struct ivshmem_private {
struct pci_dev *dev;

Expand All @@ -73,12 +75,12 @@ struct ivshmem_private {
u8 *base_addr;
u32 *msix_addr;

unsigned int bar0_addr;
unsigned int bar0_len;
unsigned int bar1_addr;
unsigned int bar1_len;
unsigned int bar2_addr;
unsigned int bar2_len;
unsigned long bar0_addr;
unsigned long bar0_len;
unsigned long bar1_addr;
unsigned long bar1_len;
unsigned long bar2_addr;
unsigned long bar2_len;

char (*msix_names)[256];
struct msix_entry *msix_entries;
Expand Down Expand Up @@ -121,6 +123,8 @@ static struct optee_smc_ring *smc_avail_ring = NULL;
static struct optee_smc_ring *smc_used_ring = NULL;
static struct optee_vm_ids *smc_vm_ids = NULL;

unsigned long optee_shm_offset = 0;

//spinlock used to protect SMC ring operations
static DEFINE_SPINLOCK(smc_ring_lock);

Expand Down Expand Up @@ -196,6 +200,8 @@ int optee_from_msg_param(struct tee_param *params, size_t num_params,
return -EINVAL;
}
}
#elif defined(CONFIG_OPTEE_IVSHMEM)
pa = pa - optee_shm_offset;
#endif
p->u.memref.shm_offs = mp->u.tmem.buf_ptr - pa;
p->u.memref.shm = shm;
Expand Down Expand Up @@ -246,7 +252,7 @@ static int to_msg_param_tmp_mem(struct optee_msg_param *mp,
rc = tee_shm_get_pa(p->u.memref.shm, p->u.memref.shm_offs, &pa);
if (rc)
return rc;
#if defined(CONFIG_OPTEE_VSOCK)
#if defined(CONFIG_OPTEE_VSOCK) || defined(CONFIG_OPTEE_IVSHMEM)
pa = pa - optee_shm_offset;
#endif

Expand Down Expand Up @@ -622,6 +628,11 @@ optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm)
}
paddr = virt_to_phys(va);
optee_shm_offset = paddr - begin;
#elif defined(CONFIG_OPTEE_IVSHMEM)
paddr = g_ivshmem_dev.bar2_addr + OPTEE_SHM_SMC_SIZE;
optee_shm_offset = paddr - begin;
pr_info("shared memory from tee 0x%llx/0x%lx/0x%llx/0x%lx\n",
begin, size, paddr, optee_shm_offset);
#else
paddr = begin;
#endif
Expand Down Expand Up @@ -1260,16 +1271,16 @@ static int ivshmem_probe(struct pci_dev *pdev, const struct pci_device_id *id)
g_ivshmem_dev.bar2_addr = pci_resource_start(pdev, 2);
g_ivshmem_dev.bar2_len = pci_resource_len(pdev, 2);

pr_info("ivshmem BAR0: 0x%0x, %d\n", g_ivshmem_dev.bar0_addr,
pr_info("ivshmem BAR0: 0x%lx, %ld\n", g_ivshmem_dev.bar0_addr,
g_ivshmem_dev.bar0_len);
pr_info("ivshmem BAR1: 0x%0x, %d\n", g_ivshmem_dev.bar1_addr,
pr_info("ivshmem BAR1: 0x%lx, %ld\n", g_ivshmem_dev.bar1_addr,
g_ivshmem_dev.bar1_len);
pr_info("ivshmem BAR2: 0x%0x, %d\n", g_ivshmem_dev.bar2_addr,
pr_info("ivshmem BAR2: 0x%lx, %ld\n", g_ivshmem_dev.bar2_addr,
g_ivshmem_dev.bar2_len);

g_ivshmem_dev.regs_addr = ioremap(g_ivshmem_dev.bar0_addr, g_ivshmem_dev.bar0_len);
if (!g_ivshmem_dev.regs_addr) {
pr_err("ivshmem unable to ioremap bar0, size: %d\n", g_ivshmem_dev.bar0_len);
pr_err("ivshmem unable to ioremap bar0, size: %ld\n", g_ivshmem_dev.bar0_len);
goto release_regions;
}

Expand All @@ -1284,7 +1295,7 @@ static int ivshmem_probe(struct pci_dev *pdev, const struct pci_device_id *id)
*(g_ivshmem_dev.msix_addr+2), *(g_ivshmem_dev.msix_addr+3));

g_ivshmem_dev.base_addr = (u8 *)memremap(g_ivshmem_dev.bar2_addr,
g_ivshmem_dev.bar2_len, MEMREMAP_WT);
OPTEE_SHM_SMC_SIZE, MEMREMAP_WT);
if (!g_ivshmem_dev.base_addr) {
pr_err("base memory ioremap failed\n");
goto iounmap_bar1;
Expand Down
8 changes: 4 additions & 4 deletions drivers/tee/optee/rpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#include "optee_smc.h"
#include "optee_rpc_cmd.h"

#if defined(CONFIG_OPTEE_VSOCK)
extern phys_addr_t optee_shm_offset;
#if defined(CONFIG_OPTEE_VSOCK) || defined(CONFIG_OPTEE_IVSHMEM)
extern unsigned long optee_shm_offset;
#endif

struct wq_entry {
Expand Down Expand Up @@ -339,7 +339,7 @@ static void handle_rpc_func_cmd_shm_alloc(struct tee_context *ctx,
arg->ret = TEEC_ERROR_BAD_PARAMETERS;
goto bad;
}
#if defined(CONFIG_OPTEE_VSOCK)
#if defined(CONFIG_OPTEE_VSOCK) || defined(CONFIG_OPTEE_IVSHMEM)
pa = pa - optee_shm_offset;
#endif

Expand Down Expand Up @@ -572,7 +572,7 @@ void optee_handle_rpc(struct tee_context *ctx, struct optee_rpc_param *param,
case OPTEE_SMC_RPC_FUNC_ALLOC:
shm = tee_shm_alloc(ctx, param->a1, TEE_SHM_MAPPED);
if (!IS_ERR(shm) && !tee_shm_get_pa(shm, 0, &pa)) {
#if defined(CONFIG_OPTEE_VSOCK)
#if defined(CONFIG_OPTEE_VSOCK) || defined(CONFIG_OPTEE_IVSHMEM)
pa = pa - optee_shm_offset;
#endif
reg_pair_from_64(&param->a1, &param->a2, pa);
Expand Down

0 comments on commit 910e6c7

Please sign in to comment.