Skip to content

Commit 72cf390

Browse files
committed
Revert "Partially revert b688880"
This reverts commit 91f4820.
1 parent a057928 commit 72cf390

File tree

4 files changed

+349
-1
lines changed

4 files changed

+349
-1
lines changed

src/acpi.c

Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <efi.h>
2+
#include <io.h>
23
#include <printf.h>
34
#include "csmwrap.h"
45

@@ -35,6 +36,312 @@ void *uacpi_kernel_map(uacpi_phys_addr addr, EFI_UNUSED uacpi_size len) {
3536
void uacpi_kernel_unmap(EFI_UNUSED void *ptr, EFI_UNUSED uacpi_size len) {
3637
}
3738

39+
uacpi_status uacpi_kernel_pci_device_open(uacpi_pci_address address, uacpi_handle *out_handle) {
40+
void *handle;
41+
if (gBS->AllocatePool(EfiLoaderData, sizeof(uacpi_pci_address), &handle) != EFI_SUCCESS) {
42+
return UACPI_STATUS_OUT_OF_MEMORY;
43+
}
44+
45+
memcpy(handle, &address, sizeof(uacpi_pci_address));
46+
*out_handle = handle;
47+
return UACPI_STATUS_OK;
48+
}
49+
50+
void uacpi_kernel_pci_device_close(uacpi_handle handle) {
51+
gBS->FreePool(handle);
52+
}
53+
54+
uacpi_status uacpi_kernel_pci_read8(uacpi_handle device, uacpi_size offset, uacpi_u8 *value) {
55+
uacpi_pci_address *address = (uacpi_pci_address *)device;
56+
*value = pciConfigReadByte(address->bus, address->device, address->function, offset);
57+
return UACPI_STATUS_OK;
58+
}
59+
60+
uacpi_status uacpi_kernel_pci_read16(uacpi_handle device, uacpi_size offset, uacpi_u16 *value) {
61+
uacpi_pci_address *address = (uacpi_pci_address *)device;
62+
*value = pciConfigReadWord(address->bus, address->device, address->function, offset);
63+
return UACPI_STATUS_OK;
64+
}
65+
66+
uacpi_status uacpi_kernel_pci_read32(uacpi_handle device, uacpi_size offset, uacpi_u32 *value) {
67+
uacpi_pci_address *address = (uacpi_pci_address *)device;
68+
*value = pciConfigReadDWord(address->bus, address->device, address->function, offset);
69+
return UACPI_STATUS_OK;
70+
}
71+
72+
uacpi_status uacpi_kernel_pci_write8(uacpi_handle device, uacpi_size offset, uacpi_u8 value) {
73+
uacpi_pci_address *address = (uacpi_pci_address *)device;
74+
pciConfigWriteByte(address->bus, address->device, address->function, offset, value);
75+
return UACPI_STATUS_OK;
76+
}
77+
78+
uacpi_status uacpi_kernel_pci_write16(uacpi_handle device, uacpi_size offset, uacpi_u16 value) {
79+
uacpi_pci_address *address = (uacpi_pci_address *)device;
80+
pciConfigWriteWord(address->bus, address->device, address->function, offset, value);
81+
return UACPI_STATUS_OK;
82+
}
83+
84+
uacpi_status uacpi_kernel_pci_write32(uacpi_handle device, uacpi_size offset, uacpi_u32 value) {
85+
uacpi_pci_address *address = (uacpi_pci_address *)device;
86+
pciConfigWriteDWord(address->bus, address->device, address->function, offset, value);
87+
return UACPI_STATUS_OK;
88+
}
89+
90+
struct mapped_io {
91+
uacpi_io_addr base;
92+
uacpi_size len;
93+
};
94+
95+
uacpi_status uacpi_kernel_io_map(uacpi_io_addr base, uacpi_size len, uacpi_handle *out_handle) {
96+
void *handle;
97+
if (gBS->AllocatePool(EfiLoaderData, sizeof(uacpi_pci_address), &handle) != EFI_SUCCESS) {
98+
return UACPI_STATUS_OUT_OF_MEMORY;
99+
}
100+
101+
struct mapped_io *io = (struct mapped_io *)handle;
102+
io->base = base;
103+
io->len = len;
104+
105+
*out_handle = handle;
106+
return UACPI_STATUS_OK;
107+
}
108+
109+
void uacpi_kernel_io_unmap(uacpi_handle handle) {
110+
gBS->FreePool(handle);
111+
}
112+
113+
uacpi_status uacpi_kernel_io_read8(uacpi_handle handle, uacpi_size offset, uacpi_u8 *out_value) {
114+
struct mapped_io *io = (struct mapped_io *)handle;
115+
if (offset >= io->len) {
116+
return UACPI_STATUS_INVALID_ARGUMENT;
117+
}
118+
119+
*out_value = inb(io->base + offset);
120+
return UACPI_STATUS_OK;
121+
}
122+
123+
uacpi_status uacpi_kernel_io_read16(uacpi_handle handle, uacpi_size offset, uacpi_u16 *out_value) {
124+
struct mapped_io *io = (struct mapped_io *)handle;
125+
if (offset >= io->len) {
126+
return UACPI_STATUS_INVALID_ARGUMENT;
127+
}
128+
129+
*out_value = inw(io->base + offset);
130+
return UACPI_STATUS_OK;
131+
}
132+
133+
uacpi_status uacpi_kernel_io_read32(uacpi_handle handle, uacpi_size offset, uacpi_u32 *out_value) {
134+
struct mapped_io *io = (struct mapped_io *)handle;
135+
if (offset >= io->len) {
136+
return UACPI_STATUS_INVALID_ARGUMENT;
137+
}
138+
139+
*out_value = inl(io->base + offset);
140+
return UACPI_STATUS_OK;
141+
}
142+
143+
uacpi_status uacpi_kernel_io_write8(uacpi_handle handle, uacpi_size offset, uacpi_u8 in_value) {
144+
struct mapped_io *io = (struct mapped_io *)handle;
145+
if (offset >= io->len) {
146+
return UACPI_STATUS_INVALID_ARGUMENT;
147+
}
148+
149+
outb(io->base + offset, in_value);
150+
return UACPI_STATUS_OK;
151+
}
152+
153+
uacpi_status uacpi_kernel_io_write16(uacpi_handle handle, uacpi_size offset, uacpi_u16 in_value) {
154+
struct mapped_io *io = (struct mapped_io *)handle;
155+
if (offset >= io->len) {
156+
return UACPI_STATUS_INVALID_ARGUMENT;
157+
}
158+
159+
outw(io->base + offset, in_value);
160+
return UACPI_STATUS_OK;
161+
}
162+
163+
uacpi_status uacpi_kernel_io_write32(uacpi_handle handle, uacpi_size offset, uacpi_u32 in_value) {
164+
struct mapped_io *io = (struct mapped_io *)handle;
165+
if (offset >= io->len) {
166+
return UACPI_STATUS_INVALID_ARGUMENT;
167+
}
168+
169+
outl(io->base + offset, in_value);
170+
return UACPI_STATUS_OK;
171+
}
172+
173+
uacpi_handle uacpi_kernel_create_spinlock(void) {
174+
void *handle;
175+
if (gBS->AllocatePool(EfiLoaderData, 0x1, &handle) != EFI_SUCCESS) {
176+
return NULL;
177+
}
178+
179+
return handle;
180+
}
181+
182+
void uacpi_kernel_free_spinlock(uacpi_handle handle) {
183+
gBS->FreePool(handle);
184+
}
185+
186+
uacpi_cpu_flags uacpi_kernel_lock_spinlock(uacpi_handle handle) {
187+
(void)handle;
188+
return 0;
189+
}
190+
191+
void uacpi_kernel_unlock_spinlock(uacpi_handle handle, uacpi_cpu_flags cpu_flags) {
192+
(void)handle;
193+
(void)cpu_flags;
194+
}
195+
196+
uacpi_handle uacpi_kernel_create_event(void) {
197+
void *handle;
198+
if (gBS->AllocatePool(EfiLoaderData, 0x1, &handle) != EFI_SUCCESS) {
199+
return NULL;
200+
}
201+
202+
return handle;
203+
}
204+
205+
void uacpi_kernel_free_event(uacpi_handle handle) {
206+
gBS->FreePool(handle);
207+
}
208+
209+
uacpi_bool uacpi_kernel_wait_for_event(uacpi_handle handle, uacpi_u16 timeout) {
210+
(void)handle;
211+
(void)timeout;
212+
return UACPI_TRUE;
213+
}
214+
215+
void uacpi_kernel_signal_event(uacpi_handle handle) {
216+
(void)handle;
217+
}
218+
219+
void uacpi_kernel_reset_event(uacpi_handle handle) {
220+
(void)handle;
221+
}
222+
223+
// Borrowed from https://github.com/limine-bootloader/limine/blob/v9.x/common/lib/time.c
224+
225+
static int get_jdn(int days, int months, int years) {
226+
return (1461 * (years + 4800 + (months - 14) / 12)) / 4
227+
+ (367 * (months - 2 - 12 * ((months - 14) / 12))) / 12
228+
- (3 * ((years + 4900 + (months - 14) / 12) / 100)) / 4
229+
+ days - 32075;
230+
}
231+
232+
#define NS_PER_S UINT64_C(1000000000)
233+
234+
static uint64_t get_unix_epoch(
235+
uint16_t nanoseconds, uint8_t seconds, uint8_t minutes, uint8_t hours,
236+
uint8_t days, uint8_t months, uint16_t years) {
237+
uint64_t jdn_current = get_jdn(days, months, years);
238+
uint64_t jdn_1970 = get_jdn(1, 1, 1970);
239+
uint64_t jdn_diff = jdn_current - jdn_1970;
240+
241+
return (jdn_diff * 60 * 60 * 24 * NS_PER_S)
242+
+ (hours * 3600 * NS_PER_S)
243+
+ (minutes * 60 * NS_PER_S)
244+
+ (seconds * NS_PER_S)
245+
+ nanoseconds;
246+
}
247+
248+
uacpi_u64 uacpi_kernel_get_nanoseconds_since_boot(void) {
249+
EFI_TIME time;
250+
if (gRT->GetTime(&time, NULL) != EFI_SUCCESS) {
251+
return 0;
252+
}
253+
254+
uint64_t boot_time = get_unix_epoch(
255+
gTimeAtBoot.Nanosecond, gTimeAtBoot.Second, gTimeAtBoot.Minute,
256+
gTimeAtBoot.Hour, gTimeAtBoot.Day, gTimeAtBoot.Month, gTimeAtBoot.Year);
257+
uint64_t current_time = get_unix_epoch(
258+
time.Nanosecond, time.Second, time.Minute, time.Hour, time.Day, time.Month, time.Year);
259+
260+
return current_time - boot_time;
261+
}
262+
263+
void uacpi_kernel_stall(uacpi_u8 usec) {
264+
gBS->Stall(usec);
265+
}
266+
267+
void uacpi_kernel_sleep(uacpi_u64 msec) {
268+
gBS->Stall(msec * 1000);
269+
}
270+
271+
uacpi_thread_id uacpi_kernel_get_thread_id(void) {
272+
return (uacpi_thread_id)1;
273+
}
274+
275+
uacpi_status uacpi_kernel_handle_firmware_request(uacpi_firmware_request *request) {
276+
(void)request;
277+
return UACPI_STATUS_UNIMPLEMENTED;
278+
}
279+
280+
uacpi_status uacpi_kernel_install_interrupt_handler(
281+
uacpi_u32 irq, uacpi_interrupt_handler handler, uacpi_handle ctx,
282+
uacpi_handle *out_irq_handle) {
283+
(void)irq;
284+
(void)handler;
285+
(void)ctx;
286+
(void)out_irq_handle;
287+
return UACPI_STATUS_OK;
288+
}
289+
290+
uacpi_status uacpi_kernel_uninstall_interrupt_handler(uacpi_interrupt_handler handler, uacpi_handle irq_handle) {
291+
(void)handler;
292+
(void)irq_handle;
293+
return UACPI_STATUS_UNIMPLEMENTED;
294+
}
295+
296+
uacpi_status uacpi_kernel_schedule_work(uacpi_work_type work_type, uacpi_work_handler handler, uacpi_handle ctx) {
297+
(void)work_type;
298+
(void)handler;
299+
(void)ctx;
300+
return UACPI_STATUS_UNIMPLEMENTED;
301+
}
302+
303+
uacpi_status uacpi_kernel_wait_for_work_completion(void) {
304+
return UACPI_STATUS_UNIMPLEMENTED;
305+
}
306+
307+
void *uacpi_kernel_alloc(uacpi_size size) {
308+
void *result;
309+
if (gBS->AllocatePool(EfiLoaderData, size, &result) != EFI_SUCCESS) {
310+
return NULL;
311+
}
312+
313+
return result;
314+
}
315+
316+
void uacpi_kernel_free(void *mem) {
317+
if (mem != NULL) {
318+
gBS->FreePool(mem);
319+
}
320+
}
321+
322+
uacpi_handle uacpi_kernel_create_mutex(void) {
323+
void *handle;
324+
if (gBS->AllocatePool(EfiLoaderData, 0x1, &handle) != EFI_SUCCESS) {
325+
return NULL;
326+
}
327+
328+
return handle;
329+
}
330+
331+
void uacpi_kernel_free_mutex(uacpi_handle handle) {
332+
gBS->FreePool(handle);
333+
}
334+
335+
uacpi_status uacpi_kernel_acquire_mutex(uacpi_handle handle, uacpi_u16 timeout) {
336+
(void)handle;
337+
(void)timeout;
338+
return UACPI_STATUS_OK;
339+
}
340+
341+
void uacpi_kernel_release_mutex(uacpi_handle handle) {
342+
(void)handle;
343+
}
344+
38345
uacpi_status uacpi_kernel_get_rsdp(uacpi_phys_addr *rsdp) {
39346
if (!g_rsdp) {
40347
return UACPI_STATUS_NOT_FOUND;
@@ -99,7 +406,38 @@ bool acpi_init(struct csmwrap_priv *priv) {
99406
return false;
100407
}
101408

409+
bool acpi_full_init(void) {
410+
enum uacpi_status uacpi_status;
411+
412+
uacpi_status = uacpi_initialize(UACPI_FLAG_NO_ACPI_MODE);
413+
if (uacpi_status != UACPI_STATUS_OK) {
414+
printf("uACPI initialization failed: %s\n", uacpi_status_to_string(uacpi_status));
415+
return false;
416+
}
417+
418+
uacpi_status = uacpi_namespace_load();
419+
if (uacpi_status != UACPI_STATUS_OK) {
420+
printf("uACPI namespace load failed: %s\n", uacpi_status_to_string(uacpi_status));
421+
return false;
422+
}
423+
424+
uacpi_status = uacpi_namespace_initialize();
425+
if (uacpi_status != UACPI_STATUS_OK) {
426+
printf("uACPI namespace initialization failed: %s\n", uacpi_status_to_string(uacpi_status));
427+
return false;
428+
}
429+
430+
if (early_table_buffer != NULL) {
431+
gBS->FreePool(early_table_buffer);
432+
early_table_buffer = NULL;
433+
}
434+
435+
return true;
436+
}
437+
102438
void acpi_prepare_exitbs(void) {
439+
uacpi_state_reset();
440+
103441
if (early_table_buffer != NULL) {
104442
gBS->FreePool(early_table_buffer);
105443
early_table_buffer = NULL;

src/csmwrap.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const char *banner = "CSMWrap Version " BUILD_VERSION "\n"
1919

2020
EFI_SYSTEM_TABLE *gST;
2121
EFI_BOOT_SERVICES *gBS;
22+
EFI_RUNTIME_SERVICES *gRT;
23+
EFI_TIME gTimeAtBoot;
2224

2325
struct csmwrap_priv priv = {
2426
.csm_bin = Csm16_bin,
@@ -94,6 +96,12 @@ EFI_STATUS efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
9496

9597
gST = SystemTable;
9698
gBS = SystemTable->BootServices;
99+
gRT = SystemTable->RuntimeServices;
100+
101+
if (gRT->GetTime(&gTimeAtBoot, NULL) != EFI_SUCCESS) {
102+
printf("Failed to query current time\n");
103+
return -1;
104+
}
97105

98106
printf("%s", banner);
99107

src/csmwrap.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
extern EFI_SYSTEM_TABLE *gST;
1414
extern EFI_BOOT_SERVICES *gBS;
15+
extern EFI_RUNTIME_SERVICES *gRT;
16+
extern EFI_TIME gTimeAtBoot;
1517

1618
enum csmwrap_video_type {
1719
CSMWRAP_VIDEO_NONE,
@@ -40,6 +42,7 @@ struct csmwrap_priv {
4042
extern int unlock_bios_region();
4143
extern int build_coreboot_table(struct csmwrap_priv *priv);
4244
bool acpi_init(struct csmwrap_priv *priv);
45+
bool acpi_full_init(void);
4346
void acpi_prepare_exitbs(void);
4447
int build_e820_map(struct csmwrap_priv *priv, EFI_MEMORY_DESCRIPTOR *memory_map, UINTN memory_map_size, UINTN descriptor_size);
4548
int apply_intel_platform_workarounds(void);

0 commit comments

Comments
 (0)