acpi: Add a method to write tables for a device
[oweals/u-boot.git] / arch / x86 / lib / acpi_table.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Based on acpi.c from coreboot
4  *
5  * Copyright (C) 2015, Saket Sinha <saket.sinha89@gmail.com>
6  * Copyright (C) 2016, Bin Meng <bmeng.cn@gmail.com>
7  */
8
9 #include <common.h>
10 #include <cpu.h>
11 #include <dm.h>
12 #include <dm/uclass-internal.h>
13 #include <serial.h>
14 #include <version.h>
15 #include <acpi/acpi_table.h>
16 #include <asm/acpi/global_nvs.h>
17 #include <asm/ioapic.h>
18 #include <asm/lapic.h>
19 #include <asm/mpspec.h>
20 #include <asm/tables.h>
21 #include <asm/arch/global_nvs.h>
22
23 /*
24  * IASL compiles the dsdt entries and writes the hex values
25  * to a C array AmlCode[] (see dsdt.c).
26  */
27 extern const unsigned char AmlCode[];
28
29 /* ACPI RSDP address to be used in boot parameters */
30 static ulong acpi_rsdp_addr;
31
32 static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
33                             struct acpi_xsdt *xsdt)
34 {
35         memset(rsdp, 0, sizeof(struct acpi_rsdp));
36
37         memcpy(rsdp->signature, RSDP_SIG, 8);
38         memcpy(rsdp->oem_id, OEM_ID, 6);
39
40         rsdp->length = sizeof(struct acpi_rsdp);
41         rsdp->rsdt_address = (u32)rsdt;
42
43         /*
44          * 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)
49          */
50         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;
55         }
56
57         /* 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));
61 }
62
63 static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
64 {
65         struct acpi_table_header *header = &(rsdt->header);
66
67         /* Fill out header fields */
68         acpi_fill_header(header, "RSDT");
69         header->length = sizeof(struct acpi_rsdt);
70         header->revision = 1;
71
72         /* Entries are filled in later, we come with an empty set */
73
74         /* Fix checksum */
75         header->checksum = table_compute_checksum((void *)rsdt,
76                         sizeof(struct acpi_rsdt));
77 }
78
79 static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
80 {
81         struct acpi_table_header *header = &(xsdt->header);
82
83         /* Fill out header fields */
84         acpi_fill_header(header, "XSDT");
85         header->length = sizeof(struct acpi_xsdt);
86         header->revision = 1;
87
88         /* Entries are filled in later, we come with an empty set */
89
90         /* Fix checksum */
91         header->checksum = table_compute_checksum((void *)xsdt,
92                         sizeof(struct acpi_xsdt));
93 }
94
95 /**
96  * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
97  * and checksum.
98  */
99 static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
100 {
101         int i, entries_num;
102         struct acpi_rsdt *rsdt;
103         struct acpi_xsdt *xsdt;
104
105         /* The RSDT is mandatory while the XSDT is not */
106         rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
107
108         /* This should always be MAX_ACPI_TABLES */
109         entries_num = ARRAY_SIZE(rsdt->entry);
110
111         for (i = 0; i < entries_num; i++) {
112                 if (rsdt->entry[i] == 0)
113                         break;
114         }
115
116         if (i >= entries_num) {
117                 debug("ACPI: Error: too many tables\n");
118                 return;
119         }
120
121         /* Add table to the RSDT */
122         rsdt->entry[i] = (u32)table;
123
124         /* Fix RSDT length or the kernel will assume invalid entries */
125         rsdt->header.length = sizeof(struct acpi_table_header) +
126                                 sizeof(u32) * (i + 1);
127
128         /* Re-calculate checksum */
129         rsdt->header.checksum = 0;
130         rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
131                         rsdt->header.length);
132
133         /* The RSDT is mandatory while the XSDT is not */
134         if (!rsdp->xsdt_address)
135                 return;
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         xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
142
143         /* Add table to the XSDT */
144         xsdt->entry[i] = (u64)(u32)table;
145
146         /* Fix XSDT length */
147         xsdt->header.length = sizeof(struct acpi_table_header) +
148                                 sizeof(u64) * (i + 1);
149
150         /* Re-calculate checksum */
151         xsdt->header.checksum = 0;
152         xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
153                         xsdt->header.length);
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 int acpi_create_madt_irq_overrides(u32 current)
241 {
242         struct acpi_madt_irqoverride *irqovr;
243         u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
244         int length = 0;
245
246         irqovr = (void *)current;
247         length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
248
249         irqovr = (void *)(current + length);
250         length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
251
252         return length;
253 }
254
255 __weak u32 acpi_fill_madt(u32 current)
256 {
257         current += acpi_create_madt_lapics(current);
258
259         current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
260                         io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
261
262         current += acpi_create_madt_irq_overrides(current);
263
264         return current;
265 }
266
267 static void acpi_create_madt(struct acpi_madt *madt)
268 {
269         struct acpi_table_header *header = &(madt->header);
270         u32 current = (u32)madt + sizeof(struct acpi_madt);
271
272         memset((void *)madt, 0, sizeof(struct acpi_madt));
273
274         /* Fill out header fields */
275         acpi_fill_header(header, "APIC");
276         header->length = sizeof(struct acpi_madt);
277         header->revision = 4;
278
279         madt->lapic_addr = LAPIC_DEFAULT_BASE;
280         madt->flags = ACPI_MADT_PCAT_COMPAT;
281
282         current = acpi_fill_madt(current);
283
284         /* (Re)calculate length and checksum */
285         header->length = current - (u32)madt;
286
287         header->checksum = table_compute_checksum((void *)madt, header->length);
288 }
289
290 int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base,
291                               u16 seg_nr, u8 start, u8 end)
292 {
293         memset(mmconfig, 0, sizeof(*mmconfig));
294         mmconfig->base_address_l = base;
295         mmconfig->base_address_h = 0;
296         mmconfig->pci_segment_group_number = seg_nr;
297         mmconfig->start_bus_number = start;
298         mmconfig->end_bus_number = end;
299
300         return sizeof(struct acpi_mcfg_mmconfig);
301 }
302
303 __weak u32 acpi_fill_mcfg(u32 current)
304 {
305         current += acpi_create_mcfg_mmconfig
306                 ((struct acpi_mcfg_mmconfig *)current,
307                 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
308
309         return current;
310 }
311
312 /* MCFG is defined in the PCI Firmware Specification 3.0 */
313 static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
314 {
315         struct acpi_table_header *header = &(mcfg->header);
316         u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
317
318         memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
319
320         /* Fill out header fields */
321         acpi_fill_header(header, "MCFG");
322         header->length = sizeof(struct acpi_mcfg);
323         header->revision = 1;
324
325         current = acpi_fill_mcfg(current);
326
327         /* (Re)calculate length and checksum */
328         header->length = current - (u32)mcfg;
329         header->checksum = table_compute_checksum((void *)mcfg, header->length);
330 }
331
332 __weak u32 acpi_fill_csrt(u32 current)
333 {
334         return current;
335 }
336
337 static void acpi_create_csrt(struct acpi_csrt *csrt)
338 {
339         struct acpi_table_header *header = &(csrt->header);
340         u32 current = (u32)csrt + sizeof(struct acpi_csrt);
341
342         memset((void *)csrt, 0, sizeof(struct acpi_csrt));
343
344         /* Fill out header fields */
345         acpi_fill_header(header, "CSRT");
346         header->length = sizeof(struct acpi_csrt);
347         header->revision = 0;
348
349         current = acpi_fill_csrt(current);
350
351         /* (Re)calculate length and checksum */
352         header->length = current - (u32)csrt;
353         header->checksum = table_compute_checksum((void *)csrt, header->length);
354 }
355
356 static void acpi_create_spcr(struct acpi_spcr *spcr)
357 {
358         struct acpi_table_header *header = &(spcr->header);
359         struct serial_device_info serial_info = {0};
360         ulong serial_address, serial_offset;
361         struct udevice *dev;
362         uint serial_config;
363         uint serial_width;
364         int access_size;
365         int space_id;
366         int ret = -ENODEV;
367
368         /* Fill out header fields */
369         acpi_fill_header(header, "SPCR");
370         header->length = sizeof(struct acpi_spcr);
371         header->revision = 2;
372
373         /* Read the device once, here. It is reused below */
374         dev = gd->cur_serial_dev;
375         if (dev)
376                 ret = serial_getinfo(dev, &serial_info);
377         if (ret)
378                 serial_info.type = SERIAL_CHIP_UNKNOWN;
379
380         /* Encode chip type */
381         switch (serial_info.type) {
382         case SERIAL_CHIP_16550_COMPATIBLE:
383                 spcr->interface_type = ACPI_DBG2_16550_COMPATIBLE;
384                 break;
385         case SERIAL_CHIP_UNKNOWN:
386         default:
387                 spcr->interface_type = ACPI_DBG2_UNKNOWN;
388                 break;
389         }
390
391         /* Encode address space */
392         switch (serial_info.addr_space) {
393         case SERIAL_ADDRESS_SPACE_MEMORY:
394                 space_id = ACPI_ADDRESS_SPACE_MEMORY;
395                 break;
396         case SERIAL_ADDRESS_SPACE_IO:
397         default:
398                 space_id = ACPI_ADDRESS_SPACE_IO;
399                 break;
400         }
401
402         serial_width = serial_info.reg_width * 8;
403         serial_offset = serial_info.reg_offset << serial_info.reg_shift;
404         serial_address = serial_info.addr + serial_offset;
405
406         /* Encode register access size */
407         switch (serial_info.reg_shift) {
408         case 0:
409                 access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
410                 break;
411         case 1:
412                 access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
413                 break;
414         case 2:
415                 access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
416                 break;
417         case 3:
418                 access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
419                 break;
420         default:
421                 access_size = ACPI_ACCESS_SIZE_UNDEFINED;
422                 break;
423         }
424
425         debug("UART type %u @ %lx\n", spcr->interface_type, serial_address);
426
427         /* Fill GAS */
428         spcr->serial_port.space_id = space_id;
429         spcr->serial_port.bit_width = serial_width;
430         spcr->serial_port.bit_offset = 0;
431         spcr->serial_port.access_size = access_size;
432         spcr->serial_port.addrl = lower_32_bits(serial_address);
433         spcr->serial_port.addrh = upper_32_bits(serial_address);
434
435         /* Encode baud rate */
436         switch (serial_info.baudrate) {
437         case 9600:
438                 spcr->baud_rate = 3;
439                 break;
440         case 19200:
441                 spcr->baud_rate = 4;
442                 break;
443         case 57600:
444                 spcr->baud_rate = 6;
445                 break;
446         case 115200:
447                 spcr->baud_rate = 7;
448                 break;
449         default:
450                 spcr->baud_rate = 0;
451                 break;
452         }
453
454         serial_config = SERIAL_DEFAULT_CONFIG;
455         if (dev)
456                 ret = serial_getconfig(dev, &serial_config);
457
458         spcr->parity = SERIAL_GET_PARITY(serial_config);
459         spcr->stop_bits = SERIAL_GET_STOP(serial_config);
460
461         /* No PCI devices for now */
462         spcr->pci_device_id = 0xffff;
463         spcr->pci_vendor_id = 0xffff;
464
465         /*
466          * SPCR has no clue if the UART base clock speed is different
467          * to the default one. However, the SPCR 1.04 defines baud rate
468          * 0 as a preconfigured state of UART and OS is supposed not
469          * to touch the configuration of the serial device.
470          */
471         if (serial_info.clock != SERIAL_DEFAULT_CLOCK)
472                 spcr->baud_rate = 0;
473
474         /* Fix checksum */
475         header->checksum = table_compute_checksum((void *)spcr, header->length);
476 }
477
478 /*
479  * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
480  */
481 ulong write_acpi_tables(ulong start)
482 {
483         u32 current;
484         struct acpi_rsdp *rsdp;
485         struct acpi_rsdt *rsdt;
486         struct acpi_xsdt *xsdt;
487         struct acpi_facs *facs;
488         struct acpi_table_header *dsdt;
489         struct acpi_fadt *fadt;
490         struct acpi_mcfg *mcfg;
491         struct acpi_madt *madt;
492         struct acpi_csrt *csrt;
493         struct acpi_spcr *spcr;
494         int i;
495
496         current = start;
497
498         /* Align ACPI tables to 16 byte */
499         current = ALIGN(current, 16);
500
501         debug("ACPI: Writing ACPI tables at %lx\n", start);
502
503         /* We need at least an RSDP and an RSDT Table */
504         rsdp = (struct acpi_rsdp *)current;
505         current += sizeof(struct acpi_rsdp);
506         current = ALIGN(current, 16);
507         rsdt = (struct acpi_rsdt *)current;
508         current += sizeof(struct acpi_rsdt);
509         current = ALIGN(current, 16);
510         xsdt = (struct acpi_xsdt *)current;
511         current += sizeof(struct acpi_xsdt);
512         /*
513          * Per ACPI spec, the FACS table address must be aligned to a 64 byte
514          * boundary (Windows checks this, but Linux does not).
515          */
516         current = ALIGN(current, 64);
517
518         /* clear all table memory */
519         memset((void *)start, 0, current - start);
520
521         acpi_write_rsdp(rsdp, rsdt, xsdt);
522         acpi_write_rsdt(rsdt);
523         acpi_write_xsdt(xsdt);
524
525         debug("ACPI:    * FACS\n");
526         facs = (struct acpi_facs *)current;
527         current += sizeof(struct acpi_facs);
528         current = ALIGN(current, 16);
529
530         acpi_create_facs(facs);
531
532         debug("ACPI:    * DSDT\n");
533         dsdt = (struct acpi_table_header *)current;
534         memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
535         current += sizeof(struct acpi_table_header);
536         memcpy((char *)current,
537                (char *)&AmlCode + sizeof(struct acpi_table_header),
538                dsdt->length - sizeof(struct acpi_table_header));
539         current += dsdt->length - sizeof(struct acpi_table_header);
540         current = ALIGN(current, 16);
541
542         /* Pack GNVS into the ACPI table area */
543         for (i = 0; i < dsdt->length; i++) {
544                 u32 *gnvs = (u32 *)((u32)dsdt + i);
545                 if (*gnvs == ACPI_GNVS_ADDR) {
546                         debug("Fix up global NVS in DSDT to 0x%08x\n", current);
547                         *gnvs = current;
548                         break;
549                 }
550         }
551
552         /* Update DSDT checksum since we patched the GNVS address */
553         dsdt->checksum = 0;
554         dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
555
556         /* Fill in platform-specific global NVS variables */
557         acpi_create_gnvs((struct acpi_global_nvs *)current);
558         current += sizeof(struct acpi_global_nvs);
559         current = ALIGN(current, 16);
560
561         debug("ACPI:    * FADT\n");
562         fadt = (struct acpi_fadt *)current;
563         current += sizeof(struct acpi_fadt);
564         current = ALIGN(current, 16);
565         acpi_create_fadt(fadt, facs, dsdt);
566         acpi_add_table(rsdp, fadt);
567
568         debug("ACPI:    * MADT\n");
569         madt = (struct acpi_madt *)current;
570         acpi_create_madt(madt);
571         current += madt->header.length;
572         acpi_add_table(rsdp, madt);
573         current = ALIGN(current, 16);
574
575         debug("ACPI:    * MCFG\n");
576         mcfg = (struct acpi_mcfg *)current;
577         acpi_create_mcfg(mcfg);
578         current += mcfg->header.length;
579         acpi_add_table(rsdp, mcfg);
580         current = ALIGN(current, 16);
581
582         debug("ACPI:    * CSRT\n");
583         csrt = (struct acpi_csrt *)current;
584         acpi_create_csrt(csrt);
585         current += csrt->header.length;
586         acpi_add_table(rsdp, csrt);
587         current = ALIGN(current, 16);
588
589         debug("ACPI:    * SPCR\n");
590         spcr = (struct acpi_spcr *)current;
591         acpi_create_spcr(spcr);
592         current += spcr->header.length;
593         acpi_add_table(rsdp, spcr);
594         current = ALIGN(current, 16);
595
596         debug("current = %x\n", current);
597
598         acpi_rsdp_addr = (unsigned long)rsdp;
599         debug("ACPI: done\n");
600
601         return current;
602 }
603
604 ulong acpi_get_rsdp_addr(void)
605 {
606         return acpi_rsdp_addr;
607 }