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 "hw/hw.h" 10813dff13SHelge Deller #include "elf.h" 11813dff13SHelge Deller #include "hw/loader.h" 12813dff13SHelge Deller #include "hw/boards.h" 13813dff13SHelge Deller #include "qemu/error-report.h" 14813dff13SHelge Deller #include "sysemu/sysemu.h" 15813dff13SHelge Deller #include "hw/timer/mc146818rtc.h" 16813dff13SHelge Deller #include "hw/ide.h" 17813dff13SHelge Deller #include "hw/timer/i8254.h" 18813dff13SHelge Deller #include "hw/char/serial.h" 19070e9a1eSMichael S. Tsirkin #include "hppa_sys.h" 20c108cc59SPhilippe Mathieu-Daudé #include "qemu/units.h" 21813dff13SHelge Deller #include "qapi/error.h" 22691cbbadSRichard Henderson #include "qemu/log.h" 23813dff13SHelge Deller 24a72bd606SHelge Deller #define MAX_IDE_BUS 2 25a72bd606SHelge Deller 26a72bd606SHelge Deller static ISABus *hppa_isa_bus(void) 27a72bd606SHelge Deller { 28a72bd606SHelge Deller ISABus *isa_bus; 29a72bd606SHelge Deller qemu_irq *isa_irqs; 30a72bd606SHelge Deller MemoryRegion *isa_region; 31a72bd606SHelge Deller 32a72bd606SHelge Deller isa_region = g_new(MemoryRegion, 1); 33a72bd606SHelge Deller memory_region_init_io(isa_region, NULL, &hppa_pci_ignore_ops, 34a72bd606SHelge Deller NULL, "isa-io", 0x800); 35a72bd606SHelge Deller memory_region_add_subregion(get_system_memory(), IDE_HPA, 36a72bd606SHelge Deller isa_region); 37a72bd606SHelge Deller 38a72bd606SHelge Deller isa_bus = isa_bus_new(NULL, get_system_memory(), isa_region, 39a72bd606SHelge Deller &error_abort); 40a72bd606SHelge Deller isa_irqs = i8259_init(isa_bus, 41a72bd606SHelge Deller /* qemu_allocate_irq(dino_set_isa_irq, s, 0)); */ 42a72bd606SHelge Deller NULL); 43a72bd606SHelge Deller isa_bus_irqs(isa_bus, isa_irqs); 44a72bd606SHelge Deller 45a72bd606SHelge Deller return isa_bus; 46a72bd606SHelge Deller } 47a72bd606SHelge Deller 48a72bd606SHelge Deller static uint64_t cpu_hppa_to_phys(void *opaque, uint64_t addr) 49a72bd606SHelge Deller { 50a72bd606SHelge Deller addr &= (0x10000000 - 1); 51a72bd606SHelge Deller return addr; 52a72bd606SHelge Deller } 53a72bd606SHelge Deller 54a72bd606SHelge Deller static HPPACPU *cpu[HPPA_MAX_CPUS]; 55a72bd606SHelge Deller static uint64_t firmware_entry; 56813dff13SHelge Deller 57813dff13SHelge Deller static void machine_hppa_init(MachineState *machine) 58813dff13SHelge Deller { 59a72bd606SHelge Deller const char *kernel_filename = machine->kernel_filename; 60a72bd606SHelge Deller const char *kernel_cmdline = machine->kernel_cmdline; 61a72bd606SHelge Deller const char *initrd_filename = machine->initrd_filename; 62877eb21dSMark Cave-Ayland DeviceState *dev; 63a72bd606SHelge Deller PCIBus *pci_bus; 64a72bd606SHelge Deller ISABus *isa_bus; 65a72bd606SHelge Deller qemu_irq rtc_irq, serial_irq; 66a72bd606SHelge Deller char *firmware_filename; 67a72bd606SHelge Deller uint64_t firmware_low, firmware_high; 68a72bd606SHelge Deller long size; 69a72bd606SHelge Deller uint64_t kernel_entry = 0, kernel_low, kernel_high; 70a72bd606SHelge Deller MemoryRegion *addr_space = get_system_memory(); 71a72bd606SHelge Deller MemoryRegion *rom_region; 72a72bd606SHelge Deller MemoryRegion *ram_region; 73a72bd606SHelge Deller MemoryRegion *cpu_region; 74a72bd606SHelge Deller long i; 75*33decbd2SLike Xu unsigned int smp_cpus = machine->smp.cpus; 76a72bd606SHelge Deller 77a72bd606SHelge Deller ram_size = machine->ram_size; 78a72bd606SHelge Deller 79a72bd606SHelge Deller /* Create CPUs. */ 80a72bd606SHelge Deller for (i = 0; i < smp_cpus; i++) { 81a72bd606SHelge Deller cpu[i] = HPPA_CPU(cpu_create(machine->cpu_type)); 82a72bd606SHelge Deller 83a72bd606SHelge Deller cpu_region = g_new(MemoryRegion, 1); 84a72bd606SHelge Deller memory_region_init_io(cpu_region, OBJECT(cpu[i]), &hppa_io_eir_ops, 85a72bd606SHelge Deller cpu[i], g_strdup_printf("cpu%ld-io-eir", i), 4); 86a72bd606SHelge Deller memory_region_add_subregion(addr_space, CPU_HPA + i * 0x1000, 87a72bd606SHelge Deller cpu_region); 88813dff13SHelge Deller } 89813dff13SHelge Deller 90a72bd606SHelge Deller /* Limit main memory. */ 91a72bd606SHelge Deller if (ram_size > FIRMWARE_START) { 92a72bd606SHelge Deller machine->ram_size = ram_size = FIRMWARE_START; 93a72bd606SHelge Deller } 94a72bd606SHelge Deller 95a72bd606SHelge Deller /* Main memory region. */ 96a72bd606SHelge Deller ram_region = g_new(MemoryRegion, 1); 97a72bd606SHelge Deller memory_region_allocate_system_memory(ram_region, OBJECT(machine), 98a72bd606SHelge Deller "ram", ram_size); 99a72bd606SHelge Deller memory_region_add_subregion(addr_space, 0, ram_region); 100a72bd606SHelge Deller 101a72bd606SHelge Deller /* Init Dino (PCI host bus chip). */ 102a72bd606SHelge Deller pci_bus = dino_init(addr_space, &rtc_irq, &serial_irq); 103a72bd606SHelge Deller assert(pci_bus); 104a72bd606SHelge Deller 105a72bd606SHelge Deller /* Create ISA bus. */ 106a72bd606SHelge Deller isa_bus = hppa_isa_bus(); 107a72bd606SHelge Deller assert(isa_bus); 108a72bd606SHelge Deller 109a72bd606SHelge Deller /* Realtime clock, used by firmware for PDC_TOD call. */ 110a72bd606SHelge Deller mc146818_rtc_init(isa_bus, 2000, rtc_irq); 111a72bd606SHelge Deller 112a72bd606SHelge Deller /* Serial code setup. */ 1139bca0edbSPeter Maydell if (serial_hd(0)) { 114a72bd606SHelge Deller uint32_t addr = DINO_UART_HPA + 0x800; 115a72bd606SHelge Deller serial_mm_init(addr_space, addr, 0, serial_irq, 1169bca0edbSPeter Maydell 115200, serial_hd(0), DEVICE_BIG_ENDIAN); 117a72bd606SHelge Deller } 118a72bd606SHelge Deller 119a72bd606SHelge Deller /* SCSI disk setup. */ 120877eb21dSMark Cave-Ayland dev = DEVICE(pci_create_simple(pci_bus, -1, "lsi53c895a")); 121877eb21dSMark Cave-Ayland lsi53c8xx_handle_legacy_cmdline(dev); 122a72bd606SHelge Deller 123a72bd606SHelge Deller /* Network setup. e1000 is good enough, failing Tulip support. */ 124a72bd606SHelge Deller for (i = 0; i < nb_nics; i++) { 125a72bd606SHelge Deller pci_nic_init_nofail(&nd_table[i], pci_bus, "e1000", NULL); 126a72bd606SHelge Deller } 127a72bd606SHelge Deller 128a72bd606SHelge Deller /* Load firmware. Given that this is not "real" firmware, 129a72bd606SHelge Deller but one explicitly written for the emulation, we might as 130a72bd606SHelge Deller well load it directly from an ELF image. */ 131a72bd606SHelge Deller firmware_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, 132a72bd606SHelge Deller bios_name ? bios_name : 133a72bd606SHelge Deller "hppa-firmware.img"); 134a72bd606SHelge Deller if (firmware_filename == NULL) { 135a72bd606SHelge Deller error_report("no firmware provided"); 136a72bd606SHelge Deller exit(1); 137a72bd606SHelge Deller } 138a72bd606SHelge Deller 1394366e1dbSLiam Merwick size = load_elf(firmware_filename, NULL, NULL, NULL, 1404366e1dbSLiam Merwick &firmware_entry, &firmware_low, &firmware_high, 141a72bd606SHelge Deller true, EM_PARISC, 0, 0); 142a72bd606SHelge Deller 143a72bd606SHelge Deller /* Unfortunately, load_elf sign-extends reading elf32. */ 144a72bd606SHelge Deller firmware_entry = (target_ureg)firmware_entry; 145a72bd606SHelge Deller firmware_low = (target_ureg)firmware_low; 146a72bd606SHelge Deller firmware_high = (target_ureg)firmware_high; 147a72bd606SHelge Deller 148a72bd606SHelge Deller if (size < 0) { 149a72bd606SHelge Deller error_report("could not load firmware '%s'", firmware_filename); 150a72bd606SHelge Deller exit(1); 151a72bd606SHelge Deller } 152691cbbadSRichard Henderson qemu_log_mask(CPU_LOG_PAGE, "Firmware loaded at 0x%08" PRIx64 153691cbbadSRichard Henderson "-0x%08" PRIx64 ", entry at 0x%08" PRIx64 ".\n", 154a72bd606SHelge Deller firmware_low, firmware_high, firmware_entry); 155a72bd606SHelge Deller if (firmware_low < ram_size || firmware_high >= FIRMWARE_END) { 156a72bd606SHelge Deller error_report("Firmware overlaps with memory or IO space"); 157a72bd606SHelge Deller exit(1); 158a72bd606SHelge Deller } 159a72bd606SHelge Deller g_free(firmware_filename); 160a72bd606SHelge Deller 161a72bd606SHelge Deller rom_region = g_new(MemoryRegion, 1); 162a72bd606SHelge Deller memory_region_allocate_system_memory(rom_region, OBJECT(machine), 163a72bd606SHelge Deller "firmware", 164a72bd606SHelge Deller (FIRMWARE_END - FIRMWARE_START)); 165a72bd606SHelge Deller memory_region_add_subregion(addr_space, FIRMWARE_START, rom_region); 166a72bd606SHelge Deller 167a72bd606SHelge Deller /* Load kernel */ 168a72bd606SHelge Deller if (kernel_filename) { 1694366e1dbSLiam Merwick size = load_elf(kernel_filename, NULL, &cpu_hppa_to_phys, 170a72bd606SHelge Deller NULL, &kernel_entry, &kernel_low, &kernel_high, 171a72bd606SHelge Deller true, EM_PARISC, 0, 0); 172a72bd606SHelge Deller 173a72bd606SHelge Deller /* Unfortunately, load_elf sign-extends reading elf32. */ 174a72bd606SHelge Deller kernel_entry = (target_ureg) cpu_hppa_to_phys(NULL, kernel_entry); 175a72bd606SHelge Deller kernel_low = (target_ureg)kernel_low; 176a72bd606SHelge Deller kernel_high = (target_ureg)kernel_high; 177a72bd606SHelge Deller 178a72bd606SHelge Deller if (size < 0) { 179a72bd606SHelge Deller error_report("could not load kernel '%s'", kernel_filename); 180a72bd606SHelge Deller exit(1); 181a72bd606SHelge Deller } 182691cbbadSRichard Henderson qemu_log_mask(CPU_LOG_PAGE, "Kernel loaded at 0x%08" PRIx64 183691cbbadSRichard Henderson "-0x%08" PRIx64 ", entry at 0x%08" PRIx64 184c108cc59SPhilippe Mathieu-Daudé ", size %" PRIu64 " kB\n", 185c108cc59SPhilippe Mathieu-Daudé kernel_low, kernel_high, kernel_entry, size / KiB); 186a72bd606SHelge Deller 187a72bd606SHelge Deller if (kernel_cmdline) { 188a72bd606SHelge Deller cpu[0]->env.gr[24] = 0x4000; 189a72bd606SHelge Deller pstrcpy_targphys("cmdline", cpu[0]->env.gr[24], 190a72bd606SHelge Deller TARGET_PAGE_SIZE, kernel_cmdline); 191a72bd606SHelge Deller } 192a72bd606SHelge Deller 193a72bd606SHelge Deller if (initrd_filename) { 194a72bd606SHelge Deller ram_addr_t initrd_base; 195f3839fdaSLi Zhijian int64_t initrd_size; 196a72bd606SHelge Deller 197a72bd606SHelge Deller initrd_size = get_image_size(initrd_filename); 198a72bd606SHelge Deller if (initrd_size < 0) { 199a72bd606SHelge Deller error_report("could not load initial ram disk '%s'", 200a72bd606SHelge Deller initrd_filename); 201a72bd606SHelge Deller exit(1); 202a72bd606SHelge Deller } 203a72bd606SHelge Deller 204a72bd606SHelge Deller /* Load the initrd image high in memory. 205a72bd606SHelge Deller Mirror the algorithm used by palo: 206a72bd606SHelge Deller (1) Due to sign-extension problems and PDC, 207a72bd606SHelge Deller put the initrd no higher than 1G. 208a72bd606SHelge Deller (2) Reserve 64k for stack. */ 209c108cc59SPhilippe Mathieu-Daudé initrd_base = MIN(ram_size, 1 * GiB); 210c108cc59SPhilippe Mathieu-Daudé initrd_base = initrd_base - 64 * KiB; 211a72bd606SHelge Deller initrd_base = (initrd_base - initrd_size) & TARGET_PAGE_MASK; 212a72bd606SHelge Deller 213a72bd606SHelge Deller if (initrd_base < kernel_high) { 214a72bd606SHelge Deller error_report("kernel and initial ram disk too large!"); 215a72bd606SHelge Deller exit(1); 216a72bd606SHelge Deller } 217a72bd606SHelge Deller 218a72bd606SHelge Deller load_image_targphys(initrd_filename, initrd_base, initrd_size); 219a72bd606SHelge Deller cpu[0]->env.gr[23] = initrd_base; 220a72bd606SHelge Deller cpu[0]->env.gr[22] = initrd_base + initrd_size; 221a72bd606SHelge Deller } 222a72bd606SHelge Deller } 223a72bd606SHelge Deller 224a72bd606SHelge Deller if (!kernel_entry) { 225a72bd606SHelge Deller /* When booting via firmware, tell firmware if we want interactive 226a72bd606SHelge Deller * mode (kernel_entry=1), and to boot from CD (gr[24]='d') 227a72bd606SHelge Deller * or hard disc * (gr[24]='c'). 228a72bd606SHelge Deller */ 229a72bd606SHelge Deller kernel_entry = boot_menu ? 1 : 0; 230a72bd606SHelge Deller cpu[0]->env.gr[24] = machine->boot_order[0]; 231a72bd606SHelge Deller } 232a72bd606SHelge Deller 233a72bd606SHelge Deller /* We jump to the firmware entry routine and pass the 234a72bd606SHelge Deller * various parameters in registers. After firmware initialization, 235a72bd606SHelge Deller * firmware will start the Linux kernel with ramdisk and cmdline. 236a72bd606SHelge Deller */ 237a72bd606SHelge Deller cpu[0]->env.gr[26] = ram_size; 238a72bd606SHelge Deller cpu[0]->env.gr[25] = kernel_entry; 239a72bd606SHelge Deller 240a72bd606SHelge Deller /* tell firmware how many SMP CPUs to present in inventory table */ 241a72bd606SHelge Deller cpu[0]->env.gr[21] = smp_cpus; 242a72bd606SHelge Deller } 243a72bd606SHelge Deller 244a0628599SLike Xu static void hppa_machine_reset(MachineState *ms) 245a72bd606SHelge Deller { 246*33decbd2SLike Xu unsigned int smp_cpus = ms->smp.cpus; 247a72bd606SHelge Deller int i; 248a72bd606SHelge Deller 249a72bd606SHelge Deller qemu_devices_reset(); 250a72bd606SHelge Deller 251a72bd606SHelge Deller /* Start all CPUs at the firmware entry point. 252a72bd606SHelge Deller * Monarch CPU will initialize firmware, secondary CPUs 253a72bd606SHelge Deller * will enter a small idle look and wait for rendevouz. */ 254a72bd606SHelge Deller for (i = 0; i < smp_cpus; i++) { 255a72bd606SHelge Deller cpu_set_pc(CPU(cpu[i]), firmware_entry); 256a72bd606SHelge Deller cpu[i]->env.gr[5] = CPU_HPA + i * 0x1000; 257a72bd606SHelge Deller } 258a72bd606SHelge Deller 259a72bd606SHelge Deller /* already initialized by machine_hppa_init()? */ 260a72bd606SHelge Deller if (cpu[0]->env.gr[26] == ram_size) { 261a72bd606SHelge Deller return; 262a72bd606SHelge Deller } 263a72bd606SHelge Deller 264a72bd606SHelge Deller cpu[0]->env.gr[26] = ram_size; 265a72bd606SHelge Deller cpu[0]->env.gr[25] = 0; /* no firmware boot menu */ 266a72bd606SHelge Deller cpu[0]->env.gr[24] = 'c'; 267a72bd606SHelge Deller /* gr22/gr23 unused, no initrd while reboot. */ 268a72bd606SHelge Deller cpu[0]->env.gr[21] = smp_cpus; 269a72bd606SHelge Deller } 270a72bd606SHelge Deller 271a72bd606SHelge Deller 272813dff13SHelge Deller static void machine_hppa_machine_init(MachineClass *mc) 273813dff13SHelge Deller { 274813dff13SHelge Deller mc->desc = "HPPA generic machine"; 275a72bd606SHelge Deller mc->default_cpu_type = TYPE_HPPA_CPU; 276813dff13SHelge Deller mc->init = machine_hppa_init; 277a72bd606SHelge Deller mc->reset = hppa_machine_reset; 278813dff13SHelge Deller mc->block_default_type = IF_SCSI; 279a72bd606SHelge Deller mc->max_cpus = HPPA_MAX_CPUS; 280a72bd606SHelge Deller mc->default_cpus = 1; 281813dff13SHelge Deller mc->is_default = 1; 282d23b6caaSPhilippe Mathieu-Daudé mc->default_ram_size = 512 * MiB; 283813dff13SHelge Deller mc->default_boot_order = "cd"; 284813dff13SHelge Deller } 285813dff13SHelge Deller 286813dff13SHelge Deller DEFINE_MACHINE("hppa", machine_hppa_machine_init) 287