1813dff13SHelge Deller /* 2813dff13SHelge Deller * QEMU HPPA hardware system emulator. 3813dff13SHelge Deller * Copyright 2018 Helge Deller <deller@gmx.de> 4813dff13SHelge Deller */ 5813dff13SHelge Deller 6813dff13SHelge Deller #include "qemu/osdep.h" 7813dff13SHelge Deller #include "qemu-common.h" 8813dff13SHelge Deller #include "cpu.h" 9813dff13SHelge Deller #include "elf.h" 10813dff13SHelge Deller #include "hw/loader.h" 11813dff13SHelge Deller #include "hw/boards.h" 12813dff13SHelge Deller #include "qemu/error-report.h" 1371e8a915SMarkus Armbruster #include "sysemu/reset.h" 14813dff13SHelge Deller #include "sysemu/sysemu.h" 15bcdb9064SPhilippe Mathieu-Daudé #include "hw/rtc/mc146818rtc.h" 16813dff13SHelge Deller #include "hw/timer/i8254.h" 17813dff13SHelge Deller #include "hw/char/serial.h" 18376b8519SHelge Deller #include "hw/net/lasi_82596.h" 19070e9a1eSMichael S. Tsirkin #include "hppa_sys.h" 20c108cc59SPhilippe Mathieu-Daudé #include "qemu/units.h" 21813dff13SHelge Deller #include "qapi/error.h" 22852c27e2SPaolo Bonzini #include "net/net.h" 23691cbbadSRichard Henderson #include "qemu/log.h" 24e07c4f44SPhilippe Mathieu-Daudé #include "net/net.h" 25813dff13SHelge Deller 26a72bd606SHelge Deller #define MAX_IDE_BUS 2 27a72bd606SHelge Deller 28a72bd606SHelge Deller static ISABus *hppa_isa_bus(void) 29a72bd606SHelge Deller { 30a72bd606SHelge Deller ISABus *isa_bus; 31a72bd606SHelge Deller qemu_irq *isa_irqs; 32a72bd606SHelge Deller MemoryRegion *isa_region; 33a72bd606SHelge Deller 34a72bd606SHelge Deller isa_region = g_new(MemoryRegion, 1); 35a72bd606SHelge Deller memory_region_init_io(isa_region, NULL, &hppa_pci_ignore_ops, 36a72bd606SHelge Deller NULL, "isa-io", 0x800); 37a72bd606SHelge Deller memory_region_add_subregion(get_system_memory(), IDE_HPA, 38a72bd606SHelge Deller isa_region); 39a72bd606SHelge Deller 40a72bd606SHelge Deller isa_bus = isa_bus_new(NULL, get_system_memory(), isa_region, 41a72bd606SHelge Deller &error_abort); 42a72bd606SHelge Deller isa_irqs = i8259_init(isa_bus, 43a72bd606SHelge Deller /* qemu_allocate_irq(dino_set_isa_irq, s, 0)); */ 44a72bd606SHelge Deller NULL); 45a72bd606SHelge Deller isa_bus_irqs(isa_bus, isa_irqs); 46a72bd606SHelge Deller 47a72bd606SHelge Deller return isa_bus; 48a72bd606SHelge Deller } 49a72bd606SHelge Deller 50a72bd606SHelge Deller static uint64_t cpu_hppa_to_phys(void *opaque, uint64_t addr) 51a72bd606SHelge Deller { 52a72bd606SHelge Deller addr &= (0x10000000 - 1); 53a72bd606SHelge Deller return addr; 54a72bd606SHelge Deller } 55a72bd606SHelge Deller 56a72bd606SHelge Deller static HPPACPU *cpu[HPPA_MAX_CPUS]; 57a72bd606SHelge Deller static uint64_t firmware_entry; 58813dff13SHelge Deller 59813dff13SHelge Deller static void machine_hppa_init(MachineState *machine) 60813dff13SHelge Deller { 61a72bd606SHelge Deller const char *kernel_filename = machine->kernel_filename; 62a72bd606SHelge Deller const char *kernel_cmdline = machine->kernel_cmdline; 63a72bd606SHelge Deller const char *initrd_filename = machine->initrd_filename; 64877eb21dSMark Cave-Ayland DeviceState *dev; 65a72bd606SHelge Deller PCIBus *pci_bus; 66a72bd606SHelge Deller ISABus *isa_bus; 67a72bd606SHelge Deller qemu_irq rtc_irq, serial_irq; 68a72bd606SHelge Deller char *firmware_filename; 69a72bd606SHelge Deller uint64_t firmware_low, firmware_high; 70a72bd606SHelge Deller long size; 71a72bd606SHelge Deller uint64_t kernel_entry = 0, kernel_low, kernel_high; 72a72bd606SHelge Deller MemoryRegion *addr_space = get_system_memory(); 73a72bd606SHelge Deller MemoryRegion *rom_region; 74a72bd606SHelge Deller MemoryRegion *cpu_region; 75a72bd606SHelge Deller long i; 7633decbd2SLike Xu unsigned int smp_cpus = machine->smp.cpus; 774765384cSSven Schnelle SysBusDevice *s; 78a72bd606SHelge Deller 79a72bd606SHelge Deller /* Create CPUs. */ 80a72bd606SHelge Deller for (i = 0; i < smp_cpus; i++) { 81266a880eSPaolo Bonzini char *name = g_strdup_printf("cpu%ld-io-eir", i); 82a72bd606SHelge Deller cpu[i] = HPPA_CPU(cpu_create(machine->cpu_type)); 83a72bd606SHelge Deller 84a72bd606SHelge Deller cpu_region = g_new(MemoryRegion, 1); 85a72bd606SHelge Deller memory_region_init_io(cpu_region, OBJECT(cpu[i]), &hppa_io_eir_ops, 86266a880eSPaolo Bonzini cpu[i], name, 4); 87a72bd606SHelge Deller memory_region_add_subregion(addr_space, CPU_HPA + i * 0x1000, 88a72bd606SHelge Deller cpu_region); 89266a880eSPaolo Bonzini g_free(name); 90813dff13SHelge Deller } 91813dff13SHelge Deller 92a72bd606SHelge Deller /* Main memory region. */ 93b7746b11SPhilippe Mathieu-Daudé if (machine->ram_size > 3 * GiB) { 94b7746b11SPhilippe Mathieu-Daudé error_report("RAM size is currently restricted to 3GB"); 95b7746b11SPhilippe Mathieu-Daudé exit(EXIT_FAILURE); 96b7746b11SPhilippe Mathieu-Daudé } 977c59c1e0SIgor Mammedov memory_region_add_subregion_overlap(addr_space, 0, machine->ram, -1); 987c59c1e0SIgor Mammedov 99a72bd606SHelge Deller 100376b8519SHelge Deller /* Init Lasi chip */ 101376b8519SHelge Deller lasi_init(addr_space); 102376b8519SHelge Deller 103a72bd606SHelge Deller /* Init Dino (PCI host bus chip). */ 104a72bd606SHelge Deller pci_bus = dino_init(addr_space, &rtc_irq, &serial_irq); 105a72bd606SHelge Deller assert(pci_bus); 106a72bd606SHelge Deller 107a72bd606SHelge Deller /* Create ISA bus. */ 108a72bd606SHelge Deller isa_bus = hppa_isa_bus(); 109a72bd606SHelge Deller assert(isa_bus); 110a72bd606SHelge Deller 111a72bd606SHelge Deller /* Realtime clock, used by firmware for PDC_TOD call. */ 112a72bd606SHelge Deller mc146818_rtc_init(isa_bus, 2000, rtc_irq); 113a72bd606SHelge Deller 114a72bd606SHelge Deller /* Serial code setup. */ 1159bca0edbSPeter Maydell if (serial_hd(0)) { 116a72bd606SHelge Deller uint32_t addr = DINO_UART_HPA + 0x800; 117a72bd606SHelge Deller serial_mm_init(addr_space, addr, 0, serial_irq, 1189bca0edbSPeter Maydell 115200, serial_hd(0), DEVICE_BIG_ENDIAN); 119a72bd606SHelge Deller } 120a72bd606SHelge Deller 121a72bd606SHelge Deller /* SCSI disk setup. */ 122877eb21dSMark Cave-Ayland dev = DEVICE(pci_create_simple(pci_bus, -1, "lsi53c895a")); 123877eb21dSMark Cave-Ayland lsi53c8xx_handle_legacy_cmdline(dev); 124a72bd606SHelge Deller 1254765384cSSven Schnelle /* Graphics setup. */ 1264765384cSSven Schnelle if (machine->enable_graphics && vga_interface_type != VGA_NONE) { 1273e80f690SMarkus Armbruster dev = qdev_new("artist"); 1284765384cSSven Schnelle s = SYS_BUS_DEVICE(dev); 129*3c6ef471SMarkus Armbruster sysbus_realize_and_unref(s, &error_fatal); 1304765384cSSven Schnelle sysbus_mmio_map(s, 0, LASI_GFX_HPA); 1314765384cSSven Schnelle sysbus_mmio_map(s, 1, ARTIST_FB_ADDR); 1324765384cSSven Schnelle } 1334765384cSSven Schnelle 1340e6de551SHelge Deller /* Network setup. */ 135a72bd606SHelge Deller for (i = 0; i < nb_nics; i++) { 136376b8519SHelge Deller if (!enable_lasi_lan()) { 1370e6de551SHelge Deller pci_nic_init_nofail(&nd_table[i], pci_bus, "tulip", NULL); 138a72bd606SHelge Deller } 139376b8519SHelge Deller } 140a72bd606SHelge Deller 141a72bd606SHelge Deller /* Load firmware. Given that this is not "real" firmware, 142a72bd606SHelge Deller but one explicitly written for the emulation, we might as 143a72bd606SHelge Deller well load it directly from an ELF image. */ 144a72bd606SHelge Deller firmware_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, 145a72bd606SHelge Deller bios_name ? bios_name : 146a72bd606SHelge Deller "hppa-firmware.img"); 147a72bd606SHelge Deller if (firmware_filename == NULL) { 148a72bd606SHelge Deller error_report("no firmware provided"); 149a72bd606SHelge Deller exit(1); 150a72bd606SHelge Deller } 151a72bd606SHelge Deller 1524366e1dbSLiam Merwick size = load_elf(firmware_filename, NULL, NULL, NULL, 1536cdda0ffSAleksandar Markovic &firmware_entry, &firmware_low, &firmware_high, NULL, 154a72bd606SHelge Deller true, EM_PARISC, 0, 0); 155a72bd606SHelge Deller 156a72bd606SHelge Deller /* Unfortunately, load_elf sign-extends reading elf32. */ 157a72bd606SHelge Deller firmware_entry = (target_ureg)firmware_entry; 158a72bd606SHelge Deller firmware_low = (target_ureg)firmware_low; 159a72bd606SHelge Deller firmware_high = (target_ureg)firmware_high; 160a72bd606SHelge Deller 161a72bd606SHelge Deller if (size < 0) { 162a72bd606SHelge Deller error_report("could not load firmware '%s'", firmware_filename); 163a72bd606SHelge Deller exit(1); 164a72bd606SHelge Deller } 165691cbbadSRichard Henderson qemu_log_mask(CPU_LOG_PAGE, "Firmware loaded at 0x%08" PRIx64 166691cbbadSRichard Henderson "-0x%08" PRIx64 ", entry at 0x%08" PRIx64 ".\n", 167a72bd606SHelge Deller firmware_low, firmware_high, firmware_entry); 1688262863dSPhilippe Mathieu-Daudé if (firmware_low < FIRMWARE_START || firmware_high >= FIRMWARE_END) { 169a72bd606SHelge Deller error_report("Firmware overlaps with memory or IO space"); 170a72bd606SHelge Deller exit(1); 171a72bd606SHelge Deller } 172a72bd606SHelge Deller g_free(firmware_filename); 173a72bd606SHelge Deller 174a72bd606SHelge Deller rom_region = g_new(MemoryRegion, 1); 1756a3a2e82SIgor Mammedov memory_region_init_ram(rom_region, NULL, "firmware", 1766a3a2e82SIgor Mammedov (FIRMWARE_END - FIRMWARE_START), &error_fatal); 177a72bd606SHelge Deller memory_region_add_subregion(addr_space, FIRMWARE_START, rom_region); 178a72bd606SHelge Deller 179a72bd606SHelge Deller /* Load kernel */ 180a72bd606SHelge Deller if (kernel_filename) { 1814366e1dbSLiam Merwick size = load_elf(kernel_filename, NULL, &cpu_hppa_to_phys, 1826cdda0ffSAleksandar Markovic NULL, &kernel_entry, &kernel_low, &kernel_high, NULL, 183a72bd606SHelge Deller true, EM_PARISC, 0, 0); 184a72bd606SHelge Deller 185a72bd606SHelge Deller /* Unfortunately, load_elf sign-extends reading elf32. */ 186a72bd606SHelge Deller kernel_entry = (target_ureg) cpu_hppa_to_phys(NULL, kernel_entry); 187a72bd606SHelge Deller kernel_low = (target_ureg)kernel_low; 188a72bd606SHelge Deller kernel_high = (target_ureg)kernel_high; 189a72bd606SHelge Deller 190a72bd606SHelge Deller if (size < 0) { 191a72bd606SHelge Deller error_report("could not load kernel '%s'", kernel_filename); 192a72bd606SHelge Deller exit(1); 193a72bd606SHelge Deller } 194691cbbadSRichard Henderson qemu_log_mask(CPU_LOG_PAGE, "Kernel loaded at 0x%08" PRIx64 195691cbbadSRichard Henderson "-0x%08" PRIx64 ", entry at 0x%08" PRIx64 196c108cc59SPhilippe Mathieu-Daudé ", size %" PRIu64 " kB\n", 197c108cc59SPhilippe Mathieu-Daudé kernel_low, kernel_high, kernel_entry, size / KiB); 198a72bd606SHelge Deller 199a72bd606SHelge Deller if (kernel_cmdline) { 200a72bd606SHelge Deller cpu[0]->env.gr[24] = 0x4000; 201a72bd606SHelge Deller pstrcpy_targphys("cmdline", cpu[0]->env.gr[24], 202a72bd606SHelge Deller TARGET_PAGE_SIZE, kernel_cmdline); 203a72bd606SHelge Deller } 204a72bd606SHelge Deller 205a72bd606SHelge Deller if (initrd_filename) { 206a72bd606SHelge Deller ram_addr_t initrd_base; 207f3839fdaSLi Zhijian int64_t initrd_size; 208a72bd606SHelge Deller 209a72bd606SHelge Deller initrd_size = get_image_size(initrd_filename); 210a72bd606SHelge Deller if (initrd_size < 0) { 211a72bd606SHelge Deller error_report("could not load initial ram disk '%s'", 212a72bd606SHelge Deller initrd_filename); 213a72bd606SHelge Deller exit(1); 214a72bd606SHelge Deller } 215a72bd606SHelge Deller 216a72bd606SHelge Deller /* Load the initrd image high in memory. 217a72bd606SHelge Deller Mirror the algorithm used by palo: 218a72bd606SHelge Deller (1) Due to sign-extension problems and PDC, 219a72bd606SHelge Deller put the initrd no higher than 1G. 220a72bd606SHelge Deller (2) Reserve 64k for stack. */ 221c108cc59SPhilippe Mathieu-Daudé initrd_base = MIN(ram_size, 1 * GiB); 222c108cc59SPhilippe Mathieu-Daudé initrd_base = initrd_base - 64 * KiB; 223a72bd606SHelge Deller initrd_base = (initrd_base - initrd_size) & TARGET_PAGE_MASK; 224a72bd606SHelge Deller 225a72bd606SHelge Deller if (initrd_base < kernel_high) { 226a72bd606SHelge Deller error_report("kernel and initial ram disk too large!"); 227a72bd606SHelge Deller exit(1); 228a72bd606SHelge Deller } 229a72bd606SHelge Deller 230a72bd606SHelge Deller load_image_targphys(initrd_filename, initrd_base, initrd_size); 231a72bd606SHelge Deller cpu[0]->env.gr[23] = initrd_base; 232a72bd606SHelge Deller cpu[0]->env.gr[22] = initrd_base + initrd_size; 233a72bd606SHelge Deller } 234a72bd606SHelge Deller } 235a72bd606SHelge Deller 236a72bd606SHelge Deller if (!kernel_entry) { 237a72bd606SHelge Deller /* When booting via firmware, tell firmware if we want interactive 238a72bd606SHelge Deller * mode (kernel_entry=1), and to boot from CD (gr[24]='d') 239a72bd606SHelge Deller * or hard disc * (gr[24]='c'). 240a72bd606SHelge Deller */ 241a72bd606SHelge Deller kernel_entry = boot_menu ? 1 : 0; 242a72bd606SHelge Deller cpu[0]->env.gr[24] = machine->boot_order[0]; 243a72bd606SHelge Deller } 244a72bd606SHelge Deller 245a72bd606SHelge Deller /* We jump to the firmware entry routine and pass the 246a72bd606SHelge Deller * various parameters in registers. After firmware initialization, 247a72bd606SHelge Deller * firmware will start the Linux kernel with ramdisk and cmdline. 248a72bd606SHelge Deller */ 249a72bd606SHelge Deller cpu[0]->env.gr[26] = ram_size; 250a72bd606SHelge Deller cpu[0]->env.gr[25] = kernel_entry; 251a72bd606SHelge Deller 252a72bd606SHelge Deller /* tell firmware how many SMP CPUs to present in inventory table */ 253a72bd606SHelge Deller cpu[0]->env.gr[21] = smp_cpus; 254a72bd606SHelge Deller } 255a72bd606SHelge Deller 256a0628599SLike Xu static void hppa_machine_reset(MachineState *ms) 257a72bd606SHelge Deller { 25833decbd2SLike Xu unsigned int smp_cpus = ms->smp.cpus; 259a72bd606SHelge Deller int i; 260a72bd606SHelge Deller 261a72bd606SHelge Deller qemu_devices_reset(); 262a72bd606SHelge Deller 263a72bd606SHelge Deller /* Start all CPUs at the firmware entry point. 264a72bd606SHelge Deller * Monarch CPU will initialize firmware, secondary CPUs 265a72bd606SHelge Deller * will enter a small idle look and wait for rendevouz. */ 266a72bd606SHelge Deller for (i = 0; i < smp_cpus; i++) { 267a72bd606SHelge Deller cpu_set_pc(CPU(cpu[i]), firmware_entry); 268a72bd606SHelge Deller cpu[i]->env.gr[5] = CPU_HPA + i * 0x1000; 269a72bd606SHelge Deller } 270a72bd606SHelge Deller 271a72bd606SHelge Deller /* already initialized by machine_hppa_init()? */ 272a72bd606SHelge Deller if (cpu[0]->env.gr[26] == ram_size) { 273a72bd606SHelge Deller return; 274a72bd606SHelge Deller } 275a72bd606SHelge Deller 276a72bd606SHelge Deller cpu[0]->env.gr[26] = ram_size; 277a72bd606SHelge Deller cpu[0]->env.gr[25] = 0; /* no firmware boot menu */ 278a72bd606SHelge Deller cpu[0]->env.gr[24] = 'c'; 279a72bd606SHelge Deller /* gr22/gr23 unused, no initrd while reboot. */ 280a72bd606SHelge Deller cpu[0]->env.gr[21] = smp_cpus; 281a72bd606SHelge Deller } 282a72bd606SHelge Deller 283a72bd606SHelge Deller 284813dff13SHelge Deller static void machine_hppa_machine_init(MachineClass *mc) 285813dff13SHelge Deller { 286813dff13SHelge Deller mc->desc = "HPPA generic machine"; 287a72bd606SHelge Deller mc->default_cpu_type = TYPE_HPPA_CPU; 288813dff13SHelge Deller mc->init = machine_hppa_init; 289a72bd606SHelge Deller mc->reset = hppa_machine_reset; 290813dff13SHelge Deller mc->block_default_type = IF_SCSI; 291a72bd606SHelge Deller mc->max_cpus = HPPA_MAX_CPUS; 292a72bd606SHelge Deller mc->default_cpus = 1; 293ea0ac7f6SPhilippe Mathieu-Daudé mc->is_default = true; 294d23b6caaSPhilippe Mathieu-Daudé mc->default_ram_size = 512 * MiB; 295813dff13SHelge Deller mc->default_boot_order = "cd"; 2967c59c1e0SIgor Mammedov mc->default_ram_id = "ram"; 297813dff13SHelge Deller } 298813dff13SHelge Deller 299813dff13SHelge Deller DEFINE_MACHINE("hppa", machine_hppa_machine_init) 300