iew_impl.hh revision 1061
1// @todo: Fix the instantaneous communication among all the stages within
2// iew.  There's a clear delay between issue and execute, yet backwards
3// communication happens simultaneously.  Might not be that bad really...
4// it might skew stats a bit though.  Issue would otherwise try to issue
5// instructions that would never be executed if there were a delay; without
6// it issue will simply squash.  Make this stage block properly.
7// Update the statuses for each stage.
8// Actually read instructions out of the skid buffer.
9
10#include <queue>
11
12#include "base/timebuf.hh"
13#include "cpu/beta_cpu/iew.hh"
14
15template<class Impl, class IQ>
16SimpleIEW<Impl, IQ>::SimpleIEW(Params &params)
17    : // Just make this time buffer really big for now
18      issueToExecQueue(5, 5),
19      instQueue(params),
20      ldstQueue(params),
21      commitToIEWDelay(params.commitToIEWDelay),
22      renameToIEWDelay(params.renameToIEWDelay),
23      issueToExecuteDelay(params.issueToExecuteDelay),
24      issueReadWidth(params.issueWidth),
25      issueWidth(params.issueWidth),
26      executeWidth(params.executeWidth)
27{
28    DPRINTF(IEW, "IEW: executeIntWidth: %i.\n", params.executeIntWidth);
29    _status = Idle;
30    _issueStatus = Idle;
31    _exeStatus = Idle;
32    _wbStatus = Idle;
33
34    // Setup wire to read instructions coming from issue.
35    fromIssue = issueToExecQueue.getWire(-issueToExecuteDelay);
36
37    // Instruction queue needs the queue between issue and execute.
38    instQueue.setIssueToExecuteQueue(&issueToExecQueue);
39}
40
41template<class Impl, class IQ>
42void
43SimpleIEW<Impl, IQ>::setCPU(FullCPU *cpu_ptr)
44{
45    DPRINTF(IEW, "IEW: Setting CPU pointer.\n");
46    cpu = cpu_ptr;
47
48    instQueue.setCPU(cpu_ptr);
49    ldstQueue.setCPU(cpu_ptr);
50}
51
52template<class Impl, class IQ>
53void
54SimpleIEW<Impl, IQ>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
55{
56    DPRINTF(IEW, "IEW: Setting time buffer pointer.\n");
57    timeBuffer = tb_ptr;
58
59    // Setup wire to read information from time buffer, from commit.
60    fromCommit = timeBuffer->getWire(-commitToIEWDelay);
61
62    // Setup wire to write information back to previous stages.
63    toRename = timeBuffer->getWire(0);
64
65    // Instruction queue also needs main time buffer.
66    instQueue.setTimeBuffer(tb_ptr);
67}
68
69template<class Impl, class IQ>
70void
71SimpleIEW<Impl, IQ>::setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr)
72{
73    DPRINTF(IEW, "IEW: Setting rename queue pointer.\n");
74    renameQueue = rq_ptr;
75
76    // Setup wire to read information from rename queue.
77    fromRename = renameQueue->getWire(-renameToIEWDelay);
78}
79
80template<class Impl, class IQ>
81void
82SimpleIEW<Impl, IQ>::setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr)
83{
84    DPRINTF(IEW, "IEW: Setting IEW queue pointer.\n");
85    iewQueue = iq_ptr;
86
87    // Setup wire to write instructions to commit.
88    toCommit = iewQueue->getWire(0);
89}
90
91template<class Impl, class IQ>
92void
93SimpleIEW<Impl, IQ>::setRenameMap(RenameMap *rm_ptr)
94{
95    DPRINTF(IEW, "IEW: Setting rename map pointer.\n");
96    renameMap = rm_ptr;
97}
98
99template<class Impl, class IQ>
100void
101SimpleIEW<Impl, IQ>::wakeDependents(DynInstPtr &inst)
102{
103    instQueue.wakeDependents(inst);
104}
105
106template<class Impl, class IQ>
107void
108SimpleIEW<Impl, IQ>::block()
109{
110    DPRINTF(IEW, "IEW: Blocking.\n");
111    // Set the status to Blocked.
112    _status = Blocked;
113
114    // Add the current inputs to the skid buffer so they can be
115    // reprocessed when this stage unblocks.
116    skidBuffer.push(*fromRename);
117
118    // Note that this stage only signals previous stages to stall when
119    // it is the cause of the stall originates at this stage.  Otherwise
120    // the previous stages are expected to check all possible stall signals.
121}
122
123template<class Impl, class IQ>
124inline void
125SimpleIEW<Impl, IQ>::unblock()
126{
127    // Check if there's information in the skid buffer.  If there is, then
128    // set status to unblocking, otherwise set it directly to running.
129    DPRINTF(IEW, "IEW: Reading instructions out of the skid "
130            "buffer.\n");
131    // Remove the now processed instructions from the skid buffer.
132    skidBuffer.pop();
133
134    // If there's still information in the skid buffer, then
135    // continue to tell previous stages to stall.  They will be
136    // able to restart once the skid buffer is empty.
137    if (!skidBuffer.empty()) {
138        toRename->iewInfo.stall = true;
139    } else {
140        DPRINTF(IEW, "IEW: Stage is done unblocking.\n");
141        _status = Running;
142    }
143}
144
145template<class Impl, class IQ>
146void
147SimpleIEW<Impl, IQ>::squash()
148{
149    DPRINTF(IEW, "IEW: Squashing all instructions.\n");
150    _status = Squashing;
151
152    // Tell the IQ to start squashing.
153    instQueue.squash();
154
155    // Tell the LDSTQ to start squashing.
156    ldstQueue.squash(fromCommit->commitInfo.doneSeqNum);
157}
158
159template<class Impl, class IQ>
160void
161SimpleIEW<Impl, IQ>::squash(DynInstPtr &inst)
162{
163    DPRINTF(IEW, "IEW: Squashing from a specific instruction, PC: %#x.\n",
164            inst->PC);
165    // Perhaps leave the squashing up to the ROB stage to tell it when to
166    // squash?
167    _status = Squashing;
168
169    // Tell rename to squash through the time buffer.
170    toRename->iewInfo.squash = true;
171    // Also send PC update information back to prior stages.
172    toRename->iewInfo.squashedSeqNum = inst->seqNum;
173    toRename->iewInfo.mispredPC = inst->readPC();
174    toRename->iewInfo.nextPC = inst->readCalcTarg();
175    toRename->iewInfo.branchMispredict = true;
176    // Prediction was incorrect, so send back inverse.
177    toRename->iewInfo.branchTaken = !(inst->predTaken());
178}
179
180template<class Impl, class IQ>
181void
182SimpleIEW<Impl, IQ>::tick()
183{
184    // Considering putting all the state-determining stuff in this section.
185
186    // Try to fill up issue queue with as many instructions as bandwidth
187    // allows.
188    // Decode should try to execute as many instructions as its bandwidth
189    // will allow, as long as it is not currently blocked.
190
191    // Check if the stage is in a running status.
192    if (_status != Blocked && _status != Squashing) {
193        DPRINTF(IEW, "IEW: Status is not blocked, attempting to run "
194                     "stage.\n");
195        iew();
196
197        // If it's currently unblocking, check to see if it should switch
198        // to running.
199        if (_status == Unblocking) {
200            unblock();
201        }
202    } else if (_status == Squashing) {
203
204        DPRINTF(IEW, "IEW: Still squashing.\n");
205
206        // Check if stage should remain squashing.  Stop squashing if the
207        // squash signal clears.
208        if (!fromCommit->commitInfo.squash &&
209            !fromCommit->commitInfo.robSquashing) {
210            DPRINTF(IEW, "IEW: Done squashing, changing status to "
211                    "running.\n");
212
213            _status = Running;
214            instQueue.stopSquash();
215        } else {
216            instQueue.doSquash();
217        }
218
219        // Also should advance its own time buffers if the stage ran.
220        // Not sure about this...
221//        issueToExecQueue.advance();
222    } else if (_status == Blocked) {
223        // Continue to tell previous stage to stall.
224        toRename->iewInfo.stall = true;
225
226        // Check if possible stall conditions have cleared.
227        if (!fromCommit->commitInfo.stall &&
228            !instQueue.isFull()) {
229            DPRINTF(IEW, "IEW: Stall signals cleared, going to unblock.\n");
230            _status = Unblocking;
231        }
232
233        // If there's still instructions coming from rename, continue to
234        // put them on the skid buffer.
235        if (fromRename->insts[0]) {
236            block();
237        }
238
239        if (fromCommit->commitInfo.squash ||
240            fromCommit->commitInfo.robSquashing) {
241            squash();
242        }
243    }
244
245    // @todo: Maybe put these at the beginning, so if it's idle it can
246    // return early.
247    // Write back number of free IQ entries here.
248    toRename->iewInfo.freeIQEntries = instQueue.numFreeEntries();
249
250    // Check the committed load/store signals to see if there's a load
251    // or store to commit.  Also check if it's being told to execute a
252    // nonspeculative instruction.
253    if (fromCommit->commitInfo.commitIsStore) {
254        ldstQueue.commitStores(fromCommit->commitInfo.doneSeqNum);
255    } else if (fromCommit->commitInfo.commitIsLoad) {
256        ldstQueue.commitLoads(fromCommit->commitInfo.doneSeqNum);
257    }
258
259    if (fromCommit->commitInfo.nonSpecSeqNum != 0) {
260        instQueue.scheduleNonSpec(fromCommit->commitInfo.nonSpecSeqNum);
261    }
262
263    DPRINTF(IEW, "IEW: IQ has %i free entries.\n",
264            instQueue.numFreeEntries());
265}
266
267template<class Impl, class IQ>
268void
269SimpleIEW<Impl, IQ>::iew()
270{
271    // Might want to put all state checks in the tick() function.
272    // Check if being told to stall from commit.
273    if (fromCommit->commitInfo.stall) {
274        block();
275        return;
276    } else if (fromCommit->commitInfo.squash ||
277               fromCommit->commitInfo.robSquashing) {
278        // Also check if commit is telling this stage to squash.
279        squash();
280        return;
281    }
282
283    ////////////////////////////////////////
284    // DISPATCH/ISSUE stage
285    ////////////////////////////////////////
286
287    //Put into its own function?
288    //Add instructions to IQ if there are any instructions there
289
290    // Check if there are any instructions coming from rename, and we're.
291    // not squashing.
292    if (fromRename->insts[0] && _status != Squashing) {
293
294        // Loop through the instructions, putting them in the instruction
295        // queue.
296        for (int inst_num = 0; inst_num < issueReadWidth; ++inst_num)
297        {
298            DynInstPtr inst = fromRename->insts[inst_num];
299
300            // Make sure there's a valid instruction there.
301            if (!inst)
302                break;
303
304            DPRINTF(IEW, "IEW: Issue: Adding PC %#x to IQ.\n",
305                    inst->readPC());
306
307            // If it's a memory reference, don't put it in the
308            // instruction queue.  These will only be executed at commit.
309            // Do the same for nonspeculative instructions and nops.
310            // Be sure to mark these instructions as ready so that the
311            // commit stage can go ahead and execute them, and mark
312            // them as issued so the IQ doesn't reprocess them.
313            if (inst->isSquashed()) {
314                continue;
315            } else if (inst->isLoad()) {
316                DPRINTF(IEW, "IEW: Issue: Memory instruction "
317                        "encountered, adding to LDSTQ.\n");
318
319                // Reserve a spot in the load store queue for this
320                // memory access.
321                ldstQueue.insertLoad(inst);
322
323            } else if (inst->isStore()) {
324                ldstQueue.insertStore(inst);
325
326                // A bit of a hack.  Set that it can commit so that
327                // the commit stage will try committing it, and then
328                // once commit realizes it's a store it will send back
329                // a signal to this stage to issue and execute that
330                // store.
331                inst->setCanCommit();
332
333                instQueue.insertNonSpec(inst);
334                continue;
335            } else if (inst->isNonSpeculative()) {
336                DPRINTF(IEW, "IEW: Issue: Nonspeculative instruction "
337                        "encountered, skipping.\n");
338
339                // Same hack as with stores.
340                inst->setCanCommit();
341
342                // Specificall insert it as nonspeculative.
343                instQueue.insertNonSpec(inst);
344
345                continue;
346            } else if (inst->isNop()) {
347                DPRINTF(IEW, "IEW: Issue: Nop instruction encountered "
348                        ", skipping.\n");
349
350                inst->setIssued();
351                inst->setExecuted();
352                inst->setCanCommit();
353
354                instQueue.advanceTail(inst);
355                continue;
356            } else if (instQueue.isFull()) {
357                DPRINTF(IEW, "IEW: Issue: IQ has become full.\n");
358                // Call function to start blocking.
359                block();
360                // Tell previous stage to stall.
361                toRename->iewInfo.stall = true;
362                break;
363            }
364
365            // If the instruction queue is not full, then add the
366            // instruction.
367            instQueue.insert(fromRename->insts[inst_num]);
368        }
369    }
370
371    // Have the instruction queue try to schedule any ready instructions.
372    instQueue.scheduleReadyInsts();
373
374    ////////////////////////////////////////
375    //EXECUTE/WRITEBACK stage
376    ////////////////////////////////////////
377
378    //Put into its own function?
379    //Similarly should probably have separate execution for int vs FP.
380    // Above comment is handled by the issue queue only issuing a valid
381    // mix of int/fp instructions.
382    //Actually okay to just have one execution, buuuuuut will need
383    //somewhere that defines the execution latency of all instructions.
384    // @todo: Move to the FU pool used in the current full cpu.
385
386    int fu_usage = 0;
387    bool fetch_redirect = false;
388
389    // Execute/writeback any instructions that are available.
390    for (int inst_num = 0;
391         fu_usage < executeWidth && /* Haven't exceeded available FU's. */
392         inst_num < issueWidth && /* Haven't exceeded issue width. */
393         fromIssue->insts[inst_num]; /* There are available instructions. */
394         ++inst_num) {
395        DPRINTF(IEW, "IEW: Execute: Executing instructions from IQ.\n");
396
397        // Get instruction from issue's queue.
398        DynInstPtr inst = fromIssue->insts[inst_num];
399
400        DPRINTF(IEW, "IEW: Execute: Processing PC %#x.\n", inst->readPC());
401
402        // Check if the instruction is squashed; if so then skip it
403        // and don't count it towards the FU usage.
404        if (inst->isSquashed()) {
405            DPRINTF(IEW, "IEW: Execute: Instruction was squashed.\n");
406
407            // Consider this instruction executed so that commit can go
408            // ahead and retire the instruction.
409            inst->setExecuted();
410
411            toCommit->insts[inst_num] = inst;
412
413            continue;
414        }
415
416        inst->setExecuted();
417
418        // If an instruction is executed, then count it towards FU usage.
419        ++fu_usage;
420
421        // Execute instruction.
422        // Note that if the instruction faults, it will be handled
423        // at the commit stage.
424        if (inst->isMemRef()) {
425            DPRINTF(IEW, "IEW: Execute: Calculating address for memory "
426                    "reference.\n");
427
428            // Tell the LDSTQ to execute this instruction (if it is a load).
429            if (inst->isLoad()) {
430                ldstQueue.executeLoad(inst);
431            } else if (inst->isStore()) {
432                ldstQueue.executeStore();
433            } else {
434                panic("IEW: Unexpected memory type!\n");
435            }
436
437        } else {
438            inst->execute();
439        }
440
441        // First check the time slot that this instruction will write
442        // to.  If there are free write ports at the time, then go ahead
443        // and write the instruction to that time.  If there are not,
444        // keep looking back to see where's the first time there's a
445        // free slot.  What happens if you run out of free spaces?
446        // For now naively assume that all instructions take one cycle.
447        // Otherwise would have to look into the time buffer based on the
448        // latency of the instruction.
449
450        // Add finished instruction to queue to commit.
451        toCommit->insts[inst_num] = inst;
452
453        // Check if branch was correct.  This check happens after the
454        // instruction is added to the queue because even if the branch
455        // is mispredicted, the branch instruction itself is still valid.
456        // Only handle this if there hasn't already been something that
457        // redirects fetch in this group of instructions.
458        if (!fetch_redirect) {
459            if (inst->mispredicted()) {
460                fetch_redirect = true;
461
462                DPRINTF(IEW, "IEW: Execute: Branch mispredict detected.\n");
463                DPRINTF(IEW, "IEW: Execute: Redirecting fetch to PC: %#x.\n",
464                        inst->nextPC);
465
466                // If incorrect, then signal the ROB that it must be squashed.
467                squash(inst);
468            } else if (ldstQueue.violation()) {
469                fetch_redirect = true;
470
471                DynInstPtr violator = ldstQueue.getMemDepViolator();
472
473                DPRINTF(IEW, "IEW: LDSTQ detected a violation.  Violator PC: "
474                        "%#x, inst PC: %#x.  Addr is: %#x.\n",
475                        violator->readPC(), inst->readPC(), inst->physEffAddr);
476
477                instQueue.violation(inst, violator);
478
479                squash(inst);
480                // Otherwise check if there was a memory ordering violation.
481                // If there was, then signal ROB that it must be squashed.  Also
482                // signal IQ that there was a violation.
483            }
484        }
485    }
486
487    // Loop through the head of the time buffer and wake any dependents.
488    // These instructions are about to write back.  In the simple model
489    // this loop can really happen within the previous loop, but when
490    // instructions have actual latencies, this loop must be separate.
491    // Also mark scoreboard that this instruction is finally complete.
492    // Either have IEW have direct access to rename map, or have this as
493    // part of backwards communication.
494    for (int inst_num = 0; inst_num < executeWidth &&
495             toCommit->insts[inst_num]; inst_num++)
496    {
497        DynInstPtr inst = toCommit->insts[inst_num];
498
499        DPRINTF(IEW, "IEW: Sending instructions to commit, PC %#x.\n",
500                inst->readPC());
501
502        if(!inst->isSquashed()) {
503            instQueue.wakeDependents(inst);
504
505            for (int i = 0; i < inst->numDestRegs(); i++)
506            {
507                renameMap->markAsReady(inst->renamedDestRegIdx(i));
508            }
509        }
510    }
511
512    // Also should advance its own time buffers if the stage ran.
513    // Not the best place for it, but this works (hopefully).
514    issueToExecQueue.advance();
515}
516