xref: /kvm-unit-tests/lib/acpi.h (revision 13182cd7705d9a39fc941bbeb5c587495b15cddc)
1  #ifndef _ACPI_H_
2  #define _ACPI_H_
3  
4  #include "libcflat.h"
5  
6  /*
7   * All tables and structures must be byte-packed to match the ACPI
8   * specification, since the tables are provided by the system firmware.
9   */
10  #pragma pack(1)
11  
12  #define ACPI_SIGNATURE(c1, c2, c3, c4) \
13  	((c1) | ((c2) << 8) | ((c3) << 16) | ((c4) << 24))
14  
15  #define RSDP_SIGNATURE ACPI_SIGNATURE('R','S','D','P')
16  #define RSDT_SIGNATURE ACPI_SIGNATURE('R','S','D','T')
17  #define XSDT_SIGNATURE ACPI_SIGNATURE('X','S','D','T')
18  #define FACP_SIGNATURE ACPI_SIGNATURE('F','A','C','P')
19  #define FACS_SIGNATURE ACPI_SIGNATURE('F','A','C','S')
20  #define MADT_SIGNATURE ACPI_SIGNATURE('A','P','I','C')
21  #define SPCR_SIGNATURE ACPI_SIGNATURE('S','P','C','R')
22  #define GTDT_SIGNATURE ACPI_SIGNATURE('G','T','D','T')
23  
24  #define ACPI_SIGNATURE_8BYTE(c1, c2, c3, c4, c5, c6, c7, c8) \
25  	(((uint64_t)(ACPI_SIGNATURE(c1, c2, c3, c4))) |	     \
26  	 ((uint64_t)(ACPI_SIGNATURE(c5, c6, c7, c8)) << 32))
27  
28  #define RSDP_SIGNATURE_8BYTE (ACPI_SIGNATURE_8BYTE('R', 'S', 'D', ' ', 'P', 'T', 'R', ' '))
29  
30  struct acpi_table_rsdp {	/* Root System Descriptor Pointer */
31  	u64 signature;		/* ACPI signature, contains "RSD PTR " */
32  	u8 checksum;		/* To make sum of struct == 0 */
33  	u8 oem_id[6];		/* OEM identification */
34  	u8 revision;		/* Must be 0 for 1.0, 2 for 2.0 */
35  	u32 rsdt_physical_address;	/* 32-bit physical address of RSDT */
36  	u32 length;		/* XSDT Length in bytes including hdr */
37  	u64 xsdt_physical_address;	/* 64-bit physical address of XSDT */
38  	u8 extended_checksum;	/* Checksum of entire table */
39  	u8 reserved[3];		/* Reserved field must be 0 */
40  };
41  
42  #define ACPI_TABLE_HEADER_DEF		/* ACPI common table header */			\
43  	u32 signature;			/* ACPI signature (4 ASCII characters) */	\
44  	u32 length;			/* Length of table, in bytes, including header */ \
45  	u8  revision;			/* ACPI Specification minor version # */	\
46  	u8  checksum;			/* To make sum of entire table == 0 */		\
47  	u8  oem_id[6];			/* OEM identification */			\
48  	u8  oem_table_id[8];		/* OEM table identification */			\
49  	u32 oem_revision;		/* OEM revision number */			\
50  	u8  asl_compiler_id[4];		/* ASL compiler vendor ID */			\
51  	u32 asl_compiler_revision;	/* ASL compiler revision number */
52  
53  struct acpi_table {
54  	ACPI_TABLE_HEADER_DEF
55  	char data[];
56  };
57  
58  struct acpi_table_rsdt_rev1 {
59  	ACPI_TABLE_HEADER_DEF
60  	u32 table_offset_entry[];
61  };
62  
63  struct acpi_table_xsdt {
64  	ACPI_TABLE_HEADER_DEF
65  	u64 table_offset_entry[];
66  };
67  
68  struct acpi_generic_address {
69  	u8 space_id;		/* Address space where struct or register exists */
70  	u8 bit_width;		/* Size in bits of given register */
71  	u8 bit_offset;		/* Bit offset within the register */
72  	u8 access_width;	/* Minimum Access size (ACPI 3.0) */
73  	u64 address;		/* 64-bit address of struct or register */
74  };
75  
76  struct acpi_table_fadt {
77  	ACPI_TABLE_HEADER_DEF	/* ACPI common table header */
78  	u32 firmware_ctrl;	/* Physical address of FACS */
79  	u32 dsdt;		/* Physical address of DSDT */
80  	u8 model;		/* System Interrupt Model */
81  	u8 reserved1;		/* Reserved */
82  	u16 sci_int;		/* System vector of SCI interrupt */
83  	u32 smi_cmd;		/* Port address of SMI command port */
84  	u8 acpi_enable;		/* Value to write to smi_cmd to enable ACPI */
85  	u8 acpi_disable;	/* Value to write to smi_cmd to disable ACPI */
86  	u8 S4bios_req;		/* Value to write to SMI CMD to enter S4BIOS state */
87  	u8 reserved2;		/* Reserved - must be zero */
88  	u32 pm1a_evt_blk;	/* Port address of Power Mgt 1a acpi_event Reg Blk */
89  	u32 pm1b_evt_blk;	/* Port address of Power Mgt 1b acpi_event Reg Blk */
90  	u32 pm1a_cnt_blk;	/* Port address of Power Mgt 1a Control Reg Blk */
91  	u32 pm1b_cnt_blk;	/* Port address of Power Mgt 1b Control Reg Blk */
92  	u32 pm2_cnt_blk;	/* Port address of Power Mgt 2 Control Reg Blk */
93  	u32 pm_tmr_blk;		/* Port address of Power Mgt Timer Ctrl Reg Blk */
94  	u32 gpe0_blk;		/* Port addr of General Purpose acpi_event 0 Reg Blk */
95  	u32 gpe1_blk;		/* Port addr of General Purpose acpi_event 1 Reg Blk */
96  	u8 pm1_evt_len;		/* Byte length of ports at pm1_x_evt_blk */
97  	u8 pm1_cnt_len;		/* Byte length of ports at pm1_x_cnt_blk */
98  	u8 pm2_cnt_len;		/* Byte Length of ports at pm2_cnt_blk */
99  	u8 pm_tmr_len;		/* Byte Length of ports at pm_tm_blk */
100  	u8 gpe0_blk_len;	/* Byte Length of ports at gpe0_blk */
101  	u8 gpe1_blk_len;	/* Byte Length of ports at gpe1_blk */
102  	u8 gpe1_base;		/* Offset in gpe model where gpe1 events start */
103  	u8 reserved3;		/* Reserved */
104  	u16 plvl2_lat;		/* Worst case HW latency to enter/exit C2 state */
105  	u16 plvl3_lat;		/* Worst case HW latency to enter/exit C3 state */
106  	u16 flush_size;		/* Size of area read to flush caches */
107  	u16 flush_stride;	/* Stride used in flushing caches */
108  	u8 duty_offset;		/* Bit location of duty cycle field in p_cnt reg */
109  	u8 duty_width;		/* Bit width of duty cycle field in p_cnt reg */
110  	u8 day_alrm;		/* Index to day-of-month alarm in RTC CMOS RAM */
111  	u8 mon_alrm;		/* Index to month-of-year alarm in RTC CMOS RAM */
112  	u8 century;		/* Index to century in RTC CMOS RAM */
113  	u16 boot_flags;		/* IA-PC Boot Architecture Flags (see below for individual flags) */
114  	u8 reserved;		/* Reserved, must be zero */
115  	u32 flags;		/* Miscellaneous flag bits (see below for individual flags) */
116  	struct acpi_generic_address reset_register;	/* 64-bit address of the Reset register */
117  	u8 reset_value;		/* Value to write to the reset_register port to reset the system */
118  	u16 arm_boot_flags;	/* ARM-Specific Boot Flags (see below for individual flags) (ACPI 5.1) */
119  	u8 minor_revision;	/* FADT Minor Revision (ACPI 5.1) */
120  	u64 Xfacs;		/* 64-bit physical address of FACS */
121  	u64 Xdsdt;		/* 64-bit physical address of DSDT */
122  	struct acpi_generic_address xpm1a_event_block;	/* 64-bit Extended Power Mgt 1a Event Reg Blk address */
123  	struct acpi_generic_address xpm1b_event_block;	/* 64-bit Extended Power Mgt 1b Event Reg Blk address */
124  	struct acpi_generic_address xpm1a_control_block;	/* 64-bit Extended Power Mgt 1a Control Reg Blk address */
125  	struct acpi_generic_address xpm1b_control_block;	/* 64-bit Extended Power Mgt 1b Control Reg Blk address */
126  	struct acpi_generic_address xpm2_control_block;	/* 64-bit Extended Power Mgt 2 Control Reg Blk address */
127  	struct acpi_generic_address xpm_timer_block;	/* 64-bit Extended Power Mgt Timer Ctrl Reg Blk address */
128  	struct acpi_generic_address xgpe0_block;	/* 64-bit Extended General Purpose Event 0 Reg Blk address */
129  	struct acpi_generic_address xgpe1_block;	/* 64-bit Extended General Purpose Event 1 Reg Blk address */
130  	struct acpi_generic_address sleep_control;	/* 64-bit Sleep Control register (ACPI 5.0) */
131  	struct acpi_generic_address sleep_status;	/* 64-bit Sleep Status register (ACPI 5.0) */
132  	u64 hypervisor_id;	/* Hypervisor Vendor ID (ACPI 6.0) */
133  };
134  
135  /* Masks for FADT ARM Boot Architecture Flags (arm_boot_flags) ACPI 5.1 */
136  
137  #define ACPI_FADT_PSCI_COMPLIANT    (1)	/* 00: [V5+] PSCI 0.2+ is implemented */
138  #define ACPI_FADT_PSCI_USE_HVC      (1<<1)	/* 01: [V5+] HVC must be used instead of SMC as the PSCI conduit */
139  
140  struct acpi_table_facs_rev1 {
141  	u32 signature;		/* ACPI Signature */
142  	u32 length;		/* Length of structure, in bytes */
143  	u32 hardware_signature;	/* Hardware configuration signature */
144  	u32 firmware_waking_vector;	/* ACPI OS waking vector */
145  	u32 global_lock;	/* Global Lock */
146  	u32 S4bios_f:1;		/* Indicates if S4BIOS support is present */
147  	u32 reserved1:31;	/* Must be 0 */
148  	u8 reserved3[40];	/* Reserved - must be zero */
149  };
150  
151  struct acpi_table_madt {
152  	ACPI_TABLE_HEADER_DEF	/* ACPI common table header */
153  	u32 address;		/* Physical address of local APIC */
154  	u32 flags;
155  };
156  
157  struct acpi_subtable_header {
158  	u8 type;
159  	u8 length;
160  };
161  
162  typedef int (*acpi_table_handler)(struct acpi_subtable_header *header);
163  
164  /* 11: Generic interrupt - GICC (ACPI 5.0 + ACPI 6.0 + ACPI 6.3 changes) */
165  
166  struct acpi_madt_generic_interrupt {
167  	u8 type;
168  	u8 length;
169  	u16 reserved;		/* reserved - must be zero */
170  	u32 cpu_interface_number;
171  	u32 uid;
172  	u32 flags;
173  	u32 parking_version;
174  	u32 performance_interrupt;
175  	u64 parked_address;
176  	u64 base_address;
177  	u64 gicv_base_address;
178  	u64 gich_base_address;
179  	u32 vgic_interrupt;
180  	u64 gicr_base_address;
181  	u64 arm_mpidr;
182  	u8 efficiency_class;
183  	u8 reserved2[1];
184  	u16 spe_interrupt;	/* ACPI 6.3 */
185  };
186  
187  /* 12: Generic Distributor (ACPI 5.0 + ACPI 6.0 changes) */
188  
189  struct acpi_madt_generic_distributor {
190  	struct acpi_subtable_header header;
191  	u16 reserved;		/* reserved - must be zero */
192  	u32 gic_id;
193  	u64 base_address;
194  	u32 global_irq_base;
195  	u8 version;
196  	u8 reserved2[3];	/* reserved - must be zero */
197  };
198  
199  /* Values for Version field above */
200  
201  enum acpi_madt_gic_version {
202  	ACPI_MADT_GIC_VERSION_NONE = 0,
203  	ACPI_MADT_GIC_VERSION_V1 = 1,
204  	ACPI_MADT_GIC_VERSION_V2 = 2,
205  	ACPI_MADT_GIC_VERSION_V3 = 3,
206  	ACPI_MADT_GIC_VERSION_V4 = 4,
207  	ACPI_MADT_GIC_VERSION_RESERVED = 5	/* 5 and greater are reserved */
208  };
209  
210  /* 14: Generic Redistributor (ACPI 5.1) */
211  
212  struct acpi_madt_generic_redistributor {
213  	struct acpi_subtable_header header;
214  	u16 reserved;		/* reserved - must be zero */
215  	u64 base_address;
216  	u32 length;
217  };
218  
219  /* 15: Generic Translator (ACPI 6.0) */
220  
221  struct acpi_madt_generic_translator {
222  	struct acpi_subtable_header header;
223  	u16 reserved;		/* reserved - must be zero */
224  	u32 translation_id;
225  	u64 base_address;
226  	u32 reserved2;
227  };
228  
229  /* Values for MADT subtable type in struct acpi_subtable_header */
230  
231  enum acpi_madt_type {
232  	ACPI_MADT_TYPE_LOCAL_APIC = 0,
233  	ACPI_MADT_TYPE_IO_APIC = 1,
234  	ACPI_MADT_TYPE_INTERRUPT_OVERRIDE = 2,
235  	ACPI_MADT_TYPE_NMI_SOURCE = 3,
236  	ACPI_MADT_TYPE_LOCAL_APIC_NMI = 4,
237  	ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE = 5,
238  	ACPI_MADT_TYPE_IO_SAPIC = 6,
239  	ACPI_MADT_TYPE_LOCAL_SAPIC = 7,
240  	ACPI_MADT_TYPE_INTERRUPT_SOURCE = 8,
241  	ACPI_MADT_TYPE_LOCAL_X2APIC = 9,
242  	ACPI_MADT_TYPE_LOCAL_X2APIC_NMI = 10,
243  	ACPI_MADT_TYPE_GENERIC_INTERRUPT = 11,
244  	ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR = 12,
245  	ACPI_MADT_TYPE_GENERIC_MSI_FRAME = 13,
246  	ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR = 14,
247  	ACPI_MADT_TYPE_GENERIC_TRANSLATOR = 15,
248  	ACPI_MADT_TYPE_RESERVED = 16	/* 16 and greater are reserved */
249  };
250  
251  /* MADT Local APIC flags */
252  #define ACPI_MADT_ENABLED		(1)	/* 00: Processor is usable if set */
253  
254  struct spcr_descriptor {
255  	ACPI_TABLE_HEADER_DEF	/* ACPI common table header */
256  	u8 interface_type;	/* 0=full 16550, 1=subset of 16550 */
257  	u8 reserved[3];
258  	struct acpi_generic_address serial_port;
259  	u8 interrupt_type;
260  	u8 pc_interrupt;
261  	u32 interrupt;
262  	u8 baud_rate;
263  	u8 parity;
264  	u8 stop_bits;
265  	u8 flow_control;
266  	u8 terminal_type;
267  	u8 reserved1;
268  	u16 pci_device_id;
269  	u16 pci_vendor_id;
270  	u8 pci_bus;
271  	u8 pci_device;
272  	u8 pci_function;
273  	u32 pci_flags;
274  	u8 pci_segment;
275  	u32 reserved2;
276  };
277  
278  struct acpi_table_gtdt {
279  	ACPI_TABLE_HEADER_DEF	/* ACPI common table header */
280  	u64 counter_block_addresss;
281  	u32 reserved;
282  	u32 secure_el1_interrupt;
283  	u32 secure_el1_flags;
284  	u32 non_secure_el1_interrupt;
285  	u32 non_secure_el1_flags;
286  	u32 virtual_timer_interrupt;
287  	u32 virtual_timer_flags;
288  	u32 non_secure_el2_interrupt;
289  	u32 non_secure_el2_flags;
290  	u64 counter_read_block_address;
291  	u32 platform_timer_count;
292  	u32 platform_timer_offset;
293  };
294  
295  /* Reset to default packing */
296  #pragma pack()
297  
298  void set_efi_rsdp(struct acpi_table_rsdp *rsdp);
299  void *find_acpi_table_addr(u32 sig);
300  int acpi_table_parse_madt(enum acpi_madt_type mtype, acpi_table_handler handler);
301  
302  #endif
303