MonkOS  v0.1
A simple 64-bit operating system (x86_64)
acpi.h
Go to the documentation of this file.
1 //============================================================================
2 /// @file acpi.h
3 /// @brief Advanced configuration and power interface (ACPI) tables.
4 //
5 // Copyright 2016 Brett Vickers.
6 // Use of this source code is governed by a BSD-style license
7 // that can be found in the MonkOS LICENSE file.
8 //============================================================================
9 
10 #pragma once
11 
12 #include <core.h>
13 
14 //----------------------------------------------------------------------------
15 // @struct acpi_hdr
16 /// @brief Header attached to the front of all ACPI tables.
17 //----------------------------------------------------------------------------
18 struct acpi_hdr
19 {
20  union
21  {
22  char bytes[4]; ///< Contains 4-letter table identifier
23  uint32_t dword;
24  } signature;
25  uint32_t length; ///< Length of this table including header
26  uint8_t revision; ///< Revision number, should be 1
27  uint8_t checksum; ///< Covers entire table
28  char oemid[6]; ///< Supplied by the OEM
29  char oemtableid[8]; ///< Supplied by the OEM
30  uint32_t oemrevision; ///< Supplied by the OEM
31  char creatorid[4]; ///< Vendor id
32  uint32_t creator_revision; ///< Revision of this utility
33 } PACKSTRUCT;
34 
35 //----------------------------------------------------------------------------
36 // @struct acpi_fadt
37 /// @brief Fixed ACPI Description Table (FADT)
38 //----------------------------------------------------------------------------
39 struct acpi_fadt
40 {
41  struct acpi_hdr hdr;
42 
43  uint32_t firmware_ctl; ///< Pointer to FACS firmware control block
44  uint32_t ptr_dsdt; ///< Pointer to DSDT block
45  uint8_t reserved1; ///< Not used
46  uint8_t pm_profile; ///< Preferred power management profile
47  uint16_t sci_interrupt; ///< SCI interrupt vector
48  uint32_t smi_cmdport; ///< SMI command port
49  uint8_t acpi_enable; ///< SMI command to disable SMI ownership
50  uint8_t acpi_disable; ///< SMI command to re-enable SMI ownership
51  uint8_t s4bios_req; ///< SMI command to enter S4BIOS state
52  uint8_t pstate_ctl; ///< SMI command to assume perf state ctl
53  uint32_t pm1a_evtblock; ///< Port of PM1a event register block
54  uint32_t pm1b_evtblock; ///< Port of PM1b event register block
55  uint32_t pm1a_ctlblock; ///< Port of PM1a ctl register block
56  uint32_t pm1b_ctlblock; ///< Port of PM1b ctl register block
57  uint32_t pm2_ctlblock; ///< Port of PM2 ctl register block
58  uint32_t pmt_ctlblock; ///< Port of PM timer ctl register block
59  uint32_t gpe0_block; ///< Port of general-purpose event 0 reg block
60  uint32_t gpe1_block; ///< Port of general-purpose event 0 reg block
61  uint8_t pm1_evt_len; ///< Bytes decoded by pm1*_evtblock
62  uint8_t pm1_ctl_len; ///< Bytes decoded by pm1*_ctlblock
63  uint8_t pm2_ctl_len; ///< Bytes decoded by pm2_ctlblock
64  uint8_t pmt_ctl_len; ///< Bytes decoded by pmt_ctlblock
65  uint8_t gpe0_len; ///< Bytes decoded by gpe0_block
66  uint8_t gpe1_len; ///< Bytes decoded by gpe1_block
67  uint8_t gpe1_base; ///< Offset where gpe1 events start
68  uint8_t cstate_ctl; ///< SMI command for C state notifications
69  uint16_t latency_c2; ///< Worst-case us latency to enter C2 state
70  uint16_t latency_c3; ///< Worst-case us latency to enter C3 state
71  uint16_t flush_size; ///< Cache reads to flush dirty cache
72  uint16_t flush_stride; ///< Cache width (flush stride)
73  uint8_t duty_offset; ///< Index of P_CNT reg duty cycle setting
74  uint8_t duty_width; ///< Width of P_CNT reg duty cycle setting
75  uint8_t alarm_day; ///< RTC RAM index day-of-month alarm: day
76  uint8_t alarm_month; ///< RTC RAM index day-of-month alarm: month
77  uint8_t century; ///< RTC RAM index of century
78  uint16_t boot_arch; ///< Boot architecture flags (ACPI 2.0+)
79  uint8_t reserved2; ///< Not used
80  uint32_t flags; ///< Fixed feature flags
81 } PACKSTRUCT;
82 
83 //----------------------------------------------------------------------------
84 // @struct acpi_mcfg
85 /// @brief PCI express Mapped Configuration (MCFG) table.
86 //----------------------------------------------------------------------------
87 struct acpi_mcfg
88 {
89  struct acpi_hdr hdr;
90 
91  uint64_t reserved;
92 } PACKSTRUCT;
93 
94 //----------------------------------------------------------------------------
95 // @struct acpi_mcfg_addr
96 /// @brief MCFG entry, one or more of which appears at the tail of the
97 /// acpi_mcfg struct.
98 //----------------------------------------------------------------------------
100 {
101  uint64_t base; ///< Base address of configuration mechanism
102  uint16_t seg_group; ///< PCI segment group number
103  uint8_t bus_start; ///< Start PCI bus number
104  uint8_t bus_end; ///< End PCI bus number
105  uint32_t reserved;
106 } PACKSTRUCT;
107 
108 //----------------------------------------------------------------------------
109 // @struct acpi_madt
110 /// @brief Multiple APIC description table (MADT).
111 //----------------------------------------------------------------------------
112 struct acpi_madt
113 {
114  struct acpi_hdr hdr;
115 
116  uint32_t ptr_local_apic; ///< Local APIC address
117  uint32_t flags; ///< APIC flags
118 } PACKSTRUCT;
119 
120 //----------------------------------------------------------------------------
121 // @enum acpi_madt_typpe
122 /// @brief MADT entry types.
123 //----------------------------------------------------------------------------
125 {
126  ACPI_MADT_LOCAL_APIC = 0, ///< Processor Local APIC
127  ACPI_MADT_IO_APIC = 1, ///< I/O APIC
128  ACPI_MADT_ISO = 2, ///< Interrupt Source Override
129  ACPI_MADT_NMIS = 3, ///< NMI Source
130  ACPI_MADT_LOCAL_NMI = 4, ///< Local APIC NMI
131  ACPI_MADT_LOCAL_ADDR = 5, ///< Local APIC Address Override
132  ACPI_MADT_IO_SAPIC = 6, ///< I/O SAPIC
133  ACPI_MADT_LOCAL_SAPIC = 7, ///< Local SAPIC
134  ACPI_MADT_PLATFORM_IS = 8, ///< Platform Interrupt Sources
135  ACPI_MADT_LOCAL_X2APIC = 9, ///< Processor Local x2APIC
136  ACPI_MADT_X2APIC_NMI = 10, ///< Local x2APIC NMI
137  ACPI_MADT_GIC = 11, ///< GIC
138  ACPI_MADT_GICD = 12, ///< GICD
139 };
140 
141 //----------------------------------------------------------------------------
142 // @struct acpi_madt_hdr
143 /// @brief MADT entry header.
144 //----------------------------------------------------------------------------
146 {
147  uint8_t type; ///< acpi_madt_type
148  uint8_t length; ///< Length of IC structure including header
149 } PACKSTRUCT;
150 
151 //----------------------------------------------------------------------------
152 // @struct acpi_madt_local_apic
153 /// @brief MADT local APIC entry
154 //----------------------------------------------------------------------------
156 {
157  struct acpi_madt_hdr hdr; // type = 0
158 
159  uint8_t procid; ///< Processor ID
160  uint8_t apicid; ///< Local APIC ID
161  uint32_t flags; ///< Local APIC flags (bit 0 = usable)
162 } PACKSTRUCT;
163 
164 //----------------------------------------------------------------------------
165 // @struct acpi_madt_local_apic
166 /// @brief MADT I/O APIC entry
167 //----------------------------------------------------------------------------
169 {
170  struct acpi_madt_hdr hdr; // type = 1
171 
172  uint8_t apicid; ///< I/O APIC ID
173  uint8_t reserved;
174  uint32_t ptr_io_apic; ///< I/O APIC address
175  uint32_t interrupt_base; ///< Interrupt # where interrupts start
176 } PACKSTRUCT;
177 
178 //----------------------------------------------------------------------------
179 // @struct acpi_madt_local_apic
180 /// @brief MADT Interrupt Source Override (ISO) entry
181 //----------------------------------------------------------------------------
183 {
184  struct acpi_madt_hdr hdr; // type = 2
185 
186  uint8_t bus; ///< Must be 0, meaning ISA
187  uint8_t source; ///< Bus-relative source (IRQ)
188  uint32_t interrupt; ///< Interrupt this soruce will signal
189  uint16_t flags; ///< MPS INTI flags
190 } PACKSTRUCT;
191 
192 //----------------------------------------------------------------------------
193 // @function acpi_init
194 /// @brief Find and parse all available ACPI tables.
195 //----------------------------------------------------------------------------
196 void
197 acpi_init();
198 
199 //----------------------------------------------------------------------------
200 // @function acpi_version
201 /// @brief Return the ACPI version number.
202 /// @returns The ACPI version number (1 through 5).
203 //----------------------------------------------------------------------------
204 int
205 acpi_version();
206 
207 //----------------------------------------------------------------------------
208 // @function acpi_fadt
209 /// @brief Return a pointer to the ACPI fixed ACPI description table.
210 /// @returns A pointer to the non-extended FADT structure.
211 //----------------------------------------------------------------------------
212 const struct acpi_fadt *
213 acpi_fadt();
214 
215 //----------------------------------------------------------------------------
216 // @function acpi_madt
217 /// @brief Return a pointer to the ACPI multiple APIC description table
218 /// (MADT).
219 /// @returns A pointer to the MADT structure.
220 //----------------------------------------------------------------------------
221 const struct acpi_madt *
222 acpi_madt();
223 
224 //----------------------------------------------------------------------------
225 // @function acpi_next_local_apic
226 /// @brief Return a pointer to the next Local APIC structure entry in
227 /// the MADT table.
228 /// @param[in] prev Pointer to the local APIC returned by a previous call
229 /// to this function. Pass NULL for the first call.
230 /// @returns A pointer to a the next local APIC structure, or NULL if none
231 /// remain.
232 //----------------------------------------------------------------------------
233 const struct acpi_madt_local_apic *
234 acpi_next_local_apic(const struct acpi_madt_local_apic *prev);
235 
236 //----------------------------------------------------------------------------
237 // @function acpi_next_io_apic
238 /// @brief Return a pointer to the next I/O APIC structure entry in
239 /// the MADT table.
240 /// @param[in] prev Pointer to the I/O APIC returned by a previous call
241 /// to this function. Pass NULL for the first call.
242 /// @returns A pointer to a the next I/O APIC structure, or NULL if none
243 /// remain.
244 //----------------------------------------------------------------------------
245 const struct acpi_madt_io_apic *
246 acpi_next_io_apic(const struct acpi_madt_io_apic *prev);
247 
248 //----------------------------------------------------------------------------
249 // @function acpi_next_iso
250 /// @brief Return a pointer to the next Interrupt Source Override (ISO)
251 /// structure entry in the MADT table.
252 /// @param[in] prev Pointer to the ISO returned by a previous call
253 /// to this function. Pass NULL for the first call.
254 /// @returns A pointer to a the next ISO structure, or NULL if none remain.
255 //----------------------------------------------------------------------------
256 const struct acpi_madt_iso *
257 acpi_next_iso(const struct acpi_madt_iso *prev);
258 
259 //----------------------------------------------------------------------------
260 // @function acpi_next_mcfg_addr
261 /// @brief Return a pointer to the next Mapped Configuration (MCFG)
262 /// address entry, used for PCIe.
263 /// @param[in] prev Pointer to the MCFG entry returned by a previous call
264 /// to this function. Pass NULL for the first call.
265 /// @returns A pointer to a the next MCFG entry, or NULL if none remain.
266 //----------------------------------------------------------------------------
267 const struct acpi_mcfg_addr *
268 acpi_next_mcfg_addr(const struct acpi_mcfg_addr *prev);
MADT I/O APIC entry.
Definition: acpi.h:168
uint8_t pm_profile
Preferred power management profile.
Definition: acpi.h:46
const struct acpi_madt_io_apic * acpi_next_io_apic(const struct acpi_madt_io_apic *prev)
Return a pointer to the next I/O APIC structure entry in the MADT table.
Definition: acpi.c:405
uint32_t flags
Fixed feature flags.
Definition: acpi.h:80
uint32_t length
Length of this table including header.
Definition: acpi.h:25
uint8_t reserved2
Not used.
Definition: acpi.h:79
MADT local APIC entry.
Definition: acpi.h:155
uint32_t ptr_io_apic
I/O APIC address.
Definition: acpi.h:174
I/O SAPIC.
Definition: acpi.h:132
uint32_t pm1a_ctlblock
Port of PM1a ctl register block.
Definition: acpi.h:55
char oemid[6]
Supplied by the OEM.
Definition: acpi.h:28
Local x2APIC NMI.
Definition: acpi.h:136
Header attached to the front of all ACPI tables.
Definition: acpi.h:18
union acpi_hdr::@1 signature
uint32_t creator_revision
Revision of this utility.
Definition: acpi.h:32
const struct acpi_madt * acpi_madt()
Return a pointer to the ACPI multiple APIC description table (MADT).
Definition: acpi.c:364
Platform Interrupt Sources.
Definition: acpi.h:134
uint8_t apicid
I/O APIC ID.
Definition: acpi.h:172
uint8_t pmt_ctl_len
Bytes decoded by pmt_ctlblock.
Definition: acpi.h:64
const struct acpi_fadt * acpi_fadt()
Return a pointer to the ACPI fixed ACPI description table.
Definition: acpi.c:358
uint8_t bus_end
End PCI bus number.
Definition: acpi.h:104
uint8_t bus
Must be 0, meaning ISA.
Definition: acpi.h:186
uint32_t flags
APIC flags.
Definition: acpi.h:117
uint32_t smi_cmdport
SMI command port.
Definition: acpi.h:48
uint8_t gpe0_len
Bytes decoded by gpe0_block.
Definition: acpi.h:65
uint8_t pstate_ctl
SMI command to assume perf state ctl.
Definition: acpi.h:52
uint32_t gpe0_block
Port of general-purpose event 0 reg block.
Definition: acpi.h:59
uint32_t interrupt
Interrupt this soruce will signal.
Definition: acpi.h:188
Core include file.
char creatorid[4]
Vendor id.
Definition: acpi.h:31
uint16_t flush_size
Cache reads to flush dirty cache.
Definition: acpi.h:71
uint32_t flags
Local APIC flags (bit 0 = usable)
Definition: acpi.h:161
NMI Source.
Definition: acpi.h:129
Interrupt Source Override.
Definition: acpi.h:128
uint8_t length
Length of IC structure including header.
Definition: acpi.h:148
const struct acpi_mcfg_addr * acpi_next_mcfg_addr(const struct acpi_mcfg_addr *prev)
Return a pointer to the next Mapped Configuration (MCFG) address entry, used for PCIe.
Definition: acpi.c:419
void acpi_init()
Find and parse all available ACPI tables.
Definition: acpi.c:281
uint32_t ptr_dsdt
Pointer to DSDT block.
Definition: acpi.h:44
uint8_t pm1_evt_len
Bytes decoded by pm1*_evtblock.
Definition: acpi.h:61
struct acpi_hdr PACKSTRUCT
GICD.
Definition: acpi.h:138
GIC.
Definition: acpi.h:137
uint32_t reserved
Definition: acpi.h:105
uint32_t ptr_local_apic
Local APIC address.
Definition: acpi.h:116
MCFG entry, one or more of which appears at the tail of the acpi_mcfg struct.
Definition: acpi.h:99
Processor Local APIC.
Definition: acpi.h:126
uint16_t flush_stride
Cache width (flush stride)
Definition: acpi.h:72
uint32_t pm1b_ctlblock
Port of PM1b ctl register block.
Definition: acpi.h:56
char oemtableid[8]
Supplied by the OEM.
Definition: acpi.h:29
uint16_t latency_c3
Worst-case us latency to enter C3 state.
Definition: acpi.h:70
const struct acpi_madt_local_apic * acpi_next_local_apic(const struct acpi_madt_local_apic *prev)
Return a pointer to the next Local APIC structure entry in the MADT table.
Definition: acpi.c:398
PCI express Mapped Configuration (MCFG) table.
Definition: acpi.h:87
uint32_t gpe1_block
Port of general-purpose event 0 reg block.
Definition: acpi.h:60
uint16_t seg_group
PCI segment group number.
Definition: acpi.h:102
uint8_t alarm_day
RTC RAM index day-of-month alarm: day.
Definition: acpi.h:75
uint8_t duty_width
Width of P_CNT reg duty cycle setting.
Definition: acpi.h:74
uint32_t oemrevision
Supplied by the OEM.
Definition: acpi.h:30
uint8_t reserved
Definition: acpi.h:173
uint8_t acpi_disable
SMI command to re-enable SMI ownership.
Definition: acpi.h:50
uint32_t pm1b_evtblock
Port of PM1b event register block.
Definition: acpi.h:54
uint32_t pm2_ctlblock
Port of PM2 ctl register block.
Definition: acpi.h:57
Fixed ACPI Description Table (FADT)
Definition: acpi.h:39
uint8_t duty_offset
Index of P_CNT reg duty cycle setting.
Definition: acpi.h:73
uint8_t apicid
Local APIC ID.
Definition: acpi.h:160
uint8_t source
Bus-relative source (IRQ)
Definition: acpi.h:187
const struct acpi_madt_iso * acpi_next_iso(const struct acpi_madt_iso *prev)
Return a pointer to the next Interrupt Source Override (ISO) structure entry in the MADT table...
Definition: acpi.c:412
MADT entry header.
Definition: acpi.h:145
uint8_t acpi_enable
SMI command to disable SMI ownership.
Definition: acpi.h:49
uint8_t checksum
Covers entire table.
Definition: acpi.h:27
uint16_t boot_arch
Boot architecture flags (ACPI 2.0+)
Definition: acpi.h:78
uint8_t type
acpi_madt_type
Definition: acpi.h:147
Local SAPIC.
Definition: acpi.h:133
MADT Interrupt Source Override (ISO) entry.
Definition: acpi.h:182
uint32_t pm1a_evtblock
Port of PM1a event register block.
Definition: acpi.h:53
uint8_t gpe1_len
Bytes decoded by gpe1_block.
Definition: acpi.h:66
uint8_t cstate_ctl
SMI command for C state notifications.
Definition: acpi.h:68
uint16_t latency_c2
Worst-case us latency to enter C2 state.
Definition: acpi.h:69
uint32_t firmware_ctl
Pointer to FACS firmware control block.
Definition: acpi.h:43
uint8_t pm2_ctl_len
Bytes decoded by pm2_ctlblock.
Definition: acpi.h:63
Multiple APIC description table (MADT).
Definition: acpi.h:112
uint16_t flags
MPS INTI flags.
Definition: acpi.h:189
uint64_t reserved
Definition: acpi.h:91
uint64_t base
Base address of configuration mechanism.
Definition: acpi.h:101
uint8_t century
RTC RAM index of century.
Definition: acpi.h:77
I/O APIC.
Definition: acpi.h:127
uint8_t procid
Processor ID.
Definition: acpi.h:159
uint8_t s4bios_req
SMI command to enter S4BIOS state.
Definition: acpi.h:51
Local APIC NMI.
Definition: acpi.h:130
uint16_t sci_interrupt
SCI interrupt vector.
Definition: acpi.h:47
uint8_t revision
Revision number, should be 1.
Definition: acpi.h:26
int acpi_version()
Return the ACPI version number.
Definition: acpi.c:352
uint32_t pmt_ctlblock
Port of PM timer ctl register block.
Definition: acpi.h:58
uint32_t interrupt_base
Interrupt # where interrupts start.
Definition: acpi.h:175
uint8_t alarm_month
RTC RAM index day-of-month alarm: month.
Definition: acpi.h:76
acpi_madt_type
MADT entry types.
Definition: acpi.h:124
Local APIC Address Override.
Definition: acpi.h:131
uint8_t reserved1
Not used.
Definition: acpi.h:45
Processor Local x2APIC.
Definition: acpi.h:135
uint8_t pm1_ctl_len
Bytes decoded by pm1*_ctlblock.
Definition: acpi.h:62
uint8_t bus_start
Start PCI bus number.
Definition: acpi.h:103
uint8_t gpe1_base
Offset where gpe1 events start.
Definition: acpi.h:67