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.
44 #ifndef __CPU_O3_ROB_IMPL_HH__
45 #define __CPU_O3_ROB_IMPL_HH__
49 #include "base/logging.hh"
50 #include "cpu/o3/rob.hh"
51 #include "debug/Fetch.hh"
52 #include "debug/ROB.hh"
53 #include "params/DerivO3CPU.hh"
58 ROB<Impl>::ROB(O3CPU *_cpu, DerivO3CPUParams *params)
59 : robPolicy(params->smtROBPolicy),
61 numEntries(params->numROBEntries),
62 squashWidth(params->squashWidth),
64 numThreads(params->numThreads)
66 //Figure out rob policy
67 if (robPolicy == SMTQueuePolicy::Dynamic) {
68 //Set Max Entries to Total ROB Capacity
69 for (ThreadID tid = 0; tid < numThreads; tid++) {
70 maxEntries[tid] = numEntries;
73 } else if (robPolicy == SMTQueuePolicy::Partitioned) {
74 DPRINTF(Fetch, "ROB sharing policy set to Partitioned\n");
76 //@todo:make work if part_amt doesnt divide evenly.
77 int part_amt = numEntries / numThreads;
79 //Divide ROB up evenly
80 for (ThreadID tid = 0; tid < numThreads; tid++) {
81 maxEntries[tid] = part_amt;
84 } else if (robPolicy == SMTQueuePolicy::Threshold) {
85 DPRINTF(Fetch, "ROB sharing policy set to Threshold\n");
87 int threshold = params->smtROBThreshold;;
89 //Divide up by threshold amount
90 for (ThreadID tid = 0; tid < numThreads; tid++) {
91 maxEntries[tid] = threshold;
95 for (ThreadID tid = numThreads; tid < Impl::MaxThreads; tid++) {
102 template <class Impl>
104 ROB<Impl>::resetState()
106 for (ThreadID tid = 0; tid < Impl::MaxThreads; tid++) {
107 threadEntries[tid] = 0;
108 squashIt[tid] = instList[tid].end();
109 squashedSeqNum[tid] = 0;
110 doneSquashing[tid] = true;
114 // Initialize the "universal" ROB head & tail point to invalid
116 head = instList[0].end();
117 tail = instList[0].end();
120 template <class Impl>
122 ROB<Impl>::name() const
124 return cpu->name() + ".rob";
127 template <class Impl>
129 ROB<Impl>::setActiveThreads(list<ThreadID> *at_ptr)
131 DPRINTF(ROB, "Setting active threads list pointer.\n");
132 activeThreads = at_ptr;
135 template <class Impl>
137 ROB<Impl>::drainSanityCheck() const
139 for (ThreadID tid = 0; tid < numThreads; tid++)
140 assert(instList[tid].empty());
144 template <class Impl>
146 ROB<Impl>::takeOverFrom()
151 template <class Impl>
153 ROB<Impl>::resetEntries()
155 if (robPolicy != SMTQueuePolicy::Dynamic || numThreads > 1) {
156 auto active_threads = activeThreads->size();
158 list<ThreadID>::iterator threads = activeThreads->begin();
159 list<ThreadID>::iterator end = activeThreads->end();
161 while (threads != end) {
162 ThreadID tid = *threads++;
164 if (robPolicy == SMTQueuePolicy::Partitioned) {
165 maxEntries[tid] = numEntries / active_threads;
166 } else if (robPolicy == SMTQueuePolicy::Threshold &&
167 active_threads == 1) {
168 maxEntries[tid] = numEntries;
174 template <class Impl>
176 ROB<Impl>::entryAmount(ThreadID num_threads)
178 if (robPolicy == SMTQueuePolicy::Partitioned) {
179 return numEntries / num_threads;
185 template <class Impl>
187 ROB<Impl>::countInsts()
191 for (ThreadID tid = 0; tid < numThreads; tid++)
192 total += countInsts(tid);
197 template <class Impl>
199 ROB<Impl>::countInsts(ThreadID tid)
201 return instList[tid].size();
204 template <class Impl>
206 ROB<Impl>::insertInst(const DynInstPtr &inst)
212 DPRINTF(ROB, "Adding inst PC %s to the ROB.\n", inst->pcState());
214 assert(numInstsInROB != numEntries);
216 ThreadID tid = inst->threadNumber;
218 instList[tid].push_back(inst);
220 //Set Up head iterator if this is the 1st instruction in the ROB
221 if (numInstsInROB == 0) {
222 head = instList[tid].begin();
223 assert((*head) == inst);
226 //Must Decrement for iterator to actually be valid since __.end()
227 //actually points to 1 after the last inst
228 tail = instList[tid].end();
234 ++threadEntries[tid];
236 assert((*tail) == inst);
238 DPRINTF(ROB, "[tid:%i] Now has %d instructions.\n", tid, threadEntries[tid]);
241 template <class Impl>
243 ROB<Impl>::retireHead(ThreadID tid)
247 assert(numInstsInROB > 0);
249 // Get the head ROB instruction by copying it and remove it from the list
250 InstIt head_it = instList[tid].begin();
252 DynInstPtr head_inst = std::move(*head_it);
253 instList[tid].erase(head_it);
255 assert(head_inst->readyToCommit());
257 DPRINTF(ROB, "[tid:%i] Retiring head instruction, "
258 "instruction PC %s, [sn:%llu]\n", tid, head_inst->pcState(),
262 --threadEntries[tid];
264 head_inst->clearInROB();
265 head_inst->setCommitted();
267 //Update "Global" Head of ROB
270 // @todo: A special case is needed if the instruction being
271 // retired is the only instruction in the ROB; otherwise the tail
272 // iterator will become invalidated.
273 cpu->removeFrontInst(head_inst);
276 template <class Impl>
278 ROB<Impl>::isHeadReady(ThreadID tid)
281 if (threadEntries[tid] != 0) {
282 return instList[tid].front()->readyToCommit();
288 template <class Impl>
290 ROB<Impl>::canCommit()
292 //@todo: set ActiveThreads through ROB or CPU
293 list<ThreadID>::iterator threads = activeThreads->begin();
294 list<ThreadID>::iterator end = activeThreads->end();
296 while (threads != end) {
297 ThreadID tid = *threads++;
299 if (isHeadReady(tid)) {
307 template <class Impl>
309 ROB<Impl>::numFreeEntries()
311 return numEntries - numInstsInROB;
314 template <class Impl>
316 ROB<Impl>::numFreeEntries(ThreadID tid)
318 return maxEntries[tid] - threadEntries[tid];
321 template <class Impl>
323 ROB<Impl>::doSquash(ThreadID tid)
326 DPRINTF(ROB, "[tid:%i] Squashing instructions until [sn:%llu].\n",
327 tid, squashedSeqNum[tid]);
329 assert(squashIt[tid] != instList[tid].end());
331 if ((*squashIt[tid])->seqNum < squashedSeqNum[tid]) {
332 DPRINTF(ROB, "[tid:%i] Done squashing instructions.\n",
335 squashIt[tid] = instList[tid].end();
337 doneSquashing[tid] = true;
341 bool robTailUpdate = false;
343 for (int numSquashed = 0;
344 numSquashed < squashWidth &&
345 squashIt[tid] != instList[tid].end() &&
346 (*squashIt[tid])->seqNum > squashedSeqNum[tid];
349 DPRINTF(ROB, "[tid:%i] Squashing instruction PC %s, seq num %i.\n",
350 (*squashIt[tid])->threadNumber,
351 (*squashIt[tid])->pcState(),
352 (*squashIt[tid])->seqNum);
354 // Mark the instruction as squashed, and ready to commit so that
355 // it can drain out of the pipeline.
356 (*squashIt[tid])->setSquashed();
358 (*squashIt[tid])->setCanCommit();
361 if (squashIt[tid] == instList[tid].begin()) {
362 DPRINTF(ROB, "Reached head of instruction list while "
365 squashIt[tid] = instList[tid].end();
367 doneSquashing[tid] = true;
372 InstIt tail_thread = instList[tid].end();
375 if ((*squashIt[tid]) == (*tail_thread))
376 robTailUpdate = true;
382 // Check if ROB is done squashing.
383 if ((*squashIt[tid])->seqNum <= squashedSeqNum[tid]) {
384 DPRINTF(ROB, "[tid:%i] Done squashing instructions.\n",
387 squashIt[tid] = instList[tid].end();
389 doneSquashing[tid] = true;
398 template <class Impl>
400 ROB<Impl>::updateHead()
402 InstSeqNum lowest_num = 0;
403 bool first_valid = true;
405 // @todo: set ActiveThreads through ROB or CPU
406 list<ThreadID>::iterator threads = activeThreads->begin();
407 list<ThreadID>::iterator end = activeThreads->end();
409 while (threads != end) {
410 ThreadID tid = *threads++;
412 if (instList[tid].empty())
416 head = instList[tid].begin();
417 lowest_num = (*head)->seqNum;
422 InstIt head_thread = instList[tid].begin();
424 DynInstPtr head_inst = (*head_thread);
426 assert(head_inst != 0);
428 if (head_inst->seqNum < lowest_num) {
430 lowest_num = head_inst->seqNum;
435 head = instList[0].end();
440 template <class Impl>
442 ROB<Impl>::updateTail()
444 tail = instList[0].end();
445 bool first_valid = true;
447 list<ThreadID>::iterator threads = activeThreads->begin();
448 list<ThreadID>::iterator end = activeThreads->end();
450 while (threads != end) {
451 ThreadID tid = *threads++;
453 if (instList[tid].empty()) {
457 // If this is the first valid then assign w/out
460 tail = instList[tid].end();
466 // Assign new tail if this thread's tail is younger
467 // than our current "tail high"
468 InstIt tail_thread = instList[tid].end();
471 if ((*tail_thread)->seqNum > (*tail)->seqNum) {
478 template <class Impl>
480 ROB<Impl>::squash(InstSeqNum squash_num, ThreadID tid)
483 DPRINTF(ROB, "Does not need to squash due to being empty "
490 DPRINTF(ROB, "Starting to squash within the ROB.\n");
492 robStatus[tid] = ROBSquashing;
494 doneSquashing[tid] = false;
496 squashedSeqNum[tid] = squash_num;
498 if (!instList[tid].empty()) {
499 InstIt tail_thread = instList[tid].end();
502 squashIt[tid] = tail_thread;
508 template <class Impl>
509 const typename Impl::DynInstPtr&
510 ROB<Impl>::readHeadInst(ThreadID tid)
512 if (threadEntries[tid] != 0) {
513 InstIt head_thread = instList[tid].begin();
515 assert((*head_thread)->isInROB());
523 template <class Impl>
524 typename Impl::DynInstPtr
525 ROB<Impl>::readTailInst(ThreadID tid)
527 InstIt tail_thread = instList[tid].end();
533 template <class Impl>
535 ROB<Impl>::regStats()
537 using namespace Stats;
539 .name(name() + ".rob_reads")
540 .desc("The number of ROB reads");
543 .name(name() + ".rob_writes")
544 .desc("The number of ROB writes");
547 template <class Impl>
548 typename Impl::DynInstPtr
549 ROB<Impl>::findInst(ThreadID tid, InstSeqNum squash_inst)
551 for (InstIt it = instList[tid].begin(); it != instList[tid].end(); it++) {
552 if ((*it)->seqNum == squash_inst) {
559 #endif//__CPU_O3_ROB_IMPL_HH__