From d2d1a742c44f64cf433d434bc3bd1d700cf9513c Mon Sep 17 00:00:00 2001 From: Axel Heider Date: Fri, 31 Mar 2023 15:28:44 +0200 Subject: [PATCH] vm_arm: move vpci code to dedicated module This calls vmm_pci_init() only when vpci is enabled. Signed-off-by: Axel Heider --- arm_vm_helpers.cmake | 9 +++- components/VM_Arm/src/main.c | 44 ++--------------- components/VM_Arm/src/modules/vpci.c | 71 ++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 40 deletions(-) create mode 100644 components/VM_Arm/src/modules/vpci.c diff --git a/arm_vm_helpers.cmake b/arm_vm_helpers.cmake index 4aea6663..125a6ea5 100644 --- a/arm_vm_helpers.cmake +++ b/arm_vm_helpers.cmake @@ -45,11 +45,18 @@ function(DeclareCAmkESARMVM init_component) vm_src ${ARM_VM_PROJECT_DIR}/components/VM_Arm/src/main.c ${ARM_VM_PROJECT_DIR}/components/VM_Arm/src/fdt_manipulation.c - ${ARM_VM_PROJECT_DIR}/components/VM_Arm/src/crossvm.c ${ARM_VM_PROJECT_DIR}/components/VM_Arm/src/modules/map_frame_hack.c ${ARM_VM_PROJECT_DIR}/components/VM_Arm/src/modules/init_ram.c ) + if(VmPCISupport) + list( + APPEND + vm_src ${ARM_VM_PROJECT_DIR}/components/VM_Arm/src/modules/vpci.c + ${ARM_VM_PROJECT_DIR}/components/VM_Arm/src/crossvm.c + ) + endif() + if(VmVirtUart) list(APPEND vm_src ${ARM_VM_PROJECT_DIR}/components/VM_Arm/src/modules/vuart_init.c) endif() diff --git a/components/VM_Arm/src/main.c b/components/VM_Arm/src/main.c index cf591e4d..a1da758f 100644 --- a/components/VM_Arm/src/main.c +++ b/components/VM_Arm/src/main.c @@ -38,12 +38,8 @@ #include #include - -#include -#include #include -#include -#include +#include #include #include #include @@ -97,7 +93,6 @@ allocman_t *allocman; seL4_CPtr _fault_endpoint; irq_server_t *_irq_server; -vmm_pci_space_t *pci; vmm_io_port_list_t *io_ports; reboot_hooks_list_t reboot_hooks_list; @@ -687,17 +682,6 @@ extern vmm_module_t __stop__vmm_module[]; static int install_vm_devices(vm_t *vm, const vm_config_t *vm_config) { - int err; - - /* Install virtual devices */ - if (config_set(CONFIG_VM_PCI_SUPPORT)) { - err = vm_install_vpci(vm, io_ports, pci); - if (err) { - ZF_LOGE("Failed to install VPCI device"); - return -1; - } - } - int max_vmm_modules = (int)(__stop__vmm_module - __start__vmm_module); int num_vmm_modules = 0; for (vmm_module_t *i = __start__vmm_module; i < __stop__vmm_module; i++) { @@ -707,7 +691,6 @@ static int install_vm_devices(vm_t *vm, const vm_config_t *vm_config) } return 0; - } static int route_irq(int irq_num, vm_vcpu_t *vcpu, irq_server_t *irq_server) @@ -875,22 +858,11 @@ static int vm_dtb_finalize(vm_t *vm, const vm_config_t *vm_config) assert(vm_config->generate_dtb); if (config_set(CONFIG_VM_PCI_SUPPORT)) { - /* Modules can add PCI devices, so the PCI device tree node can be - * created only after all modules have been set up. - */ - int gic_offset = fdt_path_offset(fdt_ori, GIC_NODE_PATH); - if (gic_offset < 0) { - ZF_LOGE("Failed to find gic node from path: %s", GIC_NODE_PATH); - return -1; - } - int gic_phandle = fdt_get_phandle(fdt_ori, gic_offset); - if (0 == gic_phandle) { - ZF_LOGE("Failed to find phandle in gic node"); - return -1; - } - int err = fdt_generate_vpci_node(vm, pci, gen_dtb_buf, gic_phandle); + extern int vpci_update_dtb(vm_t *vm, const vm_config_t *vm_config, void *dtb, + void *fdt, char const *gic_node); + int err = vpci_update_dtb(vm, vm_config, gen_dtb_buf, fdt_ori, GIC_NODE_PATH); if (err) { - ZF_LOGE("Couldn't generate vpci_node (%d)", err); + ZF_LOGE("Couldn't generate VPCI device tree node (%d)\n", err); return -1; } } @@ -1191,12 +1163,6 @@ static int main_continued(void) return -1; } - err = vmm_pci_init(&pci); - if (err) { - ZF_LOGE("Failed to initialise vmm pci"); - return err; - } - err = vmm_io_port_init(&io_ports, FREE_IOPORT_START); if (err) { ZF_LOGE("Failed to initialise VM ioports"); diff --git a/components/VM_Arm/src/modules/vpci.c b/components/VM_Arm/src/modules/vpci.c new file mode 100644 index 00000000..17b5b2e0 --- /dev/null +++ b/components/VM_Arm/src/modules/vpci.c @@ -0,0 +1,71 @@ +/* + * Copyright 2023, Hensoldt Cyber + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +#include +#include +#include + +#include + +extern vmm_io_port_list_t *io_ports; + +/* Can't make this static, because others uses it */ +vmm_pci_space_t *pci = NULL; + +/* Various other modules can register PCI devices. Update the device tree + * eventually. + */ +int vpci_update_dtb(vm_t *vm, const vm_config_t *vm_config, void *dtb, + void *fdt, char const *gic_node) +{ + if (!vm_config->generate_dtb) { + return 0; + } + + ZF_LOGF_IF(!fdt_ori, "fdt not set"); + + int gic_offset = fdt_path_offset(fdt, gic_node); + if (gic_offset < 0) { + ZF_LOGE("Failed to find gic node from path: %s", gic_node); + return -1; + } + int gic_phandle = fdt_get_phandle(fdt, gic_offset); + if (0 == gic_phandle) { + ZF_LOGE("Failed to find phandle in gic node (%d)", gic_phandle); + return -1; + } + int err = fdt_generate_vpci_node(vm, pci, dtb, gic_phandle); + if (err) { + ZF_LOGE("Couldn't generate vpci_node (%d)", err); + return -1; + } + +} + +static void vpci_init_module(vm_t *vm, void *cookie) +{ + int err; + + err = vmm_pci_init(&pci); + if (err) { + ZF_LOGE("Failed to initialise vmm pci (%d)", err); + return; + } + + ZF_LOGF_IF(!io_ports, "io_ports not set"); + + err = vm_install_vpci(vm, io_ports, pci); + if (err) { + ZF_LOGE("Failed to install VPCI device (%d)", err); + return; + } + +} + +DEFINE_MODULE(vpci, NULL, vpci_init_module)