2 * Copyright (c) 2012 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2004-2006 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #ifndef __CPU_O3_ROB_IMPL_HH__
42 #define __CPU_O3_ROB_IMPL_HH__
46 #include "base/logging.hh"
47 #include "cpu/o3/rob.hh"
48 #include "debug/Fetch.hh"
49 #include "debug/ROB.hh"
50 #include "params/DerivO3CPU.hh"
55 ROB<Impl>::ROB(O3CPU *_cpu, DerivO3CPUParams *params)
56 : robPolicy(params->smtROBPolicy),
58 numEntries(params->numROBEntries),
59 squashWidth(params->squashWidth),
61 numThreads(params->numThreads)
63 //Figure out rob policy
64 if (robPolicy == SMTQueuePolicy::Dynamic) {
65 //Set Max Entries to Total ROB Capacity
66 for (ThreadID tid = 0; tid < numThreads; tid++) {
67 maxEntries[tid] = numEntries;
70 } else if (robPolicy == SMTQueuePolicy::Partitioned) {
71 DPRINTF(Fetch, "ROB sharing policy set to Partitioned\n");
73 //@todo:make work if part_amt doesnt divide evenly.
74 int part_amt = numEntries / numThreads;
76 //Divide ROB up evenly
77 for (ThreadID tid = 0; tid < numThreads; tid++) {
78 maxEntries[tid] = part_amt;
81 } else if (robPolicy == SMTQueuePolicy::Threshold) {
82 DPRINTF(Fetch, "ROB sharing policy set to Threshold\n");
84 int threshold = params->smtROBThreshold;;
86 //Divide up by threshold amount
87 for (ThreadID tid = 0; tid < numThreads; tid++) {
88 maxEntries[tid] = threshold;
92 for (ThreadID tid = numThreads; tid < Impl::MaxThreads; tid++) {
101 ROB<Impl>::resetState()
103 for (ThreadID tid = 0; tid < Impl::MaxThreads; tid++) {
104 threadEntries[tid] = 0;
105 squashIt[tid] = instList[tid].end();
106 squashedSeqNum[tid] = 0;
107 doneSquashing[tid] = true;
111 // Initialize the "universal" ROB head & tail point to invalid
113 head = instList[0].end();
114 tail = instList[0].end();
117 template <class Impl>
119 ROB<Impl>::name() const
121 return cpu->name() + ".rob";
124 template <class Impl>
126 ROB<Impl>::setActiveThreads(list<ThreadID> *at_ptr)
128 DPRINTF(ROB, "Setting active threads list pointer.\n");
129 activeThreads = at_ptr;
132 template <class Impl>
134 ROB<Impl>::drainSanityCheck() const
136 for (ThreadID tid = 0; tid < numThreads; tid++)
137 assert(instList[tid].empty());
141 template <class Impl>
143 ROB<Impl>::takeOverFrom()
148 template <class Impl>
150 ROB<Impl>::resetEntries()
152 if (robPolicy != SMTQueuePolicy::Dynamic || numThreads > 1) {
153 auto active_threads = activeThreads->size();
155 list<ThreadID>::iterator threads = activeThreads->begin();
156 list<ThreadID>::iterator end = activeThreads->end();
158 while (threads != end) {
159 ThreadID tid = *threads++;
161 if (robPolicy == SMTQueuePolicy::Partitioned) {
162 maxEntries[tid] = numEntries / active_threads;
163 } else if (robPolicy == SMTQueuePolicy::Threshold &&
164 active_threads == 1) {
165 maxEntries[tid] = numEntries;
171 template <class Impl>
173 ROB<Impl>::entryAmount(ThreadID num_threads)
175 if (robPolicy == SMTQueuePolicy::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(const DynInstPtr &inst)
209 DPRINTF(ROB, "Adding inst PC %s to the ROB.\n", inst->pcState());
211 assert(numInstsInROB != numEntries);
213 ThreadID tid = inst->threadNumber;
215 instList[tid].push_back(inst);
217 //Set Up head iterator if this is the 1st instruction in the ROB
218 if (numInstsInROB == 0) {
219 head = instList[tid].begin();
220 assert((*head) == inst);
223 //Must Decrement for iterator to actually be valid since __.end()
224 //actually points to 1 after the last inst
225 tail = instList[tid].end();
231 ++threadEntries[tid];
233 assert((*tail) == inst);
235 DPRINTF(ROB, "[tid:%i] Now has %d instructions.\n", tid, threadEntries[tid]);
238 template <class Impl>
240 ROB<Impl>::retireHead(ThreadID tid)
244 assert(numInstsInROB > 0);
246 // Get the head ROB instruction by copying it and remove it from the list
247 InstIt head_it = instList[tid].begin();
249 DynInstPtr head_inst = std::move(*head_it);
250 instList[tid].erase(head_it);
252 assert(head_inst->readyToCommit());
254 DPRINTF(ROB, "[tid:%i] Retiring head instruction, "
255 "instruction PC %s, [sn:%llu]\n", tid, head_inst->pcState(),
259 --threadEntries[tid];
261 head_inst->clearInROB();
262 head_inst->setCommitted();
264 //Update "Global" Head of ROB
267 // @todo: A special case is needed if the instruction being
268 // retired is the only instruction in the ROB; otherwise the tail
269 // iterator will become invalidated.
270 cpu->removeFrontInst(head_inst);
273 template <class Impl>
275 ROB<Impl>::isHeadReady(ThreadID tid)
278 if (threadEntries[tid] != 0) {
279 return instList[tid].front()->readyToCommit();
285 template <class Impl>
287 ROB<Impl>::canCommit()
289 //@todo: set ActiveThreads through ROB or CPU
290 list<ThreadID>::iterator threads = activeThreads->begin();
291 list<ThreadID>::iterator end = activeThreads->end();
293 while (threads != end) {
294 ThreadID tid = *threads++;
296 if (isHeadReady(tid)) {
304 template <class Impl>
306 ROB<Impl>::numFreeEntries()
308 return numEntries - numInstsInROB;
311 template <class Impl>
313 ROB<Impl>::numFreeEntries(ThreadID tid)
315 return maxEntries[tid] - threadEntries[tid];
318 template <class Impl>
320 ROB<Impl>::doSquash(ThreadID tid)
323 DPRINTF(ROB, "[tid:%i] Squashing instructions until [sn:%llu].\n",
324 tid, squashedSeqNum[tid]);
326 assert(squashIt[tid] != instList[tid].end());
328 if ((*squashIt[tid])->seqNum < squashedSeqNum[tid]) {
329 DPRINTF(ROB, "[tid:%i] Done squashing instructions.\n",
332 squashIt[tid] = instList[tid].end();
334 doneSquashing[tid] = true;
338 bool robTailUpdate = false;
340 for (int numSquashed = 0;
341 numSquashed < squashWidth &&
342 squashIt[tid] != instList[tid].end() &&
343 (*squashIt[tid])->seqNum > squashedSeqNum[tid];
346 DPRINTF(ROB, "[tid:%i] Squashing instruction PC %s, seq num %i.\n",
347 (*squashIt[tid])->threadNumber,
348 (*squashIt[tid])->pcState(),
349 (*squashIt[tid])->seqNum);
351 // Mark the instruction as squashed, and ready to commit so that
352 // it can drain out of the pipeline.
353 (*squashIt[tid])->setSquashed();
355 (*squashIt[tid])->setCanCommit();
358 if (squashIt[tid] == instList[tid].begin()) {
359 DPRINTF(ROB, "Reached head of instruction list while "
362 squashIt[tid] = instList[tid].end();
364 doneSquashing[tid] = true;
369 InstIt tail_thread = instList[tid].end();
372 if ((*squashIt[tid]) == (*tail_thread))
373 robTailUpdate = true;
379 // Check if ROB is done squashing.
380 if ((*squashIt[tid])->seqNum <= squashedSeqNum[tid]) {
381 DPRINTF(ROB, "[tid:%i] Done squashing instructions.\n",
384 squashIt[tid] = instList[tid].end();
386 doneSquashing[tid] = true;
395 template <class Impl>
397 ROB<Impl>::updateHead()
399 InstSeqNum lowest_num = 0;
400 bool first_valid = true;
402 // @todo: set ActiveThreads through ROB or CPU
403 list<ThreadID>::iterator threads = activeThreads->begin();
404 list<ThreadID>::iterator end = activeThreads->end();
406 while (threads != end) {
407 ThreadID tid = *threads++;
409 if (instList[tid].empty())
413 head = instList[tid].begin();
414 lowest_num = (*head)->seqNum;
419 InstIt head_thread = instList[tid].begin();
421 DynInstPtr head_inst = (*head_thread);
423 assert(head_inst != 0);
425 if (head_inst->seqNum < lowest_num) {
427 lowest_num = head_inst->seqNum;
432 head = instList[0].end();
437 template <class Impl>
439 ROB<Impl>::updateTail()
441 tail = instList[0].end();
442 bool first_valid = true;
444 list<ThreadID>::iterator threads = activeThreads->begin();
445 list<ThreadID>::iterator end = activeThreads->end();
447 while (threads != end) {
448 ThreadID tid = *threads++;
450 if (instList[tid].empty()) {
454 // If this is the first valid then assign w/out
457 tail = instList[tid].end();
463 // Assign new tail if this thread's tail is younger
464 // than our current "tail high"
465 InstIt tail_thread = instList[tid].end();
468 if ((*tail_thread)->seqNum > (*tail)->seqNum) {
475 template <class Impl>
477 ROB<Impl>::squash(InstSeqNum squash_num, ThreadID tid)
480 DPRINTF(ROB, "Does not need to squash due to being empty "
487 DPRINTF(ROB, "Starting to squash within the ROB.\n");
489 robStatus[tid] = ROBSquashing;
491 doneSquashing[tid] = false;
493 squashedSeqNum[tid] = squash_num;
495 if (!instList[tid].empty()) {
496 InstIt tail_thread = instList[tid].end();
499 squashIt[tid] = tail_thread;
505 template <class Impl>
506 const typename Impl::DynInstPtr&
507 ROB<Impl>::readHeadInst(ThreadID tid)
509 if (threadEntries[tid] != 0) {
510 InstIt head_thread = instList[tid].begin();
512 assert((*head_thread)->isInROB());
520 template <class Impl>
521 typename Impl::DynInstPtr
522 ROB<Impl>::readTailInst(ThreadID tid)
524 InstIt tail_thread = instList[tid].end();
530 template <class Impl>
532 ROB<Impl>::regStats()
534 using namespace Stats;
536 .name(name() + ".rob_reads")
537 .desc("The number of ROB reads");
540 .name(name() + ".rob_writes")
541 .desc("The number of ROB writes");
544 template <class Impl>
545 typename Impl::DynInstPtr
546 ROB<Impl>::findInst(ThreadID tid, InstSeqNum squash_inst)
548 for (InstIt it = instList[tid].begin(); it != instList[tid].end(); it++) {
549 if ((*it)->seqNum == squash_inst) {
556 #endif//__CPU_O3_ROB_IMPL_HH__