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 "base/remote_gdb.hh"
49 #include "sim/system.hh"
55 using namespace TheISA;
59 DefaultFetch<Impl>::IcachePort::recvAtomic(PacketPtr pkt)
61 panic("DefaultFetch doesn't expect recvAtomic callback!");
67 DefaultFetch<Impl>::IcachePort::recvFunctional(PacketPtr pkt)
69 panic("DefaultFetch doesn't expect recvFunctional callback!");
74 DefaultFetch<Impl>::IcachePort::recvStatusChange(Status status)
76 if (status == RangeChange)
79 panic("DefaultFetch doesn't expect recvStatusChange callback!");
84 DefaultFetch<Impl>::IcachePort::recvTiming(Packet *pkt)
86 fetch->processCacheCompletion(pkt);
92 DefaultFetch<Impl>::IcachePort::recvRetry()
98 DefaultFetch<Impl>::DefaultFetch(Params *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 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];
166 stalls[tid].decode = 0;
167 stalls[tid].rename = 0;
169 stalls[tid].commit = 0;
172 // Get the size of an instruction.
173 instSize = sizeof(MachInst);
176 template <class Impl>
178 DefaultFetch<Impl>::name() const
180 return cpu->name() + ".fetch";
183 template <class Impl>
185 DefaultFetch<Impl>::regStats()
188 .name(name() + ".icacheStallCycles")
189 .desc("Number of cycles fetch is stalled on an Icache miss")
190 .prereq(icacheStallCycles);
193 .name(name() + ".Insts")
194 .desc("Number of instructions fetch has processed")
195 .prereq(fetchedInsts);
198 .name(name() + ".Branches")
199 .desc("Number of branches that fetch encountered")
200 .prereq(fetchedBranches);
203 .name(name() + ".predictedBranches")
204 .desc("Number of branches that fetch has predicted taken")
205 .prereq(predictedBranches);
208 .name(name() + ".Cycles")
209 .desc("Number of cycles fetch has run and was not squashing or"
211 .prereq(fetchCycles);
214 .name(name() + ".SquashCycles")
215 .desc("Number of cycles fetch has spent squashing")
216 .prereq(fetchSquashCycles);
219 .name(name() + ".IdleCycles")
220 .desc("Number of cycles fetch was idle")
221 .prereq(fetchIdleCycles);
224 .name(name() + ".BlockedCycles")
225 .desc("Number of cycles fetch has spent blocked")
226 .prereq(fetchBlockedCycles);
229 .name(name() + ".CacheLines")
230 .desc("Number of cache lines fetched")
231 .prereq(fetchedCacheLines);
234 .name(name() + ".MiscStallCycles")
235 .desc("Number of cycles fetch has spent waiting on interrupts, or "
236 "bad addresses, or out of MSHRs")
237 .prereq(fetchMiscStallCycles);
240 .name(name() + ".IcacheSquashes")
241 .desc("Number of outstanding Icache misses that were squashed")
242 .prereq(fetchIcacheSquashes);
245 .init(/* base value */ 0,
246 /* last value */ fetchWidth,
248 .name(name() + ".rateDist")
249 .desc("Number of instructions fetched each cycle (Total)")
253 .name(name() + ".idleRate")
254 .desc("Percent of cycles fetch was idle")
256 idleRate = fetchIdleCycles * 100 / cpu->numCycles;
259 .name(name() + ".branchRate")
260 .desc("Number of branch fetches per cycle")
261 .flags(Stats::total);
262 branchRate = fetchedBranches / cpu->numCycles;
265 .name(name() + ".rate")
266 .desc("Number of inst fetches per cycle")
267 .flags(Stats::total);
268 fetchRate = fetchedInsts / cpu->numCycles;
270 branchPred.regStats();
275 DefaultFetch<Impl>::setCPU(O3CPU *cpu_ptr)
277 DPRINTF(Fetch, "Setting the CPU pointer.\n");
280 // Name is finally available, so create the port.
281 icachePort = new IcachePort(this);
285 cpu->checker->setIcachePort(icachePort);
289 // Fetch needs to start fetching instructions at the very beginning,
290 // so it must start up in active state.
296 DefaultFetch<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *time_buffer)
298 DPRINTF(Fetch, "Setting the time buffer pointer.\n");
299 timeBuffer = time_buffer;
301 // Create wires to get information from proper places in time buffer.
302 fromDecode = timeBuffer->getWire(-decodeToFetchDelay);
303 fromRename = timeBuffer->getWire(-renameToFetchDelay);
304 fromIEW = timeBuffer->getWire(-iewToFetchDelay);
305 fromCommit = timeBuffer->getWire(-commitToFetchDelay);
310 DefaultFetch<Impl>::setActiveThreads(list<unsigned> *at_ptr)
312 DPRINTF(Fetch, "Setting active threads list pointer.\n");
313 activeThreads = at_ptr;
318 DefaultFetch<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
320 DPRINTF(Fetch, "Setting the fetch queue pointer.\n");
323 // Create wire to write information to proper place in fetch queue.
324 toDecode = fetchQueue->getWire(0);
329 DefaultFetch<Impl>::initStage()
331 // Setup PC and nextPC with initial state.
332 for (int tid = 0; tid < numThreads; tid++) {
333 PC[tid] = cpu->readPC(tid);
334 nextPC[tid] = cpu->readNextPC(tid);
335 #if THE_ISA != ALPHA_ISA
336 nextNPC[tid] = cpu->readNextNPC(tid);
343 DefaultFetch<Impl>::processCacheCompletion(PacketPtr pkt)
345 unsigned tid = pkt->req->getThreadNum();
347 DPRINTF(Fetch, "[tid:%u] Waking up from cache miss.\n",tid);
349 // Only change the status if it's still waiting on the icache access
351 if (fetchStatus[tid] != IcacheWaitResponse ||
352 pkt->req != memReq[tid] ||
354 ++fetchIcacheSquashes;
360 memcpy(cacheData[tid], pkt->getPtr<uint8_t *>(), cacheBlkSize);
363 // Wake up the CPU (if it went to sleep and was waiting on
364 // this completion event).
367 DPRINTF(Activity, "[tid:%u] Activating fetch due to cache completion\n",
373 // Only switch to IcacheAccessComplete if we're not stalled as well.
374 if (checkStall(tid)) {
375 fetchStatus[tid] = Blocked;
377 fetchStatus[tid] = IcacheAccessComplete;
380 // Reset the mem req to NULL.
386 template <class Impl>
388 DefaultFetch<Impl>::drain()
390 // Fetch is ready to drain at any time.
391 cpu->signalDrained();
396 template <class Impl>
398 DefaultFetch<Impl>::resume()
400 drainPending = false;
403 template <class Impl>
405 DefaultFetch<Impl>::switchOut()
408 // Branch predictor needs to have its state cleared.
409 branchPred.switchOut();
412 template <class Impl>
414 DefaultFetch<Impl>::takeOverFrom()
417 for (int i = 0; i < Impl::MaxThreads; ++i) {
418 stalls[i].decode = 0;
419 stalls[i].rename = 0;
421 stalls[i].commit = 0;
422 PC[i] = cpu->readPC(i);
423 nextPC[i] = cpu->readNextPC(i);
424 #if THE_ISA != ALPHA_ISA
425 nextNPC[i] = cpu->readNextNPC(i);
427 fetchStatus[i] = Running;
430 wroteToTimeBuffer = false;
433 branchPred.takeOverFrom();
436 template <class Impl>
438 DefaultFetch<Impl>::wakeFromQuiesce()
440 DPRINTF(Fetch, "Waking up from quiesce\n");
441 // Hopefully this is safe
442 // @todo: Allow other threads to wake from quiesce.
443 fetchStatus[0] = Running;
446 template <class Impl>
448 DefaultFetch<Impl>::switchToActive()
450 if (_status == Inactive) {
451 DPRINTF(Activity, "Activating stage.\n");
453 cpu->activateStage(O3CPU::FetchIdx);
459 template <class Impl>
461 DefaultFetch<Impl>::switchToInactive()
463 if (_status == Active) {
464 DPRINTF(Activity, "Deactivating stage.\n");
466 cpu->deactivateStage(O3CPU::FetchIdx);
472 template <class Impl>
474 DefaultFetch<Impl>::lookupAndUpdateNextPC(DynInstPtr &inst, Addr &next_PC)
476 // Do branch prediction check here.
477 // A bit of a misnomer...next_PC is actually the current PC until
478 // this function updates it.
481 if (!inst->isControl()) {
482 next_PC = next_PC + instSize;
483 inst->setPredTarg(next_PC);
487 predict_taken = branchPred.predict(inst, next_PC, inst->threadNumber);
495 return predict_taken;
498 template <class Impl>
500 DefaultFetch<Impl>::fetchCacheLine(Addr fetch_PC, Fault &ret_fault, unsigned tid)
502 Fault fault = NoFault;
505 // Flag to say whether or not address is physical addr.
506 unsigned flags = cpu->inPalMode(fetch_PC) ? PHYSICAL : 0;
509 #endif // FULL_SYSTEM
511 if (cacheBlocked || (interruptPending && flags == 0)) {
512 // Hold off fetch from getting new instructions when:
513 // Cache is blocked, or
514 // while an interrupt is pending and we're not in PAL mode, or
515 // fetch is switched out.
519 // Align the fetch PC so it's at the start of a cache block.
520 fetch_PC = icacheBlockAlignPC(fetch_PC);
522 // If we've already got the block, no need to try to fetch it again.
523 if (fetch_PC == cacheDataPC[tid]) {
527 // Setup the memReq to do a read of the first instruction's address.
528 // Set the appropriate read size and flags as well.
529 // Build request here.
530 RequestPtr mem_req = new Request(tid, fetch_PC, cacheBlkSize, flags,
531 fetch_PC, cpu->readCpuId(), tid);
533 memReq[tid] = mem_req;
535 // Translate the instruction request.
536 fault = cpu->translateInstReq(mem_req, cpu->thread[tid]);
538 // In the case of faults, the fetch stage may need to stall and wait
539 // for the ITB miss to be handled.
541 // If translation was successful, attempt to read the first
543 if (fault == NoFault) {
545 if (cpu->system->memctrl->badaddr(memReq[tid]->paddr) ||
546 memReq[tid]->flags & UNCACHEABLE) {
547 DPRINTF(Fetch, "Fetch: Bad address %#x (hopefully on a "
548 "misspeculating path)!",
550 ret_fault = TheISA::genMachineCheckFault();
555 // Build packet here.
556 PacketPtr data_pkt = new Packet(mem_req,
557 Packet::ReadReq, Packet::Broadcast);
558 data_pkt->dataDynamic(new uint8_t[cacheBlkSize]);
560 cacheDataPC[tid] = fetch_PC;
562 DPRINTF(Fetch, "Fetch: Doing instruction read.\n");
566 // Now do the timing access to see whether or not the instruction
567 // exists within the cache.
568 if (!icachePort->sendTiming(data_pkt)) {
569 assert(retryPkt == NULL);
570 assert(retryTid == -1);
571 DPRINTF(Fetch, "[tid:%i] Out of MSHRs!\n", tid);
572 fetchStatus[tid] = IcacheWaitRetry;
579 DPRINTF(Fetch, "[tid:%i]: Doing cache access.\n", tid);
581 lastIcacheStall[tid] = curTick;
583 DPRINTF(Activity, "[tid:%i]: Activity: Waiting on I-cache "
586 fetchStatus[tid] = IcacheWaitResponse;
596 template <class Impl>
598 DefaultFetch<Impl>::doSquash(const Addr &new_PC, unsigned tid)
600 DPRINTF(Fetch, "[tid:%i]: Squashing, setting PC to: %#x.\n",
604 nextPC[tid] = new_PC + instSize;
606 // Clear the icache miss if it's outstanding.
607 if (fetchStatus[tid] == IcacheWaitResponse) {
608 DPRINTF(Fetch, "[tid:%i]: Squashing outstanding Icache miss.\n",
613 // Get rid of the retrying packet if it was from this thread.
614 if (retryTid == tid) {
615 assert(cacheBlocked);
616 cacheBlocked = false;
619 delete retryPkt->req;
623 fetchStatus[tid] = Squashing;
630 DefaultFetch<Impl>::squashFromDecode(const Addr &new_PC,
631 const InstSeqNum &seq_num,
634 DPRINTF(Fetch, "[tid:%i]: Squashing from decode.\n",tid);
636 doSquash(new_PC, tid);
638 // Tell the CPU to remove any instructions that are in flight between
640 cpu->removeInstsUntil(seq_num, tid);
645 DefaultFetch<Impl>::checkStall(unsigned tid) const
647 bool ret_val = false;
649 if (cpu->contextSwitch) {
650 DPRINTF(Fetch,"[tid:%i]: Stalling for a context switch.\n",tid);
652 } else if (stalls[tid].decode) {
653 DPRINTF(Fetch,"[tid:%i]: Stall from Decode stage detected.\n",tid);
655 } else if (stalls[tid].rename) {
656 DPRINTF(Fetch,"[tid:%i]: Stall from Rename stage detected.\n",tid);
658 } else if (stalls[tid].iew) {
659 DPRINTF(Fetch,"[tid:%i]: Stall from IEW stage detected.\n",tid);
661 } else if (stalls[tid].commit) {
662 DPRINTF(Fetch,"[tid:%i]: Stall from Commit stage detected.\n",tid);
670 typename DefaultFetch<Impl>::FetchStatus
671 DefaultFetch<Impl>::updateFetchStatus()
674 list<unsigned>::iterator threads = (*activeThreads).begin();
676 while (threads != (*activeThreads).end()) {
678 unsigned tid = *threads++;
680 if (fetchStatus[tid] == Running ||
681 fetchStatus[tid] == Squashing ||
682 fetchStatus[tid] == IcacheAccessComplete) {
684 if (_status == Inactive) {
685 DPRINTF(Activity, "[tid:%i]: Activating stage.\n",tid);
687 if (fetchStatus[tid] == IcacheAccessComplete) {
688 DPRINTF(Activity, "[tid:%i]: Activating fetch due to cache"
692 cpu->activateStage(O3CPU::FetchIdx);
699 // Stage is switching from active to inactive, notify CPU of it.
700 if (_status == Active) {
701 DPRINTF(Activity, "Deactivating stage.\n");
703 cpu->deactivateStage(O3CPU::FetchIdx);
709 template <class Impl>
711 DefaultFetch<Impl>::squash(const Addr &new_PC, unsigned tid)
713 DPRINTF(Fetch, "[tid:%u]: Squash from commit.\n",tid);
715 doSquash(new_PC, tid);
717 // Tell the CPU to remove any instructions that are not in the ROB.
718 cpu->removeInstsNotInROB(tid);
721 template <class Impl>
723 DefaultFetch<Impl>::tick()
725 list<unsigned>::iterator threads = (*activeThreads).begin();
726 bool status_change = false;
728 wroteToTimeBuffer = false;
730 while (threads != (*activeThreads).end()) {
731 unsigned tid = *threads++;
733 // Check the signals for each thread to determine the proper status
735 bool updated_status = checkSignalsAndUpdate(tid);
736 status_change = status_change || updated_status;
739 DPRINTF(Fetch, "Running stage.\n");
741 // Reset the number of the instruction we're fetching.
745 if (fromCommit->commitInfo[0].interruptPending) {
746 interruptPending = true;
749 if (fromCommit->commitInfo[0].clearInterrupt) {
750 interruptPending = false;
754 for (threadFetched = 0; threadFetched < numFetchingThreads;
756 // Fetch each of the actively fetching threads.
757 fetch(status_change);
760 // Record number of instructions fetched this cycle for distribution.
761 fetchNisnDist.sample(numInst);
764 // Change the fetch stage status if there was a status change.
765 _status = updateFetchStatus();
768 // If there was activity this cycle, inform the CPU of it.
769 if (wroteToTimeBuffer || cpu->contextSwitch) {
770 DPRINTF(Activity, "Activity this cycle.\n");
772 cpu->activityThisCycle();
776 template <class Impl>
778 DefaultFetch<Impl>::checkSignalsAndUpdate(unsigned tid)
780 // Update the per thread stall statuses.
781 if (fromDecode->decodeBlock[tid]) {
782 stalls[tid].decode = true;
785 if (fromDecode->decodeUnblock[tid]) {
786 assert(stalls[tid].decode);
787 assert(!fromDecode->decodeBlock[tid]);
788 stalls[tid].decode = false;
791 if (fromRename->renameBlock[tid]) {
792 stalls[tid].rename = true;
795 if (fromRename->renameUnblock[tid]) {
796 assert(stalls[tid].rename);
797 assert(!fromRename->renameBlock[tid]);
798 stalls[tid].rename = false;
801 if (fromIEW->iewBlock[tid]) {
802 stalls[tid].iew = true;
805 if (fromIEW->iewUnblock[tid]) {
806 assert(stalls[tid].iew);
807 assert(!fromIEW->iewBlock[tid]);
808 stalls[tid].iew = false;
811 if (fromCommit->commitBlock[tid]) {
812 stalls[tid].commit = true;
815 if (fromCommit->commitUnblock[tid]) {
816 assert(stalls[tid].commit);
817 assert(!fromCommit->commitBlock[tid]);
818 stalls[tid].commit = false;
821 // Check squash signals from commit.
822 if (fromCommit->commitInfo[tid].squash) {
824 DPRINTF(Fetch, "[tid:%u]: Squashing instructions due to squash "
825 "from commit.\n",tid);
827 // In any case, squash.
828 squash(fromCommit->commitInfo[tid].nextPC,tid);
830 // Also check if there's a mispredict that happened.
831 if (fromCommit->commitInfo[tid].branchMispredict) {
832 branchPred.squash(fromCommit->commitInfo[tid].doneSeqNum,
833 fromCommit->commitInfo[tid].nextPC,
834 fromCommit->commitInfo[tid].branchTaken,
837 branchPred.squash(fromCommit->commitInfo[tid].doneSeqNum,
842 } else if (fromCommit->commitInfo[tid].doneSeqNum) {
843 // Update the branch predictor if it wasn't a squashed instruction
844 // that was broadcasted.
845 branchPred.update(fromCommit->commitInfo[tid].doneSeqNum, tid);
848 // Check ROB squash signals from commit.
849 if (fromCommit->commitInfo[tid].robSquashing) {
850 DPRINTF(Fetch, "[tid:%u]: ROB is still squashing.\n", tid);
852 // Continue to squash.
853 fetchStatus[tid] = Squashing;
858 // Check squash signals from decode.
859 if (fromDecode->decodeInfo[tid].squash) {
860 DPRINTF(Fetch, "[tid:%u]: Squashing instructions due to squash "
861 "from decode.\n",tid);
863 // Update the branch predictor.
864 if (fromDecode->decodeInfo[tid].branchMispredict) {
865 branchPred.squash(fromDecode->decodeInfo[tid].doneSeqNum,
866 fromDecode->decodeInfo[tid].nextPC,
867 fromDecode->decodeInfo[tid].branchTaken,
870 branchPred.squash(fromDecode->decodeInfo[tid].doneSeqNum,
874 if (fetchStatus[tid] != Squashing) {
875 // Squash unless we're already squashing
876 squashFromDecode(fromDecode->decodeInfo[tid].nextPC,
877 fromDecode->decodeInfo[tid].doneSeqNum,
884 if (checkStall(tid) && fetchStatus[tid] != IcacheWaitResponse) {
885 DPRINTF(Fetch, "[tid:%i]: Setting to blocked\n",tid);
887 fetchStatus[tid] = Blocked;
892 if (fetchStatus[tid] == Blocked ||
893 fetchStatus[tid] == Squashing) {
894 // Switch status to running if fetch isn't being told to block or
895 // squash this cycle.
896 DPRINTF(Fetch, "[tid:%i]: Done squashing, switching to running.\n",
899 fetchStatus[tid] = Running;
904 // If we've reached this point, we have not gotten any signals that
905 // cause fetch to change its status. Fetch remains the same as before.
911 DefaultFetch<Impl>::fetch(bool &status_change)
913 //////////////////////////////////////////
914 // Start actual fetch
915 //////////////////////////////////////////
916 int tid = getFetchingThread(fetchPolicy);
918 if (tid == -1 || drainPending) {
919 DPRINTF(Fetch,"There are no more threads available to fetch from.\n");
921 // Breaks looping condition in tick()
922 threadFetched = numFetchingThreads;
926 DPRINTF(Fetch, "Attempting to fetch from [tid:%i]\n", tid);
929 Addr &fetch_PC = PC[tid];
931 // Fault code for memory access.
932 Fault fault = NoFault;
934 // If returning from the delay of a cache miss, then update the status
935 // to running, otherwise do the cache access. Possibly move this up
936 // to tick() function.
937 if (fetchStatus[tid] == IcacheAccessComplete) {
938 DPRINTF(Fetch, "[tid:%i]: Icache miss is complete.\n",
941 fetchStatus[tid] = Running;
942 status_change = true;
943 } else if (fetchStatus[tid] == Running) {
944 DPRINTF(Fetch, "[tid:%i]: Attempting to translate and read "
945 "instruction, starting at PC %08p.\n",
948 bool fetch_success = fetchCacheLine(fetch_PC, fault, tid);
949 if (!fetch_success) {
953 ++fetchMiscStallCycles;
958 if (fetchStatus[tid] == Idle) {
960 } else if (fetchStatus[tid] == Blocked) {
961 ++fetchBlockedCycles;
962 } else if (fetchStatus[tid] == Squashing) {
964 } else if (fetchStatus[tid] == IcacheWaitResponse) {
968 // Status is Idle, Squashing, Blocked, or IcacheWaitResponse, so
969 // fetch should do nothing.
975 // If we had a stall due to an icache miss, then return.
976 if (fetchStatus[tid] == IcacheWaitResponse) {
978 status_change = true;
982 Addr next_PC = fetch_PC;
985 ExtMachInst ext_inst;
986 // @todo: Fix this hack.
987 unsigned offset = (fetch_PC & cacheBlkMask) & ~3;
989 if (fault == NoFault) {
990 // If the read of the first instruction was successful, then grab the
991 // instructions from the rest of the cache line and put them into the
992 // queue heading to decode.
994 DPRINTF(Fetch, "[tid:%i]: Adding instructions to queue to "
997 // Need to keep track of whether or not a predicted branch
998 // ended this fetch block.
999 bool predicted_branch = false;
1002 offset < cacheBlkSize &&
1003 numInst < fetchWidth &&
1007 // Get a sequence number.
1008 inst_seq = cpu->getAndIncrementInstSeq();
1010 // Make sure this is a valid index.
1011 assert(offset <= cacheBlkSize - instSize);
1013 // Get the instruction from the array of the cache line.
1014 inst = gtoh(*reinterpret_cast<MachInst *>
1015 (&cacheData[tid][offset]));
1017 ext_inst = TheISA::makeExtMI(inst, fetch_PC);
1019 // Create a new DynInst from the instruction fetched.
1020 DynInstPtr instruction = new DynInst(ext_inst, fetch_PC,
1023 instruction->setTid(tid);
1025 instruction->setASID(tid);
1027 instruction->setThreadState(cpu->thread[tid]);
1029 DPRINTF(Fetch, "[tid:%i]: Instruction PC %#x created "
1031 tid, instruction->readPC(), inst_seq);
1033 DPRINTF(Fetch, "[tid:%i]: Instruction is: %s\n",
1034 tid, instruction->staticInst->disassemble(fetch_PC));
1036 instruction->traceData =
1037 Trace::getInstRecord(curTick, cpu->tcBase(tid), cpu,
1038 instruction->staticInst,
1039 instruction->readPC(),tid);
1041 predicted_branch = lookupAndUpdateNextPC(instruction, next_PC);
1043 // Add instruction to the CPU's list of instructions.
1044 instruction->setInstListIt(cpu->addInst(instruction));
1046 // Write the instruction to the first slot in the queue
1047 // that heads to decode.
1048 toDecode->insts[numInst] = instruction;
1052 // Increment stat of fetched instructions.
1055 // Move to the next instruction, unless we have a branch.
1058 if (instruction->isQuiesce()) {
1059 warn("cycle %lli: Quiesce instruction encountered, halting fetch!",
1061 fetchStatus[tid] = QuiescePending;
1063 status_change = true;
1072 wroteToTimeBuffer = true;
1075 // Now that fetching is completed, update the PC to signify what the next
1077 if (fault == NoFault) {
1078 DPRINTF(Fetch, "[tid:%i]: Setting PC to %08p.\n",tid, next_PC);
1080 #if THE_ISA == ALPHA_ISA
1082 nextPC[tid] = next_PC + instSize;
1085 nextPC[tid] = next_PC + instSize;
1086 nextPC[tid] = next_PC + instSize;
1088 thread->setNextPC(thread->readNextNPC());
1089 thread->setNextNPC(thread->readNextNPC() + sizeof(MachInst));
1092 // We shouldn't be in an icache miss and also have a fault (an ITB
1094 if (fetchStatus[tid] == IcacheWaitResponse) {
1095 panic("Fetch should have exited prior to this!");
1098 // Send the fault to commit. This thread will not do anything
1099 // until commit handles the fault. The only other way it can
1100 // wake up is if a squash comes along and changes the PC.
1102 assert(numInst != fetchWidth);
1103 // Get a sequence number.
1104 inst_seq = cpu->getAndIncrementInstSeq();
1105 // We will use a nop in order to carry the fault.
1106 ext_inst = TheISA::NoopMachInst;
1108 // Create a new DynInst from the dummy nop.
1109 DynInstPtr instruction = new DynInst(ext_inst, fetch_PC,
1112 instruction->setPredTarg(next_PC + instSize);
1113 instruction->setTid(tid);
1115 instruction->setASID(tid);
1117 instruction->setThreadState(cpu->thread[tid]);
1119 instruction->traceData = NULL;
1121 instruction->setInstListIt(cpu->addInst(instruction));
1123 instruction->fault = fault;
1125 toDecode->insts[numInst] = instruction;
1128 DPRINTF(Fetch, "[tid:%i]: Blocked, need to handle the trap.\n",tid);
1130 fetchStatus[tid] = TrapPending;
1131 status_change = true;
1133 warn("cycle %lli: fault (%d) detected @ PC %08p", curTick, fault, PC[tid]);
1134 #else // !FULL_SYSTEM
1135 warn("cycle %lli: fault (%d) detected @ PC %08p", curTick, fault, PC[tid]);
1136 #endif // FULL_SYSTEM
1140 template<class Impl>
1142 DefaultFetch<Impl>::recvRetry()
1144 assert(cacheBlocked);
1145 if (retryPkt != NULL) {
1146 assert(retryTid != -1);
1147 assert(fetchStatus[retryTid] == IcacheWaitRetry);
1149 if (icachePort->sendTiming(retryPkt)) {
1150 fetchStatus[retryTid] = IcacheWaitResponse;
1153 cacheBlocked = false;
1156 assert(retryTid == -1);
1157 // Access has been squashed since it was sent out. Just clear
1158 // the cache being blocked.
1159 cacheBlocked = false;
1163 ///////////////////////////////////////
1165 // SMT FETCH POLICY MAINTAINED HERE //
1167 ///////////////////////////////////////
1168 template<class Impl>
1170 DefaultFetch<Impl>::getFetchingThread(FetchPriority &fetch_priority)
1172 if (numThreads > 1) {
1173 switch (fetch_priority) {
1179 return roundRobin();
1188 return branchCount();
1194 int tid = *((*activeThreads).begin());
1196 if (fetchStatus[tid] == Running ||
1197 fetchStatus[tid] == IcacheAccessComplete ||
1198 fetchStatus[tid] == Idle) {
1208 template<class Impl>
1210 DefaultFetch<Impl>::roundRobin()
1212 list<unsigned>::iterator pri_iter = priorityList.begin();
1213 list<unsigned>::iterator end = priorityList.end();
1217 while (pri_iter != end) {
1218 high_pri = *pri_iter;
1220 assert(high_pri <= numThreads);
1222 if (fetchStatus[high_pri] == Running ||
1223 fetchStatus[high_pri] == IcacheAccessComplete ||
1224 fetchStatus[high_pri] == Idle) {
1226 priorityList.erase(pri_iter);
1227 priorityList.push_back(high_pri);
1238 template<class Impl>
1240 DefaultFetch<Impl>::iqCount()
1242 priority_queue<unsigned> PQ;
1244 list<unsigned>::iterator threads = (*activeThreads).begin();
1246 while (threads != (*activeThreads).end()) {
1247 unsigned tid = *threads++;
1249 PQ.push(fromIEW->iewInfo[tid].iqCount);
1252 while (!PQ.empty()) {
1254 unsigned high_pri = PQ.top();
1256 if (fetchStatus[high_pri] == Running ||
1257 fetchStatus[high_pri] == IcacheAccessComplete ||
1258 fetchStatus[high_pri] == Idle)
1268 template<class Impl>
1270 DefaultFetch<Impl>::lsqCount()
1272 priority_queue<unsigned> PQ;
1275 list<unsigned>::iterator threads = (*activeThreads).begin();
1277 while (threads != (*activeThreads).end()) {
1278 unsigned tid = *threads++;
1280 PQ.push(fromIEW->iewInfo[tid].ldstqCount);
1283 while (!PQ.empty()) {
1285 unsigned high_pri = PQ.top();
1287 if (fetchStatus[high_pri] == Running ||
1288 fetchStatus[high_pri] == IcacheAccessComplete ||
1289 fetchStatus[high_pri] == Idle)
1299 template<class Impl>
1301 DefaultFetch<Impl>::branchCount()
1303 list<unsigned>::iterator threads = (*activeThreads).begin();
1304 panic("Branch Count Fetch policy unimplemented\n");