efifdt.c revision 1.33 1 /* $NetBSD: efifdt.c,v 1.33 2021/11/06 19:44:22 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2019 Jason R. Thorpe
5 * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include "efiboot.h"
31 #include "efifdt.h"
32 #include "efiblock.h"
33 #include "overlay.h"
34 #include "module.h"
35
36 #ifdef EFIBOOT_ACPI
37 #include "efiacpi.h"
38 #endif
39
40 #include <libfdt.h>
41
42 #define FDT_TABLE_GUID \
43 { 0xb1b621d5, 0xf19c, 0x41a5, { 0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69, 0xaa, 0xe0 } }
44 static EFI_GUID FdtTableGuid = FDT_TABLE_GUID;
45
46 #define FDT_MEMORY_NODE_PATH "/memory"
47 #define FDT_MEMORY_NODE_NAME "memory"
48 #define FDT_CHOSEN_NODE_PATH "/chosen"
49 #define FDT_CHOSEN_NODE_NAME "chosen"
50
51 #define FDT_MEMORY_USABLE(_md) \
52 ((_md)->Type == EfiLoaderCode || (_md)->Type == EfiLoaderData || \
53 (_md)->Type == EfiBootServicesCode || (_md)->Type == EfiBootServicesData || \
54 (_md)->Type == EfiConventionalMemory)
55
56 #define FDT_SPACE (4 * 1024 * 1024)
57 #define FDT_ALIGN (2 * 1024 * 1024)
58
59 #ifdef _LP64
60 #define PRIdUINTN "ld"
61 #define PRIxUINTN "lx"
62 #else
63 #define PRIdUINTN "d"
64 #define PRIxUINTN "x"
65 #endif
66 static void *fdt_data = NULL;
67 static size_t fdt_data_size = 512*1024;
68
69 static EFI_PHYSICAL_ADDRESS initrd_addr, dtb_addr, rndseed_addr;
70 static u_long initrd_size = 0, dtb_size = 0, rndseed_size = 0;
71
72 /* exec.c */
73 extern EFI_PHYSICAL_ADDRESS efirng_addr;
74 extern u_long efirng_size;
75
76 #ifdef EFIBOOT_ACPI
77 #define ACPI_FDT_SIZE (128 * 1024)
78 static int efi_fdt_create_acpifdt(void);
79 #endif
80
81 int
82 efi_fdt_probe(void)
83 {
84 EFI_STATUS status;
85
86 status = LibGetSystemConfigurationTable(&FdtTableGuid, &fdt_data);
87 if (EFI_ERROR(status))
88 return EIO;
89
90 if (fdt_check_header(fdt_data) != 0) {
91 fdt_data = NULL;
92 return EINVAL;
93 }
94
95 return 0;
96 }
97
98 int
99 efi_fdt_set_data(void *data)
100 {
101 int err;
102
103 if (fdt_check_header(data) != 0)
104 return EINVAL;
105
106 fdt_data = alloc(fdt_data_size);
107 if (fdt_data == NULL)
108 return ENOMEM;
109 memset(fdt_data, 0, fdt_data_size);
110
111 err = fdt_open_into(data, fdt_data, fdt_data_size);
112 if (err != 0) {
113 dealloc(fdt_data, fdt_data_size);
114 fdt_data = NULL;
115 return ENXIO;
116 }
117
118 return 0;
119 }
120
121 void *
122 efi_fdt_data(void)
123 {
124 return fdt_data;
125 }
126
127 int
128 efi_fdt_size(void)
129 {
130 return fdt_data == NULL ? 0 : fdt_totalsize(fdt_data);
131 }
132
133 bool
134 efi_fdt_overlay_is_compatible(void *dtbo)
135 {
136 const int system_root = fdt_path_offset(fdt_data, "/");
137 const int overlay_root = fdt_path_offset(dtbo, "/");
138
139 if (system_root < 0 || overlay_root < 0)
140 return false;
141
142 const int system_ncompat = fdt_stringlist_count(fdt_data, system_root,
143 "compatible");
144 const int overlay_ncompat = fdt_stringlist_count(dtbo, overlay_root,
145 "compatible");
146
147 if (system_ncompat <= 0 || overlay_ncompat <= 0)
148 return false;
149
150 const char *system_compatible, *overlay_compatible;
151 int si, oi;
152
153 for (si = 0; si < system_ncompat; si++) {
154 system_compatible = fdt_stringlist_get(fdt_data,
155 system_root, "compatible", si, NULL);
156 if (system_compatible == NULL)
157 continue;
158 for (oi = 0; oi < overlay_ncompat; oi++) {
159 overlay_compatible = fdt_stringlist_get(dtbo,
160 overlay_root, "compatible", oi, NULL);
161 if (overlay_compatible == NULL)
162 continue;
163 if (strcmp(system_compatible, overlay_compatible) == 0)
164 return true;
165 }
166 }
167
168 return false;
169 }
170
171 int
172 efi_fdt_overlay_apply(void *dtbo, int *fdterr)
173 {
174 int err = fdt_overlay_apply(fdt_data, dtbo);
175 if (fdterr)
176 *fdterr = err;
177 return err == 0 ? 0 : EIO;
178 }
179
180 void
181 efi_fdt_init(u_long addr, u_long len)
182 {
183 int error;
184
185 error = fdt_open_into(fdt_data, (void *)addr, len);
186 if (error < 0)
187 panic("fdt_open_into failed: %d", error);
188
189 fdt_data = (void *)addr;
190 }
191
192 void
193 efi_fdt_fini(void)
194 {
195 int error;
196
197 error = fdt_pack(fdt_data);
198 if (error < 0)
199 panic("fdt_pack failed: %d", error);
200 }
201
202 void
203 efi_fdt_show(void)
204 {
205 const char *model, *compat;
206 int n, ncompat;
207
208 if (fdt_data == NULL)
209 return;
210
211 model = fdt_getprop(fdt_data, fdt_path_offset(fdt_data, "/"), "model", NULL);
212 if (model)
213 printf("FDT: %s [", model);
214 ncompat = fdt_stringlist_count(fdt_data, fdt_path_offset(fdt_data, "/"), "compatible");
215 for (n = 0; n < ncompat; n++) {
216 compat = fdt_stringlist_get(fdt_data, fdt_path_offset(fdt_data, "/"),
217 "compatible", n, NULL);
218 printf("%s%s", n == 0 ? "" : ", ", compat);
219 }
220 printf("]\n");
221 }
222
223 static int
224 efi_fdt_chosen(void)
225 {
226 int chosen;
227
228 chosen = fdt_path_offset(fdt_data, FDT_CHOSEN_NODE_PATH);
229 if (chosen < 0)
230 chosen = fdt_add_subnode(fdt_data,
231 fdt_path_offset(fdt_data, "/"),
232 FDT_CHOSEN_NODE_NAME);
233 if (chosen < 0)
234 panic("FDT: Failed to create " FDT_CHOSEN_NODE_PATH " node");
235
236 return chosen;
237 }
238
239 void
240 efi_fdt_system_table(void)
241 {
242 #ifdef EFIBOOT_RUNTIME_ADDRESS
243 int chosen;
244
245 chosen = efi_fdt_chosen();
246
247 fdt_setprop_u64(fdt_data, chosen, "netbsd,uefi-system-table", (uint64_t)(uintptr_t)ST);
248 #endif
249 }
250
251 void
252 efi_fdt_memory_map(void)
253 {
254 UINTN nentries = 0, mapkey, descsize;
255 EFI_MEMORY_DESCRIPTOR *md, *memmap;
256 UINT32 descver;
257 UINT64 phys_start, phys_size;
258 int n, memory;
259
260 memory = fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH);
261 if (memory < 0)
262 memory = fdt_add_subnode(fdt_data, fdt_path_offset(fdt_data, "/"), FDT_MEMORY_NODE_NAME);
263 if (memory < 0)
264 panic("FDT: Failed to create " FDT_MEMORY_NODE_PATH " node");
265
266 fdt_delprop(fdt_data, memory, "reg");
267
268 const int address_cells = fdt_address_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
269 const int size_cells = fdt_size_cells(fdt_data, fdt_path_offset(fdt_data, "/"));
270
271 memmap = LibMemoryMap(&nentries, &mapkey, &descsize, &descver);
272 for (n = 0, md = memmap; n < nentries; n++, md = NextMemoryDescriptor(md, descsize)) {
273 /*
274 * create / find the chosen node for each iteration as it might have changed
275 * when adding to the memory node
276 */
277 int chosen = efi_fdt_chosen();
278 fdt_appendprop_u32(fdt_data, chosen, "netbsd,uefi-memmap", md->Type);
279 fdt_appendprop_u64(fdt_data, chosen, "netbsd,uefi-memmap", md->PhysicalStart);
280 fdt_appendprop_u64(fdt_data, chosen, "netbsd,uefi-memmap", md->NumberOfPages);
281 fdt_appendprop_u64(fdt_data, chosen, "netbsd,uefi-memmap", md->Attribute);
282
283 if ((md->Attribute & EFI_MEMORY_RUNTIME) != 0)
284 continue;
285
286 if ((md->Attribute & EFI_MEMORY_WB) == 0)
287 continue;
288 if (!FDT_MEMORY_USABLE(md))
289 continue;
290 if ((address_cells == 1 || size_cells == 1) && md->PhysicalStart + (md->NumberOfPages * EFI_PAGE_SIZE) > 0xffffffff)
291 continue;
292 if (md->NumberOfPages <= 1)
293 continue;
294
295 phys_start = md->PhysicalStart;
296 phys_size = md->NumberOfPages * EFI_PAGE_SIZE;
297
298 if (phys_start & EFI_PAGE_MASK) {
299 /*
300 * UEFI spec says these should be 4KB aligned, but
301 * U-Boot doesn't always, so round up to the next
302 * page.
303 */
304 phys_start = (phys_start + EFI_PAGE_SIZE) & ~EFI_PAGE_MASK;
305 phys_size -= (EFI_PAGE_SIZE * 2);
306 if (phys_size == 0)
307 continue;
308 }
309
310 memory = fdt_path_offset(fdt_data, FDT_MEMORY_NODE_PATH);
311 if (address_cells == 1)
312 fdt_appendprop_u32(fdt_data, memory, "reg",
313 (uint32_t)phys_start);
314 else
315 fdt_appendprop_u64(fdt_data, memory, "reg",
316 phys_start);
317
318 if (size_cells == 1)
319 fdt_appendprop_u32(fdt_data, memory, "reg",
320 (uint32_t)phys_size);
321 else
322 fdt_appendprop_u64(fdt_data, memory, "reg",
323 phys_size);
324 }
325 }
326
327 void
328 efi_fdt_gop(void)
329 {
330 EFI_STATUS status;
331 EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
332 EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE *mode;
333 EFI_HANDLE *gop_handle;
334 UINTN ngop_handle, n;
335 char buf[48];
336 int fb, chosen;
337
338 status = LibLocateHandle(ByProtocol, &GraphicsOutputProtocol, NULL, &ngop_handle, &gop_handle);
339 if (EFI_ERROR(status) || ngop_handle == 0)
340 return;
341
342 for (n = 0; n < ngop_handle; n++) {
343 status = uefi_call_wrapper(BS->HandleProtocol, 3, gop_handle[n], &GraphicsOutputProtocol, (void **)&gop);
344 if (EFI_ERROR(status))
345 continue;
346
347 mode = gop->Mode;
348 if (mode == NULL)
349 continue;
350
351 #ifdef EFIBOOT_DEBUG
352 printf("GOP: FB @ 0x%" PRIx64 " size 0x%" PRIxUINTN "\n", mode->FrameBufferBase, mode->FrameBufferSize);
353 printf("GOP: Version %d\n", mode->Info->Version);
354 printf("GOP: HRes %d VRes %d\n", mode->Info->HorizontalResolution, mode->Info->VerticalResolution);
355 printf("GOP: PixelFormat %d\n", mode->Info->PixelFormat);
356 printf("GOP: PixelBitmask R 0x%x G 0x%x B 0x%x Res 0x%x\n",
357 mode->Info->PixelInformation.RedMask,
358 mode->Info->PixelInformation.GreenMask,
359 mode->Info->PixelInformation.BlueMask,
360 mode->Info->PixelInformation.ReservedMask);
361 printf("GOP: Pixels per scanline %d\n", mode->Info->PixelsPerScanLine);
362 #endif
363
364 if (mode->Info->PixelFormat == PixelBltOnly) {
365 printf("GOP: PixelBltOnly pixel format not supported\n");
366 continue;
367 }
368
369 chosen = efi_fdt_chosen();
370 fdt_setprop_u32(fdt_data, chosen, "#address-cells", 2);
371 fdt_setprop_u32(fdt_data, chosen, "#size-cells", 2);
372 fdt_setprop_empty(fdt_data, chosen, "ranges");
373
374 snprintf(buf, sizeof(buf), "framebuffer@%" PRIx64, mode->FrameBufferBase);
375 fb = fdt_add_subnode(fdt_data, chosen, buf);
376 if (fb < 0) {
377 /* Framebuffer node already exists. No need to create a new one! */
378 return;
379 }
380
381 fdt_appendprop_string(fdt_data, fb, "compatible", "simple-framebuffer");
382 fdt_appendprop_string(fdt_data, fb, "status", "okay");
383 fdt_appendprop_u64(fdt_data, fb, "reg", mode->FrameBufferBase);
384 fdt_appendprop_u64(fdt_data, fb, "reg", mode->FrameBufferSize);
385 fdt_appendprop_u32(fdt_data, fb, "width", mode->Info->HorizontalResolution);
386 fdt_appendprop_u32(fdt_data, fb, "height", mode->Info->VerticalResolution);
387 fdt_appendprop_u32(fdt_data, fb, "stride", mode->Info->PixelsPerScanLine * 4); /* XXX */
388 fdt_appendprop_string(fdt_data, fb, "format", "a8b8g8r8");
389
390 #ifdef EFIBOOT_ACPI
391 /*
392 * In ACPI mode, use GOP as console.
393 */
394 if (efi_acpi_available()) {
395 snprintf(buf, sizeof(buf), "/chosen/framebuffer@%" PRIx64, mode->FrameBufferBase);
396 fdt_setprop_string(fdt_data, chosen, "stdout-path", buf);
397 }
398 #endif
399
400 return;
401 }
402 }
403
404 void
405 efi_fdt_bootargs(const char *bootargs)
406 {
407 struct efi_block_part *bpart = efi_block_boot_part();
408 uint8_t macaddr[6];
409 int chosen;
410
411 chosen = efi_fdt_chosen();
412
413 if (*bootargs)
414 fdt_setprop_string(fdt_data, chosen, "bootargs", bootargs);
415
416 if (bpart) {
417 switch (bpart->type) {
418 case EFI_BLOCK_PART_DISKLABEL:
419 fdt_setprop(fdt_data, chosen, "netbsd,mbr",
420 bpart->hash, sizeof(bpart->hash));
421 fdt_setprop_u32(fdt_data, chosen, "netbsd,partition",
422 bpart->index);
423 break;
424 case EFI_BLOCK_PART_GPT:
425 if (bpart->gpt.ent.ent_name[0] == 0x0000) {
426 fdt_setprop(fdt_data, chosen, "netbsd,gpt-guid",
427 bpart->hash, sizeof(bpart->hash));
428 } else {
429 char *label = NULL;
430 int rv = ucs2_to_utf8(bpart->gpt.ent.ent_name, &label);
431 if (rv == 0) {
432 fdt_setprop_string(fdt_data, chosen, "netbsd,gpt-label", label);
433 FreePool(label);
434 }
435 }
436 break;
437 default:
438 break;
439 }
440 } else if (efi_net_get_booted_macaddr(macaddr) == 0) {
441 fdt_setprop(fdt_data, chosen, "netbsd,booted-mac-address", macaddr, sizeof(macaddr));
442 }
443 }
444
445 void
446 efi_fdt_initrd(u_long initrd_addr, u_long initrd_size)
447 {
448 int chosen;
449
450 if (initrd_size == 0)
451 return;
452
453 chosen = efi_fdt_chosen();
454 fdt_setprop_u64(fdt_data, chosen, "linux,initrd-start", initrd_addr);
455 fdt_setprop_u64(fdt_data, chosen, "linux,initrd-end", initrd_addr + initrd_size);
456 }
457
458 /* pass in the NetBSD on-disk random seed */
459 void
460 efi_fdt_rndseed(u_long addr, u_long size)
461 {
462 int chosen;
463
464 if (size == 0)
465 return;
466
467 chosen = efi_fdt_chosen();
468 fdt_setprop_u64(fdt_data, chosen, "netbsd,rndseed-start", addr);
469 fdt_setprop_u64(fdt_data, chosen, "netbsd,rndseed-end", addr + size);
470 }
471
472 /* pass in output from the EFI firmware's RNG from some unknown source */
473 void
474 efi_fdt_efirng(u_long efirng_addr, u_long efirng_size)
475 {
476 int chosen;
477
478 if (efirng_size == 0)
479 return;
480
481 chosen = efi_fdt_chosen();
482 fdt_setprop_u64(fdt_data, chosen, "netbsd,efirng-start",
483 efirng_addr);
484 fdt_setprop_u64(fdt_data, chosen, "netbsd,efirng-end",
485 efirng_addr + efirng_size);
486 }
487
488 /* pass in module information */
489 void
490 efi_fdt_module(const char *module_name, u_long module_addr, u_long module_size)
491 {
492 int chosen;
493
494 if (module_size == 0)
495 return;
496
497 chosen = efi_fdt_chosen();
498 fdt_appendprop_string(fdt_data, chosen, "netbsd,module-names", module_name);
499 fdt_appendprop_u64(fdt_data, chosen, "netbsd,modules", module_addr);
500 fdt_appendprop_u64(fdt_data, chosen, "netbsd,modules", module_size);
501 }
502
503 static void
504 apply_overlay(const char *path, void *dtbo)
505 {
506
507 if (!efi_fdt_overlay_is_compatible(dtbo)) {
508 printf("boot: %s: incompatible overlay\n", path);
509 return;
510 }
511
512 int fdterr;
513
514 if (efi_fdt_overlay_apply(dtbo, &fdterr) != 0) {
515 printf("boot: %s: error %d applying overlay\n", path, fdterr);
516 }
517 }
518
519 static void
520 apply_overlay_file(const char *path)
521 {
522 EFI_PHYSICAL_ADDRESS dtbo_addr;
523 u_long dtbo_size;
524
525 if (strlen(path) == 0)
526 return;
527
528 if (load_file(path, 0, false, &dtbo_addr, &dtbo_size) != 0 ||
529 dtbo_addr == 0) {
530 /* Error messages have already been displayed. */
531 goto out;
532 }
533
534 apply_overlay(path, (void *)(uintptr_t)dtbo_addr);
535
536 out:
537 if (dtbo_addr) {
538 uefi_call_wrapper(BS->FreePages, 2, dtbo_addr,
539 EFI_SIZE_TO_PAGES(dtbo_size));
540 }
541 }
542
543 static void
544 load_fdt_overlays(void)
545 {
546 if (!dtoverlay_enabled)
547 return;
548
549 dtoverlay_foreach(apply_overlay_file);
550 }
551
552 static void
553 load_module(const char *module_name)
554 {
555 EFI_PHYSICAL_ADDRESS addr;
556 u_long size;
557 char path[PATH_MAX];
558
559 snprintf(path, sizeof(path), "%s/%s/%s.kmod", module_prefix,
560 module_name, module_name);
561
562 if (load_file(path, 0, false, &addr, &size) != 0 || addr == 0 || size == 0)
563 return;
564
565 efi_fdt_module(module_name, (u_long)addr, size);
566 }
567
568 static void
569 load_modules(const char *kernel_name)
570 {
571 if (!module_enabled)
572 return;
573
574 module_init(kernel_name);
575 module_foreach(load_module);
576 }
577
578
579 /*
580 * Prepare kernel arguments and shutdown boot services.
581 */
582 int
583 arch_prepare_boot(const char *fname, const char *args, u_long *marks)
584 {
585 load_file(get_initrd_path(), 0, false, &initrd_addr, &initrd_size);
586 load_file(get_dtb_path(), 0, false, &dtb_addr, &dtb_size);
587
588 #ifdef EFIBOOT_ACPI
589 /* ACPI support only works for little endian kernels */
590 if (efi_acpi_available() && netbsd_elf_data == ELFDATA2LSB) {
591 int error = efi_fdt_create_acpifdt();
592 if (error != 0) {
593 return error;
594 }
595 } else
596 #endif
597 if (dtb_addr && efi_fdt_set_data((void *)(uintptr_t)dtb_addr) != 0) {
598 return EINVAL;
599 }
600
601 if (efi_fdt_size() > 0) {
602 /*
603 * Load the rndseed as late as possible -- after we
604 * have committed to using fdt and executing this
605 * kernel -- so that it doesn't hang around in memory
606 * if we have to bail or the kernel won't use it.
607 */
608 load_file(get_rndseed_path(), 0, false,
609 &rndseed_addr, &rndseed_size);
610
611 efi_fdt_init((marks[MARK_END] + FDT_ALIGN - 1) & -FDT_ALIGN, FDT_ALIGN);
612 load_modules(fname);
613 load_fdt_overlays();
614 efi_fdt_initrd(initrd_addr, initrd_size);
615 efi_fdt_rndseed(rndseed_addr, rndseed_size);
616 efi_fdt_efirng(efirng_addr, efirng_size);
617 efi_fdt_bootargs(args);
618 efi_fdt_system_table();
619 efi_fdt_gop();
620 efi_fdt_memory_map();
621 }
622
623 efi_cleanup();
624
625 if (efi_fdt_size() > 0) {
626 efi_fdt_fini();
627 }
628
629 return 0;
630 }
631
632 /*
633 * Free memory after a failed boot.
634 */
635 void
636 arch_cleanup_boot(void)
637 {
638 if (rndseed_addr) {
639 uefi_call_wrapper(BS->FreePages, 2, rndseed_addr, EFI_SIZE_TO_PAGES(rndseed_size));
640 rndseed_addr = 0;
641 rndseed_size = 0;
642 }
643 if (initrd_addr) {
644 uefi_call_wrapper(BS->FreePages, 2, initrd_addr, EFI_SIZE_TO_PAGES(initrd_size));
645 initrd_addr = 0;
646 initrd_size = 0;
647 }
648 if (dtb_addr) {
649 uefi_call_wrapper(BS->FreePages, 2, dtb_addr, EFI_SIZE_TO_PAGES(dtb_size));
650 dtb_addr = 0;
651 dtb_size = 0;
652 }
653 }
654
655 size_t
656 arch_alloc_size(void)
657 {
658 return FDT_SPACE;
659 }
660
661 #ifdef EFIBOOT_ACPI
662 int
663 efi_fdt_create_acpifdt(void)
664 {
665 void *acpi_root = efi_acpi_root();
666 void *smbios_table = efi_acpi_smbios();
667 void *fdt;
668 int error;
669
670 if (acpi_root == NULL)
671 return EINVAL;
672
673 fdt = AllocatePool(ACPI_FDT_SIZE);
674 if (fdt == NULL)
675 return ENOMEM;
676
677 error = fdt_create_empty_tree(fdt, ACPI_FDT_SIZE);
678 if (error)
679 return EIO;
680
681 const char *model = efi_acpi_get_model();
682
683 fdt_setprop_string(fdt, fdt_path_offset(fdt, "/"), "compatible", "netbsd,generic-acpi");
684 fdt_setprop_string(fdt, fdt_path_offset(fdt, "/"), "model", model);
685 fdt_setprop_cell(fdt, fdt_path_offset(fdt, "/"), "#address-cells", 2);
686 fdt_setprop_cell(fdt, fdt_path_offset(fdt, "/"), "#size-cells", 2);
687
688 fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"), "chosen");
689 fdt_setprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,acpi-root-table", (uint64_t)(uintptr_t)acpi_root);
690 if (smbios_table)
691 fdt_setprop_u64(fdt, fdt_path_offset(fdt, "/chosen"), "netbsd,smbios-table", (uint64_t)(uintptr_t)smbios_table);
692
693 fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"), "acpi");
694 fdt_setprop_string(fdt, fdt_path_offset(fdt, "/acpi"), "compatible", "netbsd,acpi");
695
696 return efi_fdt_set_data(fdt);
697 }
698 #endif
699
700 #ifdef EFIBOOT_RUNTIME_ADDRESS
701 static uint64_t
702 efi_fdt_runtime_alloc_va(uint64_t npages)
703 {
704 static uint64_t va = EFIBOOT_RUNTIME_ADDRESS;
705 static uint64_t sz = EFIBOOT_RUNTIME_SIZE;
706 uint64_t nva;
707
708 if (sz < (npages * EFI_PAGE_SIZE)) {
709 panic("efi_acpi_alloc_va: couldn't allocate %" PRIu64 " pages",
710 npages);
711 }
712
713 nva = va;
714 va += (npages * EFI_PAGE_SIZE);
715 sz -= (npages * EFI_PAGE_SIZE);
716
717 return nva;
718 }
719
720 void
721 arch_set_virtual_address_map(EFI_MEMORY_DESCRIPTOR *memmap, UINTN nentries,
722 UINTN mapkey, UINTN descsize, UINT32 descver)
723 {
724 EFI_MEMORY_DESCRIPTOR *md, *vmd, *vmemmap;
725 EFI_STATUS status;
726 int n, nrt;
727 void *fdt;
728
729 fdt = efi_fdt_data();
730
731 vmemmap = alloc(nentries * descsize);
732 if (vmemmap == NULL)
733 panic("FATAL: couldn't allocate virtual memory map");
734
735 for (n = 0, nrt = 0, vmd = vmemmap, md = memmap;
736 n < nentries;
737 n++, md = NextMemoryDescriptor(md, descsize)) {
738
739 if ((md->Attribute & EFI_MEMORY_RUNTIME) == 0) {
740 continue;
741 }
742
743 md->VirtualStart = efi_fdt_runtime_alloc_va(md->NumberOfPages);
744
745 switch (md->Type) {
746 case EfiRuntimeServicesCode:
747 fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
748 "netbsd,uefi-runtime-code", md->PhysicalStart);
749 fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
750 "netbsd,uefi-runtime-code", md->VirtualStart);
751 fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
752 "netbsd,uefi-runtime-code",
753 md->NumberOfPages * EFI_PAGE_SIZE);
754 break;
755 case EfiRuntimeServicesData:
756 fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
757 "netbsd,uefi-runtime-data", md->PhysicalStart);
758 fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
759 "netbsd,uefi-runtime-data", md->VirtualStart);
760 fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
761 "netbsd,uefi-runtime-data",
762 md->NumberOfPages * EFI_PAGE_SIZE);
763 break;
764 case EfiMemoryMappedIO:
765 fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
766 "netbsd,uefi-runtime-mmio", md->PhysicalStart);
767 fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
768 "netbsd,uefi-runtime-mmio", md->VirtualStart);
769 fdt_appendprop_u64(fdt, fdt_path_offset(fdt, "/chosen"),
770 "netbsd,uefi-runtime-mmio",
771 md->NumberOfPages * EFI_PAGE_SIZE);
772 break;
773 default:
774 break;
775 }
776
777 *vmd = *md;
778 vmd = NextMemoryDescriptor(vmd, descsize);
779 ++nrt;
780 }
781
782 status = uefi_call_wrapper(RT->SetVirtualAddressMap, 4, nrt * descsize,
783 descsize, descver, vmemmap);
784 if (EFI_ERROR(status)) {
785 printf("WARNING: SetVirtualAddressMap failed\n");
786 return;
787 }
788 }
789 #endif
790