2 * QEMU Sun4u/Sun4v System Emulator
4 * Copyright (c) 2005 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "qemu-common.h"
29 #include "hw/pci/pci.h"
30 #include "hw/pci-host/apb.h"
31 #include "hw/i386/pc.h"
32 #include "hw/char/serial.h"
33 #include "hw/timer/m48t59.h"
34 #include "hw/block/fdc.h"
36 #include "qemu/timer.h"
37 #include "sysemu/sysemu.h"
38 #include "hw/boards.h"
39 #include "hw/nvram/sun_nvram.h"
40 #include "hw/nvram/chrp_nvram.h"
41 #include "hw/sparc/sparc64.h"
42 #include "hw/nvram/fw_cfg.h"
43 #include "hw/sysbus.h"
45 #include "hw/loader.h"
47 #include "qemu/cutils.h"
52 #define EBUS_DPRINTF(fmt, ...) \
53 do { printf("EBUS: " fmt , ## __VA_ARGS__); } while (0)
55 #define EBUS_DPRINTF(fmt, ...)
58 #define KERNEL_LOAD_ADDR 0x00404000
59 #define CMDLINE_ADDR 0x003ff000
60 #define PROM_SIZE_MAX (4 * 1024 * 1024)
61 #define PROM_VADDR 0x000ffd00000ULL
62 #define APB_SPECIAL_BASE 0x1fe00000000ULL
63 #define APB_MEM_BASE 0x1ff00000000ULL
64 #define APB_PCI_IO_BASE (APB_SPECIAL_BASE + 0x02000000ULL)
65 #define PROM_FILENAME "openbios-sparc64"
66 #define NVRAM_SIZE 0x2000
68 #define BIOS_CFG_IOPORT 0x510
69 #define FW_CFG_SPARC64_WIDTH (FW_CFG_ARCH_LOCAL + 0x00)
70 #define FW_CFG_SPARC64_HEIGHT (FW_CFG_ARCH_LOCAL + 0x01)
71 #define FW_CFG_SPARC64_DEPTH (FW_CFG_ARCH_LOCAL + 0x02)
76 const char * const default_cpu_model
;
79 uint64_t console_serial_base
;
82 typedef struct EbusState
{
88 void DMA_init(ISABus
*bus
, int high_page_enable
)
92 static void fw_cfg_boot_set(void *opaque
, const char *boot_device
,
95 fw_cfg_modify_i16(opaque
, FW_CFG_BOOT_DEVICE
, boot_device
[0]);
98 static int sun4u_NVRAM_set_params(Nvram
*nvram
, uint16_t NVRAM_size
,
99 const char *arch
, ram_addr_t RAM_size
,
100 const char *boot_devices
,
101 uint32_t kernel_image
, uint32_t kernel_size
,
103 uint32_t initrd_image
, uint32_t initrd_size
,
104 uint32_t NVRAM_image
,
105 int width
, int height
, int depth
,
106 const uint8_t *macaddr
)
110 uint8_t image
[0x1ff0];
111 NvramClass
*k
= NVRAM_GET_CLASS(nvram
);
113 memset(image
, '\0', sizeof(image
));
115 /* OpenBIOS nvram variables partition */
116 sysp_end
= chrp_nvram_create_system_partition(image
, 0);
118 /* Free space partition */
119 chrp_nvram_create_free_partition(&image
[sysp_end
], 0x1fd0 - sysp_end
);
121 Sun_init_header((struct Sun_nvram
*)&image
[0x1fd8], macaddr
, 0x80);
123 for (i
= 0; i
< sizeof(image
); i
++) {
124 (k
->write
)(nvram
, i
, image
[i
]);
130 static uint64_t sun4u_load_kernel(const char *kernel_filename
,
131 const char *initrd_filename
,
132 ram_addr_t RAM_size
, uint64_t *initrd_size
,
133 uint64_t *initrd_addr
, uint64_t *kernel_addr
,
134 uint64_t *kernel_entry
)
142 linux_boot
= (kernel_filename
!= NULL
);
153 kernel_size
= load_elf(kernel_filename
, NULL
, NULL
, kernel_entry
,
154 kernel_addr
, &kernel_top
, 1, EM_SPARCV9
, 0, 0);
155 if (kernel_size
< 0) {
156 *kernel_addr
= KERNEL_LOAD_ADDR
;
157 *kernel_entry
= KERNEL_LOAD_ADDR
;
158 kernel_size
= load_aout(kernel_filename
, KERNEL_LOAD_ADDR
,
159 RAM_size
- KERNEL_LOAD_ADDR
, bswap_needed
,
162 if (kernel_size
< 0) {
163 kernel_size
= load_image_targphys(kernel_filename
,
165 RAM_size
- KERNEL_LOAD_ADDR
);
167 if (kernel_size
< 0) {
168 fprintf(stderr
, "qemu: could not load kernel '%s'\n",
172 /* load initrd above kernel */
174 if (initrd_filename
) {
175 *initrd_addr
= TARGET_PAGE_ALIGN(kernel_top
);
177 *initrd_size
= load_image_targphys(initrd_filename
,
179 RAM_size
- *initrd_addr
);
180 if ((int)*initrd_size
< 0) {
181 fprintf(stderr
, "qemu: could not load initial ram disk '%s'\n",
186 if (*initrd_size
> 0) {
187 for (i
= 0; i
< 64 * TARGET_PAGE_SIZE
; i
+= TARGET_PAGE_SIZE
) {
188 ptr
= rom_ptr(*kernel_addr
+ i
);
189 if (ldl_p(ptr
+ 8) == 0x48647253) { /* HdrS */
190 stl_p(ptr
+ 24, *initrd_addr
+ *kernel_addr
);
191 stl_p(ptr
+ 28, *initrd_size
);
200 typedef struct ResetData
{
205 static void isa_irq_handler(void *opaque
, int n
, int level
)
207 static const int isa_irq_to_ivec
[16] = {
208 [1] = 0x29, /* keyboard */
209 [4] = 0x2b, /* serial */
210 [6] = 0x27, /* floppy */
211 [7] = 0x22, /* parallel */
212 [12] = 0x2a, /* mouse */
214 qemu_irq
*irqs
= opaque
;
217 assert(n
< ARRAY_SIZE(isa_irq_to_ivec
));
218 ivec
= isa_irq_to_ivec
[n
];
219 EBUS_DPRINTF("Set ISA IRQ %d level %d -> ivec 0x%x\n", n
, level
, ivec
);
221 qemu_set_irq(irqs
[ivec
], level
);
225 /* EBUS (Eight bit bus) bridge */
227 pci_ebus_init(PCIDevice
*pci_dev
, qemu_irq
*irqs
)
232 isa_bus
= ISA_BUS(qdev_get_child_bus(DEVICE(pci_dev
), "isa.0"));
233 isa_irq
= qemu_allocate_irqs(isa_irq_handler
, irqs
, 16);
234 isa_bus_irqs(isa_bus
, isa_irq
);
238 static void pci_ebus_realize(PCIDevice
*pci_dev
, Error
**errp
)
240 EbusState
*s
= DO_UPCAST(EbusState
, pci_dev
, pci_dev
);
242 if (!isa_bus_new(DEVICE(pci_dev
), get_system_memory(),
243 pci_address_space_io(pci_dev
), errp
)) {
247 pci_dev
->config
[0x04] = 0x06; // command = bus master, pci mem
248 pci_dev
->config
[0x05] = 0x00;
249 pci_dev
->config
[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
250 pci_dev
->config
[0x07] = 0x03; // status = medium devsel
251 pci_dev
->config
[0x09] = 0x00; // programming i/f
252 pci_dev
->config
[0x0D] = 0x0a; // latency_timer
254 memory_region_init_alias(&s
->bar0
, OBJECT(s
), "bar0", get_system_io(),
256 pci_register_bar(pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
, &s
->bar0
);
257 memory_region_init_alias(&s
->bar1
, OBJECT(s
), "bar1", get_system_io(),
259 pci_register_bar(pci_dev
, 1, PCI_BASE_ADDRESS_SPACE_IO
, &s
->bar1
);
262 static void ebus_class_init(ObjectClass
*klass
, void *data
)
264 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
266 k
->realize
= pci_ebus_realize
;
267 k
->vendor_id
= PCI_VENDOR_ID_SUN
;
268 k
->device_id
= PCI_DEVICE_ID_SUN_EBUS
;
270 k
->class_id
= PCI_CLASS_BRIDGE_OTHER
;
273 static const TypeInfo ebus_info
= {
275 .parent
= TYPE_PCI_DEVICE
,
276 .instance_size
= sizeof(EbusState
),
277 .class_init
= ebus_class_init
,
280 #define TYPE_OPENPROM "openprom"
281 #define OPENPROM(obj) OBJECT_CHECK(PROMState, (obj), TYPE_OPENPROM)
283 typedef struct PROMState
{
284 SysBusDevice parent_obj
;
289 static uint64_t translate_prom_address(void *opaque
, uint64_t addr
)
291 hwaddr
*base_addr
= (hwaddr
*)opaque
;
292 return addr
+ *base_addr
- PROM_VADDR
;
295 /* Boot PROM (OpenBIOS) */
296 static void prom_init(hwaddr addr
, const char *bios_name
)
303 dev
= qdev_create(NULL
, TYPE_OPENPROM
);
304 qdev_init_nofail(dev
);
305 s
= SYS_BUS_DEVICE(dev
);
307 sysbus_mmio_map(s
, 0, addr
);
310 if (bios_name
== NULL
) {
311 bios_name
= PROM_FILENAME
;
313 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
315 ret
= load_elf(filename
, translate_prom_address
, &addr
,
316 NULL
, NULL
, NULL
, 1, EM_SPARCV9
, 0, 0);
317 if (ret
< 0 || ret
> PROM_SIZE_MAX
) {
318 ret
= load_image_targphys(filename
, addr
, PROM_SIZE_MAX
);
324 if (ret
< 0 || ret
> PROM_SIZE_MAX
) {
325 fprintf(stderr
, "qemu: could not load prom '%s'\n", bios_name
);
330 static void prom_init1(Object
*obj
)
332 PROMState
*s
= OPENPROM(obj
);
333 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
335 memory_region_init_ram_nomigrate(&s
->prom
, obj
, "sun4u.prom", PROM_SIZE_MAX
,
337 vmstate_register_ram_global(&s
->prom
);
338 memory_region_set_readonly(&s
->prom
, true);
339 sysbus_init_mmio(dev
, &s
->prom
);
342 static Property prom_properties
[] = {
343 {/* end of property list */},
346 static void prom_class_init(ObjectClass
*klass
, void *data
)
348 DeviceClass
*dc
= DEVICE_CLASS(klass
);
350 dc
->props
= prom_properties
;
353 static const TypeInfo prom_info
= {
354 .name
= TYPE_OPENPROM
,
355 .parent
= TYPE_SYS_BUS_DEVICE
,
356 .instance_size
= sizeof(PROMState
),
357 .class_init
= prom_class_init
,
358 .instance_init
= prom_init1
,
362 #define TYPE_SUN4U_MEMORY "memory"
363 #define SUN4U_RAM(obj) OBJECT_CHECK(RamDevice, (obj), TYPE_SUN4U_MEMORY)
365 typedef struct RamDevice
{
366 SysBusDevice parent_obj
;
373 static void ram_realize(DeviceState
*dev
, Error
**errp
)
375 RamDevice
*d
= SUN4U_RAM(dev
);
376 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
378 memory_region_init_ram_nomigrate(&d
->ram
, OBJECT(d
), "sun4u.ram", d
->size
,
380 vmstate_register_ram_global(&d
->ram
);
381 sysbus_init_mmio(sbd
, &d
->ram
);
384 static void ram_init(hwaddr addr
, ram_addr_t RAM_size
)
391 dev
= qdev_create(NULL
, TYPE_SUN4U_MEMORY
);
392 s
= SYS_BUS_DEVICE(dev
);
396 qdev_init_nofail(dev
);
398 sysbus_mmio_map(s
, 0, addr
);
401 static Property ram_properties
[] = {
402 DEFINE_PROP_UINT64("size", RamDevice
, size
, 0),
403 DEFINE_PROP_END_OF_LIST(),
406 static void ram_class_init(ObjectClass
*klass
, void *data
)
408 DeviceClass
*dc
= DEVICE_CLASS(klass
);
410 dc
->realize
= ram_realize
;
411 dc
->props
= ram_properties
;
414 static const TypeInfo ram_info
= {
415 .name
= TYPE_SUN4U_MEMORY
,
416 .parent
= TYPE_SYS_BUS_DEVICE
,
417 .instance_size
= sizeof(RamDevice
),
418 .class_init
= ram_class_init
,
421 static void sun4uv_init(MemoryRegion
*address_space_mem
,
422 MachineState
*machine
,
423 const struct hwdef
*hwdef
)
428 uint64_t initrd_addr
, initrd_size
, kernel_addr
, kernel_size
, kernel_entry
;
429 PCIBus
*pci_bus
, *pci_bus2
, *pci_bus3
;
433 qemu_irq
*ivec_irqs
, *pbm_irqs
;
434 DriveInfo
*hd
[MAX_IDE_BUS
* MAX_IDE_DEVS
];
435 DriveInfo
*fd
[MAX_FD
];
440 cpu
= sparc64_cpu_devinit(machine
->cpu_model
, hwdef
->default_cpu_model
,
444 ram_init(0, machine
->ram_size
);
446 prom_init(hwdef
->prom_addr
, bios_name
);
448 ivec_irqs
= qemu_allocate_irqs(sparc64_cpu_set_ivec_irq
, cpu
, IVEC_MAX
);
449 pci_bus
= pci_apb_init(APB_SPECIAL_BASE
, APB_MEM_BASE
, ivec_irqs
, &pci_bus2
,
450 &pci_bus3
, &pbm_irqs
);
451 pci_vga_init(pci_bus
);
453 /* XXX Should be pci_bus3 */
454 ebus
= pci_create_simple(pci_bus
, -1, "ebus");
455 isa_bus
= pci_ebus_init(ebus
, pbm_irqs
);
458 if (hwdef
->console_serial_base
) {
459 serial_mm_init(address_space_mem
, hwdef
->console_serial_base
, 0,
460 NULL
, 115200, serial_hds
[i
], DEVICE_BIG_ENDIAN
);
464 serial_hds_isa_init(isa_bus
, i
, MAX_SERIAL_PORTS
);
465 parallel_hds_isa_init(isa_bus
, MAX_PARALLEL_PORTS
);
467 for(i
= 0; i
< nb_nics
; i
++)
468 pci_nic_init_nofail(&nd_table
[i
], pci_bus
, "ne2k_pci", NULL
);
470 ide_drive_get(hd
, ARRAY_SIZE(hd
));
472 pci_cmd646_ide_init(pci_bus
, hd
, 1);
474 isa_create_simple(isa_bus
, "i8042");
477 for(i
= 0; i
< MAX_FD
; i
++) {
478 fd
[i
] = drive_get(IF_FLOPPY
, 0, i
);
480 dev
= DEVICE(isa_create(isa_bus
, TYPE_ISA_FDC
));
482 qdev_prop_set_drive(dev
, "driveA", blk_by_legacy_dinfo(fd
[0]),
486 qdev_prop_set_drive(dev
, "driveB", blk_by_legacy_dinfo(fd
[1]),
489 qdev_prop_set_uint32(dev
, "dma", -1);
490 qdev_init_nofail(dev
);
492 /* Map NVRAM into I/O (ebus) space */
493 nvram
= m48t59_init(NULL
, 0, 0, NVRAM_SIZE
, 1968, 59);
494 s
= SYS_BUS_DEVICE(nvram
);
495 memory_region_add_subregion(pci_address_space_io(ebus
), 0x2000,
496 sysbus_mmio_get_region(s
, 0));
500 kernel_size
= sun4u_load_kernel(machine
->kernel_filename
,
501 machine
->initrd_filename
,
502 ram_size
, &initrd_size
, &initrd_addr
,
503 &kernel_addr
, &kernel_entry
);
505 sun4u_NVRAM_set_params(nvram
, NVRAM_SIZE
, "Sun4u", machine
->ram_size
,
507 kernel_addr
, kernel_size
,
508 machine
->kernel_cmdline
,
509 initrd_addr
, initrd_size
,
510 /* XXX: need an option to load a NVRAM image */
512 graphic_width
, graphic_height
, graphic_depth
,
513 (uint8_t *)&nd_table
[0].macaddr
);
515 dev
= qdev_create(NULL
, TYPE_FW_CFG_IO
);
516 qdev_prop_set_bit(dev
, "dma_enabled", false);
517 object_property_add_child(OBJECT(ebus
), TYPE_FW_CFG
, OBJECT(dev
), NULL
);
518 qdev_init_nofail(dev
);
519 memory_region_add_subregion(pci_address_space_io(ebus
), BIOS_CFG_IOPORT
,
520 &FW_CFG_IO(dev
)->comb_iomem
);
522 fw_cfg
= FW_CFG(dev
);
523 fw_cfg_add_i16(fw_cfg
, FW_CFG_NB_CPUS
, (uint16_t)smp_cpus
);
524 fw_cfg_add_i16(fw_cfg
, FW_CFG_MAX_CPUS
, (uint16_t)max_cpus
);
525 fw_cfg_add_i64(fw_cfg
, FW_CFG_RAM_SIZE
, (uint64_t)ram_size
);
526 fw_cfg_add_i16(fw_cfg
, FW_CFG_MACHINE_ID
, hwdef
->machine_id
);
527 fw_cfg_add_i64(fw_cfg
, FW_CFG_KERNEL_ADDR
, kernel_entry
);
528 fw_cfg_add_i64(fw_cfg
, FW_CFG_KERNEL_SIZE
, kernel_size
);
529 if (machine
->kernel_cmdline
) {
530 fw_cfg_add_i32(fw_cfg
, FW_CFG_CMDLINE_SIZE
,
531 strlen(machine
->kernel_cmdline
) + 1);
532 fw_cfg_add_string(fw_cfg
, FW_CFG_CMDLINE_DATA
, machine
->kernel_cmdline
);
534 fw_cfg_add_i32(fw_cfg
, FW_CFG_CMDLINE_SIZE
, 0);
536 fw_cfg_add_i64(fw_cfg
, FW_CFG_INITRD_ADDR
, initrd_addr
);
537 fw_cfg_add_i64(fw_cfg
, FW_CFG_INITRD_SIZE
, initrd_size
);
538 fw_cfg_add_i16(fw_cfg
, FW_CFG_BOOT_DEVICE
, machine
->boot_order
[0]);
540 fw_cfg_add_i16(fw_cfg
, FW_CFG_SPARC64_WIDTH
, graphic_width
);
541 fw_cfg_add_i16(fw_cfg
, FW_CFG_SPARC64_HEIGHT
, graphic_height
);
542 fw_cfg_add_i16(fw_cfg
, FW_CFG_SPARC64_DEPTH
, graphic_depth
);
544 qemu_register_boot_set(fw_cfg_boot_set
, fw_cfg
);
552 static const struct hwdef hwdefs
[] = {
553 /* Sun4u generic PC-like machine */
555 .default_cpu_model
= "TI UltraSparc IIi",
556 .machine_id
= sun4u_id
,
557 .prom_addr
= 0x1fff0000000ULL
,
558 .console_serial_base
= 0,
560 /* Sun4v generic PC-like machine */
562 .default_cpu_model
= "Sun UltraSparc T1",
563 .machine_id
= sun4v_id
,
564 .prom_addr
= 0x1fff0000000ULL
,
565 .console_serial_base
= 0,
569 /* Sun4u hardware initialisation */
570 static void sun4u_init(MachineState
*machine
)
572 sun4uv_init(get_system_memory(), machine
, &hwdefs
[0]);
575 /* Sun4v hardware initialisation */
576 static void sun4v_init(MachineState
*machine
)
578 sun4uv_init(get_system_memory(), machine
, &hwdefs
[1]);
581 static void sun4u_class_init(ObjectClass
*oc
, void *data
)
583 MachineClass
*mc
= MACHINE_CLASS(oc
);
585 mc
->desc
= "Sun4u platform";
586 mc
->init
= sun4u_init
;
587 mc
->block_default_type
= IF_IDE
;
588 mc
->max_cpus
= 1; /* XXX for now */
590 mc
->default_boot_order
= "c";
593 static const TypeInfo sun4u_type
= {
594 .name
= MACHINE_TYPE_NAME("sun4u"),
595 .parent
= TYPE_MACHINE
,
596 .class_init
= sun4u_class_init
,
599 static void sun4v_class_init(ObjectClass
*oc
, void *data
)
601 MachineClass
*mc
= MACHINE_CLASS(oc
);
603 mc
->desc
= "Sun4v platform";
604 mc
->init
= sun4v_init
;
605 mc
->block_default_type
= IF_IDE
;
606 mc
->max_cpus
= 1; /* XXX for now */
607 mc
->default_boot_order
= "c";
610 static const TypeInfo sun4v_type
= {
611 .name
= MACHINE_TYPE_NAME("sun4v"),
612 .parent
= TYPE_MACHINE
,
613 .class_init
= sun4v_class_init
,
616 static void sun4u_register_types(void)
618 type_register_static(&ebus_info
);
619 type_register_static(&prom_info
);
620 type_register_static(&ram_info
);
622 type_register_static(&sun4u_type
);
623 type_register_static(&sun4v_type
);
626 type_init(sun4u_register_types
)