machdep.c revision 1.1
1/* $NetBSD: machdep.c,v 1.1 2026/01/09 22:54:28 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.1 2026/01/09 22:54:28 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 /* TODO */ 316 snprintf(wii_cmdline, sizeof(wii_cmdline), WII_DEFAULT_CMDLINE); 317} 318 319static void 320wiiu_init_memmap(u_int endkernel) 321{ 322 /* MEM1 32MB */ 323 physmemr[0].start = 0x00000000; 324 physmemr[0].size = 0x2000000; 325 326 /* MEM0 3MB */ 327 physmemr[1].start = 0x08000000; 328 physmemr[1].size = 0x300000; 329 330 /* MEM2 2GB */ 331 physmemr[2].start = 0x10000000; 332 physmemr[2].size = 0x80000000; 333 334 physmemr[3].size = 0; 335 336 /* MEM1 available memory */ 337 availmemr[0].start = (endkernel + PGOFSET) & ~PGOFSET; 338 availmemr[0].size = physmemr[0].size - availmemr[0].start; 339 340 /* 341 * MEM2 available memory. Reserved regions: 342 * USB DMA 0x10000000 - 0x103fffff 343 * Sound DMA 0x10400000 - 0x1041ffff 344 * Framebuffer 0x17500000 - 0x17a7ffff 345 */ 346 availmemr[1].start = 0x10420000; 347 availmemr[1].size = 0x17500000 - availmemr[1].start; 348 availmemr[2].start = 0x17a80000; 349 availmemr[2].size = physmemr[2].start + physmemr[2].size - 350 availmemr[2].start; 351 352 availmemr[3].size = 0; 353} 354 355void 356initppc(u_int startkernel, u_int endkernel, u_int args, void *btinfo) 357{ 358 extern uint32_t ticks_per_sec; 359 extern uint32_t ticks_per_msec; 360 extern unsigned char edata[], end[]; 361 register_t scratch, spr; 362 363 memset(&edata, 0, end - edata); /* clear BSS */ 364 wii_cmdline[0] = '\0'; 365 366 system_detect(); 367 368 if (wiiu_native) { 369 wiiu_init_cmdline(); 370 wiiu_init_memmap(endkernel); 371 } else { 372 wii_init_cmdline(); 373 wii_init_memmap(endkernel); 374 } 375 376#ifdef BOOTHOWTO 377 /* 378 * boothowto 379 */ 380 boothowto = BOOTHOWTO; 381#endif 382 383 cpu_model_init(); 384 385 if (!wiiu_native) { 386 spr = mfspr(IBM750CL_SPR_HID4); 387 spr |= HID4_L2_CCFI; 388 spr |= HID4_DBP; 389 mtspr(IBM750CL_SPR_HID4, spr); 390 asm volatile ("isync"); 391 } 392 393 if (wiiu_native) { 394 spr = mfspr(SPR_IBMESPRESSO_HID5); 395 mtspr(SPR_IBMESPRESSO_HID5, spr | HID5_H5A | HID5_PIRE); 396 397 spr = mfspr(SPR_SCR); 398 mtspr(SPR_SCR, (spr & ~0x40000000) | 0x80000000); 399 400 spr = mfspr(SPR_CAR); 401 mtspr(SPR_CAR, spr | 0xfc100000); 402 403 mtspr(SPR_BCR, 0x08000000); 404 405 asm volatile ("isync"); 406 } 407 408 /* Configure L2 cache */ 409 l2cr_config = L2CR_L2E; 410 411 if (bus_space_init(&wii_mem_tag, NULL, 412 ex_storage[0], sizeof(ex_storage[0]))) { 413 panic("bus_space_init failed"); 414 } 415 416 /* 417 * Initialize the BAT registers 418 */ 419 if (wiiu_native) { 420 oea_batinit( 421 WII_IOMEM_BASE, BAT_BL_32M, 422 0); 423 } else { 424 oea_batinit( 425 EFB_BASE, BAT_BL_128M, 426 0); 427 } 428 429 /* 430 * Set up trap vectors 431 */ 432 oea_init(NULL); 433 434 /* 435 * Get CPU clock 436 */ 437 ticks_per_sec = wiiu_native ? WIIU_TIMEBASE_FREQ_HZ : 438 WII_TIMEBASE_FREQ_HZ; 439 ticks_per_msec = ticks_per_sec / 1000; 440 cpu_timebase = ticks_per_sec; 441 442 wii_setup(); 443 444 uvm_md_init(); 445 446 /* 447 * Initialize pmap module. 448 */ 449 pmap_bootstrap(startkernel, endkernel); 450 451 /* Now enable translation (and machine checks/recoverable interrupts) */ 452 asm volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync" 453 : "=r"(scratch) 454 : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI)); 455 456 /* 457 * Setup decrementer 458 */ 459 init_decrementer(); 460} 461 462void 463mem_regions(struct mem_region **mem, struct mem_region **avail) 464{ 465 *mem = physmemr; 466 *avail = availmemr; 467} 468 469/* 470 * Machine dependent startup code. 471 */ 472void 473cpu_startup(void) 474{ 475 oea_startup(NULL); 476 477 /* 478 * Now that we have VM, malloc()s are OK in bus_space. 479 */ 480 bus_space_mallocok(); 481 482 /* Set up interrupt controller */ 483 pic_init(); 484 pi_init_intr(); 485 oea_install_extint(pic_ext_intr); 486 487#ifdef MULTIPROCESSOR 488 if (wiiu_native) { 489 ipi_latte_init(); 490 oea_install_extint_vec(pic_ext_intr, EXC_IPI); 491 } 492#endif 493} 494 495/* 496 * No early console support. 497 */ 498void 499consinit(void) 500{ 501#if NGENFB > 0 502 if (wiiu_native) { 503 extern void wiiufb_consinit(void); 504 wiiufb_consinit(); 505 } 506#endif 507#if NGECKO > 0 508 if (!wiiu_native) { 509 usbgecko_consinit(); 510 } 511#endif 512#if NUKBD > 0 513 ukbd_cnattach(); 514#endif 515} 516 517/* 518 * Halt or reboot the machine after syncing/dumping according to howto. 519 */ 520void 521cpu_reboot(int howto, char *what) 522{ 523 static int syncing; 524 extern void disable_intr(void); 525 526 boothowto = howto; 527 if (!cold && !(howto & RB_NOSYNC) && !syncing) { 528 syncing = 1; 529 vfs_shutdown(); /* sync */ 530 } 531 splhigh(); 532 if (!cold && (howto & RB_DUMP)) { 533 oea_dumpsys(); 534 } 535 pmf_system_shutdown(boothowto); 536 doshutdownhooks(); 537 538 disable_intr(); 539 540#ifdef MULTIPROCESSOR 541 cpu_halt_others(); 542 delay(100000); 543#endif 544 545 /* Force halt on panic to capture the cause on screen. */ 546 if (panicstr != NULL) { 547 howto |= RB_HALT; 548 } 549 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) { 550 printf("power off\n\n"); 551 wii_poweroff(); 552 delay(100000); 553 printf("power off failed!\n\n"); 554 } 555 if (howto & RB_HALT) { 556 printf("The operating system has halted.\n"); 557 wii_halt(); 558 } 559 560 printf("rebooting...\n\n"); 561 wii_reset(); 562 while (1); 563} 564 565static void 566wii_setup(void) 567{ 568 if (!wiiu_plat) { 569 /* Turn on the drive slot LED. */ 570 wii_slot_led(true); 571 572 /* Enable PPC access to SHUTDOWN GPIO. */ 573 out32(HW_GPIO_OWNER, in32(HW_GPIO_OWNER) | __BIT(GPIO_SHUTDOWN)); 574 575 /* Enable PPC access to DI_SPIN GPIO. */ 576 out32(HW_GPIO_OWNER, in32(HW_GPIO_OWNER) | __BIT(GPIO_DI_SPIN)); 577 578 /* Enable PPC access to EXI bus. */ 579 out32(HW_AIPPROT, in32(HW_AIPPROT) | ENAHBIOPI); 580 581 /* Enable DVD video support. */ 582 out32(HW_COMPAT, in32(HW_COMPAT) & ~DVDVIDEO); 583 } 584} 585 586static void 587wiiu_wood_ipc(uint32_t msg) 588{ 589 KASSERT(wiiu_native); 590 591 out32(HW_IPCPPCMSG, msg); 592 out32(HW_IPCPPCCTRL, HW_IPCPPCCTRL_X1); 593 for (;;) { 594 if ((in32(HW_IPCPPCCTRL) & HW_IPCPPCCTRL_X1) == 0) { 595 break; 596 } 597 } 598} 599 600static void 601wii_poweroff(void) 602{ 603 if (wiiu_native) { 604#if notyet 605 wiiu_wood_ipc(0xcafe0001); /* CMD_POWEROFF */ 606#endif 607 } else { 608 out32(HW_GPIOB_OUT, in32(HW_GPIOB_OUT) | __BIT(GPIO_SHUTDOWN)); 609 } 610} 611 612static void 613wii_reset(void) 614{ 615 if (wiiu_native) { 616#if notyet 617 wiiu_wood_ipc(0xcafe0002); /* CMD_REBOOT */ 618#else 619 wiiu_wood_ipc(0xcafe0001); /* CMD_POWEROFF */ 620#endif 621 } else { 622 out32(HW_RESETS, in32(HW_RESETS) & ~RSTBINB); 623 } 624} 625 626static void 627wii_halt(void) 628{ 629 printf("Please press the %s button to reboot.\n", 630 wiiu_native ? "POWER" : "RESET"); 631 if (wiiu_native) { 632 bool pressed = false; 633 while (1) { 634 if ((in32(HW_GPIOB_IN) & __BIT(WIIU_GPIO_POWER)) != 0) { 635 /* Button was pressed */ 636 pressed = true; 637 } else if (pressed) { 638 /* Button was released */ 639 break; 640 } 641 } 642 } else { 643 while (1) { 644 if ((in32(PI_INTERRUPT_CAUSE) & RESET_SWITCH_STATE) == 0) { 645 break; 646 } 647 } 648 } 649} 650 651static void 652init_decrementer(void) 653{ 654 extern uint32_t ns_per_tick; 655 extern uint32_t ticks_per_intr; 656 extern uint32_t ticks_per_sec; 657 int scratch, msr; 658 659 KASSERT(ticks_per_sec != 0); 660 661 __asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1" 662 : "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE)); 663 ns_per_tick = 1000000000 / ticks_per_sec; 664 ticks_per_intr = ticks_per_sec / hz; 665 cpu_timebase = ticks_per_sec; 666 667 curcpu()->ci_lasttb = mftbl(); 668 669 mtspr(SPR_DEC, ticks_per_intr); 670 mtmsr(msr); 671} 672