base_set_assoc.hh revision 12746
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
5112728Snikos.nikoleris@arm.com#include <functional>
5212727Snikos.nikoleris@arm.com#include <string>
5312548Sodanrc@yahoo.com.br#include <vector>
5410263Satgutier@umich.edu
5512727Snikos.nikoleris@arm.com#include "base/logging.hh"
5612727Snikos.nikoleris@arm.com#include "base/types.hh"
5712684Sodanrc@yahoo.com.br#include "debug/CacheRepl.hh"
5811486Snikos.nikoleris@arm.com#include "mem/cache/base.hh"
5911486Snikos.nikoleris@arm.com#include "mem/cache/blk.hh"
6012684Sodanrc@yahoo.com.br#include "mem/cache/replacement_policies/base.hh"
6110263Satgutier@umich.edu#include "mem/cache/tags/base.hh"
6210263Satgutier@umich.edu#include "mem/cache/tags/cacheset.hh"
6310263Satgutier@umich.edu#include "mem/packet.hh"
6410263Satgutier@umich.edu#include "params/BaseSetAssoc.hh"
6510263Satgutier@umich.edu
6610263Satgutier@umich.edu/**
6710263Satgutier@umich.edu * A BaseSetAssoc cache tag store.
6810263Satgutier@umich.edu * @sa  \ref gem5MemorySystem "gem5 Memory System"
6910263Satgutier@umich.edu *
7012600Sodanrc@yahoo.com.br * The BaseSetAssoc placement policy divides the cache into s sets of w
7112600Sodanrc@yahoo.com.br * cache lines (ways). A cache line is mapped onto a set, and can be placed
7212600Sodanrc@yahoo.com.br * into any of the ways of this set.
7310263Satgutier@umich.edu */
7410263Satgutier@umich.educlass BaseSetAssoc : public BaseTags
7510263Satgutier@umich.edu{
7610263Satgutier@umich.edu  public:
7710263Satgutier@umich.edu    /** Typedef the block type used in this tag store. */
7810263Satgutier@umich.edu    typedef CacheBlk BlkType;
7910263Satgutier@umich.edu    /** Typedef the set type used in this tag store. */
8010263Satgutier@umich.edu    typedef CacheSet<CacheBlk> SetType;
8110263Satgutier@umich.edu
8210263Satgutier@umich.edu  protected:
8310263Satgutier@umich.edu    /** The associativity of the cache. */
8410263Satgutier@umich.edu    const unsigned assoc;
8510941Sdavid.guillen@arm.com    /** The allocatable associativity of the cache (alloc mask). */
8610941Sdavid.guillen@arm.com    unsigned allocAssoc;
8712548Sodanrc@yahoo.com.br
8812548Sodanrc@yahoo.com.br    /** The cache blocks. */
8912548Sodanrc@yahoo.com.br    std::vector<BlkType> blks;
9012548Sodanrc@yahoo.com.br
9110263Satgutier@umich.edu    /** The number of sets in the cache. */
9210263Satgutier@umich.edu    const unsigned numSets;
9312548Sodanrc@yahoo.com.br
9410263Satgutier@umich.edu    /** Whether tags and data are accessed sequentially. */
9510263Satgutier@umich.edu    const bool sequentialAccess;
9610263Satgutier@umich.edu
9710263Satgutier@umich.edu    /** The cache sets. */
9812548Sodanrc@yahoo.com.br    std::vector<SetType> sets;
9910263Satgutier@umich.edu
10010263Satgutier@umich.edu    /** The amount to shift the address to get the set. */
10110263Satgutier@umich.edu    int setShift;
10210263Satgutier@umich.edu    /** The amount to shift the address to get the tag. */
10310263Satgutier@umich.edu    int tagShift;
10410263Satgutier@umich.edu    /** Mask out all bits that aren't part of the set index. */
10510263Satgutier@umich.edu    unsigned setMask;
10610263Satgutier@umich.edu
10712600Sodanrc@yahoo.com.br    /** Replacement policy */
10812600Sodanrc@yahoo.com.br    BaseReplacementPolicy *replacementPolicy;
10912600Sodanrc@yahoo.com.br
11012600Sodanrc@yahoo.com.br  public:
11110263Satgutier@umich.edu    /** Convenience typedef. */
11210263Satgutier@umich.edu     typedef BaseSetAssocParams Params;
11310263Satgutier@umich.edu
11410263Satgutier@umich.edu    /**
11510263Satgutier@umich.edu     * Construct and initialize this tag store.
11610263Satgutier@umich.edu     */
11710263Satgutier@umich.edu    BaseSetAssoc(const Params *p);
11810263Satgutier@umich.edu
11910263Satgutier@umich.edu    /**
12010263Satgutier@umich.edu     * Destructor
12110263Satgutier@umich.edu     */
12212548Sodanrc@yahoo.com.br    virtual ~BaseSetAssoc() {};
12310263Satgutier@umich.edu
12410263Satgutier@umich.edu    /**
12512745Sodanrc@yahoo.com.br     * This function updates the tags when a block is invalidated. It also
12612745Sodanrc@yahoo.com.br     * updates the replacement data.
12712684Sodanrc@yahoo.com.br     *
12812684Sodanrc@yahoo.com.br     * @param blk The block to invalidate.
12912684Sodanrc@yahoo.com.br     */
13012684Sodanrc@yahoo.com.br    void invalidate(CacheBlk *blk) override;
13112684Sodanrc@yahoo.com.br
13212684Sodanrc@yahoo.com.br    /**
13310263Satgutier@umich.edu     * Access block and update replacement data. May not succeed, in which case
13411484Snikos.nikoleris@arm.com     * nullptr is returned. This has all the implications of a cache
13510263Satgutier@umich.edu     * access and should only be used as such. Returns the access latency as a
13610263Satgutier@umich.edu     * side effect.
13710263Satgutier@umich.edu     * @param addr The address to find.
13810263Satgutier@umich.edu     * @param is_secure True if the target memory space is secure.
13910263Satgutier@umich.edu     * @param lat The access latency.
14010263Satgutier@umich.edu     * @return Pointer to the cache block if found.
14110263Satgutier@umich.edu     */
14211870Snikos.nikoleris@arm.com    CacheBlk* accessBlock(Addr addr, bool is_secure, Cycles &lat) override
14310263Satgutier@umich.edu    {
14412555Sodanrc@yahoo.com.br        BlkType *blk = findBlock(addr, is_secure);
14510263Satgutier@umich.edu
14610263Satgutier@umich.edu        // Access all tags in parallel, hence one in each way.  The data side
14710263Satgutier@umich.edu        // either accesses all blocks in parallel, or one block sequentially on
14810263Satgutier@umich.edu        // a hit.  Sequential access with a miss doesn't access data.
14910941Sdavid.guillen@arm.com        tagAccesses += allocAssoc;
15010263Satgutier@umich.edu        if (sequentialAccess) {
15111484Snikos.nikoleris@arm.com            if (blk != nullptr) {
15210263Satgutier@umich.edu                dataAccesses += 1;
15310263Satgutier@umich.edu            }
15410263Satgutier@umich.edu        } else {
15510941Sdavid.guillen@arm.com            dataAccesses += allocAssoc;
15610263Satgutier@umich.edu        }
15710263Satgutier@umich.edu
15811484Snikos.nikoleris@arm.com        if (blk != nullptr) {
15911722Ssophiane.senni@gmail.com            // If a cache hit
16011722Ssophiane.senni@gmail.com            lat = accessLatency;
16111722Ssophiane.senni@gmail.com            // Check if the block to be accessed is available. If not,
16211722Ssophiane.senni@gmail.com            // apply the accessLatency on top of block->whenReady.
16311722Ssophiane.senni@gmail.com            if (blk->whenReady > curTick() &&
16411722Ssophiane.senni@gmail.com                cache->ticksToCycles(blk->whenReady - curTick()) >
16511722Ssophiane.senni@gmail.com                accessLatency) {
16611722Ssophiane.senni@gmail.com                lat = cache->ticksToCycles(blk->whenReady - curTick()) +
16711722Ssophiane.senni@gmail.com                accessLatency;
16810263Satgutier@umich.edu            }
16912600Sodanrc@yahoo.com.br
17012684Sodanrc@yahoo.com.br            // Update number of references to accessed block
17112684Sodanrc@yahoo.com.br            blk->refCount++;
17212684Sodanrc@yahoo.com.br
17312600Sodanrc@yahoo.com.br            // Update replacement data of accessed block
17412684Sodanrc@yahoo.com.br            replacementPolicy->touch(blk->replacementData);
17511722Ssophiane.senni@gmail.com        } else {
17611722Ssophiane.senni@gmail.com            // If a cache miss
17711722Ssophiane.senni@gmail.com            lat = lookupLatency;
17810263Satgutier@umich.edu        }
17910263Satgutier@umich.edu
18010263Satgutier@umich.edu        return blk;
18110263Satgutier@umich.edu    }
18210263Satgutier@umich.edu
18310263Satgutier@umich.edu    /**
18410263Satgutier@umich.edu     * Finds the given address in the cache, do not update replacement data.
18510263Satgutier@umich.edu     * i.e. This is a no-side-effect find of a block.
18610263Satgutier@umich.edu     * @param addr The address to find.
18710263Satgutier@umich.edu     * @param is_secure True if the target memory space is secure.
18810263Satgutier@umich.edu     * @param asid The address space ID.
18910263Satgutier@umich.edu     * @return Pointer to the cache block if found.
19010263Satgutier@umich.edu     */
19111169Sandreas.hansson@arm.com    CacheBlk* findBlock(Addr addr, bool is_secure) const override;
19210263Satgutier@umich.edu
19310263Satgutier@umich.edu    /**
19412743Sodanrc@yahoo.com.br     * Find a block given set and way.
19512743Sodanrc@yahoo.com.br     *
19612743Sodanrc@yahoo.com.br     * @param set The set of the block.
19712743Sodanrc@yahoo.com.br     * @param way The way of the block.
19812743Sodanrc@yahoo.com.br     * @return The block.
19912743Sodanrc@yahoo.com.br     */
20012743Sodanrc@yahoo.com.br    ReplaceableEntry* findBlockBySetAndWay(int set, int way) const override;
20112743Sodanrc@yahoo.com.br
20212743Sodanrc@yahoo.com.br    /**
20312744Sodanrc@yahoo.com.br     * Find replacement victim based on address. The list of evicted blocks
20412744Sodanrc@yahoo.com.br     * only contains the victim.
20512600Sodanrc@yahoo.com.br     *
20612600Sodanrc@yahoo.com.br     * @param addr Address to find a victim for.
20712746Sodanrc@yahoo.com.br     * @param is_secure True if the target memory space is secure.
20812744Sodanrc@yahoo.com.br     * @param evict_blks Cache blocks to be evicted.
20912600Sodanrc@yahoo.com.br     * @return Cache block to be replaced.
21010263Satgutier@umich.edu     */
21112746Sodanrc@yahoo.com.br    CacheBlk* findVictim(Addr addr, const bool is_secure,
21212746Sodanrc@yahoo.com.br                         std::vector<CacheBlk*>& evict_blks) const override
21310263Satgutier@umich.edu    {
21412684Sodanrc@yahoo.com.br        // Get possible locations for the victim block
21512684Sodanrc@yahoo.com.br        std::vector<CacheBlk*> locations = getPossibleLocations(addr);
21612684Sodanrc@yahoo.com.br
21712600Sodanrc@yahoo.com.br        // Choose replacement victim from replacement candidates
21812684Sodanrc@yahoo.com.br        CacheBlk* victim = static_cast<CacheBlk*>(replacementPolicy->getVictim(
21912684Sodanrc@yahoo.com.br                               std::vector<ReplaceableEntry*>(
22012684Sodanrc@yahoo.com.br                                   locations.begin(), locations.end())));
22112684Sodanrc@yahoo.com.br
22212744Sodanrc@yahoo.com.br        // There is only one eviction for this replacement
22312744Sodanrc@yahoo.com.br        evict_blks.push_back(victim);
22412744Sodanrc@yahoo.com.br
22512684Sodanrc@yahoo.com.br        DPRINTF(CacheRepl, "set %x, way %x: selecting blk for replacement\n",
22612684Sodanrc@yahoo.com.br            victim->set, victim->way);
22712684Sodanrc@yahoo.com.br
22812684Sodanrc@yahoo.com.br        return victim;
22912600Sodanrc@yahoo.com.br    }
23010263Satgutier@umich.edu
23112600Sodanrc@yahoo.com.br    /**
23212600Sodanrc@yahoo.com.br     * Find all possible block locations for insertion and replacement of
23312600Sodanrc@yahoo.com.br     * an address. Should be called immediately before ReplacementPolicy's
23412600Sodanrc@yahoo.com.br     * findVictim() not to break cache resizing.
23512600Sodanrc@yahoo.com.br     * Returns blocks in all ways belonging to the set of the address.
23612600Sodanrc@yahoo.com.br     *
23712600Sodanrc@yahoo.com.br     * @param addr The addr to a find possible locations for.
23812600Sodanrc@yahoo.com.br     * @return The possible locations.
23912600Sodanrc@yahoo.com.br     */
24012744Sodanrc@yahoo.com.br    const std::vector<CacheBlk*> getPossibleLocations(Addr addr) const
24112600Sodanrc@yahoo.com.br    {
24212600Sodanrc@yahoo.com.br        return sets[extractSet(addr)].blks;
24310263Satgutier@umich.edu    }
24410263Satgutier@umich.edu
24510263Satgutier@umich.edu    /**
24612636Sodanrc@yahoo.com.br     * Insert the new block into the cache and update replacement data.
24712636Sodanrc@yahoo.com.br     *
24810263Satgutier@umich.edu     * @param pkt Packet holding the address to update
24910263Satgutier@umich.edu     * @param blk The block to update.
25010263Satgutier@umich.edu     */
25112636Sodanrc@yahoo.com.br    void insertBlock(PacketPtr pkt, CacheBlk *blk) override
25212636Sodanrc@yahoo.com.br    {
25312636Sodanrc@yahoo.com.br        // Insert block
25412636Sodanrc@yahoo.com.br        BaseTags::insertBlock(pkt, blk);
25510274Smitch.hayenga@arm.com
25612745Sodanrc@yahoo.com.br        // Increment tag counter
25712745Sodanrc@yahoo.com.br        tagsInUse++;
25812745Sodanrc@yahoo.com.br
25912636Sodanrc@yahoo.com.br        // Update replacement policy
26012684Sodanrc@yahoo.com.br        replacementPolicy->reset(blk->replacementData);
26112636Sodanrc@yahoo.com.br    }
26210263Satgutier@umich.edu
26310263Satgutier@umich.edu    /**
26410941Sdavid.guillen@arm.com     * Limit the allocation for the cache ways.
26510941Sdavid.guillen@arm.com     * @param ways The maximum number of ways available for replacement.
26610941Sdavid.guillen@arm.com     */
26711169Sandreas.hansson@arm.com    virtual void setWayAllocationMax(int ways) override
26810941Sdavid.guillen@arm.com    {
26910941Sdavid.guillen@arm.com        fatal_if(ways < 1, "Allocation limit must be greater than zero");
27010941Sdavid.guillen@arm.com        allocAssoc = ways;
27110941Sdavid.guillen@arm.com    }
27210941Sdavid.guillen@arm.com
27310941Sdavid.guillen@arm.com    /**
27410941Sdavid.guillen@arm.com     * Get the way allocation mask limit.
27510941Sdavid.guillen@arm.com     * @return The maximum number of ways available for replacement.
27610941Sdavid.guillen@arm.com     */
27711169Sandreas.hansson@arm.com    virtual int getWayAllocationMax() const override
27810941Sdavid.guillen@arm.com    {
27910941Sdavid.guillen@arm.com        return allocAssoc;
28010941Sdavid.guillen@arm.com    }
28110941Sdavid.guillen@arm.com
28210941Sdavid.guillen@arm.com    /**
28310263Satgutier@umich.edu     * Generate the tag from the given address.
28410263Satgutier@umich.edu     * @param addr The address to get the tag from.
28510263Satgutier@umich.edu     * @return The tag of the address.
28610263Satgutier@umich.edu     */
28711169Sandreas.hansson@arm.com    Addr extractTag(Addr addr) const override
28810263Satgutier@umich.edu    {
28910263Satgutier@umich.edu        return (addr >> tagShift);
29010263Satgutier@umich.edu    }
29110263Satgutier@umich.edu
29210263Satgutier@umich.edu    /**
29312574Sodanrc@yahoo.com.br     * Regenerate the block address from the tag and set.
29412574Sodanrc@yahoo.com.br     *
29512574Sodanrc@yahoo.com.br     * @param block The block.
29612574Sodanrc@yahoo.com.br     * @return the block address.
29710263Satgutier@umich.edu     */
29812574Sodanrc@yahoo.com.br    Addr regenerateBlkAddr(const CacheBlk* blk) const override
29910263Satgutier@umich.edu    {
30012574Sodanrc@yahoo.com.br        return ((blk->tag << tagShift) | ((Addr)blk->set << setShift));
30110263Satgutier@umich.edu    }
30210263Satgutier@umich.edu
30312728Snikos.nikoleris@arm.com    void forEachBlk(std::function<void(CacheBlk &)> visitor) override {
30412728Snikos.nikoleris@arm.com        for (CacheBlk& blk : blks) {
30512728Snikos.nikoleris@arm.com            visitor(blk);
30612728Snikos.nikoleris@arm.com        }
30712728Snikos.nikoleris@arm.com    }
30810263Satgutier@umich.edu
30912728Snikos.nikoleris@arm.com    bool anyBlk(std::function<bool(CacheBlk &)> visitor) override {
31012679Sodanrc@yahoo.com.br        for (CacheBlk& blk : blks) {
31112728Snikos.nikoleris@arm.com            if (visitor(blk)) {
31212728Snikos.nikoleris@arm.com                return true;
31312728Snikos.nikoleris@arm.com            }
31410263Satgutier@umich.edu        }
31512728Snikos.nikoleris@arm.com        return false;
31610263Satgutier@umich.edu    }
31712731Sodanrc@yahoo.com.br
31812731Sodanrc@yahoo.com.br  private:
31912731Sodanrc@yahoo.com.br    /**
32012731Sodanrc@yahoo.com.br     * Calculate the set index from the address.
32112731Sodanrc@yahoo.com.br     *
32212731Sodanrc@yahoo.com.br     * @param addr The address to get the set from.
32312731Sodanrc@yahoo.com.br     * @return The set index of the address.
32412731Sodanrc@yahoo.com.br     */
32512731Sodanrc@yahoo.com.br    int extractSet(Addr addr) const
32612731Sodanrc@yahoo.com.br    {
32712731Sodanrc@yahoo.com.br        return ((addr >> setShift) & setMask);
32812731Sodanrc@yahoo.com.br    }
32910263Satgutier@umich.edu};
33010263Satgutier@umich.edu
33112492Sodanrc@yahoo.com.br#endif //__MEM_CACHE_TAGS_BASE_SET_ASSOC_HH__
332