xref: /openbmc/qemu/hw/hppa/machine.c (revision c5a58398)
1 /*
2  * QEMU HPPA hardware system emulator.
3  * Copyright 2018 Helge Deller <deller@gmx.de>
4  */
5 
6 #include "qemu/osdep.h"
7 #include "qemu-common.h"
8 #include "cpu.h"
9 #include "elf.h"
10 #include "hw/loader.h"
11 #include "hw/boards.h"
12 #include "qemu/error-report.h"
13 #include "sysemu/reset.h"
14 #include "sysemu/sysemu.h"
15 #include "hw/rtc/mc146818rtc.h"
16 #include "hw/timer/i8254.h"
17 #include "hw/char/serial.h"
18 #include "hw/net/lasi_82596.h"
19 #include "hppa_sys.h"
20 #include "qemu/units.h"
21 #include "qapi/error.h"
22 #include "net/net.h"
23 #include "qemu/log.h"
24 #include "net/net.h"
25 
26 #define MAX_IDE_BUS 2
27 
28 static ISABus *hppa_isa_bus(void)
29 {
30     ISABus *isa_bus;
31     qemu_irq *isa_irqs;
32     MemoryRegion *isa_region;
33 
34     isa_region = g_new(MemoryRegion, 1);
35     memory_region_init_io(isa_region, NULL, &hppa_pci_ignore_ops,
36                           NULL, "isa-io", 0x800);
37     memory_region_add_subregion(get_system_memory(), IDE_HPA,
38                                 isa_region);
39 
40     isa_bus = isa_bus_new(NULL, get_system_memory(), isa_region,
41                           &error_abort);
42     isa_irqs = i8259_init(isa_bus,
43                           /* qemu_allocate_irq(dino_set_isa_irq, s, 0)); */
44                           NULL);
45     isa_bus_irqs(isa_bus, isa_irqs);
46 
47     return isa_bus;
48 }
49 
50 static uint64_t cpu_hppa_to_phys(void *opaque, uint64_t addr)
51 {
52     addr &= (0x10000000 - 1);
53     return addr;
54 }
55 
56 static HPPACPU *cpu[HPPA_MAX_CPUS];
57 static uint64_t firmware_entry;
58 
59 static void machine_hppa_init(MachineState *machine)
60 {
61     const char *kernel_filename = machine->kernel_filename;
62     const char *kernel_cmdline = machine->kernel_cmdline;
63     const char *initrd_filename = machine->initrd_filename;
64     DeviceState *dev;
65     PCIBus *pci_bus;
66     ISABus *isa_bus;
67     qemu_irq rtc_irq, serial_irq;
68     char *firmware_filename;
69     uint64_t firmware_low, firmware_high;
70     long size;
71     uint64_t kernel_entry = 0, kernel_low, kernel_high;
72     MemoryRegion *addr_space = get_system_memory();
73     MemoryRegion *rom_region;
74     MemoryRegion *cpu_region;
75     long i;
76     unsigned int smp_cpus = machine->smp.cpus;
77     SysBusDevice *s;
78 
79     /* Create CPUs.  */
80     for (i = 0; i < smp_cpus; i++) {
81         char *name = g_strdup_printf("cpu%ld-io-eir", i);
82         cpu[i] = HPPA_CPU(cpu_create(machine->cpu_type));
83 
84         cpu_region = g_new(MemoryRegion, 1);
85         memory_region_init_io(cpu_region, OBJECT(cpu[i]), &hppa_io_eir_ops,
86                               cpu[i], name, 4);
87         memory_region_add_subregion(addr_space, CPU_HPA + i * 0x1000,
88                                     cpu_region);
89         g_free(name);
90     }
91 
92     /* Main memory region. */
93     if (machine->ram_size > 3 * GiB) {
94         error_report("RAM size is currently restricted to 3GB");
95         exit(EXIT_FAILURE);
96     }
97     memory_region_add_subregion_overlap(addr_space, 0, machine->ram, -1);
98 
99 
100     /* Init Lasi chip */
101     lasi_init(addr_space);
102 
103     /* Init Dino (PCI host bus chip).  */
104     pci_bus = dino_init(addr_space, &rtc_irq, &serial_irq);
105     assert(pci_bus);
106 
107     /* Create ISA bus. */
108     isa_bus = hppa_isa_bus();
109     assert(isa_bus);
110 
111     /* Realtime clock, used by firmware for PDC_TOD call. */
112     mc146818_rtc_init(isa_bus, 2000, rtc_irq);
113 
114     /* Serial code setup.  */
115     if (serial_hd(0)) {
116         uint32_t addr = DINO_UART_HPA + 0x800;
117         serial_mm_init(addr_space, addr, 0, serial_irq,
118                        115200, serial_hd(0), DEVICE_BIG_ENDIAN);
119     }
120 
121     /* SCSI disk setup. */
122     dev = DEVICE(pci_create_simple(pci_bus, -1, "lsi53c895a"));
123     lsi53c8xx_handle_legacy_cmdline(dev);
124 
125     /* Graphics setup. */
126     if (machine->enable_graphics && vga_interface_type != VGA_NONE) {
127         dev = qdev_new("artist");
128         s = SYS_BUS_DEVICE(dev);
129         sysbus_realize_and_unref(s, &error_fatal);
130         sysbus_mmio_map(s, 0, LASI_GFX_HPA);
131         sysbus_mmio_map(s, 1, ARTIST_FB_ADDR);
132     }
133 
134     /* Network setup. */
135     for (i = 0; i < nb_nics; i++) {
136         if (!enable_lasi_lan()) {
137             pci_nic_init_nofail(&nd_table[i], pci_bus, "tulip", NULL);
138         }
139     }
140 
141     /* Load firmware.  Given that this is not "real" firmware,
142        but one explicitly written for the emulation, we might as
143        well load it directly from an ELF image.  */
144     firmware_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
145                                        bios_name ? bios_name :
146                                        "hppa-firmware.img");
147     if (firmware_filename == NULL) {
148         error_report("no firmware provided");
149         exit(1);
150     }
151 
152     size = load_elf(firmware_filename, NULL, NULL, NULL,
153                     &firmware_entry, &firmware_low, &firmware_high, NULL,
154                     true, EM_PARISC, 0, 0);
155 
156     /* Unfortunately, load_elf sign-extends reading elf32.  */
157     firmware_entry = (target_ureg)firmware_entry;
158     firmware_low = (target_ureg)firmware_low;
159     firmware_high = (target_ureg)firmware_high;
160 
161     if (size < 0) {
162         error_report("could not load firmware '%s'", firmware_filename);
163         exit(1);
164     }
165     qemu_log_mask(CPU_LOG_PAGE, "Firmware loaded at 0x%08" PRIx64
166                   "-0x%08" PRIx64 ", entry at 0x%08" PRIx64 ".\n",
167                   firmware_low, firmware_high, firmware_entry);
168     if (firmware_low < FIRMWARE_START || firmware_high >= FIRMWARE_END) {
169         error_report("Firmware overlaps with memory or IO space");
170         exit(1);
171     }
172     g_free(firmware_filename);
173 
174     rom_region = g_new(MemoryRegion, 1);
175     memory_region_init_ram(rom_region, NULL, "firmware",
176                            (FIRMWARE_END - FIRMWARE_START), &error_fatal);
177     memory_region_add_subregion(addr_space, FIRMWARE_START, rom_region);
178 
179     /* Load kernel */
180     if (kernel_filename) {
181         size = load_elf(kernel_filename, NULL, &cpu_hppa_to_phys,
182                         NULL, &kernel_entry, &kernel_low, &kernel_high, NULL,
183                         true, EM_PARISC, 0, 0);
184 
185         /* Unfortunately, load_elf sign-extends reading elf32.  */
186         kernel_entry = (target_ureg) cpu_hppa_to_phys(NULL, kernel_entry);
187         kernel_low = (target_ureg)kernel_low;
188         kernel_high = (target_ureg)kernel_high;
189 
190         if (size < 0) {
191             error_report("could not load kernel '%s'", kernel_filename);
192             exit(1);
193         }
194         qemu_log_mask(CPU_LOG_PAGE, "Kernel loaded at 0x%08" PRIx64
195                       "-0x%08" PRIx64 ", entry at 0x%08" PRIx64
196                       ", size %" PRIu64 " kB\n",
197                       kernel_low, kernel_high, kernel_entry, size / KiB);
198 
199         if (kernel_cmdline) {
200             cpu[0]->env.gr[24] = 0x4000;
201             pstrcpy_targphys("cmdline", cpu[0]->env.gr[24],
202                              TARGET_PAGE_SIZE, kernel_cmdline);
203         }
204 
205         if (initrd_filename) {
206             ram_addr_t initrd_base;
207             int64_t initrd_size;
208 
209             initrd_size = get_image_size(initrd_filename);
210             if (initrd_size < 0) {
211                 error_report("could not load initial ram disk '%s'",
212                              initrd_filename);
213                 exit(1);
214             }
215 
216             /* Load the initrd image high in memory.
217                Mirror the algorithm used by palo:
218                (1) Due to sign-extension problems and PDC,
219                put the initrd no higher than 1G.
220                (2) Reserve 64k for stack.  */
221             initrd_base = MIN(ram_size, 1 * GiB);
222             initrd_base = initrd_base - 64 * KiB;
223             initrd_base = (initrd_base - initrd_size) & TARGET_PAGE_MASK;
224 
225             if (initrd_base < kernel_high) {
226                 error_report("kernel and initial ram disk too large!");
227                 exit(1);
228             }
229 
230             load_image_targphys(initrd_filename, initrd_base, initrd_size);
231             cpu[0]->env.gr[23] = initrd_base;
232             cpu[0]->env.gr[22] = initrd_base + initrd_size;
233         }
234     }
235 
236     if (!kernel_entry) {
237         /* When booting via firmware, tell firmware if we want interactive
238          * mode (kernel_entry=1), and to boot from CD (gr[24]='d')
239          * or hard disc * (gr[24]='c').
240          */
241         kernel_entry = boot_menu ? 1 : 0;
242         cpu[0]->env.gr[24] = machine->boot_order[0];
243     }
244 
245     /* We jump to the firmware entry routine and pass the
246      * various parameters in registers. After firmware initialization,
247      * firmware will start the Linux kernel with ramdisk and cmdline.
248      */
249     cpu[0]->env.gr[26] = ram_size;
250     cpu[0]->env.gr[25] = kernel_entry;
251 
252     /* tell firmware how many SMP CPUs to present in inventory table */
253     cpu[0]->env.gr[21] = smp_cpus;
254 }
255 
256 static void hppa_machine_reset(MachineState *ms)
257 {
258     unsigned int smp_cpus = ms->smp.cpus;
259     int i;
260 
261     qemu_devices_reset();
262 
263     /* Start all CPUs at the firmware entry point.
264      *  Monarch CPU will initialize firmware, secondary CPUs
265      *  will enter a small idle look and wait for rendevouz. */
266     for (i = 0; i < smp_cpus; i++) {
267         cpu_set_pc(CPU(cpu[i]), firmware_entry);
268         cpu[i]->env.gr[5] = CPU_HPA + i * 0x1000;
269     }
270 
271     /* already initialized by machine_hppa_init()? */
272     if (cpu[0]->env.gr[26] == ram_size) {
273         return;
274     }
275 
276     cpu[0]->env.gr[26] = ram_size;
277     cpu[0]->env.gr[25] = 0; /* no firmware boot menu */
278     cpu[0]->env.gr[24] = 'c';
279     /* gr22/gr23 unused, no initrd while reboot. */
280     cpu[0]->env.gr[21] = smp_cpus;
281 }
282 
283 
284 static void machine_hppa_machine_init(MachineClass *mc)
285 {
286     mc->desc = "HPPA generic machine";
287     mc->default_cpu_type = TYPE_HPPA_CPU;
288     mc->init = machine_hppa_init;
289     mc->reset = hppa_machine_reset;
290     mc->block_default_type = IF_SCSI;
291     mc->max_cpus = HPPA_MAX_CPUS;
292     mc->default_cpus = 1;
293     mc->is_default = true;
294     mc->default_ram_size = 512 * MiB;
295     mc->default_boot_order = "cd";
296     mc->default_ram_id = "ram";
297 }
298 
299 DEFINE_MACHINE("hppa", machine_hppa_machine_init)
300