]> git.proxmox.com Git - qemu.git/blame - hw/intc/i8259.c
qdev: Drop misleading qdev_free() function
[qemu.git] / hw / intc / i8259.c
CommitLineData
80cabfad
FB
1/*
2 * QEMU 8259 interrupt controller emulation
5fafdf24 3 *
80cabfad 4 * Copyright (c) 2003-2004 Fabrice Bellard
5fafdf24 5 *
80cabfad
FB
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
83c9f4ca 24#include "hw/hw.h"
0d09e41a
PB
25#include "hw/i386/pc.h"
26#include "hw/isa/isa.h"
83c9089e 27#include "monitor/monitor.h"
1de7afc9 28#include "qemu/timer.h"
0d09e41a 29#include "hw/isa/i8259_internal.h"
80cabfad
FB
30
31/* debug PIC */
32//#define DEBUG_PIC
33
8ac02ff8
BS
34#ifdef DEBUG_PIC
35#define DPRINTF(fmt, ...) \
36 do { printf("pic: " fmt , ## __VA_ARGS__); } while (0)
37#else
38#define DPRINTF(fmt, ...)
39#endif
40
b41a2cd1 41//#define DEBUG_IRQ_LATENCY
4a0fb71e 42//#define DEBUG_IRQ_COUNT
b41a2cd1 43
d1eebf4e 44#define TYPE_I8259 "isa-i8259"
d2628b7d
AF
45#define PIC_CLASS(class) OBJECT_CLASS_CHECK(PICClass, (class), TYPE_I8259)
46#define PIC_GET_CLASS(obj) OBJECT_GET_CLASS(PICClass, (obj), TYPE_I8259)
47
48/**
49 * PICClass:
50 * @parent_realize: The parent's realizefn.
51 */
52typedef struct PICClass {
53 PICCommonClass parent_class;
54
55 DeviceRealize parent_realize;
56} PICClass;
d1eebf4e 57
81a02f93 58#if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
4a0fb71e
FB
59static int irq_level[16];
60#endif
61#ifdef DEBUG_IRQ_COUNT
62static uint64_t irq_count[16];
63#endif
747c70af
JK
64#ifdef DEBUG_IRQ_LATENCY
65static int64_t irq_time[16];
66#endif
9aa78c42 67DeviceState *isa_pic;
512709f5 68static PICCommonState *slave_pic;
4a0fb71e 69
80cabfad
FB
70/* return the highest priority found in mask (highest = smallest
71 number). Return 8 if no irq */
512709f5 72static int get_priority(PICCommonState *s, int mask)
80cabfad
FB
73{
74 int priority;
81a02f93
JK
75
76 if (mask == 0) {
80cabfad 77 return 8;
81a02f93 78 }
80cabfad 79 priority = 0;
81a02f93 80 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0) {
80cabfad 81 priority++;
81a02f93 82 }
80cabfad
FB
83 return priority;
84}
85
86/* return the pic wanted interrupt. return -1 if none */
512709f5 87static int pic_get_irq(PICCommonState *s)
80cabfad
FB
88{
89 int mask, cur_priority, priority;
90
91 mask = s->irr & ~s->imr;
92 priority = get_priority(s, mask);
81a02f93 93 if (priority == 8) {
80cabfad 94 return -1;
81a02f93 95 }
80cabfad
FB
96 /* compute current priority. If special fully nested mode on the
97 master, the IRQ coming from the slave is not taken into account
98 for the priority computation. */
99 mask = s->isr;
81a02f93 100 if (s->special_mask) {
84678711 101 mask &= ~s->imr;
81a02f93 102 }
25985396 103 if (s->special_fully_nested_mode && s->master) {
80cabfad 104 mask &= ~(1 << 2);
25985396 105 }
80cabfad
FB
106 cur_priority = get_priority(s, mask);
107 if (priority < cur_priority) {
108 /* higher priority found: an irq should be generated */
109 return (priority + s->priority_add) & 7;
110 } else {
111 return -1;
112 }
113}
114
b76750c1 115/* Update INT output. Must be called every time the output may have changed. */
512709f5 116static void pic_update_irq(PICCommonState *s)
80cabfad 117{
b76750c1 118 int irq;
80cabfad 119
b76750c1 120 irq = pic_get_irq(s);
80cabfad 121 if (irq >= 0) {
b76750c1 122 DPRINTF("pic%d: imr=%x irr=%x padd=%d\n",
25985396 123 s->master ? 0 : 1, s->imr, s->irr, s->priority_add);
747c70af 124 qemu_irq_raise(s->int_out[0]);
d96e1737 125 } else {
747c70af 126 qemu_irq_lower(s->int_out[0]);
4de9b249 127 }
80cabfad
FB
128}
129
62026017 130/* set irq level. If an edge is detected, then the IRR is set to 1 */
747c70af 131static void pic_set_irq(void *opaque, int irq, int level)
62026017 132{
512709f5 133 PICCommonState *s = opaque;
747c70af
JK
134 int mask = 1 << irq;
135
136#if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT) || \
137 defined(DEBUG_IRQ_LATENCY)
138 int irq_index = s->master ? irq : irq + 8;
139#endif
140#if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
141 if (level != irq_level[irq_index]) {
142 DPRINTF("pic_set_irq: irq=%d level=%d\n", irq_index, level);
143 irq_level[irq_index] = level;
144#ifdef DEBUG_IRQ_COUNT
145 if (level == 1) {
146 irq_count[irq_index]++;
147 }
148#endif
149 }
150#endif
151#ifdef DEBUG_IRQ_LATENCY
152 if (level) {
bc72ad67 153 irq_time[irq_index] = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
747c70af
JK
154 }
155#endif
156
62026017
JK
157 if (s->elcr & mask) {
158 /* level triggered */
159 if (level) {
160 s->irr |= mask;
161 s->last_irr |= mask;
162 } else {
163 s->irr &= ~mask;
164 s->last_irr &= ~mask;
165 }
166 } else {
167 /* edge triggered */
168 if (level) {
169 if ((s->last_irr & mask) == 0) {
170 s->irr |= mask;
171 }
172 s->last_irr |= mask;
173 } else {
174 s->last_irr &= ~mask;
175 }
176 }
b76750c1 177 pic_update_irq(s);
62026017
JK
178}
179
80cabfad 180/* acknowledge interrupt 'irq' */
512709f5 181static void pic_intack(PICCommonState *s, int irq)
80cabfad
FB
182{
183 if (s->auto_eoi) {
81a02f93 184 if (s->rotate_on_auto_eoi) {
80cabfad 185 s->priority_add = (irq + 1) & 7;
81a02f93 186 }
80cabfad
FB
187 } else {
188 s->isr |= (1 << irq);
189 }
0ecf89aa 190 /* We don't clear a level sensitive interrupt here */
81a02f93 191 if (!(s->elcr & (1 << irq))) {
0ecf89aa 192 s->irr &= ~(1 << irq);
81a02f93 193 }
b76750c1 194 pic_update_irq(s);
80cabfad
FB
195}
196
9aa78c42 197int pic_read_irq(DeviceState *d)
80cabfad 198{
29bb5317 199 PICCommonState *s = PIC_COMMON(d);
80cabfad
FB
200 int irq, irq2, intno;
201
c17725f4 202 irq = pic_get_irq(s);
15aeac38 203 if (irq >= 0) {
15aeac38 204 if (irq == 2) {
c17725f4 205 irq2 = pic_get_irq(slave_pic);
15aeac38 206 if (irq2 >= 0) {
c17725f4 207 pic_intack(slave_pic, irq2);
15aeac38
FB
208 } else {
209 /* spurious IRQ on slave controller */
210 irq2 = 7;
211 }
c17725f4 212 intno = slave_pic->irq_base + irq2;
15aeac38 213 } else {
c17725f4 214 intno = s->irq_base + irq;
15aeac38 215 }
c17725f4 216 pic_intack(s, irq);
15aeac38
FB
217 } else {
218 /* spurious IRQ on host controller */
219 irq = 7;
c17725f4 220 intno = s->irq_base + irq;
15aeac38 221 }
3b46e624 222
78ef2b69
JK
223#if defined(DEBUG_PIC) || defined(DEBUG_IRQ_LATENCY)
224 if (irq == 2) {
225 irq = irq2 + 8;
226 }
227#endif
80cabfad 228#ifdef DEBUG_IRQ_LATENCY
5fafdf24
TS
229 printf("IRQ%d latency=%0.3fus\n",
230 irq,
bc72ad67 231 (double)(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) -
6ee093c9 232 irq_time[irq]) * 1000000.0 / get_ticks_per_sec());
80cabfad 233#endif
8ac02ff8 234 DPRINTF("pic_interrupt: irq=%d\n", irq);
80cabfad
FB
235 return intno;
236}
237
512709f5 238static void pic_init_reset(PICCommonState *s)
d7d02e3c 239{
512709f5 240 pic_reset_common(s);
b76750c1 241 pic_update_irq(s);
d7d02e3c
FB
242}
243
747c70af 244static void pic_reset(DeviceState *dev)
86fbf97c 245{
29bb5317 246 PICCommonState *s = PIC_COMMON(dev);
86fbf97c 247
86fbf97c 248 s->elcr = 0;
aa24822b 249 pic_init_reset(s);
86fbf97c
JK
250}
251
a8170e5e 252static void pic_ioport_write(void *opaque, hwaddr addr64,
098d314a 253 uint64_t val64, unsigned size)
80cabfad 254{
512709f5 255 PICCommonState *s = opaque;
098d314a
RH
256 uint32_t addr = addr64;
257 uint32_t val = val64;
d7d02e3c 258 int priority, cmd, irq;
80cabfad 259
8ac02ff8 260 DPRINTF("write: addr=0x%02x val=0x%02x\n", addr, val);
80cabfad
FB
261 if (addr == 0) {
262 if (val & 0x10) {
86fbf97c 263 pic_init_reset(s);
80cabfad
FB
264 s->init_state = 1;
265 s->init4 = val & 1;
2053152b 266 s->single_mode = val & 2;
81a02f93 267 if (val & 0x08) {
80cabfad 268 hw_error("level sensitive irq not supported");
81a02f93 269 }
80cabfad 270 } else if (val & 0x08) {
81a02f93 271 if (val & 0x04) {
80cabfad 272 s->poll = 1;
81a02f93
JK
273 }
274 if (val & 0x02) {
80cabfad 275 s->read_reg_select = val & 1;
81a02f93
JK
276 }
277 if (val & 0x40) {
80cabfad 278 s->special_mask = (val >> 5) & 1;
81a02f93 279 }
80cabfad
FB
280 } else {
281 cmd = val >> 5;
81a02f93 282 switch (cmd) {
80cabfad
FB
283 case 0:
284 case 4:
285 s->rotate_on_auto_eoi = cmd >> 2;
286 break;
287 case 1: /* end of interrupt */
288 case 5:
289 priority = get_priority(s, s->isr);
290 if (priority != 8) {
291 irq = (priority + s->priority_add) & 7;
292 s->isr &= ~(1 << irq);
81a02f93 293 if (cmd == 5) {
80cabfad 294 s->priority_add = (irq + 1) & 7;
81a02f93 295 }
b76750c1 296 pic_update_irq(s);
80cabfad
FB
297 }
298 break;
299 case 3:
300 irq = val & 7;
301 s->isr &= ~(1 << irq);
b76750c1 302 pic_update_irq(s);
80cabfad
FB
303 break;
304 case 6:
305 s->priority_add = (val + 1) & 7;
b76750c1 306 pic_update_irq(s);
80cabfad
FB
307 break;
308 case 7:
309 irq = val & 7;
310 s->isr &= ~(1 << irq);
311 s->priority_add = (irq + 1) & 7;
b76750c1 312 pic_update_irq(s);
80cabfad
FB
313 break;
314 default:
315 /* no operation */
316 break;
317 }
318 }
319 } else {
81a02f93 320 switch (s->init_state) {
80cabfad
FB
321 case 0:
322 /* normal mode */
323 s->imr = val;
b76750c1 324 pic_update_irq(s);
80cabfad
FB
325 break;
326 case 1:
327 s->irq_base = val & 0xf8;
2bb081f7 328 s->init_state = s->single_mode ? (s->init4 ? 3 : 0) : 2;
80cabfad
FB
329 break;
330 case 2:
331 if (s->init4) {
332 s->init_state = 3;
333 } else {
334 s->init_state = 0;
335 }
336 break;
337 case 3:
338 s->special_fully_nested_mode = (val >> 4) & 1;
339 s->auto_eoi = (val >> 1) & 1;
340 s->init_state = 0;
341 break;
342 }
343 }
344}
345
a8170e5e 346static uint64_t pic_ioport_read(void *opaque, hwaddr addr,
098d314a 347 unsigned size)
80cabfad 348{
512709f5 349 PICCommonState *s = opaque;
80cabfad
FB
350 int ret;
351
80cabfad 352 if (s->poll) {
8d484caa
JK
353 ret = pic_get_irq(s);
354 if (ret >= 0) {
355 pic_intack(s, ret);
356 ret |= 0x80;
357 } else {
358 ret = 0;
359 }
80cabfad
FB
360 s->poll = 0;
361 } else {
362 if (addr == 0) {
81a02f93 363 if (s->read_reg_select) {
80cabfad 364 ret = s->isr;
81a02f93 365 } else {
80cabfad 366 ret = s->irr;
81a02f93 367 }
80cabfad
FB
368 } else {
369 ret = s->imr;
370 }
371 }
08406b03 372 DPRINTF("read: addr=0x%02x val=0x%02x\n", addr, ret);
80cabfad
FB
373 return ret;
374}
375
9aa78c42 376int pic_get_output(DeviceState *d)
d96e1737 377{
29bb5317 378 PICCommonState *s = PIC_COMMON(d);
9aa78c42 379
c17725f4 380 return (pic_get_irq(s) >= 0);
d96e1737
JK
381}
382
a8170e5e 383static void elcr_ioport_write(void *opaque, hwaddr addr,
098d314a 384 uint64_t val, unsigned size)
660de336 385{
512709f5 386 PICCommonState *s = opaque;
660de336
FB
387 s->elcr = val & s->elcr_mask;
388}
389
a8170e5e 390static uint64_t elcr_ioport_read(void *opaque, hwaddr addr,
098d314a 391 unsigned size)
660de336 392{
512709f5 393 PICCommonState *s = opaque;
660de336
FB
394 return s->elcr;
395}
396
098d314a
RH
397static const MemoryRegionOps pic_base_ioport_ops = {
398 .read = pic_ioport_read,
399 .write = pic_ioport_write,
400 .impl = {
401 .min_access_size = 1,
402 .max_access_size = 1,
403 },
404};
405
406static const MemoryRegionOps pic_elcr_ioport_ops = {
407 .read = elcr_ioport_read,
408 .write = elcr_ioport_write,
409 .impl = {
410 .min_access_size = 1,
411 .max_access_size = 1,
412 },
413};
414
d2628b7d 415static void pic_realize(DeviceState *dev, Error **err)
b0a21b53 416{
d2628b7d
AF
417 PICCommonState *s = PIC_COMMON(dev);
418 PICClass *pc = PIC_GET_CLASS(dev);
29bb5317 419
1437c94b
PB
420 memory_region_init_io(&s->base_io, OBJECT(s), &pic_base_ioport_ops, s,
421 "pic", 2);
422 memory_region_init_io(&s->elcr_io, OBJECT(s), &pic_elcr_ioport_ops, s,
423 "elcr", 1);
098d314a 424
29bb5317
AF
425 qdev_init_gpio_out(dev, s->int_out, ARRAY_SIZE(s->int_out));
426 qdev_init_gpio_in(dev, pic_set_irq, 8);
d2628b7d
AF
427
428 pc->parent_realize(dev, err);
b0a21b53
FB
429}
430
84f2d0ea 431void pic_info(Monitor *mon, const QDict *qdict)
ba91cd80
FB
432{
433 int i;
512709f5 434 PICCommonState *s;
3b46e624 435
81a02f93 436 if (!isa_pic) {
3de388f6 437 return;
81a02f93 438 }
c17725f4 439 for (i = 0; i < 2; i++) {
29bb5317 440 s = i == 0 ? PIC_COMMON(isa_pic) : slave_pic;
376253ec
AL
441 monitor_printf(mon, "pic%d: irr=%02x imr=%02x isr=%02x hprio=%d "
442 "irq_base=%02x rr_sel=%d elcr=%02x fnm=%d\n",
443 i, s->irr, s->imr, s->isr, s->priority_add,
444 s->irq_base, s->read_reg_select, s->elcr,
445 s->special_fully_nested_mode);
ba91cd80
FB
446 }
447}
448
84f2d0ea 449void irq_info(Monitor *mon, const QDict *qdict)
4a0fb71e
FB
450{
451#ifndef DEBUG_IRQ_COUNT
376253ec 452 monitor_printf(mon, "irq statistic code not compiled.\n");
4a0fb71e
FB
453#else
454 int i;
455 int64_t count;
456
376253ec 457 monitor_printf(mon, "IRQ statistics:\n");
4a0fb71e
FB
458 for (i = 0; i < 16; i++) {
459 count = irq_count[i];
81a02f93 460 if (count > 0) {
376253ec 461 monitor_printf(mon, "%2d: %" PRId64 "\n", i, count);
81a02f93 462 }
4a0fb71e
FB
463 }
464#endif
465}
ba91cd80 466
48a18b3c 467qemu_irq *i8259_init(ISABus *bus, qemu_irq parent_irq)
80cabfad 468{
747c70af 469 qemu_irq *irq_set;
d1eebf4e
AF
470 DeviceState *dev;
471 ISADevice *isadev;
747c70af 472 int i;
c17725f4 473
747c70af 474 irq_set = g_malloc(ISA_NUM_IRQS * sizeof(qemu_irq));
c17725f4 475
d1eebf4e
AF
476 isadev = i8259_init_chip(TYPE_I8259, bus, true);
477 dev = DEVICE(isadev);
c17725f4 478
d1eebf4e 479 qdev_connect_gpio_out(dev, 0, parent_irq);
747c70af 480 for (i = 0 ; i < 8; i++) {
d1eebf4e 481 irq_set[i] = qdev_get_gpio_in(dev, i);
747c70af
JK
482 }
483
d1eebf4e 484 isa_pic = dev;
747c70af 485
d1eebf4e
AF
486 isadev = i8259_init_chip(TYPE_I8259, bus, false);
487 dev = DEVICE(isadev);
747c70af 488
d1eebf4e 489 qdev_connect_gpio_out(dev, 0, irq_set[2]);
747c70af 490 for (i = 0 ; i < 8; i++) {
d1eebf4e 491 irq_set[i + 8] = qdev_get_gpio_in(dev, i);
747c70af
JK
492 }
493
29bb5317 494 slave_pic = PIC_COMMON(dev);
c17725f4 495
747c70af
JK
496 return irq_set;
497}
498
8f04ee08
AL
499static void i8259_class_init(ObjectClass *klass, void *data)
500{
d2628b7d 501 PICClass *k = PIC_CLASS(klass);
39bffca2 502 DeviceClass *dc = DEVICE_CLASS(klass);
8f04ee08 503
d2628b7d
AF
504 k->parent_realize = dc->realize;
505 dc->realize = pic_realize;
39bffca2 506 dc->reset = pic_reset;
8f04ee08
AL
507}
508
8c43a6f0 509static const TypeInfo i8259_info = {
d1eebf4e 510 .name = TYPE_I8259,
39bffca2
AL
511 .instance_size = sizeof(PICCommonState),
512 .parent = TYPE_PIC_COMMON,
8f04ee08 513 .class_init = i8259_class_init,
d2628b7d 514 .class_size = sizeof(PICClass),
747c70af
JK
515};
516
83f7d43a 517static void pic_register_types(void)
747c70af 518{
39bffca2 519 type_register_static(&i8259_info);
80cabfad 520}
512709f5 521
83f7d43a 522type_init(pic_register_types)