DirectoryMemory.cc revision 11025
1/*
2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "base/intmath.hh"
30#include "debug/RubyCache.hh"
31#include "debug/RubyStats.hh"
32#include "mem/ruby/slicc_interface/RubySlicc_Util.hh"
33#include "mem/ruby/structures/DirectoryMemory.hh"
34#include "mem/ruby/system/System.hh"
35
36using namespace std;
37
38int DirectoryMemory::m_num_directories = 0;
39int DirectoryMemory::m_num_directories_bits = 0;
40uint64_t DirectoryMemory::m_total_size_bytes = 0;
41int DirectoryMemory::m_numa_high_bit = 0;
42
43DirectoryMemory::DirectoryMemory(const Params *p)
44    : SimObject(p)
45{
46    m_version = p->version;
47    m_size_bytes = p->size;
48    m_size_bits = floorLog2(m_size_bytes);
49    m_num_entries = 0;
50    m_numa_high_bit = p->numa_high_bit;
51}
52
53void
54DirectoryMemory::init()
55{
56    m_num_entries = m_size_bytes / RubySystem::getBlockSizeBytes();
57    m_entries = new AbstractEntry*[m_num_entries];
58    for (int i = 0; i < m_num_entries; i++)
59        m_entries[i] = NULL;
60
61    m_num_directories++;
62    m_num_directories_bits = ceilLog2(m_num_directories);
63    m_total_size_bytes += m_size_bytes;
64
65    if (m_numa_high_bit == 0) {
66        m_numa_high_bit = RubySystem::getMemorySizeBits() - 1;
67    }
68    assert(m_numa_high_bit != 0);
69}
70
71DirectoryMemory::~DirectoryMemory()
72{
73    // free up all the directory entries
74    for (uint64_t i = 0; i < m_num_entries; i++) {
75        if (m_entries[i] != NULL) {
76            delete m_entries[i];
77        }
78    }
79    delete [] m_entries;
80}
81
82uint64_t
83DirectoryMemory::mapAddressToDirectoryVersion(Addr address)
84{
85    if (m_num_directories_bits == 0)
86        return 0;
87
88    uint64_t ret = bitSelect(address,
89                           m_numa_high_bit - m_num_directories_bits + 1,
90                           m_numa_high_bit);
91    return ret;
92}
93
94bool
95DirectoryMemory::isPresent(Addr address)
96{
97    bool ret = (mapAddressToDirectoryVersion(address) == m_version);
98    return ret;
99}
100
101uint64_t
102DirectoryMemory::mapAddressToLocalIdx(Addr address)
103{
104    uint64_t ret;
105    if (m_num_directories_bits > 0) {
106        ret = bitRemove(address, m_numa_high_bit - m_num_directories_bits + 1,
107                        m_numa_high_bit);
108    } else {
109        ret = address;
110    }
111
112    return ret >> (RubySystem::getBlockSizeBits());
113}
114
115AbstractEntry*
116DirectoryMemory::lookup(Addr address)
117{
118    assert(isPresent(address));
119    DPRINTF(RubyCache, "Looking up address: %s\n", address);
120
121    uint64_t idx = mapAddressToLocalIdx(address);
122    assert(idx < m_num_entries);
123    return m_entries[idx];
124}
125
126AbstractEntry*
127DirectoryMemory::allocate(Addr address, AbstractEntry *entry)
128{
129    assert(isPresent(address));
130    uint64_t idx;
131    DPRINTF(RubyCache, "Looking up address: %s\n", address);
132
133    idx = mapAddressToLocalIdx(address);
134    assert(idx < m_num_entries);
135    entry->changePermission(AccessPermission_Read_Only);
136    m_entries[idx] = entry;
137
138    return entry;
139}
140
141void
142DirectoryMemory::print(ostream& out) const
143{
144}
145
146void
147DirectoryMemory::recordRequestType(DirectoryRequestType requestType) {
148    DPRINTF(RubyStats, "Recorded statistic: %s\n",
149            DirectoryRequestType_to_string(requestType));
150}
151
152DirectoryMemory *
153RubyDirectoryMemoryParams::create()
154{
155    return new DirectoryMemory(this);
156}
157