1 /* 2 * QEMU 8259 interrupt controller emulation 3 * 4 * Copyright (c) 2003-2004 Fabrice Bellard 5 * 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 */ 24 #include "hw/hw.h" 25 #include "hw/i386/pc.h" 26 #include "hw/isa/isa.h" 27 #include "monitor/monitor.h" 28 #include "qemu/timer.h" 29 #include "hw/isa/i8259_internal.h" 30 31 /* debug PIC */ 32 //#define DEBUG_PIC 33 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 41 //#define DEBUG_IRQ_LATENCY 42 //#define DEBUG_IRQ_COUNT 43 44 #define TYPE_I8259 "isa-i8259" 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 */ 52 typedef struct PICClass { 53 PICCommonClass parent_class; 54 55 DeviceRealize parent_realize; 56 } PICClass; 57 58 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT) 59 static int irq_level[16]; 60 #endif 61 #ifdef DEBUG_IRQ_COUNT 62 static uint64_t irq_count[16]; 63 #endif 64 #ifdef DEBUG_IRQ_LATENCY 65 static int64_t irq_time[16]; 66 #endif 67 DeviceState *isa_pic; 68 static PICCommonState *slave_pic; 69 70 /* return the highest priority found in mask (highest = smallest 71 number). Return 8 if no irq */ 72 static int get_priority(PICCommonState *s, int mask) 73 { 74 int priority; 75 76 if (mask == 0) { 77 return 8; 78 } 79 priority = 0; 80 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0) { 81 priority++; 82 } 83 return priority; 84 } 85 86 /* return the pic wanted interrupt. return -1 if none */ 87 static int pic_get_irq(PICCommonState *s) 88 { 89 int mask, cur_priority, priority; 90 91 mask = s->irr & ~s->imr; 92 priority = get_priority(s, mask); 93 if (priority == 8) { 94 return -1; 95 } 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; 100 if (s->special_mask) { 101 mask &= ~s->imr; 102 } 103 if (s->special_fully_nested_mode && s->master) { 104 mask &= ~(1 << 2); 105 } 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 115 /* Update INT output. Must be called every time the output may have changed. */ 116 static void pic_update_irq(PICCommonState *s) 117 { 118 int irq; 119 120 irq = pic_get_irq(s); 121 if (irq >= 0) { 122 DPRINTF("pic%d: imr=%x irr=%x padd=%d\n", 123 s->master ? 0 : 1, s->imr, s->irr, s->priority_add); 124 qemu_irq_raise(s->int_out[0]); 125 } else { 126 qemu_irq_lower(s->int_out[0]); 127 } 128 } 129 130 /* set irq level. If an edge is detected, then the IRR is set to 1 */ 131 static void pic_set_irq(void *opaque, int irq, int level) 132 { 133 PICCommonState *s = opaque; 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) { 153 irq_time[irq_index] = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 154 } 155 #endif 156 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 } 177 pic_update_irq(s); 178 } 179 180 /* acknowledge interrupt 'irq' */ 181 static void pic_intack(PICCommonState *s, int irq) 182 { 183 if (s->auto_eoi) { 184 if (s->rotate_on_auto_eoi) { 185 s->priority_add = (irq + 1) & 7; 186 } 187 } else { 188 s->isr |= (1 << irq); 189 } 190 /* We don't clear a level sensitive interrupt here */ 191 if (!(s->elcr & (1 << irq))) { 192 s->irr &= ~(1 << irq); 193 } 194 pic_update_irq(s); 195 } 196 197 int pic_read_irq(DeviceState *d) 198 { 199 PICCommonState *s = PIC_COMMON(d); 200 int irq, irq2, intno; 201 202 irq = pic_get_irq(s); 203 if (irq >= 0) { 204 if (irq == 2) { 205 irq2 = pic_get_irq(slave_pic); 206 if (irq2 >= 0) { 207 pic_intack(slave_pic, irq2); 208 } else { 209 /* spurious IRQ on slave controller */ 210 irq2 = 7; 211 } 212 intno = slave_pic->irq_base + irq2; 213 } else { 214 intno = s->irq_base + irq; 215 } 216 pic_intack(s, irq); 217 } else { 218 /* spurious IRQ on host controller */ 219 irq = 7; 220 intno = s->irq_base + irq; 221 } 222 223 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_LATENCY) 224 if (irq == 2) { 225 irq = irq2 + 8; 226 } 227 #endif 228 #ifdef DEBUG_IRQ_LATENCY 229 printf("IRQ%d latency=%0.3fus\n", 230 irq, 231 (double)(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - 232 irq_time[irq]) * 1000000.0 / get_ticks_per_sec()); 233 #endif 234 DPRINTF("pic_interrupt: irq=%d\n", irq); 235 return intno; 236 } 237 238 static void pic_init_reset(PICCommonState *s) 239 { 240 pic_reset_common(s); 241 pic_update_irq(s); 242 } 243 244 static void pic_reset(DeviceState *dev) 245 { 246 PICCommonState *s = PIC_COMMON(dev); 247 248 s->elcr = 0; 249 pic_init_reset(s); 250 } 251 252 static void pic_ioport_write(void *opaque, hwaddr addr64, 253 uint64_t val64, unsigned size) 254 { 255 PICCommonState *s = opaque; 256 uint32_t addr = addr64; 257 uint32_t val = val64; 258 int priority, cmd, irq; 259 260 DPRINTF("write: addr=0x%02x val=0x%02x\n", addr, val); 261 if (addr == 0) { 262 if (val & 0x10) { 263 pic_init_reset(s); 264 s->init_state = 1; 265 s->init4 = val & 1; 266 s->single_mode = val & 2; 267 if (val & 0x08) { 268 qemu_log_mask(LOG_UNIMP, 269 "i8259: level sensitive irq not supported\n"); 270 } 271 } else if (val & 0x08) { 272 if (val & 0x04) { 273 s->poll = 1; 274 } 275 if (val & 0x02) { 276 s->read_reg_select = val & 1; 277 } 278 if (val & 0x40) { 279 s->special_mask = (val >> 5) & 1; 280 } 281 } else { 282 cmd = val >> 5; 283 switch (cmd) { 284 case 0: 285 case 4: 286 s->rotate_on_auto_eoi = cmd >> 2; 287 break; 288 case 1: /* end of interrupt */ 289 case 5: 290 priority = get_priority(s, s->isr); 291 if (priority != 8) { 292 irq = (priority + s->priority_add) & 7; 293 s->isr &= ~(1 << irq); 294 if (cmd == 5) { 295 s->priority_add = (irq + 1) & 7; 296 } 297 pic_update_irq(s); 298 } 299 break; 300 case 3: 301 irq = val & 7; 302 s->isr &= ~(1 << irq); 303 pic_update_irq(s); 304 break; 305 case 6: 306 s->priority_add = (val + 1) & 7; 307 pic_update_irq(s); 308 break; 309 case 7: 310 irq = val & 7; 311 s->isr &= ~(1 << irq); 312 s->priority_add = (irq + 1) & 7; 313 pic_update_irq(s); 314 break; 315 default: 316 /* no operation */ 317 break; 318 } 319 } 320 } else { 321 switch (s->init_state) { 322 case 0: 323 /* normal mode */ 324 s->imr = val; 325 pic_update_irq(s); 326 break; 327 case 1: 328 s->irq_base = val & 0xf8; 329 s->init_state = s->single_mode ? (s->init4 ? 3 : 0) : 2; 330 break; 331 case 2: 332 if (s->init4) { 333 s->init_state = 3; 334 } else { 335 s->init_state = 0; 336 } 337 break; 338 case 3: 339 s->special_fully_nested_mode = (val >> 4) & 1; 340 s->auto_eoi = (val >> 1) & 1; 341 s->init_state = 0; 342 break; 343 } 344 } 345 } 346 347 static uint64_t pic_ioport_read(void *opaque, hwaddr addr, 348 unsigned size) 349 { 350 PICCommonState *s = opaque; 351 int ret; 352 353 if (s->poll) { 354 ret = pic_get_irq(s); 355 if (ret >= 0) { 356 pic_intack(s, ret); 357 ret |= 0x80; 358 } else { 359 ret = 0; 360 } 361 s->poll = 0; 362 } else { 363 if (addr == 0) { 364 if (s->read_reg_select) { 365 ret = s->isr; 366 } else { 367 ret = s->irr; 368 } 369 } else { 370 ret = s->imr; 371 } 372 } 373 DPRINTF("read: addr=0x%02" HWADDR_PRIx " val=0x%02x\n", addr, ret); 374 return ret; 375 } 376 377 int pic_get_output(DeviceState *d) 378 { 379 PICCommonState *s = PIC_COMMON(d); 380 381 return (pic_get_irq(s) >= 0); 382 } 383 384 static void elcr_ioport_write(void *opaque, hwaddr addr, 385 uint64_t val, unsigned size) 386 { 387 PICCommonState *s = opaque; 388 s->elcr = val & s->elcr_mask; 389 } 390 391 static uint64_t elcr_ioport_read(void *opaque, hwaddr addr, 392 unsigned size) 393 { 394 PICCommonState *s = opaque; 395 return s->elcr; 396 } 397 398 static const MemoryRegionOps pic_base_ioport_ops = { 399 .read = pic_ioport_read, 400 .write = pic_ioport_write, 401 .impl = { 402 .min_access_size = 1, 403 .max_access_size = 1, 404 }, 405 }; 406 407 static const MemoryRegionOps pic_elcr_ioport_ops = { 408 .read = elcr_ioport_read, 409 .write = elcr_ioport_write, 410 .impl = { 411 .min_access_size = 1, 412 .max_access_size = 1, 413 }, 414 }; 415 416 static void pic_realize(DeviceState *dev, Error **errp) 417 { 418 PICCommonState *s = PIC_COMMON(dev); 419 PICClass *pc = PIC_GET_CLASS(dev); 420 421 memory_region_init_io(&s->base_io, OBJECT(s), &pic_base_ioport_ops, s, 422 "pic", 2); 423 memory_region_init_io(&s->elcr_io, OBJECT(s), &pic_elcr_ioport_ops, s, 424 "elcr", 1); 425 426 qdev_init_gpio_out(dev, s->int_out, ARRAY_SIZE(s->int_out)); 427 qdev_init_gpio_in(dev, pic_set_irq, 8); 428 429 pc->parent_realize(dev, errp); 430 } 431 432 void hmp_info_pic(Monitor *mon, const QDict *qdict) 433 { 434 int i; 435 PICCommonState *s; 436 437 if (!isa_pic) { 438 return; 439 } 440 for (i = 0; i < 2; i++) { 441 s = i == 0 ? PIC_COMMON(isa_pic) : slave_pic; 442 monitor_printf(mon, "pic%d: irr=%02x imr=%02x isr=%02x hprio=%d " 443 "irq_base=%02x rr_sel=%d elcr=%02x fnm=%d\n", 444 i, s->irr, s->imr, s->isr, s->priority_add, 445 s->irq_base, s->read_reg_select, s->elcr, 446 s->special_fully_nested_mode); 447 } 448 } 449 450 void hmp_info_irq(Monitor *mon, const QDict *qdict) 451 { 452 #ifndef DEBUG_IRQ_COUNT 453 monitor_printf(mon, "irq statistic code not compiled.\n"); 454 #else 455 int i; 456 int64_t count; 457 458 monitor_printf(mon, "IRQ statistics:\n"); 459 for (i = 0; i < 16; i++) { 460 count = irq_count[i]; 461 if (count > 0) { 462 monitor_printf(mon, "%2d: %" PRId64 "\n", i, count); 463 } 464 } 465 #endif 466 } 467 468 qemu_irq *i8259_init(ISABus *bus, qemu_irq parent_irq) 469 { 470 qemu_irq *irq_set; 471 DeviceState *dev; 472 ISADevice *isadev; 473 int i; 474 475 irq_set = g_new0(qemu_irq, ISA_NUM_IRQS); 476 477 isadev = i8259_init_chip(TYPE_I8259, bus, true); 478 dev = DEVICE(isadev); 479 480 qdev_connect_gpio_out(dev, 0, parent_irq); 481 for (i = 0 ; i < 8; i++) { 482 irq_set[i] = qdev_get_gpio_in(dev, i); 483 } 484 485 isa_pic = dev; 486 487 isadev = i8259_init_chip(TYPE_I8259, bus, false); 488 dev = DEVICE(isadev); 489 490 qdev_connect_gpio_out(dev, 0, irq_set[2]); 491 for (i = 0 ; i < 8; i++) { 492 irq_set[i + 8] = qdev_get_gpio_in(dev, i); 493 } 494 495 slave_pic = PIC_COMMON(dev); 496 497 return irq_set; 498 } 499 500 static void i8259_class_init(ObjectClass *klass, void *data) 501 { 502 PICClass *k = PIC_CLASS(klass); 503 DeviceClass *dc = DEVICE_CLASS(klass); 504 505 k->parent_realize = dc->realize; 506 dc->realize = pic_realize; 507 dc->reset = pic_reset; 508 } 509 510 static const TypeInfo i8259_info = { 511 .name = TYPE_I8259, 512 .instance_size = sizeof(PICCommonState), 513 .parent = TYPE_PIC_COMMON, 514 .class_init = i8259_class_init, 515 .class_size = sizeof(PICClass), 516 }; 517 518 static void pic_register_types(void) 519 { 520 type_register_static(&i8259_info); 521 } 522 523 type_init(pic_register_types) 524