2 * QEMU HPPA hardware system emulator.
3 * Copyright 2018 Helge Deller <deller@gmx.de>
6 #include "qemu/osdep.h"
7 #include "qemu-common.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/timer/mc146818rtc.h"
17 #include "hw/timer/i8254.h"
18 #include "hw/char/serial.h"
20 #include "qemu/units.h"
21 #include "qapi/error.h"
26 static ISABus
*hppa_isa_bus(void)
30 MemoryRegion
*isa_region
;
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
,
38 isa_bus
= isa_bus_new(NULL
, get_system_memory(), isa_region
,
40 isa_irqs
= i8259_init(isa_bus
,
41 /* qemu_allocate_irq(dino_set_isa_irq, s, 0)); */
43 isa_bus_irqs(isa_bus
, isa_irqs
);
48 static uint64_t cpu_hppa_to_phys(void *opaque
, uint64_t addr
)
50 addr
&= (0x10000000 - 1);
54 static HPPACPU
*cpu
[HPPA_MAX_CPUS
];
55 static uint64_t firmware_entry
;
57 static void machine_hppa_init(MachineState
*machine
)
59 const char *kernel_filename
= machine
->kernel_filename
;
60 const char *kernel_cmdline
= machine
->kernel_cmdline
;
61 const char *initrd_filename
= machine
->initrd_filename
;
65 qemu_irq rtc_irq
, serial_irq
;
66 char *firmware_filename
;
67 uint64_t firmware_low
, firmware_high
;
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
;
75 unsigned int smp_cpus
= machine
->smp
.cpus
;
77 ram_size
= machine
->ram_size
;
80 for (i
= 0; i
< smp_cpus
; i
++) {
81 cpu
[i
] = HPPA_CPU(cpu_create(machine
->cpu_type
));
83 cpu_region
= g_new(MemoryRegion
, 1);
84 memory_region_init_io(cpu_region
, OBJECT(cpu
[i
]), &hppa_io_eir_ops
,
85 cpu
[i
], g_strdup_printf("cpu%ld-io-eir", i
), 4);
86 memory_region_add_subregion(addr_space
, CPU_HPA
+ i
* 0x1000,
90 /* Limit main memory. */
91 if (ram_size
> FIRMWARE_START
) {
92 machine
->ram_size
= ram_size
= FIRMWARE_START
;
95 /* Main memory region. */
96 ram_region
= g_new(MemoryRegion
, 1);
97 memory_region_allocate_system_memory(ram_region
, OBJECT(machine
),
99 memory_region_add_subregion(addr_space
, 0, ram_region
);
101 /* Init Dino (PCI host bus chip). */
102 pci_bus
= dino_init(addr_space
, &rtc_irq
, &serial_irq
);
105 /* Create ISA bus. */
106 isa_bus
= hppa_isa_bus();
109 /* Realtime clock, used by firmware for PDC_TOD call. */
110 mc146818_rtc_init(isa_bus
, 2000, rtc_irq
);
112 /* Serial code setup. */
114 uint32_t addr
= DINO_UART_HPA
+ 0x800;
115 serial_mm_init(addr_space
, addr
, 0, serial_irq
,
116 115200, serial_hd(0), DEVICE_BIG_ENDIAN
);
119 /* SCSI disk setup. */
120 dev
= DEVICE(pci_create_simple(pci_bus
, -1, "lsi53c895a"));
121 lsi53c8xx_handle_legacy_cmdline(dev
);
123 /* Network setup. e1000 is good enough, failing Tulip support. */
124 for (i
= 0; i
< nb_nics
; i
++) {
125 pci_nic_init_nofail(&nd_table
[i
], pci_bus
, "e1000", NULL
);
128 /* Load firmware. Given that this is not "real" firmware,
129 but one explicitly written for the emulation, we might as
130 well load it directly from an ELF image. */
131 firmware_filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
,
132 bios_name
? bios_name
:
133 "hppa-firmware.img");
134 if (firmware_filename
== NULL
) {
135 error_report("no firmware provided");
139 size
= load_elf(firmware_filename
, NULL
, NULL
, NULL
,
140 &firmware_entry
, &firmware_low
, &firmware_high
,
141 true, EM_PARISC
, 0, 0);
143 /* Unfortunately, load_elf sign-extends reading elf32. */
144 firmware_entry
= (target_ureg
)firmware_entry
;
145 firmware_low
= (target_ureg
)firmware_low
;
146 firmware_high
= (target_ureg
)firmware_high
;
149 error_report("could not load firmware '%s'", firmware_filename
);
152 qemu_log_mask(CPU_LOG_PAGE
, "Firmware loaded at 0x%08" PRIx64
153 "-0x%08" PRIx64
", entry at 0x%08" PRIx64
".\n",
154 firmware_low
, firmware_high
, firmware_entry
);
155 if (firmware_low
< ram_size
|| firmware_high
>= FIRMWARE_END
) {
156 error_report("Firmware overlaps with memory or IO space");
159 g_free(firmware_filename
);
161 rom_region
= g_new(MemoryRegion
, 1);
162 memory_region_allocate_system_memory(rom_region
, OBJECT(machine
),
164 (FIRMWARE_END
- FIRMWARE_START
));
165 memory_region_add_subregion(addr_space
, FIRMWARE_START
, rom_region
);
168 if (kernel_filename
) {
169 size
= load_elf(kernel_filename
, NULL
, &cpu_hppa_to_phys
,
170 NULL
, &kernel_entry
, &kernel_low
, &kernel_high
,
171 true, EM_PARISC
, 0, 0);
173 /* Unfortunately, load_elf sign-extends reading elf32. */
174 kernel_entry
= (target_ureg
) cpu_hppa_to_phys(NULL
, kernel_entry
);
175 kernel_low
= (target_ureg
)kernel_low
;
176 kernel_high
= (target_ureg
)kernel_high
;
179 error_report("could not load kernel '%s'", kernel_filename
);
182 qemu_log_mask(CPU_LOG_PAGE
, "Kernel loaded at 0x%08" PRIx64
183 "-0x%08" PRIx64
", entry at 0x%08" PRIx64
184 ", size %" PRIu64
" kB\n",
185 kernel_low
, kernel_high
, kernel_entry
, size
/ KiB
);
187 if (kernel_cmdline
) {
188 cpu
[0]->env
.gr
[24] = 0x4000;
189 pstrcpy_targphys("cmdline", cpu
[0]->env
.gr
[24],
190 TARGET_PAGE_SIZE
, kernel_cmdline
);
193 if (initrd_filename
) {
194 ram_addr_t initrd_base
;
197 initrd_size
= get_image_size(initrd_filename
);
198 if (initrd_size
< 0) {
199 error_report("could not load initial ram disk '%s'",
204 /* Load the initrd image high in memory.
205 Mirror the algorithm used by palo:
206 (1) Due to sign-extension problems and PDC,
207 put the initrd no higher than 1G.
208 (2) Reserve 64k for stack. */
209 initrd_base
= MIN(ram_size
, 1 * GiB
);
210 initrd_base
= initrd_base
- 64 * KiB
;
211 initrd_base
= (initrd_base
- initrd_size
) & TARGET_PAGE_MASK
;
213 if (initrd_base
< kernel_high
) {
214 error_report("kernel and initial ram disk too large!");
218 load_image_targphys(initrd_filename
, initrd_base
, initrd_size
);
219 cpu
[0]->env
.gr
[23] = initrd_base
;
220 cpu
[0]->env
.gr
[22] = initrd_base
+ initrd_size
;
225 /* When booting via firmware, tell firmware if we want interactive
226 * mode (kernel_entry=1), and to boot from CD (gr[24]='d')
227 * or hard disc * (gr[24]='c').
229 kernel_entry
= boot_menu
? 1 : 0;
230 cpu
[0]->env
.gr
[24] = machine
->boot_order
[0];
233 /* We jump to the firmware entry routine and pass the
234 * various parameters in registers. After firmware initialization,
235 * firmware will start the Linux kernel with ramdisk and cmdline.
237 cpu
[0]->env
.gr
[26] = ram_size
;
238 cpu
[0]->env
.gr
[25] = kernel_entry
;
240 /* tell firmware how many SMP CPUs to present in inventory table */
241 cpu
[0]->env
.gr
[21] = smp_cpus
;
244 static void hppa_machine_reset(MachineState
*ms
)
246 unsigned int smp_cpus
= ms
->smp
.cpus
;
249 qemu_devices_reset();
251 /* Start all CPUs at the firmware entry point.
252 * Monarch CPU will initialize firmware, secondary CPUs
253 * will enter a small idle look and wait for rendevouz. */
254 for (i
= 0; i
< smp_cpus
; i
++) {
255 cpu_set_pc(CPU(cpu
[i
]), firmware_entry
);
256 cpu
[i
]->env
.gr
[5] = CPU_HPA
+ i
* 0x1000;
259 /* already initialized by machine_hppa_init()? */
260 if (cpu
[0]->env
.gr
[26] == ram_size
) {
264 cpu
[0]->env
.gr
[26] = ram_size
;
265 cpu
[0]->env
.gr
[25] = 0; /* no firmware boot menu */
266 cpu
[0]->env
.gr
[24] = 'c';
267 /* gr22/gr23 unused, no initrd while reboot. */
268 cpu
[0]->env
.gr
[21] = smp_cpus
;
272 static void machine_hppa_machine_init(MachineClass
*mc
)
274 mc
->desc
= "HPPA generic machine";
275 mc
->default_cpu_type
= TYPE_HPPA_CPU
;
276 mc
->init
= machine_hppa_init
;
277 mc
->reset
= hppa_machine_reset
;
278 mc
->block_default_type
= IF_SCSI
;
279 mc
->max_cpus
= HPPA_MAX_CPUS
;
280 mc
->default_cpus
= 1;
282 mc
->default_ram_size
= 512 * MiB
;
283 mc
->default_boot_order
= "cd";
286 DEFINE_MACHINE("hppa", machine_hppa_machine_init
)