1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Helpers for ACPI table generation
4  *
5  * Based on acpi.c from coreboot
6  *
7  * Copyright 2019 Google LLC
8  *
9  * Copyright (C) 2015, Saket Sinha <saket.sinha89@gmail.com>
10  * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
11  */
12 
13 #ifndef __ACPI_TABLE_H__
14 #define __ACPI_TABLE_H__
15 
16 #include <linux/bitops.h>
17 
18 #define RSDP_SIG		"RSD PTR "	/* RSDP pointer signature */
19 #define OEM_ID			"U-BOOT"	/* U-Boot */
20 #define OEM_TABLE_ID		"U-BOOTBL"	/* U-Boot Table */
21 #define ASLC_ID			"INTL"		/* Intel ASL Compiler */
22 
23 #define ACPI_RSDP_REV_ACPI_1_0	0
24 #define ACPI_RSDP_REV_ACPI_2_0	2
25 
26 #if !defined(__ACPI__)
27 
28 struct acpi_ctx;
29 
30 /*
31  * RSDP (Root System Description Pointer)
32  * Note: ACPI 1.0 didn't have length, xsdt_address, and ext_checksum
33  */
34 struct acpi_rsdp {
35 	char signature[8];	/* RSDP signature */
36 	u8 checksum;		/* Checksum of the first 20 bytes */
37 	char oem_id[6];		/* OEM ID */
38 	u8 revision;		/* 0 for ACPI 1.0, others 2 */
39 	u32 rsdt_address;	/* Physical address of RSDT (32 bits) */
40 	u32 length;		/* Total RSDP length (incl. extended part) */
41 	u64 xsdt_address;	/* Physical address of XSDT (64 bits) */
42 	u8 ext_checksum;	/* Checksum of the whole table */
43 	u8 reserved[3];
44 };
45 
46 /* Generic ACPI header, provided by (almost) all tables */
47 struct __packed acpi_table_header {
48 	char signature[4];	/* ACPI signature (4 ASCII characters) */
49 	u32 length;		/* Table length in bytes (incl. header) */
50 	u8 revision;		/* Table version (not ACPI version!) */
51 	volatile u8 checksum;	/* To make sum of entire table == 0 */
52 	char oem_id[6];		/* OEM identification */
53 	char oem_table_id[8];	/* OEM table identification */
54 	u32 oem_revision;	/* OEM revision number */
55 	char aslc_id[4];	/* ASL compiler vendor ID */
56 	u32 aslc_revision;	/* ASL compiler revision number */
57 };
58 
59 /* A maximum number of 32 ACPI tables ought to be enough for now */
60 #define MAX_ACPI_TABLES		32
61 
62 /* RSDT (Root System Description Table) */
63 struct acpi_rsdt {
64 	struct acpi_table_header header;
65 	u32 entry[MAX_ACPI_TABLES];
66 };
67 
68 /* XSDT (Extended System Description Table) */
69 struct acpi_xsdt {
70 	struct acpi_table_header header;
71 	u64 entry[MAX_ACPI_TABLES];
72 };
73 
74 /* FADT Preferred Power Management Profile */
75 enum acpi_pm_profile {
76 	ACPI_PM_UNSPECIFIED = 0,
77 	ACPI_PM_DESKTOP,
78 	ACPI_PM_MOBILE,
79 	ACPI_PM_WORKSTATION,
80 	ACPI_PM_ENTERPRISE_SERVER,
81 	ACPI_PM_SOHO_SERVER,
82 	ACPI_PM_APPLIANCE_PC,
83 	ACPI_PM_PERFORMANCE_SERVER,
84 	ACPI_PM_TABLET
85 };
86 
87 /* FADT flags for p_lvl2_lat and p_lvl3_lat */
88 #define ACPI_FADT_C2_NOT_SUPPORTED	101
89 #define ACPI_FADT_C3_NOT_SUPPORTED	1001
90 
91 /* FADT Boot Architecture Flags */
92 #define ACPI_FADT_LEGACY_FREE		0x00
93 #define ACPI_FADT_LEGACY_DEVICES	BIT(0)
94 #define ACPI_FADT_8042			BIT(1)
95 #define ACPI_FADT_VGA_NOT_PRESENT	BIT(2)
96 #define ACPI_FADT_MSI_NOT_SUPPORTED	BIT(3)
97 #define ACPI_FADT_NO_PCIE_ASPM_CONTROL	BIT(4)
98 
99 /* FADT Feature Flags */
100 #define ACPI_FADT_WBINVD		BIT(0)
101 #define ACPI_FADT_WBINVD_FLUSH		BIT(1)
102 #define ACPI_FADT_C1_SUPPORTED		BIT(2)
103 #define ACPI_FADT_C2_MP_SUPPORTED	BIT(3)
104 #define ACPI_FADT_POWER_BUTTON		BIT(4)
105 #define ACPI_FADT_SLEEP_BUTTON		BIT(5)
106 #define ACPI_FADT_FIXED_RTC		BIT(6)
107 #define ACPI_FADT_S4_RTC_WAKE		BIT(7)
108 #define ACPI_FADT_32BIT_TIMER		BIT(8)
109 #define ACPI_FADT_DOCKING_SUPPORTED	BIT(9)
110 #define ACPI_FADT_RESET_REGISTER	BIT(10)
111 #define ACPI_FADT_SEALED_CASE		BIT(11)
112 #define ACPI_FADT_HEADLESS		BIT(12)
113 #define ACPI_FADT_SLEEP_TYPE		BIT(13)
114 #define ACPI_FADT_PCI_EXPRESS_WAKE	BIT(14)
115 #define ACPI_FADT_PLATFORM_CLOCK	BIT(15)
116 #define ACPI_FADT_S4_RTC_VALID		BIT(16)
117 #define ACPI_FADT_REMOTE_POWER_ON	BIT(17)
118 #define ACPI_FADT_APIC_CLUSTER		BIT(18)
119 #define ACPI_FADT_APIC_PHYSICAL		BIT(19)
120 #define ACPI_FADT_HW_REDUCED_ACPI	BIT(20)
121 #define ACPI_FADT_LOW_PWR_IDLE_S0	BIT(21)
122 
123 enum acpi_address_space_type {
124 	ACPI_ADDRESS_SPACE_MEMORY = 0,	/* System memory */
125 	ACPI_ADDRESS_SPACE_IO,		/* System I/O */
126 	ACPI_ADDRESS_SPACE_PCI,		/* PCI config space */
127 	ACPI_ADDRESS_SPACE_EC,		/* Embedded controller */
128 	ACPI_ADDRESS_SPACE_SMBUS,	/* SMBus */
129 	ACPI_ADDRESS_SPACE_PCC = 0x0a,	/* Platform Comm. Channel */
130 	ACPI_ADDRESS_SPACE_FIXED = 0x7f	/* Functional fixed hardware */
131 };
132 
133 enum acpi_address_space_size {
134 	ACPI_ACCESS_SIZE_UNDEFINED = 0,
135 	ACPI_ACCESS_SIZE_BYTE_ACCESS,
136 	ACPI_ACCESS_SIZE_WORD_ACCESS,
137 	ACPI_ACCESS_SIZE_DWORD_ACCESS,
138 	ACPI_ACCESS_SIZE_QWORD_ACCESS
139 };
140 
141 struct acpi_gen_regaddr {
142 	u8 space_id;	/* Address space ID */
143 	u8 bit_width;	/* Register size in bits */
144 	u8 bit_offset;	/* Register bit offset */
145 	u8 access_size;	/* Access size */
146 	u32 addrl;	/* Register address, low 32 bits */
147 	u32 addrh;	/* Register address, high 32 bits */
148 };
149 
150 /* FADT (Fixed ACPI Description Table) */
151 struct __packed acpi_fadt {
152 	struct acpi_table_header header;
153 	u32 firmware_ctrl;
154 	u32 dsdt;
155 	u8 res1;
156 	u8 preferred_pm_profile;
157 	u16 sci_int;
158 	u32 smi_cmd;
159 	u8 acpi_enable;
160 	u8 acpi_disable;
161 	u8 s4bios_req;
162 	u8 pstate_cnt;
163 	u32 pm1a_evt_blk;
164 	u32 pm1b_evt_blk;
165 	u32 pm1a_cnt_blk;
166 	u32 pm1b_cnt_blk;
167 	u32 pm2_cnt_blk;
168 	u32 pm_tmr_blk;
169 	u32 gpe0_blk;
170 	u32 gpe1_blk;
171 	u8 pm1_evt_len;
172 	u8 pm1_cnt_len;
173 	u8 pm2_cnt_len;
174 	u8 pm_tmr_len;
175 	u8 gpe0_blk_len;
176 	u8 gpe1_blk_len;
177 	u8 gpe1_base;
178 	u8 cst_cnt;
179 	u16 p_lvl2_lat;
180 	u16 p_lvl3_lat;
181 	u16 flush_size;
182 	u16 flush_stride;
183 	u8 duty_offset;
184 	u8 duty_width;
185 	u8 day_alrm;
186 	u8 mon_alrm;
187 	u8 century;
188 	u16 iapc_boot_arch;
189 	u8 res2;
190 	u32 flags;
191 	struct acpi_gen_regaddr reset_reg;
192 	u8 reset_value;
193 	u16 arm_boot_arch;
194 	u8 minor_revision;
195 	u32 x_firmware_ctl_l;
196 	u32 x_firmware_ctl_h;
197 	u32 x_dsdt_l;
198 	u32 x_dsdt_h;
199 	struct acpi_gen_regaddr x_pm1a_evt_blk;
200 	struct acpi_gen_regaddr x_pm1b_evt_blk;
201 	struct acpi_gen_regaddr x_pm1a_cnt_blk;
202 	struct acpi_gen_regaddr x_pm1b_cnt_blk;
203 	struct acpi_gen_regaddr x_pm2_cnt_blk;
204 	struct acpi_gen_regaddr x_pm_tmr_blk;
205 	struct acpi_gen_regaddr x_gpe0_blk;
206 	struct acpi_gen_regaddr x_gpe1_blk;
207 };
208 
209 /* FADT TABLE Revision values - note these do not match the ACPI revision */
210 #define ACPI_FADT_REV_ACPI_1_0		1
211 #define ACPI_FADT_REV_ACPI_2_0		3
212 #define ACPI_FADT_REV_ACPI_3_0		4
213 #define ACPI_FADT_REV_ACPI_4_0		4
214 #define ACPI_FADT_REV_ACPI_5_0		5
215 #define ACPI_FADT_REV_ACPI_6_0		6
216 
217 /* MADT TABLE Revision values - note these do not match the ACPI revision */
218 #define ACPI_MADT_REV_ACPI_3_0		2
219 #define ACPI_MADT_REV_ACPI_4_0		3
220 #define ACPI_MADT_REV_ACPI_5_0		3
221 #define ACPI_MADT_REV_ACPI_6_0		5
222 
223 #define ACPI_MCFG_REV_ACPI_3_0		1
224 
225 /* IVRS Revision Field */
226 #define IVRS_FORMAT_FIXED	0x01	/* Type 10h & 11h only */
227 #define IVRS_FORMAT_MIXED	0x02	/* Type 10h, 11h, & 40h */
228 
229 /* FACS flags */
230 #define ACPI_FACS_S4BIOS_F		BIT(0)
231 #define ACPI_FACS_64BIT_WAKE_F		BIT(1)
232 
233 /* FACS (Firmware ACPI Control Structure) */
234 struct acpi_facs {
235 	char signature[4];		/* "FACS" */
236 	u32 length;			/* Length in bytes (>= 64) */
237 	u32 hardware_signature;		/* Hardware signature */
238 	u32 firmware_waking_vector;	/* Firmware waking vector */
239 	u32 global_lock;		/* Global lock */
240 	u32 flags;			/* FACS flags */
241 	u32 x_firmware_waking_vector_l;	/* X FW waking vector, low */
242 	u32 x_firmware_waking_vector_h;	/* X FW waking vector, high */
243 	u8 version;			/* Version 2 */
244 	u8 res1[3];
245 	u32 ospm_flags;			/* OSPM enabled flags */
246 	u8 res2[24];
247 };
248 
249 /* MADT flags */
250 #define ACPI_MADT_PCAT_COMPAT	BIT(0)
251 
252 /* MADT (Multiple APIC Description Table) */
253 struct acpi_madt {
254 	struct acpi_table_header header;
255 	u32 lapic_addr;			/* Local APIC address */
256 	u32 flags;			/* Multiple APIC flags */
257 };
258 
259 /* MADT: APIC Structure Type*/
260 enum acpi_apic_types {
261 	ACPI_APIC_LAPIC	= 0,		/* Processor local APIC */
262 	ACPI_APIC_IOAPIC,		/* I/O APIC */
263 	ACPI_APIC_IRQ_SRC_OVERRIDE,	/* Interrupt source override */
264 	ACPI_APIC_NMI_SRC,		/* NMI source */
265 	ACPI_APIC_LAPIC_NMI,		/* Local APIC NMI */
266 	ACPI_APIC_LAPIC_ADDR_OVERRIDE,	/* Local APIC address override */
267 	ACPI_APIC_IOSAPIC,		/* I/O SAPIC */
268 	ACPI_APIC_LSAPIC,		/* Local SAPIC */
269 	ACPI_APIC_PLATFORM_IRQ_SRC,	/* Platform interrupt sources */
270 	ACPI_APIC_LX2APIC,		/* Processor local x2APIC */
271 	ACPI_APIC_LX2APIC_NMI,		/* Local x2APIC NMI */
272 };
273 
274 /* MADT: Processor Local APIC Structure */
275 
276 #define LOCAL_APIC_FLAG_ENABLED		BIT(0)
277 
278 struct acpi_madt_lapic {
279 	u8 type;		/* Type (0) */
280 	u8 length;		/* Length in bytes (8) */
281 	u8 processor_id;	/* ACPI processor ID */
282 	u8 apic_id;		/* Local APIC ID */
283 	u32 flags;		/* Local APIC flags */
284 };
285 
286 /* MADT: I/O APIC Structure */
287 struct acpi_madt_ioapic {
288 	u8 type;		/* Type (1) */
289 	u8 length;		/* Length in bytes (12) */
290 	u8 ioapic_id;		/* I/O APIC ID */
291 	u8 reserved;
292 	u32 ioapic_addr;	/* I/O APIC address */
293 	u32 gsi_base;		/* Global system interrupt base */
294 };
295 
296 /* MADT: Interrupt Source Override Structure */
297 struct __packed acpi_madt_irqoverride {
298 	u8 type;		/* Type (2) */
299 	u8 length;		/* Length in bytes (10) */
300 	u8 bus;			/* ISA (0) */
301 	u8 source;		/* Bus-relative int. source (IRQ) */
302 	u32 gsirq;		/* Global system interrupt */
303 	u16 flags;		/* MPS INTI flags */
304 };
305 
306 /* MADT: Local APIC NMI Structure */
307 struct __packed acpi_madt_lapic_nmi {
308 	u8 type;		/* Type (4) */
309 	u8 length;		/* Length in bytes (6) */
310 	u8 processor_id;	/* ACPI processor ID */
311 	u16 flags;		/* MPS INTI flags */
312 	u8 lint;		/* Local APIC LINT# */
313 };
314 
315 /* MCFG (PCI Express MMIO config space BAR description table) */
316 struct acpi_mcfg {
317 	struct acpi_table_header header;
318 	u8 reserved[8];
319 };
320 
321 struct acpi_mcfg_mmconfig {
322 	u32 base_address_l;
323 	u32 base_address_h;
324 	u16 pci_segment_group_number;
325 	u8 start_bus_number;
326 	u8 end_bus_number;
327 	u8 reserved[4];
328 };
329 
330 /* PM1_CNT bit defines */
331 #define PM1_CNT_SCI_EN		BIT(0)
332 
333 /* ACPI global NVS structure */
334 struct acpi_global_nvs;
335 
336 /* CSRT (Core System Resource Table) */
337 struct acpi_csrt {
338 	struct acpi_table_header header;
339 };
340 
341 struct acpi_csrt_group {
342 	u32 length;
343 	u32 vendor_id;
344 	u32 subvendor_id;
345 	u16 device_id;
346 	u16 subdevice_id;
347 	u16 revision;
348 	u16 reserved;
349 	u32 shared_info_length;
350 };
351 
352 struct acpi_csrt_shared_info {
353 	u16 major_version;
354 	u16 minor_version;
355 	u32 mmio_base_low;
356 	u32 mmio_base_high;
357 	u32 gsi_interrupt;
358 	u8 interrupt_polarity;
359 	u8 interrupt_mode;
360 	u8 num_channels;
361 	u8 dma_address_width;
362 	u16 base_request_line;
363 	u16 num_handshake_signals;
364 	u32 max_block_size;
365 };
366 
367 enum dmar_type {
368 	DMAR_DRHD = 0,
369 	DMAR_RMRR = 1,
370 	DMAR_ATSR = 2,
371 	DMAR_RHSA = 3,
372 	DMAR_ANDD = 4
373 };
374 
375 enum {
376 	DRHD_INCLUDE_PCI_ALL = BIT(0)
377 };
378 
379 enum dmar_flags {
380 	DMAR_INTR_REMAP			= BIT(0),
381 	DMAR_X2APIC_OPT_OUT		= BIT(1),
382 	DMAR_CTRL_PLATFORM_OPT_IN_FLAG	= BIT(2),
383 };
384 
385 struct dmar_entry {
386 	u16 type;
387 	u16 length;
388 	u8 flags;
389 	u8 reserved;
390 	u16 segment;
391 	u64 bar;
392 };
393 
394 struct dmar_rmrr_entry {
395 	u16 type;
396 	u16 length;
397 	u16 reserved;
398 	u16 segment;
399 	u64 bar;
400 	u64 limit;
401 };
402 
403 /* DMAR (DMA Remapping Reporting Structure) */
404 struct __packed acpi_dmar {
405 	struct acpi_table_header header;
406 	u8 host_address_width;
407 	u8 flags;
408 	u8 reserved[10];
409 	struct dmar_entry structure[0];
410 };
411 
412 /* DBG2 definitions are partially used for SPCR interface_type */
413 
414 /* Types for port_type field */
415 
416 #define ACPI_DBG2_SERIAL_PORT		0x8000
417 #define ACPI_DBG2_1394_PORT		0x8001
418 #define ACPI_DBG2_USB_PORT		0x8002
419 #define ACPI_DBG2_NET_PORT		0x8003
420 
421 /* Subtypes for port_subtype field */
422 
423 #define ACPI_DBG2_16550_COMPATIBLE	0x0000
424 #define ACPI_DBG2_16550_SUBSET		0x0001
425 #define ACPI_DBG2_ARM_PL011		0x0003
426 #define ACPI_DBG2_ARM_SBSA_32BIT	0x000D
427 #define ACPI_DBG2_ARM_SBSA_GENERIC	0x000E
428 #define ACPI_DBG2_ARM_DCC		0x000F
429 #define ACPI_DBG2_BCM2835		0x0010
430 
431 #define ACPI_DBG2_1394_STANDARD		0x0000
432 
433 #define ACPI_DBG2_USB_XHCI		0x0000
434 #define ACPI_DBG2_USB_EHCI		0x0001
435 
436 #define ACPI_DBG2_UNKNOWN		0x00FF
437 
438 /* SPCR (Serial Port Console Redirection table) */
439 struct __packed acpi_spcr {
440 	struct acpi_table_header header;
441 	u8 interface_type;
442 	u8 reserved[3];
443 	struct acpi_gen_regaddr serial_port;
444 	u8 interrupt_type;
445 	u8 pc_interrupt;
446 	u32 interrupt;		/* Global system interrupt */
447 	u8 baud_rate;
448 	u8 parity;
449 	u8 stop_bits;
450 	u8 flow_control;
451 	u8 terminal_type;
452 	u8 reserved1;
453 	u16 pci_device_id;	/* Must be 0xffff if not PCI device */
454 	u16 pci_vendor_id;	/* Must be 0xffff if not PCI device */
455 	u8 pci_bus;
456 	u8 pci_device;
457 	u8 pci_function;
458 	u32 pci_flags;
459 	u8 pci_segment;
460 	u32 reserved2;
461 };
462 
463 /* Tables defined/reserved by ACPI and generated by U-Boot */
464 enum acpi_tables {
465 	ACPITAB_BERT,
466 	ACPITAB_DBG2,
467 	ACPITAB_DMAR,
468 	ACPITAB_DSDT,
469 	ACPITAB_ECDT,
470 	ACPITAB_FACS,
471 	ACPITAB_FADT,
472 	ACPITAB_HEST,
473 	ACPITAB_HPET,
474 	ACPITAB_IVRS,
475 	ACPITAB_MADT,
476 	ACPITAB_MCFG,
477 	ACPITAB_NHLT,
478 	ACPITAB_RSDP,
479 	ACPITAB_RSDT,
480 	ACPITAB_SLIT,
481 	ACPITAB_SPCR,
482 	ACPITAB_SPMI,
483 	ACPITAB_SRAT,
484 	ACPITAB_SSDT,
485 	ACPITAB_TCPA,
486 	ACPITAB_TPM2,
487 	ACPITAB_VFCT,
488 	ACPITAB_XSDT,
489 
490 	ACPITAB_COUNT,
491 };
492 
493 /**
494  * acpi_get_table_revision() - Get the revision number generated for a table
495  *
496  * This keeps the version-number information in one place
497  *
498  * @table: ACPI table to check
499  * @return version number that U-Boot generates
500  */
501 int acpi_get_table_revision(enum acpi_tables table);
502 
503 /**
504  * acpi_create_dmar() - Create a DMA Remapping Reporting (DMAR) table
505  *
506  * @dmar: Place to put the table
507  * @flags: DMAR flags to use
508  * @return 0 if OK, -ve on error
509  */
510 int acpi_create_dmar(struct acpi_dmar *dmar, enum dmar_flags flags);
511 
512 /**
513  * acpi_fill_header() - Set up a new table header
514  *
515  * This sets all fields except length, revision, checksum and aslc_revision
516  *
517  * @header: ACPI header to update
518  * @signature: Table signature to use (4 characters)
519  */
520 void acpi_fill_header(struct acpi_table_header *header, char *signature);
521 
522 /**
523  * acpi_align() - Align the ACPI output pointer to a 16-byte boundary
524  *
525  * @ctx: ACPI context
526  */
527 void acpi_align(struct acpi_ctx *ctx);
528 
529 /**
530  * acpi_align64() - Align the ACPI output pointer to a 64-byte boundary
531  *
532  * @ctx: ACPI context
533  */
534 void acpi_align64(struct acpi_ctx *ctx);
535 
536 /**
537  * acpi_inc() - Increment the ACPI output pointer by a bit
538  *
539  * The pointer is NOT aligned afterwards.
540  *
541  * @ctx: ACPI context
542  * @amount: Amount to increment by
543  */
544 void acpi_inc(struct acpi_ctx *ctx, uint amount);
545 
546 /**
547  * acpi_inc_align() - Increment the ACPI output pointer by a bit and align
548  *
549  * The pointer is aligned afterwards to a 16-byte boundary
550  *
551  * @ctx: ACPI context
552  * @amount: Amount to increment by
553  */
554 void acpi_inc_align(struct acpi_ctx *ctx, uint amount);
555 
556 /**
557  * acpi_add_table() - Add a new table to the RSDP and XSDT
558  *
559  * @ctx: ACPI context
560  * @table: Table to add
561  * @return 0 if OK, -E2BIG if too many tables
562  */
563 int acpi_add_table(struct acpi_ctx *ctx, void *table);
564 
565 /**
566  * acpi_setup_base_tables() - Set up context along with RSDP, RSDT and XSDT
567  *
568  * Set up the context with the given start position. Some basic tables are
569  * always needed, so set them up as well.
570  *
571  * @ctx: Context to set up
572  */
573 void acpi_setup_base_tables(struct acpi_ctx *ctx, void *start);
574 
575 #endif /* !__ACPI__*/
576 
577 #include <asm/acpi_table.h>
578 
579 #endif /* __ACPI_TABLE_H__ */
580