diff --git a/crates/stela-drivers/build/native.rs b/crates/stela-drivers/build/native.rs index fbe96ff149..f5a5c45478 100644 --- a/crates/stela-drivers/build/native.rs +++ b/crates/stela-drivers/build/native.rs @@ -284,6 +284,7 @@ impl Linux { for (name, source) in [ ("linux_compiler_abi", "abi_test.c"), ("linux_acpi_client", "acpi_client_test.c"), + ("linux_acpi_dsm", "acpi_dsm_test.c"), ("linux_random", "random_test.c"), ("linux_seq_output", "seq_output_test.c"), ("linux_drm_mm", "drm_mm_test.c"), diff --git a/crates/stela-drivers/src/gpu/compat/linux/asm/acenv.h b/crates/stela-drivers/src/gpu/compat/linux/asm/acenv.h new file mode 100644 index 0000000000..0c61c29e8b --- /dev/null +++ b/crates/stela-drivers/src/gpu/compat/linux/asm/acenv.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: MIT */ +#ifndef _ASM_STELA_ACENV_H +#define _ASM_STELA_ACENV_H +/* Kernel clients consume public ACPICA types only. The boot-owned interpreter + * has its own environment; no cache/global-lock/interpreter operations are + * supplied or silently emulated by this client architecture boundary. */ +#include +/* Generic ACPICA defaults would claim a global-lock acquisition or silently + * omit a cache flush. Those interpreter operations are not client services. */ +void stela_linux_acpi_interpreter_operation(void) + __attribute__((error("ACPICA interpreter operations require the boot-owned Session"))); +#define ACPI_ACQUIRE_GLOBAL_LOCK(pointer, acquired) \ + do { stela_linux_acpi_interpreter_operation(); } while (0) +#define ACPI_RELEASE_GLOBAL_LOCK(pointer, pending) \ + do { stela_linux_acpi_interpreter_operation(); } while (0) +#define ACPI_FLUSH_CPU_CACHE() stela_linux_acpi_interpreter_operation() +#endif diff --git a/crates/stela-drivers/src/gpu/compat/linux/asm/acpi.h b/crates/stela-drivers/src/gpu/compat/linux/asm/acpi.h new file mode 100644 index 0000000000..d1cbd3270b --- /dev/null +++ b/crates/stela-drivers/src/gpu/compat/linux/asm/acpi.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: MIT */ +#ifndef _ASM_STELA_ACPI_H +#define _ASM_STELA_ACPI_H +/* Architecture declarations for Linux ACPI clients. They do not expose the + * interpreter or install native services. Missing providers stay link/admission + * requirements, never successful zero root addresses or ignored mutations. */ +#include +#include +extern int acpi_disabled; +extern int acpi_noirq; +extern int acpi_pci_disabled; +#define ACPI_HAVE_ARCH_SET_ROOT_POINTER +#define ACPI_HAVE_ARCH_GET_ROOT_POINTER +void acpi_arch_set_root_pointer(u64 address); +u64 acpi_arch_get_root_pointer(void); +#endif diff --git a/crates/stela-drivers/src/gpu/compat/linux/kernel/acpi_dsm.c b/crates/stela-drivers/src/gpu/compat/linux/kernel/acpi_dsm.c new file mode 100644 index 0000000000..3d52ac350d --- /dev/null +++ b/crates/stela-drivers/src/gpu/compat/linux/kernel/acpi_dsm.c @@ -0,0 +1,114 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * acpi_utils.c - ACPI Utility Functions ($Revision: 10 $) + * + * Copyright (C) 2001, 2002 Andy Grover + * Copyright (C) 2001, 2002 Paul Diefenbaugh + */ + +/* Original DSM helpers from drivers/acpi/utils.c at Linux + * cc9de5d4b5ab6459f5774946aa3195ad51236fa8 (canonical patched tree + * 8d0a987907fe264b5688fa9c76e25b533c31a3dd). Public client includes replace + * private ACPI-core includes; the sole body adaptation makes C's existing unsigned comparison conversion + * explicit for the strict Stela warning policy. + * Evaluation requires the separate owned portal, not a second interpreter. + */ +#include +#ifdef CONFIG_ACPI +/** + * acpi_evaluate_dsm - evaluate device's _DSM method + * @handle: ACPI device handle + * @guid: GUID of requested functions, should be 16 bytes + * @rev: revision number of requested function + * @func: requested function number + * @argv4: the function specific parameter + * + * Evaluate device's _DSM method with specified GUID, revision id and + * function number. Caller needs to free the returned object. + * + * Though ACPI defines the fourth parameter for _DSM should be a package, + * some old BIOSes do expect a buffer or an integer etc. + */ +union acpi_object * +acpi_evaluate_dsm(acpi_handle handle, const guid_t *guid, u64 rev, u64 func, + union acpi_object *argv4) +{ + acpi_status ret; + struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL}; + union acpi_object params[4]; + struct acpi_object_list input = { + .count = 4, + .pointer = params, + }; + + params[0].type = ACPI_TYPE_BUFFER; + params[0].buffer.length = 16; + params[0].buffer.pointer = (u8 *)guid; + params[1].type = ACPI_TYPE_INTEGER; + params[1].integer.value = rev; + params[2].type = ACPI_TYPE_INTEGER; + params[2].integer.value = func; + if (argv4) { + params[3] = *argv4; + } else { + params[3].type = ACPI_TYPE_PACKAGE; + params[3].package.count = 0; + params[3].package.elements = NULL; + } + + ret = acpi_evaluate_object(handle, "_DSM", &input, &buf); + if (ACPI_SUCCESS(ret)) + return (union acpi_object *)buf.pointer; + + if (ret != AE_NOT_FOUND) + acpi_handle_warn(handle, + "failed to evaluate _DSM %pUb rev:%lld func:%lld (0x%x)\n", + guid, rev, func, ret); + + return NULL; +} +EXPORT_SYMBOL(acpi_evaluate_dsm); + +/** + * acpi_check_dsm - check if _DSM method supports requested functions. + * @handle: ACPI device handle + * @guid: GUID of requested functions, should be 16 bytes at least + * @rev: revision number of requested functions + * @funcs: bitmap of requested functions + * + * Evaluate device's _DSM method to check whether it supports requested + * functions. Currently only support 64 functions at maximum, should be + * enough for now. + */ +bool acpi_check_dsm(acpi_handle handle, const guid_t *guid, u64 rev, u64 funcs) +{ + int i; + u64 mask = 0; + union acpi_object *obj; + + if (funcs == 0) + return false; + + obj = acpi_evaluate_dsm(handle, guid, rev, 0, NULL); + if (!obj) + return false; + + /* For compatibility, old BIOSes may return an integer */ + if (obj->type == ACPI_TYPE_INTEGER) + mask = obj->integer.value; + else if (obj->type == ACPI_TYPE_BUFFER) + for (i = 0; (u32)i < obj->buffer.length && i < 8; i++) + mask |= (((u64)obj->buffer.pointer[i]) << (i * 8)); + ACPI_FREE(obj); + + /* + * Bit 0 indicates whether there's support for any functions other than + * function 0 for the specified GUID and revision. + */ + if ((mask & 0x1) && (mask & funcs) == funcs) + return true; + + return false; +} +EXPORT_SYMBOL(acpi_check_dsm); +#endif diff --git a/crates/stela-drivers/src/gpu/compat/linux/stela/acpi_dsm_test.c b/crates/stela-drivers/src/gpu/compat/linux/stela/acpi_dsm_test.c new file mode 100644 index 0000000000..edb2c5dd73 --- /dev/null +++ b/crates/stela-drivers/src/gpu/compat/linux/stela/acpi_dsm_test.c @@ -0,0 +1,177 @@ +// SPDX-License-Identifier: MIT +/* Original DSM helper bodies over deterministic client responses. This fixture + * installs no portal, interpreter, native owner or hardware capability. */ +#define CONFIG_ACPI 1 +#undef acpi_evaluate_object +#define acpi_evaluate_object fixture_evaluate_object +#define acpi_evaluate_dsm fixture_evaluate_dsm +#define acpi_check_dsm fixture_check_dsm +#include +#include +#include +#include +#include + +static acpi_status status; +static acpi_object_type response_type; +static u64 integer, revision, function; +static u8 buffer[16]; +static u32 buffer_length; +static const guid_t *expected_guid; +static const union acpi_object *fourth; +static size_t calls, warnings, frees; +static void *allocation; +static acpi_handle expected_handle; + +acpi_status fixture_evaluate_object(acpi_handle handle, acpi_string path, + struct acpi_object_list *input, + struct acpi_buffer *output) +{ + calls++; + assert(handle == expected_handle && !strcmp(path, "_DSM")); + assert(input && input->count == 4 && input->pointer); + union acpi_object *params = input->pointer; + assert(params[0].type == ACPI_TYPE_BUFFER && params[0].buffer.length == 16); + assert(!memcmp(params[0].buffer.pointer, expected_guid, 16)); + assert(params[1].type == ACPI_TYPE_INTEGER && params[1].integer.value == revision); + assert(params[2].type == ACPI_TYPE_INTEGER && params[2].integer.value == function); + if (fourth) { + assert(!memcmp(¶ms[3], fourth, sizeof(*fourth))); + } else { + assert(params[3].type == ACPI_TYPE_PACKAGE && !params[3].package.count); + assert(!params[3].package.elements); + } + assert(output && output->length == ACPI_ALLOCATE_BUFFER && !output->pointer); + assert(!allocation); + if (status != AE_OK) + return status; + union acpi_object *object = kmalloc(sizeof(*object) + sizeof(buffer), GFP_KERNEL); + assert(object); + memset(object, 0, sizeof(*object) + sizeof(buffer)); + object->type = response_type; + if (response_type == ACPI_TYPE_INTEGER) + object->integer.value = integer; + else if (response_type == ACPI_TYPE_BUFFER) { + object->buffer.length = buffer_length; + object->buffer.pointer = buffer_length ? (u8 *)(object + 1) : NULL; + memcpy(object + 1, buffer, sizeof(buffer)); + } + output->pointer = object; + output->length = sizeof(*object) + sizeof(buffer); + allocation = object; + return AE_OK; +} +static void fixture_free(void *object) +{ + assert(object && object == allocation); + allocation = NULL; + frees++; + kfree(object); +} +static void fixture_warn(acpi_handle handle, const char *format, ...) +{ + assert(handle == expected_handle && format); + warnings++; +} +#undef ACPI_FREE +#define ACPI_FREE(pointer) fixture_free(pointer) +#undef acpi_handle_warn +#define acpi_handle_warn(...) fixture_warn(__VA_ARGS__) +#include "../kernel/acpi_dsm.c" + +static void reset(const guid_t *guid) +{ + assert(!allocation); + calls = warnings = frees = 0; + status = AE_OK; + response_type = ACPI_TYPE_INTEGER; + integer = 0; + revision = 0xfedcba9876543210ULL; + function = 0; + buffer_length = 0; + memset(buffer, 0, sizeof(buffer)); + expected_guid = guid; + fourth = NULL; + expected_handle = (acpi_handle)0x1234UL; /* opaque fixture identity, never dereferenced */ +} +static void evaluation_arguments(const guid_t *guid) +{ + union acpi_object values[3] = {}; + values[0].type = ACPI_TYPE_INTEGER; + values[0].integer.value = 0x8877665544332211ULL; + values[1].type = ACPI_TYPE_BUFFER; + values[1].buffer.length = 3; + u8 data[] = {0xa5, 0, 0xff}; + values[1].buffer.pointer = data; + values[2].type = ACPI_TYPE_PACKAGE; + values[2].package.count = 2; + values[2].package.elements = values; + for (size_t i = 0; i < 4; i++) { + reset(guid); + function = 0xabcdef0187654321ULL; + fourth = i ? &values[i - 1] : NULL; + integer = 0x13579bdf2468ace0ULL; + union acpi_object *object = fixture_evaluate_dsm(expected_handle, guid, revision, function, (union acpi_object *)fourth); + assert(object == allocation && object->type == ACPI_TYPE_INTEGER); + assert(object->integer.value == integer && calls == 1 && !warnings && !frees); + fixture_free(object); + assert(frees == 1 && !allocation); + } + for (size_t i = 0; i < 2; i++) { + reset(guid); + status = i ? AE_ERROR : AE_NOT_FOUND; + assert(!fixture_evaluate_dsm(expected_handle, guid, revision, function, NULL)); + assert(calls == 1 && warnings == i && !allocation && !frees); + } +} +static void integer_masks(const guid_t *guid) +{ + const u64 requested[] = {0, 1, 2, 4, 0x8000000000000000ULL, ~0ULL}; + const u64 masks[] = {0, 1, 2, 3, 5, 0x8000000000000001ULL, ~0ULL}; + for (size_t i = 0; i < ARRAY_SIZE(requested); i++) { + for (size_t j = 0; j < ARRAY_SIZE(masks); j++) { + reset(guid); + integer = masks[j]; + bool expected = requested[i] && (integer & 1) && (integer & requested[i]) == requested[i]; + assert(fixture_check_dsm(expected_handle, guid, revision, requested[i]) == expected); + assert(calls == !!requested[i] && frees == calls && !warnings && !allocation); + } + } +} +static void buffer_masks(const guid_t *guid) +{ + for (u32 length = 0; length <= sizeof(buffer); length++) { + reset(guid); + response_type = ACPI_TYPE_BUFFER; + buffer_length = length; + for (size_t i = 0; i < sizeof(buffer); i++) + buffer[i] = i ? 0x80 : 3; + u64 mask = 0; + for (size_t i = 0; i < length && i < 8; i++) + mask |= (u64)buffer[i] << (i * 8); + u64 wanted = length > 7 ? 0x8000000000000000ULL : 2; + assert(fixture_check_dsm(expected_handle, guid, revision, wanted) == (!!(mask & 1) && (mask & wanted) == wanted)); + assert(calls == 1 && frees == 1 && !warnings && !allocation); + } + for (u32 kind = 0; kind < 25; kind++) { + if (kind == ACPI_TYPE_INTEGER || kind == ACPI_TYPE_BUFFER) + continue; + reset(guid); + response_type = kind; + assert(!fixture_check_dsm(expected_handle, guid, revision, 2)); + assert(calls == 1 && frees == 1 && !allocation); + } +} +int main(void) +{ + guid_t guid; + for (size_t i = 0; i < sizeof(guid.b); i++) + guid.b[i] = (u8)(i * 17); + stela_linux_enter(&stela_linux_bare); + evaluation_arguments(&guid); + integer_masks(&guid); + buffer_masks(&guid); + stela_linux_exit(); + printf("Original DSM helpers: arguments, status, masks and owned frees passed\n"); + return 0; +}