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