merge
[gem5.git] / src / mem / ruby / system / DirectoryMemory.cc
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
45 int DirectoryMemory::m_num_directories = 0;
46 int DirectoryMemory::m_num_directories_bits = 0;
47 uint64_t DirectoryMemory::m_total_size_bytes = 0;
48
49 DirectoryMemory::DirectoryMemory(const string & name)
50 : m_name(name)
51 {
52 }
53
54 void DirectoryMemory::init(const vector<string> & argv)
55 {
56 m_controller = NULL;
57 for (vector<string>::const_iterator it = argv.begin(); it != argv.end(); it++) {
58 if ( (*it) == "version" )
59 m_version = atoi( (*(++it)).c_str() );
60 else if ( (*it) == "size_mb" ) {
61 m_size_bytes = atoi((*(++it)).c_str()) * static_cast<uint64>(1<<20);
62 m_size_bits = log_int(m_size_bytes);
63 } else if ( (*it) == "controller" ) {
64 m_controller = RubySystem::getController((*(++it)));
65 } else {
66 cerr << "DirectoryMemory: Unkown config parameter: " << (*it) << endl;
67 assert(0);
68 }
69 }
70 assert(m_controller != NULL);
71
72 m_num_entries = m_size_bytes / RubySystem::getBlockSizeBytes();
73 m_entries = new Directory_Entry*[m_num_entries];
74 for (int i=0; i < m_num_entries; i++)
75 m_entries[i] = NULL;
76
77 m_ram = g_system_ptr->getMemoryVector();
78
79 m_num_directories++;
80 m_num_directories_bits = log_int(m_num_directories);
81 m_total_size_bytes += m_size_bytes;
82 }
83
84 DirectoryMemory::~DirectoryMemory()
85 {
86 // free up all the directory entries
87 for (uint64 i=0;i<m_num_entries;i++) {
88 if (m_entries[i] != NULL) {
89 delete m_entries[i];
90 }
91 }
92 if (m_entries != NULL) {
93 delete [] m_entries;
94 }
95 }
96
97 void DirectoryMemory::printConfig(ostream& out) const
98 {
99 out << "DirectoryMemory module config: " << m_name << endl;
100 out << " controller: " << m_controller->getName() << endl;
101 out << " version: " << m_version << endl;
102 out << " memory_bits: " << m_size_bits << endl;
103 out << " memory_size_bytes: " << m_size_bytes << endl;
104 out << " memory_size_Kbytes: " << double(m_size_bytes) / (1<<10) << endl;
105 out << " memory_size_Mbytes: " << double(m_size_bytes) / (1<<20) << endl;
106 out << " memory_size_Gbytes: " << double(m_size_bytes) / (1<<30) << endl;
107 }
108
109 // Static method
110 void DirectoryMemory::printGlobalConfig(ostream & out)
111 {
112 out << "DirectoryMemory Global Config: " << endl;
113 out << " number of directory memories: " << m_num_directories << endl;
114 if (m_num_directories > 1) {
115 out << " number of selection bits: " << m_num_directories_bits << endl;
116 out << " selection bits: " << RubySystem::getBlockSizeBits()+m_num_directories_bits-1
117 << "-" << RubySystem::getBlockSizeBits() << endl;
118 }
119 out << " total memory size bytes: " << m_total_size_bytes << endl;
120 out << " total memory size bits: " << log_int(m_total_size_bytes) << endl;
121
122 }
123
124 int DirectoryMemory::mapAddressToDirectoryVersion(PhysAddress address)
125 {
126 if (m_num_directories_bits == 0) return 0;
127 int ret = address.bitSelect(RubySystem::getBlockSizeBits(),
128 RubySystem::getBlockSizeBits()+m_num_directories_bits-1);
129 return ret;
130 }
131
132 // Public method
133 bool DirectoryMemory::isPresent(PhysAddress address)
134 {
135 bool ret = (mapAddressToDirectoryVersion(address) == m_version);
136 return ret;
137 }
138
139 int DirectoryMemory::mapAddressToLocalIdx(PhysAddress address)
140 {
141 int ret = address.getAddress() >> (RubySystem::getBlockSizeBits() + m_num_directories_bits);
142 return ret;
143 }
144
145 Directory_Entry& DirectoryMemory::lookup(PhysAddress address)
146 {
147 assert(isPresent(address));
148 Directory_Entry* entry;
149 int idx = mapAddressToLocalIdx(address);
150 entry = m_entries[idx];
151 if (entry == NULL) {
152 entry = new Directory_Entry;
153 entry->getDataBlk().assign(m_ram->getBlockPtr(address));
154 m_entries[idx] = entry;
155 }
156 return (*entry);
157 }
158 /*
159 Directory_Entry& DirectoryMemory::lookup(PhysAddress address)
160 {
161 assert(isPresent(address));
162 Index index = address.memoryModuleIndex();
163
164 if (index < 0 || index > m_size) {
165 WARN_EXPR(address.getAddress());
166 WARN_EXPR(index);
167 WARN_EXPR(m_size);
168 ERROR_MSG("Directory Memory Assertion: accessing memory out of range.");
169 }
170 Directory_Entry* entry = m_entries[index];
171
172 // allocate the directory entry on demand.
173 if (entry == NULL) {
174 entry = new Directory_Entry;
175 entry->getDataBlk().assign(m_ram->getBlockPtr(address));
176
177 // store entry to the table
178 m_entries[index] = entry;
179 }
180
181 return (*entry);
182 }
183 */
184
185 void DirectoryMemory::invalidateBlock(PhysAddress address)
186 {
187 /*
188 assert(isPresent(address));
189
190 Index index = address.memoryModuleIndex();
191
192 if (index < 0 || index > m_size) {
193 ERROR_MSG("Directory Memory Assertion: accessing memory out of range.");
194 }
195
196 if(m_entries[index] != NULL){
197 delete m_entries[index];
198 m_entries[index] = NULL;
199 }
200 */
201 }
202
203 void DirectoryMemory::print(ostream& out) const
204 {
205
206 }
207