CacheMemory.hh revision 8193
15647Sgblack@eecs.umich.edu/* 25647Sgblack@eecs.umich.edu * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood 35647Sgblack@eecs.umich.edu * All rights reserved. 45647Sgblack@eecs.umich.edu * 55647Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without 65647Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are 75647Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright 85647Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer; 95647Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright 105647Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the 115647Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution; 125647Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its 135647Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from 145647Sgblack@eecs.umich.edu * this software without specific prior written permission. 155647Sgblack@eecs.umich.edu * 165647Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 175647Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 185647Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 195647Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 205647Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 215647Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 225647Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 235647Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 245647Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 255647Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 265647Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 275647Sgblack@eecs.umich.edu */ 285647Sgblack@eecs.umich.edu 295647Sgblack@eecs.umich.edu#ifndef __MEM_RUBY_SYSTEM_CACHEMEMORY_HH__ 305647Sgblack@eecs.umich.edu#define __MEM_RUBY_SYSTEM_CACHEMEMORY_HH__ 315647Sgblack@eecs.umich.edu 325647Sgblack@eecs.umich.edu#include <iostream> 335647Sgblack@eecs.umich.edu#include <string> 345647Sgblack@eecs.umich.edu#include <vector> 355647Sgblack@eecs.umich.edu 365647Sgblack@eecs.umich.edu#include "base/hashmap.hh" 375647Sgblack@eecs.umich.edu#include "mem/protocol/AccessPermission.hh" 385647Sgblack@eecs.umich.edu#include "mem/protocol/RubyRequest.hh" 395647Sgblack@eecs.umich.edu#include "mem/protocol/RubyRequestType.hh" 405647Sgblack@eecs.umich.edu#include "mem/protocol/GenericRequestType.hh" 415647Sgblack@eecs.umich.edu#include "mem/protocol/MachineType.hh" 425647Sgblack@eecs.umich.edu#include "mem/ruby/common/Address.hh" 435647Sgblack@eecs.umich.edu#include "mem/ruby/common/DataBlock.hh" 445647Sgblack@eecs.umich.edu#include "mem/ruby/common/Global.hh" 455647Sgblack@eecs.umich.edu#include "mem/ruby/profiler/CacheProfiler.hh" 465647Sgblack@eecs.umich.edu#include "mem/ruby/recorder/CacheRecorder.hh" 475647Sgblack@eecs.umich.edu#include "mem/ruby/slicc_interface/AbstractCacheEntry.hh" 485647Sgblack@eecs.umich.edu#include "mem/ruby/slicc_interface/AbstractController.hh" 495647Sgblack@eecs.umich.edu#include "mem/ruby/slicc_interface/RubySlicc_ComponentMapping.hh" 505647Sgblack@eecs.umich.edu#include "mem/ruby/system/LRUPolicy.hh" 515647Sgblack@eecs.umich.edu#include "mem/ruby/system/PseudoLRUPolicy.hh" 525647Sgblack@eecs.umich.edu#include "mem/ruby/system/System.hh" 535647Sgblack@eecs.umich.edu#include "params/RubyCache.hh" 545647Sgblack@eecs.umich.edu#include "sim/sim_object.hh" 555647Sgblack@eecs.umich.edu 565647Sgblack@eecs.umich.educlass CacheMemory : public SimObject 575647Sgblack@eecs.umich.edu{ 585648Sgblack@eecs.umich.edu public: 595647Sgblack@eecs.umich.edu typedef RubyCacheParams Params; 605654Sgblack@eecs.umich.edu CacheMemory(const Params *p); 615647Sgblack@eecs.umich.edu ~CacheMemory(); 625654Sgblack@eecs.umich.edu 636046Sgblack@eecs.umich.edu void init(); 645647Sgblack@eecs.umich.edu 655648Sgblack@eecs.umich.edu // Public Methods 665648Sgblack@eecs.umich.edu void printConfig(std::ostream& out); 675647Sgblack@eecs.umich.edu 685647Sgblack@eecs.umich.edu // perform a cache access and see if we hit or not. Return true on a hit. 695647Sgblack@eecs.umich.edu bool tryCacheAccess(const Address& address, RubyRequestType type, 705647Sgblack@eecs.umich.edu DataBlock*& data_ptr); 715647Sgblack@eecs.umich.edu 725647Sgblack@eecs.umich.edu // similar to above, but doesn't require full access check 735647Sgblack@eecs.umich.edu bool testCacheAccess(const Address& address, RubyRequestType type, 745647Sgblack@eecs.umich.edu DataBlock*& data_ptr); 755647Sgblack@eecs.umich.edu 765648Sgblack@eecs.umich.edu // tests to see if an address is present in the cache 775647Sgblack@eecs.umich.edu bool isTagPresent(const Address& address) const; 785648Sgblack@eecs.umich.edu 795648Sgblack@eecs.umich.edu // Returns true if there is: 805648Sgblack@eecs.umich.edu // a) a tag match on this address or there is 815648Sgblack@eecs.umich.edu // b) an unused line in the same cache "way" 825648Sgblack@eecs.umich.edu bool cacheAvail(const Address& address) const; 835648Sgblack@eecs.umich.edu 845648Sgblack@eecs.umich.edu // find an unused entry and sets the tag appropriate for the address 855648Sgblack@eecs.umich.edu AbstractCacheEntry* allocate(const Address& address, AbstractCacheEntry* new_entry); 865648Sgblack@eecs.umich.edu void allocateVoid(const Address& address, AbstractCacheEntry* new_entry) 875648Sgblack@eecs.umich.edu { 885648Sgblack@eecs.umich.edu allocate(address, new_entry); 895648Sgblack@eecs.umich.edu } 905648Sgblack@eecs.umich.edu 915648Sgblack@eecs.umich.edu // Explicitly free up this address 925648Sgblack@eecs.umich.edu void deallocate(const Address& address); 935648Sgblack@eecs.umich.edu 945648Sgblack@eecs.umich.edu // Returns with the physical address of the conflicting cache line 955648Sgblack@eecs.umich.edu Address cacheProbe(const Address& address) const; 965648Sgblack@eecs.umich.edu 975648Sgblack@eecs.umich.edu // looks an address up in the cache 985648Sgblack@eecs.umich.edu AbstractCacheEntry* lookup(const Address& address); 995648Sgblack@eecs.umich.edu const AbstractCacheEntry* lookup(const Address& address) const; 1005648Sgblack@eecs.umich.edu 1015648Sgblack@eecs.umich.edu int getLatency() const { return m_latency; } 1025648Sgblack@eecs.umich.edu 1035648Sgblack@eecs.umich.edu // Hook for checkpointing the contents of the cache 1045648Sgblack@eecs.umich.edu void recordCacheContents(CacheRecorder& tr) const; 1055648Sgblack@eecs.umich.edu void 1065648Sgblack@eecs.umich.edu setAsInstructionCache(bool is_icache) 1075648Sgblack@eecs.umich.edu { 1085648Sgblack@eecs.umich.edu m_is_instruction_only_cache = is_icache; 1095648Sgblack@eecs.umich.edu } 1105648Sgblack@eecs.umich.edu 1115648Sgblack@eecs.umich.edu // Set this address to most recently used 1125648Sgblack@eecs.umich.edu void setMRU(const Address& address); 1135648Sgblack@eecs.umich.edu 1145648Sgblack@eecs.umich.edu void profileMiss(const RubyRequest & msg); 1155648Sgblack@eecs.umich.edu 1165648Sgblack@eecs.umich.edu void profileGenericRequest(GenericRequestType requestType, 1175648Sgblack@eecs.umich.edu RubyAccessMode accessType, 1185648Sgblack@eecs.umich.edu PrefetchBit pfBit); 1195648Sgblack@eecs.umich.edu 1205648Sgblack@eecs.umich.edu void getMemoryValue(const Address& addr, char* value, 1215648Sgblack@eecs.umich.edu unsigned int size_in_bytes); 1225648Sgblack@eecs.umich.edu void setMemoryValue(const Address& addr, char* value, 1235648Sgblack@eecs.umich.edu unsigned int size_in_bytes); 1245648Sgblack@eecs.umich.edu 1255648Sgblack@eecs.umich.edu void setLocked (const Address& addr, int context); 1265648Sgblack@eecs.umich.edu void clearLocked (const Address& addr); 1275648Sgblack@eecs.umich.edu bool isLocked (const Address& addr, int context); 1285648Sgblack@eecs.umich.edu // Print cache contents 1295648Sgblack@eecs.umich.edu void print(std::ostream& out) const; 1305648Sgblack@eecs.umich.edu void printData(std::ostream& out) const; 1315648Sgblack@eecs.umich.edu 1325648Sgblack@eecs.umich.edu void clearStats() const; 1335648Sgblack@eecs.umich.edu void printStats(std::ostream& out) const; 1345648Sgblack@eecs.umich.edu 1355648Sgblack@eecs.umich.edu private: 1365648Sgblack@eecs.umich.edu // convert a Address to its location in the cache 1375648Sgblack@eecs.umich.edu Index addressToCacheSet(const Address& address) const; 1385648Sgblack@eecs.umich.edu 1395648Sgblack@eecs.umich.edu // Given a cache tag: returns the index of the tag in a set. 1405648Sgblack@eecs.umich.edu // returns -1 if the tag is not found. 1415648Sgblack@eecs.umich.edu int findTagInSet(Index line, const Address& tag) const; 1425648Sgblack@eecs.umich.edu int findTagInSetIgnorePermissions(Index cacheSet, 1435648Sgblack@eecs.umich.edu const Address& tag) const; 1445648Sgblack@eecs.umich.edu 1455648Sgblack@eecs.umich.edu // Private copy constructor and assignment operator 1465648Sgblack@eecs.umich.edu CacheMemory(const CacheMemory& obj); 1475648Sgblack@eecs.umich.edu CacheMemory& operator=(const CacheMemory& obj); 1485648Sgblack@eecs.umich.edu 1495648Sgblack@eecs.umich.edu private: 1505648Sgblack@eecs.umich.edu const std::string m_cache_name; 1515648Sgblack@eecs.umich.edu int m_latency; 1525648Sgblack@eecs.umich.edu 1535648Sgblack@eecs.umich.edu // Data Members (m_prefix) 1545648Sgblack@eecs.umich.edu bool m_is_instruction_only_cache; 1555648Sgblack@eecs.umich.edu bool m_is_data_only_cache; 1565648Sgblack@eecs.umich.edu 1575648Sgblack@eecs.umich.edu // The first index is the # of cache lines. 1585648Sgblack@eecs.umich.edu // The second index is the the amount associativity. 1595648Sgblack@eecs.umich.edu m5::hash_map<Address, int> m_tag_index; 1605648Sgblack@eecs.umich.edu std::vector<std::vector<AbstractCacheEntry*> > m_cache; 1615648Sgblack@eecs.umich.edu 1625648Sgblack@eecs.umich.edu AbstractReplacementPolicy *m_replacementPolicy_ptr; 1635648Sgblack@eecs.umich.edu 1645648Sgblack@eecs.umich.edu CacheProfiler* m_profiler_ptr; 1655648Sgblack@eecs.umich.edu 1665648Sgblack@eecs.umich.edu int m_cache_size; 1675648Sgblack@eecs.umich.edu std::string m_policy; 1685648Sgblack@eecs.umich.edu int m_cache_num_sets; 1695648Sgblack@eecs.umich.edu int m_cache_num_set_bits; 1705648Sgblack@eecs.umich.edu int m_cache_assoc; 1715648Sgblack@eecs.umich.edu int m_start_index_bit; 1725648Sgblack@eecs.umich.edu}; 1735648Sgblack@eecs.umich.edu 1745648Sgblack@eecs.umich.edu#endif // __MEM_RUBY_SYSTEM_CACHEMEMORY_HH__ 1755648Sgblack@eecs.umich.edu 1765648Sgblack@eecs.umich.edu