base.hh revision 13418
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"
6013223Sodanrc@yahoo.com.br#include "mem/cache/cache_blk.hh"
619796Sprakash.ramrakhyani@arm.com#include "params/BaseTags.hh"
629796Sprakash.ramrakhyani@arm.com#include "sim/clocked_object.hh"
632810SN/A
642810SN/Aclass BaseCache;
6513219Sodanrc@yahoo.com.brclass IndexingPolicy;
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;
8213418Sodanrc@yahoo.com.br
832810SN/A    /** Pointer to the parent cache. */
842810SN/A    BaseCache *cache;
852810SN/A
8613219Sodanrc@yahoo.com.br    /** Indexing policy */
8713219Sodanrc@yahoo.com.br    BaseIndexingPolicy *indexingPolicy;
8813219Sodanrc@yahoo.com.br
892810SN/A    /**
902810SN/A     * The number of tags that need to be touched to meet the warmup
912810SN/A     * percentage.
922810SN/A     */
9312513Sodanrc@yahoo.com.br    const unsigned warmupBound;
942810SN/A    /** Marked true when the cache is warmed up. */
952810SN/A    bool warmedUp;
962810SN/A
976978SLisa.Hsu@amd.com    /** the number of blocks in the cache */
9812553Snikos.nikoleris@arm.com    const unsigned numBlocks;
996978SLisa.Hsu@amd.com
10012629Sodanrc@yahoo.com.br    /** The data blocks, 1 per cache block. */
10112629Sodanrc@yahoo.com.br    std::unique_ptr<uint8_t[]> dataBlks;
10212629Sodanrc@yahoo.com.br
1032810SN/A    // Statistics
1042810SN/A    /**
10512513Sodanrc@yahoo.com.br     * TODO: It would be good if these stats were acquired after warmup.
1062810SN/A     * @addtogroup CacheStatistics
1072810SN/A     * @{
1082810SN/A     */
1092810SN/A
1102810SN/A    /** Per cycle average of the number of tags that hold valid data. */
1115999Snate@binkert.org    Stats::Average tagsInUse;
1122810SN/A
1132810SN/A    /** The total number of references to a block before it is replaced. */
1145999Snate@binkert.org    Stats::Scalar totalRefs;
1152810SN/A
1162810SN/A    /**
1172810SN/A     * The number of reference counts sampled. This is different from
1182810SN/A     * replacements because we sample all the valid blocks when the simulator
1192810SN/A     * exits.
1202810SN/A     */
1215999Snate@binkert.org    Stats::Scalar sampledRefs;
1222810SN/A
1232810SN/A    /**
1242810SN/A     * Average number of references to a block before is was replaced.
1252810SN/A     * @todo This should change to an average stat once we have them.
1262810SN/A     */
1272810SN/A    Stats::Formula avgRefs;
1282810SN/A
12912513Sodanrc@yahoo.com.br    /** The cycle that the warmup percentage was hit. 0 on failure. */
1305999Snate@binkert.org    Stats::Scalar warmupCycle;
1316978SLisa.Hsu@amd.com
1328833Sdam.sunwoo@arm.com    /** Average occupancy of each requestor using the cache */
1336978SLisa.Hsu@amd.com    Stats::AverageVector occupancies;
1346978SLisa.Hsu@amd.com
1358833Sdam.sunwoo@arm.com    /** Average occ % of each requestor using the cache */
1366978SLisa.Hsu@amd.com    Stats::Formula avgOccs;
1376978SLisa.Hsu@amd.com
13810024Sdam.sunwoo@arm.com    /** Occupancy of each context/cpu using the cache */
13910024Sdam.sunwoo@arm.com    Stats::Vector occupanciesTaskId;
14010024Sdam.sunwoo@arm.com
14110024Sdam.sunwoo@arm.com    /** Occupancy of each context/cpu using the cache */
14210024Sdam.sunwoo@arm.com    Stats::Vector2d ageTaskId;
14310024Sdam.sunwoo@arm.com
14410024Sdam.sunwoo@arm.com    /** Occ % of each context/cpu using the cache */
14510024Sdam.sunwoo@arm.com    Stats::Formula percentOccsTaskId;
14610024Sdam.sunwoo@arm.com
14710025Stimothy.jones@arm.com    /** Number of tags consulted over all accesses. */
14810025Stimothy.jones@arm.com    Stats::Scalar tagAccesses;
14910025Stimothy.jones@arm.com    /** Number of data blocks consulted over all accesses. */
15010025Stimothy.jones@arm.com    Stats::Scalar dataAccesses;
15110025Stimothy.jones@arm.com
1522810SN/A    /**
1532810SN/A     * @}
1542810SN/A     */
1552810SN/A
15613216Sodanrc@yahoo.com.br    /**
15713216Sodanrc@yahoo.com.br     * Set the parent cache back pointer.
15813216Sodanrc@yahoo.com.br     *
15913216Sodanrc@yahoo.com.br     * @param _cache Pointer to parent cache.
16013216Sodanrc@yahoo.com.br     */
16113216Sodanrc@yahoo.com.br    void setCache(BaseCache *_cache);
16213216Sodanrc@yahoo.com.br
1632810SN/A  public:
1649796Sprakash.ramrakhyani@arm.com    typedef BaseTagsParams Params;
1659796Sprakash.ramrakhyani@arm.com    BaseTags(const Params *p);
1662810SN/A
1672810SN/A    /**
1682810SN/A     * Destructor.
1692810SN/A     */
1702810SN/A    virtual ~BaseTags() {}
1712810SN/A
1722810SN/A    /**
17313216Sodanrc@yahoo.com.br     * Initialize blocks and set the parent cache back pointer.
17413216Sodanrc@yahoo.com.br     *
1752810SN/A     * @param _cache Pointer to parent cache.
1762810SN/A     */
17713378Sgabeblack@google.com    virtual void tagsInit(BaseCache *_cache) = 0;
1782810SN/A
1792810SN/A    /**
1809796Sprakash.ramrakhyani@arm.com     * Register local statistics.
1812810SN/A     */
1829796Sprakash.ramrakhyani@arm.com    void regStats();
1832810SN/A
1842810SN/A    /**
1852810SN/A     * Average in the reference count for valid blocks when the simulation
1862810SN/A     * exits.
1872810SN/A     */
18812728Snikos.nikoleris@arm.com    void cleanupRefs();
1897612SGene.Wu@arm.com
1907612SGene.Wu@arm.com    /**
19110024Sdam.sunwoo@arm.com     * Computes stats just prior to dump event
19210024Sdam.sunwoo@arm.com     */
19312728Snikos.nikoleris@arm.com    void computeStats();
19410024Sdam.sunwoo@arm.com
19510024Sdam.sunwoo@arm.com    /**
1969663Suri.wiener@arm.com     * Print all tags used
1979663Suri.wiener@arm.com     */
19812728Snikos.nikoleris@arm.com    std::string print();
19910815Sdavid.guillen@arm.com
20010815Sdavid.guillen@arm.com    /**
20113217Sodanrc@yahoo.com.br     * Finds the block in the cache without touching it.
20213217Sodanrc@yahoo.com.br     *
20313217Sodanrc@yahoo.com.br     * @param addr The address to look for.
20413217Sodanrc@yahoo.com.br     * @param is_secure True if the target memory space is secure.
20513217Sodanrc@yahoo.com.br     * @return Pointer to the cache block.
20610815Sdavid.guillen@arm.com     */
20713217Sodanrc@yahoo.com.br    virtual CacheBlk *findBlock(Addr addr, bool is_secure) const;
20810815Sdavid.guillen@arm.com
20910815Sdavid.guillen@arm.com    /**
21012743Sodanrc@yahoo.com.br     * Find a block given set and way.
21112743Sodanrc@yahoo.com.br     *
21212743Sodanrc@yahoo.com.br     * @param set The set of the block.
21312743Sodanrc@yahoo.com.br     * @param way The way of the block.
21412743Sodanrc@yahoo.com.br     * @return The block.
21512743Sodanrc@yahoo.com.br     */
21613219Sodanrc@yahoo.com.br    virtual ReplaceableEntry* findBlockBySetAndWay(int set, int way) const;
21712743Sodanrc@yahoo.com.br
21812743Sodanrc@yahoo.com.br    /**
21911893Snikos.nikoleris@arm.com     * Align an address to the block size.
22011893Snikos.nikoleris@arm.com     * @param addr the address to align.
22111893Snikos.nikoleris@arm.com     * @return The block address.
22211893Snikos.nikoleris@arm.com     */
22311893Snikos.nikoleris@arm.com    Addr blkAlign(Addr addr) const
22411893Snikos.nikoleris@arm.com    {
22511893Snikos.nikoleris@arm.com        return addr & ~blkMask;
22611893Snikos.nikoleris@arm.com    }
22711893Snikos.nikoleris@arm.com
22811893Snikos.nikoleris@arm.com    /**
22910815Sdavid.guillen@arm.com     * Calculate the block offset of an address.
23010815Sdavid.guillen@arm.com     * @param addr the address to get the offset of.
23110815Sdavid.guillen@arm.com     * @return the block offset.
23210815Sdavid.guillen@arm.com     */
23310815Sdavid.guillen@arm.com    int extractBlkOffset(Addr addr) const
23410815Sdavid.guillen@arm.com    {
23511893Snikos.nikoleris@arm.com        return (addr & blkMask);
23610815Sdavid.guillen@arm.com    }
23710815Sdavid.guillen@arm.com
23810941Sdavid.guillen@arm.com    /**
23910941Sdavid.guillen@arm.com     * Limit the allocation for the cache ways.
24010941Sdavid.guillen@arm.com     * @param ways The maximum number of ways available for replacement.
24110941Sdavid.guillen@arm.com     */
24210941Sdavid.guillen@arm.com    virtual void setWayAllocationMax(int ways)
24310941Sdavid.guillen@arm.com    {
24410941Sdavid.guillen@arm.com        panic("This tag class does not implement way allocation limit!\n");
24510941Sdavid.guillen@arm.com    }
24610941Sdavid.guillen@arm.com
24710941Sdavid.guillen@arm.com    /**
24810941Sdavid.guillen@arm.com     * Get the way allocation mask limit.
24910941Sdavid.guillen@arm.com     * @return The maximum number of ways available for replacement.
25010941Sdavid.guillen@arm.com     */
25110941Sdavid.guillen@arm.com    virtual int getWayAllocationMax() const
25210941Sdavid.guillen@arm.com    {
25310941Sdavid.guillen@arm.com        panic("This tag class does not implement way allocation limit!\n");
25410941Sdavid.guillen@arm.com        return -1;
25510941Sdavid.guillen@arm.com    }
25610941Sdavid.guillen@arm.com
25712566Snikos.nikoleris@arm.com    /**
25812704Snikos.nikoleris@arm.com     * This function updates the tags when a block is invalidated
25912704Snikos.nikoleris@arm.com     *
26012704Snikos.nikoleris@arm.com     * @param blk A valid block to invalidate.
26112566Snikos.nikoleris@arm.com     */
26212566Snikos.nikoleris@arm.com    virtual void invalidate(CacheBlk *blk)
26312566Snikos.nikoleris@arm.com    {
26412566Snikos.nikoleris@arm.com        assert(blk);
26512566Snikos.nikoleris@arm.com        assert(blk->isValid());
26612704Snikos.nikoleris@arm.com
26712566Snikos.nikoleris@arm.com        occupancies[blk->srcMasterId]--;
26812704Snikos.nikoleris@arm.com        totalRefs += blk->refCount;
26912704Snikos.nikoleris@arm.com        sampledRefs++;
27012704Snikos.nikoleris@arm.com
27112704Snikos.nikoleris@arm.com        blk->invalidate();
27212566Snikos.nikoleris@arm.com    }
27310815Sdavid.guillen@arm.com
27412600Sodanrc@yahoo.com.br    /**
27512744Sodanrc@yahoo.com.br     * Find replacement victim based on address. If the address requires
27612744Sodanrc@yahoo.com.br     * blocks to be evicted, their locations are listed for eviction. If a
27712744Sodanrc@yahoo.com.br     * conventional cache is being used, the list only contains the victim.
27812744Sodanrc@yahoo.com.br     * However, if using sector or compressed caches, the victim is one of
27912744Sodanrc@yahoo.com.br     * the blocks to be evicted, but its location is the only one that will
28012744Sodanrc@yahoo.com.br     * be assigned to the newly allocated block associated to this address.
28112744Sodanrc@yahoo.com.br     * @sa insertBlock
28212600Sodanrc@yahoo.com.br     *
28312600Sodanrc@yahoo.com.br     * @param addr Address to find a victim for.
28412746Sodanrc@yahoo.com.br     * @param is_secure True if the target memory space is secure.
28512744Sodanrc@yahoo.com.br     * @param evict_blks Cache blocks to be evicted.
28612600Sodanrc@yahoo.com.br     * @return Cache block to be replaced.
28712600Sodanrc@yahoo.com.br     */
28812746Sodanrc@yahoo.com.br    virtual CacheBlk* findVictim(Addr addr, const bool is_secure,
28912746Sodanrc@yahoo.com.br                                 std::vector<CacheBlk*>& evict_blks) const = 0;
29012600Sodanrc@yahoo.com.br
29113418Sodanrc@yahoo.com.br    /**
29213418Sodanrc@yahoo.com.br     * Access block and update replacement data. May not succeed, in which case
29313418Sodanrc@yahoo.com.br     * nullptr is returned. This has all the implications of a cache access and
29413418Sodanrc@yahoo.com.br     * should only be used as such. Returns the tag lookup latency as a side
29513418Sodanrc@yahoo.com.br     * effect.
29613418Sodanrc@yahoo.com.br     *
29713418Sodanrc@yahoo.com.br     * @param addr The address to find.
29813418Sodanrc@yahoo.com.br     * @param is_secure True if the target memory space is secure.
29913418Sodanrc@yahoo.com.br     * @param lat The latency of the tag lookup.
30013418Sodanrc@yahoo.com.br     * @return Pointer to the cache block if found.
30113418Sodanrc@yahoo.com.br     */
30211870Snikos.nikoleris@arm.com    virtual CacheBlk* accessBlock(Addr addr, bool is_secure, Cycles &lat) = 0;
30310815Sdavid.guillen@arm.com
30413219Sodanrc@yahoo.com.br    /**
30513219Sodanrc@yahoo.com.br     * Generate the tag from the given address.
30613219Sodanrc@yahoo.com.br     *
30713219Sodanrc@yahoo.com.br     * @param addr The address to get the tag from.
30813219Sodanrc@yahoo.com.br     * @return The tag of the address.
30913219Sodanrc@yahoo.com.br     */
31013219Sodanrc@yahoo.com.br    virtual Addr extractTag(const Addr addr) const;
31110815Sdavid.guillen@arm.com
31212636Sodanrc@yahoo.com.br    /**
31312636Sodanrc@yahoo.com.br     * Insert the new block into the cache and update stats.
31412636Sodanrc@yahoo.com.br     *
31513215Sodanrc@yahoo.com.br     * @param addr Address of the block.
31613215Sodanrc@yahoo.com.br     * @param is_secure Whether the block is in secure space or not.
31713215Sodanrc@yahoo.com.br     * @param src_master_ID The source requestor ID.
31813215Sodanrc@yahoo.com.br     * @param task_ID The new task ID.
31912636Sodanrc@yahoo.com.br     * @param blk The block to update.
32012636Sodanrc@yahoo.com.br     */
32113215Sodanrc@yahoo.com.br    virtual void insertBlock(const Addr addr, const bool is_secure,
32213215Sodanrc@yahoo.com.br                             const int src_master_ID, const uint32_t task_ID,
32313215Sodanrc@yahoo.com.br                             CacheBlk *blk);
32410815Sdavid.guillen@arm.com
32512574Sodanrc@yahoo.com.br    /**
32612574Sodanrc@yahoo.com.br     * Regenerate the block address.
32712574Sodanrc@yahoo.com.br     *
32812574Sodanrc@yahoo.com.br     * @param block The block.
32912574Sodanrc@yahoo.com.br     * @return the block address.
33012574Sodanrc@yahoo.com.br     */
33112574Sodanrc@yahoo.com.br    virtual Addr regenerateBlkAddr(const CacheBlk* blk) const = 0;
33210815Sdavid.guillen@arm.com
33312728Snikos.nikoleris@arm.com    /**
33412728Snikos.nikoleris@arm.com     * Visit each block in the tags and apply a visitor
33512728Snikos.nikoleris@arm.com     *
33612728Snikos.nikoleris@arm.com     * The visitor should be a std::function that takes a cache block
33712728Snikos.nikoleris@arm.com     * reference as its parameter.
33812728Snikos.nikoleris@arm.com     *
33912728Snikos.nikoleris@arm.com     * @param visitor Visitor to call on each block.
34012728Snikos.nikoleris@arm.com     */
34112728Snikos.nikoleris@arm.com    virtual void forEachBlk(std::function<void(CacheBlk &)> visitor) = 0;
34212728Snikos.nikoleris@arm.com
34312728Snikos.nikoleris@arm.com    /**
34412728Snikos.nikoleris@arm.com     * Find if any of the blocks satisfies a condition
34512728Snikos.nikoleris@arm.com     *
34612728Snikos.nikoleris@arm.com     * The visitor should be a std::function that takes a cache block
34712728Snikos.nikoleris@arm.com     * reference as its parameter. The visitor will terminate the
34812728Snikos.nikoleris@arm.com     * traversal early if the condition is satisfied.
34912728Snikos.nikoleris@arm.com     *
35012728Snikos.nikoleris@arm.com     * @param visitor Visitor to call on each block.
35112728Snikos.nikoleris@arm.com     */
35212728Snikos.nikoleris@arm.com    virtual bool anyBlk(std::function<bool(CacheBlk &)> visitor) = 0;
35312728Snikos.nikoleris@arm.com
35412728Snikos.nikoleris@arm.com  private:
35512728Snikos.nikoleris@arm.com    /**
35612728Snikos.nikoleris@arm.com     * Update the reference stats using data from the input block
35712728Snikos.nikoleris@arm.com     *
35812728Snikos.nikoleris@arm.com     * @param blk The input block
35912728Snikos.nikoleris@arm.com     */
36012728Snikos.nikoleris@arm.com    void cleanupRefsVisitor(CacheBlk &blk);
36112728Snikos.nikoleris@arm.com
36212728Snikos.nikoleris@arm.com    /**
36312728Snikos.nikoleris@arm.com     * Update the occupancy and age stats using data from the input block
36412728Snikos.nikoleris@arm.com     *
36512728Snikos.nikoleris@arm.com     * @param blk The input block
36612728Snikos.nikoleris@arm.com     */
36712728Snikos.nikoleris@arm.com    void computeStatsVisitor(CacheBlk &blk);
3682810SN/A};
3692810SN/A
3702810SN/Aclass BaseTagsCallback : public Callback
3712810SN/A{
3722810SN/A    BaseTags *tags;
3732810SN/A  public:
3742810SN/A    BaseTagsCallback(BaseTags *t) : tags(t) {}
3752810SN/A    virtual void process() { tags->cleanupRefs(); };
3762810SN/A};
3772810SN/A
37810024Sdam.sunwoo@arm.comclass BaseTagsDumpCallback : public Callback
37910024Sdam.sunwoo@arm.com{
38010024Sdam.sunwoo@arm.com    BaseTags *tags;
38110024Sdam.sunwoo@arm.com  public:
38210024Sdam.sunwoo@arm.com    BaseTagsDumpCallback(BaseTags *t) : tags(t) {}
38310024Sdam.sunwoo@arm.com    virtual void process() { tags->computeStats(); };
38410024Sdam.sunwoo@arm.com};
38510024Sdam.sunwoo@arm.com
38612492Sodanrc@yahoo.com.br#endif //__MEM_CACHE_TAGS_BASE_HH__
387