2 * Copyright (c) 2010 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2004-2006 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 #include "arch/isa_traits.hh"
48 #include "arch/utility.hh"
49 #include "base/types.hh"
50 #include "config/the_isa.hh"
51 #include "config/use_checker.hh"
52 #include "cpu/checker/cpu.hh"
53 #include "cpu/exetrace.hh"
54 #include "cpu/o3/fetch.hh"
55 #include "mem/packet.hh"
56 #include "mem/request.hh"
57 #include "params/DerivO3CPU.hh"
58 #include "sim/byteswap.hh"
59 #include "sim/core.hh"
62 #include "arch/tlb.hh"
63 #include "arch/vtophys.hh"
64 #include "sim/system.hh"
71 DefaultFetch<Impl>::IcachePort::setPeer(Port *port)
80 DefaultFetch<Impl>::IcachePort::recvAtomic(PacketPtr pkt)
82 panic("DefaultFetch doesn't expect recvAtomic callback!");
88 DefaultFetch<Impl>::IcachePort::recvFunctional(PacketPtr pkt)
90 DPRINTF(Fetch, "DefaultFetch doesn't update its state from a "
96 DefaultFetch<Impl>::IcachePort::recvStatusChange(Status status)
98 if (status == RangeChange) {
99 if (!snoopRangeSent) {
100 snoopRangeSent = true;
101 sendStatusChange(Port::RangeChange);
106 panic("DefaultFetch doesn't expect recvStatusChange callback!");
111 DefaultFetch<Impl>::IcachePort::recvTiming(PacketPtr pkt)
113 DPRINTF(Fetch, "Received timing\n");
114 if (pkt->isResponse()) {
115 fetch->processCacheCompletion(pkt);
117 //else Snooped a coherence request, just return
123 DefaultFetch<Impl>::IcachePort::recvRetry()
129 DefaultFetch<Impl>::DefaultFetch(O3CPU *_cpu, DerivO3CPUParams *params)
133 decodeToFetchDelay(params->decodeToFetchDelay),
134 renameToFetchDelay(params->renameToFetchDelay),
135 iewToFetchDelay(params->iewToFetchDelay),
136 commitToFetchDelay(params->commitToFetchDelay),
137 fetchWidth(params->fetchWidth),
140 retryTid(InvalidThreadID),
141 numThreads(params->numThreads),
142 numFetchingThreads(params->smtNumFetchingThreads),
143 interruptPending(false),
147 if (numThreads > Impl::MaxThreads)
148 fatal("numThreads (%d) is larger than compiled limit (%d),\n"
149 "\tincrease MaxThreads in src/cpu/o3/impl.hh\n",
150 numThreads, static_cast<int>(Impl::MaxThreads));
152 // Set fetch stage's status to inactive.
155 std::string policy = params->smtFetchPolicy;
157 // Convert string to lowercase
158 std::transform(policy.begin(), policy.end(), policy.begin(),
159 (int(*)(int)) tolower);
161 // Figure out fetch policy
162 if (policy == "singlethread") {
163 fetchPolicy = SingleThread;
165 panic("Invalid Fetch Policy for a SMT workload.");
166 } else if (policy == "roundrobin") {
167 fetchPolicy = RoundRobin;
168 DPRINTF(Fetch, "Fetch policy set to Round Robin\n");
169 } else if (policy == "branch") {
170 fetchPolicy = Branch;
171 DPRINTF(Fetch, "Fetch policy set to Branch Count\n");
172 } else if (policy == "iqcount") {
174 DPRINTF(Fetch, "Fetch policy set to IQ count\n");
175 } else if (policy == "lsqcount") {
177 DPRINTF(Fetch, "Fetch policy set to LSQ count\n");
179 fatal("Invalid Fetch Policy. Options Are: {SingleThread,"
180 " RoundRobin,LSQcount,IQcount}\n");
183 // Get the size of an instruction.
184 instSize = sizeof(TheISA::MachInst);
186 // Name is finally available, so create the port.
187 icachePort = new IcachePort(this);
189 icachePort->snoopRangeSent = false;
193 cpu->checker->setIcachePort(icachePort);
198 template <class Impl>
200 DefaultFetch<Impl>::name() const
202 return cpu->name() + ".fetch";
205 template <class Impl>
207 DefaultFetch<Impl>::regStats()
210 .name(name() + ".icacheStallCycles")
211 .desc("Number of cycles fetch is stalled on an Icache miss")
212 .prereq(icacheStallCycles);
215 .name(name() + ".Insts")
216 .desc("Number of instructions fetch has processed")
217 .prereq(fetchedInsts);
220 .name(name() + ".Branches")
221 .desc("Number of branches that fetch encountered")
222 .prereq(fetchedBranches);
225 .name(name() + ".predictedBranches")
226 .desc("Number of branches that fetch has predicted taken")
227 .prereq(predictedBranches);
230 .name(name() + ".Cycles")
231 .desc("Number of cycles fetch has run and was not squashing or"
233 .prereq(fetchCycles);
236 .name(name() + ".SquashCycles")
237 .desc("Number of cycles fetch has spent squashing")
238 .prereq(fetchSquashCycles);
241 .name(name() + ".TlbCycles")
242 .desc("Number of cycles fetch has spent waiting for tlb")
243 .prereq(fetchTlbCycles);
246 .name(name() + ".IdleCycles")
247 .desc("Number of cycles fetch was idle")
248 .prereq(fetchIdleCycles);
251 .name(name() + ".BlockedCycles")
252 .desc("Number of cycles fetch has spent blocked")
253 .prereq(fetchBlockedCycles);
256 .name(name() + ".CacheLines")
257 .desc("Number of cache lines fetched")
258 .prereq(fetchedCacheLines);
261 .name(name() + ".MiscStallCycles")
262 .desc("Number of cycles fetch has spent waiting on interrupts, or "
263 "bad addresses, or out of MSHRs")
264 .prereq(fetchMiscStallCycles);
267 .name(name() + ".IcacheSquashes")
268 .desc("Number of outstanding Icache misses that were squashed")
269 .prereq(fetchIcacheSquashes);
272 .init(/* base value */ 0,
273 /* last value */ fetchWidth,
275 .name(name() + ".rateDist")
276 .desc("Number of instructions fetched each cycle (Total)")
280 .name(name() + ".idleRate")
281 .desc("Percent of cycles fetch was idle")
283 idleRate = fetchIdleCycles * 100 / cpu->numCycles;
286 .name(name() + ".branchRate")
287 .desc("Number of branch fetches per cycle")
288 .flags(Stats::total);
289 branchRate = fetchedBranches / cpu->numCycles;
292 .name(name() + ".rate")
293 .desc("Number of inst fetches per cycle")
294 .flags(Stats::total);
295 fetchRate = fetchedInsts / cpu->numCycles;
297 branchPred.regStats();
302 DefaultFetch<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *time_buffer)
304 timeBuffer = time_buffer;
306 // Create wires to get information from proper places in time buffer.
307 fromDecode = timeBuffer->getWire(-decodeToFetchDelay);
308 fromRename = timeBuffer->getWire(-renameToFetchDelay);
309 fromIEW = timeBuffer->getWire(-iewToFetchDelay);
310 fromCommit = timeBuffer->getWire(-commitToFetchDelay);
315 DefaultFetch<Impl>::setActiveThreads(std::list<ThreadID> *at_ptr)
317 activeThreads = at_ptr;
322 DefaultFetch<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
326 // Create wire to write information to proper place in fetch queue.
327 toDecode = fetchQueue->getWire(0);
332 DefaultFetch<Impl>::initStage()
334 // Setup PC and nextPC with initial state.
335 for (ThreadID tid = 0; tid < numThreads; tid++) {
336 pc[tid] = cpu->pcState(tid);
337 fetchOffset[tid] = 0;
341 for (ThreadID tid = 0; tid < numThreads; tid++) {
343 fetchStatus[tid] = Running;
345 priorityList.push_back(tid);
349 stalls[tid].decode = false;
350 stalls[tid].rename = false;
351 stalls[tid].iew = false;
352 stalls[tid].commit = false;
355 // Schedule fetch to get the correct PC from the CPU
356 // scheduleFetchStartupEvent(1);
358 // Fetch needs to start fetching instructions at the very beginning,
359 // so it must start up in active state.
365 DefaultFetch<Impl>::setIcache()
367 // Size of cache block.
368 cacheBlkSize = icachePort->peerBlockSize();
370 // Create mask to get rid of offset bits.
371 cacheBlkMask = (cacheBlkSize - 1);
373 for (ThreadID tid = 0; tid < numThreads; tid++) {
374 // Create space to store a cache line.
375 cacheData[tid] = new uint8_t[cacheBlkSize];
376 cacheDataPC[tid] = 0;
377 cacheDataValid[tid] = false;
383 DefaultFetch<Impl>::processCacheCompletion(PacketPtr pkt)
385 ThreadID tid = pkt->req->threadId();
387 DPRINTF(Fetch, "[tid:%u] Waking up from cache miss.\n", tid);
389 assert(!pkt->wasNacked());
391 // Only change the status if it's still waiting on the icache access
393 if (fetchStatus[tid] != IcacheWaitResponse ||
394 pkt->req != memReq[tid] ||
396 ++fetchIcacheSquashes;
402 memcpy(cacheData[tid], pkt->getPtr<uint8_t>(), cacheBlkSize);
403 cacheDataValid[tid] = true;
406 // Wake up the CPU (if it went to sleep and was waiting on
407 // this completion event).
410 DPRINTF(Activity, "[tid:%u] Activating fetch due to cache completion\n",
416 // Only switch to IcacheAccessComplete if we're not stalled as well.
417 if (checkStall(tid)) {
418 fetchStatus[tid] = Blocked;
420 fetchStatus[tid] = IcacheAccessComplete;
423 // Reset the mem req to NULL.
429 template <class Impl>
431 DefaultFetch<Impl>::drain()
433 // Fetch is ready to drain at any time.
434 cpu->signalDrained();
439 template <class Impl>
441 DefaultFetch<Impl>::resume()
443 drainPending = false;
446 template <class Impl>
448 DefaultFetch<Impl>::switchOut()
451 // Branch predictor needs to have its state cleared.
452 branchPred.switchOut();
455 template <class Impl>
457 DefaultFetch<Impl>::takeOverFrom()
460 for (ThreadID i = 0; i < Impl::MaxThreads; ++i) {
461 stalls[i].decode = 0;
462 stalls[i].rename = 0;
464 stalls[i].commit = 0;
465 pc[i] = cpu->pcState(i);
466 fetchStatus[i] = Running;
469 wroteToTimeBuffer = false;
472 interruptPending = false;
473 branchPred.takeOverFrom();
476 template <class Impl>
478 DefaultFetch<Impl>::wakeFromQuiesce()
480 DPRINTF(Fetch, "Waking up from quiesce\n");
481 // Hopefully this is safe
482 // @todo: Allow other threads to wake from quiesce.
483 fetchStatus[0] = Running;
486 template <class Impl>
488 DefaultFetch<Impl>::switchToActive()
490 if (_status == Inactive) {
491 DPRINTF(Activity, "Activating stage.\n");
493 cpu->activateStage(O3CPU::FetchIdx);
499 template <class Impl>
501 DefaultFetch<Impl>::switchToInactive()
503 if (_status == Active) {
504 DPRINTF(Activity, "Deactivating stage.\n");
506 cpu->deactivateStage(O3CPU::FetchIdx);
512 template <class Impl>
514 DefaultFetch<Impl>::lookupAndUpdateNextPC(
515 DynInstPtr &inst, TheISA::PCState &nextPC)
517 // Do branch prediction check here.
518 // A bit of a misnomer...next_PC is actually the current PC until
519 // this function updates it.
522 if (!inst->isControl()) {
523 TheISA::advancePC(nextPC, inst->staticInst);
524 inst->setPredTarg(nextPC);
525 inst->setPredTaken(false);
529 ThreadID tid = inst->threadNumber;
530 predict_taken = branchPred.predict(inst, nextPC, tid);
533 DPRINTF(Fetch, "[tid:%i]: [sn:%i]: Branch predicted to be taken to %s.\n",
534 tid, inst->seqNum, nextPC);
536 DPRINTF(Fetch, "[tid:%i]: [sn:%i]:Branch predicted to be not taken.\n",
540 DPRINTF(Fetch, "[tid:%i]: [sn:%i] Branch predicted to go to %s.\n",
541 tid, inst->seqNum, nextPC);
542 inst->setPredTarg(nextPC);
543 inst->setPredTaken(predict_taken);
551 return predict_taken;
554 template <class Impl>
556 DefaultFetch<Impl>::fetchCacheLine(Addr vaddr, ThreadID tid, Addr pc)
558 Fault fault = NoFault;
560 // @todo: not sure if these should block translation.
563 DPRINTF(Fetch, "[tid:%i] Can't fetch cache line, cache blocked\n",
566 } else if (isSwitchedOut()) {
567 DPRINTF(Fetch, "[tid:%i] Can't fetch cache line, switched out\n",
570 } else if (checkInterrupt(pc)) {
571 // Hold off fetch from getting new instructions when:
572 // Cache is blocked, or
573 // while an interrupt is pending and we're not in PAL mode, or
574 // fetch is switched out.
575 DPRINTF(Fetch, "[tid:%i] Can't fetch cache line, interrupt pending\n",
580 // Align the fetch address so it's at the start of a cache block.
581 Addr block_PC = icacheBlockAlignPC(vaddr);
583 // Setup the memReq to do a read of the first instruction's address.
584 // Set the appropriate read size and flags as well.
585 // Build request here.
587 new Request(tid, block_PC, cacheBlkSize, Request::INST_FETCH,
588 pc, cpu->thread[tid]->contextId(), tid);
590 memReq[tid] = mem_req;
592 // Initiate translation of the icache block
593 fetchStatus[tid] = ItlbWait;
594 FetchTranslation *trans = new FetchTranslation(this);
595 cpu->itb->translateTiming(mem_req, cpu->thread[tid]->getTC(),
596 trans, BaseTLB::Execute);
600 template <class Impl>
602 DefaultFetch<Impl>::finishTranslation(Fault fault, RequestPtr mem_req)
604 ThreadID tid = mem_req->threadId();
605 Addr block_PC = mem_req->getVaddr();
607 // If translation was successful, attempt to read the icache block.
608 if (fault == NoFault) {
609 // Build packet here.
610 PacketPtr data_pkt = new Packet(mem_req,
611 MemCmd::ReadReq, Packet::Broadcast);
612 data_pkt->dataDynamicArray(new uint8_t[cacheBlkSize]);
614 cacheDataPC[tid] = block_PC;
615 cacheDataValid[tid] = false;
616 DPRINTF(Fetch, "Fetch: Doing instruction read.\n");
621 if (!icachePort->sendTiming(data_pkt)) {
622 assert(retryPkt == NULL);
623 assert(retryTid == InvalidThreadID);
624 DPRINTF(Fetch, "[tid:%i] Out of MSHRs!\n", tid);
626 fetchStatus[tid] = IcacheWaitRetry;
631 DPRINTF(Fetch, "[tid:%i]: Doing Icache access.\n", tid);
632 DPRINTF(Activity, "[tid:%i]: Activity: Waiting on I-cache "
635 lastIcacheStall[tid] = curTick();
636 fetchStatus[tid] = IcacheWaitResponse;
639 // Translation faulted, icache request won't be sent.
643 // Send the fault to commit. This thread will not do anything
644 // until commit handles the fault. The only other way it can
645 // wake up is if a squash comes along and changes the PC.
646 TheISA::PCState fetchPC = pc[tid];
648 // We will use a nop in ordier to carry the fault.
649 DynInstPtr instruction = buildInst(tid,
650 StaticInstPtr(TheISA::NoopMachInst, fetchPC.instAddr()),
651 NULL, fetchPC, fetchPC, false);
653 instruction->setPredTarg(fetchPC);
654 instruction->fault = fault;
655 wroteToTimeBuffer = true;
657 fetchStatus[tid] = TrapPending;
659 DPRINTF(Fetch, "[tid:%i]: Blocked, need to handle the trap.\n", tid);
660 DPRINTF(Fetch, "[tid:%i]: fault (%s) detected @ PC %s.\n",
661 tid, fault->name(), pc[tid]);
663 _status = updateFetchStatus();
666 template <class Impl>
668 DefaultFetch<Impl>::doSquash(const TheISA::PCState &newPC, ThreadID tid)
670 DPRINTF(Fetch, "[tid:%i]: Squashing, setting PC to: %s.\n",
674 fetchOffset[tid] = 0;
678 // Clear the icache miss if it's outstanding.
679 if (fetchStatus[tid] == IcacheWaitResponse) {
680 DPRINTF(Fetch, "[tid:%i]: Squashing outstanding Icache miss.\n",
685 // Get rid of the retrying packet if it was from this thread.
686 if (retryTid == tid) {
687 assert(cacheBlocked);
689 delete retryPkt->req;
693 retryTid = InvalidThreadID;
696 fetchStatus[tid] = Squashing;
703 DefaultFetch<Impl>::squashFromDecode(const TheISA::PCState &newPC,
704 const InstSeqNum &seq_num, ThreadID tid)
706 DPRINTF(Fetch, "[tid:%i]: Squashing from decode.\n", tid);
708 doSquash(newPC, tid);
710 // Tell the CPU to remove any instructions that are in flight between
712 cpu->removeInstsUntil(seq_num, tid);
717 DefaultFetch<Impl>::checkStall(ThreadID tid) const
719 bool ret_val = false;
721 if (cpu->contextSwitch) {
722 DPRINTF(Fetch,"[tid:%i]: Stalling for a context switch.\n",tid);
724 } else if (stalls[tid].decode) {
725 DPRINTF(Fetch,"[tid:%i]: Stall from Decode stage detected.\n",tid);
727 } else if (stalls[tid].rename) {
728 DPRINTF(Fetch,"[tid:%i]: Stall from Rename stage detected.\n",tid);
730 } else if (stalls[tid].iew) {
731 DPRINTF(Fetch,"[tid:%i]: Stall from IEW stage detected.\n",tid);
733 } else if (stalls[tid].commit) {
734 DPRINTF(Fetch,"[tid:%i]: Stall from Commit stage detected.\n",tid);
742 typename DefaultFetch<Impl>::FetchStatus
743 DefaultFetch<Impl>::updateFetchStatus()
746 list<ThreadID>::iterator threads = activeThreads->begin();
747 list<ThreadID>::iterator end = activeThreads->end();
749 while (threads != end) {
750 ThreadID tid = *threads++;
752 if (fetchStatus[tid] == Running ||
753 fetchStatus[tid] == Squashing ||
754 fetchStatus[tid] == IcacheAccessComplete) {
756 if (_status == Inactive) {
757 DPRINTF(Activity, "[tid:%i]: Activating stage.\n",tid);
759 if (fetchStatus[tid] == IcacheAccessComplete) {
760 DPRINTF(Activity, "[tid:%i]: Activating fetch due to cache"
764 cpu->activateStage(O3CPU::FetchIdx);
771 // Stage is switching from active to inactive, notify CPU of it.
772 if (_status == Active) {
773 DPRINTF(Activity, "Deactivating stage.\n");
775 cpu->deactivateStage(O3CPU::FetchIdx);
781 template <class Impl>
783 DefaultFetch<Impl>::squash(const TheISA::PCState &newPC,
784 const InstSeqNum &seq_num, ThreadID tid)
786 DPRINTF(Fetch, "[tid:%u]: Squash from commit.\n", tid);
788 doSquash(newPC, tid);
790 // Tell the CPU to remove any instructions that are not in the ROB.
791 cpu->removeInstsNotInROB(tid);
794 template <class Impl>
796 DefaultFetch<Impl>::tick()
798 list<ThreadID>::iterator threads = activeThreads->begin();
799 list<ThreadID>::iterator end = activeThreads->end();
800 bool status_change = false;
802 wroteToTimeBuffer = false;
804 while (threads != end) {
805 ThreadID tid = *threads++;
807 // Check the signals for each thread to determine the proper status
809 bool updated_status = checkSignalsAndUpdate(tid);
810 status_change = status_change || updated_status;
813 DPRINTF(Fetch, "Running stage.\n");
815 // Reset the number of the instruction we're fetching.
819 if (fromCommit->commitInfo[0].interruptPending) {
820 interruptPending = true;
823 if (fromCommit->commitInfo[0].clearInterrupt) {
824 interruptPending = false;
828 for (threadFetched = 0; threadFetched < numFetchingThreads;
830 // Fetch each of the actively fetching threads.
831 fetch(status_change);
834 // Record number of instructions fetched this cycle for distribution.
835 fetchNisnDist.sample(numInst);
838 // Change the fetch stage status if there was a status change.
839 _status = updateFetchStatus();
842 // If there was activity this cycle, inform the CPU of it.
843 if (wroteToTimeBuffer || cpu->contextSwitch) {
844 DPRINTF(Activity, "Activity this cycle.\n");
846 cpu->activityThisCycle();
850 template <class Impl>
852 DefaultFetch<Impl>::checkSignalsAndUpdate(ThreadID tid)
854 // Update the per thread stall statuses.
855 if (fromDecode->decodeBlock[tid]) {
856 stalls[tid].decode = true;
859 if (fromDecode->decodeUnblock[tid]) {
860 assert(stalls[tid].decode);
861 assert(!fromDecode->decodeBlock[tid]);
862 stalls[tid].decode = false;
865 if (fromRename->renameBlock[tid]) {
866 stalls[tid].rename = true;
869 if (fromRename->renameUnblock[tid]) {
870 assert(stalls[tid].rename);
871 assert(!fromRename->renameBlock[tid]);
872 stalls[tid].rename = false;
875 if (fromIEW->iewBlock[tid]) {
876 stalls[tid].iew = true;
879 if (fromIEW->iewUnblock[tid]) {
880 assert(stalls[tid].iew);
881 assert(!fromIEW->iewBlock[tid]);
882 stalls[tid].iew = false;
885 if (fromCommit->commitBlock[tid]) {
886 stalls[tid].commit = true;
889 if (fromCommit->commitUnblock[tid]) {
890 assert(stalls[tid].commit);
891 assert(!fromCommit->commitBlock[tid]);
892 stalls[tid].commit = false;
895 // Check squash signals from commit.
896 if (fromCommit->commitInfo[tid].squash) {
898 DPRINTF(Fetch, "[tid:%u]: Squashing instructions due to squash "
899 "from commit.\n",tid);
900 // In any case, squash.
901 squash(fromCommit->commitInfo[tid].pc,
902 fromCommit->commitInfo[tid].doneSeqNum,
905 // If it was a branch mispredict on a control instruction, update the
906 // branch predictor with that instruction, otherwise just kill the
907 // invalid state we generated in after sequence number
908 assert(!fromCommit->commitInfo[tid].branchMispredict ||
909 fromCommit->commitInfo[tid].mispredictInst);
911 if (fromCommit->commitInfo[tid].branchMispredict &&
912 fromCommit->commitInfo[tid].mispredictInst->isControl()) {
913 branchPred.squash(fromCommit->commitInfo[tid].doneSeqNum,
914 fromCommit->commitInfo[tid].pc,
915 fromCommit->commitInfo[tid].branchTaken,
918 branchPred.squash(fromCommit->commitInfo[tid].doneSeqNum,
923 } else if (fromCommit->commitInfo[tid].doneSeqNum) {
924 // Update the branch predictor if it wasn't a squashed instruction
925 // that was broadcasted.
926 branchPred.update(fromCommit->commitInfo[tid].doneSeqNum, tid);
929 // Check ROB squash signals from commit.
930 if (fromCommit->commitInfo[tid].robSquashing) {
931 DPRINTF(Fetch, "[tid:%u]: ROB is still squashing.\n", tid);
933 // Continue to squash.
934 fetchStatus[tid] = Squashing;
939 // Check squash signals from decode.
940 if (fromDecode->decodeInfo[tid].squash) {
941 DPRINTF(Fetch, "[tid:%u]: Squashing instructions due to squash "
942 "from decode.\n",tid);
944 // Update the branch predictor.
945 if (fromDecode->decodeInfo[tid].branchMispredict) {
946 branchPred.squash(fromDecode->decodeInfo[tid].doneSeqNum,
947 fromDecode->decodeInfo[tid].nextPC,
948 fromDecode->decodeInfo[tid].branchTaken,
951 branchPred.squash(fromDecode->decodeInfo[tid].doneSeqNum,
955 if (fetchStatus[tid] != Squashing) {
957 TheISA::PCState nextPC = fromDecode->decodeInfo[tid].nextPC;
958 DPRINTF(Fetch, "Squashing from decode with PC = %s\n", nextPC);
959 // Squash unless we're already squashing
960 squashFromDecode(fromDecode->decodeInfo[tid].nextPC,
961 fromDecode->decodeInfo[tid].doneSeqNum,
968 if (checkStall(tid) &&
969 fetchStatus[tid] != IcacheWaitResponse &&
970 fetchStatus[tid] != IcacheWaitRetry) {
971 DPRINTF(Fetch, "[tid:%i]: Setting to blocked\n",tid);
973 fetchStatus[tid] = Blocked;
978 if (fetchStatus[tid] == Blocked ||
979 fetchStatus[tid] == Squashing) {
980 // Switch status to running if fetch isn't being told to block or
981 // squash this cycle.
982 DPRINTF(Fetch, "[tid:%i]: Done squashing, switching to running.\n",
985 fetchStatus[tid] = Running;
990 // If we've reached this point, we have not gotten any signals that
991 // cause fetch to change its status. Fetch remains the same as before.
996 typename Impl::DynInstPtr
997 DefaultFetch<Impl>::buildInst(ThreadID tid, StaticInstPtr staticInst,
998 StaticInstPtr curMacroop, TheISA::PCState thisPC,
999 TheISA::PCState nextPC, bool trace)
1001 // Get a sequence number.
1002 InstSeqNum seq = cpu->getAndIncrementInstSeq();
1004 // Create a new DynInst from the instruction fetched.
1005 DynInstPtr instruction =
1006 new DynInst(staticInst, thisPC, nextPC, seq, cpu);
1007 instruction->setTid(tid);
1009 instruction->setASID(tid);
1011 instruction->setThreadState(cpu->thread[tid]);
1013 DPRINTF(Fetch, "[tid:%i]: Instruction PC %#x (%d) created "
1014 "[sn:%lli].\n", tid, thisPC.instAddr(),
1015 thisPC.microPC(), seq);
1017 DPRINTF(Fetch, "[tid:%i]: Instruction is: %s\n", tid,
1018 instruction->staticInst->
1019 disassemble(thisPC.instAddr()));
1023 instruction->traceData =
1024 cpu->getTracer()->getInstRecord(curTick(), cpu->tcBase(tid),
1025 instruction->staticInst, thisPC, curMacroop);
1028 instruction->traceData = NULL;
1031 // Add instruction to the CPU's list of instructions.
1032 instruction->setInstListIt(cpu->addInst(instruction));
1034 // Write the instruction to the first slot in the queue
1035 // that heads to decode.
1036 assert(numInst < fetchWidth);
1037 toDecode->insts[toDecode->size++] = instruction;
1042 template<class Impl>
1044 DefaultFetch<Impl>::fetch(bool &status_change)
1046 //////////////////////////////////////////
1047 // Start actual fetch
1048 //////////////////////////////////////////
1049 ThreadID tid = getFetchingThread(fetchPolicy);
1051 if (tid == InvalidThreadID || drainPending) {
1052 DPRINTF(Fetch,"There are no more threads available to fetch from.\n");
1054 // Breaks looping condition in tick()
1055 threadFetched = numFetchingThreads;
1059 DPRINTF(Fetch, "Attempting to fetch from [tid:%i]\n", tid);
1062 TheISA::PCState thisPC = pc[tid];
1064 Addr pcOffset = fetchOffset[tid];
1065 Addr fetchAddr = (thisPC.instAddr() + pcOffset) & BaseCPU::PCMask;
1067 // If returning from the delay of a cache miss, then update the status
1068 // to running, otherwise do the cache access. Possibly move this up
1069 // to tick() function.
1070 if (fetchStatus[tid] == IcacheAccessComplete) {
1071 DPRINTF(Fetch, "[tid:%i]: Icache miss is complete.\n", tid);
1073 fetchStatus[tid] = Running;
1074 status_change = true;
1075 } else if (fetchStatus[tid] == Running) {
1076 // Align the fetch PC so its at the start of a cache block.
1077 Addr block_PC = icacheBlockAlignPC(fetchAddr);
1079 // Unless buffer already got the block, fetch it from icache.
1080 if (!cacheDataValid[tid] || block_PC != cacheDataPC[tid]) {
1081 DPRINTF(Fetch, "[tid:%i]: Attempting to translate and read "
1082 "instruction, starting at PC %s.\n", tid, thisPC);
1084 fetchCacheLine(fetchAddr, tid, thisPC.instAddr());
1086 if (fetchStatus[tid] == IcacheWaitResponse)
1087 ++icacheStallCycles;
1088 else if (fetchStatus[tid] == ItlbWait)
1091 ++fetchMiscStallCycles;
1093 } else if (checkInterrupt(thisPC.instAddr()) || isSwitchedOut()) {
1094 ++fetchMiscStallCycles;
1098 if (fetchStatus[tid] == Idle) {
1100 DPRINTF(Fetch, "[tid:%i]: Fetch is idle!\n", tid);
1101 } else if (fetchStatus[tid] == Blocked) {
1102 ++fetchBlockedCycles;
1103 DPRINTF(Fetch, "[tid:%i]: Fetch is blocked!\n", tid);
1104 } else if (fetchStatus[tid] == Squashing) {
1105 ++fetchSquashCycles;
1106 DPRINTF(Fetch, "[tid:%i]: Fetch is squashing!\n", tid);
1107 } else if (fetchStatus[tid] == IcacheWaitResponse) {
1108 ++icacheStallCycles;
1109 DPRINTF(Fetch, "[tid:%i]: Fetch is waiting cache response!\n",
1111 } else if (fetchStatus[tid] == ItlbWait) {
1112 DPRINTF(Fetch, "[tid:%i]: Fetch is waiting ITLB walk to "
1117 // Status is Idle, Squashing, Blocked, ItlbWait or IcacheWaitResponse
1118 // so fetch should do nothing.
1124 TheISA::PCState nextPC = thisPC;
1126 StaticInstPtr staticInst = NULL;
1127 StaticInstPtr curMacroop = macroop[tid];
1129 // If the read of the first instruction was successful, then grab the
1130 // instructions from the rest of the cache line and put them into the
1131 // queue heading to decode.
1133 DPRINTF(Fetch, "[tid:%i]: Adding instructions to queue to "
1136 // Need to keep track of whether or not a predicted branch
1137 // ended this fetch block.
1138 bool predictedBranch = false;
1140 TheISA::MachInst *cacheInsts =
1141 reinterpret_cast<TheISA::MachInst *>(cacheData[tid]);
1143 const unsigned numInsts = cacheBlkSize / instSize;
1144 unsigned blkOffset = (fetchAddr - cacheDataPC[tid]) / instSize;
1146 // Loop through instruction memory from the cache.
1147 while (blkOffset < numInsts &&
1148 numInst < fetchWidth &&
1151 // If we need to process more memory, do it now.
1152 if (!curMacroop && !predecoder.extMachInstReady()) {
1153 if (ISA_HAS_DELAY_SLOT && pcOffset == 0) {
1154 // Walk past any annulled delay slot instructions.
1155 Addr pcAddr = thisPC.instAddr() & BaseCPU::PCMask;
1156 while (fetchAddr != pcAddr && blkOffset < numInsts) {
1158 fetchAddr += instSize;
1160 if (blkOffset >= numInsts)
1163 MachInst inst = TheISA::gtoh(cacheInsts[blkOffset]);
1165 predecoder.setTC(cpu->thread[tid]->getTC());
1166 predecoder.moreBytes(thisPC, fetchAddr, inst);
1168 if (predecoder.needMoreBytes()) {
1170 fetchAddr += instSize;
1171 pcOffset += instSize;
1175 // Extract as many instructions and/or microops as we can from
1176 // the memory we've processed so far.
1179 if (predecoder.extMachInstReady()) {
1180 ExtMachInst extMachInst;
1182 extMachInst = predecoder.getExtMachInst(thisPC);
1183 staticInst = StaticInstPtr(extMachInst,
1186 // Increment stat of fetched instructions.
1189 if (staticInst->isMacroop())
1191 curMacroop = staticInst;
1197 // We need more bytes for this instruction.
1202 staticInst = curMacroop->fetchMicroop(thisPC.microPC());
1203 if (staticInst->isLastMicroop()) {
1209 DynInstPtr instruction =
1210 buildInst(tid, staticInst, curMacroop,
1211 thisPC, nextPC, true);
1217 // If we're branching after this instruction, quite fetching
1218 // from the same block then.
1219 predictedBranch |= thisPC.branching();
1221 lookupAndUpdateNextPC(instruction, nextPC);
1222 if (predictedBranch) {
1223 DPRINTF(Fetch, "Branch detected with PC = %s\n", thisPC);
1226 // Move to the next instruction, unless we have a branch.
1229 if (instruction->isQuiesce()) {
1231 "Quiesce instruction encountered, halting fetch!");
1232 fetchStatus[tid] = QuiescePending;
1233 status_change = true;
1236 } while ((curMacroop || predecoder.extMachInstReady()) &&
1237 numInst < fetchWidth);
1240 if (predictedBranch) {
1241 DPRINTF(Fetch, "[tid:%i]: Done fetching, predicted branch "
1242 "instruction encountered.\n", tid);
1243 } else if (numInst >= fetchWidth) {
1244 DPRINTF(Fetch, "[tid:%i]: Done fetching, reached fetch bandwidth "
1245 "for this cycle.\n", tid);
1246 } else if (blkOffset >= cacheBlkSize) {
1247 DPRINTF(Fetch, "[tid:%i]: Done fetching, reached the end of cache "
1251 macroop[tid] = curMacroop;
1252 fetchOffset[tid] = pcOffset;
1255 wroteToTimeBuffer = true;
1261 template<class Impl>
1263 DefaultFetch<Impl>::recvRetry()
1265 if (retryPkt != NULL) {
1266 assert(cacheBlocked);
1267 assert(retryTid != InvalidThreadID);
1268 assert(fetchStatus[retryTid] == IcacheWaitRetry);
1270 if (icachePort->sendTiming(retryPkt)) {
1271 fetchStatus[retryTid] = IcacheWaitResponse;
1273 retryTid = InvalidThreadID;
1274 cacheBlocked = false;
1277 assert(retryTid == InvalidThreadID);
1278 // Access has been squashed since it was sent out. Just clear
1279 // the cache being blocked.
1280 cacheBlocked = false;
1284 ///////////////////////////////////////
1286 // SMT FETCH POLICY MAINTAINED HERE //
1288 ///////////////////////////////////////
1289 template<class Impl>
1291 DefaultFetch<Impl>::getFetchingThread(FetchPriority &fetch_priority)
1293 if (numThreads > 1) {
1294 switch (fetch_priority) {
1300 return roundRobin();
1309 return branchCount();
1312 return InvalidThreadID;
1315 list<ThreadID>::iterator thread = activeThreads->begin();
1316 if (thread == activeThreads->end()) {
1317 return InvalidThreadID;
1320 ThreadID tid = *thread;
1322 if (fetchStatus[tid] == Running ||
1323 fetchStatus[tid] == IcacheAccessComplete ||
1324 fetchStatus[tid] == Idle) {
1327 return InvalidThreadID;
1333 template<class Impl>
1335 DefaultFetch<Impl>::roundRobin()
1337 list<ThreadID>::iterator pri_iter = priorityList.begin();
1338 list<ThreadID>::iterator end = priorityList.end();
1342 while (pri_iter != end) {
1343 high_pri = *pri_iter;
1345 assert(high_pri <= numThreads);
1347 if (fetchStatus[high_pri] == Running ||
1348 fetchStatus[high_pri] == IcacheAccessComplete ||
1349 fetchStatus[high_pri] == Idle) {
1351 priorityList.erase(pri_iter);
1352 priorityList.push_back(high_pri);
1360 return InvalidThreadID;
1363 template<class Impl>
1365 DefaultFetch<Impl>::iqCount()
1367 std::priority_queue<ThreadID> PQ;
1369 list<ThreadID>::iterator threads = activeThreads->begin();
1370 list<ThreadID>::iterator end = activeThreads->end();
1372 while (threads != end) {
1373 ThreadID tid = *threads++;
1375 PQ.push(fromIEW->iewInfo[tid].iqCount);
1378 while (!PQ.empty()) {
1379 ThreadID high_pri = PQ.top();
1381 if (fetchStatus[high_pri] == Running ||
1382 fetchStatus[high_pri] == IcacheAccessComplete ||
1383 fetchStatus[high_pri] == Idle)
1390 return InvalidThreadID;
1393 template<class Impl>
1395 DefaultFetch<Impl>::lsqCount()
1397 std::priority_queue<ThreadID> PQ;
1399 list<ThreadID>::iterator threads = activeThreads->begin();
1400 list<ThreadID>::iterator end = activeThreads->end();
1402 while (threads != end) {
1403 ThreadID tid = *threads++;
1405 PQ.push(fromIEW->iewInfo[tid].ldstqCount);
1408 while (!PQ.empty()) {
1409 ThreadID high_pri = PQ.top();
1411 if (fetchStatus[high_pri] == Running ||
1412 fetchStatus[high_pri] == IcacheAccessComplete ||
1413 fetchStatus[high_pri] == Idle)
1419 return InvalidThreadID;
1422 template<class Impl>
1424 DefaultFetch<Impl>::branchCount()
1427 list<ThreadID>::iterator thread = activeThreads->begin();
1428 assert(thread != activeThreads->end());
1429 ThreadID tid = *thread;
1432 panic("Branch Count Fetch policy unimplemented\n");
1433 return InvalidThreadID;