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