2 * Copyright (c) 2004-2006 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #ifndef __CPU_O3_IEW_HH__
30 #define __CPU_O3_IEW_HH__
34 #include "base/statistics.hh"
35 #include "base/timebuf.hh"
36 #include "config/full_system.hh"
37 #include "cpu/o3/comm.hh"
38 #include "cpu/o3/scoreboard.hh"
39 #include "cpu/o3/lsq.hh"
44 * DefaultIEW handles both single threaded and SMT IEW
45 * (issue/execute/writeback). It handles the dispatching of
46 * instructions to the LSQ/IQ as part of the issue stage, and has the
47 * IQ try to issue instructions each cycle. The execute latency is
48 * actually tied into the issue latency to allow the IQ to be able to
49 * do back-to-back scheduling without having to speculatively schedule
50 * instructions. This happens by having the IQ have access to the
51 * functional units, and the IQ gets the execution latencies from the
52 * FUs when it issues instructions. Instructions reach the execute
53 * stage on the last cycle of their execution, which is when the IQ
54 * knows to wake up any dependent instructions, allowing back to back
55 * scheduling. The execute portion of IEW separates memory
56 * instructions from non-memory instructions, either telling the LSQ
57 * to execute the instruction, or executing the instruction directly.
58 * The writeback portion of IEW completes the instructions by waking
59 * up any dependents, and marking the register ready on the
67 typedef typename Impl::CPUPol CPUPol;
68 typedef typename Impl::DynInstPtr DynInstPtr;
69 typedef typename Impl::FullCPU FullCPU;
70 typedef typename Impl::Params Params;
72 typedef typename CPUPol::IQ IQ;
73 typedef typename CPUPol::RenameMap RenameMap;
74 typedef typename CPUPol::LSQ LSQ;
76 typedef typename CPUPol::TimeStruct TimeStruct;
77 typedef typename CPUPol::IEWStruct IEWStruct;
78 typedef typename CPUPol::RenameStruct RenameStruct;
79 typedef typename CPUPol::IssueStruct IssueStruct;
81 friend class Impl::FullCPU;
82 friend class CPUPol::IQ;
85 /** Overall IEW stage status. Used to determine if the CPU can
86 * deschedule itself due to a lack of activity.
93 /** Status for Issue, Execute, and Writeback stages. */
104 /** Overall stage status. */
106 /** Dispatch status. */
107 StageStatus dispatchStatus[Impl::MaxThreads];
108 /** Execute status. */
109 StageStatus exeStatus;
110 /** Writeback status. */
111 StageStatus wbStatus;
114 /** LdWriteback event for a load completion. */
115 class LdWritebackEvent : public Event {
117 /** Instruction that is writing back data to the register file. */
119 /** Pointer to IEW stage. */
120 DefaultIEW<Impl> *iewStage;
123 /** Constructs a load writeback event. */
124 LdWritebackEvent(DynInstPtr &_inst, DefaultIEW<Impl> *_iew);
126 /** Processes writeback event. */
127 virtual void process();
128 /** Returns the description of the writeback event. */
129 virtual const char *description();
133 /** Constructs a DefaultIEW with the given parameters. */
134 DefaultIEW(Params *params);
136 /** Returns the name of the DefaultIEW stage. */
137 std::string name() const;
139 /** Registers statistics. */
142 /** Initializes stage; sends back the number of free IQ and LSQ entries. */
145 /** Sets CPU pointer for IEW, IQ, and LSQ. */
146 void setCPU(FullCPU *cpu_ptr);
148 /** Sets main time buffer used for backwards communication. */
149 void setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr);
151 /** Sets time buffer for getting instructions coming from rename. */
152 void setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr);
154 /** Sets time buffer to pass on instructions to commit. */
155 void setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr);
157 /** Sets pointer to list of active threads. */
158 void setActiveThreads(std::list<unsigned> *at_ptr);
160 /** Sets pointer to the scoreboard. */
161 void setScoreboard(Scoreboard *sb_ptr);
169 bool isSwitchedOut() { return switchedOut; }
171 /** Sets page table pointer within LSQ. */
172 // void setPageTable(PageTable *pt_ptr);
174 /** Squashes instructions in IEW for a specific thread. */
175 void squash(unsigned tid);
177 /** Wakes all dependents of a completed instruction. */
178 void wakeDependents(DynInstPtr &inst);
180 /** Tells memory dependence unit that a memory instruction needs to be
181 * rescheduled. It will re-execute once replayMemInst() is called.
183 void rescheduleMemInst(DynInstPtr &inst);
185 /** Re-executes all rescheduled memory instructions. */
186 void replayMemInst(DynInstPtr &inst);
188 /** Sends an instruction to commit through the time buffer. */
189 void instToCommit(DynInstPtr &inst);
191 /** Inserts unused instructions of a thread into the skid buffer. */
192 void skidInsert(unsigned tid);
194 /** Returns the max of the number of entries in all of the skid buffers. */
197 /** Returns if all of the skid buffers are empty. */
200 /** Updates overall IEW status based on all of the stages' statuses. */
203 /** Resets entries of the IQ and the LSQ. */
206 /** Tells the CPU to wakeup if it has descheduled itself due to no
207 * activity. Used mainly by the LdWritebackEvent.
211 /** Reports to the CPU that there is activity this cycle. */
212 void activityThisCycle();
214 /** Tells CPU that the IEW stage is active and running. */
215 inline void activateStage();
217 /** Tells CPU that the IEW stage is inactive and idle. */
218 inline void deactivateStage();
220 /** Returns if the LSQ has any stores to writeback. */
221 bool hasStoresToWB() { return ldstQueue.hasStoresToWB(); }
224 /** Sends commit proper information for a squash due to a branch
227 void squashDueToBranch(DynInstPtr &inst, unsigned thread_id);
229 /** Sends commit proper information for a squash due to a memory order
232 void squashDueToMemOrder(DynInstPtr &inst, unsigned thread_id);
234 /** Sends commit proper information for a squash due to memory becoming
235 * blocked (younger issued instructions must be retried).
237 void squashDueToMemBlocked(DynInstPtr &inst, unsigned thread_id);
239 /** Sets Dispatch to blocked, and signals back to other stages to block. */
240 void block(unsigned thread_id);
242 /** Unblocks Dispatch if the skid buffer is empty, and signals back to
243 * other stages to unblock.
245 void unblock(unsigned thread_id);
247 /** Determines proper actions to take given Dispatch's status. */
248 void dispatch(unsigned tid);
250 /** Dispatches instructions to IQ and LSQ. */
251 void dispatchInsts(unsigned tid);
253 /** Executes instructions. In the case of memory operations, it informs the
254 * LSQ to execute the instructions. Also handles any redirects that occur
255 * due to the executed instructions.
259 /** Writebacks instructions. In our model, the instruction's execute()
260 * function atomically reads registers, executes, and writes registers.
261 * Thus this writeback only wakes up dependent instructions, and informs
262 * the scoreboard of registers becoming ready.
264 void writebackInsts();
266 /** Returns the number of valid, non-squashed instructions coming from
267 * rename to dispatch.
269 unsigned validInstsFromRename();
271 /** Reads the stall signals. */
272 void readStallSignals(unsigned tid);
274 /** Checks if any of the stall conditions are currently true. */
275 bool checkStall(unsigned tid);
277 /** Processes inputs and changes state accordingly. */
278 void checkSignalsAndUpdate(unsigned tid);
280 /** Sorts instructions coming from rename into lists separated by thread. */
284 /** Ticks IEW stage, causing Dispatch, the IQ, the LSQ, Execute, and
285 * Writeback to run for one cycle.
290 void updateExeInstStats(DynInstPtr &inst);
292 /** Pointer to main time buffer used for backwards communication. */
293 TimeBuffer<TimeStruct> *timeBuffer;
295 /** Wire to write information heading to previous stages. */
296 typename TimeBuffer<TimeStruct>::wire toFetch;
298 /** Wire to get commit's output from backwards time buffer. */
299 typename TimeBuffer<TimeStruct>::wire fromCommit;
301 /** Wire to write information heading to previous stages. */
302 typename TimeBuffer<TimeStruct>::wire toRename;
304 /** Rename instruction queue interface. */
305 TimeBuffer<RenameStruct> *renameQueue;
307 /** Wire to get rename's output from rename queue. */
308 typename TimeBuffer<RenameStruct>::wire fromRename;
310 /** Issue stage queue. */
311 TimeBuffer<IssueStruct> issueToExecQueue;
313 /** Wire to read information from the issue stage time queue. */
314 typename TimeBuffer<IssueStruct>::wire fromIssue;
317 * IEW stage time buffer. Holds ROB indices of instructions that
318 * can be marked as completed.
320 TimeBuffer<IEWStruct> *iewQueue;
322 /** Wire to write infromation heading to commit. */
323 typename TimeBuffer<IEWStruct>::wire toCommit;
325 /** Queue of all instructions coming from rename this cycle. */
326 std::queue<DynInstPtr> insts[Impl::MaxThreads];
328 /** Skid buffer between rename and IEW. */
329 std::queue<DynInstPtr> skidBuffer[Impl::MaxThreads];
331 /** Scoreboard pointer. */
332 Scoreboard* scoreboard;
335 /** Instruction queue. */
338 /** Load / store queue. */
341 /** Pointer to the functional unit pool. */
348 /** Records if IEW has written to the time buffer this cycle, so that the
349 * CPU can deschedule itself if there is no activity.
351 bool wroteToTimeBuffer;
353 /** Source of possible stalls. */
358 /** Stages that are telling IEW to stall. */
359 Stalls stalls[Impl::MaxThreads];
361 /** Debug function to print instructions that are issued this cycle. */
362 void printAvailableInsts();
365 /** Records if the LSQ needs to be updated on the next cycle, so that
366 * IEW knows if there will be activity on the next cycle.
368 bool updateLSQNextCycle;
371 /** Records if there is a fetch redirect on this cycle for each thread. */
372 bool fetchRedirect[Impl::MaxThreads];
374 /** Used to track if all instructions have been dispatched this cycle.
375 * If they have not, then blocking must have occurred, and the instructions
376 * would already be added to the skid buffer.
377 * @todo: Fix this hack.
379 bool dispatchedAllInsts;
381 /** Records if the queues have been changed (inserted or issued insts),
382 * so that IEW knows to broadcast the updated amount of free entries.
386 /** Commit to IEW delay, in ticks. */
387 unsigned commitToIEWDelay;
389 /** Rename to IEW delay, in ticks. */
390 unsigned renameToIEWDelay;
393 * Issue to execute delay, in ticks. What this actually represents is
394 * the amount of time it takes for an instruction to wake up, be
395 * scheduled, and sent to a FU for execution.
397 unsigned issueToExecuteDelay;
399 /** Width of issue's read path, in instructions. The read path is both
400 * the skid buffer and the rename instruction queue.
401 * Note to self: is this really different than issueWidth?
403 unsigned issueReadWidth;
405 /** Width of issue, in instructions. */
408 /** Width of execute, in instructions. Might make more sense to break
409 * down into FP vs int.
411 unsigned executeWidth;
413 /** Index into queue of instructions being written back. */
416 /** Cycle number within the queue of instructions being written back.
417 * Used in case there are too many instructions writing back at the current
418 * cycle and writesbacks need to be scheduled for the future. See comments
423 /** Number of active threads. */
426 /** Pointer to list of active threads. */
427 std::list<unsigned> *activeThreads;
429 /** Maximum size of the skid buffer. */
430 unsigned skidBufferMax;
434 /** Stat for total number of idle cycles. */
435 Stats::Scalar<> iewIdleCycles;
436 /** Stat for total number of squashing cycles. */
437 Stats::Scalar<> iewSquashCycles;
438 /** Stat for total number of blocking cycles. */
439 Stats::Scalar<> iewBlockCycles;
440 /** Stat for total number of unblocking cycles. */
441 Stats::Scalar<> iewUnblockCycles;
442 /** Stat for total number of instructions dispatched. */
443 Stats::Scalar<> iewDispatchedInsts;
444 /** Stat for total number of squashed instructions dispatch skips. */
445 Stats::Scalar<> iewDispSquashedInsts;
446 /** Stat for total number of dispatched load instructions. */
447 Stats::Scalar<> iewDispLoadInsts;
448 /** Stat for total number of dispatched store instructions. */
449 Stats::Scalar<> iewDispStoreInsts;
450 /** Stat for total number of dispatched non speculative instructions. */
451 Stats::Scalar<> iewDispNonSpecInsts;
452 /** Stat for number of times the IQ becomes full. */
453 Stats::Scalar<> iewIQFullEvents;
454 /** Stat for number of times the LSQ becomes full. */
455 Stats::Scalar<> iewLSQFullEvents;
456 /** Stat for total number of executed instructions. */
457 Stats::Scalar<> iewExecutedInsts;
458 /** Stat for total number of executed load instructions. */
459 Stats::Vector<> iewExecLoadInsts;
460 /** Stat for total number of executed store instructions. */
461 // Stats::Scalar<> iewExecStoreInsts;
462 /** Stat for total number of squashed instructions skipped at execute. */
463 Stats::Scalar<> iewExecSquashedInsts;
464 /** Stat for total number of memory ordering violation events. */
465 Stats::Scalar<> memOrderViolationEvents;
466 /** Stat for total number of incorrect predicted taken branches. */
467 Stats::Scalar<> predictedTakenIncorrect;
468 /** Stat for total number of incorrect predicted not taken branches. */
469 Stats::Scalar<> predictedNotTakenIncorrect;
470 /** Stat for total number of mispredicted branches detected at execute. */
471 Stats::Formula branchMispredicts;
473 Stats::Vector<> exeSwp;
474 Stats::Vector<> exeNop;
475 Stats::Vector<> exeRefs;
476 Stats::Vector<> exeBranches;
478 // Stats::Vector<> issued_ops;
480 Stats::Vector<> stat_fu_busy;
481 Stats::Vector2d<> stat_fuBusy;
482 Stats::Vector<> dist_unissued;
483 Stats::Vector2d<> stat_issued_inst_type;
485 Stats::Formula issueRate;
486 Stats::Formula iewExecStoreInsts;
487 // Stats::Formula issue_op_rate;
488 // Stats::Formula fu_busy_rate;
490 Stats::Vector<> iewInstsToCommit;
491 Stats::Vector<> writebackCount;
492 Stats::Vector<> producerInst;
493 Stats::Vector<> consumerInst;
494 Stats::Vector<> wbPenalized;
496 Stats::Formula wbRate;
497 Stats::Formula wbFanout;
498 Stats::Formula wbPenalizedRate;
501 #endif // __CPU_O3_IEW_HH__