2 * Copyright (c) 2011-2017 ARM Limited
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.
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
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.
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.
47 * Definition of a crossbar object.
50 #include "mem/coherent_xbar.hh"
52 #include "base/misc.hh"
53 #include "base/trace.hh"
54 #include "debug/AddrRanges.hh"
55 #include "debug/CoherentXBar.hh"
56 #include "sim/system.hh"
58 CoherentXBar::CoherentXBar(const CoherentXBarParams
*p
)
59 : BaseXBar(p
), system(p
->system
), snoopFilter(p
->snoop_filter
),
60 snoopResponseLatency(p
->snoop_response_latency
),
61 pointOfCoherency(p
->point_of_coherency
)
63 // create the ports based on the size of the master and slave
64 // vector ports, and the presence of the default port, the ports
65 // are enumerated starting from zero
66 for (int i
= 0; i
< p
->port_master_connection_count
; ++i
) {
67 std::string portName
= csprintf("%s.master[%d]", name(), i
);
68 MasterPort
* bp
= new CoherentXBarMasterPort(portName
, *this, i
);
69 masterPorts
.push_back(bp
);
70 reqLayers
.push_back(new ReqLayer(*bp
, *this,
71 csprintf(".reqLayer%d", i
)));
72 snoopLayers
.push_back(new SnoopRespLayer(*bp
, *this,
73 csprintf(".snoopLayer%d", i
)));
76 // see if we have a default slave device connected and if so add
77 // our corresponding master port
78 if (p
->port_default_connection_count
) {
79 defaultPortID
= masterPorts
.size();
80 std::string portName
= name() + ".default";
81 MasterPort
* bp
= new CoherentXBarMasterPort(portName
, *this,
83 masterPorts
.push_back(bp
);
84 reqLayers
.push_back(new ReqLayer(*bp
, *this, csprintf(".reqLayer%d",
86 snoopLayers
.push_back(new SnoopRespLayer(*bp
, *this,
87 csprintf(".snoopLayer%d",
91 // create the slave ports, once again starting at zero
92 for (int i
= 0; i
< p
->port_slave_connection_count
; ++i
) {
93 std::string portName
= csprintf("%s.slave[%d]", name(), i
);
94 QueuedSlavePort
* bp
= new CoherentXBarSlavePort(portName
, *this, i
);
95 slavePorts
.push_back(bp
);
96 respLayers
.push_back(new RespLayer(*bp
, *this,
97 csprintf(".respLayer%d", i
)));
98 snoopRespPorts
.push_back(new SnoopRespPort(*bp
, *this));
104 CoherentXBar::~CoherentXBar()
106 for (auto l
: reqLayers
)
108 for (auto l
: respLayers
)
110 for (auto l
: snoopLayers
)
112 for (auto p
: snoopRespPorts
)
121 // iterate over our slave ports and determine which of our
122 // neighbouring master ports are snooping and add them as snoopers
123 for (const auto& p
: slavePorts
) {
124 // check if the connected master port is snooping
125 if (p
->isSnooping()) {
126 DPRINTF(AddrRanges
, "Adding snooping master %s\n",
127 p
->getMasterPort().name());
128 snoopPorts
.push_back(p
);
132 if (snoopPorts
.empty())
133 warn("CoherentXBar %s has no snooping ports attached!\n", name());
135 // inform the snoop filter about the slave ports so it can create
136 // its own internal representation
138 snoopFilter
->setSlavePorts(slavePorts
);
142 CoherentXBar::recvTimingReq(PacketPtr pkt
, PortID slave_port_id
)
144 // determine the source port based on the id
145 SlavePort
*src_port
= slavePorts
[slave_port_id
];
147 // remember if the packet is an express snoop
148 bool is_express_snoop
= pkt
->isExpressSnoop();
149 bool cache_responding
= pkt
->cacheResponding();
150 // for normal requests, going downstream, the express snoop flag
151 // and the cache responding flag should always be the same
152 assert(is_express_snoop
== cache_responding
);
154 // determine the destination based on the address
155 PortID master_port_id
= findPort(pkt
->getAddr());
157 // test if the crossbar should be considered occupied for the current
158 // port, and exclude express snoops from the check
159 if (!is_express_snoop
&& !reqLayers
[master_port_id
]->tryTiming(src_port
)) {
160 DPRINTF(CoherentXBar
, "%s: src %s packet %s BUSY\n", __func__
,
161 src_port
->name(), pkt
->print());
165 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
166 src_port
->name(), pkt
->print());
168 // store size and command as they might be modified when
169 // forwarding the packet
170 unsigned int pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
171 unsigned int pkt_cmd
= pkt
->cmdToIndex();
173 // store the old header delay so we can restore it if needed
174 Tick old_header_delay
= pkt
->headerDelay
;
176 // a request sees the frontend and forward latency
177 Tick xbar_delay
= (frontendLatency
+ forwardLatency
) * clockPeriod();
179 // set the packet header and payload delay
180 calcPacketTiming(pkt
, xbar_delay
);
182 // determine how long to be crossbar layer is busy
183 Tick packetFinishTime
= clockEdge(Cycles(1)) + pkt
->payloadDelay
;
185 if (!system
->bypassCaches()) {
186 assert(pkt
->snoopDelay
== 0);
188 // the packet is a memory-mapped request and should be
189 // broadcasted to our snoopers but the source
191 // check with the snoop filter where to forward this packet
192 auto sf_res
= snoopFilter
->lookupRequest(pkt
, *src_port
);
193 // the time required by a packet to be delivered through
194 // the xbar has to be charged also with to lookup latency
195 // of the snoop filter
196 pkt
->headerDelay
+= sf_res
.second
* clockPeriod();
197 DPRINTF(CoherentXBar
, "%s: src %s packet %s SF size: %i lat: %i\n",
198 __func__
, src_port
->name(), pkt
->print(),
199 sf_res
.first
.size(), sf_res
.second
);
201 if (pkt
->isEviction()) {
202 // for block-evicting packets, i.e. writebacks and
203 // clean evictions, there is no need to snoop up, as
204 // all we do is determine if the block is cached or
205 // not, instead just set it here based on the snoop
207 if (!sf_res
.first
.empty())
208 pkt
->setBlockCached();
210 forwardTiming(pkt
, slave_port_id
, sf_res
.first
);
213 forwardTiming(pkt
, slave_port_id
);
216 // add the snoop delay to our header delay, and then reset it
217 pkt
->headerDelay
+= pkt
->snoopDelay
;
221 // set up a sensible starting point
224 // remember if the packet will generate a snoop response by
225 // checking if a cache set the cacheResponding flag during the
227 const bool expect_snoop_resp
= !cache_responding
&& pkt
->cacheResponding();
228 bool expect_response
= pkt
->needsResponse() && !pkt
->cacheResponding();
230 const bool sink_packet
= sinkPacket(pkt
);
232 // in certain cases the crossbar is responsible for responding
233 bool respond_directly
= false;
234 // store the original address as an address mapper could possibly
235 // modify the address upon a sendTimingRequest
236 const Addr
addr(pkt
->getAddr());
238 DPRINTF(CoherentXBar
, "%s: Not forwarding %s\n", __func__
,
241 // determine if we are forwarding the packet, or responding to
243 if (!pointOfCoherency
|| pkt
->isRead() || pkt
->isWrite()) {
244 // if we are passing on, rather than sinking, a packet to
245 // which an upstream cache has committed to responding,
246 // the line was needs writable, and the responding only
247 // had an Owned copy, so we need to immidiately let the
248 // downstream caches know, bypass any flow control
249 if (pkt
->cacheResponding()) {
250 pkt
->setExpressSnoop();
253 // since it is a normal request, attempt to send the packet
254 success
= masterPorts
[master_port_id
]->sendTimingReq(pkt
);
256 // no need to forward, turn this packet around and respond
258 assert(pkt
->needsResponse());
260 respond_directly
= true;
261 assert(!expect_snoop_resp
);
262 expect_response
= false;
266 if (snoopFilter
&& !system
->bypassCaches()) {
267 // Let the snoop filter know about the success of the send operation
268 snoopFilter
->finishRequest(!success
, addr
, pkt
->isSecure());
271 // check if we were successful in sending the packet onwards
273 // express snoops should never be forced to retry
274 assert(!is_express_snoop
);
276 // restore the header delay
277 pkt
->headerDelay
= old_header_delay
;
279 DPRINTF(CoherentXBar
, "%s: src %s packet %s RETRY\n", __func__
,
280 src_port
->name(), pkt
->print());
282 // update the layer state and schedule an idle event
283 reqLayers
[master_port_id
]->failedTiming(src_port
,
284 clockEdge(Cycles(1)));
286 // express snoops currently bypass the crossbar state entirely
287 if (!is_express_snoop
) {
288 // if this particular request will generate a snoop
290 if (expect_snoop_resp
) {
291 // we should never have an exsiting request outstanding
292 assert(outstandingSnoop
.find(pkt
->req
) ==
293 outstandingSnoop
.end());
294 outstandingSnoop
.insert(pkt
->req
);
296 // basic sanity check on the outstanding snoops
297 panic_if(outstandingSnoop
.size() > 512,
298 "Outstanding snoop requests exceeded 512\n");
301 // remember where to route the normal response to
302 if (expect_response
|| expect_snoop_resp
) {
303 assert(routeTo
.find(pkt
->req
) == routeTo
.end());
304 routeTo
[pkt
->req
] = slave_port_id
;
306 panic_if(routeTo
.size() > 512,
307 "Routing table exceeds 512 packets\n");
310 // update the layer state and schedule an idle event
311 reqLayers
[master_port_id
]->succeededTiming(packetFinishTime
);
314 // stats updates only consider packets that were successfully sent
315 pktCount
[slave_port_id
][master_port_id
]++;
316 pktSize
[slave_port_id
][master_port_id
] += pkt_size
;
317 transDist
[pkt_cmd
]++;
319 if (is_express_snoop
) {
321 snoopTraffic
+= pkt_size
;
326 // queue the packet for deletion
327 pendingDelete
.reset(pkt
);
329 if (respond_directly
) {
330 assert(pkt
->needsResponse());
335 if (snoopFilter
&& !system
->bypassCaches()) {
336 // let the snoop filter inspect the response and update its state
337 snoopFilter
->updateResponse(pkt
, *slavePorts
[slave_port_id
]);
340 Tick response_time
= clockEdge() + pkt
->headerDelay
;
341 pkt
->headerDelay
= 0;
343 slavePorts
[slave_port_id
]->schedTimingResp(pkt
, response_time
);
350 CoherentXBar::recvTimingResp(PacketPtr pkt
, PortID master_port_id
)
352 // determine the source port based on the id
353 MasterPort
*src_port
= masterPorts
[master_port_id
];
355 // determine the destination
356 const auto route_lookup
= routeTo
.find(pkt
->req
);
357 assert(route_lookup
!= routeTo
.end());
358 const PortID slave_port_id
= route_lookup
->second
;
359 assert(slave_port_id
!= InvalidPortID
);
360 assert(slave_port_id
< respLayers
.size());
362 // test if the crossbar should be considered occupied for the
364 if (!respLayers
[slave_port_id
]->tryTiming(src_port
)) {
365 DPRINTF(CoherentXBar
, "%s: src %s packet %s BUSY\n", __func__
,
366 src_port
->name(), pkt
->print());
370 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
371 src_port
->name(), pkt
->print());
373 // store size and command as they might be modified when
374 // forwarding the packet
375 unsigned int pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
376 unsigned int pkt_cmd
= pkt
->cmdToIndex();
378 // a response sees the response latency
379 Tick xbar_delay
= responseLatency
* clockPeriod();
381 // set the packet header and payload delay
382 calcPacketTiming(pkt
, xbar_delay
);
384 // determine how long to be crossbar layer is busy
385 Tick packetFinishTime
= clockEdge(Cycles(1)) + pkt
->payloadDelay
;
387 if (snoopFilter
&& !system
->bypassCaches()) {
388 // let the snoop filter inspect the response and update its state
389 snoopFilter
->updateResponse(pkt
, *slavePorts
[slave_port_id
]);
392 // send the packet through the destination slave port and pay for
393 // any outstanding header delay
394 Tick latency
= pkt
->headerDelay
;
395 pkt
->headerDelay
= 0;
396 slavePorts
[slave_port_id
]->schedTimingResp(pkt
, curTick() + latency
);
398 // remove the request from the routing table
399 routeTo
.erase(route_lookup
);
401 respLayers
[slave_port_id
]->succeededTiming(packetFinishTime
);
404 pktCount
[slave_port_id
][master_port_id
]++;
405 pktSize
[slave_port_id
][master_port_id
] += pkt_size
;
406 transDist
[pkt_cmd
]++;
412 CoherentXBar::recvTimingSnoopReq(PacketPtr pkt
, PortID master_port_id
)
414 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
415 masterPorts
[master_port_id
]->name(), pkt
->print());
417 // update stats here as we know the forwarding will succeed
418 unsigned int pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
419 transDist
[pkt
->cmdToIndex()]++;
421 snoopTraffic
+= pkt_size
;
423 // we should only see express snoops from caches
424 assert(pkt
->isExpressSnoop());
426 // set the packet header and payload delay, for now use forward latency
427 // @todo Assess the choice of latency further
428 calcPacketTiming(pkt
, forwardLatency
* clockPeriod());
430 // remember if a cache has already committed to responding so we
431 // can see if it changes during the snooping
432 const bool cache_responding
= pkt
->cacheResponding();
434 assert(pkt
->snoopDelay
== 0);
437 // let the Snoop Filter work its magic and guide probing
438 auto sf_res
= snoopFilter
->lookupSnoop(pkt
);
439 // the time required by a packet to be delivered through
440 // the xbar has to be charged also with to lookup latency
441 // of the snoop filter
442 pkt
->headerDelay
+= sf_res
.second
* clockPeriod();
443 DPRINTF(CoherentXBar
, "%s: src %s packet %s SF size: %i lat: %i\n",
444 __func__
, masterPorts
[master_port_id
]->name(), pkt
->print(),
445 sf_res
.first
.size(), sf_res
.second
);
447 // forward to all snoopers
448 forwardTiming(pkt
, InvalidPortID
, sf_res
.first
);
450 forwardTiming(pkt
, InvalidPortID
);
453 // add the snoop delay to our header delay, and then reset it
454 pkt
->headerDelay
+= pkt
->snoopDelay
;
457 // if we can expect a response, remember how to route it
458 if (!cache_responding
&& pkt
->cacheResponding()) {
459 assert(routeTo
.find(pkt
->req
) == routeTo
.end());
460 routeTo
[pkt
->req
] = master_port_id
;
463 // a snoop request came from a connected slave device (one of
464 // our master ports), and if it is not coming from the slave
465 // device responsible for the address range something is
466 // wrong, hence there is nothing further to do as the packet
467 // would be going back to where it came from
468 assert(master_port_id
== findPort(pkt
->getAddr()));
472 CoherentXBar::recvTimingSnoopResp(PacketPtr pkt
, PortID slave_port_id
)
474 // determine the source port based on the id
475 SlavePort
* src_port
= slavePorts
[slave_port_id
];
477 // get the destination
478 const auto route_lookup
= routeTo
.find(pkt
->req
);
479 assert(route_lookup
!= routeTo
.end());
480 const PortID dest_port_id
= route_lookup
->second
;
481 assert(dest_port_id
!= InvalidPortID
);
483 // determine if the response is from a snoop request we
484 // created as the result of a normal request (in which case it
485 // should be in the outstandingSnoop), or if we merely forwarded
486 // someone else's snoop request
487 const bool forwardAsSnoop
= outstandingSnoop
.find(pkt
->req
) ==
488 outstandingSnoop
.end();
490 // test if the crossbar should be considered occupied for the
491 // current port, note that the check is bypassed if the response
492 // is being passed on as a normal response since this is occupying
493 // the response layer rather than the snoop response layer
494 if (forwardAsSnoop
) {
495 assert(dest_port_id
< snoopLayers
.size());
496 if (!snoopLayers
[dest_port_id
]->tryTiming(src_port
)) {
497 DPRINTF(CoherentXBar
, "%s: src %s packet %s BUSY\n", __func__
,
498 src_port
->name(), pkt
->print());
502 // get the master port that mirrors this slave port internally
503 MasterPort
* snoop_port
= snoopRespPorts
[slave_port_id
];
504 assert(dest_port_id
< respLayers
.size());
505 if (!respLayers
[dest_port_id
]->tryTiming(snoop_port
)) {
506 DPRINTF(CoherentXBar
, "%s: src %s packet %s BUSY\n", __func__
,
507 snoop_port
->name(), pkt
->print());
512 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
513 src_port
->name(), pkt
->print());
515 // store size and command as they might be modified when
516 // forwarding the packet
517 unsigned int pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
518 unsigned int pkt_cmd
= pkt
->cmdToIndex();
520 // responses are never express snoops
521 assert(!pkt
->isExpressSnoop());
523 // a snoop response sees the snoop response latency, and if it is
524 // forwarded as a normal response, the response latency
526 (forwardAsSnoop
? snoopResponseLatency
: responseLatency
) *
529 // set the packet header and payload delay
530 calcPacketTiming(pkt
, xbar_delay
);
532 // determine how long to be crossbar layer is busy
533 Tick packetFinishTime
= clockEdge(Cycles(1)) + pkt
->payloadDelay
;
535 // forward it either as a snoop response or a normal response
536 if (forwardAsSnoop
) {
537 // this is a snoop response to a snoop request we forwarded,
538 // e.g. coming from the L1 and going to the L2, and it should
539 // be forwarded as a snoop response
542 // update the probe filter so that it can properly track the line
543 snoopFilter
->updateSnoopForward(pkt
, *slavePorts
[slave_port_id
],
544 *masterPorts
[dest_port_id
]);
547 bool success M5_VAR_USED
=
548 masterPorts
[dest_port_id
]->sendTimingSnoopResp(pkt
);
549 pktCount
[slave_port_id
][dest_port_id
]++;
550 pktSize
[slave_port_id
][dest_port_id
] += pkt_size
;
553 snoopLayers
[dest_port_id
]->succeededTiming(packetFinishTime
);
555 // we got a snoop response on one of our slave ports,
556 // i.e. from a coherent master connected to the crossbar, and
557 // since we created the snoop request as part of recvTiming,
558 // this should now be a normal response again
559 outstandingSnoop
.erase(pkt
->req
);
561 // this is a snoop response from a coherent master, hence it
562 // should never go back to where the snoop response came from,
563 // but instead to where the original request came from
564 assert(slave_port_id
!= dest_port_id
);
567 // update the probe filter so that it can properly track the line
568 snoopFilter
->updateSnoopResponse(pkt
, *slavePorts
[slave_port_id
],
569 *slavePorts
[dest_port_id
]);
572 DPRINTF(CoherentXBar
, "%s: src %s packet %s FWD RESP\n", __func__
,
573 src_port
->name(), pkt
->print());
575 // as a normal response, it should go back to a master through
576 // one of our slave ports, we also pay for any outstanding
578 Tick latency
= pkt
->headerDelay
;
579 pkt
->headerDelay
= 0;
580 slavePorts
[dest_port_id
]->schedTimingResp(pkt
, curTick() + latency
);
582 respLayers
[dest_port_id
]->succeededTiming(packetFinishTime
);
585 // remove the request from the routing table
586 routeTo
.erase(route_lookup
);
589 transDist
[pkt_cmd
]++;
591 snoopTraffic
+= pkt_size
;
598 CoherentXBar::forwardTiming(PacketPtr pkt
, PortID exclude_slave_port_id
,
599 const std::vector
<QueuedSlavePort
*>& dests
)
601 DPRINTF(CoherentXBar
, "%s for %s\n", __func__
, pkt
->print());
603 // snoops should only happen if the system isn't bypassing caches
604 assert(!system
->bypassCaches());
608 for (const auto& p
: dests
) {
609 // we could have gotten this request from a snooping master
610 // (corresponding to our own slave port that is also in
611 // snoopPorts) and should not send it back to where it came
613 if (exclude_slave_port_id
== InvalidPortID
||
614 p
->getId() != exclude_slave_port_id
) {
615 // cache is not allowed to refuse snoop
616 p
->sendTimingSnoopReq(pkt
);
621 // Stats for fanout of this forward operation
622 snoopFanout
.sample(fanout
);
626 CoherentXBar::recvReqRetry(PortID master_port_id
)
628 // responses and snoop responses never block on forwarding them,
629 // so the retry will always be coming from a port to which we
630 // tried to forward a request
631 reqLayers
[master_port_id
]->recvRetry();
635 CoherentXBar::recvAtomic(PacketPtr pkt
, PortID slave_port_id
)
637 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
638 slavePorts
[slave_port_id
]->name(), pkt
->print());
640 unsigned int pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
641 unsigned int pkt_cmd
= pkt
->cmdToIndex();
643 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
644 Tick snoop_response_latency
= 0;
646 if (!system
->bypassCaches()) {
647 // forward to all snoopers but the source
648 std::pair
<MemCmd
, Tick
> snoop_result
;
650 // check with the snoop filter where to forward this packet
652 snoopFilter
->lookupRequest(pkt
, *slavePorts
[slave_port_id
]);
653 snoop_response_latency
+= sf_res
.second
* clockPeriod();
654 DPRINTF(CoherentXBar
, "%s: src %s packet %s SF size: %i lat: %i\n",
655 __func__
, slavePorts
[slave_port_id
]->name(), pkt
->print(),
656 sf_res
.first
.size(), sf_res
.second
);
658 // let the snoop filter know about the success of the send
659 // operation, and do it even before sending it onwards to
660 // avoid situations where atomic upward snoops sneak in
661 // between and change the filter state
662 snoopFilter
->finishRequest(false, pkt
->getAddr(), pkt
->isSecure());
664 if (pkt
->isEviction()) {
665 // for block-evicting packets, i.e. writebacks and
666 // clean evictions, there is no need to snoop up, as
667 // all we do is determine if the block is cached or
668 // not, instead just set it here based on the snoop
670 if (!sf_res
.first
.empty())
671 pkt
->setBlockCached();
673 snoop_result
= forwardAtomic(pkt
, slave_port_id
, InvalidPortID
,
677 snoop_result
= forwardAtomic(pkt
, slave_port_id
);
679 snoop_response_cmd
= snoop_result
.first
;
680 snoop_response_latency
+= snoop_result
.second
;
683 // set up a sensible default value
684 Tick response_latency
= 0;
686 const bool sink_packet
= sinkPacket(pkt
);
688 // even if we had a snoop response, we must continue and also
689 // perform the actual request at the destination
690 PortID master_port_id
= findPort(pkt
->getAddr());
693 DPRINTF(CoherentXBar
, "%s: Not forwarding %s\n", __func__
,
696 if (!pointOfCoherency
|| pkt
->isRead() || pkt
->isWrite()) {
697 // forward the request to the appropriate destination
698 response_latency
= masterPorts
[master_port_id
]->sendAtomic(pkt
);
700 // if it does not need a response we sink the packet above
701 assert(pkt
->needsResponse());
707 // stats updates for the request
708 pktCount
[slave_port_id
][master_port_id
]++;
709 pktSize
[slave_port_id
][master_port_id
] += pkt_size
;
710 transDist
[pkt_cmd
]++;
713 // if lower levels have replied, tell the snoop filter
714 if (!system
->bypassCaches() && snoopFilter
&& pkt
->isResponse()) {
715 snoopFilter
->updateResponse(pkt
, *slavePorts
[slave_port_id
]);
718 // if we got a response from a snooper, restore it here
719 if (snoop_response_cmd
!= MemCmd::InvalidCmd
) {
720 // no one else should have responded
721 assert(!pkt
->isResponse());
722 pkt
->cmd
= snoop_response_cmd
;
723 response_latency
= snoop_response_latency
;
726 // add the response data
727 if (pkt
->isResponse()) {
728 pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
729 pkt_cmd
= pkt
->cmdToIndex();
732 pktCount
[slave_port_id
][master_port_id
]++;
733 pktSize
[slave_port_id
][master_port_id
] += pkt_size
;
734 transDist
[pkt_cmd
]++;
737 // @todo: Not setting header time
738 pkt
->payloadDelay
= response_latency
;
739 return response_latency
;
743 CoherentXBar::recvAtomicSnoop(PacketPtr pkt
, PortID master_port_id
)
745 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
746 masterPorts
[master_port_id
]->name(), pkt
->print());
748 // add the request snoop data
749 unsigned int pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
751 snoopTraffic
+= pkt_size
;
753 // forward to all snoopers
754 std::pair
<MemCmd
, Tick
> snoop_result
;
755 Tick snoop_response_latency
= 0;
757 auto sf_res
= snoopFilter
->lookupSnoop(pkt
);
758 snoop_response_latency
+= sf_res
.second
* clockPeriod();
759 DPRINTF(CoherentXBar
, "%s: src %s packet %s SF size: %i lat: %i\n",
760 __func__
, masterPorts
[master_port_id
]->name(), pkt
->print(),
761 sf_res
.first
.size(), sf_res
.second
);
762 snoop_result
= forwardAtomic(pkt
, InvalidPortID
, master_port_id
,
765 snoop_result
= forwardAtomic(pkt
, InvalidPortID
);
767 MemCmd snoop_response_cmd
= snoop_result
.first
;
768 snoop_response_latency
+= snoop_result
.second
;
770 if (snoop_response_cmd
!= MemCmd::InvalidCmd
)
771 pkt
->cmd
= snoop_response_cmd
;
773 // add the response snoop data
774 if (pkt
->isResponse()) {
778 // @todo: Not setting header time
779 pkt
->payloadDelay
= snoop_response_latency
;
780 return snoop_response_latency
;
783 std::pair
<MemCmd
, Tick
>
784 CoherentXBar::forwardAtomic(PacketPtr pkt
, PortID exclude_slave_port_id
,
785 PortID source_master_port_id
,
786 const std::vector
<QueuedSlavePort
*>& dests
)
788 // the packet may be changed on snoops, record the original
789 // command to enable us to restore it between snoops so that
790 // additional snoops can take place properly
791 MemCmd orig_cmd
= pkt
->cmd
;
792 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
793 Tick snoop_response_latency
= 0;
795 // snoops should only happen if the system isn't bypassing caches
796 assert(!system
->bypassCaches());
800 for (const auto& p
: dests
) {
801 // we could have gotten this request from a snooping master
802 // (corresponding to our own slave port that is also in
803 // snoopPorts) and should not send it back to where it came
805 if (exclude_slave_port_id
!= InvalidPortID
&&
806 p
->getId() == exclude_slave_port_id
)
809 Tick latency
= p
->sendAtomicSnoop(pkt
);
812 // in contrast to a functional access, we have to keep on
813 // going as all snoopers must be updated even if we get a
815 if (!pkt
->isResponse())
818 // response from snoop agent
819 assert(pkt
->cmd
!= orig_cmd
);
820 assert(pkt
->cacheResponding());
821 // should only happen once
822 assert(snoop_response_cmd
== MemCmd::InvalidCmd
);
823 // save response state
824 snoop_response_cmd
= pkt
->cmd
;
825 snoop_response_latency
= latency
;
828 // Handle responses by the snoopers and differentiate between
829 // responses to requests from above and snoops from below
830 if (source_master_port_id
!= InvalidPortID
) {
831 // Getting a response for a snoop from below
832 assert(exclude_slave_port_id
== InvalidPortID
);
833 snoopFilter
->updateSnoopForward(pkt
, *p
,
834 *masterPorts
[source_master_port_id
]);
836 // Getting a response for a request from above
837 assert(source_master_port_id
== InvalidPortID
);
838 snoopFilter
->updateSnoopResponse(pkt
, *p
,
839 *slavePorts
[exclude_slave_port_id
]);
842 // restore original packet state for remaining snoopers
847 snoopFanout
.sample(fanout
);
849 // the packet is restored as part of the loop and any potential
850 // snoop response is part of the returned pair
851 return std::make_pair(snoop_response_cmd
, snoop_response_latency
);
855 CoherentXBar::recvFunctional(PacketPtr pkt
, PortID slave_port_id
)
857 if (!pkt
->isPrint()) {
858 // don't do DPRINTFs on PrintReq as it clutters up the output
859 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
860 slavePorts
[slave_port_id
]->name(), pkt
->print());
863 if (!system
->bypassCaches()) {
864 // forward to all snoopers but the source
865 forwardFunctional(pkt
, slave_port_id
);
868 // there is no need to continue if the snooping has found what we
869 // were looking for and the packet is already a response
870 if (!pkt
->isResponse()) {
871 // since our slave ports are queued ports we need to check them as well
872 for (const auto& p
: slavePorts
) {
873 // if we find a response that has the data, then the
874 // downstream caches/memories may be out of date, so simply stop
876 if (p
->checkFunctional(pkt
)) {
877 if (pkt
->needsResponse())
883 PortID dest_id
= findPort(pkt
->getAddr());
885 masterPorts
[dest_id
]->sendFunctional(pkt
);
890 CoherentXBar::recvFunctionalSnoop(PacketPtr pkt
, PortID master_port_id
)
892 if (!pkt
->isPrint()) {
893 // don't do DPRINTFs on PrintReq as it clutters up the output
894 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
895 masterPorts
[master_port_id
]->name(), pkt
->print());
898 for (const auto& p
: slavePorts
) {
899 if (p
->checkFunctional(pkt
)) {
900 if (pkt
->needsResponse())
906 // forward to all snoopers
907 forwardFunctional(pkt
, InvalidPortID
);
911 CoherentXBar::forwardFunctional(PacketPtr pkt
, PortID exclude_slave_port_id
)
913 // snoops should only happen if the system isn't bypassing caches
914 assert(!system
->bypassCaches());
916 for (const auto& p
: snoopPorts
) {
917 // we could have gotten this request from a snooping master
918 // (corresponding to our own slave port that is also in
919 // snoopPorts) and should not send it back to where it came
921 if (exclude_slave_port_id
== InvalidPortID
||
922 p
->getId() != exclude_slave_port_id
)
923 p
->sendFunctionalSnoop(pkt
);
925 // if we get a response we are done
926 if (pkt
->isResponse()) {
933 CoherentXBar::sinkPacket(const PacketPtr pkt
) const
935 // we can sink the packet if:
936 // 1) the crossbar is the point of coherency, and a cache is
937 // responding after being snooped
938 // 2) the crossbar is the point of coherency, and the packet is a
939 // coherency packet (not a read or a write) that does not
940 // require a response
941 // 3) this is a clean evict or clean writeback, but the packet is
942 // found in a cache above this crossbar
943 // 4) a cache is responding after being snooped, and the packet
944 // either does not need the block to be writable, or the cache
945 // that has promised to respond (setting the cache responding
946 // flag) is providing writable and thus had a Modified block,
947 // and no further action is needed
948 return (pointOfCoherency
&& pkt
->cacheResponding()) ||
949 (pointOfCoherency
&& !(pkt
->isRead() || pkt
->isWrite()) &&
950 !pkt
->needsResponse()) ||
951 (pkt
->isCleanEviction() && pkt
->isBlockCached()) ||
952 (pkt
->cacheResponding() &&
953 (!pkt
->needsWritable() || pkt
->responderHadWritable()));
957 CoherentXBar::regStats()
959 // register the stats of the base class and our layers
960 BaseXBar::regStats();
961 for (auto l
: reqLayers
)
963 for (auto l
: respLayers
)
965 for (auto l
: snoopLayers
)
969 .name(name() + ".snoops")
970 .desc("Total snoops (count)")
974 .name(name() + ".snoopTraffic")
975 .desc("Total snoop traffic (bytes)")
979 .init(0, snoopPorts
.size(), 1)
980 .name(name() + ".snoop_fanout")
981 .desc("Request fanout histogram")
986 CoherentXBarParams::create()
988 return new CoherentXBar(this);