mem: Fix guest corruption when caches handle uncacheable accesses
[gem5.git] / src / mem / coherent_bus.cc
1 /*
2 * Copyright (c) 2011-2012 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Ali Saidi
41 * Andreas Hansson
42 * William Wang
43 */
44
45 /**
46 * @file
47 * Definition of a bus object.
48 */
49
50 #include "base/misc.hh"
51 #include "base/trace.hh"
52 #include "debug/BusAddrRanges.hh"
53 #include "debug/CoherentBus.hh"
54 #include "mem/coherent_bus.hh"
55
56 CoherentBus::CoherentBus(const CoherentBusParams *p)
57 : BaseBus(p), reqLayer(*this, ".reqLayer", p->clock),
58 respLayer(*this, ".respLayer", p->clock),
59 snoopRespLayer(*this, ".snoopRespLayer", p->clock)
60 {
61 // create the ports based on the size of the master and slave
62 // vector ports, and the presence of the default port, the ports
63 // are enumerated starting from zero
64 for (int i = 0; i < p->port_master_connection_count; ++i) {
65 std::string portName = csprintf("%s.master[%d]", name(), i);
66 MasterPort* bp = new CoherentBusMasterPort(portName, *this, i);
67 masterPorts.push_back(bp);
68 }
69
70 // see if we have a default slave device connected and if so add
71 // our corresponding master port
72 if (p->port_default_connection_count) {
73 defaultPortID = masterPorts.size();
74 std::string portName = name() + ".default";
75 MasterPort* bp = new CoherentBusMasterPort(portName, *this,
76 defaultPortID);
77 masterPorts.push_back(bp);
78 }
79
80 // create the slave ports, once again starting at zero
81 for (int i = 0; i < p->port_slave_connection_count; ++i) {
82 std::string portName = csprintf("%s.slave[%d]", name(), i);
83 SlavePort* bp = new CoherentBusSlavePort(portName, *this, i);
84 slavePorts.push_back(bp);
85 }
86
87 clearPortCache();
88 }
89
90 void
91 CoherentBus::init()
92 {
93 // the base class is responsible for determining the block size
94 BaseBus::init();
95
96 // iterate over our slave ports and determine which of our
97 // neighbouring master ports are snooping and add them as snoopers
98 for (SlavePortConstIter p = slavePorts.begin(); p != slavePorts.end();
99 ++p) {
100 // check if the connected master port is snooping
101 if ((*p)->isSnooping()) {
102 DPRINTF(BusAddrRanges, "Adding snooping master %s\n",
103 (*p)->getMasterPort().name());
104 snoopPorts.push_back(*p);
105 }
106 }
107
108 if (snoopPorts.empty())
109 warn("CoherentBus %s has no snooping ports attached!\n", name());
110 }
111
112 bool
113 CoherentBus::recvTimingReq(PacketPtr pkt, PortID slave_port_id)
114 {
115 // determine the source port based on the id
116 SlavePort *src_port = slavePorts[slave_port_id];
117
118 // remember if the packet is an express snoop
119 bool is_express_snoop = pkt->isExpressSnoop();
120
121 // test if the bus should be considered occupied for the current
122 // port, and exclude express snoops from the check
123 if (!is_express_snoop && !reqLayer.tryTiming(src_port)) {
124 DPRINTF(CoherentBus, "recvTimingReq: src %s %s 0x%x BUSY\n",
125 src_port->name(), pkt->cmdString(), pkt->getAddr());
126 return false;
127 }
128
129 DPRINTF(CoherentBus, "recvTimingReq: src %s %s expr %d 0x%x\n",
130 src_port->name(), pkt->cmdString(), is_express_snoop,
131 pkt->getAddr());
132
133 // set the source port for routing of the response
134 pkt->setSrc(slave_port_id);
135
136 Tick headerFinishTime = is_express_snoop ? 0 : calcPacketTiming(pkt);
137 Tick packetFinishTime = is_express_snoop ? 0 : pkt->finishTime;
138
139 // uncacheable requests need never be snooped
140 if (!pkt->req->isUncacheable()) {
141 // the packet is a memory-mapped request and should be
142 // broadcasted to our snoopers but the source
143 forwardTiming(pkt, slave_port_id);
144 }
145
146 // remember if we add an outstanding req so we can undo it if
147 // necessary, if the packet needs a response, we should add it
148 // as outstanding and express snoops never fail so there is
149 // not need to worry about them
150 bool add_outstanding = !is_express_snoop && pkt->needsResponse();
151
152 // keep track that we have an outstanding request packet
153 // matching this request, this is used by the coherency
154 // mechanism in determining what to do with snoop responses
155 // (in recvTimingSnoop)
156 if (add_outstanding) {
157 // we should never have an exsiting request outstanding
158 assert(outstandingReq.find(pkt->req) == outstandingReq.end());
159 outstandingReq.insert(pkt->req);
160 }
161
162 // since it is a normal request, determine the destination
163 // based on the address and attempt to send the packet
164 bool success = masterPorts[findPort(pkt->getAddr())]->sendTimingReq(pkt);
165
166 // if this is an express snoop, we are done at this point
167 if (is_express_snoop) {
168 assert(success);
169 } else {
170 // for normal requests, check if successful
171 if (!success) {
172 // inhibited packets should never be forced to retry
173 assert(!pkt->memInhibitAsserted());
174
175 // if it was added as outstanding and the send failed, then
176 // erase it again
177 if (add_outstanding)
178 outstandingReq.erase(pkt->req);
179
180 DPRINTF(CoherentBus, "recvTimingReq: src %s %s 0x%x RETRY\n",
181 src_port->name(), pkt->cmdString(), pkt->getAddr());
182
183 // update the bus state and schedule an idle event
184 reqLayer.failedTiming(src_port, headerFinishTime);
185 } else {
186 // update the bus state and schedule an idle event
187 reqLayer.succeededTiming(packetFinishTime);
188 }
189 }
190
191 return success;
192 }
193
194 bool
195 CoherentBus::recvTimingResp(PacketPtr pkt, PortID master_port_id)
196 {
197 // determine the source port based on the id
198 MasterPort *src_port = masterPorts[master_port_id];
199
200 // test if the bus should be considered occupied for the current
201 // port
202 if (!respLayer.tryTiming(src_port)) {
203 DPRINTF(CoherentBus, "recvTimingResp: src %s %s 0x%x BUSY\n",
204 src_port->name(), pkt->cmdString(), pkt->getAddr());
205 return false;
206 }
207
208 DPRINTF(CoherentBus, "recvTimingResp: src %s %s 0x%x\n",
209 src_port->name(), pkt->cmdString(), pkt->getAddr());
210
211 calcPacketTiming(pkt);
212 Tick packetFinishTime = pkt->finishTime;
213
214 // the packet is a normal response to a request that we should
215 // have seen passing through the bus
216 assert(outstandingReq.find(pkt->req) != outstandingReq.end());
217
218 // remove it as outstanding
219 outstandingReq.erase(pkt->req);
220
221 // send the packet to the destination through one of our slave
222 // ports, as determined by the destination field
223 bool success M5_VAR_USED = slavePorts[pkt->getDest()]->sendTimingResp(pkt);
224
225 // currently it is illegal to block responses... can lead to
226 // deadlock
227 assert(success);
228
229 respLayer.succeededTiming(packetFinishTime);
230
231 return true;
232 }
233
234 void
235 CoherentBus::recvTimingSnoopReq(PacketPtr pkt, PortID master_port_id)
236 {
237 DPRINTF(CoherentBus, "recvTimingSnoopReq: src %s %s 0x%x\n",
238 masterPorts[master_port_id]->name(), pkt->cmdString(),
239 pkt->getAddr());
240
241 // we should only see express snoops from caches
242 assert(pkt->isExpressSnoop());
243
244 // set the source port for routing of the response
245 pkt->setSrc(master_port_id);
246
247 // forward to all snoopers
248 forwardTiming(pkt, InvalidPortID);
249
250 // a snoop request came from a connected slave device (one of
251 // our master ports), and if it is not coming from the slave
252 // device responsible for the address range something is
253 // wrong, hence there is nothing further to do as the packet
254 // would be going back to where it came from
255 assert(master_port_id == findPort(pkt->getAddr()));
256 }
257
258 bool
259 CoherentBus::recvTimingSnoopResp(PacketPtr pkt, PortID slave_port_id)
260 {
261 // determine the source port based on the id
262 SlavePort* src_port = slavePorts[slave_port_id];
263
264 // test if the bus should be considered occupied for the current
265 // port
266 if (!snoopRespLayer.tryTiming(src_port)) {
267 DPRINTF(CoherentBus, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n",
268 src_port->name(), pkt->cmdString(), pkt->getAddr());
269 return false;
270 }
271
272 DPRINTF(CoherentBus, "recvTimingSnoop: src %s %s 0x%x\n",
273 src_port->name(), pkt->cmdString(), pkt->getAddr());
274
275 // get the destination from the packet
276 PortID dest = pkt->getDest();
277
278 // responses are never express snoops
279 assert(!pkt->isExpressSnoop());
280
281 calcPacketTiming(pkt);
282 Tick packetFinishTime = pkt->finishTime;
283
284 // determine if the response is from a snoop request we
285 // created as the result of a normal request (in which case it
286 // should be in the outstandingReq), or if we merely forwarded
287 // someone else's snoop request
288 if (outstandingReq.find(pkt->req) == outstandingReq.end()) {
289 // this is a snoop response to a snoop request we
290 // forwarded, e.g. coming from the L1 and going to the L2
291 // this should be forwarded as a snoop response
292 bool success M5_VAR_USED = masterPorts[dest]->sendTimingSnoopResp(pkt);
293 assert(success);
294 } else {
295 // we got a snoop response on one of our slave ports,
296 // i.e. from a coherent master connected to the bus, and
297 // since we created the snoop request as part of
298 // recvTiming, this should now be a normal response again
299 outstandingReq.erase(pkt->req);
300
301 // this is a snoop response from a coherent master, with a
302 // destination field set on its way through the bus as
303 // request, hence it should never go back to where the
304 // snoop response came from, but instead to where the
305 // original request came from
306 assert(slave_port_id != dest);
307
308 // as a normal response, it should go back to a master
309 // through one of our slave ports
310 bool success M5_VAR_USED = slavePorts[dest]->sendTimingResp(pkt);
311
312 // currently it is illegal to block responses... can lead
313 // to deadlock
314 assert(success);
315 }
316
317 snoopRespLayer.succeededTiming(packetFinishTime);
318
319 return true;
320 }
321
322
323 void
324 CoherentBus::forwardTiming(PacketPtr pkt, PortID exclude_slave_port_id)
325 {
326 for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
327 SlavePort *p = *s;
328 // we could have gotten this request from a snooping master
329 // (corresponding to our own slave port that is also in
330 // snoopPorts) and should not send it back to where it came
331 // from
332 if (exclude_slave_port_id == InvalidPortID ||
333 p->getId() != exclude_slave_port_id) {
334 // cache is not allowed to refuse snoop
335 p->sendTimingSnoopReq(pkt);
336 }
337 }
338 }
339
340 void
341 CoherentBus::recvRetry()
342 {
343 // responses and snoop responses never block on forwarding them,
344 // so the retry will always be coming from a port to which we
345 // tried to forward a request
346 reqLayer.recvRetry();
347 }
348
349 Tick
350 CoherentBus::recvAtomic(PacketPtr pkt, PortID slave_port_id)
351 {
352 DPRINTF(CoherentBus, "recvAtomic: packet src %s addr 0x%x cmd %s\n",
353 slavePorts[slave_port_id]->name(), pkt->getAddr(),
354 pkt->cmdString());
355
356 MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
357 Tick snoop_response_latency = 0;
358
359 // uncacheable requests need never be snooped
360 if (!pkt->req->isUncacheable()) {
361 // forward to all snoopers but the source
362 std::pair<MemCmd, Tick> snoop_result =
363 forwardAtomic(pkt, slave_port_id);
364 snoop_response_cmd = snoop_result.first;
365 snoop_response_latency = snoop_result.second;
366 }
367
368 // even if we had a snoop response, we must continue and also
369 // perform the actual request at the destination
370 PortID dest_id = findPort(pkt->getAddr());
371
372 // forward the request to the appropriate destination
373 Tick response_latency = masterPorts[dest_id]->sendAtomic(pkt);
374
375 // if we got a response from a snooper, restore it here
376 if (snoop_response_cmd != MemCmd::InvalidCmd) {
377 // no one else should have responded
378 assert(!pkt->isResponse());
379 pkt->cmd = snoop_response_cmd;
380 response_latency = snoop_response_latency;
381 }
382
383 pkt->finishTime = curTick() + response_latency;
384 return response_latency;
385 }
386
387 Tick
388 CoherentBus::recvAtomicSnoop(PacketPtr pkt, PortID master_port_id)
389 {
390 DPRINTF(CoherentBus, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n",
391 masterPorts[master_port_id]->name(), pkt->getAddr(),
392 pkt->cmdString());
393
394 // forward to all snoopers
395 std::pair<MemCmd, Tick> snoop_result =
396 forwardAtomic(pkt, InvalidPortID);
397 MemCmd snoop_response_cmd = snoop_result.first;
398 Tick snoop_response_latency = snoop_result.second;
399
400 if (snoop_response_cmd != MemCmd::InvalidCmd)
401 pkt->cmd = snoop_response_cmd;
402
403 pkt->finishTime = curTick() + snoop_response_latency;
404 return snoop_response_latency;
405 }
406
407 std::pair<MemCmd, Tick>
408 CoherentBus::forwardAtomic(PacketPtr pkt, PortID exclude_slave_port_id)
409 {
410 // the packet may be changed on snoops, record the original
411 // command to enable us to restore it between snoops so that
412 // additional snoops can take place properly
413 MemCmd orig_cmd = pkt->cmd;
414 MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
415 Tick snoop_response_latency = 0;
416
417 for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
418 SlavePort *p = *s;
419 // we could have gotten this request from a snooping master
420 // (corresponding to our own slave port that is also in
421 // snoopPorts) and should not send it back to where it came
422 // from
423 if (exclude_slave_port_id == InvalidPortID ||
424 p->getId() != exclude_slave_port_id) {
425 Tick latency = p->sendAtomicSnoop(pkt);
426 // in contrast to a functional access, we have to keep on
427 // going as all snoopers must be updated even if we get a
428 // response
429 if (pkt->isResponse()) {
430 // response from snoop agent
431 assert(pkt->cmd != orig_cmd);
432 assert(pkt->memInhibitAsserted());
433 // should only happen once
434 assert(snoop_response_cmd == MemCmd::InvalidCmd);
435 // save response state
436 snoop_response_cmd = pkt->cmd;
437 snoop_response_latency = latency;
438 // restore original packet state for remaining snoopers
439 pkt->cmd = orig_cmd;
440 }
441 }
442 }
443
444 // the packet is restored as part of the loop and any potential
445 // snoop response is part of the returned pair
446 return std::make_pair(snoop_response_cmd, snoop_response_latency);
447 }
448
449 void
450 CoherentBus::recvFunctional(PacketPtr pkt, PortID slave_port_id)
451 {
452 if (!pkt->isPrint()) {
453 // don't do DPRINTFs on PrintReq as it clutters up the output
454 DPRINTF(CoherentBus,
455 "recvFunctional: packet src %s addr 0x%x cmd %s\n",
456 slavePorts[slave_port_id]->name(), pkt->getAddr(),
457 pkt->cmdString());
458 }
459
460 // uncacheable requests need never be snooped
461 if (!pkt->req->isUncacheable()) {
462 // forward to all snoopers but the source
463 forwardFunctional(pkt, slave_port_id);
464 }
465
466 // there is no need to continue if the snooping has found what we
467 // were looking for and the packet is already a response
468 if (!pkt->isResponse()) {
469 PortID dest_id = findPort(pkt->getAddr());
470
471 masterPorts[dest_id]->sendFunctional(pkt);
472 }
473 }
474
475 void
476 CoherentBus::recvFunctionalSnoop(PacketPtr pkt, PortID master_port_id)
477 {
478 if (!pkt->isPrint()) {
479 // don't do DPRINTFs on PrintReq as it clutters up the output
480 DPRINTF(CoherentBus,
481 "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n",
482 masterPorts[master_port_id]->name(), pkt->getAddr(),
483 pkt->cmdString());
484 }
485
486 // forward to all snoopers
487 forwardFunctional(pkt, InvalidPortID);
488 }
489
490 void
491 CoherentBus::forwardFunctional(PacketPtr pkt, PortID exclude_slave_port_id)
492 {
493 for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
494 SlavePort *p = *s;
495 // we could have gotten this request from a snooping master
496 // (corresponding to our own slave port that is also in
497 // snoopPorts) and should not send it back to where it came
498 // from
499 if (exclude_slave_port_id == InvalidPortID ||
500 p->getId() != exclude_slave_port_id)
501 p->sendFunctionalSnoop(pkt);
502
503 // if we get a response we are done
504 if (pkt->isResponse()) {
505 break;
506 }
507 }
508 }
509
510 unsigned int
511 CoherentBus::drain(DrainManager *dm)
512 {
513 // sum up the individual layers
514 return reqLayer.drain(dm) + respLayer.drain(dm) + snoopRespLayer.drain(dm);
515 }
516
517 CoherentBus *
518 CoherentBusParams::create()
519 {
520 return new CoherentBus(this);
521 }