2 * Itanium Platform Emulator derived from QEMU PC System Emulator
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Copyright (c) 2007 Intel
7 * Ported for IA64 Platform Zhang Xiantao <xiantao.zhang@intel.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 #include "audio/audio.h"
39 #include "ia64intrin.h"
41 #include "device-assignment.h"
42 #include "virtio-blk.h"
46 #define FW_FILENAME "Flash.fd"
48 /* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables. */
49 #define ACPI_DATA_SIZE 0x10000
53 static fdctrl_t
*floppy_controller
;
54 static RTCState
*rtc_state
;
55 static PCIDevice
*i440fx_state
;
59 static uint32_t ipf_to_legacy_io(target_phys_addr_t addr
)
61 return (uint32_t)(((addr
&0x3ffffff) >> 12 << 2)|((addr
) & 0x3));
64 static void ipf_legacy_io_writeb(void *opaque
, target_phys_addr_t addr
,
66 uint32_t port
= ipf_to_legacy_io(addr
);
68 cpu_outb(0, port
, val
);
71 static void ipf_legacy_io_writew(void *opaque
, target_phys_addr_t addr
,
73 uint32_t port
= ipf_to_legacy_io(addr
);
75 cpu_outw(0, port
, val
);
78 static void ipf_legacy_io_writel(void *opaque
, target_phys_addr_t addr
,
80 uint32_t port
= ipf_to_legacy_io(addr
);
82 cpu_outl(0, port
, val
);
85 static uint32_t ipf_legacy_io_readb(void *opaque
, target_phys_addr_t addr
)
87 uint32_t port
= ipf_to_legacy_io(addr
);
89 return cpu_inb(0, port
);
92 static uint32_t ipf_legacy_io_readw(void *opaque
, target_phys_addr_t addr
)
94 uint32_t port
= ipf_to_legacy_io(addr
);
96 return cpu_inw(0, port
);
99 static uint32_t ipf_legacy_io_readl(void *opaque
, target_phys_addr_t addr
)
101 uint32_t port
= ipf_to_legacy_io(addr
);
103 return cpu_inl(0, port
);
106 static CPUReadMemoryFunc
*ipf_legacy_io_read
[3] = {
112 static CPUWriteMemoryFunc
*ipf_legacy_io_write
[3] = {
113 ipf_legacy_io_writeb
,
114 ipf_legacy_io_writew
,
115 ipf_legacy_io_writel
,
118 static void pic_irq_request(void *opaque
, int irq
, int level
)
120 fprintf(stderr
,"pic_irq_request called!\n");
123 /* PC cmos mappings */
125 #define REG_EQUIPMENT_BYTE 0x14
127 static int cmos_get_fd_drive_type(int fd0
)
133 /* 1.44 Mb 3"5 drive */
137 /* 2.88 Mb 3"5 drive */
141 /* 1.2 Mb 5"5 drive */
151 static void cmos_init_hd(int type_ofs
, int info_ofs
, BlockDriverState
*hd
)
153 RTCState
*s
= rtc_state
;
154 int cylinders
, heads
, sectors
;
156 bdrv_get_geometry_hint(hd
, &cylinders
, &heads
, §ors
);
157 rtc_set_memory(s
, type_ofs
, 47);
158 rtc_set_memory(s
, info_ofs
, cylinders
);
159 rtc_set_memory(s
, info_ofs
+ 1, cylinders
>> 8);
160 rtc_set_memory(s
, info_ofs
+ 2, heads
);
161 rtc_set_memory(s
, info_ofs
+ 3, 0xff);
162 rtc_set_memory(s
, info_ofs
+ 4, 0xff);
163 rtc_set_memory(s
, info_ofs
+ 5, 0xc0 | ((heads
> 8) << 3));
164 rtc_set_memory(s
, info_ofs
+ 6, cylinders
);
165 rtc_set_memory(s
, info_ofs
+ 7, cylinders
>> 8);
166 rtc_set_memory(s
, info_ofs
+ 8, sectors
);
169 /* convert boot_device letter to something recognizable by the bios */
170 static int boot_device2nibble(char boot_device
)
172 switch(boot_device
) {
175 return 0x01; /* floppy boot */
177 return 0x02; /* hard drive boot */
179 return 0x03; /* CD-ROM boot */
181 return 0x04; /* Network boot */
186 /* hd_table must contain 4 block drivers */
187 static void cmos_init(ram_addr_t ram_size
, ram_addr_t above_4g_mem_size
,
188 const char *boot_device
, BlockDriverState
**hd_table
)
190 RTCState
*s
= rtc_state
;
191 int nbds
, bds
[3] = { 0, };
196 /* various important CMOS locations needed by PC/Bochs bios */
199 val
= 640; /* base memory in K */
200 rtc_set_memory(s
, 0x15, val
);
201 rtc_set_memory(s
, 0x16, val
>> 8);
203 val
= (ram_size
/ 1024) - 1024;
206 rtc_set_memory(s
, 0x17, val
);
207 rtc_set_memory(s
, 0x18, val
>> 8);
208 rtc_set_memory(s
, 0x30, val
);
209 rtc_set_memory(s
, 0x31, val
>> 8);
211 if (above_4g_mem_size
) {
212 rtc_set_memory(s
, 0x5b, (unsigned int)above_4g_mem_size
>> 16);
213 rtc_set_memory(s
, 0x5c, (unsigned int)above_4g_mem_size
>> 24);
214 rtc_set_memory(s
, 0x5d, above_4g_mem_size
>> 32);
216 rtc_set_memory(s
, 0x5f, smp_cpus
- 1);
218 if (ram_size
> (16 * 1024 * 1024))
219 val
= (ram_size
/ 65536) - ((16 * 1024 * 1024) / 65536);
224 rtc_set_memory(s
, 0x34, val
);
225 rtc_set_memory(s
, 0x35, val
>> 8);
227 /* set boot devices, and disable floppy signature check if requested */
228 #define PC_MAX_BOOT_DEVICES 3
229 nbds
= strlen(boot_device
);
231 if (nbds
> PC_MAX_BOOT_DEVICES
) {
232 fprintf(stderr
, "Too many boot devices for PC\n");
236 for (i
= 0; i
< nbds
; i
++) {
237 bds
[i
] = boot_device2nibble(boot_device
[i
]);
239 fprintf(stderr
, "Invalid boot device for PC: '%c'\n",
245 rtc_set_memory(s
, 0x3d, (bds
[1] << 4) | bds
[0]);
246 rtc_set_memory(s
, 0x38, (bds
[2] << 4) | (fd_bootchk
? 0x0 : 0x1));
250 fd0
= fdctrl_get_drive_type(floppy_controller
, 0);
251 fd1
= fdctrl_get_drive_type(floppy_controller
, 1);
253 val
= (cmos_get_fd_drive_type(fd0
) << 4) | cmos_get_fd_drive_type(fd1
);
254 rtc_set_memory(s
, 0x10, val
);
267 val
|= 0x01; /* 1 drive, ready for boot */
270 val
|= 0x41; /* 2 drives, ready for boot */
274 val
|= 0x02; /* FPU is there */
275 val
|= 0x04; /* PS/2 mouse installed */
276 rtc_set_memory(s
, REG_EQUIPMENT_BYTE
, val
);
280 rtc_set_memory(s
, 0x12, (hd_table
[0] ? 0xf0 : 0) | (hd_table
[1] ? 0x0f : 0));
282 cmos_init_hd(0x19, 0x1b, hd_table
[0]);
284 cmos_init_hd(0x1a, 0x24, hd_table
[1]);
287 for (i
= 0; i
< 4; i
++) {
289 int cylinders
, heads
, sectors
, translation
;
290 /* NOTE: bdrv_get_geometry_hint() returns the physical
291 geometry. It is always such that: 1 <= sects <= 63, 1
292 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
293 geometry can be different if a translation is done. */
294 translation
= bdrv_get_translation_hint(hd_table
[i
]);
295 if (translation
== BIOS_ATA_TRANSLATION_AUTO
) {
296 bdrv_get_geometry_hint(hd_table
[i
], &cylinders
,
298 if (cylinders
<= 1024 && heads
<= 16 && sectors
<= 63) {
299 /* No translation. */
302 /* LBA translation. */
308 val
|= translation
<< (i
* 2);
311 rtc_set_memory(s
, 0x39, val
);
314 static void main_cpu_reset(void *opaque
)
316 CPUState
*env
= opaque
;
320 static const int ide_iobase
[2] = { 0x1f0, 0x170 };
321 static const int ide_iobase2
[2] = { 0x3f6, 0x376 };
322 static const int ide_irq
[2] = { 14, 15 };
324 #define NE2000_NB_MAX 6
326 static int ne2000_io
[NE2000_NB_MAX
] = { 0x300, 0x320, 0x340,
327 0x360, 0x280, 0x380 };
328 static int ne2000_irq
[NE2000_NB_MAX
] = { 9, 10, 11, 3, 4, 5 };
330 static int serial_io
[MAX_SERIAL_PORTS
] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
331 static int serial_irq
[MAX_SERIAL_PORTS
] = { 4, 3, 4, 3 };
333 static int parallel_io
[MAX_PARALLEL_PORTS
] = { 0x378, 0x278, 0x3bc };
334 static int parallel_irq
[MAX_PARALLEL_PORTS
] = { 7, 7, 7 };
337 static void audio_init (PCIBus
*pci_bus
, qemu_irq
*pic
)
340 int audio_enabled
= 0;
342 for (c
= soundhw
; !audio_enabled
&& c
->name
; ++c
) {
343 audio_enabled
= c
->enabled
;
351 for (c
= soundhw
; c
->name
; ++c
) {
354 c
->init
.init_isa (s
, pic
);
357 c
->init
.init_pci (pci_bus
, s
);
367 static void pc_init_ne2k_isa(NICInfo
*nd
, qemu_irq
*pic
)
369 static int nb_ne2k
= 0;
371 if (nb_ne2k
== NE2000_NB_MAX
)
373 isa_ne2000_init(ne2000_io
[nb_ne2k
], pic
[ne2000_irq
[nb_ne2k
]], nd
);
377 /* Itanium hardware initialisation */
378 static void ipf_init1(ram_addr_t ram_size
, int vga_ram_size
,
379 const char *boot_device
, DisplayState
*ds
,
380 const char *kernel_filename
, const char *kernel_cmdline
,
381 const char *initrd_filename
,
382 int pci_enabled
, const char *cpu_model
)
386 ram_addr_t ram_addr
, vga_ram_addr
;
387 ram_addr_t above_4g_mem_size
= 0;
389 int piix3_devfn
= -1;
395 unsigned long ipf_legacy_io_base
, ipf_legacy_io_mem
;
396 BlockDriverState
*hd
[MAX_IDE_BUS
* MAX_IDE_DEVS
];
397 BlockDriverState
*fd
[MAX_FD
];
399 page_size
= getpagesize();
400 if (page_size
!= TARGET_PAGE_SIZE
) {
401 fprintf(stderr
,"Error! Host page size != qemu target page size,"
402 " you may need to change TARGET_PAGE_BITS in qemu!"
403 "host page size:0x%x\n", page_size
);
407 if (ram_size
>= 0xc0000000 ) {
408 above_4g_mem_size
= ram_size
- 0xc0000000;
409 ram_size
= 0xc0000000;
413 if (cpu_model
== NULL
) {
417 for(i
= 0; i
< smp_cpus
; i
++) {
418 env
= cpu_init(cpu_model
);
420 fprintf(stderr
, "Unable to find CPU definition\n");
424 env
->hflags
|= HF_HALTED_MASK
;
425 register_savevm("cpu", i
, 4, cpu_save
, cpu_load
, env
);
426 qemu_register_reset(main_cpu_reset
, env
);
431 ram_addr
= qemu_ram_alloc(0xa0000);
432 cpu_register_physical_memory(0, 0xa0000, ram_addr
);
434 ram_addr
= qemu_ram_alloc(0x20000); // Workaround 0xa0000-0xc0000
436 ram_addr
= qemu_ram_alloc(0x40000);
437 cpu_register_physical_memory(0xc0000, 0x40000, ram_addr
);
439 ram_addr
= qemu_ram_alloc(ram_size
- 0x100000);
440 cpu_register_physical_memory(0x100000, ram_size
- 0x100000, ram_addr
);
442 ram_addr
= qemu_ram_alloc(ram_size
);
443 cpu_register_physical_memory(0, ram_size
, ram_addr
);
445 /* allocate VGA RAM */
446 vga_ram_addr
= qemu_ram_alloc(vga_ram_size
);
448 /* above 4giga memory allocation */
449 if (above_4g_mem_size
> 0) {
450 ram_addr
= qemu_ram_alloc(above_4g_mem_size
);
451 cpu_register_physical_memory(0x100000000, above_4g_mem_size
, ram_addr
);
454 /*Load firware to its proper position.*/
456 unsigned long image_size
;
457 uint8_t *image
= NULL
;
458 target_phys_addr_t fw_image_start
;
459 unsigned long nvram_addr
= 0;
460 unsigned long nvram_fd
= 0;
461 unsigned long type
= READ_FROM_NVRAM
;
464 ram_addr
= qemu_ram_alloc(GFW_SIZE
);
465 gfw_start
= qemu_get_ram_ptr(ram_addr
);
466 cpu_register_physical_memory(GFW_START
, GFW_SIZE
, ram_addr
);
468 snprintf(buf
, sizeof(buf
), "%s/%s", bios_dir
, FW_FILENAME
);
469 image
= read_image(buf
, &image_size
);
470 if (NULL
== image
|| !image_size
) {
471 fprintf(stderr
, "Error when reading Guest Firmware!\n");
472 fprintf(stderr
, "Please check Guest firmware at %s\n", buf
);
476 /* Load Guest Firmware to the proper postion. */
477 fw_image_start
= GFW_START
+ GFW_SIZE
- image_size
;
478 cpu_physical_memory_write(fw_image_start
, image
, image_size
);
483 nvram_addr
= NVRAM_START
;
484 nvram_fd
= kvm_ia64_nvram_init(type
);
485 if (nvram_fd
!= -1) {
486 kvm_ia64_copy_from_nvram_to_GFW(nvram_fd
, gfw_start
);
489 i
= atexit((void *)kvm_ia64_copy_from_GFW_to_nvram
);
491 fprintf(stderr
, "cannot set exit function\n");
493 kvm_ia64_build_hob(ram_size
+ above_4g_mem_size
, smp_cpus
,
494 gfw_start
, nvram_addr
);
497 /*Register legacy io address space, size:64M*/
498 ipf_legacy_io_base
= 0xE0000000;
499 ipf_legacy_io_mem
= cpu_register_io_memory(0, ipf_legacy_io_read
,
500 ipf_legacy_io_write
, NULL
);
501 cpu_register_physical_memory(ipf_legacy_io_base
, 64*1024*1024,
504 cpu_irq
= qemu_allocate_irqs(pic_irq_request
, first_cpu
, 1);
505 i8259
= i8259_init(cpu_irq
[0]);
508 pci_bus
= i440fx_init(&i440fx_state
, i8259
);
509 piix3_devfn
= piix3_init(pci_bus
, -1);
514 if (cirrus_vga_enabled
) {
516 pci_cirrus_vga_init(pci_bus
, vga_ram_size
);
518 isa_cirrus_vga_init(vga_ram_size
);
522 pci_vga_init(pci_bus
, vga_ram_size
, 0, 0);
524 isa_vga_init(vga_ram_size
);
528 rtc_state
= rtc_init(0x70, i8259
[8], 2000);
531 pic_set_alt_irq_func(isa_pic
, NULL
, NULL
);
534 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
536 serial_init(serial_io
[i
], i8259
[serial_irq
[i
]], 115200,
541 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
542 if (parallel_hds
[i
]) {
543 parallel_init(parallel_io
[i
], i8259
[parallel_irq
[i
]],
548 for(i
= 0; i
< nb_nics
; i
++) {
549 NICInfo
*nd
= &nd_table
[i
];
551 if (!pci_enabled
|| (nd
->model
&& strcmp(nd
->model
, "ne2k_isa") == 0))
552 pc_init_ne2k_isa(nd
, i8259
);
554 pci_nic_init(pci_bus
, nd
, -1, "e1000");
557 #undef USE_HYPERCALL //Disable it now, need to implement later!
559 pci_hypercall_init(pci_bus
);
562 if (drive_get_max_bus(IF_IDE
) >= MAX_IDE_BUS
) {
563 fprintf(stderr
, "qemu: too many IDE bus\n");
567 for(i
= 0; i
< MAX_IDE_BUS
* MAX_IDE_DEVS
; i
++) {
568 index
= drive_get_index(IF_IDE
, i
/ MAX_IDE_DEVS
, i
% MAX_IDE_DEVS
);
570 hd
[i
] = drives_table
[index
].bdrv
;
576 pci_piix3_ide_init(pci_bus
, hd
, piix3_devfn
+ 1, i8259
);
578 for(i
= 0; i
< MAX_IDE_BUS
; i
++) {
579 isa_ide_init(ide_iobase
[i
], ide_iobase2
[i
], i8259
[ide_irq
[i
]],
580 hd
[MAX_IDE_DEVS
* i
], hd
[MAX_IDE_DEVS
* i
+ 1]);
584 i8042_init(i8259
[1], i8259
[12], 0x60);
587 audio_init(pci_enabled
? pci_bus
: NULL
, i8259
);
590 for(i
= 0; i
< MAX_FD
; i
++) {
591 index
= drive_get_index(IF_FLOPPY
, 0, i
);
593 fd
[i
] = drives_table
[index
].bdrv
;
597 floppy_controller
= fdctrl_init(i8259
[6], 2, 0, 0x3f0, fd
);
599 cmos_init(ram_size
, above_4g_mem_size
, boot_device
, hd
);
601 if (pci_enabled
&& usb_enabled
) {
602 usb_uhci_piix3_init(pci_bus
, piix3_devfn
+ 2);
605 if (pci_enabled
&& acpi_enabled
) {
606 uint8_t *eeprom_buf
= qemu_mallocz(8 * 256); /* XXX: make this persistent */
609 /* TODO: Populate SPD eeprom data. */
610 smbus
= piix4_pm_init(pci_bus
, piix3_devfn
+ 3, 0xb100, i8259
[9]);
611 for (i
= 0; i
< 8; i
++) {
612 smbus_eeprom_device_init(smbus
, 0x50 + i
, eeprom_buf
+ (i
* 256));
617 i440fx_init_memory_mappings(i440fx_state
);
625 max_bus
= drive_get_max_bus(IF_SCSI
);
627 for (bus
= 0; bus
<= max_bus
; bus
++) {
628 scsi
= lsi_scsi_init(pci_bus
, -1);
629 for (unit
= 0; unit
< LSI_MAX_DEVS
; unit
++) {
630 index
= drive_get_index(IF_SCSI
, bus
, unit
);
633 lsi_scsi_attach(scsi
, drives_table
[index
].bdrv
, unit
);
637 /* Add virtio block devices */
642 while ((index
= drive_get_index(IF_VIRTIO
, 0, unit_id
)) != -1) {
643 virtio_blk_init(pci_bus
, drives_table
[index
].bdrv
);
648 #ifdef USE_KVM_DEVICE_ASSIGNMENT
650 add_assigned_devices(pci_bus
, assigned_devices
, assigned_devices_index
);
651 #endif /* USE_KVM_DEVICE_ASSIGNMENT */
655 static void ipf_init_pci(ram_addr_t ram_size
, int vga_ram_size
,
656 const char *boot_device
, DisplayState
*ds
,
657 const char *kernel_filename
,
658 const char *kernel_cmdline
,
659 const char *initrd_filename
,
660 const char *cpu_model
)
662 ipf_init1(ram_size
, vga_ram_size
, boot_device
, ds
, kernel_filename
,
663 kernel_cmdline
, initrd_filename
, 1, cpu_model
);
666 QEMUMachine ipf_machine
= {
668 .desc
= "Itanium Platform",
669 .init
= (QEMUMachineInitFunc
*)ipf_init_pci
,
673 #define IOAPIC_NUM_PINS 48
675 static int ioapic_irq_count
[IOAPIC_NUM_PINS
];
677 static int ioapic_map_irq(int devfn
, int irq_num
)
681 irq
= ((((dev
<< 2) + (dev
>> 3) + irq_num
) & 31) + 16);
686 * Dummy function to provide match for call from hw/apic.c
688 void apic_set_irq_delivered(void) {
691 void ioapic_set_irq(void *opaque
, int irq_num
, int level
)
695 PCIDevice
*pci_dev
= (PCIDevice
*)opaque
;
696 vector
= ioapic_map_irq(pci_dev
->devfn
, irq_num
);
699 ioapic_irq_count
[vector
] += 1;
701 ioapic_irq_count
[vector
] -= 1;
704 if (kvm_set_irq(vector
, ioapic_irq_count
[vector
] == 0, &pic_ret
))
706 apic_set_irq_delivered();
711 int ipf_map_irq(PCIDevice
*pci_dev
, int irq_num
)
713 return ioapic_map_irq(pci_dev
->devfn
, irq_num
);