Merge with head.
[gem5.git] / src / cpu / o3 / fetch_impl.hh
1 /*
2 * Copyright (c) 2004-2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Kevin Lim
29 * Korey Sewell
30 */
31
32 #include <algorithm>
33 #include <cstring>
34
35 #include "config/use_checker.hh"
36
37 #include "arch/isa_traits.hh"
38 #include "arch/utility.hh"
39 #include "cpu/checker/cpu.hh"
40 #include "cpu/exetrace.hh"
41 #include "cpu/o3/fetch.hh"
42 #include "mem/packet.hh"
43 #include "mem/request.hh"
44 #include "sim/byteswap.hh"
45 #include "sim/host.hh"
46 #include "sim/core.hh"
47
48 #if FULL_SYSTEM
49 #include "arch/tlb.hh"
50 #include "arch/vtophys.hh"
51 #include "sim/system.hh"
52 #endif // FULL_SYSTEM
53
54 template<class Impl>
55 void
56 DefaultFetch<Impl>::IcachePort::setPeer(Port *port)
57 {
58 Port::setPeer(port);
59
60 fetch->setIcache();
61 }
62
63 template<class Impl>
64 Tick
65 DefaultFetch<Impl>::IcachePort::recvAtomic(PacketPtr pkt)
66 {
67 panic("DefaultFetch doesn't expect recvAtomic callback!");
68 return curTick;
69 }
70
71 template<class Impl>
72 void
73 DefaultFetch<Impl>::IcachePort::recvFunctional(PacketPtr pkt)
74 {
75 DPRINTF(Fetch, "DefaultFetch doesn't update its state from a "
76 "functional call.");
77 }
78
79 template<class Impl>
80 void
81 DefaultFetch<Impl>::IcachePort::recvStatusChange(Status status)
82 {
83 if (status == RangeChange) {
84 if (!snoopRangeSent) {
85 snoopRangeSent = true;
86 sendStatusChange(Port::RangeChange);
87 }
88 return;
89 }
90
91 panic("DefaultFetch doesn't expect recvStatusChange callback!");
92 }
93
94 template<class Impl>
95 bool
96 DefaultFetch<Impl>::IcachePort::recvTiming(PacketPtr pkt)
97 {
98 DPRINTF(Fetch, "Received timing\n");
99 if (pkt->isResponse()) {
100 fetch->processCacheCompletion(pkt);
101 }
102 //else Snooped a coherence request, just return
103 return true;
104 }
105
106 template<class Impl>
107 void
108 DefaultFetch<Impl>::IcachePort::recvRetry()
109 {
110 fetch->recvRetry();
111 }
112
113 template<class Impl>
114 DefaultFetch<Impl>::DefaultFetch(O3CPU *_cpu, Params *params)
115 : cpu(_cpu),
116 branchPred(params),
117 predecoder(NULL),
118 decodeToFetchDelay(params->decodeToFetchDelay),
119 renameToFetchDelay(params->renameToFetchDelay),
120 iewToFetchDelay(params->iewToFetchDelay),
121 commitToFetchDelay(params->commitToFetchDelay),
122 fetchWidth(params->fetchWidth),
123 cacheBlocked(false),
124 retryPkt(NULL),
125 retryTid(-1),
126 numThreads(params->numberOfThreads),
127 numFetchingThreads(params->smtNumFetchingThreads),
128 interruptPending(false),
129 drainPending(false),
130 switchedOut(false)
131 {
132 if (numThreads > Impl::MaxThreads)
133 fatal("numThreads is not a valid value\n");
134
135 // Set fetch stage's status to inactive.
136 _status = Inactive;
137
138 std::string policy = params->smtFetchPolicy;
139
140 // Convert string to lowercase
141 std::transform(policy.begin(), policy.end(), policy.begin(),
142 (int(*)(int)) tolower);
143
144 // Figure out fetch policy
145 if (policy == "singlethread") {
146 fetchPolicy = SingleThread;
147 if (numThreads > 1)
148 panic("Invalid Fetch Policy for a SMT workload.");
149 } else if (policy == "roundrobin") {
150 fetchPolicy = RoundRobin;
151 DPRINTF(Fetch, "Fetch policy set to Round Robin\n");
152 } else if (policy == "branch") {
153 fetchPolicy = Branch;
154 DPRINTF(Fetch, "Fetch policy set to Branch Count\n");
155 } else if (policy == "iqcount") {
156 fetchPolicy = IQ;
157 DPRINTF(Fetch, "Fetch policy set to IQ count\n");
158 } else if (policy == "lsqcount") {
159 fetchPolicy = LSQ;
160 DPRINTF(Fetch, "Fetch policy set to LSQ count\n");
161 } else {
162 fatal("Invalid Fetch Policy. Options Are: {SingleThread,"
163 " RoundRobin,LSQcount,IQcount}\n");
164 }
165
166 // Get the size of an instruction.
167 instSize = sizeof(TheISA::MachInst);
168
169 // Name is finally available, so create the port.
170 icachePort = new IcachePort(this);
171
172 icachePort->snoopRangeSent = false;
173
174 #if USE_CHECKER
175 if (cpu->checker) {
176 cpu->checker->setIcachePort(icachePort);
177 }
178 #endif
179 }
180
181 template <class Impl>
182 std::string
183 DefaultFetch<Impl>::name() const
184 {
185 return cpu->name() + ".fetch";
186 }
187
188 template <class Impl>
189 void
190 DefaultFetch<Impl>::regStats()
191 {
192 icacheStallCycles
193 .name(name() + ".icacheStallCycles")
194 .desc("Number of cycles fetch is stalled on an Icache miss")
195 .prereq(icacheStallCycles);
196
197 fetchedInsts
198 .name(name() + ".Insts")
199 .desc("Number of instructions fetch has processed")
200 .prereq(fetchedInsts);
201
202 fetchedBranches
203 .name(name() + ".Branches")
204 .desc("Number of branches that fetch encountered")
205 .prereq(fetchedBranches);
206
207 predictedBranches
208 .name(name() + ".predictedBranches")
209 .desc("Number of branches that fetch has predicted taken")
210 .prereq(predictedBranches);
211
212 fetchCycles
213 .name(name() + ".Cycles")
214 .desc("Number of cycles fetch has run and was not squashing or"
215 " blocked")
216 .prereq(fetchCycles);
217
218 fetchSquashCycles
219 .name(name() + ".SquashCycles")
220 .desc("Number of cycles fetch has spent squashing")
221 .prereq(fetchSquashCycles);
222
223 fetchIdleCycles
224 .name(name() + ".IdleCycles")
225 .desc("Number of cycles fetch was idle")
226 .prereq(fetchIdleCycles);
227
228 fetchBlockedCycles
229 .name(name() + ".BlockedCycles")
230 .desc("Number of cycles fetch has spent blocked")
231 .prereq(fetchBlockedCycles);
232
233 fetchedCacheLines
234 .name(name() + ".CacheLines")
235 .desc("Number of cache lines fetched")
236 .prereq(fetchedCacheLines);
237
238 fetchMiscStallCycles
239 .name(name() + ".MiscStallCycles")
240 .desc("Number of cycles fetch has spent waiting on interrupts, or "
241 "bad addresses, or out of MSHRs")
242 .prereq(fetchMiscStallCycles);
243
244 fetchIcacheSquashes
245 .name(name() + ".IcacheSquashes")
246 .desc("Number of outstanding Icache misses that were squashed")
247 .prereq(fetchIcacheSquashes);
248
249 fetchNisnDist
250 .init(/* base value */ 0,
251 /* last value */ fetchWidth,
252 /* bucket size */ 1)
253 .name(name() + ".rateDist")
254 .desc("Number of instructions fetched each cycle (Total)")
255 .flags(Stats::pdf);
256
257 idleRate
258 .name(name() + ".idleRate")
259 .desc("Percent of cycles fetch was idle")
260 .prereq(idleRate);
261 idleRate = fetchIdleCycles * 100 / cpu->numCycles;
262
263 branchRate
264 .name(name() + ".branchRate")
265 .desc("Number of branch fetches per cycle")
266 .flags(Stats::total);
267 branchRate = fetchedBranches / cpu->numCycles;
268
269 fetchRate
270 .name(name() + ".rate")
271 .desc("Number of inst fetches per cycle")
272 .flags(Stats::total);
273 fetchRate = fetchedInsts / cpu->numCycles;
274
275 branchPred.regStats();
276 }
277
278 template<class Impl>
279 void
280 DefaultFetch<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *time_buffer)
281 {
282 timeBuffer = time_buffer;
283
284 // Create wires to get information from proper places in time buffer.
285 fromDecode = timeBuffer->getWire(-decodeToFetchDelay);
286 fromRename = timeBuffer->getWire(-renameToFetchDelay);
287 fromIEW = timeBuffer->getWire(-iewToFetchDelay);
288 fromCommit = timeBuffer->getWire(-commitToFetchDelay);
289 }
290
291 template<class Impl>
292 void
293 DefaultFetch<Impl>::setActiveThreads(std::list<unsigned> *at_ptr)
294 {
295 activeThreads = at_ptr;
296 }
297
298 template<class Impl>
299 void
300 DefaultFetch<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
301 {
302 fetchQueue = fq_ptr;
303
304 // Create wire to write information to proper place in fetch queue.
305 toDecode = fetchQueue->getWire(0);
306 }
307
308 template<class Impl>
309 void
310 DefaultFetch<Impl>::initStage()
311 {
312 // Setup PC and nextPC with initial state.
313 for (int tid = 0; tid < numThreads; tid++) {
314 PC[tid] = cpu->readPC(tid);
315 nextPC[tid] = cpu->readNextPC(tid);
316 microPC[tid] = cpu->readMicroPC(tid);
317 }
318
319 for (int tid=0; tid < numThreads; tid++) {
320
321 fetchStatus[tid] = Running;
322
323 priorityList.push_back(tid);
324
325 memReq[tid] = NULL;
326
327 stalls[tid].decode = false;
328 stalls[tid].rename = false;
329 stalls[tid].iew = false;
330 stalls[tid].commit = false;
331 }
332
333 // Schedule fetch to get the correct PC from the CPU
334 // scheduleFetchStartupEvent(1);
335
336 // Fetch needs to start fetching instructions at the very beginning,
337 // so it must start up in active state.
338 switchToActive();
339 }
340
341 template<class Impl>
342 void
343 DefaultFetch<Impl>::setIcache()
344 {
345 // Size of cache block.
346 cacheBlkSize = icachePort->peerBlockSize();
347
348 // Create mask to get rid of offset bits.
349 cacheBlkMask = (cacheBlkSize - 1);
350
351 for (int tid=0; tid < numThreads; tid++) {
352 // Create space to store a cache line.
353 cacheData[tid] = new uint8_t[cacheBlkSize];
354 cacheDataPC[tid] = 0;
355 cacheDataValid[tid] = false;
356 }
357 }
358
359 template<class Impl>
360 void
361 DefaultFetch<Impl>::processCacheCompletion(PacketPtr pkt)
362 {
363 unsigned tid = pkt->req->getThreadNum();
364
365 DPRINTF(Fetch, "[tid:%u] Waking up from cache miss.\n",tid);
366
367 // Only change the status if it's still waiting on the icache access
368 // to return.
369 if (fetchStatus[tid] != IcacheWaitResponse ||
370 pkt->req != memReq[tid] ||
371 isSwitchedOut()) {
372 ++fetchIcacheSquashes;
373 delete pkt->req;
374 delete pkt;
375 return;
376 }
377
378 memcpy(cacheData[tid], pkt->getPtr<uint8_t>(), cacheBlkSize);
379 cacheDataValid[tid] = true;
380
381 if (!drainPending) {
382 // Wake up the CPU (if it went to sleep and was waiting on
383 // this completion event).
384 cpu->wakeCPU();
385
386 DPRINTF(Activity, "[tid:%u] Activating fetch due to cache completion\n",
387 tid);
388
389 switchToActive();
390 }
391
392 // Only switch to IcacheAccessComplete if we're not stalled as well.
393 if (checkStall(tid)) {
394 fetchStatus[tid] = Blocked;
395 } else {
396 fetchStatus[tid] = IcacheAccessComplete;
397 }
398
399 // Reset the mem req to NULL.
400 delete pkt->req;
401 delete pkt;
402 memReq[tid] = NULL;
403 }
404
405 template <class Impl>
406 bool
407 DefaultFetch<Impl>::drain()
408 {
409 // Fetch is ready to drain at any time.
410 cpu->signalDrained();
411 drainPending = true;
412 return true;
413 }
414
415 template <class Impl>
416 void
417 DefaultFetch<Impl>::resume()
418 {
419 drainPending = false;
420 }
421
422 template <class Impl>
423 void
424 DefaultFetch<Impl>::switchOut()
425 {
426 switchedOut = true;
427 // Branch predictor needs to have its state cleared.
428 branchPred.switchOut();
429 }
430
431 template <class Impl>
432 void
433 DefaultFetch<Impl>::takeOverFrom()
434 {
435 // Reset all state
436 for (int i = 0; i < Impl::MaxThreads; ++i) {
437 stalls[i].decode = 0;
438 stalls[i].rename = 0;
439 stalls[i].iew = 0;
440 stalls[i].commit = 0;
441 PC[i] = cpu->readPC(i);
442 nextPC[i] = cpu->readNextPC(i);
443 microPC[i] = cpu->readMicroPC(i);
444 fetchStatus[i] = Running;
445 }
446 numInst = 0;
447 wroteToTimeBuffer = false;
448 _status = Inactive;
449 switchedOut = false;
450 interruptPending = false;
451 branchPred.takeOverFrom();
452 }
453
454 template <class Impl>
455 void
456 DefaultFetch<Impl>::wakeFromQuiesce()
457 {
458 DPRINTF(Fetch, "Waking up from quiesce\n");
459 // Hopefully this is safe
460 // @todo: Allow other threads to wake from quiesce.
461 fetchStatus[0] = Running;
462 }
463
464 template <class Impl>
465 inline void
466 DefaultFetch<Impl>::switchToActive()
467 {
468 if (_status == Inactive) {
469 DPRINTF(Activity, "Activating stage.\n");
470
471 cpu->activateStage(O3CPU::FetchIdx);
472
473 _status = Active;
474 }
475 }
476
477 template <class Impl>
478 inline void
479 DefaultFetch<Impl>::switchToInactive()
480 {
481 if (_status == Active) {
482 DPRINTF(Activity, "Deactivating stage.\n");
483
484 cpu->deactivateStage(O3CPU::FetchIdx);
485
486 _status = Inactive;
487 }
488 }
489
490 template <class Impl>
491 bool
492 DefaultFetch<Impl>::lookupAndUpdateNextPC(DynInstPtr &inst, Addr &next_PC,
493 Addr &next_NPC, Addr &next_MicroPC)
494 {
495 // Do branch prediction check here.
496 // A bit of a misnomer...next_PC is actually the current PC until
497 // this function updates it.
498 bool predict_taken;
499
500 if (!inst->isControl()) {
501 if (inst->isMicroop() && !inst->isLastMicroop()) {
502 next_MicroPC++;
503 } else {
504 next_PC = next_NPC;
505 next_NPC = next_NPC + instSize;
506 next_MicroPC = 0;
507 }
508 inst->setPredTarg(next_PC, next_NPC, next_MicroPC);
509 inst->setPredTaken(false);
510 return false;
511 }
512
513 //Assume for now that all control flow is to a different macroop which
514 //would reset the micro pc to 0.
515 next_MicroPC = 0;
516
517 int tid = inst->threadNumber;
518 Addr pred_PC = next_PC;
519 predict_taken = branchPred.predict(inst, pred_PC, tid);
520
521 /* if (predict_taken) {
522 DPRINTF(Fetch, "[tid:%i]: Branch predicted to be taken to %#x.\n",
523 tid, pred_PC);
524 } else {
525 DPRINTF(Fetch, "[tid:%i]: Branch predicted to be not taken.\n", tid);
526 }*/
527
528 #if ISA_HAS_DELAY_SLOT
529 next_PC = next_NPC;
530 if (predict_taken)
531 next_NPC = pred_PC;
532 else
533 next_NPC += instSize;
534 #else
535 if (predict_taken)
536 next_PC = pred_PC;
537 else
538 next_PC += instSize;
539 next_NPC = next_PC + instSize;
540 #endif
541 /* DPRINTF(Fetch, "[tid:%i]: Branch predicted to go to %#x and then %#x.\n",
542 tid, next_PC, next_NPC);*/
543 inst->setPredTarg(next_PC, next_NPC, next_MicroPC);
544 inst->setPredTaken(predict_taken);
545
546 ++fetchedBranches;
547
548 if (predict_taken) {
549 ++predictedBranches;
550 }
551
552 return predict_taken;
553 }
554
555 template <class Impl>
556 bool
557 DefaultFetch<Impl>::fetchCacheLine(Addr fetch_PC, Fault &ret_fault, unsigned tid)
558 {
559 Fault fault = NoFault;
560
561 //AlphaDep
562 if (cacheBlocked) {
563 DPRINTF(Fetch, "[tid:%i] Can't fetch cache line, cache blocked\n",
564 tid);
565 return false;
566 } else if (isSwitchedOut()) {
567 DPRINTF(Fetch, "[tid:%i] Can't fetch cache line, switched out\n",
568 tid);
569 return false;
570 } else if (interruptPending && !(fetch_PC & 0x3)) {
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",
576 tid);
577 return false;
578 }
579
580 // Align the fetch PC so it's at the start of a cache block.
581 Addr block_PC = icacheBlockAlignPC(fetch_PC);
582
583 // If we've already got the block, no need to try to fetch it again.
584 if (cacheDataValid[tid] && block_PC == cacheDataPC[tid]) {
585 return true;
586 }
587
588 // Setup the memReq to do a read of the first instruction's address.
589 // Set the appropriate read size and flags as well.
590 // Build request here.
591 RequestPtr mem_req = new Request(tid, block_PC, cacheBlkSize, 0,
592 fetch_PC, cpu->readCpuId(), tid);
593
594 memReq[tid] = mem_req;
595
596 // Translate the instruction request.
597 fault = cpu->translateInstReq(mem_req, cpu->thread[tid]);
598
599 // In the case of faults, the fetch stage may need to stall and wait
600 // for the ITB miss to be handled.
601
602 // If translation was successful, attempt to read the first
603 // instruction.
604 if (fault == NoFault) {
605 #if 0
606 if (cpu->system->memctrl->badaddr(memReq[tid]->paddr) ||
607 memReq[tid]->isUncacheable()) {
608 DPRINTF(Fetch, "Fetch: Bad address %#x (hopefully on a "
609 "misspeculating path)!",
610 memReq[tid]->paddr);
611 ret_fault = TheISA::genMachineCheckFault();
612 return false;
613 }
614 #endif
615
616 // Build packet here.
617 PacketPtr data_pkt = new Packet(mem_req,
618 MemCmd::ReadReq, Packet::Broadcast);
619 data_pkt->dataDynamicArray(new uint8_t[cacheBlkSize]);
620
621 cacheDataPC[tid] = block_PC;
622 cacheDataValid[tid] = false;
623
624 DPRINTF(Fetch, "Fetch: Doing instruction read.\n");
625
626 fetchedCacheLines++;
627
628 // Now do the timing access to see whether or not the instruction
629 // exists within the cache.
630 if (!icachePort->sendTiming(data_pkt)) {
631 assert(retryPkt == NULL);
632 assert(retryTid == -1);
633 DPRINTF(Fetch, "[tid:%i] Out of MSHRs!\n", tid);
634 fetchStatus[tid] = IcacheWaitRetry;
635 retryPkt = data_pkt;
636 retryTid = tid;
637 cacheBlocked = true;
638 return false;
639 }
640
641 DPRINTF(Fetch, "[tid:%i]: Doing cache access.\n", tid);
642
643 lastIcacheStall[tid] = curTick;
644
645 DPRINTF(Activity, "[tid:%i]: Activity: Waiting on I-cache "
646 "response.\n", tid);
647
648 fetchStatus[tid] = IcacheWaitResponse;
649 } else {
650 delete mem_req;
651 memReq[tid] = NULL;
652 }
653
654 ret_fault = fault;
655 return true;
656 }
657
658 template <class Impl>
659 inline void
660 DefaultFetch<Impl>::doSquash(const Addr &new_PC,
661 const Addr &new_NPC, const Addr &new_microPC, unsigned tid)
662 {
663 DPRINTF(Fetch, "[tid:%i]: Squashing, setting PC to: %#x, NPC to: %#x.\n",
664 tid, new_PC, new_NPC);
665
666 PC[tid] = new_PC;
667 nextPC[tid] = new_NPC;
668 microPC[tid] = new_microPC;
669
670 // Clear the icache miss if it's outstanding.
671 if (fetchStatus[tid] == IcacheWaitResponse) {
672 DPRINTF(Fetch, "[tid:%i]: Squashing outstanding Icache miss.\n",
673 tid);
674 memReq[tid] = NULL;
675 }
676
677 // Get rid of the retrying packet if it was from this thread.
678 if (retryTid == tid) {
679 assert(cacheBlocked);
680 if (retryPkt) {
681 delete retryPkt->req;
682 delete retryPkt;
683 }
684 retryPkt = NULL;
685 retryTid = -1;
686 }
687
688 fetchStatus[tid] = Squashing;
689
690 ++fetchSquashCycles;
691 }
692
693 template<class Impl>
694 void
695 DefaultFetch<Impl>::squashFromDecode(const Addr &new_PC, const Addr &new_NPC,
696 const Addr &new_MicroPC,
697 const InstSeqNum &seq_num, unsigned tid)
698 {
699 DPRINTF(Fetch, "[tid:%i]: Squashing from decode.\n",tid);
700
701 doSquash(new_PC, new_NPC, new_MicroPC, tid);
702
703 // Tell the CPU to remove any instructions that are in flight between
704 // fetch and decode.
705 cpu->removeInstsUntil(seq_num, tid);
706 }
707
708 template<class Impl>
709 bool
710 DefaultFetch<Impl>::checkStall(unsigned tid) const
711 {
712 bool ret_val = false;
713
714 if (cpu->contextSwitch) {
715 DPRINTF(Fetch,"[tid:%i]: Stalling for a context switch.\n",tid);
716 ret_val = true;
717 } else if (stalls[tid].decode) {
718 DPRINTF(Fetch,"[tid:%i]: Stall from Decode stage detected.\n",tid);
719 ret_val = true;
720 } else if (stalls[tid].rename) {
721 DPRINTF(Fetch,"[tid:%i]: Stall from Rename stage detected.\n",tid);
722 ret_val = true;
723 } else if (stalls[tid].iew) {
724 DPRINTF(Fetch,"[tid:%i]: Stall from IEW stage detected.\n",tid);
725 ret_val = true;
726 } else if (stalls[tid].commit) {
727 DPRINTF(Fetch,"[tid:%i]: Stall from Commit stage detected.\n",tid);
728 ret_val = true;
729 }
730
731 return ret_val;
732 }
733
734 template<class Impl>
735 typename DefaultFetch<Impl>::FetchStatus
736 DefaultFetch<Impl>::updateFetchStatus()
737 {
738 //Check Running
739 std::list<unsigned>::iterator threads = activeThreads->begin();
740 std::list<unsigned>::iterator end = activeThreads->end();
741
742 while (threads != end) {
743 unsigned tid = *threads++;
744
745 if (fetchStatus[tid] == Running ||
746 fetchStatus[tid] == Squashing ||
747 fetchStatus[tid] == IcacheAccessComplete) {
748
749 if (_status == Inactive) {
750 DPRINTF(Activity, "[tid:%i]: Activating stage.\n",tid);
751
752 if (fetchStatus[tid] == IcacheAccessComplete) {
753 DPRINTF(Activity, "[tid:%i]: Activating fetch due to cache"
754 "completion\n",tid);
755 }
756
757 cpu->activateStage(O3CPU::FetchIdx);
758 }
759
760 return Active;
761 }
762 }
763
764 // Stage is switching from active to inactive, notify CPU of it.
765 if (_status == Active) {
766 DPRINTF(Activity, "Deactivating stage.\n");
767
768 cpu->deactivateStage(O3CPU::FetchIdx);
769 }
770
771 return Inactive;
772 }
773
774 template <class Impl>
775 void
776 DefaultFetch<Impl>::squash(const Addr &new_PC, const Addr &new_NPC,
777 const Addr &new_MicroPC,
778 const InstSeqNum &seq_num, unsigned tid)
779 {
780 DPRINTF(Fetch, "[tid:%u]: Squash from commit.\n",tid);
781
782 doSquash(new_PC, new_NPC, new_MicroPC, tid);
783
784 // Tell the CPU to remove any instructions that are not in the ROB.
785 cpu->removeInstsNotInROB(tid);
786 }
787
788 template <class Impl>
789 void
790 DefaultFetch<Impl>::tick()
791 {
792 std::list<unsigned>::iterator threads = activeThreads->begin();
793 std::list<unsigned>::iterator end = activeThreads->end();
794 bool status_change = false;
795
796 wroteToTimeBuffer = false;
797
798 while (threads != end) {
799 unsigned tid = *threads++;
800
801 // Check the signals for each thread to determine the proper status
802 // for each thread.
803 bool updated_status = checkSignalsAndUpdate(tid);
804 status_change = status_change || updated_status;
805 }
806
807 DPRINTF(Fetch, "Running stage.\n");
808
809 // Reset the number of the instruction we're fetching.
810 numInst = 0;
811
812 #if FULL_SYSTEM
813 if (fromCommit->commitInfo[0].interruptPending) {
814 interruptPending = true;
815 }
816
817 if (fromCommit->commitInfo[0].clearInterrupt) {
818 interruptPending = false;
819 }
820 #endif
821
822 for (threadFetched = 0; threadFetched < numFetchingThreads;
823 threadFetched++) {
824 // Fetch each of the actively fetching threads.
825 fetch(status_change);
826 }
827
828 // Record number of instructions fetched this cycle for distribution.
829 fetchNisnDist.sample(numInst);
830
831 if (status_change) {
832 // Change the fetch stage status if there was a status change.
833 _status = updateFetchStatus();
834 }
835
836 // If there was activity this cycle, inform the CPU of it.
837 if (wroteToTimeBuffer || cpu->contextSwitch) {
838 DPRINTF(Activity, "Activity this cycle.\n");
839
840 cpu->activityThisCycle();
841 }
842 }
843
844 template <class Impl>
845 bool
846 DefaultFetch<Impl>::checkSignalsAndUpdate(unsigned tid)
847 {
848 // Update the per thread stall statuses.
849 if (fromDecode->decodeBlock[tid]) {
850 stalls[tid].decode = true;
851 }
852
853 if (fromDecode->decodeUnblock[tid]) {
854 assert(stalls[tid].decode);
855 assert(!fromDecode->decodeBlock[tid]);
856 stalls[tid].decode = false;
857 }
858
859 if (fromRename->renameBlock[tid]) {
860 stalls[tid].rename = true;
861 }
862
863 if (fromRename->renameUnblock[tid]) {
864 assert(stalls[tid].rename);
865 assert(!fromRename->renameBlock[tid]);
866 stalls[tid].rename = false;
867 }
868
869 if (fromIEW->iewBlock[tid]) {
870 stalls[tid].iew = true;
871 }
872
873 if (fromIEW->iewUnblock[tid]) {
874 assert(stalls[tid].iew);
875 assert(!fromIEW->iewBlock[tid]);
876 stalls[tid].iew = false;
877 }
878
879 if (fromCommit->commitBlock[tid]) {
880 stalls[tid].commit = true;
881 }
882
883 if (fromCommit->commitUnblock[tid]) {
884 assert(stalls[tid].commit);
885 assert(!fromCommit->commitBlock[tid]);
886 stalls[tid].commit = false;
887 }
888
889 // Check squash signals from commit.
890 if (fromCommit->commitInfo[tid].squash) {
891
892 DPRINTF(Fetch, "[tid:%u]: Squashing instructions due to squash "
893 "from commit.\n",tid);
894 // In any case, squash.
895 squash(fromCommit->commitInfo[tid].nextPC,
896 fromCommit->commitInfo[tid].nextNPC,
897 fromCommit->commitInfo[tid].nextMicroPC,
898 fromCommit->commitInfo[tid].doneSeqNum,
899 tid);
900
901 // Also check if there's a mispredict that happened.
902 if (fromCommit->commitInfo[tid].branchMispredict) {
903 branchPred.squash(fromCommit->commitInfo[tid].doneSeqNum,
904 fromCommit->commitInfo[tid].nextPC,
905 fromCommit->commitInfo[tid].branchTaken,
906 tid);
907 } else {
908 branchPred.squash(fromCommit->commitInfo[tid].doneSeqNum,
909 tid);
910 }
911
912 return true;
913 } else if (fromCommit->commitInfo[tid].doneSeqNum) {
914 // Update the branch predictor if it wasn't a squashed instruction
915 // that was broadcasted.
916 branchPred.update(fromCommit->commitInfo[tid].doneSeqNum, tid);
917 }
918
919 // Check ROB squash signals from commit.
920 if (fromCommit->commitInfo[tid].robSquashing) {
921 DPRINTF(Fetch, "[tid:%u]: ROB is still squashing.\n", tid);
922
923 // Continue to squash.
924 fetchStatus[tid] = Squashing;
925
926 return true;
927 }
928
929 // Check squash signals from decode.
930 if (fromDecode->decodeInfo[tid].squash) {
931 DPRINTF(Fetch, "[tid:%u]: Squashing instructions due to squash "
932 "from decode.\n",tid);
933
934 // Update the branch predictor.
935 if (fromDecode->decodeInfo[tid].branchMispredict) {
936 branchPred.squash(fromDecode->decodeInfo[tid].doneSeqNum,
937 fromDecode->decodeInfo[tid].nextPC,
938 fromDecode->decodeInfo[tid].branchTaken,
939 tid);
940 } else {
941 branchPred.squash(fromDecode->decodeInfo[tid].doneSeqNum,
942 tid);
943 }
944
945 if (fetchStatus[tid] != Squashing) {
946
947 DPRINTF(Fetch, "Squashing from decode with PC = %#x, NPC = %#x\n",
948 fromDecode->decodeInfo[tid].nextPC,
949 fromDecode->decodeInfo[tid].nextNPC);
950 // Squash unless we're already squashing
951 squashFromDecode(fromDecode->decodeInfo[tid].nextPC,
952 fromDecode->decodeInfo[tid].nextNPC,
953 fromDecode->decodeInfo[tid].nextMicroPC,
954 fromDecode->decodeInfo[tid].doneSeqNum,
955 tid);
956
957 return true;
958 }
959 }
960
961 if (checkStall(tid) &&
962 fetchStatus[tid] != IcacheWaitResponse &&
963 fetchStatus[tid] != IcacheWaitRetry) {
964 DPRINTF(Fetch, "[tid:%i]: Setting to blocked\n",tid);
965
966 fetchStatus[tid] = Blocked;
967
968 return true;
969 }
970
971 if (fetchStatus[tid] == Blocked ||
972 fetchStatus[tid] == Squashing) {
973 // Switch status to running if fetch isn't being told to block or
974 // squash this cycle.
975 DPRINTF(Fetch, "[tid:%i]: Done squashing, switching to running.\n",
976 tid);
977
978 fetchStatus[tid] = Running;
979
980 return true;
981 }
982
983 // If we've reached this point, we have not gotten any signals that
984 // cause fetch to change its status. Fetch remains the same as before.
985 return false;
986 }
987
988 template<class Impl>
989 void
990 DefaultFetch<Impl>::fetch(bool &status_change)
991 {
992 //////////////////////////////////////////
993 // Start actual fetch
994 //////////////////////////////////////////
995 int tid = getFetchingThread(fetchPolicy);
996
997 if (tid == -1 || drainPending) {
998 DPRINTF(Fetch,"There are no more threads available to fetch from.\n");
999
1000 // Breaks looping condition in tick()
1001 threadFetched = numFetchingThreads;
1002 return;
1003 }
1004
1005 DPRINTF(Fetch, "Attempting to fetch from [tid:%i]\n", tid);
1006
1007 // The current PC.
1008 Addr fetch_PC = PC[tid];
1009 Addr fetch_NPC = nextPC[tid];
1010 Addr fetch_MicroPC = microPC[tid];
1011
1012 // Fault code for memory access.
1013 Fault fault = NoFault;
1014
1015 // If returning from the delay of a cache miss, then update the status
1016 // to running, otherwise do the cache access. Possibly move this up
1017 // to tick() function.
1018 if (fetchStatus[tid] == IcacheAccessComplete) {
1019 DPRINTF(Fetch, "[tid:%i]: Icache miss is complete.\n",
1020 tid);
1021
1022 fetchStatus[tid] = Running;
1023 status_change = true;
1024 } else if (fetchStatus[tid] == Running) {
1025 DPRINTF(Fetch, "[tid:%i]: Attempting to translate and read "
1026 "instruction, starting at PC %08p.\n",
1027 tid, fetch_PC);
1028
1029 bool fetch_success = fetchCacheLine(fetch_PC, fault, tid);
1030 if (!fetch_success) {
1031 if (cacheBlocked) {
1032 ++icacheStallCycles;
1033 } else {
1034 ++fetchMiscStallCycles;
1035 }
1036 return;
1037 }
1038 } else {
1039 if (fetchStatus[tid] == Idle) {
1040 ++fetchIdleCycles;
1041 DPRINTF(Fetch, "[tid:%i]: Fetch is idle!\n", tid);
1042 } else if (fetchStatus[tid] == Blocked) {
1043 ++fetchBlockedCycles;
1044 DPRINTF(Fetch, "[tid:%i]: Fetch is blocked!\n", tid);
1045 } else if (fetchStatus[tid] == Squashing) {
1046 ++fetchSquashCycles;
1047 DPRINTF(Fetch, "[tid:%i]: Fetch is squashing!\n", tid);
1048 } else if (fetchStatus[tid] == IcacheWaitResponse) {
1049 ++icacheStallCycles;
1050 DPRINTF(Fetch, "[tid:%i]: Fetch is waiting cache response!\n", tid);
1051 }
1052
1053 // Status is Idle, Squashing, Blocked, or IcacheWaitResponse, so
1054 // fetch should do nothing.
1055 return;
1056 }
1057
1058 ++fetchCycles;
1059
1060 // If we had a stall due to an icache miss, then return.
1061 if (fetchStatus[tid] == IcacheWaitResponse) {
1062 ++icacheStallCycles;
1063 status_change = true;
1064 return;
1065 }
1066
1067 Addr next_PC = fetch_PC;
1068 Addr next_NPC = fetch_NPC;
1069 Addr next_MicroPC = fetch_MicroPC;
1070
1071 InstSeqNum inst_seq;
1072 MachInst inst;
1073 ExtMachInst ext_inst;
1074 // @todo: Fix this hack.
1075 unsigned offset = (fetch_PC & cacheBlkMask) & ~3;
1076
1077 StaticInstPtr staticInst = NULL;
1078 StaticInstPtr macroop = NULL;
1079
1080 if (fault == NoFault) {
1081 // If the read of the first instruction was successful, then grab the
1082 // instructions from the rest of the cache line and put them into the
1083 // queue heading to decode.
1084
1085 DPRINTF(Fetch, "[tid:%i]: Adding instructions to queue to "
1086 "decode.\n",tid);
1087
1088 // Need to keep track of whether or not a predicted branch
1089 // ended this fetch block.
1090 bool predicted_branch = false;
1091
1092 while (offset < cacheBlkSize &&
1093 numInst < fetchWidth &&
1094 !predicted_branch) {
1095
1096 // If we're branching after this instruction, quite fetching
1097 // from the same block then.
1098 predicted_branch =
1099 (fetch_PC + sizeof(TheISA::MachInst) != fetch_NPC);
1100 if (predicted_branch) {
1101 DPRINTF(Fetch, "Branch detected with PC = %#x, NPC = %#x\n",
1102 fetch_PC, fetch_NPC);
1103 }
1104
1105 // Make sure this is a valid index.
1106 assert(offset <= cacheBlkSize - instSize);
1107
1108 if (!macroop) {
1109 // Get the instruction from the array of the cache line.
1110 inst = TheISA::gtoh(*reinterpret_cast<TheISA::MachInst *>
1111 (&cacheData[tid][offset]));
1112
1113 predecoder.setTC(cpu->thread[tid]->getTC());
1114 predecoder.moreBytes(fetch_PC, fetch_PC, inst);
1115
1116 ext_inst = predecoder.getExtMachInst();
1117 staticInst = StaticInstPtr(ext_inst, fetch_PC);
1118 if (staticInst->isMacroop())
1119 macroop = staticInst;
1120 }
1121 do {
1122 if (macroop) {
1123 staticInst = macroop->fetchMicroop(fetch_MicroPC);
1124 if (staticInst->isLastMicroop())
1125 macroop = NULL;
1126 }
1127
1128 // Get a sequence number.
1129 inst_seq = cpu->getAndIncrementInstSeq();
1130
1131 // Create a new DynInst from the instruction fetched.
1132 DynInstPtr instruction = new DynInst(staticInst,
1133 fetch_PC, fetch_NPC, fetch_MicroPC,
1134 next_PC, next_NPC, next_MicroPC,
1135 inst_seq, cpu);
1136 instruction->setTid(tid);
1137
1138 instruction->setASID(tid);
1139
1140 instruction->setThreadState(cpu->thread[tid]);
1141
1142 DPRINTF(Fetch, "[tid:%i]: Instruction PC %#x created "
1143 "[sn:%lli]\n",
1144 tid, instruction->readPC(), inst_seq);
1145
1146 //DPRINTF(Fetch, "[tid:%i]: MachInst is %#x\n", tid, ext_inst);
1147
1148 DPRINTF(Fetch, "[tid:%i]: Instruction is: %s\n",
1149 tid, instruction->staticInst->disassemble(fetch_PC));
1150
1151 #if TRACING_ON
1152 instruction->traceData =
1153 cpu->getTracer()->getInstRecord(curTick, cpu->tcBase(tid),
1154 instruction->staticInst, instruction->readPC());
1155 #else
1156 instruction->traceData = NULL;
1157 #endif
1158
1159 ///FIXME This needs to be more robust in dealing with delay slots
1160 predicted_branch |=
1161 lookupAndUpdateNextPC(instruction, next_PC, next_NPC, next_MicroPC);
1162
1163 // Add instruction to the CPU's list of instructions.
1164 instruction->setInstListIt(cpu->addInst(instruction));
1165
1166 // Write the instruction to the first slot in the queue
1167 // that heads to decode.
1168 toDecode->insts[numInst] = instruction;
1169
1170 toDecode->size++;
1171
1172 // Increment stat of fetched instructions.
1173 ++fetchedInsts;
1174
1175 // Move to the next instruction, unless we have a branch.
1176 fetch_PC = next_PC;
1177 fetch_NPC = next_NPC;
1178 fetch_MicroPC = next_MicroPC;
1179
1180 if (instruction->isQuiesce()) {
1181 DPRINTF(Fetch, "Quiesce instruction encountered, halting fetch!",
1182 curTick);
1183 fetchStatus[tid] = QuiescePending;
1184 ++numInst;
1185 status_change = true;
1186 break;
1187 }
1188
1189 ++numInst;
1190 } while (staticInst->isMicroop() &&
1191 !staticInst->isLastMicroop() &&
1192 numInst < fetchWidth);
1193 offset += instSize;
1194 }
1195
1196 if (predicted_branch) {
1197 DPRINTF(Fetch, "[tid:%i]: Done fetching, predicted branch "
1198 "instruction encountered.\n", tid);
1199 } else if (numInst >= fetchWidth) {
1200 DPRINTF(Fetch, "[tid:%i]: Done fetching, reached fetch bandwidth "
1201 "for this cycle.\n", tid);
1202 } else if (offset >= cacheBlkSize) {
1203 DPRINTF(Fetch, "[tid:%i]: Done fetching, reached the end of cache "
1204 "block.\n", tid);
1205 }
1206 }
1207
1208 if (numInst > 0) {
1209 wroteToTimeBuffer = true;
1210 }
1211
1212 // Now that fetching is completed, update the PC to signify what the next
1213 // cycle will be.
1214 if (fault == NoFault) {
1215 PC[tid] = next_PC;
1216 nextPC[tid] = next_NPC;
1217 microPC[tid] = next_MicroPC;
1218 DPRINTF(Fetch, "[tid:%i]: Setting PC to %08p.\n", tid, next_PC);
1219 } else {
1220 // We shouldn't be in an icache miss and also have a fault (an ITB
1221 // miss)
1222 if (fetchStatus[tid] == IcacheWaitResponse) {
1223 panic("Fetch should have exited prior to this!");
1224 }
1225
1226 // Send the fault to commit. This thread will not do anything
1227 // until commit handles the fault. The only other way it can
1228 // wake up is if a squash comes along and changes the PC.
1229 #if FULL_SYSTEM
1230 assert(numInst < fetchWidth);
1231 // Get a sequence number.
1232 inst_seq = cpu->getAndIncrementInstSeq();
1233 // We will use a nop in order to carry the fault.
1234 ext_inst = TheISA::NoopMachInst;
1235
1236 // Create a new DynInst from the dummy nop.
1237 DynInstPtr instruction = new DynInst(ext_inst,
1238 fetch_PC, fetch_NPC, fetch_MicroPC,
1239 next_PC, next_NPC, next_MicroPC,
1240 inst_seq, cpu);
1241 instruction->setPredTarg(next_NPC, next_NPC + instSize, 0);
1242 instruction->setTid(tid);
1243
1244 instruction->setASID(tid);
1245
1246 instruction->setThreadState(cpu->thread[tid]);
1247
1248 instruction->traceData = NULL;
1249
1250 instruction->setInstListIt(cpu->addInst(instruction));
1251
1252 instruction->fault = fault;
1253
1254 toDecode->insts[numInst] = instruction;
1255 toDecode->size++;
1256
1257 DPRINTF(Fetch, "[tid:%i]: Blocked, need to handle the trap.\n",tid);
1258
1259 fetchStatus[tid] = TrapPending;
1260 status_change = true;
1261 #else // !FULL_SYSTEM
1262 fetchStatus[tid] = TrapPending;
1263 status_change = true;
1264
1265 #endif // FULL_SYSTEM
1266 DPRINTF(Fetch, "[tid:%i]: fault (%s) detected @ PC %08p",
1267 tid, fault->name(), PC[tid]);
1268 }
1269 }
1270
1271 template<class Impl>
1272 void
1273 DefaultFetch<Impl>::recvRetry()
1274 {
1275 if (retryPkt != NULL) {
1276 assert(cacheBlocked);
1277 assert(retryTid != -1);
1278 assert(fetchStatus[retryTid] == IcacheWaitRetry);
1279
1280 if (icachePort->sendTiming(retryPkt)) {
1281 fetchStatus[retryTid] = IcacheWaitResponse;
1282 retryPkt = NULL;
1283 retryTid = -1;
1284 cacheBlocked = false;
1285 }
1286 } else {
1287 assert(retryTid == -1);
1288 // Access has been squashed since it was sent out. Just clear
1289 // the cache being blocked.
1290 cacheBlocked = false;
1291 }
1292 }
1293
1294 ///////////////////////////////////////
1295 // //
1296 // SMT FETCH POLICY MAINTAINED HERE //
1297 // //
1298 ///////////////////////////////////////
1299 template<class Impl>
1300 int
1301 DefaultFetch<Impl>::getFetchingThread(FetchPriority &fetch_priority)
1302 {
1303 if (numThreads > 1) {
1304 switch (fetch_priority) {
1305
1306 case SingleThread:
1307 return 0;
1308
1309 case RoundRobin:
1310 return roundRobin();
1311
1312 case IQ:
1313 return iqCount();
1314
1315 case LSQ:
1316 return lsqCount();
1317
1318 case Branch:
1319 return branchCount();
1320
1321 default:
1322 return -1;
1323 }
1324 } else {
1325 std::list<unsigned>::iterator thread = activeThreads->begin();
1326 assert(thread != activeThreads->end());
1327 int tid = *thread;
1328
1329 if (fetchStatus[tid] == Running ||
1330 fetchStatus[tid] == IcacheAccessComplete ||
1331 fetchStatus[tid] == Idle) {
1332 return tid;
1333 } else {
1334 return -1;
1335 }
1336 }
1337
1338 }
1339
1340
1341 template<class Impl>
1342 int
1343 DefaultFetch<Impl>::roundRobin()
1344 {
1345 std::list<unsigned>::iterator pri_iter = priorityList.begin();
1346 std::list<unsigned>::iterator end = priorityList.end();
1347
1348 int high_pri;
1349
1350 while (pri_iter != end) {
1351 high_pri = *pri_iter;
1352
1353 assert(high_pri <= numThreads);
1354
1355 if (fetchStatus[high_pri] == Running ||
1356 fetchStatus[high_pri] == IcacheAccessComplete ||
1357 fetchStatus[high_pri] == Idle) {
1358
1359 priorityList.erase(pri_iter);
1360 priorityList.push_back(high_pri);
1361
1362 return high_pri;
1363 }
1364
1365 pri_iter++;
1366 }
1367
1368 return -1;
1369 }
1370
1371 template<class Impl>
1372 int
1373 DefaultFetch<Impl>::iqCount()
1374 {
1375 std::priority_queue<unsigned> PQ;
1376
1377 std::list<unsigned>::iterator threads = activeThreads->begin();
1378 std::list<unsigned>::iterator end = activeThreads->end();
1379
1380 while (threads != end) {
1381 unsigned tid = *threads++;
1382
1383 PQ.push(fromIEW->iewInfo[tid].iqCount);
1384 }
1385
1386 while (!PQ.empty()) {
1387
1388 unsigned high_pri = PQ.top();
1389
1390 if (fetchStatus[high_pri] == Running ||
1391 fetchStatus[high_pri] == IcacheAccessComplete ||
1392 fetchStatus[high_pri] == Idle)
1393 return high_pri;
1394 else
1395 PQ.pop();
1396
1397 }
1398
1399 return -1;
1400 }
1401
1402 template<class Impl>
1403 int
1404 DefaultFetch<Impl>::lsqCount()
1405 {
1406 std::priority_queue<unsigned> PQ;
1407
1408 std::list<unsigned>::iterator threads = activeThreads->begin();
1409 std::list<unsigned>::iterator end = activeThreads->end();
1410
1411 while (threads != end) {
1412 unsigned tid = *threads++;
1413
1414 PQ.push(fromIEW->iewInfo[tid].ldstqCount);
1415 }
1416
1417 while (!PQ.empty()) {
1418
1419 unsigned high_pri = PQ.top();
1420
1421 if (fetchStatus[high_pri] == Running ||
1422 fetchStatus[high_pri] == IcacheAccessComplete ||
1423 fetchStatus[high_pri] == Idle)
1424 return high_pri;
1425 else
1426 PQ.pop();
1427
1428 }
1429
1430 return -1;
1431 }
1432
1433 template<class Impl>
1434 int
1435 DefaultFetch<Impl>::branchCount()
1436 {
1437 std::list<unsigned>::iterator thread = activeThreads->begin();
1438 assert(thread != activeThreads->end());
1439 unsigned tid = *thread;
1440
1441 panic("Branch Count Fetch policy unimplemented\n");
1442 return 0 * tid;
1443 }