xref: /rk3399_rockchip-uboot/arch/x86/lib/acpi_table.c (revision 2dcbef6f6c43737b3e9244478c6e768a1e73babf)
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 <version.h>
15 #include <asm/acpi/global_nvs.h>
16 #include <asm/acpi_table.h>
17 #include <asm/io.h>
18 #include <asm/lapic.h>
19 #include <asm/tables.h>
20 #include <asm/arch/global_nvs.h>
21 
22 /*
23  * IASL compiles the dsdt entries and writes the hex values
24  * to a C array AmlCode[] (see dsdt.c).
25  */
26 extern const unsigned char AmlCode[];
27 
28 static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
29 			    struct acpi_xsdt *xsdt)
30 {
31 	memset(rsdp, 0, sizeof(struct acpi_rsdp));
32 
33 	memcpy(rsdp->signature, RSDP_SIG, 8);
34 	memcpy(rsdp->oem_id, OEM_ID, 6);
35 
36 	rsdp->length = sizeof(struct acpi_rsdp);
37 	rsdp->rsdt_address = (u32)rsdt;
38 
39 	/*
40 	 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
41 	 *
42 	 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
43 	 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
44 	 * revision 0)
45 	 */
46 	if (xsdt == NULL) {
47 		rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
48 	} else {
49 		rsdp->xsdt_address = (u64)(u32)xsdt;
50 		rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
51 	}
52 
53 	/* Calculate checksums */
54 	rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
55 	rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
56 			sizeof(struct acpi_rsdp));
57 }
58 
59 void acpi_fill_header(struct acpi_table_header *header, char *signature)
60 {
61 	memcpy(header->signature, signature, 4);
62 	memcpy(header->oem_id, OEM_ID, 6);
63 	memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
64 	header->oem_revision = U_BOOT_BUILD_DATE;
65 	memcpy(header->aslc_id, ASLC_ID, 4);
66 }
67 
68 static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
69 {
70 	struct acpi_table_header *header = &(rsdt->header);
71 
72 	/* Fill out header fields */
73 	acpi_fill_header(header, "RSDT");
74 	header->length = sizeof(struct acpi_rsdt);
75 	header->revision = 1;
76 
77 	/* Entries are filled in later, we come with an empty set */
78 
79 	/* Fix checksum */
80 	header->checksum = table_compute_checksum((void *)rsdt,
81 			sizeof(struct acpi_rsdt));
82 }
83 
84 static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
85 {
86 	struct acpi_table_header *header = &(xsdt->header);
87 
88 	/* Fill out header fields */
89 	acpi_fill_header(header, "XSDT");
90 	header->length = sizeof(struct acpi_xsdt);
91 	header->revision = 1;
92 
93 	/* Entries are filled in later, we come with an empty set */
94 
95 	/* Fix checksum */
96 	header->checksum = table_compute_checksum((void *)xsdt,
97 			sizeof(struct acpi_xsdt));
98 }
99 
100 /**
101  * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
102  * and checksum.
103  */
104 static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
105 {
106 	int i, entries_num;
107 	struct acpi_rsdt *rsdt;
108 	struct acpi_xsdt *xsdt = NULL;
109 
110 	/* The RSDT is mandatory while the XSDT is not */
111 	rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
112 
113 	if (rsdp->xsdt_address)
114 		xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
115 
116 	/* This should always be MAX_ACPI_TABLES */
117 	entries_num = ARRAY_SIZE(rsdt->entry);
118 
119 	for (i = 0; i < entries_num; i++) {
120 		if (rsdt->entry[i] == 0)
121 			break;
122 	}
123 
124 	if (i >= entries_num) {
125 		debug("ACPI: Error: too many tables\n");
126 		return;
127 	}
128 
129 	/* Add table to the RSDT */
130 	rsdt->entry[i] = (u32)table;
131 
132 	/* Fix RSDT length or the kernel will assume invalid entries */
133 	rsdt->header.length = sizeof(struct acpi_table_header) +
134 				(sizeof(u32) * (i + 1));
135 
136 	/* Re-calculate checksum */
137 	rsdt->header.checksum = 0;
138 	rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
139 			rsdt->header.length);
140 
141 	/*
142 	 * And now the same thing for the XSDT. We use the same index as for
143 	 * now we want the XSDT and RSDT to always be in sync in U-Boot
144 	 */
145 	if (xsdt) {
146 		/* Add table to the XSDT */
147 		xsdt->entry[i] = (u64)(u32)table;
148 
149 		/* Fix XSDT length */
150 		xsdt->header.length = sizeof(struct acpi_table_header) +
151 			(sizeof(u64) * (i + 1));
152 
153 		/* Re-calculate checksum */
154 		xsdt->header.checksum = 0;
155 		xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
156 				xsdt->header.length);
157 	}
158 }
159 
160 static void acpi_create_facs(struct acpi_facs *facs)
161 {
162 	memset((void *)facs, 0, sizeof(struct acpi_facs));
163 
164 	memcpy(facs->signature, "FACS", 4);
165 	facs->length = sizeof(struct acpi_facs);
166 	facs->hardware_signature = 0;
167 	facs->firmware_waking_vector = 0;
168 	facs->global_lock = 0;
169 	facs->flags = 0;
170 	facs->x_firmware_waking_vector_l = 0;
171 	facs->x_firmware_waking_vector_h = 0;
172 	facs->version = 1;
173 }
174 
175 static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
176 				  u8 cpu, u8 apic)
177 {
178 	lapic->type = ACPI_APIC_LAPIC;
179 	lapic->length = sizeof(struct acpi_madt_lapic);
180 	lapic->flags = LOCAL_APIC_FLAG_ENABLED;
181 	lapic->processor_id = cpu;
182 	lapic->apic_id = apic;
183 
184 	return lapic->length;
185 }
186 
187 int acpi_create_madt_lapics(u32 current)
188 {
189 	struct udevice *dev;
190 	int total_length = 0;
191 
192 	for (uclass_find_first_device(UCLASS_CPU, &dev);
193 	     dev;
194 	     uclass_find_next_device(&dev)) {
195 		struct cpu_platdata *plat = dev_get_parent_platdata(dev);
196 		int length = acpi_create_madt_lapic(
197 				(struct acpi_madt_lapic *)current,
198 				plat->cpu_id, plat->cpu_id);
199 		current += length;
200 		total_length += length;
201 	}
202 
203 	return total_length;
204 }
205 
206 int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
207 			    u32 addr, u32 gsi_base)
208 {
209 	ioapic->type = ACPI_APIC_IOAPIC;
210 	ioapic->length = sizeof(struct acpi_madt_ioapic);
211 	ioapic->reserved = 0x00;
212 	ioapic->gsi_base = gsi_base;
213 	ioapic->ioapic_id = id;
214 	ioapic->ioapic_addr = addr;
215 
216 	return ioapic->length;
217 }
218 
219 int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
220 				 u8 bus, u8 source, u32 gsirq, u16 flags)
221 {
222 	irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
223 	irqoverride->length = sizeof(struct acpi_madt_irqoverride);
224 	irqoverride->bus = bus;
225 	irqoverride->source = source;
226 	irqoverride->gsirq = gsirq;
227 	irqoverride->flags = flags;
228 
229 	return irqoverride->length;
230 }
231 
232 int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
233 			       u8 cpu, u16 flags, u8 lint)
234 {
235 	lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
236 	lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
237 	lapic_nmi->flags = flags;
238 	lapic_nmi->processor_id = cpu;
239 	lapic_nmi->lint = lint;
240 
241 	return lapic_nmi->length;
242 }
243 
244 static void acpi_create_madt(struct acpi_madt *madt)
245 {
246 	struct acpi_table_header *header = &(madt->header);
247 	u32 current = (u32)madt + sizeof(struct acpi_madt);
248 
249 	memset((void *)madt, 0, sizeof(struct acpi_madt));
250 
251 	/* Fill out header fields */
252 	acpi_fill_header(header, "APIC");
253 	header->length = sizeof(struct acpi_madt);
254 	header->revision = 4;
255 
256 	madt->lapic_addr = LAPIC_DEFAULT_BASE;
257 	madt->flags = ACPI_MADT_PCAT_COMPAT;
258 
259 	current = acpi_fill_madt(current);
260 
261 	/* (Re)calculate length and checksum */
262 	header->length = current - (u32)madt;
263 
264 	header->checksum = table_compute_checksum((void *)madt, header->length);
265 }
266 
267 static int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig,
268 				     u32 base, u16 seg_nr, u8 start, u8 end)
269 {
270 	memset(mmconfig, 0, sizeof(*mmconfig));
271 	mmconfig->base_address_l = base;
272 	mmconfig->base_address_h = 0;
273 	mmconfig->pci_segment_group_number = seg_nr;
274 	mmconfig->start_bus_number = start;
275 	mmconfig->end_bus_number = end;
276 
277 	return sizeof(struct acpi_mcfg_mmconfig);
278 }
279 
280 static u32 acpi_fill_mcfg(u32 current)
281 {
282 	current += acpi_create_mcfg_mmconfig
283 		((struct acpi_mcfg_mmconfig *)current,
284 		CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
285 
286 	return current;
287 }
288 
289 /* MCFG is defined in the PCI Firmware Specification 3.0 */
290 static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
291 {
292 	struct acpi_table_header *header = &(mcfg->header);
293 	u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
294 
295 	memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
296 
297 	/* Fill out header fields */
298 	acpi_fill_header(header, "MCFG");
299 	header->length = sizeof(struct acpi_mcfg);
300 	header->revision = 1;
301 
302 	current = acpi_fill_mcfg(current);
303 
304 	/* (Re)calculate length and checksum */
305 	header->length = current - (u32)mcfg;
306 	header->checksum = table_compute_checksum((void *)mcfg, header->length);
307 }
308 
309 void enter_acpi_mode(int pm1_cnt)
310 {
311 	u16 val = inw(pm1_cnt);
312 
313 	/*
314 	 * PM1_CNT register bit0 selects the power management event to be
315 	 * either an SCI or SMI interrupt. When this bit is set, then power
316 	 * management events will generate an SCI interrupt. When this bit
317 	 * is reset power management events will generate an SMI interrupt.
318 	 *
319 	 * Per ACPI spec, it is the responsibility of the hardware to set
320 	 * or reset this bit. OSPM always preserves this bit position.
321 	 *
322 	 * U-Boot does not support SMI. And we don't have plan to support
323 	 * anything running in SMM within U-Boot. To create a legacy-free
324 	 * system, and expose ourselves to OSPM as working under ACPI mode
325 	 * already, turn this bit on.
326 	 */
327 	outw(val | PM1_CNT_SCI_EN, pm1_cnt);
328 }
329 
330 /*
331  * QEMU's version of write_acpi_tables is defined in
332  * arch/x86/cpu/qemu/acpi_table.c
333  */
334 ulong write_acpi_tables(ulong start)
335 {
336 	u32 current;
337 	struct acpi_rsdp *rsdp;
338 	struct acpi_rsdt *rsdt;
339 	struct acpi_xsdt *xsdt;
340 	struct acpi_facs *facs;
341 	struct acpi_table_header *dsdt;
342 	struct acpi_fadt *fadt;
343 	struct acpi_mcfg *mcfg;
344 	struct acpi_madt *madt;
345 	int i;
346 
347 	current = start;
348 
349 	/* Align ACPI tables to 16 byte */
350 	current = ALIGN(current, 16);
351 
352 	debug("ACPI: Writing ACPI tables at %lx\n", start);
353 
354 	/* We need at least an RSDP and an RSDT Table */
355 	rsdp = (struct acpi_rsdp *)current;
356 	current += sizeof(struct acpi_rsdp);
357 	current = ALIGN(current, 16);
358 	rsdt = (struct acpi_rsdt *)current;
359 	current += sizeof(struct acpi_rsdt);
360 	current = ALIGN(current, 16);
361 	xsdt = (struct acpi_xsdt *)current;
362 	current += sizeof(struct acpi_xsdt);
363 	/*
364 	 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
365 	 * boundary (Windows checks this, but Linux does not).
366 	 */
367 	current = ALIGN(current, 64);
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 = (struct acpi_table_header *)current;
385 	memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
386 	current += sizeof(struct acpi_table_header);
387 	memcpy((char *)current,
388 	       (char *)&AmlCode + sizeof(struct acpi_table_header),
389 	       dsdt->length - sizeof(struct acpi_table_header));
390 	current += dsdt->length - sizeof(struct acpi_table_header);
391 	current = ALIGN(current, 16);
392 
393 	/* Pack GNVS into the ACPI table area */
394 	for (i = 0; i < dsdt->length; i++) {
395 		u32 *gnvs = (u32 *)((u32)dsdt + i);
396 		if (*gnvs == ACPI_GNVS_ADDR) {
397 			debug("Fix up global NVS in DSDT to 0x%08x\n", current);
398 			*gnvs = current;
399 			break;
400 		}
401 	}
402 
403 	/* Update DSDT checksum since we patched the GNVS address */
404 	dsdt->checksum = 0;
405 	dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
406 
407 	/* Fill in platform-specific global NVS variables */
408 	acpi_create_gnvs((struct acpi_global_nvs *)current);
409 	current += sizeof(struct acpi_global_nvs);
410 	current = ALIGN(current, 16);
411 
412 	debug("ACPI:    * FADT\n");
413 	fadt = (struct acpi_fadt *)current;
414 	current += sizeof(struct acpi_fadt);
415 	current = ALIGN(current, 16);
416 	acpi_create_fadt(fadt, facs, dsdt);
417 	acpi_add_table(rsdp, fadt);
418 
419 	debug("ACPI:    * MADT\n");
420 	madt = (struct acpi_madt *)current;
421 	acpi_create_madt(madt);
422 	current += madt->header.length;
423 	acpi_add_table(rsdp, madt);
424 	current = ALIGN(current, 16);
425 
426 	debug("ACPI:    * MCFG\n");
427 	mcfg = (struct acpi_mcfg *)current;
428 	acpi_create_mcfg(mcfg);
429 	current += mcfg->header.length;
430 	acpi_add_table(rsdp, mcfg);
431 	current = ALIGN(current, 16);
432 
433 	debug("current = %x\n", current);
434 
435 	debug("ACPI: done\n");
436 
437 	/*
438 	 * Other than waiting for OSPM to request us to switch to ACPI mode,
439 	 * do it by ourselves, since SMI will not be triggered.
440 	 */
441 	enter_acpi_mode(fadt->pm1a_cnt_blk);
442 
443 	return current;
444 }
445 
446 static struct acpi_rsdp *acpi_valid_rsdp(struct acpi_rsdp *rsdp)
447 {
448 	if (strncmp((char *)rsdp, RSDP_SIG, sizeof(RSDP_SIG) - 1) != 0)
449 		return NULL;
450 
451 	debug("Looking on %p for valid checksum\n", rsdp);
452 
453 	if (table_compute_checksum((void *)rsdp, 20) != 0)
454 		return NULL;
455 	debug("acpi rsdp checksum 1 passed\n");
456 
457 	if ((rsdp->revision > 1) &&
458 	    (table_compute_checksum((void *)rsdp, rsdp->length) != 0))
459 		return NULL;
460 	debug("acpi rsdp checksum 2 passed\n");
461 
462 	return rsdp;
463 }
464 
465 struct acpi_fadt *acpi_find_fadt(void)
466 {
467 	char *p, *end;
468 	struct acpi_rsdp *rsdp = NULL;
469 	struct acpi_rsdt *rsdt;
470 	struct acpi_fadt *fadt = NULL;
471 	int i;
472 
473 	/* Find RSDP */
474 	for (p = (char *)ROM_TABLE_ADDR; p < (char *)ROM_TABLE_END; p += 16) {
475 		rsdp = acpi_valid_rsdp((struct acpi_rsdp *)p);
476 		if (rsdp)
477 			break;
478 	}
479 
480 	if (rsdp == NULL)
481 		return NULL;
482 
483 	debug("RSDP found at %p\n", rsdp);
484 	rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
485 
486 	end = (char *)rsdt + rsdt->header.length;
487 	debug("RSDT found at %p ends at %p\n", rsdt, end);
488 
489 	for (i = 0; ((char *)&rsdt->entry[i]) < end; i++) {
490 		fadt = (struct acpi_fadt *)rsdt->entry[i];
491 		if (strncmp((char *)fadt, "FACP", 4) == 0)
492 			break;
493 		fadt = NULL;
494 	}
495 
496 	if (fadt == NULL)
497 		return NULL;
498 
499 	debug("FADT found at %p\n", fadt);
500 	return fadt;
501 }
502 
503 void *acpi_find_wakeup_vector(struct acpi_fadt *fadt)
504 {
505 	struct acpi_facs *facs;
506 	void *wake_vec;
507 
508 	debug("Trying to find the wakeup vector...\n");
509 
510 	facs = (struct acpi_facs *)fadt->firmware_ctrl;
511 
512 	if (facs == NULL) {
513 		debug("No FACS found, wake up from S3 not possible.\n");
514 		return NULL;
515 	}
516 
517 	debug("FACS found at %p\n", facs);
518 	wake_vec = (void *)facs->firmware_waking_vector;
519 	debug("OS waking vector is %p\n", wake_vec);
520 
521 	return wake_vec;
522 }
523