CacheMemory.hh revision 9104
112854Sgabeblack@google.com/* 212854Sgabeblack@google.com * Copyright (c) 1999-2012 Mark D. Hill and David A. Wood 312854Sgabeblack@google.com * All rights reserved. 412854Sgabeblack@google.com * 512854Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 612854Sgabeblack@google.com * modification, are permitted provided that the following conditions are 712854Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 812854Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 912854Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 1012854Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 1112854Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 1212854Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 1312854Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 1412854Sgabeblack@google.com * this software without specific prior written permission. 1512854Sgabeblack@google.com * 1612854Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1712854Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1812854Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1912854Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2012854Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2112854Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2212854Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2312854Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2412854Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2512854Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2612854Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2712854Sgabeblack@google.com */ 2812854Sgabeblack@google.com 2912854Sgabeblack@google.com#ifndef __MEM_RUBY_SYSTEM_CACHEMEMORY_HH__ 3012854Sgabeblack@google.com#define __MEM_RUBY_SYSTEM_CACHEMEMORY_HH__ 3112854Sgabeblack@google.com 3212854Sgabeblack@google.com#include <iostream> 3312854Sgabeblack@google.com#include <string> 3412854Sgabeblack@google.com#include <vector> 3512854Sgabeblack@google.com 3612854Sgabeblack@google.com#include "base/hashmap.hh" 3712854Sgabeblack@google.com#include "base/statistics.hh" 3812854Sgabeblack@google.com#include "mem/protocol/CacheRequestType.hh" 3912854Sgabeblack@google.com#include "mem/protocol/GenericRequestType.hh" 4012854Sgabeblack@google.com#include "mem/protocol/RubyRequest.hh" 4112854Sgabeblack@google.com#include "mem/ruby/common/DataBlock.hh" 4212854Sgabeblack@google.com#include "mem/ruby/profiler/CacheProfiler.hh" 4312854Sgabeblack@google.com#include "mem/ruby/recorder/CacheRecorder.hh" 4412854Sgabeblack@google.com#include "mem/ruby/slicc_interface/AbstractCacheEntry.hh" 4512854Sgabeblack@google.com#include "mem/ruby/slicc_interface/RubySlicc_ComponentMapping.hh" 4612854Sgabeblack@google.com#include "mem/ruby/system/LRUPolicy.hh" 4712854Sgabeblack@google.com#include "mem/ruby/system/PseudoLRUPolicy.hh" 4812854Sgabeblack@google.com#include "params/RubyCache.hh" 4912854Sgabeblack@google.com#include "sim/sim_object.hh" 5012854Sgabeblack@google.com 5112854Sgabeblack@google.comclass CacheMemory : public SimObject 5212854Sgabeblack@google.com{ 5312854Sgabeblack@google.com public: 5412854Sgabeblack@google.com typedef RubyCacheParams Params; 5512854Sgabeblack@google.com CacheMemory(const Params *p); 5612854Sgabeblack@google.com ~CacheMemory(); 5712854Sgabeblack@google.com 5812854Sgabeblack@google.com void init(); 5912854Sgabeblack@google.com 6012854Sgabeblack@google.com // Public Methods 6112854Sgabeblack@google.com void printConfig(std::ostream& out); 6212854Sgabeblack@google.com 6312854Sgabeblack@google.com // perform a cache access and see if we hit or not. Return true on a hit. 6412854Sgabeblack@google.com bool tryCacheAccess(const Address& address, RubyRequestType type, 6512854Sgabeblack@google.com DataBlock*& data_ptr); 6612854Sgabeblack@google.com 6712854Sgabeblack@google.com // similar to above, but doesn't require full access check 6812854Sgabeblack@google.com bool testCacheAccess(const Address& address, RubyRequestType type, 6912854Sgabeblack@google.com DataBlock*& data_ptr); 7012854Sgabeblack@google.com 7112854Sgabeblack@google.com // tests to see if an address is present in the cache 7212854Sgabeblack@google.com bool isTagPresent(const Address& address) const; 7312854Sgabeblack@google.com 7412854Sgabeblack@google.com // Returns true if there is: 7512854Sgabeblack@google.com // a) a tag match on this address or there is 7612854Sgabeblack@google.com // b) an unused line in the same cache "way" 7712854Sgabeblack@google.com bool cacheAvail(const Address& address) const; 7812854Sgabeblack@google.com 7912854Sgabeblack@google.com // find an unused entry and sets the tag appropriate for the address 8012854Sgabeblack@google.com AbstractCacheEntry* allocate(const Address& address, AbstractCacheEntry* new_entry); 8112854Sgabeblack@google.com void allocateVoid(const Address& address, AbstractCacheEntry* new_entry) 8212854Sgabeblack@google.com { 8312854Sgabeblack@google.com allocate(address, new_entry); 8412854Sgabeblack@google.com } 8512854Sgabeblack@google.com 8612854Sgabeblack@google.com // Explicitly free up this address 8712854Sgabeblack@google.com void deallocate(const Address& address); 8812854Sgabeblack@google.com 8912854Sgabeblack@google.com // Returns with the physical address of the conflicting cache line 9012854Sgabeblack@google.com Address cacheProbe(const Address& address) const; 9112854Sgabeblack@google.com 9212854Sgabeblack@google.com // looks an address up in the cache 9312854Sgabeblack@google.com AbstractCacheEntry* lookup(const Address& address); 9412854Sgabeblack@google.com const AbstractCacheEntry* lookup(const Address& address) const; 9512854Sgabeblack@google.com 9612854Sgabeblack@google.com int getLatency() const { return m_latency; } 9712854Sgabeblack@google.com 9812854Sgabeblack@google.com // Hook for checkpointing the contents of the cache 9912854Sgabeblack@google.com void recordCacheContents(int cntrl, CacheRecorder* tr) const; 10012854Sgabeblack@google.com 10112854Sgabeblack@google.com // Set this address to most recently used 10212854Sgabeblack@google.com void setMRU(const Address& address); 10312854Sgabeblack@google.com 10412854Sgabeblack@google.com void profileMiss(const RubyRequest & msg); 10512854Sgabeblack@google.com 10612854Sgabeblack@google.com void profileGenericRequest(GenericRequestType requestType, 10712854Sgabeblack@google.com RubyAccessMode accessType, 10812854Sgabeblack@google.com PrefetchBit pfBit); 10912854Sgabeblack@google.com 11012854Sgabeblack@google.com void setLocked (const Address& addr, int context); 11112854Sgabeblack@google.com void clearLocked (const Address& addr); 11212854Sgabeblack@google.com bool isLocked (const Address& addr, int context); 11312854Sgabeblack@google.com // Print cache contents 11412854Sgabeblack@google.com void print(std::ostream& out) const; 11512854Sgabeblack@google.com void printData(std::ostream& out) const; 11612854Sgabeblack@google.com 11712854Sgabeblack@google.com void clearStats() const; 11812854Sgabeblack@google.com void printStats(std::ostream& out) const; 11912854Sgabeblack@google.com 12012854Sgabeblack@google.com void recordRequestType(CacheRequestType requestType); 12112854Sgabeblack@google.com void regStats(); 12212854Sgabeblack@google.com 12312854Sgabeblack@google.com Stats::Scalar numDataArrayReads; 12412854Sgabeblack@google.com Stats::Scalar numDataArrayWrites; 12512854Sgabeblack@google.com Stats::Scalar numTagArrayReads; 12612854Sgabeblack@google.com Stats::Scalar numTagArrayWrites; 12712854Sgabeblack@google.com 12812854Sgabeblack@google.com private: 12912854Sgabeblack@google.com // convert a Address to its location in the cache 13012854Sgabeblack@google.com Index addressToCacheSet(const Address& address) const; 13112854Sgabeblack@google.com 13212854Sgabeblack@google.com // Given a cache tag: returns the index of the tag in a set. 13312854Sgabeblack@google.com // returns -1 if the tag is not found. 13412854Sgabeblack@google.com int findTagInSet(Index line, const Address& tag) const; 13512854Sgabeblack@google.com int findTagInSetIgnorePermissions(Index cacheSet, 13612854Sgabeblack@google.com const Address& tag) const; 13712854Sgabeblack@google.com 13812854Sgabeblack@google.com // Private copy constructor and assignment operator 13912854Sgabeblack@google.com CacheMemory(const CacheMemory& obj); 14012854Sgabeblack@google.com CacheMemory& operator=(const CacheMemory& obj); 14112854Sgabeblack@google.com 14212854Sgabeblack@google.com private: 14312854Sgabeblack@google.com const std::string m_cache_name; 14412854Sgabeblack@google.com int m_latency; 14512854Sgabeblack@google.com 14612854Sgabeblack@google.com // Data Members (m_prefix) 14712854Sgabeblack@google.com bool m_is_instruction_only_cache; 14812854Sgabeblack@google.com 14912854Sgabeblack@google.com // The first index is the # of cache lines. 15012854Sgabeblack@google.com // The second index is the the amount associativity. 15112854Sgabeblack@google.com m5::hash_map<Address, int> m_tag_index; 15212854Sgabeblack@google.com std::vector<std::vector<AbstractCacheEntry*> > m_cache; 15312854Sgabeblack@google.com 15412854Sgabeblack@google.com AbstractReplacementPolicy *m_replacementPolicy_ptr; 15512854Sgabeblack@google.com 15612854Sgabeblack@google.com CacheProfiler* m_profiler_ptr; 15712854Sgabeblack@google.com 15812854Sgabeblack@google.com int m_cache_size; 15912854Sgabeblack@google.com std::string m_policy; 16012854Sgabeblack@google.com int m_cache_num_sets; 16112854Sgabeblack@google.com int m_cache_num_set_bits; 16212854Sgabeblack@google.com int m_cache_assoc; 163 int m_start_index_bit; 164}; 165 166#endif // __MEM_RUBY_SYSTEM_CACHEMEMORY_HH__ 167 168