2 * Copyright (c) 2011-2015, 2018-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/xbar.hh"
48 #include "base/logging.hh"
49 #include "base/trace.hh"
50 #include "debug/AddrRanges.hh"
51 #include "debug/Drain.hh"
52 #include "debug/XBar.hh"
54 BaseXBar::BaseXBar(const BaseXBarParams
&p
)
56 frontendLatency(p
.frontend_latency
),
57 forwardLatency(p
.forward_latency
),
58 responseLatency(p
.response_latency
),
59 headerLatency(p
.header_latency
),
61 gotAddrRanges(p
.port_default_connection_count
+
62 p
.port_mem_side_ports_connection_count
, false),
63 gotAllAddrRanges(false), defaultPortID(InvalidPortID
),
64 useDefaultRange(p
.use_default_range
),
66 ADD_STAT(transDist
, UNIT_COUNT
, "Transaction distribution"),
67 ADD_STAT(pktCount
, UNIT_COUNT
,
68 "Packet count per connected requestor and responder"),
69 ADD_STAT(pktSize
, UNIT_BYTE
,
70 "Cumulative packet size per connected requestor and responder "
77 for (auto port
: memSidePorts
)
80 for (auto port
: cpuSidePorts
)
85 BaseXBar::getPort(const std::string
&if_name
, PortID idx
)
87 if (if_name
== "mem_side_ports" && idx
< memSidePorts
.size()) {
88 // the memory-side ports index translates directly to the vector
90 return *memSidePorts
[idx
];
91 } else if (if_name
== "default") {
92 return *memSidePorts
[defaultPortID
];
93 } else if (if_name
== "cpu_side_ports" && idx
< cpuSidePorts
.size()) {
94 // the CPU-side ports index translates directly to the vector position
95 return *cpuSidePorts
[idx
];
97 return ClockedObject::getPort(if_name
, idx
);
102 BaseXBar::calcPacketTiming(PacketPtr pkt
, Tick header_delay
)
104 // the crossbar will be called at a time that is not necessarily
105 // coinciding with its own clock, so start by determining how long
106 // until the next clock edge (could be zero)
107 Tick offset
= clockEdge() - curTick();
109 // the header delay depends on the path through the crossbar, and
110 // we therefore rely on the caller to provide the actual
112 pkt
->headerDelay
+= offset
+ header_delay
;
114 // note that we add the header delay to the existing value, and
115 // align it to the crossbar clock
117 // do a quick sanity check to ensure the timings are not being
118 // ignored, note that this specific value may cause problems for
119 // slower interconnects
120 panic_if(pkt
->headerDelay
> SimClock::Int::us
,
121 "Encountered header delay exceeding 1 us\n");
123 if (pkt
->hasData()) {
124 // the payloadDelay takes into account the relative time to
125 // deliver the payload of the packet, after the header delay,
126 // we take the maximum since the payload delay could already
127 // be longer than what this parcitular crossbar enforces.
128 pkt
->payloadDelay
= std::max
<Tick
>(pkt
->payloadDelay
,
129 divCeil(pkt
->getSize(), width
) *
133 // the payload delay is not paying for the clock offset as that is
134 // already done using the header delay, and the payload delay is
135 // also used to determine how long the crossbar layer is busy and
136 // thus regulates throughput
139 template <typename SrcType
, typename DstType
>
140 BaseXBar::Layer
<SrcType
, DstType
>::Layer(DstType
& _port
, BaseXBar
& _xbar
,
141 const std::string
& _name
) :
142 Stats::Group(&_xbar
, _name
.c_str()),
143 port(_port
), xbar(_xbar
), _name(xbar
.name() + "." + _name
), state(IDLE
),
144 waitingForPeer(NULL
), releaseEvent([this]{ releaseLayer(); }, name()),
145 ADD_STAT(occupancy
, UNIT_TICK
, "Layer occupancy (ticks)"),
146 ADD_STAT(utilization
, UNIT_RATIO
, "Layer utilization")
149 .flags(Stats::nozero
);
153 .flags(Stats::nozero
);
155 utilization
= occupancy
/ simTicks
;
158 template <typename SrcType
, typename DstType
>
159 void BaseXBar::Layer
<SrcType
, DstType
>::occupyLayer(Tick until
)
161 // ensure the state is busy at this point, as the layer should
162 // transition from idle as soon as it has decided to forward the
163 // packet to prevent any follow-on calls to sendTiming seeing an
165 assert(state
== BUSY
);
167 // until should never be 0 as express snoops never occupy the layer
169 xbar
.schedule(releaseEvent
, until
);
171 // account for the occupied ticks
172 occupancy
+= until
- curTick();
174 DPRINTF(BaseXBar
, "The crossbar layer is now busy from tick %d to %d\n",
178 template <typename SrcType
, typename DstType
>
180 BaseXBar::Layer
<SrcType
, DstType
>::tryTiming(SrcType
* src_port
)
182 // if we are in the retry state, we will not see anything but the
183 // retrying port (or in the case of the snoop ports the snoop
184 // response port that mirrors the actual CPU-side port) as we leave
185 // this state again in zero time if the peer does not immediately
186 // call the layer when receiving the retry
188 // first we see if the layer is busy, next we check if the
189 // destination port is already engaged in a transaction waiting
190 // for a retry from the peer
191 if (state
== BUSY
|| waitingForPeer
!= NULL
) {
192 // the port should not be waiting already
193 assert(std::find(waitingForLayer
.begin(), waitingForLayer
.end(),
194 src_port
) == waitingForLayer
.end());
196 // put the port at the end of the retry list waiting for the
197 // layer to be freed up (and in the case of a busy peer, for
198 // that transaction to go through, and then the layer to free
200 waitingForLayer
.push_back(src_port
);
209 template <typename SrcType
, typename DstType
>
211 BaseXBar::Layer
<SrcType
, DstType
>::succeededTiming(Tick busy_time
)
213 // we should have gone from idle or retry to busy in the tryTiming
215 assert(state
== BUSY
);
217 // occupy the layer accordingly
218 occupyLayer(busy_time
);
221 template <typename SrcType
, typename DstType
>
223 BaseXBar::Layer
<SrcType
, DstType
>::failedTiming(SrcType
* src_port
,
226 // ensure no one got in between and tried to send something to
228 assert(waitingForPeer
== NULL
);
230 // if the source port is the current retrying one or not, we have
231 // failed in forwarding and should track that we are now waiting
232 // for the peer to send a retry
233 waitingForPeer
= src_port
;
235 // we should have gone from idle or retry to busy in the tryTiming
237 assert(state
== BUSY
);
239 // occupy the bus accordingly
240 occupyLayer(busy_time
);
243 template <typename SrcType
, typename DstType
>
245 BaseXBar::Layer
<SrcType
, DstType
>::releaseLayer()
247 // releasing the bus means we should now be idle
248 assert(state
== BUSY
);
249 assert(!releaseEvent
.scheduled());
254 // bus layer is now idle, so if someone is waiting we can retry
255 if (!waitingForLayer
.empty()) {
256 // there is no point in sending a retry if someone is still
257 // waiting for the peer
258 if (waitingForPeer
== NULL
)
260 } else if (waitingForPeer
== NULL
&& drainState() == DrainState::Draining
) {
261 DPRINTF(Drain
, "Crossbar done draining, signaling drain manager\n");
262 //If we weren't able to drain before, do it now.
267 template <typename SrcType
, typename DstType
>
269 BaseXBar::Layer
<SrcType
, DstType
>::retryWaiting()
271 // this should never be called with no one waiting
272 assert(!waitingForLayer
.empty());
274 // we always go to retrying from idle
275 assert(state
== IDLE
);
280 // set the retrying port to the front of the retry list and pop it
282 SrcType
* retryingPort
= waitingForLayer
.front();
283 waitingForLayer
.pop_front();
285 // tell the port to retry, which in some cases ends up calling the
287 sendRetry(retryingPort
);
289 // If the layer is still in the retry state, sendTiming wasn't
290 // called in zero time (e.g. the cache does this when a writeback
292 if (state
== RETRY
) {
293 // update the state to busy and reset the retrying port, we
294 // have done our bit and sent the retry
297 // occupy the crossbar layer until the next clock edge
298 occupyLayer(xbar
.clockEdge());
302 template <typename SrcType
, typename DstType
>
304 BaseXBar::Layer
<SrcType
, DstType
>::recvRetry()
306 // we should never get a retry without having failed to forward
307 // something to this port
308 assert(waitingForPeer
!= NULL
);
310 // add the port where the failed packet originated to the front of
311 // the waiting ports for the layer, this allows us to call retry
312 // on the port immediately if the crossbar layer is idle
313 waitingForLayer
.push_front(waitingForPeer
);
315 // we are no longer waiting for the peer
316 waitingForPeer
= NULL
;
318 // if the layer is idle, retry this port straight away, if we
319 // are busy, then simply let the port wait for its turn
323 assert(state
== BUSY
);
328 BaseXBar::findPort(AddrRange addr_range
)
330 // we should never see any address lookups before we've got the
331 // ranges of all connected CPU-side-port modules
332 assert(gotAllAddrRanges
);
334 // Check the address map interval tree
335 auto i
= portMap
.contains(addr_range
);
336 if (i
!= portMap
.end()) {
340 // Check if this matches the default range
341 if (useDefaultRange
) {
342 if (addr_range
.isSubset(defaultRange
)) {
343 DPRINTF(AddrRanges
, " found addr %s on default\n",
344 addr_range
.to_string());
345 return defaultPortID
;
347 } else if (defaultPortID
!= InvalidPortID
) {
348 DPRINTF(AddrRanges
, "Unable to find destination for %s, "
349 "will use default port\n", addr_range
.to_string());
350 return defaultPortID
;
353 // we should use the range for the default port and it did not
354 // match, or the default port is not set
355 fatal("Unable to find destination for %s on %s\n", addr_range
.to_string(),
359 /** Function called by the port when the crossbar is receiving a range change.*/
361 BaseXBar::recvRangeChange(PortID mem_side_port_id
)
363 DPRINTF(AddrRanges
, "Received range change from cpu_side_ports %s\n",
364 memSidePorts
[mem_side_port_id
]->getPeer());
366 // remember that we got a range from this memory-side port and thus the
367 // connected CPU-side-port module
368 gotAddrRanges
[mem_side_port_id
] = true;
370 // update the global flag
371 if (!gotAllAddrRanges
) {
372 // take a logical AND of all the ports and see if we got
373 // ranges from everyone
374 gotAllAddrRanges
= true;
375 std::vector
<bool>::const_iterator r
= gotAddrRanges
.begin();
376 while (gotAllAddrRanges
&& r
!= gotAddrRanges
.end()) {
377 gotAllAddrRanges
&= *r
++;
379 if (gotAllAddrRanges
)
380 DPRINTF(AddrRanges
, "Got address ranges from all responders\n");
383 // note that we could get the range from the default port at any
384 // point in time, and we cannot assume that the default range is
385 // set before the other ones are, so we do additional checks once
386 // all ranges are provided
387 if (mem_side_port_id
== defaultPortID
) {
388 // only update if we are indeed checking ranges for the
389 // default port since the port might not have a valid range
391 if (useDefaultRange
) {
392 AddrRangeList ranges
= memSidePorts
[mem_side_port_id
]->
395 if (ranges
.size() != 1)
396 fatal("Crossbar %s may only have a single default range",
399 defaultRange
= ranges
.front();
402 // the ports are allowed to update their address ranges
403 // dynamically, so remove any existing entries
404 if (gotAddrRanges
[mem_side_port_id
]) {
405 for (auto p
= portMap
.begin(); p
!= portMap
.end(); ) {
406 if (p
->second
== mem_side_port_id
)
407 // erasing invalidates the iterator, so advance it
408 // before the deletion takes place
415 AddrRangeList ranges
= memSidePorts
[mem_side_port_id
]->
418 for (const auto& r
: ranges
) {
419 DPRINTF(AddrRanges
, "Adding range %s for id %d\n",
420 r
.to_string(), mem_side_port_id
);
421 if (portMap
.insert(r
, mem_side_port_id
) == portMap
.end()) {
422 PortID conflict_id
= portMap
.intersects(r
)->second
;
423 fatal("%s has two ports responding within range "
427 memSidePorts
[mem_side_port_id
]->getPeer(),
428 memSidePorts
[conflict_id
]->getPeer());
433 // if we have received ranges from all our neighbouring CPU-side-port
434 // modules, go ahead and tell our connected memory-side-port modules in
435 // turn, this effectively assumes a tree structure of the system
436 if (gotAllAddrRanges
) {
437 DPRINTF(AddrRanges
, "Aggregating address ranges\n");
440 // start out with the default range
441 if (useDefaultRange
) {
442 if (!gotAddrRanges
[defaultPortID
])
443 fatal("Crossbar %s uses default range, but none provided",
446 xbarRanges
.push_back(defaultRange
);
447 DPRINTF(AddrRanges
, "-- Adding default %s\n",
448 defaultRange
.to_string());
451 // merge all interleaved ranges and add any range that is not
452 // a subset of the default range
453 std::vector
<AddrRange
> intlv_ranges
;
454 for (const auto& r
: portMap
) {
455 // if the range is interleaved then save it for now
456 if (r
.first
.interleaved()) {
457 // if we already got interleaved ranges that are not
458 // part of the same range, then first do a merge
459 // before we add the new one
460 if (!intlv_ranges
.empty() &&
461 !intlv_ranges
.back().mergesWith(r
.first
)) {
462 DPRINTF(AddrRanges
, "-- Merging range from %d ranges\n",
463 intlv_ranges
.size());
464 AddrRange
merged_range(intlv_ranges
);
465 // next decide if we keep the merged range or not
466 if (!(useDefaultRange
&&
467 merged_range
.isSubset(defaultRange
))) {
468 xbarRanges
.push_back(merged_range
);
469 DPRINTF(AddrRanges
, "-- Adding merged range %s\n",
470 merged_range
.to_string());
472 intlv_ranges
.clear();
474 intlv_ranges
.push_back(r
.first
);
476 // keep the current range if not a subset of the default
477 if (!(useDefaultRange
&&
478 r
.first
.isSubset(defaultRange
))) {
479 xbarRanges
.push_back(r
.first
);
480 DPRINTF(AddrRanges
, "-- Adding range %s\n",
481 r
.first
.to_string());
486 // if there is still interleaved ranges waiting to be merged,
487 // go ahead and do it
488 if (!intlv_ranges
.empty()) {
489 DPRINTF(AddrRanges
, "-- Merging range from %d ranges\n",
490 intlv_ranges
.size());
491 AddrRange
merged_range(intlv_ranges
);
492 if (!(useDefaultRange
&& merged_range
.isSubset(defaultRange
))) {
493 xbarRanges
.push_back(merged_range
);
494 DPRINTF(AddrRanges
, "-- Adding merged range %s\n",
495 merged_range
.to_string());
499 // also check that no range partially intersects with the
500 // default range, this has to be done after all ranges are set
501 // as there are no guarantees for when the default range is
502 // update with respect to the other ones
503 if (useDefaultRange
) {
504 for (const auto& r
: xbarRanges
) {
505 // see if the new range is partially
506 // overlapping the default range
507 if (r
.intersects(defaultRange
) &&
508 !r
.isSubset(defaultRange
))
509 fatal("Range %s intersects the " \
510 "default range of %s but is not a " \
511 "subset\n", r
.to_string(), name());
515 // tell all our neighbouring memory-side ports that our address
516 // ranges have changed
517 for (const auto& port
: cpuSidePorts
)
518 port
->sendRangeChange();
523 BaseXBar::getAddrRanges() const
525 // we should never be asked without first having sent a range
526 // change, and the latter is only done once we have all the ranges
527 // of the connected devices
528 assert(gotAllAddrRanges
);
530 // at the moment, this never happens, as there are no cycles in
531 // the range queries and no devices on the memory side of a crossbar
532 // (CPU, cache, bridge etc) actually care about the ranges of the
533 // ports they are connected to
535 DPRINTF(AddrRanges
, "Received address range request\n");
543 ClockedObject::regStats();
545 using namespace Stats
;
548 .init(MemCmd::NUM_MEM_CMDS
)
551 // get the string representation of the commands
552 for (int i
= 0; i
< MemCmd::NUM_MEM_CMDS
; i
++) {
554 const std::string
&cstr
= cmd
.toString();
555 transDist
.subname(i
, cstr
);
559 .init(cpuSidePorts
.size(), memSidePorts
.size())
560 .flags(total
| nozero
| nonan
);
563 .init(cpuSidePorts
.size(), memSidePorts
.size())
564 .flags(total
| nozero
| nonan
);
566 // both the packet count and total size are two-dimensional
567 // vectors, indexed by CPU-side port id and memory-side port id, thus the
568 // neighbouring memory-side ports and CPU-side ports, they do not
569 // differentiate what came from the memory-side ports and was forwarded to
570 // the CPU-side ports (requests and snoop responses) and what came from
571 // the CPU-side ports and was forwarded to the memory-side ports (responses
572 // and snoop requests)
573 for (int i
= 0; i
< cpuSidePorts
.size(); i
++) {
574 pktCount
.subname(i
, cpuSidePorts
[i
]->getPeer().name());
575 pktSize
.subname(i
, cpuSidePorts
[i
]->getPeer().name());
576 for (int j
= 0; j
< memSidePorts
.size(); j
++) {
577 pktCount
.ysubname(j
, memSidePorts
[j
]->getPeer().name());
578 pktSize
.ysubname(j
, memSidePorts
[j
]->getPeer().name());
583 template <typename SrcType
, typename DstType
>
585 BaseXBar::Layer
<SrcType
, DstType
>::drain()
587 //We should check that we're not "doing" anything, and that noone is
588 //waiting. We might be idle but have someone waiting if the device we
589 //contacted for a retry didn't actually retry.
591 DPRINTF(Drain
, "Crossbar not drained\n");
592 return DrainState::Draining
;
594 return DrainState::Drained
;
599 * Crossbar layer template instantiations. Could be removed with _impl.hh
600 * file, but since there are only two given options (RequestPort and
601 * ResponsePort) it seems a bit excessive at this point.
603 template class BaseXBar::Layer
<ResponsePort
, RequestPort
>;
604 template class BaseXBar::Layer
<RequestPort
, ResponsePort
>;