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_COMMIT_HH__
30 #define __CPU_O3_COMMIT_HH__
32 #include "arch/faults.hh"
33 #include "base/statistics.hh"
34 #include "base/timebuf.hh"
35 #include "cpu/exetrace.hh"
36 #include "cpu/inst_seq.hh"
37 #include "mem/memory_interface.hh"
43 * DefaultCommit handles single threaded and SMT commit. Its width is
44 * specified by the parameters; each cycle it tries to commit that
45 * many instructions. The SMT policy decides which thread it tries to
46 * commit instructions from. Non- speculative instructions must reach
47 * the head of the ROB before they are ready to execute; once they
48 * reach the head, commit will broadcast the instruction's sequence
49 * number to the previous stages so that they can issue/ execute the
50 * instruction. Only one non-speculative instruction is handled per
51 * cycle. Commit is responsible for handling all back-end initiated
52 * redirects. It receives the redirect, and then broadcasts it to all
53 * stages, indicating the sequence number they should squash until,
54 * and any necessary branch misprediction information as well. It
55 * priortizes redirects by instruction's age, only broadcasting a
56 * redirect if it corresponds to an instruction that should currently
57 * be in the ROB. This is done by tracking the sequence number of the
58 * youngest instruction in the ROB, which gets updated to any
59 * squashing instruction's sequence number, and only broadcasting a
60 * redirect if it corresponds to an older instruction. Commit also
61 * supports multiple cycle squashing, to model a ROB that can only
62 * remove a certain number of instructions per cycle.
68 // Typedefs from the Impl.
69 typedef typename Impl::FullCPU FullCPU;
70 typedef typename Impl::DynInstPtr DynInstPtr;
71 typedef typename Impl::Params Params;
72 typedef typename Impl::CPUPol CPUPol;
74 typedef typename CPUPol::RenameMap RenameMap;
75 typedef typename CPUPol::ROB ROB;
77 typedef typename CPUPol::TimeStruct TimeStruct;
78 typedef typename CPUPol::FetchStruct FetchStruct;
79 typedef typename CPUPol::IEWStruct IEWStruct;
80 typedef typename CPUPol::RenameStruct RenameStruct;
82 typedef typename CPUPol::Fetch Fetch;
83 typedef typename CPUPol::IEW IEW;
85 typedef O3ThreadState<Impl> Thread;
87 class TrapEvent : public Event {
89 DefaultCommit<Impl> *commit;
93 TrapEvent(DefaultCommit<Impl> *_commit, unsigned _tid);
96 const char *description();
99 /** Overall commit status. Used to determine if the CPU can deschedule
100 * itself due to a lack of activity.
107 /** Individual thread status. */
116 /** Commit policy for SMT mode. */
124 /** Overall commit status. */
125 CommitStatus _status;
126 /** Next commit status, to be set at the end of the cycle. */
127 CommitStatus _nextStatus;
128 /** Per-thread status. */
129 ThreadStatus commitStatus[Impl::MaxThreads];
130 /** Commit policy used in SMT mode. */
131 CommitPolicy commitPolicy;
134 /** Construct a DefaultCommit with the given parameters. */
135 DefaultCommit(Params *params);
137 /** Returns the name of the DefaultCommit. */
138 std::string name() const;
140 /** Registers statistics. */
143 /** Sets the CPU pointer. */
144 void setCPU(FullCPU *cpu_ptr);
146 /** Sets the list of threads. */
147 void setThreads(std::vector<Thread *> &threads);
149 /** Sets the main time buffer pointer, used for backwards communication. */
150 void setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr);
152 void setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr);
154 /** Sets the pointer to the queue coming from rename. */
155 void setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr);
157 /** Sets the pointer to the queue coming from IEW. */
158 void setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr);
160 void setFetchStage(Fetch *fetch_stage);
164 /** Sets the poitner to the IEW stage. */
165 void setIEWStage(IEW *iew_stage);
167 /** The pointer to the IEW stage. Used solely to ensure that
168 * various events (traps, interrupts, syscalls) do not occur until
169 * all stores have written back.
173 /** Sets pointer to list of active threads. */
174 void setActiveThreads(std::list<unsigned> *at_ptr);
176 /** Sets pointer to the commited state rename map. */
177 void setRenameMap(RenameMap rm_ptr[Impl::MaxThreads]);
179 /** Sets pointer to the ROB. */
180 void setROB(ROB *rob_ptr);
182 /** Initializes stage by sending back the number of free entries. */
191 /** Ticks the commit stage, which tries to commit instructions. */
194 /** Handles any squashes that are sent from IEW, and adds instructions
195 * to the ROB and tries to commit instructions.
199 /** Returns the number of free ROB entries for a specific thread. */
200 unsigned numROBFreeEntries(unsigned tid);
202 void generateXCEvent(unsigned tid);
205 /** Updates the overall status of commit with the nextStatus, and
206 * tell the CPU if commit is active/inactive. */
209 /** Sets the next status based on threads' statuses, which becomes the
210 * current status at the end of the cycle.
212 void setNextStatus();
214 /** Checks if the ROB is completed with squashing. This is for the case
215 * where the ROB can take multiple cycles to complete squashing.
217 bool robDoneSquashing();
219 /** Returns if any of the threads have the number of ROB entries changed
220 * on this cycle. Used to determine if the number of free ROB entries needs
221 * to be sent back to previous stages.
223 bool changedROBEntries();
225 void squashAll(unsigned tid);
227 void squashFromTrap(unsigned tid);
229 void squashFromXC(unsigned tid);
231 /** Commits as many instructions as possible. */
234 /** Tries to commit the head ROB instruction passed in.
235 * @param head_inst The instruction to be committed.
237 bool commitHead(DynInstPtr &head_inst, unsigned inst_num);
239 void generateTrapEvent(unsigned tid);
241 /** Gets instructions from rename and inserts them into the ROB. */
244 /** Marks completed instructions using information sent from IEW. */
245 void markCompletedInsts();
247 /** Gets the thread to commit, based on the SMT policy. */
248 int getCommittingThread();
250 /** Returns the thread ID to use based on a round robin policy. */
253 /** Returns the thread ID to use based on an oldest instruction policy. */
257 /** Returns the PC of the head instruction of the ROB.
258 * @todo: Probably remove this function as it returns only thread 0.
260 uint64_t readPC() { return PC[0]; }
262 uint64_t readPC(unsigned tid) { return PC[tid]; }
264 void setPC(uint64_t val, unsigned tid) { PC[tid] = val; }
266 uint64_t readNextPC(unsigned tid) { return nextPC[tid]; }
268 void setNextPC(uint64_t val, unsigned tid) { nextPC[tid] = val; }
271 /** Time buffer interface. */
272 TimeBuffer<TimeStruct> *timeBuffer;
274 /** Wire to write information heading to previous stages. */
275 typename TimeBuffer<TimeStruct>::wire toIEW;
277 /** Wire to read information from IEW (for ROB). */
278 typename TimeBuffer<TimeStruct>::wire robInfoFromIEW;
280 TimeBuffer<FetchStruct> *fetchQueue;
282 typename TimeBuffer<FetchStruct>::wire fromFetch;
284 /** IEW instruction queue interface. */
285 TimeBuffer<IEWStruct> *iewQueue;
287 /** Wire to read information from IEW queue. */
288 typename TimeBuffer<IEWStruct>::wire fromIEW;
290 /** Rename instruction queue interface, for ROB. */
291 TimeBuffer<RenameStruct> *renameQueue;
293 /** Wire to read information from rename queue. */
294 typename TimeBuffer<RenameStruct>::wire fromRename;
297 /** ROB interface. */
301 /** Pointer to FullCPU. */
304 /** Memory interface. Used for d-cache accesses. */
305 MemInterface *dcacheInterface;
307 std::vector<Thread *> thread;
313 /** Records that commit has written to the time buffer this cycle. Used for
314 * the CPU to determine if it can deschedule itself if there is no activity.
316 bool wroteToTimeBuffer;
318 /** Records if the number of ROB entries has changed this cycle. If it has,
319 * then the number of free entries must be re-broadcast.
321 bool changedROBNumEntries[Impl::MaxThreads];
323 /** A counter of how many threads are currently squashing. */
326 /** Records if a thread has to squash this cycle due to a trap. */
327 bool trapSquash[Impl::MaxThreads];
329 /** Records if a thread has to squash this cycle due to an XC write. */
330 bool xcSquash[Impl::MaxThreads];
332 /** Priority List used for Commit Policy */
333 std::list<unsigned> priority_list;
335 /** IEW to Commit delay, in ticks. */
336 unsigned iewToCommitDelay;
338 /** Commit to IEW delay, in ticks. */
339 unsigned commitToIEWDelay;
341 /** Rename to ROB delay, in ticks. */
342 unsigned renameToROBDelay;
344 unsigned fetchToCommitDelay;
346 /** Rename width, in instructions. Used so ROB knows how many
347 * instructions to get from the rename instruction queue.
349 unsigned renameWidth;
351 /** IEW width, in instructions. Used so ROB knows how many
352 * instructions to get from the IEW instruction queue.
356 /** Commit width, in instructions. */
357 unsigned commitWidth;
359 /** Number of Reorder Buffers */
362 /** Number of Active Threads */
370 Tick fetchTrapLatency;
374 Addr PC[Impl::MaxThreads];
376 Addr nextPC[Impl::MaxThreads];
378 /** The sequence number of the youngest valid instruction in the ROB. */
379 InstSeqNum youngestSeqNum[Impl::MaxThreads];
381 /** Pointer to the list of active threads. */
382 std::list<unsigned> *activeThreads;
384 /** Rename map interface. */
385 RenameMap *renameMap[Impl::MaxThreads];
387 void updateComInstStats(DynInstPtr &inst);
389 /** Stat for the total number of committed instructions. */
390 Stats::Scalar<> commitCommittedInsts;
391 /** Stat for the total number of squashed instructions discarded by commit.
393 Stats::Scalar<> commitSquashedInsts;
394 /** Stat for the total number of times commit is told to squash.
395 * @todo: Actually increment this stat.
397 Stats::Scalar<> commitSquashEvents;
398 /** Stat for the total number of times commit has had to stall due to a non-
399 * speculative instruction reaching the head of the ROB.
401 Stats::Scalar<> commitNonSpecStalls;
402 /** Stat for the total number of branch mispredicts that caused a squash. */
403 Stats::Scalar<> branchMispredicts;
404 /** Distribution of the number of committed instructions each cycle. */
405 Stats::Distribution<> numCommittedDist;
407 /** Total number of instructions committed. */
408 Stats::Vector<> statComInst;
409 /** Total number of software prefetches committed. */
410 Stats::Vector<> statComSwp;
411 /** Stat for the total number of committed memory references. */
412 Stats::Vector<> statComRefs;
413 /** Stat for the total number of committed loads. */
414 Stats::Vector<> statComLoads;
415 /** Total number of committed memory barriers. */
416 Stats::Vector<> statComMembars;
417 /** Total number of committed branches. */
418 Stats::Vector<> statComBranches;
420 Stats::Scalar<> commitEligibleSamples;
421 Stats::Vector<> commitEligible;
424 #endif // __CPU_O3_COMMIT_HH__