base_set_assoc.cc revision 12745
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);
7610263Satgutier@umich.edu
7710263Satgutier@umich.edu    unsigned blkIndex = 0;       // index into blks array
7810263Satgutier@umich.edu    for (unsigned i = 0; i < numSets; ++i) {
7910263Satgutier@umich.edu        sets[i].assoc = assoc;
8010263Satgutier@umich.edu
8112545Sodanrc@yahoo.com.br        sets[i].blks.resize(assoc);
8210263Satgutier@umich.edu
8310263Satgutier@umich.edu        // link in the data blocks
8410263Satgutier@umich.edu        for (unsigned j = 0; j < assoc; ++j) {
8512549Sodanrc@yahoo.com.br            // Select block within the set to be linked
8612549Sodanrc@yahoo.com.br            BlkType*& blk = sets[i].blks[j];
8712549Sodanrc@yahoo.com.br
8812549Sodanrc@yahoo.com.br            // Locate next cache block
8912549Sodanrc@yahoo.com.br            blk = &blks[blkIndex];
9012549Sodanrc@yahoo.com.br
9112549Sodanrc@yahoo.com.br            // Associate a data chunk to the block
9210263Satgutier@umich.edu            blk->data = &dataBlks[blkSize*blkIndex];
9310263Satgutier@umich.edu
9412684Sodanrc@yahoo.com.br            // Associate a replacement data entry to the block
9512684Sodanrc@yahoo.com.br            blk->replacementData = replacementPolicy->instantiateEntry();
9612684Sodanrc@yahoo.com.br
9712549Sodanrc@yahoo.com.br            // Setting the tag to j is just to prevent long chains in the
9812549Sodanrc@yahoo.com.br            // hash table; won't matter because the block is invalid
9912549Sodanrc@yahoo.com.br            blk->tag = j;
10010263Satgutier@umich.edu
10112549Sodanrc@yahoo.com.br            // Set its set and way
10210263Satgutier@umich.edu            blk->set = i;
10310941Sdavid.guillen@arm.com            blk->way = j;
10412549Sodanrc@yahoo.com.br
10512549Sodanrc@yahoo.com.br            // Update block index
10612549Sodanrc@yahoo.com.br            ++blkIndex;
10710263Satgutier@umich.edu        }
10810263Satgutier@umich.edu    }
10910263Satgutier@umich.edu}
11010263Satgutier@umich.edu
11112684Sodanrc@yahoo.com.brvoid
11212684Sodanrc@yahoo.com.brBaseSetAssoc::invalidate(CacheBlk *blk)
11312684Sodanrc@yahoo.com.br{
11412684Sodanrc@yahoo.com.br    BaseTags::invalidate(blk);
11512684Sodanrc@yahoo.com.br
11612745Sodanrc@yahoo.com.br    // Decrease the number of tags in use
11712745Sodanrc@yahoo.com.br    tagsInUse--;
11812745Sodanrc@yahoo.com.br
11912684Sodanrc@yahoo.com.br    // Invalidate replacement data
12012684Sodanrc@yahoo.com.br    replacementPolicy->invalidate(blk->replacementData);
12112684Sodanrc@yahoo.com.br}
12212684Sodanrc@yahoo.com.br
12310815Sdavid.guillen@arm.comCacheBlk*
12410263Satgutier@umich.eduBaseSetAssoc::findBlock(Addr addr, bool is_secure) const
12510263Satgutier@umich.edu{
12610263Satgutier@umich.edu    Addr tag = extractTag(addr);
12710263Satgutier@umich.edu    unsigned set = extractSet(addr);
12810263Satgutier@umich.edu    BlkType *blk = sets[set].findBlk(tag, is_secure);
12910263Satgutier@umich.edu    return blk;
13010263Satgutier@umich.edu}
13110263Satgutier@umich.edu
13212743Sodanrc@yahoo.com.brReplaceableEntry*
13310941Sdavid.guillen@arm.comBaseSetAssoc::findBlockBySetAndWay(int set, int way) const
13410941Sdavid.guillen@arm.com{
13510941Sdavid.guillen@arm.com    return sets[set].blks[way];
13610941Sdavid.guillen@arm.com}
13710941Sdavid.guillen@arm.com
13812600Sodanrc@yahoo.com.brBaseSetAssoc *
13912600Sodanrc@yahoo.com.brBaseSetAssocParams::create()
14012600Sodanrc@yahoo.com.br{
14112600Sodanrc@yahoo.com.br    return new BaseSetAssoc(this);
14212600Sodanrc@yahoo.com.br}
143