CacheMemory.cc revision 11168
16782SN/A/*
28683SN/A * Copyright (c) 1999-2012 Mark D. Hill and David A. Wood
310973Sdavid.hashe@amd.com * Copyright (c) 2013 Advanced Micro Devices, Inc.
46782SN/A * All rights reserved.
56782SN/A *
66782SN/A * Redistribution and use in source and binary forms, with or without
76782SN/A * modification, are permitted provided that the following conditions are
86782SN/A * met: redistributions of source code must retain the above copyright
96782SN/A * notice, this list of conditions and the following disclaimer;
106782SN/A * redistributions in binary form must reproduce the above copyright
116782SN/A * notice, this list of conditions and the following disclaimer in the
126782SN/A * documentation and/or other materials provided with the distribution;
136782SN/A * neither the name of the copyright holders nor the names of its
146782SN/A * contributors may be used to endorse or promote products derived from
156782SN/A * this software without specific prior written permission.
166782SN/A *
176782SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
186782SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
196782SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
206782SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
216782SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
226782SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
236782SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246782SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256782SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
266782SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
276782SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286782SN/A */
296782SN/A
307056SN/A#include "base/intmath.hh"
318232SN/A#include "debug/RubyCache.hh"
328937SN/A#include "debug/RubyCacheTrace.hh"
339105SN/A#include "debug/RubyResourceStalls.hh"
349104SN/A#include "debug/RubyStats.hh"
358683SN/A#include "mem/protocol/AccessPermission.hh"
3610301Snilay@cs.wisc.edu#include "mem/ruby/structures/CacheMemory.hh"
3711108Sdavid.hashe@amd.com#include "mem/ruby/system/RubySystem.hh"
386782SN/A
397055SN/Ausing namespace std;
407055SN/A
417039SN/Aostream&
427039SN/Aoperator<<(ostream& out, const CacheMemory& obj)
436782SN/A{
447039SN/A    obj.print(out);
457039SN/A    out << flush;
467039SN/A    return out;
476782SN/A}
486782SN/A
496876SN/ACacheMemory *
506876SN/ARubyCacheParams::create()
516782SN/A{
526876SN/A    return new CacheMemory(this);
536782SN/A}
546782SN/A
556876SN/ACacheMemory::CacheMemory(const Params *p)
569105SN/A    : SimObject(p),
5710919Sbrandon.potter@amd.com    dataArray(p->dataArrayBanks, p->dataAccessLatency,
5810919Sbrandon.potter@amd.com              p->start_index_bit, p->ruby_system),
5910919Sbrandon.potter@amd.com    tagArray(p->tagArrayBanks, p->tagAccessLatency,
6010919Sbrandon.potter@amd.com             p->start_index_bit, p->ruby_system)
616782SN/A{
626882SN/A    m_cache_size = p->size;
636876SN/A    m_cache_assoc = p->assoc;
6410970Sdavid.hashe@amd.com    m_replacementPolicy_ptr = p->replacement_policy;
6510980Sdavid.hashe@amd.com    m_replacementPolicy_ptr->setCache(this);
667564SN/A    m_start_index_bit = p->start_index_bit;
678653SN/A    m_is_instruction_only_cache = p->is_icache;
689105SN/A    m_resource_stalls = p->resourceStalls;
696876SN/A}
706876SN/A
717039SN/Avoid
727039SN/ACacheMemory::init()
736876SN/A{
747039SN/A    m_cache_num_sets = (m_cache_size / m_cache_assoc) /
757039SN/A        RubySystem::getBlockSizeBytes();
766882SN/A    assert(m_cache_num_sets > 1);
777056SN/A    m_cache_num_set_bits = floorLog2(m_cache_num_sets);
786882SN/A    assert(m_cache_num_set_bits > 0);
797039SN/A
807454SN/A    m_cache.resize(m_cache_num_sets);
817039SN/A    for (int i = 0; i < m_cache_num_sets; i++) {
827454SN/A        m_cache[i].resize(m_cache_assoc);
837039SN/A        for (int j = 0; j < m_cache_assoc; j++) {
847039SN/A            m_cache[i][j] = NULL;
857039SN/A        }
866782SN/A    }
876782SN/A}
886782SN/A
896782SN/ACacheMemory::~CacheMemory()
906782SN/A{
917039SN/A    if (m_replacementPolicy_ptr != NULL)
927039SN/A        delete m_replacementPolicy_ptr;
937039SN/A    for (int i = 0; i < m_cache_num_sets; i++) {
947039SN/A        for (int j = 0; j < m_cache_assoc; j++) {
957039SN/A            delete m_cache[i][j];
967039SN/A        }
976783SN/A    }
986782SN/A}
996782SN/A
1006782SN/A// convert a Address to its location in the cache
10111061Snilay@cs.wisc.eduint64_t
10211025Snilay@cs.wisc.eduCacheMemory::addressToCacheSet(Addr address) const
1036782SN/A{
10411025Snilay@cs.wisc.edu    assert(address == makeLineAddress(address));
10511025Snilay@cs.wisc.edu    return bitSelect(address, m_start_index_bit,
10611025Snilay@cs.wisc.edu                     m_start_index_bit + m_cache_num_set_bits - 1);
1076782SN/A}
1086782SN/A
1096782SN/A// Given a cache index: returns the index of the tag in a set.
1106782SN/A// returns -1 if the tag is not found.
1117039SN/Aint
11211061Snilay@cs.wisc.eduCacheMemory::findTagInSet(int64_t cacheSet, Addr tag) const
1136782SN/A{
11411025Snilay@cs.wisc.edu    assert(tag == makeLineAddress(tag));
1157039SN/A    // search the set for the tags
11611168Sandreas.hansson@arm.com    auto it = m_tag_index.find(tag);
1177039SN/A    if (it != m_tag_index.end())
1187039SN/A        if (m_cache[cacheSet][it->second]->m_Permission !=
1197039SN/A            AccessPermission_NotPresent)
1207039SN/A            return it->second;
1217039SN/A    return -1; // Not found
1226782SN/A}
1236782SN/A
1246782SN/A// Given a cache index: returns the index of the tag in a set.
1256782SN/A// returns -1 if the tag is not found.
1267039SN/Aint
12711061Snilay@cs.wisc.eduCacheMemory::findTagInSetIgnorePermissions(int64_t cacheSet,
12811025Snilay@cs.wisc.edu                                           Addr tag) const
1296782SN/A{
13011025Snilay@cs.wisc.edu    assert(tag == makeLineAddress(tag));
1317039SN/A    // search the set for the tags
13211168Sandreas.hansson@arm.com    auto it = m_tag_index.find(tag);
1337039SN/A    if (it != m_tag_index.end())
1347039SN/A        return it->second;
1357039SN/A    return -1; // Not found
1366782SN/A}
1376782SN/A
13810973Sdavid.hashe@amd.com// Given an unique cache block identifier (idx): return the valid address
13910973Sdavid.hashe@amd.com// stored by the cache block.  If the block is invalid/notpresent, the
14010973Sdavid.hashe@amd.com// function returns the 0 address
14111025Snilay@cs.wisc.eduAddr
14210973Sdavid.hashe@amd.comCacheMemory::getAddressAtIdx(int idx) const
14310973Sdavid.hashe@amd.com{
14411025Snilay@cs.wisc.edu    Addr tmp(0);
14510973Sdavid.hashe@amd.com
14610973Sdavid.hashe@amd.com    int set = idx / m_cache_assoc;
14710973Sdavid.hashe@amd.com    assert(set < m_cache_num_sets);
14810973Sdavid.hashe@amd.com
14910973Sdavid.hashe@amd.com    int way = idx - set * m_cache_assoc;
15010973Sdavid.hashe@amd.com    assert (way < m_cache_assoc);
15110973Sdavid.hashe@amd.com
15210973Sdavid.hashe@amd.com    AbstractCacheEntry* entry = m_cache[set][way];
15310973Sdavid.hashe@amd.com    if (entry == NULL ||
15410973Sdavid.hashe@amd.com        entry->m_Permission == AccessPermission_Invalid ||
15510973Sdavid.hashe@amd.com        entry->m_Permission == AccessPermission_NotPresent) {
15610973Sdavid.hashe@amd.com        return tmp;
15710973Sdavid.hashe@amd.com    }
15810973Sdavid.hashe@amd.com    return entry->m_Address;
15910973Sdavid.hashe@amd.com}
16010973Sdavid.hashe@amd.com
16111049Snilay@cs.wisc.edubool
16211049Snilay@cs.wisc.eduCacheMemory::tryCacheAccess(Addr address, RubyRequestType type,
16311049Snilay@cs.wisc.edu                            DataBlock*& data_ptr)
16411049Snilay@cs.wisc.edu{
16511049Snilay@cs.wisc.edu    assert(address == makeLineAddress(address));
16611118Snilay@cs.wisc.edu    DPRINTF(RubyCache, "address: %#x\n", address);
16711061Snilay@cs.wisc.edu    int64_t cacheSet = addressToCacheSet(address);
16811049Snilay@cs.wisc.edu    int loc = findTagInSet(cacheSet, address);
16911049Snilay@cs.wisc.edu    if (loc != -1) {
17011049Snilay@cs.wisc.edu        // Do we even have a tag match?
17111049Snilay@cs.wisc.edu        AbstractCacheEntry* entry = m_cache[cacheSet][loc];
17211049Snilay@cs.wisc.edu        m_replacementPolicy_ptr->touch(cacheSet, loc, curTick());
17311049Snilay@cs.wisc.edu        data_ptr = &(entry->getDataBlk());
17411049Snilay@cs.wisc.edu
17511049Snilay@cs.wisc.edu        if (entry->m_Permission == AccessPermission_Read_Write) {
17611049Snilay@cs.wisc.edu            return true;
17711049Snilay@cs.wisc.edu        }
17811049Snilay@cs.wisc.edu        if ((entry->m_Permission == AccessPermission_Read_Only) &&
17911049Snilay@cs.wisc.edu            (type == RubyRequestType_LD || type == RubyRequestType_IFETCH)) {
18011049Snilay@cs.wisc.edu            return true;
18111049Snilay@cs.wisc.edu        }
18211049Snilay@cs.wisc.edu        // The line must not be accessible
18311049Snilay@cs.wisc.edu    }
18411049Snilay@cs.wisc.edu    data_ptr = NULL;
18511049Snilay@cs.wisc.edu    return false;
18611049Snilay@cs.wisc.edu}
18711049Snilay@cs.wisc.edu
18811049Snilay@cs.wisc.edubool
18911049Snilay@cs.wisc.eduCacheMemory::testCacheAccess(Addr address, RubyRequestType type,
19011049Snilay@cs.wisc.edu                             DataBlock*& data_ptr)
19111049Snilay@cs.wisc.edu{
19211049Snilay@cs.wisc.edu    assert(address == makeLineAddress(address));
19311118Snilay@cs.wisc.edu    DPRINTF(RubyCache, "address: %#x\n", address);
19411061Snilay@cs.wisc.edu    int64_t cacheSet = addressToCacheSet(address);
19511049Snilay@cs.wisc.edu    int loc = findTagInSet(cacheSet, address);
19611049Snilay@cs.wisc.edu
19711049Snilay@cs.wisc.edu    if (loc != -1) {
19811049Snilay@cs.wisc.edu        // Do we even have a tag match?
19911049Snilay@cs.wisc.edu        AbstractCacheEntry* entry = m_cache[cacheSet][loc];
20011049Snilay@cs.wisc.edu        m_replacementPolicy_ptr->touch(cacheSet, loc, curTick());
20111049Snilay@cs.wisc.edu        data_ptr = &(entry->getDataBlk());
20211049Snilay@cs.wisc.edu
20311049Snilay@cs.wisc.edu        return m_cache[cacheSet][loc]->m_Permission !=
20411049Snilay@cs.wisc.edu            AccessPermission_NotPresent;
20511049Snilay@cs.wisc.edu    }
20611049Snilay@cs.wisc.edu
20711049Snilay@cs.wisc.edu    data_ptr = NULL;
20811049Snilay@cs.wisc.edu    return false;
20911049Snilay@cs.wisc.edu}
21011049Snilay@cs.wisc.edu
2116782SN/A// tests to see if an address is present in the cache
2127039SN/Abool
21311025Snilay@cs.wisc.eduCacheMemory::isTagPresent(Addr address) const
2146782SN/A{
21511025Snilay@cs.wisc.edu    assert(address == makeLineAddress(address));
21611061Snilay@cs.wisc.edu    int64_t cacheSet = addressToCacheSet(address);
2177039SN/A    int loc = findTagInSet(cacheSet, address);
2186782SN/A
2197039SN/A    if (loc == -1) {
2207039SN/A        // We didn't find the tag
22111118Snilay@cs.wisc.edu        DPRINTF(RubyCache, "No tag match for address: %#x\n", address);
2227039SN/A        return false;
2237039SN/A    }
22411118Snilay@cs.wisc.edu    DPRINTF(RubyCache, "address: %#x found\n", address);
2257039SN/A    return true;
2266782SN/A}
2276782SN/A
2286782SN/A// Returns true if there is:
2296782SN/A//   a) a tag match on this address or there is
2306782SN/A//   b) an unused line in the same cache "way"
2317039SN/Abool
23211025Snilay@cs.wisc.eduCacheMemory::cacheAvail(Addr address) const
2336782SN/A{
23411025Snilay@cs.wisc.edu    assert(address == makeLineAddress(address));
2356782SN/A
23611061Snilay@cs.wisc.edu    int64_t cacheSet = addressToCacheSet(address);
2376782SN/A
2387039SN/A    for (int i = 0; i < m_cache_assoc; i++) {
2397039SN/A        AbstractCacheEntry* entry = m_cache[cacheSet][i];
2407039SN/A        if (entry != NULL) {
2417039SN/A            if (entry->m_Address == address ||
2427039SN/A                entry->m_Permission == AccessPermission_NotPresent) {
2437039SN/A                // Already in the cache or we found an empty entry
2447039SN/A                return true;
2457039SN/A            }
2467039SN/A        } else {
2477039SN/A            return true;
2487039SN/A        }
2496782SN/A    }
2507039SN/A    return false;
2516782SN/A}
2526782SN/A
2537839SN/AAbstractCacheEntry*
25411086Snilay@cs.wisc.eduCacheMemory::allocate(Addr address, AbstractCacheEntry *entry, bool touch)
2556782SN/A{
25611025Snilay@cs.wisc.edu    assert(address == makeLineAddress(address));
2577039SN/A    assert(!isTagPresent(address));
2587039SN/A    assert(cacheAvail(address));
25911118Snilay@cs.wisc.edu    DPRINTF(RubyCache, "address: %#x\n", address);
2606782SN/A
2617039SN/A    // Find the first open slot
26211061Snilay@cs.wisc.edu    int64_t cacheSet = addressToCacheSet(address);
2637454SN/A    std::vector<AbstractCacheEntry*> &set = m_cache[cacheSet];
2647039SN/A    for (int i = 0; i < m_cache_assoc; i++) {
2657039SN/A        if (!set[i] || set[i]->m_Permission == AccessPermission_NotPresent) {
26611145Sjthestness@gmail.com            if (set[i] && (set[i] != entry)) {
26711145Sjthestness@gmail.com                warn_once("This protocol contains a cache entry handling bug: "
26811145Sjthestness@gmail.com                    "Entries in the cache should never be NotPresent! If\n"
26911145Sjthestness@gmail.com                    "this entry (%#x) is not tracked elsewhere, it will memory "
27011145Sjthestness@gmail.com                    "leak here. Fix your protocol to eliminate these!",
27111145Sjthestness@gmail.com                    address);
27211145Sjthestness@gmail.com            }
2737039SN/A            set[i] = entry;  // Init entry
2747039SN/A            set[i]->m_Address = address;
2757039SN/A            set[i]->m_Permission = AccessPermission_Invalid;
2767039SN/A            DPRINTF(RubyCache, "Allocate clearing lock for addr: %x\n",
2777039SN/A                    address);
2787839SN/A            set[i]->m_locked = -1;
2797039SN/A            m_tag_index[address] = i;
28011086Snilay@cs.wisc.edu            entry->setSetIndex(cacheSet);
28111086Snilay@cs.wisc.edu            entry->setWayIndex(i);
2826782SN/A
28310974Sdavid.hashe@amd.com            if (touch) {
28410974Sdavid.hashe@amd.com                m_replacementPolicy_ptr->touch(cacheSet, i, curTick());
28510974Sdavid.hashe@amd.com            }
2866782SN/A
2877839SN/A            return entry;
2887039SN/A        }
2896782SN/A    }
2907805SN/A    panic("Allocate didn't find an available entry");
2916782SN/A}
2926782SN/A
2937039SN/Avoid
29411025Snilay@cs.wisc.eduCacheMemory::deallocate(Addr address)
2956782SN/A{
29611025Snilay@cs.wisc.edu    assert(address == makeLineAddress(address));
2977039SN/A    assert(isTagPresent(address));
29811118Snilay@cs.wisc.edu    DPRINTF(RubyCache, "address: %#x\n", address);
29911061Snilay@cs.wisc.edu    int64_t cacheSet = addressToCacheSet(address);
3007039SN/A    int loc = findTagInSet(cacheSet, address);
3017039SN/A    if (loc != -1) {
3027039SN/A        delete m_cache[cacheSet][loc];
3037039SN/A        m_cache[cacheSet][loc] = NULL;
3047039SN/A        m_tag_index.erase(address);
3057039SN/A    }
3066782SN/A}
3076782SN/A
3086782SN/A// Returns with the physical address of the conflicting cache line
30911025Snilay@cs.wisc.eduAddr
31011025Snilay@cs.wisc.eduCacheMemory::cacheProbe(Addr address) const
3116782SN/A{
31211025Snilay@cs.wisc.edu    assert(address == makeLineAddress(address));
3137039SN/A    assert(!cacheAvail(address));
3146782SN/A
31511061Snilay@cs.wisc.edu    int64_t cacheSet = addressToCacheSet(address);
3167039SN/A    return m_cache[cacheSet][m_replacementPolicy_ptr->getVictim(cacheSet)]->
3177039SN/A        m_Address;
3186782SN/A}
3196782SN/A
3206782SN/A// looks an address up in the cache
3217839SN/AAbstractCacheEntry*
32211025Snilay@cs.wisc.eduCacheMemory::lookup(Addr address)
3236782SN/A{
32411025Snilay@cs.wisc.edu    assert(address == makeLineAddress(address));
32511061Snilay@cs.wisc.edu    int64_t cacheSet = addressToCacheSet(address);
3267039SN/A    int loc = findTagInSet(cacheSet, address);
3277839SN/A    if(loc == -1) return NULL;
3287839SN/A    return m_cache[cacheSet][loc];
3296782SN/A}
3306782SN/A
3316782SN/A// looks an address up in the cache
3327839SN/Aconst AbstractCacheEntry*
33311025Snilay@cs.wisc.eduCacheMemory::lookup(Addr address) const
3346782SN/A{
33511025Snilay@cs.wisc.edu    assert(address == makeLineAddress(address));
33611061Snilay@cs.wisc.edu    int64_t cacheSet = addressToCacheSet(address);
3377039SN/A    int loc = findTagInSet(cacheSet, address);
3387839SN/A    if(loc == -1) return NULL;
3397839SN/A    return m_cache[cacheSet][loc];
3406782SN/A}
3416782SN/A
3426782SN/A// Sets the most recently used bit for a cache block
3437039SN/Avoid
34411025Snilay@cs.wisc.eduCacheMemory::setMRU(Addr address)
3456782SN/A{
34611061Snilay@cs.wisc.edu    int64_t cacheSet = addressToCacheSet(address);
3478828SN/A    int loc = findTagInSet(cacheSet, address);
3486782SN/A
3498828SN/A    if(loc != -1)
3509171SN/A        m_replacementPolicy_ptr->touch(cacheSet, loc, curTick());
3516782SN/A}
3526782SN/A
3537039SN/Avoid
35411087Snilay@cs.wisc.eduCacheMemory::setMRU(const AbstractCacheEntry *e)
35511087Snilay@cs.wisc.edu{
35611087Snilay@cs.wisc.edu    uint32_t cacheSet = e->getSetIndex();
35711087Snilay@cs.wisc.edu    uint32_t loc = e->getWayIndex();
35811087Snilay@cs.wisc.edu    m_replacementPolicy_ptr->touch(cacheSet, loc, curTick());
35911087Snilay@cs.wisc.edu}
36011087Snilay@cs.wisc.edu
36111087Snilay@cs.wisc.eduvoid
3628683SN/ACacheMemory::recordCacheContents(int cntrl, CacheRecorder* tr) const
3636782SN/A{
36411061Snilay@cs.wisc.edu    uint64_t warmedUpBlocks = 0;
36511061Snilay@cs.wisc.edu    uint64_t totalBlocks M5_VAR_USED = (uint64_t)m_cache_num_sets *
36611061Snilay@cs.wisc.edu                                       (uint64_t)m_cache_assoc;
3678683SN/A
3687039SN/A    for (int i = 0; i < m_cache_num_sets; i++) {
3697039SN/A        for (int j = 0; j < m_cache_assoc; j++) {
3708683SN/A            if (m_cache[i][j] != NULL) {
3718683SN/A                AccessPermission perm = m_cache[i][j]->m_Permission;
3728683SN/A                RubyRequestType request_type = RubyRequestType_NULL;
3738683SN/A                if (perm == AccessPermission_Read_Only) {
3748683SN/A                    if (m_is_instruction_only_cache) {
3758683SN/A                        request_type = RubyRequestType_IFETCH;
3768683SN/A                    } else {
3778683SN/A                        request_type = RubyRequestType_LD;
3788683SN/A                    }
3798683SN/A                } else if (perm == AccessPermission_Read_Write) {
3808683SN/A                    request_type = RubyRequestType_ST;
3817039SN/A                }
3827039SN/A
3838683SN/A                if (request_type != RubyRequestType_NULL) {
38411025Snilay@cs.wisc.edu                    tr->addRecord(cntrl, m_cache[i][j]->m_Address,
3858683SN/A                                  0, request_type,
3868683SN/A                                  m_replacementPolicy_ptr->getLastAccess(i, j),
3878683SN/A                                  m_cache[i][j]->getDataBlk());
3888683SN/A                    warmedUpBlocks++;
3898683SN/A                }
3907039SN/A            }
3916782SN/A        }
3926782SN/A    }
3938683SN/A
3948937SN/A    DPRINTF(RubyCacheTrace, "%s: %lli blocks of %lli total blocks"
3958683SN/A            "recorded %.2f%% \n", name().c_str(), warmedUpBlocks,
39611061Snilay@cs.wisc.edu            totalBlocks, (float(warmedUpBlocks) / float(totalBlocks)) * 100.0);
3976782SN/A}
3986782SN/A
3997039SN/Avoid
4007039SN/ACacheMemory::print(ostream& out) const
4016782SN/A{
40210080SN/A    out << "Cache dump: " << name() << endl;
4037039SN/A    for (int i = 0; i < m_cache_num_sets; i++) {
4047039SN/A        for (int j = 0; j < m_cache_assoc; j++) {
4057039SN/A            if (m_cache[i][j] != NULL) {
4067039SN/A                out << "  Index: " << i
4077039SN/A                    << " way: " << j
4087039SN/A                    << " entry: " << *m_cache[i][j] << endl;
4097039SN/A            } else {
4107039SN/A                out << "  Index: " << i
4117039SN/A                    << " way: " << j
4127039SN/A                    << " entry: NULL" << endl;
4137039SN/A            }
4147039SN/A        }
4156782SN/A    }
4166782SN/A}
4176782SN/A
4187039SN/Avoid
4197039SN/ACacheMemory::printData(ostream& out) const
4206782SN/A{
4217039SN/A    out << "printData() not supported" << endl;
4226782SN/A}
4236782SN/A
4247039SN/Avoid
42511025Snilay@cs.wisc.eduCacheMemory::setLocked(Addr address, int context)
4267039SN/A{
42711118Snilay@cs.wisc.edu    DPRINTF(RubyCache, "Setting Lock for addr: %#x to %d\n", address, context);
42811025Snilay@cs.wisc.edu    assert(address == makeLineAddress(address));
42911061Snilay@cs.wisc.edu    int64_t cacheSet = addressToCacheSet(address);
4307039SN/A    int loc = findTagInSet(cacheSet, address);
4317039SN/A    assert(loc != -1);
43211059Snilay@cs.wisc.edu    m_cache[cacheSet][loc]->setLocked(context);
4336782SN/A}
4346782SN/A
4357039SN/Avoid
43611025Snilay@cs.wisc.eduCacheMemory::clearLocked(Addr address)
4376782SN/A{
43811118Snilay@cs.wisc.edu    DPRINTF(RubyCache, "Clear Lock for addr: %#x\n", address);
43911025Snilay@cs.wisc.edu    assert(address == makeLineAddress(address));
44011061Snilay@cs.wisc.edu    int64_t cacheSet = addressToCacheSet(address);
4417039SN/A    int loc = findTagInSet(cacheSet, address);
4427039SN/A    assert(loc != -1);
44311059Snilay@cs.wisc.edu    m_cache[cacheSet][loc]->clearLocked();
4446782SN/A}
4456782SN/A
4466782SN/Abool
44711025Snilay@cs.wisc.eduCacheMemory::isLocked(Addr address, int context)
4486782SN/A{
44911025Snilay@cs.wisc.edu    assert(address == makeLineAddress(address));
45011061Snilay@cs.wisc.edu    int64_t cacheSet = addressToCacheSet(address);
4517039SN/A    int loc = findTagInSet(cacheSet, address);
4527039SN/A    assert(loc != -1);
45311118Snilay@cs.wisc.edu    DPRINTF(RubyCache, "Testing Lock for addr: %#llx cur %d con %d\n",
4547839SN/A            address, m_cache[cacheSet][loc]->m_locked, context);
45511059Snilay@cs.wisc.edu    return m_cache[cacheSet][loc]->isLocked(context);
4566782SN/A}
4576782SN/A
4589104SN/Avoid
4599692SN/ACacheMemory::regStats()
4609692SN/A{
4619692SN/A    m_demand_hits
4629692SN/A        .name(name() + ".demand_hits")
4639692SN/A        .desc("Number of cache demand hits")
4649692SN/A        ;
4659692SN/A
4669692SN/A    m_demand_misses
4679692SN/A        .name(name() + ".demand_misses")
4689692SN/A        .desc("Number of cache demand misses")
4699692SN/A        ;
4709692SN/A
4719692SN/A    m_demand_accesses
4729692SN/A        .name(name() + ".demand_accesses")
4739692SN/A        .desc("Number of cache demand accesses")
4749692SN/A        ;
4759692SN/A
4769692SN/A    m_demand_accesses = m_demand_hits + m_demand_misses;
4779692SN/A
4789692SN/A    m_sw_prefetches
4799692SN/A        .name(name() + ".total_sw_prefetches")
4809692SN/A        .desc("Number of software prefetches")
4819692SN/A        .flags(Stats::nozero)
4829692SN/A        ;
4839692SN/A
4849692SN/A    m_hw_prefetches
4859692SN/A        .name(name() + ".total_hw_prefetches")
4869692SN/A        .desc("Number of hardware prefetches")
4879692SN/A        .flags(Stats::nozero)
4889692SN/A        ;
4899692SN/A
4909692SN/A    m_prefetches
4919692SN/A        .name(name() + ".total_prefetches")
4929692SN/A        .desc("Number of prefetches")
4939692SN/A        .flags(Stats::nozero)
4949692SN/A        ;
4959692SN/A
4969692SN/A    m_prefetches = m_sw_prefetches + m_hw_prefetches;
4979692SN/A
4989692SN/A    m_accessModeType
4999692SN/A        .init(RubyRequestType_NUM)
5009692SN/A        .name(name() + ".access_mode")
5019692SN/A        .flags(Stats::pdf | Stats::total)
5029692SN/A        ;
5039692SN/A    for (int i = 0; i < RubyAccessMode_NUM; i++) {
5049692SN/A        m_accessModeType
5059692SN/A            .subname(i, RubyAccessMode_to_string(RubyAccessMode(i)))
5069692SN/A            .flags(Stats::nozero)
5079692SN/A            ;
5089692SN/A    }
5099692SN/A
5109692SN/A    numDataArrayReads
5119692SN/A        .name(name() + ".num_data_array_reads")
5129692SN/A        .desc("number of data array reads")
5139692SN/A        .flags(Stats::nozero)
5149692SN/A        ;
5159692SN/A
5169692SN/A    numDataArrayWrites
5179692SN/A        .name(name() + ".num_data_array_writes")
5189692SN/A        .desc("number of data array writes")
5199692SN/A        .flags(Stats::nozero)
5209692SN/A        ;
5219692SN/A
5229692SN/A    numTagArrayReads
5239692SN/A        .name(name() + ".num_tag_array_reads")
5249692SN/A        .desc("number of tag array reads")
5259692SN/A        .flags(Stats::nozero)
5269692SN/A        ;
5279692SN/A
5289692SN/A    numTagArrayWrites
5299692SN/A        .name(name() + ".num_tag_array_writes")
5309692SN/A        .desc("number of tag array writes")
5319692SN/A        .flags(Stats::nozero)
5329692SN/A        ;
5339692SN/A
5349692SN/A    numTagArrayStalls
5359692SN/A        .name(name() + ".num_tag_array_stalls")
5369692SN/A        .desc("number of stalls caused by tag array")
5379692SN/A        .flags(Stats::nozero)
5389692SN/A        ;
5399692SN/A
5409692SN/A    numDataArrayStalls
5419692SN/A        .name(name() + ".num_data_array_stalls")
5429692SN/A        .desc("number of stalls caused by data array")
5439692SN/A        .flags(Stats::nozero)
5449692SN/A        ;
5459692SN/A}
5469692SN/A
54710978Sdavid.hashe@amd.com// assumption: SLICC generated files will only call this function
54810978Sdavid.hashe@amd.com// once **all** resources are granted
5499692SN/Avoid
55011025Snilay@cs.wisc.eduCacheMemory::recordRequestType(CacheRequestType requestType, Addr addr)
5519692SN/A{
5529104SN/A    DPRINTF(RubyStats, "Recorded statistic: %s\n",
5539104SN/A            CacheRequestType_to_string(requestType));
5549104SN/A    switch(requestType) {
5559104SN/A    case CacheRequestType_DataArrayRead:
55610978Sdavid.hashe@amd.com        if (m_resource_stalls)
55710978Sdavid.hashe@amd.com            dataArray.reserve(addressToCacheSet(addr));
5589104SN/A        numDataArrayReads++;
5599104SN/A        return;
5609104SN/A    case CacheRequestType_DataArrayWrite:
56110978Sdavid.hashe@amd.com        if (m_resource_stalls)
56210978Sdavid.hashe@amd.com            dataArray.reserve(addressToCacheSet(addr));
5639104SN/A        numDataArrayWrites++;
5649104SN/A        return;
5659104SN/A    case CacheRequestType_TagArrayRead:
56610978Sdavid.hashe@amd.com        if (m_resource_stalls)
56710978Sdavid.hashe@amd.com            tagArray.reserve(addressToCacheSet(addr));
5689104SN/A        numTagArrayReads++;
5699104SN/A        return;
5709104SN/A    case CacheRequestType_TagArrayWrite:
57110978Sdavid.hashe@amd.com        if (m_resource_stalls)
57210978Sdavid.hashe@amd.com            tagArray.reserve(addressToCacheSet(addr));
5739104SN/A        numTagArrayWrites++;
5749104SN/A        return;
5759104SN/A    default:
5769104SN/A        warn("CacheMemory access_type not found: %s",
5779104SN/A             CacheRequestType_to_string(requestType));
5789104SN/A    }
5799104SN/A}
5809104SN/A
5819105SN/Abool
58211025Snilay@cs.wisc.eduCacheMemory::checkResourceAvailable(CacheResourceType res, Addr addr)
5839105SN/A{
5849105SN/A    if (!m_resource_stalls) {
5859105SN/A        return true;
5869105SN/A    }
5879105SN/A
5889105SN/A    if (res == CacheResourceType_TagArray) {
5899105SN/A        if (tagArray.tryAccess(addressToCacheSet(addr))) return true;
5909105SN/A        else {
5919692SN/A            DPRINTF(RubyResourceStalls,
59211118Snilay@cs.wisc.edu                    "Tag array stall on addr %#x in set %d\n",
5939692SN/A                    addr, addressToCacheSet(addr));
5949105SN/A            numTagArrayStalls++;
5959105SN/A            return false;
5969105SN/A        }
5979105SN/A    } else if (res == CacheResourceType_DataArray) {
5989105SN/A        if (dataArray.tryAccess(addressToCacheSet(addr))) return true;
5999105SN/A        else {
6009692SN/A            DPRINTF(RubyResourceStalls,
60111118Snilay@cs.wisc.edu                    "Data array stall on addr %#x in set %d\n",
6029692SN/A                    addr, addressToCacheSet(addr));
6039105SN/A            numDataArrayStalls++;
6049105SN/A            return false;
6059105SN/A        }
6069105SN/A    } else {
6079105SN/A        assert(false);
6089105SN/A        return true;
6099105SN/A    }
6109105SN/A}
61110980Sdavid.hashe@amd.com
61210980Sdavid.hashe@amd.combool
61311061Snilay@cs.wisc.eduCacheMemory::isBlockInvalid(int64_t cache_set, int64_t loc)
61410980Sdavid.hashe@amd.com{
61510980Sdavid.hashe@amd.com  return (m_cache[cache_set][loc]->m_Permission == AccessPermission_Invalid);
61610980Sdavid.hashe@amd.com}
61710980Sdavid.hashe@amd.com
61810980Sdavid.hashe@amd.combool
61911061Snilay@cs.wisc.eduCacheMemory::isBlockNotBusy(int64_t cache_set, int64_t loc)
62010980Sdavid.hashe@amd.com{
62110980Sdavid.hashe@amd.com  return (m_cache[cache_set][loc]->m_Permission != AccessPermission_Busy);
62210980Sdavid.hashe@amd.com}
623