fa_lru.hh revision 9796
12810Srdreslin@umich.edu/*
29796Sprakash.ramrakhyani@arm.com * Copyright (c) 2012-2013 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"
579796Sprakash.ramrakhyani@arm.com#include "params/FALRU.hh"
582810Srdreslin@umich.edu
592810Srdreslin@umich.edu/**
602810Srdreslin@umich.edu * A fully associative cache block.
612810Srdreslin@umich.edu */
622810Srdreslin@umich.educlass FALRUBlk : public CacheBlk
632810Srdreslin@umich.edu{
642810Srdreslin@umich.edupublic:
652810Srdreslin@umich.edu    /** The previous block in LRU order. */
662810Srdreslin@umich.edu    FALRUBlk *prev;
672810Srdreslin@umich.edu    /** The next block in LRU order. */
682810Srdreslin@umich.edu    FALRUBlk *next;
692810Srdreslin@umich.edu    /** Has this block been touched? */
702810Srdreslin@umich.edu    bool isTouched;
712810Srdreslin@umich.edu
722810Srdreslin@umich.edu    /**
732810Srdreslin@umich.edu     * A bit mask of the sizes of cache that this block is resident in.
742810Srdreslin@umich.edu     * Each bit represents a power of 2 in MB size cache.
752810Srdreslin@umich.edu     * If bit 0 is set, this block is in a 1MB cache
762810Srdreslin@umich.edu     * If bit 2 is set, this block is in a 4MB cache, etc.
772810Srdreslin@umich.edu     * There is one bit for each cache smaller than the full size (default
782810Srdreslin@umich.edu     * 16MB).
792810Srdreslin@umich.edu     */
802810Srdreslin@umich.edu    int inCache;
812810Srdreslin@umich.edu};
822810Srdreslin@umich.edu
832810Srdreslin@umich.edu/**
842810Srdreslin@umich.edu * A fully associative LRU cache. Keeps statistics for accesses to a number of
852810Srdreslin@umich.edu * cache sizes at once.
862810Srdreslin@umich.edu */
872810Srdreslin@umich.educlass FALRU : public BaseTags
882810Srdreslin@umich.edu{
892810Srdreslin@umich.edu  public:
902810Srdreslin@umich.edu    /** Typedef the block type used in this class. */
912810Srdreslin@umich.edu    typedef FALRUBlk BlkType;
922810Srdreslin@umich.edu    /** Typedef a list of pointers to the local block type. */
932810Srdreslin@umich.edu    typedef std::list<FALRUBlk*> BlkList;
946227Snate@binkert.org
952810Srdreslin@umich.edu  protected:
962810Srdreslin@umich.edu    /** Array of pointers to blocks at the cache size  boundaries. */
972810Srdreslin@umich.edu    FALRUBlk **cacheBoundaries;
982810Srdreslin@umich.edu    /** A mask for the FALRUBlk::inCache bits. */
992810Srdreslin@umich.edu    int cacheMask;
1002810Srdreslin@umich.edu    /** The number of different size caches being tracked. */
1016227Snate@binkert.org    unsigned numCaches;
1022810Srdreslin@umich.edu
1032810Srdreslin@umich.edu    /** The cache blocks. */
1042810Srdreslin@umich.edu    FALRUBlk *blks;
1052810Srdreslin@umich.edu
1062810Srdreslin@umich.edu    /** The MRU block. */
1072810Srdreslin@umich.edu    FALRUBlk *head;
1082810Srdreslin@umich.edu    /** The LRU block. */
1092810Srdreslin@umich.edu    FALRUBlk *tail;
1102810Srdreslin@umich.edu
1112810Srdreslin@umich.edu    /** Hash table type mapping addresses to cache block pointers. */
1122810Srdreslin@umich.edu    typedef m5::hash_map<Addr, FALRUBlk *, m5::hash<Addr> > hash_t;
1132810Srdreslin@umich.edu    /** Iterator into the address hash table. */
1142810Srdreslin@umich.edu    typedef hash_t::const_iterator tagIterator;
1152810Srdreslin@umich.edu
1162810Srdreslin@umich.edu    /** The address hash table. */
1172810Srdreslin@umich.edu    hash_t tagHash;
1182810Srdreslin@umich.edu
1192810Srdreslin@umich.edu    /**
1202810Srdreslin@umich.edu     * Find the cache block for the given address.
1212810Srdreslin@umich.edu     * @param addr The address to find.
1222810Srdreslin@umich.edu     * @return The cache block of the address, if any.
1232810Srdreslin@umich.edu     */
1242810Srdreslin@umich.edu    FALRUBlk * hashLookup(Addr addr) const;
1252810Srdreslin@umich.edu
1262810Srdreslin@umich.edu    /**
1272810Srdreslin@umich.edu     * Move a cache block to the MRU position.
1282810Srdreslin@umich.edu     * @param blk The block to promote.
1292810Srdreslin@umich.edu     */
1302810Srdreslin@umich.edu    void moveToHead(FALRUBlk *blk);
1312810Srdreslin@umich.edu
1322810Srdreslin@umich.edu    /**
1332810Srdreslin@umich.edu     * Check to make sure all the cache boundaries are still where they should
1342810Srdreslin@umich.edu     * be. Used for debugging.
1352810Srdreslin@umich.edu     * @return True if everything is correct.
1362810Srdreslin@umich.edu     */
1372810Srdreslin@umich.edu    bool check();
1382810Srdreslin@umich.edu
1392810Srdreslin@umich.edu    /**
1402810Srdreslin@umich.edu     * @defgroup FALRUStats Fully Associative LRU specific statistics
1412810Srdreslin@umich.edu     * The FA lru stack lets us track multiple cache sizes at once. These
1422810Srdreslin@umich.edu     * statistics track the hits and misses for different cache sizes.
1432810Srdreslin@umich.edu     * @{
1442810Srdreslin@umich.edu     */
1452810Srdreslin@umich.edu
1462810Srdreslin@umich.edu    /** Hits in each cache size >= 128K. */
1475999Snate@binkert.org    Stats::Vector hits;
1482810Srdreslin@umich.edu    /** Misses in each cache size >= 128K. */
1495999Snate@binkert.org    Stats::Vector misses;
1502810Srdreslin@umich.edu    /** Total number of accesses. */
1515999Snate@binkert.org    Stats::Scalar accesses;
1522810Srdreslin@umich.edu
1532810Srdreslin@umich.edu    /**
1542810Srdreslin@umich.edu     * @}
1552810Srdreslin@umich.edu     */
1562810Srdreslin@umich.edu
1572810Srdreslin@umich.edupublic:
1589796Sprakash.ramrakhyani@arm.com
1599796Sprakash.ramrakhyani@arm.com    typedef FALRUParams Params;
1609796Sprakash.ramrakhyani@arm.com
1612810Srdreslin@umich.edu    /**
1622810Srdreslin@umich.edu     * Construct and initialize this cache tagstore.
1632810Srdreslin@umich.edu     */
1649796Sprakash.ramrakhyani@arm.com    FALRU(const Params *p);
1659086Sandreas.hansson@arm.com    ~FALRU();
1662810Srdreslin@umich.edu
1672810Srdreslin@umich.edu    /**
1682810Srdreslin@umich.edu     * Register the stats for this object.
1692810Srdreslin@umich.edu     * @param name The name to prepend to the stats name.
1702810Srdreslin@umich.edu     */
1719796Sprakash.ramrakhyani@arm.com    void regStats();
1722810Srdreslin@umich.edu
1732810Srdreslin@umich.edu    /**
1743862Sstever@eecs.umich.edu     * Invalidate a cache block.
1753862Sstever@eecs.umich.edu     * @param blk The block to invalidate.
1762810Srdreslin@umich.edu     */
1779214Slena@cs.wisc.edu    void invalidate(BlkType *blk);
1782810Srdreslin@umich.edu
1792810Srdreslin@umich.edu    /**
1805716Shsul@eecs.umich.edu     * Access block and update replacement data.  May not succeed, in which case
1815716Shsul@eecs.umich.edu     * NULL pointer is returned.  This has all the implications of a cache
1825716Shsul@eecs.umich.edu     * access and should only be used as such.
1835716Shsul@eecs.umich.edu     * Returns the access latency and inCache flags as a side effect.
1842810Srdreslin@umich.edu     * @param addr The address to look for.
1852810Srdreslin@umich.edu     * @param asid The address space ID.
1862810Srdreslin@umich.edu     * @param lat The latency of the access.
1872810Srdreslin@umich.edu     * @param inCache The FALRUBlk::inCache flags.
1882810Srdreslin@umich.edu     * @return Pointer to the cache block.
1892810Srdreslin@umich.edu     */
1909288Sandreas.hansson@arm.com    FALRUBlk* accessBlock(Addr addr, Cycles &lat, int context_src, int *inCache = 0);
1912810Srdreslin@umich.edu
1922810Srdreslin@umich.edu    /**
1932810Srdreslin@umich.edu     * Find the block in the cache, do not update the replacement data.
1942810Srdreslin@umich.edu     * @param addr The address to look for.
1952810Srdreslin@umich.edu     * @param asid The address space ID.
1962810Srdreslin@umich.edu     * @return Pointer to the cache block.
1972810Srdreslin@umich.edu     */
1982991Srdreslin@umich.edu    FALRUBlk* findBlock(Addr addr) const;
1992810Srdreslin@umich.edu
2002810Srdreslin@umich.edu    /**
2012810Srdreslin@umich.edu     * Find a replacement block for the address provided.
2022982Sstever@eecs.umich.edu     * @param pkt The request to a find a replacement candidate for.
2032810Srdreslin@umich.edu     * @param writebacks List for any writebacks to be performed.
2042810Srdreslin@umich.edu     * @return The block to place the replacement in.
2052810Srdreslin@umich.edu     */
2065717Shsul@eecs.umich.edu    FALRUBlk* findVictim(Addr addr, PacketList & writebacks);
2075717Shsul@eecs.umich.edu
2089796Sprakash.ramrakhyani@arm.com    void insertBlock(PacketPtr pkt, BlkType *blk);
2092810Srdreslin@umich.edu
2102810Srdreslin@umich.edu    /**
2112810Srdreslin@umich.edu     * Return the hit latency of this cache.
2122810Srdreslin@umich.edu     * @return The hit latency.
2132810Srdreslin@umich.edu     */
2149288Sandreas.hansson@arm.com    Cycles getHitLatency() const
2152810Srdreslin@umich.edu    {
2162810Srdreslin@umich.edu        return hitLatency;
2172810Srdreslin@umich.edu    }
2182810Srdreslin@umich.edu
2192810Srdreslin@umich.edu    /**
2202810Srdreslin@umich.edu     * Return the block size of this cache.
2212810Srdreslin@umich.edu     * @return The block size.
2222810Srdreslin@umich.edu     */
2236227Snate@binkert.org    unsigned
2246227Snate@binkert.org    getBlockSize() const
2252810Srdreslin@umich.edu    {
2262810Srdreslin@umich.edu        return blkSize;
2272810Srdreslin@umich.edu    }
2282810Srdreslin@umich.edu
2292810Srdreslin@umich.edu    /**
2302810Srdreslin@umich.edu     * Return the subblock size of this cache, always the block size.
2312810Srdreslin@umich.edu     * @return The block size.
2322810Srdreslin@umich.edu     */
2336227Snate@binkert.org    unsigned
2346227Snate@binkert.org    getSubBlockSize() const
2352810Srdreslin@umich.edu    {
2362810Srdreslin@umich.edu        return blkSize;
2372810Srdreslin@umich.edu    }
2382810Srdreslin@umich.edu
2392810Srdreslin@umich.edu    /**
2402810Srdreslin@umich.edu     * Align an address to the block size.
2412810Srdreslin@umich.edu     * @param addr the address to align.
2422810Srdreslin@umich.edu     * @return The aligned address.
2432810Srdreslin@umich.edu     */
2442810Srdreslin@umich.edu    Addr blkAlign(Addr addr) const
2452810Srdreslin@umich.edu    {
2462810Srdreslin@umich.edu        return (addr & ~(Addr)(blkSize-1));
2472810Srdreslin@umich.edu    }
2482810Srdreslin@umich.edu
2492810Srdreslin@umich.edu    /**
2502810Srdreslin@umich.edu     * Generate the tag from the addres. For fully associative this is just the
2512810Srdreslin@umich.edu     * block address.
2522810Srdreslin@umich.edu     * @param addr The address to get the tag from.
2532810Srdreslin@umich.edu     * @return The tag.
2542810Srdreslin@umich.edu     */
2554626Sstever@eecs.umich.edu    Addr extractTag(Addr addr) const
2562810Srdreslin@umich.edu    {
2572810Srdreslin@umich.edu        return blkAlign(addr);
2582810Srdreslin@umich.edu    }
2592810Srdreslin@umich.edu
2602810Srdreslin@umich.edu    /**
2612810Srdreslin@umich.edu     * Return the set of an address. Only one set in a fully associative cache.
2622810Srdreslin@umich.edu     * @param addr The address to get the set from.
2632810Srdreslin@umich.edu     * @return 0.
2642810Srdreslin@umich.edu     */
2652810Srdreslin@umich.edu    int extractSet(Addr addr) const
2662810Srdreslin@umich.edu    {
2672810Srdreslin@umich.edu        return 0;
2682810Srdreslin@umich.edu    }
2692810Srdreslin@umich.edu
2702810Srdreslin@umich.edu    /**
2712810Srdreslin@umich.edu     * Calculate the block offset of an address.
2722810Srdreslin@umich.edu     * @param addr the address to get the offset of.
2732810Srdreslin@umich.edu     * @return the block offset.
2742810Srdreslin@umich.edu     */
2752810Srdreslin@umich.edu    int extractBlkOffset(Addr addr) const
2762810Srdreslin@umich.edu    {
2772810Srdreslin@umich.edu        return (addr & (Addr)(blkSize-1));
2782810Srdreslin@umich.edu    }
2792810Srdreslin@umich.edu
2802810Srdreslin@umich.edu    /**
2812810Srdreslin@umich.edu     * Regenerate the block address from the tag and the set.
2822810Srdreslin@umich.edu     * @param tag The tag of the block.
2832810Srdreslin@umich.edu     * @param set The set the block belongs to.
2842810Srdreslin@umich.edu     * @return the block address.
2852810Srdreslin@umich.edu     */
2862810Srdreslin@umich.edu    Addr regenerateBlkAddr(Addr tag, int set) const
2872810Srdreslin@umich.edu    {
2882810Srdreslin@umich.edu        return (tag);
2892810Srdreslin@umich.edu    }
2907612SGene.Wu@arm.com
2917612SGene.Wu@arm.com    /**
2927612SGene.Wu@arm.com     *iterated through all blocks and clear all locks
2937612SGene.Wu@arm.com     *Needed to clear all lock tracking at once
2947612SGene.Wu@arm.com     */
2957612SGene.Wu@arm.com    virtual void clearLocks();
2969347SAndreas.Sandberg@arm.com
2979347SAndreas.Sandberg@arm.com    /**
2989663Suri.wiener@arm.com     * @todo Implement as in lru. Currently not used
2999663Suri.wiener@arm.com     */
3009663Suri.wiener@arm.com    virtual std::string print() const { return ""; }
3019663Suri.wiener@arm.com
3029663Suri.wiener@arm.com    /**
3039347SAndreas.Sandberg@arm.com     * Visit each block in the tag store and apply a visitor to the
3049347SAndreas.Sandberg@arm.com     * block.
3059347SAndreas.Sandberg@arm.com     *
3069347SAndreas.Sandberg@arm.com     * The visitor should be a function (or object that behaves like a
3079347SAndreas.Sandberg@arm.com     * function) that takes a cache block reference as its parameter
3089347SAndreas.Sandberg@arm.com     * and returns a bool. A visitor can request the traversal to be
3099347SAndreas.Sandberg@arm.com     * stopped by returning false, returning true causes it to be
3109347SAndreas.Sandberg@arm.com     * called for the next block in the tag store.
3119347SAndreas.Sandberg@arm.com     *
3129347SAndreas.Sandberg@arm.com     * \param visitor Visitor to call on each block.
3139347SAndreas.Sandberg@arm.com     */
3149347SAndreas.Sandberg@arm.com    template <typename V>
3159347SAndreas.Sandberg@arm.com    void forEachBlk(V &visitor) {
3169347SAndreas.Sandberg@arm.com        for (int i = 0; i < numBlocks; i++) {
3179347SAndreas.Sandberg@arm.com            if (!visitor(blks[i]))
3189347SAndreas.Sandberg@arm.com                return;
3199347SAndreas.Sandberg@arm.com        }
3209347SAndreas.Sandberg@arm.com    }
3219796Sprakash.ramrakhyani@arm.com
3222810Srdreslin@umich.edu};
3232810Srdreslin@umich.edu
3246216Snate@binkert.org#endif // __MEM_CACHE_TAGS_FA_LRU_HH__
325