fa_lru.hh revision 9663
12810Srdreslin@umich.edu/*
29347SAndreas.Sandberg@arm.com * Copyright (c) 2012 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 *
142810Srdreslin@umich.edu * Copyright (c) 2003-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 */
422810Srdreslin@umich.edu
432810Srdreslin@umich.edu/**
442810Srdreslin@umich.edu * @file
452810Srdreslin@umich.edu * Declaration of a fully associative LRU tag store.
462810Srdreslin@umich.edu */
472810Srdreslin@umich.edu
486216Snate@binkert.org#ifndef __MEM_CACHE_TAGS_FA_LRU_HH__
496216Snate@binkert.org#define __MEM_CACHE_TAGS_FA_LRU_HH__
502810Srdreslin@umich.edu
512810Srdreslin@umich.edu#include <list>
522810Srdreslin@umich.edu
536216Snate@binkert.org#include "base/hashmap.hh"
548229Snate@binkert.org#include "mem/cache/tags/base.hh"
555338Sstever@gmail.com#include "mem/cache/blk.hh"
562810Srdreslin@umich.edu#include "mem/packet.hh"
572810Srdreslin@umich.edu
582810Srdreslin@umich.edu/**
592810Srdreslin@umich.edu * A fully associative cache block.
602810Srdreslin@umich.edu */
612810Srdreslin@umich.educlass FALRUBlk : public CacheBlk
622810Srdreslin@umich.edu{
632810Srdreslin@umich.edupublic:
642810Srdreslin@umich.edu    /** The previous block in LRU order. */
652810Srdreslin@umich.edu    FALRUBlk *prev;
662810Srdreslin@umich.edu    /** The next block in LRU order. */
672810Srdreslin@umich.edu    FALRUBlk *next;
682810Srdreslin@umich.edu    /** Has this block been touched? */
692810Srdreslin@umich.edu    bool isTouched;
702810Srdreslin@umich.edu
712810Srdreslin@umich.edu    /**
722810Srdreslin@umich.edu     * A bit mask of the sizes of cache that this block is resident in.
732810Srdreslin@umich.edu     * Each bit represents a power of 2 in MB size cache.
742810Srdreslin@umich.edu     * If bit 0 is set, this block is in a 1MB cache
752810Srdreslin@umich.edu     * If bit 2 is set, this block is in a 4MB cache, etc.
762810Srdreslin@umich.edu     * There is one bit for each cache smaller than the full size (default
772810Srdreslin@umich.edu     * 16MB).
782810Srdreslin@umich.edu     */
792810Srdreslin@umich.edu    int inCache;
802810Srdreslin@umich.edu};
812810Srdreslin@umich.edu
822810Srdreslin@umich.edu/**
832810Srdreslin@umich.edu * A fully associative LRU cache. Keeps statistics for accesses to a number of
842810Srdreslin@umich.edu * cache sizes at once.
852810Srdreslin@umich.edu */
862810Srdreslin@umich.educlass FALRU : public BaseTags
872810Srdreslin@umich.edu{
882810Srdreslin@umich.edu  public:
892810Srdreslin@umich.edu    /** Typedef the block type used in this class. */
902810Srdreslin@umich.edu    typedef FALRUBlk BlkType;
912810Srdreslin@umich.edu    /** Typedef a list of pointers to the local block type. */
922810Srdreslin@umich.edu    typedef std::list<FALRUBlk*> BlkList;
936227Snate@binkert.org
942810Srdreslin@umich.edu  protected:
952810Srdreslin@umich.edu    /** The block size of the cache. */
966227Snate@binkert.org    const unsigned blkSize;
972810Srdreslin@umich.edu    /** The size of the cache. */
986227Snate@binkert.org    const unsigned size;
992810Srdreslin@umich.edu    /** The hit latency of the cache. */
1009288Sandreas.hansson@arm.com    const Cycles hitLatency;
1012810Srdreslin@umich.edu
1022810Srdreslin@umich.edu    /** Array of pointers to blocks at the cache size  boundaries. */
1032810Srdreslin@umich.edu    FALRUBlk **cacheBoundaries;
1042810Srdreslin@umich.edu    /** A mask for the FALRUBlk::inCache bits. */
1052810Srdreslin@umich.edu    int cacheMask;
1062810Srdreslin@umich.edu    /** The number of different size caches being tracked. */
1076227Snate@binkert.org    unsigned numCaches;
1082810Srdreslin@umich.edu
1092810Srdreslin@umich.edu    /** The cache blocks. */
1102810Srdreslin@umich.edu    FALRUBlk *blks;
1112810Srdreslin@umich.edu
1122810Srdreslin@umich.edu    /** The MRU block. */
1132810Srdreslin@umich.edu    FALRUBlk *head;
1142810Srdreslin@umich.edu    /** The LRU block. */
1152810Srdreslin@umich.edu    FALRUBlk *tail;
1162810Srdreslin@umich.edu
1172810Srdreslin@umich.edu    /** Hash table type mapping addresses to cache block pointers. */
1182810Srdreslin@umich.edu    typedef m5::hash_map<Addr, FALRUBlk *, m5::hash<Addr> > hash_t;
1192810Srdreslin@umich.edu    /** Iterator into the address hash table. */
1202810Srdreslin@umich.edu    typedef hash_t::const_iterator tagIterator;
1212810Srdreslin@umich.edu
1222810Srdreslin@umich.edu    /** The address hash table. */
1232810Srdreslin@umich.edu    hash_t tagHash;
1242810Srdreslin@umich.edu
1252810Srdreslin@umich.edu    /**
1262810Srdreslin@umich.edu     * Find the cache block for the given address.
1272810Srdreslin@umich.edu     * @param addr The address to find.
1282810Srdreslin@umich.edu     * @return The cache block of the address, if any.
1292810Srdreslin@umich.edu     */
1302810Srdreslin@umich.edu    FALRUBlk * hashLookup(Addr addr) const;
1312810Srdreslin@umich.edu
1322810Srdreslin@umich.edu    /**
1332810Srdreslin@umich.edu     * Move a cache block to the MRU position.
1342810Srdreslin@umich.edu     * @param blk The block to promote.
1352810Srdreslin@umich.edu     */
1362810Srdreslin@umich.edu    void moveToHead(FALRUBlk *blk);
1372810Srdreslin@umich.edu
1382810Srdreslin@umich.edu    /**
1392810Srdreslin@umich.edu     * Check to make sure all the cache boundaries are still where they should
1402810Srdreslin@umich.edu     * be. Used for debugging.
1412810Srdreslin@umich.edu     * @return True if everything is correct.
1422810Srdreslin@umich.edu     */
1432810Srdreslin@umich.edu    bool check();
1442810Srdreslin@umich.edu
1452810Srdreslin@umich.edu    /**
1462810Srdreslin@umich.edu     * @defgroup FALRUStats Fully Associative LRU specific statistics
1472810Srdreslin@umich.edu     * The FA lru stack lets us track multiple cache sizes at once. These
1482810Srdreslin@umich.edu     * statistics track the hits and misses for different cache sizes.
1492810Srdreslin@umich.edu     * @{
1502810Srdreslin@umich.edu     */
1512810Srdreslin@umich.edu
1522810Srdreslin@umich.edu    /** Hits in each cache size >= 128K. */
1535999Snate@binkert.org    Stats::Vector hits;
1542810Srdreslin@umich.edu    /** Misses in each cache size >= 128K. */
1555999Snate@binkert.org    Stats::Vector misses;
1562810Srdreslin@umich.edu    /** Total number of accesses. */
1575999Snate@binkert.org    Stats::Scalar accesses;
1582810Srdreslin@umich.edu
1592810Srdreslin@umich.edu    /**
1602810Srdreslin@umich.edu     * @}
1612810Srdreslin@umich.edu     */
1622810Srdreslin@umich.edu
1632810Srdreslin@umich.edupublic:
1642810Srdreslin@umich.edu    /**
1652810Srdreslin@umich.edu     * Construct and initialize this cache tagstore.
1662810Srdreslin@umich.edu     * @param blkSize The block size of the cache.
1672810Srdreslin@umich.edu     * @param size The size of the cache.
1682810Srdreslin@umich.edu     * @param hit_latency The hit latency of the cache.
1692810Srdreslin@umich.edu     */
1709288Sandreas.hansson@arm.com    FALRU(unsigned blkSize, unsigned size, Cycles hit_latency);
1719086Sandreas.hansson@arm.com    ~FALRU();
1722810Srdreslin@umich.edu
1732810Srdreslin@umich.edu    /**
1742810Srdreslin@umich.edu     * Register the stats for this object.
1752810Srdreslin@umich.edu     * @param name The name to prepend to the stats name.
1762810Srdreslin@umich.edu     */
1772810Srdreslin@umich.edu    void regStats(const std::string &name);
1782810Srdreslin@umich.edu
1792810Srdreslin@umich.edu    /**
1803862Sstever@eecs.umich.edu     * Invalidate a cache block.
1813862Sstever@eecs.umich.edu     * @param blk The block to invalidate.
1822810Srdreslin@umich.edu     */
1839214Slena@cs.wisc.edu    void invalidate(BlkType *blk);
1842810Srdreslin@umich.edu
1852810Srdreslin@umich.edu    /**
1865716Shsul@eecs.umich.edu     * Access block and update replacement data.  May not succeed, in which case
1875716Shsul@eecs.umich.edu     * NULL pointer is returned.  This has all the implications of a cache
1885716Shsul@eecs.umich.edu     * access and should only be used as such.
1895716Shsul@eecs.umich.edu     * Returns the access latency and inCache flags as a side effect.
1902810Srdreslin@umich.edu     * @param addr The address to look for.
1912810Srdreslin@umich.edu     * @param asid The address space ID.
1922810Srdreslin@umich.edu     * @param lat The latency of the access.
1932810Srdreslin@umich.edu     * @param inCache The FALRUBlk::inCache flags.
1942810Srdreslin@umich.edu     * @return Pointer to the cache block.
1952810Srdreslin@umich.edu     */
1969288Sandreas.hansson@arm.com    FALRUBlk* accessBlock(Addr addr, Cycles &lat, int context_src, int *inCache = 0);
1972810Srdreslin@umich.edu
1982810Srdreslin@umich.edu    /**
1992810Srdreslin@umich.edu     * Find the block in the cache, do not update the replacement data.
2002810Srdreslin@umich.edu     * @param addr The address to look for.
2012810Srdreslin@umich.edu     * @param asid The address space ID.
2022810Srdreslin@umich.edu     * @return Pointer to the cache block.
2032810Srdreslin@umich.edu     */
2042991Srdreslin@umich.edu    FALRUBlk* findBlock(Addr addr) const;
2052810Srdreslin@umich.edu
2062810Srdreslin@umich.edu    /**
2072810Srdreslin@umich.edu     * Find a replacement block for the address provided.
2082982Sstever@eecs.umich.edu     * @param pkt The request to a find a replacement candidate for.
2092810Srdreslin@umich.edu     * @param writebacks List for any writebacks to be performed.
2102810Srdreslin@umich.edu     * @return The block to place the replacement in.
2112810Srdreslin@umich.edu     */
2125717Shsul@eecs.umich.edu    FALRUBlk* findVictim(Addr addr, PacketList & writebacks);
2135717Shsul@eecs.umich.edu
2146817SLisa.Hsu@amd.com    void insertBlock(Addr addr, BlkType *blk, int context_src);
2152810Srdreslin@umich.edu
2162810Srdreslin@umich.edu    /**
2172810Srdreslin@umich.edu     * Return the hit latency of this cache.
2182810Srdreslin@umich.edu     * @return The hit latency.
2192810Srdreslin@umich.edu     */
2209288Sandreas.hansson@arm.com    Cycles getHitLatency() const
2212810Srdreslin@umich.edu    {
2222810Srdreslin@umich.edu        return hitLatency;
2232810Srdreslin@umich.edu    }
2242810Srdreslin@umich.edu
2252810Srdreslin@umich.edu    /**
2262810Srdreslin@umich.edu     * Return the block size of this cache.
2272810Srdreslin@umich.edu     * @return The block size.
2282810Srdreslin@umich.edu     */
2296227Snate@binkert.org    unsigned
2306227Snate@binkert.org    getBlockSize() const
2312810Srdreslin@umich.edu    {
2322810Srdreslin@umich.edu        return blkSize;
2332810Srdreslin@umich.edu    }
2342810Srdreslin@umich.edu
2352810Srdreslin@umich.edu    /**
2362810Srdreslin@umich.edu     * Return the subblock size of this cache, always the block size.
2372810Srdreslin@umich.edu     * @return The block size.
2382810Srdreslin@umich.edu     */
2396227Snate@binkert.org    unsigned
2406227Snate@binkert.org    getSubBlockSize() const
2412810Srdreslin@umich.edu    {
2422810Srdreslin@umich.edu        return blkSize;
2432810Srdreslin@umich.edu    }
2442810Srdreslin@umich.edu
2452810Srdreslin@umich.edu    /**
2462810Srdreslin@umich.edu     * Align an address to the block size.
2472810Srdreslin@umich.edu     * @param addr the address to align.
2482810Srdreslin@umich.edu     * @return The aligned address.
2492810Srdreslin@umich.edu     */
2502810Srdreslin@umich.edu    Addr blkAlign(Addr addr) const
2512810Srdreslin@umich.edu    {
2522810Srdreslin@umich.edu        return (addr & ~(Addr)(blkSize-1));
2532810Srdreslin@umich.edu    }
2542810Srdreslin@umich.edu
2552810Srdreslin@umich.edu    /**
2562810Srdreslin@umich.edu     * Generate the tag from the addres. For fully associative this is just the
2572810Srdreslin@umich.edu     * block address.
2582810Srdreslin@umich.edu     * @param addr The address to get the tag from.
2592810Srdreslin@umich.edu     * @return The tag.
2602810Srdreslin@umich.edu     */
2614626Sstever@eecs.umich.edu    Addr extractTag(Addr addr) const
2622810Srdreslin@umich.edu    {
2632810Srdreslin@umich.edu        return blkAlign(addr);
2642810Srdreslin@umich.edu    }
2652810Srdreslin@umich.edu
2662810Srdreslin@umich.edu    /**
2672810Srdreslin@umich.edu     * Return the set of an address. Only one set in a fully associative cache.
2682810Srdreslin@umich.edu     * @param addr The address to get the set from.
2692810Srdreslin@umich.edu     * @return 0.
2702810Srdreslin@umich.edu     */
2712810Srdreslin@umich.edu    int extractSet(Addr addr) const
2722810Srdreslin@umich.edu    {
2732810Srdreslin@umich.edu        return 0;
2742810Srdreslin@umich.edu    }
2752810Srdreslin@umich.edu
2762810Srdreslin@umich.edu    /**
2772810Srdreslin@umich.edu     * Calculate the block offset of an address.
2782810Srdreslin@umich.edu     * @param addr the address to get the offset of.
2792810Srdreslin@umich.edu     * @return the block offset.
2802810Srdreslin@umich.edu     */
2812810Srdreslin@umich.edu    int extractBlkOffset(Addr addr) const
2822810Srdreslin@umich.edu    {
2832810Srdreslin@umich.edu        return (addr & (Addr)(blkSize-1));
2842810Srdreslin@umich.edu    }
2852810Srdreslin@umich.edu
2862810Srdreslin@umich.edu    /**
2872810Srdreslin@umich.edu     * Regenerate the block address from the tag and the set.
2882810Srdreslin@umich.edu     * @param tag The tag of the block.
2892810Srdreslin@umich.edu     * @param set The set the block belongs to.
2902810Srdreslin@umich.edu     * @return the block address.
2912810Srdreslin@umich.edu     */
2922810Srdreslin@umich.edu    Addr regenerateBlkAddr(Addr tag, int set) const
2932810Srdreslin@umich.edu    {
2942810Srdreslin@umich.edu        return (tag);
2952810Srdreslin@umich.edu    }
2967612SGene.Wu@arm.com
2977612SGene.Wu@arm.com    /**
2987612SGene.Wu@arm.com     *iterated through all blocks and clear all locks
2997612SGene.Wu@arm.com     *Needed to clear all lock tracking at once
3007612SGene.Wu@arm.com     */
3017612SGene.Wu@arm.com    virtual void clearLocks();
3029347SAndreas.Sandberg@arm.com
3039347SAndreas.Sandberg@arm.com    /**
3049663Suri.wiener@arm.com     * @todo Implement as in lru. Currently not used
3059663Suri.wiener@arm.com     */
3069663Suri.wiener@arm.com    virtual std::string print() const { return ""; }
3079663Suri.wiener@arm.com
3089663Suri.wiener@arm.com    /**
3099347SAndreas.Sandberg@arm.com     * Visit each block in the tag store and apply a visitor to the
3109347SAndreas.Sandberg@arm.com     * block.
3119347SAndreas.Sandberg@arm.com     *
3129347SAndreas.Sandberg@arm.com     * The visitor should be a function (or object that behaves like a
3139347SAndreas.Sandberg@arm.com     * function) that takes a cache block reference as its parameter
3149347SAndreas.Sandberg@arm.com     * and returns a bool. A visitor can request the traversal to be
3159347SAndreas.Sandberg@arm.com     * stopped by returning false, returning true causes it to be
3169347SAndreas.Sandberg@arm.com     * called for the next block in the tag store.
3179347SAndreas.Sandberg@arm.com     *
3189347SAndreas.Sandberg@arm.com     * \param visitor Visitor to call on each block.
3199347SAndreas.Sandberg@arm.com     */
3209347SAndreas.Sandberg@arm.com    template <typename V>
3219347SAndreas.Sandberg@arm.com    void forEachBlk(V &visitor) {
3229347SAndreas.Sandberg@arm.com        for (int i = 0; i < numBlocks; i++) {
3239347SAndreas.Sandberg@arm.com            if (!visitor(blks[i]))
3249347SAndreas.Sandberg@arm.com                return;
3259347SAndreas.Sandberg@arm.com        }
3269347SAndreas.Sandberg@arm.com    }
3272810Srdreslin@umich.edu};
3282810Srdreslin@umich.edu
3296216Snate@binkert.org#endif // __MEM_CACHE_TAGS_FA_LRU_HH__
330