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
45 uint8_t last_irr
; /* edge detection */
46 uint8_t irr
; /* interrupt request register */
47 uint8_t imr
; /* interrupt mask register */
48 uint8_t isr
; /* interrupt service register */
49 uint8_t priority_add
; /* highest irq priority */
51 uint8_t read_reg_select
;
56 uint8_t rotate_on_auto_eoi
;
57 uint8_t special_fully_nested_mode
;
58 uint8_t init4
; /* true if 4 byte init */
59 uint8_t single_mode
; /* true if slave pic is not initialized */
60 uint8_t elcr
; /* PIIX edge/trigger selection*/
63 uint32_t master
; /* reflects /SP input pin */
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];
76 #ifdef DEBUG_IRQ_LATENCY
77 static int64_t irq_time
[16];
80 static PicState
*slave_pic
;
82 /* return the highest priority found in mask (highest = smallest
83 number). Return 8 if no irq */
84 static int get_priority(PicState
*s
, int mask
)
92 while ((mask
& (1 << ((priority
+ s
->priority_add
) & 7))) == 0) {
98 /* return the pic wanted interrupt. return -1 if none */
99 static int pic_get_irq(PicState
*s
)
101 int mask
, cur_priority
, priority
;
103 mask
= s
->irr
& ~s
->imr
;
104 priority
= get_priority(s
, mask
);
108 /* compute current priority. If special fully nested mode on the
109 master, the IRQ coming from the slave is not taken into account
110 for the priority computation. */
112 if (s
->special_mask
) {
115 if (s
->special_fully_nested_mode
&& s
->master
) {
118 cur_priority
= get_priority(s
, mask
);
119 if (priority
< cur_priority
) {
120 /* higher priority found: an irq should be generated */
121 return (priority
+ s
->priority_add
) & 7;
127 /* Update INT output. Must be called every time the output may have changed. */
128 static void pic_update_irq(PicState
*s
)
132 irq
= pic_get_irq(s
);
134 DPRINTF("pic%d: imr=%x irr=%x padd=%d\n",
135 s
->master
? 0 : 1, s
->imr
, s
->irr
, s
->priority_add
);
136 qemu_irq_raise(s
->int_out
[0]);
138 qemu_irq_lower(s
->int_out
[0]);
142 /* set irq level. If an edge is detected, then the IRR is set to 1 */
143 static void pic_set_irq(void *opaque
, int irq
, int level
)
145 PicState
*s
= opaque
;
148 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT) || \
149 defined(DEBUG_IRQ_LATENCY)
150 int irq_index
= s
->master
? irq
: irq
+ 8;
152 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
153 if (level
!= irq_level
[irq_index
]) {
154 DPRINTF("pic_set_irq: irq=%d level=%d\n", irq_index
, level
);
155 irq_level
[irq_index
] = level
;
156 #ifdef DEBUG_IRQ_COUNT
158 irq_count
[irq_index
]++;
163 #ifdef DEBUG_IRQ_LATENCY
165 irq_time
[irq_index
] = qemu_get_clock_ns(vm_clock
);
169 if (s
->elcr
& mask
) {
170 /* level triggered */
176 s
->last_irr
&= ~mask
;
181 if ((s
->last_irr
& mask
) == 0) {
186 s
->last_irr
&= ~mask
;
192 /* acknowledge interrupt 'irq' */
193 static void pic_intack(PicState
*s
, int irq
)
196 if (s
->rotate_on_auto_eoi
) {
197 s
->priority_add
= (irq
+ 1) & 7;
200 s
->isr
|= (1 << irq
);
202 /* We don't clear a level sensitive interrupt here */
203 if (!(s
->elcr
& (1 << irq
))) {
204 s
->irr
&= ~(1 << irq
);
209 int pic_read_irq(PicState
*s
)
211 int irq
, irq2
, intno
;
213 irq
= pic_get_irq(s
);
216 irq2
= pic_get_irq(slave_pic
);
218 pic_intack(slave_pic
, irq2
);
220 /* spurious IRQ on slave controller */
223 intno
= slave_pic
->irq_base
+ irq2
;
225 intno
= s
->irq_base
+ irq
;
229 /* spurious IRQ on host controller */
231 intno
= s
->irq_base
+ irq
;
234 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_LATENCY)
239 #ifdef DEBUG_IRQ_LATENCY
240 printf("IRQ%d latency=%0.3fus\n",
242 (double)(qemu_get_clock_ns(vm_clock
) -
243 irq_time
[irq
]) * 1000000.0 / get_ticks_per_sec());
245 DPRINTF("pic_interrupt: irq=%d\n", irq
);
249 static void pic_init_reset(PicState
*s
)
257 s
->read_reg_select
= 0;
262 s
->rotate_on_auto_eoi
= 0;
263 s
->special_fully_nested_mode
= 0;
266 /* Note: ELCR is not reset */
270 static void pic_reset(DeviceState
*dev
)
272 PicState
*s
= container_of(dev
, PicState
, dev
.qdev
);
278 static void pic_ioport_write(void *opaque
, target_phys_addr_t addr64
,
279 uint64_t val64
, unsigned size
)
281 PicState
*s
= opaque
;
282 uint32_t addr
= addr64
;
283 uint32_t val
= val64
;
284 int priority
, cmd
, irq
;
286 DPRINTF("write: addr=0x%02x val=0x%02x\n", addr
, val
);
292 s
->single_mode
= val
& 2;
294 hw_error("level sensitive irq not supported");
296 } else if (val
& 0x08) {
301 s
->read_reg_select
= val
& 1;
304 s
->special_mask
= (val
>> 5) & 1;
311 s
->rotate_on_auto_eoi
= cmd
>> 2;
313 case 1: /* end of interrupt */
315 priority
= get_priority(s
, s
->isr
);
317 irq
= (priority
+ s
->priority_add
) & 7;
318 s
->isr
&= ~(1 << irq
);
320 s
->priority_add
= (irq
+ 1) & 7;
327 s
->isr
&= ~(1 << irq
);
331 s
->priority_add
= (val
+ 1) & 7;
336 s
->isr
&= ~(1 << irq
);
337 s
->priority_add
= (irq
+ 1) & 7;
346 switch (s
->init_state
) {
353 s
->irq_base
= val
& 0xf8;
354 s
->init_state
= s
->single_mode
? (s
->init4
? 3 : 0) : 2;
364 s
->special_fully_nested_mode
= (val
>> 4) & 1;
365 s
->auto_eoi
= (val
>> 1) & 1;
372 static uint64_t pic_ioport_read(void *opaque
, target_phys_addr_t addr
,
375 PicState
*s
= opaque
;
379 ret
= pic_get_irq(s
);
389 if (s
->read_reg_select
) {
398 DPRINTF("read: addr=0x%02x val=0x%02x\n", addr
, ret
);
402 int pic_get_output(PicState
*s
)
404 return (pic_get_irq(s
) >= 0);
407 static void elcr_ioport_write(void *opaque
, target_phys_addr_t addr
,
408 uint64_t val
, unsigned size
)
410 PicState
*s
= opaque
;
411 s
->elcr
= val
& s
->elcr_mask
;
414 static uint64_t elcr_ioport_read(void *opaque
, target_phys_addr_t addr
,
417 PicState
*s
= opaque
;
421 static const VMStateDescription vmstate_pic
= {
424 .minimum_version_id
= 1,
425 .minimum_version_id_old
= 1,
426 .fields
= (VMStateField
[]) {
427 VMSTATE_UINT8(last_irr
, PicState
),
428 VMSTATE_UINT8(irr
, PicState
),
429 VMSTATE_UINT8(imr
, PicState
),
430 VMSTATE_UINT8(isr
, PicState
),
431 VMSTATE_UINT8(priority_add
, PicState
),
432 VMSTATE_UINT8(irq_base
, PicState
),
433 VMSTATE_UINT8(read_reg_select
, PicState
),
434 VMSTATE_UINT8(poll
, PicState
),
435 VMSTATE_UINT8(special_mask
, PicState
),
436 VMSTATE_UINT8(init_state
, PicState
),
437 VMSTATE_UINT8(auto_eoi
, PicState
),
438 VMSTATE_UINT8(rotate_on_auto_eoi
, PicState
),
439 VMSTATE_UINT8(special_fully_nested_mode
, PicState
),
440 VMSTATE_UINT8(init4
, PicState
),
441 VMSTATE_UINT8(single_mode
, PicState
),
442 VMSTATE_UINT8(elcr
, PicState
),
443 VMSTATE_END_OF_LIST()
447 static const MemoryRegionOps pic_base_ioport_ops
= {
448 .read
= pic_ioport_read
,
449 .write
= pic_ioport_write
,
451 .min_access_size
= 1,
452 .max_access_size
= 1,
456 static const MemoryRegionOps pic_elcr_ioport_ops
= {
457 .read
= elcr_ioport_read
,
458 .write
= elcr_ioport_write
,
460 .min_access_size
= 1,
461 .max_access_size
= 1,
465 static int pic_initfn(ISADevice
*dev
)
467 PicState
*s
= DO_UPCAST(PicState
, dev
, dev
);
469 memory_region_init_io(&s
->base_io
, &pic_base_ioport_ops
, s
, "pic", 2);
470 memory_region_init_io(&s
->elcr_io
, &pic_elcr_ioport_ops
, s
, "elcr", 1);
472 isa_register_ioport(dev
, &s
->base_io
, s
->iobase
);
473 if (s
->elcr_addr
!= -1) {
474 isa_register_ioport(dev
, &s
->elcr_io
, s
->elcr_addr
);
477 qdev_init_gpio_out(&dev
->qdev
, s
->int_out
, ARRAY_SIZE(s
->int_out
));
478 qdev_init_gpio_in(&dev
->qdev
, pic_set_irq
, 8);
480 qdev_set_legacy_instance_id(&dev
->qdev
, s
->iobase
, 1);
485 void pic_info(Monitor
*mon
)
493 for (i
= 0; i
< 2; i
++) {
494 s
= i
== 0 ? isa_pic
: slave_pic
;
495 monitor_printf(mon
, "pic%d: irr=%02x imr=%02x isr=%02x hprio=%d "
496 "irq_base=%02x rr_sel=%d elcr=%02x fnm=%d\n",
497 i
, s
->irr
, s
->imr
, s
->isr
, s
->priority_add
,
498 s
->irq_base
, s
->read_reg_select
, s
->elcr
,
499 s
->special_fully_nested_mode
);
503 void irq_info(Monitor
*mon
)
505 #ifndef DEBUG_IRQ_COUNT
506 monitor_printf(mon
, "irq statistic code not compiled.\n");
511 monitor_printf(mon
, "IRQ statistics:\n");
512 for (i
= 0; i
< 16; i
++) {
513 count
= irq_count
[i
];
515 monitor_printf(mon
, "%2d: %" PRId64
"\n", i
, count
);
521 qemu_irq
*i8259_init(ISABus
*bus
, qemu_irq parent_irq
)
527 irq_set
= g_malloc(ISA_NUM_IRQS
* sizeof(qemu_irq
));
529 dev
= isa_create(bus
, "isa-i8259");
530 qdev_prop_set_uint32(&dev
->qdev
, "iobase", 0x20);
531 qdev_prop_set_uint32(&dev
->qdev
, "elcr_addr", 0x4d0);
532 qdev_prop_set_uint8(&dev
->qdev
, "elcr_mask", 0xf8);
533 qdev_prop_set_bit(&dev
->qdev
, "master", true);
534 qdev_init_nofail(&dev
->qdev
);
536 qdev_connect_gpio_out(&dev
->qdev
, 0, parent_irq
);
537 for (i
= 0 ; i
< 8; i
++) {
538 irq_set
[i
] = qdev_get_gpio_in(&dev
->qdev
, i
);
541 isa_pic
= DO_UPCAST(PicState
, dev
, dev
);
543 dev
= isa_create(bus
, "isa-i8259");
544 qdev_prop_set_uint32(&dev
->qdev
, "iobase", 0xa0);
545 qdev_prop_set_uint32(&dev
->qdev
, "elcr_addr", 0x4d1);
546 qdev_prop_set_uint8(&dev
->qdev
, "elcr_mask", 0xde);
547 qdev_init_nofail(&dev
->qdev
);
549 qdev_connect_gpio_out(&dev
->qdev
, 0, irq_set
[2]);
550 for (i
= 0 ; i
< 8; i
++) {
551 irq_set
[i
+ 8] = qdev_get_gpio_in(&dev
->qdev
, i
);
554 slave_pic
= DO_UPCAST(PicState
, dev
, dev
);
559 static ISADeviceInfo i8259_info
= {
560 .qdev
.name
= "isa-i8259",
561 .qdev
.size
= sizeof(PicState
),
562 .qdev
.vmsd
= &vmstate_pic
,
563 .qdev
.reset
= pic_reset
,
566 .qdev
.props
= (Property
[]) {
567 DEFINE_PROP_HEX32("iobase", PicState
, iobase
, -1),
568 DEFINE_PROP_HEX32("elcr_addr", PicState
, elcr_addr
, -1),
569 DEFINE_PROP_HEX8("elcr_mask", PicState
, elcr_mask
, -1),
570 DEFINE_PROP_BIT("master", PicState
, master
, 0, false),
571 DEFINE_PROP_END_OF_LIST(),
575 static void pic_register(void)
577 isa_qdev_register(&i8259_info
);
579 device_init(pic_register
)