2 * Copyright (c) 2004-2006 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.
34 #include "config/full_system.hh"
35 #include "cpu/o3/rob.hh"
40 ROB<Impl>::ROB(O3CPU *_cpu, unsigned _numEntries, unsigned _squashWidth,
41 std::string _smtROBPolicy, unsigned _smtROBThreshold,
44 numEntries(_numEntries),
45 squashWidth(_squashWidth),
47 numThreads(_numThreads)
49 for (ThreadID tid = 0; tid < numThreads; tid++) {
50 squashedSeqNum[tid] = 0;
51 doneSquashing[tid] = true;
52 threadEntries[tid] = 0;
55 std::string policy = _smtROBPolicy;
57 //Convert string to lowercase
58 std::transform(policy.begin(), policy.end(), policy.begin(),
59 (int(*)(int)) tolower);
61 //Figure out rob policy
62 if (policy == "dynamic") {
65 //Set Max Entries to Total ROB Capacity
66 for (ThreadID tid = 0; tid < numThreads; tid++) {
67 maxEntries[tid] = numEntries;
70 } else if (policy == "partitioned") {
71 robPolicy = Partitioned;
72 DPRINTF(Fetch, "ROB sharing policy set to Partitioned\n");
74 //@todo:make work if part_amt doesnt divide evenly.
75 int part_amt = numEntries / numThreads;
77 //Divide ROB up evenly
78 for (ThreadID tid = 0; tid < numThreads; tid++) {
79 maxEntries[tid] = part_amt;
82 } else if (policy == "threshold") {
83 robPolicy = Threshold;
84 DPRINTF(Fetch, "ROB sharing policy set to Threshold\n");
86 int threshold = _smtROBThreshold;;
88 //Divide up by threshold amount
89 for (ThreadID tid = 0; tid < numThreads; tid++) {
90 maxEntries[tid] = threshold;
93 assert(0 && "Invalid ROB Sharing Policy.Options Are:{Dynamic,"
94 "Partitioned, Threshold}");
97 // Set the per-thread iterators to the end of the instruction list.
98 for (ThreadID tid = 0; tid < numThreads; tid++) {
99 squashIt[tid] = instList[tid].end();
102 // Initialize the "universal" ROB head & tail point to invalid
104 head = instList[0].end();
105 tail = instList[0].end();
108 template <class Impl>
110 ROB<Impl>::name() const
112 return cpu->name() + ".rob";
115 template <class Impl>
117 ROB<Impl>::setActiveThreads(list<ThreadID> *at_ptr)
119 DPRINTF(ROB, "Setting active threads list pointer.\n");
120 activeThreads = at_ptr;
123 template <class Impl>
125 ROB<Impl>::switchOut()
127 for (ThreadID tid = 0; tid < numThreads; tid++) {
128 instList[tid].clear();
132 template <class Impl>
134 ROB<Impl>::takeOverFrom()
136 for (ThreadID tid = 0; tid < numThreads; tid++) {
137 doneSquashing[tid] = true;
138 threadEntries[tid] = 0;
139 squashIt[tid] = instList[tid].end();
143 // Initialize the "universal" ROB head & tail point to invalid
145 head = instList[0].end();
146 tail = instList[0].end();
149 template <class Impl>
151 ROB<Impl>::resetEntries()
153 if (robPolicy != Dynamic || numThreads > 1) {
154 int active_threads = activeThreads->size();
156 list<ThreadID>::iterator threads = activeThreads->begin();
157 list<ThreadID>::iterator end = activeThreads->end();
159 while (threads != end) {
160 ThreadID tid = *threads++;
162 if (robPolicy == Partitioned) {
163 maxEntries[tid] = numEntries / active_threads;
164 } else if (robPolicy == Threshold && active_threads == 1) {
165 maxEntries[tid] = numEntries;
171 template <class Impl>
173 ROB<Impl>::entryAmount(ThreadID num_threads)
175 if (robPolicy == Partitioned) {
176 return numEntries / num_threads;
182 template <class Impl>
184 ROB<Impl>::countInsts()
188 for (ThreadID tid = 0; tid < numThreads; tid++)
189 total += countInsts(tid);
194 template <class Impl>
196 ROB<Impl>::countInsts(ThreadID tid)
198 return instList[tid].size();
201 template <class Impl>
203 ROB<Impl>::insertInst(DynInstPtr &inst)
207 DPRINTF(ROB, "Adding inst PC %#x to the ROB.\n", inst->readPC());
209 assert(numInstsInROB != numEntries);
211 ThreadID tid = inst->threadNumber;
213 instList[tid].push_back(inst);
215 //Set Up head iterator if this is the 1st instruction in the ROB
216 if (numInstsInROB == 0) {
217 head = instList[tid].begin();
218 assert((*head) == inst);
221 //Must Decrement for iterator to actually be valid since __.end()
222 //actually points to 1 after the last inst
223 tail = instList[tid].end();
229 ++threadEntries[tid];
231 assert((*tail) == inst);
233 DPRINTF(ROB, "[tid:%i] Now has %d instructions.\n", tid, threadEntries[tid]);
236 template <class Impl>
238 ROB<Impl>::retireHead(ThreadID tid)
240 assert(numInstsInROB > 0);
242 // Get the head ROB instruction.
243 InstIt head_it = instList[tid].begin();
245 DynInstPtr head_inst = (*head_it);
247 assert(head_inst->readyToCommit());
249 DPRINTF(ROB, "[tid:%u]: Retiring head instruction, "
250 "instruction PC %#x,[sn:%lli]\n", tid, head_inst->readPC(),
254 --threadEntries[tid];
256 head_inst->clearInROB();
257 head_inst->setCommitted();
259 instList[tid].erase(head_it);
261 //Update "Global" Head of ROB
264 // @todo: A special case is needed if the instruction being
265 // retired is the only instruction in the ROB; otherwise the tail
266 // iterator will become invalidated.
267 cpu->removeFrontInst(head_inst);
270 template <class Impl>
272 ROB<Impl>::isHeadReady(ThreadID tid)
274 if (threadEntries[tid] != 0) {
275 return instList[tid].front()->readyToCommit();
281 template <class Impl>
283 ROB<Impl>::canCommit()
285 //@todo: set ActiveThreads through ROB or CPU
286 list<ThreadID>::iterator threads = activeThreads->begin();
287 list<ThreadID>::iterator end = activeThreads->end();
289 while (threads != end) {
290 ThreadID tid = *threads++;
292 if (isHeadReady(tid)) {
300 template <class Impl>
302 ROB<Impl>::numFreeEntries()
304 return numEntries - numInstsInROB;
307 template <class Impl>
309 ROB<Impl>::numFreeEntries(ThreadID tid)
311 return maxEntries[tid] - threadEntries[tid];
314 template <class Impl>
316 ROB<Impl>::doSquash(ThreadID tid)
318 DPRINTF(ROB, "[tid:%u]: Squashing instructions until [sn:%i].\n",
319 tid, squashedSeqNum[tid]);
321 assert(squashIt[tid] != instList[tid].end());
323 if ((*squashIt[tid])->seqNum < squashedSeqNum[tid]) {
324 DPRINTF(ROB, "[tid:%u]: Done squashing instructions.\n",
327 squashIt[tid] = instList[tid].end();
329 doneSquashing[tid] = true;
333 bool robTailUpdate = false;
335 for (int numSquashed = 0;
336 numSquashed < squashWidth &&
337 squashIt[tid] != instList[tid].end() &&
338 (*squashIt[tid])->seqNum > squashedSeqNum[tid];
341 DPRINTF(ROB, "[tid:%u]: Squashing instruction PC %#x, seq num %i.\n",
342 (*squashIt[tid])->threadNumber,
343 (*squashIt[tid])->readPC(),
344 (*squashIt[tid])->seqNum);
346 // Mark the instruction as squashed, and ready to commit so that
347 // it can drain out of the pipeline.
348 (*squashIt[tid])->setSquashed();
350 (*squashIt[tid])->setCanCommit();
353 if (squashIt[tid] == instList[tid].begin()) {
354 DPRINTF(ROB, "Reached head of instruction list while "
357 squashIt[tid] = instList[tid].end();
359 doneSquashing[tid] = true;
364 InstIt tail_thread = instList[tid].end();
367 if ((*squashIt[tid]) == (*tail_thread))
368 robTailUpdate = true;
374 // Check if ROB is done squashing.
375 if ((*squashIt[tid])->seqNum <= squashedSeqNum[tid]) {
376 DPRINTF(ROB, "[tid:%u]: Done squashing instructions.\n",
379 squashIt[tid] = instList[tid].end();
381 doneSquashing[tid] = true;
390 template <class Impl>
392 ROB<Impl>::updateHead()
394 DynInstPtr head_inst;
395 InstSeqNum lowest_num = 0;
396 bool first_valid = true;
398 // @todo: set ActiveThreads through ROB or CPU
399 list<ThreadID>::iterator threads = activeThreads->begin();
400 list<ThreadID>::iterator end = activeThreads->end();
402 while (threads != end) {
403 ThreadID tid = *threads++;
405 if (instList[tid].empty())
409 head = instList[tid].begin();
410 lowest_num = (*head)->seqNum;
415 InstIt head_thread = instList[tid].begin();
417 DynInstPtr head_inst = (*head_thread);
419 assert(head_inst != 0);
421 if (head_inst->seqNum < lowest_num) {
423 lowest_num = head_inst->seqNum;
428 head = instList[0].end();
433 template <class Impl>
435 ROB<Impl>::updateTail()
437 tail = instList[0].end();
438 bool first_valid = true;
440 list<ThreadID>::iterator threads = activeThreads->begin();
441 list<ThreadID>::iterator end = activeThreads->end();
443 while (threads != end) {
444 ThreadID tid = *threads++;
446 if (instList[tid].empty()) {
450 // If this is the first valid then assign w/out
453 tail = instList[tid].end();
459 // Assign new tail if this thread's tail is younger
460 // than our current "tail high"
461 InstIt tail_thread = instList[tid].end();
464 if ((*tail_thread)->seqNum > (*tail)->seqNum) {
471 template <class Impl>
473 ROB<Impl>::squash(InstSeqNum squash_num, ThreadID tid)
476 DPRINTF(ROB, "Does not need to squash due to being empty "
483 DPRINTF(ROB, "Starting to squash within the ROB.\n");
485 robStatus[tid] = ROBSquashing;
487 doneSquashing[tid] = false;
489 squashedSeqNum[tid] = squash_num;
491 if (!instList[tid].empty()) {
492 InstIt tail_thread = instList[tid].end();
495 squashIt[tid] = tail_thread;
501 template <class Impl>
502 typename Impl::DynInstPtr
503 ROB<Impl>::readHeadInst(ThreadID tid)
505 if (threadEntries[tid] != 0) {
506 InstIt head_thread = instList[tid].begin();
508 assert((*head_thread)->isInROB()==true);
516 template <class Impl>
517 typename Impl::DynInstPtr
518 ROB<Impl>::readTailInst(ThreadID tid)
520 InstIt tail_thread = instList[tid].end();