xref: /openbmc/qemu/hw/hppa/machine.c (revision a536f564d3281014478ef100a0cd1204a47eb9e1)
1813dff13SHelge Deller /*
2813dff13SHelge Deller  * QEMU HPPA hardware system emulator.
3*a536f564SHelge Deller  * (C) Copyright 2018-2023 Helge Deller <deller@gmx.de>
4*a536f564SHelge Deller  *
5*a536f564SHelge Deller  * This work is licensed under the GNU GPL license version 2 or later.
6813dff13SHelge Deller  */
7813dff13SHelge Deller 
8813dff13SHelge Deller #include "qemu/osdep.h"
92c65db5eSPaolo Bonzini #include "qemu/datadir.h"
10813dff13SHelge Deller #include "cpu.h"
11813dff13SHelge Deller #include "elf.h"
12813dff13SHelge Deller #include "hw/loader.h"
13813dff13SHelge Deller #include "qemu/error-report.h"
1471e8a915SMarkus Armbruster #include "sysemu/reset.h"
15813dff13SHelge Deller #include "sysemu/sysemu.h"
16b28c4a64SHelge Deller #include "sysemu/runstate.h"
17bcdb9064SPhilippe Mathieu-Daudé #include "hw/rtc/mc146818rtc.h"
18813dff13SHelge Deller #include "hw/timer/i8254.h"
19813dff13SHelge Deller #include "hw/char/serial.h"
209701e569SMark Cave-Ayland #include "hw/char/parallel.h"
21134ba73fSMark Cave-Ayland #include "hw/intc/i8259.h"
22d26c575cSMark Cave-Ayland #include "hw/input/lasips2.h"
23376b8519SHelge Deller #include "hw/net/lasi_82596.h"
244a4554c6SHelge Deller #include "hw/nmi.h"
25134ba73fSMark Cave-Ayland #include "hw/pci/pci.h"
260db9350eSMark Cave-Ayland #include "hw/pci-host/dino.h"
2745f569a1SMark Cave-Ayland #include "hw/misc/lasi.h"
28148da670SMark Cave-Ayland #include "hppa_hardware.h"
29c108cc59SPhilippe Mathieu-Daudé #include "qemu/units.h"
30813dff13SHelge Deller #include "qapi/error.h"
31852c27e2SPaolo Bonzini #include "net/net.h"
32691cbbadSRichard Henderson #include "qemu/log.h"
33813dff13SHelge Deller 
34*a536f564SHelge Deller #define MIN_SEABIOS_HPPA_VERSION 10 /* require at least this fw version */
3528b71a2eSHelge Deller 
36b28c4a64SHelge Deller #define HPA_POWER_BUTTON (FIRMWARE_END - 0x10)
37b28c4a64SHelge Deller 
38932befaaSMark Cave-Ayland #define enable_lasi_lan()       0
39932befaaSMark Cave-Ayland 
40932befaaSMark Cave-Ayland 
41b28c4a64SHelge Deller static void hppa_powerdown_req(Notifier *n, void *opaque)
42b28c4a64SHelge Deller {
43b28c4a64SHelge Deller     hwaddr soft_power_reg = HPA_POWER_BUTTON;
44b28c4a64SHelge Deller     uint32_t val;
45b28c4a64SHelge Deller 
46b28c4a64SHelge Deller     val = ldl_be_phys(&address_space_memory, soft_power_reg);
47b28c4a64SHelge Deller     if ((val >> 8) == 0) {
48b28c4a64SHelge Deller         /* immediately shut down when under hardware control */
49b28c4a64SHelge Deller         qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
50b28c4a64SHelge Deller         return;
51b28c4a64SHelge Deller     }
52b28c4a64SHelge Deller 
53b28c4a64SHelge Deller     /* clear bit 31 to indicate that the power switch was pressed. */
54b28c4a64SHelge Deller     val &= ~1;
55b28c4a64SHelge Deller     stl_be_phys(&address_space_memory, soft_power_reg, val);
56b28c4a64SHelge Deller }
57b28c4a64SHelge Deller 
58b28c4a64SHelge Deller static Notifier hppa_system_powerdown_notifier = {
59b28c4a64SHelge Deller     .notify = hppa_powerdown_req
60b28c4a64SHelge Deller };
61b28c4a64SHelge Deller 
6228f5332aSMark Cave-Ayland /* Fallback for unassigned PCI I/O operations.  Avoids MCHK.  */
6328f5332aSMark Cave-Ayland static uint64_t ignore_read(void *opaque, hwaddr addr, unsigned size)
6428f5332aSMark Cave-Ayland {
6528f5332aSMark Cave-Ayland     return 0;
6628f5332aSMark Cave-Ayland }
6728f5332aSMark Cave-Ayland 
6828f5332aSMark Cave-Ayland static void ignore_write(void *opaque, hwaddr addr, uint64_t v, unsigned size)
6928f5332aSMark Cave-Ayland {
7028f5332aSMark Cave-Ayland }
7128f5332aSMark Cave-Ayland 
7228f5332aSMark Cave-Ayland static const MemoryRegionOps hppa_pci_ignore_ops = {
7328f5332aSMark Cave-Ayland     .read = ignore_read,
7428f5332aSMark Cave-Ayland     .write = ignore_write,
7528f5332aSMark Cave-Ayland     .endianness = DEVICE_BIG_ENDIAN,
7628f5332aSMark Cave-Ayland     .valid = {
7728f5332aSMark Cave-Ayland         .min_access_size = 1,
7828f5332aSMark Cave-Ayland         .max_access_size = 8,
7928f5332aSMark Cave-Ayland     },
8028f5332aSMark Cave-Ayland     .impl = {
8128f5332aSMark Cave-Ayland         .min_access_size = 1,
8228f5332aSMark Cave-Ayland         .max_access_size = 8,
8328f5332aSMark Cave-Ayland     },
8428f5332aSMark Cave-Ayland };
85b28c4a64SHelge Deller 
86a72bd606SHelge Deller static ISABus *hppa_isa_bus(void)
87a72bd606SHelge Deller {
88a72bd606SHelge Deller     ISABus *isa_bus;
89a72bd606SHelge Deller     qemu_irq *isa_irqs;
90a72bd606SHelge Deller     MemoryRegion *isa_region;
91a72bd606SHelge Deller 
92a72bd606SHelge Deller     isa_region = g_new(MemoryRegion, 1);
93a72bd606SHelge Deller     memory_region_init_io(isa_region, NULL, &hppa_pci_ignore_ops,
94a72bd606SHelge Deller                           NULL, "isa-io", 0x800);
95a72bd606SHelge Deller     memory_region_add_subregion(get_system_memory(), IDE_HPA,
96a72bd606SHelge Deller                                 isa_region);
97a72bd606SHelge Deller 
98a72bd606SHelge Deller     isa_bus = isa_bus_new(NULL, get_system_memory(), isa_region,
99a72bd606SHelge Deller                           &error_abort);
100*a536f564SHelge Deller     isa_irqs = i8259_init(isa_bus, NULL);
1017067887eSPhilippe 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;
125069d2966SHelge Deller     const char qemu_version[] = QEMU_VERSION;
12628b71a2eSHelge Deller 
12724576007SHelge Deller     fw_cfg = fw_cfg_init_mem(FW_CFG_IO_BASE, FW_CFG_IO_BASE + 4);
12828b71a2eSHelge Deller     fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, ms->smp.cpus);
12928b71a2eSHelge Deller     fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, HPPA_MAX_CPUS);
130bfdf22bcSPaolo Bonzini     fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, ms->ram_size);
13128b71a2eSHelge Deller 
13228b71a2eSHelge Deller     val = cpu_to_le64(MIN_SEABIOS_HPPA_VERSION);
13328b71a2eSHelge Deller     fw_cfg_add_file(fw_cfg, "/etc/firmware-min-version",
13428b71a2eSHelge Deller                     g_memdup(&val, sizeof(val)), sizeof(val));
13528b71a2eSHelge Deller 
1366d1ef68cSHelge Deller     val = cpu_to_le64(HPPA_TLB_ENTRIES - HPPA_BTLB_ENTRIES);
137df5c6a50SHelge Deller     fw_cfg_add_file(fw_cfg, "/etc/cpu/tlb_entries",
138df5c6a50SHelge Deller                     g_memdup(&val, sizeof(val)), sizeof(val));
139df5c6a50SHelge Deller 
140b28c4a64SHelge Deller     val = cpu_to_le64(HPA_POWER_BUTTON);
141b28c4a64SHelge Deller     fw_cfg_add_file(fw_cfg, "/etc/power-button-addr",
142b28c4a64SHelge Deller                     g_memdup(&val, sizeof(val)), sizeof(val));
143b28c4a64SHelge Deller 
14497ec4d21SPaolo Bonzini     fw_cfg_add_i16(fw_cfg, FW_CFG_BOOT_DEVICE, ms->boot_config.order[0]);
14532ff8bf2SHelge Deller     qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);
14632ff8bf2SHelge Deller 
147069d2966SHelge Deller     fw_cfg_add_file(fw_cfg, "/etc/qemu-version",
148069d2966SHelge Deller                     g_memdup(qemu_version, sizeof(qemu_version)),
149069d2966SHelge Deller                     sizeof(qemu_version));
150069d2966SHelge Deller 
15128b71a2eSHelge Deller     return fw_cfg;
15228b71a2eSHelge Deller }
15328b71a2eSHelge Deller 
154e881e3c8SMark Cave-Ayland static LasiState *lasi_init(void)
155e881e3c8SMark Cave-Ayland {
156e881e3c8SMark Cave-Ayland     DeviceState *dev;
157e881e3c8SMark Cave-Ayland 
158e881e3c8SMark Cave-Ayland     dev = qdev_new(TYPE_LASI_CHIP);
159e881e3c8SMark Cave-Ayland     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
160e881e3c8SMark Cave-Ayland 
161e881e3c8SMark Cave-Ayland     return LASI_CHIP(dev);
162e881e3c8SMark Cave-Ayland }
163e881e3c8SMark Cave-Ayland 
1640d068996SMark Cave-Ayland static DinoState *dino_init(MemoryRegion *addr_space)
1650d068996SMark Cave-Ayland {
1660d068996SMark Cave-Ayland     DeviceState *dev;
1670d068996SMark Cave-Ayland 
1680d068996SMark Cave-Ayland     dev = qdev_new(TYPE_DINO_PCI_HOST_BRIDGE);
1690d068996SMark Cave-Ayland     object_property_set_link(OBJECT(dev), "memory-as", OBJECT(addr_space),
1700d068996SMark Cave-Ayland                              &error_fatal);
1710d068996SMark Cave-Ayland     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
1720d068996SMark Cave-Ayland 
1730d068996SMark Cave-Ayland     return DINO_PCI_HOST_BRIDGE(dev);
1740d068996SMark Cave-Ayland }
1750d068996SMark Cave-Ayland 
176813dff13SHelge Deller static void machine_hppa_init(MachineState *machine)
177813dff13SHelge Deller {
178a72bd606SHelge Deller     const char *kernel_filename = machine->kernel_filename;
179a72bd606SHelge Deller     const char *kernel_cmdline = machine->kernel_cmdline;
180a72bd606SHelge Deller     const char *initrd_filename = machine->initrd_filename;
1819f8981a9SThomas Huth     MachineClass *mc = MACHINE_GET_CLASS(machine);
1822683758cSMark Cave-Ayland     DeviceState *dev, *dino_dev, *lasi_dev;
183a72bd606SHelge Deller     PCIBus *pci_bus;
184a72bd606SHelge Deller     ISABus *isa_bus;
185a72bd606SHelge Deller     char *firmware_filename;
186a72bd606SHelge Deller     uint64_t firmware_low, firmware_high;
187a72bd606SHelge Deller     long size;
188a72bd606SHelge Deller     uint64_t kernel_entry = 0, kernel_low, kernel_high;
189a72bd606SHelge Deller     MemoryRegion *addr_space = get_system_memory();
190a72bd606SHelge Deller     MemoryRegion *rom_region;
191a72bd606SHelge Deller     MemoryRegion *cpu_region;
192a72bd606SHelge Deller     long i;
19333decbd2SLike Xu     unsigned int smp_cpus = machine->smp.cpus;
1944765384cSSven Schnelle     SysBusDevice *s;
195a72bd606SHelge Deller 
196a72bd606SHelge Deller     /* Create CPUs.  */
197a72bd606SHelge Deller     for (i = 0; i < smp_cpus; i++) {
198266a880eSPaolo Bonzini         char *name = g_strdup_printf("cpu%ld-io-eir", i);
199a72bd606SHelge Deller         cpu[i] = HPPA_CPU(cpu_create(machine->cpu_type));
200a72bd606SHelge Deller 
201a72bd606SHelge Deller         cpu_region = g_new(MemoryRegion, 1);
202a72bd606SHelge Deller         memory_region_init_io(cpu_region, OBJECT(cpu[i]), &hppa_io_eir_ops,
203266a880eSPaolo Bonzini                               cpu[i], name, 4);
204a72bd606SHelge Deller         memory_region_add_subregion(addr_space, CPU_HPA + i * 0x1000,
205a72bd606SHelge Deller                                     cpu_region);
206266a880eSPaolo Bonzini         g_free(name);
207813dff13SHelge Deller     }
208813dff13SHelge Deller 
209a72bd606SHelge Deller     /* Main memory region. */
210b7746b11SPhilippe Mathieu-Daudé     if (machine->ram_size > 3 * GiB) {
211b7746b11SPhilippe Mathieu-Daudé         error_report("RAM size is currently restricted to 3GB");
212b7746b11SPhilippe Mathieu-Daudé         exit(EXIT_FAILURE);
213b7746b11SPhilippe Mathieu-Daudé     }
2147c59c1e0SIgor Mammedov     memory_region_add_subregion_overlap(addr_space, 0, machine->ram, -1);
2157c59c1e0SIgor Mammedov 
216a72bd606SHelge Deller 
217376b8519SHelge Deller     /* Init Lasi chip */
218e881e3c8SMark Cave-Ayland     lasi_dev = DEVICE(lasi_init());
2192683758cSMark Cave-Ayland     memory_region_add_subregion(addr_space, LASI_HPA,
2202683758cSMark Cave-Ayland                                 sysbus_mmio_get_region(
2212683758cSMark Cave-Ayland                                     SYS_BUS_DEVICE(lasi_dev), 0));
222376b8519SHelge Deller 
223a72bd606SHelge Deller     /* Init Dino (PCI host bus chip).  */
22436f9bbdbSMark Cave-Ayland     dino_dev = DEVICE(dino_init(addr_space));
225efdb3ce2SMark Cave-Ayland     memory_region_add_subregion(addr_space, DINO_HPA,
226efdb3ce2SMark Cave-Ayland                                 sysbus_mmio_get_region(
227efdb3ce2SMark Cave-Ayland                                     SYS_BUS_DEVICE(dino_dev), 0));
22805245dafSMark Cave-Ayland     pci_bus = PCI_BUS(qdev_get_child_bus(dino_dev, "pci"));
229a72bd606SHelge Deller     assert(pci_bus);
230a72bd606SHelge Deller 
231a72bd606SHelge Deller     /* Create ISA bus. */
232a72bd606SHelge Deller     isa_bus = hppa_isa_bus();
233a72bd606SHelge Deller     assert(isa_bus);
234a72bd606SHelge Deller 
235a72bd606SHelge Deller     /* Realtime clock, used by firmware for PDC_TOD call. */
23636f9bbdbSMark Cave-Ayland     mc146818_rtc_init(isa_bus, 2000, NULL);
237a72bd606SHelge Deller 
2385079892dSHelge Deller     /* Serial ports: Lasi and Dino use a 7.272727 MHz clock. */
2392da547b8SMark Cave-Ayland     serial_mm_init(addr_space, LASI_UART_HPA + 0x800, 0,
2405079892dSHelge Deller         qdev_get_gpio_in(lasi_dev, LASI_IRQ_UART_HPA), 7272727 / 16,
2415079892dSHelge Deller         serial_hd(0), DEVICE_BIG_ENDIAN);
2425079892dSHelge Deller 
2435079892dSHelge Deller     serial_mm_init(addr_space, DINO_UART_HPA + 0x800, 0,
2445079892dSHelge Deller         qdev_get_gpio_in(dino_dev, DINO_IRQ_RS232INT), 7272727 / 16,
2452da547b8SMark Cave-Ayland         serial_hd(1), DEVICE_BIG_ENDIAN);
2462da547b8SMark Cave-Ayland 
2479701e569SMark Cave-Ayland     /* Parallel port */
2489701e569SMark Cave-Ayland     parallel_mm_init(addr_space, LASI_LPT_HPA + 0x800, 0,
2499701e569SMark Cave-Ayland                      qdev_get_gpio_in(lasi_dev, LASI_IRQ_LAN_HPA),
2509701e569SMark Cave-Ayland                      parallel_hds[0]);
2519701e569SMark Cave-Ayland 
25228b71a2eSHelge Deller     /* fw_cfg configuration interface */
25328b71a2eSHelge Deller     create_fw_cfg(machine);
25428b71a2eSHelge Deller 
255a72bd606SHelge Deller     /* SCSI disk setup. */
256877eb21dSMark Cave-Ayland     dev = DEVICE(pci_create_simple(pci_bus, -1, "lsi53c895a"));
257877eb21dSMark Cave-Ayland     lsi53c8xx_handle_legacy_cmdline(dev);
258a72bd606SHelge Deller 
2594765384cSSven Schnelle     /* Graphics setup. */
2604765384cSSven Schnelle     if (machine->enable_graphics && vga_interface_type != VGA_NONE) {
261f9bcb2d6SGautam Agrawal         vga_interface_created = true;
2623e80f690SMarkus Armbruster         dev = qdev_new("artist");
2634765384cSSven Schnelle         s = SYS_BUS_DEVICE(dev);
2643c6ef471SMarkus Armbruster         sysbus_realize_and_unref(s, &error_fatal);
2654765384cSSven Schnelle         sysbus_mmio_map(s, 0, LASI_GFX_HPA);
2664765384cSSven Schnelle         sysbus_mmio_map(s, 1, ARTIST_FB_ADDR);
2674765384cSSven Schnelle     }
2684765384cSSven Schnelle 
2690e6de551SHelge Deller     /* Network setup. */
270c3c3fe47SMark Cave-Ayland     if (enable_lasi_lan()) {
271c3c3fe47SMark Cave-Ayland         lasi_82596_init(addr_space, LASI_LAN_HPA,
272c3c3fe47SMark Cave-Ayland                         qdev_get_gpio_in(lasi_dev, LASI_IRQ_LAN_HPA));
273c3c3fe47SMark Cave-Ayland     }
274c3c3fe47SMark Cave-Ayland 
275a72bd606SHelge Deller     for (i = 0; i < nb_nics; i++) {
276376b8519SHelge Deller         if (!enable_lasi_lan()) {
2779f8981a9SThomas Huth             pci_nic_init_nofail(&nd_table[i], pci_bus, mc->default_nic, NULL);
278a72bd606SHelge Deller         }
279376b8519SHelge Deller     }
280a72bd606SHelge Deller 
281d26c575cSMark Cave-Ayland     /* PS/2 Keyboard/Mouse */
28292bd278cSMark Cave-Ayland     dev = qdev_new(TYPE_LASIPS2);
28392bd278cSMark Cave-Ayland     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
28492bd278cSMark Cave-Ayland     sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0,
28592bd278cSMark Cave-Ayland                        qdev_get_gpio_in(lasi_dev, LASI_IRQ_PS2KBD_HPA));
2866479296fSMark Cave-Ayland     memory_region_add_subregion(addr_space, LASI_PS2KBD_HPA,
2876479296fSMark Cave-Ayland                                 sysbus_mmio_get_region(SYS_BUS_DEVICE(dev),
2886479296fSMark Cave-Ayland                                                        0));
2896479296fSMark Cave-Ayland     memory_region_add_subregion(addr_space, LASI_PS2KBD_HPA + 0x100,
2906479296fSMark Cave-Ayland                                 sysbus_mmio_get_region(SYS_BUS_DEVICE(dev),
2916479296fSMark Cave-Ayland                                                        1));
292d26c575cSMark Cave-Ayland 
293b28c4a64SHelge Deller     /* register power switch emulation */
294b28c4a64SHelge Deller     qemu_register_powerdown_notifier(&hppa_system_powerdown_notifier);
295b28c4a64SHelge Deller 
296a72bd606SHelge Deller     /* Load firmware.  Given that this is not "real" firmware,
297a72bd606SHelge Deller        but one explicitly written for the emulation, we might as
298a72bd606SHelge Deller        well load it directly from an ELF image.  */
299a72bd606SHelge Deller     firmware_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
300b57e3e97SPaolo Bonzini                                        machine->firmware ?: "hppa-firmware.img");
301a72bd606SHelge Deller     if (firmware_filename == NULL) {
302a72bd606SHelge Deller         error_report("no firmware provided");
303a72bd606SHelge Deller         exit(1);
304a72bd606SHelge Deller     }
305a72bd606SHelge Deller 
3064366e1dbSLiam Merwick     size = load_elf(firmware_filename, NULL, NULL, NULL,
3076cdda0ffSAleksandar Markovic                     &firmware_entry, &firmware_low, &firmware_high, NULL,
308a72bd606SHelge Deller                     true, EM_PARISC, 0, 0);
309a72bd606SHelge Deller 
310a72bd606SHelge Deller     /* Unfortunately, load_elf sign-extends reading elf32.  */
311a72bd606SHelge Deller     firmware_entry = (target_ureg)firmware_entry;
312a72bd606SHelge Deller     firmware_low = (target_ureg)firmware_low;
313a72bd606SHelge Deller     firmware_high = (target_ureg)firmware_high;
314a72bd606SHelge Deller 
315a72bd606SHelge Deller     if (size < 0) {
316a72bd606SHelge Deller         error_report("could not load firmware '%s'", firmware_filename);
317a72bd606SHelge Deller         exit(1);
318a72bd606SHelge Deller     }
319691cbbadSRichard Henderson     qemu_log_mask(CPU_LOG_PAGE, "Firmware loaded at 0x%08" PRIx64
320691cbbadSRichard Henderson                   "-0x%08" PRIx64 ", entry at 0x%08" PRIx64 ".\n",
321a72bd606SHelge Deller                   firmware_low, firmware_high, firmware_entry);
3228262863dSPhilippe Mathieu-Daudé     if (firmware_low < FIRMWARE_START || firmware_high >= FIRMWARE_END) {
323a72bd606SHelge Deller         error_report("Firmware overlaps with memory or IO space");
324a72bd606SHelge Deller         exit(1);
325a72bd606SHelge Deller     }
326a72bd606SHelge Deller     g_free(firmware_filename);
327a72bd606SHelge Deller 
328a72bd606SHelge Deller     rom_region = g_new(MemoryRegion, 1);
3296a3a2e82SIgor Mammedov     memory_region_init_ram(rom_region, NULL, "firmware",
3306a3a2e82SIgor Mammedov                            (FIRMWARE_END - FIRMWARE_START), &error_fatal);
331a72bd606SHelge Deller     memory_region_add_subregion(addr_space, FIRMWARE_START, rom_region);
332a72bd606SHelge Deller 
333a72bd606SHelge Deller     /* Load kernel */
334a72bd606SHelge Deller     if (kernel_filename) {
3354366e1dbSLiam Merwick         size = load_elf(kernel_filename, NULL, &cpu_hppa_to_phys,
3366cdda0ffSAleksandar Markovic                         NULL, &kernel_entry, &kernel_low, &kernel_high, NULL,
337a72bd606SHelge Deller                         true, EM_PARISC, 0, 0);
338a72bd606SHelge Deller 
339a72bd606SHelge Deller         /* Unfortunately, load_elf sign-extends reading elf32.  */
340a72bd606SHelge Deller         kernel_entry = (target_ureg) cpu_hppa_to_phys(NULL, kernel_entry);
341a72bd606SHelge Deller         kernel_low = (target_ureg)kernel_low;
342a72bd606SHelge Deller         kernel_high = (target_ureg)kernel_high;
343a72bd606SHelge Deller 
344a72bd606SHelge Deller         if (size < 0) {
345a72bd606SHelge Deller             error_report("could not load kernel '%s'", kernel_filename);
346a72bd606SHelge Deller             exit(1);
347a72bd606SHelge Deller         }
348691cbbadSRichard Henderson         qemu_log_mask(CPU_LOG_PAGE, "Kernel loaded at 0x%08" PRIx64
349691cbbadSRichard Henderson                       "-0x%08" PRIx64 ", entry at 0x%08" PRIx64
350c108cc59SPhilippe Mathieu-Daudé                       ", size %" PRIu64 " kB\n",
351c108cc59SPhilippe Mathieu-Daudé                       kernel_low, kernel_high, kernel_entry, size / KiB);
352a72bd606SHelge Deller 
353a72bd606SHelge Deller         if (kernel_cmdline) {
354a72bd606SHelge Deller             cpu[0]->env.gr[24] = 0x4000;
355a72bd606SHelge Deller             pstrcpy_targphys("cmdline", cpu[0]->env.gr[24],
356a72bd606SHelge Deller                              TARGET_PAGE_SIZE, kernel_cmdline);
357a72bd606SHelge Deller         }
358a72bd606SHelge Deller 
359a72bd606SHelge Deller         if (initrd_filename) {
360a72bd606SHelge Deller             ram_addr_t initrd_base;
361f3839fdaSLi Zhijian             int64_t initrd_size;
362a72bd606SHelge Deller 
363a72bd606SHelge Deller             initrd_size = get_image_size(initrd_filename);
364a72bd606SHelge Deller             if (initrd_size < 0) {
365a72bd606SHelge Deller                 error_report("could not load initial ram disk '%s'",
366a72bd606SHelge Deller                              initrd_filename);
367a72bd606SHelge Deller                 exit(1);
368a72bd606SHelge Deller             }
369a72bd606SHelge Deller 
370a72bd606SHelge Deller             /* Load the initrd image high in memory.
371a72bd606SHelge Deller                Mirror the algorithm used by palo:
372a72bd606SHelge Deller                (1) Due to sign-extension problems and PDC,
373a72bd606SHelge Deller                put the initrd no higher than 1G.
374a72bd606SHelge Deller                (2) Reserve 64k for stack.  */
375bfdf22bcSPaolo Bonzini             initrd_base = MIN(machine->ram_size, 1 * GiB);
376c108cc59SPhilippe Mathieu-Daudé             initrd_base = initrd_base - 64 * KiB;
377a72bd606SHelge Deller             initrd_base = (initrd_base - initrd_size) & TARGET_PAGE_MASK;
378a72bd606SHelge Deller 
379a72bd606SHelge Deller             if (initrd_base < kernel_high) {
380a72bd606SHelge Deller                 error_report("kernel and initial ram disk too large!");
381a72bd606SHelge Deller                 exit(1);
382a72bd606SHelge Deller             }
383a72bd606SHelge Deller 
384a72bd606SHelge Deller             load_image_targphys(initrd_filename, initrd_base, initrd_size);
385a72bd606SHelge Deller             cpu[0]->env.gr[23] = initrd_base;
386a72bd606SHelge Deller             cpu[0]->env.gr[22] = initrd_base + initrd_size;
387a72bd606SHelge Deller         }
388a72bd606SHelge Deller     }
389a72bd606SHelge Deller 
390a72bd606SHelge Deller     if (!kernel_entry) {
391a72bd606SHelge Deller         /* When booting via firmware, tell firmware if we want interactive
392a72bd606SHelge Deller          * mode (kernel_entry=1), and to boot from CD (gr[24]='d')
393a72bd606SHelge Deller          * or hard disc * (gr[24]='c').
394a72bd606SHelge Deller          */
39597ec4d21SPaolo Bonzini         kernel_entry = machine->boot_config.has_menu ? machine->boot_config.menu : 0;
39697ec4d21SPaolo Bonzini         cpu[0]->env.gr[24] = machine->boot_config.order[0];
397a72bd606SHelge Deller     }
398a72bd606SHelge Deller 
399a72bd606SHelge Deller     /* We jump to the firmware entry routine and pass the
400a72bd606SHelge Deller      * various parameters in registers. After firmware initialization,
401a72bd606SHelge Deller      * firmware will start the Linux kernel with ramdisk and cmdline.
402a72bd606SHelge Deller      */
403bfdf22bcSPaolo Bonzini     cpu[0]->env.gr[26] = machine->ram_size;
404a72bd606SHelge Deller     cpu[0]->env.gr[25] = kernel_entry;
405a72bd606SHelge Deller 
406a72bd606SHelge Deller     /* tell firmware how many SMP CPUs to present in inventory table */
407a72bd606SHelge Deller     cpu[0]->env.gr[21] = smp_cpus;
40824576007SHelge Deller 
40924576007SHelge Deller     /* tell firmware fw_cfg port */
41024576007SHelge Deller     cpu[0]->env.gr[19] = FW_CFG_IO_BASE;
411a72bd606SHelge Deller }
412a72bd606SHelge Deller 
4137966d70fSJason A. Donenfeld static void hppa_machine_reset(MachineState *ms, ShutdownCause reason)
414a72bd606SHelge Deller {
41533decbd2SLike Xu     unsigned int smp_cpus = ms->smp.cpus;
416a72bd606SHelge Deller     int i;
417a72bd606SHelge Deller 
4187966d70fSJason A. Donenfeld     qemu_devices_reset(reason);
419a72bd606SHelge Deller 
420a72bd606SHelge Deller     /* Start all CPUs at the firmware entry point.
421a72bd606SHelge Deller      *  Monarch CPU will initialize firmware, secondary CPUs
42250ba97e9SHelge Deller      *  will enter a small idle loop and wait for rendevouz. */
423a72bd606SHelge Deller     for (i = 0; i < smp_cpus; i++) {
42450ba97e9SHelge Deller         CPUState *cs = CPU(cpu[i]);
42550ba97e9SHelge Deller 
42650ba97e9SHelge Deller         cpu_set_pc(cs, firmware_entry);
42750ba97e9SHelge Deller         cpu[i]->env.psw = PSW_Q;
428a72bd606SHelge Deller         cpu[i]->env.gr[5] = CPU_HPA + i * 0x1000;
42950ba97e9SHelge Deller 
43050ba97e9SHelge Deller         cs->exception_index = -1;
43150ba97e9SHelge Deller         cs->halted = 0;
4326d1ef68cSHelge Deller 
4336d1ef68cSHelge Deller         /* clear any existing TLB and BTLB entries */
4346d1ef68cSHelge Deller         memset(cpu[i]->env.tlb, 0, sizeof(cpu[i]->env.tlb));
4356d1ef68cSHelge Deller         cpu[i]->env.tlb_last = HPPA_BTLB_ENTRIES;
436a72bd606SHelge Deller     }
437a72bd606SHelge Deller 
438a72bd606SHelge Deller     /* already initialized by machine_hppa_init()? */
439bfdf22bcSPaolo Bonzini     if (cpu[0]->env.gr[26] == ms->ram_size) {
440a72bd606SHelge Deller         return;
441a72bd606SHelge Deller     }
442a72bd606SHelge Deller 
443bfdf22bcSPaolo Bonzini     cpu[0]->env.gr[26] = ms->ram_size;
444a72bd606SHelge Deller     cpu[0]->env.gr[25] = 0; /* no firmware boot menu */
445a72bd606SHelge Deller     cpu[0]->env.gr[24] = 'c';
446a72bd606SHelge Deller     /* gr22/gr23 unused, no initrd while reboot. */
447a72bd606SHelge Deller     cpu[0]->env.gr[21] = smp_cpus;
44824576007SHelge Deller     /* tell firmware fw_cfg port */
44924576007SHelge Deller     cpu[0]->env.gr[19] = FW_CFG_IO_BASE;
450a72bd606SHelge Deller }
451a72bd606SHelge Deller 
4524a4554c6SHelge Deller static void hppa_nmi(NMIState *n, int cpu_index, Error **errp)
4534a4554c6SHelge Deller {
4544a4554c6SHelge Deller     CPUState *cs;
4554a4554c6SHelge Deller 
4564a4554c6SHelge Deller     CPU_FOREACH(cs) {
4574a4554c6SHelge Deller         cpu_interrupt(cs, CPU_INTERRUPT_NMI);
4584a4554c6SHelge Deller     }
4594a4554c6SHelge Deller }
460a72bd606SHelge Deller 
461297d4103SMark Cave-Ayland static void hppa_machine_init_class_init(ObjectClass *oc, void *data)
462813dff13SHelge Deller {
46342cc2bf6SMark Cave-Ayland     MachineClass *mc = MACHINE_CLASS(oc);
46442cc2bf6SMark Cave-Ayland     NMIClass *nc = NMI_CLASS(oc);
46542cc2bf6SMark Cave-Ayland 
4660d98fbb5SMark Cave-Ayland     mc->desc = "HPPA B160L machine";
467a72bd606SHelge Deller     mc->default_cpu_type = TYPE_HPPA_CPU;
468813dff13SHelge Deller     mc->init = machine_hppa_init;
469a72bd606SHelge Deller     mc->reset = hppa_machine_reset;
470813dff13SHelge Deller     mc->block_default_type = IF_SCSI;
471a72bd606SHelge Deller     mc->max_cpus = HPPA_MAX_CPUS;
472a72bd606SHelge Deller     mc->default_cpus = 1;
473ea0ac7f6SPhilippe Mathieu-Daudé     mc->is_default = true;
474d23b6caaSPhilippe Mathieu-Daudé     mc->default_ram_size = 512 * MiB;
475813dff13SHelge Deller     mc->default_boot_order = "cd";
4767c59c1e0SIgor Mammedov     mc->default_ram_id = "ram";
4779f8981a9SThomas Huth     mc->default_nic = "tulip";
478813dff13SHelge Deller 
4794a4554c6SHelge Deller     nc->nmi_monitor_handler = hppa_nmi;
4804a4554c6SHelge Deller }
4814a4554c6SHelge Deller 
482297d4103SMark Cave-Ayland static const TypeInfo hppa_machine_init_typeinfo = {
483c165905cSMark Cave-Ayland     .name = MACHINE_TYPE_NAME("hppa"),
484c165905cSMark Cave-Ayland     .parent = TYPE_MACHINE,
485297d4103SMark Cave-Ayland     .class_init = hppa_machine_init_class_init,
4864a4554c6SHelge Deller     .interfaces = (InterfaceInfo[]) {
4874a4554c6SHelge Deller         { TYPE_NMI },
4884a4554c6SHelge Deller         { }
4894a4554c6SHelge Deller     },
4904a4554c6SHelge Deller };
4914a4554c6SHelge Deller 
492297d4103SMark Cave-Ayland static void hppa_machine_init_register_types(void)
4934a4554c6SHelge Deller {
494297d4103SMark Cave-Ayland     type_register_static(&hppa_machine_init_typeinfo);
4954a4554c6SHelge Deller }
4964a4554c6SHelge Deller 
497297d4103SMark Cave-Ayland type_init(hppa_machine_init_register_types)
498