xref: /openbmc/qemu/hw/hppa/machine.c (revision 7067887ea18622e381f09b998b67095f51e4aa49)
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"
72c65db5eSPaolo Bonzini #include "qemu/datadir.h"
8813dff13SHelge Deller #include "cpu.h"
9813dff13SHelge Deller #include "elf.h"
10813dff13SHelge Deller #include "hw/loader.h"
11813dff13SHelge Deller #include "qemu/error-report.h"
1271e8a915SMarkus Armbruster #include "sysemu/reset.h"
13813dff13SHelge Deller #include "sysemu/sysemu.h"
14b28c4a64SHelge Deller #include "sysemu/runstate.h"
15bcdb9064SPhilippe Mathieu-Daudé #include "hw/rtc/mc146818rtc.h"
16813dff13SHelge Deller #include "hw/timer/i8254.h"
17813dff13SHelge Deller #include "hw/char/serial.h"
189701e569SMark Cave-Ayland #include "hw/char/parallel.h"
19134ba73fSMark Cave-Ayland #include "hw/intc/i8259.h"
20d26c575cSMark Cave-Ayland #include "hw/input/lasips2.h"
21376b8519SHelge Deller #include "hw/net/lasi_82596.h"
224a4554c6SHelge Deller #include "hw/nmi.h"
23134ba73fSMark Cave-Ayland #include "hw/pci/pci.h"
240db9350eSMark Cave-Ayland #include "hw/pci-host/dino.h"
2545f569a1SMark Cave-Ayland #include "hw/misc/lasi.h"
26148da670SMark Cave-Ayland #include "hppa_hardware.h"
27c108cc59SPhilippe Mathieu-Daudé #include "qemu/units.h"
28813dff13SHelge Deller #include "qapi/error.h"
29852c27e2SPaolo Bonzini #include "net/net.h"
30691cbbadSRichard Henderson #include "qemu/log.h"
31813dff13SHelge Deller 
325079892dSHelge Deller #define MIN_SEABIOS_HPPA_VERSION 6 /* require at least this fw version */
3328b71a2eSHelge Deller 
34b28c4a64SHelge Deller #define HPA_POWER_BUTTON (FIRMWARE_END - 0x10)
35b28c4a64SHelge Deller 
36932befaaSMark Cave-Ayland #define enable_lasi_lan()       0
37932befaaSMark Cave-Ayland 
38932befaaSMark Cave-Ayland 
39b28c4a64SHelge Deller static void hppa_powerdown_req(Notifier *n, void *opaque)
40b28c4a64SHelge Deller {
41b28c4a64SHelge Deller     hwaddr soft_power_reg = HPA_POWER_BUTTON;
42b28c4a64SHelge Deller     uint32_t val;
43b28c4a64SHelge Deller 
44b28c4a64SHelge Deller     val = ldl_be_phys(&address_space_memory, soft_power_reg);
45b28c4a64SHelge Deller     if ((val >> 8) == 0) {
46b28c4a64SHelge Deller         /* immediately shut down when under hardware control */
47b28c4a64SHelge Deller         qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
48b28c4a64SHelge Deller         return;
49b28c4a64SHelge Deller     }
50b28c4a64SHelge Deller 
51b28c4a64SHelge Deller     /* clear bit 31 to indicate that the power switch was pressed. */
52b28c4a64SHelge Deller     val &= ~1;
53b28c4a64SHelge Deller     stl_be_phys(&address_space_memory, soft_power_reg, val);
54b28c4a64SHelge Deller }
55b28c4a64SHelge Deller 
56b28c4a64SHelge Deller static Notifier hppa_system_powerdown_notifier = {
57b28c4a64SHelge Deller     .notify = hppa_powerdown_req
58b28c4a64SHelge Deller };
59b28c4a64SHelge Deller 
6028f5332aSMark Cave-Ayland /* Fallback for unassigned PCI I/O operations.  Avoids MCHK.  */
6128f5332aSMark Cave-Ayland static uint64_t ignore_read(void *opaque, hwaddr addr, unsigned size)
6228f5332aSMark Cave-Ayland {
6328f5332aSMark Cave-Ayland     return 0;
6428f5332aSMark Cave-Ayland }
6528f5332aSMark Cave-Ayland 
6628f5332aSMark Cave-Ayland static void ignore_write(void *opaque, hwaddr addr, uint64_t v, unsigned size)
6728f5332aSMark Cave-Ayland {
6828f5332aSMark Cave-Ayland }
6928f5332aSMark Cave-Ayland 
7028f5332aSMark Cave-Ayland static const MemoryRegionOps hppa_pci_ignore_ops = {
7128f5332aSMark Cave-Ayland     .read = ignore_read,
7228f5332aSMark Cave-Ayland     .write = ignore_write,
7328f5332aSMark Cave-Ayland     .endianness = DEVICE_BIG_ENDIAN,
7428f5332aSMark Cave-Ayland     .valid = {
7528f5332aSMark Cave-Ayland         .min_access_size = 1,
7628f5332aSMark Cave-Ayland         .max_access_size = 8,
7728f5332aSMark Cave-Ayland     },
7828f5332aSMark Cave-Ayland     .impl = {
7928f5332aSMark Cave-Ayland         .min_access_size = 1,
8028f5332aSMark Cave-Ayland         .max_access_size = 8,
8128f5332aSMark Cave-Ayland     },
8228f5332aSMark Cave-Ayland };
83b28c4a64SHelge Deller 
84a72bd606SHelge Deller static ISABus *hppa_isa_bus(void)
85a72bd606SHelge Deller {
86a72bd606SHelge Deller     ISABus *isa_bus;
87a72bd606SHelge Deller     qemu_irq *isa_irqs;
88a72bd606SHelge Deller     MemoryRegion *isa_region;
89a72bd606SHelge Deller 
90a72bd606SHelge Deller     isa_region = g_new(MemoryRegion, 1);
91a72bd606SHelge Deller     memory_region_init_io(isa_region, NULL, &hppa_pci_ignore_ops,
92a72bd606SHelge Deller                           NULL, "isa-io", 0x800);
93a72bd606SHelge Deller     memory_region_add_subregion(get_system_memory(), IDE_HPA,
94a72bd606SHelge Deller                                 isa_region);
95a72bd606SHelge Deller 
96a72bd606SHelge Deller     isa_bus = isa_bus_new(NULL, get_system_memory(), isa_region,
97a72bd606SHelge Deller                           &error_abort);
98a72bd606SHelge Deller     isa_irqs = i8259_init(isa_bus,
99a72bd606SHelge Deller                           /* qemu_allocate_irq(dino_set_isa_irq, s, 0)); */
100a72bd606SHelge Deller                           NULL);
101*7067887eSPhilippe Mathieu-Daudé     isa_bus_register_input_irqs(isa_bus, isa_irqs);
102a72bd606SHelge Deller 
103a72bd606SHelge Deller     return isa_bus;
104a72bd606SHelge Deller }
105a72bd606SHelge Deller 
106a72bd606SHelge Deller static uint64_t cpu_hppa_to_phys(void *opaque, uint64_t addr)
107a72bd606SHelge Deller {
108a72bd606SHelge Deller     addr &= (0x10000000 - 1);
109a72bd606SHelge Deller     return addr;
110a72bd606SHelge Deller }
111a72bd606SHelge Deller 
112a72bd606SHelge Deller static HPPACPU *cpu[HPPA_MAX_CPUS];
113a72bd606SHelge Deller static uint64_t firmware_entry;
114813dff13SHelge Deller 
11532ff8bf2SHelge Deller static void fw_cfg_boot_set(void *opaque, const char *boot_device,
11632ff8bf2SHelge Deller                             Error **errp)
11732ff8bf2SHelge Deller {
11832ff8bf2SHelge Deller     fw_cfg_modify_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
11932ff8bf2SHelge Deller }
12032ff8bf2SHelge Deller 
12128b71a2eSHelge Deller static FWCfgState *create_fw_cfg(MachineState *ms)
12228b71a2eSHelge Deller {
12328b71a2eSHelge Deller     FWCfgState *fw_cfg;
12428b71a2eSHelge Deller     uint64_t val;
12528b71a2eSHelge Deller 
12624576007SHelge Deller     fw_cfg = fw_cfg_init_mem(FW_CFG_IO_BASE, FW_CFG_IO_BASE + 4);
12728b71a2eSHelge Deller     fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, ms->smp.cpus);
12828b71a2eSHelge Deller     fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, HPPA_MAX_CPUS);
129bfdf22bcSPaolo Bonzini     fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, ms->ram_size);
13028b71a2eSHelge Deller 
13128b71a2eSHelge Deller     val = cpu_to_le64(MIN_SEABIOS_HPPA_VERSION);
13228b71a2eSHelge Deller     fw_cfg_add_file(fw_cfg, "/etc/firmware-min-version",
13328b71a2eSHelge Deller                     g_memdup(&val, sizeof(val)), sizeof(val));
13428b71a2eSHelge Deller 
135df5c6a50SHelge Deller     val = cpu_to_le64(HPPA_TLB_ENTRIES);
136df5c6a50SHelge Deller     fw_cfg_add_file(fw_cfg, "/etc/cpu/tlb_entries",
137df5c6a50SHelge Deller                     g_memdup(&val, sizeof(val)), sizeof(val));
138df5c6a50SHelge Deller 
139df5c6a50SHelge Deller     val = cpu_to_le64(HPPA_BTLB_ENTRIES);
140df5c6a50SHelge Deller     fw_cfg_add_file(fw_cfg, "/etc/cpu/btlb_entries",
141df5c6a50SHelge Deller                     g_memdup(&val, sizeof(val)), sizeof(val));
142df5c6a50SHelge Deller 
143b28c4a64SHelge Deller     val = cpu_to_le64(HPA_POWER_BUTTON);
144b28c4a64SHelge Deller     fw_cfg_add_file(fw_cfg, "/etc/power-button-addr",
145b28c4a64SHelge Deller                     g_memdup(&val, sizeof(val)), sizeof(val));
146b28c4a64SHelge Deller 
14797ec4d21SPaolo Bonzini     fw_cfg_add_i16(fw_cfg, FW_CFG_BOOT_DEVICE, ms->boot_config.order[0]);
14832ff8bf2SHelge Deller     qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);
14932ff8bf2SHelge Deller 
15028b71a2eSHelge Deller     return fw_cfg;
15128b71a2eSHelge Deller }
15228b71a2eSHelge Deller 
153e881e3c8SMark Cave-Ayland static LasiState *lasi_init(void)
154e881e3c8SMark Cave-Ayland {
155e881e3c8SMark Cave-Ayland     DeviceState *dev;
156e881e3c8SMark Cave-Ayland 
157e881e3c8SMark Cave-Ayland     dev = qdev_new(TYPE_LASI_CHIP);
158e881e3c8SMark Cave-Ayland     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
159e881e3c8SMark Cave-Ayland 
160e881e3c8SMark Cave-Ayland     return LASI_CHIP(dev);
161e881e3c8SMark Cave-Ayland }
162e881e3c8SMark Cave-Ayland 
1630d068996SMark Cave-Ayland static DinoState *dino_init(MemoryRegion *addr_space)
1640d068996SMark Cave-Ayland {
1650d068996SMark Cave-Ayland     DeviceState *dev;
1660d068996SMark Cave-Ayland 
1670d068996SMark Cave-Ayland     dev = qdev_new(TYPE_DINO_PCI_HOST_BRIDGE);
1680d068996SMark Cave-Ayland     object_property_set_link(OBJECT(dev), "memory-as", OBJECT(addr_space),
1690d068996SMark Cave-Ayland                              &error_fatal);
1700d068996SMark Cave-Ayland     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
1710d068996SMark Cave-Ayland 
1720d068996SMark Cave-Ayland     return DINO_PCI_HOST_BRIDGE(dev);
1730d068996SMark Cave-Ayland }
1740d068996SMark Cave-Ayland 
175813dff13SHelge Deller static void machine_hppa_init(MachineState *machine)
176813dff13SHelge Deller {
177a72bd606SHelge Deller     const char *kernel_filename = machine->kernel_filename;
178a72bd606SHelge Deller     const char *kernel_cmdline = machine->kernel_cmdline;
179a72bd606SHelge Deller     const char *initrd_filename = machine->initrd_filename;
1802683758cSMark Cave-Ayland     DeviceState *dev, *dino_dev, *lasi_dev;
181a72bd606SHelge Deller     PCIBus *pci_bus;
182a72bd606SHelge Deller     ISABus *isa_bus;
183a72bd606SHelge Deller     char *firmware_filename;
184a72bd606SHelge Deller     uint64_t firmware_low, firmware_high;
185a72bd606SHelge Deller     long size;
186a72bd606SHelge Deller     uint64_t kernel_entry = 0, kernel_low, kernel_high;
187a72bd606SHelge Deller     MemoryRegion *addr_space = get_system_memory();
188a72bd606SHelge Deller     MemoryRegion *rom_region;
189a72bd606SHelge Deller     MemoryRegion *cpu_region;
190a72bd606SHelge Deller     long i;
19133decbd2SLike Xu     unsigned int smp_cpus = machine->smp.cpus;
1924765384cSSven Schnelle     SysBusDevice *s;
193a72bd606SHelge Deller 
194a72bd606SHelge Deller     /* Create CPUs.  */
195a72bd606SHelge Deller     for (i = 0; i < smp_cpus; i++) {
196266a880eSPaolo Bonzini         char *name = g_strdup_printf("cpu%ld-io-eir", i);
197a72bd606SHelge Deller         cpu[i] = HPPA_CPU(cpu_create(machine->cpu_type));
198a72bd606SHelge Deller 
199a72bd606SHelge Deller         cpu_region = g_new(MemoryRegion, 1);
200a72bd606SHelge Deller         memory_region_init_io(cpu_region, OBJECT(cpu[i]), &hppa_io_eir_ops,
201266a880eSPaolo Bonzini                               cpu[i], name, 4);
202a72bd606SHelge Deller         memory_region_add_subregion(addr_space, CPU_HPA + i * 0x1000,
203a72bd606SHelge Deller                                     cpu_region);
204266a880eSPaolo Bonzini         g_free(name);
205813dff13SHelge Deller     }
206813dff13SHelge Deller 
207a72bd606SHelge Deller     /* Main memory region. */
208b7746b11SPhilippe Mathieu-Daudé     if (machine->ram_size > 3 * GiB) {
209b7746b11SPhilippe Mathieu-Daudé         error_report("RAM size is currently restricted to 3GB");
210b7746b11SPhilippe Mathieu-Daudé         exit(EXIT_FAILURE);
211b7746b11SPhilippe Mathieu-Daudé     }
2127c59c1e0SIgor Mammedov     memory_region_add_subregion_overlap(addr_space, 0, machine->ram, -1);
2137c59c1e0SIgor Mammedov 
214a72bd606SHelge Deller 
215376b8519SHelge Deller     /* Init Lasi chip */
216e881e3c8SMark Cave-Ayland     lasi_dev = DEVICE(lasi_init());
2172683758cSMark Cave-Ayland     memory_region_add_subregion(addr_space, LASI_HPA,
2182683758cSMark Cave-Ayland                                 sysbus_mmio_get_region(
2192683758cSMark Cave-Ayland                                     SYS_BUS_DEVICE(lasi_dev), 0));
220376b8519SHelge Deller 
221a72bd606SHelge Deller     /* Init Dino (PCI host bus chip).  */
22236f9bbdbSMark Cave-Ayland     dino_dev = DEVICE(dino_init(addr_space));
223efdb3ce2SMark Cave-Ayland     memory_region_add_subregion(addr_space, DINO_HPA,
224efdb3ce2SMark Cave-Ayland                                 sysbus_mmio_get_region(
225efdb3ce2SMark Cave-Ayland                                     SYS_BUS_DEVICE(dino_dev), 0));
22605245dafSMark Cave-Ayland     pci_bus = PCI_BUS(qdev_get_child_bus(dino_dev, "pci"));
227a72bd606SHelge Deller     assert(pci_bus);
228a72bd606SHelge Deller 
229a72bd606SHelge Deller     /* Create ISA bus. */
230a72bd606SHelge Deller     isa_bus = hppa_isa_bus();
231a72bd606SHelge Deller     assert(isa_bus);
232a72bd606SHelge Deller 
233a72bd606SHelge Deller     /* Realtime clock, used by firmware for PDC_TOD call. */
23436f9bbdbSMark Cave-Ayland     mc146818_rtc_init(isa_bus, 2000, NULL);
235a72bd606SHelge Deller 
2365079892dSHelge Deller     /* Serial ports: Lasi and Dino use a 7.272727 MHz clock. */
2372da547b8SMark Cave-Ayland     serial_mm_init(addr_space, LASI_UART_HPA + 0x800, 0,
2385079892dSHelge Deller         qdev_get_gpio_in(lasi_dev, LASI_IRQ_UART_HPA), 7272727 / 16,
2395079892dSHelge Deller         serial_hd(0), DEVICE_BIG_ENDIAN);
2405079892dSHelge Deller 
2415079892dSHelge Deller     serial_mm_init(addr_space, DINO_UART_HPA + 0x800, 0,
2425079892dSHelge Deller         qdev_get_gpio_in(dino_dev, DINO_IRQ_RS232INT), 7272727 / 16,
2432da547b8SMark Cave-Ayland         serial_hd(1), DEVICE_BIG_ENDIAN);
2442da547b8SMark Cave-Ayland 
2459701e569SMark Cave-Ayland     /* Parallel port */
2469701e569SMark Cave-Ayland     parallel_mm_init(addr_space, LASI_LPT_HPA + 0x800, 0,
2479701e569SMark Cave-Ayland                      qdev_get_gpio_in(lasi_dev, LASI_IRQ_LAN_HPA),
2489701e569SMark Cave-Ayland                      parallel_hds[0]);
2499701e569SMark Cave-Ayland 
25028b71a2eSHelge Deller     /* fw_cfg configuration interface */
25128b71a2eSHelge Deller     create_fw_cfg(machine);
25228b71a2eSHelge Deller 
253a72bd606SHelge Deller     /* SCSI disk setup. */
254877eb21dSMark Cave-Ayland     dev = DEVICE(pci_create_simple(pci_bus, -1, "lsi53c895a"));
255877eb21dSMark Cave-Ayland     lsi53c8xx_handle_legacy_cmdline(dev);
256a72bd606SHelge Deller 
2574765384cSSven Schnelle     /* Graphics setup. */
2584765384cSSven Schnelle     if (machine->enable_graphics && vga_interface_type != VGA_NONE) {
259f9bcb2d6SGautam Agrawal         vga_interface_created = true;
2603e80f690SMarkus Armbruster         dev = qdev_new("artist");
2614765384cSSven Schnelle         s = SYS_BUS_DEVICE(dev);
2623c6ef471SMarkus Armbruster         sysbus_realize_and_unref(s, &error_fatal);
2634765384cSSven Schnelle         sysbus_mmio_map(s, 0, LASI_GFX_HPA);
2644765384cSSven Schnelle         sysbus_mmio_map(s, 1, ARTIST_FB_ADDR);
2654765384cSSven Schnelle     }
2664765384cSSven Schnelle 
2670e6de551SHelge Deller     /* Network setup. */
268c3c3fe47SMark Cave-Ayland     if (enable_lasi_lan()) {
269c3c3fe47SMark Cave-Ayland         lasi_82596_init(addr_space, LASI_LAN_HPA,
270c3c3fe47SMark Cave-Ayland                         qdev_get_gpio_in(lasi_dev, LASI_IRQ_LAN_HPA));
271c3c3fe47SMark Cave-Ayland     }
272c3c3fe47SMark Cave-Ayland 
273a72bd606SHelge Deller     for (i = 0; i < nb_nics; i++) {
274376b8519SHelge Deller         if (!enable_lasi_lan()) {
2750e6de551SHelge Deller             pci_nic_init_nofail(&nd_table[i], pci_bus, "tulip", NULL);
276a72bd606SHelge Deller         }
277376b8519SHelge Deller     }
278a72bd606SHelge Deller 
279d26c575cSMark Cave-Ayland     /* PS/2 Keyboard/Mouse */
28092bd278cSMark Cave-Ayland     dev = qdev_new(TYPE_LASIPS2);
28192bd278cSMark Cave-Ayland     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
28292bd278cSMark Cave-Ayland     sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0,
28392bd278cSMark Cave-Ayland                        qdev_get_gpio_in(lasi_dev, LASI_IRQ_PS2KBD_HPA));
2846479296fSMark Cave-Ayland     memory_region_add_subregion(addr_space, LASI_PS2KBD_HPA,
2856479296fSMark Cave-Ayland                                 sysbus_mmio_get_region(SYS_BUS_DEVICE(dev),
2866479296fSMark Cave-Ayland                                                        0));
2876479296fSMark Cave-Ayland     memory_region_add_subregion(addr_space, LASI_PS2KBD_HPA + 0x100,
2886479296fSMark Cave-Ayland                                 sysbus_mmio_get_region(SYS_BUS_DEVICE(dev),
2896479296fSMark Cave-Ayland                                                        1));
290d26c575cSMark Cave-Ayland 
291b28c4a64SHelge Deller     /* register power switch emulation */
292b28c4a64SHelge Deller     qemu_register_powerdown_notifier(&hppa_system_powerdown_notifier);
293b28c4a64SHelge Deller 
294a72bd606SHelge Deller     /* Load firmware.  Given that this is not "real" firmware,
295a72bd606SHelge Deller        but one explicitly written for the emulation, we might as
296a72bd606SHelge Deller        well load it directly from an ELF image.  */
297a72bd606SHelge Deller     firmware_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
298b57e3e97SPaolo Bonzini                                        machine->firmware ?: "hppa-firmware.img");
299a72bd606SHelge Deller     if (firmware_filename == NULL) {
300a72bd606SHelge Deller         error_report("no firmware provided");
301a72bd606SHelge Deller         exit(1);
302a72bd606SHelge Deller     }
303a72bd606SHelge Deller 
3044366e1dbSLiam Merwick     size = load_elf(firmware_filename, NULL, NULL, NULL,
3056cdda0ffSAleksandar Markovic                     &firmware_entry, &firmware_low, &firmware_high, NULL,
306a72bd606SHelge Deller                     true, EM_PARISC, 0, 0);
307a72bd606SHelge Deller 
308a72bd606SHelge Deller     /* Unfortunately, load_elf sign-extends reading elf32.  */
309a72bd606SHelge Deller     firmware_entry = (target_ureg)firmware_entry;
310a72bd606SHelge Deller     firmware_low = (target_ureg)firmware_low;
311a72bd606SHelge Deller     firmware_high = (target_ureg)firmware_high;
312a72bd606SHelge Deller 
313a72bd606SHelge Deller     if (size < 0) {
314a72bd606SHelge Deller         error_report("could not load firmware '%s'", firmware_filename);
315a72bd606SHelge Deller         exit(1);
316a72bd606SHelge Deller     }
317691cbbadSRichard Henderson     qemu_log_mask(CPU_LOG_PAGE, "Firmware loaded at 0x%08" PRIx64
318691cbbadSRichard Henderson                   "-0x%08" PRIx64 ", entry at 0x%08" PRIx64 ".\n",
319a72bd606SHelge Deller                   firmware_low, firmware_high, firmware_entry);
3208262863dSPhilippe Mathieu-Daudé     if (firmware_low < FIRMWARE_START || firmware_high >= FIRMWARE_END) {
321a72bd606SHelge Deller         error_report("Firmware overlaps with memory or IO space");
322a72bd606SHelge Deller         exit(1);
323a72bd606SHelge Deller     }
324a72bd606SHelge Deller     g_free(firmware_filename);
325a72bd606SHelge Deller 
326a72bd606SHelge Deller     rom_region = g_new(MemoryRegion, 1);
3276a3a2e82SIgor Mammedov     memory_region_init_ram(rom_region, NULL, "firmware",
3286a3a2e82SIgor Mammedov                            (FIRMWARE_END - FIRMWARE_START), &error_fatal);
329a72bd606SHelge Deller     memory_region_add_subregion(addr_space, FIRMWARE_START, rom_region);
330a72bd606SHelge Deller 
331a72bd606SHelge Deller     /* Load kernel */
332a72bd606SHelge Deller     if (kernel_filename) {
3334366e1dbSLiam Merwick         size = load_elf(kernel_filename, NULL, &cpu_hppa_to_phys,
3346cdda0ffSAleksandar Markovic                         NULL, &kernel_entry, &kernel_low, &kernel_high, NULL,
335a72bd606SHelge Deller                         true, EM_PARISC, 0, 0);
336a72bd606SHelge Deller 
337a72bd606SHelge Deller         /* Unfortunately, load_elf sign-extends reading elf32.  */
338a72bd606SHelge Deller         kernel_entry = (target_ureg) cpu_hppa_to_phys(NULL, kernel_entry);
339a72bd606SHelge Deller         kernel_low = (target_ureg)kernel_low;
340a72bd606SHelge Deller         kernel_high = (target_ureg)kernel_high;
341a72bd606SHelge Deller 
342a72bd606SHelge Deller         if (size < 0) {
343a72bd606SHelge Deller             error_report("could not load kernel '%s'", kernel_filename);
344a72bd606SHelge Deller             exit(1);
345a72bd606SHelge Deller         }
346691cbbadSRichard Henderson         qemu_log_mask(CPU_LOG_PAGE, "Kernel loaded at 0x%08" PRIx64
347691cbbadSRichard Henderson                       "-0x%08" PRIx64 ", entry at 0x%08" PRIx64
348c108cc59SPhilippe Mathieu-Daudé                       ", size %" PRIu64 " kB\n",
349c108cc59SPhilippe Mathieu-Daudé                       kernel_low, kernel_high, kernel_entry, size / KiB);
350a72bd606SHelge Deller 
351a72bd606SHelge Deller         if (kernel_cmdline) {
352a72bd606SHelge Deller             cpu[0]->env.gr[24] = 0x4000;
353a72bd606SHelge Deller             pstrcpy_targphys("cmdline", cpu[0]->env.gr[24],
354a72bd606SHelge Deller                              TARGET_PAGE_SIZE, kernel_cmdline);
355a72bd606SHelge Deller         }
356a72bd606SHelge Deller 
357a72bd606SHelge Deller         if (initrd_filename) {
358a72bd606SHelge Deller             ram_addr_t initrd_base;
359f3839fdaSLi Zhijian             int64_t initrd_size;
360a72bd606SHelge Deller 
361a72bd606SHelge Deller             initrd_size = get_image_size(initrd_filename);
362a72bd606SHelge Deller             if (initrd_size < 0) {
363a72bd606SHelge Deller                 error_report("could not load initial ram disk '%s'",
364a72bd606SHelge Deller                              initrd_filename);
365a72bd606SHelge Deller                 exit(1);
366a72bd606SHelge Deller             }
367a72bd606SHelge Deller 
368a72bd606SHelge Deller             /* Load the initrd image high in memory.
369a72bd606SHelge Deller                Mirror the algorithm used by palo:
370a72bd606SHelge Deller                (1) Due to sign-extension problems and PDC,
371a72bd606SHelge Deller                put the initrd no higher than 1G.
372a72bd606SHelge Deller                (2) Reserve 64k for stack.  */
373bfdf22bcSPaolo Bonzini             initrd_base = MIN(machine->ram_size, 1 * GiB);
374c108cc59SPhilippe Mathieu-Daudé             initrd_base = initrd_base - 64 * KiB;
375a72bd606SHelge Deller             initrd_base = (initrd_base - initrd_size) & TARGET_PAGE_MASK;
376a72bd606SHelge Deller 
377a72bd606SHelge Deller             if (initrd_base < kernel_high) {
378a72bd606SHelge Deller                 error_report("kernel and initial ram disk too large!");
379a72bd606SHelge Deller                 exit(1);
380a72bd606SHelge Deller             }
381a72bd606SHelge Deller 
382a72bd606SHelge Deller             load_image_targphys(initrd_filename, initrd_base, initrd_size);
383a72bd606SHelge Deller             cpu[0]->env.gr[23] = initrd_base;
384a72bd606SHelge Deller             cpu[0]->env.gr[22] = initrd_base + initrd_size;
385a72bd606SHelge Deller         }
386a72bd606SHelge Deller     }
387a72bd606SHelge Deller 
388a72bd606SHelge Deller     if (!kernel_entry) {
389a72bd606SHelge Deller         /* When booting via firmware, tell firmware if we want interactive
390a72bd606SHelge Deller          * mode (kernel_entry=1), and to boot from CD (gr[24]='d')
391a72bd606SHelge Deller          * or hard disc * (gr[24]='c').
392a72bd606SHelge Deller          */
39397ec4d21SPaolo Bonzini         kernel_entry = machine->boot_config.has_menu ? machine->boot_config.menu : 0;
39497ec4d21SPaolo Bonzini         cpu[0]->env.gr[24] = machine->boot_config.order[0];
395a72bd606SHelge Deller     }
396a72bd606SHelge Deller 
397a72bd606SHelge Deller     /* We jump to the firmware entry routine and pass the
398a72bd606SHelge Deller      * various parameters in registers. After firmware initialization,
399a72bd606SHelge Deller      * firmware will start the Linux kernel with ramdisk and cmdline.
400a72bd606SHelge Deller      */
401bfdf22bcSPaolo Bonzini     cpu[0]->env.gr[26] = machine->ram_size;
402a72bd606SHelge Deller     cpu[0]->env.gr[25] = kernel_entry;
403a72bd606SHelge Deller 
404a72bd606SHelge Deller     /* tell firmware how many SMP CPUs to present in inventory table */
405a72bd606SHelge Deller     cpu[0]->env.gr[21] = smp_cpus;
40624576007SHelge Deller 
40724576007SHelge Deller     /* tell firmware fw_cfg port */
40824576007SHelge Deller     cpu[0]->env.gr[19] = FW_CFG_IO_BASE;
409a72bd606SHelge Deller }
410a72bd606SHelge Deller 
4117966d70fSJason A. Donenfeld static void hppa_machine_reset(MachineState *ms, ShutdownCause reason)
412a72bd606SHelge Deller {
41333decbd2SLike Xu     unsigned int smp_cpus = ms->smp.cpus;
414a72bd606SHelge Deller     int i;
415a72bd606SHelge Deller 
4167966d70fSJason A. Donenfeld     qemu_devices_reset(reason);
417a72bd606SHelge Deller 
418a72bd606SHelge Deller     /* Start all CPUs at the firmware entry point.
419a72bd606SHelge Deller      *  Monarch CPU will initialize firmware, secondary CPUs
420a72bd606SHelge Deller      *  will enter a small idle look and wait for rendevouz. */
421a72bd606SHelge Deller     for (i = 0; i < smp_cpus; i++) {
422a72bd606SHelge Deller         cpu_set_pc(CPU(cpu[i]), firmware_entry);
423a72bd606SHelge Deller         cpu[i]->env.gr[5] = CPU_HPA + i * 0x1000;
424a72bd606SHelge Deller     }
425a72bd606SHelge Deller 
426a72bd606SHelge Deller     /* already initialized by machine_hppa_init()? */
427bfdf22bcSPaolo Bonzini     if (cpu[0]->env.gr[26] == ms->ram_size) {
428a72bd606SHelge Deller         return;
429a72bd606SHelge Deller     }
430a72bd606SHelge Deller 
431bfdf22bcSPaolo Bonzini     cpu[0]->env.gr[26] = ms->ram_size;
432a72bd606SHelge Deller     cpu[0]->env.gr[25] = 0; /* no firmware boot menu */
433a72bd606SHelge Deller     cpu[0]->env.gr[24] = 'c';
434a72bd606SHelge Deller     /* gr22/gr23 unused, no initrd while reboot. */
435a72bd606SHelge Deller     cpu[0]->env.gr[21] = smp_cpus;
43624576007SHelge Deller     /* tell firmware fw_cfg port */
43724576007SHelge Deller     cpu[0]->env.gr[19] = FW_CFG_IO_BASE;
438a72bd606SHelge Deller }
439a72bd606SHelge Deller 
4404a4554c6SHelge Deller static void hppa_nmi(NMIState *n, int cpu_index, Error **errp)
4414a4554c6SHelge Deller {
4424a4554c6SHelge Deller     CPUState *cs;
4434a4554c6SHelge Deller 
4444a4554c6SHelge Deller     CPU_FOREACH(cs) {
4454a4554c6SHelge Deller         cpu_interrupt(cs, CPU_INTERRUPT_NMI);
4464a4554c6SHelge Deller     }
4474a4554c6SHelge Deller }
448a72bd606SHelge Deller 
449297d4103SMark Cave-Ayland static void hppa_machine_init_class_init(ObjectClass *oc, void *data)
450813dff13SHelge Deller {
45142cc2bf6SMark Cave-Ayland     MachineClass *mc = MACHINE_CLASS(oc);
45242cc2bf6SMark Cave-Ayland     NMIClass *nc = NMI_CLASS(oc);
45342cc2bf6SMark Cave-Ayland 
4540d98fbb5SMark Cave-Ayland     mc->desc = "HPPA B160L machine";
455a72bd606SHelge Deller     mc->default_cpu_type = TYPE_HPPA_CPU;
456813dff13SHelge Deller     mc->init = machine_hppa_init;
457a72bd606SHelge Deller     mc->reset = hppa_machine_reset;
458813dff13SHelge Deller     mc->block_default_type = IF_SCSI;
459a72bd606SHelge Deller     mc->max_cpus = HPPA_MAX_CPUS;
460a72bd606SHelge Deller     mc->default_cpus = 1;
461ea0ac7f6SPhilippe Mathieu-Daudé     mc->is_default = true;
462d23b6caaSPhilippe Mathieu-Daudé     mc->default_ram_size = 512 * MiB;
463813dff13SHelge Deller     mc->default_boot_order = "cd";
4647c59c1e0SIgor Mammedov     mc->default_ram_id = "ram";
465813dff13SHelge Deller 
4664a4554c6SHelge Deller     nc->nmi_monitor_handler = hppa_nmi;
4674a4554c6SHelge Deller }
4684a4554c6SHelge Deller 
469297d4103SMark Cave-Ayland static const TypeInfo hppa_machine_init_typeinfo = {
470c165905cSMark Cave-Ayland     .name = MACHINE_TYPE_NAME("hppa"),
471c165905cSMark Cave-Ayland     .parent = TYPE_MACHINE,
472297d4103SMark Cave-Ayland     .class_init = hppa_machine_init_class_init,
4734a4554c6SHelge Deller     .interfaces = (InterfaceInfo[]) {
4744a4554c6SHelge Deller         { TYPE_NMI },
4754a4554c6SHelge Deller         { }
4764a4554c6SHelge Deller     },
4774a4554c6SHelge Deller };
4784a4554c6SHelge Deller 
479297d4103SMark Cave-Ayland static void hppa_machine_init_register_types(void)
4804a4554c6SHelge Deller {
481297d4103SMark Cave-Ayland     type_register_static(&hppa_machine_init_typeinfo);
4824a4554c6SHelge Deller }
4834a4554c6SHelge Deller 
484297d4103SMark Cave-Ayland type_init(hppa_machine_init_register_types)
485