b73cc6433974d7702d6888e8fd8fe53fd5ee91c2
[platform/kernel/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 <mapmem.h>
14 #include <serial.h>
15 #include <version.h>
16 #include <acpi/acpi_table.h>
17 #include <asm/acpi/global_nvs.h>
18 #include <asm/ioapic.h>
19 #include <asm/lapic.h>
20 #include <asm/mpspec.h>
21 #include <asm/tables.h>
22 #include <asm/arch/global_nvs.h>
23 #include <dm/acpi.h>
24
25 /*
26  * IASL compiles the dsdt entries and writes the hex values
27  * to a C array AmlCode[] (see dsdt.c).
28  */
29 extern const unsigned char AmlCode[];
30
31 /* ACPI RSDP address to be used in boot parameters */
32 static ulong acpi_rsdp_addr;
33
34 static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
35                             struct acpi_xsdt *xsdt)
36 {
37         memset(rsdp, 0, sizeof(struct acpi_rsdp));
38
39         memcpy(rsdp->signature, RSDP_SIG, 8);
40         memcpy(rsdp->oem_id, OEM_ID, 6);
41
42         rsdp->length = sizeof(struct acpi_rsdp);
43         rsdp->rsdt_address = (u32)rsdt;
44
45         /*
46          * Revision: ACPI 1.0: 0, ACPI 2.0/3.0/4.0: 2
47          *
48          * Some OSes expect an XSDT to be present for RSD PTR revisions >= 2.
49          * If we don't have an ACPI XSDT, force ACPI 1.0 (and thus RSD PTR
50          * revision 0)
51          */
52         if (xsdt == NULL) {
53                 rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
54         } else {
55                 rsdp->xsdt_address = (u64)(u32)xsdt;
56                 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
57         }
58
59         /* Calculate checksums */
60         rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
61         rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
62                         sizeof(struct acpi_rsdp));
63 }
64
65 static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
66 {
67         struct acpi_table_header *header = &(rsdt->header);
68
69         /* Fill out header fields */
70         acpi_fill_header(header, "RSDT");
71         header->length = sizeof(struct acpi_rsdt);
72         header->revision = 1;
73
74         /* Entries are filled in later, we come with an empty set */
75
76         /* Fix checksum */
77         header->checksum = table_compute_checksum((void *)rsdt,
78                         sizeof(struct acpi_rsdt));
79 }
80
81 static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
82 {
83         struct acpi_table_header *header = &(xsdt->header);
84
85         /* Fill out header fields */
86         acpi_fill_header(header, "XSDT");
87         header->length = sizeof(struct acpi_xsdt);
88         header->revision = 1;
89
90         /* Entries are filled in later, we come with an empty set */
91
92         /* Fix checksum */
93         header->checksum = table_compute_checksum((void *)xsdt,
94                         sizeof(struct acpi_xsdt));
95 }
96
97 /**
98  * Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
99  * and checksum.
100  */
101 static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
102 {
103         int i, entries_num;
104         struct acpi_rsdt *rsdt;
105         struct acpi_xsdt *xsdt;
106
107         /* The RSDT is mandatory while the XSDT is not */
108         rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
109
110         /* This should always be MAX_ACPI_TABLES */
111         entries_num = ARRAY_SIZE(rsdt->entry);
112
113         for (i = 0; i < entries_num; i++) {
114                 if (rsdt->entry[i] == 0)
115                         break;
116         }
117
118         if (i >= entries_num) {
119                 debug("ACPI: Error: too many tables\n");
120                 return;
121         }
122
123         /* Add table to the RSDT */
124         rsdt->entry[i] = (u32)table;
125
126         /* Fix RSDT length or the kernel will assume invalid entries */
127         rsdt->header.length = sizeof(struct acpi_table_header) +
128                                 sizeof(u32) * (i + 1);
129
130         /* Re-calculate checksum */
131         rsdt->header.checksum = 0;
132         rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
133                         rsdt->header.length);
134
135         /* The RSDT is mandatory while the XSDT is not */
136         if (!rsdp->xsdt_address)
137                 return;
138
139         /*
140          * And now the same thing for the XSDT. We use the same index as for
141          * now we want the XSDT and RSDT to always be in sync in U-Boot
142          */
143         xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
144
145         /* Add table to the XSDT */
146         xsdt->entry[i] = (u64)(u32)table;
147
148         /* Fix XSDT length */
149         xsdt->header.length = sizeof(struct acpi_table_header) +
150                                 sizeof(u64) * (i + 1);
151
152         /* Re-calculate checksum */
153         xsdt->header.checksum = 0;
154         xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
155                         xsdt->header.length);
156 }
157
158 static void acpi_create_facs(struct acpi_facs *facs)
159 {
160         memset((void *)facs, 0, sizeof(struct acpi_facs));
161
162         memcpy(facs->signature, "FACS", 4);
163         facs->length = sizeof(struct acpi_facs);
164         facs->hardware_signature = 0;
165         facs->firmware_waking_vector = 0;
166         facs->global_lock = 0;
167         facs->flags = 0;
168         facs->x_firmware_waking_vector_l = 0;
169         facs->x_firmware_waking_vector_h = 0;
170         facs->version = 1;
171 }
172
173 static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
174                                   u8 cpu, u8 apic)
175 {
176         lapic->type = ACPI_APIC_LAPIC;
177         lapic->length = sizeof(struct acpi_madt_lapic);
178         lapic->flags = LOCAL_APIC_FLAG_ENABLED;
179         lapic->processor_id = cpu;
180         lapic->apic_id = apic;
181
182         return lapic->length;
183 }
184
185 int acpi_create_madt_lapics(u32 current)
186 {
187         struct udevice *dev;
188         int total_length = 0;
189
190         for (uclass_find_first_device(UCLASS_CPU, &dev);
191              dev;
192              uclass_find_next_device(&dev)) {
193                 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
194                 int length = acpi_create_madt_lapic(
195                                 (struct acpi_madt_lapic *)current,
196                                 plat->cpu_id, plat->cpu_id);
197                 current += length;
198                 total_length += length;
199         }
200
201         return total_length;
202 }
203
204 int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
205                             u32 addr, u32 gsi_base)
206 {
207         ioapic->type = ACPI_APIC_IOAPIC;
208         ioapic->length = sizeof(struct acpi_madt_ioapic);
209         ioapic->reserved = 0x00;
210         ioapic->gsi_base = gsi_base;
211         ioapic->ioapic_id = id;
212         ioapic->ioapic_addr = addr;
213
214         return ioapic->length;
215 }
216
217 int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
218                                  u8 bus, u8 source, u32 gsirq, u16 flags)
219 {
220         irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
221         irqoverride->length = sizeof(struct acpi_madt_irqoverride);
222         irqoverride->bus = bus;
223         irqoverride->source = source;
224         irqoverride->gsirq = gsirq;
225         irqoverride->flags = flags;
226
227         return irqoverride->length;
228 }
229
230 int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
231                                u8 cpu, u16 flags, u8 lint)
232 {
233         lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
234         lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
235         lapic_nmi->flags = flags;
236         lapic_nmi->processor_id = cpu;
237         lapic_nmi->lint = lint;
238
239         return lapic_nmi->length;
240 }
241
242 static int acpi_create_madt_irq_overrides(u32 current)
243 {
244         struct acpi_madt_irqoverride *irqovr;
245         u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
246         int length = 0;
247
248         irqovr = (void *)current;
249         length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
250
251         irqovr = (void *)(current + length);
252         length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
253
254         return length;
255 }
256
257 __weak u32 acpi_fill_madt(u32 current)
258 {
259         current += acpi_create_madt_lapics(current);
260
261         current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
262                         io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
263
264         current += acpi_create_madt_irq_overrides(current);
265
266         return current;
267 }
268
269 static void acpi_create_madt(struct acpi_madt *madt)
270 {
271         struct acpi_table_header *header = &(madt->header);
272         u32 current = (u32)madt + sizeof(struct acpi_madt);
273
274         memset((void *)madt, 0, sizeof(struct acpi_madt));
275
276         /* Fill out header fields */
277         acpi_fill_header(header, "APIC");
278         header->length = sizeof(struct acpi_madt);
279         header->revision = 4;
280
281         madt->lapic_addr = LAPIC_DEFAULT_BASE;
282         madt->flags = ACPI_MADT_PCAT_COMPAT;
283
284         current = acpi_fill_madt(current);
285
286         /* (Re)calculate length and checksum */
287         header->length = current - (u32)madt;
288
289         header->checksum = table_compute_checksum((void *)madt, header->length);
290 }
291
292 int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base,
293                               u16 seg_nr, u8 start, u8 end)
294 {
295         memset(mmconfig, 0, sizeof(*mmconfig));
296         mmconfig->base_address_l = base;
297         mmconfig->base_address_h = 0;
298         mmconfig->pci_segment_group_number = seg_nr;
299         mmconfig->start_bus_number = start;
300         mmconfig->end_bus_number = end;
301
302         return sizeof(struct acpi_mcfg_mmconfig);
303 }
304
305 __weak u32 acpi_fill_mcfg(u32 current)
306 {
307         current += acpi_create_mcfg_mmconfig
308                 ((struct acpi_mcfg_mmconfig *)current,
309                 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
310
311         return current;
312 }
313
314 /* MCFG is defined in the PCI Firmware Specification 3.0 */
315 static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
316 {
317         struct acpi_table_header *header = &(mcfg->header);
318         u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
319
320         memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
321
322         /* Fill out header fields */
323         acpi_fill_header(header, "MCFG");
324         header->length = sizeof(struct acpi_mcfg);
325         header->revision = 1;
326
327         current = acpi_fill_mcfg(current);
328
329         /* (Re)calculate length and checksum */
330         header->length = current - (u32)mcfg;
331         header->checksum = table_compute_checksum((void *)mcfg, header->length);
332 }
333
334 __weak u32 acpi_fill_csrt(u32 current)
335 {
336         return current;
337 }
338
339 static void acpi_create_csrt(struct acpi_csrt *csrt)
340 {
341         struct acpi_table_header *header = &(csrt->header);
342         u32 current = (u32)csrt + sizeof(struct acpi_csrt);
343
344         memset((void *)csrt, 0, sizeof(struct acpi_csrt));
345
346         /* Fill out header fields */
347         acpi_fill_header(header, "CSRT");
348         header->length = sizeof(struct acpi_csrt);
349         header->revision = 0;
350
351         current = acpi_fill_csrt(current);
352
353         /* (Re)calculate length and checksum */
354         header->length = current - (u32)csrt;
355         header->checksum = table_compute_checksum((void *)csrt, header->length);
356 }
357
358 static void acpi_create_spcr(struct acpi_spcr *spcr)
359 {
360         struct acpi_table_header *header = &(spcr->header);
361         struct serial_device_info serial_info = {0};
362         ulong serial_address, serial_offset;
363         struct udevice *dev;
364         uint serial_config;
365         uint serial_width;
366         int access_size;
367         int space_id;
368         int ret = -ENODEV;
369
370         /* Fill out header fields */
371         acpi_fill_header(header, "SPCR");
372         header->length = sizeof(struct acpi_spcr);
373         header->revision = 2;
374
375         /* Read the device once, here. It is reused below */
376         dev = gd->cur_serial_dev;
377         if (dev)
378                 ret = serial_getinfo(dev, &serial_info);
379         if (ret)
380                 serial_info.type = SERIAL_CHIP_UNKNOWN;
381
382         /* Encode chip type */
383         switch (serial_info.type) {
384         case SERIAL_CHIP_16550_COMPATIBLE:
385                 spcr->interface_type = ACPI_DBG2_16550_COMPATIBLE;
386                 break;
387         case SERIAL_CHIP_UNKNOWN:
388         default:
389                 spcr->interface_type = ACPI_DBG2_UNKNOWN;
390                 break;
391         }
392
393         /* Encode address space */
394         switch (serial_info.addr_space) {
395         case SERIAL_ADDRESS_SPACE_MEMORY:
396                 space_id = ACPI_ADDRESS_SPACE_MEMORY;
397                 break;
398         case SERIAL_ADDRESS_SPACE_IO:
399         default:
400                 space_id = ACPI_ADDRESS_SPACE_IO;
401                 break;
402         }
403
404         serial_width = serial_info.reg_width * 8;
405         serial_offset = serial_info.reg_offset << serial_info.reg_shift;
406         serial_address = serial_info.addr + serial_offset;
407
408         /* Encode register access size */
409         switch (serial_info.reg_shift) {
410         case 0:
411                 access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS;
412                 break;
413         case 1:
414                 access_size = ACPI_ACCESS_SIZE_WORD_ACCESS;
415                 break;
416         case 2:
417                 access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
418                 break;
419         case 3:
420                 access_size = ACPI_ACCESS_SIZE_QWORD_ACCESS;
421                 break;
422         default:
423                 access_size = ACPI_ACCESS_SIZE_UNDEFINED;
424                 break;
425         }
426
427         debug("UART type %u @ %lx\n", spcr->interface_type, serial_address);
428
429         /* Fill GAS */
430         spcr->serial_port.space_id = space_id;
431         spcr->serial_port.bit_width = serial_width;
432         spcr->serial_port.bit_offset = 0;
433         spcr->serial_port.access_size = access_size;
434         spcr->serial_port.addrl = lower_32_bits(serial_address);
435         spcr->serial_port.addrh = upper_32_bits(serial_address);
436
437         /* Encode baud rate */
438         switch (serial_info.baudrate) {
439         case 9600:
440                 spcr->baud_rate = 3;
441                 break;
442         case 19200:
443                 spcr->baud_rate = 4;
444                 break;
445         case 57600:
446                 spcr->baud_rate = 6;
447                 break;
448         case 115200:
449                 spcr->baud_rate = 7;
450                 break;
451         default:
452                 spcr->baud_rate = 0;
453                 break;
454         }
455
456         serial_config = SERIAL_DEFAULT_CONFIG;
457         if (dev)
458                 ret = serial_getconfig(dev, &serial_config);
459
460         spcr->parity = SERIAL_GET_PARITY(serial_config);
461         spcr->stop_bits = SERIAL_GET_STOP(serial_config);
462
463         /* No PCI devices for now */
464         spcr->pci_device_id = 0xffff;
465         spcr->pci_vendor_id = 0xffff;
466
467         /*
468          * SPCR has no clue if the UART base clock speed is different
469          * to the default one. However, the SPCR 1.04 defines baud rate
470          * 0 as a preconfigured state of UART and OS is supposed not
471          * to touch the configuration of the serial device.
472          */
473         if (serial_info.clock != SERIAL_DEFAULT_CLOCK)
474                 spcr->baud_rate = 0;
475
476         /* Fix checksum */
477         header->checksum = table_compute_checksum((void *)spcr, header->length);
478 }
479
480 /*
481  * QEMU's version of write_acpi_tables is defined in drivers/misc/qfw.c
482  */
483 ulong write_acpi_tables(ulong start_addr)
484 {
485         struct acpi_ctx sctx, *ctx = &sctx;
486         struct acpi_rsdp *rsdp;
487         struct acpi_rsdt *rsdt;
488         struct acpi_xsdt *xsdt;
489         struct acpi_facs *facs;
490         struct acpi_table_header *dsdt;
491         struct acpi_fadt *fadt;
492         struct acpi_mcfg *mcfg;
493         struct acpi_madt *madt;
494         struct acpi_csrt *csrt;
495         struct acpi_spcr *spcr;
496         void *start;
497         ulong addr;
498         int i;
499
500         start = map_sysmem(start_addr, 0);
501         ctx->current = start;
502
503         /* Align ACPI tables to 16 byte */
504         acpi_align(ctx);
505
506         debug("ACPI: Writing ACPI tables at %lx\n", start_addr);
507
508         /* We need at least an RSDP and an RSDT Table */
509         rsdp = ctx->current;
510         acpi_inc_align(ctx, sizeof(struct acpi_rsdp));
511         rsdt = ctx->current;
512         acpi_inc_align(ctx, sizeof(struct acpi_rsdt));
513         xsdt = ctx->current;
514         acpi_inc_align(ctx, sizeof(struct acpi_xsdt));
515         /*
516          * Per ACPI spec, the FACS table address must be aligned to a 64 byte
517          * boundary (Windows checks this, but Linux does not).
518          */
519         acpi_align64(ctx);
520
521         /* clear all table memory */
522         memset((void *)start, 0, ctx->current - start);
523
524         acpi_write_rsdp(rsdp, rsdt, xsdt);
525         acpi_write_rsdt(rsdt);
526         acpi_write_xsdt(xsdt);
527
528         debug("ACPI:    * FACS\n");
529         facs = ctx->current;
530         acpi_inc_align(ctx, sizeof(struct acpi_facs));
531
532         acpi_create_facs(facs);
533
534         debug("ACPI:    * DSDT\n");
535         dsdt = ctx->current;
536         memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
537         acpi_inc(ctx, sizeof(struct acpi_table_header));
538         memcpy(ctx->current,
539                (char *)&AmlCode + sizeof(struct acpi_table_header),
540                dsdt->length - sizeof(struct acpi_table_header));
541         acpi_inc_align(ctx, dsdt->length - sizeof(struct acpi_table_header));
542
543         /* Pack GNVS into the ACPI table area */
544         for (i = 0; i < dsdt->length; i++) {
545                 u32 *gnvs = (u32 *)((u32)dsdt + i);
546                 if (*gnvs == ACPI_GNVS_ADDR) {
547                         ulong addr = (ulong)map_to_sysmem(ctx->current);
548
549                         debug("Fix up global NVS in DSDT to %#08lx\n", addr);
550                         *gnvs = addr;
551                         break;
552                 }
553         }
554
555         /* Update DSDT checksum since we patched the GNVS address */
556         dsdt->checksum = 0;
557         dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
558
559         /* Fill in platform-specific global NVS variables */
560         acpi_create_gnvs(ctx->current);
561         acpi_inc_align(ctx, sizeof(struct acpi_global_nvs));
562
563         debug("ACPI:    * FADT\n");
564         fadt = ctx->current;
565         acpi_inc_align(ctx, sizeof(struct acpi_fadt));
566         acpi_create_fadt(fadt, facs, dsdt);
567         acpi_add_table(rsdp, fadt);
568
569         debug("ACPI:    * MADT\n");
570         madt = ctx->current;
571         acpi_create_madt(madt);
572         acpi_inc_align(ctx, madt->header.length);
573         acpi_add_table(rsdp, madt);
574
575         debug("ACPI:    * MCFG\n");
576         mcfg = ctx->current;
577         acpi_create_mcfg(mcfg);
578         acpi_inc_align(ctx, mcfg->header.length);
579         acpi_add_table(rsdp, mcfg);
580
581         debug("ACPI:    * CSRT\n");
582         csrt = ctx->current;
583         acpi_create_csrt(csrt);
584         acpi_inc_align(ctx, csrt->header.length);
585         acpi_add_table(rsdp, csrt);
586
587         debug("ACPI:    * SPCR\n");
588         spcr = ctx->current;
589         acpi_create_spcr(spcr);
590         acpi_inc_align(ctx, spcr->header.length);
591         acpi_add_table(rsdp, spcr);
592
593         addr = map_to_sysmem(ctx->current);
594         debug("current = %lx\n", addr);
595
596         acpi_rsdp_addr = (unsigned long)rsdp;
597         debug("ACPI: done\n");
598
599         return addr;
600 }
601
602 ulong acpi_get_rsdp_addr(void)
603 {
604         return acpi_rsdp_addr;
605 }