cpu: Update DRAM traffic gen
[gem5.git] / src / cpu / inorder / resources / fetch_unit.cc
index 04b80fbc9bb48217b832123f01bf08d95b74ccb8..6892688b2828b83391ea405b628741ee4c43a419 100644 (file)
@@ -34,7 +34,6 @@
 
 #include "arch/isa_traits.hh"
 #include "arch/locked_mem.hh"
-#include "arch/predecoder.hh"
 #include "arch/utility.hh"
 #include "config/the_isa.hh"
 #include "cpu/inorder/resources/cache_unit.hh"
@@ -54,12 +53,14 @@ using namespace TheISA;
 using namespace ThePipeline;
 
 FetchUnit::FetchUnit(string res_name, int res_id, int res_width,
-                     int res_latency, InOrderCPU *_cpu,
+                     Cycles res_latency, InOrderCPU *_cpu,
                      ThePipeline::Params *params)
     : CacheUnit(res_name, res_id, res_width, res_latency, _cpu, params),
-      instSize(sizeof(TheISA::MachInst)), fetchBuffSize(params->fetchBuffSize),
-      predecoder(NULL)
-{ }
+      instSize(sizeof(TheISA::MachInst)), fetchBuffSize(params->fetchBuffSize)
+{
+    for (int tid = 0; tid < MaxThreads; tid++)
+        decoder[tid] = new Decoder;
+}
 
 FetchUnit::~FetchUnit()
 {
@@ -90,7 +91,6 @@ void
 FetchUnit::createMachInst(std::list<FetchBlock*>::iterator fetch_it,
                           DynInstPtr inst)
 {
-    ExtMachInst ext_inst;
     Addr block_addr = cacheBlockAlign(inst->getMemAddr());
     Addr fetch_addr = inst->getMemAddr();
     unsigned fetch_offset = (fetch_addr - block_addr) / instSize;
@@ -109,34 +109,10 @@ FetchUnit::createMachInst(std::list<FetchBlock*>::iterator fetch_it,
     MachInst mach_inst =
         TheISA::gtoh(fetchInsts[fetch_offset]);
 
-    predecoder.setTC(cpu->thread[tid]->getTC());
-    predecoder.moreBytes(instPC, inst->instAddr(), mach_inst);
-    assert(predecoder.extMachInstReady());
-    ext_inst = predecoder.getExtMachInst(instPC);
-
+    decoder[tid]->moreBytes(instPC, inst->instAddr(), mach_inst);
+    assert(decoder[tid]->instReady());
+    inst->setStaticInst(decoder[tid]->decode(instPC));
     inst->pcState(instPC);
-    inst->setMachInst(ext_inst);
-}
-
-int
-FetchUnit::getSlot(DynInstPtr inst)
-{
-    if (tlbBlocked[inst->threadNumber]) {
-        return -1;
-    }
-
-    if (!inst->validMemAddr()) {
-        panic("[tid:%i][sn:%i] Mem. Addr. must be set before requesting "
-              "cache access\n", inst->readTid(), inst->seqNum);
-    }
-
-    int new_slot = Resource::getSlot(inst);
-
-    if (new_slot == -1)
-        return -1;
-
-    inst->memTime = curTick();
-    return new_slot;
 }
 
 void
@@ -175,9 +151,14 @@ FetchUnit::setupMemRequest(DynInstPtr inst, CacheReqPtr cache_req,
 {
     ThreadID tid = inst->readTid();
     Addr aligned_addr = cacheBlockAlign(inst->getMemAddr());
-    cache_req->memReq =
+    if (cache_req->memReq == NULL) {
+        cache_req->memReq =
             new Request(tid, aligned_addr, acc_size, flags,
-                        inst->instAddr(), cpu->readCpuId(), tid);
+                        cpu->instMasterId(), inst->instAddr(), cpu->readCpuId(),
+                        tid);
+        DPRINTF(InOrderCachePort, "[sn:%i] Created memReq @%x, ->%x\n",
+                inst->seqNum, &cache_req->memReq, cache_req->memReq);
+    }
 }
 
 std::list<FetchUnit::FetchBlock*>::iterator
@@ -230,6 +211,39 @@ FetchUnit::markBlockUsed(std::list<FetchBlock*>::iterator block_it)
     }
 }
 
+int
+FetchUnit::blocksInUse()
+{
+    std::list<FetchBlock*>::iterator fetch_it = fetchBuffer.begin();
+    std::list<FetchBlock*>::iterator end_it = fetchBuffer.end();
+
+    int cnt = 0;
+    while (fetch_it != end_it) {
+        if ((*fetch_it)->cnt > 0)
+            cnt++;
+
+        fetch_it++;
+    }
+
+    return cnt;
+}
+
+void
+FetchUnit::clearFetchBuffer()
+{
+    std::list<FetchBlock*>::iterator fetch_it = fetchBuffer.begin();
+    std::list<FetchBlock*>::iterator end_it = fetchBuffer.end();
+
+    while (fetch_it != end_it) {
+        if ((*fetch_it)->block) {
+            delete [] (*fetch_it)->block;
+        }
+        delete *fetch_it;
+        fetch_it++;
+    }
+    fetchBuffer.clear();
+}
+
 void
 FetchUnit::execute(int slot_num)
 {
@@ -247,7 +261,14 @@ FetchUnit::execute(int slot_num)
     Addr block_addr = cacheBlockAlign(inst->getMemAddr());
     int asid = cpu->asid[tid];
 
-    inst->fault = NoFault;
+    if (inst->fault != NoFault) {
+        DPRINTF(InOrderCachePort,
+                "[tid:%i]: [sn:%i]: Detected %s fault @ %x. Forwarding to "
+                "next stage.\n", tid, inst->seqNum, inst->fault->name(),
+                cacheBlockAlign(inst->getMemAddr()));
+        finishCacheUnitReq(inst, cache_req);
+        return;
+    }
 
     switch (cache_req->cmd)
     {
@@ -256,6 +277,8 @@ FetchUnit::execute(int slot_num)
             // Check to see if we've already got this request buffered
             // or pending to be buffered
             bool do_fetch = true;
+            int total_pending = pendingFetch.size() + blocksInUse();
+
             std::list<FetchBlock*>::iterator pending_it;
             pending_it = findBlock(pendingFetch, asid, block_addr);
             if (pending_it != pendingFetch.end()) {
@@ -265,14 +288,14 @@ FetchUnit::execute(int slot_num)
                 DPRINTF(InOrderCachePort, "%08p is a pending fetch block "
                         "(pending:%i).\n", block_addr,
                         (*pending_it)->cnt);
-            } else if (pendingFetch.size() < fetchBuffSize) {
+            } else if (total_pending < fetchBuffSize) {
                 std::list<FetchBlock*>::iterator buff_it;
                 buff_it = findBlock(fetchBuffer, asid, block_addr);
-                if (buff_it  != fetchBuffer.end()) {
+                if (buff_it != fetchBuffer.end()) {
                     (*buff_it)->cnt++;
                     do_fetch = false;
 
-                    DPRINTF(InOrderCachePort, "%08p is in fetch buffer"
+                    DPRINTF(InOrderCachePort, "%08p is in fetch buffer "
                             "(pending:%i).\n", block_addr, (*buff_it)->cnt);
                 }
             }
@@ -287,13 +310,13 @@ FetchUnit::execute(int slot_num)
 
             // Check to see if there is room in the fetchbuffer for this instruction.
             // If not, block this request.
-            if (pendingFetch.size() >= fetchBuffSize) {
+            if (total_pending >= fetchBuffSize) {
                 DPRINTF(InOrderCachePort, "No room available in fetch buffer.\n");
-                cache_req->done();
+                cache_req->done(false);
                 return;
             }
 
-            doTLBAccess(inst, cache_req, cacheBlkSize, 0, TheISA::TLB::Execute);
+            doTLBAccess(inst, cache_req, cacheBlkSize, Request::INST_FETCH, TheISA::TLB::Execute);
 
             if (inst->fault == NoFault) {
                 DPRINTF(InOrderCachePort,
@@ -309,6 +332,8 @@ FetchUnit::execute(int slot_num)
 
                 if (cache_req->isMemAccPending()) {
                     pendingFetch.push_back(new FetchBlock(asid, block_addr));
+
+                    // mark replacement block
                 }
             }
 
@@ -316,6 +341,15 @@ FetchUnit::execute(int slot_num)
         }
 
       case CompleteFetch:
+        if (inst->fault != NoFault) {
+            DPRINTF(InOrderCachePort,
+                "[tid:%i]: [sn:%i]: Detected %s fault @ %x. Forwarding to "
+                "next stage.\n", tid, inst->seqNum, inst->fault->name(),
+                inst->getMemAddr());
+            finishCacheUnitReq(inst, cache_req);
+            return;
+        }
+
         if (cache_req->fetchBufferFill) {
             // Block request if it's depending on a previous fetch, but it hasnt made it yet
             std::list<FetchBlock*>::iterator fetch_it = findBlock(fetchBuffer, asid, block_addr);
@@ -419,6 +453,8 @@ void
 FetchUnit::processCacheCompletion(PacketPtr pkt)
 {
     // Cast to correct packet type
+    // @todo: use pkt Sender state here to be consistent with other
+    // cpu models
     CacheReqPacket* cache_pkt = dynamic_cast<CacheReqPacket*>(pkt);
     assert(cache_pkt);
 
@@ -509,19 +545,22 @@ FetchUnit::squashCacheRequest(CacheReqPtr req_ptr)
                                                          block_addr);
     if (buff_it != fetchBuffer.end()) {
         (*buff_it)->cnt--;
-        DPRINTF(InOrderCachePort, "[sn:%i] Removing Pending Fetch "
-                "for Buffer block %08p (cnt=%i)\n", inst->seqNum,
+        DPRINTF(InOrderCachePort, "[sn:%i] Removing Pending Access "
+                "for Fetch Buffer block %08p (cnt=%i)\n", inst->seqNum,
                 block_addr, (*buff_it)->cnt);
+        assert((*buff_it)->cnt >= 0);
     } else {
         std::list<FetchBlock*>::iterator block_it = findBlock(pendingFetch,
                                                               asid,
                                                               block_addr);
         if (block_it != pendingFetch.end()) {
             (*block_it)->cnt--;
+            DPRINTF(InOrderCachePort, "[sn:%i] Removing Pending Access "
+                    "for Pending Buffer Block %08p (cnt=%i)\n",
+                    inst->seqNum,
+                    block_addr, (*block_it)->cnt);
+            assert((*block_it)->cnt >= 0);
             if ((*block_it)->cnt == 0) {
-                DPRINTF(InOrderCachePort, "[sn:%i] Removing Pending Fetch "
-                        "for block %08p (cnt=%i)\n", inst->seqNum,
-                        block_addr, (*block_it)->cnt);
                 if ((*block_it)->block) {
                     delete [] (*block_it)->block;
                 }
@@ -535,8 +574,18 @@ FetchUnit::squashCacheRequest(CacheReqPtr req_ptr)
 }
 
 void
-FetchUnit::trap(Fault fault, ThreadID tid, DynInstPtr inst)
+FetchUnit::trap(const Fault &fault, ThreadID tid, DynInstPtr inst)
 {
     //@todo: per thread?
-    predecoder.reset();
+    decoder[tid]->reset();
+
+    //@todo: squash using dummy inst seq num
+    squash(NULL, NumStages - 1, 0, tid);
+
+    //@todo: make sure no blocks are in use
+    assert(blocksInUse() == 0);
+    assert(pendingFetch.size() == 0);
+
+    //@todo: clear pendingFetch and fetchBuffer
+    clearFetchBuffer();
 }