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; 75a72bd606SHelge Deller 76a72bd606SHelge Deller ram_size = machine->ram_size; 77a72bd606SHelge Deller 78a72bd606SHelge Deller /* Create CPUs. */ 79a72bd606SHelge Deller for (i = 0; i < smp_cpus; i++) { 80a72bd606SHelge Deller cpu[i] = HPPA_CPU(cpu_create(machine->cpu_type)); 81a72bd606SHelge Deller 82a72bd606SHelge Deller cpu_region = g_new(MemoryRegion, 1); 83a72bd606SHelge Deller memory_region_init_io(cpu_region, OBJECT(cpu[i]), &hppa_io_eir_ops, 84a72bd606SHelge Deller cpu[i], g_strdup_printf("cpu%ld-io-eir", i), 4); 85a72bd606SHelge Deller memory_region_add_subregion(addr_space, CPU_HPA + i * 0x1000, 86a72bd606SHelge Deller cpu_region); 87813dff13SHelge Deller } 88813dff13SHelge Deller 89a72bd606SHelge Deller /* Limit main memory. */ 90a72bd606SHelge Deller if (ram_size > FIRMWARE_START) { 91a72bd606SHelge Deller machine->ram_size = ram_size = FIRMWARE_START; 92a72bd606SHelge Deller } 93a72bd606SHelge Deller 94a72bd606SHelge Deller /* Main memory region. */ 95a72bd606SHelge Deller ram_region = g_new(MemoryRegion, 1); 96a72bd606SHelge Deller memory_region_allocate_system_memory(ram_region, OBJECT(machine), 97a72bd606SHelge Deller "ram", ram_size); 98a72bd606SHelge Deller memory_region_add_subregion(addr_space, 0, ram_region); 99a72bd606SHelge Deller 100a72bd606SHelge Deller /* Init Dino (PCI host bus chip). */ 101a72bd606SHelge Deller pci_bus = dino_init(addr_space, &rtc_irq, &serial_irq); 102a72bd606SHelge Deller assert(pci_bus); 103a72bd606SHelge Deller 104a72bd606SHelge Deller /* Create ISA bus. */ 105a72bd606SHelge Deller isa_bus = hppa_isa_bus(); 106a72bd606SHelge Deller assert(isa_bus); 107a72bd606SHelge Deller 108a72bd606SHelge Deller /* Realtime clock, used by firmware for PDC_TOD call. */ 109a72bd606SHelge Deller mc146818_rtc_init(isa_bus, 2000, rtc_irq); 110a72bd606SHelge Deller 111a72bd606SHelge Deller /* Serial code setup. */ 1129bca0edbSPeter Maydell if (serial_hd(0)) { 113a72bd606SHelge Deller uint32_t addr = DINO_UART_HPA + 0x800; 114a72bd606SHelge Deller serial_mm_init(addr_space, addr, 0, serial_irq, 1159bca0edbSPeter Maydell 115200, serial_hd(0), DEVICE_BIG_ENDIAN); 116a72bd606SHelge Deller } 117a72bd606SHelge Deller 118a72bd606SHelge Deller /* SCSI disk setup. */ 119877eb21dSMark Cave-Ayland dev = DEVICE(pci_create_simple(pci_bus, -1, "lsi53c895a")); 120877eb21dSMark Cave-Ayland lsi53c8xx_handle_legacy_cmdline(dev); 121a72bd606SHelge Deller 122a72bd606SHelge Deller /* Network setup. e1000 is good enough, failing Tulip support. */ 123a72bd606SHelge Deller for (i = 0; i < nb_nics; i++) { 124a72bd606SHelge Deller pci_nic_init_nofail(&nd_table[i], pci_bus, "e1000", NULL); 125a72bd606SHelge Deller } 126a72bd606SHelge Deller 127a72bd606SHelge Deller /* Load firmware. Given that this is not "real" firmware, 128a72bd606SHelge Deller but one explicitly written for the emulation, we might as 129a72bd606SHelge Deller well load it directly from an ELF image. */ 130a72bd606SHelge Deller firmware_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, 131a72bd606SHelge Deller bios_name ? bios_name : 132a72bd606SHelge Deller "hppa-firmware.img"); 133a72bd606SHelge Deller if (firmware_filename == NULL) { 134a72bd606SHelge Deller error_report("no firmware provided"); 135a72bd606SHelge Deller exit(1); 136a72bd606SHelge Deller } 137a72bd606SHelge Deller 138a72bd606SHelge Deller size = load_elf(firmware_filename, NULL, 139a72bd606SHelge Deller NULL, &firmware_entry, &firmware_low, &firmware_high, 140a72bd606SHelge Deller true, EM_PARISC, 0, 0); 141a72bd606SHelge Deller 142a72bd606SHelge Deller /* Unfortunately, load_elf sign-extends reading elf32. */ 143a72bd606SHelge Deller firmware_entry = (target_ureg)firmware_entry; 144a72bd606SHelge Deller firmware_low = (target_ureg)firmware_low; 145a72bd606SHelge Deller firmware_high = (target_ureg)firmware_high; 146a72bd606SHelge Deller 147a72bd606SHelge Deller if (size < 0) { 148a72bd606SHelge Deller error_report("could not load firmware '%s'", firmware_filename); 149a72bd606SHelge Deller exit(1); 150a72bd606SHelge Deller } 151691cbbadSRichard Henderson qemu_log_mask(CPU_LOG_PAGE, "Firmware loaded at 0x%08" PRIx64 152691cbbadSRichard Henderson "-0x%08" PRIx64 ", entry at 0x%08" PRIx64 ".\n", 153a72bd606SHelge Deller firmware_low, firmware_high, firmware_entry); 154a72bd606SHelge Deller if (firmware_low < ram_size || firmware_high >= FIRMWARE_END) { 155a72bd606SHelge Deller error_report("Firmware overlaps with memory or IO space"); 156a72bd606SHelge Deller exit(1); 157a72bd606SHelge Deller } 158a72bd606SHelge Deller g_free(firmware_filename); 159a72bd606SHelge Deller 160a72bd606SHelge Deller rom_region = g_new(MemoryRegion, 1); 161a72bd606SHelge Deller memory_region_allocate_system_memory(rom_region, OBJECT(machine), 162a72bd606SHelge Deller "firmware", 163a72bd606SHelge Deller (FIRMWARE_END - FIRMWARE_START)); 164a72bd606SHelge Deller memory_region_add_subregion(addr_space, FIRMWARE_START, rom_region); 165a72bd606SHelge Deller 166a72bd606SHelge Deller /* Load kernel */ 167a72bd606SHelge Deller if (kernel_filename) { 168a72bd606SHelge Deller size = load_elf(kernel_filename, &cpu_hppa_to_phys, 169a72bd606SHelge Deller NULL, &kernel_entry, &kernel_low, &kernel_high, 170a72bd606SHelge Deller true, EM_PARISC, 0, 0); 171a72bd606SHelge Deller 172a72bd606SHelge Deller /* Unfortunately, load_elf sign-extends reading elf32. */ 173a72bd606SHelge Deller kernel_entry = (target_ureg) cpu_hppa_to_phys(NULL, kernel_entry); 174a72bd606SHelge Deller kernel_low = (target_ureg)kernel_low; 175a72bd606SHelge Deller kernel_high = (target_ureg)kernel_high; 176a72bd606SHelge Deller 177a72bd606SHelge Deller if (size < 0) { 178a72bd606SHelge Deller error_report("could not load kernel '%s'", kernel_filename); 179a72bd606SHelge Deller exit(1); 180a72bd606SHelge Deller } 181691cbbadSRichard Henderson qemu_log_mask(CPU_LOG_PAGE, "Kernel loaded at 0x%08" PRIx64 182691cbbadSRichard Henderson "-0x%08" PRIx64 ", entry at 0x%08" PRIx64 183c108cc59SPhilippe Mathieu-Daudé ", size %" PRIu64 " kB\n", 184c108cc59SPhilippe Mathieu-Daudé kernel_low, kernel_high, kernel_entry, size / KiB); 185a72bd606SHelge Deller 186a72bd606SHelge Deller if (kernel_cmdline) { 187a72bd606SHelge Deller cpu[0]->env.gr[24] = 0x4000; 188a72bd606SHelge Deller pstrcpy_targphys("cmdline", cpu[0]->env.gr[24], 189a72bd606SHelge Deller TARGET_PAGE_SIZE, kernel_cmdline); 190a72bd606SHelge Deller } 191a72bd606SHelge Deller 192a72bd606SHelge Deller if (initrd_filename) { 193a72bd606SHelge Deller ram_addr_t initrd_base; 194*f3839fdaSLi Zhijian int64_t initrd_size; 195a72bd606SHelge Deller 196a72bd606SHelge Deller initrd_size = get_image_size(initrd_filename); 197a72bd606SHelge Deller if (initrd_size < 0) { 198a72bd606SHelge Deller error_report("could not load initial ram disk '%s'", 199a72bd606SHelge Deller initrd_filename); 200a72bd606SHelge Deller exit(1); 201a72bd606SHelge Deller } 202a72bd606SHelge Deller 203a72bd606SHelge Deller /* Load the initrd image high in memory. 204a72bd606SHelge Deller Mirror the algorithm used by palo: 205a72bd606SHelge Deller (1) Due to sign-extension problems and PDC, 206a72bd606SHelge Deller put the initrd no higher than 1G. 207a72bd606SHelge Deller (2) Reserve 64k for stack. */ 208c108cc59SPhilippe Mathieu-Daudé initrd_base = MIN(ram_size, 1 * GiB); 209c108cc59SPhilippe Mathieu-Daudé initrd_base = initrd_base - 64 * KiB; 210a72bd606SHelge Deller initrd_base = (initrd_base - initrd_size) & TARGET_PAGE_MASK; 211a72bd606SHelge Deller 212a72bd606SHelge Deller if (initrd_base < kernel_high) { 213a72bd606SHelge Deller error_report("kernel and initial ram disk too large!"); 214a72bd606SHelge Deller exit(1); 215a72bd606SHelge Deller } 216a72bd606SHelge Deller 217a72bd606SHelge Deller load_image_targphys(initrd_filename, initrd_base, initrd_size); 218a72bd606SHelge Deller cpu[0]->env.gr[23] = initrd_base; 219a72bd606SHelge Deller cpu[0]->env.gr[22] = initrd_base + initrd_size; 220a72bd606SHelge Deller } 221a72bd606SHelge Deller } 222a72bd606SHelge Deller 223a72bd606SHelge Deller if (!kernel_entry) { 224a72bd606SHelge Deller /* When booting via firmware, tell firmware if we want interactive 225a72bd606SHelge Deller * mode (kernel_entry=1), and to boot from CD (gr[24]='d') 226a72bd606SHelge Deller * or hard disc * (gr[24]='c'). 227a72bd606SHelge Deller */ 228a72bd606SHelge Deller kernel_entry = boot_menu ? 1 : 0; 229a72bd606SHelge Deller cpu[0]->env.gr[24] = machine->boot_order[0]; 230a72bd606SHelge Deller } 231a72bd606SHelge Deller 232a72bd606SHelge Deller /* We jump to the firmware entry routine and pass the 233a72bd606SHelge Deller * various parameters in registers. After firmware initialization, 234a72bd606SHelge Deller * firmware will start the Linux kernel with ramdisk and cmdline. 235a72bd606SHelge Deller */ 236a72bd606SHelge Deller cpu[0]->env.gr[26] = ram_size; 237a72bd606SHelge Deller cpu[0]->env.gr[25] = kernel_entry; 238a72bd606SHelge Deller 239a72bd606SHelge Deller /* tell firmware how many SMP CPUs to present in inventory table */ 240a72bd606SHelge Deller cpu[0]->env.gr[21] = smp_cpus; 241a72bd606SHelge Deller } 242a72bd606SHelge Deller 243a72bd606SHelge Deller static void hppa_machine_reset(void) 244a72bd606SHelge Deller { 245a72bd606SHelge Deller int i; 246a72bd606SHelge Deller 247a72bd606SHelge Deller qemu_devices_reset(); 248a72bd606SHelge Deller 249a72bd606SHelge Deller /* Start all CPUs at the firmware entry point. 250a72bd606SHelge Deller * Monarch CPU will initialize firmware, secondary CPUs 251a72bd606SHelge Deller * will enter a small idle look and wait for rendevouz. */ 252a72bd606SHelge Deller for (i = 0; i < smp_cpus; i++) { 253a72bd606SHelge Deller cpu_set_pc(CPU(cpu[i]), firmware_entry); 254a72bd606SHelge Deller cpu[i]->env.gr[5] = CPU_HPA + i * 0x1000; 255a72bd606SHelge Deller } 256a72bd606SHelge Deller 257a72bd606SHelge Deller /* already initialized by machine_hppa_init()? */ 258a72bd606SHelge Deller if (cpu[0]->env.gr[26] == ram_size) { 259a72bd606SHelge Deller return; 260a72bd606SHelge Deller } 261a72bd606SHelge Deller 262a72bd606SHelge Deller cpu[0]->env.gr[26] = ram_size; 263a72bd606SHelge Deller cpu[0]->env.gr[25] = 0; /* no firmware boot menu */ 264a72bd606SHelge Deller cpu[0]->env.gr[24] = 'c'; 265a72bd606SHelge Deller /* gr22/gr23 unused, no initrd while reboot. */ 266a72bd606SHelge Deller cpu[0]->env.gr[21] = smp_cpus; 267a72bd606SHelge Deller } 268a72bd606SHelge Deller 269a72bd606SHelge Deller 270813dff13SHelge Deller static void machine_hppa_machine_init(MachineClass *mc) 271813dff13SHelge Deller { 272813dff13SHelge Deller mc->desc = "HPPA generic machine"; 273a72bd606SHelge Deller mc->default_cpu_type = TYPE_HPPA_CPU; 274813dff13SHelge Deller mc->init = machine_hppa_init; 275a72bd606SHelge Deller mc->reset = hppa_machine_reset; 276813dff13SHelge Deller mc->block_default_type = IF_SCSI; 277a72bd606SHelge Deller mc->max_cpus = HPPA_MAX_CPUS; 278a72bd606SHelge Deller mc->default_cpus = 1; 279813dff13SHelge Deller mc->is_default = 1; 280d23b6caaSPhilippe Mathieu-Daudé mc->default_ram_size = 512 * MiB; 281813dff13SHelge Deller mc->default_boot_order = "cd"; 282813dff13SHelge Deller } 283813dff13SHelge Deller 284813dff13SHelge Deller DEFINE_MACHINE("hppa", machine_hppa_machine_init) 285