bridge.cc revision 4433
12568SN/A
22568SN/A/*
32568SN/A * Copyright (c) 2006 The Regents of The University of Michigan
42568SN/A * All rights reserved.
52568SN/A *
62568SN/A * Redistribution and use in source and binary forms, with or without
72568SN/A * modification, are permitted provided that the following conditions are
82568SN/A * met: redistributions of source code must retain the above copyright
92568SN/A * notice, this list of conditions and the following disclaimer;
102568SN/A * redistributions in binary form must reproduce the above copyright
112568SN/A * notice, this list of conditions and the following disclaimer in the
122568SN/A * documentation and/or other materials provided with the distribution;
132568SN/A * neither the name of the copyright holders nor the names of its
142568SN/A * contributors may be used to endorse or promote products derived from
152568SN/A * this software without specific prior written permission.
162568SN/A *
172568SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182568SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192568SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
202568SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
212568SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
222568SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
232568SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242568SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252568SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262568SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
272568SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu *
292665Ssaidi@eecs.umich.edu * Authors: Ali Saidi
302665Ssaidi@eecs.umich.edu *          Steve Reinhardt
312568SN/A */
322568SN/A
332568SN/A/**
342982Sstever@eecs.umich.edu * @file
352982Sstever@eecs.umich.edu * Definition of a simple bus bridge without buffering.
362568SN/A */
372568SN/A
382643Sstever@eecs.umich.edu#include <algorithm>
392568SN/A
402568SN/A#include "base/trace.hh"
412568SN/A#include "mem/bridge.hh"
422568SN/A#include "sim/builder.hh"
432568SN/A
442643Sstever@eecs.umich.eduBridge::BridgePort::BridgePort(const std::string &_name,
452643Sstever@eecs.umich.edu                               Bridge *_bridge, BridgePort *_otherPort,
464432Ssaidi@eecs.umich.edu                               int _delay, int _queueLimit,
474432Ssaidi@eecs.umich.edu                               bool fix_partial_write)
482643Sstever@eecs.umich.edu    : Port(_name), bridge(_bridge), otherPort(_otherPort),
494432Ssaidi@eecs.umich.edu      delay(_delay), fixPartialWrite(fix_partial_write),
504433Ssaidi@eecs.umich.edu      outstandingResponses(0), queuedRequests(0),
514433Ssaidi@eecs.umich.edu      queueLimit(_queueLimit), sendEvent(this)
522643Sstever@eecs.umich.edu{
532643Sstever@eecs.umich.edu}
542643Sstever@eecs.umich.edu
552643Sstever@eecs.umich.eduBridge::Bridge(const std::string &n, int qsa, int qsb,
564432Ssaidi@eecs.umich.edu               Tick _delay, int write_ack, bool fix_partial_write_a,
574432Ssaidi@eecs.umich.edu               bool fix_partial_write_b)
582643Sstever@eecs.umich.edu    : MemObject(n),
594432Ssaidi@eecs.umich.edu      portA(n + "-portA", this, &portB, _delay, qsa, fix_partial_write_a),
604432Ssaidi@eecs.umich.edu      portB(n + "-portB", this, &portA, _delay, qsa, fix_partial_write_b),
612643Sstever@eecs.umich.edu      ackWrites(write_ack)
622643Sstever@eecs.umich.edu{
634432Ssaidi@eecs.umich.edu    if (ackWrites)
644432Ssaidi@eecs.umich.edu        panic("No support for acknowledging writes\n");
652643Sstever@eecs.umich.edu}
662643Sstever@eecs.umich.edu
672643Sstever@eecs.umich.eduPort *
682738Sstever@eecs.umich.eduBridge::getPort(const std::string &if_name, int idx)
692643Sstever@eecs.umich.edu{
702643Sstever@eecs.umich.edu    BridgePort *port;
712643Sstever@eecs.umich.edu
722643Sstever@eecs.umich.edu    if (if_name == "side_a")
732643Sstever@eecs.umich.edu        port = &portA;
742643Sstever@eecs.umich.edu    else if (if_name == "side_b")
752643Sstever@eecs.umich.edu        port = &portB;
762643Sstever@eecs.umich.edu    else
772643Sstever@eecs.umich.edu        return NULL;
782643Sstever@eecs.umich.edu
792643Sstever@eecs.umich.edu    if (port->getPeer() != NULL)
802643Sstever@eecs.umich.edu        panic("bridge side %s already connected to.", if_name);
812643Sstever@eecs.umich.edu    return port;
822643Sstever@eecs.umich.edu}
832643Sstever@eecs.umich.edu
842643Sstever@eecs.umich.edu
852568SN/Avoid
862568SN/ABridge::init()
872568SN/A{
882568SN/A    // Make sure that both sides are connected to.
892643Sstever@eecs.umich.edu    if (portA.getPeer() == NULL || portB.getPeer() == NULL)
904432Ssaidi@eecs.umich.edu        fatal("Both ports of bus bridge are not connected to a bus.\n");
914432Ssaidi@eecs.umich.edu
924432Ssaidi@eecs.umich.edu    if (portA.peerBlockSize() != portB.peerBlockSize())
934432Ssaidi@eecs.umich.edu        fatal("Busses don't have the same block size... Not supported.\n");
942568SN/A}
952568SN/A
964433Ssaidi@eecs.umich.edubool
974433Ssaidi@eecs.umich.eduBridge::BridgePort::queueFull()
984433Ssaidi@eecs.umich.edu{
994433Ssaidi@eecs.umich.edu    // use >= here because sendQueue could get larger because of
1004433Ssaidi@eecs.umich.edu    // nacks getting inserted
1014433Ssaidi@eecs.umich.edu    return queuedRequests + outstandingResponses >= queueLimit;
1024433Ssaidi@eecs.umich.edu}
1032568SN/A
1042643Sstever@eecs.umich.edu/** Function called by the port when the bus is receiving a Timing
1052568SN/A * transaction.*/
1062568SN/Abool
1073349Sbinkertn@umich.eduBridge::BridgePort::recvTiming(PacketPtr pkt)
1082568SN/A{
1094433Ssaidi@eecs.umich.edu    if (!(pkt->flags & SNOOP_COMMIT))
1104433Ssaidi@eecs.umich.edu        return true;
1114433Ssaidi@eecs.umich.edu
1124433Ssaidi@eecs.umich.edu
1134433Ssaidi@eecs.umich.edu    DPRINTF(BusBridge, "recvTiming: src %d dest %d addr 0x%x\n",
1143662Srdreslin@umich.edu                pkt->getSrc(), pkt->getDest(), pkt->getAddr());
1152643Sstever@eecs.umich.edu
1164433Ssaidi@eecs.umich.edu    if (pkt->isRequest() && otherPort->queueFull()) {
1174433Ssaidi@eecs.umich.edu        DPRINTF(BusBridge, "Remote queue full, nacking\n");
1184433Ssaidi@eecs.umich.edu        nackRequest(pkt);
1194433Ssaidi@eecs.umich.edu        return true;
1203662Srdreslin@umich.edu    }
1214433Ssaidi@eecs.umich.edu
1224433Ssaidi@eecs.umich.edu    if (pkt->needsResponse() && pkt->result != Packet::Nacked)
1234433Ssaidi@eecs.umich.edu        if (queueFull()) {
1244433Ssaidi@eecs.umich.edu            DPRINTF(BusBridge, "Local queue full, no space for response, nacking\n");
1254433Ssaidi@eecs.umich.edu            DPRINTF(BusBridge, "queue size: %d outreq: %d outstanding resp: %d\n",
1264433Ssaidi@eecs.umich.edu                    sendQueue.size(), queuedRequests, outstandingResponses);
1274433Ssaidi@eecs.umich.edu            nackRequest(pkt);
1284433Ssaidi@eecs.umich.edu            return true;
1294433Ssaidi@eecs.umich.edu        } else {
1304433Ssaidi@eecs.umich.edu            DPRINTF(BusBridge, "Request Needs response, reserving space\n");
1314433Ssaidi@eecs.umich.edu            ++outstandingResponses;
1324433Ssaidi@eecs.umich.edu        }
1334433Ssaidi@eecs.umich.edu
1344433Ssaidi@eecs.umich.edu    otherPort->queueForSendTiming(pkt);
1354433Ssaidi@eecs.umich.edu
1364433Ssaidi@eecs.umich.edu    return true;
1372657Ssaidi@eecs.umich.edu}
1382657Ssaidi@eecs.umich.edu
1394433Ssaidi@eecs.umich.eduvoid
1404433Ssaidi@eecs.umich.eduBridge::BridgePort::nackRequest(PacketPtr pkt)
1414433Ssaidi@eecs.umich.edu{
1424433Ssaidi@eecs.umich.edu    // Nack the packet
1434433Ssaidi@eecs.umich.edu    pkt->result = Packet::Nacked;
1444433Ssaidi@eecs.umich.edu    pkt->setDest(pkt->getSrc());
1452657Ssaidi@eecs.umich.edu
1464433Ssaidi@eecs.umich.edu    //put it on the list to send
1474433Ssaidi@eecs.umich.edu    Tick readyTime = curTick + delay;
1484433Ssaidi@eecs.umich.edu    PacketBuffer *buf = new PacketBuffer(pkt, readyTime, true);
1494433Ssaidi@eecs.umich.edu    if (sendQueue.empty()) {
1504433Ssaidi@eecs.umich.edu        sendEvent.schedule(readyTime);
1514433Ssaidi@eecs.umich.edu    }
1524433Ssaidi@eecs.umich.edu    sendQueue.push_back(buf);
1534433Ssaidi@eecs.umich.edu}
1544433Ssaidi@eecs.umich.edu
1554433Ssaidi@eecs.umich.edu
1564433Ssaidi@eecs.umich.eduvoid
1573349Sbinkertn@umich.eduBridge::BridgePort::queueForSendTiming(PacketPtr pkt)
1582657Ssaidi@eecs.umich.edu{
1594433Ssaidi@eecs.umich.edu   if (pkt->isResponse() || pkt->result == Packet::Nacked) {
1602643Sstever@eecs.umich.edu        // This is a response for a request we forwarded earlier.  The
1612643Sstever@eecs.umich.edu        // corresponding PacketBuffer should be stored in the packet's
1622643Sstever@eecs.umich.edu        // senderState field.
1632643Sstever@eecs.umich.edu        PacketBuffer *buf = dynamic_cast<PacketBuffer*>(pkt->senderState);
1642643Sstever@eecs.umich.edu        assert(buf != NULL);
1652643Sstever@eecs.umich.edu        // set up new packet dest & senderState based on values saved
1662643Sstever@eecs.umich.edu        // from original request
1672643Sstever@eecs.umich.edu        buf->fixResponse(pkt);
1684433Ssaidi@eecs.umich.edu
1694433Ssaidi@eecs.umich.edu        // Check if this packet was expecting a response (this is either it or
1704433Ssaidi@eecs.umich.edu        // its a nacked packet and we won't be seeing that response)
1714433Ssaidi@eecs.umich.edu        if (buf->expectResponse)
1724433Ssaidi@eecs.umich.edu            --outstandingResponses;
1734433Ssaidi@eecs.umich.edu
1744433Ssaidi@eecs.umich.edu
1752657Ssaidi@eecs.umich.edu        DPRINTF(BusBridge, "restoring  sender state: %#X, from packet buffer: %#X\n",
1762657Ssaidi@eecs.umich.edu                        pkt->senderState, buf);
1772643Sstever@eecs.umich.edu        DPRINTF(BusBridge, "  is response, new dest %d\n", pkt->getDest());
1782643Sstever@eecs.umich.edu        delete buf;
1792643Sstever@eecs.umich.edu    }
1802643Sstever@eecs.umich.edu
1812643Sstever@eecs.umich.edu    Tick readyTime = curTick + delay;
1822643Sstever@eecs.umich.edu    PacketBuffer *buf = new PacketBuffer(pkt, readyTime);
1832657Ssaidi@eecs.umich.edu    DPRINTF(BusBridge, "old sender state: %#X, new sender state: %#X\n",
1842657Ssaidi@eecs.umich.edu            buf->origSenderState, buf);
1852643Sstever@eecs.umich.edu
1862643Sstever@eecs.umich.edu    // If we're about to put this packet at the head of the queue, we
1872643Sstever@eecs.umich.edu    // need to schedule an event to do the transmit.  Otherwise there
1882643Sstever@eecs.umich.edu    // should already be an event scheduled for sending the head
1892643Sstever@eecs.umich.edu    // packet.
1902643Sstever@eecs.umich.edu    if (sendQueue.empty()) {
1912643Sstever@eecs.umich.edu        sendEvent.schedule(readyTime);
1922568SN/A    }
1934433Ssaidi@eecs.umich.edu    ++queuedRequests;
1942643Sstever@eecs.umich.edu    sendQueue.push_back(buf);
1952568SN/A}
1962568SN/A
1972568SN/Avoid
1982643Sstever@eecs.umich.eduBridge::BridgePort::trySend()
1992568SN/A{
2002643Sstever@eecs.umich.edu    assert(!sendQueue.empty());
2012568SN/A
2022657Ssaidi@eecs.umich.edu    bool was_full = queueFull();
2034432Ssaidi@eecs.umich.edu    int pbs = peerBlockSize();
2042657Ssaidi@eecs.umich.edu
2052643Sstever@eecs.umich.edu    PacketBuffer *buf = sendQueue.front();
2062643Sstever@eecs.umich.edu
2072643Sstever@eecs.umich.edu    assert(buf->ready <= curTick);
2082643Sstever@eecs.umich.edu
2093349Sbinkertn@umich.edu    PacketPtr pkt = buf->pkt;
2102643Sstever@eecs.umich.edu
2114432Ssaidi@eecs.umich.edu    pkt->flags &= ~SNOOP_COMMIT; //CLear it if it was set
2124432Ssaidi@eecs.umich.edu
2134432Ssaidi@eecs.umich.edu    if (pkt->cmd == MemCmd::WriteInvalidateReq && fixPartialWrite &&
2144433Ssaidi@eecs.umich.edu            pkt->result != Packet::Nacked && pkt->getOffset(pbs) &&
2154433Ssaidi@eecs.umich.edu            pkt->getSize() != pbs) {
2164432Ssaidi@eecs.umich.edu        buf->partialWriteFix(this);
2174432Ssaidi@eecs.umich.edu        pkt = buf->pkt;
2184432Ssaidi@eecs.umich.edu    }
2194432Ssaidi@eecs.umich.edu
2202643Sstever@eecs.umich.edu    DPRINTF(BusBridge, "trySend: origSrc %d dest %d addr 0x%x\n",
2212643Sstever@eecs.umich.edu            buf->origSrc, pkt->getDest(), pkt->getAddr());
2222643Sstever@eecs.umich.edu
2234432Ssaidi@eecs.umich.edu
2242643Sstever@eecs.umich.edu    if (sendTiming(pkt)) {
2252643Sstever@eecs.umich.edu        // send successful
2262643Sstever@eecs.umich.edu        sendQueue.pop_front();
2272643Sstever@eecs.umich.edu        buf->pkt = NULL; // we no longer own packet, so it's not safe to look at it
2282657Ssaidi@eecs.umich.edu
2292657Ssaidi@eecs.umich.edu        if (buf->expectResponse) {
2304433Ssaidi@eecs.umich.edu            // Must wait for response
2312657Ssaidi@eecs.umich.edu            DPRINTF(BusBridge, "  successful: awaiting response (%d)\n",
2322657Ssaidi@eecs.umich.edu                    outstandingResponses);
2332657Ssaidi@eecs.umich.edu        } else {
2342657Ssaidi@eecs.umich.edu            // no response expected... deallocate packet buffer now.
2352657Ssaidi@eecs.umich.edu            DPRINTF(BusBridge, "  successful: no response expected\n");
2362657Ssaidi@eecs.umich.edu            delete buf;
2372657Ssaidi@eecs.umich.edu        }
2382657Ssaidi@eecs.umich.edu
2394433Ssaidi@eecs.umich.edu        if (!buf->nacked)
2404433Ssaidi@eecs.umich.edu                --queuedRequests;
2414433Ssaidi@eecs.umich.edu
2422657Ssaidi@eecs.umich.edu        // If there are more packets to send, schedule event to try again.
2432657Ssaidi@eecs.umich.edu        if (!sendQueue.empty()) {
2442657Ssaidi@eecs.umich.edu            buf = sendQueue.front();
2454433Ssaidi@eecs.umich.edu            DPRINTF(BusBridge, "Scheduling next send\n");
2462657Ssaidi@eecs.umich.edu            sendEvent.schedule(std::max(buf->ready, curTick + 1));
2472657Ssaidi@eecs.umich.edu        }
2482657Ssaidi@eecs.umich.edu        // Let things start sending again
2494433Ssaidi@eecs.umich.edu        if (was_full && !queueFull()) {
2502657Ssaidi@eecs.umich.edu          DPRINTF(BusBridge, "Queue was full, sending retry\n");
2512657Ssaidi@eecs.umich.edu          otherPort->sendRetry();
2522657Ssaidi@eecs.umich.edu        }
2532657Ssaidi@eecs.umich.edu
2542643Sstever@eecs.umich.edu    } else {
2552643Sstever@eecs.umich.edu        DPRINTF(BusBridge, "  unsuccessful\n");
2564432Ssaidi@eecs.umich.edu        buf->undoPartialWriteFix();
2572643Sstever@eecs.umich.edu    }
2584433Ssaidi@eecs.umich.edu    DPRINTF(BusBridge, "trySend: queue size: %d outreq: %d outstanding resp: %d\n",
2594433Ssaidi@eecs.umich.edu                    sendQueue.size(), queuedRequests, outstandingResponses);
2602568SN/A}
2612568SN/A
2622568SN/A
2632657Ssaidi@eecs.umich.eduvoid
2642568SN/ABridge::BridgePort::recvRetry()
2652568SN/A{
2662657Ssaidi@eecs.umich.edu    trySend();
2672568SN/A}
2682568SN/A
2692643Sstever@eecs.umich.edu/** Function called by the port when the bus is receiving a Atomic
2702568SN/A * transaction.*/
2712568SN/ATick
2723349Sbinkertn@umich.eduBridge::BridgePort::recvAtomic(PacketPtr pkt)
2732568SN/A{
2742643Sstever@eecs.umich.edu    return otherPort->sendAtomic(pkt) + delay;
2752568SN/A}
2762568SN/A
2772643Sstever@eecs.umich.edu/** Function called by the port when the bus is receiving a Functional
2782568SN/A * transaction.*/
2792568SN/Avoid
2803349Sbinkertn@umich.eduBridge::BridgePort::recvFunctional(PacketPtr pkt)
2812568SN/A{
2822643Sstever@eecs.umich.edu    std::list<PacketBuffer*>::iterator i;
2832568SN/A    bool pktContinue = true;
2842568SN/A
2852643Sstever@eecs.umich.edu    for (i = sendQueue.begin();  i != sendQueue.end(); ++i) {
2862643Sstever@eecs.umich.edu        if (pkt->intersect((*i)->pkt)) {
2872643Sstever@eecs.umich.edu            pktContinue &= fixPacket(pkt, (*i)->pkt);
2882568SN/A        }
2892568SN/A    }
2902568SN/A
2912568SN/A    if (pktContinue) {
2922643Sstever@eecs.umich.edu        otherPort->sendFunctional(pkt);
2932568SN/A    }
2942568SN/A}
2952568SN/A
2962643Sstever@eecs.umich.edu/** Function called by the port when the bus is receiving a status change.*/
2972568SN/Avoid
2982643Sstever@eecs.umich.eduBridge::BridgePort::recvStatusChange(Port::Status status)
2992568SN/A{
3002643Sstever@eecs.umich.edu    otherPort->sendStatusChange(status);
3012568SN/A}
3022568SN/A
3032568SN/Avoid
3042643Sstever@eecs.umich.eduBridge::BridgePort::getDeviceAddressRanges(AddrRangeList &resp,
3052643Sstever@eecs.umich.edu                                           AddrRangeList &snoop)
3062568SN/A{
3072643Sstever@eecs.umich.edu    otherPort->getPeerAddressRanges(resp, snoop);
3082568SN/A}
3092568SN/A
3102568SN/ABEGIN_DECLARE_SIM_OBJECT_PARAMS(Bridge)
3112568SN/A
3122568SN/A   Param<int> queue_size_a;
3132568SN/A   Param<int> queue_size_b;
3142568SN/A   Param<Tick> delay;
3152568SN/A   Param<bool> write_ack;
3164432Ssaidi@eecs.umich.edu   Param<bool> fix_partial_write_a;
3174432Ssaidi@eecs.umich.edu   Param<bool> fix_partial_write_b;
3182568SN/A
3192568SN/AEND_DECLARE_SIM_OBJECT_PARAMS(Bridge)
3202568SN/A
3212568SN/ABEGIN_INIT_SIM_OBJECT_PARAMS(Bridge)
3222568SN/A
3232568SN/A    INIT_PARAM(queue_size_a, "The size of the queue for data coming into side a"),
3242568SN/A    INIT_PARAM(queue_size_b, "The size of the queue for data coming into side b"),
3252568SN/A    INIT_PARAM(delay, "The miminum delay to cross this bridge"),
3264432Ssaidi@eecs.umich.edu    INIT_PARAM(write_ack, "Acknowledge any writes that are received."),
3274432Ssaidi@eecs.umich.edu    INIT_PARAM(fix_partial_write_a, "Fixup any partial block writes that are received"),
3284432Ssaidi@eecs.umich.edu    INIT_PARAM(fix_partial_write_b, "Fixup any partial block writes that are received")
3292568SN/A
3302568SN/AEND_INIT_SIM_OBJECT_PARAMS(Bridge)
3312568SN/A
3322568SN/ACREATE_SIM_OBJECT(Bridge)
3332568SN/A{
3342568SN/A    return new Bridge(getInstanceName(), queue_size_a, queue_size_b, delay,
3354432Ssaidi@eecs.umich.edu            write_ack, fix_partial_write_a, fix_partial_write_b);
3362568SN/A}
3372568SN/A
3382568SN/AREGISTER_SIM_OBJECT("Bridge", Bridge)
3394433Ssaidi@eecs.umich.edu
340