base_set_assoc.cc revision 13216
110263Satgutier@umich.edu/*
210941Sdavid.guillen@arm.com * Copyright (c) 2012-2014 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 * Definitions of a base set associative tag store.
4610263Satgutier@umich.edu */
4710263Satgutier@umich.edu
4811486Snikos.nikoleris@arm.com#include "mem/cache/tags/base_set_assoc.hh"
4911486Snikos.nikoleris@arm.com
5010263Satgutier@umich.edu#include <string>
5110263Satgutier@umich.edu
5210263Satgutier@umich.edu#include "base/intmath.hh"
5310263Satgutier@umich.edu
5410263Satgutier@umich.eduBaseSetAssoc::BaseSetAssoc(const Params *p)
5510941Sdavid.guillen@arm.com    :BaseTags(p), assoc(p->assoc), allocAssoc(p->assoc),
5612548Sodanrc@yahoo.com.br     blks(p->size / p->block_size),
5710263Satgutier@umich.edu     numSets(p->size / (p->block_size * p->assoc)),
5812548Sodanrc@yahoo.com.br     sequentialAccess(p->sequential_access),
5912600Sodanrc@yahoo.com.br     sets(p->size / (p->block_size * p->assoc)),
6012600Sodanrc@yahoo.com.br     replacementPolicy(p->replacement_policy)
6110263Satgutier@umich.edu{
6210263Satgutier@umich.edu    // Check parameters
6310263Satgutier@umich.edu    if (blkSize < 4 || !isPowerOf2(blkSize)) {
6410263Satgutier@umich.edu        fatal("Block size must be at least 4 and a power of 2");
6510263Satgutier@umich.edu    }
6612493Sodanrc@yahoo.com.br    if (!isPowerOf2(numSets)) {
6710263Satgutier@umich.edu        fatal("# of sets must be non-zero and a power of 2");
6810263Satgutier@umich.edu    }
6910263Satgutier@umich.edu    if (assoc <= 0) {
7010263Satgutier@umich.edu        fatal("associativity must be greater than zero");
7110263Satgutier@umich.edu    }
7210263Satgutier@umich.edu
7310263Satgutier@umich.edu    setShift = floorLog2(blkSize);
7410263Satgutier@umich.edu    setMask = numSets - 1;
7510263Satgutier@umich.edu    tagShift = setShift + floorLog2(numSets);
7613216Sodanrc@yahoo.com.br}
7710263Satgutier@umich.edu
7813216Sodanrc@yahoo.com.brvoid
7913216Sodanrc@yahoo.com.brBaseSetAssoc::init(BaseCache* cache)
8013216Sodanrc@yahoo.com.br{
8113216Sodanrc@yahoo.com.br    // Set parent cache
8213216Sodanrc@yahoo.com.br    setCache(cache);
8313216Sodanrc@yahoo.com.br
8413216Sodanrc@yahoo.com.br    // Initialize blocks
8510263Satgutier@umich.edu    unsigned blkIndex = 0;       // index into blks array
8610263Satgutier@umich.edu    for (unsigned i = 0; i < numSets; ++i) {
8710263Satgutier@umich.edu        sets[i].assoc = assoc;
8810263Satgutier@umich.edu
8912545Sodanrc@yahoo.com.br        sets[i].blks.resize(assoc);
9010263Satgutier@umich.edu
9110263Satgutier@umich.edu        // link in the data blocks
9210263Satgutier@umich.edu        for (unsigned j = 0; j < assoc; ++j) {
9312549Sodanrc@yahoo.com.br            // Select block within the set to be linked
9412549Sodanrc@yahoo.com.br            BlkType*& blk = sets[i].blks[j];
9512549Sodanrc@yahoo.com.br
9612549Sodanrc@yahoo.com.br            // Locate next cache block
9712549Sodanrc@yahoo.com.br            blk = &blks[blkIndex];
9812549Sodanrc@yahoo.com.br
9912549Sodanrc@yahoo.com.br            // Associate a data chunk to the block
10010263Satgutier@umich.edu            blk->data = &dataBlks[blkSize*blkIndex];
10110263Satgutier@umich.edu
10212684Sodanrc@yahoo.com.br            // Associate a replacement data entry to the block
10312684Sodanrc@yahoo.com.br            blk->replacementData = replacementPolicy->instantiateEntry();
10412684Sodanrc@yahoo.com.br
10512549Sodanrc@yahoo.com.br            // Setting the tag to j is just to prevent long chains in the
10612549Sodanrc@yahoo.com.br            // hash table; won't matter because the block is invalid
10712549Sodanrc@yahoo.com.br            blk->tag = j;
10810263Satgutier@umich.edu
10912549Sodanrc@yahoo.com.br            // Set its set and way
11010263Satgutier@umich.edu            blk->set = i;
11110941Sdavid.guillen@arm.com            blk->way = j;
11212549Sodanrc@yahoo.com.br
11312549Sodanrc@yahoo.com.br            // Update block index
11412549Sodanrc@yahoo.com.br            ++blkIndex;
11510263Satgutier@umich.edu        }
11610263Satgutier@umich.edu    }
11710263Satgutier@umich.edu}
11810263Satgutier@umich.edu
11912684Sodanrc@yahoo.com.brvoid
12012684Sodanrc@yahoo.com.brBaseSetAssoc::invalidate(CacheBlk *blk)
12112684Sodanrc@yahoo.com.br{
12212684Sodanrc@yahoo.com.br    BaseTags::invalidate(blk);
12312684Sodanrc@yahoo.com.br
12412745Sodanrc@yahoo.com.br    // Decrease the number of tags in use
12512745Sodanrc@yahoo.com.br    tagsInUse--;
12612745Sodanrc@yahoo.com.br
12712684Sodanrc@yahoo.com.br    // Invalidate replacement data
12812684Sodanrc@yahoo.com.br    replacementPolicy->invalidate(blk->replacementData);
12912684Sodanrc@yahoo.com.br}
13012684Sodanrc@yahoo.com.br
13110815Sdavid.guillen@arm.comCacheBlk*
13210263Satgutier@umich.eduBaseSetAssoc::findBlock(Addr addr, bool is_secure) const
13310263Satgutier@umich.edu{
13410263Satgutier@umich.edu    Addr tag = extractTag(addr);
13510263Satgutier@umich.edu    unsigned set = extractSet(addr);
13610263Satgutier@umich.edu    BlkType *blk = sets[set].findBlk(tag, is_secure);
13710263Satgutier@umich.edu    return blk;
13810263Satgutier@umich.edu}
13910263Satgutier@umich.edu
14012743Sodanrc@yahoo.com.brReplaceableEntry*
14110941Sdavid.guillen@arm.comBaseSetAssoc::findBlockBySetAndWay(int set, int way) const
14210941Sdavid.guillen@arm.com{
14310941Sdavid.guillen@arm.com    return sets[set].blks[way];
14410941Sdavid.guillen@arm.com}
14510941Sdavid.guillen@arm.com
14612600Sodanrc@yahoo.com.brBaseSetAssoc *
14712600Sodanrc@yahoo.com.brBaseSetAssocParams::create()
14812600Sodanrc@yahoo.com.br{
14912600Sodanrc@yahoo.com.br    return new BaseSetAssoc(this);
15012600Sodanrc@yahoo.com.br}
151