blob: ab1db7ec26eee8e08aabf101aa4add678415e5da [file] [log] [blame]
Saket Sinha331141a2015-08-22 12:20:55 +05301/*
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 */
26extern const unsigned char AmlCode[];
27
28/**
29* Add an ACPI table to the RSDT (and XSDT) structure, recalculate length
30* and checksum.
31*/
32static 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
87static 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
99unsigned 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
115int 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
128int 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
141int 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
153static 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
161static 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
186static 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
199static 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 */
209static 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
230static 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
245static 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
263static 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
281static 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
312static 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
334unsigned long write_acpi_tables(unsigned long start)
335{
336 unsigned long current;
337 struct acpi_rsdp *rsdp;
338 struct acpi_rsdt *rsdt;
339 struct acpi_xsdt *xsdt;
340 struct acpi_facs *facs;
341 acpi_header_t *dsdt;
342 struct acpi_fadt *fadt;
343 struct acpi_mcfg *mcfg;
344 struct acpi_madt *madt;
345 acpi_header_t *ssdt;
346
347 current = start;
348
349 /* Align ACPI tables to 16byte */
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 current = ALIGN(current, 16);
364
365 /* clear all table memory */
366 memset((void *)start, 0, current - start);
367
368 acpi_write_rsdp(rsdp, rsdt, xsdt);
369 acpi_write_rsdt(rsdt);
370 acpi_write_xsdt(xsdt);
371
372 debug("ACPI: * FACS\n");
373 facs = (struct acpi_facs *)current;
374 current += sizeof(struct acpi_facs);
375 current = ALIGN(current, 16);
376
377 acpi_create_facs(facs);
378
379 debug("ACPI: * DSDT\n");
380 dsdt = (acpi_header_t *)current;
381 memcpy(dsdt, &AmlCode, sizeof(acpi_header_t));
382 if (dsdt->length >= sizeof(acpi_header_t)) {
383 current += sizeof(acpi_header_t);
384 memcpy((char *)current,
385 (char *)&AmlCode + sizeof(acpi_header_t),
386 dsdt->length - sizeof(acpi_header_t));
387 current += dsdt->length - sizeof(acpi_header_t);
388
389 /* (Re)calculate length and checksum */
390 dsdt->length = current - (unsigned long)dsdt;
391 dsdt->checksum = 0;
392 dsdt->checksum = table_compute_checksum((void *)dsdt,
393 dsdt->length);
394 }
395 current = ALIGN(current, 16);
396
397 debug("ACPI: * FADT\n");
398 fadt = (struct acpi_fadt *)current;
399 current += sizeof(struct acpi_fadt);
400 current = ALIGN(current, 16);
401 acpi_create_fadt(fadt, facs, dsdt);
402 acpi_add_table(rsdp, fadt);
403
404 debug("ACPI: * MCFG\n");
405 mcfg = (struct acpi_mcfg *)current;
406 acpi_create_mcfg(mcfg);
407 if (mcfg->header.length > sizeof(struct acpi_mcfg)) {
408 current += mcfg->header.length;
409 current = ALIGN(current, 16);
410 acpi_add_table(rsdp, mcfg);
411 }
412
413 debug("ACPI: * MADT\n");
414 madt = (struct acpi_madt *)current;
415 acpi_create_madt(madt);
416 if (madt->header.length > sizeof(struct acpi_madt)) {
417 current += madt->header.length;
418 acpi_add_table(rsdp, madt);
419 }
420 current = ALIGN(current, 16);
421
422 debug("ACPI: * SSDT\n");
423 ssdt = (acpi_header_t *)current;
424 acpi_create_ssdt_generator(ssdt, ACPI_TABLE_CREATOR);
425 if (ssdt->length > sizeof(acpi_header_t)) {
426 current += ssdt->length;
427 acpi_add_table(rsdp, ssdt);
428 current = ALIGN(current, 16);
429 }
430
431 debug("current = %lx\n", current);
432
433 debug("ACPI: done.\n");
434
435 return current;
436}