mem-cache: Only pendingModified MSHRs can satisfy CMO snoops
[gem5.git] / src / mem / cache / mshr.cc
index 86b5a4c0884cd8de5a798aa564a64b3a87e10294..493b7f02101a5fc04b70a79c36cfc1c6cad5c341 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012-2013, 2015-2016 ARM Limited
+ * Copyright (c) 2012-2013, 2015-2018 ARM Limited
  * All rights reserved.
  *
  * The license below extends only to copyright in the software and shall
@@ -54,7 +54,7 @@
 #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"
@@ -65,18 +65,18 @@ using namespace std;
 MSHR::MSHR() : downstreamPending(false),
                pendingModified(false),
                postInvalidate(false), postDowngrade(false),
-               isForward(false), allocOnFill(false)
+               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()) {
@@ -89,8 +89,28 @@ 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
@@ -105,7 +125,7 @@ MSHR::TargetList::add(PacketPtr pkt, Tick readyTime,
         }
     }
 
-    emplace_back(pkt, readyTime, order, source, markPending);
+    emplace_back(pkt, readyTime, order, source, markPending, alloc_on_fill);
 }
 
 
@@ -170,6 +190,7 @@ MSHR::TargetList::clearDownstreamPending()
             if (mshr != nullptr) {
                 mshr->clearDownstreamPending();
             }
+            t.markedPending = false;
         }
     }
 }
@@ -210,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");
     }
 }
 
@@ -225,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;
@@ -234,7 +255,7 @@ 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());
 }
 
@@ -291,50 +312,55 @@ 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:
     // - there are other targets already deferred
     // - there's a pending invalidate to be applied after the response
     //   comes back (but before this target is processed)
+    // - the MSHR's first (and only) non-deferred target is a cache
+    //   maintenance packet
+    // - the new target is a cache maintenance packet (this is probably
+    //   overly conservative but certainly safe)
     // - this target requires a writable block and either we're not
     //   getting a writable block back or we have already snooped
     //   another read request that will downgrade our writable block
     //   to non-writable (Shared or Owned)
-    if (inService &&
-        (!deferredTargets.empty() || hasPostInvalidate() ||
-         (pkt->needsWritable() &&
-          (!isPendingModified() || hasPostDowngrade() || isForward)))) {
+    PacketPtr tgt_pkt = targets.front().pkt;
+    if (pkt->req->isCacheMaintenance() ||
+        tgt_pkt->req->isCacheMaintenance() ||
+        !deferredTargets.empty() ||
+        (inService &&
+         (hasPostInvalidate() ||
+          (pkt->needsWritable() &&
+           (!isPendingModified() || hasPostDowngrade() || isForward))))) {
         // need to put on deferred list
-        if (hasPostInvalidate())
+        if (inService && 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());
+    panic_if((pkt->needsWritable() != pkt->isInvalidate()) &&
+             !pkt->req->isCacheMaintenance(),
+             "%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
@@ -350,7 +376,7 @@ MSHR::handleSnoop(PacketPtr pkt, Counter _order)
         // That is, even though the upper-level cache got out on its
         // local bus first, some other invalidating transaction
         // reached the global bus before the upgrade did.
-        if (pkt->needsWritable()) {
+        if (pkt->needsWritable() || pkt->req->isCacheInvalidate()) {
             targets.replaceUpgrades();
             deferredTargets.replaceUpgrades();
         }
@@ -360,16 +386,18 @@ MSHR::handleSnoop(PacketPtr pkt, Counter _order)
 
     // From here on down, the request issued by this MSHR logically
     // precedes the request we're snooping.
-    if (pkt->needsWritable()) {
+    if (pkt->needsWritable() || pkt->req->isCacheInvalidate()) {
         // snooped request still precedes the re-request we'll have to
         // issue for deferred targets, if any...
         deferredTargets.replaceUpgrades();
     }
 
-    if (hasPostInvalidate()) {
-        // a prior snoop has already appended an invalidation, so
-        // logically we don't have the block anymore; no need for
-        // further snooping.
+    PacketPtr tgt_pkt = targets.front().pkt;
+    if (hasPostInvalidate() || tgt_pkt->req->isCacheInvalidate()) {
+        // a prior snoop has already appended an invalidation or a
+        // cache invalidation operation is in progress, so logically
+        // we don't have the block anymore; no need for further
+        // snooping.
         return true;
     }
 
@@ -385,7 +413,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;
+                      !pkt->isClean();
 
         // The packet we are snooping may be deleted by the time we
         // actually process the target, and we consequently need to
@@ -398,7 +426,7 @@ 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, pkt->id);
 
         if (will_respond) {
             // we are the ordering point, and will consequently
@@ -417,12 +445,16 @@ 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()) {
+        if (pkt->needsWritable() || pkt->isInvalidate()) {
             // This transaction will take away our pending copy
             postInvalidate = true;
         }
+
+        if (isPendingModified() && pkt->isClean()) {
+            pkt->setSatisfied();
+        }
     }
 
     if (!pkt->needsWritable() && !pkt->req->isUncacheable()) {
@@ -438,21 +470,72 @@ 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()) {
+    if (targets.empty() && deferredTargets.empty()) {
+        // nothing to promote
         return false;
     }
 
-    // swap targets & deferredTargets lists
-    std::swap(targets, deferredTargets);
-
-    // clear deferredTargets flags
-    deferredTargets.resetFlags();
+    // the deferred targets can be generally promoted unless they
+    // contain a cache maintenance request
+
+    // find the first target that is a cache maintenance request
+    auto it = std::find_if(deferredTargets.begin(), deferredTargets.end(),
+                           [](MSHR::Target &t) {
+                               return t.pkt->req->isCacheMaintenance();
+                           });
+    if (it == deferredTargets.begin()) {
+        // if the first deferred target is a cache maintenance packet
+        // then we can promote provided the targets list is empty and
+        // we can service it on its own
+        if (targets.empty()) {
+            targets.splice(targets.end(), deferredTargets, it);
+        }
+    } else {
+        // if a cache maintenance operation exists, we promote all the
+        // deferred targets that precede it, or all deferred targets
+        // otherwise
+        targets.splice(targets.end(), deferredTargets,
+                       deferredTargets.begin(), it);
+    }
 
+    deferredTargets.populateFlags();
+    targets.populateFlags();
     order = targets.front().order;
     readyTime = std::max(curTick(), targets.front().readyTime);
 
@@ -513,7 +596,7 @@ MSHR::print(std::ostream &os, int verbosity, const std::string &prefix) const
              prefix, blkAddr, blkAddr + blkSize - 1,
              isSecure ? "s" : "ns",
              isForward ? "Forward" : "",
-             allocOnFill ? "AllocOnFill" : "",
+             allocOnFill() ? "AllocOnFill" : "",
              needsWritable() ? "Wrtbl" : "",
              _isUncacheable ? "Unc" : "",
              inService ? "InSvc" : "",