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. */
178 /** Ticks the commit stage, which tries to commit instructions. */
181 /** Handles any squashes that are sent from IEW, and adds instructions
182 * to the ROB and tries to commit instructions.
186 /** Returns the number of free ROB entries for a specific thread. */
187 unsigned numROBFreeEntries(unsigned tid);
189 void generateXCEvent(unsigned tid);
192 /** Updates the overall status of commit with the nextStatus, and
193 * tell the CPU if commit is active/inactive. */
196 /** Sets the next status based on threads' statuses, which becomes the
197 * current status at the end of the cycle.
199 void setNextStatus();
201 /** Checks if the ROB is completed with squashing. This is for the case
202 * where the ROB can take multiple cycles to complete squashing.
204 bool robDoneSquashing();
206 /** Returns if any of the threads have the number of ROB entries changed
207 * on this cycle. Used to determine if the number of free ROB entries needs
208 * to be sent back to previous stages.
210 bool changedROBEntries();
212 void squashFromTrap(unsigned tid);
214 void squashFromXC(unsigned tid);
216 void squashInFlightInsts(unsigned tid);
219 /** Commits as many instructions as possible. */
222 /** Tries to commit the head ROB instruction passed in.
223 * @param head_inst The instruction to be committed.
225 bool commitHead(DynInstPtr &head_inst, unsigned inst_num);
227 void generateTrapEvent(unsigned tid);
229 /** Gets instructions from rename and inserts them into the ROB. */
232 /** Marks completed instructions using information sent from IEW. */
233 void markCompletedInsts();
235 /** Gets the thread to commit, based on the SMT policy. */
236 int getCommittingThread();
238 /** Returns the thread ID to use based on a round robin policy. */
241 /** Returns the thread ID to use based on an oldest instruction policy. */
245 /** Returns the PC of the head instruction of the ROB. */
248 uint64_t readPC(unsigned tid) { return PC[tid]; }
250 void setPC(uint64_t val, unsigned tid) { PC[tid] = val; }
252 uint64_t readNextPC(unsigned tid) { return nextPC[tid]; }
254 void setNextPC(uint64_t val, unsigned tid) { nextPC[tid] = val; }
256 /** Sets that the ROB is currently squashing. */
257 void setSquashing(unsigned tid);
260 /** Time buffer interface. */
261 TimeBuffer<TimeStruct> *timeBuffer;
263 /** Wire to write information heading to previous stages. */
264 typename TimeBuffer<TimeStruct>::wire toIEW;
266 /** Wire to read information from IEW (for ROB). */
267 typename TimeBuffer<TimeStruct>::wire robInfoFromIEW;
269 TimeBuffer<FetchStruct> *fetchQueue;
271 typename TimeBuffer<FetchStruct>::wire fromFetch;
273 /** IEW instruction queue interface. */
274 TimeBuffer<IEWStruct> *iewQueue;
276 /** Wire to read information from IEW queue. */
277 typename TimeBuffer<IEWStruct>::wire fromIEW;
279 /** Rename instruction queue interface, for ROB. */
280 TimeBuffer<RenameStruct> *renameQueue;
282 /** Wire to read information from rename queue. */
283 typename TimeBuffer<RenameStruct>::wire fromRename;
286 /** ROB interface. */
290 /** Pointer to FullCPU. */
293 /** Memory interface. Used for d-cache accesses. */
294 MemInterface *dcacheInterface;
296 std::vector<Thread *> thread;
300 InstSeqNum fetchFaultSN;
302 /** Records that commit has written to the time buffer this cycle. Used for
303 * the CPU to determine if it can deschedule itself if there is no activity.
305 bool wroteToTimeBuffer;
307 /** Records if the number of ROB entries has changed this cycle. If it has,
308 * then the number of free entries must be re-broadcast.
310 bool changedROBNumEntries[Impl::MaxThreads];
312 /** A counter of how many threads are currently squashing. */
315 /** Records if a thread has to squash this cycle due to a trap. */
316 bool trapSquash[Impl::MaxThreads];
318 /** Records if a thread has to squash this cycle due to an XC write. */
319 bool xcSquash[Impl::MaxThreads];
321 /** Priority List used for Commit Policy */
322 std::list<unsigned> priority_list;
324 /** IEW to Commit delay, in ticks. */
325 unsigned iewToCommitDelay;
327 /** Commit to IEW delay, in ticks. */
328 unsigned commitToIEWDelay;
330 /** Rename to ROB delay, in ticks. */
331 unsigned renameToROBDelay;
333 unsigned fetchToCommitDelay;
335 /** Rename width, in instructions. Used so ROB knows how many
336 * instructions to get from the rename instruction queue.
338 unsigned renameWidth;
340 /** IEW width, in instructions. Used so ROB knows how many
341 * instructions to get from the IEW instruction queue.
345 /** Commit width, in instructions. */
346 unsigned commitWidth;
348 /** Number of Reorder Buffers */
351 /** Number of Active Threads */
356 Tick fetchTrapLatency;
359 Addr PC[Impl::MaxThreads];
361 Addr nextPC[Impl::MaxThreads];
363 /** The sequence number of the youngest valid instruction in the ROB. */
364 InstSeqNum youngestSeqNum[Impl::MaxThreads];
366 /** Pointer to the list of active threads. */
367 std::list<unsigned> *activeThreads;
369 /** Rename map interface. */
370 RenameMap *renameMap[Impl::MaxThreads];
372 void updateComInstStats(DynInstPtr &inst);
374 /** Stat for the total number of committed instructions. */
375 Stats::Scalar<> commitCommittedInsts;
376 /** Stat for the total number of squashed instructions discarded by commit.
378 Stats::Scalar<> commitSquashedInsts;
379 /** Stat for the total number of times commit is told to squash.
380 * @todo: Actually increment this stat.
382 Stats::Scalar<> commitSquashEvents;
383 /** Stat for the total number of times commit has had to stall due to a non-
384 * speculative instruction reaching the head of the ROB.
386 Stats::Scalar<> commitNonSpecStalls;
387 /** Stat for the total number of committed branches. */
388 // Stats::Scalar<> commitCommittedBranches;
389 /** Stat for the total number of committed loads. */
390 // Stats::Scalar<> commitCommittedLoads;
391 /** Stat for the total number of committed memory references. */
392 // Stats::Scalar<> commitCommittedMemRefs;
393 /** Stat for the total number of branch mispredicts that caused a squash. */
394 Stats::Scalar<> branchMispredicts;
395 /** Distribution of the number of committed instructions each cycle. */
396 Stats::Distribution<> numCommittedDist;
398 // total number of instructions committed
399 Stats::Vector<> stat_com_inst;
400 Stats::Vector<> stat_com_swp;
401 Stats::Vector<> stat_com_refs;
402 Stats::Vector<> stat_com_loads;
403 Stats::Vector<> stat_com_membars;
404 Stats::Vector<> stat_com_branches;
406 Stats::Scalar<> commit_eligible_samples;
407 Stats::Vector<> commit_eligible;
410 #endif // __CPU_O3_COMMIT_HH__