2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
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.
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.
34 * $Id: CacheMemory.hh,v 3.7 2004/06/18 20:15:15 beckmann Exp $
41 #include "mem/ruby/common/Global.hh"
42 #include "mem/protocol/AccessPermission.hh"
43 #include "mem/ruby/common/Address.hh"
44 #include "mem/ruby/recorder/CacheRecorder.hh"
45 #include "mem/protocol/CacheRequestType.hh"
46 #include "mem/gems_common/Vector.hh"
47 #include "mem/ruby/common/DataBlock.hh"
48 #include "mem/protocol/MachineType.hh"
49 #include "mem/ruby/slicc_interface/RubySlicc_ComponentMapping.hh"
50 #include "mem/ruby/system/PseudoLRUPolicy.hh"
51 #include "mem/ruby/system/LRUPolicy.hh"
52 #include "mem/ruby/slicc_interface/AbstractCacheEntry.hh"
53 #include "mem/ruby/system/System.hh"
54 #include "mem/ruby/slicc_interface/AbstractController.hh"
61 CacheMemory(const string & name);
62 void init(const vector<string> & argv);
68 // static CacheMemory* createCache(int level, int num, char split_type, AbstractCacheEntry* (*entry_factory)());
69 // static CacheMemory* getCache(int cache_id);
72 void printConfig(ostream& out);
74 // perform a cache access and see if we hit or not. Return true on a hit.
75 bool tryCacheAccess(const Address& address, CacheRequestType type, DataBlock*& data_ptr);
77 // similar to above, but doesn't require full access check
78 bool testCacheAccess(const Address& address, CacheRequestType type, DataBlock*& data_ptr);
80 // tests to see if an address is present in the cache
81 bool isTagPresent(const Address& address) const;
83 // Returns true if there is:
84 // a) a tag match on this address or there is
85 // b) an unused line in the same cache "way"
86 bool cacheAvail(const Address& address) const;
88 // find an unused entry and sets the tag appropriate for the address
89 void allocate(const Address& address, AbstractCacheEntry* new_entry);
91 // Explicitly free up this address
92 void deallocate(const Address& address);
94 // Returns with the physical address of the conflicting cache line
95 Address cacheProbe(const Address& address) const;
97 // looks an address up in the cache
98 AbstractCacheEntry& lookup(const Address& address);
99 const AbstractCacheEntry& lookup(const Address& address) const;
101 // Get/Set permission of cache block
102 AccessPermission getPermission(const Address& address) const;
103 void changePermission(const Address& address, AccessPermission new_perm);
105 int getLatency() const { return m_latency; }
107 // Hook for checkpointing the contents of the cache
108 void recordCacheContents(CacheRecorder& tr) const;
109 void setAsInstructionCache(bool is_icache) { m_is_instruction_only_cache = is_icache; }
111 // Set this address to most recently used
112 void setMRU(const Address& address);
114 void getMemoryValue(const Address& addr, char* value,
115 unsigned int size_in_bytes );
116 void setMemoryValue(const Address& addr, char* value,
117 unsigned int size_in_bytes );
119 void setLocked (const Address& addr, int context);
120 void clearLocked (const Address& addr);
121 bool isLocked (const Address& addr, int context);
122 // Print cache contents
123 void print(ostream& out) const;
124 void printData(ostream& out) const;
129 // convert a Address to its location in the cache
130 Index addressToCacheSet(const Address& address) const;
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, const Address& tag) const;
137 // Private copy constructor and assignment operator
138 CacheMemory(const CacheMemory& obj);
139 CacheMemory& operator=(const CacheMemory& obj);
142 const string m_cache_name;
143 AbstractController* m_controller;
146 // Data Members (m_prefix)
147 bool m_is_instruction_only_cache;
148 bool m_is_data_only_cache;
150 // The first index is the # of cache lines.
151 // The second index is the the amount associativity.
152 Vector<Vector<AbstractCacheEntry*> > m_cache;
153 Vector<Vector<int> > m_locked;
155 AbstractReplacementPolicy *m_replacementPolicy_ptr;
157 int m_cache_num_sets;
158 int m_cache_num_set_bits;
161 static Vector< CacheMemory* > m_all_caches;
164 // Output operator declaration
165 //ostream& operator<<(ostream& out, const CacheMemory<ENTRY>& obj);
167 // ******************* Definitions *******************
169 // Output operator definition
171 ostream& operator<<(ostream& out, const CacheMemory& obj)
179 // ****************************************************************
182 CacheMemory::CacheMemory(const string & name)
188 void CacheMemory::init(const vector<string> & argv)
194 for (uint32 i=0; i<argv.size(); i+=2) {
195 if (argv[i] == "size_kb") {
196 cache_size = atoi(argv[i+1].c_str());
197 } else if (argv[i] == "latency") {
198 m_latency = atoi(argv[i+1].c_str());
199 } else if (argv[i] == "assoc") {
200 m_cache_assoc = atoi(argv[i+1].c_str());
201 } else if (argv[i] == "replacement_policy") {
203 } else if (argv[i] == "controller") {
204 m_controller = RubySystem::getController(argv[i+1]);
206 cerr << "WARNING: CacheMemory: Unknown configuration parameter: " << argv[i] << endl;
210 m_cache_num_sets = cache_size / m_cache_assoc;
211 m_cache_num_set_bits = log_int(m_cache_num_sets);
213 if(policy == "PSEUDO_LRU")
214 m_replacementPolicy_ptr = new PseudoLRUPolicy(m_cache_num_sets, m_cache_assoc);
215 else if (policy == "LRU")
216 m_replacementPolicy_ptr = new LRUPolicy(m_cache_num_sets, m_cache_assoc);
220 m_cache.setSize(m_cache_num_sets);
221 m_locked.setSize(m_cache_num_sets);
222 for (int i = 0; i < m_cache_num_sets; i++) {
223 m_cache[i].setSize(m_cache_assoc);
224 m_locked[i].setSize(m_cache_assoc);
225 for (int j = 0; j < m_cache_assoc; j++) {
226 m_cache[i][j] = NULL;
233 CacheMemory::~CacheMemory()
235 if(m_replacementPolicy_ptr != NULL)
236 delete m_replacementPolicy_ptr;
240 void CacheMemory::printConfig(ostream& out)
242 out << "Cache config: " << m_cache_name << endl;
243 if (m_controller != NULL)
244 out << " controller: " << m_controller->getName() << endl;
245 out << " cache_associativity: " << m_cache_assoc << endl;
246 out << " num_cache_sets_bits: " << m_cache_num_set_bits << endl;
247 const int cache_num_sets = 1 << m_cache_num_set_bits;
248 out << " num_cache_sets: " << cache_num_sets << endl;
249 out << " cache_set_size_bytes: " << cache_num_sets * RubySystem::getBlockSizeBytes() << endl;
250 out << " cache_set_size_Kbytes: "
251 << double(cache_num_sets * RubySystem::getBlockSizeBytes()) / (1<<10) << endl;
252 out << " cache_set_size_Mbytes: "
253 << double(cache_num_sets * RubySystem::getBlockSizeBytes()) / (1<<20) << endl;
254 out << " cache_size_bytes: "
255 << cache_num_sets * RubySystem::getBlockSizeBytes() * m_cache_assoc << endl;
256 out << " cache_size_Kbytes: "
257 << double(cache_num_sets * RubySystem::getBlockSizeBytes() * m_cache_assoc) / (1<<10) << endl;
258 out << " cache_size_Mbytes: "
259 << double(cache_num_sets * RubySystem::getBlockSizeBytes() * m_cache_assoc) / (1<<20) << endl;
264 // convert a Address to its location in the cache
266 Index CacheMemory::addressToCacheSet(const Address& address) const
268 assert(address == line_address(address));
269 return address.bitSelect(RubySystem::getBlockSizeBits(), RubySystem::getBlockSizeBits() + m_cache_num_set_bits-1);
272 // Given a cache index: returns the index of the tag in a set.
273 // returns -1 if the tag is not found.
275 int CacheMemory::findTagInSet(Index cacheSet, const Address& tag) const
277 assert(tag == line_address(tag));
278 // search the set for the tags
279 for (int i=0; i < m_cache_assoc; i++) {
280 if ((m_cache[cacheSet][i] != NULL) &&
281 (m_cache[cacheSet][i]->m_Address == tag) &&
282 (m_cache[cacheSet][i]->m_Permission != AccessPermission_NotPresent)) {
286 return -1; // Not found
289 // Given a cache index: returns the index of the tag in a set.
290 // returns -1 if the tag is not found.
292 int CacheMemory::findTagInSetIgnorePermissions(Index cacheSet, const Address& tag) const
294 assert(tag == line_address(tag));
295 // search the set for the tags
296 for (int i=0; i < m_cache_assoc; i++) {
297 if (m_cache[cacheSet][i] != NULL && m_cache[cacheSet][i]->m_Address == tag)
300 return -1; // Not found
305 bool CacheMemory::tryCacheAccess(const Address& address,
306 CacheRequestType type,
307 DataBlock*& data_ptr)
309 assert(address == line_address(address));
310 DEBUG_EXPR(CACHE_COMP, HighPrio, address);
311 Index cacheSet = addressToCacheSet(address);
312 int loc = findTagInSet(cacheSet, address);
313 if(loc != -1){ // Do we even have a tag match?
314 AbstractCacheEntry* entry = m_cache[cacheSet][loc];
315 m_replacementPolicy_ptr->touch(cacheSet, loc, g_eventQueue_ptr->getTime());
316 data_ptr = &(entry->getDataBlk());
318 if(entry->m_Permission == AccessPermission_Read_Write) {
321 if ((entry->m_Permission == AccessPermission_Read_Only) &&
322 (type == CacheRequestType_LD || type == CacheRequestType_IFETCH)) {
325 // The line must not be accessible
332 bool CacheMemory::testCacheAccess(const Address& address,
333 CacheRequestType type,
334 DataBlock*& data_ptr)
336 assert(address == line_address(address));
337 DEBUG_EXPR(CACHE_COMP, HighPrio, address);
338 Index cacheSet = addressToCacheSet(address);
339 int loc = findTagInSet(cacheSet, address);
340 if(loc != -1){ // Do we even have a tag match?
341 AbstractCacheEntry* entry = m_cache[cacheSet][loc];
342 m_replacementPolicy_ptr->touch(cacheSet, loc, g_eventQueue_ptr->getTime());
343 data_ptr = &(entry->getDataBlk());
345 return (m_cache[cacheSet][loc]->m_Permission != AccessPermission_NotPresent);
351 // tests to see if an address is present in the cache
353 bool CacheMemory::isTagPresent(const Address& address) const
355 assert(address == line_address(address));
356 Index cacheSet = addressToCacheSet(address);
357 int location = findTagInSet(cacheSet, address);
359 if (location == -1) {
360 // We didn't find the tag
361 DEBUG_EXPR(CACHE_COMP, LowPrio, address);
362 DEBUG_MSG(CACHE_COMP, LowPrio, "No tag match");
365 DEBUG_EXPR(CACHE_COMP, LowPrio, address);
366 DEBUG_MSG(CACHE_COMP, LowPrio, "found");
370 // Returns true if there is:
371 // a) a tag match on this address or there is
372 // b) an unused line in the same cache "way"
374 bool CacheMemory::cacheAvail(const Address& address) const
376 assert(address == line_address(address));
378 Index cacheSet = addressToCacheSet(address);
380 for (int i=0; i < m_cache_assoc; i++) {
381 AbstractCacheEntry* entry = m_cache[cacheSet][i];
383 if (entry->m_Address == address || // Already in the cache
384 entry->m_Permission == AccessPermission_NotPresent) { // We found an empty entry
395 void CacheMemory::allocate(const Address& address, AbstractCacheEntry* entry)
397 assert(address == line_address(address));
398 assert(!isTagPresent(address));
399 assert(cacheAvail(address));
400 DEBUG_EXPR(CACHE_COMP, HighPrio, address);
402 // Find the first open slot
403 Index cacheSet = addressToCacheSet(address);
404 for (int i=0; i < m_cache_assoc; i++) {
405 if (m_cache[cacheSet][i] == NULL ||
406 m_cache[cacheSet][i]->m_Permission == AccessPermission_NotPresent) {
407 m_cache[cacheSet][i] = entry; // Init entry
408 m_cache[cacheSet][i]->m_Address = address;
409 m_cache[cacheSet][i]->m_Permission = AccessPermission_Invalid;
410 m_locked[cacheSet][i] = -1;
412 m_replacementPolicy_ptr->touch(cacheSet, i, g_eventQueue_ptr->getTime());
417 ERROR_MSG("Allocate didn't find an available entry");
421 void CacheMemory::deallocate(const Address& address)
423 assert(address == line_address(address));
424 assert(isTagPresent(address));
425 DEBUG_EXPR(CACHE_COMP, HighPrio, address);
426 Index cacheSet = addressToCacheSet(address);
427 int location = findTagInSet(cacheSet, address);
429 delete m_cache[cacheSet][location];
430 m_cache[cacheSet][location] = NULL;
431 m_locked[cacheSet][location] = -1;
435 // Returns with the physical address of the conflicting cache line
437 Address CacheMemory::cacheProbe(const Address& address) const
439 assert(address == line_address(address));
440 assert(!cacheAvail(address));
442 Index cacheSet = addressToCacheSet(address);
443 return m_cache[cacheSet][m_replacementPolicy_ptr->getVictim(cacheSet)]->m_Address;
446 // looks an address up in the cache
448 AbstractCacheEntry& CacheMemory::lookup(const Address& address)
450 assert(address == line_address(address));
451 Index cacheSet = addressToCacheSet(address);
452 int loc = findTagInSet(cacheSet, address);
454 return *m_cache[cacheSet][loc];
457 // looks an address up in the cache
459 const AbstractCacheEntry& CacheMemory::lookup(const Address& address) const
461 assert(address == line_address(address));
462 Index cacheSet = addressToCacheSet(address);
463 int loc = findTagInSet(cacheSet, address);
465 return *m_cache[cacheSet][loc];
469 AccessPermission CacheMemory::getPermission(const Address& address) const
471 assert(address == line_address(address));
472 return lookup(address).m_Permission;
476 void CacheMemory::changePermission(const Address& address, AccessPermission new_perm)
478 assert(address == line_address(address));
479 lookup(address).m_Permission = new_perm;
480 Index cacheSet = addressToCacheSet(address);
481 int loc = findTagInSet(cacheSet, address);
482 m_locked[cacheSet][loc] = -1;
483 assert(getPermission(address) == new_perm);
486 // Sets the most recently used bit for a cache block
488 void CacheMemory::setMRU(const Address& address)
492 cacheSet = addressToCacheSet(address);
493 m_replacementPolicy_ptr->touch(cacheSet,
494 findTagInSet(cacheSet, address),
495 g_eventQueue_ptr->getTime());
499 void CacheMemory::recordCacheContents(CacheRecorder& tr) const
501 for (int i = 0; i < m_cache_num_sets; i++) {
502 for (int j = 0; j < m_cache_assoc; j++) {
503 AccessPermission perm = m_cache[i][j]->m_Permission;
504 CacheRequestType request_type = CacheRequestType_NULL;
505 if (perm == AccessPermission_Read_Only) {
506 if (m_is_instruction_only_cache) {
507 request_type = CacheRequestType_IFETCH;
509 request_type = CacheRequestType_LD;
511 } else if (perm == AccessPermission_Read_Write) {
512 request_type = CacheRequestType_ST;
515 if (request_type != CacheRequestType_NULL) {
516 // tr.addRecord(m_chip_ptr->getID(), m_cache[i][j].m_Address,
517 // Address(0), request_type, m_replacementPolicy_ptr->getLastAccess(i, j));
524 void CacheMemory::print(ostream& out) const
526 out << "Cache dump: " << m_cache_name << endl;
527 for (int i = 0; i < m_cache_num_sets; i++) {
528 for (int j = 0; j < m_cache_assoc; j++) {
529 if (m_cache[i][j] != NULL) {
530 out << " Index: " << i
532 << " entry: " << *m_cache[i][j] << endl;
534 out << " Index: " << i
536 << " entry: NULL" << endl;
543 void CacheMemory::printData(ostream& out) const
545 out << "printData() not supported" << endl;
549 void CacheMemory::getMemoryValue(const Address& addr, char* value,
550 unsigned int size_in_bytes ){
551 AbstractCacheEntry& entry = lookup(line_address(addr));
552 unsigned int startByte = addr.getAddress() - line_address(addr).getAddress();
553 for(unsigned int i=0; i<size_in_bytes; ++i){
554 value[i] = entry.getDataBlk().getByte(i + startByte);
559 void CacheMemory::setMemoryValue(const Address& addr, char* value,
560 unsigned int size_in_bytes ){
561 AbstractCacheEntry& entry = lookup(line_address(addr));
562 unsigned int startByte = addr.getAddress() - line_address(addr).getAddress();
563 assert(size_in_bytes > 0);
564 for(unsigned int i=0; i<size_in_bytes; ++i){
565 entry.getDataBlk().setByte(i + startByte, value[i]);
568 // entry = lookup(line_address(addr));
573 CacheMemory::setLocked(const Address& address, int context)
575 assert(address == line_address(address));
576 Index cacheSet = addressToCacheSet(address);
577 int loc = findTagInSet(cacheSet, address);
579 m_locked[cacheSet][loc] = context;
584 CacheMemory::clearLocked(const Address& address)
586 assert(address == line_address(address));
587 Index cacheSet = addressToCacheSet(address);
588 int loc = findTagInSet(cacheSet, address);
590 m_locked[cacheSet][loc] = -1;
595 CacheMemory::isLocked(const Address& address, int context)
597 assert(address == line_address(address));
598 Index cacheSet = addressToCacheSet(address);
599 int loc = findTagInSet(cacheSet, address);
601 return m_locked[cacheSet][loc] == context;
604 #endif //CACHEMEMORY_H