lsq_impl.hh revision 8948
1/*
2 * Copyright (c) 2011-2012 ARM Limited
3 * All rights reserved
4 *
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.
13 *
14 * Copyright (c) 2005-2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
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.
27 *
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.
39 *
40 * Authors: Korey Sewell
41 */
42
43#include <algorithm>
44#include <list>
45#include <string>
46
47#include "cpu/o3/lsq.hh"
48#include "debug/Fetch.hh"
49#include "debug/LSQ.hh"
50#include "debug/Writeback.hh"
51#include "params/DerivO3CPU.hh"
52
53using namespace std;
54
55template <class Impl>
56LSQ<Impl>::LSQ(O3CPU *cpu_ptr, IEW *iew_ptr, DerivO3CPUParams *params)
57    : cpu(cpu_ptr), iewStage(iew_ptr),
58      LQEntries(params->LQEntries),
59      SQEntries(params->SQEntries),
60      numThreads(params->numThreads),
61      retryTid(-1)
62{
63    //**********************************************/
64    //************ Handle SMT Parameters ***********/
65    //**********************************************/
66    std::string policy = params->smtLSQPolicy;
67
68    //Convert string to lowercase
69    std::transform(policy.begin(), policy.end(), policy.begin(),
70                   (int(*)(int)) tolower);
71
72    //Figure out fetch policy
73    if (policy == "dynamic") {
74        lsqPolicy = Dynamic;
75
76        maxLQEntries = LQEntries;
77        maxSQEntries = SQEntries;
78
79        DPRINTF(LSQ, "LSQ sharing policy set to Dynamic\n");
80    } else if (policy == "partitioned") {
81        lsqPolicy = Partitioned;
82
83        //@todo:make work if part_amt doesnt divide evenly.
84        maxLQEntries = LQEntries / numThreads;
85        maxSQEntries = SQEntries / numThreads;
86
87        DPRINTF(Fetch, "LSQ sharing policy set to Partitioned: "
88                "%i entries per LQ | %i entries per SQ\n",
89                maxLQEntries,maxSQEntries);
90    } else if (policy == "threshold") {
91        lsqPolicy = Threshold;
92
93        assert(params->smtLSQThreshold > LQEntries);
94        assert(params->smtLSQThreshold > SQEntries);
95
96        //Divide up by threshold amount
97        //@todo: Should threads check the max and the total
98        //amount of the LSQ
99        maxLQEntries  = params->smtLSQThreshold;
100        maxSQEntries  = params->smtLSQThreshold;
101
102        DPRINTF(LSQ, "LSQ sharing policy set to Threshold: "
103                "%i entries per LQ | %i entries per SQ\n",
104                maxLQEntries,maxSQEntries);
105    } else {
106        assert(0 && "Invalid LSQ Sharing Policy.Options Are:{Dynamic,"
107                    "Partitioned, Threshold}");
108    }
109
110    //Initialize LSQs
111    for (ThreadID tid = 0; tid < numThreads; tid++) {
112        thread[tid].init(cpu, iew_ptr, params, this,
113                         maxLQEntries, maxSQEntries, tid);
114        thread[tid].setDcachePort(&cpu_ptr->getDataPort());
115    }
116}
117
118
119template<class Impl>
120std::string
121LSQ<Impl>::name() const
122{
123    return iewStage->name() + ".lsq";
124}
125
126template<class Impl>
127void
128LSQ<Impl>::regStats()
129{
130    //Initialize LSQs
131    for (ThreadID tid = 0; tid < numThreads; tid++) {
132        thread[tid].regStats();
133    }
134}
135
136template<class Impl>
137void
138LSQ<Impl>::setActiveThreads(list<ThreadID> *at_ptr)
139{
140    activeThreads = at_ptr;
141    assert(activeThreads != 0);
142}
143
144template <class Impl>
145void
146LSQ<Impl>::switchOut()
147{
148    for (ThreadID tid = 0; tid < numThreads; tid++) {
149        thread[tid].switchOut();
150    }
151}
152
153template <class Impl>
154void
155LSQ<Impl>::takeOverFrom()
156{
157    for (ThreadID tid = 0; tid < numThreads; tid++) {
158        thread[tid].takeOverFrom();
159    }
160}
161
162template <class Impl>
163int
164LSQ<Impl>::entryAmount(ThreadID num_threads)
165{
166    if (lsqPolicy == Partitioned) {
167        return LQEntries / num_threads;
168    } else {
169        return 0;
170    }
171}
172
173template <class Impl>
174void
175LSQ<Impl>::resetEntries()
176{
177    if (lsqPolicy != Dynamic || numThreads > 1) {
178        int active_threads = activeThreads->size();
179
180        int maxEntries;
181
182        if (lsqPolicy == Partitioned) {
183            maxEntries = LQEntries / active_threads;
184        } else if (lsqPolicy == Threshold && active_threads == 1) {
185            maxEntries = LQEntries;
186        } else {
187            maxEntries = LQEntries;
188        }
189
190        list<ThreadID>::iterator threads  = activeThreads->begin();
191        list<ThreadID>::iterator end = activeThreads->end();
192
193        while (threads != end) {
194            ThreadID tid = *threads++;
195
196            resizeEntries(maxEntries, tid);
197        }
198    }
199}
200
201template<class Impl>
202void
203LSQ<Impl>::removeEntries(ThreadID tid)
204{
205    thread[tid].clearLQ();
206    thread[tid].clearSQ();
207}
208
209template<class Impl>
210void
211LSQ<Impl>::resizeEntries(unsigned size, ThreadID tid)
212{
213    thread[tid].resizeLQ(size);
214    thread[tid].resizeSQ(size);
215}
216
217template<class Impl>
218void
219LSQ<Impl>::tick()
220{
221    list<ThreadID>::iterator threads = activeThreads->begin();
222    list<ThreadID>::iterator end = activeThreads->end();
223
224    while (threads != end) {
225        ThreadID tid = *threads++;
226
227        thread[tid].tick();
228    }
229}
230
231template<class Impl>
232void
233LSQ<Impl>::insertLoad(DynInstPtr &load_inst)
234{
235    ThreadID tid = load_inst->threadNumber;
236
237    thread[tid].insertLoad(load_inst);
238}
239
240template<class Impl>
241void
242LSQ<Impl>::insertStore(DynInstPtr &store_inst)
243{
244    ThreadID tid = store_inst->threadNumber;
245
246    thread[tid].insertStore(store_inst);
247}
248
249template<class Impl>
250Fault
251LSQ<Impl>::executeLoad(DynInstPtr &inst)
252{
253    ThreadID tid = inst->threadNumber;
254
255    return thread[tid].executeLoad(inst);
256}
257
258template<class Impl>
259Fault
260LSQ<Impl>::executeStore(DynInstPtr &inst)
261{
262    ThreadID tid = inst->threadNumber;
263
264    return thread[tid].executeStore(inst);
265}
266
267template<class Impl>
268void
269LSQ<Impl>::writebackStores()
270{
271    list<ThreadID>::iterator threads = activeThreads->begin();
272    list<ThreadID>::iterator end = activeThreads->end();
273
274    while (threads != end) {
275        ThreadID tid = *threads++;
276
277        if (numStoresToWB(tid) > 0) {
278            DPRINTF(Writeback,"[tid:%i] Writing back stores. %i stores "
279                "available for Writeback.\n", tid, numStoresToWB(tid));
280        }
281
282        thread[tid].writebackStores();
283    }
284}
285
286template<class Impl>
287bool
288LSQ<Impl>::violation()
289{
290    /* Answers: Does Anybody Have a Violation?*/
291    list<ThreadID>::iterator threads = activeThreads->begin();
292    list<ThreadID>::iterator end = activeThreads->end();
293
294    while (threads != end) {
295        ThreadID tid = *threads++;
296
297        if (thread[tid].violation())
298            return true;
299    }
300
301    return false;
302}
303
304template <class Impl>
305void
306LSQ<Impl>::recvRetry()
307{
308    if (retryTid == InvalidThreadID)
309    {
310        //Squashed, so drop it
311        return;
312    }
313    int curr_retry_tid = retryTid;
314    // Speculatively clear the retry Tid.  This will get set again if
315    // the LSQUnit was unable to complete its access.
316    retryTid = -1;
317    thread[curr_retry_tid].recvRetry();
318}
319
320template <class Impl>
321bool
322LSQ<Impl>::recvTiming(PacketPtr pkt)
323{
324    assert(pkt->isResponse());
325    if (pkt->isError())
326        DPRINTF(LSQ, "Got error packet back for address: %#X\n",
327                pkt->getAddr());
328    thread[pkt->req->threadId()].completeDataAccess(pkt);
329    return true;
330}
331
332template <class Impl>
333bool
334LSQ<Impl>::recvTimingSnoop(PacketPtr pkt)
335{
336    assert(pkt->isRequest());
337    DPRINTF(LSQ, "received pkt for addr:%#x %s\n", pkt->getAddr(),
338            pkt->cmdString());
339
340    // must be a snoop
341    if (pkt->isInvalidate()) {
342        DPRINTF(LSQ, "received invalidation for addr:%#x\n",
343                pkt->getAddr());
344        for (ThreadID tid = 0; tid < numThreads; tid++) {
345            thread[tid].checkSnoop(pkt);
346        }
347    }
348
349    // to provide stronger consistency model
350    return true;
351}
352
353template<class Impl>
354int
355LSQ<Impl>::getCount()
356{
357    unsigned total = 0;
358
359    list<ThreadID>::iterator threads = activeThreads->begin();
360    list<ThreadID>::iterator end = activeThreads->end();
361
362    while (threads != end) {
363        ThreadID tid = *threads++;
364
365        total += getCount(tid);
366    }
367
368    return total;
369}
370
371template<class Impl>
372int
373LSQ<Impl>::numLoads()
374{
375    unsigned total = 0;
376
377    list<ThreadID>::iterator threads = activeThreads->begin();
378    list<ThreadID>::iterator end = activeThreads->end();
379
380    while (threads != end) {
381        ThreadID tid = *threads++;
382
383        total += numLoads(tid);
384    }
385
386    return total;
387}
388
389template<class Impl>
390int
391LSQ<Impl>::numStores()
392{
393    unsigned total = 0;
394
395    list<ThreadID>::iterator threads = activeThreads->begin();
396    list<ThreadID>::iterator end = activeThreads->end();
397
398    while (threads != end) {
399        ThreadID tid = *threads++;
400
401        total += thread[tid].numStores();
402    }
403
404    return total;
405}
406
407template<class Impl>
408int
409LSQ<Impl>::numLoadsReady()
410{
411    unsigned total = 0;
412
413    list<ThreadID>::iterator threads = activeThreads->begin();
414    list<ThreadID>::iterator end = activeThreads->end();
415
416    while (threads != end) {
417        ThreadID tid = *threads++;
418
419        total += thread[tid].numLoadsReady();
420    }
421
422    return total;
423}
424
425template<class Impl>
426unsigned
427LSQ<Impl>::numFreeEntries()
428{
429    unsigned total = 0;
430
431    list<ThreadID>::iterator threads = activeThreads->begin();
432    list<ThreadID>::iterator end = activeThreads->end();
433
434    while (threads != end) {
435        ThreadID tid = *threads++;
436
437        total += thread[tid].numFreeEntries();
438    }
439
440    return total;
441}
442
443template<class Impl>
444unsigned
445LSQ<Impl>::numFreeEntries(ThreadID tid)
446{
447    //if (lsqPolicy == Dynamic)
448    //return numFreeEntries();
449    //else
450        return thread[tid].numFreeEntries();
451}
452
453template<class Impl>
454bool
455LSQ<Impl>::isFull()
456{
457    list<ThreadID>::iterator threads = activeThreads->begin();
458    list<ThreadID>::iterator end = activeThreads->end();
459
460    while (threads != end) {
461        ThreadID tid = *threads++;
462
463        if (!(thread[tid].lqFull() || thread[tid].sqFull()))
464            return false;
465    }
466
467    return true;
468}
469
470template<class Impl>
471bool
472LSQ<Impl>::isFull(ThreadID tid)
473{
474    //@todo: Change to Calculate All Entries for
475    //Dynamic Policy
476    if (lsqPolicy == Dynamic)
477        return isFull();
478    else
479        return thread[tid].lqFull() || thread[tid].sqFull();
480}
481
482template<class Impl>
483bool
484LSQ<Impl>::lqFull()
485{
486    list<ThreadID>::iterator threads = activeThreads->begin();
487    list<ThreadID>::iterator end = activeThreads->end();
488
489    while (threads != end) {
490        ThreadID tid = *threads++;
491
492        if (!thread[tid].lqFull())
493            return false;
494    }
495
496    return true;
497}
498
499template<class Impl>
500bool
501LSQ<Impl>::lqFull(ThreadID tid)
502{
503    //@todo: Change to Calculate All Entries for
504    //Dynamic Policy
505    if (lsqPolicy == Dynamic)
506        return lqFull();
507    else
508        return thread[tid].lqFull();
509}
510
511template<class Impl>
512bool
513LSQ<Impl>::sqFull()
514{
515    list<ThreadID>::iterator threads = activeThreads->begin();
516    list<ThreadID>::iterator end = activeThreads->end();
517
518    while (threads != end) {
519        ThreadID tid = *threads++;
520
521        if (!sqFull(tid))
522            return false;
523    }
524
525    return true;
526}
527
528template<class Impl>
529bool
530LSQ<Impl>::sqFull(ThreadID tid)
531{
532     //@todo: Change to Calculate All Entries for
533    //Dynamic Policy
534    if (lsqPolicy == Dynamic)
535        return sqFull();
536    else
537        return thread[tid].sqFull();
538}
539
540template<class Impl>
541bool
542LSQ<Impl>::isStalled()
543{
544    list<ThreadID>::iterator threads = activeThreads->begin();
545    list<ThreadID>::iterator end = activeThreads->end();
546
547    while (threads != end) {
548        ThreadID tid = *threads++;
549
550        if (!thread[tid].isStalled())
551            return false;
552    }
553
554    return true;
555}
556
557template<class Impl>
558bool
559LSQ<Impl>::isStalled(ThreadID tid)
560{
561    if (lsqPolicy == Dynamic)
562        return isStalled();
563    else
564        return thread[tid].isStalled();
565}
566
567template<class Impl>
568bool
569LSQ<Impl>::hasStoresToWB()
570{
571    list<ThreadID>::iterator threads = activeThreads->begin();
572    list<ThreadID>::iterator end = activeThreads->end();
573
574    while (threads != end) {
575        ThreadID tid = *threads++;
576
577        if (hasStoresToWB(tid))
578            return true;
579    }
580
581    return false;
582}
583
584template<class Impl>
585bool
586LSQ<Impl>::willWB()
587{
588    list<ThreadID>::iterator threads = activeThreads->begin();
589    list<ThreadID>::iterator end = activeThreads->end();
590
591    while (threads != end) {
592        ThreadID tid = *threads++;
593
594        if (willWB(tid))
595            return true;
596    }
597
598    return false;
599}
600
601template<class Impl>
602void
603LSQ<Impl>::dumpInsts()
604{
605    list<ThreadID>::iterator threads = activeThreads->begin();
606    list<ThreadID>::iterator end = activeThreads->end();
607
608    while (threads != end) {
609        ThreadID tid = *threads++;
610
611        thread[tid].dumpInsts();
612    }
613}
614