xref: /openbmc/qemu/hw/hppa/machine.c (revision deaa5d3b4e8eccf4010c9099b99c267ff1f2223e)
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/datadir.h"
8 #include "cpu.h"
9 #include "elf.h"
10 #include "hw/loader.h"
11 #include "qemu/error-report.h"
12 #include "sysemu/reset.h"
13 #include "sysemu/sysemu.h"
14 #include "sysemu/runstate.h"
15 #include "hw/rtc/mc146818rtc.h"
16 #include "hw/timer/i8254.h"
17 #include "hw/char/serial.h"
18 #include "hw/char/parallel.h"
19 #include "hw/input/lasips2.h"
20 #include "hw/net/lasi_82596.h"
21 #include "hw/nmi.h"
22 #include "hw/pci-host/dino.h"
23 #include "lasi.h"
24 #include "hppa_sys.h"
25 #include "qemu/units.h"
26 #include "qapi/error.h"
27 #include "net/net.h"
28 #include "qemu/log.h"
29 #include "net/net.h"
30 
31 #define MAX_IDE_BUS 2
32 
33 #define MIN_SEABIOS_HPPA_VERSION 1 /* require at least this fw version */
34 
35 #define HPA_POWER_BUTTON (FIRMWARE_END - 0x10)
36 
37 static void hppa_powerdown_req(Notifier *n, void *opaque)
38 {
39     hwaddr soft_power_reg = HPA_POWER_BUTTON;
40     uint32_t val;
41 
42     val = ldl_be_phys(&address_space_memory, soft_power_reg);
43     if ((val >> 8) == 0) {
44         /* immediately shut down when under hardware control */
45         qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
46         return;
47     }
48 
49     /* clear bit 31 to indicate that the power switch was pressed. */
50     val &= ~1;
51     stl_be_phys(&address_space_memory, soft_power_reg, val);
52 }
53 
54 static Notifier hppa_system_powerdown_notifier = {
55     .notify = hppa_powerdown_req
56 };
57 
58 
59 static ISABus *hppa_isa_bus(void)
60 {
61     ISABus *isa_bus;
62     qemu_irq *isa_irqs;
63     MemoryRegion *isa_region;
64 
65     isa_region = g_new(MemoryRegion, 1);
66     memory_region_init_io(isa_region, NULL, &hppa_pci_ignore_ops,
67                           NULL, "isa-io", 0x800);
68     memory_region_add_subregion(get_system_memory(), IDE_HPA,
69                                 isa_region);
70 
71     isa_bus = isa_bus_new(NULL, get_system_memory(), isa_region,
72                           &error_abort);
73     isa_irqs = i8259_init(isa_bus,
74                           /* qemu_allocate_irq(dino_set_isa_irq, s, 0)); */
75                           NULL);
76     isa_bus_irqs(isa_bus, isa_irqs);
77 
78     return isa_bus;
79 }
80 
81 static uint64_t cpu_hppa_to_phys(void *opaque, uint64_t addr)
82 {
83     addr &= (0x10000000 - 1);
84     return addr;
85 }
86 
87 static HPPACPU *cpu[HPPA_MAX_CPUS];
88 static uint64_t firmware_entry;
89 
90 static void fw_cfg_boot_set(void *opaque, const char *boot_device,
91                             Error **errp)
92 {
93     fw_cfg_modify_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
94 }
95 
96 static FWCfgState *create_fw_cfg(MachineState *ms)
97 {
98     FWCfgState *fw_cfg;
99     uint64_t val;
100 
101     fw_cfg = fw_cfg_init_mem(FW_CFG_IO_BASE, FW_CFG_IO_BASE + 4);
102     fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, ms->smp.cpus);
103     fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, HPPA_MAX_CPUS);
104     fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, ms->ram_size);
105 
106     val = cpu_to_le64(MIN_SEABIOS_HPPA_VERSION);
107     fw_cfg_add_file(fw_cfg, "/etc/firmware-min-version",
108                     g_memdup(&val, sizeof(val)), sizeof(val));
109 
110     val = cpu_to_le64(HPPA_TLB_ENTRIES);
111     fw_cfg_add_file(fw_cfg, "/etc/cpu/tlb_entries",
112                     g_memdup(&val, sizeof(val)), sizeof(val));
113 
114     val = cpu_to_le64(HPPA_BTLB_ENTRIES);
115     fw_cfg_add_file(fw_cfg, "/etc/cpu/btlb_entries",
116                     g_memdup(&val, sizeof(val)), sizeof(val));
117 
118     val = cpu_to_le64(HPA_POWER_BUTTON);
119     fw_cfg_add_file(fw_cfg, "/etc/power-button-addr",
120                     g_memdup(&val, sizeof(val)), sizeof(val));
121 
122     fw_cfg_add_i16(fw_cfg, FW_CFG_BOOT_DEVICE, ms->boot_order[0]);
123     qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);
124 
125     return fw_cfg;
126 }
127 
128 static DinoState *dino_init(MemoryRegion *addr_space)
129 {
130     DeviceState *dev;
131 
132     dev = qdev_new(TYPE_DINO_PCI_HOST_BRIDGE);
133     object_property_set_link(OBJECT(dev), "memory-as", OBJECT(addr_space),
134                              &error_fatal);
135     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
136 
137     return DINO_PCI_HOST_BRIDGE(dev);
138 }
139 
140 static void machine_hppa_init(MachineState *machine)
141 {
142     const char *kernel_filename = machine->kernel_filename;
143     const char *kernel_cmdline = machine->kernel_cmdline;
144     const char *initrd_filename = machine->initrd_filename;
145     DeviceState *dev, *dino_dev, *lasi_dev;
146     PCIBus *pci_bus;
147     ISABus *isa_bus;
148     char *firmware_filename;
149     uint64_t firmware_low, firmware_high;
150     long size;
151     uint64_t kernel_entry = 0, kernel_low, kernel_high;
152     MemoryRegion *addr_space = get_system_memory();
153     MemoryRegion *rom_region;
154     MemoryRegion *cpu_region;
155     long i;
156     unsigned int smp_cpus = machine->smp.cpus;
157     SysBusDevice *s;
158 
159     /* Create CPUs.  */
160     for (i = 0; i < smp_cpus; i++) {
161         char *name = g_strdup_printf("cpu%ld-io-eir", i);
162         cpu[i] = HPPA_CPU(cpu_create(machine->cpu_type));
163 
164         cpu_region = g_new(MemoryRegion, 1);
165         memory_region_init_io(cpu_region, OBJECT(cpu[i]), &hppa_io_eir_ops,
166                               cpu[i], name, 4);
167         memory_region_add_subregion(addr_space, CPU_HPA + i * 0x1000,
168                                     cpu_region);
169         g_free(name);
170     }
171 
172     /* Main memory region. */
173     if (machine->ram_size > 3 * GiB) {
174         error_report("RAM size is currently restricted to 3GB");
175         exit(EXIT_FAILURE);
176     }
177     memory_region_add_subregion_overlap(addr_space, 0, machine->ram, -1);
178 
179 
180     /* Init Lasi chip */
181     lasi_dev = DEVICE(lasi_initfn());
182     memory_region_add_subregion(addr_space, LASI_HPA,
183                                 sysbus_mmio_get_region(
184                                     SYS_BUS_DEVICE(lasi_dev), 0));
185 
186     /* Init Dino (PCI host bus chip).  */
187     dino_dev = DEVICE(dino_init(addr_space));
188     memory_region_add_subregion(addr_space, DINO_HPA,
189                                 sysbus_mmio_get_region(
190                                     SYS_BUS_DEVICE(dino_dev), 0));
191     pci_bus = PCI_BUS(qdev_get_child_bus(dino_dev, "pci"));
192     assert(pci_bus);
193 
194     /* Create ISA bus. */
195     isa_bus = hppa_isa_bus();
196     assert(isa_bus);
197 
198     /* Realtime clock, used by firmware for PDC_TOD call. */
199     mc146818_rtc_init(isa_bus, 2000, NULL);
200 
201     /* Serial code setup.  */
202     if (serial_hd(0)) {
203         uint32_t addr = DINO_UART_HPA + 0x800;
204         serial_mm_init(addr_space, addr, 0,
205                        qdev_get_gpio_in(dino_dev, DINO_IRQ_RS232INT),
206                        115200, serial_hd(0), DEVICE_BIG_ENDIAN);
207     }
208 
209     if (serial_hd(1)) {
210         /* Serial port */
211         serial_mm_init(addr_space, LASI_UART_HPA + 0x800, 0,
212                 qdev_get_gpio_in(lasi_dev, LASI_IRQ_UART_HPA), 8000000 / 16,
213                 serial_hd(1), DEVICE_BIG_ENDIAN);
214     }
215 
216     /* Parallel port */
217     parallel_mm_init(addr_space, LASI_LPT_HPA + 0x800, 0,
218                      qdev_get_gpio_in(lasi_dev, LASI_IRQ_LAN_HPA),
219                      parallel_hds[0]);
220 
221     /* fw_cfg configuration interface */
222     create_fw_cfg(machine);
223 
224     /* SCSI disk setup. */
225     dev = DEVICE(pci_create_simple(pci_bus, -1, "lsi53c895a"));
226     lsi53c8xx_handle_legacy_cmdline(dev);
227 
228     /* Graphics setup. */
229     if (machine->enable_graphics && vga_interface_type != VGA_NONE) {
230         dev = qdev_new("artist");
231         s = SYS_BUS_DEVICE(dev);
232         sysbus_realize_and_unref(s, &error_fatal);
233         sysbus_mmio_map(s, 0, LASI_GFX_HPA);
234         sysbus_mmio_map(s, 1, ARTIST_FB_ADDR);
235     }
236 
237     /* Network setup. */
238     if (enable_lasi_lan()) {
239         lasi_82596_init(addr_space, LASI_LAN_HPA,
240                         qdev_get_gpio_in(lasi_dev, LASI_IRQ_LAN_HPA));
241     }
242 
243     for (i = 0; i < nb_nics; i++) {
244         if (!enable_lasi_lan()) {
245             pci_nic_init_nofail(&nd_table[i], pci_bus, "tulip", NULL);
246         }
247     }
248 
249     /* PS/2 Keyboard/Mouse */
250     lasips2_init(addr_space, LASI_PS2KBD_HPA,
251                  qdev_get_gpio_in(lasi_dev, LASI_IRQ_PS2KBD_HPA));
252 
253     /* register power switch emulation */
254     qemu_register_powerdown_notifier(&hppa_system_powerdown_notifier);
255 
256     /* Load firmware.  Given that this is not "real" firmware,
257        but one explicitly written for the emulation, we might as
258        well load it directly from an ELF image.  */
259     firmware_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
260                                        machine->firmware ?: "hppa-firmware.img");
261     if (firmware_filename == NULL) {
262         error_report("no firmware provided");
263         exit(1);
264     }
265 
266     size = load_elf(firmware_filename, NULL, NULL, NULL,
267                     &firmware_entry, &firmware_low, &firmware_high, NULL,
268                     true, EM_PARISC, 0, 0);
269 
270     /* Unfortunately, load_elf sign-extends reading elf32.  */
271     firmware_entry = (target_ureg)firmware_entry;
272     firmware_low = (target_ureg)firmware_low;
273     firmware_high = (target_ureg)firmware_high;
274 
275     if (size < 0) {
276         error_report("could not load firmware '%s'", firmware_filename);
277         exit(1);
278     }
279     qemu_log_mask(CPU_LOG_PAGE, "Firmware loaded at 0x%08" PRIx64
280                   "-0x%08" PRIx64 ", entry at 0x%08" PRIx64 ".\n",
281                   firmware_low, firmware_high, firmware_entry);
282     if (firmware_low < FIRMWARE_START || firmware_high >= FIRMWARE_END) {
283         error_report("Firmware overlaps with memory or IO space");
284         exit(1);
285     }
286     g_free(firmware_filename);
287 
288     rom_region = g_new(MemoryRegion, 1);
289     memory_region_init_ram(rom_region, NULL, "firmware",
290                            (FIRMWARE_END - FIRMWARE_START), &error_fatal);
291     memory_region_add_subregion(addr_space, FIRMWARE_START, rom_region);
292 
293     /* Load kernel */
294     if (kernel_filename) {
295         size = load_elf(kernel_filename, NULL, &cpu_hppa_to_phys,
296                         NULL, &kernel_entry, &kernel_low, &kernel_high, NULL,
297                         true, EM_PARISC, 0, 0);
298 
299         /* Unfortunately, load_elf sign-extends reading elf32.  */
300         kernel_entry = (target_ureg) cpu_hppa_to_phys(NULL, kernel_entry);
301         kernel_low = (target_ureg)kernel_low;
302         kernel_high = (target_ureg)kernel_high;
303 
304         if (size < 0) {
305             error_report("could not load kernel '%s'", kernel_filename);
306             exit(1);
307         }
308         qemu_log_mask(CPU_LOG_PAGE, "Kernel loaded at 0x%08" PRIx64
309                       "-0x%08" PRIx64 ", entry at 0x%08" PRIx64
310                       ", size %" PRIu64 " kB\n",
311                       kernel_low, kernel_high, kernel_entry, size / KiB);
312 
313         if (kernel_cmdline) {
314             cpu[0]->env.gr[24] = 0x4000;
315             pstrcpy_targphys("cmdline", cpu[0]->env.gr[24],
316                              TARGET_PAGE_SIZE, kernel_cmdline);
317         }
318 
319         if (initrd_filename) {
320             ram_addr_t initrd_base;
321             int64_t initrd_size;
322 
323             initrd_size = get_image_size(initrd_filename);
324             if (initrd_size < 0) {
325                 error_report("could not load initial ram disk '%s'",
326                              initrd_filename);
327                 exit(1);
328             }
329 
330             /* Load the initrd image high in memory.
331                Mirror the algorithm used by palo:
332                (1) Due to sign-extension problems and PDC,
333                put the initrd no higher than 1G.
334                (2) Reserve 64k for stack.  */
335             initrd_base = MIN(machine->ram_size, 1 * GiB);
336             initrd_base = initrd_base - 64 * KiB;
337             initrd_base = (initrd_base - initrd_size) & TARGET_PAGE_MASK;
338 
339             if (initrd_base < kernel_high) {
340                 error_report("kernel and initial ram disk too large!");
341                 exit(1);
342             }
343 
344             load_image_targphys(initrd_filename, initrd_base, initrd_size);
345             cpu[0]->env.gr[23] = initrd_base;
346             cpu[0]->env.gr[22] = initrd_base + initrd_size;
347         }
348     }
349 
350     if (!kernel_entry) {
351         /* When booting via firmware, tell firmware if we want interactive
352          * mode (kernel_entry=1), and to boot from CD (gr[24]='d')
353          * or hard disc * (gr[24]='c').
354          */
355         kernel_entry = boot_menu ? 1 : 0;
356         cpu[0]->env.gr[24] = machine->boot_order[0];
357     }
358 
359     /* We jump to the firmware entry routine and pass the
360      * various parameters in registers. After firmware initialization,
361      * firmware will start the Linux kernel with ramdisk and cmdline.
362      */
363     cpu[0]->env.gr[26] = machine->ram_size;
364     cpu[0]->env.gr[25] = kernel_entry;
365 
366     /* tell firmware how many SMP CPUs to present in inventory table */
367     cpu[0]->env.gr[21] = smp_cpus;
368 
369     /* tell firmware fw_cfg port */
370     cpu[0]->env.gr[19] = FW_CFG_IO_BASE;
371 }
372 
373 static void hppa_machine_reset(MachineState *ms)
374 {
375     unsigned int smp_cpus = ms->smp.cpus;
376     int i;
377 
378     qemu_devices_reset();
379 
380     /* Start all CPUs at the firmware entry point.
381      *  Monarch CPU will initialize firmware, secondary CPUs
382      *  will enter a small idle look and wait for rendevouz. */
383     for (i = 0; i < smp_cpus; i++) {
384         cpu_set_pc(CPU(cpu[i]), firmware_entry);
385         cpu[i]->env.gr[5] = CPU_HPA + i * 0x1000;
386     }
387 
388     /* already initialized by machine_hppa_init()? */
389     if (cpu[0]->env.gr[26] == ms->ram_size) {
390         return;
391     }
392 
393     cpu[0]->env.gr[26] = ms->ram_size;
394     cpu[0]->env.gr[25] = 0; /* no firmware boot menu */
395     cpu[0]->env.gr[24] = 'c';
396     /* gr22/gr23 unused, no initrd while reboot. */
397     cpu[0]->env.gr[21] = smp_cpus;
398     /* tell firmware fw_cfg port */
399     cpu[0]->env.gr[19] = FW_CFG_IO_BASE;
400 }
401 
402 static void hppa_nmi(NMIState *n, int cpu_index, Error **errp)
403 {
404     CPUState *cs;
405 
406     CPU_FOREACH(cs) {
407         cpu_interrupt(cs, CPU_INTERRUPT_NMI);
408     }
409 }
410 
411 static void machine_hppa_machine_init(MachineClass *mc)
412 {
413     mc->desc = "HPPA B160L machine";
414     mc->default_cpu_type = TYPE_HPPA_CPU;
415     mc->init = machine_hppa_init;
416     mc->reset = hppa_machine_reset;
417     mc->block_default_type = IF_SCSI;
418     mc->max_cpus = HPPA_MAX_CPUS;
419     mc->default_cpus = 1;
420     mc->is_default = true;
421     mc->default_ram_size = 512 * MiB;
422     mc->default_boot_order = "cd";
423     mc->default_ram_id = "ram";
424 }
425 
426 static void machine_hppa_machine_init_class_init(ObjectClass *oc, void *data)
427 {
428     MachineClass *mc = MACHINE_CLASS(oc);
429     machine_hppa_machine_init(mc);
430 
431     NMIClass *nc = NMI_CLASS(oc);
432     nc->nmi_monitor_handler = hppa_nmi;
433 }
434 
435 static const TypeInfo machine_hppa_machine_init_typeinfo = {
436     .name = ("hppa" "-machine"),
437     .parent = "machine",
438     .class_init = machine_hppa_machine_init_class_init,
439     .interfaces = (InterfaceInfo[]) {
440         { TYPE_NMI },
441         { }
442     },
443 };
444 
445 static void machine_hppa_machine_init_register_types(void)
446 {
447     type_register_static(&machine_hppa_machine_init_typeinfo);
448 }
449 
450 type_init(machine_hppa_machine_init_register_types)
451