cache.cc revision 11741
12810Srdreslin@umich.edu/*
211375Sandreas.hansson@arm.com * Copyright (c) 2010-2016 ARM Limited
311051Sandreas.hansson@arm.com * All rights reserved.
411051Sandreas.hansson@arm.com *
511051Sandreas.hansson@arm.com * The license below extends only to copyright in the software and shall
611051Sandreas.hansson@arm.com * not be construed as granting a license to any other intellectual
711051Sandreas.hansson@arm.com * property including but not limited to intellectual property relating
811051Sandreas.hansson@arm.com * to a hardware implementation of the functionality of the software
911051Sandreas.hansson@arm.com * licensed hereunder.  You may use the software subject to the license
1011051Sandreas.hansson@arm.com * terms below provided that you ensure that this notice is replicated
1111051Sandreas.hansson@arm.com * unmodified and in its entirety in all distributions of the software,
1211051Sandreas.hansson@arm.com * modified or unmodified, in source code or in binary form.
1311051Sandreas.hansson@arm.com *
1411051Sandreas.hansson@arm.com * Copyright (c) 2002-2005 The Regents of The University of Michigan
1511051Sandreas.hansson@arm.com * Copyright (c) 2010,2015 Advanced Micro Devices, Inc.
162810Srdreslin@umich.edu * All rights reserved.
172810Srdreslin@umich.edu *
182810Srdreslin@umich.edu * Redistribution and use in source and binary forms, with or without
192810Srdreslin@umich.edu * modification, are permitted provided that the following conditions are
202810Srdreslin@umich.edu * met: redistributions of source code must retain the above copyright
212810Srdreslin@umich.edu * notice, this list of conditions and the following disclaimer;
222810Srdreslin@umich.edu * redistributions in binary form must reproduce the above copyright
232810Srdreslin@umich.edu * notice, this list of conditions and the following disclaimer in the
242810Srdreslin@umich.edu * documentation and/or other materials provided with the distribution;
252810Srdreslin@umich.edu * neither the name of the copyright holders nor the names of its
262810Srdreslin@umich.edu * contributors may be used to endorse or promote products derived from
272810Srdreslin@umich.edu * this software without specific prior written permission.
282810Srdreslin@umich.edu *
292810Srdreslin@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
302810Srdreslin@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
312810Srdreslin@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
322810Srdreslin@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
332810Srdreslin@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
342810Srdreslin@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
352810Srdreslin@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
362810Srdreslin@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
372810Srdreslin@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
382810Srdreslin@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
392810Srdreslin@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
402810Srdreslin@umich.edu *
412810Srdreslin@umich.edu * Authors: Erik Hallnor
4211051Sandreas.hansson@arm.com *          Dave Greene
4311051Sandreas.hansson@arm.com *          Nathan Binkert
442810Srdreslin@umich.edu *          Steve Reinhardt
4511051Sandreas.hansson@arm.com *          Ron Dreslinski
4611051Sandreas.hansson@arm.com *          Andreas Sandberg
472810Srdreslin@umich.edu */
482810Srdreslin@umich.edu
492810Srdreslin@umich.edu/**
502810Srdreslin@umich.edu * @file
5111051Sandreas.hansson@arm.com * Cache definitions.
522810Srdreslin@umich.edu */
532810Srdreslin@umich.edu
5411051Sandreas.hansson@arm.com#include "mem/cache/cache.hh"
552810Srdreslin@umich.edu
5611051Sandreas.hansson@arm.com#include "base/misc.hh"
5711051Sandreas.hansson@arm.com#include "base/types.hh"
5811051Sandreas.hansson@arm.com#include "debug/Cache.hh"
5911051Sandreas.hansson@arm.com#include "debug/CachePort.hh"
6011051Sandreas.hansson@arm.com#include "debug/CacheTags.hh"
6111288Ssteve.reinhardt@amd.com#include "debug/CacheVerbose.hh"
6211051Sandreas.hansson@arm.com#include "mem/cache/blk.hh"
6311051Sandreas.hansson@arm.com#include "mem/cache/mshr.hh"
6411051Sandreas.hansson@arm.com#include "mem/cache/prefetch/base.hh"
6511051Sandreas.hansson@arm.com#include "sim/sim_exit.hh"
6611051Sandreas.hansson@arm.com
6711053Sandreas.hansson@arm.comCache::Cache(const CacheParams *p)
6811053Sandreas.hansson@arm.com    : BaseCache(p, p->system->cacheLineSize()),
6911051Sandreas.hansson@arm.com      tags(p->tags),
7011051Sandreas.hansson@arm.com      prefetcher(p->prefetcher),
7111051Sandreas.hansson@arm.com      doFastWrites(true),
7211197Sandreas.hansson@arm.com      prefetchOnAccess(p->prefetch_on_access),
7311197Sandreas.hansson@arm.com      clusivity(p->clusivity),
7411199Sandreas.hansson@arm.com      writebackClean(p->writeback_clean),
7511197Sandreas.hansson@arm.com      tempBlockWriteback(nullptr),
7611197Sandreas.hansson@arm.com      writebackTempBlockAtomicEvent(this, false,
7711197Sandreas.hansson@arm.com                                    EventBase::Delayed_Writeback_Pri)
7811051Sandreas.hansson@arm.com{
7911051Sandreas.hansson@arm.com    tempBlock = new CacheBlk();
8011051Sandreas.hansson@arm.com    tempBlock->data = new uint8_t[blkSize];
8111051Sandreas.hansson@arm.com
8211051Sandreas.hansson@arm.com    cpuSidePort = new CpuSidePort(p->name + ".cpu_side", this,
8311051Sandreas.hansson@arm.com                                  "CpuSidePort");
8411051Sandreas.hansson@arm.com    memSidePort = new MemSidePort(p->name + ".mem_side", this,
8511051Sandreas.hansson@arm.com                                  "MemSidePort");
8611051Sandreas.hansson@arm.com
8711051Sandreas.hansson@arm.com    tags->setCache(this);
8811051Sandreas.hansson@arm.com    if (prefetcher)
8911051Sandreas.hansson@arm.com        prefetcher->setCache(this);
9011051Sandreas.hansson@arm.com}
9111051Sandreas.hansson@arm.com
9211051Sandreas.hansson@arm.comCache::~Cache()
9311051Sandreas.hansson@arm.com{
9411051Sandreas.hansson@arm.com    delete [] tempBlock->data;
9511051Sandreas.hansson@arm.com    delete tempBlock;
9611051Sandreas.hansson@arm.com
9711051Sandreas.hansson@arm.com    delete cpuSidePort;
9811051Sandreas.hansson@arm.com    delete memSidePort;
9911051Sandreas.hansson@arm.com}
10011051Sandreas.hansson@arm.com
10111051Sandreas.hansson@arm.comvoid
10211051Sandreas.hansson@arm.comCache::regStats()
10311051Sandreas.hansson@arm.com{
10411051Sandreas.hansson@arm.com    BaseCache::regStats();
10511051Sandreas.hansson@arm.com}
10611051Sandreas.hansson@arm.com
10711051Sandreas.hansson@arm.comvoid
10811051Sandreas.hansson@arm.comCache::cmpAndSwap(CacheBlk *blk, PacketPtr pkt)
10911051Sandreas.hansson@arm.com{
11011051Sandreas.hansson@arm.com    assert(pkt->isRequest());
11111051Sandreas.hansson@arm.com
11211051Sandreas.hansson@arm.com    uint64_t overwrite_val;
11311051Sandreas.hansson@arm.com    bool overwrite_mem;
11411051Sandreas.hansson@arm.com    uint64_t condition_val64;
11511051Sandreas.hansson@arm.com    uint32_t condition_val32;
11611051Sandreas.hansson@arm.com
11711051Sandreas.hansson@arm.com    int offset = tags->extractBlkOffset(pkt->getAddr());
11811051Sandreas.hansson@arm.com    uint8_t *blk_data = blk->data + offset;
11911051Sandreas.hansson@arm.com
12011051Sandreas.hansson@arm.com    assert(sizeof(uint64_t) >= pkt->getSize());
12111051Sandreas.hansson@arm.com
12211051Sandreas.hansson@arm.com    overwrite_mem = true;
12311051Sandreas.hansson@arm.com    // keep a copy of our possible write value, and copy what is at the
12411051Sandreas.hansson@arm.com    // memory address into the packet
12511051Sandreas.hansson@arm.com    pkt->writeData((uint8_t *)&overwrite_val);
12611051Sandreas.hansson@arm.com    pkt->setData(blk_data);
12711051Sandreas.hansson@arm.com
12811051Sandreas.hansson@arm.com    if (pkt->req->isCondSwap()) {
12911051Sandreas.hansson@arm.com        if (pkt->getSize() == sizeof(uint64_t)) {
13011051Sandreas.hansson@arm.com            condition_val64 = pkt->req->getExtraData();
13111051Sandreas.hansson@arm.com            overwrite_mem = !std::memcmp(&condition_val64, blk_data,
13211051Sandreas.hansson@arm.com                                         sizeof(uint64_t));
13311051Sandreas.hansson@arm.com        } else if (pkt->getSize() == sizeof(uint32_t)) {
13411051Sandreas.hansson@arm.com            condition_val32 = (uint32_t)pkt->req->getExtraData();
13511051Sandreas.hansson@arm.com            overwrite_mem = !std::memcmp(&condition_val32, blk_data,
13611051Sandreas.hansson@arm.com                                         sizeof(uint32_t));
13711051Sandreas.hansson@arm.com        } else
13811051Sandreas.hansson@arm.com            panic("Invalid size for conditional read/write\n");
13911051Sandreas.hansson@arm.com    }
14011051Sandreas.hansson@arm.com
14111051Sandreas.hansson@arm.com    if (overwrite_mem) {
14211051Sandreas.hansson@arm.com        std::memcpy(blk_data, &overwrite_val, pkt->getSize());
14311051Sandreas.hansson@arm.com        blk->status |= BlkDirty;
14411051Sandreas.hansson@arm.com    }
14511051Sandreas.hansson@arm.com}
14611051Sandreas.hansson@arm.com
14711051Sandreas.hansson@arm.com
14811051Sandreas.hansson@arm.comvoid
14911601Sandreas.hansson@arm.comCache::satisfyRequest(PacketPtr pkt, CacheBlk *blk,
15011601Sandreas.hansson@arm.com                      bool deferred_response, bool pending_downgrade)
15111051Sandreas.hansson@arm.com{
15211051Sandreas.hansson@arm.com    assert(pkt->isRequest());
15311051Sandreas.hansson@arm.com
15411051Sandreas.hansson@arm.com    assert(blk && blk->isValid());
15511051Sandreas.hansson@arm.com    // Occasionally this is not true... if we are a lower-level cache
15611051Sandreas.hansson@arm.com    // satisfying a string of Read and ReadEx requests from
15711051Sandreas.hansson@arm.com    // upper-level caches, a Read will mark the block as shared but we
15811051Sandreas.hansson@arm.com    // can satisfy a following ReadEx anyway since we can rely on the
15911051Sandreas.hansson@arm.com    // Read requester(s) to have buffered the ReadEx snoop and to
16011051Sandreas.hansson@arm.com    // invalidate their blocks after receiving them.
16111284Sandreas.hansson@arm.com    // assert(!pkt->needsWritable() || blk->isWritable());
16211051Sandreas.hansson@arm.com    assert(pkt->getOffset(blkSize) + pkt->getSize() <= blkSize);
16311051Sandreas.hansson@arm.com
16411051Sandreas.hansson@arm.com    // Check RMW operations first since both isRead() and
16511051Sandreas.hansson@arm.com    // isWrite() will be true for them
16611051Sandreas.hansson@arm.com    if (pkt->cmd == MemCmd::SwapReq) {
16711051Sandreas.hansson@arm.com        cmpAndSwap(blk, pkt);
16811051Sandreas.hansson@arm.com    } else if (pkt->isWrite()) {
16911284Sandreas.hansson@arm.com        // we have the block in a writable state and can go ahead,
17011284Sandreas.hansson@arm.com        // note that the line may be also be considered writable in
17111284Sandreas.hansson@arm.com        // downstream caches along the path to memory, but always
17211284Sandreas.hansson@arm.com        // Exclusive, and never Modified
17311051Sandreas.hansson@arm.com        assert(blk->isWritable());
17411284Sandreas.hansson@arm.com        // Write or WriteLine at the first cache with block in writable state
17511051Sandreas.hansson@arm.com        if (blk->checkWrite(pkt)) {
17611051Sandreas.hansson@arm.com            pkt->writeDataToBlock(blk->data, blkSize);
17711051Sandreas.hansson@arm.com        }
17811284Sandreas.hansson@arm.com        // Always mark the line as dirty (and thus transition to the
17911284Sandreas.hansson@arm.com        // Modified state) even if we are a failed StoreCond so we
18011284Sandreas.hansson@arm.com        // supply data to any snoops that have appended themselves to
18111284Sandreas.hansson@arm.com        // this cache before knowing the store will fail.
18211051Sandreas.hansson@arm.com        blk->status |= BlkDirty;
18311288Ssteve.reinhardt@amd.com        DPRINTF(CacheVerbose, "%s for %s addr %#llx size %d (write)\n",
18411288Ssteve.reinhardt@amd.com                __func__, pkt->cmdString(), pkt->getAddr(), pkt->getSize());
18511051Sandreas.hansson@arm.com    } else if (pkt->isRead()) {
18611051Sandreas.hansson@arm.com        if (pkt->isLLSC()) {
18711051Sandreas.hansson@arm.com            blk->trackLoadLocked(pkt);
18811051Sandreas.hansson@arm.com        }
18911286Sandreas.hansson@arm.com
19011286Sandreas.hansson@arm.com        // all read responses have a data payload
19111286Sandreas.hansson@arm.com        assert(pkt->hasRespData());
19211051Sandreas.hansson@arm.com        pkt->setDataFromBlock(blk->data, blkSize);
19311286Sandreas.hansson@arm.com
19411600Sandreas.hansson@arm.com        // determine if this read is from a (coherent) cache or not
19511600Sandreas.hansson@arm.com        if (pkt->fromCache()) {
19611051Sandreas.hansson@arm.com            assert(pkt->getSize() == blkSize);
19711051Sandreas.hansson@arm.com            // special handling for coherent block requests from
19811051Sandreas.hansson@arm.com            // upper-level caches
19911284Sandreas.hansson@arm.com            if (pkt->needsWritable()) {
20011051Sandreas.hansson@arm.com                // sanity check
20111051Sandreas.hansson@arm.com                assert(pkt->cmd == MemCmd::ReadExReq ||
20211051Sandreas.hansson@arm.com                       pkt->cmd == MemCmd::SCUpgradeFailReq);
20311602Sandreas.hansson@arm.com                assert(!pkt->hasSharers());
20411051Sandreas.hansson@arm.com
20511051Sandreas.hansson@arm.com                // if we have a dirty copy, make sure the recipient
20611284Sandreas.hansson@arm.com                // keeps it marked dirty (in the modified state)
20711051Sandreas.hansson@arm.com                if (blk->isDirty()) {
20811284Sandreas.hansson@arm.com                    pkt->setCacheResponding();
20911602Sandreas.hansson@arm.com                    blk->status &= ~BlkDirty;
21011051Sandreas.hansson@arm.com                }
21111051Sandreas.hansson@arm.com            } else if (blk->isWritable() && !pending_downgrade &&
21211284Sandreas.hansson@arm.com                       !pkt->hasSharers() &&
21311051Sandreas.hansson@arm.com                       pkt->cmd != MemCmd::ReadCleanReq) {
21411284Sandreas.hansson@arm.com                // we can give the requester a writable copy on a read
21511284Sandreas.hansson@arm.com                // request if:
21611284Sandreas.hansson@arm.com                // - we have a writable copy at this level (& below)
21711051Sandreas.hansson@arm.com                // - we don't have a pending snoop from below
21811051Sandreas.hansson@arm.com                //   signaling another read request
21911051Sandreas.hansson@arm.com                // - no other cache above has a copy (otherwise it
22011284Sandreas.hansson@arm.com                //   would have set hasSharers flag when
22111284Sandreas.hansson@arm.com                //   snooping the packet)
22211284Sandreas.hansson@arm.com                // - the read has explicitly asked for a clean
22311284Sandreas.hansson@arm.com                //   copy of the line
22411051Sandreas.hansson@arm.com                if (blk->isDirty()) {
22511051Sandreas.hansson@arm.com                    // special considerations if we're owner:
22611051Sandreas.hansson@arm.com                    if (!deferred_response) {
22711284Sandreas.hansson@arm.com                        // respond with the line in Modified state
22811284Sandreas.hansson@arm.com                        // (cacheResponding set, hasSharers not set)
22911284Sandreas.hansson@arm.com                        pkt->setCacheResponding();
23011197Sandreas.hansson@arm.com
23111601Sandreas.hansson@arm.com                        // if this cache is mostly inclusive, we
23211601Sandreas.hansson@arm.com                        // keep the block in the Exclusive state,
23311601Sandreas.hansson@arm.com                        // and pass it upwards as Modified
23411601Sandreas.hansson@arm.com                        // (writable and dirty), hence we have
23511601Sandreas.hansson@arm.com                        // multiple caches, all on the same path
23611601Sandreas.hansson@arm.com                        // towards memory, all considering the
23711601Sandreas.hansson@arm.com                        // same block writable, but only one
23811601Sandreas.hansson@arm.com                        // considering it Modified
23911197Sandreas.hansson@arm.com
24011601Sandreas.hansson@arm.com                        // we get away with multiple caches (on
24111601Sandreas.hansson@arm.com                        // the same path to memory) considering
24211601Sandreas.hansson@arm.com                        // the block writeable as we always enter
24311601Sandreas.hansson@arm.com                        // the cache hierarchy through a cache,
24411601Sandreas.hansson@arm.com                        // and first snoop upwards in all other
24511601Sandreas.hansson@arm.com                        // branches
24611601Sandreas.hansson@arm.com                        blk->status &= ~BlkDirty;
24711051Sandreas.hansson@arm.com                    } else {
24811051Sandreas.hansson@arm.com                        // if we're responding after our own miss,
24911051Sandreas.hansson@arm.com                        // there's a window where the recipient didn't
25011051Sandreas.hansson@arm.com                        // know it was getting ownership and may not
25111051Sandreas.hansson@arm.com                        // have responded to snoops correctly, so we
25211284Sandreas.hansson@arm.com                        // have to respond with a shared line
25311284Sandreas.hansson@arm.com                        pkt->setHasSharers();
25411051Sandreas.hansson@arm.com                    }
25511051Sandreas.hansson@arm.com                }
25611051Sandreas.hansson@arm.com            } else {
25711051Sandreas.hansson@arm.com                // otherwise only respond with a shared copy
25811284Sandreas.hansson@arm.com                pkt->setHasSharers();
25911051Sandreas.hansson@arm.com            }
26011051Sandreas.hansson@arm.com        }
26111602Sandreas.hansson@arm.com    } else if (pkt->isUpgrade()) {
26211602Sandreas.hansson@arm.com        // sanity check
26311602Sandreas.hansson@arm.com        assert(!pkt->hasSharers());
26411602Sandreas.hansson@arm.com
26511602Sandreas.hansson@arm.com        if (blk->isDirty()) {
26611602Sandreas.hansson@arm.com            // we were in the Owned state, and a cache above us that
26711602Sandreas.hansson@arm.com            // has the line in Shared state needs to be made aware
26811602Sandreas.hansson@arm.com            // that the data it already has is in fact dirty
26911602Sandreas.hansson@arm.com            pkt->setCacheResponding();
27011602Sandreas.hansson@arm.com            blk->status &= ~BlkDirty;
27111602Sandreas.hansson@arm.com        }
27211051Sandreas.hansson@arm.com    } else {
27311602Sandreas.hansson@arm.com        assert(pkt->isInvalidate());
27411197Sandreas.hansson@arm.com        invalidateBlock(blk);
27511288Ssteve.reinhardt@amd.com        DPRINTF(CacheVerbose, "%s for %s addr %#llx size %d (invalidation)\n",
27611051Sandreas.hansson@arm.com                __func__, pkt->cmdString(), pkt->getAddr(), pkt->getSize());
27711051Sandreas.hansson@arm.com    }
27811051Sandreas.hansson@arm.com}
27911051Sandreas.hansson@arm.com
28011051Sandreas.hansson@arm.com/////////////////////////////////////////////////////
28111051Sandreas.hansson@arm.com//
28211051Sandreas.hansson@arm.com// Access path: requests coming in from the CPU side
28311051Sandreas.hansson@arm.com//
28411051Sandreas.hansson@arm.com/////////////////////////////////////////////////////
28511051Sandreas.hansson@arm.com
28611051Sandreas.hansson@arm.combool
28711051Sandreas.hansson@arm.comCache::access(PacketPtr pkt, CacheBlk *&blk, Cycles &lat,
28811051Sandreas.hansson@arm.com              PacketList &writebacks)
28911051Sandreas.hansson@arm.com{
29011051Sandreas.hansson@arm.com    // sanity check
29111051Sandreas.hansson@arm.com    assert(pkt->isRequest());
29211051Sandreas.hansson@arm.com
29311051Sandreas.hansson@arm.com    chatty_assert(!(isReadOnly && pkt->isWrite()),
29411051Sandreas.hansson@arm.com                  "Should never see a write in a read-only cache %s\n",
29511051Sandreas.hansson@arm.com                  name());
29611051Sandreas.hansson@arm.com
29711288Ssteve.reinhardt@amd.com    DPRINTF(CacheVerbose, "%s for %s addr %#llx size %d\n", __func__,
29811051Sandreas.hansson@arm.com            pkt->cmdString(), pkt->getAddr(), pkt->getSize());
29911051Sandreas.hansson@arm.com
30011051Sandreas.hansson@arm.com    if (pkt->req->isUncacheable()) {
30111051Sandreas.hansson@arm.com        DPRINTF(Cache, "%s%s addr %#llx uncacheable\n", pkt->cmdString(),
30211051Sandreas.hansson@arm.com                pkt->req->isInstFetch() ? " (ifetch)" : "",
30311051Sandreas.hansson@arm.com                pkt->getAddr());
30411051Sandreas.hansson@arm.com
30511051Sandreas.hansson@arm.com        // flush and invalidate any existing block
30611051Sandreas.hansson@arm.com        CacheBlk *old_blk(tags->findBlock(pkt->getAddr(), pkt->isSecure()));
30711051Sandreas.hansson@arm.com        if (old_blk && old_blk->isValid()) {
30811199Sandreas.hansson@arm.com            if (old_blk->isDirty() || writebackClean)
30911051Sandreas.hansson@arm.com                writebacks.push_back(writebackBlk(old_blk));
31011051Sandreas.hansson@arm.com            else
31111051Sandreas.hansson@arm.com                writebacks.push_back(cleanEvictBlk(old_blk));
31211051Sandreas.hansson@arm.com            tags->invalidate(old_blk);
31311051Sandreas.hansson@arm.com            old_blk->invalidate();
31411051Sandreas.hansson@arm.com        }
31511051Sandreas.hansson@arm.com
31611484Snikos.nikoleris@arm.com        blk = nullptr;
31711051Sandreas.hansson@arm.com        // lookupLatency is the latency in case the request is uncacheable.
31811051Sandreas.hansson@arm.com        lat = lookupLatency;
31911051Sandreas.hansson@arm.com        return false;
32011051Sandreas.hansson@arm.com    }
32111051Sandreas.hansson@arm.com
32211051Sandreas.hansson@arm.com    ContextID id = pkt->req->hasContextId() ?
32311051Sandreas.hansson@arm.com        pkt->req->contextId() : InvalidContextID;
32411051Sandreas.hansson@arm.com    // Here lat is the value passed as parameter to accessBlock() function
32511051Sandreas.hansson@arm.com    // that can modify its value.
32611051Sandreas.hansson@arm.com    blk = tags->accessBlock(pkt->getAddr(), pkt->isSecure(), lat, id);
32711051Sandreas.hansson@arm.com
32811051Sandreas.hansson@arm.com    DPRINTF(Cache, "%s%s addr %#llx size %d (%s) %s\n", pkt->cmdString(),
32911051Sandreas.hansson@arm.com            pkt->req->isInstFetch() ? " (ifetch)" : "",
33011051Sandreas.hansson@arm.com            pkt->getAddr(), pkt->getSize(), pkt->isSecure() ? "s" : "ns",
33111051Sandreas.hansson@arm.com            blk ? "hit " + blk->print() : "miss");
33211051Sandreas.hansson@arm.com
33311051Sandreas.hansson@arm.com
33411199Sandreas.hansson@arm.com    if (pkt->isEviction()) {
33511051Sandreas.hansson@arm.com        // We check for presence of block in above caches before issuing
33611051Sandreas.hansson@arm.com        // Writeback or CleanEvict to write buffer. Therefore the only
33711051Sandreas.hansson@arm.com        // possible cases can be of a CleanEvict packet coming from above
33811051Sandreas.hansson@arm.com        // encountering a Writeback generated in this cache peer cache and
33911051Sandreas.hansson@arm.com        // waiting in the write buffer. Cases of upper level peer caches
34011051Sandreas.hansson@arm.com        // generating CleanEvict and Writeback or simply CleanEvict and
34111051Sandreas.hansson@arm.com        // CleanEvict almost simultaneously will be caught by snoops sent out
34211051Sandreas.hansson@arm.com        // by crossbar.
34311375Sandreas.hansson@arm.com        WriteQueueEntry *wb_entry = writeBuffer.findMatch(pkt->getAddr(),
34411375Sandreas.hansson@arm.com                                                          pkt->isSecure());
34511375Sandreas.hansson@arm.com        if (wb_entry) {
34611199Sandreas.hansson@arm.com            assert(wb_entry->getNumTargets() == 1);
34711199Sandreas.hansson@arm.com            PacketPtr wbPkt = wb_entry->getTarget()->pkt;
34811199Sandreas.hansson@arm.com            assert(wbPkt->isWriteback());
34911199Sandreas.hansson@arm.com
35011199Sandreas.hansson@arm.com            if (pkt->isCleanEviction()) {
35111199Sandreas.hansson@arm.com                // The CleanEvict and WritebackClean snoops into other
35211199Sandreas.hansson@arm.com                // peer caches of the same level while traversing the
35311199Sandreas.hansson@arm.com                // crossbar. If a copy of the block is found, the
35411199Sandreas.hansson@arm.com                // packet is deleted in the crossbar. Hence, none of
35511199Sandreas.hansson@arm.com                // the other upper level caches connected to this
35611199Sandreas.hansson@arm.com                // cache have the block, so we can clear the
35711199Sandreas.hansson@arm.com                // BLOCK_CACHED flag in the Writeback if set and
35811199Sandreas.hansson@arm.com                // discard the CleanEvict by returning true.
35911199Sandreas.hansson@arm.com                wbPkt->clearBlockCached();
36011199Sandreas.hansson@arm.com                return true;
36111199Sandreas.hansson@arm.com            } else {
36211199Sandreas.hansson@arm.com                assert(pkt->cmd == MemCmd::WritebackDirty);
36311199Sandreas.hansson@arm.com                // Dirty writeback from above trumps our clean
36411199Sandreas.hansson@arm.com                // writeback... discard here
36511199Sandreas.hansson@arm.com                // Note: markInService will remove entry from writeback buffer.
36611375Sandreas.hansson@arm.com                markInService(wb_entry);
36711199Sandreas.hansson@arm.com                delete wbPkt;
36811199Sandreas.hansson@arm.com            }
36911051Sandreas.hansson@arm.com        }
37011051Sandreas.hansson@arm.com    }
37111051Sandreas.hansson@arm.com
37211051Sandreas.hansson@arm.com    // Writeback handling is special case.  We can write the block into
37311051Sandreas.hansson@arm.com    // the cache without having a writeable copy (or any copy at all).
37411199Sandreas.hansson@arm.com    if (pkt->isWriteback()) {
37511051Sandreas.hansson@arm.com        assert(blkSize == pkt->getSize());
37611199Sandreas.hansson@arm.com
37711199Sandreas.hansson@arm.com        // we could get a clean writeback while we are having
37811199Sandreas.hansson@arm.com        // outstanding accesses to a block, do the simple thing for
37911199Sandreas.hansson@arm.com        // now and drop the clean writeback so that we do not upset
38011199Sandreas.hansson@arm.com        // any ordering/decisions about ownership already taken
38111199Sandreas.hansson@arm.com        if (pkt->cmd == MemCmd::WritebackClean &&
38211199Sandreas.hansson@arm.com            mshrQueue.findMatch(pkt->getAddr(), pkt->isSecure())) {
38311199Sandreas.hansson@arm.com            DPRINTF(Cache, "Clean writeback %#llx to block with MSHR, "
38411199Sandreas.hansson@arm.com                    "dropping\n", pkt->getAddr());
38511199Sandreas.hansson@arm.com            return true;
38611199Sandreas.hansson@arm.com        }
38711199Sandreas.hansson@arm.com
38811484Snikos.nikoleris@arm.com        if (blk == nullptr) {
38911051Sandreas.hansson@arm.com            // need to do a replacement
39011051Sandreas.hansson@arm.com            blk = allocateBlock(pkt->getAddr(), pkt->isSecure(), writebacks);
39111484Snikos.nikoleris@arm.com            if (blk == nullptr) {
39211051Sandreas.hansson@arm.com                // no replaceable block available: give up, fwd to next level.
39311051Sandreas.hansson@arm.com                incMissCount(pkt);
39411051Sandreas.hansson@arm.com                return false;
39511051Sandreas.hansson@arm.com            }
39611051Sandreas.hansson@arm.com            tags->insertBlock(pkt, blk);
39711051Sandreas.hansson@arm.com
39811051Sandreas.hansson@arm.com            blk->status = (BlkValid | BlkReadable);
39911051Sandreas.hansson@arm.com            if (pkt->isSecure()) {
40011051Sandreas.hansson@arm.com                blk->status |= BlkSecure;
40111051Sandreas.hansson@arm.com            }
40211051Sandreas.hansson@arm.com        }
40311199Sandreas.hansson@arm.com        // only mark the block dirty if we got a writeback command,
40411199Sandreas.hansson@arm.com        // and leave it as is for a clean writeback
40511199Sandreas.hansson@arm.com        if (pkt->cmd == MemCmd::WritebackDirty) {
40611199Sandreas.hansson@arm.com            blk->status |= BlkDirty;
40711199Sandreas.hansson@arm.com        }
40811284Sandreas.hansson@arm.com        // if the packet does not have sharers, it is passing
40911284Sandreas.hansson@arm.com        // writable, and we got the writeback in Modified or Exclusive
41011284Sandreas.hansson@arm.com        // state, if not we are in the Owned or Shared state
41111284Sandreas.hansson@arm.com        if (!pkt->hasSharers()) {
41211051Sandreas.hansson@arm.com            blk->status |= BlkWritable;
41311051Sandreas.hansson@arm.com        }
41411051Sandreas.hansson@arm.com        // nothing else to do; writeback doesn't expect response
41511051Sandreas.hansson@arm.com        assert(!pkt->needsResponse());
41611051Sandreas.hansson@arm.com        std::memcpy(blk->data, pkt->getConstPtr<uint8_t>(), blkSize);
41711051Sandreas.hansson@arm.com        DPRINTF(Cache, "%s new state is %s\n", __func__, blk->print());
41811051Sandreas.hansson@arm.com        incHitCount(pkt);
41911051Sandreas.hansson@arm.com        return true;
42011051Sandreas.hansson@arm.com    } else if (pkt->cmd == MemCmd::CleanEvict) {
42111484Snikos.nikoleris@arm.com        if (blk != nullptr) {
42211051Sandreas.hansson@arm.com            // Found the block in the tags, need to stop CleanEvict from
42311051Sandreas.hansson@arm.com            // propagating further down the hierarchy. Returning true will
42411051Sandreas.hansson@arm.com            // treat the CleanEvict like a satisfied write request and delete
42511051Sandreas.hansson@arm.com            // it.
42611051Sandreas.hansson@arm.com            return true;
42711051Sandreas.hansson@arm.com        }
42811051Sandreas.hansson@arm.com        // We didn't find the block here, propagate the CleanEvict further
42911051Sandreas.hansson@arm.com        // down the memory hierarchy. Returning false will treat the CleanEvict
43011051Sandreas.hansson@arm.com        // like a Writeback which could not find a replaceable block so has to
43111051Sandreas.hansson@arm.com        // go to next level.
43211051Sandreas.hansson@arm.com        return false;
43311601Sandreas.hansson@arm.com    } else if (blk && (pkt->needsWritable() ? blk->isWritable() :
43411601Sandreas.hansson@arm.com                       blk->isReadable())) {
43511051Sandreas.hansson@arm.com        // OK to satisfy access
43611051Sandreas.hansson@arm.com        incHitCount(pkt);
43711601Sandreas.hansson@arm.com        satisfyRequest(pkt, blk);
43811601Sandreas.hansson@arm.com        maintainClusivity(pkt->fromCache(), blk);
43911601Sandreas.hansson@arm.com
44011051Sandreas.hansson@arm.com        return true;
44111051Sandreas.hansson@arm.com    }
44211051Sandreas.hansson@arm.com
44311484Snikos.nikoleris@arm.com    // Can't satisfy access normally... either no block (blk == nullptr)
44411284Sandreas.hansson@arm.com    // or have block but need writable
44511051Sandreas.hansson@arm.com
44611051Sandreas.hansson@arm.com    incMissCount(pkt);
44711051Sandreas.hansson@arm.com
44811484Snikos.nikoleris@arm.com    if (blk == nullptr && pkt->isLLSC() && pkt->isWrite()) {
44911051Sandreas.hansson@arm.com        // complete miss on store conditional... just give up now
45011051Sandreas.hansson@arm.com        pkt->req->setExtraData(0);
45111051Sandreas.hansson@arm.com        return true;
45211051Sandreas.hansson@arm.com    }
45311051Sandreas.hansson@arm.com
45411051Sandreas.hansson@arm.com    return false;
45511051Sandreas.hansson@arm.com}
45611051Sandreas.hansson@arm.com
45711051Sandreas.hansson@arm.comvoid
45811601Sandreas.hansson@arm.comCache::maintainClusivity(bool from_cache, CacheBlk *blk)
45911601Sandreas.hansson@arm.com{
46011601Sandreas.hansson@arm.com    if (from_cache && blk && blk->isValid() && !blk->isDirty() &&
46111601Sandreas.hansson@arm.com        clusivity == Enums::mostly_excl) {
46211601Sandreas.hansson@arm.com        // if we have responded to a cache, and our block is still
46311601Sandreas.hansson@arm.com        // valid, but not dirty, and this cache is mostly exclusive
46411601Sandreas.hansson@arm.com        // with respect to the cache above, drop the block
46511601Sandreas.hansson@arm.com        invalidateBlock(blk);
46611601Sandreas.hansson@arm.com    }
46711601Sandreas.hansson@arm.com}
46811601Sandreas.hansson@arm.com
46911601Sandreas.hansson@arm.comvoid
47011051Sandreas.hansson@arm.comCache::doWritebacks(PacketList& writebacks, Tick forward_time)
47111051Sandreas.hansson@arm.com{
47211051Sandreas.hansson@arm.com    while (!writebacks.empty()) {
47311051Sandreas.hansson@arm.com        PacketPtr wbPkt = writebacks.front();
47411051Sandreas.hansson@arm.com        // We use forwardLatency here because we are copying writebacks to
47511051Sandreas.hansson@arm.com        // write buffer.  Call isCachedAbove for both Writebacks and
47611051Sandreas.hansson@arm.com        // CleanEvicts. If isCachedAbove returns true we set BLOCK_CACHED flag
47711051Sandreas.hansson@arm.com        // in Writebacks and discard CleanEvicts.
47811051Sandreas.hansson@arm.com        if (isCachedAbove(wbPkt)) {
47911051Sandreas.hansson@arm.com            if (wbPkt->cmd == MemCmd::CleanEvict) {
48011051Sandreas.hansson@arm.com                // Delete CleanEvict because cached copies exist above. The
48111051Sandreas.hansson@arm.com                // packet destructor will delete the request object because
48211051Sandreas.hansson@arm.com                // this is a non-snoop request packet which does not require a
48311051Sandreas.hansson@arm.com                // response.
48411051Sandreas.hansson@arm.com                delete wbPkt;
48511199Sandreas.hansson@arm.com            } else if (wbPkt->cmd == MemCmd::WritebackClean) {
48611199Sandreas.hansson@arm.com                // clean writeback, do not send since the block is
48711199Sandreas.hansson@arm.com                // still cached above
48811199Sandreas.hansson@arm.com                assert(writebackClean);
48911199Sandreas.hansson@arm.com                delete wbPkt;
49011051Sandreas.hansson@arm.com            } else {
49111199Sandreas.hansson@arm.com                assert(wbPkt->cmd == MemCmd::WritebackDirty);
49211051Sandreas.hansson@arm.com                // Set BLOCK_CACHED flag in Writeback and send below, so that
49311051Sandreas.hansson@arm.com                // the Writeback does not reset the bit corresponding to this
49411051Sandreas.hansson@arm.com                // address in the snoop filter below.
49511051Sandreas.hansson@arm.com                wbPkt->setBlockCached();
49611051Sandreas.hansson@arm.com                allocateWriteBuffer(wbPkt, forward_time);
49711051Sandreas.hansson@arm.com            }
49811051Sandreas.hansson@arm.com        } else {
49911051Sandreas.hansson@arm.com            // If the block is not cached above, send packet below. Both
50011051Sandreas.hansson@arm.com            // CleanEvict and Writeback with BLOCK_CACHED flag cleared will
50111051Sandreas.hansson@arm.com            // reset the bit corresponding to this address in the snoop filter
50211051Sandreas.hansson@arm.com            // below.
50311051Sandreas.hansson@arm.com            allocateWriteBuffer(wbPkt, forward_time);
50411051Sandreas.hansson@arm.com        }
50511051Sandreas.hansson@arm.com        writebacks.pop_front();
50611051Sandreas.hansson@arm.com    }
50711051Sandreas.hansson@arm.com}
50811051Sandreas.hansson@arm.com
50911130Sali.jafri@arm.comvoid
51011130Sali.jafri@arm.comCache::doWritebacksAtomic(PacketList& writebacks)
51111130Sali.jafri@arm.com{
51211130Sali.jafri@arm.com    while (!writebacks.empty()) {
51311130Sali.jafri@arm.com        PacketPtr wbPkt = writebacks.front();
51411130Sali.jafri@arm.com        // Call isCachedAbove for both Writebacks and CleanEvicts. If
51511130Sali.jafri@arm.com        // isCachedAbove returns true we set BLOCK_CACHED flag in Writebacks
51611130Sali.jafri@arm.com        // and discard CleanEvicts.
51711130Sali.jafri@arm.com        if (isCachedAbove(wbPkt, false)) {
51811199Sandreas.hansson@arm.com            if (wbPkt->cmd == MemCmd::WritebackDirty) {
51911130Sali.jafri@arm.com                // Set BLOCK_CACHED flag in Writeback and send below,
52011130Sali.jafri@arm.com                // so that the Writeback does not reset the bit
52111130Sali.jafri@arm.com                // corresponding to this address in the snoop filter
52211130Sali.jafri@arm.com                // below. We can discard CleanEvicts because cached
52311130Sali.jafri@arm.com                // copies exist above. Atomic mode isCachedAbove
52411130Sali.jafri@arm.com                // modifies packet to set BLOCK_CACHED flag
52511130Sali.jafri@arm.com                memSidePort->sendAtomic(wbPkt);
52611130Sali.jafri@arm.com            }
52711130Sali.jafri@arm.com        } else {
52811130Sali.jafri@arm.com            // If the block is not cached above, send packet below. Both
52911130Sali.jafri@arm.com            // CleanEvict and Writeback with BLOCK_CACHED flag cleared will
53011130Sali.jafri@arm.com            // reset the bit corresponding to this address in the snoop filter
53111130Sali.jafri@arm.com            // below.
53211130Sali.jafri@arm.com            memSidePort->sendAtomic(wbPkt);
53311130Sali.jafri@arm.com        }
53411130Sali.jafri@arm.com        writebacks.pop_front();
53511130Sali.jafri@arm.com        // In case of CleanEvicts, the packet destructor will delete the
53611130Sali.jafri@arm.com        // request object because this is a non-snoop request packet which
53711130Sali.jafri@arm.com        // does not require a response.
53811130Sali.jafri@arm.com        delete wbPkt;
53911130Sali.jafri@arm.com    }
54011130Sali.jafri@arm.com}
54111130Sali.jafri@arm.com
54211051Sandreas.hansson@arm.com
54311051Sandreas.hansson@arm.comvoid
54411051Sandreas.hansson@arm.comCache::recvTimingSnoopResp(PacketPtr pkt)
54511051Sandreas.hansson@arm.com{
54611051Sandreas.hansson@arm.com    DPRINTF(Cache, "%s for %s addr %#llx size %d\n", __func__,
54711051Sandreas.hansson@arm.com            pkt->cmdString(), pkt->getAddr(), pkt->getSize());
54811051Sandreas.hansson@arm.com
54911051Sandreas.hansson@arm.com    assert(pkt->isResponse());
55011051Sandreas.hansson@arm.com    assert(!system->bypassCaches());
55111051Sandreas.hansson@arm.com
55211276Sandreas.hansson@arm.com    // determine if the response is from a snoop request we created
55311276Sandreas.hansson@arm.com    // (in which case it should be in the outstandingSnoop), or if we
55411276Sandreas.hansson@arm.com    // merely forwarded someone else's snoop request
55511276Sandreas.hansson@arm.com    const bool forwardAsSnoop = outstandingSnoop.find(pkt->req) ==
55611276Sandreas.hansson@arm.com        outstandingSnoop.end();
55711276Sandreas.hansson@arm.com
55811276Sandreas.hansson@arm.com    if (!forwardAsSnoop) {
55911276Sandreas.hansson@arm.com        // the packet came from this cache, so sink it here and do not
56011276Sandreas.hansson@arm.com        // forward it
56111051Sandreas.hansson@arm.com        assert(pkt->cmd == MemCmd::HardPFResp);
56211276Sandreas.hansson@arm.com
56311276Sandreas.hansson@arm.com        outstandingSnoop.erase(pkt->req);
56411276Sandreas.hansson@arm.com
56511276Sandreas.hansson@arm.com        DPRINTF(Cache, "Got prefetch response from above for addr "
56611276Sandreas.hansson@arm.com                "%#llx (%s)\n", pkt->getAddr(), pkt->isSecure() ? "s" : "ns");
56711051Sandreas.hansson@arm.com        recvTimingResp(pkt);
56811051Sandreas.hansson@arm.com        return;
56911051Sandreas.hansson@arm.com    }
57011051Sandreas.hansson@arm.com
57111051Sandreas.hansson@arm.com    // forwardLatency is set here because there is a response from an
57211051Sandreas.hansson@arm.com    // upper level cache.
57311051Sandreas.hansson@arm.com    // To pay the delay that occurs if the packet comes from the bus,
57411051Sandreas.hansson@arm.com    // we charge also headerDelay.
57511051Sandreas.hansson@arm.com    Tick snoop_resp_time = clockEdge(forwardLatency) + pkt->headerDelay;
57611051Sandreas.hansson@arm.com    // Reset the timing of the packet.
57711051Sandreas.hansson@arm.com    pkt->headerDelay = pkt->payloadDelay = 0;
57811051Sandreas.hansson@arm.com    memSidePort->schedTimingSnoopResp(pkt, snoop_resp_time);
57911051Sandreas.hansson@arm.com}
58011051Sandreas.hansson@arm.com
58111051Sandreas.hansson@arm.comvoid
58211051Sandreas.hansson@arm.comCache::promoteWholeLineWrites(PacketPtr pkt)
58311051Sandreas.hansson@arm.com{
58411051Sandreas.hansson@arm.com    // Cache line clearing instructions
58511051Sandreas.hansson@arm.com    if (doFastWrites && (pkt->cmd == MemCmd::WriteReq) &&
58611051Sandreas.hansson@arm.com        (pkt->getSize() == blkSize) && (pkt->getOffset(blkSize) == 0)) {
58711051Sandreas.hansson@arm.com        pkt->cmd = MemCmd::WriteLineReq;
58811051Sandreas.hansson@arm.com        DPRINTF(Cache, "packet promoted from Write to WriteLineReq\n");
58911051Sandreas.hansson@arm.com    }
59011051Sandreas.hansson@arm.com}
59111051Sandreas.hansson@arm.com
59211051Sandreas.hansson@arm.combool
59311051Sandreas.hansson@arm.comCache::recvTimingReq(PacketPtr pkt)
59411051Sandreas.hansson@arm.com{
59511051Sandreas.hansson@arm.com    DPRINTF(CacheTags, "%s tags: %s\n", __func__, tags->print());
59611051Sandreas.hansson@arm.com
59711051Sandreas.hansson@arm.com    assert(pkt->isRequest());
59811051Sandreas.hansson@arm.com
59911051Sandreas.hansson@arm.com    // Just forward the packet if caches are disabled.
60011051Sandreas.hansson@arm.com    if (system->bypassCaches()) {
60111051Sandreas.hansson@arm.com        // @todo This should really enqueue the packet rather
60211051Sandreas.hansson@arm.com        bool M5_VAR_USED success = memSidePort->sendTimingReq(pkt);
60311051Sandreas.hansson@arm.com        assert(success);
60411051Sandreas.hansson@arm.com        return true;
60511051Sandreas.hansson@arm.com    }
60611051Sandreas.hansson@arm.com
60711051Sandreas.hansson@arm.com    promoteWholeLineWrites(pkt);
60811051Sandreas.hansson@arm.com
60911284Sandreas.hansson@arm.com    if (pkt->cacheResponding()) {
61011051Sandreas.hansson@arm.com        // a cache above us (but not where the packet came from) is
61111284Sandreas.hansson@arm.com        // responding to the request, in other words it has the line
61211284Sandreas.hansson@arm.com        // in Modified or Owned state
61311284Sandreas.hansson@arm.com        DPRINTF(Cache, "Cache above responding to %#llx (%s): "
61411284Sandreas.hansson@arm.com                "not responding\n",
61511051Sandreas.hansson@arm.com                pkt->getAddr(), pkt->isSecure() ? "s" : "ns");
61611051Sandreas.hansson@arm.com
61711284Sandreas.hansson@arm.com        // if the packet needs the block to be writable, and the cache
61811284Sandreas.hansson@arm.com        // that has promised to respond (setting the cache responding
61911284Sandreas.hansson@arm.com        // flag) is not providing writable (it is in Owned rather than
62011284Sandreas.hansson@arm.com        // the Modified state), we know that there may be other Shared
62111284Sandreas.hansson@arm.com        // copies in the system; go out and invalidate them all
62211334Sandreas.hansson@arm.com        assert(pkt->needsWritable() && !pkt->responderHadWritable());
62311284Sandreas.hansson@arm.com
62411334Sandreas.hansson@arm.com        // an upstream cache that had the line in Owned state
62511334Sandreas.hansson@arm.com        // (dirty, but not writable), is responding and thus
62611334Sandreas.hansson@arm.com        // transferring the dirty line from one branch of the
62711334Sandreas.hansson@arm.com        // cache hierarchy to another
62811284Sandreas.hansson@arm.com
62911334Sandreas.hansson@arm.com        // send out an express snoop and invalidate all other
63011334Sandreas.hansson@arm.com        // copies (snooping a packet that needs writable is the
63111334Sandreas.hansson@arm.com        // same as an invalidation), thus turning the Owned line
63211334Sandreas.hansson@arm.com        // into a Modified line, note that we don't invalidate the
63311334Sandreas.hansson@arm.com        // block in the current cache or any other cache on the
63411334Sandreas.hansson@arm.com        // path to memory
63511051Sandreas.hansson@arm.com
63611334Sandreas.hansson@arm.com        // create a downstream express snoop with cleared packet
63711334Sandreas.hansson@arm.com        // flags, there is no need to allocate any data as the
63811334Sandreas.hansson@arm.com        // packet is merely used to co-ordinate state transitions
63911334Sandreas.hansson@arm.com        Packet *snoop_pkt = new Packet(pkt, true, false);
64011051Sandreas.hansson@arm.com
64111334Sandreas.hansson@arm.com        // also reset the bus time that the original packet has
64211334Sandreas.hansson@arm.com        // not yet paid for
64311334Sandreas.hansson@arm.com        snoop_pkt->headerDelay = snoop_pkt->payloadDelay = 0;
64411051Sandreas.hansson@arm.com
64511334Sandreas.hansson@arm.com        // make this an instantaneous express snoop, and let the
64611334Sandreas.hansson@arm.com        // other caches in the system know that the another cache
64711334Sandreas.hansson@arm.com        // is responding, because we have found the authorative
64811334Sandreas.hansson@arm.com        // copy (Modified or Owned) that will supply the right
64911334Sandreas.hansson@arm.com        // data
65011334Sandreas.hansson@arm.com        snoop_pkt->setExpressSnoop();
65111334Sandreas.hansson@arm.com        snoop_pkt->setCacheResponding();
65211051Sandreas.hansson@arm.com
65311334Sandreas.hansson@arm.com        // this express snoop travels towards the memory, and at
65411334Sandreas.hansson@arm.com        // every crossbar it is snooped upwards thus reaching
65511334Sandreas.hansson@arm.com        // every cache in the system
65611334Sandreas.hansson@arm.com        bool M5_VAR_USED success = memSidePort->sendTimingReq(snoop_pkt);
65711334Sandreas.hansson@arm.com        // express snoops always succeed
65811334Sandreas.hansson@arm.com        assert(success);
65911334Sandreas.hansson@arm.com
66011334Sandreas.hansson@arm.com        // main memory will delete the snoop packet
66111051Sandreas.hansson@arm.com
66211284Sandreas.hansson@arm.com        // queue for deletion, as opposed to immediate deletion, as
66311284Sandreas.hansson@arm.com        // the sending cache is still relying on the packet
66411190Sandreas.hansson@arm.com        pendingDelete.reset(pkt);
66511051Sandreas.hansson@arm.com
66611334Sandreas.hansson@arm.com        // no need to take any further action in this particular cache
66711334Sandreas.hansson@arm.com        // as an upstram cache has already committed to responding,
66811334Sandreas.hansson@arm.com        // and we have already sent out any express snoops in the
66911334Sandreas.hansson@arm.com        // section above to ensure all other copies in the system are
67011334Sandreas.hansson@arm.com        // invalidated
67111051Sandreas.hansson@arm.com        return true;
67211051Sandreas.hansson@arm.com    }
67311051Sandreas.hansson@arm.com
67411051Sandreas.hansson@arm.com    // anything that is merely forwarded pays for the forward latency and
67511051Sandreas.hansson@arm.com    // the delay provided by the crossbar
67611051Sandreas.hansson@arm.com    Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
67711051Sandreas.hansson@arm.com
67811051Sandreas.hansson@arm.com    // We use lookupLatency here because it is used to specify the latency
67911051Sandreas.hansson@arm.com    // to access.
68011051Sandreas.hansson@arm.com    Cycles lat = lookupLatency;
68111484Snikos.nikoleris@arm.com    CacheBlk *blk = nullptr;
68211051Sandreas.hansson@arm.com    bool satisfied = false;
68311051Sandreas.hansson@arm.com    {
68411051Sandreas.hansson@arm.com        PacketList writebacks;
68511051Sandreas.hansson@arm.com        // Note that lat is passed by reference here. The function
68611051Sandreas.hansson@arm.com        // access() calls accessBlock() which can modify lat value.
68711051Sandreas.hansson@arm.com        satisfied = access(pkt, blk, lat, writebacks);
68811051Sandreas.hansson@arm.com
68911051Sandreas.hansson@arm.com        // copy writebacks to write buffer here to ensure they logically
69011051Sandreas.hansson@arm.com        // proceed anything happening below
69111051Sandreas.hansson@arm.com        doWritebacks(writebacks, forward_time);
69211051Sandreas.hansson@arm.com    }
69311051Sandreas.hansson@arm.com
69411051Sandreas.hansson@arm.com    // Here we charge the headerDelay that takes into account the latencies
69511051Sandreas.hansson@arm.com    // of the bus, if the packet comes from it.
69611051Sandreas.hansson@arm.com    // The latency charged it is just lat that is the value of lookupLatency
69711051Sandreas.hansson@arm.com    // modified by access() function, or if not just lookupLatency.
69811051Sandreas.hansson@arm.com    // In case of a hit we are neglecting response latency.
69911051Sandreas.hansson@arm.com    // In case of a miss we are neglecting forward latency.
70011051Sandreas.hansson@arm.com    Tick request_time = clockEdge(lat) + pkt->headerDelay;
70111051Sandreas.hansson@arm.com    // Here we reset the timing of the packet.
70211051Sandreas.hansson@arm.com    pkt->headerDelay = pkt->payloadDelay = 0;
70311051Sandreas.hansson@arm.com
70411051Sandreas.hansson@arm.com    // track time of availability of next prefetch, if any
70511051Sandreas.hansson@arm.com    Tick next_pf_time = MaxTick;
70611051Sandreas.hansson@arm.com
70711051Sandreas.hansson@arm.com    bool needsResponse = pkt->needsResponse();
70811051Sandreas.hansson@arm.com
70911051Sandreas.hansson@arm.com    if (satisfied) {
71011051Sandreas.hansson@arm.com        // should never be satisfying an uncacheable access as we
71111051Sandreas.hansson@arm.com        // flush and invalidate any existing block as part of the
71211051Sandreas.hansson@arm.com        // lookup
71311051Sandreas.hansson@arm.com        assert(!pkt->req->isUncacheable());
71411051Sandreas.hansson@arm.com
71511051Sandreas.hansson@arm.com        // hit (for all other request types)
71611051Sandreas.hansson@arm.com
71711483Snikos.nikoleris@arm.com        if (prefetcher && (prefetchOnAccess ||
71811483Snikos.nikoleris@arm.com                           (blk && blk->wasPrefetched()))) {
71911051Sandreas.hansson@arm.com            if (blk)
72011051Sandreas.hansson@arm.com                blk->status &= ~BlkHWPrefetched;
72111051Sandreas.hansson@arm.com
72211051Sandreas.hansson@arm.com            // Don't notify on SWPrefetch
72311051Sandreas.hansson@arm.com            if (!pkt->cmd.isSWPrefetch())
72411051Sandreas.hansson@arm.com                next_pf_time = prefetcher->notify(pkt);
72511051Sandreas.hansson@arm.com        }
72611051Sandreas.hansson@arm.com
72711051Sandreas.hansson@arm.com        if (needsResponse) {
72811051Sandreas.hansson@arm.com            pkt->makeTimingResponse();
72911051Sandreas.hansson@arm.com            // @todo: Make someone pay for this
73011051Sandreas.hansson@arm.com            pkt->headerDelay = pkt->payloadDelay = 0;
73111051Sandreas.hansson@arm.com
73211051Sandreas.hansson@arm.com            // In this case we are considering request_time that takes
73311051Sandreas.hansson@arm.com            // into account the delay of the xbar, if any, and just
73411051Sandreas.hansson@arm.com            // lat, neglecting responseLatency, modelling hit latency
73511051Sandreas.hansson@arm.com            // just as lookupLatency or or the value of lat overriden
73611051Sandreas.hansson@arm.com            // by access(), that calls accessBlock() function.
73711194Sali.jafri@arm.com            cpuSidePort->schedTimingResp(pkt, request_time, true);
73811051Sandreas.hansson@arm.com        } else {
73911199Sandreas.hansson@arm.com            DPRINTF(Cache, "%s satisfied %s addr %#llx, no response needed\n",
74011558Snikos.nikoleris@arm.com                    __func__, pkt->cmdString(), pkt->getAddr());
74111199Sandreas.hansson@arm.com
74211190Sandreas.hansson@arm.com            // queue the packet for deletion, as the sending cache is
74311190Sandreas.hansson@arm.com            // still relying on it; if the block is found in access(),
74411190Sandreas.hansson@arm.com            // CleanEvict and Writeback messages will be deleted
74511190Sandreas.hansson@arm.com            // here as well
74611190Sandreas.hansson@arm.com            pendingDelete.reset(pkt);
74711051Sandreas.hansson@arm.com        }
74811051Sandreas.hansson@arm.com    } else {
74911051Sandreas.hansson@arm.com        // miss
75011051Sandreas.hansson@arm.com
75111051Sandreas.hansson@arm.com        Addr blk_addr = blockAlign(pkt->getAddr());
75211051Sandreas.hansson@arm.com
75311051Sandreas.hansson@arm.com        // ignore any existing MSHR if we are dealing with an
75411051Sandreas.hansson@arm.com        // uncacheable request
75511051Sandreas.hansson@arm.com        MSHR *mshr = pkt->req->isUncacheable() ? nullptr :
75611051Sandreas.hansson@arm.com            mshrQueue.findMatch(blk_addr, pkt->isSecure());
75711051Sandreas.hansson@arm.com
75811051Sandreas.hansson@arm.com        // Software prefetch handling:
75911051Sandreas.hansson@arm.com        // To keep the core from waiting on data it won't look at
76011051Sandreas.hansson@arm.com        // anyway, send back a response with dummy data. Miss handling
76111051Sandreas.hansson@arm.com        // will continue asynchronously. Unfortunately, the core will
76211051Sandreas.hansson@arm.com        // insist upon freeing original Packet/Request, so we have to
76311051Sandreas.hansson@arm.com        // create a new pair with a different lifecycle. Note that this
76411051Sandreas.hansson@arm.com        // processing happens before any MSHR munging on the behalf of
76511051Sandreas.hansson@arm.com        // this request because this new Request will be the one stored
76611051Sandreas.hansson@arm.com        // into the MSHRs, not the original.
76711051Sandreas.hansson@arm.com        if (pkt->cmd.isSWPrefetch()) {
76811051Sandreas.hansson@arm.com            assert(needsResponse);
76911051Sandreas.hansson@arm.com            assert(pkt->req->hasPaddr());
77011051Sandreas.hansson@arm.com            assert(!pkt->req->isUncacheable());
77111051Sandreas.hansson@arm.com
77211051Sandreas.hansson@arm.com            // There's no reason to add a prefetch as an additional target
77311051Sandreas.hansson@arm.com            // to an existing MSHR. If an outstanding request is already
77411051Sandreas.hansson@arm.com            // in progress, there is nothing for the prefetch to do.
77511051Sandreas.hansson@arm.com            // If this is the case, we don't even create a request at all.
77611051Sandreas.hansson@arm.com            PacketPtr pf = nullptr;
77711051Sandreas.hansson@arm.com
77811051Sandreas.hansson@arm.com            if (!mshr) {
77911051Sandreas.hansson@arm.com                // copy the request and create a new SoftPFReq packet
78011051Sandreas.hansson@arm.com                RequestPtr req = new Request(pkt->req->getPaddr(),
78111051Sandreas.hansson@arm.com                                             pkt->req->getSize(),
78211051Sandreas.hansson@arm.com                                             pkt->req->getFlags(),
78311051Sandreas.hansson@arm.com                                             pkt->req->masterId());
78411051Sandreas.hansson@arm.com                pf = new Packet(req, pkt->cmd);
78511051Sandreas.hansson@arm.com                pf->allocate();
78611051Sandreas.hansson@arm.com                assert(pf->getAddr() == pkt->getAddr());
78711051Sandreas.hansson@arm.com                assert(pf->getSize() == pkt->getSize());
78811051Sandreas.hansson@arm.com            }
78911051Sandreas.hansson@arm.com
79011051Sandreas.hansson@arm.com            pkt->makeTimingResponse();
79111286Sandreas.hansson@arm.com
79211051Sandreas.hansson@arm.com            // request_time is used here, taking into account lat and the delay
79311051Sandreas.hansson@arm.com            // charged if the packet comes from the xbar.
79411194Sali.jafri@arm.com            cpuSidePort->schedTimingResp(pkt, request_time, true);
79511051Sandreas.hansson@arm.com
79611051Sandreas.hansson@arm.com            // If an outstanding request is in progress (we found an
79711051Sandreas.hansson@arm.com            // MSHR) this is set to null
79811051Sandreas.hansson@arm.com            pkt = pf;
79911051Sandreas.hansson@arm.com        }
80011051Sandreas.hansson@arm.com
80111051Sandreas.hansson@arm.com        if (mshr) {
80211051Sandreas.hansson@arm.com            /// MSHR hit
80311051Sandreas.hansson@arm.com            /// @note writebacks will be checked in getNextMSHR()
80411051Sandreas.hansson@arm.com            /// for any conflicting requests to the same block
80511051Sandreas.hansson@arm.com
80611051Sandreas.hansson@arm.com            //@todo remove hw_pf here
80711051Sandreas.hansson@arm.com
80811051Sandreas.hansson@arm.com            // Coalesce unless it was a software prefetch (see above).
80911051Sandreas.hansson@arm.com            if (pkt) {
81011199Sandreas.hansson@arm.com                assert(!pkt->isWriteback());
81111199Sandreas.hansson@arm.com                // CleanEvicts corresponding to blocks which have
81211199Sandreas.hansson@arm.com                // outstanding requests in MSHRs are simply sunk here
81311051Sandreas.hansson@arm.com                if (pkt->cmd == MemCmd::CleanEvict) {
81411190Sandreas.hansson@arm.com                    pendingDelete.reset(pkt);
81511051Sandreas.hansson@arm.com                } else {
81611483Snikos.nikoleris@arm.com                    DPRINTF(Cache, "%s coalescing MSHR for %s addr %#llx "
81711483Snikos.nikoleris@arm.com                            "size %d\n", __func__, pkt->cmdString(),
81811483Snikos.nikoleris@arm.com                            pkt->getAddr(), pkt->getSize());
81911051Sandreas.hansson@arm.com
82011051Sandreas.hansson@arm.com                    assert(pkt->req->masterId() < system->maxMasters());
82111051Sandreas.hansson@arm.com                    mshr_hits[pkt->cmdToIndex()][pkt->req->masterId()]++;
82211051Sandreas.hansson@arm.com                    // We use forward_time here because it is the same
82311051Sandreas.hansson@arm.com                    // considering new targets. We have multiple
82411051Sandreas.hansson@arm.com                    // requests for the same address here. It
82511051Sandreas.hansson@arm.com                    // specifies the latency to allocate an internal
82611051Sandreas.hansson@arm.com                    // buffer and to schedule an event to the queued
82711051Sandreas.hansson@arm.com                    // port and also takes into account the additional
82811051Sandreas.hansson@arm.com                    // delay of the xbar.
82911197Sandreas.hansson@arm.com                    mshr->allocateTarget(pkt, forward_time, order++,
83011197Sandreas.hansson@arm.com                                         allocOnFill(pkt->cmd));
83111051Sandreas.hansson@arm.com                    if (mshr->getNumTargets() == numTarget) {
83211051Sandreas.hansson@arm.com                        noTargetMSHR = mshr;
83311051Sandreas.hansson@arm.com                        setBlocked(Blocked_NoTargets);
83411051Sandreas.hansson@arm.com                        // need to be careful with this... if this mshr isn't
83511051Sandreas.hansson@arm.com                        // ready yet (i.e. time > curTick()), we don't want to
83611051Sandreas.hansson@arm.com                        // move it ahead of mshrs that are ready
83711051Sandreas.hansson@arm.com                        // mshrQueue.moveToFront(mshr);
83811051Sandreas.hansson@arm.com                    }
83911051Sandreas.hansson@arm.com                }
84011051Sandreas.hansson@arm.com                // We should call the prefetcher reguardless if the request is
84111483Snikos.nikoleris@arm.com                // satisfied or not, reguardless if the request is in the MSHR
84211483Snikos.nikoleris@arm.com                // or not.  The request could be a ReadReq hit, but still not
84311051Sandreas.hansson@arm.com                // satisfied (potentially because of a prior write to the same
84411051Sandreas.hansson@arm.com                // cache line.  So, even when not satisfied, tehre is an MSHR
84511483Snikos.nikoleris@arm.com                // already allocated for this, we need to let the prefetcher
84611483Snikos.nikoleris@arm.com                // know about the request
84711051Sandreas.hansson@arm.com                if (prefetcher) {
84811051Sandreas.hansson@arm.com                    // Don't notify on SWPrefetch
84911051Sandreas.hansson@arm.com                    if (!pkt->cmd.isSWPrefetch())
85011051Sandreas.hansson@arm.com                        next_pf_time = prefetcher->notify(pkt);
85111051Sandreas.hansson@arm.com                }
85211051Sandreas.hansson@arm.com            }
85311051Sandreas.hansson@arm.com        } else {
85411051Sandreas.hansson@arm.com            // no MSHR
85511051Sandreas.hansson@arm.com            assert(pkt->req->masterId() < system->maxMasters());
85611051Sandreas.hansson@arm.com            if (pkt->req->isUncacheable()) {
85711051Sandreas.hansson@arm.com                mshr_uncacheable[pkt->cmdToIndex()][pkt->req->masterId()]++;
85811051Sandreas.hansson@arm.com            } else {
85911051Sandreas.hansson@arm.com                mshr_misses[pkt->cmdToIndex()][pkt->req->masterId()]++;
86011051Sandreas.hansson@arm.com            }
86111051Sandreas.hansson@arm.com
86211199Sandreas.hansson@arm.com            if (pkt->isEviction() ||
86311051Sandreas.hansson@arm.com                (pkt->req->isUncacheable() && pkt->isWrite())) {
86411051Sandreas.hansson@arm.com                // We use forward_time here because there is an
86511051Sandreas.hansson@arm.com                // uncached memory write, forwarded to WriteBuffer.
86611051Sandreas.hansson@arm.com                allocateWriteBuffer(pkt, forward_time);
86711051Sandreas.hansson@arm.com            } else {
86811051Sandreas.hansson@arm.com                if (blk && blk->isValid()) {
86911051Sandreas.hansson@arm.com                    // should have flushed and have no valid block
87011051Sandreas.hansson@arm.com                    assert(!pkt->req->isUncacheable());
87111051Sandreas.hansson@arm.com
87211051Sandreas.hansson@arm.com                    // If we have a write miss to a valid block, we
87311051Sandreas.hansson@arm.com                    // need to mark the block non-readable.  Otherwise
87411051Sandreas.hansson@arm.com                    // if we allow reads while there's an outstanding
87511051Sandreas.hansson@arm.com                    // write miss, the read could return stale data
87611051Sandreas.hansson@arm.com                    // out of the cache block... a more aggressive
87711051Sandreas.hansson@arm.com                    // system could detect the overlap (if any) and
87811051Sandreas.hansson@arm.com                    // forward data out of the MSHRs, but we don't do
87911051Sandreas.hansson@arm.com                    // that yet.  Note that we do need to leave the
88011051Sandreas.hansson@arm.com                    // block valid so that it stays in the cache, in
88111051Sandreas.hansson@arm.com                    // case we get an upgrade response (and hence no
88211051Sandreas.hansson@arm.com                    // new data) when the write miss completes.
88311051Sandreas.hansson@arm.com                    // As long as CPUs do proper store/load forwarding
88411051Sandreas.hansson@arm.com                    // internally, and have a sufficiently weak memory
88511051Sandreas.hansson@arm.com                    // model, this is probably unnecessary, but at some
88611051Sandreas.hansson@arm.com                    // point it must have seemed like we needed it...
88711284Sandreas.hansson@arm.com                    assert(pkt->needsWritable());
88811051Sandreas.hansson@arm.com                    assert(!blk->isWritable());
88911051Sandreas.hansson@arm.com                    blk->status &= ~BlkReadable;
89011051Sandreas.hansson@arm.com                }
89111051Sandreas.hansson@arm.com                // Here we are using forward_time, modelling the latency of
89211051Sandreas.hansson@arm.com                // a miss (outbound) just as forwardLatency, neglecting the
89311051Sandreas.hansson@arm.com                // lookupLatency component.
89411051Sandreas.hansson@arm.com                allocateMissBuffer(pkt, forward_time);
89511051Sandreas.hansson@arm.com            }
89611051Sandreas.hansson@arm.com
89711051Sandreas.hansson@arm.com            if (prefetcher) {
89811051Sandreas.hansson@arm.com                // Don't notify on SWPrefetch
89911051Sandreas.hansson@arm.com                if (!pkt->cmd.isSWPrefetch())
90011051Sandreas.hansson@arm.com                    next_pf_time = prefetcher->notify(pkt);
90111051Sandreas.hansson@arm.com            }
90211051Sandreas.hansson@arm.com        }
90311051Sandreas.hansson@arm.com    }
90411051Sandreas.hansson@arm.com
90511051Sandreas.hansson@arm.com    if (next_pf_time != MaxTick)
90611051Sandreas.hansson@arm.com        schedMemSideSendEvent(next_pf_time);
90711051Sandreas.hansson@arm.com
90811051Sandreas.hansson@arm.com    return true;
90911051Sandreas.hansson@arm.com}
91011051Sandreas.hansson@arm.com
91111051Sandreas.hansson@arm.comPacketPtr
91211452Sandreas.hansson@arm.comCache::createMissPacket(PacketPtr cpu_pkt, CacheBlk *blk,
91311452Sandreas.hansson@arm.com                        bool needsWritable) const
91411051Sandreas.hansson@arm.com{
91511452Sandreas.hansson@arm.com    // should never see evictions here
91611452Sandreas.hansson@arm.com    assert(!cpu_pkt->isEviction());
91711452Sandreas.hansson@arm.com
91811051Sandreas.hansson@arm.com    bool blkValid = blk && blk->isValid();
91911051Sandreas.hansson@arm.com
92011452Sandreas.hansson@arm.com    if (cpu_pkt->req->isUncacheable() ||
92111452Sandreas.hansson@arm.com        (!blkValid && cpu_pkt->isUpgrade())) {
92211452Sandreas.hansson@arm.com        // uncacheable requests and upgrades from upper-level caches
92311452Sandreas.hansson@arm.com        // that missed completely just go through as is
92411452Sandreas.hansson@arm.com        return nullptr;
92511051Sandreas.hansson@arm.com    }
92611051Sandreas.hansson@arm.com
92711051Sandreas.hansson@arm.com    assert(cpu_pkt->needsResponse());
92811051Sandreas.hansson@arm.com
92911051Sandreas.hansson@arm.com    MemCmd cmd;
93011051Sandreas.hansson@arm.com    // @TODO make useUpgrades a parameter.
93111051Sandreas.hansson@arm.com    // Note that ownership protocols require upgrade, otherwise a
93211051Sandreas.hansson@arm.com    // write miss on a shared owned block will generate a ReadExcl,
93311051Sandreas.hansson@arm.com    // which will clobber the owned copy.
93411051Sandreas.hansson@arm.com    const bool useUpgrades = true;
93511051Sandreas.hansson@arm.com    if (blkValid && useUpgrades) {
93611284Sandreas.hansson@arm.com        // only reason to be here is that blk is read only and we need
93711284Sandreas.hansson@arm.com        // it to be writable
93811284Sandreas.hansson@arm.com        assert(needsWritable);
93911051Sandreas.hansson@arm.com        assert(!blk->isWritable());
94011051Sandreas.hansson@arm.com        cmd = cpu_pkt->isLLSC() ? MemCmd::SCUpgradeReq : MemCmd::UpgradeReq;
94111051Sandreas.hansson@arm.com    } else if (cpu_pkt->cmd == MemCmd::SCUpgradeFailReq ||
94211051Sandreas.hansson@arm.com               cpu_pkt->cmd == MemCmd::StoreCondFailReq) {
94311051Sandreas.hansson@arm.com        // Even though this SC will fail, we still need to send out the
94411051Sandreas.hansson@arm.com        // request and get the data to supply it to other snoopers in the case
94511051Sandreas.hansson@arm.com        // where the determination the StoreCond fails is delayed due to
94611051Sandreas.hansson@arm.com        // all caches not being on the same local bus.
94711051Sandreas.hansson@arm.com        cmd = MemCmd::SCUpgradeFailReq;
94811352Sandreas.hansson@arm.com    } else if (cpu_pkt->cmd == MemCmd::WriteLineReq ||
94911352Sandreas.hansson@arm.com               cpu_pkt->cmd == MemCmd::InvalidateReq) {
95011051Sandreas.hansson@arm.com        // forward as invalidate to all other caches, this gives us
95111284Sandreas.hansson@arm.com        // the line in Exclusive state, and invalidates all other
95211051Sandreas.hansson@arm.com        // copies
95311051Sandreas.hansson@arm.com        cmd = MemCmd::InvalidateReq;
95411051Sandreas.hansson@arm.com    } else {
95511051Sandreas.hansson@arm.com        // block is invalid
95611284Sandreas.hansson@arm.com        cmd = needsWritable ? MemCmd::ReadExReq :
95711051Sandreas.hansson@arm.com            (isReadOnly ? MemCmd::ReadCleanReq : MemCmd::ReadSharedReq);
95811051Sandreas.hansson@arm.com    }
95911051Sandreas.hansson@arm.com    PacketPtr pkt = new Packet(cpu_pkt->req, cmd, blkSize);
96011051Sandreas.hansson@arm.com
96111284Sandreas.hansson@arm.com    // if there are upstream caches that have already marked the
96211284Sandreas.hansson@arm.com    // packet as having sharers (not passing writable), pass that info
96311284Sandreas.hansson@arm.com    // downstream
96411602Sandreas.hansson@arm.com    if (cpu_pkt->hasSharers() && !needsWritable) {
96511051Sandreas.hansson@arm.com        // note that cpu_pkt may have spent a considerable time in the
96611051Sandreas.hansson@arm.com        // MSHR queue and that the information could possibly be out
96711051Sandreas.hansson@arm.com        // of date, however, there is no harm in conservatively
96811284Sandreas.hansson@arm.com        // assuming the block has sharers
96911284Sandreas.hansson@arm.com        pkt->setHasSharers();
97011284Sandreas.hansson@arm.com        DPRINTF(Cache, "%s passing hasSharers from %s to %s addr %#llx "
97111284Sandreas.hansson@arm.com                "size %d\n",
97211051Sandreas.hansson@arm.com                __func__, cpu_pkt->cmdString(), pkt->cmdString(),
97311051Sandreas.hansson@arm.com                pkt->getAddr(), pkt->getSize());
97411051Sandreas.hansson@arm.com    }
97511051Sandreas.hansson@arm.com
97611051Sandreas.hansson@arm.com    // the packet should be block aligned
97711051Sandreas.hansson@arm.com    assert(pkt->getAddr() == blockAlign(pkt->getAddr()));
97811051Sandreas.hansson@arm.com
97911051Sandreas.hansson@arm.com    pkt->allocate();
98011051Sandreas.hansson@arm.com    DPRINTF(Cache, "%s created %s from %s for  addr %#llx size %d\n",
98111051Sandreas.hansson@arm.com            __func__, pkt->cmdString(), cpu_pkt->cmdString(), pkt->getAddr(),
98211051Sandreas.hansson@arm.com            pkt->getSize());
98311051Sandreas.hansson@arm.com    return pkt;
98411051Sandreas.hansson@arm.com}
98511051Sandreas.hansson@arm.com
98611051Sandreas.hansson@arm.com
98711051Sandreas.hansson@arm.comTick
98811051Sandreas.hansson@arm.comCache::recvAtomic(PacketPtr pkt)
98911051Sandreas.hansson@arm.com{
99011051Sandreas.hansson@arm.com    // We are in atomic mode so we pay just for lookupLatency here.
99111051Sandreas.hansson@arm.com    Cycles lat = lookupLatency;
99211051Sandreas.hansson@arm.com
99311051Sandreas.hansson@arm.com    // Forward the request if the system is in cache bypass mode.
99411051Sandreas.hansson@arm.com    if (system->bypassCaches())
99511051Sandreas.hansson@arm.com        return ticksToCycles(memSidePort->sendAtomic(pkt));
99611051Sandreas.hansson@arm.com
99711051Sandreas.hansson@arm.com    promoteWholeLineWrites(pkt);
99811051Sandreas.hansson@arm.com
99911333Sandreas.hansson@arm.com    // follow the same flow as in recvTimingReq, and check if a cache
100011333Sandreas.hansson@arm.com    // above us is responding
100111284Sandreas.hansson@arm.com    if (pkt->cacheResponding()) {
100211333Sandreas.hansson@arm.com        DPRINTF(Cache, "Cache above responding to %#llx (%s): "
100311333Sandreas.hansson@arm.com                "not responding\n",
100411333Sandreas.hansson@arm.com                pkt->getAddr(), pkt->isSecure() ? "s" : "ns");
100511333Sandreas.hansson@arm.com
100611333Sandreas.hansson@arm.com        // if a cache is responding, and it had the line in Owned
100711333Sandreas.hansson@arm.com        // rather than Modified state, we need to invalidate any
100811333Sandreas.hansson@arm.com        // copies that are not on the same path to memory
100911334Sandreas.hansson@arm.com        assert(pkt->needsWritable() && !pkt->responderHadWritable());
101011334Sandreas.hansson@arm.com        lat += ticksToCycles(memSidePort->sendAtomic(pkt));
101111051Sandreas.hansson@arm.com
101211051Sandreas.hansson@arm.com        return lat * clockPeriod();
101311051Sandreas.hansson@arm.com    }
101411051Sandreas.hansson@arm.com
101511051Sandreas.hansson@arm.com    // should assert here that there are no outstanding MSHRs or
101611051Sandreas.hansson@arm.com    // writebacks... that would mean that someone used an atomic
101711051Sandreas.hansson@arm.com    // access in timing mode
101811051Sandreas.hansson@arm.com
101911484Snikos.nikoleris@arm.com    CacheBlk *blk = nullptr;
102011051Sandreas.hansson@arm.com    PacketList writebacks;
102111051Sandreas.hansson@arm.com    bool satisfied = access(pkt, blk, lat, writebacks);
102211051Sandreas.hansson@arm.com
102311051Sandreas.hansson@arm.com    // handle writebacks resulting from the access here to ensure they
102411051Sandreas.hansson@arm.com    // logically proceed anything happening below
102511130Sali.jafri@arm.com    doWritebacksAtomic(writebacks);
102611051Sandreas.hansson@arm.com
102711051Sandreas.hansson@arm.com    if (!satisfied) {
102811051Sandreas.hansson@arm.com        // MISS
102911051Sandreas.hansson@arm.com
103011452Sandreas.hansson@arm.com        // deal with the packets that go through the write path of
103111452Sandreas.hansson@arm.com        // the cache, i.e. any evictions and uncacheable writes
103211452Sandreas.hansson@arm.com        if (pkt->isEviction() ||
103311452Sandreas.hansson@arm.com            (pkt->req->isUncacheable() && pkt->isWrite())) {
103411452Sandreas.hansson@arm.com            lat += ticksToCycles(memSidePort->sendAtomic(pkt));
103511452Sandreas.hansson@arm.com            return lat * clockPeriod();
103611452Sandreas.hansson@arm.com        }
103711452Sandreas.hansson@arm.com        // only misses left
103811452Sandreas.hansson@arm.com
103911452Sandreas.hansson@arm.com        PacketPtr bus_pkt = createMissPacket(pkt, blk, pkt->needsWritable());
104011051Sandreas.hansson@arm.com
104111484Snikos.nikoleris@arm.com        bool is_forward = (bus_pkt == nullptr);
104211051Sandreas.hansson@arm.com
104311051Sandreas.hansson@arm.com        if (is_forward) {
104411051Sandreas.hansson@arm.com            // just forwarding the same request to the next level
104511051Sandreas.hansson@arm.com            // no local cache operation involved
104611051Sandreas.hansson@arm.com            bus_pkt = pkt;
104711051Sandreas.hansson@arm.com        }
104811051Sandreas.hansson@arm.com
104911051Sandreas.hansson@arm.com        DPRINTF(Cache, "Sending an atomic %s for %#llx (%s)\n",
105011051Sandreas.hansson@arm.com                bus_pkt->cmdString(), bus_pkt->getAddr(),
105111051Sandreas.hansson@arm.com                bus_pkt->isSecure() ? "s" : "ns");
105211051Sandreas.hansson@arm.com
105311051Sandreas.hansson@arm.com#if TRACING_ON
105411051Sandreas.hansson@arm.com        CacheBlk::State old_state = blk ? blk->status : 0;
105511051Sandreas.hansson@arm.com#endif
105611051Sandreas.hansson@arm.com
105711051Sandreas.hansson@arm.com        lat += ticksToCycles(memSidePort->sendAtomic(bus_pkt));
105811051Sandreas.hansson@arm.com
105911452Sandreas.hansson@arm.com        bool is_invalidate = bus_pkt->isInvalidate();
106011452Sandreas.hansson@arm.com
106111051Sandreas.hansson@arm.com        // We are now dealing with the response handling
106211483Snikos.nikoleris@arm.com        DPRINTF(Cache, "Receive response: %s for addr %#llx (%s) in "
106311483Snikos.nikoleris@arm.com                "state %i\n", bus_pkt->cmdString(), bus_pkt->getAddr(),
106411051Sandreas.hansson@arm.com                bus_pkt->isSecure() ? "s" : "ns",
106511051Sandreas.hansson@arm.com                old_state);
106611051Sandreas.hansson@arm.com
106711051Sandreas.hansson@arm.com        // If packet was a forward, the response (if any) is already
106811051Sandreas.hansson@arm.com        // in place in the bus_pkt == pkt structure, so we don't need
106911051Sandreas.hansson@arm.com        // to do anything.  Otherwise, use the separate bus_pkt to
107011051Sandreas.hansson@arm.com        // generate response to pkt and then delete it.
107111051Sandreas.hansson@arm.com        if (!is_forward) {
107211051Sandreas.hansson@arm.com            if (pkt->needsResponse()) {
107311051Sandreas.hansson@arm.com                assert(bus_pkt->isResponse());
107411051Sandreas.hansson@arm.com                if (bus_pkt->isError()) {
107511051Sandreas.hansson@arm.com                    pkt->makeAtomicResponse();
107611051Sandreas.hansson@arm.com                    pkt->copyError(bus_pkt);
107711051Sandreas.hansson@arm.com                } else if (pkt->cmd == MemCmd::WriteLineReq) {
107811051Sandreas.hansson@arm.com                    // note the use of pkt, not bus_pkt here.
107911051Sandreas.hansson@arm.com
108011051Sandreas.hansson@arm.com                    // write-line request to the cache that promoted
108111051Sandreas.hansson@arm.com                    // the write to a whole line
108211197Sandreas.hansson@arm.com                    blk = handleFill(pkt, blk, writebacks,
108311197Sandreas.hansson@arm.com                                     allocOnFill(pkt->cmd));
108411452Sandreas.hansson@arm.com                    assert(blk != NULL);
108511452Sandreas.hansson@arm.com                    is_invalidate = false;
108611601Sandreas.hansson@arm.com                    satisfyRequest(pkt, blk);
108711051Sandreas.hansson@arm.com                } else if (bus_pkt->isRead() ||
108811051Sandreas.hansson@arm.com                           bus_pkt->cmd == MemCmd::UpgradeResp) {
108911051Sandreas.hansson@arm.com                    // we're updating cache state to allow us to
109011051Sandreas.hansson@arm.com                    // satisfy the upstream request from the cache
109111197Sandreas.hansson@arm.com                    blk = handleFill(bus_pkt, blk, writebacks,
109211197Sandreas.hansson@arm.com                                     allocOnFill(pkt->cmd));
109311601Sandreas.hansson@arm.com                    satisfyRequest(pkt, blk);
109411601Sandreas.hansson@arm.com                    maintainClusivity(pkt->fromCache(), blk);
109511051Sandreas.hansson@arm.com                } else {
109611051Sandreas.hansson@arm.com                    // we're satisfying the upstream request without
109711051Sandreas.hansson@arm.com                    // modifying cache state, e.g., a write-through
109811051Sandreas.hansson@arm.com                    pkt->makeAtomicResponse();
109911051Sandreas.hansson@arm.com                }
110011051Sandreas.hansson@arm.com            }
110111051Sandreas.hansson@arm.com            delete bus_pkt;
110211051Sandreas.hansson@arm.com        }
110311452Sandreas.hansson@arm.com
110411452Sandreas.hansson@arm.com        if (is_invalidate && blk && blk->isValid()) {
110511452Sandreas.hansson@arm.com            invalidateBlock(blk);
110611452Sandreas.hansson@arm.com        }
110711051Sandreas.hansson@arm.com    }
110811051Sandreas.hansson@arm.com
110911051Sandreas.hansson@arm.com    // Note that we don't invoke the prefetcher at all in atomic mode.
111011051Sandreas.hansson@arm.com    // It's not clear how to do it properly, particularly for
111111051Sandreas.hansson@arm.com    // prefetchers that aggressively generate prefetch candidates and
111211051Sandreas.hansson@arm.com    // rely on bandwidth contention to throttle them; these will tend
111311051Sandreas.hansson@arm.com    // to pollute the cache in atomic mode since there is no bandwidth
111411051Sandreas.hansson@arm.com    // contention.  If we ever do want to enable prefetching in atomic
111511051Sandreas.hansson@arm.com    // mode, though, this is the place to do it... see timingAccess()
111611051Sandreas.hansson@arm.com    // for an example (though we'd want to issue the prefetch(es)
111711051Sandreas.hansson@arm.com    // immediately rather than calling requestMemSideBus() as we do
111811051Sandreas.hansson@arm.com    // there).
111911051Sandreas.hansson@arm.com
112011197Sandreas.hansson@arm.com    // do any writebacks resulting from the response handling
112111130Sali.jafri@arm.com    doWritebacksAtomic(writebacks);
112211051Sandreas.hansson@arm.com
112311197Sandreas.hansson@arm.com    // if we used temp block, check to see if its valid and if so
112411197Sandreas.hansson@arm.com    // clear it out, but only do so after the call to recvAtomic is
112511197Sandreas.hansson@arm.com    // finished so that any downstream observers (such as a snoop
112611197Sandreas.hansson@arm.com    // filter), first see the fill, and only then see the eviction
112711197Sandreas.hansson@arm.com    if (blk == tempBlock && tempBlock->isValid()) {
112811197Sandreas.hansson@arm.com        // the atomic CPU calls recvAtomic for fetch and load/store
112911197Sandreas.hansson@arm.com        // sequentuially, and we may already have a tempBlock
113011197Sandreas.hansson@arm.com        // writeback from the fetch that we have not yet sent
113111197Sandreas.hansson@arm.com        if (tempBlockWriteback) {
113211197Sandreas.hansson@arm.com            // if that is the case, write the prevoius one back, and
113311197Sandreas.hansson@arm.com            // do not schedule any new event
113411197Sandreas.hansson@arm.com            writebackTempBlockAtomic();
113511197Sandreas.hansson@arm.com        } else {
113611197Sandreas.hansson@arm.com            // the writeback/clean eviction happens after the call to
113711197Sandreas.hansson@arm.com            // recvAtomic has finished (but before any successive
113811197Sandreas.hansson@arm.com            // calls), so that the response handling from the fill is
113911197Sandreas.hansson@arm.com            // allowed to happen first
114011197Sandreas.hansson@arm.com            schedule(writebackTempBlockAtomicEvent, curTick());
114111197Sandreas.hansson@arm.com        }
114211197Sandreas.hansson@arm.com
114311199Sandreas.hansson@arm.com        tempBlockWriteback = (blk->isDirty() || writebackClean) ?
114411199Sandreas.hansson@arm.com            writebackBlk(blk) : cleanEvictBlk(blk);
114511197Sandreas.hansson@arm.com        blk->invalidate();
114611197Sandreas.hansson@arm.com    }
114711197Sandreas.hansson@arm.com
114811051Sandreas.hansson@arm.com    if (pkt->needsResponse()) {
114911051Sandreas.hansson@arm.com        pkt->makeAtomicResponse();
115011051Sandreas.hansson@arm.com    }
115111051Sandreas.hansson@arm.com
115211051Sandreas.hansson@arm.com    return lat * clockPeriod();
115311051Sandreas.hansson@arm.com}
115411051Sandreas.hansson@arm.com
115511051Sandreas.hansson@arm.com
115611051Sandreas.hansson@arm.comvoid
115711051Sandreas.hansson@arm.comCache::functionalAccess(PacketPtr pkt, bool fromCpuSide)
115811051Sandreas.hansson@arm.com{
115911051Sandreas.hansson@arm.com    if (system->bypassCaches()) {
116011051Sandreas.hansson@arm.com        // Packets from the memory side are snoop request and
116111051Sandreas.hansson@arm.com        // shouldn't happen in bypass mode.
116211051Sandreas.hansson@arm.com        assert(fromCpuSide);
116311051Sandreas.hansson@arm.com
116411051Sandreas.hansson@arm.com        // The cache should be flushed if we are in cache bypass mode,
116511051Sandreas.hansson@arm.com        // so we don't need to check if we need to update anything.
116611051Sandreas.hansson@arm.com        memSidePort->sendFunctional(pkt);
116711051Sandreas.hansson@arm.com        return;
116811051Sandreas.hansson@arm.com    }
116911051Sandreas.hansson@arm.com
117011051Sandreas.hansson@arm.com    Addr blk_addr = blockAlign(pkt->getAddr());
117111051Sandreas.hansson@arm.com    bool is_secure = pkt->isSecure();
117211051Sandreas.hansson@arm.com    CacheBlk *blk = tags->findBlock(pkt->getAddr(), is_secure);
117311051Sandreas.hansson@arm.com    MSHR *mshr = mshrQueue.findMatch(blk_addr, is_secure);
117411051Sandreas.hansson@arm.com
117511051Sandreas.hansson@arm.com    pkt->pushLabel(name());
117611051Sandreas.hansson@arm.com
117711051Sandreas.hansson@arm.com    CacheBlkPrintWrapper cbpw(blk);
117811051Sandreas.hansson@arm.com
117911051Sandreas.hansson@arm.com    // Note that just because an L2/L3 has valid data doesn't mean an
118011051Sandreas.hansson@arm.com    // L1 doesn't have a more up-to-date modified copy that still
118111051Sandreas.hansson@arm.com    // needs to be found.  As a result we always update the request if
118211051Sandreas.hansson@arm.com    // we have it, but only declare it satisfied if we are the owner.
118311051Sandreas.hansson@arm.com
118411051Sandreas.hansson@arm.com    // see if we have data at all (owned or otherwise)
118511051Sandreas.hansson@arm.com    bool have_data = blk && blk->isValid()
118611051Sandreas.hansson@arm.com        && pkt->checkFunctional(&cbpw, blk_addr, is_secure, blkSize,
118711051Sandreas.hansson@arm.com                                blk->data);
118811051Sandreas.hansson@arm.com
118911284Sandreas.hansson@arm.com    // data we have is dirty if marked as such or if we have an
119011284Sandreas.hansson@arm.com    // in-service MSHR that is pending a modified line
119111051Sandreas.hansson@arm.com    bool have_dirty =
119211051Sandreas.hansson@arm.com        have_data && (blk->isDirty() ||
119311284Sandreas.hansson@arm.com                      (mshr && mshr->inService && mshr->isPendingModified()));
119411051Sandreas.hansson@arm.com
119511051Sandreas.hansson@arm.com    bool done = have_dirty
119611051Sandreas.hansson@arm.com        || cpuSidePort->checkFunctional(pkt)
119711051Sandreas.hansson@arm.com        || mshrQueue.checkFunctional(pkt, blk_addr)
119811051Sandreas.hansson@arm.com        || writeBuffer.checkFunctional(pkt, blk_addr)
119911051Sandreas.hansson@arm.com        || memSidePort->checkFunctional(pkt);
120011051Sandreas.hansson@arm.com
120111288Ssteve.reinhardt@amd.com    DPRINTF(CacheVerbose, "functional %s %#llx (%s) %s%s%s\n",
120211051Sandreas.hansson@arm.com            pkt->cmdString(), pkt->getAddr(), is_secure ? "s" : "ns",
120311051Sandreas.hansson@arm.com            (blk && blk->isValid()) ? "valid " : "",
120411051Sandreas.hansson@arm.com            have_data ? "data " : "", done ? "done " : "");
120511051Sandreas.hansson@arm.com
120611051Sandreas.hansson@arm.com    // We're leaving the cache, so pop cache->name() label
120711051Sandreas.hansson@arm.com    pkt->popLabel();
120811051Sandreas.hansson@arm.com
120911051Sandreas.hansson@arm.com    if (done) {
121011051Sandreas.hansson@arm.com        pkt->makeResponse();
121111051Sandreas.hansson@arm.com    } else {
121211051Sandreas.hansson@arm.com        // if it came as a request from the CPU side then make sure it
121311051Sandreas.hansson@arm.com        // continues towards the memory side
121411051Sandreas.hansson@arm.com        if (fromCpuSide) {
121511051Sandreas.hansson@arm.com            memSidePort->sendFunctional(pkt);
121611485Snikos.nikoleris@arm.com        } else if (cpuSidePort->isSnooping()) {
121711051Sandreas.hansson@arm.com            // if it came from the memory side, it must be a snoop request
121811051Sandreas.hansson@arm.com            // and we should only forward it if we are forwarding snoops
121911051Sandreas.hansson@arm.com            cpuSidePort->sendFunctionalSnoop(pkt);
122011051Sandreas.hansson@arm.com        }
122111051Sandreas.hansson@arm.com    }
122211051Sandreas.hansson@arm.com}
122311051Sandreas.hansson@arm.com
122411051Sandreas.hansson@arm.com
122511051Sandreas.hansson@arm.com/////////////////////////////////////////////////////
122611051Sandreas.hansson@arm.com//
122711051Sandreas.hansson@arm.com// Response handling: responses from the memory side
122811051Sandreas.hansson@arm.com//
122911051Sandreas.hansson@arm.com/////////////////////////////////////////////////////
123011051Sandreas.hansson@arm.com
123111051Sandreas.hansson@arm.com
123211051Sandreas.hansson@arm.comvoid
123311375Sandreas.hansson@arm.comCache::handleUncacheableWriteResp(PacketPtr pkt)
123411375Sandreas.hansson@arm.com{
123511375Sandreas.hansson@arm.com    Tick completion_time = clockEdge(responseLatency) +
123611375Sandreas.hansson@arm.com        pkt->headerDelay + pkt->payloadDelay;
123711375Sandreas.hansson@arm.com
123811453Sandreas.hansson@arm.com    // Reset the bus additional time as it is now accounted for
123911453Sandreas.hansson@arm.com    pkt->headerDelay = pkt->payloadDelay = 0;
124011375Sandreas.hansson@arm.com
124111453Sandreas.hansson@arm.com    cpuSidePort->schedTimingResp(pkt, completion_time, true);
124211375Sandreas.hansson@arm.com}
124311375Sandreas.hansson@arm.com
124411375Sandreas.hansson@arm.comvoid
124511051Sandreas.hansson@arm.comCache::recvTimingResp(PacketPtr pkt)
124611051Sandreas.hansson@arm.com{
124711051Sandreas.hansson@arm.com    assert(pkt->isResponse());
124811051Sandreas.hansson@arm.com
124911051Sandreas.hansson@arm.com    // all header delay should be paid for by the crossbar, unless
125011051Sandreas.hansson@arm.com    // this is a prefetch response from above
125111051Sandreas.hansson@arm.com    panic_if(pkt->headerDelay != 0 && pkt->cmd != MemCmd::HardPFResp,
125211051Sandreas.hansson@arm.com             "%s saw a non-zero packet delay\n", name());
125311051Sandreas.hansson@arm.com
125411051Sandreas.hansson@arm.com    bool is_error = pkt->isError();
125511051Sandreas.hansson@arm.com
125611051Sandreas.hansson@arm.com    if (is_error) {
125711051Sandreas.hansson@arm.com        DPRINTF(Cache, "Cache received packet with error for addr %#llx (%s), "
125811051Sandreas.hansson@arm.com                "cmd: %s\n", pkt->getAddr(), pkt->isSecure() ? "s" : "ns",
125911051Sandreas.hansson@arm.com                pkt->cmdString());
126011051Sandreas.hansson@arm.com    }
126111051Sandreas.hansson@arm.com
126211051Sandreas.hansson@arm.com    DPRINTF(Cache, "Handling response %s for addr %#llx size %d (%s)\n",
126311051Sandreas.hansson@arm.com            pkt->cmdString(), pkt->getAddr(), pkt->getSize(),
126411051Sandreas.hansson@arm.com            pkt->isSecure() ? "s" : "ns");
126511051Sandreas.hansson@arm.com
126611375Sandreas.hansson@arm.com    // if this is a write, we should be looking at an uncacheable
126711375Sandreas.hansson@arm.com    // write
126811375Sandreas.hansson@arm.com    if (pkt->isWrite()) {
126911375Sandreas.hansson@arm.com        assert(pkt->req->isUncacheable());
127011375Sandreas.hansson@arm.com        handleUncacheableWriteResp(pkt);
127111375Sandreas.hansson@arm.com        return;
127211375Sandreas.hansson@arm.com    }
127311375Sandreas.hansson@arm.com
127411375Sandreas.hansson@arm.com    // we have dealt with any (uncacheable) writes above, from here on
127511375Sandreas.hansson@arm.com    // we know we are dealing with an MSHR due to a miss or a prefetch
127611453Sandreas.hansson@arm.com    MSHR *mshr = dynamic_cast<MSHR*>(pkt->popSenderState());
127711375Sandreas.hansson@arm.com    assert(mshr);
127811051Sandreas.hansson@arm.com
127911051Sandreas.hansson@arm.com    if (mshr == noTargetMSHR) {
128011051Sandreas.hansson@arm.com        // we always clear at least one target
128111051Sandreas.hansson@arm.com        clearBlocked(Blocked_NoTargets);
128211484Snikos.nikoleris@arm.com        noTargetMSHR = nullptr;
128311051Sandreas.hansson@arm.com    }
128411051Sandreas.hansson@arm.com
128511051Sandreas.hansson@arm.com    // Initial target is used just for stats
128611051Sandreas.hansson@arm.com    MSHR::Target *initial_tgt = mshr->getTarget();
128711051Sandreas.hansson@arm.com    int stats_cmd_idx = initial_tgt->pkt->cmdToIndex();
128811051Sandreas.hansson@arm.com    Tick miss_latency = curTick() - initial_tgt->recvTime;
128911051Sandreas.hansson@arm.com
129011051Sandreas.hansson@arm.com    if (pkt->req->isUncacheable()) {
129111051Sandreas.hansson@arm.com        assert(pkt->req->masterId() < system->maxMasters());
129211051Sandreas.hansson@arm.com        mshr_uncacheable_lat[stats_cmd_idx][pkt->req->masterId()] +=
129311051Sandreas.hansson@arm.com            miss_latency;
129411051Sandreas.hansson@arm.com    } else {
129511051Sandreas.hansson@arm.com        assert(pkt->req->masterId() < system->maxMasters());
129611051Sandreas.hansson@arm.com        mshr_miss_latency[stats_cmd_idx][pkt->req->masterId()] +=
129711051Sandreas.hansson@arm.com            miss_latency;
129811051Sandreas.hansson@arm.com    }
129911051Sandreas.hansson@arm.com
130011375Sandreas.hansson@arm.com    bool wasFull = mshrQueue.isFull();
130111375Sandreas.hansson@arm.com
130211375Sandreas.hansson@arm.com    PacketList writebacks;
130311375Sandreas.hansson@arm.com
130411375Sandreas.hansson@arm.com    Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
130511375Sandreas.hansson@arm.com
130611284Sandreas.hansson@arm.com    // upgrade deferred targets if the response has no sharers, and is
130711284Sandreas.hansson@arm.com    // thus passing writable
130811284Sandreas.hansson@arm.com    if (!pkt->hasSharers()) {
130911284Sandreas.hansson@arm.com        mshr->promoteWritable();
131011177Sandreas.hansson@arm.com    }
131111177Sandreas.hansson@arm.com
131211051Sandreas.hansson@arm.com    bool is_fill = !mshr->isForward &&
131311051Sandreas.hansson@arm.com        (pkt->isRead() || pkt->cmd == MemCmd::UpgradeResp);
131411051Sandreas.hansson@arm.com
131511177Sandreas.hansson@arm.com    CacheBlk *blk = tags->findBlock(pkt->getAddr(), pkt->isSecure());
131611177Sandreas.hansson@arm.com
131711051Sandreas.hansson@arm.com    if (is_fill && !is_error) {
131811051Sandreas.hansson@arm.com        DPRINTF(Cache, "Block for addr %#llx being updated in Cache\n",
131911051Sandreas.hansson@arm.com                pkt->getAddr());
132011051Sandreas.hansson@arm.com
132111741Snikos.nikoleris@arm.com        blk = handleFill(pkt, blk, writebacks, mshr->allocOnFill());
132211484Snikos.nikoleris@arm.com        assert(blk != nullptr);
132311051Sandreas.hansson@arm.com    }
132411051Sandreas.hansson@arm.com
132511051Sandreas.hansson@arm.com    // allow invalidation responses originating from write-line
132611051Sandreas.hansson@arm.com    // requests to be discarded
132711136Sandreas.hansson@arm.com    bool is_invalidate = pkt->isInvalidate();
132811051Sandreas.hansson@arm.com
132911051Sandreas.hansson@arm.com    // First offset for critical word first calculations
133011051Sandreas.hansson@arm.com    int initial_offset = initial_tgt->pkt->getOffset(blkSize);
133111051Sandreas.hansson@arm.com
133211601Sandreas.hansson@arm.com    bool from_cache = false;
133311601Sandreas.hansson@arm.com
133411051Sandreas.hansson@arm.com    while (mshr->hasTargets()) {
133511051Sandreas.hansson@arm.com        MSHR::Target *target = mshr->getTarget();
133611051Sandreas.hansson@arm.com        Packet *tgt_pkt = target->pkt;
133711051Sandreas.hansson@arm.com
133811051Sandreas.hansson@arm.com        switch (target->source) {
133911051Sandreas.hansson@arm.com          case MSHR::Target::FromCPU:
134011051Sandreas.hansson@arm.com            Tick completion_time;
134111051Sandreas.hansson@arm.com            // Here we charge on completion_time the delay of the xbar if the
134211051Sandreas.hansson@arm.com            // packet comes from it, charged on headerDelay.
134311051Sandreas.hansson@arm.com            completion_time = pkt->headerDelay;
134411051Sandreas.hansson@arm.com
134511051Sandreas.hansson@arm.com            // Software prefetch handling for cache closest to core
134611051Sandreas.hansson@arm.com            if (tgt_pkt->cmd.isSWPrefetch()) {
134711483Snikos.nikoleris@arm.com                // a software prefetch would have already been ack'd
134811483Snikos.nikoleris@arm.com                // immediately with dummy data so the core would be able to
134911483Snikos.nikoleris@arm.com                // retire it. This request completes right here, so we
135011483Snikos.nikoleris@arm.com                // deallocate it.
135111051Sandreas.hansson@arm.com                delete tgt_pkt->req;
135211051Sandreas.hansson@arm.com                delete tgt_pkt;
135311051Sandreas.hansson@arm.com                break; // skip response
135411051Sandreas.hansson@arm.com            }
135511051Sandreas.hansson@arm.com
135611601Sandreas.hansson@arm.com            // keep track of whether we have responded to another
135711601Sandreas.hansson@arm.com            // cache
135811601Sandreas.hansson@arm.com            from_cache = from_cache || tgt_pkt->fromCache();
135911601Sandreas.hansson@arm.com
136011051Sandreas.hansson@arm.com            // unlike the other packet flows, where data is found in other
136111051Sandreas.hansson@arm.com            // caches or memory and brought back, write-line requests always
136211051Sandreas.hansson@arm.com            // have the data right away, so the above check for "is fill?"
136311051Sandreas.hansson@arm.com            // cannot actually be determined until examining the stored MSHR
136411051Sandreas.hansson@arm.com            // state. We "catch up" with that logic here, which is duplicated
136511051Sandreas.hansson@arm.com            // from above.
136611051Sandreas.hansson@arm.com            if (tgt_pkt->cmd == MemCmd::WriteLineReq) {
136711051Sandreas.hansson@arm.com                assert(!is_error);
136811284Sandreas.hansson@arm.com                // we got the block in a writable state, so promote
136911284Sandreas.hansson@arm.com                // any deferred targets if possible
137011284Sandreas.hansson@arm.com                mshr->promoteWritable();
137111051Sandreas.hansson@arm.com                // NB: we use the original packet here and not the response!
137211741Snikos.nikoleris@arm.com                blk = handleFill(tgt_pkt, blk, writebacks,
137311741Snikos.nikoleris@arm.com                                 mshr->allocOnFill());
137411484Snikos.nikoleris@arm.com                assert(blk != nullptr);
137511051Sandreas.hansson@arm.com
137611051Sandreas.hansson@arm.com                // treat as a fill, and discard the invalidation
137711051Sandreas.hansson@arm.com                // response
137811051Sandreas.hansson@arm.com                is_fill = true;
137911136Sandreas.hansson@arm.com                is_invalidate = false;
138011051Sandreas.hansson@arm.com            }
138111051Sandreas.hansson@arm.com
138211051Sandreas.hansson@arm.com            if (is_fill) {
138311601Sandreas.hansson@arm.com                satisfyRequest(tgt_pkt, blk, true, mshr->hasPostDowngrade());
138411051Sandreas.hansson@arm.com
138511051Sandreas.hansson@arm.com                // How many bytes past the first request is this one
138611051Sandreas.hansson@arm.com                int transfer_offset =
138711051Sandreas.hansson@arm.com                    tgt_pkt->getOffset(blkSize) - initial_offset;
138811051Sandreas.hansson@arm.com                if (transfer_offset < 0) {
138911051Sandreas.hansson@arm.com                    transfer_offset += blkSize;
139011051Sandreas.hansson@arm.com                }
139111051Sandreas.hansson@arm.com
139211051Sandreas.hansson@arm.com                // If not critical word (offset) return payloadDelay.
139311051Sandreas.hansson@arm.com                // responseLatency is the latency of the return path
139411051Sandreas.hansson@arm.com                // from lower level caches/memory to an upper level cache or
139511051Sandreas.hansson@arm.com                // the core.
139611051Sandreas.hansson@arm.com                completion_time += clockEdge(responseLatency) +
139711051Sandreas.hansson@arm.com                    (transfer_offset ? pkt->payloadDelay : 0);
139811051Sandreas.hansson@arm.com
139911051Sandreas.hansson@arm.com                assert(!tgt_pkt->req->isUncacheable());
140011051Sandreas.hansson@arm.com
140111051Sandreas.hansson@arm.com                assert(tgt_pkt->req->masterId() < system->maxMasters());
140211051Sandreas.hansson@arm.com                missLatency[tgt_pkt->cmdToIndex()][tgt_pkt->req->masterId()] +=
140311051Sandreas.hansson@arm.com                    completion_time - target->recvTime;
140411051Sandreas.hansson@arm.com            } else if (pkt->cmd == MemCmd::UpgradeFailResp) {
140511051Sandreas.hansson@arm.com                // failed StoreCond upgrade
140611051Sandreas.hansson@arm.com                assert(tgt_pkt->cmd == MemCmd::StoreCondReq ||
140711051Sandreas.hansson@arm.com                       tgt_pkt->cmd == MemCmd::StoreCondFailReq ||
140811051Sandreas.hansson@arm.com                       tgt_pkt->cmd == MemCmd::SCUpgradeFailReq);
140911051Sandreas.hansson@arm.com                // responseLatency is the latency of the return path
141011051Sandreas.hansson@arm.com                // from lower level caches/memory to an upper level cache or
141111051Sandreas.hansson@arm.com                // the core.
141211051Sandreas.hansson@arm.com                completion_time += clockEdge(responseLatency) +
141311051Sandreas.hansson@arm.com                    pkt->payloadDelay;
141411051Sandreas.hansson@arm.com                tgt_pkt->req->setExtraData(0);
141511051Sandreas.hansson@arm.com            } else {
141611051Sandreas.hansson@arm.com                // not a cache fill, just forwarding response
141711051Sandreas.hansson@arm.com                // responseLatency is the latency of the return path
141811051Sandreas.hansson@arm.com                // from lower level cahces/memory to the core.
141911051Sandreas.hansson@arm.com                completion_time += clockEdge(responseLatency) +
142011051Sandreas.hansson@arm.com                    pkt->payloadDelay;
142111051Sandreas.hansson@arm.com                if (pkt->isRead() && !is_error) {
142211051Sandreas.hansson@arm.com                    // sanity check
142311051Sandreas.hansson@arm.com                    assert(pkt->getAddr() == tgt_pkt->getAddr());
142411051Sandreas.hansson@arm.com                    assert(pkt->getSize() >= tgt_pkt->getSize());
142511051Sandreas.hansson@arm.com
142611051Sandreas.hansson@arm.com                    tgt_pkt->setData(pkt->getConstPtr<uint8_t>());
142711051Sandreas.hansson@arm.com                }
142811051Sandreas.hansson@arm.com            }
142911051Sandreas.hansson@arm.com            tgt_pkt->makeTimingResponse();
143011051Sandreas.hansson@arm.com            // if this packet is an error copy that to the new packet
143111051Sandreas.hansson@arm.com            if (is_error)
143211051Sandreas.hansson@arm.com                tgt_pkt->copyError(pkt);
143311051Sandreas.hansson@arm.com            if (tgt_pkt->cmd == MemCmd::ReadResp &&
143411136Sandreas.hansson@arm.com                (is_invalidate || mshr->hasPostInvalidate())) {
143511051Sandreas.hansson@arm.com                // If intermediate cache got ReadRespWithInvalidate,
143611051Sandreas.hansson@arm.com                // propagate that.  Response should not have
143711051Sandreas.hansson@arm.com                // isInvalidate() set otherwise.
143811051Sandreas.hansson@arm.com                tgt_pkt->cmd = MemCmd::ReadRespWithInvalidate;
143911051Sandreas.hansson@arm.com                DPRINTF(Cache, "%s updated cmd to %s for addr %#llx\n",
144011051Sandreas.hansson@arm.com                        __func__, tgt_pkt->cmdString(), tgt_pkt->getAddr());
144111051Sandreas.hansson@arm.com            }
144211051Sandreas.hansson@arm.com            // Reset the bus additional time as it is now accounted for
144311051Sandreas.hansson@arm.com            tgt_pkt->headerDelay = tgt_pkt->payloadDelay = 0;
144411194Sali.jafri@arm.com            cpuSidePort->schedTimingResp(tgt_pkt, completion_time, true);
144511051Sandreas.hansson@arm.com            break;
144611051Sandreas.hansson@arm.com
144711051Sandreas.hansson@arm.com          case MSHR::Target::FromPrefetcher:
144811051Sandreas.hansson@arm.com            assert(tgt_pkt->cmd == MemCmd::HardPFReq);
144911051Sandreas.hansson@arm.com            if (blk)
145011051Sandreas.hansson@arm.com                blk->status |= BlkHWPrefetched;
145111051Sandreas.hansson@arm.com            delete tgt_pkt->req;
145211051Sandreas.hansson@arm.com            delete tgt_pkt;
145311051Sandreas.hansson@arm.com            break;
145411051Sandreas.hansson@arm.com
145511051Sandreas.hansson@arm.com          case MSHR::Target::FromSnoop:
145611051Sandreas.hansson@arm.com            // I don't believe that a snoop can be in an error state
145711051Sandreas.hansson@arm.com            assert(!is_error);
145811051Sandreas.hansson@arm.com            // response to snoop request
145911051Sandreas.hansson@arm.com            DPRINTF(Cache, "processing deferred snoop...\n");
146011136Sandreas.hansson@arm.com            assert(!(is_invalidate && !mshr->hasPostInvalidate()));
146111051Sandreas.hansson@arm.com            handleSnoop(tgt_pkt, blk, true, true, mshr->hasPostInvalidate());
146211051Sandreas.hansson@arm.com            break;
146311051Sandreas.hansson@arm.com
146411051Sandreas.hansson@arm.com          default:
146511051Sandreas.hansson@arm.com            panic("Illegal target->source enum %d\n", target->source);
146611051Sandreas.hansson@arm.com        }
146711051Sandreas.hansson@arm.com
146811051Sandreas.hansson@arm.com        mshr->popTarget();
146911051Sandreas.hansson@arm.com    }
147011051Sandreas.hansson@arm.com
147111601Sandreas.hansson@arm.com    maintainClusivity(from_cache, blk);
147211601Sandreas.hansson@arm.com
147311051Sandreas.hansson@arm.com    if (blk && blk->isValid()) {
147411051Sandreas.hansson@arm.com        // an invalidate response stemming from a write line request
147511051Sandreas.hansson@arm.com        // should not invalidate the block, so check if the
147611051Sandreas.hansson@arm.com        // invalidation should be discarded
147711136Sandreas.hansson@arm.com        if (is_invalidate || mshr->hasPostInvalidate()) {
147811197Sandreas.hansson@arm.com            invalidateBlock(blk);
147911051Sandreas.hansson@arm.com        } else if (mshr->hasPostDowngrade()) {
148011051Sandreas.hansson@arm.com            blk->status &= ~BlkWritable;
148111051Sandreas.hansson@arm.com        }
148211051Sandreas.hansson@arm.com    }
148311051Sandreas.hansson@arm.com
148411051Sandreas.hansson@arm.com    if (mshr->promoteDeferredTargets()) {
148511051Sandreas.hansson@arm.com        // avoid later read getting stale data while write miss is
148611051Sandreas.hansson@arm.com        // outstanding.. see comment in timingAccess()
148711051Sandreas.hansson@arm.com        if (blk) {
148811051Sandreas.hansson@arm.com            blk->status &= ~BlkReadable;
148911051Sandreas.hansson@arm.com        }
149011375Sandreas.hansson@arm.com        mshrQueue.markPending(mshr);
149111051Sandreas.hansson@arm.com        schedMemSideSendEvent(clockEdge() + pkt->payloadDelay);
149211051Sandreas.hansson@arm.com    } else {
149311375Sandreas.hansson@arm.com        mshrQueue.deallocate(mshr);
149411375Sandreas.hansson@arm.com        if (wasFull && !mshrQueue.isFull()) {
149511375Sandreas.hansson@arm.com            clearBlocked(Blocked_NoMSHRs);
149611051Sandreas.hansson@arm.com        }
149711051Sandreas.hansson@arm.com
149811051Sandreas.hansson@arm.com        // Request the bus for a prefetch if this deallocation freed enough
149911051Sandreas.hansson@arm.com        // MSHRs for a prefetch to take place
150011375Sandreas.hansson@arm.com        if (prefetcher && mshrQueue.canPrefetch()) {
150111051Sandreas.hansson@arm.com            Tick next_pf_time = std::max(prefetcher->nextPrefetchReadyTime(),
150211051Sandreas.hansson@arm.com                                         clockEdge());
150311051Sandreas.hansson@arm.com            if (next_pf_time != MaxTick)
150411051Sandreas.hansson@arm.com                schedMemSideSendEvent(next_pf_time);
150511051Sandreas.hansson@arm.com        }
150611051Sandreas.hansson@arm.com    }
150711051Sandreas.hansson@arm.com    // reset the xbar additional timinig  as it is now accounted for
150811051Sandreas.hansson@arm.com    pkt->headerDelay = pkt->payloadDelay = 0;
150911051Sandreas.hansson@arm.com
151011051Sandreas.hansson@arm.com    // copy writebacks to write buffer
151111051Sandreas.hansson@arm.com    doWritebacks(writebacks, forward_time);
151211051Sandreas.hansson@arm.com
151311051Sandreas.hansson@arm.com    // if we used temp block, check to see if its valid and then clear it out
151411051Sandreas.hansson@arm.com    if (blk == tempBlock && tempBlock->isValid()) {
151511051Sandreas.hansson@arm.com        // We use forwardLatency here because we are copying
151611051Sandreas.hansson@arm.com        // Writebacks/CleanEvicts to write buffer. It specifies the latency to
151711051Sandreas.hansson@arm.com        // allocate an internal buffer and to schedule an event to the
151811051Sandreas.hansson@arm.com        // queued port.
151911199Sandreas.hansson@arm.com        if (blk->isDirty() || writebackClean) {
152011051Sandreas.hansson@arm.com            PacketPtr wbPkt = writebackBlk(blk);
152111051Sandreas.hansson@arm.com            allocateWriteBuffer(wbPkt, forward_time);
152211051Sandreas.hansson@arm.com            // Set BLOCK_CACHED flag if cached above.
152311051Sandreas.hansson@arm.com            if (isCachedAbove(wbPkt))
152411051Sandreas.hansson@arm.com                wbPkt->setBlockCached();
152511051Sandreas.hansson@arm.com        } else {
152611051Sandreas.hansson@arm.com            PacketPtr wcPkt = cleanEvictBlk(blk);
152711051Sandreas.hansson@arm.com            // Check to see if block is cached above. If not allocate
152811051Sandreas.hansson@arm.com            // write buffer
152911051Sandreas.hansson@arm.com            if (isCachedAbove(wcPkt))
153011051Sandreas.hansson@arm.com                delete wcPkt;
153111051Sandreas.hansson@arm.com            else
153211051Sandreas.hansson@arm.com                allocateWriteBuffer(wcPkt, forward_time);
153311051Sandreas.hansson@arm.com        }
153411051Sandreas.hansson@arm.com        blk->invalidate();
153511051Sandreas.hansson@arm.com    }
153611051Sandreas.hansson@arm.com
153711288Ssteve.reinhardt@amd.com    DPRINTF(CacheVerbose, "Leaving %s with %s for addr %#llx\n", __func__,
153811051Sandreas.hansson@arm.com            pkt->cmdString(), pkt->getAddr());
153911051Sandreas.hansson@arm.com    delete pkt;
154011051Sandreas.hansson@arm.com}
154111051Sandreas.hansson@arm.com
154211051Sandreas.hansson@arm.comPacketPtr
154311051Sandreas.hansson@arm.comCache::writebackBlk(CacheBlk *blk)
154411051Sandreas.hansson@arm.com{
154511199Sandreas.hansson@arm.com    chatty_assert(!isReadOnly || writebackClean,
154611199Sandreas.hansson@arm.com                  "Writeback from read-only cache");
154711199Sandreas.hansson@arm.com    assert(blk && blk->isValid() && (blk->isDirty() || writebackClean));
154811051Sandreas.hansson@arm.com
154911051Sandreas.hansson@arm.com    writebacks[Request::wbMasterId]++;
155011051Sandreas.hansson@arm.com
155111199Sandreas.hansson@arm.com    Request *req = new Request(tags->regenerateBlkAddr(blk->tag, blk->set),
155211199Sandreas.hansson@arm.com                               blkSize, 0, Request::wbMasterId);
155311051Sandreas.hansson@arm.com    if (blk->isSecure())
155411199Sandreas.hansson@arm.com        req->setFlags(Request::SECURE);
155511051Sandreas.hansson@arm.com
155611199Sandreas.hansson@arm.com    req->taskId(blk->task_id);
155711051Sandreas.hansson@arm.com    blk->task_id= ContextSwitchTaskId::Unknown;
155811051Sandreas.hansson@arm.com    blk->tickInserted = curTick();
155911051Sandreas.hansson@arm.com
156011199Sandreas.hansson@arm.com    PacketPtr pkt =
156111199Sandreas.hansson@arm.com        new Packet(req, blk->isDirty() ?
156211199Sandreas.hansson@arm.com                   MemCmd::WritebackDirty : MemCmd::WritebackClean);
156311199Sandreas.hansson@arm.com
156411199Sandreas.hansson@arm.com    DPRINTF(Cache, "Create Writeback %#llx writable: %d, dirty: %d\n",
156511199Sandreas.hansson@arm.com            pkt->getAddr(), blk->isWritable(), blk->isDirty());
156611199Sandreas.hansson@arm.com
156711051Sandreas.hansson@arm.com    if (blk->isWritable()) {
156811051Sandreas.hansson@arm.com        // not asserting shared means we pass the block in modified
156911051Sandreas.hansson@arm.com        // state, mark our own block non-writeable
157011051Sandreas.hansson@arm.com        blk->status &= ~BlkWritable;
157111051Sandreas.hansson@arm.com    } else {
157211284Sandreas.hansson@arm.com        // we are in the Owned state, tell the receiver
157311284Sandreas.hansson@arm.com        pkt->setHasSharers();
157411051Sandreas.hansson@arm.com    }
157511051Sandreas.hansson@arm.com
157611199Sandreas.hansson@arm.com    // make sure the block is not marked dirty
157711199Sandreas.hansson@arm.com    blk->status &= ~BlkDirty;
157811051Sandreas.hansson@arm.com
157911199Sandreas.hansson@arm.com    pkt->allocate();
158011199Sandreas.hansson@arm.com    std::memcpy(pkt->getPtr<uint8_t>(), blk->data, blkSize);
158111199Sandreas.hansson@arm.com
158211199Sandreas.hansson@arm.com    return pkt;
158311051Sandreas.hansson@arm.com}
158411051Sandreas.hansson@arm.com
158511051Sandreas.hansson@arm.comPacketPtr
158611051Sandreas.hansson@arm.comCache::cleanEvictBlk(CacheBlk *blk)
158711051Sandreas.hansson@arm.com{
158811199Sandreas.hansson@arm.com    assert(!writebackClean);
158911051Sandreas.hansson@arm.com    assert(blk && blk->isValid() && !blk->isDirty());
159011051Sandreas.hansson@arm.com    // Creating a zero sized write, a message to the snoop filter
159111051Sandreas.hansson@arm.com    Request *req =
159211051Sandreas.hansson@arm.com        new Request(tags->regenerateBlkAddr(blk->tag, blk->set), blkSize, 0,
159311051Sandreas.hansson@arm.com                    Request::wbMasterId);
159411051Sandreas.hansson@arm.com    if (blk->isSecure())
159511051Sandreas.hansson@arm.com        req->setFlags(Request::SECURE);
159611051Sandreas.hansson@arm.com
159711051Sandreas.hansson@arm.com    req->taskId(blk->task_id);
159811051Sandreas.hansson@arm.com    blk->task_id = ContextSwitchTaskId::Unknown;
159911051Sandreas.hansson@arm.com    blk->tickInserted = curTick();
160011051Sandreas.hansson@arm.com
160111051Sandreas.hansson@arm.com    PacketPtr pkt = new Packet(req, MemCmd::CleanEvict);
160211051Sandreas.hansson@arm.com    pkt->allocate();
160311051Sandreas.hansson@arm.com    DPRINTF(Cache, "%s%s %x Create CleanEvict\n", pkt->cmdString(),
160411051Sandreas.hansson@arm.com            pkt->req->isInstFetch() ? " (ifetch)" : "",
160511051Sandreas.hansson@arm.com            pkt->getAddr());
160611051Sandreas.hansson@arm.com
160711051Sandreas.hansson@arm.com    return pkt;
160811051Sandreas.hansson@arm.com}
160911051Sandreas.hansson@arm.com
161011051Sandreas.hansson@arm.comvoid
161111051Sandreas.hansson@arm.comCache::memWriteback()
161211051Sandreas.hansson@arm.com{
161311051Sandreas.hansson@arm.com    CacheBlkVisitorWrapper visitor(*this, &Cache::writebackVisitor);
161411051Sandreas.hansson@arm.com    tags->forEachBlk(visitor);
161511051Sandreas.hansson@arm.com}
161611051Sandreas.hansson@arm.com
161711051Sandreas.hansson@arm.comvoid
161811051Sandreas.hansson@arm.comCache::memInvalidate()
161911051Sandreas.hansson@arm.com{
162011051Sandreas.hansson@arm.com    CacheBlkVisitorWrapper visitor(*this, &Cache::invalidateVisitor);
162111051Sandreas.hansson@arm.com    tags->forEachBlk(visitor);
162211051Sandreas.hansson@arm.com}
162311051Sandreas.hansson@arm.com
162411051Sandreas.hansson@arm.combool
162511051Sandreas.hansson@arm.comCache::isDirty() const
162611051Sandreas.hansson@arm.com{
162711051Sandreas.hansson@arm.com    CacheBlkIsDirtyVisitor visitor;
162811051Sandreas.hansson@arm.com    tags->forEachBlk(visitor);
162911051Sandreas.hansson@arm.com
163011051Sandreas.hansson@arm.com    return visitor.isDirty();
163111051Sandreas.hansson@arm.com}
163211051Sandreas.hansson@arm.com
163311051Sandreas.hansson@arm.combool
163411051Sandreas.hansson@arm.comCache::writebackVisitor(CacheBlk &blk)
163511051Sandreas.hansson@arm.com{
163611051Sandreas.hansson@arm.com    if (blk.isDirty()) {
163711051Sandreas.hansson@arm.com        assert(blk.isValid());
163811051Sandreas.hansson@arm.com
163911051Sandreas.hansson@arm.com        Request request(tags->regenerateBlkAddr(blk.tag, blk.set),
164011051Sandreas.hansson@arm.com                        blkSize, 0, Request::funcMasterId);
164111051Sandreas.hansson@arm.com        request.taskId(blk.task_id);
164211051Sandreas.hansson@arm.com
164311051Sandreas.hansson@arm.com        Packet packet(&request, MemCmd::WriteReq);
164411051Sandreas.hansson@arm.com        packet.dataStatic(blk.data);
164511051Sandreas.hansson@arm.com
164611051Sandreas.hansson@arm.com        memSidePort->sendFunctional(&packet);
164711051Sandreas.hansson@arm.com
164811051Sandreas.hansson@arm.com        blk.status &= ~BlkDirty;
164911051Sandreas.hansson@arm.com    }
165011051Sandreas.hansson@arm.com
165111051Sandreas.hansson@arm.com    return true;
165211051Sandreas.hansson@arm.com}
165311051Sandreas.hansson@arm.com
165411051Sandreas.hansson@arm.combool
165511051Sandreas.hansson@arm.comCache::invalidateVisitor(CacheBlk &blk)
165611051Sandreas.hansson@arm.com{
165711051Sandreas.hansson@arm.com
165811051Sandreas.hansson@arm.com    if (blk.isDirty())
165911051Sandreas.hansson@arm.com        warn_once("Invalidating dirty cache lines. Expect things to break.\n");
166011051Sandreas.hansson@arm.com
166111051Sandreas.hansson@arm.com    if (blk.isValid()) {
166211051Sandreas.hansson@arm.com        assert(!blk.isDirty());
166311051Sandreas.hansson@arm.com        tags->invalidate(&blk);
166411051Sandreas.hansson@arm.com        blk.invalidate();
166511051Sandreas.hansson@arm.com    }
166611051Sandreas.hansson@arm.com
166711051Sandreas.hansson@arm.com    return true;
166811051Sandreas.hansson@arm.com}
166911051Sandreas.hansson@arm.com
167011051Sandreas.hansson@arm.comCacheBlk*
167111051Sandreas.hansson@arm.comCache::allocateBlock(Addr addr, bool is_secure, PacketList &writebacks)
167211051Sandreas.hansson@arm.com{
167311051Sandreas.hansson@arm.com    CacheBlk *blk = tags->findVictim(addr);
167411051Sandreas.hansson@arm.com
167511484Snikos.nikoleris@arm.com    // It is valid to return nullptr if there is no victim
167611051Sandreas.hansson@arm.com    if (!blk)
167711051Sandreas.hansson@arm.com        return nullptr;
167811051Sandreas.hansson@arm.com
167911051Sandreas.hansson@arm.com    if (blk->isValid()) {
168011051Sandreas.hansson@arm.com        Addr repl_addr = tags->regenerateBlkAddr(blk->tag, blk->set);
168111051Sandreas.hansson@arm.com        MSHR *repl_mshr = mshrQueue.findMatch(repl_addr, blk->isSecure());
168211051Sandreas.hansson@arm.com        if (repl_mshr) {
168311051Sandreas.hansson@arm.com            // must be an outstanding upgrade request
168411051Sandreas.hansson@arm.com            // on a block we're about to replace...
168511051Sandreas.hansson@arm.com            assert(!blk->isWritable() || blk->isDirty());
168611284Sandreas.hansson@arm.com            assert(repl_mshr->needsWritable());
168711051Sandreas.hansson@arm.com            // too hard to replace block with transient state
168811051Sandreas.hansson@arm.com            // allocation failed, block not inserted
168911484Snikos.nikoleris@arm.com            return nullptr;
169011051Sandreas.hansson@arm.com        } else {
169111483Snikos.nikoleris@arm.com            DPRINTF(Cache, "replacement: replacing %#llx (%s) with %#llx "
169211483Snikos.nikoleris@arm.com                    "(%s): %s\n", repl_addr, blk->isSecure() ? "s" : "ns",
169311051Sandreas.hansson@arm.com                    addr, is_secure ? "s" : "ns",
169411051Sandreas.hansson@arm.com                    blk->isDirty() ? "writeback" : "clean");
169511051Sandreas.hansson@arm.com
169611436SRekai.GonzalezAlberquilla@arm.com            if (blk->wasPrefetched()) {
169711436SRekai.GonzalezAlberquilla@arm.com                unusedPrefetches++;
169811436SRekai.GonzalezAlberquilla@arm.com            }
169911051Sandreas.hansson@arm.com            // Will send up Writeback/CleanEvict snoops via isCachedAbove
170011051Sandreas.hansson@arm.com            // when pushing this writeback list into the write buffer.
170111199Sandreas.hansson@arm.com            if (blk->isDirty() || writebackClean) {
170211051Sandreas.hansson@arm.com                // Save writeback packet for handling by caller
170311051Sandreas.hansson@arm.com                writebacks.push_back(writebackBlk(blk));
170411051Sandreas.hansson@arm.com            } else {
170511051Sandreas.hansson@arm.com                writebacks.push_back(cleanEvictBlk(blk));
170611051Sandreas.hansson@arm.com            }
170711051Sandreas.hansson@arm.com        }
170811051Sandreas.hansson@arm.com    }
170911051Sandreas.hansson@arm.com
171011051Sandreas.hansson@arm.com    return blk;
171111051Sandreas.hansson@arm.com}
171211051Sandreas.hansson@arm.com
171311197Sandreas.hansson@arm.comvoid
171411197Sandreas.hansson@arm.comCache::invalidateBlock(CacheBlk *blk)
171511197Sandreas.hansson@arm.com{
171611197Sandreas.hansson@arm.com    if (blk != tempBlock)
171711197Sandreas.hansson@arm.com        tags->invalidate(blk);
171811197Sandreas.hansson@arm.com    blk->invalidate();
171911197Sandreas.hansson@arm.com}
172011051Sandreas.hansson@arm.com
172111051Sandreas.hansson@arm.com// Note that the reason we return a list of writebacks rather than
172211051Sandreas.hansson@arm.com// inserting them directly in the write buffer is that this function
172311051Sandreas.hansson@arm.com// is called by both atomic and timing-mode accesses, and in atomic
172411051Sandreas.hansson@arm.com// mode we don't mess with the write buffer (we just perform the
172511051Sandreas.hansson@arm.com// writebacks atomically once the original request is complete).
172611051Sandreas.hansson@arm.comCacheBlk*
172711197Sandreas.hansson@arm.comCache::handleFill(PacketPtr pkt, CacheBlk *blk, PacketList &writebacks,
172811197Sandreas.hansson@arm.com                  bool allocate)
172911051Sandreas.hansson@arm.com{
173011051Sandreas.hansson@arm.com    assert(pkt->isResponse() || pkt->cmd == MemCmd::WriteLineReq);
173111051Sandreas.hansson@arm.com    Addr addr = pkt->getAddr();
173211051Sandreas.hansson@arm.com    bool is_secure = pkt->isSecure();
173311051Sandreas.hansson@arm.com#if TRACING_ON
173411051Sandreas.hansson@arm.com    CacheBlk::State old_state = blk ? blk->status : 0;
173511051Sandreas.hansson@arm.com#endif
173611051Sandreas.hansson@arm.com
173711375Sandreas.hansson@arm.com    // When handling a fill, we should have no writes to this line.
173811375Sandreas.hansson@arm.com    assert(addr == blockAlign(addr));
173911375Sandreas.hansson@arm.com    assert(!writeBuffer.findMatch(addr, is_secure));
174011051Sandreas.hansson@arm.com
174111484Snikos.nikoleris@arm.com    if (blk == nullptr) {
174211051Sandreas.hansson@arm.com        // better have read new data...
174311051Sandreas.hansson@arm.com        assert(pkt->hasData());
174411051Sandreas.hansson@arm.com
174511051Sandreas.hansson@arm.com        // only read responses and write-line requests have data;
174611051Sandreas.hansson@arm.com        // note that we don't write the data here for write-line - that
174711601Sandreas.hansson@arm.com        // happens in the subsequent call to satisfyRequest
174811051Sandreas.hansson@arm.com        assert(pkt->isRead() || pkt->cmd == MemCmd::WriteLineReq);
174911051Sandreas.hansson@arm.com
175011197Sandreas.hansson@arm.com        // need to do a replacement if allocating, otherwise we stick
175111197Sandreas.hansson@arm.com        // with the temporary storage
175211484Snikos.nikoleris@arm.com        blk = allocate ? allocateBlock(addr, is_secure, writebacks) : nullptr;
175311197Sandreas.hansson@arm.com
175411484Snikos.nikoleris@arm.com        if (blk == nullptr) {
175511197Sandreas.hansson@arm.com            // No replaceable block or a mostly exclusive
175611197Sandreas.hansson@arm.com            // cache... just use temporary storage to complete the
175711197Sandreas.hansson@arm.com            // current request and then get rid of it
175811051Sandreas.hansson@arm.com            assert(!tempBlock->isValid());
175911051Sandreas.hansson@arm.com            blk = tempBlock;
176011051Sandreas.hansson@arm.com            tempBlock->set = tags->extractSet(addr);
176111051Sandreas.hansson@arm.com            tempBlock->tag = tags->extractTag(addr);
176211051Sandreas.hansson@arm.com            // @todo: set security state as well...
176311051Sandreas.hansson@arm.com            DPRINTF(Cache, "using temp block for %#llx (%s)\n", addr,
176411051Sandreas.hansson@arm.com                    is_secure ? "s" : "ns");
176511051Sandreas.hansson@arm.com        } else {
176611051Sandreas.hansson@arm.com            tags->insertBlock(pkt, blk);
176711051Sandreas.hansson@arm.com        }
176811051Sandreas.hansson@arm.com
176911051Sandreas.hansson@arm.com        // we should never be overwriting a valid block
177011051Sandreas.hansson@arm.com        assert(!blk->isValid());
177111051Sandreas.hansson@arm.com    } else {
177211051Sandreas.hansson@arm.com        // existing block... probably an upgrade
177311051Sandreas.hansson@arm.com        assert(blk->tag == tags->extractTag(addr));
177411051Sandreas.hansson@arm.com        // either we're getting new data or the block should already be valid
177511051Sandreas.hansson@arm.com        assert(pkt->hasData() || blk->isValid());
177611051Sandreas.hansson@arm.com        // don't clear block status... if block is already dirty we
177711051Sandreas.hansson@arm.com        // don't want to lose that
177811051Sandreas.hansson@arm.com    }
177911051Sandreas.hansson@arm.com
178011051Sandreas.hansson@arm.com    if (is_secure)
178111051Sandreas.hansson@arm.com        blk->status |= BlkSecure;
178211051Sandreas.hansson@arm.com    blk->status |= BlkValid | BlkReadable;
178311051Sandreas.hansson@arm.com
178411137Sandreas.hansson@arm.com    // sanity check for whole-line writes, which should always be
178511137Sandreas.hansson@arm.com    // marked as writable as part of the fill, and then later marked
178611601Sandreas.hansson@arm.com    // dirty as part of satisfyRequest
178711137Sandreas.hansson@arm.com    if (pkt->cmd == MemCmd::WriteLineReq) {
178811284Sandreas.hansson@arm.com        assert(!pkt->hasSharers());
178911137Sandreas.hansson@arm.com        // at the moment other caches do not respond to the
179011137Sandreas.hansson@arm.com        // invalidation requests corresponding to a whole-line write
179111284Sandreas.hansson@arm.com        assert(!pkt->cacheResponding());
179211137Sandreas.hansson@arm.com    }
179311137Sandreas.hansson@arm.com
179411284Sandreas.hansson@arm.com    // here we deal with setting the appropriate state of the line,
179511284Sandreas.hansson@arm.com    // and we start by looking at the hasSharers flag, and ignore the
179611284Sandreas.hansson@arm.com    // cacheResponding flag (normally signalling dirty data) if the
179711284Sandreas.hansson@arm.com    // packet has sharers, thus the line is never allocated as Owned
179811284Sandreas.hansson@arm.com    // (dirty but not writable), and always ends up being either
179911284Sandreas.hansson@arm.com    // Shared, Exclusive or Modified, see Packet::setCacheResponding
180011284Sandreas.hansson@arm.com    // for more details
180111284Sandreas.hansson@arm.com    if (!pkt->hasSharers()) {
180211284Sandreas.hansson@arm.com        // we could get a writable line from memory (rather than a
180311284Sandreas.hansson@arm.com        // cache) even in a read-only cache, note that we set this bit
180411284Sandreas.hansson@arm.com        // even for a read-only cache, possibly revisit this decision
180511051Sandreas.hansson@arm.com        blk->status |= BlkWritable;
180611051Sandreas.hansson@arm.com
180711284Sandreas.hansson@arm.com        // check if we got this via cache-to-cache transfer (i.e., from a
180811284Sandreas.hansson@arm.com        // cache that had the block in Modified or Owned state)
180911284Sandreas.hansson@arm.com        if (pkt->cacheResponding()) {
181011284Sandreas.hansson@arm.com            // we got the block in Modified state, and invalidated the
181111284Sandreas.hansson@arm.com            // owners copy
181211051Sandreas.hansson@arm.com            blk->status |= BlkDirty;
181311051Sandreas.hansson@arm.com
181411051Sandreas.hansson@arm.com            chatty_assert(!isReadOnly, "Should never see dirty snoop response "
181511051Sandreas.hansson@arm.com                          "in read-only cache %s\n", name());
181611051Sandreas.hansson@arm.com        }
181711051Sandreas.hansson@arm.com    }
181811051Sandreas.hansson@arm.com
181911051Sandreas.hansson@arm.com    DPRINTF(Cache, "Block addr %#llx (%s) moving from state %x to %s\n",
182011051Sandreas.hansson@arm.com            addr, is_secure ? "s" : "ns", old_state, blk->print());
182111051Sandreas.hansson@arm.com
182211051Sandreas.hansson@arm.com    // if we got new data, copy it in (checking for a read response
182311051Sandreas.hansson@arm.com    // and a response that has data is the same in the end)
182411051Sandreas.hansson@arm.com    if (pkt->isRead()) {
182511051Sandreas.hansson@arm.com        // sanity checks
182611051Sandreas.hansson@arm.com        assert(pkt->hasData());
182711051Sandreas.hansson@arm.com        assert(pkt->getSize() == blkSize);
182811051Sandreas.hansson@arm.com
182911051Sandreas.hansson@arm.com        std::memcpy(blk->data, pkt->getConstPtr<uint8_t>(), blkSize);
183011051Sandreas.hansson@arm.com    }
183111051Sandreas.hansson@arm.com    // We pay for fillLatency here.
183211051Sandreas.hansson@arm.com    blk->whenReady = clockEdge() + fillLatency * clockPeriod() +
183311051Sandreas.hansson@arm.com        pkt->payloadDelay;
183411051Sandreas.hansson@arm.com
183511051Sandreas.hansson@arm.com    return blk;
183611051Sandreas.hansson@arm.com}
183711051Sandreas.hansson@arm.com
183811051Sandreas.hansson@arm.com
183911051Sandreas.hansson@arm.com/////////////////////////////////////////////////////
184011051Sandreas.hansson@arm.com//
184111051Sandreas.hansson@arm.com// Snoop path: requests coming in from the memory side
184211051Sandreas.hansson@arm.com//
184311051Sandreas.hansson@arm.com/////////////////////////////////////////////////////
184411051Sandreas.hansson@arm.com
184511051Sandreas.hansson@arm.comvoid
184611051Sandreas.hansson@arm.comCache::doTimingSupplyResponse(PacketPtr req_pkt, const uint8_t *blk_data,
184711051Sandreas.hansson@arm.com                              bool already_copied, bool pending_inval)
184811051Sandreas.hansson@arm.com{
184911051Sandreas.hansson@arm.com    // sanity check
185011051Sandreas.hansson@arm.com    assert(req_pkt->isRequest());
185111051Sandreas.hansson@arm.com    assert(req_pkt->needsResponse());
185211051Sandreas.hansson@arm.com
185311051Sandreas.hansson@arm.com    DPRINTF(Cache, "%s for %s addr %#llx size %d\n", __func__,
185411051Sandreas.hansson@arm.com            req_pkt->cmdString(), req_pkt->getAddr(), req_pkt->getSize());
185511051Sandreas.hansson@arm.com    // timing-mode snoop responses require a new packet, unless we
185611051Sandreas.hansson@arm.com    // already made a copy...
185711051Sandreas.hansson@arm.com    PacketPtr pkt = req_pkt;
185811051Sandreas.hansson@arm.com    if (!already_copied)
185911051Sandreas.hansson@arm.com        // do not clear flags, and allocate space for data if the
186011051Sandreas.hansson@arm.com        // packet needs it (the only packets that carry data are read
186111051Sandreas.hansson@arm.com        // responses)
186211051Sandreas.hansson@arm.com        pkt = new Packet(req_pkt, false, req_pkt->isRead());
186311051Sandreas.hansson@arm.com
186411051Sandreas.hansson@arm.com    assert(req_pkt->req->isUncacheable() || req_pkt->isInvalidate() ||
186511284Sandreas.hansson@arm.com           pkt->hasSharers());
186611051Sandreas.hansson@arm.com    pkt->makeTimingResponse();
186711051Sandreas.hansson@arm.com    if (pkt->isRead()) {
186811051Sandreas.hansson@arm.com        pkt->setDataFromBlock(blk_data, blkSize);
186911051Sandreas.hansson@arm.com    }
187011051Sandreas.hansson@arm.com    if (pkt->cmd == MemCmd::ReadResp && pending_inval) {
187111051Sandreas.hansson@arm.com        // Assume we defer a response to a read from a far-away cache
187211051Sandreas.hansson@arm.com        // A, then later defer a ReadExcl from a cache B on the same
187311284Sandreas.hansson@arm.com        // bus as us. We'll assert cacheResponding in both cases, but
187411284Sandreas.hansson@arm.com        // in the latter case cacheResponding will keep the
187511284Sandreas.hansson@arm.com        // invalidation from reaching cache A. This special response
187611284Sandreas.hansson@arm.com        // tells cache A that it gets the block to satisfy its read,
187711284Sandreas.hansson@arm.com        // but must immediately invalidate it.
187811051Sandreas.hansson@arm.com        pkt->cmd = MemCmd::ReadRespWithInvalidate;
187911051Sandreas.hansson@arm.com    }
188011051Sandreas.hansson@arm.com    // Here we consider forward_time, paying for just forward latency and
188111051Sandreas.hansson@arm.com    // also charging the delay provided by the xbar.
188211051Sandreas.hansson@arm.com    // forward_time is used as send_time in next allocateWriteBuffer().
188311051Sandreas.hansson@arm.com    Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
188411051Sandreas.hansson@arm.com    // Here we reset the timing of the packet.
188511051Sandreas.hansson@arm.com    pkt->headerDelay = pkt->payloadDelay = 0;
188611288Ssteve.reinhardt@amd.com    DPRINTF(CacheVerbose,
188711288Ssteve.reinhardt@amd.com            "%s created response: %s addr %#llx size %d tick: %lu\n",
188811051Sandreas.hansson@arm.com            __func__, pkt->cmdString(), pkt->getAddr(), pkt->getSize(),
188911051Sandreas.hansson@arm.com            forward_time);
189011051Sandreas.hansson@arm.com    memSidePort->schedTimingSnoopResp(pkt, forward_time, true);
189111051Sandreas.hansson@arm.com}
189211051Sandreas.hansson@arm.com
189311127Sandreas.hansson@arm.comuint32_t
189411051Sandreas.hansson@arm.comCache::handleSnoop(PacketPtr pkt, CacheBlk *blk, bool is_timing,
189511051Sandreas.hansson@arm.com                   bool is_deferred, bool pending_inval)
189611051Sandreas.hansson@arm.com{
189711288Ssteve.reinhardt@amd.com    DPRINTF(CacheVerbose, "%s for %s addr %#llx size %d\n", __func__,
189811051Sandreas.hansson@arm.com            pkt->cmdString(), pkt->getAddr(), pkt->getSize());
189911051Sandreas.hansson@arm.com    // deferred snoops can only happen in timing mode
190011051Sandreas.hansson@arm.com    assert(!(is_deferred && !is_timing));
190111051Sandreas.hansson@arm.com    // pending_inval only makes sense on deferred snoops
190211051Sandreas.hansson@arm.com    assert(!(pending_inval && !is_deferred));
190311051Sandreas.hansson@arm.com    assert(pkt->isRequest());
190411051Sandreas.hansson@arm.com
190511051Sandreas.hansson@arm.com    // the packet may get modified if we or a forwarded snooper
190611051Sandreas.hansson@arm.com    // responds in atomic mode, so remember a few things about the
190711051Sandreas.hansson@arm.com    // original packet up front
190811051Sandreas.hansson@arm.com    bool invalidate = pkt->isInvalidate();
190911284Sandreas.hansson@arm.com    bool M5_VAR_USED needs_writable = pkt->needsWritable();
191011051Sandreas.hansson@arm.com
191111285Sandreas.hansson@arm.com    // at the moment we could get an uncacheable write which does not
191211285Sandreas.hansson@arm.com    // have the invalidate flag, and we need a suitable way of dealing
191311285Sandreas.hansson@arm.com    // with this case
191411285Sandreas.hansson@arm.com    panic_if(invalidate && pkt->req->isUncacheable(),
191511285Sandreas.hansson@arm.com             "%s got an invalidating uncacheable snoop request %s to %#llx",
191611285Sandreas.hansson@arm.com             name(), pkt->cmdString(), pkt->getAddr());
191711285Sandreas.hansson@arm.com
191811127Sandreas.hansson@arm.com    uint32_t snoop_delay = 0;
191911127Sandreas.hansson@arm.com
192011051Sandreas.hansson@arm.com    if (forwardSnoops) {
192111051Sandreas.hansson@arm.com        // first propagate snoop upward to see if anyone above us wants to
192211051Sandreas.hansson@arm.com        // handle it.  save & restore packet src since it will get
192311051Sandreas.hansson@arm.com        // rewritten to be relative to cpu-side bus (if any)
192411284Sandreas.hansson@arm.com        bool alreadyResponded = pkt->cacheResponding();
192511051Sandreas.hansson@arm.com        if (is_timing) {
192611051Sandreas.hansson@arm.com            // copy the packet so that we can clear any flags before
192711051Sandreas.hansson@arm.com            // forwarding it upwards, we also allocate data (passing
192811051Sandreas.hansson@arm.com            // the pointer along in case of static data), in case
192911051Sandreas.hansson@arm.com            // there is a snoop hit in upper levels
193011051Sandreas.hansson@arm.com            Packet snoopPkt(pkt, true, true);
193111051Sandreas.hansson@arm.com            snoopPkt.setExpressSnoop();
193211051Sandreas.hansson@arm.com            // the snoop packet does not need to wait any additional
193311051Sandreas.hansson@arm.com            // time
193411051Sandreas.hansson@arm.com            snoopPkt.headerDelay = snoopPkt.payloadDelay = 0;
193511051Sandreas.hansson@arm.com            cpuSidePort->sendTimingSnoopReq(&snoopPkt);
193611127Sandreas.hansson@arm.com
193711127Sandreas.hansson@arm.com            // add the header delay (including crossbar and snoop
193811127Sandreas.hansson@arm.com            // delays) of the upward snoop to the snoop delay for this
193911127Sandreas.hansson@arm.com            // cache
194011127Sandreas.hansson@arm.com            snoop_delay += snoopPkt.headerDelay;
194111127Sandreas.hansson@arm.com
194211284Sandreas.hansson@arm.com            if (snoopPkt.cacheResponding()) {
194311051Sandreas.hansson@arm.com                // cache-to-cache response from some upper cache
194411051Sandreas.hansson@arm.com                assert(!alreadyResponded);
194511284Sandreas.hansson@arm.com                pkt->setCacheResponding();
194611051Sandreas.hansson@arm.com            }
194711284Sandreas.hansson@arm.com            // upstream cache has the block, or has an outstanding
194811284Sandreas.hansson@arm.com            // MSHR, pass the flag on
194911284Sandreas.hansson@arm.com            if (snoopPkt.hasSharers()) {
195011284Sandreas.hansson@arm.com                pkt->setHasSharers();
195111051Sandreas.hansson@arm.com            }
195211051Sandreas.hansson@arm.com            // If this request is a prefetch or clean evict and an upper level
195311051Sandreas.hansson@arm.com            // signals block present, make sure to propagate the block
195411051Sandreas.hansson@arm.com            // presence to the requester.
195511051Sandreas.hansson@arm.com            if (snoopPkt.isBlockCached()) {
195611051Sandreas.hansson@arm.com                pkt->setBlockCached();
195711051Sandreas.hansson@arm.com            }
195811051Sandreas.hansson@arm.com        } else {
195911051Sandreas.hansson@arm.com            cpuSidePort->sendAtomicSnoop(pkt);
196011284Sandreas.hansson@arm.com            if (!alreadyResponded && pkt->cacheResponding()) {
196111051Sandreas.hansson@arm.com                // cache-to-cache response from some upper cache:
196211051Sandreas.hansson@arm.com                // forward response to original requester
196311051Sandreas.hansson@arm.com                assert(pkt->isResponse());
196411051Sandreas.hansson@arm.com            }
196511051Sandreas.hansson@arm.com        }
196611051Sandreas.hansson@arm.com    }
196711051Sandreas.hansson@arm.com
196811051Sandreas.hansson@arm.com    if (!blk || !blk->isValid()) {
196911493Sandreas.hansson@arm.com        if (is_deferred) {
197011493Sandreas.hansson@arm.com            // we no longer have the block, and will not respond, but a
197111493Sandreas.hansson@arm.com            // packet was allocated in MSHR::handleSnoop and we have
197211493Sandreas.hansson@arm.com            // to delete it
197311493Sandreas.hansson@arm.com            assert(pkt->needsResponse());
197411493Sandreas.hansson@arm.com
197511493Sandreas.hansson@arm.com            // we have passed the block to a cache upstream, that
197611493Sandreas.hansson@arm.com            // cache should be responding
197711493Sandreas.hansson@arm.com            assert(pkt->cacheResponding());
197811493Sandreas.hansson@arm.com
197911493Sandreas.hansson@arm.com            delete pkt;
198011493Sandreas.hansson@arm.com        }
198111493Sandreas.hansson@arm.com
198211288Ssteve.reinhardt@amd.com        DPRINTF(CacheVerbose, "%s snoop miss for %s addr %#llx size %d\n",
198311051Sandreas.hansson@arm.com                __func__, pkt->cmdString(), pkt->getAddr(), pkt->getSize());
198411127Sandreas.hansson@arm.com        return snoop_delay;
198511051Sandreas.hansson@arm.com    } else {
198611288Ssteve.reinhardt@amd.com        DPRINTF(Cache, "%s snoop hit for %s addr %#llx size %d, "
198711288Ssteve.reinhardt@amd.com                "old state is %s\n", __func__, pkt->cmdString(),
198811288Ssteve.reinhardt@amd.com                pkt->getAddr(), pkt->getSize(), blk->print());
198911051Sandreas.hansson@arm.com    }
199011051Sandreas.hansson@arm.com
199111051Sandreas.hansson@arm.com    chatty_assert(!(isReadOnly && blk->isDirty()),
199211051Sandreas.hansson@arm.com                  "Should never have a dirty block in a read-only cache %s\n",
199311051Sandreas.hansson@arm.com                  name());
199411051Sandreas.hansson@arm.com
199511051Sandreas.hansson@arm.com    // We may end up modifying both the block state and the packet (if
199611051Sandreas.hansson@arm.com    // we respond in atomic mode), so just figure out what to do now
199711051Sandreas.hansson@arm.com    // and then do it later. If we find dirty data while snooping for
199811051Sandreas.hansson@arm.com    // an invalidate, we don't need to send a response. The
199911051Sandreas.hansson@arm.com    // invalidation itself is taken care of below.
200011051Sandreas.hansson@arm.com    bool respond = blk->isDirty() && pkt->needsResponse() &&
200111051Sandreas.hansson@arm.com        pkt->cmd != MemCmd::InvalidateReq;
200211284Sandreas.hansson@arm.com    bool have_writable = blk->isWritable();
200311051Sandreas.hansson@arm.com
200411051Sandreas.hansson@arm.com    // Invalidate any prefetch's from below that would strip write permissions
200511051Sandreas.hansson@arm.com    // MemCmd::HardPFReq is only observed by upstream caches.  After missing
200611051Sandreas.hansson@arm.com    // above and in it's own cache, a new MemCmd::ReadReq is created that
200711051Sandreas.hansson@arm.com    // downstream caches observe.
200811051Sandreas.hansson@arm.com    if (pkt->mustCheckAbove()) {
200911483Snikos.nikoleris@arm.com        DPRINTF(Cache, "Found addr %#llx in upper level cache for snoop %s "
201011483Snikos.nikoleris@arm.com                "from lower cache\n", pkt->getAddr(), pkt->cmdString());
201111051Sandreas.hansson@arm.com        pkt->setBlockCached();
201211127Sandreas.hansson@arm.com        return snoop_delay;
201311051Sandreas.hansson@arm.com    }
201411051Sandreas.hansson@arm.com
201511285Sandreas.hansson@arm.com    if (pkt->isRead() && !invalidate) {
201611285Sandreas.hansson@arm.com        // reading without requiring the line in a writable state
201711284Sandreas.hansson@arm.com        assert(!needs_writable);
201811284Sandreas.hansson@arm.com        pkt->setHasSharers();
201911285Sandreas.hansson@arm.com
202011285Sandreas.hansson@arm.com        // if the requesting packet is uncacheable, retain the line in
202111285Sandreas.hansson@arm.com        // the current state, otherwhise unset the writable flag,
202211285Sandreas.hansson@arm.com        // which means we go from Modified to Owned (and will respond
202311285Sandreas.hansson@arm.com        // below), remain in Owned (and will respond below), from
202411285Sandreas.hansson@arm.com        // Exclusive to Shared, or remain in Shared
202511285Sandreas.hansson@arm.com        if (!pkt->req->isUncacheable())
202611285Sandreas.hansson@arm.com            blk->status &= ~BlkWritable;
202711051Sandreas.hansson@arm.com    }
202811051Sandreas.hansson@arm.com
202911051Sandreas.hansson@arm.com    if (respond) {
203011051Sandreas.hansson@arm.com        // prevent anyone else from responding, cache as well as
203111051Sandreas.hansson@arm.com        // memory, and also prevent any memory from even seeing the
203211284Sandreas.hansson@arm.com        // request
203311284Sandreas.hansson@arm.com        pkt->setCacheResponding();
203411284Sandreas.hansson@arm.com        if (have_writable) {
203511284Sandreas.hansson@arm.com            // inform the cache hierarchy that this cache had the line
203611284Sandreas.hansson@arm.com            // in the Modified state so that we avoid unnecessary
203711284Sandreas.hansson@arm.com            // invalidations (see Packet::setResponderHadWritable)
203811284Sandreas.hansson@arm.com            pkt->setResponderHadWritable();
203911284Sandreas.hansson@arm.com
204011081Sandreas.hansson@arm.com            // in the case of an uncacheable request there is no point
204111284Sandreas.hansson@arm.com            // in setting the responderHadWritable flag, but since the
204211284Sandreas.hansson@arm.com            // recipient does not care there is no harm in doing so
204311284Sandreas.hansson@arm.com        } else {
204411284Sandreas.hansson@arm.com            // if the packet has needsWritable set we invalidate our
204511284Sandreas.hansson@arm.com            // copy below and all other copies will be invalidates
204611284Sandreas.hansson@arm.com            // through express snoops, and if needsWritable is not set
204711284Sandreas.hansson@arm.com            // we already called setHasSharers above
204811051Sandreas.hansson@arm.com        }
204911284Sandreas.hansson@arm.com
205011285Sandreas.hansson@arm.com        // if we are returning a writable and dirty (Modified) line,
205111285Sandreas.hansson@arm.com        // we should be invalidating the line
205211285Sandreas.hansson@arm.com        panic_if(!invalidate && !pkt->hasSharers(),
205311285Sandreas.hansson@arm.com                 "%s is passing a Modified line through %s to %#llx, "
205411285Sandreas.hansson@arm.com                 "but keeping the block",
205511285Sandreas.hansson@arm.com                 name(), pkt->cmdString(), pkt->getAddr());
205611285Sandreas.hansson@arm.com
205711051Sandreas.hansson@arm.com        if (is_timing) {
205811051Sandreas.hansson@arm.com            doTimingSupplyResponse(pkt, blk->data, is_deferred, pending_inval);
205911051Sandreas.hansson@arm.com        } else {
206011051Sandreas.hansson@arm.com            pkt->makeAtomicResponse();
206111286Sandreas.hansson@arm.com            // packets such as upgrades do not actually have any data
206211286Sandreas.hansson@arm.com            // payload
206311286Sandreas.hansson@arm.com            if (pkt->hasData())
206411286Sandreas.hansson@arm.com                pkt->setDataFromBlock(blk->data, blkSize);
206511051Sandreas.hansson@arm.com        }
206611051Sandreas.hansson@arm.com    }
206711051Sandreas.hansson@arm.com
206811602Sandreas.hansson@arm.com    if (!respond && is_deferred) {
206911051Sandreas.hansson@arm.com        assert(pkt->needsResponse());
207011602Sandreas.hansson@arm.com
207111602Sandreas.hansson@arm.com        // if we copied the deferred packet with the intention to
207211602Sandreas.hansson@arm.com        // respond, but are not responding, then a cache above us must
207311602Sandreas.hansson@arm.com        // be, and we can use this as the indication of whether this
207411602Sandreas.hansson@arm.com        // is a packet where we created a copy of the request or not
207511602Sandreas.hansson@arm.com        if (!pkt->cacheResponding()) {
207611602Sandreas.hansson@arm.com            delete pkt->req;
207711602Sandreas.hansson@arm.com        }
207811602Sandreas.hansson@arm.com
207911051Sandreas.hansson@arm.com        delete pkt;
208011051Sandreas.hansson@arm.com    }
208111051Sandreas.hansson@arm.com
208211051Sandreas.hansson@arm.com    // Do this last in case it deallocates block data or something
208311051Sandreas.hansson@arm.com    // like that
208411051Sandreas.hansson@arm.com    if (invalidate) {
208511197Sandreas.hansson@arm.com        invalidateBlock(blk);
208611051Sandreas.hansson@arm.com    }
208711051Sandreas.hansson@arm.com
208811051Sandreas.hansson@arm.com    DPRINTF(Cache, "new state is %s\n", blk->print());
208911127Sandreas.hansson@arm.com
209011127Sandreas.hansson@arm.com    return snoop_delay;
209111051Sandreas.hansson@arm.com}
209211051Sandreas.hansson@arm.com
209311051Sandreas.hansson@arm.com
209411051Sandreas.hansson@arm.comvoid
209511051Sandreas.hansson@arm.comCache::recvTimingSnoopReq(PacketPtr pkt)
209611051Sandreas.hansson@arm.com{
209711288Ssteve.reinhardt@amd.com    DPRINTF(CacheVerbose, "%s for %s addr %#llx size %d\n", __func__,
209811051Sandreas.hansson@arm.com            pkt->cmdString(), pkt->getAddr(), pkt->getSize());
209911051Sandreas.hansson@arm.com
210011051Sandreas.hansson@arm.com    // Snoops shouldn't happen when bypassing caches
210111051Sandreas.hansson@arm.com    assert(!system->bypassCaches());
210211051Sandreas.hansson@arm.com
210311130Sali.jafri@arm.com    // no need to snoop requests that are not in range
210411051Sandreas.hansson@arm.com    if (!inRange(pkt->getAddr())) {
210511051Sandreas.hansson@arm.com        return;
210611051Sandreas.hansson@arm.com    }
210711051Sandreas.hansson@arm.com
210811051Sandreas.hansson@arm.com    bool is_secure = pkt->isSecure();
210911051Sandreas.hansson@arm.com    CacheBlk *blk = tags->findBlock(pkt->getAddr(), is_secure);
211011051Sandreas.hansson@arm.com
211111051Sandreas.hansson@arm.com    Addr blk_addr = blockAlign(pkt->getAddr());
211211051Sandreas.hansson@arm.com    MSHR *mshr = mshrQueue.findMatch(blk_addr, is_secure);
211311051Sandreas.hansson@arm.com
211411127Sandreas.hansson@arm.com    // Update the latency cost of the snoop so that the crossbar can
211511127Sandreas.hansson@arm.com    // account for it. Do not overwrite what other neighbouring caches
211611127Sandreas.hansson@arm.com    // have already done, rather take the maximum. The update is
211711127Sandreas.hansson@arm.com    // tentative, for cases where we return before an upward snoop
211811127Sandreas.hansson@arm.com    // happens below.
211911127Sandreas.hansson@arm.com    pkt->snoopDelay = std::max<uint32_t>(pkt->snoopDelay,
212011127Sandreas.hansson@arm.com                                         lookupLatency * clockPeriod());
212111127Sandreas.hansson@arm.com
212211051Sandreas.hansson@arm.com    // Inform request(Prefetch, CleanEvict or Writeback) from below of
212311051Sandreas.hansson@arm.com    // MSHR hit, set setBlockCached.
212411051Sandreas.hansson@arm.com    if (mshr && pkt->mustCheckAbove()) {
212511051Sandreas.hansson@arm.com        DPRINTF(Cache, "Setting block cached for %s from"
212611051Sandreas.hansson@arm.com                "lower cache on mshr hit %#x\n",
212711051Sandreas.hansson@arm.com                pkt->cmdString(), pkt->getAddr());
212811051Sandreas.hansson@arm.com        pkt->setBlockCached();
212911051Sandreas.hansson@arm.com        return;
213011051Sandreas.hansson@arm.com    }
213111051Sandreas.hansson@arm.com
213211051Sandreas.hansson@arm.com    // Let the MSHR itself track the snoop and decide whether we want
213311051Sandreas.hansson@arm.com    // to go ahead and do the regular cache snoop
213411051Sandreas.hansson@arm.com    if (mshr && mshr->handleSnoop(pkt, order++)) {
213511051Sandreas.hansson@arm.com        DPRINTF(Cache, "Deferring snoop on in-service MSHR to blk %#llx (%s)."
213611051Sandreas.hansson@arm.com                "mshrs: %s\n", blk_addr, is_secure ? "s" : "ns",
213711051Sandreas.hansson@arm.com                mshr->print());
213811051Sandreas.hansson@arm.com
213911051Sandreas.hansson@arm.com        if (mshr->getNumTargets() > numTarget)
214011051Sandreas.hansson@arm.com            warn("allocating bonus target for snoop"); //handle later
214111051Sandreas.hansson@arm.com        return;
214211051Sandreas.hansson@arm.com    }
214311051Sandreas.hansson@arm.com
214411051Sandreas.hansson@arm.com    //We also need to check the writeback buffers and handle those
214511375Sandreas.hansson@arm.com    WriteQueueEntry *wb_entry = writeBuffer.findMatch(blk_addr, is_secure);
214611375Sandreas.hansson@arm.com    if (wb_entry) {
214711051Sandreas.hansson@arm.com        DPRINTF(Cache, "Snoop hit in writeback to addr %#llx (%s)\n",
214811051Sandreas.hansson@arm.com                pkt->getAddr(), is_secure ? "s" : "ns");
214911051Sandreas.hansson@arm.com        // Expect to see only Writebacks and/or CleanEvicts here, both of
215011051Sandreas.hansson@arm.com        // which should not be generated for uncacheable data.
215111051Sandreas.hansson@arm.com        assert(!wb_entry->isUncacheable());
215211051Sandreas.hansson@arm.com        // There should only be a single request responsible for generating
215311051Sandreas.hansson@arm.com        // Writebacks/CleanEvicts.
215411051Sandreas.hansson@arm.com        assert(wb_entry->getNumTargets() == 1);
215511051Sandreas.hansson@arm.com        PacketPtr wb_pkt = wb_entry->getTarget()->pkt;
215611199Sandreas.hansson@arm.com        assert(wb_pkt->isEviction());
215711051Sandreas.hansson@arm.com
215811199Sandreas.hansson@arm.com        if (pkt->isEviction()) {
215911051Sandreas.hansson@arm.com            // if the block is found in the write queue, set the BLOCK_CACHED
216011051Sandreas.hansson@arm.com            // flag for Writeback/CleanEvict snoop. On return the snoop will
216111051Sandreas.hansson@arm.com            // propagate the BLOCK_CACHED flag in Writeback packets and prevent
216211051Sandreas.hansson@arm.com            // any CleanEvicts from travelling down the memory hierarchy.
216311051Sandreas.hansson@arm.com            pkt->setBlockCached();
216411051Sandreas.hansson@arm.com            DPRINTF(Cache, "Squashing %s from lower cache on writequeue hit"
216511051Sandreas.hansson@arm.com                    " %#x\n", pkt->cmdString(), pkt->getAddr());
216611051Sandreas.hansson@arm.com            return;
216711051Sandreas.hansson@arm.com        }
216811051Sandreas.hansson@arm.com
216911332Sandreas.hansson@arm.com        // conceptually writebacks are no different to other blocks in
217011332Sandreas.hansson@arm.com        // this cache, so the behaviour is modelled after handleSnoop,
217111332Sandreas.hansson@arm.com        // the difference being that instead of querying the block
217211332Sandreas.hansson@arm.com        // state to determine if it is dirty and writable, we use the
217311332Sandreas.hansson@arm.com        // command and fields of the writeback packet
217411332Sandreas.hansson@arm.com        bool respond = wb_pkt->cmd == MemCmd::WritebackDirty &&
217511332Sandreas.hansson@arm.com            pkt->needsResponse() && pkt->cmd != MemCmd::InvalidateReq;
217611332Sandreas.hansson@arm.com        bool have_writable = !wb_pkt->hasSharers();
217711332Sandreas.hansson@arm.com        bool invalidate = pkt->isInvalidate();
217811332Sandreas.hansson@arm.com
217911332Sandreas.hansson@arm.com        if (!pkt->req->isUncacheable() && pkt->isRead() && !invalidate) {
218011332Sandreas.hansson@arm.com            assert(!pkt->needsWritable());
218111332Sandreas.hansson@arm.com            pkt->setHasSharers();
218211332Sandreas.hansson@arm.com            wb_pkt->setHasSharers();
218311332Sandreas.hansson@arm.com        }
218411332Sandreas.hansson@arm.com
218511332Sandreas.hansson@arm.com        if (respond) {
218611284Sandreas.hansson@arm.com            pkt->setCacheResponding();
218711332Sandreas.hansson@arm.com
218811332Sandreas.hansson@arm.com            if (have_writable) {
218911332Sandreas.hansson@arm.com                pkt->setResponderHadWritable();
219011051Sandreas.hansson@arm.com            }
219111332Sandreas.hansson@arm.com
219211051Sandreas.hansson@arm.com            doTimingSupplyResponse(pkt, wb_pkt->getConstPtr<uint8_t>(),
219311051Sandreas.hansson@arm.com                                   false, false);
219411051Sandreas.hansson@arm.com        }
219511051Sandreas.hansson@arm.com
219611332Sandreas.hansson@arm.com        if (invalidate) {
219711051Sandreas.hansson@arm.com            // Invalidation trumps our writeback... discard here
219811051Sandreas.hansson@arm.com            // Note: markInService will remove entry from writeback buffer.
219911375Sandreas.hansson@arm.com            markInService(wb_entry);
220011051Sandreas.hansson@arm.com            delete wb_pkt;
220111051Sandreas.hansson@arm.com        }
220211051Sandreas.hansson@arm.com    }
220311051Sandreas.hansson@arm.com
220411051Sandreas.hansson@arm.com    // If this was a shared writeback, there may still be
220511051Sandreas.hansson@arm.com    // other shared copies above that require invalidation.
220611051Sandreas.hansson@arm.com    // We could be more selective and return here if the
220711051Sandreas.hansson@arm.com    // request is non-exclusive or if the writeback is
220811051Sandreas.hansson@arm.com    // exclusive.
220911127Sandreas.hansson@arm.com    uint32_t snoop_delay = handleSnoop(pkt, blk, true, false, false);
221011127Sandreas.hansson@arm.com
221111127Sandreas.hansson@arm.com    // Override what we did when we first saw the snoop, as we now
221211127Sandreas.hansson@arm.com    // also have the cost of the upwards snoops to account for
221311127Sandreas.hansson@arm.com    pkt->snoopDelay = std::max<uint32_t>(pkt->snoopDelay, snoop_delay +
221411127Sandreas.hansson@arm.com                                         lookupLatency * clockPeriod());
221511051Sandreas.hansson@arm.com}
221611051Sandreas.hansson@arm.com
221711051Sandreas.hansson@arm.combool
221811051Sandreas.hansson@arm.comCache::CpuSidePort::recvTimingSnoopResp(PacketPtr pkt)
221911051Sandreas.hansson@arm.com{
222011051Sandreas.hansson@arm.com    // Express snoop responses from master to slave, e.g., from L1 to L2
222111051Sandreas.hansson@arm.com    cache->recvTimingSnoopResp(pkt);
222211051Sandreas.hansson@arm.com    return true;
222311051Sandreas.hansson@arm.com}
222411051Sandreas.hansson@arm.com
222511051Sandreas.hansson@arm.comTick
222611051Sandreas.hansson@arm.comCache::recvAtomicSnoop(PacketPtr pkt)
222711051Sandreas.hansson@arm.com{
222811051Sandreas.hansson@arm.com    // Snoops shouldn't happen when bypassing caches
222911051Sandreas.hansson@arm.com    assert(!system->bypassCaches());
223011051Sandreas.hansson@arm.com
223111130Sali.jafri@arm.com    // no need to snoop requests that are not in range.
223211130Sali.jafri@arm.com    if (!inRange(pkt->getAddr())) {
223311051Sandreas.hansson@arm.com        return 0;
223411051Sandreas.hansson@arm.com    }
223511051Sandreas.hansson@arm.com
223611051Sandreas.hansson@arm.com    CacheBlk *blk = tags->findBlock(pkt->getAddr(), pkt->isSecure());
223711127Sandreas.hansson@arm.com    uint32_t snoop_delay = handleSnoop(pkt, blk, false, false, false);
223811127Sandreas.hansson@arm.com    return snoop_delay + lookupLatency * clockPeriod();
223911051Sandreas.hansson@arm.com}
224011051Sandreas.hansson@arm.com
224111051Sandreas.hansson@arm.com
224211375Sandreas.hansson@arm.comQueueEntry*
224311375Sandreas.hansson@arm.comCache::getNextQueueEntry()
224411051Sandreas.hansson@arm.com{
224511051Sandreas.hansson@arm.com    // Check both MSHR queue and write buffer for potential requests,
224611051Sandreas.hansson@arm.com    // note that null does not mean there is no request, it could
224711051Sandreas.hansson@arm.com    // simply be that it is not ready
224811375Sandreas.hansson@arm.com    MSHR *miss_mshr  = mshrQueue.getNext();
224911375Sandreas.hansson@arm.com    WriteQueueEntry *wq_entry = writeBuffer.getNext();
225011051Sandreas.hansson@arm.com
225111051Sandreas.hansson@arm.com    // If we got a write buffer request ready, first priority is a
225211453Sandreas.hansson@arm.com    // full write buffer, otherwise we favour the miss requests
225311453Sandreas.hansson@arm.com    if (wq_entry && (writeBuffer.isFull() || !miss_mshr)) {
225411051Sandreas.hansson@arm.com        // need to search MSHR queue for conflicting earlier miss.
225511051Sandreas.hansson@arm.com        MSHR *conflict_mshr =
225611375Sandreas.hansson@arm.com            mshrQueue.findPending(wq_entry->blkAddr,
225711375Sandreas.hansson@arm.com                                  wq_entry->isSecure);
225811375Sandreas.hansson@arm.com
225911375Sandreas.hansson@arm.com        if (conflict_mshr && conflict_mshr->order < wq_entry->order) {
226011051Sandreas.hansson@arm.com            // Service misses in order until conflict is cleared.
226111051Sandreas.hansson@arm.com            return conflict_mshr;
226211051Sandreas.hansson@arm.com
226311051Sandreas.hansson@arm.com            // @todo Note that we ignore the ready time of the conflict here
226411051Sandreas.hansson@arm.com        }
226511051Sandreas.hansson@arm.com
226611051Sandreas.hansson@arm.com        // No conflicts; issue write
226711375Sandreas.hansson@arm.com        return wq_entry;
226811051Sandreas.hansson@arm.com    } else if (miss_mshr) {
226911051Sandreas.hansson@arm.com        // need to check for conflicting earlier writeback
227011375Sandreas.hansson@arm.com        WriteQueueEntry *conflict_mshr =
227111051Sandreas.hansson@arm.com            writeBuffer.findPending(miss_mshr->blkAddr,
227211051Sandreas.hansson@arm.com                                    miss_mshr->isSecure);
227311051Sandreas.hansson@arm.com        if (conflict_mshr) {
227411051Sandreas.hansson@arm.com            // not sure why we don't check order here... it was in the
227511051Sandreas.hansson@arm.com            // original code but commented out.
227611051Sandreas.hansson@arm.com
227711051Sandreas.hansson@arm.com            // The only way this happens is if we are
227811051Sandreas.hansson@arm.com            // doing a write and we didn't have permissions
227911051Sandreas.hansson@arm.com            // then subsequently saw a writeback (owned got evicted)
228011051Sandreas.hansson@arm.com            // We need to make sure to perform the writeback first
228111051Sandreas.hansson@arm.com            // To preserve the dirty data, then we can issue the write
228211051Sandreas.hansson@arm.com
228311375Sandreas.hansson@arm.com            // should we return wq_entry here instead?  I.e. do we
228411051Sandreas.hansson@arm.com            // have to flush writes in order?  I don't think so... not
228511051Sandreas.hansson@arm.com            // for Alpha anyway.  Maybe for x86?
228611051Sandreas.hansson@arm.com            return conflict_mshr;
228711051Sandreas.hansson@arm.com
228811051Sandreas.hansson@arm.com            // @todo Note that we ignore the ready time of the conflict here
228911051Sandreas.hansson@arm.com        }
229011051Sandreas.hansson@arm.com
229111051Sandreas.hansson@arm.com        // No conflicts; issue read
229211051Sandreas.hansson@arm.com        return miss_mshr;
229311051Sandreas.hansson@arm.com    }
229411051Sandreas.hansson@arm.com
229511051Sandreas.hansson@arm.com    // fall through... no pending requests.  Try a prefetch.
229611375Sandreas.hansson@arm.com    assert(!miss_mshr && !wq_entry);
229711051Sandreas.hansson@arm.com    if (prefetcher && mshrQueue.canPrefetch()) {
229811051Sandreas.hansson@arm.com        // If we have a miss queue slot, we can try a prefetch
229911051Sandreas.hansson@arm.com        PacketPtr pkt = prefetcher->getPacket();
230011051Sandreas.hansson@arm.com        if (pkt) {
230111051Sandreas.hansson@arm.com            Addr pf_addr = blockAlign(pkt->getAddr());
230211051Sandreas.hansson@arm.com            if (!tags->findBlock(pf_addr, pkt->isSecure()) &&
230311051Sandreas.hansson@arm.com                !mshrQueue.findMatch(pf_addr, pkt->isSecure()) &&
230411051Sandreas.hansson@arm.com                !writeBuffer.findMatch(pf_addr, pkt->isSecure())) {
230511051Sandreas.hansson@arm.com                // Update statistic on number of prefetches issued
230611051Sandreas.hansson@arm.com                // (hwpf_mshr_misses)
230711051Sandreas.hansson@arm.com                assert(pkt->req->masterId() < system->maxMasters());
230811051Sandreas.hansson@arm.com                mshr_misses[pkt->cmdToIndex()][pkt->req->masterId()]++;
230911051Sandreas.hansson@arm.com
231011051Sandreas.hansson@arm.com                // allocate an MSHR and return it, note
231111051Sandreas.hansson@arm.com                // that we send the packet straight away, so do not
231211051Sandreas.hansson@arm.com                // schedule the send
231311051Sandreas.hansson@arm.com                return allocateMissBuffer(pkt, curTick(), false);
231411051Sandreas.hansson@arm.com            } else {
231511051Sandreas.hansson@arm.com                // free the request and packet
231611051Sandreas.hansson@arm.com                delete pkt->req;
231711051Sandreas.hansson@arm.com                delete pkt;
231811051Sandreas.hansson@arm.com            }
231911051Sandreas.hansson@arm.com        }
232011051Sandreas.hansson@arm.com    }
232111051Sandreas.hansson@arm.com
232211375Sandreas.hansson@arm.com    return nullptr;
232311051Sandreas.hansson@arm.com}
232411051Sandreas.hansson@arm.com
232511051Sandreas.hansson@arm.combool
232611130Sali.jafri@arm.comCache::isCachedAbove(PacketPtr pkt, bool is_timing) const
232711051Sandreas.hansson@arm.com{
232811051Sandreas.hansson@arm.com    if (!forwardSnoops)
232911051Sandreas.hansson@arm.com        return false;
233011051Sandreas.hansson@arm.com    // Mirroring the flow of HardPFReqs, the cache sends CleanEvict and
233111051Sandreas.hansson@arm.com    // Writeback snoops into upper level caches to check for copies of the
233211051Sandreas.hansson@arm.com    // same block. Using the BLOCK_CACHED flag with the Writeback/CleanEvict
233311051Sandreas.hansson@arm.com    // packet, the cache can inform the crossbar below of presence or absence
233411051Sandreas.hansson@arm.com    // of the block.
233511130Sali.jafri@arm.com    if (is_timing) {
233611130Sali.jafri@arm.com        Packet snoop_pkt(pkt, true, false);
233711130Sali.jafri@arm.com        snoop_pkt.setExpressSnoop();
233811130Sali.jafri@arm.com        // Assert that packet is either Writeback or CleanEvict and not a
233911130Sali.jafri@arm.com        // prefetch request because prefetch requests need an MSHR and may
234011130Sali.jafri@arm.com        // generate a snoop response.
234111199Sandreas.hansson@arm.com        assert(pkt->isEviction());
234211484Snikos.nikoleris@arm.com        snoop_pkt.senderState = nullptr;
234311130Sali.jafri@arm.com        cpuSidePort->sendTimingSnoopReq(&snoop_pkt);
234411130Sali.jafri@arm.com        // Writeback/CleanEvict snoops do not generate a snoop response.
234511284Sandreas.hansson@arm.com        assert(!(snoop_pkt.cacheResponding()));
234611130Sali.jafri@arm.com        return snoop_pkt.isBlockCached();
234711130Sali.jafri@arm.com    } else {
234811130Sali.jafri@arm.com        cpuSidePort->sendAtomicSnoop(pkt);
234911130Sali.jafri@arm.com        return pkt->isBlockCached();
235011130Sali.jafri@arm.com    }
235111051Sandreas.hansson@arm.com}
235211051Sandreas.hansson@arm.com
235311375Sandreas.hansson@arm.comTick
235411375Sandreas.hansson@arm.comCache::nextQueueReadyTime() const
235511051Sandreas.hansson@arm.com{
235611375Sandreas.hansson@arm.com    Tick nextReady = std::min(mshrQueue.nextReadyTime(),
235711375Sandreas.hansson@arm.com                              writeBuffer.nextReadyTime());
235811375Sandreas.hansson@arm.com
235911375Sandreas.hansson@arm.com    // Don't signal prefetch ready time if no MSHRs available
236011375Sandreas.hansson@arm.com    // Will signal once enoguh MSHRs are deallocated
236111375Sandreas.hansson@arm.com    if (prefetcher && mshrQueue.canPrefetch()) {
236211375Sandreas.hansson@arm.com        nextReady = std::min(nextReady,
236311375Sandreas.hansson@arm.com                             prefetcher->nextPrefetchReadyTime());
236411051Sandreas.hansson@arm.com    }
236511051Sandreas.hansson@arm.com
236611375Sandreas.hansson@arm.com    return nextReady;
236711375Sandreas.hansson@arm.com}
236811375Sandreas.hansson@arm.com
236911375Sandreas.hansson@arm.combool
237011375Sandreas.hansson@arm.comCache::sendMSHRQueuePacket(MSHR* mshr)
237111375Sandreas.hansson@arm.com{
237211375Sandreas.hansson@arm.com    assert(mshr);
237311375Sandreas.hansson@arm.com
237411051Sandreas.hansson@arm.com    // use request from 1st target
237511051Sandreas.hansson@arm.com    PacketPtr tgt_pkt = mshr->getTarget()->pkt;
237611375Sandreas.hansson@arm.com
237711375Sandreas.hansson@arm.com    DPRINTF(Cache, "%s MSHR %s for addr %#llx size %d\n", __func__,
237811375Sandreas.hansson@arm.com            tgt_pkt->cmdString(), tgt_pkt->getAddr(),
237911375Sandreas.hansson@arm.com            tgt_pkt->getSize());
238011051Sandreas.hansson@arm.com
238111051Sandreas.hansson@arm.com    CacheBlk *blk = tags->findBlock(mshr->blkAddr, mshr->isSecure);
238211051Sandreas.hansson@arm.com
238311051Sandreas.hansson@arm.com    if (tgt_pkt->cmd == MemCmd::HardPFReq && forwardSnoops) {
238411375Sandreas.hansson@arm.com        // we should never have hardware prefetches to allocated
238511375Sandreas.hansson@arm.com        // blocks
238611484Snikos.nikoleris@arm.com        assert(blk == nullptr);
238711375Sandreas.hansson@arm.com
238811051Sandreas.hansson@arm.com        // We need to check the caches above us to verify that
238911051Sandreas.hansson@arm.com        // they don't have a copy of this block in the dirty state
239011051Sandreas.hansson@arm.com        // at the moment. Without this check we could get a stale
239111051Sandreas.hansson@arm.com        // copy from memory that might get used in place of the
239211051Sandreas.hansson@arm.com        // dirty one.
239311051Sandreas.hansson@arm.com        Packet snoop_pkt(tgt_pkt, true, false);
239411051Sandreas.hansson@arm.com        snoop_pkt.setExpressSnoop();
239511275Sandreas.hansson@arm.com        // We are sending this packet upwards, but if it hits we will
239611275Sandreas.hansson@arm.com        // get a snoop response that we end up treating just like a
239711275Sandreas.hansson@arm.com        // normal response, hence it needs the MSHR as its sender
239811275Sandreas.hansson@arm.com        // state
239911051Sandreas.hansson@arm.com        snoop_pkt.senderState = mshr;
240011051Sandreas.hansson@arm.com        cpuSidePort->sendTimingSnoopReq(&snoop_pkt);
240111051Sandreas.hansson@arm.com
240211051Sandreas.hansson@arm.com        // Check to see if the prefetch was squashed by an upper cache (to
240311051Sandreas.hansson@arm.com        // prevent us from grabbing the line) or if a Check to see if a
240411051Sandreas.hansson@arm.com        // writeback arrived between the time the prefetch was placed in
240511051Sandreas.hansson@arm.com        // the MSHRs and when it was selected to be sent or if the
240611051Sandreas.hansson@arm.com        // prefetch was squashed by an upper cache.
240711051Sandreas.hansson@arm.com
240811284Sandreas.hansson@arm.com        // It is important to check cacheResponding before
240911284Sandreas.hansson@arm.com        // prefetchSquashed. If another cache has committed to
241011284Sandreas.hansson@arm.com        // responding, it will be sending a dirty response which will
241111284Sandreas.hansson@arm.com        // arrive at the MSHR allocated for this request. Checking the
241211284Sandreas.hansson@arm.com        // prefetchSquash first may result in the MSHR being
241311284Sandreas.hansson@arm.com        // prematurely deallocated.
241411284Sandreas.hansson@arm.com        if (snoop_pkt.cacheResponding()) {
241511276Sandreas.hansson@arm.com            auto M5_VAR_USED r = outstandingSnoop.insert(snoop_pkt.req);
241611276Sandreas.hansson@arm.com            assert(r.second);
241711284Sandreas.hansson@arm.com
241811284Sandreas.hansson@arm.com            // if we are getting a snoop response with no sharers it
241911284Sandreas.hansson@arm.com            // will be allocated as Modified
242011284Sandreas.hansson@arm.com            bool pending_modified_resp = !snoop_pkt.hasSharers();
242111284Sandreas.hansson@arm.com            markInService(mshr, pending_modified_resp);
242211284Sandreas.hansson@arm.com
242311051Sandreas.hansson@arm.com            DPRINTF(Cache, "Upward snoop of prefetch for addr"
242411051Sandreas.hansson@arm.com                    " %#x (%s) hit\n",
242511051Sandreas.hansson@arm.com                    tgt_pkt->getAddr(), tgt_pkt->isSecure()? "s": "ns");
242611375Sandreas.hansson@arm.com            return false;
242711051Sandreas.hansson@arm.com        }
242811051Sandreas.hansson@arm.com
242911375Sandreas.hansson@arm.com        if (snoop_pkt.isBlockCached()) {
243011051Sandreas.hansson@arm.com            DPRINTF(Cache, "Block present, prefetch squashed by cache.  "
243111051Sandreas.hansson@arm.com                    "Deallocating mshr target %#x.\n",
243211051Sandreas.hansson@arm.com                    mshr->blkAddr);
243311375Sandreas.hansson@arm.com
243411051Sandreas.hansson@arm.com            // Deallocate the mshr target
243511375Sandreas.hansson@arm.com            if (mshrQueue.forceDeallocateTarget(mshr)) {
243611277Sandreas.hansson@arm.com                // Clear block if this deallocation resulted freed an
243711277Sandreas.hansson@arm.com                // mshr when all had previously been utilized
243811375Sandreas.hansson@arm.com                clearBlocked(Blocked_NoMSHRs);
243911051Sandreas.hansson@arm.com            }
244011375Sandreas.hansson@arm.com            return false;
244111051Sandreas.hansson@arm.com        }
244211051Sandreas.hansson@arm.com    }
244311051Sandreas.hansson@arm.com
244411375Sandreas.hansson@arm.com    // either a prefetch that is not present upstream, or a normal
244511375Sandreas.hansson@arm.com    // MSHR request, proceed to get the packet to send downstream
244611452Sandreas.hansson@arm.com    PacketPtr pkt = createMissPacket(tgt_pkt, blk, mshr->needsWritable());
244711375Sandreas.hansson@arm.com
244811484Snikos.nikoleris@arm.com    mshr->isForward = (pkt == nullptr);
244911375Sandreas.hansson@arm.com
245011375Sandreas.hansson@arm.com    if (mshr->isForward) {
245111375Sandreas.hansson@arm.com        // not a cache block request, but a response is expected
245211375Sandreas.hansson@arm.com        // make copy of current packet to forward, keep current
245311375Sandreas.hansson@arm.com        // copy for response handling
245411375Sandreas.hansson@arm.com        pkt = new Packet(tgt_pkt, false, true);
245511375Sandreas.hansson@arm.com        assert(!pkt->isWrite());
245611375Sandreas.hansson@arm.com    }
245711375Sandreas.hansson@arm.com
245811375Sandreas.hansson@arm.com    // play it safe and append (rather than set) the sender state,
245911375Sandreas.hansson@arm.com    // as forwarded packets may already have existing state
246011375Sandreas.hansson@arm.com    pkt->pushSenderState(mshr);
246111375Sandreas.hansson@arm.com
246211375Sandreas.hansson@arm.com    if (!memSidePort->sendTimingReq(pkt)) {
246311375Sandreas.hansson@arm.com        // we are awaiting a retry, but we
246411375Sandreas.hansson@arm.com        // delete the packet and will be creating a new packet
246511375Sandreas.hansson@arm.com        // when we get the opportunity
246611375Sandreas.hansson@arm.com        delete pkt;
246711375Sandreas.hansson@arm.com
246811375Sandreas.hansson@arm.com        // note that we have now masked any requestBus and
246911375Sandreas.hansson@arm.com        // schedSendEvent (we will wait for a retry before
247011375Sandreas.hansson@arm.com        // doing anything), and this is so even if we do not
247111375Sandreas.hansson@arm.com        // care about this packet and might override it before
247211375Sandreas.hansson@arm.com        // it gets retried
247311375Sandreas.hansson@arm.com        return true;
247411375Sandreas.hansson@arm.com    } else {
247511375Sandreas.hansson@arm.com        // As part of the call to sendTimingReq the packet is
247611375Sandreas.hansson@arm.com        // forwarded to all neighbouring caches (and any caches
247711375Sandreas.hansson@arm.com        // above them) as a snoop. Thus at this point we know if
247811375Sandreas.hansson@arm.com        // any of the neighbouring caches are responding, and if
247911375Sandreas.hansson@arm.com        // so, we know it is dirty, and we can determine if it is
248011375Sandreas.hansson@arm.com        // being passed as Modified, making our MSHR the ordering
248111375Sandreas.hansson@arm.com        // point
248211375Sandreas.hansson@arm.com        bool pending_modified_resp = !pkt->hasSharers() &&
248311375Sandreas.hansson@arm.com            pkt->cacheResponding();
248411375Sandreas.hansson@arm.com        markInService(mshr, pending_modified_resp);
248511375Sandreas.hansson@arm.com        return false;
248611375Sandreas.hansson@arm.com    }
248711375Sandreas.hansson@arm.com}
248811375Sandreas.hansson@arm.com
248911375Sandreas.hansson@arm.combool
249011375Sandreas.hansson@arm.comCache::sendWriteQueuePacket(WriteQueueEntry* wq_entry)
249111375Sandreas.hansson@arm.com{
249211375Sandreas.hansson@arm.com    assert(wq_entry);
249311375Sandreas.hansson@arm.com
249411375Sandreas.hansson@arm.com    // always a single target for write queue entries
249511375Sandreas.hansson@arm.com    PacketPtr tgt_pkt = wq_entry->getTarget()->pkt;
249611375Sandreas.hansson@arm.com
249711375Sandreas.hansson@arm.com    DPRINTF(Cache, "%s write %s for addr %#llx size %d\n", __func__,
249811375Sandreas.hansson@arm.com            tgt_pkt->cmdString(), tgt_pkt->getAddr(),
249911375Sandreas.hansson@arm.com            tgt_pkt->getSize());
250011375Sandreas.hansson@arm.com
250111453Sandreas.hansson@arm.com    // forward as is, both for evictions and uncacheable writes
250211453Sandreas.hansson@arm.com    if (!memSidePort->sendTimingReq(tgt_pkt)) {
250311375Sandreas.hansson@arm.com        // note that we have now masked any requestBus and
250411375Sandreas.hansson@arm.com        // schedSendEvent (we will wait for a retry before
250511375Sandreas.hansson@arm.com        // doing anything), and this is so even if we do not
250611375Sandreas.hansson@arm.com        // care about this packet and might override it before
250711375Sandreas.hansson@arm.com        // it gets retried
250811375Sandreas.hansson@arm.com        return true;
250911375Sandreas.hansson@arm.com    } else {
251011375Sandreas.hansson@arm.com        markInService(wq_entry);
251111375Sandreas.hansson@arm.com        return false;
251211051Sandreas.hansson@arm.com    }
251311051Sandreas.hansson@arm.com}
251411051Sandreas.hansson@arm.com
251511051Sandreas.hansson@arm.comvoid
251611051Sandreas.hansson@arm.comCache::serialize(CheckpointOut &cp) const
251711051Sandreas.hansson@arm.com{
251811051Sandreas.hansson@arm.com    bool dirty(isDirty());
251911051Sandreas.hansson@arm.com
252011051Sandreas.hansson@arm.com    if (dirty) {
252111051Sandreas.hansson@arm.com        warn("*** The cache still contains dirty data. ***\n");
252211051Sandreas.hansson@arm.com        warn("    Make sure to drain the system using the correct flags.\n");
252311483Snikos.nikoleris@arm.com        warn("    This checkpoint will not restore correctly and dirty data "
252411483Snikos.nikoleris@arm.com             "    in the cache will be lost!\n");
252511051Sandreas.hansson@arm.com    }
252611051Sandreas.hansson@arm.com
252711051Sandreas.hansson@arm.com    // Since we don't checkpoint the data in the cache, any dirty data
252811051Sandreas.hansson@arm.com    // will be lost when restoring from a checkpoint of a system that
252911051Sandreas.hansson@arm.com    // wasn't drained properly. Flag the checkpoint as invalid if the
253011051Sandreas.hansson@arm.com    // cache contains dirty data.
253111051Sandreas.hansson@arm.com    bool bad_checkpoint(dirty);
253211051Sandreas.hansson@arm.com    SERIALIZE_SCALAR(bad_checkpoint);
253311051Sandreas.hansson@arm.com}
253411051Sandreas.hansson@arm.com
253511051Sandreas.hansson@arm.comvoid
253611051Sandreas.hansson@arm.comCache::unserialize(CheckpointIn &cp)
253711051Sandreas.hansson@arm.com{
253811051Sandreas.hansson@arm.com    bool bad_checkpoint;
253911051Sandreas.hansson@arm.com    UNSERIALIZE_SCALAR(bad_checkpoint);
254011051Sandreas.hansson@arm.com    if (bad_checkpoint) {
254111051Sandreas.hansson@arm.com        fatal("Restoring from checkpoints with dirty caches is not supported "
254211051Sandreas.hansson@arm.com              "in the classic memory system. Please remove any caches or "
254311051Sandreas.hansson@arm.com              " drain them properly before taking checkpoints.\n");
254411051Sandreas.hansson@arm.com    }
254511051Sandreas.hansson@arm.com}
254611051Sandreas.hansson@arm.com
254711051Sandreas.hansson@arm.com///////////////
254811051Sandreas.hansson@arm.com//
254911051Sandreas.hansson@arm.com// CpuSidePort
255011051Sandreas.hansson@arm.com//
255111051Sandreas.hansson@arm.com///////////////
255211051Sandreas.hansson@arm.com
255311051Sandreas.hansson@arm.comAddrRangeList
255411051Sandreas.hansson@arm.comCache::CpuSidePort::getAddrRanges() const
255511051Sandreas.hansson@arm.com{
255611051Sandreas.hansson@arm.com    return cache->getAddrRanges();
255711051Sandreas.hansson@arm.com}
255811051Sandreas.hansson@arm.com
255911051Sandreas.hansson@arm.combool
256011051Sandreas.hansson@arm.comCache::CpuSidePort::recvTimingReq(PacketPtr pkt)
256111051Sandreas.hansson@arm.com{
256211051Sandreas.hansson@arm.com    assert(!cache->system->bypassCaches());
256311051Sandreas.hansson@arm.com
256411051Sandreas.hansson@arm.com    bool success = false;
256511051Sandreas.hansson@arm.com
256611334Sandreas.hansson@arm.com    // always let express snoop packets through if even if blocked
256711334Sandreas.hansson@arm.com    if (pkt->isExpressSnoop()) {
256811051Sandreas.hansson@arm.com        // do not change the current retry state
256911051Sandreas.hansson@arm.com        bool M5_VAR_USED bypass_success = cache->recvTimingReq(pkt);
257011051Sandreas.hansson@arm.com        assert(bypass_success);
257111051Sandreas.hansson@arm.com        return true;
257211051Sandreas.hansson@arm.com    } else if (blocked || mustSendRetry) {
257311051Sandreas.hansson@arm.com        // either already committed to send a retry, or blocked
257411051Sandreas.hansson@arm.com        success = false;
257511051Sandreas.hansson@arm.com    } else {
257611051Sandreas.hansson@arm.com        // pass it on to the cache, and let the cache decide if we
257711051Sandreas.hansson@arm.com        // have to retry or not
257811051Sandreas.hansson@arm.com        success = cache->recvTimingReq(pkt);
257911051Sandreas.hansson@arm.com    }
258011051Sandreas.hansson@arm.com
258111051Sandreas.hansson@arm.com    // remember if we have to retry
258211051Sandreas.hansson@arm.com    mustSendRetry = !success;
258311051Sandreas.hansson@arm.com    return success;
258411051Sandreas.hansson@arm.com}
258511051Sandreas.hansson@arm.com
258611051Sandreas.hansson@arm.comTick
258711051Sandreas.hansson@arm.comCache::CpuSidePort::recvAtomic(PacketPtr pkt)
258811051Sandreas.hansson@arm.com{
258911051Sandreas.hansson@arm.com    return cache->recvAtomic(pkt);
259011051Sandreas.hansson@arm.com}
259111051Sandreas.hansson@arm.com
259211051Sandreas.hansson@arm.comvoid
259311051Sandreas.hansson@arm.comCache::CpuSidePort::recvFunctional(PacketPtr pkt)
259411051Sandreas.hansson@arm.com{
259511051Sandreas.hansson@arm.com    // functional request
259611051Sandreas.hansson@arm.com    cache->functionalAccess(pkt, true);
259711051Sandreas.hansson@arm.com}
259811051Sandreas.hansson@arm.com
259911051Sandreas.hansson@arm.comCache::
260011051Sandreas.hansson@arm.comCpuSidePort::CpuSidePort(const std::string &_name, Cache *_cache,
260111051Sandreas.hansson@arm.com                         const std::string &_label)
260211051Sandreas.hansson@arm.com    : BaseCache::CacheSlavePort(_name, _cache, _label), cache(_cache)
260311051Sandreas.hansson@arm.com{
260411051Sandreas.hansson@arm.com}
260511051Sandreas.hansson@arm.com
260611053Sandreas.hansson@arm.comCache*
260711053Sandreas.hansson@arm.comCacheParams::create()
260811053Sandreas.hansson@arm.com{
260911053Sandreas.hansson@arm.com    assert(tags);
261011053Sandreas.hansson@arm.com
261111053Sandreas.hansson@arm.com    return new Cache(this);
261211053Sandreas.hansson@arm.com}
261311051Sandreas.hansson@arm.com///////////////
261411051Sandreas.hansson@arm.com//
261511051Sandreas.hansson@arm.com// MemSidePort
261611051Sandreas.hansson@arm.com//
261711051Sandreas.hansson@arm.com///////////////
261811051Sandreas.hansson@arm.com
261911051Sandreas.hansson@arm.combool
262011051Sandreas.hansson@arm.comCache::MemSidePort::recvTimingResp(PacketPtr pkt)
262111051Sandreas.hansson@arm.com{
262211051Sandreas.hansson@arm.com    cache->recvTimingResp(pkt);
262311051Sandreas.hansson@arm.com    return true;
262411051Sandreas.hansson@arm.com}
262511051Sandreas.hansson@arm.com
262611051Sandreas.hansson@arm.com// Express snooping requests to memside port
262711051Sandreas.hansson@arm.comvoid
262811051Sandreas.hansson@arm.comCache::MemSidePort::recvTimingSnoopReq(PacketPtr pkt)
262911051Sandreas.hansson@arm.com{
263011051Sandreas.hansson@arm.com    // handle snooping requests
263111051Sandreas.hansson@arm.com    cache->recvTimingSnoopReq(pkt);
263211051Sandreas.hansson@arm.com}
263311051Sandreas.hansson@arm.com
263411051Sandreas.hansson@arm.comTick
263511051Sandreas.hansson@arm.comCache::MemSidePort::recvAtomicSnoop(PacketPtr pkt)
263611051Sandreas.hansson@arm.com{
263711051Sandreas.hansson@arm.com    return cache->recvAtomicSnoop(pkt);
263811051Sandreas.hansson@arm.com}
263911051Sandreas.hansson@arm.com
264011051Sandreas.hansson@arm.comvoid
264111051Sandreas.hansson@arm.comCache::MemSidePort::recvFunctionalSnoop(PacketPtr pkt)
264211051Sandreas.hansson@arm.com{
264311051Sandreas.hansson@arm.com    // functional snoop (note that in contrast to atomic we don't have
264411051Sandreas.hansson@arm.com    // a specific functionalSnoop method, as they have the same
264511051Sandreas.hansson@arm.com    // behaviour regardless)
264611051Sandreas.hansson@arm.com    cache->functionalAccess(pkt, false);
264711051Sandreas.hansson@arm.com}
264811051Sandreas.hansson@arm.com
264911051Sandreas.hansson@arm.comvoid
265011051Sandreas.hansson@arm.comCache::CacheReqPacketQueue::sendDeferredPacket()
265111051Sandreas.hansson@arm.com{
265211051Sandreas.hansson@arm.com    // sanity check
265311051Sandreas.hansson@arm.com    assert(!waitingOnRetry);
265411051Sandreas.hansson@arm.com
265511051Sandreas.hansson@arm.com    // there should never be any deferred request packets in the
265611051Sandreas.hansson@arm.com    // queue, instead we resly on the cache to provide the packets
265711051Sandreas.hansson@arm.com    // from the MSHR queue or write queue
265811051Sandreas.hansson@arm.com    assert(deferredPacketReadyTime() == MaxTick);
265911051Sandreas.hansson@arm.com
266011051Sandreas.hansson@arm.com    // check for request packets (requests & writebacks)
266111375Sandreas.hansson@arm.com    QueueEntry* entry = cache.getNextQueueEntry();
266211375Sandreas.hansson@arm.com
266311375Sandreas.hansson@arm.com    if (!entry) {
266411051Sandreas.hansson@arm.com        // can happen if e.g. we attempt a writeback and fail, but
266511051Sandreas.hansson@arm.com        // before the retry, the writeback is eliminated because
266611051Sandreas.hansson@arm.com        // we snoop another cache's ReadEx.
266711051Sandreas.hansson@arm.com    } else {
266811051Sandreas.hansson@arm.com        // let our snoop responses go first if there are responses to
266911375Sandreas.hansson@arm.com        // the same addresses
267011375Sandreas.hansson@arm.com        if (checkConflictingSnoop(entry->blkAddr)) {
267111051Sandreas.hansson@arm.com            return;
267211051Sandreas.hansson@arm.com        }
267311375Sandreas.hansson@arm.com        waitingOnRetry = entry->sendPacket(cache);
267411051Sandreas.hansson@arm.com    }
267511051Sandreas.hansson@arm.com
267611051Sandreas.hansson@arm.com    // if we succeeded and are not waiting for a retry, schedule the
267711375Sandreas.hansson@arm.com    // next send considering when the next queue is ready, note that
267811051Sandreas.hansson@arm.com    // snoop responses have their own packet queue and thus schedule
267911051Sandreas.hansson@arm.com    // their own events
268011051Sandreas.hansson@arm.com    if (!waitingOnRetry) {
268111375Sandreas.hansson@arm.com        schedSendEvent(cache.nextQueueReadyTime());
268211051Sandreas.hansson@arm.com    }
268311051Sandreas.hansson@arm.com}
268411051Sandreas.hansson@arm.com
268511051Sandreas.hansson@arm.comCache::
268611051Sandreas.hansson@arm.comMemSidePort::MemSidePort(const std::string &_name, Cache *_cache,
268711051Sandreas.hansson@arm.com                         const std::string &_label)
268811051Sandreas.hansson@arm.com    : BaseCache::CacheMasterPort(_name, _cache, _reqQueue, _snoopRespQueue),
268911051Sandreas.hansson@arm.com      _reqQueue(*_cache, *this, _snoopRespQueue, _label),
269011051Sandreas.hansson@arm.com      _snoopRespQueue(*_cache, *this, _label), cache(_cache)
269111051Sandreas.hansson@arm.com{
269211051Sandreas.hansson@arm.com}
2693