cache.cc revision 13564:9bbd53a77887
112855Sgabeblack@google.com/*
212855Sgabeblack@google.com * Copyright (c) 2010-2018 ARM Limited
312855Sgabeblack@google.com * All rights reserved.
412855Sgabeblack@google.com *
512855Sgabeblack@google.com * The license below extends only to copyright in the software and shall
612855Sgabeblack@google.com * not be construed as granting a license to any other intellectual
712855Sgabeblack@google.com * property including but not limited to intellectual property relating
812855Sgabeblack@google.com * to a hardware implementation of the functionality of the software
912855Sgabeblack@google.com * licensed hereunder.  You may use the software subject to the license
1012855Sgabeblack@google.com * terms below provided that you ensure that this notice is replicated
1112855Sgabeblack@google.com * unmodified and in its entirety in all distributions of the software,
1212855Sgabeblack@google.com * modified or unmodified, in source code or in binary form.
1312855Sgabeblack@google.com *
1412855Sgabeblack@google.com * Copyright (c) 2002-2005 The Regents of The University of Michigan
1512855Sgabeblack@google.com * Copyright (c) 2010,2015 Advanced Micro Devices, Inc.
1612855Sgabeblack@google.com * All rights reserved.
1712855Sgabeblack@google.com *
1812855Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
1912855Sgabeblack@google.com * modification, are permitted provided that the following conditions are
2012855Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
2112855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
2212855Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
2312855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
2412855Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
2512855Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
2612855Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
2712855Sgabeblack@google.com * this software without specific prior written permission.
2812855Sgabeblack@google.com *
2912855Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3012855Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3112855Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3212855Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3312855Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3412855Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3512855Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3612855Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3712855Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3812855Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3912855Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4012855Sgabeblack@google.com *
4112855Sgabeblack@google.com * Authors: Erik Hallnor
4212855Sgabeblack@google.com *          Dave Greene
4312855Sgabeblack@google.com *          Nathan Binkert
4412855Sgabeblack@google.com *          Steve Reinhardt
4512855Sgabeblack@google.com *          Ron Dreslinski
4612855Sgabeblack@google.com *          Andreas Sandberg
4712855Sgabeblack@google.com *          Nikos Nikoleris
4812855Sgabeblack@google.com */
4912855Sgabeblack@google.com
5012855Sgabeblack@google.com/**
5112855Sgabeblack@google.com * @file
5212855Sgabeblack@google.com * Cache definitions.
5312855Sgabeblack@google.com */
5412855Sgabeblack@google.com
5512855Sgabeblack@google.com#include "mem/cache/cache.hh"
5612855Sgabeblack@google.com
5712855Sgabeblack@google.com#include <cassert>
5812855Sgabeblack@google.com
5912855Sgabeblack@google.com#include "base/compiler.hh"
6012855Sgabeblack@google.com#include "base/logging.hh"
6112855Sgabeblack@google.com#include "base/trace.hh"
6212855Sgabeblack@google.com#include "base/types.hh"
6312855Sgabeblack@google.com#include "debug/Cache.hh"
6412855Sgabeblack@google.com#include "debug/CacheTags.hh"
6512855Sgabeblack@google.com#include "debug/CacheVerbose.hh"
6612855Sgabeblack@google.com#include "enums/Clusivity.hh"
6712855Sgabeblack@google.com#include "mem/cache/cache_blk.hh"
6812855Sgabeblack@google.com#include "mem/cache/mshr.hh"
6912855Sgabeblack@google.com#include "mem/cache/tags/base.hh"
7012855Sgabeblack@google.com#include "mem/cache/write_queue_entry.hh"
7112855Sgabeblack@google.com#include "mem/request.hh"
7212855Sgabeblack@google.com#include "params/Cache.hh"
7312855Sgabeblack@google.com
7412855Sgabeblack@google.comCache::Cache(const CacheParams *p)
7512855Sgabeblack@google.com    : BaseCache(p, p->system->cacheLineSize()),
7612855Sgabeblack@google.com      doFastWrites(true)
7712855Sgabeblack@google.com{
7812855Sgabeblack@google.com}
7912855Sgabeblack@google.com
8012855Sgabeblack@google.comvoid
8112855Sgabeblack@google.comCache::satisfyRequest(PacketPtr pkt, CacheBlk *blk,
8212855Sgabeblack@google.com                      bool deferred_response, bool pending_downgrade)
8312855Sgabeblack@google.com{
8412855Sgabeblack@google.com    BaseCache::satisfyRequest(pkt, blk);
8512855Sgabeblack@google.com
8612855Sgabeblack@google.com    if (pkt->isRead()) {
8712855Sgabeblack@google.com        // determine if this read is from a (coherent) cache or not
8812855Sgabeblack@google.com        if (pkt->fromCache()) {
8912855Sgabeblack@google.com            assert(pkt->getSize() == blkSize);
9012855Sgabeblack@google.com            // special handling for coherent block requests from
9112855Sgabeblack@google.com            // upper-level caches
9212855Sgabeblack@google.com            if (pkt->needsWritable()) {
9312855Sgabeblack@google.com                // sanity check
9412855Sgabeblack@google.com                assert(pkt->cmd == MemCmd::ReadExReq ||
9512855Sgabeblack@google.com                       pkt->cmd == MemCmd::SCUpgradeFailReq);
9612855Sgabeblack@google.com                assert(!pkt->hasSharers());
9712855Sgabeblack@google.com
9812855Sgabeblack@google.com                // if we have a dirty copy, make sure the recipient
9912855Sgabeblack@google.com                // keeps it marked dirty (in the modified state)
10012855Sgabeblack@google.com                if (blk->isDirty()) {
10112855Sgabeblack@google.com                    pkt->setCacheResponding();
10212855Sgabeblack@google.com                    blk->status &= ~BlkDirty;
10312855Sgabeblack@google.com                }
10412855Sgabeblack@google.com            } else if (blk->isWritable() && !pending_downgrade &&
10512855Sgabeblack@google.com                       !pkt->hasSharers() &&
10612855Sgabeblack@google.com                       pkt->cmd != MemCmd::ReadCleanReq) {
10712855Sgabeblack@google.com                // we can give the requester a writable copy on a read
10812855Sgabeblack@google.com                // request if:
10912855Sgabeblack@google.com                // - we have a writable copy at this level (& below)
11012855Sgabeblack@google.com                // - we don't have a pending snoop from below
11112855Sgabeblack@google.com                //   signaling another read request
11212855Sgabeblack@google.com                // - no other cache above has a copy (otherwise it
11312855Sgabeblack@google.com                //   would have set hasSharers flag when
11412855Sgabeblack@google.com                //   snooping the packet)
11512855Sgabeblack@google.com                // - the read has explicitly asked for a clean
11612855Sgabeblack@google.com                //   copy of the line
11712855Sgabeblack@google.com                if (blk->isDirty()) {
11812855Sgabeblack@google.com                    // special considerations if we're owner:
11912855Sgabeblack@google.com                    if (!deferred_response) {
12012855Sgabeblack@google.com                        // respond with the line in Modified state
12112855Sgabeblack@google.com                        // (cacheResponding set, hasSharers not set)
12212855Sgabeblack@google.com                        pkt->setCacheResponding();
12312855Sgabeblack@google.com
12412855Sgabeblack@google.com                        // if this cache is mostly inclusive, we
12512855Sgabeblack@google.com                        // keep the block in the Exclusive state,
12612855Sgabeblack@google.com                        // and pass it upwards as Modified
12712855Sgabeblack@google.com                        // (writable and dirty), hence we have
12812855Sgabeblack@google.com                        // multiple caches, all on the same path
12912855Sgabeblack@google.com                        // towards memory, all considering the
13012855Sgabeblack@google.com                        // same block writable, but only one
13112855Sgabeblack@google.com                        // considering it Modified
13212855Sgabeblack@google.com
13312855Sgabeblack@google.com                        // we get away with multiple caches (on
13412855Sgabeblack@google.com                        // the same path to memory) considering
13512855Sgabeblack@google.com                        // the block writeable as we always enter
13612855Sgabeblack@google.com                        // the cache hierarchy through a cache,
13712855Sgabeblack@google.com                        // and first snoop upwards in all other
13812855Sgabeblack@google.com                        // branches
13912855Sgabeblack@google.com                        blk->status &= ~BlkDirty;
14012855Sgabeblack@google.com                    } else {
14112855Sgabeblack@google.com                        // if we're responding after our own miss,
14212855Sgabeblack@google.com                        // there's a window where the recipient didn't
14312855Sgabeblack@google.com                        // know it was getting ownership and may not
14412855Sgabeblack@google.com                        // have responded to snoops correctly, so we
14512855Sgabeblack@google.com                        // have to respond with a shared line
14612855Sgabeblack@google.com                        pkt->setHasSharers();
14712855Sgabeblack@google.com                    }
14812855Sgabeblack@google.com                }
14912855Sgabeblack@google.com            } else {
15012855Sgabeblack@google.com                // otherwise only respond with a shared copy
15112855Sgabeblack@google.com                pkt->setHasSharers();
15212855Sgabeblack@google.com            }
15312855Sgabeblack@google.com        }
15412855Sgabeblack@google.com    }
15512855Sgabeblack@google.com}
15612855Sgabeblack@google.com
15712855Sgabeblack@google.com/////////////////////////////////////////////////////
15812855Sgabeblack@google.com//
15912855Sgabeblack@google.com// Access path: requests coming in from the CPU side
16012855Sgabeblack@google.com//
16112855Sgabeblack@google.com/////////////////////////////////////////////////////
16212855Sgabeblack@google.com
16312855Sgabeblack@google.combool
16412855Sgabeblack@google.comCache::access(PacketPtr pkt, CacheBlk *&blk, Cycles &lat,
16512855Sgabeblack@google.com              PacketList &writebacks)
16612855Sgabeblack@google.com{
16712855Sgabeblack@google.com
16812855Sgabeblack@google.com    if (pkt->req->isUncacheable()) {
16912855Sgabeblack@google.com        assert(pkt->isRequest());
17012855Sgabeblack@google.com
17112855Sgabeblack@google.com        chatty_assert(!(isReadOnly && pkt->isWrite()),
17212855Sgabeblack@google.com                      "Should never see a write in a read-only cache %s\n",
17312855Sgabeblack@google.com                      name());
17412855Sgabeblack@google.com
17512855Sgabeblack@google.com        DPRINTF(Cache, "%s for %s\n", __func__, pkt->print());
17612855Sgabeblack@google.com
17712855Sgabeblack@google.com        // flush and invalidate any existing block
17812855Sgabeblack@google.com        CacheBlk *old_blk(tags->findBlock(pkt->getAddr(), pkt->isSecure()));
17912855Sgabeblack@google.com        if (old_blk && old_blk->isValid()) {
18012855Sgabeblack@google.com            BaseCache::evictBlock(old_blk, writebacks);
18112855Sgabeblack@google.com        }
18212855Sgabeblack@google.com
18312855Sgabeblack@google.com        blk = nullptr;
18412855Sgabeblack@google.com        // lookupLatency is the latency in case the request is uncacheable.
18512855Sgabeblack@google.com        lat = lookupLatency;
18612855Sgabeblack@google.com        return false;
18712855Sgabeblack@google.com    }
18812855Sgabeblack@google.com
18912855Sgabeblack@google.com    return BaseCache::access(pkt, blk, lat, writebacks);
19012855Sgabeblack@google.com}
19112855Sgabeblack@google.com
19212855Sgabeblack@google.comvoid
19312855Sgabeblack@google.comCache::doWritebacks(PacketList& writebacks, Tick forward_time)
19412855Sgabeblack@google.com{
19512855Sgabeblack@google.com    while (!writebacks.empty()) {
19612855Sgabeblack@google.com        PacketPtr wbPkt = writebacks.front();
19712855Sgabeblack@google.com        // We use forwardLatency here because we are copying writebacks to
19812855Sgabeblack@google.com        // write buffer.
19912855Sgabeblack@google.com
20012855Sgabeblack@google.com        // Call isCachedAbove for Writebacks, CleanEvicts and
20112855Sgabeblack@google.com        // WriteCleans to discover if the block is cached above.
20212855Sgabeblack@google.com        if (isCachedAbove(wbPkt)) {
20312855Sgabeblack@google.com            if (wbPkt->cmd == MemCmd::CleanEvict) {
20412855Sgabeblack@google.com                // Delete CleanEvict because cached copies exist above. The
20512855Sgabeblack@google.com                // packet destructor will delete the request object because
20612855Sgabeblack@google.com                // this is a non-snoop request packet which does not require a
20712855Sgabeblack@google.com                // response.
20812855Sgabeblack@google.com                delete wbPkt;
20912855Sgabeblack@google.com            } else if (wbPkt->cmd == MemCmd::WritebackClean) {
21012855Sgabeblack@google.com                // clean writeback, do not send since the block is
21112855Sgabeblack@google.com                // still cached above
21212855Sgabeblack@google.com                assert(writebackClean);
21312855Sgabeblack@google.com                delete wbPkt;
21412855Sgabeblack@google.com            } else {
21512855Sgabeblack@google.com                assert(wbPkt->cmd == MemCmd::WritebackDirty ||
21612855Sgabeblack@google.com                       wbPkt->cmd == MemCmd::WriteClean);
21712855Sgabeblack@google.com                // Set BLOCK_CACHED flag in Writeback and send below, so that
21812855Sgabeblack@google.com                // the Writeback does not reset the bit corresponding to this
21912855Sgabeblack@google.com                // address in the snoop filter below.
22012855Sgabeblack@google.com                wbPkt->setBlockCached();
22112855Sgabeblack@google.com                allocateWriteBuffer(wbPkt, forward_time);
22212855Sgabeblack@google.com            }
22312855Sgabeblack@google.com        } else {
22412855Sgabeblack@google.com            // If the block is not cached above, send packet below. Both
22512855Sgabeblack@google.com            // CleanEvict and Writeback with BLOCK_CACHED flag cleared will
22612855Sgabeblack@google.com            // reset the bit corresponding to this address in the snoop filter
22712855Sgabeblack@google.com            // below.
22812855Sgabeblack@google.com            allocateWriteBuffer(wbPkt, forward_time);
22912855Sgabeblack@google.com        }
23012855Sgabeblack@google.com        writebacks.pop_front();
23112855Sgabeblack@google.com    }
23212855Sgabeblack@google.com}
23312855Sgabeblack@google.com
23412855Sgabeblack@google.comvoid
23512855Sgabeblack@google.comCache::doWritebacksAtomic(PacketList& writebacks)
23612855Sgabeblack@google.com{
23712855Sgabeblack@google.com    while (!writebacks.empty()) {
23812855Sgabeblack@google.com        PacketPtr wbPkt = writebacks.front();
23912855Sgabeblack@google.com        // Call isCachedAbove for both Writebacks and CleanEvicts. If
24012855Sgabeblack@google.com        // isCachedAbove returns true we set BLOCK_CACHED flag in Writebacks
24112855Sgabeblack@google.com        // and discard CleanEvicts.
24212855Sgabeblack@google.com        if (isCachedAbove(wbPkt, false)) {
24312855Sgabeblack@google.com            if (wbPkt->cmd == MemCmd::WritebackDirty ||
24412855Sgabeblack@google.com                wbPkt->cmd == MemCmd::WriteClean) {
24512855Sgabeblack@google.com                // Set BLOCK_CACHED flag in Writeback and send below,
24612855Sgabeblack@google.com                // so that the Writeback does not reset the bit
24712855Sgabeblack@google.com                // corresponding to this address in the snoop filter
24812855Sgabeblack@google.com                // below. We can discard CleanEvicts because cached
24912855Sgabeblack@google.com                // copies exist above. Atomic mode isCachedAbove
25012855Sgabeblack@google.com                // modifies packet to set BLOCK_CACHED flag
25112855Sgabeblack@google.com                memSidePort.sendAtomic(wbPkt);
25212855Sgabeblack@google.com            }
25312855Sgabeblack@google.com        } else {
25412855Sgabeblack@google.com            // If the block is not cached above, send packet below. Both
25512855Sgabeblack@google.com            // CleanEvict and Writeback with BLOCK_CACHED flag cleared will
25612855Sgabeblack@google.com            // reset the bit corresponding to this address in the snoop filter
25712855Sgabeblack@google.com            // below.
25812855Sgabeblack@google.com            memSidePort.sendAtomic(wbPkt);
25912855Sgabeblack@google.com        }
26012855Sgabeblack@google.com        writebacks.pop_front();
26112855Sgabeblack@google.com        // In case of CleanEvicts, the packet destructor will delete the
26212855Sgabeblack@google.com        // request object because this is a non-snoop request packet which
26312855Sgabeblack@google.com        // does not require a response.
26412855Sgabeblack@google.com        delete wbPkt;
26512855Sgabeblack@google.com    }
26612855Sgabeblack@google.com}
26712855Sgabeblack@google.com
26812855Sgabeblack@google.com
26912855Sgabeblack@google.comvoid
27012855Sgabeblack@google.comCache::recvTimingSnoopResp(PacketPtr pkt)
27112855Sgabeblack@google.com{
27212855Sgabeblack@google.com    DPRINTF(Cache, "%s for %s\n", __func__, pkt->print());
27312855Sgabeblack@google.com
27412855Sgabeblack@google.com    // determine if the response is from a snoop request we created
27512855Sgabeblack@google.com    // (in which case it should be in the outstandingSnoop), or if we
27612855Sgabeblack@google.com    // merely forwarded someone else's snoop request
27712855Sgabeblack@google.com    const bool forwardAsSnoop = outstandingSnoop.find(pkt->req) ==
27812855Sgabeblack@google.com        outstandingSnoop.end();
27912855Sgabeblack@google.com
28012855Sgabeblack@google.com    if (!forwardAsSnoop) {
28112855Sgabeblack@google.com        // the packet came from this cache, so sink it here and do not
28212855Sgabeblack@google.com        // forward it
28312855Sgabeblack@google.com        assert(pkt->cmd == MemCmd::HardPFResp);
28412855Sgabeblack@google.com
28512855Sgabeblack@google.com        outstandingSnoop.erase(pkt->req);
28612855Sgabeblack@google.com
28712855Sgabeblack@google.com        DPRINTF(Cache, "Got prefetch response from above for addr "
28812855Sgabeblack@google.com                "%#llx (%s)\n", pkt->getAddr(), pkt->isSecure() ? "s" : "ns");
28912855Sgabeblack@google.com        recvTimingResp(pkt);
29012855Sgabeblack@google.com        return;
29112855Sgabeblack@google.com    }
29212855Sgabeblack@google.com
29312855Sgabeblack@google.com    // forwardLatency is set here because there is a response from an
29412855Sgabeblack@google.com    // upper level cache.
29512855Sgabeblack@google.com    // To pay the delay that occurs if the packet comes from the bus,
29612855Sgabeblack@google.com    // we charge also headerDelay.
29712855Sgabeblack@google.com    Tick snoop_resp_time = clockEdge(forwardLatency) + pkt->headerDelay;
29812855Sgabeblack@google.com    // Reset the timing of the packet.
29912855Sgabeblack@google.com    pkt->headerDelay = pkt->payloadDelay = 0;
30012855Sgabeblack@google.com    memSidePort.schedTimingSnoopResp(pkt, snoop_resp_time);
30112855Sgabeblack@google.com}
30212855Sgabeblack@google.com
30312855Sgabeblack@google.comvoid
30412855Sgabeblack@google.comCache::promoteWholeLineWrites(PacketPtr pkt)
30512855Sgabeblack@google.com{
30612855Sgabeblack@google.com    // Cache line clearing instructions
30712855Sgabeblack@google.com    if (doFastWrites && (pkt->cmd == MemCmd::WriteReq) &&
30812855Sgabeblack@google.com        (pkt->getSize() == blkSize) && (pkt->getOffset(blkSize) == 0)) {
30912855Sgabeblack@google.com        pkt->cmd = MemCmd::WriteLineReq;
31012855Sgabeblack@google.com        DPRINTF(Cache, "packet promoted from Write to WriteLineReq\n");
31112855Sgabeblack@google.com    }
31212855Sgabeblack@google.com}
31312855Sgabeblack@google.com
31412855Sgabeblack@google.comvoid
31512855Sgabeblack@google.comCache::handleTimingReqHit(PacketPtr pkt, CacheBlk *blk, Tick request_time)
31612855Sgabeblack@google.com{
31712855Sgabeblack@google.com    // should never be satisfying an uncacheable access as we
31812855Sgabeblack@google.com    // flush and invalidate any existing block as part of the
31912855Sgabeblack@google.com    // lookup
32012855Sgabeblack@google.com    assert(!pkt->req->isUncacheable());
32112855Sgabeblack@google.com
32212855Sgabeblack@google.com    BaseCache::handleTimingReqHit(pkt, blk, request_time);
32312855Sgabeblack@google.com}
32412855Sgabeblack@google.com
32512855Sgabeblack@google.comvoid
32612855Sgabeblack@google.comCache::handleTimingReqMiss(PacketPtr pkt, CacheBlk *blk, Tick forward_time,
32712855Sgabeblack@google.com                           Tick request_time)
32812855Sgabeblack@google.com{
32912855Sgabeblack@google.com    if (pkt->req->isUncacheable()) {
33012855Sgabeblack@google.com        // ignore any existing MSHR if we are dealing with an
33112855Sgabeblack@google.com        // uncacheable request
33212855Sgabeblack@google.com
33312855Sgabeblack@google.com        // should have flushed and have no valid block
33412855Sgabeblack@google.com        assert(!blk || !blk->isValid());
33512855Sgabeblack@google.com
33612855Sgabeblack@google.com        mshr_uncacheable[pkt->cmdToIndex()][pkt->req->masterId()]++;
33712855Sgabeblack@google.com
33812855Sgabeblack@google.com        if (pkt->isWrite()) {
33912855Sgabeblack@google.com            allocateWriteBuffer(pkt, forward_time);
34012855Sgabeblack@google.com        } else {
34112855Sgabeblack@google.com            assert(pkt->isRead());
34212855Sgabeblack@google.com
34312855Sgabeblack@google.com            // uncacheable accesses always allocate a new MSHR
34412855Sgabeblack@google.com
34512855Sgabeblack@google.com            // Here we are using forward_time, modelling the latency of
34612855Sgabeblack@google.com            // a miss (outbound) just as forwardLatency, neglecting the
34712855Sgabeblack@google.com            // lookupLatency component.
34812855Sgabeblack@google.com            allocateMissBuffer(pkt, forward_time);
34912855Sgabeblack@google.com        }
35012855Sgabeblack@google.com
35112855Sgabeblack@google.com        return;
35212855Sgabeblack@google.com    }
35312855Sgabeblack@google.com
35412855Sgabeblack@google.com    Addr blk_addr = pkt->getBlockAddr(blkSize);
35512855Sgabeblack@google.com
35612855Sgabeblack@google.com    MSHR *mshr = mshrQueue.findMatch(blk_addr, pkt->isSecure());
35712855Sgabeblack@google.com
35812855Sgabeblack@google.com    // Software prefetch handling:
35912855Sgabeblack@google.com    // To keep the core from waiting on data it won't look at
36012855Sgabeblack@google.com    // anyway, send back a response with dummy data. Miss handling
36112855Sgabeblack@google.com    // will continue asynchronously. Unfortunately, the core will
36212855Sgabeblack@google.com    // insist upon freeing original Packet/Request, so we have to
36312855Sgabeblack@google.com    // create a new pair with a different lifecycle. Note that this
36412855Sgabeblack@google.com    // processing happens before any MSHR munging on the behalf of
36512855Sgabeblack@google.com    // this request because this new Request will be the one stored
36612855Sgabeblack@google.com    // into the MSHRs, not the original.
36712855Sgabeblack@google.com    if (pkt->cmd.isSWPrefetch()) {
36812855Sgabeblack@google.com        assert(pkt->needsResponse());
36912855Sgabeblack@google.com        assert(pkt->req->hasPaddr());
37012855Sgabeblack@google.com        assert(!pkt->req->isUncacheable());
37112855Sgabeblack@google.com
37212855Sgabeblack@google.com        // There's no reason to add a prefetch as an additional target
37312855Sgabeblack@google.com        // to an existing MSHR. If an outstanding request is already
37412855Sgabeblack@google.com        // in progress, there is nothing for the prefetch to do.
37512855Sgabeblack@google.com        // If this is the case, we don't even create a request at all.
37612855Sgabeblack@google.com        PacketPtr pf = nullptr;
37712855Sgabeblack@google.com
37812855Sgabeblack@google.com        if (!mshr) {
37912855Sgabeblack@google.com            // copy the request and create a new SoftPFReq packet
38012855Sgabeblack@google.com            RequestPtr req = std::make_shared<Request>(pkt->req->getPaddr(),
38112855Sgabeblack@google.com                                                       pkt->req->getSize(),
38212855Sgabeblack@google.com                                                       pkt->req->getFlags(),
38312855Sgabeblack@google.com                                                       pkt->req->masterId());
38412855Sgabeblack@google.com            pf = new Packet(req, pkt->cmd);
38512855Sgabeblack@google.com            pf->allocate();
38612855Sgabeblack@google.com            assert(pf->getAddr() == pkt->getAddr());
38712855Sgabeblack@google.com            assert(pf->getSize() == pkt->getSize());
38812855Sgabeblack@google.com        }
38912855Sgabeblack@google.com
39012855Sgabeblack@google.com        pkt->makeTimingResponse();
39112855Sgabeblack@google.com
39212855Sgabeblack@google.com        // request_time is used here, taking into account lat and the delay
39312855Sgabeblack@google.com        // charged if the packet comes from the xbar.
39412855Sgabeblack@google.com        cpuSidePort.schedTimingResp(pkt, request_time);
39512855Sgabeblack@google.com
39612855Sgabeblack@google.com        // If an outstanding request is in progress (we found an
39712855Sgabeblack@google.com        // MSHR) this is set to null
39812855Sgabeblack@google.com        pkt = pf;
39912855Sgabeblack@google.com    }
40012855Sgabeblack@google.com
40112855Sgabeblack@google.com    BaseCache::handleTimingReqMiss(pkt, mshr, blk, forward_time, request_time);
40212855Sgabeblack@google.com}
40312855Sgabeblack@google.com
40412855Sgabeblack@google.comvoid
40512855Sgabeblack@google.comCache::recvTimingReq(PacketPtr pkt)
40612855Sgabeblack@google.com{
40712855Sgabeblack@google.com    DPRINTF(CacheTags, "%s tags:\n%s\n", __func__, tags->print());
40812855Sgabeblack@google.com
40912855Sgabeblack@google.com    promoteWholeLineWrites(pkt);
41012855Sgabeblack@google.com
41112855Sgabeblack@google.com    if (pkt->cacheResponding()) {
41212855Sgabeblack@google.com        // a cache above us (but not where the packet came from) is
41312855Sgabeblack@google.com        // responding to the request, in other words it has the line
41412855Sgabeblack@google.com        // in Modified or Owned state
41512855Sgabeblack@google.com        DPRINTF(Cache, "Cache above responding to %s: not responding\n",
41612855Sgabeblack@google.com                pkt->print());
41712855Sgabeblack@google.com
41812855Sgabeblack@google.com        // if the packet needs the block to be writable, and the cache
41912855Sgabeblack@google.com        // that has promised to respond (setting the cache responding
42012855Sgabeblack@google.com        // flag) is not providing writable (it is in Owned rather than
42112855Sgabeblack@google.com        // the Modified state), we know that there may be other Shared
42212855Sgabeblack@google.com        // copies in the system; go out and invalidate them all
42312855Sgabeblack@google.com        assert(pkt->needsWritable() && !pkt->responderHadWritable());
42412855Sgabeblack@google.com
42512855Sgabeblack@google.com        // an upstream cache that had the line in Owned state
42612855Sgabeblack@google.com        // (dirty, but not writable), is responding and thus
42712855Sgabeblack@google.com        // transferring the dirty line from one branch of the
42812855Sgabeblack@google.com        // cache hierarchy to another
42912855Sgabeblack@google.com
43012855Sgabeblack@google.com        // send out an express snoop and invalidate all other
43112855Sgabeblack@google.com        // copies (snooping a packet that needs writable is the
43212855Sgabeblack@google.com        // same as an invalidation), thus turning the Owned line
43312855Sgabeblack@google.com        // into a Modified line, note that we don't invalidate the
43412855Sgabeblack@google.com        // block in the current cache or any other cache on the
43512855Sgabeblack@google.com        // path to memory
43612855Sgabeblack@google.com
43712855Sgabeblack@google.com        // create a downstream express snoop with cleared packet
43812855Sgabeblack@google.com        // flags, there is no need to allocate any data as the
43912855Sgabeblack@google.com        // packet is merely used to co-ordinate state transitions
44012855Sgabeblack@google.com        Packet *snoop_pkt = new Packet(pkt, true, false);
44112855Sgabeblack@google.com
44212855Sgabeblack@google.com        // also reset the bus time that the original packet has
44312855Sgabeblack@google.com        // not yet paid for
44412855Sgabeblack@google.com        snoop_pkt->headerDelay = snoop_pkt->payloadDelay = 0;
44512855Sgabeblack@google.com
44612855Sgabeblack@google.com        // make this an instantaneous express snoop, and let the
44712855Sgabeblack@google.com        // other caches in the system know that the another cache
44812855Sgabeblack@google.com        // is responding, because we have found the authorative
44912855Sgabeblack@google.com        // copy (Modified or Owned) that will supply the right
45012855Sgabeblack@google.com        // data
45112855Sgabeblack@google.com        snoop_pkt->setExpressSnoop();
45212855Sgabeblack@google.com        snoop_pkt->setCacheResponding();
45312855Sgabeblack@google.com
45412855Sgabeblack@google.com        // this express snoop travels towards the memory, and at
45512855Sgabeblack@google.com        // every crossbar it is snooped upwards thus reaching
45612855Sgabeblack@google.com        // every cache in the system
45712855Sgabeblack@google.com        bool M5_VAR_USED success = memSidePort.sendTimingReq(snoop_pkt);
45812855Sgabeblack@google.com        // express snoops always succeed
45912855Sgabeblack@google.com        assert(success);
46012855Sgabeblack@google.com
46112855Sgabeblack@google.com        // main memory will delete the snoop packet
46212855Sgabeblack@google.com
46312855Sgabeblack@google.com        // queue for deletion, as opposed to immediate deletion, as
46412855Sgabeblack@google.com        // the sending cache is still relying on the packet
46512855Sgabeblack@google.com        pendingDelete.reset(pkt);
46612855Sgabeblack@google.com
46712855Sgabeblack@google.com        // no need to take any further action in this particular cache
46812855Sgabeblack@google.com        // as an upstram cache has already committed to responding,
46912855Sgabeblack@google.com        // and we have already sent out any express snoops in the
47012855Sgabeblack@google.com        // section above to ensure all other copies in the system are
47112855Sgabeblack@google.com        // invalidated
47212855Sgabeblack@google.com        return;
47312855Sgabeblack@google.com    }
47412855Sgabeblack@google.com
47512855Sgabeblack@google.com    BaseCache::recvTimingReq(pkt);
47612855Sgabeblack@google.com}
47712855Sgabeblack@google.com
47812855Sgabeblack@google.comPacketPtr
47912855Sgabeblack@google.comCache::createMissPacket(PacketPtr cpu_pkt, CacheBlk *blk,
48012855Sgabeblack@google.com                        bool needsWritable,
48112855Sgabeblack@google.com                        bool is_whole_line_write) const
48212855Sgabeblack@google.com{
48312855Sgabeblack@google.com    // should never see evictions here
48412855Sgabeblack@google.com    assert(!cpu_pkt->isEviction());
48512855Sgabeblack@google.com
48612855Sgabeblack@google.com    bool blkValid = blk && blk->isValid();
48712855Sgabeblack@google.com
48812855Sgabeblack@google.com    if (cpu_pkt->req->isUncacheable() ||
48912855Sgabeblack@google.com        (!blkValid && cpu_pkt->isUpgrade()) ||
49012855Sgabeblack@google.com        cpu_pkt->cmd == MemCmd::InvalidateReq || cpu_pkt->isClean()) {
49112855Sgabeblack@google.com        // uncacheable requests and upgrades from upper-level caches
49212855Sgabeblack@google.com        // that missed completely just go through as is
49312855Sgabeblack@google.com        return nullptr;
49412855Sgabeblack@google.com    }
49512855Sgabeblack@google.com
49612855Sgabeblack@google.com    assert(cpu_pkt->needsResponse());
49712855Sgabeblack@google.com
49812855Sgabeblack@google.com    MemCmd cmd;
49912855Sgabeblack@google.com    // @TODO make useUpgrades a parameter.
50012855Sgabeblack@google.com    // Note that ownership protocols require upgrade, otherwise a
50112855Sgabeblack@google.com    // write miss on a shared owned block will generate a ReadExcl,
50212855Sgabeblack@google.com    // which will clobber the owned copy.
50312855Sgabeblack@google.com    const bool useUpgrades = true;
50412855Sgabeblack@google.com    assert(cpu_pkt->cmd != MemCmd::WriteLineReq || is_whole_line_write);
50512855Sgabeblack@google.com    if (is_whole_line_write) {
50612855Sgabeblack@google.com        assert(!blkValid || !blk->isWritable());
50712855Sgabeblack@google.com        // forward as invalidate to all other caches, this gives us
50812855Sgabeblack@google.com        // the line in Exclusive state, and invalidates all other
50912855Sgabeblack@google.com        // copies
51012855Sgabeblack@google.com        cmd = MemCmd::InvalidateReq;
51112855Sgabeblack@google.com    } else if (blkValid && useUpgrades) {
51212855Sgabeblack@google.com        // only reason to be here is that blk is read only and we need
51312855Sgabeblack@google.com        // it to be writable
51412855Sgabeblack@google.com        assert(needsWritable);
51512855Sgabeblack@google.com        assert(!blk->isWritable());
51612855Sgabeblack@google.com        cmd = cpu_pkt->isLLSC() ? MemCmd::SCUpgradeReq : MemCmd::UpgradeReq;
51712855Sgabeblack@google.com    } else if (cpu_pkt->cmd == MemCmd::SCUpgradeFailReq ||
51812855Sgabeblack@google.com               cpu_pkt->cmd == MemCmd::StoreCondFailReq) {
51912855Sgabeblack@google.com        // Even though this SC will fail, we still need to send out the
52012855Sgabeblack@google.com        // request and get the data to supply it to other snoopers in the case
52112855Sgabeblack@google.com        // where the determination the StoreCond fails is delayed due to
52212855Sgabeblack@google.com        // all caches not being on the same local bus.
52312855Sgabeblack@google.com        cmd = MemCmd::SCUpgradeFailReq;
52412855Sgabeblack@google.com    } else {
52512855Sgabeblack@google.com        // block is invalid
52612855Sgabeblack@google.com
52712855Sgabeblack@google.com        // If the request does not need a writable there are two cases
52812855Sgabeblack@google.com        // where we need to ensure the response will not fetch the
52912855Sgabeblack@google.com        // block in dirty state:
53012855Sgabeblack@google.com        // * this cache is read only and it does not perform
53112855Sgabeblack@google.com        //   writebacks,
53212855Sgabeblack@google.com        // * this cache is mostly exclusive and will not fill (since
53312855Sgabeblack@google.com        //   it does not fill it will have to writeback the dirty data
53412855Sgabeblack@google.com        //   immediately which generates uneccesary writebacks).
53512855Sgabeblack@google.com        bool force_clean_rsp = isReadOnly || clusivity == Enums::mostly_excl;
53612855Sgabeblack@google.com        cmd = needsWritable ? MemCmd::ReadExReq :
53712855Sgabeblack@google.com            (force_clean_rsp ? MemCmd::ReadCleanReq : MemCmd::ReadSharedReq);
53812855Sgabeblack@google.com    }
53912855Sgabeblack@google.com    PacketPtr pkt = new Packet(cpu_pkt->req, cmd, blkSize);
54012855Sgabeblack@google.com
54112855Sgabeblack@google.com    // if there are upstream caches that have already marked the
54212855Sgabeblack@google.com    // packet as having sharers (not passing writable), pass that info
54312855Sgabeblack@google.com    // downstream
54412855Sgabeblack@google.com    if (cpu_pkt->hasSharers() && !needsWritable) {
54512855Sgabeblack@google.com        // note that cpu_pkt may have spent a considerable time in the
54612855Sgabeblack@google.com        // MSHR queue and that the information could possibly be out
54712855Sgabeblack@google.com        // of date, however, there is no harm in conservatively
54812855Sgabeblack@google.com        // assuming the block has sharers
54912855Sgabeblack@google.com        pkt->setHasSharers();
55012855Sgabeblack@google.com        DPRINTF(Cache, "%s: passing hasSharers from %s to %s\n",
55112855Sgabeblack@google.com                __func__, cpu_pkt->print(), pkt->print());
55212855Sgabeblack@google.com    }
55312855Sgabeblack@google.com
55412855Sgabeblack@google.com    // the packet should be block aligned
55512855Sgabeblack@google.com    assert(pkt->getAddr() == pkt->getBlockAddr(blkSize));
55612855Sgabeblack@google.com
55712855Sgabeblack@google.com    pkt->allocate();
55812855Sgabeblack@google.com    DPRINTF(Cache, "%s: created %s from %s\n", __func__, pkt->print(),
55912855Sgabeblack@google.com            cpu_pkt->print());
56012855Sgabeblack@google.com    return pkt;
56112855Sgabeblack@google.com}
56212855Sgabeblack@google.com
56312855Sgabeblack@google.com
56412855Sgabeblack@google.comCycles
56512855Sgabeblack@google.comCache::handleAtomicReqMiss(PacketPtr pkt, CacheBlk *&blk,
56612855Sgabeblack@google.com                           PacketList &writebacks)
56712855Sgabeblack@google.com{
56812855Sgabeblack@google.com    // deal with the packets that go through the write path of
56912855Sgabeblack@google.com    // the cache, i.e. any evictions and writes
57012855Sgabeblack@google.com    if (pkt->isEviction() || pkt->cmd == MemCmd::WriteClean ||
57112855Sgabeblack@google.com        (pkt->req->isUncacheable() && pkt->isWrite())) {
57212855Sgabeblack@google.com        Cycles latency = ticksToCycles(memSidePort.sendAtomic(pkt));
57312855Sgabeblack@google.com
57412855Sgabeblack@google.com        // at this point, if the request was an uncacheable write
57512855Sgabeblack@google.com        // request, it has been satisfied by a memory below and the
57612855Sgabeblack@google.com        // packet carries the response back
57712855Sgabeblack@google.com        assert(!(pkt->req->isUncacheable() && pkt->isWrite()) ||
57812855Sgabeblack@google.com               pkt->isResponse());
57912855Sgabeblack@google.com
58012855Sgabeblack@google.com        return latency;
58112855Sgabeblack@google.com    }
58212855Sgabeblack@google.com
58312855Sgabeblack@google.com    // only misses left
58412855Sgabeblack@google.com
58512855Sgabeblack@google.com    PacketPtr bus_pkt = createMissPacket(pkt, blk, pkt->needsWritable(),
58612855Sgabeblack@google.com                                         pkt->isWholeLineWrite(blkSize));
58712855Sgabeblack@google.com
58812855Sgabeblack@google.com    bool is_forward = (bus_pkt == nullptr);
58912855Sgabeblack@google.com
59012855Sgabeblack@google.com    if (is_forward) {
59112855Sgabeblack@google.com        // just forwarding the same request to the next level
59212855Sgabeblack@google.com        // no local cache operation involved
59312855Sgabeblack@google.com        bus_pkt = pkt;
59412855Sgabeblack@google.com    }
59512855Sgabeblack@google.com
59612855Sgabeblack@google.com    DPRINTF(Cache, "%s: Sending an atomic %s\n", __func__,
59712855Sgabeblack@google.com            bus_pkt->print());
59812855Sgabeblack@google.com
59912855Sgabeblack@google.com#if TRACING_ON
60012855Sgabeblack@google.com    CacheBlk::State old_state = blk ? blk->status : 0;
60112855Sgabeblack@google.com#endif
60212855Sgabeblack@google.com
60312855Sgabeblack@google.com    Cycles latency = ticksToCycles(memSidePort.sendAtomic(bus_pkt));
60412855Sgabeblack@google.com
60512855Sgabeblack@google.com    bool is_invalidate = bus_pkt->isInvalidate();
60612855Sgabeblack@google.com
60712855Sgabeblack@google.com    // We are now dealing with the response handling
60812855Sgabeblack@google.com    DPRINTF(Cache, "%s: Receive response: %s in state %i\n", __func__,
60912855Sgabeblack@google.com            bus_pkt->print(), old_state);
61012855Sgabeblack@google.com
61112855Sgabeblack@google.com    // If packet was a forward, the response (if any) is already
61212855Sgabeblack@google.com    // in place in the bus_pkt == pkt structure, so we don't need
61312855Sgabeblack@google.com    // to do anything.  Otherwise, use the separate bus_pkt to
61412855Sgabeblack@google.com    // generate response to pkt and then delete it.
61512855Sgabeblack@google.com    if (!is_forward) {
61612855Sgabeblack@google.com        if (pkt->needsResponse()) {
61712855Sgabeblack@google.com            assert(bus_pkt->isResponse());
61812855Sgabeblack@google.com            if (bus_pkt->isError()) {
61912855Sgabeblack@google.com                pkt->makeAtomicResponse();
62012855Sgabeblack@google.com                pkt->copyError(bus_pkt);
62112855Sgabeblack@google.com            } else if (pkt->isWholeLineWrite(blkSize)) {
62212855Sgabeblack@google.com                // note the use of pkt, not bus_pkt here.
62312855Sgabeblack@google.com
62412855Sgabeblack@google.com                // write-line request to the cache that promoted
62512855Sgabeblack@google.com                // the write to a whole line
62612855Sgabeblack@google.com                const bool allocate = allocOnFill(pkt->cmd) &&
62712855Sgabeblack@google.com                    (!writeAllocator || writeAllocator->allocate());
62812855Sgabeblack@google.com                blk = handleFill(bus_pkt, blk, writebacks, allocate);
62912855Sgabeblack@google.com                assert(blk != NULL);
63012855Sgabeblack@google.com                is_invalidate = false;
63112855Sgabeblack@google.com                satisfyRequest(pkt, blk);
63212855Sgabeblack@google.com            } else if (bus_pkt->isRead() ||
63312855Sgabeblack@google.com                       bus_pkt->cmd == MemCmd::UpgradeResp) {
63412855Sgabeblack@google.com                // we're updating cache state to allow us to
63512855Sgabeblack@google.com                // satisfy the upstream request from the cache
63612855Sgabeblack@google.com                blk = handleFill(bus_pkt, blk, writebacks,
63712855Sgabeblack@google.com                                 allocOnFill(pkt->cmd));
63812855Sgabeblack@google.com                satisfyRequest(pkt, blk);
63912855Sgabeblack@google.com                maintainClusivity(pkt->fromCache(), blk);
64012855Sgabeblack@google.com            } else {
64112855Sgabeblack@google.com                // we're satisfying the upstream request without
64212855Sgabeblack@google.com                // modifying cache state, e.g., a write-through
64312855Sgabeblack@google.com                pkt->makeAtomicResponse();
64412855Sgabeblack@google.com            }
64512855Sgabeblack@google.com        }
64612855Sgabeblack@google.com        delete bus_pkt;
64712855Sgabeblack@google.com    }
64812855Sgabeblack@google.com
64912855Sgabeblack@google.com    if (is_invalidate && blk && blk->isValid()) {
65012855Sgabeblack@google.com        invalidateBlock(blk);
65112855Sgabeblack@google.com    }
65212855Sgabeblack@google.com
65312855Sgabeblack@google.com    return latency;
65412855Sgabeblack@google.com}
65512855Sgabeblack@google.com
65612855Sgabeblack@google.comTick
65712855Sgabeblack@google.comCache::recvAtomic(PacketPtr pkt)
65812855Sgabeblack@google.com{
65912855Sgabeblack@google.com    promoteWholeLineWrites(pkt);
66012855Sgabeblack@google.com
66112855Sgabeblack@google.com    // follow the same flow as in recvTimingReq, and check if a cache
66212855Sgabeblack@google.com    // above us is responding
66312855Sgabeblack@google.com    if (pkt->cacheResponding()) {
66412855Sgabeblack@google.com        assert(!pkt->req->isCacheInvalidate());
66512855Sgabeblack@google.com        DPRINTF(Cache, "Cache above responding to %s: not responding\n",
66612855Sgabeblack@google.com                pkt->print());
66712855Sgabeblack@google.com
66812855Sgabeblack@google.com        // if a cache is responding, and it had the line in Owned
66912855Sgabeblack@google.com        // rather than Modified state, we need to invalidate any
67012855Sgabeblack@google.com        // copies that are not on the same path to memory
67112855Sgabeblack@google.com        assert(pkt->needsWritable() && !pkt->responderHadWritable());
67212855Sgabeblack@google.com
67312855Sgabeblack@google.com        return memSidePort.sendAtomic(pkt);
67412855Sgabeblack@google.com    }
67512855Sgabeblack@google.com
67612855Sgabeblack@google.com    return BaseCache::recvAtomic(pkt);
67712855Sgabeblack@google.com}
67812855Sgabeblack@google.com
67912855Sgabeblack@google.com
68012855Sgabeblack@google.com/////////////////////////////////////////////////////
68112855Sgabeblack@google.com//
68212855Sgabeblack@google.com// Response handling: responses from the memory side
68312855Sgabeblack@google.com//
68412855Sgabeblack@google.com/////////////////////////////////////////////////////
68512855Sgabeblack@google.com
68612855Sgabeblack@google.com
68712855Sgabeblack@google.comvoid
68812855Sgabeblack@google.comCache::serviceMSHRTargets(MSHR *mshr, const PacketPtr pkt, CacheBlk *blk)
68912855Sgabeblack@google.com{
69012855Sgabeblack@google.com    MSHR::Target *initial_tgt = mshr->getTarget();
69112855Sgabeblack@google.com    // First offset for critical word first calculations
69212855Sgabeblack@google.com    const int initial_offset = initial_tgt->pkt->getOffset(blkSize);
69312855Sgabeblack@google.com
69412855Sgabeblack@google.com    const bool is_error = pkt->isError();
69512855Sgabeblack@google.com    // allow invalidation responses originating from write-line
69612855Sgabeblack@google.com    // requests to be discarded
69712855Sgabeblack@google.com    bool is_invalidate = pkt->isInvalidate() &&
69812855Sgabeblack@google.com        !mshr->wasWholeLineWrite;
69912855Sgabeblack@google.com
70012855Sgabeblack@google.com    MSHR::TargetList targets = mshr->extractServiceableTargets(pkt);
70112855Sgabeblack@google.com    for (auto &target: targets) {
70212855Sgabeblack@google.com        Packet *tgt_pkt = target.pkt;
70312855Sgabeblack@google.com        switch (target.source) {
70412855Sgabeblack@google.com          case MSHR::Target::FromCPU:
70512855Sgabeblack@google.com            Tick completion_time;
70612855Sgabeblack@google.com            // Here we charge on completion_time the delay of the xbar if the
70712855Sgabeblack@google.com            // packet comes from it, charged on headerDelay.
70812855Sgabeblack@google.com            completion_time = pkt->headerDelay;
70912855Sgabeblack@google.com
71012855Sgabeblack@google.com            // Software prefetch handling for cache closest to core
71112855Sgabeblack@google.com            if (tgt_pkt->cmd.isSWPrefetch()) {
71212855Sgabeblack@google.com                // a software prefetch would have already been ack'd
71312855Sgabeblack@google.com                // immediately with dummy data so the core would be able to
71412855Sgabeblack@google.com                // retire it. This request completes right here, so we
71512855Sgabeblack@google.com                // deallocate it.
71612855Sgabeblack@google.com                delete tgt_pkt;
71712855Sgabeblack@google.com                break; // skip response
71812855Sgabeblack@google.com            }
71912855Sgabeblack@google.com
72012855Sgabeblack@google.com            // unlike the other packet flows, where data is found in other
72112855Sgabeblack@google.com            // caches or memory and brought back, write-line requests always
72212855Sgabeblack@google.com            // have the data right away, so the above check for "is fill?"
72312855Sgabeblack@google.com            // cannot actually be determined until examining the stored MSHR
72412855Sgabeblack@google.com            // state. We "catch up" with that logic here, which is duplicated
72512855Sgabeblack@google.com            // from above.
72612855Sgabeblack@google.com            if (tgt_pkt->cmd == MemCmd::WriteLineReq) {
72712855Sgabeblack@google.com                assert(!is_error);
72812855Sgabeblack@google.com                assert(blk);
72912855Sgabeblack@google.com                assert(blk->isWritable());
73012855Sgabeblack@google.com            }
731
732            if (blk && blk->isValid() && !mshr->isForward) {
733                satisfyRequest(tgt_pkt, blk, true, mshr->hasPostDowngrade());
734
735                // How many bytes past the first request is this one
736                int transfer_offset =
737                    tgt_pkt->getOffset(blkSize) - initial_offset;
738                if (transfer_offset < 0) {
739                    transfer_offset += blkSize;
740                }
741
742                // If not critical word (offset) return payloadDelay.
743                // responseLatency is the latency of the return path
744                // from lower level caches/memory to an upper level cache or
745                // the core.
746                completion_time += clockEdge(responseLatency) +
747                    (transfer_offset ? pkt->payloadDelay : 0);
748
749                assert(!tgt_pkt->req->isUncacheable());
750
751                assert(tgt_pkt->req->masterId() < system->maxMasters());
752                missLatency[tgt_pkt->cmdToIndex()][tgt_pkt->req->masterId()] +=
753                    completion_time - target.recvTime;
754            } else if (pkt->cmd == MemCmd::UpgradeFailResp) {
755                // failed StoreCond upgrade
756                assert(tgt_pkt->cmd == MemCmd::StoreCondReq ||
757                       tgt_pkt->cmd == MemCmd::StoreCondFailReq ||
758                       tgt_pkt->cmd == MemCmd::SCUpgradeFailReq);
759                // responseLatency is the latency of the return path
760                // from lower level caches/memory to an upper level cache or
761                // the core.
762                completion_time += clockEdge(responseLatency) +
763                    pkt->payloadDelay;
764                tgt_pkt->req->setExtraData(0);
765            } else {
766                // We are about to send a response to a cache above
767                // that asked for an invalidation; we need to
768                // invalidate our copy immediately as the most
769                // up-to-date copy of the block will now be in the
770                // cache above. It will also prevent this cache from
771                // responding (if the block was previously dirty) to
772                // snoops as they should snoop the caches above where
773                // they will get the response from.
774                if (is_invalidate && blk && blk->isValid()) {
775                    invalidateBlock(blk);
776                }
777                // not a cache fill, just forwarding response
778                // responseLatency is the latency of the return path
779                // from lower level cahces/memory to the core.
780                completion_time += clockEdge(responseLatency) +
781                    pkt->payloadDelay;
782                if (pkt->isRead() && !is_error) {
783                    // sanity check
784                    assert(pkt->getAddr() == tgt_pkt->getAddr());
785                    assert(pkt->getSize() >= tgt_pkt->getSize());
786
787                    tgt_pkt->setData(pkt->getConstPtr<uint8_t>());
788                }
789            }
790            tgt_pkt->makeTimingResponse();
791            // if this packet is an error copy that to the new packet
792            if (is_error)
793                tgt_pkt->copyError(pkt);
794            if (tgt_pkt->cmd == MemCmd::ReadResp &&
795                (is_invalidate || mshr->hasPostInvalidate())) {
796                // If intermediate cache got ReadRespWithInvalidate,
797                // propagate that.  Response should not have
798                // isInvalidate() set otherwise.
799                tgt_pkt->cmd = MemCmd::ReadRespWithInvalidate;
800                DPRINTF(Cache, "%s: updated cmd to %s\n", __func__,
801                        tgt_pkt->print());
802            }
803            // Reset the bus additional time as it is now accounted for
804            tgt_pkt->headerDelay = tgt_pkt->payloadDelay = 0;
805            cpuSidePort.schedTimingResp(tgt_pkt, completion_time);
806            break;
807
808          case MSHR::Target::FromPrefetcher:
809            assert(tgt_pkt->cmd == MemCmd::HardPFReq);
810            if (blk)
811                blk->status |= BlkHWPrefetched;
812            delete tgt_pkt;
813            break;
814
815          case MSHR::Target::FromSnoop:
816            // I don't believe that a snoop can be in an error state
817            assert(!is_error);
818            // response to snoop request
819            DPRINTF(Cache, "processing deferred snoop...\n");
820            // If the response is invalidating, a snooping target can
821            // be satisfied if it is also invalidating. If the reponse is, not
822            // only invalidating, but more specifically an InvalidateResp and
823            // the MSHR was created due to an InvalidateReq then a cache above
824            // is waiting to satisfy a WriteLineReq. In this case even an
825            // non-invalidating snoop is added as a target here since this is
826            // the ordering point. When the InvalidateResp reaches this cache,
827            // the snooping target will snoop further the cache above with the
828            // WriteLineReq.
829            assert(!is_invalidate || pkt->cmd == MemCmd::InvalidateResp ||
830                   pkt->req->isCacheMaintenance() ||
831                   mshr->hasPostInvalidate());
832            handleSnoop(tgt_pkt, blk, true, true, mshr->hasPostInvalidate());
833            break;
834
835          default:
836            panic("Illegal target->source enum %d\n", target.source);
837        }
838    }
839
840    maintainClusivity(targets.hasFromCache, blk);
841
842    if (blk && blk->isValid()) {
843        // an invalidate response stemming from a write line request
844        // should not invalidate the block, so check if the
845        // invalidation should be discarded
846        if (is_invalidate || mshr->hasPostInvalidate()) {
847            invalidateBlock(blk);
848        } else if (mshr->hasPostDowngrade()) {
849            blk->status &= ~BlkWritable;
850        }
851    }
852}
853
854PacketPtr
855Cache::evictBlock(CacheBlk *blk)
856{
857    PacketPtr pkt = (blk->isDirty() || writebackClean) ?
858        writebackBlk(blk) : cleanEvictBlk(blk);
859
860    invalidateBlock(blk);
861
862    return pkt;
863}
864
865PacketPtr
866Cache::cleanEvictBlk(CacheBlk *blk)
867{
868    assert(!writebackClean);
869    assert(blk && blk->isValid() && !blk->isDirty());
870
871    // Creating a zero sized write, a message to the snoop filter
872    RequestPtr req = std::make_shared<Request>(
873        regenerateBlkAddr(blk), blkSize, 0, Request::wbMasterId);
874
875    if (blk->isSecure())
876        req->setFlags(Request::SECURE);
877
878    req->taskId(blk->task_id);
879
880    PacketPtr pkt = new Packet(req, MemCmd::CleanEvict);
881    pkt->allocate();
882    DPRINTF(Cache, "Create CleanEvict %s\n", pkt->print());
883
884    return pkt;
885}
886
887/////////////////////////////////////////////////////
888//
889// Snoop path: requests coming in from the memory side
890//
891/////////////////////////////////////////////////////
892
893void
894Cache::doTimingSupplyResponse(PacketPtr req_pkt, const uint8_t *blk_data,
895                              bool already_copied, bool pending_inval)
896{
897    // sanity check
898    assert(req_pkt->isRequest());
899    assert(req_pkt->needsResponse());
900
901    DPRINTF(Cache, "%s: for %s\n", __func__, req_pkt->print());
902    // timing-mode snoop responses require a new packet, unless we
903    // already made a copy...
904    PacketPtr pkt = req_pkt;
905    if (!already_copied)
906        // do not clear flags, and allocate space for data if the
907        // packet needs it (the only packets that carry data are read
908        // responses)
909        pkt = new Packet(req_pkt, false, req_pkt->isRead());
910
911    assert(req_pkt->req->isUncacheable() || req_pkt->isInvalidate() ||
912           pkt->hasSharers());
913    pkt->makeTimingResponse();
914    if (pkt->isRead()) {
915        pkt->setDataFromBlock(blk_data, blkSize);
916    }
917    if (pkt->cmd == MemCmd::ReadResp && pending_inval) {
918        // Assume we defer a response to a read from a far-away cache
919        // A, then later defer a ReadExcl from a cache B on the same
920        // bus as us. We'll assert cacheResponding in both cases, but
921        // in the latter case cacheResponding will keep the
922        // invalidation from reaching cache A. This special response
923        // tells cache A that it gets the block to satisfy its read,
924        // but must immediately invalidate it.
925        pkt->cmd = MemCmd::ReadRespWithInvalidate;
926    }
927    // Here we consider forward_time, paying for just forward latency and
928    // also charging the delay provided by the xbar.
929    // forward_time is used as send_time in next allocateWriteBuffer().
930    Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
931    // Here we reset the timing of the packet.
932    pkt->headerDelay = pkt->payloadDelay = 0;
933    DPRINTF(CacheVerbose, "%s: created response: %s tick: %lu\n", __func__,
934            pkt->print(), forward_time);
935    memSidePort.schedTimingSnoopResp(pkt, forward_time);
936}
937
938uint32_t
939Cache::handleSnoop(PacketPtr pkt, CacheBlk *blk, bool is_timing,
940                   bool is_deferred, bool pending_inval)
941{
942    DPRINTF(CacheVerbose, "%s: for %s\n", __func__, pkt->print());
943    // deferred snoops can only happen in timing mode
944    assert(!(is_deferred && !is_timing));
945    // pending_inval only makes sense on deferred snoops
946    assert(!(pending_inval && !is_deferred));
947    assert(pkt->isRequest());
948
949    // the packet may get modified if we or a forwarded snooper
950    // responds in atomic mode, so remember a few things about the
951    // original packet up front
952    bool invalidate = pkt->isInvalidate();
953    bool M5_VAR_USED needs_writable = pkt->needsWritable();
954
955    // at the moment we could get an uncacheable write which does not
956    // have the invalidate flag, and we need a suitable way of dealing
957    // with this case
958    panic_if(invalidate && pkt->req->isUncacheable(),
959             "%s got an invalidating uncacheable snoop request %s",
960             name(), pkt->print());
961
962    uint32_t snoop_delay = 0;
963
964    if (forwardSnoops) {
965        // first propagate snoop upward to see if anyone above us wants to
966        // handle it.  save & restore packet src since it will get
967        // rewritten to be relative to cpu-side bus (if any)
968        bool alreadyResponded = pkt->cacheResponding();
969        if (is_timing) {
970            // copy the packet so that we can clear any flags before
971            // forwarding it upwards, we also allocate data (passing
972            // the pointer along in case of static data), in case
973            // there is a snoop hit in upper levels
974            Packet snoopPkt(pkt, true, true);
975            snoopPkt.setExpressSnoop();
976            // the snoop packet does not need to wait any additional
977            // time
978            snoopPkt.headerDelay = snoopPkt.payloadDelay = 0;
979            cpuSidePort.sendTimingSnoopReq(&snoopPkt);
980
981            // add the header delay (including crossbar and snoop
982            // delays) of the upward snoop to the snoop delay for this
983            // cache
984            snoop_delay += snoopPkt.headerDelay;
985
986            if (snoopPkt.cacheResponding()) {
987                // cache-to-cache response from some upper cache
988                assert(!alreadyResponded);
989                pkt->setCacheResponding();
990            }
991            // upstream cache has the block, or has an outstanding
992            // MSHR, pass the flag on
993            if (snoopPkt.hasSharers()) {
994                pkt->setHasSharers();
995            }
996            // If this request is a prefetch or clean evict and an upper level
997            // signals block present, make sure to propagate the block
998            // presence to the requester.
999            if (snoopPkt.isBlockCached()) {
1000                pkt->setBlockCached();
1001            }
1002            // If the request was satisfied by snooping the cache
1003            // above, mark the original packet as satisfied too.
1004            if (snoopPkt.satisfied()) {
1005                pkt->setSatisfied();
1006            }
1007        } else {
1008            cpuSidePort.sendAtomicSnoop(pkt);
1009            if (!alreadyResponded && pkt->cacheResponding()) {
1010                // cache-to-cache response from some upper cache:
1011                // forward response to original requester
1012                assert(pkt->isResponse());
1013            }
1014        }
1015    }
1016
1017    bool respond = false;
1018    bool blk_valid = blk && blk->isValid();
1019    if (pkt->isClean()) {
1020        if (blk_valid && blk->isDirty()) {
1021            DPRINTF(CacheVerbose, "%s: packet (snoop) %s found block: %s\n",
1022                    __func__, pkt->print(), blk->print());
1023            PacketPtr wb_pkt = writecleanBlk(blk, pkt->req->getDest(), pkt->id);
1024            PacketList writebacks;
1025            writebacks.push_back(wb_pkt);
1026
1027            if (is_timing) {
1028                // anything that is merely forwarded pays for the forward
1029                // latency and the delay provided by the crossbar
1030                Tick forward_time = clockEdge(forwardLatency) +
1031                    pkt->headerDelay;
1032                doWritebacks(writebacks, forward_time);
1033            } else {
1034                doWritebacksAtomic(writebacks);
1035            }
1036            pkt->setSatisfied();
1037        }
1038    } else if (!blk_valid) {
1039        DPRINTF(CacheVerbose, "%s: snoop miss for %s\n", __func__,
1040                pkt->print());
1041        if (is_deferred) {
1042            // we no longer have the block, and will not respond, but a
1043            // packet was allocated in MSHR::handleSnoop and we have
1044            // to delete it
1045            assert(pkt->needsResponse());
1046
1047            // we have passed the block to a cache upstream, that
1048            // cache should be responding
1049            assert(pkt->cacheResponding());
1050
1051            delete pkt;
1052        }
1053        return snoop_delay;
1054    } else {
1055        DPRINTF(Cache, "%s: snoop hit for %s, old state is %s\n", __func__,
1056                pkt->print(), blk->print());
1057
1058        // We may end up modifying both the block state and the packet (if
1059        // we respond in atomic mode), so just figure out what to do now
1060        // and then do it later. We respond to all snoops that need
1061        // responses provided we have the block in dirty state. The
1062        // invalidation itself is taken care of below. We don't respond to
1063        // cache maintenance operations as this is done by the destination
1064        // xbar.
1065        respond = blk->isDirty() && pkt->needsResponse();
1066
1067        chatty_assert(!(isReadOnly && blk->isDirty()), "Should never have "
1068                      "a dirty block in a read-only cache %s\n", name());
1069    }
1070
1071    // Invalidate any prefetch's from below that would strip write permissions
1072    // MemCmd::HardPFReq is only observed by upstream caches.  After missing
1073    // above and in it's own cache, a new MemCmd::ReadReq is created that
1074    // downstream caches observe.
1075    if (pkt->mustCheckAbove()) {
1076        DPRINTF(Cache, "Found addr %#llx in upper level cache for snoop %s "
1077                "from lower cache\n", pkt->getAddr(), pkt->print());
1078        pkt->setBlockCached();
1079        return snoop_delay;
1080    }
1081
1082    if (pkt->isRead() && !invalidate) {
1083        // reading without requiring the line in a writable state
1084        assert(!needs_writable);
1085        pkt->setHasSharers();
1086
1087        // if the requesting packet is uncacheable, retain the line in
1088        // the current state, otherwhise unset the writable flag,
1089        // which means we go from Modified to Owned (and will respond
1090        // below), remain in Owned (and will respond below), from
1091        // Exclusive to Shared, or remain in Shared
1092        if (!pkt->req->isUncacheable())
1093            blk->status &= ~BlkWritable;
1094        DPRINTF(Cache, "new state is %s\n", blk->print());
1095    }
1096
1097    if (respond) {
1098        // prevent anyone else from responding, cache as well as
1099        // memory, and also prevent any memory from even seeing the
1100        // request
1101        pkt->setCacheResponding();
1102        if (!pkt->isClean() && blk->isWritable()) {
1103            // inform the cache hierarchy that this cache had the line
1104            // in the Modified state so that we avoid unnecessary
1105            // invalidations (see Packet::setResponderHadWritable)
1106            pkt->setResponderHadWritable();
1107
1108            // in the case of an uncacheable request there is no point
1109            // in setting the responderHadWritable flag, but since the
1110            // recipient does not care there is no harm in doing so
1111        } else {
1112            // if the packet has needsWritable set we invalidate our
1113            // copy below and all other copies will be invalidates
1114            // through express snoops, and if needsWritable is not set
1115            // we already called setHasSharers above
1116        }
1117
1118        // if we are returning a writable and dirty (Modified) line,
1119        // we should be invalidating the line
1120        panic_if(!invalidate && !pkt->hasSharers(),
1121                 "%s is passing a Modified line through %s, "
1122                 "but keeping the block", name(), pkt->print());
1123
1124        if (is_timing) {
1125            doTimingSupplyResponse(pkt, blk->data, is_deferred, pending_inval);
1126        } else {
1127            pkt->makeAtomicResponse();
1128            // packets such as upgrades do not actually have any data
1129            // payload
1130            if (pkt->hasData())
1131                pkt->setDataFromBlock(blk->data, blkSize);
1132        }
1133    }
1134
1135    if (!respond && is_deferred) {
1136        assert(pkt->needsResponse());
1137        delete pkt;
1138    }
1139
1140    // Do this last in case it deallocates block data or something
1141    // like that
1142    if (blk_valid && invalidate) {
1143        invalidateBlock(blk);
1144        DPRINTF(Cache, "new state is %s\n", blk->print());
1145    }
1146
1147    return snoop_delay;
1148}
1149
1150
1151void
1152Cache::recvTimingSnoopReq(PacketPtr pkt)
1153{
1154    DPRINTF(CacheVerbose, "%s: for %s\n", __func__, pkt->print());
1155
1156    // no need to snoop requests that are not in range
1157    if (!inRange(pkt->getAddr())) {
1158        return;
1159    }
1160
1161    bool is_secure = pkt->isSecure();
1162    CacheBlk *blk = tags->findBlock(pkt->getAddr(), is_secure);
1163
1164    Addr blk_addr = pkt->getBlockAddr(blkSize);
1165    MSHR *mshr = mshrQueue.findMatch(blk_addr, is_secure);
1166
1167    // Update the latency cost of the snoop so that the crossbar can
1168    // account for it. Do not overwrite what other neighbouring caches
1169    // have already done, rather take the maximum. The update is
1170    // tentative, for cases where we return before an upward snoop
1171    // happens below.
1172    pkt->snoopDelay = std::max<uint32_t>(pkt->snoopDelay,
1173                                         lookupLatency * clockPeriod());
1174
1175    // Inform request(Prefetch, CleanEvict or Writeback) from below of
1176    // MSHR hit, set setBlockCached.
1177    if (mshr && pkt->mustCheckAbove()) {
1178        DPRINTF(Cache, "Setting block cached for %s from lower cache on "
1179                "mshr hit\n", pkt->print());
1180        pkt->setBlockCached();
1181        return;
1182    }
1183
1184    // Bypass any existing cache maintenance requests if the request
1185    // has been satisfied already (i.e., the dirty block has been
1186    // found).
1187    if (mshr && pkt->req->isCacheMaintenance() && pkt->satisfied()) {
1188        return;
1189    }
1190
1191    // Let the MSHR itself track the snoop and decide whether we want
1192    // to go ahead and do the regular cache snoop
1193    if (mshr && mshr->handleSnoop(pkt, order++)) {
1194        DPRINTF(Cache, "Deferring snoop on in-service MSHR to blk %#llx (%s)."
1195                "mshrs: %s\n", blk_addr, is_secure ? "s" : "ns",
1196                mshr->print());
1197
1198        if (mshr->getNumTargets() > numTarget)
1199            warn("allocating bonus target for snoop"); //handle later
1200        return;
1201    }
1202
1203    //We also need to check the writeback buffers and handle those
1204    WriteQueueEntry *wb_entry = writeBuffer.findMatch(blk_addr, is_secure);
1205    if (wb_entry) {
1206        DPRINTF(Cache, "Snoop hit in writeback to addr %#llx (%s)\n",
1207                pkt->getAddr(), is_secure ? "s" : "ns");
1208        // Expect to see only Writebacks and/or CleanEvicts here, both of
1209        // which should not be generated for uncacheable data.
1210        assert(!wb_entry->isUncacheable());
1211        // There should only be a single request responsible for generating
1212        // Writebacks/CleanEvicts.
1213        assert(wb_entry->getNumTargets() == 1);
1214        PacketPtr wb_pkt = wb_entry->getTarget()->pkt;
1215        assert(wb_pkt->isEviction() || wb_pkt->cmd == MemCmd::WriteClean);
1216
1217        if (pkt->isEviction()) {
1218            // if the block is found in the write queue, set the BLOCK_CACHED
1219            // flag for Writeback/CleanEvict snoop. On return the snoop will
1220            // propagate the BLOCK_CACHED flag in Writeback packets and prevent
1221            // any CleanEvicts from travelling down the memory hierarchy.
1222            pkt->setBlockCached();
1223            DPRINTF(Cache, "%s: Squashing %s from lower cache on writequeue "
1224                    "hit\n", __func__, pkt->print());
1225            return;
1226        }
1227
1228        // conceptually writebacks are no different to other blocks in
1229        // this cache, so the behaviour is modelled after handleSnoop,
1230        // the difference being that instead of querying the block
1231        // state to determine if it is dirty and writable, we use the
1232        // command and fields of the writeback packet
1233        bool respond = wb_pkt->cmd == MemCmd::WritebackDirty &&
1234            pkt->needsResponse();
1235        bool have_writable = !wb_pkt->hasSharers();
1236        bool invalidate = pkt->isInvalidate();
1237
1238        if (!pkt->req->isUncacheable() && pkt->isRead() && !invalidate) {
1239            assert(!pkt->needsWritable());
1240            pkt->setHasSharers();
1241            wb_pkt->setHasSharers();
1242        }
1243
1244        if (respond) {
1245            pkt->setCacheResponding();
1246
1247            if (have_writable) {
1248                pkt->setResponderHadWritable();
1249            }
1250
1251            doTimingSupplyResponse(pkt, wb_pkt->getConstPtr<uint8_t>(),
1252                                   false, false);
1253        }
1254
1255        if (invalidate && wb_pkt->cmd != MemCmd::WriteClean) {
1256            // Invalidation trumps our writeback... discard here
1257            // Note: markInService will remove entry from writeback buffer.
1258            markInService(wb_entry);
1259            delete wb_pkt;
1260        }
1261    }
1262
1263    // If this was a shared writeback, there may still be
1264    // other shared copies above that require invalidation.
1265    // We could be more selective and return here if the
1266    // request is non-exclusive or if the writeback is
1267    // exclusive.
1268    uint32_t snoop_delay = handleSnoop(pkt, blk, true, false, false);
1269
1270    // Override what we did when we first saw the snoop, as we now
1271    // also have the cost of the upwards snoops to account for
1272    pkt->snoopDelay = std::max<uint32_t>(pkt->snoopDelay, snoop_delay +
1273                                         lookupLatency * clockPeriod());
1274}
1275
1276Tick
1277Cache::recvAtomicSnoop(PacketPtr pkt)
1278{
1279    // no need to snoop requests that are not in range.
1280    if (!inRange(pkt->getAddr())) {
1281        return 0;
1282    }
1283
1284    CacheBlk *blk = tags->findBlock(pkt->getAddr(), pkt->isSecure());
1285    uint32_t snoop_delay = handleSnoop(pkt, blk, false, false, false);
1286    return snoop_delay + lookupLatency * clockPeriod();
1287}
1288
1289bool
1290Cache::isCachedAbove(PacketPtr pkt, bool is_timing)
1291{
1292    if (!forwardSnoops)
1293        return false;
1294    // Mirroring the flow of HardPFReqs, the cache sends CleanEvict and
1295    // Writeback snoops into upper level caches to check for copies of the
1296    // same block. Using the BLOCK_CACHED flag with the Writeback/CleanEvict
1297    // packet, the cache can inform the crossbar below of presence or absence
1298    // of the block.
1299    if (is_timing) {
1300        Packet snoop_pkt(pkt, true, false);
1301        snoop_pkt.setExpressSnoop();
1302        // Assert that packet is either Writeback or CleanEvict and not a
1303        // prefetch request because prefetch requests need an MSHR and may
1304        // generate a snoop response.
1305        assert(pkt->isEviction() || pkt->cmd == MemCmd::WriteClean);
1306        snoop_pkt.senderState = nullptr;
1307        cpuSidePort.sendTimingSnoopReq(&snoop_pkt);
1308        // Writeback/CleanEvict snoops do not generate a snoop response.
1309        assert(!(snoop_pkt.cacheResponding()));
1310        return snoop_pkt.isBlockCached();
1311    } else {
1312        cpuSidePort.sendAtomicSnoop(pkt);
1313        return pkt->isBlockCached();
1314    }
1315}
1316
1317bool
1318Cache::sendMSHRQueuePacket(MSHR* mshr)
1319{
1320    assert(mshr);
1321
1322    // use request from 1st target
1323    PacketPtr tgt_pkt = mshr->getTarget()->pkt;
1324
1325    if (tgt_pkt->cmd == MemCmd::HardPFReq && forwardSnoops) {
1326        DPRINTF(Cache, "%s: MSHR %s\n", __func__, tgt_pkt->print());
1327
1328        // we should never have hardware prefetches to allocated
1329        // blocks
1330        assert(!tags->findBlock(mshr->blkAddr, mshr->isSecure));
1331
1332        // We need to check the caches above us to verify that
1333        // they don't have a copy of this block in the dirty state
1334        // at the moment. Without this check we could get a stale
1335        // copy from memory that might get used in place of the
1336        // dirty one.
1337        Packet snoop_pkt(tgt_pkt, true, false);
1338        snoop_pkt.setExpressSnoop();
1339        // We are sending this packet upwards, but if it hits we will
1340        // get a snoop response that we end up treating just like a
1341        // normal response, hence it needs the MSHR as its sender
1342        // state
1343        snoop_pkt.senderState = mshr;
1344        cpuSidePort.sendTimingSnoopReq(&snoop_pkt);
1345
1346        // Check to see if the prefetch was squashed by an upper cache (to
1347        // prevent us from grabbing the line) or if a Check to see if a
1348        // writeback arrived between the time the prefetch was placed in
1349        // the MSHRs and when it was selected to be sent or if the
1350        // prefetch was squashed by an upper cache.
1351
1352        // It is important to check cacheResponding before
1353        // prefetchSquashed. If another cache has committed to
1354        // responding, it will be sending a dirty response which will
1355        // arrive at the MSHR allocated for this request. Checking the
1356        // prefetchSquash first may result in the MSHR being
1357        // prematurely deallocated.
1358        if (snoop_pkt.cacheResponding()) {
1359            auto M5_VAR_USED r = outstandingSnoop.insert(snoop_pkt.req);
1360            assert(r.second);
1361
1362            // if we are getting a snoop response with no sharers it
1363            // will be allocated as Modified
1364            bool pending_modified_resp = !snoop_pkt.hasSharers();
1365            markInService(mshr, pending_modified_resp);
1366
1367            DPRINTF(Cache, "Upward snoop of prefetch for addr"
1368                    " %#x (%s) hit\n",
1369                    tgt_pkt->getAddr(), tgt_pkt->isSecure()? "s": "ns");
1370            return false;
1371        }
1372
1373        if (snoop_pkt.isBlockCached()) {
1374            DPRINTF(Cache, "Block present, prefetch squashed by cache.  "
1375                    "Deallocating mshr target %#x.\n",
1376                    mshr->blkAddr);
1377
1378            // Deallocate the mshr target
1379            if (mshrQueue.forceDeallocateTarget(mshr)) {
1380                // Clear block if this deallocation resulted freed an
1381                // mshr when all had previously been utilized
1382                clearBlocked(Blocked_NoMSHRs);
1383            }
1384
1385            // given that no response is expected, delete Request and Packet
1386            delete tgt_pkt;
1387
1388            return false;
1389        }
1390    }
1391
1392    return BaseCache::sendMSHRQueuePacket(mshr);
1393}
1394
1395Cache*
1396CacheParams::create()
1397{
1398    assert(tags);
1399    assert(replacement_policy);
1400
1401    return new Cache(this);
1402}
1403