1 /* $OpenBSD: efiriscv.c,v 1.1 2024/03/26 22:26:04 kettenis Exp $ */
2
3 /*
4 * Copyright (c) 2024 Mark Kettenis <kettenis@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/param.h>
20
21 #include <efi.h>
22 #include <efiapi.h>
23
24 #include "libsa.h"
25
26 #include "efiboot.h"
27
28 extern EFI_BOOT_SERVICES *BS;
29
30 /* RISC-V UEFI Boot Protocol */
31
32 #define RISCV_EFI_BOOT_PROTOCOL_GUID \
33 { 0xccd15fec, 0x6f73, 0x4eec, { 0x83, 0x95, 0x3e, 0x69, 0xe4, 0xb9, 0x40, 0xbf } }
34
35 INTERFACE_DECL(_RISCV_EFI_BOOT_PROTOCOL);
36
37 typedef
38 EFI_STATUS
39 (EFIAPI *EFI_GET_BOOT_HARTID) (
40 IN struct _RISCV_EFI_BOOT_PROTOCOL *This,
41 OUT UINTN *BootHartId
42 );
43
44 typedef struct _RISCV_EFI_BOOT_PROTOCOL {
45 UINT64 Revision;
46 EFI_GET_BOOT_HARTID GetBootHartId;
47 } RISCV_EFI_BOOT_PROTOCOL;
48
49 static EFI_GUID riscv_guid = RISCV_EFI_BOOT_PROTOCOL_GUID;
50
51 int32_t
efi_get_boot_hart_id(void)52 efi_get_boot_hart_id(void)
53 {
54 EFI_STATUS status;
55 RISCV_EFI_BOOT_PROTOCOL *riscv = NULL;
56 UINTN hartid;
57
58 status = BS->LocateProtocol(&riscv_guid, NULL, (void **)&riscv);
59 if (riscv == NULL || EFI_ERROR(status))
60 return -1;
61
62 status = riscv->GetBootHartId(riscv, &hartid);
63 if (status == EFI_SUCCESS)
64 return hartid;
65
66 return -1;
67 }
68