CacheMemory.cc revision 11145:939f3919b108
1/*
2 * Copyright (c) 1999-2012 Mark D. Hill and David A. Wood
3 * Copyright (c) 2013 Advanced Micro Devices, Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "base/intmath.hh"
31#include "debug/RubyCache.hh"
32#include "debug/RubyCacheTrace.hh"
33#include "debug/RubyResourceStalls.hh"
34#include "debug/RubyStats.hh"
35#include "mem/protocol/AccessPermission.hh"
36#include "mem/ruby/structures/CacheMemory.hh"
37#include "mem/ruby/system/RubySystem.hh"
38
39using namespace std;
40
41ostream&
42operator<<(ostream& out, const CacheMemory& obj)
43{
44    obj.print(out);
45    out << flush;
46    return out;
47}
48
49CacheMemory *
50RubyCacheParams::create()
51{
52    return new CacheMemory(this);
53}
54
55CacheMemory::CacheMemory(const Params *p)
56    : SimObject(p),
57    dataArray(p->dataArrayBanks, p->dataAccessLatency,
58              p->start_index_bit, p->ruby_system),
59    tagArray(p->tagArrayBanks, p->tagAccessLatency,
60             p->start_index_bit, p->ruby_system)
61{
62    m_cache_size = p->size;
63    m_cache_assoc = p->assoc;
64    m_replacementPolicy_ptr = p->replacement_policy;
65    m_replacementPolicy_ptr->setCache(this);
66    m_start_index_bit = p->start_index_bit;
67    m_is_instruction_only_cache = p->is_icache;
68    m_resource_stalls = p->resourceStalls;
69}
70
71void
72CacheMemory::init()
73{
74    m_cache_num_sets = (m_cache_size / m_cache_assoc) /
75        RubySystem::getBlockSizeBytes();
76    assert(m_cache_num_sets > 1);
77    m_cache_num_set_bits = floorLog2(m_cache_num_sets);
78    assert(m_cache_num_set_bits > 0);
79
80    m_cache.resize(m_cache_num_sets);
81    for (int i = 0; i < m_cache_num_sets; i++) {
82        m_cache[i].resize(m_cache_assoc);
83        for (int j = 0; j < m_cache_assoc; j++) {
84            m_cache[i][j] = NULL;
85        }
86    }
87}
88
89CacheMemory::~CacheMemory()
90{
91    if (m_replacementPolicy_ptr != NULL)
92        delete m_replacementPolicy_ptr;
93    for (int i = 0; i < m_cache_num_sets; i++) {
94        for (int j = 0; j < m_cache_assoc; j++) {
95            delete m_cache[i][j];
96        }
97    }
98}
99
100// convert a Address to its location in the cache
101int64_t
102CacheMemory::addressToCacheSet(Addr address) const
103{
104    assert(address == makeLineAddress(address));
105    return bitSelect(address, m_start_index_bit,
106                     m_start_index_bit + m_cache_num_set_bits - 1);
107}
108
109// Given a cache index: returns the index of the tag in a set.
110// returns -1 if the tag is not found.
111int
112CacheMemory::findTagInSet(int64_t cacheSet, Addr tag) const
113{
114    assert(tag == makeLineAddress(tag));
115    // search the set for the tags
116    m5::hash_map<Addr, int>::const_iterator it = m_tag_index.find(tag);
117    if (it != m_tag_index.end())
118        if (m_cache[cacheSet][it->second]->m_Permission !=
119            AccessPermission_NotPresent)
120            return it->second;
121    return -1; // Not found
122}
123
124// Given a cache index: returns the index of the tag in a set.
125// returns -1 if the tag is not found.
126int
127CacheMemory::findTagInSetIgnorePermissions(int64_t cacheSet,
128                                           Addr tag) const
129{
130    assert(tag == makeLineAddress(tag));
131    // search the set for the tags
132    m5::hash_map<Addr, int>::const_iterator it = m_tag_index.find(tag);
133    if (it != m_tag_index.end())
134        return it->second;
135    return -1; // Not found
136}
137
138// Given an unique cache block identifier (idx): return the valid address
139// stored by the cache block.  If the block is invalid/notpresent, the
140// function returns the 0 address
141Addr
142CacheMemory::getAddressAtIdx(int idx) const
143{
144    Addr tmp(0);
145
146    int set = idx / m_cache_assoc;
147    assert(set < m_cache_num_sets);
148
149    int way = idx - set * m_cache_assoc;
150    assert (way < m_cache_assoc);
151
152    AbstractCacheEntry* entry = m_cache[set][way];
153    if (entry == NULL ||
154        entry->m_Permission == AccessPermission_Invalid ||
155        entry->m_Permission == AccessPermission_NotPresent) {
156        return tmp;
157    }
158    return entry->m_Address;
159}
160
161bool
162CacheMemory::tryCacheAccess(Addr address, RubyRequestType type,
163                            DataBlock*& data_ptr)
164{
165    assert(address == makeLineAddress(address));
166    DPRINTF(RubyCache, "address: %#x\n", address);
167    int64_t cacheSet = addressToCacheSet(address);
168    int loc = findTagInSet(cacheSet, address);
169    if (loc != -1) {
170        // Do we even have a tag match?
171        AbstractCacheEntry* entry = m_cache[cacheSet][loc];
172        m_replacementPolicy_ptr->touch(cacheSet, loc, curTick());
173        data_ptr = &(entry->getDataBlk());
174
175        if (entry->m_Permission == AccessPermission_Read_Write) {
176            return true;
177        }
178        if ((entry->m_Permission == AccessPermission_Read_Only) &&
179            (type == RubyRequestType_LD || type == RubyRequestType_IFETCH)) {
180            return true;
181        }
182        // The line must not be accessible
183    }
184    data_ptr = NULL;
185    return false;
186}
187
188bool
189CacheMemory::testCacheAccess(Addr address, RubyRequestType type,
190                             DataBlock*& data_ptr)
191{
192    assert(address == makeLineAddress(address));
193    DPRINTF(RubyCache, "address: %#x\n", address);
194    int64_t cacheSet = addressToCacheSet(address);
195    int loc = findTagInSet(cacheSet, address);
196
197    if (loc != -1) {
198        // Do we even have a tag match?
199        AbstractCacheEntry* entry = m_cache[cacheSet][loc];
200        m_replacementPolicy_ptr->touch(cacheSet, loc, curTick());
201        data_ptr = &(entry->getDataBlk());
202
203        return m_cache[cacheSet][loc]->m_Permission !=
204            AccessPermission_NotPresent;
205    }
206
207    data_ptr = NULL;
208    return false;
209}
210
211// tests to see if an address is present in the cache
212bool
213CacheMemory::isTagPresent(Addr address) const
214{
215    assert(address == makeLineAddress(address));
216    int64_t cacheSet = addressToCacheSet(address);
217    int loc = findTagInSet(cacheSet, address);
218
219    if (loc == -1) {
220        // We didn't find the tag
221        DPRINTF(RubyCache, "No tag match for address: %#x\n", address);
222        return false;
223    }
224    DPRINTF(RubyCache, "address: %#x found\n", address);
225    return true;
226}
227
228// Returns true if there is:
229//   a) a tag match on this address or there is
230//   b) an unused line in the same cache "way"
231bool
232CacheMemory::cacheAvail(Addr address) const
233{
234    assert(address == makeLineAddress(address));
235
236    int64_t cacheSet = addressToCacheSet(address);
237
238    for (int i = 0; i < m_cache_assoc; i++) {
239        AbstractCacheEntry* entry = m_cache[cacheSet][i];
240        if (entry != NULL) {
241            if (entry->m_Address == address ||
242                entry->m_Permission == AccessPermission_NotPresent) {
243                // Already in the cache or we found an empty entry
244                return true;
245            }
246        } else {
247            return true;
248        }
249    }
250    return false;
251}
252
253AbstractCacheEntry*
254CacheMemory::allocate(Addr address, AbstractCacheEntry *entry, bool touch)
255{
256    assert(address == makeLineAddress(address));
257    assert(!isTagPresent(address));
258    assert(cacheAvail(address));
259    DPRINTF(RubyCache, "address: %#x\n", address);
260
261    // Find the first open slot
262    int64_t cacheSet = addressToCacheSet(address);
263    std::vector<AbstractCacheEntry*> &set = m_cache[cacheSet];
264    for (int i = 0; i < m_cache_assoc; i++) {
265        if (!set[i] || set[i]->m_Permission == AccessPermission_NotPresent) {
266            if (set[i] && (set[i] != entry)) {
267                warn_once("This protocol contains a cache entry handling bug: "
268                    "Entries in the cache should never be NotPresent! If\n"
269                    "this entry (%#x) is not tracked elsewhere, it will memory "
270                    "leak here. Fix your protocol to eliminate these!",
271                    address);
272            }
273            set[i] = entry;  // Init entry
274            set[i]->m_Address = address;
275            set[i]->m_Permission = AccessPermission_Invalid;
276            DPRINTF(RubyCache, "Allocate clearing lock for addr: %x\n",
277                    address);
278            set[i]->m_locked = -1;
279            m_tag_index[address] = i;
280            entry->setSetIndex(cacheSet);
281            entry->setWayIndex(i);
282
283            if (touch) {
284                m_replacementPolicy_ptr->touch(cacheSet, i, curTick());
285            }
286
287            return entry;
288        }
289    }
290    panic("Allocate didn't find an available entry");
291}
292
293void
294CacheMemory::deallocate(Addr address)
295{
296    assert(address == makeLineAddress(address));
297    assert(isTagPresent(address));
298    DPRINTF(RubyCache, "address: %#x\n", address);
299    int64_t cacheSet = addressToCacheSet(address);
300    int loc = findTagInSet(cacheSet, address);
301    if (loc != -1) {
302        delete m_cache[cacheSet][loc];
303        m_cache[cacheSet][loc] = NULL;
304        m_tag_index.erase(address);
305    }
306}
307
308// Returns with the physical address of the conflicting cache line
309Addr
310CacheMemory::cacheProbe(Addr address) const
311{
312    assert(address == makeLineAddress(address));
313    assert(!cacheAvail(address));
314
315    int64_t cacheSet = addressToCacheSet(address);
316    return m_cache[cacheSet][m_replacementPolicy_ptr->getVictim(cacheSet)]->
317        m_Address;
318}
319
320// looks an address up in the cache
321AbstractCacheEntry*
322CacheMemory::lookup(Addr address)
323{
324    assert(address == makeLineAddress(address));
325    int64_t cacheSet = addressToCacheSet(address);
326    int loc = findTagInSet(cacheSet, address);
327    if(loc == -1) return NULL;
328    return m_cache[cacheSet][loc];
329}
330
331// looks an address up in the cache
332const AbstractCacheEntry*
333CacheMemory::lookup(Addr address) const
334{
335    assert(address == makeLineAddress(address));
336    int64_t cacheSet = addressToCacheSet(address);
337    int loc = findTagInSet(cacheSet, address);
338    if(loc == -1) return NULL;
339    return m_cache[cacheSet][loc];
340}
341
342// Sets the most recently used bit for a cache block
343void
344CacheMemory::setMRU(Addr address)
345{
346    int64_t cacheSet = addressToCacheSet(address);
347    int loc = findTagInSet(cacheSet, address);
348
349    if(loc != -1)
350        m_replacementPolicy_ptr->touch(cacheSet, loc, curTick());
351}
352
353void
354CacheMemory::setMRU(const AbstractCacheEntry *e)
355{
356    uint32_t cacheSet = e->getSetIndex();
357    uint32_t loc = e->getWayIndex();
358    m_replacementPolicy_ptr->touch(cacheSet, loc, curTick());
359}
360
361void
362CacheMemory::recordCacheContents(int cntrl, CacheRecorder* tr) const
363{
364    uint64_t warmedUpBlocks = 0;
365    uint64_t totalBlocks M5_VAR_USED = (uint64_t)m_cache_num_sets *
366                                       (uint64_t)m_cache_assoc;
367
368    for (int i = 0; i < m_cache_num_sets; i++) {
369        for (int j = 0; j < m_cache_assoc; j++) {
370            if (m_cache[i][j] != NULL) {
371                AccessPermission perm = m_cache[i][j]->m_Permission;
372                RubyRequestType request_type = RubyRequestType_NULL;
373                if (perm == AccessPermission_Read_Only) {
374                    if (m_is_instruction_only_cache) {
375                        request_type = RubyRequestType_IFETCH;
376                    } else {
377                        request_type = RubyRequestType_LD;
378                    }
379                } else if (perm == AccessPermission_Read_Write) {
380                    request_type = RubyRequestType_ST;
381                }
382
383                if (request_type != RubyRequestType_NULL) {
384                    tr->addRecord(cntrl, m_cache[i][j]->m_Address,
385                                  0, request_type,
386                                  m_replacementPolicy_ptr->getLastAccess(i, j),
387                                  m_cache[i][j]->getDataBlk());
388                    warmedUpBlocks++;
389                }
390            }
391        }
392    }
393
394    DPRINTF(RubyCacheTrace, "%s: %lli blocks of %lli total blocks"
395            "recorded %.2f%% \n", name().c_str(), warmedUpBlocks,
396            totalBlocks, (float(warmedUpBlocks) / float(totalBlocks)) * 100.0);
397}
398
399void
400CacheMemory::print(ostream& out) const
401{
402    out << "Cache dump: " << name() << endl;
403    for (int i = 0; i < m_cache_num_sets; i++) {
404        for (int j = 0; j < m_cache_assoc; j++) {
405            if (m_cache[i][j] != NULL) {
406                out << "  Index: " << i
407                    << " way: " << j
408                    << " entry: " << *m_cache[i][j] << endl;
409            } else {
410                out << "  Index: " << i
411                    << " way: " << j
412                    << " entry: NULL" << endl;
413            }
414        }
415    }
416}
417
418void
419CacheMemory::printData(ostream& out) const
420{
421    out << "printData() not supported" << endl;
422}
423
424void
425CacheMemory::setLocked(Addr address, int context)
426{
427    DPRINTF(RubyCache, "Setting Lock for addr: %#x to %d\n", address, context);
428    assert(address == makeLineAddress(address));
429    int64_t cacheSet = addressToCacheSet(address);
430    int loc = findTagInSet(cacheSet, address);
431    assert(loc != -1);
432    m_cache[cacheSet][loc]->setLocked(context);
433}
434
435void
436CacheMemory::clearLocked(Addr address)
437{
438    DPRINTF(RubyCache, "Clear Lock for addr: %#x\n", address);
439    assert(address == makeLineAddress(address));
440    int64_t cacheSet = addressToCacheSet(address);
441    int loc = findTagInSet(cacheSet, address);
442    assert(loc != -1);
443    m_cache[cacheSet][loc]->clearLocked();
444}
445
446bool
447CacheMemory::isLocked(Addr address, int context)
448{
449    assert(address == makeLineAddress(address));
450    int64_t cacheSet = addressToCacheSet(address);
451    int loc = findTagInSet(cacheSet, address);
452    assert(loc != -1);
453    DPRINTF(RubyCache, "Testing Lock for addr: %#llx cur %d con %d\n",
454            address, m_cache[cacheSet][loc]->m_locked, context);
455    return m_cache[cacheSet][loc]->isLocked(context);
456}
457
458void
459CacheMemory::regStats()
460{
461    m_demand_hits
462        .name(name() + ".demand_hits")
463        .desc("Number of cache demand hits")
464        ;
465
466    m_demand_misses
467        .name(name() + ".demand_misses")
468        .desc("Number of cache demand misses")
469        ;
470
471    m_demand_accesses
472        .name(name() + ".demand_accesses")
473        .desc("Number of cache demand accesses")
474        ;
475
476    m_demand_accesses = m_demand_hits + m_demand_misses;
477
478    m_sw_prefetches
479        .name(name() + ".total_sw_prefetches")
480        .desc("Number of software prefetches")
481        .flags(Stats::nozero)
482        ;
483
484    m_hw_prefetches
485        .name(name() + ".total_hw_prefetches")
486        .desc("Number of hardware prefetches")
487        .flags(Stats::nozero)
488        ;
489
490    m_prefetches
491        .name(name() + ".total_prefetches")
492        .desc("Number of prefetches")
493        .flags(Stats::nozero)
494        ;
495
496    m_prefetches = m_sw_prefetches + m_hw_prefetches;
497
498    m_accessModeType
499        .init(RubyRequestType_NUM)
500        .name(name() + ".access_mode")
501        .flags(Stats::pdf | Stats::total)
502        ;
503    for (int i = 0; i < RubyAccessMode_NUM; i++) {
504        m_accessModeType
505            .subname(i, RubyAccessMode_to_string(RubyAccessMode(i)))
506            .flags(Stats::nozero)
507            ;
508    }
509
510    numDataArrayReads
511        .name(name() + ".num_data_array_reads")
512        .desc("number of data array reads")
513        .flags(Stats::nozero)
514        ;
515
516    numDataArrayWrites
517        .name(name() + ".num_data_array_writes")
518        .desc("number of data array writes")
519        .flags(Stats::nozero)
520        ;
521
522    numTagArrayReads
523        .name(name() + ".num_tag_array_reads")
524        .desc("number of tag array reads")
525        .flags(Stats::nozero)
526        ;
527
528    numTagArrayWrites
529        .name(name() + ".num_tag_array_writes")
530        .desc("number of tag array writes")
531        .flags(Stats::nozero)
532        ;
533
534    numTagArrayStalls
535        .name(name() + ".num_tag_array_stalls")
536        .desc("number of stalls caused by tag array")
537        .flags(Stats::nozero)
538        ;
539
540    numDataArrayStalls
541        .name(name() + ".num_data_array_stalls")
542        .desc("number of stalls caused by data array")
543        .flags(Stats::nozero)
544        ;
545}
546
547// assumption: SLICC generated files will only call this function
548// once **all** resources are granted
549void
550CacheMemory::recordRequestType(CacheRequestType requestType, Addr addr)
551{
552    DPRINTF(RubyStats, "Recorded statistic: %s\n",
553            CacheRequestType_to_string(requestType));
554    switch(requestType) {
555    case CacheRequestType_DataArrayRead:
556        if (m_resource_stalls)
557            dataArray.reserve(addressToCacheSet(addr));
558        numDataArrayReads++;
559        return;
560    case CacheRequestType_DataArrayWrite:
561        if (m_resource_stalls)
562            dataArray.reserve(addressToCacheSet(addr));
563        numDataArrayWrites++;
564        return;
565    case CacheRequestType_TagArrayRead:
566        if (m_resource_stalls)
567            tagArray.reserve(addressToCacheSet(addr));
568        numTagArrayReads++;
569        return;
570    case CacheRequestType_TagArrayWrite:
571        if (m_resource_stalls)
572            tagArray.reserve(addressToCacheSet(addr));
573        numTagArrayWrites++;
574        return;
575    default:
576        warn("CacheMemory access_type not found: %s",
577             CacheRequestType_to_string(requestType));
578    }
579}
580
581bool
582CacheMemory::checkResourceAvailable(CacheResourceType res, Addr addr)
583{
584    if (!m_resource_stalls) {
585        return true;
586    }
587
588    if (res == CacheResourceType_TagArray) {
589        if (tagArray.tryAccess(addressToCacheSet(addr))) return true;
590        else {
591            DPRINTF(RubyResourceStalls,
592                    "Tag array stall on addr %#x in set %d\n",
593                    addr, addressToCacheSet(addr));
594            numTagArrayStalls++;
595            return false;
596        }
597    } else if (res == CacheResourceType_DataArray) {
598        if (dataArray.tryAccess(addressToCacheSet(addr))) return true;
599        else {
600            DPRINTF(RubyResourceStalls,
601                    "Data array stall on addr %#x in set %d\n",
602                    addr, addressToCacheSet(addr));
603            numDataArrayStalls++;
604            return false;
605        }
606    } else {
607        assert(false);
608        return true;
609    }
610}
611
612bool
613CacheMemory::isBlockInvalid(int64_t cache_set, int64_t loc)
614{
615  return (m_cache[cache_set][loc]->m_Permission == AccessPermission_Invalid);
616}
617
618bool
619CacheMemory::isBlockNotBusy(int64_t cache_set, int64_t loc)
620{
621  return (m_cache[cache_set][loc]->m_Permission != AccessPermission_Busy);
622}
623