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
29 #include "qemu-timer.h"
37 #define DPRINTF(fmt, ...) \
38 do { printf("pic: " fmt , ## __VA_ARGS__); } while (0)
40 #define DPRINTF(fmt, ...)
43 //#define DEBUG_IRQ_LATENCY
44 //#define DEBUG_IRQ_COUNT
46 typedef struct PicState
{
47 uint8_t last_irr
; /* edge detection */
48 uint8_t irr
; /* interrupt request register */
49 uint8_t imr
; /* interrupt mask register */
50 uint8_t isr
; /* interrupt service register */
51 uint8_t priority_add
; /* highest irq priority */
53 uint8_t read_reg_select
;
58 uint8_t rotate_on_auto_eoi
;
59 uint8_t special_fully_nested_mode
;
60 uint8_t init4
; /* true if 4 byte init */
61 uint8_t single_mode
; /* true if slave pic is not initialized */
62 uint8_t elcr
; /* PIIX edge/trigger selection*/
64 PicState2
*pics_state
;
68 /* 0 is master pic, 1 is slave pic */
69 /* XXX: better separation between the two pics */
72 void *irq_request_opaque
;
75 #if defined(DEBUG_PIC) || defined (DEBUG_IRQ_COUNT)
76 static int irq_level
[16];
78 #ifdef DEBUG_IRQ_COUNT
79 static uint64_t irq_count
[16];
83 /* set irq level. If an edge is detected, then the IRR is set to 1 */
84 static inline void pic_set_irq1(PicState
*s
, int irq
, int level
)
100 if ((s
->last_irr
& mask
) == 0)
104 s
->last_irr
&= ~mask
;
109 /* return the highest priority found in mask (highest = smallest
110 number). Return 8 if no irq */
111 static inline int get_priority(PicState
*s
, int mask
)
117 while ((mask
& (1 << ((priority
+ s
->priority_add
) & 7))) == 0)
122 /* return the pic wanted interrupt. return -1 if none */
123 static int pic_get_irq(PicState
*s
)
125 int mask
, cur_priority
, priority
;
127 mask
= s
->irr
& ~s
->imr
;
128 priority
= get_priority(s
, mask
);
131 /* compute current priority. If special fully nested mode on the
132 master, the IRQ coming from the slave is not taken into account
133 for the priority computation. */
137 if (s
->special_fully_nested_mode
&& s
== &s
->pics_state
->pics
[0])
139 cur_priority
= get_priority(s
, mask
);
140 if (priority
< cur_priority
) {
141 /* higher priority found: an irq should be generated */
142 return (priority
+ s
->priority_add
) & 7;
148 /* raise irq to CPU if necessary. must be called every time the active
150 /* XXX: should not export it, but it is needed for an APIC kludge */
151 void pic_update_irq(PicState2
*s
)
155 /* first look at slave pic */
156 irq2
= pic_get_irq(&s
->pics
[1]);
158 /* if irq request by slave pic, signal master PIC */
159 pic_set_irq1(&s
->pics
[0], 2, 1);
160 pic_set_irq1(&s
->pics
[0], 2, 0);
162 /* look at requested irq */
163 irq
= pic_get_irq(&s
->pics
[0]);
165 #if defined(DEBUG_PIC)
168 for(i
= 0; i
< 2; i
++) {
169 printf("pic%d: imr=%x irr=%x padd=%d\n",
170 i
, s
->pics
[i
].imr
, s
->pics
[i
].irr
,
171 s
->pics
[i
].priority_add
);
175 printf("pic: cpu_interrupt\n");
177 qemu_irq_raise(s
->parent_irq
);
180 /* all targets should do this rather than acking the IRQ in the cpu */
181 #if defined(TARGET_MIPS) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
183 qemu_irq_lower(s
->parent_irq
);
188 #ifdef DEBUG_IRQ_LATENCY
189 int64_t irq_time
[16];
192 static void i8259_set_irq(void *opaque
, int irq
, int level
)
194 PicState2
*s
= opaque
;
195 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
196 if (level
!= irq_level
[irq
]) {
197 DPRINTF("i8259_set_irq: irq=%d level=%d\n", irq
, level
);
198 irq_level
[irq
] = level
;
199 #ifdef DEBUG_IRQ_COUNT
205 #ifdef DEBUG_IRQ_LATENCY
207 irq_time
[irq
] = qemu_get_clock(vm_clock
);
210 pic_set_irq1(&s
->pics
[irq
>> 3], irq
& 7, level
);
214 /* acknowledge interrupt 'irq' */
215 static inline void pic_intack(PicState
*s
, int irq
)
218 if (s
->rotate_on_auto_eoi
)
219 s
->priority_add
= (irq
+ 1) & 7;
221 s
->isr
|= (1 << irq
);
224 /* We don't clear a level sensitive interrupt here */
225 if (!(s
->elcr
& (1 << irq
)))
226 s
->irr
&= ~(1 << irq
);
230 extern int time_drift_fix
;
231 extern int64_t timer_acks
, timer_ints_to_push
;
233 int pic_read_irq(PicState2
*s
)
235 int irq
, irq2
, intno
;
237 irq
= pic_get_irq(&s
->pics
[0]);
240 pic_intack(&s
->pics
[0], irq
);
242 if (time_drift_fix
&& irq
== 0) {
244 if (timer_ints_to_push
> 0) {
245 timer_ints_to_push
--;
246 /* simulate an edge irq0, like the one generated by i8254 */
247 pic_set_irq1(&s
->pics
[0], 0, 0);
248 pic_set_irq1(&s
->pics
[0], 0, 1);
253 irq2
= pic_get_irq(&s
->pics
[1]);
255 pic_intack(&s
->pics
[1], irq2
);
257 /* spurious IRQ on slave controller */
260 intno
= s
->pics
[1].irq_base
+ irq2
;
261 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_LATENCY)
265 intno
= s
->pics
[0].irq_base
+ irq
;
268 /* spurious IRQ on host controller */
270 intno
= s
->pics
[0].irq_base
+ irq
;
274 #ifdef DEBUG_IRQ_LATENCY
275 printf("IRQ%d latency=%0.3fus\n",
277 (double)(qemu_get_clock(vm_clock
) -
278 irq_time
[irq
]) * 1000000.0 / get_ticks_per_sec());
280 DPRINTF("pic_interrupt: irq=%d\n", irq
);
284 static void pic_reset(void *opaque
)
286 PicState
*s
= opaque
;
294 s
->read_reg_select
= 0;
299 s
->rotate_on_auto_eoi
= 0;
300 s
->special_fully_nested_mode
= 0;
303 /* Note: ELCR is not reset */
306 static void pic_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
308 PicState
*s
= opaque
;
309 int priority
, cmd
, irq
;
311 DPRINTF("write: addr=0x%02x val=0x%02x\n", addr
, val
);
317 /* deassert a pending interrupt */
318 qemu_irq_lower(s
->pics_state
->parent_irq
);
321 s
->single_mode
= val
& 2;
323 hw_error("level sensitive irq not supported");
324 } else if (val
& 0x08) {
328 s
->read_reg_select
= val
& 1;
330 s
->special_mask
= (val
>> 5) & 1;
336 s
->rotate_on_auto_eoi
= cmd
>> 2;
338 case 1: /* end of interrupt */
340 priority
= get_priority(s
, s
->isr
);
342 irq
= (priority
+ s
->priority_add
) & 7;
343 s
->isr
&= ~(1 << irq
);
345 s
->priority_add
= (irq
+ 1) & 7;
346 pic_update_irq(s
->pics_state
);
351 s
->isr
&= ~(1 << irq
);
352 pic_update_irq(s
->pics_state
);
355 s
->priority_add
= (val
+ 1) & 7;
356 pic_update_irq(s
->pics_state
);
360 s
->isr
&= ~(1 << irq
);
361 s
->priority_add
= (irq
+ 1) & 7;
362 pic_update_irq(s
->pics_state
);
370 switch(s
->init_state
) {
374 pic_update_irq(s
->pics_state
);
377 s
->irq_base
= val
& 0xf8;
378 s
->init_state
= s
->single_mode
? (s
->init4
? 3 : 0) : 2;
388 s
->special_fully_nested_mode
= (val
>> 4) & 1;
389 s
->auto_eoi
= (val
>> 1) & 1;
396 static uint32_t pic_poll_read (PicState
*s
, uint32_t addr1
)
400 ret
= pic_get_irq(s
);
403 s
->pics_state
->pics
[0].isr
&= ~(1 << 2);
404 s
->pics_state
->pics
[0].irr
&= ~(1 << 2);
406 s
->irr
&= ~(1 << ret
);
407 s
->isr
&= ~(1 << ret
);
408 if (addr1
>> 7 || ret
!= 2)
409 pic_update_irq(s
->pics_state
);
412 pic_update_irq(s
->pics_state
);
418 static uint32_t pic_ioport_read(void *opaque
, uint32_t addr1
)
420 PicState
*s
= opaque
;
427 ret
= pic_poll_read(s
, addr1
);
431 if (s
->read_reg_select
)
439 DPRINTF("read: addr=0x%02x val=0x%02x\n", addr1
, ret
);
443 /* memory mapped interrupt status */
444 /* XXX: may be the same than pic_read_irq() */
445 uint32_t pic_intack_read(PicState2
*s
)
449 ret
= pic_poll_read(&s
->pics
[0], 0x00);
451 ret
= pic_poll_read(&s
->pics
[1], 0x80) + 8;
452 /* Prepare for ISR read */
453 s
->pics
[0].read_reg_select
= 1;
458 static void elcr_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
460 PicState
*s
= opaque
;
461 s
->elcr
= val
& s
->elcr_mask
;
464 static uint32_t elcr_ioport_read(void *opaque
, uint32_t addr1
)
466 PicState
*s
= opaque
;
470 static void kvm_kernel_pic_save_to_user(PicState
*s
);
471 static int kvm_kernel_pic_load_from_user(PicState
*s
);
473 static void pic_pre_save(void *opaque
)
475 PicState
*s
= opaque
;
477 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
478 kvm_kernel_pic_save_to_user(s
);
482 static int pic_post_load(void *opaque
, int version_id
)
484 PicState
*s
= opaque
;
486 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
487 kvm_kernel_pic_load_from_user(s
);
492 static const VMStateDescription vmstate_pic
= {
495 .pre_save
= pic_pre_save
,
496 .post_load
= pic_post_load
,
497 .minimum_version_id
= 1,
498 .minimum_version_id_old
= 1,
499 .fields
= (VMStateField
[]) {
500 VMSTATE_UINT8(last_irr
, PicState
),
501 VMSTATE_UINT8(irr
, PicState
),
502 VMSTATE_UINT8(imr
, PicState
),
503 VMSTATE_UINT8(isr
, PicState
),
504 VMSTATE_UINT8(priority_add
, PicState
),
505 VMSTATE_UINT8(irq_base
, PicState
),
506 VMSTATE_UINT8(read_reg_select
, PicState
),
507 VMSTATE_UINT8(poll
, PicState
),
508 VMSTATE_UINT8(special_mask
, PicState
),
509 VMSTATE_UINT8(init_state
, PicState
),
510 VMSTATE_UINT8(auto_eoi
, PicState
),
511 VMSTATE_UINT8(rotate_on_auto_eoi
, PicState
),
512 VMSTATE_UINT8(special_fully_nested_mode
, PicState
),
513 VMSTATE_UINT8(init4
, PicState
),
514 VMSTATE_UINT8(single_mode
, PicState
),
515 VMSTATE_UINT8(elcr
, PicState
),
516 VMSTATE_END_OF_LIST()
520 /* XXX: add generic master/slave system */
521 static void pic_init1(int io_addr
, int elcr_addr
, PicState
*s
)
523 register_ioport_write(io_addr
, 2, 1, pic_ioport_write
, s
);
524 register_ioport_read(io_addr
, 2, 1, pic_ioport_read
, s
);
525 if (elcr_addr
>= 0) {
526 register_ioport_write(elcr_addr
, 1, 1, elcr_ioport_write
, s
);
527 register_ioport_read(elcr_addr
, 1, 1, elcr_ioport_read
, s
);
529 vmstate_register(NULL
, io_addr
, &vmstate_pic
, s
);
530 qemu_register_reset(pic_reset
, s
);
533 void pic_info(Monitor
*mon
)
542 s
= &isa_pic
->pics
[i
];
543 monitor_printf(mon
, "pic%d: irr=%02x imr=%02x isr=%02x hprio=%d "
544 "irq_base=%02x rr_sel=%d elcr=%02x fnm=%d\n",
545 i
, s
->irr
, s
->imr
, s
->isr
, s
->priority_add
,
546 s
->irq_base
, s
->read_reg_select
, s
->elcr
,
547 s
->special_fully_nested_mode
);
551 void irq_info(Monitor
*mon
)
553 #ifndef DEBUG_IRQ_COUNT
554 monitor_printf(mon
, "irq statistic code not compiled.\n");
559 monitor_printf(mon
, "IRQ statistics:\n");
560 for (i
= 0; i
< 16; i
++) {
561 count
= irq_count
[i
];
563 monitor_printf(mon
, "%2d: %" PRId64
"\n", i
, count
);
568 qemu_irq
*i8259_init(qemu_irq parent_irq
)
572 s
= qemu_mallocz(sizeof(PicState2
));
573 pic_init1(0x20, 0x4d0, &s
->pics
[0]);
574 pic_init1(0xa0, 0x4d1, &s
->pics
[1]);
575 s
->pics
[0].elcr_mask
= 0xf8;
576 s
->pics
[1].elcr_mask
= 0xde;
577 s
->parent_irq
= parent_irq
;
578 s
->pics
[0].pics_state
= s
;
579 s
->pics
[1].pics_state
= s
;
581 return qemu_allocate_irqs(i8259_set_irq
, s
, 16);
584 static void kvm_kernel_pic_save_to_user(PicState
*s
)
586 #ifdef KVM_CAP_IRQCHIP
587 struct kvm_irqchip chip
;
588 struct kvm_pic_state
*kpic
;
590 chip
.chip_id
= (&s
->pics_state
->pics
[0] == s
) ?
591 KVM_IRQCHIP_PIC_MASTER
:
592 KVM_IRQCHIP_PIC_SLAVE
;
593 kvm_get_irqchip(kvm_context
, &chip
);
594 kpic
= &chip
.chip
.pic
;
596 s
->last_irr
= kpic
->last_irr
;
600 s
->priority_add
= kpic
->priority_add
;
601 s
->irq_base
= kpic
->irq_base
;
602 s
->read_reg_select
= kpic
->read_reg_select
;
603 s
->poll
= kpic
->poll
;
604 s
->special_mask
= kpic
->special_mask
;
605 s
->init_state
= kpic
->init_state
;
606 s
->auto_eoi
= kpic
->auto_eoi
;
607 s
->rotate_on_auto_eoi
= kpic
->rotate_on_auto_eoi
;
608 s
->special_fully_nested_mode
= kpic
->special_fully_nested_mode
;
609 s
->init4
= kpic
->init4
;
610 s
->elcr
= kpic
->elcr
;
611 s
->elcr_mask
= kpic
->elcr_mask
;
615 static int kvm_kernel_pic_load_from_user(PicState
*s
)
617 #ifdef KVM_CAP_IRQCHIP
618 struct kvm_irqchip chip
;
619 struct kvm_pic_state
*kpic
;
621 chip
.chip_id
= (&s
->pics_state
->pics
[0] == s
) ?
622 KVM_IRQCHIP_PIC_MASTER
:
623 KVM_IRQCHIP_PIC_SLAVE
;
624 kpic
= &chip
.chip
.pic
;
626 kpic
->last_irr
= s
->last_irr
;
630 kpic
->priority_add
= s
->priority_add
;
631 kpic
->irq_base
= s
->irq_base
;
632 kpic
->read_reg_select
= s
->read_reg_select
;
633 kpic
->poll
= s
->poll
;
634 kpic
->special_mask
= s
->special_mask
;
635 kpic
->init_state
= s
->init_state
;
636 kpic
->auto_eoi
= s
->auto_eoi
;
637 kpic
->rotate_on_auto_eoi
= s
->rotate_on_auto_eoi
;
638 kpic
->special_fully_nested_mode
= s
->special_fully_nested_mode
;
639 kpic
->init4
= s
->init4
;
640 kpic
->elcr
= s
->elcr
;
641 kpic
->elcr_mask
= s
->elcr_mask
;
643 kvm_set_irqchip(kvm_context
, &chip
);
648 #ifdef KVM_CAP_IRQCHIP
649 static void kvm_i8259_set_irq(void *opaque
, int irq
, int level
)
652 if (kvm_set_irq(irq
, level
, &pic_ret
)) {
654 apic_set_irq_delivered();
659 static void kvm_pic_init1(int io_addr
, PicState
*s
)
661 vmstate_register(NULL
, io_addr
, &vmstate_pic
, s
);
662 qemu_register_reset(pic_reset
, s
);
665 qemu_irq
*kvm_i8259_init(qemu_irq parent_irq
)
669 s
= qemu_mallocz(sizeof(PicState2
));
671 kvm_pic_init1(0x20, &s
->pics
[0]);
672 kvm_pic_init1(0xa0, &s
->pics
[1]);
673 s
->parent_irq
= parent_irq
;
674 s
->pics
[0].pics_state
= s
;
675 s
->pics
[1].pics_state
= s
;
677 return qemu_allocate_irqs(kvm_i8259_set_irq
, s
, 24);