cache.hh revision 12719
12810Srdreslin@umich.edu/*
212719Snikos.nikoleris@arm.com * Copyright (c) 2012-2018 ARM Limited
38702Sandreas.hansson@arm.com * All rights reserved.
48702Sandreas.hansson@arm.com *
58702Sandreas.hansson@arm.com * The license below extends only to copyright in the software and shall
68702Sandreas.hansson@arm.com * not be construed as granting a license to any other intellectual
78702Sandreas.hansson@arm.com * property including but not limited to intellectual property relating
88702Sandreas.hansson@arm.com * to a hardware implementation of the functionality of the software
98702Sandreas.hansson@arm.com * licensed hereunder.  You may use the software subject to the license
108702Sandreas.hansson@arm.com * terms below provided that you ensure that this notice is replicated
118702Sandreas.hansson@arm.com * unmodified and in its entirety in all distributions of the software,
128702Sandreas.hansson@arm.com * modified or unmodified, in source code or in binary form.
138702Sandreas.hansson@arm.com *
142810Srdreslin@umich.edu * Copyright (c) 2002-2005 The Regents of The University of Michigan
152810Srdreslin@umich.edu * All rights reserved.
162810Srdreslin@umich.edu *
172810Srdreslin@umich.edu * Redistribution and use in source and binary forms, with or without
182810Srdreslin@umich.edu * modification, are permitted provided that the following conditions are
192810Srdreslin@umich.edu * met: redistributions of source code must retain the above copyright
202810Srdreslin@umich.edu * notice, this list of conditions and the following disclaimer;
212810Srdreslin@umich.edu * redistributions in binary form must reproduce the above copyright
222810Srdreslin@umich.edu * notice, this list of conditions and the following disclaimer in the
232810Srdreslin@umich.edu * documentation and/or other materials provided with the distribution;
242810Srdreslin@umich.edu * neither the name of the copyright holders nor the names of its
252810Srdreslin@umich.edu * contributors may be used to endorse or promote products derived from
262810Srdreslin@umich.edu * this software without specific prior written permission.
272810Srdreslin@umich.edu *
282810Srdreslin@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292810Srdreslin@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302810Srdreslin@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
312810Srdreslin@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
322810Srdreslin@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332810Srdreslin@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
342810Srdreslin@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352810Srdreslin@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362810Srdreslin@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372810Srdreslin@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
382810Srdreslin@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392810Srdreslin@umich.edu *
402810Srdreslin@umich.edu * Authors: Erik Hallnor
412810Srdreslin@umich.edu *          Dave Greene
422810Srdreslin@umich.edu *          Steve Reinhardt
434458Sstever@eecs.umich.edu *          Ron Dreslinski
448856Sandreas.hansson@arm.com *          Andreas Hansson
452810Srdreslin@umich.edu */
462810Srdreslin@umich.edu
472810Srdreslin@umich.edu/**
482810Srdreslin@umich.edu * @file
492810Srdreslin@umich.edu * Describes a cache based on template policies.
502810Srdreslin@umich.edu */
512810Srdreslin@umich.edu
5211051Sandreas.hansson@arm.com#ifndef __MEM_CACHE_CACHE_HH__
5311051Sandreas.hansson@arm.com#define __MEM_CACHE_CACHE_HH__
542810Srdreslin@umich.edu
5511859Sandreas.hansson@arm.com#include <unordered_set>
5611859Sandreas.hansson@arm.com
5712334Sgabeblack@google.com#include "base/logging.hh" // fatal, panic, and warn
5811197Sandreas.hansson@arm.com#include "enums/Clusivity.hh"
595338Sstever@gmail.com#include "mem/cache/base.hh"
605338Sstever@gmail.com#include "mem/cache/blk.hh"
615338Sstever@gmail.com#include "mem/cache/mshr.hh"
6210815Sdavid.guillen@arm.com#include "mem/cache/tags/base.hh"
6311053Sandreas.hansson@arm.com#include "params/Cache.hh"
644458Sstever@eecs.umich.edu#include "sim/eventq.hh"
654458Sstever@eecs.umich.edu
662813Srdreslin@umich.edu//Forward decleration
673861Sstever@eecs.umich.educlass BasePrefetcher;
682810Srdreslin@umich.edu
692810Srdreslin@umich.edu/**
702810Srdreslin@umich.edu * A template-policy based cache. The behavior of the cache can be altered by
712810Srdreslin@umich.edu * supplying different template policies. TagStore handles all tag and data
729264Sdjordje.kovacevic@arm.com * storage @sa TagStore, \ref gem5MemorySystem "gem5 Memory System"
732810Srdreslin@umich.edu */
742810Srdreslin@umich.educlass Cache : public BaseCache
752810Srdreslin@umich.edu{
762810Srdreslin@umich.edu  protected:
772810Srdreslin@umich.edu
788856Sandreas.hansson@arm.com    /**
798856Sandreas.hansson@arm.com     * The CPU-side port extends the base cache slave port with access
808856Sandreas.hansson@arm.com     * functions for functional, atomic and timing requests.
818856Sandreas.hansson@arm.com     */
828856Sandreas.hansson@arm.com    class CpuSidePort : public CacheSlavePort
833738Sstever@eecs.umich.edu    {
848856Sandreas.hansson@arm.com      private:
853738Sstever@eecs.umich.edu
868856Sandreas.hansson@arm.com        // a pointer to our specific cache implementation
8710815Sdavid.guillen@arm.com        Cache *cache;
883738Sstever@eecs.umich.edu
898856Sandreas.hansson@arm.com      protected:
904478Sstever@eecs.umich.edu
918975Sandreas.hansson@arm.com        virtual bool recvTimingSnoopResp(PacketPtr pkt);
928948Sandreas.hansson@arm.com
9312343Snikos.nikoleris@arm.com        virtual bool tryTiming(PacketPtr pkt);
9412343Snikos.nikoleris@arm.com
958975Sandreas.hansson@arm.com        virtual bool recvTimingReq(PacketPtr pkt);
963738Sstever@eecs.umich.edu
973738Sstever@eecs.umich.edu        virtual Tick recvAtomic(PacketPtr pkt);
983738Sstever@eecs.umich.edu
993738Sstever@eecs.umich.edu        virtual void recvFunctional(PacketPtr pkt);
1008856Sandreas.hansson@arm.com
1019090Sandreas.hansson@arm.com        virtual AddrRangeList getAddrRanges() const;
1028856Sandreas.hansson@arm.com
1038856Sandreas.hansson@arm.com      public:
1048856Sandreas.hansson@arm.com
10510815Sdavid.guillen@arm.com        CpuSidePort(const std::string &_name, Cache *_cache,
1068856Sandreas.hansson@arm.com                    const std::string &_label);
1078856Sandreas.hansson@arm.com
1083738Sstever@eecs.umich.edu    };
1093738Sstever@eecs.umich.edu
1108856Sandreas.hansson@arm.com    /**
1118914Sandreas.hansson@arm.com     * Override the default behaviour of sendDeferredPacket to enable
1128914Sandreas.hansson@arm.com     * the memory-side cache port to also send requests based on the
1138914Sandreas.hansson@arm.com     * current MSHR status. This queue has a pointer to our specific
1148914Sandreas.hansson@arm.com     * cache implementation and is used by the MemSidePort.
1158914Sandreas.hansson@arm.com     */
11610713Sandreas.hansson@arm.com    class CacheReqPacketQueue : public ReqPacketQueue
1178914Sandreas.hansson@arm.com    {
1188914Sandreas.hansson@arm.com
1198914Sandreas.hansson@arm.com      protected:
1208914Sandreas.hansson@arm.com
12110815Sdavid.guillen@arm.com        Cache &cache;
12210713Sandreas.hansson@arm.com        SnoopRespPacketQueue &snoopRespQueue;
1238914Sandreas.hansson@arm.com
1248914Sandreas.hansson@arm.com      public:
1258914Sandreas.hansson@arm.com
12610815Sdavid.guillen@arm.com        CacheReqPacketQueue(Cache &cache, MasterPort &port,
12710713Sandreas.hansson@arm.com                            SnoopRespPacketQueue &snoop_resp_queue,
12810713Sandreas.hansson@arm.com                            const std::string &label) :
12910713Sandreas.hansson@arm.com            ReqPacketQueue(cache, port, label), cache(cache),
13010713Sandreas.hansson@arm.com            snoopRespQueue(snoop_resp_queue) { }
1318914Sandreas.hansson@arm.com
1328914Sandreas.hansson@arm.com        /**
1338914Sandreas.hansson@arm.com         * Override the normal sendDeferredPacket and do not only
1348914Sandreas.hansson@arm.com         * consider the transmit list (used for responses), but also
1358914Sandreas.hansson@arm.com         * requests.
1368914Sandreas.hansson@arm.com         */
1378914Sandreas.hansson@arm.com        virtual void sendDeferredPacket();
1388914Sandreas.hansson@arm.com
13911375Sandreas.hansson@arm.com        /**
14011375Sandreas.hansson@arm.com         * Check if there is a conflicting snoop response about to be
14111375Sandreas.hansson@arm.com         * send out, and if so simply stall any requests, and schedule
14211375Sandreas.hansson@arm.com         * a send event at the same time as the next snoop response is
14311375Sandreas.hansson@arm.com         * being sent out.
14411375Sandreas.hansson@arm.com         */
14511375Sandreas.hansson@arm.com        bool checkConflictingSnoop(Addr addr)
14611375Sandreas.hansson@arm.com        {
14711375Sandreas.hansson@arm.com            if (snoopRespQueue.hasAddr(addr)) {
14811375Sandreas.hansson@arm.com                DPRINTF(CachePort, "Waiting for snoop response to be "
14911375Sandreas.hansson@arm.com                        "sent\n");
15011375Sandreas.hansson@arm.com                Tick when = snoopRespQueue.deferredPacketReadyTime();
15111375Sandreas.hansson@arm.com                schedSendEvent(when);
15211375Sandreas.hansson@arm.com                return true;
15311375Sandreas.hansson@arm.com            }
15411375Sandreas.hansson@arm.com            return false;
15511375Sandreas.hansson@arm.com        }
1568914Sandreas.hansson@arm.com    };
1578914Sandreas.hansson@arm.com
1588914Sandreas.hansson@arm.com    /**
1598856Sandreas.hansson@arm.com     * The memory-side port extends the base cache master port with
1608856Sandreas.hansson@arm.com     * access functions for functional, atomic and timing snoops.
1618856Sandreas.hansson@arm.com     */
1628856Sandreas.hansson@arm.com    class MemSidePort : public CacheMasterPort
1633738Sstever@eecs.umich.edu    {
1648856Sandreas.hansson@arm.com      private:
1653738Sstever@eecs.umich.edu
1668914Sandreas.hansson@arm.com        /** The cache-specific queue. */
16710713Sandreas.hansson@arm.com        CacheReqPacketQueue _reqQueue;
16810713Sandreas.hansson@arm.com
16910713Sandreas.hansson@arm.com        SnoopRespPacketQueue _snoopRespQueue;
1708914Sandreas.hansson@arm.com
1718856Sandreas.hansson@arm.com        // a pointer to our specific cache implementation
17210815Sdavid.guillen@arm.com        Cache *cache;
1733738Sstever@eecs.umich.edu
1748856Sandreas.hansson@arm.com      protected:
1754478Sstever@eecs.umich.edu
1768975Sandreas.hansson@arm.com        virtual void recvTimingSnoopReq(PacketPtr pkt);
1778948Sandreas.hansson@arm.com
1788975Sandreas.hansson@arm.com        virtual bool recvTimingResp(PacketPtr pkt);
1793738Sstever@eecs.umich.edu
1808948Sandreas.hansson@arm.com        virtual Tick recvAtomicSnoop(PacketPtr pkt);
1813738Sstever@eecs.umich.edu
1828948Sandreas.hansson@arm.com        virtual void recvFunctionalSnoop(PacketPtr pkt);
1834458Sstever@eecs.umich.edu
1848856Sandreas.hansson@arm.com      public:
1858856Sandreas.hansson@arm.com
18610815Sdavid.guillen@arm.com        MemSidePort(const std::string &_name, Cache *_cache,
1878856Sandreas.hansson@arm.com                    const std::string &_label);
1883738Sstever@eecs.umich.edu    };
1893738Sstever@eecs.umich.edu
1902810Srdreslin@umich.edu    /** Tag and data Storage */
19110815Sdavid.guillen@arm.com    BaseTags *tags;
1924626Sstever@eecs.umich.edu
1932810Srdreslin@umich.edu    /** Prefetcher */
1943861Sstever@eecs.umich.edu    BasePrefetcher *prefetcher;
1952810Srdreslin@umich.edu
1964671Sstever@eecs.umich.edu    /** Temporary cache block for occasional transitory use */
19710815Sdavid.guillen@arm.com    CacheBlk *tempBlock;
1984671Sstever@eecs.umich.edu
1992810Srdreslin@umich.edu    /**
2005707Shsul@eecs.umich.edu     * This cache should allocate a block on a line-sized write miss.
2013860Sstever@eecs.umich.edu     */
2023860Sstever@eecs.umich.edu    const bool doFastWrites;
2033860Sstever@eecs.umich.edu
2045875Ssteve.reinhardt@amd.com    /**
20510345SCurtis.Dunham@arm.com     * Turn line-sized writes into WriteInvalidate transactions.
20610345SCurtis.Dunham@arm.com     */
20710345SCurtis.Dunham@arm.com    void promoteWholeLineWrites(PacketPtr pkt);
20810345SCurtis.Dunham@arm.com
20910345SCurtis.Dunham@arm.com    /**
2105875Ssteve.reinhardt@amd.com     * Notify the prefetcher on every access, not just misses.
2115875Ssteve.reinhardt@amd.com     */
2125875Ssteve.reinhardt@amd.com    const bool prefetchOnAccess;
2133860Sstever@eecs.umich.edu
21411197Sandreas.hansson@arm.com     /**
21511197Sandreas.hansson@arm.com     * Clusivity with respect to the upstream cache, determining if we
21611197Sandreas.hansson@arm.com     * fill into both this cache and the cache above on a miss. Note
21711197Sandreas.hansson@arm.com     * that we currently do not support strict clusivity policies.
21811197Sandreas.hansson@arm.com     */
21911197Sandreas.hansson@arm.com    const Enums::Clusivity clusivity;
22011197Sandreas.hansson@arm.com
22111199Sandreas.hansson@arm.com     /**
22211199Sandreas.hansson@arm.com     * Determine if clean lines should be written back or not. In
22311199Sandreas.hansson@arm.com     * cases where a downstream cache is mostly inclusive we likely
22411199Sandreas.hansson@arm.com     * want it to act as a victim cache also for lines that have not
22511199Sandreas.hansson@arm.com     * been modified. Hence, we cannot simply drop the line (or send a
22611199Sandreas.hansson@arm.com     * clean evict), but rather need to send the actual data.
22711199Sandreas.hansson@arm.com     */
22811199Sandreas.hansson@arm.com    const bool writebackClean;
22911199Sandreas.hansson@arm.com
2303860Sstever@eecs.umich.edu    /**
23111190Sandreas.hansson@arm.com     * Upstream caches need this packet until true is returned, so
23211190Sandreas.hansson@arm.com     * hold it for deletion until a subsequent call
2339063SAli.Saidi@ARM.com     */
23411190Sandreas.hansson@arm.com    std::unique_ptr<Packet> pendingDelete;
2359063SAli.Saidi@ARM.com
2369063SAli.Saidi@ARM.com    /**
23711197Sandreas.hansson@arm.com     * Writebacks from the tempBlock, resulting on the response path
23811197Sandreas.hansson@arm.com     * in atomic mode, must happen after the call to recvAtomic has
23911197Sandreas.hansson@arm.com     * finished (for the right ordering of the packets). We therefore
24011197Sandreas.hansson@arm.com     * need to hold on to the packets, and have a method and an event
24111197Sandreas.hansson@arm.com     * to send them.
24211197Sandreas.hansson@arm.com     */
24311197Sandreas.hansson@arm.com    PacketPtr tempBlockWriteback;
24411197Sandreas.hansson@arm.com
24511197Sandreas.hansson@arm.com    /**
24611197Sandreas.hansson@arm.com     * Send the outstanding tempBlock writeback. To be called after
24711197Sandreas.hansson@arm.com     * recvAtomic finishes in cases where the block we filled is in
24811197Sandreas.hansson@arm.com     * fact the tempBlock, and now needs to be written back.
24911197Sandreas.hansson@arm.com     */
25011197Sandreas.hansson@arm.com    void writebackTempBlockAtomic() {
25111197Sandreas.hansson@arm.com        assert(tempBlockWriteback != nullptr);
25211197Sandreas.hansson@arm.com        PacketList writebacks{tempBlockWriteback};
25311197Sandreas.hansson@arm.com        doWritebacksAtomic(writebacks);
25411197Sandreas.hansson@arm.com        tempBlockWriteback = nullptr;
25511197Sandreas.hansson@arm.com    }
25611197Sandreas.hansson@arm.com
25711197Sandreas.hansson@arm.com    /**
25811197Sandreas.hansson@arm.com     * An event to writeback the tempBlock after recvAtomic
25911197Sandreas.hansson@arm.com     * finishes. To avoid other calls to recvAtomic getting in
26011197Sandreas.hansson@arm.com     * between, we create this event with a higher priority.
26111197Sandreas.hansson@arm.com     */
26212084Sspwilson2@wisc.edu    EventFunctionWrapper writebackTempBlockAtomicEvent;
26311197Sandreas.hansson@arm.com
26411197Sandreas.hansson@arm.com    /**
26511276Sandreas.hansson@arm.com     * Store the outstanding requests that we are expecting snoop
26611276Sandreas.hansson@arm.com     * responses from so we can determine which snoop responses we
26711276Sandreas.hansson@arm.com     * generated and which ones were merely forwarded.
26811276Sandreas.hansson@arm.com     */
26911276Sandreas.hansson@arm.com    std::unordered_set<RequestPtr> outstandingSnoop;
27011276Sandreas.hansson@arm.com
27111276Sandreas.hansson@arm.com    /**
2723860Sstever@eecs.umich.edu     * Does all the processing necessary to perform the provided request.
2733860Sstever@eecs.umich.edu     * @param pkt The memory request to perform.
27410048Saminfar@gmail.com     * @param blk The cache block to be updated.
2753860Sstever@eecs.umich.edu     * @param lat The latency of the access.
2763860Sstever@eecs.umich.edu     * @param writebacks List for any writebacks that need to be performed.
2775707Shsul@eecs.umich.edu     * @return Boolean indicating whether the request was satisfied.
2783860Sstever@eecs.umich.edu     */
27910815Sdavid.guillen@arm.com    bool access(PacketPtr pkt, CacheBlk *&blk,
2809288Sandreas.hansson@arm.com                Cycles &lat, PacketList &writebacks);
2814219Srdreslin@umich.edu
2824219Srdreslin@umich.edu    /**
2834219Srdreslin@umich.edu     *Handle doing the Compare and Swap function for SPARC.
2844219Srdreslin@umich.edu     */
28510815Sdavid.guillen@arm.com    void cmpAndSwap(CacheBlk *blk, PacketPtr pkt);
2863860Sstever@eecs.umich.edu
2873860Sstever@eecs.umich.edu    /**
28810028SGiacomo.Gabrielli@arm.com     * Find a block frame for new block at address addr targeting the
28910028SGiacomo.Gabrielli@arm.com     * given security space, assuming that the block is not currently
29010028SGiacomo.Gabrielli@arm.com     * in the cache.  Append writebacks if any to provided packet
29111484Snikos.nikoleris@arm.com     * list.  Return free block frame.  May return nullptr if there are
29210028SGiacomo.Gabrielli@arm.com     * no replaceable blocks at the moment.
2935350Sstever@gmail.com     */
29410815Sdavid.guillen@arm.com    CacheBlk *allocateBlock(Addr addr, bool is_secure, PacketList &writebacks);
2955350Sstever@gmail.com
2965350Sstever@gmail.com    /**
29711197Sandreas.hansson@arm.com     * Invalidate a cache block.
29811197Sandreas.hansson@arm.com     *
29911197Sandreas.hansson@arm.com     * @param blk Block to invalidate
30011197Sandreas.hansson@arm.com     */
30111197Sandreas.hansson@arm.com    void invalidateBlock(CacheBlk *blk);
30211197Sandreas.hansson@arm.com
30311197Sandreas.hansson@arm.com    /**
30411601Sandreas.hansson@arm.com     * Maintain the clusivity of this cache by potentially
30511601Sandreas.hansson@arm.com     * invalidating a block. This method works in conjunction with
30611601Sandreas.hansson@arm.com     * satisfyRequest, but is separate to allow us to handle all MSHR
30711601Sandreas.hansson@arm.com     * targets before potentially dropping a block.
30811601Sandreas.hansson@arm.com     *
30911601Sandreas.hansson@arm.com     * @param from_cache Whether we have dealt with a packet from a cache
31011601Sandreas.hansson@arm.com     * @param blk The block that should potentially be dropped
31111601Sandreas.hansson@arm.com     */
31211601Sandreas.hansson@arm.com    void maintainClusivity(bool from_cache, CacheBlk *blk);
31311601Sandreas.hansson@arm.com
31411601Sandreas.hansson@arm.com    /**
3153860Sstever@eecs.umich.edu     * Populates a cache block and handles all outstanding requests for the
3163860Sstever@eecs.umich.edu     * satisfied fill request. This version takes two memory requests. One
3173860Sstever@eecs.umich.edu     * contains the fill data, the other is an optional target to satisfy.
3184626Sstever@eecs.umich.edu     * @param pkt The memory request with the fill data.
3193860Sstever@eecs.umich.edu     * @param blk The cache block if it already exists.
3203860Sstever@eecs.umich.edu     * @param writebacks List for any writebacks that need to be performed.
32111197Sandreas.hansson@arm.com     * @param allocate Whether to allocate a block or use the temp block
3223860Sstever@eecs.umich.edu     * @return Pointer to the new cache block.
3233860Sstever@eecs.umich.edu     */
32410815Sdavid.guillen@arm.com    CacheBlk *handleFill(PacketPtr pkt, CacheBlk *blk,
32511197Sandreas.hansson@arm.com                         PacketList &writebacks, bool allocate);
3263860Sstever@eecs.umich.edu
32711197Sandreas.hansson@arm.com    /**
32811197Sandreas.hansson@arm.com     * Determine whether we should allocate on a fill or not. If this
32911197Sandreas.hansson@arm.com     * cache is mostly inclusive with regards to the upstream cache(s)
33011197Sandreas.hansson@arm.com     * we always allocate (for any non-forwarded and cacheable
33111197Sandreas.hansson@arm.com     * requests). In the case of a mostly exclusive cache, we allocate
33211197Sandreas.hansson@arm.com     * on fill if the packet did not come from a cache, thus if we:
33311197Sandreas.hansson@arm.com     * are dealing with a whole-line write (the latter behaves much
33411197Sandreas.hansson@arm.com     * like a writeback), the original target packet came from a
33511197Sandreas.hansson@arm.com     * non-caching source, or if we are performing a prefetch or LLSC.
33611197Sandreas.hansson@arm.com     *
33711197Sandreas.hansson@arm.com     * @param cmd Command of the incoming requesting packet
33811197Sandreas.hansson@arm.com     * @return Whether we should allocate on the fill
33911197Sandreas.hansson@arm.com     */
34011211Sandreas.sandberg@arm.com    inline bool allocOnFill(MemCmd cmd) const override
34111197Sandreas.hansson@arm.com    {
34211197Sandreas.hansson@arm.com        return clusivity == Enums::mostly_incl ||
34311197Sandreas.hansson@arm.com            cmd == MemCmd::WriteLineReq ||
34411197Sandreas.hansson@arm.com            cmd == MemCmd::ReadReq ||
34511197Sandreas.hansson@arm.com            cmd == MemCmd::WriteReq ||
34611197Sandreas.hansson@arm.com            cmd.isPrefetch() ||
34711197Sandreas.hansson@arm.com            cmd.isLLSC();
34811197Sandreas.hansson@arm.com    }
3499548Sandreas.hansson@arm.com
3509548Sandreas.hansson@arm.com    /**
3519548Sandreas.hansson@arm.com     * Performs the access specified by the request.
3529548Sandreas.hansson@arm.com     * @param pkt The request to perform.
3539548Sandreas.hansson@arm.com     */
35412630Snikos.nikoleris@arm.com    void recvTimingReq(PacketPtr pkt);
3559548Sandreas.hansson@arm.com
3569548Sandreas.hansson@arm.com    /**
35710883Sali.jafri@arm.com     * Insert writebacks into the write buffer
35810883Sali.jafri@arm.com     */
35910883Sali.jafri@arm.com    void doWritebacks(PacketList& writebacks, Tick forward_time);
36010883Sali.jafri@arm.com
36110883Sali.jafri@arm.com    /**
36211130Sali.jafri@arm.com     * Send writebacks down the memory hierarchy in atomic mode
36311130Sali.jafri@arm.com     */
36411130Sali.jafri@arm.com    void doWritebacksAtomic(PacketList& writebacks);
36511130Sali.jafri@arm.com
36611130Sali.jafri@arm.com    /**
36711375Sandreas.hansson@arm.com     * Handling the special case of uncacheable write responses to
36811375Sandreas.hansson@arm.com     * make recvTimingResp less cluttered.
36911375Sandreas.hansson@arm.com     */
37011375Sandreas.hansson@arm.com    void handleUncacheableWriteResp(PacketPtr pkt);
37111375Sandreas.hansson@arm.com
37211375Sandreas.hansson@arm.com    /**
37312719Snikos.nikoleris@arm.com     * Service non-deferred MSHR targets using the received response
37412719Snikos.nikoleris@arm.com     *
37512719Snikos.nikoleris@arm.com     * Iterates through the list of targets that can be serviced with
37612719Snikos.nikoleris@arm.com     * the current response. Any writebacks that need to performed
37712719Snikos.nikoleris@arm.com     * must be appended to the writebacks parameter.
37812719Snikos.nikoleris@arm.com     *
37912719Snikos.nikoleris@arm.com     * @param mshr The MSHR that corresponds to the reponse
38012719Snikos.nikoleris@arm.com     * @param pkt The response packet
38112719Snikos.nikoleris@arm.com     * @param blk The reference block
38212719Snikos.nikoleris@arm.com     * @param writebacks List of writebacks that need to be performed
38312719Snikos.nikoleris@arm.com     */
38412719Snikos.nikoleris@arm.com    void serviceMSHRTargets(MSHR *mshr, const PacketPtr pkt, CacheBlk *blk,
38512719Snikos.nikoleris@arm.com                            PacketList& writebacks);
38612719Snikos.nikoleris@arm.com
38712719Snikos.nikoleris@arm.com    /**
3889548Sandreas.hansson@arm.com     * Handles a response (cache line fill/write ack) from the bus.
3899548Sandreas.hansson@arm.com     * @param pkt The response packet
3909548Sandreas.hansson@arm.com     */
3919548Sandreas.hansson@arm.com    void recvTimingResp(PacketPtr pkt);
3929548Sandreas.hansson@arm.com
3939548Sandreas.hansson@arm.com    /**
3949548Sandreas.hansson@arm.com     * Snoops bus transactions to maintain coherence.
3959548Sandreas.hansson@arm.com     * @param pkt The current bus transaction.
3969548Sandreas.hansson@arm.com     */
3979548Sandreas.hansson@arm.com    void recvTimingSnoopReq(PacketPtr pkt);
3989548Sandreas.hansson@arm.com
3999548Sandreas.hansson@arm.com    /**
4009548Sandreas.hansson@arm.com     * Handle a snoop response.
4019548Sandreas.hansson@arm.com     * @param pkt Snoop response packet
4029548Sandreas.hansson@arm.com     */
4039548Sandreas.hansson@arm.com    void recvTimingSnoopResp(PacketPtr pkt);
4049548Sandreas.hansson@arm.com
4059548Sandreas.hansson@arm.com    /**
4069548Sandreas.hansson@arm.com     * Performs the access specified by the request.
4079548Sandreas.hansson@arm.com     * @param pkt The request to perform.
4089782Sandreas.hansson@arm.com     * @return The number of ticks required for the access.
4099548Sandreas.hansson@arm.com     */
4109782Sandreas.hansson@arm.com    Tick recvAtomic(PacketPtr pkt);
4119548Sandreas.hansson@arm.com
4129548Sandreas.hansson@arm.com    /**
4139548Sandreas.hansson@arm.com     * Snoop for the provided request in the cache and return the estimated
4149782Sandreas.hansson@arm.com     * time taken.
4159548Sandreas.hansson@arm.com     * @param pkt The memory request to snoop
4169782Sandreas.hansson@arm.com     * @return The number of ticks required for the snoop.
4179548Sandreas.hansson@arm.com     */
4189782Sandreas.hansson@arm.com    Tick recvAtomicSnoop(PacketPtr pkt);
4199548Sandreas.hansson@arm.com
4209548Sandreas.hansson@arm.com    /**
4219548Sandreas.hansson@arm.com     * Performs the access specified by the request.
4229548Sandreas.hansson@arm.com     * @param pkt The request to perform.
4239548Sandreas.hansson@arm.com     * @param fromCpuSide from the CPU side port or the memory side port
4249548Sandreas.hansson@arm.com     */
4259548Sandreas.hansson@arm.com    void functionalAccess(PacketPtr pkt, bool fromCpuSide);
4269548Sandreas.hansson@arm.com
42711601Sandreas.hansson@arm.com    /**
42811601Sandreas.hansson@arm.com     * Perform any necessary updates to the block and perform any data
42911601Sandreas.hansson@arm.com     * exchange between the packet and the block. The flags of the
43011601Sandreas.hansson@arm.com     * packet are also set accordingly.
43111601Sandreas.hansson@arm.com     *
43211601Sandreas.hansson@arm.com     * @param pkt Request packet from upstream that hit a block
43311601Sandreas.hansson@arm.com     * @param blk Cache block that the packet hit
43411601Sandreas.hansson@arm.com     * @param deferred_response Whether this hit is to block that
43511601Sandreas.hansson@arm.com     *                          originally missed
43611601Sandreas.hansson@arm.com     * @param pending_downgrade Whether the writable flag is to be removed
43711601Sandreas.hansson@arm.com     *
43811601Sandreas.hansson@arm.com     * @return True if the block is to be invalidated
43911601Sandreas.hansson@arm.com     */
44011601Sandreas.hansson@arm.com    void satisfyRequest(PacketPtr pkt, CacheBlk *blk,
44111601Sandreas.hansson@arm.com                        bool deferred_response = false,
44211601Sandreas.hansson@arm.com                        bool pending_downgrade = false);
4434626Sstever@eecs.umich.edu
44410563Sandreas.hansson@arm.com    void doTimingSupplyResponse(PacketPtr req_pkt, const uint8_t *blk_data,
4455319Sstever@gmail.com                                bool already_copied, bool pending_inval);
4463860Sstever@eecs.umich.edu
4473860Sstever@eecs.umich.edu    /**
44811127Sandreas.hansson@arm.com     * Perform an upward snoop if needed, and update the block state
44911127Sandreas.hansson@arm.com     * (possibly invalidating the block). Also create a response if required.
45011127Sandreas.hansson@arm.com     *
45111127Sandreas.hansson@arm.com     * @param pkt Snoop packet
45211127Sandreas.hansson@arm.com     * @param blk Cache block being snooped
45311127Sandreas.hansson@arm.com     * @param is_timing Timing or atomic for the response
45411127Sandreas.hansson@arm.com     * @param is_deferred Is this a deferred snoop or not?
45511127Sandreas.hansson@arm.com     * @param pending_inval Do we have a pending invalidation?
45611127Sandreas.hansson@arm.com     *
45711127Sandreas.hansson@arm.com     * @return The snoop delay incurred by the upwards snoop
4583860Sstever@eecs.umich.edu     */
45911127Sandreas.hansson@arm.com    uint32_t handleSnoop(PacketPtr pkt, CacheBlk *blk,
46011127Sandreas.hansson@arm.com                         bool is_timing, bool is_deferred, bool pending_inval);
4613860Sstever@eecs.umich.edu
4623860Sstever@eecs.umich.edu    /**
4633860Sstever@eecs.umich.edu     * Create a writeback request for the given block.
4643860Sstever@eecs.umich.edu     * @param blk The block to writeback.
4653860Sstever@eecs.umich.edu     * @return The writeback request for the block.
4663860Sstever@eecs.umich.edu     */
46710815Sdavid.guillen@arm.com    PacketPtr writebackBlk(CacheBlk *blk);
4683860Sstever@eecs.umich.edu
46910883Sali.jafri@arm.com    /**
47012345Snikos.nikoleris@arm.com     * Create a writeclean request for the given block.
47112345Snikos.nikoleris@arm.com     * @param blk The block to write clean
47212346Snikos.nikoleris@arm.com     * @param dest The destination of this clean operation
47312345Snikos.nikoleris@arm.com     * @return The write clean packet for the block.
47412345Snikos.nikoleris@arm.com     */
47512351Snikos.nikoleris@arm.com    PacketPtr writecleanBlk(CacheBlk *blk, Request::Flags dest, PacketId id);
47612345Snikos.nikoleris@arm.com
47712345Snikos.nikoleris@arm.com    /**
47810883Sali.jafri@arm.com     * Create a CleanEvict request for the given block.
47910883Sali.jafri@arm.com     * @param blk The block to evict.
48010883Sali.jafri@arm.com     * @return The CleanEvict request for the block.
48110883Sali.jafri@arm.com     */
48210883Sali.jafri@arm.com    PacketPtr cleanEvictBlk(CacheBlk *blk);
48310883Sali.jafri@arm.com
4849347SAndreas.Sandberg@arm.com
48511169Sandreas.hansson@arm.com    void memWriteback() override;
48611169Sandreas.hansson@arm.com    void memInvalidate() override;
48711169Sandreas.hansson@arm.com    bool isDirty() const override;
4889347SAndreas.Sandberg@arm.com
4899347SAndreas.Sandberg@arm.com    /**
4909347SAndreas.Sandberg@arm.com     * Cache block visitor that writes back dirty cache blocks using
4919347SAndreas.Sandberg@arm.com     * functional writes.
4929347SAndreas.Sandberg@arm.com     *
4939347SAndreas.Sandberg@arm.com     * \return Always returns true.
4949347SAndreas.Sandberg@arm.com     */
49510815Sdavid.guillen@arm.com    bool writebackVisitor(CacheBlk &blk);
4969347SAndreas.Sandberg@arm.com    /**
4979347SAndreas.Sandberg@arm.com     * Cache block visitor that invalidates all blocks in the cache.
4989347SAndreas.Sandberg@arm.com     *
4999347SAndreas.Sandberg@arm.com     * @warn Dirty cache lines will not be written back to memory.
5009347SAndreas.Sandberg@arm.com     *
5019347SAndreas.Sandberg@arm.com     * \return Always returns true.
5029347SAndreas.Sandberg@arm.com     */
50310815Sdavid.guillen@arm.com    bool invalidateVisitor(CacheBlk &blk);
5049347SAndreas.Sandberg@arm.com
5059445SAndreas.Sandberg@ARM.com    /**
50611452Sandreas.hansson@arm.com     * Create an appropriate downstream bus request packet for the
5075365Sstever@gmail.com     * given parameters.
50811452Sandreas.hansson@arm.com     * @param cpu_pkt  The miss that needs to be satisfied.
5095365Sstever@gmail.com     * @param blk The block currently in the cache corresponding to
51011484Snikos.nikoleris@arm.com     * cpu_pkt (nullptr if none).
51111452Sandreas.hansson@arm.com     * @param needsWritable Indicates that the block must be writable
5125365Sstever@gmail.com     * even if the request in cpu_pkt doesn't indicate that.
51311484Snikos.nikoleris@arm.com     * @return A new Packet containing the request, or nullptr if the
5145365Sstever@gmail.com     * current request in cpu_pkt should just be forwarded on.
5154626Sstever@eecs.umich.edu     */
51611452Sandreas.hansson@arm.com    PacketPtr createMissPacket(PacketPtr cpu_pkt, CacheBlk *blk,
51711452Sandreas.hansson@arm.com                               bool needsWritable) const;
5185365Sstever@gmail.com
5195365Sstever@gmail.com    /**
52011375Sandreas.hansson@arm.com     * Return the next queue entry to service, either a pending miss
52111375Sandreas.hansson@arm.com     * from the MSHR queue, a buffered write from the write buffer, or
52211375Sandreas.hansson@arm.com     * something from the prefetcher. This function is responsible
52311375Sandreas.hansson@arm.com     * for prioritizing among those sources on the fly.
5245365Sstever@gmail.com     */
52511375Sandreas.hansson@arm.com    QueueEntry* getNextQueueEntry();
5265365Sstever@gmail.com
5275365Sstever@gmail.com    /**
52810883Sali.jafri@arm.com     * Send up a snoop request and find cached copies. If cached copies are
52910883Sali.jafri@arm.com     * found, set the BLOCK_CACHED flag in pkt.
53010883Sali.jafri@arm.com     */
53111130Sali.jafri@arm.com    bool isCachedAbove(PacketPtr pkt, bool is_timing = true) const;
53210883Sali.jafri@arm.com
53310883Sali.jafri@arm.com    /**
5344626Sstever@eecs.umich.edu     * Return whether there are any outstanding misses.
5354626Sstever@eecs.umich.edu     */
5364626Sstever@eecs.umich.edu    bool outstandingMisses() const
5372810Srdreslin@umich.edu    {
53811375Sandreas.hansson@arm.com        return !mshrQueue.isEmpty();
5392810Srdreslin@umich.edu    }
5402810Srdreslin@umich.edu
54110028SGiacomo.Gabrielli@arm.com    CacheBlk *findBlock(Addr addr, bool is_secure) const {
54210028SGiacomo.Gabrielli@arm.com        return tags->findBlock(addr, is_secure);
5432810Srdreslin@umich.edu    }
5442810Srdreslin@umich.edu
54511169Sandreas.hansson@arm.com    bool inCache(Addr addr, bool is_secure) const override {
54610028SGiacomo.Gabrielli@arm.com        return (tags->findBlock(addr, is_secure) != 0);
5473861Sstever@eecs.umich.edu    }
5483861Sstever@eecs.umich.edu
54911169Sandreas.hansson@arm.com    bool inMissQueue(Addr addr, bool is_secure) const override {
55010028SGiacomo.Gabrielli@arm.com        return (mshrQueue.findMatch(addr, is_secure) != 0);
5513861Sstever@eecs.umich.edu    }
5525875Ssteve.reinhardt@amd.com
5535875Ssteve.reinhardt@amd.com    /**
5545875Ssteve.reinhardt@amd.com     * Find next request ready time from among possible sources.
5555875Ssteve.reinhardt@amd.com     */
55611375Sandreas.hansson@arm.com    Tick nextQueueReadyTime() const;
5579529Sandreas.hansson@arm.com
5589529Sandreas.hansson@arm.com  public:
5599529Sandreas.hansson@arm.com    /** Instantiates a basic cache object. */
56011053Sandreas.hansson@arm.com    Cache(const CacheParams *p);
5619529Sandreas.hansson@arm.com
5629813Srioshering@gmail.com    /** Non-default destructor is needed to deallocate memory. */
5639813Srioshering@gmail.com    virtual ~Cache();
5649813Srioshering@gmail.com
56511169Sandreas.hansson@arm.com    void regStats() override;
5668985SAli.Saidi@ARM.com
56711375Sandreas.hansson@arm.com    /**
56811375Sandreas.hansson@arm.com     * Take an MSHR, turn it into a suitable downstream packet, and
56911375Sandreas.hansson@arm.com     * send it out. This construct allows a queue entry to choose a suitable
57011375Sandreas.hansson@arm.com     * approach based on its type.
57111375Sandreas.hansson@arm.com     *
57211375Sandreas.hansson@arm.com     * @param mshr The MSHR to turn into a packet and send
57311375Sandreas.hansson@arm.com     * @return True if the port is waiting for a retry
57411375Sandreas.hansson@arm.com     */
57511375Sandreas.hansson@arm.com    bool sendMSHRQueuePacket(MSHR* mshr);
57611375Sandreas.hansson@arm.com
57711375Sandreas.hansson@arm.com    /**
57811375Sandreas.hansson@arm.com     * Similar to sendMSHR, but for a write-queue entry
57911375Sandreas.hansson@arm.com     * instead. Create the packet, and send it, and if successful also
58011375Sandreas.hansson@arm.com     * mark the entry in service.
58111375Sandreas.hansson@arm.com     *
58211375Sandreas.hansson@arm.com     * @param wq_entry The write-queue entry to turn into a packet and send
58311375Sandreas.hansson@arm.com     * @return True if the port is waiting for a retry
58411375Sandreas.hansson@arm.com     */
58511375Sandreas.hansson@arm.com    bool sendWriteQueuePacket(WriteQueueEntry* wq_entry);
58611375Sandreas.hansson@arm.com
5878985SAli.Saidi@ARM.com    /** serialize the state of the caches
5888985SAli.Saidi@ARM.com     * We currently don't support checkpointing cache state, so this panics.
5898985SAli.Saidi@ARM.com     */
59011168Sandreas.hansson@arm.com    void serialize(CheckpointOut &cp) const override;
59111168Sandreas.hansson@arm.com    void unserialize(CheckpointIn &cp) override;
5922810Srdreslin@umich.edu};
5932810Srdreslin@umich.edu
59410815Sdavid.guillen@arm.com/**
59510815Sdavid.guillen@arm.com * Wrap a method and present it as a cache block visitor.
59610815Sdavid.guillen@arm.com *
59710815Sdavid.guillen@arm.com * For example the forEachBlk method in the tag arrays expects a
59810815Sdavid.guillen@arm.com * callable object/function as their parameter. This class wraps a
59910815Sdavid.guillen@arm.com * method in an object and presents  callable object that adheres to
60010815Sdavid.guillen@arm.com * the cache block visitor protocol.
60110815Sdavid.guillen@arm.com */
60210815Sdavid.guillen@arm.comclass CacheBlkVisitorWrapper : public CacheBlkVisitor
60310815Sdavid.guillen@arm.com{
60410815Sdavid.guillen@arm.com  public:
60510815Sdavid.guillen@arm.com    typedef bool (Cache::*VisitorPtr)(CacheBlk &blk);
60610815Sdavid.guillen@arm.com
60710815Sdavid.guillen@arm.com    CacheBlkVisitorWrapper(Cache &_cache, VisitorPtr _visitor)
60810815Sdavid.guillen@arm.com        : cache(_cache), visitor(_visitor) {}
60910815Sdavid.guillen@arm.com
61011168Sandreas.hansson@arm.com    bool operator()(CacheBlk &blk) override {
61110815Sdavid.guillen@arm.com        return (cache.*visitor)(blk);
61210815Sdavid.guillen@arm.com    }
61310815Sdavid.guillen@arm.com
61410815Sdavid.guillen@arm.com  private:
61510815Sdavid.guillen@arm.com    Cache &cache;
61610815Sdavid.guillen@arm.com    VisitorPtr visitor;
61710815Sdavid.guillen@arm.com};
61810815Sdavid.guillen@arm.com
61910815Sdavid.guillen@arm.com/**
62010815Sdavid.guillen@arm.com * Cache block visitor that determines if there are dirty blocks in a
62110815Sdavid.guillen@arm.com * cache.
62210815Sdavid.guillen@arm.com *
62310815Sdavid.guillen@arm.com * Use with the forEachBlk method in the tag array to determine if the
62410815Sdavid.guillen@arm.com * array contains dirty blocks.
62510815Sdavid.guillen@arm.com */
62610815Sdavid.guillen@arm.comclass CacheBlkIsDirtyVisitor : public CacheBlkVisitor
62710815Sdavid.guillen@arm.com{
62810815Sdavid.guillen@arm.com  public:
62910815Sdavid.guillen@arm.com    CacheBlkIsDirtyVisitor()
63010815Sdavid.guillen@arm.com        : _isDirty(false) {}
63110815Sdavid.guillen@arm.com
63211168Sandreas.hansson@arm.com    bool operator()(CacheBlk &blk) override {
63310815Sdavid.guillen@arm.com        if (blk.isDirty()) {
63410815Sdavid.guillen@arm.com            _isDirty = true;
63510815Sdavid.guillen@arm.com            return false;
63610815Sdavid.guillen@arm.com        } else {
63710815Sdavid.guillen@arm.com            return true;
63810815Sdavid.guillen@arm.com        }
63910815Sdavid.guillen@arm.com    }
64010815Sdavid.guillen@arm.com
64110815Sdavid.guillen@arm.com    /**
64210815Sdavid.guillen@arm.com     * Does the array contain a dirty line?
64310815Sdavid.guillen@arm.com     *
64410815Sdavid.guillen@arm.com     * \return true if yes, false otherwise.
64510815Sdavid.guillen@arm.com     */
64610815Sdavid.guillen@arm.com    bool isDirty() const { return _isDirty; };
64710815Sdavid.guillen@arm.com
64810815Sdavid.guillen@arm.com  private:
64910815Sdavid.guillen@arm.com    bool _isDirty;
65010815Sdavid.guillen@arm.com};
65110815Sdavid.guillen@arm.com
65211051Sandreas.hansson@arm.com#endif // __MEM_CACHE_CACHE_HH__
653