From 56d8b08b78bd085a426ad28d52d2bf784a3baaf7 Mon Sep 17 00:00:00 2001 From: Tw Date: Mon, 4 Mar 2019 13:34:09 +0800 Subject: [PATCH] hv: merge SBL and UEFI related stuff under bsp This patch unifies the bsp interface between UEFI and SBL. Tracked-On: #2708 Signed-off-by: Tw Acked-by: Eddie Dong --- hypervisor/Makefile | 11 ++-- hypervisor/arch/x86/cpu.c | 3 + hypervisor/arch/x86/init.c | 4 +- hypervisor/arch/x86/irq.c | 4 +- hypervisor/arch/x86/trampoline.c | 4 +- hypervisor/boot/acpi.c | 2 +- hypervisor/boot/uefi/uefi_boot.c | 14 ---- hypervisor/bsp/{uefi => }/cmdline.c | 0 hypervisor/bsp/{sbl => }/const_dmar.c | 0 hypervisor/bsp/firmware_sbl.c | 44 +++++++++++++ hypervisor/bsp/firmware_uefi.c | 88 +++++++++++++++++++++++++ hypervisor/bsp/firmware_wrapper.c | 79 ++++++++++++++++++++++ hypervisor/bsp/include/bsp_extern.h | 31 --------- hypervisor/bsp/include/firmware.h | 28 ++++++++ hypervisor/bsp/include/firmware_sbl.h | 15 +++++ hypervisor/bsp/include/firmware_uefi.h | 23 +++++++ hypervisor/bsp/include/uefi/uefi.h | 19 ------ hypervisor/bsp/sbl/sbl.c | 30 --------- hypervisor/bsp/uefi/uefi.c | 74 --------------------- hypervisor/include/arch/x86/guest/vm.h | 2 +- hypervisor/include/arch/x86/multiboot.h | 1 + 21 files changed, 295 insertions(+), 181 deletions(-) rename hypervisor/bsp/{uefi => }/cmdline.c (100%) rename hypervisor/bsp/{sbl => }/const_dmar.c (100%) create mode 100644 hypervisor/bsp/firmware_sbl.c create mode 100644 hypervisor/bsp/firmware_uefi.c create mode 100644 hypervisor/bsp/firmware_wrapper.c delete mode 100644 hypervisor/bsp/include/bsp_extern.h create mode 100644 hypervisor/bsp/include/firmware.h create mode 100644 hypervisor/bsp/include/firmware_sbl.h create mode 100644 hypervisor/bsp/include/firmware_uefi.h delete mode 100644 hypervisor/bsp/include/uefi/uefi.h delete mode 100644 hypervisor/bsp/sbl/sbl.c delete mode 100644 hypervisor/bsp/uefi/uefi.c diff --git a/hypervisor/Makefile b/hypervisor/Makefile index 3ff9bc821..57727a79a 100644 --- a/hypervisor/Makefile +++ b/hypervisor/Makefile @@ -226,15 +226,16 @@ C_SRCS += dm/vpci/msi.c C_SRCS += dm/vpci/msix.c endif +C_SRCS += bsp/firmware_wrapper.c +C_SRCS += bsp/firmware_sbl.c +C_SRCS += bsp/firmware_uefi.c +C_SRCS += bsp/cmdline.c +C_SRCS += bsp/const_dmar.c + ifeq ($(CONFIG_PLATFORM_UEFI),y) -INCLUDE_PATH += bsp/include/uefi -C_SRCS += bsp/uefi/uefi.c -C_SRCS += bsp/uefi/cmdline.c C_SRCS += boot/uefi/uefi_boot.c else ifeq ($(CONFIG_PLATFORM_SBL),y) -C_SRCS += bsp/sbl/sbl.c -C_SRCS += bsp/sbl/const_dmar.c C_SRCS += boot/sbl/multiboot.c C_SRCS += boot/sbl/sbl_seed_parse.c C_SRCS += boot/sbl/abl_seed_parse.c diff --git a/hypervisor/arch/x86/cpu.c b/hypervisor/arch/x86/cpu.c index 0d223e2ba..1a4966e87 100644 --- a/hypervisor/arch/x86/cpu.c +++ b/hypervisor/arch/x86/cpu.c @@ -25,6 +25,7 @@ #include #include #include +#include struct per_cpu_region per_cpu_data[CONFIG_MAX_PCPU_NUM] __aligned(PAGE_SIZE); static uint16_t phys_cpu_num = 0U; @@ -112,6 +113,8 @@ void init_cpu_pre(uint16_t pcpu_id_args) */ init_cpu_capabilities(); + init_firmware_operations(); + init_cpu_model_name(); load_cpu_state_data(); diff --git a/hypervisor/arch/x86/init.c b/hypervisor/arch/x86/init.c index 8dc95ad2a..40a32dfeb 100644 --- a/hypervisor/arch/x86/init.c +++ b/hypervisor/arch/x86/init.c @@ -62,8 +62,8 @@ static void enter_guest_mode(uint16_t pcpu_id) static void init_primary_cpu_post(void) { - /* Perform any necessary BSP initialization */ - init_bsp(); + /* Perform any necessary firmware initialization */ + init_firmware(); init_debug_pre(); diff --git a/hypervisor/arch/x86/irq.c b/hypervisor/arch/x86/irq.c index 0c0b408fd..5c97a0e52 100644 --- a/hypervisor/arch/x86/irq.c +++ b/hypervisor/arch/x86/irq.c @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include @@ -480,5 +480,5 @@ void interrupt_init(uint16_t pcpu_id) init_lapic(pcpu_id); init_default_irqs(pcpu_id); - bsp_init_irq(); + firmware_init_irq(); } diff --git a/hypervisor/arch/x86/trampoline.c b/hypervisor/arch/x86/trampoline.c index d40ebe553..d0ad010cc 100644 --- a/hypervisor/arch/x86/trampoline.c +++ b/hypervisor/arch/x86/trampoline.c @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include static uint64_t trampoline_start16_paddr; @@ -110,7 +110,7 @@ uint64_t prepare_trampoline(void) uint64_t size, dest_pa, i; size = (uint64_t)(&ld_trampoline_end - &ld_trampoline_start); - dest_pa = bsp_get_ap_trampoline(); + dest_pa = firmware_get_ap_trampoline(); pr_dbg("trampoline code: %llx size %x", dest_pa, size); diff --git a/hypervisor/boot/acpi.c b/hypervisor/boot/acpi.c index efb240af4..374e016fc 100644 --- a/hypervisor/boot/acpi.c +++ b/hypervisor/boot/acpi.c @@ -146,7 +146,7 @@ static struct acpi_table_rsdp *get_rsdp(void) struct acpi_table_rsdp *rsdp = NULL; uint16_t *addr; - rsdp = (struct acpi_table_rsdp *)bsp_get_rsdp(); + rsdp = (struct acpi_table_rsdp *)firmware_get_rsdp(); if (rsdp == NULL) { /* EBDA is addressed by the 16 bit pointer at 0x40E */ addr = (uint16_t *)hpa2hva(0x40eUL); diff --git a/hypervisor/boot/uefi/uefi_boot.c b/hypervisor/boot/uefi/uefi_boot.c index d1e5fed75..8a89add30 100644 --- a/hypervisor/boot/uefi/uefi_boot.c +++ b/hypervisor/boot/uefi/uefi_boot.c @@ -9,19 +9,6 @@ #include #include -static void efi_spurious_handler(int32_t vector) -{ - if (get_cpu_id() == BOOT_CPU_ID) { - struct acrn_vcpu *vcpu = per_cpu(vcpu, BOOT_CPU_ID); - - if (vcpu != NULL) { - vlapic_set_intr(vcpu, vector, LAPIC_TRIG_EDGE); - } else { - pr_err("%s vcpu or vlapic is not ready, interrupt lost\n", __func__); - } - } -} - static int32_t uefi_sw_loader(struct acrn_vm *vm) { int32_t ret = 0; @@ -56,7 +43,6 @@ static int32_t uefi_sw_loader(struct acrn_vm *vm) int32_t init_vm_boot_info(__unused struct acrn_vm *vm) { vm_sw_loader = uefi_sw_loader; - spurious_handler = (spurious_handler_t)efi_spurious_handler; return 0; } diff --git a/hypervisor/bsp/uefi/cmdline.c b/hypervisor/bsp/cmdline.c similarity index 100% rename from hypervisor/bsp/uefi/cmdline.c rename to hypervisor/bsp/cmdline.c diff --git a/hypervisor/bsp/sbl/const_dmar.c b/hypervisor/bsp/const_dmar.c similarity index 100% rename from hypervisor/bsp/sbl/const_dmar.c rename to hypervisor/bsp/const_dmar.c diff --git a/hypervisor/bsp/firmware_sbl.c b/hypervisor/bsp/firmware_sbl.c new file mode 100644 index 000000000..188c3deac --- /dev/null +++ b/hypervisor/bsp/firmware_sbl.c @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +/* this is for both SBL and ABL platform */ + +#include +#include + +static void sbl_init(void) +{ + /* nothing to do for now */ +} + + +/* @post: return != 0UL */ +static uint64_t sbl_get_ap_trampoline(void) +{ + return e820_alloc_low_memory(CONFIG_LOW_RAM_SIZE); +} + +static void* sbl_get_rsdp(void) +{ + return NULL; +} + +static void sbl_init_irq(void) +{ + CPU_IRQ_ENABLE(); +} + +static struct firmware_operations firmware_sbl_ops = { + .init = sbl_init, + .get_ap_trampoline = sbl_get_ap_trampoline, + .get_rsdp = sbl_get_rsdp, + .init_irq = sbl_init_irq, +}; + +struct firmware_operations* sbl_get_firmware_operations(void) +{ + return &firmware_sbl_ops; +} diff --git a/hypervisor/bsp/firmware_uefi.c b/hypervisor/bsp/firmware_uefi.c new file mode 100644 index 000000000..5acb4b0c4 --- /dev/null +++ b/hypervisor/bsp/firmware_uefi.c @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +/* this is for UEFI platform */ + +#include +#include +#include +#include + +static struct uefi_context uefi_ctx; +static struct lapic_regs uefi_lapic_regs; + +static void uefi_init(void) +{ + static bool uefi_initialized = false; + struct multiboot_info *mbi = NULL; + + if (!uefi_initialized) { + parse_hv_cmdline(); + + mbi = (struct multiboot_info *) hpa2hva(((uint64_t)(uint32_t)boot_regs[1])); + if ((mbi->mi_flags & MULTIBOOT_INFO_HAS_DRIVES) == 0U) { + pr_err("no multiboot drivers for uefi found"); + } else { + memcpy_s(&uefi_ctx, sizeof(struct uefi_context), hpa2hva((uint64_t)mbi->mi_drives_addr), + sizeof(struct uefi_context)); + save_lapic(&uefi_lapic_regs); + } + uefi_initialized = true; + } +} + +const struct uefi_context *get_efi_ctx(void) +{ + uefi_init(); + return &uefi_ctx; +} + +const struct lapic_regs *get_efi_lapic_regs(void) +{ + uefi_init(); + return &uefi_lapic_regs; +} + +static uint64_t uefi_get_ap_trampoline(void) +{ + return (uint64_t)(uefi_ctx.ap_trampoline_buf); +} + +static void* uefi_get_rsdp(void) +{ + return hpa2hva((uint64_t)(uefi_ctx.rsdp)); +} + +static void uefi_spurious_handler(int32_t vector) +{ + if (get_cpu_id() == BOOT_CPU_ID) { + struct acrn_vcpu *vcpu = per_cpu(vcpu, BOOT_CPU_ID); + + if (vcpu != NULL) { + vlapic_set_intr(vcpu, vector, LAPIC_TRIG_EDGE); + } else { + pr_err("%s vcpu or vlapic is not ready, interrupt lost\n", __func__); + } + } +} + +static void uefi_init_irq(void) +{ + spurious_handler = (spurious_handler_t)uefi_spurious_handler; + /* we defer irq enabling till vlapic is ready */ +} + +static struct firmware_operations firmware_uefi_ops = { + .init = uefi_init, + .get_ap_trampoline = uefi_get_ap_trampoline, + .get_rsdp = uefi_get_rsdp, + .init_irq = uefi_init_irq, +}; + +struct firmware_operations* uefi_get_firmware_operations(void) +{ + return &firmware_uefi_ops; +} diff --git a/hypervisor/bsp/firmware_wrapper.c b/hypervisor/bsp/firmware_wrapper.c new file mode 100644 index 000000000..b29af49de --- /dev/null +++ b/hypervisor/bsp/firmware_wrapper.c @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include +#include +#include +#include + +static struct firmware_operations *firmware_ops; + +static bool is_firmware_sbl(void) +{ + bool ret = false; + struct multiboot_info *mbi = NULL; + size_t i; + const char *sbl_candidates[] = { + "Slim BootLoader", + "Intel IOTG/TSD ABL", + }; + + mbi = (struct multiboot_info *)hpa2hva((uint64_t)boot_regs[1]); + if (mbi != NULL) { + if ((mbi->mi_flags & MULTIBOOT_INFO_HAS_LOADER_NAME) != 0U) { + for (i = 0; i < sizeof(sbl_candidates); i++) { + if (strcmp(hpa2hva(mbi->mi_loader_name), sbl_candidates[i]) == 0) { + ret = true; + break; + } + } + } + } + + return ret; +} + +/** + * @pre: this function is called during detect mode which is very early stage, + * other exported interfaces should not be called beforehand. + */ +void init_firmware_operations(void) +{ + if (is_firmware_sbl()) { + firmware_ops = sbl_get_firmware_operations(); + } else { + firmware_ops = uefi_get_firmware_operations(); + } +} + + +/* @pre: firmware_ops->init != NULL */ +void init_firmware(void) +{ +#ifndef CONFIG_CONSTANT_ACPI + acpi_fixup(); +#endif + firmware_ops->init(); +} + +/* @pre: firmware_ops->get_ap_trampoline != NULL */ +uint64_t firmware_get_ap_trampoline(void) +{ + return firmware_ops->get_ap_trampoline(); +} + +/* @pre: firmware_ops->get_rsdp != NULL */ +void *firmware_get_rsdp(void) +{ + return firmware_ops->get_rsdp(); +} + +/* @pre: firmware_ops->init_irq != NULL */ +void firmware_init_irq(void) +{ + return firmware_ops->init_irq(); +} diff --git a/hypervisor/bsp/include/bsp_extern.h b/hypervisor/bsp/include/bsp_extern.h deleted file mode 100644 index 79235e533..000000000 --- a/hypervisor/bsp/include/bsp_extern.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (C) 2018 Intel Corporation. All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -/************************************************************************ - * - * FILE NAME - * - * bsp_extern.h - * - * DESCRIPTION - * - * This file defines the generic BSP interface - * - ************************************************************************/ -#ifndef BSP_EXTERN_H -#define BSP_EXTERN_H - -/* BSP Interfaces */ -void init_bsp(void); -uint64_t bsp_get_ap_trampoline(void); -void *bsp_get_rsdp(void); -void bsp_init_irq(void); - -#ifndef CONFIG_CONSTANT_ACPI -void acpi_fixup(void); -#endif - -#endif /* BSP_EXTERN_H */ diff --git a/hypervisor/bsp/include/firmware.h b/hypervisor/bsp/include/firmware.h new file mode 100644 index 000000000..2b284c932 --- /dev/null +++ b/hypervisor/bsp/include/firmware.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef FIRMWARE_H + +#define FIRMWARE_H + +struct firmware_operations { + void (*init)(void); + uint64_t (*get_ap_trampoline)(void); + void *(*get_rsdp)(void); + void (*init_irq)(void); +}; + +void init_firmware_operations(void); +void init_firmware(void); +uint64_t firmware_get_ap_trampoline(void); +void *firmware_get_rsdp(void); +void firmware_init_irq(void); + +#ifndef CONFIG_CONSTANT_ACPI +void acpi_fixup(void); +#endif + +#endif /* end of include guard: FIRMWARE_H */ diff --git a/hypervisor/bsp/include/firmware_sbl.h b/hypervisor/bsp/include/firmware_sbl.h new file mode 100644 index 000000000..b4de2ce69 --- /dev/null +++ b/hypervisor/bsp/include/firmware_sbl.h @@ -0,0 +1,15 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef FIRMWARE_SBL_H + +#define FIRMWARE_SBL_H + +#include + +struct firmware_operations* sbl_get_firmware_operations(void); + +#endif /* end of include guard: FIRMWARE_SBL_H */ diff --git a/hypervisor/bsp/include/firmware_uefi.h b/hypervisor/bsp/include/firmware_uefi.h new file mode 100644 index 000000000..c9ea65a69 --- /dev/null +++ b/hypervisor/bsp/include/firmware_uefi.h @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +#ifndef FIRMWARE_UEFI_H + +#define FIRMWARE_UEFI_H + +#include + +struct uefi_context { + struct acrn_vcpu_regs vcpu_regs; + void *rsdp; + void *ap_trampoline_buf; +} __packed; + +const struct uefi_context *get_uefi_ctx(void); +const struct lapic_regs *get_uefi_lapic_regs(void); + +struct firmware_operations* uefi_get_firmware_operations(void); + +#endif /* end of include guard: FIRMWARE_UEFI_H */ diff --git a/hypervisor/bsp/include/uefi/uefi.h b/hypervisor/bsp/include/uefi/uefi.h deleted file mode 100644 index b6651681e..000000000 --- a/hypervisor/bsp/include/uefi/uefi.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (C) 2018 Intel Corporation. All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef _UEFI_H_ -#define _UEFI_H_ - -struct efi_context { - struct acrn_vcpu_regs vcpu_regs; - void *rsdp; - void *ap_trampoline_buf; -} __packed; - -const struct efi_context *get_efi_ctx(void); -const struct lapic_regs *get_efi_lapic_regs(void); - -#endif diff --git a/hypervisor/bsp/sbl/sbl.c b/hypervisor/bsp/sbl/sbl.c deleted file mode 100644 index 6f8eacc24..000000000 --- a/hypervisor/bsp/sbl/sbl.c +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (C) 2018 Intel Corporation. All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include -#include - -void init_bsp(void) -{ -#ifndef CONFIG_CONSTANT_ACPI - acpi_fixup(); -#endif -} - -uint64_t bsp_get_ap_trampoline(void) -{ - return e820_alloc_low_memory(CONFIG_LOW_RAM_SIZE); -} - -void *bsp_get_rsdp(void) -{ - return NULL; -} - -void bsp_init_irq(void) -{ - CPU_IRQ_ENABLE(); -} diff --git a/hypervisor/bsp/uefi/uefi.c b/hypervisor/bsp/uefi/uefi.c deleted file mode 100644 index 258da4f50..000000000 --- a/hypervisor/bsp/uefi/uefi.c +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (C) 2018 Intel Corporation. All rights reserved. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include -#include -#include -#include - -static struct efi_context efi_ctx; -static struct lapic_regs uefi_lapic_regs; -static int32_t efi_initialized; - -static void efi_init(void) -{ - struct multiboot_info *mbi = NULL; - - if (boot_regs[0] != MULTIBOOT_INFO_MAGIC) { - pr_err("no multiboot info found"); - } else { - - mbi = (struct multiboot_info *) hpa2hva(((uint64_t)(uint32_t)boot_regs[1])); - if ((mbi->mi_flags & MULTIBOOT_INFO_HAS_DRIVES) == 0U) { - pr_err("no multiboot drivers for uefi found"); - } else { - - memcpy_s(&efi_ctx, sizeof(struct efi_context), hpa2hva((uint64_t)mbi->mi_drives_addr), - sizeof(struct efi_context)); - save_lapic(&uefi_lapic_regs); - efi_initialized = 1; - } - } -} - -void *bsp_get_rsdp(void) -{ - if (!efi_initialized) { - efi_init(); - } - - return hpa2hva((uint64_t)efi_ctx.rsdp); -} - -uint64_t bsp_get_ap_trampoline(void) -{ - return (uint64_t)efi_ctx.ap_trampoline_buf; -} - -const struct efi_context *get_efi_ctx(void) -{ - return &efi_ctx; -} - -const struct lapic_regs *get_efi_lapic_regs(void) -{ - return &uefi_lapic_regs; -} - -void bsp_init_irq(void) -{ -} - -void init_bsp(void) -{ -#ifndef CONFIG_CONSTANT_ACPI - acpi_fixup(); -#endif - parse_hv_cmdline(); - - if (!efi_initialized) - efi_init(); -} diff --git a/hypervisor/include/arch/x86/guest/vm.h b/hypervisor/include/arch/x86/guest/vm.h index e69053ad5..b99fc6d9a 100644 --- a/hypervisor/include/arch/x86/guest/vm.h +++ b/hypervisor/include/arch/x86/guest/vm.h @@ -17,7 +17,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/hypervisor/include/arch/x86/multiboot.h b/hypervisor/include/arch/x86/multiboot.h index c160e3c3c..33a216b8f 100644 --- a/hypervisor/include/arch/x86/multiboot.h +++ b/hypervisor/include/arch/x86/multiboot.h @@ -12,6 +12,7 @@ #define MULTIBOOT_INFO_HAS_MODS 0x00000008U #define MULTIBOOT_INFO_HAS_MMAP 0x00000040U #define MULTIBOOT_INFO_HAS_DRIVES 0x00000080U +#define MULTIBOOT_INFO_HAS_LOADER_NAME 0x00000200U struct acrn_vm; struct multiboot_info {