i8259.c 12.9 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
pbrook 已提交
24 25 26
#include "hw.h"
#include "pc.h"
#include "isa.h"
27
#include "monitor/monitor.h"
28
#include "qemu/timer.h"
29
#include "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

J
Jan Kiszka 已提交
44
#if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
45 46 47 48 49
static int irq_level[16];
#endif
#ifdef DEBUG_IRQ_COUNT
static uint64_t irq_count[16];
#endif
J
Jan Kiszka 已提交
50 51 52
#ifdef DEBUG_IRQ_LATENCY
static int64_t irq_time[16];
#endif
53
DeviceState *isa_pic;
54
static PICCommonState *slave_pic;
55

B
bellard 已提交
56 57
/* return the highest priority found in mask (highest = smallest
   number). Return 8 if no irq */
58
static int get_priority(PICCommonState *s, int mask)
B
bellard 已提交
59 60
{
    int priority;
J
Jan Kiszka 已提交
61 62

    if (mask == 0) {
B
bellard 已提交
63
        return 8;
J
Jan Kiszka 已提交
64
    }
B
bellard 已提交
65
    priority = 0;
J
Jan Kiszka 已提交
66
    while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0) {
B
bellard 已提交
67
        priority++;
J
Jan Kiszka 已提交
68
    }
B
bellard 已提交
69 70 71 72
    return priority;
}

/* return the pic wanted interrupt. return -1 if none */
73
static int pic_get_irq(PICCommonState *s)
B
bellard 已提交
74 75 76 77 78
{
    int mask, cur_priority, priority;

    mask = s->irr & ~s->imr;
    priority = get_priority(s, mask);
J
Jan Kiszka 已提交
79
    if (priority == 8) {
B
bellard 已提交
80
        return -1;
J
Jan Kiszka 已提交
81
    }
B
bellard 已提交
82 83 84 85
    /* 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 已提交
86
    if (s->special_mask) {
87
        mask &= ~s->imr;
J
Jan Kiszka 已提交
88
    }
89
    if (s->special_fully_nested_mode && s->master) {
B
bellard 已提交
90
        mask &= ~(1 << 2);
91
    }
B
bellard 已提交
92 93 94 95 96 97 98 99 100
    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 已提交
101
/* Update INT output. Must be called every time the output may have changed. */
102
static void pic_update_irq(PICCommonState *s)
B
bellard 已提交
103
{
J
Jan Kiszka 已提交
104
    int irq;
B
bellard 已提交
105

J
Jan Kiszka 已提交
106
    irq = pic_get_irq(s);
B
bellard 已提交
107
    if (irq >= 0) {
J
Jan Kiszka 已提交
108
        DPRINTF("pic%d: imr=%x irr=%x padd=%d\n",
109
                s->master ? 0 : 1, s->imr, s->irr, s->priority_add);
J
Jan Kiszka 已提交
110
        qemu_irq_raise(s->int_out[0]);
111
    } else {
J
Jan Kiszka 已提交
112
        qemu_irq_lower(s->int_out[0]);
113
    }
B
bellard 已提交
114 115
}

116
/* set irq level. If an edge is detected, then the IRR is set to 1 */
J
Jan Kiszka 已提交
117
static void pic_set_irq(void *opaque, int irq, int level)
118
{
119
    PICCommonState *s = opaque;
J
Jan Kiszka 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    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

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    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 已提交
163
    pic_update_irq(s);
164 165
}

B
bellard 已提交
166
/* acknowledge interrupt 'irq' */
167
static void pic_intack(PICCommonState *s, int irq)
B
bellard 已提交
168 169
{
    if (s->auto_eoi) {
J
Jan Kiszka 已提交
170
        if (s->rotate_on_auto_eoi) {
B
bellard 已提交
171
            s->priority_add = (irq + 1) & 7;
J
Jan Kiszka 已提交
172
        }
B
bellard 已提交
173 174 175
    } else {
        s->isr |= (1 << irq);
    }
176
    /* We don't clear a level sensitive interrupt here */
J
Jan Kiszka 已提交
177
    if (!(s->elcr & (1 << irq))) {
178
        s->irr &= ~(1 << irq);
J
Jan Kiszka 已提交
179
    }
J
Jan Kiszka 已提交
180
    pic_update_irq(s);
B
bellard 已提交
181 182
}

183
int pic_read_irq(DeviceState *d)
B
bellard 已提交
184
{
185
    PICCommonState *s = DO_UPCAST(PICCommonState, dev.qdev, d);
B
bellard 已提交
186 187
    int irq, irq2, intno;

J
Jan Kiszka 已提交
188
    irq = pic_get_irq(s);
189 190
    if (irq >= 0) {
        if (irq == 2) {
J
Jan Kiszka 已提交
191
            irq2 = pic_get_irq(slave_pic);
192
            if (irq2 >= 0) {
J
Jan Kiszka 已提交
193
                pic_intack(slave_pic, irq2);
194 195 196 197
            } else {
                /* spurious IRQ on slave controller */
                irq2 = 7;
            }
J
Jan Kiszka 已提交
198
            intno = slave_pic->irq_base + irq2;
199
        } else {
J
Jan Kiszka 已提交
200
            intno = s->irq_base + irq;
201
        }
J
Jan Kiszka 已提交
202
        pic_intack(s, irq);
203 204 205
    } else {
        /* spurious IRQ on host controller */
        irq = 7;
J
Jan Kiszka 已提交
206
        intno = s->irq_base + irq;
207
    }
208

J
Jan Kiszka 已提交
209 210 211 212 213
#if defined(DEBUG_PIC) || defined(DEBUG_IRQ_LATENCY)
    if (irq == 2) {
        irq = irq2 + 8;
    }
#endif
B
bellard 已提交
214
#ifdef DEBUG_IRQ_LATENCY
215 216
    printf("IRQ%d latency=%0.3fus\n",
           irq,
217
           (double)(qemu_get_clock_ns(vm_clock) -
218
                    irq_time[irq]) * 1000000.0 / get_ticks_per_sec());
B
bellard 已提交
219
#endif
B
Blue Swirl 已提交
220
    DPRINTF("pic_interrupt: irq=%d\n", irq);
B
bellard 已提交
221 222 223
    return intno;
}

224
static void pic_init_reset(PICCommonState *s)
B
bellard 已提交
225
{
226
    pic_reset_common(s);
J
Jan Kiszka 已提交
227
    pic_update_irq(s);
B
bellard 已提交
228 229
}

J
Jan Kiszka 已提交
230
static void pic_reset(DeviceState *dev)
J
Jan Kiszka 已提交
231
{
232
    PICCommonState *s = DO_UPCAST(PICCommonState, dev.qdev, dev);
J
Jan Kiszka 已提交
233 234

    s->elcr = 0;
235
    pic_init_reset(s);
J
Jan Kiszka 已提交
236 237
}

A
Avi Kivity 已提交
238
static void pic_ioport_write(void *opaque, hwaddr addr64,
239
                             uint64_t val64, unsigned size)
B
bellard 已提交
240
{
241
    PICCommonState *s = opaque;
242 243
    uint32_t addr = addr64;
    uint32_t val = val64;
B
bellard 已提交
244
    int priority, cmd, irq;
B
bellard 已提交
245

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

    if (s->poll) {
J
Jan Kiszka 已提交
339 340 341 342 343 344 345
        ret = pic_get_irq(s);
        if (ret >= 0) {
            pic_intack(s, ret);
            ret |= 0x80;
        } else {
            ret = 0;
        }
B
bellard 已提交
346 347 348
        s->poll = 0;
    } else {
        if (addr == 0) {
J
Jan Kiszka 已提交
349
            if (s->read_reg_select) {
B
bellard 已提交
350
                ret = s->isr;
J
Jan Kiszka 已提交
351
            } else {
B
bellard 已提交
352
                ret = s->irr;
J
Jan Kiszka 已提交
353
            }
B
bellard 已提交
354 355 356 357
        } else {
            ret = s->imr;
        }
    }
358
    DPRINTF("read: addr=0x%02x val=0x%02x\n", addr, ret);
B
bellard 已提交
359 360 361
    return ret;
}

362
int pic_get_output(DeviceState *d)
363
{
364
    PICCommonState *s = DO_UPCAST(PICCommonState, dev.qdev, d);
365

J
Jan Kiszka 已提交
366
    return (pic_get_irq(s) >= 0);
367 368
}

A
Avi Kivity 已提交
369
static void elcr_ioport_write(void *opaque, hwaddr addr,
370
                              uint64_t val, unsigned size)
B
bellard 已提交
371
{
372
    PICCommonState *s = opaque;
B
bellard 已提交
373 374 375
    s->elcr = val & s->elcr_mask;
}

A
Avi Kivity 已提交
376
static uint64_t elcr_ioport_read(void *opaque, hwaddr addr,
377
                                 unsigned size)
B
bellard 已提交
378
{
379
    PICCommonState *s = opaque;
B
bellard 已提交
380 381 382
    return s->elcr;
}

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
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,
    },
};

401
static void pic_init(PICCommonState *s)
B
bellard 已提交
402
{
403 404 405
    memory_region_init_io(&s->base_io, &pic_base_ioport_ops, s, "pic", 2);
    memory_region_init_io(&s->elcr_io, &pic_elcr_ioport_ops, s, "elcr", 1);

406 407
    qdev_init_gpio_out(&s->dev.qdev, s->int_out, ARRAY_SIZE(s->int_out));
    qdev_init_gpio_in(&s->dev.qdev, pic_set_irq, 8);
B
bellard 已提交
408 409
}

410
void pic_info(Monitor *mon, const QDict *qdict)
B
bellard 已提交
411 412
{
    int i;
413
    PICCommonState *s;
414

J
Jan Kiszka 已提交
415
    if (!isa_pic) {
B
bellard 已提交
416
        return;
J
Jan Kiszka 已提交
417
    }
J
Jan Kiszka 已提交
418
    for (i = 0; i < 2; i++) {
419
        s = i == 0 ? DO_UPCAST(PICCommonState, dev.qdev, isa_pic) : slave_pic;
A
aliguori 已提交
420 421 422 423 424
        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 已提交
425 426 427
    }
}

428
void irq_info(Monitor *mon, const QDict *qdict)
429 430
{
#ifndef DEBUG_IRQ_COUNT
A
aliguori 已提交
431
    monitor_printf(mon, "irq statistic code not compiled.\n");
432 433 434 435
#else
    int i;
    int64_t count;

A
aliguori 已提交
436
    monitor_printf(mon, "IRQ statistics:\n");
437 438
    for (i = 0; i < 16; i++) {
        count = irq_count[i];
J
Jan Kiszka 已提交
439
        if (count > 0) {
A
aliguori 已提交
440
            monitor_printf(mon, "%2d: %" PRId64 "\n", i, count);
J
Jan Kiszka 已提交
441
        }
442 443 444
    }
#endif
}
B
bellard 已提交
445

446
qemu_irq *i8259_init(ISABus *bus, qemu_irq parent_irq)
B
bellard 已提交
447
{
J
Jan Kiszka 已提交
448 449 450
    qemu_irq *irq_set;
    ISADevice *dev;
    int i;
J
Jan Kiszka 已提交
451

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

454
    dev = i8259_init_chip("isa-i8259", bus, true);
J
Jan Kiszka 已提交
455

J
Jan Kiszka 已提交
456 457 458 459 460
    qdev_connect_gpio_out(&dev->qdev, 0, parent_irq);
    for (i = 0 ; i < 8; i++) {
        irq_set[i] = qdev_get_gpio_in(&dev->qdev, i);
    }

461
    isa_pic = &dev->qdev;
J
Jan Kiszka 已提交
462

463
    dev = i8259_init_chip("isa-i8259", bus, false);
J
Jan Kiszka 已提交
464 465 466 467 468 469

    qdev_connect_gpio_out(&dev->qdev, 0, irq_set[2]);
    for (i = 0 ; i < 8; i++) {
        irq_set[i + 8] = qdev_get_gpio_in(&dev->qdev, i);
    }

470
    slave_pic = DO_UPCAST(PICCommonState, dev, dev);
J
Jan Kiszka 已提交
471

J
Jan Kiszka 已提交
472 473 474
    return irq_set;
}

475 476 477
static void i8259_class_init(ObjectClass *klass, void *data)
{
    PICCommonClass *k = PIC_COMMON_CLASS(klass);
478
    DeviceClass *dc = DEVICE_CLASS(klass);
479 480

    k->init = pic_init;
481
    dc->reset = pic_reset;
482 483
}

484
static const TypeInfo i8259_info = {
485 486 487
    .name       = "isa-i8259",
    .instance_size = sizeof(PICCommonState),
    .parent     = TYPE_PIC_COMMON,
488
    .class_init = i8259_class_init,
J
Jan Kiszka 已提交
489 490
};

A
Andreas Färber 已提交
491
static void pic_register_types(void)
J
Jan Kiszka 已提交
492
{
493
    type_register_static(&i8259_info);
B
bellard 已提交
494
}
495

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