mem: Fix guest corruption when caches handle uncacheable accesses
[gem5.git] / src / mem / bridge.cc
index e7d52b178c5a3c86d20997c116322a3686795e8a..bece5e6a13d6827669f766b0e6d4bff8cdf0c674 100644 (file)
@@ -1,5 +1,16 @@
-
 /*
+ * Copyright (c) 2011-2012 ARM Limited
+ * All rights reserved
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder.  You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
  * Copyright (c) 2006 The Regents of The University of Michigan
  * All rights reserved.
  *
  *
  * Authors: Ali Saidi
  *          Steve Reinhardt
+ *          Andreas Hansson
  */
 
 /**
  * @file
- * Definition of a simple bus bridge without buffering.
+ * Implementation of a memory-mapped bus bridge that connects a master
+ * and a slave through a request and response queue.
  */
 
-#include <algorithm>
-
 #include "base/trace.hh"
+#include "debug/Bridge.hh"
 #include "mem/bridge.hh"
-#include "sim/builder.hh"
-
-Bridge::BridgePort::BridgePort(const std::string &_name,
-                               Bridge *_bridge, BridgePort *_otherPort,
-                               int _delay, int _queueLimit,
-                               bool fix_partial_write)
-    : Port(_name), bridge(_bridge), otherPort(_otherPort),
-      delay(_delay), fixPartialWrite(fix_partial_write),
-      outstandingResponses(0), queuedRequests(0),
-      queueLimit(_queueLimit), sendEvent(this)
+#include "params/Bridge.hh"
+
+Bridge::BridgeSlavePort::BridgeSlavePort(const std::string& _name,
+                                         Bridge& _bridge,
+                                         BridgeMasterPort& _masterPort,
+                                         Cycles _delay, int _resp_limit,
+                                         std::vector<AddrRange> _ranges)
+    : SlavePort(_name, &_bridge), bridge(_bridge), masterPort(_masterPort),
+      delay(_delay), ranges(_ranges.begin(), _ranges.end()),
+      outstandingResponses(0), retryReq(false),
+      respQueueLimit(_resp_limit), sendEvent(*this)
 {
 }
 
-Bridge::Bridge(const std::string &n, int qsa, int qsb,
-               Tick _delay, int write_ack, bool fix_partial_write_a,
-               bool fix_partial_write_b)
-    : MemObject(n),
-      portA(n + "-portA", this, &portB, _delay, qsa, fix_partial_write_a),
-      portB(n + "-portB", this, &portA, _delay, qsa, fix_partial_write_b),
-      ackWrites(write_ack)
+Bridge::BridgeMasterPort::BridgeMasterPort(const std::string& _name,
+                                           Bridge& _bridge,
+                                           BridgeSlavePort& _slavePort,
+                                           Cycles _delay, int _req_limit)
+    : MasterPort(_name, &_bridge), bridge(_bridge), slavePort(_slavePort),
+      delay(_delay), reqQueueLimit(_req_limit), sendEvent(*this)
 {
-    if (ackWrites)
-        panic("No support for acknowledging writes\n");
 }
 
-Port *
-Bridge::getPort(const std::string &if_name, int idx)
+Bridge::Bridge(Params *p)
+    : MemObject(p),
+      slavePort(p->name + ".slave", *this, masterPort,
+                ticksToCycles(p->delay), p->resp_size, p->ranges),
+      masterPort(p->name + ".master", *this, slavePort,
+                 ticksToCycles(p->delay), p->req_size)
 {
-    BridgePort *port;
+}
 
-    if (if_name == "side_a")
-        port = &portA;
-    else if (if_name == "side_b")
-        port = &portB;
+BaseMasterPort&
+Bridge::getMasterPort(const std::string &if_name, PortID idx)
+{
+    if (if_name == "master")
+        return masterPort;
     else
-        return NULL;
-
-    if (port->getPeer() != NULL)
-        panic("bridge side %s already connected to.", if_name);
-    return port;
+        // pass it along to our super class
+        return MemObject::getMasterPort(if_name, idx);
 }
 
+BaseSlavePort&
+Bridge::getSlavePort(const std::string &if_name, PortID idx)
+{
+    if (if_name == "slave")
+        return slavePort;
+    else
+        // pass it along to our super class
+        return MemObject::getSlavePort(if_name, idx);
+}
 
 void
 Bridge::init()
 {
-    // Make sure that both sides are connected to.
-    if (portA.getPeer() == NULL || portB.getPeer() == NULL)
+    // make sure both sides are connected and have the same block size
+    if (!slavePort.isConnected() || !masterPort.isConnected())
         fatal("Both ports of bus bridge are not connected to a bus.\n");
 
-    if (portA.peerBlockSize() != portB.peerBlockSize())
-        fatal("Busses don't have the same block size... Not supported.\n");
+    if (slavePort.peerBlockSize() != masterPort.peerBlockSize())
+        fatal("Slave port size %d, master port size %d \n " \
+              "Busses don't have the same block size... Not supported.\n",
+              slavePort.peerBlockSize(), masterPort.peerBlockSize());
+
+    // notify the master side  of our address ranges
+    slavePort.sendRangeChange();
 }
 
 bool
-Bridge::BridgePort::queueFull()
+Bridge::BridgeSlavePort::respQueueFull()
 {
-    // use >= here because sendQueue could get larger because of
-    // nacks getting inserted
-    return queuedRequests + outstandingResponses >= queueLimit;
+    return outstandingResponses == respQueueLimit;
 }
 
-/** Function called by the port when the bus is receiving a Timing
- * transaction.*/
 bool
-Bridge::BridgePort::recvTiming(PacketPtr pkt)
+Bridge::BridgeMasterPort::reqQueueFull()
 {
-    if (!(pkt->flags & SNOOP_COMMIT))
-        return true;
+    return transmitList.size() == reqQueueLimit;
+}
 
+bool
+Bridge::BridgeMasterPort::recvTimingResp(PacketPtr pkt)
+{
+    // all checks are done when the request is accepted on the slave
+    // side, so we are guaranteed to have space for the response
+    DPRINTF(Bridge, "recvTimingResp: %s addr 0x%x\n",
+            pkt->cmdString(), pkt->getAddr());
 
-    DPRINTF(BusBridge, "recvTiming: src %d dest %d addr 0x%x\n",
-                pkt->getSrc(), pkt->getDest(), pkt->getAddr());
+    DPRINTF(Bridge, "Request queue size: %d\n", transmitList.size());
 
-    if (pkt->isRequest() && otherPort->queueFull()) {
-        DPRINTF(BusBridge, "Remote queue full, nacking\n");
-        nackRequest(pkt);
-        return true;
-    }
+    slavePort.schedTimingResp(pkt, bridge.clockEdge(delay));
 
-    if (pkt->needsResponse() && pkt->result != Packet::Nacked)
-        if (queueFull()) {
-            DPRINTF(BusBridge, "Local queue full, no space for response, nacking\n");
-            DPRINTF(BusBridge, "queue size: %d outreq: %d outstanding resp: %d\n",
-                    sendQueue.size(), queuedRequests, outstandingResponses);
-            nackRequest(pkt);
-            return true;
+    return true;
+}
+
+bool
+Bridge::BridgeSlavePort::recvTimingReq(PacketPtr pkt)
+{
+    DPRINTF(Bridge, "recvTimingReq: %s addr 0x%x\n",
+            pkt->cmdString(), pkt->getAddr());
+
+    // ensure we do not have something waiting to retry
+    if(retryReq)
+        return false;
+
+    DPRINTF(Bridge, "Response queue size: %d outresp: %d\n",
+            transmitList.size(), outstandingResponses);
+
+    if (masterPort.reqQueueFull()) {
+        DPRINTF(Bridge, "Request queue full\n");
+        retryReq = true;
+    } else if (pkt->needsResponse()) {
+        if (respQueueFull()) {
+            DPRINTF(Bridge, "Response queue full\n");
+            retryReq = true;
         } else {
-            DPRINTF(BusBridge, "Request Needs response, reserving space\n");
+            DPRINTF(Bridge, "Reserving space for response\n");
+            assert(outstandingResponses != respQueueLimit);
             ++outstandingResponses;
+            retryReq = false;
+            masterPort.schedTimingReq(pkt, bridge.clockEdge(delay));
         }
+    }
 
-    otherPort->queueForSendTiming(pkt);
-
-    return true;
+    // remember that we are now stalling a packet and that we have to
+    // tell the sending master to retry once space becomes available,
+    // we make no distinction whether the stalling is due to the
+    // request queue or response queue being full
+    return !retryReq;
 }
 
 void
-Bridge::BridgePort::nackRequest(PacketPtr pkt)
+Bridge::BridgeSlavePort::retryStalledReq()
 {
-    // Nack the packet
-    pkt->result = Packet::Nacked;
-    pkt->setDest(pkt->getSrc());
-
-    //put it on the list to send
-    Tick readyTime = curTick + delay;
-    PacketBuffer *buf = new PacketBuffer(pkt, readyTime, true);
-    if (sendQueue.empty()) {
-        sendEvent.schedule(readyTime);
+    if (retryReq) {
+        DPRINTF(Bridge, "Request waiting for retry, now retrying\n");
+        retryReq = false;
+        sendRetry();
     }
-    sendQueue.push_back(buf);
 }
 
-
 void
-Bridge::BridgePort::queueForSendTiming(PacketPtr pkt)
+Bridge::BridgeMasterPort::schedTimingReq(PacketPtr pkt, Tick when)
 {
-   if (pkt->isResponse() || pkt->result == Packet::Nacked) {
-        // This is a response for a request we forwarded earlier.  The
-        // corresponding PacketBuffer should be stored in the packet's
-        // senderState field.
-        PacketBuffer *buf = dynamic_cast<PacketBuffer*>(pkt->senderState);
-        assert(buf != NULL);
-        // set up new packet dest & senderState based on values saved
-        // from original request
-        buf->fixResponse(pkt);
-
-        // Check if this packet was expecting a response (this is either it or
-        // its a nacked packet and we won't be seeing that response)
-        if (buf->expectResponse)
-            --outstandingResponses;
-
-
-        DPRINTF(BusBridge, "restoring  sender state: %#X, from packet buffer: %#X\n",
-                        pkt->senderState, buf);
-        DPRINTF(BusBridge, "  is response, new dest %d\n", pkt->getDest());
-        delete buf;
+    // If we expect to see a response, we need to restore the source
+    // and destination field that is potentially changed by a second
+    // bus
+    if (!pkt->memInhibitAsserted() && pkt->needsResponse()) {
+        // Update the sender state so we can deal with the response
+        // appropriately
+        RequestState *req_state = new RequestState(pkt);
+        pkt->senderState = req_state;
+    }
+
+    // If we're about to put this packet at the head of the queue, we
+    // need to schedule an event to do the transmit.  Otherwise there
+    // should already be an event scheduled for sending the head
+    // packet.
+    if (transmitList.empty()) {
+        bridge.schedule(sendEvent, when);
     }
 
-    Tick readyTime = curTick + delay;
-    PacketBuffer *buf = new PacketBuffer(pkt, readyTime);
-    DPRINTF(BusBridge, "old sender state: %#X, new sender state: %#X\n",
-            buf->origSenderState, buf);
+    assert(transmitList.size() != reqQueueLimit);
+
+    transmitList.push_back(DeferredPacket(pkt, when));
+}
+
+
+void
+Bridge::BridgeSlavePort::schedTimingResp(PacketPtr pkt, Tick when)
+{
+    // This is a response for a request we forwarded earlier.  The
+    // corresponding request state should be stored in the packet's
+    // senderState field.
+    RequestState *req_state = dynamic_cast<RequestState*>(pkt->senderState);
+    assert(req_state != NULL);
+    // set up new packet dest & senderState based on values saved
+    // from original request
+    req_state->fixResponse(pkt);
+    delete req_state;
+
+    // the bridge assumes that at least one bus has set the
+    // destination field of the packet
+    assert(pkt->isDestValid());
+    DPRINTF(Bridge, "response, new dest %d\n", pkt->getDest());
 
     // If we're about to put this packet at the head of the queue, we
     // need to schedule an event to do the transmit.  Otherwise there
     // should already be an event scheduled for sending the head
     // packet.
-    if (sendQueue.empty()) {
-        sendEvent.schedule(readyTime);
+    if (transmitList.empty()) {
+        bridge.schedule(sendEvent, when);
     }
-    ++queuedRequests;
-    sendQueue.push_back(buf);
+
+    transmitList.push_back(DeferredPacket(pkt, when));
 }
 
 void
-Bridge::BridgePort::trySend()
+Bridge::BridgeMasterPort::trySendTiming()
 {
-    assert(!sendQueue.empty());
+    assert(!transmitList.empty());
 
-    bool was_full = queueFull();
-    int pbs = peerBlockSize();
+    DeferredPacket req = transmitList.front();
 
-    PacketBuffer *buf = sendQueue.front();
+    assert(req.tick <= curTick());
 
-    assert(buf->ready <= curTick);
+    PacketPtr pkt = req.pkt;
 
-    PacketPtr pkt = buf->pkt;
+    DPRINTF(Bridge, "trySend request addr 0x%x, queue size %d\n",
+            pkt->getAddr(), transmitList.size());
 
-    pkt->flags &= ~SNOOP_COMMIT; //CLear it if it was set
+    if (sendTimingReq(pkt)) {
+        // send successful
+        transmitList.pop_front();
+        DPRINTF(Bridge, "trySend request successful\n");
 
-    if (pkt->cmd == MemCmd::WriteInvalidateReq && fixPartialWrite &&
-            pkt->result != Packet::Nacked && pkt->getOffset(pbs) &&
-            pkt->getSize() != pbs) {
-        buf->partialWriteFix(this);
-        pkt = buf->pkt;
+        // If there are more packets to send, schedule event to try again.
+        if (!transmitList.empty()) {
+            req = transmitList.front();
+            DPRINTF(Bridge, "Scheduling next send\n");
+            bridge.schedule(sendEvent, std::max(req.tick,
+                                                bridge.nextCycle()));
+        }
+
+        // if we have stalled a request due to a full request queue,
+        // then send a retry at this point, also note that if the
+        // request we stalled was waiting for the response queue
+        // rather than the request queue we might stall it again
+        slavePort.retryStalledReq();
     }
 
-    DPRINTF(BusBridge, "trySend: origSrc %d dest %d addr 0x%x\n",
-            buf->origSrc, pkt->getDest(), pkt->getAddr());
+    // if the send failed, then we try again once we receive a retry,
+    // and therefore there is no need to take any action
+}
+
+void
+Bridge::BridgeSlavePort::trySendTiming()
+{
+    assert(!transmitList.empty());
 
+    DeferredPacket resp = transmitList.front();
 
-    if (sendTiming(pkt)) {
-        // send successful
-        sendQueue.pop_front();
-        buf->pkt = NULL; // we no longer own packet, so it's not safe to look at it
+    assert(resp.tick <= curTick());
 
-        if (buf->expectResponse) {
-            // Must wait for response
-            DPRINTF(BusBridge, "  successful: awaiting response (%d)\n",
-                    outstandingResponses);
-        } else {
-            // no response expected... deallocate packet buffer now.
-            DPRINTF(BusBridge, "  successful: no response expected\n");
-            delete buf;
-        }
+    PacketPtr pkt = resp.pkt;
 
-        if (!buf->nacked)
-                --queuedRequests;
+    DPRINTF(Bridge, "trySend response addr 0x%x, outstanding %d\n",
+            pkt->getAddr(), outstandingResponses);
+
+    if (sendTimingResp(pkt)) {
+        // send successful
+        transmitList.pop_front();
+        DPRINTF(Bridge, "trySend response successful\n");
+
+        assert(outstandingResponses != 0);
+        --outstandingResponses;
 
         // If there are more packets to send, schedule event to try again.
-        if (!sendQueue.empty()) {
-            buf = sendQueue.front();
-            DPRINTF(BusBridge, "Scheduling next send\n");
-            sendEvent.schedule(std::max(buf->ready, curTick + 1));
-        }
-        // Let things start sending again
-        if (was_full && !queueFull()) {
-          DPRINTF(BusBridge, "Queue was full, sending retry\n");
-          otherPort->sendRetry();
+        if (!transmitList.empty()) {
+            resp = transmitList.front();
+            DPRINTF(Bridge, "Scheduling next send\n");
+            bridge.schedule(sendEvent, std::max(resp.tick,
+                                                bridge.nextCycle()));
         }
 
-    } else {
-        DPRINTF(BusBridge, "  unsuccessful\n");
-        buf->undoPartialWriteFix();
+        // if there is space in the request queue and we were stalling
+        // a request, it will definitely be possible to accept it now
+        // since there is guaranteed space in the response queue
+        if (!masterPort.reqQueueFull() && retryReq) {
+            DPRINTF(Bridge, "Request waiting for retry, now retrying\n");
+            retryReq = false;
+            sendRetry();
+        }
     }
-    DPRINTF(BusBridge, "trySend: queue size: %d outreq: %d outstanding resp: %d\n",
-                    sendQueue.size(), queuedRequests, outstandingResponses);
+
+    // if the send failed, then we try again once we receive a retry,
+    // and therefore there is no need to take any action
 }
 
+void
+Bridge::BridgeMasterPort::recvRetry()
+{
+    Tick nextReady = transmitList.front().tick;
+    if (nextReady <= curTick())
+        trySendTiming();
+    else
+        bridge.schedule(sendEvent, nextReady);
+}
 
 void
-Bridge::BridgePort::recvRetry()
+Bridge::BridgeSlavePort::recvRetry()
 {
-    trySend();
+    Tick nextReady = transmitList.front().tick;
+    if (nextReady <= curTick())
+        trySendTiming();
+    else
+        bridge.schedule(sendEvent, nextReady);
 }
 
-/** Function called by the port when the bus is receiving a Atomic
- * transaction.*/
 Tick
-Bridge::BridgePort::recvAtomic(PacketPtr pkt)
+Bridge::BridgeSlavePort::recvAtomic(PacketPtr pkt)
 {
-    return otherPort->sendAtomic(pkt) + delay;
+    return delay * bridge.clockPeriod() + masterPort.sendAtomic(pkt);
 }
 
-/** Function called by the port when the bus is receiving a Functional
- * transaction.*/
 void
-Bridge::BridgePort::recvFunctional(PacketPtr pkt)
+Bridge::BridgeSlavePort::recvFunctional(PacketPtr pkt)
 {
-    std::list<PacketBuffer*>::iterator i;
-    bool pktContinue = true;
+    std::list<DeferredPacket>::iterator i;
+
+    pkt->pushLabel(name());
 
-    for (i = sendQueue.begin();  i != sendQueue.end(); ++i) {
-        if (pkt->intersect((*i)->pkt)) {
-            pktContinue &= fixPacket(pkt, (*i)->pkt);
+    // check the response queue
+    for (i = transmitList.begin();  i != transmitList.end(); ++i) {
+        if (pkt->checkFunctional((*i).pkt)) {
+            pkt->makeResponse();
+            return;
         }
     }
 
-    if (pktContinue) {
-        otherPort->sendFunctional(pkt);
+    // also check the master port's request queue
+    if (masterPort.checkFunctional(pkt)) {
+        return;
     }
-}
 
-/** Function called by the port when the bus is receiving a status change.*/
-void
-Bridge::BridgePort::recvStatusChange(Port::Status status)
-{
-    otherPort->sendStatusChange(status);
-}
+    pkt->popLabel();
 
-void
-Bridge::BridgePort::getDeviceAddressRanges(AddrRangeList &resp,
-                                           AddrRangeList &snoop)
-{
-    otherPort->getPeerAddressRanges(resp, snoop);
+    // fall through if pkt still not satisfied
+    masterPort.sendFunctional(pkt);
 }
 
-BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bridge)
-
-   Param<int> queue_size_a;
-   Param<int> queue_size_b;
-   Param<Tick> delay;
-   Param<bool> write_ack;
-   Param<bool> fix_partial_write_a;
-   Param<bool> fix_partial_write_b;
-
-END_DECLARE_SIM_OBJECT_PARAMS(Bridge)
-
-BEGIN_INIT_SIM_OBJECT_PARAMS(Bridge)
+bool
+Bridge::BridgeMasterPort::checkFunctional(PacketPtr pkt)
+{
+    bool found = false;
+    std::list<DeferredPacket>::iterator i = transmitList.begin();
 
-    INIT_PARAM(queue_size_a, "The size of the queue for data coming into side a"),
-    INIT_PARAM(queue_size_b, "The size of the queue for data coming into side b"),
-    INIT_PARAM(delay, "The miminum delay to cross this bridge"),
-    INIT_PARAM(write_ack, "Acknowledge any writes that are received."),
-    INIT_PARAM(fix_partial_write_a, "Fixup any partial block writes that are received"),
-    INIT_PARAM(fix_partial_write_b, "Fixup any partial block writes that are received")
+    while(i != transmitList.end() && !found) {
+        if (pkt->checkFunctional((*i).pkt)) {
+            pkt->makeResponse();
+            found = true;
+        }
+        ++i;
+    }
 
-END_INIT_SIM_OBJECT_PARAMS(Bridge)
+    return found;
+}
 
-CREATE_SIM_OBJECT(Bridge)
+AddrRangeList
+Bridge::BridgeSlavePort::getAddrRanges() const
 {
-    return new Bridge(getInstanceName(), queue_size_a, queue_size_b, delay,
-            write_ack, fix_partial_write_a, fix_partial_write_b);
+    return ranges;
 }
 
-REGISTER_SIM_OBJECT("Bridge", Bridge)
-
+Bridge *
+BridgeParams::create()
+{
+    return new Bridge(this);
+}