xref: /openbmc/u-boot/arch/x86/lib/acpi_table.c (revision 9702ec00)
1 /*
2  * Based on acpi.c from coreboot
3  *
4  * Copyright (C) 2015, Saket Sinha <saket.sinha89@gmail.com>
5  * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
6  *
7  * SPDX-License-Identifier: GPL-2.0+
8  */
9 
10 #include <common.h>
11 #include <cpu.h>
12 #include <dm.h>
13 #include <dm/uclass-internal.h>
14 #include <asm/acpi_table.h>
15 #include <asm/io.h>
16 #include <asm/lapic.h>
17 #include <asm/tables.h>
18 
19 /*
20  * IASL compiles the dsdt entries and writes the hex values
21  * to a C array AmlCode[] (see dsdt.c).
22  */
23 extern const unsigned char AmlCode[];
24 
25 static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
26 			    struct acpi_xsdt *xsdt)
27 {
28 	memset(rsdp, 0, sizeof(struct acpi_rsdp));
29 
30 	memcpy(rsdp->signature, RSDP_SIG, 8);
31 	memcpy(rsdp->oem_id, OEM_ID, 6);
32 
33 	rsdp->length = sizeof(struct acpi_rsdp);
34 	rsdp->rsdt_address = (u32)rsdt;
35 
36 	/*
37 	 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
38 	 *
39 	 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
40 	 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
41 	 * revision 0)
42 	 */
43 	if (xsdt == NULL) {
44 		rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
45 	} else {
46 		rsdp->xsdt_address = (u64)(u32)xsdt;
47 		rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
48 	}
49 
50 	/* Calculate checksums */
51 	rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
52 	rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
53 			sizeof(struct acpi_rsdp));
54 }
55 
56 void acpi_fill_header(struct acpi_table_header *header, char *signature)
57 {
58 	memcpy(header->signature, signature, 4);
59 	memcpy(header->oem_id, OEM_ID, 6);
60 	memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
61 	memcpy(header->aslc_id, ASLC_ID, 4);
62 }
63 
64 static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
65 {
66 	struct acpi_table_header *header = &(rsdt->header);
67 
68 	/* Fill out header fields */
69 	acpi_fill_header(header, "RSDT");
70 	header->length = sizeof(struct acpi_rsdt);
71 	header->revision = 1;
72 
73 	/* Entries are filled in later, we come with an empty set */
74 
75 	/* Fix checksum */
76 	header->checksum = table_compute_checksum((void *)rsdt,
77 			sizeof(struct acpi_rsdt));
78 }
79 
80 static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
81 {
82 	struct acpi_table_header *header = &(xsdt->header);
83 
84 	/* Fill out header fields */
85 	acpi_fill_header(header, "XSDT");
86 	header->length = sizeof(struct acpi_xsdt);
87 	header->revision = 1;
88 
89 	/* Entries are filled in later, we come with an empty set */
90 
91 	/* Fix checksum */
92 	header->checksum = table_compute_checksum((void *)xsdt,
93 			sizeof(struct acpi_xsdt));
94 }
95 
96 /**
97  * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
98  * and checksum.
99  */
100 static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
101 {
102 	int i, entries_num;
103 	struct acpi_rsdt *rsdt;
104 	struct acpi_xsdt *xsdt = NULL;
105 
106 	/* The RSDT is mandatory while the XSDT is not */
107 	rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
108 
109 	if (rsdp->xsdt_address)
110 		xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
111 
112 	/* This should always be MAX_ACPI_TABLES */
113 	entries_num = ARRAY_SIZE(rsdt->entry);
114 
115 	for (i = 0; i < entries_num; i++) {
116 		if (rsdt->entry[i] == 0)
117 			break;
118 	}
119 
120 	if (i >= entries_num) {
121 		debug("ACPI: Error: too many tables\n");
122 		return;
123 	}
124 
125 	/* Add table to the RSDT */
126 	rsdt->entry[i] = (u32)table;
127 
128 	/* Fix RSDT length or the kernel will assume invalid entries */
129 	rsdt->header.length = sizeof(struct acpi_table_header) +
130 				(sizeof(u32) * (i + 1));
131 
132 	/* Re-calculate checksum */
133 	rsdt->header.checksum = 0;
134 	rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
135 			rsdt->header.length);
136 
137 	/*
138 	 * And now the same thing for the XSDT. We use the same index as for
139 	 * now we want the XSDT and RSDT to always be in sync in U-Boot
140 	 */
141 	if (xsdt) {
142 		/* Add table to the XSDT */
143 		xsdt->entry[i] = (u64)(u32)table;
144 
145 		/* Fix XSDT length */
146 		xsdt->header.length = sizeof(struct acpi_table_header) +
147 			(sizeof(u64) * (i + 1));
148 
149 		/* Re-calculate checksum */
150 		xsdt->header.checksum = 0;
151 		xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
152 				xsdt->header.length);
153 	}
154 }
155 
156 static void acpi_create_facs(struct acpi_facs *facs)
157 {
158 	memset((void *)facs, 0, sizeof(struct acpi_facs));
159 
160 	memcpy(facs->signature, "FACS", 4);
161 	facs->length = sizeof(struct acpi_facs);
162 	facs->hardware_signature = 0;
163 	facs->firmware_waking_vector = 0;
164 	facs->global_lock = 0;
165 	facs->flags = 0;
166 	facs->x_firmware_waking_vector_l = 0;
167 	facs->x_firmware_waking_vector_h = 0;
168 	facs->version = 1;
169 }
170 
171 static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
172 				  u8 cpu, u8 apic)
173 {
174 	lapic->type = ACPI_APIC_LAPIC;
175 	lapic->length = sizeof(struct acpi_madt_lapic);
176 	lapic->flags = LOCAL_APIC_FLAG_ENABLED;
177 	lapic->processor_id = cpu;
178 	lapic->apic_id = apic;
179 
180 	return lapic->length;
181 }
182 
183 int acpi_create_madt_lapics(u32 current)
184 {
185 	struct udevice *dev;
186 	int total_length = 0;
187 
188 	for (uclass_find_first_device(UCLASS_CPU, &dev);
189 	     dev;
190 	     uclass_find_next_device(&dev)) {
191 		struct cpu_platdata *plat = dev_get_parent_platdata(dev);
192 		int length = acpi_create_madt_lapic(
193 				(struct acpi_madt_lapic *)current,
194 				plat->cpu_id, plat->cpu_id);
195 		current += length;
196 		total_length += length;
197 	}
198 
199 	return total_length;
200 }
201 
202 int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
203 			    u32 addr, u32 gsi_base)
204 {
205 	ioapic->type = ACPI_APIC_IOAPIC;
206 	ioapic->length = sizeof(struct acpi_madt_ioapic);
207 	ioapic->reserved = 0x00;
208 	ioapic->gsi_base = gsi_base;
209 	ioapic->ioapic_id = id;
210 	ioapic->ioapic_addr = addr;
211 
212 	return ioapic->length;
213 }
214 
215 int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
216 				 u8 bus, u8 source, u32 gsirq, u16 flags)
217 {
218 	irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
219 	irqoverride->length = sizeof(struct acpi_madt_irqoverride);
220 	irqoverride->bus = bus;
221 	irqoverride->source = source;
222 	irqoverride->gsirq = gsirq;
223 	irqoverride->flags = flags;
224 
225 	return irqoverride->length;
226 }
227 
228 int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
229 			       u8 cpu, u16 flags, u8 lint)
230 {
231 	lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
232 	lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
233 	lapic_nmi->flags = flags;
234 	lapic_nmi->processor_id = cpu;
235 	lapic_nmi->lint = lint;
236 
237 	return lapic_nmi->length;
238 }
239 
240 static void acpi_create_madt(struct acpi_madt *madt)
241 {
242 	struct acpi_table_header *header = &(madt->header);
243 	u32 current = (u32)madt + sizeof(struct acpi_madt);
244 
245 	memset((void *)madt, 0, sizeof(struct acpi_madt));
246 
247 	/* Fill out header fields */
248 	acpi_fill_header(header, "APIC");
249 	header->length = sizeof(struct acpi_madt);
250 	header->revision = 4;
251 
252 	madt->lapic_addr = LAPIC_DEFAULT_BASE;
253 	madt->flags = ACPI_MADT_PCAT_COMPAT;
254 
255 	current = acpi_fill_madt(current);
256 
257 	/* (Re)calculate length and checksum */
258 	header->length = current - (u32)madt;
259 
260 	header->checksum = table_compute_checksum((void *)madt, header->length);
261 }
262 
263 static int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig,
264 				     u32 base, u16 seg_nr, u8 start, u8 end)
265 {
266 	memset(mmconfig, 0, sizeof(*mmconfig));
267 	mmconfig->base_address_l = base;
268 	mmconfig->base_address_h = 0;
269 	mmconfig->pci_segment_group_number = seg_nr;
270 	mmconfig->start_bus_number = start;
271 	mmconfig->end_bus_number = end;
272 
273 	return sizeof(struct acpi_mcfg_mmconfig);
274 }
275 
276 static u32 acpi_fill_mcfg(u32 current)
277 {
278 	current += acpi_create_mcfg_mmconfig
279 		((struct acpi_mcfg_mmconfig *)current,
280 		CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
281 
282 	return current;
283 }
284 
285 /* MCFG is defined in the PCI Firmware Specification 3.0 */
286 static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
287 {
288 	struct acpi_table_header *header = &(mcfg->header);
289 	u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
290 
291 	memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
292 
293 	/* Fill out header fields */
294 	acpi_fill_header(header, "MCFG");
295 	header->length = sizeof(struct acpi_mcfg);
296 	header->revision = 1;
297 
298 	current = acpi_fill_mcfg(current);
299 
300 	/* (Re)calculate length and checksum */
301 	header->length = current - (u32)mcfg;
302 	header->checksum = table_compute_checksum((void *)mcfg, header->length);
303 }
304 
305 static void enter_acpi_mode(int pm1_cnt)
306 {
307 	/*
308 	 * PM1_CNT register bit0 selects the power management event to be
309 	 * either an SCI or SMI interrupt. When this bit is set, then power
310 	 * management events will generate an SCI interrupt. When this bit
311 	 * is reset power management events will generate an SMI interrupt.
312 	 *
313 	 * Per ACPI spec, it is the responsibility of the hardware to set
314 	 * or reset this bit. OSPM always preserves this bit position.
315 	 *
316 	 * U-Boot does not support SMI. And we don't have plan to support
317 	 * anything running in SMM within U-Boot. To create a legacy-free
318 	 * system, and expose ourselves to OSPM as working under ACPI mode
319 	 * already, turn this bit on.
320 	 */
321 	outw(PM1_CNT_SCI_EN, pm1_cnt);
322 }
323 
324 /*
325  * QEMU's version of write_acpi_tables is defined in
326  * arch/x86/cpu/qemu/acpi_table.c
327  */
328 u32 write_acpi_tables(u32 start)
329 {
330 	u32 current;
331 	struct acpi_rsdp *rsdp;
332 	struct acpi_rsdt *rsdt;
333 	struct acpi_xsdt *xsdt;
334 	struct acpi_facs *facs;
335 	struct acpi_table_header *dsdt;
336 	struct acpi_fadt *fadt;
337 	struct acpi_mcfg *mcfg;
338 	struct acpi_madt *madt;
339 
340 	current = start;
341 
342 	/* Align ACPI tables to 16 byte */
343 	current = ALIGN(current, 16);
344 
345 	debug("ACPI: Writing ACPI tables at %x\n", start);
346 
347 	/* We need at least an RSDP and an RSDT Table */
348 	rsdp = (struct acpi_rsdp *)current;
349 	current += sizeof(struct acpi_rsdp);
350 	current = ALIGN(current, 16);
351 	rsdt = (struct acpi_rsdt *)current;
352 	current += sizeof(struct acpi_rsdt);
353 	current = ALIGN(current, 16);
354 	xsdt = (struct acpi_xsdt *)current;
355 	current += sizeof(struct acpi_xsdt);
356 	/*
357 	 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
358 	 * boundary (Windows checks this, but Linux does not).
359 	 */
360 	current = ALIGN(current, 64);
361 
362 	/* clear all table memory */
363 	memset((void *)start, 0, current - start);
364 
365 	acpi_write_rsdp(rsdp, rsdt, xsdt);
366 	acpi_write_rsdt(rsdt);
367 	acpi_write_xsdt(xsdt);
368 
369 	debug("ACPI:    * FACS\n");
370 	facs = (struct acpi_facs *)current;
371 	current += sizeof(struct acpi_facs);
372 	current = ALIGN(current, 16);
373 
374 	acpi_create_facs(facs);
375 
376 	debug("ACPI:    * DSDT\n");
377 	dsdt = (struct acpi_table_header *)current;
378 	memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
379 	current += sizeof(struct acpi_table_header);
380 	memcpy((char *)current,
381 	       (char *)&AmlCode + sizeof(struct acpi_table_header),
382 	       dsdt->length - sizeof(struct acpi_table_header));
383 	current += dsdt->length - sizeof(struct acpi_table_header);
384 	current = ALIGN(current, 16);
385 
386 	debug("ACPI:    * FADT\n");
387 	fadt = (struct acpi_fadt *)current;
388 	current += sizeof(struct acpi_fadt);
389 	current = ALIGN(current, 16);
390 	acpi_create_fadt(fadt, facs, dsdt);
391 	acpi_add_table(rsdp, fadt);
392 
393 	debug("ACPI:    * MADT\n");
394 	madt = (struct acpi_madt *)current;
395 	acpi_create_madt(madt);
396 	current += madt->header.length;
397 	acpi_add_table(rsdp, madt);
398 	current = ALIGN(current, 16);
399 
400 	debug("ACPI:    * MCFG\n");
401 	mcfg = (struct acpi_mcfg *)current;
402 	acpi_create_mcfg(mcfg);
403 	current += mcfg->header.length;
404 	acpi_add_table(rsdp, mcfg);
405 	current = ALIGN(current, 16);
406 
407 	debug("current = %x\n", current);
408 
409 	debug("ACPI: done\n");
410 
411 	/*
412 	 * Other than waiting for OSPM to request us to switch to ACPI mode,
413 	 * do it by ourselves, since SMI will not be triggered.
414 	 */
415 	enter_acpi_mode(fadt->pm1a_cnt_blk);
416 
417 	return current;
418 }
419