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
30 #include "qemu-timer.h"
33 #include "firmware_abi.h"
39 #define DPRINTF(fmt, args...) \
40 do { printf("CPUIRQ: " fmt , ##args); } while (0)
42 #define DPRINTF(fmt, args...)
45 #define KERNEL_LOAD_ADDR 0x00404000
46 #define CMDLINE_ADDR 0x003ff000
47 #define INITRD_LOAD_ADDR 0x00300000
48 #define PROM_SIZE_MAX (4 * 1024 * 1024)
49 #define PROM_VADDR 0x000ffd00000ULL
50 #define APB_SPECIAL_BASE 0x1fe00000000ULL
51 #define APB_MEM_BASE 0x1ff00000000ULL
52 #define VGA_BASE (APB_MEM_BASE + 0x400000ULL)
53 #define PROM_FILENAME "openbios-sparc64"
54 #define NVRAM_SIZE 0x2000
56 #define BIOS_CFG_IOPORT 0x510
60 #define TICK_INT_DIS 0x8000000000000000ULL
61 #define TICK_MAX 0x7fffffffffffffffULL
64 const char * const default_cpu_model
;
67 uint64_t console_serial_base
;
70 int DMA_get_channel_mode (int nchan
)
74 int DMA_read_memory (int nchan
, void *buf
, int pos
, int size
)
78 int DMA_write_memory (int nchan
, void *buf
, int pos
, int size
)
82 void DMA_hold_DREQ (int nchan
) {}
83 void DMA_release_DREQ (int nchan
) {}
84 void DMA_schedule(int nchan
) {}
85 void DMA_init (int high_page_enable
) {}
86 void DMA_register_channel (int nchan
,
87 DMA_transfer_handler transfer_handler
,
92 static int fw_cfg_boot_set(void *opaque
, const char *boot_device
)
94 fw_cfg_add_i16(opaque
, FW_CFG_BOOT_DEVICE
, boot_device
[0]);
98 static int sun4u_NVRAM_set_params (m48t59_t
*nvram
, uint16_t NVRAM_size
,
101 const char *boot_devices
,
102 uint32_t kernel_image
, uint32_t kernel_size
,
104 uint32_t initrd_image
, uint32_t initrd_size
,
105 uint32_t NVRAM_image
,
106 int width
, int height
, int depth
,
107 const uint8_t *macaddr
)
111 uint8_t image
[0x1ff0];
112 struct OpenBIOS_nvpart_v1
*part_header
;
114 memset(image
, '\0', sizeof(image
));
118 // OpenBIOS nvram variables
119 // Variable partition
120 part_header
= (struct OpenBIOS_nvpart_v1
*)&image
[start
];
121 part_header
->signature
= OPENBIOS_PART_SYSTEM
;
122 pstrcpy(part_header
->name
, sizeof(part_header
->name
), "system");
124 end
= start
+ sizeof(struct OpenBIOS_nvpart_v1
);
125 for (i
= 0; i
< nb_prom_envs
; i
++)
126 end
= OpenBIOS_set_var(image
, end
, prom_envs
[i
]);
131 end
= start
+ ((end
- start
+ 15) & ~15);
132 OpenBIOS_finish_partition(part_header
, end
- start
);
136 part_header
= (struct OpenBIOS_nvpart_v1
*)&image
[start
];
137 part_header
->signature
= OPENBIOS_PART_FREE
;
138 pstrcpy(part_header
->name
, sizeof(part_header
->name
), "free");
141 OpenBIOS_finish_partition(part_header
, end
- start
);
143 Sun_init_header((struct Sun_nvram
*)&image
[0x1fd8], macaddr
, 0x80);
145 for (i
= 0; i
< sizeof(image
); i
++)
146 m48t59_write(nvram
, i
, image
[i
]);
151 void pic_info(Monitor
*mon
)
155 void irq_info(Monitor
*mon
)
159 void cpu_check_irqs(CPUState
*env
)
161 uint32_t pil
= env
->pil_in
| (env
->softint
& ~SOFTINT_TIMER
) |
162 ((env
->softint
& SOFTINT_TIMER
) << 14);
164 if (pil
&& (env
->interrupt_index
== 0 ||
165 (env
->interrupt_index
& ~15) == TT_EXTINT
)) {
168 for (i
= 15; i
> 0; i
--) {
169 if (pil
& (1 << i
)) {
170 int old_interrupt
= env
->interrupt_index
;
172 env
->interrupt_index
= TT_EXTINT
| i
;
173 if (old_interrupt
!= env
->interrupt_index
) {
174 DPRINTF("Set CPU IRQ %d\n", i
);
175 cpu_interrupt(env
, CPU_INTERRUPT_HARD
);
180 } else if (!pil
&& (env
->interrupt_index
& ~15) == TT_EXTINT
) {
181 DPRINTF("Reset CPU IRQ %d\n", env
->interrupt_index
& 15);
182 env
->interrupt_index
= 0;
183 cpu_reset_interrupt(env
, CPU_INTERRUPT_HARD
);
187 static void cpu_set_irq(void *opaque
, int irq
, int level
)
189 CPUState
*env
= opaque
;
192 DPRINTF("Raise CPU IRQ %d\n", irq
);
194 env
->pil_in
|= 1 << irq
;
197 DPRINTF("Lower CPU IRQ %d\n", irq
);
198 env
->pil_in
&= ~(1 << irq
);
203 void qemu_system_powerdown(void)
207 typedef struct ResetData
{
212 static void main_cpu_reset(void *opaque
)
214 ResetData
*s
= (ResetData
*)opaque
;
215 CPUState
*env
= s
->env
;
218 env
->tick_cmpr
= TICK_INT_DIS
| 0;
219 ptimer_set_limit(env
->tick
, TICK_MAX
, 1);
220 ptimer_run(env
->tick
, 1);
221 env
->stick_cmpr
= TICK_INT_DIS
| 0;
222 ptimer_set_limit(env
->stick
, TICK_MAX
, 1);
223 ptimer_run(env
->stick
, 1);
224 env
->hstick_cmpr
= TICK_INT_DIS
| 0;
225 ptimer_set_limit(env
->hstick
, TICK_MAX
, 1);
226 ptimer_run(env
->hstick
, 1);
227 env
->gregs
[1] = 0; // Memory start
228 env
->gregs
[2] = ram_size
; // Memory size
229 env
->gregs
[3] = 0; // Machine description XXX
230 env
->pc
= s
->reset_addr
;
231 env
->npc
= env
->pc
+ 4;
234 static void tick_irq(void *opaque
)
236 CPUState
*env
= opaque
;
238 if (!(env
->tick_cmpr
& TICK_INT_DIS
)) {
239 env
->softint
|= SOFTINT_TIMER
;
240 cpu_interrupt(env
, CPU_INTERRUPT_TIMER
);
244 static void stick_irq(void *opaque
)
246 CPUState
*env
= opaque
;
248 if (!(env
->stick_cmpr
& TICK_INT_DIS
)) {
249 env
->softint
|= SOFTINT_STIMER
;
250 cpu_interrupt(env
, CPU_INTERRUPT_TIMER
);
254 static void hstick_irq(void *opaque
)
256 CPUState
*env
= opaque
;
258 if (!(env
->hstick_cmpr
& TICK_INT_DIS
)) {
259 cpu_interrupt(env
, CPU_INTERRUPT_TIMER
);
263 void cpu_tick_set_count(void *opaque
, uint64_t count
)
265 ptimer_set_count(opaque
, -count
);
268 uint64_t cpu_tick_get_count(void *opaque
)
270 return -ptimer_get_count(opaque
);
273 void cpu_tick_set_limit(void *opaque
, uint64_t limit
)
275 ptimer_set_limit(opaque
, -limit
, 0);
278 static const int ide_iobase
[2] = { 0x1f0, 0x170 };
279 static const int ide_iobase2
[2] = { 0x3f6, 0x376 };
280 static const int ide_irq
[2] = { 14, 15 };
282 static const int serial_io
[MAX_SERIAL_PORTS
] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
283 static const int serial_irq
[MAX_SERIAL_PORTS
] = { 4, 3, 4, 3 };
285 static const int parallel_io
[MAX_PARALLEL_PORTS
] = { 0x378, 0x278, 0x3bc };
286 static const int parallel_irq
[MAX_PARALLEL_PORTS
] = { 7, 7, 7 };
288 static fdctrl_t
*floppy_controller
;
290 static void ebus_mmio_mapfunc(PCIDevice
*pci_dev
, int region_num
,
291 uint32_t addr
, uint32_t size
, int type
)
293 DPRINTF("Mapping region %d registers at %08x\n", region_num
, addr
);
294 switch (region_num
) {
296 isa_mmio_init(addr
, 0x1000000);
299 isa_mmio_init(addr
, 0x800000);
304 /* EBUS (Eight bit bus) bridge */
306 pci_ebus_init(PCIBus
*bus
, int devfn
)
310 s
= pci_register_device(bus
, "EBUS", sizeof(*s
), devfn
, NULL
, NULL
);
311 pci_config_set_vendor_id(s
->config
, PCI_VENDOR_ID_SUN
);
312 pci_config_set_device_id(s
->config
, PCI_DEVICE_ID_SUN_EBUS
);
313 s
->config
[0x04] = 0x06; // command = bus master, pci mem
314 s
->config
[0x05] = 0x00;
315 s
->config
[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
316 s
->config
[0x07] = 0x03; // status = medium devsel
317 s
->config
[0x08] = 0x01; // revision
318 s
->config
[0x09] = 0x00; // programming i/f
319 pci_config_set_class(s
->config
, PCI_CLASS_BRIDGE_OTHER
);
320 s
->config
[0x0D] = 0x0a; // latency_timer
321 s
->config
[0x0E] = 0x00; // header_type
323 pci_register_io_region(s
, 0, 0x1000000, PCI_ADDRESS_SPACE_MEM
,
325 pci_register_io_region(s
, 1, 0x800000, PCI_ADDRESS_SPACE_MEM
,
329 static void sun4uv_init(ram_addr_t RAM_size
, int vga_ram_size
,
330 const char *boot_devices
,
331 const char *kernel_filename
, const char *kernel_cmdline
,
332 const char *initrd_filename
, const char *cpu_model
,
333 const struct hwdef
*hwdef
)
340 ram_addr_t ram_offset
, prom_offset
, vga_ram_offset
;
341 long initrd_size
, kernel_size
;
342 PCIBus
*pci_bus
, *pci_bus2
, *pci_bus3
;
346 BlockDriverState
*hd
[MAX_IDE_BUS
* MAX_IDE_DEVS
];
347 BlockDriverState
*fd
[MAX_FD
];
349 ResetData
*reset_info
;
351 linux_boot
= (kernel_filename
!= NULL
);
355 cpu_model
= hwdef
->default_cpu_model
;
357 env
= cpu_init(cpu_model
);
359 fprintf(stderr
, "Unable to find Sparc CPU definition\n");
362 bh
= qemu_bh_new(tick_irq
, env
);
363 env
->tick
= ptimer_init(bh
);
364 ptimer_set_period(env
->tick
, 1ULL);
366 bh
= qemu_bh_new(stick_irq
, env
);
367 env
->stick
= ptimer_init(bh
);
368 ptimer_set_period(env
->stick
, 1ULL);
370 bh
= qemu_bh_new(hstick_irq
, env
);
371 env
->hstick
= ptimer_init(bh
);
372 ptimer_set_period(env
->hstick
, 1ULL);
374 reset_info
= qemu_mallocz(sizeof(ResetData
));
375 reset_info
->env
= env
;
376 reset_info
->reset_addr
= hwdef
->prom_addr
+ 0x40ULL
;
377 qemu_register_reset(main_cpu_reset
, reset_info
);
378 main_cpu_reset(reset_info
);
379 // Override warm reset address with cold start address
380 env
->pc
= hwdef
->prom_addr
+ 0x20ULL
;
381 env
->npc
= env
->pc
+ 4;
384 ram_offset
= qemu_ram_alloc(RAM_size
);
385 cpu_register_physical_memory(0, RAM_size
, ram_offset
);
387 prom_offset
= qemu_ram_alloc(PROM_SIZE_MAX
);
388 cpu_register_physical_memory(hwdef
->prom_addr
,
389 (PROM_SIZE_MAX
+ TARGET_PAGE_SIZE
) &
391 prom_offset
| IO_MEM_ROM
);
393 if (bios_name
== NULL
)
394 bios_name
= PROM_FILENAME
;
395 snprintf(buf
, sizeof(buf
), "%s/%s", bios_dir
, bios_name
);
396 ret
= load_elf(buf
, hwdef
->prom_addr
- PROM_VADDR
, NULL
, NULL
, NULL
);
398 ret
= load_image_targphys(buf
, hwdef
->prom_addr
,
399 (PROM_SIZE_MAX
+ TARGET_PAGE_SIZE
) &
402 fprintf(stderr
, "qemu: could not load prom '%s'\n",
411 /* XXX: put correct offset */
412 kernel_size
= load_elf(kernel_filename
, 0, NULL
, NULL
, NULL
);
414 kernel_size
= load_aout(kernel_filename
, KERNEL_LOAD_ADDR
,
415 ram_size
- KERNEL_LOAD_ADDR
);
417 kernel_size
= load_image_targphys(kernel_filename
,
419 ram_size
- KERNEL_LOAD_ADDR
);
420 if (kernel_size
< 0) {
421 fprintf(stderr
, "qemu: could not load kernel '%s'\n",
427 if (initrd_filename
) {
428 initrd_size
= load_image_targphys(initrd_filename
,
430 ram_size
- INITRD_LOAD_ADDR
);
431 if (initrd_size
< 0) {
432 fprintf(stderr
, "qemu: could not load initial ram disk '%s'\n",
437 if (initrd_size
> 0) {
438 for (i
= 0; i
< 64 * TARGET_PAGE_SIZE
; i
+= TARGET_PAGE_SIZE
) {
439 if (ldl_phys(KERNEL_LOAD_ADDR
+ i
) == 0x48647253) { // HdrS
440 stl_phys(KERNEL_LOAD_ADDR
+ i
+ 16, INITRD_LOAD_ADDR
);
441 stl_phys(KERNEL_LOAD_ADDR
+ i
+ 20, initrd_size
);
447 pci_bus
= pci_apb_init(APB_SPECIAL_BASE
, APB_MEM_BASE
, NULL
, &pci_bus2
,
449 isa_mem_base
= VGA_BASE
;
450 vga_ram_offset
= qemu_ram_alloc(vga_ram_size
);
451 pci_vga_init(pci_bus
, phys_ram_base
+ vga_ram_offset
,
452 vga_ram_offset
, vga_ram_size
,
455 // XXX Should be pci_bus3
456 pci_ebus_init(pci_bus
, -1);
459 if (hwdef
->console_serial_base
) {
460 serial_mm_init(hwdef
->console_serial_base
, 0, NULL
, 115200,
464 for(; i
< MAX_SERIAL_PORTS
; i
++) {
466 serial_init(serial_io
[i
], NULL
/*serial_irq[i]*/, 115200,
471 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
472 if (parallel_hds
[i
]) {
473 parallel_init(parallel_io
[i
], NULL
/*parallel_irq[i]*/,
478 for(i
= 0; i
< nb_nics
; i
++)
479 pci_nic_init(pci_bus
, &nd_table
[i
], -1, "ne2k_pci");
481 irq
= qemu_allocate_irqs(cpu_set_irq
, env
, MAX_PILS
);
482 if (drive_get_max_bus(IF_IDE
) >= MAX_IDE_BUS
) {
483 fprintf(stderr
, "qemu: too many IDE bus\n");
486 for(i
= 0; i
< MAX_IDE_BUS
* MAX_IDE_DEVS
; i
++) {
487 drive_index
= drive_get_index(IF_IDE
, i
/ MAX_IDE_DEVS
,
489 if (drive_index
!= -1)
490 hd
[i
] = drives_table
[drive_index
].bdrv
;
495 pci_cmd646_ide_init(pci_bus
, hd
, 1);
497 /* FIXME: wire up interrupts. */
498 i8042_init(NULL
/*1*/, NULL
/*12*/, 0x60);
499 for(i
= 0; i
< MAX_FD
; i
++) {
500 drive_index
= drive_get_index(IF_FLOPPY
, 0, i
);
501 if (drive_index
!= -1)
502 fd
[i
] = drives_table
[drive_index
].bdrv
;
506 floppy_controller
= fdctrl_init(NULL
/*6*/, 2, 0, 0x3f0, fd
);
507 nvram
= m48t59_init(NULL
/*8*/, 0, 0x0074, NVRAM_SIZE
, 59);
508 sun4u_NVRAM_set_params(nvram
, NVRAM_SIZE
, "Sun4u", RAM_size
, boot_devices
,
509 KERNEL_LOAD_ADDR
, kernel_size
,
511 INITRD_LOAD_ADDR
, initrd_size
,
512 /* XXX: need an option to load a NVRAM image */
514 graphic_width
, graphic_height
, graphic_depth
,
515 (uint8_t *)&nd_table
[0].macaddr
);
517 fw_cfg
= fw_cfg_init(BIOS_CFG_IOPORT
, BIOS_CFG_IOPORT
+ 1, 0, 0);
518 fw_cfg_add_i32(fw_cfg
, FW_CFG_ID
, 1);
519 fw_cfg_add_i64(fw_cfg
, FW_CFG_RAM_SIZE
, (uint64_t)ram_size
);
520 fw_cfg_add_i16(fw_cfg
, FW_CFG_MACHINE_ID
, hwdef
->machine_id
);
521 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ADDR
, KERNEL_LOAD_ADDR
);
522 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_SIZE
, kernel_size
);
523 if (kernel_cmdline
) {
524 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_CMDLINE
, CMDLINE_ADDR
);
525 pstrcpy_targphys(CMDLINE_ADDR
, TARGET_PAGE_SIZE
, kernel_cmdline
);
527 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_CMDLINE
, 0);
529 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_ADDR
, INITRD_LOAD_ADDR
);
530 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_SIZE
, initrd_size
);
531 fw_cfg_add_i16(fw_cfg
, FW_CFG_BOOT_DEVICE
, boot_devices
[0]);
532 qemu_register_boot_set(fw_cfg_boot_set
, fw_cfg
);
541 static const struct hwdef hwdefs
[] = {
542 /* Sun4u generic PC-like machine */
544 .default_cpu_model
= "TI UltraSparc II",
545 .machine_id
= sun4u_id
,
546 .prom_addr
= 0x1fff0000000ULL
,
547 .console_serial_base
= 0,
549 /* Sun4v generic PC-like machine */
551 .default_cpu_model
= "Sun UltraSparc T1",
552 .machine_id
= sun4v_id
,
553 .prom_addr
= 0x1fff0000000ULL
,
554 .console_serial_base
= 0,
556 /* Sun4v generic Niagara machine */
558 .default_cpu_model
= "Sun UltraSparc T1",
559 .machine_id
= niagara_id
,
560 .prom_addr
= 0xfff0000000ULL
,
561 .console_serial_base
= 0xfff0c2c000ULL
,
565 /* Sun4u hardware initialisation */
566 static void sun4u_init(ram_addr_t RAM_size
, int vga_ram_size
,
567 const char *boot_devices
,
568 const char *kernel_filename
, const char *kernel_cmdline
,
569 const char *initrd_filename
, const char *cpu_model
)
571 sun4uv_init(RAM_size
, vga_ram_size
, boot_devices
, kernel_filename
,
572 kernel_cmdline
, initrd_filename
, cpu_model
, &hwdefs
[0]);
575 /* Sun4v hardware initialisation */
576 static void sun4v_init(ram_addr_t RAM_size
, int vga_ram_size
,
577 const char *boot_devices
,
578 const char *kernel_filename
, const char *kernel_cmdline
,
579 const char *initrd_filename
, const char *cpu_model
)
581 sun4uv_init(RAM_size
, vga_ram_size
, boot_devices
, kernel_filename
,
582 kernel_cmdline
, initrd_filename
, cpu_model
, &hwdefs
[1]);
585 /* Niagara hardware initialisation */
586 static void niagara_init(ram_addr_t RAM_size
, int vga_ram_size
,
587 const char *boot_devices
,
588 const char *kernel_filename
, const char *kernel_cmdline
,
589 const char *initrd_filename
, const char *cpu_model
)
591 sun4uv_init(RAM_size
, vga_ram_size
, boot_devices
, kernel_filename
,
592 kernel_cmdline
, initrd_filename
, cpu_model
, &hwdefs
[2]);
595 QEMUMachine sun4u_machine
= {
597 .desc
= "Sun4u platform",
599 .ram_require
= PROM_SIZE_MAX
+ VGA_RAM_SIZE
,
601 .max_cpus
= 1, // XXX for now
604 QEMUMachine sun4v_machine
= {
606 .desc
= "Sun4v platform",
608 .ram_require
= PROM_SIZE_MAX
+ VGA_RAM_SIZE
,
610 .max_cpus
= 1, // XXX for now
613 QEMUMachine niagara_machine
= {
615 .desc
= "Sun4v platform, Niagara",
616 .init
= niagara_init
,
617 .ram_require
= PROM_SIZE_MAX
+ VGA_RAM_SIZE
,
619 .max_cpus
= 1, // XXX for now