From d6f563c4ebda807b9710bfa7d739322f79c76198 Mon Sep 17 00:00:00 2001 From: Yuan Liu Date: Tue, 28 Jul 2020 15:02:49 +0800 Subject: [PATCH] hv: implement ivshmem memory regions initialization The ivshmem memory regions use the memory of the hypervisor and they are continuous and page aligned. this patch is used to initialize each memory region hpa. v2: 1) if CONFIG_IVSHMEM_SHARED_MEMORY_ENABLED is not defined, the entire code of ivshmem will not be compiled. 2) change ivshmem shared memory unit from byte to page to avoid misconfiguration. 3) add ivshmem configuration and vm configuration references v3: 1) change CONFIG_IVSHMEM_SHARED_MEMORY_ENABLED to CONFIG_IVSHMEM_ENABLED 2) remove the ivshmem configuration sample, offline tool provides default ivshmem configuration. 3) refine code style. v4: 1) make ivshmem_base 2M aligned. Tracked-On: #4853 Signed-off-by: Yuan Liu Acked-by: Eddie Dong --- hypervisor/Makefile | 1 + hypervisor/arch/x86/Kconfig | 8 ++++++++ hypervisor/arch/x86/cpu.c | 4 ++++ hypervisor/dm/vpci/ivshmem.c | 29 +++++++++++++++++++++++++++++ hypervisor/include/dm/ivshmem.h | 28 ++++++++++++++++++++++++++++ 5 files changed, 70 insertions(+) create mode 100644 hypervisor/dm/vpci/ivshmem.c create mode 100644 hypervisor/include/dm/ivshmem.h diff --git a/hypervisor/Makefile b/hypervisor/Makefile index 625cb9a6d..fde511f0d 100644 --- a/hypervisor/Makefile +++ b/hypervisor/Makefile @@ -306,6 +306,7 @@ VP_DM_C_SRCS += dm/vpci/vdev.c VP_DM_C_SRCS += dm/vpci/vpci.c VP_DM_C_SRCS += dm/vpci/vhostbridge.c VP_DM_C_SRCS += dm/vpci/vpci_bridge.c +VP_DM_C_SRCS += dm/vpci/ivshmem.c VP_DM_C_SRCS += dm/vpci/pci_pt.c VP_DM_C_SRCS += dm/vpci/vmsi.c VP_DM_C_SRCS += dm/vpci/vmsix.c diff --git a/hypervisor/arch/x86/Kconfig b/hypervisor/arch/x86/Kconfig index f1c969c84..7b3d49a9d 100644 --- a/hypervisor/arch/x86/Kconfig +++ b/hypervisor/arch/x86/Kconfig @@ -336,3 +336,11 @@ config ENFORCE_TURNOFF_AC help If CPU has #AC for split-locked access, HV enable it and VMs can't disable. Set this to enforce turn off that #AC, for community developer only. + +config IVSHMEM_ENABLED + bool "Enable ivshmem inter-vm communication based on hypervisor shared memory" + default n + help + Ivshmem shared memory is located in the hypervisor, it can provide multiple + memory regions as communication channels between pre-launched or post-launched + VMs. diff --git a/hypervisor/arch/x86/cpu.c b/hypervisor/arch/x86/cpu.c index d17233508..021c6c118 100644 --- a/hypervisor/arch/x86/cpu.c +++ b/hypervisor/arch/x86/cpu.c @@ -27,6 +27,7 @@ #include #include #include +#include #define CPU_UP_TIMEOUT 100U /* millisecond */ #define CPU_DOWN_TIMEOUT 100U /* millisecond */ @@ -244,6 +245,9 @@ void init_pcpu_post(uint16_t pcpu_id) } hv_access_memory_region_update(get_mmcfg_base(), PCI_MMCONFIG_SIZE); +#ifdef CONFIG_IVSHMEM_ENABLED + init_ivshmem_shared_memory(); +#endif init_pci_pdev_list(); /* init_iommu must come before this */ ptdev_init(); diff --git a/hypervisor/dm/vpci/ivshmem.c b/hypervisor/dm/vpci/ivshmem.c new file mode 100644 index 000000000..42e34fa9e --- /dev/null +++ b/hypervisor/dm/vpci/ivshmem.c @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2020 Intel Corporation. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifdef CONFIG_IVSHMEM_ENABLED +#include +#include +#include +#include +#include +#include "vpci_priv.h" + +/* IVSHMEM_SHM_SIZE is provided by offline tool */ +static uint8_t ivshmem_base[IVSHMEM_SHM_SIZE] __aligned(PDE_SIZE); + +void init_ivshmem_shared_memory() +{ + uint32_t i; + uint64_t addr = hva2hpa(&ivshmem_base); + + for (i = 0U; i < ARRAY_SIZE(mem_regions); i++) { + mem_regions[i].hpa = addr; + addr += mem_regions[i].size; + } +} + +#endif diff --git a/hypervisor/include/dm/ivshmem.h b/hypervisor/include/dm/ivshmem.h new file mode 100644 index 000000000..7a8020b38 --- /dev/null +++ b/hypervisor/include/dm/ivshmem.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2020 Intel Corporation. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef IVSHMEM_H +#define IVSHMEM_H + +#ifdef CONFIG_IVSHMEM_ENABLED +struct ivshmem_shm_region { + char name[32]; + uint64_t hpa; + uint64_t size; +}; + +/** + * @brief Initialize ivshmem shared memory regions + * + * Initialize ivshmem shared memory regions based on user configuration. + * + * @return None + */ +void init_ivshmem_shared_memory(void); + +#endif /* CONFIG_IVSHMEM_ENABLED */ + +#endif /* IVSHMEM_H */