blob: 270274f6b3b7fa71d2c3586a2e57614013ee7be9 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Saket Sinha867bcb62015-08-22 12:20:55 +05302/*
3 * Based on acpi.c from coreboot
4 *
5 * Copyright (C) 2015, Saket Sinha <saket.sinha89@gmail.com>
Bin Mengab5efd52016-05-07 07:46:25 -07006 * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
Saket Sinha867bcb62015-08-22 12:20:55 +05307 */
8
9#include <common.h>
10#include <cpu.h>
11#include <dm.h>
12#include <dm/uclass-internal.h>
Andy Shevchenkob288cd92018-11-20 23:52:38 +020013#include <serial.h>
Andy Shevchenko684c4cd2017-07-21 22:32:02 +030014#include <version.h>
Bin Meng79c2c252016-06-17 02:13:16 -070015#include <asm/acpi/global_nvs.h>
Saket Sinha867bcb62015-08-22 12:20:55 +053016#include <asm/acpi_table.h>
Andy Shevchenkob156da92017-07-21 22:32:04 +030017#include <asm/ioapic.h>
Saket Sinha867bcb62015-08-22 12:20:55 +053018#include <asm/lapic.h>
Andy Shevchenkob156da92017-07-21 22:32:04 +030019#include <asm/mpspec.h>
Saket Sinha867bcb62015-08-22 12:20:55 +053020#include <asm/tables.h>
Bin Meng79c2c252016-06-17 02:13:16 -070021#include <asm/arch/global_nvs.h>
Saket Sinha867bcb62015-08-22 12:20:55 +053022
23/*
Bin Mengdfbb18b2016-05-07 07:46:24 -070024 * IASL compiles the dsdt entries and writes the hex values
25 * to a C array AmlCode[] (see dsdt.c).
Saket Sinha867bcb62015-08-22 12:20:55 +053026 */
27extern const unsigned char AmlCode[];
28
Andy Shevchenko3469bf42018-01-10 19:40:15 +020029/* ACPI RSDP address to be used in boot parameters */
Bin Meng45410da2018-01-30 05:01:16 -080030static ulong acpi_rsdp_addr;
Andy Shevchenko3469bf42018-01-10 19:40:15 +020031
Bin Mengab5efd52016-05-07 07:46:25 -070032static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
33 struct acpi_xsdt *xsdt)
Saket Sinha867bcb62015-08-22 12:20:55 +053034{
Bin Mengab5efd52016-05-07 07:46:25 -070035 memset(rsdp, 0, sizeof(struct acpi_rsdp));
Saket Sinha867bcb62015-08-22 12:20:55 +053036
Bin Mengab5efd52016-05-07 07:46:25 -070037 memcpy(rsdp->signature, RSDP_SIG, 8);
38 memcpy(rsdp->oem_id, OEM_ID, 6);
Saket Sinha867bcb62015-08-22 12:20:55 +053039
Bin Mengab5efd52016-05-07 07:46:25 -070040 rsdp->length = sizeof(struct acpi_rsdp);
41 rsdp->rsdt_address = (u32)rsdt;
Saket Sinha867bcb62015-08-22 12:20:55 +053042
43 /*
Bin Mengab5efd52016-05-07 07:46:25 -070044 * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
45 *
46 * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
47 * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
48 * revision 0)
Saket Sinha867bcb62015-08-22 12:20:55 +053049 */
Bin Mengab5efd52016-05-07 07:46:25 -070050 if (xsdt == NULL) {
51 rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
52 } else {
53 rsdp->xsdt_address = (u64)(u32)xsdt;
54 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
Saket Sinha867bcb62015-08-22 12:20:55 +053055 }
Saket Sinha867bcb62015-08-22 12:20:55 +053056
Bin Mengab5efd52016-05-07 07:46:25 -070057 /* Calculate checksums */
58 rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
59 rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
60 sizeof(struct acpi_rsdp));
Saket Sinha867bcb62015-08-22 12:20:55 +053061}
62
Bin Mengdfbb18b2016-05-07 07:46:24 -070063void acpi_fill_header(struct acpi_table_header *header, char *signature)
Saket Sinha867bcb62015-08-22 12:20:55 +053064{
Bin Mengdfbb18b2016-05-07 07:46:24 -070065 memcpy(header->signature, signature, 4);
Saket Sinha867bcb62015-08-22 12:20:55 +053066 memcpy(header->oem_id, OEM_ID, 6);
Bin Meng8a8c0352016-05-07 07:46:21 -070067 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
Andy Shevchenko684c4cd2017-07-21 22:32:02 +030068 header->oem_revision = U_BOOT_BUILD_DATE;
Bin Meng8a8c0352016-05-07 07:46:21 -070069 memcpy(header->aslc_id, ASLC_ID, 4);
Saket Sinha867bcb62015-08-22 12:20:55 +053070}
71
Saket Sinha867bcb62015-08-22 12:20:55 +053072static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
73{
Bin Meng8a8c0352016-05-07 07:46:21 -070074 struct acpi_table_header *header = &(rsdt->header);
Saket Sinha867bcb62015-08-22 12:20:55 +053075
76 /* Fill out header fields */
Bin Mengdfbb18b2016-05-07 07:46:24 -070077 acpi_fill_header(header, "RSDT");
Saket Sinha867bcb62015-08-22 12:20:55 +053078 header->length = sizeof(struct acpi_rsdt);
Bin Meng7e6343e2016-05-07 07:46:28 -070079 header->revision = 1;
Saket Sinha867bcb62015-08-22 12:20:55 +053080
81 /* Entries are filled in later, we come with an empty set */
82
83 /* Fix checksum */
84 header->checksum = table_compute_checksum((void *)rsdt,
85 sizeof(struct acpi_rsdt));
86}
87
88static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
89{
Bin Meng8a8c0352016-05-07 07:46:21 -070090 struct acpi_table_header *header = &(xsdt->header);
Saket Sinha867bcb62015-08-22 12:20:55 +053091
92 /* Fill out header fields */
Bin Mengdfbb18b2016-05-07 07:46:24 -070093 acpi_fill_header(header, "XSDT");
Saket Sinha867bcb62015-08-22 12:20:55 +053094 header->length = sizeof(struct acpi_xsdt);
Bin Meng7e6343e2016-05-07 07:46:28 -070095 header->revision = 1;
Saket Sinha867bcb62015-08-22 12:20:55 +053096
97 /* Entries are filled in later, we come with an empty set */
98
99 /* Fix checksum */
100 header->checksum = table_compute_checksum((void *)xsdt,
101 sizeof(struct acpi_xsdt));
102}
103
Bin Mengab5efd52016-05-07 07:46:25 -0700104/**
105 * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
106 * and checksum.
107 */
108static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
Saket Sinha867bcb62015-08-22 12:20:55 +0530109{
Bin Mengab5efd52016-05-07 07:46:25 -0700110 int i, entries_num;
111 struct acpi_rsdt *rsdt;
112 struct acpi_xsdt *xsdt = NULL;
Saket Sinha867bcb62015-08-22 12:20:55 +0530113
Bin Mengab5efd52016-05-07 07:46:25 -0700114 /* The RSDT is mandatory while the XSDT is not */
115 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
Saket Sinha867bcb62015-08-22 12:20:55 +0530116
Bin Mengab5efd52016-05-07 07:46:25 -0700117 if (rsdp->xsdt_address)
118 xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
Saket Sinha867bcb62015-08-22 12:20:55 +0530119
Bin Mengab5efd52016-05-07 07:46:25 -0700120 /* This should always be MAX_ACPI_TABLES */
121 entries_num = ARRAY_SIZE(rsdt->entry);
122
123 for (i = 0; i < entries_num; i++) {
124 if (rsdt->entry[i] == 0)
125 break;
Saket Sinha867bcb62015-08-22 12:20:55 +0530126 }
127
Bin Mengab5efd52016-05-07 07:46:25 -0700128 if (i >= entries_num) {
129 debug("ACPI: Error: too many tables\n");
130 return;
131 }
132
133 /* Add table to the RSDT */
134 rsdt->entry[i] = (u32)table;
135
136 /* Fix RSDT length or the kernel will assume invalid entries */
137 rsdt->header.length = sizeof(struct acpi_table_header) +
138 (sizeof(u32) * (i + 1));
139
140 /* Re-calculate checksum */
141 rsdt->header.checksum = 0;
142 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
143 rsdt->header.length);
144
145 /*
146 * And now the same thing for the XSDT. We use the same index as for
147 * now we want the XSDT and RSDT to always be in sync in U-Boot
148 */
149 if (xsdt) {
150 /* Add table to the XSDT */
151 xsdt->entry[i] = (u64)(u32)table;
152
153 /* Fix XSDT length */
154 xsdt->header.length = sizeof(struct acpi_table_header) +
155 (sizeof(u64) * (i + 1));
156
157 /* Re-calculate checksum */
158 xsdt->header.checksum = 0;
159 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
160 xsdt->header.length);
161 }
162}
163
164static void acpi_create_facs(struct acpi_facs *facs)
165{
166 memset((void *)facs, 0, sizeof(struct acpi_facs));
167
168 memcpy(facs->signature, "FACS", 4);
169 facs->length = sizeof(struct acpi_facs);
170 facs->hardware_signature = 0;
171 facs->firmware_waking_vector = 0;
172 facs->global_lock = 0;
173 facs->flags = 0;
174 facs->x_firmware_waking_vector_l = 0;
175 facs->x_firmware_waking_vector_h = 0;
176 facs->version = 1;
177}
178
179static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
180 u8 cpu, u8 apic)
181{
182 lapic->type = ACPI_APIC_LAPIC;
183 lapic->length = sizeof(struct acpi_madt_lapic);
184 lapic->flags = LOCAL_APIC_FLAG_ENABLED;
185 lapic->processor_id = cpu;
186 lapic->apic_id = apic;
187
188 return lapic->length;
189}
190
Bin Mengfc4f5cc2016-05-07 07:46:30 -0700191int acpi_create_madt_lapics(u32 current)
Bin Mengab5efd52016-05-07 07:46:25 -0700192{
193 struct udevice *dev;
George McCollister8a1a7592016-06-07 13:40:18 -0500194 int total_length = 0;
Bin Mengab5efd52016-05-07 07:46:25 -0700195
196 for (uclass_find_first_device(UCLASS_CPU, &dev);
197 dev;
198 uclass_find_next_device(&dev)) {
199 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
George McCollister8a1a7592016-06-07 13:40:18 -0500200 int length = acpi_create_madt_lapic(
201 (struct acpi_madt_lapic *)current,
202 plat->cpu_id, plat->cpu_id);
Bin Mengfc4f5cc2016-05-07 07:46:30 -0700203 current += length;
George McCollister8a1a7592016-06-07 13:40:18 -0500204 total_length += length;
Bin Mengab5efd52016-05-07 07:46:25 -0700205 }
206
George McCollister8a1a7592016-06-07 13:40:18 -0500207 return total_length;
Bin Mengab5efd52016-05-07 07:46:25 -0700208}
209
210int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
211 u32 addr, u32 gsi_base)
212{
213 ioapic->type = ACPI_APIC_IOAPIC;
214 ioapic->length = sizeof(struct acpi_madt_ioapic);
215 ioapic->reserved = 0x00;
216 ioapic->gsi_base = gsi_base;
217 ioapic->ioapic_id = id;
218 ioapic->ioapic_addr = addr;
219
220 return ioapic->length;
221}
222
223int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
224 u8 bus, u8 source, u32 gsirq, u16 flags)
225{
226 irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
227 irqoverride->length = sizeof(struct acpi_madt_irqoverride);
228 irqoverride->bus = bus;
229 irqoverride->source = source;
230 irqoverride->gsirq = gsirq;
231 irqoverride->flags = flags;
232
233 return irqoverride->length;
234}
235
236int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
237 u8 cpu, u16 flags, u8 lint)
238{
239 lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
240 lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
241 lapic_nmi->flags = flags;
242 lapic_nmi->processor_id = cpu;
243 lapic_nmi->lint = lint;
244
245 return lapic_nmi->length;
246}
247
Andy Shevchenkob156da92017-07-21 22:32:04 +0300248static int acpi_create_madt_irq_overrides(u32 current)
249{
250 struct acpi_madt_irqoverride *irqovr;
251 u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
252 int length = 0;
253
254 irqovr = (void *)current;
255 length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
256
257 irqovr = (void *)(current + length);
258 length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
259
260 return length;
261}
262
263__weak u32 acpi_fill_madt(u32 current)
264{
265 current += acpi_create_madt_lapics(current);
266
267 current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
268 io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
269
270 current += acpi_create_madt_irq_overrides(current);
271
272 return current;
273}
274
Bin Mengab5efd52016-05-07 07:46:25 -0700275static void acpi_create_madt(struct acpi_madt *madt)
276{
277 struct acpi_table_header *header = &(madt->header);
Bin Meng7e79a6b2016-05-07 07:46:26 -0700278 u32 current = (u32)madt + sizeof(struct acpi_madt);
Bin Mengab5efd52016-05-07 07:46:25 -0700279
280 memset((void *)madt, 0, sizeof(struct acpi_madt));
281
282 /* Fill out header fields */
283 acpi_fill_header(header, "APIC");
284 header->length = sizeof(struct acpi_madt);
Bin Meng7e6343e2016-05-07 07:46:28 -0700285 header->revision = 4;
Bin Mengab5efd52016-05-07 07:46:25 -0700286
287 madt->lapic_addr = LAPIC_DEFAULT_BASE;
288 madt->flags = ACPI_MADT_PCAT_COMPAT;
289
290 current = acpi_fill_madt(current);
291
292 /* (Re)calculate length and checksum */
Bin Meng7e79a6b2016-05-07 07:46:26 -0700293 header->length = current - (u32)madt;
Bin Mengab5efd52016-05-07 07:46:25 -0700294
295 header->checksum = table_compute_checksum((void *)madt, header->length);
296}
297
Andy Shevchenkoace77622017-07-21 22:32:05 +0300298int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base,
299 u16 seg_nr, u8 start, u8 end)
Bin Mengab5efd52016-05-07 07:46:25 -0700300{
301 memset(mmconfig, 0, sizeof(*mmconfig));
302 mmconfig->base_address_l = base;
303 mmconfig->base_address_h = 0;
304 mmconfig->pci_segment_group_number = seg_nr;
305 mmconfig->start_bus_number = start;
306 mmconfig->end_bus_number = end;
307
308 return sizeof(struct acpi_mcfg_mmconfig);
309}
310
Andy Shevchenkoace77622017-07-21 22:32:05 +0300311__weak u32 acpi_fill_mcfg(u32 current)
Bin Mengab5efd52016-05-07 07:46:25 -0700312{
313 current += acpi_create_mcfg_mmconfig
314 ((struct acpi_mcfg_mmconfig *)current,
315 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
316
317 return current;
318}
319
320/* MCFG is defined in the PCI Firmware Specification 3.0 */
321static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
322{
323 struct acpi_table_header *header = &(mcfg->header);
Bin Meng7e79a6b2016-05-07 07:46:26 -0700324 u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
Bin Mengab5efd52016-05-07 07:46:25 -0700325
326 memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
327
328 /* Fill out header fields */
329 acpi_fill_header(header, "MCFG");
330 header->length = sizeof(struct acpi_mcfg);
Bin Meng7e6343e2016-05-07 07:46:28 -0700331 header->revision = 1;
Bin Mengab5efd52016-05-07 07:46:25 -0700332
333 current = acpi_fill_mcfg(current);
334
335 /* (Re)calculate length and checksum */
Bin Meng7e79a6b2016-05-07 07:46:26 -0700336 header->length = current - (u32)mcfg;
Bin Mengab5efd52016-05-07 07:46:25 -0700337 header->checksum = table_compute_checksum((void *)mcfg, header->length);
Saket Sinha867bcb62015-08-22 12:20:55 +0530338}
339
Andy Shevchenkob288cd92018-11-20 23:52:38 +0200340static void acpi_create_spcr(struct acpi_spcr *spcr)
341{
342 struct acpi_table_header *header = &(spcr->header);
343 struct serial_device_info serial_info = {0};
344 ulong serial_address, serial_offset;
Simon Glass67d1b052018-12-28 14:23:08 -0700345 struct udevice *dev;
Andy Shevchenkob288cd92018-11-20 23:52:38 +0200346 uint serial_config;
347 uint serial_width;
348 int access_size;
349 int space_id;
Andy Shevchenkoedf18a82019-02-28 17:19:54 +0200350 int ret = -ENODEV;
Andy Shevchenkob288cd92018-11-20 23:52:38 +0200351
352 /* Fill out header fields */
353 acpi_fill_header(header, "SPCR");
354 header->length = sizeof(struct acpi_spcr);
355 header->revision = 2;
356
Simon Glassa61cbad2018-12-28 14:23:10 -0700357 /* Read the device once, here. It is reused below */
Andy Shevchenkoedf18a82019-02-28 17:19:54 +0200358 dev = gd->cur_serial_dev;
359 if (dev)
Simon Glassa61cbad2018-12-28 14:23:10 -0700360 ret = serial_getinfo(dev, &serial_info);
Andy Shevchenkob288cd92018-11-20 23:52:38 +0200361 if (ret)
362 serial_info.type = SERIAL_CHIP_UNKNOWN;
363
364 /* Encode chip type */
365 switch (serial_info.type) {
366 case SERIAL_CHIP_16550_COMPATIBLE:
367 spcr->interface_type = ACPI_DBG2_16550_COMPATIBLE;
368 break;
369 case SERIAL_CHIP_UNKNOWN:
370 default:
371 spcr->interface_type = ACPI_DBG2_UNKNOWN;
372 break;
373 }
374
375 /* Encode address space */
376 switch (serial_info.addr_space) {
377 case SERIAL_ADDRESS_SPACE_MEMORY:
378 space_id = ACPI_ADDRESS_SPACE_MEMORY;
379 break;
380 case SERIAL_ADDRESS_SPACE_IO:
381 default:
382 space_id = ACPI_ADDRESS_SPACE_IO;
383 break;
384 }
385
386 serial_width = serial_info.reg_width * 8;
387 serial_offset = serial_info.reg_offset << serial_info.reg_shift;
388 serial_address = serial_info.addr + serial_offset;
389
390 /* Encode register access size */
391 switch (serial_info.reg_shift) {
392 case 0:
393 access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
394 break;
395 case 1:
396 access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
397 break;
398 case 2:
399 access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
400 break;
401 case 3:
402 access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
403 break;
404 default:
405 access_size = ACPI_ACCESS_SIZE_UNDEFINED;
406 break;
407 }
408
409 debug("UART type %u @ %lx\n", spcr->interface_type, serial_address);
410
411 /* Fill GAS */
412 spcr->serial_port.space_id = space_id;
413 spcr->serial_port.bit_width = serial_width;
414 spcr->serial_port.bit_offset = 0;
415 spcr->serial_port.access_size = access_size;
416 spcr->serial_port.addrl = lower_32_bits(serial_address);
417 spcr->serial_port.addrh = upper_32_bits(serial_address);
418
419 /* Encode baud rate */
420 switch (serial_info.baudrate) {
421 case 9600:
422 spcr->baud_rate = 3;
423 break;
424 case 19200:
425 spcr->baud_rate = 4;
426 break;
427 case 57600:
428 spcr->baud_rate = 6;
429 break;
430 case 115200:
431 spcr->baud_rate = 7;
432 break;
433 default:
434 spcr->baud_rate = 0;
435 break;
436 }
437
Simon Glassa61cbad2018-12-28 14:23:10 -0700438 serial_config = SERIAL_DEFAULT_CONFIG;
439 if (dev)
Simon Glass67d1b052018-12-28 14:23:08 -0700440 ret = serial_getconfig(dev, &serial_config);
Andy Shevchenkob288cd92018-11-20 23:52:38 +0200441
442 spcr->parity = SERIAL_GET_PARITY(serial_config);
443 spcr->stop_bits = SERIAL_GET_STOP(serial_config);
444
445 /* No PCI devices for now */
446 spcr->pci_device_id = 0xffff;
447 spcr->pci_vendor_id = 0xffff;
448
449 /* Fix checksum */
450 header->checksum = table_compute_checksum((void *)spcr, header->length);
451}
452
Miao Yanfa287b12016-01-20 01:57:06 -0800453/*
Andy Shevchenko7b36dbd2018-01-10 19:33:10 +0200454 * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
Miao Yanfa287b12016-01-20 01:57:06 -0800455 */
Simon Glass42fd8c12017-01-16 07:03:35 -0700456ulong write_acpi_tables(ulong start)
Saket Sinha867bcb62015-08-22 12:20:55 +0530457{
Bin Meng358bb3f2016-02-27 22:58:00 -0800458 u32 current;
Saket Sinha867bcb62015-08-22 12:20:55 +0530459 struct acpi_rsdp *rsdp;
460 struct acpi_rsdt *rsdt;
461 struct acpi_xsdt *xsdt;
462 struct acpi_facs *facs;
Bin Meng8a8c0352016-05-07 07:46:21 -0700463 struct acpi_table_header *dsdt;
Saket Sinha867bcb62015-08-22 12:20:55 +0530464 struct acpi_fadt *fadt;
465 struct acpi_mcfg *mcfg;
466 struct acpi_madt *madt;
Andy Shevchenkob288cd92018-11-20 23:52:38 +0200467 struct acpi_spcr *spcr;
Bin Meng79c2c252016-06-17 02:13:16 -0700468 int i;
Saket Sinha867bcb62015-08-22 12:20:55 +0530469
470 current = start;
471
Bin Mengab5efd52016-05-07 07:46:25 -0700472 /* Align ACPI tables to 16 byte */
Saket Sinha867bcb62015-08-22 12:20:55 +0530473 current = ALIGN(current, 16);
474
Simon Glass42fd8c12017-01-16 07:03:35 -0700475 debug("ACPI: Writing ACPI tables at %lx\n", start);
Saket Sinha867bcb62015-08-22 12:20:55 +0530476
477 /* We need at least an RSDP and an RSDT Table */
478 rsdp = (struct acpi_rsdp *)current;
479 current += sizeof(struct acpi_rsdp);
480 current = ALIGN(current, 16);
481 rsdt = (struct acpi_rsdt *)current;
482 current += sizeof(struct acpi_rsdt);
483 current = ALIGN(current, 16);
484 xsdt = (struct acpi_xsdt *)current;
485 current += sizeof(struct acpi_xsdt);
Bin Meng25e133e2016-05-07 07:46:27 -0700486 /*
487 * Per ACPI spec, the FACS table address must be aligned to a 64 byte
488 * boundary (Windows checks this, but Linux does not).
489 */
490 current = ALIGN(current, 64);
Saket Sinha867bcb62015-08-22 12:20:55 +0530491
492 /* clear all table memory */
493 memset((void *)start, 0, current - start);
494
495 acpi_write_rsdp(rsdp, rsdt, xsdt);
496 acpi_write_rsdt(rsdt);
497 acpi_write_xsdt(xsdt);
498
499 debug("ACPI: * FACS\n");
500 facs = (struct acpi_facs *)current;
501 current += sizeof(struct acpi_facs);
502 current = ALIGN(current, 16);
503
504 acpi_create_facs(facs);
505
506 debug("ACPI: * DSDT\n");
Bin Meng8a8c0352016-05-07 07:46:21 -0700507 dsdt = (struct acpi_table_header *)current;
508 memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
Bin Meng10fcabe2016-05-11 07:45:05 -0700509 current += sizeof(struct acpi_table_header);
510 memcpy((char *)current,
511 (char *)&AmlCode + sizeof(struct acpi_table_header),
512 dsdt->length - sizeof(struct acpi_table_header));
513 current += dsdt->length - sizeof(struct acpi_table_header);
Saket Sinha867bcb62015-08-22 12:20:55 +0530514 current = ALIGN(current, 16);
515
Bin Meng79c2c252016-06-17 02:13:16 -0700516 /* Pack GNVS into the ACPI table area */
517 for (i = 0; i < dsdt->length; i++) {
518 u32 *gnvs = (u32 *)((u32)dsdt + i);
519 if (*gnvs == ACPI_GNVS_ADDR) {
520 debug("Fix up global NVS in DSDT to 0x%08x\n", current);
521 *gnvs = current;
522 break;
523 }
524 }
525
526 /* Update DSDT checksum since we patched the GNVS address */
527 dsdt->checksum = 0;
528 dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
529
530 /* Fill in platform-specific global NVS variables */
531 acpi_create_gnvs((struct acpi_global_nvs *)current);
532 current += sizeof(struct acpi_global_nvs);
533 current = ALIGN(current, 16);
534
Saket Sinha867bcb62015-08-22 12:20:55 +0530535 debug("ACPI: * FADT\n");
536 fadt = (struct acpi_fadt *)current;
537 current += sizeof(struct acpi_fadt);
538 current = ALIGN(current, 16);
539 acpi_create_fadt(fadt, facs, dsdt);
540 acpi_add_table(rsdp, fadt);
541
Saket Sinha867bcb62015-08-22 12:20:55 +0530542 debug("ACPI: * MADT\n");
543 madt = (struct acpi_madt *)current;
544 acpi_create_madt(madt);
Bin Meng10fcabe2016-05-11 07:45:05 -0700545 current += madt->header.length;
546 acpi_add_table(rsdp, madt);
Saket Sinha867bcb62015-08-22 12:20:55 +0530547 current = ALIGN(current, 16);
548
Bin Mengab5efd52016-05-07 07:46:25 -0700549 debug("ACPI: * MCFG\n");
550 mcfg = (struct acpi_mcfg *)current;
551 acpi_create_mcfg(mcfg);
Bin Meng10fcabe2016-05-11 07:45:05 -0700552 current += mcfg->header.length;
553 acpi_add_table(rsdp, mcfg);
554 current = ALIGN(current, 16);
Bin Mengab5efd52016-05-07 07:46:25 -0700555
Andy Shevchenkob288cd92018-11-20 23:52:38 +0200556 debug("ACPI: * SPCR\n");
557 spcr = (struct acpi_spcr *)current;
558 acpi_create_spcr(spcr);
559 current += spcr->header.length;
560 acpi_add_table(rsdp, spcr);
561 current = ALIGN(current, 16);
562
Bin Mengdca4d1a2016-05-07 07:46:12 -0700563 debug("current = %x\n", current);
Saket Sinha867bcb62015-08-22 12:20:55 +0530564
Andy Shevchenko3469bf42018-01-10 19:40:15 +0200565 acpi_rsdp_addr = (unsigned long)rsdp;
Bin Mengdca4d1a2016-05-07 07:46:12 -0700566 debug("ACPI: done\n");
Saket Sinha867bcb62015-08-22 12:20:55 +0530567
Saket Sinha867bcb62015-08-22 12:20:55 +0530568 return current;
569}
Bin Menge76bf382017-04-21 07:24:36 -0700570
Bin Meng45410da2018-01-30 05:01:16 -0800571ulong acpi_get_rsdp_addr(void)
572{
573 return acpi_rsdp_addr;
574}