2 * Copyright (c) 2004-2005 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.
29 // @todo: Fix the instantaneous communication among all the stages within
30 // iew. There's a clear delay between issue and execute, yet backwards
31 // communication happens simultaneously.
32 // Update the statuses for each stage.
36 #include "base/timebuf.hh"
37 #include "cpu/o3/iew.hh"
40 SimpleIEW<Impl>::WritebackEvent::WritebackEvent(DynInstPtr &_inst,
41 SimpleIEW<Impl> *_iew)
42 : Event(&mainEventQueue, CPU_Tick_Pri), inst(_inst), iewStage(_iew)
44 this->setFlags(Event::AutoDelete);
49 SimpleIEW<Impl>::WritebackEvent::process()
51 DPRINTF(IEW, "IEW: WRITEBACK EVENT!!!!\n");
53 // Need to insert instruction into queue to commit
54 iewStage->instToCommit(inst);
55 // Need to execute second half of the instruction, do actual writing to
62 SimpleIEW<Impl>::WritebackEvent::description()
64 return "LSQ writeback event";
68 SimpleIEW<Impl>::SimpleIEW(Params ¶ms)
69 : // Just make this time buffer really big for now
70 issueToExecQueue(5, 5),
73 commitToIEWDelay(params.commitToIEWDelay),
74 renameToIEWDelay(params.renameToIEWDelay),
75 issueToExecuteDelay(params.issueToExecuteDelay),
76 issueReadWidth(params.issueWidth),
77 issueWidth(params.issueWidth),
78 executeWidth(params.executeWidth)
80 DPRINTF(IEW, "IEW: executeIntWidth: %i.\n", params.executeIntWidth);
86 // Setup wire to read instructions coming from issue.
87 fromIssue = issueToExecQueue.getWire(-issueToExecuteDelay);
89 // Instruction queue needs the queue between issue and execute.
90 instQueue.setIssueToExecuteQueue(&issueToExecQueue);
92 ldstQueue.setIEW(this);
97 SimpleIEW<Impl>::regStats()
102 .name(name() + ".iewIdleCycles")
103 .desc("Number of cycles IEW is idle");
106 .name(name() + ".iewSquashCycles")
107 .desc("Number of cycles IEW is squashing");
110 .name(name() + ".iewBlockCycles")
111 .desc("Number of cycles IEW is blocking");
114 .name(name() + ".iewUnblockCycles")
115 .desc("Number of cycles IEW is unblocking");
120 .name(name() + ".iewDispatchedInsts")
121 .desc("Number of instructions dispatched to IQ");
124 .name(name() + ".iewDispSquashedInsts")
125 .desc("Number of squashed instructions skipped by dispatch");
128 .name(name() + ".iewDispLoadInsts")
129 .desc("Number of dispatched load instructions");
132 .name(name() + ".iewDispStoreInsts")
133 .desc("Number of dispatched store instructions");
136 .name(name() + ".iewDispNonSpecInsts")
137 .desc("Number of dispatched non-speculative instructions");
140 .name(name() + ".iewIQFullEvents")
141 .desc("Number of times the IQ has become full, causing a stall");
144 .name(name() + ".iewExecutedInsts")
145 .desc("Number of executed instructions");
148 .name(name() + ".iewExecLoadInsts")
149 .desc("Number of load instructions executed");
152 .name(name() + ".iewExecStoreInsts")
153 .desc("Number of store instructions executed");
156 .name(name() + ".iewExecSquashedInsts")
157 .desc("Number of squashed instructions skipped in execute");
159 memOrderViolationEvents
160 .name(name() + ".memOrderViolationEvents")
161 .desc("Number of memory order violations");
163 predictedTakenIncorrect
164 .name(name() + ".predictedTakenIncorrect")
165 .desc("Number of branches that were predicted taken incorrectly");
170 SimpleIEW<Impl>::setCPU(FullCPU *cpu_ptr)
172 DPRINTF(IEW, "IEW: Setting CPU pointer.\n");
175 instQueue.setCPU(cpu_ptr);
176 ldstQueue.setCPU(cpu_ptr);
181 SimpleIEW<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
183 DPRINTF(IEW, "IEW: Setting time buffer pointer.\n");
186 // Setup wire to read information from time buffer, from commit.
187 fromCommit = timeBuffer->getWire(-commitToIEWDelay);
189 // Setup wire to write information back to previous stages.
190 toRename = timeBuffer->getWire(0);
192 // Instruction queue also needs main time buffer.
193 instQueue.setTimeBuffer(tb_ptr);
198 SimpleIEW<Impl>::setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr)
200 DPRINTF(IEW, "IEW: Setting rename queue pointer.\n");
201 renameQueue = rq_ptr;
203 // Setup wire to read information from rename queue.
204 fromRename = renameQueue->getWire(-renameToIEWDelay);
209 SimpleIEW<Impl>::setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr)
211 DPRINTF(IEW, "IEW: Setting IEW queue pointer.\n");
214 // Setup wire to write instructions to commit.
215 toCommit = iewQueue->getWire(0);
220 SimpleIEW<Impl>::setRenameMap(RenameMap *rm_ptr)
222 DPRINTF(IEW, "IEW: Setting rename map pointer.\n");
228 SimpleIEW<Impl>::squash()
230 DPRINTF(IEW, "IEW: Squashing all instructions.\n");
233 // Tell the IQ to start squashing.
236 // Tell the LDSTQ to start squashing.
237 ldstQueue.squash(fromCommit->commitInfo.doneSeqNum);
242 SimpleIEW<Impl>::squashDueToBranch(DynInstPtr &inst)
244 DPRINTF(IEW, "IEW: Squashing from a specific instruction, PC: %#x.\n",
246 // Perhaps leave the squashing up to the ROB stage to tell it when to
250 // Tell rename to squash through the time buffer.
251 toCommit->squash = true;
252 // Also send PC update information back to prior stages.
253 toCommit->squashedSeqNum = inst->seqNum;
254 toCommit->mispredPC = inst->readPC();
255 toCommit->nextPC = inst->readNextPC();
256 toCommit->branchMispredict = true;
257 // Prediction was incorrect, so send back inverse.
258 toCommit->branchTaken = inst->readNextPC() !=
259 (inst->readPC() + sizeof(MachInst));
264 SimpleIEW<Impl>::squashDueToMem(DynInstPtr &inst)
266 DPRINTF(IEW, "IEW: Squashing from a specific instruction, PC: %#x.\n",
268 // Perhaps leave the squashing up to the ROB stage to tell it when to
272 // Tell rename to squash through the time buffer.
273 toCommit->squash = true;
274 // Also send PC update information back to prior stages.
275 toCommit->squashedSeqNum = inst->seqNum;
276 toCommit->nextPC = inst->readNextPC();
281 SimpleIEW<Impl>::block()
283 DPRINTF(IEW, "IEW: Blocking.\n");
284 // Set the status to Blocked.
287 // Add the current inputs to the skid buffer so they can be
288 // reprocessed when this stage unblocks.
289 skidBuffer.push(*fromRename);
291 // Note that this stage only signals previous stages to stall when
292 // it is the cause of the stall originates at this stage. Otherwise
293 // the previous stages are expected to check all possible stall signals.
298 SimpleIEW<Impl>::unblock()
300 // Check if there's information in the skid buffer. If there is, then
301 // set status to unblocking, otherwise set it directly to running.
302 DPRINTF(IEW, "IEW: Reading instructions out of the skid "
304 // Remove the now processed instructions from the skid buffer.
307 // If there's still information in the skid buffer, then
308 // continue to tell previous stages to stall. They will be
309 // able to restart once the skid buffer is empty.
310 if (!skidBuffer.empty()) {
311 toRename->iewInfo.stall = true;
313 DPRINTF(IEW, "IEW: Stage is done unblocking.\n");
320 SimpleIEW<Impl>::wakeDependents(DynInstPtr &inst)
322 instQueue.wakeDependents(inst);
328 SimpleIEW<Impl>::instToCommit(DynInstPtr &inst)
333 template <class Impl>
335 SimpleIEW<Impl>::dispatchInsts()
337 ////////////////////////////////////////
338 // DISPATCH/ISSUE stage
339 ////////////////////////////////////////
341 //Put into its own function?
342 //Add instructions to IQ if there are any instructions there
344 // Check if there are any instructions coming from rename, and we're.
346 if (fromRename->size > 0) {
347 int insts_to_add = fromRename->size;
349 // Loop through the instructions, putting them in the instruction
351 for (int inst_num = 0; inst_num < insts_to_add; ++inst_num)
353 DynInstPtr inst = fromRename->insts[inst_num];
355 // Make sure there's a valid instruction there.
358 DPRINTF(IEW, "IEW: Issue: Adding PC %#x to IQ.\n",
361 // Be sure to mark these instructions as ready so that the
362 // commit stage can go ahead and execute them, and mark
363 // them as issued so the IQ doesn't reprocess them.
364 if (inst->isSquashed()) {
365 ++iewDispSquashedInsts;
367 } else if (instQueue.isFull()) {
368 DPRINTF(IEW, "IEW: Issue: IQ has become full.\n");
369 // Call function to start blocking.
371 // Tell previous stage to stall.
372 toRename->iewInfo.stall = true;
376 } else if (inst->isLoad()) {
377 DPRINTF(IEW, "IEW: Issue: Memory instruction "
378 "encountered, adding to LDSTQ.\n");
380 // Reserve a spot in the load store queue for this
382 ldstQueue.insertLoad(inst);
385 } else if (inst->isStore()) {
386 ldstQueue.insertStore(inst);
389 } else if (inst->isNonSpeculative()) {
390 DPRINTF(IEW, "IEW: Issue: Nonspeculative instruction "
391 "encountered, skipping.\n");
393 // Same hack as with stores.
394 inst->setCanCommit();
396 // Specificall insert it as nonspeculative.
397 instQueue.insertNonSpec(inst);
399 ++iewDispNonSpecInsts;
402 } else if (inst->isNop()) {
403 DPRINTF(IEW, "IEW: Issue: Nop instruction encountered "
408 inst->setCanCommit();
410 instQueue.advanceTail(inst);
413 } else if (inst->isExecuted()) {
414 assert(0 && "Instruction shouldn't be executed.\n");
415 DPRINTF(IEW, "IEW: Issue: Executed branch encountered, "
419 inst->setCanCommit();
421 instQueue.advanceTail(inst);
426 // If the instruction queue is not full, then add the
428 instQueue.insert(fromRename->insts[inst_num]);
430 ++iewDispatchedInsts;
435 template <class Impl>
437 SimpleIEW<Impl>::executeInsts()
439 ////////////////////////////////////////
440 //EXECUTE/WRITEBACK stage
441 ////////////////////////////////////////
443 //Put into its own function?
444 //Similarly should probably have separate execution for int vs FP.
445 // Above comment is handled by the issue queue only issuing a valid
446 // mix of int/fp instructions.
447 //Actually okay to just have one execution, buuuuuut will need
448 //somewhere that defines the execution latency of all instructions.
449 // @todo: Move to the FU pool used in the current full cpu.
452 bool fetch_redirect = false;
456 // Execute/writeback any instructions that are available.
457 for (int inst_num = 0;
458 fu_usage < executeWidth && /* Haven't exceeded available FU's. */
459 inst_num < issueWidth &&
460 fromIssue->insts[inst_num];
463 DPRINTF(IEW, "IEW: Execute: Executing instructions from IQ.\n");
465 // Get instruction from issue's queue.
466 DynInstPtr inst = fromIssue->insts[inst_num];
468 DPRINTF(IEW, "IEW: Execute: Processing PC %#x.\n", inst->readPC());
470 // Check if the instruction is squashed; if so then skip it
471 // and don't count it towards the FU usage.
472 if (inst->isSquashed()) {
473 DPRINTF(IEW, "IEW: Execute: Instruction was squashed.\n");
475 // Consider this instruction executed so that commit can go
476 // ahead and retire the instruction.
479 toCommit->insts[inst_num] = inst;
481 ++iewExecSquashedInsts;
488 // If an instruction is executed, then count it towards FU usage.
491 // Execute instruction.
492 // Note that if the instruction faults, it will be handled
493 // at the commit stage.
494 if (inst->isMemRef()) {
495 DPRINTF(IEW, "IEW: Execute: Calculating address for memory "
498 // Tell the LDSTQ to execute this instruction (if it is a load).
499 if (inst->isLoad()) {
500 ldstQueue.executeLoad(inst);
503 } else if (inst->isStore()) {
504 ldstQueue.executeStore(inst);
508 panic("IEW: Unexpected memory type!\n");
517 // First check the time slot that this instruction will write
518 // to. If there are free write ports at the time, then go ahead
519 // and write the instruction to that time. If there are not,
520 // keep looking back to see where's the first time there's a
521 // free slot. What happens if you run out of free spaces?
522 // For now naively assume that all instructions take one cycle.
523 // Otherwise would have to look into the time buffer based on the
524 // latency of the instruction.
525 (*iewQueue)[time_slot].insts[inst_slot];
526 while ((*iewQueue)[time_slot].insts[inst_slot]) {
527 if (inst_slot < issueWidth) {
534 assert(time_slot < 5);
537 // May actually have to work this out, especially with loads and stores
539 // Add finished instruction to queue to commit.
540 (*iewQueue)[time_slot].insts[inst_slot] = inst;
541 (*iewQueue)[time_slot].size++;
543 // Check if branch was correct. This check happens after the
544 // instruction is added to the queue because even if the branch
545 // is mispredicted, the branch instruction itself is still valid.
546 // Only handle this if there hasn't already been something that
547 // redirects fetch in this group of instructions.
548 if (!fetch_redirect) {
549 if (inst->mispredicted()) {
550 fetch_redirect = true;
552 DPRINTF(IEW, "IEW: Execute: Branch mispredict detected.\n");
553 DPRINTF(IEW, "IEW: Execute: Redirecting fetch to PC: %#x.\n",
556 // If incorrect, then signal the ROB that it must be squashed.
557 squashDueToBranch(inst);
559 if (inst->predTaken()) {
560 predictedTakenIncorrect++;
562 } else if (ldstQueue.violation()) {
563 fetch_redirect = true;
565 // Get the DynInst that caused the violation.
566 DynInstPtr violator = ldstQueue.getMemDepViolator();
568 DPRINTF(IEW, "IEW: LDSTQ detected a violation. Violator PC: "
569 "%#x, inst PC: %#x. Addr is: %#x.\n",
570 violator->readPC(), inst->readPC(), inst->physEffAddr);
572 // Tell the instruction queue that a violation has occured.
573 instQueue.violation(inst, violator);
576 squashDueToMem(inst);
578 ++memOrderViolationEvents;
586 SimpleIEW<Impl>::tick()
588 // Considering putting all the state-determining stuff in this section.
590 // Try to fill up issue queue with as many instructions as bandwidth
592 // Decode should try to execute as many instructions as its bandwidth
593 // will allow, as long as it is not currently blocked.
595 // Check if the stage is in a running status.
596 if (_status != Blocked && _status != Squashing) {
597 DPRINTF(IEW, "IEW: Status is not blocked, attempting to run "
601 // If it's currently unblocking, check to see if it should switch
603 if (_status == Unblocking) {
608 } else if (_status == Squashing) {
610 DPRINTF(IEW, "IEW: Still squashing.\n");
612 // Check if stage should remain squashing. Stop squashing if the
613 // squash signal clears.
614 if (!fromCommit->commitInfo.squash &&
615 !fromCommit->commitInfo.robSquashing) {
616 DPRINTF(IEW, "IEW: Done squashing, changing status to "
620 instQueue.stopSquash();
622 instQueue.doSquash();
626 } else if (_status == Blocked) {
627 // Continue to tell previous stage to stall.
628 toRename->iewInfo.stall = true;
630 // Check if possible stall conditions have cleared.
631 if (!fromCommit->commitInfo.stall &&
632 !instQueue.isFull()) {
633 DPRINTF(IEW, "IEW: Stall signals cleared, going to unblock.\n");
634 _status = Unblocking;
637 // If there's still instructions coming from rename, continue to
638 // put them on the skid buffer.
639 if (fromRename->size == 0) {
643 if (fromCommit->commitInfo.squash ||
644 fromCommit->commitInfo.robSquashing) {
651 // @todo: Maybe put these at the beginning, so if it's idle it can
653 // Write back number of free IQ entries here.
654 toRename->iewInfo.freeIQEntries = instQueue.numFreeEntries();
656 ldstQueue.writebackStores();
658 // Check the committed load/store signals to see if there's a load
659 // or store to commit. Also check if it's being told to execute a
660 // nonspeculative instruction.
661 // This is pretty inefficient...
662 if (!fromCommit->commitInfo.squash &&
663 !fromCommit->commitInfo.robSquashing) {
664 ldstQueue.commitStores(fromCommit->commitInfo.doneSeqNum);
665 ldstQueue.commitLoads(fromCommit->commitInfo.doneSeqNum);
668 if (fromCommit->commitInfo.nonSpecSeqNum != 0) {
669 instQueue.scheduleNonSpec(fromCommit->commitInfo.nonSpecSeqNum);
672 DPRINTF(IEW, "IEW: IQ has %i free entries.\n",
673 instQueue.numFreeEntries());
678 SimpleIEW<Impl>::iew()
680 // Might want to put all state checks in the tick() function.
681 // Check if being told to stall from commit.
682 if (fromCommit->commitInfo.stall) {
685 } else if (fromCommit->commitInfo.squash ||
686 fromCommit->commitInfo.robSquashing) {
687 // Also check if commit is telling this stage to squash.
694 // Have the instruction queue try to schedule any ready instructions.
695 instQueue.scheduleReadyInsts();
699 // Loop through the head of the time buffer and wake any dependents.
700 // These instructions are about to write back. In the simple model
701 // this loop can really happen within the previous loop, but when
702 // instructions have actual latencies, this loop must be separate.
703 // Also mark scoreboard that this instruction is finally complete.
704 // Either have IEW have direct access to rename map, or have this as
705 // part of backwards communication.
706 for (int inst_num = 0; inst_num < issueWidth &&
707 toCommit->insts[inst_num]; inst_num++)
709 DynInstPtr inst = toCommit->insts[inst_num];
711 DPRINTF(IEW, "IEW: Sending instructions to commit, PC %#x.\n",
714 if(!inst->isSquashed()) {
715 instQueue.wakeDependents(inst);
717 for (int i = 0; i < inst->numDestRegs(); i++)
719 renameMap->markAsReady(inst->renamedDestRegIdx(i));
724 // Also should advance its own time buffers if the stage ran.
725 // Not the best place for it, but this works (hopefully).
726 issueToExecQueue.advance();
732 SimpleIEW<Impl>::lsqWriteback()
734 ldstQueue.writebackAllInsts();