2 * Copyright (c) 2008 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "dev/x86/i82094aa.hh"
33 #include "arch/x86/interrupts.hh"
34 #include "arch/x86/intmessage.hh"
35 #include "cpu/base.hh"
36 #include "debug/I82094AA.hh"
37 #include "dev/x86/i8259.hh"
38 #include "mem/packet.hh"
39 #include "mem/packet_access.hh"
40 #include "sim/system.hh"
42 X86ISA::I82094AA::I82094AA(Params
*p
)
43 : BasicPioDevice(p
, 20), extIntPic(p
->external_int_pic
),
44 lowestPriorityOffset(0),
45 intRequestPort(name() + ".int_request", this, this, p
->int_latency
)
47 // This assumes there's only one I/O APIC in the system and since the apic
48 // id is stored in a 8-bit field with 0xff meaning broadcast, the id must
51 assert(p
->apic_id
< 0xff);
52 initialApicId
= id
= p
->apic_id
;
55 RedirTableEntry entry
= 0;
57 for (int i
= 0; i
< TableSize
; i
++) {
58 redirTable
[i
] = entry
;
62 for (int i
= 0; i
< p
->port_inputs_connection_count
; i
++)
63 inputs
.push_back(new IntSinkPin
<I82094AA
>(
64 csprintf("%s.inputs[%d]", name(), i
), i
, this));
68 X86ISA::I82094AA::init()
70 // The io apic must register its address range with its pio port via
71 // the piodevice init() function.
72 BasicPioDevice::init();
74 // If the request port isn't connected, we can't send interrupts anywhere.
75 panic_if(!intRequestPort
.isConnected(),
76 "Int port not connected to anything!");
80 X86ISA::I82094AA::getPort(const std::string
&if_name
, PortID idx
)
82 if (if_name
== "int_request")
83 return intRequestPort
;
84 if (if_name
== "inputs")
85 return *inputs
.at(idx
);
87 return BasicPioDevice::getPort(if_name
, idx
);
91 X86ISA::I82094AA::read(PacketPtr pkt
)
93 assert(pkt
->getSize() == 4);
94 Addr offset
= pkt
->getAddr() - pioAddr
;
97 pkt
->setLE
<uint32_t>(regSel
);
100 pkt
->setLE
<uint32_t>(readReg(regSel
));
103 panic("Illegal read from I/O APIC.\n");
105 pkt
->makeAtomicResponse();
110 X86ISA::I82094AA::write(PacketPtr pkt
)
112 assert(pkt
->getSize() == 4);
113 Addr offset
= pkt
->getAddr() - pioAddr
;
116 regSel
= pkt
->getLE
<uint32_t>();
119 writeReg(regSel
, pkt
->getLE
<uint32_t>());
122 panic("Illegal write to I/O APIC.\n");
124 pkt
->makeAtomicResponse();
129 X86ISA::I82094AA::writeReg(uint8_t offset
, uint32_t value
)
132 id
= bits(value
, 31, 24);
133 } else if (offset
== 0x1) {
134 // The IOAPICVER register is read only.
135 } else if (offset
== 0x2) {
136 arbId
= bits(value
, 31, 24);
137 } else if (offset
>= 0x10 && offset
<= (0x10 + TableSize
* 2)) {
138 int index
= (offset
- 0x10) / 2;
140 redirTable
[index
].topDW
= value
;
141 redirTable
[index
].topReserved
= 0;
143 redirTable
[index
].bottomDW
= value
;
144 redirTable
[index
].bottomReserved
= 0;
147 warn("Access to undefined I/O APIC register %#x.\n", offset
);
150 "Wrote %#x to I/O APIC register %#x .\n", value
, offset
);
154 X86ISA::I82094AA::readReg(uint8_t offset
)
159 } else if (offset
== 0x1) {
160 result
= ((TableSize
- 1) << 16) | APICVersion
;
161 } else if (offset
== 0x2) {
162 result
= arbId
<< 24;
163 } else if (offset
>= 0x10 && offset
<= (0x10 + TableSize
* 2)) {
164 int index
= (offset
- 0x10) / 2;
166 result
= redirTable
[index
].topDW
;
168 result
= redirTable
[index
].bottomDW
;
171 warn("Access to undefined I/O APIC register %#x.\n", offset
);
174 "Read %#x from I/O APIC register %#x.\n", result
, offset
);
179 X86ISA::I82094AA::signalInterrupt(int line
)
181 DPRINTF(I82094AA
, "Received interrupt %d.\n", line
);
182 assert(line
< TableSize
);
183 RedirTableEntry entry
= redirTable
[line
];
185 DPRINTF(I82094AA
, "Entry was masked.\n");
188 TriggerIntMessage message
= 0;
189 message
.destination
= entry
.dest
;
190 if (entry
.deliveryMode
== DeliveryMode::ExtInt
) {
192 message
.vector
= extIntPic
->getVector();
194 message
.vector
= entry
.vector
;
196 message
.deliveryMode
= entry
.deliveryMode
;
197 message
.destMode
= entry
.destMode
;
198 message
.level
= entry
.polarity
;
199 message
.trigger
= entry
.trigger
;
200 std::list
<int> apics
;
201 int numContexts
= sys
->threads
.size();
202 if (message
.destMode
== 0) {
203 if (message
.deliveryMode
== DeliveryMode::LowestPriority
) {
204 panic("Lowest priority delivery mode from the "
205 "IO APIC aren't supported in physical "
206 "destination mode.\n");
208 if (message
.destination
== 0xFF) {
209 for (int i
= 0; i
< numContexts
; i
++) {
213 apics
.push_back(message
.destination
);
216 for (int i
= 0; i
< numContexts
; i
++) {
217 BaseInterrupts
*base_int
= sys
->threads
[i
]->
218 getCpuPtr()->getInterruptController(0);
219 auto *localApic
= dynamic_cast<Interrupts
*>(base_int
);
220 if ((localApic
->readReg(APIC_LOGICAL_DESTINATION
) >> 24) &
221 message
.destination
) {
222 apics
.push_back(localApic
->getInitialApicId());
225 if (message
.deliveryMode
== DeliveryMode::LowestPriority
&&
227 // The manual seems to suggest that the chipset just does
228 // something reasonable for these instead of actually using
229 // state from the local APIC. We'll just rotate an offset
230 // through the set of APICs selected above.
231 uint64_t modOffset
= lowestPriorityOffset
% apics
.size();
232 lowestPriorityOffset
++;
233 auto apicIt
= apics
.begin();
234 while (modOffset
--) {
236 assert(apicIt
!= apics
.end());
238 int selected
= *apicIt
;
240 apics
.push_back(selected
);
243 for (auto id
: apics
) {
244 PacketPtr pkt
= buildIntTriggerPacket(id
, message
);
245 intRequestPort
.sendMessage(pkt
, sys
->isTimingMode());
251 X86ISA::I82094AA::raiseInterruptPin(int number
)
253 assert(number
< TableSize
);
254 if (!pinStates
[number
])
255 signalInterrupt(number
);
256 pinStates
[number
] = true;
260 X86ISA::I82094AA::lowerInterruptPin(int number
)
262 assert(number
< TableSize
);
263 pinStates
[number
] = false;
267 X86ISA::I82094AA::serialize(CheckpointOut
&cp
) const
269 uint64_t* redirTableArray
= (uint64_t*)redirTable
;
270 SERIALIZE_SCALAR(regSel
);
271 SERIALIZE_SCALAR(initialApicId
);
272 SERIALIZE_SCALAR(id
);
273 SERIALIZE_SCALAR(arbId
);
274 SERIALIZE_SCALAR(lowestPriorityOffset
);
275 SERIALIZE_ARRAY(redirTableArray
, TableSize
);
276 SERIALIZE_ARRAY(pinStates
, TableSize
);
280 X86ISA::I82094AA::unserialize(CheckpointIn
&cp
)
282 uint64_t redirTableArray
[TableSize
];
283 UNSERIALIZE_SCALAR(regSel
);
284 UNSERIALIZE_SCALAR(initialApicId
);
285 UNSERIALIZE_SCALAR(id
);
286 UNSERIALIZE_SCALAR(arbId
);
287 UNSERIALIZE_SCALAR(lowestPriorityOffset
);
288 UNSERIALIZE_ARRAY(redirTableArray
, TableSize
);
289 UNSERIALIZE_ARRAY(pinStates
, TableSize
);
290 for (int i
= 0; i
< TableSize
; i
++) {
291 redirTable
[i
] = (RedirTableEntry
)redirTableArray
[i
];
296 I82094AAParams::create()
298 return new X86ISA::I82094AA(this);