2 * Copyright (c) 2004-2006 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "config/use_checker.hh"
34 #include "arch/isa_traits.hh"
35 #include "arch/utility.hh"
36 #include "cpu/checker/cpu.hh"
37 #include "cpu/exetrace.hh"
38 #include "cpu/o3/fetch.hh"
39 #include "mem/packet.hh"
40 #include "mem/request.hh"
41 #include "sim/byteswap.hh"
42 #include "sim/host.hh"
43 #include "sim/root.hh"
46 #include "arch/tlb.hh"
47 #include "arch/vtophys.hh"
48 #include "sim/system.hh"
55 DefaultFetch<Impl>::IcachePort::recvAtomic(PacketPtr pkt)
57 panic("DefaultFetch doesn't expect recvAtomic callback!");
63 DefaultFetch<Impl>::IcachePort::recvFunctional(PacketPtr pkt)
65 DPRINTF(Fetch, "DefaultFetch doesn't update its state from a "
71 DefaultFetch<Impl>::IcachePort::recvStatusChange(Status status)
73 if (status == RangeChange)
76 panic("DefaultFetch doesn't expect recvStatusChange callback!");
81 DefaultFetch<Impl>::IcachePort::recvTiming(PacketPtr pkt)
83 DPRINTF(Fetch, "Received timing\n");
84 if (pkt->isResponse()) {
85 fetch->processCacheCompletion(pkt);
87 //else Snooped a coherence request, just return
93 DefaultFetch<Impl>::IcachePort::recvRetry()
99 DefaultFetch<Impl>::DefaultFetch(Params *params)
100 : branchPred(params),
101 decodeToFetchDelay(params->decodeToFetchDelay),
102 renameToFetchDelay(params->renameToFetchDelay),
103 iewToFetchDelay(params->iewToFetchDelay),
104 commitToFetchDelay(params->commitToFetchDelay),
105 fetchWidth(params->fetchWidth),
109 numThreads(params->numberOfThreads),
110 numFetchingThreads(params->smtNumFetchingThreads),
111 interruptPending(false),
115 if (numThreads > Impl::MaxThreads)
116 fatal("numThreads is not a valid value\n");
118 // Set fetch stage's status to inactive.
121 std::string policy = params->smtFetchPolicy;
123 // Convert string to lowercase
124 std::transform(policy.begin(), policy.end(), policy.begin(),
125 (int(*)(int)) tolower);
127 // Figure out fetch policy
128 if (policy == "singlethread") {
129 fetchPolicy = SingleThread;
131 panic("Invalid Fetch Policy for a SMT workload.");
132 } else if (policy == "roundrobin") {
133 fetchPolicy = RoundRobin;
134 DPRINTF(Fetch, "Fetch policy set to Round Robin\n");
135 } else if (policy == "branch") {
136 fetchPolicy = Branch;
137 DPRINTF(Fetch, "Fetch policy set to Branch Count\n");
138 } else if (policy == "iqcount") {
140 DPRINTF(Fetch, "Fetch policy set to IQ count\n");
141 } else if (policy == "lsqcount") {
143 DPRINTF(Fetch, "Fetch policy set to LSQ count\n");
145 fatal("Invalid Fetch Policy. Options Are: {SingleThread,"
146 " RoundRobin,LSQcount,IQcount}\n");
149 // Size of cache block.
152 // Create mask to get rid of offset bits.
153 cacheBlkMask = (cacheBlkSize - 1);
155 for (int tid=0; tid < numThreads; tid++) {
157 fetchStatus[tid] = Running;
159 priorityList.push_back(tid);
163 // Create space to store a cache line.
164 cacheData[tid] = new uint8_t[cacheBlkSize];
165 cacheDataPC[tid] = 0;
166 cacheDataValid[tid] = false;
168 delaySlotInfo[tid].branchSeqNum = -1;
169 delaySlotInfo[tid].numInsts = 0;
170 delaySlotInfo[tid].targetAddr = 0;
171 delaySlotInfo[tid].targetReady = false;
173 stalls[tid].decode = false;
174 stalls[tid].rename = false;
175 stalls[tid].iew = false;
176 stalls[tid].commit = false;
179 // Get the size of an instruction.
180 instSize = sizeof(TheISA::MachInst);
183 template <class Impl>
185 DefaultFetch<Impl>::name() const
187 return cpu->name() + ".fetch";
190 template <class Impl>
192 DefaultFetch<Impl>::regStats()
195 .name(name() + ".icacheStallCycles")
196 .desc("Number of cycles fetch is stalled on an Icache miss")
197 .prereq(icacheStallCycles);
200 .name(name() + ".Insts")
201 .desc("Number of instructions fetch has processed")
202 .prereq(fetchedInsts);
205 .name(name() + ".Branches")
206 .desc("Number of branches that fetch encountered")
207 .prereq(fetchedBranches);
210 .name(name() + ".predictedBranches")
211 .desc("Number of branches that fetch has predicted taken")
212 .prereq(predictedBranches);
215 .name(name() + ".Cycles")
216 .desc("Number of cycles fetch has run and was not squashing or"
218 .prereq(fetchCycles);
221 .name(name() + ".SquashCycles")
222 .desc("Number of cycles fetch has spent squashing")
223 .prereq(fetchSquashCycles);
226 .name(name() + ".IdleCycles")
227 .desc("Number of cycles fetch was idle")
228 .prereq(fetchIdleCycles);
231 .name(name() + ".BlockedCycles")
232 .desc("Number of cycles fetch has spent blocked")
233 .prereq(fetchBlockedCycles);
236 .name(name() + ".CacheLines")
237 .desc("Number of cache lines fetched")
238 .prereq(fetchedCacheLines);
241 .name(name() + ".MiscStallCycles")
242 .desc("Number of cycles fetch has spent waiting on interrupts, or "
243 "bad addresses, or out of MSHRs")
244 .prereq(fetchMiscStallCycles);
247 .name(name() + ".IcacheSquashes")
248 .desc("Number of outstanding Icache misses that were squashed")
249 .prereq(fetchIcacheSquashes);
252 .init(/* base value */ 0,
253 /* last value */ fetchWidth,
255 .name(name() + ".rateDist")
256 .desc("Number of instructions fetched each cycle (Total)")
260 .name(name() + ".idleRate")
261 .desc("Percent of cycles fetch was idle")
263 idleRate = fetchIdleCycles * 100 / cpu->numCycles;
266 .name(name() + ".branchRate")
267 .desc("Number of branch fetches per cycle")
268 .flags(Stats::total);
269 branchRate = fetchedBranches / cpu->numCycles;
272 .name(name() + ".rate")
273 .desc("Number of inst fetches per cycle")
274 .flags(Stats::total);
275 fetchRate = fetchedInsts / cpu->numCycles;
277 branchPred.regStats();
282 DefaultFetch<Impl>::setCPU(O3CPU *cpu_ptr)
284 DPRINTF(Fetch, "Setting the CPU pointer.\n");
287 // Name is finally available, so create the port.
288 icachePort = new IcachePort(this);
292 cpu->checker->setIcachePort(icachePort);
296 // Schedule fetch to get the correct PC from the CPU
297 // scheduleFetchStartupEvent(1);
299 // Fetch needs to start fetching instructions at the very beginning,
300 // so it must start up in active state.
306 DefaultFetch<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *time_buffer)
308 DPRINTF(Fetch, "Setting the time buffer pointer.\n");
309 timeBuffer = time_buffer;
311 // Create wires to get information from proper places in time buffer.
312 fromDecode = timeBuffer->getWire(-decodeToFetchDelay);
313 fromRename = timeBuffer->getWire(-renameToFetchDelay);
314 fromIEW = timeBuffer->getWire(-iewToFetchDelay);
315 fromCommit = timeBuffer->getWire(-commitToFetchDelay);
320 DefaultFetch<Impl>::setActiveThreads(std::list<unsigned> *at_ptr)
322 DPRINTF(Fetch, "Setting active threads list pointer.\n");
323 activeThreads = at_ptr;
328 DefaultFetch<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
330 DPRINTF(Fetch, "Setting the fetch queue pointer.\n");
333 // Create wire to write information to proper place in fetch queue.
334 toDecode = fetchQueue->getWire(0);
339 DefaultFetch<Impl>::initStage()
341 // Setup PC and nextPC with initial state.
342 for (int tid = 0; tid < numThreads; tid++) {
343 PC[tid] = cpu->readPC(tid);
344 nextPC[tid] = cpu->readNextPC(tid);
345 #if ISA_HAS_DELAY_SLOT
346 nextNPC[tid] = cpu->readNextNPC(tid);
353 DefaultFetch<Impl>::processCacheCompletion(PacketPtr pkt)
355 unsigned tid = pkt->req->getThreadNum();
357 DPRINTF(Fetch, "[tid:%u] Waking up from cache miss.\n",tid);
359 // Only change the status if it's still waiting on the icache access
361 if (fetchStatus[tid] != IcacheWaitResponse ||
362 pkt->req != memReq[tid] ||
364 ++fetchIcacheSquashes;
370 memcpy(cacheData[tid], pkt->getPtr<uint8_t *>(), cacheBlkSize);
371 cacheDataValid[tid] = true;
374 // Wake up the CPU (if it went to sleep and was waiting on
375 // this completion event).
378 DPRINTF(Activity, "[tid:%u] Activating fetch due to cache completion\n",
384 // Only switch to IcacheAccessComplete if we're not stalled as well.
385 if (checkStall(tid)) {
386 fetchStatus[tid] = Blocked;
388 fetchStatus[tid] = IcacheAccessComplete;
391 // Reset the mem req to NULL.
397 template <class Impl>
399 DefaultFetch<Impl>::drain()
401 // Fetch is ready to drain at any time.
402 cpu->signalDrained();
407 template <class Impl>
409 DefaultFetch<Impl>::resume()
411 drainPending = false;
414 template <class Impl>
416 DefaultFetch<Impl>::switchOut()
419 // Branch predictor needs to have its state cleared.
420 branchPred.switchOut();
423 template <class Impl>
425 DefaultFetch<Impl>::takeOverFrom()
428 for (int i = 0; i < Impl::MaxThreads; ++i) {
429 stalls[i].decode = 0;
430 stalls[i].rename = 0;
432 stalls[i].commit = 0;
433 PC[i] = cpu->readPC(i);
434 nextPC[i] = cpu->readNextPC(i);
435 #if ISA_HAS_DELAY_SLOT
436 nextNPC[i] = cpu->readNextNPC(i);
437 delaySlotInfo[i].branchSeqNum = -1;
438 delaySlotInfo[i].numInsts = 0;
439 delaySlotInfo[i].targetAddr = 0;
440 delaySlotInfo[i].targetReady = false;
442 fetchStatus[i] = Running;
445 wroteToTimeBuffer = false;
448 interruptPending = false;
449 branchPred.takeOverFrom();
452 template <class Impl>
454 DefaultFetch<Impl>::wakeFromQuiesce()
456 DPRINTF(Fetch, "Waking up from quiesce\n");
457 // Hopefully this is safe
458 // @todo: Allow other threads to wake from quiesce.
459 fetchStatus[0] = Running;
462 template <class Impl>
464 DefaultFetch<Impl>::switchToActive()
466 if (_status == Inactive) {
467 DPRINTF(Activity, "Activating stage.\n");
469 cpu->activateStage(O3CPU::FetchIdx);
475 template <class Impl>
477 DefaultFetch<Impl>::switchToInactive()
479 if (_status == Active) {
480 DPRINTF(Activity, "Deactivating stage.\n");
482 cpu->deactivateStage(O3CPU::FetchIdx);
488 template <class Impl>
490 DefaultFetch<Impl>::lookupAndUpdateNextPC(DynInstPtr &inst, Addr &next_PC,
493 // Do branch prediction check here.
494 // A bit of a misnomer...next_PC is actually the current PC until
495 // this function updates it.
498 if (!inst->isControl()) {
499 #if ISA_HAS_DELAY_SLOT
500 Addr cur_PC = next_PC;
501 next_PC = cur_PC + instSize; //next_NPC;
502 next_NPC = cur_PC + (2 * instSize);//next_NPC + instSize;
503 inst->setPredTarg(next_NPC);
505 next_PC = next_PC + instSize;
506 inst->setPredTarg(next_PC);
511 int tid = inst->threadNumber;
512 #if ISA_HAS_DELAY_SLOT
513 Addr pred_PC = next_PC;
514 predict_taken = branchPred.predict(inst, pred_PC, tid);
517 DPRINTF(Fetch, "[tid:%i]: Branch predicted to be true.\n", tid);
519 DPRINTF(Fetch, "[tid:%i]: Branch predicted to be false.\n", tid);
526 // Update delay slot info
527 ++delaySlotInfo[tid].numInsts;
528 delaySlotInfo[tid].targetAddr = pred_PC;
529 DPRINTF(Fetch, "[tid:%i]: %i delay slot inst(s) to process.\n", tid,
530 delaySlotInfo[tid].numInsts);
531 } else { // !predict_taken
532 if (inst->isCondDelaySlot()) {
534 // The delay slot is skipped here if there is on
538 // No need to declare a delay slot here since
539 // there is no for the pred. target to jump
542 next_NPC = next_NPC + instSize;
545 predict_taken = branchPred.predict(inst, next_PC, tid);
554 return predict_taken;
557 template <class Impl>
559 DefaultFetch<Impl>::fetchCacheLine(Addr fetch_PC, Fault &ret_fault, unsigned tid)
561 Fault fault = NoFault;
565 DPRINTF(Fetch, "[tid:%i] Can't fetch cache line, cache blocked\n",
568 } else if (isSwitchedOut()) {
569 DPRINTF(Fetch, "[tid:%i] Can't fetch cache line, switched out\n",
572 } else if (interruptPending && !(fetch_PC & 0x3)) {
573 // Hold off fetch from getting new instructions when:
574 // Cache is blocked, or
575 // while an interrupt is pending and we're not in PAL mode, or
576 // fetch is switched out.
577 DPRINTF(Fetch, "[tid:%i] Can't fetch cache line, interrupt pending\n",
582 // Align the fetch PC so it's at the start of a cache block.
583 Addr block_PC = icacheBlockAlignPC(fetch_PC);
585 // If we've already got the block, no need to try to fetch it again.
586 if (cacheDataValid[tid] && block_PC == cacheDataPC[tid]) {
590 // Setup the memReq to do a read of the first instruction's address.
591 // Set the appropriate read size and flags as well.
592 // Build request here.
593 RequestPtr mem_req = new Request(tid, block_PC, cacheBlkSize, 0,
594 fetch_PC, cpu->readCpuId(), tid);
596 memReq[tid] = mem_req;
598 // Translate the instruction request.
599 fault = cpu->translateInstReq(mem_req, cpu->thread[tid]);
601 // In the case of faults, the fetch stage may need to stall and wait
602 // for the ITB miss to be handled.
604 // If translation was successful, attempt to read the first
606 if (fault == NoFault) {
608 if (cpu->system->memctrl->badaddr(memReq[tid]->paddr) ||
609 memReq[tid]->isUncacheable()) {
610 DPRINTF(Fetch, "Fetch: Bad address %#x (hopefully on a "
611 "misspeculating path)!",
613 ret_fault = TheISA::genMachineCheckFault();
618 // Build packet here.
619 PacketPtr data_pkt = new Packet(mem_req,
620 Packet::ReadReq, Packet::Broadcast);
621 data_pkt->dataDynamicArray(new uint8_t[cacheBlkSize]);
623 cacheDataPC[tid] = block_PC;
624 cacheDataValid[tid] = false;
626 DPRINTF(Fetch, "Fetch: Doing instruction read.\n");
630 // Now do the timing access to see whether or not the instruction
631 // exists within the cache.
632 if (!icachePort->sendTiming(data_pkt)) {
633 if (data_pkt->result == Packet::BadAddress) {
634 fault = TheISA::genMachineCheckFault();
638 assert(retryPkt == NULL);
639 assert(retryTid == -1);
640 DPRINTF(Fetch, "[tid:%i] Out of MSHRs!\n", tid);
641 fetchStatus[tid] = IcacheWaitRetry;
648 DPRINTF(Fetch, "[tid:%i]: Doing cache access.\n", tid);
650 lastIcacheStall[tid] = curTick;
652 DPRINTF(Activity, "[tid:%i]: Activity: Waiting on I-cache "
655 fetchStatus[tid] = IcacheWaitResponse;
665 template <class Impl>
667 DefaultFetch<Impl>::doSquash(const Addr &new_PC, unsigned tid)
669 DPRINTF(Fetch, "[tid:%i]: Squashing, setting PC to: %#x.\n",
673 nextPC[tid] = new_PC + instSize;
674 nextNPC[tid] = new_PC + (2 * instSize);
676 // Clear the icache miss if it's outstanding.
677 if (fetchStatus[tid] == IcacheWaitResponse) {
678 DPRINTF(Fetch, "[tid:%i]: Squashing outstanding Icache miss.\n",
683 // Get rid of the retrying packet if it was from this thread.
684 if (retryTid == tid) {
685 assert(cacheBlocked);
686 cacheBlocked = false;
688 delete retryPkt->req;
693 fetchStatus[tid] = Squashing;
700 DefaultFetch<Impl>::squashFromDecode(const Addr &new_PC,
701 const InstSeqNum &seq_num,
704 DPRINTF(Fetch, "[tid:%i]: Squashing from decode.\n",tid);
706 doSquash(new_PC, tid);
708 #if ISA_HAS_DELAY_SLOT
709 if (seq_num <= delaySlotInfo[tid].branchSeqNum) {
710 delaySlotInfo[tid].numInsts = 0;
711 delaySlotInfo[tid].targetAddr = 0;
712 delaySlotInfo[tid].targetReady = false;
716 // Tell the CPU to remove any instructions that are in flight between
718 cpu->removeInstsUntil(seq_num, tid);
723 DefaultFetch<Impl>::checkStall(unsigned tid) const
725 bool ret_val = false;
727 if (cpu->contextSwitch) {
728 DPRINTF(Fetch,"[tid:%i]: Stalling for a context switch.\n",tid);
730 } else if (stalls[tid].decode) {
731 DPRINTF(Fetch,"[tid:%i]: Stall from Decode stage detected.\n",tid);
733 } else if (stalls[tid].rename) {
734 DPRINTF(Fetch,"[tid:%i]: Stall from Rename stage detected.\n",tid);
736 } else if (stalls[tid].iew) {
737 DPRINTF(Fetch,"[tid:%i]: Stall from IEW stage detected.\n",tid);
739 } else if (stalls[tid].commit) {
740 DPRINTF(Fetch,"[tid:%i]: Stall from Commit stage detected.\n",tid);
748 typename DefaultFetch<Impl>::FetchStatus
749 DefaultFetch<Impl>::updateFetchStatus()
752 std::list<unsigned>::iterator threads = (*activeThreads).begin();
754 while (threads != (*activeThreads).end()) {
756 unsigned tid = *threads++;
758 if (fetchStatus[tid] == Running ||
759 fetchStatus[tid] == Squashing ||
760 fetchStatus[tid] == IcacheAccessComplete) {
762 if (_status == Inactive) {
763 DPRINTF(Activity, "[tid:%i]: Activating stage.\n",tid);
765 if (fetchStatus[tid] == IcacheAccessComplete) {
766 DPRINTF(Activity, "[tid:%i]: Activating fetch due to cache"
770 cpu->activateStage(O3CPU::FetchIdx);
777 // Stage is switching from active to inactive, notify CPU of it.
778 if (_status == Active) {
779 DPRINTF(Activity, "Deactivating stage.\n");
781 cpu->deactivateStage(O3CPU::FetchIdx);
787 template <class Impl>
789 DefaultFetch<Impl>::squash(const Addr &new_PC, const InstSeqNum &seq_num,
790 bool squash_delay_slot, unsigned tid)
792 DPRINTF(Fetch, "[tid:%u]: Squash from commit.\n",tid);
794 doSquash(new_PC, tid);
796 #if ISA_HAS_DELAY_SLOT
797 if (seq_num <= delaySlotInfo[tid].branchSeqNum) {
798 delaySlotInfo[tid].numInsts = 0;
799 delaySlotInfo[tid].targetAddr = 0;
800 delaySlotInfo[tid].targetReady = false;
803 // Tell the CPU to remove any instructions that are not in the ROB.
804 cpu->removeInstsNotInROB(tid, squash_delay_slot, seq_num);
806 // Tell the CPU to remove any instructions that are not in the ROB.
807 cpu->removeInstsNotInROB(tid, true, 0);
811 template <class Impl>
813 DefaultFetch<Impl>::tick()
815 std::list<unsigned>::iterator threads = (*activeThreads).begin();
816 bool status_change = false;
818 wroteToTimeBuffer = false;
820 while (threads != (*activeThreads).end()) {
821 unsigned tid = *threads++;
823 // Check the signals for each thread to determine the proper status
825 bool updated_status = checkSignalsAndUpdate(tid);
826 status_change = status_change || updated_status;
829 DPRINTF(Fetch, "Running stage.\n");
831 // Reset the number of the instruction we're fetching.
835 if (fromCommit->commitInfo[0].interruptPending) {
836 interruptPending = true;
839 if (fromCommit->commitInfo[0].clearInterrupt) {
840 interruptPending = false;
844 for (threadFetched = 0; threadFetched < numFetchingThreads;
846 // Fetch each of the actively fetching threads.
847 fetch(status_change);
850 // Record number of instructions fetched this cycle for distribution.
851 fetchNisnDist.sample(numInst);
854 // Change the fetch stage status if there was a status change.
855 _status = updateFetchStatus();
858 // If there was activity this cycle, inform the CPU of it.
859 if (wroteToTimeBuffer || cpu->contextSwitch) {
860 DPRINTF(Activity, "Activity this cycle.\n");
862 cpu->activityThisCycle();
866 template <class Impl>
868 DefaultFetch<Impl>::checkSignalsAndUpdate(unsigned tid)
870 // Update the per thread stall statuses.
871 if (fromDecode->decodeBlock[tid]) {
872 stalls[tid].decode = true;
875 if (fromDecode->decodeUnblock[tid]) {
876 assert(stalls[tid].decode);
877 assert(!fromDecode->decodeBlock[tid]);
878 stalls[tid].decode = false;
881 if (fromRename->renameBlock[tid]) {
882 stalls[tid].rename = true;
885 if (fromRename->renameUnblock[tid]) {
886 assert(stalls[tid].rename);
887 assert(!fromRename->renameBlock[tid]);
888 stalls[tid].rename = false;
891 if (fromIEW->iewBlock[tid]) {
892 stalls[tid].iew = true;
895 if (fromIEW->iewUnblock[tid]) {
896 assert(stalls[tid].iew);
897 assert(!fromIEW->iewBlock[tid]);
898 stalls[tid].iew = false;
901 if (fromCommit->commitBlock[tid]) {
902 stalls[tid].commit = true;
905 if (fromCommit->commitUnblock[tid]) {
906 assert(stalls[tid].commit);
907 assert(!fromCommit->commitBlock[tid]);
908 stalls[tid].commit = false;
911 // Check squash signals from commit.
912 if (fromCommit->commitInfo[tid].squash) {
914 DPRINTF(Fetch, "[tid:%u]: Squashing instructions due to squash "
915 "from commit.\n",tid);
917 #if ISA_HAS_DELAY_SLOT
918 InstSeqNum doneSeqNum = fromCommit->commitInfo[tid].bdelayDoneSeqNum;
920 InstSeqNum doneSeqNum = fromCommit->commitInfo[tid].doneSeqNum;
922 // In any case, squash.
923 squash(fromCommit->commitInfo[tid].nextPC,
925 fromCommit->commitInfo[tid].squashDelaySlot,
928 // Also check if there's a mispredict that happened.
929 if (fromCommit->commitInfo[tid].branchMispredict) {
930 branchPred.squash(fromCommit->commitInfo[tid].doneSeqNum,
931 fromCommit->commitInfo[tid].nextPC,
932 fromCommit->commitInfo[tid].branchTaken,
935 branchPred.squash(fromCommit->commitInfo[tid].doneSeqNum,
940 } else if (fromCommit->commitInfo[tid].doneSeqNum) {
941 // Update the branch predictor if it wasn't a squashed instruction
942 // that was broadcasted.
943 branchPred.update(fromCommit->commitInfo[tid].doneSeqNum, tid);
946 // Check ROB squash signals from commit.
947 if (fromCommit->commitInfo[tid].robSquashing) {
948 DPRINTF(Fetch, "[tid:%u]: ROB is still squashing.\n", tid);
950 // Continue to squash.
951 fetchStatus[tid] = Squashing;
956 // Check squash signals from decode.
957 if (fromDecode->decodeInfo[tid].squash) {
958 DPRINTF(Fetch, "[tid:%u]: Squashing instructions due to squash "
959 "from decode.\n",tid);
961 // Update the branch predictor.
962 if (fromDecode->decodeInfo[tid].branchMispredict) {
963 branchPred.squash(fromDecode->decodeInfo[tid].doneSeqNum,
964 fromDecode->decodeInfo[tid].nextPC,
965 fromDecode->decodeInfo[tid].branchTaken,
968 branchPred.squash(fromDecode->decodeInfo[tid].doneSeqNum,
972 if (fetchStatus[tid] != Squashing) {
974 #if ISA_HAS_DELAY_SLOT
975 InstSeqNum doneSeqNum = fromDecode->decodeInfo[tid].bdelayDoneSeqNum;
977 InstSeqNum doneSeqNum = fromDecode->decodeInfo[tid].doneSeqNum;
979 // Squash unless we're already squashing
980 squashFromDecode(fromDecode->decodeInfo[tid].nextPC,
988 if (checkStall(tid) &&
989 fetchStatus[tid] != IcacheWaitResponse &&
990 fetchStatus[tid] != IcacheWaitRetry) {
991 DPRINTF(Fetch, "[tid:%i]: Setting to blocked\n",tid);
993 fetchStatus[tid] = Blocked;
998 if (fetchStatus[tid] == Blocked ||
999 fetchStatus[tid] == Squashing) {
1000 // Switch status to running if fetch isn't being told to block or
1001 // squash this cycle.
1002 DPRINTF(Fetch, "[tid:%i]: Done squashing, switching to running.\n",
1005 fetchStatus[tid] = Running;
1010 // If we've reached this point, we have not gotten any signals that
1011 // cause fetch to change its status. Fetch remains the same as before.
1015 template<class Impl>
1017 DefaultFetch<Impl>::fetch(bool &status_change)
1019 //////////////////////////////////////////
1020 // Start actual fetch
1021 //////////////////////////////////////////
1022 int tid = getFetchingThread(fetchPolicy);
1024 if (tid == -1 || drainPending) {
1025 DPRINTF(Fetch,"There are no more threads available to fetch from.\n");
1027 // Breaks looping condition in tick()
1028 threadFetched = numFetchingThreads;
1032 DPRINTF(Fetch, "Attempting to fetch from [tid:%i]\n", tid);
1035 Addr &fetch_PC = PC[tid];
1037 // Fault code for memory access.
1038 Fault fault = NoFault;
1040 // If returning from the delay of a cache miss, then update the status
1041 // to running, otherwise do the cache access. Possibly move this up
1042 // to tick() function.
1043 if (fetchStatus[tid] == IcacheAccessComplete) {
1044 DPRINTF(Fetch, "[tid:%i]: Icache miss is complete.\n",
1047 fetchStatus[tid] = Running;
1048 status_change = true;
1049 } else if (fetchStatus[tid] == Running) {
1050 DPRINTF(Fetch, "[tid:%i]: Attempting to translate and read "
1051 "instruction, starting at PC %08p.\n",
1054 bool fetch_success = fetchCacheLine(fetch_PC, fault, tid);
1055 if (!fetch_success) {
1057 ++icacheStallCycles;
1059 ++fetchMiscStallCycles;
1064 if (fetchStatus[tid] == Idle) {
1066 DPRINTF(Fetch, "[tid:%i]: Fetch is idle!\n", tid);
1067 } else if (fetchStatus[tid] == Blocked) {
1068 ++fetchBlockedCycles;
1069 DPRINTF(Fetch, "[tid:%i]: Fetch is blocked!\n", tid);
1070 } else if (fetchStatus[tid] == Squashing) {
1071 ++fetchSquashCycles;
1072 DPRINTF(Fetch, "[tid:%i]: Fetch is squashing!\n", tid);
1073 } else if (fetchStatus[tid] == IcacheWaitResponse) {
1074 ++icacheStallCycles;
1075 DPRINTF(Fetch, "[tid:%i]: Fetch is waiting cache response!\n", tid);
1078 // Status is Idle, Squashing, Blocked, or IcacheWaitResponse, so
1079 // fetch should do nothing.
1085 // If we had a stall due to an icache miss, then return.
1086 if (fetchStatus[tid] == IcacheWaitResponse) {
1087 ++icacheStallCycles;
1088 status_change = true;
1092 Addr next_PC = fetch_PC;
1093 Addr next_NPC = next_PC + instSize;
1094 InstSeqNum inst_seq;
1096 ExtMachInst ext_inst;
1097 // @todo: Fix this hack.
1098 unsigned offset = (fetch_PC & cacheBlkMask) & ~3;
1100 if (fault == NoFault) {
1101 // If the read of the first instruction was successful, then grab the
1102 // instructions from the rest of the cache line and put them into the
1103 // queue heading to decode.
1105 DPRINTF(Fetch, "[tid:%i]: Adding instructions to queue to "
1108 // Need to keep track of whether or not a predicted branch
1109 // ended this fetch block.
1110 bool predicted_branch = false;
1112 // Need to keep track of whether or not a delay slot
1113 // instruction has been fetched
1116 offset < cacheBlkSize &&
1117 numInst < fetchWidth &&
1118 (!predicted_branch || delaySlotInfo[tid].numInsts > 0);
1121 // Get a sequence number.
1122 inst_seq = cpu->getAndIncrementInstSeq();
1124 // Make sure this is a valid index.
1125 assert(offset <= cacheBlkSize - instSize);
1127 // Get the instruction from the array of the cache line.
1128 inst = TheISA::gtoh(*reinterpret_cast<TheISA::MachInst *>
1129 (&cacheData[tid][offset]));
1131 #if THE_ISA == ALPHA_ISA
1132 ext_inst = TheISA::makeExtMI(inst, fetch_PC);
1133 #elif THE_ISA == SPARC_ISA
1134 ext_inst = TheISA::makeExtMI(inst, cpu->thread[tid]->getTC());
1137 // Create a new DynInst from the instruction fetched.
1138 DynInstPtr instruction = new DynInst(ext_inst, fetch_PC,
1141 instruction->setTid(tid);
1143 instruction->setASID(tid);
1145 instruction->setThreadState(cpu->thread[tid]);
1147 DPRINTF(Fetch, "[tid:%i]: Instruction PC %#x created "
1149 tid, instruction->readPC(), inst_seq);
1151 DPRINTF(Fetch, "[tid:%i]: Instruction is: %s\n",
1152 tid, instruction->staticInst->disassemble(fetch_PC));
1154 instruction->traceData =
1155 Trace::getInstRecord(curTick, cpu->tcBase(tid),
1156 instruction->staticInst,
1157 instruction->readPC());
1159 predicted_branch = lookupAndUpdateNextPC(instruction, next_PC,
1162 // Add instruction to the CPU's list of instructions.
1163 instruction->setInstListIt(cpu->addInst(instruction));
1165 // Write the instruction to the first slot in the queue
1166 // that heads to decode.
1167 toDecode->insts[numInst] = instruction;
1171 // Increment stat of fetched instructions.
1174 // Move to the next instruction, unless we have a branch.
1177 if (instruction->isQuiesce()) {
1178 DPRINTF(Fetch, "Quiesce instruction encountered, halting fetch!",
1180 fetchStatus[tid] = QuiescePending;
1182 status_change = true;
1188 #if ISA_HAS_DELAY_SLOT
1189 if (predicted_branch) {
1190 delaySlotInfo[tid].branchSeqNum = inst_seq;
1192 DPRINTF(Fetch, "[tid:%i]: Delay slot branch set to [sn:%i]\n",
1195 } else if (delaySlotInfo[tid].numInsts > 0) {
1196 --delaySlotInfo[tid].numInsts;
1198 // It's OK to set PC to target of branch
1199 if (delaySlotInfo[tid].numInsts == 0) {
1200 delaySlotInfo[tid].targetReady = true;
1202 // Break the looping condition
1203 predicted_branch = true;
1206 DPRINTF(Fetch, "[tid:%i]: %i delay slot inst(s) left to"
1207 " process.\n", tid, delaySlotInfo[tid].numInsts);
1212 if (offset >= cacheBlkSize) {
1213 DPRINTF(Fetch, "[tid:%i]: Done fetching, reached the end of cache "
1215 } else if (numInst >= fetchWidth) {
1216 DPRINTF(Fetch, "[tid:%i]: Done fetching, reached fetch bandwidth "
1217 "for this cycle.\n", tid);
1218 } else if (predicted_branch && delaySlotInfo[tid].numInsts <= 0) {
1219 DPRINTF(Fetch, "[tid:%i]: Done fetching, predicted branch "
1220 "instruction encountered.\n", tid);
1225 wroteToTimeBuffer = true;
1228 // Now that fetching is completed, update the PC to signify what the next
1230 if (fault == NoFault) {
1231 #if ISA_HAS_DELAY_SLOT
1232 if (delaySlotInfo[tid].targetReady &&
1233 delaySlotInfo[tid].numInsts == 0) {
1235 PC[tid] = delaySlotInfo[tid].targetAddr; //next_PC
1236 nextPC[tid] = next_PC + instSize; //next_NPC
1237 nextNPC[tid] = next_PC + (2 * instSize);
1239 delaySlotInfo[tid].targetReady = false;
1242 nextPC[tid] = next_NPC;
1243 nextNPC[tid] = next_NPC + instSize;
1246 DPRINTF(Fetch, "[tid:%i]: Setting PC to %08p.\n", tid, PC[tid]);
1248 DPRINTF(Fetch, "[tid:%i]: Setting PC to %08p.\n",tid, next_PC);
1250 nextPC[tid] = next_PC + instSize;
1253 // We shouldn't be in an icache miss and also have a fault (an ITB
1255 if (fetchStatus[tid] == IcacheWaitResponse) {
1256 panic("Fetch should have exited prior to this!");
1259 // Send the fault to commit. This thread will not do anything
1260 // until commit handles the fault. The only other way it can
1261 // wake up is if a squash comes along and changes the PC.
1263 assert(numInst != fetchWidth);
1264 // Get a sequence number.
1265 inst_seq = cpu->getAndIncrementInstSeq();
1266 // We will use a nop in order to carry the fault.
1267 ext_inst = TheISA::NoopMachInst;
1269 // Create a new DynInst from the dummy nop.
1270 DynInstPtr instruction = new DynInst(ext_inst, fetch_PC,
1273 instruction->setPredTarg(next_PC + instSize);
1274 instruction->setTid(tid);
1276 instruction->setASID(tid);
1278 instruction->setThreadState(cpu->thread[tid]);
1280 instruction->traceData = NULL;
1282 instruction->setInstListIt(cpu->addInst(instruction));
1284 instruction->fault = fault;
1286 toDecode->insts[numInst] = instruction;
1289 DPRINTF(Fetch, "[tid:%i]: Blocked, need to handle the trap.\n",tid);
1291 fetchStatus[tid] = TrapPending;
1292 status_change = true;
1293 #else // !FULL_SYSTEM
1294 fetchStatus[tid] = TrapPending;
1295 status_change = true;
1297 #endif // FULL_SYSTEM
1298 DPRINTF(Fetch, "[tid:%i]: fault (%s) detected @ PC %08p",
1299 tid, fault->name(), PC[tid]);
1303 template<class Impl>
1305 DefaultFetch<Impl>::recvRetry()
1307 if (retryPkt != NULL) {
1308 assert(cacheBlocked);
1309 assert(retryTid != -1);
1310 assert(fetchStatus[retryTid] == IcacheWaitRetry);
1312 if (icachePort->sendTiming(retryPkt)) {
1313 fetchStatus[retryTid] = IcacheWaitResponse;
1316 cacheBlocked = false;
1319 assert(retryTid == -1);
1320 // Access has been squashed since it was sent out. Just clear
1321 // the cache being blocked.
1322 cacheBlocked = false;
1326 ///////////////////////////////////////
1328 // SMT FETCH POLICY MAINTAINED HERE //
1330 ///////////////////////////////////////
1331 template<class Impl>
1333 DefaultFetch<Impl>::getFetchingThread(FetchPriority &fetch_priority)
1335 if (numThreads > 1) {
1336 switch (fetch_priority) {
1342 return roundRobin();
1351 return branchCount();
1357 int tid = *((*activeThreads).begin());
1359 if (fetchStatus[tid] == Running ||
1360 fetchStatus[tid] == IcacheAccessComplete ||
1361 fetchStatus[tid] == Idle) {
1371 template<class Impl>
1373 DefaultFetch<Impl>::roundRobin()
1375 std::list<unsigned>::iterator pri_iter = priorityList.begin();
1376 std::list<unsigned>::iterator end = priorityList.end();
1380 while (pri_iter != end) {
1381 high_pri = *pri_iter;
1383 assert(high_pri <= numThreads);
1385 if (fetchStatus[high_pri] == Running ||
1386 fetchStatus[high_pri] == IcacheAccessComplete ||
1387 fetchStatus[high_pri] == Idle) {
1389 priorityList.erase(pri_iter);
1390 priorityList.push_back(high_pri);
1401 template<class Impl>
1403 DefaultFetch<Impl>::iqCount()
1405 std::priority_queue<unsigned> PQ;
1407 std::list<unsigned>::iterator threads = (*activeThreads).begin();
1409 while (threads != (*activeThreads).end()) {
1410 unsigned tid = *threads++;
1412 PQ.push(fromIEW->iewInfo[tid].iqCount);
1415 while (!PQ.empty()) {
1417 unsigned high_pri = PQ.top();
1419 if (fetchStatus[high_pri] == Running ||
1420 fetchStatus[high_pri] == IcacheAccessComplete ||
1421 fetchStatus[high_pri] == Idle)
1431 template<class Impl>
1433 DefaultFetch<Impl>::lsqCount()
1435 std::priority_queue<unsigned> PQ;
1438 std::list<unsigned>::iterator threads = (*activeThreads).begin();
1440 while (threads != (*activeThreads).end()) {
1441 unsigned tid = *threads++;
1443 PQ.push(fromIEW->iewInfo[tid].ldstqCount);
1446 while (!PQ.empty()) {
1448 unsigned high_pri = PQ.top();
1450 if (fetchStatus[high_pri] == Running ||
1451 fetchStatus[high_pri] == IcacheAccessComplete ||
1452 fetchStatus[high_pri] == Idle)
1462 template<class Impl>
1464 DefaultFetch<Impl>::branchCount()
1466 std::list<unsigned>::iterator threads = (*activeThreads).begin();
1467 panic("Branch Count Fetch policy unimplemented\n");