1/*
2 * Copyright (c) 2011-2015 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 crossbar object.
48 */
49
50#include "base/misc.hh"
51#include "base/trace.hh"
52#include "debug/AddrRanges.hh"
53#include "debug/CoherentXBar.hh"
54#include "mem/coherent_xbar.hh"
55#include "sim/system.hh"
56
57CoherentXBar::CoherentXBar(const CoherentXBarParams *p)
58 : BaseXBar(p), system(p->system), snoopFilter(p->snoop_filter),
59 snoopResponseLatency(p->snoop_response_latency)
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 CoherentXBarMasterPort(portName, *this, i);
67 masterPorts.push_back(bp);
68 reqLayers.push_back(new ReqLayer(*bp, *this,
69 csprintf(".reqLayer%d", i)));
70 snoopLayers.push_back(new SnoopRespLayer(*bp, *this,
71 csprintf(".snoopLayer%d", i)));
72 }
73
74 // see if we have a default slave device connected and if so add
75 // our corresponding master port
76 if (p->port_default_connection_count) {
77 defaultPortID = masterPorts.size();
78 std::string portName = name() + ".default";
79 MasterPort* bp = new CoherentXBarMasterPort(portName, *this,
80 defaultPortID);
81 masterPorts.push_back(bp);
82 reqLayers.push_back(new ReqLayer(*bp, *this, csprintf(".reqLayer%d",
83 defaultPortID)));
84 snoopLayers.push_back(new SnoopRespLayer(*bp, *this,
85 csprintf(".snoopLayer%d",
86 defaultPortID)));
87 }
88
89 // create the slave ports, once again starting at zero
90 for (int i = 0; i < p->port_slave_connection_count; ++i) {
91 std::string portName = csprintf("%s.slave[%d]", name(), i);
92 SlavePort* bp = new CoherentXBarSlavePort(portName, *this, i);
92 QueuedSlavePort* bp = new CoherentXBarSlavePort(portName, *this, i);
93 slavePorts.push_back(bp);
94 respLayers.push_back(new RespLayer(*bp, *this,
95 csprintf(".respLayer%d", i)));
96 snoopRespPorts.push_back(new SnoopRespPort(*bp, *this));
97 }
98
99 if (snoopFilter)
100 snoopFilter->setSlavePorts(slavePorts);
101
102 clearPortCache();
103}
104
105CoherentXBar::~CoherentXBar()
106{
107 for (auto l: reqLayers)
108 delete l;
109 for (auto l: respLayers)
110 delete l;
111 for (auto l: snoopLayers)
112 delete l;
113 for (auto p: snoopRespPorts)
114 delete p;
115}
116
117void
118CoherentXBar::init()
119{
120 // the base class is responsible for determining the block size
121 BaseXBar::init();
122
123 // iterate over our slave ports and determine which of our
124 // neighbouring master ports are snooping and add them as snoopers
125 for (const auto& p: slavePorts) {
126 // check if the connected master port is snooping
127 if (p->isSnooping()) {
128 DPRINTF(AddrRanges, "Adding snooping master %s\n",
129 p->getMasterPort().name());
130 snoopPorts.push_back(p);
131 }
132 }
133
134 if (snoopPorts.empty())
135 warn("CoherentXBar %s has no snooping ports attached!\n", name());
136}
137
138bool
139CoherentXBar::recvTimingReq(PacketPtr pkt, PortID slave_port_id)
140{
141 // @todo temporary hack to deal with memory corruption issue until
142 // 4-phase transactions are complete
143 for (int x = 0; x < pendingDelete.size(); x++)
144 delete pendingDelete[x];
145 pendingDelete.clear();
146
147 // determine the source port based on the id
148 SlavePort *src_port = slavePorts[slave_port_id];
149
150 // remember if the packet is an express snoop
151 bool is_express_snoop = pkt->isExpressSnoop();
152 bool is_inhibited = pkt->memInhibitAsserted();
153 // for normal requests, going downstream, the express snoop flag
154 // and the inhibited flag should always be the same
155 assert(is_express_snoop == is_inhibited);
156
157 // determine the destination based on the address
158 PortID master_port_id = findPort(pkt->getAddr());
159
160 // test if the crossbar should be considered occupied for the current
161 // port, and exclude express snoops from the check
162 if (!is_express_snoop && !reqLayers[master_port_id]->tryTiming(src_port)) {
163 DPRINTF(CoherentXBar, "recvTimingReq: src %s %s 0x%x BUSY\n",
164 src_port->name(), pkt->cmdString(), pkt->getAddr());
165 return false;
166 }
167
168 DPRINTF(CoherentXBar, "recvTimingReq: src %s %s expr %d 0x%x\n",
169 src_port->name(), pkt->cmdString(), is_express_snoop,
170 pkt->getAddr());
171
172 // store size and command as they might be modified when
173 // forwarding the packet
174 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
175 unsigned int pkt_cmd = pkt->cmdToIndex();
176
177 // store the old header delay so we can restore it if needed
178 Tick old_header_delay = pkt->headerDelay;
179
180 // a request sees the frontend and forward latency
181 Tick xbar_delay = (frontendLatency + forwardLatency) * clockPeriod();
182
183 // set the packet header and payload delay
184 calcPacketTiming(pkt, xbar_delay);
185
186 // determine how long to be crossbar layer is busy
187 Tick packetFinishTime = clockEdge(Cycles(1)) + pkt->payloadDelay;
188
189 if (!system->bypassCaches()) {
190 // the packet is a memory-mapped request and should be
191 // broadcasted to our snoopers but the source
192 if (snoopFilter) {
193 // check with the snoop filter where to forward this packet
194 auto sf_res = snoopFilter->lookupRequest(pkt, *src_port);
195 // If SnoopFilter is enabled, the total time required by a packet
196 // to be delivered through the xbar has to be charged also with
197 // to lookup latency of the snoop filter (sf_res.second).
198 pkt->headerDelay += sf_res.second * clockPeriod();
199 packetFinishTime += sf_res.second * clockPeriod();
200 DPRINTF(CoherentXBar, "recvTimingReq: src %s %s 0x%x"\
201 " SF size: %i lat: %i\n", src_port->name(),
202 pkt->cmdString(), pkt->getAddr(), sf_res.first.size(),
203 sf_res.second);
204 forwardTiming(pkt, slave_port_id, sf_res.first);
205 } else {
206 forwardTiming(pkt, slave_port_id);
207 }
208 }
209
210 // forwardTiming snooped into peer caches of the sender, and if
211 // this is a clean evict, but the packet is found in a cache, do
212 // not forward it
213 if (pkt->cmd == MemCmd::CleanEvict && pkt->isBlockCached()) {
214 DPRINTF(CoherentXBar, "recvTimingReq: Clean evict 0x%x still cached, "
215 "not forwarding\n", pkt->getAddr());
216
217 // update the layer state and schedule an idle event
218 reqLayers[master_port_id]->succeededTiming(packetFinishTime);
219 pendingDelete.push_back(pkt);
220 return true;
221 }
222
223 // remember if the packet will generate a snoop response
224 const bool expect_snoop_resp = !is_inhibited && pkt->memInhibitAsserted();
225 const bool expect_response = pkt->needsResponse() &&
226 !pkt->memInhibitAsserted();
227
228 // Note: Cannot create a copy of the full packet, here.
229 MemCmd orig_cmd(pkt->cmd);
230
231 // since it is a normal request, attempt to send the packet
232 bool success = masterPorts[master_port_id]->sendTimingReq(pkt);
233
234 if (snoopFilter && !system->bypassCaches()) {
235 // The packet may already be overwritten by the sendTimingReq function.
236 // The snoop filter needs to see the original request *and* the return
237 // status of the send operation, so we need to recreate the original
238 // request. Atomic mode does not have the issue, as there the send
239 // operation and the response happen instantaneously and don't need two
240 // phase tracking.
241 MemCmd tmp_cmd(pkt->cmd);
242 pkt->cmd = orig_cmd;
243 // Let the snoop filter know about the success of the send operation
244 snoopFilter->updateRequest(pkt, *src_port, !success);
245 pkt->cmd = tmp_cmd;
246 }
247
248 // check if we were successful in sending the packet onwards
249 if (!success) {
250 // express snoops and inhibited packets should never be forced
251 // to retry
252 assert(!is_express_snoop);
253 assert(!pkt->memInhibitAsserted());
254
255 // restore the header delay
256 pkt->headerDelay = old_header_delay;
257
258 DPRINTF(CoherentXBar, "recvTimingReq: src %s %s 0x%x RETRY\n",
259 src_port->name(), pkt->cmdString(), pkt->getAddr());
260
261 // update the layer state and schedule an idle event
262 reqLayers[master_port_id]->failedTiming(src_port,
263 clockEdge(Cycles(1)));
264 } else {
265 // express snoops currently bypass the crossbar state entirely
266 if (!is_express_snoop) {
267 // if this particular request will generate a snoop
268 // response
269 if (expect_snoop_resp) {
270 // we should never have an exsiting request outstanding
271 assert(outstandingSnoop.find(pkt->req) ==
272 outstandingSnoop.end());
273 outstandingSnoop.insert(pkt->req);
274
275 // basic sanity check on the outstanding snoops
276 panic_if(outstandingSnoop.size() > 512,
277 "Outstanding snoop requests exceeded 512\n");
278 }
279
280 // remember where to route the normal response to
281 if (expect_response || expect_snoop_resp) {
282 assert(routeTo.find(pkt->req) == routeTo.end());
283 routeTo[pkt->req] = slave_port_id;
284
285 panic_if(routeTo.size() > 512,
286 "Routing table exceeds 512 packets\n");
287 }
288
289 // update the layer state and schedule an idle event
290 reqLayers[master_port_id]->succeededTiming(packetFinishTime);
291 }
292
293 // stats updates only consider packets that were successfully sent
294 pktCount[slave_port_id][master_port_id]++;
295 pktSize[slave_port_id][master_port_id] += pkt_size;
296 transDist[pkt_cmd]++;
297
298 if (is_express_snoop)
299 snoops++;
300 }
301
302 return success;
303}
304
305bool
306CoherentXBar::recvTimingResp(PacketPtr pkt, PortID master_port_id)
307{
308 // determine the source port based on the id
309 MasterPort *src_port = masterPorts[master_port_id];
310
311 // determine the destination
312 const auto route_lookup = routeTo.find(pkt->req);
313 assert(route_lookup != routeTo.end());
314 const PortID slave_port_id = route_lookup->second;
315 assert(slave_port_id != InvalidPortID);
316 assert(slave_port_id < respLayers.size());
317
318 // test if the crossbar should be considered occupied for the
319 // current port
320 if (!respLayers[slave_port_id]->tryTiming(src_port)) {
321 DPRINTF(CoherentXBar, "recvTimingResp: src %s %s 0x%x BUSY\n",
322 src_port->name(), pkt->cmdString(), pkt->getAddr());
323 return false;
324 }
325
326 DPRINTF(CoherentXBar, "recvTimingResp: src %s %s 0x%x\n",
327 src_port->name(), pkt->cmdString(), pkt->getAddr());
328
329 // store size and command as they might be modified when
330 // forwarding the packet
331 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
332 unsigned int pkt_cmd = pkt->cmdToIndex();
333
334 // a response sees the response latency
335 Tick xbar_delay = responseLatency * clockPeriod();
336
337 // set the packet header and payload delay
338 calcPacketTiming(pkt, xbar_delay);
339
340 // determine how long to be crossbar layer is busy
341 Tick packetFinishTime = clockEdge(Cycles(1)) + pkt->payloadDelay;
342
343 if (snoopFilter && !system->bypassCaches()) {
344 // let the snoop filter inspect the response and update its state
345 snoopFilter->updateResponse(pkt, *slavePorts[slave_port_id]);
346 }
347
348 // send the packet through the destination slave port
349 bool success M5_VAR_USED = slavePorts[slave_port_id]->sendTimingResp(pkt);
348 // send the packet through the destination slave port and pay for
349 // any outstanding header delay
350 Tick latency = pkt->headerDelay;
351 pkt->headerDelay = 0;
352 slavePorts[slave_port_id]->schedTimingResp(pkt, curTick() + latency);
353
351 // currently it is illegal to block responses... can lead to
352 // deadlock
353 assert(success);
354
354 // remove the request from the routing table
355 routeTo.erase(route_lookup);
356
357 respLayers[slave_port_id]->succeededTiming(packetFinishTime);
358
359 // stats updates
360 pktCount[slave_port_id][master_port_id]++;
361 pktSize[slave_port_id][master_port_id] += pkt_size;
362 transDist[pkt_cmd]++;
363
364 return true;
365}
366
367void
368CoherentXBar::recvTimingSnoopReq(PacketPtr pkt, PortID master_port_id)
369{
370 DPRINTF(CoherentXBar, "recvTimingSnoopReq: src %s %s 0x%x\n",
371 masterPorts[master_port_id]->name(), pkt->cmdString(),
372 pkt->getAddr());
373
374 // update stats here as we know the forwarding will succeed
375 transDist[pkt->cmdToIndex()]++;
376 snoops++;
377
378 // we should only see express snoops from caches
379 assert(pkt->isExpressSnoop());
380
381 // remeber if the packet is inhibited so we can see if it changes
382 const bool is_inhibited = pkt->memInhibitAsserted();
383
384 if (snoopFilter) {
385 // let the Snoop Filter work its magic and guide probing
386 auto sf_res = snoopFilter->lookupSnoop(pkt);
387 // No timing here: packetFinishTime += sf_res.second * clockPeriod();
388 DPRINTF(CoherentXBar, "recvTimingSnoopReq: src %s %s 0x%x"\
389 " SF size: %i lat: %i\n", masterPorts[master_port_id]->name(),
390 pkt->cmdString(), pkt->getAddr(), sf_res.first.size(),
391 sf_res.second);
392
393 // forward to all snoopers
394 forwardTiming(pkt, InvalidPortID, sf_res.first);
395 } else {
396 forwardTiming(pkt, InvalidPortID);
397 }
398
399 // if we can expect a response, remember how to route it
400 if (!is_inhibited && pkt->memInhibitAsserted()) {
401 assert(routeTo.find(pkt->req) == routeTo.end());
402 routeTo[pkt->req] = master_port_id;
403 }
404
405 // a snoop request came from a connected slave device (one of
406 // our master ports), and if it is not coming from the slave
407 // device responsible for the address range something is
408 // wrong, hence there is nothing further to do as the packet
409 // would be going back to where it came from
410 assert(master_port_id == findPort(pkt->getAddr()));
411}
412
413bool
414CoherentXBar::recvTimingSnoopResp(PacketPtr pkt, PortID slave_port_id)
415{
416 // determine the source port based on the id
417 SlavePort* src_port = slavePorts[slave_port_id];
418
419 // get the destination
420 const auto route_lookup = routeTo.find(pkt->req);
421 assert(route_lookup != routeTo.end());
422 const PortID dest_port_id = route_lookup->second;
423 assert(dest_port_id != InvalidPortID);
424
425 // determine if the response is from a snoop request we
426 // created as the result of a normal request (in which case it
427 // should be in the outstandingSnoop), or if we merely forwarded
428 // someone else's snoop request
429 const bool forwardAsSnoop = outstandingSnoop.find(pkt->req) ==
430 outstandingSnoop.end();
431
432 // test if the crossbar should be considered occupied for the
433 // current port, note that the check is bypassed if the response
434 // is being passed on as a normal response since this is occupying
435 // the response layer rather than the snoop response layer
436 if (forwardAsSnoop) {
437 assert(dest_port_id < snoopLayers.size());
438 if (!snoopLayers[dest_port_id]->tryTiming(src_port)) {
439 DPRINTF(CoherentXBar, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n",
440 src_port->name(), pkt->cmdString(), pkt->getAddr());
441 return false;
442 }
443 } else {
444 // get the master port that mirrors this slave port internally
445 MasterPort* snoop_port = snoopRespPorts[slave_port_id];
446 assert(dest_port_id < respLayers.size());
447 if (!respLayers[dest_port_id]->tryTiming(snoop_port)) {
448 DPRINTF(CoherentXBar, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n",
449 snoop_port->name(), pkt->cmdString(), pkt->getAddr());
450 return false;
451 }
452 }
453
454 DPRINTF(CoherentXBar, "recvTimingSnoopResp: src %s %s 0x%x\n",
455 src_port->name(), pkt->cmdString(), pkt->getAddr());
456
457 // store size and command as they might be modified when
458 // forwarding the packet
459 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
460 unsigned int pkt_cmd = pkt->cmdToIndex();
461
462 // responses are never express snoops
463 assert(!pkt->isExpressSnoop());
464
465 // a snoop response sees the snoop response latency, and if it is
466 // forwarded as a normal response, the response latency
467 Tick xbar_delay =
468 (forwardAsSnoop ? snoopResponseLatency : responseLatency) *
469 clockPeriod();
470
471 // set the packet header and payload delay
472 calcPacketTiming(pkt, xbar_delay);
473
474 // determine how long to be crossbar layer is busy
475 Tick packetFinishTime = clockEdge(Cycles(1)) + pkt->payloadDelay;
476
477 // forward it either as a snoop response or a normal response
478 if (forwardAsSnoop) {
479 // this is a snoop response to a snoop request we forwarded,
480 // e.g. coming from the L1 and going to the L2, and it should
481 // be forwarded as a snoop response
482
483 if (snoopFilter) {
484 // update the probe filter so that it can properly track the line
485 snoopFilter->updateSnoopForward(pkt, *slavePorts[slave_port_id],
486 *masterPorts[dest_port_id]);
487 }
488
489 bool success M5_VAR_USED =
490 masterPorts[dest_port_id]->sendTimingSnoopResp(pkt);
491 pktCount[slave_port_id][dest_port_id]++;
492 pktSize[slave_port_id][dest_port_id] += pkt_size;
493 assert(success);
494
495 snoopLayers[dest_port_id]->succeededTiming(packetFinishTime);
496 } else {
497 // we got a snoop response on one of our slave ports,
498 // i.e. from a coherent master connected to the crossbar, and
499 // since we created the snoop request as part of recvTiming,
500 // this should now be a normal response again
501 outstandingSnoop.erase(pkt->req);
502
503 // this is a snoop response from a coherent master, hence it
504 // should never go back to where the snoop response came from,
505 // but instead to where the original request came from
506 assert(slave_port_id != dest_port_id);
507
508 if (snoopFilter) {
509 // update the probe filter so that it can properly track the line
510 snoopFilter->updateSnoopResponse(pkt, *slavePorts[slave_port_id],
511 *slavePorts[dest_port_id]);
512 }
513
514 DPRINTF(CoherentXBar, "recvTimingSnoopResp: src %s %s 0x%x"\
515 " FWD RESP\n", src_port->name(), pkt->cmdString(),
516 pkt->getAddr());
517
518 // as a normal response, it should go back to a master through
520 // one of our slave ports, at this point we are ignoring the
521 // fact that the response layer could be busy and do not touch
522 // its state
523 bool success M5_VAR_USED =
524 slavePorts[dest_port_id]->sendTimingResp(pkt);
519 // one of our slave ports, we also pay for any outstanding
520 // header latency
521 Tick latency = pkt->headerDelay;
522 pkt->headerDelay = 0;
523 slavePorts[dest_port_id]->schedTimingResp(pkt, curTick() + latency);
524
526 // @todo Put the response in an internal FIFO and pass it on
527 // to the response layer from there
528
529 // currently it is illegal to block responses... can lead
530 // to deadlock
531 assert(success);
532
525 respLayers[dest_port_id]->succeededTiming(packetFinishTime);
526 }
527
528 // remove the request from the routing table
529 routeTo.erase(route_lookup);
530
531 // stats updates
532 transDist[pkt_cmd]++;
533 snoops++;
534
535 return true;
536}
537
538
539void
540CoherentXBar::forwardTiming(PacketPtr pkt, PortID exclude_slave_port_id,
549 const std::vector& dests)
541 const std::vector<QueuedSlavePort*>& dests)
542{
543 DPRINTF(CoherentXBar, "%s for %s address %x size %d\n", __func__,
544 pkt->cmdString(), pkt->getAddr(), pkt->getSize());
545
546 // snoops should only happen if the system isn't bypassing caches
547 assert(!system->bypassCaches());
548
549 unsigned fanout = 0;
550
551 for (const auto& p: dests) {
552 // we could have gotten this request from a snooping master
553 // (corresponding to our own slave port that is also in
554 // snoopPorts) and should not send it back to where it came
555 // from
556 if (exclude_slave_port_id == InvalidPortID ||
557 p->getId() != exclude_slave_port_id) {
558 // cache is not allowed to refuse snoop
559 p->sendTimingSnoopReq(pkt);
560 fanout++;
561 }
562 }
563
564 // Stats for fanout of this forward operation
565 snoopFanout.sample(fanout);
566}
567
568void
569CoherentXBar::recvReqRetry(PortID master_port_id)
570{
571 // responses and snoop responses never block on forwarding them,
572 // so the retry will always be coming from a port to which we
573 // tried to forward a request
574 reqLayers[master_port_id]->recvRetry();
575}
576
577Tick
578CoherentXBar::recvAtomic(PacketPtr pkt, PortID slave_port_id)
579{
580 DPRINTF(CoherentXBar, "recvAtomic: packet src %s addr 0x%x cmd %s\n",
581 slavePorts[slave_port_id]->name(), pkt->getAddr(),
582 pkt->cmdString());
583
584 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
585 unsigned int pkt_cmd = pkt->cmdToIndex();
586
587 MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
588 Tick snoop_response_latency = 0;
589
590 if (!system->bypassCaches()) {
591 // forward to all snoopers but the source
592 std::pair<MemCmd, Tick> snoop_result;
593 if (snoopFilter) {
594 // check with the snoop filter where to forward this packet
595 auto sf_res =
596 snoopFilter->lookupRequest(pkt, *slavePorts[slave_port_id]);
597 snoop_response_latency += sf_res.second * clockPeriod();
598 DPRINTF(CoherentXBar, "%s: src %s %s 0x%x"\
599 " SF size: %i lat: %i\n", __func__,
600 slavePorts[slave_port_id]->name(), pkt->cmdString(),
601 pkt->getAddr(), sf_res.first.size(), sf_res.second);
602 snoop_result = forwardAtomic(pkt, slave_port_id, InvalidPortID,
603 sf_res.first);
604 } else {
605 snoop_result = forwardAtomic(pkt, slave_port_id);
606 }
607 snoop_response_cmd = snoop_result.first;
608 snoop_response_latency += snoop_result.second;
609 }
610
611 // even if we had a snoop response, we must continue and also
612 // perform the actual request at the destination
613 PortID master_port_id = findPort(pkt->getAddr());
614
615 // stats updates for the request
616 pktCount[slave_port_id][master_port_id]++;
617 pktSize[slave_port_id][master_port_id] += pkt_size;
618 transDist[pkt_cmd]++;
619
620 // forward the request to the appropriate destination
621 Tick response_latency = masterPorts[master_port_id]->sendAtomic(pkt);
622
623 // Lower levels have replied, tell the snoop filter
624 if (snoopFilter && !system->bypassCaches() && pkt->isResponse()) {
625 snoopFilter->updateResponse(pkt, *slavePorts[slave_port_id]);
626 }
627
628 // if we got a response from a snooper, restore it here
629 if (snoop_response_cmd != MemCmd::InvalidCmd) {
630 // no one else should have responded
631 assert(!pkt->isResponse());
632 pkt->cmd = snoop_response_cmd;
633 response_latency = snoop_response_latency;
634 }
635
636 // add the response data
637 if (pkt->isResponse()) {
638 pkt_size = pkt->hasData() ? pkt->getSize() : 0;
639 pkt_cmd = pkt->cmdToIndex();
640
641 // stats updates
642 pktCount[slave_port_id][master_port_id]++;
643 pktSize[slave_port_id][master_port_id] += pkt_size;
644 transDist[pkt_cmd]++;
645 }
646
647 // @todo: Not setting header time
648 pkt->payloadDelay = response_latency;
649 return response_latency;
650}
651
652Tick
653CoherentXBar::recvAtomicSnoop(PacketPtr pkt, PortID master_port_id)
654{
655 DPRINTF(CoherentXBar, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n",
656 masterPorts[master_port_id]->name(), pkt->getAddr(),
657 pkt->cmdString());
658
659 // add the request snoop data
660 snoops++;
661
662 // forward to all snoopers
663 std::pair<MemCmd, Tick> snoop_result;
664 Tick snoop_response_latency = 0;
665 if (snoopFilter) {
666 auto sf_res = snoopFilter->lookupSnoop(pkt);
667 snoop_response_latency += sf_res.second * clockPeriod();
668 DPRINTF(CoherentXBar, "%s: src %s %s 0x%x SF size: %i lat: %i\n",
669 __func__, masterPorts[master_port_id]->name(), pkt->cmdString(),
670 pkt->getAddr(), sf_res.first.size(), sf_res.second);
671 snoop_result = forwardAtomic(pkt, InvalidPortID, master_port_id,
672 sf_res.first);
673 } else {
674 snoop_result = forwardAtomic(pkt, InvalidPortID);
675 }
676 MemCmd snoop_response_cmd = snoop_result.first;
677 snoop_response_latency += snoop_result.second;
678
679 if (snoop_response_cmd != MemCmd::InvalidCmd)
680 pkt->cmd = snoop_response_cmd;
681
682 // add the response snoop data
683 if (pkt->isResponse()) {
684 snoops++;
685 }
686
687 // @todo: Not setting header time
688 pkt->payloadDelay = snoop_response_latency;
689 return snoop_response_latency;
690}
691
692std::pair<MemCmd, Tick>
693CoherentXBar::forwardAtomic(PacketPtr pkt, PortID exclude_slave_port_id,
694 PortID source_master_port_id,
703 const std::vector& dests)
695 const std::vector<QueuedSlavePort*>& dests)
696{
697 // the packet may be changed on snoops, record the original
698 // command to enable us to restore it between snoops so that
699 // additional snoops can take place properly
700 MemCmd orig_cmd = pkt->cmd;
701 MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
702 Tick snoop_response_latency = 0;
703
704 // snoops should only happen if the system isn't bypassing caches
705 assert(!system->bypassCaches());
706
707 unsigned fanout = 0;
708
709 for (const auto& p: dests) {
710 // we could have gotten this request from a snooping master
711 // (corresponding to our own slave port that is also in
712 // snoopPorts) and should not send it back to where it came
713 // from
714 if (exclude_slave_port_id != InvalidPortID &&
715 p->getId() == exclude_slave_port_id)
716 continue;
717
718 Tick latency = p->sendAtomicSnoop(pkt);
719 fanout++;
720
721 // in contrast to a functional access, we have to keep on
722 // going as all snoopers must be updated even if we get a
723 // response
724 if (!pkt->isResponse())
725 continue;
726
727 // response from snoop agent
728 assert(pkt->cmd != orig_cmd);
729 assert(pkt->memInhibitAsserted());
730 // should only happen once
731 assert(snoop_response_cmd == MemCmd::InvalidCmd);
732 // save response state
733 snoop_response_cmd = pkt->cmd;
734 snoop_response_latency = latency;
735
736 if (snoopFilter) {
737 // Handle responses by the snoopers and differentiate between
738 // responses to requests from above and snoops from below
739 if (source_master_port_id != InvalidPortID) {
740 // Getting a response for a snoop from below
741 assert(exclude_slave_port_id == InvalidPortID);
742 snoopFilter->updateSnoopForward(pkt, *p,
743 *masterPorts[source_master_port_id]);
744 } else {
745 // Getting a response for a request from above
746 assert(source_master_port_id == InvalidPortID);
747 snoopFilter->updateSnoopResponse(pkt, *p,
748 *slavePorts[exclude_slave_port_id]);
749 }
750 }
751 // restore original packet state for remaining snoopers
752 pkt->cmd = orig_cmd;
753 }
754
755 // Stats for fanout
756 snoopFanout.sample(fanout);
757
758 // the packet is restored as part of the loop and any potential
759 // snoop response is part of the returned pair
760 return std::make_pair(snoop_response_cmd, snoop_response_latency);
761}
762
763void
764CoherentXBar::recvFunctional(PacketPtr pkt, PortID slave_port_id)
765{
766 if (!pkt->isPrint()) {
767 // don't do DPRINTFs on PrintReq as it clutters up the output
768 DPRINTF(CoherentXBar,
769 "recvFunctional: packet src %s addr 0x%x cmd %s\n",
770 slavePorts[slave_port_id]->name(), pkt->getAddr(),
771 pkt->cmdString());
772 }
773
774 if (!system->bypassCaches()) {
775 // forward to all snoopers but the source
776 forwardFunctional(pkt, slave_port_id);
777 }
778
779 // there is no need to continue if the snooping has found what we
780 // were looking for and the packet is already a response
781 if (!pkt->isResponse()) {
782 // since our slave ports are queued ports we need to check them as well
783 for (const auto& p : slavePorts) {
784 // if we find a response that has the data, then the
785 // downstream caches/memories may be out of date, so simply stop
786 // here
787 if (p->checkFunctional(pkt)) {
788 if (pkt->needsResponse())
789 pkt->makeResponse();
790 return;
791 }
792 }
793
794 PortID dest_id = findPort(pkt->getAddr());
795
796 masterPorts[dest_id]->sendFunctional(pkt);
797 }
798}
799
800void
801CoherentXBar::recvFunctionalSnoop(PacketPtr pkt, PortID master_port_id)
802{
803 if (!pkt->isPrint()) {
804 // don't do DPRINTFs on PrintReq as it clutters up the output
805 DPRINTF(CoherentXBar,
806 "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n",
807 masterPorts[master_port_id]->name(), pkt->getAddr(),
808 pkt->cmdString());
809 }
810
811 // forward to all snoopers
812 forwardFunctional(pkt, InvalidPortID);
813}
814
815void
816CoherentXBar::forwardFunctional(PacketPtr pkt, PortID exclude_slave_port_id)
817{
818 // snoops should only happen if the system isn't bypassing caches
819 assert(!system->bypassCaches());
820
821 for (const auto& p: snoopPorts) {
822 // we could have gotten this request from a snooping master
823 // (corresponding to our own slave port that is also in
824 // snoopPorts) and should not send it back to where it came
825 // from
826 if (exclude_slave_port_id == InvalidPortID ||
827 p->getId() != exclude_slave_port_id)
828 p->sendFunctionalSnoop(pkt);
829
830 // if we get a response we are done
831 if (pkt->isResponse()) {
832 break;
833 }
834 }
835}
836
837unsigned int
838CoherentXBar::drain(DrainManager *dm)
839{
840 // sum up the individual layers
841 unsigned int total = 0;
842 for (auto l: reqLayers)
843 total += l->drain(dm);
844 for (auto l: respLayers)
845 total += l->drain(dm);
846 for (auto l: snoopLayers)
847 total += l->drain(dm);
848 return total;
849}
850
851void
852CoherentXBar::regStats()
853{
854 // register the stats of the base class and our layers
855 BaseXBar::regStats();
856 for (auto l: reqLayers)
857 l->regStats();
858 for (auto l: respLayers)
859 l->regStats();
860 for (auto l: snoopLayers)
861 l->regStats();
862
863 snoops
864 .name(name() + ".snoops")
865 .desc("Total snoops (count)")
866 ;
867
868 snoopFanout
869 .init(0, snoopPorts.size(), 1)
870 .name(name() + ".snoop_fanout")
871 .desc("Request fanout histogram")
872 ;
873}
874
875CoherentXBar *
876CoherentXBarParams::create()
877{
878 return new CoherentXBar(this);
879}