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"
35 //#define DEBUG_IRQ_LATENCY
36 //#define DEBUG_IRQ_COUNT
38 typedef struct PicState
{
39 uint8_t last_irr
; /* edge detection */
40 uint8_t irr
; /* interrupt request register */
41 uint8_t imr
; /* interrupt mask register */
42 uint8_t isr
; /* interrupt service register */
43 uint8_t priority_add
; /* highest irq priority */
45 uint8_t read_reg_select
;
50 uint8_t rotate_on_auto_eoi
;
51 uint8_t special_fully_nested_mode
;
52 uint8_t init4
; /* true if 4 byte init */
53 uint8_t single_mode
; /* true if slave pic is not initialized */
54 uint8_t elcr
; /* PIIX edge/trigger selection*/
56 PicState2
*pics_state
;
60 /* 0 is master pic, 1 is slave pic */
61 /* XXX: better separation between the two pics */
64 void *irq_request_opaque
;
65 /* IOAPIC callback support */
66 SetIRQFunc
*alt_irq_func
;
70 #if defined(DEBUG_PIC) || defined (DEBUG_IRQ_COUNT)
71 static int irq_level
[16];
73 #ifdef DEBUG_IRQ_COUNT
74 static uint64_t irq_count
[16];
77 /* set irq level. If an edge is detected, then the IRR is set to 1 */
78 static inline void pic_set_irq1(PicState
*s
, int irq
, int level
)
94 if ((s
->last_irr
& mask
) == 0)
103 /* return the highest priority found in mask (highest = smallest
104 number). Return 8 if no irq */
105 static inline int get_priority(PicState
*s
, int mask
)
111 while ((mask
& (1 << ((priority
+ s
->priority_add
) & 7))) == 0)
116 /* return the pic wanted interrupt. return -1 if none */
117 static int pic_get_irq(PicState
*s
)
119 int mask
, cur_priority
, priority
;
121 mask
= s
->irr
& ~s
->imr
;
122 priority
= get_priority(s
, mask
);
125 /* compute current priority. If special fully nested mode on the
126 master, the IRQ coming from the slave is not taken into account
127 for the priority computation. */
131 if (s
->special_fully_nested_mode
&& s
== &s
->pics_state
->pics
[0])
133 cur_priority
= get_priority(s
, mask
);
134 if (priority
< cur_priority
) {
135 /* higher priority found: an irq should be generated */
136 return (priority
+ s
->priority_add
) & 7;
142 /* raise irq to CPU if necessary. must be called every time the active
144 /* XXX: should not export it, but it is needed for an APIC kludge */
145 void pic_update_irq(PicState2
*s
)
149 /* first look at slave pic */
150 irq2
= pic_get_irq(&s
->pics
[1]);
152 /* if irq request by slave pic, signal master PIC */
153 pic_set_irq1(&s
->pics
[0], 2, 1);
154 pic_set_irq1(&s
->pics
[0], 2, 0);
156 /* look at requested irq */
157 irq
= pic_get_irq(&s
->pics
[0]);
159 #if defined(DEBUG_PIC)
162 for(i
= 0; i
< 2; i
++) {
163 printf("pic%d: imr=%x irr=%x padd=%d\n",
164 i
, s
->pics
[i
].imr
, s
->pics
[i
].irr
,
165 s
->pics
[i
].priority_add
);
169 printf("pic: cpu_interrupt\n");
171 qemu_irq_raise(s
->parent_irq
);
174 /* all targets should do this rather than acking the IRQ in the cpu */
175 #if defined(TARGET_MIPS) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
177 qemu_irq_lower(s
->parent_irq
);
182 #ifdef DEBUG_IRQ_LATENCY
183 int64_t irq_time
[16];
186 static void i8259_set_irq(void *opaque
, int irq
, int level
)
188 PicState2
*s
= opaque
;
189 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
190 if (level
!= irq_level
[irq
]) {
191 #if defined(DEBUG_PIC)
192 printf("i8259_set_irq: irq=%d level=%d\n", irq
, level
);
194 irq_level
[irq
] = level
;
195 #ifdef DEBUG_IRQ_COUNT
201 #ifdef DEBUG_IRQ_LATENCY
203 irq_time
[irq
] = qemu_get_clock(vm_clock
);
206 pic_set_irq1(&s
->pics
[irq
>> 3], irq
& 7, level
);
207 /* used for IOAPIC irqs */
209 s
->alt_irq_func(s
->alt_irq_opaque
, irq
, level
);
213 /* acknowledge interrupt 'irq' */
214 static inline void pic_intack(PicState
*s
, int irq
)
217 if (s
->rotate_on_auto_eoi
)
218 s
->priority_add
= (irq
+ 1) & 7;
220 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
);
229 extern int time_drift_fix
;
231 int pic_read_irq(PicState2
*s
)
233 int irq
, irq2
, intno
;
235 irq
= pic_get_irq(&s
->pics
[0]);
238 pic_intack(&s
->pics
[0], irq
);
240 if (time_drift_fix
&& irq
== 0) {
241 extern int64_t timer_acks
, timer_ints_to_push
;
243 if (timer_ints_to_push
> 0) {
244 timer_ints_to_push
--;
245 /* simulate an edge irq0, like the one generated by i8254 */
246 pic_set_irq1(&s
->pics
[0], 0, 0);
247 pic_set_irq1(&s
->pics
[0], 0, 1);
252 irq2
= pic_get_irq(&s
->pics
[1]);
254 pic_intack(&s
->pics
[1], irq2
);
256 /* spurious IRQ on slave controller */
259 intno
= s
->pics
[1].irq_base
+ irq2
;
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(vm_clock
) - irq_time
[irq
]) * 1000000.0 / ticks_per_sec
);
276 #if defined(DEBUG_PIC)
277 printf("pic_interrupt: irq=%d\n", irq
);
282 static void pic_reset(void *opaque
)
284 PicState
*s
= opaque
;
292 s
->read_reg_select
= 0;
297 s
->rotate_on_auto_eoi
= 0;
298 s
->special_fully_nested_mode
= 0;
301 /* Note: ELCR is not reset */
304 static void pic_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
306 PicState
*s
= opaque
;
307 int priority
, cmd
, irq
;
310 printf("pic_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
)
440 printf("pic_read: addr=0x%02x val=0x%02x\n", addr1
, ret
);
445 /* memory mapped interrupt status */
446 /* XXX: may be the same than pic_read_irq() */
447 uint32_t pic_intack_read(PicState2
*s
)
451 ret
= pic_poll_read(&s
->pics
[0], 0x00);
453 ret
= pic_poll_read(&s
->pics
[1], 0x80) + 8;
454 /* Prepare for ISR read */
455 s
->pics
[0].read_reg_select
= 1;
460 static void elcr_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
462 PicState
*s
= opaque
;
463 s
->elcr
= val
& s
->elcr_mask
;
466 static uint32_t elcr_ioport_read(void *opaque
, uint32_t addr1
)
468 PicState
*s
= opaque
;
472 static void pic_save_common(QEMUFile
*f
, void *opaque
)
474 PicState
*s
= opaque
;
476 qemu_put_8s(f
, &s
->last_irr
);
477 qemu_put_8s(f
, &s
->irr
);
478 qemu_put_8s(f
, &s
->imr
);
479 qemu_put_8s(f
, &s
->isr
);
480 qemu_put_8s(f
, &s
->priority_add
);
481 qemu_put_8s(f
, &s
->irq_base
);
482 qemu_put_8s(f
, &s
->read_reg_select
);
483 qemu_put_8s(f
, &s
->poll
);
484 qemu_put_8s(f
, &s
->special_mask
);
485 qemu_put_8s(f
, &s
->init_state
);
486 qemu_put_8s(f
, &s
->auto_eoi
);
487 qemu_put_8s(f
, &s
->rotate_on_auto_eoi
);
488 qemu_put_8s(f
, &s
->special_fully_nested_mode
);
489 qemu_put_8s(f
, &s
->init4
);
490 qemu_put_8s(f
, &s
->single_mode
);
491 qemu_put_8s(f
, &s
->elcr
);
494 static void pic_save(QEMUFile
*f
, void *opaque
)
496 pic_save_common(f
, opaque
);
499 static int pic_load_common(QEMUFile
*f
, void *opaque
, int version_id
)
501 PicState
*s
= opaque
;
506 qemu_get_8s(f
, &s
->last_irr
);
507 qemu_get_8s(f
, &s
->irr
);
508 qemu_get_8s(f
, &s
->imr
);
509 qemu_get_8s(f
, &s
->isr
);
510 qemu_get_8s(f
, &s
->priority_add
);
511 qemu_get_8s(f
, &s
->irq_base
);
512 qemu_get_8s(f
, &s
->read_reg_select
);
513 qemu_get_8s(f
, &s
->poll
);
514 qemu_get_8s(f
, &s
->special_mask
);
515 qemu_get_8s(f
, &s
->init_state
);
516 qemu_get_8s(f
, &s
->auto_eoi
);
517 qemu_get_8s(f
, &s
->rotate_on_auto_eoi
);
518 qemu_get_8s(f
, &s
->special_fully_nested_mode
);
519 qemu_get_8s(f
, &s
->init4
);
520 qemu_get_8s(f
, &s
->single_mode
);
521 qemu_get_8s(f
, &s
->elcr
);
526 static int pic_load(QEMUFile
*f
, void *opaque
, int version_id
)
528 return pic_load_common(f
, opaque
, version_id
);
531 /* XXX: add generic master/slave system */
532 static void pic_init1(int io_addr
, int elcr_addr
, PicState
*s
)
534 register_ioport_write(io_addr
, 2, 1, pic_ioport_write
, s
);
535 register_ioport_read(io_addr
, 2, 1, pic_ioport_read
, s
);
536 if (elcr_addr
>= 0) {
537 register_ioport_write(elcr_addr
, 1, 1, elcr_ioport_write
, s
);
538 register_ioport_read(elcr_addr
, 1, 1, elcr_ioport_read
, s
);
540 register_savevm("i8259", io_addr
, 1, pic_save
, pic_load
, s
);
541 qemu_register_reset(pic_reset
, s
);
544 void pic_info(Monitor
*mon
)
553 s
= &isa_pic
->pics
[i
];
554 monitor_printf(mon
, "pic%d: irr=%02x imr=%02x isr=%02x hprio=%d "
555 "irq_base=%02x rr_sel=%d elcr=%02x fnm=%d\n",
556 i
, s
->irr
, s
->imr
, s
->isr
, s
->priority_add
,
557 s
->irq_base
, s
->read_reg_select
, s
->elcr
,
558 s
->special_fully_nested_mode
);
562 void irq_info(Monitor
*mon
)
564 #ifndef DEBUG_IRQ_COUNT
565 monitor_printf(mon
, "irq statistic code not compiled.\n");
570 monitor_printf(mon
, "IRQ statistics:\n");
571 for (i
= 0; i
< 16; i
++) {
572 count
= irq_count
[i
];
574 monitor_printf(mon
, "%2d: %" PRId64
"\n", i
, count
);
579 qemu_irq
*i8259_init(qemu_irq parent_irq
)
583 s
= qemu_mallocz(sizeof(PicState2
));
584 pic_init1(0x20, 0x4d0, &s
->pics
[0]);
585 pic_init1(0xa0, 0x4d1, &s
->pics
[1]);
586 s
->pics
[0].elcr_mask
= 0xf8;
587 s
->pics
[1].elcr_mask
= 0xde;
588 s
->parent_irq
= parent_irq
;
589 s
->pics
[0].pics_state
= s
;
590 s
->pics
[1].pics_state
= s
;
592 return qemu_allocate_irqs(i8259_set_irq
, s
, 16);
595 void pic_set_alt_irq_func(PicState2
*s
, SetIRQFunc
*alt_irq_func
,
596 void *alt_irq_opaque
)
598 s
->alt_irq_func
= alt_irq_func
;
599 s
->alt_irq_opaque
= alt_irq_opaque
;
602 #ifdef KVM_CAP_IRQCHIP
603 static void kvm_kernel_pic_save_to_user(PicState
*s
)
605 #if defined(TARGET_I386)
606 struct kvm_irqchip chip
;
607 struct kvm_pic_state
*kpic
;
609 chip
.chip_id
= (&s
->pics_state
->pics
[0] == s
) ?
610 KVM_IRQCHIP_PIC_MASTER
:
611 KVM_IRQCHIP_PIC_SLAVE
;
612 kvm_get_irqchip(kvm_context
, &chip
);
613 kpic
= &chip
.chip
.pic
;
615 s
->last_irr
= kpic
->last_irr
;
619 s
->priority_add
= kpic
->priority_add
;
620 s
->irq_base
= kpic
->irq_base
;
621 s
->read_reg_select
= kpic
->read_reg_select
;
622 s
->poll
= kpic
->poll
;
623 s
->special_mask
= kpic
->special_mask
;
624 s
->init_state
= kpic
->init_state
;
625 s
->auto_eoi
= kpic
->auto_eoi
;
626 s
->rotate_on_auto_eoi
= kpic
->rotate_on_auto_eoi
;
627 s
->special_fully_nested_mode
= kpic
->special_fully_nested_mode
;
628 s
->init4
= kpic
->init4
;
629 s
->elcr
= kpic
->elcr
;
630 s
->elcr_mask
= kpic
->elcr_mask
;
634 static void kvm_kernel_pic_load_from_user(PicState
*s
)
636 #if defined(TARGET_I386)
637 struct kvm_irqchip chip
;
638 struct kvm_pic_state
*kpic
;
640 chip
.chip_id
= (&s
->pics_state
->pics
[0] == s
) ?
641 KVM_IRQCHIP_PIC_MASTER
:
642 KVM_IRQCHIP_PIC_SLAVE
;
643 kpic
= &chip
.chip
.pic
;
645 kpic
->last_irr
= s
->last_irr
;
649 kpic
->priority_add
= s
->priority_add
;
650 kpic
->irq_base
= s
->irq_base
;
651 kpic
->read_reg_select
= s
->read_reg_select
;
652 kpic
->poll
= s
->poll
;
653 kpic
->special_mask
= s
->special_mask
;
654 kpic
->init_state
= s
->init_state
;
655 kpic
->auto_eoi
= s
->auto_eoi
;
656 kpic
->rotate_on_auto_eoi
= s
->rotate_on_auto_eoi
;
657 kpic
->special_fully_nested_mode
= s
->special_fully_nested_mode
;
658 kpic
->init4
= s
->init4
;
659 kpic
->elcr
= s
->elcr
;
660 kpic
->elcr_mask
= s
->elcr_mask
;
662 kvm_set_irqchip(kvm_context
, &chip
);
666 static void kvm_i8259_set_irq(void *opaque
, int irq
, int level
)
669 if (kvm_set_irq(irq
, level
, &pic_ret
)) {
671 apic_set_irq_delivered();
676 static void kvm_pic_save(QEMUFile
*f
, void *opaque
)
678 PicState
*s
= opaque
;
680 kvm_kernel_pic_save_to_user(s
);
681 pic_save_common(f
, opaque
);
684 static int kvm_pic_load(QEMUFile
*f
, void *opaque
, int version_id
)
686 PicState
*s
= opaque
;
687 int r
= pic_load_common(f
, s
, version_id
);
689 kvm_kernel_pic_load_from_user(s
);
693 static void kvm_pic_init1(int io_addr
, PicState
*s
)
695 register_savevm("i8259", io_addr
, 1, kvm_pic_save
, kvm_pic_load
, s
);
696 qemu_register_reset(pic_reset
, s
);
699 qemu_irq
*kvm_i8259_init(qemu_irq parent_irq
)
703 s
= qemu_mallocz(sizeof(PicState2
));
705 kvm_pic_init1(0x20, &s
->pics
[0]);
706 kvm_pic_init1(0xa0, &s
->pics
[1]);
707 s
->parent_irq
= parent_irq
;
708 s
->pics
[0].pics_state
= s
;
709 s
->pics
[1].pics_state
= s
;
711 return qemu_allocate_irqs(kvm_i8259_set_irq
, s
, 16);