91b1b567939abc384159105534e0af76488af662
2 * Copyright (c) 2013,2016 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.
48 #include "mem/cache/tags/fa_lru.hh"
53 #include "base/intmath.hh"
54 #include "base/misc.hh"
58 FALRU::FALRU(const Params
*p
)
59 : BaseTags(p
), cacheBoundaries(nullptr)
61 if (!isPowerOf2(blkSize
))
62 fatal("cache block size (in bytes) `%d' must be a power of two",
64 if (!isPowerOf2(size
))
65 fatal("Cache Size must be power of 2 for now");
67 // Track all cache sizes from 128K up by powers of 2
68 numCaches
= floorLog2(size
) - 17;
70 cacheBoundaries
= new FALRUBlk
*[numCaches
];
71 cacheMask
= (ULL(1) << numCaches
) - 1;
76 warmupBound
= size
/blkSize
;
77 numBlocks
= size
/blkSize
;
79 blks
= new FALRUBlk
[numBlocks
];
81 tail
= &(blks
[numBlocks
-1]);
84 head
->next
= &(blks
[1]);
85 head
->inCache
= cacheMask
;
87 tail
->prev
= &(blks
[numBlocks
-2]);
91 unsigned index
= (1 << 17) / blkSize
;
93 int flags
= cacheMask
;
94 for (unsigned i
= 1; i
< numBlocks
- 1; i
++) {
95 blks
[i
].inCache
= flags
;
97 cacheBoundaries
[j
] = &(blks
[i
]);
102 blks
[i
].prev
= &(blks
[i
-1]);
103 blks
[i
].next
= &(blks
[i
+1]);
104 blks
[i
].isTouched
= false;
108 assert(j
== numCaches
);
109 assert(index
== numBlocks
);
116 delete[] cacheBoundaries
;
124 using namespace Stats
;
125 BaseTags::regStats();
128 .name(name() + ".falru_hits")
129 .desc("The number of hits in each cache size.")
133 .name(name() + ".falru_misses")
134 .desc("The number of misses in each cache size.")
137 .name(name() + ".falru_accesses")
138 .desc("The number of accesses to the FA LRU cache.")
141 for (unsigned i
= 0; i
<= numCaches
; ++i
) {
142 stringstream size_str
;
144 size_str
<< (1<<(i
+7)) <<"K";
146 size_str
<< (1<<(i
-3)) <<"M";
149 hits
.subname(i
, size_str
.str());
150 hits
.subdesc(i
, "Hits in a " + size_str
.str() +" cache");
151 misses
.subname(i
, size_str
.str());
152 misses
.subdesc(i
, "Misses in a " + size_str
.str() +" cache");
157 FALRU::hashLookup(Addr addr
) const
159 tagIterator iter
= tagHash
.find(addr
);
160 if (iter
!= tagHash
.end()) {
161 return (*iter
).second
;
167 FALRU::invalidate(CacheBlk
*blk
)
174 FALRU::accessBlock(Addr addr
, bool is_secure
, Cycles
&lat
)
176 return accessBlock(addr
, is_secure
, lat
, 0);
180 FALRU::accessBlock(Addr addr
, bool is_secure
, Cycles
&lat
, int *inCache
)
183 int tmp_in_cache
= 0;
184 Addr blkAddr
= blkAlign(addr
);
185 FALRUBlk
* blk
= hashLookup(blkAddr
);
187 if (blk
&& blk
->isValid()) {
190 // Check if the block to be accessed is available. If not,
191 // apply the accessLatency on top of block->whenReady.
192 if (blk
->whenReady
> curTick() &&
193 cache
->ticksToCycles(blk
->whenReady
- curTick()) >
195 lat
= cache
->ticksToCycles(blk
->whenReady
- curTick()) +
198 assert(blk
->tag
== blkAddr
);
199 tmp_in_cache
= blk
->inCache
;
200 for (unsigned i
= 0; i
< numCaches
; i
++) {
201 if (1<<i
& blk
->inCache
) {
215 for (unsigned i
= 0; i
<= numCaches
; ++i
) {
220 *inCache
= tmp_in_cache
;
229 FALRU::findBlock(Addr addr
, bool is_secure
) const
231 Addr blkAddr
= blkAlign(addr
);
232 FALRUBlk
* blk
= hashLookup(blkAddr
);
234 if (blk
&& blk
->isValid()) {
235 assert(blk
->tag
== blkAddr
);
243 FALRU::findBlockBySetAndWay(int set
, int way
) const
250 FALRU::findVictim(Addr addr
)
252 FALRUBlk
* blk
= tail
;
253 assert(blk
->inCache
== 0);
255 tagHash
.erase(blk
->tag
);
256 tagHash
[blkAlign(addr
)] = blk
;
257 if (blk
->isValid()) {
261 blk
->isTouched
= true;
262 if (!warmedUp
&& tagsInUse
.value() >= warmupBound
) {
264 warmupCycle
= curTick();
272 FALRU::insertBlock(PacketPtr pkt
, CacheBlk
*blk
)
277 FALRU::moveToHead(FALRUBlk
*blk
)
279 int updateMask
= blk
->inCache
^ cacheMask
;
280 for (unsigned i
= 0; i
< numCaches
; i
++){
281 if ((1<<i
) & updateMask
) {
282 cacheBoundaries
[i
]->inCache
&= ~(1<<i
);
283 cacheBoundaries
[i
] = cacheBoundaries
[i
]->prev
;
284 } else if (cacheBoundaries
[i
] == blk
) {
285 cacheBoundaries
[i
] = blk
->prev
;
288 blk
->inCache
= cacheMask
;
291 assert(blk
->next
== nullptr);
293 tail
->next
= nullptr;
295 blk
->prev
->next
= blk
->next
;
296 blk
->next
->prev
= blk
->prev
;
308 FALRUBlk
* blk
= head
;
310 int boundary
= 1<<17;
312 int flags
= cacheMask
;
315 if (blk
->inCache
!= flags
) {
318 if (tot_size
== boundary
&& blk
!= tail
) {
319 if (cacheBoundaries
[j
] != blk
) {
323 boundary
= boundary
<<1;
332 FALRUParams::create()
334 return new FALRU(this);