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