base.hh revision 12773
12810SN/A/*
212728Snikos.nikoleris@arm.com * Copyright (c) 2012-2014,2016-2018 ARM Limited
39347SAndreas.Sandberg@arm.com * All rights reserved.
49347SAndreas.Sandberg@arm.com *
59347SAndreas.Sandberg@arm.com * The license below extends only to copyright in the software and shall
69347SAndreas.Sandberg@arm.com * not be construed as granting a license to any other intellectual
79347SAndreas.Sandberg@arm.com * property including but not limited to intellectual property relating
89347SAndreas.Sandberg@arm.com * to a hardware implementation of the functionality of the software
99347SAndreas.Sandberg@arm.com * licensed hereunder.  You may use the software subject to the license
109347SAndreas.Sandberg@arm.com * terms below provided that you ensure that this notice is replicated
119347SAndreas.Sandberg@arm.com * unmodified and in its entirety in all distributions of the software,
129347SAndreas.Sandberg@arm.com * modified or unmodified, in source code or in binary form.
139347SAndreas.Sandberg@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
412810SN/A *          Ron Dreslinski
422810SN/A */
432810SN/A
442810SN/A/**
452810SN/A * @file
462810SN/A * Declaration of a common base class for cache tagstore objects.
472810SN/A */
482810SN/A
4912492Sodanrc@yahoo.com.br#ifndef __MEM_CACHE_TAGS_BASE_HH__
5012492Sodanrc@yahoo.com.br#define __MEM_CACHE_TAGS_BASE_HH__
512810SN/A
5212727Snikos.nikoleris@arm.com#include <cassert>
5312728Snikos.nikoleris@arm.com#include <functional>
542810SN/A#include <string>
558229Snate@binkert.org
568229Snate@binkert.org#include "base/callback.hh"
5712727Snikos.nikoleris@arm.com#include "base/logging.hh"
582810SN/A#include "base/statistics.hh"
5912727Snikos.nikoleris@arm.com#include "base/types.hh"
6010815Sdavid.guillen@arm.com#include "mem/cache/blk.hh"
6112727Snikos.nikoleris@arm.com#include "mem/packet.hh"
629796Sprakash.ramrakhyani@arm.com#include "params/BaseTags.hh"
639796Sprakash.ramrakhyani@arm.com#include "sim/clocked_object.hh"
642810SN/A
652810SN/Aclass BaseCache;
6612773Sodanrc@yahoo.com.brclass ReplaceableEntry;
672810SN/A
682810SN/A/**
692810SN/A * A common base class of Cache tagstore objects.
702810SN/A */
719796Sprakash.ramrakhyani@arm.comclass BaseTags : public ClockedObject
722810SN/A{
732810SN/A  protected:
749796Sprakash.ramrakhyani@arm.com    /** The block size of the cache. */
759796Sprakash.ramrakhyani@arm.com    const unsigned blkSize;
7611893Snikos.nikoleris@arm.com    /** Mask out all bits that aren't part of the block offset. */
7711893Snikos.nikoleris@arm.com    const Addr blkMask;
789796Sprakash.ramrakhyani@arm.com    /** The size of the cache. */
799796Sprakash.ramrakhyani@arm.com    const unsigned size;
8011722Ssophiane.senni@gmail.com    /** The tag lookup latency of the cache. */
8111722Ssophiane.senni@gmail.com    const Cycles lookupLatency;
8211722Ssophiane.senni@gmail.com    /**
8311722Ssophiane.senni@gmail.com     * The total access latency of the cache. This latency
8411722Ssophiane.senni@gmail.com     * is different depending on the cache access mode
8511722Ssophiane.senni@gmail.com     * (parallel or sequential)
8611722Ssophiane.senni@gmail.com     */
8710693SMarco.Balboni@ARM.com    const Cycles accessLatency;
882810SN/A    /** Pointer to the parent cache. */
892810SN/A    BaseCache *cache;
902810SN/A
912810SN/A    /**
922810SN/A     * The number of tags that need to be touched to meet the warmup
932810SN/A     * percentage.
942810SN/A     */
9512513Sodanrc@yahoo.com.br    const unsigned warmupBound;
962810SN/A    /** Marked true when the cache is warmed up. */
972810SN/A    bool warmedUp;
982810SN/A
996978SLisa.Hsu@amd.com    /** the number of blocks in the cache */
10012553Snikos.nikoleris@arm.com    const unsigned numBlocks;
1016978SLisa.Hsu@amd.com
10212629Sodanrc@yahoo.com.br    /** The data blocks, 1 per cache block. */
10312629Sodanrc@yahoo.com.br    std::unique_ptr<uint8_t[]> dataBlks;
10412629Sodanrc@yahoo.com.br
1052810SN/A    // Statistics
1062810SN/A    /**
10712513Sodanrc@yahoo.com.br     * TODO: It would be good if these stats were acquired after warmup.
1082810SN/A     * @addtogroup CacheStatistics
1092810SN/A     * @{
1102810SN/A     */
1112810SN/A
1122810SN/A    /** Per cycle average of the number of tags that hold valid data. */
1135999Snate@binkert.org    Stats::Average tagsInUse;
1142810SN/A
1152810SN/A    /** The total number of references to a block before it is replaced. */
1165999Snate@binkert.org    Stats::Scalar totalRefs;
1172810SN/A
1182810SN/A    /**
1192810SN/A     * The number of reference counts sampled. This is different from
1202810SN/A     * replacements because we sample all the valid blocks when the simulator
1212810SN/A     * exits.
1222810SN/A     */
1235999Snate@binkert.org    Stats::Scalar sampledRefs;
1242810SN/A
1252810SN/A    /**
1262810SN/A     * Average number of references to a block before is was replaced.
1272810SN/A     * @todo This should change to an average stat once we have them.
1282810SN/A     */
1292810SN/A    Stats::Formula avgRefs;
1302810SN/A
13112513Sodanrc@yahoo.com.br    /** The cycle that the warmup percentage was hit. 0 on failure. */
1325999Snate@binkert.org    Stats::Scalar warmupCycle;
1336978SLisa.Hsu@amd.com
1348833Sdam.sunwoo@arm.com    /** Average occupancy of each requestor using the cache */
1356978SLisa.Hsu@amd.com    Stats::AverageVector occupancies;
1366978SLisa.Hsu@amd.com
1378833Sdam.sunwoo@arm.com    /** Average occ % of each requestor using the cache */
1386978SLisa.Hsu@amd.com    Stats::Formula avgOccs;
1396978SLisa.Hsu@amd.com
14010024Sdam.sunwoo@arm.com    /** Occupancy of each context/cpu using the cache */
14110024Sdam.sunwoo@arm.com    Stats::Vector occupanciesTaskId;
14210024Sdam.sunwoo@arm.com
14310024Sdam.sunwoo@arm.com    /** Occupancy of each context/cpu using the cache */
14410024Sdam.sunwoo@arm.com    Stats::Vector2d ageTaskId;
14510024Sdam.sunwoo@arm.com
14610024Sdam.sunwoo@arm.com    /** Occ % of each context/cpu using the cache */
14710024Sdam.sunwoo@arm.com    Stats::Formula percentOccsTaskId;
14810024Sdam.sunwoo@arm.com
14910025Stimothy.jones@arm.com    /** Number of tags consulted over all accesses. */
15010025Stimothy.jones@arm.com    Stats::Scalar tagAccesses;
15110025Stimothy.jones@arm.com    /** Number of data blocks consulted over all accesses. */
15210025Stimothy.jones@arm.com    Stats::Scalar dataAccesses;
15310025Stimothy.jones@arm.com
1542810SN/A    /**
1552810SN/A     * @}
1562810SN/A     */
1572810SN/A
1582810SN/A  public:
1599796Sprakash.ramrakhyani@arm.com    typedef BaseTagsParams Params;
1609796Sprakash.ramrakhyani@arm.com    BaseTags(const Params *p);
1612810SN/A
1622810SN/A    /**
1632810SN/A     * Destructor.
1642810SN/A     */
1652810SN/A    virtual ~BaseTags() {}
1662810SN/A
1672810SN/A    /**
1689796Sprakash.ramrakhyani@arm.com     * Set the parent cache back pointer.
1692810SN/A     * @param _cache Pointer to parent cache.
1702810SN/A     */
1712810SN/A    void setCache(BaseCache *_cache);
1722810SN/A
1732810SN/A    /**
1749796Sprakash.ramrakhyani@arm.com     * Register local statistics.
1752810SN/A     */
1769796Sprakash.ramrakhyani@arm.com    void regStats();
1772810SN/A
1782810SN/A    /**
1792810SN/A     * Average in the reference count for valid blocks when the simulation
1802810SN/A     * exits.
1812810SN/A     */
18212728Snikos.nikoleris@arm.com    void cleanupRefs();
1837612SGene.Wu@arm.com
1847612SGene.Wu@arm.com    /**
18510024Sdam.sunwoo@arm.com     * Computes stats just prior to dump event
18610024Sdam.sunwoo@arm.com     */
18712728Snikos.nikoleris@arm.com    void computeStats();
18810024Sdam.sunwoo@arm.com
18910024Sdam.sunwoo@arm.com    /**
1909663Suri.wiener@arm.com     * Print all tags used
1919663Suri.wiener@arm.com     */
19212728Snikos.nikoleris@arm.com    std::string print();
19310815Sdavid.guillen@arm.com
19410815Sdavid.guillen@arm.com    /**
19510815Sdavid.guillen@arm.com     * Find a block using the memory address
19610815Sdavid.guillen@arm.com     */
19710815Sdavid.guillen@arm.com    virtual CacheBlk * findBlock(Addr addr, bool is_secure) const = 0;
19810815Sdavid.guillen@arm.com
19910815Sdavid.guillen@arm.com    /**
20012743Sodanrc@yahoo.com.br     * Find a block given set and way.
20112743Sodanrc@yahoo.com.br     *
20212743Sodanrc@yahoo.com.br     * @param set The set of the block.
20312743Sodanrc@yahoo.com.br     * @param way The way of the block.
20412743Sodanrc@yahoo.com.br     * @return The block.
20512743Sodanrc@yahoo.com.br     */
20612743Sodanrc@yahoo.com.br    virtual ReplaceableEntry* findBlockBySetAndWay(int set, int way) const = 0;
20712743Sodanrc@yahoo.com.br
20812743Sodanrc@yahoo.com.br    /**
20911893Snikos.nikoleris@arm.com     * Align an address to the block size.
21011893Snikos.nikoleris@arm.com     * @param addr the address to align.
21111893Snikos.nikoleris@arm.com     * @return The block address.
21211893Snikos.nikoleris@arm.com     */
21311893Snikos.nikoleris@arm.com    Addr blkAlign(Addr addr) const
21411893Snikos.nikoleris@arm.com    {
21511893Snikos.nikoleris@arm.com        return addr & ~blkMask;
21611893Snikos.nikoleris@arm.com    }
21711893Snikos.nikoleris@arm.com
21811893Snikos.nikoleris@arm.com    /**
21910815Sdavid.guillen@arm.com     * Calculate the block offset of an address.
22010815Sdavid.guillen@arm.com     * @param addr the address to get the offset of.
22110815Sdavid.guillen@arm.com     * @return the block offset.
22210815Sdavid.guillen@arm.com     */
22310815Sdavid.guillen@arm.com    int extractBlkOffset(Addr addr) const
22410815Sdavid.guillen@arm.com    {
22511893Snikos.nikoleris@arm.com        return (addr & blkMask);
22610815Sdavid.guillen@arm.com    }
22710815Sdavid.guillen@arm.com
22810941Sdavid.guillen@arm.com    /**
22910941Sdavid.guillen@arm.com     * Limit the allocation for the cache ways.
23010941Sdavid.guillen@arm.com     * @param ways The maximum number of ways available for replacement.
23110941Sdavid.guillen@arm.com     */
23210941Sdavid.guillen@arm.com    virtual void setWayAllocationMax(int ways)
23310941Sdavid.guillen@arm.com    {
23410941Sdavid.guillen@arm.com        panic("This tag class does not implement way allocation limit!\n");
23510941Sdavid.guillen@arm.com    }
23610941Sdavid.guillen@arm.com
23710941Sdavid.guillen@arm.com    /**
23810941Sdavid.guillen@arm.com     * Get the way allocation mask limit.
23910941Sdavid.guillen@arm.com     * @return The maximum number of ways available for replacement.
24010941Sdavid.guillen@arm.com     */
24110941Sdavid.guillen@arm.com    virtual int getWayAllocationMax() const
24210941Sdavid.guillen@arm.com    {
24310941Sdavid.guillen@arm.com        panic("This tag class does not implement way allocation limit!\n");
24410941Sdavid.guillen@arm.com        return -1;
24510941Sdavid.guillen@arm.com    }
24610941Sdavid.guillen@arm.com
24712566Snikos.nikoleris@arm.com    /**
24812704Snikos.nikoleris@arm.com     * This function updates the tags when a block is invalidated
24912704Snikos.nikoleris@arm.com     *
25012704Snikos.nikoleris@arm.com     * @param blk A valid block to invalidate.
25112566Snikos.nikoleris@arm.com     */
25212566Snikos.nikoleris@arm.com    virtual void invalidate(CacheBlk *blk)
25312566Snikos.nikoleris@arm.com    {
25412566Snikos.nikoleris@arm.com        assert(blk);
25512566Snikos.nikoleris@arm.com        assert(blk->isValid());
25612704Snikos.nikoleris@arm.com
25712566Snikos.nikoleris@arm.com        occupancies[blk->srcMasterId]--;
25812704Snikos.nikoleris@arm.com        totalRefs += blk->refCount;
25912704Snikos.nikoleris@arm.com        sampledRefs++;
26012704Snikos.nikoleris@arm.com
26112704Snikos.nikoleris@arm.com        blk->invalidate();
26212566Snikos.nikoleris@arm.com    }
26310815Sdavid.guillen@arm.com
26412600Sodanrc@yahoo.com.br    /**
26512744Sodanrc@yahoo.com.br     * Find replacement victim based on address. If the address requires
26612744Sodanrc@yahoo.com.br     * blocks to be evicted, their locations are listed for eviction. If a
26712744Sodanrc@yahoo.com.br     * conventional cache is being used, the list only contains the victim.
26812744Sodanrc@yahoo.com.br     * However, if using sector or compressed caches, the victim is one of
26912744Sodanrc@yahoo.com.br     * the blocks to be evicted, but its location is the only one that will
27012744Sodanrc@yahoo.com.br     * be assigned to the newly allocated block associated to this address.
27112744Sodanrc@yahoo.com.br     * @sa insertBlock
27212600Sodanrc@yahoo.com.br     *
27312600Sodanrc@yahoo.com.br     * @param addr Address to find a victim for.
27412746Sodanrc@yahoo.com.br     * @param is_secure True if the target memory space is secure.
27512744Sodanrc@yahoo.com.br     * @param evict_blks Cache blocks to be evicted.
27612600Sodanrc@yahoo.com.br     * @return Cache block to be replaced.
27712600Sodanrc@yahoo.com.br     */
27812746Sodanrc@yahoo.com.br    virtual CacheBlk* findVictim(Addr addr, const bool is_secure,
27912746Sodanrc@yahoo.com.br                                 std::vector<CacheBlk*>& evict_blks) const = 0;
28012600Sodanrc@yahoo.com.br
28111870Snikos.nikoleris@arm.com    virtual CacheBlk* accessBlock(Addr addr, bool is_secure, Cycles &lat) = 0;
28210815Sdavid.guillen@arm.com
28310815Sdavid.guillen@arm.com    virtual Addr extractTag(Addr addr) const = 0;
28410815Sdavid.guillen@arm.com
28512636Sodanrc@yahoo.com.br    /**
28612636Sodanrc@yahoo.com.br     * Insert the new block into the cache and update stats.
28712636Sodanrc@yahoo.com.br     *
28812636Sodanrc@yahoo.com.br     * @param pkt Packet holding the address to update
28912636Sodanrc@yahoo.com.br     * @param blk The block to update.
29012636Sodanrc@yahoo.com.br     */
29112753Sodanrc@yahoo.com.br    virtual void insertBlock(const PacketPtr pkt, CacheBlk *blk);
29210815Sdavid.guillen@arm.com
29312574Sodanrc@yahoo.com.br    /**
29412574Sodanrc@yahoo.com.br     * Regenerate the block address.
29512574Sodanrc@yahoo.com.br     *
29612574Sodanrc@yahoo.com.br     * @param block The block.
29712574Sodanrc@yahoo.com.br     * @return the block address.
29812574Sodanrc@yahoo.com.br     */
29912574Sodanrc@yahoo.com.br    virtual Addr regenerateBlkAddr(const CacheBlk* blk) const = 0;
30010815Sdavid.guillen@arm.com
30112728Snikos.nikoleris@arm.com    /**
30212728Snikos.nikoleris@arm.com     * Visit each block in the tags and apply a visitor
30312728Snikos.nikoleris@arm.com     *
30412728Snikos.nikoleris@arm.com     * The visitor should be a std::function that takes a cache block
30512728Snikos.nikoleris@arm.com     * reference as its parameter.
30612728Snikos.nikoleris@arm.com     *
30712728Snikos.nikoleris@arm.com     * @param visitor Visitor to call on each block.
30812728Snikos.nikoleris@arm.com     */
30912728Snikos.nikoleris@arm.com    virtual void forEachBlk(std::function<void(CacheBlk &)> visitor) = 0;
31012728Snikos.nikoleris@arm.com
31112728Snikos.nikoleris@arm.com    /**
31212728Snikos.nikoleris@arm.com     * Find if any of the blocks satisfies a condition
31312728Snikos.nikoleris@arm.com     *
31412728Snikos.nikoleris@arm.com     * The visitor should be a std::function that takes a cache block
31512728Snikos.nikoleris@arm.com     * reference as its parameter. The visitor will terminate the
31612728Snikos.nikoleris@arm.com     * traversal early if the condition is satisfied.
31712728Snikos.nikoleris@arm.com     *
31812728Snikos.nikoleris@arm.com     * @param visitor Visitor to call on each block.
31912728Snikos.nikoleris@arm.com     */
32012728Snikos.nikoleris@arm.com    virtual bool anyBlk(std::function<bool(CacheBlk &)> visitor) = 0;
32112728Snikos.nikoleris@arm.com
32212728Snikos.nikoleris@arm.com  private:
32312728Snikos.nikoleris@arm.com    /**
32412728Snikos.nikoleris@arm.com     * Update the reference stats using data from the input block
32512728Snikos.nikoleris@arm.com     *
32612728Snikos.nikoleris@arm.com     * @param blk The input block
32712728Snikos.nikoleris@arm.com     */
32812728Snikos.nikoleris@arm.com    void cleanupRefsVisitor(CacheBlk &blk);
32912728Snikos.nikoleris@arm.com
33012728Snikos.nikoleris@arm.com    /**
33112728Snikos.nikoleris@arm.com     * Update the occupancy and age stats using data from the input block
33212728Snikos.nikoleris@arm.com     *
33312728Snikos.nikoleris@arm.com     * @param blk The input block
33412728Snikos.nikoleris@arm.com     */
33512728Snikos.nikoleris@arm.com    void computeStatsVisitor(CacheBlk &blk);
3362810SN/A};
3372810SN/A
3382810SN/Aclass BaseTagsCallback : public Callback
3392810SN/A{
3402810SN/A    BaseTags *tags;
3412810SN/A  public:
3422810SN/A    BaseTagsCallback(BaseTags *t) : tags(t) {}
3432810SN/A    virtual void process() { tags->cleanupRefs(); };
3442810SN/A};
3452810SN/A
34610024Sdam.sunwoo@arm.comclass BaseTagsDumpCallback : public Callback
34710024Sdam.sunwoo@arm.com{
34810024Sdam.sunwoo@arm.com    BaseTags *tags;
34910024Sdam.sunwoo@arm.com  public:
35010024Sdam.sunwoo@arm.com    BaseTagsDumpCallback(BaseTags *t) : tags(t) {}
35110024Sdam.sunwoo@arm.com    virtual void process() { tags->computeStats(); };
35210024Sdam.sunwoo@arm.com};
35310024Sdam.sunwoo@arm.com
35412492Sodanrc@yahoo.com.br#endif //__MEM_CACHE_TAGS_BASE_HH__
355