xref: /openbmc/qemu/hw/hppa/machine.c (revision 691cbbad0b977686c1176f2d907aa0b15ef2beb9)
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"
7813dff13SHelge Deller #include "qemu-common.h"
8813dff13SHelge Deller #include "cpu.h"
9813dff13SHelge Deller #include "hw/hw.h"
10813dff13SHelge Deller #include "elf.h"
11813dff13SHelge Deller #include "hw/loader.h"
12813dff13SHelge Deller #include "hw/boards.h"
13813dff13SHelge Deller #include "qemu/error-report.h"
14813dff13SHelge Deller #include "sysemu/sysemu.h"
15813dff13SHelge Deller #include "hw/timer/mc146818rtc.h"
16813dff13SHelge Deller #include "hw/ide.h"
17813dff13SHelge Deller #include "hw/timer/i8254.h"
18813dff13SHelge Deller #include "hw/char/serial.h"
19a72bd606SHelge Deller #include "hw/hppa/hppa_sys.h"
20813dff13SHelge Deller #include "qemu/cutils.h"
21813dff13SHelge Deller #include "qapi/error.h"
22*691cbbadSRichard Henderson #include "qemu/log.h"
23813dff13SHelge Deller 
24a72bd606SHelge Deller #define MAX_IDE_BUS 2
25a72bd606SHelge Deller 
26a72bd606SHelge Deller static ISABus *hppa_isa_bus(void)
27a72bd606SHelge Deller {
28a72bd606SHelge Deller     ISABus *isa_bus;
29a72bd606SHelge Deller     qemu_irq *isa_irqs;
30a72bd606SHelge Deller     MemoryRegion *isa_region;
31a72bd606SHelge Deller 
32a72bd606SHelge Deller     isa_region = g_new(MemoryRegion, 1);
33a72bd606SHelge Deller     memory_region_init_io(isa_region, NULL, &hppa_pci_ignore_ops,
34a72bd606SHelge Deller                           NULL, "isa-io", 0x800);
35a72bd606SHelge Deller     memory_region_add_subregion(get_system_memory(), IDE_HPA,
36a72bd606SHelge Deller                                 isa_region);
37a72bd606SHelge Deller 
38a72bd606SHelge Deller     isa_bus = isa_bus_new(NULL, get_system_memory(), isa_region,
39a72bd606SHelge Deller                           &error_abort);
40a72bd606SHelge Deller     isa_irqs = i8259_init(isa_bus,
41a72bd606SHelge Deller                           /* qemu_allocate_irq(dino_set_isa_irq, s, 0)); */
42a72bd606SHelge Deller                           NULL);
43a72bd606SHelge Deller     isa_bus_irqs(isa_bus, isa_irqs);
44a72bd606SHelge Deller 
45a72bd606SHelge Deller     return isa_bus;
46a72bd606SHelge Deller }
47a72bd606SHelge Deller 
48a72bd606SHelge Deller static uint64_t cpu_hppa_to_phys(void *opaque, uint64_t addr)
49a72bd606SHelge Deller {
50a72bd606SHelge Deller     addr &= (0x10000000 - 1);
51a72bd606SHelge Deller     return addr;
52a72bd606SHelge Deller }
53a72bd606SHelge Deller 
54a72bd606SHelge Deller static HPPACPU *cpu[HPPA_MAX_CPUS];
55a72bd606SHelge Deller static uint64_t firmware_entry;
56813dff13SHelge Deller 
57813dff13SHelge Deller static void machine_hppa_init(MachineState *machine)
58813dff13SHelge Deller {
59a72bd606SHelge Deller     const char *kernel_filename = machine->kernel_filename;
60a72bd606SHelge Deller     const char *kernel_cmdline = machine->kernel_cmdline;
61a72bd606SHelge Deller     const char *initrd_filename = machine->initrd_filename;
62a72bd606SHelge Deller     PCIBus *pci_bus;
63a72bd606SHelge Deller     ISABus *isa_bus;
64a72bd606SHelge Deller     qemu_irq rtc_irq, serial_irq;
65a72bd606SHelge Deller     char *firmware_filename;
66a72bd606SHelge Deller     uint64_t firmware_low, firmware_high;
67a72bd606SHelge Deller     long size;
68a72bd606SHelge Deller     uint64_t kernel_entry = 0, kernel_low, kernel_high;
69a72bd606SHelge Deller     MemoryRegion *addr_space = get_system_memory();
70a72bd606SHelge Deller     MemoryRegion *rom_region;
71a72bd606SHelge Deller     MemoryRegion *ram_region;
72a72bd606SHelge Deller     MemoryRegion *cpu_region;
73a72bd606SHelge Deller     long i;
74a72bd606SHelge Deller 
75a72bd606SHelge Deller     ram_size = machine->ram_size;
76a72bd606SHelge Deller 
77a72bd606SHelge Deller     /* Create CPUs.  */
78a72bd606SHelge Deller     for (i = 0; i < smp_cpus; i++) {
79a72bd606SHelge Deller         cpu[i] = HPPA_CPU(cpu_create(machine->cpu_type));
80a72bd606SHelge Deller 
81a72bd606SHelge Deller         cpu_region = g_new(MemoryRegion, 1);
82a72bd606SHelge Deller         memory_region_init_io(cpu_region, OBJECT(cpu[i]), &hppa_io_eir_ops,
83a72bd606SHelge Deller                               cpu[i], g_strdup_printf("cpu%ld-io-eir", i), 4);
84a72bd606SHelge Deller         memory_region_add_subregion(addr_space, CPU_HPA + i * 0x1000,
85a72bd606SHelge Deller                                     cpu_region);
86813dff13SHelge Deller     }
87813dff13SHelge Deller 
88a72bd606SHelge Deller     /* Limit main memory. */
89a72bd606SHelge Deller     if (ram_size > FIRMWARE_START) {
90a72bd606SHelge Deller         machine->ram_size = ram_size = FIRMWARE_START;
91a72bd606SHelge Deller     }
92a72bd606SHelge Deller 
93a72bd606SHelge Deller     /* Main memory region. */
94a72bd606SHelge Deller     ram_region = g_new(MemoryRegion, 1);
95a72bd606SHelge Deller     memory_region_allocate_system_memory(ram_region, OBJECT(machine),
96a72bd606SHelge Deller                                          "ram", ram_size);
97a72bd606SHelge Deller     memory_region_add_subregion(addr_space, 0, ram_region);
98a72bd606SHelge Deller 
99a72bd606SHelge Deller     /* Init Dino (PCI host bus chip).  */
100a72bd606SHelge Deller     pci_bus = dino_init(addr_space, &rtc_irq, &serial_irq);
101a72bd606SHelge Deller     assert(pci_bus);
102a72bd606SHelge Deller 
103a72bd606SHelge Deller     /* Create ISA bus. */
104a72bd606SHelge Deller     isa_bus = hppa_isa_bus();
105a72bd606SHelge Deller     assert(isa_bus);
106a72bd606SHelge Deller 
107a72bd606SHelge Deller     /* Realtime clock, used by firmware for PDC_TOD call. */
108a72bd606SHelge Deller     mc146818_rtc_init(isa_bus, 2000, rtc_irq);
109a72bd606SHelge Deller 
110a72bd606SHelge Deller     /* Serial code setup.  */
111a72bd606SHelge Deller     if (serial_hds[0]) {
112a72bd606SHelge Deller         uint32_t addr = DINO_UART_HPA + 0x800;
113a72bd606SHelge Deller         serial_mm_init(addr_space, addr, 0, serial_irq,
114a72bd606SHelge Deller                        115200, serial_hds[0], DEVICE_BIG_ENDIAN);
115a72bd606SHelge Deller     }
116a72bd606SHelge Deller 
117a72bd606SHelge Deller     /* SCSI disk setup. */
118a72bd606SHelge Deller     lsi53c895a_create(pci_bus);
119a72bd606SHelge Deller 
120a72bd606SHelge Deller     /* Network setup.  e1000 is good enough, failing Tulip support.  */
121a72bd606SHelge Deller     for (i = 0; i < nb_nics; i++) {
122a72bd606SHelge Deller         pci_nic_init_nofail(&nd_table[i], pci_bus, "e1000", NULL);
123a72bd606SHelge Deller     }
124a72bd606SHelge Deller 
125a72bd606SHelge Deller     /* Load firmware.  Given that this is not "real" firmware,
126a72bd606SHelge Deller        but one explicitly written for the emulation, we might as
127a72bd606SHelge Deller        well load it directly from an ELF image.  */
128a72bd606SHelge Deller     firmware_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
129a72bd606SHelge Deller                                        bios_name ? bios_name :
130a72bd606SHelge Deller                                        "hppa-firmware.img");
131a72bd606SHelge Deller     if (firmware_filename == NULL) {
132a72bd606SHelge Deller         error_report("no firmware provided");
133a72bd606SHelge Deller         exit(1);
134a72bd606SHelge Deller     }
135a72bd606SHelge Deller 
136a72bd606SHelge Deller     size = load_elf(firmware_filename, NULL,
137a72bd606SHelge Deller                     NULL, &firmware_entry, &firmware_low, &firmware_high,
138a72bd606SHelge Deller                     true, EM_PARISC, 0, 0);
139a72bd606SHelge Deller 
140a72bd606SHelge Deller     /* Unfortunately, load_elf sign-extends reading elf32.  */
141a72bd606SHelge Deller     firmware_entry = (target_ureg)firmware_entry;
142a72bd606SHelge Deller     firmware_low = (target_ureg)firmware_low;
143a72bd606SHelge Deller     firmware_high = (target_ureg)firmware_high;
144a72bd606SHelge Deller 
145a72bd606SHelge Deller     if (size < 0) {
146a72bd606SHelge Deller         error_report("could not load firmware '%s'", firmware_filename);
147a72bd606SHelge Deller         exit(1);
148a72bd606SHelge Deller     }
149*691cbbadSRichard Henderson     qemu_log_mask(CPU_LOG_PAGE, "Firmware loaded at 0x%08" PRIx64
150*691cbbadSRichard Henderson                   "-0x%08" PRIx64 ", entry at 0x%08" PRIx64 ".\n",
151a72bd606SHelge Deller                   firmware_low, firmware_high, firmware_entry);
152a72bd606SHelge Deller     if (firmware_low < ram_size || firmware_high >= FIRMWARE_END) {
153a72bd606SHelge Deller         error_report("Firmware overlaps with memory or IO space");
154a72bd606SHelge Deller         exit(1);
155a72bd606SHelge Deller     }
156a72bd606SHelge Deller     g_free(firmware_filename);
157a72bd606SHelge Deller 
158a72bd606SHelge Deller     rom_region = g_new(MemoryRegion, 1);
159a72bd606SHelge Deller     memory_region_allocate_system_memory(rom_region, OBJECT(machine),
160a72bd606SHelge Deller                                          "firmware",
161a72bd606SHelge Deller                                          (FIRMWARE_END - FIRMWARE_START));
162a72bd606SHelge Deller     memory_region_add_subregion(addr_space, FIRMWARE_START, rom_region);
163a72bd606SHelge Deller 
164a72bd606SHelge Deller     /* Load kernel */
165a72bd606SHelge Deller     if (kernel_filename) {
166a72bd606SHelge Deller         size = load_elf(kernel_filename, &cpu_hppa_to_phys,
167a72bd606SHelge Deller                         NULL, &kernel_entry, &kernel_low, &kernel_high,
168a72bd606SHelge Deller                         true, EM_PARISC, 0, 0);
169a72bd606SHelge Deller 
170a72bd606SHelge Deller         /* Unfortunately, load_elf sign-extends reading elf32.  */
171a72bd606SHelge Deller         kernel_entry = (target_ureg) cpu_hppa_to_phys(NULL, kernel_entry);
172a72bd606SHelge Deller         kernel_low = (target_ureg)kernel_low;
173a72bd606SHelge Deller         kernel_high = (target_ureg)kernel_high;
174a72bd606SHelge Deller 
175a72bd606SHelge Deller         if (size < 0) {
176a72bd606SHelge Deller             error_report("could not load kernel '%s'", kernel_filename);
177a72bd606SHelge Deller             exit(1);
178a72bd606SHelge Deller         }
179*691cbbadSRichard Henderson         qemu_log_mask(CPU_LOG_PAGE, "Kernel loaded at 0x%08" PRIx64
180*691cbbadSRichard Henderson                       "-0x%08" PRIx64 ", entry at 0x%08" PRIx64
181*691cbbadSRichard Henderson                       ", size %ld kB.\n",
182a72bd606SHelge Deller                       kernel_low, kernel_high, kernel_entry, size / 1024);
183a72bd606SHelge Deller 
184a72bd606SHelge Deller         if (kernel_cmdline) {
185a72bd606SHelge Deller             cpu[0]->env.gr[24] = 0x4000;
186a72bd606SHelge Deller             pstrcpy_targphys("cmdline", cpu[0]->env.gr[24],
187a72bd606SHelge Deller                              TARGET_PAGE_SIZE, kernel_cmdline);
188a72bd606SHelge Deller         }
189a72bd606SHelge Deller 
190a72bd606SHelge Deller         if (initrd_filename) {
191a72bd606SHelge Deller             ram_addr_t initrd_base;
192a72bd606SHelge Deller             long initrd_size;
193a72bd606SHelge Deller 
194a72bd606SHelge Deller             initrd_size = get_image_size(initrd_filename);
195a72bd606SHelge Deller             if (initrd_size < 0) {
196a72bd606SHelge Deller                 error_report("could not load initial ram disk '%s'",
197a72bd606SHelge Deller                              initrd_filename);
198a72bd606SHelge Deller                 exit(1);
199a72bd606SHelge Deller             }
200a72bd606SHelge Deller 
201a72bd606SHelge Deller             /* Load the initrd image high in memory.
202a72bd606SHelge Deller                Mirror the algorithm used by palo:
203a72bd606SHelge Deller                (1) Due to sign-extension problems and PDC,
204a72bd606SHelge Deller                put the initrd no higher than 1G.
205a72bd606SHelge Deller                (2) Reserve 64k for stack.  */
206a72bd606SHelge Deller             initrd_base = MIN(ram_size, 1024 * 1024 * 1024);
207a72bd606SHelge Deller             initrd_base = initrd_base - 64 * 1024;
208a72bd606SHelge Deller             initrd_base = (initrd_base - initrd_size) & TARGET_PAGE_MASK;
209a72bd606SHelge Deller 
210a72bd606SHelge Deller             if (initrd_base < kernel_high) {
211a72bd606SHelge Deller                 error_report("kernel and initial ram disk too large!");
212a72bd606SHelge Deller                 exit(1);
213a72bd606SHelge Deller             }
214a72bd606SHelge Deller 
215a72bd606SHelge Deller             load_image_targphys(initrd_filename, initrd_base, initrd_size);
216a72bd606SHelge Deller             cpu[0]->env.gr[23] = initrd_base;
217a72bd606SHelge Deller             cpu[0]->env.gr[22] = initrd_base + initrd_size;
218a72bd606SHelge Deller         }
219a72bd606SHelge Deller     }
220a72bd606SHelge Deller 
221a72bd606SHelge Deller     if (!kernel_entry) {
222a72bd606SHelge Deller         /* When booting via firmware, tell firmware if we want interactive
223a72bd606SHelge Deller          * mode (kernel_entry=1), and to boot from CD (gr[24]='d')
224a72bd606SHelge Deller          * or hard disc * (gr[24]='c').
225a72bd606SHelge Deller          */
226a72bd606SHelge Deller         kernel_entry = boot_menu ? 1 : 0;
227a72bd606SHelge Deller         cpu[0]->env.gr[24] = machine->boot_order[0];
228a72bd606SHelge Deller     }
229a72bd606SHelge Deller 
230a72bd606SHelge Deller     /* We jump to the firmware entry routine and pass the
231a72bd606SHelge Deller      * various parameters in registers. After firmware initialization,
232a72bd606SHelge Deller      * firmware will start the Linux kernel with ramdisk and cmdline.
233a72bd606SHelge Deller      */
234a72bd606SHelge Deller     cpu[0]->env.gr[26] = ram_size;
235a72bd606SHelge Deller     cpu[0]->env.gr[25] = kernel_entry;
236a72bd606SHelge Deller 
237a72bd606SHelge Deller     /* tell firmware how many SMP CPUs to present in inventory table */
238a72bd606SHelge Deller     cpu[0]->env.gr[21] = smp_cpus;
239a72bd606SHelge Deller }
240a72bd606SHelge Deller 
241a72bd606SHelge Deller static void hppa_machine_reset(void)
242a72bd606SHelge Deller {
243a72bd606SHelge Deller     int i;
244a72bd606SHelge Deller 
245a72bd606SHelge Deller     qemu_devices_reset();
246a72bd606SHelge Deller 
247a72bd606SHelge Deller     /* Start all CPUs at the firmware entry point.
248a72bd606SHelge Deller      *  Monarch CPU will initialize firmware, secondary CPUs
249a72bd606SHelge Deller      *  will enter a small idle look and wait for rendevouz. */
250a72bd606SHelge Deller     for (i = 0; i < smp_cpus; i++) {
251a72bd606SHelge Deller         cpu_set_pc(CPU(cpu[i]), firmware_entry);
252a72bd606SHelge Deller         cpu[i]->env.gr[5] = CPU_HPA + i * 0x1000;
253a72bd606SHelge Deller     }
254a72bd606SHelge Deller 
255a72bd606SHelge Deller     /* already initialized by machine_hppa_init()? */
256a72bd606SHelge Deller     if (cpu[0]->env.gr[26] == ram_size) {
257a72bd606SHelge Deller         return;
258a72bd606SHelge Deller     }
259a72bd606SHelge Deller 
260a72bd606SHelge Deller     cpu[0]->env.gr[26] = ram_size;
261a72bd606SHelge Deller     cpu[0]->env.gr[25] = 0; /* no firmware boot menu */
262a72bd606SHelge Deller     cpu[0]->env.gr[24] = 'c';
263a72bd606SHelge Deller     /* gr22/gr23 unused, no initrd while reboot. */
264a72bd606SHelge Deller     cpu[0]->env.gr[21] = smp_cpus;
265a72bd606SHelge Deller }
266a72bd606SHelge Deller 
267a72bd606SHelge Deller 
268813dff13SHelge Deller static void machine_hppa_machine_init(MachineClass *mc)
269813dff13SHelge Deller {
270813dff13SHelge Deller     mc->desc = "HPPA generic machine";
271a72bd606SHelge Deller     mc->default_cpu_type = TYPE_HPPA_CPU;
272813dff13SHelge Deller     mc->init = machine_hppa_init;
273a72bd606SHelge Deller     mc->reset = hppa_machine_reset;
274813dff13SHelge Deller     mc->block_default_type = IF_SCSI;
275a72bd606SHelge Deller     mc->max_cpus = HPPA_MAX_CPUS;
276a72bd606SHelge Deller     mc->default_cpus = 1;
277813dff13SHelge Deller     mc->is_default = 1;
278813dff13SHelge Deller     mc->default_ram_size = 512 * M_BYTE;
279813dff13SHelge Deller     mc->default_boot_order = "cd";
280813dff13SHelge Deller }
281813dff13SHelge Deller 
282813dff13SHelge Deller DEFINE_MACHINE("hppa", machine_hppa_machine_init)
283