/* * 86Box A hypervisor and IBM PC system emulator that specializes in * running old operating systems and software designed for IBM * PC systems and compatibles from 1981 through fairly recent * system designs based on the PCI bus. * * This file is part of the 86Box distribution. * * Implementation of the Intel PIC chip emulation, partially * ported from reenigne's XTCE. * * Authors: Andrew Jenner, * Miran Grca, * * Copyright 2015-2020 Andrew Jenner. * Copyright 2016-2020 Miran Grca. */ #include #include #include #include #include #include #include #define HAVE_STDARG_H #include <86box/86box.h> #include "cpu.h" #include <86box/machine.h> #include <86box/io.h> #include <86box/pci.h> #include <86box/pic.h> #include <86box/timer.h> #include <86box/pit.h> #include <86box/device.h> #include <86box/chipset.h> #include <86box/apm.h> #include <86box/nvr.h> #include <86box/acpi.h> #include <86box/plat_unused.h> #ifdef USE_VFIO # include <86box/vfio.h> #endif enum { STATE_NONE = 0, STATE_ICW2, STATE_ICW3, STATE_ICW4 }; pic_t pic; pic_t pic2; static pc_timer_t pic_timer; static int shadow = 0; static int elcr_enabled = 0; static int tmr_inited = 0; /* Persistent override for machines pairing a 286-or-higher accelerator CPU with a genuine single-PIC XT board (e.g. the Intel Inboard 386/PC on a stock 5150/5160), where is286-driven IMR-write timing would otherwise silently never schedule a pending-IRQ check at all. Set once via pic_set_force_xt_imr_timing() by the owning device at init time. */ static int force_xt_imr_timing = 0; static int pic_pci = 0; static int kbd_latch = 0; static int mouse_latch = 0; static uint8_t ibm5140_diag_value; static uint8_t ibm5140_diag_enabled; static uint8_t ibm5140_diag_lines[8]; static void (*ibm5140_wake)(void); static uint16_t smi_irq_mask = 0x0000; static uint16_t smi_irq_status = 0x0000; static void (*irq_callback)(uint16_t, int, void *); static void *irq_callback_priv; static uint16_t latched_irqs = 0x0000; static int16_t irq_vector_override[8] = { -1, -1, -1, -1, -1, -1, -1, -1 }; /* Lines a device has taken over for a controller of its own: the interrupt controller must not deliver them, whatever the guest writes to its mask register, so those bits are held set here. */ static uint16_t pic_pinned_irqs = 0x0000; static void (*update_pending)(void); /* The level sources on each IRQ line as the I/O APIC's input sees it: a PCI interrupt steered by the chipset's router reaches the 8259 alone (picint_steered), so the line can be up for one and not the other. */ static uint32_t pic_ioapic_lines[16]; #ifdef USE_VFIO /* IRQs delivered since their level was last known to be current. VFIO cannot report a passed-through device deasserting INTx, so such a level is resampled before use. */ static uint16_t pic_acked = 0x0000; static int pic_resampling = 0; static uint8_t pic_level_triggered(pic_t *dev, int irq); /* Have a level resampled before it is delivered. */ void pic_resample_mark(int irq) { pic_acked |= 1 << irq; } static void pic_resample(int irq) { /* Lines dropped by the resample must not re-enter the priority resolver. */ pic_resampling++; vfio_irq_intx_resample(irq); pic_resampling--; } #endif #ifdef ENABLE_PIC_LOG int pic_do_log = ENABLE_PIC_LOG; static void pic_log(const char *fmt, ...) { va_list ap; if (pic_do_log) { va_start(ap, fmt); pclog_ex(fmt, ap); va_end(ap); } } #else # define pic_log(fmt, ...) #endif void pic_set_irq_callback(void (*callback)(uint16_t, int, void *), void *priv) { irq_callback = callback; irq_callback_priv = priv; } void pic_reset_smi_irq_mask(void) { smi_irq_mask = 0x0000; } void pic_set_smi_irq_mask(int irq, int set) { if ((irq >= 0) && (irq <= 15)) { if (set) smi_irq_mask |= (1 << irq); else smi_irq_mask &= ~(1 << irq); } } uint16_t pic_get_smi_irq_status(void) { return smi_irq_status; } void pic_clear_smi_irq_status(int irq) { if ((irq >= 0) && (irq <= 15)) smi_irq_status &= ~(1 << irq); } void pic_elcr_write(uint16_t port, uint8_t val, void *priv) { pic_t *dev = (pic_t *) priv; pic_log("ELCR%i: WRITE %02X\n", port & 1, val); if (port & 1) val &= 0xde; else val &= 0xf8; dev->elcr = val; pic_log("ELCR %i: %c %c %c %c %c %c %c %c\n", port & 1, (val & 1) ? 'L' : 'E', (val & 2) ? 'L' : 'E', (val & 4) ? 'L' : 'E', (val & 8) ? 'L' : 'E', (val & 0x10) ? 'L' : 'E', (val & 0x20) ? 'L' : 'E', (val & 0x40) ? 'L' : 'E', (val & 0x80) ? 'L' : 'E'); } uint8_t pic_elcr_read(UNUSED(uint16_t port), void *priv) { const pic_t *dev = (pic_t *) priv; pic_log("ELCR%i: READ %02X\n", port & 1, dev->elcr); return dev->elcr; } int pic_elcr_get_enabled(void) { return elcr_enabled; } void pic_elcr_set_enabled(int enabled) { elcr_enabled = enabled; } void pic_elcr_io_handler(int set) { io_handler(set, 0x04d0, 0x0001, pic_elcr_read, NULL, NULL, pic_elcr_write, NULL, NULL, &pic); io_handler(set, 0x04d1, 0x0001, pic_elcr_read, NULL, NULL, pic_elcr_write, NULL, NULL, &pic2); } static uint8_t pic_cascade_mode(pic_t *dev) { return !(dev->flags & PIC_IBM5140) && !(dev->icw1 & 2); } static __inline uint8_t pic_slave_on(pic_t *dev, int channel) { pic_log("pic_slave_on(%i): %i, %02X, %02X\n", channel, pic_cascade_mode(dev), dev->icw4 & 0x0c, dev->icw3 & (1 << channel)); return pic_cascade_mode(dev) && (dev->is_master || ((dev->icw4 & 0x0c) == 0x0c)) && (dev->icw3 & (1 << channel)); } static __inline int find_best_interrupt(pic_t *dev) { uint8_t b; uint8_t intr; uint8_t j; int8_t ret = -1; for (uint8_t i = 0; i < 8; i++) { j = (i + dev->priority) & 7; b = 1 << j; if ((dev->isr & b) && !((dev->flags & PIC_IBM5140) && dev->special_mask_mode && (dev->imr & b))) break; else if ((dev->state == 0) && ((dev->irr & ~dev->imr) & b)) { #ifdef USE_VFIO /* Resample a level that was already delivered before raising it again. */ const int irq = j + ((dev == &pic2) ? 8 : 0); if ((pic_acked & (1 << irq)) && pic_level_triggered(dev, j)) { pic_acked &= ~(1 << irq); pic_resample(irq); if (!((dev->irr & ~dev->imr) & b)) continue; } #endif ret = j; break; } } intr = dev->interrupt = (ret == -1) ? 0x17 : ret; if (dev->at && (ret != -1)) { if (dev == &pic2) intr += 8; if (cpu_fast_off_flags & (1u << intr)) cpu_fast_off_advance(); } return ret; } /* The master 8259's INTR output, as INTIN0 of an I/O APIC sees it. */ static int pic_intr_reported; static __inline void pic_intr_report(void) { if (pic.int_pending != pic_intr_reported) { pic_intr_reported = pic.int_pending; ioapic_pic_intr(pic_intr_reported); } } static __inline void pic_update_pending_xt(void) { #ifdef USE_VFIO if (pic_resampling) return; #endif if (!(pic.interrupt & 0x20)) pic.int_pending = (find_best_interrupt(&pic) != -1); if (pic.int_pending && (pic.flags & PIC_IBM5140) && ibm5140_wake) ibm5140_wake(); pic_intr_report(); } /* Only check if PIC 1 frozen, because it should not happen that one is frozen but the other is not. */ static __inline void pic_update_pending_at(void) { #ifdef USE_VFIO if (pic_resampling) return; #endif if (!(pic.interrupt & 0x20)) { pic2.int_pending = (find_best_interrupt(&pic2) != -1); /* The slave's INT output is wired to the master's IR2 (see pic.slaves[2]). ICW3 selects the cascade identity for INTA, not the wired input. Before the slave is initialized, its ICW3 is zero; using it here clears IRQ 0 during the master PIC test. */ if (pic2.int_pending) pic.irr |= 0x04; else pic.irr &= ~0x04; pic.int_pending = (find_best_interrupt(&pic) != -1); pic_intr_report(); } } static void pic_callback(UNUSED(void *priv)) { update_pending(); } void pic_reset(void) { int is_at = IS_AT(machine); int is_zenith = !!machine_has_flags_64(machine, MACHINE_ZENITH); is_at = is_at || (machines[machine].init == machine_xt_xi8088_init); const uint8_t board_flags = pic.flags & PIC_IBM5140; memset(&pic, 0, sizeof(pic_t)); memset(pic_ioapic_lines, 0, sizeof(pic_ioapic_lines)); pic_intr_reported = 0; ioapic_pic_intr(0); /* INTR is inactive after reset, for INTIN0 too */ memset(&pic2, 0, sizeof(pic_t)); /* Controller reset preserves the integrated controller's hardware type. */ pic.flags = board_flags; pic.is_master = 1; pic.interrupt = pic2.interrupt = 0x17; if (is_at) pic.slaves[2] = &pic2; if (tmr_inited) timer_on_auto(&pic_timer, 0.0); memset(&pic_timer, 0x00, sizeof(pc_timer_t)); timer_add(&pic_timer, pic_callback, &pic, 0); tmr_inited = 1; update_pending = is_at ? pic_update_pending_at : pic_update_pending_xt; pic.at = pic2.at = is_at; pic.zenith = pic2.zenith = is_zenith; smi_irq_mask = smi_irq_status = 0x0000; shadow = 0; pic_pci = 0; #ifdef USE_VFIO pic_acked = 0x0000; #endif /* A line a device has taken over stays out of delivery across a reset. */ pic.imr = (uint8_t) pic_pinned_irqs; pic2.imr = (uint8_t) (pic_pinned_irqs >> 8); } void pic_set_shadow(int sh) { shadow = sh; } void pic_set_force_xt_imr_timing(int enable) { force_xt_imr_timing = enable; } int pic_get_pci_flag(void) { return pic_pci; } void pic_set_pci_flag(int pci) { pic_pci = pci; } void pic_set_vector_override(uint8_t irq, int vector) { if (irq < 8) irq_vector_override[irq] = (vector >= 0) ? (vector & 0xff) : -1; } /* Keep a line out of the controller's delivery for good, or hand it back when the device that claimed it goes away. The line is still reported to whoever watches the controller, it is only the delivery that is held off. */ void pic_set_line_pinned(int irq, int pinned) { uint16_t bit; uint8_t b; pic_t *dev; if ((irq < 0) || (irq > 15)) return; dev = (irq < 8) ? &pic : &pic2; bit = (uint16_t) (1u << irq); b = (uint8_t) (1u << (irq & 7)); if (pinned) pic_pinned_irqs |= bit; else pic_pinned_irqs &= (uint16_t) ~bit; if (pic_pinned_irqs & bit) dev->imr |= b; else dev->imr &= (uint8_t) ~b; if (update_pending != NULL) update_pending(); } static uint8_t pic_level_triggered(pic_t *dev, int irq) { if (elcr_enabled) return !!(dev->elcr & (1 << irq)); else return !!(dev->icw1 & 8); } int picint_is_level(int irq) { return pic_level_triggered(((irq > 7) ? &pic2 : &pic), irq & 7); } static void pic_acknowledge(pic_t *dev) { int pic_int = dev->interrupt & 7; int pic_int_num = 1 << pic_int; dev->isr |= pic_int_num; if (!pic_level_triggered(dev, pic_int) || (dev->lines[pic_int] == 0)) dev->irr &= ~pic_int_num; #ifdef USE_VFIO pic_acked |= 1 << (pic_int + ((dev == &pic2) ? 8 : 0)); #endif } #ifdef USE_VFIO /* Resample the delivered levels of one PIC before they become visible. */ static void pic_resample_delivered(pic_t *dev) { const int base = (dev == &pic2) ? 8 : 0; int resampled = 0; for (uint8_t i = 0; i < 8; i++) { if ((pic_acked & (1 << (base + i))) && (dev->irr & (1 << i)) && pic_level_triggered(dev, i)) { pic_acked &= ~(1 << (base + i)); pic_resample(base + i); resampled = 1; } } if (resampled) update_pending(); } #endif /* Find IRQ for non-specific EOI (either by command or automatic) by finding the highest IRQ priority with ISR bit set, that is also not masked if the PIC is in special mask mode. */ static uint8_t pic_non_specific_find(pic_t *dev) { uint8_t j; uint8_t b; uint8_t irq = 0xff; for (uint8_t i = 0; i < 8; i++) { j = (i + dev->priority) & 7; b = (1 << j); if ((dev->isr & b) && (!dev->special_mask_mode || !(dev->imr & b))) { irq = j; break; } } return irq; } /* Do the EOI and rotation, if either is requested, on the given IRQ. */ static void pic_action(pic_t *dev, uint8_t irq, uint8_t eoi, uint8_t rotate) { uint8_t b = (1 << irq); if (irq != 0xff) { if (eoi) { #ifdef UAE /* Bridgeboard: the EOI of the keyboard interrupt tells the Amiga side that the PC has taken the key. */ if ((dev == &pic) && (irq == 1) && (dev->isr & b)) { extern void x86_ack_keyboard(void); x86_ack_keyboard(); } #endif dev->isr &= ~b; #ifdef USE_VFIO /* Nothing samples an edge-triggered input's level, so let VFIO rearm a passed-through device once its interrupt was serviced. */ if (!pic_level_triggered(dev, irq)) pic_resample(irq + ((dev == &pic2) ? 8 : 0)); #endif } if (rotate && !(dev->flags & PIC_IBM5140)) dev->priority = (irq + 1) & 7; update_pending(); } } /* Automatic non-specific EOI. */ static __inline void pic_auto_non_specific_eoi(pic_t *dev) { uint8_t irq; if (dev->icw4 & 2) { irq = pic_non_specific_find(dev); pic_action(dev, irq, 1, dev->auto_eoi_rotate); } } /* Do the PIC command specified by bits 7-5 of the value written to the OCW2 register. */ static void pic_command(pic_t *dev) { uint8_t irq = 0xff; if (dev->ocw2 & 0x60) { /* SL and/or EOI set */ if (dev->ocw2 & 0x40) /* SL set, specific priority level */ irq = (dev->ocw2 & 0x07); else /* SL clear, non-specific priority level (find highest with ISR set) */ irq = pic_non_specific_find(dev); pic_action(dev, irq, dev->ocw2 & 0x20, dev->ocw2 & 0x80); } else /* SL and EOI clear */ dev->auto_eoi_rotate = !!(dev->ocw2 & 0x80); } uint8_t pic_latch_read(UNUSED(uint16_t addr), UNUSED(void *priv)) { uint8_t ret = 0xff; pic_log("pic_latch_read(%i, %i)\n", kbd_latch, mouse_latch); if (kbd_latch && (latched_irqs & 0x0002)) picintc(0x0002); if (mouse_latch && (latched_irqs & 0x1000)) picintc(0x1000); /* Return FF - we just lower IRQ 1 and IRQ 12. */ return ret; } uint8_t pic_read_icw(uint8_t pic_id, uint8_t icw) { pic_t *dev = pic_id ? &pic2 : &pic; uint8_t ret = 0xff; switch (icw) { case 0x00: ret = dev->icw1; break; case 0x01: ret = dev->icw2; break; case 0x02: ret = dev->icw3; break; case 0x03: ret = dev->icw4; break; } return ret; } uint8_t pic_read_ocw(uint8_t pic_id, uint8_t ocw) { pic_t *dev = pic_id ? &pic2 : &pic; uint8_t ret = 0xff; switch (ocw) { case 0x00: ret = dev->ocw2; break; case 0x01: ret = dev->ocw3; break; } return ret; } uint8_t pic_read(uint16_t addr, void *priv) { pic_t *dev = (pic_t *) priv; if (shadow) { /* VIA PIC shadow read */ if (addr & 0x0001) dev->data_bus = ((dev->icw2 & 0xf8) >> 3) << 0; else { dev->data_bus = ((dev->ocw3 & 0x20) >> 5) << 4; dev->data_bus |= ((dev->ocw2 & 0x80) >> 7) << 3; dev->data_bus |= ((dev->icw4 & 0x10) >> 4) << 2; dev->data_bus |= ((dev->icw4 & 0x02) >> 1) << 1; dev->data_bus |= ((dev->icw4 & 0x08) >> 3) << 0; } } else { /* Standard 8259 PIC read */ #ifndef UNDEFINED_READ /* Put the IRR on to the data bus by default until the real PIC is probed. */ dev->data_bus = dev->irr; #endif if ((dev->ocw3 & 0x04) && (!(dev->flags & PIC_IBM5140) || !(addr & 1))) { dev->interrupt &= ~0x20; /* Freeze the interrupt until the poll is over. */ if (dev->flags & PIC_IBM5140) { const int irq = find_best_interrupt(dev); dev->interrupt = irq < 0 ? 0x17 : irq; dev->int_pending = irq >= 0; } if (dev->int_pending) { dev->data_bus = 0x80 | (dev->interrupt & 7); pic_acknowledge(dev); dev->int_pending = 0; update_pending(); } else dev->data_bus = (dev->flags & PIC_IBM5140) ? (dev->interrupt & 7) : 0x00; dev->ocw3 &= ~0x04; } else if (addr & 0x0001) dev->data_bus = dev->imr; else if (dev->ocw3 & 0x02) { if (dev->ocw3 & 0x01) dev->data_bus = dev->isr; else { #ifdef USE_VFIO /* The IRR shows level-triggered line levels, which must be current. */ pic_resample_delivered(dev); #endif #ifdef UNDEFINED_READ dev->data_bus = 0x00; #else dev->data_bus = dev->irr; #endif } } /* If A0 = 0, VIA shadow is disabled, and poll mode is disabled, simply read whatever is currently on the data bus. */ } pic_log("pic_read(%04X) = %02X\n", addr, dev->data_bus); return dev->data_bus; } void pic_write(uint16_t addr, uint8_t val, void *priv) { pic_t *dev = (pic_t *) priv; pic_log("pic_write(%04X, %02X, %08X)\n", addr, val, priv); dev->data_bus = val; if (addr & 0x0001) { switch (dev->state) { case STATE_ICW2: dev->icw2 = val; if (dev->flags & PIC_IBM5140) dev->state = STATE_ICW4; else if (pic_cascade_mode(dev)) dev->state = STATE_ICW3; else dev->state = (dev->icw1 & 1) ? STATE_ICW4 : STATE_NONE; break; case STATE_ICW3: dev->icw3 = val; dev->state = (dev->icw1 & 1) ? STATE_ICW4 : STATE_NONE; break; case STATE_ICW4: dev->icw4 = val; dev->state = STATE_NONE; if (dev->flags & PIC_IBM5140) update_pending(); break; case STATE_NONE: dev->imr = (uint8_t) (val | (uint8_t) ((dev == &pic2) ? (pic_pinned_irqs >> 8) : pic_pinned_irqs)); if ((dev->flags & PIC_IBM5140) || (is286 && !force_xt_imr_timing) || dev->zenith) update_pending(); else if (force_xt_imr_timing) /* Deliberately small but non-zero, unlike the stock formula below (which evaluates to exactly 0.0 and so hits timer_on_auto()'s "else timer_stop()" branch - i.e. it never actually schedules anything). This only affects a forced-XT-PIC-timing scenario opted into via pic_set_force_xt_imr_timing() (e.g. a 286-or-higher accelerator CPU on a genuine single-PIC XT board, like the Intel Inboard 386/PC) - every other machine's existing behavior via the branch below is left completely unchanged. */ timer_on_auto(&pic_timer, 1.0); else timer_on_auto(&pic_timer, .0 * ((10000000.0 * (double) xt_cpu_multi) / (double) cpu_s->rspeed)); break; default: break; } } else { if (val & 0x10) { /* Treat any write with any of the bits 7 to 5 set as invalid if PCI. */ if (pic_pci && (val & 0xe0)) return; dev->icw1 = val; dev->icw2 = dev->icw3 = 0x00; if (!(dev->icw1 & 1)) dev->icw4 = 0x00; dev->ocw2 = dev->ocw3 = 0x00; dev->irr = 0x00; for (uint8_t i = 0; i <= 7; i++) { if (dev->lines[i] > 0 && (!(dev->flags & PIC_IBM5140) || pic_level_triggered(dev, i))) dev->irr |= (1 << i); } dev->imr = dev->isr = 0x00; #ifdef USE_VFIO /* The rebuilt IRR comes from line levels which may be stale. */ pic_acked |= 0xff << ((dev == &pic2) ? 8 : 0); #endif dev->ack_bytes = dev->priority = 0x00; dev->auto_eoi_rotate = dev->special_mask_mode = 0x00; dev->interrupt = 0x17; dev->int_pending = 0x00; dev->state = STATE_ICW2; update_pending(); } else if (val & 0x08) { /* RR is a command strobe: a poll or special-mask command must not * replace the integrated controller's current ISR/IRR selection. */ dev->ocw3 = ((dev->flags & PIC_IBM5140) && !(val & 2)) ? (val & ~3) | (dev->ocw3 & 3) : val; if (dev->ocw3 & 0x04) dev->interrupt |= 0x20; /* Freeze the interrupt until the poll is over. */ if (dev->ocw3 & 0x40) dev->special_mask_mode = !!(dev->ocw3 & 0x20); } else { dev->ocw2 = val; pic_command(dev); } } } void pic_set_pci(void) { for (uint8_t i = 0x0024; i < 0x0040; i += 4) { io_sethandler(i, 0x0002, pic_read, NULL, NULL, pic_write, NULL, NULL, &pic); io_sethandler(i + 0x0080, 0x0002, pic_read, NULL, NULL, pic_write, NULL, NULL, &pic2); } for (uint16_t i = 0x1120; i < 0x1140; i += 4) { io_sethandler(i, 0x0002, pic_read, NULL, NULL, pic_write, NULL, NULL, &pic); io_sethandler(i + 0x0080, 0x0002, pic_read, NULL, NULL, pic_write, NULL, NULL, &pic2); } } void pic_kbd_latch(int enable) { pic_log("PIC keyboard latch now %sabled\n", enable ? "en" : "dis"); if (!!(enable | mouse_latch) != !!(kbd_latch | mouse_latch)) io_handler(!!(enable | mouse_latch), 0x0060, 0x0001, pic_latch_read, NULL, NULL, NULL, NULL, NULL, NULL); kbd_latch = !!enable; if (!enable) picintc(0x0002); } void pic_mouse_latch(int enable) { pic_log("PIC mouse latch now %sabled\n", enable ? "en" : "dis"); if (!!(kbd_latch | enable) != !!(kbd_latch | mouse_latch)) io_handler(!!(kbd_latch | enable), 0x0060, 0x0001, pic_latch_read, NULL, NULL, NULL, NULL, NULL, NULL); mouse_latch = !!enable; if (!enable) picintc(0x1000); } static void pic_reset_hard(void) { /* Full machine initialization must not inherit the previous board type. */ pic.flags &= ~PIC_IBM5140; ibm5140_wake = NULL; pic_reset(); /* Explicitly reset the latches. */ kbd_latch = mouse_latch = 0; latched_irqs = 0x0000; /* The situation is as follows: There is a giant mess when it comes to these latches on real hardware, to the point that there's even boards with board-level latched that get used in place of the latches on the chipset, therefore, I'm just doing this here for the sake of simplicity. */ if (machine_has_flags(machine, MACHINE_PS2_KBC)) { pic_kbd_latch(0x01); pic_mouse_latch(0x01); } else { pic_kbd_latch(0x00); pic_mouse_latch(0x00); } } void pic_toggle_latch(int is_ps2) { pic_kbd_latch(0x00); pic_mouse_latch(0x00); /* Explicitly reset the latches. */ kbd_latch = mouse_latch = 0; latched_irqs = 0x0000; /* The situation is as follows: There is a giant mess when it comes to these latches on real hardware, to the point that there's even boards with board-level latched that get used in place of the latches on the chipset, therefore, I'm just doing this here for the sake of simplicity. */ if (is_ps2) { pic_kbd_latch(0x01); pic_mouse_latch(0x01); } } void pic_init(void) { for (uint8_t irq = 0; irq < 8; irq++) irq_vector_override[irq] = -1; pic_reset_hard(); shadow = 0; io_sethandler(0x0020, 0x0002, pic_read, NULL, NULL, pic_write, NULL, NULL, &pic); } /* The Convertible implements a single-controller subset and exposes its configuration through the board's 63h/72h diagnostic multiplexer. */ void pic_init_ibm5140(void (*wake)(void)) { pic.flags |= PIC_IBM5140; ibm5140_wake = wake; ibm5140_diag_value = ibm5140_diag_enabled = 0; memset(ibm5140_diag_lines, 0, sizeof(ibm5140_diag_lines)); } uint8_t pic_ibm5140_diag_read(uint8_t selector) { if (!(selector & 0x40)) return (pic.icw2 & 0xf8) | (pic.ocw2 & 7); return ((pic.ocw2 & 0x60) << 1) | (pic.special_mask_mode ? 0x10 : 0) | (pic.icw1 & 8) | ((pic.icw4 & 2) << 1) | ((pic.ocw3 & 4) >> 1) | (pic.ocw3 & 1); } static void pic_ibm5140_diag_update(void) { /* IRQ1 comes from the keyboard output latch, not this diagnostic port. * Simulation bit1 drives IRQ0; bit0 is the board's I/O-check NMI input. */ const uint8_t requests = (ibm5140_diag_value & 0xfc) | ((ibm5140_diag_value & 2) >> 1); for (unsigned i = 0; i < 8; i++) { const int asserted = ibm5140_diag_enabled && (requests & (1 << i)); if (!!asserted != ibm5140_diag_lines[i]) picint_common(1 << i, PIC_IRQ_LEVEL, !!asserted, &ibm5140_diag_lines[i]); } } void pic_ibm5140_diag_write(uint8_t value) { ibm5140_diag_value = value; pic_ibm5140_diag_update(); } void pic_ibm5140_diag_enable(int enabled) { ibm5140_diag_enabled = !!enabled; pic_ibm5140_diag_update(); } void pic_handler(int set, uint16_t base, int size) { io_handler(set, base, size, pic_read, NULL, NULL, pic_write, NULL, NULL, &pic); } void pic_init_pcjr(void) { pic_reset_hard(); shadow = 0; pic_handler(1, 0x0020, 0x0008); } void pic2_init(void) { io_sethandler(0x00a0, 0x0002, pic_read, NULL, NULL, pic_write, NULL, NULL, &pic2); pic.slaves[2] = &pic2; } static void picint_lines(uint16_t num, int level, int set, uint8_t *irq_state, int to_ioapic) { int raise; int max = 16; uint8_t b; uint8_t slaves = 0; uint16_t w; pic_t *dev; /* Do this because some emulated cards will, for whatever reason, attempt to raise an IRQ at init when the PIC has not yet been properly initialized. */ if (update_pending == NULL) return; /* Make sure to ignore all slave IRQ's, and in case of AT+, translate IRQ 2 to IRQ 9. */ for (uint8_t i = 0; i < 8; i++) { b = (uint8_t) (1 << i); raise = num & b; if (pic.icw3 & b) { slaves++; if (raise) { num &= ~b; if (pic.at && (i == 2)) num |= (1 << 9); } } } uint16_t lines = level ? 0x0000 : num; /* What the I/O APIC's inputs see: for a level source, the wired-OR of every source on the line, so only its changes; an edge source's every raise. */ uint16_t apic_lines = (level || !to_ioapic) ? 0x0000 : num; /* The Tandy VIS firmware leaves the master's ICW3 at 0 and never initializes the slave, but the slave is still wired to the master's IR2 and its IRQ's still reach its IRR - Modular Windows probes IRQ 9 through the IRR's at startup, and the VIS reboots in a loop if the probe fails. */ if (!slaves && (machines[machine].init == machine_at_tandy_vis_init)) slaves = 1; if (!slaves) max = 8; if (!num) { pic_log("Attempting to %s null IRQ\n", set ? "raise" : "lower"); return; } if (level) { dev = &pic; for (uint16_t i = 0; i < max; i++) { if (i == 8) dev = &pic2; b = i & 7; w = 1 << i; if (num & w) { if ((!!*irq_state) != !!set) { set ? dev->lines[b]++ : dev->lines[b]--; if (to_ioapic) { if (set) pic_ioapic_lines[i]++; else if (pic_ioapic_lines[i]) pic_ioapic_lines[i]--; if (pic_ioapic_lines[i] == (!!set)) apic_lines |= w; } } if (!pic_level_triggered(dev, b) || (((!!*irq_state) != !!set) && (dev->lines[b] == (!!set)))) lines |= w; } } if ((!!*irq_state) != !!set) *irq_state = set; num = lines; } if (!slaves) num &= 0x00ff; if (!slaves) apic_lines &= 0x00ff; if (num & 0x0100) acpi_rtc_status = !!set; if (num) { if (irq_callback != NULL) irq_callback(num, set, irq_callback_priv); if (set) { if (smi_irq_mask & num) { smi_raise(); smi_irq_status |= num; } if (num & 0xff00) { /* Latch IRQ 12 if the mouse latch is enabled. */ if ((num & 0x1000) && mouse_latch) latched_irqs |= 0x1000; pic2.irr |= (num >> 8); } if (num & 0x00ff) { /* Latch IRQ 1 if the keyboard latch is enabled. */ if (kbd_latch && (num & 0x0002)) latched_irqs |= 0x0002; pic.irr |= (num & 0x00ff); } } else { smi_irq_status &= ~num; if (num & 0xff00) { /* Unlatch IRQ 12 if the mouse latch is enabled. */ if ((num & 0x1000) && mouse_latch) latched_irqs &= 0xefff; pic2.irr &= ~(num >> 8); } if (num & 0x00ff) { /* Unlatch IRQ 1 if the keyboard latch is enabled. */ if (kbd_latch && (num & 0x0002)) latched_irqs &= 0xfffd; pic.irr &= ~(num & 0x00ff); } } update_pending(); } /* APIC mirror runs after PIC state/latches settle for this request. */ if (apic_lines) ioapic_legacy_irq(apic_lines, level, set); } void picint_common(uint16_t num, int level, int set, uint8_t *irq_state) { picint_lines(num, level, set, irq_state, 1); } /* A PCI interrupt the chipset's router steers onto an IRQ: it is routed "to the IRQ inputs of the interrupt controller" inside the chipset, which "masks the corresponding IRQ signal" (PIIX3 2.2.10), so it is not on the IRQ line the I/O APIC's input is wired to. A board with an I/O APIC wires the PCI interrupts to inputs of their own (ioapic_pirq_intin). */ void picint_steered(uint16_t num, int level, int set, uint8_t *irq_state) { picint_lines(num, level, set, irq_state, 0); } static uint8_t pic_i86_mode(pic_t *dev) { return (dev->flags & PIC_IBM5140) || (dev->icw4 & 1); } static uint8_t pic_irq_ack_read(pic_t *dev, int phase) { uint8_t intr = dev->interrupt & 0x47; uint8_t slave = intr & 0x40; intr &= 0x07; pic_log(" pic_irq_ack_read(%08X, %i)\n", dev, phase); if (dev != NULL) { if (phase == 0) { dev->interrupt |= 0x20; /* Freeze it so it still takes interrupts but they do not override the one currently being processed. */ pic_acknowledge(dev); if (slave) dev->data_bus = pic_irq_ack_read(dev->slaves[intr], phase); else dev->data_bus = pic_i86_mode(dev) ? 0xff : 0xcd; } else if (pic_i86_mode(dev)) { dev->int_pending = 0; if (slave) dev->data_bus = pic_irq_ack_read(dev->slaves[intr], phase); else if ((dev == &pic) && (irq_vector_override[intr] >= 0)) dev->data_bus = irq_vector_override[intr]; else dev->data_bus = intr + (dev->icw2 & 0xf8); pic_auto_non_specific_eoi(dev); } else if (phase == 1) { if (slave) dev->data_bus = pic_irq_ack_read(dev->slaves[intr], phase); else if (dev->icw1 & 0x04) dev->data_bus = (intr << 2) + (dev->icw1 & 0xe0); else dev->data_bus = (intr << 3) + (dev->icw1 & 0xc0); } else if (phase == 2) { dev->int_pending = 0; if (slave) dev->data_bus = pic_irq_ack_read(dev->slaves[intr], phase); else dev->data_bus = dev->icw2; pic_auto_non_specific_eoi(dev); } } return dev->data_bus; } uint8_t pic_irq_ack(void) { uint8_t ret; /* Needed for Xi8088. */ if ((pic.ack_bytes == 0) && pic.int_pending && pic_slave_on(&pic, pic.interrupt)) { if (!pic.slaves[pic.interrupt]->int_pending) { /* If we are on AT, IRQ 2 is pending, and we cannot find a pending IRQ on PIC 2, fatal out. */ fatal("IRQ %i pending on AT without a pending IRQ on PIC %i (normal)\n", pic.interrupt, pic.interrupt); exit(-1); } pic.interrupt |= 0x40; /* Mark slave pending. */ } ret = pic_irq_ack_read(&pic, pic.ack_bytes); pic.ack_bytes = (pic.ack_bytes + 1) % (pic_i86_mode(&pic) ? 2 : 3); if (pic.ack_bytes == 0) { /* Needed for Xi8088. */ if (pic.interrupt & 0x40) pic2.interrupt = 0x17; pic.interrupt = 0x17; update_pending(); } return ret; } int picinterrupt(void) { int ret = -1; #ifdef UAE /* ATonce: the LCA supplies INTR vectors directly, there is no 8259 */ extern int atonce_intr_vector; if (atonce_intr_vector >= 0) { ret = atonce_intr_vector; atonce_intr_vector = -1; pic.int_pending = 0; return ret; } #endif if (pic.int_pending) { if (pic_slave_on(&pic, pic.interrupt)) { if (!pic.slaves[pic.interrupt]->int_pending) { /* If we are on AT, IRQ 2 is pending, and we cannot find a pending IRQ on PIC 2, fatal out. */ // fatal("IRQ %i pending on AT without a pending IRQ on PIC %i (normal)\n", pic.interrupt, pic.interrupt); // exit(-1); /* Error correction mechanism: Do a supurious IRQ 15 (spurious IRQ 7 on PIC 2). */ pic.slaves[pic.interrupt]->int_pending = 1; pic.slaves[pic.interrupt]->interrupt = 0x07; } else pic.interrupt |= 0x40; /* Mark slave pending. */ } } else { /* pic.int_pending was somehow cleared despite the fact we made it here, do a spurious IRQ 7. */ pic.int_pending = 1; pic.interrupt = 0x07; } /* The PS/2 watchdog's gate falls when IRQ 0 is acknowledged. Only the watchdog's: on an EISA board the second timer is the ESC's (or the SiS 85C411's) fail-safe timer, whose gate is tied high, and dropping it on every tick of the system timer stopped the fail-safe counter from ever reaching terminal count -- no fail-safe NMI, and every EISA BIOS that tests for one said so at POST. */ if ((pic.interrupt == 0) && (pit_ps2_watchdog != NULL) && (pit_devs[1].data == pit_ps2_watchdog)) pit_devs[1].set_gate(pit_devs[1].data, 0, 0); /* Two ACK's - do them in a loop to avoid potential compiler misoptimizations. */ for (uint8_t i = 0; i < 2; i++) { ret = pic_irq_ack_read(&pic, pic.ack_bytes); pic.ack_bytes = (pic.ack_bytes + 1) % (pic_i86_mode(&pic) ? 2 : 3); if (pic.ack_bytes == 0) { if (pic.interrupt & 0x40) pic2.interrupt = 0x17; pic.interrupt = 0x17; /* INTR falls with the acknowledge; another request raises it again, a new edge. */ pic.int_pending = 0; pic_intr_report(); update_pending(); } } return ret; }