2 * Copyright (c) 2011-2015, 2018 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 * Definition of a crossbar object.
50 #include "mem/xbar.hh"
52 #include "base/logging.hh"
53 #include "base/trace.hh"
54 #include "debug/AddrRanges.hh"
55 #include "debug/Drain.hh"
56 #include "debug/XBar.hh"
58 BaseXBar::BaseXBar(const BaseXBarParams
*p
)
60 frontendLatency(p
->frontend_latency
),
61 forwardLatency(p
->forward_latency
),
62 responseLatency(p
->response_latency
),
64 gotAddrRanges(p
->port_default_connection_count
+
65 p
->port_master_connection_count
, false),
66 gotAllAddrRanges(false), defaultPortID(InvalidPortID
),
67 useDefaultRange(p
->use_default_range
)
72 for (auto m
: masterPorts
)
75 for (auto s
: slavePorts
)
80 BaseXBar::getPort(const std::string
&if_name
, PortID idx
)
82 if (if_name
== "master" && idx
< masterPorts
.size()) {
83 // the master port index translates directly to the vector position
84 return *masterPorts
[idx
];
85 } else if (if_name
== "default") {
86 return *masterPorts
[defaultPortID
];
87 } else if (if_name
== "slave" && idx
< slavePorts
.size()) {
88 // the slave port index translates directly to the vector position
89 return *slavePorts
[idx
];
91 return ClockedObject::getPort(if_name
, idx
);
96 BaseXBar::calcPacketTiming(PacketPtr pkt
, Tick header_delay
)
98 // the crossbar will be called at a time that is not necessarily
99 // coinciding with its own clock, so start by determining how long
100 // until the next clock edge (could be zero)
101 Tick offset
= clockEdge() - curTick();
103 // the header delay depends on the path through the crossbar, and
104 // we therefore rely on the caller to provide the actual
106 pkt
->headerDelay
+= offset
+ header_delay
;
108 // note that we add the header delay to the existing value, and
109 // align it to the crossbar clock
111 // do a quick sanity check to ensure the timings are not being
112 // ignored, note that this specific value may cause problems for
113 // slower interconnects
114 panic_if(pkt
->headerDelay
> SimClock::Int::us
,
115 "Encountered header delay exceeding 1 us\n");
117 if (pkt
->hasData()) {
118 // the payloadDelay takes into account the relative time to
119 // deliver the payload of the packet, after the header delay,
120 // we take the maximum since the payload delay could already
121 // be longer than what this parcitular crossbar enforces.
122 pkt
->payloadDelay
= std::max
<Tick
>(pkt
->payloadDelay
,
123 divCeil(pkt
->getSize(), width
) *
127 // the payload delay is not paying for the clock offset as that is
128 // already done using the header delay, and the payload delay is
129 // also used to determine how long the crossbar layer is busy and
130 // thus regulates throughput
133 template <typename SrcType
, typename DstType
>
134 BaseXBar::Layer
<SrcType
, DstType
>::Layer(DstType
& _port
, BaseXBar
& _xbar
,
135 const std::string
& _name
) :
136 port(_port
), xbar(_xbar
), _name(_name
), state(IDLE
),
137 waitingForPeer(NULL
), releaseEvent([this]{ releaseLayer(); }, name())
141 template <typename SrcType
, typename DstType
>
142 void BaseXBar::Layer
<SrcType
, DstType
>::occupyLayer(Tick until
)
144 // ensure the state is busy at this point, as the layer should
145 // transition from idle as soon as it has decided to forward the
146 // packet to prevent any follow-on calls to sendTiming seeing an
148 assert(state
== BUSY
);
150 // until should never be 0 as express snoops never occupy the layer
152 xbar
.schedule(releaseEvent
, until
);
154 // account for the occupied ticks
155 occupancy
+= until
- curTick();
157 DPRINTF(BaseXBar
, "The crossbar layer is now busy from tick %d to %d\n",
161 template <typename SrcType
, typename DstType
>
163 BaseXBar::Layer
<SrcType
, DstType
>::tryTiming(SrcType
* src_port
)
165 // if we are in the retry state, we will not see anything but the
166 // retrying port (or in the case of the snoop ports the snoop
167 // response port that mirrors the actual slave port) as we leave
168 // this state again in zero time if the peer does not immediately
169 // call the layer when receiving the retry
171 // first we see if the layer is busy, next we check if the
172 // destination port is already engaged in a transaction waiting
173 // for a retry from the peer
174 if (state
== BUSY
|| waitingForPeer
!= NULL
) {
175 // the port should not be waiting already
176 assert(std::find(waitingForLayer
.begin(), waitingForLayer
.end(),
177 src_port
) == waitingForLayer
.end());
179 // put the port at the end of the retry list waiting for the
180 // layer to be freed up (and in the case of a busy peer, for
181 // that transaction to go through, and then the layer to free
183 waitingForLayer
.push_back(src_port
);
192 template <typename SrcType
, typename DstType
>
194 BaseXBar::Layer
<SrcType
, DstType
>::succeededTiming(Tick busy_time
)
196 // we should have gone from idle or retry to busy in the tryTiming
198 assert(state
== BUSY
);
200 // occupy the layer accordingly
201 occupyLayer(busy_time
);
204 template <typename SrcType
, typename DstType
>
206 BaseXBar::Layer
<SrcType
, DstType
>::failedTiming(SrcType
* src_port
,
209 // ensure no one got in between and tried to send something to
211 assert(waitingForPeer
== NULL
);
213 // if the source port is the current retrying one or not, we have
214 // failed in forwarding and should track that we are now waiting
215 // for the peer to send a retry
216 waitingForPeer
= src_port
;
218 // we should have gone from idle or retry to busy in the tryTiming
220 assert(state
== BUSY
);
222 // occupy the bus accordingly
223 occupyLayer(busy_time
);
226 template <typename SrcType
, typename DstType
>
228 BaseXBar::Layer
<SrcType
, DstType
>::releaseLayer()
230 // releasing the bus means we should now be idle
231 assert(state
== BUSY
);
232 assert(!releaseEvent
.scheduled());
237 // bus layer is now idle, so if someone is waiting we can retry
238 if (!waitingForLayer
.empty()) {
239 // there is no point in sending a retry if someone is still
240 // waiting for the peer
241 if (waitingForPeer
== NULL
)
243 } else if (waitingForPeer
== NULL
&& drainState() == DrainState::Draining
) {
244 DPRINTF(Drain
, "Crossbar done draining, signaling drain manager\n");
245 //If we weren't able to drain before, do it now.
250 template <typename SrcType
, typename DstType
>
252 BaseXBar::Layer
<SrcType
, DstType
>::retryWaiting()
254 // this should never be called with no one waiting
255 assert(!waitingForLayer
.empty());
257 // we always go to retrying from idle
258 assert(state
== IDLE
);
263 // set the retrying port to the front of the retry list and pop it
265 SrcType
* retryingPort
= waitingForLayer
.front();
266 waitingForLayer
.pop_front();
268 // tell the port to retry, which in some cases ends up calling the
270 sendRetry(retryingPort
);
272 // If the layer is still in the retry state, sendTiming wasn't
273 // called in zero time (e.g. the cache does this when a writeback
275 if (state
== RETRY
) {
276 // update the state to busy and reset the retrying port, we
277 // have done our bit and sent the retry
280 // occupy the crossbar layer until the next clock edge
281 occupyLayer(xbar
.clockEdge());
285 template <typename SrcType
, typename DstType
>
287 BaseXBar::Layer
<SrcType
, DstType
>::recvRetry()
289 // we should never get a retry without having failed to forward
290 // something to this port
291 assert(waitingForPeer
!= NULL
);
293 // add the port where the failed packet originated to the front of
294 // the waiting ports for the layer, this allows us to call retry
295 // on the port immediately if the crossbar layer is idle
296 waitingForLayer
.push_front(waitingForPeer
);
298 // we are no longer waiting for the peer
299 waitingForPeer
= NULL
;
301 // if the layer is idle, retry this port straight away, if we
302 // are busy, then simply let the port wait for its turn
306 assert(state
== BUSY
);
311 BaseXBar::findPort(AddrRange addr_range
)
313 // we should never see any address lookups before we've got the
314 // ranges of all connected slave modules
315 assert(gotAllAddrRanges
);
317 // Check the address map interval tree
318 auto i
= portMap
.contains(addr_range
);
319 if (i
!= portMap
.end()) {
323 // Check if this matches the default range
324 if (useDefaultRange
) {
325 if (addr_range
.isSubset(defaultRange
)) {
326 DPRINTF(AddrRanges
, " found addr %s on default\n",
327 addr_range
.to_string());
328 return defaultPortID
;
330 } else if (defaultPortID
!= InvalidPortID
) {
331 DPRINTF(AddrRanges
, "Unable to find destination for %s, "
332 "will use default port\n", addr_range
.to_string());
333 return defaultPortID
;
336 // we should use the range for the default port and it did not
337 // match, or the default port is not set
338 fatal("Unable to find destination for %s on %s\n", addr_range
.to_string(),
342 /** Function called by the port when the crossbar is receiving a range change.*/
344 BaseXBar::recvRangeChange(PortID master_port_id
)
346 DPRINTF(AddrRanges
, "Received range change from slave port %s\n",
347 masterPorts
[master_port_id
]->getSlavePort().name());
349 // remember that we got a range from this master port and thus the
350 // connected slave module
351 gotAddrRanges
[master_port_id
] = true;
353 // update the global flag
354 if (!gotAllAddrRanges
) {
355 // take a logical AND of all the ports and see if we got
356 // ranges from everyone
357 gotAllAddrRanges
= true;
358 std::vector
<bool>::const_iterator r
= gotAddrRanges
.begin();
359 while (gotAllAddrRanges
&& r
!= gotAddrRanges
.end()) {
360 gotAllAddrRanges
&= *r
++;
362 if (gotAllAddrRanges
)
363 DPRINTF(AddrRanges
, "Got address ranges from all slaves\n");
366 // note that we could get the range from the default port at any
367 // point in time, and we cannot assume that the default range is
368 // set before the other ones are, so we do additional checks once
369 // all ranges are provided
370 if (master_port_id
== defaultPortID
) {
371 // only update if we are indeed checking ranges for the
372 // default port since the port might not have a valid range
374 if (useDefaultRange
) {
375 AddrRangeList ranges
= masterPorts
[master_port_id
]->getAddrRanges();
377 if (ranges
.size() != 1)
378 fatal("Crossbar %s may only have a single default range",
381 defaultRange
= ranges
.front();
384 // the ports are allowed to update their address ranges
385 // dynamically, so remove any existing entries
386 if (gotAddrRanges
[master_port_id
]) {
387 for (auto p
= portMap
.begin(); p
!= portMap
.end(); ) {
388 if (p
->second
== master_port_id
)
389 // erasing invalidates the iterator, so advance it
390 // before the deletion takes place
397 AddrRangeList ranges
= masterPorts
[master_port_id
]->getAddrRanges();
399 for (const auto& r
: ranges
) {
400 DPRINTF(AddrRanges
, "Adding range %s for id %d\n",
401 r
.to_string(), master_port_id
);
402 if (portMap
.insert(r
, master_port_id
) == portMap
.end()) {
403 PortID conflict_id
= portMap
.intersects(r
)->second
;
404 fatal("%s has two ports responding within range "
408 masterPorts
[master_port_id
]->getSlavePort().name(),
409 masterPorts
[conflict_id
]->getSlavePort().name());
414 // if we have received ranges from all our neighbouring slave
415 // modules, go ahead and tell our connected master modules in
416 // turn, this effectively assumes a tree structure of the system
417 if (gotAllAddrRanges
) {
418 DPRINTF(AddrRanges
, "Aggregating address ranges\n");
421 // start out with the default range
422 if (useDefaultRange
) {
423 if (!gotAddrRanges
[defaultPortID
])
424 fatal("Crossbar %s uses default range, but none provided",
427 xbarRanges
.push_back(defaultRange
);
428 DPRINTF(AddrRanges
, "-- Adding default %s\n",
429 defaultRange
.to_string());
432 // merge all interleaved ranges and add any range that is not
433 // a subset of the default range
434 std::vector
<AddrRange
> intlv_ranges
;
435 for (const auto& r
: portMap
) {
436 // if the range is interleaved then save it for now
437 if (r
.first
.interleaved()) {
438 // if we already got interleaved ranges that are not
439 // part of the same range, then first do a merge
440 // before we add the new one
441 if (!intlv_ranges
.empty() &&
442 !intlv_ranges
.back().mergesWith(r
.first
)) {
443 DPRINTF(AddrRanges
, "-- Merging range from %d ranges\n",
444 intlv_ranges
.size());
445 AddrRange
merged_range(intlv_ranges
);
446 // next decide if we keep the merged range or not
447 if (!(useDefaultRange
&&
448 merged_range
.isSubset(defaultRange
))) {
449 xbarRanges
.push_back(merged_range
);
450 DPRINTF(AddrRanges
, "-- Adding merged range %s\n",
451 merged_range
.to_string());
453 intlv_ranges
.clear();
455 intlv_ranges
.push_back(r
.first
);
457 // keep the current range if not a subset of the default
458 if (!(useDefaultRange
&&
459 r
.first
.isSubset(defaultRange
))) {
460 xbarRanges
.push_back(r
.first
);
461 DPRINTF(AddrRanges
, "-- Adding range %s\n",
462 r
.first
.to_string());
467 // if there is still interleaved ranges waiting to be merged,
468 // go ahead and do it
469 if (!intlv_ranges
.empty()) {
470 DPRINTF(AddrRanges
, "-- Merging range from %d ranges\n",
471 intlv_ranges
.size());
472 AddrRange
merged_range(intlv_ranges
);
473 if (!(useDefaultRange
&& merged_range
.isSubset(defaultRange
))) {
474 xbarRanges
.push_back(merged_range
);
475 DPRINTF(AddrRanges
, "-- Adding merged range %s\n",
476 merged_range
.to_string());
480 // also check that no range partially intersects with the
481 // default range, this has to be done after all ranges are set
482 // as there are no guarantees for when the default range is
483 // update with respect to the other ones
484 if (useDefaultRange
) {
485 for (const auto& r
: xbarRanges
) {
486 // see if the new range is partially
487 // overlapping the default range
488 if (r
.intersects(defaultRange
) &&
489 !r
.isSubset(defaultRange
))
490 fatal("Range %s intersects the " \
491 "default range of %s but is not a " \
492 "subset\n", r
.to_string(), name());
496 // tell all our neighbouring master ports that our address
497 // ranges have changed
498 for (const auto& s
: slavePorts
)
499 s
->sendRangeChange();
504 BaseXBar::getAddrRanges() const
506 // we should never be asked without first having sent a range
507 // change, and the latter is only done once we have all the ranges
508 // of the connected devices
509 assert(gotAllAddrRanges
);
511 // at the moment, this never happens, as there are no cycles in
512 // the range queries and no devices on the master side of a crossbar
513 // (CPU, cache, bridge etc) actually care about the ranges of the
514 // ports they are connected to
516 DPRINTF(AddrRanges
, "Received address range request\n");
524 ClockedObject::regStats();
526 using namespace Stats
;
529 .init(MemCmd::NUM_MEM_CMDS
)
530 .name(name() + ".trans_dist")
531 .desc("Transaction distribution")
534 // get the string representation of the commands
535 for (int i
= 0; i
< MemCmd::NUM_MEM_CMDS
; i
++) {
537 const std::string
&cstr
= cmd
.toString();
538 transDist
.subname(i
, cstr
);
542 .init(slavePorts
.size(), masterPorts
.size())
543 .name(name() + ".pkt_count")
544 .desc("Packet count per connected master and slave (bytes)")
545 .flags(total
| nozero
| nonan
);
548 .init(slavePorts
.size(), masterPorts
.size())
549 .name(name() + ".pkt_size")
550 .desc("Cumulative packet size per connected master and slave (bytes)")
551 .flags(total
| nozero
| nonan
);
553 // both the packet count and total size are two-dimensional
554 // vectors, indexed by slave port id and master port id, thus the
555 // neighbouring master and slave, they do not differentiate what
556 // came from the master and was forwarded to the slave (requests
557 // and snoop responses) and what came from the slave and was
558 // forwarded to the master (responses and snoop requests)
559 for (int i
= 0; i
< slavePorts
.size(); i
++) {
560 pktCount
.subname(i
, slavePorts
[i
]->getMasterPort().name());
561 pktSize
.subname(i
, slavePorts
[i
]->getMasterPort().name());
562 for (int j
= 0; j
< masterPorts
.size(); j
++) {
563 pktCount
.ysubname(j
, masterPorts
[j
]->getSlavePort().name());
564 pktSize
.ysubname(j
, masterPorts
[j
]->getSlavePort().name());
569 template <typename SrcType
, typename DstType
>
571 BaseXBar::Layer
<SrcType
, DstType
>::drain()
573 //We should check that we're not "doing" anything, and that noone is
574 //waiting. We might be idle but have someone waiting if the device we
575 //contacted for a retry didn't actually retry.
577 DPRINTF(Drain
, "Crossbar not drained\n");
578 return DrainState::Draining
;
580 return DrainState::Drained
;
584 template <typename SrcType
, typename DstType
>
586 BaseXBar::Layer
<SrcType
, DstType
>::regStats()
588 using namespace Stats
;
591 .name(name() + ".occupancy")
592 .desc("Layer occupancy (ticks)")
596 .name(name() + ".utilization")
597 .desc("Layer utilization (%)")
601 utilization
= 100 * occupancy
/ simTicks
;
605 * Crossbar layer template instantiations. Could be removed with _impl.hh
606 * file, but since there are only two given options (MasterPort and
607 * SlavePort) it seems a bit excessive at this point.
609 template class BaseXBar::Layer
<SlavePort
, MasterPort
>;
610 template class BaseXBar::Layer
<MasterPort
, SlavePort
>;