1
2
3
4
5
6
7
8
9#include <common.h>
10#include <cpu.h>
11#include <dm.h>
12#include <dm/uclass-internal.h>
13#include <version.h>
14#include <asm/acpi/global_nvs.h>
15#include <asm/acpi_table.h>
16#include <asm/ioapic.h>
17#include <asm/lapic.h>
18#include <asm/mpspec.h>
19#include <asm/tables.h>
20#include <asm/arch/global_nvs.h>
21
22
23
24
25
26extern const unsigned char AmlCode[];
27
28
29static ulong acpi_rsdp_addr;
30
31static void acpi_write_rsdp(struct acpi_rsdp *rsdp, struct acpi_rsdt *rsdt,
32 struct acpi_xsdt *xsdt)
33{
34 memset(rsdp, 0, sizeof(struct acpi_rsdp));
35
36 memcpy(rsdp->signature, RSDP_SIG, 8);
37 memcpy(rsdp->oem_id, OEM_ID, 6);
38
39 rsdp->length = sizeof(struct acpi_rsdp);
40 rsdp->rsdt_address = (u32)rsdt;
41
42
43
44
45
46
47
48
49 if (xsdt == NULL) {
50 rsdp->revision = ACPI_RSDP_REV_ACPI_1_0;
51 } else {
52 rsdp->xsdt_address = (u64)(u32)xsdt;
53 rsdp->revision = ACPI_RSDP_REV_ACPI_2_0;
54 }
55
56
57 rsdp->checksum = table_compute_checksum((void *)rsdp, 20);
58 rsdp->ext_checksum = table_compute_checksum((void *)rsdp,
59 sizeof(struct acpi_rsdp));
60}
61
62void acpi_fill_header(struct acpi_table_header *header, char *signature)
63{
64 memcpy(header->signature, signature, 4);
65 memcpy(header->oem_id, OEM_ID, 6);
66 memcpy(header->oem_table_id, OEM_TABLE_ID, 8);
67 header->oem_revision = U_BOOT_BUILD_DATE;
68 memcpy(header->aslc_id, ASLC_ID, 4);
69}
70
71static void acpi_write_rsdt(struct acpi_rsdt *rsdt)
72{
73 struct acpi_table_header *header = &(rsdt->header);
74
75
76 acpi_fill_header(header, "RSDT");
77 header->length = sizeof(struct acpi_rsdt);
78 header->revision = 1;
79
80
81
82
83 header->checksum = table_compute_checksum((void *)rsdt,
84 sizeof(struct acpi_rsdt));
85}
86
87static void acpi_write_xsdt(struct acpi_xsdt *xsdt)
88{
89 struct acpi_table_header *header = &(xsdt->header);
90
91
92 acpi_fill_header(header, "XSDT");
93 header->length = sizeof(struct acpi_xsdt);
94 header->revision = 1;
95
96
97
98
99 header->checksum = table_compute_checksum((void *)xsdt,
100 sizeof(struct acpi_xsdt));
101}
102
103
104
105
106
107static void acpi_add_table(struct acpi_rsdp *rsdp, void *table)
108{
109 int i, entries_num;
110 struct acpi_rsdt *rsdt;
111 struct acpi_xsdt *xsdt = NULL;
112
113
114 rsdt = (struct acpi_rsdt *)rsdp->rsdt_address;
115
116 if (rsdp->xsdt_address)
117 xsdt = (struct acpi_xsdt *)((u32)rsdp->xsdt_address);
118
119
120 entries_num = ARRAY_SIZE(rsdt->entry);
121
122 for (i = 0; i < entries_num; i++) {
123 if (rsdt->entry[i] == 0)
124 break;
125 }
126
127 if (i >= entries_num) {
128 debug("ACPI: Error: too many tables\n");
129 return;
130 }
131
132
133 rsdt->entry[i] = (u32)table;
134
135
136 rsdt->header.length = sizeof(struct acpi_table_header) +
137 (sizeof(u32) * (i + 1));
138
139
140 rsdt->header.checksum = 0;
141 rsdt->header.checksum = table_compute_checksum((u8 *)rsdt,
142 rsdt->header.length);
143
144
145
146
147
148 if (xsdt) {
149
150 xsdt->entry[i] = (u64)(u32)table;
151
152
153 xsdt->header.length = sizeof(struct acpi_table_header) +
154 (sizeof(u64) * (i + 1));
155
156
157 xsdt->header.checksum = 0;
158 xsdt->header.checksum = table_compute_checksum((u8 *)xsdt,
159 xsdt->header.length);
160 }
161}
162
163static void acpi_create_facs(struct acpi_facs *facs)
164{
165 memset((void *)facs, 0, sizeof(struct acpi_facs));
166
167 memcpy(facs->signature, "FACS", 4);
168 facs->length = sizeof(struct acpi_facs);
169 facs->hardware_signature = 0;
170 facs->firmware_waking_vector = 0;
171 facs->global_lock = 0;
172 facs->flags = 0;
173 facs->x_firmware_waking_vector_l = 0;
174 facs->x_firmware_waking_vector_h = 0;
175 facs->version = 1;
176}
177
178static int acpi_create_madt_lapic(struct acpi_madt_lapic *lapic,
179 u8 cpu, u8 apic)
180{
181 lapic->type = ACPI_APIC_LAPIC;
182 lapic->length = sizeof(struct acpi_madt_lapic);
183 lapic->flags = LOCAL_APIC_FLAG_ENABLED;
184 lapic->processor_id = cpu;
185 lapic->apic_id = apic;
186
187 return lapic->length;
188}
189
190int acpi_create_madt_lapics(u32 current)
191{
192 struct udevice *dev;
193 int total_length = 0;
194
195 for (uclass_find_first_device(UCLASS_CPU, &dev);
196 dev;
197 uclass_find_next_device(&dev)) {
198 struct cpu_platdata *plat = dev_get_parent_platdata(dev);
199 int length = acpi_create_madt_lapic(
200 (struct acpi_madt_lapic *)current,
201 plat->cpu_id, plat->cpu_id);
202 current += length;
203 total_length += length;
204 }
205
206 return total_length;
207}
208
209int acpi_create_madt_ioapic(struct acpi_madt_ioapic *ioapic, u8 id,
210 u32 addr, u32 gsi_base)
211{
212 ioapic->type = ACPI_APIC_IOAPIC;
213 ioapic->length = sizeof(struct acpi_madt_ioapic);
214 ioapic->reserved = 0x00;
215 ioapic->gsi_base = gsi_base;
216 ioapic->ioapic_id = id;
217 ioapic->ioapic_addr = addr;
218
219 return ioapic->length;
220}
221
222int acpi_create_madt_irqoverride(struct acpi_madt_irqoverride *irqoverride,
223 u8 bus, u8 source, u32 gsirq, u16 flags)
224{
225 irqoverride->type = ACPI_APIC_IRQ_SRC_OVERRIDE;
226 irqoverride->length = sizeof(struct acpi_madt_irqoverride);
227 irqoverride->bus = bus;
228 irqoverride->source = source;
229 irqoverride->gsirq = gsirq;
230 irqoverride->flags = flags;
231
232 return irqoverride->length;
233}
234
235int acpi_create_madt_lapic_nmi(struct acpi_madt_lapic_nmi *lapic_nmi,
236 u8 cpu, u16 flags, u8 lint)
237{
238 lapic_nmi->type = ACPI_APIC_LAPIC_NMI;
239 lapic_nmi->length = sizeof(struct acpi_madt_lapic_nmi);
240 lapic_nmi->flags = flags;
241 lapic_nmi->processor_id = cpu;
242 lapic_nmi->lint = lint;
243
244 return lapic_nmi->length;
245}
246
247static int acpi_create_madt_irq_overrides(u32 current)
248{
249 struct acpi_madt_irqoverride *irqovr;
250 u16 sci_flags = MP_IRQ_TRIGGER_LEVEL | MP_IRQ_POLARITY_HIGH;
251 int length = 0;
252
253 irqovr = (void *)current;
254 length += acpi_create_madt_irqoverride(irqovr, 0, 0, 2, 0);
255
256 irqovr = (void *)(current + length);
257 length += acpi_create_madt_irqoverride(irqovr, 0, 9, 9, sci_flags);
258
259 return length;
260}
261
262__weak u32 acpi_fill_madt(u32 current)
263{
264 current += acpi_create_madt_lapics(current);
265
266 current += acpi_create_madt_ioapic((struct acpi_madt_ioapic *)current,
267 io_apic_read(IO_APIC_ID) >> 24, IO_APIC_ADDR, 0);
268
269 current += acpi_create_madt_irq_overrides(current);
270
271 return current;
272}
273
274static void acpi_create_madt(struct acpi_madt *madt)
275{
276 struct acpi_table_header *header = &(madt->header);
277 u32 current = (u32)madt + sizeof(struct acpi_madt);
278
279 memset((void *)madt, 0, sizeof(struct acpi_madt));
280
281
282 acpi_fill_header(header, "APIC");
283 header->length = sizeof(struct acpi_madt);
284 header->revision = 4;
285
286 madt->lapic_addr = LAPIC_DEFAULT_BASE;
287 madt->flags = ACPI_MADT_PCAT_COMPAT;
288
289 current = acpi_fill_madt(current);
290
291
292 header->length = current - (u32)madt;
293
294 header->checksum = table_compute_checksum((void *)madt, header->length);
295}
296
297int acpi_create_mcfg_mmconfig(struct acpi_mcfg_mmconfig *mmconfig, u32 base,
298 u16 seg_nr, u8 start, u8 end)
299{
300 memset(mmconfig, 0, sizeof(*mmconfig));
301 mmconfig->base_address_l = base;
302 mmconfig->base_address_h = 0;
303 mmconfig->pci_segment_group_number = seg_nr;
304 mmconfig->start_bus_number = start;
305 mmconfig->end_bus_number = end;
306
307 return sizeof(struct acpi_mcfg_mmconfig);
308}
309
310__weak u32 acpi_fill_mcfg(u32 current)
311{
312 current += acpi_create_mcfg_mmconfig
313 ((struct acpi_mcfg_mmconfig *)current,
314 CONFIG_PCIE_ECAM_BASE, 0x0, 0x0, 255);
315
316 return current;
317}
318
319
320static void acpi_create_mcfg(struct acpi_mcfg *mcfg)
321{
322 struct acpi_table_header *header = &(mcfg->header);
323 u32 current = (u32)mcfg + sizeof(struct acpi_mcfg);
324
325 memset((void *)mcfg, 0, sizeof(struct acpi_mcfg));
326
327
328 acpi_fill_header(header, "MCFG");
329 header->length = sizeof(struct acpi_mcfg);
330 header->revision = 1;
331
332 current = acpi_fill_mcfg(current);
333
334
335 header->length = current - (u32)mcfg;
336 header->checksum = table_compute_checksum((void *)mcfg, header->length);
337}
338
339
340
341
342ulong write_acpi_tables(ulong start)
343{
344 u32 current;
345 struct acpi_rsdp *rsdp;
346 struct acpi_rsdt *rsdt;
347 struct acpi_xsdt *xsdt;
348 struct acpi_facs *facs;
349 struct acpi_table_header *dsdt;
350 struct acpi_fadt *fadt;
351 struct acpi_mcfg *mcfg;
352 struct acpi_madt *madt;
353 int i;
354
355 current = start;
356
357
358 current = ALIGN(current, 16);
359
360 debug("ACPI: Writing ACPI tables at %lx\n", start);
361
362
363 rsdp = (struct acpi_rsdp *)current;
364 current += sizeof(struct acpi_rsdp);
365 current = ALIGN(current, 16);
366 rsdt = (struct acpi_rsdt *)current;
367 current += sizeof(struct acpi_rsdt);
368 current = ALIGN(current, 16);
369 xsdt = (struct acpi_xsdt *)current;
370 current += sizeof(struct acpi_xsdt);
371
372
373
374
375 current = ALIGN(current, 64);
376
377
378 memset((void *)start, 0, current - start);
379
380 acpi_write_rsdp(rsdp, rsdt, xsdt);
381 acpi_write_rsdt(rsdt);
382 acpi_write_xsdt(xsdt);
383
384 debug("ACPI: * FACS\n");
385 facs = (struct acpi_facs *)current;
386 current += sizeof(struct acpi_facs);
387 current = ALIGN(current, 16);
388
389 acpi_create_facs(facs);
390
391 debug("ACPI: * DSDT\n");
392 dsdt = (struct acpi_table_header *)current;
393 memcpy(dsdt, &AmlCode, sizeof(struct acpi_table_header));
394 current += sizeof(struct acpi_table_header);
395 memcpy((char *)current,
396 (char *)&AmlCode + sizeof(struct acpi_table_header),
397 dsdt->length - sizeof(struct acpi_table_header));
398 current += dsdt->length - sizeof(struct acpi_table_header);
399 current = ALIGN(current, 16);
400
401
402 for (i = 0; i < dsdt->length; i++) {
403 u32 *gnvs = (u32 *)((u32)dsdt + i);
404 if (*gnvs == ACPI_GNVS_ADDR) {
405 debug("Fix up global NVS in DSDT to 0x%08x\n", current);
406 *gnvs = current;
407 break;
408 }
409 }
410
411
412 dsdt->checksum = 0;
413 dsdt->checksum = table_compute_checksum((void *)dsdt, dsdt->length);
414
415
416 acpi_create_gnvs((struct acpi_global_nvs *)current);
417 current += sizeof(struct acpi_global_nvs);
418 current = ALIGN(current, 16);
419
420 debug("ACPI: * FADT\n");
421 fadt = (struct acpi_fadt *)current;
422 current += sizeof(struct acpi_fadt);
423 current = ALIGN(current, 16);
424 acpi_create_fadt(fadt, facs, dsdt);
425 acpi_add_table(rsdp, fadt);
426
427 debug("ACPI: * MADT\n");
428 madt = (struct acpi_madt *)current;
429 acpi_create_madt(madt);
430 current += madt->header.length;
431 acpi_add_table(rsdp, madt);
432 current = ALIGN(current, 16);
433
434 debug("ACPI: * MCFG\n");
435 mcfg = (struct acpi_mcfg *)current;
436 acpi_create_mcfg(mcfg);
437 current += mcfg->header.length;
438 acpi_add_table(rsdp, mcfg);
439 current = ALIGN(current, 16);
440
441 debug("current = %x\n", current);
442
443 acpi_rsdp_addr = (unsigned long)rsdp;
444 debug("ACPI: done\n");
445
446 return current;
447}
448
449ulong acpi_get_rsdp_addr(void)
450{
451 return acpi_rsdp_addr;
452}
453