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
25 #include "hw/i386/pc.h"
26 #include "hw/isa/isa.h"
27 #include "monitor/monitor.h"
28 #include "qemu/timer.h"
29 #include "hw/isa/i8259_internal.h"
35 #define DPRINTF(fmt, ...) \
36 do { printf("pic: " fmt , ## __VA_ARGS__); } while (0)
38 #define DPRINTF(fmt, ...)
41 //#define DEBUG_IRQ_LATENCY
42 //#define DEBUG_IRQ_COUNT
44 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
45 static int irq_level
[16];
47 #ifdef DEBUG_IRQ_COUNT
48 static uint64_t irq_count
[16];
50 #ifdef DEBUG_IRQ_LATENCY
51 static int64_t irq_time
[16];
54 static PICCommonState
*slave_pic
;
56 /* return the highest priority found in mask (highest = smallest
57 number). Return 8 if no irq */
58 static int get_priority(PICCommonState
*s
, int mask
)
66 while ((mask
& (1 << ((priority
+ s
->priority_add
) & 7))) == 0) {
72 /* return the pic wanted interrupt. return -1 if none */
73 static int pic_get_irq(PICCommonState
*s
)
75 int mask
, cur_priority
, priority
;
77 mask
= s
->irr
& ~s
->imr
;
78 priority
= get_priority(s
, mask
);
82 /* compute current priority. If special fully nested mode on the
83 master, the IRQ coming from the slave is not taken into account
84 for the priority computation. */
86 if (s
->special_mask
) {
89 if (s
->special_fully_nested_mode
&& s
->master
) {
92 cur_priority
= get_priority(s
, mask
);
93 if (priority
< cur_priority
) {
94 /* higher priority found: an irq should be generated */
95 return (priority
+ s
->priority_add
) & 7;
101 /* Update INT output. Must be called every time the output may have changed. */
102 static void pic_update_irq(PICCommonState
*s
)
106 irq
= pic_get_irq(s
);
108 DPRINTF("pic%d: imr=%x irr=%x padd=%d\n",
109 s
->master
? 0 : 1, s
->imr
, s
->irr
, s
->priority_add
);
110 qemu_irq_raise(s
->int_out
[0]);
112 qemu_irq_lower(s
->int_out
[0]);
116 /* set irq level. If an edge is detected, then the IRR is set to 1 */
117 static void pic_set_irq(void *opaque
, int irq
, int level
)
119 PICCommonState
*s
= opaque
;
122 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT) || \
123 defined(DEBUG_IRQ_LATENCY)
124 int irq_index
= s
->master
? irq
: irq
+ 8;
126 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
127 if (level
!= irq_level
[irq_index
]) {
128 DPRINTF("pic_set_irq: irq=%d level=%d\n", irq_index
, level
);
129 irq_level
[irq_index
] = level
;
130 #ifdef DEBUG_IRQ_COUNT
132 irq_count
[irq_index
]++;
137 #ifdef DEBUG_IRQ_LATENCY
139 irq_time
[irq_index
] = qemu_get_clock_ns(vm_clock
);
143 if (s
->elcr
& mask
) {
144 /* level triggered */
150 s
->last_irr
&= ~mask
;
155 if ((s
->last_irr
& mask
) == 0) {
160 s
->last_irr
&= ~mask
;
166 /* acknowledge interrupt 'irq' */
167 static void pic_intack(PICCommonState
*s
, int irq
)
170 if (s
->rotate_on_auto_eoi
) {
171 s
->priority_add
= (irq
+ 1) & 7;
174 s
->isr
|= (1 << irq
);
176 /* We don't clear a level sensitive interrupt here */
177 if (!(s
->elcr
& (1 << irq
))) {
178 s
->irr
&= ~(1 << irq
);
183 int pic_read_irq(DeviceState
*d
)
185 PICCommonState
*s
= DO_UPCAST(PICCommonState
, dev
.qdev
, d
);
186 int irq
, irq2
, intno
;
188 irq
= pic_get_irq(s
);
191 irq2
= pic_get_irq(slave_pic
);
193 pic_intack(slave_pic
, irq2
);
195 /* spurious IRQ on slave controller */
198 intno
= slave_pic
->irq_base
+ irq2
;
200 intno
= s
->irq_base
+ irq
;
204 /* spurious IRQ on host controller */
206 intno
= s
->irq_base
+ irq
;
209 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_LATENCY)
214 #ifdef DEBUG_IRQ_LATENCY
215 printf("IRQ%d latency=%0.3fus\n",
217 (double)(qemu_get_clock_ns(vm_clock
) -
218 irq_time
[irq
]) * 1000000.0 / get_ticks_per_sec());
220 DPRINTF("pic_interrupt: irq=%d\n", irq
);
224 static void pic_init_reset(PICCommonState
*s
)
230 static void pic_reset(DeviceState
*dev
)
232 PICCommonState
*s
= DO_UPCAST(PICCommonState
, dev
.qdev
, dev
);
238 static void pic_ioport_write(void *opaque
, hwaddr addr64
,
239 uint64_t val64
, unsigned size
)
241 PICCommonState
*s
= opaque
;
242 uint32_t addr
= addr64
;
243 uint32_t val
= val64
;
244 int priority
, cmd
, irq
;
246 DPRINTF("write: addr=0x%02x val=0x%02x\n", addr
, val
);
252 s
->single_mode
= val
& 2;
254 hw_error("level sensitive irq not supported");
256 } else if (val
& 0x08) {
261 s
->read_reg_select
= val
& 1;
264 s
->special_mask
= (val
>> 5) & 1;
271 s
->rotate_on_auto_eoi
= cmd
>> 2;
273 case 1: /* end of interrupt */
275 priority
= get_priority(s
, s
->isr
);
277 irq
= (priority
+ s
->priority_add
) & 7;
278 s
->isr
&= ~(1 << irq
);
280 s
->priority_add
= (irq
+ 1) & 7;
287 s
->isr
&= ~(1 << irq
);
291 s
->priority_add
= (val
+ 1) & 7;
296 s
->isr
&= ~(1 << irq
);
297 s
->priority_add
= (irq
+ 1) & 7;
306 switch (s
->init_state
) {
313 s
->irq_base
= val
& 0xf8;
314 s
->init_state
= s
->single_mode
? (s
->init4
? 3 : 0) : 2;
324 s
->special_fully_nested_mode
= (val
>> 4) & 1;
325 s
->auto_eoi
= (val
>> 1) & 1;
332 static uint64_t pic_ioport_read(void *opaque
, hwaddr addr
,
335 PICCommonState
*s
= opaque
;
339 ret
= pic_get_irq(s
);
349 if (s
->read_reg_select
) {
358 DPRINTF("read: addr=0x%02x val=0x%02x\n", addr
, ret
);
362 int pic_get_output(DeviceState
*d
)
364 PICCommonState
*s
= DO_UPCAST(PICCommonState
, dev
.qdev
, d
);
366 return (pic_get_irq(s
) >= 0);
369 static void elcr_ioport_write(void *opaque
, hwaddr addr
,
370 uint64_t val
, unsigned size
)
372 PICCommonState
*s
= opaque
;
373 s
->elcr
= val
& s
->elcr_mask
;
376 static uint64_t elcr_ioport_read(void *opaque
, hwaddr addr
,
379 PICCommonState
*s
= opaque
;
383 static const MemoryRegionOps pic_base_ioport_ops
= {
384 .read
= pic_ioport_read
,
385 .write
= pic_ioport_write
,
387 .min_access_size
= 1,
388 .max_access_size
= 1,
392 static const MemoryRegionOps pic_elcr_ioport_ops
= {
393 .read
= elcr_ioport_read
,
394 .write
= elcr_ioport_write
,
396 .min_access_size
= 1,
397 .max_access_size
= 1,
401 static void pic_init(PICCommonState
*s
)
403 memory_region_init_io(&s
->base_io
, &pic_base_ioport_ops
, s
, "pic", 2);
404 memory_region_init_io(&s
->elcr_io
, &pic_elcr_ioport_ops
, s
, "elcr", 1);
406 qdev_init_gpio_out(&s
->dev
.qdev
, s
->int_out
, ARRAY_SIZE(s
->int_out
));
407 qdev_init_gpio_in(&s
->dev
.qdev
, pic_set_irq
, 8);
410 void pic_info(Monitor
*mon
, const QDict
*qdict
)
418 for (i
= 0; i
< 2; i
++) {
419 s
= i
== 0 ? DO_UPCAST(PICCommonState
, dev
.qdev
, isa_pic
) : slave_pic
;
420 monitor_printf(mon
, "pic%d: irr=%02x imr=%02x isr=%02x hprio=%d "
421 "irq_base=%02x rr_sel=%d elcr=%02x fnm=%d\n",
422 i
, s
->irr
, s
->imr
, s
->isr
, s
->priority_add
,
423 s
->irq_base
, s
->read_reg_select
, s
->elcr
,
424 s
->special_fully_nested_mode
);
428 void irq_info(Monitor
*mon
, const QDict
*qdict
)
430 #ifndef DEBUG_IRQ_COUNT
431 monitor_printf(mon
, "irq statistic code not compiled.\n");
436 monitor_printf(mon
, "IRQ statistics:\n");
437 for (i
= 0; i
< 16; i
++) {
438 count
= irq_count
[i
];
440 monitor_printf(mon
, "%2d: %" PRId64
"\n", i
, count
);
446 qemu_irq
*i8259_init(ISABus
*bus
, qemu_irq parent_irq
)
452 irq_set
= g_malloc(ISA_NUM_IRQS
* sizeof(qemu_irq
));
454 dev
= i8259_init_chip("isa-i8259", bus
, true);
456 qdev_connect_gpio_out(&dev
->qdev
, 0, parent_irq
);
457 for (i
= 0 ; i
< 8; i
++) {
458 irq_set
[i
] = qdev_get_gpio_in(&dev
->qdev
, i
);
461 isa_pic
= &dev
->qdev
;
463 dev
= i8259_init_chip("isa-i8259", bus
, false);
465 qdev_connect_gpio_out(&dev
->qdev
, 0, irq_set
[2]);
466 for (i
= 0 ; i
< 8; i
++) {
467 irq_set
[i
+ 8] = qdev_get_gpio_in(&dev
->qdev
, i
);
470 slave_pic
= DO_UPCAST(PICCommonState
, dev
, dev
);
475 static void i8259_class_init(ObjectClass
*klass
, void *data
)
477 PICCommonClass
*k
= PIC_COMMON_CLASS(klass
);
478 DeviceClass
*dc
= DEVICE_CLASS(klass
);
481 dc
->reset
= pic_reset
;
484 static const TypeInfo i8259_info
= {
486 .instance_size
= sizeof(PICCommonState
),
487 .parent
= TYPE_PIC_COMMON
,
488 .class_init
= i8259_class_init
,
491 static void pic_register_types(void)
493 type_register_static(&i8259_info
);
496 type_init(pic_register_types
)