xbar.cc revision 10405
12497SN/A/*
210405Sandreas.hansson@arm.com * Copyright (c) 2011-2014 ARM Limited
38711SN/A * All rights reserved
48711SN/A *
58711SN/A * The license below extends only to copyright in the software and shall
68711SN/A * not be construed as granting a license to any other intellectual
78711SN/A * property including but not limited to intellectual property relating
88711SN/A * to a hardware implementation of the functionality of the software
98711SN/A * licensed hereunder.  You may use the software subject to the license
108711SN/A * terms below provided that you ensure that this notice is replicated
118711SN/A * unmodified and in its entirety in all distributions of the software,
128711SN/A * modified or unmodified, in source code or in binary form.
138711SN/A *
142497SN/A * Copyright (c) 2006 The Regents of The University of Michigan
152497SN/A * All rights reserved.
162497SN/A *
172497SN/A * Redistribution and use in source and binary forms, with or without
182497SN/A * modification, are permitted provided that the following conditions are
192497SN/A * met: redistributions of source code must retain the above copyright
202497SN/A * notice, this list of conditions and the following disclaimer;
212497SN/A * redistributions in binary form must reproduce the above copyright
222497SN/A * notice, this list of conditions and the following disclaimer in the
232497SN/A * documentation and/or other materials provided with the distribution;
242497SN/A * neither the name of the copyright holders nor the names of its
252497SN/A * contributors may be used to endorse or promote products derived from
262497SN/A * this software without specific prior written permission.
272497SN/A *
282497SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292497SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302497SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
312497SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322497SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332497SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
342497SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352497SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362497SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372497SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
382497SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392665SN/A *
402665SN/A * Authors: Ali Saidi
418715SN/A *          Andreas Hansson
428922SN/A *          William Wang
432497SN/A */
442497SN/A
452497SN/A/**
462982SN/A * @file
4710405Sandreas.hansson@arm.com * Definition of a crossbar object.
482497SN/A */
492497SN/A
502846SN/A#include "base/misc.hh"
512548SN/A#include "base/trace.hh"
5210405Sandreas.hansson@arm.com#include "debug/AddrRanges.hh"
539152SN/A#include "debug/Drain.hh"
5410405Sandreas.hansson@arm.com#include "debug/XBar.hh"
5510405Sandreas.hansson@arm.com#include "mem/xbar.hh"
562497SN/A
5710405Sandreas.hansson@arm.comBaseXBar::BaseXBar(const BaseXBarParams *p)
589157SN/A    : MemObject(p),
599091SN/A      headerCycles(p->header_cycles), width(p->width),
609279SN/A      gotAddrRanges(p->port_default_connection_count +
619279SN/A                          p->port_master_connection_count, false),
629279SN/A      gotAllAddrRanges(false), defaultPortID(InvalidPortID),
639814SN/A      useDefaultRange(p->use_default_range)
649240SN/A{}
658851SN/A
6610405Sandreas.hansson@arm.comBaseXBar::~BaseXBar()
679036SN/A{
6810405Sandreas.hansson@arm.com    for (auto m: masterPorts)
6910405Sandreas.hansson@arm.com        delete m;
708851SN/A
7110405Sandreas.hansson@arm.com    for (auto s: slavePorts)
7210405Sandreas.hansson@arm.com        delete s;
737523SN/A}
747523SN/A
759278SN/Avoid
7610405Sandreas.hansson@arm.comBaseXBar::init()
779278SN/A{
789278SN/A}
799278SN/A
809294SN/ABaseMasterPort &
8110405Sandreas.hansson@arm.comBaseXBar::getMasterPort(const std::string &if_name, PortID idx)
822640SN/A{
838948SN/A    if (if_name == "master" && idx < masterPorts.size()) {
848948SN/A        // the master port index translates directly to the vector position
858922SN/A        return *masterPorts[idx];
868851SN/A    } else  if (if_name == "default") {
879031SN/A        return *masterPorts[defaultPortID];
888715SN/A    } else {
898922SN/A        return MemObject::getMasterPort(if_name, idx);
908922SN/A    }
918922SN/A}
928922SN/A
939294SN/ABaseSlavePort &
9410405Sandreas.hansson@arm.comBaseXBar::getSlavePort(const std::string &if_name, PortID idx)
958922SN/A{
968948SN/A    if (if_name == "slave" && idx < slavePorts.size()) {
978948SN/A        // the slave port index translates directly to the vector position
988948SN/A        return *slavePorts[idx];
998922SN/A    } else {
1008922SN/A        return MemObject::getSlavePort(if_name, idx);
1013489SN/A    }
1022640SN/A}
1032640SN/A
1049547SN/Avoid
10510405Sandreas.hansson@arm.comBaseXBar::calcPacketTiming(PacketPtr pkt)
1062497SN/A{
10710405Sandreas.hansson@arm.com    // the crossbar will be called at a time that is not necessarily
1089547SN/A    // coinciding with its own clock, so start by determining how long
1099547SN/A    // until the next clock edge (could be zero)
1109648SN/A    Tick offset = clockEdge() - curTick();
1115354SN/A
1129547SN/A    // determine how many cycles are needed to send the data
1139547SN/A    unsigned dataCycles = pkt->hasData() ? divCeil(pkt->getSize(), width) : 0;
1143210SN/A
1159549SN/A    // before setting the bus delay fields of the packet, ensure that
11610405Sandreas.hansson@arm.com    // the delay from any previous crossbar has been accounted for
11710405Sandreas.hansson@arm.com    if (pkt->firstWordDelay != 0 || pkt->lastWordDelay != 0)
11810405Sandreas.hansson@arm.com        panic("Packet %s already has delay (%d, %d) that should be "
11910405Sandreas.hansson@arm.com              "accounted for.\n", pkt->cmdString(), pkt->firstWordDelay,
12010405Sandreas.hansson@arm.com              pkt->lastWordDelay);
1219549SN/A
1229545SN/A    // The first word will be delivered on the cycle after the header.
12310405Sandreas.hansson@arm.com    pkt->firstWordDelay = (headerCycles + 1) * clockPeriod() + offset;
1243218SN/A
12510405Sandreas.hansson@arm.com    // Note that currently lastWordDelay can be smaller than
12610405Sandreas.hansson@arm.com    // firstWordDelay if the packet has no data
12710405Sandreas.hansson@arm.com    pkt->lastWordDelay = (headerCycles + dataCycles) * clockPeriod() +
1289547SN/A        offset;
1295354SN/A}
1305354SN/A
1319715SN/Atemplate <typename SrcType, typename DstType>
13210405Sandreas.hansson@arm.comBaseXBar::Layer<SrcType,DstType>::Layer(DstType& _port, BaseXBar& _xbar,
1339715SN/A                                       const std::string& _name) :
13410405Sandreas.hansson@arm.com    port(_port), xbar(_xbar), _name(_name), state(IDLE), drainManager(NULL),
13510347SN/A    waitingForPeer(NULL), releaseEvent(this)
1369092SN/A{
1379092SN/A}
1389092SN/A
1399715SN/Atemplate <typename SrcType, typename DstType>
14010405Sandreas.hansson@arm.comvoid BaseXBar::Layer<SrcType,DstType>::occupyLayer(Tick until)
1415354SN/A{
14210405Sandreas.hansson@arm.com    // ensure the state is busy at this point, as the layer should
1439612SN/A    // transition from idle as soon as it has decided to forward the
1449612SN/A    // packet to prevent any follow-on calls to sendTiming seeing an
14510405Sandreas.hansson@arm.com    // unoccupied layer
1469612SN/A    assert(state == BUSY);
1475354SN/A
14810405Sandreas.hansson@arm.com    // until should never be 0 as express snoops never occupy the layer
1499091SN/A    assert(until != 0);
15010405Sandreas.hansson@arm.com    xbar.schedule(releaseEvent, until);
1519091SN/A
1529712SN/A    // account for the occupied ticks
1539712SN/A    occupancy += until - curTick();
1549712SN/A
15510405Sandreas.hansson@arm.com    DPRINTF(BaseXBar, "The crossbar layer is now busy from tick %d to %d\n",
1569091SN/A            curTick(), until);
1573244SN/A}
1583244SN/A
1599715SN/Atemplate <typename SrcType, typename DstType>
1608948SN/Abool
16110405Sandreas.hansson@arm.comBaseXBar::Layer<SrcType,DstType>::tryTiming(SrcType* src_port)
1628948SN/A{
1639716SN/A    // if we are in the retry state, we will not see anything but the
1649716SN/A    // retrying port (or in the case of the snoop ports the snoop
1659716SN/A    // response port that mirrors the actual slave port) as we leave
1669716SN/A    // this state again in zero time if the peer does not immediately
16710405Sandreas.hansson@arm.com    // call the layer when receiving the retry
1689716SN/A
1699716SN/A    // first we see if the layer is busy, next we check if the
1709716SN/A    // destination port is already engaged in a transaction waiting
1719716SN/A    // for a retry from the peer
1729716SN/A    if (state == BUSY || waitingForPeer != NULL) {
1739716SN/A        // the port should not be waiting already
1749716SN/A        assert(std::find(waitingForLayer.begin(), waitingForLayer.end(),
1759716SN/A                         src_port) == waitingForLayer.end());
1769716SN/A
1779612SN/A        // put the port at the end of the retry list waiting for the
1789612SN/A        // layer to be freed up (and in the case of a busy peer, for
17910405Sandreas.hansson@arm.com        // that transaction to go through, and then the layer to free
1809612SN/A        // up)
1819715SN/A        waitingForLayer.push_back(src_port);
1829091SN/A        return false;
1838948SN/A    }
1849091SN/A
1859612SN/A    state = BUSY;
1869611SN/A
1879091SN/A    return true;
1888948SN/A}
1898948SN/A
1909715SN/Atemplate <typename SrcType, typename DstType>
1918975SN/Avoid
19210405Sandreas.hansson@arm.comBaseXBar::Layer<SrcType,DstType>::succeededTiming(Tick busy_time)
1938948SN/A{
1949612SN/A    // we should have gone from idle or retry to busy in the tryTiming
1959612SN/A    // test
1969091SN/A    assert(state == BUSY);
1979091SN/A
19810405Sandreas.hansson@arm.com    // occupy the layer accordingly
1999092SN/A    occupyLayer(busy_time);
2009091SN/A}
2019091SN/A
2029715SN/Atemplate <typename SrcType, typename DstType>
2039091SN/Avoid
20410405Sandreas.hansson@arm.comBaseXBar::Layer<SrcType,DstType>::failedTiming(SrcType* src_port,
2059715SN/A                                              Tick busy_time)
2069091SN/A{
2079612SN/A    // ensure no one got in between and tried to send something to
2089612SN/A    // this port
2099715SN/A    assert(waitingForPeer == NULL);
2109091SN/A
2119612SN/A    // if the source port is the current retrying one or not, we have
2129612SN/A    // failed in forwarding and should track that we are now waiting
2139612SN/A    // for the peer to send a retry
2149715SN/A    waitingForPeer = src_port;
2159612SN/A
2169612SN/A    // we should have gone from idle or retry to busy in the tryTiming
2179612SN/A    // test
2189612SN/A    assert(state == BUSY);
2199091SN/A
2209091SN/A    // occupy the bus accordingly
2219092SN/A    occupyLayer(busy_time);
2228948SN/A}
2238948SN/A
2249715SN/Atemplate <typename SrcType, typename DstType>
2258948SN/Avoid
22610405Sandreas.hansson@arm.comBaseXBar::Layer<SrcType,DstType>::releaseLayer()
2272657SN/A{
2288915SN/A    // releasing the bus means we should now be idle
2299091SN/A    assert(state == BUSY);
2309092SN/A    assert(!releaseEvent.scheduled());
2319091SN/A
2329091SN/A    // update the state
2339091SN/A    state = IDLE;
2343252SN/A
2359612SN/A    // bus layer is now idle, so if someone is waiting we can retry
2369612SN/A    if (!waitingForLayer.empty()) {
23710347SN/A        // there is no point in sending a retry if someone is still
23810347SN/A        // waiting for the peer
23910347SN/A        if (waitingForPeer == NULL)
24010347SN/A            retryWaiting();
2419710SN/A    } else if (waitingForPeer == NULL && drainManager) {
24210405Sandreas.hansson@arm.com        DPRINTF(Drain, "Crossbar done draining, signaling drain manager\n");
2439091SN/A        //If we weren't able to drain before, do it now.
2449342SN/A        drainManager->signalDrainDone();
2453512SN/A        // Clear the drain event once we're done with it.
2469342SN/A        drainManager = NULL;
2473512SN/A    }
2482657SN/A}
2492657SN/A
2509715SN/Atemplate <typename SrcType, typename DstType>
2518915SN/Avoid
25210405Sandreas.hansson@arm.comBaseXBar::Layer<SrcType,DstType>::retryWaiting()
2538915SN/A{
2549612SN/A    // this should never be called with no one waiting
2559612SN/A    assert(!waitingForLayer.empty());
2568915SN/A
2579091SN/A    // we always go to retrying from idle
2589091SN/A    assert(state == IDLE);
2599091SN/A
2609611SN/A    // update the state
2619091SN/A    state = RETRY;
2628915SN/A
2639611SN/A    // set the retrying port to the front of the retry list and pop it
2649611SN/A    // off the list
26510347SN/A    SrcType* retryingPort = waitingForLayer.front();
2669612SN/A    waitingForLayer.pop_front();
2679611SN/A
2689612SN/A    // tell the port to retry, which in some cases ends up calling the
26910405Sandreas.hansson@arm.com    // layer again
2709611SN/A    retryingPort->sendRetry();
2718915SN/A
27210405Sandreas.hansson@arm.com    // If the layer is still in the retry state, sendTiming wasn't
2739612SN/A    // called in zero time (e.g. the cache does this), burn a cycle
2749091SN/A    if (state == RETRY) {
2759612SN/A        // update the state to busy and reset the retrying port, we
2769612SN/A        // have done our bit and sent the retry
2779091SN/A        state = BUSY;
2789091SN/A
27910405Sandreas.hansson@arm.com        // occupy the crossbar layer until the next cycle ends
28010405Sandreas.hansson@arm.com        occupyLayer(xbar.clockEdge(Cycles(1)));
2818915SN/A    }
2828915SN/A}
2838915SN/A
2849715SN/Atemplate <typename SrcType, typename DstType>
2858915SN/Avoid
28610405Sandreas.hansson@arm.comBaseXBar::Layer<SrcType,DstType>::recvRetry()
2878915SN/A{
2889612SN/A    // we should never get a retry without having failed to forward
2899612SN/A    // something to this port
2909715SN/A    assert(waitingForPeer != NULL);
2918915SN/A
2929715SN/A    // add the port where the failed packet originated to the front of
2939715SN/A    // the waiting ports for the layer, this allows us to call retry
29410405Sandreas.hansson@arm.com    // on the port immediately if the crossbar layer is idle
2959715SN/A    waitingForLayer.push_front(waitingForPeer);
2969612SN/A
2979715SN/A    // we are no longer waiting for the peer
2989715SN/A    waitingForPeer = NULL;
2999612SN/A
30010405Sandreas.hansson@arm.com    // if the layer is idle, retry this port straight away, if we
3019612SN/A    // are busy, then simply let the port wait for its turn
3029091SN/A    if (state == IDLE) {
3038915SN/A        retryWaiting();
3049612SN/A    } else {
3059612SN/A        assert(state == BUSY);
3068915SN/A    }
3078915SN/A}
3088915SN/A
3099031SN/APortID
31010405Sandreas.hansson@arm.comBaseXBar::findPort(Addr addr)
3112497SN/A{
3129279SN/A    // we should never see any address lookups before we've got the
3139279SN/A    // ranges of all connected slave modules
3149279SN/A    assert(gotAllAddrRanges);
3159279SN/A
3169279SN/A    // Check the cache
3179031SN/A    PortID dest_id = checkPortCache(addr);
3189031SN/A    if (dest_id != InvalidPortID)
3197523SN/A        return dest_id;
3207523SN/A
3219279SN/A    // Check the address map interval tree
32210405Sandreas.hansson@arm.com    auto i = portMap.find(addr);
3237523SN/A    if (i != portMap.end()) {
3247523SN/A        dest_id = i->second;
3259279SN/A        updatePortCache(dest_id, i->first);
3267523SN/A        return dest_id;
3274958SN/A    }
3282846SN/A
3292846SN/A    // Check if this matches the default range
3307523SN/A    if (useDefaultRange) {
3319405SN/A        if (defaultRange.contains(addr)) {
33210405Sandreas.hansson@arm.com            DPRINTF(AddrRanges, "  found addr %#llx on default\n",
3339279SN/A                    addr);
3349279SN/A            return defaultPortID;
3352846SN/A        }
3369031SN/A    } else if (defaultPortID != InvalidPortID) {
33710405Sandreas.hansson@arm.com        DPRINTF(AddrRanges, "Unable to find destination for addr %#llx, "
3388849SN/A                "will use default port\n", addr);
3399031SN/A        return defaultPortID;
3402846SN/A    }
3412846SN/A
3428849SN/A    // we should use the range for the default port and it did not
3438849SN/A    // match, or the default port is not set
34410405Sandreas.hansson@arm.com    fatal("Unable to find destination for addr %#llx on %s\n", addr,
3458849SN/A          name());
3463074SN/A}
3473074SN/A
34810405Sandreas.hansson@arm.com/** Function called by the port when the crossbar is receiving a range change.*/
3492497SN/Avoid
35010405Sandreas.hansson@arm.comBaseXBar::recvRangeChange(PortID master_port_id)
3512497SN/A{
35210405Sandreas.hansson@arm.com    DPRINTF(AddrRanges, "Received range change from slave port %s\n",
3539407SN/A            masterPorts[master_port_id]->getSlavePort().name());
3549407SN/A
3559279SN/A    // remember that we got a range from this master port and thus the
3569279SN/A    // connected slave module
3579279SN/A    gotAddrRanges[master_port_id] = true;
3582846SN/A
3599279SN/A    // update the global flag
3609279SN/A    if (!gotAllAddrRanges) {
3619279SN/A        // take a logical AND of all the ports and see if we got
3629279SN/A        // ranges from everyone
3639279SN/A        gotAllAddrRanges = true;
3649279SN/A        std::vector<bool>::const_iterator r = gotAddrRanges.begin();
3659279SN/A        while (gotAllAddrRanges &&  r != gotAddrRanges.end()) {
3669279SN/A            gotAllAddrRanges &= *r++;
3679279SN/A        }
3689407SN/A        if (gotAllAddrRanges)
36910405Sandreas.hansson@arm.com            DPRINTF(AddrRanges, "Got address ranges from all slaves\n");
3709279SN/A    }
3712534SN/A
3729279SN/A    // note that we could get the range from the default port at any
3739279SN/A    // point in time, and we cannot assume that the default range is
3749279SN/A    // set before the other ones are, so we do additional checks once
3759279SN/A    // all ranges are provided
3769032SN/A    if (master_port_id == defaultPortID) {
3779279SN/A        // only update if we are indeed checking ranges for the
3789279SN/A        // default port since the port might not have a valid range
3799279SN/A        // otherwise
3807523SN/A        if (useDefaultRange) {
3819279SN/A            AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges();
3829279SN/A
3839279SN/A            if (ranges.size() != 1)
38410405Sandreas.hansson@arm.com                fatal("Crossbar %s may only have a single default range",
3859279SN/A                      name());
3869279SN/A
3879279SN/A            defaultRange = ranges.front();
3889279SN/A        }
3899279SN/A    } else {
3909279SN/A        // the ports are allowed to update their address ranges
3919279SN/A        // dynamically, so remove any existing entries
3929279SN/A        if (gotAddrRanges[master_port_id]) {
39310405Sandreas.hansson@arm.com            for (auto p = portMap.begin(); p != portMap.end(); ) {
3949279SN/A                if (p->second == master_port_id)
3959279SN/A                    // erasing invalidates the iterator, so advance it
3969279SN/A                    // before the deletion takes place
3979279SN/A                    portMap.erase(p++);
3989279SN/A                else
3999279SN/A                    p++;
4003489SN/A            }
4012846SN/A        }
4022565SN/A
4039279SN/A        AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges();
4042497SN/A
40510405Sandreas.hansson@arm.com        for (const auto& r: ranges) {
40610405Sandreas.hansson@arm.com            DPRINTF(AddrRanges, "Adding range %s for id %d\n",
40710405Sandreas.hansson@arm.com                    r.to_string(), master_port_id);
40810405Sandreas.hansson@arm.com            if (portMap.insert(r, master_port_id) == portMap.end()) {
40910405Sandreas.hansson@arm.com                PortID conflict_id = portMap.find(r)->second;
4105490SN/A                fatal("%s has two ports with same range:\n\t%s\n\t%s\n",
4119032SN/A                      name(),
4129032SN/A                      masterPorts[master_port_id]->getSlavePort().name(),
4138922SN/A                      masterPorts[conflict_id]->getSlavePort().name());
4145490SN/A            }
4152846SN/A        }
4162565SN/A    }
4172568SN/A
4189279SN/A    // if we have received ranges from all our neighbouring slave
4199279SN/A    // modules, go ahead and tell our connected master modules in
4209279SN/A    // turn, this effectively assumes a tree structure of the system
4219279SN/A    if (gotAllAddrRanges) {
42210405Sandreas.hansson@arm.com        DPRINTF(AddrRanges, "Aggregating address ranges\n");
42310405Sandreas.hansson@arm.com        xbarRanges.clear();
4249564SN/A
4259564SN/A        // start out with the default range
4269564SN/A        if (useDefaultRange) {
4279564SN/A            if (!gotAddrRanges[defaultPortID])
42810405Sandreas.hansson@arm.com                fatal("Crossbar %s uses default range, but none provided",
4299564SN/A                      name());
4309564SN/A
43110405Sandreas.hansson@arm.com            xbarRanges.push_back(defaultRange);
43210405Sandreas.hansson@arm.com            DPRINTF(AddrRanges, "-- Adding default %s\n",
4339564SN/A                    defaultRange.to_string());
4349564SN/A        }
4359564SN/A
4369564SN/A        // merge all interleaved ranges and add any range that is not
4379564SN/A        // a subset of the default range
4389564SN/A        std::vector<AddrRange> intlv_ranges;
43910405Sandreas.hansson@arm.com        for (const auto& r: portMap) {
4409564SN/A            // if the range is interleaved then save it for now
44110405Sandreas.hansson@arm.com            if (r.first.interleaved()) {
4429564SN/A                // if we already got interleaved ranges that are not
4439564SN/A                // part of the same range, then first do a merge
4449564SN/A                // before we add the new one
4459564SN/A                if (!intlv_ranges.empty() &&
44610405Sandreas.hansson@arm.com                    !intlv_ranges.back().mergesWith(r.first)) {
44710405Sandreas.hansson@arm.com                    DPRINTF(AddrRanges, "-- Merging range from %d ranges\n",
4489564SN/A                            intlv_ranges.size());
4499564SN/A                    AddrRange merged_range(intlv_ranges);
4509564SN/A                    // next decide if we keep the merged range or not
4519564SN/A                    if (!(useDefaultRange &&
4529564SN/A                          merged_range.isSubset(defaultRange))) {
45310405Sandreas.hansson@arm.com                        xbarRanges.push_back(merged_range);
45410405Sandreas.hansson@arm.com                        DPRINTF(AddrRanges, "-- Adding merged range %s\n",
4559564SN/A                                merged_range.to_string());
4569564SN/A                    }
4579564SN/A                    intlv_ranges.clear();
4589564SN/A                }
45910405Sandreas.hansson@arm.com                intlv_ranges.push_back(r.first);
4609564SN/A            } else {
4619564SN/A                // keep the current range if not a subset of the default
4629564SN/A                if (!(useDefaultRange &&
46310405Sandreas.hansson@arm.com                      r.first.isSubset(defaultRange))) {
46410405Sandreas.hansson@arm.com                    xbarRanges.push_back(r.first);
46510405Sandreas.hansson@arm.com                    DPRINTF(AddrRanges, "-- Adding range %s\n",
46610405Sandreas.hansson@arm.com                            r.first.to_string());
4679564SN/A                }
4689564SN/A            }
4699564SN/A        }
4709564SN/A
4719564SN/A        // if there is still interleaved ranges waiting to be merged,
4729564SN/A        // go ahead and do it
4739564SN/A        if (!intlv_ranges.empty()) {
47410405Sandreas.hansson@arm.com            DPRINTF(AddrRanges, "-- Merging range from %d ranges\n",
4759564SN/A                    intlv_ranges.size());
4769564SN/A            AddrRange merged_range(intlv_ranges);
4779564SN/A            if (!(useDefaultRange && merged_range.isSubset(defaultRange))) {
47810405Sandreas.hansson@arm.com                xbarRanges.push_back(merged_range);
47910405Sandreas.hansson@arm.com                DPRINTF(AddrRanges, "-- Adding merged range %s\n",
4809564SN/A                        merged_range.to_string());
4819564SN/A            }
4829564SN/A        }
4839564SN/A
4849279SN/A        // also check that no range partially overlaps with the
4859279SN/A        // default range, this has to be done after all ranges are set
4869279SN/A        // as there are no guarantees for when the default range is
4879279SN/A        // update with respect to the other ones
4889279SN/A        if (useDefaultRange) {
48910405Sandreas.hansson@arm.com            for (const auto& r: xbarRanges) {
4909564SN/A                // see if the new range is partially
4919564SN/A                // overlapping the default range
49210405Sandreas.hansson@arm.com                if (r.intersects(defaultRange) &&
49310405Sandreas.hansson@arm.com                    !r.isSubset(defaultRange))
4949564SN/A                    fatal("Range %s intersects the "                    \
4959564SN/A                          "default range of %s but is not a "           \
49610405Sandreas.hansson@arm.com                          "subset\n", r.to_string(), name());
4979279SN/A            }
4989279SN/A        }
4999279SN/A
5009279SN/A        // tell all our neighbouring master ports that our address
5019279SN/A        // ranges have changed
50210405Sandreas.hansson@arm.com        for (const auto& s: slavePorts)
50310405Sandreas.hansson@arm.com            s->sendRangeChange();
5049279SN/A    }
5059279SN/A
5069279SN/A    clearPortCache();
5072497SN/A}
5082497SN/A
5098711SN/AAddrRangeList
51010405Sandreas.hansson@arm.comBaseXBar::getAddrRanges() const
5112497SN/A{
5129279SN/A    // we should never be asked without first having sent a range
5139279SN/A    // change, and the latter is only done once we have all the ranges
5149279SN/A    // of the connected devices
5159279SN/A    assert(gotAllAddrRanges);
5162568SN/A
5179407SN/A    // at the moment, this never happens, as there are no cycles in
51810405Sandreas.hansson@arm.com    // the range queries and no devices on the master side of a crossbar
5199407SN/A    // (CPU, cache, bridge etc) actually care about the ranges of the
5209407SN/A    // ports they are connected to
5219407SN/A
52210405Sandreas.hansson@arm.com    DPRINTF(AddrRanges, "Received address range request\n");
5232846SN/A
52410405Sandreas.hansson@arm.com    return xbarRanges;
5258711SN/A}
5268711SN/A
5279712SN/Avoid
52810405Sandreas.hansson@arm.comBaseXBar::regStats()
5299712SN/A{
5309712SN/A    using namespace Stats;
5319712SN/A
5329712SN/A    transDist
5339712SN/A        .init(MemCmd::NUM_MEM_CMDS)
5349712SN/A        .name(name() + ".trans_dist")
5359712SN/A        .desc("Transaction distribution")
5369712SN/A        .flags(nozero);
5379712SN/A
5389712SN/A    // get the string representation of the commands
5399712SN/A    for (int i = 0; i < MemCmd::NUM_MEM_CMDS; i++) {
5409712SN/A        MemCmd cmd(i);
5419712SN/A        const std::string &cstr = cmd.toString();
5429712SN/A        transDist.subname(i, cstr);
5439712SN/A    }
5449712SN/A
5459712SN/A    pktCount
5469712SN/A        .init(slavePorts.size(), masterPorts.size())
5479712SN/A        .name(name() + ".pkt_count")
5489712SN/A        .desc("Packet count per connected master and slave (bytes)")
5499712SN/A        .flags(total | nozero | nonan);
5509712SN/A
55110405Sandreas.hansson@arm.com    pktSize
5529712SN/A        .init(slavePorts.size(), masterPorts.size())
55310405Sandreas.hansson@arm.com        .name(name() + ".pkt_size")
5549712SN/A        .desc("Cumulative packet size per connected master and slave (bytes)")
5559712SN/A        .flags(total | nozero | nonan);
5569712SN/A
5579712SN/A    // both the packet count and total size are two-dimensional
5589712SN/A    // vectors, indexed by slave port id and master port id, thus the
5599712SN/A    // neighbouring master and slave, they do not differentiate what
5609712SN/A    // came from the master and was forwarded to the slave (requests
5619712SN/A    // and snoop responses) and what came from the slave and was
5629712SN/A    // forwarded to the master (responses and snoop requests)
5639712SN/A    for (int i = 0; i < slavePorts.size(); i++) {
5649712SN/A        pktCount.subname(i, slavePorts[i]->getMasterPort().name());
56510405Sandreas.hansson@arm.com        pktSize.subname(i, slavePorts[i]->getMasterPort().name());
5669712SN/A        for (int j = 0; j < masterPorts.size(); j++) {
5679712SN/A            pktCount.ysubname(j, masterPorts[j]->getSlavePort().name());
56810405Sandreas.hansson@arm.com            pktSize.ysubname(j, masterPorts[j]->getSlavePort().name());
5699712SN/A        }
5709712SN/A    }
5719712SN/A}
5729712SN/A
5739715SN/Atemplate <typename SrcType, typename DstType>
5743470SN/Aunsigned int
57510405Sandreas.hansson@arm.comBaseXBar::Layer<SrcType,DstType>::drain(DrainManager *dm)
5763470SN/A{
5773470SN/A    //We should check that we're not "doing" anything, and that noone is
5783470SN/A    //waiting. We might be idle but have someone waiting if the device we
5793470SN/A    //contacted for a retry didn't actually retry.
5809612SN/A    if (state != IDLE) {
58110405Sandreas.hansson@arm.com        DPRINTF(Drain, "Crossbar not drained\n");
5829342SN/A        drainManager = dm;
5833470SN/A        return 1;
5843470SN/A    }
5855057SN/A    return 0;
5863470SN/A}
5879093SN/A
5889715SN/Atemplate <typename SrcType, typename DstType>
5899712SN/Avoid
59010405Sandreas.hansson@arm.comBaseXBar::Layer<SrcType,DstType>::regStats()
5919712SN/A{
5929712SN/A    using namespace Stats;
5939712SN/A
5949712SN/A    occupancy
5959712SN/A        .name(name() + ".occupancy")
5969712SN/A        .desc("Layer occupancy (ticks)")
5979712SN/A        .flags(nozero);
5989712SN/A
5999712SN/A    utilization
6009712SN/A        .name(name() + ".utilization")
6019712SN/A        .desc("Layer utilization (%)")
6029712SN/A        .precision(1)
6039712SN/A        .flags(nozero);
6049712SN/A
6059712SN/A    utilization = 100 * occupancy / simTicks;
6069712SN/A}
6079712SN/A
6089093SN/A/**
60910405Sandreas.hansson@arm.com * Crossbar layer template instantiations. Could be removed with _impl.hh
6109093SN/A * file, but since there are only two given options (MasterPort and
6119093SN/A * SlavePort) it seems a bit excessive at this point.
6129093SN/A */
61310405Sandreas.hansson@arm.comtemplate class BaseXBar::Layer<SlavePort,MasterPort>;
61410405Sandreas.hansson@arm.comtemplate class BaseXBar::Layer<MasterPort,SlavePort>;
615