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
5113941Sodanrc@yahoo.com.br#include <cstdint>
5212728Snikos.nikoleris@arm.com#include <functional>
5312727Snikos.nikoleris@arm.com#include <string>
5412548Sodanrc@yahoo.com.br#include <vector>
5510263Satgutier@umich.edu
5612727Snikos.nikoleris@arm.com#include "base/logging.hh"
5712727Snikos.nikoleris@arm.com#include "base/types.hh"
5811486Snikos.nikoleris@arm.com#include "mem/cache/base.hh"
5913223Sodanrc@yahoo.com.br#include "mem/cache/cache_blk.hh"
6012684Sodanrc@yahoo.com.br#include "mem/cache/replacement_policies/base.hh"
6113225Sodanrc@yahoo.com.br#include "mem/cache/replacement_policies/replaceable_entry.hh"
6210263Satgutier@umich.edu#include "mem/cache/tags/base.hh"
6313219Sodanrc@yahoo.com.br#include "mem/cache/tags/indexing_policies/base.hh"
6413752Sodanrc@yahoo.com.br#include "mem/packet.hh"
6510263Satgutier@umich.edu#include "params/BaseSetAssoc.hh"
6610263Satgutier@umich.edu
6710263Satgutier@umich.edu/**
6813219Sodanrc@yahoo.com.br * A basic cache tag store.
6910263Satgutier@umich.edu * @sa  \ref gem5MemorySystem "gem5 Memory System"
7010263Satgutier@umich.edu *
7112600Sodanrc@yahoo.com.br * The BaseSetAssoc placement policy divides the cache into s sets of w
7213219Sodanrc@yahoo.com.br * cache lines (ways).
7310263Satgutier@umich.edu */
7410263Satgutier@umich.educlass BaseSetAssoc : public BaseTags
7510263Satgutier@umich.edu{
7610263Satgutier@umich.edu  protected:
7710941Sdavid.guillen@arm.com    /** The allocatable associativity of the cache (alloc mask). */
7810941Sdavid.guillen@arm.com    unsigned allocAssoc;
7912548Sodanrc@yahoo.com.br
8012548Sodanrc@yahoo.com.br    /** The cache blocks. */
8113220Sodanrc@yahoo.com.br    std::vector<CacheBlk> blks;
8212548Sodanrc@yahoo.com.br
8310263Satgutier@umich.edu    /** Whether tags and data are accessed sequentially. */
8410263Satgutier@umich.edu    const bool sequentialAccess;
8510263Satgutier@umich.edu
8612600Sodanrc@yahoo.com.br    /** Replacement policy */
8712600Sodanrc@yahoo.com.br    BaseReplacementPolicy *replacementPolicy;
8812600Sodanrc@yahoo.com.br
8912600Sodanrc@yahoo.com.br  public:
9010263Satgutier@umich.edu    /** Convenience typedef. */
9110263Satgutier@umich.edu     typedef BaseSetAssocParams Params;
9210263Satgutier@umich.edu
9310263Satgutier@umich.edu    /**
9410263Satgutier@umich.edu     * Construct and initialize this tag store.
9510263Satgutier@umich.edu     */
9610263Satgutier@umich.edu    BaseSetAssoc(const Params *p);
9710263Satgutier@umich.edu
9810263Satgutier@umich.edu    /**
9910263Satgutier@umich.edu     * Destructor
10010263Satgutier@umich.edu     */
10112548Sodanrc@yahoo.com.br    virtual ~BaseSetAssoc() {};
10210263Satgutier@umich.edu
10310263Satgutier@umich.edu    /**
10413419Sodanrc@yahoo.com.br     * Initialize blocks as CacheBlk instances.
10513216Sodanrc@yahoo.com.br     */
10613419Sodanrc@yahoo.com.br    void tagsInit() override;
10713216Sodanrc@yahoo.com.br
10813216Sodanrc@yahoo.com.br    /**
10912745Sodanrc@yahoo.com.br     * This function updates the tags when a block is invalidated. It also
11012745Sodanrc@yahoo.com.br     * updates the replacement data.
11112684Sodanrc@yahoo.com.br     *
11212684Sodanrc@yahoo.com.br     * @param blk The block to invalidate.
11312684Sodanrc@yahoo.com.br     */
11412684Sodanrc@yahoo.com.br    void invalidate(CacheBlk *blk) override;
11512684Sodanrc@yahoo.com.br
11612684Sodanrc@yahoo.com.br    /**
11710263Satgutier@umich.edu     * Access block and update replacement data. May not succeed, in which case
11813418Sodanrc@yahoo.com.br     * nullptr is returned. This has all the implications of a cache access and
11913418Sodanrc@yahoo.com.br     * should only be used as such. Returns the tag lookup latency as a side
12013418Sodanrc@yahoo.com.br     * effect.
12113418Sodanrc@yahoo.com.br     *
12210263Satgutier@umich.edu     * @param addr The address to find.
12310263Satgutier@umich.edu     * @param is_secure True if the target memory space is secure.
12413418Sodanrc@yahoo.com.br     * @param lat The latency of the tag lookup.
12510263Satgutier@umich.edu     * @return Pointer to the cache block if found.
12610263Satgutier@umich.edu     */
12711870Snikos.nikoleris@arm.com    CacheBlk* accessBlock(Addr addr, bool is_secure, Cycles &lat) override
12810263Satgutier@umich.edu    {
12913220Sodanrc@yahoo.com.br        CacheBlk *blk = findBlock(addr, is_secure);
13010263Satgutier@umich.edu
13110263Satgutier@umich.edu        // Access all tags in parallel, hence one in each way.  The data side
13210263Satgutier@umich.edu        // either accesses all blocks in parallel, or one block sequentially on
13310263Satgutier@umich.edu        // a hit.  Sequential access with a miss doesn't access data.
13410941Sdavid.guillen@arm.com        tagAccesses += allocAssoc;
13510263Satgutier@umich.edu        if (sequentialAccess) {
13611484Snikos.nikoleris@arm.com            if (blk != nullptr) {
13710263Satgutier@umich.edu                dataAccesses += 1;
13810263Satgutier@umich.edu            }
13910263Satgutier@umich.edu        } else {
14010941Sdavid.guillen@arm.com            dataAccesses += allocAssoc;
14110263Satgutier@umich.edu        }
14210263Satgutier@umich.edu
14313418Sodanrc@yahoo.com.br        // If a cache hit
14411484Snikos.nikoleris@arm.com        if (blk != nullptr) {
14512684Sodanrc@yahoo.com.br            // Update number of references to accessed block
14612684Sodanrc@yahoo.com.br            blk->refCount++;
14712684Sodanrc@yahoo.com.br
14812600Sodanrc@yahoo.com.br            // Update replacement data of accessed block
14912684Sodanrc@yahoo.com.br            replacementPolicy->touch(blk->replacementData);
15010263Satgutier@umich.edu        }
15110263Satgutier@umich.edu
15213418Sodanrc@yahoo.com.br        // The tag lookup latency is the same for a hit or a miss
15313418Sodanrc@yahoo.com.br        lat = lookupLatency;
15413418Sodanrc@yahoo.com.br
15510263Satgutier@umich.edu        return blk;
15610263Satgutier@umich.edu    }
15710263Satgutier@umich.edu
15810263Satgutier@umich.edu    /**
15912744Sodanrc@yahoo.com.br     * Find replacement victim based on address. The list of evicted blocks
16012744Sodanrc@yahoo.com.br     * only contains the victim.
16112600Sodanrc@yahoo.com.br     *
16212600Sodanrc@yahoo.com.br     * @param addr Address to find a victim for.
16312746Sodanrc@yahoo.com.br     * @param is_secure True if the target memory space is secure.
16413941Sodanrc@yahoo.com.br     * @param size Size, in bits, of new block to allocate.
16512744Sodanrc@yahoo.com.br     * @param evict_blks Cache blocks to be evicted.
16612600Sodanrc@yahoo.com.br     * @return Cache block to be replaced.
16710263Satgutier@umich.edu     */
16812746Sodanrc@yahoo.com.br    CacheBlk* findVictim(Addr addr, const bool is_secure,
16913941Sodanrc@yahoo.com.br                         const std::size_t size,
17012746Sodanrc@yahoo.com.br                         std::vector<CacheBlk*>& evict_blks) const override
17110263Satgutier@umich.edu    {
17213219Sodanrc@yahoo.com.br        // Get possible entries to be victimized
17313219Sodanrc@yahoo.com.br        const std::vector<ReplaceableEntry*> entries =
17413219Sodanrc@yahoo.com.br            indexingPolicy->getPossibleEntries(addr);
17512684Sodanrc@yahoo.com.br
17612600Sodanrc@yahoo.com.br        // Choose replacement victim from replacement candidates
17712684Sodanrc@yahoo.com.br        CacheBlk* victim = static_cast<CacheBlk*>(replacementPolicy->getVictim(
17813219Sodanrc@yahoo.com.br                                entries));
17912684Sodanrc@yahoo.com.br
18012744Sodanrc@yahoo.com.br        // There is only one eviction for this replacement
18112744Sodanrc@yahoo.com.br        evict_blks.push_back(victim);
18212744Sodanrc@yahoo.com.br
18312684Sodanrc@yahoo.com.br        return victim;
18412600Sodanrc@yahoo.com.br    }
18510263Satgutier@umich.edu
18612600Sodanrc@yahoo.com.br    /**
18712636Sodanrc@yahoo.com.br     * Insert the new block into the cache and update replacement data.
18812636Sodanrc@yahoo.com.br     *
18913752Sodanrc@yahoo.com.br     * @param pkt Packet holding the address to update
19010263Satgutier@umich.edu     * @param blk The block to update.
19110263Satgutier@umich.edu     */
19213752Sodanrc@yahoo.com.br    void insertBlock(const PacketPtr pkt, CacheBlk *blk) override
19312636Sodanrc@yahoo.com.br    {
19412636Sodanrc@yahoo.com.br        // Insert block
19513752Sodanrc@yahoo.com.br        BaseTags::insertBlock(pkt, blk);
19610274Smitch.hayenga@arm.com
19712745Sodanrc@yahoo.com.br        // Increment tag counter
19812745Sodanrc@yahoo.com.br        tagsInUse++;
19912745Sodanrc@yahoo.com.br
20012636Sodanrc@yahoo.com.br        // Update replacement policy
20112684Sodanrc@yahoo.com.br        replacementPolicy->reset(blk->replacementData);
20212636Sodanrc@yahoo.com.br    }
20310263Satgutier@umich.edu
20410263Satgutier@umich.edu    /**
20510941Sdavid.guillen@arm.com     * Limit the allocation for the cache ways.
20610941Sdavid.guillen@arm.com     * @param ways The maximum number of ways available for replacement.
20710941Sdavid.guillen@arm.com     */
20811169Sandreas.hansson@arm.com    virtual void setWayAllocationMax(int ways) override
20910941Sdavid.guillen@arm.com    {
21010941Sdavid.guillen@arm.com        fatal_if(ways < 1, "Allocation limit must be greater than zero");
21110941Sdavid.guillen@arm.com        allocAssoc = ways;
21210941Sdavid.guillen@arm.com    }
21310941Sdavid.guillen@arm.com
21410941Sdavid.guillen@arm.com    /**
21510941Sdavid.guillen@arm.com     * Get the way allocation mask limit.
21610941Sdavid.guillen@arm.com     * @return The maximum number of ways available for replacement.
21710941Sdavid.guillen@arm.com     */
21811169Sandreas.hansson@arm.com    virtual int getWayAllocationMax() const override
21910941Sdavid.guillen@arm.com    {
22010941Sdavid.guillen@arm.com        return allocAssoc;
22110941Sdavid.guillen@arm.com    }
22210941Sdavid.guillen@arm.com
22310941Sdavid.guillen@arm.com    /**
22413219Sodanrc@yahoo.com.br     * Regenerate the block address from the tag and indexing location.
22512574Sodanrc@yahoo.com.br     *
22612574Sodanrc@yahoo.com.br     * @param block The block.
22712574Sodanrc@yahoo.com.br     * @return the block address.
22810263Satgutier@umich.edu     */
22912574Sodanrc@yahoo.com.br    Addr regenerateBlkAddr(const CacheBlk* blk) const override
23010263Satgutier@umich.edu    {
23113219Sodanrc@yahoo.com.br        return indexingPolicy->regenerateAddr(blk->tag, blk);
23210263Satgutier@umich.edu    }
23310263Satgutier@umich.edu
23412728Snikos.nikoleris@arm.com    void forEachBlk(std::function<void(CacheBlk &)> visitor) override {
23512728Snikos.nikoleris@arm.com        for (CacheBlk& blk : blks) {
23612728Snikos.nikoleris@arm.com            visitor(blk);
23712728Snikos.nikoleris@arm.com        }
23812728Snikos.nikoleris@arm.com    }
23910263Satgutier@umich.edu
24012728Snikos.nikoleris@arm.com    bool anyBlk(std::function<bool(CacheBlk &)> visitor) override {
24112679Sodanrc@yahoo.com.br        for (CacheBlk& blk : blks) {
24212728Snikos.nikoleris@arm.com            if (visitor(blk)) {
24312728Snikos.nikoleris@arm.com                return true;
24412728Snikos.nikoleris@arm.com            }
24510263Satgutier@umich.edu        }
24612728Snikos.nikoleris@arm.com        return false;
24710263Satgutier@umich.edu    }
24810263Satgutier@umich.edu};
24910263Satgutier@umich.edu
25012492Sodanrc@yahoo.com.br#endif //__MEM_CACHE_TAGS_BASE_SET_ASSOC_HH__
251