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"
34 #define DPRINTF(fmt, ...) \
35 do { printf("pic: " fmt , ## __VA_ARGS__); } while (0)
37 #define DPRINTF(fmt, ...)
40 //#define DEBUG_IRQ_LATENCY
41 //#define DEBUG_IRQ_COUNT
44 uint8_t last_irr
; /* edge detection */
45 uint8_t irr
; /* interrupt request register */
46 uint8_t imr
; /* interrupt mask register */
47 uint8_t isr
; /* interrupt service register */
48 uint8_t priority_add
; /* highest irq priority */
50 uint8_t read_reg_select
;
55 uint8_t rotate_on_auto_eoi
;
56 uint8_t special_fully_nested_mode
;
57 uint8_t init4
; /* true if 4 byte init */
58 uint8_t single_mode
; /* true if slave pic is not initialized */
59 uint8_t elcr
; /* PIIX edge/trigger selection*/
62 bool master
; /* reflects /SP input pin */
67 #if defined(DEBUG_PIC) || defined (DEBUG_IRQ_COUNT)
68 static int irq_level
[16];
70 #ifdef DEBUG_IRQ_COUNT
71 static uint64_t irq_count
[16];
74 static PicState
*slave_pic
;
76 /* return the highest priority found in mask (highest = smallest
77 number). Return 8 if no irq */
78 static int get_priority(PicState
*s
, int mask
)
84 while ((mask
& (1 << ((priority
+ s
->priority_add
) & 7))) == 0)
89 /* return the pic wanted interrupt. return -1 if none */
90 static int pic_get_irq(PicState
*s
)
92 int mask
, cur_priority
, priority
;
94 mask
= s
->irr
& ~s
->imr
;
95 priority
= get_priority(s
, mask
);
98 /* compute current priority. If special fully nested mode on the
99 master, the IRQ coming from the slave is not taken into account
100 for the priority computation. */
104 if (s
->special_fully_nested_mode
&& s
->master
) {
107 cur_priority
= get_priority(s
, mask
);
108 if (priority
< cur_priority
) {
109 /* higher priority found: an irq should be generated */
110 return (priority
+ s
->priority_add
) & 7;
116 /* Update INT output. Must be called every time the output may have changed. */
117 static void pic_update_irq(PicState
*s
)
121 irq
= pic_get_irq(s
);
123 DPRINTF("pic%d: imr=%x irr=%x padd=%d\n",
124 s
->master
? 0 : 1, s
->imr
, s
->irr
, s
->priority_add
);
125 qemu_irq_raise(s
->int_out
);
127 qemu_irq_lower(s
->int_out
);
131 /* set irq level. If an edge is detected, then the IRR is set to 1 */
132 static void pic_set_irq1(PicState
*s
, int irq
, int level
)
136 if (s
->elcr
& mask
) {
137 /* level triggered */
143 s
->last_irr
&= ~mask
;
148 if ((s
->last_irr
& mask
) == 0) {
153 s
->last_irr
&= ~mask
;
159 #ifdef DEBUG_IRQ_LATENCY
160 int64_t irq_time
[16];
163 static void i8259_set_irq(void *opaque
, int irq
, int level
)
165 PicState
*s
= irq
<= 7 ? isa_pic
: slave_pic
;
167 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
168 if (level
!= irq_level
[irq
]) {
169 DPRINTF("i8259_set_irq: irq=%d level=%d\n", irq
, level
);
170 irq_level
[irq
] = level
;
171 #ifdef DEBUG_IRQ_COUNT
177 #ifdef DEBUG_IRQ_LATENCY
179 irq_time
[irq
] = qemu_get_clock_ns(vm_clock
);
182 pic_set_irq1(s
, irq
& 7, level
);
185 /* acknowledge interrupt 'irq' */
186 static void pic_intack(PicState
*s
, int irq
)
189 if (s
->rotate_on_auto_eoi
)
190 s
->priority_add
= (irq
+ 1) & 7;
192 s
->isr
|= (1 << irq
);
194 /* We don't clear a level sensitive interrupt here */
195 if (!(s
->elcr
& (1 << irq
)))
196 s
->irr
&= ~(1 << irq
);
200 int pic_read_irq(PicState
*s
)
202 int irq
, irq2
, intno
;
204 irq
= pic_get_irq(s
);
207 irq2
= pic_get_irq(slave_pic
);
209 pic_intack(slave_pic
, irq2
);
211 /* spurious IRQ on slave controller */
214 intno
= slave_pic
->irq_base
+ irq2
;
216 intno
= s
->irq_base
+ irq
;
220 /* spurious IRQ on host controller */
222 intno
= s
->irq_base
+ irq
;
225 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_LATENCY)
230 #ifdef DEBUG_IRQ_LATENCY
231 printf("IRQ%d latency=%0.3fus\n",
233 (double)(qemu_get_clock_ns(vm_clock
) -
234 irq_time
[irq
]) * 1000000.0 / get_ticks_per_sec());
236 DPRINTF("pic_interrupt: irq=%d\n", irq
);
240 static void pic_init_reset(PicState
*s
)
248 s
->read_reg_select
= 0;
253 s
->rotate_on_auto_eoi
= 0;
254 s
->special_fully_nested_mode
= 0;
257 /* Note: ELCR is not reset */
261 static void pic_reset(void *opaque
)
263 PicState
*s
= opaque
;
269 static void pic_ioport_write(void *opaque
, target_phys_addr_t addr64
,
270 uint64_t val64
, unsigned size
)
272 PicState
*s
= opaque
;
273 uint32_t addr
= addr64
;
274 uint32_t val
= val64
;
275 int priority
, cmd
, irq
;
277 DPRINTF("write: addr=0x%02x val=0x%02x\n", addr
, val
);
283 s
->single_mode
= val
& 2;
285 hw_error("level sensitive irq not supported");
286 } else if (val
& 0x08) {
290 s
->read_reg_select
= val
& 1;
292 s
->special_mask
= (val
>> 5) & 1;
298 s
->rotate_on_auto_eoi
= cmd
>> 2;
300 case 1: /* end of interrupt */
302 priority
= get_priority(s
, s
->isr
);
304 irq
= (priority
+ s
->priority_add
) & 7;
305 s
->isr
&= ~(1 << irq
);
307 s
->priority_add
= (irq
+ 1) & 7;
313 s
->isr
&= ~(1 << irq
);
317 s
->priority_add
= (val
+ 1) & 7;
322 s
->isr
&= ~(1 << irq
);
323 s
->priority_add
= (irq
+ 1) & 7;
332 switch(s
->init_state
) {
339 s
->irq_base
= val
& 0xf8;
340 s
->init_state
= s
->single_mode
? (s
->init4
? 3 : 0) : 2;
350 s
->special_fully_nested_mode
= (val
>> 4) & 1;
351 s
->auto_eoi
= (val
>> 1) & 1;
358 static uint64_t pic_ioport_read(void *opaque
, target_phys_addr_t addr
,
361 PicState
*s
= opaque
;
365 ret
= pic_get_irq(s
);
375 if (s
->read_reg_select
)
383 DPRINTF("read: addr=0x%02x val=0x%02x\n", addr
, ret
);
387 int pic_get_output(PicState
*s
)
389 return (pic_get_irq(s
) >= 0);
392 static void elcr_ioport_write(void *opaque
, target_phys_addr_t addr
,
393 uint64_t val
, unsigned size
)
395 PicState
*s
= opaque
;
396 s
->elcr
= val
& s
->elcr_mask
;
399 static uint64_t elcr_ioport_read(void *opaque
, target_phys_addr_t addr
,
402 PicState
*s
= opaque
;
406 static const VMStateDescription vmstate_pic
= {
409 .minimum_version_id
= 1,
410 .minimum_version_id_old
= 1,
411 .fields
= (VMStateField
[]) {
412 VMSTATE_UINT8(last_irr
, PicState
),
413 VMSTATE_UINT8(irr
, PicState
),
414 VMSTATE_UINT8(imr
, PicState
),
415 VMSTATE_UINT8(isr
, PicState
),
416 VMSTATE_UINT8(priority_add
, PicState
),
417 VMSTATE_UINT8(irq_base
, PicState
),
418 VMSTATE_UINT8(read_reg_select
, PicState
),
419 VMSTATE_UINT8(poll
, PicState
),
420 VMSTATE_UINT8(special_mask
, PicState
),
421 VMSTATE_UINT8(init_state
, PicState
),
422 VMSTATE_UINT8(auto_eoi
, PicState
),
423 VMSTATE_UINT8(rotate_on_auto_eoi
, PicState
),
424 VMSTATE_UINT8(special_fully_nested_mode
, PicState
),
425 VMSTATE_UINT8(init4
, PicState
),
426 VMSTATE_UINT8(single_mode
, PicState
),
427 VMSTATE_UINT8(elcr
, PicState
),
428 VMSTATE_END_OF_LIST()
432 static const MemoryRegionOps pic_base_ioport_ops
= {
433 .read
= pic_ioport_read
,
434 .write
= pic_ioport_write
,
436 .min_access_size
= 1,
437 .max_access_size
= 1,
441 static const MemoryRegionOps pic_elcr_ioport_ops
= {
442 .read
= elcr_ioport_read
,
443 .write
= elcr_ioport_write
,
445 .min_access_size
= 1,
446 .max_access_size
= 1,
450 /* XXX: add generic master/slave system */
451 static void pic_init(int io_addr
, int elcr_addr
, PicState
*s
, qemu_irq int_out
,
454 s
->int_out
= int_out
;
457 memory_region_init_io(&s
->base_io
, &pic_base_ioport_ops
, s
, "pic", 2);
458 memory_region_init_io(&s
->elcr_io
, &pic_elcr_ioport_ops
, s
, "elcr", 1);
460 isa_register_ioport(NULL
, &s
->base_io
, io_addr
);
461 if (elcr_addr
>= 0) {
462 isa_register_ioport(NULL
, &s
->elcr_io
, elcr_addr
);
465 vmstate_register(NULL
, io_addr
, &vmstate_pic
, s
);
466 qemu_register_reset(pic_reset
, s
);
469 void pic_info(Monitor
*mon
)
477 for (i
= 0; i
< 2; i
++) {
478 s
= i
== 0 ? isa_pic
: slave_pic
;
479 monitor_printf(mon
, "pic%d: irr=%02x imr=%02x isr=%02x hprio=%d "
480 "irq_base=%02x rr_sel=%d elcr=%02x fnm=%d\n",
481 i
, s
->irr
, s
->imr
, s
->isr
, s
->priority_add
,
482 s
->irq_base
, s
->read_reg_select
, s
->elcr
,
483 s
->special_fully_nested_mode
);
487 void irq_info(Monitor
*mon
)
489 #ifndef DEBUG_IRQ_COUNT
490 monitor_printf(mon
, "irq statistic code not compiled.\n");
495 monitor_printf(mon
, "IRQ statistics:\n");
496 for (i
= 0; i
< 16; i
++) {
497 count
= irq_count
[i
];
499 monitor_printf(mon
, "%2d: %" PRId64
"\n", i
, count
);
504 qemu_irq
*i8259_init(qemu_irq parent_irq
)
509 irqs
= qemu_allocate_irqs(i8259_set_irq
, NULL
, 16);
511 s
= g_malloc0(sizeof(PicState
));
512 pic_init(0x20, 0x4d0, s
, parent_irq
, true);
516 s
= g_malloc0(sizeof(PicState
));
517 pic_init(0xa0, 0x4d1, s
, irqs
[2], false);