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