2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
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.
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.
29 #include "base/intmath.hh"
30 #include "debug/RubyCache.hh"
31 #include "mem/ruby/slicc_interface/RubySlicc_Util.hh"
32 #include "mem/ruby/system/DirectoryMemory.hh"
33 #include "mem/ruby/system/System.hh"
37 int DirectoryMemory::m_num_directories
= 0;
38 int DirectoryMemory::m_num_directories_bits
= 0;
39 uint64_t DirectoryMemory::m_total_size_bytes
= 0;
40 int DirectoryMemory::m_numa_high_bit
= 0;
42 DirectoryMemory::DirectoryMemory(const Params
*p
)
45 m_version
= p
->version
;
46 m_size_bytes
= p
->size
;
47 m_size_bits
= floorLog2(m_size_bytes
);
49 m_use_map
= p
->use_map
;
50 m_map_levels
= p
->map_levels
;
51 m_numa_high_bit
= p
->numa_high_bit
;
55 DirectoryMemory::init()
57 m_num_entries
= m_size_bytes
/ RubySystem::getBlockSizeBytes();
60 m_sparseMemory
= new SparseMemory(m_map_levels
);
62 m_entries
= new Directory_Entry
*[m_num_entries
];
63 for (int i
= 0; i
< m_num_entries
; i
++)
65 m_ram
= g_system_ptr
->getMemoryVector();
69 m_num_directories_bits
= floorLog2(m_num_directories
);
70 m_total_size_bytes
+= m_size_bytes
;
72 if (m_numa_high_bit
== 0) {
73 m_numa_high_bit
= RubySystem::getMemorySizeBits() - 1;
75 assert(m_numa_high_bit
!= 0);
78 DirectoryMemory::~DirectoryMemory()
80 // free up all the directory entries
81 if (m_entries
!= NULL
) {
82 for (uint64 i
= 0; i
< m_num_entries
; i
++) {
83 if (m_entries
[i
] != NULL
) {
88 } else if (m_use_map
) {
89 delete m_sparseMemory
;
94 DirectoryMemory::printConfig(ostream
& out
) const
96 out
<< "DirectoryMemory module config: " << m_name
<< endl
97 << " version: " << m_version
<< endl
98 << " memory_bits: " << m_size_bits
<< endl
99 << " memory_size_bytes: " << m_size_bytes
<< endl
100 << " memory_size_Kbytes: " << double(m_size_bytes
) / (1<<10) << endl
101 << " memory_size_Mbytes: " << double(m_size_bytes
) / (1<<20) << endl
102 << " memory_size_Gbytes: " << double(m_size_bytes
) / (1<<30) << endl
;
107 DirectoryMemory::printGlobalConfig(ostream
& out
)
109 out
<< "DirectoryMemory Global Config: " << endl
;
110 out
<< " number of directory memories: " << m_num_directories
<< endl
;
111 if (m_num_directories
> 1) {
112 out
<< " number of selection bits: " << m_num_directories_bits
<< endl
113 << " selection bits: " << m_numa_high_bit
114 << "-" << m_numa_high_bit
-m_num_directories_bits
117 out
<< " total memory size bytes: " << m_total_size_bytes
<< endl
;
118 out
<< " total memory bits: " << floorLog2(m_total_size_bytes
) << endl
;
122 DirectoryMemory::mapAddressToDirectoryVersion(PhysAddress address
)
124 if (m_num_directories_bits
== 0)
127 uint64 ret
= address
.bitSelect(m_numa_high_bit
- m_num_directories_bits
+ 1,
133 DirectoryMemory::isPresent(PhysAddress address
)
135 bool ret
= (mapAddressToDirectoryVersion(address
) == m_version
);
140 DirectoryMemory::mapAddressToLocalIdx(PhysAddress address
)
143 if (m_num_directories_bits
> 0) {
144 ret
= address
.bitRemove(m_numa_high_bit
- m_num_directories_bits
+ 1,
147 ret
= address
.getAddress();
150 return ret
>> (RubySystem::getBlockSizeBits());
154 DirectoryMemory::lookup(PhysAddress address
)
156 assert(isPresent(address
));
157 Directory_Entry
* entry
;
159 DPRINTF(RubyCache
, "Looking up address: %s\n", address
);
162 if (m_sparseMemory
->exist(address
)) {
163 entry
= m_sparseMemory
->lookup(address
);
164 assert(entry
!= NULL
);
166 // Note: SparseMemory internally creates a new Directory Entry
167 m_sparseMemory
->add(address
);
168 entry
= m_sparseMemory
->lookup(address
);
169 entry
->changePermission(AccessPermission_Read_Write
);
172 idx
= mapAddressToLocalIdx(address
);
173 assert(idx
< m_num_entries
);
174 entry
= m_entries
[idx
];
177 entry
= new Directory_Entry();
178 entry
->getDataBlk().assign(m_ram
->getBlockPtr(address
));
179 entry
->changePermission(AccessPermission_Read_Only
);
180 m_entries
[idx
] = entry
;
189 DirectoryMemory::lookup(PhysAddress address
)
191 assert(isPresent(address
));
192 Index index
= address
.memoryModuleIndex();
194 if (index
< 0 || index
> m_size
) {
195 WARN_EXPR(address
.getAddress());
198 ERROR_MSG("Directory Memory Assertion: accessing memory out of range");
200 Directory_Entry
* entry
= m_entries
[index
];
202 // allocate the directory entry on demand.
204 entry
= new Directory_Entry
;
205 entry
->getDataBlk().assign(m_ram
->getBlockPtr(address
));
207 // store entry to the table
208 m_entries
[index
] = entry
;
216 DirectoryMemory::invalidateBlock(PhysAddress address
)
219 assert(m_sparseMemory
->exist(address
));
220 m_sparseMemory
->remove(address
);
224 assert(isPresent(address
));
226 Index index
= address
.memoryModuleIndex();
228 if (index
< 0 || index
> m_size
) {
229 ERROR_MSG("Directory Memory Assertion: "
230 "accessing memory out of range.");
233 if (m_entries
[index
] != NULL
){
234 delete m_entries
[index
];
235 m_entries
[index
] = NULL
;
242 DirectoryMemory::print(ostream
& out
) const
247 DirectoryMemory::printStats(ostream
& out
) const
250 m_sparseMemory
->printStats(out
);
255 RubyDirectoryMemoryParams::create()
257 return new DirectoryMemory(this);