PatchworkOS
da8a090
A non-POSIX operating system.
Theme:
Default
Round
Robot
Loading...
Searching...
No Matches
ioapic.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <
kernel/defs.h
>
4
#include <
stdint.h
>
5
6
/**
7
* @brief Input / Output Advanced Programmable Interrupt Controller.
8
* @defgroup modules_drivers_apic_ioapic IO APIC
9
* @ingroup modules_drivers_apic
10
*
11
* The IO APICs are used to route external interrupts to a CPUs local APIC. Each IO APIC handles a range of Global
12
* System Interrupts (GSIs) or in PatchworkOS terms, physical IRQs, which it receives from external devices such as a
13
* keyboard. The IO APIC then routes these physical IRQs to a local APIC using that local APICs ID, that local APIC then
14
* triggers the interrupt on its CPU.
15
*
16
* So, for example, say we have two IO APICs, 0 and 1, where IO APIC 0 handles physical IRQs 0-23 and IO APIC 1 handles
17
* physical IRQs 24-47. Then lets say we want to route physical IRQ 1 to CPU 4. In this case, we would use IO APIC 0 to
18
* route physical IRQ 1 to the local APIC ID of CPU 4, lets say this ID is 5. The IO APIC would then send the interrupt
19
* to the local APIC with ID 5, which would then trigger the interrupt on CPU 4.
20
*
21
* The range that each IO APIC handles is defined as the range `[globalSystemInterruptBase, globalSystemInterruptBase +
22
* maxRedirs)`, where `globalSystemInterruptBase` is defined in the ACPI MADT table and `maxRedirs` is read from the IO
23
* APICs version register.
24
*
25
* @note The only reason there can be multiple IO APICs is for hardware implementation reasons, things we dont care
26
* about. As far as I know, the OS itself does not benefit from having multiple IO APICs.
27
*
28
* @see [ACPI Specification Version 6.6](https://uefi.org/sites/default/files/resources/ACPI_Spec_6.6.pdf)
29
* @see [82093AA I/O ADVANCED PROGRAMMABLE INTERRUPT CONTROLLER
30
* (IOAPIC)](https://web.archive.org/web/20161130153145/http://download.intel.com/design/chipsets/datashts/29056601.pdf)
31
*
32
* @{
33
*/
34
35
/**
36
* @brief IO APIC Global System Interrupt type.
37
*/
38
typedef
uint32_t
ioapic_gsi_t
;
39
40
/**
41
* @brief IO APIC Memory Mapped Registers.
42
* @enum ioapic_mmio_register_t
43
*/
44
typedef
enum
45
{
46
IOAPIC_MMIO_REG_SELECT
= 0x00,
47
IOAPIC_MMIO_REG_DATA
= 0x10
48
}
ioapic_mmio_register_t
;
49
50
/**
51
* @brief IO APIC Registers.
52
* @enum ioapic_register_t
53
*/
54
typedef
enum
55
{
56
IOAPIC_REG_IDENTIFICATION
= 0x00,
57
IOAPIC_REG_VERSION
= 0x01,
58
IOAPIC_REG_ARBITRATION
= 0x02,
59
IOAPIC_REG_REDIRECTION_BASE
= 0x10
60
}
ioapic_register_t
;
61
62
/**
63
* @brief IO APIC Delivery Modes.
64
* @enum ioapic_delivery_mode_t
65
*/
66
typedef
enum
67
{
68
IOAPIC_DELIVERY_NORMAL
= 0,
69
IOAPIC_DELIVERY_LOW_PRIO
= 1,
70
IOAPIC_DELIVERY_SMI
= 2,
71
IOAPIC_DELIVERY_NMI
= 4,
72
IOAPIC_DELIVERY_INIT
= 5,
73
IOAPIC_DELIVERY_EXTERNAL
= 7
74
}
ioapic_delivery_mode_t
;
75
76
/**
77
* @brief IO APIC Destination Modes.
78
* @enum ioapic_destination_mode_t
79
*/
80
typedef
enum
81
{
82
IOAPIC_DESTINATION_PHYSICAL
= 0,
83
IOAPIC_DESTINATION_LOGICAL
= 1
84
}
ioapic_destination_mode_t
;
85
86
/**
87
* @brief IO APIC Trigger Modes.
88
* @enum ioapic_trigger_mode_t
89
*/
90
typedef
enum
91
{
92
IOAPIC_TRIGGER_EDGE
= 0,
93
IOAPIC_TRIGGER_LEVEL
= 1
94
}
ioapic_trigger_mode_t
;
95
96
/**
97
* @brief IO APIC Polarity Modes.
98
* @enum ioapic_polarity_t
99
*/
100
typedef
enum
101
{
102
IOAPIC_POLARITY_HIGH
= 0,
103
IOAPIC_POLARITY_LOW
= 1
104
}
ioapic_polarity_t
;
105
106
/**
107
* @brief IO APIC Version Structure.
108
* @struct ioapic_version_t
109
*
110
* Stored in the `IOAPIC_REG_VERSION` register.
111
*/
112
typedef
struct
PACKED
113
{
114
union
{
115
uint32_t
raw
;
116
struct
PACKED
117
{
118
uint8_t
version
;
119
uint8_t
reserved
;
120
uint8_t
maxRedirs
;
121
uint8_t
reserved2
;
122
};
123
};
124
}
ioapic_version_t
;
125
126
/**
127
* @brief IO APIC Redirection Entry Structure.
128
* @struct ioapic_redirect_entry_t
129
*
130
* Represents a single redirection entry in the IO APIC.
131
*/
132
typedef
union
{
133
struct
PACKED
134
{
135
uint8_t
vector
;
136
uint8_t
deliveryMode
: 3;
///< ioapic_delivery_mode_t
137
uint8_t
destinationMode
: 1;
///< ioapic_destination_mode_t
138
uint8_t
deliveryStatus
: 1;
139
uint8_t
polarity
: 1;
///< ioapic_polarity_t
140
uint8_t
remoteIRR
: 1;
141
uint8_t
triggerMode
: 1;
///< ioapic_trigger_mode_t
142
uint8_t
mask
: 1;
///< If set, the interrupt is masked (disabled)
143
uint64_t
reserved
: 39;
144
uint8_t
destination
: 8;
145
};
146
struct
PACKED
147
{
148
uint32_t
low
;
149
uint32_t
high
;
150
} raw;
151
}
ioapic_redirect_entry_t
;
152
153
/**
154
* @brief Initialize all IO APICs found in the MADT.
155
*
156
* @return On success, `0`. On failure, `ERR`.
157
*/
158
uint64_t
ioapic_all_init
(
void
);
159
160
/** @} */
defs.h
PACKED
#define PACKED
GCC packed attribute.
Definition
defs.h:32
ioapic_delivery_mode_t
ioapic_delivery_mode_t
IO APIC Delivery Modes.
Definition
ioapic.h:67
ioapic_register_t
ioapic_register_t
IO APIC Registers.
Definition
ioapic.h:55
ioapic_polarity_t
ioapic_polarity_t
IO APIC Polarity Modes.
Definition
ioapic.h:101
ioapic_gsi_t
uint32_t ioapic_gsi_t
IO APIC Global System Interrupt type.
Definition
ioapic.h:38
ioapic_mmio_register_t
ioapic_mmio_register_t
IO APIC Memory Mapped Registers.
Definition
ioapic.h:45
ioapic_all_init
uint64_t ioapic_all_init(void)
Initialize all IO APICs found in the MADT.
Definition
ioapic.c:91
ioapic_trigger_mode_t
ioapic_trigger_mode_t
IO APIC Trigger Modes.
Definition
ioapic.h:91
ioapic_destination_mode_t
ioapic_destination_mode_t
IO APIC Destination Modes.
Definition
ioapic.h:81
IOAPIC_DELIVERY_EXTERNAL
@ IOAPIC_DELIVERY_EXTERNAL
Definition
ioapic.h:73
IOAPIC_DELIVERY_LOW_PRIO
@ IOAPIC_DELIVERY_LOW_PRIO
Definition
ioapic.h:69
IOAPIC_DELIVERY_NMI
@ IOAPIC_DELIVERY_NMI
Definition
ioapic.h:71
IOAPIC_DELIVERY_SMI
@ IOAPIC_DELIVERY_SMI
Definition
ioapic.h:70
IOAPIC_DELIVERY_INIT
@ IOAPIC_DELIVERY_INIT
Definition
ioapic.h:72
IOAPIC_DELIVERY_NORMAL
@ IOAPIC_DELIVERY_NORMAL
Definition
ioapic.h:68
IOAPIC_REG_ARBITRATION
@ IOAPIC_REG_ARBITRATION
Definition
ioapic.h:58
IOAPIC_REG_VERSION
@ IOAPIC_REG_VERSION
Definition
ioapic.h:57
IOAPIC_REG_REDIRECTION_BASE
@ IOAPIC_REG_REDIRECTION_BASE
Definition
ioapic.h:59
IOAPIC_REG_IDENTIFICATION
@ IOAPIC_REG_IDENTIFICATION
Definition
ioapic.h:56
IOAPIC_POLARITY_LOW
@ IOAPIC_POLARITY_LOW
Definition
ioapic.h:103
IOAPIC_POLARITY_HIGH
@ IOAPIC_POLARITY_HIGH
Definition
ioapic.h:102
IOAPIC_MMIO_REG_DATA
@ IOAPIC_MMIO_REG_DATA
Definition
ioapic.h:47
IOAPIC_MMIO_REG_SELECT
@ IOAPIC_MMIO_REG_SELECT
Definition
ioapic.h:46
IOAPIC_TRIGGER_LEVEL
@ IOAPIC_TRIGGER_LEVEL
Definition
ioapic.h:93
IOAPIC_TRIGGER_EDGE
@ IOAPIC_TRIGGER_EDGE
Definition
ioapic.h:92
IOAPIC_DESTINATION_PHYSICAL
@ IOAPIC_DESTINATION_PHYSICAL
Definition
ioapic.h:82
IOAPIC_DESTINATION_LOGICAL
@ IOAPIC_DESTINATION_LOGICAL
Definition
ioapic.h:83
stdint.h
uint32_t
__UINT32_TYPE__ uint32_t
Definition
stdint.h:15
uint64_t
__UINT64_TYPE__ uint64_t
Definition
stdint.h:17
uint8_t
__UINT8_TYPE__ uint8_t
Definition
stdint.h:11
ioapic_redirect_entry_t::PACKED
Definition
ioapic.h:134
ioapic_redirect_entry_t::PACKED::mask
uint8_t mask
If set, the interrupt is masked (disabled)
Definition
ioapic.h:142
ioapic_redirect_entry_t::PACKED::triggerMode
uint8_t triggerMode
ioapic_trigger_mode_t
Definition
ioapic.h:141
ioapic_redirect_entry_t::PACKED::deliveryMode
uint8_t deliveryMode
ioapic_delivery_mode_t
Definition
ioapic.h:136
ioapic_redirect_entry_t::PACKED::destinationMode
uint8_t destinationMode
ioapic_destination_mode_t
Definition
ioapic.h:137
ioapic_redirect_entry_t::PACKED::high
uint32_t high
Definition
ioapic.h:149
ioapic_redirect_entry_t::PACKED::low
uint32_t low
Definition
ioapic.h:148
ioapic_redirect_entry_t::PACKED::vector
uint8_t vector
Definition
ioapic.h:135
ioapic_redirect_entry_t::PACKED::destination
uint8_t destination
Definition
ioapic.h:144
ioapic_redirect_entry_t::PACKED::reserved
uint64_t reserved
Definition
ioapic.h:143
ioapic_redirect_entry_t::PACKED::remoteIRR
uint8_t remoteIRR
Definition
ioapic.h:140
ioapic_redirect_entry_t::PACKED::deliveryStatus
uint8_t deliveryStatus
Definition
ioapic.h:138
ioapic_redirect_entry_t::PACKED::polarity
uint8_t polarity
ioapic_polarity_t
Definition
ioapic.h:139
ioapic_version_t
IO APIC Version Structure.
Definition
ioapic.h:113
ioapic_version_t::version
uint8_t version
Definition
ioapic.h:118
ioapic_version_t::raw
uint32_t raw
Definition
ioapic.h:115
ioapic_version_t::reserved2
uint8_t reserved2
Definition
ioapic.h:121
ioapic_version_t::reserved
uint8_t reserved
Definition
ioapic.h:119
ioapic_version_t::maxRedirs
uint8_t maxRedirs
Definition
ioapic.h:120
ioapic_redirect_entry_t
IO APIC Redirection Entry Structure.
Definition
ioapic.h:132
include
modules
drivers
apic
ioapic.h
Generated on Fri Dec 12 2025 00:22:10 for PatchworkOS by
1.9.8