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/core.hh"
46 #include "arch/tlb.hh"
47 #include "arch/vtophys.hh"
48 #include "sim/system.hh"
55 DefaultFetch<Impl>::IcachePort::setPeer(Port *port)
64 DefaultFetch<Impl>::IcachePort::recvAtomic(PacketPtr pkt)
66 panic("DefaultFetch doesn't expect recvAtomic callback!");
72 DefaultFetch<Impl>::IcachePort::recvFunctional(PacketPtr pkt)
74 DPRINTF(Fetch, "DefaultFetch doesn't update its state from a "
80 DefaultFetch<Impl>::IcachePort::recvStatusChange(Status status)
82 if (status == RangeChange) {
83 if (!snoopRangeSent) {
84 snoopRangeSent = true;
85 sendStatusChange(Port::RangeChange);
90 panic("DefaultFetch doesn't expect recvStatusChange callback!");
95 DefaultFetch<Impl>::IcachePort::recvTiming(PacketPtr pkt)
97 DPRINTF(Fetch, "Received timing\n");
98 if (pkt->isResponse()) {
99 fetch->processCacheCompletion(pkt);
101 //else Snooped a coherence request, just return
107 DefaultFetch<Impl>::IcachePort::recvRetry()
113 DefaultFetch<Impl>::DefaultFetch(O3CPU *_cpu, Params *params)
117 decodeToFetchDelay(params->decodeToFetchDelay),
118 renameToFetchDelay(params->renameToFetchDelay),
119 iewToFetchDelay(params->iewToFetchDelay),
120 commitToFetchDelay(params->commitToFetchDelay),
121 fetchWidth(params->fetchWidth),
125 numThreads(params->numberOfThreads),
126 numFetchingThreads(params->smtNumFetchingThreads),
127 interruptPending(false),
131 if (numThreads > Impl::MaxThreads)
132 fatal("numThreads is not a valid value\n");
134 // Set fetch stage's status to inactive.
137 std::string policy = params->smtFetchPolicy;
139 // Convert string to lowercase
140 std::transform(policy.begin(), policy.end(), policy.begin(),
141 (int(*)(int)) tolower);
143 // Figure out fetch policy
144 if (policy == "singlethread") {
145 fetchPolicy = SingleThread;
147 panic("Invalid Fetch Policy for a SMT workload.");
148 } else if (policy == "roundrobin") {
149 fetchPolicy = RoundRobin;
150 DPRINTF(Fetch, "Fetch policy set to Round Robin\n");
151 } else if (policy == "branch") {
152 fetchPolicy = Branch;
153 DPRINTF(Fetch, "Fetch policy set to Branch Count\n");
154 } else if (policy == "iqcount") {
156 DPRINTF(Fetch, "Fetch policy set to IQ count\n");
157 } else if (policy == "lsqcount") {
159 DPRINTF(Fetch, "Fetch policy set to LSQ count\n");
161 fatal("Invalid Fetch Policy. Options Are: {SingleThread,"
162 " RoundRobin,LSQcount,IQcount}\n");
165 // Get the size of an instruction.
166 instSize = sizeof(TheISA::MachInst);
168 // Name is finally available, so create the port.
169 icachePort = new IcachePort(this);
171 icachePort->snoopRangeSent = false;
175 cpu->checker->setIcachePort(icachePort);
180 template <class Impl>
182 DefaultFetch<Impl>::name() const
184 return cpu->name() + ".fetch";
187 template <class Impl>
189 DefaultFetch<Impl>::regStats()
192 .name(name() + ".icacheStallCycles")
193 .desc("Number of cycles fetch is stalled on an Icache miss")
194 .prereq(icacheStallCycles);
197 .name(name() + ".Insts")
198 .desc("Number of instructions fetch has processed")
199 .prereq(fetchedInsts);
202 .name(name() + ".Branches")
203 .desc("Number of branches that fetch encountered")
204 .prereq(fetchedBranches);
207 .name(name() + ".predictedBranches")
208 .desc("Number of branches that fetch has predicted taken")
209 .prereq(predictedBranches);
212 .name(name() + ".Cycles")
213 .desc("Number of cycles fetch has run and was not squashing or"
215 .prereq(fetchCycles);
218 .name(name() + ".SquashCycles")
219 .desc("Number of cycles fetch has spent squashing")
220 .prereq(fetchSquashCycles);
223 .name(name() + ".IdleCycles")
224 .desc("Number of cycles fetch was idle")
225 .prereq(fetchIdleCycles);
228 .name(name() + ".BlockedCycles")
229 .desc("Number of cycles fetch has spent blocked")
230 .prereq(fetchBlockedCycles);
233 .name(name() + ".CacheLines")
234 .desc("Number of cache lines fetched")
235 .prereq(fetchedCacheLines);
238 .name(name() + ".MiscStallCycles")
239 .desc("Number of cycles fetch has spent waiting on interrupts, or "
240 "bad addresses, or out of MSHRs")
241 .prereq(fetchMiscStallCycles);
244 .name(name() + ".IcacheSquashes")
245 .desc("Number of outstanding Icache misses that were squashed")
246 .prereq(fetchIcacheSquashes);
249 .init(/* base value */ 0,
250 /* last value */ fetchWidth,
252 .name(name() + ".rateDist")
253 .desc("Number of instructions fetched each cycle (Total)")
257 .name(name() + ".idleRate")
258 .desc("Percent of cycles fetch was idle")
260 idleRate = fetchIdleCycles * 100 / cpu->numCycles;
263 .name(name() + ".branchRate")
264 .desc("Number of branch fetches per cycle")
265 .flags(Stats::total);
266 branchRate = fetchedBranches / cpu->numCycles;
269 .name(name() + ".rate")
270 .desc("Number of inst fetches per cycle")
271 .flags(Stats::total);
272 fetchRate = fetchedInsts / cpu->numCycles;
274 branchPred.regStats();
279 DefaultFetch<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *time_buffer)
281 timeBuffer = time_buffer;
283 // Create wires to get information from proper places in time buffer.
284 fromDecode = timeBuffer->getWire(-decodeToFetchDelay);
285 fromRename = timeBuffer->getWire(-renameToFetchDelay);
286 fromIEW = timeBuffer->getWire(-iewToFetchDelay);
287 fromCommit = timeBuffer->getWire(-commitToFetchDelay);
292 DefaultFetch<Impl>::setActiveThreads(std::list<unsigned> *at_ptr)
294 activeThreads = at_ptr;
299 DefaultFetch<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
303 // Create wire to write information to proper place in fetch queue.
304 toDecode = fetchQueue->getWire(0);
309 DefaultFetch<Impl>::initStage()
311 // Setup PC and nextPC with initial state.
312 for (int tid = 0; tid < numThreads; tid++) {
313 PC[tid] = cpu->readPC(tid);
314 nextPC[tid] = cpu->readNextPC(tid);
315 microPC[tid] = cpu->readMicroPC(tid);
318 for (int tid=0; tid < numThreads; tid++) {
320 fetchStatus[tid] = Running;
322 priorityList.push_back(tid);
326 stalls[tid].decode = false;
327 stalls[tid].rename = false;
328 stalls[tid].iew = false;
329 stalls[tid].commit = false;
332 // Schedule fetch to get the correct PC from the CPU
333 // scheduleFetchStartupEvent(1);
335 // Fetch needs to start fetching instructions at the very beginning,
336 // so it must start up in active state.
342 DefaultFetch<Impl>::setIcache()
344 // Size of cache block.
345 cacheBlkSize = icachePort->peerBlockSize();
347 // Create mask to get rid of offset bits.
348 cacheBlkMask = (cacheBlkSize - 1);
350 for (int tid=0; tid < numThreads; tid++) {
351 // Create space to store a cache line.
352 cacheData[tid] = new uint8_t[cacheBlkSize];
353 cacheDataPC[tid] = 0;
354 cacheDataValid[tid] = false;
360 DefaultFetch<Impl>::processCacheCompletion(PacketPtr pkt)
362 unsigned tid = pkt->req->getThreadNum();
364 DPRINTF(Fetch, "[tid:%u] Waking up from cache miss.\n",tid);
366 // Only change the status if it's still waiting on the icache access
368 if (fetchStatus[tid] != IcacheWaitResponse ||
369 pkt->req != memReq[tid] ||
371 ++fetchIcacheSquashes;
377 memcpy(cacheData[tid], pkt->getPtr<uint8_t *>(), cacheBlkSize);
378 cacheDataValid[tid] = true;
381 // Wake up the CPU (if it went to sleep and was waiting on
382 // this completion event).
385 DPRINTF(Activity, "[tid:%u] Activating fetch due to cache completion\n",
391 // Only switch to IcacheAccessComplete if we're not stalled as well.
392 if (checkStall(tid)) {
393 fetchStatus[tid] = Blocked;
395 fetchStatus[tid] = IcacheAccessComplete;
398 // Reset the mem req to NULL.
404 template <class Impl>
406 DefaultFetch<Impl>::drain()
408 // Fetch is ready to drain at any time.
409 cpu->signalDrained();
414 template <class Impl>
416 DefaultFetch<Impl>::resume()
418 drainPending = false;
421 template <class Impl>
423 DefaultFetch<Impl>::switchOut()
426 // Branch predictor needs to have its state cleared.
427 branchPred.switchOut();
430 template <class Impl>
432 DefaultFetch<Impl>::takeOverFrom()
435 for (int i = 0; i < Impl::MaxThreads; ++i) {
436 stalls[i].decode = 0;
437 stalls[i].rename = 0;
439 stalls[i].commit = 0;
440 PC[i] = cpu->readPC(i);
441 nextPC[i] = cpu->readNextPC(i);
442 microPC[i] = cpu->readMicroPC(i);
443 fetchStatus[i] = Running;
446 wroteToTimeBuffer = false;
449 interruptPending = false;
450 branchPred.takeOverFrom();
453 template <class Impl>
455 DefaultFetch<Impl>::wakeFromQuiesce()
457 DPRINTF(Fetch, "Waking up from quiesce\n");
458 // Hopefully this is safe
459 // @todo: Allow other threads to wake from quiesce.
460 fetchStatus[0] = Running;
463 template <class Impl>
465 DefaultFetch<Impl>::switchToActive()
467 if (_status == Inactive) {
468 DPRINTF(Activity, "Activating stage.\n");
470 cpu->activateStage(O3CPU::FetchIdx);
476 template <class Impl>
478 DefaultFetch<Impl>::switchToInactive()
480 if (_status == Active) {
481 DPRINTF(Activity, "Deactivating stage.\n");
483 cpu->deactivateStage(O3CPU::FetchIdx);
489 template <class Impl>
491 DefaultFetch<Impl>::lookupAndUpdateNextPC(DynInstPtr &inst, Addr &next_PC,
492 Addr &next_NPC, Addr &next_MicroPC)
494 // Do branch prediction check here.
495 // A bit of a misnomer...next_PC is actually the current PC until
496 // this function updates it.
499 if (!inst->isControl()) {
500 if (inst->isMicroOp() && !inst->isLastMicroOp()) {
504 next_NPC = next_NPC + instSize;
507 inst->setPredTarg(next_PC, next_NPC, next_MicroPC);
508 inst->setPredTaken(false);
512 //Assume for now that all control flow is to a different macroop which
513 //would reset the micro pc to 0.
516 int tid = inst->threadNumber;
517 Addr pred_PC = next_PC;
518 predict_taken = branchPred.predict(inst, pred_PC, tid);
520 /* if (predict_taken) {
521 DPRINTF(Fetch, "[tid:%i]: Branch predicted to be taken to %#x.\n",
524 DPRINTF(Fetch, "[tid:%i]: Branch predicted to be not taken.\n", tid);
527 #if ISA_HAS_DELAY_SLOT
532 next_NPC += instSize;
538 next_NPC = next_PC + instSize;
540 /* DPRINTF(Fetch, "[tid:%i]: Branch predicted to go to %#x and then %#x.\n",
541 tid, next_PC, next_NPC);*/
542 inst->setPredTarg(next_PC, next_NPC, next_MicroPC);
543 inst->setPredTaken(predict_taken);
551 return predict_taken;
554 template <class Impl>
556 DefaultFetch<Impl>::fetchCacheLine(Addr fetch_PC, Fault &ret_fault, unsigned tid)
558 Fault fault = NoFault;
562 DPRINTF(Fetch, "[tid:%i] Can't fetch cache line, cache blocked\n",
565 } else if (isSwitchedOut()) {
566 DPRINTF(Fetch, "[tid:%i] Can't fetch cache line, switched out\n",
569 } else if (interruptPending && !(fetch_PC & 0x3)) {
570 // Hold off fetch from getting new instructions when:
571 // Cache is blocked, or
572 // while an interrupt is pending and we're not in PAL mode, or
573 // fetch is switched out.
574 DPRINTF(Fetch, "[tid:%i] Can't fetch cache line, interrupt pending\n",
579 // Align the fetch PC so it's at the start of a cache block.
580 Addr block_PC = icacheBlockAlignPC(fetch_PC);
582 // If we've already got the block, no need to try to fetch it again.
583 if (cacheDataValid[tid] && block_PC == cacheDataPC[tid]) {
587 // Setup the memReq to do a read of the first instruction's address.
588 // Set the appropriate read size and flags as well.
589 // Build request here.
590 RequestPtr mem_req = new Request(tid, block_PC, cacheBlkSize, 0,
591 fetch_PC, cpu->readCpuId(), tid);
593 memReq[tid] = mem_req;
595 // Translate the instruction request.
596 fault = cpu->translateInstReq(mem_req, cpu->thread[tid]);
598 // In the case of faults, the fetch stage may need to stall and wait
599 // for the ITB miss to be handled.
601 // If translation was successful, attempt to read the first
603 if (fault == NoFault) {
605 if (cpu->system->memctrl->badaddr(memReq[tid]->paddr) ||
606 memReq[tid]->isUncacheable()) {
607 DPRINTF(Fetch, "Fetch: Bad address %#x (hopefully on a "
608 "misspeculating path)!",
610 ret_fault = TheISA::genMachineCheckFault();
615 // Build packet here.
616 PacketPtr data_pkt = new Packet(mem_req,
617 MemCmd::ReadReq, Packet::Broadcast);
618 data_pkt->dataDynamicArray(new uint8_t[cacheBlkSize]);
620 cacheDataPC[tid] = block_PC;
621 cacheDataValid[tid] = false;
623 DPRINTF(Fetch, "Fetch: Doing instruction read.\n");
627 // Now do the timing access to see whether or not the instruction
628 // exists within the cache.
629 if (!icachePort->sendTiming(data_pkt)) {
630 if (data_pkt->result == Packet::BadAddress) {
631 fault = TheISA::genMachineCheckFault();
634 warn("Bad address!\n");
636 assert(retryPkt == NULL);
637 assert(retryTid == -1);
638 DPRINTF(Fetch, "[tid:%i] Out of MSHRs!\n", tid);
639 fetchStatus[tid] = IcacheWaitRetry;
646 DPRINTF(Fetch, "[tid:%i]: Doing cache access.\n", tid);
648 lastIcacheStall[tid] = curTick;
650 DPRINTF(Activity, "[tid:%i]: Activity: Waiting on I-cache "
653 fetchStatus[tid] = IcacheWaitResponse;
663 template <class Impl>
665 DefaultFetch<Impl>::doSquash(const Addr &new_PC,
666 const Addr &new_NPC, const Addr &new_microPC, unsigned tid)
668 DPRINTF(Fetch, "[tid:%i]: Squashing, setting PC to: %#x, NPC to: %#x.\n",
669 tid, new_PC, new_NPC);
672 nextPC[tid] = new_NPC;
673 microPC[tid] = new_microPC;
675 // Clear the icache miss if it's outstanding.
676 if (fetchStatus[tid] == IcacheWaitResponse) {
677 DPRINTF(Fetch, "[tid:%i]: Squashing outstanding Icache miss.\n",
682 // Get rid of the retrying packet if it was from this thread.
683 if (retryTid == tid) {
684 assert(cacheBlocked);
686 delete retryPkt->req;
693 fetchStatus[tid] = Squashing;
700 DefaultFetch<Impl>::squashFromDecode(const Addr &new_PC, const Addr &new_NPC,
701 const Addr &new_MicroPC,
702 const InstSeqNum &seq_num, unsigned tid)
704 DPRINTF(Fetch, "[tid:%i]: Squashing from decode.\n",tid);
706 doSquash(new_PC, new_NPC, new_MicroPC, tid);
708 // Tell the CPU to remove any instructions that are in flight between
710 cpu->removeInstsUntil(seq_num, tid);
715 DefaultFetch<Impl>::checkStall(unsigned tid) const
717 bool ret_val = false;
719 if (cpu->contextSwitch) {
720 DPRINTF(Fetch,"[tid:%i]: Stalling for a context switch.\n",tid);
722 } else if (stalls[tid].decode) {
723 DPRINTF(Fetch,"[tid:%i]: Stall from Decode stage detected.\n",tid);
725 } else if (stalls[tid].rename) {
726 DPRINTF(Fetch,"[tid:%i]: Stall from Rename stage detected.\n",tid);
728 } else if (stalls[tid].iew) {
729 DPRINTF(Fetch,"[tid:%i]: Stall from IEW stage detected.\n",tid);
731 } else if (stalls[tid].commit) {
732 DPRINTF(Fetch,"[tid:%i]: Stall from Commit stage detected.\n",tid);
740 typename DefaultFetch<Impl>::FetchStatus
741 DefaultFetch<Impl>::updateFetchStatus()
744 std::list<unsigned>::iterator threads = activeThreads->begin();
745 std::list<unsigned>::iterator end = activeThreads->end();
747 while (threads != end) {
748 unsigned tid = *threads++;
750 if (fetchStatus[tid] == Running ||
751 fetchStatus[tid] == Squashing ||
752 fetchStatus[tid] == IcacheAccessComplete) {
754 if (_status == Inactive) {
755 DPRINTF(Activity, "[tid:%i]: Activating stage.\n",tid);
757 if (fetchStatus[tid] == IcacheAccessComplete) {
758 DPRINTF(Activity, "[tid:%i]: Activating fetch due to cache"
762 cpu->activateStage(O3CPU::FetchIdx);
769 // Stage is switching from active to inactive, notify CPU of it.
770 if (_status == Active) {
771 DPRINTF(Activity, "Deactivating stage.\n");
773 cpu->deactivateStage(O3CPU::FetchIdx);
779 template <class Impl>
781 DefaultFetch<Impl>::squash(const Addr &new_PC, const Addr &new_NPC,
782 const Addr &new_MicroPC,
783 const InstSeqNum &seq_num, unsigned tid)
785 DPRINTF(Fetch, "[tid:%u]: Squash from commit.\n",tid);
787 doSquash(new_PC, new_NPC, new_MicroPC, tid);
789 // Tell the CPU to remove any instructions that are not in the ROB.
790 cpu->removeInstsNotInROB(tid);
793 template <class Impl>
795 DefaultFetch<Impl>::tick()
797 std::list<unsigned>::iterator threads = activeThreads->begin();
798 std::list<unsigned>::iterator end = activeThreads->end();
799 bool status_change = false;
801 wroteToTimeBuffer = false;
803 while (threads != end) {
804 unsigned tid = *threads++;
806 // Check the signals for each thread to determine the proper status
808 bool updated_status = checkSignalsAndUpdate(tid);
809 status_change = status_change || updated_status;
812 DPRINTF(Fetch, "Running stage.\n");
814 // Reset the number of the instruction we're fetching.
818 if (fromCommit->commitInfo[0].interruptPending) {
819 interruptPending = true;
822 if (fromCommit->commitInfo[0].clearInterrupt) {
823 interruptPending = false;
827 for (threadFetched = 0; threadFetched < numFetchingThreads;
829 // Fetch each of the actively fetching threads.
830 fetch(status_change);
833 // Record number of instructions fetched this cycle for distribution.
834 fetchNisnDist.sample(numInst);
837 // Change the fetch stage status if there was a status change.
838 _status = updateFetchStatus();
841 // If there was activity this cycle, inform the CPU of it.
842 if (wroteToTimeBuffer || cpu->contextSwitch) {
843 DPRINTF(Activity, "Activity this cycle.\n");
845 cpu->activityThisCycle();
849 template <class Impl>
851 DefaultFetch<Impl>::checkSignalsAndUpdate(unsigned tid)
853 // Update the per thread stall statuses.
854 if (fromDecode->decodeBlock[tid]) {
855 stalls[tid].decode = true;
858 if (fromDecode->decodeUnblock[tid]) {
859 assert(stalls[tid].decode);
860 assert(!fromDecode->decodeBlock[tid]);
861 stalls[tid].decode = false;
864 if (fromRename->renameBlock[tid]) {
865 stalls[tid].rename = true;
868 if (fromRename->renameUnblock[tid]) {
869 assert(stalls[tid].rename);
870 assert(!fromRename->renameBlock[tid]);
871 stalls[tid].rename = false;
874 if (fromIEW->iewBlock[tid]) {
875 stalls[tid].iew = true;
878 if (fromIEW->iewUnblock[tid]) {
879 assert(stalls[tid].iew);
880 assert(!fromIEW->iewBlock[tid]);
881 stalls[tid].iew = false;
884 if (fromCommit->commitBlock[tid]) {
885 stalls[tid].commit = true;
888 if (fromCommit->commitUnblock[tid]) {
889 assert(stalls[tid].commit);
890 assert(!fromCommit->commitBlock[tid]);
891 stalls[tid].commit = false;
894 // Check squash signals from commit.
895 if (fromCommit->commitInfo[tid].squash) {
897 DPRINTF(Fetch, "[tid:%u]: Squashing instructions due to squash "
898 "from commit.\n",tid);
899 // In any case, squash.
900 squash(fromCommit->commitInfo[tid].nextPC,
901 fromCommit->commitInfo[tid].nextNPC,
902 fromCommit->commitInfo[tid].nextMicroPC,
903 fromCommit->commitInfo[tid].doneSeqNum,
906 // Also check if there's a mispredict that happened.
907 if (fromCommit->commitInfo[tid].branchMispredict) {
908 branchPred.squash(fromCommit->commitInfo[tid].doneSeqNum,
909 fromCommit->commitInfo[tid].nextPC,
910 fromCommit->commitInfo[tid].branchTaken,
913 branchPred.squash(fromCommit->commitInfo[tid].doneSeqNum,
918 } else if (fromCommit->commitInfo[tid].doneSeqNum) {
919 // Update the branch predictor if it wasn't a squashed instruction
920 // that was broadcasted.
921 branchPred.update(fromCommit->commitInfo[tid].doneSeqNum, tid);
924 // Check ROB squash signals from commit.
925 if (fromCommit->commitInfo[tid].robSquashing) {
926 DPRINTF(Fetch, "[tid:%u]: ROB is still squashing.\n", tid);
928 // Continue to squash.
929 fetchStatus[tid] = Squashing;
934 // Check squash signals from decode.
935 if (fromDecode->decodeInfo[tid].squash) {
936 DPRINTF(Fetch, "[tid:%u]: Squashing instructions due to squash "
937 "from decode.\n",tid);
939 // Update the branch predictor.
940 if (fromDecode->decodeInfo[tid].branchMispredict) {
941 branchPred.squash(fromDecode->decodeInfo[tid].doneSeqNum,
942 fromDecode->decodeInfo[tid].nextPC,
943 fromDecode->decodeInfo[tid].branchTaken,
946 branchPred.squash(fromDecode->decodeInfo[tid].doneSeqNum,
950 if (fetchStatus[tid] != Squashing) {
952 DPRINTF(Fetch, "Squashing from decode with PC = %#x, NPC = %#x\n",
953 fromDecode->decodeInfo[tid].nextPC,
954 fromDecode->decodeInfo[tid].nextNPC);
955 // Squash unless we're already squashing
956 squashFromDecode(fromDecode->decodeInfo[tid].nextPC,
957 fromDecode->decodeInfo[tid].nextNPC,
958 fromDecode->decodeInfo[tid].nextMicroPC,
959 fromDecode->decodeInfo[tid].doneSeqNum,
966 if (checkStall(tid) &&
967 fetchStatus[tid] != IcacheWaitResponse &&
968 fetchStatus[tid] != IcacheWaitRetry) {
969 DPRINTF(Fetch, "[tid:%i]: Setting to blocked\n",tid);
971 fetchStatus[tid] = Blocked;
976 if (fetchStatus[tid] == Blocked ||
977 fetchStatus[tid] == Squashing) {
978 // Switch status to running if fetch isn't being told to block or
979 // squash this cycle.
980 DPRINTF(Fetch, "[tid:%i]: Done squashing, switching to running.\n",
983 fetchStatus[tid] = Running;
988 // If we've reached this point, we have not gotten any signals that
989 // cause fetch to change its status. Fetch remains the same as before.
995 DefaultFetch<Impl>::fetch(bool &status_change)
997 //////////////////////////////////////////
998 // Start actual fetch
999 //////////////////////////////////////////
1000 int tid = getFetchingThread(fetchPolicy);
1002 if (tid == -1 || drainPending) {
1003 DPRINTF(Fetch,"There are no more threads available to fetch from.\n");
1005 // Breaks looping condition in tick()
1006 threadFetched = numFetchingThreads;
1010 DPRINTF(Fetch, "Attempting to fetch from [tid:%i]\n", tid);
1013 Addr fetch_PC = PC[tid];
1014 Addr fetch_NPC = nextPC[tid];
1015 Addr fetch_MicroPC = microPC[tid];
1017 // Fault code for memory access.
1018 Fault fault = NoFault;
1020 // If returning from the delay of a cache miss, then update the status
1021 // to running, otherwise do the cache access. Possibly move this up
1022 // to tick() function.
1023 if (fetchStatus[tid] == IcacheAccessComplete) {
1024 DPRINTF(Fetch, "[tid:%i]: Icache miss is complete.\n",
1027 fetchStatus[tid] = Running;
1028 status_change = true;
1029 } else if (fetchStatus[tid] == Running) {
1030 DPRINTF(Fetch, "[tid:%i]: Attempting to translate and read "
1031 "instruction, starting at PC %08p.\n",
1034 bool fetch_success = fetchCacheLine(fetch_PC, fault, tid);
1035 if (!fetch_success) {
1037 ++icacheStallCycles;
1039 ++fetchMiscStallCycles;
1044 if (fetchStatus[tid] == Idle) {
1046 DPRINTF(Fetch, "[tid:%i]: Fetch is idle!\n", tid);
1047 } else if (fetchStatus[tid] == Blocked) {
1048 ++fetchBlockedCycles;
1049 DPRINTF(Fetch, "[tid:%i]: Fetch is blocked!\n", tid);
1050 } else if (fetchStatus[tid] == Squashing) {
1051 ++fetchSquashCycles;
1052 DPRINTF(Fetch, "[tid:%i]: Fetch is squashing!\n", tid);
1053 } else if (fetchStatus[tid] == IcacheWaitResponse) {
1054 ++icacheStallCycles;
1055 DPRINTF(Fetch, "[tid:%i]: Fetch is waiting cache response!\n", tid);
1058 // Status is Idle, Squashing, Blocked, or IcacheWaitResponse, so
1059 // fetch should do nothing.
1065 // If we had a stall due to an icache miss, then return.
1066 if (fetchStatus[tid] == IcacheWaitResponse) {
1067 ++icacheStallCycles;
1068 status_change = true;
1072 Addr next_PC = fetch_PC;
1073 Addr next_NPC = fetch_NPC;
1074 Addr next_MicroPC = fetch_MicroPC;
1076 InstSeqNum inst_seq;
1078 ExtMachInst ext_inst;
1079 // @todo: Fix this hack.
1080 unsigned offset = (fetch_PC & cacheBlkMask) & ~3;
1082 StaticInstPtr staticInst = NULL;
1083 StaticInstPtr macroop = NULL;
1085 if (fault == NoFault) {
1086 // If the read of the first instruction was successful, then grab the
1087 // instructions from the rest of the cache line and put them into the
1088 // queue heading to decode.
1090 DPRINTF(Fetch, "[tid:%i]: Adding instructions to queue to "
1093 // Need to keep track of whether or not a predicted branch
1094 // ended this fetch block.
1095 bool predicted_branch = false;
1097 while (offset < cacheBlkSize &&
1098 numInst < fetchWidth &&
1099 !predicted_branch) {
1101 // If we're branching after this instruction, quite fetching
1102 // from the same block then.
1104 (fetch_PC + sizeof(TheISA::MachInst) != fetch_NPC);
1105 if (predicted_branch) {
1106 DPRINTF(Fetch, "Branch detected with PC = %#x, NPC = %#x\n",
1107 fetch_PC, fetch_NPC);
1110 // Make sure this is a valid index.
1111 assert(offset <= cacheBlkSize - instSize);
1114 // Get the instruction from the array of the cache line.
1115 inst = TheISA::gtoh(*reinterpret_cast<TheISA::MachInst *>
1116 (&cacheData[tid][offset]));
1118 predecoder.setTC(cpu->thread[tid]->getTC());
1119 predecoder.moreBytes(fetch_PC, 0, inst);
1121 ext_inst = predecoder.getExtMachInst();
1122 staticInst = StaticInstPtr(ext_inst);
1123 if (staticInst->isMacroOp())
1124 macroop = staticInst;
1128 staticInst = macroop->fetchMicroOp(fetch_MicroPC);
1129 if (staticInst->isLastMicroOp())
1133 // Get a sequence number.
1134 inst_seq = cpu->getAndIncrementInstSeq();
1136 // Create a new DynInst from the instruction fetched.
1137 DynInstPtr instruction = new DynInst(staticInst,
1138 fetch_PC, fetch_NPC, fetch_MicroPC,
1139 next_PC, next_NPC, next_MicroPC,
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]: MachInst is %#x\n", tid, ext_inst);
1153 DPRINTF(Fetch, "[tid:%i]: Instruction is: %s\n",
1154 tid, instruction->staticInst->disassemble(fetch_PC));
1156 instruction->traceData =
1157 Trace::getInstRecord(curTick, cpu->tcBase(tid),
1158 instruction->staticInst,
1159 instruction->readPC());
1161 ///FIXME This needs to be more robust in dealing with delay slots
1163 lookupAndUpdateNextPC(instruction, next_PC, next_NPC, next_MicroPC);
1165 // Add instruction to the CPU's list of instructions.
1166 instruction->setInstListIt(cpu->addInst(instruction));
1168 // Write the instruction to the first slot in the queue
1169 // that heads to decode.
1170 toDecode->insts[numInst] = instruction;
1174 // Increment stat of fetched instructions.
1177 // Move to the next instruction, unless we have a branch.
1179 fetch_NPC = next_NPC;
1180 fetch_MicroPC = next_MicroPC;
1182 if (instruction->isQuiesce()) {
1183 DPRINTF(Fetch, "Quiesce instruction encountered, halting fetch!",
1185 fetchStatus[tid] = QuiescePending;
1187 status_change = true;
1192 } while (staticInst->isMicroOp() &&
1193 !staticInst->isLastMicroOp() &&
1194 numInst < fetchWidth);
1198 if (predicted_branch) {
1199 DPRINTF(Fetch, "[tid:%i]: Done fetching, predicted branch "
1200 "instruction encountered.\n", tid);
1201 } else if (numInst >= fetchWidth) {
1202 DPRINTF(Fetch, "[tid:%i]: Done fetching, reached fetch bandwidth "
1203 "for this cycle.\n", tid);
1204 } else if (offset >= cacheBlkSize) {
1205 DPRINTF(Fetch, "[tid:%i]: Done fetching, reached the end of cache "
1211 wroteToTimeBuffer = true;
1214 // Now that fetching is completed, update the PC to signify what the next
1216 if (fault == NoFault) {
1218 nextPC[tid] = next_NPC;
1219 microPC[tid] = next_MicroPC;
1220 DPRINTF(Fetch, "[tid:%i]: Setting PC to %08p.\n", tid, next_PC);
1222 // We shouldn't be in an icache miss and also have a fault (an ITB
1224 if (fetchStatus[tid] == IcacheWaitResponse) {
1225 panic("Fetch should have exited prior to this!");
1228 // Send the fault to commit. This thread will not do anything
1229 // until commit handles the fault. The only other way it can
1230 // wake up is if a squash comes along and changes the PC.
1232 assert(numInst < fetchWidth);
1233 // Get a sequence number.
1234 inst_seq = cpu->getAndIncrementInstSeq();
1235 // We will use a nop in order to carry the fault.
1236 ext_inst = TheISA::NoopMachInst;
1238 StaticInstPtr staticInst = new StaticInst(ext_inst);
1239 // Create a new DynInst from the dummy nop.
1240 DynInstPtr instruction = new DynInst(staticInst,
1241 fetch_PC, fetch_NPC,
1244 instruction->setPredTarg(next_PC, next_NPC);
1245 instruction->setTid(tid);
1247 instruction->setASID(tid);
1249 instruction->setThreadState(cpu->thread[tid]);
1251 instruction->traceData = NULL;
1253 instruction->setInstListIt(cpu->addInst(instruction));
1255 instruction->fault = fault;
1257 toDecode->insts[numInst] = instruction;
1260 DPRINTF(Fetch, "[tid:%i]: Blocked, need to handle the trap.\n",tid);
1262 fetchStatus[tid] = TrapPending;
1263 status_change = true;
1264 #else // !FULL_SYSTEM
1265 fetchStatus[tid] = TrapPending;
1266 status_change = true;
1268 #endif // FULL_SYSTEM
1269 DPRINTF(Fetch, "[tid:%i]: fault (%s) detected @ PC %08p",
1270 tid, fault->name(), PC[tid]);
1274 template<class Impl>
1276 DefaultFetch<Impl>::recvRetry()
1278 if (retryPkt != NULL) {
1279 assert(cacheBlocked);
1280 assert(retryTid != -1);
1281 assert(fetchStatus[retryTid] == IcacheWaitRetry);
1283 if (icachePort->sendTiming(retryPkt)) {
1284 fetchStatus[retryTid] = IcacheWaitResponse;
1287 cacheBlocked = false;
1290 assert(retryTid == -1);
1291 // Access has been squashed since it was sent out. Just clear
1292 // the cache being blocked.
1293 cacheBlocked = false;
1297 ///////////////////////////////////////
1299 // SMT FETCH POLICY MAINTAINED HERE //
1301 ///////////////////////////////////////
1302 template<class Impl>
1304 DefaultFetch<Impl>::getFetchingThread(FetchPriority &fetch_priority)
1306 if (numThreads > 1) {
1307 switch (fetch_priority) {
1313 return roundRobin();
1322 return branchCount();
1328 std::list<unsigned>::iterator thread = activeThreads->begin();
1329 assert(thread != activeThreads->end());
1332 if (fetchStatus[tid] == Running ||
1333 fetchStatus[tid] == IcacheAccessComplete ||
1334 fetchStatus[tid] == Idle) {
1344 template<class Impl>
1346 DefaultFetch<Impl>::roundRobin()
1348 std::list<unsigned>::iterator pri_iter = priorityList.begin();
1349 std::list<unsigned>::iterator end = priorityList.end();
1353 while (pri_iter != end) {
1354 high_pri = *pri_iter;
1356 assert(high_pri <= numThreads);
1358 if (fetchStatus[high_pri] == Running ||
1359 fetchStatus[high_pri] == IcacheAccessComplete ||
1360 fetchStatus[high_pri] == Idle) {
1362 priorityList.erase(pri_iter);
1363 priorityList.push_back(high_pri);
1374 template<class Impl>
1376 DefaultFetch<Impl>::iqCount()
1378 std::priority_queue<unsigned> PQ;
1380 std::list<unsigned>::iterator threads = activeThreads->begin();
1381 std::list<unsigned>::iterator end = activeThreads->end();
1383 while (threads != end) {
1384 unsigned tid = *threads++;
1386 PQ.push(fromIEW->iewInfo[tid].iqCount);
1389 while (!PQ.empty()) {
1391 unsigned high_pri = PQ.top();
1393 if (fetchStatus[high_pri] == Running ||
1394 fetchStatus[high_pri] == IcacheAccessComplete ||
1395 fetchStatus[high_pri] == Idle)
1405 template<class Impl>
1407 DefaultFetch<Impl>::lsqCount()
1409 std::priority_queue<unsigned> PQ;
1411 std::list<unsigned>::iterator threads = activeThreads->begin();
1412 std::list<unsigned>::iterator end = activeThreads->end();
1414 while (threads != end) {
1415 unsigned tid = *threads++;
1417 PQ.push(fromIEW->iewInfo[tid].ldstqCount);
1420 while (!PQ.empty()) {
1422 unsigned high_pri = PQ.top();
1424 if (fetchStatus[high_pri] == Running ||
1425 fetchStatus[high_pri] == IcacheAccessComplete ||
1426 fetchStatus[high_pri] == Idle)
1436 template<class Impl>
1438 DefaultFetch<Impl>::branchCount()
1440 std::list<unsigned>::iterator thread = activeThreads->begin();
1441 assert(thread != activeThreads->end());
1442 unsigned tid = *thread;
1444 panic("Branch Count Fetch policy unimplemented\n");