2 * Copyright (c) 2013 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
45 * Definitions a fully associative LRU tagstore.
51 #include "base/intmath.hh"
52 #include "base/misc.hh"
53 #include "mem/cache/tags/fa_lru.hh"
57 FALRU::FALRU(const Params
*p
)
58 : BaseTags(p
), cacheBoundaries(nullptr)
60 if (!isPowerOf2(blkSize
))
61 fatal("cache block size (in bytes) `%d' must be a power of two",
63 if (!isPowerOf2(size
))
64 fatal("Cache Size must be power of 2 for now");
66 // Track all cache sizes from 128K up by powers of 2
67 numCaches
= floorLog2(size
) - 17;
69 cacheBoundaries
= new FALRUBlk
*[numCaches
];
70 cacheMask
= (ULL(1) << numCaches
) - 1;
75 warmupBound
= size
/blkSize
;
76 numBlocks
= size
/blkSize
;
78 blks
= new FALRUBlk
[numBlocks
];
80 tail
= &(blks
[numBlocks
-1]);
83 head
->next
= &(blks
[1]);
84 head
->inCache
= cacheMask
;
86 tail
->prev
= &(blks
[numBlocks
-2]);
90 unsigned index
= (1 << 17) / blkSize
;
92 int flags
= cacheMask
;
93 for (unsigned i
= 1; i
< numBlocks
- 1; i
++) {
94 blks
[i
].inCache
= flags
;
96 cacheBoundaries
[j
] = &(blks
[i
]);
101 blks
[i
].prev
= &(blks
[i
-1]);
102 blks
[i
].next
= &(blks
[i
+1]);
103 blks
[i
].isTouched
= false;
107 assert(j
== numCaches
);
108 assert(index
== numBlocks
);
115 delete[] cacheBoundaries
;
123 using namespace Stats
;
124 BaseTags::regStats();
127 .name(name() + ".falru_hits")
128 .desc("The number of hits in each cache size.")
132 .name(name() + ".falru_misses")
133 .desc("The number of misses in each cache size.")
136 .name(name() + ".falru_accesses")
137 .desc("The number of accesses to the FA LRU cache.")
140 for (unsigned i
= 0; i
<= numCaches
; ++i
) {
141 stringstream size_str
;
143 size_str
<< (1<<(i
+7)) <<"K";
145 size_str
<< (1<<(i
-3)) <<"M";
148 hits
.subname(i
, size_str
.str());
149 hits
.subdesc(i
, "Hits in a " + size_str
.str() +" cache");
150 misses
.subname(i
, size_str
.str());
151 misses
.subdesc(i
, "Misses in a " + size_str
.str() +" cache");
156 FALRU::hashLookup(Addr addr
) const
158 tagIterator iter
= tagHash
.find(addr
);
159 if (iter
!= tagHash
.end()) {
160 return (*iter
).second
;
166 FALRU::invalidate(CacheBlk
*blk
)
173 FALRU::accessBlock(Addr addr
, bool is_secure
, Cycles
&lat
, int context_src
)
175 return accessBlock(addr
, is_secure
, lat
, context_src
, 0);
179 FALRU::accessBlock(Addr addr
, bool is_secure
, Cycles
&lat
, int context_src
,
183 int tmp_in_cache
= 0;
184 Addr blkAddr
= blkAlign(addr
);
185 FALRUBlk
* blk
= hashLookup(blkAddr
);
187 if (blk
&& blk
->isValid()) {
188 assert(blk
->tag
== blkAddr
);
189 tmp_in_cache
= blk
->inCache
;
190 for (unsigned i
= 0; i
< numCaches
; i
++) {
191 if (1<<i
& blk
->inCache
) {
203 for (unsigned i
= 0; i
<= numCaches
; ++i
) {
208 *inCache
= tmp_in_cache
;
218 FALRU::findBlock(Addr addr
, bool is_secure
) const
220 Addr blkAddr
= blkAlign(addr
);
221 FALRUBlk
* blk
= hashLookup(blkAddr
);
223 if (blk
&& blk
->isValid()) {
224 assert(blk
->tag
== blkAddr
);
232 FALRU::findBlockBySetAndWay(int set
, int way
) const
239 FALRU::findVictim(Addr addr
)
241 FALRUBlk
* blk
= tail
;
242 assert(blk
->inCache
== 0);
244 tagHash
.erase(blk
->tag
);
245 tagHash
[blkAlign(addr
)] = blk
;
246 if (blk
->isValid()) {
250 blk
->isTouched
= true;
251 if (!warmedUp
&& tagsInUse
.value() >= warmupBound
) {
253 warmupCycle
= curTick();
261 FALRU::insertBlock(PacketPtr pkt
, CacheBlk
*blk
)
266 FALRU::moveToHead(FALRUBlk
*blk
)
268 int updateMask
= blk
->inCache
^ cacheMask
;
269 for (unsigned i
= 0; i
< numCaches
; i
++){
270 if ((1<<i
) & updateMask
) {
271 cacheBoundaries
[i
]->inCache
&= ~(1<<i
);
272 cacheBoundaries
[i
] = cacheBoundaries
[i
]->prev
;
273 } else if (cacheBoundaries
[i
] == blk
) {
274 cacheBoundaries
[i
] = blk
->prev
;
277 blk
->inCache
= cacheMask
;
280 assert(blk
->next
== nullptr);
282 tail
->next
= nullptr;
284 blk
->prev
->next
= blk
->next
;
285 blk
->next
->prev
= blk
->prev
;
297 FALRUBlk
* blk
= head
;
299 int boundary
= 1<<17;
301 int flags
= cacheMask
;
304 if (blk
->inCache
!= flags
) {
307 if (tot_size
== boundary
&& blk
!= tail
) {
308 if (cacheBoundaries
[j
] != blk
) {
312 boundary
= boundary
<<1;
321 FALRUParams::create()
323 return new FALRU(this);