xbar.cc (11522:348411ec525a) xbar.cc (11793:ef606668d247)
1/*
2 * Copyright (c) 2011-2015 ARM Limited
3 * All rights reserved
4 *
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.
13 *
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
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.
27 *
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.
39 *
40 * Authors: Ali Saidi
41 * Andreas Hansson
42 * William Wang
43 */
44
45/**
46 * @file
47 * Definition of a crossbar object.
48 */
49
1/*
2 * Copyright (c) 2011-2015 ARM Limited
3 * All rights reserved
4 *
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.
13 *
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
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.
27 *
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.
39 *
40 * Authors: Ali Saidi
41 * Andreas Hansson
42 * William Wang
43 */
44
45/**
46 * @file
47 * Definition of a crossbar object.
48 */
49
50#include "mem/xbar.hh"
51
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"
52#include "base/misc.hh"
53#include "base/trace.hh"
54#include "debug/AddrRanges.hh"
55#include "debug/Drain.hh"
56#include "debug/XBar.hh"
55#include "mem/xbar.hh"
56
57BaseXBar::BaseXBar(const BaseXBarParams *p)
58 : MemObject(p),
59 frontendLatency(p->frontend_latency),
60 forwardLatency(p->forward_latency),
61 responseLatency(p->response_latency),
62 width(p->width),
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)
67{}
68
69BaseXBar::~BaseXBar()
70{
71 for (auto m: masterPorts)
72 delete m;
73
74 for (auto s: slavePorts)
75 delete s;
76}
77
78void
79BaseXBar::init()
80{
81}
82
83BaseMasterPort &
84BaseXBar::getMasterPort(const std::string &if_name, PortID idx)
85{
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];
91 } else {
92 return MemObject::getMasterPort(if_name, idx);
93 }
94}
95
96BaseSlavePort &
97BaseXBar::getSlavePort(const std::string &if_name, PortID idx)
98{
99 if (if_name == "slave" && idx < slavePorts.size()) {
100 // the slave port index translates directly to the vector position
101 return *slavePorts[idx];
102 } else {
103 return MemObject::getSlavePort(if_name, idx);
104 }
105}
106
107void
108BaseXBar::calcPacketTiming(PacketPtr pkt, Tick header_delay)
109{
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();
114
115 // the header delay depends on the path through the crossbar, and
116 // we therefore rely on the caller to provide the actual
117 // value
118 pkt->headerDelay += offset + header_delay;
119
120 // note that we add the header delay to the existing value, and
121 // align it to the crossbar clock
122
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");
128
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) *
136 clockPeriod());
137 }
138
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
143}
144
145template <typename SrcType, typename DstType>
146BaseXBar::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)
150{
151}
152
153template <typename SrcType, typename DstType>
154void BaseXBar::Layer<SrcType,DstType>::occupyLayer(Tick until)
155{
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
159 // unoccupied layer
160 assert(state == BUSY);
161
162 // until should never be 0 as express snoops never occupy the layer
163 assert(until != 0);
164 xbar.schedule(releaseEvent, until);
165
166 // account for the occupied ticks
167 occupancy += until - curTick();
168
169 DPRINTF(BaseXBar, "The crossbar layer is now busy from tick %d to %d\n",
170 curTick(), until);
171}
172
173template <typename SrcType, typename DstType>
174bool
175BaseXBar::Layer<SrcType,DstType>::tryTiming(SrcType* src_port)
176{
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
182
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());
190
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
194 // up)
195 waitingForLayer.push_back(src_port);
196 return false;
197 }
198
199 state = BUSY;
200
201 return true;
202}
203
204template <typename SrcType, typename DstType>
205void
206BaseXBar::Layer<SrcType,DstType>::succeededTiming(Tick busy_time)
207{
208 // we should have gone from idle or retry to busy in the tryTiming
209 // test
210 assert(state == BUSY);
211
212 // occupy the layer accordingly
213 occupyLayer(busy_time);
214}
215
216template <typename SrcType, typename DstType>
217void
218BaseXBar::Layer<SrcType,DstType>::failedTiming(SrcType* src_port,
219 Tick busy_time)
220{
221 // ensure no one got in between and tried to send something to
222 // this port
223 assert(waitingForPeer == NULL);
224
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;
229
230 // we should have gone from idle or retry to busy in the tryTiming
231 // test
232 assert(state == BUSY);
233
234 // occupy the bus accordingly
235 occupyLayer(busy_time);
236}
237
238template <typename SrcType, typename DstType>
239void
240BaseXBar::Layer<SrcType,DstType>::releaseLayer()
241{
242 // releasing the bus means we should now be idle
243 assert(state == BUSY);
244 assert(!releaseEvent.scheduled());
245
246 // update the state
247 state = IDLE;
248
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)
254 retryWaiting();
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.
258 signalDrainDone();
259 }
260}
261
262template <typename SrcType, typename DstType>
263void
264BaseXBar::Layer<SrcType,DstType>::retryWaiting()
265{
266 // this should never be called with no one waiting
267 assert(!waitingForLayer.empty());
268
269 // we always go to retrying from idle
270 assert(state == IDLE);
271
272 // update the state
273 state = RETRY;
274
275 // set the retrying port to the front of the retry list and pop it
276 // off the list
277 SrcType* retryingPort = waitingForLayer.front();
278 waitingForLayer.pop_front();
279
280 // tell the port to retry, which in some cases ends up calling the
281 // layer again
282 sendRetry(retryingPort);
283
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
286 // is squashed)
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
290 state = BUSY;
291
292 // occupy the crossbar layer until the next clock edge
293 occupyLayer(xbar.clockEdge());
294 }
295}
296
297template <typename SrcType, typename DstType>
298void
299BaseXBar::Layer<SrcType,DstType>::recvRetry()
300{
301 // we should never get a retry without having failed to forward
302 // something to this port
303 assert(waitingForPeer != NULL);
304
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);
309
310 // we are no longer waiting for the peer
311 waitingForPeer = NULL;
312
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
315 if (state == IDLE) {
316 retryWaiting();
317 } else {
318 assert(state == BUSY);
319 }
320}
321
322PortID
323BaseXBar::findPort(Addr addr)
324{
325 // we should never see any address lookups before we've got the
326 // ranges of all connected slave modules
327 assert(gotAllAddrRanges);
328
329 // Check the cache
330 PortID dest_id = checkPortCache(addr);
331 if (dest_id != InvalidPortID)
332 return dest_id;
333
334 // Check the address map interval tree
335 auto i = portMap.find(addr);
336 if (i != portMap.end()) {
337 dest_id = i->second;
338 updatePortCache(dest_id, i->first);
339 return dest_id;
340 }
341
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",
346 addr);
347 return defaultPortID;
348 }
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;
353 }
354
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,
358 name());
359}
360
361/** Function called by the port when the crossbar is receiving a range change.*/
362void
363BaseXBar::recvRangeChange(PortID master_port_id)
364{
365 DPRINTF(AddrRanges, "Received range change from slave port %s\n",
366 masterPorts[master_port_id]->getSlavePort().name());
367
368 // remember that we got a range from this master port and thus the
369 // connected slave module
370 gotAddrRanges[master_port_id] = true;
371
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++;
380 }
381 if (gotAllAddrRanges)
382 DPRINTF(AddrRanges, "Got address ranges from all slaves\n");
383 }
384
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
392 // otherwise
393 if (useDefaultRange) {
394 AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges();
395
396 if (ranges.size() != 1)
397 fatal("Crossbar %s may only have a single default range",
398 name());
399
400 defaultRange = ranges.front();
401 }
402 } else {
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
410 portMap.erase(p++);
411 else
412 p++;
413 }
414 }
415
416 AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges();
417
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",
424 name(),
425 r.to_string(),
426 masterPorts[master_port_id]->getSlavePort().name(),
427 masterPorts[conflict_id]->getSlavePort().name());
428 }
429 }
430 }
431
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");
437 xbarRanges.clear();
438
439 // start out with the default range
440 if (useDefaultRange) {
441 if (!gotAddrRanges[defaultPortID])
442 fatal("Crossbar %s uses default range, but none provided",
443 name());
444
445 xbarRanges.push_back(defaultRange);
446 DPRINTF(AddrRanges, "-- Adding default %s\n",
447 defaultRange.to_string());
448 }
449
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());
470 }
471 intlv_ranges.clear();
472 }
473 intlv_ranges.push_back(r.first);
474 } else {
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());
481 }
482 }
483 }
484
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());
495 }
496 }
497
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());
511 }
512 }
513
514 // tell all our neighbouring master ports that our address
515 // ranges have changed
516 for (const auto& s: slavePorts)
517 s->sendRangeChange();
518 }
519
520 clearPortCache();
521}
522
523AddrRangeList
524BaseXBar::getAddrRanges() const
525{
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);
530
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
535
536 DPRINTF(AddrRanges, "Received address range request\n");
537
538 return xbarRanges;
539}
540
541void
542BaseXBar::regStats()
543{
544 ClockedObject::regStats();
545
546 using namespace Stats;
547
548 transDist
549 .init(MemCmd::NUM_MEM_CMDS)
550 .name(name() + ".trans_dist")
551 .desc("Transaction distribution")
552 .flags(nozero);
553
554 // get the string representation of the commands
555 for (int i = 0; i < MemCmd::NUM_MEM_CMDS; i++) {
556 MemCmd cmd(i);
557 const std::string &cstr = cmd.toString();
558 transDist.subname(i, cstr);
559 }
560
561 pktCount
562 .init(slavePorts.size(), masterPorts.size())
563 .name(name() + ".pkt_count")
564 .desc("Packet count per connected master and slave (bytes)")
565 .flags(total | nozero | nonan);
566
567 pktSize
568 .init(slavePorts.size(), masterPorts.size())
569 .name(name() + ".pkt_size")
570 .desc("Cumulative packet size per connected master and slave (bytes)")
571 .flags(total | nozero | nonan);
572
573 // both the packet count and total size are two-dimensional
574 // vectors, indexed by slave port id and master port id, thus the
575 // neighbouring master and slave, they do not differentiate what
576 // came from the master and was forwarded to the slave (requests
577 // and snoop responses) and what came from the slave and was
578 // forwarded to the master (responses and snoop requests)
579 for (int i = 0; i < slavePorts.size(); i++) {
580 pktCount.subname(i, slavePorts[i]->getMasterPort().name());
581 pktSize.subname(i, slavePorts[i]->getMasterPort().name());
582 for (int j = 0; j < masterPorts.size(); j++) {
583 pktCount.ysubname(j, masterPorts[j]->getSlavePort().name());
584 pktSize.ysubname(j, masterPorts[j]->getSlavePort().name());
585 }
586 }
587}
588
589template <typename SrcType, typename DstType>
590DrainState
591BaseXBar::Layer<SrcType,DstType>::drain()
592{
593 //We should check that we're not "doing" anything, and that noone is
594 //waiting. We might be idle but have someone waiting if the device we
595 //contacted for a retry didn't actually retry.
596 if (state != IDLE) {
597 DPRINTF(Drain, "Crossbar not drained\n");
598 return DrainState::Draining;
599 } else {
600 return DrainState::Drained;
601 }
602}
603
604template <typename SrcType, typename DstType>
605void
606BaseXBar::Layer<SrcType,DstType>::regStats()
607{
608 using namespace Stats;
609
610 occupancy
611 .name(name() + ".occupancy")
612 .desc("Layer occupancy (ticks)")
613 .flags(nozero);
614
615 utilization
616 .name(name() + ".utilization")
617 .desc("Layer utilization (%)")
618 .precision(1)
619 .flags(nozero);
620
621 utilization = 100 * occupancy / simTicks;
622}
623
624/**
625 * Crossbar layer template instantiations. Could be removed with _impl.hh
626 * file, but since there are only two given options (MasterPort and
627 * SlavePort) it seems a bit excessive at this point.
628 */
629template class BaseXBar::Layer<SlavePort,MasterPort>;
630template class BaseXBar::Layer<MasterPort,SlavePort>;
57
58BaseXBar::BaseXBar(const BaseXBarParams *p)
59 : MemObject(p),
60 frontendLatency(p->frontend_latency),
61 forwardLatency(p->forward_latency),
62 responseLatency(p->response_latency),
63 width(p->width),
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)
68{}
69
70BaseXBar::~BaseXBar()
71{
72 for (auto m: masterPorts)
73 delete m;
74
75 for (auto s: slavePorts)
76 delete s;
77}
78
79void
80BaseXBar::init()
81{
82}
83
84BaseMasterPort &
85BaseXBar::getMasterPort(const std::string &if_name, PortID idx)
86{
87 if (if_name == "master" && idx < masterPorts.size()) {
88 // the master port index translates directly to the vector position
89 return *masterPorts[idx];
90 } else if (if_name == "default") {
91 return *masterPorts[defaultPortID];
92 } else {
93 return MemObject::getMasterPort(if_name, idx);
94 }
95}
96
97BaseSlavePort &
98BaseXBar::getSlavePort(const std::string &if_name, PortID idx)
99{
100 if (if_name == "slave" && idx < slavePorts.size()) {
101 // the slave port index translates directly to the vector position
102 return *slavePorts[idx];
103 } else {
104 return MemObject::getSlavePort(if_name, idx);
105 }
106}
107
108void
109BaseXBar::calcPacketTiming(PacketPtr pkt, Tick header_delay)
110{
111 // the crossbar will be called at a time that is not necessarily
112 // coinciding with its own clock, so start by determining how long
113 // until the next clock edge (could be zero)
114 Tick offset = clockEdge() - curTick();
115
116 // the header delay depends on the path through the crossbar, and
117 // we therefore rely on the caller to provide the actual
118 // value
119 pkt->headerDelay += offset + header_delay;
120
121 // note that we add the header delay to the existing value, and
122 // align it to the crossbar clock
123
124 // do a quick sanity check to ensure the timings are not being
125 // ignored, note that this specific value may cause problems for
126 // slower interconnects
127 panic_if(pkt->headerDelay > SimClock::Int::us,
128 "Encountered header delay exceeding 1 us\n");
129
130 if (pkt->hasData()) {
131 // the payloadDelay takes into account the relative time to
132 // deliver the payload of the packet, after the header delay,
133 // we take the maximum since the payload delay could already
134 // be longer than what this parcitular crossbar enforces.
135 pkt->payloadDelay = std::max<Tick>(pkt->payloadDelay,
136 divCeil(pkt->getSize(), width) *
137 clockPeriod());
138 }
139
140 // the payload delay is not paying for the clock offset as that is
141 // already done using the header delay, and the payload delay is
142 // also used to determine how long the crossbar layer is busy and
143 // thus regulates throughput
144}
145
146template <typename SrcType, typename DstType>
147BaseXBar::Layer<SrcType,DstType>::Layer(DstType& _port, BaseXBar& _xbar,
148 const std::string& _name) :
149 port(_port), xbar(_xbar), _name(_name), state(IDLE),
150 waitingForPeer(NULL), releaseEvent(this)
151{
152}
153
154template <typename SrcType, typename DstType>
155void BaseXBar::Layer<SrcType,DstType>::occupyLayer(Tick until)
156{
157 // ensure the state is busy at this point, as the layer should
158 // transition from idle as soon as it has decided to forward the
159 // packet to prevent any follow-on calls to sendTiming seeing an
160 // unoccupied layer
161 assert(state == BUSY);
162
163 // until should never be 0 as express snoops never occupy the layer
164 assert(until != 0);
165 xbar.schedule(releaseEvent, until);
166
167 // account for the occupied ticks
168 occupancy += until - curTick();
169
170 DPRINTF(BaseXBar, "The crossbar layer is now busy from tick %d to %d\n",
171 curTick(), until);
172}
173
174template <typename SrcType, typename DstType>
175bool
176BaseXBar::Layer<SrcType,DstType>::tryTiming(SrcType* src_port)
177{
178 // if we are in the retry state, we will not see anything but the
179 // retrying port (or in the case of the snoop ports the snoop
180 // response port that mirrors the actual slave port) as we leave
181 // this state again in zero time if the peer does not immediately
182 // call the layer when receiving the retry
183
184 // first we see if the layer is busy, next we check if the
185 // destination port is already engaged in a transaction waiting
186 // for a retry from the peer
187 if (state == BUSY || waitingForPeer != NULL) {
188 // the port should not be waiting already
189 assert(std::find(waitingForLayer.begin(), waitingForLayer.end(),
190 src_port) == waitingForLayer.end());
191
192 // put the port at the end of the retry list waiting for the
193 // layer to be freed up (and in the case of a busy peer, for
194 // that transaction to go through, and then the layer to free
195 // up)
196 waitingForLayer.push_back(src_port);
197 return false;
198 }
199
200 state = BUSY;
201
202 return true;
203}
204
205template <typename SrcType, typename DstType>
206void
207BaseXBar::Layer<SrcType,DstType>::succeededTiming(Tick busy_time)
208{
209 // we should have gone from idle or retry to busy in the tryTiming
210 // test
211 assert(state == BUSY);
212
213 // occupy the layer accordingly
214 occupyLayer(busy_time);
215}
216
217template <typename SrcType, typename DstType>
218void
219BaseXBar::Layer<SrcType,DstType>::failedTiming(SrcType* src_port,
220 Tick busy_time)
221{
222 // ensure no one got in between and tried to send something to
223 // this port
224 assert(waitingForPeer == NULL);
225
226 // if the source port is the current retrying one or not, we have
227 // failed in forwarding and should track that we are now waiting
228 // for the peer to send a retry
229 waitingForPeer = src_port;
230
231 // we should have gone from idle or retry to busy in the tryTiming
232 // test
233 assert(state == BUSY);
234
235 // occupy the bus accordingly
236 occupyLayer(busy_time);
237}
238
239template <typename SrcType, typename DstType>
240void
241BaseXBar::Layer<SrcType,DstType>::releaseLayer()
242{
243 // releasing the bus means we should now be idle
244 assert(state == BUSY);
245 assert(!releaseEvent.scheduled());
246
247 // update the state
248 state = IDLE;
249
250 // bus layer is now idle, so if someone is waiting we can retry
251 if (!waitingForLayer.empty()) {
252 // there is no point in sending a retry if someone is still
253 // waiting for the peer
254 if (waitingForPeer == NULL)
255 retryWaiting();
256 } else if (waitingForPeer == NULL && drainState() == DrainState::Draining) {
257 DPRINTF(Drain, "Crossbar done draining, signaling drain manager\n");
258 //If we weren't able to drain before, do it now.
259 signalDrainDone();
260 }
261}
262
263template <typename SrcType, typename DstType>
264void
265BaseXBar::Layer<SrcType,DstType>::retryWaiting()
266{
267 // this should never be called with no one waiting
268 assert(!waitingForLayer.empty());
269
270 // we always go to retrying from idle
271 assert(state == IDLE);
272
273 // update the state
274 state = RETRY;
275
276 // set the retrying port to the front of the retry list and pop it
277 // off the list
278 SrcType* retryingPort = waitingForLayer.front();
279 waitingForLayer.pop_front();
280
281 // tell the port to retry, which in some cases ends up calling the
282 // layer again
283 sendRetry(retryingPort);
284
285 // If the layer is still in the retry state, sendTiming wasn't
286 // called in zero time (e.g. the cache does this when a writeback
287 // is squashed)
288 if (state == RETRY) {
289 // update the state to busy and reset the retrying port, we
290 // have done our bit and sent the retry
291 state = BUSY;
292
293 // occupy the crossbar layer until the next clock edge
294 occupyLayer(xbar.clockEdge());
295 }
296}
297
298template <typename SrcType, typename DstType>
299void
300BaseXBar::Layer<SrcType,DstType>::recvRetry()
301{
302 // we should never get a retry without having failed to forward
303 // something to this port
304 assert(waitingForPeer != NULL);
305
306 // add the port where the failed packet originated to the front of
307 // the waiting ports for the layer, this allows us to call retry
308 // on the port immediately if the crossbar layer is idle
309 waitingForLayer.push_front(waitingForPeer);
310
311 // we are no longer waiting for the peer
312 waitingForPeer = NULL;
313
314 // if the layer is idle, retry this port straight away, if we
315 // are busy, then simply let the port wait for its turn
316 if (state == IDLE) {
317 retryWaiting();
318 } else {
319 assert(state == BUSY);
320 }
321}
322
323PortID
324BaseXBar::findPort(Addr addr)
325{
326 // we should never see any address lookups before we've got the
327 // ranges of all connected slave modules
328 assert(gotAllAddrRanges);
329
330 // Check the cache
331 PortID dest_id = checkPortCache(addr);
332 if (dest_id != InvalidPortID)
333 return dest_id;
334
335 // Check the address map interval tree
336 auto i = portMap.find(addr);
337 if (i != portMap.end()) {
338 dest_id = i->second;
339 updatePortCache(dest_id, i->first);
340 return dest_id;
341 }
342
343 // Check if this matches the default range
344 if (useDefaultRange) {
345 if (defaultRange.contains(addr)) {
346 DPRINTF(AddrRanges, " found addr %#llx on default\n",
347 addr);
348 return defaultPortID;
349 }
350 } else if (defaultPortID != InvalidPortID) {
351 DPRINTF(AddrRanges, "Unable to find destination for addr %#llx, "
352 "will use default port\n", addr);
353 return defaultPortID;
354 }
355
356 // we should use the range for the default port and it did not
357 // match, or the default port is not set
358 fatal("Unable to find destination for addr %#llx on %s\n", addr,
359 name());
360}
361
362/** Function called by the port when the crossbar is receiving a range change.*/
363void
364BaseXBar::recvRangeChange(PortID master_port_id)
365{
366 DPRINTF(AddrRanges, "Received range change from slave port %s\n",
367 masterPorts[master_port_id]->getSlavePort().name());
368
369 // remember that we got a range from this master port and thus the
370 // connected slave module
371 gotAddrRanges[master_port_id] = true;
372
373 // update the global flag
374 if (!gotAllAddrRanges) {
375 // take a logical AND of all the ports and see if we got
376 // ranges from everyone
377 gotAllAddrRanges = true;
378 std::vector<bool>::const_iterator r = gotAddrRanges.begin();
379 while (gotAllAddrRanges && r != gotAddrRanges.end()) {
380 gotAllAddrRanges &= *r++;
381 }
382 if (gotAllAddrRanges)
383 DPRINTF(AddrRanges, "Got address ranges from all slaves\n");
384 }
385
386 // note that we could get the range from the default port at any
387 // point in time, and we cannot assume that the default range is
388 // set before the other ones are, so we do additional checks once
389 // all ranges are provided
390 if (master_port_id == defaultPortID) {
391 // only update if we are indeed checking ranges for the
392 // default port since the port might not have a valid range
393 // otherwise
394 if (useDefaultRange) {
395 AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges();
396
397 if (ranges.size() != 1)
398 fatal("Crossbar %s may only have a single default range",
399 name());
400
401 defaultRange = ranges.front();
402 }
403 } else {
404 // the ports are allowed to update their address ranges
405 // dynamically, so remove any existing entries
406 if (gotAddrRanges[master_port_id]) {
407 for (auto p = portMap.begin(); p != portMap.end(); ) {
408 if (p->second == master_port_id)
409 // erasing invalidates the iterator, so advance it
410 // before the deletion takes place
411 portMap.erase(p++);
412 else
413 p++;
414 }
415 }
416
417 AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges();
418
419 for (const auto& r: ranges) {
420 DPRINTF(AddrRanges, "Adding range %s for id %d\n",
421 r.to_string(), master_port_id);
422 if (portMap.insert(r, master_port_id) == portMap.end()) {
423 PortID conflict_id = portMap.find(r)->second;
424 fatal("%s has two ports responding within range %s:\n\t%s\n\t%s\n",
425 name(),
426 r.to_string(),
427 masterPorts[master_port_id]->getSlavePort().name(),
428 masterPorts[conflict_id]->getSlavePort().name());
429 }
430 }
431 }
432
433 // if we have received ranges from all our neighbouring slave
434 // modules, go ahead and tell our connected master modules in
435 // turn, this effectively assumes a tree structure of the system
436 if (gotAllAddrRanges) {
437 DPRINTF(AddrRanges, "Aggregating address ranges\n");
438 xbarRanges.clear();
439
440 // start out with the default range
441 if (useDefaultRange) {
442 if (!gotAddrRanges[defaultPortID])
443 fatal("Crossbar %s uses default range, but none provided",
444 name());
445
446 xbarRanges.push_back(defaultRange);
447 DPRINTF(AddrRanges, "-- Adding default %s\n",
448 defaultRange.to_string());
449 }
450
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());
471 }
472 intlv_ranges.clear();
473 }
474 intlv_ranges.push_back(r.first);
475 } else {
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());
482 }
483 }
484 }
485
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());
496 }
497 }
498
499 // also check that no range partially overlaps 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());
512 }
513 }
514
515 // tell all our neighbouring master ports that our address
516 // ranges have changed
517 for (const auto& s: slavePorts)
518 s->sendRangeChange();
519 }
520
521 clearPortCache();
522}
523
524AddrRangeList
525BaseXBar::getAddrRanges() const
526{
527 // we should never be asked without first having sent a range
528 // change, and the latter is only done once we have all the ranges
529 // of the connected devices
530 assert(gotAllAddrRanges);
531
532 // at the moment, this never happens, as there are no cycles in
533 // the range queries and no devices on the master side of a crossbar
534 // (CPU, cache, bridge etc) actually care about the ranges of the
535 // ports they are connected to
536
537 DPRINTF(AddrRanges, "Received address range request\n");
538
539 return xbarRanges;
540}
541
542void
543BaseXBar::regStats()
544{
545 ClockedObject::regStats();
546
547 using namespace Stats;
548
549 transDist
550 .init(MemCmd::NUM_MEM_CMDS)
551 .name(name() + ".trans_dist")
552 .desc("Transaction distribution")
553 .flags(nozero);
554
555 // get the string representation of the commands
556 for (int i = 0; i < MemCmd::NUM_MEM_CMDS; i++) {
557 MemCmd cmd(i);
558 const std::string &cstr = cmd.toString();
559 transDist.subname(i, cstr);
560 }
561
562 pktCount
563 .init(slavePorts.size(), masterPorts.size())
564 .name(name() + ".pkt_count")
565 .desc("Packet count per connected master and slave (bytes)")
566 .flags(total | nozero | nonan);
567
568 pktSize
569 .init(slavePorts.size(), masterPorts.size())
570 .name(name() + ".pkt_size")
571 .desc("Cumulative packet size per connected master and slave (bytes)")
572 .flags(total | nozero | nonan);
573
574 // both the packet count and total size are two-dimensional
575 // vectors, indexed by slave port id and master port id, thus the
576 // neighbouring master and slave, they do not differentiate what
577 // came from the master and was forwarded to the slave (requests
578 // and snoop responses) and what came from the slave and was
579 // forwarded to the master (responses and snoop requests)
580 for (int i = 0; i < slavePorts.size(); i++) {
581 pktCount.subname(i, slavePorts[i]->getMasterPort().name());
582 pktSize.subname(i, slavePorts[i]->getMasterPort().name());
583 for (int j = 0; j < masterPorts.size(); j++) {
584 pktCount.ysubname(j, masterPorts[j]->getSlavePort().name());
585 pktSize.ysubname(j, masterPorts[j]->getSlavePort().name());
586 }
587 }
588}
589
590template <typename SrcType, typename DstType>
591DrainState
592BaseXBar::Layer<SrcType,DstType>::drain()
593{
594 //We should check that we're not "doing" anything, and that noone is
595 //waiting. We might be idle but have someone waiting if the device we
596 //contacted for a retry didn't actually retry.
597 if (state != IDLE) {
598 DPRINTF(Drain, "Crossbar not drained\n");
599 return DrainState::Draining;
600 } else {
601 return DrainState::Drained;
602 }
603}
604
605template <typename SrcType, typename DstType>
606void
607BaseXBar::Layer<SrcType,DstType>::regStats()
608{
609 using namespace Stats;
610
611 occupancy
612 .name(name() + ".occupancy")
613 .desc("Layer occupancy (ticks)")
614 .flags(nozero);
615
616 utilization
617 .name(name() + ".utilization")
618 .desc("Layer utilization (%)")
619 .precision(1)
620 .flags(nozero);
621
622 utilization = 100 * occupancy / simTicks;
623}
624
625/**
626 * Crossbar layer template instantiations. Could be removed with _impl.hh
627 * file, but since there are only two given options (MasterPort and
628 * SlavePort) it seems a bit excessive at this point.
629 */
630template class BaseXBar::Layer<SlavePort,MasterPort>;
631template class BaseXBar::Layer<MasterPort,SlavePort>;