Port: Add isSnooping to slave port (asking master port)
[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)
58 {
59 // create the ports based on the size of the master and slave
60 // vector ports, and the presence of the default port, the ports
61 // are enumerated starting from zero
62 for (int i = 0; i < p->port_master_connection_count; ++i) {
63 std::string portName = csprintf("%s-p%d", name(), i);
64 MasterPort* bp = new CoherentBusMasterPort(portName, *this, i);
65 masterPorts.push_back(bp);
66 }
67
68 // see if we have a default slave device connected and if so add
69 // our corresponding master port
70 if (p->port_default_connection_count) {
71 defaultPortID = masterPorts.size();
72 std::string portName = csprintf("%s-default", name());
73 MasterPort* bp = new CoherentBusMasterPort(portName, *this,
74 defaultPortID);
75 masterPorts.push_back(bp);
76 }
77
78 // create the slave ports, once again starting at zero
79 for (int i = 0; i < p->port_slave_connection_count; ++i) {
80 std::string portName = csprintf("%s-p%d", name(), i);
81 SlavePort* bp = new CoherentBusSlavePort(portName, *this, i);
82 slavePorts.push_back(bp);
83 }
84
85 clearPortCache();
86 }
87
88 void
89 CoherentBus::init()
90 {
91 // iterate over our slave ports and determine which of our
92 // neighbouring master ports are snooping and add them as snoopers
93 for (SlavePortConstIter p = slavePorts.begin(); p != slavePorts.end();
94 ++p) {
95 // check if the connected master port is snooping
96 if ((*p)->isSnooping()) {
97 DPRINTF(BusAddrRanges, "Adding snooping master %s\n",
98 (*p)->getMasterPort().name());
99 snoopPorts.push_back(*p);
100 }
101 }
102
103 if (snoopPorts.empty())
104 warn("CoherentBus %s has no snooping ports attached!\n", name());
105 }
106
107 bool
108 CoherentBus::recvTimingReq(PacketPtr pkt, PortID slave_port_id)
109 {
110 // determine the source port based on the id
111 SlavePort *src_port = slavePorts[slave_port_id];
112
113 // test if the bus should be considered occupied for the current
114 // port, and exclude express snoops from the check
115 if (!pkt->isExpressSnoop() && isOccupied(src_port)) {
116 DPRINTF(CoherentBus, "recvTimingReq: src %s %s 0x%x BUSY\n",
117 src_port->name(), pkt->cmdString(), pkt->getAddr());
118 return false;
119 }
120
121 DPRINTF(CoherentBus, "recvTimingReq: src %s %s 0x%x\n",
122 src_port->name(), pkt->cmdString(), pkt->getAddr());
123
124 // set the source port for routing of the response
125 pkt->setSrc(slave_port_id);
126
127 Tick headerFinishTime = pkt->isExpressSnoop() ? 0 : calcPacketTiming(pkt);
128 Tick packetFinishTime = pkt->isExpressSnoop() ? 0 : pkt->finishTime;
129
130 // uncacheable requests need never be snooped
131 if (!pkt->req->isUncacheable()) {
132 // the packet is a memory-mapped request and should be
133 // broadcasted to our snoopers but the source
134 forwardTiming(pkt, slave_port_id);
135 }
136
137 // remember if we add an outstanding req so we can undo it if
138 // necessary, if the packet needs a response, we should add it
139 // as outstanding and express snoops never fail so there is
140 // not need to worry about them
141 bool add_outstanding = !pkt->isExpressSnoop() && pkt->needsResponse();
142
143 // keep track that we have an outstanding request packet
144 // matching this request, this is used by the coherency
145 // mechanism in determining what to do with snoop responses
146 // (in recvTimingSnoop)
147 if (add_outstanding) {
148 // we should never have an exsiting request outstanding
149 assert(outstandingReq.find(pkt->req) == outstandingReq.end());
150 outstandingReq.insert(pkt->req);
151 }
152
153 // since it is a normal request, determine the destination
154 // based on the address and attempt to send the packet
155 bool success = masterPorts[findPort(pkt->getAddr())]->sendTimingReq(pkt);
156
157 if (!success) {
158 // inhibited packets should never be forced to retry
159 assert(!pkt->memInhibitAsserted());
160
161 // if it was added as outstanding and the send failed, then
162 // erase it again
163 if (add_outstanding)
164 outstandingReq.erase(pkt->req);
165
166 DPRINTF(CoherentBus, "recvTimingReq: src %s %s 0x%x RETRY\n",
167 src_port->name(), pkt->cmdString(), pkt->getAddr());
168
169 addToRetryList(src_port);
170 occupyBus(headerFinishTime);
171
172 return false;
173 }
174
175 succeededTiming(packetFinishTime);
176
177 return true;
178 }
179
180 bool
181 CoherentBus::recvTimingResp(PacketPtr pkt, PortID master_port_id)
182 {
183 // determine the source port based on the id
184 MasterPort *src_port = masterPorts[master_port_id];
185
186 // test if the bus should be considered occupied for the current
187 // port
188 if (isOccupied(src_port)) {
189 DPRINTF(CoherentBus, "recvTimingResp: src %s %s 0x%x BUSY\n",
190 src_port->name(), pkt->cmdString(), pkt->getAddr());
191 return false;
192 }
193
194 DPRINTF(CoherentBus, "recvTimingResp: src %s %s 0x%x\n",
195 src_port->name(), pkt->cmdString(), pkt->getAddr());
196
197 calcPacketTiming(pkt);
198 Tick packetFinishTime = pkt->finishTime;
199
200 // the packet is a normal response to a request that we should
201 // have seen passing through the bus
202 assert(outstandingReq.find(pkt->req) != outstandingReq.end());
203
204 // remove it as outstanding
205 outstandingReq.erase(pkt->req);
206
207 // send the packet to the destination through one of our slave
208 // ports, as determined by the destination field
209 bool success M5_VAR_USED = slavePorts[pkt->getDest()]->sendTimingResp(pkt);
210
211 // currently it is illegal to block responses... can lead to
212 // deadlock
213 assert(success);
214
215 succeededTiming(packetFinishTime);
216
217 return true;
218 }
219
220 void
221 CoherentBus::recvTimingSnoopReq(PacketPtr pkt, PortID master_port_id)
222 {
223 DPRINTF(CoherentBus, "recvTimingSnoopReq: src %s %s 0x%x\n",
224 masterPorts[master_port_id]->name(), pkt->cmdString(),
225 pkt->getAddr());
226
227 // we should only see express snoops from caches
228 assert(pkt->isExpressSnoop());
229
230 // set the source port for routing of the response
231 pkt->setSrc(master_port_id);
232
233 // forward to all snoopers
234 forwardTiming(pkt, InvalidPortID);
235
236 // a snoop request came from a connected slave device (one of
237 // our master ports), and if it is not coming from the slave
238 // device responsible for the address range something is
239 // wrong, hence there is nothing further to do as the packet
240 // would be going back to where it came from
241 assert(master_port_id == findPort(pkt->getAddr()));
242
243 // this is an express snoop and is never forced to retry
244 assert(!inRetry);
245 }
246
247 bool
248 CoherentBus::recvTimingSnoopResp(PacketPtr pkt, PortID slave_port_id)
249 {
250 // determine the source port based on the id
251 SlavePort* src_port = slavePorts[slave_port_id];
252
253 // test if the bus should be considered occupied for the current
254 // port
255 if (isOccupied(src_port)) {
256 DPRINTF(CoherentBus, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n",
257 src_port->name(), pkt->cmdString(), pkt->getAddr());
258 return false;
259 }
260
261 DPRINTF(CoherentBus, "recvTimingSnoop: src %s %s 0x%x\n",
262 src_port->name(), pkt->cmdString(), pkt->getAddr());
263
264 // get the destination from the packet
265 PortID dest = pkt->getDest();
266
267 // responses are never express snoops
268 assert(!pkt->isExpressSnoop());
269
270 calcPacketTiming(pkt);
271 Tick packetFinishTime = pkt->finishTime;
272
273 // determine if the response is from a snoop request we
274 // created as the result of a normal request (in which case it
275 // should be in the outstandingReq), or if we merely forwarded
276 // someone else's snoop request
277 if (outstandingReq.find(pkt->req) == outstandingReq.end()) {
278 // this is a snoop response to a snoop request we
279 // forwarded, e.g. coming from the L1 and going to the L2
280 // this should be forwarded as a snoop response
281 bool success M5_VAR_USED = masterPorts[dest]->sendTimingSnoopResp(pkt);
282 assert(success);
283 } else {
284 // we got a snoop response on one of our slave ports,
285 // i.e. from a coherent master connected to the bus, and
286 // since we created the snoop request as part of
287 // recvTiming, this should now be a normal response again
288 outstandingReq.erase(pkt->req);
289
290 // this is a snoop response from a coherent master, with a
291 // destination field set on its way through the bus as
292 // request, hence it should never go back to where the
293 // snoop response came from, but instead to where the
294 // original request came from
295 assert(slave_port_id != dest);
296
297 // as a normal response, it should go back to a master
298 // through one of our slave ports
299 bool success M5_VAR_USED = slavePorts[dest]->sendTimingResp(pkt);
300
301 // currently it is illegal to block responses... can lead
302 // to deadlock
303 assert(success);
304 }
305
306 succeededTiming(packetFinishTime);
307
308 return true;
309 }
310
311
312 void
313 CoherentBus::forwardTiming(PacketPtr pkt, PortID exclude_slave_port_id)
314 {
315 for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
316 SlavePort *p = *s;
317 // we could have gotten this request from a snooping master
318 // (corresponding to our own slave port that is also in
319 // snoopPorts) and should not send it back to where it came
320 // from
321 if (exclude_slave_port_id == InvalidPortID ||
322 p->getId() != exclude_slave_port_id) {
323 // cache is not allowed to refuse snoop
324 p->sendTimingSnoopReq(pkt);
325 }
326 }
327 }
328
329 Tick
330 CoherentBus::recvAtomic(PacketPtr pkt, PortID slave_port_id)
331 {
332 DPRINTF(CoherentBus, "recvAtomic: packet src %s addr 0x%x cmd %s\n",
333 slavePorts[slave_port_id]->name(), pkt->getAddr(),
334 pkt->cmdString());
335
336 MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
337 Tick snoop_response_latency = 0;
338
339 // uncacheable requests need never be snooped
340 if (!pkt->req->isUncacheable()) {
341 // forward to all snoopers but the source
342 std::pair<MemCmd, Tick> snoop_result =
343 forwardAtomic(pkt, slave_port_id);
344 snoop_response_cmd = snoop_result.first;
345 snoop_response_latency = snoop_result.second;
346 }
347
348 // even if we had a snoop response, we must continue and also
349 // perform the actual request at the destination
350 PortID dest_id = findPort(pkt->getAddr());
351
352 // forward the request to the appropriate destination
353 Tick response_latency = masterPorts[dest_id]->sendAtomic(pkt);
354
355 // if we got a response from a snooper, restore it here
356 if (snoop_response_cmd != MemCmd::InvalidCmd) {
357 // no one else should have responded
358 assert(!pkt->isResponse());
359 pkt->cmd = snoop_response_cmd;
360 response_latency = snoop_response_latency;
361 }
362
363 pkt->finishTime = curTick() + response_latency;
364 return response_latency;
365 }
366
367 Tick
368 CoherentBus::recvAtomicSnoop(PacketPtr pkt, PortID master_port_id)
369 {
370 DPRINTF(CoherentBus, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n",
371 masterPorts[master_port_id]->name(), pkt->getAddr(),
372 pkt->cmdString());
373
374 // forward to all snoopers
375 std::pair<MemCmd, Tick> snoop_result =
376 forwardAtomic(pkt, InvalidPortID);
377 MemCmd snoop_response_cmd = snoop_result.first;
378 Tick snoop_response_latency = snoop_result.second;
379
380 if (snoop_response_cmd != MemCmd::InvalidCmd)
381 pkt->cmd = snoop_response_cmd;
382
383 pkt->finishTime = curTick() + snoop_response_latency;
384 return snoop_response_latency;
385 }
386
387 std::pair<MemCmd, Tick>
388 CoherentBus::forwardAtomic(PacketPtr pkt, PortID exclude_slave_port_id)
389 {
390 // the packet may be changed on snoops, record the original
391 // command to enable us to restore it between snoops so that
392 // additional snoops can take place properly
393 MemCmd orig_cmd = pkt->cmd;
394 MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
395 Tick snoop_response_latency = 0;
396
397 for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
398 SlavePort *p = *s;
399 // we could have gotten this request from a snooping master
400 // (corresponding to our own slave port that is also in
401 // snoopPorts) and should not send it back to where it came
402 // from
403 if (exclude_slave_port_id == InvalidPortID ||
404 p->getId() != exclude_slave_port_id) {
405 Tick latency = p->sendAtomicSnoop(pkt);
406 // in contrast to a functional access, we have to keep on
407 // going as all snoopers must be updated even if we get a
408 // response
409 if (pkt->isResponse()) {
410 // response from snoop agent
411 assert(pkt->cmd != orig_cmd);
412 assert(pkt->memInhibitAsserted());
413 // should only happen once
414 assert(snoop_response_cmd == MemCmd::InvalidCmd);
415 // save response state
416 snoop_response_cmd = pkt->cmd;
417 snoop_response_latency = latency;
418 // restore original packet state for remaining snoopers
419 pkt->cmd = orig_cmd;
420 }
421 }
422 }
423
424 // the packet is restored as part of the loop and any potential
425 // snoop response is part of the returned pair
426 return std::make_pair(snoop_response_cmd, snoop_response_latency);
427 }
428
429 void
430 CoherentBus::recvFunctional(PacketPtr pkt, PortID slave_port_id)
431 {
432 if (!pkt->isPrint()) {
433 // don't do DPRINTFs on PrintReq as it clutters up the output
434 DPRINTF(CoherentBus,
435 "recvFunctional: packet src %s addr 0x%x cmd %s\n",
436 slavePorts[slave_port_id]->name(), pkt->getAddr(),
437 pkt->cmdString());
438 }
439
440 // uncacheable requests need never be snooped
441 if (!pkt->req->isUncacheable()) {
442 // forward to all snoopers but the source
443 forwardFunctional(pkt, slave_port_id);
444 }
445
446 // there is no need to continue if the snooping has found what we
447 // were looking for and the packet is already a response
448 if (!pkt->isResponse()) {
449 PortID dest_id = findPort(pkt->getAddr());
450
451 masterPorts[dest_id]->sendFunctional(pkt);
452 }
453 }
454
455 void
456 CoherentBus::recvFunctionalSnoop(PacketPtr pkt, PortID master_port_id)
457 {
458 if (!pkt->isPrint()) {
459 // don't do DPRINTFs on PrintReq as it clutters up the output
460 DPRINTF(CoherentBus,
461 "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n",
462 masterPorts[master_port_id]->name(), pkt->getAddr(),
463 pkt->cmdString());
464 }
465
466 // forward to all snoopers
467 forwardFunctional(pkt, InvalidPortID);
468 }
469
470 void
471 CoherentBus::forwardFunctional(PacketPtr pkt, PortID exclude_slave_port_id)
472 {
473 for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
474 SlavePort *p = *s;
475 // we could have gotten this request from a snooping master
476 // (corresponding to our own slave port that is also in
477 // snoopPorts) and should not send it back to where it came
478 // from
479 if (exclude_slave_port_id == InvalidPortID ||
480 p->getId() != exclude_slave_port_id)
481 p->sendFunctionalSnoop(pkt);
482
483 // if we get a response we are done
484 if (pkt->isResponse()) {
485 break;
486 }
487 }
488 }
489
490 CoherentBus *
491 CoherentBusParams::create()
492 {
493 return new CoherentBus(this);
494 }