kvm: qemu: remove kernel memory allocation code from ipf.c
[kvm-userspace.git] / qemu / hw / ipf.c
blobd70af909833613063f067ac593a5cdd6cafe54e7
1 /*
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
25 * THE SOFTWARE.
28 #include "hw.h"
29 #include "pc.h"
30 #include "fdc.h"
31 #include "pci.h"
32 #include "block.h"
33 #include "sysemu.h"
34 #include "audio/audio.h"
35 #include "net.h"
36 #include "smbus.h"
37 #include "boards.h"
38 #include "firmware.h"
39 #include "ia64intrin.h"
40 #include <unistd.h>
42 #include "qemu-kvm.h"
44 #define FW_FILENAME "Flash.fd"
46 /* Leave a chunk of memory at the top of RAM for the BIOS ACPI tables. */
47 #define ACPI_DATA_SIZE 0x10000
49 #define MAX_IDE_BUS 2
51 static fdctrl_t *floppy_controller;
52 static RTCState *rtc_state;
53 static PCIDevice *i440fx_state;
55 static uint32_t ipf_to_legacy_io(target_phys_addr_t addr)
57 return (uint32_t)(((addr&0x3ffffff) >> 12 << 2)|((addr) & 0x3));
60 static void ipf_legacy_io_writeb(void *opaque, target_phys_addr_t addr,
61 uint32_t val) {
62 uint32_t port = ipf_to_legacy_io(addr);
63 cpu_outb(0, port, val);
66 static void ipf_legacy_io_writew(void *opaque, target_phys_addr_t addr,
67 uint32_t val) {
68 uint32_t port = ipf_to_legacy_io(addr);
70 cpu_outw(0, port, val);
73 static void ipf_legacy_io_writel(void *opaque, target_phys_addr_t addr,
74 uint32_t val) {
75 uint32_t port = ipf_to_legacy_io(addr);
77 cpu_outl(0, port, val);
80 static uint32_t ipf_legacy_io_readb(void *opaque, target_phys_addr_t addr)
82 uint32_t port = ipf_to_legacy_io(addr);
84 return cpu_inb(0, port);
87 static uint32_t ipf_legacy_io_readw(void *opaque, target_phys_addr_t addr)
89 uint32_t port = ipf_to_legacy_io(addr);
91 return cpu_inw(0, port);
94 static uint32_t ipf_legacy_io_readl(void *opaque, target_phys_addr_t addr)
96 uint32_t port = ipf_to_legacy_io(addr);
98 return cpu_inl(0, port);
101 static CPUReadMemoryFunc *ipf_legacy_io_read[3] = {
102 ipf_legacy_io_readb,
103 ipf_legacy_io_readw,
104 ipf_legacy_io_readl,
107 static CPUWriteMemoryFunc *ipf_legacy_io_write[3] = {
108 ipf_legacy_io_writeb,
109 ipf_legacy_io_writew,
110 ipf_legacy_io_writel,
113 static void pic_irq_request(void *opaque, int irq, int level)
115 fprintf(stderr,"pic_irq_request called!\n");
118 /* PC cmos mappings */
120 #define REG_EQUIPMENT_BYTE 0x14
122 static int cmos_get_fd_drive_type(int fd0)
124 int val;
126 switch (fd0) {
127 case 0:
128 /* 1.44 Mb 3"5 drive */
129 val = 4;
130 break;
131 case 1:
132 /* 2.88 Mb 3"5 drive */
133 val = 5;
134 break;
135 case 2:
136 /* 1.2 Mb 5"5 drive */
137 val = 2;
138 break;
139 default:
140 val = 0;
141 break;
143 return val;
146 static void cmos_init_hd(int type_ofs, int info_ofs, BlockDriverState *hd)
148 RTCState *s = rtc_state;
149 int cylinders, heads, sectors;
150 bdrv_get_geometry_hint(hd, &cylinders, &heads, &sectors);
151 rtc_set_memory(s, type_ofs, 47);
152 rtc_set_memory(s, info_ofs, cylinders);
153 rtc_set_memory(s, info_ofs + 1, cylinders >> 8);
154 rtc_set_memory(s, info_ofs + 2, heads);
155 rtc_set_memory(s, info_ofs + 3, 0xff);
156 rtc_set_memory(s, info_ofs + 4, 0xff);
157 rtc_set_memory(s, info_ofs + 5, 0xc0 | ((heads > 8) << 3));
158 rtc_set_memory(s, info_ofs + 6, cylinders);
159 rtc_set_memory(s, info_ofs + 7, cylinders >> 8);
160 rtc_set_memory(s, info_ofs + 8, sectors);
163 /* convert boot_device letter to something recognizable by the bios */
164 static int boot_device2nibble(char boot_device)
166 switch(boot_device) {
167 case 'a':
168 case 'b':
169 return 0x01; /* floppy boot */
170 case 'c':
171 return 0x02; /* hard drive boot */
172 case 'd':
173 return 0x03; /* CD-ROM boot */
174 case 'n':
175 return 0x04; /* Network boot */
177 return 0;
180 /* hd_table must contain 4 block drivers */
181 static void cmos_init(ram_addr_t ram_size, ram_addr_t above_4g_mem_size,
182 const char *boot_device, BlockDriverState **hd_table,
183 int smp_cpus)
185 RTCState *s = rtc_state;
186 int nbds, bds[3] = { 0, };
187 int val;
188 int fd0, fd1, nb;
189 int i;
191 /* various important CMOS locations needed by PC/Bochs bios */
193 /* memory size */
194 val = 640; /* base memory in K */
195 rtc_set_memory(s, 0x15, val);
196 rtc_set_memory(s, 0x16, val >> 8);
198 val = (ram_size / 1024) - 1024;
199 if (val > 65535)
200 val = 65535;
201 rtc_set_memory(s, 0x17, val);
202 rtc_set_memory(s, 0x18, val >> 8);
203 rtc_set_memory(s, 0x30, val);
204 rtc_set_memory(s, 0x31, val >> 8);
206 if (above_4g_mem_size) {
207 rtc_set_memory(s, 0x5b, (unsigned int)above_4g_mem_size >> 16);
208 rtc_set_memory(s, 0x5c, (unsigned int)above_4g_mem_size >> 24);
209 rtc_set_memory(s, 0x5d, above_4g_mem_size >> 32);
211 rtc_set_memory(s, 0x5f, smp_cpus - 1);
213 if (ram_size > (16 * 1024 * 1024))
214 val = (ram_size / 65536) - ((16 * 1024 * 1024) / 65536);
215 else
216 val = 0;
217 if (val > 65535)
218 val = 65535;
219 rtc_set_memory(s, 0x34, val);
220 rtc_set_memory(s, 0x35, val >> 8);
222 /* set boot devices, and disable floppy signature check if requested */
223 #define PC_MAX_BOOT_DEVICES 3
224 nbds = strlen(boot_device);
225 if (nbds > PC_MAX_BOOT_DEVICES) {
226 fprintf(stderr, "Too many boot devices for PC\n");
227 exit(1);
229 for (i = 0; i < nbds; i++) {
230 bds[i] = boot_device2nibble(boot_device[i]);
231 if (bds[i] == 0) {
232 fprintf(stderr, "Invalid boot device for PC: '%c'\n",
233 boot_device[i]);
234 exit(1);
237 rtc_set_memory(s, 0x3d, (bds[1] << 4) | bds[0]);
238 rtc_set_memory(s, 0x38, (bds[2] << 4) | (fd_bootchk ? 0x0 : 0x1));
240 /* floppy type */
242 fd0 = fdctrl_get_drive_type(floppy_controller, 0);
243 fd1 = fdctrl_get_drive_type(floppy_controller, 1);
245 val = (cmos_get_fd_drive_type(fd0) << 4) | cmos_get_fd_drive_type(fd1);
246 rtc_set_memory(s, 0x10, val);
248 val = 0;
249 nb = 0;
250 if (fd0 < 3)
251 nb++;
252 if (fd1 < 3)
253 nb++;
254 switch (nb) {
255 case 0:
256 break;
257 case 1:
258 val |= 0x01; /* 1 drive, ready for boot */
259 break;
260 case 2:
261 val |= 0x41; /* 2 drives, ready for boot */
262 break;
264 val |= 0x02; /* FPU is there */
265 val |= 0x04; /* PS/2 mouse installed */
266 rtc_set_memory(s, REG_EQUIPMENT_BYTE, val);
268 /* hard drives */
270 rtc_set_memory(s, 0x12, (hd_table[0] ? 0xf0 : 0) | (hd_table[1] ? 0x0f : 0));
271 if (hd_table[0])
272 cmos_init_hd(0x19, 0x1b, hd_table[0]);
273 if (hd_table[1])
274 cmos_init_hd(0x1a, 0x24, hd_table[1]);
276 val = 0;
277 for (i = 0; i < 4; i++) {
278 if (hd_table[i]) {
279 int cylinders, heads, sectors, translation;
280 /* NOTE: bdrv_get_geometry_hint() returns the physical
281 geometry. It is always such that: 1 <= sects <= 63, 1
282 <= heads <= 16, 1 <= cylinders <= 16383. The BIOS
283 geometry can be different if a translation is done. */
284 translation = bdrv_get_translation_hint(hd_table[i]);
285 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
286 bdrv_get_geometry_hint(hd_table[i], &cylinders, &heads, &sectors);
287 if (cylinders <= 1024 && heads <= 16 && sectors <= 63) {
288 /* No translation. */
289 translation = 0;
290 } else {
291 /* LBA translation. */
292 translation = 1;
294 } else {
295 translation--;
297 val |= translation << (i * 2);
300 rtc_set_memory(s, 0x39, val);
303 static void main_cpu_reset(void *opaque)
305 CPUState *env = opaque;
306 cpu_reset(env);
309 static const int ide_iobase[2] = { 0x1f0, 0x170 };
310 static const int ide_iobase2[2] = { 0x3f6, 0x376 };
311 static const int ide_irq[2] = { 14, 15 };
313 #define NE2000_NB_MAX 6
315 static int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
316 static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
318 static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
319 static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
321 static int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
322 static int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
324 #ifdef HAS_AUDIO
325 static void audio_init (PCIBus *pci_bus, qemu_irq *pic)
327 struct soundhw *c;
328 int audio_enabled = 0;
330 for (c = soundhw; !audio_enabled && c->name; ++c) {
331 audio_enabled = c->enabled;
334 if (audio_enabled) {
335 AudioState *s;
337 s = AUD_init ();
338 if (s) {
339 for (c = soundhw; c->name; ++c) {
340 if (c->enabled) {
341 if (c->isa) {
342 c->init.init_isa (s, pic);
344 else {
345 if (pci_bus) {
346 c->init.init_pci (pci_bus, s);
354 #endif
356 static void pc_init_ne2k_isa(NICInfo *nd, qemu_irq *pic)
358 static int nb_ne2k = 0;
360 if (nb_ne2k == NE2000_NB_MAX)
361 return;
362 isa_ne2000_init(ne2000_io[nb_ne2k], pic[ne2000_irq[nb_ne2k]], nd);
363 nb_ne2k++;
366 /* Itanium hardware initialisation */
367 static void ipf_init1(ram_addr_t ram_size, int vga_ram_size,
368 const char *boot_device, DisplayState *ds,
369 const char *kernel_filename, const char *kernel_cmdline,
370 const char *initrd_filename,
371 int pci_enabled, const char *cpu_model)
373 char buf[1024];
374 int i;
375 ram_addr_t ram_addr, vga_ram_addr;
376 ram_addr_t above_4g_mem_size = 0;
377 PCIBus *pci_bus;
378 int piix3_devfn = -1;
379 CPUState *env;
380 NICInfo *nd;
381 qemu_irq *cpu_irq;
382 qemu_irq *i8259;
383 int page_size;
384 int index;
385 unsigned long ipf_legacy_io_base, ipf_legacy_io_mem;
386 BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
387 BlockDriverState *fd[MAX_FD];
389 page_size = getpagesize();
390 if (page_size != TARGET_PAGE_SIZE) {
391 fprintf(stderr,"Error! Host page size != qemu target page size,"
392 " you may need to change TARGET_PAGE_BITS in qemu!"
393 "host page size:0x%x\n", page_size);
394 exit(-1);
397 if (ram_size >= 0xc0000000 ) {
398 above_4g_mem_size = ram_size - 0xc0000000;
399 ram_size = 0xc0000000;
402 /* init CPUs */
403 if (cpu_model == NULL) {
404 cpu_model = "IA64";
407 for(i = 0; i < smp_cpus; i++) {
408 env = cpu_init(cpu_model);
409 if (!env) {
410 fprintf(stderr, "Unable to find CPU definition\n");
411 exit(1);
413 if (i != 0)
414 env->hflags |= HF_HALTED_MASK;
415 register_savevm("cpu", i, 4, cpu_save, cpu_load, env);
416 qemu_register_reset(main_cpu_reset, env);
419 /* allocate RAM */
420 if (kvm_enabled()) {
421 ram_addr = qemu_ram_alloc(0xa0000);
422 cpu_register_physical_memory(0, 0xa0000, ram_addr);
423 kvm_cpu_register_physical_memory(0, 0xa0000, ram_addr);
425 ram_addr = qemu_ram_alloc(0x20000); // Workaround 0xa0000-0xc0000
427 ram_addr = qemu_ram_alloc(0x40000);
428 cpu_register_physical_memory(0xc0000, 0x40000, ram_addr);
429 kvm_cpu_register_physical_memory(0xc0000, 0x40000, ram_addr);
431 ram_addr = qemu_ram_alloc(ram_size - 0x100000);
432 cpu_register_physical_memory(0x100000, ram_size - 0x100000, ram_addr);
433 kvm_cpu_register_physical_memory(0x100000, ram_size - 0x100000,
434 ram_addr);
435 } else
437 ram_addr = qemu_ram_alloc(ram_size);
438 cpu_register_physical_memory(0, ram_size, ram_addr);
440 /* allocate VGA RAM */
441 vga_ram_addr = qemu_ram_alloc(vga_ram_size);
443 /* above 4giga memory allocation */
444 if (above_4g_mem_size > 0) {
445 ram_addr = qemu_ram_alloc(above_4g_mem_size);
446 cpu_register_physical_memory(0x100000000, above_4g_mem_size, ram_addr);
447 if (kvm_enabled())
448 kvm_cpu_register_physical_memory(0x100000000, above_4g_mem_size,
449 ram_addr);
452 /*Load firware to its proper position.*/
453 if (kvm_enabled()) {
454 int r;
455 unsigned long image_size;
456 char *image = NULL;
457 uint8_t *fw_image_start;
458 ram_addr_t fw_offset = qemu_ram_alloc(GFW_SIZE);
459 uint8_t *fw_start = phys_ram_base + fw_offset;
461 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, FW_FILENAME);
462 image = read_image(buf, &image_size );
463 if (NULL == image || !image_size) {
464 fprintf(stderr, "Error when reading Guest Firmware!\n");
465 fprintf(stderr, "Please check Guest firmware at %s\n", buf);
466 exit(1);
468 fw_image_start = fw_start + GFW_SIZE - image_size;
470 cpu_register_physical_memory(GFW_START, GFW_SIZE, fw_offset);
471 kvm_cpu_register_physical_memory(GFW_START,GFW_SIZE, fw_offset);
472 memcpy(fw_image_start, image, image_size);
474 free(image);
475 flush_icache_range((unsigned long)fw_image_start,
476 (unsigned long)fw_image_start + image_size);
477 kvm_ia64_build_hob(ram_size + above_4g_mem_size, smp_cpus, fw_start);
480 /*Register legacy io address space, size:64M*/
481 ipf_legacy_io_base = 0xE0000000;
482 ipf_legacy_io_mem = cpu_register_io_memory(0, ipf_legacy_io_read,
483 ipf_legacy_io_write, NULL);
484 cpu_register_physical_memory(ipf_legacy_io_base, 64*1024*1024, ipf_legacy_io_mem);
486 cpu_irq = qemu_allocate_irqs(pic_irq_request, first_cpu, 1);
487 i8259 = i8259_init(cpu_irq[0]);
489 if (pci_enabled) {
490 pci_bus = i440fx_init(&i440fx_state, i8259);
491 piix3_devfn = piix3_init(pci_bus, -1);
492 } else {
493 pci_bus = NULL;
496 if (cirrus_vga_enabled) {
497 if (pci_enabled) {
498 pci_cirrus_vga_init(pci_bus,
499 ds, phys_ram_base + vga_ram_addr,
500 vga_ram_addr, vga_ram_size);
501 } else {
502 isa_cirrus_vga_init(ds, phys_ram_base + vga_ram_addr,
503 vga_ram_addr, vga_ram_size);
505 } else {
506 if (pci_enabled) {
507 pci_vga_init(pci_bus, ds, phys_ram_base + vga_ram_addr,
508 vga_ram_addr, vga_ram_size, 0, 0);
509 } else {
510 isa_vga_init(ds, phys_ram_base + vga_ram_addr,
511 vga_ram_addr, vga_ram_size);
515 rtc_state = rtc_init(0x70, i8259[8]);
517 if (pci_enabled) {
518 pic_set_alt_irq_func(isa_pic, NULL, NULL);
521 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
522 if (serial_hds[i]) {
523 serial_init(serial_io[i], i8259[serial_irq[i]], 115200,
524 serial_hds[i]);
528 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
529 if (parallel_hds[i]) {
530 parallel_init(parallel_io[i], i8259[parallel_irq[i]],
531 parallel_hds[i]);
535 for(i = 0; i < nb_nics; i++) {
536 nd = &nd_table[i];
537 if (!nd->model) {
538 if (pci_enabled) {
539 nd->model = "ne2k_pci";
540 } else {
541 nd->model = "ne2k_isa";
544 if (strcmp(nd->model, "ne2k_isa") == 0) {
545 pc_init_ne2k_isa(nd, i8259);
546 } else if (pci_enabled) {
547 if (strcmp(nd->model, "?") == 0)
548 fprintf(stderr, "qemu: Supported ISA NICs: ne2k_isa\n");
549 if (!pci_nic_init(pci_bus, nd, -1))
550 exit(1);
551 } else if (strcmp(nd->model, "?") == 0) {
552 fprintf(stderr, "qemu: Supported ISA NICs: ne2k_isa\n");
553 exit(1);
554 } else {
555 fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd->model);
556 exit(1);
560 #undef USE_HYPERCALL //Disable it now, need to implement later!
561 #ifdef USE_HYPERCALL
562 pci_hypercall_init(pci_bus);
563 #endif
565 if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
566 fprintf(stderr, "qemu: too many IDE bus\n");
567 exit(1);
570 for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
571 index = drive_get_index(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
572 if (index != -1)
573 hd[i] = drives_table[index].bdrv;
574 else
575 hd[i] = NULL;
578 if (pci_enabled) {
579 pci_piix3_ide_init(pci_bus, hd, piix3_devfn + 1, i8259);
580 } else {
581 for(i = 0; i < MAX_IDE_BUS; i++) {
582 isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]],
583 hd[MAX_IDE_DEVS * i], hd[MAX_IDE_DEVS * i + 1]);
587 i8042_init(i8259[1], i8259[12], 0x60);
588 DMA_init(0);
589 #ifdef HAS_AUDIO
590 audio_init(pci_enabled ? pci_bus : NULL, i8259);
591 #endif
593 for(i = 0; i < MAX_FD; i++) {
594 index = drive_get_index(IF_FLOPPY, 0, i);
595 if (index != -1)
596 fd[i] = drives_table[index].bdrv;
597 else
598 fd[i] = NULL;
600 floppy_controller = fdctrl_init(i8259[6], 2, 0, 0x3f0, fd);
602 cmos_init(ram_size, above_4g_mem_size, boot_device, hd, smp_cpus);
604 if (pci_enabled && usb_enabled) {
605 usb_uhci_piix3_init(pci_bus, piix3_devfn + 2);
608 if (pci_enabled && acpi_enabled) {
609 uint8_t *eeprom_buf = qemu_mallocz(8 * 256); /* XXX: make this persistent */
610 i2c_bus *smbus;
612 /* TODO: Populate SPD eeprom data. */
613 smbus = piix4_pm_init(pci_bus, piix3_devfn + 3, 0xb100, i8259[9]);
614 for (i = 0; i < 8; i++) {
615 smbus_eeprom_device_init(smbus, 0x50 + i, eeprom_buf + (i * 256));
619 if (i440fx_state) {
620 i440fx_init_memory_mappings(i440fx_state);
623 if (pci_enabled) {
624 int max_bus;
625 int bus, unit;
626 void *scsi;
628 max_bus = drive_get_max_bus(IF_SCSI);
630 for (bus = 0; bus <= max_bus; bus++) {
631 scsi = lsi_scsi_init(pci_bus, -1);
632 for (unit = 0; unit < LSI_MAX_DEVS; unit++) {
633 index = drive_get_index(IF_SCSI, bus, unit);
634 if (index == -1)
635 continue;
636 lsi_scsi_attach(scsi, drives_table[index].bdrv, unit);
640 /* Add virtio block devices */
641 if (pci_enabled) {
642 int index;
643 int unit_id = 0;
645 while ((index = drive_get_index(IF_VIRTIO, 0, unit_id)) != -1) {
646 virtio_blk_init(pci_bus, 0x1AF4, 0x1001,
647 drives_table[index].bdrv);
648 unit_id++;
653 static void ipf_init_pci(ram_addr_t ram_size, int vga_ram_size,
654 const char *boot_device, DisplayState *ds,
655 const char *kernel_filename,
656 const char *kernel_cmdline,
657 const char *initrd_filename,
658 const char *cpu_model)
660 ipf_init1(ram_size, vga_ram_size, boot_device, ds,
661 kernel_filename, kernel_cmdline,
662 initrd_filename, 1, cpu_model);
665 QEMUMachine ipf_machine = {
666 "itanium",
667 "Itanium Platform",
668 ipf_init_pci,
669 VGA_RAM_SIZE + GFW_SIZE,
672 #define IOAPIC_NUM_PINS 48
674 static int ioapic_irq_count[IOAPIC_NUM_PINS];
676 static int ioapic_map_irq(int devfn, int irq_num)
678 int irq, dev;
679 dev = devfn >> 3;
680 irq = ((((dev << 2) + (dev >> 3) + irq_num) & 31) + 16);
681 return irq;
684 void ioapic_set_irq(void *opaque, int irq_num, int level)
686 int vector;
688 PCIDevice *pci_dev = (PCIDevice *)opaque;
689 vector = ioapic_map_irq(pci_dev->devfn, irq_num);
691 if (level)
692 ioapic_irq_count[vector] += 1;
693 else
694 ioapic_irq_count[vector] -= 1;
696 if (kvm_enabled())
697 if (kvm_set_irq(vector, ioapic_irq_count[vector] == 0))
698 return;