Skip to content

Commit bc3888a

Browse files
willdeaconoupton
authored andcommitted
KVM: arm64: Allocate pages for hypervisor FF-A mailboxes
The FF-A proxy code needs to allocate its own buffer pair for communication with EL3 and for forwarding calls from the host at EL1. Reserve a couple of pages for this purpose and use them to initialise the hypervisor's FF-A buffer structure. Co-developed-by: Andrew Walbran <qwandor@google.com> Signed-off-by: Andrew Walbran <qwandor@google.com> Signed-off-by: Will Deacon <will@kernel.org> Link: https://lore.kernel.org/r/20230523101828.7328-4-will@kernel.org Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
1 parent 12bdce4 commit bc3888a

5 files changed

Lines changed: 40 additions & 3 deletions

File tree

arch/arm64/include/asm/kvm_pkvm.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,12 @@ static inline unsigned long host_s2_pgtable_pages(void)
106106
return res;
107107
}
108108

109+
#define KVM_FFA_MBOX_NR_PAGES 1
110+
111+
static inline unsigned long hyp_ffa_proxy_pages(void)
112+
{
113+
/* A page each for the hypervisor's RX and TX mailboxes. */
114+
return 2 * KVM_FFA_MBOX_NR_PAGES;
115+
}
116+
109117
#endif /* __ARM64_KVM_PKVM_H__ */

arch/arm64/kvm/hyp/include/nvhe/ffa.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define FFA_MIN_FUNC_NUM 0x60
1212
#define FFA_MAX_FUNC_NUM 0x7F
1313

14-
int hyp_ffa_init(void);
14+
int hyp_ffa_init(void *pages);
1515
bool kvm_host_ffa_handler(struct kvm_cpu_context *host_ctxt);
1616

1717
#endif /* __KVM_HYP_FFA_H */

arch/arm64/kvm/hyp/nvhe/ffa.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,31 @@
2828

2929
#include <linux/arm-smccc.h>
3030
#include <linux/arm_ffa.h>
31+
#include <asm/kvm_pkvm.h>
32+
3133
#include <nvhe/ffa.h>
3234
#include <nvhe/trap_handler.h>
35+
#include <nvhe/spinlock.h>
3336

3437
/*
3538
* "ID value 0 must be returned at the Non-secure physical FF-A instance"
3639
* We share this ID with the host.
3740
*/
3841
#define HOST_FFA_ID 0
3942

43+
struct kvm_ffa_buffers {
44+
hyp_spinlock_t lock;
45+
void *tx;
46+
void *rx;
47+
};
48+
49+
/*
50+
* Note that we don't currently lock these buffers explicitly, instead
51+
* relying on the locking of the host FFA buffers as we only have one
52+
* client.
53+
*/
54+
static struct kvm_ffa_buffers hyp_buffers;
55+
4056
static void ffa_to_smccc_error(struct arm_smccc_res *res, u64 ffa_errno)
4157
{
4258
*res = (struct arm_smccc_res) {
@@ -124,7 +140,7 @@ bool kvm_host_ffa_handler(struct kvm_cpu_context *host_ctxt)
124140
return true;
125141
}
126142

127-
int hyp_ffa_init(void)
143+
int hyp_ffa_init(void *pages)
128144
{
129145
struct arm_smccc_res res;
130146

@@ -145,5 +161,11 @@ int hyp_ffa_init(void)
145161
if (res.a2 != HOST_FFA_ID)
146162
return -EINVAL;
147163

164+
hyp_buffers = (struct kvm_ffa_buffers) {
165+
.lock = __HYP_SPIN_LOCK_UNLOCKED,
166+
.tx = pages,
167+
.rx = pages + (KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE),
168+
};
169+
148170
return 0;
149171
}

arch/arm64/kvm/hyp/nvhe/setup.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ static void *vmemmap_base;
2929
static void *vm_table_base;
3030
static void *hyp_pgt_base;
3131
static void *host_s2_pgt_base;
32+
static void *ffa_proxy_pages;
3233
static struct kvm_pgtable_mm_ops pkvm_pgtable_mm_ops;
3334
static struct hyp_pool hpool;
3435

@@ -58,6 +59,11 @@ static int divide_memory_pool(void *virt, unsigned long size)
5859
if (!host_s2_pgt_base)
5960
return -ENOMEM;
6061

62+
nr_pages = hyp_ffa_proxy_pages();
63+
ffa_proxy_pages = hyp_early_alloc_contig(nr_pages);
64+
if (!ffa_proxy_pages)
65+
return -ENOMEM;
66+
6167
return 0;
6268
}
6369

@@ -315,7 +321,7 @@ void __noreturn __pkvm_init_finalise(void)
315321
if (ret)
316322
goto out;
317323

318-
ret = hyp_ffa_init();
324+
ret = hyp_ffa_init(ffa_proxy_pages);
319325
if (ret)
320326
goto out;
321327

arch/arm64/kvm/pkvm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ void __init kvm_hyp_reserve(void)
7878
hyp_mem_pages += host_s2_pgtable_pages();
7979
hyp_mem_pages += hyp_vm_table_pages();
8080
hyp_mem_pages += hyp_vmemmap_pages(STRUCT_HYP_PAGE_SIZE);
81+
hyp_mem_pages += hyp_ffa_proxy_pages();
8182

8283
/*
8384
* Try to allocate a PMD-aligned region to reduce TLB pressure once

0 commit comments

Comments
 (0)