Skip to content

Commit 4a65dc9

Browse files
committed
stand/efi/loader: ACPICA subset compilation
Description: Refactored ACPICA for partial initialization in the loader. This work is the preface for Lua bindings. Work on this commit involves: Subset ACPICA for loader land: For our Lua bindings, we need to be able to walk the ACPI namespace and evaluate objects. To do so, we need AcpiInitializeSubsystem, AcpiInitializeTables, AcpiEnableSubsystem, and AcpiLoadTables. Notes: -AcpiEnableSubsystem is initialized in reduced hardware mode, but with events enabled. This is so that we do not need to pull in all of ACPI_HARDWARE, but can evaluate objects. -Stubbed out Osd functions required for this limited initialization. -Conditionally exclude functions to make a minimal subset designed around loader's/lua binding's needs: --Exclude AcpiInitializeObjects, as we do not need this for walking the namespace and evaluating objects. (utxfinit.c) --Exclude notify handlers or fixed event handlers. (evxface.c) --Stub out print functions, as the loader has no console or standard output. (osunixxf.c) --Stub out AcpiOsSleep and AcpiOsGetTimer, as the loader is not multi-threaded and does not implement timers. (osunixxf.c)
1 parent 004ce88 commit 4a65dc9

File tree

13 files changed

+1843
-5
lines changed

13 files changed

+1843
-5
lines changed

stand/efi/acpica/Osd/OsdMemory.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*-
2+
* Copyright (c) 2025 Kayla Powell <[email protected]>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24+
* SUCH DAMAGE.
25+
*/
26+
27+
#include <efi.h>
28+
#include <acpi.h>
29+
#include <acpi_detect.h>
30+
31+
void *
32+
AcpiOsAllocate(ACPI_SIZE Size)
33+
{
34+
return (malloc(Size));
35+
}
36+
37+
void
38+
AcpiOsFree(void *Memory)
39+
{
40+
free(Memory);
41+
}
42+
43+
void *
44+
AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS PhysicalAddress, ACPI_SIZE Length)
45+
{
46+
return (void *)(PhysicalAddress);
47+
}
48+
49+
void
50+
AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length)
51+
{
52+
/* No-op as we never mapped any memory. */
53+
}
54+
55+
ACPI_PHYSICAL_ADDRESS
56+
AcpiOsGetRootPointer (
57+
void)
58+
{
59+
if (!rsdp) {
60+
return (0);
61+
}
62+
63+
return (ACPI_PHYSICAL_ADDRESS)(uintptr_t)(rsdp);
64+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*-
2+
* Copyright (c) 2025 Kayla Powell <[email protected]>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24+
* SUCH DAMAGE.
25+
*/
26+
27+
#ifndef INIT_ACPI_H
28+
#define INIT_ACPI_H
29+
30+
#include <efi.h>
31+
#include <efilib.h>
32+
33+
#define ACPI_LOADER 0x00020000
34+
35+
#include <contrib/dev/acpica/include/acpi.h>
36+
37+
ACPI_STATUS acpi_Startup(void);
38+
int init_acpi(void);
39+
int acpi_is_initialized(void);
40+
41+
#endif /* ACPI_H */

stand/efi/acpica/init_acpi.c

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*-
2+
* Copyright (c) 2025 Kayla Powell <[email protected]>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24+
* SUCH DAMAGE.
25+
*/
26+
27+
#include <machine/_inttypes.h>
28+
29+
#include <efi.h>
30+
31+
#include <stand.h>
32+
33+
#include <acpi.h>
34+
#include <accommon.h>
35+
#include <acoutput.h>
36+
37+
#include <init_acpi.h>
38+
39+
#define _COMPONENT ACPI_LOADER
40+
ACPI_MODULE_NAME("init_acpi");
41+
42+
/* Singleton protection. */
43+
static int acpi_inited = 0;
44+
45+
/*
46+
* Initialize a smaller subset of the ACPI subsystem in the
47+
* loader. This subset is enough for users to evaluate
48+
* objects on the namespace, and includes event management.
49+
*
50+
* This function brings up the ACPICA core in four stages:
51+
* 1. Initialize the ACPICA subsystem.
52+
* 2. Initialize and validate ACPI root tables.
53+
* 3. Enable the subsystem (without full hardware mode, but events
54+
* enabled).
55+
* 4. Load ACPI tables into the namespace.
56+
*
57+
* Returns ACPI_STATUS; failure indicates issue reading RSDT/XSDT.
58+
* Please refer to your motherboard manual to verify ACPI support.
59+
*/
60+
ACPI_STATUS
61+
acpi_Startup(void)
62+
{
63+
ACPI_STATUS status;
64+
65+
ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
66+
67+
/* Initialize the ACPICA subsystem. */
68+
if (ACPI_FAILURE(status = AcpiInitializeSubsystem())) {
69+
printf("ACPI: Could not initialize Subsystem: %s\n",
70+
AcpiFormatException(status));
71+
return_VALUE(status);
72+
}
73+
74+
/* Initialize the ACPICA tables. */
75+
if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) {
76+
printf("ACPI: Table initialisation failed: %s\n",
77+
AcpiFormatException(status));
78+
return_VALUE(status);
79+
}
80+
81+
/* Enable the ACPI subsystem. */
82+
uint32_t flags = ACPI_REDUCED_HARDWARE | ACPI_NO_ACPI_ENABLE;
83+
if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) {
84+
printf("ACPI: Enable subsystem failed: %s\n",
85+
AcpiFormatException(status));
86+
return_VALUE(status);
87+
}
88+
89+
/* Create the ACPI namespace from ACPI tables. */
90+
if (ACPI_FAILURE(status = AcpiLoadTables())) {
91+
printf("ACPI: Load tables failed: %s\n",
92+
AcpiFormatException(status));
93+
return_VALUE(status);
94+
}
95+
96+
return_VALUE(status);
97+
}
98+
99+
/*
100+
* Initialize ACPI once for the loader.
101+
*
102+
* This is a singleton entry point. Safe to call multiple times;
103+
* subsequent calls will return success immediately.
104+
*
105+
* Returns 0 on success, ENXIO if ACPI could not be initialized.
106+
*/
107+
int
108+
init_acpi(void)
109+
{
110+
ACPI_STATUS status;
111+
112+
ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
113+
114+
if (acpi_inited) {
115+
return (0);
116+
}
117+
118+
if (ACPI_FAILURE(status = acpi_Startup())) {
119+
printf("ACPI: Loader initialization failed with status %s\n",
120+
AcpiFormatException(status));
121+
return (ENXIO);
122+
}
123+
124+
acpi_inited = 1;
125+
126+
return (0);
127+
}
128+
129+
/*
130+
* Confirm ACPI initialization.
131+
*/
132+
int
133+
acpi_is_initialized(void)
134+
{
135+
return (acpi_inited);
136+
}

0 commit comments

Comments
 (0)