2 * QEMU 8259 interrupt controller emulation
4 * Copyright (c) 2003-2004 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
28 #include "qemu-timer.h"
29 #include "i8259_internal.h"
32 #include "apic_internal.h"
34 static void kvm_i8259_set_irq(void *opaque
, int irq
, int level
);
40 #define DPRINTF(fmt, ...) \
41 do { printf("pic: " fmt , ## __VA_ARGS__); } while (0)
43 #define DPRINTF(fmt, ...)
46 //#define DEBUG_IRQ_LATENCY
47 //#define DEBUG_IRQ_COUNT
49 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
50 static int irq_level
[16];
52 #ifdef DEBUG_IRQ_COUNT
53 static uint64_t irq_count
[16];
55 #ifdef DEBUG_IRQ_LATENCY
56 static int64_t irq_time
[16];
59 static PICCommonState
*slave_pic
;
61 /* return the highest priority found in mask (highest = smallest
62 number). Return 8 if no irq */
63 static int get_priority(PICCommonState
*s
, int mask
)
71 while ((mask
& (1 << ((priority
+ s
->priority_add
) & 7))) == 0) {
77 /* return the pic wanted interrupt. return -1 if none */
78 static int pic_get_irq(PICCommonState
*s
)
80 int mask
, cur_priority
, priority
;
82 mask
= s
->irr
& ~s
->imr
;
83 priority
= get_priority(s
, mask
);
87 /* compute current priority. If special fully nested mode on the
88 master, the IRQ coming from the slave is not taken into account
89 for the priority computation. */
91 if (s
->special_mask
) {
94 if (s
->special_fully_nested_mode
&& s
->master
) {
97 cur_priority
= get_priority(s
, mask
);
98 if (priority
< cur_priority
) {
99 /* higher priority found: an irq should be generated */
100 return (priority
+ s
->priority_add
) & 7;
106 /* Update INT output. Must be called every time the output may have changed. */
107 static void pic_update_irq(PICCommonState
*s
)
111 irq
= pic_get_irq(s
);
113 DPRINTF("pic%d: imr=%x irr=%x padd=%d\n",
114 s
->master
? 0 : 1, s
->imr
, s
->irr
, s
->priority_add
);
115 qemu_irq_raise(s
->int_out
[0]);
117 qemu_irq_lower(s
->int_out
[0]);
121 /* set irq level. If an edge is detected, then the IRR is set to 1 */
122 static void pic_set_irq(void *opaque
, int irq
, int level
)
124 PICCommonState
*s
= opaque
;
127 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT) || \
128 defined(DEBUG_IRQ_LATENCY)
129 int irq_index
= s
->master
? irq
: irq
+ 8;
131 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
132 if (level
!= irq_level
[irq_index
]) {
133 DPRINTF("pic_set_irq: irq=%d level=%d\n", irq_index
, level
);
134 irq_level
[irq_index
] = level
;
135 #ifdef DEBUG_IRQ_COUNT
137 irq_count
[irq_index
]++;
142 #ifdef DEBUG_IRQ_LATENCY
144 irq_time
[irq_index
] = qemu_get_clock_ns(vm_clock
);
148 if (s
->elcr
& mask
) {
149 /* level triggered */
155 s
->last_irr
&= ~mask
;
160 if ((s
->last_irr
& mask
) == 0) {
165 s
->last_irr
&= ~mask
;
171 /* acknowledge interrupt 'irq' */
172 static void pic_intack(PICCommonState
*s
, int irq
)
175 if (s
->rotate_on_auto_eoi
) {
176 s
->priority_add
= (irq
+ 1) & 7;
179 s
->isr
|= (1 << irq
);
181 /* We don't clear a level sensitive interrupt here */
182 if (!(s
->elcr
& (1 << irq
))) {
183 s
->irr
&= ~(1 << irq
);
188 int pic_read_irq(DeviceState
*d
)
190 PICCommonState
*s
= DO_UPCAST(PICCommonState
, dev
.qdev
, d
);
191 int irq
, irq2
, intno
;
193 irq
= pic_get_irq(s
);
196 irq2
= pic_get_irq(slave_pic
);
198 pic_intack(slave_pic
, irq2
);
200 /* spurious IRQ on slave controller */
203 intno
= slave_pic
->irq_base
+ irq2
;
205 intno
= s
->irq_base
+ irq
;
209 /* spurious IRQ on host controller */
211 intno
= s
->irq_base
+ irq
;
214 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_LATENCY)
219 #ifdef DEBUG_IRQ_LATENCY
220 printf("IRQ%d latency=%0.3fus\n",
222 (double)(qemu_get_clock_ns(vm_clock
) -
223 irq_time
[irq
]) * 1000000.0 / get_ticks_per_sec());
225 DPRINTF("pic_interrupt: irq=%d\n", irq
);
229 static int kvm_kernel_pic_load_from_user(PICCommonState
*s
);
231 static void pic_init_reset(PICCommonState
*s
)
235 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
236 kvm_kernel_pic_load_from_user(s
);
243 static void pic_reset(DeviceState
*dev
)
245 PICCommonState
*s
= DO_UPCAST(PICCommonState
, dev
.qdev
, dev
);
251 static void pic_ioport_write(void *opaque
, target_phys_addr_t addr64
,
252 uint64_t val64
, unsigned size
)
254 PICCommonState
*s
= opaque
;
255 uint32_t addr
= addr64
;
256 uint32_t val
= val64
;
257 int priority
, cmd
, irq
;
259 DPRINTF("write: addr=0x%02x val=0x%02x\n", addr
, val
);
265 s
->single_mode
= val
& 2;
267 hw_error("level sensitive irq not supported");
269 } else if (val
& 0x08) {
274 s
->read_reg_select
= val
& 1;
277 s
->special_mask
= (val
>> 5) & 1;
284 s
->rotate_on_auto_eoi
= cmd
>> 2;
286 case 1: /* end of interrupt */
288 priority
= get_priority(s
, s
->isr
);
290 irq
= (priority
+ s
->priority_add
) & 7;
291 s
->isr
&= ~(1 << irq
);
293 s
->priority_add
= (irq
+ 1) & 7;
300 s
->isr
&= ~(1 << irq
);
304 s
->priority_add
= (val
+ 1) & 7;
309 s
->isr
&= ~(1 << irq
);
310 s
->priority_add
= (irq
+ 1) & 7;
319 switch (s
->init_state
) {
326 s
->irq_base
= val
& 0xf8;
327 s
->init_state
= s
->single_mode
? (s
->init4
? 3 : 0) : 2;
337 s
->special_fully_nested_mode
= (val
>> 4) & 1;
338 s
->auto_eoi
= (val
>> 1) & 1;
345 static uint64_t pic_ioport_read(void *opaque
, target_phys_addr_t addr
,
348 PICCommonState
*s
= opaque
;
352 ret
= pic_get_irq(s
);
362 if (s
->read_reg_select
) {
371 DPRINTF("read: addr=0x%02x val=0x%02x\n", addr
, ret
);
375 int pic_get_output(DeviceState
*d
)
377 PICCommonState
*s
= DO_UPCAST(PICCommonState
, dev
.qdev
, d
);
379 return (pic_get_irq(s
) >= 0);
382 static void elcr_ioport_write(void *opaque
, target_phys_addr_t addr
,
383 uint64_t val
, unsigned size
)
385 PICCommonState
*s
= opaque
;
386 s
->elcr
= val
& s
->elcr_mask
;
389 static uint64_t elcr_ioport_read(void *opaque
, target_phys_addr_t addr
,
392 PICCommonState
*s
= opaque
;
396 static void kvm_kernel_pic_save_to_user(PICCommonState
*s
);
398 static void kvm_pic_pre_save(PICCommonState
*s
)
400 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
401 kvm_kernel_pic_save_to_user(s
);
405 static void kvm_pic_post_load(PICCommonState
*s
)
407 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
408 kvm_kernel_pic_load_from_user(s
);
412 static const MemoryRegionOps pic_base_ioport_ops
= {
413 .read
= pic_ioport_read
,
414 .write
= pic_ioport_write
,
416 .min_access_size
= 1,
417 .max_access_size
= 1,
421 static const MemoryRegionOps pic_elcr_ioport_ops
= {
422 .read
= elcr_ioport_read
,
423 .write
= elcr_ioport_write
,
425 .min_access_size
= 1,
426 .max_access_size
= 1,
430 static void pic_init(PICCommonState
*s
)
432 memory_region_init_io(&s
->base_io
, &pic_base_ioport_ops
, s
, "pic", 2);
433 memory_region_init_io(&s
->elcr_io
, &pic_elcr_ioport_ops
, s
, "elcr", 1);
435 qdev_init_gpio_out(&s
->dev
.qdev
, s
->int_out
, ARRAY_SIZE(s
->int_out
));
436 qdev_init_gpio_in(&s
->dev
.qdev
, pic_set_irq
, 8);
439 void pic_info(Monitor
*mon
)
447 for (i
= 0; i
< 2; i
++) {
448 s
= i
== 0 ? DO_UPCAST(PICCommonState
, dev
.qdev
, isa_pic
) : slave_pic
;
449 monitor_printf(mon
, "pic%d: irr=%02x imr=%02x isr=%02x hprio=%d "
450 "irq_base=%02x rr_sel=%d elcr=%02x fnm=%d\n",
451 i
, s
->irr
, s
->imr
, s
->isr
, s
->priority_add
,
452 s
->irq_base
, s
->read_reg_select
, s
->elcr
,
453 s
->special_fully_nested_mode
);
457 void irq_info(Monitor
*mon
)
459 #ifndef DEBUG_IRQ_COUNT
460 monitor_printf(mon
, "irq statistic code not compiled.\n");
465 monitor_printf(mon
, "IRQ statistics:\n");
466 for (i
= 0; i
< 16; i
++) {
467 count
= irq_count
[i
];
469 monitor_printf(mon
, "%2d: %" PRId64
"\n", i
, count
);
475 qemu_irq
*i8259_init(ISABus
*bus
, qemu_irq parent_irq
)
481 irq_set
= g_malloc(ISA_NUM_IRQS
* sizeof(qemu_irq
));
483 dev
= i8259_init_chip("isa-i8259", bus
, true);
485 qdev_connect_gpio_out(&dev
->qdev
, 0, parent_irq
);
486 for (i
= 0 ; i
< 8; i
++) {
487 irq_set
[i
] = qdev_get_gpio_in(&dev
->qdev
, i
);
490 isa_pic
= &dev
->qdev
;
492 dev
= i8259_init_chip("isa-i8259", bus
, false);
494 qdev_connect_gpio_out(&dev
->qdev
, 0, irq_set
[2]);
495 for (i
= 0 ; i
< 8; i
++) {
496 irq_set
[i
+ 8] = qdev_get_gpio_in(&dev
->qdev
, i
);
499 slave_pic
= DO_UPCAST(PICCommonState
, dev
, dev
);
501 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
502 irq_set
= qemu_allocate_irqs(kvm_i8259_set_irq
, NULL
, 24);
508 static void i8259_class_init(ObjectClass
*klass
, void *data
)
510 PICCommonClass
*k
= PIC_COMMON_CLASS(klass
);
513 k
->pre_save
= kvm_pic_pre_save
;
514 k
->post_load
= kvm_pic_post_load
;
517 static DeviceInfo i8259_info
= {
520 .class_init
= i8259_class_init
,
523 static void pic_register(void)
525 pic_qdev_register(&i8259_info
);
528 static void kvm_kernel_pic_save_to_user(PICCommonState
*s
)
530 #ifdef KVM_CAP_IRQCHIP
531 struct kvm_irqchip chip
;
532 struct kvm_pic_state
*kpic
;
534 chip
.chip_id
= s
->master
?
535 KVM_IRQCHIP_PIC_MASTER
:
536 KVM_IRQCHIP_PIC_SLAVE
;
537 kvm_get_irqchip(kvm_state
, &chip
);
538 kpic
= &chip
.chip
.pic
;
540 s
->last_irr
= kpic
->last_irr
;
544 s
->priority_add
= kpic
->priority_add
;
545 s
->irq_base
= kpic
->irq_base
;
546 s
->read_reg_select
= kpic
->read_reg_select
;
547 s
->poll
= kpic
->poll
;
548 s
->special_mask
= kpic
->special_mask
;
549 s
->init_state
= kpic
->init_state
;
550 s
->auto_eoi
= kpic
->auto_eoi
;
551 s
->rotate_on_auto_eoi
= kpic
->rotate_on_auto_eoi
;
552 s
->special_fully_nested_mode
= kpic
->special_fully_nested_mode
;
553 s
->init4
= kpic
->init4
;
554 s
->elcr
= kpic
->elcr
;
555 s
->elcr_mask
= kpic
->elcr_mask
;
559 static int kvm_kernel_pic_load_from_user(PICCommonState
*s
)
561 #ifdef KVM_CAP_IRQCHIP
562 struct kvm_irqchip chip
;
563 struct kvm_pic_state
*kpic
;
565 chip
.chip_id
= s
->master
?
566 KVM_IRQCHIP_PIC_MASTER
:
567 KVM_IRQCHIP_PIC_SLAVE
;
568 kpic
= &chip
.chip
.pic
;
570 kpic
->last_irr
= s
->last_irr
;
574 kpic
->priority_add
= s
->priority_add
;
575 kpic
->irq_base
= s
->irq_base
;
576 kpic
->read_reg_select
= s
->read_reg_select
;
577 kpic
->poll
= s
->poll
;
578 kpic
->special_mask
= s
->special_mask
;
579 kpic
->init_state
= s
->init_state
;
580 kpic
->auto_eoi
= s
->auto_eoi
;
581 kpic
->rotate_on_auto_eoi
= s
->rotate_on_auto_eoi
;
582 kpic
->special_fully_nested_mode
= s
->special_fully_nested_mode
;
583 kpic
->init4
= s
->init4
;
584 kpic
->elcr
= s
->elcr
;
585 kpic
->elcr_mask
= s
->elcr_mask
;
587 kvm_set_irqchip(kvm_state
, &chip
);
592 static void kvm_i8259_set_irq(void *opaque
, int irq
, int level
)
595 if (kvm_set_irq(irq
, level
, &pic_ret
)) {
596 apic_report_irq_delivered(pic_ret
);
601 device_init(pic_register
)