xref: /netbsd-src/sys/arch/evbarm/fdt/fdt_machdep.c (revision b7341c0e432b08a315d32c52c618eef902d0379d)
1*b7341c0eSjmcneill /* $NetBSD: fdt_machdep.c,v 1.108 2024/06/30 17:55:28 jmcneill Exp $ */
23ec47f87Sjmcneill 
33ec47f87Sjmcneill /*-
43ec47f87Sjmcneill  * Copyright (c) 2015-2017 Jared McNeill <jmcneill@invisible.ca>
53ec47f87Sjmcneill  * All rights reserved.
63ec47f87Sjmcneill  *
73ec47f87Sjmcneill  * Redistribution and use in source and binary forms, with or without
83ec47f87Sjmcneill  * modification, are permitted provided that the following conditions
93ec47f87Sjmcneill  * are met:
103ec47f87Sjmcneill  * 1. Redistributions of source code must retain the above copyright
113ec47f87Sjmcneill  *    notice, this list of conditions and the following disclaimer.
123ec47f87Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
133ec47f87Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
143ec47f87Sjmcneill  *    documentation and/or other materials provided with the distribution.
153ec47f87Sjmcneill  *
163ec47f87Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
173ec47f87Sjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
183ec47f87Sjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
193ec47f87Sjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
203ec47f87Sjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
213ec47f87Sjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
223ec47f87Sjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
233ec47f87Sjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
243ec47f87Sjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
253ec47f87Sjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
263ec47f87Sjmcneill  * SUCH DAMAGE.
273ec47f87Sjmcneill  */
283ec47f87Sjmcneill 
293ec47f87Sjmcneill #include <sys/cdefs.h>
30*b7341c0eSjmcneill __KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.108 2024/06/30 17:55:28 jmcneill Exp $");
313ec47f87Sjmcneill 
323ec47f87Sjmcneill #include "opt_arm_debug.h"
33c1ca2ff5Sskrll #include "opt_bootconfig.h"
343ec47f87Sjmcneill #include "opt_cpuoptions.h"
35c1ca2ff5Sskrll #include "opt_ddb.h"
3656949079Sjmcneill #include "opt_efi.h"
37c1ca2ff5Sskrll #include "opt_machdep.h"
38c1ca2ff5Sskrll #include "opt_multiprocessor.h"
393ec47f87Sjmcneill 
402824038dSjmcneill #include "genfb.h"
41e96f84fcSjmcneill #include "ukbd.h"
425f3db904Sbouyer #include "wsdisplay.h"
43e96f84fcSjmcneill 
443ec47f87Sjmcneill #include <sys/param.h>
4588261558Sskrll #include <sys/types.h>
4688261558Sskrll 
473ec47f87Sjmcneill #include <sys/atomic.h>
4888261558Sskrll #include <sys/bootblock.h>
4988261558Sskrll #include <sys/bus.h>
5088261558Sskrll #include <sys/conf.h>
513ec47f87Sjmcneill #include <sys/cpu.h>
523ec47f87Sjmcneill #include <sys/device.h>
5388261558Sskrll #include <sys/disk.h>
5488261558Sskrll #include <sys/disklabel.h>
559047c5eaSrin #include <sys/endian.h>
563ec47f87Sjmcneill #include <sys/exec.h>
5788261558Sskrll #include <sys/fcntl.h>
5888261558Sskrll #include <sys/kauth.h>
593ec47f87Sjmcneill #include <sys/kernel.h>
603ec47f87Sjmcneill #include <sys/kmem.h>
613ec47f87Sjmcneill #include <sys/ksyms.h>
6288261558Sskrll #include <sys/md5.h>
633ec47f87Sjmcneill #include <sys/msgbuf.h>
643ec47f87Sjmcneill #include <sys/proc.h>
6589217092Sjmcneill #include <sys/pserialize.h>
6688261558Sskrll #include <sys/reboot.h>
6788261558Sskrll #include <sys/systm.h>
6888261558Sskrll #include <sys/termios.h>
6988261558Sskrll #include <sys/vnode.h>
7088261558Sskrll #include <sys/uuid.h>
7189217092Sjmcneill 
7289217092Sjmcneill #include <net/if.h>
7389217092Sjmcneill #include <net/if_dl.h>
743ec47f87Sjmcneill 
75f36fc520Sryo #include <dev/cons.h>
763ec47f87Sjmcneill #include <uvm/uvm_extern.h>
773ec47f87Sjmcneill 
783ec47f87Sjmcneill #include <machine/db_machdep.h>
793ec47f87Sjmcneill #include <ddb/db_sym.h>
803ec47f87Sjmcneill #include <ddb/db_extern.h>
813ec47f87Sjmcneill 
823ec47f87Sjmcneill #include <machine/bootconfig.h>
833ec47f87Sjmcneill #include <arm/armreg.h>
843ec47f87Sjmcneill 
85fe33aa27Sryo #include <arm/cpufunc.h>
863ec47f87Sjmcneill 
873ec47f87Sjmcneill #include <evbarm/include/autoconf.h>
88b9673f86Sskrll #include <evbarm/fdt/machdep.h>
893ec47f87Sjmcneill #include <evbarm/fdt/platform.h>
903ec47f87Sjmcneill 
913ec47f87Sjmcneill #include <arm/fdt/arm_fdtvar.h>
928d564c5dSskrll 
938d564c5dSskrll #include <dev/fdt/fdtvar.h>
947fc70db4Sskrll #include <dev/fdt/fdt_boot.h>
9525c08ff6Sskrll #include <dev/fdt/fdt_private.h>
96ff970d37Sskrll #include <dev/fdt/fdt_memory.h>
973ec47f87Sjmcneill 
9856949079Sjmcneill #ifdef EFI_RUNTIME
9956949079Sjmcneill #include <arm/arm/efi_runtime.h>
10056949079Sjmcneill #endif
10156949079Sjmcneill 
1022824038dSjmcneill #if NWSDISPLAY > 0 && NGENFB > 0
1032824038dSjmcneill #include <arm/fdt/arm_simplefb.h>
1042824038dSjmcneill #endif
1052824038dSjmcneill 
106e96f84fcSjmcneill #if NUKBD > 0
107e96f84fcSjmcneill #include <dev/usb/ukbdvar.h>
108e96f84fcSjmcneill #endif
1095f3db904Sbouyer #if NWSDISPLAY > 0
1105f3db904Sbouyer #include <dev/wscons/wsdisplayvar.h>
1115f3db904Sbouyer #endif
112e96f84fcSjmcneill 
1133ec47f87Sjmcneill BootConfig bootconfig;
1143ec47f87Sjmcneill char *boot_args = NULL;
115dfc83eacSchristos 
116dfc83eacSchristos /* filled in before cleaning bss. keep in .data */
1172eb89e8aSchristos u_long uboot_args[4] __attribute__((__section__(".data")));
118823c3a9bSskrll const uint8_t *fdt_addr_r __attribute__((__section__(".data")));
1193ec47f87Sjmcneill 
1203ec47f87Sjmcneill #include <libfdt.h>
1213ec47f87Sjmcneill #include <dev/fdt/fdtvar.h>
12278107969Sjmcneill #define FDT_BUF_SIZE	(512*1024)
1233ec47f87Sjmcneill static uint8_t fdt_data[FDT_BUF_SIZE];
1243ec47f87Sjmcneill 
1253ec47f87Sjmcneill extern char KERNEL_BASE_phys[];
1263ec47f87Sjmcneill #define KERNEL_BASE_PHYS ((paddr_t)KERNEL_BASE_phys)
1273ec47f87Sjmcneill 
1283ec47f87Sjmcneill static void fdt_device_register(device_t, void *);
1295f3db904Sbouyer static void fdt_device_register_post_config(device_t, void *);
1303ec47f87Sjmcneill static void fdt_reset(void);
1313ec47f87Sjmcneill static void fdt_powerdown(void);
1323ec47f87Sjmcneill 
1339047c5eaSrin #if BYTE_ORDER == BIG_ENDIAN
1349047c5eaSrin static void fdt_update_fb_format(void);
1359047c5eaSrin #endif
1369047c5eaSrin 
1373ec47f87Sjmcneill static void
earlyconsputc(dev_t dev,int c)138f36fc520Sryo earlyconsputc(dev_t dev, int c)
1393ec47f87Sjmcneill {
140d9031769Sskrll 	uartputc(c);
1413ec47f87Sjmcneill }
142f36fc520Sryo 
143f36fc520Sryo static int
earlyconsgetc(dev_t dev)144f36fc520Sryo earlyconsgetc(dev_t dev)
145f36fc520Sryo {
146bdf24698Sjmcneill 	return -1;
147f36fc520Sryo }
148f36fc520Sryo 
149ea0da87bSskrll static struct consdev earlycons = {
150ea0da87bSskrll 	.cn_putc = earlyconsputc,
151ea0da87bSskrll 	.cn_getc = earlyconsgetc,
152ea0da87bSskrll 	.cn_pollc = nullcnpollc,
153ea0da87bSskrll };
154ea0da87bSskrll 
155f36fc520Sryo #ifdef VERBOSE_INIT_ARM
156e9a606f2Sskrll #define VPRINTF(...)	printf(__VA_ARGS__)
1573ec47f87Sjmcneill #else
1582505ba45Sskrll #define VPRINTF(...)	__nothing
1593ec47f87Sjmcneill #endif
1603ec47f87Sjmcneill 
161d377d504Sjmcneill static void
fdt_add_dram_blocks(const struct fdt_memory * m,void * arg)162d377d504Sjmcneill fdt_add_dram_blocks(const struct fdt_memory *m, void *arg)
163d377d504Sjmcneill {
164d377d504Sjmcneill 	BootConfig *bc = arg;
165d377d504Sjmcneill 
16697044f64Sjmcneill 	VPRINTF("  %" PRIx64 " - %" PRIx64 "\n", m->start, m->end - 1);
167d377d504Sjmcneill 	bc->dram[bc->dramblocks].address = m->start;
168d377d504Sjmcneill 	bc->dram[bc->dramblocks].pages =
169d377d504Sjmcneill 	    (m->end - m->start) / PAGE_SIZE;
170d377d504Sjmcneill 	bc->dramblocks++;
171d377d504Sjmcneill }
172d377d504Sjmcneill 
173d377d504Sjmcneill static int nfdt_physmem = 0;
174accf8784Sjmcneill static struct boot_physmem fdt_physmem[FDT_MEMORY_RANGES];
175d377d504Sjmcneill 
176d377d504Sjmcneill static void
fdt_add_boot_physmem(const struct fdt_memory * m,void * arg)177d377d504Sjmcneill fdt_add_boot_physmem(const struct fdt_memory *m, void *arg)
178d377d504Sjmcneill {
17942840b43Sskrll 	const paddr_t saddr = round_page(m->start);
18042840b43Sskrll 	const paddr_t eaddr = trunc_page(m->end);
181d377d504Sjmcneill 
18242840b43Sskrll 	VPRINTF("  %" PRIx64 " - %" PRIx64, m->start, m->end - 1);
18342840b43Sskrll 	if (saddr >= eaddr) {
18442840b43Sskrll 		VPRINTF(" skipped\n");
18542840b43Sskrll 		return;
18642840b43Sskrll 	}
18742840b43Sskrll 	VPRINTF("\n");
18842840b43Sskrll 
18942840b43Sskrll 	struct boot_physmem *bp = &fdt_physmem[nfdt_physmem++];
190d377d504Sjmcneill 
191accf8784Sjmcneill 	KASSERT(nfdt_physmem <= FDT_MEMORY_RANGES);
192d377d504Sjmcneill 
19342840b43Sskrll 	bp->bp_start = atop(saddr);
19442840b43Sskrll 	bp->bp_pages = atop(eaddr) - bp->bp_start;
195d377d504Sjmcneill 	bp->bp_freelist = VM_FREELIST_DEFAULT;
196d377d504Sjmcneill 
197d377d504Sjmcneill #ifdef PMAP_NEED_ALLOC_POOLPAGE
198d377d504Sjmcneill 	const uint64_t memory_size = *(uint64_t *)arg;
199d377d504Sjmcneill 	if (atop(memory_size) > bp->bp_pages) {
200d377d504Sjmcneill 		arm_poolpage_vmfreelist = VM_FREELIST_DIRECTMAP;
201d377d504Sjmcneill 		bp->bp_freelist = VM_FREELIST_DIRECTMAP;
202d377d504Sjmcneill 	}
203d377d504Sjmcneill #endif
204d377d504Sjmcneill }
205d377d504Sjmcneill 
206ff970d37Sskrll 
207ff970d37Sskrll static void
fdt_print_memory(const struct fdt_memory * m,void * arg)208ff970d37Sskrll fdt_print_memory(const struct fdt_memory *m, void *arg)
209ff970d37Sskrll {
210ff970d37Sskrll 
211ff970d37Sskrll 	VPRINTF("FDT /memory @ 0x%" PRIx64 " size 0x%" PRIx64 "\n",
212ff970d37Sskrll 	    m->start, m->end - m->start);
213ff970d37Sskrll }
214ff970d37Sskrll 
215ff970d37Sskrll 
2161f933be8Sjmcneill /*
2171f933be8Sjmcneill  * Define usable memory regions.
2181f933be8Sjmcneill  */
2191f933be8Sjmcneill static void
fdt_build_bootconfig(uint64_t mem_start,uint64_t mem_end)220fe33aa27Sryo fdt_build_bootconfig(uint64_t mem_start, uint64_t mem_end)
2211f933be8Sjmcneill {
2221f933be8Sjmcneill 	BootConfig *bc = &bootconfig;
223ff970d37Sskrll 
2241f933be8Sjmcneill 	uint64_t addr, size;
225d377d504Sjmcneill 	int index;
2261f933be8Sjmcneill 
2279b383adcSrin 	/* Reserve pages for ramdisk, rndseed, and firmware's RNG */
2289b383adcSrin 	fdt_reserve_initrd();
2299b383adcSrin 	fdt_reserve_rndseed();
2309b383adcSrin 	fdt_reserve_efirng();
23176fee9e5Sriastradh 
232932d2dbdSjmcneill 	const int framebuffer = OF_finddevice("/chosen/framebuffer");
233932d2dbdSjmcneill 	if (framebuffer >= 0) {
234932d2dbdSjmcneill 		for (index = 0;
235932d2dbdSjmcneill 		     fdtbus_get_reg64(framebuffer, index, &addr, &size) == 0;
236932d2dbdSjmcneill 		     index++) {
237ff970d37Sskrll 			fdt_memory_remove_range(addr, size);
238932d2dbdSjmcneill 		}
239932d2dbdSjmcneill 	}
240932d2dbdSjmcneill 
241e9a606f2Sskrll 	VPRINTF("Usable memory:\n");
2421f933be8Sjmcneill 	bc->dramblocks = 0;
243d377d504Sjmcneill 	fdt_memory_foreach(fdt_add_dram_blocks, bc);
2441f933be8Sjmcneill }
2451f933be8Sjmcneill 
24656949079Sjmcneill 
247bee3dfabSskrll vaddr_t
initarm(void * arg)2483ec47f87Sjmcneill initarm(void *arg)
2493ec47f87Sjmcneill {
2508d564c5dSskrll 	const struct fdt_platform *plat;
251fe33aa27Sryo 	uint64_t memory_start, memory_end;
2523ec47f87Sjmcneill 
253f36fc520Sryo 	/* set temporally to work printf()/panic() even before consinit() */
254f36fc520Sryo 	cn_tab = &earlycons;
255f36fc520Sryo 
2563ec47f87Sjmcneill 	/* Load FDT */
2573ec47f87Sjmcneill 	int error = fdt_check_header(fdt_addr_r);
2589497cc21Sskrll 	if (error != 0)
2599497cc21Sskrll 		panic("fdt_check_header failed: %s", fdt_strerror(error));
2609497cc21Sskrll 
261fb674b86Sjmcneill 	/* If the DTB is too big, try to pack it in place first. */
262fb674b86Sjmcneill 	if (fdt_totalsize(fdt_addr_r) > sizeof(fdt_data))
263fb674b86Sjmcneill 		(void)fdt_pack(__UNCONST(fdt_addr_r));
264ff970d37Sskrll 
2656366e9bfSjmcneill 	error = fdt_open_into(fdt_addr_r, fdt_data, sizeof(fdt_data));
2663ec47f87Sjmcneill 	if (error != 0)
2673ec47f87Sjmcneill 		panic("fdt_move failed: %s", fdt_strerror(error));
2689497cc21Sskrll 
2692b1ba9ffSthorpej 	fdtbus_init(fdt_data);
2703ec47f87Sjmcneill 
2713ec47f87Sjmcneill 	/* Lookup platform specific backend */
2728d564c5dSskrll 	plat = fdt_platform_find();
2733ec47f87Sjmcneill 	if (plat == NULL)
2743ec47f87Sjmcneill 		panic("Kernel does not support this device");
2753ec47f87Sjmcneill 
2763ec47f87Sjmcneill 	/* Early console may be available, announce ourselves. */
277e9a606f2Sskrll 	VPRINTF("FDT<%p>\n", fdt_addr_r);
2783ec47f87Sjmcneill 
279ad888f5eSrin 	boot_args = fdt_get_bootargs();
28098ec040aSjmcneill 
2813ec47f87Sjmcneill 	/* Heads up ... Setup the CPU / MMU / TLB functions. */
282e9a606f2Sskrll 	VPRINTF("cpufunc\n");
2833ec47f87Sjmcneill 	if (set_cpufuncs())
2843ec47f87Sjmcneill 		panic("cpu not recognized!");
2853ec47f87Sjmcneill 
286e6c2e807Sskrll 	/*
287e6c2e807Sskrll 	 * Memory is still identity/flat mapped this point so using ttbr for
288e6c2e807Sskrll 	 * l1pt VA is fine
289e6c2e807Sskrll 	 */
290e6c2e807Sskrll 
2918d564c5dSskrll 	VPRINTF("devmap %p\n", plat->fp_devmap());
292e6c2e807Sskrll 	extern char ARM_BOOTSTRAP_LxPT[];
2938d564c5dSskrll 	pmap_devmap_bootstrap((vaddr_t)ARM_BOOTSTRAP_LxPT, plat->fp_devmap());
294e6c2e807Sskrll 
295e9a606f2Sskrll 	VPRINTF("bootstrap\n");
2968d564c5dSskrll 	plat->fp_bootstrap();
297ee91b1e5Sskrll 
298b4f7190fSjmcneill 	/*
299b4f7190fSjmcneill 	 * If stdout-path is specified on the command line, override the
300b4f7190fSjmcneill 	 * value in /chosen/stdout-path before initializing console.
301b4f7190fSjmcneill 	 */
302e6c2e807Sskrll 	VPRINTF("stdout\n");
3037fc70db4Sskrll 	fdt_update_stdout_path(fdt_data, boot_args);
304b4f7190fSjmcneill 
3059047c5eaSrin #if BYTE_ORDER == BIG_ENDIAN
3069047c5eaSrin 	/*
3073ee2dbe8Sskrll 	 * Most boards are configured to little-endian mode initially, and
3089047c5eaSrin 	 * switched to big-endian mode after kernel is loaded. In this case,
3099047c5eaSrin 	 * framebuffer seems byte-swapped to CPU. Override FDT to let
3109047c5eaSrin 	 * drivers know.
3119047c5eaSrin 	 */
3129047c5eaSrin 	VPRINTF("fb_format\n");
3139047c5eaSrin 	fdt_update_fb_format();
3149047c5eaSrin #endif
3159047c5eaSrin 
3166366e9bfSjmcneill 	/*
3176366e9bfSjmcneill 	 * Done making changes to the FDT.
3186366e9bfSjmcneill 	 */
3196366e9bfSjmcneill 	fdt_pack(fdt_data);
3206366e9bfSjmcneill 
321e9a606f2Sskrll 	VPRINTF("consinit ");
3223ec47f87Sjmcneill 	consinit();
323e9a606f2Sskrll 	VPRINTF("ok\n");
3243ec47f87Sjmcneill 
325e9a606f2Sskrll 	VPRINTF("uboot: args %#lx, %#lx, %#lx, %#lx\n",
3263ec47f87Sjmcneill 	    uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
3273ec47f87Sjmcneill 
3283ec47f87Sjmcneill 	cpu_reset_address = fdt_reset;
3293ec47f87Sjmcneill 	cpu_powerdown_address = fdt_powerdown;
3303ec47f87Sjmcneill 	evbarm_device_register = fdt_device_register;
3315f3db904Sbouyer 	evbarm_device_register_post_config = fdt_device_register_post_config;
33281742547Sjmcneill 	evbarm_cpu_rootconf = fdt_cpu_rootconf;
3333ec47f87Sjmcneill 
3343ec47f87Sjmcneill 	/* Talk to the user */
3357377108cSskrll 	printf("NetBSD/evbarm (fdt) booting ...\n");
3363ec47f87Sjmcneill 
3373ec47f87Sjmcneill #ifdef BOOT_ARGS
3383ec47f87Sjmcneill 	char mi_bootargs[] = BOOT_ARGS;
3393ec47f87Sjmcneill 	parse_mi_bootargs(mi_bootargs);
3403ec47f87Sjmcneill #endif
3413ec47f87Sjmcneill 
342ff970d37Sskrll 	fdt_memory_get(&memory_start, &memory_end);
343ff970d37Sskrll 
344ff970d37Sskrll 	fdt_memory_foreach(fdt_print_memory, NULL);
3453ec47f87Sjmcneill 
3463ec47f87Sjmcneill #if !defined(_LP64)
347fef687f7Sskrll 	/* Cannot map memory above 4GB (remove last page as well) */
348fef687f7Sskrll 	const uint64_t memory_limit = 0x100000000ULL - PAGE_SIZE;
349ff970d37Sskrll 	if (memory_end > memory_limit) {
350ff970d37Sskrll 		fdt_memory_remove_range(memory_limit , memory_end);
351fef687f7Sskrll 		memory_end = memory_limit;
352ff970d37Sskrll 	}
35341117db8Sskrll #endif
354fe33aa27Sryo 	uint64_t memory_size = memory_end - memory_start;
3553ec47f87Sjmcneill 
356e6c2e807Sskrll 	VPRINTF("%s: memory start %" PRIx64 " end %" PRIx64 " (len %"
357e6c2e807Sskrll 	    PRIx64 ")\n", __func__, memory_start, memory_end, memory_size);
358e6c2e807Sskrll 
3591f933be8Sjmcneill 	/* Parse ramdisk info */
3609b383adcSrin 	fdt_probe_initrd();
3611f933be8Sjmcneill 
36276fee9e5Sriastradh 	/* Parse our on-disk rndseed and the firmware's RNG from EFI */
3639b383adcSrin 	fdt_probe_rndseed();
3649b383adcSrin 	fdt_probe_efirng();
365fedc9173Sriastradh 
366fad86990Sskrll 	fdt_memory_remove_reserved(memory_start, memory_end);
3675787a5adSskrll 
368ee91b1e5Sskrll 	/*
369d3a09a98Sskrll 	 * Populate bootconfig structure for the benefit of dodumpsys
370ee91b1e5Sskrll 	 */
371e6c2e807Sskrll 	VPRINTF("%s: fdt_build_bootconfig\n", __func__);
372fe33aa27Sryo 	fdt_build_bootconfig(memory_start, memory_end);
3733ec47f87Sjmcneill 
37441117db8Sskrll 	/* Perform PT build and VM init */
37541117db8Sskrll 	cpu_kernel_vm_init(memory_start, memory_size);
3763ec47f87Sjmcneill 
377ad888f5eSrin 	VPRINTF("bootargs: %s\n", boot_args);
3783ec47f87Sjmcneill 
3793ec47f87Sjmcneill 	parse_mi_bootargs(boot_args);
3803ec47f87Sjmcneill 
381e6c2e807Sskrll 	VPRINTF("Memory regions:\n");
3827b4484abSskrll 
3837b4484abSskrll 	/* Populate fdt_physmem / nfdt_physmem for initarm_common */
384d377d504Sjmcneill 	fdt_memory_foreach(fdt_add_boot_physmem, &memory_size);
3853ec47f87Sjmcneill 
386bee3dfabSskrll 	vaddr_t sp = initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, fdt_physmem,
387ee91b1e5Sskrll 	     nfdt_physmem);
388e6c2e807Sskrll 
38907964189Sskrll 	/*
3906dcefd7bSskrll 	 * initarm_common flushes cache if required before AP start
39107964189Sskrll 	 */
392ff397156Sskrll 	error = 0;
39318fe7abaSryo 	if ((boothowto & RB_MD1) == 0) {
394e6c2e807Sskrll 		VPRINTF("mpstart\n");
3958d564c5dSskrll 		if (plat->fp_mpstart)
3968d564c5dSskrll 			error = plat->fp_mpstart();
39718fe7abaSryo 	}
398e6c2e807Sskrll 
399ff397156Sskrll 	if (error)
400ff397156Sskrll 		return sp;
4011167459dSskrll 
402e6c2e807Sskrll 	/*
403e6c2e807Sskrll 	 * Now we have APs started the pages used for stacks and L1PT can
404e6c2e807Sskrll 	 * be given to uvm
405e6c2e807Sskrll 	 */
406c1c5485aSskrll 	extern char const __start__init_memory[];
407c1c5485aSskrll 	extern char const __stop__init_memory[] __weak;
408c1c5485aSskrll 
40962d13ffdSmrg 	if (&__start__init_memory[0] != &__stop__init_memory[0]) {
410e6c2e807Sskrll 		const paddr_t spa = KERN_VTOPHYS((vaddr_t)__start__init_memory);
411e6c2e807Sskrll 		const paddr_t epa = KERN_VTOPHYS((vaddr_t)__stop__init_memory);
412e6c2e807Sskrll 		const paddr_t spg = atop(spa);
413e6c2e807Sskrll 		const paddr_t epg = atop(epa);
414e6c2e807Sskrll 
415b74ac930Sskrll 		VPRINTF("         start %08lx  end %08lx... "
416b74ac930Sskrll 		    "loading in freelist %d\n", spa, epa, VM_FREELIST_DEFAULT);
417b74ac930Sskrll 
418e6c2e807Sskrll 		uvm_page_physload(spg, epg, spg, epg, VM_FREELIST_DEFAULT);
419e6c2e807Sskrll 	}
420e6c2e807Sskrll 
421e6c2e807Sskrll 	return sp;
4223ec47f87Sjmcneill }
4233ec47f87Sjmcneill 
4243ec47f87Sjmcneill void
consinit(void)4253ec47f87Sjmcneill consinit(void)
4263ec47f87Sjmcneill {
4273ec47f87Sjmcneill 	static bool initialized = false;
4288d564c5dSskrll 	const struct fdt_platform *plat = fdt_platform_find();
4293ec47f87Sjmcneill 	const struct fdt_console *cons = fdtbus_get_console();
4303ec47f87Sjmcneill 	struct fdt_attach_args faa;
431faf8273eSjmcneill 	u_int uart_freq = 0;
4323ec47f87Sjmcneill 
4333ec47f87Sjmcneill 	if (initialized || cons == NULL)
4343ec47f87Sjmcneill 		return;
4353ec47f87Sjmcneill 
4368d564c5dSskrll 	plat->fp_init_attach_args(&faa);
4373ec47f87Sjmcneill 	faa.faa_phandle = fdtbus_get_stdout_phandle();
4383ec47f87Sjmcneill 
4398d564c5dSskrll 	if (plat->fp_uart_freq != NULL)
4408d564c5dSskrll 		uart_freq = plat->fp_uart_freq();
441faf8273eSjmcneill 
442faf8273eSjmcneill 	cons->consinit(&faa, uart_freq);
4433ec47f87Sjmcneill 
4443ec47f87Sjmcneill 	initialized = true;
4453ec47f87Sjmcneill }
4463ec47f87Sjmcneill 
4472a1124acSjmcneill void
cpu_startup_hook(void)448fedc9173Sriastradh cpu_startup_hook(void)
449fedc9173Sriastradh {
450e65a0eaaSskrll #ifdef EFI_RUNTIME
451e65a0eaaSskrll 	fdt_map_efi_runtime("netbsd,uefi-runtime-code", ARM_EFIRT_MEM_CODE);
452e65a0eaaSskrll 	fdt_map_efi_runtime("netbsd,uefi-runtime-data", ARM_EFIRT_MEM_DATA);
453e65a0eaaSskrll 	fdt_map_efi_runtime("netbsd,uefi-runtime-mmio", ARM_EFIRT_MEM_MMIO);
454e65a0eaaSskrll #endif
455fedc9173Sriastradh 
45625c08ff6Sskrll 	fdtbus_intr_init();
45725c08ff6Sskrll 
458fedc9173Sriastradh 	fdt_setup_rndseed();
45976fee9e5Sriastradh 	fdt_setup_efirng();
460fedc9173Sriastradh }
461fedc9173Sriastradh 
462fedc9173Sriastradh void
delay(u_int us)4632a1124acSjmcneill delay(u_int us)
4642a1124acSjmcneill {
4658d564c5dSskrll 	const struct fdt_platform *plat = fdt_platform_find();
4662a1124acSjmcneill 
4678d564c5dSskrll 	plat->fp_delay(us);
4682a1124acSjmcneill }
46981742547Sjmcneill static void
fdt_device_register(device_t self,void * aux)4703ec47f87Sjmcneill fdt_device_register(device_t self, void *aux)
4713ec47f87Sjmcneill {
4728d564c5dSskrll 	const struct fdt_platform *plat = fdt_platform_find();
4733ec47f87Sjmcneill 
4742824038dSjmcneill 	if (device_is_a(self, "armfdt")) {
4751f933be8Sjmcneill 		fdt_setup_initrd();
4761f933be8Sjmcneill 
4772824038dSjmcneill #if NWSDISPLAY > 0 && NGENFB > 0
4782824038dSjmcneill 		/*
4792824038dSjmcneill 		 * Setup framebuffer console, if present.
4802824038dSjmcneill 		 */
4812824038dSjmcneill 		arm_simplefb_preattach();
4822824038dSjmcneill #endif
4832824038dSjmcneill 	}
4842824038dSjmcneill 
48536ef29e8Sjmcneill #if NWSDISPLAY > 0 && NGENFB > 0
48636ef29e8Sjmcneill 	if (device_is_a(self, "genfb")) {
48736ef29e8Sjmcneill 		prop_dictionary_t dict = device_properties(self);
48836ef29e8Sjmcneill 		prop_dictionary_set_uint64(dict,
48936ef29e8Sjmcneill 		    "simplefb-physaddr", arm_simplefb_physaddr());
49036ef29e8Sjmcneill 	}
49136ef29e8Sjmcneill #endif
49236ef29e8Sjmcneill 
4938d564c5dSskrll 	if (plat && plat->fp_device_register)
4948d564c5dSskrll 		plat->fp_device_register(self, aux);
4953ec47f87Sjmcneill }
4963ec47f87Sjmcneill 
4973ec47f87Sjmcneill static void
fdt_device_register_post_config(device_t self,void * aux)4985f3db904Sbouyer fdt_device_register_post_config(device_t self, void *aux)
4995f3db904Sbouyer {
500*b7341c0eSjmcneill 	const struct fdt_platform *plat = fdt_platform_find();
501*b7341c0eSjmcneill 
502*b7341c0eSjmcneill 	if (plat && plat->fp_device_register_post_config)
503*b7341c0eSjmcneill 		plat->fp_device_register_post_config(self, aux);
504*b7341c0eSjmcneill 
5055f3db904Sbouyer #if NUKBD > 0 && NWSDISPLAY > 0
5065f3db904Sbouyer 	if (device_is_a(self, "wsdisplay")) {
5075f3db904Sbouyer 		struct wsdisplay_softc *sc = device_private(self);
5085f3db904Sbouyer 		if (wsdisplay_isconsole(sc))
5095f3db904Sbouyer 			ukbd_cnattach();
5105f3db904Sbouyer 	}
5115f3db904Sbouyer #endif
5125f3db904Sbouyer }
5135f3db904Sbouyer 
5145f3db904Sbouyer static void
fdt_reset(void)5153ec47f87Sjmcneill fdt_reset(void)
5163ec47f87Sjmcneill {
5178d564c5dSskrll 	const struct fdt_platform *plat = fdt_platform_find();
5183ec47f87Sjmcneill 
5193ec47f87Sjmcneill 	fdtbus_power_reset();
5203ec47f87Sjmcneill 
5218d564c5dSskrll 	if (plat && plat->fp_reset)
5228d564c5dSskrll 		plat->fp_reset();
5233ec47f87Sjmcneill }
5243ec47f87Sjmcneill 
5253ec47f87Sjmcneill static void
fdt_powerdown(void)5263ec47f87Sjmcneill fdt_powerdown(void)
5273ec47f87Sjmcneill {
5283ec47f87Sjmcneill 	fdtbus_power_poweroff();
5293ec47f87Sjmcneill }
5309047c5eaSrin 
5319047c5eaSrin #if BYTE_ORDER == BIG_ENDIAN
5329047c5eaSrin static void
fdt_update_fb_format(void)5339047c5eaSrin fdt_update_fb_format(void)
5349047c5eaSrin {
5359047c5eaSrin 	int off, len;
5369047c5eaSrin 	const char *format, *replace;
5379047c5eaSrin 
5389047c5eaSrin 	off = fdt_path_offset(fdt_data, "/chosen");
5399047c5eaSrin 	if (off < 0)
5409047c5eaSrin 		return;
5419047c5eaSrin 
5429047c5eaSrin 	for (;;) {
5439047c5eaSrin 		off = fdt_node_offset_by_compatible(fdt_data, off,
5449047c5eaSrin 		    "simple-framebuffer");
5459047c5eaSrin 		if (off < 0)
5469047c5eaSrin 			return;
5479047c5eaSrin 
5489047c5eaSrin 		format = fdt_getprop(fdt_data, off, "format", &len);
5499047c5eaSrin 		if (format == NULL)
5509047c5eaSrin 			continue;
5519047c5eaSrin 
5529047c5eaSrin 		replace = NULL;
5539047c5eaSrin 		if (strcmp(format, "a8b8g8r8") == 0)
5549047c5eaSrin 			replace = "r8g8b8a8";
5559047c5eaSrin 		else if (strcmp(format, "x8r8g8b8") == 0)
5569047c5eaSrin 			replace = "b8g8r8x8";
5579047c5eaSrin 		if (replace != NULL)
5589047c5eaSrin 			fdt_setprop(fdt_data, off, "format", replace,
5599047c5eaSrin 			    strlen(replace) + 1);
5609047c5eaSrin 	}
5619047c5eaSrin }
5629047c5eaSrin #endif
563