From f7b9cd43a797274db7730233a296bc2893df4f44 Mon Sep 17 00:00:00 2001 From: kettenis Date: Tue, 26 Mar 2024 22:26:04 +0000 Subject: [PATCH] Implement support for the RISC-V UEFI Boot Protocol. This provides us the boot hart ID for firmware that doesn't provide it through the device tree such as the EDK2-based firmware for the Sophgo SG2042 SoC. ok patrick@, miod@ --- sys/arch/riscv64/stand/efiboot/Makefile | 4 +- sys/arch/riscv64/stand/efiboot/conf.c | 4 +- sys/arch/riscv64/stand/efiboot/efiboot.c | 9 ++- sys/arch/riscv64/stand/efiboot/efiboot.h | 4 +- sys/arch/riscv64/stand/efiboot/efiriscv.c | 67 +++++++++++++++++++++++ 5 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 sys/arch/riscv64/stand/efiboot/efiriscv.c diff --git a/sys/arch/riscv64/stand/efiboot/Makefile b/sys/arch/riscv64/stand/efiboot/Makefile index e0daecf447d..4ebfe813455 100644 --- a/sys/arch/riscv64/stand/efiboot/Makefile +++ b/sys/arch/riscv64/stand/efiboot/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.6 2023/09/06 01:47:37 jsg Exp $ +# $OpenBSD: Makefile,v 1.7 2024/03/26 22:26:04 kettenis Exp $ NOMAN= # @@ -10,7 +10,7 @@ INSTALL_STRIP= BINDIR= /usr/mdec SRCS= start.S self_reloc.c efiboot.c conf.c exec.c SRCS+= efidev.c efipxe.c efirng.c fdt.c -SRCS+= softraid_riscv64.c +SRCS+= softraid_riscv64.c efiriscv.c S= ${.CURDIR}/../../../.. EFIDIR= ${S}/stand/efi diff --git a/sys/arch/riscv64/stand/efiboot/conf.c b/sys/arch/riscv64/stand/efiboot/conf.c index 12e685232aa..3396162c80f 100644 --- a/sys/arch/riscv64/stand/efiboot/conf.c +++ b/sys/arch/riscv64/stand/efiboot/conf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: conf.c,v 1.2 2023/01/16 21:32:12 kn Exp $ */ +/* $OpenBSD: conf.c,v 1.3 2024/03/26 22:26:04 kettenis Exp $ */ /* * Copyright (c) 1996 Michael Shalayeff @@ -46,7 +46,7 @@ #include "efipxe.h" #include "softraid_riscv64.h" -const char version[] = "1.5"; +const char version[] = "1.6"; int debug = 0; struct fs_ops file_system[] = { diff --git a/sys/arch/riscv64/stand/efiboot/efiboot.c b/sys/arch/riscv64/stand/efiboot/efiboot.c index 79a26319364..38321fe3b27 100644 --- a/sys/arch/riscv64/stand/efiboot/efiboot.c +++ b/sys/arch/riscv64/stand/efiboot/efiboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: efiboot.c,v 1.6 2023/07/05 09:25:55 kettenis Exp $ */ +/* $OpenBSD: efiboot.c,v 1.7 2024/03/26 22:26:04 kettenis Exp $ */ /* * Copyright (c) 2015 YASUOKA Masahiko @@ -492,6 +492,7 @@ efi_makebootargs(char *bootargs, int howto) u_char zero[8] = { 0 }; uint64_t uefi_system_table = htobe64((uintptr_t)ST); uint32_t boothowto = htobe32(howto); + int32_t hartid; EFI_PHYSICAL_ADDRESS addr; void *node; size_t len; @@ -523,6 +524,12 @@ efi_makebootargs(char *bootargs, int howto) if (!node) return NULL; + hartid = efi_get_boot_hart_id(); + if (hartid >= 0) { + hartid = htobe32(hartid); + fdt_node_add_property(node, "boot-hartid", &hartid, 4); + } + len = strlen(bootargs) + 1; fdt_node_add_property(node, "bootargs", bootargs, len); fdt_node_add_property(node, "openbsd,boothowto", diff --git a/sys/arch/riscv64/stand/efiboot/efiboot.h b/sys/arch/riscv64/stand/efiboot/efiboot.h index 18fea5026e5..92830616f2a 100644 --- a/sys/arch/riscv64/stand/efiboot/efiboot.h +++ b/sys/arch/riscv64/stand/efiboot/efiboot.h @@ -1,4 +1,4 @@ -/* $OpenBSD: efiboot.h,v 1.1 2021/04/28 19:01:00 drahn Exp $ */ +/* $OpenBSD: efiboot.h,v 1.2 2024/03/26 22:26:04 kettenis Exp $ */ /* * Copyright (c) 2015 YASUOKA Masahiko @@ -28,3 +28,5 @@ void efi_fb_probe(struct consdev *); void efi_fb_init(struct consdev *); int efi_fb_getc(dev_t); void efi_fb_putc(dev_t, int); + +int32_t efi_get_boot_hart_id(void); diff --git a/sys/arch/riscv64/stand/efiboot/efiriscv.c b/sys/arch/riscv64/stand/efiboot/efiriscv.c new file mode 100644 index 00000000000..1a60c086d73 --- /dev/null +++ b/sys/arch/riscv64/stand/efiboot/efiriscv.c @@ -0,0 +1,67 @@ +/* $OpenBSD: efiriscv.c,v 1.1 2024/03/26 22:26:04 kettenis Exp $ */ + +/* + * Copyright (c) 2024 Mark Kettenis + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include +#include + +#include "libsa.h" + +#include "efiboot.h" + +extern EFI_BOOT_SERVICES *BS; + +/* RISC-V UEFI Boot Protocol */ + +#define RISCV_EFI_BOOT_PROTOCOL_GUID \ + { 0xccd15fec, 0x6f73, 0x4eec, { 0x83, 0x95, 0x3e, 0x69, 0xe4, 0xb9, 0x40, 0xbf } } + +INTERFACE_DECL(_RISCV_EFI_BOOT_PROTOCOL); + +typedef +EFI_STATUS +(EFIAPI *EFI_GET_BOOT_HARTID) ( + IN struct _RISCV_EFI_BOOT_PROTOCOL *This, + OUT UINTN *BootHartId + ); + +typedef struct _RISCV_EFI_BOOT_PROTOCOL { + UINT64 Revision; + EFI_GET_BOOT_HARTID GetBootHartId; +} RISCV_EFI_BOOT_PROTOCOL; + +static EFI_GUID riscv_guid = RISCV_EFI_BOOT_PROTOCOL_GUID; + +int32_t +efi_get_boot_hart_id(void) +{ + EFI_STATUS status; + RISCV_EFI_BOOT_PROTOCOL *riscv = NULL; + UINTN hartid; + + status = BS->LocateProtocol(&riscv_guid, NULL, (void **)&riscv); + if (riscv == NULL || EFI_ERROR(status)) + return -1; + + status = riscv->GetBootHartId(riscv, &hartid); + if (status == EFI_SUCCESS) + return hartid; + + return -1; +} -- 2.20.1