CacheMemory.hh revision 8683
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 "mem/protocol/GenericRequestType.hh"
3812854Sgabeblack@google.com#include "mem/protocol/RubyRequest.hh"
3912854Sgabeblack@google.com#include "mem/ruby/common/DataBlock.hh"
4012854Sgabeblack@google.com#include "mem/ruby/profiler/CacheProfiler.hh"
4112854Sgabeblack@google.com#include "mem/ruby/recorder/CacheRecorder.hh"
4212854Sgabeblack@google.com#include "mem/ruby/slicc_interface/AbstractCacheEntry.hh"
4312854Sgabeblack@google.com#include "mem/ruby/slicc_interface/RubySlicc_ComponentMapping.hh"
4412854Sgabeblack@google.com#include "mem/ruby/system/LRUPolicy.hh"
4512854Sgabeblack@google.com#include "mem/ruby/system/PseudoLRUPolicy.hh"
4612854Sgabeblack@google.com#include "params/RubyCache.hh"
4712854Sgabeblack@google.com#include "sim/sim_object.hh"
4812854Sgabeblack@google.com
4912854Sgabeblack@google.comclass CacheMemory : public SimObject
5012854Sgabeblack@google.com{
5112854Sgabeblack@google.com  public:
5212854Sgabeblack@google.com    typedef RubyCacheParams Params;
5312854Sgabeblack@google.com    CacheMemory(const Params *p);
5412854Sgabeblack@google.com    ~CacheMemory();
5512854Sgabeblack@google.com
5612854Sgabeblack@google.com    void init();
5712854Sgabeblack@google.com
5812854Sgabeblack@google.com    // Public Methods
5912854Sgabeblack@google.com    void printConfig(std::ostream& out);
6012854Sgabeblack@google.com
6112854Sgabeblack@google.com    // perform a cache access and see if we hit or not.  Return true on a hit.
6212854Sgabeblack@google.com    bool tryCacheAccess(const Address& address, RubyRequestType type,
6312854Sgabeblack@google.com                        DataBlock*& data_ptr);
6412854Sgabeblack@google.com
6512854Sgabeblack@google.com    // similar to above, but doesn't require full access check
6612854Sgabeblack@google.com    bool testCacheAccess(const Address& address, RubyRequestType type,
6712854Sgabeblack@google.com                         DataBlock*& data_ptr);
6812854Sgabeblack@google.com
6912854Sgabeblack@google.com    // tests to see if an address is present in the cache
7012854Sgabeblack@google.com    bool isTagPresent(const Address& address) const;
7112854Sgabeblack@google.com
7212854Sgabeblack@google.com    // Returns true if there is:
7312854Sgabeblack@google.com    //   a) a tag match on this address or there is
7412854Sgabeblack@google.com    //   b) an unused line in the same cache "way"
7512854Sgabeblack@google.com    bool cacheAvail(const Address& address) const;
7612854Sgabeblack@google.com
7712854Sgabeblack@google.com    // find an unused entry and sets the tag appropriate for the address
7812854Sgabeblack@google.com    AbstractCacheEntry* allocate(const Address& address, AbstractCacheEntry* new_entry);
7912854Sgabeblack@google.com    void allocateVoid(const Address& address, AbstractCacheEntry* new_entry)
8012854Sgabeblack@google.com    {
8112854Sgabeblack@google.com        allocate(address, new_entry);
8212854Sgabeblack@google.com    }
8312854Sgabeblack@google.com
8412854Sgabeblack@google.com    // Explicitly free up this address
8512854Sgabeblack@google.com    void deallocate(const Address& address);
8612854Sgabeblack@google.com
8712854Sgabeblack@google.com    // Returns with the physical address of the conflicting cache line
8812854Sgabeblack@google.com    Address cacheProbe(const Address& address) const;
8912854Sgabeblack@google.com
9012854Sgabeblack@google.com    // looks an address up in the cache
9112854Sgabeblack@google.com    AbstractCacheEntry* lookup(const Address& address);
9212854Sgabeblack@google.com    const AbstractCacheEntry* lookup(const Address& address) const;
9312854Sgabeblack@google.com
9412854Sgabeblack@google.com    int getLatency() const { return m_latency; }
9512854Sgabeblack@google.com
9612854Sgabeblack@google.com    // Hook for checkpointing the contents of the cache
9712854Sgabeblack@google.com    void recordCacheContents(int cntrl, CacheRecorder* tr) const;
9812854Sgabeblack@google.com
9912854Sgabeblack@google.com    // Set this address to most recently used
10012854Sgabeblack@google.com    void setMRU(const Address& address);
10112854Sgabeblack@google.com
10212854Sgabeblack@google.com    void profileMiss(const RubyRequest & msg);
10312854Sgabeblack@google.com
10412854Sgabeblack@google.com    void profileGenericRequest(GenericRequestType requestType,
10512854Sgabeblack@google.com                               RubyAccessMode accessType,
10612854Sgabeblack@google.com                               PrefetchBit pfBit);
10712854Sgabeblack@google.com
10812854Sgabeblack@google.com    void setLocked (const Address& addr, int context);
10912854Sgabeblack@google.com    void clearLocked (const Address& addr);
11012854Sgabeblack@google.com    bool isLocked (const Address& addr, int context);
11112854Sgabeblack@google.com    // Print cache contents
11212854Sgabeblack@google.com    void print(std::ostream& out) const;
11312854Sgabeblack@google.com    void printData(std::ostream& out) const;
11412854Sgabeblack@google.com
11512854Sgabeblack@google.com    void clearStats() const;
11612854Sgabeblack@google.com    void printStats(std::ostream& out) const;
11712854Sgabeblack@google.com
11812854Sgabeblack@google.com  private:
11912854Sgabeblack@google.com    // convert a Address to its location in the cache
12012854Sgabeblack@google.com    Index addressToCacheSet(const Address& address) const;
12112854Sgabeblack@google.com
12212854Sgabeblack@google.com    // Given a cache tag: returns the index of the tag in a set.
12312854Sgabeblack@google.com    // returns -1 if the tag is not found.
12412854Sgabeblack@google.com    int findTagInSet(Index line, const Address& tag) const;
12512854Sgabeblack@google.com    int findTagInSetIgnorePermissions(Index cacheSet,
12612854Sgabeblack@google.com                                      const Address& tag) const;
12712854Sgabeblack@google.com
12812854Sgabeblack@google.com    // Private copy constructor and assignment operator
12912854Sgabeblack@google.com    CacheMemory(const CacheMemory& obj);
13012854Sgabeblack@google.com    CacheMemory& operator=(const CacheMemory& obj);
13112854Sgabeblack@google.com
13212854Sgabeblack@google.com  private:
13312854Sgabeblack@google.com    const std::string m_cache_name;
13412854Sgabeblack@google.com    int m_latency;
13512854Sgabeblack@google.com
13612854Sgabeblack@google.com    // Data Members (m_prefix)
13712854Sgabeblack@google.com    bool m_is_instruction_only_cache;
13812854Sgabeblack@google.com
13912854Sgabeblack@google.com    // The first index is the # of cache lines.
14012854Sgabeblack@google.com    // The second index is the the amount associativity.
14112854Sgabeblack@google.com    m5::hash_map<Address, int> m_tag_index;
14212854Sgabeblack@google.com    std::vector<std::vector<AbstractCacheEntry*> > m_cache;
14312854Sgabeblack@google.com
14412854Sgabeblack@google.com    AbstractReplacementPolicy *m_replacementPolicy_ptr;
14512854Sgabeblack@google.com
14612854Sgabeblack@google.com    CacheProfiler* m_profiler_ptr;
14712854Sgabeblack@google.com
14812854Sgabeblack@google.com    int m_cache_size;
14912854Sgabeblack@google.com    std::string m_policy;
15012854Sgabeblack@google.com    int m_cache_num_sets;
15112854Sgabeblack@google.com    int m_cache_num_set_bits;
15212854Sgabeblack@google.com    int m_cache_assoc;
15312854Sgabeblack@google.com    int m_start_index_bit;
15412854Sgabeblack@google.com};
15512854Sgabeblack@google.com
15612854Sgabeblack@google.com#endif // __MEM_RUBY_SYSTEM_CACHEMEMORY_HH__
15712854Sgabeblack@google.com
15812854Sgabeblack@google.com