The i8259A Programmable Interrupt Controller

The Intel i8259A Programmable Interrupt Controller (PIC), AKA "Legacy PIC", is the IC used as the interrupt controller for the original IBM PC and later variants. Since the PC "AT" model (1983) there were actually two 8259A's used together, one "cascaded" through the other. The functionality was later moved into southbridge chipsets, and has been superceded by the more advanced "IOAPIC", but the 8259A-pair programming interface and functionality remains present in machines even today (2026).

The PIC receives interrupt requests (IRQs) from devices, via IRQ lines into the PIC. It is responsible for prioritising/masking individual requests (from devices) and passing them on to the CPU, which historically was via an interrupt input line on the CPU package (with the interrupt number being supplied to the CPU via the data bus). As part of the interrupt handshake, the PIC also informs the processor of the interrupt number corresponding to the interrupt request by outputting the number to the data bus (the processor looks up the handler vector in a table, using the interrupt number as an index; the specifics depend on the current processor operating mode).

In the PC AT and beyond, as mentioned above, there are two 8259A's which are "cascaded"; one is the "master" and the other is the "slave". Only the master is connected to the CPU's interrupt input; the slave is connected instead to an IRQ input in the master. The master device is configured so that, when it receives an IRQ signal from the slave device, it does not put an interrupt number on the data bus but instead expects the slave to perform that duty. This setup is emulated in modern chipsets, at least until now, though of course there are no longer physical 8259A chips on your motherboard (and, there are quiet signs that hardware manufacturers will eventually remove the legacy PIC functionality, though it's hard to predict when that will happen in earnest).

Basic operation

When an 8259A receives an interrupt request, it raises the interrupt line to the processor and upon receiving an acknowledgement, it pushes the interrupt vector onto the data bus (the processor reads this to find the address of the interrupt handler), and marks the corresponding IRQ line as "in service". In the processor, the interrupt handler routine does whatever processing is required, and then informs the PIC that processing is complete by issuing an "EOI" (end of interrupt) command at which point the PIC marks the IRQ as no longer in service.

An in-service IRQ line normally is not able to be serviced again until the IRQ is no longer marked as in-service (i.e. until an appropriate EOI has been received), although the device may raise an interrupt request again during this time in which case another interrupt will be issued to the processor once the EOI is received.

IRQs are prioritised, and a lower priority IRQ cannot raise an interrupt while a higher priority IRQ is in service, but a higher priority IRQ can raise an interrupt when a lower priority IRQ is in service.

Programming the 8259A PICs

Note: the 8259A is way more complex than it really needs to be, for its use in the PC. Here I'll cover mainly the standard use, and brush over the rest. In any case deviation from standard register settings is less likely to work in new chipsets which (perhaps only partially) reproduce the functional interface.

Overview

The 2 PICs were/are mapped to I/O addresses:

32 IO ports were mapped only to reduce circuitry requirements, in practice only 2 are needed and it is recommended to only use 0x20/0x21 and 0xA0/0xA1 for maximum compatibility. Modern systems do not support use of ports other than 0x20-0x21 and 0xA0-0xA1 to access the PICs.

The 8259A's interrupt request inputs are active high. The ISA bus architecture in early PCs actually used active low IRQ signalling, pulsing the line low briefly before it was raised high again (via a "pull up" resistor). By using the "edge triggered" mode, the 8259 would recognise the rising edge at the trailing end of the pulse as an interrupt request signal.

The 8259A can be configured to use level-triggered interrupt requests, but this was not useful on ISA-based PCs. In EISA systems (produced from 1988 onwards, largely superceded in 1993 by PCI), two additional "edge level control registers" (ELCRs) were located as follows:

These ELCR registers allowed selecting edge-vs-level triggering on an IRQ-by-IRQ basis, allowing compatibility with EISA devices (which use level-triggered interrupts) and later PCI devices (which also use level-triggered interrupts).

The slave 8259A was connected to IRQ lines 8-15, and had no direct connection to the processor interrupt line, instead cascading via master's IRQ2. The master treats the slave as just another interrupt-requesting device, with the main exception being that it allows the slave to push the interrupt vector onto the bus instead of doing so itself.

In the initial configuration as set up by BIOS (for real mode) the base vector will be 0x8 for IRQS 0-7 (the master PIC) and 0x70 for IRQS 8-15. The interrupt number sent to the processor is the base vector plus the IR line (relative to the 8259A; eg. for an interrupt in the slave, on IRQ 9, interrupt 0x71 would be selected, since this is the IR 1 input as far as the individual chip is concerned). When the processor is not running in real mode, the base vector of the master PIC must be re-programmed, to avoid conflicts with processor exceptions (interrupts 0-31).

Standard IRQ (and interrupt) assignments to (legacy) devices are as follows:

[*] IRQ 2 from ISA extension cards was routed to IRQ 9. For software compatibility, the BIOS firmware in the original PC AT would install a handler for interrupt 0x71 which invoked interrupt 0xA.

Registers and commands

The PIC has three registers tracking interrupt status:

IRR
Interrupt Request Register, flags which lines have seen requests that have not yet been raised to the CPU (because a higher priority request is already in service, or they are currently masked, or because the processor is not accepting interrupts).
ISR
In Service Register, flags which lines have an interrupt in service (i.e. the interrupt number has been signalled to the CPU and acknowledged, but no EOI has been received yet). If an interrupt is in service it will not be raised again, and no lower-priorty IRQ will be raised, until an appropriate EOI is received.
IMR
Interrupt Mask Register. A masked interrupt line will not trigger an interrupt on the processor.

An 8259A accepts "ICW"s (Initialisation Command Words) and "OCW"s (Operation Command Words).

ICWs are sent in a sequence and used to set operating mode and parameters and (via ICW2) the base interrupt vector (for 'x86 mode). For example, the base vector can be set to 0x20 and then the interrupts that will be generated to the CPU are 0x20-0x27. The first ICW is sent to the low port (0x20 or 0xA0) and the rest are sent to the high port (0x21 or 0xA1). Technically ICW3 & ICW4 are optional (their presence/absence are signalled in ICW1) but they are necessary to correctly configure for x86 PC systems.

Following are bitwise descriptions for each ICW. Where values are given they are taken from the original PC AT BIOS routines (and in some cases are the only supported value on x86 PCs). The ICWs must be sent in sequence:

ICW1 (sent to low port):

        Bit(s)
         7-5 ----------------- X Don't matter for x86 mode
         4 ------------------- 1  (identifies ICW1 vs OCW2)
         3 ------------------- 0 = edge triggered mode (1 = level triggered)
         2 ------------------- X Doesn't matter for x86 ("call address interval")
         1 ------------------- 0 = cascade mode, ICW3 will be sent
         0 ------------------- 1 = ICW4 will be sent (to select x86 mode)

ICW2 (sent to high port, after ICW1 has been issued):

        Bit(s)
         7-3 ----------------- base vector bits 7-3
         2-0 ----------------- Don't matter for x86 mode

ICW3, for master (sent to high port, after ICW2 issued):

        Bit(s)
         7-3 ----------------- 00000
         2 ------------------- 1 (IRQ2 has slave attached)
         1-9 ----------------- 00

ICW3, for slave (sent to high port, after ICW2 issued):

        Bit(s)
         7-3 ----------------- 00000
         2-0 ----------------- 010 (i.e. value 2, attached via IRQ2 to master)

ICW4 (sent to high port, after ICW3 issued):

        Bit(s)
         7-5 ----------------- 000
         4 ------------------- 0 (normal "fully nested mode"; 1 = "special fully nested mode")
         3-2 ----------------- 0X (non-buffered mode; 10=buffered slave, 11=buffered master)
         1 ------------------- 0 (disable automatic EOI; 1 = enable. See "EOI signalling")
         0 ------------------- 1 (select x86 mode)

Note that after initialisation (IC, all interrupts are unmasked (although interrupts that were pending at initialisation time might not be recognised, due to edge-triggering, until the device raising them is appropriately serviced).

The edge-vs-level trigger option in ICW1 specifies how devices signal interrupts to the PIC. In the PC AT, devices (on-board and plug-in ISA) used edge-triggered signalling. Microchannel architecture (MCA) PCs (the IBM PS/2 line) used level-triggered signalling (these were rare since MCA was not implemented by PC clone manufacturers). See also the later discussion of the ELCR registers, including EISA and PCI details. All modern PCs which include 8259A functionality incorporate ELCRs and so the choice of edge-triggered or level-triggered interrupts in ICW1 is not actually significant (edge-triggered should be considered the default).

Notice that ICW4 selects "non-buffered mode" and does not inform the 8259A whether it should act as master or slave. In this non-buffered mode, the master/slave selection is made by the signal on an external pin which is hardwired appropriately for each 8259A in the PC AT (the same external pin that would be used for the buffer enable signal in buffered mode, which is why this mode shouldn't really be used, although it will likely still work just fine - the line enabling buffer output won't have any effect, which doesn't matter because there's no buffer anyway).

Operation Command Words (OCWs) can be used to set the mask register (mask particular interrupt request lines), signal an interrupt handler has completed (end-of-interrupt, EOI), or specify which register should be read via the low I/O port. Unlike ICWs these do not need to be sent in a sequence.

OCW1 (sent to high port)

        Bit(s)
         0-7 ----------------- set (1)/clear (0) mask bit for corresponding IRQ (i.e. write IMR)

OCW2 (sent to low port)

        Bit(s)
         7-5 ----------------- select operation:
                               000 - disable priority rotation when in automatic EOI mode
                               001 - non-specific EOI
                               010 - no operation
                               011 - specific EOI (IRQ specified in bits 2-0)
                               100 - enable priority rotation when in automatic EOI mode
                               101 - non-specific EOI with priority rotation
                               110 - set lowest priority IRQ (specified in bits 2-0)
                               111 - specific EOI and set lowest priority
         4 ------------------- 0
         3 ------------------- 0
         2-0 ----------------- specify IRQ for operations 011/110/111

(For more details, see notes on EOI signalling and priority below).

OCW3 (sent to low port)

        Bit(s)
         7 ------------------- 0
         6-5 ----------------- set/reset special mask mode
                               00/01 - no special mask mode action
                               10 - reset (disable) special mask mode
                               11 - set special mask mode
         4 ------------------- 0
         3 ------------------- 1 (identifies OCW3)
         2 ------------------- poll action (see "Polling")
                               0 - don't issue "poll" command
                               1 - issue "poll" command
         1-0 ----------------- specifies register to be read via low port reads:
                               10 = IRR, 11 = ISR, 0X = no action

(More information provided in sections on polling and special mask mode below).

Reading IMR, ISR, IRR

The IMR can be read via the odd-numbered port address at any time (it can be written via the same port, as OCW1). The ISR and IRR are multiplexed (for reading) over the even-numbered port and OCW3 (sent to the same port) selects which will be read.

EOI signalling

Once an interrupt has been received by the CPU it is marked as "in service" in the ISR. Lower priority interrupts will not be sent to the processor until the "in service" bit is cleared, by means of an "End-of-interrupt" (EOI) command sent by the processor (OCW2 above). The typically-used "non-specific" EOI (value of 20h sent as OCW2 to low port) will mark the highest-priority currently-in-service interrupt as no longer in service (and so allow that interrupt, and lower-priority interrupts, to be issued to the CPU again). The "specific" EOI by contrast selects a specific interrupt to clear the in-service bit for.

The EOI variants with priority rotation actually rotate interrupt priorities, see the "Interrupt priorities" section below. If any form of priority rotation is used, it may be advisable to use the "specific" EOI command to ensure that the correct ISR bit is cleared. This may also be necessary if using Special Mask Mode (described below) as a non-specific EOI will not clear the in-service mark of an IRQ that is masked while in this mode, according to the Intel documentation (it is not clear what happens instead - likely, the highest-priority non-masked currently in-service IRQ has its in-service mark cleared, but I have not verified this - nor is it clear whether this behaviour is indeed specific to Special Mask Mode).

An "automatic EOI" mode can be enabled (via ICW4) and effectively dispenses with the ISR. It could perhaps be used for slightly more efficient interrupt handling, but in this case it would be advisable to ensure that the processor keeps interrupts disabled for the entire duration of the interrupt handler, to avoid a flurry of interrupts coming in from a single device (particularly if level-triggered) and potentially overflowing the stack. In general, automatic EOI for level-triggered IRQs is going to be awkward, so I would recommend avoiding this mode.

An interrupt on the slave PIC will require an EOI to be issued both to the slave and the master (for the cascade line, IRQ2). Note that in the usual mode, the slave can have only interrupt in service at a time, and a higher-priority slave interrupt cannot interrupt the service routine of a lower-priority slave interrupt, since the ISR bit in the master is already set for the cascade line (and is not cleared until the EOI is sent to the master). The "special fully nested mode" (which can be enabled in the master via ICW4) overcomes this issue by allowing the slave(s) to generate further interrupts while already marked as in-service, but note that this complicates the EOI logic (software needs ensure that the EOI is only sent to the master once no interrupts from the slave are in-service).

Interrupt priorities

After initialisation, the highest priority interrupt is #0 and each interrupt through to 7 decreases priority compared to the previous so that #7 has the lowest priority. Priority can be rotated by the use of EOI-with-rotation commands (these shift priority so that the interrupt no longer being serviced becomes the lowest priority and each interrupt line prior is higher priority than the one succeeding it. For example, if IRQ 2 is the lowest priority then IRQ 1 is the 2nd lowest, IRQ 0 is the 3rd lowest, IRQ 7 is the 4th lowst, and so on, with IRQ 3 being the highest priority). The lowest priority can also be rotated to a specific interrupt without issuing an EOI via operation 110b in OCW2. Priority rotation has not (as far as I know) generally been employed in PC OSes, and is perhaps not advisable; it's usually desirable to keep the timer interrupt at highest priority, for example.

Spurious interrupts

Due to the signal handshake between PIC(s) and CPU it is possible for an interrupt to be acknowledged by the CPU but de-asserted by the device before the PIC has transmitted the interrupt vector; if this happens the PIC produces a "spurious" interrupt #7 (i.e. the interrupt corresponding to IRQ 7, or IRQ 15 on the slave), which will not set the ISR #7 bit but may occur even if interrupt #7 is already in service. This requires some care in handling and in the sending of EOI (and in the handling of IRQ 7 in general) because an EOI should not be sent to acknowledge a spurious interrupt. Note however that a spurious interrupt generated in the slave PIC will generally require an EOI to be issued to the master (the master is not aware that the interrupt is spurious).

(It might seem impossible for edge-triggered interrupts to be "de-asserted" but in fact the 8259A requires that the level after the rising edge is sustained for a certain period).

To detect a spurious interrupt, the handler for the IRQ 7/15 interrupt can check the ISR in the appropriate 8259A and see if bit #7 is set; if not, the interrupt is spurious. The handler may also need to separately keep track of whether a legitimate IRQ 7 is currently being handled, since if a spurious interrupt arrives while that is the case (and before an appropriate EOI has been issued), bit #7 in the ISR will be set and not clear; This is normally only necessary (unless special fully-nested mode is active) for the master PIC, since the slave cannot normally issue another interrupt while it has one in service (because IRQ 2 in the master will be marked as in-service). It would also be unnecessary to separately track in-service state of IRQ 7 If the handler keeps interrupts disabled at least up until it sends EOI.

Since automatic-EOI mode will not set any bit in IRR when an interrupt is generated, using it may make it difficult or impossible to differentiate spurious interrupts from legitimate interrupts.

Special mask mode

There is a "special mask mode" which can be enabled via OCW3, to allow better software control over prioritising different IRQs. The original Intel datasheet confusingly states only that:

In the special Mask Mode, when a mask bit is set in OCW1, it inhibits further interrupts at that level and enables interrupts from all other levels (lower as well as higher) that are not masked.

What it actually means is that an IRQ that is in-service (as marked in the ISR) and also masked (via the IMR) will not prevent a lower priority IRQ from generating an interrupt. That is, when an IRQ is generated, the normal check for higher-priority IRQs already in service (which would prevent an interrupt) ignores those higher-priority IRQs that are masked (as opposed to normal mode where it would not ignore masked IRQs that were marked as in-service).

The datasheet is not completely correct, in that setting a mask bit via OCW1 does not "enable interrupts" from all lower-priority levels; rather, it will allow generating interrupts via all IRQs that are themselves unmasked, not currently in service, and that do not have a higher-priority IRQ that is both unmasked and currently in-service.

The idea is that on entry to the interrupt handler for some IRQ, the handler can immediately send an OCW1 to set the IMR, masking its own IRQ as well as any other IRQs that the software considers to be lower priority (as opposed to IRQs that are lower priority by the PIC's own priority scheme). If it then re-enables interrupts in the processor, it will be possible for unmasked IRQs (i.e. those that are higher-priority, software-wise) to interrupt the current handler, even though an IRQ that may be higher-priority (according to the PIC) is in-service.

It's probably tricky to use special mask mode together with special fully nested mode. That is, masking (in the master) the cascaded slave presumably prevents it from generating further interrupts even when it already has one in service, negating the effect of SFNM. Keeping it unmasked however makes it take precedence (when it has any interrupt in-service) over any interrupts that the PIC considers lower priority, losing some of the advantage of SMM. One trick that might work is to keep the slave unmasked in the master (but mask interrupts in the slave itself instead) and always send EOI to the master (only) immediately upon handling any interrupt in the slave, immediately after adjusting the masks in both PICs (to enforce the desired priority scheme). With this scheme, the slave having an interrupt in service won't by itself prevent other interrupts in the master, and the slave can have multiple interrupts in-service at the same time (just as in SFNM, except that SFNM need not actually be enabled for this to work).

Polling

A "poll command" can be issued via OCW3. This can be used to check for, and acknowledge, a pending interrupt; the next read from either of the PIC's IO ports will yield the highest priority IRQ currently pending, if any, with bit 7 set to indicate a pending IRQ (which is not masked and has no higher-priority IRQ in service) or clear if there is no pending IRQ. If an IRQ is returned, the corresponding bit in the ISR is set (and presumably the bit in the IRR is cleared, though Intel documentation fails to mention this).

Polling is intended to be used with interrupts disabled in the CPU.

Since "polling" an IRQ sets its ISR bit, it will no longer generate an interrupt if interrupts are later enabled. An EOI must be issued as usual to clear the ISR bit.

Once a value is read, whether an IRQ was pending or not, the poll command is complete.

Interrupt sharing

In theory it is possible, assuming correct electronic design, for ISA devices (using edge-triggered interrupt requests) to share an interrupt line, although interrupt sharing was not part of the ISA standard. When two or more such devices are sharing an interrupt, the interrupt handler must check each device in turn and service it if necessary, since a simultaneous interrupt request from two devices will register as only a single interrupt request in th 8259A. This in turn requires that it be possible to query each device using the shared IRQ and determine if it does in fact require service, which is not supported by all devices.

(A level-triggered scheme such as is used in EISA or PCI, in contrast, only requires one device to be serviced per interrupt, since devices that still require servicing will be holding the interrupt request active and so will re-trigger the interrupt if an EOI is issued before they are serviced).

Disabling the 8259A PIC

A PIC can be effectively disabled by masking all interrupts, i.e. by writing an all-1's mask to the odd-numbered port. This should also prevent spurious interrupts (since they should only occur when the PIC has passed an interrupt request upstream to the CPU).

ELCR (edge-level control registers)

In the EISA system variant of the 8259A, IR lines can be set as edge- or level- triggered on an individual basis. This is done via the 8-bit ELCR registers at ports 0x4D0 (master) and 0x4D1 (slave). Each bit corresponds to an IR line and selects edge-sensitive (0) or level-sensitive (1) operation for the line. Note that certain system board devices associated with certain interrupt request lines require edge-sensitive operation: IRQ0 (system timer), IRQ1 (keyboard controller), IRQ2 (cascade), IRQ8 (Real-time Clock alarm), IRQ13 (FPU error signal). The global edge-vs-level select bit (in ICW1) is ignored in systems with ELCRs.

The ELCR registers from EISA were carried through into at least some (and possibly most or all) chipsets designed around PCI, such as the Intel ICH5 (~2003) and Intel 9-series PCH (~2015). Handling PCI interrupts in the PIC may require programming the corresponding interrupt request line as level-triggered. Documentation for the Intel 9-series PCH indicates that PCI interrupt signals are internally inverted before being routed to the legacy PICs, so they are effectively active high as the 8259 requires; it's likely that other (post-)PCI-era chipsets do the same. (IRQ lines can therefore be shared between PCI devices, but they can not be shared between PCI and ISA devices including legacy system devices).

Note that firmware will (via the ACPI namespace) typically advertise the 8259A PIC pair as a (single) "PNP0000" class device, i.e. AT interrupt controller. If the ELCR is present (which in modern machines it generally should be) this is not really correct; it should be PNP0001 (EISA interrupt controller). If PNP0000 is used, then the ELCR register IO ports (4D0h, 4D1h) may be marked as resources of the PIC, or of a generic system board device (PNP0C02).

Software detection

On ACPI-supporting systems (i.e. all modern PCs), the presence of the 8259A pair is advertised via ACPI tables and in the ACPI device namespace. The namespace device ID indicates the system type which determines whether edge-triggered or level-triggered interrupts (or a combination) are used; see the "ELCR" section above. It's likely that in practice ACPI-supporting systems will support the use of the ELCR registers (if they include 8259A support at all).

On earlier (BIOS-based) systems, at least a single 8259A was present. On standard PC AT clones with at least an 80286 processor, a second 8259A is always available. This can be verified (in case you're concerned your software is running on an early 1980's relic) via the BIOS interrupt 0x15 interface, which can also be used to detect MCA systems:

Set: AH=0xC0
Execute: INT 0x15

Then: If CF clear and AH==0:

    ES:[BX+5] bit 6 - indicates presence of 2nd 8259A
              bit 1 - indicates MCA system (level-triggered interrupts)

An EISA system board can be identified by the presence of the ASCII string "EISA" at memory address FOO0:FFD9h through FOO0:FFDCh. An EISA system includes dual 8259A-compatible PICs which support a combination of level-triggered and edge-triggered interrupts, via the ELCR registers.

If there's any doubt, the presence of the 8259A can be probed by attempting to write and read back different values to the IMR via port 0x21/0xA1.

General notes

Since reinitialising the 8259A unmasks all IRQ lines, it is usually best done with interrupts disabled at the CPU level.

It's advisable to mask IRQs for any devices (any IRQ lines) that are not specifically handled. Unmasking them can only result in useless interrupts. In the case of level-triggered IRQs this can even result in an endless series of interrupts with no delay between them (assuming that the handler sends an EOI).

IRQ 2 (cascade) must be enabled (not masked) in the master 8259A for any interrupts generated in the slave to be serviced.

Unserviced edge-triggered interrupts raised by devices prior to initialisation of the 8259 may be lost. A device that requires servicing before it will generate interrupts may therefore not do so unless it is explicitly serviced. For example, the keyboard controller buffer typically should be polled and cleared to ensure that the controller will generate further interrupts. This would not be a problem with level-triggered interrupts, since the device will continue to assert the interrupt request until it is serviced.

Level-triggered interrupts are probably not latched; for IRQs in level-triggered mode, if a device de-asserts an interrupt request, the corresponding bit in the IRR may automatically clear.

Being masked (via the IMR) does not prevent an interrupt request from setting the corresponding bit in the IRR. It is safe to mask interrupts temporarily without fear of losing an interrupt.

Micro-channel (MCA) based systems (80's-90's, rare even then) use level-triggered interrupts even for system board devices, and require that the 8259s are initialised to use level-triggering. These systems do not support the ELCR registers. Interrupt controllers in MCA systems should have an ACPI ID of PNP0002 (but I do not know if such machines ever supported ACPI).

Modern systems do not have physical 8259A chips but implement their functional interface as part of a chipset. Although I am not aware of specific cases, it is possible that some of less widely-used features (such as special mask mode, and the poll command) may not be implemented correctly or at all. That said, Intel chipsets as late as 2015 seemed to support all features, according to documentation.


Please use comments for suggestions or corrections only.
Comments may be deleted once actioned. Requests for help will be deleted immediately, use an appropriate forum instead.