inorder: cleanup virtual functions
[gem5.git] / src / cpu / inorder / pipeline_stage.hh
1 /*
2 * Copyright (c) 2007 MIPS Technologies, Inc.
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Korey Sewell
29 *
30 */
31
32 #ifndef __CPU_INORDER_PIPELINE_STAGE_HH__
33 #define __CPU_INORDER_PIPELINE_STAGE_HH__
34
35 #include <queue>
36 #include <vector>
37
38 #include "base/statistics.hh"
39 #include "base/timebuf.hh"
40 #include "cpu/inorder/inorder_dyn_inst.hh"
41 #include "cpu/inorder/comm.hh"
42 #include "params/InOrderCPU.hh"
43 #include "cpu/inorder/pipeline_traits.hh"
44
45 class InOrderCPU;
46
47 class PipelineStage
48 {
49 protected:
50 typedef ThePipeline::Params Params;
51 typedef ThePipeline::DynInstPtr DynInstPtr;
52
53 public:
54 /** Overall stage status. Used to determine if the CPU can
55 * deschedule itself due to a lack of activity.
56 */
57 enum StageStatus {
58 Active,
59 Inactive
60 };
61
62 /** Individual thread status. */
63 enum ThreadStatus {
64 Running,
65 Idle,
66 StartSquash,
67 Squashing,
68 Blocked,
69 Unblocking,
70 MemWaitResponse,
71 MemWaitRetry,
72 MemAccessComplete
73 };
74
75 protected:
76 /** The Number of This Pipeline Stage */
77 unsigned stageNum;
78
79 /** The width of stage, in instructions. */
80 unsigned stageWidth;
81
82 /** Number of Threads*/
83 ThreadID numThreads;
84
85 /** Stage status. */
86 StageStatus _status;
87
88 /** Per-thread status. */
89 ThreadStatus stageStatus[ThePipeline::MaxThreads];
90
91 public:
92 PipelineStage(Params *params, unsigned stage_num);
93
94 /** MUST use init() function if this constructor is used. */
95 PipelineStage() { }
96
97 virtual ~PipelineStage() { }
98
99 /** PipelineStage initialization. */
100 void init(Params *params);
101
102 /** Returns the name of stage. */
103 std::string name() const;
104
105 /** Registers statistics. */
106 void regStats();
107
108 /** Sets CPU pointer. */
109 void setCPU(InOrderCPU *cpu_ptr);
110
111 /** Sets the main backwards communication time buffer pointer. */
112 void setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr);
113
114 /** Sets pointer to time buffer coming from fetch. */
115 void setPrevStageQueue(TimeBuffer<InterStageStruct> *prev_stage_ptr);
116
117 /** Sets pointer to time buffer used to communicate to the next stage. */
118 void setNextStageQueue(TimeBuffer<InterStageStruct> *next_stage_ptr);
119
120 /** Sets pointer to list of active threads. */
121 void setActiveThreads(std::list<ThreadID> *at_ptr);
122
123 bool nextStageQueueValid(int stage_num);
124
125 bool isBlocked(ThreadID tid);
126
127 /** Changes the status of this stage to active, and indicates this
128 * to the CPU.
129 */
130 //inline void switchToActive();
131
132 /** Changes the status of this stage to inactive, and indicates
133 * this to the CPU.
134 */
135 //inline void switchToInactive();
136
137 /** Switches out the stage stage. */
138 void switchOut();
139
140 /** Takes over from another CPU's thread. */
141 void takeOverFrom();
142
143 /** Ticks stage, processing all input signals and executing as many
144 * instructions as possible.
145 */
146 void tick();
147
148 /** Set a resource stall in the pipeline-stage */
149 void setResStall(ResReqPtr res_req, ThreadID tid);
150
151 /** Unset a resource stall in the pipeline-stage */
152 void unsetResStall(ResReqPtr res_req, ThreadID tid);
153
154 /** Remove all stall signals for a particular thread; */
155 void removeStalls(ThreadID tid);
156
157 /** Is there room in the stage buffer? */
158 int stageBufferAvail();
159
160 protected:
161 /** Evaluate Stage Conditions and then process stage */
162 virtual void processStage(bool &status_change);
163
164 /** Determines what to do based on stage's current status.
165 * @param status_change stage() sets this variable if there was a status
166 * change (ie switching from from blocking to unblocking).
167 * @param tid Thread id to stage instructions from.
168 */
169 void processThread(bool &status_change, ThreadID tid);
170
171 /** Processes instructions from fetch and passes them on to rename.
172 * Decoding of instructions actually happens when they are created in
173 * fetch, so this function mostly checks if PC-relative branches are
174 * correct.
175 */
176 virtual void processInsts(ThreadID tid);
177
178 /** Process all resources on an instruction's resource schedule */
179 bool processInstSchedule(DynInstPtr inst, int &reqs_processed);
180
181 /** Is there room in the next stage buffer for this instruction? */
182 bool canSendInstToStage(unsigned stage_num);
183
184 /** Send an instruction to the next stage buffer */
185 bool sendInstToNextStage(DynInstPtr inst);
186
187 /** Inserts a thread's instructions into the skid buffer, to be staged
188 * once stage unblocks.
189 */
190 virtual void skidInsert(ThreadID tid);
191
192 /** Total size of all skid buffers */
193 int skidSize();
194
195 /** Returns if all of the skid buffers are empty. */
196 bool skidsEmpty();
197
198 /** Updates overall stage status based on all of the threads' statuses. */
199 void updateStatus();
200
201 /** Separates instructions from fetch into individual lists of instructions
202 * sorted by thread.
203 */
204 void sortInsts();
205
206 /** Reads all stall signals from the backwards communication timebuffer. */
207 void readStallSignals(ThreadID tid);
208
209 /** Checks all input signals and updates stage's status appropriately. */
210 bool checkSignalsAndUpdate(ThreadID tid);
211
212 /** Checks all stall signals, and returns if any are true. */
213 bool checkStall(ThreadID tid) const;
214
215 /** Returns if there any instructions from the previous stage
216 * on this cycle.
217 */
218 inline bool prevStageInstsValid();
219
220 /** Switches stage to blocking, and signals back that stage has
221 * become blocked.
222 * @return Returns true if there is a status change.
223 */
224 bool block(ThreadID tid);
225
226 void blockDueToBuffer(ThreadID tid);
227
228 /** Switches stage to unblocking if the skid buffer is empty, and
229 * signals back that stage has unblocked.
230 * @return Returns true if there is a status change.
231 */
232 bool unblock(ThreadID tid);
233
234
235 public:
236 void activateThread(ThreadID tid);
237
238 /** Squashes if there is a PC-relative branch that was predicted
239 * incorrectly. Sends squash information back to fetch.
240 */
241 void squashDueToBranch(DynInstPtr &inst, ThreadID tid);
242
243 virtual void squashDueToMemStall(InstSeqNum seq_num, ThreadID tid);
244
245 /** Squash instructions from stage buffer */
246 void squashPrevStageInsts(InstSeqNum squash_seq_num, ThreadID tid);
247
248 /** Squashes due to commit signalling a squash. Changes status to
249 * squashing and clears block/unblock signals as needed.
250 */
251 virtual void squash(InstSeqNum squash_num, ThreadID tid);
252
253 void dumpInsts();
254
255 protected:
256 /** CPU interface. */
257 InOrderCPU *cpu;
258
259 Trace::InOrderTrace *tracer;
260
261 /** List of active thread ids */
262 std::list<ThreadID> *activeThreads;
263
264 /** Buffer of instructions switched out to mem-stall.
265 * Only used when using SwitchOnCacheMiss threading model
266 * Used as 1-to-1 mapping between ThreadID and Entry.
267 */
268 std::vector<DynInstPtr> switchedOutBuffer;
269 std::vector<bool> switchedOutValid;
270
271 /** Instructions that we've processed this tick
272 * NOTE: "Processed" means completed at least 1 instruction request
273 */
274 unsigned instsProcessed;
275
276 /** Queue of all instructions coming from previous stage on this cycle. */
277 std::queue<DynInstPtr> insts[ThePipeline::MaxThreads];
278
279 /** Queue of instructions that are finished processing and ready to go
280 * next stage. This is used to prevent from processing an instrution more
281 * than once on any stage. NOTE: It is up to the PROGRAMMER must manage
282 * this as a queue
283 */
284 std::list<DynInstPtr> instsToNextStage;
285
286 /** Skid buffer between previous stage and this one. */
287 std::queue<DynInstPtr> skidBuffer[ThePipeline::MaxThreads];
288
289 /** Instruction used to signify that there is no *real* instruction in
290 * buffer slot */
291 DynInstPtr dummyBufferInst;
292
293 /** SeqNum of Squashing Branch Delay Instruction (used for MIPS) */
294 Addr bdelayDoneSeqNum[ThePipeline::MaxThreads];
295
296 /** Instruction used for squashing branch (used for MIPS) */
297 DynInstPtr squashInst[ThePipeline::MaxThreads];
298
299 /** Tells when their is a pending delay slot inst. to send
300 * to rename. If there is, then wait squash after the next
301 * instruction (used for MIPS).
302 */
303 bool squashAfterDelaySlot[ThePipeline::MaxThreads];
304
305 /** Maximum size of the inter-stage buffer connecting the previous stage to
306 * this stage (which we call a skid buffer) */
307 unsigned stageBufferMax;
308
309 /** Variable that tracks if stage has written to the time buffer this
310 * cycle. Used to tell CPU if there is activity this cycle.
311 */
312 bool wroteToTimeBuffer;
313
314 /** Index of instructions being sent to the next stage. */
315 unsigned toNextStageIndex;
316
317 /** The last stage that this particular stage should look for stalls */
318 int lastStallingStage[ThePipeline::MaxThreads];
319
320 /** Time buffer interface. */
321 TimeBuffer<TimeStruct> *timeBuffer;
322
323 public:
324 /** Wire to get rename's output from backwards time buffer. */
325 TimeBuffer<TimeStruct>::wire fromNextStages;
326
327 /** Wire to get iew's information from backwards time buffer. */
328 TimeBuffer<TimeStruct>::wire toPrevStages;
329
330 /** Instruction queue linking previous stage */
331 TimeBuffer<InterStageStruct> *prevStageQueue;
332
333 /** Wire to get the previous stage's. */
334 TimeBuffer<InterStageStruct>::wire prevStage;
335
336 /** Instruction queue linking next stage */
337 TimeBuffer<InterStageStruct> *nextStageQueue;
338
339 /** Wire to write to the next stage */
340 TimeBuffer<InterStageStruct>::wire nextStage;
341
342 /** Is Previous Stage Valid? */
343 bool prevStageValid;
344
345 /** Is Next Stage Valid? */
346 bool nextStageValid;
347
348 bool idle;
349
350 /** Source of possible stalls. */
351 struct Stalls {
352 bool stage[ThePipeline::NumStages];
353 std::vector<ResReqPtr> resources;
354 };
355
356 /** Tracks stage/resource stalls */
357 Stalls stalls[ThePipeline::MaxThreads];
358
359 /** Number of cycles 0 instruction(s) are processed. */
360 Stats::Scalar idleCycles;
361
362 /** Number of cycles 1+ instructions are processed. */
363 Stats::Scalar runCycles;
364
365 /** Percentage of cycles 1+ instructions are processed. */
366 Stats::Formula utilization;
367
368
369 };
370
371 #endif