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/ruby/config/RubyConfig.hh"
43 #include "mem/ruby/slicc_interface/AbstractController.hh"
44 #include "mem/gems_common/util.hh"
46 int DirectoryMemory::m_num_directories
= 0;
47 int DirectoryMemory::m_num_directories_bits
= 0;
48 int DirectoryMemory::m_total_size_bytes
= 0;
50 DirectoryMemory::DirectoryMemory(const string
& name
)
55 void DirectoryMemory::init(const vector
<string
> & argv
)
58 for (vector
<string
>::const_iterator it
= argv
.begin(); it
!= argv
.end(); it
++) {
59 if ( (*it
) == "version" )
60 m_version
= atoi( (*(++it
)).c_str() );
61 else if ( (*it
) == "size_mb" ) {
62 m_size_bytes
= atoi((*(++it
)).c_str()) * (1<<20);
63 m_size_bits
= log_int(m_size_bytes
);
64 } else if ( (*it
) == "controller" ) {
65 m_controller
= RubySystem::getController((*(++it
)));
69 assert(m_controller
!= NULL
);
71 m_num_entries
= m_size_bytes
/ RubySystem::getBlockSizeBytes();
72 m_entries
= new Directory_Entry
*[m_num_entries
];
73 for (int i
=0; i
< m_num_entries
; i
++)
76 m_ram
= g_system_ptr
->getMemoryVector();
79 m_num_directories_bits
= log_int(m_num_directories
);
80 m_total_size_bytes
+= m_size_bytes
;
83 DirectoryMemory::~DirectoryMemory()
85 // free up all the directory entries
86 for (int i
=0;i
<m_num_entries
;i
++)
87 if (m_entries
[i
] != NULL
)
89 if (m_entries
!= NULL
)
93 void DirectoryMemory::printConfig(ostream
& out
) const
95 out
<< "DirectoryMemory module config: " << m_name
<< endl
;
96 out
<< " controller: " << m_controller
->getName() << endl
;
97 out
<< " version: " << m_version
<< endl
;
98 out
<< " memory_bits: " << m_size_bits
<< endl
;
99 out
<< " memory_size_bytes: " << m_size_bytes
<< endl
;
100 out
<< " memory_size_Kbytes: " << double(m_size_bytes
) / (1<<10) << endl
;
101 out
<< " memory_size_Mbytes: " << double(m_size_bytes
) / (1<<20) << endl
;
102 out
<< " memory_size_Gbytes: " << double(m_size_bytes
) / (1<<30) << endl
;
106 void DirectoryMemory::printGlobalConfig(ostream
& out
)
108 out
<< "DirectoryMemory Global Config: " << endl
;
109 out
<< " number of directory memories: " << m_num_directories
<< endl
;
110 if (m_num_directories
> 1) {
111 out
<< " number of selection bits: " << m_num_directories_bits
<< endl
;
112 out
<< " selection bits: " << RubySystem::getBlockSizeBits()+m_num_directories_bits
-1
113 << "-" << RubySystem::getBlockSizeBits() << endl
;
115 out
<< " total memory size bytes: " << m_total_size_bytes
<< endl
;
116 out
<< " total memory size bits: " << log_int(m_total_size_bytes
) << endl
;
120 int DirectoryMemory::mapAddressToDirectoryVersion(PhysAddress address
)
122 if (m_num_directories_bits
== 0) return 0;
123 int ret
= address
.bitSelect(RubySystem::getBlockSizeBits(),
124 RubySystem::getBlockSizeBits()+m_num_directories_bits
-1);
129 bool DirectoryMemory::isPresent(PhysAddress address
)
131 bool ret
= (mapAddressToDirectoryVersion(address
) == m_version
);
135 int DirectoryMemory::mapAddressToLocalIdx(PhysAddress address
)
137 int ret
= address
.getAddress() >> (RubySystem::getBlockSizeBits() + m_num_directories_bits
);
141 Directory_Entry
& DirectoryMemory::lookup(PhysAddress address
)
143 assert(isPresent(address
));
144 Directory_Entry
* entry
;
145 int idx
= mapAddressToLocalIdx(address
);
146 entry
= m_entries
[idx
];
148 entry
= new Directory_Entry
;
149 entry
->getDataBlk().assign(m_ram
->getBlockPtr(address
));
150 m_entries
[idx
] = entry
;
155 Directory_Entry& DirectoryMemory::lookup(PhysAddress address)
157 assert(isPresent(address));
158 Index index = address.memoryModuleIndex();
160 if (index < 0 || index > m_size) {
161 WARN_EXPR(address.getAddress());
164 ERROR_MSG("Directory Memory Assertion: accessing memory out of range.");
166 Directory_Entry* entry = m_entries[index];
168 // allocate the directory entry on demand.
170 entry = new Directory_Entry;
171 entry->getDataBlk().assign(m_ram->getBlockPtr(address));
173 // store entry to the table
174 m_entries[index] = entry;
181 void DirectoryMemory::invalidateBlock(PhysAddress address
)
184 assert(isPresent(address));
186 Index index = address.memoryModuleIndex();
188 if (index < 0 || index > m_size) {
189 ERROR_MSG("Directory Memory Assertion: accessing memory out of range.");
192 if(m_entries[index] != NULL){
193 delete m_entries[index];
194 m_entries[index] = NULL;
199 void DirectoryMemory::print(ostream
& out
) const