Make Bus::findPort() a little more useful.
[gem5.git] / src / mem / bus.cc
index 6b5b63f504a4377a7994ec91240d7c677c4959da..24a0c6f02319a19f32835438bbd2180f8164b960 100644 (file)
@@ -33,6 +33,8 @@
  * Definition of a bus object.
  */
 
+#include <algorithm>
+#include <limits>
 
 #include "base/misc.hh"
 #include "base/trace.hh"
@@ -46,26 +48,53 @@ Bus::getPort(const std::string &if_name, int idx)
         if (defaultPort == NULL) {
             defaultPort = new BusPort(csprintf("%s-default",name()), this,
                                       defaultId);
+            cachedBlockSizeValid = false;
             return defaultPort;
         } else
             fatal("Default port already set\n");
     }
+    int id;
+    if (if_name == "functional") {
+        if (!funcPort) {
+            id = maxId++;
+            funcPort = new BusPort(csprintf("%s-p%d-func", name(), id), this, id);
+            funcPortId = id;
+            interfaces[id] = funcPort;
+        }
+        return funcPort;
+    }
 
     // if_name ignored?  forced to be empty?
-    int id = interfaces.size();
+    id = maxId++;
+    assert(maxId < std::numeric_limits<typeof(maxId)>::max());
     BusPort *bp = new BusPort(csprintf("%s-p%d", name(), id), this, id);
-    interfaces.push_back(bp);
+    interfaces[id] = bp;
+    cachedBlockSizeValid = false;
     return bp;
 }
 
+void
+Bus::deletePortRefs(Port *p)
+{
+
+    BusPort *bp =  dynamic_cast<BusPort*>(p);
+    if (bp == NULL)
+        panic("Couldn't convert Port* to BusPort*\n");
+    // If this is our one functional port
+    if (funcPort == bp)
+        return;
+    interfaces.erase(bp->getId());
+    delete bp;
+}
+
 /** Get the ranges of anyone other buses that we are connected to. */
 void
 Bus::init()
 {
-    std::vector<BusPort*>::iterator intIter;
+    m5::hash_map<short,BusPort*>::iterator intIter;
 
     for (intIter = interfaces.begin(); intIter != interfaces.end(); intIter++)
-        (*intIter)->sendStatusChange(Port::RangeChange);
+        intIter->second->sendStatusChange(Port::RangeChange);
 }
 
 Bus::BusFreeEvent::BusFreeEvent(Bus *_bus) : Event(&mainEventQueue), bus(_bus)
@@ -86,11 +115,14 @@ void Bus::occupyBus(PacketPtr pkt)
     //Bring tickNextIdle up to the present tick
     //There is some potential ambiguity where a cycle starts, which might make
     //a difference when devices are acting right around a cycle boundary. Using
-    //a < allows things which happen exactly on a cycle boundary to take up only
-    //the following cycle. Anthing that happens later will have to "wait" for
-    //the end of that cycle, and then start using the bus after that.
-    while (tickNextIdle < curTick)
-        tickNextIdle += clock;
+    //a < allows things which happen exactly on a cycle boundary to take up
+    //only the following cycle. Anything that happens later will have to "wait"
+    //for the end of that cycle, and then start using the bus after that.
+    if (tickNextIdle < curTick) {
+        tickNextIdle = curTick;
+        if (tickNextIdle % clock != 0)
+            tickNextIdle = curTick - (curTick % clock) + clock;
+    }
 
     // The packet will be sent. Figure out how long it occupies the bus, and
     // how much of that time is for the first "word", aka bus width.
@@ -103,10 +135,9 @@ void Bus::occupyBus(PacketPtr pkt)
         // We're using the "adding instead of dividing" trick again here
         if (pkt->hasData()) {
             int dataSize = pkt->getSize();
-            for (int transmitted = 0; transmitted < dataSize;
-                    transmitted += width) {
+            numCycles += dataSize/width;
+            if (dataSize % width)
                 numCycles++;
-            }
         } else {
             // If the packet didn't have data, it must have been a response.
             // Those use the bus for one cycle to send their data.
@@ -141,7 +172,7 @@ void Bus::occupyBus(PacketPtr pkt)
 bool
 Bus::recvTiming(PacketPtr pkt)
 {
-    Port *port;
+    int port_id;
     DPRINTF(Bus, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
             pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
 
@@ -153,51 +184,47 @@ Bus::recvTiming(PacketPtr pkt)
     // If the bus is busy, or other devices are in line ahead of the current
     // one, put this device on the retry list.
     if (tickNextIdle > curTick ||
-            (retryList.size() && (!inRetry || pktPort != retryList.front()))) {
+        (retryList.size() && (!inRetry || pktPort != retryList.front())))
+    {
         addToRetryList(pktPort);
+        DPRINTF(Bus, "recvTiming: Bus is busy, returning false\n");
         return false;
     }
 
     short dest = pkt->getDest();
+
+    // Make sure to clear the snoop commit flag so it doesn't think an
+    // access has been handled twice.
     if (dest == Packet::Broadcast) {
-        port = findPort(pkt->getAddr(), pkt->getSrc());
-        if (timingSnoop(pkt, port ? port : interfaces[pkt->getSrc()])) {
-            bool success;
-
-            pkt->flags |= SNOOP_COMMIT;
-            success = timingSnoop(pkt, port ? port : interfaces[pkt->getSrc()]);
-            assert(success);
-
-            if (pkt->flags & SATISFIED) {
-                //Cache-Cache transfer occuring
-                if (inRetry) {
-                    retryList.front()->onRetryList(false);
-                    retryList.pop_front();
-                    inRetry = false;
-                }
-                occupyBus(pkt);
-                return true;
+        port_id = findPort(pkt->getAddr());
+        timingSnoop(pkt, interfaces[port_id]);
+
+        if (pkt->memInhibitAsserted()) {
+            //Cache-Cache transfer occuring
+            if (inRetry) {
+                retryList.front()->onRetryList(false);
+                retryList.pop_front();
+                inRetry = false;
             }
-        } else {
-            //Snoop didn't succeed
-            DPRINTF(Bus, "Adding a retry to RETRY list %i\n", pktPort);
-            addToRetryList(pktPort);
-            return false;
+            occupyBus(pkt);
+            DPRINTF(Bus, "recvTiming: Packet sucessfully sent\n");
+            return true;
         }
     } else {
-        assert(dest >= 0 && dest < interfaces.size());
+        assert(dest >= 0 && dest < maxId);
         assert(dest != pkt->getSrc()); // catch infinite loops
-        port = interfaces[dest];
+        port_id = dest;
     }
 
     occupyBus(pkt);
 
-    if (port) {
-        if (port->sendTiming(pkt))  {
+    if (port_id != pkt->getSrc()) {
+        if (interfaces[port_id]->sendTiming(pkt))  {
             // Packet was successfully sent. Return true.
             // Also take care of retries
             if (inRetry) {
-                DPRINTF(Bus, "Remove retry from list %i\n", retryList.front());
+                DPRINTF(Bus, "Remove retry from list %d\n",
+                        retryList.front()->getId());
                 retryList.front()->onRetryList(false);
                 retryList.pop_front();
                 inRetry = false;
@@ -206,12 +233,14 @@ Bus::recvTiming(PacketPtr pkt)
         }
 
         // Packet not successfully sent. Leave or put it on the retry list.
-        DPRINTF(Bus, "Adding a retry to RETRY list %i\n", pktPort);
+        DPRINTF(Bus, "Adding2 a retry to RETRY list %d\n",
+                pktPort->getId());
         addToRetryList(pktPort);
         return false;
     }
     else {
         //Forwarding up from responder, just return true;
+        DPRINTF(Bus, "recvTiming: can we be here?\n");
         return true;
     }
 }
@@ -219,12 +248,11 @@ Bus::recvTiming(PacketPtr pkt)
 void
 Bus::recvRetry(int id)
 {
-    DPRINTF(Bus, "Received a retry\n");
     // If there's anything waiting, and the bus isn't busy...
     if (retryList.size() && curTick >= tickNextIdle) {
         //retryingPort = retryList.front();
         inRetry = true;
-        DPRINTF(Bus, "Sending a retry\n");
+        DPRINTF(Bus, "Sending a retry to %s\n", retryList.front()->getPeer()->name());
         retryList.front()->sendRetry();
         // If inRetry is still true, sendTiming wasn't called
         if (inRetry)
@@ -240,11 +268,7 @@ Bus::recvRetry(int id)
             //Burn a cycle for the missed grant.
             tickNextIdle += clock;
 
-            if (!busIdle.scheduled()) {
-                busIdle.schedule(tickNextIdle);
-            } else {
-                busIdle.reschedule(tickNextIdle);
-            }
+            busIdle.reschedule(tickNextIdle, true);
         }
     }
     //If we weren't able to drain before, we might be able to now.
@@ -255,31 +279,23 @@ Bus::recvRetry(int id)
     }
 }
 
-Port *
-Bus::findPort(Addr addr, int id)
+int
+Bus::findPort(Addr addr)
 {
     /* An interval tree would be a better way to do this. --ali. */
     int dest_id = -1;
-    int i = 0;
-    bool found = false;
-    AddrRangeIter iter;
 
-    while (i < portList.size() && !found)
-    {
-        if (portList[i].range == addr) {
-            dest_id = portList[i].portId;
-            found = true;
-            DPRINTF(Bus, "  found addr %#llx on device %d\n", addr, dest_id);
-        }
-        i++;
-    }
+    PortIter i = portMap.find(RangeSize(addr,1));
+    if (i != portMap.end())
+        dest_id = i->second;
 
     // Check if this matches the default range
     if (dest_id == -1) {
-        for (iter = defaultRange.begin(); iter != defaultRange.end(); iter++) {
+        for (AddrRangeIter iter = defaultRange.begin();
+             iter != defaultRange.end(); iter++) {
             if (*iter == addr) {
                 DPRINTF(Bus, "  found addr %#llx on default\n", addr);
-                return defaultPort;
+                return defaultId;
             }
         }
 
@@ -290,102 +306,51 @@ Bus::findPort(Addr addr, int id)
             DPRINTF(Bus, "Unable to find destination for addr: %#llx, will use "
                     "default port", addr);
 
-            return defaultPort;
-        }
-    }
-
-
-    // we shouldn't be sending this back to where it came from
-    // only on a functional access and then we should terminate
-    // the cyclical call.
-    if (dest_id == id)
-        return 0;
-
-    return interfaces[dest_id];
-}
-
-std::vector<int>
-Bus::findSnoopPorts(Addr addr, int id)
-{
-    int i = 0;
-    AddrRangeIter iter;
-    std::vector<int> ports;
-
-    while (i < portSnoopList.size())
-    {
-        if (portSnoopList[i].range == addr && portSnoopList[i].portId != id) {
-            //Careful  to not overlap ranges
-            //or snoop will be called more than once on the port
-
-            //@todo Fix this hack because ranges are overlapping
-            //need to make sure we dont't create overlapping ranges
-            bool hack_overlap = false;
-            int size = ports.size();
-            for (int j=0; j < size; j++) {
-                if (ports[j] == portSnoopList[i].portId)
-                    hack_overlap = true;
-            }
-
-            if (!hack_overlap)
-                ports.push_back(portSnoopList[i].portId);
-//            DPRINTF(Bus, "  found snoop addr %#llx on device%d\n", addr,
-//                    portSnoopList[i].portId);
+            return defaultId;
         }
-        i++;
     }
-    return ports;
-}
 
-Tick
-Bus::atomicSnoop(PacketPtr pkt, Port *responder)
-{
-    std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
-    Tick response_time = 0;
-
-    while (!ports.empty())
-    {
-        if (interfaces[ports.back()] != responder) {
-            Tick response = interfaces[ports.back()]->sendAtomic(pkt);
-            if (response) {
-                assert(!response_time);  //Multiple responders
-                response_time = response;
-            }
-        }
-        ports.pop_back();
-    }
-    return response_time;
+    return dest_id;
 }
 
 void
 Bus::functionalSnoop(PacketPtr pkt, Port *responder)
 {
-    std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
-
-    //The packet may be changed by another bus on snoops, restore the id after each
-    int id = pkt->getSrc();
-    while (!ports.empty() && pkt->result != Packet::Success)
-    {
-        if (interfaces[ports.back()] != responder)
-            interfaces[ports.back()]->sendFunctional(pkt);
-        ports.pop_back();
-        pkt->setSrc(id);
+    // The packet may be changed by another bus on snoops, restore the
+    // id after each
+    int src_id = pkt->getSrc();
+
+    assert(pkt->isRequest()); // hasn't already been satisfied
+
+    for (SnoopIter s_iter = snoopPorts.begin();
+         s_iter != snoopPorts.end();
+         s_iter++) {
+        BusPort *p = *s_iter;
+        if (p != responder && p->getId() != src_id) {
+            p->sendFunctional(pkt);
+        }
+        if (pkt->isResponse()) {
+            break;
+        }
+        pkt->setSrc(src_id);
     }
 }
 
 bool
 Bus::timingSnoop(PacketPtr pkt, Port* responder)
 {
-    std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
-    bool success = true;
-
-    while (!ports.empty() && success)
-    {
-        if (interfaces[ports.back()] != responder) //Don't call if responder also, once will do
-            success = interfaces[ports.back()]->sendTiming(pkt);
-        ports.pop_back();
+    for (SnoopIter s_iter = snoopPorts.begin();
+         s_iter != snoopPorts.end();
+         s_iter++) {
+        BusPort *p = *s_iter;
+        if (p != responder && p->getId() != pkt->getSrc()) {
+            bool success = p->sendTiming(pkt);
+            if (!success)
+                return false;
+        }
     }
 
-    return success;
+    return true;
 }
 
 
@@ -397,21 +362,64 @@ Bus::recvAtomic(PacketPtr pkt)
     DPRINTF(Bus, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
             pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
     assert(pkt->getDest() == Packet::Broadcast);
-    pkt->flags |= SNOOP_COMMIT;
+    assert(pkt->isRequest());
+
+    // Variables for recording original command and snoop response (if
+    // any)... if a snooper respondes, we will need to restore
+    // original command so that additional snoops can take place
+    // properly
+    MemCmd orig_cmd = pkt->cmd;
+    MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
+    Tick snoop_response_latency = 0;
+    int orig_src = pkt->getSrc();
+
+    int target_port_id = findPort(pkt->getAddr());
+    Port *target_port = interfaces[target_port_id];
+
+    SnoopIter s_end = snoopPorts.end();
+    for (SnoopIter s_iter = snoopPorts.begin(); s_iter != s_end; s_iter++) {
+        BusPort *p = *s_iter;
+        // same port should not have both target addresses and snooping
+        assert(p != target_port);
+        if (p->getId() != pkt->getSrc()) {
+            Tick latency = p->sendAtomic(pkt);
+            if (pkt->isResponse()) {
+                // response from snoop agent
+                assert(pkt->cmd != orig_cmd);
+                assert(pkt->memInhibitAsserted());
+                // should only happen once
+                assert(snoop_response_cmd == MemCmd::InvalidCmd);
+                // save response state
+                snoop_response_cmd = pkt->cmd;
+                snoop_response_latency = latency;
+                // restore original packet state for remaining snoopers
+                pkt->cmd = orig_cmd;
+                pkt->setSrc(orig_src);
+                pkt->setDest(Packet::Broadcast);
+            }
+        }
+    }
 
-    // Assume one bus cycle in order to get through.  This may have
-    // some clock skew issues yet again...
-    pkt->finishTime = curTick + clock;
+    Tick response_latency = 0;
 
-    Port *port = findPort(pkt->getAddr(), pkt->getSrc());
-    Tick snoopTime = atomicSnoop(pkt, port ? port : interfaces[pkt->getSrc()]);
+    // we can get requests sent up from the memory side of the bus for
+    // snooping... don't send them back down!
+    if (target_port_id != pkt->getSrc()) {
+        response_latency = target_port->sendAtomic(pkt);
+    }
 
-    if (snoopTime)
-        return snoopTime;  //Snoop satisfies it
-    else if (port)
-        return port->sendAtomic(pkt);
-    else
-        return 0;
+    // if we got a response from a snooper, restore it here
+    if (snoop_response_cmd != MemCmd::InvalidCmd) {
+        // no one else should have responded
+        assert(!pkt->isResponse());
+        assert(pkt->cmd == orig_cmd);
+        pkt->cmd = snoop_response_cmd;
+        response_latency = snoop_response_latency;
+    }
+
+    // why do we have this packet field and the return value both???
+    pkt->finishTime = curTick + response_latency;
+    return response_latency;
 }
 
 /** Function called by the port when the bus is receiving a Functional
@@ -422,13 +430,13 @@ Bus::recvFunctional(PacketPtr pkt)
     DPRINTF(Bus, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
             pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
     assert(pkt->getDest() == Packet::Broadcast);
-    pkt->flags |= SNOOP_COMMIT;
 
-    Port* port = findPort(pkt->getAddr(), pkt->getSrc());
-    functionalSnoop(pkt, port ? port : interfaces[pkt->getSrc()]);
+    int port_id = findPort(pkt->getAddr());
+    Port *port = interfaces[port_id];
+    functionalSnoop(pkt, port);
 
-    // If the snooping found what we were looking for, we're done.
-    if (pkt->result != Packet::Success && port) {
+    // If the snooping hasn't found what we were looking for, keep going.
+    if (!pkt->isResponse() && port_id != pkt->getSrc()) {
         port->sendFunctional(pkt);
     }
 }
@@ -438,8 +446,7 @@ void
 Bus::recvStatusChange(Port::Status status, int id)
 {
     AddrRangeList ranges;
-    AddrRangeList snoops;
-    int x;
+    bool snoops;
     AddrRangeIter iter;
 
     assert(status == Port::RangeChange &&
@@ -452,7 +459,7 @@ Bus::recvStatusChange(Port::Status status, int id)
         // Only try to update these ranges if the user set a default responder.
         if (responderSet) {
             defaultPort->getPeerAddressRanges(ranges, snoops);
-            assert(snoops.size() == 0);
+            assert(snoops == false);
             for(iter = ranges.begin(); iter != ranges.end(); iter++) {
                 defaultRange.push_back(*iter);
                 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for default range\n",
@@ -461,116 +468,134 @@ Bus::recvStatusChange(Port::Status status, int id)
         }
     } else {
 
-        assert((id < interfaces.size() && id >= 0) || id == defaultId);
-        Port *port = interfaces[id];
-        std::vector<DevMap>::iterator portIter;
-        std::vector<DevMap>::iterator snoopIter;
+        assert((id < maxId && id >= 0) || id == defaultId);
+        BusPort *port = interfaces[id];
 
         // Clean out any previously existent ids
-        for (portIter = portList.begin(); portIter != portList.end(); ) {
-            if (portIter->portId == id)
-                portIter = portList.erase(portIter);
+        for (PortIter portIter = portMap.begin();
+             portIter != portMap.end(); ) {
+            if (portIter->second == id)
+                portMap.erase(portIter++);
             else
                 portIter++;
         }
 
-        for (snoopIter = portSnoopList.begin(); snoopIter != portSnoopList.end(); ) {
-            if (snoopIter->portId == id)
-                snoopIter = portSnoopList.erase(snoopIter);
+        for (SnoopIter s_iter = snoopPorts.begin();
+             s_iter != snoopPorts.end(); ) {
+            if ((*s_iter)->getId() == id)
+                s_iter = snoopPorts.erase(s_iter);
             else
-                snoopIter++;
+                s_iter++;
         }
 
         port->getPeerAddressRanges(ranges, snoops);
 
-        for(iter = snoops.begin(); iter != snoops.end(); iter++) {
-            DevMap dm;
-            dm.portId = id;
-            dm.range = *iter;
-
-            //@todo, make sure we don't overlap ranges
-            DPRINTF(BusAddrRanges, "Adding snoop range %#llx - %#llx for id %d\n",
-                    dm.range.start, dm.range.end, id);
-            portSnoopList.push_back(dm);
+        if (snoops) {
+            DPRINTF(BusAddrRanges, "Adding id %d to snoop list\n", id);
+            snoopPorts.push_back(port);
         }
 
-        for(iter = ranges.begin(); iter != ranges.end(); iter++) {
-            DevMap dm;
-            dm.portId = id;
-            dm.range = *iter;
-
+        for (iter = ranges.begin(); iter != ranges.end(); iter++) {
             DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for id %d\n",
-                    dm.range.start, dm.range.end, id);
-            portList.push_back(dm);
+                    iter->start, iter->end, id);
+            if (portMap.insert(*iter, id) == portMap.end())
+                panic("Two devices with same range\n");
+
         }
     }
-    DPRINTF(MMU, "port list has %d entries\n", portList.size());
+    DPRINTF(MMU, "port list has %d entries\n", portMap.size());
 
     // tell all our peers that our address range has changed.
     // Don't tell the device that caused this change, it already knows
-    for (x = 0; x < interfaces.size(); x++)
-        if (x != id)
-            interfaces[x]->sendStatusChange(Port::RangeChange);
+    m5::hash_map<short,BusPort*>::iterator intIter;
+
+    for (intIter = interfaces.begin(); intIter != interfaces.end(); intIter++)
+        if (intIter->first != id && intIter->first != funcPortId)
+            intIter->second->sendStatusChange(Port::RangeChange);
 
     if (id != defaultId && defaultPort)
         defaultPort->sendStatusChange(Port::RangeChange);
 }
 
 void
-Bus::addressRanges(AddrRangeList &resp, AddrRangeList &snoop, int id)
+Bus::addressRanges(AddrRangeList &resp, bool &snoop, int id)
 {
-    std::vector<DevMap>::iterator portIter;
-    AddrRangeIter dflt_iter;
-    bool subset;
-
     resp.clear();
-    snoop.clear();
+    snoop = false;
 
     DPRINTF(BusAddrRanges, "received address range request, returning:\n");
 
-    for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
-            dflt_iter++) {
+    for (AddrRangeIter dflt_iter = defaultRange.begin();
+         dflt_iter != defaultRange.end(); dflt_iter++) {
         resp.push_back(*dflt_iter);
         DPRINTF(BusAddrRanges, "  -- Dflt: %#llx : %#llx\n",dflt_iter->start,
                 dflt_iter->end);
     }
-    for (portIter = portList.begin(); portIter != portList.end(); portIter++) {
-        subset = false;
-        for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
-                dflt_iter++) {
-            if ((portIter->range.start < dflt_iter->start &&
-                portIter->range.end >= dflt_iter->start) ||
-               (portIter->range.start < dflt_iter->end &&
-                portIter->range.end >= dflt_iter->end))
+    for (PortIter portIter = portMap.begin();
+         portIter != portMap.end(); portIter++) {
+        bool subset = false;
+        for (AddrRangeIter dflt_iter = defaultRange.begin();
+             dflt_iter != defaultRange.end(); dflt_iter++) {
+            if ((portIter->first.start < dflt_iter->start &&
+                portIter->first.end >= dflt_iter->start) ||
+               (portIter->first.start < dflt_iter->end &&
+                portIter->first.end >= dflt_iter->end))
                 fatal("Devices can not set ranges that itersect the default set\
                         but are not a subset of the default set.\n");
-            if (portIter->range.start >= dflt_iter->start &&
-                portIter->range.end <= dflt_iter->end) {
+            if (portIter->first.start >= dflt_iter->start &&
+                portIter->first.end <= dflt_iter->end) {
                 subset = true;
                 DPRINTF(BusAddrRanges, "  -- %#llx : %#llx is a SUBSET\n",
-                    portIter->range.start, portIter->range.end);
+                    portIter->first.start, portIter->first.end);
             }
         }
-        if (portIter->portId != id && !subset) {
-            resp.push_back(portIter->range);
+        if (portIter->second != id && !subset) {
+            resp.push_back(portIter->first);
             DPRINTF(BusAddrRanges, "  -- %#llx : %#llx\n",
-                    portIter->range.start, portIter->range.end);
+                    portIter->first.start, portIter->first.end);
         }
     }
 
-    for (portIter = portSnoopList.begin();
-         portIter != portSnoopList.end(); portIter++)
-    {
-        if (portIter->portId != id) {
-            snoop.push_back(portIter->range);
-            DPRINTF(BusAddrRanges, "  -- Snoop: %#llx : %#llx\n",
-                    portIter->range.start, portIter->range.end);
-            //@todo We need to properly insert snoop ranges
-            //not overlapping the ranges (multiple)
+    for (SnoopIter s_iter = snoopPorts.begin(); s_iter != snoopPorts.end();
+         s_iter++) {
+        if ((*s_iter)->getId() != id) {
+            snoop = true;
+            break;
         }
     }
 }
 
+int
+Bus::findBlockSize(int id)
+{
+    if (cachedBlockSizeValid)
+        return cachedBlockSize;
+
+    int max_bs = -1;
+
+    for (PortIter portIter = portMap.begin();
+         portIter != portMap.end(); portIter++) {
+        int tmp_bs = interfaces[portIter->second]->peerBlockSize();
+        if (tmp_bs > max_bs)
+            max_bs = tmp_bs;
+    }
+    for (SnoopIter s_iter = snoopPorts.begin();
+         s_iter != snoopPorts.end(); s_iter++) {
+        int tmp_bs = (*s_iter)->peerBlockSize();
+        if (tmp_bs > max_bs)
+            max_bs = tmp_bs;
+    }
+    if (max_bs <= 0)
+        max_bs = defaultBlockSize;
+
+    if (max_bs != 64)
+        warn_once("Blocksize found to not be 64... hmm... probably not.\n");
+    cachedBlockSize = max_bs;
+    cachedBlockSizeValid = true;
+    return max_bs;
+}
+
+
 unsigned int
 Bus::drain(Event * de)
 {
@@ -585,12 +610,20 @@ Bus::drain(Event * de)
     }
 }
 
+void
+Bus::startup()
+{
+    if (tickNextIdle < curTick)
+        tickNextIdle = (curTick / clock) * clock + clock;
+}
+
 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus)
 
     Param<int> bus_id;
     Param<int> clock;
     Param<int> width;
     Param<bool> responder_set;
+    Param<int> block_size;
 
 END_DECLARE_SIM_OBJECT_PARAMS(Bus)
 
@@ -598,12 +631,14 @@ BEGIN_INIT_SIM_OBJECT_PARAMS(Bus)
     INIT_PARAM(bus_id, "a globally unique bus id"),
     INIT_PARAM(clock, "bus clock speed"),
     INIT_PARAM(width, "width of the bus (bits)"),
-    INIT_PARAM(responder_set, "Is a default responder set by the user")
+    INIT_PARAM(responder_set, "Is a default responder set by the user"),
+    INIT_PARAM(block_size, "Default blocksize if no device has one")
 END_INIT_SIM_OBJECT_PARAMS(Bus)
 
 CREATE_SIM_OBJECT(Bus)
 {
-    return new Bus(getInstanceName(), bus_id, clock, width, responder_set);
+    return new Bus(getInstanceName(), bus_id, clock, width, responder_set,
+            block_size);
 }
 
 REGISTER_SIM_OBJECT("Bus", Bus)