Merge Ruby Stuff
[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.C
32 *
33 * Description: See DirectoryMemory.h
34 *
35 * $Id$
36 *
37 */
38
39 #include "mem/ruby/system/System.hh"
40 #include "mem/ruby/common/Driver.hh"
41 #include "mem/ruby/system/DirectoryMemory.hh"
42 #include "mem/ruby/slicc_interface/RubySlicc_Util.hh"
43 #include "mem/ruby/config/RubyConfig.hh"
44 #include "mem/protocol/Chip.hh"
45
46 DirectoryMemory::DirectoryMemory(Chip* chip_ptr, int version)
47 {
48 m_chip_ptr = chip_ptr;
49 m_version = version;
50 // THIS DOESN'T SEEM TO WORK -- MRM
51 // m_size = RubyConfig::memoryModuleBlocks()/RubyConfig::numberOfDirectory();
52 m_size = RubyConfig::memoryModuleBlocks();
53 assert(m_size > 0);
54 /*********************************************************************
55 // allocates an array of directory entry pointers & sets them to NULL
56 m_entries = new Directory_Entry*[m_size];
57 if (m_entries == NULL) {
58 ERROR_MSG("Directory Memory: unable to allocate memory.");
59 }
60
61 for (int i=0; i < m_size; i++) {
62 m_entries[i] = NULL;
63 }
64 */////////////////////////////////////////////////////////////////////
65 }
66
67 DirectoryMemory::~DirectoryMemory()
68 {
69 /*********************************************************************
70 // free up all the directory entries
71 for (int i=0; i < m_size; i++) {
72 if (m_entries[i] != NULL) {
73 delete m_entries[i];
74 m_entries[i] = NULL;
75 }
76 }
77
78 // free up the array of directory entries
79 delete[] m_entries;
80 *//////////////////////////////////////////////////////////////////////
81 m_entries.clear();
82 }
83
84 // Static method
85 void DirectoryMemory::printConfig(ostream& out)
86 {
87 out << "Memory config:" << endl;
88 out << " memory_bits: " << RubyConfig::memorySizeBits() << endl;
89 out << " memory_size_bytes: " << RubyConfig::memorySizeBytes() << endl;
90 out << " memory_size_Kbytes: " << double(RubyConfig::memorySizeBytes()) / (1<<10) << endl;
91 out << " memory_size_Mbytes: " << double(RubyConfig::memorySizeBytes()) / (1<<20) << endl;
92 out << " memory_size_Gbytes: " << double(RubyConfig::memorySizeBytes()) / (1<<30) << endl;
93
94 out << " module_bits: " << RubyConfig::memoryModuleBits() << endl;
95 out << " module_size_lines: " << RubyConfig::memoryModuleBlocks() << endl;
96 out << " module_size_bytes: " << RubyConfig::memoryModuleBlocks() * RubyConfig::dataBlockBytes() << endl;
97 out << " module_size_Kbytes: " << double(RubyConfig::memoryModuleBlocks() * RubyConfig::dataBlockBytes()) / (1<<10) << endl;
98 out << " module_size_Mbytes: " << double(RubyConfig::memoryModuleBlocks() * RubyConfig::dataBlockBytes()) / (1<<20) << endl;
99 }
100
101 // Public method
102 bool DirectoryMemory::isPresent(PhysAddress address)
103 {
104 return (map_Address_to_DirectoryNode(address) == m_chip_ptr->getID()*RubyConfig::numberOfDirectoryPerChip()+m_version);
105 }
106
107 void DirectoryMemory::readPhysMem(uint64 address, int size, void * data)
108 {
109 }
110
111 Directory_Entry& DirectoryMemory::lookup(PhysAddress address)
112 {
113 assert(isPresent(address));
114 Index index = address.memoryModuleIndex();
115
116 if (index < 0 || index > m_size) {
117 WARN_EXPR(m_chip_ptr->getID());
118 WARN_EXPR(address.getAddress());
119 WARN_EXPR(index);
120 WARN_EXPR(m_size);
121 ERROR_MSG("Directory Memory Assertion: accessing memory out of range.");
122 }
123
124 map<Index, Directory_Entry*>::iterator iter = m_entries.find(index);
125 Directory_Entry* entry = m_entries.find(index)->second;
126
127 // allocate the directory entry on demand.
128 if (iter == m_entries.end()) {
129 entry = new Directory_Entry;
130
131 // entry->getProcOwner() = m_chip_ptr->getID(); // FIXME - This should not be hard coded
132 // entry->getDirOwner() = true; // FIXME - This should not be hard-coded
133
134 // load the data from physicalMemory when first initalizing
135 physical_address_t physAddr = address.getAddress();
136 int8 * dataArray = (int8 * )malloc(RubyConfig::dataBlockBytes() * sizeof(int8));
137 readPhysMem(physAddr, RubyConfig::dataBlockBytes(), dataArray);
138
139 for(int j=0; j < RubyConfig::dataBlockBytes(); j++) {
140 entry->getDataBlk().setByte(j, dataArray[j]);
141 }
142 DEBUG_EXPR(NODE_COMP, MedPrio,entry->getDataBlk());
143 // store entry to the table
144 m_entries.insert(make_pair(index, entry));
145 }
146 return (*entry);
147 }
148
149 /*
150 void DirectoryMemory::invalidateBlock(PhysAddress address)
151 {
152 assert(isPresent(address));
153
154 Index index = address.memoryModuleIndex();
155
156 if (index < 0 || index > m_size) {
157 ERROR_MSG("Directory Memory Assertion: accessing memory out of range.");
158 }
159
160 if(m_entries[index] != NULL){
161 delete m_entries[index];
162 m_entries[index] = NULL;
163 }
164
165 }
166 */
167
168 void DirectoryMemory::print(ostream& out) const
169 {
170 out << "Directory dump: " << endl;
171 for(map<Index, Directory_Entry*>::const_iterator it = m_entries.begin(); it != m_entries.end(); ++it) {
172 out << it->first << ": ";
173 out << *(it->second) << endl;
174 }
175 }
176