2 * QEMU HPPA hardware system emulator.
3 * Copyright 2018 Helge Deller <deller@gmx.de>
6 #include "qemu/osdep.h"
7 #include "qemu-common.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"
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
;
64 qemu_irq rtc_irq
, serial_irq
;
65 char *firmware_filename
;
66 uint64_t firmware_low
, firmware_high
;
68 uint64_t kernel_entry
= 0, kernel_low
, kernel_high
;
69 MemoryRegion
*addr_space
= get_system_memory();
70 MemoryRegion
*rom_region
;
71 MemoryRegion
*ram_region
;
72 MemoryRegion
*cpu_region
;
75 ram_size
= machine
->ram_size
;
78 for (i
= 0; i
< smp_cpus
; i
++) {
79 cpu
[i
] = HPPA_CPU(cpu_create(machine
->cpu_type
));
81 cpu_region
= g_new(MemoryRegion
, 1);
82 memory_region_init_io(cpu_region
, OBJECT(cpu
[i
]), &hppa_io_eir_ops
,
83 cpu
[i
], g_strdup_printf("cpu%ld-io-eir", i
), 4);
84 memory_region_add_subregion(addr_space
, CPU_HPA
+ i
* 0x1000,
88 /* Limit main memory. */
89 if (ram_size
> FIRMWARE_START
) {
90 machine
->ram_size
= ram_size
= FIRMWARE_START
;
93 /* Main memory region. */
94 ram_region
= g_new(MemoryRegion
, 1);
95 memory_region_allocate_system_memory(ram_region
, OBJECT(machine
),
97 memory_region_add_subregion(addr_space
, 0, ram_region
);
99 /* Init Dino (PCI host bus chip). */
100 pci_bus
= dino_init(addr_space
, &rtc_irq
, &serial_irq
);
103 /* Create ISA bus. */
104 isa_bus
= hppa_isa_bus();
107 /* Realtime clock, used by firmware for PDC_TOD call. */
108 mc146818_rtc_init(isa_bus
, 2000, rtc_irq
);
110 /* Serial code setup. */
112 uint32_t addr
= DINO_UART_HPA
+ 0x800;
113 serial_mm_init(addr_space
, addr
, 0, serial_irq
,
114 115200, serial_hd(0), DEVICE_BIG_ENDIAN
);
117 /* SCSI disk setup. */
118 lsi53c895a_create(pci_bus
);
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
);
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");
136 size
= load_elf(firmware_filename
, NULL
,
137 NULL
, &firmware_entry
, &firmware_low
, &firmware_high
,
138 true, EM_PARISC
, 0, 0);
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
;
146 error_report("could not load firmware '%s'", firmware_filename
);
149 qemu_log_mask(CPU_LOG_PAGE
, "Firmware loaded at 0x%08" PRIx64
150 "-0x%08" PRIx64
", 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");
156 g_free(firmware_filename
);
158 rom_region
= g_new(MemoryRegion
, 1);
159 memory_region_allocate_system_memory(rom_region
, OBJECT(machine
),
161 (FIRMWARE_END
- FIRMWARE_START
));
162 memory_region_add_subregion(addr_space
, FIRMWARE_START
, rom_region
);
165 if (kernel_filename
) {
166 size
= load_elf(kernel_filename
, &cpu_hppa_to_phys
,
167 NULL
, &kernel_entry
, &kernel_low
, &kernel_high
,
168 true, EM_PARISC
, 0, 0);
170 /* Unfortunately, load_elf sign-extends reading elf32. */
171 kernel_entry
= (target_ureg
) cpu_hppa_to_phys(NULL
, kernel_entry
);
172 kernel_low
= (target_ureg
)kernel_low
;
173 kernel_high
= (target_ureg
)kernel_high
;
176 error_report("could not load kernel '%s'", kernel_filename
);
179 qemu_log_mask(CPU_LOG_PAGE
, "Kernel loaded at 0x%08" PRIx64
180 "-0x%08" PRIx64
", entry at 0x%08" PRIx64
181 ", size %" PRIu64
" kB\n",
182 kernel_low
, kernel_high
, kernel_entry
, size
/ KiB
);
184 if (kernel_cmdline
) {
185 cpu
[0]->env
.gr
[24] = 0x4000;
186 pstrcpy_targphys("cmdline", cpu
[0]->env
.gr
[24],
187 TARGET_PAGE_SIZE
, kernel_cmdline
);
190 if (initrd_filename
) {
191 ram_addr_t initrd_base
;
194 initrd_size
= get_image_size(initrd_filename
);
195 if (initrd_size
< 0) {
196 error_report("could not load initial ram disk '%s'",
201 /* Load the initrd image high in memory.
202 Mirror the algorithm used by palo:
203 (1) Due to sign-extension problems and PDC,
204 put the initrd no higher than 1G.
205 (2) Reserve 64k for stack. */
206 initrd_base
= MIN(ram_size
, 1 * GiB
);
207 initrd_base
= initrd_base
- 64 * KiB
;
208 initrd_base
= (initrd_base
- initrd_size
) & TARGET_PAGE_MASK
;
210 if (initrd_base
< kernel_high
) {
211 error_report("kernel and initial ram disk too large!");
215 load_image_targphys(initrd_filename
, initrd_base
, initrd_size
);
216 cpu
[0]->env
.gr
[23] = initrd_base
;
217 cpu
[0]->env
.gr
[22] = initrd_base
+ initrd_size
;
222 /* When booting via firmware, tell firmware if we want interactive
223 * mode (kernel_entry=1), and to boot from CD (gr[24]='d')
224 * or hard disc * (gr[24]='c').
226 kernel_entry
= boot_menu
? 1 : 0;
227 cpu
[0]->env
.gr
[24] = machine
->boot_order
[0];
230 /* We jump to the firmware entry routine and pass the
231 * various parameters in registers. After firmware initialization,
232 * firmware will start the Linux kernel with ramdisk and cmdline.
234 cpu
[0]->env
.gr
[26] = ram_size
;
235 cpu
[0]->env
.gr
[25] = kernel_entry
;
237 /* tell firmware how many SMP CPUs to present in inventory table */
238 cpu
[0]->env
.gr
[21] = smp_cpus
;
241 static void hppa_machine_reset(void)
245 qemu_devices_reset();
247 /* Start all CPUs at the firmware entry point.
248 * Monarch CPU will initialize firmware, secondary CPUs
249 * will enter a small idle look and wait for rendevouz. */
250 for (i
= 0; i
< smp_cpus
; i
++) {
251 cpu_set_pc(CPU(cpu
[i
]), firmware_entry
);
252 cpu
[i
]->env
.gr
[5] = CPU_HPA
+ i
* 0x1000;
255 /* already initialized by machine_hppa_init()? */
256 if (cpu
[0]->env
.gr
[26] == ram_size
) {
260 cpu
[0]->env
.gr
[26] = ram_size
;
261 cpu
[0]->env
.gr
[25] = 0; /* no firmware boot menu */
262 cpu
[0]->env
.gr
[24] = 'c';
263 /* gr22/gr23 unused, no initrd while reboot. */
264 cpu
[0]->env
.gr
[21] = smp_cpus
;
268 static void machine_hppa_machine_init(MachineClass
*mc
)
270 mc
->desc
= "HPPA generic machine";
271 mc
->default_cpu_type
= TYPE_HPPA_CPU
;
272 mc
->init
= machine_hppa_init
;
273 mc
->reset
= hppa_machine_reset
;
274 mc
->block_default_type
= IF_SCSI
;
275 mc
->max_cpus
= HPPA_MAX_CPUS
;
276 mc
->default_cpus
= 1;
278 mc
->default_ram_size
= 512 * MiB
;
279 mc
->default_boot_order
= "cd";
282 DEFINE_MACHINE("hppa", machine_hppa_machine_init
)