2 * Copyright (c) 2011-2020 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.
43 * Definition of a crossbar object.
46 #include "mem/coherent_xbar.hh"
48 #include "base/logging.hh"
49 #include "base/trace.hh"
50 #include "debug/AddrRanges.hh"
51 #include "debug/CoherentXBar.hh"
52 #include "sim/system.hh"
54 CoherentXBar::CoherentXBar(const CoherentXBarParams
*p
)
55 : BaseXBar(p
), system(p
->system
), snoopFilter(p
->snoop_filter
),
56 snoopResponseLatency(p
->snoop_response_latency
),
57 maxOutstandingSnoopCheck(p
->max_outstanding_snoops
),
58 maxRoutingTableSizeCheck(p
->max_routing_table_size
),
59 pointOfCoherency(p
->point_of_coherency
),
60 pointOfUnification(p
->point_of_unification
),
62 snoops(this, "snoops", "Total snoops (count)"),
63 snoopTraffic(this, "snoopTraffic", "Total snoop traffic (bytes)"),
64 snoopFanout(this, "snoop_fanout", "Request fanout histogram")
66 // create the ports based on the size of the master and slave
67 // vector ports, and the presence of the default port, the ports
68 // are enumerated starting from zero
69 for (int i
= 0; i
< p
->port_master_connection_count
; ++i
) {
70 std::string portName
= csprintf("%s.master[%d]", name(), i
);
71 RequestPort
* bp
= new CoherentXBarMasterPort(portName
, *this, i
);
72 masterPorts
.push_back(bp
);
73 reqLayers
.push_back(new ReqLayer(*bp
, *this,
74 csprintf("reqLayer%d", i
)));
75 snoopLayers
.push_back(
76 new SnoopRespLayer(*bp
, *this, csprintf("snoopLayer%d", i
)));
79 // see if we have a default slave device connected and if so add
80 // our corresponding master port
81 if (p
->port_default_connection_count
) {
82 defaultPortID
= masterPorts
.size();
83 std::string portName
= name() + ".default";
84 RequestPort
* bp
= new CoherentXBarMasterPort(portName
, *this,
86 masterPorts
.push_back(bp
);
87 reqLayers
.push_back(new ReqLayer(*bp
, *this, csprintf("reqLayer%d",
89 snoopLayers
.push_back(new SnoopRespLayer(*bp
, *this,
90 csprintf("snoopLayer%d",
94 // create the slave ports, once again starting at zero
95 for (int i
= 0; i
< p
->port_slave_connection_count
; ++i
) {
96 std::string portName
= csprintf("%s.slave[%d]", name(), i
);
97 QueuedSlavePort
* bp
= new CoherentXBarSlavePort(portName
, *this, i
);
98 slavePorts
.push_back(bp
);
99 respLayers
.push_back(new RespLayer(*bp
, *this,
100 csprintf("respLayer%d", i
)));
101 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", p
->getPeer());
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 ResponsePort
*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 destination address range
155 PortID master_port_id
= findPort(pkt
->getAddrRange());
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(headerLatency
) + pkt
->payloadDelay
;
185 // is this the destination point for this packet? (e.g. true if
186 // this xbar is the PoC for a cache maintenance operation to the
187 // PoC) otherwise the destination is any cache that can satisfy
189 const bool is_destination
= isDestination(pkt
);
191 const bool snoop_caches
= !system
->bypassCaches() &&
192 pkt
->cmd
!= MemCmd::WriteClean
;
194 assert(pkt
->snoopDelay
== 0);
196 if (pkt
->isClean() && !is_destination
) {
197 // before snooping we need to make sure that the memory
198 // below is not busy and the cache clean request can be
200 if (!masterPorts
[master_port_id
]->tryTiming(pkt
)) {
201 DPRINTF(CoherentXBar
, "%s: src %s packet %s RETRY\n", __func__
,
202 src_port
->name(), pkt
->print());
204 // update the layer state and schedule an idle event
205 reqLayers
[master_port_id
]->failedTiming(src_port
,
206 clockEdge(Cycles(1)));
212 // the packet is a memory-mapped request and should be
213 // broadcasted to our snoopers but the source
215 // check with the snoop filter where to forward this packet
216 auto sf_res
= snoopFilter
->lookupRequest(pkt
, *src_port
);
217 // the time required by a packet to be delivered through
218 // the xbar has to be charged also with to lookup latency
219 // of the snoop filter
220 pkt
->headerDelay
+= sf_res
.second
* clockPeriod();
221 DPRINTF(CoherentXBar
, "%s: src %s packet %s SF size: %i lat: %i\n",
222 __func__
, src_port
->name(), pkt
->print(),
223 sf_res
.first
.size(), sf_res
.second
);
225 if (pkt
->isEviction()) {
226 // for block-evicting packets, i.e. writebacks and
227 // clean evictions, there is no need to snoop up, as
228 // all we do is determine if the block is cached or
229 // not, instead just set it here based on the snoop
231 if (!sf_res
.first
.empty())
232 pkt
->setBlockCached();
234 forwardTiming(pkt
, slave_port_id
, sf_res
.first
);
237 forwardTiming(pkt
, slave_port_id
);
240 // add the snoop delay to our header delay, and then reset it
241 pkt
->headerDelay
+= pkt
->snoopDelay
;
245 // set up a sensible starting point
248 // remember if the packet will generate a snoop response by
249 // checking if a cache set the cacheResponding flag during the
251 const bool expect_snoop_resp
= !cache_responding
&& pkt
->cacheResponding();
252 bool expect_response
= pkt
->needsResponse() && !pkt
->cacheResponding();
254 const bool sink_packet
= sinkPacket(pkt
);
256 // in certain cases the crossbar is responsible for responding
257 bool respond_directly
= false;
258 // store the original address as an address mapper could possibly
259 // modify the address upon a sendTimingRequest
260 const Addr
addr(pkt
->getAddr());
262 DPRINTF(CoherentXBar
, "%s: Not forwarding %s\n", __func__
,
265 // determine if we are forwarding the packet, or responding to
267 if (forwardPacket(pkt
)) {
268 // if we are passing on, rather than sinking, a packet to
269 // which an upstream cache has committed to responding,
270 // the line was needs writable, and the responding only
271 // had an Owned copy, so we need to immidiately let the
272 // downstream caches know, bypass any flow control
273 if (pkt
->cacheResponding()) {
274 pkt
->setExpressSnoop();
277 // make sure that the write request (e.g., WriteClean)
278 // will stop at the memory below if this crossbar is its
280 if (pkt
->isWrite() && is_destination
) {
281 pkt
->clearWriteThrough();
284 // since it is a normal request, attempt to send the packet
285 success
= masterPorts
[master_port_id
]->sendTimingReq(pkt
);
287 // no need to forward, turn this packet around and respond
289 assert(pkt
->needsResponse());
291 respond_directly
= true;
292 assert(!expect_snoop_resp
);
293 expect_response
= false;
297 if (snoopFilter
&& snoop_caches
) {
298 // Let the snoop filter know about the success of the send operation
299 snoopFilter
->finishRequest(!success
, addr
, pkt
->isSecure());
302 // check if we were successful in sending the packet onwards
304 // express snoops should never be forced to retry
305 assert(!is_express_snoop
);
307 // restore the header delay
308 pkt
->headerDelay
= old_header_delay
;
310 DPRINTF(CoherentXBar
, "%s: src %s packet %s RETRY\n", __func__
,
311 src_port
->name(), pkt
->print());
313 // update the layer state and schedule an idle event
314 reqLayers
[master_port_id
]->failedTiming(src_port
,
315 clockEdge(Cycles(1)));
317 // express snoops currently bypass the crossbar state entirely
318 if (!is_express_snoop
) {
319 // if this particular request will generate a snoop
321 if (expect_snoop_resp
) {
322 // we should never have an exsiting request outstanding
323 assert(outstandingSnoop
.find(pkt
->req
) ==
324 outstandingSnoop
.end());
325 outstandingSnoop
.insert(pkt
->req
);
327 // basic sanity check on the outstanding snoops
328 panic_if(outstandingSnoop
.size() > maxOutstandingSnoopCheck
,
329 "%s: Outstanding snoop requests exceeded %d\n",
330 name(), maxOutstandingSnoopCheck
);
333 // remember where to route the normal response to
334 if (expect_response
|| expect_snoop_resp
) {
335 assert(routeTo
.find(pkt
->req
) == routeTo
.end());
336 routeTo
[pkt
->req
] = slave_port_id
;
338 panic_if(routeTo
.size() > maxRoutingTableSizeCheck
,
339 "%s: Routing table exceeds %d packets\n",
340 name(), maxRoutingTableSizeCheck
);
343 // update the layer state and schedule an idle event
344 reqLayers
[master_port_id
]->succeededTiming(packetFinishTime
);
347 // stats updates only consider packets that were successfully sent
348 pktCount
[slave_port_id
][master_port_id
]++;
349 pktSize
[slave_port_id
][master_port_id
] += pkt_size
;
350 transDist
[pkt_cmd
]++;
352 if (is_express_snoop
) {
354 snoopTraffic
+= pkt_size
;
359 // queue the packet for deletion
360 pendingDelete
.reset(pkt
);
362 // normally we respond to the packet we just received if we need to
363 PacketPtr rsp_pkt
= pkt
;
364 PortID rsp_port_id
= slave_port_id
;
366 // If this is the destination of the cache clean operation the
367 // crossbar is responsible for responding. This crossbar will
368 // respond when the cache clean is complete. A cache clean
369 // is complete either:
370 // * direcly, if no cache above had a dirty copy of the block
371 // as indicated by the satisfied flag of the packet, or
372 // * when the crossbar has seen both the cache clean request
373 // (CleanSharedReq, CleanInvalidReq) and the corresponding
374 // write (WriteClean) which updates the block in the memory
377 ((pkt
->isClean() && pkt
->satisfied()) ||
378 pkt
->cmd
== MemCmd::WriteClean
) &&
380 PacketPtr deferred_rsp
= pkt
->isWrite() ? nullptr : pkt
;
381 auto cmo_lookup
= outstandingCMO
.find(pkt
->id
);
382 if (cmo_lookup
!= outstandingCMO
.end()) {
383 // the cache clean request has already reached this xbar
384 respond_directly
= true;
385 if (pkt
->isWrite()) {
386 rsp_pkt
= cmo_lookup
->second
;
389 // determine the destination
390 const auto route_lookup
= routeTo
.find(rsp_pkt
->req
);
391 assert(route_lookup
!= routeTo
.end());
392 rsp_port_id
= route_lookup
->second
;
393 assert(rsp_port_id
!= InvalidPortID
);
394 assert(rsp_port_id
< respLayers
.size());
395 // remove the request from the routing table
396 routeTo
.erase(route_lookup
);
398 outstandingCMO
.erase(cmo_lookup
);
400 respond_directly
= false;
401 outstandingCMO
.emplace(pkt
->id
, deferred_rsp
);
402 if (!pkt
->isWrite()) {
403 assert(routeTo
.find(pkt
->req
) == routeTo
.end());
404 routeTo
[pkt
->req
] = slave_port_id
;
406 panic_if(routeTo
.size() > maxRoutingTableSizeCheck
,
407 "%s: Routing table exceeds %d packets\n",
408 name(), maxRoutingTableSizeCheck
);
414 if (respond_directly
) {
415 assert(rsp_pkt
->needsResponse());
418 rsp_pkt
->makeResponse();
420 if (snoopFilter
&& !system
->bypassCaches()) {
421 // let the snoop filter inspect the response and update its state
422 snoopFilter
->updateResponse(rsp_pkt
, *slavePorts
[rsp_port_id
]);
425 // we send the response after the current packet, even if the
426 // response is not for this packet (e.g. cache clean operation
427 // where both the request and the write packet have to cross
428 // the destination xbar before the response is sent.)
429 Tick response_time
= clockEdge() + pkt
->headerDelay
;
430 rsp_pkt
->headerDelay
= 0;
432 slavePorts
[rsp_port_id
]->schedTimingResp(rsp_pkt
, response_time
);
439 CoherentXBar::recvTimingResp(PacketPtr pkt
, PortID master_port_id
)
441 // determine the source port based on the id
442 RequestPort
*src_port
= masterPorts
[master_port_id
];
444 // determine the destination
445 const auto route_lookup
= routeTo
.find(pkt
->req
);
446 assert(route_lookup
!= routeTo
.end());
447 const PortID slave_port_id
= route_lookup
->second
;
448 assert(slave_port_id
!= InvalidPortID
);
449 assert(slave_port_id
< respLayers
.size());
451 // test if the crossbar should be considered occupied for the
453 if (!respLayers
[slave_port_id
]->tryTiming(src_port
)) {
454 DPRINTF(CoherentXBar
, "%s: src %s packet %s BUSY\n", __func__
,
455 src_port
->name(), pkt
->print());
459 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
460 src_port
->name(), pkt
->print());
462 // store size and command as they might be modified when
463 // forwarding the packet
464 unsigned int pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
465 unsigned int pkt_cmd
= pkt
->cmdToIndex();
467 // a response sees the response latency
468 Tick xbar_delay
= responseLatency
* clockPeriod();
470 // set the packet header and payload delay
471 calcPacketTiming(pkt
, xbar_delay
);
473 // determine how long to be crossbar layer is busy
474 Tick packetFinishTime
= clockEdge(headerLatency
) + pkt
->payloadDelay
;
476 if (snoopFilter
&& !system
->bypassCaches()) {
477 // let the snoop filter inspect the response and update its state
478 snoopFilter
->updateResponse(pkt
, *slavePorts
[slave_port_id
]);
481 // send the packet through the destination slave port and pay for
482 // any outstanding header delay
483 Tick latency
= pkt
->headerDelay
;
484 pkt
->headerDelay
= 0;
485 slavePorts
[slave_port_id
]->schedTimingResp(pkt
, curTick() + latency
);
487 // remove the request from the routing table
488 routeTo
.erase(route_lookup
);
490 respLayers
[slave_port_id
]->succeededTiming(packetFinishTime
);
493 pktCount
[slave_port_id
][master_port_id
]++;
494 pktSize
[slave_port_id
][master_port_id
] += pkt_size
;
495 transDist
[pkt_cmd
]++;
501 CoherentXBar::recvTimingSnoopReq(PacketPtr pkt
, PortID master_port_id
)
503 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
504 masterPorts
[master_port_id
]->name(), pkt
->print());
506 // update stats here as we know the forwarding will succeed
507 unsigned int pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
508 transDist
[pkt
->cmdToIndex()]++;
510 snoopTraffic
+= pkt_size
;
512 // we should only see express snoops from caches
513 assert(pkt
->isExpressSnoop());
515 // set the packet header and payload delay, for now use forward latency
516 // @todo Assess the choice of latency further
517 calcPacketTiming(pkt
, forwardLatency
* clockPeriod());
519 // remember if a cache has already committed to responding so we
520 // can see if it changes during the snooping
521 const bool cache_responding
= pkt
->cacheResponding();
523 assert(pkt
->snoopDelay
== 0);
526 // let the Snoop Filter work its magic and guide probing
527 auto sf_res
= snoopFilter
->lookupSnoop(pkt
);
528 // the time required by a packet to be delivered through
529 // the xbar has to be charged also with to lookup latency
530 // of the snoop filter
531 pkt
->headerDelay
+= sf_res
.second
* clockPeriod();
532 DPRINTF(CoherentXBar
, "%s: src %s packet %s SF size: %i lat: %i\n",
533 __func__
, masterPorts
[master_port_id
]->name(), pkt
->print(),
534 sf_res
.first
.size(), sf_res
.second
);
536 // forward to all snoopers
537 forwardTiming(pkt
, InvalidPortID
, sf_res
.first
);
539 forwardTiming(pkt
, InvalidPortID
);
542 // add the snoop delay to our header delay, and then reset it
543 pkt
->headerDelay
+= pkt
->snoopDelay
;
546 // if we can expect a response, remember how to route it
547 if (!cache_responding
&& pkt
->cacheResponding()) {
548 assert(routeTo
.find(pkt
->req
) == routeTo
.end());
549 routeTo
[pkt
->req
] = master_port_id
;
552 // a snoop request came from a connected slave device (one of
553 // our master ports), and if it is not coming from the slave
554 // device responsible for the address range something is
555 // wrong, hence there is nothing further to do as the packet
556 // would be going back to where it came from
557 assert(findPort(pkt
->getAddrRange()) == master_port_id
);
561 CoherentXBar::recvTimingSnoopResp(PacketPtr pkt
, PortID slave_port_id
)
563 // determine the source port based on the id
564 ResponsePort
* src_port
= slavePorts
[slave_port_id
];
566 // get the destination
567 const auto route_lookup
= routeTo
.find(pkt
->req
);
568 assert(route_lookup
!= routeTo
.end());
569 const PortID dest_port_id
= route_lookup
->second
;
570 assert(dest_port_id
!= InvalidPortID
);
572 // determine if the response is from a snoop request we
573 // created as the result of a normal request (in which case it
574 // should be in the outstandingSnoop), or if we merely forwarded
575 // someone else's snoop request
576 const bool forwardAsSnoop
= outstandingSnoop
.find(pkt
->req
) ==
577 outstandingSnoop
.end();
579 // test if the crossbar should be considered occupied for the
580 // current port, note that the check is bypassed if the response
581 // is being passed on as a normal response since this is occupying
582 // the response layer rather than the snoop response layer
583 if (forwardAsSnoop
) {
584 assert(dest_port_id
< snoopLayers
.size());
585 if (!snoopLayers
[dest_port_id
]->tryTiming(src_port
)) {
586 DPRINTF(CoherentXBar
, "%s: src %s packet %s BUSY\n", __func__
,
587 src_port
->name(), pkt
->print());
591 // get the master port that mirrors this slave port internally
592 RequestPort
* snoop_port
= snoopRespPorts
[slave_port_id
];
593 assert(dest_port_id
< respLayers
.size());
594 if (!respLayers
[dest_port_id
]->tryTiming(snoop_port
)) {
595 DPRINTF(CoherentXBar
, "%s: src %s packet %s BUSY\n", __func__
,
596 snoop_port
->name(), pkt
->print());
601 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
602 src_port
->name(), pkt
->print());
604 // store size and command as they might be modified when
605 // forwarding the packet
606 unsigned int pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
607 unsigned int pkt_cmd
= pkt
->cmdToIndex();
609 // responses are never express snoops
610 assert(!pkt
->isExpressSnoop());
612 // a snoop response sees the snoop response latency, and if it is
613 // forwarded as a normal response, the response latency
615 (forwardAsSnoop
? snoopResponseLatency
: responseLatency
) *
618 // set the packet header and payload delay
619 calcPacketTiming(pkt
, xbar_delay
);
621 // determine how long to be crossbar layer is busy
622 Tick packetFinishTime
= clockEdge(headerLatency
) + pkt
->payloadDelay
;
624 // forward it either as a snoop response or a normal response
625 if (forwardAsSnoop
) {
626 // this is a snoop response to a snoop request we forwarded,
627 // e.g. coming from the L1 and going to the L2, and it should
628 // be forwarded as a snoop response
631 // update the probe filter so that it can properly track the line
632 snoopFilter
->updateSnoopForward(pkt
, *slavePorts
[slave_port_id
],
633 *masterPorts
[dest_port_id
]);
636 bool success M5_VAR_USED
=
637 masterPorts
[dest_port_id
]->sendTimingSnoopResp(pkt
);
638 pktCount
[slave_port_id
][dest_port_id
]++;
639 pktSize
[slave_port_id
][dest_port_id
] += pkt_size
;
642 snoopLayers
[dest_port_id
]->succeededTiming(packetFinishTime
);
644 // we got a snoop response on one of our slave ports,
645 // i.e. from a coherent master connected to the crossbar, and
646 // since we created the snoop request as part of recvTiming,
647 // this should now be a normal response again
648 outstandingSnoop
.erase(pkt
->req
);
650 // this is a snoop response from a coherent master, hence it
651 // should never go back to where the snoop response came from,
652 // but instead to where the original request came from
653 assert(slave_port_id
!= dest_port_id
);
656 // update the probe filter so that it can properly track the line
657 snoopFilter
->updateSnoopResponse(pkt
, *slavePorts
[slave_port_id
],
658 *slavePorts
[dest_port_id
]);
661 DPRINTF(CoherentXBar
, "%s: src %s packet %s FWD RESP\n", __func__
,
662 src_port
->name(), pkt
->print());
664 // as a normal response, it should go back to a master through
665 // one of our slave ports, we also pay for any outstanding
667 Tick latency
= pkt
->headerDelay
;
668 pkt
->headerDelay
= 0;
669 slavePorts
[dest_port_id
]->schedTimingResp(pkt
, curTick() + latency
);
671 respLayers
[dest_port_id
]->succeededTiming(packetFinishTime
);
674 // remove the request from the routing table
675 routeTo
.erase(route_lookup
);
678 transDist
[pkt_cmd
]++;
680 snoopTraffic
+= pkt_size
;
687 CoherentXBar::forwardTiming(PacketPtr pkt
, PortID exclude_slave_port_id
,
688 const std::vector
<QueuedSlavePort
*>& dests
)
690 DPRINTF(CoherentXBar
, "%s for %s\n", __func__
, pkt
->print());
692 // snoops should only happen if the system isn't bypassing caches
693 assert(!system
->bypassCaches());
697 for (const auto& p
: dests
) {
698 // we could have gotten this request from a snooping master
699 // (corresponding to our own slave port that is also in
700 // snoopPorts) and should not send it back to where it came
702 if (exclude_slave_port_id
== InvalidPortID
||
703 p
->getId() != exclude_slave_port_id
) {
704 // cache is not allowed to refuse snoop
705 p
->sendTimingSnoopReq(pkt
);
710 // Stats for fanout of this forward operation
711 snoopFanout
.sample(fanout
);
715 CoherentXBar::recvReqRetry(PortID master_port_id
)
717 // responses and snoop responses never block on forwarding them,
718 // so the retry will always be coming from a port to which we
719 // tried to forward a request
720 reqLayers
[master_port_id
]->recvRetry();
724 CoherentXBar::recvAtomicBackdoor(PacketPtr pkt
, PortID slave_port_id
,
725 MemBackdoorPtr
*backdoor
)
727 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
728 slavePorts
[slave_port_id
]->name(), pkt
->print());
730 unsigned int pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
731 unsigned int pkt_cmd
= pkt
->cmdToIndex();
733 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
734 Tick snoop_response_latency
= 0;
736 // is this the destination point for this packet? (e.g. true if
737 // this xbar is the PoC for a cache maintenance operation to the
738 // PoC) otherwise the destination is any cache that can satisfy
740 const bool is_destination
= isDestination(pkt
);
742 const bool snoop_caches
= !system
->bypassCaches() &&
743 pkt
->cmd
!= MemCmd::WriteClean
;
745 // forward to all snoopers but the source
746 std::pair
<MemCmd
, Tick
> snoop_result
;
748 // check with the snoop filter where to forward this packet
750 snoopFilter
->lookupRequest(pkt
, *slavePorts
[slave_port_id
]);
751 snoop_response_latency
+= sf_res
.second
* clockPeriod();
752 DPRINTF(CoherentXBar
, "%s: src %s packet %s SF size: %i lat: %i\n",
753 __func__
, slavePorts
[slave_port_id
]->name(), pkt
->print(),
754 sf_res
.first
.size(), sf_res
.second
);
756 // let the snoop filter know about the success of the send
757 // operation, and do it even before sending it onwards to
758 // avoid situations where atomic upward snoops sneak in
759 // between and change the filter state
760 snoopFilter
->finishRequest(false, pkt
->getAddr(), pkt
->isSecure());
762 if (pkt
->isEviction()) {
763 // for block-evicting packets, i.e. writebacks and
764 // clean evictions, there is no need to snoop up, as
765 // all we do is determine if the block is cached or
766 // not, instead just set it here based on the snoop
768 if (!sf_res
.first
.empty())
769 pkt
->setBlockCached();
771 snoop_result
= forwardAtomic(pkt
, slave_port_id
, InvalidPortID
,
775 snoop_result
= forwardAtomic(pkt
, slave_port_id
);
777 snoop_response_cmd
= snoop_result
.first
;
778 snoop_response_latency
+= snoop_result
.second
;
781 // set up a sensible default value
782 Tick response_latency
= 0;
784 const bool sink_packet
= sinkPacket(pkt
);
786 // even if we had a snoop response, we must continue and also
787 // perform the actual request at the destination
788 PortID master_port_id
= findPort(pkt
->getAddrRange());
791 DPRINTF(CoherentXBar
, "%s: Not forwarding %s\n", __func__
,
794 if (forwardPacket(pkt
)) {
795 // make sure that the write request (e.g., WriteClean)
796 // will stop at the memory below if this crossbar is its
798 if (pkt
->isWrite() && is_destination
) {
799 pkt
->clearWriteThrough();
802 // forward the request to the appropriate destination
803 auto master
= masterPorts
[master_port_id
];
804 response_latency
= backdoor
?
805 master
->sendAtomicBackdoor(pkt
, *backdoor
) :
806 master
->sendAtomic(pkt
);
808 // if it does not need a response we sink the packet above
809 assert(pkt
->needsResponse());
815 // stats updates for the request
816 pktCount
[slave_port_id
][master_port_id
]++;
817 pktSize
[slave_port_id
][master_port_id
] += pkt_size
;
818 transDist
[pkt_cmd
]++;
821 // if lower levels have replied, tell the snoop filter
822 if (!system
->bypassCaches() && snoopFilter
&& pkt
->isResponse()) {
823 snoopFilter
->updateResponse(pkt
, *slavePorts
[slave_port_id
]);
826 // if we got a response from a snooper, restore it here
827 if (snoop_response_cmd
!= MemCmd::InvalidCmd
) {
828 // no one else should have responded
829 assert(!pkt
->isResponse());
830 pkt
->cmd
= snoop_response_cmd
;
831 response_latency
= snoop_response_latency
;
834 // If this is the destination of the cache clean operation the
835 // crossbar is responsible for responding. This crossbar will
836 // respond when the cache clean is complete. An atomic cache clean
837 // is complete when the crossbars receives the cache clean
838 // request (CleanSharedReq, CleanInvalidReq), as either:
839 // * no cache above had a dirty copy of the block as indicated by
840 // the satisfied flag of the packet, or
841 // * the crossbar has already seen the corresponding write
842 // (WriteClean) which updates the block in the memory below.
843 if (pkt
->isClean() && isDestination(pkt
) && pkt
->satisfied()) {
844 auto it
= outstandingCMO
.find(pkt
->id
);
845 assert(it
!= outstandingCMO
.end());
846 // we are responding right away
847 outstandingCMO
.erase(it
);
848 } else if (pkt
->cmd
== MemCmd::WriteClean
&& isDestination(pkt
)) {
849 // if this is the destination of the operation, the xbar
850 // sends the responce to the cache clean operation only
851 // after having encountered the cache clean request
852 auto M5_VAR_USED ret
= outstandingCMO
.emplace(pkt
->id
, nullptr);
853 // in atomic mode we know that the WriteClean packet should
854 // precede the clean request
858 // add the response data
859 if (pkt
->isResponse()) {
860 pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
861 pkt_cmd
= pkt
->cmdToIndex();
864 pktCount
[slave_port_id
][master_port_id
]++;
865 pktSize
[slave_port_id
][master_port_id
] += pkt_size
;
866 transDist
[pkt_cmd
]++;
869 // @todo: Not setting header time
870 pkt
->payloadDelay
= response_latency
;
871 return response_latency
;
875 CoherentXBar::recvAtomicSnoop(PacketPtr pkt
, PortID master_port_id
)
877 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
878 masterPorts
[master_port_id
]->name(), pkt
->print());
880 // add the request snoop data
881 unsigned int pkt_size
= pkt
->hasData() ? pkt
->getSize() : 0;
883 snoopTraffic
+= pkt_size
;
885 // forward to all snoopers
886 std::pair
<MemCmd
, Tick
> snoop_result
;
887 Tick snoop_response_latency
= 0;
889 auto sf_res
= snoopFilter
->lookupSnoop(pkt
);
890 snoop_response_latency
+= sf_res
.second
* clockPeriod();
891 DPRINTF(CoherentXBar
, "%s: src %s packet %s SF size: %i lat: %i\n",
892 __func__
, masterPorts
[master_port_id
]->name(), pkt
->print(),
893 sf_res
.first
.size(), sf_res
.second
);
894 snoop_result
= forwardAtomic(pkt
, InvalidPortID
, master_port_id
,
897 snoop_result
= forwardAtomic(pkt
, InvalidPortID
);
899 MemCmd snoop_response_cmd
= snoop_result
.first
;
900 snoop_response_latency
+= snoop_result
.second
;
902 if (snoop_response_cmd
!= MemCmd::InvalidCmd
)
903 pkt
->cmd
= snoop_response_cmd
;
905 // add the response snoop data
906 if (pkt
->isResponse()) {
910 // @todo: Not setting header time
911 pkt
->payloadDelay
= snoop_response_latency
;
912 return snoop_response_latency
;
915 std::pair
<MemCmd
, Tick
>
916 CoherentXBar::forwardAtomic(PacketPtr pkt
, PortID exclude_slave_port_id
,
917 PortID source_master_port_id
,
918 const std::vector
<QueuedSlavePort
*>& dests
)
920 // the packet may be changed on snoops, record the original
921 // command to enable us to restore it between snoops so that
922 // additional snoops can take place properly
923 MemCmd orig_cmd
= pkt
->cmd
;
924 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
925 Tick snoop_response_latency
= 0;
927 // snoops should only happen if the system isn't bypassing caches
928 assert(!system
->bypassCaches());
932 for (const auto& p
: dests
) {
933 // we could have gotten this request from a snooping master
934 // (corresponding to our own slave port that is also in
935 // snoopPorts) and should not send it back to where it came
937 if (exclude_slave_port_id
!= InvalidPortID
&&
938 p
->getId() == exclude_slave_port_id
)
941 Tick latency
= p
->sendAtomicSnoop(pkt
);
944 // in contrast to a functional access, we have to keep on
945 // going as all snoopers must be updated even if we get a
947 if (!pkt
->isResponse())
950 // response from snoop agent
951 assert(pkt
->cmd
!= orig_cmd
);
952 assert(pkt
->cacheResponding());
953 // should only happen once
954 assert(snoop_response_cmd
== MemCmd::InvalidCmd
);
955 // save response state
956 snoop_response_cmd
= pkt
->cmd
;
957 snoop_response_latency
= latency
;
960 // Handle responses by the snoopers and differentiate between
961 // responses to requests from above and snoops from below
962 if (source_master_port_id
!= InvalidPortID
) {
963 // Getting a response for a snoop from below
964 assert(exclude_slave_port_id
== InvalidPortID
);
965 snoopFilter
->updateSnoopForward(pkt
, *p
,
966 *masterPorts
[source_master_port_id
]);
968 // Getting a response for a request from above
969 assert(source_master_port_id
== InvalidPortID
);
970 snoopFilter
->updateSnoopResponse(pkt
, *p
,
971 *slavePorts
[exclude_slave_port_id
]);
974 // restore original packet state for remaining snoopers
979 snoopFanout
.sample(fanout
);
981 // the packet is restored as part of the loop and any potential
982 // snoop response is part of the returned pair
983 return std::make_pair(snoop_response_cmd
, snoop_response_latency
);
987 CoherentXBar::recvFunctional(PacketPtr pkt
, PortID slave_port_id
)
989 if (!pkt
->isPrint()) {
990 // don't do DPRINTFs on PrintReq as it clutters up the output
991 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
992 slavePorts
[slave_port_id
]->name(), pkt
->print());
995 if (!system
->bypassCaches()) {
996 // forward to all snoopers but the source
997 forwardFunctional(pkt
, slave_port_id
);
1000 // there is no need to continue if the snooping has found what we
1001 // were looking for and the packet is already a response
1002 if (!pkt
->isResponse()) {
1003 // since our slave ports are queued ports we need to check them as well
1004 for (const auto& p
: slavePorts
) {
1005 // if we find a response that has the data, then the
1006 // downstream caches/memories may be out of date, so simply stop
1008 if (p
->trySatisfyFunctional(pkt
)) {
1009 if (pkt
->needsResponse())
1010 pkt
->makeResponse();
1015 PortID dest_id
= findPort(pkt
->getAddrRange());
1017 masterPorts
[dest_id
]->sendFunctional(pkt
);
1022 CoherentXBar::recvFunctionalSnoop(PacketPtr pkt
, PortID master_port_id
)
1024 if (!pkt
->isPrint()) {
1025 // don't do DPRINTFs on PrintReq as it clutters up the output
1026 DPRINTF(CoherentXBar
, "%s: src %s packet %s\n", __func__
,
1027 masterPorts
[master_port_id
]->name(), pkt
->print());
1030 for (const auto& p
: slavePorts
) {
1031 if (p
->trySatisfyFunctional(pkt
)) {
1032 if (pkt
->needsResponse())
1033 pkt
->makeResponse();
1038 // forward to all snoopers
1039 forwardFunctional(pkt
, InvalidPortID
);
1043 CoherentXBar::forwardFunctional(PacketPtr pkt
, PortID exclude_slave_port_id
)
1045 // snoops should only happen if the system isn't bypassing caches
1046 assert(!system
->bypassCaches());
1048 for (const auto& p
: snoopPorts
) {
1049 // we could have gotten this request from a snooping master
1050 // (corresponding to our own slave port that is also in
1051 // snoopPorts) and should not send it back to where it came
1053 if (exclude_slave_port_id
== InvalidPortID
||
1054 p
->getId() != exclude_slave_port_id
)
1055 p
->sendFunctionalSnoop(pkt
);
1057 // if we get a response we are done
1058 if (pkt
->isResponse()) {
1065 CoherentXBar::sinkPacket(const PacketPtr pkt
) const
1067 // we can sink the packet if:
1068 // 1) the crossbar is the point of coherency, and a cache is
1069 // responding after being snooped
1070 // 2) the crossbar is the point of coherency, and the packet is a
1071 // coherency packet (not a read or a write) that does not
1072 // require a response
1073 // 3) this is a clean evict or clean writeback, but the packet is
1074 // found in a cache above this crossbar
1075 // 4) a cache is responding after being snooped, and the packet
1076 // either does not need the block to be writable, or the cache
1077 // that has promised to respond (setting the cache responding
1078 // flag) is providing writable and thus had a Modified block,
1079 // and no further action is needed
1080 return (pointOfCoherency
&& pkt
->cacheResponding()) ||
1081 (pointOfCoherency
&& !(pkt
->isRead() || pkt
->isWrite()) &&
1082 !pkt
->needsResponse()) ||
1083 (pkt
->isCleanEviction() && pkt
->isBlockCached()) ||
1084 (pkt
->cacheResponding() &&
1085 (!pkt
->needsWritable() || pkt
->responderHadWritable()));
1089 CoherentXBar::forwardPacket(const PacketPtr pkt
)
1091 // we are forwarding the packet if:
1092 // 1) this is a cache clean request to the PoU/PoC and this
1093 // crossbar is above the PoU/PoC
1094 // 2) this is a read or a write
1095 // 3) this crossbar is above the point of coherency
1096 if (pkt
->isClean()) {
1097 return !isDestination(pkt
);
1099 return pkt
->isRead() || pkt
->isWrite() || !pointOfCoherency
;
1104 CoherentXBar::regStats()
1106 BaseXBar::regStats();
1108 snoopFanout
.init(0, snoopPorts
.size(), 1);
1112 CoherentXBarParams::create()
1114 return new CoherentXBar(this);