CacheMemory.hh revision 7039
15081Sgblack@eecs.umich.edu/* 25081Sgblack@eecs.umich.edu * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 35081Sgblack@eecs.umich.edu * All rights reserved. 45081Sgblack@eecs.umich.edu * 55081Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without 65081Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are 75081Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright 85081Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer; 95081Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright 105081Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the 115081Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution; 125081Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its 135081Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from 145081Sgblack@eecs.umich.edu * this software without specific prior written permission. 155081Sgblack@eecs.umich.edu * 165081Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 175081Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 185081Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 195081Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 205081Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 215081Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 225081Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 235081Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 245081Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 255081Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 265081Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 275081Sgblack@eecs.umich.edu */ 285081Sgblack@eecs.umich.edu 295081Sgblack@eecs.umich.edu#ifndef __MEM_RUBY_SYSTEM_CACHEMEMORY_HH__ 305081Sgblack@eecs.umich.edu#define __MEM_RUBY_SYSTEM_CACHEMEMORY_HH__ 315081Sgblack@eecs.umich.edu 325081Sgblack@eecs.umich.edu#include <vector> 335081Sgblack@eecs.umich.edu 345081Sgblack@eecs.umich.edu#include "base/hashmap.hh" 355081Sgblack@eecs.umich.edu#include "mem/gems_common/Vector.hh" 365081Sgblack@eecs.umich.edu#include "mem/protocol/AccessPermission.hh" 375081Sgblack@eecs.umich.edu#include "mem/protocol/CacheMsg.hh" 385081Sgblack@eecs.umich.edu#include "mem/protocol/CacheRequestType.hh" 395081Sgblack@eecs.umich.edu#include "mem/protocol/MachineType.hh" 405081Sgblack@eecs.umich.edu#include "mem/ruby/common/Address.hh" 415081Sgblack@eecs.umich.edu#include "mem/ruby/common/DataBlock.hh" 425081Sgblack@eecs.umich.edu#include "mem/ruby/common/Global.hh" 435081Sgblack@eecs.umich.edu#include "mem/ruby/profiler/CacheProfiler.hh" 445081Sgblack@eecs.umich.edu#include "mem/ruby/recorder/CacheRecorder.hh" 455081Sgblack@eecs.umich.edu#include "mem/ruby/slicc_interface/AbstractCacheEntry.hh" 465081Sgblack@eecs.umich.edu#include "mem/ruby/slicc_interface/AbstractController.hh" 475081Sgblack@eecs.umich.edu#include "mem/ruby/slicc_interface/RubySlicc_ComponentMapping.hh" 485081Sgblack@eecs.umich.edu#include "mem/ruby/system/LRUPolicy.hh" 495081Sgblack@eecs.umich.edu#include "mem/ruby/system/PseudoLRUPolicy.hh" 505081Sgblack@eecs.umich.edu#include "mem/ruby/system/System.hh" 515081Sgblack@eecs.umich.edu#include "params/RubyCache.hh" 525081Sgblack@eecs.umich.edu#include "sim/sim_object.hh" 535081Sgblack@eecs.umich.edu 545081Sgblack@eecs.umich.educlass CacheMemory : public SimObject 555081Sgblack@eecs.umich.edu{ 565081Sgblack@eecs.umich.edu public: 575081Sgblack@eecs.umich.edu typedef RubyCacheParams Params; 585081Sgblack@eecs.umich.edu CacheMemory(const Params *p); 595081Sgblack@eecs.umich.edu ~CacheMemory(); 605081Sgblack@eecs.umich.edu 615081Sgblack@eecs.umich.edu void init(); 625081Sgblack@eecs.umich.edu 635081Sgblack@eecs.umich.edu // Public Methods 645081Sgblack@eecs.umich.edu void printConfig(ostream& out); 655081Sgblack@eecs.umich.edu 665081Sgblack@eecs.umich.edu // perform a cache access and see if we hit or not. Return true on a hit. 67 bool tryCacheAccess(const Address& address, CacheRequestType type, 68 DataBlock*& data_ptr); 69 70 // similar to above, but doesn't require full access check 71 bool testCacheAccess(const Address& address, CacheRequestType type, 72 DataBlock*& data_ptr); 73 74 // tests to see if an address is present in the cache 75 bool isTagPresent(const Address& address) const; 76 77 // Returns true if there is: 78 // a) a tag match on this address or there is 79 // b) an unused line in the same cache "way" 80 bool cacheAvail(const Address& address) const; 81 82 // find an unused entry and sets the tag appropriate for the address 83 void allocate(const Address& address, AbstractCacheEntry* new_entry); 84 85 // Explicitly free up this address 86 void deallocate(const Address& address); 87 88 // Returns with the physical address of the conflicting cache line 89 Address cacheProbe(const Address& address) const; 90 91 // looks an address up in the cache 92 AbstractCacheEntry& lookup(const Address& address); 93 const AbstractCacheEntry& lookup(const Address& address) const; 94 95 // Get/Set permission of cache block 96 AccessPermission getPermission(const Address& address) const; 97 void changePermission(const Address& address, AccessPermission new_perm); 98 99 int getLatency() const { return m_latency; } 100 101 // Hook for checkpointing the contents of the cache 102 void recordCacheContents(CacheRecorder& tr) const; 103 void 104 setAsInstructionCache(bool is_icache) 105 { 106 m_is_instruction_only_cache = is_icache; 107 } 108 109 // Set this address to most recently used 110 void setMRU(const Address& address); 111 112 void profileMiss(const CacheMsg & msg); 113 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); 118 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; 125 126 void clearStats() const; 127 void printStats(ostream& out) const; 128 129 private: 130 // convert a Address to its location in the cache 131 Index addressToCacheSet(const Address& address) const; 132 133 // Given a cache tag: returns the index of the tag in a set. 134 // returns -1 if the tag is not found. 135 int findTagInSet(Index line, const Address& tag) const; 136 int findTagInSetIgnorePermissions(Index cacheSet, 137 const Address& tag) const; 138 139 // Private copy constructor and assignment operator 140 CacheMemory(const CacheMemory& obj); 141 CacheMemory& operator=(const CacheMemory& obj); 142 143 private: 144 const string m_cache_name; 145 int m_latency; 146 147 // Data Members (m_prefix) 148 bool m_is_instruction_only_cache; 149 bool m_is_data_only_cache; 150 151 // The first index is the # of cache lines. 152 // The second index is the the amount associativity. 153 m5::hash_map<Address, int> m_tag_index; 154 Vector<Vector<AbstractCacheEntry*> > m_cache; 155 Vector<Vector<int> > m_locked; 156 157 AbstractReplacementPolicy *m_replacementPolicy_ptr; 158 159 CacheProfiler* m_profiler_ptr; 160 161 int m_cache_size; 162 string m_policy; 163 int m_cache_num_sets; 164 int m_cache_num_set_bits; 165 int m_cache_assoc; 166}; 167 168#endif // __MEM_RUBY_SYSTEM_CACHEMEMORY_HH__ 169 170