PatchworkOS  966e257
A non-POSIX operating system.
Loading...
Searching...
No Matches
devices.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/cpu/io.h>
4#include <kernel/cpu/irq.h>
7
8#include <sys/io.h>
9
10/**
11 * @brief Device and Power Management
12 * @defgroup modules_acpi_devices Devices
13 * @ingroup modules_acpi
14 *
15 * Handles enumeration and configuration of ACPI devices, along with dynamic loading of device drivers.
16 *
17 * Each device found under the `\_SB` namespace with a `_HID` method will have its HID collected and the module system
18 * will be notified that a device with that HID exists, if there is no module supporting that HID then the devices
19 * `_CID` method will be evaluated (if it exists) and the module system will be notified of the CID returned by that
20 * method.
21 *
22 * Processor Container Devices (HID "ACPI0010") are ignored as their use is deprecated so even if the hardware provides
23 * them, we dont want to use them. Its also possible that certain devices such as the HPET are not reported even if they
24 * exist, in these cases we manually check for them and add their HIDs.
25 *
26 * ## Hardware IDs (HIDs) and Compatible IDs (CIDs)
27 *
28 * The difference between HIDs and CIDs is that HIDs are unique identifiers for the specific device type, while CIDs are
29 * more generic identifiers. Its the difference between a specific model of network card and just a generic network
30 * card.
31 *
32 * Trying HIDs first and CIDs after means we try to load a module for the exact device, or if that fails a generic
33 * module that can handle the device, tho perhaps not optimally.
34 *
35 * ## Device Configuration
36 *
37 * Each ACPI device specifies the resources it needs via its AML, for example via the `_CRS` method. This can include
38 * IRQs, IO ports, etc. During device initialization, this data is parsed and the necessary resources are allocated and
39 * configured for the device.
40 *
41 * ## Module Loading and Device Configuration Order
42 *
43 * For the sake of ensuring consistency across different systems, all modules will be loaded based on their ACPI
44 * HIDs or CIDs in alphanumerical order, this also applies to device configuration. This means that a device with the
45 * ACPI HID "ACPI0001" will be loaded before a device with the ACPI HID "ACPI0002" and that one before the device with
46 * the ACPI HID "PNP0000". This only applies to the module loading and device configuration but not to device
47 * enumeration.
48 *
49 * @todo Implement hotplugging support.
50 *
51 * @see [PNP ACPI Registry](https://uefi.org/PNP_ACPI_Registry) for a list of known ACPI HIDs.
52 * @see Section 6.1.2 and 6.1.5 of the ACPI specification for more details on HIDs and CIDs.
53 *
54 * @{
55 */
56
57/**
58 * @brief Flags for the _STA method.
59 * @enum acpi_sta_flags_t
60 *
61 * @see Section 6.3.7 of the ACPI specification for more details.
62 */
63typedef enum
64{
65 ACPI_STA_PRESENT = 1 << 0, ///< Set if the device is present
66 ACPI_STA_ENABLED = 1 << 1, ///< Set if the device is enabled and decoding its resources
67 ACPI_STA_SHOW_IN_UI = 1 << 2, ///< Set if the device should be shown in the UI.
69 1 << 3, ///< Set if the device is functioning properly (cleared if device failed its diagnostics)
70 ACPI_STA_BATTERY_PRESENT = 1 << 4, ///< Set if a battery is present
72
73/**
74 * @brief Default _STA flags if the _STA method does not exist.
75 *
76 * If the _STA method does not exist, the device is assumed to be present, enabled, shown in the UI and functioning.
77 *
78 * @see Section 6.3.7 of the ACPI specification for more details.
79 */
80#define ACPI_STA_FLAGS_DEFAULT (ACPI_STA_PRESENT | ACPI_STA_ENABLED | ACPI_STA_SHOW_IN_UI | ACPI_STA_FUNCTIONAL)
81
82/**
83 * @brief Represents a IRQ assigned to an ACPI device.
84 * @struct acpi_device_irq_t
85 */
86typedef struct acpi_device_irq
87{
92
93/**
94 * @brief Represents an IO port range assigned to an ACPI device.
95 * @struct acpi_device_io_t
96 */
97typedef struct acpi_device_io
98{
102
103/**
104 * @brief ACPI device configuration structure.
105 * @struct acpi_device_cfg_t
106 *
107 * Stores the resources assigned to an ACPI device, like IRQs and IO ports.
108 *
109 * @todo Add more config stuff like memory ranges, DMA etc.
110 */
120
121/**
122 * @brief Enumerate, configure and load modules for ACPI devices.
123 *
124 * This function always evaluates the \_SB._INI node if it exists, enumerates ACPI devices (found under \_SB), evaluates
125 * their _STA object retrieving its present and functional status (if it exists) and then evaluates their _INI object
126 * according to these rules:
127 * - If the _INI object does not exist it is ignored.
128 * - If the _STA object does not exist the device is assumed to be present and functional
129 * - If the _STA object does exist its status is read.
130 * - Depending on the status returned by _STA or assumed, the device is treated in one of four ways:
131 * - If the device is not present and not functional, the device is ignored.
132 * - If the device is not present and functional, the device's _INI is ignored but its children are enumerated.
133 * - If the device is present and not functional, the device's _INI is evaluated and its children are enumerated.
134 * - If the device is present and functional, the device's _INI is evaluated and its children are enumerated.
135 *
136 * @see Section 6.5.1 of the ACPI specification for more details.
137 * @return On success, 0. On failure, `ERR`.
138 */
140
141/**
142 * @brief Retrieves the ACPI device configuration for a device by its name.
143 *
144 * @param name The name of the device to retrieve the configuration for.
145 * @return On success, a pointer to the device configuration. On failure, `NULL` and `errno` is set to:
146 * - `EINVAL`: Invalid parameters.
147 * - `ENOENT`: The specified name does not exist in the ACPI namespace.
148 * - `ENOTTY`: The specified name is not a device.
149 * - `ENODEV`: The specified device has no configuration.
150 */
152
153/**
154 * @brief Retrieves an the nth IO port assigned to an ACPI device.
155 *
156 * Useful as each IO entry contains a base and length, making it rather complex to just, for example, get the 5th port.
157 *
158 * @param cfg The device configuration to retrieve the port from.
159 * @param index The index of the IO port to retrieve.
160 * @param out Output pointer to store the retrieved port.
161 * @return On success, `0`. On failure, `ERR` and `errno` is set to:
162 * - `EINVAL`: Invalid parameters.
163 * - `ENOSPC`: The specified index is out of bounds.
164 */
166
167/** @} */
#define MAX_NAME
Maximum length of names.
Definition MAX_NAME.h:11
uint16_t port_t
I/O port type.
Definition io.h:27
uint32_t irq_phys_t
Physical IRQ numbers.
Definition irq.h:46
irq_flags_t
IRQ flags.
Definition irq.h:94
uint8_t irq_virt_t
Virtual IRQ numbers.
Definition irq.h:57
acpi_device_cfg_t * acpi_device_cfg_lookup(const char *name)
Retrieves the ACPI device configuration for a device by its name.
Definition devices.c:567
uint64_t acpi_devices_init(void)
Enumerate, configure and load modules for ACPI devices.
Definition devices.c:480
uint64_t acpi_device_cfg_get_port(acpi_device_cfg_t *cfg, uint64_t index, port_t *out)
Retrieves an the nth IO port assigned to an ACPI device.
Definition devices.c:603
acpi_sta_flags_t
Flags for the _STA method.
Definition devices.h:64
@ ACPI_STA_BATTERY_PRESENT
Set if a battery is present.
Definition devices.h:70
@ ACPI_STA_SHOW_IN_UI
Set if the device should be shown in the UI.
Definition devices.h:67
@ ACPI_STA_PRESENT
Set if the device is present.
Definition devices.h:65
@ ACPI_STA_FUNCTIONAL
Set if the device is functioning properly (cleared if device failed its diagnostics)
Definition devices.h:68
@ ACPI_STA_ENABLED
Set if the device is enabled and decoding its resources.
Definition devices.h:66
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
ACPI device configuration structure.
Definition devices.h:112
acpi_device_irq_t * irqs
Definition devices.h:115
uint64_t ioCount
Definition devices.h:118
uint64_t irqCount
Definition devices.h:116
acpi_device_io_t * ios
Definition devices.h:117
Represents an IO port range assigned to an ACPI device.
Definition devices.h:98
uint64_t length
Definition devices.h:100
Represents a IRQ assigned to an ACPI device.
Definition devices.h:87
irq_virt_t virt
Definition devices.h:89
irq_phys_t phys
Definition devices.h:88
irq_flags_t flags
Definition devices.h:90