testers,protocol,mem: fix integration issues between GCN3 and VIPER

This patch basically changes how coalescer supports two different cases:
(1) using real CUs and (2) using tester threads.

This patch keeps the implementation of writeCompleteCallback in
GPUCoalescer instead of placing it in VIPER-specific coalescer.

Change-Id: I6d54795ac60d7aa020bc94b853dc3673f59762ba
diff --git a/src/cpu/testers/gpu_ruby_test/GpuWavefront.cc b/src/cpu/testers/gpu_ruby_test/GpuWavefront.cc
index 699776c..33625e6 100644
--- a/src/cpu/testers/gpu_ruby_test/GpuWavefront.cc
+++ b/src/cpu/testers/gpu_ruby_test/GpuWavefront.cc
@@ -287,7 +287,7 @@
 {
     assert(pkt);
     MemCmd resp_cmd = pkt->cmd;
-    Addr addr = pkt->getAddr();
+    Addr addr = (resp_cmd == MemCmd::MessageResp) ? 0 : pkt->getAddr();
 
     DPRINTF(ProtocolTest, "%s Episode %d: hitCallback - Command %s - "
                     "Addr %s\n", this->getName(),
diff --git a/src/mem/protocol/GPU_VIPER-TCP.sm b/src/mem/protocol/GPU_VIPER-TCP.sm
index 3f61791..eed5272 100644
--- a/src/mem/protocol/GPU_VIPER-TCP.sm
+++ b/src/mem/protocol/GPU_VIPER-TCP.sm
@@ -495,7 +495,7 @@
       assert(false);
     } else {
       peek(responseToTCP_in, ResponseMsg) {
-        coalescer.writeCompleteCallback(address, in_msg.instSeqNum);
+        coalescer.writeCompleteCallback(address, in_msg.instSeqNum, MachineType:L1Cache);
       }
     }
   }
diff --git a/src/mem/protocol/RubySlicc_Types.sm b/src/mem/protocol/RubySlicc_Types.sm
index f60466a..bbae1da 100644
--- a/src/mem/protocol/RubySlicc_Types.sm
+++ b/src/mem/protocol/RubySlicc_Types.sm
@@ -137,6 +137,7 @@
                      Cycles, Cycles, Cycles);
   void writeCallback(Addr, MachineType, DataBlock,
                      Cycles, Cycles, Cycles, bool);
+  void writeCompleteCallback(Addr, uint64_t, MachineType);
   void atomicCallback(Addr, MachineType, DataBlock);
   void checkCoherence(Addr);
   void evictionCallback(Addr);
@@ -159,7 +160,7 @@
                      Cycles, Cycles, Cycles, bool);
   void atomicCallback(Addr, MachineType, DataBlock);
   void invTCPCallback(Addr);
-  void writeCompleteCallback(Addr, uint64_t);
+  void writeCompleteCallback(Addr, uint64_t, MachineType);
   void checkCoherence(Addr);
   void evictionCallback(Addr);
 }
diff --git a/src/mem/ruby/system/GPUCoalescer.cc b/src/mem/ruby/system/GPUCoalescer.cc
index f19e419..cb9b2e1 100644
--- a/src/mem/ruby/system/GPUCoalescer.cc
+++ b/src/mem/ruby/system/GPUCoalescer.cc
@@ -648,22 +648,8 @@
             // instruction.
             RubyPort::MemSlavePort* mem_slave_port = ss->port;
 
-            GPUDynInstPtr gpuDynInst = nullptr;
-
-            if (!m_usingRubyTester) {
-                // If this coalescer is connected to a real CU, we need
-                // to save the corresponding gpu dynamic instruction.
-                // CU will use that instruction to decrement wait counters
-                // in the issuing wavefront.
-                // For Ruby tester, gpuDynInst == nullptr
-                ComputeUnit::DataPort::SenderState* cu_state =
-                    safe_cast<ComputeUnit::DataPort::SenderState*>
-                        (ss->predecessor);
-                gpuDynInst = cu_state->_gpuDynInst;
-            }
-
             PendingWriteInst& inst = pendingWriteInsts[seqNum];
-            inst.addPendingReq(mem_slave_port, gpuDynInst, m_usingRubyTester);
+            inst.addPendingReq(mem_slave_port, pkt, m_usingRubyTester);
         }
 
         return true;
diff --git a/src/mem/ruby/system/GPUCoalescer.hh b/src/mem/ruby/system/GPUCoalescer.hh
index 89e08e8..8695cdb 100644
--- a/src/mem/ruby/system/GPUCoalescer.hh
+++ b/src/mem/ruby/system/GPUCoalescer.hh
@@ -40,6 +40,7 @@
 #include <unordered_map>
 
 #include "base/statistics.hh"
+#include "cpu/testers/gpu_ruby_test/ProtocolTester.hh"
 #include "gpu-compute/gpu_dyn_inst.hh"
 #include "gpu-compute/misc.hh"
 #include "mem/protocol/PrefetchBit.hh"
@@ -124,6 +125,7 @@
   public:
     PendingWriteInst()
         : numPendingStores(0),
+          numRequiredWriteCompleteAcks(0),
           originalPort(nullptr),
           gpuDynInstPtr(nullptr)
     {}
@@ -132,14 +134,35 @@
     {}
 
     void
-    addPendingReq(RubyPort::MemSlavePort* port, GPUDynInstPtr inst,
+    addPendingReq(RubyPort::MemSlavePort* port, PacketPtr pkt,
                   bool usingRubyTester)
     {
         assert(port);
         originalPort = port;
 
-        if (!usingRubyTester) {
-            gpuDynInstPtr = inst;
+        RubyPort::SenderState* ss =
+                safe_cast<RubyPort::SenderState*>(pkt->senderState);
+
+        if (usingRubyTester) {
+            // If this coalescer is connected to a tester thread, we need
+            // to save the corresponding requesting thread.
+            // get the requesting thread from the original sender state
+            ProtocolTester::SenderState* senderState =
+                            safe_cast<ProtocolTester::SenderState*>
+                                (ss->predecessor);
+            testerThreadPtr = senderState->th;
+            numRequiredWriteCompleteAcks++;
+        } else {
+            // If this coalescer is connected to a real CU, we need
+            // to save the corresponding gpu dynamic instruction.
+            // CU will use that instruction to decrement wait counters
+            // in the issuing wavefront.
+            // For Ruby tester, gpuDynInst == nullptr
+            ComputeUnit::DataPort::SenderState* cu_state =
+                safe_cast<ComputeUnit::DataPort::SenderState*>
+                    (ss->predecessor);
+            gpuDynInstPtr = cu_state->_gpuDynInst;
+            numRequiredWriteCompleteAcks = 1;
         }
 
         numPendingStores++;
@@ -159,20 +182,28 @@
     ackWriteCompletion(bool usingRubyTester)
     {
         assert(numPendingStores == 0);
+        assert(numRequiredWriteCompleteAcks > 0);
 
-        // make a response packet
-        PacketPtr pkt = new Packet(new Request(), MemCmd::MessageResp);
+        for (int i = 0; i < numRequiredWriteCompleteAcks; ++i) {
+            // make a response packet
+            PacketPtr pkt = new Packet(new Request(), MemCmd::MessageResp);
 
-        if (!usingRubyTester) {
-            assert(gpuDynInstPtr);
-            ComputeUnit::DataPort::SenderState* ss =
-                    new ComputeUnit::DataPort::SenderState
-                                            (gpuDynInstPtr, 0, nullptr);
-            pkt->senderState = ss;
+            if (usingRubyTester) {
+                assert(testerThreadPtr);
+                ProtocolTester::SenderState* ss =
+                        new ProtocolTester::SenderState(testerThreadPtr);
+                pkt->senderState = ss;
+            } else {
+                assert(gpuDynInstPtr);
+                ComputeUnit::DataPort::SenderState* ss =
+                        new ComputeUnit::DataPort::SenderState
+                                                (gpuDynInstPtr, 0, nullptr);
+                pkt->senderState = ss;
+            }
+
+            // send the ack response to the requester
+            originalPort->sendTimingResp(pkt);
         }
-
-        // send the ack response to the requester
-        originalPort->sendTimingResp(pkt);
     }
 
     int
@@ -183,6 +214,11 @@
   private:
     // the number of stores waiting for writeCompleteCallback
     int numPendingStores;
+    // number of write-complete acks required by the requestor
+    // if the requestor is a CU, this number is always 1
+    // if the requestor is a tester thread, this number is equal to the number
+    // of store requests sent to the coalescer
+    int numRequiredWriteCompleteAcks;
     // The original port that sent one of packets associated with this
     // write instruction. We may have more than one packet per instruction,
     // which implies multiple ports per instruction. However, we need
@@ -192,6 +228,8 @@
     // similar to the originalPort, this gpuDynInstPtr is set only for
     // the first packet of this instruction.
     GPUDynInstPtr gpuDynInstPtr;
+    // if protocol tester is used, this points to the requesting tester thread
+    Thread* testerThreadPtr;
 };
 
 class GPUCoalescer : public RubyPort
diff --git a/src/mem/ruby/system/VIPERCoalescer.cc b/src/mem/ruby/system/VIPERCoalescer.cc
index 0fe4b94..e164e76 100644
--- a/src/mem/ruby/system/VIPERCoalescer.cc
+++ b/src/mem/ruby/system/VIPERCoalescer.cc
@@ -182,9 +182,9 @@
                               dataBlock, crequest->getSeqNum());
     }
 
-    if (pkt->cmd == MemCmd::WriteReq) {
-        makeWriteCompletePkts(crequest);
-    }
+//    if (pkt->cmd == MemCmd::WriteReq) {
+//        makeWriteCompletePkts(crequest);
+//    }
 
     DPRINTFR(ProtocolTrace, "%15s %3s %10s%20s %6s>%-6s %s %s\n",
              curTick(), m_version, "Coal", "Begin", "", "",
@@ -209,64 +209,6 @@
 }
 
 void
-VIPERCoalescer::makeWriteCompletePkts(CoalescedRequest* crequest)
-{
-    // In VIPER protocol, for each write request, down-stream caches
-    // return two responses: writeCallback and writeCompleteCallback.
-    // We need to prepare a writeCompletePkt for each write request so
-    // that when writeCompleteCallback is called, we can respond
-    // requesting wavefront right away.
-    // writeCompletePkt inherits request and senderState of the original
-    // write request packet so that we can find the original requestor
-    // later. This assumes that request and senderState are not deleted
-    // before writeCompleteCallback is called.
-
-    auto key = crequest->getSeqNum();
-    std::vector<PacketPtr>& req_pkts = crequest->getPackets();
-
-    for (auto pkt : req_pkts) {
-        DPRINTF(GPUCoalescer, "makeWriteCompletePkts: instSeqNum %d\n",
-                key);
-        assert(pkt->cmd == MemCmd::WriteReq);
-
-        PacketPtr writeCompletePkt = new Packet(pkt->req, MemCmd::MessageResp);
-        writeCompletePkt->setAddr(pkt->getAddr());
-        writeCompletePkt->senderState = pkt->senderState;
-        m_writeCompletePktMap[key].push_back(writeCompletePkt);
-    }
-}
-
-void
-VIPERCoalescer::writeCompleteCallback(Addr addr, uint64_t instSeqNum)
-{
-    DPRINTF(GPUCoalescer, "writeCompleteCallback: instSeqNum %d addr 0x%x\n",
-            instSeqNum, addr);
-
-    auto key = instSeqNum;
-    assert(m_writeCompletePktMap.count(key) == 1 &&
-           !m_writeCompletePktMap[key].empty());
-
-    for (auto writeCompletePkt : m_writeCompletePktMap[key]) {
-        if (makeLineAddress(writeCompletePkt->getAddr()) == addr) {
-            RubyPort::SenderState *ss =
-                safe_cast<RubyPort::SenderState *>
-                    (writeCompletePkt->senderState);
-            MemSlavePort *port = ss->port;
-            assert(port != NULL);
-
-            writeCompletePkt->senderState = ss->predecessor;
-            delete ss;
-            port->hitCallback(writeCompletePkt);
-        }
-    }
-
-    trySendRetries();
-
-    if (m_writeCompletePktMap[key].empty())
-        m_writeCompletePktMap.erase(key);
-}
-
-void
 VIPERCoalescer::invTCPCallback(Addr addr)
 {
     assert(m_cache_inv_pkt && m_num_pending_invs > 0);
diff --git a/src/mem/ruby/system/VIPERCoalescer.hh b/src/mem/ruby/system/VIPERCoalescer.hh
index 7a88b1f..27e281c 100644
--- a/src/mem/ruby/system/VIPERCoalescer.hh
+++ b/src/mem/ruby/system/VIPERCoalescer.hh
@@ -59,7 +59,6 @@
     typedef VIPERCoalescerParams Params;
     VIPERCoalescer(const Params *);
     ~VIPERCoalescer();
-    void writeCompleteCallback(Addr address, uint64_t instSeqNum);
     void invTCPCallback(Addr address);
     RequestStatus makeRequest(PacketPtr pkt);
     void issueRequest(CoalescedRequest* crequest);
@@ -67,23 +66,11 @@
   private:
     void invTCP();
 
-    // make write-complete response packets from original write request packets
-    void makeWriteCompletePkts(CoalescedRequest* crequest);
-
     // current cache invalidation packet
     // nullptr if there is no active cache invalidation request
     PacketPtr m_cache_inv_pkt;
 
     // number of remaining cache lines to be invalidated in TCP
     int m_num_pending_invs;
-
-    // a map of instruction sequence number and corresponding pending
-    // write-complete response packets. Each write-complete response
-    // corresponds to a pending store request that is waiting for
-    // writeCompleteCallback. We may have multiple pending store requests per
-    // wavefront at a time. Each time writeCompleteCallback is called, an entry
-    // with a corresponding seqNum is popped off from map and returned to
-    // compute unit.
-    std::unordered_map<uint64_t, std::vector<PacketPtr>> m_writeCompletePktMap;
 };
 #endif //__MEM_RUBY_SYSTEM_VIPERCOALESCER_HH__