base.cc revision 12729
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"
5512724Snikos.nikoleris@arm.com#include "debug/CacheVerbose.hh"
5611486Snikos.nikoleris@arm.com#include "mem/cache/mshr.hh"
5712724Snikos.nikoleris@arm.com#include "mem/cache/prefetch/base.hh"
5812724Snikos.nikoleris@arm.com#include "mem/cache/queue_entry.hh"
5912724Snikos.nikoleris@arm.com#include "params/BaseCache.hh"
6012724Snikos.nikoleris@arm.com#include "sim/core.hh"
6112724Snikos.nikoleris@arm.com
6212724Snikos.nikoleris@arm.comclass BaseMasterPort;
6312724Snikos.nikoleris@arm.comclass BaseSlavePort;
642810SN/A
652810SN/Ausing namespace std;
662810SN/A
678856Sandreas.hansson@arm.comBaseCache::CacheSlavePort::CacheSlavePort(const std::string &_name,
688856Sandreas.hansson@arm.com                                          BaseCache *_cache,
698856Sandreas.hansson@arm.com                                          const std::string &_label)
708922Swilliam.wang@arm.com    : QueuedSlavePort(_name, _cache, queue), queue(*_cache, *this, _label),
7112084Sspwilson2@wisc.edu      blocked(false), mustSendRetry(false),
7212084Sspwilson2@wisc.edu      sendRetryEvent([this]{ processSendRetry(); }, _name)
738856Sandreas.hansson@arm.com{
748856Sandreas.hansson@arm.com}
754475SN/A
7611053Sandreas.hansson@arm.comBaseCache::BaseCache(const BaseCacheParams *p, unsigned blk_size)
775034SN/A    : MemObject(p),
7812724Snikos.nikoleris@arm.com      cpuSidePort (p->name + ".cpu_side", this, "CpuSidePort"),
7912724Snikos.nikoleris@arm.com      memSidePort(p->name + ".mem_side", this, "MemSidePort"),
8011377Sandreas.hansson@arm.com      mshrQueue("MSHRs", p->mshrs, 0, p->demand_mshr_reserve), // see below
8111377Sandreas.hansson@arm.com      writeBuffer("write buffer", p->write_buffers, p->mshrs), // see below
8212724Snikos.nikoleris@arm.com      tags(p->tags),
8312724Snikos.nikoleris@arm.com      prefetcher(p->prefetcher),
8412724Snikos.nikoleris@arm.com      prefetchOnAccess(p->prefetch_on_access),
8512724Snikos.nikoleris@arm.com      writebackClean(p->writeback_clean),
8612724Snikos.nikoleris@arm.com      tempBlockWriteback(nullptr),
8712724Snikos.nikoleris@arm.com      writebackTempBlockAtomicEvent([this]{ writebackTempBlockAtomic(); },
8812724Snikos.nikoleris@arm.com                                    name(), false,
8912724Snikos.nikoleris@arm.com                                    EventBase::Delayed_Writeback_Pri),
9011053Sandreas.hansson@arm.com      blkSize(blk_size),
9111722Ssophiane.senni@gmail.com      lookupLatency(p->tag_latency),
9211722Ssophiane.senni@gmail.com      dataLatency(p->data_latency),
9311722Ssophiane.senni@gmail.com      forwardLatency(p->tag_latency),
9411722Ssophiane.senni@gmail.com      fillLatency(p->data_latency),
959263Smrinmoy.ghosh@arm.com      responseLatency(p->response_latency),
965034SN/A      numTarget(p->tgts_per_mshr),
9711331Sandreas.hansson@arm.com      forwardSnoops(true),
9812724Snikos.nikoleris@arm.com      clusivity(p->clusivity),
9910884Sandreas.hansson@arm.com      isReadOnly(p->is_read_only),
1004626SN/A      blocked(0),
10110360Sandreas.hansson@arm.com      order(0),
10211484Snikos.nikoleris@arm.com      noTargetMSHR(nullptr),
1035034SN/A      missCount(p->max_miss_count),
1048883SAli.Saidi@ARM.com      addrRanges(p->addr_ranges.begin(), p->addr_ranges.end()),
1058833Sdam.sunwoo@arm.com      system(p->system)
1064458SN/A{
10711377Sandreas.hansson@arm.com    // the MSHR queue has no reserve entries as we check the MSHR
10811377Sandreas.hansson@arm.com    // queue on every single allocation, whereas the write queue has
10911377Sandreas.hansson@arm.com    // as many reserve entries as we have MSHRs, since every MSHR may
11011377Sandreas.hansson@arm.com    // eventually require a writeback, and we do not check the write
11111377Sandreas.hansson@arm.com    // buffer before committing to an MSHR
11211377Sandreas.hansson@arm.com
11311331Sandreas.hansson@arm.com    // forward snoops is overridden in init() once we can query
11411331Sandreas.hansson@arm.com    // whether the connected master is actually snooping or not
11512724Snikos.nikoleris@arm.com
11612724Snikos.nikoleris@arm.com    tempBlock = new CacheBlk();
11712724Snikos.nikoleris@arm.com    tempBlock->data = new uint8_t[blkSize];
11812724Snikos.nikoleris@arm.com
11912724Snikos.nikoleris@arm.com    tags->setCache(this);
12012724Snikos.nikoleris@arm.com    if (prefetcher)
12112724Snikos.nikoleris@arm.com        prefetcher->setCache(this);
12212724Snikos.nikoleris@arm.com}
12312724Snikos.nikoleris@arm.com
12412724Snikos.nikoleris@arm.comBaseCache::~BaseCache()
12512724Snikos.nikoleris@arm.com{
12612724Snikos.nikoleris@arm.com    delete [] tempBlock->data;
12712724Snikos.nikoleris@arm.com    delete tempBlock;
1282810SN/A}
1292810SN/A
1303013SN/Avoid
1318856Sandreas.hansson@arm.comBaseCache::CacheSlavePort::setBlocked()
1322810SN/A{
1333013SN/A    assert(!blocked);
13410714Sandreas.hansson@arm.com    DPRINTF(CachePort, "Port is blocking new requests\n");
1352810SN/A    blocked = true;
1369614Srene.dejong@arm.com    // if we already scheduled a retry in this cycle, but it has not yet
1379614Srene.dejong@arm.com    // happened, cancel it
1389614Srene.dejong@arm.com    if (sendRetryEvent.scheduled()) {
13910345SCurtis.Dunham@arm.com        owner.deschedule(sendRetryEvent);
14010714Sandreas.hansson@arm.com        DPRINTF(CachePort, "Port descheduled retry\n");
14110345SCurtis.Dunham@arm.com        mustSendRetry = true;
1429614Srene.dejong@arm.com    }
1432810SN/A}
1442810SN/A
1452810SN/Avoid
1468856Sandreas.hansson@arm.comBaseCache::CacheSlavePort::clearBlocked()
1472810SN/A{
1483013SN/A    assert(blocked);
14910714Sandreas.hansson@arm.com    DPRINTF(CachePort, "Port is accepting new requests\n");
1503013SN/A    blocked = false;
1518856Sandreas.hansson@arm.com    if (mustSendRetry) {
15210714Sandreas.hansson@arm.com        // @TODO: need to find a better time (next cycle?)
1538922Swilliam.wang@arm.com        owner.schedule(sendRetryEvent, curTick() + 1);
1542897SN/A    }
1552810SN/A}
1562810SN/A
15710344Sandreas.hansson@arm.comvoid
15810344Sandreas.hansson@arm.comBaseCache::CacheSlavePort::processSendRetry()
15910344Sandreas.hansson@arm.com{
16010714Sandreas.hansson@arm.com    DPRINTF(CachePort, "Port is sending retry\n");
16110344Sandreas.hansson@arm.com
16210344Sandreas.hansson@arm.com    // reset the flag and call retry
16310344Sandreas.hansson@arm.com    mustSendRetry = false;
16410713Sandreas.hansson@arm.com    sendRetryReq();
16510344Sandreas.hansson@arm.com}
1662844SN/A
1672810SN/Avoid
1682858SN/ABaseCache::init()
1692858SN/A{
17012724Snikos.nikoleris@arm.com    if (!cpuSidePort.isConnected() || !memSidePort.isConnected())
1718922Swilliam.wang@arm.com        fatal("Cache ports on %s are not connected\n", name());
17212724Snikos.nikoleris@arm.com    cpuSidePort.sendRangeChange();
17312724Snikos.nikoleris@arm.com    forwardSnoops = cpuSidePort.isSnooping();
1742858SN/A}
1752858SN/A
1769294Sandreas.hansson@arm.comBaseMasterPort &
1779294Sandreas.hansson@arm.comBaseCache::getMasterPort(const std::string &if_name, PortID idx)
1788922Swilliam.wang@arm.com{
1798922Swilliam.wang@arm.com    if (if_name == "mem_side") {
18012724Snikos.nikoleris@arm.com        return memSidePort;
1818922Swilliam.wang@arm.com    }  else {
1828922Swilliam.wang@arm.com        return MemObject::getMasterPort(if_name, idx);
1838922Swilliam.wang@arm.com    }
1848922Swilliam.wang@arm.com}
1858922Swilliam.wang@arm.com
1869294Sandreas.hansson@arm.comBaseSlavePort &
1879294Sandreas.hansson@arm.comBaseCache::getSlavePort(const std::string &if_name, PortID idx)
1888922Swilliam.wang@arm.com{
1898922Swilliam.wang@arm.com    if (if_name == "cpu_side") {
19012724Snikos.nikoleris@arm.com        return cpuSidePort;
1918922Swilliam.wang@arm.com    } else {
1928922Swilliam.wang@arm.com        return MemObject::getSlavePort(if_name, idx);
1938922Swilliam.wang@arm.com    }
1948922Swilliam.wang@arm.com}
1954628SN/A
19610821Sandreas.hansson@arm.combool
19710821Sandreas.hansson@arm.comBaseCache::inRange(Addr addr) const
19810821Sandreas.hansson@arm.com{
19910821Sandreas.hansson@arm.com    for (const auto& r : addrRanges) {
20010821Sandreas.hansson@arm.com        if (r.contains(addr)) {
20110821Sandreas.hansson@arm.com            return true;
20210821Sandreas.hansson@arm.com       }
20310821Sandreas.hansson@arm.com    }
20410821Sandreas.hansson@arm.com    return false;
20510821Sandreas.hansson@arm.com}
20610821Sandreas.hansson@arm.com
2072858SN/Avoid
20812724Snikos.nikoleris@arm.comBaseCache::handleTimingReqHit(PacketPtr pkt, CacheBlk *blk, Tick request_time)
20912724Snikos.nikoleris@arm.com{
21012724Snikos.nikoleris@arm.com    if (pkt->needsResponse()) {
21112724Snikos.nikoleris@arm.com        pkt->makeTimingResponse();
21212724Snikos.nikoleris@arm.com        // @todo: Make someone pay for this
21312724Snikos.nikoleris@arm.com        pkt->headerDelay = pkt->payloadDelay = 0;
21412724Snikos.nikoleris@arm.com
21512724Snikos.nikoleris@arm.com        // In this case we are considering request_time that takes
21612724Snikos.nikoleris@arm.com        // into account the delay of the xbar, if any, and just
21712724Snikos.nikoleris@arm.com        // lat, neglecting responseLatency, modelling hit latency
21812724Snikos.nikoleris@arm.com        // just as lookupLatency or or the value of lat overriden
21912724Snikos.nikoleris@arm.com        // by access(), that calls accessBlock() function.
22012724Snikos.nikoleris@arm.com        cpuSidePort.schedTimingResp(pkt, request_time, true);
22112724Snikos.nikoleris@arm.com    } else {
22212724Snikos.nikoleris@arm.com        DPRINTF(Cache, "%s satisfied %s, no response needed\n", __func__,
22312724Snikos.nikoleris@arm.com                pkt->print());
22412724Snikos.nikoleris@arm.com
22512724Snikos.nikoleris@arm.com        // queue the packet for deletion, as the sending cache is
22612724Snikos.nikoleris@arm.com        // still relying on it; if the block is found in access(),
22712724Snikos.nikoleris@arm.com        // CleanEvict and Writeback messages will be deleted
22812724Snikos.nikoleris@arm.com        // here as well
22912724Snikos.nikoleris@arm.com        pendingDelete.reset(pkt);
23012724Snikos.nikoleris@arm.com    }
23112724Snikos.nikoleris@arm.com}
23212724Snikos.nikoleris@arm.com
23312724Snikos.nikoleris@arm.comvoid
23412724Snikos.nikoleris@arm.comBaseCache::handleTimingReqMiss(PacketPtr pkt, MSHR *mshr, CacheBlk *blk,
23512724Snikos.nikoleris@arm.com                               Tick forward_time, Tick request_time)
23612724Snikos.nikoleris@arm.com{
23712724Snikos.nikoleris@arm.com    if (mshr) {
23812724Snikos.nikoleris@arm.com        /// MSHR hit
23912724Snikos.nikoleris@arm.com        /// @note writebacks will be checked in getNextMSHR()
24012724Snikos.nikoleris@arm.com        /// for any conflicting requests to the same block
24112724Snikos.nikoleris@arm.com
24212724Snikos.nikoleris@arm.com        //@todo remove hw_pf here
24312724Snikos.nikoleris@arm.com
24412724Snikos.nikoleris@arm.com        // Coalesce unless it was a software prefetch (see above).
24512724Snikos.nikoleris@arm.com        if (pkt) {
24612724Snikos.nikoleris@arm.com            assert(!pkt->isWriteback());
24712724Snikos.nikoleris@arm.com            // CleanEvicts corresponding to blocks which have
24812724Snikos.nikoleris@arm.com            // outstanding requests in MSHRs are simply sunk here
24912724Snikos.nikoleris@arm.com            if (pkt->cmd == MemCmd::CleanEvict) {
25012724Snikos.nikoleris@arm.com                pendingDelete.reset(pkt);
25112724Snikos.nikoleris@arm.com            } else if (pkt->cmd == MemCmd::WriteClean) {
25212724Snikos.nikoleris@arm.com                // A WriteClean should never coalesce with any
25312724Snikos.nikoleris@arm.com                // outstanding cache maintenance requests.
25412724Snikos.nikoleris@arm.com
25512724Snikos.nikoleris@arm.com                // We use forward_time here because there is an
25612724Snikos.nikoleris@arm.com                // uncached memory write, forwarded to WriteBuffer.
25712724Snikos.nikoleris@arm.com                allocateWriteBuffer(pkt, forward_time);
25812724Snikos.nikoleris@arm.com            } else {
25912724Snikos.nikoleris@arm.com                DPRINTF(Cache, "%s coalescing MSHR for %s\n", __func__,
26012724Snikos.nikoleris@arm.com                        pkt->print());
26112724Snikos.nikoleris@arm.com
26212724Snikos.nikoleris@arm.com                assert(pkt->req->masterId() < system->maxMasters());
26312724Snikos.nikoleris@arm.com                mshr_hits[pkt->cmdToIndex()][pkt->req->masterId()]++;
26412724Snikos.nikoleris@arm.com
26512724Snikos.nikoleris@arm.com                // We use forward_time here because it is the same
26612724Snikos.nikoleris@arm.com                // considering new targets. We have multiple
26712724Snikos.nikoleris@arm.com                // requests for the same address here. It
26812724Snikos.nikoleris@arm.com                // specifies the latency to allocate an internal
26912724Snikos.nikoleris@arm.com                // buffer and to schedule an event to the queued
27012724Snikos.nikoleris@arm.com                // port and also takes into account the additional
27112724Snikos.nikoleris@arm.com                // delay of the xbar.
27212724Snikos.nikoleris@arm.com                mshr->allocateTarget(pkt, forward_time, order++,
27312724Snikos.nikoleris@arm.com                                     allocOnFill(pkt->cmd));
27412724Snikos.nikoleris@arm.com                if (mshr->getNumTargets() == numTarget) {
27512724Snikos.nikoleris@arm.com                    noTargetMSHR = mshr;
27612724Snikos.nikoleris@arm.com                    setBlocked(Blocked_NoTargets);
27712724Snikos.nikoleris@arm.com                    // need to be careful with this... if this mshr isn't
27812724Snikos.nikoleris@arm.com                    // ready yet (i.e. time > curTick()), we don't want to
27912724Snikos.nikoleris@arm.com                    // move it ahead of mshrs that are ready
28012724Snikos.nikoleris@arm.com                    // mshrQueue.moveToFront(mshr);
28112724Snikos.nikoleris@arm.com                }
28212724Snikos.nikoleris@arm.com            }
28312724Snikos.nikoleris@arm.com        }
28412724Snikos.nikoleris@arm.com    } else {
28512724Snikos.nikoleris@arm.com        // no MSHR
28612724Snikos.nikoleris@arm.com        assert(pkt->req->masterId() < system->maxMasters());
28712724Snikos.nikoleris@arm.com        mshr_misses[pkt->cmdToIndex()][pkt->req->masterId()]++;
28812724Snikos.nikoleris@arm.com
28912724Snikos.nikoleris@arm.com        if (pkt->isEviction() || pkt->cmd == MemCmd::WriteClean) {
29012724Snikos.nikoleris@arm.com            // We use forward_time here because there is an
29112724Snikos.nikoleris@arm.com            // writeback or writeclean, forwarded to WriteBuffer.
29212724Snikos.nikoleris@arm.com            allocateWriteBuffer(pkt, forward_time);
29312724Snikos.nikoleris@arm.com        } else {
29412724Snikos.nikoleris@arm.com            if (blk && blk->isValid()) {
29512724Snikos.nikoleris@arm.com                // If we have a write miss to a valid block, we
29612724Snikos.nikoleris@arm.com                // need to mark the block non-readable.  Otherwise
29712724Snikos.nikoleris@arm.com                // if we allow reads while there's an outstanding
29812724Snikos.nikoleris@arm.com                // write miss, the read could return stale data
29912724Snikos.nikoleris@arm.com                // out of the cache block... a more aggressive
30012724Snikos.nikoleris@arm.com                // system could detect the overlap (if any) and
30112724Snikos.nikoleris@arm.com                // forward data out of the MSHRs, but we don't do
30212724Snikos.nikoleris@arm.com                // that yet.  Note that we do need to leave the
30312724Snikos.nikoleris@arm.com                // block valid so that it stays in the cache, in
30412724Snikos.nikoleris@arm.com                // case we get an upgrade response (and hence no
30512724Snikos.nikoleris@arm.com                // new data) when the write miss completes.
30612724Snikos.nikoleris@arm.com                // As long as CPUs do proper store/load forwarding
30712724Snikos.nikoleris@arm.com                // internally, and have a sufficiently weak memory
30812724Snikos.nikoleris@arm.com                // model, this is probably unnecessary, but at some
30912724Snikos.nikoleris@arm.com                // point it must have seemed like we needed it...
31012724Snikos.nikoleris@arm.com                assert((pkt->needsWritable() && !blk->isWritable()) ||
31112724Snikos.nikoleris@arm.com                       pkt->req->isCacheMaintenance());
31212724Snikos.nikoleris@arm.com                blk->status &= ~BlkReadable;
31312724Snikos.nikoleris@arm.com            }
31412724Snikos.nikoleris@arm.com            // Here we are using forward_time, modelling the latency of
31512724Snikos.nikoleris@arm.com            // a miss (outbound) just as forwardLatency, neglecting the
31612724Snikos.nikoleris@arm.com            // lookupLatency component.
31712724Snikos.nikoleris@arm.com            allocateMissBuffer(pkt, forward_time);
31812724Snikos.nikoleris@arm.com        }
31912724Snikos.nikoleris@arm.com    }
32012724Snikos.nikoleris@arm.com}
32112724Snikos.nikoleris@arm.com
32212724Snikos.nikoleris@arm.comvoid
32312724Snikos.nikoleris@arm.comBaseCache::recvTimingReq(PacketPtr pkt)
32412724Snikos.nikoleris@arm.com{
32512724Snikos.nikoleris@arm.com    // anything that is merely forwarded pays for the forward latency and
32612724Snikos.nikoleris@arm.com    // the delay provided by the crossbar
32712724Snikos.nikoleris@arm.com    Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
32812724Snikos.nikoleris@arm.com
32912724Snikos.nikoleris@arm.com    // We use lookupLatency here because it is used to specify the latency
33012724Snikos.nikoleris@arm.com    // to access.
33112724Snikos.nikoleris@arm.com    Cycles lat = lookupLatency;
33212724Snikos.nikoleris@arm.com    CacheBlk *blk = nullptr;
33312724Snikos.nikoleris@arm.com    bool satisfied = false;
33412724Snikos.nikoleris@arm.com    {
33512724Snikos.nikoleris@arm.com        PacketList writebacks;
33612724Snikos.nikoleris@arm.com        // Note that lat is passed by reference here. The function
33712724Snikos.nikoleris@arm.com        // access() calls accessBlock() which can modify lat value.
33812724Snikos.nikoleris@arm.com        satisfied = access(pkt, blk, lat, writebacks);
33912724Snikos.nikoleris@arm.com
34012724Snikos.nikoleris@arm.com        // copy writebacks to write buffer here to ensure they logically
34112724Snikos.nikoleris@arm.com        // proceed anything happening below
34212724Snikos.nikoleris@arm.com        doWritebacks(writebacks, forward_time);
34312724Snikos.nikoleris@arm.com    }
34412724Snikos.nikoleris@arm.com
34512724Snikos.nikoleris@arm.com    // Here we charge the headerDelay that takes into account the latencies
34612724Snikos.nikoleris@arm.com    // of the bus, if the packet comes from it.
34712724Snikos.nikoleris@arm.com    // The latency charged it is just lat that is the value of lookupLatency
34812724Snikos.nikoleris@arm.com    // modified by access() function, or if not just lookupLatency.
34912724Snikos.nikoleris@arm.com    // In case of a hit we are neglecting response latency.
35012724Snikos.nikoleris@arm.com    // In case of a miss we are neglecting forward latency.
35112724Snikos.nikoleris@arm.com    Tick request_time = clockEdge(lat) + pkt->headerDelay;
35212724Snikos.nikoleris@arm.com    // Here we reset the timing of the packet.
35312724Snikos.nikoleris@arm.com    pkt->headerDelay = pkt->payloadDelay = 0;
35412724Snikos.nikoleris@arm.com    // track time of availability of next prefetch, if any
35512724Snikos.nikoleris@arm.com    Tick next_pf_time = MaxTick;
35612724Snikos.nikoleris@arm.com
35712724Snikos.nikoleris@arm.com    if (satisfied) {
35812724Snikos.nikoleris@arm.com        // if need to notify the prefetcher we have to do it before
35912724Snikos.nikoleris@arm.com        // anything else as later handleTimingReqHit might turn the
36012724Snikos.nikoleris@arm.com        // packet in a response
36112724Snikos.nikoleris@arm.com        if (prefetcher &&
36212724Snikos.nikoleris@arm.com            (prefetchOnAccess || (blk && blk->wasPrefetched()))) {
36312724Snikos.nikoleris@arm.com            if (blk)
36412724Snikos.nikoleris@arm.com                blk->status &= ~BlkHWPrefetched;
36512724Snikos.nikoleris@arm.com
36612724Snikos.nikoleris@arm.com            // Don't notify on SWPrefetch
36712724Snikos.nikoleris@arm.com            if (!pkt->cmd.isSWPrefetch()) {
36812724Snikos.nikoleris@arm.com                assert(!pkt->req->isCacheMaintenance());
36912724Snikos.nikoleris@arm.com                next_pf_time = prefetcher->notify(pkt);
37012724Snikos.nikoleris@arm.com            }
37112724Snikos.nikoleris@arm.com        }
37212724Snikos.nikoleris@arm.com
37312724Snikos.nikoleris@arm.com        handleTimingReqHit(pkt, blk, request_time);
37412724Snikos.nikoleris@arm.com    } else {
37512724Snikos.nikoleris@arm.com        handleTimingReqMiss(pkt, blk, forward_time, request_time);
37612724Snikos.nikoleris@arm.com
37712724Snikos.nikoleris@arm.com        // We should call the prefetcher reguardless if the request is
37812724Snikos.nikoleris@arm.com        // satisfied or not, reguardless if the request is in the MSHR
37912724Snikos.nikoleris@arm.com        // or not. The request could be a ReadReq hit, but still not
38012724Snikos.nikoleris@arm.com        // satisfied (potentially because of a prior write to the same
38112724Snikos.nikoleris@arm.com        // cache line. So, even when not satisfied, there is an MSHR
38212724Snikos.nikoleris@arm.com        // already allocated for this, we need to let the prefetcher
38312724Snikos.nikoleris@arm.com        // know about the request
38412724Snikos.nikoleris@arm.com
38512724Snikos.nikoleris@arm.com        // Don't notify prefetcher on SWPrefetch or cache maintenance
38612724Snikos.nikoleris@arm.com        // operations
38712724Snikos.nikoleris@arm.com        if (prefetcher && pkt &&
38812724Snikos.nikoleris@arm.com            !pkt->cmd.isSWPrefetch() &&
38912724Snikos.nikoleris@arm.com            !pkt->req->isCacheMaintenance()) {
39012724Snikos.nikoleris@arm.com            next_pf_time = prefetcher->notify(pkt);
39112724Snikos.nikoleris@arm.com        }
39212724Snikos.nikoleris@arm.com    }
39312724Snikos.nikoleris@arm.com
39412724Snikos.nikoleris@arm.com    if (next_pf_time != MaxTick) {
39512724Snikos.nikoleris@arm.com        schedMemSideSendEvent(next_pf_time);
39612724Snikos.nikoleris@arm.com    }
39712724Snikos.nikoleris@arm.com}
39812724Snikos.nikoleris@arm.com
39912724Snikos.nikoleris@arm.comvoid
40012724Snikos.nikoleris@arm.comBaseCache::handleUncacheableWriteResp(PacketPtr pkt)
40112724Snikos.nikoleris@arm.com{
40212724Snikos.nikoleris@arm.com    Tick completion_time = clockEdge(responseLatency) +
40312724Snikos.nikoleris@arm.com        pkt->headerDelay + pkt->payloadDelay;
40412724Snikos.nikoleris@arm.com
40512724Snikos.nikoleris@arm.com    // Reset the bus additional time as it is now accounted for
40612724Snikos.nikoleris@arm.com    pkt->headerDelay = pkt->payloadDelay = 0;
40712724Snikos.nikoleris@arm.com
40812724Snikos.nikoleris@arm.com    cpuSidePort.schedTimingResp(pkt, completion_time, true);
40912724Snikos.nikoleris@arm.com}
41012724Snikos.nikoleris@arm.com
41112724Snikos.nikoleris@arm.comvoid
41212724Snikos.nikoleris@arm.comBaseCache::recvTimingResp(PacketPtr pkt)
41312724Snikos.nikoleris@arm.com{
41412724Snikos.nikoleris@arm.com    assert(pkt->isResponse());
41512724Snikos.nikoleris@arm.com
41612724Snikos.nikoleris@arm.com    // all header delay should be paid for by the crossbar, unless
41712724Snikos.nikoleris@arm.com    // this is a prefetch response from above
41812724Snikos.nikoleris@arm.com    panic_if(pkt->headerDelay != 0 && pkt->cmd != MemCmd::HardPFResp,
41912724Snikos.nikoleris@arm.com             "%s saw a non-zero packet delay\n", name());
42012724Snikos.nikoleris@arm.com
42112724Snikos.nikoleris@arm.com    const bool is_error = pkt->isError();
42212724Snikos.nikoleris@arm.com
42312724Snikos.nikoleris@arm.com    if (is_error) {
42412724Snikos.nikoleris@arm.com        DPRINTF(Cache, "%s: Cache received %s with error\n", __func__,
42512724Snikos.nikoleris@arm.com                pkt->print());
42612724Snikos.nikoleris@arm.com    }
42712724Snikos.nikoleris@arm.com
42812724Snikos.nikoleris@arm.com    DPRINTF(Cache, "%s: Handling response %s\n", __func__,
42912724Snikos.nikoleris@arm.com            pkt->print());
43012724Snikos.nikoleris@arm.com
43112724Snikos.nikoleris@arm.com    // if this is a write, we should be looking at an uncacheable
43212724Snikos.nikoleris@arm.com    // write
43312724Snikos.nikoleris@arm.com    if (pkt->isWrite()) {
43412724Snikos.nikoleris@arm.com        assert(pkt->req->isUncacheable());
43512724Snikos.nikoleris@arm.com        handleUncacheableWriteResp(pkt);
43612724Snikos.nikoleris@arm.com        return;
43712724Snikos.nikoleris@arm.com    }
43812724Snikos.nikoleris@arm.com
43912724Snikos.nikoleris@arm.com    // we have dealt with any (uncacheable) writes above, from here on
44012724Snikos.nikoleris@arm.com    // we know we are dealing with an MSHR due to a miss or a prefetch
44112724Snikos.nikoleris@arm.com    MSHR *mshr = dynamic_cast<MSHR*>(pkt->popSenderState());
44212724Snikos.nikoleris@arm.com    assert(mshr);
44312724Snikos.nikoleris@arm.com
44412724Snikos.nikoleris@arm.com    if (mshr == noTargetMSHR) {
44512724Snikos.nikoleris@arm.com        // we always clear at least one target
44612724Snikos.nikoleris@arm.com        clearBlocked(Blocked_NoTargets);
44712724Snikos.nikoleris@arm.com        noTargetMSHR = nullptr;
44812724Snikos.nikoleris@arm.com    }
44912724Snikos.nikoleris@arm.com
45012724Snikos.nikoleris@arm.com    // Initial target is used just for stats
45112724Snikos.nikoleris@arm.com    MSHR::Target *initial_tgt = mshr->getTarget();
45212724Snikos.nikoleris@arm.com    int stats_cmd_idx = initial_tgt->pkt->cmdToIndex();
45312724Snikos.nikoleris@arm.com    Tick miss_latency = curTick() - initial_tgt->recvTime;
45412724Snikos.nikoleris@arm.com
45512724Snikos.nikoleris@arm.com    if (pkt->req->isUncacheable()) {
45612724Snikos.nikoleris@arm.com        assert(pkt->req->masterId() < system->maxMasters());
45712724Snikos.nikoleris@arm.com        mshr_uncacheable_lat[stats_cmd_idx][pkt->req->masterId()] +=
45812724Snikos.nikoleris@arm.com            miss_latency;
45912724Snikos.nikoleris@arm.com    } else {
46012724Snikos.nikoleris@arm.com        assert(pkt->req->masterId() < system->maxMasters());
46112724Snikos.nikoleris@arm.com        mshr_miss_latency[stats_cmd_idx][pkt->req->masterId()] +=
46212724Snikos.nikoleris@arm.com            miss_latency;
46312724Snikos.nikoleris@arm.com    }
46412724Snikos.nikoleris@arm.com
46512724Snikos.nikoleris@arm.com    PacketList writebacks;
46612724Snikos.nikoleris@arm.com
46712724Snikos.nikoleris@arm.com    bool is_fill = !mshr->isForward &&
46812724Snikos.nikoleris@arm.com        (pkt->isRead() || pkt->cmd == MemCmd::UpgradeResp);
46912724Snikos.nikoleris@arm.com
47012724Snikos.nikoleris@arm.com    CacheBlk *blk = tags->findBlock(pkt->getAddr(), pkt->isSecure());
47112724Snikos.nikoleris@arm.com
47212724Snikos.nikoleris@arm.com    if (is_fill && !is_error) {
47312724Snikos.nikoleris@arm.com        DPRINTF(Cache, "Block for addr %#llx being updated in Cache\n",
47412724Snikos.nikoleris@arm.com                pkt->getAddr());
47512724Snikos.nikoleris@arm.com
47612724Snikos.nikoleris@arm.com        blk = handleFill(pkt, blk, writebacks, mshr->allocOnFill());
47712724Snikos.nikoleris@arm.com        assert(blk != nullptr);
47812724Snikos.nikoleris@arm.com    }
47912724Snikos.nikoleris@arm.com
48012724Snikos.nikoleris@arm.com    if (blk && blk->isValid() && pkt->isClean() && !pkt->isInvalidate()) {
48112724Snikos.nikoleris@arm.com        // The block was marked not readable while there was a pending
48212724Snikos.nikoleris@arm.com        // cache maintenance operation, restore its flag.
48312724Snikos.nikoleris@arm.com        blk->status |= BlkReadable;
48412724Snikos.nikoleris@arm.com    }
48512724Snikos.nikoleris@arm.com
48612724Snikos.nikoleris@arm.com    if (blk && blk->isWritable() && !pkt->req->isCacheInvalidate()) {
48712724Snikos.nikoleris@arm.com        // If at this point the referenced block is writable and the
48812724Snikos.nikoleris@arm.com        // response is not a cache invalidate, we promote targets that
48912724Snikos.nikoleris@arm.com        // were deferred as we couldn't guarrantee a writable copy
49012724Snikos.nikoleris@arm.com        mshr->promoteWritable();
49112724Snikos.nikoleris@arm.com    }
49212724Snikos.nikoleris@arm.com
49312724Snikos.nikoleris@arm.com    serviceMSHRTargets(mshr, pkt, blk, writebacks);
49412724Snikos.nikoleris@arm.com
49512724Snikos.nikoleris@arm.com    if (mshr->promoteDeferredTargets()) {
49612724Snikos.nikoleris@arm.com        // avoid later read getting stale data while write miss is
49712724Snikos.nikoleris@arm.com        // outstanding.. see comment in timingAccess()
49812724Snikos.nikoleris@arm.com        if (blk) {
49912724Snikos.nikoleris@arm.com            blk->status &= ~BlkReadable;
50012724Snikos.nikoleris@arm.com        }
50112724Snikos.nikoleris@arm.com        mshrQueue.markPending(mshr);
50212724Snikos.nikoleris@arm.com        schedMemSideSendEvent(clockEdge() + pkt->payloadDelay);
50312724Snikos.nikoleris@arm.com    } else {
50412724Snikos.nikoleris@arm.com        // while we deallocate an mshr from the queue we still have to
50512724Snikos.nikoleris@arm.com        // check the isFull condition before and after as we might
50612724Snikos.nikoleris@arm.com        // have been using the reserved entries already
50712724Snikos.nikoleris@arm.com        const bool was_full = mshrQueue.isFull();
50812724Snikos.nikoleris@arm.com        mshrQueue.deallocate(mshr);
50912724Snikos.nikoleris@arm.com        if (was_full && !mshrQueue.isFull()) {
51012724Snikos.nikoleris@arm.com            clearBlocked(Blocked_NoMSHRs);
51112724Snikos.nikoleris@arm.com        }
51212724Snikos.nikoleris@arm.com
51312724Snikos.nikoleris@arm.com        // Request the bus for a prefetch if this deallocation freed enough
51412724Snikos.nikoleris@arm.com        // MSHRs for a prefetch to take place
51512724Snikos.nikoleris@arm.com        if (prefetcher && mshrQueue.canPrefetch()) {
51612724Snikos.nikoleris@arm.com            Tick next_pf_time = std::max(prefetcher->nextPrefetchReadyTime(),
51712724Snikos.nikoleris@arm.com                                         clockEdge());
51812724Snikos.nikoleris@arm.com            if (next_pf_time != MaxTick)
51912724Snikos.nikoleris@arm.com                schedMemSideSendEvent(next_pf_time);
52012724Snikos.nikoleris@arm.com        }
52112724Snikos.nikoleris@arm.com    }
52212724Snikos.nikoleris@arm.com
52312724Snikos.nikoleris@arm.com    // if we used temp block, check to see if its valid and then clear it out
52412724Snikos.nikoleris@arm.com    if (blk == tempBlock && tempBlock->isValid()) {
52512724Snikos.nikoleris@arm.com        evictBlock(blk, writebacks);
52612724Snikos.nikoleris@arm.com    }
52712724Snikos.nikoleris@arm.com
52812724Snikos.nikoleris@arm.com    const Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
52912724Snikos.nikoleris@arm.com    // copy writebacks to write buffer
53012724Snikos.nikoleris@arm.com    doWritebacks(writebacks, forward_time);
53112724Snikos.nikoleris@arm.com
53212724Snikos.nikoleris@arm.com    DPRINTF(CacheVerbose, "%s: Leaving with %s\n", __func__, pkt->print());
53312724Snikos.nikoleris@arm.com    delete pkt;
53412724Snikos.nikoleris@arm.com}
53512724Snikos.nikoleris@arm.com
53612724Snikos.nikoleris@arm.com
53712724Snikos.nikoleris@arm.comTick
53812724Snikos.nikoleris@arm.comBaseCache::recvAtomic(PacketPtr pkt)
53912724Snikos.nikoleris@arm.com{
54012724Snikos.nikoleris@arm.com    // We are in atomic mode so we pay just for lookupLatency here.
54112724Snikos.nikoleris@arm.com    Cycles lat = lookupLatency;
54212724Snikos.nikoleris@arm.com
54312724Snikos.nikoleris@arm.com    // follow the same flow as in recvTimingReq, and check if a cache
54412724Snikos.nikoleris@arm.com    // above us is responding
54512724Snikos.nikoleris@arm.com    if (pkt->cacheResponding() && !pkt->isClean()) {
54612724Snikos.nikoleris@arm.com        assert(!pkt->req->isCacheInvalidate());
54712724Snikos.nikoleris@arm.com        DPRINTF(Cache, "Cache above responding to %s: not responding\n",
54812724Snikos.nikoleris@arm.com                pkt->print());
54912724Snikos.nikoleris@arm.com
55012724Snikos.nikoleris@arm.com        // if a cache is responding, and it had the line in Owned
55112724Snikos.nikoleris@arm.com        // rather than Modified state, we need to invalidate any
55212724Snikos.nikoleris@arm.com        // copies that are not on the same path to memory
55312724Snikos.nikoleris@arm.com        assert(pkt->needsWritable() && !pkt->responderHadWritable());
55412724Snikos.nikoleris@arm.com        lat += ticksToCycles(memSidePort.sendAtomic(pkt));
55512724Snikos.nikoleris@arm.com
55612724Snikos.nikoleris@arm.com        return lat * clockPeriod();
55712724Snikos.nikoleris@arm.com    }
55812724Snikos.nikoleris@arm.com
55912724Snikos.nikoleris@arm.com    // should assert here that there are no outstanding MSHRs or
56012724Snikos.nikoleris@arm.com    // writebacks... that would mean that someone used an atomic
56112724Snikos.nikoleris@arm.com    // access in timing mode
56212724Snikos.nikoleris@arm.com
56312724Snikos.nikoleris@arm.com    CacheBlk *blk = nullptr;
56412724Snikos.nikoleris@arm.com    PacketList writebacks;
56512724Snikos.nikoleris@arm.com    bool satisfied = access(pkt, blk, lat, writebacks);
56612724Snikos.nikoleris@arm.com
56712724Snikos.nikoleris@arm.com    if (pkt->isClean() && blk && blk->isDirty()) {
56812724Snikos.nikoleris@arm.com        // A cache clean opearation is looking for a dirty
56912724Snikos.nikoleris@arm.com        // block. If a dirty block is encountered a WriteClean
57012724Snikos.nikoleris@arm.com        // will update any copies to the path to the memory
57112724Snikos.nikoleris@arm.com        // until the point of reference.
57212724Snikos.nikoleris@arm.com        DPRINTF(CacheVerbose, "%s: packet %s found block: %s\n",
57312724Snikos.nikoleris@arm.com                __func__, pkt->print(), blk->print());
57412724Snikos.nikoleris@arm.com        PacketPtr wb_pkt = writecleanBlk(blk, pkt->req->getDest(), pkt->id);
57512724Snikos.nikoleris@arm.com        writebacks.push_back(wb_pkt);
57612724Snikos.nikoleris@arm.com        pkt->setSatisfied();
57712724Snikos.nikoleris@arm.com    }
57812724Snikos.nikoleris@arm.com
57912724Snikos.nikoleris@arm.com    // handle writebacks resulting from the access here to ensure they
58012724Snikos.nikoleris@arm.com    // logically proceed anything happening below
58112724Snikos.nikoleris@arm.com    doWritebacksAtomic(writebacks);
58212724Snikos.nikoleris@arm.com    assert(writebacks.empty());
58312724Snikos.nikoleris@arm.com
58412724Snikos.nikoleris@arm.com    if (!satisfied) {
58512724Snikos.nikoleris@arm.com        lat += handleAtomicReqMiss(pkt, blk, writebacks);
58612724Snikos.nikoleris@arm.com    }
58712724Snikos.nikoleris@arm.com
58812724Snikos.nikoleris@arm.com    // Note that we don't invoke the prefetcher at all in atomic mode.
58912724Snikos.nikoleris@arm.com    // It's not clear how to do it properly, particularly for
59012724Snikos.nikoleris@arm.com    // prefetchers that aggressively generate prefetch candidates and
59112724Snikos.nikoleris@arm.com    // rely on bandwidth contention to throttle them; these will tend
59212724Snikos.nikoleris@arm.com    // to pollute the cache in atomic mode since there is no bandwidth
59312724Snikos.nikoleris@arm.com    // contention.  If we ever do want to enable prefetching in atomic
59412724Snikos.nikoleris@arm.com    // mode, though, this is the place to do it... see timingAccess()
59512724Snikos.nikoleris@arm.com    // for an example (though we'd want to issue the prefetch(es)
59612724Snikos.nikoleris@arm.com    // immediately rather than calling requestMemSideBus() as we do
59712724Snikos.nikoleris@arm.com    // there).
59812724Snikos.nikoleris@arm.com
59912724Snikos.nikoleris@arm.com    // do any writebacks resulting from the response handling
60012724Snikos.nikoleris@arm.com    doWritebacksAtomic(writebacks);
60112724Snikos.nikoleris@arm.com
60212724Snikos.nikoleris@arm.com    // if we used temp block, check to see if its valid and if so
60312724Snikos.nikoleris@arm.com    // clear it out, but only do so after the call to recvAtomic is
60412724Snikos.nikoleris@arm.com    // finished so that any downstream observers (such as a snoop
60512724Snikos.nikoleris@arm.com    // filter), first see the fill, and only then see the eviction
60612724Snikos.nikoleris@arm.com    if (blk == tempBlock && tempBlock->isValid()) {
60712724Snikos.nikoleris@arm.com        // the atomic CPU calls recvAtomic for fetch and load/store
60812724Snikos.nikoleris@arm.com        // sequentuially, and we may already have a tempBlock
60912724Snikos.nikoleris@arm.com        // writeback from the fetch that we have not yet sent
61012724Snikos.nikoleris@arm.com        if (tempBlockWriteback) {
61112724Snikos.nikoleris@arm.com            // if that is the case, write the prevoius one back, and
61212724Snikos.nikoleris@arm.com            // do not schedule any new event
61312724Snikos.nikoleris@arm.com            writebackTempBlockAtomic();
61412724Snikos.nikoleris@arm.com        } else {
61512724Snikos.nikoleris@arm.com            // the writeback/clean eviction happens after the call to
61612724Snikos.nikoleris@arm.com            // recvAtomic has finished (but before any successive
61712724Snikos.nikoleris@arm.com            // calls), so that the response handling from the fill is
61812724Snikos.nikoleris@arm.com            // allowed to happen first
61912724Snikos.nikoleris@arm.com            schedule(writebackTempBlockAtomicEvent, curTick());
62012724Snikos.nikoleris@arm.com        }
62112724Snikos.nikoleris@arm.com
62212724Snikos.nikoleris@arm.com        tempBlockWriteback = evictBlock(blk);
62312724Snikos.nikoleris@arm.com    }
62412724Snikos.nikoleris@arm.com
62512724Snikos.nikoleris@arm.com    if (pkt->needsResponse()) {
62612724Snikos.nikoleris@arm.com        pkt->makeAtomicResponse();
62712724Snikos.nikoleris@arm.com    }
62812724Snikos.nikoleris@arm.com
62912724Snikos.nikoleris@arm.com    return lat * clockPeriod();
63012724Snikos.nikoleris@arm.com}
63112724Snikos.nikoleris@arm.com
63212724Snikos.nikoleris@arm.comvoid
63312724Snikos.nikoleris@arm.comBaseCache::functionalAccess(PacketPtr pkt, bool from_cpu_side)
63412724Snikos.nikoleris@arm.com{
63512724Snikos.nikoleris@arm.com    Addr blk_addr = pkt->getBlockAddr(blkSize);
63612724Snikos.nikoleris@arm.com    bool is_secure = pkt->isSecure();
63712724Snikos.nikoleris@arm.com    CacheBlk *blk = tags->findBlock(pkt->getAddr(), is_secure);
63812724Snikos.nikoleris@arm.com    MSHR *mshr = mshrQueue.findMatch(blk_addr, is_secure);
63912724Snikos.nikoleris@arm.com
64012724Snikos.nikoleris@arm.com    pkt->pushLabel(name());
64112724Snikos.nikoleris@arm.com
64212724Snikos.nikoleris@arm.com    CacheBlkPrintWrapper cbpw(blk);
64312724Snikos.nikoleris@arm.com
64412724Snikos.nikoleris@arm.com    // Note that just because an L2/L3 has valid data doesn't mean an
64512724Snikos.nikoleris@arm.com    // L1 doesn't have a more up-to-date modified copy that still
64612724Snikos.nikoleris@arm.com    // needs to be found.  As a result we always update the request if
64712724Snikos.nikoleris@arm.com    // we have it, but only declare it satisfied if we are the owner.
64812724Snikos.nikoleris@arm.com
64912724Snikos.nikoleris@arm.com    // see if we have data at all (owned or otherwise)
65012724Snikos.nikoleris@arm.com    bool have_data = blk && blk->isValid()
65112724Snikos.nikoleris@arm.com        && pkt->checkFunctional(&cbpw, blk_addr, is_secure, blkSize,
65212724Snikos.nikoleris@arm.com                                blk->data);
65312724Snikos.nikoleris@arm.com
65412724Snikos.nikoleris@arm.com    // data we have is dirty if marked as such or if we have an
65512724Snikos.nikoleris@arm.com    // in-service MSHR that is pending a modified line
65612724Snikos.nikoleris@arm.com    bool have_dirty =
65712724Snikos.nikoleris@arm.com        have_data && (blk->isDirty() ||
65812724Snikos.nikoleris@arm.com                      (mshr && mshr->inService && mshr->isPendingModified()));
65912724Snikos.nikoleris@arm.com
66012724Snikos.nikoleris@arm.com    bool done = have_dirty ||
66112724Snikos.nikoleris@arm.com        cpuSidePort.checkFunctional(pkt) ||
66212724Snikos.nikoleris@arm.com        mshrQueue.checkFunctional(pkt, blk_addr) ||
66312724Snikos.nikoleris@arm.com        writeBuffer.checkFunctional(pkt, blk_addr) ||
66412724Snikos.nikoleris@arm.com        memSidePort.checkFunctional(pkt);
66512724Snikos.nikoleris@arm.com
66612724Snikos.nikoleris@arm.com    DPRINTF(CacheVerbose, "%s: %s %s%s%s\n", __func__,  pkt->print(),
66712724Snikos.nikoleris@arm.com            (blk && blk->isValid()) ? "valid " : "",
66812724Snikos.nikoleris@arm.com            have_data ? "data " : "", done ? "done " : "");
66912724Snikos.nikoleris@arm.com
67012724Snikos.nikoleris@arm.com    // We're leaving the cache, so pop cache->name() label
67112724Snikos.nikoleris@arm.com    pkt->popLabel();
67212724Snikos.nikoleris@arm.com
67312724Snikos.nikoleris@arm.com    if (done) {
67412724Snikos.nikoleris@arm.com        pkt->makeResponse();
67512724Snikos.nikoleris@arm.com    } else {
67612724Snikos.nikoleris@arm.com        // if it came as a request from the CPU side then make sure it
67712724Snikos.nikoleris@arm.com        // continues towards the memory side
67812724Snikos.nikoleris@arm.com        if (from_cpu_side) {
67912724Snikos.nikoleris@arm.com            memSidePort.sendFunctional(pkt);
68012724Snikos.nikoleris@arm.com        } else if (cpuSidePort.isSnooping()) {
68112724Snikos.nikoleris@arm.com            // if it came from the memory side, it must be a snoop request
68212724Snikos.nikoleris@arm.com            // and we should only forward it if we are forwarding snoops
68312724Snikos.nikoleris@arm.com            cpuSidePort.sendFunctionalSnoop(pkt);
68412724Snikos.nikoleris@arm.com        }
68512724Snikos.nikoleris@arm.com    }
68612724Snikos.nikoleris@arm.com}
68712724Snikos.nikoleris@arm.com
68812724Snikos.nikoleris@arm.com
68912724Snikos.nikoleris@arm.comvoid
69012724Snikos.nikoleris@arm.comBaseCache::cmpAndSwap(CacheBlk *blk, PacketPtr pkt)
69112724Snikos.nikoleris@arm.com{
69212724Snikos.nikoleris@arm.com    assert(pkt->isRequest());
69312724Snikos.nikoleris@arm.com
69412724Snikos.nikoleris@arm.com    uint64_t overwrite_val;
69512724Snikos.nikoleris@arm.com    bool overwrite_mem;
69612724Snikos.nikoleris@arm.com    uint64_t condition_val64;
69712724Snikos.nikoleris@arm.com    uint32_t condition_val32;
69812724Snikos.nikoleris@arm.com
69912724Snikos.nikoleris@arm.com    int offset = pkt->getOffset(blkSize);
70012724Snikos.nikoleris@arm.com    uint8_t *blk_data = blk->data + offset;
70112724Snikos.nikoleris@arm.com
70212724Snikos.nikoleris@arm.com    assert(sizeof(uint64_t) >= pkt->getSize());
70312724Snikos.nikoleris@arm.com
70412724Snikos.nikoleris@arm.com    overwrite_mem = true;
70512724Snikos.nikoleris@arm.com    // keep a copy of our possible write value, and copy what is at the
70612724Snikos.nikoleris@arm.com    // memory address into the packet
70712724Snikos.nikoleris@arm.com    pkt->writeData((uint8_t *)&overwrite_val);
70812724Snikos.nikoleris@arm.com    pkt->setData(blk_data);
70912724Snikos.nikoleris@arm.com
71012724Snikos.nikoleris@arm.com    if (pkt->req->isCondSwap()) {
71112724Snikos.nikoleris@arm.com        if (pkt->getSize() == sizeof(uint64_t)) {
71212724Snikos.nikoleris@arm.com            condition_val64 = pkt->req->getExtraData();
71312724Snikos.nikoleris@arm.com            overwrite_mem = !std::memcmp(&condition_val64, blk_data,
71412724Snikos.nikoleris@arm.com                                         sizeof(uint64_t));
71512724Snikos.nikoleris@arm.com        } else if (pkt->getSize() == sizeof(uint32_t)) {
71612724Snikos.nikoleris@arm.com            condition_val32 = (uint32_t)pkt->req->getExtraData();
71712724Snikos.nikoleris@arm.com            overwrite_mem = !std::memcmp(&condition_val32, blk_data,
71812724Snikos.nikoleris@arm.com                                         sizeof(uint32_t));
71912724Snikos.nikoleris@arm.com        } else
72012724Snikos.nikoleris@arm.com            panic("Invalid size for conditional read/write\n");
72112724Snikos.nikoleris@arm.com    }
72212724Snikos.nikoleris@arm.com
72312724Snikos.nikoleris@arm.com    if (overwrite_mem) {
72412724Snikos.nikoleris@arm.com        std::memcpy(blk_data, &overwrite_val, pkt->getSize());
72512724Snikos.nikoleris@arm.com        blk->status |= BlkDirty;
72612724Snikos.nikoleris@arm.com    }
72712724Snikos.nikoleris@arm.com}
72812724Snikos.nikoleris@arm.com
72912724Snikos.nikoleris@arm.comQueueEntry*
73012724Snikos.nikoleris@arm.comBaseCache::getNextQueueEntry()
73112724Snikos.nikoleris@arm.com{
73212724Snikos.nikoleris@arm.com    // Check both MSHR queue and write buffer for potential requests,
73312724Snikos.nikoleris@arm.com    // note that null does not mean there is no request, it could
73412724Snikos.nikoleris@arm.com    // simply be that it is not ready
73512724Snikos.nikoleris@arm.com    MSHR *miss_mshr  = mshrQueue.getNext();
73612724Snikos.nikoleris@arm.com    WriteQueueEntry *wq_entry = writeBuffer.getNext();
73712724Snikos.nikoleris@arm.com
73812724Snikos.nikoleris@arm.com    // If we got a write buffer request ready, first priority is a
73912724Snikos.nikoleris@arm.com    // full write buffer, otherwise we favour the miss requests
74012724Snikos.nikoleris@arm.com    if (wq_entry && (writeBuffer.isFull() || !miss_mshr)) {
74112724Snikos.nikoleris@arm.com        // need to search MSHR queue for conflicting earlier miss.
74212724Snikos.nikoleris@arm.com        MSHR *conflict_mshr =
74312724Snikos.nikoleris@arm.com            mshrQueue.findPending(wq_entry->blkAddr,
74412724Snikos.nikoleris@arm.com                                  wq_entry->isSecure);
74512724Snikos.nikoleris@arm.com
74612724Snikos.nikoleris@arm.com        if (conflict_mshr && conflict_mshr->order < wq_entry->order) {
74712724Snikos.nikoleris@arm.com            // Service misses in order until conflict is cleared.
74812724Snikos.nikoleris@arm.com            return conflict_mshr;
74912724Snikos.nikoleris@arm.com
75012724Snikos.nikoleris@arm.com            // @todo Note that we ignore the ready time of the conflict here
75112724Snikos.nikoleris@arm.com        }
75212724Snikos.nikoleris@arm.com
75312724Snikos.nikoleris@arm.com        // No conflicts; issue write
75412724Snikos.nikoleris@arm.com        return wq_entry;
75512724Snikos.nikoleris@arm.com    } else if (miss_mshr) {
75612724Snikos.nikoleris@arm.com        // need to check for conflicting earlier writeback
75712724Snikos.nikoleris@arm.com        WriteQueueEntry *conflict_mshr =
75812724Snikos.nikoleris@arm.com            writeBuffer.findPending(miss_mshr->blkAddr,
75912724Snikos.nikoleris@arm.com                                    miss_mshr->isSecure);
76012724Snikos.nikoleris@arm.com        if (conflict_mshr) {
76112724Snikos.nikoleris@arm.com            // not sure why we don't check order here... it was in the
76212724Snikos.nikoleris@arm.com            // original code but commented out.
76312724Snikos.nikoleris@arm.com
76412724Snikos.nikoleris@arm.com            // The only way this happens is if we are
76512724Snikos.nikoleris@arm.com            // doing a write and we didn't have permissions
76612724Snikos.nikoleris@arm.com            // then subsequently saw a writeback (owned got evicted)
76712724Snikos.nikoleris@arm.com            // We need to make sure to perform the writeback first
76812724Snikos.nikoleris@arm.com            // To preserve the dirty data, then we can issue the write
76912724Snikos.nikoleris@arm.com
77012724Snikos.nikoleris@arm.com            // should we return wq_entry here instead?  I.e. do we
77112724Snikos.nikoleris@arm.com            // have to flush writes in order?  I don't think so... not
77212724Snikos.nikoleris@arm.com            // for Alpha anyway.  Maybe for x86?
77312724Snikos.nikoleris@arm.com            return conflict_mshr;
77412724Snikos.nikoleris@arm.com
77512724Snikos.nikoleris@arm.com            // @todo Note that we ignore the ready time of the conflict here
77612724Snikos.nikoleris@arm.com        }
77712724Snikos.nikoleris@arm.com
77812724Snikos.nikoleris@arm.com        // No conflicts; issue read
77912724Snikos.nikoleris@arm.com        return miss_mshr;
78012724Snikos.nikoleris@arm.com    }
78112724Snikos.nikoleris@arm.com
78212724Snikos.nikoleris@arm.com    // fall through... no pending requests.  Try a prefetch.
78312724Snikos.nikoleris@arm.com    assert(!miss_mshr && !wq_entry);
78412724Snikos.nikoleris@arm.com    if (prefetcher && mshrQueue.canPrefetch()) {
78512724Snikos.nikoleris@arm.com        // If we have a miss queue slot, we can try a prefetch
78612724Snikos.nikoleris@arm.com        PacketPtr pkt = prefetcher->getPacket();
78712724Snikos.nikoleris@arm.com        if (pkt) {
78812724Snikos.nikoleris@arm.com            Addr pf_addr = pkt->getBlockAddr(blkSize);
78912724Snikos.nikoleris@arm.com            if (!tags->findBlock(pf_addr, pkt->isSecure()) &&
79012724Snikos.nikoleris@arm.com                !mshrQueue.findMatch(pf_addr, pkt->isSecure()) &&
79112724Snikos.nikoleris@arm.com                !writeBuffer.findMatch(pf_addr, pkt->isSecure())) {
79212724Snikos.nikoleris@arm.com                // Update statistic on number of prefetches issued
79312724Snikos.nikoleris@arm.com                // (hwpf_mshr_misses)
79412724Snikos.nikoleris@arm.com                assert(pkt->req->masterId() < system->maxMasters());
79512724Snikos.nikoleris@arm.com                mshr_misses[pkt->cmdToIndex()][pkt->req->masterId()]++;
79612724Snikos.nikoleris@arm.com
79712724Snikos.nikoleris@arm.com                // allocate an MSHR and return it, note
79812724Snikos.nikoleris@arm.com                // that we send the packet straight away, so do not
79912724Snikos.nikoleris@arm.com                // schedule the send
80012724Snikos.nikoleris@arm.com                return allocateMissBuffer(pkt, curTick(), false);
80112724Snikos.nikoleris@arm.com            } else {
80212724Snikos.nikoleris@arm.com                // free the request and packet
80312724Snikos.nikoleris@arm.com                delete pkt->req;
80412724Snikos.nikoleris@arm.com                delete pkt;
80512724Snikos.nikoleris@arm.com            }
80612724Snikos.nikoleris@arm.com        }
80712724Snikos.nikoleris@arm.com    }
80812724Snikos.nikoleris@arm.com
80912724Snikos.nikoleris@arm.com    return nullptr;
81012724Snikos.nikoleris@arm.com}
81112724Snikos.nikoleris@arm.com
81212724Snikos.nikoleris@arm.comvoid
81312724Snikos.nikoleris@arm.comBaseCache::satisfyRequest(PacketPtr pkt, CacheBlk *blk, bool, bool)
81412724Snikos.nikoleris@arm.com{
81512724Snikos.nikoleris@arm.com    assert(pkt->isRequest());
81612724Snikos.nikoleris@arm.com
81712724Snikos.nikoleris@arm.com    assert(blk && blk->isValid());
81812724Snikos.nikoleris@arm.com    // Occasionally this is not true... if we are a lower-level cache
81912724Snikos.nikoleris@arm.com    // satisfying a string of Read and ReadEx requests from
82012724Snikos.nikoleris@arm.com    // upper-level caches, a Read will mark the block as shared but we
82112724Snikos.nikoleris@arm.com    // can satisfy a following ReadEx anyway since we can rely on the
82212724Snikos.nikoleris@arm.com    // Read requester(s) to have buffered the ReadEx snoop and to
82312724Snikos.nikoleris@arm.com    // invalidate their blocks after receiving them.
82412724Snikos.nikoleris@arm.com    // assert(!pkt->needsWritable() || blk->isWritable());
82512724Snikos.nikoleris@arm.com    assert(pkt->getOffset(blkSize) + pkt->getSize() <= blkSize);
82612724Snikos.nikoleris@arm.com
82712724Snikos.nikoleris@arm.com    // Check RMW operations first since both isRead() and
82812724Snikos.nikoleris@arm.com    // isWrite() will be true for them
82912724Snikos.nikoleris@arm.com    if (pkt->cmd == MemCmd::SwapReq) {
83012724Snikos.nikoleris@arm.com        cmpAndSwap(blk, pkt);
83112724Snikos.nikoleris@arm.com    } else if (pkt->isWrite()) {
83212724Snikos.nikoleris@arm.com        // we have the block in a writable state and can go ahead,
83312724Snikos.nikoleris@arm.com        // note that the line may be also be considered writable in
83412724Snikos.nikoleris@arm.com        // downstream caches along the path to memory, but always
83512724Snikos.nikoleris@arm.com        // Exclusive, and never Modified
83612724Snikos.nikoleris@arm.com        assert(blk->isWritable());
83712724Snikos.nikoleris@arm.com        // Write or WriteLine at the first cache with block in writable state
83812724Snikos.nikoleris@arm.com        if (blk->checkWrite(pkt)) {
83912724Snikos.nikoleris@arm.com            pkt->writeDataToBlock(blk->data, blkSize);
84012724Snikos.nikoleris@arm.com        }
84112724Snikos.nikoleris@arm.com        // Always mark the line as dirty (and thus transition to the
84212724Snikos.nikoleris@arm.com        // Modified state) even if we are a failed StoreCond so we
84312724Snikos.nikoleris@arm.com        // supply data to any snoops that have appended themselves to
84412724Snikos.nikoleris@arm.com        // this cache before knowing the store will fail.
84512724Snikos.nikoleris@arm.com        blk->status |= BlkDirty;
84612724Snikos.nikoleris@arm.com        DPRINTF(CacheVerbose, "%s for %s (write)\n", __func__, pkt->print());
84712724Snikos.nikoleris@arm.com    } else if (pkt->isRead()) {
84812724Snikos.nikoleris@arm.com        if (pkt->isLLSC()) {
84912724Snikos.nikoleris@arm.com            blk->trackLoadLocked(pkt);
85012724Snikos.nikoleris@arm.com        }
85112724Snikos.nikoleris@arm.com
85212724Snikos.nikoleris@arm.com        // all read responses have a data payload
85312724Snikos.nikoleris@arm.com        assert(pkt->hasRespData());
85412724Snikos.nikoleris@arm.com        pkt->setDataFromBlock(blk->data, blkSize);
85512724Snikos.nikoleris@arm.com    } else if (pkt->isUpgrade()) {
85612724Snikos.nikoleris@arm.com        // sanity check
85712724Snikos.nikoleris@arm.com        assert(!pkt->hasSharers());
85812724Snikos.nikoleris@arm.com
85912724Snikos.nikoleris@arm.com        if (blk->isDirty()) {
86012724Snikos.nikoleris@arm.com            // we were in the Owned state, and a cache above us that
86112724Snikos.nikoleris@arm.com            // has the line in Shared state needs to be made aware
86212724Snikos.nikoleris@arm.com            // that the data it already has is in fact dirty
86312724Snikos.nikoleris@arm.com            pkt->setCacheResponding();
86412724Snikos.nikoleris@arm.com            blk->status &= ~BlkDirty;
86512724Snikos.nikoleris@arm.com        }
86612724Snikos.nikoleris@arm.com    } else {
86712724Snikos.nikoleris@arm.com        assert(pkt->isInvalidate());
86812724Snikos.nikoleris@arm.com        invalidateBlock(blk);
86912724Snikos.nikoleris@arm.com        DPRINTF(CacheVerbose, "%s for %s (invalidation)\n", __func__,
87012724Snikos.nikoleris@arm.com                pkt->print());
87112724Snikos.nikoleris@arm.com    }
87212724Snikos.nikoleris@arm.com}
87312724Snikos.nikoleris@arm.com
87412724Snikos.nikoleris@arm.com/////////////////////////////////////////////////////
87512724Snikos.nikoleris@arm.com//
87612724Snikos.nikoleris@arm.com// Access path: requests coming in from the CPU side
87712724Snikos.nikoleris@arm.com//
87812724Snikos.nikoleris@arm.com/////////////////////////////////////////////////////
87912724Snikos.nikoleris@arm.com
88012724Snikos.nikoleris@arm.combool
88112724Snikos.nikoleris@arm.comBaseCache::access(PacketPtr pkt, CacheBlk *&blk, Cycles &lat,
88212724Snikos.nikoleris@arm.com                  PacketList &writebacks)
88312724Snikos.nikoleris@arm.com{
88412724Snikos.nikoleris@arm.com    // sanity check
88512724Snikos.nikoleris@arm.com    assert(pkt->isRequest());
88612724Snikos.nikoleris@arm.com
88712724Snikos.nikoleris@arm.com    chatty_assert(!(isReadOnly && pkt->isWrite()),
88812724Snikos.nikoleris@arm.com                  "Should never see a write in a read-only cache %s\n",
88912724Snikos.nikoleris@arm.com                  name());
89012724Snikos.nikoleris@arm.com
89112724Snikos.nikoleris@arm.com    // Here lat is the value passed as parameter to accessBlock() function
89212724Snikos.nikoleris@arm.com    // that can modify its value.
89312724Snikos.nikoleris@arm.com    blk = tags->accessBlock(pkt->getAddr(), pkt->isSecure(), lat);
89412724Snikos.nikoleris@arm.com
89512724Snikos.nikoleris@arm.com    DPRINTF(Cache, "%s for %s %s\n", __func__, pkt->print(),
89612724Snikos.nikoleris@arm.com            blk ? "hit " + blk->print() : "miss");
89712724Snikos.nikoleris@arm.com
89812724Snikos.nikoleris@arm.com    if (pkt->req->isCacheMaintenance()) {
89912724Snikos.nikoleris@arm.com        // A cache maintenance operation is always forwarded to the
90012724Snikos.nikoleris@arm.com        // memory below even if the block is found in dirty state.
90112724Snikos.nikoleris@arm.com
90212724Snikos.nikoleris@arm.com        // We defer any changes to the state of the block until we
90312724Snikos.nikoleris@arm.com        // create and mark as in service the mshr for the downstream
90412724Snikos.nikoleris@arm.com        // packet.
90512724Snikos.nikoleris@arm.com        return false;
90612724Snikos.nikoleris@arm.com    }
90712724Snikos.nikoleris@arm.com
90812724Snikos.nikoleris@arm.com    if (pkt->isEviction()) {
90912724Snikos.nikoleris@arm.com        // We check for presence of block in above caches before issuing
91012724Snikos.nikoleris@arm.com        // Writeback or CleanEvict to write buffer. Therefore the only
91112724Snikos.nikoleris@arm.com        // possible cases can be of a CleanEvict packet coming from above
91212724Snikos.nikoleris@arm.com        // encountering a Writeback generated in this cache peer cache and
91312724Snikos.nikoleris@arm.com        // waiting in the write buffer. Cases of upper level peer caches
91412724Snikos.nikoleris@arm.com        // generating CleanEvict and Writeback or simply CleanEvict and
91512724Snikos.nikoleris@arm.com        // CleanEvict almost simultaneously will be caught by snoops sent out
91612724Snikos.nikoleris@arm.com        // by crossbar.
91712724Snikos.nikoleris@arm.com        WriteQueueEntry *wb_entry = writeBuffer.findMatch(pkt->getAddr(),
91812724Snikos.nikoleris@arm.com                                                          pkt->isSecure());
91912724Snikos.nikoleris@arm.com        if (wb_entry) {
92012724Snikos.nikoleris@arm.com            assert(wb_entry->getNumTargets() == 1);
92112724Snikos.nikoleris@arm.com            PacketPtr wbPkt = wb_entry->getTarget()->pkt;
92212724Snikos.nikoleris@arm.com            assert(wbPkt->isWriteback());
92312724Snikos.nikoleris@arm.com
92412724Snikos.nikoleris@arm.com            if (pkt->isCleanEviction()) {
92512724Snikos.nikoleris@arm.com                // The CleanEvict and WritebackClean snoops into other
92612724Snikos.nikoleris@arm.com                // peer caches of the same level while traversing the
92712724Snikos.nikoleris@arm.com                // crossbar. If a copy of the block is found, the
92812724Snikos.nikoleris@arm.com                // packet is deleted in the crossbar. Hence, none of
92912724Snikos.nikoleris@arm.com                // the other upper level caches connected to this
93012724Snikos.nikoleris@arm.com                // cache have the block, so we can clear the
93112724Snikos.nikoleris@arm.com                // BLOCK_CACHED flag in the Writeback if set and
93212724Snikos.nikoleris@arm.com                // discard the CleanEvict by returning true.
93312724Snikos.nikoleris@arm.com                wbPkt->clearBlockCached();
93412724Snikos.nikoleris@arm.com                return true;
93512724Snikos.nikoleris@arm.com            } else {
93612724Snikos.nikoleris@arm.com                assert(pkt->cmd == MemCmd::WritebackDirty);
93712724Snikos.nikoleris@arm.com                // Dirty writeback from above trumps our clean
93812724Snikos.nikoleris@arm.com                // writeback... discard here
93912724Snikos.nikoleris@arm.com                // Note: markInService will remove entry from writeback buffer.
94012724Snikos.nikoleris@arm.com                markInService(wb_entry);
94112724Snikos.nikoleris@arm.com                delete wbPkt;
94212724Snikos.nikoleris@arm.com            }
94312724Snikos.nikoleris@arm.com        }
94412724Snikos.nikoleris@arm.com    }
94512724Snikos.nikoleris@arm.com
94612724Snikos.nikoleris@arm.com    // Writeback handling is special case.  We can write the block into
94712724Snikos.nikoleris@arm.com    // the cache without having a writeable copy (or any copy at all).
94812724Snikos.nikoleris@arm.com    if (pkt->isWriteback()) {
94912724Snikos.nikoleris@arm.com        assert(blkSize == pkt->getSize());
95012724Snikos.nikoleris@arm.com
95112724Snikos.nikoleris@arm.com        // we could get a clean writeback while we are having
95212724Snikos.nikoleris@arm.com        // outstanding accesses to a block, do the simple thing for
95312724Snikos.nikoleris@arm.com        // now and drop the clean writeback so that we do not upset
95412724Snikos.nikoleris@arm.com        // any ordering/decisions about ownership already taken
95512724Snikos.nikoleris@arm.com        if (pkt->cmd == MemCmd::WritebackClean &&
95612724Snikos.nikoleris@arm.com            mshrQueue.findMatch(pkt->getAddr(), pkt->isSecure())) {
95712724Snikos.nikoleris@arm.com            DPRINTF(Cache, "Clean writeback %#llx to block with MSHR, "
95812724Snikos.nikoleris@arm.com                    "dropping\n", pkt->getAddr());
95912724Snikos.nikoleris@arm.com            return true;
96012724Snikos.nikoleris@arm.com        }
96112724Snikos.nikoleris@arm.com
96212724Snikos.nikoleris@arm.com        if (!blk) {
96312724Snikos.nikoleris@arm.com            // need to do a replacement
96412724Snikos.nikoleris@arm.com            blk = allocateBlock(pkt->getAddr(), pkt->isSecure(), writebacks);
96512724Snikos.nikoleris@arm.com            if (!blk) {
96612724Snikos.nikoleris@arm.com                // no replaceable block available: give up, fwd to next level.
96712724Snikos.nikoleris@arm.com                incMissCount(pkt);
96812724Snikos.nikoleris@arm.com                return false;
96912724Snikos.nikoleris@arm.com            }
97012724Snikos.nikoleris@arm.com            tags->insertBlock(pkt, blk);
97112724Snikos.nikoleris@arm.com
97212724Snikos.nikoleris@arm.com            blk->status |= (BlkValid | BlkReadable);
97312724Snikos.nikoleris@arm.com        }
97412724Snikos.nikoleris@arm.com        // only mark the block dirty if we got a writeback command,
97512724Snikos.nikoleris@arm.com        // and leave it as is for a clean writeback
97612724Snikos.nikoleris@arm.com        if (pkt->cmd == MemCmd::WritebackDirty) {
97712724Snikos.nikoleris@arm.com            // TODO: the coherent cache can assert(!blk->isDirty());
97812724Snikos.nikoleris@arm.com            blk->status |= BlkDirty;
97912724Snikos.nikoleris@arm.com        }
98012724Snikos.nikoleris@arm.com        // if the packet does not have sharers, it is passing
98112724Snikos.nikoleris@arm.com        // writable, and we got the writeback in Modified or Exclusive
98212724Snikos.nikoleris@arm.com        // state, if not we are in the Owned or Shared state
98312724Snikos.nikoleris@arm.com        if (!pkt->hasSharers()) {
98412724Snikos.nikoleris@arm.com            blk->status |= BlkWritable;
98512724Snikos.nikoleris@arm.com        }
98612724Snikos.nikoleris@arm.com        // nothing else to do; writeback doesn't expect response
98712724Snikos.nikoleris@arm.com        assert(!pkt->needsResponse());
98812724Snikos.nikoleris@arm.com        pkt->writeDataToBlock(blk->data, blkSize);
98912724Snikos.nikoleris@arm.com        DPRINTF(Cache, "%s new state is %s\n", __func__, blk->print());
99012724Snikos.nikoleris@arm.com        incHitCount(pkt);
99112724Snikos.nikoleris@arm.com        // populate the time when the block will be ready to access.
99212724Snikos.nikoleris@arm.com        blk->whenReady = clockEdge(fillLatency) + pkt->headerDelay +
99312724Snikos.nikoleris@arm.com            pkt->payloadDelay;
99412724Snikos.nikoleris@arm.com        return true;
99512724Snikos.nikoleris@arm.com    } else if (pkt->cmd == MemCmd::CleanEvict) {
99612724Snikos.nikoleris@arm.com        if (blk) {
99712724Snikos.nikoleris@arm.com            // Found the block in the tags, need to stop CleanEvict from
99812724Snikos.nikoleris@arm.com            // propagating further down the hierarchy. Returning true will
99912724Snikos.nikoleris@arm.com            // treat the CleanEvict like a satisfied write request and delete
100012724Snikos.nikoleris@arm.com            // it.
100112724Snikos.nikoleris@arm.com            return true;
100212724Snikos.nikoleris@arm.com        }
100312724Snikos.nikoleris@arm.com        // We didn't find the block here, propagate the CleanEvict further
100412724Snikos.nikoleris@arm.com        // down the memory hierarchy. Returning false will treat the CleanEvict
100512724Snikos.nikoleris@arm.com        // like a Writeback which could not find a replaceable block so has to
100612724Snikos.nikoleris@arm.com        // go to next level.
100712724Snikos.nikoleris@arm.com        return false;
100812724Snikos.nikoleris@arm.com    } else if (pkt->cmd == MemCmd::WriteClean) {
100912724Snikos.nikoleris@arm.com        // WriteClean handling is a special case. We can allocate a
101012724Snikos.nikoleris@arm.com        // block directly if it doesn't exist and we can update the
101112724Snikos.nikoleris@arm.com        // block immediately. The WriteClean transfers the ownership
101212724Snikos.nikoleris@arm.com        // of the block as well.
101312724Snikos.nikoleris@arm.com        assert(blkSize == pkt->getSize());
101412724Snikos.nikoleris@arm.com
101512724Snikos.nikoleris@arm.com        if (!blk) {
101612724Snikos.nikoleris@arm.com            if (pkt->writeThrough()) {
101712724Snikos.nikoleris@arm.com                // if this is a write through packet, we don't try to
101812724Snikos.nikoleris@arm.com                // allocate if the block is not present
101912724Snikos.nikoleris@arm.com                return false;
102012724Snikos.nikoleris@arm.com            } else {
102112724Snikos.nikoleris@arm.com                // a writeback that misses needs to allocate a new block
102212724Snikos.nikoleris@arm.com                blk = allocateBlock(pkt->getAddr(), pkt->isSecure(),
102312724Snikos.nikoleris@arm.com                                    writebacks);
102412724Snikos.nikoleris@arm.com                if (!blk) {
102512724Snikos.nikoleris@arm.com                    // no replaceable block available: give up, fwd to
102612724Snikos.nikoleris@arm.com                    // next level.
102712724Snikos.nikoleris@arm.com                    incMissCount(pkt);
102812724Snikos.nikoleris@arm.com                    return false;
102912724Snikos.nikoleris@arm.com                }
103012724Snikos.nikoleris@arm.com                tags->insertBlock(pkt, blk);
103112724Snikos.nikoleris@arm.com
103212724Snikos.nikoleris@arm.com                blk->status |= (BlkValid | BlkReadable);
103312724Snikos.nikoleris@arm.com            }
103412724Snikos.nikoleris@arm.com        }
103512724Snikos.nikoleris@arm.com
103612724Snikos.nikoleris@arm.com        // at this point either this is a writeback or a write-through
103712724Snikos.nikoleris@arm.com        // write clean operation and the block is already in this
103812724Snikos.nikoleris@arm.com        // cache, we need to update the data and the block flags
103912724Snikos.nikoleris@arm.com        assert(blk);
104012724Snikos.nikoleris@arm.com        // TODO: the coherent cache can assert(!blk->isDirty());
104112724Snikos.nikoleris@arm.com        if (!pkt->writeThrough()) {
104212724Snikos.nikoleris@arm.com            blk->status |= BlkDirty;
104312724Snikos.nikoleris@arm.com        }
104412724Snikos.nikoleris@arm.com        // nothing else to do; writeback doesn't expect response
104512724Snikos.nikoleris@arm.com        assert(!pkt->needsResponse());
104612724Snikos.nikoleris@arm.com        pkt->writeDataToBlock(blk->data, blkSize);
104712724Snikos.nikoleris@arm.com        DPRINTF(Cache, "%s new state is %s\n", __func__, blk->print());
104812724Snikos.nikoleris@arm.com
104912724Snikos.nikoleris@arm.com        incHitCount(pkt);
105012724Snikos.nikoleris@arm.com        // populate the time when the block will be ready to access.
105112724Snikos.nikoleris@arm.com        blk->whenReady = clockEdge(fillLatency) + pkt->headerDelay +
105212724Snikos.nikoleris@arm.com            pkt->payloadDelay;
105312724Snikos.nikoleris@arm.com        // if this a write-through packet it will be sent to cache
105412724Snikos.nikoleris@arm.com        // below
105512724Snikos.nikoleris@arm.com        return !pkt->writeThrough();
105612724Snikos.nikoleris@arm.com    } else if (blk && (pkt->needsWritable() ? blk->isWritable() :
105712724Snikos.nikoleris@arm.com                       blk->isReadable())) {
105812724Snikos.nikoleris@arm.com        // OK to satisfy access
105912724Snikos.nikoleris@arm.com        incHitCount(pkt);
106012724Snikos.nikoleris@arm.com        satisfyRequest(pkt, blk);
106112724Snikos.nikoleris@arm.com        maintainClusivity(pkt->fromCache(), blk);
106212724Snikos.nikoleris@arm.com
106312724Snikos.nikoleris@arm.com        return true;
106412724Snikos.nikoleris@arm.com    }
106512724Snikos.nikoleris@arm.com
106612724Snikos.nikoleris@arm.com    // Can't satisfy access normally... either no block (blk == nullptr)
106712724Snikos.nikoleris@arm.com    // or have block but need writable
106812724Snikos.nikoleris@arm.com
106912724Snikos.nikoleris@arm.com    incMissCount(pkt);
107012724Snikos.nikoleris@arm.com
107112724Snikos.nikoleris@arm.com    if (!blk && pkt->isLLSC() && pkt->isWrite()) {
107212724Snikos.nikoleris@arm.com        // complete miss on store conditional... just give up now
107312724Snikos.nikoleris@arm.com        pkt->req->setExtraData(0);
107412724Snikos.nikoleris@arm.com        return true;
107512724Snikos.nikoleris@arm.com    }
107612724Snikos.nikoleris@arm.com
107712724Snikos.nikoleris@arm.com    return false;
107812724Snikos.nikoleris@arm.com}
107912724Snikos.nikoleris@arm.com
108012724Snikos.nikoleris@arm.comvoid
108112724Snikos.nikoleris@arm.comBaseCache::maintainClusivity(bool from_cache, CacheBlk *blk)
108212724Snikos.nikoleris@arm.com{
108312724Snikos.nikoleris@arm.com    if (from_cache && blk && blk->isValid() && !blk->isDirty() &&
108412724Snikos.nikoleris@arm.com        clusivity == Enums::mostly_excl) {
108512724Snikos.nikoleris@arm.com        // if we have responded to a cache, and our block is still
108612724Snikos.nikoleris@arm.com        // valid, but not dirty, and this cache is mostly exclusive
108712724Snikos.nikoleris@arm.com        // with respect to the cache above, drop the block
108812724Snikos.nikoleris@arm.com        invalidateBlock(blk);
108912724Snikos.nikoleris@arm.com    }
109012724Snikos.nikoleris@arm.com}
109112724Snikos.nikoleris@arm.com
109212724Snikos.nikoleris@arm.comCacheBlk*
109312724Snikos.nikoleris@arm.comBaseCache::handleFill(PacketPtr pkt, CacheBlk *blk, PacketList &writebacks,
109412724Snikos.nikoleris@arm.com                      bool allocate)
109512724Snikos.nikoleris@arm.com{
109612724Snikos.nikoleris@arm.com    assert(pkt->isResponse() || pkt->cmd == MemCmd::WriteLineReq);
109712724Snikos.nikoleris@arm.com    Addr addr = pkt->getAddr();
109812724Snikos.nikoleris@arm.com    bool is_secure = pkt->isSecure();
109912724Snikos.nikoleris@arm.com#if TRACING_ON
110012724Snikos.nikoleris@arm.com    CacheBlk::State old_state = blk ? blk->status : 0;
110112724Snikos.nikoleris@arm.com#endif
110212724Snikos.nikoleris@arm.com
110312724Snikos.nikoleris@arm.com    // When handling a fill, we should have no writes to this line.
110412724Snikos.nikoleris@arm.com    assert(addr == pkt->getBlockAddr(blkSize));
110512724Snikos.nikoleris@arm.com    assert(!writeBuffer.findMatch(addr, is_secure));
110612724Snikos.nikoleris@arm.com
110712724Snikos.nikoleris@arm.com    if (!blk) {
110812724Snikos.nikoleris@arm.com        // better have read new data...
110912724Snikos.nikoleris@arm.com        assert(pkt->hasData());
111012724Snikos.nikoleris@arm.com
111112724Snikos.nikoleris@arm.com        // only read responses and write-line requests have data;
111212724Snikos.nikoleris@arm.com        // note that we don't write the data here for write-line - that
111312724Snikos.nikoleris@arm.com        // happens in the subsequent call to satisfyRequest
111412724Snikos.nikoleris@arm.com        assert(pkt->isRead() || pkt->cmd == MemCmd::WriteLineReq);
111512724Snikos.nikoleris@arm.com
111612724Snikos.nikoleris@arm.com        // need to do a replacement if allocating, otherwise we stick
111712724Snikos.nikoleris@arm.com        // with the temporary storage
111812724Snikos.nikoleris@arm.com        blk = allocate ? allocateBlock(addr, is_secure, writebacks) : nullptr;
111912724Snikos.nikoleris@arm.com
112012724Snikos.nikoleris@arm.com        if (!blk) {
112112724Snikos.nikoleris@arm.com            // No replaceable block or a mostly exclusive
112212724Snikos.nikoleris@arm.com            // cache... just use temporary storage to complete the
112312724Snikos.nikoleris@arm.com            // current request and then get rid of it
112412724Snikos.nikoleris@arm.com            assert(!tempBlock->isValid());
112512724Snikos.nikoleris@arm.com            blk = tempBlock;
112612724Snikos.nikoleris@arm.com            tempBlock->set = tags->extractSet(addr);
112712724Snikos.nikoleris@arm.com            tempBlock->tag = tags->extractTag(addr);
112812724Snikos.nikoleris@arm.com            DPRINTF(Cache, "using temp block for %#llx (%s)\n", addr,
112912724Snikos.nikoleris@arm.com                    is_secure ? "s" : "ns");
113012724Snikos.nikoleris@arm.com        } else {
113112724Snikos.nikoleris@arm.com            tags->insertBlock(pkt, blk);
113212724Snikos.nikoleris@arm.com        }
113312724Snikos.nikoleris@arm.com
113412724Snikos.nikoleris@arm.com        // we should never be overwriting a valid block
113512724Snikos.nikoleris@arm.com        assert(!blk->isValid());
113612724Snikos.nikoleris@arm.com    } else {
113712724Snikos.nikoleris@arm.com        // existing block... probably an upgrade
113812724Snikos.nikoleris@arm.com        assert(blk->tag == tags->extractTag(addr));
113912729Sodanrc@yahoo.com.br        assert(blk->isSecure() == is_secure);
114012724Snikos.nikoleris@arm.com        // either we're getting new data or the block should already be valid
114112724Snikos.nikoleris@arm.com        assert(pkt->hasData() || blk->isValid());
114212724Snikos.nikoleris@arm.com        // don't clear block status... if block is already dirty we
114312724Snikos.nikoleris@arm.com        // don't want to lose that
114412724Snikos.nikoleris@arm.com    }
114512724Snikos.nikoleris@arm.com
114612724Snikos.nikoleris@arm.com    blk->status |= BlkValid | BlkReadable;
114712724Snikos.nikoleris@arm.com
114812724Snikos.nikoleris@arm.com    // sanity check for whole-line writes, which should always be
114912724Snikos.nikoleris@arm.com    // marked as writable as part of the fill, and then later marked
115012724Snikos.nikoleris@arm.com    // dirty as part of satisfyRequest
115112724Snikos.nikoleris@arm.com    if (pkt->cmd == MemCmd::WriteLineReq) {
115212724Snikos.nikoleris@arm.com        assert(!pkt->hasSharers());
115312724Snikos.nikoleris@arm.com    }
115412724Snikos.nikoleris@arm.com
115512724Snikos.nikoleris@arm.com    // here we deal with setting the appropriate state of the line,
115612724Snikos.nikoleris@arm.com    // and we start by looking at the hasSharers flag, and ignore the
115712724Snikos.nikoleris@arm.com    // cacheResponding flag (normally signalling dirty data) if the
115812724Snikos.nikoleris@arm.com    // packet has sharers, thus the line is never allocated as Owned
115912724Snikos.nikoleris@arm.com    // (dirty but not writable), and always ends up being either
116012724Snikos.nikoleris@arm.com    // Shared, Exclusive or Modified, see Packet::setCacheResponding
116112724Snikos.nikoleris@arm.com    // for more details
116212724Snikos.nikoleris@arm.com    if (!pkt->hasSharers()) {
116312724Snikos.nikoleris@arm.com        // we could get a writable line from memory (rather than a
116412724Snikos.nikoleris@arm.com        // cache) even in a read-only cache, note that we set this bit
116512724Snikos.nikoleris@arm.com        // even for a read-only cache, possibly revisit this decision
116612724Snikos.nikoleris@arm.com        blk->status |= BlkWritable;
116712724Snikos.nikoleris@arm.com
116812724Snikos.nikoleris@arm.com        // check if we got this via cache-to-cache transfer (i.e., from a
116912724Snikos.nikoleris@arm.com        // cache that had the block in Modified or Owned state)
117012724Snikos.nikoleris@arm.com        if (pkt->cacheResponding()) {
117112724Snikos.nikoleris@arm.com            // we got the block in Modified state, and invalidated the
117212724Snikos.nikoleris@arm.com            // owners copy
117312724Snikos.nikoleris@arm.com            blk->status |= BlkDirty;
117412724Snikos.nikoleris@arm.com
117512724Snikos.nikoleris@arm.com            chatty_assert(!isReadOnly, "Should never see dirty snoop response "
117612724Snikos.nikoleris@arm.com                          "in read-only cache %s\n", name());
117712724Snikos.nikoleris@arm.com        }
117812724Snikos.nikoleris@arm.com    }
117912724Snikos.nikoleris@arm.com
118012724Snikos.nikoleris@arm.com    DPRINTF(Cache, "Block addr %#llx (%s) moving from state %x to %s\n",
118112724Snikos.nikoleris@arm.com            addr, is_secure ? "s" : "ns", old_state, blk->print());
118212724Snikos.nikoleris@arm.com
118312724Snikos.nikoleris@arm.com    // if we got new data, copy it in (checking for a read response
118412724Snikos.nikoleris@arm.com    // and a response that has data is the same in the end)
118512724Snikos.nikoleris@arm.com    if (pkt->isRead()) {
118612724Snikos.nikoleris@arm.com        // sanity checks
118712724Snikos.nikoleris@arm.com        assert(pkt->hasData());
118812724Snikos.nikoleris@arm.com        assert(pkt->getSize() == blkSize);
118912724Snikos.nikoleris@arm.com
119012724Snikos.nikoleris@arm.com        pkt->writeDataToBlock(blk->data, blkSize);
119112724Snikos.nikoleris@arm.com    }
119212724Snikos.nikoleris@arm.com    // We pay for fillLatency here.
119312724Snikos.nikoleris@arm.com    blk->whenReady = clockEdge() + fillLatency * clockPeriod() +
119412724Snikos.nikoleris@arm.com        pkt->payloadDelay;
119512724Snikos.nikoleris@arm.com
119612724Snikos.nikoleris@arm.com    return blk;
119712724Snikos.nikoleris@arm.com}
119812724Snikos.nikoleris@arm.com
119912724Snikos.nikoleris@arm.comCacheBlk*
120012724Snikos.nikoleris@arm.comBaseCache::allocateBlock(Addr addr, bool is_secure, PacketList &writebacks)
120112724Snikos.nikoleris@arm.com{
120212724Snikos.nikoleris@arm.com    // Find replacement victim
120312724Snikos.nikoleris@arm.com    CacheBlk *blk = tags->findVictim(addr);
120412724Snikos.nikoleris@arm.com
120512724Snikos.nikoleris@arm.com    // It is valid to return nullptr if there is no victim
120612724Snikos.nikoleris@arm.com    if (!blk)
120712724Snikos.nikoleris@arm.com        return nullptr;
120812724Snikos.nikoleris@arm.com
120912724Snikos.nikoleris@arm.com    if (blk->isValid()) {
121012724Snikos.nikoleris@arm.com        Addr repl_addr = tags->regenerateBlkAddr(blk);
121112724Snikos.nikoleris@arm.com        MSHR *repl_mshr = mshrQueue.findMatch(repl_addr, blk->isSecure());
121212724Snikos.nikoleris@arm.com        if (repl_mshr) {
121312724Snikos.nikoleris@arm.com            // must be an outstanding upgrade or clean request
121412724Snikos.nikoleris@arm.com            // on a block we're about to replace...
121512724Snikos.nikoleris@arm.com            assert((!blk->isWritable() && repl_mshr->needsWritable()) ||
121612724Snikos.nikoleris@arm.com                   repl_mshr->isCleaning());
121712724Snikos.nikoleris@arm.com            // too hard to replace block with transient state
121812724Snikos.nikoleris@arm.com            // allocation failed, block not inserted
121912724Snikos.nikoleris@arm.com            return nullptr;
122012724Snikos.nikoleris@arm.com        } else {
122112724Snikos.nikoleris@arm.com            DPRINTF(Cache, "replacement: replacing %#llx (%s) with %#llx "
122212724Snikos.nikoleris@arm.com                    "(%s): %s\n", repl_addr, blk->isSecure() ? "s" : "ns",
122312724Snikos.nikoleris@arm.com                    addr, is_secure ? "s" : "ns",
122412724Snikos.nikoleris@arm.com                    blk->isDirty() ? "writeback" : "clean");
122512724Snikos.nikoleris@arm.com
122612724Snikos.nikoleris@arm.com            if (blk->wasPrefetched()) {
122712724Snikos.nikoleris@arm.com                unusedPrefetches++;
122812724Snikos.nikoleris@arm.com            }
122912724Snikos.nikoleris@arm.com            evictBlock(blk, writebacks);
123012724Snikos.nikoleris@arm.com            replacements++;
123112724Snikos.nikoleris@arm.com        }
123212724Snikos.nikoleris@arm.com    }
123312724Snikos.nikoleris@arm.com
123412724Snikos.nikoleris@arm.com    return blk;
123512724Snikos.nikoleris@arm.com}
123612724Snikos.nikoleris@arm.com
123712724Snikos.nikoleris@arm.comvoid
123812724Snikos.nikoleris@arm.comBaseCache::invalidateBlock(CacheBlk *blk)
123912724Snikos.nikoleris@arm.com{
124012724Snikos.nikoleris@arm.com    if (blk != tempBlock)
124112724Snikos.nikoleris@arm.com        tags->invalidate(blk);
124212724Snikos.nikoleris@arm.com    blk->invalidate();
124312724Snikos.nikoleris@arm.com}
124412724Snikos.nikoleris@arm.com
124512724Snikos.nikoleris@arm.comPacketPtr
124612724Snikos.nikoleris@arm.comBaseCache::writebackBlk(CacheBlk *blk)
124712724Snikos.nikoleris@arm.com{
124812724Snikos.nikoleris@arm.com    chatty_assert(!isReadOnly || writebackClean,
124912724Snikos.nikoleris@arm.com                  "Writeback from read-only cache");
125012724Snikos.nikoleris@arm.com    assert(blk && blk->isValid() && (blk->isDirty() || writebackClean));
125112724Snikos.nikoleris@arm.com
125212724Snikos.nikoleris@arm.com    writebacks[Request::wbMasterId]++;
125312724Snikos.nikoleris@arm.com
125412724Snikos.nikoleris@arm.com    Request *req = new Request(tags->regenerateBlkAddr(blk), blkSize, 0,
125512724Snikos.nikoleris@arm.com                               Request::wbMasterId);
125612724Snikos.nikoleris@arm.com    if (blk->isSecure())
125712724Snikos.nikoleris@arm.com        req->setFlags(Request::SECURE);
125812724Snikos.nikoleris@arm.com
125912724Snikos.nikoleris@arm.com    req->taskId(blk->task_id);
126012724Snikos.nikoleris@arm.com
126112724Snikos.nikoleris@arm.com    PacketPtr pkt =
126212724Snikos.nikoleris@arm.com        new Packet(req, blk->isDirty() ?
126312724Snikos.nikoleris@arm.com                   MemCmd::WritebackDirty : MemCmd::WritebackClean);
126412724Snikos.nikoleris@arm.com
126512724Snikos.nikoleris@arm.com    DPRINTF(Cache, "Create Writeback %s writable: %d, dirty: %d\n",
126612724Snikos.nikoleris@arm.com            pkt->print(), blk->isWritable(), blk->isDirty());
126712724Snikos.nikoleris@arm.com
126812724Snikos.nikoleris@arm.com    if (blk->isWritable()) {
126912724Snikos.nikoleris@arm.com        // not asserting shared means we pass the block in modified
127012724Snikos.nikoleris@arm.com        // state, mark our own block non-writeable
127112724Snikos.nikoleris@arm.com        blk->status &= ~BlkWritable;
127212724Snikos.nikoleris@arm.com    } else {
127312724Snikos.nikoleris@arm.com        // we are in the Owned state, tell the receiver
127412724Snikos.nikoleris@arm.com        pkt->setHasSharers();
127512724Snikos.nikoleris@arm.com    }
127612724Snikos.nikoleris@arm.com
127712724Snikos.nikoleris@arm.com    // make sure the block is not marked dirty
127812724Snikos.nikoleris@arm.com    blk->status &= ~BlkDirty;
127912724Snikos.nikoleris@arm.com
128012724Snikos.nikoleris@arm.com    pkt->allocate();
128112724Snikos.nikoleris@arm.com    pkt->setDataFromBlock(blk->data, blkSize);
128212724Snikos.nikoleris@arm.com
128312724Snikos.nikoleris@arm.com    return pkt;
128412724Snikos.nikoleris@arm.com}
128512724Snikos.nikoleris@arm.com
128612724Snikos.nikoleris@arm.comPacketPtr
128712724Snikos.nikoleris@arm.comBaseCache::writecleanBlk(CacheBlk *blk, Request::Flags dest, PacketId id)
128812724Snikos.nikoleris@arm.com{
128912724Snikos.nikoleris@arm.com    Request *req = new Request(tags->regenerateBlkAddr(blk), blkSize, 0,
129012724Snikos.nikoleris@arm.com                               Request::wbMasterId);
129112724Snikos.nikoleris@arm.com    if (blk->isSecure()) {
129212724Snikos.nikoleris@arm.com        req->setFlags(Request::SECURE);
129312724Snikos.nikoleris@arm.com    }
129412724Snikos.nikoleris@arm.com    req->taskId(blk->task_id);
129512724Snikos.nikoleris@arm.com
129612724Snikos.nikoleris@arm.com    PacketPtr pkt = new Packet(req, MemCmd::WriteClean, blkSize, id);
129712724Snikos.nikoleris@arm.com
129812724Snikos.nikoleris@arm.com    if (dest) {
129912724Snikos.nikoleris@arm.com        req->setFlags(dest);
130012724Snikos.nikoleris@arm.com        pkt->setWriteThrough();
130112724Snikos.nikoleris@arm.com    }
130212724Snikos.nikoleris@arm.com
130312724Snikos.nikoleris@arm.com    DPRINTF(Cache, "Create %s writable: %d, dirty: %d\n", pkt->print(),
130412724Snikos.nikoleris@arm.com            blk->isWritable(), blk->isDirty());
130512724Snikos.nikoleris@arm.com
130612724Snikos.nikoleris@arm.com    if (blk->isWritable()) {
130712724Snikos.nikoleris@arm.com        // not asserting shared means we pass the block in modified
130812724Snikos.nikoleris@arm.com        // state, mark our own block non-writeable
130912724Snikos.nikoleris@arm.com        blk->status &= ~BlkWritable;
131012724Snikos.nikoleris@arm.com    } else {
131112724Snikos.nikoleris@arm.com        // we are in the Owned state, tell the receiver
131212724Snikos.nikoleris@arm.com        pkt->setHasSharers();
131312724Snikos.nikoleris@arm.com    }
131412724Snikos.nikoleris@arm.com
131512724Snikos.nikoleris@arm.com    // make sure the block is not marked dirty
131612724Snikos.nikoleris@arm.com    blk->status &= ~BlkDirty;
131712724Snikos.nikoleris@arm.com
131812724Snikos.nikoleris@arm.com    pkt->allocate();
131912724Snikos.nikoleris@arm.com    pkt->setDataFromBlock(blk->data, blkSize);
132012724Snikos.nikoleris@arm.com
132112724Snikos.nikoleris@arm.com    return pkt;
132212724Snikos.nikoleris@arm.com}
132312724Snikos.nikoleris@arm.com
132412724Snikos.nikoleris@arm.com
132512724Snikos.nikoleris@arm.comvoid
132612724Snikos.nikoleris@arm.comBaseCache::memWriteback()
132712724Snikos.nikoleris@arm.com{
132812728Snikos.nikoleris@arm.com    tags->forEachBlk([this](CacheBlk &blk) { writebackVisitor(blk); });
132912724Snikos.nikoleris@arm.com}
133012724Snikos.nikoleris@arm.com
133112724Snikos.nikoleris@arm.comvoid
133212724Snikos.nikoleris@arm.comBaseCache::memInvalidate()
133312724Snikos.nikoleris@arm.com{
133412728Snikos.nikoleris@arm.com    tags->forEachBlk([this](CacheBlk &blk) { invalidateVisitor(blk); });
133512724Snikos.nikoleris@arm.com}
133612724Snikos.nikoleris@arm.com
133712724Snikos.nikoleris@arm.combool
133812724Snikos.nikoleris@arm.comBaseCache::isDirty() const
133912724Snikos.nikoleris@arm.com{
134012728Snikos.nikoleris@arm.com    return tags->anyBlk([](CacheBlk &blk) { return blk.isDirty(); });
134112724Snikos.nikoleris@arm.com}
134212724Snikos.nikoleris@arm.com
134312728Snikos.nikoleris@arm.comvoid
134412724Snikos.nikoleris@arm.comBaseCache::writebackVisitor(CacheBlk &blk)
134512724Snikos.nikoleris@arm.com{
134612724Snikos.nikoleris@arm.com    if (blk.isDirty()) {
134712724Snikos.nikoleris@arm.com        assert(blk.isValid());
134812724Snikos.nikoleris@arm.com
134912724Snikos.nikoleris@arm.com        Request request(tags->regenerateBlkAddr(&blk),
135012724Snikos.nikoleris@arm.com                        blkSize, 0, Request::funcMasterId);
135112724Snikos.nikoleris@arm.com        request.taskId(blk.task_id);
135212724Snikos.nikoleris@arm.com        if (blk.isSecure()) {
135312724Snikos.nikoleris@arm.com            request.setFlags(Request::SECURE);
135412724Snikos.nikoleris@arm.com        }
135512724Snikos.nikoleris@arm.com
135612724Snikos.nikoleris@arm.com        Packet packet(&request, MemCmd::WriteReq);
135712724Snikos.nikoleris@arm.com        packet.dataStatic(blk.data);
135812724Snikos.nikoleris@arm.com
135912724Snikos.nikoleris@arm.com        memSidePort.sendFunctional(&packet);
136012724Snikos.nikoleris@arm.com
136112724Snikos.nikoleris@arm.com        blk.status &= ~BlkDirty;
136212724Snikos.nikoleris@arm.com    }
136312724Snikos.nikoleris@arm.com}
136412724Snikos.nikoleris@arm.com
136512728Snikos.nikoleris@arm.comvoid
136612724Snikos.nikoleris@arm.comBaseCache::invalidateVisitor(CacheBlk &blk)
136712724Snikos.nikoleris@arm.com{
136812724Snikos.nikoleris@arm.com    if (blk.isDirty())
136912724Snikos.nikoleris@arm.com        warn_once("Invalidating dirty cache lines. " \
137012724Snikos.nikoleris@arm.com                  "Expect things to break.\n");
137112724Snikos.nikoleris@arm.com
137212724Snikos.nikoleris@arm.com    if (blk.isValid()) {
137312724Snikos.nikoleris@arm.com        assert(!blk.isDirty());
137412724Snikos.nikoleris@arm.com        invalidateBlock(&blk);
137512724Snikos.nikoleris@arm.com    }
137612724Snikos.nikoleris@arm.com}
137712724Snikos.nikoleris@arm.com
137812724Snikos.nikoleris@arm.comTick
137912724Snikos.nikoleris@arm.comBaseCache::nextQueueReadyTime() const
138012724Snikos.nikoleris@arm.com{
138112724Snikos.nikoleris@arm.com    Tick nextReady = std::min(mshrQueue.nextReadyTime(),
138212724Snikos.nikoleris@arm.com                              writeBuffer.nextReadyTime());
138312724Snikos.nikoleris@arm.com
138412724Snikos.nikoleris@arm.com    // Don't signal prefetch ready time if no MSHRs available
138512724Snikos.nikoleris@arm.com    // Will signal once enoguh MSHRs are deallocated
138612724Snikos.nikoleris@arm.com    if (prefetcher && mshrQueue.canPrefetch()) {
138712724Snikos.nikoleris@arm.com        nextReady = std::min(nextReady,
138812724Snikos.nikoleris@arm.com                             prefetcher->nextPrefetchReadyTime());
138912724Snikos.nikoleris@arm.com    }
139012724Snikos.nikoleris@arm.com
139112724Snikos.nikoleris@arm.com    return nextReady;
139212724Snikos.nikoleris@arm.com}
139312724Snikos.nikoleris@arm.com
139412724Snikos.nikoleris@arm.com
139512724Snikos.nikoleris@arm.combool
139612724Snikos.nikoleris@arm.comBaseCache::sendMSHRQueuePacket(MSHR* mshr)
139712724Snikos.nikoleris@arm.com{
139812724Snikos.nikoleris@arm.com    assert(mshr);
139912724Snikos.nikoleris@arm.com
140012724Snikos.nikoleris@arm.com    // use request from 1st target
140112724Snikos.nikoleris@arm.com    PacketPtr tgt_pkt = mshr->getTarget()->pkt;
140212724Snikos.nikoleris@arm.com
140312724Snikos.nikoleris@arm.com    DPRINTF(Cache, "%s: MSHR %s\n", __func__, tgt_pkt->print());
140412724Snikos.nikoleris@arm.com
140512724Snikos.nikoleris@arm.com    CacheBlk *blk = tags->findBlock(mshr->blkAddr, mshr->isSecure);
140612724Snikos.nikoleris@arm.com
140712724Snikos.nikoleris@arm.com    // either a prefetch that is not present upstream, or a normal
140812724Snikos.nikoleris@arm.com    // MSHR request, proceed to get the packet to send downstream
140912724Snikos.nikoleris@arm.com    PacketPtr pkt = createMissPacket(tgt_pkt, blk, mshr->needsWritable());
141012724Snikos.nikoleris@arm.com
141112724Snikos.nikoleris@arm.com    mshr->isForward = (pkt == nullptr);
141212724Snikos.nikoleris@arm.com
141312724Snikos.nikoleris@arm.com    if (mshr->isForward) {
141412724Snikos.nikoleris@arm.com        // not a cache block request, but a response is expected
141512724Snikos.nikoleris@arm.com        // make copy of current packet to forward, keep current
141612724Snikos.nikoleris@arm.com        // copy for response handling
141712724Snikos.nikoleris@arm.com        pkt = new Packet(tgt_pkt, false, true);
141812724Snikos.nikoleris@arm.com        assert(!pkt->isWrite());
141912724Snikos.nikoleris@arm.com    }
142012724Snikos.nikoleris@arm.com
142112724Snikos.nikoleris@arm.com    // play it safe and append (rather than set) the sender state,
142212724Snikos.nikoleris@arm.com    // as forwarded packets may already have existing state
142312724Snikos.nikoleris@arm.com    pkt->pushSenderState(mshr);
142412724Snikos.nikoleris@arm.com
142512724Snikos.nikoleris@arm.com    if (pkt->isClean() && blk && blk->isDirty()) {
142612724Snikos.nikoleris@arm.com        // A cache clean opearation is looking for a dirty block. Mark
142712724Snikos.nikoleris@arm.com        // the packet so that the destination xbar can determine that
142812724Snikos.nikoleris@arm.com        // there will be a follow-up write packet as well.
142912724Snikos.nikoleris@arm.com        pkt->setSatisfied();
143012724Snikos.nikoleris@arm.com    }
143112724Snikos.nikoleris@arm.com
143212724Snikos.nikoleris@arm.com    if (!memSidePort.sendTimingReq(pkt)) {
143312724Snikos.nikoleris@arm.com        // we are awaiting a retry, but we
143412724Snikos.nikoleris@arm.com        // delete the packet and will be creating a new packet
143512724Snikos.nikoleris@arm.com        // when we get the opportunity
143612724Snikos.nikoleris@arm.com        delete pkt;
143712724Snikos.nikoleris@arm.com
143812724Snikos.nikoleris@arm.com        // note that we have now masked any requestBus and
143912724Snikos.nikoleris@arm.com        // schedSendEvent (we will wait for a retry before
144012724Snikos.nikoleris@arm.com        // doing anything), and this is so even if we do not
144112724Snikos.nikoleris@arm.com        // care about this packet and might override it before
144212724Snikos.nikoleris@arm.com        // it gets retried
144312724Snikos.nikoleris@arm.com        return true;
144412724Snikos.nikoleris@arm.com    } else {
144512724Snikos.nikoleris@arm.com        // As part of the call to sendTimingReq the packet is
144612724Snikos.nikoleris@arm.com        // forwarded to all neighbouring caches (and any caches
144712724Snikos.nikoleris@arm.com        // above them) as a snoop. Thus at this point we know if
144812724Snikos.nikoleris@arm.com        // any of the neighbouring caches are responding, and if
144912724Snikos.nikoleris@arm.com        // so, we know it is dirty, and we can determine if it is
145012724Snikos.nikoleris@arm.com        // being passed as Modified, making our MSHR the ordering
145112724Snikos.nikoleris@arm.com        // point
145212724Snikos.nikoleris@arm.com        bool pending_modified_resp = !pkt->hasSharers() &&
145312724Snikos.nikoleris@arm.com            pkt->cacheResponding();
145412724Snikos.nikoleris@arm.com        markInService(mshr, pending_modified_resp);
145512724Snikos.nikoleris@arm.com
145612724Snikos.nikoleris@arm.com        if (pkt->isClean() && blk && blk->isDirty()) {
145712724Snikos.nikoleris@arm.com            // A cache clean opearation is looking for a dirty
145812724Snikos.nikoleris@arm.com            // block. If a dirty block is encountered a WriteClean
145912724Snikos.nikoleris@arm.com            // will update any copies to the path to the memory
146012724Snikos.nikoleris@arm.com            // until the point of reference.
146112724Snikos.nikoleris@arm.com            DPRINTF(CacheVerbose, "%s: packet %s found block: %s\n",
146212724Snikos.nikoleris@arm.com                    __func__, pkt->print(), blk->print());
146312724Snikos.nikoleris@arm.com            PacketPtr wb_pkt = writecleanBlk(blk, pkt->req->getDest(),
146412724Snikos.nikoleris@arm.com                                             pkt->id);
146512724Snikos.nikoleris@arm.com            PacketList writebacks;
146612724Snikos.nikoleris@arm.com            writebacks.push_back(wb_pkt);
146712724Snikos.nikoleris@arm.com            doWritebacks(writebacks, 0);
146812724Snikos.nikoleris@arm.com        }
146912724Snikos.nikoleris@arm.com
147012724Snikos.nikoleris@arm.com        return false;
147112724Snikos.nikoleris@arm.com    }
147212724Snikos.nikoleris@arm.com}
147312724Snikos.nikoleris@arm.com
147412724Snikos.nikoleris@arm.combool
147512724Snikos.nikoleris@arm.comBaseCache::sendWriteQueuePacket(WriteQueueEntry* wq_entry)
147612724Snikos.nikoleris@arm.com{
147712724Snikos.nikoleris@arm.com    assert(wq_entry);
147812724Snikos.nikoleris@arm.com
147912724Snikos.nikoleris@arm.com    // always a single target for write queue entries
148012724Snikos.nikoleris@arm.com    PacketPtr tgt_pkt = wq_entry->getTarget()->pkt;
148112724Snikos.nikoleris@arm.com
148212724Snikos.nikoleris@arm.com    DPRINTF(Cache, "%s: write %s\n", __func__, tgt_pkt->print());
148312724Snikos.nikoleris@arm.com
148412724Snikos.nikoleris@arm.com    // forward as is, both for evictions and uncacheable writes
148512724Snikos.nikoleris@arm.com    if (!memSidePort.sendTimingReq(tgt_pkt)) {
148612724Snikos.nikoleris@arm.com        // note that we have now masked any requestBus and
148712724Snikos.nikoleris@arm.com        // schedSendEvent (we will wait for a retry before
148812724Snikos.nikoleris@arm.com        // doing anything), and this is so even if we do not
148912724Snikos.nikoleris@arm.com        // care about this packet and might override it before
149012724Snikos.nikoleris@arm.com        // it gets retried
149112724Snikos.nikoleris@arm.com        return true;
149212724Snikos.nikoleris@arm.com    } else {
149312724Snikos.nikoleris@arm.com        markInService(wq_entry);
149412724Snikos.nikoleris@arm.com        return false;
149512724Snikos.nikoleris@arm.com    }
149612724Snikos.nikoleris@arm.com}
149712724Snikos.nikoleris@arm.com
149812724Snikos.nikoleris@arm.comvoid
149912724Snikos.nikoleris@arm.comBaseCache::serialize(CheckpointOut &cp) const
150012724Snikos.nikoleris@arm.com{
150112724Snikos.nikoleris@arm.com    bool dirty(isDirty());
150212724Snikos.nikoleris@arm.com
150312724Snikos.nikoleris@arm.com    if (dirty) {
150412724Snikos.nikoleris@arm.com        warn("*** The cache still contains dirty data. ***\n");
150512724Snikos.nikoleris@arm.com        warn("    Make sure to drain the system using the correct flags.\n");
150612724Snikos.nikoleris@arm.com        warn("    This checkpoint will not restore correctly " \
150712724Snikos.nikoleris@arm.com             "and dirty data in the cache will be lost!\n");
150812724Snikos.nikoleris@arm.com    }
150912724Snikos.nikoleris@arm.com
151012724Snikos.nikoleris@arm.com    // Since we don't checkpoint the data in the cache, any dirty data
151112724Snikos.nikoleris@arm.com    // will be lost when restoring from a checkpoint of a system that
151212724Snikos.nikoleris@arm.com    // wasn't drained properly. Flag the checkpoint as invalid if the
151312724Snikos.nikoleris@arm.com    // cache contains dirty data.
151412724Snikos.nikoleris@arm.com    bool bad_checkpoint(dirty);
151512724Snikos.nikoleris@arm.com    SERIALIZE_SCALAR(bad_checkpoint);
151612724Snikos.nikoleris@arm.com}
151712724Snikos.nikoleris@arm.com
151812724Snikos.nikoleris@arm.comvoid
151912724Snikos.nikoleris@arm.comBaseCache::unserialize(CheckpointIn &cp)
152012724Snikos.nikoleris@arm.com{
152112724Snikos.nikoleris@arm.com    bool bad_checkpoint;
152212724Snikos.nikoleris@arm.com    UNSERIALIZE_SCALAR(bad_checkpoint);
152312724Snikos.nikoleris@arm.com    if (bad_checkpoint) {
152412724Snikos.nikoleris@arm.com        fatal("Restoring from checkpoints with dirty caches is not "
152512724Snikos.nikoleris@arm.com              "supported in the classic memory system. Please remove any "
152612724Snikos.nikoleris@arm.com              "caches or drain them properly before taking checkpoints.\n");
152712724Snikos.nikoleris@arm.com    }
152812724Snikos.nikoleris@arm.com}
152912724Snikos.nikoleris@arm.com
153012724Snikos.nikoleris@arm.comvoid
15312810SN/ABaseCache::regStats()
15322810SN/A{
153311522Sstephan.diestelhorst@arm.com    MemObject::regStats();
153411522Sstephan.diestelhorst@arm.com
15352810SN/A    using namespace Stats;
15362810SN/A
15372810SN/A    // Hit statistics
15384022SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
15394022SN/A        MemCmd cmd(access_idx);
15404022SN/A        const string &cstr = cmd.toString();
15412810SN/A
15422810SN/A        hits[access_idx]
15438833Sdam.sunwoo@arm.com            .init(system->maxMasters())
15442810SN/A            .name(name() + "." + cstr + "_hits")
15452810SN/A            .desc("number of " + cstr + " hits")
15462810SN/A            .flags(total | nozero | nonan)
15472810SN/A            ;
15488833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
15498833Sdam.sunwoo@arm.com            hits[access_idx].subname(i, system->getMasterName(i));
15508833Sdam.sunwoo@arm.com        }
15512810SN/A    }
15522810SN/A
15534871SN/A// These macros make it easier to sum the right subset of commands and
15544871SN/A// to change the subset of commands that are considered "demand" vs
15554871SN/A// "non-demand"
15564871SN/A#define SUM_DEMAND(s) \
155711455Sandreas.hansson@arm.com    (s[MemCmd::ReadReq] + s[MemCmd::WriteReq] + s[MemCmd::WriteLineReq] + \
155810885Sandreas.hansson@arm.com     s[MemCmd::ReadExReq] + s[MemCmd::ReadCleanReq] + s[MemCmd::ReadSharedReq])
15594871SN/A
15604871SN/A// should writebacks be included here?  prior code was inconsistent...
15614871SN/A#define SUM_NON_DEMAND(s) \
15624871SN/A    (s[MemCmd::SoftPFReq] + s[MemCmd::HardPFReq])
15634871SN/A
15642810SN/A    demandHits
15652810SN/A        .name(name() + ".demand_hits")
15662810SN/A        .desc("number of demand (read+write) hits")
15678833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
15682810SN/A        ;
15694871SN/A    demandHits = SUM_DEMAND(hits);
15708833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
15718833Sdam.sunwoo@arm.com        demandHits.subname(i, system->getMasterName(i));
15728833Sdam.sunwoo@arm.com    }
15732810SN/A
15742810SN/A    overallHits
15752810SN/A        .name(name() + ".overall_hits")
15762810SN/A        .desc("number of overall hits")
15778833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
15782810SN/A        ;
15794871SN/A    overallHits = demandHits + SUM_NON_DEMAND(hits);
15808833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
15818833Sdam.sunwoo@arm.com        overallHits.subname(i, system->getMasterName(i));
15828833Sdam.sunwoo@arm.com    }
15832810SN/A
15842810SN/A    // Miss statistics
15854022SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
15864022SN/A        MemCmd cmd(access_idx);
15874022SN/A        const string &cstr = cmd.toString();
15882810SN/A
15892810SN/A        misses[access_idx]
15908833Sdam.sunwoo@arm.com            .init(system->maxMasters())
15912810SN/A            .name(name() + "." + cstr + "_misses")
15922810SN/A            .desc("number of " + cstr + " misses")
15932810SN/A            .flags(total | nozero | nonan)
15942810SN/A            ;
15958833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
15968833Sdam.sunwoo@arm.com            misses[access_idx].subname(i, system->getMasterName(i));
15978833Sdam.sunwoo@arm.com        }
15982810SN/A    }
15992810SN/A
16002810SN/A    demandMisses
16012810SN/A        .name(name() + ".demand_misses")
16022810SN/A        .desc("number of demand (read+write) misses")
16038833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
16042810SN/A        ;
16054871SN/A    demandMisses = SUM_DEMAND(misses);
16068833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
16078833Sdam.sunwoo@arm.com        demandMisses.subname(i, system->getMasterName(i));
16088833Sdam.sunwoo@arm.com    }
16092810SN/A
16102810SN/A    overallMisses
16112810SN/A        .name(name() + ".overall_misses")
16122810SN/A        .desc("number of overall misses")
16138833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
16142810SN/A        ;
16154871SN/A    overallMisses = demandMisses + SUM_NON_DEMAND(misses);
16168833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
16178833Sdam.sunwoo@arm.com        overallMisses.subname(i, system->getMasterName(i));
16188833Sdam.sunwoo@arm.com    }
16192810SN/A
16202810SN/A    // Miss latency statistics
16214022SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
16224022SN/A        MemCmd cmd(access_idx);
16234022SN/A        const string &cstr = cmd.toString();
16242810SN/A
16252810SN/A        missLatency[access_idx]
16268833Sdam.sunwoo@arm.com            .init(system->maxMasters())
16272810SN/A            .name(name() + "." + cstr + "_miss_latency")
16282810SN/A            .desc("number of " + cstr + " miss cycles")
16292810SN/A            .flags(total | nozero | nonan)
16302810SN/A            ;
16318833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
16328833Sdam.sunwoo@arm.com            missLatency[access_idx].subname(i, system->getMasterName(i));
16338833Sdam.sunwoo@arm.com        }
16342810SN/A    }
16352810SN/A
16362810SN/A    demandMissLatency
16372810SN/A        .name(name() + ".demand_miss_latency")
16382810SN/A        .desc("number of demand (read+write) miss cycles")
16398833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
16402810SN/A        ;
16414871SN/A    demandMissLatency = SUM_DEMAND(missLatency);
16428833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
16438833Sdam.sunwoo@arm.com        demandMissLatency.subname(i, system->getMasterName(i));
16448833Sdam.sunwoo@arm.com    }
16452810SN/A
16462810SN/A    overallMissLatency
16472810SN/A        .name(name() + ".overall_miss_latency")
16482810SN/A        .desc("number of overall miss cycles")
16498833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
16502810SN/A        ;
16514871SN/A    overallMissLatency = demandMissLatency + SUM_NON_DEMAND(missLatency);
16528833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
16538833Sdam.sunwoo@arm.com        overallMissLatency.subname(i, system->getMasterName(i));
16548833Sdam.sunwoo@arm.com    }
16552810SN/A
16562810SN/A    // access formulas
16574022SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
16584022SN/A        MemCmd cmd(access_idx);
16594022SN/A        const string &cstr = cmd.toString();
16602810SN/A
16612810SN/A        accesses[access_idx]
16622810SN/A            .name(name() + "." + cstr + "_accesses")
16632810SN/A            .desc("number of " + cstr + " accesses(hits+misses)")
16642810SN/A            .flags(total | nozero | nonan)
16652810SN/A            ;
16668833Sdam.sunwoo@arm.com        accesses[access_idx] = hits[access_idx] + misses[access_idx];
16672810SN/A
16688833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
16698833Sdam.sunwoo@arm.com            accesses[access_idx].subname(i, system->getMasterName(i));
16708833Sdam.sunwoo@arm.com        }
16712810SN/A    }
16722810SN/A
16732810SN/A    demandAccesses
16742810SN/A        .name(name() + ".demand_accesses")
16752810SN/A        .desc("number of demand (read+write) accesses")
16768833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
16772810SN/A        ;
16782810SN/A    demandAccesses = demandHits + demandMisses;
16798833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
16808833Sdam.sunwoo@arm.com        demandAccesses.subname(i, system->getMasterName(i));
16818833Sdam.sunwoo@arm.com    }
16822810SN/A
16832810SN/A    overallAccesses
16842810SN/A        .name(name() + ".overall_accesses")
16852810SN/A        .desc("number of overall (read+write) accesses")
16868833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
16872810SN/A        ;
16882810SN/A    overallAccesses = overallHits + overallMisses;
16898833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
16908833Sdam.sunwoo@arm.com        overallAccesses.subname(i, system->getMasterName(i));
16918833Sdam.sunwoo@arm.com    }
16922810SN/A
16932810SN/A    // miss rate formulas
16944022SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
16954022SN/A        MemCmd cmd(access_idx);
16964022SN/A        const string &cstr = cmd.toString();
16972810SN/A
16982810SN/A        missRate[access_idx]
16992810SN/A            .name(name() + "." + cstr + "_miss_rate")
17002810SN/A            .desc("miss rate for " + cstr + " accesses")
17012810SN/A            .flags(total | nozero | nonan)
17022810SN/A            ;
17038833Sdam.sunwoo@arm.com        missRate[access_idx] = misses[access_idx] / accesses[access_idx];
17042810SN/A
17058833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
17068833Sdam.sunwoo@arm.com            missRate[access_idx].subname(i, system->getMasterName(i));
17078833Sdam.sunwoo@arm.com        }
17082810SN/A    }
17092810SN/A
17102810SN/A    demandMissRate
17112810SN/A        .name(name() + ".demand_miss_rate")
17122810SN/A        .desc("miss rate for demand accesses")
17138833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
17142810SN/A        ;
17152810SN/A    demandMissRate = demandMisses / demandAccesses;
17168833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
17178833Sdam.sunwoo@arm.com        demandMissRate.subname(i, system->getMasterName(i));
17188833Sdam.sunwoo@arm.com    }
17192810SN/A
17202810SN/A    overallMissRate
17212810SN/A        .name(name() + ".overall_miss_rate")
17222810SN/A        .desc("miss rate for overall accesses")
17238833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
17242810SN/A        ;
17252810SN/A    overallMissRate = overallMisses / overallAccesses;
17268833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
17278833Sdam.sunwoo@arm.com        overallMissRate.subname(i, system->getMasterName(i));
17288833Sdam.sunwoo@arm.com    }
17292810SN/A
17302810SN/A    // miss latency formulas
17314022SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
17324022SN/A        MemCmd cmd(access_idx);
17334022SN/A        const string &cstr = cmd.toString();
17342810SN/A
17352810SN/A        avgMissLatency[access_idx]
17362810SN/A            .name(name() + "." + cstr + "_avg_miss_latency")
17372810SN/A            .desc("average " + cstr + " miss latency")
17382810SN/A            .flags(total | nozero | nonan)
17392810SN/A            ;
17402810SN/A        avgMissLatency[access_idx] =
17412810SN/A            missLatency[access_idx] / misses[access_idx];
17428833Sdam.sunwoo@arm.com
17438833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
17448833Sdam.sunwoo@arm.com            avgMissLatency[access_idx].subname(i, system->getMasterName(i));
17458833Sdam.sunwoo@arm.com        }
17462810SN/A    }
17472810SN/A
17482810SN/A    demandAvgMissLatency
17492810SN/A        .name(name() + ".demand_avg_miss_latency")
17502810SN/A        .desc("average overall miss latency")
17518833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
17522810SN/A        ;
17532810SN/A    demandAvgMissLatency = demandMissLatency / demandMisses;
17548833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
17558833Sdam.sunwoo@arm.com        demandAvgMissLatency.subname(i, system->getMasterName(i));
17568833Sdam.sunwoo@arm.com    }
17572810SN/A
17582810SN/A    overallAvgMissLatency
17592810SN/A        .name(name() + ".overall_avg_miss_latency")
17602810SN/A        .desc("average overall miss latency")
17618833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
17622810SN/A        ;
17632810SN/A    overallAvgMissLatency = overallMissLatency / overallMisses;
17648833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
17658833Sdam.sunwoo@arm.com        overallAvgMissLatency.subname(i, system->getMasterName(i));
17668833Sdam.sunwoo@arm.com    }
17672810SN/A
17682810SN/A    blocked_cycles.init(NUM_BLOCKED_CAUSES);
17692810SN/A    blocked_cycles
17702810SN/A        .name(name() + ".blocked_cycles")
17712810SN/A        .desc("number of cycles access was blocked")
17722810SN/A        .subname(Blocked_NoMSHRs, "no_mshrs")
17732810SN/A        .subname(Blocked_NoTargets, "no_targets")
17742810SN/A        ;
17752810SN/A
17762810SN/A
17772810SN/A    blocked_causes.init(NUM_BLOCKED_CAUSES);
17782810SN/A    blocked_causes
17792810SN/A        .name(name() + ".blocked")
17802810SN/A        .desc("number of cycles access was blocked")
17812810SN/A        .subname(Blocked_NoMSHRs, "no_mshrs")
17822810SN/A        .subname(Blocked_NoTargets, "no_targets")
17832810SN/A        ;
17842810SN/A
17852810SN/A    avg_blocked
17862810SN/A        .name(name() + ".avg_blocked_cycles")
17872810SN/A        .desc("average number of cycles each access was blocked")
17882810SN/A        .subname(Blocked_NoMSHRs, "no_mshrs")
17892810SN/A        .subname(Blocked_NoTargets, "no_targets")
17902810SN/A        ;
17912810SN/A
17922810SN/A    avg_blocked = blocked_cycles / blocked_causes;
17932810SN/A
179411436SRekai.GonzalezAlberquilla@arm.com    unusedPrefetches
179511436SRekai.GonzalezAlberquilla@arm.com        .name(name() + ".unused_prefetches")
179611436SRekai.GonzalezAlberquilla@arm.com        .desc("number of HardPF blocks evicted w/o reference")
179711436SRekai.GonzalezAlberquilla@arm.com        .flags(nozero)
179811436SRekai.GonzalezAlberquilla@arm.com        ;
179911436SRekai.GonzalezAlberquilla@arm.com
18004626SN/A    writebacks
18018833Sdam.sunwoo@arm.com        .init(system->maxMasters())
18024626SN/A        .name(name() + ".writebacks")
18034626SN/A        .desc("number of writebacks")
18048833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
18054626SN/A        ;
18068833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
18078833Sdam.sunwoo@arm.com        writebacks.subname(i, system->getMasterName(i));
18088833Sdam.sunwoo@arm.com    }
18094626SN/A
18104626SN/A    // MSHR statistics
18114626SN/A    // MSHR hit statistics
18124626SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
18134626SN/A        MemCmd cmd(access_idx);
18144626SN/A        const string &cstr = cmd.toString();
18154626SN/A
18164626SN/A        mshr_hits[access_idx]
18178833Sdam.sunwoo@arm.com            .init(system->maxMasters())
18184626SN/A            .name(name() + "." + cstr + "_mshr_hits")
18194626SN/A            .desc("number of " + cstr + " MSHR hits")
18204626SN/A            .flags(total | nozero | nonan)
18214626SN/A            ;
18228833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
18238833Sdam.sunwoo@arm.com            mshr_hits[access_idx].subname(i, system->getMasterName(i));
18248833Sdam.sunwoo@arm.com        }
18254626SN/A    }
18264626SN/A
18274626SN/A    demandMshrHits
18284626SN/A        .name(name() + ".demand_mshr_hits")
18294626SN/A        .desc("number of demand (read+write) MSHR hits")
18308833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
18314626SN/A        ;
18324871SN/A    demandMshrHits = SUM_DEMAND(mshr_hits);
18338833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
18348833Sdam.sunwoo@arm.com        demandMshrHits.subname(i, system->getMasterName(i));
18358833Sdam.sunwoo@arm.com    }
18364626SN/A
18374626SN/A    overallMshrHits
18384626SN/A        .name(name() + ".overall_mshr_hits")
18394626SN/A        .desc("number of overall MSHR hits")
18408833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
18414626SN/A        ;
18424871SN/A    overallMshrHits = demandMshrHits + SUM_NON_DEMAND(mshr_hits);
18438833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
18448833Sdam.sunwoo@arm.com        overallMshrHits.subname(i, system->getMasterName(i));
18458833Sdam.sunwoo@arm.com    }
18464626SN/A
18474626SN/A    // MSHR miss statistics
18484626SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
18494626SN/A        MemCmd cmd(access_idx);
18504626SN/A        const string &cstr = cmd.toString();
18514626SN/A
18524626SN/A        mshr_misses[access_idx]
18538833Sdam.sunwoo@arm.com            .init(system->maxMasters())
18544626SN/A            .name(name() + "." + cstr + "_mshr_misses")
18554626SN/A            .desc("number of " + cstr + " MSHR misses")
18564626SN/A            .flags(total | nozero | nonan)
18574626SN/A            ;
18588833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
18598833Sdam.sunwoo@arm.com            mshr_misses[access_idx].subname(i, system->getMasterName(i));
18608833Sdam.sunwoo@arm.com        }
18614626SN/A    }
18624626SN/A
18634626SN/A    demandMshrMisses
18644626SN/A        .name(name() + ".demand_mshr_misses")
18654626SN/A        .desc("number of demand (read+write) MSHR misses")
18668833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
18674626SN/A        ;
18684871SN/A    demandMshrMisses = SUM_DEMAND(mshr_misses);
18698833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
18708833Sdam.sunwoo@arm.com        demandMshrMisses.subname(i, system->getMasterName(i));
18718833Sdam.sunwoo@arm.com    }
18724626SN/A
18734626SN/A    overallMshrMisses
18744626SN/A        .name(name() + ".overall_mshr_misses")
18754626SN/A        .desc("number of overall MSHR misses")
18768833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
18774626SN/A        ;
18784871SN/A    overallMshrMisses = demandMshrMisses + SUM_NON_DEMAND(mshr_misses);
18798833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
18808833Sdam.sunwoo@arm.com        overallMshrMisses.subname(i, system->getMasterName(i));
18818833Sdam.sunwoo@arm.com    }
18824626SN/A
18834626SN/A    // MSHR miss latency statistics
18844626SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
18854626SN/A        MemCmd cmd(access_idx);
18864626SN/A        const string &cstr = cmd.toString();
18874626SN/A
18884626SN/A        mshr_miss_latency[access_idx]
18898833Sdam.sunwoo@arm.com            .init(system->maxMasters())
18904626SN/A            .name(name() + "." + cstr + "_mshr_miss_latency")
18914626SN/A            .desc("number of " + cstr + " MSHR miss cycles")
18924626SN/A            .flags(total | nozero | nonan)
18934626SN/A            ;
18948833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
18958833Sdam.sunwoo@arm.com            mshr_miss_latency[access_idx].subname(i, system->getMasterName(i));
18968833Sdam.sunwoo@arm.com        }
18974626SN/A    }
18984626SN/A
18994626SN/A    demandMshrMissLatency
19004626SN/A        .name(name() + ".demand_mshr_miss_latency")
19014626SN/A        .desc("number of demand (read+write) MSHR miss cycles")
19028833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
19034626SN/A        ;
19044871SN/A    demandMshrMissLatency = SUM_DEMAND(mshr_miss_latency);
19058833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
19068833Sdam.sunwoo@arm.com        demandMshrMissLatency.subname(i, system->getMasterName(i));
19078833Sdam.sunwoo@arm.com    }
19084626SN/A
19094626SN/A    overallMshrMissLatency
19104626SN/A        .name(name() + ".overall_mshr_miss_latency")
19114626SN/A        .desc("number of overall MSHR miss cycles")
19128833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
19134626SN/A        ;
19144871SN/A    overallMshrMissLatency =
19154871SN/A        demandMshrMissLatency + SUM_NON_DEMAND(mshr_miss_latency);
19168833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
19178833Sdam.sunwoo@arm.com        overallMshrMissLatency.subname(i, system->getMasterName(i));
19188833Sdam.sunwoo@arm.com    }
19194626SN/A
19204626SN/A    // MSHR uncacheable statistics
19214626SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
19224626SN/A        MemCmd cmd(access_idx);
19234626SN/A        const string &cstr = cmd.toString();
19244626SN/A
19254626SN/A        mshr_uncacheable[access_idx]
19268833Sdam.sunwoo@arm.com            .init(system->maxMasters())
19274626SN/A            .name(name() + "." + cstr + "_mshr_uncacheable")
19284626SN/A            .desc("number of " + cstr + " MSHR uncacheable")
19294626SN/A            .flags(total | nozero | nonan)
19304626SN/A            ;
19318833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
19328833Sdam.sunwoo@arm.com            mshr_uncacheable[access_idx].subname(i, system->getMasterName(i));
19338833Sdam.sunwoo@arm.com        }
19344626SN/A    }
19354626SN/A
19364626SN/A    overallMshrUncacheable
19374626SN/A        .name(name() + ".overall_mshr_uncacheable_misses")
19384626SN/A        .desc("number of overall MSHR uncacheable misses")
19398833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
19404626SN/A        ;
19414871SN/A    overallMshrUncacheable =
19424871SN/A        SUM_DEMAND(mshr_uncacheable) + SUM_NON_DEMAND(mshr_uncacheable);
19438833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
19448833Sdam.sunwoo@arm.com        overallMshrUncacheable.subname(i, system->getMasterName(i));
19458833Sdam.sunwoo@arm.com    }
19464626SN/A
19474626SN/A    // MSHR miss latency statistics
19484626SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
19494626SN/A        MemCmd cmd(access_idx);
19504626SN/A        const string &cstr = cmd.toString();
19514626SN/A
19524626SN/A        mshr_uncacheable_lat[access_idx]
19538833Sdam.sunwoo@arm.com            .init(system->maxMasters())
19544626SN/A            .name(name() + "." + cstr + "_mshr_uncacheable_latency")
19554626SN/A            .desc("number of " + cstr + " MSHR uncacheable cycles")
19564626SN/A            .flags(total | nozero | nonan)
19574626SN/A            ;
19588833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
195911483Snikos.nikoleris@arm.com            mshr_uncacheable_lat[access_idx].subname(
196011483Snikos.nikoleris@arm.com                i, system->getMasterName(i));
19618833Sdam.sunwoo@arm.com        }
19624626SN/A    }
19634626SN/A
19644626SN/A    overallMshrUncacheableLatency
19654626SN/A        .name(name() + ".overall_mshr_uncacheable_latency")
19664626SN/A        .desc("number of overall MSHR uncacheable cycles")
19678833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
19684626SN/A        ;
19694871SN/A    overallMshrUncacheableLatency =
19704871SN/A        SUM_DEMAND(mshr_uncacheable_lat) +
19714871SN/A        SUM_NON_DEMAND(mshr_uncacheable_lat);
19728833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
19738833Sdam.sunwoo@arm.com        overallMshrUncacheableLatency.subname(i, system->getMasterName(i));
19748833Sdam.sunwoo@arm.com    }
19754626SN/A
19764626SN/A#if 0
19774626SN/A    // MSHR access formulas
19784626SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
19794626SN/A        MemCmd cmd(access_idx);
19804626SN/A        const string &cstr = cmd.toString();
19814626SN/A
19824626SN/A        mshrAccesses[access_idx]
19834626SN/A            .name(name() + "." + cstr + "_mshr_accesses")
19844626SN/A            .desc("number of " + cstr + " mshr accesses(hits+misses)")
19854626SN/A            .flags(total | nozero | nonan)
19864626SN/A            ;
19874626SN/A        mshrAccesses[access_idx] =
19884626SN/A            mshr_hits[access_idx] + mshr_misses[access_idx]
19894626SN/A            + mshr_uncacheable[access_idx];
19904626SN/A    }
19914626SN/A
19924626SN/A    demandMshrAccesses
19934626SN/A        .name(name() + ".demand_mshr_accesses")
19944626SN/A        .desc("number of demand (read+write) mshr accesses")
19954626SN/A        .flags(total | nozero | nonan)
19964626SN/A        ;
19974626SN/A    demandMshrAccesses = demandMshrHits + demandMshrMisses;
19984626SN/A
19994626SN/A    overallMshrAccesses
20004626SN/A        .name(name() + ".overall_mshr_accesses")
20014626SN/A        .desc("number of overall (read+write) mshr accesses")
20024626SN/A        .flags(total | nozero | nonan)
20034626SN/A        ;
20044626SN/A    overallMshrAccesses = overallMshrHits + overallMshrMisses
20054626SN/A        + overallMshrUncacheable;
20064626SN/A#endif
20074626SN/A
20084626SN/A    // MSHR miss rate formulas
20094626SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
20104626SN/A        MemCmd cmd(access_idx);
20114626SN/A        const string &cstr = cmd.toString();
20124626SN/A
20134626SN/A        mshrMissRate[access_idx]
20144626SN/A            .name(name() + "." + cstr + "_mshr_miss_rate")
20154626SN/A            .desc("mshr miss rate for " + cstr + " accesses")
20164626SN/A            .flags(total | nozero | nonan)
20174626SN/A            ;
20184626SN/A        mshrMissRate[access_idx] =
20194626SN/A            mshr_misses[access_idx] / accesses[access_idx];
20208833Sdam.sunwoo@arm.com
20218833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
20228833Sdam.sunwoo@arm.com            mshrMissRate[access_idx].subname(i, system->getMasterName(i));
20238833Sdam.sunwoo@arm.com        }
20244626SN/A    }
20254626SN/A
20264626SN/A    demandMshrMissRate
20274626SN/A        .name(name() + ".demand_mshr_miss_rate")
20284626SN/A        .desc("mshr miss rate for demand accesses")
20298833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
20304626SN/A        ;
20314626SN/A    demandMshrMissRate = demandMshrMisses / demandAccesses;
20328833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
20338833Sdam.sunwoo@arm.com        demandMshrMissRate.subname(i, system->getMasterName(i));
20348833Sdam.sunwoo@arm.com    }
20354626SN/A
20364626SN/A    overallMshrMissRate
20374626SN/A        .name(name() + ".overall_mshr_miss_rate")
20384626SN/A        .desc("mshr miss rate for overall accesses")
20398833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
20404626SN/A        ;
20414626SN/A    overallMshrMissRate = overallMshrMisses / overallAccesses;
20428833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
20438833Sdam.sunwoo@arm.com        overallMshrMissRate.subname(i, system->getMasterName(i));
20448833Sdam.sunwoo@arm.com    }
20454626SN/A
20464626SN/A    // mshrMiss latency formulas
20474626SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
20484626SN/A        MemCmd cmd(access_idx);
20494626SN/A        const string &cstr = cmd.toString();
20504626SN/A
20514626SN/A        avgMshrMissLatency[access_idx]
20524626SN/A            .name(name() + "." + cstr + "_avg_mshr_miss_latency")
20534626SN/A            .desc("average " + cstr + " mshr miss latency")
20544626SN/A            .flags(total | nozero | nonan)
20554626SN/A            ;
20564626SN/A        avgMshrMissLatency[access_idx] =
20574626SN/A            mshr_miss_latency[access_idx] / mshr_misses[access_idx];
20588833Sdam.sunwoo@arm.com
20598833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
206011483Snikos.nikoleris@arm.com            avgMshrMissLatency[access_idx].subname(
206111483Snikos.nikoleris@arm.com                i, system->getMasterName(i));
20628833Sdam.sunwoo@arm.com        }
20634626SN/A    }
20644626SN/A
20654626SN/A    demandAvgMshrMissLatency
20664626SN/A        .name(name() + ".demand_avg_mshr_miss_latency")
20674626SN/A        .desc("average overall mshr miss latency")
20688833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
20694626SN/A        ;
20704626SN/A    demandAvgMshrMissLatency = demandMshrMissLatency / demandMshrMisses;
20718833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
20728833Sdam.sunwoo@arm.com        demandAvgMshrMissLatency.subname(i, system->getMasterName(i));
20738833Sdam.sunwoo@arm.com    }
20744626SN/A
20754626SN/A    overallAvgMshrMissLatency
20764626SN/A        .name(name() + ".overall_avg_mshr_miss_latency")
20774626SN/A        .desc("average overall mshr miss latency")
20788833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
20794626SN/A        ;
20804626SN/A    overallAvgMshrMissLatency = overallMshrMissLatency / overallMshrMisses;
20818833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
20828833Sdam.sunwoo@arm.com        overallAvgMshrMissLatency.subname(i, system->getMasterName(i));
20838833Sdam.sunwoo@arm.com    }
20844626SN/A
20854626SN/A    // mshrUncacheable latency formulas
20864626SN/A    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
20874626SN/A        MemCmd cmd(access_idx);
20884626SN/A        const string &cstr = cmd.toString();
20894626SN/A
20904626SN/A        avgMshrUncacheableLatency[access_idx]
20914626SN/A            .name(name() + "." + cstr + "_avg_mshr_uncacheable_latency")
20924626SN/A            .desc("average " + cstr + " mshr uncacheable latency")
20934626SN/A            .flags(total | nozero | nonan)
20944626SN/A            ;
20954626SN/A        avgMshrUncacheableLatency[access_idx] =
20964626SN/A            mshr_uncacheable_lat[access_idx] / mshr_uncacheable[access_idx];
20978833Sdam.sunwoo@arm.com
20988833Sdam.sunwoo@arm.com        for (int i = 0; i < system->maxMasters(); i++) {
209911483Snikos.nikoleris@arm.com            avgMshrUncacheableLatency[access_idx].subname(
210011483Snikos.nikoleris@arm.com                i, system->getMasterName(i));
21018833Sdam.sunwoo@arm.com        }
21024626SN/A    }
21034626SN/A
21044626SN/A    overallAvgMshrUncacheableLatency
21054626SN/A        .name(name() + ".overall_avg_mshr_uncacheable_latency")
21064626SN/A        .desc("average overall mshr uncacheable latency")
21078833Sdam.sunwoo@arm.com        .flags(total | nozero | nonan)
21084626SN/A        ;
210911483Snikos.nikoleris@arm.com    overallAvgMshrUncacheableLatency =
211011483Snikos.nikoleris@arm.com        overallMshrUncacheableLatency / overallMshrUncacheable;
21118833Sdam.sunwoo@arm.com    for (int i = 0; i < system->maxMasters(); i++) {
21128833Sdam.sunwoo@arm.com        overallAvgMshrUncacheableLatency.subname(i, system->getMasterName(i));
21138833Sdam.sunwoo@arm.com    }
21144626SN/A
211512702Snikos.nikoleris@arm.com    replacements
211612702Snikos.nikoleris@arm.com        .name(name() + ".replacements")
211712702Snikos.nikoleris@arm.com        .desc("number of replacements")
211812702Snikos.nikoleris@arm.com        ;
21192810SN/A}
212012724Snikos.nikoleris@arm.com
212112724Snikos.nikoleris@arm.com///////////////
212212724Snikos.nikoleris@arm.com//
212312724Snikos.nikoleris@arm.com// CpuSidePort
212412724Snikos.nikoleris@arm.com//
212512724Snikos.nikoleris@arm.com///////////////
212612724Snikos.nikoleris@arm.combool
212712724Snikos.nikoleris@arm.comBaseCache::CpuSidePort::recvTimingSnoopResp(PacketPtr pkt)
212812724Snikos.nikoleris@arm.com{
212912725Snikos.nikoleris@arm.com    // Snoops shouldn't happen when bypassing caches
213012725Snikos.nikoleris@arm.com    assert(!cache->system->bypassCaches());
213112725Snikos.nikoleris@arm.com
213212725Snikos.nikoleris@arm.com    assert(pkt->isResponse());
213312725Snikos.nikoleris@arm.com
213412724Snikos.nikoleris@arm.com    // Express snoop responses from master to slave, e.g., from L1 to L2
213512724Snikos.nikoleris@arm.com    cache->recvTimingSnoopResp(pkt);
213612724Snikos.nikoleris@arm.com    return true;
213712724Snikos.nikoleris@arm.com}
213812724Snikos.nikoleris@arm.com
213912724Snikos.nikoleris@arm.com
214012724Snikos.nikoleris@arm.combool
214112724Snikos.nikoleris@arm.comBaseCache::CpuSidePort::tryTiming(PacketPtr pkt)
214212724Snikos.nikoleris@arm.com{
214312725Snikos.nikoleris@arm.com    if (cache->system->bypassCaches() || pkt->isExpressSnoop()) {
214412724Snikos.nikoleris@arm.com        // always let express snoop packets through even if blocked
214512724Snikos.nikoleris@arm.com        return true;
214612724Snikos.nikoleris@arm.com    } else if (blocked || mustSendRetry) {
214712724Snikos.nikoleris@arm.com        // either already committed to send a retry, or blocked
214812724Snikos.nikoleris@arm.com        mustSendRetry = true;
214912724Snikos.nikoleris@arm.com        return false;
215012724Snikos.nikoleris@arm.com    }
215112724Snikos.nikoleris@arm.com    mustSendRetry = false;
215212724Snikos.nikoleris@arm.com    return true;
215312724Snikos.nikoleris@arm.com}
215412724Snikos.nikoleris@arm.com
215512724Snikos.nikoleris@arm.combool
215612724Snikos.nikoleris@arm.comBaseCache::CpuSidePort::recvTimingReq(PacketPtr pkt)
215712724Snikos.nikoleris@arm.com{
215812725Snikos.nikoleris@arm.com    assert(pkt->isRequest());
215912725Snikos.nikoleris@arm.com
216012725Snikos.nikoleris@arm.com    if (cache->system->bypassCaches()) {
216112725Snikos.nikoleris@arm.com        // Just forward the packet if caches are disabled.
216212725Snikos.nikoleris@arm.com        // @todo This should really enqueue the packet rather
216312725Snikos.nikoleris@arm.com        bool M5_VAR_USED success = cache->memSidePort.sendTimingReq(pkt);
216412725Snikos.nikoleris@arm.com        assert(success);
216512725Snikos.nikoleris@arm.com        return true;
216612725Snikos.nikoleris@arm.com    } else if (tryTiming(pkt)) {
216712724Snikos.nikoleris@arm.com        cache->recvTimingReq(pkt);
216812724Snikos.nikoleris@arm.com        return true;
216912724Snikos.nikoleris@arm.com    }
217012724Snikos.nikoleris@arm.com    return false;
217112724Snikos.nikoleris@arm.com}
217212724Snikos.nikoleris@arm.com
217312724Snikos.nikoleris@arm.comTick
217412724Snikos.nikoleris@arm.comBaseCache::CpuSidePort::recvAtomic(PacketPtr pkt)
217512724Snikos.nikoleris@arm.com{
217612725Snikos.nikoleris@arm.com    if (cache->system->bypassCaches()) {
217712725Snikos.nikoleris@arm.com        // Forward the request if the system is in cache bypass mode.
217812725Snikos.nikoleris@arm.com        return cache->memSidePort.sendAtomic(pkt);
217912725Snikos.nikoleris@arm.com    } else {
218012725Snikos.nikoleris@arm.com        return cache->recvAtomic(pkt);
218112725Snikos.nikoleris@arm.com    }
218212724Snikos.nikoleris@arm.com}
218312724Snikos.nikoleris@arm.com
218412724Snikos.nikoleris@arm.comvoid
218512724Snikos.nikoleris@arm.comBaseCache::CpuSidePort::recvFunctional(PacketPtr pkt)
218612724Snikos.nikoleris@arm.com{
218712725Snikos.nikoleris@arm.com    if (cache->system->bypassCaches()) {
218812725Snikos.nikoleris@arm.com        // The cache should be flushed if we are in cache bypass mode,
218912725Snikos.nikoleris@arm.com        // so we don't need to check if we need to update anything.
219012725Snikos.nikoleris@arm.com        cache->memSidePort.sendFunctional(pkt);
219112725Snikos.nikoleris@arm.com        return;
219212725Snikos.nikoleris@arm.com    }
219312725Snikos.nikoleris@arm.com
219412724Snikos.nikoleris@arm.com    // functional request
219512724Snikos.nikoleris@arm.com    cache->functionalAccess(pkt, true);
219612724Snikos.nikoleris@arm.com}
219712724Snikos.nikoleris@arm.com
219812724Snikos.nikoleris@arm.comAddrRangeList
219912724Snikos.nikoleris@arm.comBaseCache::CpuSidePort::getAddrRanges() const
220012724Snikos.nikoleris@arm.com{
220112724Snikos.nikoleris@arm.com    return cache->getAddrRanges();
220212724Snikos.nikoleris@arm.com}
220312724Snikos.nikoleris@arm.com
220412724Snikos.nikoleris@arm.com
220512724Snikos.nikoleris@arm.comBaseCache::
220612724Snikos.nikoleris@arm.comCpuSidePort::CpuSidePort(const std::string &_name, BaseCache *_cache,
220712724Snikos.nikoleris@arm.com                         const std::string &_label)
220812724Snikos.nikoleris@arm.com    : CacheSlavePort(_name, _cache, _label), cache(_cache)
220912724Snikos.nikoleris@arm.com{
221012724Snikos.nikoleris@arm.com}
221112724Snikos.nikoleris@arm.com
221212724Snikos.nikoleris@arm.com///////////////
221312724Snikos.nikoleris@arm.com//
221412724Snikos.nikoleris@arm.com// MemSidePort
221512724Snikos.nikoleris@arm.com//
221612724Snikos.nikoleris@arm.com///////////////
221712724Snikos.nikoleris@arm.combool
221812724Snikos.nikoleris@arm.comBaseCache::MemSidePort::recvTimingResp(PacketPtr pkt)
221912724Snikos.nikoleris@arm.com{
222012724Snikos.nikoleris@arm.com    cache->recvTimingResp(pkt);
222112724Snikos.nikoleris@arm.com    return true;
222212724Snikos.nikoleris@arm.com}
222312724Snikos.nikoleris@arm.com
222412724Snikos.nikoleris@arm.com// Express snooping requests to memside port
222512724Snikos.nikoleris@arm.comvoid
222612724Snikos.nikoleris@arm.comBaseCache::MemSidePort::recvTimingSnoopReq(PacketPtr pkt)
222712724Snikos.nikoleris@arm.com{
222812725Snikos.nikoleris@arm.com    // Snoops shouldn't happen when bypassing caches
222912725Snikos.nikoleris@arm.com    assert(!cache->system->bypassCaches());
223012725Snikos.nikoleris@arm.com
223112724Snikos.nikoleris@arm.com    // handle snooping requests
223212724Snikos.nikoleris@arm.com    cache->recvTimingSnoopReq(pkt);
223312724Snikos.nikoleris@arm.com}
223412724Snikos.nikoleris@arm.com
223512724Snikos.nikoleris@arm.comTick
223612724Snikos.nikoleris@arm.comBaseCache::MemSidePort::recvAtomicSnoop(PacketPtr pkt)
223712724Snikos.nikoleris@arm.com{
223812725Snikos.nikoleris@arm.com    // Snoops shouldn't happen when bypassing caches
223912725Snikos.nikoleris@arm.com    assert(!cache->system->bypassCaches());
224012725Snikos.nikoleris@arm.com
224112724Snikos.nikoleris@arm.com    return cache->recvAtomicSnoop(pkt);
224212724Snikos.nikoleris@arm.com}
224312724Snikos.nikoleris@arm.com
224412724Snikos.nikoleris@arm.comvoid
224512724Snikos.nikoleris@arm.comBaseCache::MemSidePort::recvFunctionalSnoop(PacketPtr pkt)
224612724Snikos.nikoleris@arm.com{
224712725Snikos.nikoleris@arm.com    // Snoops shouldn't happen when bypassing caches
224812725Snikos.nikoleris@arm.com    assert(!cache->system->bypassCaches());
224912725Snikos.nikoleris@arm.com
225012724Snikos.nikoleris@arm.com    // functional snoop (note that in contrast to atomic we don't have
225112724Snikos.nikoleris@arm.com    // a specific functionalSnoop method, as they have the same
225212724Snikos.nikoleris@arm.com    // behaviour regardless)
225312724Snikos.nikoleris@arm.com    cache->functionalAccess(pkt, false);
225412724Snikos.nikoleris@arm.com}
225512724Snikos.nikoleris@arm.com
225612724Snikos.nikoleris@arm.comvoid
225712724Snikos.nikoleris@arm.comBaseCache::CacheReqPacketQueue::sendDeferredPacket()
225812724Snikos.nikoleris@arm.com{
225912724Snikos.nikoleris@arm.com    // sanity check
226012724Snikos.nikoleris@arm.com    assert(!waitingOnRetry);
226112724Snikos.nikoleris@arm.com
226212724Snikos.nikoleris@arm.com    // there should never be any deferred request packets in the
226312724Snikos.nikoleris@arm.com    // queue, instead we resly on the cache to provide the packets
226412724Snikos.nikoleris@arm.com    // from the MSHR queue or write queue
226512724Snikos.nikoleris@arm.com    assert(deferredPacketReadyTime() == MaxTick);
226612724Snikos.nikoleris@arm.com
226712724Snikos.nikoleris@arm.com    // check for request packets (requests & writebacks)
226812724Snikos.nikoleris@arm.com    QueueEntry* entry = cache.getNextQueueEntry();
226912724Snikos.nikoleris@arm.com
227012724Snikos.nikoleris@arm.com    if (!entry) {
227112724Snikos.nikoleris@arm.com        // can happen if e.g. we attempt a writeback and fail, but
227212724Snikos.nikoleris@arm.com        // before the retry, the writeback is eliminated because
227312724Snikos.nikoleris@arm.com        // we snoop another cache's ReadEx.
227412724Snikos.nikoleris@arm.com    } else {
227512724Snikos.nikoleris@arm.com        // let our snoop responses go first if there are responses to
227612724Snikos.nikoleris@arm.com        // the same addresses
227712724Snikos.nikoleris@arm.com        if (checkConflictingSnoop(entry->blkAddr)) {
227812724Snikos.nikoleris@arm.com            return;
227912724Snikos.nikoleris@arm.com        }
228012724Snikos.nikoleris@arm.com        waitingOnRetry = entry->sendPacket(cache);
228112724Snikos.nikoleris@arm.com    }
228212724Snikos.nikoleris@arm.com
228312724Snikos.nikoleris@arm.com    // if we succeeded and are not waiting for a retry, schedule the
228412724Snikos.nikoleris@arm.com    // next send considering when the next queue is ready, note that
228512724Snikos.nikoleris@arm.com    // snoop responses have their own packet queue and thus schedule
228612724Snikos.nikoleris@arm.com    // their own events
228712724Snikos.nikoleris@arm.com    if (!waitingOnRetry) {
228812724Snikos.nikoleris@arm.com        schedSendEvent(cache.nextQueueReadyTime());
228912724Snikos.nikoleris@arm.com    }
229012724Snikos.nikoleris@arm.com}
229112724Snikos.nikoleris@arm.com
229212724Snikos.nikoleris@arm.comBaseCache::MemSidePort::MemSidePort(const std::string &_name,
229312724Snikos.nikoleris@arm.com                                    BaseCache *_cache,
229412724Snikos.nikoleris@arm.com                                    const std::string &_label)
229512724Snikos.nikoleris@arm.com    : CacheMasterPort(_name, _cache, _reqQueue, _snoopRespQueue),
229612724Snikos.nikoleris@arm.com      _reqQueue(*_cache, *this, _snoopRespQueue, _label),
229712724Snikos.nikoleris@arm.com      _snoopRespQueue(*_cache, *this, _label), cache(_cache)
229812724Snikos.nikoleris@arm.com{
229912724Snikos.nikoleris@arm.com}
2300