misc: Rename misc.(hh|cc) to logging.(hh|cc)
[gem5.git] / src / mem / cache / mshr.cc
index e2141a429318d852792213b571accf1cb8be1844..fa21f5cfc2b18fe8bc9de21840ec74f7eccd797a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012-2013, 2015 ARM Limited
+ * Copyright (c) 2012-2013, 2015-2017 ARM Limited
  * All rights reserved.
  *
  * The license below extends only to copyright in the software and shall
  * Miss Status and Handling Register (MSHR) definitions.
  */
 
+#include "mem/cache/mshr.hh"
+
 #include <algorithm>
 #include <cassert>
 #include <string>
 #include <vector>
 
-#include "base/misc.hh"
+#include "base/logging.hh"
 #include "base/types.hh"
 #include "debug/Cache.hh"
 #include "mem/cache/cache.hh"
-#include "mem/cache/mshr.hh"
 #include "sim/core.hh"
 
 using namespace std;
 
-MSHR::MSHR() : readyTime(0), _isUncacheable(false), downstreamPending(false),
+MSHR::MSHR() : downstreamPending(false),
                pendingModified(false),
                postInvalidate(false), postDowngrade(false),
-               queue(NULL), order(0), blkAddr(0),
-               blkSize(0), isSecure(false), inService(false),
-               isForward(false), allocOnFill(false),
-               data(NULL)
+               isForward(false)
 {
 }
 
-
 MSHR::TargetList::TargetList()
-    : needsWritable(false), hasUpgrade(false)
+    : needsWritable(false), hasUpgrade(false), allocOnFill(false)
 {}
 
 
-inline void
-MSHR::TargetList::add(PacketPtr pkt, Tick readyTime,
-                      Counter order, Target::Source source, bool markPending)
+void
+MSHR::TargetList::updateFlags(PacketPtr pkt, Target::Source source,
+                              bool alloc_on_fill)
 {
     if (source != Target::FromSnoop) {
         if (pkt->needsWritable()) {
@@ -92,14 +89,34 @@ MSHR::TargetList::add(PacketPtr pkt, Tick readyTime,
         if (pkt->isUpgrade() || pkt->cmd == MemCmd::StoreCondReq) {
             hasUpgrade = true;
         }
+
+        // potentially re-evaluate whether we should allocate on a fill or
+        // not
+        allocOnFill = allocOnFill || alloc_on_fill;
     }
+}
 
+void
+MSHR::TargetList::populateFlags()
+{
+    resetFlags();
+    for (auto& t: *this) {
+        updateFlags(t.pkt, t.source, t.allocOnFill);
+    }
+}
+
+inline void
+MSHR::TargetList::add(PacketPtr pkt, Tick readyTime,
+                      Counter order, Target::Source source, bool markPending,
+                      bool alloc_on_fill)
+{
+    updateFlags(pkt, source, alloc_on_fill);
     if (markPending) {
         // Iterate over the SenderState stack and see if we find
         // an MSHR entry. If we do, set the downstreamPending
         // flag. Otherwise, do nothing.
         MSHR *mshr = pkt->findNextSenderState<MSHR>();
-        if (mshr != NULL) {
+        if (mshr != nullptr) {
             assert(!mshr->downstreamPending);
             mshr->downstreamPending = true;
         } else {
@@ -108,13 +125,16 @@ MSHR::TargetList::add(PacketPtr pkt, Tick readyTime,
         }
     }
 
-    emplace_back(pkt, readyTime, order, source, markPending);
+    emplace_back(pkt, readyTime, order, source, markPending, alloc_on_fill);
 }
 
 
 static void
 replaceUpgrade(PacketPtr pkt)
 {
+    // remember if the current packet has data allocated
+    bool has_data = pkt->hasData() || pkt->hasRespData();
+
     if (pkt->cmd == MemCmd::UpgradeReq) {
         pkt->cmd = MemCmd::ReadExReq;
         DPRINTF(Cache, "Replacing UpgradeReq with ReadExReq\n");
@@ -125,6 +145,19 @@ replaceUpgrade(PacketPtr pkt)
         pkt->cmd = MemCmd::StoreCondFailReq;
         DPRINTF(Cache, "Replacing StoreCondReq with StoreCondFailReq\n");
     }
+
+    if (!has_data) {
+        // there is no sensible way of setting the data field if the
+        // new command actually would carry data
+        assert(!pkt->hasData());
+
+        if (pkt->hasRespData()) {
+            // we went from a packet that had no data (neither request,
+            // nor response), to one that does, and therefore we need to
+            // actually allocate space for the data payload
+            pkt->allocate();
+        }
+    }
 }
 
 
@@ -154,9 +187,10 @@ MSHR::TargetList::clearDownstreamPending()
             // downstreamPending flag in all caches this packet has
             // passed through.
             MSHR *mshr = t.pkt->findNextSenderState<MSHR>();
-            if (mshr != NULL) {
+            if (mshr != nullptr) {
                 mshr->clearDownstreamPending();
             }
+            t.markedPending = false;
         }
     }
 }
@@ -197,6 +231,7 @@ MSHR::TargetList::print(std::ostream &os, int verbosity,
         }
         ccprintf(os, "%s%s: ", prefix, s);
         t.pkt->print(os, verbosity, "");
+        ccprintf(os, "\n");
     }
 }
 
@@ -212,7 +247,6 @@ MSHR::allocate(Addr blk_addr, unsigned blk_size, PacketPtr target,
     order = _order;
     assert(target);
     isForward = false;
-    allocOnFill = alloc_on_fill;
     _isUncacheable = target->req->isUncacheable();
     inService = false;
     downstreamPending = false;
@@ -221,9 +255,8 @@ MSHR::allocate(Addr blk_addr, unsigned blk_size, PacketPtr target,
     // snoop (mem-side request), so set source according to request here
     Target::Source source = (target->cmd == MemCmd::HardPFReq) ?
         Target::FromPrefetcher : Target::FromCPU;
-    targets.add(target, when_ready, _order, source, true);
+    targets.add(target, when_ready, _order, source, true, alloc_on_fill);
     assert(deferredTargets.isReset());
-    data = NULL;
 }
 
 
@@ -237,17 +270,10 @@ MSHR::clearDownstreamPending()
     targets.clearDownstreamPending();
 }
 
-bool
+void
 MSHR::markInService(bool pending_modified_resp)
 {
     assert(!inService);
-    if (isForwardNoResponse()) {
-        // we just forwarded the request packet & don't expect a
-        // response, so get rid of it
-        assert(getNumTargets() == 1);
-        popTarget();
-        return true;
-    }
 
     inService = true;
     pendingModified = targets.needsWritable || pending_modified_resp;
@@ -258,7 +284,6 @@ MSHR::markInService(bool pending_modified_resp)
         // level where it's going to get a response
         targets.clearDownstreamPending();
     }
-    return false;
 }
 
 
@@ -287,10 +312,6 @@ MSHR::allocateTarget(PacketPtr pkt, Tick whenReady, Counter _order,
     // have targets addded if originally allocated uncacheable
     assert(!_isUncacheable);
 
-    // potentially re-evaluate whether we should allocate on a fill or
-    // not
-    allocOnFill = allocOnFill || alloc_on_fill;
-
     // if there's a request already in service for this MSHR, we will
     // have to defer the new target until after the response if any of
     // the following are true:
@@ -308,29 +329,29 @@ MSHR::allocateTarget(PacketPtr pkt, Tick whenReady, Counter _order,
         // need to put on deferred list
         if (hasPostInvalidate())
             replaceUpgrade(pkt);
-        deferredTargets.add(pkt, whenReady, _order, Target::FromCPU, true);
+        deferredTargets.add(pkt, whenReady, _order, Target::FromCPU, true,
+                            alloc_on_fill);
     } else {
         // No request outstanding, or still OK to append to
         // outstanding request: append to regular target list.  Only
         // mark pending if current request hasn't been issued yet
         // (isn't in service).
-        targets.add(pkt, whenReady, _order, Target::FromCPU, !inService);
+        targets.add(pkt, whenReady, _order, Target::FromCPU, !inService,
+                    alloc_on_fill);
     }
 }
 
 bool
 MSHR::handleSnoop(PacketPtr pkt, Counter _order)
 {
-    DPRINTF(Cache, "%s for %s addr %#llx size %d\n", __func__,
-            pkt->cmdString(), pkt->getAddr(), pkt->getSize());
+    DPRINTF(Cache, "%s for %s\n", __func__, pkt->print());
 
     // when we snoop packets the needsWritable and isInvalidate flags
     // should always be the same, however, this assumes that we never
     // snoop writes as they are currently not marked as invalidations
     panic_if(pkt->needsWritable() != pkt->isInvalidate(),
-             "%s got snoop %s to addr %#llx where needsWritable, "
-             "does not match isInvalidate", name(), pkt->cmdString(),
-             pkt->getAddr());
+             "%s got snoop %s where needsWritable, "
+             "does not match isInvalidate", name(), pkt->print());
 
     if (!inService || (pkt->isExpressSnoop() && downstreamPending)) {
         // Request has not been issued yet, or it's been issued
@@ -380,8 +401,7 @@ MSHR::handleSnoop(PacketPtr pkt, Counter _order)
 
         // Start by determining if we will eventually respond or not,
         // matching the conditions checked in Cache::handleSnoop
-        bool will_respond = isPendingModified() && pkt->needsResponse() &&
-            pkt->cmd != MemCmd::InvalidateReq;
+        bool will_respond = isPendingModified() && pkt->needsResponse();
 
         // The packet we are snooping may be deleted by the time we
         // actually process the target, and we consequently need to
@@ -394,9 +414,9 @@ MSHR::handleSnoop(PacketPtr pkt, Counter _order)
         // the packet and the request as part of handling the deferred
         // snoop.
         PacketPtr cp_pkt = will_respond ? new Packet(pkt, true, true) :
-            new Packet(new Request(*pkt->req), pkt->cmd);
+            new Packet(new Request(*pkt->req), pkt->cmd, blkSize);
 
-        if (isPendingModified()) {
+        if (will_respond) {
             // we are the ordering point, and will consequently
             // respond, and depending on whether the packet
             // needsWritable or not we either pass a Shared line or a
@@ -413,7 +433,7 @@ MSHR::handleSnoop(PacketPtr pkt, Counter _order)
             // recipient does not care there is no harm in doing so
         }
         targets.add(cp_pkt, curTick(), _order, Target::FromSnoop,
-                    downstreamPending && targets.needsWritable);
+                    downstreamPending && targets.needsWritable, false);
 
         if (pkt->needsWritable()) {
             // This transaction will take away our pending copy
@@ -434,17 +454,55 @@ MSHR::handleSnoop(PacketPtr pkt, Counter _order)
     return true;
 }
 
+MSHR::TargetList
+MSHR::extractServiceableTargets(PacketPtr pkt)
+{
+    TargetList ready_targets;
+    // If the downstream MSHR got an invalidation request then we only
+    // service the first of the FromCPU targets and any other
+    // non-FromCPU target. This way the remaining FromCPU targets
+    // issue a new request and get a fresh copy of the block and we
+    // avoid memory consistency violations.
+    if (pkt->cmd == MemCmd::ReadRespWithInvalidate) {
+        auto it = targets.begin();
+        assert((it->source == Target::FromCPU) ||
+               (it->source == Target::FromPrefetcher));
+        ready_targets.push_back(*it);
+        it = targets.erase(it);
+        while (it != targets.end()) {
+            if (it->source == Target::FromCPU) {
+                it++;
+            } else {
+                assert(it->source == Target::FromSnoop);
+                ready_targets.push_back(*it);
+                it = targets.erase(it);
+            }
+        }
+        ready_targets.populateFlags();
+    } else {
+        std::swap(ready_targets, targets);
+    }
+    targets.populateFlags();
+
+    return ready_targets;
+}
 
 bool
 MSHR::promoteDeferredTargets()
 {
-    assert(targets.empty());
-    if (deferredTargets.empty()) {
-        return false;
-    }
+    if (targets.empty())  {
+        if (deferredTargets.empty()) {
+            return false;
+        }
 
-    // swap targets & deferredTargets lists
-    std::swap(targets, deferredTargets);
+        std::swap(targets, deferredTargets);
+    } else {
+        // If the targets list is not empty then we have one targets
+        // from the deferredTargets list to the targets list. A new
+        // request will then service the targets list.
+        targets.splice(targets.end(), deferredTargets);
+        targets.populateFlags();
+    }
 
     // clear deferredTargets flags
     deferredTargets.resetFlags();
@@ -488,7 +546,7 @@ MSHR::checkFunctional(PacketPtr pkt)
     // For other requests, we iterate over the individual targets
     // since that's where the actual data lies.
     if (pkt->isPrint()) {
-        pkt->checkFunctional(this, blkAddr, isSecure, blkSize, NULL);
+        pkt->checkFunctional(this, blkAddr, isSecure, blkSize, nullptr);
         return false;
     } else {
         return (targets.checkFunctional(pkt) ||
@@ -496,6 +554,11 @@ MSHR::checkFunctional(PacketPtr pkt)
     }
 }
 
+bool
+MSHR::sendPacket(Cache &cache)
+{
+    return cache.sendMSHRQueuePacket(this);
+}
 
 void
 MSHR::print(std::ostream &os, int verbosity, const std::string &prefix) const
@@ -504,17 +567,18 @@ MSHR::print(std::ostream &os, int verbosity, const std::string &prefix) const
              prefix, blkAddr, blkAddr + blkSize - 1,
              isSecure ? "s" : "ns",
              isForward ? "Forward" : "",
-             allocOnFill ? "AllocOnFill" : "",
-             isForwardNoResponse() ? "ForwNoResp" : "",
+             allocOnFill() ? "AllocOnFill" : "",
              needsWritable() ? "Wrtbl" : "",
              _isUncacheable ? "Unc" : "",
              inService ? "InSvc" : "",
              downstreamPending ? "DwnPend" : "",
-             hasPostInvalidate() ? "PostInv" : "",
-             hasPostDowngrade() ? "PostDowngr" : "");
+             postInvalidate ? "PostInv" : "",
+             postDowngrade ? "PostDowngr" : "");
 
-    ccprintf(os, "%s  Targets:\n", prefix);
-    targets.print(os, verbosity, prefix + "    ");
+    if (!targets.empty()) {
+        ccprintf(os, "%s  Targets:\n", prefix);
+        targets.print(os, verbosity, prefix + "    ");
+    }
     if (!deferredTargets.empty()) {
         ccprintf(os, "%s  Deferred Targets:\n", prefix);
         deferredTargets.print(os, verbosity, prefix + "      ");