2 * Copyright (c) 2013,2016-2018 ARM Limited
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.
14 * Copyright (c) 2003-2005 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 * Authors: Erik Hallnor
46 * Definitions a fully associative LRU tagstore.
49 #include "mem/cache/tags/fa_lru.hh"
54 #include "base/intmath.hh"
55 #include "base/logging.hh"
56 #include "mem/cache/base.hh"
58 FALRU::FALRU(const Params
*p
)
61 cacheTracking(p
->min_tracked_cache_size
, size
, blkSize
)
63 if (!isPowerOf2(blkSize
))
64 fatal("cache block size (in bytes) `%d' must be a power of two",
66 if (!isPowerOf2(size
))
67 fatal("Cache Size must be power of 2 for now");
69 blks
= new FALRUBlk
[numBlocks
];
73 head
->next
= &(blks
[1]);
76 head
->data
= &dataBlks
[0];
78 for (unsigned i
= 1; i
< numBlocks
- 1; i
++) {
79 blks
[i
].prev
= &(blks
[i
-1]);
80 blks
[i
].next
= &(blks
[i
+1]);
84 // Associate a data chunk to the block
85 blks
[i
].data
= &dataBlks
[blkSize
*i
];
88 tail
= &(blks
[numBlocks
- 1]);
89 tail
->prev
= &(blks
[numBlocks
- 2]);
92 tail
->way
= numBlocks
- 1;
93 tail
->data
= &dataBlks
[(numBlocks
- 1) * blkSize
];
95 cacheTracking
.init(head
, tail
);
106 BaseTags::regStats();
107 cacheTracking
.regStats(name());
111 FALRU::hashLookup(Addr addr
) const
113 tagIterator iter
= tagHash
.find(addr
);
114 if (iter
!= tagHash
.end()) {
115 return (*iter
).second
;
121 FALRU::invalidate(CacheBlk
*blk
)
123 BaseTags::invalidate(blk
);
125 // Decrease the number of tags in use
128 // Move the block to the tail to make it the next victim
129 moveToTail((FALRUBlk
*)blk
);
131 // Erase block entry in the hash table
132 tagHash
.erase(blk
->tag
);
136 FALRU::accessBlock(Addr addr
, bool is_secure
, Cycles
&lat
)
138 return accessBlock(addr
, is_secure
, lat
, 0);
142 FALRU::accessBlock(Addr addr
, bool is_secure
, Cycles
&lat
,
143 CachesMask
*in_caches_mask
)
146 FALRUBlk
* blk
= static_cast<FALRUBlk
*>(findBlock(addr
, is_secure
));
148 if (blk
!= nullptr) {
151 // Check if the block to be accessed is available. If not,
152 // apply the accessLatency on top of block->whenReady.
153 if (blk
->whenReady
> curTick() &&
154 cache
->ticksToCycles(blk
->whenReady
- curTick()) >
156 lat
= cache
->ticksToCycles(blk
->whenReady
- curTick()) +
159 mask
= blk
->inCachesMask
;
166 if (in_caches_mask
) {
167 *in_caches_mask
= mask
;
170 cacheTracking
.recordAccess(blk
);
176 FALRU::findBlock(Addr addr
, bool is_secure
) const
178 Addr tag
= extractTag(addr
);
179 FALRUBlk
* blk
= hashLookup(tag
);
181 if (blk
&& blk
->isValid()) {
182 assert(blk
->tag
== tag
);
183 assert(blk
->isSecure() == is_secure
);
191 FALRU::findBlockBySetAndWay(int set
, int way
) const
198 FALRU::findVictim(Addr addr
, const bool is_secure
,
199 std::vector
<CacheBlk
*>& evict_blks
) const
201 // The victim is always stored on the tail for the FALRU
202 FALRUBlk
* victim
= tail
;
204 // There is only one eviction for this replacement
205 evict_blks
.push_back(victim
);
211 FALRU::insertBlock(const PacketPtr pkt
, CacheBlk
*blk
)
213 FALRUBlk
* falruBlk
= static_cast<FALRUBlk
*>(blk
);
215 // Make sure block is not present in the cache
216 assert(falruBlk
->inCachesMask
== 0);
218 // Do common block insertion functionality
219 BaseTags::insertBlock(pkt
, blk
);
221 // Increment tag counter
224 // New block is the MRU
225 moveToHead(falruBlk
);
227 // Insert new block in the hash table
228 tagHash
[falruBlk
->tag
] = falruBlk
;
232 FALRU::moveToHead(FALRUBlk
*blk
)
234 // If block is not already head, do the moving
236 cacheTracking
.moveBlockToHead(blk
);
237 // If block is tail, set previous block as new tail
239 assert(blk
->next
== nullptr);
241 tail
->next
= nullptr;
242 // Inform block's surrounding blocks that it has been moved
244 blk
->prev
->next
= blk
->next
;
245 blk
->next
->prev
= blk
->prev
;
254 cacheTracking
.check(head
, tail
);
259 FALRU::moveToTail(FALRUBlk
*blk
)
261 // If block is not already tail, do the moving
263 cacheTracking
.moveBlockToTail(blk
);
264 // If block is head, set next block as new head
266 assert(blk
->prev
== nullptr);
268 head
->prev
= nullptr;
269 // Inform block's surrounding blocks that it has been moved
271 blk
->prev
->next
= blk
->next
;
272 blk
->next
->prev
= blk
->prev
;
281 cacheTracking
.check(head
, tail
);
286 FALRUParams::create()
288 return new FALRU(this);
292 FALRU::CacheTracking::check(FALRUBlk
*head
, FALRUBlk
*tail
)
295 FALRUBlk
* blk
= head
;
296 unsigned curr_size
= 0;
297 unsigned tracked_cache_size
= minTrackedSize
;
298 CachesMask in_caches_mask
= inAllCachesMask
;
302 panic_if(blk
->inCachesMask
!= in_caches_mask
, "Expected cache mask "
303 "%x found %x", blk
->inCachesMask
, in_caches_mask
);
305 curr_size
+= blkSize
;
306 if (curr_size
== tracked_cache_size
&& blk
!= tail
) {
307 panic_if(boundaries
[j
] != blk
, "Unexpected boundary for the %d-th "
309 tracked_cache_size
<<= 1;
310 // from this point, blocks fit only in the larger caches
311 in_caches_mask
&= ~(1U << j
);
316 #endif // FALRU_DEBUG
320 FALRU::CacheTracking::init(FALRUBlk
*head
, FALRUBlk
*tail
)
322 // early exit if we are not tracking any extra caches
323 FALRUBlk
* blk
= numTrackedCaches
? head
: nullptr;
324 unsigned curr_size
= 0;
325 unsigned tracked_cache_size
= minTrackedSize
;
326 CachesMask in_caches_mask
= inAllCachesMask
;
330 blk
->inCachesMask
= in_caches_mask
;
332 curr_size
+= blkSize
;
333 if (curr_size
== tracked_cache_size
&& blk
!= tail
) {
336 tracked_cache_size
<<= 1;
337 // from this point, blocks fit only in the larger caches
338 in_caches_mask
&= ~(1U << j
);
347 FALRU::CacheTracking::moveBlockToHead(FALRUBlk
*blk
)
349 // Get the mask of all caches, in which the block didn't fit
350 // before moving it to the head
351 CachesMask update_caches_mask
= inAllCachesMask
^ blk
->inCachesMask
;
353 for (int i
= 0; i
< numTrackedCaches
; i
++) {
354 CachesMask current_cache_mask
= 1U << i
;
355 if (current_cache_mask
& update_caches_mask
) {
356 // if the ith cache didn't fit the block (before it is moved to
357 // the head), move the ith boundary 1 block closer to the
359 boundaries
[i
]->inCachesMask
&= ~current_cache_mask
;
360 boundaries
[i
] = boundaries
[i
]->prev
;
361 } else if (boundaries
[i
] == blk
) {
362 // Make sure the boundary doesn't point to the block
363 // we are about to move
364 boundaries
[i
] = blk
->prev
;
368 // Make block reside in all caches
369 blk
->inCachesMask
= inAllCachesMask
;
373 FALRU::CacheTracking::moveBlockToTail(FALRUBlk
*blk
)
375 CachesMask update_caches_mask
= blk
->inCachesMask
;
377 for (int i
= 0; i
< numTrackedCaches
; i
++) {
378 CachesMask current_cache_mask
= 1U << i
;
379 if (current_cache_mask
& update_caches_mask
) {
380 // if the ith cache fitted the block (before it is moved to
381 // the tail), move the ith boundary 1 block closer to the
383 boundaries
[i
] = boundaries
[i
]->next
;
384 if (boundaries
[i
] == blk
) {
385 // Make sure the boundary doesn't point to the block
386 // we are about to move
387 boundaries
[i
] = blk
->next
;
389 boundaries
[i
]->inCachesMask
|= current_cache_mask
;
393 // The block now fits only in the actual cache
394 blk
->inCachesMask
= 0;
398 FALRU::CacheTracking::recordAccess(FALRUBlk
*blk
)
400 for (int i
= 0; i
< numTrackedCaches
; i
++) {
401 if (blk
&& ((1U << i
) & blk
->inCachesMask
)) {
408 // Record stats for the actual cache too
410 hits
[numTrackedCaches
]++;
412 misses
[numTrackedCaches
]++;
419 printSize(std::ostream
&stream
, size_t size
)
421 static const char *SIZES
[] = { "B", "kB", "MB", "GB", "TB", "ZB" };
423 while (size
>= 1024 && div
< (sizeof SIZES
/ sizeof *SIZES
)) {
427 stream
<< size
<< SIZES
[div
];
431 FALRU::CacheTracking::regStats(std::string name
)
434 .init(numTrackedCaches
+ 1)
435 .name(name
+ ".falru_hits")
436 .desc("The number of hits in each cache size.")
439 .init(numTrackedCaches
+ 1)
440 .name(name
+ ".falru_misses")
441 .desc("The number of misses in each cache size.")
444 .name(name
+ ".falru_accesses")
445 .desc("The number of accesses to the FA LRU cache.")
448 for (unsigned i
= 0; i
< numTrackedCaches
+ 1; ++i
) {
449 std::stringstream size_str
;
450 printSize(size_str
, minTrackedSize
<< i
);
451 hits
.subname(i
, size_str
.str());
452 hits
.subdesc(i
, "Hits in a " + size_str
.str() + " cache");
453 misses
.subname(i
, size_str
.str());
454 misses
.subdesc(i
, "Misses in a " + size_str
.str() + " cache");