i8259.c 13.5 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.
 */
P
Peter Maydell 已提交
24
#include "qemu/osdep.h"
25
#include "hw/hw.h"
P
Paolo Bonzini 已提交
26 27
#include "hw/i386/pc.h"
#include "hw/isa/isa.h"
28
#include "monitor/monitor.h"
29
#include "qemu/timer.h"
P
Paolo Bonzini 已提交
30
#include "hw/isa/i8259_internal.h"
B
bellard 已提交
31 32 33 34

/* debug PIC */
//#define DEBUG_PIC

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

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

A
Andreas Färber 已提交
45
#define TYPE_I8259 "isa-i8259"
46 47 48 49 50 51 52 53 54 55 56 57
#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 已提交
58

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

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

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

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

    mask = s->irr & ~s->imr;
    priority = get_priority(s, mask);
J
Jan Kiszka 已提交
94
    if (priority == 8) {
B
bellard 已提交
95
        return -1;
J
Jan Kiszka 已提交
96
    }
B
bellard 已提交
97 98 99 100
    /* 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 已提交
101
    if (s->special_mask) {
102
        mask &= ~s->imr;
J
Jan Kiszka 已提交
103
    }
104
    if (s->special_fully_nested_mode && s->master) {
B
bellard 已提交
105
        mask &= ~(1 << 2);
106
    }
B
bellard 已提交
107 108 109 110 111 112 113 114 115
    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 已提交
116
/* Update INT output. Must be called every time the output may have changed. */
117
static void pic_update_irq(PICCommonState *s)
B
bellard 已提交
118
{
J
Jan Kiszka 已提交
119
    int irq;
B
bellard 已提交
120

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

131
/* set irq level. If an edge is detected, then the IRR is set to 1 */
J
Jan Kiszka 已提交
132
static void pic_set_irq(void *opaque, int irq, int level)
133
{
134
    PICCommonState *s = opaque;
J
Jan Kiszka 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    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) {
154
        irq_time[irq_index] = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
J
Jan Kiszka 已提交
155 156 157
    }
#endif

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    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 已提交
178
    pic_update_irq(s);
179 180
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
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,
    },
};

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

422 423 424 425
    memory_region_init_io(&s->base_io, OBJECT(s), &pic_base_ioport_ops, s,
                          "pic", 2);
    memory_region_init_io(&s->elcr_io, OBJECT(s), &pic_elcr_ioport_ops, s,
                          "elcr", 1);
426

A
Andreas Färber 已提交
427 428
    qdev_init_gpio_out(dev, s->int_out, ARRAY_SIZE(s->int_out));
    qdev_init_gpio_in(dev, pic_set_irq, 8);
429

430
    pc->parent_realize(dev, errp);
B
bellard 已提交
431 432
}

433
void hmp_info_pic(Monitor *mon, const QDict *qdict)
B
bellard 已提交
434 435
{
    int i;
436
    PICCommonState *s;
437

J
Jan Kiszka 已提交
438
    if (!isa_pic) {
B
bellard 已提交
439
        return;
J
Jan Kiszka 已提交
440
    }
J
Jan Kiszka 已提交
441
    for (i = 0; i < 2; i++) {
A
Andreas Färber 已提交
442
        s = i == 0 ? PIC_COMMON(isa_pic) : slave_pic;
A
aliguori 已提交
443 444 445 446 447
        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 已提交
448 449 450
    }
}

451
void hmp_info_irq(Monitor *mon, const QDict *qdict)
452 453
{
#ifndef DEBUG_IRQ_COUNT
A
aliguori 已提交
454
    monitor_printf(mon, "irq statistic code not compiled.\n");
455 456 457 458
#else
    int i;
    int64_t count;

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

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

476
    irq_set = g_new0(qemu_irq, ISA_NUM_IRQS);
J
Jan Kiszka 已提交
477

A
Andreas Färber 已提交
478 479
    isadev = i8259_init_chip(TYPE_I8259, bus, true);
    dev = DEVICE(isadev);
J
Jan Kiszka 已提交
480

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

A
Andreas Färber 已提交
486
    isa_pic = dev;
J
Jan Kiszka 已提交
487

A
Andreas Färber 已提交
488 489
    isadev = i8259_init_chip(TYPE_I8259, bus, false);
    dev = DEVICE(isadev);
J
Jan Kiszka 已提交
490

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

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

J
Jan Kiszka 已提交
498 499 500
    return irq_set;
}

501 502
static void i8259_class_init(ObjectClass *klass, void *data)
{
503
    PICClass *k = PIC_CLASS(klass);
504
    DeviceClass *dc = DEVICE_CLASS(klass);
505

506 507
    k->parent_realize = dc->realize;
    dc->realize = pic_realize;
508
    dc->reset = pic_reset;
509 510
}

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

A
Andreas Färber 已提交
519
static void pic_register_types(void)
J
Jan Kiszka 已提交
520
{
521
    type_register_static(&i8259_info);
B
bellard 已提交
522
}
523

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