bridge.cc revision 4432
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),
504432Ssaidi@eecs.umich.edu      outstandingResponses(0), queueLimit(_queueLimit), sendEvent(this)
512643Sstever@eecs.umich.edu{
522643Sstever@eecs.umich.edu}
532643Sstever@eecs.umich.edu
542643Sstever@eecs.umich.eduBridge::Bridge(const std::string &n, int qsa, int qsb,
554432Ssaidi@eecs.umich.edu               Tick _delay, int write_ack, bool fix_partial_write_a,
564432Ssaidi@eecs.umich.edu               bool fix_partial_write_b)
572643Sstever@eecs.umich.edu    : MemObject(n),
584432Ssaidi@eecs.umich.edu      portA(n + "-portA", this, &portB, _delay, qsa, fix_partial_write_a),
594432Ssaidi@eecs.umich.edu      portB(n + "-portB", this, &portA, _delay, qsa, fix_partial_write_b),
602643Sstever@eecs.umich.edu      ackWrites(write_ack)
612643Sstever@eecs.umich.edu{
624432Ssaidi@eecs.umich.edu    if (ackWrites)
634432Ssaidi@eecs.umich.edu        panic("No support for acknowledging writes\n");
642643Sstever@eecs.umich.edu}
652643Sstever@eecs.umich.edu
662643Sstever@eecs.umich.eduPort *
672738Sstever@eecs.umich.eduBridge::getPort(const std::string &if_name, int idx)
682643Sstever@eecs.umich.edu{
692643Sstever@eecs.umich.edu    BridgePort *port;
702643Sstever@eecs.umich.edu
712643Sstever@eecs.umich.edu    if (if_name == "side_a")
722643Sstever@eecs.umich.edu        port = &portA;
732643Sstever@eecs.umich.edu    else if (if_name == "side_b")
742643Sstever@eecs.umich.edu        port = &portB;
752643Sstever@eecs.umich.edu    else
762643Sstever@eecs.umich.edu        return NULL;
772643Sstever@eecs.umich.edu
782643Sstever@eecs.umich.edu    if (port->getPeer() != NULL)
792643Sstever@eecs.umich.edu        panic("bridge side %s already connected to.", if_name);
802643Sstever@eecs.umich.edu    return port;
812643Sstever@eecs.umich.edu}
822643Sstever@eecs.umich.edu
832643Sstever@eecs.umich.edu
842568SN/Avoid
852568SN/ABridge::init()
862568SN/A{
872568SN/A    // Make sure that both sides are connected to.
882643Sstever@eecs.umich.edu    if (portA.getPeer() == NULL || portB.getPeer() == NULL)
894432Ssaidi@eecs.umich.edu        fatal("Both ports of bus bridge are not connected to a bus.\n");
904432Ssaidi@eecs.umich.edu
914432Ssaidi@eecs.umich.edu    if (portA.peerBlockSize() != portB.peerBlockSize())
924432Ssaidi@eecs.umich.edu        fatal("Busses don't have the same block size... Not supported.\n");
932568SN/A}
942568SN/A
952568SN/A
962643Sstever@eecs.umich.edu/** Function called by the port when the bus is receiving a Timing
972568SN/A * transaction.*/
982568SN/Abool
993349Sbinkertn@umich.eduBridge::BridgePort::recvTiming(PacketPtr pkt)
1002568SN/A{
1013662Srdreslin@umich.edu    if (pkt->flags & SNOOP_COMMIT) {
1023662Srdreslin@umich.edu        DPRINTF(BusBridge, "recvTiming: src %d dest %d addr 0x%x\n",
1033662Srdreslin@umich.edu                pkt->getSrc(), pkt->getDest(), pkt->getAddr());
1042643Sstever@eecs.umich.edu
1053662Srdreslin@umich.edu        return otherPort->queueForSendTiming(pkt);
1063662Srdreslin@umich.edu    }
1073662Srdreslin@umich.edu    else {
1083662Srdreslin@umich.edu        // Else it's just a snoop, properly return if we are blocking
1093662Srdreslin@umich.edu        return !queueFull();
1103662Srdreslin@umich.edu    }
1112657Ssaidi@eecs.umich.edu}
1122657Ssaidi@eecs.umich.edu
1132657Ssaidi@eecs.umich.edu
1142657Ssaidi@eecs.umich.edubool
1153349Sbinkertn@umich.eduBridge::BridgePort::queueForSendTiming(PacketPtr pkt)
1162657Ssaidi@eecs.umich.edu{
1174432Ssaidi@eecs.umich.edu    if (queueFull()) {
1184432Ssaidi@eecs.umich.edu        DPRINTF(BusBridge, "Queue full, returning false\n");
1192657Ssaidi@eecs.umich.edu        return false;
1204432Ssaidi@eecs.umich.edu    }
1212657Ssaidi@eecs.umich.edu
1222657Ssaidi@eecs.umich.edu   if (pkt->isResponse()) {
1232643Sstever@eecs.umich.edu        // This is a response for a request we forwarded earlier.  The
1242643Sstever@eecs.umich.edu        // corresponding PacketBuffer should be stored in the packet's
1252643Sstever@eecs.umich.edu        // senderState field.
1262643Sstever@eecs.umich.edu        PacketBuffer *buf = dynamic_cast<PacketBuffer*>(pkt->senderState);
1272643Sstever@eecs.umich.edu        assert(buf != NULL);
1282643Sstever@eecs.umich.edu        // set up new packet dest & senderState based on values saved
1292643Sstever@eecs.umich.edu        // from original request
1302643Sstever@eecs.umich.edu        buf->fixResponse(pkt);
1312657Ssaidi@eecs.umich.edu        DPRINTF(BusBridge, "restoring  sender state: %#X, from packet buffer: %#X\n",
1322657Ssaidi@eecs.umich.edu                        pkt->senderState, buf);
1332643Sstever@eecs.umich.edu        DPRINTF(BusBridge, "  is response, new dest %d\n", pkt->getDest());
1342643Sstever@eecs.umich.edu        delete buf;
1352643Sstever@eecs.umich.edu    }
1362643Sstever@eecs.umich.edu
1372643Sstever@eecs.umich.edu    Tick readyTime = curTick + delay;
1382643Sstever@eecs.umich.edu    PacketBuffer *buf = new PacketBuffer(pkt, readyTime);
1392657Ssaidi@eecs.umich.edu    DPRINTF(BusBridge, "old sender state: %#X, new sender state: %#X\n",
1402657Ssaidi@eecs.umich.edu            buf->origSenderState, buf);
1412643Sstever@eecs.umich.edu
1422643Sstever@eecs.umich.edu    // If we're about to put this packet at the head of the queue, we
1432643Sstever@eecs.umich.edu    // need to schedule an event to do the transmit.  Otherwise there
1442643Sstever@eecs.umich.edu    // should already be an event scheduled for sending the head
1452643Sstever@eecs.umich.edu    // packet.
1462643Sstever@eecs.umich.edu    if (sendQueue.empty()) {
1472643Sstever@eecs.umich.edu        sendEvent.schedule(readyTime);
1482568SN/A    }
1492643Sstever@eecs.umich.edu
1502643Sstever@eecs.umich.edu    sendQueue.push_back(buf);
1512643Sstever@eecs.umich.edu
1522568SN/A    return true;
1532568SN/A}
1542568SN/A
1552568SN/Avoid
1562643Sstever@eecs.umich.eduBridge::BridgePort::trySend()
1572568SN/A{
1582643Sstever@eecs.umich.edu    assert(!sendQueue.empty());
1592568SN/A
1602657Ssaidi@eecs.umich.edu    bool was_full = queueFull();
1614432Ssaidi@eecs.umich.edu    int pbs = peerBlockSize();
1622657Ssaidi@eecs.umich.edu
1632643Sstever@eecs.umich.edu    PacketBuffer *buf = sendQueue.front();
1642643Sstever@eecs.umich.edu
1652643Sstever@eecs.umich.edu    assert(buf->ready <= curTick);
1662643Sstever@eecs.umich.edu
1673349Sbinkertn@umich.edu    PacketPtr pkt = buf->pkt;
1682643Sstever@eecs.umich.edu
1694432Ssaidi@eecs.umich.edu    pkt->flags &= ~SNOOP_COMMIT; //CLear it if it was set
1704432Ssaidi@eecs.umich.edu
1714432Ssaidi@eecs.umich.edu    if (pkt->cmd == MemCmd::WriteInvalidateReq && fixPartialWrite &&
1724432Ssaidi@eecs.umich.edu            pkt->getOffset(pbs) && pkt->getSize() != pbs) {
1734432Ssaidi@eecs.umich.edu        buf->partialWriteFix(this);
1744432Ssaidi@eecs.umich.edu        pkt = buf->pkt;
1754432Ssaidi@eecs.umich.edu    }
1764432Ssaidi@eecs.umich.edu
1772643Sstever@eecs.umich.edu    DPRINTF(BusBridge, "trySend: origSrc %d dest %d addr 0x%x\n",
1782643Sstever@eecs.umich.edu            buf->origSrc, pkt->getDest(), pkt->getAddr());
1792643Sstever@eecs.umich.edu
1804432Ssaidi@eecs.umich.edu
1812643Sstever@eecs.umich.edu    if (sendTiming(pkt)) {
1822643Sstever@eecs.umich.edu        // send successful
1832643Sstever@eecs.umich.edu        sendQueue.pop_front();
1842643Sstever@eecs.umich.edu        buf->pkt = NULL; // we no longer own packet, so it's not safe to look at it
1852657Ssaidi@eecs.umich.edu
1862657Ssaidi@eecs.umich.edu        if (buf->expectResponse) {
1872657Ssaidi@eecs.umich.edu            // Must wait for response.  We just need to count outstanding
1882657Ssaidi@eecs.umich.edu            // responses (in case we want to cap them); PacketBuffer
1892657Ssaidi@eecs.umich.edu            // pointer will be recovered on response.
1902657Ssaidi@eecs.umich.edu            ++outstandingResponses;
1912657Ssaidi@eecs.umich.edu            DPRINTF(BusBridge, "  successful: awaiting response (%d)\n",
1922657Ssaidi@eecs.umich.edu                    outstandingResponses);
1932657Ssaidi@eecs.umich.edu        } else {
1942657Ssaidi@eecs.umich.edu            // no response expected... deallocate packet buffer now.
1952657Ssaidi@eecs.umich.edu            DPRINTF(BusBridge, "  successful: no response expected\n");
1962657Ssaidi@eecs.umich.edu            delete buf;
1972657Ssaidi@eecs.umich.edu        }
1982657Ssaidi@eecs.umich.edu
1992657Ssaidi@eecs.umich.edu        // If there are more packets to send, schedule event to try again.
2002657Ssaidi@eecs.umich.edu        if (!sendQueue.empty()) {
2012657Ssaidi@eecs.umich.edu            buf = sendQueue.front();
2022657Ssaidi@eecs.umich.edu            sendEvent.schedule(std::max(buf->ready, curTick + 1));
2032657Ssaidi@eecs.umich.edu        }
2042657Ssaidi@eecs.umich.edu        // Let things start sending again
2052657Ssaidi@eecs.umich.edu        if (was_full) {
2062657Ssaidi@eecs.umich.edu          DPRINTF(BusBridge, "Queue was full, sending retry\n");
2072657Ssaidi@eecs.umich.edu          otherPort->sendRetry();
2082657Ssaidi@eecs.umich.edu        }
2092657Ssaidi@eecs.umich.edu
2102643Sstever@eecs.umich.edu    } else {
2112643Sstever@eecs.umich.edu        DPRINTF(BusBridge, "  unsuccessful\n");
2124432Ssaidi@eecs.umich.edu        buf->undoPartialWriteFix();
2132643Sstever@eecs.umich.edu    }
2142568SN/A}
2152568SN/A
2162568SN/A
2172657Ssaidi@eecs.umich.eduvoid
2182568SN/ABridge::BridgePort::recvRetry()
2192568SN/A{
2202657Ssaidi@eecs.umich.edu    trySend();
2212568SN/A}
2222568SN/A
2232643Sstever@eecs.umich.edu/** Function called by the port when the bus is receiving a Atomic
2242568SN/A * transaction.*/
2252568SN/ATick
2263349Sbinkertn@umich.eduBridge::BridgePort::recvAtomic(PacketPtr pkt)
2272568SN/A{
2282643Sstever@eecs.umich.edu    return otherPort->sendAtomic(pkt) + delay;
2292568SN/A}
2302568SN/A
2312643Sstever@eecs.umich.edu/** Function called by the port when the bus is receiving a Functional
2322568SN/A * transaction.*/
2332568SN/Avoid
2343349Sbinkertn@umich.eduBridge::BridgePort::recvFunctional(PacketPtr pkt)
2352568SN/A{
2362643Sstever@eecs.umich.edu    std::list<PacketBuffer*>::iterator i;
2372568SN/A    bool pktContinue = true;
2382568SN/A
2392643Sstever@eecs.umich.edu    for (i = sendQueue.begin();  i != sendQueue.end(); ++i) {
2402643Sstever@eecs.umich.edu        if (pkt->intersect((*i)->pkt)) {
2412643Sstever@eecs.umich.edu            pktContinue &= fixPacket(pkt, (*i)->pkt);
2422568SN/A        }
2432568SN/A    }
2442568SN/A
2452568SN/A    if (pktContinue) {
2462643Sstever@eecs.umich.edu        otherPort->sendFunctional(pkt);
2472568SN/A    }
2482568SN/A}
2492568SN/A
2502643Sstever@eecs.umich.edu/** Function called by the port when the bus is receiving a status change.*/
2512568SN/Avoid
2522643Sstever@eecs.umich.eduBridge::BridgePort::recvStatusChange(Port::Status status)
2532568SN/A{
2542643Sstever@eecs.umich.edu    otherPort->sendStatusChange(status);
2552568SN/A}
2562568SN/A
2572568SN/Avoid
2582643Sstever@eecs.umich.eduBridge::BridgePort::getDeviceAddressRanges(AddrRangeList &resp,
2592643Sstever@eecs.umich.edu                                           AddrRangeList &snoop)
2602568SN/A{
2612643Sstever@eecs.umich.edu    otherPort->getPeerAddressRanges(resp, snoop);
2622568SN/A}
2632568SN/A
2642568SN/ABEGIN_DECLARE_SIM_OBJECT_PARAMS(Bridge)
2652568SN/A
2662568SN/A   Param<int> queue_size_a;
2672568SN/A   Param<int> queue_size_b;
2682568SN/A   Param<Tick> delay;
2692568SN/A   Param<bool> write_ack;
2704432Ssaidi@eecs.umich.edu   Param<bool> fix_partial_write_a;
2714432Ssaidi@eecs.umich.edu   Param<bool> fix_partial_write_b;
2722568SN/A
2732568SN/AEND_DECLARE_SIM_OBJECT_PARAMS(Bridge)
2742568SN/A
2752568SN/ABEGIN_INIT_SIM_OBJECT_PARAMS(Bridge)
2762568SN/A
2772568SN/A    INIT_PARAM(queue_size_a, "The size of the queue for data coming into side a"),
2782568SN/A    INIT_PARAM(queue_size_b, "The size of the queue for data coming into side b"),
2792568SN/A    INIT_PARAM(delay, "The miminum delay to cross this bridge"),
2804432Ssaidi@eecs.umich.edu    INIT_PARAM(write_ack, "Acknowledge any writes that are received."),
2814432Ssaidi@eecs.umich.edu    INIT_PARAM(fix_partial_write_a, "Fixup any partial block writes that are received"),
2824432Ssaidi@eecs.umich.edu    INIT_PARAM(fix_partial_write_b, "Fixup any partial block writes that are received")
2832568SN/A
2842568SN/AEND_INIT_SIM_OBJECT_PARAMS(Bridge)
2852568SN/A
2862568SN/ACREATE_SIM_OBJECT(Bridge)
2872568SN/A{
2882568SN/A    return new Bridge(getInstanceName(), queue_size_a, queue_size_b, delay,
2894432Ssaidi@eecs.umich.edu            write_ack, fix_partial_write_a, fix_partial_write_b);
2902568SN/A}
2912568SN/A
2922568SN/AREGISTER_SIM_OBJECT("Bridge", Bridge)
293