3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
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.
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.
33 * Description: See DirectoryMemory.hh
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/gems_common/util.hh"
44 int DirectoryMemory::m_num_directories
= 0;
45 int DirectoryMemory::m_num_directories_bits
= 0;
46 uint64_t DirectoryMemory::m_total_size_bytes
= 0;
48 DirectoryMemory::DirectoryMemory(const Params
*p
)
51 m_version
= p
->version
;
52 m_size_bytes
= p
->size_mb
* static_cast<uint64
>(1<<20);
53 m_size_bits
= log_int(m_size_bytes
);
56 void DirectoryMemory::init()
58 m_num_entries
= m_size_bytes
/ RubySystem::getBlockSizeBytes();
59 m_entries
= new Directory_Entry
*[m_num_entries
];
60 for (int i
=0; i
< m_num_entries
; i
++)
63 m_ram
= g_system_ptr
->getMemoryVector();
66 m_num_directories_bits
= log_int(m_num_directories
);
67 m_total_size_bytes
+= m_size_bytes
;
70 DirectoryMemory::~DirectoryMemory()
72 // free up all the directory entries
73 for (uint64 i
=0;i
<m_num_entries
;i
++) {
74 if (m_entries
[i
] != NULL
) {
78 if (m_entries
!= NULL
) {
83 void DirectoryMemory::printConfig(ostream
& out
) const
85 out
<< "DirectoryMemory module config: " << m_name
<< endl
;
86 out
<< " version: " << m_version
<< endl
;
87 out
<< " memory_bits: " << m_size_bits
<< endl
;
88 out
<< " memory_size_bytes: " << m_size_bytes
<< endl
;
89 out
<< " memory_size_Kbytes: " << double(m_size_bytes
) / (1<<10) << endl
;
90 out
<< " memory_size_Mbytes: " << double(m_size_bytes
) / (1<<20) << endl
;
91 out
<< " memory_size_Gbytes: " << double(m_size_bytes
) / (1<<30) << endl
;
95 void DirectoryMemory::printGlobalConfig(ostream
& out
)
97 out
<< "DirectoryMemory Global Config: " << endl
;
98 out
<< " number of directory memories: " << m_num_directories
<< endl
;
99 if (m_num_directories
> 1) {
100 out
<< " number of selection bits: " << m_num_directories_bits
<< endl
;
101 out
<< " selection bits: " << RubySystem::getBlockSizeBits()+m_num_directories_bits
-1
102 << "-" << RubySystem::getBlockSizeBits() << endl
;
104 out
<< " total memory size bytes: " << m_total_size_bytes
<< endl
;
105 out
<< " total memory size bits: " << log_int(m_total_size_bytes
) << endl
;
109 int DirectoryMemory::mapAddressToDirectoryVersion(PhysAddress address
)
111 if (m_num_directories_bits
== 0) return 0;
112 int ret
= address
.bitSelect(RubySystem::getBlockSizeBits(),
113 RubySystem::getBlockSizeBits()+m_num_directories_bits
-1);
118 bool DirectoryMemory::isPresent(PhysAddress address
)
120 bool ret
= (mapAddressToDirectoryVersion(address
) == m_version
);
124 int DirectoryMemory::mapAddressToLocalIdx(PhysAddress address
)
126 int ret
= address
.getAddress() >> (RubySystem::getBlockSizeBits() + m_num_directories_bits
);
130 Directory_Entry
& DirectoryMemory::lookup(PhysAddress address
)
132 assert(isPresent(address
));
133 Directory_Entry
* entry
;
134 int idx
= mapAddressToLocalIdx(address
);
135 entry
= m_entries
[idx
];
137 entry
= new Directory_Entry
;
138 entry
->getDataBlk().assign(m_ram
->getBlockPtr(address
));
139 m_entries
[idx
] = entry
;
144 Directory_Entry& DirectoryMemory::lookup(PhysAddress address)
146 assert(isPresent(address));
147 Index index = address.memoryModuleIndex();
149 if (index < 0 || index > m_size) {
150 WARN_EXPR(address.getAddress());
153 ERROR_MSG("Directory Memory Assertion: accessing memory out of range.");
155 Directory_Entry* entry = m_entries[index];
157 // allocate the directory entry on demand.
159 entry = new Directory_Entry;
160 entry->getDataBlk().assign(m_ram->getBlockPtr(address));
162 // store entry to the table
163 m_entries[index] = entry;
170 void DirectoryMemory::invalidateBlock(PhysAddress address
)
173 assert(isPresent(address));
175 Index index = address.memoryModuleIndex();
177 if (index < 0 || index > m_size) {
178 ERROR_MSG("Directory Memory Assertion: accessing memory out of range.");
181 if(m_entries[index] != NULL){
182 delete m_entries[index];
183 m_entries[index] = NULL;
188 void DirectoryMemory::print(ostream
& out
) const
194 RubyDirectoryMemoryParams::create()
196 return new DirectoryMemory(this);