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/logging.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
),
62 pointOfUnification(p
->point_of_unification
)
64 // create the ports based on the size of the master and slave
65 // vector ports, and the presence of the default port, the ports
66 // are enumerated starting from zero
67 for (int i
= 0; i
< p
->port_master_connection_count
; ++i
) {
68 std::string portName
= csprintf("%s.master[%d]", name(), i
);
69 MasterPort
* bp
= new CoherentXBarMasterPort(portName
, *this, i
);
70 masterPorts
.push_back(bp
);
71 reqLayers
.push_back(new ReqLayer(*bp
, *this,
72 csprintf(".reqLayer%d", i
)));
73 snoopLayers
.push_back(new SnoopRespLayer(*bp
, *this,
74 csprintf(".snoopLayer%d", i
)));
77 // see if we have a default slave device connected and if so add
78 // our corresponding master port
79 if (p
->port_default_connection_count
) {
80 defaultPortID
= masterPorts
.size();
81 std::string portName
= name() + ".default";
82 MasterPort
* bp
= new CoherentXBarMasterPort(portName
, *this,
84 masterPorts
.push_back(bp
);
85 reqLayers
.push_back(new ReqLayer(*bp
, *this, csprintf(".reqLayer%d",
87 snoopLayers
.push_back(new SnoopRespLayer(*bp
, *this,
88 csprintf(".snoopLayer%d",
92 // create the slave ports, once again starting at zero
93 for (int i
= 0; i
< p
->port_slave_connection_count
; ++i
) {
94 std::string portName
= csprintf("%s.slave[%d]", name(), i
);
95 QueuedSlavePort
* bp
= new CoherentXBarSlavePort(portName
, *this, i
);
96 slavePorts
.push_back(bp
);
97 respLayers
.push_back(new RespLayer(*bp
, *this,
98 csprintf(".respLayer%d", i
)));
99 snoopRespPorts
.push_back(new SnoopRespPort(*bp
, *this));
105 CoherentXBar::~CoherentXBar()
107 for (auto l
: reqLayers
)
109 for (auto l
: respLayers
)
111 for (auto l
: snoopLayers
)
113 for (auto p
: snoopRespPorts
)
122 // iterate over our slave ports and determine which of our
123 // neighbouring master ports are snooping and add them as snoopers
124 for (const auto& p
: slavePorts
) {
125 // check if the connected master port is snooping
126 if (p
->isSnooping()) {
127 DPRINTF(AddrRanges
, "Adding snooping master %s\n",
128 p
->getMasterPort().name());
129 snoopPorts
.push_back(p
);
133 if (snoopPorts
.empty())
134 warn("CoherentXBar %s has no snooping ports attached!\n", name());
136 // inform the snoop filter about the slave ports so it can create
137 // its own internal representation
139 snoopFilter
->setSlavePorts(slavePorts
);
143 CoherentXBar::recvTimingReq(PacketPtr pkt
, PortID slave_port_id
)
145 // determine the source port based on the id
146 SlavePort
*src_port
= slavePorts
[slave_port_id
];
148 // remember if the packet is an express snoop
149 bool is_express_snoop
= pkt
->isExpressSnoop();
150 bool cache_responding
= pkt
->cacheResponding();
151 // for normal requests, going downstream, the express snoop flag
152 // and the cache responding flag should always be the same
153 assert(is_express_snoop
== cache_responding
);
155 // determine the destination based on the address
156 PortID master_port_id
= findPort(pkt
->getAddr());
158 // test if the crossbar should be considered occupied for the current
159 // port, and exclude express snoops from the check
160 if (!is_express_snoop
&& !reqLayers
[master_port_id
]->tryTiming(src_port
)) {
161 DPRINTF(CoherentXBar
, "%s: src %s packet %s BUSY\n", __func__
,
162 src_port
->name(), pkt
->print());
166 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
167 src_port
->name(), pkt
->print());
169 // store size and command as they might be modified when
170 // forwarding the packet
171 unsigned int pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
172 unsigned int pkt_cmd
= pkt
->cmdToIndex();
174 // store the old header delay so we can restore it if needed
175 Tick old_header_delay
= pkt
->headerDelay
;
177 // a request sees the frontend and forward latency
178 Tick xbar_delay
= (frontendLatency
+ forwardLatency
) * clockPeriod();
180 // set the packet header and payload delay
181 calcPacketTiming(pkt
, xbar_delay
);
183 // determine how long to be crossbar layer is busy
184 Tick packetFinishTime
= clockEdge(Cycles(1)) + pkt
->payloadDelay
;
186 // is this the destination point for this packet? (e.g. true if
187 // this xbar is the PoC for a cache maintenance operation to the
188 // PoC) otherwise the destination is any cache that can satisfy
190 const bool is_destination
= isDestination(pkt
);
192 const bool snoop_caches
= !system
->bypassCaches() &&
193 pkt
->cmd
!= MemCmd::WriteClean
;
195 assert(pkt
->snoopDelay
== 0);
197 // the packet is a memory-mapped request and should be
198 // broadcasted to our snoopers but the source
200 // check with the snoop filter where to forward this packet
201 auto sf_res
= snoopFilter
->lookupRequest(pkt
, *src_port
);
202 // the time required by a packet to be delivered through
203 // the xbar has to be charged also with to lookup latency
204 // of the snoop filter
205 pkt
->headerDelay
+= sf_res
.second
* clockPeriod();
206 DPRINTF(CoherentXBar
, "%s: src %s packet %s SF size: %i lat: %i\n",
207 __func__
, src_port
->name(), pkt
->print(),
208 sf_res
.first
.size(), sf_res
.second
);
210 if (pkt
->isEviction()) {
211 // for block-evicting packets, i.e. writebacks and
212 // clean evictions, there is no need to snoop up, as
213 // all we do is determine if the block is cached or
214 // not, instead just set it here based on the snoop
216 if (!sf_res
.first
.empty())
217 pkt
->setBlockCached();
219 forwardTiming(pkt
, slave_port_id
, sf_res
.first
);
222 forwardTiming(pkt
, slave_port_id
);
225 // add the snoop delay to our header delay, and then reset it
226 pkt
->headerDelay
+= pkt
->snoopDelay
;
230 // set up a sensible starting point
233 // remember if the packet will generate a snoop response by
234 // checking if a cache set the cacheResponding flag during the
236 const bool expect_snoop_resp
= !cache_responding
&& pkt
->cacheResponding();
237 bool expect_response
= pkt
->needsResponse() && !pkt
->cacheResponding();
239 const bool sink_packet
= sinkPacket(pkt
);
241 // in certain cases the crossbar is responsible for responding
242 bool respond_directly
= false;
243 // store the original address as an address mapper could possibly
244 // modify the address upon a sendTimingRequest
245 const Addr
addr(pkt
->getAddr());
247 DPRINTF(CoherentXBar
, "%s: Not forwarding %s\n", __func__
,
250 // determine if we are forwarding the packet, or responding to
252 if (forwardPacket(pkt
)) {
253 // if we are passing on, rather than sinking, a packet to
254 // which an upstream cache has committed to responding,
255 // the line was needs writable, and the responding only
256 // had an Owned copy, so we need to immidiately let the
257 // downstream caches know, bypass any flow control
258 if (pkt
->cacheResponding()) {
259 pkt
->setExpressSnoop();
262 // make sure that the write request (e.g., WriteClean)
263 // will stop at the memory below if this crossbar is its
265 if (pkt
->isWrite() && is_destination
) {
266 pkt
->clearWriteThrough();
269 // since it is a normal request, attempt to send the packet
270 success
= masterPorts
[master_port_id
]->sendTimingReq(pkt
);
272 // no need to forward, turn this packet around and respond
274 assert(pkt
->needsResponse());
276 respond_directly
= true;
277 assert(!expect_snoop_resp
);
278 expect_response
= false;
282 if (snoopFilter
&& snoop_caches
) {
283 // Let the snoop filter know about the success of the send operation
284 snoopFilter
->finishRequest(!success
, addr
, pkt
->isSecure());
287 // check if we were successful in sending the packet onwards
289 // express snoops should never be forced to retry
290 assert(!is_express_snoop
);
292 // restore the header delay
293 pkt
->headerDelay
= old_header_delay
;
295 DPRINTF(CoherentXBar
, "%s: src %s packet %s RETRY\n", __func__
,
296 src_port
->name(), pkt
->print());
298 // update the layer state and schedule an idle event
299 reqLayers
[master_port_id
]->failedTiming(src_port
,
300 clockEdge(Cycles(1)));
302 // express snoops currently bypass the crossbar state entirely
303 if (!is_express_snoop
) {
304 // if this particular request will generate a snoop
306 if (expect_snoop_resp
) {
307 // we should never have an exsiting request outstanding
308 assert(outstandingSnoop
.find(pkt
->req
) ==
309 outstandingSnoop
.end());
310 outstandingSnoop
.insert(pkt
->req
);
312 // basic sanity check on the outstanding snoops
313 panic_if(outstandingSnoop
.size() > 512,
314 "Outstanding snoop requests exceeded 512\n");
317 // remember where to route the normal response to
318 if (expect_response
|| expect_snoop_resp
) {
319 assert(routeTo
.find(pkt
->req
) == routeTo
.end());
320 routeTo
[pkt
->req
] = slave_port_id
;
322 panic_if(routeTo
.size() > 512,
323 "Routing table exceeds 512 packets\n");
326 // update the layer state and schedule an idle event
327 reqLayers
[master_port_id
]->succeededTiming(packetFinishTime
);
330 // stats updates only consider packets that were successfully sent
331 pktCount
[slave_port_id
][master_port_id
]++;
332 pktSize
[slave_port_id
][master_port_id
] += pkt_size
;
333 transDist
[pkt_cmd
]++;
335 if (is_express_snoop
) {
337 snoopTraffic
+= pkt_size
;
342 // queue the packet for deletion
343 pendingDelete
.reset(pkt
);
345 if (respond_directly
) {
346 assert(pkt
->needsResponse());
351 if (snoopFilter
&& !system
->bypassCaches()) {
352 // let the snoop filter inspect the response and update its state
353 snoopFilter
->updateResponse(pkt
, *slavePorts
[slave_port_id
]);
356 Tick response_time
= clockEdge() + pkt
->headerDelay
;
357 pkt
->headerDelay
= 0;
359 slavePorts
[slave_port_id
]->schedTimingResp(pkt
, response_time
);
366 CoherentXBar::recvTimingResp(PacketPtr pkt
, PortID master_port_id
)
368 // determine the source port based on the id
369 MasterPort
*src_port
= masterPorts
[master_port_id
];
371 // determine the destination
372 const auto route_lookup
= routeTo
.find(pkt
->req
);
373 assert(route_lookup
!= routeTo
.end());
374 const PortID slave_port_id
= route_lookup
->second
;
375 assert(slave_port_id
!= InvalidPortID
);
376 assert(slave_port_id
< respLayers
.size());
378 // test if the crossbar should be considered occupied for the
380 if (!respLayers
[slave_port_id
]->tryTiming(src_port
)) {
381 DPRINTF(CoherentXBar
, "%s: src %s packet %s BUSY\n", __func__
,
382 src_port
->name(), pkt
->print());
386 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
387 src_port
->name(), pkt
->print());
389 // store size and command as they might be modified when
390 // forwarding the packet
391 unsigned int pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
392 unsigned int pkt_cmd
= pkt
->cmdToIndex();
394 // a response sees the response latency
395 Tick xbar_delay
= responseLatency
* clockPeriod();
397 // set the packet header and payload delay
398 calcPacketTiming(pkt
, xbar_delay
);
400 // determine how long to be crossbar layer is busy
401 Tick packetFinishTime
= clockEdge(Cycles(1)) + pkt
->payloadDelay
;
403 if (snoopFilter
&& !system
->bypassCaches()) {
404 // let the snoop filter inspect the response and update its state
405 snoopFilter
->updateResponse(pkt
, *slavePorts
[slave_port_id
]);
408 // send the packet through the destination slave port and pay for
409 // any outstanding header delay
410 Tick latency
= pkt
->headerDelay
;
411 pkt
->headerDelay
= 0;
412 slavePorts
[slave_port_id
]->schedTimingResp(pkt
, curTick() + latency
);
414 // remove the request from the routing table
415 routeTo
.erase(route_lookup
);
417 respLayers
[slave_port_id
]->succeededTiming(packetFinishTime
);
420 pktCount
[slave_port_id
][master_port_id
]++;
421 pktSize
[slave_port_id
][master_port_id
] += pkt_size
;
422 transDist
[pkt_cmd
]++;
428 CoherentXBar::recvTimingSnoopReq(PacketPtr pkt
, PortID master_port_id
)
430 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
431 masterPorts
[master_port_id
]->name(), pkt
->print());
433 // update stats here as we know the forwarding will succeed
434 unsigned int pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
435 transDist
[pkt
->cmdToIndex()]++;
437 snoopTraffic
+= pkt_size
;
439 // we should only see express snoops from caches
440 assert(pkt
->isExpressSnoop());
442 // set the packet header and payload delay, for now use forward latency
443 // @todo Assess the choice of latency further
444 calcPacketTiming(pkt
, forwardLatency
* clockPeriod());
446 // remember if a cache has already committed to responding so we
447 // can see if it changes during the snooping
448 const bool cache_responding
= pkt
->cacheResponding();
450 assert(pkt
->snoopDelay
== 0);
453 // let the Snoop Filter work its magic and guide probing
454 auto sf_res
= snoopFilter
->lookupSnoop(pkt
);
455 // the time required by a packet to be delivered through
456 // the xbar has to be charged also with to lookup latency
457 // of the snoop filter
458 pkt
->headerDelay
+= sf_res
.second
* clockPeriod();
459 DPRINTF(CoherentXBar
, "%s: src %s packet %s SF size: %i lat: %i\n",
460 __func__
, masterPorts
[master_port_id
]->name(), pkt
->print(),
461 sf_res
.first
.size(), sf_res
.second
);
463 // forward to all snoopers
464 forwardTiming(pkt
, InvalidPortID
, sf_res
.first
);
466 forwardTiming(pkt
, InvalidPortID
);
469 // add the snoop delay to our header delay, and then reset it
470 pkt
->headerDelay
+= pkt
->snoopDelay
;
473 // if we can expect a response, remember how to route it
474 if (!cache_responding
&& pkt
->cacheResponding()) {
475 assert(routeTo
.find(pkt
->req
) == routeTo
.end());
476 routeTo
[pkt
->req
] = master_port_id
;
479 // a snoop request came from a connected slave device (one of
480 // our master ports), and if it is not coming from the slave
481 // device responsible for the address range something is
482 // wrong, hence there is nothing further to do as the packet
483 // would be going back to where it came from
484 assert(master_port_id
== findPort(pkt
->getAddr()));
488 CoherentXBar::recvTimingSnoopResp(PacketPtr pkt
, PortID slave_port_id
)
490 // determine the source port based on the id
491 SlavePort
* src_port
= slavePorts
[slave_port_id
];
493 // get the destination
494 const auto route_lookup
= routeTo
.find(pkt
->req
);
495 assert(route_lookup
!= routeTo
.end());
496 const PortID dest_port_id
= route_lookup
->second
;
497 assert(dest_port_id
!= InvalidPortID
);
499 // determine if the response is from a snoop request we
500 // created as the result of a normal request (in which case it
501 // should be in the outstandingSnoop), or if we merely forwarded
502 // someone else's snoop request
503 const bool forwardAsSnoop
= outstandingSnoop
.find(pkt
->req
) ==
504 outstandingSnoop
.end();
506 // test if the crossbar should be considered occupied for the
507 // current port, note that the check is bypassed if the response
508 // is being passed on as a normal response since this is occupying
509 // the response layer rather than the snoop response layer
510 if (forwardAsSnoop
) {
511 assert(dest_port_id
< snoopLayers
.size());
512 if (!snoopLayers
[dest_port_id
]->tryTiming(src_port
)) {
513 DPRINTF(CoherentXBar
, "%s: src %s packet %s BUSY\n", __func__
,
514 src_port
->name(), pkt
->print());
518 // get the master port that mirrors this slave port internally
519 MasterPort
* snoop_port
= snoopRespPorts
[slave_port_id
];
520 assert(dest_port_id
< respLayers
.size());
521 if (!respLayers
[dest_port_id
]->tryTiming(snoop_port
)) {
522 DPRINTF(CoherentXBar
, "%s: src %s packet %s BUSY\n", __func__
,
523 snoop_port
->name(), pkt
->print());
528 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
529 src_port
->name(), pkt
->print());
531 // store size and command as they might be modified when
532 // forwarding the packet
533 unsigned int pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
534 unsigned int pkt_cmd
= pkt
->cmdToIndex();
536 // responses are never express snoops
537 assert(!pkt
->isExpressSnoop());
539 // a snoop response sees the snoop response latency, and if it is
540 // forwarded as a normal response, the response latency
542 (forwardAsSnoop
? snoopResponseLatency
: responseLatency
) *
545 // set the packet header and payload delay
546 calcPacketTiming(pkt
, xbar_delay
);
548 // determine how long to be crossbar layer is busy
549 Tick packetFinishTime
= clockEdge(Cycles(1)) + pkt
->payloadDelay
;
551 // forward it either as a snoop response or a normal response
552 if (forwardAsSnoop
) {
553 // this is a snoop response to a snoop request we forwarded,
554 // e.g. coming from the L1 and going to the L2, and it should
555 // be forwarded as a snoop response
558 // update the probe filter so that it can properly track the line
559 snoopFilter
->updateSnoopForward(pkt
, *slavePorts
[slave_port_id
],
560 *masterPorts
[dest_port_id
]);
563 bool success M5_VAR_USED
=
564 masterPorts
[dest_port_id
]->sendTimingSnoopResp(pkt
);
565 pktCount
[slave_port_id
][dest_port_id
]++;
566 pktSize
[slave_port_id
][dest_port_id
] += pkt_size
;
569 snoopLayers
[dest_port_id
]->succeededTiming(packetFinishTime
);
571 // we got a snoop response on one of our slave ports,
572 // i.e. from a coherent master connected to the crossbar, and
573 // since we created the snoop request as part of recvTiming,
574 // this should now be a normal response again
575 outstandingSnoop
.erase(pkt
->req
);
577 // this is a snoop response from a coherent master, hence it
578 // should never go back to where the snoop response came from,
579 // but instead to where the original request came from
580 assert(slave_port_id
!= dest_port_id
);
583 // update the probe filter so that it can properly track the line
584 snoopFilter
->updateSnoopResponse(pkt
, *slavePorts
[slave_port_id
],
585 *slavePorts
[dest_port_id
]);
588 DPRINTF(CoherentXBar
, "%s: src %s packet %s FWD RESP\n", __func__
,
589 src_port
->name(), pkt
->print());
591 // as a normal response, it should go back to a master through
592 // one of our slave ports, we also pay for any outstanding
594 Tick latency
= pkt
->headerDelay
;
595 pkt
->headerDelay
= 0;
596 slavePorts
[dest_port_id
]->schedTimingResp(pkt
, curTick() + latency
);
598 respLayers
[dest_port_id
]->succeededTiming(packetFinishTime
);
601 // remove the request from the routing table
602 routeTo
.erase(route_lookup
);
605 transDist
[pkt_cmd
]++;
607 snoopTraffic
+= pkt_size
;
614 CoherentXBar::forwardTiming(PacketPtr pkt
, PortID exclude_slave_port_id
,
615 const std::vector
<QueuedSlavePort
*>& dests
)
617 DPRINTF(CoherentXBar
, "%s for %s\n", __func__
, pkt
->print());
619 // snoops should only happen if the system isn't bypassing caches
620 assert(!system
->bypassCaches());
624 for (const auto& p
: dests
) {
625 // we could have gotten this request from a snooping master
626 // (corresponding to our own slave port that is also in
627 // snoopPorts) and should not send it back to where it came
629 if (exclude_slave_port_id
== InvalidPortID
||
630 p
->getId() != exclude_slave_port_id
) {
631 // cache is not allowed to refuse snoop
632 p
->sendTimingSnoopReq(pkt
);
637 // Stats for fanout of this forward operation
638 snoopFanout
.sample(fanout
);
642 CoherentXBar::recvReqRetry(PortID master_port_id
)
644 // responses and snoop responses never block on forwarding them,
645 // so the retry will always be coming from a port to which we
646 // tried to forward a request
647 reqLayers
[master_port_id
]->recvRetry();
651 CoherentXBar::recvAtomic(PacketPtr pkt
, PortID slave_port_id
)
653 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
654 slavePorts
[slave_port_id
]->name(), pkt
->print());
656 unsigned int pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
657 unsigned int pkt_cmd
= pkt
->cmdToIndex();
659 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
660 Tick snoop_response_latency
= 0;
662 // is this the destination point for this packet? (e.g. true if
663 // this xbar is the PoC for a cache maintenance operation to the
664 // PoC) otherwise the destination is any cache that can satisfy
666 const bool is_destination
= isDestination(pkt
);
668 const bool snoop_caches
= !system
->bypassCaches() &&
669 pkt
->cmd
!= MemCmd::WriteClean
;
671 // forward to all snoopers but the source
672 std::pair
<MemCmd
, Tick
> snoop_result
;
674 // check with the snoop filter where to forward this packet
676 snoopFilter
->lookupRequest(pkt
, *slavePorts
[slave_port_id
]);
677 snoop_response_latency
+= sf_res
.second
* clockPeriod();
678 DPRINTF(CoherentXBar
, "%s: src %s packet %s SF size: %i lat: %i\n",
679 __func__
, slavePorts
[slave_port_id
]->name(), pkt
->print(),
680 sf_res
.first
.size(), sf_res
.second
);
682 // let the snoop filter know about the success of the send
683 // operation, and do it even before sending it onwards to
684 // avoid situations where atomic upward snoops sneak in
685 // between and change the filter state
686 snoopFilter
->finishRequest(false, pkt
->getAddr(), pkt
->isSecure());
688 if (pkt
->isEviction()) {
689 // for block-evicting packets, i.e. writebacks and
690 // clean evictions, there is no need to snoop up, as
691 // all we do is determine if the block is cached or
692 // not, instead just set it here based on the snoop
694 if (!sf_res
.first
.empty())
695 pkt
->setBlockCached();
697 snoop_result
= forwardAtomic(pkt
, slave_port_id
, InvalidPortID
,
701 snoop_result
= forwardAtomic(pkt
, slave_port_id
);
703 snoop_response_cmd
= snoop_result
.first
;
704 snoop_response_latency
+= snoop_result
.second
;
707 // set up a sensible default value
708 Tick response_latency
= 0;
710 const bool sink_packet
= sinkPacket(pkt
);
712 // even if we had a snoop response, we must continue and also
713 // perform the actual request at the destination
714 PortID master_port_id
= findPort(pkt
->getAddr());
717 DPRINTF(CoherentXBar
, "%s: Not forwarding %s\n", __func__
,
720 if (forwardPacket(pkt
)) {
721 // make sure that the write request (e.g., WriteClean)
722 // will stop at the memory below if this crossbar is its
724 if (pkt
->isWrite() && is_destination
) {
725 pkt
->clearWriteThrough();
728 // forward the request to the appropriate destination
729 response_latency
= masterPorts
[master_port_id
]->sendAtomic(pkt
);
731 // if it does not need a response we sink the packet above
732 assert(pkt
->needsResponse());
738 // stats updates for the request
739 pktCount
[slave_port_id
][master_port_id
]++;
740 pktSize
[slave_port_id
][master_port_id
] += pkt_size
;
741 transDist
[pkt_cmd
]++;
744 // if lower levels have replied, tell the snoop filter
745 if (!system
->bypassCaches() && snoopFilter
&& pkt
->isResponse()) {
746 snoopFilter
->updateResponse(pkt
, *slavePorts
[slave_port_id
]);
749 // if we got a response from a snooper, restore it here
750 if (snoop_response_cmd
!= MemCmd::InvalidCmd
) {
751 // no one else should have responded
752 assert(!pkt
->isResponse());
753 pkt
->cmd
= snoop_response_cmd
;
754 response_latency
= snoop_response_latency
;
757 // add the response data
758 if (pkt
->isResponse()) {
759 pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
760 pkt_cmd
= pkt
->cmdToIndex();
763 pktCount
[slave_port_id
][master_port_id
]++;
764 pktSize
[slave_port_id
][master_port_id
] += pkt_size
;
765 transDist
[pkt_cmd
]++;
768 // @todo: Not setting header time
769 pkt
->payloadDelay
= response_latency
;
770 return response_latency
;
774 CoherentXBar::recvAtomicSnoop(PacketPtr pkt
, PortID master_port_id
)
776 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
777 masterPorts
[master_port_id
]->name(), pkt
->print());
779 // add the request snoop data
780 unsigned int pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
782 snoopTraffic
+= pkt_size
;
784 // forward to all snoopers
785 std::pair
<MemCmd
, Tick
> snoop_result
;
786 Tick snoop_response_latency
= 0;
788 auto sf_res
= snoopFilter
->lookupSnoop(pkt
);
789 snoop_response_latency
+= sf_res
.second
* clockPeriod();
790 DPRINTF(CoherentXBar
, "%s: src %s packet %s SF size: %i lat: %i\n",
791 __func__
, masterPorts
[master_port_id
]->name(), pkt
->print(),
792 sf_res
.first
.size(), sf_res
.second
);
793 snoop_result
= forwardAtomic(pkt
, InvalidPortID
, master_port_id
,
796 snoop_result
= forwardAtomic(pkt
, InvalidPortID
);
798 MemCmd snoop_response_cmd
= snoop_result
.first
;
799 snoop_response_latency
+= snoop_result
.second
;
801 if (snoop_response_cmd
!= MemCmd::InvalidCmd
)
802 pkt
->cmd
= snoop_response_cmd
;
804 // add the response snoop data
805 if (pkt
->isResponse()) {
809 // @todo: Not setting header time
810 pkt
->payloadDelay
= snoop_response_latency
;
811 return snoop_response_latency
;
814 std::pair
<MemCmd
, Tick
>
815 CoherentXBar::forwardAtomic(PacketPtr pkt
, PortID exclude_slave_port_id
,
816 PortID source_master_port_id
,
817 const std::vector
<QueuedSlavePort
*>& dests
)
819 // the packet may be changed on snoops, record the original
820 // command to enable us to restore it between snoops so that
821 // additional snoops can take place properly
822 MemCmd orig_cmd
= pkt
->cmd
;
823 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
824 Tick snoop_response_latency
= 0;
826 // snoops should only happen if the system isn't bypassing caches
827 assert(!system
->bypassCaches());
831 for (const auto& p
: dests
) {
832 // we could have gotten this request from a snooping master
833 // (corresponding to our own slave port that is also in
834 // snoopPorts) and should not send it back to where it came
836 if (exclude_slave_port_id
!= InvalidPortID
&&
837 p
->getId() == exclude_slave_port_id
)
840 Tick latency
= p
->sendAtomicSnoop(pkt
);
843 // in contrast to a functional access, we have to keep on
844 // going as all snoopers must be updated even if we get a
846 if (!pkt
->isResponse())
849 // response from snoop agent
850 assert(pkt
->cmd
!= orig_cmd
);
851 assert(pkt
->cacheResponding());
852 // should only happen once
853 assert(snoop_response_cmd
== MemCmd::InvalidCmd
);
854 // save response state
855 snoop_response_cmd
= pkt
->cmd
;
856 snoop_response_latency
= latency
;
859 // Handle responses by the snoopers and differentiate between
860 // responses to requests from above and snoops from below
861 if (source_master_port_id
!= InvalidPortID
) {
862 // Getting a response for a snoop from below
863 assert(exclude_slave_port_id
== InvalidPortID
);
864 snoopFilter
->updateSnoopForward(pkt
, *p
,
865 *masterPorts
[source_master_port_id
]);
867 // Getting a response for a request from above
868 assert(source_master_port_id
== InvalidPortID
);
869 snoopFilter
->updateSnoopResponse(pkt
, *p
,
870 *slavePorts
[exclude_slave_port_id
]);
873 // restore original packet state for remaining snoopers
878 snoopFanout
.sample(fanout
);
880 // the packet is restored as part of the loop and any potential
881 // snoop response is part of the returned pair
882 return std::make_pair(snoop_response_cmd
, snoop_response_latency
);
886 CoherentXBar::recvFunctional(PacketPtr pkt
, PortID slave_port_id
)
888 if (!pkt
->isPrint()) {
889 // don't do DPRINTFs on PrintReq as it clutters up the output
890 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
891 slavePorts
[slave_port_id
]->name(), pkt
->print());
894 if (!system
->bypassCaches()) {
895 // forward to all snoopers but the source
896 forwardFunctional(pkt
, slave_port_id
);
899 // there is no need to continue if the snooping has found what we
900 // were looking for and the packet is already a response
901 if (!pkt
->isResponse()) {
902 // since our slave ports are queued ports we need to check them as well
903 for (const auto& p
: slavePorts
) {
904 // if we find a response that has the data, then the
905 // downstream caches/memories may be out of date, so simply stop
907 if (p
->checkFunctional(pkt
)) {
908 if (pkt
->needsResponse())
914 PortID dest_id
= findPort(pkt
->getAddr());
916 masterPorts
[dest_id
]->sendFunctional(pkt
);
921 CoherentXBar::recvFunctionalSnoop(PacketPtr pkt
, PortID master_port_id
)
923 if (!pkt
->isPrint()) {
924 // don't do DPRINTFs on PrintReq as it clutters up the output
925 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
926 masterPorts
[master_port_id
]->name(), pkt
->print());
929 for (const auto& p
: slavePorts
) {
930 if (p
->checkFunctional(pkt
)) {
931 if (pkt
->needsResponse())
937 // forward to all snoopers
938 forwardFunctional(pkt
, InvalidPortID
);
942 CoherentXBar::forwardFunctional(PacketPtr pkt
, PortID exclude_slave_port_id
)
944 // snoops should only happen if the system isn't bypassing caches
945 assert(!system
->bypassCaches());
947 for (const auto& p
: snoopPorts
) {
948 // we could have gotten this request from a snooping master
949 // (corresponding to our own slave port that is also in
950 // snoopPorts) and should not send it back to where it came
952 if (exclude_slave_port_id
== InvalidPortID
||
953 p
->getId() != exclude_slave_port_id
)
954 p
->sendFunctionalSnoop(pkt
);
956 // if we get a response we are done
957 if (pkt
->isResponse()) {
964 CoherentXBar::sinkPacket(const PacketPtr pkt
) const
966 // we can sink the packet if:
967 // 1) the crossbar is the point of coherency, and a cache is
968 // responding after being snooped
969 // 2) the crossbar is the point of coherency, and the packet is a
970 // coherency packet (not a read or a write) that does not
971 // require a response
972 // 3) this is a clean evict or clean writeback, but the packet is
973 // found in a cache above this crossbar
974 // 4) a cache is responding after being snooped, and the packet
975 // either does not need the block to be writable, or the cache
976 // that has promised to respond (setting the cache responding
977 // flag) is providing writable and thus had a Modified block,
978 // and no further action is needed
979 return (pointOfCoherency
&& pkt
->cacheResponding()) ||
980 (pointOfCoherency
&& !(pkt
->isRead() || pkt
->isWrite()) &&
981 !pkt
->needsResponse()) ||
982 (pkt
->isCleanEviction() && pkt
->isBlockCached()) ||
983 (pkt
->cacheResponding() &&
984 (!pkt
->needsWritable() || pkt
->responderHadWritable()));
988 CoherentXBar::forwardPacket(const PacketPtr pkt
)
990 // we are forwarding the packet if:
991 // 1) this is a read or a write
992 // 2) this crossbar is above the point of coherency
993 return pkt
->isRead() || pkt
->isWrite() || !pointOfCoherency
;
998 CoherentXBar::regStats()
1000 // register the stats of the base class and our layers
1001 BaseXBar::regStats();
1002 for (auto l
: reqLayers
)
1004 for (auto l
: respLayers
)
1006 for (auto l
: snoopLayers
)
1010 .name(name() + ".snoops")
1011 .desc("Total snoops (count)")
1015 .name(name() + ".snoopTraffic")
1016 .desc("Total snoop traffic (bytes)")
1020 .init(0, snoopPorts
.size(), 1)
1021 .name(name() + ".snoop_fanout")
1022 .desc("Request fanout histogram")
1027 CoherentXBarParams::create()
1029 return new CoherentXBar(this);