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