DirectoryMemory.cc revision 6876
1
2/*
3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/*
31 * DirectoryMemory.cc
32 *
33 * Description: See DirectoryMemory.hh
34 *
35 * $Id$
36 *
37 */
38
39#include "mem/ruby/system/System.hh"
40#include "mem/ruby/system/DirectoryMemory.hh"
41#include "mem/ruby/slicc_interface/RubySlicc_Util.hh"
42#include "mem/ruby/slicc_interface/AbstractController.hh"
43#include "mem/gems_common/util.hh"
44
45int DirectoryMemory::m_num_directories = 0;
46int DirectoryMemory::m_num_directories_bits = 0;
47uint64_t DirectoryMemory::m_total_size_bytes = 0;
48
49DirectoryMemory::DirectoryMemory(const Params *p)
50    : SimObject(p)
51{
52    m_version = p->version;
53    m_size_bytes = p->size_mb * static_cast<uint64>(1<<20);
54    m_size_bits = log_int(m_size_bytes);
55    m_controller = p->controller;
56}
57
58void DirectoryMemory::init()
59{
60  m_num_entries = m_size_bytes / RubySystem::getBlockSizeBytes();
61  m_entries = new Directory_Entry*[m_num_entries];
62  for (int i=0; i < m_num_entries; i++)
63    m_entries[i] = NULL;
64
65  m_ram = g_system_ptr->getMemoryVector();
66
67  m_num_directories++;
68  m_num_directories_bits = log_int(m_num_directories);
69  m_total_size_bytes += m_size_bytes;
70}
71
72DirectoryMemory::~DirectoryMemory()
73{
74  // free up all the directory entries
75  for (uint64 i=0;i<m_num_entries;i++) {
76    if (m_entries[i] != NULL) {
77      delete m_entries[i];
78    }
79  }
80  if (m_entries != NULL) {
81    delete [] m_entries;
82  }
83}
84
85void DirectoryMemory::printConfig(ostream& out) const
86{
87  out << "DirectoryMemory module config: " << m_name << endl;
88  out << "  controller: " << m_controller->getName() << endl;
89  out << "  version: " << m_version << endl;
90  out << "  memory_bits: " << m_size_bits << endl;
91  out << "  memory_size_bytes: " << m_size_bytes << endl;
92  out << "  memory_size_Kbytes: " << double(m_size_bytes) / (1<<10) << endl;
93  out << "  memory_size_Mbytes: " << double(m_size_bytes) / (1<<20) << endl;
94  out << "  memory_size_Gbytes: " << double(m_size_bytes) / (1<<30) << endl;
95}
96
97// Static method
98void DirectoryMemory::printGlobalConfig(ostream & out)
99{
100  out << "DirectoryMemory Global Config: " << endl;
101  out << "  number of directory memories: " << m_num_directories << endl;
102  if (m_num_directories > 1) {
103    out << "  number of selection bits: " << m_num_directories_bits << endl;
104    out << "  selection bits: " << RubySystem::getBlockSizeBits()+m_num_directories_bits-1
105        << "-" << RubySystem::getBlockSizeBits() << endl;
106  }
107  out << "  total memory size bytes: " << m_total_size_bytes << endl;
108  out << "  total memory size bits: " << log_int(m_total_size_bytes) << endl;
109
110}
111
112int DirectoryMemory::mapAddressToDirectoryVersion(PhysAddress address)
113{
114  if (m_num_directories_bits == 0) return 0;
115  int ret = address.bitSelect(RubySystem::getBlockSizeBits(),
116                              RubySystem::getBlockSizeBits()+m_num_directories_bits-1);
117  return ret;
118}
119
120// Public method
121bool DirectoryMemory::isPresent(PhysAddress address)
122{
123  bool ret = (mapAddressToDirectoryVersion(address) == m_version);
124  return ret;
125}
126
127int DirectoryMemory::mapAddressToLocalIdx(PhysAddress address)
128{
129  int ret = address.getAddress() >> (RubySystem::getBlockSizeBits() + m_num_directories_bits);
130  return ret;
131}
132
133Directory_Entry& DirectoryMemory::lookup(PhysAddress address)
134{
135  assert(isPresent(address));
136  Directory_Entry* entry;
137  int idx = mapAddressToLocalIdx(address);
138  entry = m_entries[idx];
139  if (entry == NULL) {
140    entry = new Directory_Entry;
141    entry->getDataBlk().assign(m_ram->getBlockPtr(address));
142    m_entries[idx] = entry;
143  }
144  return (*entry);
145}
146/*
147Directory_Entry& DirectoryMemory::lookup(PhysAddress address)
148{
149  assert(isPresent(address));
150  Index index = address.memoryModuleIndex();
151
152  if (index < 0 || index > m_size) {
153    WARN_EXPR(address.getAddress());
154    WARN_EXPR(index);
155    WARN_EXPR(m_size);
156    ERROR_MSG("Directory Memory Assertion: accessing memory out of range.");
157  }
158  Directory_Entry* entry = m_entries[index];
159
160  // allocate the directory entry on demand.
161  if (entry == NULL) {
162    entry = new Directory_Entry;
163    entry->getDataBlk().assign(m_ram->getBlockPtr(address));
164
165    // store entry to the table
166    m_entries[index] = entry;
167  }
168
169  return (*entry);
170}
171*/
172
173void DirectoryMemory::invalidateBlock(PhysAddress address)
174{
175  /*
176  assert(isPresent(address));
177
178  Index index = address.memoryModuleIndex();
179
180  if (index < 0 || index > m_size) {
181    ERROR_MSG("Directory Memory Assertion: accessing memory out of range.");
182  }
183
184  if(m_entries[index] != NULL){
185    delete m_entries[index];
186    m_entries[index] = NULL;
187  }
188  */
189}
190
191void DirectoryMemory::print(ostream& out) const
192{
193
194}
195
196DirectoryMemory *
197RubyDirectoryMemoryParams::create()
198{
199    return new DirectoryMemory(this);
200}
201