coherent_xbar.cc revision 10572
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"
5310405Sandreas.hansson@arm.com#include "debug/CoherentXBar.hh"
5410405Sandreas.hansson@arm.com#include "mem/coherent_xbar.hh"
559524SN/A#include "sim/system.hh"
562497SN/A
5710405Sandreas.hansson@arm.comCoherentXBar::CoherentXBar(const CoherentXBarParams *p)
5810405Sandreas.hansson@arm.com    : BaseXBar(p), system(p->system), snoopFilter(p->snoop_filter)
597523SN/A{
608851SN/A    // create the ports based on the size of the master and slave
618948SN/A    // vector ports, and the presence of the default port, the ports
628948SN/A    // are enumerated starting from zero
638851SN/A    for (int i = 0; i < p->port_master_connection_count; ++i) {
649095SN/A        std::string portName = csprintf("%s.master[%d]", name(), i);
6510405Sandreas.hansson@arm.com        MasterPort* bp = new CoherentXBarMasterPort(portName, *this, i);
668922SN/A        masterPorts.push_back(bp);
679715SN/A        reqLayers.push_back(new ReqLayer(*bp, *this,
689715SN/A                                         csprintf(".reqLayer%d", i)));
699715SN/A        snoopLayers.push_back(new SnoopLayer(*bp, *this,
709715SN/A                                             csprintf(".snoopLayer%d", i)));
718851SN/A    }
728851SN/A
738948SN/A    // see if we have a default slave device connected and if so add
748948SN/A    // our corresponding master port
758915SN/A    if (p->port_default_connection_count) {
769031SN/A        defaultPortID = masterPorts.size();
779095SN/A        std::string portName = name() + ".default";
7810405Sandreas.hansson@arm.com        MasterPort* bp = new CoherentXBarMasterPort(portName, *this,
799036SN/A                                                   defaultPortID);
808922SN/A        masterPorts.push_back(bp);
819715SN/A        reqLayers.push_back(new ReqLayer(*bp, *this, csprintf(".reqLayer%d",
829715SN/A                                             defaultPortID)));
839715SN/A        snoopLayers.push_back(new SnoopLayer(*bp, *this,
849715SN/A                                             csprintf(".snoopLayer%d",
859715SN/A                                                      defaultPortID)));
868915SN/A    }
878915SN/A
888948SN/A    // create the slave ports, once again starting at zero
898851SN/A    for (int i = 0; i < p->port_slave_connection_count; ++i) {
909095SN/A        std::string portName = csprintf("%s.slave[%d]", name(), i);
9110405Sandreas.hansson@arm.com        SlavePort* bp = new CoherentXBarSlavePort(portName, *this, i);
928922SN/A        slavePorts.push_back(bp);
939715SN/A        respLayers.push_back(new RespLayer(*bp, *this,
949715SN/A                                           csprintf(".respLayer%d", i)));
959716SN/A        snoopRespPorts.push_back(new SnoopRespPort(*bp, *this));
968851SN/A    }
978851SN/A
9810402SN/A    if (snoopFilter)
9910402SN/A        snoopFilter->setSlavePorts(slavePorts);
10010402SN/A
1017523SN/A    clearPortCache();
1027523SN/A}
1037523SN/A
10410405Sandreas.hansson@arm.comCoherentXBar::~CoherentXBar()
1059715SN/A{
10610405Sandreas.hansson@arm.com    for (auto l: reqLayers)
10710405Sandreas.hansson@arm.com        delete l;
10810405Sandreas.hansson@arm.com    for (auto l: respLayers)
10910405Sandreas.hansson@arm.com        delete l;
11010405Sandreas.hansson@arm.com    for (auto l: snoopLayers)
11110405Sandreas.hansson@arm.com        delete l;
11210405Sandreas.hansson@arm.com    for (auto p: snoopRespPorts)
11310405Sandreas.hansson@arm.com        delete p;
1149715SN/A}
1159715SN/A
1162568SN/Avoid
11710405Sandreas.hansson@arm.comCoherentXBar::init()
1182568SN/A{
1199278SN/A    // the base class is responsible for determining the block size
12010405Sandreas.hansson@arm.com    BaseXBar::init();
1219278SN/A
1228948SN/A    // iterate over our slave ports and determine which of our
1238948SN/A    // neighbouring master ports are snooping and add them as snoopers
12410405Sandreas.hansson@arm.com    for (const auto& p: slavePorts) {
1259088SN/A        // check if the connected master port is snooping
12610405Sandreas.hansson@arm.com        if (p->isSnooping()) {
12710405Sandreas.hansson@arm.com            DPRINTF(AddrRanges, "Adding snooping master %s\n",
12810405Sandreas.hansson@arm.com                    p->getMasterPort().name());
12910405Sandreas.hansson@arm.com            snoopPorts.push_back(p);
1308711SN/A        }
1318711SN/A    }
1322568SN/A
1339036SN/A    if (snoopPorts.empty())
13410405Sandreas.hansson@arm.com        warn("CoherentXBar %s has no snooping ports attached!\n", name());
1353244SN/A}
1363244SN/A
1378948SN/Abool
13810405Sandreas.hansson@arm.comCoherentXBar::recvTimingReq(PacketPtr pkt, PortID slave_port_id)
1393244SN/A{
1408975SN/A    // determine the source port based on the id
1419032SN/A    SlavePort *src_port = slavePorts[slave_port_id];
1423244SN/A
1439091SN/A    // remember if the packet is an express snoop
1449091SN/A    bool is_express_snoop = pkt->isExpressSnoop();
1459091SN/A
1469612SN/A    // determine the destination based on the address
1479712SN/A    PortID master_port_id = findPort(pkt->getAddr());
1489612SN/A
14910405Sandreas.hansson@arm.com    // test if the crossbar should be considered occupied for the current
1509033SN/A    // port, and exclude express snoops from the check
1519715SN/A    if (!is_express_snoop && !reqLayers[master_port_id]->tryTiming(src_port)) {
15210405Sandreas.hansson@arm.com        DPRINTF(CoherentXBar, "recvTimingReq: src %s %s 0x%x BUSY\n",
1538949SN/A                src_port->name(), pkt->cmdString(), pkt->getAddr());
1543244SN/A        return false;
1553244SN/A    }
1563244SN/A
15710405Sandreas.hansson@arm.com    DPRINTF(CoherentXBar, "recvTimingReq: src %s %s expr %d 0x%x\n",
1589091SN/A            src_port->name(), pkt->cmdString(), is_express_snoop,
1599091SN/A            pkt->getAddr());
1605197SN/A
1619712SN/A    // store size and command as they might be modified when
1629712SN/A    // forwarding the packet
1639712SN/A    unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
1649712SN/A    unsigned int pkt_cmd = pkt->cmdToIndex();
1659712SN/A
1669032SN/A    // set the source port for routing of the response
1679032SN/A    pkt->setSrc(slave_port_id);
1689032SN/A
1699547SN/A    calcPacketTiming(pkt);
17010405Sandreas.hansson@arm.com    Tick packetFinishTime = pkt->lastWordDelay + curTick();
1714912SN/A
1728979SN/A    // uncacheable requests need never be snooped
1739524SN/A    if (!pkt->req->isUncacheable() && !system->bypassCaches()) {
1748979SN/A        // the packet is a memory-mapped request and should be
1758979SN/A        // broadcasted to our snoopers but the source
17610402SN/A        if (snoopFilter) {
17710402SN/A            // check with the snoop filter where to forward this packet
17810402SN/A            auto sf_res = snoopFilter->lookupRequest(pkt, *src_port);
17910402SN/A            packetFinishTime += sf_res.second * clockPeriod();
18010405Sandreas.hansson@arm.com            DPRINTF(CoherentXBar, "recvTimingReq: src %s %s 0x%x"\
18110402SN/A                    " SF size: %i lat: %i\n", src_port->name(),
18210402SN/A                    pkt->cmdString(), pkt->getAddr(), sf_res.first.size(),
18310402SN/A                    sf_res.second);
18410402SN/A            forwardTiming(pkt, slave_port_id, sf_res.first);
18510402SN/A        } else {
18610402SN/A            forwardTiming(pkt, slave_port_id);
18710402SN/A        }
1888979SN/A    }
1898948SN/A
1908975SN/A    // remember if we add an outstanding req so we can undo it if
1918975SN/A    // necessary, if the packet needs a response, we should add it
1928975SN/A    // as outstanding and express snoops never fail so there is
1938975SN/A    // not need to worry about them
1949091SN/A    bool add_outstanding = !is_express_snoop && pkt->needsResponse();
1958948SN/A
1968975SN/A    // keep track that we have an outstanding request packet
1978975SN/A    // matching this request, this is used by the coherency
1988975SN/A    // mechanism in determining what to do with snoop responses
1998975SN/A    // (in recvTimingSnoop)
2008975SN/A    if (add_outstanding) {
2018975SN/A        // we should never have an exsiting request outstanding
2028975SN/A        assert(outstandingReq.find(pkt->req) == outstandingReq.end());
2038975SN/A        outstandingReq.insert(pkt->req);
2048975SN/A    }
2058915SN/A
20610402SN/A    // Note: Cannot create a copy of the full packet, here.
20710402SN/A    MemCmd orig_cmd(pkt->cmd);
20810402SN/A
2099612SN/A    // since it is a normal request, attempt to send the packet
2109712SN/A    bool success = masterPorts[master_port_id]->sendTimingReq(pkt);
2118948SN/A
21210402SN/A    if (snoopFilter && !pkt->req->isUncacheable()
21310402SN/A        && !system->bypassCaches()) {
21410402SN/A        // The packet may already be overwritten by the sendTimingReq function.
21510402SN/A        // The snoop filter needs to see the original request *and* the return
21610402SN/A        // status of the send operation, so we need to recreate the original
21710402SN/A        // request.  Atomic mode does not have the issue, as there the send
21810402SN/A        // operation and the response happen instantaneously and don't need two
21910402SN/A        // phase tracking.
22010402SN/A        MemCmd tmp_cmd(pkt->cmd);
22110402SN/A        pkt->cmd = orig_cmd;
22210402SN/A        // Let the snoop filter know about the success of the send operation
22310402SN/A        snoopFilter->updateRequest(pkt, *src_port, !success);
22410402SN/A        pkt->cmd = tmp_cmd;
22510402SN/A    }
22610402SN/A
2279091SN/A    // if this is an express snoop, we are done at this point
2289091SN/A    if (is_express_snoop) {
2299091SN/A        assert(success);
23010405Sandreas.hansson@arm.com        snoops++;
2319091SN/A    } else {
2329091SN/A        // for normal requests, check if successful
2339091SN/A        if (!success)  {
2349091SN/A            // inhibited packets should never be forced to retry
2359091SN/A            assert(!pkt->memInhibitAsserted());
2368948SN/A
2379091SN/A            // if it was added as outstanding and the send failed, then
2389091SN/A            // erase it again
2399091SN/A            if (add_outstanding)
2409091SN/A                outstandingReq.erase(pkt->req);
2418948SN/A
2429549SN/A            // undo the calculation so we can check for 0 again
24310405Sandreas.hansson@arm.com            pkt->firstWordDelay = pkt->lastWordDelay = 0;
2449549SN/A
24510405Sandreas.hansson@arm.com            DPRINTF(CoherentXBar, "recvTimingReq: src %s %s 0x%x RETRY\n",
2469091SN/A                    src_port->name(), pkt->cmdString(), pkt->getAddr());
2478948SN/A
24810405Sandreas.hansson@arm.com            // update the layer state and schedule an idle event
2499715SN/A            reqLayers[master_port_id]->failedTiming(src_port,
2509715SN/A                                                    clockEdge(headerCycles));
2519091SN/A        } else {
25210405Sandreas.hansson@arm.com            // update the layer state and schedule an idle event
2539715SN/A            reqLayers[master_port_id]->succeededTiming(packetFinishTime);
2549091SN/A        }
2558975SN/A    }
2568975SN/A
2579712SN/A    // stats updates only consider packets that were successfully sent
2589712SN/A    if (success) {
2599712SN/A        pktCount[slave_port_id][master_port_id]++;
26010405Sandreas.hansson@arm.com        pktSize[slave_port_id][master_port_id] += pkt_size;
2619712SN/A        transDist[pkt_cmd]++;
2629712SN/A    }
2639712SN/A
2649091SN/A    return success;
2658975SN/A}
2668975SN/A
2678975SN/Abool
26810405Sandreas.hansson@arm.comCoherentXBar::recvTimingResp(PacketPtr pkt, PortID master_port_id)
2698975SN/A{
2708975SN/A    // determine the source port based on the id
2719032SN/A    MasterPort *src_port = masterPorts[master_port_id];
2728975SN/A
2739713SN/A    // determine the destination based on what is stored in the packet
2749713SN/A    PortID slave_port_id = pkt->getDest();
27510572Sandreas.hansson@arm.com    assert(slave_port_id != InvalidPortID);
27610572Sandreas.hansson@arm.com    assert(slave_port_id < respLayers.size());
2779713SN/A
27810405Sandreas.hansson@arm.com    // test if the crossbar should be considered occupied for the
27910405Sandreas.hansson@arm.com    // current port
2809715SN/A    if (!respLayers[slave_port_id]->tryTiming(src_port)) {
28110405Sandreas.hansson@arm.com        DPRINTF(CoherentXBar, "recvTimingResp: src %s %s 0x%x BUSY\n",
2828975SN/A                src_port->name(), pkt->cmdString(), pkt->getAddr());
2838975SN/A        return false;
2848975SN/A    }
2858975SN/A
28610405Sandreas.hansson@arm.com    DPRINTF(CoherentXBar, "recvTimingResp: src %s %s 0x%x\n",
2878975SN/A            src_port->name(), pkt->cmdString(), pkt->getAddr());
2888975SN/A
2899712SN/A    // store size and command as they might be modified when
2909712SN/A    // forwarding the packet
2919712SN/A    unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
2929712SN/A    unsigned int pkt_cmd = pkt->cmdToIndex();
2939712SN/A
2948975SN/A    calcPacketTiming(pkt);
29510405Sandreas.hansson@arm.com    Tick packetFinishTime = pkt->lastWordDelay + curTick();
2968975SN/A
2978975SN/A    // the packet is a normal response to a request that we should
29810405Sandreas.hansson@arm.com    // have seen passing through the crossbar
2998975SN/A    assert(outstandingReq.find(pkt->req) != outstandingReq.end());
3008975SN/A
30110402SN/A    if (snoopFilter && !pkt->req->isUncacheable() && !system->bypassCaches()) {
30210402SN/A        // let the snoop filter inspect the response and update its state
30310402SN/A        snoopFilter->updateResponse(pkt, *slavePorts[slave_port_id]);
30410402SN/A    }
30510402SN/A
3068975SN/A    // remove it as outstanding
3078975SN/A    outstandingReq.erase(pkt->req);
3088975SN/A
3099712SN/A    // send the packet through the destination slave port
3109712SN/A    bool success M5_VAR_USED = slavePorts[slave_port_id]->sendTimingResp(pkt);
3118975SN/A
3128975SN/A    // currently it is illegal to block responses... can lead to
3138975SN/A    // deadlock
3148975SN/A    assert(success);
3158975SN/A
3169715SN/A    respLayers[slave_port_id]->succeededTiming(packetFinishTime);
3178975SN/A
3189712SN/A    // stats updates
3199712SN/A    pktCount[slave_port_id][master_port_id]++;
32010405Sandreas.hansson@arm.com    pktSize[slave_port_id][master_port_id] += pkt_size;
3219712SN/A    transDist[pkt_cmd]++;
3229712SN/A
3238975SN/A    return true;
3248975SN/A}
3258975SN/A
3268975SN/Avoid
32710405Sandreas.hansson@arm.comCoherentXBar::recvTimingSnoopReq(PacketPtr pkt, PortID master_port_id)
3288975SN/A{
32910405Sandreas.hansson@arm.com    DPRINTF(CoherentXBar, "recvTimingSnoopReq: src %s %s 0x%x\n",
3309032SN/A            masterPorts[master_port_id]->name(), pkt->cmdString(),
3318975SN/A            pkt->getAddr());
3328975SN/A
3339712SN/A    // update stats here as we know the forwarding will succeed
3349712SN/A    transDist[pkt->cmdToIndex()]++;
33510405Sandreas.hansson@arm.com    snoops++;
3369712SN/A
3378975SN/A    // we should only see express snoops from caches
3388975SN/A    assert(pkt->isExpressSnoop());
3398975SN/A
3409032SN/A    // set the source port for routing of the response
3419032SN/A    pkt->setSrc(master_port_id);
3429032SN/A
34310402SN/A    if (snoopFilter) {
34410402SN/A        // let the Snoop Filter work its magic and guide probing
34510402SN/A        auto sf_res = snoopFilter->lookupSnoop(pkt);
34610402SN/A        // No timing here: packetFinishTime += sf_res.second * clockPeriod();
34710405Sandreas.hansson@arm.com        DPRINTF(CoherentXBar, "recvTimingSnoopReq: src %s %s 0x%x"\
34810402SN/A                " SF size: %i lat: %i\n", masterPorts[master_port_id]->name(),
34910402SN/A                pkt->cmdString(), pkt->getAddr(), sf_res.first.size(),
35010402SN/A                sf_res.second);
35110402SN/A
35210402SN/A        // forward to all snoopers
35310402SN/A        forwardTiming(pkt, InvalidPortID, sf_res.first);
35410402SN/A    } else {
35510402SN/A        forwardTiming(pkt, InvalidPortID);
35610402SN/A    }
3578975SN/A
3588975SN/A    // a snoop request came from a connected slave device (one of
3598975SN/A    // our master ports), and if it is not coming from the slave
3608975SN/A    // device responsible for the address range something is
3618975SN/A    // wrong, hence there is nothing further to do as the packet
3628975SN/A    // would be going back to where it came from
3639032SN/A    assert(master_port_id == findPort(pkt->getAddr()));
3648975SN/A}
3658975SN/A
3668975SN/Abool
36710405Sandreas.hansson@arm.comCoherentXBar::recvTimingSnoopResp(PacketPtr pkt, PortID slave_port_id)
3688975SN/A{
3698975SN/A    // determine the source port based on the id
3709032SN/A    SlavePort* src_port = slavePorts[slave_port_id];
3718975SN/A
3729714SN/A    // get the destination from the packet
3739714SN/A    PortID dest_port_id = pkt->getDest();
37410572Sandreas.hansson@arm.com    assert(dest_port_id != InvalidPortID);
3759714SN/A
3769714SN/A    // determine if the response is from a snoop request we
3779714SN/A    // created as the result of a normal request (in which case it
3789714SN/A    // should be in the outstandingReq), or if we merely forwarded
3799714SN/A    // someone else's snoop request
3809714SN/A    bool forwardAsSnoop = outstandingReq.find(pkt->req) ==
3819714SN/A        outstandingReq.end();
3829714SN/A
38310405Sandreas.hansson@arm.com    // test if the crossbar should be considered occupied for the
38410405Sandreas.hansson@arm.com    // current port, note that the check is bypassed if the response
38510405Sandreas.hansson@arm.com    // is being passed on as a normal response since this is occupying
38610405Sandreas.hansson@arm.com    // the response layer rather than the snoop response layer
3879715SN/A    if (forwardAsSnoop) {
38810572Sandreas.hansson@arm.com        assert(dest_port_id < snoopLayers.size());
3899715SN/A        if (!snoopLayers[dest_port_id]->tryTiming(src_port)) {
39010405Sandreas.hansson@arm.com            DPRINTF(CoherentXBar, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n",
3919715SN/A                    src_port->name(), pkt->cmdString(), pkt->getAddr());
3929715SN/A            return false;
3939715SN/A        }
3949716SN/A    } else {
3959716SN/A        // get the master port that mirrors this slave port internally
3969716SN/A        MasterPort* snoop_port = snoopRespPorts[slave_port_id];
39710572Sandreas.hansson@arm.com        assert(dest_port_id < respLayers.size());
3989716SN/A        if (!respLayers[dest_port_id]->tryTiming(snoop_port)) {
39910405Sandreas.hansson@arm.com            DPRINTF(CoherentXBar, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n",
4009716SN/A                    snoop_port->name(), pkt->cmdString(), pkt->getAddr());
4019716SN/A            return false;
4029716SN/A        }
4038975SN/A    }
4048975SN/A
40510405Sandreas.hansson@arm.com    DPRINTF(CoherentXBar, "recvTimingSnoopResp: src %s %s 0x%x\n",
4068975SN/A            src_port->name(), pkt->cmdString(), pkt->getAddr());
4078975SN/A
4089712SN/A    // store size and command as they might be modified when
4099712SN/A    // forwarding the packet
4109712SN/A    unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
4119712SN/A    unsigned int pkt_cmd = pkt->cmdToIndex();
4129712SN/A
4138975SN/A    // responses are never express snoops
4148975SN/A    assert(!pkt->isExpressSnoop());
4158975SN/A
4168975SN/A    calcPacketTiming(pkt);
41710405Sandreas.hansson@arm.com    Tick packetFinishTime = pkt->lastWordDelay + curTick();
4188975SN/A
4199714SN/A    // forward it either as a snoop response or a normal response
4209714SN/A    if (forwardAsSnoop) {
4219714SN/A        // this is a snoop response to a snoop request we forwarded,
4229714SN/A        // e.g. coming from the L1 and going to the L2, and it should
4239714SN/A        // be forwarded as a snoop response
42410402SN/A
42510402SN/A        if (snoopFilter) {
42610402SN/A            // update the probe filter so that it can properly track the line
42710402SN/A            snoopFilter->updateSnoopForward(pkt, *slavePorts[slave_port_id],
42810402SN/A                                            *masterPorts[dest_port_id]);
42910402SN/A        }
43010402SN/A
4319712SN/A        bool success M5_VAR_USED =
4329712SN/A            masterPorts[dest_port_id]->sendTimingSnoopResp(pkt);
4339712SN/A        pktCount[slave_port_id][dest_port_id]++;
43410405Sandreas.hansson@arm.com        pktSize[slave_port_id][dest_port_id] += pkt_size;
4358975SN/A        assert(success);
4369714SN/A
4379715SN/A        snoopLayers[dest_port_id]->succeededTiming(packetFinishTime);
4383244SN/A    } else {
4398975SN/A        // we got a snoop response on one of our slave ports,
44010405Sandreas.hansson@arm.com        // i.e. from a coherent master connected to the crossbar, and
44110405Sandreas.hansson@arm.com        // since we created the snoop request as part of recvTiming,
44210405Sandreas.hansson@arm.com        // this should now be a normal response again
4438948SN/A        outstandingReq.erase(pkt->req);
4448948SN/A
4458975SN/A        // this is a snoop response from a coherent master, with a
44610405Sandreas.hansson@arm.com        // destination field set on its way through the crossbar as
44710405Sandreas.hansson@arm.com        // request, hence it should never go back to where the snoop
44810405Sandreas.hansson@arm.com        // response came from, but instead to where the original
44910405Sandreas.hansson@arm.com        // request came from
4509712SN/A        assert(slave_port_id != dest_port_id);
4518948SN/A
45210402SN/A        if (snoopFilter) {
45310402SN/A            // update the probe filter so that it can properly track the line
45410402SN/A            snoopFilter->updateSnoopResponse(pkt, *slavePorts[slave_port_id],
45510402SN/A                                    *slavePorts[dest_port_id]);
45610402SN/A        }
45710402SN/A
45810405Sandreas.hansson@arm.com        DPRINTF(CoherentXBar, "recvTimingSnoopResp: src %s %s 0x%x"\
45910402SN/A                " FWD RESP\n", src_port->name(), pkt->cmdString(),
46010402SN/A                pkt->getAddr());
46110402SN/A
4629714SN/A        // as a normal response, it should go back to a master through
4639714SN/A        // one of our slave ports, at this point we are ignoring the
4649714SN/A        // fact that the response layer could be busy and do not touch
4659714SN/A        // its state
4669712SN/A        bool success M5_VAR_USED =
4679712SN/A            slavePorts[dest_port_id]->sendTimingResp(pkt);
4688975SN/A
4699714SN/A        // @todo Put the response in an internal FIFO and pass it on
4709714SN/A        // to the response layer from there
4719714SN/A
4728975SN/A        // currently it is illegal to block responses... can lead
4738975SN/A        // to deadlock
4748948SN/A        assert(success);
4759716SN/A
4769716SN/A        respLayers[dest_port_id]->succeededTiming(packetFinishTime);
4773244SN/A    }
4783244SN/A
4799712SN/A    // stats updates
4809712SN/A    transDist[pkt_cmd]++;
48110405Sandreas.hansson@arm.com    snoops++;
4829712SN/A
4838948SN/A    return true;
4848948SN/A}
4858948SN/A
4863210SN/A
4878948SN/Avoid
48810405Sandreas.hansson@arm.comCoherentXBar::forwardTiming(PacketPtr pkt, PortID exclude_slave_port_id,
48910402SN/A                           const std::vector<SlavePort*>& dests)
4908948SN/A{
49110405Sandreas.hansson@arm.com    DPRINTF(CoherentXBar, "%s for %s address %x size %d\n", __func__,
4929663SN/A            pkt->cmdString(), pkt->getAddr(), pkt->getSize());
4939663SN/A
4949524SN/A    // snoops should only happen if the system isn't bypassing caches
4959524SN/A    assert(!system->bypassCaches());
4969524SN/A
49710401SN/A    unsigned fanout = 0;
49810401SN/A
49910405Sandreas.hansson@arm.com    for (const auto& p: dests) {
5008948SN/A        // we could have gotten this request from a snooping master
5018948SN/A        // (corresponding to our own slave port that is also in
5028948SN/A        // snoopPorts) and should not send it back to where it came
5038948SN/A        // from
5049031SN/A        if (exclude_slave_port_id == InvalidPortID ||
5058948SN/A            p->getId() != exclude_slave_port_id) {
5068948SN/A            // cache is not allowed to refuse snoop
5078975SN/A            p->sendTimingSnoopReq(pkt);
50810401SN/A            fanout++;
5098948SN/A        }
5108948SN/A    }
51110401SN/A
51210401SN/A    // Stats for fanout of this forward operation
51310401SN/A    snoopFanout.sample(fanout);
5142497SN/A}
5152497SN/A
5169092SN/Avoid
51710405Sandreas.hansson@arm.comCoherentXBar::recvRetry(PortID master_port_id)
5189092SN/A{
5199093SN/A    // responses and snoop responses never block on forwarding them,
5209093SN/A    // so the retry will always be coming from a port to which we
5219093SN/A    // tried to forward a request
5229715SN/A    reqLayers[master_port_id]->recvRetry();
5239092SN/A}
5249092SN/A
5259036SN/ATick
52610405Sandreas.hansson@arm.comCoherentXBar::recvAtomic(PacketPtr pkt, PortID slave_port_id)
5272657SN/A{
52810405Sandreas.hansson@arm.com    DPRINTF(CoherentXBar, "recvAtomic: packet src %s addr 0x%x cmd %s\n",
5299032SN/A            slavePorts[slave_port_id]->name(), pkt->getAddr(),
5308949SN/A            pkt->cmdString());
5318915SN/A
53210405Sandreas.hansson@arm.com    unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
53310405Sandreas.hansson@arm.com    unsigned int pkt_cmd = pkt->cmdToIndex();
5349712SN/A
5358979SN/A    MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
5368979SN/A    Tick snoop_response_latency = 0;
5378979SN/A
5388979SN/A    // uncacheable requests need never be snooped
5399524SN/A    if (!pkt->req->isUncacheable() && !system->bypassCaches()) {
5408979SN/A        // forward to all snoopers but the source
54110402SN/A        std::pair<MemCmd, Tick> snoop_result;
54210402SN/A        if (snoopFilter) {
54310402SN/A            // check with the snoop filter where to forward this packet
54410402SN/A            auto sf_res =
54510402SN/A                snoopFilter->lookupRequest(pkt, *slavePorts[slave_port_id]);
54610402SN/A            snoop_response_latency += sf_res.second * clockPeriod();
54710405Sandreas.hansson@arm.com            DPRINTF(CoherentXBar, "%s: src %s %s 0x%x"\
54810402SN/A                    " SF size: %i lat: %i\n", __func__,
54910402SN/A                    slavePorts[slave_port_id]->name(), pkt->cmdString(),
55010402SN/A                    pkt->getAddr(), sf_res.first.size(), sf_res.second);
55110402SN/A            snoop_result = forwardAtomic(pkt, slave_port_id, InvalidPortID,
55210402SN/A                                         sf_res.first);
55310402SN/A        } else {
55410402SN/A            snoop_result = forwardAtomic(pkt, slave_port_id);
55510402SN/A        }
5568979SN/A        snoop_response_cmd = snoop_result.first;
55710402SN/A        snoop_response_latency += snoop_result.second;
5588979SN/A    }
5598915SN/A
5608948SN/A    // even if we had a snoop response, we must continue and also
5618948SN/A    // perform the actual request at the destination
56210405Sandreas.hansson@arm.com    PortID master_port_id = findPort(pkt->getAddr());
56310405Sandreas.hansson@arm.com
56410405Sandreas.hansson@arm.com    // stats updates for the request
56510405Sandreas.hansson@arm.com    pktCount[slave_port_id][master_port_id]++;
56610405Sandreas.hansson@arm.com    pktSize[slave_port_id][master_port_id] += pkt_size;
56710405Sandreas.hansson@arm.com    transDist[pkt_cmd]++;
5688948SN/A
5698948SN/A    // forward the request to the appropriate destination
57010405Sandreas.hansson@arm.com    Tick response_latency = masterPorts[master_port_id]->sendAtomic(pkt);
5718948SN/A
57210402SN/A    // Lower levels have replied, tell the snoop filter
57310402SN/A    if (snoopFilter && !pkt->req->isUncacheable() && !system->bypassCaches() &&
57410402SN/A        pkt->isResponse()) {
57510402SN/A        snoopFilter->updateResponse(pkt, *slavePorts[slave_port_id]);
57610402SN/A    }
57710402SN/A
5788948SN/A    // if we got a response from a snooper, restore it here
5798948SN/A    if (snoop_response_cmd != MemCmd::InvalidCmd) {
5808948SN/A        // no one else should have responded
5818948SN/A        assert(!pkt->isResponse());
5828948SN/A        pkt->cmd = snoop_response_cmd;
5838948SN/A        response_latency = snoop_response_latency;
5848948SN/A    }
5858948SN/A
5869712SN/A    // add the response data
58710405Sandreas.hansson@arm.com    if (pkt->isResponse()) {
58810405Sandreas.hansson@arm.com        pkt_size = pkt->hasData() ? pkt->getSize() : 0;
58910405Sandreas.hansson@arm.com        pkt_cmd = pkt->cmdToIndex();
59010405Sandreas.hansson@arm.com
59110405Sandreas.hansson@arm.com        // stats updates
59210405Sandreas.hansson@arm.com        pktCount[slave_port_id][master_port_id]++;
59310405Sandreas.hansson@arm.com        pktSize[slave_port_id][master_port_id] += pkt_size;
59410405Sandreas.hansson@arm.com        transDist[pkt_cmd]++;
59510405Sandreas.hansson@arm.com    }
5969712SN/A
5979547SN/A    // @todo: Not setting first-word time
59810405Sandreas.hansson@arm.com    pkt->lastWordDelay = response_latency;
5998948SN/A    return response_latency;
6008948SN/A}
6018948SN/A
6028948SN/ATick
60310405Sandreas.hansson@arm.comCoherentXBar::recvAtomicSnoop(PacketPtr pkt, PortID master_port_id)
6048948SN/A{
60510405Sandreas.hansson@arm.com    DPRINTF(CoherentXBar, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n",
6069032SN/A            masterPorts[master_port_id]->name(), pkt->getAddr(),
6078949SN/A            pkt->cmdString());
6088948SN/A
6099712SN/A    // add the request snoop data
61010405Sandreas.hansson@arm.com    snoops++;
6119712SN/A
6128948SN/A    // forward to all snoopers
61310402SN/A    std::pair<MemCmd, Tick> snoop_result;
61410402SN/A    Tick snoop_response_latency = 0;
61510402SN/A    if (snoopFilter) {
61610402SN/A        auto sf_res = snoopFilter->lookupSnoop(pkt);
61710402SN/A        snoop_response_latency += sf_res.second * clockPeriod();
61810405Sandreas.hansson@arm.com        DPRINTF(CoherentXBar, "%s: src %s %s 0x%x SF size: %i lat: %i\n",
61910402SN/A                __func__, masterPorts[master_port_id]->name(), pkt->cmdString(),
62010402SN/A                pkt->getAddr(), sf_res.first.size(), sf_res.second);
62110402SN/A        snoop_result = forwardAtomic(pkt, InvalidPortID, master_port_id,
62210402SN/A                                     sf_res.first);
62310402SN/A    } else {
62410402SN/A        snoop_result = forwardAtomic(pkt, InvalidPortID);
62510402SN/A    }
6268948SN/A    MemCmd snoop_response_cmd = snoop_result.first;
62710402SN/A    snoop_response_latency += snoop_result.second;
6288948SN/A
6298948SN/A    if (snoop_response_cmd != MemCmd::InvalidCmd)
6308948SN/A        pkt->cmd = snoop_response_cmd;
6318948SN/A
6329712SN/A    // add the response snoop data
63310401SN/A    if (pkt->isResponse()) {
63410405Sandreas.hansson@arm.com        snoops++;
63510401SN/A    }
6369712SN/A
6379547SN/A    // @todo: Not setting first-word time
63810405Sandreas.hansson@arm.com    pkt->lastWordDelay = snoop_response_latency;
6398948SN/A    return snoop_response_latency;
6408948SN/A}
6418948SN/A
6428948SN/Astd::pair<MemCmd, Tick>
64310405Sandreas.hansson@arm.comCoherentXBar::forwardAtomic(PacketPtr pkt, PortID exclude_slave_port_id,
64410402SN/A                           PortID source_master_port_id,
64510402SN/A                           const std::vector<SlavePort*>& dests)
6468948SN/A{
6479032SN/A    // the packet may be changed on snoops, record the original
6489032SN/A    // command to enable us to restore it between snoops so that
6498948SN/A    // additional snoops can take place properly
6504626SN/A    MemCmd orig_cmd = pkt->cmd;
6514879SN/A    MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
6524879SN/A    Tick snoop_response_latency = 0;
6533662SN/A
6549524SN/A    // snoops should only happen if the system isn't bypassing caches
6559524SN/A    assert(!system->bypassCaches());
6569524SN/A
65710401SN/A    unsigned fanout = 0;
65810401SN/A
65910405Sandreas.hansson@arm.com    for (const auto& p: dests) {
6608915SN/A        // we could have gotten this request from a snooping master
6618915SN/A        // (corresponding to our own slave port that is also in
6628915SN/A        // snoopPorts) and should not send it back to where it came
6638915SN/A        // from
66410402SN/A        if (exclude_slave_port_id != InvalidPortID &&
66510402SN/A            p->getId() == exclude_slave_port_id)
66610402SN/A            continue;
66710401SN/A
66810402SN/A        Tick latency = p->sendAtomicSnoop(pkt);
66910402SN/A        fanout++;
67010402SN/A
67110402SN/A        // in contrast to a functional access, we have to keep on
67210402SN/A        // going as all snoopers must be updated even if we get a
67310402SN/A        // response
67410402SN/A        if (!pkt->isResponse())
67510402SN/A            continue;
67610402SN/A
67710402SN/A        // response from snoop agent
67810402SN/A        assert(pkt->cmd != orig_cmd);
67910402SN/A        assert(pkt->memInhibitAsserted());
68010402SN/A        // should only happen once
68110402SN/A        assert(snoop_response_cmd == MemCmd::InvalidCmd);
68210402SN/A        // save response state
68310402SN/A        snoop_response_cmd = pkt->cmd;
68410402SN/A        snoop_response_latency = latency;
68510402SN/A
68610402SN/A        if (snoopFilter) {
68710402SN/A            // Handle responses by the snoopers and differentiate between
68810402SN/A            // responses to requests from above and snoops from below
68910402SN/A            if (source_master_port_id != InvalidPortID) {
69010402SN/A                // Getting a response for a snoop from below
69110402SN/A                assert(exclude_slave_port_id == InvalidPortID);
69210402SN/A                snoopFilter->updateSnoopForward(pkt, *p,
69310402SN/A                             *masterPorts[source_master_port_id]);
69410402SN/A            } else {
69510402SN/A                // Getting a response for a request from above
69610402SN/A                assert(source_master_port_id == InvalidPortID);
69710402SN/A                snoopFilter->updateSnoopResponse(pkt, *p,
69810402SN/A                             *slavePorts[exclude_slave_port_id]);
6994626SN/A            }
7004626SN/A        }
70110402SN/A        // restore original packet state for remaining snoopers
70210402SN/A        pkt->cmd = orig_cmd;
7034626SN/A    }
7044626SN/A
70510401SN/A    // Stats for fanout
70610401SN/A    snoopFanout.sample(fanout);
70710401SN/A
7088948SN/A    // the packet is restored as part of the loop and any potential
7098948SN/A    // snoop response is part of the returned pair
7108948SN/A    return std::make_pair(snoop_response_cmd, snoop_response_latency);
7112497SN/A}
7122497SN/A
7132497SN/Avoid
71410405Sandreas.hansson@arm.comCoherentXBar::recvFunctional(PacketPtr pkt, PortID slave_port_id)
7152497SN/A{
7168663SN/A    if (!pkt->isPrint()) {
7178663SN/A        // don't do DPRINTFs on PrintReq as it clutters up the output
71810405Sandreas.hansson@arm.com        DPRINTF(CoherentXBar,
7198949SN/A                "recvFunctional: packet src %s addr 0x%x cmd %s\n",
7209032SN/A                slavePorts[slave_port_id]->name(), pkt->getAddr(),
7218663SN/A                pkt->cmdString());
7228663SN/A    }
7238663SN/A
7248979SN/A    // uncacheable requests need never be snooped
7259524SN/A    if (!pkt->req->isUncacheable() && !system->bypassCaches()) {
7268979SN/A        // forward to all snoopers but the source
7279032SN/A        forwardFunctional(pkt, slave_port_id);
7288979SN/A    }
7294912SN/A
7308948SN/A    // there is no need to continue if the snooping has found what we
7318948SN/A    // were looking for and the packet is already a response
7328948SN/A    if (!pkt->isResponse()) {
7339031SN/A        PortID dest_id = findPort(pkt->getAddr());
7348948SN/A
7358948SN/A        masterPorts[dest_id]->sendFunctional(pkt);
7368948SN/A    }
7378948SN/A}
7388948SN/A
7398948SN/Avoid
74010405Sandreas.hansson@arm.comCoherentXBar::recvFunctionalSnoop(PacketPtr pkt, PortID master_port_id)
7418948SN/A{
7428948SN/A    if (!pkt->isPrint()) {
7438948SN/A        // don't do DPRINTFs on PrintReq as it clutters up the output
74410405Sandreas.hansson@arm.com        DPRINTF(CoherentXBar,
7458949SN/A                "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n",
7469032SN/A                masterPorts[master_port_id]->name(), pkt->getAddr(),
7478948SN/A                pkt->cmdString());
7488948SN/A    }
7498948SN/A
7508948SN/A    // forward to all snoopers
7519031SN/A    forwardFunctional(pkt, InvalidPortID);
7528948SN/A}
7538948SN/A
7548948SN/Avoid
75510405Sandreas.hansson@arm.comCoherentXBar::forwardFunctional(PacketPtr pkt, PortID exclude_slave_port_id)
7568948SN/A{
7579524SN/A    // snoops should only happen if the system isn't bypassing caches
7589524SN/A    assert(!system->bypassCaches());
7599524SN/A
76010405Sandreas.hansson@arm.com    for (const auto& p: snoopPorts) {
7618915SN/A        // we could have gotten this request from a snooping master
7628915SN/A        // (corresponding to our own slave port that is also in
7638915SN/A        // snoopPorts) and should not send it back to where it came
7648915SN/A        // from
7659031SN/A        if (exclude_slave_port_id == InvalidPortID ||
7668948SN/A            p->getId() != exclude_slave_port_id)
7678948SN/A            p->sendFunctionalSnoop(pkt);
7688915SN/A
7698948SN/A        // if we get a response we are done
7708948SN/A        if (pkt->isResponse()) {
7718948SN/A            break;
7728915SN/A        }
7733650SN/A    }
7742497SN/A}
7752497SN/A
7769092SN/Aunsigned int
77710405Sandreas.hansson@arm.comCoherentXBar::drain(DrainManager *dm)
7789092SN/A{
7799093SN/A    // sum up the individual layers
7809715SN/A    unsigned int total = 0;
78110405Sandreas.hansson@arm.com    for (auto l: reqLayers)
78210405Sandreas.hansson@arm.com        total += l->drain(dm);
78310405Sandreas.hansson@arm.com    for (auto l: respLayers)
78410405Sandreas.hansson@arm.com        total += l->drain(dm);
78510405Sandreas.hansson@arm.com    for (auto l: snoopLayers)
78610405Sandreas.hansson@arm.com        total += l->drain(dm);
7879715SN/A    return total;
7889092SN/A}
7899092SN/A
7909712SN/Avoid
79110405Sandreas.hansson@arm.comCoherentXBar::regStats()
7929712SN/A{
79310405Sandreas.hansson@arm.com    // register the stats of the base class and our layers
79410405Sandreas.hansson@arm.com    BaseXBar::regStats();
79510405Sandreas.hansson@arm.com    for (auto l: reqLayers)
79610405Sandreas.hansson@arm.com        l->regStats();
79710405Sandreas.hansson@arm.com    for (auto l: respLayers)
79810405Sandreas.hansson@arm.com        l->regStats();
79910405Sandreas.hansson@arm.com    for (auto l: snoopLayers)
80010405Sandreas.hansson@arm.com        l->regStats();
8019712SN/A
80210405Sandreas.hansson@arm.com    snoops
80310405Sandreas.hansson@arm.com        .name(name() + ".snoops")
80410401SN/A        .desc("Total snoops (count)")
80510401SN/A    ;
80610401SN/A
80710401SN/A    snoopFanout
80810401SN/A        .init(0, snoopPorts.size(), 1)
80910401SN/A        .name(name() + ".snoop_fanout")
81010401SN/A        .desc("Request fanout histogram")
81110401SN/A    ;
8129712SN/A}
8139712SN/A
81410405Sandreas.hansson@arm.comCoherentXBar *
81510405Sandreas.hansson@arm.comCoherentXBarParams::create()
8162497SN/A{
81710405Sandreas.hansson@arm.com    return new CoherentXBar(this);
8182497SN/A}
819