xref: /openbmc/qemu/hw/hppa/machine.c (revision 2da547b81968f601ca870ace0a1e1528c4402d09)
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"
19376b8519SHelge Deller #include "hw/net/lasi_82596.h"
204a4554c6SHelge Deller #include "hw/nmi.h"
210db9350eSMark Cave-Ayland #include "hw/pci-host/dino.h"
22954f6f75SMark Cave-Ayland #include "lasi.h"
23070e9a1eSMichael S. Tsirkin #include "hppa_sys.h"
24c108cc59SPhilippe Mathieu-Daudé #include "qemu/units.h"
25813dff13SHelge Deller #include "qapi/error.h"
26852c27e2SPaolo Bonzini #include "net/net.h"
27691cbbadSRichard Henderson #include "qemu/log.h"
28e07c4f44SPhilippe Mathieu-Daudé #include "net/net.h"
29813dff13SHelge Deller 
30a72bd606SHelge Deller #define MAX_IDE_BUS 2
31a72bd606SHelge Deller 
3228b71a2eSHelge Deller #define MIN_SEABIOS_HPPA_VERSION 1 /* require at least this fw version */
3328b71a2eSHelge Deller 
34b28c4a64SHelge Deller #define HPA_POWER_BUTTON (FIRMWARE_END - 0x10)
35b28c4a64SHelge Deller 
36b28c4a64SHelge Deller static void hppa_powerdown_req(Notifier *n, void *opaque)
37b28c4a64SHelge Deller {
38b28c4a64SHelge Deller     hwaddr soft_power_reg = HPA_POWER_BUTTON;
39b28c4a64SHelge Deller     uint32_t val;
40b28c4a64SHelge Deller 
41b28c4a64SHelge Deller     val = ldl_be_phys(&address_space_memory, soft_power_reg);
42b28c4a64SHelge Deller     if ((val >> 8) == 0) {
43b28c4a64SHelge Deller         /* immediately shut down when under hardware control */
44b28c4a64SHelge Deller         qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
45b28c4a64SHelge Deller         return;
46b28c4a64SHelge Deller     }
47b28c4a64SHelge Deller 
48b28c4a64SHelge Deller     /* clear bit 31 to indicate that the power switch was pressed. */
49b28c4a64SHelge Deller     val &= ~1;
50b28c4a64SHelge Deller     stl_be_phys(&address_space_memory, soft_power_reg, val);
51b28c4a64SHelge Deller }
52b28c4a64SHelge Deller 
53b28c4a64SHelge Deller static Notifier hppa_system_powerdown_notifier = {
54b28c4a64SHelge Deller     .notify = hppa_powerdown_req
55b28c4a64SHelge Deller };
56b28c4a64SHelge Deller 
57b28c4a64SHelge Deller 
58a72bd606SHelge Deller static ISABus *hppa_isa_bus(void)
59a72bd606SHelge Deller {
60a72bd606SHelge Deller     ISABus *isa_bus;
61a72bd606SHelge Deller     qemu_irq *isa_irqs;
62a72bd606SHelge Deller     MemoryRegion *isa_region;
63a72bd606SHelge Deller 
64a72bd606SHelge Deller     isa_region = g_new(MemoryRegion, 1);
65a72bd606SHelge Deller     memory_region_init_io(isa_region, NULL, &hppa_pci_ignore_ops,
66a72bd606SHelge Deller                           NULL, "isa-io", 0x800);
67a72bd606SHelge Deller     memory_region_add_subregion(get_system_memory(), IDE_HPA,
68a72bd606SHelge Deller                                 isa_region);
69a72bd606SHelge Deller 
70a72bd606SHelge Deller     isa_bus = isa_bus_new(NULL, get_system_memory(), isa_region,
71a72bd606SHelge Deller                           &error_abort);
72a72bd606SHelge Deller     isa_irqs = i8259_init(isa_bus,
73a72bd606SHelge Deller                           /* qemu_allocate_irq(dino_set_isa_irq, s, 0)); */
74a72bd606SHelge Deller                           NULL);
75a72bd606SHelge Deller     isa_bus_irqs(isa_bus, isa_irqs);
76a72bd606SHelge Deller 
77a72bd606SHelge Deller     return isa_bus;
78a72bd606SHelge Deller }
79a72bd606SHelge Deller 
80a72bd606SHelge Deller static uint64_t cpu_hppa_to_phys(void *opaque, uint64_t addr)
81a72bd606SHelge Deller {
82a72bd606SHelge Deller     addr &= (0x10000000 - 1);
83a72bd606SHelge Deller     return addr;
84a72bd606SHelge Deller }
85a72bd606SHelge Deller 
86a72bd606SHelge Deller static HPPACPU *cpu[HPPA_MAX_CPUS];
87a72bd606SHelge Deller static uint64_t firmware_entry;
88813dff13SHelge Deller 
8932ff8bf2SHelge Deller static void fw_cfg_boot_set(void *opaque, const char *boot_device,
9032ff8bf2SHelge Deller                             Error **errp)
9132ff8bf2SHelge Deller {
9232ff8bf2SHelge Deller     fw_cfg_modify_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
9332ff8bf2SHelge Deller }
9432ff8bf2SHelge Deller 
9528b71a2eSHelge Deller static FWCfgState *create_fw_cfg(MachineState *ms)
9628b71a2eSHelge Deller {
9728b71a2eSHelge Deller     FWCfgState *fw_cfg;
9828b71a2eSHelge Deller     uint64_t val;
9928b71a2eSHelge Deller 
10024576007SHelge Deller     fw_cfg = fw_cfg_init_mem(FW_CFG_IO_BASE, FW_CFG_IO_BASE + 4);
10128b71a2eSHelge Deller     fw_cfg_add_i16(fw_cfg, FW_CFG_NB_CPUS, ms->smp.cpus);
10228b71a2eSHelge Deller     fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, HPPA_MAX_CPUS);
103bfdf22bcSPaolo Bonzini     fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, ms->ram_size);
10428b71a2eSHelge Deller 
10528b71a2eSHelge Deller     val = cpu_to_le64(MIN_SEABIOS_HPPA_VERSION);
10628b71a2eSHelge Deller     fw_cfg_add_file(fw_cfg, "/etc/firmware-min-version",
10728b71a2eSHelge Deller                     g_memdup(&val, sizeof(val)), sizeof(val));
10828b71a2eSHelge Deller 
109df5c6a50SHelge Deller     val = cpu_to_le64(HPPA_TLB_ENTRIES);
110df5c6a50SHelge Deller     fw_cfg_add_file(fw_cfg, "/etc/cpu/tlb_entries",
111df5c6a50SHelge Deller                     g_memdup(&val, sizeof(val)), sizeof(val));
112df5c6a50SHelge Deller 
113df5c6a50SHelge Deller     val = cpu_to_le64(HPPA_BTLB_ENTRIES);
114df5c6a50SHelge Deller     fw_cfg_add_file(fw_cfg, "/etc/cpu/btlb_entries",
115df5c6a50SHelge Deller                     g_memdup(&val, sizeof(val)), sizeof(val));
116df5c6a50SHelge Deller 
117b28c4a64SHelge Deller     val = cpu_to_le64(HPA_POWER_BUTTON);
118b28c4a64SHelge Deller     fw_cfg_add_file(fw_cfg, "/etc/power-button-addr",
119b28c4a64SHelge Deller                     g_memdup(&val, sizeof(val)), sizeof(val));
120b28c4a64SHelge Deller 
12132ff8bf2SHelge Deller     fw_cfg_add_i16(fw_cfg, FW_CFG_BOOT_DEVICE, ms->boot_order[0]);
12232ff8bf2SHelge Deller     qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);
12332ff8bf2SHelge Deller 
12428b71a2eSHelge Deller     return fw_cfg;
12528b71a2eSHelge Deller }
12628b71a2eSHelge Deller 
1270d068996SMark Cave-Ayland static DinoState *dino_init(MemoryRegion *addr_space)
1280d068996SMark Cave-Ayland {
1290d068996SMark Cave-Ayland     DeviceState *dev;
1300d068996SMark Cave-Ayland 
1310d068996SMark Cave-Ayland     dev = qdev_new(TYPE_DINO_PCI_HOST_BRIDGE);
1320d068996SMark Cave-Ayland     object_property_set_link(OBJECT(dev), "memory-as", OBJECT(addr_space),
1330d068996SMark Cave-Ayland                              &error_fatal);
1340d068996SMark Cave-Ayland     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
1350d068996SMark Cave-Ayland 
1360d068996SMark Cave-Ayland     return DINO_PCI_HOST_BRIDGE(dev);
1370d068996SMark Cave-Ayland }
1380d068996SMark Cave-Ayland 
139813dff13SHelge Deller static void machine_hppa_init(MachineState *machine)
140813dff13SHelge Deller {
141a72bd606SHelge Deller     const char *kernel_filename = machine->kernel_filename;
142a72bd606SHelge Deller     const char *kernel_cmdline = machine->kernel_cmdline;
143a72bd606SHelge Deller     const char *initrd_filename = machine->initrd_filename;
1442683758cSMark Cave-Ayland     DeviceState *dev, *dino_dev, *lasi_dev;
145a72bd606SHelge Deller     PCIBus *pci_bus;
146a72bd606SHelge Deller     ISABus *isa_bus;
147a72bd606SHelge Deller     char *firmware_filename;
148a72bd606SHelge Deller     uint64_t firmware_low, firmware_high;
149a72bd606SHelge Deller     long size;
150a72bd606SHelge Deller     uint64_t kernel_entry = 0, kernel_low, kernel_high;
151a72bd606SHelge Deller     MemoryRegion *addr_space = get_system_memory();
152a72bd606SHelge Deller     MemoryRegion *rom_region;
153a72bd606SHelge Deller     MemoryRegion *cpu_region;
154a72bd606SHelge Deller     long i;
15533decbd2SLike Xu     unsigned int smp_cpus = machine->smp.cpus;
1564765384cSSven Schnelle     SysBusDevice *s;
157a72bd606SHelge Deller 
158a72bd606SHelge Deller     /* Create CPUs.  */
159a72bd606SHelge Deller     for (i = 0; i < smp_cpus; i++) {
160266a880eSPaolo Bonzini         char *name = g_strdup_printf("cpu%ld-io-eir", i);
161a72bd606SHelge Deller         cpu[i] = HPPA_CPU(cpu_create(machine->cpu_type));
162a72bd606SHelge Deller 
163a72bd606SHelge Deller         cpu_region = g_new(MemoryRegion, 1);
164a72bd606SHelge Deller         memory_region_init_io(cpu_region, OBJECT(cpu[i]), &hppa_io_eir_ops,
165266a880eSPaolo Bonzini                               cpu[i], name, 4);
166a72bd606SHelge Deller         memory_region_add_subregion(addr_space, CPU_HPA + i * 0x1000,
167a72bd606SHelge Deller                                     cpu_region);
168266a880eSPaolo Bonzini         g_free(name);
169813dff13SHelge Deller     }
170813dff13SHelge Deller 
171a72bd606SHelge Deller     /* Main memory region. */
172b7746b11SPhilippe Mathieu-Daudé     if (machine->ram_size > 3 * GiB) {
173b7746b11SPhilippe Mathieu-Daudé         error_report("RAM size is currently restricted to 3GB");
174b7746b11SPhilippe Mathieu-Daudé         exit(EXIT_FAILURE);
175b7746b11SPhilippe Mathieu-Daudé     }
1767c59c1e0SIgor Mammedov     memory_region_add_subregion_overlap(addr_space, 0, machine->ram, -1);
1777c59c1e0SIgor Mammedov 
178a72bd606SHelge Deller 
179376b8519SHelge Deller     /* Init Lasi chip */
180954f6f75SMark Cave-Ayland     lasi_dev = DEVICE(lasi_initfn(addr_space));
1812683758cSMark Cave-Ayland     memory_region_add_subregion(addr_space, LASI_HPA,
1822683758cSMark Cave-Ayland                                 sysbus_mmio_get_region(
1832683758cSMark Cave-Ayland                                     SYS_BUS_DEVICE(lasi_dev), 0));
184376b8519SHelge Deller 
185a72bd606SHelge Deller     /* Init Dino (PCI host bus chip).  */
18636f9bbdbSMark Cave-Ayland     dino_dev = DEVICE(dino_init(addr_space));
187efdb3ce2SMark Cave-Ayland     memory_region_add_subregion(addr_space, DINO_HPA,
188efdb3ce2SMark Cave-Ayland                                 sysbus_mmio_get_region(
189efdb3ce2SMark Cave-Ayland                                     SYS_BUS_DEVICE(dino_dev), 0));
19005245dafSMark Cave-Ayland     pci_bus = PCI_BUS(qdev_get_child_bus(dino_dev, "pci"));
191a72bd606SHelge Deller     assert(pci_bus);
192a72bd606SHelge Deller 
193a72bd606SHelge Deller     /* Create ISA bus. */
194a72bd606SHelge Deller     isa_bus = hppa_isa_bus();
195a72bd606SHelge Deller     assert(isa_bus);
196a72bd606SHelge Deller 
197a72bd606SHelge Deller     /* Realtime clock, used by firmware for PDC_TOD call. */
19836f9bbdbSMark Cave-Ayland     mc146818_rtc_init(isa_bus, 2000, NULL);
199a72bd606SHelge Deller 
200a72bd606SHelge Deller     /* Serial code setup.  */
2019bca0edbSPeter Maydell     if (serial_hd(0)) {
202a72bd606SHelge Deller         uint32_t addr = DINO_UART_HPA + 0x800;
203a4b74c19SMark Cave-Ayland         serial_mm_init(addr_space, addr, 0,
204a4b74c19SMark Cave-Ayland                        qdev_get_gpio_in(dino_dev, DINO_IRQ_RS232INT),
2059bca0edbSPeter Maydell                        115200, serial_hd(0), DEVICE_BIG_ENDIAN);
206a72bd606SHelge Deller     }
207a72bd606SHelge Deller 
208*2da547b8SMark Cave-Ayland     if (serial_hd(1)) {
209*2da547b8SMark Cave-Ayland         /* Serial port */
210*2da547b8SMark Cave-Ayland         serial_mm_init(addr_space, LASI_UART_HPA + 0x800, 0,
211*2da547b8SMark Cave-Ayland                 qdev_get_gpio_in(lasi_dev, LASI_IRQ_UART_HPA), 8000000 / 16,
212*2da547b8SMark Cave-Ayland                 serial_hd(1), DEVICE_BIG_ENDIAN);
213*2da547b8SMark Cave-Ayland     }
214*2da547b8SMark Cave-Ayland 
2159701e569SMark Cave-Ayland     /* Parallel port */
2169701e569SMark Cave-Ayland     parallel_mm_init(addr_space, LASI_LPT_HPA + 0x800, 0,
2179701e569SMark Cave-Ayland                      qdev_get_gpio_in(lasi_dev, LASI_IRQ_LAN_HPA),
2189701e569SMark Cave-Ayland                      parallel_hds[0]);
2199701e569SMark Cave-Ayland 
22028b71a2eSHelge Deller     /* fw_cfg configuration interface */
22128b71a2eSHelge Deller     create_fw_cfg(machine);
22228b71a2eSHelge Deller 
223a72bd606SHelge Deller     /* SCSI disk setup. */
224877eb21dSMark Cave-Ayland     dev = DEVICE(pci_create_simple(pci_bus, -1, "lsi53c895a"));
225877eb21dSMark Cave-Ayland     lsi53c8xx_handle_legacy_cmdline(dev);
226a72bd606SHelge Deller 
2274765384cSSven Schnelle     /* Graphics setup. */
2284765384cSSven Schnelle     if (machine->enable_graphics && vga_interface_type != VGA_NONE) {
2293e80f690SMarkus Armbruster         dev = qdev_new("artist");
2304765384cSSven Schnelle         s = SYS_BUS_DEVICE(dev);
2313c6ef471SMarkus Armbruster         sysbus_realize_and_unref(s, &error_fatal);
2324765384cSSven Schnelle         sysbus_mmio_map(s, 0, LASI_GFX_HPA);
2334765384cSSven Schnelle         sysbus_mmio_map(s, 1, ARTIST_FB_ADDR);
2344765384cSSven Schnelle     }
2354765384cSSven Schnelle 
2360e6de551SHelge Deller     /* Network setup. */
237c3c3fe47SMark Cave-Ayland     if (enable_lasi_lan()) {
238c3c3fe47SMark Cave-Ayland         lasi_82596_init(addr_space, LASI_LAN_HPA,
239c3c3fe47SMark Cave-Ayland                         qdev_get_gpio_in(lasi_dev, LASI_IRQ_LAN_HPA));
240c3c3fe47SMark Cave-Ayland     }
241c3c3fe47SMark Cave-Ayland 
242a72bd606SHelge Deller     for (i = 0; i < nb_nics; i++) {
243376b8519SHelge Deller         if (!enable_lasi_lan()) {
2440e6de551SHelge Deller             pci_nic_init_nofail(&nd_table[i], pci_bus, "tulip", NULL);
245a72bd606SHelge Deller         }
246376b8519SHelge Deller     }
247a72bd606SHelge Deller 
248b28c4a64SHelge Deller     /* register power switch emulation */
249b28c4a64SHelge Deller     qemu_register_powerdown_notifier(&hppa_system_powerdown_notifier);
250b28c4a64SHelge Deller 
251a72bd606SHelge Deller     /* Load firmware.  Given that this is not "real" firmware,
252a72bd606SHelge Deller        but one explicitly written for the emulation, we might as
253a72bd606SHelge Deller        well load it directly from an ELF image.  */
254a72bd606SHelge Deller     firmware_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
255b57e3e97SPaolo Bonzini                                        machine->firmware ?: "hppa-firmware.img");
256a72bd606SHelge Deller     if (firmware_filename == NULL) {
257a72bd606SHelge Deller         error_report("no firmware provided");
258a72bd606SHelge Deller         exit(1);
259a72bd606SHelge Deller     }
260a72bd606SHelge Deller 
2614366e1dbSLiam Merwick     size = load_elf(firmware_filename, NULL, NULL, NULL,
2626cdda0ffSAleksandar Markovic                     &firmware_entry, &firmware_low, &firmware_high, NULL,
263a72bd606SHelge Deller                     true, EM_PARISC, 0, 0);
264a72bd606SHelge Deller 
265a72bd606SHelge Deller     /* Unfortunately, load_elf sign-extends reading elf32.  */
266a72bd606SHelge Deller     firmware_entry = (target_ureg)firmware_entry;
267a72bd606SHelge Deller     firmware_low = (target_ureg)firmware_low;
268a72bd606SHelge Deller     firmware_high = (target_ureg)firmware_high;
269a72bd606SHelge Deller 
270a72bd606SHelge Deller     if (size < 0) {
271a72bd606SHelge Deller         error_report("could not load firmware '%s'", firmware_filename);
272a72bd606SHelge Deller         exit(1);
273a72bd606SHelge Deller     }
274691cbbadSRichard Henderson     qemu_log_mask(CPU_LOG_PAGE, "Firmware loaded at 0x%08" PRIx64
275691cbbadSRichard Henderson                   "-0x%08" PRIx64 ", entry at 0x%08" PRIx64 ".\n",
276a72bd606SHelge Deller                   firmware_low, firmware_high, firmware_entry);
2778262863dSPhilippe Mathieu-Daudé     if (firmware_low < FIRMWARE_START || firmware_high >= FIRMWARE_END) {
278a72bd606SHelge Deller         error_report("Firmware overlaps with memory or IO space");
279a72bd606SHelge Deller         exit(1);
280a72bd606SHelge Deller     }
281a72bd606SHelge Deller     g_free(firmware_filename);
282a72bd606SHelge Deller 
283a72bd606SHelge Deller     rom_region = g_new(MemoryRegion, 1);
2846a3a2e82SIgor Mammedov     memory_region_init_ram(rom_region, NULL, "firmware",
2856a3a2e82SIgor Mammedov                            (FIRMWARE_END - FIRMWARE_START), &error_fatal);
286a72bd606SHelge Deller     memory_region_add_subregion(addr_space, FIRMWARE_START, rom_region);
287a72bd606SHelge Deller 
288a72bd606SHelge Deller     /* Load kernel */
289a72bd606SHelge Deller     if (kernel_filename) {
2904366e1dbSLiam Merwick         size = load_elf(kernel_filename, NULL, &cpu_hppa_to_phys,
2916cdda0ffSAleksandar Markovic                         NULL, &kernel_entry, &kernel_low, &kernel_high, NULL,
292a72bd606SHelge Deller                         true, EM_PARISC, 0, 0);
293a72bd606SHelge Deller 
294a72bd606SHelge Deller         /* Unfortunately, load_elf sign-extends reading elf32.  */
295a72bd606SHelge Deller         kernel_entry = (target_ureg) cpu_hppa_to_phys(NULL, kernel_entry);
296a72bd606SHelge Deller         kernel_low = (target_ureg)kernel_low;
297a72bd606SHelge Deller         kernel_high = (target_ureg)kernel_high;
298a72bd606SHelge Deller 
299a72bd606SHelge Deller         if (size < 0) {
300a72bd606SHelge Deller             error_report("could not load kernel '%s'", kernel_filename);
301a72bd606SHelge Deller             exit(1);
302a72bd606SHelge Deller         }
303691cbbadSRichard Henderson         qemu_log_mask(CPU_LOG_PAGE, "Kernel loaded at 0x%08" PRIx64
304691cbbadSRichard Henderson                       "-0x%08" PRIx64 ", entry at 0x%08" PRIx64
305c108cc59SPhilippe Mathieu-Daudé                       ", size %" PRIu64 " kB\n",
306c108cc59SPhilippe Mathieu-Daudé                       kernel_low, kernel_high, kernel_entry, size / KiB);
307a72bd606SHelge Deller 
308a72bd606SHelge Deller         if (kernel_cmdline) {
309a72bd606SHelge Deller             cpu[0]->env.gr[24] = 0x4000;
310a72bd606SHelge Deller             pstrcpy_targphys("cmdline", cpu[0]->env.gr[24],
311a72bd606SHelge Deller                              TARGET_PAGE_SIZE, kernel_cmdline);
312a72bd606SHelge Deller         }
313a72bd606SHelge Deller 
314a72bd606SHelge Deller         if (initrd_filename) {
315a72bd606SHelge Deller             ram_addr_t initrd_base;
316f3839fdaSLi Zhijian             int64_t initrd_size;
317a72bd606SHelge Deller 
318a72bd606SHelge Deller             initrd_size = get_image_size(initrd_filename);
319a72bd606SHelge Deller             if (initrd_size < 0) {
320a72bd606SHelge Deller                 error_report("could not load initial ram disk '%s'",
321a72bd606SHelge Deller                              initrd_filename);
322a72bd606SHelge Deller                 exit(1);
323a72bd606SHelge Deller             }
324a72bd606SHelge Deller 
325a72bd606SHelge Deller             /* Load the initrd image high in memory.
326a72bd606SHelge Deller                Mirror the algorithm used by palo:
327a72bd606SHelge Deller                (1) Due to sign-extension problems and PDC,
328a72bd606SHelge Deller                put the initrd no higher than 1G.
329a72bd606SHelge Deller                (2) Reserve 64k for stack.  */
330bfdf22bcSPaolo Bonzini             initrd_base = MIN(machine->ram_size, 1 * GiB);
331c108cc59SPhilippe Mathieu-Daudé             initrd_base = initrd_base - 64 * KiB;
332a72bd606SHelge Deller             initrd_base = (initrd_base - initrd_size) & TARGET_PAGE_MASK;
333a72bd606SHelge Deller 
334a72bd606SHelge Deller             if (initrd_base < kernel_high) {
335a72bd606SHelge Deller                 error_report("kernel and initial ram disk too large!");
336a72bd606SHelge Deller                 exit(1);
337a72bd606SHelge Deller             }
338a72bd606SHelge Deller 
339a72bd606SHelge Deller             load_image_targphys(initrd_filename, initrd_base, initrd_size);
340a72bd606SHelge Deller             cpu[0]->env.gr[23] = initrd_base;
341a72bd606SHelge Deller             cpu[0]->env.gr[22] = initrd_base + initrd_size;
342a72bd606SHelge Deller         }
343a72bd606SHelge Deller     }
344a72bd606SHelge Deller 
345a72bd606SHelge Deller     if (!kernel_entry) {
346a72bd606SHelge Deller         /* When booting via firmware, tell firmware if we want interactive
347a72bd606SHelge Deller          * mode (kernel_entry=1), and to boot from CD (gr[24]='d')
348a72bd606SHelge Deller          * or hard disc * (gr[24]='c').
349a72bd606SHelge Deller          */
350a72bd606SHelge Deller         kernel_entry = boot_menu ? 1 : 0;
351a72bd606SHelge Deller         cpu[0]->env.gr[24] = machine->boot_order[0];
352a72bd606SHelge Deller     }
353a72bd606SHelge Deller 
354a72bd606SHelge Deller     /* We jump to the firmware entry routine and pass the
355a72bd606SHelge Deller      * various parameters in registers. After firmware initialization,
356a72bd606SHelge Deller      * firmware will start the Linux kernel with ramdisk and cmdline.
357a72bd606SHelge Deller      */
358bfdf22bcSPaolo Bonzini     cpu[0]->env.gr[26] = machine->ram_size;
359a72bd606SHelge Deller     cpu[0]->env.gr[25] = kernel_entry;
360a72bd606SHelge Deller 
361a72bd606SHelge Deller     /* tell firmware how many SMP CPUs to present in inventory table */
362a72bd606SHelge Deller     cpu[0]->env.gr[21] = smp_cpus;
36324576007SHelge Deller 
36424576007SHelge Deller     /* tell firmware fw_cfg port */
36524576007SHelge Deller     cpu[0]->env.gr[19] = FW_CFG_IO_BASE;
366a72bd606SHelge Deller }
367a72bd606SHelge Deller 
368a0628599SLike Xu static void hppa_machine_reset(MachineState *ms)
369a72bd606SHelge Deller {
37033decbd2SLike Xu     unsigned int smp_cpus = ms->smp.cpus;
371a72bd606SHelge Deller     int i;
372a72bd606SHelge Deller 
373a72bd606SHelge Deller     qemu_devices_reset();
374a72bd606SHelge Deller 
375a72bd606SHelge Deller     /* Start all CPUs at the firmware entry point.
376a72bd606SHelge Deller      *  Monarch CPU will initialize firmware, secondary CPUs
377a72bd606SHelge Deller      *  will enter a small idle look and wait for rendevouz. */
378a72bd606SHelge Deller     for (i = 0; i < smp_cpus; i++) {
379a72bd606SHelge Deller         cpu_set_pc(CPU(cpu[i]), firmware_entry);
380a72bd606SHelge Deller         cpu[i]->env.gr[5] = CPU_HPA + i * 0x1000;
381a72bd606SHelge Deller     }
382a72bd606SHelge Deller 
383a72bd606SHelge Deller     /* already initialized by machine_hppa_init()? */
384bfdf22bcSPaolo Bonzini     if (cpu[0]->env.gr[26] == ms->ram_size) {
385a72bd606SHelge Deller         return;
386a72bd606SHelge Deller     }
387a72bd606SHelge Deller 
388bfdf22bcSPaolo Bonzini     cpu[0]->env.gr[26] = ms->ram_size;
389a72bd606SHelge Deller     cpu[0]->env.gr[25] = 0; /* no firmware boot menu */
390a72bd606SHelge Deller     cpu[0]->env.gr[24] = 'c';
391a72bd606SHelge Deller     /* gr22/gr23 unused, no initrd while reboot. */
392a72bd606SHelge Deller     cpu[0]->env.gr[21] = smp_cpus;
39324576007SHelge Deller     /* tell firmware fw_cfg port */
39424576007SHelge Deller     cpu[0]->env.gr[19] = FW_CFG_IO_BASE;
395a72bd606SHelge Deller }
396a72bd606SHelge Deller 
3974a4554c6SHelge Deller static void hppa_nmi(NMIState *n, int cpu_index, Error **errp)
3984a4554c6SHelge Deller {
3994a4554c6SHelge Deller     CPUState *cs;
4004a4554c6SHelge Deller 
4014a4554c6SHelge Deller     CPU_FOREACH(cs) {
4024a4554c6SHelge Deller         cpu_interrupt(cs, CPU_INTERRUPT_NMI);
4034a4554c6SHelge Deller     }
4044a4554c6SHelge Deller }
405a72bd606SHelge Deller 
406813dff13SHelge Deller static void machine_hppa_machine_init(MachineClass *mc)
407813dff13SHelge Deller {
4080d98fbb5SMark Cave-Ayland     mc->desc = "HPPA B160L machine";
409a72bd606SHelge Deller     mc->default_cpu_type = TYPE_HPPA_CPU;
410813dff13SHelge Deller     mc->init = machine_hppa_init;
411a72bd606SHelge Deller     mc->reset = hppa_machine_reset;
412813dff13SHelge Deller     mc->block_default_type = IF_SCSI;
413a72bd606SHelge Deller     mc->max_cpus = HPPA_MAX_CPUS;
414a72bd606SHelge Deller     mc->default_cpus = 1;
415ea0ac7f6SPhilippe Mathieu-Daudé     mc->is_default = true;
416d23b6caaSPhilippe Mathieu-Daudé     mc->default_ram_size = 512 * MiB;
417813dff13SHelge Deller     mc->default_boot_order = "cd";
4187c59c1e0SIgor Mammedov     mc->default_ram_id = "ram";
419813dff13SHelge Deller }
420813dff13SHelge Deller 
4214a4554c6SHelge Deller static void machine_hppa_machine_init_class_init(ObjectClass *oc, void *data)
4224a4554c6SHelge Deller {
4234a4554c6SHelge Deller     MachineClass *mc = MACHINE_CLASS(oc);
4244a4554c6SHelge Deller     machine_hppa_machine_init(mc);
4254a4554c6SHelge Deller 
4264a4554c6SHelge Deller     NMIClass *nc = NMI_CLASS(oc);
4274a4554c6SHelge Deller     nc->nmi_monitor_handler = hppa_nmi;
4284a4554c6SHelge Deller }
4294a4554c6SHelge Deller 
4304a4554c6SHelge Deller static const TypeInfo machine_hppa_machine_init_typeinfo = {
4314a4554c6SHelge Deller     .name = ("hppa" "-machine"),
4324a4554c6SHelge Deller     .parent = "machine",
4334a4554c6SHelge Deller     .class_init = machine_hppa_machine_init_class_init,
4344a4554c6SHelge Deller     .interfaces = (InterfaceInfo[]) {
4354a4554c6SHelge Deller         { TYPE_NMI },
4364a4554c6SHelge Deller         { }
4374a4554c6SHelge Deller     },
4384a4554c6SHelge Deller };
4394a4554c6SHelge Deller 
4404a4554c6SHelge Deller static void machine_hppa_machine_init_register_types(void)
4414a4554c6SHelge Deller {
4424a4554c6SHelge Deller     type_register_static(&machine_hppa_machine_init_typeinfo);
4434a4554c6SHelge Deller }
4444a4554c6SHelge Deller 
4454a4554c6SHelge Deller type_init(machine_hppa_machine_init_register_types)
446