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
, "Transaction distribution"),
67 ADD_STAT(pktCount
, "Packet count per connected requestor and responder"),
68 ADD_STAT(pktSize
, "Cumulative packet size per connected requestor and "
75 for (auto port
: memSidePorts
)
78 for (auto port
: cpuSidePorts
)
83 BaseXBar::getPort(const std::string
&if_name
, PortID idx
)
85 if (if_name
== "mem_side_ports" && idx
< memSidePorts
.size()) {
86 // the memory-side ports index translates directly to the vector
88 return *memSidePorts
[idx
];
89 } else if (if_name
== "default") {
90 return *memSidePorts
[defaultPortID
];
91 } else if (if_name
== "cpu_side_ports" && idx
< cpuSidePorts
.size()) {
92 // the CPU-side ports index translates directly to the vector position
93 return *cpuSidePorts
[idx
];
95 return ClockedObject::getPort(if_name
, idx
);
100 BaseXBar::calcPacketTiming(PacketPtr pkt
, Tick header_delay
)
102 // the crossbar will be called at a time that is not necessarily
103 // coinciding with its own clock, so start by determining how long
104 // until the next clock edge (could be zero)
105 Tick offset
= clockEdge() - curTick();
107 // the header delay depends on the path through the crossbar, and
108 // we therefore rely on the caller to provide the actual
110 pkt
->headerDelay
+= offset
+ header_delay
;
112 // note that we add the header delay to the existing value, and
113 // align it to the crossbar clock
115 // do a quick sanity check to ensure the timings are not being
116 // ignored, note that this specific value may cause problems for
117 // slower interconnects
118 panic_if(pkt
->headerDelay
> SimClock::Int::us
,
119 "Encountered header delay exceeding 1 us\n");
121 if (pkt
->hasData()) {
122 // the payloadDelay takes into account the relative time to
123 // deliver the payload of the packet, after the header delay,
124 // we take the maximum since the payload delay could already
125 // be longer than what this parcitular crossbar enforces.
126 pkt
->payloadDelay
= std::max
<Tick
>(pkt
->payloadDelay
,
127 divCeil(pkt
->getSize(), width
) *
131 // the payload delay is not paying for the clock offset as that is
132 // already done using the header delay, and the payload delay is
133 // also used to determine how long the crossbar layer is busy and
134 // thus regulates throughput
137 template <typename SrcType
, typename DstType
>
138 BaseXBar::Layer
<SrcType
, DstType
>::Layer(DstType
& _port
, BaseXBar
& _xbar
,
139 const std::string
& _name
) :
140 Stats::Group(&_xbar
, _name
.c_str()),
141 port(_port
), xbar(_xbar
), _name(xbar
.name() + "." + _name
), state(IDLE
),
142 waitingForPeer(NULL
), releaseEvent([this]{ releaseLayer(); }, name()),
143 ADD_STAT(occupancy
, "Layer occupancy (ticks)"),
144 ADD_STAT(utilization
, "Layer utilization")
147 .flags(Stats::nozero
);
151 .flags(Stats::nozero
);
153 utilization
= occupancy
/ simTicks
;
156 template <typename SrcType
, typename DstType
>
157 void BaseXBar::Layer
<SrcType
, DstType
>::occupyLayer(Tick until
)
159 // ensure the state is busy at this point, as the layer should
160 // transition from idle as soon as it has decided to forward the
161 // packet to prevent any follow-on calls to sendTiming seeing an
163 assert(state
== BUSY
);
165 // until should never be 0 as express snoops never occupy the layer
167 xbar
.schedule(releaseEvent
, until
);
169 // account for the occupied ticks
170 occupancy
+= until
- curTick();
172 DPRINTF(BaseXBar
, "The crossbar layer is now busy from tick %d to %d\n",
176 template <typename SrcType
, typename DstType
>
178 BaseXBar::Layer
<SrcType
, DstType
>::tryTiming(SrcType
* src_port
)
180 // if we are in the retry state, we will not see anything but the
181 // retrying port (or in the case of the snoop ports the snoop
182 // response port that mirrors the actual CPU-side port) as we leave
183 // this state again in zero time if the peer does not immediately
184 // call the layer when receiving the retry
186 // first we see if the layer is busy, next we check if the
187 // destination port is already engaged in a transaction waiting
188 // for a retry from the peer
189 if (state
== BUSY
|| waitingForPeer
!= NULL
) {
190 // the port should not be waiting already
191 assert(std::find(waitingForLayer
.begin(), waitingForLayer
.end(),
192 src_port
) == waitingForLayer
.end());
194 // put the port at the end of the retry list waiting for the
195 // layer to be freed up (and in the case of a busy peer, for
196 // that transaction to go through, and then the layer to free
198 waitingForLayer
.push_back(src_port
);
207 template <typename SrcType
, typename DstType
>
209 BaseXBar::Layer
<SrcType
, DstType
>::succeededTiming(Tick busy_time
)
211 // we should have gone from idle or retry to busy in the tryTiming
213 assert(state
== BUSY
);
215 // occupy the layer accordingly
216 occupyLayer(busy_time
);
219 template <typename SrcType
, typename DstType
>
221 BaseXBar::Layer
<SrcType
, DstType
>::failedTiming(SrcType
* src_port
,
224 // ensure no one got in between and tried to send something to
226 assert(waitingForPeer
== NULL
);
228 // if the source port is the current retrying one or not, we have
229 // failed in forwarding and should track that we are now waiting
230 // for the peer to send a retry
231 waitingForPeer
= src_port
;
233 // we should have gone from idle or retry to busy in the tryTiming
235 assert(state
== BUSY
);
237 // occupy the bus accordingly
238 occupyLayer(busy_time
);
241 template <typename SrcType
, typename DstType
>
243 BaseXBar::Layer
<SrcType
, DstType
>::releaseLayer()
245 // releasing the bus means we should now be idle
246 assert(state
== BUSY
);
247 assert(!releaseEvent
.scheduled());
252 // bus layer is now idle, so if someone is waiting we can retry
253 if (!waitingForLayer
.empty()) {
254 // there is no point in sending a retry if someone is still
255 // waiting for the peer
256 if (waitingForPeer
== NULL
)
258 } else if (waitingForPeer
== NULL
&& drainState() == DrainState::Draining
) {
259 DPRINTF(Drain
, "Crossbar done draining, signaling drain manager\n");
260 //If we weren't able to drain before, do it now.
265 template <typename SrcType
, typename DstType
>
267 BaseXBar::Layer
<SrcType
, DstType
>::retryWaiting()
269 // this should never be called with no one waiting
270 assert(!waitingForLayer
.empty());
272 // we always go to retrying from idle
273 assert(state
== IDLE
);
278 // set the retrying port to the front of the retry list and pop it
280 SrcType
* retryingPort
= waitingForLayer
.front();
281 waitingForLayer
.pop_front();
283 // tell the port to retry, which in some cases ends up calling the
285 sendRetry(retryingPort
);
287 // If the layer is still in the retry state, sendTiming wasn't
288 // called in zero time (e.g. the cache does this when a writeback
290 if (state
== RETRY
) {
291 // update the state to busy and reset the retrying port, we
292 // have done our bit and sent the retry
295 // occupy the crossbar layer until the next clock edge
296 occupyLayer(xbar
.clockEdge());
300 template <typename SrcType
, typename DstType
>
302 BaseXBar::Layer
<SrcType
, DstType
>::recvRetry()
304 // we should never get a retry without having failed to forward
305 // something to this port
306 assert(waitingForPeer
!= NULL
);
308 // add the port where the failed packet originated to the front of
309 // the waiting ports for the layer, this allows us to call retry
310 // on the port immediately if the crossbar layer is idle
311 waitingForLayer
.push_front(waitingForPeer
);
313 // we are no longer waiting for the peer
314 waitingForPeer
= NULL
;
316 // if the layer is idle, retry this port straight away, if we
317 // are busy, then simply let the port wait for its turn
321 assert(state
== BUSY
);
326 BaseXBar::findPort(AddrRange addr_range
)
328 // we should never see any address lookups before we've got the
329 // ranges of all connected CPU-side-port modules
330 assert(gotAllAddrRanges
);
332 // Check the address map interval tree
333 auto i
= portMap
.contains(addr_range
);
334 if (i
!= portMap
.end()) {
338 // Check if this matches the default range
339 if (useDefaultRange
) {
340 if (addr_range
.isSubset(defaultRange
)) {
341 DPRINTF(AddrRanges
, " found addr %s on default\n",
342 addr_range
.to_string());
343 return defaultPortID
;
345 } else if (defaultPortID
!= InvalidPortID
) {
346 DPRINTF(AddrRanges
, "Unable to find destination for %s, "
347 "will use default port\n", addr_range
.to_string());
348 return defaultPortID
;
351 // we should use the range for the default port and it did not
352 // match, or the default port is not set
353 fatal("Unable to find destination for %s on %s\n", addr_range
.to_string(),
357 /** Function called by the port when the crossbar is receiving a range change.*/
359 BaseXBar::recvRangeChange(PortID mem_side_port_id
)
361 DPRINTF(AddrRanges
, "Received range change from cpu_side_ports %s\n",
362 memSidePorts
[mem_side_port_id
]->getPeer());
364 // remember that we got a range from this memory-side port and thus the
365 // connected CPU-side-port module
366 gotAddrRanges
[mem_side_port_id
] = true;
368 // update the global flag
369 if (!gotAllAddrRanges
) {
370 // take a logical AND of all the ports and see if we got
371 // ranges from everyone
372 gotAllAddrRanges
= true;
373 std::vector
<bool>::const_iterator r
= gotAddrRanges
.begin();
374 while (gotAllAddrRanges
&& r
!= gotAddrRanges
.end()) {
375 gotAllAddrRanges
&= *r
++;
377 if (gotAllAddrRanges
)
378 DPRINTF(AddrRanges
, "Got address ranges from all responders\n");
381 // note that we could get the range from the default port at any
382 // point in time, and we cannot assume that the default range is
383 // set before the other ones are, so we do additional checks once
384 // all ranges are provided
385 if (mem_side_port_id
== defaultPortID
) {
386 // only update if we are indeed checking ranges for the
387 // default port since the port might not have a valid range
389 if (useDefaultRange
) {
390 AddrRangeList ranges
= memSidePorts
[mem_side_port_id
]->
393 if (ranges
.size() != 1)
394 fatal("Crossbar %s may only have a single default range",
397 defaultRange
= ranges
.front();
400 // the ports are allowed to update their address ranges
401 // dynamically, so remove any existing entries
402 if (gotAddrRanges
[mem_side_port_id
]) {
403 for (auto p
= portMap
.begin(); p
!= portMap
.end(); ) {
404 if (p
->second
== mem_side_port_id
)
405 // erasing invalidates the iterator, so advance it
406 // before the deletion takes place
413 AddrRangeList ranges
= memSidePorts
[mem_side_port_id
]->
416 for (const auto& r
: ranges
) {
417 DPRINTF(AddrRanges
, "Adding range %s for id %d\n",
418 r
.to_string(), mem_side_port_id
);
419 if (portMap
.insert(r
, mem_side_port_id
) == portMap
.end()) {
420 PortID conflict_id
= portMap
.intersects(r
)->second
;
421 fatal("%s has two ports responding within range "
425 memSidePorts
[mem_side_port_id
]->getPeer(),
426 memSidePorts
[conflict_id
]->getPeer());
431 // if we have received ranges from all our neighbouring CPU-side-port
432 // modules, go ahead and tell our connected memory-side-port modules in
433 // turn, this effectively assumes a tree structure of the system
434 if (gotAllAddrRanges
) {
435 DPRINTF(AddrRanges
, "Aggregating address ranges\n");
438 // start out with the default range
439 if (useDefaultRange
) {
440 if (!gotAddrRanges
[defaultPortID
])
441 fatal("Crossbar %s uses default range, but none provided",
444 xbarRanges
.push_back(defaultRange
);
445 DPRINTF(AddrRanges
, "-- Adding default %s\n",
446 defaultRange
.to_string());
449 // merge all interleaved ranges and add any range that is not
450 // a subset of the default range
451 std::vector
<AddrRange
> intlv_ranges
;
452 for (const auto& r
: portMap
) {
453 // if the range is interleaved then save it for now
454 if (r
.first
.interleaved()) {
455 // if we already got interleaved ranges that are not
456 // part of the same range, then first do a merge
457 // before we add the new one
458 if (!intlv_ranges
.empty() &&
459 !intlv_ranges
.back().mergesWith(r
.first
)) {
460 DPRINTF(AddrRanges
, "-- Merging range from %d ranges\n",
461 intlv_ranges
.size());
462 AddrRange
merged_range(intlv_ranges
);
463 // next decide if we keep the merged range or not
464 if (!(useDefaultRange
&&
465 merged_range
.isSubset(defaultRange
))) {
466 xbarRanges
.push_back(merged_range
);
467 DPRINTF(AddrRanges
, "-- Adding merged range %s\n",
468 merged_range
.to_string());
470 intlv_ranges
.clear();
472 intlv_ranges
.push_back(r
.first
);
474 // keep the current range if not a subset of the default
475 if (!(useDefaultRange
&&
476 r
.first
.isSubset(defaultRange
))) {
477 xbarRanges
.push_back(r
.first
);
478 DPRINTF(AddrRanges
, "-- Adding range %s\n",
479 r
.first
.to_string());
484 // if there is still interleaved ranges waiting to be merged,
485 // go ahead and do it
486 if (!intlv_ranges
.empty()) {
487 DPRINTF(AddrRanges
, "-- Merging range from %d ranges\n",
488 intlv_ranges
.size());
489 AddrRange
merged_range(intlv_ranges
);
490 if (!(useDefaultRange
&& merged_range
.isSubset(defaultRange
))) {
491 xbarRanges
.push_back(merged_range
);
492 DPRINTF(AddrRanges
, "-- Adding merged range %s\n",
493 merged_range
.to_string());
497 // also check that no range partially intersects with the
498 // default range, this has to be done after all ranges are set
499 // as there are no guarantees for when the default range is
500 // update with respect to the other ones
501 if (useDefaultRange
) {
502 for (const auto& r
: xbarRanges
) {
503 // see if the new range is partially
504 // overlapping the default range
505 if (r
.intersects(defaultRange
) &&
506 !r
.isSubset(defaultRange
))
507 fatal("Range %s intersects the " \
508 "default range of %s but is not a " \
509 "subset\n", r
.to_string(), name());
513 // tell all our neighbouring memory-side ports that our address
514 // ranges have changed
515 for (const auto& port
: cpuSidePorts
)
516 port
->sendRangeChange();
521 BaseXBar::getAddrRanges() const
523 // we should never be asked without first having sent a range
524 // change, and the latter is only done once we have all the ranges
525 // of the connected devices
526 assert(gotAllAddrRanges
);
528 // at the moment, this never happens, as there are no cycles in
529 // the range queries and no devices on the memory side of a crossbar
530 // (CPU, cache, bridge etc) actually care about the ranges of the
531 // ports they are connected to
533 DPRINTF(AddrRanges
, "Received address range request\n");
541 ClockedObject::regStats();
543 using namespace Stats
;
546 .init(MemCmd::NUM_MEM_CMDS
)
549 // get the string representation of the commands
550 for (int i
= 0; i
< MemCmd::NUM_MEM_CMDS
; i
++) {
552 const std::string
&cstr
= cmd
.toString();
553 transDist
.subname(i
, cstr
);
557 .init(cpuSidePorts
.size(), memSidePorts
.size())
558 .flags(total
| nozero
| nonan
);
561 .init(cpuSidePorts
.size(), memSidePorts
.size())
562 .flags(total
| nozero
| nonan
);
564 // both the packet count and total size are two-dimensional
565 // vectors, indexed by CPU-side port id and memory-side port id, thus the
566 // neighbouring memory-side ports and CPU-side ports, they do not
567 // differentiate what came from the memory-side ports and was forwarded to
568 // the CPU-side ports (requests and snoop responses) and what came from
569 // the CPU-side ports and was forwarded to the memory-side ports (responses
570 // and snoop requests)
571 for (int i
= 0; i
< cpuSidePorts
.size(); i
++) {
572 pktCount
.subname(i
, cpuSidePorts
[i
]->getPeer().name());
573 pktSize
.subname(i
, cpuSidePorts
[i
]->getPeer().name());
574 for (int j
= 0; j
< memSidePorts
.size(); j
++) {
575 pktCount
.ysubname(j
, memSidePorts
[j
]->getPeer().name());
576 pktSize
.ysubname(j
, memSidePorts
[j
]->getPeer().name());
581 template <typename SrcType
, typename DstType
>
583 BaseXBar::Layer
<SrcType
, DstType
>::drain()
585 //We should check that we're not "doing" anything, and that noone is
586 //waiting. We might be idle but have someone waiting if the device we
587 //contacted for a retry didn't actually retry.
589 DPRINTF(Drain
, "Crossbar not drained\n");
590 return DrainState::Draining
;
592 return DrainState::Drained
;
597 * Crossbar layer template instantiations. Could be removed with _impl.hh
598 * file, but since there are only two given options (RequestPort and
599 * ResponsePort) it seems a bit excessive at this point.
601 template class BaseXBar::Layer
<ResponsePort
, RequestPort
>;
602 template class BaseXBar::Layer
<RequestPort
, ResponsePort
>;