cache.cc revision 11750:c15cc4d973ea
1/*
2 * Copyright (c) 2010-2016 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) 2002-2005 The Regents of The University of Michigan
15 * Copyright (c) 2010,2015 Advanced Micro Devices, Inc.
16 * All rights reserved.
17 *
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.
28 *
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.
40 *
41 * Authors: Erik Hallnor
42 *          Dave Greene
43 *          Nathan Binkert
44 *          Steve Reinhardt
45 *          Ron Dreslinski
46 *          Andreas Sandberg
47 */
48
49/**
50 * @file
51 * Cache definitions.
52 */
53
54#include "mem/cache/cache.hh"
55
56#include "base/misc.hh"
57#include "base/types.hh"
58#include "debug/Cache.hh"
59#include "debug/CachePort.hh"
60#include "debug/CacheTags.hh"
61#include "debug/CacheVerbose.hh"
62#include "mem/cache/blk.hh"
63#include "mem/cache/mshr.hh"
64#include "mem/cache/prefetch/base.hh"
65#include "sim/sim_exit.hh"
66
67Cache::Cache(const CacheParams *p)
68    : BaseCache(p, p->system->cacheLineSize()),
69      tags(p->tags),
70      prefetcher(p->prefetcher),
71      doFastWrites(true),
72      prefetchOnAccess(p->prefetch_on_access),
73      clusivity(p->clusivity),
74      writebackClean(p->writeback_clean),
75      tempBlockWriteback(nullptr),
76      writebackTempBlockAtomicEvent(this, false,
77                                    EventBase::Delayed_Writeback_Pri)
78{
79    tempBlock = new CacheBlk();
80    tempBlock->data = new uint8_t[blkSize];
81
82    cpuSidePort = new CpuSidePort(p->name + ".cpu_side", this,
83                                  "CpuSidePort");
84    memSidePort = new MemSidePort(p->name + ".mem_side", this,
85                                  "MemSidePort");
86
87    tags->setCache(this);
88    if (prefetcher)
89        prefetcher->setCache(this);
90}
91
92Cache::~Cache()
93{
94    delete [] tempBlock->data;
95    delete tempBlock;
96
97    delete cpuSidePort;
98    delete memSidePort;
99}
100
101void
102Cache::regStats()
103{
104    BaseCache::regStats();
105}
106
107void
108Cache::cmpAndSwap(CacheBlk *blk, PacketPtr pkt)
109{
110    assert(pkt->isRequest());
111
112    uint64_t overwrite_val;
113    bool overwrite_mem;
114    uint64_t condition_val64;
115    uint32_t condition_val32;
116
117    int offset = tags->extractBlkOffset(pkt->getAddr());
118    uint8_t *blk_data = blk->data + offset;
119
120    assert(sizeof(uint64_t) >= pkt->getSize());
121
122    overwrite_mem = true;
123    // keep a copy of our possible write value, and copy what is at the
124    // memory address into the packet
125    pkt->writeData((uint8_t *)&overwrite_val);
126    pkt->setData(blk_data);
127
128    if (pkt->req->isCondSwap()) {
129        if (pkt->getSize() == sizeof(uint64_t)) {
130            condition_val64 = pkt->req->getExtraData();
131            overwrite_mem = !std::memcmp(&condition_val64, blk_data,
132                                         sizeof(uint64_t));
133        } else if (pkt->getSize() == sizeof(uint32_t)) {
134            condition_val32 = (uint32_t)pkt->req->getExtraData();
135            overwrite_mem = !std::memcmp(&condition_val32, blk_data,
136                                         sizeof(uint32_t));
137        } else
138            panic("Invalid size for conditional read/write\n");
139    }
140
141    if (overwrite_mem) {
142        std::memcpy(blk_data, &overwrite_val, pkt->getSize());
143        blk->status |= BlkDirty;
144    }
145}
146
147
148void
149Cache::satisfyRequest(PacketPtr pkt, CacheBlk *blk,
150                      bool deferred_response, bool pending_downgrade)
151{
152    assert(pkt->isRequest());
153
154    assert(blk && blk->isValid());
155    // Occasionally this is not true... if we are a lower-level cache
156    // satisfying a string of Read and ReadEx requests from
157    // upper-level caches, a Read will mark the block as shared but we
158    // can satisfy a following ReadEx anyway since we can rely on the
159    // Read requester(s) to have buffered the ReadEx snoop and to
160    // invalidate their blocks after receiving them.
161    // assert(!pkt->needsWritable() || blk->isWritable());
162    assert(pkt->getOffset(blkSize) + pkt->getSize() <= blkSize);
163
164    // Check RMW operations first since both isRead() and
165    // isWrite() will be true for them
166    if (pkt->cmd == MemCmd::SwapReq) {
167        cmpAndSwap(blk, pkt);
168    } else if (pkt->isWrite()) {
169        // we have the block in a writable state and can go ahead,
170        // note that the line may be also be considered writable in
171        // downstream caches along the path to memory, but always
172        // Exclusive, and never Modified
173        assert(blk->isWritable());
174        // Write or WriteLine at the first cache with block in writable state
175        if (blk->checkWrite(pkt)) {
176            pkt->writeDataToBlock(blk->data, blkSize);
177        }
178        // Always mark the line as dirty (and thus transition to the
179        // Modified state) even if we are a failed StoreCond so we
180        // supply data to any snoops that have appended themselves to
181        // this cache before knowing the store will fail.
182        blk->status |= BlkDirty;
183        DPRINTF(CacheVerbose, "%s for %s (write)\n", __func__, pkt->print());
184    } else if (pkt->isRead()) {
185        if (pkt->isLLSC()) {
186            blk->trackLoadLocked(pkt);
187        }
188
189        // all read responses have a data payload
190        assert(pkt->hasRespData());
191        pkt->setDataFromBlock(blk->data, blkSize);
192
193        // determine if this read is from a (coherent) cache or not
194        if (pkt->fromCache()) {
195            assert(pkt->getSize() == blkSize);
196            // special handling for coherent block requests from
197            // upper-level caches
198            if (pkt->needsWritable()) {
199                // sanity check
200                assert(pkt->cmd == MemCmd::ReadExReq ||
201                       pkt->cmd == MemCmd::SCUpgradeFailReq);
202                assert(!pkt->hasSharers());
203
204                // if we have a dirty copy, make sure the recipient
205                // keeps it marked dirty (in the modified state)
206                if (blk->isDirty()) {
207                    pkt->setCacheResponding();
208                    blk->status &= ~BlkDirty;
209                }
210            } else if (blk->isWritable() && !pending_downgrade &&
211                       !pkt->hasSharers() &&
212                       pkt->cmd != MemCmd::ReadCleanReq) {
213                // we can give the requester a writable copy on a read
214                // request if:
215                // - we have a writable copy at this level (& below)
216                // - we don't have a pending snoop from below
217                //   signaling another read request
218                // - no other cache above has a copy (otherwise it
219                //   would have set hasSharers flag when
220                //   snooping the packet)
221                // - the read has explicitly asked for a clean
222                //   copy of the line
223                if (blk->isDirty()) {
224                    // special considerations if we're owner:
225                    if (!deferred_response) {
226                        // respond with the line in Modified state
227                        // (cacheResponding set, hasSharers not set)
228                        pkt->setCacheResponding();
229
230                        // if this cache is mostly inclusive, we
231                        // keep the block in the Exclusive state,
232                        // and pass it upwards as Modified
233                        // (writable and dirty), hence we have
234                        // multiple caches, all on the same path
235                        // towards memory, all considering the
236                        // same block writable, but only one
237                        // considering it Modified
238
239                        // we get away with multiple caches (on
240                        // the same path to memory) considering
241                        // the block writeable as we always enter
242                        // the cache hierarchy through a cache,
243                        // and first snoop upwards in all other
244                        // branches
245                        blk->status &= ~BlkDirty;
246                    } else {
247                        // if we're responding after our own miss,
248                        // there's a window where the recipient didn't
249                        // know it was getting ownership and may not
250                        // have responded to snoops correctly, so we
251                        // have to respond with a shared line
252                        pkt->setHasSharers();
253                    }
254                }
255            } else {
256                // otherwise only respond with a shared copy
257                pkt->setHasSharers();
258            }
259        }
260    } else if (pkt->isUpgrade()) {
261        // sanity check
262        assert(!pkt->hasSharers());
263
264        if (blk->isDirty()) {
265            // we were in the Owned state, and a cache above us that
266            // has the line in Shared state needs to be made aware
267            // that the data it already has is in fact dirty
268            pkt->setCacheResponding();
269            blk->status &= ~BlkDirty;
270        }
271    } else {
272        assert(pkt->isInvalidate());
273        invalidateBlock(blk);
274        DPRINTF(CacheVerbose, "%s for %s (invalidation)\n", __func__,
275                pkt->print());
276    }
277}
278
279/////////////////////////////////////////////////////
280//
281// Access path: requests coming in from the CPU side
282//
283/////////////////////////////////////////////////////
284
285bool
286Cache::access(PacketPtr pkt, CacheBlk *&blk, Cycles &lat,
287              PacketList &writebacks)
288{
289    // sanity check
290    assert(pkt->isRequest());
291
292    chatty_assert(!(isReadOnly && pkt->isWrite()),
293                  "Should never see a write in a read-only cache %s\n",
294                  name());
295
296    DPRINTF(CacheVerbose, "%s for %s\n", __func__, pkt->print());
297
298    if (pkt->req->isUncacheable()) {
299        DPRINTF(Cache, "uncacheable: %s\n", pkt->print());
300
301        // flush and invalidate any existing block
302        CacheBlk *old_blk(tags->findBlock(pkt->getAddr(), pkt->isSecure()));
303        if (old_blk && old_blk->isValid()) {
304            if (old_blk->isDirty() || writebackClean)
305                writebacks.push_back(writebackBlk(old_blk));
306            else
307                writebacks.push_back(cleanEvictBlk(old_blk));
308            tags->invalidate(old_blk);
309            old_blk->invalidate();
310        }
311
312        blk = nullptr;
313        // lookupLatency is the latency in case the request is uncacheable.
314        lat = lookupLatency;
315        return false;
316    }
317
318    ContextID id = pkt->req->hasContextId() ?
319        pkt->req->contextId() : InvalidContextID;
320    // Here lat is the value passed as parameter to accessBlock() function
321    // that can modify its value.
322    blk = tags->accessBlock(pkt->getAddr(), pkt->isSecure(), lat, id);
323
324    DPRINTF(Cache, "%s %s\n", pkt->print(),
325            blk ? "hit " + blk->print() : "miss");
326
327
328    if (pkt->isEviction()) {
329        // We check for presence of block in above caches before issuing
330        // Writeback or CleanEvict to write buffer. Therefore the only
331        // possible cases can be of a CleanEvict packet coming from above
332        // encountering a Writeback generated in this cache peer cache and
333        // waiting in the write buffer. Cases of upper level peer caches
334        // generating CleanEvict and Writeback or simply CleanEvict and
335        // CleanEvict almost simultaneously will be caught by snoops sent out
336        // by crossbar.
337        WriteQueueEntry *wb_entry = writeBuffer.findMatch(pkt->getAddr(),
338                                                          pkt->isSecure());
339        if (wb_entry) {
340            assert(wb_entry->getNumTargets() == 1);
341            PacketPtr wbPkt = wb_entry->getTarget()->pkt;
342            assert(wbPkt->isWriteback());
343
344            if (pkt->isCleanEviction()) {
345                // The CleanEvict and WritebackClean snoops into other
346                // peer caches of the same level while traversing the
347                // crossbar. If a copy of the block is found, the
348                // packet is deleted in the crossbar. Hence, none of
349                // the other upper level caches connected to this
350                // cache have the block, so we can clear the
351                // BLOCK_CACHED flag in the Writeback if set and
352                // discard the CleanEvict by returning true.
353                wbPkt->clearBlockCached();
354                return true;
355            } else {
356                assert(pkt->cmd == MemCmd::WritebackDirty);
357                // Dirty writeback from above trumps our clean
358                // writeback... discard here
359                // Note: markInService will remove entry from writeback buffer.
360                markInService(wb_entry);
361                delete wbPkt;
362            }
363        }
364    }
365
366    // Writeback handling is special case.  We can write the block into
367    // the cache without having a writeable copy (or any copy at all).
368    if (pkt->isWriteback()) {
369        assert(blkSize == pkt->getSize());
370
371        // we could get a clean writeback while we are having
372        // outstanding accesses to a block, do the simple thing for
373        // now and drop the clean writeback so that we do not upset
374        // any ordering/decisions about ownership already taken
375        if (pkt->cmd == MemCmd::WritebackClean &&
376            mshrQueue.findMatch(pkt->getAddr(), pkt->isSecure())) {
377            DPRINTF(Cache, "Clean writeback %#llx to block with MSHR, "
378                    "dropping\n", pkt->getAddr());
379            return true;
380        }
381
382        if (blk == nullptr) {
383            // need to do a replacement
384            blk = allocateBlock(pkt->getAddr(), pkt->isSecure(), writebacks);
385            if (blk == nullptr) {
386                // no replaceable block available: give up, fwd to next level.
387                incMissCount(pkt);
388                return false;
389            }
390            tags->insertBlock(pkt, blk);
391
392            blk->status = (BlkValid | BlkReadable);
393            if (pkt->isSecure()) {
394                blk->status |= BlkSecure;
395            }
396        }
397        // only mark the block dirty if we got a writeback command,
398        // and leave it as is for a clean writeback
399        if (pkt->cmd == MemCmd::WritebackDirty) {
400            blk->status |= BlkDirty;
401        }
402        // if the packet does not have sharers, it is passing
403        // writable, and we got the writeback in Modified or Exclusive
404        // state, if not we are in the Owned or Shared state
405        if (!pkt->hasSharers()) {
406            blk->status |= BlkWritable;
407        }
408        // nothing else to do; writeback doesn't expect response
409        assert(!pkt->needsResponse());
410        std::memcpy(blk->data, pkt->getConstPtr<uint8_t>(), blkSize);
411        DPRINTF(Cache, "%s new state is %s\n", __func__, blk->print());
412        incHitCount(pkt);
413        return true;
414    } else if (pkt->cmd == MemCmd::CleanEvict) {
415        if (blk != nullptr) {
416            // Found the block in the tags, need to stop CleanEvict from
417            // propagating further down the hierarchy. Returning true will
418            // treat the CleanEvict like a satisfied write request and delete
419            // it.
420            return true;
421        }
422        // We didn't find the block here, propagate the CleanEvict further
423        // down the memory hierarchy. Returning false will treat the CleanEvict
424        // like a Writeback which could not find a replaceable block so has to
425        // go to next level.
426        return false;
427    } else if (blk && (pkt->needsWritable() ? blk->isWritable() :
428                       blk->isReadable())) {
429        // OK to satisfy access
430        incHitCount(pkt);
431        satisfyRequest(pkt, blk);
432        maintainClusivity(pkt->fromCache(), blk);
433
434        return true;
435    }
436
437    // Can't satisfy access normally... either no block (blk == nullptr)
438    // or have block but need writable
439
440    incMissCount(pkt);
441
442    if (blk == nullptr && pkt->isLLSC() && pkt->isWrite()) {
443        // complete miss on store conditional... just give up now
444        pkt->req->setExtraData(0);
445        return true;
446    }
447
448    return false;
449}
450
451void
452Cache::maintainClusivity(bool from_cache, CacheBlk *blk)
453{
454    if (from_cache && blk && blk->isValid() && !blk->isDirty() &&
455        clusivity == Enums::mostly_excl) {
456        // if we have responded to a cache, and our block is still
457        // valid, but not dirty, and this cache is mostly exclusive
458        // with respect to the cache above, drop the block
459        invalidateBlock(blk);
460    }
461}
462
463void
464Cache::doWritebacks(PacketList& writebacks, Tick forward_time)
465{
466    while (!writebacks.empty()) {
467        PacketPtr wbPkt = writebacks.front();
468        // We use forwardLatency here because we are copying writebacks to
469        // write buffer.  Call isCachedAbove for both Writebacks and
470        // CleanEvicts. If isCachedAbove returns true we set BLOCK_CACHED flag
471        // in Writebacks and discard CleanEvicts.
472        if (isCachedAbove(wbPkt)) {
473            if (wbPkt->cmd == MemCmd::CleanEvict) {
474                // Delete CleanEvict because cached copies exist above. The
475                // packet destructor will delete the request object because
476                // this is a non-snoop request packet which does not require a
477                // response.
478                delete wbPkt;
479            } else if (wbPkt->cmd == MemCmd::WritebackClean) {
480                // clean writeback, do not send since the block is
481                // still cached above
482                assert(writebackClean);
483                delete wbPkt;
484            } else {
485                assert(wbPkt->cmd == MemCmd::WritebackDirty);
486                // Set BLOCK_CACHED flag in Writeback and send below, so that
487                // the Writeback does not reset the bit corresponding to this
488                // address in the snoop filter below.
489                wbPkt->setBlockCached();
490                allocateWriteBuffer(wbPkt, forward_time);
491            }
492        } else {
493            // If the block is not cached above, send packet below. Both
494            // CleanEvict and Writeback with BLOCK_CACHED flag cleared will
495            // reset the bit corresponding to this address in the snoop filter
496            // below.
497            allocateWriteBuffer(wbPkt, forward_time);
498        }
499        writebacks.pop_front();
500    }
501}
502
503void
504Cache::doWritebacksAtomic(PacketList& writebacks)
505{
506    while (!writebacks.empty()) {
507        PacketPtr wbPkt = writebacks.front();
508        // Call isCachedAbove for both Writebacks and CleanEvicts. If
509        // isCachedAbove returns true we set BLOCK_CACHED flag in Writebacks
510        // and discard CleanEvicts.
511        if (isCachedAbove(wbPkt, false)) {
512            if (wbPkt->cmd == MemCmd::WritebackDirty) {
513                // Set BLOCK_CACHED flag in Writeback and send below,
514                // so that the Writeback does not reset the bit
515                // corresponding to this address in the snoop filter
516                // below. We can discard CleanEvicts because cached
517                // copies exist above. Atomic mode isCachedAbove
518                // modifies packet to set BLOCK_CACHED flag
519                memSidePort->sendAtomic(wbPkt);
520            }
521        } else {
522            // If the block is not cached above, send packet below. Both
523            // CleanEvict and Writeback with BLOCK_CACHED flag cleared will
524            // reset the bit corresponding to this address in the snoop filter
525            // below.
526            memSidePort->sendAtomic(wbPkt);
527        }
528        writebacks.pop_front();
529        // In case of CleanEvicts, the packet destructor will delete the
530        // request object because this is a non-snoop request packet which
531        // does not require a response.
532        delete wbPkt;
533    }
534}
535
536
537void
538Cache::recvTimingSnoopResp(PacketPtr pkt)
539{
540    DPRINTF(Cache, "%s for %s\n", __func__, pkt->print());
541
542    assert(pkt->isResponse());
543    assert(!system->bypassCaches());
544
545    // determine if the response is from a snoop request we created
546    // (in which case it should be in the outstandingSnoop), or if we
547    // merely forwarded someone else's snoop request
548    const bool forwardAsSnoop = outstandingSnoop.find(pkt->req) ==
549        outstandingSnoop.end();
550
551    if (!forwardAsSnoop) {
552        // the packet came from this cache, so sink it here and do not
553        // forward it
554        assert(pkt->cmd == MemCmd::HardPFResp);
555
556        outstandingSnoop.erase(pkt->req);
557
558        DPRINTF(Cache, "Got prefetch response from above for addr "
559                "%#llx (%s)\n", pkt->getAddr(), pkt->isSecure() ? "s" : "ns");
560        recvTimingResp(pkt);
561        return;
562    }
563
564    // forwardLatency is set here because there is a response from an
565    // upper level cache.
566    // To pay the delay that occurs if the packet comes from the bus,
567    // we charge also headerDelay.
568    Tick snoop_resp_time = clockEdge(forwardLatency) + pkt->headerDelay;
569    // Reset the timing of the packet.
570    pkt->headerDelay = pkt->payloadDelay = 0;
571    memSidePort->schedTimingSnoopResp(pkt, snoop_resp_time);
572}
573
574void
575Cache::promoteWholeLineWrites(PacketPtr pkt)
576{
577    // Cache line clearing instructions
578    if (doFastWrites && (pkt->cmd == MemCmd::WriteReq) &&
579        (pkt->getSize() == blkSize) && (pkt->getOffset(blkSize) == 0)) {
580        pkt->cmd = MemCmd::WriteLineReq;
581        DPRINTF(Cache, "packet promoted from Write to WriteLineReq\n");
582    }
583}
584
585bool
586Cache::recvTimingReq(PacketPtr pkt)
587{
588    DPRINTF(CacheTags, "%s tags: %s\n", __func__, tags->print());
589
590    assert(pkt->isRequest());
591
592    // Just forward the packet if caches are disabled.
593    if (system->bypassCaches()) {
594        // @todo This should really enqueue the packet rather
595        bool M5_VAR_USED success = memSidePort->sendTimingReq(pkt);
596        assert(success);
597        return true;
598    }
599
600    promoteWholeLineWrites(pkt);
601
602    if (pkt->cacheResponding()) {
603        // a cache above us (but not where the packet came from) is
604        // responding to the request, in other words it has the line
605        // in Modified or Owned state
606        DPRINTF(Cache, "Cache above responding to %s: not responding\n",
607                pkt->print());
608
609        // if the packet needs the block to be writable, and the cache
610        // that has promised to respond (setting the cache responding
611        // flag) is not providing writable (it is in Owned rather than
612        // the Modified state), we know that there may be other Shared
613        // copies in the system; go out and invalidate them all
614        assert(pkt->needsWritable() && !pkt->responderHadWritable());
615
616        // an upstream cache that had the line in Owned state
617        // (dirty, but not writable), is responding and thus
618        // transferring the dirty line from one branch of the
619        // cache hierarchy to another
620
621        // send out an express snoop and invalidate all other
622        // copies (snooping a packet that needs writable is the
623        // same as an invalidation), thus turning the Owned line
624        // into a Modified line, note that we don't invalidate the
625        // block in the current cache or any other cache on the
626        // path to memory
627
628        // create a downstream express snoop with cleared packet
629        // flags, there is no need to allocate any data as the
630        // packet is merely used to co-ordinate state transitions
631        Packet *snoop_pkt = new Packet(pkt, true, false);
632
633        // also reset the bus time that the original packet has
634        // not yet paid for
635        snoop_pkt->headerDelay = snoop_pkt->payloadDelay = 0;
636
637        // make this an instantaneous express snoop, and let the
638        // other caches in the system know that the another cache
639        // is responding, because we have found the authorative
640        // copy (Modified or Owned) that will supply the right
641        // data
642        snoop_pkt->setExpressSnoop();
643        snoop_pkt->setCacheResponding();
644
645        // this express snoop travels towards the memory, and at
646        // every crossbar it is snooped upwards thus reaching
647        // every cache in the system
648        bool M5_VAR_USED success = memSidePort->sendTimingReq(snoop_pkt);
649        // express snoops always succeed
650        assert(success);
651
652        // main memory will delete the snoop packet
653
654        // queue for deletion, as opposed to immediate deletion, as
655        // the sending cache is still relying on the packet
656        pendingDelete.reset(pkt);
657
658        // no need to take any further action in this particular cache
659        // as an upstram cache has already committed to responding,
660        // and we have already sent out any express snoops in the
661        // section above to ensure all other copies in the system are
662        // invalidated
663        return true;
664    }
665
666    // anything that is merely forwarded pays for the forward latency and
667    // the delay provided by the crossbar
668    Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
669
670    // We use lookupLatency here because it is used to specify the latency
671    // to access.
672    Cycles lat = lookupLatency;
673    CacheBlk *blk = nullptr;
674    bool satisfied = false;
675    {
676        PacketList writebacks;
677        // Note that lat is passed by reference here. The function
678        // access() calls accessBlock() which can modify lat value.
679        satisfied = access(pkt, blk, lat, writebacks);
680
681        // copy writebacks to write buffer here to ensure they logically
682        // proceed anything happening below
683        doWritebacks(writebacks, forward_time);
684    }
685
686    // Here we charge the headerDelay that takes into account the latencies
687    // of the bus, if the packet comes from it.
688    // The latency charged it is just lat that is the value of lookupLatency
689    // modified by access() function, or if not just lookupLatency.
690    // In case of a hit we are neglecting response latency.
691    // In case of a miss we are neglecting forward latency.
692    Tick request_time = clockEdge(lat) + pkt->headerDelay;
693    // Here we reset the timing of the packet.
694    pkt->headerDelay = pkt->payloadDelay = 0;
695
696    // track time of availability of next prefetch, if any
697    Tick next_pf_time = MaxTick;
698
699    bool needsResponse = pkt->needsResponse();
700
701    if (satisfied) {
702        // should never be satisfying an uncacheable access as we
703        // flush and invalidate any existing block as part of the
704        // lookup
705        assert(!pkt->req->isUncacheable());
706
707        // hit (for all other request types)
708
709        if (prefetcher && (prefetchOnAccess ||
710                           (blk && blk->wasPrefetched()))) {
711            if (blk)
712                blk->status &= ~BlkHWPrefetched;
713
714            // Don't notify on SWPrefetch
715            if (!pkt->cmd.isSWPrefetch())
716                next_pf_time = prefetcher->notify(pkt);
717        }
718
719        if (needsResponse) {
720            pkt->makeTimingResponse();
721            // @todo: Make someone pay for this
722            pkt->headerDelay = pkt->payloadDelay = 0;
723
724            // In this case we are considering request_time that takes
725            // into account the delay of the xbar, if any, and just
726            // lat, neglecting responseLatency, modelling hit latency
727            // just as lookupLatency or or the value of lat overriden
728            // by access(), that calls accessBlock() function.
729            cpuSidePort->schedTimingResp(pkt, request_time, true);
730        } else {
731            DPRINTF(Cache, "%s satisfied %s, no response needed\n", __func__,
732                    pkt->print());
733
734            // queue the packet for deletion, as the sending cache is
735            // still relying on it; if the block is found in access(),
736            // CleanEvict and Writeback messages will be deleted
737            // here as well
738            pendingDelete.reset(pkt);
739        }
740    } else {
741        // miss
742
743        Addr blk_addr = blockAlign(pkt->getAddr());
744
745        // ignore any existing MSHR if we are dealing with an
746        // uncacheable request
747        MSHR *mshr = pkt->req->isUncacheable() ? nullptr :
748            mshrQueue.findMatch(blk_addr, pkt->isSecure());
749
750        // Software prefetch handling:
751        // To keep the core from waiting on data it won't look at
752        // anyway, send back a response with dummy data. Miss handling
753        // will continue asynchronously. Unfortunately, the core will
754        // insist upon freeing original Packet/Request, so we have to
755        // create a new pair with a different lifecycle. Note that this
756        // processing happens before any MSHR munging on the behalf of
757        // this request because this new Request will be the one stored
758        // into the MSHRs, not the original.
759        if (pkt->cmd.isSWPrefetch()) {
760            assert(needsResponse);
761            assert(pkt->req->hasPaddr());
762            assert(!pkt->req->isUncacheable());
763
764            // There's no reason to add a prefetch as an additional target
765            // to an existing MSHR. If an outstanding request is already
766            // in progress, there is nothing for the prefetch to do.
767            // If this is the case, we don't even create a request at all.
768            PacketPtr pf = nullptr;
769
770            if (!mshr) {
771                // copy the request and create a new SoftPFReq packet
772                RequestPtr req = new Request(pkt->req->getPaddr(),
773                                             pkt->req->getSize(),
774                                             pkt->req->getFlags(),
775                                             pkt->req->masterId());
776                pf = new Packet(req, pkt->cmd);
777                pf->allocate();
778                assert(pf->getAddr() == pkt->getAddr());
779                assert(pf->getSize() == pkt->getSize());
780            }
781
782            pkt->makeTimingResponse();
783
784            // request_time is used here, taking into account lat and the delay
785            // charged if the packet comes from the xbar.
786            cpuSidePort->schedTimingResp(pkt, request_time, true);
787
788            // If an outstanding request is in progress (we found an
789            // MSHR) this is set to null
790            pkt = pf;
791        }
792
793        if (mshr) {
794            /// MSHR hit
795            /// @note writebacks will be checked in getNextMSHR()
796            /// for any conflicting requests to the same block
797
798            //@todo remove hw_pf here
799
800            // Coalesce unless it was a software prefetch (see above).
801            if (pkt) {
802                assert(!pkt->isWriteback());
803                // CleanEvicts corresponding to blocks which have
804                // outstanding requests in MSHRs are simply sunk here
805                if (pkt->cmd == MemCmd::CleanEvict) {
806                    pendingDelete.reset(pkt);
807                } else {
808                    DPRINTF(Cache, "%s coalescing MSHR for %s\n", __func__,
809                            pkt->print());
810
811                    assert(pkt->req->masterId() < system->maxMasters());
812                    mshr_hits[pkt->cmdToIndex()][pkt->req->masterId()]++;
813                    // We use forward_time here because it is the same
814                    // considering new targets. We have multiple
815                    // requests for the same address here. It
816                    // specifies the latency to allocate an internal
817                    // buffer and to schedule an event to the queued
818                    // port and also takes into account the additional
819                    // delay of the xbar.
820                    mshr->allocateTarget(pkt, forward_time, order++,
821                                         allocOnFill(pkt->cmd));
822                    if (mshr->getNumTargets() == numTarget) {
823                        noTargetMSHR = mshr;
824                        setBlocked(Blocked_NoTargets);
825                        // need to be careful with this... if this mshr isn't
826                        // ready yet (i.e. time > curTick()), we don't want to
827                        // move it ahead of mshrs that are ready
828                        // mshrQueue.moveToFront(mshr);
829                    }
830                }
831                // We should call the prefetcher reguardless if the request is
832                // satisfied or not, reguardless if the request is in the MSHR
833                // or not.  The request could be a ReadReq hit, but still not
834                // satisfied (potentially because of a prior write to the same
835                // cache line.  So, even when not satisfied, tehre is an MSHR
836                // already allocated for this, we need to let the prefetcher
837                // know about the request
838                if (prefetcher) {
839                    // Don't notify on SWPrefetch
840                    if (!pkt->cmd.isSWPrefetch())
841                        next_pf_time = prefetcher->notify(pkt);
842                }
843            }
844        } else {
845            // no MSHR
846            assert(pkt->req->masterId() < system->maxMasters());
847            if (pkt->req->isUncacheable()) {
848                mshr_uncacheable[pkt->cmdToIndex()][pkt->req->masterId()]++;
849            } else {
850                mshr_misses[pkt->cmdToIndex()][pkt->req->masterId()]++;
851            }
852
853            if (pkt->isEviction() ||
854                (pkt->req->isUncacheable() && pkt->isWrite())) {
855                // We use forward_time here because there is an
856                // uncached memory write, forwarded to WriteBuffer.
857                allocateWriteBuffer(pkt, forward_time);
858            } else {
859                if (blk && blk->isValid()) {
860                    // should have flushed and have no valid block
861                    assert(!pkt->req->isUncacheable());
862
863                    // If we have a write miss to a valid block, we
864                    // need to mark the block non-readable.  Otherwise
865                    // if we allow reads while there's an outstanding
866                    // write miss, the read could return stale data
867                    // out of the cache block... a more aggressive
868                    // system could detect the overlap (if any) and
869                    // forward data out of the MSHRs, but we don't do
870                    // that yet.  Note that we do need to leave the
871                    // block valid so that it stays in the cache, in
872                    // case we get an upgrade response (and hence no
873                    // new data) when the write miss completes.
874                    // As long as CPUs do proper store/load forwarding
875                    // internally, and have a sufficiently weak memory
876                    // model, this is probably unnecessary, but at some
877                    // point it must have seemed like we needed it...
878                    assert(pkt->needsWritable());
879                    assert(!blk->isWritable());
880                    blk->status &= ~BlkReadable;
881                }
882                // Here we are using forward_time, modelling the latency of
883                // a miss (outbound) just as forwardLatency, neglecting the
884                // lookupLatency component.
885                allocateMissBuffer(pkt, forward_time);
886            }
887
888            if (prefetcher) {
889                // Don't notify on SWPrefetch
890                if (!pkt->cmd.isSWPrefetch())
891                    next_pf_time = prefetcher->notify(pkt);
892            }
893        }
894    }
895
896    if (next_pf_time != MaxTick)
897        schedMemSideSendEvent(next_pf_time);
898
899    return true;
900}
901
902PacketPtr
903Cache::createMissPacket(PacketPtr cpu_pkt, CacheBlk *blk,
904                        bool needsWritable) const
905{
906    // should never see evictions here
907    assert(!cpu_pkt->isEviction());
908
909    bool blkValid = blk && blk->isValid();
910
911    if (cpu_pkt->req->isUncacheable() ||
912        (!blkValid && cpu_pkt->isUpgrade()) ||
913        cpu_pkt->cmd == MemCmd::InvalidateReq) {
914        // uncacheable requests and upgrades from upper-level caches
915        // that missed completely just go through as is
916        return nullptr;
917    }
918
919    assert(cpu_pkt->needsResponse());
920
921    MemCmd cmd;
922    // @TODO make useUpgrades a parameter.
923    // Note that ownership protocols require upgrade, otherwise a
924    // write miss on a shared owned block will generate a ReadExcl,
925    // which will clobber the owned copy.
926    const bool useUpgrades = true;
927    if (cpu_pkt->cmd == MemCmd::WriteLineReq) {
928        assert(!blkValid || !blk->isWritable());
929        // forward as invalidate to all other caches, this gives us
930        // the line in Exclusive state, and invalidates all other
931        // copies
932        cmd = MemCmd::InvalidateReq;
933    } else if (blkValid && useUpgrades) {
934        // only reason to be here is that blk is read only and we need
935        // it to be writable
936        assert(needsWritable);
937        assert(!blk->isWritable());
938        cmd = cpu_pkt->isLLSC() ? MemCmd::SCUpgradeReq : MemCmd::UpgradeReq;
939    } else if (cpu_pkt->cmd == MemCmd::SCUpgradeFailReq ||
940               cpu_pkt->cmd == MemCmd::StoreCondFailReq) {
941        // Even though this SC will fail, we still need to send out the
942        // request and get the data to supply it to other snoopers in the case
943        // where the determination the StoreCond fails is delayed due to
944        // all caches not being on the same local bus.
945        cmd = MemCmd::SCUpgradeFailReq;
946    } else {
947        // block is invalid
948        cmd = needsWritable ? MemCmd::ReadExReq :
949            (isReadOnly ? MemCmd::ReadCleanReq : MemCmd::ReadSharedReq);
950    }
951    PacketPtr pkt = new Packet(cpu_pkt->req, cmd, blkSize);
952
953    // if there are upstream caches that have already marked the
954    // packet as having sharers (not passing writable), pass that info
955    // downstream
956    if (cpu_pkt->hasSharers() && !needsWritable) {
957        // note that cpu_pkt may have spent a considerable time in the
958        // MSHR queue and that the information could possibly be out
959        // of date, however, there is no harm in conservatively
960        // assuming the block has sharers
961        pkt->setHasSharers();
962        DPRINTF(Cache, "%s: passing hasSharers from %s to %s\n",
963                __func__, cpu_pkt->print(), pkt->print());
964    }
965
966    // the packet should be block aligned
967    assert(pkt->getAddr() == blockAlign(pkt->getAddr()));
968
969    pkt->allocate();
970    DPRINTF(Cache, "%s: created %s from %s\n", __func__, pkt->print(),
971            cpu_pkt->print());
972    return pkt;
973}
974
975
976Tick
977Cache::recvAtomic(PacketPtr pkt)
978{
979    // We are in atomic mode so we pay just for lookupLatency here.
980    Cycles lat = lookupLatency;
981
982    // Forward the request if the system is in cache bypass mode.
983    if (system->bypassCaches())
984        return ticksToCycles(memSidePort->sendAtomic(pkt));
985
986    promoteWholeLineWrites(pkt);
987
988    // follow the same flow as in recvTimingReq, and check if a cache
989    // above us is responding
990    if (pkt->cacheResponding()) {
991        DPRINTF(Cache, "Cache above responding to %s: not responding\n",
992                pkt->print());
993
994        // if a cache is responding, and it had the line in Owned
995        // rather than Modified state, we need to invalidate any
996        // copies that are not on the same path to memory
997        assert(pkt->needsWritable() && !pkt->responderHadWritable());
998        lat += ticksToCycles(memSidePort->sendAtomic(pkt));
999
1000        return lat * clockPeriod();
1001    }
1002
1003    // should assert here that there are no outstanding MSHRs or
1004    // writebacks... that would mean that someone used an atomic
1005    // access in timing mode
1006
1007    CacheBlk *blk = nullptr;
1008    PacketList writebacks;
1009    bool satisfied = access(pkt, blk, lat, writebacks);
1010
1011    // handle writebacks resulting from the access here to ensure they
1012    // logically proceed anything happening below
1013    doWritebacksAtomic(writebacks);
1014
1015    if (!satisfied) {
1016        // MISS
1017
1018        // deal with the packets that go through the write path of
1019        // the cache, i.e. any evictions and uncacheable writes
1020        if (pkt->isEviction() ||
1021            (pkt->req->isUncacheable() && pkt->isWrite())) {
1022            lat += ticksToCycles(memSidePort->sendAtomic(pkt));
1023            return lat * clockPeriod();
1024        }
1025        // only misses left
1026
1027        PacketPtr bus_pkt = createMissPacket(pkt, blk, pkt->needsWritable());
1028
1029        bool is_forward = (bus_pkt == nullptr);
1030
1031        if (is_forward) {
1032            // just forwarding the same request to the next level
1033            // no local cache operation involved
1034            bus_pkt = pkt;
1035        }
1036
1037        DPRINTF(Cache, "%s: Sending an atomic %s\n", __func__,
1038                bus_pkt->print());
1039
1040#if TRACING_ON
1041        CacheBlk::State old_state = blk ? blk->status : 0;
1042#endif
1043
1044        lat += ticksToCycles(memSidePort->sendAtomic(bus_pkt));
1045
1046        bool is_invalidate = bus_pkt->isInvalidate();
1047
1048        // We are now dealing with the response handling
1049        DPRINTF(Cache, "%s: Receive response: %s in state %i\n", __func__,
1050                bus_pkt->print(), old_state);
1051
1052        // If packet was a forward, the response (if any) is already
1053        // in place in the bus_pkt == pkt structure, so we don't need
1054        // to do anything.  Otherwise, use the separate bus_pkt to
1055        // generate response to pkt and then delete it.
1056        if (!is_forward) {
1057            if (pkt->needsResponse()) {
1058                assert(bus_pkt->isResponse());
1059                if (bus_pkt->isError()) {
1060                    pkt->makeAtomicResponse();
1061                    pkt->copyError(bus_pkt);
1062                } else if (pkt->cmd == MemCmd::WriteLineReq) {
1063                    // note the use of pkt, not bus_pkt here.
1064
1065                    // write-line request to the cache that promoted
1066                    // the write to a whole line
1067                    blk = handleFill(pkt, blk, writebacks,
1068                                     allocOnFill(pkt->cmd));
1069                    assert(blk != NULL);
1070                    is_invalidate = false;
1071                    satisfyRequest(pkt, blk);
1072                } else if (bus_pkt->isRead() ||
1073                           bus_pkt->cmd == MemCmd::UpgradeResp) {
1074                    // we're updating cache state to allow us to
1075                    // satisfy the upstream request from the cache
1076                    blk = handleFill(bus_pkt, blk, writebacks,
1077                                     allocOnFill(pkt->cmd));
1078                    satisfyRequest(pkt, blk);
1079                    maintainClusivity(pkt->fromCache(), blk);
1080                } else {
1081                    // we're satisfying the upstream request without
1082                    // modifying cache state, e.g., a write-through
1083                    pkt->makeAtomicResponse();
1084                }
1085            }
1086            delete bus_pkt;
1087        }
1088
1089        if (is_invalidate && blk && blk->isValid()) {
1090            invalidateBlock(blk);
1091        }
1092    }
1093
1094    // Note that we don't invoke the prefetcher at all in atomic mode.
1095    // It's not clear how to do it properly, particularly for
1096    // prefetchers that aggressively generate prefetch candidates and
1097    // rely on bandwidth contention to throttle them; these will tend
1098    // to pollute the cache in atomic mode since there is no bandwidth
1099    // contention.  If we ever do want to enable prefetching in atomic
1100    // mode, though, this is the place to do it... see timingAccess()
1101    // for an example (though we'd want to issue the prefetch(es)
1102    // immediately rather than calling requestMemSideBus() as we do
1103    // there).
1104
1105    // do any writebacks resulting from the response handling
1106    doWritebacksAtomic(writebacks);
1107
1108    // if we used temp block, check to see if its valid and if so
1109    // clear it out, but only do so after the call to recvAtomic is
1110    // finished so that any downstream observers (such as a snoop
1111    // filter), first see the fill, and only then see the eviction
1112    if (blk == tempBlock && tempBlock->isValid()) {
1113        // the atomic CPU calls recvAtomic for fetch and load/store
1114        // sequentuially, and we may already have a tempBlock
1115        // writeback from the fetch that we have not yet sent
1116        if (tempBlockWriteback) {
1117            // if that is the case, write the prevoius one back, and
1118            // do not schedule any new event
1119            writebackTempBlockAtomic();
1120        } else {
1121            // the writeback/clean eviction happens after the call to
1122            // recvAtomic has finished (but before any successive
1123            // calls), so that the response handling from the fill is
1124            // allowed to happen first
1125            schedule(writebackTempBlockAtomicEvent, curTick());
1126        }
1127
1128        tempBlockWriteback = (blk->isDirty() || writebackClean) ?
1129            writebackBlk(blk) : cleanEvictBlk(blk);
1130        blk->invalidate();
1131    }
1132
1133    if (pkt->needsResponse()) {
1134        pkt->makeAtomicResponse();
1135    }
1136
1137    return lat * clockPeriod();
1138}
1139
1140
1141void
1142Cache::functionalAccess(PacketPtr pkt, bool fromCpuSide)
1143{
1144    if (system->bypassCaches()) {
1145        // Packets from the memory side are snoop request and
1146        // shouldn't happen in bypass mode.
1147        assert(fromCpuSide);
1148
1149        // The cache should be flushed if we are in cache bypass mode,
1150        // so we don't need to check if we need to update anything.
1151        memSidePort->sendFunctional(pkt);
1152        return;
1153    }
1154
1155    Addr blk_addr = blockAlign(pkt->getAddr());
1156    bool is_secure = pkt->isSecure();
1157    CacheBlk *blk = tags->findBlock(pkt->getAddr(), is_secure);
1158    MSHR *mshr = mshrQueue.findMatch(blk_addr, is_secure);
1159
1160    pkt->pushLabel(name());
1161
1162    CacheBlkPrintWrapper cbpw(blk);
1163
1164    // Note that just because an L2/L3 has valid data doesn't mean an
1165    // L1 doesn't have a more up-to-date modified copy that still
1166    // needs to be found.  As a result we always update the request if
1167    // we have it, but only declare it satisfied if we are the owner.
1168
1169    // see if we have data at all (owned or otherwise)
1170    bool have_data = blk && blk->isValid()
1171        && pkt->checkFunctional(&cbpw, blk_addr, is_secure, blkSize,
1172                                blk->data);
1173
1174    // data we have is dirty if marked as such or if we have an
1175    // in-service MSHR that is pending a modified line
1176    bool have_dirty =
1177        have_data && (blk->isDirty() ||
1178                      (mshr && mshr->inService && mshr->isPendingModified()));
1179
1180    bool done = have_dirty
1181        || cpuSidePort->checkFunctional(pkt)
1182        || mshrQueue.checkFunctional(pkt, blk_addr)
1183        || writeBuffer.checkFunctional(pkt, blk_addr)
1184        || memSidePort->checkFunctional(pkt);
1185
1186    DPRINTF(CacheVerbose, "%s: %s %s%s%s\n", __func__,  pkt->print(),
1187            (blk && blk->isValid()) ? "valid " : "",
1188            have_data ? "data " : "", done ? "done " : "");
1189
1190    // We're leaving the cache, so pop cache->name() label
1191    pkt->popLabel();
1192
1193    if (done) {
1194        pkt->makeResponse();
1195    } else {
1196        // if it came as a request from the CPU side then make sure it
1197        // continues towards the memory side
1198        if (fromCpuSide) {
1199            memSidePort->sendFunctional(pkt);
1200        } else if (cpuSidePort->isSnooping()) {
1201            // if it came from the memory side, it must be a snoop request
1202            // and we should only forward it if we are forwarding snoops
1203            cpuSidePort->sendFunctionalSnoop(pkt);
1204        }
1205    }
1206}
1207
1208
1209/////////////////////////////////////////////////////
1210//
1211// Response handling: responses from the memory side
1212//
1213/////////////////////////////////////////////////////
1214
1215
1216void
1217Cache::handleUncacheableWriteResp(PacketPtr pkt)
1218{
1219    Tick completion_time = clockEdge(responseLatency) +
1220        pkt->headerDelay + pkt->payloadDelay;
1221
1222    // Reset the bus additional time as it is now accounted for
1223    pkt->headerDelay = pkt->payloadDelay = 0;
1224
1225    cpuSidePort->schedTimingResp(pkt, completion_time, true);
1226}
1227
1228void
1229Cache::recvTimingResp(PacketPtr pkt)
1230{
1231    assert(pkt->isResponse());
1232
1233    // all header delay should be paid for by the crossbar, unless
1234    // this is a prefetch response from above
1235    panic_if(pkt->headerDelay != 0 && pkt->cmd != MemCmd::HardPFResp,
1236             "%s saw a non-zero packet delay\n", name());
1237
1238    bool is_error = pkt->isError();
1239
1240    if (is_error) {
1241        DPRINTF(Cache, "%s: Cache received %s with error\n", __func__,
1242                pkt->print());
1243    }
1244
1245    DPRINTF(Cache, "%s: Handling response %s\n", __func__,
1246            pkt->print());
1247
1248    // if this is a write, we should be looking at an uncacheable
1249    // write
1250    if (pkt->isWrite()) {
1251        assert(pkt->req->isUncacheable());
1252        handleUncacheableWriteResp(pkt);
1253        return;
1254    }
1255
1256    // we have dealt with any (uncacheable) writes above, from here on
1257    // we know we are dealing with an MSHR due to a miss or a prefetch
1258    MSHR *mshr = dynamic_cast<MSHR*>(pkt->popSenderState());
1259    assert(mshr);
1260
1261    if (mshr == noTargetMSHR) {
1262        // we always clear at least one target
1263        clearBlocked(Blocked_NoTargets);
1264        noTargetMSHR = nullptr;
1265    }
1266
1267    // Initial target is used just for stats
1268    MSHR::Target *initial_tgt = mshr->getTarget();
1269    int stats_cmd_idx = initial_tgt->pkt->cmdToIndex();
1270    Tick miss_latency = curTick() - initial_tgt->recvTime;
1271
1272    if (pkt->req->isUncacheable()) {
1273        assert(pkt->req->masterId() < system->maxMasters());
1274        mshr_uncacheable_lat[stats_cmd_idx][pkt->req->masterId()] +=
1275            miss_latency;
1276    } else {
1277        assert(pkt->req->masterId() < system->maxMasters());
1278        mshr_miss_latency[stats_cmd_idx][pkt->req->masterId()] +=
1279            miss_latency;
1280    }
1281
1282    bool wasFull = mshrQueue.isFull();
1283
1284    PacketList writebacks;
1285
1286    Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
1287
1288    // upgrade deferred targets if the response has no sharers, and is
1289    // thus passing writable
1290    if (!pkt->hasSharers()) {
1291        mshr->promoteWritable();
1292    }
1293
1294    bool is_fill = !mshr->isForward &&
1295        (pkt->isRead() || pkt->cmd == MemCmd::UpgradeResp);
1296
1297    CacheBlk *blk = tags->findBlock(pkt->getAddr(), pkt->isSecure());
1298
1299    if (is_fill && !is_error) {
1300        DPRINTF(Cache, "Block for addr %#llx being updated in Cache\n",
1301                pkt->getAddr());
1302
1303        blk = handleFill(pkt, blk, writebacks, mshr->allocOnFill());
1304        assert(blk != nullptr);
1305    }
1306
1307    // allow invalidation responses originating from write-line
1308    // requests to be discarded
1309    bool is_invalidate = pkt->isInvalidate();
1310
1311    // First offset for critical word first calculations
1312    int initial_offset = initial_tgt->pkt->getOffset(blkSize);
1313
1314    bool from_cache = false;
1315    MSHR::TargetList targets = mshr->extractServiceableTargets(pkt);
1316    for (auto &target: targets) {
1317        Packet *tgt_pkt = target.pkt;
1318        switch (target.source) {
1319          case MSHR::Target::FromCPU:
1320            Tick completion_time;
1321            // Here we charge on completion_time the delay of the xbar if the
1322            // packet comes from it, charged on headerDelay.
1323            completion_time = pkt->headerDelay;
1324
1325            // Software prefetch handling for cache closest to core
1326            if (tgt_pkt->cmd.isSWPrefetch()) {
1327                // a software prefetch would have already been ack'd
1328                // immediately with dummy data so the core would be able to
1329                // retire it. This request completes right here, so we
1330                // deallocate it.
1331                delete tgt_pkt->req;
1332                delete tgt_pkt;
1333                break; // skip response
1334            }
1335
1336            // keep track of whether we have responded to another
1337            // cache
1338            from_cache = from_cache || tgt_pkt->fromCache();
1339
1340            // unlike the other packet flows, where data is found in other
1341            // caches or memory and brought back, write-line requests always
1342            // have the data right away, so the above check for "is fill?"
1343            // cannot actually be determined until examining the stored MSHR
1344            // state. We "catch up" with that logic here, which is duplicated
1345            // from above.
1346            if (tgt_pkt->cmd == MemCmd::WriteLineReq) {
1347                assert(!is_error);
1348                // we got the block in a writable state, so promote
1349                // any deferred targets if possible
1350                mshr->promoteWritable();
1351                // NB: we use the original packet here and not the response!
1352                blk = handleFill(tgt_pkt, blk, writebacks,
1353                                 targets.allocOnFill);
1354                assert(blk != nullptr);
1355
1356                // treat as a fill, and discard the invalidation
1357                // response
1358                is_fill = true;
1359                is_invalidate = false;
1360            }
1361
1362            if (is_fill) {
1363                satisfyRequest(tgt_pkt, blk, true, mshr->hasPostDowngrade());
1364
1365                // How many bytes past the first request is this one
1366                int transfer_offset =
1367                    tgt_pkt->getOffset(blkSize) - initial_offset;
1368                if (transfer_offset < 0) {
1369                    transfer_offset += blkSize;
1370                }
1371
1372                // If not critical word (offset) return payloadDelay.
1373                // responseLatency is the latency of the return path
1374                // from lower level caches/memory to an upper level cache or
1375                // the core.
1376                completion_time += clockEdge(responseLatency) +
1377                    (transfer_offset ? pkt->payloadDelay : 0);
1378
1379                assert(!tgt_pkt->req->isUncacheable());
1380
1381                assert(tgt_pkt->req->masterId() < system->maxMasters());
1382                missLatency[tgt_pkt->cmdToIndex()][tgt_pkt->req->masterId()] +=
1383                    completion_time - target.recvTime;
1384            } else if (pkt->cmd == MemCmd::UpgradeFailResp) {
1385                // failed StoreCond upgrade
1386                assert(tgt_pkt->cmd == MemCmd::StoreCondReq ||
1387                       tgt_pkt->cmd == MemCmd::StoreCondFailReq ||
1388                       tgt_pkt->cmd == MemCmd::SCUpgradeFailReq);
1389                // responseLatency is the latency of the return path
1390                // from lower level caches/memory to an upper level cache or
1391                // the core.
1392                completion_time += clockEdge(responseLatency) +
1393                    pkt->payloadDelay;
1394                tgt_pkt->req->setExtraData(0);
1395            } else {
1396                // We are about to send a response to a cache above
1397                // that asked for an invalidation; we need to
1398                // invalidate our copy immediately as the most
1399                // up-to-date copy of the block will now be in the
1400                // cache above. It will also prevent this cache from
1401                // responding (if the block was previously dirty) to
1402                // snoops as they should snoop the caches above where
1403                // they will get the response from.
1404                if (is_invalidate && blk && blk->isValid()) {
1405                    invalidateBlock(blk);
1406                }
1407                // not a cache fill, just forwarding response
1408                // responseLatency is the latency of the return path
1409                // from lower level cahces/memory to the core.
1410                completion_time += clockEdge(responseLatency) +
1411                    pkt->payloadDelay;
1412                if (pkt->isRead() && !is_error) {
1413                    // sanity check
1414                    assert(pkt->getAddr() == tgt_pkt->getAddr());
1415                    assert(pkt->getSize() >= tgt_pkt->getSize());
1416
1417                    tgt_pkt->setData(pkt->getConstPtr<uint8_t>());
1418                }
1419            }
1420            tgt_pkt->makeTimingResponse();
1421            // if this packet is an error copy that to the new packet
1422            if (is_error)
1423                tgt_pkt->copyError(pkt);
1424            if (tgt_pkt->cmd == MemCmd::ReadResp &&
1425                (is_invalidate || mshr->hasPostInvalidate())) {
1426                // If intermediate cache got ReadRespWithInvalidate,
1427                // propagate that.  Response should not have
1428                // isInvalidate() set otherwise.
1429                tgt_pkt->cmd = MemCmd::ReadRespWithInvalidate;
1430                DPRINTF(Cache, "%s: updated cmd to %s\n", __func__,
1431                        tgt_pkt->print());
1432            }
1433            // Reset the bus additional time as it is now accounted for
1434            tgt_pkt->headerDelay = tgt_pkt->payloadDelay = 0;
1435            cpuSidePort->schedTimingResp(tgt_pkt, completion_time, true);
1436            break;
1437
1438          case MSHR::Target::FromPrefetcher:
1439            assert(tgt_pkt->cmd == MemCmd::HardPFReq);
1440            if (blk)
1441                blk->status |= BlkHWPrefetched;
1442            delete tgt_pkt->req;
1443            delete tgt_pkt;
1444            break;
1445
1446          case MSHR::Target::FromSnoop:
1447            // I don't believe that a snoop can be in an error state
1448            assert(!is_error);
1449            // response to snoop request
1450            DPRINTF(Cache, "processing deferred snoop...\n");
1451            // If the response is invalidating, a snooping target can
1452            // be satisfied if it is also invalidating. If the reponse is, not
1453            // only invalidating, but more specifically an InvalidateResp, the
1454            // MSHR was created due to an InvalidateReq and a cache above is
1455            // waiting to satisfy a WriteLineReq. In this case even an
1456            // non-invalidating snoop is added as a target here since this is
1457            // the ordering point. When the InvalidateResp reaches this cache,
1458            // the snooping target will snoop further the cache above with the
1459            // WriteLineReq.
1460            assert(!(is_invalidate &&
1461                     pkt->cmd != MemCmd::InvalidateResp &&
1462                     !mshr->hasPostInvalidate()));
1463            handleSnoop(tgt_pkt, blk, true, true, mshr->hasPostInvalidate());
1464            break;
1465
1466          default:
1467            panic("Illegal target->source enum %d\n", target.source);
1468        }
1469    }
1470
1471    maintainClusivity(from_cache, blk);
1472
1473    if (blk && blk->isValid()) {
1474        // an invalidate response stemming from a write line request
1475        // should not invalidate the block, so check if the
1476        // invalidation should be discarded
1477        if (is_invalidate || mshr->hasPostInvalidate()) {
1478            invalidateBlock(blk);
1479        } else if (mshr->hasPostDowngrade()) {
1480            blk->status &= ~BlkWritable;
1481        }
1482    }
1483
1484    if (mshr->promoteDeferredTargets()) {
1485        // avoid later read getting stale data while write miss is
1486        // outstanding.. see comment in timingAccess()
1487        if (blk) {
1488            blk->status &= ~BlkReadable;
1489        }
1490        mshrQueue.markPending(mshr);
1491        schedMemSideSendEvent(clockEdge() + pkt->payloadDelay);
1492    } else {
1493        mshrQueue.deallocate(mshr);
1494        if (wasFull && !mshrQueue.isFull()) {
1495            clearBlocked(Blocked_NoMSHRs);
1496        }
1497
1498        // Request the bus for a prefetch if this deallocation freed enough
1499        // MSHRs for a prefetch to take place
1500        if (prefetcher && mshrQueue.canPrefetch()) {
1501            Tick next_pf_time = std::max(prefetcher->nextPrefetchReadyTime(),
1502                                         clockEdge());
1503            if (next_pf_time != MaxTick)
1504                schedMemSideSendEvent(next_pf_time);
1505        }
1506    }
1507    // reset the xbar additional timinig  as it is now accounted for
1508    pkt->headerDelay = pkt->payloadDelay = 0;
1509
1510    // copy writebacks to write buffer
1511    doWritebacks(writebacks, forward_time);
1512
1513    // if we used temp block, check to see if its valid and then clear it out
1514    if (blk == tempBlock && tempBlock->isValid()) {
1515        // We use forwardLatency here because we are copying
1516        // Writebacks/CleanEvicts to write buffer. It specifies the latency to
1517        // allocate an internal buffer and to schedule an event to the
1518        // queued port.
1519        if (blk->isDirty() || writebackClean) {
1520            PacketPtr wbPkt = writebackBlk(blk);
1521            allocateWriteBuffer(wbPkt, forward_time);
1522            // Set BLOCK_CACHED flag if cached above.
1523            if (isCachedAbove(wbPkt))
1524                wbPkt->setBlockCached();
1525        } else {
1526            PacketPtr wcPkt = cleanEvictBlk(blk);
1527            // Check to see if block is cached above. If not allocate
1528            // write buffer
1529            if (isCachedAbove(wcPkt))
1530                delete wcPkt;
1531            else
1532                allocateWriteBuffer(wcPkt, forward_time);
1533        }
1534        blk->invalidate();
1535    }
1536
1537    DPRINTF(CacheVerbose, "%s: Leaving with %s\n", __func__, pkt->print());
1538    delete pkt;
1539}
1540
1541PacketPtr
1542Cache::writebackBlk(CacheBlk *blk)
1543{
1544    chatty_assert(!isReadOnly || writebackClean,
1545                  "Writeback from read-only cache");
1546    assert(blk && blk->isValid() && (blk->isDirty() || writebackClean));
1547
1548    writebacks[Request::wbMasterId]++;
1549
1550    Request *req = new Request(tags->regenerateBlkAddr(blk->tag, blk->set),
1551                               blkSize, 0, Request::wbMasterId);
1552    if (blk->isSecure())
1553        req->setFlags(Request::SECURE);
1554
1555    req->taskId(blk->task_id);
1556    blk->task_id= ContextSwitchTaskId::Unknown;
1557    blk->tickInserted = curTick();
1558
1559    PacketPtr pkt =
1560        new Packet(req, blk->isDirty() ?
1561                   MemCmd::WritebackDirty : MemCmd::WritebackClean);
1562
1563    DPRINTF(Cache, "Create Writeback %s writable: %d, dirty: %d\n",
1564            pkt->print(), blk->isWritable(), blk->isDirty());
1565
1566    if (blk->isWritable()) {
1567        // not asserting shared means we pass the block in modified
1568        // state, mark our own block non-writeable
1569        blk->status &= ~BlkWritable;
1570    } else {
1571        // we are in the Owned state, tell the receiver
1572        pkt->setHasSharers();
1573    }
1574
1575    // make sure the block is not marked dirty
1576    blk->status &= ~BlkDirty;
1577
1578    pkt->allocate();
1579    std::memcpy(pkt->getPtr<uint8_t>(), blk->data, blkSize);
1580
1581    return pkt;
1582}
1583
1584PacketPtr
1585Cache::cleanEvictBlk(CacheBlk *blk)
1586{
1587    assert(!writebackClean);
1588    assert(blk && blk->isValid() && !blk->isDirty());
1589    // Creating a zero sized write, a message to the snoop filter
1590    Request *req =
1591        new Request(tags->regenerateBlkAddr(blk->tag, blk->set), blkSize, 0,
1592                    Request::wbMasterId);
1593    if (blk->isSecure())
1594        req->setFlags(Request::SECURE);
1595
1596    req->taskId(blk->task_id);
1597    blk->task_id = ContextSwitchTaskId::Unknown;
1598    blk->tickInserted = curTick();
1599
1600    PacketPtr pkt = new Packet(req, MemCmd::CleanEvict);
1601    pkt->allocate();
1602    DPRINTF(Cache, "Create CleanEvict %s\n", pkt->print());
1603
1604    return pkt;
1605}
1606
1607void
1608Cache::memWriteback()
1609{
1610    CacheBlkVisitorWrapper visitor(*this, &Cache::writebackVisitor);
1611    tags->forEachBlk(visitor);
1612}
1613
1614void
1615Cache::memInvalidate()
1616{
1617    CacheBlkVisitorWrapper visitor(*this, &Cache::invalidateVisitor);
1618    tags->forEachBlk(visitor);
1619}
1620
1621bool
1622Cache::isDirty() const
1623{
1624    CacheBlkIsDirtyVisitor visitor;
1625    tags->forEachBlk(visitor);
1626
1627    return visitor.isDirty();
1628}
1629
1630bool
1631Cache::writebackVisitor(CacheBlk &blk)
1632{
1633    if (blk.isDirty()) {
1634        assert(blk.isValid());
1635
1636        Request request(tags->regenerateBlkAddr(blk.tag, blk.set),
1637                        blkSize, 0, Request::funcMasterId);
1638        request.taskId(blk.task_id);
1639
1640        Packet packet(&request, MemCmd::WriteReq);
1641        packet.dataStatic(blk.data);
1642
1643        memSidePort->sendFunctional(&packet);
1644
1645        blk.status &= ~BlkDirty;
1646    }
1647
1648    return true;
1649}
1650
1651bool
1652Cache::invalidateVisitor(CacheBlk &blk)
1653{
1654
1655    if (blk.isDirty())
1656        warn_once("Invalidating dirty cache lines. Expect things to break.\n");
1657
1658    if (blk.isValid()) {
1659        assert(!blk.isDirty());
1660        tags->invalidate(&blk);
1661        blk.invalidate();
1662    }
1663
1664    return true;
1665}
1666
1667CacheBlk*
1668Cache::allocateBlock(Addr addr, bool is_secure, PacketList &writebacks)
1669{
1670    CacheBlk *blk = tags->findVictim(addr);
1671
1672    // It is valid to return nullptr if there is no victim
1673    if (!blk)
1674        return nullptr;
1675
1676    if (blk->isValid()) {
1677        Addr repl_addr = tags->regenerateBlkAddr(blk->tag, blk->set);
1678        MSHR *repl_mshr = mshrQueue.findMatch(repl_addr, blk->isSecure());
1679        if (repl_mshr) {
1680            // must be an outstanding upgrade request
1681            // on a block we're about to replace...
1682            assert(!blk->isWritable() || blk->isDirty());
1683            assert(repl_mshr->needsWritable());
1684            // too hard to replace block with transient state
1685            // allocation failed, block not inserted
1686            return nullptr;
1687        } else {
1688            DPRINTF(Cache, "replacement: replacing %#llx (%s) with %#llx "
1689                    "(%s): %s\n", repl_addr, blk->isSecure() ? "s" : "ns",
1690                    addr, is_secure ? "s" : "ns",
1691                    blk->isDirty() ? "writeback" : "clean");
1692
1693            if (blk->wasPrefetched()) {
1694                unusedPrefetches++;
1695            }
1696            // Will send up Writeback/CleanEvict snoops via isCachedAbove
1697            // when pushing this writeback list into the write buffer.
1698            if (blk->isDirty() || writebackClean) {
1699                // Save writeback packet for handling by caller
1700                writebacks.push_back(writebackBlk(blk));
1701            } else {
1702                writebacks.push_back(cleanEvictBlk(blk));
1703            }
1704        }
1705    }
1706
1707    return blk;
1708}
1709
1710void
1711Cache::invalidateBlock(CacheBlk *blk)
1712{
1713    if (blk != tempBlock)
1714        tags->invalidate(blk);
1715    blk->invalidate();
1716}
1717
1718// Note that the reason we return a list of writebacks rather than
1719// inserting them directly in the write buffer is that this function
1720// is called by both atomic and timing-mode accesses, and in atomic
1721// mode we don't mess with the write buffer (we just perform the
1722// writebacks atomically once the original request is complete).
1723CacheBlk*
1724Cache::handleFill(PacketPtr pkt, CacheBlk *blk, PacketList &writebacks,
1725                  bool allocate)
1726{
1727    assert(pkt->isResponse() || pkt->cmd == MemCmd::WriteLineReq);
1728    Addr addr = pkt->getAddr();
1729    bool is_secure = pkt->isSecure();
1730#if TRACING_ON
1731    CacheBlk::State old_state = blk ? blk->status : 0;
1732#endif
1733
1734    // When handling a fill, we should have no writes to this line.
1735    assert(addr == blockAlign(addr));
1736    assert(!writeBuffer.findMatch(addr, is_secure));
1737
1738    if (blk == nullptr) {
1739        // better have read new data...
1740        assert(pkt->hasData());
1741
1742        // only read responses and write-line requests have data;
1743        // note that we don't write the data here for write-line - that
1744        // happens in the subsequent call to satisfyRequest
1745        assert(pkt->isRead() || pkt->cmd == MemCmd::WriteLineReq);
1746
1747        // need to do a replacement if allocating, otherwise we stick
1748        // with the temporary storage
1749        blk = allocate ? allocateBlock(addr, is_secure, writebacks) : nullptr;
1750
1751        if (blk == nullptr) {
1752            // No replaceable block or a mostly exclusive
1753            // cache... just use temporary storage to complete the
1754            // current request and then get rid of it
1755            assert(!tempBlock->isValid());
1756            blk = tempBlock;
1757            tempBlock->set = tags->extractSet(addr);
1758            tempBlock->tag = tags->extractTag(addr);
1759            // @todo: set security state as well...
1760            DPRINTF(Cache, "using temp block for %#llx (%s)\n", addr,
1761                    is_secure ? "s" : "ns");
1762        } else {
1763            tags->insertBlock(pkt, blk);
1764        }
1765
1766        // we should never be overwriting a valid block
1767        assert(!blk->isValid());
1768    } else {
1769        // existing block... probably an upgrade
1770        assert(blk->tag == tags->extractTag(addr));
1771        // either we're getting new data or the block should already be valid
1772        assert(pkt->hasData() || blk->isValid());
1773        // don't clear block status... if block is already dirty we
1774        // don't want to lose that
1775    }
1776
1777    if (is_secure)
1778        blk->status |= BlkSecure;
1779    blk->status |= BlkValid | BlkReadable;
1780
1781    // sanity check for whole-line writes, which should always be
1782    // marked as writable as part of the fill, and then later marked
1783    // dirty as part of satisfyRequest
1784    if (pkt->cmd == MemCmd::WriteLineReq) {
1785        assert(!pkt->hasSharers());
1786        // at the moment other caches do not respond to the
1787        // invalidation requests corresponding to a whole-line write
1788        assert(!pkt->cacheResponding());
1789    }
1790
1791    // here we deal with setting the appropriate state of the line,
1792    // and we start by looking at the hasSharers flag, and ignore the
1793    // cacheResponding flag (normally signalling dirty data) if the
1794    // packet has sharers, thus the line is never allocated as Owned
1795    // (dirty but not writable), and always ends up being either
1796    // Shared, Exclusive or Modified, see Packet::setCacheResponding
1797    // for more details
1798    if (!pkt->hasSharers()) {
1799        // we could get a writable line from memory (rather than a
1800        // cache) even in a read-only cache, note that we set this bit
1801        // even for a read-only cache, possibly revisit this decision
1802        blk->status |= BlkWritable;
1803
1804        // check if we got this via cache-to-cache transfer (i.e., from a
1805        // cache that had the block in Modified or Owned state)
1806        if (pkt->cacheResponding()) {
1807            // we got the block in Modified state, and invalidated the
1808            // owners copy
1809            blk->status |= BlkDirty;
1810
1811            chatty_assert(!isReadOnly, "Should never see dirty snoop response "
1812                          "in read-only cache %s\n", name());
1813        }
1814    }
1815
1816    DPRINTF(Cache, "Block addr %#llx (%s) moving from state %x to %s\n",
1817            addr, is_secure ? "s" : "ns", old_state, blk->print());
1818
1819    // if we got new data, copy it in (checking for a read response
1820    // and a response that has data is the same in the end)
1821    if (pkt->isRead()) {
1822        // sanity checks
1823        assert(pkt->hasData());
1824        assert(pkt->getSize() == blkSize);
1825
1826        std::memcpy(blk->data, pkt->getConstPtr<uint8_t>(), blkSize);
1827    }
1828    // We pay for fillLatency here.
1829    blk->whenReady = clockEdge() + fillLatency * clockPeriod() +
1830        pkt->payloadDelay;
1831
1832    return blk;
1833}
1834
1835
1836/////////////////////////////////////////////////////
1837//
1838// Snoop path: requests coming in from the memory side
1839//
1840/////////////////////////////////////////////////////
1841
1842void
1843Cache::doTimingSupplyResponse(PacketPtr req_pkt, const uint8_t *blk_data,
1844                              bool already_copied, bool pending_inval)
1845{
1846    // sanity check
1847    assert(req_pkt->isRequest());
1848    assert(req_pkt->needsResponse());
1849
1850    DPRINTF(Cache, "%s: for %s\n", __func__, req_pkt->print());
1851    // timing-mode snoop responses require a new packet, unless we
1852    // already made a copy...
1853    PacketPtr pkt = req_pkt;
1854    if (!already_copied)
1855        // do not clear flags, and allocate space for data if the
1856        // packet needs it (the only packets that carry data are read
1857        // responses)
1858        pkt = new Packet(req_pkt, false, req_pkt->isRead());
1859
1860    assert(req_pkt->req->isUncacheable() || req_pkt->isInvalidate() ||
1861           pkt->hasSharers());
1862    pkt->makeTimingResponse();
1863    if (pkt->isRead()) {
1864        pkt->setDataFromBlock(blk_data, blkSize);
1865    }
1866    if (pkt->cmd == MemCmd::ReadResp && pending_inval) {
1867        // Assume we defer a response to a read from a far-away cache
1868        // A, then later defer a ReadExcl from a cache B on the same
1869        // bus as us. We'll assert cacheResponding in both cases, but
1870        // in the latter case cacheResponding will keep the
1871        // invalidation from reaching cache A. This special response
1872        // tells cache A that it gets the block to satisfy its read,
1873        // but must immediately invalidate it.
1874        pkt->cmd = MemCmd::ReadRespWithInvalidate;
1875    }
1876    // Here we consider forward_time, paying for just forward latency and
1877    // also charging the delay provided by the xbar.
1878    // forward_time is used as send_time in next allocateWriteBuffer().
1879    Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
1880    // Here we reset the timing of the packet.
1881    pkt->headerDelay = pkt->payloadDelay = 0;
1882    DPRINTF(CacheVerbose, "%s: created response: %s tick: %lu\n", __func__,
1883            pkt->print(), forward_time);
1884    memSidePort->schedTimingSnoopResp(pkt, forward_time, true);
1885}
1886
1887uint32_t
1888Cache::handleSnoop(PacketPtr pkt, CacheBlk *blk, bool is_timing,
1889                   bool is_deferred, bool pending_inval)
1890{
1891    DPRINTF(CacheVerbose, "%s: for %s\n", __func__, pkt->print());
1892    // deferred snoops can only happen in timing mode
1893    assert(!(is_deferred && !is_timing));
1894    // pending_inval only makes sense on deferred snoops
1895    assert(!(pending_inval && !is_deferred));
1896    assert(pkt->isRequest());
1897
1898    // the packet may get modified if we or a forwarded snooper
1899    // responds in atomic mode, so remember a few things about the
1900    // original packet up front
1901    bool invalidate = pkt->isInvalidate();
1902    bool M5_VAR_USED needs_writable = pkt->needsWritable();
1903
1904    // at the moment we could get an uncacheable write which does not
1905    // have the invalidate flag, and we need a suitable way of dealing
1906    // with this case
1907    panic_if(invalidate && pkt->req->isUncacheable(),
1908             "%s got an invalidating uncacheable snoop request %s",
1909             name(), pkt->print());
1910
1911    uint32_t snoop_delay = 0;
1912
1913    if (forwardSnoops) {
1914        // first propagate snoop upward to see if anyone above us wants to
1915        // handle it.  save & restore packet src since it will get
1916        // rewritten to be relative to cpu-side bus (if any)
1917        bool alreadyResponded = pkt->cacheResponding();
1918        if (is_timing) {
1919            // copy the packet so that we can clear any flags before
1920            // forwarding it upwards, we also allocate data (passing
1921            // the pointer along in case of static data), in case
1922            // there is a snoop hit in upper levels
1923            Packet snoopPkt(pkt, true, true);
1924            snoopPkt.setExpressSnoop();
1925            // the snoop packet does not need to wait any additional
1926            // time
1927            snoopPkt.headerDelay = snoopPkt.payloadDelay = 0;
1928            cpuSidePort->sendTimingSnoopReq(&snoopPkt);
1929
1930            // add the header delay (including crossbar and snoop
1931            // delays) of the upward snoop to the snoop delay for this
1932            // cache
1933            snoop_delay += snoopPkt.headerDelay;
1934
1935            if (snoopPkt.cacheResponding()) {
1936                // cache-to-cache response from some upper cache
1937                assert(!alreadyResponded);
1938                pkt->setCacheResponding();
1939            }
1940            // upstream cache has the block, or has an outstanding
1941            // MSHR, pass the flag on
1942            if (snoopPkt.hasSharers()) {
1943                pkt->setHasSharers();
1944            }
1945            // If this request is a prefetch or clean evict and an upper level
1946            // signals block present, make sure to propagate the block
1947            // presence to the requester.
1948            if (snoopPkt.isBlockCached()) {
1949                pkt->setBlockCached();
1950            }
1951        } else {
1952            cpuSidePort->sendAtomicSnoop(pkt);
1953            if (!alreadyResponded && pkt->cacheResponding()) {
1954                // cache-to-cache response from some upper cache:
1955                // forward response to original requester
1956                assert(pkt->isResponse());
1957            }
1958        }
1959    }
1960
1961    if (!blk || !blk->isValid()) {
1962        DPRINTF(CacheVerbose, "%s: snoop miss for %s\n", __func__,
1963                pkt->print());
1964        if (is_deferred) {
1965            // we no longer have the block, and will not respond, but a
1966            // packet was allocated in MSHR::handleSnoop and we have
1967            // to delete it
1968            assert(pkt->needsResponse());
1969
1970            // we have passed the block to a cache upstream, that
1971            // cache should be responding
1972            assert(pkt->cacheResponding());
1973
1974            delete pkt;
1975        }
1976        return snoop_delay;
1977    } else {
1978        DPRINTF(Cache, "%s: snoop hit for %s, old state is %s\n", __func__,
1979                pkt->print(), blk->print());
1980    }
1981
1982    chatty_assert(!(isReadOnly && blk->isDirty()),
1983                  "Should never have a dirty block in a read-only cache %s\n",
1984                  name());
1985
1986    // We may end up modifying both the block state and the packet (if
1987    // we respond in atomic mode), so just figure out what to do now
1988    // and then do it later. If we find dirty data while snooping for
1989    // an invalidate, we don't need to send a response. The
1990    // invalidation itself is taken care of below.
1991    bool respond = blk->isDirty() && pkt->needsResponse() &&
1992        pkt->cmd != MemCmd::InvalidateReq;
1993    bool have_writable = blk->isWritable();
1994
1995    // Invalidate any prefetch's from below that would strip write permissions
1996    // MemCmd::HardPFReq is only observed by upstream caches.  After missing
1997    // above and in it's own cache, a new MemCmd::ReadReq is created that
1998    // downstream caches observe.
1999    if (pkt->mustCheckAbove()) {
2000        DPRINTF(Cache, "Found addr %#llx in upper level cache for snoop %s "
2001                "from lower cache\n", pkt->getAddr(), pkt->print());
2002        pkt->setBlockCached();
2003        return snoop_delay;
2004    }
2005
2006    if (pkt->isRead() && !invalidate) {
2007        // reading without requiring the line in a writable state
2008        assert(!needs_writable);
2009        pkt->setHasSharers();
2010
2011        // if the requesting packet is uncacheable, retain the line in
2012        // the current state, otherwhise unset the writable flag,
2013        // which means we go from Modified to Owned (and will respond
2014        // below), remain in Owned (and will respond below), from
2015        // Exclusive to Shared, or remain in Shared
2016        if (!pkt->req->isUncacheable())
2017            blk->status &= ~BlkWritable;
2018    }
2019
2020    if (respond) {
2021        // prevent anyone else from responding, cache as well as
2022        // memory, and also prevent any memory from even seeing the
2023        // request
2024        pkt->setCacheResponding();
2025        if (have_writable) {
2026            // inform the cache hierarchy that this cache had the line
2027            // in the Modified state so that we avoid unnecessary
2028            // invalidations (see Packet::setResponderHadWritable)
2029            pkt->setResponderHadWritable();
2030
2031            // in the case of an uncacheable request there is no point
2032            // in setting the responderHadWritable flag, but since the
2033            // recipient does not care there is no harm in doing so
2034        } else {
2035            // if the packet has needsWritable set we invalidate our
2036            // copy below and all other copies will be invalidates
2037            // through express snoops, and if needsWritable is not set
2038            // we already called setHasSharers above
2039        }
2040
2041        // if we are returning a writable and dirty (Modified) line,
2042        // we should be invalidating the line
2043        panic_if(!invalidate && !pkt->hasSharers(),
2044                 "%s is passing a Modified line through %s, "
2045                 "but keeping the block", name(), pkt->print());
2046
2047        if (is_timing) {
2048            doTimingSupplyResponse(pkt, blk->data, is_deferred, pending_inval);
2049        } else {
2050            pkt->makeAtomicResponse();
2051            // packets such as upgrades do not actually have any data
2052            // payload
2053            if (pkt->hasData())
2054                pkt->setDataFromBlock(blk->data, blkSize);
2055        }
2056    }
2057
2058    if (!respond && is_deferred) {
2059        assert(pkt->needsResponse());
2060
2061        // if we copied the deferred packet with the intention to
2062        // respond, but are not responding, then a cache above us must
2063        // be, and we can use this as the indication of whether this
2064        // is a packet where we created a copy of the request or not
2065        if (!pkt->cacheResponding()) {
2066            delete pkt->req;
2067        }
2068
2069        delete pkt;
2070    }
2071
2072    // Do this last in case it deallocates block data or something
2073    // like that
2074    if (invalidate) {
2075        invalidateBlock(blk);
2076    }
2077
2078    DPRINTF(Cache, "new state is %s\n", blk->print());
2079
2080    return snoop_delay;
2081}
2082
2083
2084void
2085Cache::recvTimingSnoopReq(PacketPtr pkt)
2086{
2087    DPRINTF(CacheVerbose, "%s: for %s\n", __func__, pkt->print());
2088
2089    // Snoops shouldn't happen when bypassing caches
2090    assert(!system->bypassCaches());
2091
2092    // no need to snoop requests that are not in range
2093    if (!inRange(pkt->getAddr())) {
2094        return;
2095    }
2096
2097    bool is_secure = pkt->isSecure();
2098    CacheBlk *blk = tags->findBlock(pkt->getAddr(), is_secure);
2099
2100    Addr blk_addr = blockAlign(pkt->getAddr());
2101    MSHR *mshr = mshrQueue.findMatch(blk_addr, is_secure);
2102
2103    // Update the latency cost of the snoop so that the crossbar can
2104    // account for it. Do not overwrite what other neighbouring caches
2105    // have already done, rather take the maximum. The update is
2106    // tentative, for cases where we return before an upward snoop
2107    // happens below.
2108    pkt->snoopDelay = std::max<uint32_t>(pkt->snoopDelay,
2109                                         lookupLatency * clockPeriod());
2110
2111    // Inform request(Prefetch, CleanEvict or Writeback) from below of
2112    // MSHR hit, set setBlockCached.
2113    if (mshr && pkt->mustCheckAbove()) {
2114        DPRINTF(Cache, "Setting block cached for %s from lower cache on "
2115                "mshr hit\n", pkt->print());
2116        pkt->setBlockCached();
2117        return;
2118    }
2119
2120    // Let the MSHR itself track the snoop and decide whether we want
2121    // to go ahead and do the regular cache snoop
2122    if (mshr && mshr->handleSnoop(pkt, order++)) {
2123        DPRINTF(Cache, "Deferring snoop on in-service MSHR to blk %#llx (%s)."
2124                "mshrs: %s\n", blk_addr, is_secure ? "s" : "ns",
2125                mshr->print());
2126
2127        if (mshr->getNumTargets() > numTarget)
2128            warn("allocating bonus target for snoop"); //handle later
2129        return;
2130    }
2131
2132    //We also need to check the writeback buffers and handle those
2133    WriteQueueEntry *wb_entry = writeBuffer.findMatch(blk_addr, is_secure);
2134    if (wb_entry) {
2135        DPRINTF(Cache, "Snoop hit in writeback to addr %#llx (%s)\n",
2136                pkt->getAddr(), is_secure ? "s" : "ns");
2137        // Expect to see only Writebacks and/or CleanEvicts here, both of
2138        // which should not be generated for uncacheable data.
2139        assert(!wb_entry->isUncacheable());
2140        // There should only be a single request responsible for generating
2141        // Writebacks/CleanEvicts.
2142        assert(wb_entry->getNumTargets() == 1);
2143        PacketPtr wb_pkt = wb_entry->getTarget()->pkt;
2144        assert(wb_pkt->isEviction());
2145
2146        if (pkt->isEviction()) {
2147            // if the block is found in the write queue, set the BLOCK_CACHED
2148            // flag for Writeback/CleanEvict snoop. On return the snoop will
2149            // propagate the BLOCK_CACHED flag in Writeback packets and prevent
2150            // any CleanEvicts from travelling down the memory hierarchy.
2151            pkt->setBlockCached();
2152            DPRINTF(Cache, "%s: Squashing %s from lower cache on writequeue "
2153                    "hit\n", __func__, pkt->print());
2154            return;
2155        }
2156
2157        // conceptually writebacks are no different to other blocks in
2158        // this cache, so the behaviour is modelled after handleSnoop,
2159        // the difference being that instead of querying the block
2160        // state to determine if it is dirty and writable, we use the
2161        // command and fields of the writeback packet
2162        bool respond = wb_pkt->cmd == MemCmd::WritebackDirty &&
2163            pkt->needsResponse() && pkt->cmd != MemCmd::InvalidateReq;
2164        bool have_writable = !wb_pkt->hasSharers();
2165        bool invalidate = pkt->isInvalidate();
2166
2167        if (!pkt->req->isUncacheable() && pkt->isRead() && !invalidate) {
2168            assert(!pkt->needsWritable());
2169            pkt->setHasSharers();
2170            wb_pkt->setHasSharers();
2171        }
2172
2173        if (respond) {
2174            pkt->setCacheResponding();
2175
2176            if (have_writable) {
2177                pkt->setResponderHadWritable();
2178            }
2179
2180            doTimingSupplyResponse(pkt, wb_pkt->getConstPtr<uint8_t>(),
2181                                   false, false);
2182        }
2183
2184        if (invalidate) {
2185            // Invalidation trumps our writeback... discard here
2186            // Note: markInService will remove entry from writeback buffer.
2187            markInService(wb_entry);
2188            delete wb_pkt;
2189        }
2190    }
2191
2192    // If this was a shared writeback, there may still be
2193    // other shared copies above that require invalidation.
2194    // We could be more selective and return here if the
2195    // request is non-exclusive or if the writeback is
2196    // exclusive.
2197    uint32_t snoop_delay = handleSnoop(pkt, blk, true, false, false);
2198
2199    // Override what we did when we first saw the snoop, as we now
2200    // also have the cost of the upwards snoops to account for
2201    pkt->snoopDelay = std::max<uint32_t>(pkt->snoopDelay, snoop_delay +
2202                                         lookupLatency * clockPeriod());
2203}
2204
2205bool
2206Cache::CpuSidePort::recvTimingSnoopResp(PacketPtr pkt)
2207{
2208    // Express snoop responses from master to slave, e.g., from L1 to L2
2209    cache->recvTimingSnoopResp(pkt);
2210    return true;
2211}
2212
2213Tick
2214Cache::recvAtomicSnoop(PacketPtr pkt)
2215{
2216    // Snoops shouldn't happen when bypassing caches
2217    assert(!system->bypassCaches());
2218
2219    // no need to snoop requests that are not in range.
2220    if (!inRange(pkt->getAddr())) {
2221        return 0;
2222    }
2223
2224    CacheBlk *blk = tags->findBlock(pkt->getAddr(), pkt->isSecure());
2225    uint32_t snoop_delay = handleSnoop(pkt, blk, false, false, false);
2226    return snoop_delay + lookupLatency * clockPeriod();
2227}
2228
2229
2230QueueEntry*
2231Cache::getNextQueueEntry()
2232{
2233    // Check both MSHR queue and write buffer for potential requests,
2234    // note that null does not mean there is no request, it could
2235    // simply be that it is not ready
2236    MSHR *miss_mshr  = mshrQueue.getNext();
2237    WriteQueueEntry *wq_entry = writeBuffer.getNext();
2238
2239    // If we got a write buffer request ready, first priority is a
2240    // full write buffer, otherwise we favour the miss requests
2241    if (wq_entry && (writeBuffer.isFull() || !miss_mshr)) {
2242        // need to search MSHR queue for conflicting earlier miss.
2243        MSHR *conflict_mshr =
2244            mshrQueue.findPending(wq_entry->blkAddr,
2245                                  wq_entry->isSecure);
2246
2247        if (conflict_mshr && conflict_mshr->order < wq_entry->order) {
2248            // Service misses in order until conflict is cleared.
2249            return conflict_mshr;
2250
2251            // @todo Note that we ignore the ready time of the conflict here
2252        }
2253
2254        // No conflicts; issue write
2255        return wq_entry;
2256    } else if (miss_mshr) {
2257        // need to check for conflicting earlier writeback
2258        WriteQueueEntry *conflict_mshr =
2259            writeBuffer.findPending(miss_mshr->blkAddr,
2260                                    miss_mshr->isSecure);
2261        if (conflict_mshr) {
2262            // not sure why we don't check order here... it was in the
2263            // original code but commented out.
2264
2265            // The only way this happens is if we are
2266            // doing a write and we didn't have permissions
2267            // then subsequently saw a writeback (owned got evicted)
2268            // We need to make sure to perform the writeback first
2269            // To preserve the dirty data, then we can issue the write
2270
2271            // should we return wq_entry here instead?  I.e. do we
2272            // have to flush writes in order?  I don't think so... not
2273            // for Alpha anyway.  Maybe for x86?
2274            return conflict_mshr;
2275
2276            // @todo Note that we ignore the ready time of the conflict here
2277        }
2278
2279        // No conflicts; issue read
2280        return miss_mshr;
2281    }
2282
2283    // fall through... no pending requests.  Try a prefetch.
2284    assert(!miss_mshr && !wq_entry);
2285    if (prefetcher && mshrQueue.canPrefetch()) {
2286        // If we have a miss queue slot, we can try a prefetch
2287        PacketPtr pkt = prefetcher->getPacket();
2288        if (pkt) {
2289            Addr pf_addr = blockAlign(pkt->getAddr());
2290            if (!tags->findBlock(pf_addr, pkt->isSecure()) &&
2291                !mshrQueue.findMatch(pf_addr, pkt->isSecure()) &&
2292                !writeBuffer.findMatch(pf_addr, pkt->isSecure())) {
2293                // Update statistic on number of prefetches issued
2294                // (hwpf_mshr_misses)
2295                assert(pkt->req->masterId() < system->maxMasters());
2296                mshr_misses[pkt->cmdToIndex()][pkt->req->masterId()]++;
2297
2298                // allocate an MSHR and return it, note
2299                // that we send the packet straight away, so do not
2300                // schedule the send
2301                return allocateMissBuffer(pkt, curTick(), false);
2302            } else {
2303                // free the request and packet
2304                delete pkt->req;
2305                delete pkt;
2306            }
2307        }
2308    }
2309
2310    return nullptr;
2311}
2312
2313bool
2314Cache::isCachedAbove(PacketPtr pkt, bool is_timing) const
2315{
2316    if (!forwardSnoops)
2317        return false;
2318    // Mirroring the flow of HardPFReqs, the cache sends CleanEvict and
2319    // Writeback snoops into upper level caches to check for copies of the
2320    // same block. Using the BLOCK_CACHED flag with the Writeback/CleanEvict
2321    // packet, the cache can inform the crossbar below of presence or absence
2322    // of the block.
2323    if (is_timing) {
2324        Packet snoop_pkt(pkt, true, false);
2325        snoop_pkt.setExpressSnoop();
2326        // Assert that packet is either Writeback or CleanEvict and not a
2327        // prefetch request because prefetch requests need an MSHR and may
2328        // generate a snoop response.
2329        assert(pkt->isEviction());
2330        snoop_pkt.senderState = nullptr;
2331        cpuSidePort->sendTimingSnoopReq(&snoop_pkt);
2332        // Writeback/CleanEvict snoops do not generate a snoop response.
2333        assert(!(snoop_pkt.cacheResponding()));
2334        return snoop_pkt.isBlockCached();
2335    } else {
2336        cpuSidePort->sendAtomicSnoop(pkt);
2337        return pkt->isBlockCached();
2338    }
2339}
2340
2341Tick
2342Cache::nextQueueReadyTime() const
2343{
2344    Tick nextReady = std::min(mshrQueue.nextReadyTime(),
2345                              writeBuffer.nextReadyTime());
2346
2347    // Don't signal prefetch ready time if no MSHRs available
2348    // Will signal once enoguh MSHRs are deallocated
2349    if (prefetcher && mshrQueue.canPrefetch()) {
2350        nextReady = std::min(nextReady,
2351                             prefetcher->nextPrefetchReadyTime());
2352    }
2353
2354    return nextReady;
2355}
2356
2357bool
2358Cache::sendMSHRQueuePacket(MSHR* mshr)
2359{
2360    assert(mshr);
2361
2362    // use request from 1st target
2363    PacketPtr tgt_pkt = mshr->getTarget()->pkt;
2364
2365    DPRINTF(Cache, "%s: MSHR %s\n", __func__, tgt_pkt->print());
2366
2367    CacheBlk *blk = tags->findBlock(mshr->blkAddr, mshr->isSecure);
2368
2369    if (tgt_pkt->cmd == MemCmd::HardPFReq && forwardSnoops) {
2370        // we should never have hardware prefetches to allocated
2371        // blocks
2372        assert(blk == nullptr);
2373
2374        // We need to check the caches above us to verify that
2375        // they don't have a copy of this block in the dirty state
2376        // at the moment. Without this check we could get a stale
2377        // copy from memory that might get used in place of the
2378        // dirty one.
2379        Packet snoop_pkt(tgt_pkt, true, false);
2380        snoop_pkt.setExpressSnoop();
2381        // We are sending this packet upwards, but if it hits we will
2382        // get a snoop response that we end up treating just like a
2383        // normal response, hence it needs the MSHR as its sender
2384        // state
2385        snoop_pkt.senderState = mshr;
2386        cpuSidePort->sendTimingSnoopReq(&snoop_pkt);
2387
2388        // Check to see if the prefetch was squashed by an upper cache (to
2389        // prevent us from grabbing the line) or if a Check to see if a
2390        // writeback arrived between the time the prefetch was placed in
2391        // the MSHRs and when it was selected to be sent or if the
2392        // prefetch was squashed by an upper cache.
2393
2394        // It is important to check cacheResponding before
2395        // prefetchSquashed. If another cache has committed to
2396        // responding, it will be sending a dirty response which will
2397        // arrive at the MSHR allocated for this request. Checking the
2398        // prefetchSquash first may result in the MSHR being
2399        // prematurely deallocated.
2400        if (snoop_pkt.cacheResponding()) {
2401            auto M5_VAR_USED r = outstandingSnoop.insert(snoop_pkt.req);
2402            assert(r.second);
2403
2404            // if we are getting a snoop response with no sharers it
2405            // will be allocated as Modified
2406            bool pending_modified_resp = !snoop_pkt.hasSharers();
2407            markInService(mshr, pending_modified_resp);
2408
2409            DPRINTF(Cache, "Upward snoop of prefetch for addr"
2410                    " %#x (%s) hit\n",
2411                    tgt_pkt->getAddr(), tgt_pkt->isSecure()? "s": "ns");
2412            return false;
2413        }
2414
2415        if (snoop_pkt.isBlockCached()) {
2416            DPRINTF(Cache, "Block present, prefetch squashed by cache.  "
2417                    "Deallocating mshr target %#x.\n",
2418                    mshr->blkAddr);
2419
2420            // Deallocate the mshr target
2421            if (mshrQueue.forceDeallocateTarget(mshr)) {
2422                // Clear block if this deallocation resulted freed an
2423                // mshr when all had previously been utilized
2424                clearBlocked(Blocked_NoMSHRs);
2425            }
2426            return false;
2427        }
2428    }
2429
2430    // either a prefetch that is not present upstream, or a normal
2431    // MSHR request, proceed to get the packet to send downstream
2432    PacketPtr pkt = createMissPacket(tgt_pkt, blk, mshr->needsWritable());
2433
2434    mshr->isForward = (pkt == nullptr);
2435
2436    if (mshr->isForward) {
2437        // not a cache block request, but a response is expected
2438        // make copy of current packet to forward, keep current
2439        // copy for response handling
2440        pkt = new Packet(tgt_pkt, false, true);
2441        assert(!pkt->isWrite());
2442    }
2443
2444    // play it safe and append (rather than set) the sender state,
2445    // as forwarded packets may already have existing state
2446    pkt->pushSenderState(mshr);
2447
2448    if (!memSidePort->sendTimingReq(pkt)) {
2449        // we are awaiting a retry, but we
2450        // delete the packet and will be creating a new packet
2451        // when we get the opportunity
2452        delete pkt;
2453
2454        // note that we have now masked any requestBus and
2455        // schedSendEvent (we will wait for a retry before
2456        // doing anything), and this is so even if we do not
2457        // care about this packet and might override it before
2458        // it gets retried
2459        return true;
2460    } else {
2461        // As part of the call to sendTimingReq the packet is
2462        // forwarded to all neighbouring caches (and any caches
2463        // above them) as a snoop. Thus at this point we know if
2464        // any of the neighbouring caches are responding, and if
2465        // so, we know it is dirty, and we can determine if it is
2466        // being passed as Modified, making our MSHR the ordering
2467        // point
2468        bool pending_modified_resp = !pkt->hasSharers() &&
2469            pkt->cacheResponding();
2470        markInService(mshr, pending_modified_resp);
2471        return false;
2472    }
2473}
2474
2475bool
2476Cache::sendWriteQueuePacket(WriteQueueEntry* wq_entry)
2477{
2478    assert(wq_entry);
2479
2480    // always a single target for write queue entries
2481    PacketPtr tgt_pkt = wq_entry->getTarget()->pkt;
2482
2483    DPRINTF(Cache, "%s: write %s\n", __func__, tgt_pkt->print());
2484
2485    // forward as is, both for evictions and uncacheable writes
2486    if (!memSidePort->sendTimingReq(tgt_pkt)) {
2487        // note that we have now masked any requestBus and
2488        // schedSendEvent (we will wait for a retry before
2489        // doing anything), and this is so even if we do not
2490        // care about this packet and might override it before
2491        // it gets retried
2492        return true;
2493    } else {
2494        markInService(wq_entry);
2495        return false;
2496    }
2497}
2498
2499void
2500Cache::serialize(CheckpointOut &cp) const
2501{
2502    bool dirty(isDirty());
2503
2504    if (dirty) {
2505        warn("*** The cache still contains dirty data. ***\n");
2506        warn("    Make sure to drain the system using the correct flags.\n");
2507        warn("    This checkpoint will not restore correctly and dirty data "
2508             "    in the cache will be lost!\n");
2509    }
2510
2511    // Since we don't checkpoint the data in the cache, any dirty data
2512    // will be lost when restoring from a checkpoint of a system that
2513    // wasn't drained properly. Flag the checkpoint as invalid if the
2514    // cache contains dirty data.
2515    bool bad_checkpoint(dirty);
2516    SERIALIZE_SCALAR(bad_checkpoint);
2517}
2518
2519void
2520Cache::unserialize(CheckpointIn &cp)
2521{
2522    bool bad_checkpoint;
2523    UNSERIALIZE_SCALAR(bad_checkpoint);
2524    if (bad_checkpoint) {
2525        fatal("Restoring from checkpoints with dirty caches is not supported "
2526              "in the classic memory system. Please remove any caches or "
2527              " drain them properly before taking checkpoints.\n");
2528    }
2529}
2530
2531///////////////
2532//
2533// CpuSidePort
2534//
2535///////////////
2536
2537AddrRangeList
2538Cache::CpuSidePort::getAddrRanges() const
2539{
2540    return cache->getAddrRanges();
2541}
2542
2543bool
2544Cache::CpuSidePort::recvTimingReq(PacketPtr pkt)
2545{
2546    assert(!cache->system->bypassCaches());
2547
2548    bool success = false;
2549
2550    // always let express snoop packets through if even if blocked
2551    if (pkt->isExpressSnoop()) {
2552        // do not change the current retry state
2553        bool M5_VAR_USED bypass_success = cache->recvTimingReq(pkt);
2554        assert(bypass_success);
2555        return true;
2556    } else if (blocked || mustSendRetry) {
2557        // either already committed to send a retry, or blocked
2558        success = false;
2559    } else {
2560        // pass it on to the cache, and let the cache decide if we
2561        // have to retry or not
2562        success = cache->recvTimingReq(pkt);
2563    }
2564
2565    // remember if we have to retry
2566    mustSendRetry = !success;
2567    return success;
2568}
2569
2570Tick
2571Cache::CpuSidePort::recvAtomic(PacketPtr pkt)
2572{
2573    return cache->recvAtomic(pkt);
2574}
2575
2576void
2577Cache::CpuSidePort::recvFunctional(PacketPtr pkt)
2578{
2579    // functional request
2580    cache->functionalAccess(pkt, true);
2581}
2582
2583Cache::
2584CpuSidePort::CpuSidePort(const std::string &_name, Cache *_cache,
2585                         const std::string &_label)
2586    : BaseCache::CacheSlavePort(_name, _cache, _label), cache(_cache)
2587{
2588}
2589
2590Cache*
2591CacheParams::create()
2592{
2593    assert(tags);
2594
2595    return new Cache(this);
2596}
2597///////////////
2598//
2599// MemSidePort
2600//
2601///////////////
2602
2603bool
2604Cache::MemSidePort::recvTimingResp(PacketPtr pkt)
2605{
2606    cache->recvTimingResp(pkt);
2607    return true;
2608}
2609
2610// Express snooping requests to memside port
2611void
2612Cache::MemSidePort::recvTimingSnoopReq(PacketPtr pkt)
2613{
2614    // handle snooping requests
2615    cache->recvTimingSnoopReq(pkt);
2616}
2617
2618Tick
2619Cache::MemSidePort::recvAtomicSnoop(PacketPtr pkt)
2620{
2621    return cache->recvAtomicSnoop(pkt);
2622}
2623
2624void
2625Cache::MemSidePort::recvFunctionalSnoop(PacketPtr pkt)
2626{
2627    // functional snoop (note that in contrast to atomic we don't have
2628    // a specific functionalSnoop method, as they have the same
2629    // behaviour regardless)
2630    cache->functionalAccess(pkt, false);
2631}
2632
2633void
2634Cache::CacheReqPacketQueue::sendDeferredPacket()
2635{
2636    // sanity check
2637    assert(!waitingOnRetry);
2638
2639    // there should never be any deferred request packets in the
2640    // queue, instead we resly on the cache to provide the packets
2641    // from the MSHR queue or write queue
2642    assert(deferredPacketReadyTime() == MaxTick);
2643
2644    // check for request packets (requests & writebacks)
2645    QueueEntry* entry = cache.getNextQueueEntry();
2646
2647    if (!entry) {
2648        // can happen if e.g. we attempt a writeback and fail, but
2649        // before the retry, the writeback is eliminated because
2650        // we snoop another cache's ReadEx.
2651    } else {
2652        // let our snoop responses go first if there are responses to
2653        // the same addresses
2654        if (checkConflictingSnoop(entry->blkAddr)) {
2655            return;
2656        }
2657        waitingOnRetry = entry->sendPacket(cache);
2658    }
2659
2660    // if we succeeded and are not waiting for a retry, schedule the
2661    // next send considering when the next queue is ready, note that
2662    // snoop responses have their own packet queue and thus schedule
2663    // their own events
2664    if (!waitingOnRetry) {
2665        schedSendEvent(cache.nextQueueReadyTime());
2666    }
2667}
2668
2669Cache::
2670MemSidePort::MemSidePort(const std::string &_name, Cache *_cache,
2671                         const std::string &_label)
2672    : BaseCache::CacheMasterPort(_name, _cache, _reqQueue, _snoopRespQueue),
2673      _reqQueue(*_cache, *this, _snoopRespQueue, _label),
2674      _snoopRespQueue(*_cache, *this, _label), cache(_cache)
2675{
2676}
2677