base.cc revision 13859
12810SN/A/*
212724Snikos.nikoleris@arm.com * Copyright (c) 2012-2013, 2018 ARM Limited
38856Sandreas.hansson@arm.com * All rights reserved.
48856Sandreas.hansson@arm.com *
58856Sandreas.hansson@arm.com * The license below extends only to copyright in the software and shall
68856Sandreas.hansson@arm.com * not be construed as granting a license to any other intellectual
78856Sandreas.hansson@arm.com * property including but not limited to intellectual property relating
88856Sandreas.hansson@arm.com * to a hardware implementation of the functionality of the software
98856Sandreas.hansson@arm.com * licensed hereunder.  You may use the software subject to the license
108856Sandreas.hansson@arm.com * terms below provided that you ensure that this notice is replicated
118856Sandreas.hansson@arm.com * unmodified and in its entirety in all distributions of the software,
128856Sandreas.hansson@arm.com * modified or unmodified, in source code or in binary form.
138856Sandreas.hansson@arm.com *
142810SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
152810SN/A * All rights reserved.
162810SN/A *
172810SN/A * Redistribution and use in source and binary forms, with or without
182810SN/A * modification, are permitted provided that the following conditions are
192810SN/A * met: redistributions of source code must retain the above copyright
202810SN/A * notice, this list of conditions and the following disclaimer;
212810SN/A * redistributions in binary form must reproduce the above copyright
222810SN/A * notice, this list of conditions and the following disclaimer in the
232810SN/A * documentation and/or other materials provided with the distribution;
242810SN/A * neither the name of the copyright holders nor the names of its
252810SN/A * contributors may be used to endorse or promote products derived from
262810SN/A * this software without specific prior written permission.
272810SN/A *
282810SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292810SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302810SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
312810SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322810SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332810SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
342810SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352810SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362810SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372810SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
382810SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392810SN/A *
402810SN/A * Authors: Erik Hallnor
4112724Snikos.nikoleris@arm.com *          Nikos Nikoleris
422810SN/A */
432810SN/A
442810SN/A/**
452810SN/A * @file
462810SN/A * Definition of BaseCache functions.
472810SN/A */
482810SN/A
4911486Snikos.nikoleris@arm.com#include "mem/cache/base.hh"
5011486Snikos.nikoleris@arm.com
5112724Snikos.nikoleris@arm.com#include "base/compiler.hh"
5212724Snikos.nikoleris@arm.com#include "base/logging.hh"
538232Snate@binkert.org#include "debug/Cache.hh"
5412724Snikos.nikoleris@arm.com#include "debug/CachePort.hh"
5513222Sodanrc@yahoo.com.br#include "debug/CacheRepl.hh"
5612724Snikos.nikoleris@arm.com#include "debug/CacheVerbose.hh"
5711486Snikos.nikoleris@arm.com#include "mem/cache/mshr.hh"
5812724Snikos.nikoleris@arm.com#include "mem/cache/prefetch/base.hh"
5912724Snikos.nikoleris@arm.com#include "mem/cache/queue_entry.hh"
6012724Snikos.nikoleris@arm.com#include "params/BaseCache.hh"
6113352Snikos.nikoleris@arm.com#include "params/WriteAllocator.hh"
6212724Snikos.nikoleris@arm.com#include "sim/core.hh"
6312724Snikos.nikoleris@arm.com
6412724Snikos.nikoleris@arm.comclass BaseMasterPort;
6512724Snikos.nikoleris@arm.comclass BaseSlavePort;
662810SN/A
672810SN/Ausing namespace std;
682810SN/A
698856Sandreas.hansson@arm.comBaseCache::CacheSlavePort::CacheSlavePort(const std::string &_name,
708856Sandreas.hansson@arm.com                                          BaseCache *_cache,
718856Sandreas.hansson@arm.com                                          const std::string &_label)
7213564Snikos.nikoleris@arm.com    : QueuedSlavePort(_name, _cache, queue),
7313564Snikos.nikoleris@arm.com      queue(*_cache, *this, true, _label),
7412084Sspwilson2@wisc.edu      blocked(false), mustSendRetry(false),
7512084Sspwilson2@wisc.edu      sendRetryEvent([this]{ processSendRetry(); }, _name)
768856Sandreas.hansson@arm.com{
778856Sandreas.hansson@arm.com}
784475SN/A
7911053Sandreas.hansson@arm.comBaseCache::BaseCache(const BaseCacheParams *p, unsigned blk_size)
805034SN/A    : MemObject(p),
8112724Snikos.nikoleris@arm.com      cpuSidePort (p->name + ".cpu_side", this, "CpuSidePort"),
8212724Snikos.nikoleris@arm.com      memSidePort(p->name + ".mem_side", this, "MemSidePort"),
8311377Sandreas.hansson@arm.com      mshrQueue("MSHRs", p->mshrs, 0, p->demand_mshr_reserve), // see below
8411377Sandreas.hansson@arm.com      writeBuffer("write buffer", p->write_buffers, p->mshrs), // see below
8512724Snikos.nikoleris@arm.com      tags(p->tags),
8612724Snikos.nikoleris@arm.com      prefetcher(p->prefetcher),
8713352Snikos.nikoleris@arm.com      writeAllocator(p->write_allocator),
8812724Snikos.nikoleris@arm.com      writebackClean(p->writeback_clean),
8912724Snikos.nikoleris@arm.com      tempBlockWriteback(nullptr),
9012724Snikos.nikoleris@arm.com      writebackTempBlockAtomicEvent([this]{ writebackTempBlockAtomic(); },
9112724Snikos.nikoleris@arm.com                                    name(), false,
9212724Snikos.nikoleris@arm.com                                    EventBase::Delayed_Writeback_Pri),
9311053Sandreas.hansson@arm.com      blkSize(blk_size),
9411722Ssophiane.senni@gmail.com      lookupLatency(p->tag_latency),
9511722Ssophiane.senni@gmail.com      dataLatency(p->data_latency),
9611722Ssophiane.senni@gmail.com      forwardLatency(p->tag_latency),
9711722Ssophiane.senni@gmail.com      fillLatency(p->data_latency),
989263Smrinmoy.ghosh@arm.com      responseLatency(p->response_latency),
9913418Sodanrc@yahoo.com.br      sequentialAccess(p->sequential_access),
1005034SN/A      numTarget(p->tgts_per_mshr),
10111331Sandreas.hansson@arm.com      forwardSnoops(true),
10212724Snikos.nikoleris@arm.com      clusivity(p->clusivity),
10310884Sandreas.hansson@arm.com      isReadOnly(p->is_read_only),
1044626SN/A      blocked(0),
10510360Sandreas.hansson@arm.com      order(0),
10611484Snikos.nikoleris@arm.com      noTargetMSHR(nullptr),
1075034SN/A      missCount(p->max_miss_count),
1088883SAli.Saidi@ARM.com      addrRanges(p->addr_ranges.begin(), p->addr_ranges.end()),
1098833Sdam.sunwoo@arm.com      system(p->system)
1104458SN/A{
11111377Sandreas.hansson@arm.com    // the MSHR queue has no reserve entries as we check the MSHR
11211377Sandreas.hansson@arm.com    // queue on every single allocation, whereas the write queue has
11311377Sandreas.hansson@arm.com    // as many reserve entries as we have MSHRs, since every MSHR may
11411377Sandreas.hansson@arm.com    // eventually require a writeback, and we do not check the write
11511377Sandreas.hansson@arm.com    // buffer before committing to an MSHR
11611377Sandreas.hansson@arm.com
11711331Sandreas.hansson@arm.com    // forward snoops is overridden in init() once we can query
11811331Sandreas.hansson@arm.com    // whether the connected master is actually snooping or not
11912724Snikos.nikoleris@arm.com
12012843Srmk35@cl.cam.ac.uk    tempBlock = new TempCacheBlk(blkSize);
12112724Snikos.nikoleris@arm.com
12213419Sodanrc@yahoo.com.br    tags->tagsInit();
12312724Snikos.nikoleris@arm.com    if (prefetcher)
12412724Snikos.nikoleris@arm.com        prefetcher->setCache(this);
12512724Snikos.nikoleris@arm.com}
12612724Snikos.nikoleris@arm.com
12712724Snikos.nikoleris@arm.comBaseCache::~BaseCache()
12812724Snikos.nikoleris@arm.com{
12912724Snikos.nikoleris@arm.com    delete tempBlock;
1302810SN/A}
1312810SN/A
1323013SN/Avoid
1338856Sandreas.hansson@arm.comBaseCache::CacheSlavePort::setBlocked()
1342810SN/A{
1353013SN/A    assert(!blocked);
13610714Sandreas.hansson@arm.com    DPRINTF(CachePort, "Port is blocking new requests\n");
1372810SN/A    blocked = true;
1389614Srene.dejong@arm.com    // if we already scheduled a retry in this cycle, but it has not yet
1399614Srene.dejong@arm.com    // happened, cancel it
1409614Srene.dejong@arm.com    if (sendRetryEvent.scheduled()) {
14110345SCurtis.Dunham@arm.com        owner.deschedule(sendRetryEvent);
14210714Sandreas.hansson@arm.com        DPRINTF(CachePort, "Port descheduled retry\n");
14310345SCurtis.Dunham@arm.com        mustSendRetry = true;
1449614Srene.dejong@arm.com    }
1452810SN/A}
1462810SN/A
1472810SN/Avoid
1488856Sandreas.hansson@arm.comBaseCache::CacheSlavePort::clearBlocked()
1492810SN/A{
1503013SN/A    assert(blocked);
15110714Sandreas.hansson@arm.com    DPRINTF(CachePort, "Port is accepting new requests\n");
1523013SN/A    blocked = false;
1538856Sandreas.hansson@arm.com    if (mustSendRetry) {
15410714Sandreas.hansson@arm.com        // @TODO: need to find a better time (next cycle?)
1558922Swilliam.wang@arm.com        owner.schedule(sendRetryEvent, curTick() + 1);
1562897SN/A    }
1572810SN/A}
1582810SN/A
15910344Sandreas.hansson@arm.comvoid
16010344Sandreas.hansson@arm.comBaseCache::CacheSlavePort::processSendRetry()
16110344Sandreas.hansson@arm.com{
16210714Sandreas.hansson@arm.com    DPRINTF(CachePort, "Port is sending retry\n");
16310344Sandreas.hansson@arm.com
16410344Sandreas.hansson@arm.com    // reset the flag and call retry
16510344Sandreas.hansson@arm.com    mustSendRetry = false;
16610713Sandreas.hansson@arm.com    sendRetryReq();
16710344Sandreas.hansson@arm.com}
1682844SN/A
16912730Sodanrc@yahoo.com.brAddr
17012730Sodanrc@yahoo.com.brBaseCache::regenerateBlkAddr(CacheBlk* blk)
17112730Sodanrc@yahoo.com.br{
17212730Sodanrc@yahoo.com.br    if (blk != tempBlock) {
17312730Sodanrc@yahoo.com.br        return tags->regenerateBlkAddr(blk);
17412730Sodanrc@yahoo.com.br    } else {
17512730Sodanrc@yahoo.com.br        return tempBlock->getAddr();
17612730Sodanrc@yahoo.com.br    }
17712730Sodanrc@yahoo.com.br}
17812730Sodanrc@yahoo.com.br
1792810SN/Avoid
1802858SN/ABaseCache::init()
1812858SN/A{
18212724Snikos.nikoleris@arm.com    if (!cpuSidePort.isConnected() || !memSidePort.isConnected())
1838922Swilliam.wang@arm.com        fatal("Cache ports on %s are not connected\n", name());
18412724Snikos.nikoleris@arm.com    cpuSidePort.sendRangeChange();
18512724Snikos.nikoleris@arm.com    forwardSnoops = cpuSidePort.isSnooping();
1862858SN/A}
1872858SN/A
18813784Sgabeblack@google.comPort &
18913784Sgabeblack@google.comBaseCache::getPort(const std::string &if_name, PortID idx)
1908922Swilliam.wang@arm.com{
1918922Swilliam.wang@arm.com    if (if_name == "mem_side") {
19212724Snikos.nikoleris@arm.com        return memSidePort;
19313784Sgabeblack@google.com    } else if (if_name == "cpu_side") {
19413784Sgabeblack@google.com        return cpuSidePort;
1958922Swilliam.wang@arm.com    }  else {
19613784Sgabeblack@google.com        return MemObject::getPort(if_name, idx);
1978922Swilliam.wang@arm.com    }
1988922Swilliam.wang@arm.com}
1994628SN/A
20010821Sandreas.hansson@arm.combool
20110821Sandreas.hansson@arm.comBaseCache::inRange(Addr addr) const
20210821Sandreas.hansson@arm.com{
20310821Sandreas.hansson@arm.com    for (const auto& r : addrRanges) {
20410821Sandreas.hansson@arm.com        if (r.contains(addr)) {
20510821Sandreas.hansson@arm.com            return true;
20610821Sandreas.hansson@arm.com       }
20710821Sandreas.hansson@arm.com    }
20810821Sandreas.hansson@arm.com    return false;
20910821Sandreas.hansson@arm.com}
21010821Sandreas.hansson@arm.com
2112858SN/Avoid
21212724Snikos.nikoleris@arm.comBaseCache::handleTimingReqHit(PacketPtr pkt, CacheBlk *blk, Tick request_time)
21312724Snikos.nikoleris@arm.com{
21412724Snikos.nikoleris@arm.com    if (pkt->needsResponse()) {
21513745Sodanrc@yahoo.com.br        // These delays should have been consumed by now
21613745Sodanrc@yahoo.com.br        assert(pkt->headerDelay == 0);
21713745Sodanrc@yahoo.com.br        assert(pkt->payloadDelay == 0);
21813745Sodanrc@yahoo.com.br
21912724Snikos.nikoleris@arm.com        pkt->makeTimingResponse();
22012724Snikos.nikoleris@arm.com
22112724Snikos.nikoleris@arm.com        // In this case we are considering request_time that takes
22212724Snikos.nikoleris@arm.com        // into account the delay of the xbar, if any, and just
22312724Snikos.nikoleris@arm.com        // lat, neglecting responseLatency, modelling hit latency
22413418Sodanrc@yahoo.com.br        // just as the value of lat overriden by access(), which calls
22513418Sodanrc@yahoo.com.br        // the calculateAccessLatency() function.
22613564Snikos.nikoleris@arm.com        cpuSidePort.schedTimingResp(pkt, request_time);
22712724Snikos.nikoleris@arm.com    } else {
22812724Snikos.nikoleris@arm.com        DPRINTF(Cache, "%s satisfied %s, no response needed\n", __func__,
22912724Snikos.nikoleris@arm.com                pkt->print());
23012724Snikos.nikoleris@arm.com
23112724Snikos.nikoleris@arm.com        // queue the packet for deletion, as the sending cache is
23212724Snikos.nikoleris@arm.com        // still relying on it; if the block is found in access(),
23312724Snikos.nikoleris@arm.com        // CleanEvict and Writeback messages will be deleted
23412724Snikos.nikoleris@arm.com        // here as well
23512724Snikos.nikoleris@arm.com        pendingDelete.reset(pkt);
23612724Snikos.nikoleris@arm.com    }
23712724Snikos.nikoleris@arm.com}
23812724Snikos.nikoleris@arm.com
23912724Snikos.nikoleris@arm.comvoid
24012724Snikos.nikoleris@arm.comBaseCache::handleTimingReqMiss(PacketPtr pkt, MSHR *mshr, CacheBlk *blk,
24112724Snikos.nikoleris@arm.com                               Tick forward_time, Tick request_time)
24212724Snikos.nikoleris@arm.com{
24313352Snikos.nikoleris@arm.com    if (writeAllocator &&
24413352Snikos.nikoleris@arm.com        pkt && pkt->isWrite() && !pkt->req->isUncacheable()) {
24513352Snikos.nikoleris@arm.com        writeAllocator->updateMode(pkt->getAddr(), pkt->getSize(),
24613352Snikos.nikoleris@arm.com                                   pkt->getBlockAddr(blkSize));
24713352Snikos.nikoleris@arm.com    }
24813352Snikos.nikoleris@arm.com
24912724Snikos.nikoleris@arm.com    if (mshr) {
25012724Snikos.nikoleris@arm.com        /// MSHR hit
25112724Snikos.nikoleris@arm.com        /// @note writebacks will be checked in getNextMSHR()
25212724Snikos.nikoleris@arm.com        /// for any conflicting requests to the same block
25312724Snikos.nikoleris@arm.com
25412724Snikos.nikoleris@arm.com        //@todo remove hw_pf here
25512724Snikos.nikoleris@arm.com
25612724Snikos.nikoleris@arm.com        // Coalesce unless it was a software prefetch (see above).
25712724Snikos.nikoleris@arm.com        if (pkt) {
25812724Snikos.nikoleris@arm.com            assert(!pkt->isWriteback());
25912724Snikos.nikoleris@arm.com            // CleanEvicts corresponding to blocks which have
26012724Snikos.nikoleris@arm.com            // outstanding requests in MSHRs are simply sunk here
26112724Snikos.nikoleris@arm.com            if (pkt->cmd == MemCmd::CleanEvict) {
26212724Snikos.nikoleris@arm.com                pendingDelete.reset(pkt);
26312724Snikos.nikoleris@arm.com            } else if (pkt->cmd == MemCmd::WriteClean) {
26412724Snikos.nikoleris@arm.com                // A WriteClean should never coalesce with any
26512724Snikos.nikoleris@arm.com                // outstanding cache maintenance requests.
26612724Snikos.nikoleris@arm.com
26712724Snikos.nikoleris@arm.com                // We use forward_time here because there is an
26812724Snikos.nikoleris@arm.com                // uncached memory write, forwarded to WriteBuffer.
26912724Snikos.nikoleris@arm.com                allocateWriteBuffer(pkt, forward_time);
27012724Snikos.nikoleris@arm.com            } else {
27112724Snikos.nikoleris@arm.com                DPRINTF(Cache, "%s coalescing MSHR for %s\n", __func__,
27212724Snikos.nikoleris@arm.com                        pkt->print());
27312724Snikos.nikoleris@arm.com
27412724Snikos.nikoleris@arm.com                assert(pkt->req->masterId() < system->maxMasters());
27512724Snikos.nikoleris@arm.com                mshr_hits[pkt->cmdToIndex()][pkt->req->masterId()]++;
27612724Snikos.nikoleris@arm.com
27712724Snikos.nikoleris@arm.com                // We use forward_time here because it is the same
27812724Snikos.nikoleris@arm.com                // considering new targets. We have multiple
27912724Snikos.nikoleris@arm.com                // requests for the same address here. It
28012724Snikos.nikoleris@arm.com                // specifies the latency to allocate an internal
28112724Snikos.nikoleris@arm.com                // buffer and to schedule an event to the queued
28212724Snikos.nikoleris@arm.com                // port and also takes into account the additional
28312724Snikos.nikoleris@arm.com                // delay of the xbar.
28412724Snikos.nikoleris@arm.com                mshr->allocateTarget(pkt, forward_time, order++,
28512724Snikos.nikoleris@arm.com                                     allocOnFill(pkt->cmd));
28612724Snikos.nikoleris@arm.com                if (mshr->getNumTargets() == numTarget) {
28712724Snikos.nikoleris@arm.com                    noTargetMSHR = mshr;
28812724Snikos.nikoleris@arm.com                    setBlocked(Blocked_NoTargets);
28912724Snikos.nikoleris@arm.com                    // need to be careful with this... if this mshr isn't
29012724Snikos.nikoleris@arm.com                    // ready yet (i.e. time > curTick()), we don't want to
29112724Snikos.nikoleris@arm.com                    // move it ahead of mshrs that are ready
29212724Snikos.nikoleris@arm.com                    // mshrQueue.moveToFront(mshr);
29312724Snikos.nikoleris@arm.com                }
29412724Snikos.nikoleris@arm.com            }
29512724Snikos.nikoleris@arm.com        }
29612724Snikos.nikoleris@arm.com    } else {
29712724Snikos.nikoleris@arm.com        // no MSHR
29812724Snikos.nikoleris@arm.com        assert(pkt->req->masterId() < system->maxMasters());
29912724Snikos.nikoleris@arm.com        mshr_misses[pkt->cmdToIndex()][pkt->req->masterId()]++;
30012724Snikos.nikoleris@arm.com
30112724Snikos.nikoleris@arm.com        if (pkt->isEviction() || pkt->cmd == MemCmd::WriteClean) {
30212724Snikos.nikoleris@arm.com            // We use forward_time here because there is an
30312724Snikos.nikoleris@arm.com            // writeback or writeclean, forwarded to WriteBuffer.
30412724Snikos.nikoleris@arm.com            allocateWriteBuffer(pkt, forward_time);
30512724Snikos.nikoleris@arm.com        } else {
30612724Snikos.nikoleris@arm.com            if (blk && blk->isValid()) {
30712724Snikos.nikoleris@arm.com                // If we have a write miss to a valid block, we
30812724Snikos.nikoleris@arm.com                // need to mark the block non-readable.  Otherwise
30912724Snikos.nikoleris@arm.com                // if we allow reads while there's an outstanding
31012724Snikos.nikoleris@arm.com                // write miss, the read could return stale data
31112724Snikos.nikoleris@arm.com                // out of the cache block... a more aggressive
31212724Snikos.nikoleris@arm.com                // system could detect the overlap (if any) and
31312724Snikos.nikoleris@arm.com                // forward data out of the MSHRs, but we don't do
31412724Snikos.nikoleris@arm.com                // that yet.  Note that we do need to leave the
31512724Snikos.nikoleris@arm.com                // block valid so that it stays in the cache, in
31612724Snikos.nikoleris@arm.com                // case we get an upgrade response (and hence no
31712724Snikos.nikoleris@arm.com                // new data) when the write miss completes.
31812724Snikos.nikoleris@arm.com                // As long as CPUs do proper store/load forwarding
31912724Snikos.nikoleris@arm.com                // internally, and have a sufficiently weak memory
32012724Snikos.nikoleris@arm.com                // model, this is probably unnecessary, but at some
32112724Snikos.nikoleris@arm.com                // point it must have seemed like we needed it...
32212724Snikos.nikoleris@arm.com                assert((pkt->needsWritable() && !blk->isWritable()) ||
32312724Snikos.nikoleris@arm.com                       pkt->req->isCacheMaintenance());
32412724Snikos.nikoleris@arm.com                blk->status &= ~BlkReadable;
32512724Snikos.nikoleris@arm.com            }
32612724Snikos.nikoleris@arm.com            // Here we are using forward_time, modelling the latency of
32712724Snikos.nikoleris@arm.com            // a miss (outbound) just as forwardLatency, neglecting the
32812724Snikos.nikoleris@arm.com            // lookupLatency component.
32912724Snikos.nikoleris@arm.com            allocateMissBuffer(pkt, forward_time);
33012724Snikos.nikoleris@arm.com        }
33112724Snikos.nikoleris@arm.com    }
33212724Snikos.nikoleris@arm.com}
33312724Snikos.nikoleris@arm.com
33412724Snikos.nikoleris@arm.comvoid
33512724Snikos.nikoleris@arm.comBaseCache::recvTimingReq(PacketPtr pkt)
33612724Snikos.nikoleris@arm.com{
33712724Snikos.nikoleris@arm.com    // anything that is merely forwarded pays for the forward latency and
33812724Snikos.nikoleris@arm.com    // the delay provided by the crossbar
33912724Snikos.nikoleris@arm.com    Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
34012724Snikos.nikoleris@arm.com
34113418Sodanrc@yahoo.com.br    Cycles lat;
34212724Snikos.nikoleris@arm.com    CacheBlk *blk = nullptr;
34312724Snikos.nikoleris@arm.com    bool satisfied = false;
34412724Snikos.nikoleris@arm.com    {
34512724Snikos.nikoleris@arm.com        PacketList writebacks;
34612724Snikos.nikoleris@arm.com        // Note that lat is passed by reference here. The function
34713418Sodanrc@yahoo.com.br        // access() will set the lat value.
34812724Snikos.nikoleris@arm.com        satisfied = access(pkt, blk, lat, writebacks);
34912724Snikos.nikoleris@arm.com
35013747Sodanrc@yahoo.com.br        // After the evicted blocks are selected, they must be forwarded
35113747Sodanrc@yahoo.com.br        // to the write buffer to ensure they logically precede anything
35213747Sodanrc@yahoo.com.br        // happening below
35313747Sodanrc@yahoo.com.br        doWritebacks(writebacks, clockEdge(lat + forwardLatency));
35412724Snikos.nikoleris@arm.com    }
35512724Snikos.nikoleris@arm.com
35612724Snikos.nikoleris@arm.com    // Here we charge the headerDelay that takes into account the latencies
35712724Snikos.nikoleris@arm.com    // of the bus, if the packet comes from it.
35813418Sodanrc@yahoo.com.br    // The latency charged is just the value set by the access() function.
35912724Snikos.nikoleris@arm.com    // In case of a hit we are neglecting response latency.
36012724Snikos.nikoleris@arm.com    // In case of a miss we are neglecting forward latency.
36113746Sodanrc@yahoo.com.br    Tick request_time = clockEdge(lat);
36212724Snikos.nikoleris@arm.com    // Here we reset the timing of the packet.
36312724Snikos.nikoleris@arm.com    pkt->headerDelay = pkt->payloadDelay = 0;
36412724Snikos.nikoleris@arm.com
36512724Snikos.nikoleris@arm.com    if (satisfied) {
36613416Sjavier.bueno@metempsy.com        // notify before anything else as later handleTimingReqHit might turn
36713416Sjavier.bueno@metempsy.com        // the packet in a response
36813416Sjavier.bueno@metempsy.com        ppHit->notify(pkt);
36912724Snikos.nikoleris@arm.com
37013416Sjavier.bueno@metempsy.com        if (prefetcher && blk && blk->wasPrefetched()) {
37113416Sjavier.bueno@metempsy.com            blk->status &= ~BlkHWPrefetched;
37212724Snikos.nikoleris@arm.com        }
37312724Snikos.nikoleris@arm.com
37412724Snikos.nikoleris@arm.com        handleTimingReqHit(pkt, blk, request_time);
37512724Snikos.nikoleris@arm.com    } else {
37612724Snikos.nikoleris@arm.com        handleTimingReqMiss(pkt, blk, forward_time, request_time);
37712724Snikos.nikoleris@arm.com
37813416Sjavier.bueno@metempsy.com        ppMiss->notify(pkt);
37912724Snikos.nikoleris@arm.com    }
38012724Snikos.nikoleris@arm.com
38113416Sjavier.bueno@metempsy.com    if (prefetcher) {
38213416Sjavier.bueno@metempsy.com        // track time of availability of next prefetch, if any
38313416Sjavier.bueno@metempsy.com        Tick next_pf_time = prefetcher->nextPrefetchReadyTime();
38413416Sjavier.bueno@metempsy.com        if (next_pf_time != MaxTick) {
38513416Sjavier.bueno@metempsy.com            schedMemSideSendEvent(next_pf_time);
38613416Sjavier.bueno@metempsy.com        }
38712724Snikos.nikoleris@arm.com    }
38812724Snikos.nikoleris@arm.com}
38912724Snikos.nikoleris@arm.com
39012724Snikos.nikoleris@arm.comvoid
39112724Snikos.nikoleris@arm.comBaseCache::handleUncacheableWriteResp(PacketPtr pkt)
39212724Snikos.nikoleris@arm.com{
39312724Snikos.nikoleris@arm.com    Tick completion_time = clockEdge(responseLatency) +
39412724Snikos.nikoleris@arm.com        pkt->headerDelay + pkt->payloadDelay;
39512724Snikos.nikoleris@arm.com
39612724Snikos.nikoleris@arm.com    // Reset the bus additional time as it is now accounted for
39712724Snikos.nikoleris@arm.com    pkt->headerDelay = pkt->payloadDelay = 0;
39812724Snikos.nikoleris@arm.com
39913564Snikos.nikoleris@arm.com    cpuSidePort.schedTimingResp(pkt, completion_time);
40012724Snikos.nikoleris@arm.com}
40112724Snikos.nikoleris@arm.com
40212724Snikos.nikoleris@arm.comvoid
40312724Snikos.nikoleris@arm.comBaseCache::recvTimingResp(PacketPtr pkt)
40412724Snikos.nikoleris@arm.com{
40512724Snikos.nikoleris@arm.com    assert(pkt->isResponse());
40612724Snikos.nikoleris@arm.com
40712724Snikos.nikoleris@arm.com    // all header delay should be paid for by the crossbar, unless
40812724Snikos.nikoleris@arm.com    // this is a prefetch response from above
40912724Snikos.nikoleris@arm.com    panic_if(pkt->headerDelay != 0 && pkt->cmd != MemCmd::HardPFResp,
41012724Snikos.nikoleris@arm.com             "%s saw a non-zero packet delay\n", name());
41112724Snikos.nikoleris@arm.com
41212724Snikos.nikoleris@arm.com    const bool is_error = pkt->isError();
41312724Snikos.nikoleris@arm.com
41412724Snikos.nikoleris@arm.com    if (is_error) {
41512724Snikos.nikoleris@arm.com        DPRINTF(Cache, "%s: Cache received %s with error\n", __func__,
41612724Snikos.nikoleris@arm.com                pkt->print());
41712724Snikos.nikoleris@arm.com    }
41812724Snikos.nikoleris@arm.com
41912724Snikos.nikoleris@arm.com    DPRINTF(Cache, "%s: Handling response %s\n", __func__,
42012724Snikos.nikoleris@arm.com            pkt->print());
42112724Snikos.nikoleris@arm.com
42212724Snikos.nikoleris@arm.com    // if this is a write, we should be looking at an uncacheable
42312724Snikos.nikoleris@arm.com    // write
42412724Snikos.nikoleris@arm.com    if (pkt->isWrite()) {
42512724Snikos.nikoleris@arm.com        assert(pkt->req->isUncacheable());
42612724Snikos.nikoleris@arm.com        handleUncacheableWriteResp(pkt);
42712724Snikos.nikoleris@arm.com        return;
42812724Snikos.nikoleris@arm.com    }
42912724Snikos.nikoleris@arm.com
43012724Snikos.nikoleris@arm.com    // we have dealt with any (uncacheable) writes above, from here on
43112724Snikos.nikoleris@arm.com    // we know we are dealing with an MSHR due to a miss or a prefetch
43212724Snikos.nikoleris@arm.com    MSHR *mshr = dynamic_cast<MSHR*>(pkt->popSenderState());
43312724Snikos.nikoleris@arm.com    assert(mshr);
43412724Snikos.nikoleris@arm.com
43512724Snikos.nikoleris@arm.com    if (mshr == noTargetMSHR) {
43612724Snikos.nikoleris@arm.com        // we always clear at least one target
43712724Snikos.nikoleris@arm.com        clearBlocked(Blocked_NoTargets);
43812724Snikos.nikoleris@arm.com        noTargetMSHR = nullptr;
43912724Snikos.nikoleris@arm.com    }
44012724Snikos.nikoleris@arm.com
44112724Snikos.nikoleris@arm.com    // Initial target is used just for stats
44213859Sodanrc@yahoo.com.br    QueueEntry::Target *initial_tgt = mshr->getTarget();
44312724Snikos.nikoleris@arm.com    int stats_cmd_idx = initial_tgt->pkt->cmdToIndex();
44412724Snikos.nikoleris@arm.com    Tick miss_latency = curTick() - initial_tgt->recvTime;
44512724Snikos.nikoleris@arm.com
44612724Snikos.nikoleris@arm.com    if (pkt->req->isUncacheable()) {
44712724Snikos.nikoleris@arm.com        assert(pkt->req->masterId() < system->maxMasters());
44812724Snikos.nikoleris@arm.com        mshr_uncacheable_lat[stats_cmd_idx][pkt->req->masterId()] +=
44912724Snikos.nikoleris@arm.com            miss_latency;
45012724Snikos.nikoleris@arm.com    } else {
45112724Snikos.nikoleris@arm.com        assert(pkt->req->masterId() < system->maxMasters());
45212724Snikos.nikoleris@arm.com        mshr_miss_latency[stats_cmd_idx][pkt->req->masterId()] +=
45312724Snikos.nikoleris@arm.com            miss_latency;
45412724Snikos.nikoleris@arm.com    }
45512724Snikos.nikoleris@arm.com
45612724Snikos.nikoleris@arm.com    PacketList writebacks;
45712724Snikos.nikoleris@arm.com
45812724Snikos.nikoleris@arm.com    bool is_fill = !mshr->isForward &&
45913350Snikos.nikoleris@arm.com        (pkt->isRead() || pkt->cmd == MemCmd::UpgradeResp ||
46013350Snikos.nikoleris@arm.com         mshr->wasWholeLineWrite);
46113350Snikos.nikoleris@arm.com
46213350Snikos.nikoleris@arm.com    // make sure that if the mshr was due to a whole line write then
46313350Snikos.nikoleris@arm.com    // the response is an invalidation
46413350Snikos.nikoleris@arm.com    assert(!mshr->wasWholeLineWrite || pkt->isInvalidate());
46512724Snikos.nikoleris@arm.com
46612724Snikos.nikoleris@arm.com    CacheBlk *blk = tags->findBlock(pkt->getAddr(), pkt->isSecure());
46712724Snikos.nikoleris@arm.com
46812724Snikos.nikoleris@arm.com    if (is_fill && !is_error) {
46912724Snikos.nikoleris@arm.com        DPRINTF(Cache, "Block for addr %#llx being updated in Cache\n",
47012724Snikos.nikoleris@arm.com                pkt->getAddr());
47112724Snikos.nikoleris@arm.com
47213352Snikos.nikoleris@arm.com        const bool allocate = (writeAllocator && mshr->wasWholeLineWrite) ?
47313352Snikos.nikoleris@arm.com            writeAllocator->allocate() : mshr->allocOnFill();
47413352Snikos.nikoleris@arm.com        blk = handleFill(pkt, blk, writebacks, allocate);
47512724Snikos.nikoleris@arm.com        assert(blk != nullptr);
47613717Sivan.pizarro@metempsy.com        ppFill->notify(pkt);
47712724Snikos.nikoleris@arm.com    }
47812724Snikos.nikoleris@arm.com
47912724Snikos.nikoleris@arm.com    if (blk && blk->isValid() && pkt->isClean() && !pkt->isInvalidate()) {
48012724Snikos.nikoleris@arm.com        // The block was marked not readable while there was a pending
48112724Snikos.nikoleris@arm.com        // cache maintenance operation, restore its flag.
48212724Snikos.nikoleris@arm.com        blk->status |= BlkReadable;
48312794Snikos.nikoleris@arm.com
48412794Snikos.nikoleris@arm.com        // This was a cache clean operation (without invalidate)
48512794Snikos.nikoleris@arm.com        // and we have a copy of the block already. Since there
48612794Snikos.nikoleris@arm.com        // is no invalidation, we can promote targets that don't
48712794Snikos.nikoleris@arm.com        // require a writable copy
48812794Snikos.nikoleris@arm.com        mshr->promoteReadable();
48912724Snikos.nikoleris@arm.com    }
49012724Snikos.nikoleris@arm.com
49112724Snikos.nikoleris@arm.com    if (blk && blk->isWritable() && !pkt->req->isCacheInvalidate()) {
49212724Snikos.nikoleris@arm.com        // If at this point the referenced block is writable and the
49312724Snikos.nikoleris@arm.com        // response is not a cache invalidate, we promote targets that
49412724Snikos.nikoleris@arm.com        // were deferred as we couldn't guarrantee a writable copy
49512724Snikos.nikoleris@arm.com        mshr->promoteWritable();
49612724Snikos.nikoleris@arm.com    }
49712724Snikos.nikoleris@arm.com
49813478Sodanrc@yahoo.com.br    serviceMSHRTargets(mshr, pkt, blk);
49912724Snikos.nikoleris@arm.com
50012724Snikos.nikoleris@arm.com    if (mshr->promoteDeferredTargets()) {
50112724Snikos.nikoleris@arm.com        // avoid later read getting stale data while write miss is
50212724Snikos.nikoleris@arm.com        // outstanding.. see comment in timingAccess()
50312724Snikos.nikoleris@arm.com        if (blk) {
50412724Snikos.nikoleris@arm.com            blk->status &= ~BlkReadable;
50512724Snikos.nikoleris@arm.com        }
50612724Snikos.nikoleris@arm.com        mshrQueue.markPending(mshr);
50712724Snikos.nikoleris@arm.com        schedMemSideSendEvent(clockEdge() + pkt->payloadDelay);
50812724Snikos.nikoleris@arm.com    } else {
50912724Snikos.nikoleris@arm.com        // while we deallocate an mshr from the queue we still have to
51012724Snikos.nikoleris@arm.com        // check the isFull condition before and after as we might
51112724Snikos.nikoleris@arm.com        // have been using the reserved entries already
51212724Snikos.nikoleris@arm.com        const bool was_full = mshrQueue.isFull();
51312724Snikos.nikoleris@arm.com        mshrQueue.deallocate(mshr);
51412724Snikos.nikoleris@arm.com        if (was_full && !mshrQueue.isFull()) {
51512724Snikos.nikoleris@arm.com            clearBlocked(Blocked_NoMSHRs);
51612724Snikos.nikoleris@arm.com        }
51712724Snikos.nikoleris@arm.com
51812724Snikos.nikoleris@arm.com        // Request the bus for a prefetch if this deallocation freed enough
51912724Snikos.nikoleris@arm.com        // MSHRs for a prefetch to take place
52012724Snikos.nikoleris@arm.com        if (prefetcher && mshrQueue.canPrefetch()) {
52112724Snikos.nikoleris@arm.com            Tick next_pf_time = std::max(prefetcher->nextPrefetchReadyTime(),
52212724Snikos.nikoleris@arm.com                                         clockEdge());
52312724Snikos.nikoleris@arm.com            if (next_pf_time != MaxTick)
52412724Snikos.nikoleris@arm.com                schedMemSideSendEvent(next_pf_time);
52512724Snikos.nikoleris@arm.com        }
52612724Snikos.nikoleris@arm.com    }
52712724Snikos.nikoleris@arm.com
52812724Snikos.nikoleris@arm.com    // if we used temp block, check to see if its valid and then clear it out
52912724Snikos.nikoleris@arm.com    if (blk == tempBlock && tempBlock->isValid()) {
53012724Snikos.nikoleris@arm.com        evictBlock(blk, writebacks);
53112724Snikos.nikoleris@arm.com    }
53212724Snikos.nikoleris@arm.com
53312724Snikos.nikoleris@arm.com    const Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
53412724Snikos.nikoleris@arm.com    // copy writebacks to write buffer
53512724Snikos.nikoleris@arm.com    doWritebacks(writebacks, forward_time);
53612724Snikos.nikoleris@arm.com
53712724Snikos.nikoleris@arm.com    DPRINTF(CacheVerbose, "%s: Leaving with %s\n", __func__, pkt->print());
53812724Snikos.nikoleris@arm.com    delete pkt;
53912724Snikos.nikoleris@arm.com}
54012724Snikos.nikoleris@arm.com
54112724Snikos.nikoleris@arm.com
54212724Snikos.nikoleris@arm.comTick
54312724Snikos.nikoleris@arm.comBaseCache::recvAtomic(PacketPtr pkt)
54412724Snikos.nikoleris@arm.com{
54512724Snikos.nikoleris@arm.com    // should assert here that there are no outstanding MSHRs or
54612724Snikos.nikoleris@arm.com    // writebacks... that would mean that someone used an atomic
54712724Snikos.nikoleris@arm.com    // access in timing mode
54812724Snikos.nikoleris@arm.com
54913412Snikos.nikoleris@arm.com    // We use lookupLatency here because it is used to specify the latency
55013412Snikos.nikoleris@arm.com    // to access.
55113412Snikos.nikoleris@arm.com    Cycles lat = lookupLatency;
55213412Snikos.nikoleris@arm.com
55312724Snikos.nikoleris@arm.com    CacheBlk *blk = nullptr;
55412724Snikos.nikoleris@arm.com    PacketList writebacks;
55512724Snikos.nikoleris@arm.com    bool satisfied = access(pkt, blk, lat, writebacks);
55612724Snikos.nikoleris@arm.com
55712724Snikos.nikoleris@arm.com    if (pkt->isClean() && blk && blk->isDirty()) {
55812724Snikos.nikoleris@arm.com        // A cache clean opearation is looking for a dirty
55912724Snikos.nikoleris@arm.com        // block. If a dirty block is encountered a WriteClean
56012724Snikos.nikoleris@arm.com        // will update any copies to the path to the memory
56112724Snikos.nikoleris@arm.com        // until the point of reference.
56212724Snikos.nikoleris@arm.com        DPRINTF(CacheVerbose, "%s: packet %s found block: %s\n",
56312724Snikos.nikoleris@arm.com                __func__, pkt->print(), blk->print());
56412724Snikos.nikoleris@arm.com        PacketPtr wb_pkt = writecleanBlk(blk, pkt->req->getDest(), pkt->id);
56512724Snikos.nikoleris@arm.com        writebacks.push_back(wb_pkt);
56612724Snikos.nikoleris@arm.com        pkt->setSatisfied();
56712724Snikos.nikoleris@arm.com    }
56812724Snikos.nikoleris@arm.com
56912724Snikos.nikoleris@arm.com    // handle writebacks resulting from the access here to ensure they
57012820Srmk35@cl.cam.ac.uk    // logically precede anything happening below
57112724Snikos.nikoleris@arm.com    doWritebacksAtomic(writebacks);
57212724Snikos.nikoleris@arm.com    assert(writebacks.empty());
57312724Snikos.nikoleris@arm.com
57412724Snikos.nikoleris@arm.com    if (!satisfied) {
57512724Snikos.nikoleris@arm.com        lat += handleAtomicReqMiss(pkt, blk, writebacks);
57612724Snikos.nikoleris@arm.com    }
57712724Snikos.nikoleris@arm.com
57812724Snikos.nikoleris@arm.com    // Note that we don't invoke the prefetcher at all in atomic mode.
57912724Snikos.nikoleris@arm.com    // It's not clear how to do it properly, particularly for
58012724Snikos.nikoleris@arm.com    // prefetchers that aggressively generate prefetch candidates and
58112724Snikos.nikoleris@arm.com    // rely on bandwidth contention to throttle them; these will tend
58212724Snikos.nikoleris@arm.com    // to pollute the cache in atomic mode since there is no bandwidth
58312724Snikos.nikoleris@arm.com    // contention.  If we ever do want to enable prefetching in atomic
58412724Snikos.nikoleris@arm.com    // mode, though, this is the place to do it... see timingAccess()
58512724Snikos.nikoleris@arm.com    // for an example (though we'd want to issue the prefetch(es)
58612724Snikos.nikoleris@arm.com    // immediately rather than calling requestMemSideBus() as we do
58712724Snikos.nikoleris@arm.com    // there).
58812724Snikos.nikoleris@arm.com
58912724Snikos.nikoleris@arm.com    // do any writebacks resulting from the response handling
59012724Snikos.nikoleris@arm.com    doWritebacksAtomic(writebacks);
59112724Snikos.nikoleris@arm.com
59212724Snikos.nikoleris@arm.com    // if we used temp block, check to see if its valid and if so
59312724Snikos.nikoleris@arm.com    // clear it out, but only do so after the call to recvAtomic is
59412724Snikos.nikoleris@arm.com    // finished so that any downstream observers (such as a snoop
59512724Snikos.nikoleris@arm.com    // filter), first see the fill, and only then see the eviction
59612724Snikos.nikoleris@arm.com    if (blk == tempBlock && tempBlock->isValid()) {
59712724Snikos.nikoleris@arm.com        // the atomic CPU calls recvAtomic for fetch and load/store
59812724Snikos.nikoleris@arm.com        // sequentuially, and we may already have a tempBlock
59912724Snikos.nikoleris@arm.com        // writeback from the fetch that we have not yet sent
60012724Snikos.nikoleris@arm.com        if (tempBlockWriteback) {
60112724Snikos.nikoleris@arm.com            // if that is the case, write the prevoius one back, and
60212724Snikos.nikoleris@arm.com            // do not schedule any new event
60312724Snikos.nikoleris@arm.com            writebackTempBlockAtomic();
60412724Snikos.nikoleris@arm.com        } else {
60512724Snikos.nikoleris@arm.com            // the writeback/clean eviction happens after the call to
60612724Snikos.nikoleris@arm.com            // recvAtomic has finished (but before any successive
60712724Snikos.nikoleris@arm.com            // calls), so that the response handling from the fill is
60812724Snikos.nikoleris@arm.com            // allowed to happen first
60912724Snikos.nikoleris@arm.com            schedule(writebackTempBlockAtomicEvent, curTick());
61012724Snikos.nikoleris@arm.com        }
61112724Snikos.nikoleris@arm.com
61212724Snikos.nikoleris@arm.com        tempBlockWriteback = evictBlock(blk);
61312724Snikos.nikoleris@arm.com    }
61412724Snikos.nikoleris@arm.com
61512724Snikos.nikoleris@arm.com    if (pkt->needsResponse()) {
61612724Snikos.nikoleris@arm.com        pkt->makeAtomicResponse();
61712724Snikos.nikoleris@arm.com    }
61812724Snikos.nikoleris@arm.com
61912724Snikos.nikoleris@arm.com    return lat * clockPeriod();
62012724Snikos.nikoleris@arm.com}
62112724Snikos.nikoleris@arm.com
62212724Snikos.nikoleris@arm.comvoid
62312724Snikos.nikoleris@arm.comBaseCache::functionalAccess(PacketPtr pkt, bool from_cpu_side)
62412724Snikos.nikoleris@arm.com{
62512724Snikos.nikoleris@arm.com    Addr blk_addr = pkt->getBlockAddr(blkSize);
62612724Snikos.nikoleris@arm.com    bool is_secure = pkt->isSecure();
62712724Snikos.nikoleris@arm.com    CacheBlk *blk = tags->findBlock(pkt->getAddr(), is_secure);
62812724Snikos.nikoleris@arm.com    MSHR *mshr = mshrQueue.findMatch(blk_addr, is_secure);
62912724Snikos.nikoleris@arm.com
63012724Snikos.nikoleris@arm.com    pkt->pushLabel(name());
63112724Snikos.nikoleris@arm.com
63212724Snikos.nikoleris@arm.com    CacheBlkPrintWrapper cbpw(blk);
63312724Snikos.nikoleris@arm.com
63412724Snikos.nikoleris@arm.com    // Note that just because an L2/L3 has valid data doesn't mean an
63512724Snikos.nikoleris@arm.com    // L1 doesn't have a more up-to-date modified copy that still
63612724Snikos.nikoleris@arm.com    // needs to be found.  As a result we always update the request if
63712724Snikos.nikoleris@arm.com    // we have it, but only declare it satisfied if we are the owner.
63812724Snikos.nikoleris@arm.com
63912724Snikos.nikoleris@arm.com    // see if we have data at all (owned or otherwise)
64012724Snikos.nikoleris@arm.com    bool have_data = blk && blk->isValid()
64112823Srmk35@cl.cam.ac.uk        && pkt->trySatisfyFunctional(&cbpw, blk_addr, is_secure, blkSize,
64212823Srmk35@cl.cam.ac.uk                                     blk->data);
64312724Snikos.nikoleris@arm.com
64412724Snikos.nikoleris@arm.com    // data we have is dirty if marked as such or if we have an
64512724Snikos.nikoleris@arm.com    // in-service MSHR that is pending a modified line
64612724Snikos.nikoleris@arm.com    bool have_dirty =
64712724Snikos.nikoleris@arm.com        have_data && (blk->isDirty() ||
64812724Snikos.nikoleris@arm.com                      (mshr && mshr->inService && mshr->isPendingModified()));
64912724Snikos.nikoleris@arm.com
65012724Snikos.nikoleris@arm.com    bool done = have_dirty ||
65112823Srmk35@cl.cam.ac.uk        cpuSidePort.trySatisfyFunctional(pkt) ||
65212823Srmk35@cl.cam.ac.uk        mshrQueue.trySatisfyFunctional(pkt, blk_addr) ||
65312823Srmk35@cl.cam.ac.uk        writeBuffer.trySatisfyFunctional(pkt, blk_addr) ||
65412823Srmk35@cl.cam.ac.uk        memSidePort.trySatisfyFunctional(pkt);
65512724Snikos.nikoleris@arm.com
65612724Snikos.nikoleris@arm.com    DPRINTF(CacheVerbose, "%s: %s %s%s%s\n", __func__,  pkt->print(),
65712724Snikos.nikoleris@arm.com            (blk && blk->isValid()) ? "valid " : "",
65812724Snikos.nikoleris@arm.com            have_data ? "data " : "", done ? "done " : "");
65912724Snikos.nikoleris@arm.com
66012724Snikos.nikoleris@arm.com    // We're leaving the cache, so pop cache->name() label
66112724Snikos.nikoleris@arm.com    pkt->popLabel();
66212724Snikos.nikoleris@arm.com
66312724Snikos.nikoleris@arm.com    if (done) {
66412724Snikos.nikoleris@arm.com        pkt->makeResponse();
66512724Snikos.nikoleris@arm.com    } else {
66612724Snikos.nikoleris@arm.com        // if it came as a request from the CPU side then make sure it
66712724Snikos.nikoleris@arm.com        // continues towards the memory side
66812724Snikos.nikoleris@arm.com        if (from_cpu_side) {
66912724Snikos.nikoleris@arm.com            memSidePort.sendFunctional(pkt);
67012724Snikos.nikoleris@arm.com        } else if (cpuSidePort.isSnooping()) {
67112724Snikos.nikoleris@arm.com            // if it came from the memory side, it must be a snoop request
67212724Snikos.nikoleris@arm.com            // and we should only forward it if we are forwarding snoops
67312724Snikos.nikoleris@arm.com            cpuSidePort.sendFunctionalSnoop(pkt);
67412724Snikos.nikoleris@arm.com        }
67512724Snikos.nikoleris@arm.com    }
67612724Snikos.nikoleris@arm.com}
67712724Snikos.nikoleris@arm.com
67812724Snikos.nikoleris@arm.com
67912724Snikos.nikoleris@arm.comvoid
68012724Snikos.nikoleris@arm.comBaseCache::cmpAndSwap(CacheBlk *blk, PacketPtr pkt)
68112724Snikos.nikoleris@arm.com{
68212724Snikos.nikoleris@arm.com    assert(pkt->isRequest());
68312724Snikos.nikoleris@arm.com
68412724Snikos.nikoleris@arm.com    uint64_t overwrite_val;
68512724Snikos.nikoleris@arm.com    bool overwrite_mem;
68612724Snikos.nikoleris@arm.com    uint64_t condition_val64;
68712724Snikos.nikoleris@arm.com    uint32_t condition_val32;
68812724Snikos.nikoleris@arm.com
68912724Snikos.nikoleris@arm.com    int offset = pkt->getOffset(blkSize);
69012724Snikos.nikoleris@arm.com    uint8_t *blk_data = blk->data + offset;
69112724Snikos.nikoleris@arm.com
69212724Snikos.nikoleris@arm.com    assert(sizeof(uint64_t) >= pkt->getSize());
69312724Snikos.nikoleris@arm.com
69412724Snikos.nikoleris@arm.com    overwrite_mem = true;
69512724Snikos.nikoleris@arm.com    // keep a copy of our possible write value, and copy what is at the
69612724Snikos.nikoleris@arm.com    // memory address into the packet
69712724Snikos.nikoleris@arm.com    pkt->writeData((uint8_t *)&overwrite_val);
69812724Snikos.nikoleris@arm.com    pkt->setData(blk_data);
69912724Snikos.nikoleris@arm.com
70012724Snikos.nikoleris@arm.com    if (pkt->req->isCondSwap()) {
70112724Snikos.nikoleris@arm.com        if (pkt->getSize() == sizeof(uint64_t)) {
70212724Snikos.nikoleris@arm.com            condition_val64 = pkt->req->getExtraData();
70312724Snikos.nikoleris@arm.com            overwrite_mem = !std::memcmp(&condition_val64, blk_data,
70412724Snikos.nikoleris@arm.com                                         sizeof(uint64_t));
70512724Snikos.nikoleris@arm.com        } else if (pkt->getSize() == sizeof(uint32_t)) {
70612724Snikos.nikoleris@arm.com            condition_val32 = (uint32_t)pkt->req->getExtraData();
70712724Snikos.nikoleris@arm.com            overwrite_mem = !std::memcmp(&condition_val32, blk_data,
70812724Snikos.nikoleris@arm.com                                         sizeof(uint32_t));
70912724Snikos.nikoleris@arm.com        } else
71012724Snikos.nikoleris@arm.com            panic("Invalid size for conditional read/write\n");
71112724Snikos.nikoleris@arm.com    }
71212724Snikos.nikoleris@arm.com
71312724Snikos.nikoleris@arm.com    if (overwrite_mem) {
71412724Snikos.nikoleris@arm.com        std::memcpy(blk_data, &overwrite_val, pkt->getSize());
71512724Snikos.nikoleris@arm.com        blk->status |= BlkDirty;
71612724Snikos.nikoleris@arm.com    }
71712724Snikos.nikoleris@arm.com}
71812724Snikos.nikoleris@arm.com
71912724Snikos.nikoleris@arm.comQueueEntry*
72012724Snikos.nikoleris@arm.comBaseCache::getNextQueueEntry()
72112724Snikos.nikoleris@arm.com{
72212724Snikos.nikoleris@arm.com    // Check both MSHR queue and write buffer for potential requests,
72312724Snikos.nikoleris@arm.com    // note that null does not mean there is no request, it could
72412724Snikos.nikoleris@arm.com    // simply be that it is not ready
72512724Snikos.nikoleris@arm.com    MSHR *miss_mshr  = mshrQueue.getNext();
72612724Snikos.nikoleris@arm.com    WriteQueueEntry *wq_entry = writeBuffer.getNext();
72712724Snikos.nikoleris@arm.com
72812724Snikos.nikoleris@arm.com    // If we got a write buffer request ready, first priority is a
72912724Snikos.nikoleris@arm.com    // full write buffer, otherwise we favour the miss requests
73012724Snikos.nikoleris@arm.com    if (wq_entry && (writeBuffer.isFull() || !miss_mshr)) {
73112724Snikos.nikoleris@arm.com        // need to search MSHR queue for conflicting earlier miss.
73212724Snikos.nikoleris@arm.com        MSHR *conflict_mshr =
73312724Snikos.nikoleris@arm.com            mshrQueue.findPending(wq_entry->blkAddr,
73412724Snikos.nikoleris@arm.com                                  wq_entry->isSecure);
73512724Snikos.nikoleris@arm.com
73612724Snikos.nikoleris@arm.com        if (conflict_mshr && conflict_mshr->order < wq_entry->order) {
73712724Snikos.nikoleris@arm.com            // Service misses in order until conflict is cleared.
73812724Snikos.nikoleris@arm.com            return conflict_mshr;
73912724Snikos.nikoleris@arm.com
74012724Snikos.nikoleris@arm.com            // @todo Note that we ignore the ready time of the conflict here
74112724Snikos.nikoleris@arm.com        }
74212724Snikos.nikoleris@arm.com
74312724Snikos.nikoleris@arm.com        // No conflicts; issue write
74412724Snikos.nikoleris@arm.com        return wq_entry;
74512724Snikos.nikoleris@arm.com    } else if (miss_mshr) {
74612724Snikos.nikoleris@arm.com        // need to check for conflicting earlier writeback
74712724Snikos.nikoleris@arm.com        WriteQueueEntry *conflict_mshr =
74812724Snikos.nikoleris@arm.com            writeBuffer.findPending(miss_mshr->blkAddr,
74912724Snikos.nikoleris@arm.com                                    miss_mshr->isSecure);
75012724Snikos.nikoleris@arm.com        if (conflict_mshr) {
75112724Snikos.nikoleris@arm.com            // not sure why we don't check order here... it was in the
75212724Snikos.nikoleris@arm.com            // original code but commented out.
75312724Snikos.nikoleris@arm.com
75412724Snikos.nikoleris@arm.com            // The only way this happens is if we are
75512724Snikos.nikoleris@arm.com            // doing a write and we didn't have permissions
75612724Snikos.nikoleris@arm.com            // then subsequently saw a writeback (owned got evicted)
75712724Snikos.nikoleris@arm.com            // We need to make sure to perform the writeback first
75812724Snikos.nikoleris@arm.com            // To preserve the dirty data, then we can issue the write
75912724Snikos.nikoleris@arm.com
76012724Snikos.nikoleris@arm.com            // should we return wq_entry here instead?  I.e. do we
76112724Snikos.nikoleris@arm.com            // have to flush writes in order?  I don't think so... not
76212724Snikos.nikoleris@arm.com            // for Alpha anyway.  Maybe for x86?
76312724Snikos.nikoleris@arm.com            return conflict_mshr;
76412724Snikos.nikoleris@arm.com
76512724Snikos.nikoleris@arm.com            // @todo Note that we ignore the ready time of the conflict here
76612724Snikos.nikoleris@arm.com        }
76712724Snikos.nikoleris@arm.com
76812724Snikos.nikoleris@arm.com        // No conflicts; issue read
76912724Snikos.nikoleris@arm.com        return miss_mshr;
77012724Snikos.nikoleris@arm.com    }
77112724Snikos.nikoleris@arm.com
77212724Snikos.nikoleris@arm.com    // fall through... no pending requests.  Try a prefetch.
77312724Snikos.nikoleris@arm.com    assert(!miss_mshr && !wq_entry);
77412724Snikos.nikoleris@arm.com    if (prefetcher && mshrQueue.canPrefetch()) {
77512724Snikos.nikoleris@arm.com        // If we have a miss queue slot, we can try a prefetch
77612724Snikos.nikoleris@arm.com        PacketPtr pkt = prefetcher->getPacket();
77712724Snikos.nikoleris@arm.com        if (pkt) {
77812724Snikos.nikoleris@arm.com            Addr pf_addr = pkt->getBlockAddr(blkSize);
77912724Snikos.nikoleris@arm.com            if (!tags->findBlock(pf_addr, pkt->isSecure()) &&
78012724Snikos.nikoleris@arm.com                !mshrQueue.findMatch(pf_addr, pkt->isSecure()) &&
78112724Snikos.nikoleris@arm.com                !writeBuffer.findMatch(pf_addr, pkt->isSecure())) {
78212724Snikos.nikoleris@arm.com                // Update statistic on number of prefetches issued
78312724Snikos.nikoleris@arm.com                // (hwpf_mshr_misses)
78412724Snikos.nikoleris@arm.com                assert(pkt->req->masterId() < system->maxMasters());
78512724Snikos.nikoleris@arm.com                mshr_misses[pkt->cmdToIndex()][pkt->req->masterId()]++;
78612724Snikos.nikoleris@arm.com
78712724Snikos.nikoleris@arm.com                // allocate an MSHR and return it, note
78812724Snikos.nikoleris@arm.com                // that we send the packet straight away, so do not
78912724Snikos.nikoleris@arm.com                // schedule the send
79012724Snikos.nikoleris@arm.com                return allocateMissBuffer(pkt, curTick(), false);
79112724Snikos.nikoleris@arm.com            } else {
79212724Snikos.nikoleris@arm.com                // free the request and packet
79312724Snikos.nikoleris@arm.com                delete pkt;
79412724Snikos.nikoleris@arm.com            }
79512724Snikos.nikoleris@arm.com        }
79612724Snikos.nikoleris@arm.com    }
79712724Snikos.nikoleris@arm.com
79812724Snikos.nikoleris@arm.com    return nullptr;
79912724Snikos.nikoleris@arm.com}
80012724Snikos.nikoleris@arm.com
80112724Snikos.nikoleris@arm.comvoid
80212724Snikos.nikoleris@arm.comBaseCache::satisfyRequest(PacketPtr pkt, CacheBlk *blk, bool, bool)
80312724Snikos.nikoleris@arm.com{
80412724Snikos.nikoleris@arm.com    assert(pkt->isRequest());
80512724Snikos.nikoleris@arm.com
80612724Snikos.nikoleris@arm.com    assert(blk && blk->isValid());
80712724Snikos.nikoleris@arm.com    // Occasionally this is not true... if we are a lower-level cache
80812724Snikos.nikoleris@arm.com    // satisfying a string of Read and ReadEx requests from
80912724Snikos.nikoleris@arm.com    // upper-level caches, a Read will mark the block as shared but we
81012724Snikos.nikoleris@arm.com    // can satisfy a following ReadEx anyway since we can rely on the
81112724Snikos.nikoleris@arm.com    // Read requester(s) to have buffered the ReadEx snoop and to
81212724Snikos.nikoleris@arm.com    // invalidate their blocks after receiving them.
81312724Snikos.nikoleris@arm.com    // assert(!pkt->needsWritable() || blk->isWritable());
81412724Snikos.nikoleris@arm.com    assert(pkt->getOffset(blkSize) + pkt->getSize() <= blkSize);
81512724Snikos.nikoleris@arm.com
81612724Snikos.nikoleris@arm.com    // Check RMW operations first since both isRead() and
81712724Snikos.nikoleris@arm.com    // isWrite() will be true for them
81812724Snikos.nikoleris@arm.com    if (pkt->cmd == MemCmd::SwapReq) {
81912766Sqtt2@cornell.edu        if (pkt->isAtomicOp()) {
82012766Sqtt2@cornell.edu            // extract data from cache and save it into the data field in
82112766Sqtt2@cornell.edu            // the packet as a return value from this atomic op
82212766Sqtt2@cornell.edu            int offset = tags->extractBlkOffset(pkt->getAddr());
82312766Sqtt2@cornell.edu            uint8_t *blk_data = blk->data + offset;
82413377Sodanrc@yahoo.com.br            pkt->setData(blk_data);
82512766Sqtt2@cornell.edu
82612766Sqtt2@cornell.edu            // execute AMO operation
82712766Sqtt2@cornell.edu            (*(pkt->getAtomicOp()))(blk_data);
82812766Sqtt2@cornell.edu
82912766Sqtt2@cornell.edu            // set block status to dirty
83012766Sqtt2@cornell.edu            blk->status |= BlkDirty;
83112766Sqtt2@cornell.edu        } else {
83212766Sqtt2@cornell.edu            cmpAndSwap(blk, pkt);
83312766Sqtt2@cornell.edu        }
83412724Snikos.nikoleris@arm.com    } else if (pkt->isWrite()) {
83512724Snikos.nikoleris@arm.com        // we have the block in a writable state and can go ahead,
83612724Snikos.nikoleris@arm.com        // note that the line may be also be considered writable in
83712724Snikos.nikoleris@arm.com        // downstream caches along the path to memory, but always
83812724Snikos.nikoleris@arm.com        // Exclusive, and never Modified
83912724Snikos.nikoleris@arm.com        assert(blk->isWritable());
84012724Snikos.nikoleris@arm.com        // Write or WriteLine at the first cache with block in writable state
84112724Snikos.nikoleris@arm.com        if (blk->checkWrite(pkt)) {
84212724Snikos.nikoleris@arm.com            pkt->writeDataToBlock(blk->data, blkSize);
84312724Snikos.nikoleris@arm.com        }
84412724Snikos.nikoleris@arm.com        // Always mark the line as dirty (and thus transition to the
84512724Snikos.nikoleris@arm.com        // Modified state) even if we are a failed StoreCond so we
84612724Snikos.nikoleris@arm.com        // supply data to any snoops that have appended themselves to
84712724Snikos.nikoleris@arm.com        // this cache before knowing the store will fail.
84812724Snikos.nikoleris@arm.com        blk->status |= BlkDirty;
84912724Snikos.nikoleris@arm.com        DPRINTF(CacheVerbose, "%s for %s (write)\n", __func__, pkt->print());
85012724Snikos.nikoleris@arm.com    } else if (pkt->isRead()) {
85112724Snikos.nikoleris@arm.com        if (pkt->isLLSC()) {
85212724Snikos.nikoleris@arm.com            blk->trackLoadLocked(pkt);
85312724Snikos.nikoleris@arm.com        }
85412724Snikos.nikoleris@arm.com
85512724Snikos.nikoleris@arm.com        // all read responses have a data payload
85612724Snikos.nikoleris@arm.com        assert(pkt->hasRespData());
85712724Snikos.nikoleris@arm.com        pkt->setDataFromBlock(blk->data, blkSize);
85812724Snikos.nikoleris@arm.com    } else if (pkt->isUpgrade()) {
85912724Snikos.nikoleris@arm.com        // sanity check
86012724Snikos.nikoleris@arm.com        assert(!pkt->hasSharers());
86112724Snikos.nikoleris@arm.com
86212724Snikos.nikoleris@arm.com        if (blk->isDirty()) {
86312724Snikos.nikoleris@arm.com            // we were in the Owned state, and a cache above us that
86412724Snikos.nikoleris@arm.com            // has the line in Shared state needs to be made aware
86512724Snikos.nikoleris@arm.com            // that the data it already has is in fact dirty
86612724Snikos.nikoleris@arm.com            pkt->setCacheResponding();
86712724Snikos.nikoleris@arm.com            blk->status &= ~BlkDirty;
86812724Snikos.nikoleris@arm.com        }
86912794Snikos.nikoleris@arm.com    } else if (pkt->isClean()) {
87012794Snikos.nikoleris@arm.com        blk->status &= ~BlkDirty;
87112724Snikos.nikoleris@arm.com    } else {
87212724Snikos.nikoleris@arm.com        assert(pkt->isInvalidate());
87312724Snikos.nikoleris@arm.com        invalidateBlock(blk);
87412724Snikos.nikoleris@arm.com        DPRINTF(CacheVerbose, "%s for %s (invalidation)\n", __func__,
87512724Snikos.nikoleris@arm.com                pkt->print());
87612724Snikos.nikoleris@arm.com    }
87712724Snikos.nikoleris@arm.com}
87812724Snikos.nikoleris@arm.com
87912724Snikos.nikoleris@arm.com/////////////////////////////////////////////////////
88012724Snikos.nikoleris@arm.com//
88112724Snikos.nikoleris@arm.com// Access path: requests coming in from the CPU side
88212724Snikos.nikoleris@arm.com//
88312724Snikos.nikoleris@arm.com/////////////////////////////////////////////////////
88413418Sodanrc@yahoo.com.brCycles
88513749Sodanrc@yahoo.com.brBaseCache::calculateTagOnlyLatency(const uint32_t delay,
88613749Sodanrc@yahoo.com.br                                   const Cycles lookup_lat) const
88713749Sodanrc@yahoo.com.br{
88813749Sodanrc@yahoo.com.br    // A tag-only access has to wait for the packet to arrive in order to
88913749Sodanrc@yahoo.com.br    // perform the tag lookup.
89013749Sodanrc@yahoo.com.br    return ticksToCycles(delay) + lookup_lat;
89113749Sodanrc@yahoo.com.br}
89213749Sodanrc@yahoo.com.br
89313749Sodanrc@yahoo.com.brCycles
89413746Sodanrc@yahoo.com.brBaseCache::calculateAccessLatency(const CacheBlk* blk, const uint32_t delay,
89513418Sodanrc@yahoo.com.br                                  const Cycles lookup_lat) const
89613418Sodanrc@yahoo.com.br{
89713746Sodanrc@yahoo.com.br    Cycles lat(0);
89813418Sodanrc@yahoo.com.br
89913418Sodanrc@yahoo.com.br    if (blk != nullptr) {
90013746Sodanrc@yahoo.com.br        // As soon as the access arrives, for sequential accesses first access
90113746Sodanrc@yahoo.com.br        // tags, then the data entry. In the case of parallel accesses the
90213746Sodanrc@yahoo.com.br        // latency is dictated by the slowest of tag and data latencies.
90313418Sodanrc@yahoo.com.br        if (sequentialAccess) {
90413746Sodanrc@yahoo.com.br            lat = ticksToCycles(delay) + lookup_lat + dataLatency;
90513418Sodanrc@yahoo.com.br        } else {
90613746Sodanrc@yahoo.com.br            lat = ticksToCycles(delay) + std::max(lookup_lat, dataLatency);
90713418Sodanrc@yahoo.com.br        }
90813418Sodanrc@yahoo.com.br
90913418Sodanrc@yahoo.com.br        // Check if the block to be accessed is available. If not, apply the
91013477Sodanrc@yahoo.com.br        // access latency on top of when the block is ready to be accessed.
91113746Sodanrc@yahoo.com.br        const Tick tick = curTick() + delay;
91213477Sodanrc@yahoo.com.br        const Tick when_ready = blk->getWhenReady();
91313746Sodanrc@yahoo.com.br        if (when_ready > tick &&
91413746Sodanrc@yahoo.com.br            ticksToCycles(when_ready - tick) > lat) {
91513746Sodanrc@yahoo.com.br            lat += ticksToCycles(when_ready - tick);
91613418Sodanrc@yahoo.com.br        }
91713746Sodanrc@yahoo.com.br    } else {
91813749Sodanrc@yahoo.com.br        // In case of a miss, we neglect the data access in a parallel
91913749Sodanrc@yahoo.com.br        // configuration (i.e., the data access will be stopped as soon as
92013749Sodanrc@yahoo.com.br        // we find out it is a miss), and use the tag-only latency.
92113749Sodanrc@yahoo.com.br        lat = calculateTagOnlyLatency(delay, lookup_lat);
92213418Sodanrc@yahoo.com.br    }
92313418Sodanrc@yahoo.com.br
92413418Sodanrc@yahoo.com.br    return lat;
92513418Sodanrc@yahoo.com.br}
92612724Snikos.nikoleris@arm.com
92712724Snikos.nikoleris@arm.combool
92812724Snikos.nikoleris@arm.comBaseCache::access(PacketPtr pkt, CacheBlk *&blk, Cycles &lat,
92912724Snikos.nikoleris@arm.com                  PacketList &writebacks)
93012724Snikos.nikoleris@arm.com{
93112724Snikos.nikoleris@arm.com    // sanity check
93212724Snikos.nikoleris@arm.com    assert(pkt->isRequest());
93312724Snikos.nikoleris@arm.com
93412724Snikos.nikoleris@arm.com    chatty_assert(!(isReadOnly && pkt->isWrite()),
93512724Snikos.nikoleris@arm.com                  "Should never see a write in a read-only cache %s\n",
93612724Snikos.nikoleris@arm.com                  name());
93712724Snikos.nikoleris@arm.com
93813418Sodanrc@yahoo.com.br    // Access block in the tags
93913418Sodanrc@yahoo.com.br    Cycles tag_latency(0);
94013418Sodanrc@yahoo.com.br    blk = tags->accessBlock(pkt->getAddr(), pkt->isSecure(), tag_latency);
94113418Sodanrc@yahoo.com.br
94212724Snikos.nikoleris@arm.com    DPRINTF(Cache, "%s for %s %s\n", __func__, pkt->print(),
94312724Snikos.nikoleris@arm.com            blk ? "hit " + blk->print() : "miss");
94412724Snikos.nikoleris@arm.com
94512724Snikos.nikoleris@arm.com    if (pkt->req->isCacheMaintenance()) {
94612724Snikos.nikoleris@arm.com        // A cache maintenance operation is always forwarded to the
94712724Snikos.nikoleris@arm.com        // memory below even if the block is found in dirty state.
94812724Snikos.nikoleris@arm.com
94912724Snikos.nikoleris@arm.com        // We defer any changes to the state of the block until we
95012724Snikos.nikoleris@arm.com        // create and mark as in service the mshr for the downstream
95112724Snikos.nikoleris@arm.com        // packet.
95213749Sodanrc@yahoo.com.br
95313749Sodanrc@yahoo.com.br        // Calculate access latency on top of when the packet arrives. This
95413749Sodanrc@yahoo.com.br        // takes into account the bus delay.
95513749Sodanrc@yahoo.com.br        lat = calculateTagOnlyLatency(pkt->headerDelay, tag_latency);
95613749Sodanrc@yahoo.com.br
95712724Snikos.nikoleris@arm.com        return false;
95812724Snikos.nikoleris@arm.com    }
95912724Snikos.nikoleris@arm.com
96012724Snikos.nikoleris@arm.com    if (pkt->isEviction()) {
96112724Snikos.nikoleris@arm.com        // We check for presence of block in above caches before issuing
96212724Snikos.nikoleris@arm.com        // Writeback or CleanEvict to write buffer. Therefore the only
96312724Snikos.nikoleris@arm.com        // possible cases can be of a CleanEvict packet coming from above
96412724Snikos.nikoleris@arm.com        // encountering a Writeback generated in this cache peer cache and
96512724Snikos.nikoleris@arm.com        // waiting in the write buffer. Cases of upper level peer caches
96612724Snikos.nikoleris@arm.com        // generating CleanEvict and Writeback or simply CleanEvict and
96712724Snikos.nikoleris@arm.com        // CleanEvict almost simultaneously will be caught by snoops sent out
96812724Snikos.nikoleris@arm.com        // by crossbar.
96912724Snikos.nikoleris@arm.com        WriteQueueEntry *wb_entry = writeBuffer.findMatch(pkt->getAddr(),
97012724Snikos.nikoleris@arm.com                                                          pkt->isSecure());
97112724Snikos.nikoleris@arm.com        if (wb_entry) {
97212724Snikos.nikoleris@arm.com            assert(wb_entry->getNumTargets() == 1);
97312724Snikos.nikoleris@arm.com            PacketPtr wbPkt = wb_entry->getTarget()->pkt;
97412724Snikos.nikoleris@arm.com            assert(wbPkt->isWriteback());
97512724Snikos.nikoleris@arm.com
97612724Snikos.nikoleris@arm.com            if (pkt->isCleanEviction()) {
97712724Snikos.nikoleris@arm.com                // The CleanEvict and WritebackClean snoops into other
97812724Snikos.nikoleris@arm.com                // peer caches of the same level while traversing the
97912724Snikos.nikoleris@arm.com                // crossbar. If a copy of the block is found, the
98012724Snikos.nikoleris@arm.com                // packet is deleted in the crossbar. Hence, none of
98112724Snikos.nikoleris@arm.com                // the other upper level caches connected to this
98212724Snikos.nikoleris@arm.com                // cache have the block, so we can clear the
98312724Snikos.nikoleris@arm.com                // BLOCK_CACHED flag in the Writeback if set and
98412724Snikos.nikoleris@arm.com                // discard the CleanEvict by returning true.
98512724Snikos.nikoleris@arm.com                wbPkt->clearBlockCached();
98613749Sodanrc@yahoo.com.br
98713749Sodanrc@yahoo.com.br                // A clean evict does not need to access the data array
98813749Sodanrc@yahoo.com.br                lat = calculateTagOnlyLatency(pkt->headerDelay, tag_latency);
98913749Sodanrc@yahoo.com.br
99012724Snikos.nikoleris@arm.com                return true;
99112724Snikos.nikoleris@arm.com            } else {
99212724Snikos.nikoleris@arm.com                assert(pkt->cmd == MemCmd::WritebackDirty);
99312724Snikos.nikoleris@arm.com                // Dirty writeback from above trumps our clean
99412724Snikos.nikoleris@arm.com                // writeback... discard here
99512724Snikos.nikoleris@arm.com                // Note: markInService will remove entry from writeback buffer.
99612724Snikos.nikoleris@arm.com                markInService(wb_entry);
99712724Snikos.nikoleris@arm.com                delete wbPkt;
99812724Snikos.nikoleris@arm.com            }
99912724Snikos.nikoleris@arm.com        }
100012724Snikos.nikoleris@arm.com    }
100112724Snikos.nikoleris@arm.com
100212724Snikos.nikoleris@arm.com    // Writeback handling is special case.  We can write the block into
100312724Snikos.nikoleris@arm.com    // the cache without having a writeable copy (or any copy at all).
100412724Snikos.nikoleris@arm.com    if (pkt->isWriteback()) {
100512724Snikos.nikoleris@arm.com        assert(blkSize == pkt->getSize());
100612724Snikos.nikoleris@arm.com
100712724Snikos.nikoleris@arm.com        // we could get a clean writeback while we are having
100812724Snikos.nikoleris@arm.com        // outstanding accesses to a block, do the simple thing for
100912724Snikos.nikoleris@arm.com        // now and drop the clean writeback so that we do not upset
101012724Snikos.nikoleris@arm.com        // any ordering/decisions about ownership already taken
101112724Snikos.nikoleris@arm.com        if (pkt->cmd == MemCmd::WritebackClean &&
101212724Snikos.nikoleris@arm.com            mshrQueue.findMatch(pkt->getAddr(), pkt->isSecure())) {
101312724Snikos.nikoleris@arm.com            DPRINTF(Cache, "Clean writeback %#llx to block with MSHR, "
101412724Snikos.nikoleris@arm.com                    "dropping\n", pkt->getAddr());
101513749Sodanrc@yahoo.com.br
101613749Sodanrc@yahoo.com.br            // A writeback searches for the block, then writes the data.
101713749Sodanrc@yahoo.com.br            // As the writeback is being dropped, the data is not touched,
101813749Sodanrc@yahoo.com.br            // and we just had to wait for the time to find a match in the
101913749Sodanrc@yahoo.com.br            // MSHR. As of now assume a mshr queue search takes as long as
102013749Sodanrc@yahoo.com.br            // a tag lookup for simplicity.
102113749Sodanrc@yahoo.com.br            lat = calculateTagOnlyLatency(pkt->headerDelay, tag_latency);
102213749Sodanrc@yahoo.com.br
102312724Snikos.nikoleris@arm.com            return true;
102412724Snikos.nikoleris@arm.com        }
102512724Snikos.nikoleris@arm.com
102612724Snikos.nikoleris@arm.com        if (!blk) {
102712724Snikos.nikoleris@arm.com            // need to do a replacement
102812754Sodanrc@yahoo.com.br            blk = allocateBlock(pkt, writebacks);
102912724Snikos.nikoleris@arm.com            if (!blk) {
103012724Snikos.nikoleris@arm.com                // no replaceable block available: give up, fwd to next level.
103112724Snikos.nikoleris@arm.com                incMissCount(pkt);
103213749Sodanrc@yahoo.com.br
103313749Sodanrc@yahoo.com.br                // A writeback searches for the block, then writes the data.
103413749Sodanrc@yahoo.com.br                // As the block could not be found, it was a tag-only access.
103513749Sodanrc@yahoo.com.br                lat = calculateTagOnlyLatency(pkt->headerDelay, tag_latency);
103613749Sodanrc@yahoo.com.br
103712724Snikos.nikoleris@arm.com                return false;
103812724Snikos.nikoleris@arm.com            }
103912724Snikos.nikoleris@arm.com
104013445Sodanrc@yahoo.com.br            blk->status |= BlkReadable;
104112724Snikos.nikoleris@arm.com        }
104212724Snikos.nikoleris@arm.com        // only mark the block dirty if we got a writeback command,
104312724Snikos.nikoleris@arm.com        // and leave it as is for a clean writeback
104412724Snikos.nikoleris@arm.com        if (pkt->cmd == MemCmd::WritebackDirty) {
104512724Snikos.nikoleris@arm.com            // TODO: the coherent cache can assert(!blk->isDirty());
104612724Snikos.nikoleris@arm.com            blk->status |= BlkDirty;
104712724Snikos.nikoleris@arm.com        }
104812724Snikos.nikoleris@arm.com        // if the packet does not have sharers, it is passing
104912724Snikos.nikoleris@arm.com        // writable, and we got the writeback in Modified or Exclusive
105012724Snikos.nikoleris@arm.com        // state, if not we are in the Owned or Shared state
105112724Snikos.nikoleris@arm.com        if (!pkt->hasSharers()) {
105212724Snikos.nikoleris@arm.com            blk->status |= BlkWritable;
105312724Snikos.nikoleris@arm.com        }
105412724Snikos.nikoleris@arm.com        // nothing else to do; writeback doesn't expect response
105512724Snikos.nikoleris@arm.com        assert(!pkt->needsResponse());
105612724Snikos.nikoleris@arm.com        pkt->writeDataToBlock(blk->data, blkSize);
105712724Snikos.nikoleris@arm.com        DPRINTF(Cache, "%s new state is %s\n", __func__, blk->print());
105812724Snikos.nikoleris@arm.com        incHitCount(pkt);
105913748Sodanrc@yahoo.com.br
106013765Sodanrc@yahoo.com.br        // A writeback searches for the block, then writes the data
106113765Sodanrc@yahoo.com.br        lat = calculateAccessLatency(blk, pkt->headerDelay, tag_latency);
106213765Sodanrc@yahoo.com.br
106313748Sodanrc@yahoo.com.br        // When the packet metadata arrives, the tag lookup will be done while
106413748Sodanrc@yahoo.com.br        // the payload is arriving. Then the block will be ready to access as
106513748Sodanrc@yahoo.com.br        // soon as the fill is done
106613477Sodanrc@yahoo.com.br        blk->setWhenReady(clockEdge(fillLatency) + pkt->headerDelay +
106713748Sodanrc@yahoo.com.br            std::max(cyclesToTicks(tag_latency), (uint64_t)pkt->payloadDelay));
106813749Sodanrc@yahoo.com.br
106912724Snikos.nikoleris@arm.com        return true;
107012724Snikos.nikoleris@arm.com    } else if (pkt->cmd == MemCmd::CleanEvict) {
107113749Sodanrc@yahoo.com.br        // A CleanEvict does not need to access the data array
107213749Sodanrc@yahoo.com.br        lat = calculateTagOnlyLatency(pkt->headerDelay, tag_latency);
107313749Sodanrc@yahoo.com.br
107412724Snikos.nikoleris@arm.com        if (blk) {
107512724Snikos.nikoleris@arm.com            // Found the block in the tags, need to stop CleanEvict from
107612724Snikos.nikoleris@arm.com            // propagating further down the hierarchy. Returning true will
107712724Snikos.nikoleris@arm.com            // treat the CleanEvict like a satisfied write request and delete
107812724Snikos.nikoleris@arm.com            // it.
107912724Snikos.nikoleris@arm.com            return true;
108012724Snikos.nikoleris@arm.com        }
108112724Snikos.nikoleris@arm.com        // We didn't find the block here, propagate the CleanEvict further
108212724Snikos.nikoleris@arm.com        // down the memory hierarchy. Returning false will treat the CleanEvict
108312724Snikos.nikoleris@arm.com        // like a Writeback which could not find a replaceable block so has to
108412724Snikos.nikoleris@arm.com        // go to next level.
108512724Snikos.nikoleris@arm.com        return false;
108612724Snikos.nikoleris@arm.com    } else if (pkt->cmd == MemCmd::WriteClean) {
108712724Snikos.nikoleris@arm.com        // WriteClean handling is a special case. We can allocate a
108812724Snikos.nikoleris@arm.com        // block directly if it doesn't exist and we can update the
108912724Snikos.nikoleris@arm.com        // block immediately. The WriteClean transfers the ownership
109012724Snikos.nikoleris@arm.com        // of the block as well.
109112724Snikos.nikoleris@arm.com        assert(blkSize == pkt->getSize());
109212724Snikos.nikoleris@arm.com
109312724Snikos.nikoleris@arm.com        if (!blk) {
109412724Snikos.nikoleris@arm.com            if (pkt->writeThrough()) {
109513749Sodanrc@yahoo.com.br                // A writeback searches for the block, then writes the data.
109613749Sodanrc@yahoo.com.br                // As the block could not be found, it was a tag-only access.
109713749Sodanrc@yahoo.com.br                lat = calculateTagOnlyLatency(pkt->headerDelay, tag_latency);
109813749Sodanrc@yahoo.com.br
109912724Snikos.nikoleris@arm.com                // if this is a write through packet, we don't try to
110012724Snikos.nikoleris@arm.com                // allocate if the block is not present
110112724Snikos.nikoleris@arm.com                return false;
110212724Snikos.nikoleris@arm.com            } else {
110312724Snikos.nikoleris@arm.com                // a writeback that misses needs to allocate a new block
110412754Sodanrc@yahoo.com.br                blk = allocateBlock(pkt, writebacks);
110512724Snikos.nikoleris@arm.com                if (!blk) {
110612724Snikos.nikoleris@arm.com                    // no replaceable block available: give up, fwd to
110712724Snikos.nikoleris@arm.com                    // next level.
110812724Snikos.nikoleris@arm.com                    incMissCount(pkt);
110913749Sodanrc@yahoo.com.br
111013749Sodanrc@yahoo.com.br                    // A writeback searches for the block, then writes the
111113749Sodanrc@yahoo.com.br                    // data. As the block could not be found, it was a tag-only
111213749Sodanrc@yahoo.com.br                    // access.
111313749Sodanrc@yahoo.com.br                    lat = calculateTagOnlyLatency(pkt->headerDelay,
111413749Sodanrc@yahoo.com.br                                                  tag_latency);
111513749Sodanrc@yahoo.com.br
111612724Snikos.nikoleris@arm.com                    return false;
111712724Snikos.nikoleris@arm.com                }
111812724Snikos.nikoleris@arm.com
111913445Sodanrc@yahoo.com.br                blk->status |= BlkReadable;
112012724Snikos.nikoleris@arm.com            }
112112724Snikos.nikoleris@arm.com        }
112212724Snikos.nikoleris@arm.com
112312724Snikos.nikoleris@arm.com        // at this point either this is a writeback or a write-through
112412724Snikos.nikoleris@arm.com        // write clean operation and the block is already in this
112512724Snikos.nikoleris@arm.com        // cache, we need to update the data and the block flags
112612724Snikos.nikoleris@arm.com        assert(blk);
112712724Snikos.nikoleris@arm.com        // TODO: the coherent cache can assert(!blk->isDirty());
112812724Snikos.nikoleris@arm.com        if (!pkt->writeThrough()) {
112912724Snikos.nikoleris@arm.com            blk->status |= BlkDirty;
113012724Snikos.nikoleris@arm.com        }
113112724Snikos.nikoleris@arm.com        // nothing else to do; writeback doesn't expect response
113212724Snikos.nikoleris@arm.com        assert(!pkt->needsResponse());
113312724Snikos.nikoleris@arm.com        pkt->writeDataToBlock(blk->data, blkSize);
113412724Snikos.nikoleris@arm.com        DPRINTF(Cache, "%s new state is %s\n", __func__, blk->print());
113512724Snikos.nikoleris@arm.com
113612724Snikos.nikoleris@arm.com        incHitCount(pkt);
113713748Sodanrc@yahoo.com.br
113813765Sodanrc@yahoo.com.br        // A writeback searches for the block, then writes the data
113913765Sodanrc@yahoo.com.br        lat = calculateAccessLatency(blk, pkt->headerDelay, tag_latency);
114013765Sodanrc@yahoo.com.br
114113748Sodanrc@yahoo.com.br        // When the packet metadata arrives, the tag lookup will be done while
114213748Sodanrc@yahoo.com.br        // the payload is arriving. Then the block will be ready to access as
114313748Sodanrc@yahoo.com.br        // soon as the fill is done
114413477Sodanrc@yahoo.com.br        blk->setWhenReady(clockEdge(fillLatency) + pkt->headerDelay +
114513748Sodanrc@yahoo.com.br            std::max(cyclesToTicks(tag_latency), (uint64_t)pkt->payloadDelay));
114613748Sodanrc@yahoo.com.br
114712724Snikos.nikoleris@arm.com        // if this a write-through packet it will be sent to cache
114812724Snikos.nikoleris@arm.com        // below
114912724Snikos.nikoleris@arm.com        return !pkt->writeThrough();
115012724Snikos.nikoleris@arm.com    } else if (blk && (pkt->needsWritable() ? blk->isWritable() :
115112724Snikos.nikoleris@arm.com                       blk->isReadable())) {
115212724Snikos.nikoleris@arm.com        // OK to satisfy access
115312724Snikos.nikoleris@arm.com        incHitCount(pkt);
115412724Snikos.nikoleris@arm.com
115513749Sodanrc@yahoo.com.br        // Calculate access latency based on the need to access the data array
115613749Sodanrc@yahoo.com.br        if (pkt->isRead() || pkt->isWrite()) {
115713749Sodanrc@yahoo.com.br            lat = calculateAccessLatency(blk, pkt->headerDelay, tag_latency);
115813749Sodanrc@yahoo.com.br        } else {
115913749Sodanrc@yahoo.com.br            lat = calculateTagOnlyLatency(pkt->headerDelay, tag_latency);
116013749Sodanrc@yahoo.com.br        }
116113749Sodanrc@yahoo.com.br
116213765Sodanrc@yahoo.com.br        satisfyRequest(pkt, blk);
116313765Sodanrc@yahoo.com.br        maintainClusivity(pkt->fromCache(), blk);
116413765Sodanrc@yahoo.com.br
116512724Snikos.nikoleris@arm.com        return true;
116612724Snikos.nikoleris@arm.com    }
116712724Snikos.nikoleris@arm.com
116812724Snikos.nikoleris@arm.com    // Can't satisfy access normally... either no block (blk == nullptr)
116912724Snikos.nikoleris@arm.com    // or have block but need writable
117012724Snikos.nikoleris@arm.com
117112724Snikos.nikoleris@arm.com    incMissCount(pkt);
117212724Snikos.nikoleris@arm.com
117313749Sodanrc@yahoo.com.br    lat = calculateAccessLatency(blk, pkt->headerDelay, tag_latency);
117413749Sodanrc@yahoo.com.br
117512724Snikos.nikoleris@arm.com    if (!blk && pkt->isLLSC() && pkt->isWrite()) {
117612724Snikos.nikoleris@arm.com        // complete miss on store conditional... just give up now
117712724Snikos.nikoleris@arm.com        pkt->req->setExtraData(0);
117812724Snikos.nikoleris@arm.com        return true;
117912724Snikos.nikoleris@arm.com    }
118012724Snikos.nikoleris@arm.com
118112724Snikos.nikoleris@arm.com    return false;
118212724Snikos.nikoleris@arm.com}
118312724Snikos.nikoleris@arm.com
118412724Snikos.nikoleris@arm.comvoid
118512724Snikos.nikoleris@arm.comBaseCache::maintainClusivity(bool from_cache, CacheBlk *blk)
118612724Snikos.nikoleris@arm.com{
118712724Snikos.nikoleris@arm.com    if (from_cache && blk && blk->isValid() && !blk->isDirty() &&
118812724Snikos.nikoleris@arm.com        clusivity == Enums::mostly_excl) {
118912724Snikos.nikoleris@arm.com        // if we have responded to a cache, and our block is still
119012724Snikos.nikoleris@arm.com        // valid, but not dirty, and this cache is mostly exclusive
119112724Snikos.nikoleris@arm.com        // with respect to the cache above, drop the block
119212724Snikos.nikoleris@arm.com        invalidateBlock(blk);
119312724Snikos.nikoleris@arm.com    }
119412724Snikos.nikoleris@arm.com}
119512724Snikos.nikoleris@arm.com
119612724Snikos.nikoleris@arm.comCacheBlk*
119712724Snikos.nikoleris@arm.comBaseCache::handleFill(PacketPtr pkt, CacheBlk *blk, PacketList &writebacks,
119812724Snikos.nikoleris@arm.com                      bool allocate)
119912724Snikos.nikoleris@arm.com{
120013350Snikos.nikoleris@arm.com    assert(pkt->isResponse());
120112724Snikos.nikoleris@arm.com    Addr addr = pkt->getAddr();
120212724Snikos.nikoleris@arm.com    bool is_secure = pkt->isSecure();
120312724Snikos.nikoleris@arm.com#if TRACING_ON
120412724Snikos.nikoleris@arm.com    CacheBlk::State old_state = blk ? blk->status : 0;
120512724Snikos.nikoleris@arm.com#endif
120612724Snikos.nikoleris@arm.com
120712724Snikos.nikoleris@arm.com    // When handling a fill, we should have no writes to this line.
120812724Snikos.nikoleris@arm.com    assert(addr == pkt->getBlockAddr(blkSize));
120912724Snikos.nikoleris@arm.com    assert(!writeBuffer.findMatch(addr, is_secure));
121012724Snikos.nikoleris@arm.com
121112724Snikos.nikoleris@arm.com    if (!blk) {
121212724Snikos.nikoleris@arm.com        // better have read new data...
121313350Snikos.nikoleris@arm.com        assert(pkt->hasData() || pkt->cmd == MemCmd::InvalidateResp);
121412724Snikos.nikoleris@arm.com
121512724Snikos.nikoleris@arm.com        // need to do a replacement if allocating, otherwise we stick
121612724Snikos.nikoleris@arm.com        // with the temporary storage
121712754Sodanrc@yahoo.com.br        blk = allocate ? allocateBlock(pkt, writebacks) : nullptr;
121812724Snikos.nikoleris@arm.com
121912724Snikos.nikoleris@arm.com        if (!blk) {
122012724Snikos.nikoleris@arm.com            // No replaceable block or a mostly exclusive
122112724Snikos.nikoleris@arm.com            // cache... just use temporary storage to complete the
122212724Snikos.nikoleris@arm.com            // current request and then get rid of it
122312724Snikos.nikoleris@arm.com            blk = tempBlock;
122412730Sodanrc@yahoo.com.br            tempBlock->insert(addr, is_secure);
122512724Snikos.nikoleris@arm.com            DPRINTF(Cache, "using temp block for %#llx (%s)\n", addr,
122612724Snikos.nikoleris@arm.com                    is_secure ? "s" : "ns");
122712724Snikos.nikoleris@arm.com        }
122812724Snikos.nikoleris@arm.com    } else {
122912724Snikos.nikoleris@arm.com        // existing block... probably an upgrade
123012724Snikos.nikoleris@arm.com        // don't clear block status... if block is already dirty we
123112724Snikos.nikoleris@arm.com        // don't want to lose that
123212724Snikos.nikoleris@arm.com    }
123312724Snikos.nikoleris@arm.com
123413445Sodanrc@yahoo.com.br    // Block is guaranteed to be valid at this point
123513445Sodanrc@yahoo.com.br    assert(blk->isValid());
123613445Sodanrc@yahoo.com.br    assert(blk->isSecure() == is_secure);
123713445Sodanrc@yahoo.com.br    assert(regenerateBlkAddr(blk) == addr);
123813445Sodanrc@yahoo.com.br
123913445Sodanrc@yahoo.com.br    blk->status |= BlkReadable;
124012724Snikos.nikoleris@arm.com
124112724Snikos.nikoleris@arm.com    // sanity check for whole-line writes, which should always be
124212724Snikos.nikoleris@arm.com    // marked as writable as part of the fill, and then later marked
124312724Snikos.nikoleris@arm.com    // dirty as part of satisfyRequest
124413350Snikos.nikoleris@arm.com    if (pkt->cmd == MemCmd::InvalidateResp) {
124512724Snikos.nikoleris@arm.com        assert(!pkt->hasSharers());
124612724Snikos.nikoleris@arm.com    }
124712724Snikos.nikoleris@arm.com
124812724Snikos.nikoleris@arm.com    // here we deal with setting the appropriate state of the line,
124912724Snikos.nikoleris@arm.com    // and we start by looking at the hasSharers flag, and ignore the
125012724Snikos.nikoleris@arm.com    // cacheResponding flag (normally signalling dirty data) if the
125112724Snikos.nikoleris@arm.com    // packet has sharers, thus the line is never allocated as Owned
125212724Snikos.nikoleris@arm.com    // (dirty but not writable), and always ends up being either
125312724Snikos.nikoleris@arm.com    // Shared, Exclusive or Modified, see Packet::setCacheResponding
125412724Snikos.nikoleris@arm.com    // for more details
125512724Snikos.nikoleris@arm.com    if (!pkt->hasSharers()) {
125612724Snikos.nikoleris@arm.com        // we could get a writable line from memory (rather than a
125712724Snikos.nikoleris@arm.com        // cache) even in a read-only cache, note that we set this bit
125812724Snikos.nikoleris@arm.com        // even for a read-only cache, possibly revisit this decision
125912724Snikos.nikoleris@arm.com        blk->status |= BlkWritable;
126012724Snikos.nikoleris@arm.com
126112724Snikos.nikoleris@arm.com        // check if we got this via cache-to-cache transfer (i.e., from a
126212724Snikos.nikoleris@arm.com        // cache that had the block in Modified or Owned state)
126312724Snikos.nikoleris@arm.com        if (pkt->cacheResponding()) {
126412724Snikos.nikoleris@arm.com            // we got the block in Modified state, and invalidated the
126512724Snikos.nikoleris@arm.com            // owners copy
126612724Snikos.nikoleris@arm.com            blk->status |= BlkDirty;
126712724Snikos.nikoleris@arm.com
126812724Snikos.nikoleris@arm.com            chatty_assert(!isReadOnly, "Should never see dirty snoop response "
126912724Snikos.nikoleris@arm.com                          "in read-only cache %s\n", name());
127012724Snikos.nikoleris@arm.com        }
127112724Snikos.nikoleris@arm.com    }
127212724Snikos.nikoleris@arm.com
127312724Snikos.nikoleris@arm.com    DPRINTF(Cache, "Block addr %#llx (%s) moving from state %x to %s\n",
127412724Snikos.nikoleris@arm.com            addr, is_secure ? "s" : "ns", old_state, blk->print());
127512724Snikos.nikoleris@arm.com
127612724Snikos.nikoleris@arm.com    // if we got new data, copy it in (checking for a read response
127712724Snikos.nikoleris@arm.com    // and a response that has data is the same in the end)
127812724Snikos.nikoleris@arm.com    if (pkt->isRead()) {
127912724Snikos.nikoleris@arm.com        // sanity checks
128012724Snikos.nikoleris@arm.com        assert(pkt->hasData());
128112724Snikos.nikoleris@arm.com        assert(pkt->getSize() == blkSize);
128212724Snikos.nikoleris@arm.com
128312724Snikos.nikoleris@arm.com        pkt->writeDataToBlock(blk->data, blkSize);
128412724Snikos.nikoleris@arm.com    }
128513750Sodanrc@yahoo.com.br    // The block will be ready when the payload arrives and the fill is done
128613750Sodanrc@yahoo.com.br    blk->setWhenReady(clockEdge(fillLatency) + pkt->headerDelay +
128713750Sodanrc@yahoo.com.br                      pkt->payloadDelay);
128812724Snikos.nikoleris@arm.com
128912724Snikos.nikoleris@arm.com    return blk;
129012724Snikos.nikoleris@arm.com}
129112724Snikos.nikoleris@arm.com
129212724Snikos.nikoleris@arm.comCacheBlk*
129312754Sodanrc@yahoo.com.brBaseCache::allocateBlock(const PacketPtr pkt, PacketList &writebacks)
129412724Snikos.nikoleris@arm.com{
129512754Sodanrc@yahoo.com.br    // Get address
129612754Sodanrc@yahoo.com.br    const Addr addr = pkt->getAddr();
129712754Sodanrc@yahoo.com.br
129812754Sodanrc@yahoo.com.br    // Get secure bit
129912754Sodanrc@yahoo.com.br    const bool is_secure = pkt->isSecure();
130012754Sodanrc@yahoo.com.br
130112724Snikos.nikoleris@arm.com    // Find replacement victim
130212744Sodanrc@yahoo.com.br    std::vector<CacheBlk*> evict_blks;
130312746Sodanrc@yahoo.com.br    CacheBlk *victim = tags->findVictim(addr, is_secure, evict_blks);
130412724Snikos.nikoleris@arm.com
130512724Snikos.nikoleris@arm.com    // It is valid to return nullptr if there is no victim
130612744Sodanrc@yahoo.com.br    if (!victim)
130712724Snikos.nikoleris@arm.com        return nullptr;
130812724Snikos.nikoleris@arm.com
130913222Sodanrc@yahoo.com.br    // Print victim block's information
131013222Sodanrc@yahoo.com.br    DPRINTF(CacheRepl, "Replacement victim: %s\n", victim->print());
131113222Sodanrc@yahoo.com.br
131212744Sodanrc@yahoo.com.br    // Check for transient state allocations. If any of the entries listed
131312744Sodanrc@yahoo.com.br    // for eviction has a transient state, the allocation fails
131412744Sodanrc@yahoo.com.br    for (const auto& blk : evict_blks) {
131512744Sodanrc@yahoo.com.br        if (blk->isValid()) {
131612744Sodanrc@yahoo.com.br            Addr repl_addr = regenerateBlkAddr(blk);
131712744Sodanrc@yahoo.com.br            MSHR *repl_mshr = mshrQueue.findMatch(repl_addr, blk->isSecure());
131812744Sodanrc@yahoo.com.br            if (repl_mshr) {
131912744Sodanrc@yahoo.com.br                // must be an outstanding upgrade or clean request
132012744Sodanrc@yahoo.com.br                // on a block we're about to replace...
132112744Sodanrc@yahoo.com.br                assert((!blk->isWritable() && repl_mshr->needsWritable()) ||
132212744Sodanrc@yahoo.com.br                       repl_mshr->isCleaning());
132312724Snikos.nikoleris@arm.com
132412744Sodanrc@yahoo.com.br                // too hard to replace block with transient state
132512744Sodanrc@yahoo.com.br                // allocation failed, block not inserted
132612744Sodanrc@yahoo.com.br                return nullptr;
132712744Sodanrc@yahoo.com.br            }
132812744Sodanrc@yahoo.com.br        }
132912744Sodanrc@yahoo.com.br    }
133012744Sodanrc@yahoo.com.br
133112744Sodanrc@yahoo.com.br    // The victim will be replaced by a new entry, so increase the replacement
133212744Sodanrc@yahoo.com.br    // counter if a valid block is being replaced
133312744Sodanrc@yahoo.com.br    if (victim->isValid()) {
133412744Sodanrc@yahoo.com.br        DPRINTF(Cache, "replacement: replacing %#llx (%s) with %#llx "
133512744Sodanrc@yahoo.com.br                "(%s): %s\n", regenerateBlkAddr(victim),
133612744Sodanrc@yahoo.com.br                victim->isSecure() ? "s" : "ns",
133712744Sodanrc@yahoo.com.br                addr, is_secure ? "s" : "ns",
133812744Sodanrc@yahoo.com.br                victim->isDirty() ? "writeback" : "clean");
133912744Sodanrc@yahoo.com.br
134012744Sodanrc@yahoo.com.br        replacements++;
134112744Sodanrc@yahoo.com.br    }
134212744Sodanrc@yahoo.com.br
134312744Sodanrc@yahoo.com.br    // Evict valid blocks associated to this victim block
134412744Sodanrc@yahoo.com.br    for (const auto& blk : evict_blks) {
134512744Sodanrc@yahoo.com.br        if (blk->isValid()) {
134612724Snikos.nikoleris@arm.com            if (blk->wasPrefetched()) {
134712724Snikos.nikoleris@arm.com                unusedPrefetches++;
134812724Snikos.nikoleris@arm.com            }
134912744Sodanrc@yahoo.com.br
135012724Snikos.nikoleris@arm.com            evictBlock(blk, writebacks);
135112724Snikos.nikoleris@arm.com        }
135212724Snikos.nikoleris@arm.com    }
135312724Snikos.nikoleris@arm.com
135412754Sodanrc@yahoo.com.br    // Insert new block at victimized entry
135513752Sodanrc@yahoo.com.br    tags->insertBlock(pkt, victim);
135612754Sodanrc@yahoo.com.br
135712744Sodanrc@yahoo.com.br    return victim;
135812724Snikos.nikoleris@arm.com}
135912724Snikos.nikoleris@arm.com
136012724Snikos.nikoleris@arm.comvoid
136112724Snikos.nikoleris@arm.comBaseCache::invalidateBlock(CacheBlk *blk)
136212724Snikos.nikoleris@arm.com{
136313376Sodanrc@yahoo.com.br    // If handling a block present in the Tags, let it do its invalidation
136413376Sodanrc@yahoo.com.br    // process, which will update stats and invalidate the block itself
136513376Sodanrc@yahoo.com.br    if (blk != tempBlock) {
136612724Snikos.nikoleris@arm.com        tags->invalidate(blk);
136713376Sodanrc@yahoo.com.br    } else {
136813376Sodanrc@yahoo.com.br        tempBlock->invalidate();
136913376Sodanrc@yahoo.com.br    }
137012724Snikos.nikoleris@arm.com}
137112724Snikos.nikoleris@arm.com
137213358Sodanrc@yahoo.com.brvoid
137313358Sodanrc@yahoo.com.brBaseCache::evictBlock(CacheBlk *blk, PacketList &writebacks)
137413358Sodanrc@yahoo.com.br{
137513358Sodanrc@yahoo.com.br    PacketPtr pkt = evictBlock(blk);
137613358Sodanrc@yahoo.com.br    if (pkt) {
137713358Sodanrc@yahoo.com.br        writebacks.push_back(pkt);
137813358Sodanrc@yahoo.com.br    }
137913358Sodanrc@yahoo.com.br}
138013358Sodanrc@yahoo.com.br
138112724Snikos.nikoleris@arm.comPacketPtr
138212724Snikos.nikoleris@arm.comBaseCache::writebackBlk(CacheBlk *blk)
138312724Snikos.nikoleris@arm.com{
138412724Snikos.nikoleris@arm.com    chatty_assert(!isReadOnly || writebackClean,
138512724Snikos.nikoleris@arm.com                  "Writeback from read-only cache");
138612724Snikos.nikoleris@arm.com    assert(blk && blk->isValid() && (blk->isDirty() || writebackClean));
138712724Snikos.nikoleris@arm.com
138812724Snikos.nikoleris@arm.com    writebacks[Request::wbMasterId]++;
138912724Snikos.nikoleris@arm.com
139012749Sgiacomo.travaglini@arm.com    RequestPtr req = std::make_shared<Request>(
139112749Sgiacomo.travaglini@arm.com        regenerateBlkAddr(blk), blkSize, 0, Request::wbMasterId);
139212749Sgiacomo.travaglini@arm.com
139312724Snikos.nikoleris@arm.com    if (blk->isSecure())
139412724Snikos.nikoleris@arm.com        req->setFlags(Request::SECURE);
139512724Snikos.nikoleris@arm.com
139612724Snikos.nikoleris@arm.com    req->taskId(blk->task_id);
139712724Snikos.nikoleris@arm.com
139812724Snikos.nikoleris@arm.com    PacketPtr pkt =
139912724Snikos.nikoleris@arm.com        new Packet(req, blk->isDirty() ?
140012724Snikos.nikoleris@arm.com                   MemCmd::WritebackDirty : MemCmd::WritebackClean);
140112724Snikos.nikoleris@arm.com
140212724Snikos.nikoleris@arm.com    DPRINTF(Cache, "Create Writeback %s writable: %d, dirty: %d\n",
140312724Snikos.nikoleris@arm.com            pkt->print(), blk->isWritable(), blk->isDirty());
140412724Snikos.nikoleris@arm.com
140512724Snikos.nikoleris@arm.com    if (blk->isWritable()) {
140612724Snikos.nikoleris@arm.com        // not asserting shared means we pass the block in modified
140712724Snikos.nikoleris@arm.com        // state, mark our own block non-writeable
140812724Snikos.nikoleris@arm.com        blk->status &= ~BlkWritable;
140912724Snikos.nikoleris@arm.com    } else {
141012724Snikos.nikoleris@arm.com        // we are in the Owned state, tell the receiver
141112724Snikos.nikoleris@arm.com        pkt->setHasSharers();
141212724Snikos.nikoleris@arm.com    }
141312724Snikos.nikoleris@arm.com
141412724Snikos.nikoleris@arm.com    // make sure the block is not marked dirty
141512724Snikos.nikoleris@arm.com    blk->status &= ~BlkDirty;
141612724Snikos.nikoleris@arm.com
141712724Snikos.nikoleris@arm.com    pkt->allocate();
141812724Snikos.nikoleris@arm.com    pkt->setDataFromBlock(blk->data, blkSize);
141912724Snikos.nikoleris@arm.com
142012724Snikos.nikoleris@arm.com    return pkt;
142112724Snikos.nikoleris@arm.com}
142212724Snikos.nikoleris@arm.com
142312724Snikos.nikoleris@arm.comPacketPtr
142412724Snikos.nikoleris@arm.comBaseCache::writecleanBlk(CacheBlk *blk, Request::Flags dest, PacketId id)
142512724Snikos.nikoleris@arm.com{
142612749Sgiacomo.travaglini@arm.com    RequestPtr req = std::make_shared<Request>(
142712749Sgiacomo.travaglini@arm.com        regenerateBlkAddr(blk), blkSize, 0, Request::wbMasterId);
142812749Sgiacomo.travaglini@arm.com
142912724Snikos.nikoleris@arm.com    if (blk->isSecure()) {
143012724Snikos.nikoleris@arm.com        req->setFlags(Request::SECURE);
143112724Snikos.nikoleris@arm.com    }
143212724Snikos.nikoleris@arm.com    req->taskId(blk->task_id);
143312724Snikos.nikoleris@arm.com
143412724Snikos.nikoleris@arm.com    PacketPtr pkt = new Packet(req, MemCmd::WriteClean, blkSize, id);
143512724Snikos.nikoleris@arm.com
143612724Snikos.nikoleris@arm.com    if (dest) {
143712724Snikos.nikoleris@arm.com        req->setFlags(dest);
143812724Snikos.nikoleris@arm.com        pkt->setWriteThrough();
143912724Snikos.nikoleris@arm.com    }
144012724Snikos.nikoleris@arm.com
144112724Snikos.nikoleris@arm.com    DPRINTF(Cache, "Create %s writable: %d, dirty: %d\n", pkt->print(),
144212724Snikos.nikoleris@arm.com            blk->isWritable(), blk->isDirty());
144312724Snikos.nikoleris@arm.com
144412724Snikos.nikoleris@arm.com    if (blk->isWritable()) {
144512724Snikos.nikoleris@arm.com        // not asserting shared means we pass the block in modified
144612724Snikos.nikoleris@arm.com        // state, mark our own block non-writeable
144712724Snikos.nikoleris@arm.com        blk->status &= ~BlkWritable;
144812724Snikos.nikoleris@arm.com    } else {
144912724Snikos.nikoleris@arm.com        // we are in the Owned state, tell the receiver
145012724Snikos.nikoleris@arm.com        pkt->setHasSharers();
145112724Snikos.nikoleris@arm.com    }
145212724Snikos.nikoleris@arm.com
145312724Snikos.nikoleris@arm.com    // make sure the block is not marked dirty
145412724Snikos.nikoleris@arm.com    blk->status &= ~BlkDirty;
145512724Snikos.nikoleris@arm.com
145612724Snikos.nikoleris@arm.com    pkt->allocate();
145712724Snikos.nikoleris@arm.com    pkt->setDataFromBlock(blk->data, blkSize);
145812724Snikos.nikoleris@arm.com
145912724Snikos.nikoleris@arm.com    return pkt;
146012724Snikos.nikoleris@arm.com}
146112724Snikos.nikoleris@arm.com
146212724Snikos.nikoleris@arm.com
146312724Snikos.nikoleris@arm.comvoid
146412724Snikos.nikoleris@arm.comBaseCache::memWriteback()
146512724Snikos.nikoleris@arm.com{
146612728Snikos.nikoleris@arm.com    tags->forEachBlk([this](CacheBlk &blk) { writebackVisitor(blk); });
146712724Snikos.nikoleris@arm.com}
146812724Snikos.nikoleris@arm.com
146912724Snikos.nikoleris@arm.comvoid
147012724Snikos.nikoleris@arm.comBaseCache::memInvalidate()
147112724Snikos.nikoleris@arm.com{
147212728Snikos.nikoleris@arm.com    tags->forEachBlk([this](CacheBlk &blk) { invalidateVisitor(blk); });
147312724Snikos.nikoleris@arm.com}
147412724Snikos.nikoleris@arm.com
147512724Snikos.nikoleris@arm.combool
147612724Snikos.nikoleris@arm.comBaseCache::isDirty() const
147712724Snikos.nikoleris@arm.com{
147812728Snikos.nikoleris@arm.com    return tags->anyBlk([](CacheBlk &blk) { return blk.isDirty(); });
147912724Snikos.nikoleris@arm.com}
148012724Snikos.nikoleris@arm.com
148113416Sjavier.bueno@metempsy.combool
148213416Sjavier.bueno@metempsy.comBaseCache::coalesce() const
148313416Sjavier.bueno@metempsy.com{
148413416Sjavier.bueno@metempsy.com    return writeAllocator && writeAllocator->coalesce();
148513416Sjavier.bueno@metempsy.com}
148613416Sjavier.bueno@metempsy.com
148712728Snikos.nikoleris@arm.comvoid
148812724Snikos.nikoleris@arm.comBaseCache::writebackVisitor(CacheBlk &blk)
148912724Snikos.nikoleris@arm.com{
149012724Snikos.nikoleris@arm.com    if (blk.isDirty()) {
149112724Snikos.nikoleris@arm.com        assert(blk.isValid());
149212724Snikos.nikoleris@arm.com
149312749Sgiacomo.travaglini@arm.com        RequestPtr request = std::make_shared<Request>(
149412749Sgiacomo.travaglini@arm.com            regenerateBlkAddr(&blk), blkSize, 0, Request::funcMasterId);
149512749Sgiacomo.travaglini@arm.com
149612749Sgiacomo.travaglini@arm.com        request->taskId(blk.task_id);
149712724Snikos.nikoleris@arm.com        if (blk.isSecure()) {
149812749Sgiacomo.travaglini@arm.com            request->setFlags(Request::SECURE);
149912724Snikos.nikoleris@arm.com        }
150012724Snikos.nikoleris@arm.com
150112749Sgiacomo.travaglini@arm.com        Packet packet(request, MemCmd::WriteReq);
150212724Snikos.nikoleris@arm.com        packet.dataStatic(blk.data);
150312724Snikos.nikoleris@arm.com
150412724Snikos.nikoleris@arm.com        memSidePort.sendFunctional(&packet);
150512724Snikos.nikoleris@arm.com
150612724Snikos.nikoleris@arm.com        blk.status &= ~BlkDirty;
150712724Snikos.nikoleris@arm.com    }
150812724Snikos.nikoleris@arm.com}
150912724Snikos.nikoleris@arm.com
151012728Snikos.nikoleris@arm.comvoid
151112724Snikos.nikoleris@arm.comBaseCache::invalidateVisitor(CacheBlk &blk)
151212724Snikos.nikoleris@arm.com{
151312724Snikos.nikoleris@arm.com    if (blk.isDirty())
151412724Snikos.nikoleris@arm.com        warn_once("Invalidating dirty cache lines. " \
151512724Snikos.nikoleris@arm.com                  "Expect things to break.\n");
151612724Snikos.nikoleris@arm.com
151712724Snikos.nikoleris@arm.com    if (blk.isValid()) {
151812724Snikos.nikoleris@arm.com        assert(!blk.isDirty());
151912724Snikos.nikoleris@arm.com        invalidateBlock(&blk);
152012724Snikos.nikoleris@arm.com    }
152112724Snikos.nikoleris@arm.com}
152212724Snikos.nikoleris@arm.com
152312724Snikos.nikoleris@arm.comTick
152412724Snikos.nikoleris@arm.comBaseCache::nextQueueReadyTime() const
152512724Snikos.nikoleris@arm.com{
152612724Snikos.nikoleris@arm.com    Tick nextReady = std::min(mshrQueue.nextReadyTime(),
152712724Snikos.nikoleris@arm.com                              writeBuffer.nextReadyTime());
152812724Snikos.nikoleris@arm.com
152912724Snikos.nikoleris@arm.com    // Don't signal prefetch ready time if no MSHRs available
153012724Snikos.nikoleris@arm.com    // Will signal once enoguh MSHRs are deallocated
153112724Snikos.nikoleris@arm.com    if (prefetcher && mshrQueue.canPrefetch()) {
153212724Snikos.nikoleris@arm.com        nextReady = std::min(nextReady,
153312724Snikos.nikoleris@arm.com                             prefetcher->nextPrefetchReadyTime());
153412724Snikos.nikoleris@arm.com    }
153512724Snikos.nikoleris@arm.com
153612724Snikos.nikoleris@arm.com    return nextReady;
153712724Snikos.nikoleris@arm.com}
153812724Snikos.nikoleris@arm.com
153912724Snikos.nikoleris@arm.com
154012724Snikos.nikoleris@arm.combool
154112724Snikos.nikoleris@arm.comBaseCache::sendMSHRQueuePacket(MSHR* mshr)
154212724Snikos.nikoleris@arm.com{
154312724Snikos.nikoleris@arm.com    assert(mshr);
154412724Snikos.nikoleris@arm.com
154512724Snikos.nikoleris@arm.com    // use request from 1st target
154612724Snikos.nikoleris@arm.com    PacketPtr tgt_pkt = mshr->getTarget()->pkt;
154712724Snikos.nikoleris@arm.com
154812724Snikos.nikoleris@arm.com    DPRINTF(Cache, "%s: MSHR %s\n", __func__, tgt_pkt->print());
154912724Snikos.nikoleris@arm.com
155013352Snikos.nikoleris@arm.com    // if the cache is in write coalescing mode or (additionally) in
155113352Snikos.nikoleris@arm.com    // no allocation mode, and we have a write packet with an MSHR
155213352Snikos.nikoleris@arm.com    // that is not a whole-line write (due to incompatible flags etc),
155313352Snikos.nikoleris@arm.com    // then reset the write mode
155413352Snikos.nikoleris@arm.com    if (writeAllocator && writeAllocator->coalesce() && tgt_pkt->isWrite()) {
155513352Snikos.nikoleris@arm.com        if (!mshr->isWholeLineWrite()) {
155613352Snikos.nikoleris@arm.com            // if we are currently write coalescing, hold on the
155713352Snikos.nikoleris@arm.com            // MSHR as many cycles extra as we need to completely
155813352Snikos.nikoleris@arm.com            // write a cache line
155913352Snikos.nikoleris@arm.com            if (writeAllocator->delay(mshr->blkAddr)) {
156013352Snikos.nikoleris@arm.com                Tick delay = blkSize / tgt_pkt->getSize() * clockPeriod();
156113352Snikos.nikoleris@arm.com                DPRINTF(CacheVerbose, "Delaying pkt %s %llu ticks to allow "
156213352Snikos.nikoleris@arm.com                        "for write coalescing\n", tgt_pkt->print(), delay);
156313352Snikos.nikoleris@arm.com                mshrQueue.delay(mshr, delay);
156413352Snikos.nikoleris@arm.com                return false;
156513352Snikos.nikoleris@arm.com            } else {
156613352Snikos.nikoleris@arm.com                writeAllocator->reset();
156713352Snikos.nikoleris@arm.com            }
156813352Snikos.nikoleris@arm.com        } else {
156913352Snikos.nikoleris@arm.com            writeAllocator->resetDelay(mshr->blkAddr);
157013352Snikos.nikoleris@arm.com        }
157113352Snikos.nikoleris@arm.com    }
157213352Snikos.nikoleris@arm.com
157312724Snikos.nikoleris@arm.com    CacheBlk *blk = tags->findBlock(mshr->blkAddr, mshr->isSecure);
157412724Snikos.nikoleris@arm.com
157512724Snikos.nikoleris@arm.com    // either a prefetch that is not present upstream, or a normal
157612724Snikos.nikoleris@arm.com    // MSHR request, proceed to get the packet to send downstream
157713350Snikos.nikoleris@arm.com    PacketPtr pkt = createMissPacket(tgt_pkt, blk, mshr->needsWritable(),
157813350Snikos.nikoleris@arm.com                                     mshr->isWholeLineWrite());
157912724Snikos.nikoleris@arm.com
158012724Snikos.nikoleris@arm.com    mshr->isForward = (pkt == nullptr);
158112724Snikos.nikoleris@arm.com
158212724Snikos.nikoleris@arm.com    if (mshr->isForward) {
158312724Snikos.nikoleris@arm.com        // not a cache block request, but a response is expected
158412724Snikos.nikoleris@arm.com        // make copy of current packet to forward, keep current
158512724Snikos.nikoleris@arm.com        // copy for response handling
158612724Snikos.nikoleris@arm.com        pkt = new Packet(tgt_pkt, false, true);
158712724Snikos.nikoleris@arm.com        assert(!pkt->isWrite());
158812724Snikos.nikoleris@arm.com    }
158912724Snikos.nikoleris@arm.com
159012724Snikos.nikoleris@arm.com    // play it safe and append (rather than set) the sender state,
159112724Snikos.nikoleris@arm.com    // as forwarded packets may already have existing state
159212724Snikos.nikoleris@arm.com    pkt->pushSenderState(mshr);
159312724Snikos.nikoleris@arm.com
159412724Snikos.nikoleris@arm.com    if (pkt->isClean() && blk && blk->isDirty()) {
159512724Snikos.nikoleris@arm.com        // A cache clean opearation is looking for a dirty block. Mark
159612724Snikos.nikoleris@arm.com        // the packet so that the destination xbar can determine that
159712724Snikos.nikoleris@arm.com        // there will be a follow-up write packet as well.
159812724Snikos.nikoleris@arm.com        pkt->setSatisfied();
159912724Snikos.nikoleris@arm.com    }
160012724Snikos.nikoleris@arm.com
160112724Snikos.nikoleris@arm.com    if (!memSidePort.sendTimingReq(pkt)) {
160212724Snikos.nikoleris@arm.com        // we are awaiting a retry, but we
160312724Snikos.nikoleris@arm.com        // delete the packet and will be creating a new packet
160412724Snikos.nikoleris@arm.com        // when we get the opportunity
160512724Snikos.nikoleris@arm.com        delete pkt;
160612724Snikos.nikoleris@arm.com
160712724Snikos.nikoleris@arm.com        // note that we have now masked any requestBus and
160812724Snikos.nikoleris@arm.com        // schedSendEvent (we will wait for a retry before
160912724Snikos.nikoleris@arm.com        // doing anything), and this is so even if we do not
161012724Snikos.nikoleris@arm.com        // care about this packet and might override it before
161112724Snikos.nikoleris@arm.com        // it gets retried
161212724Snikos.nikoleris@arm.com        return true;
161312724Snikos.nikoleris@arm.com    } else {
161412724Snikos.nikoleris@arm.com        // As part of the call to sendTimingReq the packet is
161512724Snikos.nikoleris@arm.com        // forwarded to all neighbouring caches (and any caches
161612724Snikos.nikoleris@arm.com        // above them) as a snoop. Thus at this point we know if
161712724Snikos.nikoleris@arm.com        // any of the neighbouring caches are responding, and if
161812724Snikos.nikoleris@arm.com        // so, we know it is dirty, and we can determine if it is
161912724Snikos.nikoleris@arm.com        // being passed as Modified, making our MSHR the ordering
162012724Snikos.nikoleris@arm.com        // point
162112724Snikos.nikoleris@arm.com        bool pending_modified_resp = !pkt->hasSharers() &&
162212724Snikos.nikoleris@arm.com            pkt->cacheResponding();
162312724Snikos.nikoleris@arm.com        markInService(mshr, pending_modified_resp);
162412724Snikos.nikoleris@arm.com
162512724Snikos.nikoleris@arm.com        if (pkt->isClean() && blk && blk->isDirty()) {
162612724Snikos.nikoleris@arm.com            // A cache clean opearation is looking for a dirty
162712724Snikos.nikoleris@arm.com            // block. If a dirty block is encountered a WriteClean
162812724Snikos.nikoleris@arm.com            // will update any copies to the path to the memory
162912724Snikos.nikoleris@arm.com            // until the point of reference.
163012724Snikos.nikoleris@arm.com            DPRINTF(CacheVerbose, "%s: packet %s found block: %s\n",
163112724Snikos.nikoleris@arm.com                    __func__, pkt->print(), blk->print());
163212724Snikos.nikoleris@arm.com            PacketPtr wb_pkt = writecleanBlk(blk, pkt->req->getDest(),
163312724Snikos.nikoleris@arm.com                                             pkt->id);
163412724Snikos.nikoleris@arm.com            PacketList writebacks;
163512724Snikos.nikoleris@arm.com            writebacks.push_back(wb_pkt);
163612724Snikos.nikoleris@arm.com            doWritebacks(writebacks, 0);
163712724Snikos.nikoleris@arm.com        }
163812724Snikos.nikoleris@arm.com
163912724Snikos.nikoleris@arm.com        return false;
164012724Snikos.nikoleris@arm.com    }
164112724Snikos.nikoleris@arm.com}
164212724Snikos.nikoleris@arm.com
164312724Snikos.nikoleris@arm.combool
164412724Snikos.nikoleris@arm.comBaseCache::sendWriteQueuePacket(WriteQueueEntry* wq_entry)
164512724Snikos.nikoleris@arm.com{
164612724Snikos.nikoleris@arm.com    assert(wq_entry);
164712724Snikos.nikoleris@arm.com
164812724Snikos.nikoleris@arm.com    // always a single target for write queue entries
164912724Snikos.nikoleris@arm.com    PacketPtr tgt_pkt = wq_entry->getTarget()->pkt;
165012724Snikos.nikoleris@arm.com
165112724Snikos.nikoleris@arm.com    DPRINTF(Cache, "%s: write %s\n", __func__, tgt_pkt->print());
165212724Snikos.nikoleris@arm.com
165312724Snikos.nikoleris@arm.com    // forward as is, both for evictions and uncacheable writes
165412724Snikos.nikoleris@arm.com    if (!memSidePort.sendTimingReq(tgt_pkt)) {
165512724Snikos.nikoleris@arm.com        // note that we have now masked any requestBus and
165612724Snikos.nikoleris@arm.com        // schedSendEvent (we will wait for a retry before
165712724Snikos.nikoleris@arm.com        // doing anything), and this is so even if we do not
165812724Snikos.nikoleris@arm.com        // care about this packet and might override it before
165912724Snikos.nikoleris@arm.com        // it gets retried
166012724Snikos.nikoleris@arm.com        return true;
166112724Snikos.nikoleris@arm.com    } else {
166212724Snikos.nikoleris@arm.com        markInService(wq_entry);
166312724Snikos.nikoleris@arm.com        return false;
166412724Snikos.nikoleris@arm.com    }
166512724Snikos.nikoleris@arm.com}
166612724Snikos.nikoleris@arm.com
166712724Snikos.nikoleris@arm.comvoid
166812724Snikos.nikoleris@arm.comBaseCache::serialize(CheckpointOut &cp) const
166912724Snikos.nikoleris@arm.com{
167012724Snikos.nikoleris@arm.com    bool dirty(isDirty());
167112724Snikos.nikoleris@arm.com
167212724Snikos.nikoleris@arm.com    if (dirty) {
167312724Snikos.nikoleris@arm.com        warn("*** The cache still contains dirty data. ***\n");
167412724Snikos.nikoleris@arm.com        warn("    Make sure to drain the system using the correct flags.\n");
167512724Snikos.nikoleris@arm.com        warn("    This checkpoint will not restore correctly " \
167612724Snikos.nikoleris@arm.com             "and dirty data in the cache will be lost!\n");
167712724Snikos.nikoleris@arm.com    }
167812724Snikos.nikoleris@arm.com
167912724Snikos.nikoleris@arm.com    // Since we don't checkpoint the data in the cache, any dirty data
168012724Snikos.nikoleris@arm.com    // will be lost when restoring from a checkpoint of a system that
168112724Snikos.nikoleris@arm.com    // wasn't drained properly. Flag the checkpoint as invalid if the
168212724Snikos.nikoleris@arm.com    // cache contains dirty data.
168312724Snikos.nikoleris@arm.com    bool bad_checkpoint(dirty);
168412724Snikos.nikoleris@arm.com    SERIALIZE_SCALAR(bad_checkpoint);
168512724Snikos.nikoleris@arm.com}
168612724Snikos.nikoleris@arm.com
168712724Snikos.nikoleris@arm.comvoid
168812724Snikos.nikoleris@arm.comBaseCache::unserialize(CheckpointIn &cp)
168912724Snikos.nikoleris@arm.com{
169012724Snikos.nikoleris@arm.com    bool bad_checkpoint;
169112724Snikos.nikoleris@arm.com    UNSERIALIZE_SCALAR(bad_checkpoint);
169212724Snikos.nikoleris@arm.com    if (bad_checkpoint) {
169312724Snikos.nikoleris@arm.com        fatal("Restoring from checkpoints with dirty caches is not "
169412724Snikos.nikoleris@arm.com              "supported in the classic memory system. Please remove any "
169512724Snikos.nikoleris@arm.com              "caches or drain them properly before taking checkpoints.\n");
169612724Snikos.nikoleris@arm.com    }
169712724Snikos.nikoleris@arm.com}
169812724Snikos.nikoleris@arm.com
169912724Snikos.nikoleris@arm.comvoid
17002810SN/ABaseCache::regStats()
17012810SN/A{
170211522Sstephan.diestelhorst@arm.com    MemObject::regStats();
170311522Sstephan.diestelhorst@arm.com
17042810SN/A    using namespace Stats;
17052810SN/A
17062810SN/A    // Hit statistics
17074022SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
17084022SN/A        MemCmd cmd(access_idx);
17094022SN/A        const string &cstr = cmd.toString();
17102810SN/A
17112810SN/A        hits[access_idx]
17128833Sdam.sunwoo@arm.com            .init(system->maxMasters())
17132810SN/A            .name(name() + "." + cstr + "_hits")
17142810SN/A            .desc("number of " + cstr + " hits")
17152810SN/A            .flags(total | nozero | nonan)
17162810SN/A            ;
17178833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
17188833Sdam.sunwoo@arm.com            hits[access_idx].subname(i, system->getMasterName(i));
17198833Sdam.sunwoo@arm.com        }
17202810SN/A    }
17212810SN/A
17224871SN/A// These macros make it easier to sum the right subset of commands and
17234871SN/A// to change the subset of commands that are considered "demand" vs
17244871SN/A// "non-demand"
17254871SN/A#define SUM_DEMAND(s) \
172611455Sandreas.hansson@arm.com    (s[MemCmd::ReadReq] + s[MemCmd::WriteReq] + s[MemCmd::WriteLineReq] + \
172710885Sandreas.hansson@arm.com     s[MemCmd::ReadExReq] + s[MemCmd::ReadCleanReq] + s[MemCmd::ReadSharedReq])
17284871SN/A
17294871SN/A// should writebacks be included here?  prior code was inconsistent...
17304871SN/A#define SUM_NON_DEMAND(s) \
173113367Syuetsu.kodama@riken.jp    (s[MemCmd::SoftPFReq] + s[MemCmd::HardPFReq] + s[MemCmd::SoftPFExReq])
17324871SN/A
17332810SN/A    demandHits
17342810SN/A        .name(name() + ".demand_hits")
17352810SN/A        .desc("number of demand (read+write) hits")
17368833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
17372810SN/A        ;
17384871SN/A    demandHits = SUM_DEMAND(hits);
17398833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
17408833Sdam.sunwoo@arm.com        demandHits.subname(i, system->getMasterName(i));
17418833Sdam.sunwoo@arm.com    }
17422810SN/A
17432810SN/A    overallHits
17442810SN/A        .name(name() + ".overall_hits")
17452810SN/A        .desc("number of overall hits")
17468833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
17472810SN/A        ;
17484871SN/A    overallHits = demandHits + SUM_NON_DEMAND(hits);
17498833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
17508833Sdam.sunwoo@arm.com        overallHits.subname(i, system->getMasterName(i));
17518833Sdam.sunwoo@arm.com    }
17522810SN/A
17532810SN/A    // Miss statistics
17544022SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
17554022SN/A        MemCmd cmd(access_idx);
17564022SN/A        const string &cstr = cmd.toString();
17572810SN/A
17582810SN/A        misses[access_idx]
17598833Sdam.sunwoo@arm.com            .init(system->maxMasters())
17602810SN/A            .name(name() + "." + cstr + "_misses")
17612810SN/A            .desc("number of " + cstr + " misses")
17622810SN/A            .flags(total | nozero | nonan)
17632810SN/A            ;
17648833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
17658833Sdam.sunwoo@arm.com            misses[access_idx].subname(i, system->getMasterName(i));
17668833Sdam.sunwoo@arm.com        }
17672810SN/A    }
17682810SN/A
17692810SN/A    demandMisses
17702810SN/A        .name(name() + ".demand_misses")
17712810SN/A        .desc("number of demand (read+write) misses")
17728833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
17732810SN/A        ;
17744871SN/A    demandMisses = SUM_DEMAND(misses);
17758833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
17768833Sdam.sunwoo@arm.com        demandMisses.subname(i, system->getMasterName(i));
17778833Sdam.sunwoo@arm.com    }
17782810SN/A
17792810SN/A    overallMisses
17802810SN/A        .name(name() + ".overall_misses")
17812810SN/A        .desc("number of overall misses")
17828833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
17832810SN/A        ;
17844871SN/A    overallMisses = demandMisses + SUM_NON_DEMAND(misses);
17858833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
17868833Sdam.sunwoo@arm.com        overallMisses.subname(i, system->getMasterName(i));
17878833Sdam.sunwoo@arm.com    }
17882810SN/A
17892810SN/A    // Miss latency statistics
17904022SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
17914022SN/A        MemCmd cmd(access_idx);
17924022SN/A        const string &cstr = cmd.toString();
17932810SN/A
17942810SN/A        missLatency[access_idx]
17958833Sdam.sunwoo@arm.com            .init(system->maxMasters())
17962810SN/A            .name(name() + "." + cstr + "_miss_latency")
17972810SN/A            .desc("number of " + cstr + " miss cycles")
17982810SN/A            .flags(total | nozero | nonan)
17992810SN/A            ;
18008833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
18018833Sdam.sunwoo@arm.com            missLatency[access_idx].subname(i, system->getMasterName(i));
18028833Sdam.sunwoo@arm.com        }
18032810SN/A    }
18042810SN/A
18052810SN/A    demandMissLatency
18062810SN/A        .name(name() + ".demand_miss_latency")
18072810SN/A        .desc("number of demand (read+write) miss cycles")
18088833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
18092810SN/A        ;
18104871SN/A    demandMissLatency = SUM_DEMAND(missLatency);
18118833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
18128833Sdam.sunwoo@arm.com        demandMissLatency.subname(i, system->getMasterName(i));
18138833Sdam.sunwoo@arm.com    }
18142810SN/A
18152810SN/A    overallMissLatency
18162810SN/A        .name(name() + ".overall_miss_latency")
18172810SN/A        .desc("number of overall miss cycles")
18188833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
18192810SN/A        ;
18204871SN/A    overallMissLatency = demandMissLatency + SUM_NON_DEMAND(missLatency);
18218833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
18228833Sdam.sunwoo@arm.com        overallMissLatency.subname(i, system->getMasterName(i));
18238833Sdam.sunwoo@arm.com    }
18242810SN/A
18252810SN/A    // access formulas
18264022SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
18274022SN/A        MemCmd cmd(access_idx);
18284022SN/A        const string &cstr = cmd.toString();
18292810SN/A
18302810SN/A        accesses[access_idx]
18312810SN/A            .name(name() + "." + cstr + "_accesses")
18322810SN/A            .desc("number of " + cstr + " accesses(hits+misses)")
18332810SN/A            .flags(total | nozero | nonan)
18342810SN/A            ;
18358833Sdam.sunwoo@arm.com        accesses[access_idx] = hits[access_idx] + misses[access_idx];
18362810SN/A
18378833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
18388833Sdam.sunwoo@arm.com            accesses[access_idx].subname(i, system->getMasterName(i));
18398833Sdam.sunwoo@arm.com        }
18402810SN/A    }
18412810SN/A
18422810SN/A    demandAccesses
18432810SN/A        .name(name() + ".demand_accesses")
18442810SN/A        .desc("number of demand (read+write) accesses")
18458833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
18462810SN/A        ;
18472810SN/A    demandAccesses = demandHits + demandMisses;
18488833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
18498833Sdam.sunwoo@arm.com        demandAccesses.subname(i, system->getMasterName(i));
18508833Sdam.sunwoo@arm.com    }
18512810SN/A
18522810SN/A    overallAccesses
18532810SN/A        .name(name() + ".overall_accesses")
18542810SN/A        .desc("number of overall (read+write) accesses")
18558833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
18562810SN/A        ;
18572810SN/A    overallAccesses = overallHits + overallMisses;
18588833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
18598833Sdam.sunwoo@arm.com        overallAccesses.subname(i, system->getMasterName(i));
18608833Sdam.sunwoo@arm.com    }
18612810SN/A
18622810SN/A    // miss rate formulas
18634022SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
18644022SN/A        MemCmd cmd(access_idx);
18654022SN/A        const string &cstr = cmd.toString();
18662810SN/A
18672810SN/A        missRate[access_idx]
18682810SN/A            .name(name() + "." + cstr + "_miss_rate")
18692810SN/A            .desc("miss rate for " + cstr + " accesses")
18702810SN/A            .flags(total | nozero | nonan)
18712810SN/A            ;
18728833Sdam.sunwoo@arm.com        missRate[access_idx] = misses[access_idx] / accesses[access_idx];
18732810SN/A
18748833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
18758833Sdam.sunwoo@arm.com            missRate[access_idx].subname(i, system->getMasterName(i));
18768833Sdam.sunwoo@arm.com        }
18772810SN/A    }
18782810SN/A
18792810SN/A    demandMissRate
18802810SN/A        .name(name() + ".demand_miss_rate")
18812810SN/A        .desc("miss rate for demand accesses")
18828833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
18832810SN/A        ;
18842810SN/A    demandMissRate = demandMisses / demandAccesses;
18858833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
18868833Sdam.sunwoo@arm.com        demandMissRate.subname(i, system->getMasterName(i));
18878833Sdam.sunwoo@arm.com    }
18882810SN/A
18892810SN/A    overallMissRate
18902810SN/A        .name(name() + ".overall_miss_rate")
18912810SN/A        .desc("miss rate for overall accesses")
18928833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
18932810SN/A        ;
18942810SN/A    overallMissRate = overallMisses / overallAccesses;
18958833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
18968833Sdam.sunwoo@arm.com        overallMissRate.subname(i, system->getMasterName(i));
18978833Sdam.sunwoo@arm.com    }
18982810SN/A
18992810SN/A    // miss latency formulas
19004022SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
19014022SN/A        MemCmd cmd(access_idx);
19024022SN/A        const string &cstr = cmd.toString();
19032810SN/A
19042810SN/A        avgMissLatency[access_idx]
19052810SN/A            .name(name() + "." + cstr + "_avg_miss_latency")
19062810SN/A            .desc("average " + cstr + " miss latency")
19072810SN/A            .flags(total | nozero | nonan)
19082810SN/A            ;
19092810SN/A        avgMissLatency[access_idx] =
19102810SN/A            missLatency[access_idx] / misses[access_idx];
19118833Sdam.sunwoo@arm.com
19128833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
19138833Sdam.sunwoo@arm.com            avgMissLatency[access_idx].subname(i, system->getMasterName(i));
19148833Sdam.sunwoo@arm.com        }
19152810SN/A    }
19162810SN/A
19172810SN/A    demandAvgMissLatency
19182810SN/A        .name(name() + ".demand_avg_miss_latency")
19192810SN/A        .desc("average overall miss latency")
19208833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
19212810SN/A        ;
19222810SN/A    demandAvgMissLatency = demandMissLatency / demandMisses;
19238833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
19248833Sdam.sunwoo@arm.com        demandAvgMissLatency.subname(i, system->getMasterName(i));
19258833Sdam.sunwoo@arm.com    }
19262810SN/A
19272810SN/A    overallAvgMissLatency
19282810SN/A        .name(name() + ".overall_avg_miss_latency")
19292810SN/A        .desc("average overall miss latency")
19308833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
19312810SN/A        ;
19322810SN/A    overallAvgMissLatency = overallMissLatency / overallMisses;
19338833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
19348833Sdam.sunwoo@arm.com        overallAvgMissLatency.subname(i, system->getMasterName(i));
19358833Sdam.sunwoo@arm.com    }
19362810SN/A
19372810SN/A    blocked_cycles.init(NUM_BLOCKED_CAUSES);
19382810SN/A    blocked_cycles
19392810SN/A        .name(name() + ".blocked_cycles")
19402810SN/A        .desc("number of cycles access was blocked")
19412810SN/A        .subname(Blocked_NoMSHRs, "no_mshrs")
19422810SN/A        .subname(Blocked_NoTargets, "no_targets")
19432810SN/A        ;
19442810SN/A
19452810SN/A
19462810SN/A    blocked_causes.init(NUM_BLOCKED_CAUSES);
19472810SN/A    blocked_causes
19482810SN/A        .name(name() + ".blocked")
19492810SN/A        .desc("number of cycles access was blocked")
19502810SN/A        .subname(Blocked_NoMSHRs, "no_mshrs")
19512810SN/A        .subname(Blocked_NoTargets, "no_targets")
19522810SN/A        ;
19532810SN/A
19542810SN/A    avg_blocked
19552810SN/A        .name(name() + ".avg_blocked_cycles")
19562810SN/A        .desc("average number of cycles each access was blocked")
19572810SN/A        .subname(Blocked_NoMSHRs, "no_mshrs")
19582810SN/A        .subname(Blocked_NoTargets, "no_targets")
19592810SN/A        ;
19602810SN/A
19612810SN/A    avg_blocked = blocked_cycles / blocked_causes;
19622810SN/A
196311436SRekai.GonzalezAlberquilla@arm.com    unusedPrefetches
196411436SRekai.GonzalezAlberquilla@arm.com        .name(name() + ".unused_prefetches")
196511436SRekai.GonzalezAlberquilla@arm.com        .desc("number of HardPF blocks evicted w/o reference")
196611436SRekai.GonzalezAlberquilla@arm.com        .flags(nozero)
196711436SRekai.GonzalezAlberquilla@arm.com        ;
196811436SRekai.GonzalezAlberquilla@arm.com
19694626SN/A    writebacks
19708833Sdam.sunwoo@arm.com        .init(system->maxMasters())
19714626SN/A        .name(name() + ".writebacks")
19724626SN/A        .desc("number of writebacks")
19738833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
19744626SN/A        ;
19758833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
19768833Sdam.sunwoo@arm.com        writebacks.subname(i, system->getMasterName(i));
19778833Sdam.sunwoo@arm.com    }
19784626SN/A
19794626SN/A    // MSHR statistics
19804626SN/A    // MSHR hit statistics
19814626SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
19824626SN/A        MemCmd cmd(access_idx);
19834626SN/A        const string &cstr = cmd.toString();
19844626SN/A
19854626SN/A        mshr_hits[access_idx]
19868833Sdam.sunwoo@arm.com            .init(system->maxMasters())
19874626SN/A            .name(name() + "." + cstr + "_mshr_hits")
19884626SN/A            .desc("number of " + cstr + " MSHR hits")
19894626SN/A            .flags(total | nozero | nonan)
19904626SN/A            ;
19918833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
19928833Sdam.sunwoo@arm.com            mshr_hits[access_idx].subname(i, system->getMasterName(i));
19938833Sdam.sunwoo@arm.com        }
19944626SN/A    }
19954626SN/A
19964626SN/A    demandMshrHits
19974626SN/A        .name(name() + ".demand_mshr_hits")
19984626SN/A        .desc("number of demand (read+write) MSHR hits")
19998833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
20004626SN/A        ;
20014871SN/A    demandMshrHits = SUM_DEMAND(mshr_hits);
20028833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
20038833Sdam.sunwoo@arm.com        demandMshrHits.subname(i, system->getMasterName(i));
20048833Sdam.sunwoo@arm.com    }
20054626SN/A
20064626SN/A    overallMshrHits
20074626SN/A        .name(name() + ".overall_mshr_hits")
20084626SN/A        .desc("number of overall MSHR hits")
20098833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
20104626SN/A        ;
20114871SN/A    overallMshrHits = demandMshrHits + SUM_NON_DEMAND(mshr_hits);
20128833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
20138833Sdam.sunwoo@arm.com        overallMshrHits.subname(i, system->getMasterName(i));
20148833Sdam.sunwoo@arm.com    }
20154626SN/A
20164626SN/A    // MSHR miss statistics
20174626SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
20184626SN/A        MemCmd cmd(access_idx);
20194626SN/A        const string &cstr = cmd.toString();
20204626SN/A
20214626SN/A        mshr_misses[access_idx]
20228833Sdam.sunwoo@arm.com            .init(system->maxMasters())
20234626SN/A            .name(name() + "." + cstr + "_mshr_misses")
20244626SN/A            .desc("number of " + cstr + " MSHR misses")
20254626SN/A            .flags(total | nozero | nonan)
20264626SN/A            ;
20278833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
20288833Sdam.sunwoo@arm.com            mshr_misses[access_idx].subname(i, system->getMasterName(i));
20298833Sdam.sunwoo@arm.com        }
20304626SN/A    }
20314626SN/A
20324626SN/A    demandMshrMisses
20334626SN/A        .name(name() + ".demand_mshr_misses")
20344626SN/A        .desc("number of demand (read+write) MSHR misses")
20358833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
20364626SN/A        ;
20374871SN/A    demandMshrMisses = SUM_DEMAND(mshr_misses);
20388833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
20398833Sdam.sunwoo@arm.com        demandMshrMisses.subname(i, system->getMasterName(i));
20408833Sdam.sunwoo@arm.com    }
20414626SN/A
20424626SN/A    overallMshrMisses
20434626SN/A        .name(name() + ".overall_mshr_misses")
20444626SN/A        .desc("number of overall MSHR misses")
20458833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
20464626SN/A        ;
20474871SN/A    overallMshrMisses = demandMshrMisses + SUM_NON_DEMAND(mshr_misses);
20488833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
20498833Sdam.sunwoo@arm.com        overallMshrMisses.subname(i, system->getMasterName(i));
20508833Sdam.sunwoo@arm.com    }
20514626SN/A
20524626SN/A    // MSHR miss latency statistics
20534626SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
20544626SN/A        MemCmd cmd(access_idx);
20554626SN/A        const string &cstr = cmd.toString();
20564626SN/A
20574626SN/A        mshr_miss_latency[access_idx]
20588833Sdam.sunwoo@arm.com            .init(system->maxMasters())
20594626SN/A            .name(name() + "." + cstr + "_mshr_miss_latency")
20604626SN/A            .desc("number of " + cstr + " MSHR miss cycles")
20614626SN/A            .flags(total | nozero | nonan)
20624626SN/A            ;
20638833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
20648833Sdam.sunwoo@arm.com            mshr_miss_latency[access_idx].subname(i, system->getMasterName(i));
20658833Sdam.sunwoo@arm.com        }
20664626SN/A    }
20674626SN/A
20684626SN/A    demandMshrMissLatency
20694626SN/A        .name(name() + ".demand_mshr_miss_latency")
20704626SN/A        .desc("number of demand (read+write) MSHR miss cycles")
20718833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
20724626SN/A        ;
20734871SN/A    demandMshrMissLatency = SUM_DEMAND(mshr_miss_latency);
20748833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
20758833Sdam.sunwoo@arm.com        demandMshrMissLatency.subname(i, system->getMasterName(i));
20768833Sdam.sunwoo@arm.com    }
20774626SN/A
20784626SN/A    overallMshrMissLatency
20794626SN/A        .name(name() + ".overall_mshr_miss_latency")
20804626SN/A        .desc("number of overall MSHR miss cycles")
20818833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
20824626SN/A        ;
20834871SN/A    overallMshrMissLatency =
20844871SN/A        demandMshrMissLatency + SUM_NON_DEMAND(mshr_miss_latency);
20858833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
20868833Sdam.sunwoo@arm.com        overallMshrMissLatency.subname(i, system->getMasterName(i));
20878833Sdam.sunwoo@arm.com    }
20884626SN/A
20894626SN/A    // MSHR uncacheable statistics
20904626SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
20914626SN/A        MemCmd cmd(access_idx);
20924626SN/A        const string &cstr = cmd.toString();
20934626SN/A
20944626SN/A        mshr_uncacheable[access_idx]
20958833Sdam.sunwoo@arm.com            .init(system->maxMasters())
20964626SN/A            .name(name() + "." + cstr + "_mshr_uncacheable")
20974626SN/A            .desc("number of " + cstr + " MSHR uncacheable")
20984626SN/A            .flags(total | nozero | nonan)
20994626SN/A            ;
21008833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
21018833Sdam.sunwoo@arm.com            mshr_uncacheable[access_idx].subname(i, system->getMasterName(i));
21028833Sdam.sunwoo@arm.com        }
21034626SN/A    }
21044626SN/A
21054626SN/A    overallMshrUncacheable
21064626SN/A        .name(name() + ".overall_mshr_uncacheable_misses")
21074626SN/A        .desc("number of overall MSHR uncacheable misses")
21088833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
21094626SN/A        ;
21104871SN/A    overallMshrUncacheable =
21114871SN/A        SUM_DEMAND(mshr_uncacheable) + SUM_NON_DEMAND(mshr_uncacheable);
21128833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
21138833Sdam.sunwoo@arm.com        overallMshrUncacheable.subname(i, system->getMasterName(i));
21148833Sdam.sunwoo@arm.com    }
21154626SN/A
21164626SN/A    // MSHR miss latency statistics
21174626SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
21184626SN/A        MemCmd cmd(access_idx);
21194626SN/A        const string &cstr = cmd.toString();
21204626SN/A
21214626SN/A        mshr_uncacheable_lat[access_idx]
21228833Sdam.sunwoo@arm.com            .init(system->maxMasters())
21234626SN/A            .name(name() + "." + cstr + "_mshr_uncacheable_latency")
21244626SN/A            .desc("number of " + cstr + " MSHR uncacheable cycles")
21254626SN/A            .flags(total | nozero | nonan)
21264626SN/A            ;
21278833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
212811483Snikos.nikoleris@arm.com            mshr_uncacheable_lat[access_idx].subname(
212911483Snikos.nikoleris@arm.com                i, system->getMasterName(i));
21308833Sdam.sunwoo@arm.com        }
21314626SN/A    }
21324626SN/A
21334626SN/A    overallMshrUncacheableLatency
21344626SN/A        .name(name() + ".overall_mshr_uncacheable_latency")
21354626SN/A        .desc("number of overall MSHR uncacheable cycles")
21368833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
21374626SN/A        ;
21384871SN/A    overallMshrUncacheableLatency =
21394871SN/A        SUM_DEMAND(mshr_uncacheable_lat) +
21404871SN/A        SUM_NON_DEMAND(mshr_uncacheable_lat);
21418833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
21428833Sdam.sunwoo@arm.com        overallMshrUncacheableLatency.subname(i, system->getMasterName(i));
21438833Sdam.sunwoo@arm.com    }
21444626SN/A
21454626SN/A#if 0
21464626SN/A    // MSHR access formulas
21474626SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
21484626SN/A        MemCmd cmd(access_idx);
21494626SN/A        const string &cstr = cmd.toString();
21504626SN/A
21514626SN/A        mshrAccesses[access_idx]
21524626SN/A            .name(name() + "." + cstr + "_mshr_accesses")
21534626SN/A            .desc("number of " + cstr + " mshr accesses(hits+misses)")
21544626SN/A            .flags(total | nozero | nonan)
21554626SN/A            ;
21564626SN/A        mshrAccesses[access_idx] =
21574626SN/A            mshr_hits[access_idx] + mshr_misses[access_idx]
21584626SN/A            + mshr_uncacheable[access_idx];
21594626SN/A    }
21604626SN/A
21614626SN/A    demandMshrAccesses
21624626SN/A        .name(name() + ".demand_mshr_accesses")
21634626SN/A        .desc("number of demand (read+write) mshr accesses")
21644626SN/A        .flags(total | nozero | nonan)
21654626SN/A        ;
21664626SN/A    demandMshrAccesses = demandMshrHits + demandMshrMisses;
21674626SN/A
21684626SN/A    overallMshrAccesses
21694626SN/A        .name(name() + ".overall_mshr_accesses")
21704626SN/A        .desc("number of overall (read+write) mshr accesses")
21714626SN/A        .flags(total | nozero | nonan)
21724626SN/A        ;
21734626SN/A    overallMshrAccesses = overallMshrHits + overallMshrMisses
21744626SN/A        + overallMshrUncacheable;
21754626SN/A#endif
21764626SN/A
21774626SN/A    // MSHR miss rate formulas
21784626SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
21794626SN/A        MemCmd cmd(access_idx);
21804626SN/A        const string &cstr = cmd.toString();
21814626SN/A
21824626SN/A        mshrMissRate[access_idx]
21834626SN/A            .name(name() + "." + cstr + "_mshr_miss_rate")
21844626SN/A            .desc("mshr miss rate for " + cstr + " accesses")
21854626SN/A            .flags(total | nozero | nonan)
21864626SN/A            ;
21874626SN/A        mshrMissRate[access_idx] =
21884626SN/A            mshr_misses[access_idx] / accesses[access_idx];
21898833Sdam.sunwoo@arm.com
21908833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
21918833Sdam.sunwoo@arm.com            mshrMissRate[access_idx].subname(i, system->getMasterName(i));
21928833Sdam.sunwoo@arm.com        }
21934626SN/A    }
21944626SN/A
21954626SN/A    demandMshrMissRate
21964626SN/A        .name(name() + ".demand_mshr_miss_rate")
21974626SN/A        .desc("mshr miss rate for demand accesses")
21988833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
21994626SN/A        ;
22004626SN/A    demandMshrMissRate = demandMshrMisses / demandAccesses;
22018833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
22028833Sdam.sunwoo@arm.com        demandMshrMissRate.subname(i, system->getMasterName(i));
22038833Sdam.sunwoo@arm.com    }
22044626SN/A
22054626SN/A    overallMshrMissRate
22064626SN/A        .name(name() + ".overall_mshr_miss_rate")
22074626SN/A        .desc("mshr miss rate for overall accesses")
22088833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
22094626SN/A        ;
22104626SN/A    overallMshrMissRate = overallMshrMisses / overallAccesses;
22118833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
22128833Sdam.sunwoo@arm.com        overallMshrMissRate.subname(i, system->getMasterName(i));
22138833Sdam.sunwoo@arm.com    }
22144626SN/A
22154626SN/A    // mshrMiss latency formulas
22164626SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
22174626SN/A        MemCmd cmd(access_idx);
22184626SN/A        const string &cstr = cmd.toString();
22194626SN/A
22204626SN/A        avgMshrMissLatency[access_idx]
22214626SN/A            .name(name() + "." + cstr + "_avg_mshr_miss_latency")
22224626SN/A            .desc("average " + cstr + " mshr miss latency")
22234626SN/A            .flags(total | nozero | nonan)
22244626SN/A            ;
22254626SN/A        avgMshrMissLatency[access_idx] =
22264626SN/A            mshr_miss_latency[access_idx] / mshr_misses[access_idx];
22278833Sdam.sunwoo@arm.com
22288833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
222911483Snikos.nikoleris@arm.com            avgMshrMissLatency[access_idx].subname(
223011483Snikos.nikoleris@arm.com                i, system->getMasterName(i));
22318833Sdam.sunwoo@arm.com        }
22324626SN/A    }
22334626SN/A
22344626SN/A    demandAvgMshrMissLatency
22354626SN/A        .name(name() + ".demand_avg_mshr_miss_latency")
22364626SN/A        .desc("average overall mshr miss latency")
22378833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
22384626SN/A        ;
22394626SN/A    demandAvgMshrMissLatency = demandMshrMissLatency / demandMshrMisses;
22408833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
22418833Sdam.sunwoo@arm.com        demandAvgMshrMissLatency.subname(i, system->getMasterName(i));
22428833Sdam.sunwoo@arm.com    }
22434626SN/A
22444626SN/A    overallAvgMshrMissLatency
22454626SN/A        .name(name() + ".overall_avg_mshr_miss_latency")
22464626SN/A        .desc("average overall mshr miss latency")
22478833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
22484626SN/A        ;
22494626SN/A    overallAvgMshrMissLatency = overallMshrMissLatency / overallMshrMisses;
22508833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
22518833Sdam.sunwoo@arm.com        overallAvgMshrMissLatency.subname(i, system->getMasterName(i));
22528833Sdam.sunwoo@arm.com    }
22534626SN/A
22544626SN/A    // mshrUncacheable latency formulas
22554626SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
22564626SN/A        MemCmd cmd(access_idx);
22574626SN/A        const string &cstr = cmd.toString();
22584626SN/A
22594626SN/A        avgMshrUncacheableLatency[access_idx]
22604626SN/A            .name(name() + "." + cstr + "_avg_mshr_uncacheable_latency")
22614626SN/A            .desc("average " + cstr + " mshr uncacheable latency")
22624626SN/A            .flags(total | nozero | nonan)
22634626SN/A            ;
22644626SN/A        avgMshrUncacheableLatency[access_idx] =
22654626SN/A            mshr_uncacheable_lat[access_idx] / mshr_uncacheable[access_idx];
22668833Sdam.sunwoo@arm.com
22678833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
226811483Snikos.nikoleris@arm.com            avgMshrUncacheableLatency[access_idx].subname(
226911483Snikos.nikoleris@arm.com                i, system->getMasterName(i));
22708833Sdam.sunwoo@arm.com        }
22714626SN/A    }
22724626SN/A
22734626SN/A    overallAvgMshrUncacheableLatency
22744626SN/A        .name(name() + ".overall_avg_mshr_uncacheable_latency")
22754626SN/A        .desc("average overall mshr uncacheable latency")
22768833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
22774626SN/A        ;
227811483Snikos.nikoleris@arm.com    overallAvgMshrUncacheableLatency =
227911483Snikos.nikoleris@arm.com        overallMshrUncacheableLatency / overallMshrUncacheable;
22808833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
22818833Sdam.sunwoo@arm.com        overallAvgMshrUncacheableLatency.subname(i, system->getMasterName(i));
22828833Sdam.sunwoo@arm.com    }
22834626SN/A
228412702Snikos.nikoleris@arm.com    replacements
228512702Snikos.nikoleris@arm.com        .name(name() + ".replacements")
228612702Snikos.nikoleris@arm.com        .desc("number of replacements")
228712702Snikos.nikoleris@arm.com        ;
22882810SN/A}
228912724Snikos.nikoleris@arm.com
229013416Sjavier.bueno@metempsy.comvoid
229113416Sjavier.bueno@metempsy.comBaseCache::regProbePoints()
229213416Sjavier.bueno@metempsy.com{
229313416Sjavier.bueno@metempsy.com    ppHit = new ProbePointArg<PacketPtr>(this->getProbeManager(), "Hit");
229413416Sjavier.bueno@metempsy.com    ppMiss = new ProbePointArg<PacketPtr>(this->getProbeManager(), "Miss");
229513717Sivan.pizarro@metempsy.com    ppFill = new ProbePointArg<PacketPtr>(this->getProbeManager(), "Fill");
229613416Sjavier.bueno@metempsy.com}
229713416Sjavier.bueno@metempsy.com
229812724Snikos.nikoleris@arm.com///////////////
229912724Snikos.nikoleris@arm.com//
230012724Snikos.nikoleris@arm.com// CpuSidePort
230112724Snikos.nikoleris@arm.com//
230212724Snikos.nikoleris@arm.com///////////////
230312724Snikos.nikoleris@arm.combool
230412724Snikos.nikoleris@arm.comBaseCache::CpuSidePort::recvTimingSnoopResp(PacketPtr pkt)
230512724Snikos.nikoleris@arm.com{
230612725Snikos.nikoleris@arm.com    // Snoops shouldn't happen when bypassing caches
230712725Snikos.nikoleris@arm.com    assert(!cache->system->bypassCaches());
230812725Snikos.nikoleris@arm.com
230912725Snikos.nikoleris@arm.com    assert(pkt->isResponse());
231012725Snikos.nikoleris@arm.com
231112724Snikos.nikoleris@arm.com    // Express snoop responses from master to slave, e.g., from L1 to L2
231212724Snikos.nikoleris@arm.com    cache->recvTimingSnoopResp(pkt);
231312724Snikos.nikoleris@arm.com    return true;
231412724Snikos.nikoleris@arm.com}
231512724Snikos.nikoleris@arm.com
231612724Snikos.nikoleris@arm.com
231712724Snikos.nikoleris@arm.combool
231812724Snikos.nikoleris@arm.comBaseCache::CpuSidePort::tryTiming(PacketPtr pkt)
231912724Snikos.nikoleris@arm.com{
232012725Snikos.nikoleris@arm.com    if (cache->system->bypassCaches() || pkt->isExpressSnoop()) {
232112724Snikos.nikoleris@arm.com        // always let express snoop packets through even if blocked
232212724Snikos.nikoleris@arm.com        return true;
232312724Snikos.nikoleris@arm.com    } else if (blocked || mustSendRetry) {
232412724Snikos.nikoleris@arm.com        // either already committed to send a retry, or blocked
232512724Snikos.nikoleris@arm.com        mustSendRetry = true;
232612724Snikos.nikoleris@arm.com        return false;
232712724Snikos.nikoleris@arm.com    }
232812724Snikos.nikoleris@arm.com    mustSendRetry = false;
232912724Snikos.nikoleris@arm.com    return true;
233012724Snikos.nikoleris@arm.com}
233112724Snikos.nikoleris@arm.com
233212724Snikos.nikoleris@arm.combool
233312724Snikos.nikoleris@arm.comBaseCache::CpuSidePort::recvTimingReq(PacketPtr pkt)
233412724Snikos.nikoleris@arm.com{
233512725Snikos.nikoleris@arm.com    assert(pkt->isRequest());
233612725Snikos.nikoleris@arm.com
233712725Snikos.nikoleris@arm.com    if (cache->system->bypassCaches()) {
233812725Snikos.nikoleris@arm.com        // Just forward the packet if caches are disabled.
233912725Snikos.nikoleris@arm.com        // @todo This should really enqueue the packet rather
234012725Snikos.nikoleris@arm.com        bool M5_VAR_USED success = cache->memSidePort.sendTimingReq(pkt);
234112725Snikos.nikoleris@arm.com        assert(success);
234212725Snikos.nikoleris@arm.com        return true;
234312725Snikos.nikoleris@arm.com    } else if (tryTiming(pkt)) {
234412724Snikos.nikoleris@arm.com        cache->recvTimingReq(pkt);
234512724Snikos.nikoleris@arm.com        return true;
234612724Snikos.nikoleris@arm.com    }
234712724Snikos.nikoleris@arm.com    return false;
234812724Snikos.nikoleris@arm.com}
234912724Snikos.nikoleris@arm.com
235012724Snikos.nikoleris@arm.comTick
235112724Snikos.nikoleris@arm.comBaseCache::CpuSidePort::recvAtomic(PacketPtr pkt)
235212724Snikos.nikoleris@arm.com{
235312725Snikos.nikoleris@arm.com    if (cache->system->bypassCaches()) {
235412725Snikos.nikoleris@arm.com        // Forward the request if the system is in cache bypass mode.
235512725Snikos.nikoleris@arm.com        return cache->memSidePort.sendAtomic(pkt);
235612725Snikos.nikoleris@arm.com    } else {
235712725Snikos.nikoleris@arm.com        return cache->recvAtomic(pkt);
235812725Snikos.nikoleris@arm.com    }
235912724Snikos.nikoleris@arm.com}
236012724Snikos.nikoleris@arm.com
236112724Snikos.nikoleris@arm.comvoid
236212724Snikos.nikoleris@arm.comBaseCache::CpuSidePort::recvFunctional(PacketPtr pkt)
236312724Snikos.nikoleris@arm.com{
236412725Snikos.nikoleris@arm.com    if (cache->system->bypassCaches()) {
236512725Snikos.nikoleris@arm.com        // The cache should be flushed if we are in cache bypass mode,
236612725Snikos.nikoleris@arm.com        // so we don't need to check if we need to update anything.
236712725Snikos.nikoleris@arm.com        cache->memSidePort.sendFunctional(pkt);
236812725Snikos.nikoleris@arm.com        return;
236912725Snikos.nikoleris@arm.com    }
237012725Snikos.nikoleris@arm.com
237112724Snikos.nikoleris@arm.com    // functional request
237212724Snikos.nikoleris@arm.com    cache->functionalAccess(pkt, true);
237312724Snikos.nikoleris@arm.com}
237412724Snikos.nikoleris@arm.com
237512724Snikos.nikoleris@arm.comAddrRangeList
237612724Snikos.nikoleris@arm.comBaseCache::CpuSidePort::getAddrRanges() const
237712724Snikos.nikoleris@arm.com{
237812724Snikos.nikoleris@arm.com    return cache->getAddrRanges();
237912724Snikos.nikoleris@arm.com}
238012724Snikos.nikoleris@arm.com
238112724Snikos.nikoleris@arm.com
238212724Snikos.nikoleris@arm.comBaseCache::
238312724Snikos.nikoleris@arm.comCpuSidePort::CpuSidePort(const std::string &_name, BaseCache *_cache,
238412724Snikos.nikoleris@arm.com                         const std::string &_label)
238512724Snikos.nikoleris@arm.com    : CacheSlavePort(_name, _cache, _label), cache(_cache)
238612724Snikos.nikoleris@arm.com{
238712724Snikos.nikoleris@arm.com}
238812724Snikos.nikoleris@arm.com
238912724Snikos.nikoleris@arm.com///////////////
239012724Snikos.nikoleris@arm.com//
239112724Snikos.nikoleris@arm.com// MemSidePort
239212724Snikos.nikoleris@arm.com//
239312724Snikos.nikoleris@arm.com///////////////
239412724Snikos.nikoleris@arm.combool
239512724Snikos.nikoleris@arm.comBaseCache::MemSidePort::recvTimingResp(PacketPtr pkt)
239612724Snikos.nikoleris@arm.com{
239712724Snikos.nikoleris@arm.com    cache->recvTimingResp(pkt);
239812724Snikos.nikoleris@arm.com    return true;
239912724Snikos.nikoleris@arm.com}
240012724Snikos.nikoleris@arm.com
240112724Snikos.nikoleris@arm.com// Express snooping requests to memside port
240212724Snikos.nikoleris@arm.comvoid
240312724Snikos.nikoleris@arm.comBaseCache::MemSidePort::recvTimingSnoopReq(PacketPtr pkt)
240412724Snikos.nikoleris@arm.com{
240512725Snikos.nikoleris@arm.com    // Snoops shouldn't happen when bypassing caches
240612725Snikos.nikoleris@arm.com    assert(!cache->system->bypassCaches());
240712725Snikos.nikoleris@arm.com
240812724Snikos.nikoleris@arm.com    // handle snooping requests
240912724Snikos.nikoleris@arm.com    cache->recvTimingSnoopReq(pkt);
241012724Snikos.nikoleris@arm.com}
241112724Snikos.nikoleris@arm.com
241212724Snikos.nikoleris@arm.comTick
241312724Snikos.nikoleris@arm.comBaseCache::MemSidePort::recvAtomicSnoop(PacketPtr pkt)
241412724Snikos.nikoleris@arm.com{
241512725Snikos.nikoleris@arm.com    // Snoops shouldn't happen when bypassing caches
241612725Snikos.nikoleris@arm.com    assert(!cache->system->bypassCaches());
241712725Snikos.nikoleris@arm.com
241812724Snikos.nikoleris@arm.com    return cache->recvAtomicSnoop(pkt);
241912724Snikos.nikoleris@arm.com}
242012724Snikos.nikoleris@arm.com
242112724Snikos.nikoleris@arm.comvoid
242212724Snikos.nikoleris@arm.comBaseCache::MemSidePort::recvFunctionalSnoop(PacketPtr pkt)
242312724Snikos.nikoleris@arm.com{
242412725Snikos.nikoleris@arm.com    // Snoops shouldn't happen when bypassing caches
242512725Snikos.nikoleris@arm.com    assert(!cache->system->bypassCaches());
242612725Snikos.nikoleris@arm.com
242712724Snikos.nikoleris@arm.com    // functional snoop (note that in contrast to atomic we don't have
242812724Snikos.nikoleris@arm.com    // a specific functionalSnoop method, as they have the same
242912724Snikos.nikoleris@arm.com    // behaviour regardless)
243012724Snikos.nikoleris@arm.com    cache->functionalAccess(pkt, false);
243112724Snikos.nikoleris@arm.com}
243212724Snikos.nikoleris@arm.com
243312724Snikos.nikoleris@arm.comvoid
243412724Snikos.nikoleris@arm.comBaseCache::CacheReqPacketQueue::sendDeferredPacket()
243512724Snikos.nikoleris@arm.com{
243612724Snikos.nikoleris@arm.com    // sanity check
243712724Snikos.nikoleris@arm.com    assert(!waitingOnRetry);
243812724Snikos.nikoleris@arm.com
243912724Snikos.nikoleris@arm.com    // there should never be any deferred request packets in the
244012724Snikos.nikoleris@arm.com    // queue, instead we resly on the cache to provide the packets
244112724Snikos.nikoleris@arm.com    // from the MSHR queue or write queue
244212724Snikos.nikoleris@arm.com    assert(deferredPacketReadyTime() == MaxTick);
244312724Snikos.nikoleris@arm.com
244412724Snikos.nikoleris@arm.com    // check for request packets (requests & writebacks)
244512724Snikos.nikoleris@arm.com    QueueEntry* entry = cache.getNextQueueEntry();
244612724Snikos.nikoleris@arm.com
244712724Snikos.nikoleris@arm.com    if (!entry) {
244812724Snikos.nikoleris@arm.com        // can happen if e.g. we attempt a writeback and fail, but
244912724Snikos.nikoleris@arm.com        // before the retry, the writeback is eliminated because
245012724Snikos.nikoleris@arm.com        // we snoop another cache's ReadEx.
245112724Snikos.nikoleris@arm.com    } else {
245212724Snikos.nikoleris@arm.com        // let our snoop responses go first if there are responses to
245312724Snikos.nikoleris@arm.com        // the same addresses
245412724Snikos.nikoleris@arm.com        if (checkConflictingSnoop(entry->blkAddr)) {
245512724Snikos.nikoleris@arm.com            return;
245612724Snikos.nikoleris@arm.com        }
245712724Snikos.nikoleris@arm.com        waitingOnRetry = entry->sendPacket(cache);
245812724Snikos.nikoleris@arm.com    }
245912724Snikos.nikoleris@arm.com
246012724Snikos.nikoleris@arm.com    // if we succeeded and are not waiting for a retry, schedule the
246112724Snikos.nikoleris@arm.com    // next send considering when the next queue is ready, note that
246212724Snikos.nikoleris@arm.com    // snoop responses have their own packet queue and thus schedule
246312724Snikos.nikoleris@arm.com    // their own events
246412724Snikos.nikoleris@arm.com    if (!waitingOnRetry) {
246512724Snikos.nikoleris@arm.com        schedSendEvent(cache.nextQueueReadyTime());
246612724Snikos.nikoleris@arm.com    }
246712724Snikos.nikoleris@arm.com}
246812724Snikos.nikoleris@arm.com
246912724Snikos.nikoleris@arm.comBaseCache::MemSidePort::MemSidePort(const std::string &_name,
247012724Snikos.nikoleris@arm.com                                    BaseCache *_cache,
247112724Snikos.nikoleris@arm.com                                    const std::string &_label)
247212724Snikos.nikoleris@arm.com    : CacheMasterPort(_name, _cache, _reqQueue, _snoopRespQueue),
247312724Snikos.nikoleris@arm.com      _reqQueue(*_cache, *this, _snoopRespQueue, _label),
247413564Snikos.nikoleris@arm.com      _snoopRespQueue(*_cache, *this, true, _label), cache(_cache)
247512724Snikos.nikoleris@arm.com{
247612724Snikos.nikoleris@arm.com}
247713352Snikos.nikoleris@arm.com
247813352Snikos.nikoleris@arm.comvoid
247913352Snikos.nikoleris@arm.comWriteAllocator::updateMode(Addr write_addr, unsigned write_size,
248013352Snikos.nikoleris@arm.com                           Addr blk_addr)
248113352Snikos.nikoleris@arm.com{
248213352Snikos.nikoleris@arm.com    // check if we are continuing where the last write ended
248313352Snikos.nikoleris@arm.com    if (nextAddr == write_addr) {
248413352Snikos.nikoleris@arm.com        delayCtr[blk_addr] = delayThreshold;
248513352Snikos.nikoleris@arm.com        // stop if we have already saturated
248613352Snikos.nikoleris@arm.com        if (mode != WriteMode::NO_ALLOCATE) {
248713352Snikos.nikoleris@arm.com            byteCount += write_size;
248813352Snikos.nikoleris@arm.com            // switch to streaming mode if we have passed the lower
248913352Snikos.nikoleris@arm.com            // threshold
249013352Snikos.nikoleris@arm.com            if (mode == WriteMode::ALLOCATE &&
249113352Snikos.nikoleris@arm.com                byteCount > coalesceLimit) {
249213352Snikos.nikoleris@arm.com                mode = WriteMode::COALESCE;
249313352Snikos.nikoleris@arm.com                DPRINTF(Cache, "Switched to write coalescing\n");
249413352Snikos.nikoleris@arm.com            } else if (mode == WriteMode::COALESCE &&
249513352Snikos.nikoleris@arm.com                       byteCount > noAllocateLimit) {
249613352Snikos.nikoleris@arm.com                // and continue and switch to non-allocating mode if we
249713352Snikos.nikoleris@arm.com                // pass the upper threshold
249813352Snikos.nikoleris@arm.com                mode = WriteMode::NO_ALLOCATE;
249913352Snikos.nikoleris@arm.com                DPRINTF(Cache, "Switched to write-no-allocate\n");
250013352Snikos.nikoleris@arm.com            }
250113352Snikos.nikoleris@arm.com        }
250213352Snikos.nikoleris@arm.com    } else {
250313352Snikos.nikoleris@arm.com        // we did not see a write matching the previous one, start
250413352Snikos.nikoleris@arm.com        // over again
250513352Snikos.nikoleris@arm.com        byteCount = write_size;
250613352Snikos.nikoleris@arm.com        mode = WriteMode::ALLOCATE;
250713352Snikos.nikoleris@arm.com        resetDelay(blk_addr);
250813352Snikos.nikoleris@arm.com    }
250913352Snikos.nikoleris@arm.com    nextAddr = write_addr + write_size;
251013352Snikos.nikoleris@arm.com}
251113352Snikos.nikoleris@arm.com
251213352Snikos.nikoleris@arm.comWriteAllocator*
251313352Snikos.nikoleris@arm.comWriteAllocatorParams::create()
251413352Snikos.nikoleris@arm.com{
251513352Snikos.nikoleris@arm.com    return new WriteAllocator(this);
251613352Snikos.nikoleris@arm.com}
2517