summaryrefslogtreecommitdiff
path: root/src/mem/cache/miss/miss_queue.cc
blob: 3c45862725024bd236012f891008922c8fee649b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/*
 * Copyright (c) 2003-2005 The Regents of The University of Michigan
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Erik Hallnor
 *          Ron Dreslinski
 */

/**
 * @file
 * Miss and writeback queue definitions.
 */

#include "cpu/smt.hh" //for maxThreadsPerCPU
#include "mem/cache/base_cache.hh"
#include "mem/cache/miss/miss_queue.hh"
#include "mem/cache/prefetch/base_prefetcher.hh"

using namespace std;

// simple constructor
/**
 * @todo Remove the +16 from the write buffer constructor once we handle
 * stalling on writebacks do to compression writes.
 */
MissQueue::MissQueue(int numMSHRs, int numTargets, int write_buffers,
                     bool write_allocate, bool prefetch_miss)
    : mq(numMSHRs, 4), wb(write_buffers,numMSHRs+1000), numMSHR(numMSHRs),
      numTarget(numTargets), writeBuffers(write_buffers),
      writeAllocate(write_allocate), order(0), prefetchMiss(prefetch_miss)
{
    noTargetMSHR = NULL;
}

void
MissQueue::regStats(const string &name)
{
    Request temp_req((Addr) NULL, 4, 0);
    Packet::Command temp_cmd = Packet::ReadReq;
    Packet temp_pkt(&temp_req, temp_cmd, 0);  //@todo FIx command strings so this isn't neccessary
    temp_pkt.allocate();

    using namespace Stats;

    writebacks
        .init(maxThreadsPerCPU)
        .name(name + ".writebacks")
        .desc("number of writebacks")
        .flags(total)
        ;

    // MSHR hit statistics
    for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
        Packet::Command cmd = (Packet::Command)access_idx;
        const string &cstr = temp_pkt.cmdIdxToString(cmd);

        mshr_hits[access_idx]
            .init(maxThreadsPerCPU)
            .name(name + "." + cstr + "_mshr_hits")
            .desc("number of " + cstr + " MSHR hits")
            .flags(total | nozero | nonan)
            ;
    }

    demandMshrHits
        .name(name + ".demand_mshr_hits")
        .desc("number of demand (read+write) MSHR hits")
        .flags(total)
        ;
    demandMshrHits = mshr_hits[Packet::ReadReq] + mshr_hits[Packet::WriteReq];

    overallMshrHits
        .name(name + ".overall_mshr_hits")
        .desc("number of overall MSHR hits")
        .flags(total)
        ;
    overallMshrHits = demandMshrHits + mshr_hits[Packet::SoftPFReq] +
        mshr_hits[Packet::HardPFReq];

    // MSHR miss statistics
    for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
        Packet::Command cmd = (Packet::Command)access_idx;
        const string &cstr = temp_pkt.cmdIdxToString(cmd);

        mshr_misses[access_idx]
            .init(maxThreadsPerCPU)
            .name(name + "." + cstr + "_mshr_misses")
            .desc("number of " + cstr + " MSHR misses")
            .flags(total | nozero | nonan)
            ;
    }

    demandMshrMisses
        .name(name + ".demand_mshr_misses")
        .desc("number of demand (read+write) MSHR misses")
        .flags(total)
        ;
    demandMshrMisses = mshr_misses[Packet::ReadReq] + mshr_misses[Packet::WriteReq];

    overallMshrMisses
        .name(name + ".overall_mshr_misses")
        .desc("number of overall MSHR misses")
        .flags(total)
        ;
    overallMshrMisses = demandMshrMisses + mshr_misses[Packet::SoftPFReq] +
        mshr_misses[Packet::HardPFReq];

    // MSHR miss latency statistics
    for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
        Packet::Command cmd = (Packet::Command)access_idx;
        const string &cstr = temp_pkt.cmdIdxToString(cmd);

        mshr_miss_latency[access_idx]
            .init(maxThreadsPerCPU)
            .name(name + "." + cstr + "_mshr_miss_latency")
            .desc("number of " + cstr + " MSHR miss cycles")
            .flags(total | nozero | nonan)
            ;
    }

    demandMshrMissLatency
        .name(name + ".demand_mshr_miss_latency")
        .desc("number of demand (read+write) MSHR miss cycles")
        .flags(total)
        ;
    demandMshrMissLatency = mshr_miss_latency[Packet::ReadReq]
        + mshr_miss_latency[Packet::WriteReq];

    overallMshrMissLatency
        .name(name + ".overall_mshr_miss_latency")
        .desc("number of overall MSHR miss cycles")
        .flags(total)
        ;
    overallMshrMissLatency = demandMshrMissLatency +
        mshr_miss_latency[Packet::SoftPFReq] + mshr_miss_latency[Packet::HardPFReq];

    // MSHR uncacheable statistics
    for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
        Packet::Command cmd = (Packet::Command)access_idx;
        const string &cstr = temp_pkt.cmdIdxToString(cmd);

        mshr_uncacheable[access_idx]
            .init(maxThreadsPerCPU)
            .name(name + "." + cstr + "_mshr_uncacheable")
            .desc("number of " + cstr + " MSHR uncacheable")
            .flags(total | nozero | nonan)
            ;
    }

    overallMshrUncacheable
        .name(name + ".overall_mshr_uncacheable_misses")
        .desc("number of overall MSHR uncacheable misses")
        .flags(total)
        ;
    overallMshrUncacheable = mshr_uncacheable[Packet::ReadReq]
        + mshr_uncacheable[Packet::WriteReq] + mshr_uncacheable[Packet::SoftPFReq]
        + mshr_uncacheable[Packet::HardPFReq];

    // MSHR miss latency statistics
    for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
        Packet::Command cmd = (Packet::Command)access_idx;
        const string &cstr = temp_pkt.cmdIdxToString(cmd);

        mshr_uncacheable_lat[access_idx]
            .init(maxThreadsPerCPU)
            .name(name + "." + cstr + "_mshr_uncacheable_latency")
            .desc("number of " + cstr + " MSHR uncacheable cycles")
            .flags(total | nozero | nonan)
            ;
    }

    overallMshrUncacheableLatency
        .name(name + ".overall_mshr_uncacheable_latency")
        .desc("number of overall MSHR uncacheable cycles")
        .flags(total)
        ;
    overallMshrUncacheableLatency = mshr_uncacheable_lat[Packet::ReadReq]
        + mshr_uncacheable_lat[Packet::WriteReq]
        + mshr_uncacheable_lat[Packet::SoftPFReq]
        + mshr_uncacheable_lat[Packet::HardPFReq];

#if 0
    // MSHR access formulas
    for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
        Packet::Command cmd = (Packet::Command)access_idx;
        const string &cstr = temp_pkt.cmdIdxToString(cmd);

        mshrAccesses[access_idx]
            .name(name + "." + cstr + "_mshr_accesses")
            .desc("number of " + cstr + " mshr accesses(hits+misses)")
            .flags(total | nozero | nonan)
            ;
        mshrAccesses[access_idx] =
            mshr_hits[access_idx] + mshr_misses[access_idx]
            + mshr_uncacheable[access_idx];
    }

    demandMshrAccesses
        .name(name + ".demand_mshr_accesses")
        .desc("number of demand (read+write) mshr accesses")
        .flags(total | nozero | nonan)
        ;
    demandMshrAccesses = demandMshrHits + demandMshrMisses;

    overallMshrAccesses
        .name(name + ".overall_mshr_accesses")
        .desc("number of overall (read+write) mshr accesses")
        .flags(total | nozero | nonan)
        ;
    overallMshrAccesses = overallMshrHits + overallMshrMisses
        + overallMshrUncacheable;
#endif

    // MSHR miss rate formulas
    for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
        Packet::Command cmd = (Packet::Command)access_idx;
        const string &cstr = temp_pkt.cmdIdxToString(cmd);

        mshrMissRate[access_idx]
            .name(name + "." + cstr + "_mshr_miss_rate")
            .desc("mshr miss rate for " + cstr + " accesses")
            .flags(total | nozero | nonan)
            ;

        mshrMissRate[access_idx] =
            mshr_misses[access_idx] / cache->accesses[access_idx];
    }

    demandMshrMissRate
        .name(name + ".demand_mshr_miss_rate")
        .desc("mshr miss rate for demand accesses")
        .flags(total)
        ;
    demandMshrMissRate = demandMshrMisses / cache->demandAccesses;

    overallMshrMissRate
        .name(name + ".overall_mshr_miss_rate")
        .desc("mshr miss rate for overall accesses")
        .flags(total)
        ;
    overallMshrMissRate = overallMshrMisses / cache->overallAccesses;

    // mshrMiss latency formulas
    for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
        Packet::Command cmd = (Packet::Command)access_idx;
        const string &cstr = temp_pkt.cmdIdxToString(cmd);

        avgMshrMissLatency[access_idx]
            .name(name + "." + cstr + "_avg_mshr_miss_latency")
            .desc("average " + cstr + " mshr miss latency")
            .flags(total | nozero | nonan)
            ;

        avgMshrMissLatency[access_idx] =
            mshr_miss_latency[access_idx] / mshr_misses[access_idx];
    }

    demandAvgMshrMissLatency
        .name(name + ".demand_avg_mshr_miss_latency")
        .desc("average overall mshr miss latency")
        .flags(total)
        ;
    demandAvgMshrMissLatency = demandMshrMissLatency / demandMshrMisses;

    overallAvgMshrMissLatency
        .name(name + ".overall_avg_mshr_miss_latency")
        .desc("average overall mshr miss latency")
        .flags(total)
        ;
    overallAvgMshrMissLatency = overallMshrMissLatency / overallMshrMisses;

    // mshrUncacheable latency formulas
    for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
        Packet::Command cmd = (Packet::Command)access_idx;
        const string &cstr = temp_pkt.cmdIdxToString(cmd);

        avgMshrUncacheableLatency[access_idx]
            .name(name + "." + cstr + "_avg_mshr_uncacheable_latency")
            .desc("average " + cstr + " mshr uncacheable latency")
            .flags(total | nozero | nonan)
            ;

        avgMshrUncacheableLatency[access_idx] =
            mshr_uncacheable_lat[access_idx] / mshr_uncacheable[access_idx];
    }

    overallAvgMshrUncacheableLatency
        .name(name + ".overall_avg_mshr_uncacheable_latency")
        .desc("average overall mshr uncacheable latency")
        .flags(total)
        ;
    overallAvgMshrUncacheableLatency = overallMshrUncacheableLatency / overallMshrUncacheable;

    mshr_cap_events
        .init(maxThreadsPerCPU)
        .name(name + ".mshr_cap_events")
        .desc("number of times MSHR cap was activated")
        .flags(total)
        ;

    //software prefetching stats
    soft_prefetch_mshr_full
        .init(maxThreadsPerCPU)
        .name(name + ".soft_prefetch_mshr_full")
        .desc("number of mshr full events for SW prefetching instrutions")
        .flags(total)
        ;

    mshr_no_allocate_misses
        .name(name +".no_allocate_misses")
        .desc("Number of misses that were no-allocate")
        ;

}

void
MissQueue::setCache(BaseCache *_cache)
{
    cache = _cache;
    blkSize = cache->getBlockSize();
}

void
MissQueue::setPrefetcher(BasePrefetcher *_prefetcher)
{
    prefetcher = _prefetcher;
}

MSHR*
MissQueue::allocateMiss(PacketPtr &pkt, int size, Tick time)
{
    MSHR* mshr = mq.allocate(pkt, size);
    mshr->order = order++;
    if (!pkt->req->isUncacheable() ){//&& !pkt->isNoAllocate()) {
        // Mark this as a cache line fill
        mshr->pkt->flags |= CACHE_LINE_FILL;
    }
    if (mq.isFull()) {
        cache->setBlocked(Blocked_NoMSHRs);
    }
    if (pkt->cmd != Packet::HardPFReq) {
        //If we need to request the bus (not on HW prefetch), do so
        cache->setMasterRequest(Request_MSHR, time);
    }
    return mshr;
}


MSHR*
MissQueue::allocateWrite(PacketPtr &pkt, int size, Tick time)
{
    MSHR* mshr = wb.allocate(pkt,size);
    mshr->order = order++;

//REMOVING COMPRESSION FOR NOW
#if 0
    if (pkt->isCompressed()) {
        mshr->pkt->deleteData();
        mshr->pkt->actualSize = pkt->actualSize;
        mshr->pkt->data = new uint8_t[pkt->actualSize];
        memcpy(mshr->pkt->data, pkt->data, pkt->actualSize);
    } else {
#endif
        memcpy(mshr->pkt->getPtr<uint8_t>(), pkt->getPtr<uint8_t>(), pkt->getSize());
  //{

    if (wb.isFull()) {
        cache->setBlocked(Blocked_NoWBBuffers);
    }

    cache->setMasterRequest(Request_WB, time);

    return mshr;
}


/**
 * @todo Remove SW prefetches on mshr hits.
 */
void
MissQueue::handleMiss(PacketPtr &pkt, int blkSize, Tick time)
{
//    if (!cache->isTopLevel())
    if (prefetchMiss) prefetcher->handleMiss(pkt, time);

    int size = blkSize;
    Addr blkAddr = pkt->getAddr() & ~(Addr)(blkSize-1);
    MSHR* mshr = NULL;
    if (!pkt->req->isUncacheable()) {
        mshr = mq.findMatch(blkAddr);
        if (mshr) {
            //@todo remove hw_pf here
            mshr_hits[pkt->cmdToIndex()][0/*pkt->req->getThreadNum()*/]++;
            if (mshr->threadNum != 0/*pkt->req->getThreadNum()*/) {
                mshr->threadNum = -1;
            }
            mq.allocateTarget(mshr, pkt);
            if (mshr->pkt->isNoAllocate() && !pkt->isNoAllocate()) {
                //We are adding an allocate after a no-allocate
                mshr->pkt->flags &= ~NO_ALLOCATE;
            }
            if (mshr->getNumTargets() == numTarget) {
                noTargetMSHR = mshr;
                cache->setBlocked(Blocked_NoTargets);
                mq.moveToFront(mshr);
            }
            return;
        }
        if (pkt->isNoAllocate()) {
            //Count no-allocate requests differently
            mshr_no_allocate_misses++;
        }
        else {
            mshr_misses[pkt->cmdToIndex()][0/*pkt->req->getThreadNum()*/]++;
        }
    } else {
        //Count uncacheable accesses
        mshr_uncacheable[pkt->cmdToIndex()][0/*pkt->req->getThreadNum()*/]++;
        size = pkt->getSize();
    }
    if (pkt->isWrite() && (pkt->req->isUncacheable() || !writeAllocate ||
                               !pkt->needsResponse())) {
        /**
         * @todo Add write merging here.
         */
        mshr = allocateWrite(pkt, pkt->getSize(), time);
        return;
    }

    mshr = allocateMiss(pkt, blkSize, time);
}

MSHR*
MissQueue::fetchBlock(Addr addr, int blk_size, Tick time,
                      PacketPtr &target)
{
    Addr blkAddr = addr & ~(Addr)(blk_size - 1);
    assert(mq.findMatch(addr) == NULL);
    MSHR *mshr = mq.allocateFetch(blkAddr, blk_size, target);
    mshr->order = order++;
    mshr->pkt->flags |= CACHE_LINE_FILL;
    if (mq.isFull()) {
        cache->setBlocked(Blocked_NoMSHRs);
    }
    cache->setMasterRequest(Request_MSHR, time);
    return mshr;
}

PacketPtr
MissQueue::getPacket()
{
    PacketPtr pkt = mq.getReq();
    if (((wb.isFull() && wb.inServiceMSHRs == 0) || !pkt ||
         pkt->time > curTick) && wb.havePending()) {
        pkt = wb.getReq();
        // Need to search for earlier miss.
        MSHR *mshr = mq.findPending(pkt);
        if (mshr && mshr->order < ((MSHR*)(pkt->senderState))->order) {
            // Service misses in order until conflict is cleared.
            return mq.getReq();
        }
    }
    if (pkt) {
        MSHR* mshr = wb.findPending(pkt);
        if (mshr /*&& mshr->order < pkt->senderState->order*/) {
            // The only way this happens is if we are
            // doing a write and we didn't have permissions
            // then subsequently saw a writeback(owned got evicted)
            // We need to make sure to perform the writeback first
            // To preserve the dirty data, then we can issue the write
            return wb.getReq();
        }
    }
    else if (!mq.isFull()){
        //If we have a miss queue slot, we can try a prefetch
        pkt = prefetcher->getPacket();
        if (pkt) {
            //Update statistic on number of prefetches issued (hwpf_mshr_misses)
            mshr_misses[pkt->cmdToIndex()][0/*pkt->req->getThreadNum()*/]++;
            //It will request the bus for the future, but should clear that immedieatley
            allocateMiss(pkt, pkt->getSize(), curTick);
            pkt = mq.getReq();
            assert(pkt); //We should get back a req b/c we just put one in
        }
    }
    return pkt;
}

void
MissQueue::setBusCmd(PacketPtr &pkt, Packet::Command cmd)
{
    assert(pkt->senderState != 0);
    MSHR * mshr = (MSHR*)pkt->senderState;
    mshr->originalCmd = pkt->cmd;
    if (cmd == Packet::UpgradeReq || cmd == Packet::InvalidateReq) {
        pkt->flags |= NO_ALLOCATE;
        pkt->flags &= ~CACHE_LINE_FILL;
    }
    else if (!pkt->req->isUncacheable() && !pkt->isNoAllocate() &&
             (cmd & (1 << 6)/*NeedsResponse*/)) {
        pkt->flags |= CACHE_LINE_FILL;
    }
    if (pkt->isCacheFill() || pkt->isNoAllocate())
        pkt->cmd = cmd;
}

void
MissQueue::restoreOrigCmd(PacketPtr &pkt)
{
    pkt->cmd = ((MSHR*)(pkt->senderState))->originalCmd;
}

void
MissQueue::markInService(PacketPtr &pkt, MSHR* mshr)
{
    bool unblock = false;
    BlockedCause cause = NUM_BLOCKED_CAUSES;

    /**
     * @todo Should include MSHRQueue pointer in MSHR to select the correct
     * one.
     */
    if ((!pkt->isCacheFill() && pkt->isWrite())) {
        // Forwarding a write/ writeback, don't need to change
        // the command
        unblock = wb.isFull();
        wb.markInService(mshr);
        if (!wb.havePending()){
            cache->clearMasterRequest(Request_WB);
        }
        if (unblock) {
            // Do we really unblock?
            unblock = !wb.isFull();
            cause = Blocked_NoWBBuffers;
        }
    } else {
        unblock = mq.isFull();
        mq.markInService(mshr);
        if (!mq.havePending()){
            cache->clearMasterRequest(Request_MSHR);
        }
        if (mshr->originalCmd == Packet::HardPFReq) {
            DPRINTF(HWPrefetch, "%s:Marking a HW_PF in service\n",
                    cache->name());
            //Also clear pending if need be
            if (!prefetcher->havePending())
            {
                cache->clearMasterRequest(Request_PF);
            }
        }
        if (unblock) {
            unblock = !mq.isFull();
            cause = Blocked_NoMSHRs;
        }
    }
    if (unblock) {
        cache->clearBlocked(cause);
    }
}


void
MissQueue::handleResponse(PacketPtr &pkt, Tick time)
{
    MSHR* mshr = (MSHR*)pkt->senderState;
    if (((MSHR*)(pkt->senderState))->originalCmd == Packet::HardPFReq) {
        DPRINTF(HWPrefetch, "%s:Handling the response to a HW_PF\n",
                cache->name());
    }
#ifndef NDEBUG
    int num_targets = mshr->getNumTargets();
#endif

    bool unblock = false;
    bool unblock_target = false;
    BlockedCause cause = NUM_BLOCKED_CAUSES;

    if (pkt->isCacheFill() && !pkt->isNoAllocate()) {
        mshr_miss_latency[mshr->originalCmd][0/*pkt->req->getThreadNum()*/] +=
            curTick - pkt->time;
        // targets were handled in the cache tags
        if (mshr == noTargetMSHR) {
            // we always clear at least one target
            unblock_target = true;
            cause = Blocked_NoTargets;
            noTargetMSHR = NULL;
        }

        if (mshr->hasTargets()) {
            // Didn't satisfy all the targets, need to resend
            Packet::Command cmd = mshr->getTarget()->cmd;
            mshr->pkt->setDest(Packet::Broadcast);
            mshr->pkt->result = Packet::Unknown;
            mq.markPending(mshr, cmd);
            mshr->order = order++;
            cache->setMasterRequest(Request_MSHR, time);
        }
        else {
            unblock = mq.isFull();
            mq.deallocate(mshr);
            if (unblock) {
                unblock = !mq.isFull();
                cause = Blocked_NoMSHRs;
            }
        }
    } else {
        if (pkt->req->isUncacheable()) {
            mshr_uncacheable_lat[pkt->cmd][0/*pkt->req->getThreadNum()*/] +=
                curTick - pkt->time;
        }
        if (mshr->hasTargets() && pkt->req->isUncacheable()) {
            // Should only have 1 target if we had any
            assert(num_targets == 1);
            PacketPtr target = mshr->getTarget();
            mshr->popTarget();
            if (pkt->isRead()) {
                memcpy(target->getPtr<uint8_t>(), pkt->getPtr<uint8_t>(),
                       target->getSize());
            }
            cache->respond(target, time);
            assert(!mshr->hasTargets());
        }
        else if (mshr->hasTargets()) {
            //Must be a no_allocate with possibly more than one target
            assert(mshr->pkt->isNoAllocate());
            while (mshr->hasTargets()) {
                PacketPtr target = mshr->getTarget();
                mshr->popTarget();
                if (pkt->isRead()) {
                    memcpy(target->getPtr<uint8_t>(), pkt->getPtr<uint8_t>(),
                           target->getSize());
                }
                cache->respond(target, time);
            }
        }

        if (pkt->isWrite()) {
            // If the wrtie buffer is full, we might unblock now
            unblock = wb.isFull();
            wb.deallocate(mshr);
            if (unblock) {
                // Did we really unblock?
                unblock = !wb.isFull();
                cause = Blocked_NoWBBuffers;
            }
        } else {
            unblock = mq.isFull();
            mq.deallocate(mshr);
            if (unblock) {
                unblock = !mq.isFull();
                cause = Blocked_NoMSHRs;
            }
        }
    }
    if (unblock || unblock_target) {
        cache->clearBlocked(cause);
    }
}

void
MissQueue::squash(int threadNum)
{
    bool unblock = false;
    BlockedCause cause = NUM_BLOCKED_CAUSES;

    if (noTargetMSHR && noTargetMSHR->threadNum == threadNum) {
        noTargetMSHR = NULL;
        unblock = true;
        cause = Blocked_NoTargets;
    }
    if (mq.isFull()) {
        unblock = true;
        cause = Blocked_NoMSHRs;
    }
    mq.squash(threadNum);
    if (!mq.havePending()) {
        cache->clearMasterRequest(Request_MSHR);
    }
    if (unblock && !mq.isFull()) {
        cache->clearBlocked(cause);
    }

}

MSHR*
MissQueue::findMSHR(Addr addr) const
{
    return mq.findMatch(addr);
}

bool
MissQueue::findWrites(Addr addr, vector<MSHR*> &writes) const
{
    return wb.findMatches(addr,writes);
}

void
MissQueue::doWriteback(Addr addr,
                       int size, uint8_t *data, bool compressed)
{
    // Generate request
    Request * req = new Request(addr, size, 0);
    PacketPtr pkt = new Packet(req, Packet::Writeback, -1);
    pkt->allocate();
    if (data) {
        memcpy(pkt->getPtr<uint8_t>(), data, size);
    }

    if (compressed) {
        pkt->flags |= COMPRESSED;
    }

    ///All writebacks charged to same thread @todo figure this out
    writebacks[0/*pkt->req->getThreadNum()*/]++;

    allocateWrite(pkt, 0, curTick);
}


void
MissQueue::doWriteback(PacketPtr &pkt)
{
    writebacks[0/*pkt->req->getThreadNum()*/]++;
    allocateWrite(pkt, 0, curTick);
}


MSHR*
MissQueue::allocateTargetList(Addr addr)
{
   MSHR* mshr = mq.allocateTargetList(addr, blkSize);
   mshr->pkt->flags |= CACHE_LINE_FILL;
   if (mq.isFull()) {
       cache->setBlocked(Blocked_NoMSHRs);
   }
   return mshr;
}

bool
MissQueue::havePending()
{
    return mq.havePending() || wb.havePending() || prefetcher->havePending();
}