mem: Use address range to find the destination port in the xbar

Previously the xbar used the start address to lookup the port map and
determine the right destination of an incoming packet. This change
uses the full address range to correctly determine the right master.

Change-Id: I5118712c43ae65aba64e71bf030bca5c99770bdd
Reviewed-on: https://gem5-review.googlesource.com/11117
Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Maintainer: Nikos Nikoleris <nikos.nikoleris@arm.com>
diff --git a/src/mem/coherent_xbar.cc b/src/mem/coherent_xbar.cc
index 872ee5c..d46f389 100644
--- a/src/mem/coherent_xbar.cc
+++ b/src/mem/coherent_xbar.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2017 ARM Limited
+ * Copyright (c) 2011-2018 ARM Limited
  * All rights reserved
  *
  * The license below extends only to copyright in the software and shall
@@ -151,8 +151,9 @@
     // and the cache responding flag should always be the same
     assert(is_express_snoop == cache_responding);
 
-    // determine the destination based on the address
-    PortID master_port_id = findPort(pkt->getAddr());
+    // determine the destination based on the destination address range
+    AddrRange addr_range = RangeSize(pkt->getAddr(), pkt->getSize());
+    PortID master_port_id = findPort(addr_range);
 
     // test if the crossbar should be considered occupied for the current
     // port, and exclude express snoops from the check
@@ -551,7 +552,9 @@
     // device responsible for the address range something is
     // wrong, hence there is nothing further to do as the packet
     // would be going back to where it came from
-    assert(master_port_id == findPort(pkt->getAddr()));
+    AddrRange addr_range M5_VAR_USED =
+        RangeSize(pkt->getAddr(), pkt->getSize());
+    assert(findPort(addr_range) == master_port_id);
 }
 
 bool
@@ -781,7 +784,8 @@
 
     // even if we had a snoop response, we must continue and also
     // perform the actual request at the destination
-    PortID master_port_id = findPort(pkt->getAddr());
+    AddrRange addr_range = RangeSize(pkt->getAddr(), pkt->getSize());
+    PortID master_port_id = findPort(addr_range);
 
     if (sink_packet) {
         DPRINTF(CoherentXBar, "%s: Not forwarding %s\n", __func__,
@@ -1005,7 +1009,7 @@
             }
         }
 
-        PortID dest_id = findPort(pkt->getAddr());
+        PortID dest_id = findPort(RangeSize(pkt->getAddr(), pkt->getSize()));
 
         masterPorts[dest_id]->sendFunctional(pkt);
     }
diff --git a/src/mem/noncoherent_xbar.cc b/src/mem/noncoherent_xbar.cc
index 3ff991f..7bd04cb 100644
--- a/src/mem/noncoherent_xbar.cc
+++ b/src/mem/noncoherent_xbar.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2015 ARM Limited
+ * Copyright (c) 2011-2015, 2018 ARM Limited
  * All rights reserved
  *
  * The license below extends only to copyright in the software and shall
@@ -108,7 +108,8 @@
     assert(!pkt->isExpressSnoop());
 
     // determine the destination based on the address
-    PortID master_port_id = findPort(pkt->getAddr());
+    AddrRange addr_range = RangeSize(pkt->getAddr(), pkt->getSize());
+    PortID master_port_id = findPort(addr_range);
 
     // test if the layer should be considered occupied for the current
     // port
@@ -253,7 +254,8 @@
     unsigned int pkt_cmd = pkt->cmdToIndex();
 
     // determine the destination port
-    PortID master_port_id = findPort(pkt->getAddr());
+    AddrRange addr_range = RangeSize(pkt->getAddr(), pkt->getSize());
+    PortID master_port_id = findPort(addr_range);
 
     // stats updates for the request
     pktCount[slave_port_id][master_port_id]++;
@@ -303,7 +305,8 @@
     }
 
     // determine the destination port
-    PortID dest_id = findPort(pkt->getAddr());
+    AddrRange addr_range = RangeSize(pkt->getAddr(), pkt->getSize());
+    PortID dest_id = findPort(addr_range);
 
     // forward the request to the appropriate destination
     masterPorts[dest_id]->sendFunctional(pkt);
diff --git a/src/mem/xbar.cc b/src/mem/xbar.cc
index c3a5c83..b139cdc 100644
--- a/src/mem/xbar.cc
+++ b/src/mem/xbar.cc
@@ -321,34 +321,34 @@
 }
 
 PortID
-BaseXBar::findPort(Addr addr)
+BaseXBar::findPort(AddrRange addr_range)
 {
     // we should never see any address lookups before we've got the
     // ranges of all connected slave modules
     assert(gotAllAddrRanges);
 
     // Check the address map interval tree
-    auto i = portMap.contains(addr);
+    auto i = portMap.contains(addr_range);
     if (i != portMap.end()) {
         return i->second;
     }
 
     // Check if this matches the default range
     if (useDefaultRange) {
-        if (defaultRange.contains(addr)) {
-            DPRINTF(AddrRanges, "  found addr %#llx on default\n",
-                    addr);
+        if (addr_range.isSubset(defaultRange)) {
+            DPRINTF(AddrRanges, "  found addr %s on default\n",
+                    addr_range.to_string());
             return defaultPortID;
         }
     } else if (defaultPortID != InvalidPortID) {
-        DPRINTF(AddrRanges, "Unable to find destination for addr %#llx, "
-                "will use default port\n", addr);
+        DPRINTF(AddrRanges, "Unable to find destination for %s, "
+                "will use default port\n", addr_range.to_string());
         return defaultPortID;
     }
 
     // we should use the range for the default port and it did not
     // match, or the default port is not set
-    fatal("Unable to find destination for addr %#llx on %s\n", addr,
+    fatal("Unable to find destination for %s on %s\n", addr_range.to_string(),
           name());
 }
 
diff --git a/src/mem/xbar.hh b/src/mem/xbar.hh
index 2b7e7ed..abe2a10 100644
--- a/src/mem/xbar.hh
+++ b/src/mem/xbar.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2015 ARM Limited
+ * Copyright (c) 2011-2015, 2018 ARM Limited
  * All rights reserved
  *
  * The license below extends only to copyright in the software and shall
@@ -342,13 +342,14 @@
      */
     virtual void recvRangeChange(PortID master_port_id);
 
-    /** Find which port connected to this crossbar (if any) should be
-     * given a packet with this address.
+    /**
+     * Find which port connected to this crossbar (if any) should be
+     * given a packet with this address range.
      *
-     * @param addr Address to find port for.
+     * @param addr_range Address range to find port for.
      * @return id of port that the packet should be sent out of.
      */
-    PortID findPort(Addr addr);
+    PortID findPort(AddrRange addr_range);
 
     /**
      * Return the address ranges the crossbar is responsible for.