PatchworkOS  da8a090
A non-POSIX operating system.
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 */
39
40/**
41 * @brief IO APIC Memory Mapped Registers.
42 * @enum ioapic_mmio_register_t
43 */
49
50/**
51 * @brief IO APIC Registers.
52 * @enum ioapic_register_t
53 */
61
62/**
63 * @brief IO APIC Delivery Modes.
64 * @enum ioapic_delivery_mode_t
65 */
75
76/**
77 * @brief IO APIC Destination Modes.
78 * @enum ioapic_destination_mode_t
79 */
85
86/**
87 * @brief IO APIC Trigger Modes.
88 * @enum ioapic_trigger_mode_t
89 */
95
96/**
97 * @brief IO APIC Polarity Modes.
98 * @enum ioapic_polarity_t
99 */
105
106/**
107 * @brief IO APIC Version Structure.
108 * @struct ioapic_version_t
109 *
110 * Stored in the `IOAPIC_REG_VERSION` register.
111 */
112typedef struct PACKED
113{
114 union {
116 struct PACKED
117 {
122 };
123 };
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 */
132typedef union {
133 struct PACKED
134 {
136 uint8_t deliveryMode : 3; ///< ioapic_delivery_mode_t
137 uint8_t destinationMode : 1; ///< ioapic_destination_mode_t
139 uint8_t polarity : 1; ///< ioapic_polarity_t
141 uint8_t triggerMode : 1; ///< ioapic_trigger_mode_t
142 uint8_t mask : 1; ///< If set, the interrupt is masked (disabled)
145 };
146 struct PACKED
147 {
150 } raw;
152
153/**
154 * @brief Initialize all IO APICs found in the MADT.
155 *
156 * @return On success, `0`. On failure, `ERR`.
157 */
159
160/** @} */
#define PACKED
GCC packed attribute.
Definition defs.h:32
ioapic_delivery_mode_t
IO APIC Delivery Modes.
Definition ioapic.h:67
ioapic_register_t
IO APIC Registers.
Definition ioapic.h:55
ioapic_polarity_t
IO APIC Polarity Modes.
Definition ioapic.h:101
uint32_t ioapic_gsi_t
IO APIC Global System Interrupt type.
Definition ioapic.h:38
ioapic_mmio_register_t
IO APIC Memory Mapped Registers.
Definition ioapic.h:45
uint64_t ioapic_all_init(void)
Initialize all IO APICs found in the MADT.
Definition ioapic.c:91
ioapic_trigger_mode_t
IO APIC Trigger Modes.
Definition ioapic.h:91
ioapic_destination_mode_t
IO APIC Destination Modes.
Definition ioapic.h:81
@ IOAPIC_DELIVERY_EXTERNAL
Definition ioapic.h:73
@ IOAPIC_DELIVERY_LOW_PRIO
Definition ioapic.h:69
@ IOAPIC_DELIVERY_NMI
Definition ioapic.h:71
@ IOAPIC_DELIVERY_SMI
Definition ioapic.h:70
@ IOAPIC_DELIVERY_INIT
Definition ioapic.h:72
@ IOAPIC_DELIVERY_NORMAL
Definition ioapic.h:68
@ IOAPIC_REG_ARBITRATION
Definition ioapic.h:58
@ IOAPIC_REG_VERSION
Definition ioapic.h:57
@ IOAPIC_REG_REDIRECTION_BASE
Definition ioapic.h:59
@ IOAPIC_REG_IDENTIFICATION
Definition ioapic.h:56
@ IOAPIC_POLARITY_LOW
Definition ioapic.h:103
@ IOAPIC_POLARITY_HIGH
Definition ioapic.h:102
@ IOAPIC_MMIO_REG_DATA
Definition ioapic.h:47
@ IOAPIC_MMIO_REG_SELECT
Definition ioapic.h:46
@ IOAPIC_TRIGGER_LEVEL
Definition ioapic.h:93
@ IOAPIC_TRIGGER_EDGE
Definition ioapic.h:92
@ IOAPIC_DESTINATION_PHYSICAL
Definition ioapic.h:82
@ IOAPIC_DESTINATION_LOGICAL
Definition ioapic.h:83
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
Definition ioapic.h:134
uint8_t mask
If set, the interrupt is masked (disabled)
Definition ioapic.h:142
uint8_t triggerMode
ioapic_trigger_mode_t
Definition ioapic.h:141
uint8_t deliveryMode
ioapic_delivery_mode_t
Definition ioapic.h:136
uint8_t destinationMode
ioapic_destination_mode_t
Definition ioapic.h:137
uint32_t high
Definition ioapic.h:149
uint32_t low
Definition ioapic.h:148
uint8_t vector
Definition ioapic.h:135
uint8_t destination
Definition ioapic.h:144
uint64_t reserved
Definition ioapic.h:143
uint8_t remoteIRR
Definition ioapic.h:140
uint8_t deliveryStatus
Definition ioapic.h:138
uint8_t polarity
ioapic_polarity_t
Definition ioapic.h:139
IO APIC Version Structure.
Definition ioapic.h:113
uint8_t version
Definition ioapic.h:118
uint32_t raw
Definition ioapic.h:115
uint8_t reserved2
Definition ioapic.h:121
uint8_t reserved
Definition ioapic.h:119
uint8_t maxRedirs
Definition ioapic.h:120
IO APIC Redirection Entry Structure.
Definition ioapic.h:132