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"
33 //#define DEBUG_IRQ_LATENCY
34 //#define DEBUG_IRQ_COUNT
36 typedef struct PicState
{
37 uint8_t last_irr
; /* edge detection */
38 uint8_t irr
; /* interrupt request register */
39 uint8_t imr
; /* interrupt mask register */
40 uint8_t isr
; /* interrupt service register */
41 uint8_t priority_add
; /* highest irq priority */
43 uint8_t read_reg_select
;
48 uint8_t rotate_on_auto_eoi
;
49 uint8_t special_fully_nested_mode
;
50 uint8_t init4
; /* true if 4 byte init */
51 uint8_t single_mode
; /* true if slave pic is not initialized */
52 uint8_t elcr
; /* PIIX edge/trigger selection*/
54 PicState2
*pics_state
;
58 /* 0 is master pic, 1 is slave pic */
59 /* XXX: better separation between the two pics */
62 void *irq_request_opaque
;
65 #if defined(DEBUG_PIC) || defined (DEBUG_IRQ_COUNT)
66 static int irq_level
[16];
68 #ifdef DEBUG_IRQ_COUNT
69 static uint64_t irq_count
[16];
72 /* set irq level. If an edge is detected, then the IRR is set to 1 */
73 static inline void pic_set_irq1(PicState
*s
, int irq
, int level
)
89 if ((s
->last_irr
& mask
) == 0)
98 /* return the highest priority found in mask (highest = smallest
99 number). Return 8 if no irq */
100 static inline int get_priority(PicState
*s
, int mask
)
106 while ((mask
& (1 << ((priority
+ s
->priority_add
) & 7))) == 0)
111 /* return the pic wanted interrupt. return -1 if none */
112 static int pic_get_irq(PicState
*s
)
114 int mask
, cur_priority
, priority
;
116 mask
= s
->irr
& ~s
->imr
;
117 priority
= get_priority(s
, mask
);
120 /* compute current priority. If special fully nested mode on the
121 master, the IRQ coming from the slave is not taken into account
122 for the priority computation. */
126 if (s
->special_fully_nested_mode
&& s
== &s
->pics_state
->pics
[0])
128 cur_priority
= get_priority(s
, mask
);
129 if (priority
< cur_priority
) {
130 /* higher priority found: an irq should be generated */
131 return (priority
+ s
->priority_add
) & 7;
137 /* raise irq to CPU if necessary. must be called every time the active
139 /* XXX: should not export it, but it is needed for an APIC kludge */
140 void pic_update_irq(PicState2
*s
)
144 /* first look at slave pic */
145 irq2
= pic_get_irq(&s
->pics
[1]);
147 /* if irq request by slave pic, signal master PIC */
148 pic_set_irq1(&s
->pics
[0], 2, 1);
149 pic_set_irq1(&s
->pics
[0], 2, 0);
151 /* look at requested irq */
152 irq
= pic_get_irq(&s
->pics
[0]);
154 #if defined(DEBUG_PIC)
157 for(i
= 0; i
< 2; i
++) {
158 printf("pic%d: imr=%x irr=%x padd=%d\n",
159 i
, s
->pics
[i
].imr
, s
->pics
[i
].irr
,
160 s
->pics
[i
].priority_add
);
164 printf("pic: cpu_interrupt\n");
166 qemu_irq_raise(s
->parent_irq
);
169 /* all targets should do this rather than acking the IRQ in the cpu */
170 #if defined(TARGET_MIPS) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
172 qemu_irq_lower(s
->parent_irq
);
177 #ifdef DEBUG_IRQ_LATENCY
178 int64_t irq_time
[16];
181 static void i8259_set_irq(void *opaque
, int irq
, int level
)
183 PicState2
*s
= opaque
;
185 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
186 if (level
!= irq_level
[irq
]) {
187 #if defined(DEBUG_PIC)
188 printf("i8259_set_irq: irq=%d level=%d\n", irq
, level
);
190 irq_level
[irq
] = level
;
191 #ifdef DEBUG_IRQ_COUNT
197 #ifdef DEBUG_IRQ_LATENCY
199 irq_time
[irq
] = qemu_get_clock(vm_clock
);
202 pic_set_irq1(&s
->pics
[irq
>> 3], irq
& 7, level
);
206 /* acknowledge interrupt 'irq' */
207 static inline void pic_intack(PicState
*s
, int irq
)
210 if (s
->rotate_on_auto_eoi
)
211 s
->priority_add
= (irq
+ 1) & 7;
213 s
->isr
|= (1 << irq
);
215 /* We don't clear a level sensitive interrupt here */
216 if (!(s
->elcr
& (1 << irq
)))
217 s
->irr
&= ~(1 << irq
);
220 int pic_read_irq(PicState2
*s
)
222 int irq
, irq2
, intno
;
224 irq
= pic_get_irq(&s
->pics
[0]);
226 pic_intack(&s
->pics
[0], irq
);
228 irq2
= pic_get_irq(&s
->pics
[1]);
230 pic_intack(&s
->pics
[1], irq2
);
232 /* spurious IRQ on slave controller */
235 intno
= s
->pics
[1].irq_base
+ irq2
;
238 intno
= s
->pics
[0].irq_base
+ irq
;
241 /* spurious IRQ on host controller */
243 intno
= s
->pics
[0].irq_base
+ irq
;
247 #ifdef DEBUG_IRQ_LATENCY
248 printf("IRQ%d latency=%0.3fus\n",
250 (double)(qemu_get_clock(vm_clock
) -
251 irq_time
[irq
]) * 1000000.0 / get_ticks_per_sec());
253 #if defined(DEBUG_PIC)
254 printf("pic_interrupt: irq=%d\n", irq
);
259 static void pic_reset(void *opaque
)
261 PicState
*s
= opaque
;
269 s
->read_reg_select
= 0;
274 s
->rotate_on_auto_eoi
= 0;
275 s
->special_fully_nested_mode
= 0;
278 /* Note: ELCR is not reset */
281 static void pic_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
283 PicState
*s
= opaque
;
284 int priority
, cmd
, irq
;
287 printf("pic_write: addr=0x%02x val=0x%02x\n", addr
, val
);
294 /* deassert a pending interrupt */
295 qemu_irq_lower(s
->pics_state
->parent_irq
);
298 s
->single_mode
= val
& 2;
300 hw_error("level sensitive irq not supported");
301 } else if (val
& 0x08) {
305 s
->read_reg_select
= val
& 1;
307 s
->special_mask
= (val
>> 5) & 1;
313 s
->rotate_on_auto_eoi
= cmd
>> 2;
315 case 1: /* end of interrupt */
317 priority
= get_priority(s
, s
->isr
);
319 irq
= (priority
+ s
->priority_add
) & 7;
320 s
->isr
&= ~(1 << irq
);
322 s
->priority_add
= (irq
+ 1) & 7;
323 pic_update_irq(s
->pics_state
);
328 s
->isr
&= ~(1 << irq
);
329 pic_update_irq(s
->pics_state
);
332 s
->priority_add
= (val
+ 1) & 7;
333 pic_update_irq(s
->pics_state
);
337 s
->isr
&= ~(1 << irq
);
338 s
->priority_add
= (irq
+ 1) & 7;
339 pic_update_irq(s
->pics_state
);
347 switch(s
->init_state
) {
351 pic_update_irq(s
->pics_state
);
354 s
->irq_base
= val
& 0xf8;
355 s
->init_state
= s
->single_mode
? (s
->init4
? 3 : 0) : 2;
365 s
->special_fully_nested_mode
= (val
>> 4) & 1;
366 s
->auto_eoi
= (val
>> 1) & 1;
373 static uint32_t pic_poll_read (PicState
*s
, uint32_t addr1
)
377 ret
= pic_get_irq(s
);
380 s
->pics_state
->pics
[0].isr
&= ~(1 << 2);
381 s
->pics_state
->pics
[0].irr
&= ~(1 << 2);
383 s
->irr
&= ~(1 << ret
);
384 s
->isr
&= ~(1 << ret
);
385 if (addr1
>> 7 || ret
!= 2)
386 pic_update_irq(s
->pics_state
);
389 pic_update_irq(s
->pics_state
);
395 static uint32_t pic_ioport_read(void *opaque
, uint32_t addr1
)
397 PicState
*s
= opaque
;
404 ret
= pic_poll_read(s
, addr1
);
408 if (s
->read_reg_select
)
417 printf("pic_read: addr=0x%02x val=0x%02x\n", addr1
, ret
);
422 /* memory mapped interrupt status */
423 /* XXX: may be the same than pic_read_irq() */
424 uint32_t pic_intack_read(PicState2
*s
)
428 ret
= pic_poll_read(&s
->pics
[0], 0x00);
430 ret
= pic_poll_read(&s
->pics
[1], 0x80) + 8;
431 /* Prepare for ISR read */
432 s
->pics
[0].read_reg_select
= 1;
437 static void elcr_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
439 PicState
*s
= opaque
;
440 s
->elcr
= val
& s
->elcr_mask
;
443 static uint32_t elcr_ioport_read(void *opaque
, uint32_t addr1
)
445 PicState
*s
= opaque
;
449 static const VMStateDescription vmstate_pic
= {
452 .minimum_version_id
= 1,
453 .minimum_version_id_old
= 1,
454 .fields
= (VMStateField
[]) {
455 VMSTATE_UINT8(last_irr
, PicState
),
456 VMSTATE_UINT8(irr
, PicState
),
457 VMSTATE_UINT8(imr
, PicState
),
458 VMSTATE_UINT8(isr
, PicState
),
459 VMSTATE_UINT8(priority_add
, PicState
),
460 VMSTATE_UINT8(irq_base
, PicState
),
461 VMSTATE_UINT8(read_reg_select
, PicState
),
462 VMSTATE_UINT8(poll
, PicState
),
463 VMSTATE_UINT8(special_mask
, PicState
),
464 VMSTATE_UINT8(init_state
, PicState
),
465 VMSTATE_UINT8(auto_eoi
, PicState
),
466 VMSTATE_UINT8(rotate_on_auto_eoi
, PicState
),
467 VMSTATE_UINT8(special_fully_nested_mode
, PicState
),
468 VMSTATE_UINT8(init4
, PicState
),
469 VMSTATE_UINT8(single_mode
, PicState
),
470 VMSTATE_UINT8(elcr
, PicState
),
471 VMSTATE_END_OF_LIST()
475 /* XXX: add generic master/slave system */
476 static void pic_init1(int io_addr
, int elcr_addr
, PicState
*s
)
478 register_ioport_write(io_addr
, 2, 1, pic_ioport_write
, s
);
479 register_ioport_read(io_addr
, 2, 1, pic_ioport_read
, s
);
480 if (elcr_addr
>= 0) {
481 register_ioport_write(elcr_addr
, 1, 1, elcr_ioport_write
, s
);
482 register_ioport_read(elcr_addr
, 1, 1, elcr_ioport_read
, s
);
484 vmstate_register(io_addr
, &vmstate_pic
, s
);
485 qemu_register_reset(pic_reset
, s
);
488 void pic_info(Monitor
*mon
)
497 s
= &isa_pic
->pics
[i
];
498 monitor_printf(mon
, "pic%d: irr=%02x imr=%02x isr=%02x hprio=%d "
499 "irq_base=%02x rr_sel=%d elcr=%02x fnm=%d\n",
500 i
, s
->irr
, s
->imr
, s
->isr
, s
->priority_add
,
501 s
->irq_base
, s
->read_reg_select
, s
->elcr
,
502 s
->special_fully_nested_mode
);
506 void irq_info(Monitor
*mon
)
508 #ifndef DEBUG_IRQ_COUNT
509 monitor_printf(mon
, "irq statistic code not compiled.\n");
514 monitor_printf(mon
, "IRQ statistics:\n");
515 for (i
= 0; i
< 16; i
++) {
516 count
= irq_count
[i
];
518 monitor_printf(mon
, "%2d: %" PRId64
"\n", i
, count
);
523 qemu_irq
*i8259_init(qemu_irq parent_irq
)
527 s
= qemu_mallocz(sizeof(PicState2
));
528 pic_init1(0x20, 0x4d0, &s
->pics
[0]);
529 pic_init1(0xa0, 0x4d1, &s
->pics
[1]);
530 s
->pics
[0].elcr_mask
= 0xf8;
531 s
->pics
[1].elcr_mask
= 0xde;
532 s
->parent_irq
= parent_irq
;
533 s
->pics
[0].pics_state
= s
;
534 s
->pics
[1].pics_state
= s
;
536 return qemu_allocate_irqs(i8259_set_irq
, s
, 16);