2 * Copyright (c) 2007 MIPS Technologies, Inc.
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.
28 * Authors: Korey Sewell
32 #include "config/the_isa.hh"
33 #include "cpu/inorder/resources/fetch_seq_unit.hh"
34 #include "cpu/inorder/resource_pool.hh"
35 #include "debug/InOrderFetchSeq.hh"
36 #include "debug/InOrderStall.hh"
39 using namespace TheISA
;
40 using namespace ThePipeline
;
42 FetchSeqUnit::FetchSeqUnit(std::string res_name
, int res_id
, int res_width
,
43 int res_latency
, InOrderCPU
*_cpu
,
44 ThePipeline::Params
*params
)
45 : Resource(res_name
, res_id
, res_width
, res_latency
, _cpu
),
46 instSize(sizeof(MachInst
))
48 for (ThreadID tid
= 0; tid
< ThePipeline::MaxThreads
; tid
++) {
50 pcBlockStage
[tid
] = 0;
52 squashSeqNum
[tid
] = (InstSeqNum
)-1;
53 lastSquashCycle
[tid
] = 0;
57 FetchSeqUnit::~FetchSeqUnit()
59 delete [] resourceEvent
;
65 resourceEvent
= new FetchSeqEvent
[width
];
67 for (int i
= 0; i
< width
; i
++) {
68 reqs
[i
] = new ResourceRequest(this);
75 FetchSeqUnit::execute(int slot_num
)
77 ResourceRequest
* fs_req
= reqs
[slot_num
];
78 DynInstPtr inst
= fs_req
->inst
;
79 ThreadID tid
= inst
->readTid();
80 int stage_num
= fs_req
->getStageNum();
82 DPRINTF(InOrderFetchSeq
, "[tid:%i]: Current PC is %s\n", tid
,
90 inst
->pcState(pc
[tid
]);
91 inst
->setMemAddr(pc
[tid
].instAddr());
93 // Advance to next PC (typically PC + 4)
96 inst
->setSeqNum(cpu
->getAndIncrementInstSeq(tid
));
98 DPRINTF(InOrderFetchSeq
, "[tid:%i]: Assigning [sn:%i] to "
99 "PC %s\n", tid
, inst
->seqNum
, inst
->pcState());
103 DPRINTF(InOrderStall
, "STALL: [tid:%i]: NPC not valid\n", tid
);
111 assert(!inst
->isCondDelaySlot() &&
112 "Not Handling Conditional Delay Slot");
114 if (inst
->isControl()) {
115 if (inst
->isReturn() && !inst
->predTaken()) {
116 // If it's a return, then we must wait for resolved address.
117 // The Predictor will mark a return a false as "not taken"
118 // if there is no RAS entry
119 DPRINTF(InOrderFetchSeq
, "[tid:%d]: Setting block signal "
122 cpu
->pipelineStage
[stage_num
]->
123 toPrevStages
->stageBlock
[stage_num
][tid
] = true;
124 pcValid
[tid
] = false;
125 pcBlockStage
[tid
] = stage_num
;
126 } else if (inst
->predTaken()) {
128 inst
->setSquashInfo(stage_num
);
129 setupSquash(inst
, stage_num
, tid
);
131 DPRINTF(InOrderFetchSeq
, "[tid:%i] Setting up squash to "
132 "start from stage %i, after [sn:%i].\n",
133 tid
, stage_num
, inst
->squashSeqNum
);
136 DPRINTF(InOrderFetchSeq
, "[tid:%i]: [sn:%i]: Ignoring branch "
137 "target update since then is not a control "
138 "instruction.\n", tid
, inst
->seqNum
);
146 fatal("Unrecognized command to %s", resName
);
151 FetchSeqUnit::squash(DynInstPtr inst
, int squash_stage
,
152 InstSeqNum squash_seq_num
, ThreadID tid
)
154 DPRINTF(InOrderFetchSeq
, "[tid:%i]: Updating due to squash from %s (%s) "
155 "stage %i.\n", tid
, inst
->instName(), inst
->pcState(),
158 if (lastSquashCycle
[tid
] == curTick() &&
159 squashSeqNum
[tid
] <= squash_seq_num
) {
160 DPRINTF(InOrderFetchSeq
, "[tid:%i]: Ignoring squash from stage %i, "
161 "since there is an outstanding squash that is older.\n",
164 squashSeqNum
[tid
] = squash_seq_num
;
165 lastSquashCycle
[tid
] = curTick();
167 if (inst
->fault
!= NoFault
) {
168 // A Trap Caused This Fault and will update the pc state
169 // when done trapping
170 DPRINTF(InOrderFetchSeq
, "[tid:%i] Blocking due to fault @ "
171 "[sn:%i].%s %s \n", inst
->seqNum
,
172 inst
->instName(), inst
->pcState());
173 pcValid
[tid
] = false;
175 TheISA::PCState nextPC
;
176 assert(inst
->staticInst
);
177 if (inst
->isControl()) {
178 nextPC
= inst
->readPredTarg();
180 // If we are already fetching this PC then advance to next PC
182 // This should handle ISAs w/delay slots and annulled delay
183 // slots to figure out which is the next PC to fetch after
185 DynInstPtr bdelay_inst
= NULL
;
187 if (inst
->onInstList
) {
188 bdelay_it
= inst
->getInstListIt();
191 InstSeqNum branch_delay_num
= inst
->seqNum
+ 1;
192 bdelay_it
= cpu
->findInst(branch_delay_num
, tid
);
195 if (bdelay_it
!= cpu
->instList
[tid
].end()) {
196 bdelay_inst
= (*bdelay_it
);
200 DPRINTF(Resource
, "Evaluating %s v. %s\n",
201 bdelay_inst
->pc
, nextPC
);
203 if (bdelay_inst
->pc
.instAddr() == nextPC
.instAddr()) {
204 bdelay_inst
->pc
= nextPC
;
205 advancePC(nextPC
, inst
->staticInst
);
206 DPRINTF(Resource
, "Advanced PC to %s\n", nextPC
);
210 nextPC
= inst
->pcState();
211 advancePC(nextPC
, inst
->staticInst
);
215 DPRINTF(InOrderFetchSeq
, "[tid:%i]: Setting PC to %s.\n",
219 // Unblock Any Stages Waiting for this information to be updated ...
221 DPRINTF(InOrderFetchSeq
, "[tid:%d]: Setting unblock signal "
223 tid
, pcBlockStage
[tid
]);
225 // Need to use "fromNextStages" instead of "toPrevStages"
226 // because the timebuffer will have already have advanced
227 // in the tick function and this squash function will happen after
229 cpu
->pipelineStage
[pcBlockStage
[tid
]]->
230 fromNextStages
->stageUnblock
[pcBlockStage
[tid
]][tid
] = true;
237 Resource::squash(inst
, squash_stage
, squash_seq_num
, tid
);
240 FetchSeqUnit::FetchSeqEvent::FetchSeqEvent()
245 FetchSeqUnit::FetchSeqEvent::process()
247 FetchSeqUnit
* fs_res
= dynamic_cast<FetchSeqUnit
*>(resource
);
250 for (int i
= 0; i
< MaxThreads
; i
++) {
251 fs_res
->pc
[i
] = fs_res
->cpu
->pcState(i
);
252 DPRINTF(InOrderFetchSeq
, "[tid:%i]: Setting PC: %s.\n",
255 fs_res
->pcValid
[i
] = true;
261 FetchSeqUnit::activateThread(ThreadID tid
)
265 pc
[tid
] = cpu
->pcState(tid
);
267 cpu
->fetchPriorityList
.push_back(tid
);
269 DPRINTF(InOrderFetchSeq
, "[tid:%i]: Reading PC: %s.\n",
274 FetchSeqUnit::deactivateThread(ThreadID tid
)
276 pcValid
[tid
] = false;
277 pcBlockStage
[tid
] = 0;
279 squashSeqNum
[tid
] = (InstSeqNum
)-1;
280 lastSquashCycle
[tid
] = 0;
282 list
<ThreadID
>::iterator thread_it
= find(cpu
->fetchPriorityList
.begin(),
283 cpu
->fetchPriorityList
.end(),
286 if (thread_it
!= cpu
->fetchPriorityList
.end())
287 cpu
->fetchPriorityList
.erase(thread_it
);
291 FetchSeqUnit::suspendThread(ThreadID tid
)
293 deactivateThread(tid
);
297 FetchSeqUnit::trap(Fault fault
, ThreadID tid
, DynInstPtr inst
)
300 pc
[tid
] = cpu
->pcState(tid
);
301 DPRINTF(Fault
, "[tid:%i]: Trap updating to PC: "
302 "%s.\n", tid
, pc
[tid
]);
303 DPRINTF(InOrderFetchSeq
, "[tid:%i]: Trap updating to PC: "
304 "%s.\n", tid
, pc
[tid
]);
306 cpu
->removePipelineStalls(tid
);
310 FetchSeqUnit::updateAfterContextSwitch(DynInstPtr inst
, ThreadID tid
)
314 if (cpu
->thread
[tid
]->lastGradIsBranch
) {
315 /** This function assumes that the instruction causing the context
316 * switch was right after the branch. Thus, if it's not, then
317 * we are updating incorrectly here
319 assert(cpu
->nextInstAddr(tid
) == inst
->instAddr());
320 pc
[tid
] = cpu
->thread
[tid
]->lastBranchPC
;
322 pc
[tid
] = inst
->pcState();
324 assert(inst
->staticInst
);
325 advancePC(pc
[tid
], inst
->staticInst
);
327 DPRINTF(InOrderFetchSeq
, "[tid:%i]: Updating PCs due to Context Switch."
328 "Assigning PC: %s.\n", tid
, pc
[tid
]);