base_set_assoc.hh revision 12679
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 <memory>
5412548Sodanrc@yahoo.com.br#include <vector>
5510263Satgutier@umich.edu
5611486Snikos.nikoleris@arm.com#include "mem/cache/base.hh"
5711486Snikos.nikoleris@arm.com#include "mem/cache/blk.hh"
5810263Satgutier@umich.edu#include "mem/cache/tags/base.hh"
5910263Satgutier@umich.edu#include "mem/cache/tags/cacheset.hh"
6010263Satgutier@umich.edu#include "mem/packet.hh"
6110263Satgutier@umich.edu#include "params/BaseSetAssoc.hh"
6210263Satgutier@umich.edu
6310263Satgutier@umich.edu/**
6410263Satgutier@umich.edu * A BaseSetAssoc cache tag store.
6510263Satgutier@umich.edu * @sa  \ref gem5MemorySystem "gem5 Memory System"
6610263Satgutier@umich.edu *
6712600Sodanrc@yahoo.com.br * The BaseSetAssoc placement policy divides the cache into s sets of w
6812600Sodanrc@yahoo.com.br * cache lines (ways). A cache line is mapped onto a set, and can be placed
6912600Sodanrc@yahoo.com.br * into any of the ways of this set.
7010263Satgutier@umich.edu */
7110263Satgutier@umich.educlass BaseSetAssoc : public BaseTags
7210263Satgutier@umich.edu{
7310263Satgutier@umich.edu  public:
7410263Satgutier@umich.edu    /** Typedef the block type used in this tag store. */
7510263Satgutier@umich.edu    typedef CacheBlk BlkType;
7610263Satgutier@umich.edu    /** Typedef the set type used in this tag store. */
7710263Satgutier@umich.edu    typedef CacheSet<CacheBlk> SetType;
7810263Satgutier@umich.edu
7910263Satgutier@umich.edu  protected:
8010263Satgutier@umich.edu    /** The associativity of the cache. */
8110263Satgutier@umich.edu    const unsigned assoc;
8210941Sdavid.guillen@arm.com    /** The allocatable associativity of the cache (alloc mask). */
8310941Sdavid.guillen@arm.com    unsigned allocAssoc;
8412548Sodanrc@yahoo.com.br
8512548Sodanrc@yahoo.com.br    /** The cache blocks. */
8612548Sodanrc@yahoo.com.br    std::vector<BlkType> blks;
8712548Sodanrc@yahoo.com.br
8810263Satgutier@umich.edu    /** The number of sets in the cache. */
8910263Satgutier@umich.edu    const unsigned numSets;
9012548Sodanrc@yahoo.com.br
9110263Satgutier@umich.edu    /** Whether tags and data are accessed sequentially. */
9210263Satgutier@umich.edu    const bool sequentialAccess;
9310263Satgutier@umich.edu
9410263Satgutier@umich.edu    /** The cache sets. */
9512548Sodanrc@yahoo.com.br    std::vector<SetType> sets;
9610263Satgutier@umich.edu
9710263Satgutier@umich.edu    /** The amount to shift the address to get the set. */
9810263Satgutier@umich.edu    int setShift;
9910263Satgutier@umich.edu    /** The amount to shift the address to get the tag. */
10010263Satgutier@umich.edu    int tagShift;
10110263Satgutier@umich.edu    /** Mask out all bits that aren't part of the set index. */
10210263Satgutier@umich.edu    unsigned setMask;
10310263Satgutier@umich.edu
10412600Sodanrc@yahoo.com.br    /** Replacement policy */
10512600Sodanrc@yahoo.com.br    BaseReplacementPolicy *replacementPolicy;
10612600Sodanrc@yahoo.com.br
10712600Sodanrc@yahoo.com.br  public:
10810263Satgutier@umich.edu
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    /**
12310941Sdavid.guillen@arm.com     * Find the cache block given set and way
12410941Sdavid.guillen@arm.com     * @param set The set of the block.
12510941Sdavid.guillen@arm.com     * @param way The way of the block.
12610941Sdavid.guillen@arm.com     * @return The cache block.
12710941Sdavid.guillen@arm.com     */
12811169Sandreas.hansson@arm.com    CacheBlk *findBlockBySetAndWay(int set, int way) const override;
12910941Sdavid.guillen@arm.com
13010941Sdavid.guillen@arm.com    /**
13110263Satgutier@umich.edu     * Access block and update replacement data. May not succeed, in which case
13211484Snikos.nikoleris@arm.com     * nullptr is returned. This has all the implications of a cache
13310263Satgutier@umich.edu     * access and should only be used as such. Returns the access latency as a
13410263Satgutier@umich.edu     * side effect.
13510263Satgutier@umich.edu     * @param addr The address to find.
13610263Satgutier@umich.edu     * @param is_secure True if the target memory space is secure.
13710263Satgutier@umich.edu     * @param lat The access latency.
13810263Satgutier@umich.edu     * @return Pointer to the cache block if found.
13910263Satgutier@umich.edu     */
14011870Snikos.nikoleris@arm.com    CacheBlk* accessBlock(Addr addr, bool is_secure, Cycles &lat) override
14110263Satgutier@umich.edu    {
14212555Sodanrc@yahoo.com.br        BlkType *blk = findBlock(addr, is_secure);
14310263Satgutier@umich.edu
14410263Satgutier@umich.edu        // Access all tags in parallel, hence one in each way.  The data side
14510263Satgutier@umich.edu        // either accesses all blocks in parallel, or one block sequentially on
14610263Satgutier@umich.edu        // a hit.  Sequential access with a miss doesn't access data.
14710941Sdavid.guillen@arm.com        tagAccesses += allocAssoc;
14810263Satgutier@umich.edu        if (sequentialAccess) {
14911484Snikos.nikoleris@arm.com            if (blk != nullptr) {
15010263Satgutier@umich.edu                dataAccesses += 1;
15110263Satgutier@umich.edu            }
15210263Satgutier@umich.edu        } else {
15310941Sdavid.guillen@arm.com            dataAccesses += allocAssoc;
15410263Satgutier@umich.edu        }
15510263Satgutier@umich.edu
15611484Snikos.nikoleris@arm.com        if (blk != nullptr) {
15711722Ssophiane.senni@gmail.com            // If a cache hit
15811722Ssophiane.senni@gmail.com            lat = accessLatency;
15911722Ssophiane.senni@gmail.com            // Check if the block to be accessed is available. If not,
16011722Ssophiane.senni@gmail.com            // apply the accessLatency on top of block->whenReady.
16111722Ssophiane.senni@gmail.com            if (blk->whenReady > curTick() &&
16211722Ssophiane.senni@gmail.com                cache->ticksToCycles(blk->whenReady - curTick()) >
16311722Ssophiane.senni@gmail.com                accessLatency) {
16411722Ssophiane.senni@gmail.com                lat = cache->ticksToCycles(blk->whenReady - curTick()) +
16511722Ssophiane.senni@gmail.com                accessLatency;
16610263Satgutier@umich.edu            }
16712600Sodanrc@yahoo.com.br
16812600Sodanrc@yahoo.com.br            // Update replacement data of accessed block
16912600Sodanrc@yahoo.com.br            replacementPolicy->touch(blk);
17011722Ssophiane.senni@gmail.com        } else {
17111722Ssophiane.senni@gmail.com            // If a cache miss
17211722Ssophiane.senni@gmail.com            lat = lookupLatency;
17310263Satgutier@umich.edu        }
17410263Satgutier@umich.edu
17510263Satgutier@umich.edu        return blk;
17610263Satgutier@umich.edu    }
17710263Satgutier@umich.edu
17810263Satgutier@umich.edu    /**
17910263Satgutier@umich.edu     * Finds the given address in the cache, do not update replacement data.
18010263Satgutier@umich.edu     * i.e. This is a no-side-effect find of a block.
18110263Satgutier@umich.edu     * @param addr The address to find.
18210263Satgutier@umich.edu     * @param is_secure True if the target memory space is secure.
18310263Satgutier@umich.edu     * @param asid The address space ID.
18410263Satgutier@umich.edu     * @return Pointer to the cache block if found.
18510263Satgutier@umich.edu     */
18611169Sandreas.hansson@arm.com    CacheBlk* findBlock(Addr addr, bool is_secure) const override;
18710263Satgutier@umich.edu
18810263Satgutier@umich.edu    /**
18912600Sodanrc@yahoo.com.br     * Find replacement victim based on address.
19012600Sodanrc@yahoo.com.br     *
19112600Sodanrc@yahoo.com.br     * @param addr Address to find a victim for.
19212600Sodanrc@yahoo.com.br     * @return Cache block to be replaced.
19310263Satgutier@umich.edu     */
19411169Sandreas.hansson@arm.com    CacheBlk* findVictim(Addr addr) override
19510263Satgutier@umich.edu    {
19612600Sodanrc@yahoo.com.br        // Choose replacement victim from replacement candidates
19712600Sodanrc@yahoo.com.br        return replacementPolicy->getVictim(getPossibleLocations(addr));
19812600Sodanrc@yahoo.com.br    }
19910263Satgutier@umich.edu
20012600Sodanrc@yahoo.com.br    /**
20112600Sodanrc@yahoo.com.br     * Find all possible block locations for insertion and replacement of
20212600Sodanrc@yahoo.com.br     * an address. Should be called immediately before ReplacementPolicy's
20312600Sodanrc@yahoo.com.br     * findVictim() not to break cache resizing.
20412600Sodanrc@yahoo.com.br     * Returns blocks in all ways belonging to the set of the address.
20512600Sodanrc@yahoo.com.br     *
20612600Sodanrc@yahoo.com.br     * @param addr The addr to a find possible locations for.
20712600Sodanrc@yahoo.com.br     * @return The possible locations.
20812600Sodanrc@yahoo.com.br     */
20912600Sodanrc@yahoo.com.br    const std::vector<CacheBlk*> getPossibleLocations(Addr addr)
21012600Sodanrc@yahoo.com.br    {
21112600Sodanrc@yahoo.com.br        return sets[extractSet(addr)].blks;
21210263Satgutier@umich.edu    }
21310263Satgutier@umich.edu
21410263Satgutier@umich.edu    /**
21512636Sodanrc@yahoo.com.br     * Insert the new block into the cache and update replacement data.
21612636Sodanrc@yahoo.com.br     *
21710263Satgutier@umich.edu     * @param pkt Packet holding the address to update
21810263Satgutier@umich.edu     * @param blk The block to update.
21910263Satgutier@umich.edu     */
22012636Sodanrc@yahoo.com.br    void insertBlock(PacketPtr pkt, CacheBlk *blk) override
22112636Sodanrc@yahoo.com.br    {
22212636Sodanrc@yahoo.com.br        // Insert block
22312636Sodanrc@yahoo.com.br        BaseTags::insertBlock(pkt, blk);
22410274Smitch.hayenga@arm.com
22512636Sodanrc@yahoo.com.br        // Update replacement policy
22612636Sodanrc@yahoo.com.br        replacementPolicy->reset(blk);
22712636Sodanrc@yahoo.com.br    }
22810263Satgutier@umich.edu
22910263Satgutier@umich.edu    /**
23010941Sdavid.guillen@arm.com     * Limit the allocation for the cache ways.
23110941Sdavid.guillen@arm.com     * @param ways The maximum number of ways available for replacement.
23210941Sdavid.guillen@arm.com     */
23311169Sandreas.hansson@arm.com    virtual void setWayAllocationMax(int ways) override
23410941Sdavid.guillen@arm.com    {
23510941Sdavid.guillen@arm.com        fatal_if(ways < 1, "Allocation limit must be greater than zero");
23610941Sdavid.guillen@arm.com        allocAssoc = ways;
23710941Sdavid.guillen@arm.com    }
23810941Sdavid.guillen@arm.com
23910941Sdavid.guillen@arm.com    /**
24010941Sdavid.guillen@arm.com     * Get the way allocation mask limit.
24110941Sdavid.guillen@arm.com     * @return The maximum number of ways available for replacement.
24210941Sdavid.guillen@arm.com     */
24311169Sandreas.hansson@arm.com    virtual int getWayAllocationMax() const override
24410941Sdavid.guillen@arm.com    {
24510941Sdavid.guillen@arm.com        return allocAssoc;
24610941Sdavid.guillen@arm.com    }
24710941Sdavid.guillen@arm.com
24810941Sdavid.guillen@arm.com    /**
24910263Satgutier@umich.edu     * Generate the tag from the given address.
25010263Satgutier@umich.edu     * @param addr The address to get the tag from.
25110263Satgutier@umich.edu     * @return The tag of the address.
25210263Satgutier@umich.edu     */
25311169Sandreas.hansson@arm.com    Addr extractTag(Addr addr) const override
25410263Satgutier@umich.edu    {
25510263Satgutier@umich.edu        return (addr >> tagShift);
25610263Satgutier@umich.edu    }
25710263Satgutier@umich.edu
25810263Satgutier@umich.edu    /**
25910263Satgutier@umich.edu     * Calculate the set index from the address.
26010263Satgutier@umich.edu     * @param addr The address to get the set from.
26110263Satgutier@umich.edu     * @return The set index of the address.
26210263Satgutier@umich.edu     */
26311169Sandreas.hansson@arm.com    int extractSet(Addr addr) const override
26410263Satgutier@umich.edu    {
26510263Satgutier@umich.edu        return ((addr >> setShift) & setMask);
26610263Satgutier@umich.edu    }
26710263Satgutier@umich.edu
26810263Satgutier@umich.edu    /**
26912574Sodanrc@yahoo.com.br     * Regenerate the block address from the tag and set.
27012574Sodanrc@yahoo.com.br     *
27112574Sodanrc@yahoo.com.br     * @param block The block.
27212574Sodanrc@yahoo.com.br     * @return the block address.
27310263Satgutier@umich.edu     */
27412574Sodanrc@yahoo.com.br    Addr regenerateBlkAddr(const CacheBlk* blk) const override
27510263Satgutier@umich.edu    {
27612574Sodanrc@yahoo.com.br        return ((blk->tag << tagShift) | ((Addr)blk->set << setShift));
27710263Satgutier@umich.edu    }
27810263Satgutier@umich.edu
27910263Satgutier@umich.edu    /**
28010263Satgutier@umich.edu     * Called at end of simulation to complete average block reference stats.
28110263Satgutier@umich.edu     */
28211169Sandreas.hansson@arm.com    void cleanupRefs() override;
28310263Satgutier@umich.edu
28410263Satgutier@umich.edu    /**
28510263Satgutier@umich.edu     * Print all tags used
28610263Satgutier@umich.edu     */
28711169Sandreas.hansson@arm.com    std::string print() const override;
28810263Satgutier@umich.edu
28910263Satgutier@umich.edu    /**
29010263Satgutier@umich.edu     * Called prior to dumping stats to compute task occupancy
29110263Satgutier@umich.edu     */
29211169Sandreas.hansson@arm.com    void computeStats() override;
29310263Satgutier@umich.edu
29410263Satgutier@umich.edu    /**
29510263Satgutier@umich.edu     * Visit each block in the tag store and apply a visitor to the
29610263Satgutier@umich.edu     * block.
29710263Satgutier@umich.edu     *
29810263Satgutier@umich.edu     * The visitor should be a function (or object that behaves like a
29910263Satgutier@umich.edu     * function) that takes a cache block reference as its parameter
30010263Satgutier@umich.edu     * and returns a bool. A visitor can request the traversal to be
30110263Satgutier@umich.edu     * stopped by returning false, returning true causes it to be
30210263Satgutier@umich.edu     * called for the next block in the tag store.
30310263Satgutier@umich.edu     *
30410263Satgutier@umich.edu     * \param visitor Visitor to call on each block.
30510263Satgutier@umich.edu     */
30611168Sandreas.hansson@arm.com    void forEachBlk(CacheBlkVisitor &visitor) override {
30712679Sodanrc@yahoo.com.br        for (CacheBlk& blk : blks) {
30812679Sodanrc@yahoo.com.br            if (!visitor(blk))
30910263Satgutier@umich.edu                return;
31010263Satgutier@umich.edu        }
31110263Satgutier@umich.edu    }
31210263Satgutier@umich.edu};
31310263Satgutier@umich.edu
31412492Sodanrc@yahoo.com.br#endif //__MEM_CACHE_TAGS_BASE_SET_ASSOC_HH__
315