misc: Delete the now unnecessary create methods.
[gem5.git] / src / mem / ruby / structures / DirectoryMemory.cc
1 /*
2 * Copyright (c) 2017,2019 ARM Limited
3 * All rights reserved.
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
15 * Copyright (c) 2017 Google Inc.
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42 #include "mem/ruby/structures/DirectoryMemory.hh"
43
44 #include "base/addr_range.hh"
45 #include "base/intmath.hh"
46 #include "debug/RubyCache.hh"
47 #include "debug/RubyStats.hh"
48 #include "mem/ruby/slicc_interface/RubySlicc_Util.hh"
49 #include "mem/ruby/system/RubySystem.hh"
50 #include "sim/system.hh"
51
52 using namespace std;
53
54 DirectoryMemory::DirectoryMemory(const Params &p)
55 : SimObject(p), addrRanges(p.addr_ranges.begin(), p.addr_ranges.end())
56 {
57 m_size_bytes = 0;
58 for (const auto &r: addrRanges) {
59 m_size_bytes += r.size();
60 }
61 m_size_bits = floorLog2(m_size_bytes);
62 m_num_entries = 0;
63 }
64
65 void
66 DirectoryMemory::init()
67 {
68 m_num_entries = m_size_bytes / RubySystem::getBlockSizeBytes();
69 m_entries = new AbstractCacheEntry*[m_num_entries];
70 for (int i = 0; i < m_num_entries; i++)
71 m_entries[i] = NULL;
72 }
73
74 DirectoryMemory::~DirectoryMemory()
75 {
76 // free up all the directory entries
77 for (uint64_t i = 0; i < m_num_entries; i++) {
78 if (m_entries[i] != NULL) {
79 delete m_entries[i];
80 }
81 }
82 delete [] m_entries;
83 }
84
85 bool
86 DirectoryMemory::isPresent(Addr address)
87 {
88 for (const auto& r: addrRanges) {
89 if (r.contains(address)) {
90 return true;
91 }
92 }
93 return false;
94 }
95
96 uint64_t
97 DirectoryMemory::mapAddressToLocalIdx(Addr address)
98 {
99 uint64_t ret = 0;
100 for (const auto& r: addrRanges) {
101 if (r.contains(address)) {
102 ret += r.getOffset(address);
103 break;
104 }
105 ret += r.size();
106 }
107 return ret >> RubySystem::getBlockSizeBits();
108 }
109
110 AbstractCacheEntry*
111 DirectoryMemory::lookup(Addr address)
112 {
113 assert(isPresent(address));
114 DPRINTF(RubyCache, "Looking up address: %#x\n", address);
115
116 uint64_t idx = mapAddressToLocalIdx(address);
117 assert(idx < m_num_entries);
118 return m_entries[idx];
119 }
120
121 AbstractCacheEntry*
122 DirectoryMemory::allocate(Addr address, AbstractCacheEntry *entry)
123 {
124 assert(isPresent(address));
125 uint64_t idx;
126 DPRINTF(RubyCache, "Looking up address: %#x\n", address);
127
128 idx = mapAddressToLocalIdx(address);
129 assert(idx < m_num_entries);
130 assert(m_entries[idx] == NULL);
131 entry->changePermission(AccessPermission_Read_Only);
132 m_entries[idx] = entry;
133
134 return entry;
135 }
136
137 void
138 DirectoryMemory::deallocate(Addr address)
139 {
140 assert(isPresent(address));
141 uint64_t idx;
142 DPRINTF(RubyCache, "Removing entry for address: %#x\n", address);
143
144 idx = mapAddressToLocalIdx(address);
145 assert(idx < m_num_entries);
146 assert(m_entries[idx] != NULL);
147 delete m_entries[idx];
148 m_entries[idx] = NULL;
149 }
150
151 void
152 DirectoryMemory::print(ostream& out) const
153 {
154 }
155
156 void
157 DirectoryMemory::recordRequestType(DirectoryRequestType requestType) {
158 DPRINTF(RubyStats, "Recorded statistic: %s\n",
159 DirectoryRequestType_to_string(requestType));
160 }