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 #ifndef __CPU_O3_COMMIT_HH__
30 #define __CPU_O3_COMMIT_HH__
32 #include "arch/faults.hh"
33 #include "cpu/inst_seq.hh"
34 #include "base/statistics.hh"
35 #include "base/timebuf.hh"
36 #include "cpu/exetrace.hh"
37 #include "mem/memory_interface.hh"
43 * DefaultCommit handles single threaded and SMT commit. Its width is specified
44 * by the parameters; each cycle it tries to commit that many instructions. The
45 * SMT policy decides which thread it tries to commit instructions from. Non-
46 * speculative instructions must reach the head of the ROB before they are
47 * ready to execute; once they reach the head, commit will broadcast the
48 * instruction's sequence number to the previous stages so that they can issue/
49 * execute the instruction. Only one non-speculative instruction is handled per
50 * cycle. Commit is responsible for handling all back-end initiated redirects.
51 * It receives the redirect, and then broadcasts it to all stages, indicating
52 * the sequence number they should squash until, and any necessary branch mis-
53 * prediction information as well. It priortizes redirects by instruction's age,
54 * only broadcasting a redirect if it corresponds to an instruction that should
55 * currently be in the ROB. This is done by tracking the sequence number of the
56 * youngest instruction in the ROB, which gets updated to any squashing
57 * instruction's sequence number, and only broadcasting a redirect if it
58 * corresponds to an older instruction. Commit also supports multiple cycle
59 * squashing, to model a ROB that can only remove a certain number of
60 * instructions per cycle. Eventually traps and interrupts will most likely
61 * be handled here as well.
67 // Typedefs from the Impl.
68 typedef typename Impl::FullCPU FullCPU;
69 typedef typename Impl::DynInstPtr DynInstPtr;
70 typedef typename Impl::Params Params;
71 typedef typename Impl::CPUPol CPUPol;
73 typedef typename CPUPol::RenameMap RenameMap;
74 typedef typename CPUPol::ROB ROB;
76 typedef typename CPUPol::TimeStruct TimeStruct;
77 typedef typename CPUPol::FetchStruct FetchStruct;
78 typedef typename CPUPol::IEWStruct IEWStruct;
79 typedef typename CPUPol::RenameStruct RenameStruct;
81 typedef typename CPUPol::IEW IEW;
83 typedef O3ThreadState<Impl> Thread;
85 class TrapEvent : public Event {
87 DefaultCommit<Impl> *commit;
91 TrapEvent(DefaultCommit<Impl> *_commit, unsigned _tid);
94 const char *description();
97 /** Overall commit status. Used to determine if the CPU can deschedule
98 * itself due to a lack of activity.
105 /** Individual thread status. */
114 /** Commit policy for SMT mode. */
122 /** Overall commit status. */
123 CommitStatus _status;
124 /** Next commit status, to be set at the end of the cycle. */
125 CommitStatus _nextStatus;
126 /** Per-thread status. */
127 ThreadStatus commitStatus[Impl::MaxThreads];
128 /** Commit policy used in SMT mode. */
129 CommitPolicy commitPolicy;
132 /** Construct a DefaultCommit with the given parameters. */
133 DefaultCommit(Params *params);
135 /** Returns the name of the DefaultCommit. */
136 std::string name() const;
138 /** Registers statistics. */
141 /** Sets the CPU pointer. */
142 void setCPU(FullCPU *cpu_ptr);
144 /** Sets the list of threads. */
145 void setThreads(std::vector<Thread *> &threads);
147 /** Sets the main time buffer pointer, used for backwards communication. */
148 void setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr);
150 void setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr);
152 /** Sets the pointer to the queue coming from rename. */
153 void setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr);
155 /** Sets the pointer to the queue coming from IEW. */
156 void setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr);
158 /** Sets the poitner to the IEW stage. */
159 void setIEWStage(IEW *iew_stage);
161 /** The pointer to the IEW stage. Used solely to ensure that syscalls do
162 * not execute until all stores have written back.
166 /** Sets pointer to list of active threads. */
167 void setActiveThreads(std::list<unsigned> *at_ptr);
169 /** Sets pointer to the commited state rename map. */
170 void setRenameMap(RenameMap rm_ptr[Impl::MaxThreads]);
172 /** Sets pointer to the ROB. */
173 void setROB(ROB *rob_ptr);
175 /** Initializes stage by sending back the number of free entries. */
182 /** Ticks the commit stage, which tries to commit instructions. */
185 /** Handles any squashes that are sent from IEW, and adds instructions
186 * to the ROB and tries to commit instructions.
190 /** Returns the number of free ROB entries for a specific thread. */
191 unsigned numROBFreeEntries(unsigned tid);
193 void generateXCEvent(unsigned tid);
196 /** Updates the overall status of commit with the nextStatus, and
197 * tell the CPU if commit is active/inactive. */
200 /** Sets the next status based on threads' statuses, which becomes the
201 * current status at the end of the cycle.
203 void setNextStatus();
205 /** Checks if the ROB is completed with squashing. This is for the case
206 * where the ROB can take multiple cycles to complete squashing.
208 bool robDoneSquashing();
210 /** Returns if any of the threads have the number of ROB entries changed
211 * on this cycle. Used to determine if the number of free ROB entries needs
212 * to be sent back to previous stages.
214 bool changedROBEntries();
216 void squashFromTrap(unsigned tid);
218 void squashFromXC(unsigned tid);
220 void squashInFlightInsts(unsigned tid);
223 /** Commits as many instructions as possible. */
226 /** Tries to commit the head ROB instruction passed in.
227 * @param head_inst The instruction to be committed.
229 bool commitHead(DynInstPtr &head_inst, unsigned inst_num);
231 void generateTrapEvent(unsigned tid);
233 /** Gets instructions from rename and inserts them into the ROB. */
236 /** Marks completed instructions using information sent from IEW. */
237 void markCompletedInsts();
239 /** Gets the thread to commit, based on the SMT policy. */
240 int getCommittingThread();
242 /** Returns the thread ID to use based on a round robin policy. */
245 /** Returns the thread ID to use based on an oldest instruction policy. */
249 /** Returns the PC of the head instruction of the ROB. */
252 uint64_t readPC(unsigned tid) { return PC[tid]; }
254 void setPC(uint64_t val, unsigned tid) { PC[tid] = val; }
256 uint64_t readNextPC(unsigned tid) { return nextPC[tid]; }
258 void setNextPC(uint64_t val, unsigned tid) { nextPC[tid] = val; }
260 /** Sets that the ROB is currently squashing. */
261 void setSquashing(unsigned tid);
264 /** Time buffer interface. */
265 TimeBuffer<TimeStruct> *timeBuffer;
267 /** Wire to write information heading to previous stages. */
268 typename TimeBuffer<TimeStruct>::wire toIEW;
270 /** Wire to read information from IEW (for ROB). */
271 typename TimeBuffer<TimeStruct>::wire robInfoFromIEW;
273 TimeBuffer<FetchStruct> *fetchQueue;
275 typename TimeBuffer<FetchStruct>::wire fromFetch;
277 /** IEW instruction queue interface. */
278 TimeBuffer<IEWStruct> *iewQueue;
280 /** Wire to read information from IEW queue. */
281 typename TimeBuffer<IEWStruct>::wire fromIEW;
283 /** Rename instruction queue interface, for ROB. */
284 TimeBuffer<RenameStruct> *renameQueue;
286 /** Wire to read information from rename queue. */
287 typename TimeBuffer<RenameStruct>::wire fromRename;
290 /** ROB interface. */
294 /** Pointer to FullCPU. */
297 /** Memory interface. Used for d-cache accesses. */
298 MemInterface *dcacheInterface;
300 std::vector<Thread *> thread;
304 InstSeqNum fetchFaultSN;
306 /** Records that commit has written to the time buffer this cycle. Used for
307 * the CPU to determine if it can deschedule itself if there is no activity.
309 bool wroteToTimeBuffer;
311 /** Records if the number of ROB entries has changed this cycle. If it has,
312 * then the number of free entries must be re-broadcast.
314 bool changedROBNumEntries[Impl::MaxThreads];
316 /** A counter of how many threads are currently squashing. */
319 /** Records if a thread has to squash this cycle due to a trap. */
320 bool trapSquash[Impl::MaxThreads];
322 /** Records if a thread has to squash this cycle due to an XC write. */
323 bool xcSquash[Impl::MaxThreads];
325 /** Priority List used for Commit Policy */
326 std::list<unsigned> priority_list;
328 /** IEW to Commit delay, in ticks. */
329 unsigned iewToCommitDelay;
331 /** Commit to IEW delay, in ticks. */
332 unsigned commitToIEWDelay;
334 /** Rename to ROB delay, in ticks. */
335 unsigned renameToROBDelay;
337 unsigned fetchToCommitDelay;
339 /** Rename width, in instructions. Used so ROB knows how many
340 * instructions to get from the rename instruction queue.
342 unsigned renameWidth;
344 /** IEW width, in instructions. Used so ROB knows how many
345 * instructions to get from the IEW instruction queue.
349 /** Commit width, in instructions. */
350 unsigned commitWidth;
352 /** Number of Reorder Buffers */
355 /** Number of Active Threads */
362 Tick fetchTrapLatency;
365 Addr PC[Impl::MaxThreads];
367 Addr nextPC[Impl::MaxThreads];
369 /** The sequence number of the youngest valid instruction in the ROB. */
370 InstSeqNum youngestSeqNum[Impl::MaxThreads];
372 /** Pointer to the list of active threads. */
373 std::list<unsigned> *activeThreads;
375 /** Rename map interface. */
376 RenameMap *renameMap[Impl::MaxThreads];
378 void updateComInstStats(DynInstPtr &inst);
380 /** Stat for the total number of committed instructions. */
381 Stats::Scalar<> commitCommittedInsts;
382 /** Stat for the total number of squashed instructions discarded by commit.
384 Stats::Scalar<> commitSquashedInsts;
385 /** Stat for the total number of times commit is told to squash.
386 * @todo: Actually increment this stat.
388 Stats::Scalar<> commitSquashEvents;
389 /** Stat for the total number of times commit has had to stall due to a non-
390 * speculative instruction reaching the head of the ROB.
392 Stats::Scalar<> commitNonSpecStalls;
393 /** Stat for the total number of committed branches. */
394 // Stats::Scalar<> commitCommittedBranches;
395 /** Stat for the total number of committed loads. */
396 // Stats::Scalar<> commitCommittedLoads;
397 /** Stat for the total number of committed memory references. */
398 // Stats::Scalar<> commitCommittedMemRefs;
399 /** Stat for the total number of branch mispredicts that caused a squash. */
400 Stats::Scalar<> branchMispredicts;
401 /** Distribution of the number of committed instructions each cycle. */
402 Stats::Distribution<> numCommittedDist;
404 // total number of instructions committed
405 Stats::Vector<> stat_com_inst;
406 Stats::Vector<> stat_com_swp;
407 Stats::Vector<> stat_com_refs;
408 Stats::Vector<> stat_com_loads;
409 Stats::Vector<> stat_com_membars;
410 Stats::Vector<> stat_com_branches;
412 Stats::Scalar<> commit_eligible_samples;
413 Stats::Vector<> commit_eligible;
416 #endif // __CPU_O3_COMMIT_HH__