Merge zizzer.eecs.umich.edu:/bk/newmem
[gem5.git] / src / cpu / o3 / lsq_impl.hh
1 /*
2 * Copyright (c) 2005-2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Korey Sewell
29 */
30
31 #include <algorithm>
32 #include <list>
33 #include <string>
34
35 #include "cpu/o3/lsq.hh"
36
37 template<class Impl>
38 void
39 LSQ<Impl>::DcachePort::setPeer(Port *port)
40 {
41 Port::setPeer(port);
42
43 #if FULL_SYSTEM
44 // Update the ThreadContext's memory ports (Functional/Virtual
45 // Ports)
46 lsq->updateMemPorts();
47 #endif
48 }
49
50 template <class Impl>
51 Tick
52 LSQ<Impl>::DcachePort::recvAtomic(PacketPtr pkt)
53 {
54 panic("O3CPU model does not work with atomic mode!");
55 return curTick;
56 }
57
58 template <class Impl>
59 void
60 LSQ<Impl>::DcachePort::recvFunctional(PacketPtr pkt)
61 {
62 DPRINTF(LSQ, "LSQ doesn't update things on a recvFunctional.");
63 }
64
65 template <class Impl>
66 void
67 LSQ<Impl>::DcachePort::recvStatusChange(Status status)
68 {
69 if (status == RangeChange) {
70 if (!snoopRangeSent) {
71 snoopRangeSent = true;
72 sendStatusChange(Port::RangeChange);
73 }
74 return;
75 }
76 panic("O3CPU doesn't expect recvStatusChange callback!");
77 }
78
79 template <class Impl>
80 bool
81 LSQ<Impl>::DcachePort::recvTiming(PacketPtr pkt)
82 {
83 if (pkt->isResponse()) {
84 lsq->thread[pkt->req->getThreadNum()].completeDataAccess(pkt);
85 }
86 else {
87 //else it is a coherence request, maybe you need to do something
88 warn("Recieved a coherence request (Invalidate?), 03CPU doesn't"
89 "update LSQ for these\n");
90 }
91 return true;
92 }
93
94 template <class Impl>
95 void
96 LSQ<Impl>::DcachePort::recvRetry()
97 {
98 if (lsq->retryTid == -1)
99 {
100 //Squashed, so drop it
101 return;
102 }
103 lsq->thread[lsq->retryTid].recvRetry();
104 // Speculatively clear the retry Tid. This will get set again if
105 // the LSQUnit was unable to complete its access.
106 lsq->retryTid = -1;
107 }
108
109 template <class Impl>
110 LSQ<Impl>::LSQ(O3CPU *cpu_ptr, IEW *iew_ptr, Params *params)
111 : cpu(cpu_ptr), iewStage(iew_ptr), dcachePort(this),
112 LQEntries(params->LQEntries),
113 SQEntries(params->SQEntries),
114 numThreads(params->numberOfThreads),
115 retryTid(-1)
116 {
117 dcachePort.snoopRangeSent = false;
118
119 //**********************************************/
120 //************ Handle SMT Parameters ***********/
121 //**********************************************/
122 std::string policy = params->smtLSQPolicy;
123
124 //Convert string to lowercase
125 std::transform(policy.begin(), policy.end(), policy.begin(),
126 (int(*)(int)) tolower);
127
128 //Figure out fetch policy
129 if (policy == "dynamic") {
130 lsqPolicy = Dynamic;
131
132 maxLQEntries = LQEntries;
133 maxSQEntries = SQEntries;
134
135 DPRINTF(LSQ, "LSQ sharing policy set to Dynamic\n");
136 } else if (policy == "partitioned") {
137 lsqPolicy = Partitioned;
138
139 //@todo:make work if part_amt doesnt divide evenly.
140 maxLQEntries = LQEntries / numThreads;
141 maxSQEntries = SQEntries / numThreads;
142
143 DPRINTF(Fetch, "LSQ sharing policy set to Partitioned: "
144 "%i entries per LQ | %i entries per SQ",
145 maxLQEntries,maxSQEntries);
146 } else if (policy == "threshold") {
147 lsqPolicy = Threshold;
148
149 assert(params->smtLSQThreshold > LQEntries);
150 assert(params->smtLSQThreshold > SQEntries);
151
152 //Divide up by threshold amount
153 //@todo: Should threads check the max and the total
154 //amount of the LSQ
155 maxLQEntries = params->smtLSQThreshold;
156 maxSQEntries = params->smtLSQThreshold;
157
158 DPRINTF(LSQ, "LSQ sharing policy set to Threshold: "
159 "%i entries per LQ | %i entries per SQ",
160 maxLQEntries,maxSQEntries);
161 } else {
162 assert(0 && "Invalid LSQ Sharing Policy.Options Are:{Dynamic,"
163 "Partitioned, Threshold}");
164 }
165
166 //Initialize LSQs
167 for (int tid=0; tid < numThreads; tid++) {
168 thread[tid].init(cpu, iew_ptr, params, this,
169 maxLQEntries, maxSQEntries, tid);
170 thread[tid].setDcachePort(&dcachePort);
171 }
172 }
173
174
175 template<class Impl>
176 std::string
177 LSQ<Impl>::name() const
178 {
179 return iewStage->name() + ".lsq";
180 }
181
182 template<class Impl>
183 void
184 LSQ<Impl>::regStats()
185 {
186 //Initialize LSQs
187 for (int tid=0; tid < numThreads; tid++) {
188 thread[tid].regStats();
189 }
190 }
191
192 template<class Impl>
193 void
194 LSQ<Impl>::setActiveThreads(std::list<unsigned> *at_ptr)
195 {
196 activeThreads = at_ptr;
197 assert(activeThreads != 0);
198 }
199
200 template <class Impl>
201 void
202 LSQ<Impl>::switchOut()
203 {
204 for (int tid = 0; tid < numThreads; tid++) {
205 thread[tid].switchOut();
206 }
207 }
208
209 template <class Impl>
210 void
211 LSQ<Impl>::takeOverFrom()
212 {
213 for (int tid = 0; tid < numThreads; tid++) {
214 thread[tid].takeOverFrom();
215 }
216 }
217
218 template <class Impl>
219 int
220 LSQ<Impl>::entryAmount(int num_threads)
221 {
222 if (lsqPolicy == Partitioned) {
223 return LQEntries / num_threads;
224 } else {
225 return 0;
226 }
227 }
228
229 template <class Impl>
230 void
231 LSQ<Impl>::resetEntries()
232 {
233 if (lsqPolicy != Dynamic || numThreads > 1) {
234 int active_threads = activeThreads->size();
235
236 int maxEntries;
237
238 if (lsqPolicy == Partitioned) {
239 maxEntries = LQEntries / active_threads;
240 } else if (lsqPolicy == Threshold && active_threads == 1) {
241 maxEntries = LQEntries;
242 } else {
243 maxEntries = LQEntries;
244 }
245
246 std::list<unsigned>::iterator threads = activeThreads->begin();
247 std::list<unsigned>::iterator end = activeThreads->end();
248
249 while (threads != end) {
250 unsigned tid = *threads++;
251
252 resizeEntries(maxEntries, tid);
253 }
254 }
255 }
256
257 template<class Impl>
258 void
259 LSQ<Impl>::removeEntries(unsigned tid)
260 {
261 thread[tid].clearLQ();
262 thread[tid].clearSQ();
263 }
264
265 template<class Impl>
266 void
267 LSQ<Impl>::resizeEntries(unsigned size,unsigned tid)
268 {
269 thread[tid].resizeLQ(size);
270 thread[tid].resizeSQ(size);
271 }
272
273 template<class Impl>
274 void
275 LSQ<Impl>::tick()
276 {
277 std::list<unsigned>::iterator threads = activeThreads->begin();
278 std::list<unsigned>::iterator end = activeThreads->end();
279
280 while (threads != end) {
281 unsigned tid = *threads++;
282
283 thread[tid].tick();
284 }
285 }
286
287 template<class Impl>
288 void
289 LSQ<Impl>::insertLoad(DynInstPtr &load_inst)
290 {
291 unsigned tid = load_inst->threadNumber;
292
293 thread[tid].insertLoad(load_inst);
294 }
295
296 template<class Impl>
297 void
298 LSQ<Impl>::insertStore(DynInstPtr &store_inst)
299 {
300 unsigned tid = store_inst->threadNumber;
301
302 thread[tid].insertStore(store_inst);
303 }
304
305 template<class Impl>
306 Fault
307 LSQ<Impl>::executeLoad(DynInstPtr &inst)
308 {
309 unsigned tid = inst->threadNumber;
310
311 return thread[tid].executeLoad(inst);
312 }
313
314 template<class Impl>
315 Fault
316 LSQ<Impl>::executeStore(DynInstPtr &inst)
317 {
318 unsigned tid = inst->threadNumber;
319
320 return thread[tid].executeStore(inst);
321 }
322
323 template<class Impl>
324 void
325 LSQ<Impl>::writebackStores()
326 {
327 std::list<unsigned>::iterator threads = activeThreads->begin();
328 std::list<unsigned>::iterator end = activeThreads->end();
329
330 while (threads != end) {
331 unsigned tid = *threads++;
332
333 if (numStoresToWB(tid) > 0) {
334 DPRINTF(Writeback,"[tid:%i] Writing back stores. %i stores "
335 "available for Writeback.\n", tid, numStoresToWB(tid));
336 }
337
338 thread[tid].writebackStores();
339 }
340 }
341
342 template<class Impl>
343 bool
344 LSQ<Impl>::violation()
345 {
346 /* Answers: Does Anybody Have a Violation?*/
347 std::list<unsigned>::iterator threads = activeThreads->begin();
348 std::list<unsigned>::iterator end = activeThreads->end();
349
350 while (threads != end) {
351 unsigned tid = *threads++;
352
353 if (thread[tid].violation())
354 return true;
355 }
356
357 return false;
358 }
359
360 template<class Impl>
361 int
362 LSQ<Impl>::getCount()
363 {
364 unsigned total = 0;
365
366 std::list<unsigned>::iterator threads = activeThreads->begin();
367 std::list<unsigned>::iterator end = activeThreads->end();
368
369 while (threads != end) {
370 unsigned tid = *threads++;
371
372 total += getCount(tid);
373 }
374
375 return total;
376 }
377
378 template<class Impl>
379 int
380 LSQ<Impl>::numLoads()
381 {
382 unsigned total = 0;
383
384 std::list<unsigned>::iterator threads = activeThreads->begin();
385 std::list<unsigned>::iterator end = activeThreads->end();
386
387 while (threads != end) {
388 unsigned tid = *threads++;
389
390 total += numLoads(tid);
391 }
392
393 return total;
394 }
395
396 template<class Impl>
397 int
398 LSQ<Impl>::numStores()
399 {
400 unsigned total = 0;
401
402 std::list<unsigned>::iterator threads = activeThreads->begin();
403 std::list<unsigned>::iterator end = activeThreads->end();
404
405 while (threads != end) {
406 unsigned tid = *threads++;
407
408 total += thread[tid].numStores();
409 }
410
411 return total;
412 }
413
414 template<class Impl>
415 int
416 LSQ<Impl>::numLoadsReady()
417 {
418 unsigned total = 0;
419
420 std::list<unsigned>::iterator threads = activeThreads->begin();
421 std::list<unsigned>::iterator end = activeThreads->end();
422
423 while (threads != end) {
424 unsigned tid = *threads++;
425
426 total += thread[tid].numLoadsReady();
427 }
428
429 return total;
430 }
431
432 template<class Impl>
433 unsigned
434 LSQ<Impl>::numFreeEntries()
435 {
436 unsigned total = 0;
437
438 std::list<unsigned>::iterator threads = activeThreads->begin();
439 std::list<unsigned>::iterator end = activeThreads->end();
440
441 while (threads != end) {
442 unsigned tid = *threads++;
443
444 total += thread[tid].numFreeEntries();
445 }
446
447 return total;
448 }
449
450 template<class Impl>
451 unsigned
452 LSQ<Impl>::numFreeEntries(unsigned tid)
453 {
454 //if (lsqPolicy == Dynamic)
455 //return numFreeEntries();
456 //else
457 return thread[tid].numFreeEntries();
458 }
459
460 template<class Impl>
461 bool
462 LSQ<Impl>::isFull()
463 {
464 std::list<unsigned>::iterator threads = activeThreads->begin();
465 std::list<unsigned>::iterator end = activeThreads->end();
466
467 while (threads != end) {
468 unsigned tid = *threads++;
469
470 if (!(thread[tid].lqFull() || thread[tid].sqFull()))
471 return false;
472 }
473
474 return true;
475 }
476
477 template<class Impl>
478 bool
479 LSQ<Impl>::isFull(unsigned tid)
480 {
481 //@todo: Change to Calculate All Entries for
482 //Dynamic Policy
483 if (lsqPolicy == Dynamic)
484 return isFull();
485 else
486 return thread[tid].lqFull() || thread[tid].sqFull();
487 }
488
489 template<class Impl>
490 bool
491 LSQ<Impl>::lqFull()
492 {
493 std::list<unsigned>::iterator threads = activeThreads->begin();
494 std::list<unsigned>::iterator end = activeThreads->end();
495
496 while (threads != end) {
497 unsigned tid = *threads++;
498
499 if (!thread[tid].lqFull())
500 return false;
501 }
502
503 return true;
504 }
505
506 template<class Impl>
507 bool
508 LSQ<Impl>::lqFull(unsigned tid)
509 {
510 //@todo: Change to Calculate All Entries for
511 //Dynamic Policy
512 if (lsqPolicy == Dynamic)
513 return lqFull();
514 else
515 return thread[tid].lqFull();
516 }
517
518 template<class Impl>
519 bool
520 LSQ<Impl>::sqFull()
521 {
522 std::list<unsigned>::iterator threads = activeThreads->begin();
523 std::list<unsigned>::iterator end = activeThreads->end();
524
525 while (threads != end) {
526 unsigned tid = *threads++;
527
528 if (!sqFull(tid))
529 return false;
530 }
531
532 return true;
533 }
534
535 template<class Impl>
536 bool
537 LSQ<Impl>::sqFull(unsigned tid)
538 {
539 //@todo: Change to Calculate All Entries for
540 //Dynamic Policy
541 if (lsqPolicy == Dynamic)
542 return sqFull();
543 else
544 return thread[tid].sqFull();
545 }
546
547 template<class Impl>
548 bool
549 LSQ<Impl>::isStalled()
550 {
551 std::list<unsigned>::iterator threads = activeThreads->begin();
552 std::list<unsigned>::iterator end = activeThreads->end();
553
554 while (threads != end) {
555 unsigned tid = *threads++;
556
557 if (!thread[tid].isStalled())
558 return false;
559 }
560
561 return true;
562 }
563
564 template<class Impl>
565 bool
566 LSQ<Impl>::isStalled(unsigned tid)
567 {
568 if (lsqPolicy == Dynamic)
569 return isStalled();
570 else
571 return thread[tid].isStalled();
572 }
573
574 template<class Impl>
575 bool
576 LSQ<Impl>::hasStoresToWB()
577 {
578 std::list<unsigned>::iterator threads = activeThreads->begin();
579 std::list<unsigned>::iterator end = activeThreads->end();
580
581 if (threads == end)
582 return false;
583
584 while (threads != end) {
585 unsigned tid = *threads++;
586
587 if (!hasStoresToWB(tid))
588 return false;
589 }
590
591 return true;
592 }
593
594 template<class Impl>
595 bool
596 LSQ<Impl>::willWB()
597 {
598 std::list<unsigned>::iterator threads = activeThreads->begin();
599 std::list<unsigned>::iterator end = activeThreads->end();
600
601 while (threads != end) {
602 unsigned tid = *threads++;
603
604 if (!willWB(tid))
605 return false;
606 }
607
608 return true;
609 }
610
611 template<class Impl>
612 void
613 LSQ<Impl>::dumpInsts()
614 {
615 std::list<unsigned>::iterator threads = activeThreads->begin();
616 std::list<unsigned>::iterator end = activeThreads->end();
617
618 while (threads != end) {
619 unsigned tid = *threads++;
620
621 thread[tid].dumpInsts();
622 }
623 }