i8259.cc revision 5686
12023SN/A/*
22023SN/A * Copyright (c) 2004-2005 The Regents of The University of Michigan
32023SN/A * All rights reserved.
42023SN/A *
52023SN/A * Redistribution and use in source and binary forms, with or without
62023SN/A * modification, are permitted provided that the following conditions are
72023SN/A * met: redistributions of source code must retain the above copyright
82023SN/A * notice, this list of conditions and the following disclaimer;
92023SN/A * redistributions in binary form must reproduce the above copyright
102023SN/A * notice, this list of conditions and the following disclaimer in the
112023SN/A * documentation and/or other materials provided with the distribution;
122023SN/A * neither the name of the copyright holders nor the names of its
132023SN/A * contributors may be used to endorse or promote products derived from
142023SN/A * this software without specific prior written permission.
152023SN/A *
162023SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172023SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182023SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192023SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202023SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212023SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222023SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232023SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242023SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252023SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262023SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272023SN/A *
282665Ssaidi@eecs.umich.edu * Authors: Gabe Black
292665Ssaidi@eecs.umich.edu */
302665Ssaidi@eecs.umich.edu
312023SN/A#include "base/bitfield.hh"
324202Sbinkertn@umich.edu#include "dev/x86/i82094aa.hh"
332023SN/A#include "dev/x86/i8259.hh"
344202Sbinkertn@umich.edu
354997Sgblack@eecs.umich.eduX86ISA::I8259::I8259(Params * p) : BasicPioDevice(p), IntDev(this),
364202Sbinkertn@umich.edu                    latency(p->pio_latency), output(p->output),
374997Sgblack@eecs.umich.edu                    mode(p->mode), slave(NULL),
386313Sgblack@eecs.umich.edu                    IRR(0), ISR(0), IMR(0),
394997Sgblack@eecs.umich.edu                    readIRR(true), initControlWord(0)
406327Sgblack@eecs.umich.edu{
414202Sbinkertn@umich.edu    if (output) {
424997Sgblack@eecs.umich.edu        I8259 * master;
434826Ssaidi@eecs.umich.edu        master = dynamic_cast<I8259 *>(output->getDevice());
442023SN/A        if (master)
454997Sgblack@eecs.umich.edu            master->setSlave(this);
464997Sgblack@eecs.umich.edu        I82094AA * ioApic;
474202Sbinkertn@umich.edu        ioApic = dynamic_cast<I82094AA *>(output->getDevice());
485647Sgblack@eecs.umich.edu        if (ioApic)
494486Sbinkertn@umich.edu            ioApic->setExtIntPic(this);
504486Sbinkertn@umich.edu    }
514202Sbinkertn@umich.edu    pioSize = 2;
525647Sgblack@eecs.umich.edu}
534202Sbinkertn@umich.edu
544202Sbinkertn@umich.eduTick
554202Sbinkertn@umich.eduX86ISA::I8259::read(PacketPtr pkt)
564202Sbinkertn@umich.edu{
574202Sbinkertn@umich.edu    assert(pkt->getSize() == 1);
582023SN/A    switch(pkt->getAddr() - pioAddr)
594202Sbinkertn@umich.edu    {
604202Sbinkertn@umich.edu      case 0x0:
614202Sbinkertn@umich.edu        if (readIRR) {
622023SN/A            DPRINTF(I8259, "Reading IRR as %#x.\n", IRR);
634202Sbinkertn@umich.edu            pkt->set(IRR);
644202Sbinkertn@umich.edu        } else {
652023SN/A            DPRINTF(I8259, "Reading ISR as %#x.\n", ISR);
664202Sbinkertn@umich.edu            pkt->set(ISR);
674202Sbinkertn@umich.edu        }
682023SN/A        break;
694202Sbinkertn@umich.edu      case 0x1:
704202Sbinkertn@umich.edu        DPRINTF(I8259, "Reading IMR as %#x.\n", IMR);
712023SN/A        pkt->set(IMR);
724202Sbinkertn@umich.edu        break;
734202Sbinkertn@umich.edu    }
744202Sbinkertn@umich.edu    return latency;
754202Sbinkertn@umich.edu}
764202Sbinkertn@umich.edu
774202Sbinkertn@umich.eduTick
78X86ISA::I8259::write(PacketPtr pkt)
79{
80    assert(pkt->getSize() == 1);
81    uint8_t val = pkt->get<uint8_t>();
82    switch (pkt->getAddr() - pioAddr) {
83      case 0x0:
84        if (bits(val, 4)) {
85            DPRINTF(I8259, "Received initialization command word 1.\n");
86            IMR = 0;
87            edgeTriggered = bits(val, 3);
88            DPRINTF(I8259, "%s triggered mode.\n",
89                    edgeTriggered ? "Edge" : "Level");
90            cascadeMode = !bits(val, 1);
91            DPRINTF(I8259, "%s mode.\n",
92                    cascadeMode ? "Cascade" : "Single");
93            expectICW4 = bits(val, 0);
94            initControlWord = 1;
95            DPRINTF(I8259, "Expecting %d more bytes.\n", expectICW4 ? 3 : 2);
96        } else if (bits(val, 4, 3) == 0) {
97            DPRINTF(I8259, "Received operation command word 2.\n");
98            switch (bits(val, 7, 5)) {
99              case 0x0:
100                DPRINTF(I8259,
101                        "Subcommand: Rotate in auto-EOI mode (clear).\n");
102                break;
103              case 0x1:
104                DPRINTF(I8259, "Subcommand: Nonspecific EOI.\n");
105                break;
106              case 0x2:
107                DPRINTF(I8259, "Subcommand: No operation.\n");
108                break;
109              case 0x3:
110                {
111                    int line = bits(val, 2, 0);
112                    DPRINTF(I8259, "Subcommand: Specific EIO on line %d.\n",
113                            line);
114                    handleEOI(line);
115                }
116                break;
117              case 0x4:
118                DPRINTF(I8259, "Subcommand: Rotate in auto-EOI mode (set).\n");
119                break;
120              case 0x5:
121                DPRINTF(I8259, "Subcommand: Rotate on nonspecific EOI.\n");
122                break;
123              case 0x6:
124                DPRINTF(I8259, "Subcommand: Set priority command.\n");
125                DPRINTF(I8259, "Lowest: IRQ%d   Highest IRQ%d.\n",
126                        bits(val, 2, 0), (bits(val, 2, 0) + 1) % 8);
127                break;
128              case 0x7:
129                DPRINTF(I8259, "Subcommand: Rotate on specific EOI.\n");
130                DPRINTF(I8259, "Lowest: IRQ%d   Highest IRQ%d.\n",
131                        bits(val, 2, 0), (bits(val, 2, 0) + 1) % 8);
132                break;
133            }
134        } else if (bits(val, 4, 3) == 1) {
135            DPRINTF(I8259, "Received operation command word 3.\n");
136            if (bits(val, 7)) {
137                DPRINTF(I8259, "%s special mask mode.\n",
138                        bits(val, 6) ? "Set" : "Clear");
139            }
140            if (bits(val, 1)) {
141                readIRR = bits(val, 0);
142                DPRINTF(I8259, "Read %s.\n", readIRR ? "IRR" : "ISR");
143            }
144        }
145        break;
146      case 0x1:
147        switch (initControlWord) {
148          case 0x0:
149            DPRINTF(I8259, "Received operation command word 1.\n");
150            DPRINTF(I8259, "Wrote IMR value %#x.\n", val);
151            IMR = val;
152            break;
153          case 0x1:
154            DPRINTF(I8259, "Received initialization command word 2.\n");
155            vectorOffset = val & ~mask(3);
156            DPRINTF(I8259, "Responsible for vectors %#x-%#x.\n",
157                    vectorOffset, vectorOffset | mask(3));
158            if (cascadeMode) {
159                initControlWord++;
160            } else {
161                cascadeBits = 0;
162                initControlWord = 0;
163            }
164            break;
165          case 0x2:
166            DPRINTF(I8259, "Received initialization command word 3.\n");
167            if (mode == Enums::I8259Master) {
168                DPRINTF(I8259, "Slaves attached to IRQs:%s%s%s%s%s%s%s%s\n",
169                        bits(val, 0) ? " 0" : "",
170                        bits(val, 1) ? " 1" : "",
171                        bits(val, 2) ? " 2" : "",
172                        bits(val, 3) ? " 3" : "",
173                        bits(val, 4) ? " 4" : "",
174                        bits(val, 5) ? " 5" : "",
175                        bits(val, 6) ? " 6" : "",
176                        bits(val, 7) ? " 7" : "");
177                cascadeBits = val;
178            } else {
179                DPRINTF(I8259, "Slave ID is %d.\n", val & mask(3));
180                cascadeBits = val & mask(3);
181            }
182            if (expectICW4)
183                initControlWord++;
184            else
185                initControlWord = 0;
186            break;
187          case 0x3:
188            DPRINTF(I8259, "Received initialization command word 4.\n");
189            if (bits(val, 4)) {
190                DPRINTF(I8259, "Special fully nested mode.\n");
191            } else {
192                DPRINTF(I8259, "Not special fully nested mode.\n");
193            }
194            if (bits(val, 3) == 0) {
195                DPRINTF(I8259, "Nonbuffered.\n");
196            } else if (bits(val, 2) == 0) {
197                DPRINTF(I8259, "Buffered.\n");
198            } else {
199                DPRINTF(I8259, "Unrecognized buffer mode.\n");
200            }
201            DPRINTF(I8259, "%s End Of Interrupt.\n",
202                    bits(val, 1) ? "Automatic" : "Normal");
203            DPRINTF(I8259, "%s mode.\n", bits(val, 0) ? "80x86" : "MCX-80/85");
204            initControlWord = 0;
205            break;
206        }
207        break;
208    }
209    return latency;
210}
211
212void
213X86ISA::I8259::handleEOI(int line)
214{
215    ISR &= ~(1 << line);
216    // There may be an interrupt that was waiting which can
217    // now be sent.
218    if (IRR)
219        requestInterrupt(findMsbSet(IRR));
220}
221
222void
223X86ISA::I8259::requestInterrupt(int line)
224{
225    if (bits(ISR, 7, line) == 0) {
226        if (output) {
227            DPRINTF(I8259, "Propogating interrupt.\n");
228            output->signalInterrupt();
229        } else {
230            warn("Received interrupt but didn't have "
231                    "anyone to tell about it.\n");
232        }
233    }
234}
235
236void
237X86ISA::I8259::signalInterrupt(int line)
238{
239    DPRINTF(I8259, "Interrupt raised on line %d.\n", line);
240    if (line >= NumLines)
241        fatal("Line number %d doesn't exist. The max is %d.\n",
242                line, NumLines - 1);
243    if (bits(IMR, line)) {
244        DPRINTF(I8259, "Interrupt %d was masked.\n", line);
245    } else {
246        IRR |= 1 << line;
247        requestInterrupt(line);
248    }
249}
250
251int
252X86ISA::I8259::getVector()
253{
254    /*
255     * This code only handles one slave. Since that's how the PC platform
256     * always uses the 8259 PIC, there shouldn't be any need for more. If
257     * there -is- a need for more for some reason, "slave" can become a
258     * vector of slaves.
259     */
260    int line = findMsbSet(IRR);
261    IRR &= ~(1 << line);
262    DPRINTF(I8259, "Interrupt %d was accepted.\n", line);
263    ISR |= 1 << line;
264    if (slave && bits(cascadeBits, line)) {
265        DPRINTF(I8259, "Interrupt was from slave who will "
266                "provide the vector.\n");
267        return slave->getVector();
268    }
269    return line | vectorOffset;
270}
271
272X86ISA::I8259 *
273I8259Params::create()
274{
275    return new X86ISA::I8259(this);
276}
277