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