xbar.cc revision 10719:b4fc9ad648aa
15083Sgblack@eecs.umich.edu/*
25083Sgblack@eecs.umich.edu * Copyright (c) 2011-2015 ARM Limited
35083Sgblack@eecs.umich.edu * All rights reserved
45083Sgblack@eecs.umich.edu *
55083Sgblack@eecs.umich.edu * The license below extends only to copyright in the software and shall
65083Sgblack@eecs.umich.edu * not be construed as granting a license to any other intellectual
75083Sgblack@eecs.umich.edu * property including but not limited to intellectual property relating
85083Sgblack@eecs.umich.edu * to a hardware implementation of the functionality of the software
95083Sgblack@eecs.umich.edu * licensed hereunder.  You may use the software subject to the license
105083Sgblack@eecs.umich.edu * terms below provided that you ensure that this notice is replicated
115083Sgblack@eecs.umich.edu * unmodified and in its entirety in all distributions of the software,
125083Sgblack@eecs.umich.edu * modified or unmodified, in source code or in binary form.
135083Sgblack@eecs.umich.edu *
145083Sgblack@eecs.umich.edu * Copyright (c) 2006 The Regents of The University of Michigan
155083Sgblack@eecs.umich.edu * All rights reserved.
165083Sgblack@eecs.umich.edu *
175083Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
185083Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
195083Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
205083Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
215083Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
225083Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
235083Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
245083Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
255083Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
265083Sgblack@eecs.umich.edu * this software without specific prior written permission.
275083Sgblack@eecs.umich.edu *
285083Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
295083Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
305083Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
315083Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
325083Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
335083Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
345083Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
355083Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
365083Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
375083Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
385083Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
395083Sgblack@eecs.umich.edu *
405083Sgblack@eecs.umich.edu * Authors: Ali Saidi
415083Sgblack@eecs.umich.edu *          Andreas Hansson
425083Sgblack@eecs.umich.edu *          William Wang
435083Sgblack@eecs.umich.edu */
445083Sgblack@eecs.umich.edu
455083Sgblack@eecs.umich.edu/**
465083Sgblack@eecs.umich.edu * @file
475083Sgblack@eecs.umich.edu * Definition of a crossbar object.
485083Sgblack@eecs.umich.edu */
495083Sgblack@eecs.umich.edu
505083Sgblack@eecs.umich.edu#include "base/misc.hh"
515083Sgblack@eecs.umich.edu#include "base/trace.hh"
525083Sgblack@eecs.umich.edu#include "debug/AddrRanges.hh"
535083Sgblack@eecs.umich.edu#include "debug/Drain.hh"
545083Sgblack@eecs.umich.edu#include "debug/XBar.hh"
555083Sgblack@eecs.umich.edu#include "mem/xbar.hh"
565083Sgblack@eecs.umich.edu
575083Sgblack@eecs.umich.eduBaseXBar::BaseXBar(const BaseXBarParams *p)
585083Sgblack@eecs.umich.edu    : MemObject(p),
595083Sgblack@eecs.umich.edu      frontendLatency(p->frontend_latency),
605083Sgblack@eecs.umich.edu      forwardLatency(p->forward_latency),
615083Sgblack@eecs.umich.edu      responseLatency(p->response_latency),
625083Sgblack@eecs.umich.edu      width(p->width),
635083Sgblack@eecs.umich.edu      gotAddrRanges(p->port_default_connection_count +
645083Sgblack@eecs.umich.edu                          p->port_master_connection_count, false),
655083Sgblack@eecs.umich.edu      gotAllAddrRanges(false), defaultPortID(InvalidPortID),
665083Sgblack@eecs.umich.edu      useDefaultRange(p->use_default_range)
675083Sgblack@eecs.umich.edu{}
685083Sgblack@eecs.umich.edu
695083Sgblack@eecs.umich.eduBaseXBar::~BaseXBar()
705083Sgblack@eecs.umich.edu{
715083Sgblack@eecs.umich.edu    for (auto m: masterPorts)
725083Sgblack@eecs.umich.edu        delete m;
735083Sgblack@eecs.umich.edu
745083Sgblack@eecs.umich.edu    for (auto s: slavePorts)
755083Sgblack@eecs.umich.edu        delete s;
765083Sgblack@eecs.umich.edu}
775083Sgblack@eecs.umich.edu
785083Sgblack@eecs.umich.eduvoid
795083Sgblack@eecs.umich.eduBaseXBar::init()
805083Sgblack@eecs.umich.edu{
815083Sgblack@eecs.umich.edu}
825083Sgblack@eecs.umich.edu
835083Sgblack@eecs.umich.eduBaseMasterPort &
845083Sgblack@eecs.umich.eduBaseXBar::getMasterPort(const std::string &if_name, PortID idx)
855083Sgblack@eecs.umich.edu{
865083Sgblack@eecs.umich.edu    if (if_name == "master" && idx < masterPorts.size()) {
875083Sgblack@eecs.umich.edu        // the master port index translates directly to the vector position
885083Sgblack@eecs.umich.edu        return *masterPorts[idx];
895083Sgblack@eecs.umich.edu    } else  if (if_name == "default") {
905083Sgblack@eecs.umich.edu        return *masterPorts[defaultPortID];
915083Sgblack@eecs.umich.edu    } else {
925083Sgblack@eecs.umich.edu        return MemObject::getMasterPort(if_name, idx);
935083Sgblack@eecs.umich.edu    }
945083Sgblack@eecs.umich.edu}
955083Sgblack@eecs.umich.edu
965083Sgblack@eecs.umich.eduBaseSlavePort &
975083Sgblack@eecs.umich.eduBaseXBar::getSlavePort(const std::string &if_name, PortID idx)
985083Sgblack@eecs.umich.edu{
995083Sgblack@eecs.umich.edu    if (if_name == "slave" && idx < slavePorts.size()) {
1005083Sgblack@eecs.umich.edu        // the slave port index translates directly to the vector position
1015083Sgblack@eecs.umich.edu        return *slavePorts[idx];
1025083Sgblack@eecs.umich.edu    } else {
1035083Sgblack@eecs.umich.edu        return MemObject::getSlavePort(if_name, idx);
1045083Sgblack@eecs.umich.edu    }
1055083Sgblack@eecs.umich.edu}
1065083Sgblack@eecs.umich.edu
1075083Sgblack@eecs.umich.eduvoid
1085083Sgblack@eecs.umich.eduBaseXBar::calcPacketTiming(PacketPtr pkt, Tick header_delay)
1095083Sgblack@eecs.umich.edu{
1105083Sgblack@eecs.umich.edu    // the crossbar will be called at a time that is not necessarily
1115083Sgblack@eecs.umich.edu    // coinciding with its own clock, so start by determining how long
1125083Sgblack@eecs.umich.edu    // until the next clock edge (could be zero)
1135083Sgblack@eecs.umich.edu    Tick offset = clockEdge() - curTick();
1145083Sgblack@eecs.umich.edu
1155083Sgblack@eecs.umich.edu    // the header delay depends on the path through the crossbar, and
1165083Sgblack@eecs.umich.edu    // we therefore rely on the caller to provide the actual
1175083Sgblack@eecs.umich.edu    // value
1185083Sgblack@eecs.umich.edu    pkt->headerDelay += offset + header_delay;
1195083Sgblack@eecs.umich.edu
1205083Sgblack@eecs.umich.edu    // note that we add the header delay to the existing value, and
1215083Sgblack@eecs.umich.edu    // align it to the crossbar clock
1225083Sgblack@eecs.umich.edu
1235083Sgblack@eecs.umich.edu    // do a quick sanity check to ensure the timings are not being
1245083Sgblack@eecs.umich.edu    // ignored, note that this specific value may cause problems for
1255083Sgblack@eecs.umich.edu    // slower interconnects
1265083Sgblack@eecs.umich.edu    panic_if(pkt->headerDelay > SimClock::Int::us,
1275083Sgblack@eecs.umich.edu             "Encountered header delay exceeding 1 us\n");
1285083Sgblack@eecs.umich.edu
1295083Sgblack@eecs.umich.edu    if (pkt->hasData()) {
1305083Sgblack@eecs.umich.edu        // the payloadDelay takes into account the relative time to
1315083Sgblack@eecs.umich.edu        // deliver the payload of the packet, after the header delay,
1325083Sgblack@eecs.umich.edu        // we take the maximum since the payload delay could already
1335083Sgblack@eecs.umich.edu        // be longer than what this parcitular crossbar enforces.
1345083Sgblack@eecs.umich.edu        pkt->payloadDelay = std::max<Tick>(pkt->payloadDelay,
1355083Sgblack@eecs.umich.edu                                           divCeil(pkt->getSize(), width) *
1365083Sgblack@eecs.umich.edu                                           clockPeriod());
1375083Sgblack@eecs.umich.edu    }
1385083Sgblack@eecs.umich.edu
1395083Sgblack@eecs.umich.edu    // the payload delay is not paying for the clock offset as that is
1405083Sgblack@eecs.umich.edu    // already done using the header delay, and the payload delay is
1415083Sgblack@eecs.umich.edu    // also used to determine how long the crossbar layer is busy and
1425083Sgblack@eecs.umich.edu    // thus regulates throughput
1435083Sgblack@eecs.umich.edu}
1445083Sgblack@eecs.umich.edu
1455083Sgblack@eecs.umich.edutemplate <typename SrcType, typename DstType>
1465083Sgblack@eecs.umich.eduBaseXBar::Layer<SrcType,DstType>::Layer(DstType& _port, BaseXBar& _xbar,
1475083Sgblack@eecs.umich.edu                                       const std::string& _name) :
1485083Sgblack@eecs.umich.edu    port(_port), xbar(_xbar), _name(_name), state(IDLE), drainManager(NULL),
1495083Sgblack@eecs.umich.edu    waitingForPeer(NULL), releaseEvent(this)
1505083Sgblack@eecs.umich.edu{
1515083Sgblack@eecs.umich.edu}
1525083Sgblack@eecs.umich.edu
1535083Sgblack@eecs.umich.edutemplate <typename SrcType, typename DstType>
1545083Sgblack@eecs.umich.eduvoid BaseXBar::Layer<SrcType,DstType>::occupyLayer(Tick until)
1555083Sgblack@eecs.umich.edu{
1565083Sgblack@eecs.umich.edu    // ensure the state is busy at this point, as the layer should
1575083Sgblack@eecs.umich.edu    // transition from idle as soon as it has decided to forward the
1585083Sgblack@eecs.umich.edu    // packet to prevent any follow-on calls to sendTiming seeing an
1595083Sgblack@eecs.umich.edu    // unoccupied layer
1605083Sgblack@eecs.umich.edu    assert(state == BUSY);
1615083Sgblack@eecs.umich.edu
1625083Sgblack@eecs.umich.edu    // until should never be 0 as express snoops never occupy the layer
1635083Sgblack@eecs.umich.edu    assert(until != 0);
1645083Sgblack@eecs.umich.edu    xbar.schedule(releaseEvent, until);
1655083Sgblack@eecs.umich.edu
1665083Sgblack@eecs.umich.edu    // account for the occupied ticks
1675083Sgblack@eecs.umich.edu    occupancy += until - curTick();
1685083Sgblack@eecs.umich.edu
1695083Sgblack@eecs.umich.edu    DPRINTF(BaseXBar, "The crossbar layer is now busy from tick %d to %d\n",
1705083Sgblack@eecs.umich.edu            curTick(), until);
1715083Sgblack@eecs.umich.edu}
1725083Sgblack@eecs.umich.edu
1735083Sgblack@eecs.umich.edutemplate <typename SrcType, typename DstType>
1745083Sgblack@eecs.umich.edubool
1755083Sgblack@eecs.umich.eduBaseXBar::Layer<SrcType,DstType>::tryTiming(SrcType* src_port)
1765083Sgblack@eecs.umich.edu{
1775083Sgblack@eecs.umich.edu    // if we are in the retry state, we will not see anything but the
1785083Sgblack@eecs.umich.edu    // retrying port (or in the case of the snoop ports the snoop
1795083Sgblack@eecs.umich.edu    // response port that mirrors the actual slave port) as we leave
1805083Sgblack@eecs.umich.edu    // this state again in zero time if the peer does not immediately
1815083Sgblack@eecs.umich.edu    // call the layer when receiving the retry
1825083Sgblack@eecs.umich.edu
1835083Sgblack@eecs.umich.edu    // first we see if the layer is busy, next we check if the
1845083Sgblack@eecs.umich.edu    // destination port is already engaged in a transaction waiting
1855083Sgblack@eecs.umich.edu    // for a retry from the peer
1865083Sgblack@eecs.umich.edu    if (state == BUSY || waitingForPeer != NULL) {
1875083Sgblack@eecs.umich.edu        // the port should not be waiting already
1885083Sgblack@eecs.umich.edu        assert(std::find(waitingForLayer.begin(), waitingForLayer.end(),
1895083Sgblack@eecs.umich.edu                         src_port) == waitingForLayer.end());
1905083Sgblack@eecs.umich.edu
1915083Sgblack@eecs.umich.edu        // put the port at the end of the retry list waiting for the
1925083Sgblack@eecs.umich.edu        // layer to be freed up (and in the case of a busy peer, for
1935083Sgblack@eecs.umich.edu        // that transaction to go through, and then the layer to free
1945083Sgblack@eecs.umich.edu        // up)
1955083Sgblack@eecs.umich.edu        waitingForLayer.push_back(src_port);
1965083Sgblack@eecs.umich.edu        return false;
1975083Sgblack@eecs.umich.edu    }
1985083Sgblack@eecs.umich.edu
1995083Sgblack@eecs.umich.edu    state = BUSY;
2005083Sgblack@eecs.umich.edu
2015083Sgblack@eecs.umich.edu    return true;
2025083Sgblack@eecs.umich.edu}
2035083Sgblack@eecs.umich.edu
2045083Sgblack@eecs.umich.edutemplate <typename SrcType, typename DstType>
2055083Sgblack@eecs.umich.eduvoid
2065083Sgblack@eecs.umich.eduBaseXBar::Layer<SrcType,DstType>::succeededTiming(Tick busy_time)
2075083Sgblack@eecs.umich.edu{
2085083Sgblack@eecs.umich.edu    // we should have gone from idle or retry to busy in the tryTiming
2095083Sgblack@eecs.umich.edu    // test
2105083Sgblack@eecs.umich.edu    assert(state == BUSY);
2115083Sgblack@eecs.umich.edu
2125083Sgblack@eecs.umich.edu    // occupy the layer accordingly
2135083Sgblack@eecs.umich.edu    occupyLayer(busy_time);
2145083Sgblack@eecs.umich.edu}
2155083Sgblack@eecs.umich.edu
2165083Sgblack@eecs.umich.edutemplate <typename SrcType, typename DstType>
2175083Sgblack@eecs.umich.eduvoid
2185083Sgblack@eecs.umich.eduBaseXBar::Layer<SrcType,DstType>::failedTiming(SrcType* src_port,
2195083Sgblack@eecs.umich.edu                                              Tick busy_time)
2205083Sgblack@eecs.umich.edu{
2215083Sgblack@eecs.umich.edu    // ensure no one got in between and tried to send something to
2225083Sgblack@eecs.umich.edu    // this port
2235083Sgblack@eecs.umich.edu    assert(waitingForPeer == NULL);
2245083Sgblack@eecs.umich.edu
2255083Sgblack@eecs.umich.edu    // if the source port is the current retrying one or not, we have
2265083Sgblack@eecs.umich.edu    // failed in forwarding and should track that we are now waiting
2275083Sgblack@eecs.umich.edu    // for the peer to send a retry
2285083Sgblack@eecs.umich.edu    waitingForPeer = src_port;
2295083Sgblack@eecs.umich.edu
2305083Sgblack@eecs.umich.edu    // we should have gone from idle or retry to busy in the tryTiming
2315083Sgblack@eecs.umich.edu    // test
2325083Sgblack@eecs.umich.edu    assert(state == BUSY);
2335083Sgblack@eecs.umich.edu
2345083Sgblack@eecs.umich.edu    // occupy the bus accordingly
2355083Sgblack@eecs.umich.edu    occupyLayer(busy_time);
2365083Sgblack@eecs.umich.edu}
2375083Sgblack@eecs.umich.edu
2385083Sgblack@eecs.umich.edutemplate <typename SrcType, typename DstType>
2395083Sgblack@eecs.umich.eduvoid
2405083Sgblack@eecs.umich.eduBaseXBar::Layer<SrcType,DstType>::releaseLayer()
2415083Sgblack@eecs.umich.edu{
2425083Sgblack@eecs.umich.edu    // releasing the bus means we should now be idle
2435083Sgblack@eecs.umich.edu    assert(state == BUSY);
2445083Sgblack@eecs.umich.edu    assert(!releaseEvent.scheduled());
2455083Sgblack@eecs.umich.edu
2465083Sgblack@eecs.umich.edu    // update the state
2475083Sgblack@eecs.umich.edu    state = IDLE;
2485788Sgblack@eecs.umich.edu
2495083Sgblack@eecs.umich.edu    // bus layer is now idle, so if someone is waiting we can retry
2505083Sgblack@eecs.umich.edu    if (!waitingForLayer.empty()) {
2515083Sgblack@eecs.umich.edu        // there is no point in sending a retry if someone is still
2525083Sgblack@eecs.umich.edu        // waiting for the peer
2535083Sgblack@eecs.umich.edu        if (waitingForPeer == NULL)
2545083Sgblack@eecs.umich.edu            retryWaiting();
2555083Sgblack@eecs.umich.edu    } else if (waitingForPeer == NULL && drainManager) {
2565083Sgblack@eecs.umich.edu        DPRINTF(Drain, "Crossbar done draining, signaling drain manager\n");
2575083Sgblack@eecs.umich.edu        //If we weren't able to drain before, do it now.
2585083Sgblack@eecs.umich.edu        drainManager->signalDrainDone();
2595083Sgblack@eecs.umich.edu        // Clear the drain event once we're done with it.
2605083Sgblack@eecs.umich.edu        drainManager = NULL;
2615083Sgblack@eecs.umich.edu    }
2625083Sgblack@eecs.umich.edu}
2635122Sgblack@eecs.umich.edu
2645083Sgblack@eecs.umich.edutemplate <typename SrcType, typename DstType>
2655083Sgblack@eecs.umich.eduvoid
2665083Sgblack@eecs.umich.eduBaseXBar::Layer<SrcType,DstType>::retryWaiting()
2675083Sgblack@eecs.umich.edu{
2685083Sgblack@eecs.umich.edu    // this should never be called with no one waiting
2695083Sgblack@eecs.umich.edu    assert(!waitingForLayer.empty());
2705083Sgblack@eecs.umich.edu
2715083Sgblack@eecs.umich.edu    // we always go to retrying from idle
2725083Sgblack@eecs.umich.edu    assert(state == IDLE);
2735083Sgblack@eecs.umich.edu
2745083Sgblack@eecs.umich.edu    // update the state
2755083Sgblack@eecs.umich.edu    state = RETRY;
2765083Sgblack@eecs.umich.edu
2775083Sgblack@eecs.umich.edu    // set the retrying port to the front of the retry list and pop it
2785083Sgblack@eecs.umich.edu    // off the list
2795083Sgblack@eecs.umich.edu    SrcType* retryingPort = waitingForLayer.front();
2805083Sgblack@eecs.umich.edu    waitingForLayer.pop_front();
2815083Sgblack@eecs.umich.edu
2825083Sgblack@eecs.umich.edu    // tell the port to retry, which in some cases ends up calling the
2835083Sgblack@eecs.umich.edu    // layer again
2845083Sgblack@eecs.umich.edu    sendRetry(retryingPort);
2855083Sgblack@eecs.umich.edu
2865083Sgblack@eecs.umich.edu    // If the layer is still in the retry state, sendTiming wasn't
2875083Sgblack@eecs.umich.edu    // called in zero time (e.g. the cache does this when a writeback
2885083Sgblack@eecs.umich.edu    // is squashed)
2895083Sgblack@eecs.umich.edu    if (state == RETRY) {
2905083Sgblack@eecs.umich.edu        // update the state to busy and reset the retrying port, we
2915083Sgblack@eecs.umich.edu        // have done our bit and sent the retry
2925083Sgblack@eecs.umich.edu        state = BUSY;
2935083Sgblack@eecs.umich.edu
2945083Sgblack@eecs.umich.edu        // occupy the crossbar layer until the next clock edge
2955083Sgblack@eecs.umich.edu        occupyLayer(xbar.clockEdge());
2965083Sgblack@eecs.umich.edu    }
2975083Sgblack@eecs.umich.edu}
2985083Sgblack@eecs.umich.edu
2995083Sgblack@eecs.umich.edutemplate <typename SrcType, typename DstType>
3005083Sgblack@eecs.umich.eduvoid
3015083Sgblack@eecs.umich.eduBaseXBar::Layer<SrcType,DstType>::recvRetry()
3025083Sgblack@eecs.umich.edu{
3035083Sgblack@eecs.umich.edu    // we should never get a retry without having failed to forward
3045083Sgblack@eecs.umich.edu    // something to this port
3055083Sgblack@eecs.umich.edu    assert(waitingForPeer != NULL);
3065083Sgblack@eecs.umich.edu
3075083Sgblack@eecs.umich.edu    // add the port where the failed packet originated to the front of
3085083Sgblack@eecs.umich.edu    // the waiting ports for the layer, this allows us to call retry
3095083Sgblack@eecs.umich.edu    // on the port immediately if the crossbar layer is idle
3105083Sgblack@eecs.umich.edu    waitingForLayer.push_front(waitingForPeer);
3115083Sgblack@eecs.umich.edu
3125083Sgblack@eecs.umich.edu    // we are no longer waiting for the peer
3135083Sgblack@eecs.umich.edu    waitingForPeer = NULL;
3145083Sgblack@eecs.umich.edu
3155083Sgblack@eecs.umich.edu    // if the layer is idle, retry this port straight away, if we
3165083Sgblack@eecs.umich.edu    // are busy, then simply let the port wait for its turn
3175083Sgblack@eecs.umich.edu    if (state == IDLE) {
3185083Sgblack@eecs.umich.edu        retryWaiting();
3195083Sgblack@eecs.umich.edu    } else {
3205083Sgblack@eecs.umich.edu        assert(state == BUSY);
3215083Sgblack@eecs.umich.edu    }
3225083Sgblack@eecs.umich.edu}
3235083Sgblack@eecs.umich.edu
3245083Sgblack@eecs.umich.eduPortID
3255083Sgblack@eecs.umich.eduBaseXBar::findPort(Addr addr)
3265083Sgblack@eecs.umich.edu{
3275083Sgblack@eecs.umich.edu    // we should never see any address lookups before we've got the
3285083Sgblack@eecs.umich.edu    // ranges of all connected slave modules
3295083Sgblack@eecs.umich.edu    assert(gotAllAddrRanges);
3305083Sgblack@eecs.umich.edu
3315083Sgblack@eecs.umich.edu    // Check the cache
3325083Sgblack@eecs.umich.edu    PortID dest_id = checkPortCache(addr);
3335083Sgblack@eecs.umich.edu    if (dest_id != InvalidPortID)
3345083Sgblack@eecs.umich.edu        return dest_id;
3355083Sgblack@eecs.umich.edu
3365083Sgblack@eecs.umich.edu    // Check the address map interval tree
3375083Sgblack@eecs.umich.edu    auto i = portMap.find(addr);
3385083Sgblack@eecs.umich.edu    if (i != portMap.end()) {
3395083Sgblack@eecs.umich.edu        dest_id = i->second;
3405083Sgblack@eecs.umich.edu        updatePortCache(dest_id, i->first);
3415083Sgblack@eecs.umich.edu        return dest_id;
342    }
343
344    // Check if this matches the default range
345    if (useDefaultRange) {
346        if (defaultRange.contains(addr)) {
347            DPRINTF(AddrRanges, "  found addr %#llx on default\n",
348                    addr);
349            return defaultPortID;
350        }
351    } else if (defaultPortID != InvalidPortID) {
352        DPRINTF(AddrRanges, "Unable to find destination for addr %#llx, "
353                "will use default port\n", addr);
354        return defaultPortID;
355    }
356
357    // we should use the range for the default port and it did not
358    // match, or the default port is not set
359    fatal("Unable to find destination for addr %#llx on %s\n", addr,
360          name());
361}
362
363/** Function called by the port when the crossbar is receiving a range change.*/
364void
365BaseXBar::recvRangeChange(PortID master_port_id)
366{
367    DPRINTF(AddrRanges, "Received range change from slave port %s\n",
368            masterPorts[master_port_id]->getSlavePort().name());
369
370    // remember that we got a range from this master port and thus the
371    // connected slave module
372    gotAddrRanges[master_port_id] = true;
373
374    // update the global flag
375    if (!gotAllAddrRanges) {
376        // take a logical AND of all the ports and see if we got
377        // ranges from everyone
378        gotAllAddrRanges = true;
379        std::vector<bool>::const_iterator r = gotAddrRanges.begin();
380        while (gotAllAddrRanges &&  r != gotAddrRanges.end()) {
381            gotAllAddrRanges &= *r++;
382        }
383        if (gotAllAddrRanges)
384            DPRINTF(AddrRanges, "Got address ranges from all slaves\n");
385    }
386
387    // note that we could get the range from the default port at any
388    // point in time, and we cannot assume that the default range is
389    // set before the other ones are, so we do additional checks once
390    // all ranges are provided
391    if (master_port_id == defaultPortID) {
392        // only update if we are indeed checking ranges for the
393        // default port since the port might not have a valid range
394        // otherwise
395        if (useDefaultRange) {
396            AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges();
397
398            if (ranges.size() != 1)
399                fatal("Crossbar %s may only have a single default range",
400                      name());
401
402            defaultRange = ranges.front();
403        }
404    } else {
405        // the ports are allowed to update their address ranges
406        // dynamically, so remove any existing entries
407        if (gotAddrRanges[master_port_id]) {
408            for (auto p = portMap.begin(); p != portMap.end(); ) {
409                if (p->second == master_port_id)
410                    // erasing invalidates the iterator, so advance it
411                    // before the deletion takes place
412                    portMap.erase(p++);
413                else
414                    p++;
415            }
416        }
417
418        AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges();
419
420        for (const auto& r: ranges) {
421            DPRINTF(AddrRanges, "Adding range %s for id %d\n",
422                    r.to_string(), master_port_id);
423            if (portMap.insert(r, master_port_id) == portMap.end()) {
424                PortID conflict_id = portMap.find(r)->second;
425                fatal("%s has two ports responding within range %s:\n\t%s\n\t%s\n",
426                      name(),
427                      r.to_string(),
428                      masterPorts[master_port_id]->getSlavePort().name(),
429                      masterPorts[conflict_id]->getSlavePort().name());
430            }
431        }
432    }
433
434    // if we have received ranges from all our neighbouring slave
435    // modules, go ahead and tell our connected master modules in
436    // turn, this effectively assumes a tree structure of the system
437    if (gotAllAddrRanges) {
438        DPRINTF(AddrRanges, "Aggregating address ranges\n");
439        xbarRanges.clear();
440
441        // start out with the default range
442        if (useDefaultRange) {
443            if (!gotAddrRanges[defaultPortID])
444                fatal("Crossbar %s uses default range, but none provided",
445                      name());
446
447            xbarRanges.push_back(defaultRange);
448            DPRINTF(AddrRanges, "-- Adding default %s\n",
449                    defaultRange.to_string());
450        }
451
452        // merge all interleaved ranges and add any range that is not
453        // a subset of the default range
454        std::vector<AddrRange> intlv_ranges;
455        for (const auto& r: portMap) {
456            // if the range is interleaved then save it for now
457            if (r.first.interleaved()) {
458                // if we already got interleaved ranges that are not
459                // part of the same range, then first do a merge
460                // before we add the new one
461                if (!intlv_ranges.empty() &&
462                    !intlv_ranges.back().mergesWith(r.first)) {
463                    DPRINTF(AddrRanges, "-- Merging range from %d ranges\n",
464                            intlv_ranges.size());
465                    AddrRange merged_range(intlv_ranges);
466                    // next decide if we keep the merged range or not
467                    if (!(useDefaultRange &&
468                          merged_range.isSubset(defaultRange))) {
469                        xbarRanges.push_back(merged_range);
470                        DPRINTF(AddrRanges, "-- Adding merged range %s\n",
471                                merged_range.to_string());
472                    }
473                    intlv_ranges.clear();
474                }
475                intlv_ranges.push_back(r.first);
476            } else {
477                // keep the current range if not a subset of the default
478                if (!(useDefaultRange &&
479                      r.first.isSubset(defaultRange))) {
480                    xbarRanges.push_back(r.first);
481                    DPRINTF(AddrRanges, "-- Adding range %s\n",
482                            r.first.to_string());
483                }
484            }
485        }
486
487        // if there is still interleaved ranges waiting to be merged,
488        // go ahead and do it
489        if (!intlv_ranges.empty()) {
490            DPRINTF(AddrRanges, "-- Merging range from %d ranges\n",
491                    intlv_ranges.size());
492            AddrRange merged_range(intlv_ranges);
493            if (!(useDefaultRange && merged_range.isSubset(defaultRange))) {
494                xbarRanges.push_back(merged_range);
495                DPRINTF(AddrRanges, "-- Adding merged range %s\n",
496                        merged_range.to_string());
497            }
498        }
499
500        // also check that no range partially overlaps with the
501        // default range, this has to be done after all ranges are set
502        // as there are no guarantees for when the default range is
503        // update with respect to the other ones
504        if (useDefaultRange) {
505            for (const auto& r: xbarRanges) {
506                // see if the new range is partially
507                // overlapping the default range
508                if (r.intersects(defaultRange) &&
509                    !r.isSubset(defaultRange))
510                    fatal("Range %s intersects the "                    \
511                          "default range of %s but is not a "           \
512                          "subset\n", r.to_string(), name());
513            }
514        }
515
516        // tell all our neighbouring master ports that our address
517        // ranges have changed
518        for (const auto& s: slavePorts)
519            s->sendRangeChange();
520    }
521
522    clearPortCache();
523}
524
525AddrRangeList
526BaseXBar::getAddrRanges() const
527{
528    // we should never be asked without first having sent a range
529    // change, and the latter is only done once we have all the ranges
530    // of the connected devices
531    assert(gotAllAddrRanges);
532
533    // at the moment, this never happens, as there are no cycles in
534    // the range queries and no devices on the master side of a crossbar
535    // (CPU, cache, bridge etc) actually care about the ranges of the
536    // ports they are connected to
537
538    DPRINTF(AddrRanges, "Received address range request\n");
539
540    return xbarRanges;
541}
542
543void
544BaseXBar::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>
590unsigned int
591BaseXBar::Layer<SrcType,DstType>::drain(DrainManager *dm)
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        drainManager = dm;
599        return 1;
600    }
601    return 0;
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>;
631