PatchworkOS
Loading...
Searching...
No Matches
devices.c
Go to the documentation of this file.
2
8#include <kernel/log/log.h>
9#include <kernel/log/panic.h>
10
11#include <errno.h>
12
14{
15 aml_object_t* sta = aml_namespace_find(&state->overlay, device, AML_NAME('_', 'S', 'T', 'A'));
16 if (sta == NULL)
17 {
19 return 0;
20 }
21 DEREF_DEFER(sta);
22
23 aml_integer_t value;
24 if (aml_method_evaluate_integer(state, sta, &value) == ERR)
25 {
26 LOG_ERR("could not evaluate %s._STA\n", AML_NAME_TO_STRING(device->name));
27 return ERR;
28 }
29
30 if (value &
32 {
33 LOG_ERR("%s._STA returned invalid value 0x%llx\n", AML_NAME_TO_STRING(device->name), value);
34 errno = EILSEQ;
35 return ERR;
36 }
37
38 *out = (acpi_sta_flags_t)value;
39 return 0;
40}
41
43{
44 if (parent->type != AML_DEVICE)
45 {
46 return 0; // Nothing to do
47 }
48
49 aml_object_t* child;
50 LIST_FOR_EACH(child, &parent->children, siblingsEntry)
51 {
52 if (child->type != AML_DEVICE)
53 {
54 continue; // Only devices can have _STA and _INI
55 }
56
58 if (acpi_sta_get_flags(state, child, &sta) == ERR)
59 {
60 return ERR;
61 }
62
63 if (sta & ACPI_STA_PRESENT)
64 {
65 aml_object_t* ini = aml_namespace_find(NULL, child, AML_NAME('_', 'I', 'N', 'I'));
66 if (ini != NULL)
67 {
68 DEREF_DEFER(ini);
69
70 if (ini->type != AML_METHOD)
71 {
72 LOG_ERR("%s._INI is a '%s', not a method\n", AML_NAME_TO_STRING(child->name),
73 aml_type_to_string(ini->type));
74 return ERR;
75 }
76
77 LOG_INFO("ACPI device '%s._INI'\n", AML_NAME_TO_STRING(child->name));
78 if (aml_method_evaluate_integer(state, ini, NULL) == ERR)
79 {
80 LOG_ERR("could not evaluate %s._INI\n", AML_NAME_TO_STRING(child->name));
81 return ERR;
82 }
83 }
84 }
85
87 {
88 if (acpi_devices_init_children(state, child) == ERR)
89 {
90 return ERR;
91 }
92 }
93 }
94
95 return 0;
96}
97
99{
101
102 aml_state_t state;
103 if (aml_state_init(&state, NULL) == ERR)
104 {
105 panic(NULL, "could not initialize AML state for ACPI device initialization\n");
106 }
107
108 aml_object_t* sb = aml_namespace_find(NULL, NULL, 1, AML_NAME('_', 'S', 'B', '_'));
109 if (sb == NULL) // Should never happen
110 {
111 aml_state_deinit(&state);
112 LOG_ERR("could not find \\_SB_ in namespace\n");
113 }
114 DEREF_DEFER(sb);
115
116 aml_object_t* sbIni = aml_namespace_find_child(NULL, sb, AML_NAME('_', 'I', 'N', 'I'));
117 if (sbIni != NULL)
118 {
119 DEREF_DEFER(sbIni);
120
121 if (sbIni->type != AML_METHOD)
122 {
123 aml_state_deinit(&state);
124 panic(NULL, "\\_SB_._INI is a '%s', not a method\n", aml_type_to_string(sbIni->type));
125 }
126
127 LOG_INFO("found \\_SB_._INI\n");
128 if (aml_method_evaluate_integer(&state, sbIni, NULL) == ERR)
129 {
130 aml_state_deinit(&state);
131 panic(NULL, "could not evaluate \\_SB_._INI\n");
132 }
133 }
134
135 if (acpi_devices_init_children(&state, sb) == ERR)
136 {
137 aml_state_deinit(&state);
138 panic(NULL, "could not initialize ACPI devices\n");
139 }
140
141 aml_state_deinit(&state);
142}
static uint64_t acpi_devices_init_children(aml_state_t *state, aml_object_t *parent)
Definition devices.c:42
static uint64_t acpi_sta_get_flags(aml_state_t *state, aml_object_t *device, acpi_sta_flags_t *out)
Definition devices.c:13
uint64_t aml_integer_t
AML Integer type.
Definition integer.h:20
uint64_t aml_method_evaluate_integer(aml_state_t *parentState, aml_object_t *object, aml_integer_t *out)
Wrapper around aml_method_evaluate for zero argument methods that return an integer or nothing and fo...
Definition method.c:124
#define AML_NAME_TO_STRING(name)
Macro to convert an aml_name_t to a stack allocated string.
Definition namespace.h:129
aml_object_t * aml_namespace_find(aml_namespace_overlay_t *overlay, aml_object_t *start, uint64_t nameCount,...)
Find an object in the namespace heirarchy by name segments.
Definition namespace.c:129
aml_object_t * aml_namespace_find_child(aml_namespace_overlay_t *overlay, aml_object_t *parent, aml_name_t name)
Find a child object directly under a parent object in the namespace heirarchy.
Definition namespace.c:91
#define AML_NAME(a, b, c, d)
Macro to create an aml_name_t from 4 characters.
Definition namespace.h:112
@ AML_METHOD
Definition object.h:75
@ AML_DEVICE
Definition object.h:62
uint64_t aml_state_init(aml_state_t *state, aml_object_t **args)
Initialize an AML state.
Definition state.c:8
void aml_state_deinit(aml_state_t *state)
Deinitialize an AML state.
Definition state.c:79
const char * aml_type_to_string(aml_type_t type)
Convert an aml data type to a string.
Definition to_string.c:5
mutex_t * aml_big_mutex_get(void)
Get the mutex for the entire AML subsystem.
Definition aml.c:183
void acpi_devices_init(void)
Enumerate and configure ACPI devices.
Definition devices.c:98
acpi_sta_flags_t
Flags for the _STA method.
Definition devices.h:26
#define ACPI_STA_FLAGS_DEFAULT
Default _STA flags if the _STA method does not exist.
Definition devices.h:42
@ ACPI_STA_BATTERY_PRESENT
Set if a battery is present.
Definition devices.h:32
@ ACPI_STA_SHOW_IN_UI
Set if the device should be shown in the UI.
Definition devices.h:29
@ ACPI_STA_PRESENT
Set if the device is present.
Definition devices.h:27
@ ACPI_STA_FUNCTIONAL
Set if the device is functioning properly (cleared if device failed its diagnostics)
Definition devices.h:30
@ ACPI_STA_ENABLED
Set if the device is enabled and decoding its resources.
Definition devices.h:28
NORETURN void panic(const interrupt_frame_t *frame, const char *format,...)
Panic the kernel, printing a message and halting.
Definition panic.c:362
#define LOG_ERR(format,...)
Definition log.h:89
#define LOG_INFO(format,...)
Definition log.h:87
#define MUTEX_SCOPE(mutex)
Acquires a mutex for the reminder of the current scope.
Definition mutex.h:23
#define DEREF_DEFER(ptr)
RAII-style cleanup for scoped references.
Definition ref.h:54
#define errno
Error number variable.
Definition errno.h:27
#define EILSEQ
Illegal byte sequence.
Definition errno.h:447
#define LIST_FOR_EACH(elem, list, member)
Iterates over a list.
Definition list.h:65
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
ACPI object.
Definition object.h:425
AML State.
Definition state.h:25
aml_namespace_overlay_t overlay
Holds any named objects created during parsing.
Definition state.h:30