machdep.c revision 1.2
1/* $NetBSD: machdep.c,v 1.2 2026/01/10 22:45:57 jmcneill Exp $ */
2
3/*
4 * Copyright (c) 2002, 2024 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net) at Sandburst Corp.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
34 * Copyright (C) 1995, 1996 TooLs GmbH.
35 * All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 *    notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 *    notice, this list of conditions and the following disclaimer in the
44 *    documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 *    must display the following acknowledgement:
47 *	This product includes software developed by TooLs GmbH.
48 * 4. The name of TooLs GmbH may not be used to endorse or promote products
49 *    derived from this software without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
56 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
57 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
58 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
59 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
60 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 */
62
63#define _POWERPC_BUS_DMA_PRIVATE
64
65#include <sys/cdefs.h>
66__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.2 2026/01/10 22:45:57 jmcneill Exp $");
67
68#include "opt_compat_netbsd.h"
69#include "opt_ddb.h"
70#include "opt_ddbparam.h"
71#include "opt_inet.h"
72#include "opt_ns.h"
73#include "opt_oea.h"
74#include "opt_multiprocessor.h"
75
76#include <sys/param.h>
77#include <sys/buf.h>
78#include <sys/bus.h>
79#include <sys/conf.h>
80#include <sys/device.h>
81#include <sys/exec.h>
82#include <sys/extent.h>
83#include <sys/intr.h>
84#include <sys/kernel.h>
85#include <sys/kgdb.h>
86#include <sys/ksyms.h>
87#include <sys/mbuf.h>
88#include <sys/mount.h>
89#include <sys/msgbuf.h>
90#include <sys/proc.h>
91#include <sys/reboot.h>
92#include <sys/syscallargs.h>
93#include <sys/sysctl.h>
94#include <sys/syslog.h>
95#include <sys/systm.h>
96#include <sys/cpu.h>
97
98#include <uvm/uvm_extern.h>
99
100#include <machine/powerpc.h>
101#include <machine/wii.h>
102#include <machine/wiiu.h>
103#include <arch/evbppc/nintendo/dev/gecko.h>
104#include <arch/evbppc/nintendo/pic_pi.h>
105#include <arch/evbppc/nintendo/ipi_latte.h>
106
107#include <powerpc/bus_funcs.h>
108#include <powerpc/db_machdep.h>
109#include <powerpc/pio.h>
110#include <powerpc/pmap.h>
111#include <powerpc/spr.h>
112#include <powerpc/trap.h>
113
114#include <powerpc/oea/bat.h>
115#include <powerpc/oea/spr.h>
116#include <powerpc/oea/hid.h>
117#include <powerpc/pic/picvar.h>
118
119#include <ddb/db_extern.h>
120
121#include <dev/wscons/wsconsio.h>
122#include <dev/wscons/wsdisplayvar.h>
123#include <dev/rasops/rasops.h>
124#include <dev/wsfont/wsfont.h>
125#include <dev/wscons/wsdisplay_vconsvar.h>
126
127#include <dev/usb/ukbdvar.h>
128
129#include "ksyms.h"
130#include "ukbd.h"
131#include "genfb.h"
132#include "gecko.h"
133
134#ifndef WII_DEFAULT_CMDLINE
135#define WII_DEFAULT_CMDLINE "root=ld0a"
136#endif
137
138#define IBM750CL_SPR_HID4	1011
139
140#define MINI_MEM2_START		0x13f00000	/* Start of reserved MEM2 for MINI */
141
142#define PI_INTERRUPT_CAUSE	0x0c003000
143#define  RESET_SWITCH_STATE	__BIT(16)
144
145extern u_int l2cr_config;
146
147struct powerpc_bus_space wii_mem_tag = {
148	.pbs_flags = _BUS_SPACE_BIG_ENDIAN |
149		     _BUS_SPACE_MEM_TYPE,
150};
151
152static char ex_storage[1][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
153    __attribute__((aligned(8)));
154
155static bus_addr_t
156wii_dma_phys_to_bus_mem(bus_dma_tag_t t, bus_addr_t addr)
157{
158	return addr;
159}
160
161static bus_addr_t
162wii_dma_bus_mem_to_phys(bus_dma_tag_t t, bus_addr_t addr)
163{
164	return addr;
165}
166
167static int
168wii_mem2_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
169    bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
170    int flags)
171{
172	struct mem_region *mem, *avail;
173	const int mem2_index = wiiu_native ? 2 : 1;
174
175	/* Restrict memory used for DMA to ranges in MEM2 */
176	mem_regions(&mem, &avail);
177	if (mem[mem2_index].size == 0) {
178		return ENOMEM;
179	}
180
181	return _bus_dmamem_alloc_range(t, size, alignment, boundary, segs,
182	    nsegs, rsegs, flags, mem[mem2_index].start,
183	    mem[mem2_index].start + mem[mem2_index].size - PAGE_SIZE - 1);
184}
185
186struct powerpc_bus_dma_tag wii_bus_dma_tag = {
187	0, 0,				/* _bounce_thresh_min/max */
188	_bus_dmamap_create,
189	_bus_dmamap_destroy,
190	_bus_dmamap_load,
191	_bus_dmamap_load_mbuf,
192	_bus_dmamap_load_uio,
193	_bus_dmamap_load_raw,
194	_bus_dmamap_unload,
195	_bus_dmamap_sync,
196	_bus_dmamem_alloc,
197	_bus_dmamem_free,
198	_bus_dmamem_map,
199	_bus_dmamem_unmap,
200	_bus_dmamem_mmap,
201	wii_dma_phys_to_bus_mem,
202	wii_dma_bus_mem_to_phys,
203};
204
205struct powerpc_bus_dma_tag wii_mem2_bus_dma_tag = {
206	WII_MEM2_BASE,				/* _bounce_thresh_min */
207	WII_MEM2_BASE + WII_MEM2_SIZE - 1,	/* _bounce_thresh_max */
208	_bus_dmamap_create,
209	_bus_dmamap_destroy,
210	_bus_dmamap_load,
211	_bus_dmamap_load_mbuf,
212	_bus_dmamap_load_uio,
213	_bus_dmamap_load_raw,
214	_bus_dmamap_unload,
215	_bus_dmamap_sync,
216	wii_mem2_dmamem_alloc,
217	_bus_dmamem_free,
218	_bus_dmamem_map,
219	_bus_dmamem_unmap,
220	_bus_dmamem_mmap,
221	wii_dma_phys_to_bus_mem,
222	wii_dma_bus_mem_to_phys,
223};
224
225
226/*
227 * Global variables used here and there
228 */
229struct mem_region physmemr[4], availmemr[4];
230char wii_cmdline[1024];
231bool wiiu_plat;		/* Running on a Wii U */
232bool wiiu_native;	/* Native Wii U mode (not vWii) */
233
234void initppc(u_int, u_int, u_int, void *); /* Called from locore */
235void wii_dolphin_elf_loader_id(void);
236
237static void wii_setup(void);
238static void wii_poweroff(void);
239static void wii_reset(void);
240static void wii_halt(void);
241static void init_decrementer(void);
242
243static void
244system_detect(void)
245{
246	uint32_t val;
247
248	val = in32(LT_CHIPREVID);
249	if (__SHIFTOUT(val, LT_CHIPREVID_MAGIC) == LT_CHIPREVID_MAGIC_CAFE) {
250		wiiu_plat = true;
251		wiiu_native = (in32(LT_PIMCOMPAT) & PPC_COMPAT) == 0;
252	}
253}
254
255static void
256wii_init_cmdline(void)
257{
258	extern struct wii_argv wii_argv;
259
260	if (wii_argv.magic == WII_ARGV_MAGIC) {
261		void *ptr = (void *)(uintptr_t)(wii_argv.cmdline & ~0x80000000);
262		if (ptr != NULL) {
263			memcpy(wii_cmdline, ptr, wii_argv.length);
264		}
265	} else {
266		snprintf(wii_cmdline, sizeof(wii_cmdline), WII_DEFAULT_CMDLINE);
267	}
268}
269
270static void
271wii_init_memmap(u_int endkernel)
272{
273	uint32_t mem2_start, mem2_end;
274
275	mem2_start = in32(GLOBAL_MEM2_AVAIL_START) & ~0x80000000;
276	mem2_end = in32(GLOBAL_MEM2_AVAIL_END) & ~0x80000000;
277	if (mem2_start < WII_MEM2_BASE) {
278		/* Must have been booted from MINI. */
279		mem2_start = WII_MEM2_BASE + DSP_MEM_SIZE;
280		mem2_end = MINI_MEM2_START;
281	}
282	/*
283	 * Clear GLOBAL_MEM2_AVAIL_{START,END} so we can detect the correct
284	 * memory size when soft resetting from IOS to MINI.
285	 */
286	out32(GLOBAL_MEM2_AVAIL_START, 0);
287	out32(GLOBAL_MEM2_AVAIL_END, 0);
288
289	/* MEM1 24MB 1T-SRAM */
290	physmemr[0].start = WII_MEM1_BASE;
291	physmemr[0].size = WII_MEM1_SIZE;
292
293	/* MEM2 64MB GDDR3 */
294	physmemr[1].start = WII_MEM2_BASE;
295	physmemr[1].size = WII_MEM2_SIZE;
296
297	physmemr[2].size = 0;
298
299	/* MEM1 available memory */
300	availmemr[0].start = ((endkernel & ~0x80000000) + PGOFSET) & ~PGOFSET;
301	availmemr[0].size = physmemr[0].size - availmemr[0].start;
302	/* External framebuffer is at the end of MEM1 */
303	availmemr[0].size -= XFB_SIZE;
304
305	/* MEM2 available memory */
306	availmemr[1].start = mem2_start;
307	availmemr[1].size = mem2_end - mem2_start;
308
309	availmemr[2].size = 0;
310}
311
312static void
313wiiu_init_cmdline(void)
314{
315	struct wiiu_argv *argv = (struct wiiu_argv *)WIIU_LOADER_DATA_ADDR;
316
317	if (argv->magic == WIIU_LOADER_MAGIC) {
318		memcpy(wii_cmdline, argv->cmdline, sizeof(argv->cmdline));
319		wii_cmdline[sizeof(argv->cmdline) - 1] = '\0';
320	} else {
321		snprintf(wii_cmdline, sizeof(wii_cmdline), WII_DEFAULT_CMDLINE);
322	}
323}
324
325static void
326wiiu_init_memmap(u_int endkernel)
327{
328	/* MEM1 32MB */
329	physmemr[0].start  = 0x00000000;
330	physmemr[0].size   = 0x2000000;
331
332	/* MEM0 3MB */
333	physmemr[1].start  = 0x08000000;
334	physmemr[1].size   = 0x300000;
335
336	/* MEM2 2GB */
337	physmemr[2].start  = 0x10000000;
338	physmemr[2].size   = 0x80000000;
339
340	physmemr[3].size   = 0;
341
342	/* MEM1 available memory */
343	availmemr[0].start = (endkernel + PGOFSET) & ~PGOFSET;
344	availmemr[0].size  = physmemr[0].size - availmemr[0].start;
345
346	/*
347	 * MEM2 available memory. Reserved regions:
348	 *   USB DMA			0x10000000 - 0x103fffff
349	 *   Sound DMA			0x10400000 - 0x1041ffff
350	 *   Framebuffer		0x17500000 - 0x17a7ffff
351	 */
352	availmemr[1].start = 0x10420000;
353	availmemr[1].size  = 0x17500000 - availmemr[1].start;
354	availmemr[2].start = 0x17a80000;
355	availmemr[2].size  = physmemr[2].start + physmemr[2].size -
356			     availmemr[2].start;
357
358	availmemr[3].size  = 0;
359}
360
361void
362initppc(u_int startkernel, u_int endkernel, u_int args, void *btinfo)
363{
364	extern uint32_t ticks_per_sec;
365	extern uint32_t ticks_per_msec;
366	extern unsigned char edata[], end[];
367	register_t scratch, spr;
368
369	memset(&edata, 0, end - edata); /* clear BSS */
370	wii_cmdline[0] = '\0';
371
372	system_detect();
373
374	if (wiiu_native) {
375		wiiu_init_cmdline();
376		wiiu_init_memmap(endkernel);
377	} else {
378		wii_init_cmdline();
379		wii_init_memmap(endkernel);
380	}
381
382#ifdef BOOTHOWTO
383	/*
384	 * boothowto
385	 */
386	boothowto = BOOTHOWTO;
387#endif
388
389	cpu_model_init();
390
391	if (!wiiu_native) {
392		spr = mfspr(IBM750CL_SPR_HID4);
393		spr |= HID4_L2_CCFI;
394		spr |= HID4_DBP;
395		mtspr(IBM750CL_SPR_HID4, spr);
396		asm volatile ("isync");
397	}
398
399	if (wiiu_native) {
400		spr = mfspr(SPR_IBMESPRESSO_HID5);
401		mtspr(SPR_IBMESPRESSO_HID5, spr | HID5_H5A | HID5_PIRE);
402
403		spr = mfspr(SPR_SCR);
404		mtspr(SPR_SCR, (spr & ~0x40000000) | 0x80000000);
405
406		spr = mfspr(SPR_CAR);
407		mtspr(SPR_CAR, spr | 0xfc100000);
408
409		mtspr(SPR_BCR, 0x08000000);
410
411		asm volatile ("isync");
412	}
413
414	/* Configure L2 cache */
415	l2cr_config = L2CR_L2E;
416
417	if (bus_space_init(&wii_mem_tag, NULL,
418			   ex_storage[0], sizeof(ex_storage[0]))) {
419		panic("bus_space_init failed");
420	}
421
422	/*
423	 * Initialize the BAT registers
424	 */
425	if (wiiu_native) {
426		oea_batinit(
427		    WII_IOMEM_BASE, BAT_BL_32M,
428		    0);
429	} else {
430		oea_batinit(
431		    EFB_BASE, BAT_BL_128M,
432		    0);
433	}
434
435	/*
436	 * Set up trap vectors
437	 */
438	oea_init(NULL);
439
440	/*
441	 * Get CPU clock
442	 */
443	ticks_per_sec = wiiu_native ? WIIU_TIMEBASE_FREQ_HZ :
444				      WII_TIMEBASE_FREQ_HZ;
445	ticks_per_msec = ticks_per_sec / 1000;
446	cpu_timebase = ticks_per_sec;
447
448	wii_setup();
449
450	uvm_md_init();
451
452	/*
453	 * Initialize pmap module.
454	 */
455	pmap_bootstrap(startkernel, endkernel);
456
457	/* Now enable translation (and machine checks/recoverable interrupts) */
458	asm volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
459		      : "=r"(scratch)
460		      : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
461
462	/*
463	 * Setup decrementer
464	 */
465	init_decrementer();
466}
467
468void
469mem_regions(struct mem_region **mem, struct mem_region **avail)
470{
471	*mem = physmemr;
472	*avail = availmemr;
473}
474
475/*
476 * Machine dependent startup code.
477 */
478void
479cpu_startup(void)
480{
481	oea_startup(NULL);
482
483	/*
484	 * Now that we have VM, malloc()s are OK in bus_space.
485	 */
486	bus_space_mallocok();
487
488	/* Set up interrupt controller */
489	pic_init();
490	pi_init_intr();
491	oea_install_extint(pic_ext_intr);
492
493#ifdef MULTIPROCESSOR
494	if (wiiu_native) {
495		ipi_latte_init();
496		oea_install_extint_vec(pic_ext_intr, EXC_IPI);
497	}
498#endif
499}
500
501/*
502 * No early console support.
503 */
504void
505consinit(void)
506{
507#if NGENFB > 0
508	if (wiiu_native) {
509		extern void wiiufb_consinit(void);
510		wiiufb_consinit();
511	}
512#endif
513#if NGECKO > 0
514	if (!wiiu_native) {
515		usbgecko_consinit();
516	}
517#endif
518#if NUKBD > 0
519	ukbd_cnattach();
520#endif
521}
522
523/*
524 * Halt or reboot the machine after syncing/dumping according to howto.
525 */
526void
527cpu_reboot(int howto, char *what)
528{
529	static int syncing;
530	extern void disable_intr(void);
531
532	boothowto = howto;
533	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
534		syncing = 1;
535		vfs_shutdown();		/* sync */
536	}
537	splhigh();
538	if (!cold && (howto & RB_DUMP)) {
539		oea_dumpsys();
540	}
541	pmf_system_shutdown(boothowto);
542	doshutdownhooks();
543
544	disable_intr();
545
546#ifdef MULTIPROCESSOR
547	cpu_halt_others();
548	delay(100000);
549#endif
550
551	/* Force halt on panic to capture the cause on screen. */
552	if (panicstr != NULL) {
553		howto |= RB_HALT;
554	}
555	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
556		printf("power off\n\n");
557		wii_poweroff();
558		delay(100000);
559		printf("power off failed!\n\n");
560	}
561	if (howto & RB_HALT) {
562		printf("The operating system has halted.\n");
563		wii_halt();
564	}
565
566	printf("rebooting...\n\n");
567	wii_reset();
568	while (1);
569}
570
571static void
572wii_setup(void)
573{
574	if (!wiiu_plat) {
575		/* Turn on the drive slot LED. */
576		wii_slot_led(true);
577
578		/* Enable PPC access to SHUTDOWN GPIO. */
579		out32(HW_GPIO_OWNER, in32(HW_GPIO_OWNER) | __BIT(GPIO_SHUTDOWN));
580
581		/* Enable PPC access to DI_SPIN GPIO. */
582		out32(HW_GPIO_OWNER, in32(HW_GPIO_OWNER) | __BIT(GPIO_DI_SPIN));
583
584		/* Enable PPC access to EXI bus. */
585		out32(HW_AIPPROT, in32(HW_AIPPROT) | ENAHBIOPI);
586
587		/* Enable DVD video support. */
588		out32(HW_COMPAT, in32(HW_COMPAT) & ~DVDVIDEO);
589	}
590}
591
592static void
593wiiu_wood_ipc(uint32_t msg)
594{
595	KASSERT(wiiu_native);
596
597	out32(HW_IPCPPCMSG, msg);
598	out32(HW_IPCPPCCTRL, HW_IPCPPCCTRL_X1);
599	for (;;) {
600		if ((in32(HW_IPCPPCCTRL) & HW_IPCPPCCTRL_X1) == 0) {
601			break;
602		}
603	}
604}
605
606static void
607wii_poweroff(void)
608{
609	if (wiiu_native) {
610#if notyet
611		wiiu_wood_ipc(0xcafe0001);	/* CMD_POWEROFF */
612#endif
613	} else {
614		out32(HW_GPIOB_OUT, in32(HW_GPIOB_OUT) | __BIT(GPIO_SHUTDOWN));
615	}
616}
617
618static void
619wii_reset(void)
620{
621	if (wiiu_native) {
622#if notyet
623		wiiu_wood_ipc(0xcafe0002);	/* CMD_REBOOT */
624#else
625		wiiu_wood_ipc(0xcafe0001);	/* CMD_POWEROFF */
626#endif
627	} else {
628		out32(HW_RESETS, in32(HW_RESETS) & ~RSTBINB);
629	}
630}
631
632static void
633wii_halt(void)
634{
635	printf("Please press the %s button to reboot.\n",
636	    wiiu_native ? "POWER" : "RESET");
637	if (wiiu_native) {
638		bool pressed = false;
639		while (1) {
640			if ((in32(HW_GPIOB_IN) & __BIT(WIIU_GPIO_POWER)) != 0) {
641				/* Button was pressed */
642				pressed = true;
643			} else if (pressed) {
644				/* Button was released */
645				break;
646			}
647		}
648	} else {
649		while (1) {
650			if ((in32(PI_INTERRUPT_CAUSE) & RESET_SWITCH_STATE) == 0) {
651				break;
652			}
653		}
654	}
655}
656
657static void
658init_decrementer(void)
659{
660	extern uint32_t ns_per_tick;
661	extern uint32_t ticks_per_intr;
662	extern uint32_t ticks_per_sec;
663	int scratch, msr;
664
665	KASSERT(ticks_per_sec != 0);
666
667	__asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
668			: "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE));
669	ns_per_tick = 1000000000 / ticks_per_sec;
670	ticks_per_intr = ticks_per_sec / hz;
671	cpu_timebase = ticks_per_sec;
672
673	curcpu()->ci_lasttb = mftbl();
674
675	mtspr(SPR_DEC, ticks_per_intr);
676	mtmsr(msr);
677}
678