base_set_assoc.hh revision 12684
110263Satgutier@umich.edu/*
212566Snikos.nikoleris@arm.com * Copyright (c) 2012-2014,2017 ARM Limited
310263Satgutier@umich.edu * All rights reserved.
410263Satgutier@umich.edu *
510263Satgutier@umich.edu * The license below extends only to copyright in the software and shall
610263Satgutier@umich.edu * not be construed as granting a license to any other intellectual
710263Satgutier@umich.edu * property including but not limited to intellectual property relating
810263Satgutier@umich.edu * to a hardware implementation of the functionality of the software
910263Satgutier@umich.edu * licensed hereunder.  You may use the software subject to the license
1010263Satgutier@umich.edu * terms below provided that you ensure that this notice is replicated
1110263Satgutier@umich.edu * unmodified and in its entirety in all distributions of the software,
1210263Satgutier@umich.edu * modified or unmodified, in source code or in binary form.
1310263Satgutier@umich.edu *
1410263Satgutier@umich.edu * Copyright (c) 2003-2005,2014 The Regents of The University of Michigan
1510263Satgutier@umich.edu * All rights reserved.
1610263Satgutier@umich.edu *
1710263Satgutier@umich.edu * Redistribution and use in source and binary forms, with or without
1810263Satgutier@umich.edu * modification, are permitted provided that the following conditions are
1910263Satgutier@umich.edu * met: redistributions of source code must retain the above copyright
2010263Satgutier@umich.edu * notice, this list of conditions and the following disclaimer;
2110263Satgutier@umich.edu * redistributions in binary form must reproduce the above copyright
2210263Satgutier@umich.edu * notice, this list of conditions and the following disclaimer in the
2310263Satgutier@umich.edu * documentation and/or other materials provided with the distribution;
2410263Satgutier@umich.edu * neither the name of the copyright holders nor the names of its
2510263Satgutier@umich.edu * contributors may be used to endorse or promote products derived from
2610263Satgutier@umich.edu * this software without specific prior written permission.
2710263Satgutier@umich.edu *
2810263Satgutier@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2910263Satgutier@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3010263Satgutier@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3110263Satgutier@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3210263Satgutier@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3310263Satgutier@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3410263Satgutier@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3510263Satgutier@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3610263Satgutier@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3710263Satgutier@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3810263Satgutier@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3910263Satgutier@umich.edu *
4010263Satgutier@umich.edu * Authors: Erik Hallnor
4110263Satgutier@umich.edu */
4210263Satgutier@umich.edu
4310263Satgutier@umich.edu/**
4410263Satgutier@umich.edu * @file
4510263Satgutier@umich.edu * Declaration of a base set associative tag store.
4610263Satgutier@umich.edu */
4710263Satgutier@umich.edu
4812492Sodanrc@yahoo.com.br#ifndef __MEM_CACHE_TAGS_BASE_SET_ASSOC_HH__
4912492Sodanrc@yahoo.com.br#define __MEM_CACHE_TAGS_BASE_SET_ASSOC_HH__
5010263Satgutier@umich.edu
5110263Satgutier@umich.edu#include <cassert>
5210263Satgutier@umich.edu#include <cstring>
5312548Sodanrc@yahoo.com.br#include <vector>
5410263Satgutier@umich.edu
5512684Sodanrc@yahoo.com.br#include "debug/CacheRepl.hh"
5611486Snikos.nikoleris@arm.com#include "mem/cache/base.hh"
5711486Snikos.nikoleris@arm.com#include "mem/cache/blk.hh"
5812684Sodanrc@yahoo.com.br#include "mem/cache/replacement_policies/base.hh"
5910263Satgutier@umich.edu#include "mem/cache/tags/base.hh"
6010263Satgutier@umich.edu#include "mem/cache/tags/cacheset.hh"
6110263Satgutier@umich.edu#include "mem/packet.hh"
6210263Satgutier@umich.edu#include "params/BaseSetAssoc.hh"
6310263Satgutier@umich.edu
6410263Satgutier@umich.edu/**
6510263Satgutier@umich.edu * A BaseSetAssoc cache tag store.
6610263Satgutier@umich.edu * @sa  \ref gem5MemorySystem "gem5 Memory System"
6710263Satgutier@umich.edu *
6812600Sodanrc@yahoo.com.br * The BaseSetAssoc placement policy divides the cache into s sets of w
6912600Sodanrc@yahoo.com.br * cache lines (ways). A cache line is mapped onto a set, and can be placed
7012600Sodanrc@yahoo.com.br * into any of the ways of this set.
7110263Satgutier@umich.edu */
7210263Satgutier@umich.educlass BaseSetAssoc : public BaseTags
7310263Satgutier@umich.edu{
7410263Satgutier@umich.edu  public:
7510263Satgutier@umich.edu    /** Typedef the block type used in this tag store. */
7610263Satgutier@umich.edu    typedef CacheBlk BlkType;
7710263Satgutier@umich.edu    /** Typedef the set type used in this tag store. */
7810263Satgutier@umich.edu    typedef CacheSet<CacheBlk> SetType;
7910263Satgutier@umich.edu
8010263Satgutier@umich.edu  protected:
8110263Satgutier@umich.edu    /** The associativity of the cache. */
8210263Satgutier@umich.edu    const unsigned assoc;
8310941Sdavid.guillen@arm.com    /** The allocatable associativity of the cache (alloc mask). */
8410941Sdavid.guillen@arm.com    unsigned allocAssoc;
8512548Sodanrc@yahoo.com.br
8612548Sodanrc@yahoo.com.br    /** The cache blocks. */
8712548Sodanrc@yahoo.com.br    std::vector<BlkType> blks;
8812548Sodanrc@yahoo.com.br
8910263Satgutier@umich.edu    /** The number of sets in the cache. */
9010263Satgutier@umich.edu    const unsigned numSets;
9112548Sodanrc@yahoo.com.br
9210263Satgutier@umich.edu    /** Whether tags and data are accessed sequentially. */
9310263Satgutier@umich.edu    const bool sequentialAccess;
9410263Satgutier@umich.edu
9510263Satgutier@umich.edu    /** The cache sets. */
9612548Sodanrc@yahoo.com.br    std::vector<SetType> sets;
9710263Satgutier@umich.edu
9810263Satgutier@umich.edu    /** The amount to shift the address to get the set. */
9910263Satgutier@umich.edu    int setShift;
10010263Satgutier@umich.edu    /** The amount to shift the address to get the tag. */
10110263Satgutier@umich.edu    int tagShift;
10210263Satgutier@umich.edu    /** Mask out all bits that aren't part of the set index. */
10310263Satgutier@umich.edu    unsigned setMask;
10410263Satgutier@umich.edu
10512600Sodanrc@yahoo.com.br    /** Replacement policy */
10612600Sodanrc@yahoo.com.br    BaseReplacementPolicy *replacementPolicy;
10712600Sodanrc@yahoo.com.br
10812600Sodanrc@yahoo.com.br  public:
10910263Satgutier@umich.edu    /** Convenience typedef. */
11010263Satgutier@umich.edu     typedef BaseSetAssocParams Params;
11110263Satgutier@umich.edu
11210263Satgutier@umich.edu    /**
11310263Satgutier@umich.edu     * Construct and initialize this tag store.
11410263Satgutier@umich.edu     */
11510263Satgutier@umich.edu    BaseSetAssoc(const Params *p);
11610263Satgutier@umich.edu
11710263Satgutier@umich.edu    /**
11810263Satgutier@umich.edu     * Destructor
11910263Satgutier@umich.edu     */
12012548Sodanrc@yahoo.com.br    virtual ~BaseSetAssoc() {};
12110263Satgutier@umich.edu
12210263Satgutier@umich.edu    /**
12312684Sodanrc@yahoo.com.br     * This function updates the tags when a block is invalidated but does
12412684Sodanrc@yahoo.com.br     * not invalidate the block itself. It also updates the replacement data.
12512684Sodanrc@yahoo.com.br     *
12612684Sodanrc@yahoo.com.br     * @param blk The block to invalidate.
12712684Sodanrc@yahoo.com.br     */
12812684Sodanrc@yahoo.com.br    void invalidate(CacheBlk *blk) override;
12912684Sodanrc@yahoo.com.br
13012684Sodanrc@yahoo.com.br    /**
13110941Sdavid.guillen@arm.com     * Find the cache block given set and way
13210941Sdavid.guillen@arm.com     * @param set The set of the block.
13310941Sdavid.guillen@arm.com     * @param way The way of the block.
13410941Sdavid.guillen@arm.com     * @return The cache block.
13510941Sdavid.guillen@arm.com     */
13611169Sandreas.hansson@arm.com    CacheBlk *findBlockBySetAndWay(int set, int way) const override;
13710941Sdavid.guillen@arm.com
13810941Sdavid.guillen@arm.com    /**
13910263Satgutier@umich.edu     * Access block and update replacement data. May not succeed, in which case
14011484Snikos.nikoleris@arm.com     * nullptr is returned. This has all the implications of a cache
14110263Satgutier@umich.edu     * access and should only be used as such. Returns the access latency as a
14210263Satgutier@umich.edu     * side effect.
14310263Satgutier@umich.edu     * @param addr The address to find.
14410263Satgutier@umich.edu     * @param is_secure True if the target memory space is secure.
14510263Satgutier@umich.edu     * @param lat The access latency.
14610263Satgutier@umich.edu     * @return Pointer to the cache block if found.
14710263Satgutier@umich.edu     */
14811870Snikos.nikoleris@arm.com    CacheBlk* accessBlock(Addr addr, bool is_secure, Cycles &lat) override
14910263Satgutier@umich.edu    {
15012555Sodanrc@yahoo.com.br        BlkType *blk = findBlock(addr, is_secure);
15110263Satgutier@umich.edu
15210263Satgutier@umich.edu        // Access all tags in parallel, hence one in each way.  The data side
15310263Satgutier@umich.edu        // either accesses all blocks in parallel, or one block sequentially on
15410263Satgutier@umich.edu        // a hit.  Sequential access with a miss doesn't access data.
15510941Sdavid.guillen@arm.com        tagAccesses += allocAssoc;
15610263Satgutier@umich.edu        if (sequentialAccess) {
15711484Snikos.nikoleris@arm.com            if (blk != nullptr) {
15810263Satgutier@umich.edu                dataAccesses += 1;
15910263Satgutier@umich.edu            }
16010263Satgutier@umich.edu        } else {
16110941Sdavid.guillen@arm.com            dataAccesses += allocAssoc;
16210263Satgutier@umich.edu        }
16310263Satgutier@umich.edu
16411484Snikos.nikoleris@arm.com        if (blk != nullptr) {
16511722Ssophiane.senni@gmail.com            // If a cache hit
16611722Ssophiane.senni@gmail.com            lat = accessLatency;
16711722Ssophiane.senni@gmail.com            // Check if the block to be accessed is available. If not,
16811722Ssophiane.senni@gmail.com            // apply the accessLatency on top of block->whenReady.
16911722Ssophiane.senni@gmail.com            if (blk->whenReady > curTick() &&
17011722Ssophiane.senni@gmail.com                cache->ticksToCycles(blk->whenReady - curTick()) >
17111722Ssophiane.senni@gmail.com                accessLatency) {
17211722Ssophiane.senni@gmail.com                lat = cache->ticksToCycles(blk->whenReady - curTick()) +
17311722Ssophiane.senni@gmail.com                accessLatency;
17410263Satgutier@umich.edu            }
17512600Sodanrc@yahoo.com.br
17612684Sodanrc@yahoo.com.br            // Update number of references to accessed block
17712684Sodanrc@yahoo.com.br            blk->refCount++;
17812684Sodanrc@yahoo.com.br
17912600Sodanrc@yahoo.com.br            // Update replacement data of accessed block
18012684Sodanrc@yahoo.com.br            replacementPolicy->touch(blk->replacementData);
18111722Ssophiane.senni@gmail.com        } else {
18211722Ssophiane.senni@gmail.com            // If a cache miss
18311722Ssophiane.senni@gmail.com            lat = lookupLatency;
18410263Satgutier@umich.edu        }
18510263Satgutier@umich.edu
18610263Satgutier@umich.edu        return blk;
18710263Satgutier@umich.edu    }
18810263Satgutier@umich.edu
18910263Satgutier@umich.edu    /**
19010263Satgutier@umich.edu     * Finds the given address in the cache, do not update replacement data.
19110263Satgutier@umich.edu     * i.e. This is a no-side-effect find of a block.
19210263Satgutier@umich.edu     * @param addr The address to find.
19310263Satgutier@umich.edu     * @param is_secure True if the target memory space is secure.
19410263Satgutier@umich.edu     * @param asid The address space ID.
19510263Satgutier@umich.edu     * @return Pointer to the cache block if found.
19610263Satgutier@umich.edu     */
19711169Sandreas.hansson@arm.com    CacheBlk* findBlock(Addr addr, bool is_secure) const override;
19810263Satgutier@umich.edu
19910263Satgutier@umich.edu    /**
20012600Sodanrc@yahoo.com.br     * Find replacement victim based on address.
20112600Sodanrc@yahoo.com.br     *
20212600Sodanrc@yahoo.com.br     * @param addr Address to find a victim for.
20312600Sodanrc@yahoo.com.br     * @return Cache block to be replaced.
20410263Satgutier@umich.edu     */
20511169Sandreas.hansson@arm.com    CacheBlk* findVictim(Addr addr) override
20610263Satgutier@umich.edu    {
20712684Sodanrc@yahoo.com.br        // Get possible locations for the victim block
20812684Sodanrc@yahoo.com.br        std::vector<CacheBlk*> locations = getPossibleLocations(addr);
20912684Sodanrc@yahoo.com.br
21012600Sodanrc@yahoo.com.br        // Choose replacement victim from replacement candidates
21112684Sodanrc@yahoo.com.br        CacheBlk* victim = static_cast<CacheBlk*>(replacementPolicy->getVictim(
21212684Sodanrc@yahoo.com.br                               std::vector<ReplaceableEntry*>(
21312684Sodanrc@yahoo.com.br                                   locations.begin(), locations.end())));
21412684Sodanrc@yahoo.com.br
21512684Sodanrc@yahoo.com.br        DPRINTF(CacheRepl, "set %x, way %x: selecting blk for replacement\n",
21612684Sodanrc@yahoo.com.br            victim->set, victim->way);
21712684Sodanrc@yahoo.com.br
21812684Sodanrc@yahoo.com.br        return victim;
21912600Sodanrc@yahoo.com.br    }
22010263Satgutier@umich.edu
22112600Sodanrc@yahoo.com.br    /**
22212600Sodanrc@yahoo.com.br     * Find all possible block locations for insertion and replacement of
22312600Sodanrc@yahoo.com.br     * an address. Should be called immediately before ReplacementPolicy's
22412600Sodanrc@yahoo.com.br     * findVictim() not to break cache resizing.
22512600Sodanrc@yahoo.com.br     * Returns blocks in all ways belonging to the set of the address.
22612600Sodanrc@yahoo.com.br     *
22712600Sodanrc@yahoo.com.br     * @param addr The addr to a find possible locations for.
22812600Sodanrc@yahoo.com.br     * @return The possible locations.
22912600Sodanrc@yahoo.com.br     */
23012600Sodanrc@yahoo.com.br    const std::vector<CacheBlk*> getPossibleLocations(Addr addr)
23112600Sodanrc@yahoo.com.br    {
23212600Sodanrc@yahoo.com.br        return sets[extractSet(addr)].blks;
23310263Satgutier@umich.edu    }
23410263Satgutier@umich.edu
23510263Satgutier@umich.edu    /**
23612636Sodanrc@yahoo.com.br     * Insert the new block into the cache and update replacement data.
23712636Sodanrc@yahoo.com.br     *
23810263Satgutier@umich.edu     * @param pkt Packet holding the address to update
23910263Satgutier@umich.edu     * @param blk The block to update.
24010263Satgutier@umich.edu     */
24112636Sodanrc@yahoo.com.br    void insertBlock(PacketPtr pkt, CacheBlk *blk) override
24212636Sodanrc@yahoo.com.br    {
24312636Sodanrc@yahoo.com.br        // Insert block
24412636Sodanrc@yahoo.com.br        BaseTags::insertBlock(pkt, blk);
24510274Smitch.hayenga@arm.com
24612636Sodanrc@yahoo.com.br        // Update replacement policy
24712684Sodanrc@yahoo.com.br        replacementPolicy->reset(blk->replacementData);
24812636Sodanrc@yahoo.com.br    }
24910263Satgutier@umich.edu
25010263Satgutier@umich.edu    /**
25110941Sdavid.guillen@arm.com     * Limit the allocation for the cache ways.
25210941Sdavid.guillen@arm.com     * @param ways The maximum number of ways available for replacement.
25310941Sdavid.guillen@arm.com     */
25411169Sandreas.hansson@arm.com    virtual void setWayAllocationMax(int ways) override
25510941Sdavid.guillen@arm.com    {
25610941Sdavid.guillen@arm.com        fatal_if(ways < 1, "Allocation limit must be greater than zero");
25710941Sdavid.guillen@arm.com        allocAssoc = ways;
25810941Sdavid.guillen@arm.com    }
25910941Sdavid.guillen@arm.com
26010941Sdavid.guillen@arm.com    /**
26110941Sdavid.guillen@arm.com     * Get the way allocation mask limit.
26210941Sdavid.guillen@arm.com     * @return The maximum number of ways available for replacement.
26310941Sdavid.guillen@arm.com     */
26411169Sandreas.hansson@arm.com    virtual int getWayAllocationMax() const override
26510941Sdavid.guillen@arm.com    {
26610941Sdavid.guillen@arm.com        return allocAssoc;
26710941Sdavid.guillen@arm.com    }
26810941Sdavid.guillen@arm.com
26910941Sdavid.guillen@arm.com    /**
27010263Satgutier@umich.edu     * Generate the tag from the given address.
27110263Satgutier@umich.edu     * @param addr The address to get the tag from.
27210263Satgutier@umich.edu     * @return The tag of the address.
27310263Satgutier@umich.edu     */
27411169Sandreas.hansson@arm.com    Addr extractTag(Addr addr) const override
27510263Satgutier@umich.edu    {
27610263Satgutier@umich.edu        return (addr >> tagShift);
27710263Satgutier@umich.edu    }
27810263Satgutier@umich.edu
27910263Satgutier@umich.edu    /**
28010263Satgutier@umich.edu     * Calculate the set index from the address.
28110263Satgutier@umich.edu     * @param addr The address to get the set from.
28210263Satgutier@umich.edu     * @return The set index of the address.
28310263Satgutier@umich.edu     */
28411169Sandreas.hansson@arm.com    int extractSet(Addr addr) const override
28510263Satgutier@umich.edu    {
28610263Satgutier@umich.edu        return ((addr >> setShift) & setMask);
28710263Satgutier@umich.edu    }
28810263Satgutier@umich.edu
28910263Satgutier@umich.edu    /**
29012574Sodanrc@yahoo.com.br     * Regenerate the block address from the tag and set.
29112574Sodanrc@yahoo.com.br     *
29212574Sodanrc@yahoo.com.br     * @param block The block.
29312574Sodanrc@yahoo.com.br     * @return the block address.
29410263Satgutier@umich.edu     */
29512574Sodanrc@yahoo.com.br    Addr regenerateBlkAddr(const CacheBlk* blk) const override
29610263Satgutier@umich.edu    {
29712574Sodanrc@yahoo.com.br        return ((blk->tag << tagShift) | ((Addr)blk->set << setShift));
29810263Satgutier@umich.edu    }
29910263Satgutier@umich.edu
30010263Satgutier@umich.edu    /**
30110263Satgutier@umich.edu     * Called at end of simulation to complete average block reference stats.
30210263Satgutier@umich.edu     */
30311169Sandreas.hansson@arm.com    void cleanupRefs() override;
30410263Satgutier@umich.edu
30510263Satgutier@umich.edu    /**
30610263Satgutier@umich.edu     * Print all tags used
30710263Satgutier@umich.edu     */
30811169Sandreas.hansson@arm.com    std::string print() const override;
30910263Satgutier@umich.edu
31010263Satgutier@umich.edu    /**
31110263Satgutier@umich.edu     * Called prior to dumping stats to compute task occupancy
31210263Satgutier@umich.edu     */
31311169Sandreas.hansson@arm.com    void computeStats() override;
31410263Satgutier@umich.edu
31510263Satgutier@umich.edu    /**
31610263Satgutier@umich.edu     * Visit each block in the tag store and apply a visitor to the
31710263Satgutier@umich.edu     * block.
31810263Satgutier@umich.edu     *
31910263Satgutier@umich.edu     * The visitor should be a function (or object that behaves like a
32010263Satgutier@umich.edu     * function) that takes a cache block reference as its parameter
32110263Satgutier@umich.edu     * and returns a bool. A visitor can request the traversal to be
32210263Satgutier@umich.edu     * stopped by returning false, returning true causes it to be
32310263Satgutier@umich.edu     * called for the next block in the tag store.
32410263Satgutier@umich.edu     *
32510263Satgutier@umich.edu     * \param visitor Visitor to call on each block.
32610263Satgutier@umich.edu     */
32711168Sandreas.hansson@arm.com    void forEachBlk(CacheBlkVisitor &visitor) override {
32812679Sodanrc@yahoo.com.br        for (CacheBlk& blk : blks) {
32912679Sodanrc@yahoo.com.br            if (!visitor(blk))
33010263Satgutier@umich.edu                return;
33110263Satgutier@umich.edu        }
33210263Satgutier@umich.edu    }
33310263Satgutier@umich.edu};
33410263Satgutier@umich.edu
33512492Sodanrc@yahoo.com.br#endif //__MEM_CACHE_TAGS_BASE_SET_ASSOC_HH__
336