2 * Copyright (c) 2011-2012, 2014 ARM Limited
3 * Copyright (c) 2013 Advanced Micro Devices, Inc.
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
15 * Copyright (c) 2005-2006 The Regents of The University of Michigan
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * Authors: Korey Sewell
44 #ifndef __CPU_O3_LSQ_IMPL_HH__
45 #define __CPU_O3_LSQ_IMPL_HH__
51 #include "cpu/o3/lsq.hh"
52 #include "debug/Drain.hh"
53 #include "debug/Fetch.hh"
54 #include "debug/LSQ.hh"
55 #include "debug/Writeback.hh"
56 #include "params/DerivO3CPU.hh"
61 LSQ<Impl>::LSQ(O3CPU *cpu_ptr, IEW *iew_ptr, DerivO3CPUParams *params)
62 : cpu(cpu_ptr), iewStage(iew_ptr),
63 LQEntries(params->LQEntries),
64 SQEntries(params->SQEntries),
65 numThreads(params->numThreads)
67 assert(numThreads > 0 && numThreads <= Impl::MaxThreads);
69 //**********************************************/
70 //************ Handle SMT Parameters ***********/
71 //**********************************************/
72 std::string policy = params->smtLSQPolicy;
74 //Convert string to lowercase
75 std::transform(policy.begin(), policy.end(), policy.begin(),
76 (int(*)(int)) tolower);
78 //Figure out fetch policy
79 if (policy == "dynamic") {
82 maxLQEntries = LQEntries;
83 maxSQEntries = SQEntries;
85 DPRINTF(LSQ, "LSQ sharing policy set to Dynamic\n");
86 } else if (policy == "partitioned") {
87 lsqPolicy = Partitioned;
89 //@todo:make work if part_amt doesnt divide evenly.
90 maxLQEntries = LQEntries / numThreads;
91 maxSQEntries = SQEntries / numThreads;
93 DPRINTF(Fetch, "LSQ sharing policy set to Partitioned: "
94 "%i entries per LQ | %i entries per SQ\n",
95 maxLQEntries,maxSQEntries);
96 } else if (policy == "threshold") {
97 lsqPolicy = Threshold;
99 assert(params->smtLSQThreshold > LQEntries);
100 assert(params->smtLSQThreshold > SQEntries);
102 //Divide up by threshold amount
103 //@todo: Should threads check the max and the total
105 maxLQEntries = params->smtLSQThreshold;
106 maxSQEntries = params->smtLSQThreshold;
108 DPRINTF(LSQ, "LSQ sharing policy set to Threshold: "
109 "%i entries per LQ | %i entries per SQ\n",
110 maxLQEntries,maxSQEntries);
112 assert(0 && "Invalid LSQ Sharing Policy.Options Are:{Dynamic,"
113 "Partitioned, Threshold}");
117 thread = new LSQUnit[numThreads];
118 for (ThreadID tid = 0; tid < numThreads; tid++) {
119 thread[tid].init(cpu, iew_ptr, params, this,
120 maxLQEntries, maxSQEntries, tid);
121 thread[tid].setDcachePort(&cpu_ptr->getDataPort());
128 LSQ<Impl>::name() const
130 return iewStage->name() + ".lsq";
135 LSQ<Impl>::regStats()
138 for (ThreadID tid = 0; tid < numThreads; tid++) {
139 thread[tid].regStats();
145 LSQ<Impl>::setActiveThreads(list<ThreadID> *at_ptr)
147 activeThreads = at_ptr;
148 assert(activeThreads != 0);
151 template <class Impl>
153 LSQ<Impl>::drainSanityCheck() const
157 for (ThreadID tid = 0; tid < numThreads; tid++)
158 thread[tid].drainSanityCheck();
161 template <class Impl>
163 LSQ<Impl>::isDrained() const
168 DPRINTF(Drain, "Not drained, LQ not empty.\n");
173 DPRINTF(Drain, "Not drained, SQ not empty.\n");
180 template <class Impl>
182 LSQ<Impl>::takeOverFrom()
184 for (ThreadID tid = 0; tid < numThreads; tid++) {
185 thread[tid].takeOverFrom();
189 template <class Impl>
191 LSQ<Impl>::entryAmount(ThreadID num_threads)
193 if (lsqPolicy == Partitioned) {
194 return LQEntries / num_threads;
200 template <class Impl>
202 LSQ<Impl>::resetEntries()
204 if (lsqPolicy != Dynamic || numThreads > 1) {
205 int active_threads = activeThreads->size();
209 if (lsqPolicy == Partitioned) {
210 maxEntries = LQEntries / active_threads;
211 } else if (lsqPolicy == Threshold && active_threads == 1) {
212 maxEntries = LQEntries;
214 maxEntries = LQEntries;
217 list<ThreadID>::iterator threads = activeThreads->begin();
218 list<ThreadID>::iterator end = activeThreads->end();
220 while (threads != end) {
221 ThreadID tid = *threads++;
223 resizeEntries(maxEntries, tid);
230 LSQ<Impl>::removeEntries(ThreadID tid)
232 thread[tid].clearLQ();
233 thread[tid].clearSQ();
238 LSQ<Impl>::resizeEntries(unsigned size, ThreadID tid)
240 thread[tid].resizeLQ(size);
241 thread[tid].resizeSQ(size);
248 list<ThreadID>::iterator threads = activeThreads->begin();
249 list<ThreadID>::iterator end = activeThreads->end();
251 while (threads != end) {
252 ThreadID tid = *threads++;
260 LSQ<Impl>::insertLoad(const DynInstPtr &load_inst)
262 ThreadID tid = load_inst->threadNumber;
264 thread[tid].insertLoad(load_inst);
269 LSQ<Impl>::insertStore(const DynInstPtr &store_inst)
271 ThreadID tid = store_inst->threadNumber;
273 thread[tid].insertStore(store_inst);
278 LSQ<Impl>::executeLoad(const DynInstPtr &inst)
280 ThreadID tid = inst->threadNumber;
282 return thread[tid].executeLoad(inst);
287 LSQ<Impl>::executeStore(const DynInstPtr &inst)
289 ThreadID tid = inst->threadNumber;
291 return thread[tid].executeStore(inst);
296 LSQ<Impl>::writebackStores()
298 list<ThreadID>::iterator threads = activeThreads->begin();
299 list<ThreadID>::iterator end = activeThreads->end();
301 while (threads != end) {
302 ThreadID tid = *threads++;
304 if (numStoresToWB(tid) > 0) {
305 DPRINTF(Writeback,"[tid:%i] Writing back stores. %i stores "
306 "available for Writeback.\n", tid, numStoresToWB(tid));
309 thread[tid].writebackStores();
315 LSQ<Impl>::violation()
317 /* Answers: Does Anybody Have a Violation?*/
318 list<ThreadID>::iterator threads = activeThreads->begin();
319 list<ThreadID>::iterator end = activeThreads->end();
321 while (threads != end) {
322 ThreadID tid = *threads++;
324 if (thread[tid].violation())
331 template <class Impl>
333 LSQ<Impl>::recvReqRetry()
335 iewStage->cacheUnblocked();
337 for (ThreadID tid : *activeThreads) {
338 thread[tid].recvRetry();
342 template <class Impl>
344 LSQ<Impl>::recvTimingResp(PacketPtr pkt)
347 DPRINTF(LSQ, "Got error packet back for address: %#X\n",
350 thread[cpu->contextToThread(pkt->req->contextId())]
351 .completeDataAccess(pkt);
353 if (pkt->isInvalidate()) {
354 // This response also contains an invalidate; e.g. this can be the case
355 // if cmd is ReadRespWithInvalidate.
357 // The calling order between completeDataAccess and checkSnoop matters.
358 // By calling checkSnoop after completeDataAccess, we ensure that the
359 // fault set by checkSnoop is not lost. Calling writeback (more
360 // specifically inst->completeAcc) in completeDataAccess overwrites
361 // fault, and in case this instruction requires squashing (as
362 // determined by checkSnoop), the ReExec fault set by checkSnoop would
363 // be lost otherwise.
365 DPRINTF(LSQ, "received invalidation with response for addr:%#x\n",
368 for (ThreadID tid = 0; tid < numThreads; tid++) {
369 thread[tid].checkSnoop(pkt);
377 template <class Impl>
379 LSQ<Impl>::recvTimingSnoopReq(PacketPtr pkt)
381 DPRINTF(LSQ, "received pkt for addr:%#x %s\n", pkt->getAddr(),
385 if (pkt->isInvalidate()) {
386 DPRINTF(LSQ, "received invalidation for addr:%#x\n",
388 for (ThreadID tid = 0; tid < numThreads; tid++) {
389 thread[tid].checkSnoop(pkt);
396 LSQ<Impl>::getCount()
400 list<ThreadID>::iterator threads = activeThreads->begin();
401 list<ThreadID>::iterator end = activeThreads->end();
403 while (threads != end) {
404 ThreadID tid = *threads++;
406 total += getCount(tid);
414 LSQ<Impl>::numLoads()
418 list<ThreadID>::iterator threads = activeThreads->begin();
419 list<ThreadID>::iterator end = activeThreads->end();
421 while (threads != end) {
422 ThreadID tid = *threads++;
424 total += numLoads(tid);
432 LSQ<Impl>::numStores()
436 list<ThreadID>::iterator threads = activeThreads->begin();
437 list<ThreadID>::iterator end = activeThreads->end();
439 while (threads != end) {
440 ThreadID tid = *threads++;
442 total += thread[tid].numStores();
450 LSQ<Impl>::numFreeLoadEntries()
454 list<ThreadID>::iterator threads = activeThreads->begin();
455 list<ThreadID>::iterator end = activeThreads->end();
457 while (threads != end) {
458 ThreadID tid = *threads++;
460 total += thread[tid].numFreeLoadEntries();
468 LSQ<Impl>::numFreeStoreEntries()
472 list<ThreadID>::iterator threads = activeThreads->begin();
473 list<ThreadID>::iterator end = activeThreads->end();
475 while (threads != end) {
476 ThreadID tid = *threads++;
478 total += thread[tid].numFreeStoreEntries();
486 LSQ<Impl>::numFreeLoadEntries(ThreadID tid)
488 return thread[tid].numFreeLoadEntries();
493 LSQ<Impl>::numFreeStoreEntries(ThreadID tid)
495 return thread[tid].numFreeStoreEntries();
502 list<ThreadID>::iterator threads = activeThreads->begin();
503 list<ThreadID>::iterator end = activeThreads->end();
505 while (threads != end) {
506 ThreadID tid = *threads++;
508 if (!(thread[tid].lqFull() || thread[tid].sqFull()))
517 LSQ<Impl>::isFull(ThreadID tid)
519 //@todo: Change to Calculate All Entries for
521 if (lsqPolicy == Dynamic)
524 return thread[tid].lqFull() || thread[tid].sqFull();
529 LSQ<Impl>::isEmpty() const
531 return lqEmpty() && sqEmpty();
536 LSQ<Impl>::lqEmpty() const
538 list<ThreadID>::const_iterator threads = activeThreads->begin();
539 list<ThreadID>::const_iterator end = activeThreads->end();
541 while (threads != end) {
542 ThreadID tid = *threads++;
544 if (!thread[tid].lqEmpty())
553 LSQ<Impl>::sqEmpty() const
555 list<ThreadID>::const_iterator threads = activeThreads->begin();
556 list<ThreadID>::const_iterator end = activeThreads->end();
558 while (threads != end) {
559 ThreadID tid = *threads++;
561 if (!thread[tid].sqEmpty())
572 list<ThreadID>::iterator threads = activeThreads->begin();
573 list<ThreadID>::iterator end = activeThreads->end();
575 while (threads != end) {
576 ThreadID tid = *threads++;
578 if (!thread[tid].lqFull())
587 LSQ<Impl>::lqFull(ThreadID tid)
589 //@todo: Change to Calculate All Entries for
591 if (lsqPolicy == Dynamic)
594 return thread[tid].lqFull();
601 list<ThreadID>::iterator threads = activeThreads->begin();
602 list<ThreadID>::iterator end = activeThreads->end();
604 while (threads != end) {
605 ThreadID tid = *threads++;
616 LSQ<Impl>::sqFull(ThreadID tid)
618 //@todo: Change to Calculate All Entries for
620 if (lsqPolicy == Dynamic)
623 return thread[tid].sqFull();
628 LSQ<Impl>::isStalled()
630 list<ThreadID>::iterator threads = activeThreads->begin();
631 list<ThreadID>::iterator end = activeThreads->end();
633 while (threads != end) {
634 ThreadID tid = *threads++;
636 if (!thread[tid].isStalled())
645 LSQ<Impl>::isStalled(ThreadID tid)
647 if (lsqPolicy == Dynamic)
650 return thread[tid].isStalled();
655 LSQ<Impl>::hasStoresToWB()
657 list<ThreadID>::iterator threads = activeThreads->begin();
658 list<ThreadID>::iterator end = activeThreads->end();
660 while (threads != end) {
661 ThreadID tid = *threads++;
663 if (hasStoresToWB(tid))
674 list<ThreadID>::iterator threads = activeThreads->begin();
675 list<ThreadID>::iterator end = activeThreads->end();
677 while (threads != end) {
678 ThreadID tid = *threads++;
689 LSQ<Impl>::dumpInsts() const
691 list<ThreadID>::const_iterator threads = activeThreads->begin();
692 list<ThreadID>::const_iterator end = activeThreads->end();
694 while (threads != end) {
695 ThreadID tid = *threads++;
697 thread[tid].dumpInsts();
701 #endif//__CPU_O3_LSQ_IMPL_HH__