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/rtc/mc146818rtc.h"
16 #include "hw/timer/i8254.h"
17 #include "hw/char/serial.h"
18 #include "hw/net/lasi_82596.h"
20 #include "qemu/units.h"
21 #include "qapi/error.h"
28 static ISABus
*hppa_isa_bus(void)
32 MemoryRegion
*isa_region
;
34 isa_region
= g_new(MemoryRegion
, 1);
35 memory_region_init_io(isa_region
, NULL
, &hppa_pci_ignore_ops
,
36 NULL
, "isa-io", 0x800);
37 memory_region_add_subregion(get_system_memory(), IDE_HPA
,
40 isa_bus
= isa_bus_new(NULL
, get_system_memory(), isa_region
,
42 isa_irqs
= i8259_init(isa_bus
,
43 /* qemu_allocate_irq(dino_set_isa_irq, s, 0)); */
45 isa_bus_irqs(isa_bus
, isa_irqs
);
50 static uint64_t cpu_hppa_to_phys(void *opaque
, uint64_t addr
)
52 addr
&= (0x10000000 - 1);
56 static HPPACPU
*cpu
[HPPA_MAX_CPUS
];
57 static uint64_t firmware_entry
;
59 static void machine_hppa_init(MachineState
*machine
)
61 const char *kernel_filename
= machine
->kernel_filename
;
62 const char *kernel_cmdline
= machine
->kernel_cmdline
;
63 const char *initrd_filename
= machine
->initrd_filename
;
67 qemu_irq rtc_irq
, serial_irq
;
68 char *firmware_filename
;
69 uint64_t firmware_low
, firmware_high
;
71 uint64_t kernel_entry
= 0, kernel_low
, kernel_high
;
72 MemoryRegion
*addr_space
= get_system_memory();
73 MemoryRegion
*rom_region
;
74 MemoryRegion
*cpu_region
;
76 unsigned int smp_cpus
= machine
->smp
.cpus
;
80 for (i
= 0; i
< smp_cpus
; i
++) {
81 char *name
= g_strdup_printf("cpu%ld-io-eir", i
);
82 cpu
[i
] = HPPA_CPU(cpu_create(machine
->cpu_type
));
84 cpu_region
= g_new(MemoryRegion
, 1);
85 memory_region_init_io(cpu_region
, OBJECT(cpu
[i
]), &hppa_io_eir_ops
,
87 memory_region_add_subregion(addr_space
, CPU_HPA
+ i
* 0x1000,
92 /* Main memory region. */
93 if (machine
->ram_size
> 3 * GiB
) {
94 error_report("RAM size is currently restricted to 3GB");
97 memory_region_add_subregion_overlap(addr_space
, 0, machine
->ram
, -1);
101 lasi_init(addr_space
);
103 /* Init Dino (PCI host bus chip). */
104 pci_bus
= dino_init(addr_space
, &rtc_irq
, &serial_irq
);
107 /* Create ISA bus. */
108 isa_bus
= hppa_isa_bus();
111 /* Realtime clock, used by firmware for PDC_TOD call. */
112 mc146818_rtc_init(isa_bus
, 2000, rtc_irq
);
114 /* Serial code setup. */
116 uint32_t addr
= DINO_UART_HPA
+ 0x800;
117 serial_mm_init(addr_space
, addr
, 0, serial_irq
,
118 115200, serial_hd(0), DEVICE_BIG_ENDIAN
);
121 /* SCSI disk setup. */
122 dev
= DEVICE(pci_create_simple(pci_bus
, -1, "lsi53c895a"));
123 lsi53c8xx_handle_legacy_cmdline(dev
);
125 /* Graphics setup. */
126 if (machine
->enable_graphics
&& vga_interface_type
!= VGA_NONE
) {
127 dev
= qdev_new("artist");
128 s
= SYS_BUS_DEVICE(dev
);
129 sysbus_realize_and_unref(s
, &error_fatal
);
130 sysbus_mmio_map(s
, 0, LASI_GFX_HPA
);
131 sysbus_mmio_map(s
, 1, ARTIST_FB_ADDR
);
135 for (i
= 0; i
< nb_nics
; i
++) {
136 if (!enable_lasi_lan()) {
137 pci_nic_init_nofail(&nd_table
[i
], pci_bus
, "tulip", NULL
);
141 /* Load firmware. Given that this is not "real" firmware,
142 but one explicitly written for the emulation, we might as
143 well load it directly from an ELF image. */
144 firmware_filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
,
145 bios_name
? bios_name
:
146 "hppa-firmware.img");
147 if (firmware_filename
== NULL
) {
148 error_report("no firmware provided");
152 size
= load_elf(firmware_filename
, NULL
, NULL
, NULL
,
153 &firmware_entry
, &firmware_low
, &firmware_high
, NULL
,
154 true, EM_PARISC
, 0, 0);
156 /* Unfortunately, load_elf sign-extends reading elf32. */
157 firmware_entry
= (target_ureg
)firmware_entry
;
158 firmware_low
= (target_ureg
)firmware_low
;
159 firmware_high
= (target_ureg
)firmware_high
;
162 error_report("could not load firmware '%s'", firmware_filename
);
165 qemu_log_mask(CPU_LOG_PAGE
, "Firmware loaded at 0x%08" PRIx64
166 "-0x%08" PRIx64
", entry at 0x%08" PRIx64
".\n",
167 firmware_low
, firmware_high
, firmware_entry
);
168 if (firmware_low
< FIRMWARE_START
|| firmware_high
>= FIRMWARE_END
) {
169 error_report("Firmware overlaps with memory or IO space");
172 g_free(firmware_filename
);
174 rom_region
= g_new(MemoryRegion
, 1);
175 memory_region_init_ram(rom_region
, NULL
, "firmware",
176 (FIRMWARE_END
- FIRMWARE_START
), &error_fatal
);
177 memory_region_add_subregion(addr_space
, FIRMWARE_START
, rom_region
);
180 if (kernel_filename
) {
181 size
= load_elf(kernel_filename
, NULL
, &cpu_hppa_to_phys
,
182 NULL
, &kernel_entry
, &kernel_low
, &kernel_high
, NULL
,
183 true, EM_PARISC
, 0, 0);
185 /* Unfortunately, load_elf sign-extends reading elf32. */
186 kernel_entry
= (target_ureg
) cpu_hppa_to_phys(NULL
, kernel_entry
);
187 kernel_low
= (target_ureg
)kernel_low
;
188 kernel_high
= (target_ureg
)kernel_high
;
191 error_report("could not load kernel '%s'", kernel_filename
);
194 qemu_log_mask(CPU_LOG_PAGE
, "Kernel loaded at 0x%08" PRIx64
195 "-0x%08" PRIx64
", entry at 0x%08" PRIx64
196 ", size %" PRIu64
" kB\n",
197 kernel_low
, kernel_high
, kernel_entry
, size
/ KiB
);
199 if (kernel_cmdline
) {
200 cpu
[0]->env
.gr
[24] = 0x4000;
201 pstrcpy_targphys("cmdline", cpu
[0]->env
.gr
[24],
202 TARGET_PAGE_SIZE
, kernel_cmdline
);
205 if (initrd_filename
) {
206 ram_addr_t initrd_base
;
209 initrd_size
= get_image_size(initrd_filename
);
210 if (initrd_size
< 0) {
211 error_report("could not load initial ram disk '%s'",
216 /* Load the initrd image high in memory.
217 Mirror the algorithm used by palo:
218 (1) Due to sign-extension problems and PDC,
219 put the initrd no higher than 1G.
220 (2) Reserve 64k for stack. */
221 initrd_base
= MIN(ram_size
, 1 * GiB
);
222 initrd_base
= initrd_base
- 64 * KiB
;
223 initrd_base
= (initrd_base
- initrd_size
) & TARGET_PAGE_MASK
;
225 if (initrd_base
< kernel_high
) {
226 error_report("kernel and initial ram disk too large!");
230 load_image_targphys(initrd_filename
, initrd_base
, initrd_size
);
231 cpu
[0]->env
.gr
[23] = initrd_base
;
232 cpu
[0]->env
.gr
[22] = initrd_base
+ initrd_size
;
237 /* When booting via firmware, tell firmware if we want interactive
238 * mode (kernel_entry=1), and to boot from CD (gr[24]='d')
239 * or hard disc * (gr[24]='c').
241 kernel_entry
= boot_menu
? 1 : 0;
242 cpu
[0]->env
.gr
[24] = machine
->boot_order
[0];
245 /* We jump to the firmware entry routine and pass the
246 * various parameters in registers. After firmware initialization,
247 * firmware will start the Linux kernel with ramdisk and cmdline.
249 cpu
[0]->env
.gr
[26] = ram_size
;
250 cpu
[0]->env
.gr
[25] = kernel_entry
;
252 /* tell firmware how many SMP CPUs to present in inventory table */
253 cpu
[0]->env
.gr
[21] = smp_cpus
;
256 static void hppa_machine_reset(MachineState
*ms
)
258 unsigned int smp_cpus
= ms
->smp
.cpus
;
261 qemu_devices_reset();
263 /* Start all CPUs at the firmware entry point.
264 * Monarch CPU will initialize firmware, secondary CPUs
265 * will enter a small idle look and wait for rendevouz. */
266 for (i
= 0; i
< smp_cpus
; i
++) {
267 cpu_set_pc(CPU(cpu
[i
]), firmware_entry
);
268 cpu
[i
]->env
.gr
[5] = CPU_HPA
+ i
* 0x1000;
271 /* already initialized by machine_hppa_init()? */
272 if (cpu
[0]->env
.gr
[26] == ram_size
) {
276 cpu
[0]->env
.gr
[26] = ram_size
;
277 cpu
[0]->env
.gr
[25] = 0; /* no firmware boot menu */
278 cpu
[0]->env
.gr
[24] = 'c';
279 /* gr22/gr23 unused, no initrd while reboot. */
280 cpu
[0]->env
.gr
[21] = smp_cpus
;
284 static void machine_hppa_machine_init(MachineClass
*mc
)
286 mc
->desc
= "HPPA generic machine";
287 mc
->default_cpu_type
= TYPE_HPPA_CPU
;
288 mc
->init
= machine_hppa_init
;
289 mc
->reset
= hppa_machine_reset
;
290 mc
->block_default_type
= IF_SCSI
;
291 mc
->max_cpus
= HPPA_MAX_CPUS
;
292 mc
->default_cpus
= 1;
293 mc
->is_default
= true;
294 mc
->default_ram_size
= 512 * MiB
;
295 mc
->default_boot_order
= "cd";
296 mc
->default_ram_id
= "ram";
299 DEFINE_MACHINE("hppa", machine_hppa_machine_init
)