i8259.c 13.4 KB
Newer Older
B
bellard 已提交
1 2
/*
 * QEMU 8259 interrupt controller emulation
3
 *
B
bellard 已提交
4
 * Copyright (c) 2003-2004 Fabrice Bellard
5
 *
B
bellard 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24
#include "hw/hw.h"
P
Paolo Bonzini 已提交
25 26
#include "hw/i386/pc.h"
#include "hw/isa/isa.h"
27
#include "monitor/monitor.h"
28
#include "qemu/timer.h"
P
Paolo Bonzini 已提交
29
#include "hw/isa/i8259_internal.h"
B
bellard 已提交
30 31 32 33

/* debug PIC */
//#define DEBUG_PIC

B
Blue Swirl 已提交
34 35 36 37 38 39 40
#ifdef DEBUG_PIC
#define DPRINTF(fmt, ...)                                       \
    do { printf("pic: " fmt , ## __VA_ARGS__); } while (0)
#else
#define DPRINTF(fmt, ...)
#endif

B
bellard 已提交
41
//#define DEBUG_IRQ_LATENCY
42
//#define DEBUG_IRQ_COUNT
B
bellard 已提交
43

A
Andreas Färber 已提交
44
#define TYPE_I8259 "isa-i8259"
45 46 47 48 49 50 51 52 53 54 55 56
#define PIC_CLASS(class) OBJECT_CLASS_CHECK(PICClass, (class), TYPE_I8259)
#define PIC_GET_CLASS(obj) OBJECT_GET_CLASS(PICClass, (obj), TYPE_I8259)

/**
 * PICClass:
 * @parent_realize: The parent's realizefn.
 */
typedef struct PICClass {
    PICCommonClass parent_class;

    DeviceRealize parent_realize;
} PICClass;
A
Andreas Färber 已提交
57

J
Jan Kiszka 已提交
58
#if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
59 60 61 62 63
static int irq_level[16];
#endif
#ifdef DEBUG_IRQ_COUNT
static uint64_t irq_count[16];
#endif
J
Jan Kiszka 已提交
64 65 66
#ifdef DEBUG_IRQ_LATENCY
static int64_t irq_time[16];
#endif
67
DeviceState *isa_pic;
68
static PICCommonState *slave_pic;
69

B
bellard 已提交
70 71
/* return the highest priority found in mask (highest = smallest
   number). Return 8 if no irq */
72
static int get_priority(PICCommonState *s, int mask)
B
bellard 已提交
73 74
{
    int priority;
J
Jan Kiszka 已提交
75 76

    if (mask == 0) {
B
bellard 已提交
77
        return 8;
J
Jan Kiszka 已提交
78
    }
B
bellard 已提交
79
    priority = 0;
J
Jan Kiszka 已提交
80
    while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0) {
B
bellard 已提交
81
        priority++;
J
Jan Kiszka 已提交
82
    }
B
bellard 已提交
83 84 85 86
    return priority;
}

/* return the pic wanted interrupt. return -1 if none */
87
static int pic_get_irq(PICCommonState *s)
B
bellard 已提交
88 89 90 91 92
{
    int mask, cur_priority, priority;

    mask = s->irr & ~s->imr;
    priority = get_priority(s, mask);
J
Jan Kiszka 已提交
93
    if (priority == 8) {
B
bellard 已提交
94
        return -1;
J
Jan Kiszka 已提交
95
    }
B
bellard 已提交
96 97 98 99
    /* compute current priority. If special fully nested mode on the
       master, the IRQ coming from the slave is not taken into account
       for the priority computation. */
    mask = s->isr;
J
Jan Kiszka 已提交
100
    if (s->special_mask) {
101
        mask &= ~s->imr;
J
Jan Kiszka 已提交
102
    }
103
    if (s->special_fully_nested_mode && s->master) {
B
bellard 已提交
104
        mask &= ~(1 << 2);
105
    }
B
bellard 已提交
106 107 108 109 110 111 112 113 114
    cur_priority = get_priority(s, mask);
    if (priority < cur_priority) {
        /* higher priority found: an irq should be generated */
        return (priority + s->priority_add) & 7;
    } else {
        return -1;
    }
}

J
Jan Kiszka 已提交
115
/* Update INT output. Must be called every time the output may have changed. */
116
static void pic_update_irq(PICCommonState *s)
B
bellard 已提交
117
{
J
Jan Kiszka 已提交
118
    int irq;
B
bellard 已提交
119

J
Jan Kiszka 已提交
120
    irq = pic_get_irq(s);
B
bellard 已提交
121
    if (irq >= 0) {
J
Jan Kiszka 已提交
122
        DPRINTF("pic%d: imr=%x irr=%x padd=%d\n",
123
                s->master ? 0 : 1, s->imr, s->irr, s->priority_add);
J
Jan Kiszka 已提交
124
        qemu_irq_raise(s->int_out[0]);
125
    } else {
J
Jan Kiszka 已提交
126
        qemu_irq_lower(s->int_out[0]);
127
    }
B
bellard 已提交
128 129
}

130
/* set irq level. If an edge is detected, then the IRR is set to 1 */
J
Jan Kiszka 已提交
131
static void pic_set_irq(void *opaque, int irq, int level)
132
{
133
    PICCommonState *s = opaque;
J
Jan Kiszka 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    int mask = 1 << irq;

#if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT) || \
    defined(DEBUG_IRQ_LATENCY)
    int irq_index = s->master ? irq : irq + 8;
#endif
#if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
    if (level != irq_level[irq_index]) {
        DPRINTF("pic_set_irq: irq=%d level=%d\n", irq_index, level);
        irq_level[irq_index] = level;
#ifdef DEBUG_IRQ_COUNT
        if (level == 1) {
            irq_count[irq_index]++;
        }
#endif
    }
#endif
#ifdef DEBUG_IRQ_LATENCY
    if (level) {
        irq_time[irq_index] = qemu_get_clock_ns(vm_clock);
    }
#endif

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    if (s->elcr & mask) {
        /* level triggered */
        if (level) {
            s->irr |= mask;
            s->last_irr |= mask;
        } else {
            s->irr &= ~mask;
            s->last_irr &= ~mask;
        }
    } else {
        /* edge triggered */
        if (level) {
            if ((s->last_irr & mask) == 0) {
                s->irr |= mask;
            }
            s->last_irr |= mask;
        } else {
            s->last_irr &= ~mask;
        }
    }
J
Jan Kiszka 已提交
177
    pic_update_irq(s);
178 179
}

B
bellard 已提交
180
/* acknowledge interrupt 'irq' */
181
static void pic_intack(PICCommonState *s, int irq)
B
bellard 已提交
182 183
{
    if (s->auto_eoi) {
J
Jan Kiszka 已提交
184
        if (s->rotate_on_auto_eoi) {
B
bellard 已提交
185
            s->priority_add = (irq + 1) & 7;
J
Jan Kiszka 已提交
186
        }
B
bellard 已提交
187 188 189
    } else {
        s->isr |= (1 << irq);
    }
190
    /* We don't clear a level sensitive interrupt here */
J
Jan Kiszka 已提交
191
    if (!(s->elcr & (1 << irq))) {
192
        s->irr &= ~(1 << irq);
J
Jan Kiszka 已提交
193
    }
J
Jan Kiszka 已提交
194
    pic_update_irq(s);
B
bellard 已提交
195 196
}

197
int pic_read_irq(DeviceState *d)
B
bellard 已提交
198
{
A
Andreas Färber 已提交
199
    PICCommonState *s = PIC_COMMON(d);
B
bellard 已提交
200 201
    int irq, irq2, intno;

J
Jan Kiszka 已提交
202
    irq = pic_get_irq(s);
203 204
    if (irq >= 0) {
        if (irq == 2) {
J
Jan Kiszka 已提交
205
            irq2 = pic_get_irq(slave_pic);
206
            if (irq2 >= 0) {
J
Jan Kiszka 已提交
207
                pic_intack(slave_pic, irq2);
208 209 210 211
            } else {
                /* spurious IRQ on slave controller */
                irq2 = 7;
            }
J
Jan Kiszka 已提交
212
            intno = slave_pic->irq_base + irq2;
213
        } else {
J
Jan Kiszka 已提交
214
            intno = s->irq_base + irq;
215
        }
J
Jan Kiszka 已提交
216
        pic_intack(s, irq);
217 218 219
    } else {
        /* spurious IRQ on host controller */
        irq = 7;
J
Jan Kiszka 已提交
220
        intno = s->irq_base + irq;
221
    }
222

J
Jan Kiszka 已提交
223 224 225 226 227
#if defined(DEBUG_PIC) || defined(DEBUG_IRQ_LATENCY)
    if (irq == 2) {
        irq = irq2 + 8;
    }
#endif
B
bellard 已提交
228
#ifdef DEBUG_IRQ_LATENCY
229 230
    printf("IRQ%d latency=%0.3fus\n",
           irq,
231
           (double)(qemu_get_clock_ns(vm_clock) -
232
                    irq_time[irq]) * 1000000.0 / get_ticks_per_sec());
B
bellard 已提交
233
#endif
B
Blue Swirl 已提交
234
    DPRINTF("pic_interrupt: irq=%d\n", irq);
B
bellard 已提交
235 236 237
    return intno;
}

238
static void pic_init_reset(PICCommonState *s)
B
bellard 已提交
239
{
240
    pic_reset_common(s);
J
Jan Kiszka 已提交
241
    pic_update_irq(s);
B
bellard 已提交
242 243
}

J
Jan Kiszka 已提交
244
static void pic_reset(DeviceState *dev)
J
Jan Kiszka 已提交
245
{
A
Andreas Färber 已提交
246
    PICCommonState *s = PIC_COMMON(dev);
J
Jan Kiszka 已提交
247 248

    s->elcr = 0;
249
    pic_init_reset(s);
J
Jan Kiszka 已提交
250 251
}

A
Avi Kivity 已提交
252
static void pic_ioport_write(void *opaque, hwaddr addr64,
253
                             uint64_t val64, unsigned size)
B
bellard 已提交
254
{
255
    PICCommonState *s = opaque;
256 257
    uint32_t addr = addr64;
    uint32_t val = val64;
B
bellard 已提交
258
    int priority, cmd, irq;
B
bellard 已提交
259

B
Blue Swirl 已提交
260
    DPRINTF("write: addr=0x%02x val=0x%02x\n", addr, val);
B
bellard 已提交
261 262
    if (addr == 0) {
        if (val & 0x10) {
J
Jan Kiszka 已提交
263
            pic_init_reset(s);
B
bellard 已提交
264 265
            s->init_state = 1;
            s->init4 = val & 1;
266
            s->single_mode = val & 2;
J
Jan Kiszka 已提交
267
            if (val & 0x08) {
B
bellard 已提交
268
                hw_error("level sensitive irq not supported");
J
Jan Kiszka 已提交
269
            }
B
bellard 已提交
270
        } else if (val & 0x08) {
J
Jan Kiszka 已提交
271
            if (val & 0x04) {
B
bellard 已提交
272
                s->poll = 1;
J
Jan Kiszka 已提交
273 274
            }
            if (val & 0x02) {
B
bellard 已提交
275
                s->read_reg_select = val & 1;
J
Jan Kiszka 已提交
276 277
            }
            if (val & 0x40) {
B
bellard 已提交
278
                s->special_mask = (val >> 5) & 1;
J
Jan Kiszka 已提交
279
            }
B
bellard 已提交
280 281
        } else {
            cmd = val >> 5;
J
Jan Kiszka 已提交
282
            switch (cmd) {
B
bellard 已提交
283 284 285 286 287 288 289 290 291 292
            case 0:
            case 4:
                s->rotate_on_auto_eoi = cmd >> 2;
                break;
            case 1: /* end of interrupt */
            case 5:
                priority = get_priority(s, s->isr);
                if (priority != 8) {
                    irq = (priority + s->priority_add) & 7;
                    s->isr &= ~(1 << irq);
J
Jan Kiszka 已提交
293
                    if (cmd == 5) {
B
bellard 已提交
294
                        s->priority_add = (irq + 1) & 7;
J
Jan Kiszka 已提交
295
                    }
J
Jan Kiszka 已提交
296
                    pic_update_irq(s);
B
bellard 已提交
297 298 299 300 301
                }
                break;
            case 3:
                irq = val & 7;
                s->isr &= ~(1 << irq);
J
Jan Kiszka 已提交
302
                pic_update_irq(s);
B
bellard 已提交
303 304 305
                break;
            case 6:
                s->priority_add = (val + 1) & 7;
J
Jan Kiszka 已提交
306
                pic_update_irq(s);
B
bellard 已提交
307 308 309 310 311
                break;
            case 7:
                irq = val & 7;
                s->isr &= ~(1 << irq);
                s->priority_add = (irq + 1) & 7;
J
Jan Kiszka 已提交
312
                pic_update_irq(s);
B
bellard 已提交
313 314 315 316 317 318 319
                break;
            default:
                /* no operation */
                break;
            }
        }
    } else {
J
Jan Kiszka 已提交
320
        switch (s->init_state) {
B
bellard 已提交
321 322 323
        case 0:
            /* normal mode */
            s->imr = val;
J
Jan Kiszka 已提交
324
            pic_update_irq(s);
B
bellard 已提交
325 326 327
            break;
        case 1:
            s->irq_base = val & 0xf8;
328
            s->init_state = s->single_mode ? (s->init4 ? 3 : 0) : 2;
B
bellard 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
            break;
        case 2:
            if (s->init4) {
                s->init_state = 3;
            } else {
                s->init_state = 0;
            }
            break;
        case 3:
            s->special_fully_nested_mode = (val >> 4) & 1;
            s->auto_eoi = (val >> 1) & 1;
            s->init_state = 0;
            break;
        }
    }
}

A
Avi Kivity 已提交
346
static uint64_t pic_ioport_read(void *opaque, hwaddr addr,
347
                                unsigned size)
B
bellard 已提交
348
{
349
    PICCommonState *s = opaque;
B
bellard 已提交
350 351 352
    int ret;

    if (s->poll) {
J
Jan Kiszka 已提交
353 354 355 356 357 358 359
        ret = pic_get_irq(s);
        if (ret >= 0) {
            pic_intack(s, ret);
            ret |= 0x80;
        } else {
            ret = 0;
        }
B
bellard 已提交
360 361 362
        s->poll = 0;
    } else {
        if (addr == 0) {
J
Jan Kiszka 已提交
363
            if (s->read_reg_select) {
B
bellard 已提交
364
                ret = s->isr;
J
Jan Kiszka 已提交
365
            } else {
B
bellard 已提交
366
                ret = s->irr;
J
Jan Kiszka 已提交
367
            }
B
bellard 已提交
368 369 370 371
        } else {
            ret = s->imr;
        }
    }
372
    DPRINTF("read: addr=0x%02x val=0x%02x\n", addr, ret);
B
bellard 已提交
373 374 375
    return ret;
}

376
int pic_get_output(DeviceState *d)
377
{
A
Andreas Färber 已提交
378
    PICCommonState *s = PIC_COMMON(d);
379

J
Jan Kiszka 已提交
380
    return (pic_get_irq(s) >= 0);
381 382
}

A
Avi Kivity 已提交
383
static void elcr_ioport_write(void *opaque, hwaddr addr,
384
                              uint64_t val, unsigned size)
B
bellard 已提交
385
{
386
    PICCommonState *s = opaque;
B
bellard 已提交
387 388 389
    s->elcr = val & s->elcr_mask;
}

A
Avi Kivity 已提交
390
static uint64_t elcr_ioport_read(void *opaque, hwaddr addr,
391
                                 unsigned size)
B
bellard 已提交
392
{
393
    PICCommonState *s = opaque;
B
bellard 已提交
394 395 396
    return s->elcr;
}

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
static const MemoryRegionOps pic_base_ioport_ops = {
    .read = pic_ioport_read,
    .write = pic_ioport_write,
    .impl = {
        .min_access_size = 1,
        .max_access_size = 1,
    },
};

static const MemoryRegionOps pic_elcr_ioport_ops = {
    .read = elcr_ioport_read,
    .write = elcr_ioport_write,
    .impl = {
        .min_access_size = 1,
        .max_access_size = 1,
    },
};

415
static void pic_realize(DeviceState *dev, Error **err)
B
bellard 已提交
416
{
417 418
    PICCommonState *s = PIC_COMMON(dev);
    PICClass *pc = PIC_GET_CLASS(dev);
A
Andreas Färber 已提交
419

420 421
    memory_region_init_io(&s->base_io, NULL, &pic_base_ioport_ops, s, "pic", 2);
    memory_region_init_io(&s->elcr_io, NULL, &pic_elcr_ioport_ops, s, "elcr", 1);
422

A
Andreas Färber 已提交
423 424
    qdev_init_gpio_out(dev, s->int_out, ARRAY_SIZE(s->int_out));
    qdev_init_gpio_in(dev, pic_set_irq, 8);
425 426

    pc->parent_realize(dev, err);
B
bellard 已提交
427 428
}

429
void pic_info(Monitor *mon, const QDict *qdict)
B
bellard 已提交
430 431
{
    int i;
432
    PICCommonState *s;
433

J
Jan Kiszka 已提交
434
    if (!isa_pic) {
B
bellard 已提交
435
        return;
J
Jan Kiszka 已提交
436
    }
J
Jan Kiszka 已提交
437
    for (i = 0; i < 2; i++) {
A
Andreas Färber 已提交
438
        s = i == 0 ? PIC_COMMON(isa_pic) : slave_pic;
A
aliguori 已提交
439 440 441 442 443
        monitor_printf(mon, "pic%d: irr=%02x imr=%02x isr=%02x hprio=%d "
                       "irq_base=%02x rr_sel=%d elcr=%02x fnm=%d\n",
                       i, s->irr, s->imr, s->isr, s->priority_add,
                       s->irq_base, s->read_reg_select, s->elcr,
                       s->special_fully_nested_mode);
B
bellard 已提交
444 445 446
    }
}

447
void irq_info(Monitor *mon, const QDict *qdict)
448 449
{
#ifndef DEBUG_IRQ_COUNT
A
aliguori 已提交
450
    monitor_printf(mon, "irq statistic code not compiled.\n");
451 452 453 454
#else
    int i;
    int64_t count;

A
aliguori 已提交
455
    monitor_printf(mon, "IRQ statistics:\n");
456 457
    for (i = 0; i < 16; i++) {
        count = irq_count[i];
J
Jan Kiszka 已提交
458
        if (count > 0) {
A
aliguori 已提交
459
            monitor_printf(mon, "%2d: %" PRId64 "\n", i, count);
J
Jan Kiszka 已提交
460
        }
461 462 463
    }
#endif
}
B
bellard 已提交
464

465
qemu_irq *i8259_init(ISABus *bus, qemu_irq parent_irq)
B
bellard 已提交
466
{
J
Jan Kiszka 已提交
467
    qemu_irq *irq_set;
A
Andreas Färber 已提交
468 469
    DeviceState *dev;
    ISADevice *isadev;
J
Jan Kiszka 已提交
470
    int i;
J
Jan Kiszka 已提交
471

J
Jan Kiszka 已提交
472
    irq_set = g_malloc(ISA_NUM_IRQS * sizeof(qemu_irq));
J
Jan Kiszka 已提交
473

A
Andreas Färber 已提交
474 475
    isadev = i8259_init_chip(TYPE_I8259, bus, true);
    dev = DEVICE(isadev);
J
Jan Kiszka 已提交
476

A
Andreas Färber 已提交
477
    qdev_connect_gpio_out(dev, 0, parent_irq);
J
Jan Kiszka 已提交
478
    for (i = 0 ; i < 8; i++) {
A
Andreas Färber 已提交
479
        irq_set[i] = qdev_get_gpio_in(dev, i);
J
Jan Kiszka 已提交
480 481
    }

A
Andreas Färber 已提交
482
    isa_pic = dev;
J
Jan Kiszka 已提交
483

A
Andreas Färber 已提交
484 485
    isadev = i8259_init_chip(TYPE_I8259, bus, false);
    dev = DEVICE(isadev);
J
Jan Kiszka 已提交
486

A
Andreas Färber 已提交
487
    qdev_connect_gpio_out(dev, 0, irq_set[2]);
J
Jan Kiszka 已提交
488
    for (i = 0 ; i < 8; i++) {
A
Andreas Färber 已提交
489
        irq_set[i + 8] = qdev_get_gpio_in(dev, i);
J
Jan Kiszka 已提交
490 491
    }

A
Andreas Färber 已提交
492
    slave_pic = PIC_COMMON(dev);
J
Jan Kiszka 已提交
493

J
Jan Kiszka 已提交
494 495 496
    return irq_set;
}

497 498
static void i8259_class_init(ObjectClass *klass, void *data)
{
499
    PICClass *k = PIC_CLASS(klass);
500
    DeviceClass *dc = DEVICE_CLASS(klass);
501

502 503
    k->parent_realize = dc->realize;
    dc->realize = pic_realize;
504
    dc->reset = pic_reset;
505 506
}

507
static const TypeInfo i8259_info = {
A
Andreas Färber 已提交
508
    .name       = TYPE_I8259,
509 510
    .instance_size = sizeof(PICCommonState),
    .parent     = TYPE_PIC_COMMON,
511
    .class_init = i8259_class_init,
512
    .class_size = sizeof(PICClass),
J
Jan Kiszka 已提交
513 514
};

A
Andreas Färber 已提交
515
static void pic_register_types(void)
J
Jan Kiszka 已提交
516
{
517
    type_register_static(&i8259_info);
B
bellard 已提交
518
}
519

A
Andreas Färber 已提交
520
type_init(pic_register_types)