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 headerCycles(p
->header_cycles
), width(p
->width
),
60 gotAddrRanges(p
->port_default_connection_count
+
61 p
->port_master_connection_count
, false),
62 gotAllAddrRanges(false), defaultPortID(InvalidPortID
),
63 useDefaultRange(p
->use_default_range
)
68 for (auto m
: masterPorts
)
71 for (auto s
: slavePorts
)
81 BaseXBar::getMasterPort(const std::string
&if_name
, PortID idx
)
83 if (if_name
== "master" && idx
< masterPorts
.size()) {
84 // the master port index translates directly to the vector position
85 return *masterPorts
[idx
];
86 } else if (if_name
== "default") {
87 return *masterPorts
[defaultPortID
];
89 return MemObject::getMasterPort(if_name
, idx
);
94 BaseXBar::getSlavePort(const std::string
&if_name
, PortID idx
)
96 if (if_name
== "slave" && idx
< slavePorts
.size()) {
97 // the slave port index translates directly to the vector position
98 return *slavePorts
[idx
];
100 return MemObject::getSlavePort(if_name
, idx
);
105 BaseXBar::calcPacketTiming(PacketPtr pkt
)
107 // the crossbar will be called at a time that is not necessarily
108 // coinciding with its own clock, so start by determining how long
109 // until the next clock edge (could be zero)
110 Tick offset
= clockEdge() - curTick();
112 // Determine how many cycles are needed to send the data
113 // If the packet has no data we take into account just the cycle to send
115 unsigned dataCycles
= pkt
->hasData() ? divCeil(pkt
->getSize(), width
) : 0;
117 // before setting the bus delay fields of the packet, ensure that
118 // the delay from any previous crossbar has been accounted for
119 if (pkt
->headerDelay
!= 0 || pkt
->payloadDelay
!= 0)
120 panic("Packet %s already has delay (%d, %d) that should be "
121 "accounted for.\n", pkt
->cmdString(), pkt
->headerDelay
,
124 // The headerDelay takes into account the relative time to deliver the
125 // header of the packet. It will be charged of the additional delay of
126 // the xbar if the packet goes through it.
127 pkt
->headerDelay
= (headerCycles
+ 1) * clockPeriod() + offset
;
129 // The payloadDelay takes into account the relative time to deliver the
130 // payload of the packet. If the packet has no data its value is just one
131 // tick (due to header) plus the offset value.
132 pkt
->payloadDelay
= (headerCycles
+ dataCycles
) * clockPeriod() + offset
;
135 template <typename SrcType
, typename DstType
>
136 BaseXBar::Layer
<SrcType
,DstType
>::Layer(DstType
& _port
, BaseXBar
& _xbar
,
137 const std::string
& _name
) :
138 port(_port
), xbar(_xbar
), _name(_name
), state(IDLE
), drainManager(NULL
),
139 waitingForPeer(NULL
), releaseEvent(this)
143 template <typename SrcType
, typename DstType
>
144 void BaseXBar::Layer
<SrcType
,DstType
>::occupyLayer(Tick until
)
146 // ensure the state is busy at this point, as the layer should
147 // transition from idle as soon as it has decided to forward the
148 // packet to prevent any follow-on calls to sendTiming seeing an
150 assert(state
== BUSY
);
152 // until should never be 0 as express snoops never occupy the layer
154 xbar
.schedule(releaseEvent
, until
);
156 // account for the occupied ticks
157 occupancy
+= until
- curTick();
159 DPRINTF(BaseXBar
, "The crossbar layer is now busy from tick %d to %d\n",
163 template <typename SrcType
, typename DstType
>
165 BaseXBar::Layer
<SrcType
,DstType
>::tryTiming(SrcType
* src_port
)
167 // if we are in the retry state, we will not see anything but the
168 // retrying port (or in the case of the snoop ports the snoop
169 // response port that mirrors the actual slave port) as we leave
170 // this state again in zero time if the peer does not immediately
171 // call the layer when receiving the retry
173 // first we see if the layer is busy, next we check if the
174 // destination port is already engaged in a transaction waiting
175 // for a retry from the peer
176 if (state
== BUSY
|| waitingForPeer
!= NULL
) {
177 // the port should not be waiting already
178 assert(std::find(waitingForLayer
.begin(), waitingForLayer
.end(),
179 src_port
) == waitingForLayer
.end());
181 // put the port at the end of the retry list waiting for the
182 // layer to be freed up (and in the case of a busy peer, for
183 // that transaction to go through, and then the layer to free
185 waitingForLayer
.push_back(src_port
);
194 template <typename SrcType
, typename DstType
>
196 BaseXBar::Layer
<SrcType
,DstType
>::succeededTiming(Tick busy_time
)
198 // we should have gone from idle or retry to busy in the tryTiming
200 assert(state
== BUSY
);
202 // occupy the layer accordingly
203 occupyLayer(busy_time
);
206 template <typename SrcType
, typename DstType
>
208 BaseXBar::Layer
<SrcType
,DstType
>::failedTiming(SrcType
* src_port
,
211 // ensure no one got in between and tried to send something to
213 assert(waitingForPeer
== NULL
);
215 // if the source port is the current retrying one or not, we have
216 // failed in forwarding and should track that we are now waiting
217 // for the peer to send a retry
218 waitingForPeer
= src_port
;
220 // we should have gone from idle or retry to busy in the tryTiming
222 assert(state
== BUSY
);
224 // occupy the bus accordingly
225 occupyLayer(busy_time
);
228 template <typename SrcType
, typename DstType
>
230 BaseXBar::Layer
<SrcType
,DstType
>::releaseLayer()
232 // releasing the bus means we should now be idle
233 assert(state
== BUSY
);
234 assert(!releaseEvent
.scheduled());
239 // bus layer is now idle, so if someone is waiting we can retry
240 if (!waitingForLayer
.empty()) {
241 // there is no point in sending a retry if someone is still
242 // waiting for the peer
243 if (waitingForPeer
== NULL
)
245 } else if (waitingForPeer
== NULL
&& drainManager
) {
246 DPRINTF(Drain
, "Crossbar done draining, signaling drain manager\n");
247 //If we weren't able to drain before, do it now.
248 drainManager
->signalDrainDone();
249 // Clear the drain event once we're done with it.
254 template <typename SrcType
, typename DstType
>
256 BaseXBar::Layer
<SrcType
,DstType
>::retryWaiting()
258 // this should never be called with no one waiting
259 assert(!waitingForLayer
.empty());
261 // we always go to retrying from idle
262 assert(state
== IDLE
);
267 // set the retrying port to the front of the retry list and pop it
269 SrcType
* retryingPort
= waitingForLayer
.front();
270 waitingForLayer
.pop_front();
272 // tell the port to retry, which in some cases ends up calling the
274 sendRetry(retryingPort
);
276 // If the layer is still in the retry state, sendTiming wasn't
277 // called in zero time (e.g. the cache does this), burn a cycle
278 if (state
== RETRY
) {
279 // update the state to busy and reset the retrying port, we
280 // have done our bit and sent the retry
283 // occupy the crossbar layer until the next cycle ends
284 occupyLayer(xbar
.clockEdge(Cycles(1)));
288 template <typename SrcType
, typename DstType
>
290 BaseXBar::Layer
<SrcType
,DstType
>::recvRetry()
292 // we should never get a retry without having failed to forward
293 // something to this port
294 assert(waitingForPeer
!= NULL
);
296 // add the port where the failed packet originated to the front of
297 // the waiting ports for the layer, this allows us to call retry
298 // on the port immediately if the crossbar layer is idle
299 waitingForLayer
.push_front(waitingForPeer
);
301 // we are no longer waiting for the peer
302 waitingForPeer
= NULL
;
304 // if the layer is idle, retry this port straight away, if we
305 // are busy, then simply let the port wait for its turn
309 assert(state
== BUSY
);
314 BaseXBar::findPort(Addr addr
)
316 // we should never see any address lookups before we've got the
317 // ranges of all connected slave modules
318 assert(gotAllAddrRanges
);
321 PortID dest_id
= checkPortCache(addr
);
322 if (dest_id
!= InvalidPortID
)
325 // Check the address map interval tree
326 auto i
= portMap
.find(addr
);
327 if (i
!= portMap
.end()) {
329 updatePortCache(dest_id
, i
->first
);
333 // Check if this matches the default range
334 if (useDefaultRange
) {
335 if (defaultRange
.contains(addr
)) {
336 DPRINTF(AddrRanges
, " found addr %#llx on default\n",
338 return defaultPortID
;
340 } else if (defaultPortID
!= InvalidPortID
) {
341 DPRINTF(AddrRanges
, "Unable to find destination for addr %#llx, "
342 "will use default port\n", addr
);
343 return defaultPortID
;
346 // we should use the range for the default port and it did not
347 // match, or the default port is not set
348 fatal("Unable to find destination for addr %#llx on %s\n", addr
,
352 /** Function called by the port when the crossbar is receiving a range change.*/
354 BaseXBar::recvRangeChange(PortID master_port_id
)
356 DPRINTF(AddrRanges
, "Received range change from slave port %s\n",
357 masterPorts
[master_port_id
]->getSlavePort().name());
359 // remember that we got a range from this master port and thus the
360 // connected slave module
361 gotAddrRanges
[master_port_id
] = true;
363 // update the global flag
364 if (!gotAllAddrRanges
) {
365 // take a logical AND of all the ports and see if we got
366 // ranges from everyone
367 gotAllAddrRanges
= true;
368 std::vector
<bool>::const_iterator r
= gotAddrRanges
.begin();
369 while (gotAllAddrRanges
&& r
!= gotAddrRanges
.end()) {
370 gotAllAddrRanges
&= *r
++;
372 if (gotAllAddrRanges
)
373 DPRINTF(AddrRanges
, "Got address ranges from all slaves\n");
376 // note that we could get the range from the default port at any
377 // point in time, and we cannot assume that the default range is
378 // set before the other ones are, so we do additional checks once
379 // all ranges are provided
380 if (master_port_id
== defaultPortID
) {
381 // only update if we are indeed checking ranges for the
382 // default port since the port might not have a valid range
384 if (useDefaultRange
) {
385 AddrRangeList ranges
= masterPorts
[master_port_id
]->getAddrRanges();
387 if (ranges
.size() != 1)
388 fatal("Crossbar %s may only have a single default range",
391 defaultRange
= ranges
.front();
394 // the ports are allowed to update their address ranges
395 // dynamically, so remove any existing entries
396 if (gotAddrRanges
[master_port_id
]) {
397 for (auto p
= portMap
.begin(); p
!= portMap
.end(); ) {
398 if (p
->second
== master_port_id
)
399 // erasing invalidates the iterator, so advance it
400 // before the deletion takes place
407 AddrRangeList ranges
= masterPorts
[master_port_id
]->getAddrRanges();
409 for (const auto& r
: ranges
) {
410 DPRINTF(AddrRanges
, "Adding range %s for id %d\n",
411 r
.to_string(), master_port_id
);
412 if (portMap
.insert(r
, master_port_id
) == portMap
.end()) {
413 PortID conflict_id
= portMap
.find(r
)->second
;
414 fatal("%s has two ports responding within range %s:\n\t%s\n\t%s\n",
417 masterPorts
[master_port_id
]->getSlavePort().name(),
418 masterPorts
[conflict_id
]->getSlavePort().name());
423 // if we have received ranges from all our neighbouring slave
424 // modules, go ahead and tell our connected master modules in
425 // turn, this effectively assumes a tree structure of the system
426 if (gotAllAddrRanges
) {
427 DPRINTF(AddrRanges
, "Aggregating address ranges\n");
430 // start out with the default range
431 if (useDefaultRange
) {
432 if (!gotAddrRanges
[defaultPortID
])
433 fatal("Crossbar %s uses default range, but none provided",
436 xbarRanges
.push_back(defaultRange
);
437 DPRINTF(AddrRanges
, "-- Adding default %s\n",
438 defaultRange
.to_string());
441 // merge all interleaved ranges and add any range that is not
442 // a subset of the default range
443 std::vector
<AddrRange
> intlv_ranges
;
444 for (const auto& r
: portMap
) {
445 // if the range is interleaved then save it for now
446 if (r
.first
.interleaved()) {
447 // if we already got interleaved ranges that are not
448 // part of the same range, then first do a merge
449 // before we add the new one
450 if (!intlv_ranges
.empty() &&
451 !intlv_ranges
.back().mergesWith(r
.first
)) {
452 DPRINTF(AddrRanges
, "-- Merging range from %d ranges\n",
453 intlv_ranges
.size());
454 AddrRange
merged_range(intlv_ranges
);
455 // next decide if we keep the merged range or not
456 if (!(useDefaultRange
&&
457 merged_range
.isSubset(defaultRange
))) {
458 xbarRanges
.push_back(merged_range
);
459 DPRINTF(AddrRanges
, "-- Adding merged range %s\n",
460 merged_range
.to_string());
462 intlv_ranges
.clear();
464 intlv_ranges
.push_back(r
.first
);
466 // keep the current range if not a subset of the default
467 if (!(useDefaultRange
&&
468 r
.first
.isSubset(defaultRange
))) {
469 xbarRanges
.push_back(r
.first
);
470 DPRINTF(AddrRanges
, "-- Adding range %s\n",
471 r
.first
.to_string());
476 // if there is still interleaved ranges waiting to be merged,
477 // go ahead and do it
478 if (!intlv_ranges
.empty()) {
479 DPRINTF(AddrRanges
, "-- Merging range from %d ranges\n",
480 intlv_ranges
.size());
481 AddrRange
merged_range(intlv_ranges
);
482 if (!(useDefaultRange
&& merged_range
.isSubset(defaultRange
))) {
483 xbarRanges
.push_back(merged_range
);
484 DPRINTF(AddrRanges
, "-- Adding merged range %s\n",
485 merged_range
.to_string());
489 // also check that no range partially overlaps with the
490 // default range, this has to be done after all ranges are set
491 // as there are no guarantees for when the default range is
492 // update with respect to the other ones
493 if (useDefaultRange
) {
494 for (const auto& r
: xbarRanges
) {
495 // see if the new range is partially
496 // overlapping the default range
497 if (r
.intersects(defaultRange
) &&
498 !r
.isSubset(defaultRange
))
499 fatal("Range %s intersects the " \
500 "default range of %s but is not a " \
501 "subset\n", r
.to_string(), name());
505 // tell all our neighbouring master ports that our address
506 // ranges have changed
507 for (const auto& s
: slavePorts
)
508 s
->sendRangeChange();
515 BaseXBar::getAddrRanges() const
517 // we should never be asked without first having sent a range
518 // change, and the latter is only done once we have all the ranges
519 // of the connected devices
520 assert(gotAllAddrRanges
);
522 // at the moment, this never happens, as there are no cycles in
523 // the range queries and no devices on the master side of a crossbar
524 // (CPU, cache, bridge etc) actually care about the ranges of the
525 // ports they are connected to
527 DPRINTF(AddrRanges
, "Received address range request\n");
535 using namespace Stats
;
538 .init(MemCmd::NUM_MEM_CMDS
)
539 .name(name() + ".trans_dist")
540 .desc("Transaction distribution")
543 // get the string representation of the commands
544 for (int i
= 0; i
< MemCmd::NUM_MEM_CMDS
; i
++) {
546 const std::string
&cstr
= cmd
.toString();
547 transDist
.subname(i
, cstr
);
551 .init(slavePorts
.size(), masterPorts
.size())
552 .name(name() + ".pkt_count")
553 .desc("Packet count per connected master and slave (bytes)")
554 .flags(total
| nozero
| nonan
);
557 .init(slavePorts
.size(), masterPorts
.size())
558 .name(name() + ".pkt_size")
559 .desc("Cumulative packet size per connected master and slave (bytes)")
560 .flags(total
| nozero
| nonan
);
562 // both the packet count and total size are two-dimensional
563 // vectors, indexed by slave port id and master port id, thus the
564 // neighbouring master and slave, they do not differentiate what
565 // came from the master and was forwarded to the slave (requests
566 // and snoop responses) and what came from the slave and was
567 // forwarded to the master (responses and snoop requests)
568 for (int i
= 0; i
< slavePorts
.size(); i
++) {
569 pktCount
.subname(i
, slavePorts
[i
]->getMasterPort().name());
570 pktSize
.subname(i
, slavePorts
[i
]->getMasterPort().name());
571 for (int j
= 0; j
< masterPorts
.size(); j
++) {
572 pktCount
.ysubname(j
, masterPorts
[j
]->getSlavePort().name());
573 pktSize
.ysubname(j
, masterPorts
[j
]->getSlavePort().name());
578 template <typename SrcType
, typename DstType
>
580 BaseXBar::Layer
<SrcType
,DstType
>::drain(DrainManager
*dm
)
582 //We should check that we're not "doing" anything, and that noone is
583 //waiting. We might be idle but have someone waiting if the device we
584 //contacted for a retry didn't actually retry.
586 DPRINTF(Drain
, "Crossbar not drained\n");
593 template <typename SrcType
, typename DstType
>
595 BaseXBar::Layer
<SrcType
,DstType
>::regStats()
597 using namespace Stats
;
600 .name(name() + ".occupancy")
601 .desc("Layer occupancy (ticks)")
605 .name(name() + ".utilization")
606 .desc("Layer utilization (%)")
610 utilization
= 100 * occupancy
/ simTicks
;
614 * Crossbar layer template instantiations. Could be removed with _impl.hh
615 * file, but since there are only two given options (MasterPort and
616 * SlavePort) it seems a bit excessive at this point.
618 template class BaseXBar::Layer
<SlavePort
,MasterPort
>;
619 template class BaseXBar::Layer
<MasterPort
,SlavePort
>;