base.hh revision 10713
12810SN/A/*
210693SMarco.Balboni@ARM.com * Copyright (c) 2012-2013, 2015 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
414458SN/A *          Steve Reinhardt
424458SN/A *          Ron Dreslinski
432810SN/A */
442810SN/A
452810SN/A/**
462810SN/A * @file
472810SN/A * Declares a basic cache interface BaseCache.
482810SN/A */
492810SN/A
502810SN/A#ifndef __BASE_CACHE_HH__
512810SN/A#define __BASE_CACHE_HH__
522810SN/A
537676Snate@binkert.org#include <algorithm>
547676Snate@binkert.org#include <list>
557676Snate@binkert.org#include <string>
562810SN/A#include <vector>
572810SN/A
582825SN/A#include "base/misc.hh"
592810SN/A#include "base/statistics.hh"
602810SN/A#include "base/trace.hh"
616215Snate@binkert.org#include "base/types.hh"
628232Snate@binkert.org#include "debug/Cache.hh"
638232Snate@binkert.org#include "debug/CachePort.hh"
645338Sstever@gmail.com#include "mem/cache/mshr_queue.hh"
652810SN/A#include "mem/mem_object.hh"
662810SN/A#include "mem/packet.hh"
678914Sandreas.hansson@arm.com#include "mem/qport.hh"
688229Snate@binkert.org#include "mem/request.hh"
695034SN/A#include "params/BaseCache.hh"
702811SN/A#include "sim/eventq.hh"
718786Sgblack@eecs.umich.edu#include "sim/full_system.hh"
724626SN/A#include "sim/sim_exit.hh"
738833Sdam.sunwoo@arm.com#include "sim/system.hh"
742810SN/A
753194SN/Aclass MSHR;
762810SN/A/**
772810SN/A * A basic cache interface. Implements some common functions for speed.
782810SN/A */
792810SN/Aclass BaseCache : public MemObject
802810SN/A{
814628SN/A    /**
824628SN/A     * Indexes to enumerate the MSHR queues.
834628SN/A     */
844628SN/A    enum MSHRQueueIndex {
854628SN/A        MSHRQueue_MSHRs,
864628SN/A        MSHRQueue_WriteBuffer
874628SN/A    };
884628SN/A
898737Skoansin.tan@gmail.com  public:
904628SN/A    /**
914628SN/A     * Reasons for caches to be blocked.
924628SN/A     */
934628SN/A    enum BlockedCause {
944628SN/A        Blocked_NoMSHRs = MSHRQueue_MSHRs,
954628SN/A        Blocked_NoWBBuffers = MSHRQueue_WriteBuffer,
964628SN/A        Blocked_NoTargets,
974628SN/A        NUM_BLOCKED_CAUSES
984628SN/A    };
994628SN/A
1004628SN/A    /**
1014628SN/A     * Reasons for cache to request a bus.
1024628SN/A     */
1034628SN/A    enum RequestCause {
1044628SN/A        Request_MSHR = MSHRQueue_MSHRs,
1054628SN/A        Request_WB = MSHRQueue_WriteBuffer,
1064628SN/A        Request_PF,
1074628SN/A        NUM_REQUEST_CAUSES
1084628SN/A    };
1094628SN/A
1108737Skoansin.tan@gmail.com  protected:
1114628SN/A
1128856Sandreas.hansson@arm.com    /**
1138856Sandreas.hansson@arm.com     * A cache master port is used for the memory-side port of the
1148856Sandreas.hansson@arm.com     * cache, and in addition to the basic timing port that only sends
1158856Sandreas.hansson@arm.com     * response packets through a transmit list, it also offers the
1168856Sandreas.hansson@arm.com     * ability to schedule and send request packets (requests &
1178856Sandreas.hansson@arm.com     * writebacks). The send event is scheduled through requestBus,
1188856Sandreas.hansson@arm.com     * and the sendDeferredPacket of the timing port is modified to
1198856Sandreas.hansson@arm.com     * consider both the transmit list and the requests from the MSHR.
1208856Sandreas.hansson@arm.com     */
1218922Swilliam.wang@arm.com    class CacheMasterPort : public QueuedMasterPort
1222810SN/A    {
1238856Sandreas.hansson@arm.com
1242844SN/A      public:
1258856Sandreas.hansson@arm.com
1268856Sandreas.hansson@arm.com        /**
1278856Sandreas.hansson@arm.com         * Schedule a send of a request packet (from the MSHR). Note
12810713Sandreas.hansson@arm.com         * that we could already have a retry outstanding.
1298856Sandreas.hansson@arm.com         */
1308856Sandreas.hansson@arm.com        void requestBus(RequestCause cause, Tick time)
1318856Sandreas.hansson@arm.com        {
1328856Sandreas.hansson@arm.com            DPRINTF(CachePort, "Asserting bus request for cause %d\n", cause);
13310713Sandreas.hansson@arm.com            reqQueue.schedSendEvent(time);
1348856Sandreas.hansson@arm.com        }
1358856Sandreas.hansson@arm.com
1363738SN/A      protected:
1374458SN/A
1388856Sandreas.hansson@arm.com        CacheMasterPort(const std::string &_name, BaseCache *_cache,
13910713Sandreas.hansson@arm.com                        ReqPacketQueue &_reqQueue,
14010713Sandreas.hansson@arm.com                        SnoopRespPacketQueue &_snoopRespQueue) :
14110713Sandreas.hansson@arm.com            QueuedMasterPort(_name, _cache, _reqQueue, _snoopRespQueue)
1428914Sandreas.hansson@arm.com        { }
1432810SN/A
1448856Sandreas.hansson@arm.com        /**
1458856Sandreas.hansson@arm.com         * Memory-side port always snoops.
1468856Sandreas.hansson@arm.com         *
1478914Sandreas.hansson@arm.com         * @return always true
1488856Sandreas.hansson@arm.com         */
1498922Swilliam.wang@arm.com        virtual bool isSnooping() const { return true; }
1508856Sandreas.hansson@arm.com    };
1513013SN/A
1528856Sandreas.hansson@arm.com    /**
1538856Sandreas.hansson@arm.com     * A cache slave port is used for the CPU-side port of the cache,
1548856Sandreas.hansson@arm.com     * and it is basically a simple timing port that uses a transmit
1558856Sandreas.hansson@arm.com     * list for responses to the CPU (or connected master). In
1568856Sandreas.hansson@arm.com     * addition, it has the functionality to block the port for
1578856Sandreas.hansson@arm.com     * incoming requests. If blocked, the port will issue a retry once
1588856Sandreas.hansson@arm.com     * unblocked.
1598856Sandreas.hansson@arm.com     */
1608922Swilliam.wang@arm.com    class CacheSlavePort : public QueuedSlavePort
1618856Sandreas.hansson@arm.com    {
1625314SN/A
1632811SN/A      public:
1648856Sandreas.hansson@arm.com
1658856Sandreas.hansson@arm.com        /** Do not accept any new requests. */
1662810SN/A        void setBlocked();
1672810SN/A
1688856Sandreas.hansson@arm.com        /** Return to normal operation and accept new requests. */
1692810SN/A        void clearBlocked();
1702810SN/A
17110345SCurtis.Dunham@arm.com        bool isBlocked() const { return blocked; }
17210345SCurtis.Dunham@arm.com
1738856Sandreas.hansson@arm.com      protected:
1748856Sandreas.hansson@arm.com
1758856Sandreas.hansson@arm.com        CacheSlavePort(const std::string &_name, BaseCache *_cache,
1768856Sandreas.hansson@arm.com                       const std::string &_label);
1773606SN/A
1788914Sandreas.hansson@arm.com        /** A normal packet queue used to store responses. */
17910713Sandreas.hansson@arm.com        RespPacketQueue queue;
1808914Sandreas.hansson@arm.com
1812810SN/A        bool blocked;
1822810SN/A
1832897SN/A        bool mustSendRetry;
1842897SN/A
1858856Sandreas.hansson@arm.com      private:
1864458SN/A
18710344Sandreas.hansson@arm.com        void processSendRetry();
18810344Sandreas.hansson@arm.com
18910344Sandreas.hansson@arm.com        EventWrapper<CacheSlavePort,
19010344Sandreas.hansson@arm.com                     &CacheSlavePort::processSendRetry> sendRetryEvent;
1918856Sandreas.hansson@arm.com
1922811SN/A    };
1932810SN/A
1948856Sandreas.hansson@arm.com    CacheSlavePort *cpuSidePort;
1958856Sandreas.hansson@arm.com    CacheMasterPort *memSidePort;
1963338SN/A
1974626SN/A  protected:
1984626SN/A
1994626SN/A    /** Miss status registers */
2004626SN/A    MSHRQueue mshrQueue;
2014626SN/A
2024626SN/A    /** Write/writeback buffer */
2034626SN/A    MSHRQueue writeBuffer;
2044626SN/A
20510693SMarco.Balboni@ARM.com    /**
20610693SMarco.Balboni@ARM.com     * Allocate a buffer, passing the time indicating when schedule an
20710693SMarco.Balboni@ARM.com     * event to the queued port to go and ask the MSHR and write queue
20810693SMarco.Balboni@ARM.com     * if they have packets to send.
20910693SMarco.Balboni@ARM.com     *
21010693SMarco.Balboni@ARM.com     * allocateBufferInternal() function is called in:
21110693SMarco.Balboni@ARM.com     * - MSHR allocateWriteBuffer (unchached write forwarded to WriteBuffer);
21210693SMarco.Balboni@ARM.com     * - MSHR allocateMissBuffer (cacheable miss in MSHR queue);
21310693SMarco.Balboni@ARM.com     * - MSHR allocateUncachedReadBuffer (unchached read allocated in MSHR
21410693SMarco.Balboni@ARM.com     *   queue)
21510693SMarco.Balboni@ARM.com     */
2164628SN/A    MSHR *allocateBufferInternal(MSHRQueue *mq, Addr addr, int size,
2174628SN/A                                 PacketPtr pkt, Tick time, bool requestBus)
2184628SN/A    {
2194666SN/A        MSHR *mshr = mq->allocate(addr, size, pkt, time, order++);
2204628SN/A
2214628SN/A        if (mq->isFull()) {
2224628SN/A            setBlocked((BlockedCause)mq->index);
2234628SN/A        }
2244628SN/A
2254628SN/A        if (requestBus) {
2264628SN/A            requestMemSideBus((RequestCause)mq->index, time);
2274628SN/A        }
2284628SN/A
2294628SN/A        return mshr;
2304628SN/A    }
2314628SN/A
23210679Sandreas.hansson@arm.com    void markInServiceInternal(MSHR *mshr, bool pending_dirty_resp)
2334628SN/A    {
2344628SN/A        MSHRQueue *mq = mshr->queue;
2354628SN/A        bool wasFull = mq->isFull();
23610679Sandreas.hansson@arm.com        mq->markInService(mshr, pending_dirty_resp);
2374628SN/A        if (wasFull && !mq->isFull()) {
2384628SN/A            clearBlocked((BlockedCause)mq->index);
2394628SN/A        }
2404628SN/A    }
2414628SN/A
2429347SAndreas.Sandberg@arm.com    /**
2439347SAndreas.Sandberg@arm.com     * Write back dirty blocks in the cache using functional accesses.
2449347SAndreas.Sandberg@arm.com     */
2459347SAndreas.Sandberg@arm.com    virtual void memWriteback() = 0;
2469347SAndreas.Sandberg@arm.com    /**
2479347SAndreas.Sandberg@arm.com     * Invalidates all blocks in the cache.
2489347SAndreas.Sandberg@arm.com     *
2499347SAndreas.Sandberg@arm.com     * @warn Dirty cache lines will not be written back to
2509347SAndreas.Sandberg@arm.com     * memory. Make sure to call functionalWriteback() first if you
2519347SAndreas.Sandberg@arm.com     * want the to write them to memory.
2529347SAndreas.Sandberg@arm.com     */
2539347SAndreas.Sandberg@arm.com    virtual void memInvalidate() = 0;
2549347SAndreas.Sandberg@arm.com    /**
2559347SAndreas.Sandberg@arm.com     * Determine if there are any dirty blocks in the cache.
2569347SAndreas.Sandberg@arm.com     *
2579347SAndreas.Sandberg@arm.com     * \return true if at least one block is dirty, false otherwise.
2589347SAndreas.Sandberg@arm.com     */
2599347SAndreas.Sandberg@arm.com    virtual bool isDirty() const = 0;
2609347SAndreas.Sandberg@arm.com
2614626SN/A    /** Block size of this cache */
2626227Snate@binkert.org    const unsigned blkSize;
2634626SN/A
2644630SN/A    /**
26510693SMarco.Balboni@ARM.com     * The latency of tag lookup of a cache. It occurs when there is
26610693SMarco.Balboni@ARM.com     * an access to the cache.
2674630SN/A     */
26810693SMarco.Balboni@ARM.com    const Cycles lookupLatency;
2699263Smrinmoy.ghosh@arm.com
2709263Smrinmoy.ghosh@arm.com    /**
27110693SMarco.Balboni@ARM.com     * This is the forward latency of the cache. It occurs when there
27210693SMarco.Balboni@ARM.com     * is a cache miss and a request is forwarded downstream, in
27310693SMarco.Balboni@ARM.com     * particular an outbound miss.
27410693SMarco.Balboni@ARM.com     */
27510693SMarco.Balboni@ARM.com    const Cycles forwardLatency;
27610693SMarco.Balboni@ARM.com
27710693SMarco.Balboni@ARM.com    /** The latency to fill a cache block */
27810693SMarco.Balboni@ARM.com    const Cycles fillLatency;
27910693SMarco.Balboni@ARM.com
28010693SMarco.Balboni@ARM.com    /**
28110693SMarco.Balboni@ARM.com     * The latency of sending reponse to its upper level cache/core on
28210693SMarco.Balboni@ARM.com     * a linefill. The responseLatency parameter captures this
28310693SMarco.Balboni@ARM.com     * latency.
2849263Smrinmoy.ghosh@arm.com     */
2859288Sandreas.hansson@arm.com    const Cycles responseLatency;
2864630SN/A
2874626SN/A    /** The number of targets for each MSHR. */
2884626SN/A    const int numTarget;
2894626SN/A
2906122SSteve.Reinhardt@amd.com    /** Do we forward snoops from mem side port through to cpu side port? */
2919529Sandreas.hansson@arm.com    const bool forwardSnoops;
2924626SN/A
2938134SAli.Saidi@ARM.com    /** Is this cache a toplevel cache (e.g. L1, I/O cache). If so we should
2948134SAli.Saidi@ARM.com     * never try to forward ownership and similar optimizations to the cpu
2958134SAli.Saidi@ARM.com     * side */
2969529Sandreas.hansson@arm.com    const bool isTopLevel;
2978134SAli.Saidi@ARM.com
2982810SN/A    /**
2992810SN/A     * Bit vector of the blocking reasons for the access path.
3002810SN/A     * @sa #BlockedCause
3012810SN/A     */
3022810SN/A    uint8_t blocked;
3032810SN/A
3046122SSteve.Reinhardt@amd.com    /** Increasing order number assigned to each incoming request. */
3056122SSteve.Reinhardt@amd.com    uint64_t order;
3066122SSteve.Reinhardt@amd.com
3072810SN/A    /** Stores time the cache blocked for statistics. */
3089288Sandreas.hansson@arm.com    Cycles blockedCycle;
3092810SN/A
3104626SN/A    /** Pointer to the MSHR that has no targets. */
3114626SN/A    MSHR *noTargetMSHR;
3122810SN/A
3132810SN/A    /** The number of misses to trigger an exit event. */
3142810SN/A    Counter missCount;
3152810SN/A
3166122SSteve.Reinhardt@amd.com    /**
3176122SSteve.Reinhardt@amd.com     * The address range to which the cache responds on the CPU side.
3186122SSteve.Reinhardt@amd.com     * Normally this is all possible memory addresses. */
3199529Sandreas.hansson@arm.com    const AddrRangeList addrRanges;
3206122SSteve.Reinhardt@amd.com
3218833Sdam.sunwoo@arm.com  public:
3228833Sdam.sunwoo@arm.com    /** System we are currently operating in. */
3238833Sdam.sunwoo@arm.com    System *system;
3246978SLisa.Hsu@amd.com
3252810SN/A    // Statistics
3262810SN/A    /**
3272810SN/A     * @addtogroup CacheStatistics
3282810SN/A     * @{
3292810SN/A     */
3302810SN/A
3312810SN/A    /** Number of hits per thread for each type of command. @sa Packet::Command */
3325999Snate@binkert.org    Stats::Vector hits[MemCmd::NUM_MEM_CMDS];
3332810SN/A    /** Number of hits for demand accesses. */
3342810SN/A    Stats::Formula demandHits;
3352810SN/A    /** Number of hit for all accesses. */
3362810SN/A    Stats::Formula overallHits;
3372810SN/A
3382810SN/A    /** Number of misses per thread for each type of command. @sa Packet::Command */
3395999Snate@binkert.org    Stats::Vector misses[MemCmd::NUM_MEM_CMDS];
3402810SN/A    /** Number of misses for demand accesses. */
3412810SN/A    Stats::Formula demandMisses;
3422810SN/A    /** Number of misses for all accesses. */
3432810SN/A    Stats::Formula overallMisses;
3442810SN/A
3452810SN/A    /**
3462810SN/A     * Total number of cycles per thread/command spent waiting for a miss.
3472810SN/A     * Used to calculate the average miss latency.
3482810SN/A     */
3495999Snate@binkert.org    Stats::Vector missLatency[MemCmd::NUM_MEM_CMDS];
3502810SN/A    /** Total number of cycles spent waiting for demand misses. */
3512810SN/A    Stats::Formula demandMissLatency;
3522810SN/A    /** Total number of cycles spent waiting for all misses. */
3532810SN/A    Stats::Formula overallMissLatency;
3542810SN/A
3552810SN/A    /** The number of accesses per command and thread. */
3564022SN/A    Stats::Formula accesses[MemCmd::NUM_MEM_CMDS];
3572810SN/A    /** The number of demand accesses. */
3582810SN/A    Stats::Formula demandAccesses;
3592810SN/A    /** The number of overall accesses. */
3602810SN/A    Stats::Formula overallAccesses;
3612810SN/A
3622810SN/A    /** The miss rate per command and thread. */
3634022SN/A    Stats::Formula missRate[MemCmd::NUM_MEM_CMDS];
3642810SN/A    /** The miss rate of all demand accesses. */
3652810SN/A    Stats::Formula demandMissRate;
3662810SN/A    /** The miss rate for all accesses. */
3672810SN/A    Stats::Formula overallMissRate;
3682810SN/A
3692810SN/A    /** The average miss latency per command and thread. */
3704022SN/A    Stats::Formula avgMissLatency[MemCmd::NUM_MEM_CMDS];
3712810SN/A    /** The average miss latency for demand misses. */
3722810SN/A    Stats::Formula demandAvgMissLatency;
3732810SN/A    /** The average miss latency for all misses. */
3742810SN/A    Stats::Formula overallAvgMissLatency;
3752810SN/A
3762810SN/A    /** The total number of cycles blocked for each blocked cause. */
3775999Snate@binkert.org    Stats::Vector blocked_cycles;
3782810SN/A    /** The number of times this cache blocked for each blocked cause. */
3795999Snate@binkert.org    Stats::Vector blocked_causes;
3802810SN/A
3812810SN/A    /** The average number of cycles blocked for each blocked cause. */
3822810SN/A    Stats::Formula avg_blocked;
3832810SN/A
3842810SN/A    /** The number of fast writes (WH64) performed. */
3855999Snate@binkert.org    Stats::Scalar fastWrites;
3862810SN/A
3872810SN/A    /** The number of cache copies performed. */
3885999Snate@binkert.org    Stats::Scalar cacheCopies;
3892810SN/A
3904626SN/A    /** Number of blocks written back per thread. */
3915999Snate@binkert.org    Stats::Vector writebacks;
3924626SN/A
3934626SN/A    /** Number of misses that hit in the MSHRs per command and thread. */
3945999Snate@binkert.org    Stats::Vector mshr_hits[MemCmd::NUM_MEM_CMDS];
3954626SN/A    /** Demand misses that hit in the MSHRs. */
3964626SN/A    Stats::Formula demandMshrHits;
3974626SN/A    /** Total number of misses that hit in the MSHRs. */
3984626SN/A    Stats::Formula overallMshrHits;
3994626SN/A
4004626SN/A    /** Number of misses that miss in the MSHRs, per command and thread. */
4015999Snate@binkert.org    Stats::Vector mshr_misses[MemCmd::NUM_MEM_CMDS];
4024626SN/A    /** Demand misses that miss in the MSHRs. */
4034626SN/A    Stats::Formula demandMshrMisses;
4044626SN/A    /** Total number of misses that miss in the MSHRs. */
4054626SN/A    Stats::Formula overallMshrMisses;
4064626SN/A
4074626SN/A    /** Number of misses that miss in the MSHRs, per command and thread. */
4085999Snate@binkert.org    Stats::Vector mshr_uncacheable[MemCmd::NUM_MEM_CMDS];
4094626SN/A    /** Total number of misses that miss in the MSHRs. */
4104626SN/A    Stats::Formula overallMshrUncacheable;
4114626SN/A
4124626SN/A    /** Total cycle latency of each MSHR miss, per command and thread. */
4135999Snate@binkert.org    Stats::Vector mshr_miss_latency[MemCmd::NUM_MEM_CMDS];
4144626SN/A    /** Total cycle latency of demand MSHR misses. */
4154626SN/A    Stats::Formula demandMshrMissLatency;
4164626SN/A    /** Total cycle latency of overall MSHR misses. */
4174626SN/A    Stats::Formula overallMshrMissLatency;
4184626SN/A
4194626SN/A    /** Total cycle latency of each MSHR miss, per command and thread. */
4205999Snate@binkert.org    Stats::Vector mshr_uncacheable_lat[MemCmd::NUM_MEM_CMDS];
4214626SN/A    /** Total cycle latency of overall MSHR misses. */
4224626SN/A    Stats::Formula overallMshrUncacheableLatency;
4234626SN/A
4247461Snate@binkert.org#if 0
4254626SN/A    /** The total number of MSHR accesses per command and thread. */
4264626SN/A    Stats::Formula mshrAccesses[MemCmd::NUM_MEM_CMDS];
4274626SN/A    /** The total number of demand MSHR accesses. */
4284626SN/A    Stats::Formula demandMshrAccesses;
4294626SN/A    /** The total number of MSHR accesses. */
4304626SN/A    Stats::Formula overallMshrAccesses;
4317461Snate@binkert.org#endif
4324626SN/A
4334626SN/A    /** The miss rate in the MSHRs pre command and thread. */
4344626SN/A    Stats::Formula mshrMissRate[MemCmd::NUM_MEM_CMDS];
4354626SN/A    /** The demand miss rate in the MSHRs. */
4364626SN/A    Stats::Formula demandMshrMissRate;
4374626SN/A    /** The overall miss rate in the MSHRs. */
4384626SN/A    Stats::Formula overallMshrMissRate;
4394626SN/A
4404626SN/A    /** The average latency of an MSHR miss, per command and thread. */
4414626SN/A    Stats::Formula avgMshrMissLatency[MemCmd::NUM_MEM_CMDS];
4424626SN/A    /** The average latency of a demand MSHR miss. */
4434626SN/A    Stats::Formula demandAvgMshrMissLatency;
4444626SN/A    /** The average overall latency of an MSHR miss. */
4454626SN/A    Stats::Formula overallAvgMshrMissLatency;
4464626SN/A
4474626SN/A    /** The average latency of an MSHR miss, per command and thread. */
4484626SN/A    Stats::Formula avgMshrUncacheableLatency[MemCmd::NUM_MEM_CMDS];
4494626SN/A    /** The average overall latency of an MSHR miss. */
4504626SN/A    Stats::Formula overallAvgMshrUncacheableLatency;
4514626SN/A
4524626SN/A    /** The number of times a thread hit its MSHR cap. */
4535999Snate@binkert.org    Stats::Vector mshr_cap_events;
4544626SN/A    /** The number of times software prefetches caused the MSHR to block. */
4555999Snate@binkert.org    Stats::Vector soft_prefetch_mshr_full;
4564626SN/A
4575999Snate@binkert.org    Stats::Scalar mshr_no_allocate_misses;
4584626SN/A
4592810SN/A    /**
4602810SN/A     * @}
4612810SN/A     */
4622810SN/A
4632810SN/A    /**
4642810SN/A     * Register stats for this object.
4652810SN/A     */
4662810SN/A    virtual void regStats();
4672810SN/A
4682810SN/A  public:
4695034SN/A    typedef BaseCacheParams Params;
4705034SN/A    BaseCache(const Params *p);
4715034SN/A    ~BaseCache() {}
4723606SN/A
4732858SN/A    virtual void init();
4742858SN/A
4759294Sandreas.hansson@arm.com    virtual BaseMasterPort &getMasterPort(const std::string &if_name,
4769294Sandreas.hansson@arm.com                                          PortID idx = InvalidPortID);
4779294Sandreas.hansson@arm.com    virtual BaseSlavePort &getSlavePort(const std::string &if_name,
4789294Sandreas.hansson@arm.com                                        PortID idx = InvalidPortID);
4798922Swilliam.wang@arm.com
4802810SN/A    /**
4812810SN/A     * Query block size of a cache.
4822810SN/A     * @return  The block size
4832810SN/A     */
4846227Snate@binkert.org    unsigned
4856227Snate@binkert.org    getBlockSize() const
4862810SN/A    {
4872810SN/A        return blkSize;
4882810SN/A    }
4892810SN/A
4904626SN/A
4916666Ssteve.reinhardt@amd.com    Addr blockAlign(Addr addr) const { return (addr & ~(Addr(blkSize - 1))); }
4924626SN/A
4934626SN/A
4948883SAli.Saidi@ARM.com    const AddrRangeList &getAddrRanges() const { return addrRanges; }
4956122SSteve.Reinhardt@amd.com
4964628SN/A    MSHR *allocateMissBuffer(PacketPtr pkt, Tick time, bool requestBus)
4974628SN/A    {
4984902SN/A        assert(!pkt->req->isUncacheable());
4994628SN/A        return allocateBufferInternal(&mshrQueue,
5004628SN/A                                      blockAlign(pkt->getAddr()), blkSize,
5014628SN/A                                      pkt, time, requestBus);
5024628SN/A    }
5034628SN/A
5044902SN/A    MSHR *allocateWriteBuffer(PacketPtr pkt, Tick time, bool requestBus)
5054628SN/A    {
5064902SN/A        assert(pkt->isWrite() && !pkt->isRead());
5074902SN/A        return allocateBufferInternal(&writeBuffer,
5084902SN/A                                      pkt->getAddr(), pkt->getSize(),
5094628SN/A                                      pkt, time, requestBus);
5104628SN/A    }
5114628SN/A
5124902SN/A    MSHR *allocateUncachedReadBuffer(PacketPtr pkt, Tick time, bool requestBus)
5134902SN/A    {
5144902SN/A        assert(pkt->req->isUncacheable());
5154902SN/A        assert(pkt->isRead());
5164902SN/A        return allocateBufferInternal(&mshrQueue,
5174902SN/A                                      pkt->getAddr(), pkt->getSize(),
5184902SN/A                                      pkt, time, requestBus);
5194902SN/A    }
5204628SN/A
5212810SN/A    /**
5222810SN/A     * Returns true if the cache is blocked for accesses.
5232810SN/A     */
5249529Sandreas.hansson@arm.com    bool isBlocked() const
5252810SN/A    {
5262810SN/A        return blocked != 0;
5272810SN/A    }
5282810SN/A
5292810SN/A    /**
5302810SN/A     * Marks the access path of the cache as blocked for the given cause. This
5312810SN/A     * also sets the blocked flag in the slave interface.
5322810SN/A     * @param cause The reason for the cache blocking.
5332810SN/A     */
5342810SN/A    void setBlocked(BlockedCause cause)
5352810SN/A    {
5362810SN/A        uint8_t flag = 1 << cause;
5372810SN/A        if (blocked == 0) {
5382810SN/A            blocked_causes[cause]++;
5399288Sandreas.hansson@arm.com            blockedCycle = curCycle();
5404630SN/A            cpuSidePort->setBlocked();
5412810SN/A        }
5424630SN/A        blocked |= flag;
5434630SN/A        DPRINTF(Cache,"Blocking for cause %d, mask=%d\n", cause, blocked);
5442810SN/A    }
5452810SN/A
5462810SN/A    /**
5472810SN/A     * Marks the cache as unblocked for the given cause. This also clears the
5482810SN/A     * blocked flags in the appropriate interfaces.
5492810SN/A     * @param cause The newly unblocked cause.
5502810SN/A     * @warning Calling this function can cause a blocked request on the bus to
5512810SN/A     * access the cache. The cache must be in a state to handle that request.
5522810SN/A     */
5532810SN/A    void clearBlocked(BlockedCause cause)
5542810SN/A    {
5552810SN/A        uint8_t flag = 1 << cause;
5564630SN/A        blocked &= ~flag;
5574630SN/A        DPRINTF(Cache,"Unblocking for cause %d, mask=%d\n", cause, blocked);
5584630SN/A        if (blocked == 0) {
5599288Sandreas.hansson@arm.com            blocked_cycles[cause] += curCycle() - blockedCycle;
5604630SN/A            cpuSidePort->clearBlocked();
5612810SN/A        }
5622810SN/A    }
5632810SN/A
5642810SN/A    /**
5652810SN/A     * Request the master bus for the given cause and time.
5662810SN/A     * @param cause The reason for the request.
5672810SN/A     * @param time The time to make the request.
5682810SN/A     */
5694458SN/A    void requestMemSideBus(RequestCause cause, Tick time)
5702810SN/A    {
5714458SN/A        memSidePort->requestBus(cause, time);
5722810SN/A    }
5732810SN/A
5742810SN/A    /**
5752810SN/A     * Clear the master bus request for the given cause.
5762810SN/A     * @param cause The request reason to clear.
5772810SN/A     */
5784458SN/A    void deassertMemSideBusRequest(RequestCause cause)
5792810SN/A    {
5805875Ssteve.reinhardt@amd.com        // Obsolete... we no longer signal bus requests explicitly so
5815875Ssteve.reinhardt@amd.com        // we can't deassert them.  Leaving this in as a no-op since
5825875Ssteve.reinhardt@amd.com        // the prefetcher calls it to indicate that it no longer wants
5835875Ssteve.reinhardt@amd.com        // to request a prefetch, and someday that might be
5845875Ssteve.reinhardt@amd.com        // interesting again.
5852811SN/A    }
5863503SN/A
5879342SAndreas.Sandberg@arm.com    virtual unsigned int drain(DrainManager *dm);
5883503SN/A
58910028SGiacomo.Gabrielli@arm.com    virtual bool inCache(Addr addr, bool is_secure) const = 0;
5904626SN/A
59110028SGiacomo.Gabrielli@arm.com    virtual bool inMissQueue(Addr addr, bool is_secure) const = 0;
5924626SN/A
5938833Sdam.sunwoo@arm.com    void incMissCount(PacketPtr pkt)
5943503SN/A    {
5958833Sdam.sunwoo@arm.com        assert(pkt->req->masterId() < system->maxMasters());
5968833Sdam.sunwoo@arm.com        misses[pkt->cmdToIndex()][pkt->req->masterId()]++;
59710020Smatt.horsnell@ARM.com        pkt->req->incAccessDepth();
5984626SN/A        if (missCount) {
5994626SN/A            --missCount;
6004626SN/A            if (missCount == 0)
6014626SN/A                exitSimLoop("A cache reached the maximum miss count");
6023503SN/A        }
6033503SN/A    }
6048833Sdam.sunwoo@arm.com    void incHitCount(PacketPtr pkt)
6056978SLisa.Hsu@amd.com    {
6068833Sdam.sunwoo@arm.com        assert(pkt->req->masterId() < system->maxMasters());
6078833Sdam.sunwoo@arm.com        hits[pkt->cmdToIndex()][pkt->req->masterId()]++;
6086978SLisa.Hsu@amd.com
6096978SLisa.Hsu@amd.com    }
6103503SN/A
6112810SN/A};
6122810SN/A
6132810SN/A#endif //__BASE_CACHE_HH__
614