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
24 #include "qemu/osdep.h"
26 #include "hw/i386/pc.h"
27 #include "hw/isa/isa.h"
28 #include "monitor/monitor.h"
29 #include "qemu/timer.h"
30 #include "hw/isa/i8259_internal.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 #define TYPE_I8259 "isa-i8259"
46 #define PIC_CLASS(class) OBJECT_CLASS_CHECK(PICClass, (class), TYPE_I8259)
47 #define PIC_GET_CLASS(obj) OBJECT_GET_CLASS(PICClass, (obj), TYPE_I8259)
51 * @parent_realize: The parent's realizefn.
53 typedef struct PICClass
{
54 PICCommonClass parent_class
;
56 DeviceRealize parent_realize
;
59 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
60 static int irq_level
[16];
62 #ifdef DEBUG_IRQ_COUNT
63 static uint64_t irq_count
[16];
65 #ifdef DEBUG_IRQ_LATENCY
66 static int64_t irq_time
[16];
69 static PICCommonState
*slave_pic
;
71 /* return the highest priority found in mask (highest = smallest
72 number). Return 8 if no irq */
73 static int get_priority(PICCommonState
*s
, int mask
)
81 while ((mask
& (1 << ((priority
+ s
->priority_add
) & 7))) == 0) {
87 /* return the pic wanted interrupt. return -1 if none */
88 static int pic_get_irq(PICCommonState
*s
)
90 int mask
, cur_priority
, priority
;
92 mask
= s
->irr
& ~s
->imr
;
93 priority
= get_priority(s
, mask
);
97 /* compute current priority. If special fully nested mode on the
98 master, the IRQ coming from the slave is not taken into account
99 for the priority computation. */
101 if (s
->special_mask
) {
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(PICCommonState
*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
[0]);
127 qemu_irq_lower(s
->int_out
[0]);
131 /* set irq level. If an edge is detected, then the IRR is set to 1 */
132 static void pic_set_irq(void *opaque
, int irq
, int level
)
134 PICCommonState
*s
= opaque
;
137 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT) || \
138 defined(DEBUG_IRQ_LATENCY)
139 int irq_index
= s
->master
? irq
: irq
+ 8;
141 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
142 if (level
!= irq_level
[irq_index
]) {
143 DPRINTF("pic_set_irq: irq=%d level=%d\n", irq_index
, level
);
144 irq_level
[irq_index
] = level
;
145 #ifdef DEBUG_IRQ_COUNT
147 irq_count
[irq_index
]++;
152 #ifdef DEBUG_IRQ_LATENCY
154 irq_time
[irq_index
] = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
158 if (s
->elcr
& mask
) {
159 /* level triggered */
165 s
->last_irr
&= ~mask
;
170 if ((s
->last_irr
& mask
) == 0) {
175 s
->last_irr
&= ~mask
;
181 /* acknowledge interrupt 'irq' */
182 static void pic_intack(PICCommonState
*s
, int irq
)
185 if (s
->rotate_on_auto_eoi
) {
186 s
->priority_add
= (irq
+ 1) & 7;
189 s
->isr
|= (1 << irq
);
191 /* We don't clear a level sensitive interrupt here */
192 if (!(s
->elcr
& (1 << irq
))) {
193 s
->irr
&= ~(1 << irq
);
198 int pic_read_irq(DeviceState
*d
)
200 PICCommonState
*s
= PIC_COMMON(d
);
201 int irq
, irq2
, intno
;
203 irq
= pic_get_irq(s
);
206 irq2
= pic_get_irq(slave_pic
);
208 pic_intack(slave_pic
, irq2
);
210 /* spurious IRQ on slave controller */
213 intno
= slave_pic
->irq_base
+ irq2
;
215 intno
= s
->irq_base
+ irq
;
219 /* spurious IRQ on host controller */
221 intno
= s
->irq_base
+ irq
;
224 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_LATENCY)
229 #ifdef DEBUG_IRQ_LATENCY
230 printf("IRQ%d latency=%0.3fus\n",
232 (double)(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) -
233 irq_time
[irq
]) * 1000000.0 / get_ticks_per_sec());
235 DPRINTF("pic_interrupt: irq=%d\n", irq
);
239 static void pic_init_reset(PICCommonState
*s
)
245 static void pic_reset(DeviceState
*dev
)
247 PICCommonState
*s
= PIC_COMMON(dev
);
253 static void pic_ioport_write(void *opaque
, hwaddr addr64
,
254 uint64_t val64
, unsigned size
)
256 PICCommonState
*s
= opaque
;
257 uint32_t addr
= addr64
;
258 uint32_t val
= val64
;
259 int priority
, cmd
, irq
;
261 DPRINTF("write: addr=0x%02x val=0x%02x\n", addr
, val
);
267 s
->single_mode
= val
& 2;
269 qemu_log_mask(LOG_UNIMP
,
270 "i8259: level sensitive irq not supported\n");
272 } else if (val
& 0x08) {
277 s
->read_reg_select
= val
& 1;
280 s
->special_mask
= (val
>> 5) & 1;
287 s
->rotate_on_auto_eoi
= cmd
>> 2;
289 case 1: /* end of interrupt */
291 priority
= get_priority(s
, s
->isr
);
293 irq
= (priority
+ s
->priority_add
) & 7;
294 s
->isr
&= ~(1 << irq
);
296 s
->priority_add
= (irq
+ 1) & 7;
303 s
->isr
&= ~(1 << irq
);
307 s
->priority_add
= (val
+ 1) & 7;
312 s
->isr
&= ~(1 << irq
);
313 s
->priority_add
= (irq
+ 1) & 7;
322 switch (s
->init_state
) {
329 s
->irq_base
= val
& 0xf8;
330 s
->init_state
= s
->single_mode
? (s
->init4
? 3 : 0) : 2;
340 s
->special_fully_nested_mode
= (val
>> 4) & 1;
341 s
->auto_eoi
= (val
>> 1) & 1;
348 static uint64_t pic_ioport_read(void *opaque
, hwaddr addr
,
351 PICCommonState
*s
= opaque
;
355 ret
= pic_get_irq(s
);
365 if (s
->read_reg_select
) {
374 DPRINTF("read: addr=0x%02" HWADDR_PRIx
" val=0x%02x\n", addr
, ret
);
378 int pic_get_output(DeviceState
*d
)
380 PICCommonState
*s
= PIC_COMMON(d
);
382 return (pic_get_irq(s
) >= 0);
385 static void elcr_ioport_write(void *opaque
, hwaddr addr
,
386 uint64_t val
, unsigned size
)
388 PICCommonState
*s
= opaque
;
389 s
->elcr
= val
& s
->elcr_mask
;
392 static uint64_t elcr_ioport_read(void *opaque
, hwaddr addr
,
395 PICCommonState
*s
= opaque
;
399 static const MemoryRegionOps pic_base_ioport_ops
= {
400 .read
= pic_ioport_read
,
401 .write
= pic_ioport_write
,
403 .min_access_size
= 1,
404 .max_access_size
= 1,
408 static const MemoryRegionOps pic_elcr_ioport_ops
= {
409 .read
= elcr_ioport_read
,
410 .write
= elcr_ioport_write
,
412 .min_access_size
= 1,
413 .max_access_size
= 1,
417 static void pic_realize(DeviceState
*dev
, Error
**errp
)
419 PICCommonState
*s
= PIC_COMMON(dev
);
420 PICClass
*pc
= PIC_GET_CLASS(dev
);
422 memory_region_init_io(&s
->base_io
, OBJECT(s
), &pic_base_ioport_ops
, s
,
424 memory_region_init_io(&s
->elcr_io
, OBJECT(s
), &pic_elcr_ioport_ops
, s
,
427 qdev_init_gpio_out(dev
, s
->int_out
, ARRAY_SIZE(s
->int_out
));
428 qdev_init_gpio_in(dev
, pic_set_irq
, 8);
430 pc
->parent_realize(dev
, errp
);
433 void hmp_info_pic(Monitor
*mon
, const QDict
*qdict
)
441 for (i
= 0; i
< 2; i
++) {
442 s
= i
== 0 ? PIC_COMMON(isa_pic
) : slave_pic
;
443 monitor_printf(mon
, "pic%d: irr=%02x imr=%02x isr=%02x hprio=%d "
444 "irq_base=%02x rr_sel=%d elcr=%02x fnm=%d\n",
445 i
, s
->irr
, s
->imr
, s
->isr
, s
->priority_add
,
446 s
->irq_base
, s
->read_reg_select
, s
->elcr
,
447 s
->special_fully_nested_mode
);
451 void hmp_info_irq(Monitor
*mon
, const QDict
*qdict
)
453 #ifndef DEBUG_IRQ_COUNT
454 monitor_printf(mon
, "irq statistic code not compiled.\n");
459 monitor_printf(mon
, "IRQ statistics:\n");
460 for (i
= 0; i
< 16; i
++) {
461 count
= irq_count
[i
];
463 monitor_printf(mon
, "%2d: %" PRId64
"\n", i
, count
);
469 qemu_irq
*i8259_init(ISABus
*bus
, qemu_irq parent_irq
)
476 irq_set
= g_new0(qemu_irq
, ISA_NUM_IRQS
);
478 isadev
= i8259_init_chip(TYPE_I8259
, bus
, true);
479 dev
= DEVICE(isadev
);
481 qdev_connect_gpio_out(dev
, 0, parent_irq
);
482 for (i
= 0 ; i
< 8; i
++) {
483 irq_set
[i
] = qdev_get_gpio_in(dev
, i
);
488 isadev
= i8259_init_chip(TYPE_I8259
, bus
, false);
489 dev
= DEVICE(isadev
);
491 qdev_connect_gpio_out(dev
, 0, irq_set
[2]);
492 for (i
= 0 ; i
< 8; i
++) {
493 irq_set
[i
+ 8] = qdev_get_gpio_in(dev
, i
);
496 slave_pic
= PIC_COMMON(dev
);
501 static void i8259_class_init(ObjectClass
*klass
, void *data
)
503 PICClass
*k
= PIC_CLASS(klass
);
504 DeviceClass
*dc
= DEVICE_CLASS(klass
);
506 k
->parent_realize
= dc
->realize
;
507 dc
->realize
= pic_realize
;
508 dc
->reset
= pic_reset
;
511 static const TypeInfo i8259_info
= {
513 .instance_size
= sizeof(PICCommonState
),
514 .parent
= TYPE_PIC_COMMON
,
515 .class_init
= i8259_class_init
,
516 .class_size
= sizeof(PICClass
),
519 static void pic_register_types(void)
521 type_register_static(&i8259_info
);
524 type_init(pic_register_types
)