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)
60 numEntries(params->numROBEntries),
61 squashWidth(params->squashWidth),
63 numThreads(params->numThreads)
65 std::string policy = params->smtROBPolicy;
67 //Convert string to lowercase
68 std::transform(policy.begin(), policy.end(), policy.begin(),
69 (int(*)(int)) tolower);
71 //Figure out rob policy
72 if (policy == "dynamic") {
75 //Set Max Entries to Total ROB Capacity
76 for (ThreadID tid = 0; tid < numThreads; tid++) {
77 maxEntries[tid] = numEntries;
80 } else if (policy == "partitioned") {
81 robPolicy = Partitioned;
82 DPRINTF(Fetch, "ROB sharing policy set to Partitioned\n");
84 //@todo:make work if part_amt doesnt divide evenly.
85 int part_amt = numEntries / numThreads;
87 //Divide ROB up evenly
88 for (ThreadID tid = 0; tid < numThreads; tid++) {
89 maxEntries[tid] = part_amt;
92 } else if (policy == "threshold") {
93 robPolicy = Threshold;
94 DPRINTF(Fetch, "ROB sharing policy set to Threshold\n");
96 int threshold = params->smtROBThreshold;;
98 //Divide up by threshold amount
99 for (ThreadID tid = 0; tid < numThreads; tid++) {
100 maxEntries[tid] = threshold;
103 panic("Invalid ROB sharing policy. Options are: Dynamic, "
104 "Partitioned, Threshold");
106 for (ThreadID tid = numThreads; tid < Impl::MaxThreads; tid++) {
113 template <class Impl>
115 ROB<Impl>::resetState()
117 for (ThreadID tid = 0; tid < Impl::MaxThreads; tid++) {
118 threadEntries[tid] = 0;
119 squashIt[tid] = instList[tid].end();
120 squashedSeqNum[tid] = 0;
121 doneSquashing[tid] = true;
125 // Initialize the "universal" ROB head & tail point to invalid
127 head = instList[0].end();
128 tail = instList[0].end();
131 template <class Impl>
133 ROB<Impl>::name() const
135 return cpu->name() + ".rob";
138 template <class Impl>
140 ROB<Impl>::setActiveThreads(list<ThreadID> *at_ptr)
142 DPRINTF(ROB, "Setting active threads list pointer.\n");
143 activeThreads = at_ptr;
146 template <class Impl>
148 ROB<Impl>::drainSanityCheck() const
150 for (ThreadID tid = 0; tid < numThreads; tid++)
151 assert(instList[tid].empty());
155 template <class Impl>
157 ROB<Impl>::takeOverFrom()
162 template <class Impl>
164 ROB<Impl>::resetEntries()
166 if (robPolicy != Dynamic || numThreads > 1) {
167 int active_threads = activeThreads->size();
169 list<ThreadID>::iterator threads = activeThreads->begin();
170 list<ThreadID>::iterator end = activeThreads->end();
172 while (threads != end) {
173 ThreadID tid = *threads++;
175 if (robPolicy == Partitioned) {
176 maxEntries[tid] = numEntries / active_threads;
177 } else if (robPolicy == Threshold && active_threads == 1) {
178 maxEntries[tid] = numEntries;
184 template <class Impl>
186 ROB<Impl>::entryAmount(ThreadID num_threads)
188 if (robPolicy == Partitioned) {
189 return numEntries / num_threads;
195 template <class Impl>
197 ROB<Impl>::countInsts()
201 for (ThreadID tid = 0; tid < numThreads; tid++)
202 total += countInsts(tid);
207 template <class Impl>
209 ROB<Impl>::countInsts(ThreadID tid)
211 return instList[tid].size();
214 template <class Impl>
216 ROB<Impl>::insertInst(const DynInstPtr &inst)
222 DPRINTF(ROB, "Adding inst PC %s to the ROB.\n", inst->pcState());
224 assert(numInstsInROB != numEntries);
226 ThreadID tid = inst->threadNumber;
228 instList[tid].push_back(inst);
230 //Set Up head iterator if this is the 1st instruction in the ROB
231 if (numInstsInROB == 0) {
232 head = instList[tid].begin();
233 assert((*head) == inst);
236 //Must Decrement for iterator to actually be valid since __.end()
237 //actually points to 1 after the last inst
238 tail = instList[tid].end();
244 ++threadEntries[tid];
246 assert((*tail) == inst);
248 DPRINTF(ROB, "[tid:%i] Now has %d instructions.\n", tid, threadEntries[tid]);
251 template <class Impl>
253 ROB<Impl>::retireHead(ThreadID tid)
257 assert(numInstsInROB > 0);
259 // Get the head ROB instruction by copying it and remove it from the list
260 InstIt head_it = instList[tid].begin();
262 DynInstPtr head_inst = std::move(*head_it);
263 instList[tid].erase(head_it);
265 assert(head_inst->readyToCommit());
267 DPRINTF(ROB, "[tid:%u]: Retiring head instruction, "
268 "instruction PC %s, [sn:%lli]\n", tid, head_inst->pcState(),
272 --threadEntries[tid];
274 head_inst->clearInROB();
275 head_inst->setCommitted();
277 //Update "Global" Head of ROB
280 // @todo: A special case is needed if the instruction being
281 // retired is the only instruction in the ROB; otherwise the tail
282 // iterator will become invalidated.
283 cpu->removeFrontInst(head_inst);
286 template <class Impl>
288 ROB<Impl>::isHeadReady(ThreadID tid)
291 if (threadEntries[tid] != 0) {
292 return instList[tid].front()->readyToCommit();
298 template <class Impl>
300 ROB<Impl>::canCommit()
302 //@todo: set ActiveThreads through ROB or CPU
303 list<ThreadID>::iterator threads = activeThreads->begin();
304 list<ThreadID>::iterator end = activeThreads->end();
306 while (threads != end) {
307 ThreadID tid = *threads++;
309 if (isHeadReady(tid)) {
317 template <class Impl>
319 ROB<Impl>::numFreeEntries()
321 return numEntries - numInstsInROB;
324 template <class Impl>
326 ROB<Impl>::numFreeEntries(ThreadID tid)
328 return maxEntries[tid] - threadEntries[tid];
331 template <class Impl>
333 ROB<Impl>::doSquash(ThreadID tid)
336 DPRINTF(ROB, "[tid:%u]: Squashing instructions until [sn:%i].\n",
337 tid, squashedSeqNum[tid]);
339 assert(squashIt[tid] != instList[tid].end());
341 if ((*squashIt[tid])->seqNum < squashedSeqNum[tid]) {
342 DPRINTF(ROB, "[tid:%u]: Done squashing instructions.\n",
345 squashIt[tid] = instList[tid].end();
347 doneSquashing[tid] = true;
351 bool robTailUpdate = false;
353 for (int numSquashed = 0;
354 numSquashed < squashWidth &&
355 squashIt[tid] != instList[tid].end() &&
356 (*squashIt[tid])->seqNum > squashedSeqNum[tid];
359 DPRINTF(ROB, "[tid:%u]: Squashing instruction PC %s, seq num %i.\n",
360 (*squashIt[tid])->threadNumber,
361 (*squashIt[tid])->pcState(),
362 (*squashIt[tid])->seqNum);
364 // Mark the instruction as squashed, and ready to commit so that
365 // it can drain out of the pipeline.
366 (*squashIt[tid])->setSquashed();
368 (*squashIt[tid])->setCanCommit();
371 if (squashIt[tid] == instList[tid].begin()) {
372 DPRINTF(ROB, "Reached head of instruction list while "
375 squashIt[tid] = instList[tid].end();
377 doneSquashing[tid] = true;
382 InstIt tail_thread = instList[tid].end();
385 if ((*squashIt[tid]) == (*tail_thread))
386 robTailUpdate = true;
392 // Check if ROB is done squashing.
393 if ((*squashIt[tid])->seqNum <= squashedSeqNum[tid]) {
394 DPRINTF(ROB, "[tid:%u]: Done squashing instructions.\n",
397 squashIt[tid] = instList[tid].end();
399 doneSquashing[tid] = true;
408 template <class Impl>
410 ROB<Impl>::updateHead()
412 InstSeqNum lowest_num = 0;
413 bool first_valid = true;
415 // @todo: set ActiveThreads through ROB or CPU
416 list<ThreadID>::iterator threads = activeThreads->begin();
417 list<ThreadID>::iterator end = activeThreads->end();
419 while (threads != end) {
420 ThreadID tid = *threads++;
422 if (instList[tid].empty())
426 head = instList[tid].begin();
427 lowest_num = (*head)->seqNum;
432 InstIt head_thread = instList[tid].begin();
434 DynInstPtr head_inst = (*head_thread);
436 assert(head_inst != 0);
438 if (head_inst->seqNum < lowest_num) {
440 lowest_num = head_inst->seqNum;
445 head = instList[0].end();
450 template <class Impl>
452 ROB<Impl>::updateTail()
454 tail = instList[0].end();
455 bool first_valid = true;
457 list<ThreadID>::iterator threads = activeThreads->begin();
458 list<ThreadID>::iterator end = activeThreads->end();
460 while (threads != end) {
461 ThreadID tid = *threads++;
463 if (instList[tid].empty()) {
467 // If this is the first valid then assign w/out
470 tail = instList[tid].end();
476 // Assign new tail if this thread's tail is younger
477 // than our current "tail high"
478 InstIt tail_thread = instList[tid].end();
481 if ((*tail_thread)->seqNum > (*tail)->seqNum) {
488 template <class Impl>
490 ROB<Impl>::squash(InstSeqNum squash_num, ThreadID tid)
493 DPRINTF(ROB, "Does not need to squash due to being empty "
500 DPRINTF(ROB, "Starting to squash within the ROB.\n");
502 robStatus[tid] = ROBSquashing;
504 doneSquashing[tid] = false;
506 squashedSeqNum[tid] = squash_num;
508 if (!instList[tid].empty()) {
509 InstIt tail_thread = instList[tid].end();
512 squashIt[tid] = tail_thread;
518 template <class Impl>
519 const typename Impl::DynInstPtr&
520 ROB<Impl>::readHeadInst(ThreadID tid)
522 if (threadEntries[tid] != 0) {
523 InstIt head_thread = instList[tid].begin();
525 assert((*head_thread)->isInROB());
533 template <class Impl>
534 typename Impl::DynInstPtr
535 ROB<Impl>::readTailInst(ThreadID tid)
537 InstIt tail_thread = instList[tid].end();
543 template <class Impl>
545 ROB<Impl>::regStats()
547 using namespace Stats;
549 .name(name() + ".rob_reads")
550 .desc("The number of ROB reads");
553 .name(name() + ".rob_writes")
554 .desc("The number of ROB writes");
557 template <class Impl>
558 typename Impl::DynInstPtr
559 ROB<Impl>::findInst(ThreadID tid, InstSeqNum squash_inst)
561 for (InstIt it = instList[tid].begin(); it != instList[tid].end(); it++) {
562 if ((*it)->seqNum == squash_inst) {
569 #endif//__CPU_O3_ROB_IMPL_HH__