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