Ruby Memory Vector: Allow more than 4GB of memory
[gem5.git] / src / mem / bus.cc
index 5be53dbcd03cd2bfc2d1906d609dc97bbe02adc3..829d694de3bfdfff1cb41c5c5b2e688f2f9d1526 100644 (file)
 #include "base/trace.hh"
 #include "debug/Bus.hh"
 #include "debug/BusAddrRanges.hh"
+#include "debug/Drain.hh"
 #include "mem/bus.hh"
 
 BaseBus::BaseBus(const BaseBusParams *p)
-    : MemObject(p), clock(p->clock),
-      headerCycles(p->header_cycles), width(p->width), tickNextIdle(0),
-      drainEvent(NULL), busIdleEvent(this), inRetry(false),
+    : MemObject(p),
+      headerCycles(p->header_cycles), width(p->width),
       defaultPortID(InvalidPortID),
       useDefaultRange(p->use_default_range),
       defaultBlockSize(p->block_size),
@@ -113,10 +113,7 @@ BaseBus::calcPacketTiming(PacketPtr pkt)
 {
     // determine the current time rounded to the closest following
     // clock edge
-    Tick now = curTick();
-    if (now % clock != 0) {
-        now = ((now / clock) + 1) * clock;
-    }
+    Tick now = nextCycle();
 
     Tick headerTime = now + headerCycles * clock;
 
@@ -140,54 +137,106 @@ BaseBus::calcPacketTiming(PacketPtr pkt)
     return headerTime;
 }
 
-void BaseBus::occupyBus(Tick until)
+template <typename PortClass>
+BaseBus::Layer<PortClass>::Layer(BaseBus& _bus, const std::string& _name,
+                                 Tick _clock) :
+    bus(_bus), _name(_name), state(IDLE), clock(_clock), drainEvent(NULL),
+    releaseEvent(this)
 {
-    if (until == 0) {
-        // shortcut for express snoop packets
-        return;
-    }
-
-    tickNextIdle = until;
-    reschedule(busIdleEvent, tickNextIdle, true);
+}
 
-    DPRINTF(BaseBus, "The bus is now occupied from tick %d to %d\n",
-            curTick(), tickNextIdle);
+template <typename PortClass>
+void BaseBus::Layer<PortClass>::occupyLayer(Tick until)
+{
+    // ensure the state is busy or in retry and never idle at this
+    // point, as the bus should transition from idle as soon as it has
+    // decided to forward the packet to prevent any follow-on calls to
+    // sendTiming seeing an unoccupied bus
+    assert(state != IDLE);
+
+    // note that we do not change the bus state here, if we are going
+    // from idle to busy it is handled by tryTiming, and if we
+    // are in retry we should remain in retry such that
+    // succeededTiming still sees the accurate state
+
+    // until should never be 0 as express snoops never occupy the bus
+    assert(until != 0);
+    bus.schedule(releaseEvent, until);
+
+    DPRINTF(BaseBus, "The bus is now busy from tick %d to %d\n",
+            curTick(), until);
 }
 
+template <typename PortClass>
 bool
-BaseBus::isOccupied(Port* port)
+BaseBus::Layer<PortClass>::tryTiming(PortClass* port)
 {
-    // first we see if the next idle tick is in the future, next the
-    // bus is considered occupied if there are ports on the retry list
-    // and we are not in a retry with the current port
-    if (tickNextIdle > curTick() ||
-        (!retryList.empty() && !(inRetry && port == retryList.front()))) {
-        addToRetryList(port);
-        return true;
+    // first we see if the bus is busy, next we check if we are in a
+    // retry with a port other than the current one
+    if (state == BUSY || (state == RETRY && port != retryList.front())) {
+        // put the port at the end of the retry list
+        retryList.push_back(port);
+        return false;
     }
-    return false;
+
+    // update the state which is shared for request, response and
+    // snoop responses, if we were idle we are now busy, if we are in
+    // a retry, then do not change
+    if (state == IDLE)
+        state = BUSY;
+
+    return true;
 }
 
+template <typename PortClass>
 void
-BaseBus::succeededTiming(Tick busy_time)
+BaseBus::Layer<PortClass>::succeededTiming(Tick busy_time)
 {
-    // occupy the bus accordingly
-    occupyBus(busy_time);
-
     // if a retrying port succeeded, also take it off the retry list
-    if (inRetry) {
+    if (state == RETRY) {
         DPRINTF(BaseBus, "Remove retry from list %s\n",
                 retryList.front()->name());
         retryList.pop_front();
-        inRetry = false;
+        state = BUSY;
     }
+
+    // we should either have gone from idle to busy in the
+    // tryTiming test, or just gone from a retry to busy
+    assert(state == BUSY);
+
+    // occupy the bus accordingly
+    occupyLayer(busy_time);
 }
 
+template <typename PortClass>
 void
-BaseBus::releaseBus()
+BaseBus::Layer<PortClass>::failedTiming(PortClass* port, Tick busy_time)
+{
+    // if we are not in a retry, i.e. busy (but never idle), or we are
+    // in a retry but not for the current port, then add the port at
+    // the end of the retry list
+    if (state != RETRY || port != retryList.front()) {
+        retryList.push_back(port);
+    }
+
+    // even if we retried the current one and did not succeed,
+    // we are no longer retrying but instead busy
+    state = BUSY;
+
+    // occupy the bus accordingly
+    occupyLayer(busy_time);
+}
+
+template <typename PortClass>
+void
+BaseBus::Layer<PortClass>::releaseLayer()
 {
     // releasing the bus means we should now be idle
-    assert(curTick() >= tickNextIdle);
+    assert(state == BUSY);
+    assert(!releaseEvent.scheduled());
+
+    // update the state
+    state = IDLE;
 
     // bus is now idle, so if someone is waiting we can retry
     if (!retryList.empty()) {
@@ -196,64 +245,68 @@ BaseBus::releaseBus()
         // busy, and in the latter case the bus may be released before
         // we see a retry from the destination
         retryWaiting();
-    }
-
-    //If we weren't able to drain before, we might be able to now.
-    if (drainEvent && retryList.empty() && curTick() >= tickNextIdle) {
+    } else if (drainEvent) {
+        DPRINTF(Drain, "Bus done draining, processing drain event\n");
+        //If we weren't able to drain before, do it now.
         drainEvent->process();
         // Clear the drain event once we're done with it.
         drainEvent = NULL;
     }
 }
 
+template <typename PortClass>
 void
-BaseBus::retryWaiting()
+BaseBus::Layer<PortClass>::retryWaiting()
 {
     // this should never be called with an empty retry list
     assert(!retryList.empty());
 
-    // send a retry to the port at the head of the retry list
-    inRetry = true;
+    // we always go to retrying from idle
+    assert(state == IDLE);
+
+    // update the state which is shared for request, response and
+    // snoop responses
+    state = RETRY;
 
     // note that we might have blocked on the receiving port being
     // busy (rather than the bus itself) and now call retry before the
     // destination called retry on the bus
-    if (dynamic_cast<SlavePort*>(retryList.front()) != NULL)
-        (dynamic_cast<SlavePort*>(retryList.front()))->sendRetry();
-    else
-        (dynamic_cast<MasterPort*>(retryList.front()))->sendRetry();
-
-    // If inRetry is still true, sendTiming wasn't called in zero time
-    // (e.g. the cache does this)
-    if (inRetry) {
-        retryList.pop_front();
-        inRetry = false;
+    retryList.front()->sendRetry();
 
-        //Bring tickNextIdle up to the present
-        while (tickNextIdle < curTick())
-            tickNextIdle += clock;
+    // If the bus is still in the retry state, sendTiming wasn't
+    // called in zero time (e.g. the cache does this)
+    if (state == RETRY) {
+        retryList.pop_front();
 
         //Burn a cycle for the missed grant.
-        tickNextIdle += clock;
 
-        reschedule(busIdleEvent, tickNextIdle, true);
+        // update the state which is shared for request, response and
+        // snoop responses
+        state = BUSY;
+
+        // determine the current time rounded to the closest following
+        // clock edge
+        Tick now = bus.nextCycle();
+
+        occupyLayer(now + clock);
     }
 }
 
+template <typename PortClass>
 void
-BaseBus::recvRetry()
+BaseBus::Layer<PortClass>::recvRetry()
 {
     // we got a retry from a peer that we tried to send something to
     // and failed, but we sent it on the account of someone else, and
     // that source port should be on our retry list, however if the
-    // bus is released before this happens and the retry (from the bus
-    // point of view) is successful then this no longer holds and we
-    // could in fact have an empty retry list
+    // bus layer is released before this happens and the retry (from
+    // the bus point of view) is successful then this no longer holds
+    // and we could in fact have an empty retry list
     if (retryList.empty())
         return;
 
-    // if the bus isn't busy
-    if (curTick() >= tickNextIdle) {
+    // if the bus layer is idle
+    if (state == IDLE) {
         // note that we do not care who told us to retry at the moment, we
         // merely let the first one on the retry list go
         retryWaiting();
@@ -269,7 +322,7 @@ BaseBus::findPort(Addr addr)
         return dest_id;
 
     // Check normal port ranges
-    PortIter i = portMap.find(RangeSize(addr,1));
+    PortMapConstIter i = portMap.find(RangeSize(addr,1));
     if (i != portMap.end()) {
         dest_id = i->second;
         updatePortCache(dest_id, i->first.start, i->first.end);
@@ -278,8 +331,8 @@ BaseBus::findPort(Addr addr)
 
     // Check if this matches the default range
     if (useDefaultRange) {
-        AddrRangeIter a_end = defaultRange.end();
-        for (AddrRangeIter i = defaultRange.begin(); i != a_end; i++) {
+        AddrRangeConstIter a_end = defaultRange.end();
+        for (AddrRangeConstIter i = defaultRange.begin(); i != a_end; i++) {
             if (*i == addr) {
                 DPRINTF(BusAddrRanges, "  found addr %#llx on default\n",
                         addr);
@@ -332,7 +385,7 @@ BaseBus::recvRangeChange(PortID master_port_id)
         MasterPort *port = masterPorts[master_port_id];
 
         // Clean out any previously existent ids
-        for (PortIter portIter = portMap.begin();
+        for (PortMapIter portIter = portMap.begin();
              portIter != portMap.end(); ) {
             if (portIter->second == master_port_id)
                 portMap.erase(portIter++);
@@ -367,22 +420,22 @@ BaseBus::recvRangeChange(PortID master_port_id)
 }
 
 AddrRangeList
-BaseBus::getAddrRanges()
+BaseBus::getAddrRanges() const
 {
     AddrRangeList ranges;
 
     DPRINTF(BusAddrRanges, "received address range request, returning:\n");
 
-    for (AddrRangeIter dflt_iter = defaultRange.begin();
+    for (AddrRangeConstIter dflt_iter = defaultRange.begin();
          dflt_iter != defaultRange.end(); dflt_iter++) {
         ranges.push_back(*dflt_iter);
         DPRINTF(BusAddrRanges, "  -- Dflt: %#llx : %#llx\n",dflt_iter->start,
                 dflt_iter->end);
     }
-    for (PortIter portIter = portMap.begin();
+    for (PortMapConstIter portIter = portMap.begin();
          portIter != portMap.end(); portIter++) {
         bool subset = false;
-        for (AddrRangeIter dflt_iter = defaultRange.begin();
+        for (AddrRangeConstIter dflt_iter = defaultRange.begin();
              dflt_iter != defaultRange.end(); dflt_iter++) {
             if ((portIter->first.start < dflt_iter->start &&
                 portIter->first.end >= dflt_iter->start) ||
@@ -438,24 +491,25 @@ BaseBus::findBlockSize()
     return max_bs;
 }
 
-
+template <typename PortClass>
 unsigned int
-BaseBus::drain(Event * de)
+BaseBus::Layer<PortClass>::drain(Event * de)
 {
     //We should check that we're not "doing" anything, and that noone is
     //waiting. We might be idle but have someone waiting if the device we
     //contacted for a retry didn't actually retry.
-    if (!retryList.empty() || (curTick() < tickNextIdle &&
-                               busIdleEvent.scheduled())) {
+    if (!retryList.empty() || state != IDLE) {
+        DPRINTF(Drain, "Bus not drained\n");
         drainEvent = de;
         return 1;
     }
     return 0;
 }
 
-void
-BaseBus::startup()
-{
-    if (tickNextIdle < curTick())
-        tickNextIdle = (curTick() / clock) * clock + clock;
-}
+/**
+ * Bus layer template instantiations. Could be removed with _impl.hh
+ * file, but since there are only two given options (MasterPort and
+ * SlavePort) it seems a bit excessive at this point.
+ */
+template class BaseBus::Layer<SlavePort>;
+template class BaseBus::Layer<MasterPort>;