2 * Copyright (c) 2011-2015 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 "base/misc.hh"
51 #include "base/trace.hh"
52 #include "debug/AddrRanges.hh"
53 #include "debug/Drain.hh"
54 #include "debug/XBar.hh"
55 #include "mem/xbar.hh"
57 BaseXBar::BaseXBar(const BaseXBarParams
*p
)
59 frontendLatency(p
->frontend_latency
),
60 forwardLatency(p
->forward_latency
),
61 responseLatency(p
->response_latency
),
63 gotAddrRanges(p
->port_default_connection_count
+
64 p
->port_master_connection_count
, false),
65 gotAllAddrRanges(false), defaultPortID(InvalidPortID
),
66 useDefaultRange(p
->use_default_range
)
71 for (auto m
: masterPorts
)
74 for (auto s
: slavePorts
)
84 BaseXBar::getMasterPort(const std::string
&if_name
, PortID idx
)
86 if (if_name
== "master" && idx
< masterPorts
.size()) {
87 // the master port index translates directly to the vector position
88 return *masterPorts
[idx
];
89 } else if (if_name
== "default") {
90 return *masterPorts
[defaultPortID
];
92 return MemObject::getMasterPort(if_name
, idx
);
97 BaseXBar::getSlavePort(const std::string
&if_name
, PortID idx
)
99 if (if_name
== "slave" && idx
< slavePorts
.size()) {
100 // the slave port index translates directly to the vector position
101 return *slavePorts
[idx
];
103 return MemObject::getSlavePort(if_name
, idx
);
108 BaseXBar::calcPacketTiming(PacketPtr pkt
, Tick header_delay
)
110 // the crossbar will be called at a time that is not necessarily
111 // coinciding with its own clock, so start by determining how long
112 // until the next clock edge (could be zero)
113 Tick offset
= clockEdge() - curTick();
115 // the header delay depends on the path through the crossbar, and
116 // we therefore rely on the caller to provide the actual
118 pkt
->headerDelay
+= offset
+ header_delay
;
120 // note that we add the header delay to the existing value, and
121 // align it to the crossbar clock
123 // do a quick sanity check to ensure the timings are not being
124 // ignored, note that this specific value may cause problems for
125 // slower interconnects
126 panic_if(pkt
->headerDelay
> SimClock::Int::us
,
127 "Encountered header delay exceeding 1 us\n");
129 if (pkt
->hasData()) {
130 // the payloadDelay takes into account the relative time to
131 // deliver the payload of the packet, after the header delay,
132 // we take the maximum since the payload delay could already
133 // be longer than what this parcitular crossbar enforces.
134 pkt
->payloadDelay
= std::max
<Tick
>(pkt
->payloadDelay
,
135 divCeil(pkt
->getSize(), width
) *
139 // the payload delay is not paying for the clock offset as that is
140 // already done using the header delay, and the payload delay is
141 // also used to determine how long the crossbar layer is busy and
142 // thus regulates throughput
145 template <typename SrcType
, typename DstType
>
146 BaseXBar::Layer
<SrcType
,DstType
>::Layer(DstType
& _port
, BaseXBar
& _xbar
,
147 const std::string
& _name
) :
148 port(_port
), xbar(_xbar
), _name(_name
), state(IDLE
),
149 waitingForPeer(NULL
), releaseEvent(this)
153 template <typename SrcType
, typename DstType
>
154 void BaseXBar::Layer
<SrcType
,DstType
>::occupyLayer(Tick until
)
156 // ensure the state is busy at this point, as the layer should
157 // transition from idle as soon as it has decided to forward the
158 // packet to prevent any follow-on calls to sendTiming seeing an
160 assert(state
== BUSY
);
162 // until should never be 0 as express snoops never occupy the layer
164 xbar
.schedule(releaseEvent
, until
);
166 // account for the occupied ticks
167 occupancy
+= until
- curTick();
169 DPRINTF(BaseXBar
, "The crossbar layer is now busy from tick %d to %d\n",
173 template <typename SrcType
, typename DstType
>
175 BaseXBar::Layer
<SrcType
,DstType
>::tryTiming(SrcType
* src_port
)
177 // if we are in the retry state, we will not see anything but the
178 // retrying port (or in the case of the snoop ports the snoop
179 // response port that mirrors the actual slave port) as we leave
180 // this state again in zero time if the peer does not immediately
181 // call the layer when receiving the retry
183 // first we see if the layer is busy, next we check if the
184 // destination port is already engaged in a transaction waiting
185 // for a retry from the peer
186 if (state
== BUSY
|| waitingForPeer
!= NULL
) {
187 // the port should not be waiting already
188 assert(std::find(waitingForLayer
.begin(), waitingForLayer
.end(),
189 src_port
) == waitingForLayer
.end());
191 // put the port at the end of the retry list waiting for the
192 // layer to be freed up (and in the case of a busy peer, for
193 // that transaction to go through, and then the layer to free
195 waitingForLayer
.push_back(src_port
);
204 template <typename SrcType
, typename DstType
>
206 BaseXBar::Layer
<SrcType
,DstType
>::succeededTiming(Tick busy_time
)
208 // we should have gone from idle or retry to busy in the tryTiming
210 assert(state
== BUSY
);
212 // occupy the layer accordingly
213 occupyLayer(busy_time
);
216 template <typename SrcType
, typename DstType
>
218 BaseXBar::Layer
<SrcType
,DstType
>::failedTiming(SrcType
* src_port
,
221 // ensure no one got in between and tried to send something to
223 assert(waitingForPeer
== NULL
);
225 // if the source port is the current retrying one or not, we have
226 // failed in forwarding and should track that we are now waiting
227 // for the peer to send a retry
228 waitingForPeer
= src_port
;
230 // we should have gone from idle or retry to busy in the tryTiming
232 assert(state
== BUSY
);
234 // occupy the bus accordingly
235 occupyLayer(busy_time
);
238 template <typename SrcType
, typename DstType
>
240 BaseXBar::Layer
<SrcType
,DstType
>::releaseLayer()
242 // releasing the bus means we should now be idle
243 assert(state
== BUSY
);
244 assert(!releaseEvent
.scheduled());
249 // bus layer is now idle, so if someone is waiting we can retry
250 if (!waitingForLayer
.empty()) {
251 // there is no point in sending a retry if someone is still
252 // waiting for the peer
253 if (waitingForPeer
== NULL
)
255 } else if (waitingForPeer
== NULL
&& drainState() == DrainState::Draining
) {
256 DPRINTF(Drain
, "Crossbar done draining, signaling drain manager\n");
257 //If we weren't able to drain before, do it now.
262 template <typename SrcType
, typename DstType
>
264 BaseXBar::Layer
<SrcType
,DstType
>::retryWaiting()
266 // this should never be called with no one waiting
267 assert(!waitingForLayer
.empty());
269 // we always go to retrying from idle
270 assert(state
== IDLE
);
275 // set the retrying port to the front of the retry list and pop it
277 SrcType
* retryingPort
= waitingForLayer
.front();
278 waitingForLayer
.pop_front();
280 // tell the port to retry, which in some cases ends up calling the
282 sendRetry(retryingPort
);
284 // If the layer is still in the retry state, sendTiming wasn't
285 // called in zero time (e.g. the cache does this when a writeback
287 if (state
== RETRY
) {
288 // update the state to busy and reset the retrying port, we
289 // have done our bit and sent the retry
292 // occupy the crossbar layer until the next clock edge
293 occupyLayer(xbar
.clockEdge());
297 template <typename SrcType
, typename DstType
>
299 BaseXBar::Layer
<SrcType
,DstType
>::recvRetry()
301 // we should never get a retry without having failed to forward
302 // something to this port
303 assert(waitingForPeer
!= NULL
);
305 // add the port where the failed packet originated to the front of
306 // the waiting ports for the layer, this allows us to call retry
307 // on the port immediately if the crossbar layer is idle
308 waitingForLayer
.push_front(waitingForPeer
);
310 // we are no longer waiting for the peer
311 waitingForPeer
= NULL
;
313 // if the layer is idle, retry this port straight away, if we
314 // are busy, then simply let the port wait for its turn
318 assert(state
== BUSY
);
323 BaseXBar::findPort(Addr addr
)
325 // we should never see any address lookups before we've got the
326 // ranges of all connected slave modules
327 assert(gotAllAddrRanges
);
330 PortID dest_id
= checkPortCache(addr
);
331 if (dest_id
!= InvalidPortID
)
334 // Check the address map interval tree
335 auto i
= portMap
.find(addr
);
336 if (i
!= portMap
.end()) {
338 updatePortCache(dest_id
, i
->first
);
342 // Check if this matches the default range
343 if (useDefaultRange
) {
344 if (defaultRange
.contains(addr
)) {
345 DPRINTF(AddrRanges
, " found addr %#llx on default\n",
347 return defaultPortID
;
349 } else if (defaultPortID
!= InvalidPortID
) {
350 DPRINTF(AddrRanges
, "Unable to find destination for addr %#llx, "
351 "will use default port\n", addr
);
352 return defaultPortID
;
355 // we should use the range for the default port and it did not
356 // match, or the default port is not set
357 fatal("Unable to find destination for addr %#llx on %s\n", addr
,
361 /** Function called by the port when the crossbar is receiving a range change.*/
363 BaseXBar::recvRangeChange(PortID master_port_id
)
365 DPRINTF(AddrRanges
, "Received range change from slave port %s\n",
366 masterPorts
[master_port_id
]->getSlavePort().name());
368 // remember that we got a range from this master port and thus the
369 // connected slave module
370 gotAddrRanges
[master_port_id
] = true;
372 // update the global flag
373 if (!gotAllAddrRanges
) {
374 // take a logical AND of all the ports and see if we got
375 // ranges from everyone
376 gotAllAddrRanges
= true;
377 std::vector
<bool>::const_iterator r
= gotAddrRanges
.begin();
378 while (gotAllAddrRanges
&& r
!= gotAddrRanges
.end()) {
379 gotAllAddrRanges
&= *r
++;
381 if (gotAllAddrRanges
)
382 DPRINTF(AddrRanges
, "Got address ranges from all slaves\n");
385 // note that we could get the range from the default port at any
386 // point in time, and we cannot assume that the default range is
387 // set before the other ones are, so we do additional checks once
388 // all ranges are provided
389 if (master_port_id
== defaultPortID
) {
390 // only update if we are indeed checking ranges for the
391 // default port since the port might not have a valid range
393 if (useDefaultRange
) {
394 AddrRangeList ranges
= masterPorts
[master_port_id
]->getAddrRanges();
396 if (ranges
.size() != 1)
397 fatal("Crossbar %s may only have a single default range",
400 defaultRange
= ranges
.front();
403 // the ports are allowed to update their address ranges
404 // dynamically, so remove any existing entries
405 if (gotAddrRanges
[master_port_id
]) {
406 for (auto p
= portMap
.begin(); p
!= portMap
.end(); ) {
407 if (p
->second
== master_port_id
)
408 // erasing invalidates the iterator, so advance it
409 // before the deletion takes place
416 AddrRangeList ranges
= masterPorts
[master_port_id
]->getAddrRanges();
418 for (const auto& r
: ranges
) {
419 DPRINTF(AddrRanges
, "Adding range %s for id %d\n",
420 r
.to_string(), master_port_id
);
421 if (portMap
.insert(r
, master_port_id
) == portMap
.end()) {
422 PortID conflict_id
= portMap
.find(r
)->second
;
423 fatal("%s has two ports responding within range %s:\n\t%s\n\t%s\n",
426 masterPorts
[master_port_id
]->getSlavePort().name(),
427 masterPorts
[conflict_id
]->getSlavePort().name());
432 // if we have received ranges from all our neighbouring slave
433 // modules, go ahead and tell our connected master modules in
434 // turn, this effectively assumes a tree structure of the system
435 if (gotAllAddrRanges
) {
436 DPRINTF(AddrRanges
, "Aggregating address ranges\n");
439 // start out with the default range
440 if (useDefaultRange
) {
441 if (!gotAddrRanges
[defaultPortID
])
442 fatal("Crossbar %s uses default range, but none provided",
445 xbarRanges
.push_back(defaultRange
);
446 DPRINTF(AddrRanges
, "-- Adding default %s\n",
447 defaultRange
.to_string());
450 // merge all interleaved ranges and add any range that is not
451 // a subset of the default range
452 std::vector
<AddrRange
> intlv_ranges
;
453 for (const auto& r
: portMap
) {
454 // if the range is interleaved then save it for now
455 if (r
.first
.interleaved()) {
456 // if we already got interleaved ranges that are not
457 // part of the same range, then first do a merge
458 // before we add the new one
459 if (!intlv_ranges
.empty() &&
460 !intlv_ranges
.back().mergesWith(r
.first
)) {
461 DPRINTF(AddrRanges
, "-- Merging range from %d ranges\n",
462 intlv_ranges
.size());
463 AddrRange
merged_range(intlv_ranges
);
464 // next decide if we keep the merged range or not
465 if (!(useDefaultRange
&&
466 merged_range
.isSubset(defaultRange
))) {
467 xbarRanges
.push_back(merged_range
);
468 DPRINTF(AddrRanges
, "-- Adding merged range %s\n",
469 merged_range
.to_string());
471 intlv_ranges
.clear();
473 intlv_ranges
.push_back(r
.first
);
475 // keep the current range if not a subset of the default
476 if (!(useDefaultRange
&&
477 r
.first
.isSubset(defaultRange
))) {
478 xbarRanges
.push_back(r
.first
);
479 DPRINTF(AddrRanges
, "-- Adding range %s\n",
480 r
.first
.to_string());
485 // if there is still interleaved ranges waiting to be merged,
486 // go ahead and do it
487 if (!intlv_ranges
.empty()) {
488 DPRINTF(AddrRanges
, "-- Merging range from %d ranges\n",
489 intlv_ranges
.size());
490 AddrRange
merged_range(intlv_ranges
);
491 if (!(useDefaultRange
&& merged_range
.isSubset(defaultRange
))) {
492 xbarRanges
.push_back(merged_range
);
493 DPRINTF(AddrRanges
, "-- Adding merged range %s\n",
494 merged_range
.to_string());
498 // also check that no range partially overlaps with the
499 // default range, this has to be done after all ranges are set
500 // as there are no guarantees for when the default range is
501 // update with respect to the other ones
502 if (useDefaultRange
) {
503 for (const auto& r
: xbarRanges
) {
504 // see if the new range is partially
505 // overlapping the default range
506 if (r
.intersects(defaultRange
) &&
507 !r
.isSubset(defaultRange
))
508 fatal("Range %s intersects the " \
509 "default range of %s but is not a " \
510 "subset\n", r
.to_string(), name());
514 // tell all our neighbouring master ports that our address
515 // ranges have changed
516 for (const auto& s
: slavePorts
)
517 s
->sendRangeChange();
524 BaseXBar::getAddrRanges() const
526 // we should never be asked without first having sent a range
527 // change, and the latter is only done once we have all the ranges
528 // of the connected devices
529 assert(gotAllAddrRanges
);
531 // at the moment, this never happens, as there are no cycles in
532 // the range queries and no devices on the master side of a crossbar
533 // (CPU, cache, bridge etc) actually care about the ranges of the
534 // ports they are connected to
536 DPRINTF(AddrRanges
, "Received address range request\n");
544 using namespace Stats
;
547 .init(MemCmd::NUM_MEM_CMDS
)
548 .name(name() + ".trans_dist")
549 .desc("Transaction distribution")
552 // get the string representation of the commands
553 for (int i
= 0; i
< MemCmd::NUM_MEM_CMDS
; i
++) {
555 const std::string
&cstr
= cmd
.toString();
556 transDist
.subname(i
, cstr
);
560 .init(slavePorts
.size(), masterPorts
.size())
561 .name(name() + ".pkt_count")
562 .desc("Packet count per connected master and slave (bytes)")
563 .flags(total
| nozero
| nonan
);
566 .init(slavePorts
.size(), masterPorts
.size())
567 .name(name() + ".pkt_size")
568 .desc("Cumulative packet size per connected master and slave (bytes)")
569 .flags(total
| nozero
| nonan
);
571 // both the packet count and total size are two-dimensional
572 // vectors, indexed by slave port id and master port id, thus the
573 // neighbouring master and slave, they do not differentiate what
574 // came from the master and was forwarded to the slave (requests
575 // and snoop responses) and what came from the slave and was
576 // forwarded to the master (responses and snoop requests)
577 for (int i
= 0; i
< slavePorts
.size(); i
++) {
578 pktCount
.subname(i
, slavePorts
[i
]->getMasterPort().name());
579 pktSize
.subname(i
, slavePorts
[i
]->getMasterPort().name());
580 for (int j
= 0; j
< masterPorts
.size(); j
++) {
581 pktCount
.ysubname(j
, masterPorts
[j
]->getSlavePort().name());
582 pktSize
.ysubname(j
, masterPorts
[j
]->getSlavePort().name());
587 template <typename SrcType
, typename DstType
>
589 BaseXBar::Layer
<SrcType
,DstType
>::drain()
591 //We should check that we're not "doing" anything, and that noone is
592 //waiting. We might be idle but have someone waiting if the device we
593 //contacted for a retry didn't actually retry.
595 DPRINTF(Drain
, "Crossbar not drained\n");
596 return DrainState::Draining
;
598 return DrainState::Drained
;
602 template <typename SrcType
, typename DstType
>
604 BaseXBar::Layer
<SrcType
,DstType
>::regStats()
606 using namespace Stats
;
609 .name(name() + ".occupancy")
610 .desc("Layer occupancy (ticks)")
614 .name(name() + ".utilization")
615 .desc("Layer utilization (%)")
619 utilization
= 100 * occupancy
/ simTicks
;
623 * Crossbar layer template instantiations. Could be removed with _impl.hh
624 * file, but since there are only two given options (MasterPort and
625 * SlavePort) it seems a bit excessive at this point.
627 template class BaseXBar::Layer
<SlavePort
,MasterPort
>;
628 template class BaseXBar::Layer
<MasterPort
,SlavePort
>;