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