PatchworkOS  da8a090
A non-POSIX operating system.
Loading...
Searching...
No Matches
lapic.h
Go to the documentation of this file.
1#pragma once
2
3#include <kernel/cpu/cpu.h>
4#include <kernel/cpu/irq.h>
5#include <stdint.h>
6
7/**
8 * @brief Local Advanced Programmable Interrupt Controller.
9 * @defgroup modules_drivers_apic_lapic Local APIC
10 * @ingroup modules_drivers_apic
11 *
12 * ## Local APICs
13 *
14 * Each CPU has its own local APIC, which, when used with the IO APICs, allows for more advanced
15 * interrupt handling in comparison to the traditional PIC, such as routing interrupts to specific CPUs, interrupt
16 * prioritization, and more. Most of its features are not used in PatchworkOS yet.
17 *
18 * Additionally, the local APICs provide Inter-Processor Interrupts (IPIs) which allow a CPU to interrupt another CPU by
19 * using its local APIC.
20 *
21 * @note Its a common mistake to assume that the local APIC IDs are contiguous, or that they are the same as the CPU
22 * IDs, but this is not the case. The local APIC IDs are assigned by the firmware and can be any value.
23 *
24 * @see [ACPI Specification Version 6.6](https://uefi.org/sites/default/files/resources/ACPI_Spec_6.6.pdf)
25 *
26 * @{
27 */
28
29/**
30 * @brief Local APIC ID type.
31 */
33
34/**
35 * @brief Local APIC MSR Flags.
36 * @enum lapic_msr_flags_t
37 */
38typedef enum
39{
41 LAPIC_MSR_BSP = 0x100
43
44/**
45 * @brief Local APIC Registers.
46 * @enum lapic_register_t
47 */
77
78/**
79 * @brief The offset at which the lapic id is stored in the LAPIC_REG_ID register.
80 */
81#define LAPIC_REG_ID_OFFSET 24
82
83/**
84 * @brief Local APIC Flags.
85 * @enum lapic_flags_t
86 */
87typedef enum
88{
90 LAPIC_LVT_MASKED = (1 << 16)
92
93/**
94 * @brief Local APIC ICR Delivery Modes.
95 * @enum lapic_icr_delivery_mode_t
96 */
97typedef enum
98{
99 LAPIC_ICR_FIXED = (0 << 8),
101 LAPIC_ICR_SMI = (2 << 8),
102 LAPIC_ICR_NMI = (4 << 8),
103 LAPIC_ICR_INIT = (5 << 8),
104 LAPIC_ICR_STARTUP = (6 << 8)
106
107/**
108 * @brief Local APIC ICR Flags.
109 * @enum lapic_icr_flags_t
110 */
111typedef enum
112{
115
116/**
117 * @brief Local APIC Structure.
118 * @struct lapic_t
119 *
120 * Represents each CPU's local APIC and local data.
121 */
122typedef struct
123{
124 uint64_t ticksPerMs; ///< Initialized to 0, set on first use of the APIC timer on the CPU.
126} lapic_t;
127
128/**
129 * @brief Initialize the local APIC subsystem.
130 *
131 * @return On success, `0`. On failure, `ERR`.
132 */
134
135/**
136 * @brief Initialize the local APIC for a CPU.
137 *
138 * @param cpu The current CPU.
139 */
140void lapic_init(cpu_t* cpu);
141
142/**
143 * @brief Read from a local apic register.
144 *
145 * @param reg The register to read from.
146 * @return The value read from the register.
147 */
149
150/**
151 * @brief Write to a local apic register.
152 *
153 * @param reg The register to write to.
154 * @param value The value to write.
155 */
156void lapic_write(uint32_t reg, uint32_t value);
157
158/**
159 * @brief Get the lapic data for the specified CPU.
160 *
161 * @param cpuId The CPU ID.
162 * @return Pointer to the lapic structure for the CPU.
163 */
165
166/**
167 * @brief Send an INIT IPI to the specified local APIC.
168 *
169 * Sending an INIT IPI will cause the target CPU to enter the INIT state, preparing it for startup.
170 *
171 * @param id The ID of the local APIC to send the INIT IPI to.
172 */
174
175/**
176 * @brief Send a Startup IPI (SIPI) to the specified local APIC.
177 *
178 * Sending a SIPI will cause the target CPU to start executing code at the specified entry point address.
179 *
180 * @param id The ID of the local APIC to send the SIPI to.
181 * @param entryPoint The entry point address for the SIPI, must be page-aligned.
182 */
183void lapic_send_sipi(lapic_id_t id, void* entryPoint);
184
185/** @} */
lapic_flags_t
Local APIC Flags.
Definition lapic.h:88
lapic_t * lapic_get(uint32_t cpuId)
Get the lapic data for the specified CPU.
Definition lapic.c:49
void lapic_init(cpu_t *cpu)
Initialize the local APIC for a CPU.
Definition lapic.c:28
lapic_msr_flags_t
Local APIC MSR Flags.
Definition lapic.h:39
void lapic_send_init(lapic_id_t id)
Send an INIT IPI to the specified local APIC.
Definition lapic.c:118
void lapic_send_sipi(lapic_id_t id, void *entryPoint)
Send a Startup IPI (SIPI) to the specified local APIC.
Definition lapic.c:124
lapic_icr_flags_t
Local APIC ICR Flags.
Definition lapic.h:112
uint32_t lapic_read(uint32_t reg)
Read from a local apic register.
Definition lapic.c:18
uint64_t lapic_global_init(void)
Initialize the local APIC subsystem.
Definition lapic.c:76
uint8_t lapic_id_t
Local APIC ID type.
Definition lapic.h:32
lapic_register_t
Local APIC Registers.
Definition lapic.h:49
lapic_icr_delivery_mode_t
Local APIC ICR Delivery Modes.
Definition lapic.h:98
void lapic_write(uint32_t reg, uint32_t value)
Write to a local apic register.
Definition lapic.c:23
@ LAPIC_LVT_MASKED
Definition lapic.h:90
@ LAPIC_SPURIOUS_ENABLE
Definition lapic.h:89
@ LAPIC_MSR_BSP
Definition lapic.h:41
@ LAPIC_MSR_ENABLE
Definition lapic.h:40
@ LAPIC_ICR_CLEAR_INIT_LEVEL
Definition lapic.h:113
@ LAPIC_REG_TASK_PRIORITY
Definition lapic.h:52
@ LAPIC_REG_TIMER_CURRENT_COUNT
Definition lapic.h:74
@ LAPIC_REG_TIMER_INITIAL_COUNT
Definition lapic.h:73
@ LAPIC_REG_LVT_CMCI
Definition lapic.h:64
@ LAPIC_REG_REMOTE_READ
Definition lapic.h:56
@ LAPIC_REG_LOGICAL_DEST
Definition lapic.h:57
@ LAPIC_REG_LVT_PERFCTR
Definition lapic.h:69
@ LAPIC_REG_ERROR_STATUS
Definition lapic.h:63
@ LAPIC_REG_TIMER_DIVIDER
Definition lapic.h:75
@ LAPIC_REG_LVT_THERMAL
Definition lapic.h:68
@ LAPIC_REG_LVT_LINT0
Definition lapic.h:70
@ LAPIC_REG_IRR_BASE
Definition lapic.h:62
@ LAPIC_REG_VERSION
Definition lapic.h:51
@ LAPIC_REG_PROCESSOR_PRIORITY
Definition lapic.h:54
@ LAPIC_REG_ID
Definition lapic.h:50
@ LAPIC_REG_LVT_ERROR
Definition lapic.h:72
@ LAPIC_REG_ISR_BASE
Definition lapic.h:60
@ LAPIC_REG_ICR1
Definition lapic.h:66
@ LAPIC_REG_TMR_BASE
Definition lapic.h:61
@ LAPIC_REG_EOI
Definition lapic.h:55
@ LAPIC_REG_DEST_FORMAT
Definition lapic.h:58
@ LAPIC_REG_ARBITRATION_PRIORITY
Definition lapic.h:53
@ LAPIC_REG_LVT_LINT1
Definition lapic.h:71
@ LAPIC_REG_SPURIOUS
Definition lapic.h:59
@ LAPIC_REG_ICR0
Definition lapic.h:65
@ LAPIC_REG_LVT_TIMER
Definition lapic.h:67
@ LAPIC_ICR_SMI
Definition lapic.h:101
@ LAPIC_ICR_STARTUP
Definition lapic.h:104
@ LAPIC_ICR_LOWEST_PRIORITY
Definition lapic.h:100
@ LAPIC_ICR_FIXED
Definition lapic.h:99
@ LAPIC_ICR_NMI
Definition lapic.h:102
@ LAPIC_ICR_INIT
Definition lapic.h:103
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINT8_TYPE__ uint8_t
Definition stdint.h:11
CPU structure.
Definition cpu.h:122
Local APIC Structure.
Definition lapic.h:123
uint64_t ticksPerMs
Initialized to 0, set on first use of the APIC timer on the CPU.
Definition lapic.h:124
lapic_id_t lapicId
Definition lapic.h:125