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