CacheMemory.hh revision 9104
1/*
2 * Copyright (c) 1999-2012 Mark D. Hill and David A. Wood
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef __MEM_RUBY_SYSTEM_CACHEMEMORY_HH__
30#define __MEM_RUBY_SYSTEM_CACHEMEMORY_HH__
31
32#include <iostream>
33#include <string>
34#include <vector>
35
36#include "base/hashmap.hh"
37#include "base/statistics.hh"
38#include "mem/protocol/CacheRequestType.hh"
39#include "mem/protocol/GenericRequestType.hh"
40#include "mem/protocol/RubyRequest.hh"
41#include "mem/ruby/common/DataBlock.hh"
42#include "mem/ruby/profiler/CacheProfiler.hh"
43#include "mem/ruby/recorder/CacheRecorder.hh"
44#include "mem/ruby/slicc_interface/AbstractCacheEntry.hh"
45#include "mem/ruby/slicc_interface/RubySlicc_ComponentMapping.hh"
46#include "mem/ruby/system/LRUPolicy.hh"
47#include "mem/ruby/system/PseudoLRUPolicy.hh"
48#include "params/RubyCache.hh"
49#include "sim/sim_object.hh"
50
51class CacheMemory : public SimObject
52{
53  public:
54    typedef RubyCacheParams Params;
55    CacheMemory(const Params *p);
56    ~CacheMemory();
57
58    void init();
59
60    // Public Methods
61    void printConfig(std::ostream& out);
62
63    // perform a cache access and see if we hit or not.  Return true on a hit.
64    bool tryCacheAccess(const Address& address, RubyRequestType type,
65                        DataBlock*& data_ptr);
66
67    // similar to above, but doesn't require full access check
68    bool testCacheAccess(const Address& address, RubyRequestType type,
69                         DataBlock*& data_ptr);
70
71    // tests to see if an address is present in the cache
72    bool isTagPresent(const Address& address) const;
73
74    // Returns true if there is:
75    //   a) a tag match on this address or there is
76    //   b) an unused line in the same cache "way"
77    bool cacheAvail(const Address& address) const;
78
79    // find an unused entry and sets the tag appropriate for the address
80    AbstractCacheEntry* allocate(const Address& address, AbstractCacheEntry* new_entry);
81    void allocateVoid(const Address& address, AbstractCacheEntry* new_entry)
82    {
83        allocate(address, new_entry);
84    }
85
86    // Explicitly free up this address
87    void deallocate(const Address& address);
88
89    // Returns with the physical address of the conflicting cache line
90    Address cacheProbe(const Address& address) const;
91
92    // looks an address up in the cache
93    AbstractCacheEntry* lookup(const Address& address);
94    const AbstractCacheEntry* lookup(const Address& address) const;
95
96    int getLatency() const { return m_latency; }
97
98    // Hook for checkpointing the contents of the cache
99    void recordCacheContents(int cntrl, CacheRecorder* tr) const;
100
101    // Set this address to most recently used
102    void setMRU(const Address& address);
103
104    void profileMiss(const RubyRequest & msg);
105
106    void profileGenericRequest(GenericRequestType requestType,
107                               RubyAccessMode accessType,
108                               PrefetchBit pfBit);
109
110    void setLocked (const Address& addr, int context);
111    void clearLocked (const Address& addr);
112    bool isLocked (const Address& addr, int context);
113    // Print cache contents
114    void print(std::ostream& out) const;
115    void printData(std::ostream& out) const;
116
117    void clearStats() const;
118    void printStats(std::ostream& out) const;
119
120    void recordRequestType(CacheRequestType requestType);
121    void regStats();
122
123    Stats::Scalar numDataArrayReads;
124    Stats::Scalar numDataArrayWrites;
125    Stats::Scalar numTagArrayReads;
126    Stats::Scalar numTagArrayWrites;
127
128  private:
129    // convert a Address to its location in the cache
130    Index addressToCacheSet(const Address& address) const;
131
132    // Given a cache tag: returns the index of the tag in a set.
133    // returns -1 if the tag is not found.
134    int findTagInSet(Index line, const Address& tag) const;
135    int findTagInSetIgnorePermissions(Index cacheSet,
136                                      const Address& tag) const;
137
138    // Private copy constructor and assignment operator
139    CacheMemory(const CacheMemory& obj);
140    CacheMemory& operator=(const CacheMemory& obj);
141
142  private:
143    const std::string m_cache_name;
144    int m_latency;
145
146    // Data Members (m_prefix)
147    bool m_is_instruction_only_cache;
148
149    // The first index is the # of cache lines.
150    // The second index is the the amount associativity.
151    m5::hash_map<Address, int> m_tag_index;
152    std::vector<std::vector<AbstractCacheEntry*> > m_cache;
153
154    AbstractReplacementPolicy *m_replacementPolicy_ptr;
155
156    CacheProfiler* m_profiler_ptr;
157
158    int m_cache_size;
159    std::string m_policy;
160    int m_cache_num_sets;
161    int m_cache_num_set_bits;
162    int m_cache_assoc;
163    int m_start_index_bit;
164};
165
166#endif // __MEM_RUBY_SYSTEM_CACHEMEMORY_HH__
167
168