2 * Copyright (c) 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.
31 #include "arch/faults.hh"
32 #include "arch/isa_traits.hh"
33 #include "base/statistics.hh"
34 #include "cpu/thread_context.hh"
35 #include "cpu/exetrace.hh"
36 #include "cpu/ozone/front_end.hh"
37 #include "mem/packet.hh"
38 #include "mem/request.hh"
40 using namespace TheISA;
44 FrontEnd<Impl>::IcachePort::recvAtomic(PacketPtr pkt)
46 panic("FrontEnd doesn't expect recvAtomic callback!");
52 FrontEnd<Impl>::IcachePort::recvFunctional(PacketPtr pkt)
54 panic("FrontEnd doesn't expect recvFunctional callback!");
59 FrontEnd<Impl>::IcachePort::recvStatusChange(Status status)
61 if (status == RangeChange)
64 panic("FrontEnd doesn't expect recvStatusChange callback!");
69 FrontEnd<Impl>::IcachePort::recvTiming(Packet *pkt)
71 fe->processCacheCompletion(pkt);
77 FrontEnd<Impl>::IcachePort::recvRetry()
83 FrontEnd<Impl>::FrontEnd(Params *params)
87 maxInstBufferSize(params->maxInstBufferSize),
88 width(params->frontEndWidth),
89 freeRegs(params->numPhysicalRegs),
90 numPhysRegs(params->numPhysicalRegs),
92 interruptPending(false)
99 // Size of cache block.
102 assert(isPowerOf2(cacheBlkSize));
104 // Create mask to get rid of offset bits.
105 cacheBlkMask = (cacheBlkSize - 1);
107 // Create space to store a cache line.
108 cacheData = new uint8_t[cacheBlkSize];
110 fetchCacheLineNextCycle = true;
112 cacheBlkValid = cacheBlocked = false;
116 fetchFault = NoFault;
119 template <class Impl>
121 FrontEnd<Impl>::name() const
123 return cpu->name() + ".frontend";
126 template <class Impl>
128 FrontEnd<Impl>::setCommBuffer(TimeBuffer<CommStruct> *_comm)
131 // @todo: Hardcoded for now. Allow this to be set by a latency.
132 fromCommit = comm->getWire(-1);
135 template <class Impl>
137 FrontEnd<Impl>::setTC(ThreadContext *tc_ptr)
142 template <class Impl>
144 FrontEnd<Impl>::regStats()
147 .name(name() + ".icacheStallCycles")
148 .desc("Number of cycles fetch is stalled on an Icache miss")
149 .prereq(icacheStallCycles);
152 .name(name() + ".fetchedInsts")
153 .desc("Number of instructions fetch has processed")
154 .prereq(fetchedInsts);
157 .name(name() + ".fetchedBranches")
158 .desc("Number of fetched branches")
159 .prereq(fetchedBranches);
162 .name(name() + ".predictedBranches")
163 .desc("Number of branches that fetch has predicted taken")
164 .prereq(predictedBranches);
167 .name(name() + ".fetchCycles")
168 .desc("Number of cycles fetch has run and was not squashing or"
170 .prereq(fetchCycles);
173 .name(name() + ".fetchIdleCycles")
174 .desc("Number of cycles fetch was idle")
175 .prereq(fetchIdleCycles);
178 .name(name() + ".fetchSquashCycles")
179 .desc("Number of cycles fetch has spent squashing")
180 .prereq(fetchSquashCycles);
183 .name(name() + ".fetchBlockedCycles")
184 .desc("Number of cycles fetch has spent blocked")
185 .prereq(fetchBlockedCycles);
188 .name(name() + ".fetchedCacheLines")
189 .desc("Number of cache lines fetched")
190 .prereq(fetchedCacheLines);
193 .name(name() + ".fetchIcacheSquashes")
194 .desc("Number of outstanding Icache misses that were squashed")
195 .prereq(fetchIcacheSquashes);
198 .init(/* base value */ 0,
199 /* last value */ width,
201 .name(name() + ".rateDist")
202 .desc("Number of instructions fetched each cycle (Total)")
206 .name(name() + ".idleRate")
207 .desc("Percent of cycles fetch was idle")
209 idleRate = fetchIdleCycles * 100 / cpu->numCycles;
212 .name(name() + ".branchRate")
213 .desc("Number of branch fetches per cycle")
214 .flags(Stats::total);
215 branchRate = fetchedBranches / cpu->numCycles;
218 .name(name() + ".rate")
219 .desc("Number of inst fetches per cycle")
220 .flags(Stats::total);
221 fetchRate = fetchedInsts / cpu->numCycles;
224 .name(name() + ".IFQ:count")
225 .desc("cumulative IFQ occupancy")
229 .name(name() + ".IFQ:fullCount")
230 .desc("cumulative IFQ full count")
235 .name(name() + ".IFQ:occupancy")
236 .desc("avg IFQ occupancy (inst's)")
238 IFQOccupancy = IFQCount / cpu->numCycles;
241 .name(name() + ".IFQ:latency")
242 .desc("avg IFQ occupant latency (cycle's)")
247 .name(name() + ".IFQ:fullRate")
248 .desc("fraction of time (cycles) IFQ was full")
249 .flags(Stats::total);
251 IFQFullRate = IFQFcount * Stats::constant(100) / cpu->numCycles;
254 .name(name() + ".DIS:count")
255 .desc("cumulative count of dispatched insts")
259 dispatchedSerializing
260 .name(name() + ".DIS:serializingInsts")
261 .desc("count of serializing insts dispatched")
265 dispatchedTempSerializing
266 .name(name() + ".DIS:tempSerializingInsts")
267 .desc("count of temporary serializing insts dispatched")
271 dispatchSerializeStallCycles
272 .name(name() + ".DIS:serializeStallCycles")
273 .desc("count of cycles dispatch stalled for serializing inst")
278 .name(name() + ".DIS:rate")
279 .desc("dispatched insts per cycle")
282 dispatchRate = dispatchCountStat / cpu->numCycles;
285 .name(name() + ".REG:int:full")
286 .desc("number of cycles where there were no INT registers")
290 .name(name() + ".REG:fp:full")
291 .desc("number of cycles where there were no FP registers")
293 IFQLatency = IFQOccupancy / dispatchRate;
295 branchPred.regStats();
298 template <class Impl>
300 FrontEnd<Impl>::tick()
305 // @todo: Maybe I want to just have direct communication...
306 if (fromCommit->doneSeqNum) {
307 branchPred.update(fromCommit->doneSeqNum, 0);
310 IFQCount += instBufferSize;
311 IFQFcount += instBufferSize == maxInstBufferSize;
314 if (status == IcacheAccessComplete) {
315 cacheBlkValid = true;
319 status = SerializeBlocked;
321 status = RenameBlocked;
323 } else if (status == IcacheWaitResponse || status == IcacheWaitRetry) {
324 DPRINTF(FE, "Still in Icache wait.\n");
329 if (status == RenameBlocked || status == SerializeBlocked ||
330 status == TrapPending || status == BEBlocked) {
331 // Will cause a one cycle bubble between changing state and
333 DPRINTF(FE, "In blocked status.\n");
335 fetchBlockedCycles++;
337 if (status == SerializeBlocked) {
338 dispatchSerializeStallCycles++;
342 } else if (status == QuiescePending) {
343 DPRINTF(FE, "Waiting for quiesce to execute or get squashed.\n");
345 } else if (status != IcacheAccessComplete) {
346 if (fetchCacheLineNextCycle) {
347 Fault fault = fetchCacheLine();
348 if (fault != NoFault) {
353 fetchCacheLineNextCycle = false;
355 // If miss, stall until it returns.
356 if (status == IcacheWaitResponse || status == IcacheWaitRetry) {
357 // Tell CPU to not tick me for now.
366 // Otherwise loop and process instructions.
367 // One way to hack infinite width is to set width and maxInstBufferSize
368 // both really high. Inelegant, but probably will work.
369 while (num_inst < width &&
370 instBufferSize < maxInstBufferSize) {
371 // Get instruction from cache line.
372 DynInstPtr inst = getInstFromCacheline();
375 // PC is no longer in the cache line, end fetch.
376 // Might want to check this at the end of the cycle so that
377 // there's no cycle lost to checking for a new cache line.
378 DPRINTF(FE, "Need to get new cache line\n");
379 fetchCacheLineNextCycle = true;
385 if (status == SerializeBlocked) {
389 // Possibly push into a time buffer that estimates the front end
391 instBuffer.push_back(inst);
396 if (inst->isQuiesce()) {
397 warn("%lli: Quiesce instruction encountered, halting fetch!", curTick);
398 status = QuiescePending;
403 if (inst->predTaken()) {
404 // Start over with tick?
406 } else if (freeRegs <= 0) {
407 DPRINTF(FE, "Ran out of free registers to rename to!\n");
408 status = RenameBlocked;
410 } else if (serializeNext) {
415 fetchNisnDist.sample(num_inst);
418 DPRINTF(FE, "Num insts processed: %i, Inst Buffer size: %i, Free "
419 "Regs %i\n", num_inst, instBufferSize, freeRegs);
422 template <class Impl>
424 FrontEnd<Impl>::fetchCacheLine()
426 // Read a cache line, based on the current PC.
428 // Flag to say whether or not address is physical addr.
429 unsigned flags = cpu->inPalMode(PC) ? PHYSICAL : 0;
432 #endif // FULL_SYSTEM
433 Fault fault = NoFault;
435 if (interruptPending && flags == 0) {
439 // Align the fetch PC so it's at the start of a cache block.
440 Addr fetch_PC = icacheBlockAlignPC(PC);
442 DPRINTF(FE, "Fetching cache line starting at %#x.\n", fetch_PC);
444 // Setup the memReq to do a read of the first isntruction's address.
445 // Set the appropriate read size and flags as well.
446 memReq = new Request(0, fetch_PC, cacheBlkSize, flags,
447 fetch_PC, cpu->readCpuId(), 0);
449 // Translate the instruction request.
450 fault = cpu->translateInstReq(memReq, thread);
452 // Now do the timing access to see whether or not the instruction
453 // exists within the cache.
454 if (fault == NoFault) {
456 if (cpu->system->memctrl->badaddr(memReq->paddr) ||
457 memReq->flags & UNCACHEABLE) {
458 DPRINTF(FE, "Fetch: Bad address %#x (hopefully on a "
459 "misspeculating path!",
461 return TheISA::genMachineCheckFault();
465 // Build packet here.
466 PacketPtr data_pkt = new Packet(memReq,
467 Packet::ReadReq, Packet::Broadcast);
468 data_pkt->dataStatic(cacheData);
470 if (!icachePort.sendTiming(data_pkt)) {
471 assert(retryPkt == NULL);
472 DPRINTF(Fetch, "Out of MSHRs!\n");
473 status = IcacheWaitRetry;
479 status = IcacheWaitResponse;
482 // Note that this will set the cache block PC a bit earlier than it should
484 cacheBlkPC = fetch_PC;
488 DPRINTF(FE, "Done fetching cache line.\n");
493 template <class Impl>
495 FrontEnd<Impl>::processInst(DynInstPtr &inst)
497 if (processBarriers(inst)) {
501 Addr inst_PC = inst->readPC();
503 if (!inst->isControl()) {
504 inst->setPredTarg(inst->readNextPC());
507 if (branchPred.predict(inst, inst_PC, inst->threadNumber)) {
512 Addr next_PC = inst->readPredTarg();
514 DPRINTF(FE, "[sn:%lli] Predicted and processed inst PC %#x, next PC "
515 "%#x\n", inst->seqNum, inst_PC, next_PC);
517 // inst->setNextPC(next_PC);
519 // Not sure where I should set this
525 template <class Impl>
527 FrontEnd<Impl>::processBarriers(DynInstPtr &inst)
530 inst->setSerializeBefore();
531 serializeNext = false;
532 } else if (!inst->isSerializing() &&
533 !inst->isIprAccess() &&
534 !inst->isStoreConditional()) {
538 if ((inst->isIprAccess() || inst->isSerializeBefore()) &&
539 !inst->isSerializeHandled()) {
540 DPRINTF(FE, "Serialize before instruction encountered.\n");
542 if (!inst->isTempSerializeBefore()) {
543 dispatchedSerializing++;
544 inst->setSerializeHandled();
546 dispatchedTempSerializing++;
549 // Change status over to SerializeBlocked so that other stages know
550 // what this is blocked on.
551 status = SerializeBlocked;
555 } else if ((inst->isStoreConditional() || inst->isSerializeAfter())
556 && !inst->isSerializeHandled()) {
557 DPRINTF(FE, "Serialize after instruction encountered.\n");
559 inst->setSerializeHandled();
561 dispatchedSerializing++;
563 serializeNext = true;
569 template <class Impl>
571 FrontEnd<Impl>::handleFault(Fault &fault)
573 DPRINTF(FE, "Fault at fetch, telling commit\n");
575 // We're blocked on the back end until it handles this fault.
576 status = TrapPending;
578 // Get a sequence number.
579 InstSeqNum inst_seq = getAndIncrementInstSeq();
580 // We will use a nop in order to carry the fault.
581 ExtMachInst ext_inst = TheISA::NoopMachInst;
583 // Create a new DynInst from the dummy nop.
584 DynInstPtr instruction = new DynInst(ext_inst, PC,
587 instruction->setPredTarg(instruction->readNextPC());
588 // instruction->setThread(tid);
590 // instruction->setASID(tid);
592 instruction->setThreadState(thread);
594 instruction->traceData = NULL;
596 instruction->fault = fault;
597 instruction->setCanIssue();
598 instBuffer.push_back(instruction);
602 template <class Impl>
604 FrontEnd<Impl>::squash(const InstSeqNum &squash_num, const Addr &next_PC,
605 const bool is_branch, const bool branch_taken)
607 DPRINTF(FE, "Squashing from [sn:%lli], setting PC to %#x\n",
608 squash_num, next_PC);
610 if (fetchFault != NoFault)
611 fetchFault = NoFault;
613 while (!instBuffer.empty() &&
614 instBuffer.back()->seqNum > squash_num) {
615 DynInstPtr inst = instBuffer.back();
617 DPRINTF(FE, "Squashing instruction [sn:%lli] PC %#x\n",
618 inst->seqNum, inst->readPC());
620 inst->clearDependents();
622 instBuffer.pop_back();
625 freeRegs+= inst->numDestRegs();
628 // Copy over rename table from the back end.
629 renameTable.copyFrom(backEnd->renameTable);
633 // Update BP with proper information.
635 branchPred.squash(squash_num, next_PC, branch_taken, 0);
637 branchPred.squash(squash_num, 0);
640 // Clear the icache miss if it's outstanding.
641 if (status == IcacheWaitResponse) {
642 DPRINTF(FE, "Squashing outstanding Icache access.\n");
646 if (status == SerializeBlocked) {
647 assert(barrierInst->seqNum > squash_num);
651 // Unless this squash originated from the front end, we're probably
652 // in running mode now.
653 // Actually might want to make this latency dependent.
655 fetchCacheLineNextCycle = true;
658 template <class Impl>
659 typename Impl::DynInstPtr
660 FrontEnd<Impl>::getInst()
662 if (instBufferSize == 0) {
666 DynInstPtr inst = instBuffer.front();
668 instBuffer.pop_front();
677 template <class Impl>
679 FrontEnd<Impl>::processCacheCompletion(PacketPtr pkt)
681 DPRINTF(FE, "Processing cache completion\n");
683 // Do something here.
684 if (status != IcacheWaitResponse ||
685 pkt->req != memReq ||
687 DPRINTF(FE, "Previous fetch was squashed.\n");
688 fetchIcacheSquashes++;
694 status = IcacheAccessComplete;
696 /* if (checkStall(tid)) {
697 fetchStatus[tid] = Blocked;
699 fetchStatus[tid] = IcacheMissComplete;
702 // memcpy(cacheData, memReq->data, memReq->size);
704 // Reset the completion event to NULL.
705 // memReq->completionEvent = NULL;
711 template <class Impl>
713 FrontEnd<Impl>::addFreeRegs(int num_freed)
715 if (status == RenameBlocked && freeRegs + num_freed > 0) {
719 DPRINTF(FE, "Adding %i freed registers\n", num_freed);
721 freeRegs+= num_freed;
723 // assert(freeRegs <= numPhysRegs);
724 if (freeRegs > numPhysRegs)
725 freeRegs = numPhysRegs;
728 template <class Impl>
730 FrontEnd<Impl>::recvRetry()
732 assert(cacheBlocked);
733 if (retryPkt != NULL) {
734 assert(status == IcacheWaitRetry);
736 if (icachePort.sendTiming(retryPkt)) {
737 status = IcacheWaitResponse;
739 cacheBlocked = false;
742 // Access has been squashed since it was sent out. Just clear
743 // the cache being blocked.
744 cacheBlocked = false;
749 template <class Impl>
751 FrontEnd<Impl>::updateStatus()
753 bool serialize_block = !backEnd->robEmpty() || instBufferSize;
754 bool be_block = cpu->decoupledFrontEnd ? false : backEnd->isBlocked();
755 bool ret_val = false;
757 if (status == SerializeBlocked && !serialize_block) {
758 status = SerializeComplete;
762 if (status == BEBlocked && !be_block) {
764 status = SerializeBlocked;
773 template <class Impl>
775 FrontEnd<Impl>::checkBE()
777 bool be_block = cpu->decoupledFrontEnd ? false : backEnd->isBlocked();
779 if (status == Running || status == Idle) {
785 template <class Impl>
786 typename Impl::DynInstPtr
787 FrontEnd<Impl>::getInstFromCacheline()
789 if (status == SerializeComplete) {
790 DynInstPtr inst = barrierInst;
793 inst->clearSerializeBefore();
799 // @todo: Fix this magic number used here to handle word offset (and
800 // getting rid of PAL bit)
801 unsigned offset = (PC & cacheBlkMask) & ~3;
803 // PC of inst is not in this cache block
804 if (PC >= (cacheBlkPC + cacheBlkSize) || PC < cacheBlkPC || !cacheBlkValid) {
808 //////////////////////////
809 // Fetch one instruction
810 //////////////////////////
812 // Get a sequence number.
813 inst_seq = getAndIncrementInstSeq();
815 // Make sure this is a valid index.
816 assert(offset <= cacheBlkSize - sizeof(MachInst));
818 // Get the instruction from the array of the cache line.
819 inst = htog(*reinterpret_cast<MachInst *>(&cacheData[offset]));
821 ExtMachInst decode_inst = TheISA::makeExtMI(inst, PC);
823 // Create a new DynInst from the instruction fetched.
824 DynInstPtr instruction = new DynInst(decode_inst, PC, PC+sizeof(MachInst),
827 instruction->setThreadState(thread);
829 DPRINTF(FE, "Instruction [sn:%lli] created, with PC %#x\n%s\n",
830 inst_seq, instruction->readPC(),
831 instruction->staticInst->disassemble(PC));
833 instruction->traceData =
834 Trace::getInstRecord(curTick, tc, cpu,
835 instruction->staticInst,
836 instruction->readPC(), 0);
838 // Increment stat of fetched instructions.
844 template <class Impl>
846 FrontEnd<Impl>::renameInst(DynInstPtr &inst)
848 DynInstPtr src_inst = NULL;
849 int num_src_regs = inst->numSrcRegs();
850 if (num_src_regs == 0) {
853 for (int i = 0; i < num_src_regs; ++i) {
854 src_inst = renameTable[inst->srcRegIdx(i)];
856 inst->setSrcInst(src_inst, i);
858 DPRINTF(FE, "[sn:%lli]: Src reg %i is inst [sn:%lli]\n",
859 inst->seqNum, (int)inst->srcRegIdx(i), src_inst->seqNum);
861 if (src_inst->isResultReady()) {
862 DPRINTF(FE, "Reg ready.\n");
863 inst->markSrcRegReady(i);
865 DPRINTF(FE, "Adding to dependent list.\n");
866 src_inst->addDependent(inst);
871 for (int i = 0; i < inst->numDestRegs(); ++i) {
872 RegIndex idx = inst->destRegIdx(i);
874 DPRINTF(FE, "Dest reg %i is now inst [sn:%lli], was previously "
876 (int)inst->destRegIdx(i), inst->seqNum,
877 renameTable[idx]->seqNum);
879 inst->setPrevDestInst(renameTable[idx], i);
881 renameTable[idx] = inst;
886 template <class Impl>
888 FrontEnd<Impl>::wakeFromQuiesce()
890 DPRINTF(FE, "Waking up from quiesce\n");
891 // Hopefully this is safe
895 template <class Impl>
897 FrontEnd<Impl>::switchOut()
900 cpu->signalSwitched();
903 template <class Impl>
905 FrontEnd<Impl>::doSwitchOut()
914 template <class Impl>
916 FrontEnd<Impl>::takeOverFrom(ThreadContext *old_tc)
918 assert(freeRegs == numPhysRegs);
919 fetchCacheLineNextCycle = true;
921 cacheBlkValid = false;
924 // pTable = params->pTable;
926 fetchFault = NoFault;
927 serializeNext = false;
931 interruptPending = false;
934 template <class Impl>
936 FrontEnd<Impl>::dumpInsts()
938 cprintf("instBuffer size: %i\n", instBuffer.size());
940 InstBuffIt buff_it = instBuffer.begin();
942 for (int num = 0; buff_it != instBuffer.end(); num++) {
943 cprintf("Instruction:%i\nPC:%#x\n[tid:%i]\n[sn:%lli]\nIssued:%i\n"
945 num, (*buff_it)->readPC(), (*buff_it)->threadNumber,
946 (*buff_it)->seqNum, (*buff_it)->isIssued(),
947 (*buff_it)->isSquashed());