CacheMemory.hh revision 9105
113372Sgabeblack@google.com/*
213372Sgabeblack@google.com * Copyright (c) 1999-2012 Mark D. Hill and David A. Wood
313372Sgabeblack@google.com * All rights reserved.
413372Sgabeblack@google.com *
513372Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
613372Sgabeblack@google.com * modification, are permitted provided that the following conditions are
713372Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
813372Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
913372Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1013372Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1113372Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1213372Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1313372Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1413372Sgabeblack@google.com * this software without specific prior written permission.
1513372Sgabeblack@google.com *
1613372Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1713372Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1813372Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1913372Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2013372Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2113372Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2213372Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2313372Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2413372Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2513372Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2613372Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2713372Sgabeblack@google.com */
2813372Sgabeblack@google.com
2913372Sgabeblack@google.com#ifndef __MEM_RUBY_SYSTEM_CACHEMEMORY_HH__
3013372Sgabeblack@google.com#define __MEM_RUBY_SYSTEM_CACHEMEMORY_HH__
3113372Sgabeblack@google.com
3213372Sgabeblack@google.com#include <iostream>
3313372Sgabeblack@google.com#include <string>
3413372Sgabeblack@google.com#include <vector>
3513372Sgabeblack@google.com
3613372Sgabeblack@google.com#include "base/hashmap.hh"
3713372Sgabeblack@google.com#include "base/statistics.hh"
3813372Sgabeblack@google.com#include "mem/protocol/CacheResourceType.hh"
3913372Sgabeblack@google.com#include "mem/protocol/CacheRequestType.hh"
4013372Sgabeblack@google.com#include "mem/protocol/GenericRequestType.hh"
4113372Sgabeblack@google.com#include "mem/protocol/RubyRequest.hh"
4213372Sgabeblack@google.com#include "mem/ruby/common/DataBlock.hh"
4313372Sgabeblack@google.com#include "mem/ruby/profiler/CacheProfiler.hh"
4413372Sgabeblack@google.com#include "mem/ruby/recorder/CacheRecorder.hh"
4513372Sgabeblack@google.com#include "mem/ruby/slicc_interface/AbstractCacheEntry.hh"
4613372Sgabeblack@google.com#include "mem/ruby/slicc_interface/RubySlicc_ComponentMapping.hh"
4713372Sgabeblack@google.com#include "mem/ruby/system/BankedArray.hh"
4813372Sgabeblack@google.com#include "mem/ruby/system/LRUPolicy.hh"
4913372Sgabeblack@google.com#include "mem/ruby/system/PseudoLRUPolicy.hh"
5013372Sgabeblack@google.com#include "params/RubyCache.hh"
5113372Sgabeblack@google.com#include "sim/sim_object.hh"
5213372Sgabeblack@google.com
5313372Sgabeblack@google.comclass CacheMemory : public SimObject
5413372Sgabeblack@google.com{
5513372Sgabeblack@google.com  public:
5613372Sgabeblack@google.com    typedef RubyCacheParams Params;
5713372Sgabeblack@google.com    CacheMemory(const Params *p);
5813372Sgabeblack@google.com    ~CacheMemory();
5913372Sgabeblack@google.com
6013372Sgabeblack@google.com    void init();
6113372Sgabeblack@google.com
6213372Sgabeblack@google.com    // Public Methods
6313372Sgabeblack@google.com    void printConfig(std::ostream& out);
6413372Sgabeblack@google.com
6513372Sgabeblack@google.com    // perform a cache access and see if we hit or not.  Return true on a hit.
6613372Sgabeblack@google.com    bool tryCacheAccess(const Address& address, RubyRequestType type,
6713372Sgabeblack@google.com                        DataBlock*& data_ptr);
6813372Sgabeblack@google.com
6913372Sgabeblack@google.com    // similar to above, but doesn't require full access check
7013372Sgabeblack@google.com    bool testCacheAccess(const Address& address, RubyRequestType type,
7113372Sgabeblack@google.com                         DataBlock*& data_ptr);
7213372Sgabeblack@google.com
7313372Sgabeblack@google.com    // tests to see if an address is present in the cache
7413372Sgabeblack@google.com    bool isTagPresent(const Address& address) const;
7513372Sgabeblack@google.com
7613372Sgabeblack@google.com    // Returns true if there is:
7713372Sgabeblack@google.com    //   a) a tag match on this address or there is
7813372Sgabeblack@google.com    //   b) an unused line in the same cache "way"
7913372Sgabeblack@google.com    bool cacheAvail(const Address& address) const;
8013372Sgabeblack@google.com
8113372Sgabeblack@google.com    // find an unused entry and sets the tag appropriate for the address
8213372Sgabeblack@google.com    AbstractCacheEntry* allocate(const Address& address, AbstractCacheEntry* new_entry);
8313372Sgabeblack@google.com    void allocateVoid(const Address& address, AbstractCacheEntry* new_entry)
8413372Sgabeblack@google.com    {
8513372Sgabeblack@google.com        allocate(address, new_entry);
8613372Sgabeblack@google.com    }
8713372Sgabeblack@google.com
8813372Sgabeblack@google.com    // Explicitly free up this address
8913372Sgabeblack@google.com    void deallocate(const Address& address);
9013372Sgabeblack@google.com
9113372Sgabeblack@google.com    // Returns with the physical address of the conflicting cache line
9213372Sgabeblack@google.com    Address cacheProbe(const Address& address) const;
9313372Sgabeblack@google.com
9413372Sgabeblack@google.com    // looks an address up in the cache
9513372Sgabeblack@google.com    AbstractCacheEntry* lookup(const Address& address);
9613372Sgabeblack@google.com    const AbstractCacheEntry* lookup(const Address& address) const;
9713372Sgabeblack@google.com
9813372Sgabeblack@google.com    int getLatency() const { return m_latency; }
9913372Sgabeblack@google.com
10013372Sgabeblack@google.com    // Hook for checkpointing the contents of the cache
10113372Sgabeblack@google.com    void recordCacheContents(int cntrl, CacheRecorder* tr) const;
10213372Sgabeblack@google.com
10313372Sgabeblack@google.com    // Set this address to most recently used
10413372Sgabeblack@google.com    void setMRU(const Address& address);
10513372Sgabeblack@google.com
10613372Sgabeblack@google.com    void profileMiss(const RubyRequest & msg);
10713372Sgabeblack@google.com
10813372Sgabeblack@google.com    void profileGenericRequest(GenericRequestType requestType,
10913372Sgabeblack@google.com                               RubyAccessMode accessType,
11013372Sgabeblack@google.com                               PrefetchBit pfBit);
11113372Sgabeblack@google.com
11213372Sgabeblack@google.com    void setLocked (const Address& addr, int context);
11313372Sgabeblack@google.com    void clearLocked (const Address& addr);
11413372Sgabeblack@google.com    bool isLocked (const Address& addr, int context);
11513372Sgabeblack@google.com    // Print cache contents
11613372Sgabeblack@google.com    void print(std::ostream& out) const;
11713372Sgabeblack@google.com    void printData(std::ostream& out) const;
11813372Sgabeblack@google.com
11913372Sgabeblack@google.com    void clearStats() const;
12013372Sgabeblack@google.com    void printStats(std::ostream& out) const;
12113372Sgabeblack@google.com
12213372Sgabeblack@google.com    void recordRequestType(CacheRequestType requestType);
12313372Sgabeblack@google.com    void regStats();
12413372Sgabeblack@google.com
12513372Sgabeblack@google.com    Stats::Scalar numDataArrayReads;
12613372Sgabeblack@google.com    Stats::Scalar numDataArrayWrites;
12713372Sgabeblack@google.com    Stats::Scalar numTagArrayReads;
12813372Sgabeblack@google.com    Stats::Scalar numTagArrayWrites;
12913372Sgabeblack@google.com
13013372Sgabeblack@google.com    bool checkResourceAvailable(CacheResourceType res, Address addr);
13113372Sgabeblack@google.com
13213372Sgabeblack@google.com    Stats::Scalar numTagArrayStalls;
13313372Sgabeblack@google.com    Stats::Scalar numDataArrayStalls;
13413372Sgabeblack@google.com  private:
13513372Sgabeblack@google.com    // convert a Address to its location in the cache
13613372Sgabeblack@google.com    Index addressToCacheSet(const Address& address) const;
13713372Sgabeblack@google.com
13813372Sgabeblack@google.com    // Given a cache tag: returns the index of the tag in a set.
13913372Sgabeblack@google.com    // returns -1 if the tag is not found.
14013372Sgabeblack@google.com    int findTagInSet(Index line, const Address& tag) const;
14113372Sgabeblack@google.com    int findTagInSetIgnorePermissions(Index cacheSet,
14213372Sgabeblack@google.com                                      const Address& tag) const;
14313372Sgabeblack@google.com
14413372Sgabeblack@google.com    // Private copy constructor and assignment operator
14513372Sgabeblack@google.com    CacheMemory(const CacheMemory& obj);
14613372Sgabeblack@google.com    CacheMemory& operator=(const CacheMemory& obj);
14713372Sgabeblack@google.com
14813372Sgabeblack@google.com  private:
14913372Sgabeblack@google.com    const std::string m_cache_name;
15013372Sgabeblack@google.com    int m_latency;
15113372Sgabeblack@google.com
15213372Sgabeblack@google.com    // Data Members (m_prefix)
15313372Sgabeblack@google.com    bool m_is_instruction_only_cache;
15413372Sgabeblack@google.com
15513372Sgabeblack@google.com    // The first index is the # of cache lines.
15613372Sgabeblack@google.com    // The second index is the the amount associativity.
15713372Sgabeblack@google.com    m5::hash_map<Address, int> m_tag_index;
15813372Sgabeblack@google.com    std::vector<std::vector<AbstractCacheEntry*> > m_cache;
15913372Sgabeblack@google.com
16013372Sgabeblack@google.com    AbstractReplacementPolicy *m_replacementPolicy_ptr;
16113372Sgabeblack@google.com
16213372Sgabeblack@google.com    CacheProfiler* m_profiler_ptr;
16313372Sgabeblack@google.com
16413372Sgabeblack@google.com    BankedArray dataArray;
16513372Sgabeblack@google.com    BankedArray tagArray;
16613372Sgabeblack@google.com
16713372Sgabeblack@google.com    int m_cache_size;
16813372Sgabeblack@google.com    std::string m_policy;
16913372Sgabeblack@google.com    int m_cache_num_sets;
17013372Sgabeblack@google.com    int m_cache_num_set_bits;
17113372Sgabeblack@google.com    int m_cache_assoc;
17213372Sgabeblack@google.com    int m_start_index_bit;
17313372Sgabeblack@google.com    bool m_resource_stalls;
17413372Sgabeblack@google.com};
17513372Sgabeblack@google.com
17613372Sgabeblack@google.com#endif // __MEM_RUBY_SYSTEM_CACHEMEMORY_HH__
17713372Sgabeblack@google.com
17813372Sgabeblack@google.com