Skip to content

Commit 62fd5ab

Browse files
xakep-amatopoleksiimoisieiev
authored andcommitted
drivers: xen: gnttab: do Xen node mapping inside driver
Move memory mapping of Xen node to Grant Table driver system init function. After moving mapping we don't need anymore records of xen-xen node into 'mmu_regions' array, so they were deleted from all SoCs: Rcar Gen3/Gen4 and XenVM. We need at least 16M of virtual address space to map memory of Xen node, so the virtual memory sized has been increased to 32 MB, it should be enough for basic use-cases and mapping of 16M mem region of Xen node. Unfortunately, after moving we also need to increase number of XLAT tables. The previous code was more efficient if we talking about usage of XLAT tables, because it mapped grant tables using a higher- order table that allows mapping blocks of 2MB. And after the changes is maps every 4KB page, so we need more XLAT tables. Increase number of grant frames, it is needed to sync stage 1 and stage 2 memory mappings, previously we map only one page on stage 2 and further usage of unmap regions can cause MMU translation errors. Perform mapping stage 1 before mapping for stage 2 (add to physmap), because right after stage 1 we can try to access memory and if it is unmap in stage 2, error will be received during translation. Note: Xen Grant Table driver doesn't use Zephyr Device Model. Authored-by: Mykola Kvach <[email protected]> Co-authored-by: Oleksii Moisieiev <[email protected]> Signed-off-by: Mykola Kvach <[email protected]>
1 parent ef041c6 commit 62fd5ab

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

boards/arm64/xenvm/xenvm_defconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CONFIG_BOARD_XENVM=y
44
# Enable UART driver
55
CONFIG_SERIAL=y
66

7-
CONFIG_MAX_XLAT_TABLES=10
7+
CONFIG_MAX_XLAT_TABLES=24
88
CONFIG_HEAP_MEM_POOL_SIZE=16384
99

1010
# Enable console

boards/arm64/xenvm/xenvm_gicv3_defconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CONFIG_BOARD_XENVM=y
44
# Enable UART driver
55
CONFIG_SERIAL=y
66

7-
CONFIG_MAX_XLAT_TABLES=10
7+
CONFIG_MAX_XLAT_TABLES=24
88

99
# Enable console
1010
CONFIG_CONSOLE=y

drivers/xen/gnttab.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,24 @@
2828
#include <zephyr/init.h>
2929
#include <zephyr/kernel.h>
3030
#include <zephyr/logging/log.h>
31+
#include <zephyr/sys/device_mmio.h>
3132

3233
LOG_MODULE_REGISTER(xen_gnttab);
3334

3435
/* Timeout for grant table ops retrying */
3536
#define GOP_RETRY_DELAY 200
3637

38+
#define GNTTAB_SIZE DT_REG_SIZE_BY_IDX(DT_INST(0, xen_xen), 0)
39+
BUILD_ASSERT(!(GNTTAB_SIZE % XEN_PAGE_SIZE), "Size of gnttab have to be aligned on XEN_PAGE_SIZE");
40+
3741
/* NR_GRANT_FRAMES must be less than or equal to that configured in Xen */
38-
#define NR_GRANT_FRAMES 1
42+
#define NR_GRANT_FRAMES (GNTTAB_SIZE / XEN_PAGE_SIZE)
3943
#define NR_GRANT_ENTRIES \
4044
(NR_GRANT_FRAMES * XEN_PAGE_SIZE / sizeof(grant_entry_v1_t))
4145

46+
BUILD_ASSERT(GNTTAB_SIZE <= CONFIG_KERNEL_VM_SIZE);
47+
DEVICE_MMIO_TOPLEVEL_STATIC(grant_tables, DT_INST(0, xen_xen));
48+
4249
static struct gnttab {
4350
struct k_sem sem;
4451
grant_entry_v1_t *table;
@@ -307,15 +314,12 @@ static int gnttab_init(void)
307314
put_free_entry(gref);
308315
}
309316

310-
gnttab.table = (grant_entry_v1_t *)
311-
DT_REG_ADDR_BY_IDX(DT_INST(0, xen_xen), 0);
312-
313317
for (i = 0; i < NR_GRANT_FRAMES; i++) {
314318
xatp.domid = DOMID_SELF;
315319
xatp.size = 0;
316320
xatp.space = XENMAPSPACE_grant_table;
317321
xatp.idx = i;
318-
xatp.gpfn = xen_virt_to_gfn(gnttab.table) + i;
322+
xatp.gpfn = xen_virt_to_gfn(Z_TOPLEVEL_ROM_NAME(grant_tables).phys_addr) + i;
319323
rc = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
320324
__ASSERT(!rc, "add_to_physmap failed; status = %d\n", rc);
321325
}
@@ -327,6 +331,9 @@ static int gnttab_init(void)
327331
__ASSERT((!rc) && (!setup.status), "Table setup failed; status = %s\n",
328332
gnttabop_error(setup.status));
329333

334+
DEVICE_MMIO_TOPLEVEL_MAP(grant_tables, K_MEM_CACHE_WB | K_MEM_PERM_RW);
335+
gnttab.table = (grant_entry_v1_t *)DEVICE_MMIO_TOPLEVEL_GET(grant_tables);
336+
330337
LOG_DBG("%s: grant table mapped\n", __func__);
331338

332339
return 0;

soc/arm64/xenvm/Kconfig.defconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ config SYS_CLOCK_HW_CYCLES_PER_SEC
1414
int
1515
default 8320000
1616

17+
# We need at least 16M of virtual address space to map memory of Xen node
18+
# 32M should be enough for basic use-cases
19+
config KERNEL_VM_SIZE
20+
default 0x2000000
21+
1722
endif

soc/arm64/xenvm/mmu_regions.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ static const struct arm_mmu_region mmu_regions[] = {
1818
DT_REG_ADDR_BY_IDX(DT_INST(0, arm_gic), 1),
1919
DT_REG_SIZE_BY_IDX(DT_INST(0, arm_gic), 1),
2020
MT_DEVICE_nGnRnE | MT_P_RW_U_NA | MT_NS),
21-
22-
MMU_REGION_FLAT_ENTRY("HYPERVISOR",
23-
DT_REG_ADDR_BY_IDX(DT_INST(0, xen_xen), 0),
24-
DT_REG_SIZE_BY_IDX(DT_INST(0, xen_xen), 0),
25-
MT_NORMAL | MT_P_RW_U_NA | MT_NS),
2621
};
2722

2823
const struct arm_mmu_config mmu_config = {

0 commit comments

Comments
 (0)