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"
36 #define DPRINTF(fmt, ...) \
37 do { printf("pic: " fmt , ## __VA_ARGS__); } while (0)
39 #define DPRINTF(fmt, ...)
42 //#define DEBUG_IRQ_LATENCY
43 //#define DEBUG_IRQ_COUNT
45 typedef struct PicState
{
46 uint8_t last_irr
; /* edge detection */
47 uint8_t irr
; /* interrupt request register */
48 uint8_t imr
; /* interrupt mask register */
49 uint8_t isr
; /* interrupt service register */
50 uint8_t priority_add
; /* highest irq priority */
52 uint8_t read_reg_select
;
57 uint8_t rotate_on_auto_eoi
;
58 uint8_t special_fully_nested_mode
;
59 uint8_t init4
; /* true if 4 byte init */
60 uint8_t single_mode
; /* true if slave pic is not initialized */
61 uint8_t elcr
; /* PIIX edge/trigger selection*/
63 PicState2
*pics_state
;
67 /* 0 is master pic, 1 is slave pic */
68 /* XXX: better separation between the two pics */
71 void *irq_request_opaque
;
74 #if defined(DEBUG_PIC) || defined (DEBUG_IRQ_COUNT)
75 static int irq_level
[16];
77 #ifdef DEBUG_IRQ_COUNT
78 static uint64_t irq_count
[16];
82 /* set irq level. If an edge is detected, then the IRR is set to 1 */
83 static inline void pic_set_irq1(PicState
*s
, int irq
, int level
)
99 if ((s
->last_irr
& mask
) == 0)
103 s
->last_irr
&= ~mask
;
108 /* return the highest priority found in mask (highest = smallest
109 number). Return 8 if no irq */
110 static inline int get_priority(PicState
*s
, int mask
)
116 while ((mask
& (1 << ((priority
+ s
->priority_add
) & 7))) == 0)
121 /* return the pic wanted interrupt. return -1 if none */
122 static int pic_get_irq(PicState
*s
)
124 int mask
, cur_priority
, priority
;
126 mask
= s
->irr
& ~s
->imr
;
127 priority
= get_priority(s
, mask
);
130 /* compute current priority. If special fully nested mode on the
131 master, the IRQ coming from the slave is not taken into account
132 for the priority computation. */
136 if (s
->special_fully_nested_mode
&& s
== &s
->pics_state
->pics
[0])
138 cur_priority
= get_priority(s
, mask
);
139 if (priority
< cur_priority
) {
140 /* higher priority found: an irq should be generated */
141 return (priority
+ s
->priority_add
) & 7;
147 /* raise irq to CPU if necessary. must be called every time the active
149 /* XXX: should not export it, but it is needed for an APIC kludge */
150 void pic_update_irq(PicState2
*s
)
154 /* first look at slave pic */
155 irq2
= pic_get_irq(&s
->pics
[1]);
157 /* if irq request by slave pic, signal master PIC */
158 pic_set_irq1(&s
->pics
[0], 2, 1);
159 pic_set_irq1(&s
->pics
[0], 2, 0);
161 /* look at requested irq */
162 irq
= pic_get_irq(&s
->pics
[0]);
164 #if defined(DEBUG_PIC)
167 for(i
= 0; i
< 2; i
++) {
168 printf("pic%d: imr=%x irr=%x padd=%d\n",
169 i
, s
->pics
[i
].imr
, s
->pics
[i
].irr
,
170 s
->pics
[i
].priority_add
);
174 printf("pic: cpu_interrupt\n");
176 qemu_irq_raise(s
->parent_irq
);
179 /* all targets should do this rather than acking the IRQ in the cpu */
180 #if defined(TARGET_MIPS) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
182 qemu_irq_lower(s
->parent_irq
);
187 #ifdef DEBUG_IRQ_LATENCY
188 int64_t irq_time
[16];
191 static void i8259_set_irq(void *opaque
, int irq
, int level
)
193 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_ns(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
);
223 /* We don't clear a level sensitive interrupt here */
224 if (!(s
->elcr
& (1 << irq
)))
225 s
->irr
&= ~(1 << irq
);
228 extern int time_drift_fix
;
229 extern int64_t timer_acks
, timer_ints_to_push
;
231 int pic_read_irq(PicState2
*s
)
233 int irq
, irq2
, intno
;
235 irq
= pic_get_irq(&s
->pics
[0]);
237 pic_intack(&s
->pics
[0], irq
);
239 if (time_drift_fix
&& irq
== 0) {
241 if (timer_ints_to_push
> 0) {
242 timer_ints_to_push
--;
243 /* simulate an edge irq0, like the one generated by i8254 */
244 pic_set_irq1(&s
->pics
[0], 0, 0);
245 pic_set_irq1(&s
->pics
[0], 0, 1);
250 irq2
= pic_get_irq(&s
->pics
[1]);
252 pic_intack(&s
->pics
[1], irq2
);
254 /* spurious IRQ on slave controller */
257 intno
= s
->pics
[1].irq_base
+ irq2
;
258 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_LATENCY)
262 intno
= s
->pics
[0].irq_base
+ irq
;
265 /* spurious IRQ on host controller */
267 intno
= s
->pics
[0].irq_base
+ irq
;
271 #ifdef DEBUG_IRQ_LATENCY
272 printf("IRQ%d latency=%0.3fus\n",
274 (double)(qemu_get_clock_ns(vm_clock
) -
275 irq_time
[irq
]) * 1000000.0 / get_ticks_per_sec());
277 DPRINTF("pic_interrupt: irq=%d\n", irq
);
281 static void pic_reset(void *opaque
)
283 PicState
*s
= opaque
;
291 s
->read_reg_select
= 0;
296 s
->rotate_on_auto_eoi
= 0;
297 s
->special_fully_nested_mode
= 0;
300 /* Note: ELCR is not reset */
303 static void pic_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
305 PicState
*s
= opaque
;
306 int priority
, cmd
, irq
;
308 DPRINTF("write: addr=0x%02x val=0x%02x\n", addr
, val
);
314 /* deassert a pending interrupt */
315 qemu_irq_lower(s
->pics_state
->parent_irq
);
318 s
->single_mode
= val
& 2;
320 hw_error("level sensitive irq not supported");
321 } else if (val
& 0x08) {
325 s
->read_reg_select
= val
& 1;
327 s
->special_mask
= (val
>> 5) & 1;
333 s
->rotate_on_auto_eoi
= cmd
>> 2;
335 case 1: /* end of interrupt */
337 priority
= get_priority(s
, s
->isr
);
339 irq
= (priority
+ s
->priority_add
) & 7;
340 s
->isr
&= ~(1 << irq
);
342 s
->priority_add
= (irq
+ 1) & 7;
343 pic_update_irq(s
->pics_state
);
348 s
->isr
&= ~(1 << irq
);
349 pic_update_irq(s
->pics_state
);
352 s
->priority_add
= (val
+ 1) & 7;
353 pic_update_irq(s
->pics_state
);
357 s
->isr
&= ~(1 << irq
);
358 s
->priority_add
= (irq
+ 1) & 7;
359 pic_update_irq(s
->pics_state
);
367 switch(s
->init_state
) {
371 pic_update_irq(s
->pics_state
);
374 s
->irq_base
= val
& 0xf8;
375 s
->init_state
= s
->single_mode
? (s
->init4
? 3 : 0) : 2;
385 s
->special_fully_nested_mode
= (val
>> 4) & 1;
386 s
->auto_eoi
= (val
>> 1) & 1;
393 static uint32_t pic_poll_read (PicState
*s
, uint32_t addr1
)
397 ret
= pic_get_irq(s
);
400 s
->pics_state
->pics
[0].isr
&= ~(1 << 2);
401 s
->pics_state
->pics
[0].irr
&= ~(1 << 2);
403 s
->irr
&= ~(1 << ret
);
404 s
->isr
&= ~(1 << ret
);
405 if (addr1
>> 7 || ret
!= 2)
406 pic_update_irq(s
->pics_state
);
409 pic_update_irq(s
->pics_state
);
415 static uint32_t pic_ioport_read(void *opaque
, uint32_t addr1
)
417 PicState
*s
= opaque
;
424 ret
= pic_poll_read(s
, addr1
);
428 if (s
->read_reg_select
)
436 DPRINTF("read: addr=0x%02x val=0x%02x\n", addr1
, ret
);
440 /* memory mapped interrupt status */
441 /* XXX: may be the same than pic_read_irq() */
442 uint32_t pic_intack_read(PicState2
*s
)
446 ret
= pic_poll_read(&s
->pics
[0], 0x00);
448 ret
= pic_poll_read(&s
->pics
[1], 0x80) + 8;
449 /* Prepare for ISR read */
450 s
->pics
[0].read_reg_select
= 1;
455 static void elcr_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
457 PicState
*s
= opaque
;
458 s
->elcr
= val
& s
->elcr_mask
;
461 static uint32_t elcr_ioport_read(void *opaque
, uint32_t addr1
)
463 PicState
*s
= opaque
;
467 static void kvm_kernel_pic_save_to_user(PicState
*s
);
468 static int kvm_kernel_pic_load_from_user(PicState
*s
);
470 static void pic_pre_save(void *opaque
)
472 PicState
*s
= opaque
;
474 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
475 kvm_kernel_pic_save_to_user(s
);
479 static int pic_post_load(void *opaque
, int version_id
)
481 PicState
*s
= opaque
;
483 if (kvm_enabled() && kvm_irqchip_in_kernel()) {
484 kvm_kernel_pic_load_from_user(s
);
489 static const VMStateDescription vmstate_pic
= {
492 .pre_save
= pic_pre_save
,
493 .post_load
= pic_post_load
,
494 .minimum_version_id
= 1,
495 .minimum_version_id_old
= 1,
496 .fields
= (VMStateField
[]) {
497 VMSTATE_UINT8(last_irr
, PicState
),
498 VMSTATE_UINT8(irr
, PicState
),
499 VMSTATE_UINT8(imr
, PicState
),
500 VMSTATE_UINT8(isr
, PicState
),
501 VMSTATE_UINT8(priority_add
, PicState
),
502 VMSTATE_UINT8(irq_base
, PicState
),
503 VMSTATE_UINT8(read_reg_select
, PicState
),
504 VMSTATE_UINT8(poll
, PicState
),
505 VMSTATE_UINT8(special_mask
, PicState
),
506 VMSTATE_UINT8(init_state
, PicState
),
507 VMSTATE_UINT8(auto_eoi
, PicState
),
508 VMSTATE_UINT8(rotate_on_auto_eoi
, PicState
),
509 VMSTATE_UINT8(special_fully_nested_mode
, PicState
),
510 VMSTATE_UINT8(init4
, PicState
),
511 VMSTATE_UINT8(single_mode
, PicState
),
512 VMSTATE_UINT8(elcr
, PicState
),
513 VMSTATE_END_OF_LIST()
517 /* XXX: add generic master/slave system */
518 static void pic_init1(int io_addr
, int elcr_addr
, PicState
*s
)
520 register_ioport_write(io_addr
, 2, 1, pic_ioport_write
, s
);
521 register_ioport_read(io_addr
, 2, 1, pic_ioport_read
, s
);
522 if (elcr_addr
>= 0) {
523 register_ioport_write(elcr_addr
, 1, 1, elcr_ioport_write
, s
);
524 register_ioport_read(elcr_addr
, 1, 1, elcr_ioport_read
, s
);
526 vmstate_register(NULL
, io_addr
, &vmstate_pic
, s
);
527 qemu_register_reset(pic_reset
, s
);
530 void pic_info(Monitor
*mon
)
539 s
= &isa_pic
->pics
[i
];
540 monitor_printf(mon
, "pic%d: irr=%02x imr=%02x isr=%02x hprio=%d "
541 "irq_base=%02x rr_sel=%d elcr=%02x fnm=%d\n",
542 i
, s
->irr
, s
->imr
, s
->isr
, s
->priority_add
,
543 s
->irq_base
, s
->read_reg_select
, s
->elcr
,
544 s
->special_fully_nested_mode
);
548 void irq_info(Monitor
*mon
)
550 #ifndef DEBUG_IRQ_COUNT
551 monitor_printf(mon
, "irq statistic code not compiled.\n");
556 monitor_printf(mon
, "IRQ statistics:\n");
557 for (i
= 0; i
< 16; i
++) {
558 count
= irq_count
[i
];
560 monitor_printf(mon
, "%2d: %" PRId64
"\n", i
, count
);
565 qemu_irq
*i8259_init(qemu_irq parent_irq
)
569 s
= qemu_mallocz(sizeof(PicState2
));
570 pic_init1(0x20, 0x4d0, &s
->pics
[0]);
571 pic_init1(0xa0, 0x4d1, &s
->pics
[1]);
572 s
->pics
[0].elcr_mask
= 0xf8;
573 s
->pics
[1].elcr_mask
= 0xde;
574 s
->parent_irq
= parent_irq
;
575 s
->pics
[0].pics_state
= s
;
576 s
->pics
[1].pics_state
= s
;
578 return qemu_allocate_irqs(i8259_set_irq
, s
, 16);
581 static void kvm_kernel_pic_save_to_user(PicState
*s
)
583 #ifdef KVM_CAP_IRQCHIP
584 struct kvm_irqchip chip
;
585 struct kvm_pic_state
*kpic
;
587 chip
.chip_id
= (&s
->pics_state
->pics
[0] == s
) ?
588 KVM_IRQCHIP_PIC_MASTER
:
589 KVM_IRQCHIP_PIC_SLAVE
;
590 kvm_get_irqchip(kvm_state
, &chip
);
591 kpic
= &chip
.chip
.pic
;
593 s
->last_irr
= kpic
->last_irr
;
597 s
->priority_add
= kpic
->priority_add
;
598 s
->irq_base
= kpic
->irq_base
;
599 s
->read_reg_select
= kpic
->read_reg_select
;
600 s
->poll
= kpic
->poll
;
601 s
->special_mask
= kpic
->special_mask
;
602 s
->init_state
= kpic
->init_state
;
603 s
->auto_eoi
= kpic
->auto_eoi
;
604 s
->rotate_on_auto_eoi
= kpic
->rotate_on_auto_eoi
;
605 s
->special_fully_nested_mode
= kpic
->special_fully_nested_mode
;
606 s
->init4
= kpic
->init4
;
607 s
->elcr
= kpic
->elcr
;
608 s
->elcr_mask
= kpic
->elcr_mask
;
612 static int kvm_kernel_pic_load_from_user(PicState
*s
)
614 #ifdef KVM_CAP_IRQCHIP
615 struct kvm_irqchip chip
;
616 struct kvm_pic_state
*kpic
;
618 chip
.chip_id
= (&s
->pics_state
->pics
[0] == s
) ?
619 KVM_IRQCHIP_PIC_MASTER
:
620 KVM_IRQCHIP_PIC_SLAVE
;
621 kpic
= &chip
.chip
.pic
;
623 kpic
->last_irr
= s
->last_irr
;
627 kpic
->priority_add
= s
->priority_add
;
628 kpic
->irq_base
= s
->irq_base
;
629 kpic
->read_reg_select
= s
->read_reg_select
;
630 kpic
->poll
= s
->poll
;
631 kpic
->special_mask
= s
->special_mask
;
632 kpic
->init_state
= s
->init_state
;
633 kpic
->auto_eoi
= s
->auto_eoi
;
634 kpic
->rotate_on_auto_eoi
= s
->rotate_on_auto_eoi
;
635 kpic
->special_fully_nested_mode
= s
->special_fully_nested_mode
;
636 kpic
->init4
= s
->init4
;
637 kpic
->elcr
= s
->elcr
;
638 kpic
->elcr_mask
= s
->elcr_mask
;
640 kvm_set_irqchip(kvm_state
, &chip
);
645 #ifdef KVM_CAP_IRQCHIP
646 static void kvm_i8259_set_irq(void *opaque
, int irq
, int level
)
649 if (kvm_set_irq(irq
, level
, &pic_ret
)) {
651 apic_set_irq_delivered();
656 static void kvm_pic_init1(int io_addr
, PicState
*s
)
658 vmstate_register(NULL
, io_addr
, &vmstate_pic
, s
);
659 qemu_register_reset(pic_reset
, s
);
662 qemu_irq
*kvm_i8259_init(qemu_irq parent_irq
)
666 s
= qemu_mallocz(sizeof(PicState2
));
668 kvm_pic_init1(0x20, &s
->pics
[0]);
669 kvm_pic_init1(0xa0, &s
->pics
[1]);
670 s
->parent_irq
= parent_irq
;
671 s
->pics
[0].pics_state
= s
;
672 s
->pics
[1].pics_state
= s
;
674 return qemu_allocate_irqs(kvm_i8259_set_irq
, s
, 24);