mem-cache: Forward declare ReplaceableEntry
[gem5.git] / src / mem / cache / tags / sector_tags.cc
1 /*
2 * Copyright (c) 2018 Inria
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Daniel Carvalho
29 */
30
31 /**
32 * @file
33 * Definitions of a base set associative sector tag store.
34 */
35
36 #include "mem/cache/tags/sector_tags.hh"
37
38 #include <cassert>
39 #include <memory>
40 #include <string>
41
42 #include "base/intmath.hh"
43 #include "base/logging.hh"
44 #include "base/types.hh"
45 #include "debug/CacheRepl.hh"
46 #include "mem/cache/base.hh"
47 #include "mem/cache/replacement_policies/base.hh"
48
49 SectorTags::SectorTags(const SectorTagsParams *p)
50 : BaseTags(p), assoc(p->assoc), allocAssoc(p->assoc),
51 sequentialAccess(p->sequential_access),
52 replacementPolicy(p->replacement_policy),
53 numBlocksPerSector(p->num_blocks_per_sector),
54 numSectors(numBlocks / p->num_blocks_per_sector),
55 numSets(numSectors / p->assoc),
56 blks(numBlocks), secBlks(numSectors), sets(numSets),
57 sectorShift(floorLog2(blkSize)),
58 setShift(sectorShift + floorLog2(numBlocksPerSector)),
59 tagShift(setShift + floorLog2(numSets)),
60 sectorMask(numBlocksPerSector - 1), setMask(numSets - 1)
61 {
62 // Check parameters
63 fatal_if(blkSize < 4 || !isPowerOf2(blkSize),
64 "Block size must be at least 4 and a power of 2");
65 fatal_if(!isPowerOf2(numSets),
66 "# of sets must be non-zero and a power of 2");
67 fatal_if(!isPowerOf2(numBlocksPerSector),
68 "# of blocks per sector must be non-zero and a power of 2");
69 fatal_if(assoc <= 0, "associativity must be greater than zero");
70
71 // Initialize all sets
72 unsigned sec_blk_index = 0; // index into sector blks array
73 unsigned blk_index = 0; // index into blks array
74 for (unsigned i = 0; i < numSets; ++i) {
75 sets[i].resize(assoc);
76
77 // Initialize all sectors in this set
78 for (unsigned j = 0; j < assoc; ++j) {
79 // Select block within the set to be linked
80 SectorBlk*& sec_blk = sets[i][j];
81
82 // Locate next cache sector
83 sec_blk = &secBlks[sec_blk_index];
84
85 // Associate a replacement data entry to the sector
86 sec_blk->replacementData = replacementPolicy->instantiateEntry();
87
88 // Initialize all blocks in this sector
89 sec_blk->blks.resize(numBlocksPerSector);
90 for (unsigned k = 0; k < numBlocksPerSector; ++k){
91 // Select block within the set to be linked
92 SectorSubBlk*& blk = sec_blk->blks[k];
93
94 // Locate next cache block
95 blk = &blks[blk_index];
96
97 // Associate a data chunk to the block
98 blk->data = &dataBlks[blkSize*blk_index];
99
100 // Associate sector block to this block
101 blk->setSectorBlock(sec_blk);
102
103 // Associate the sector replacement data to this block
104 blk->replacementData = sec_blk->replacementData;
105
106 // Set its set, way and sector offset
107 blk->set = i;
108 blk->way = j;
109 blk->setSectorOffset(k);
110
111 // Update block index
112 ++blk_index;
113 }
114
115 // Update sector block index
116 ++sec_blk_index;
117 }
118 }
119 }
120
121 void
122 SectorTags::invalidate(CacheBlk *blk)
123 {
124 BaseTags::invalidate(blk);
125
126 // Get block's sector
127 SectorSubBlk* sub_blk = static_cast<SectorSubBlk*>(blk);
128 const SectorBlk* sector_blk = sub_blk->getSectorBlock();
129
130 // When a block in a sector is invalidated, it does not make the tag
131 // invalid automatically, as there might be other blocks in the sector
132 // using it. The tag is invalidated only when there is a single block
133 // in the sector.
134 if (!sector_blk->isValid()) {
135 // Decrease the number of tags in use
136 tagsInUse--;
137
138 // Invalidate replacement data, as we're invalidating the sector
139 replacementPolicy->invalidate(sector_blk->replacementData);
140 }
141 }
142
143 CacheBlk*
144 SectorTags::accessBlock(Addr addr, bool is_secure, Cycles &lat)
145 {
146 CacheBlk *blk = findBlock(addr, is_secure);
147
148 // Access all tags in parallel, hence one in each way. The data side
149 // either accesses all blocks in parallel, or one block sequentially on
150 // a hit. Sequential access with a miss doesn't access data.
151 tagAccesses += allocAssoc;
152 if (sequentialAccess) {
153 if (blk != nullptr) {
154 dataAccesses += 1;
155 }
156 } else {
157 dataAccesses += allocAssoc*numBlocksPerSector;
158 }
159
160 if (blk != nullptr) {
161 // If a cache hit
162 lat = accessLatency;
163 // Check if the block to be accessed is available. If not,
164 // apply the accessLatency on top of block->whenReady.
165 if (blk->whenReady > curTick() &&
166 cache->ticksToCycles(blk->whenReady - curTick()) >
167 accessLatency) {
168 lat = cache->ticksToCycles(blk->whenReady - curTick()) +
169 accessLatency;
170 }
171
172 // Update number of references to accessed block
173 blk->refCount++;
174
175 // Get block's sector
176 SectorSubBlk* sub_blk = static_cast<SectorSubBlk*>(blk);
177 const SectorBlk* sector_blk = sub_blk->getSectorBlock();
178
179 // Update replacement data of accessed block, which is shared with
180 // the whole sector it belongs to
181 replacementPolicy->touch(sector_blk->replacementData);
182 } else {
183 // If a cache miss
184 lat = lookupLatency;
185 }
186
187 return blk;
188 }
189
190 const std::vector<SectorBlk*>
191 SectorTags::getPossibleLocations(Addr addr) const
192 {
193 return sets[extractSet(addr)];
194 }
195
196 void
197 SectorTags::insertBlock(const PacketPtr pkt, CacheBlk *blk)
198 {
199 // Insert block
200 BaseTags::insertBlock(pkt, blk);
201
202 // Get block's sector
203 SectorSubBlk* sub_blk = static_cast<SectorSubBlk*>(blk);
204 const SectorBlk* sector_blk = sub_blk->getSectorBlock();
205
206 // When a block is inserted, the tag is only a newly used tag if the
207 // sector was not previously present in the cache.
208 // This assumes BaseTags::insertBlock does not set the valid bit.
209 if (sector_blk->isValid()) {
210 // An existing entry's replacement data is just updated
211 replacementPolicy->touch(sector_blk->replacementData);
212 } else {
213 // Increment tag counter
214 tagsInUse++;
215
216 // A new entry resets the replacement data
217 replacementPolicy->reset(sector_blk->replacementData);
218 }
219 }
220
221 CacheBlk*
222 SectorTags::findBlock(Addr addr, bool is_secure) const
223 {
224 // Extract sector tag
225 const Addr tag = extractTag(addr);
226
227 // The address can only be mapped to a specific location of a sector
228 // due to sectors being composed of contiguous-address entries
229 const Addr offset = extractSectorOffset(addr);
230
231 // Find all possible sector locations for the given address
232 const std::vector<SectorBlk*> locations = getPossibleLocations(addr);
233
234 // Search for block
235 for (const auto& sector : locations) {
236 auto blk = sector->blks[offset];
237 if (blk->getTag() == tag && blk->isValid() &&
238 blk->isSecure() == is_secure) {
239 return blk;
240 }
241 }
242
243 // Did not find block
244 return nullptr;
245 }
246
247 ReplaceableEntry*
248 SectorTags::findBlockBySetAndWay(int set, int way) const
249 {
250 return sets[set][way];
251 }
252
253 CacheBlk*
254 SectorTags::findVictim(Addr addr, const bool is_secure,
255 std::vector<CacheBlk*>& evict_blks) const
256 {
257 // Get all possible locations of this sector
258 const std::vector<SectorBlk*> sector_locations =
259 getPossibleLocations(addr);
260
261 // Check if the sector this address belongs to has been allocated
262 Addr tag = extractTag(addr);
263 SectorBlk* victim_sector = nullptr;
264 for (const auto& sector : sector_locations){
265 if ((tag == sector->getTag()) && sector->isValid() &&
266 (is_secure == sector->isSecure())){
267 victim_sector = sector;
268 break;
269 }
270 }
271
272 // If the sector is not present
273 if (victim_sector == nullptr){
274 // Choose replacement victim from replacement candidates
275 victim_sector = static_cast<SectorBlk*>(replacementPolicy->getVictim(
276 std::vector<ReplaceableEntry*>(
277 sector_locations.begin(), sector_locations.end())));
278 }
279
280 // Get the location of the victim block within the sector
281 SectorSubBlk* victim = victim_sector->blks[extractSectorOffset(addr)];
282
283 // Get evicted blocks. Blocks are only evicted if the sectors mismatch and
284 // the currently existing sector is valid.
285 if ((tag == victim_sector->getTag()) &&
286 (is_secure == victim_sector->isSecure())){
287 // It would be a hit if victim was valid, and upgrades do not call
288 // findVictim, so it cannot happen
289 assert(!victim->isValid());
290 } else {
291 // The whole sector must be evicted to make room for the new sector
292 for (const auto& blk : victim_sector->blks){
293 evict_blks.push_back(blk);
294 }
295 }
296
297 DPRINTF(CacheRepl, "set %x, way %x, sector offset %x: %s\n",
298 "selecting blk for replacement\n", victim->set, victim->way,
299 victim->getSectorOffset());
300
301 return victim;
302 }
303
304 Addr
305 SectorTags::extractTag(Addr addr) const
306 {
307 return addr >> tagShift;
308 }
309
310 int
311 SectorTags::extractSet(Addr addr) const
312 {
313 return (addr >> setShift) & setMask;
314 }
315
316 int
317 SectorTags::extractSectorOffset(Addr addr) const
318 {
319 return (addr >> sectorShift) & sectorMask;
320 }
321
322 Addr
323 SectorTags::regenerateBlkAddr(const CacheBlk* blk) const
324 {
325 const SectorSubBlk* blk_cast = static_cast<const SectorSubBlk*>(blk);
326 return ((blk_cast->getTag() << tagShift) | ((Addr)blk->set << setShift) |
327 ((Addr)blk_cast->getSectorOffset() << sectorShift));
328 }
329
330 void
331 SectorTags::forEachBlk(std::function<void(CacheBlk &)> visitor)
332 {
333 for (SectorSubBlk& blk : blks) {
334 visitor(blk);
335 }
336 }
337
338 bool
339 SectorTags::anyBlk(std::function<bool(CacheBlk &)> visitor)
340 {
341 for (SectorSubBlk& blk : blks) {
342 if (visitor(blk)) {
343 return true;
344 }
345 }
346 return false;
347 }
348
349 SectorTags *
350 SectorTagsParams::create()
351 {
352 return new SectorTags(this);
353 }