2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Erik Hallnor
33 * Definitions a fully associative LRU tagstore.
39 #include "base/intmath.hh"
40 #include "base/misc.hh"
41 #include "mem/cache/tags/fa_lru.hh"
45 FALRU::FALRU(unsigned _blkSize
, unsigned _size
, unsigned hit_latency
)
46 : blkSize(_blkSize
), size(_size
), hitLatency(hit_latency
)
48 if (!isPowerOf2(blkSize
))
49 fatal("cache block size (in bytes) `%d' must be a power of two",
51 if (!(hitLatency
> 0))
52 fatal("Access latency in cycles must be at least one cycle");
53 if (!isPowerOf2(size
))
54 fatal("Cache Size must be power of 2 for now");
56 // Track all cache sizes from 128K up by powers of 2
57 numCaches
= floorLog2(size
) - 17;
59 cacheBoundaries
= new FALRUBlk
*[numCaches
];
60 cacheMask
= (1 << numCaches
) - 1;
66 warmupBound
= size
/blkSize
;
67 numBlocks
= size
/blkSize
;
69 blks
= new FALRUBlk
[numBlocks
];
71 tail
= &(blks
[numBlocks
-1]);
74 head
->next
= &(blks
[1]);
75 head
->inCache
= cacheMask
;
77 tail
->prev
= &(blks
[numBlocks
-2]);
81 unsigned index
= (1 << 17) / blkSize
;
83 int flags
= cacheMask
;
84 for (unsigned i
= 1; i
< numBlocks
- 1; i
++) {
85 blks
[i
].inCache
= flags
;
87 cacheBoundaries
[j
] = &(blks
[i
]);
92 blks
[i
].prev
= &(blks
[i
-1]);
93 blks
[i
].next
= &(blks
[i
+1]);
94 blks
[i
].isTouched
= false;
96 assert(j
== numCaches
);
97 assert(index
== numBlocks
);
104 delete[] cacheBoundaries
;
110 FALRU::regStats(const string
&name
)
112 using namespace Stats
;
113 BaseTags::regStats(name
);
116 .name(name
+ ".falru_hits")
117 .desc("The number of hits in each cache size.")
121 .name(name
+ ".falru_misses")
122 .desc("The number of misses in each cache size.")
125 .name(name
+ ".falru_accesses")
126 .desc("The number of accesses to the FA LRU cache.")
129 for (unsigned i
= 0; i
<= numCaches
; ++i
) {
130 stringstream size_str
;
132 size_str
<< (1<<(i
+7)) <<"K";
134 size_str
<< (1<<(i
-3)) <<"M";
137 hits
.subname(i
, size_str
.str());
138 hits
.subdesc(i
, "Hits in a " + size_str
.str() +" cache");
139 misses
.subname(i
, size_str
.str());
140 misses
.subdesc(i
, "Misses in a " + size_str
.str() +" cache");
145 FALRU::hashLookup(Addr addr
) const
147 tagIterator iter
= tagHash
.find(addr
);
148 if (iter
!= tagHash
.end()) {
149 return (*iter
).second
;
155 FALRU::invalidate(FALRU::BlkType
*blk
)
162 FALRU::accessBlock(Addr addr
, int &lat
, int context_src
, int *inCache
)
165 int tmp_in_cache
= 0;
166 Addr blkAddr
= blkAlign(addr
);
167 FALRUBlk
* blk
= hashLookup(blkAddr
);
169 if (blk
&& blk
->isValid()) {
170 assert(blk
->tag
== blkAddr
);
171 tmp_in_cache
= blk
->inCache
;
172 for (unsigned i
= 0; i
< numCaches
; i
++) {
173 if (1<<i
& blk
->inCache
) {
185 for (unsigned i
= 0; i
<= numCaches
; ++i
) {
190 *inCache
= tmp_in_cache
;
200 FALRU::findBlock(Addr addr
) const
202 Addr blkAddr
= blkAlign(addr
);
203 FALRUBlk
* blk
= hashLookup(blkAddr
);
205 if (blk
&& blk
->isValid()) {
206 assert(blk
->tag
== blkAddr
);
214 FALRU::findVictim(Addr addr
, PacketList
&writebacks
)
216 FALRUBlk
* blk
= tail
;
217 assert(blk
->inCache
== 0);
219 tagHash
.erase(blk
->tag
);
220 tagHash
[blkAlign(addr
)] = blk
;
221 if (blk
->isValid()) {
225 blk
->isTouched
= true;
226 if (!warmedUp
&& tagsInUse
.value() >= warmupBound
) {
228 warmupCycle
= curTick();
236 FALRU::insertBlock(Addr addr
, FALRU::BlkType
*blk
, int context_src
)
241 FALRU::moveToHead(FALRUBlk
*blk
)
243 int updateMask
= blk
->inCache
^ cacheMask
;
244 for (unsigned i
= 0; i
< numCaches
; i
++){
245 if ((1<<i
) & updateMask
) {
246 cacheBoundaries
[i
]->inCache
&= ~(1<<i
);
247 cacheBoundaries
[i
] = cacheBoundaries
[i
]->prev
;
248 } else if (cacheBoundaries
[i
] == blk
) {
249 cacheBoundaries
[i
] = blk
->prev
;
252 blk
->inCache
= cacheMask
;
255 assert(blk
->next
== NULL
);
259 blk
->prev
->next
= blk
->next
;
260 blk
->next
->prev
= blk
->prev
;
272 FALRUBlk
* blk
= head
;
274 int boundary
= 1<<17;
276 int flags
= cacheMask
;
279 if (blk
->inCache
!= flags
) {
282 if (size
== boundary
&& blk
!= tail
) {
283 if (cacheBoundaries
[j
] != blk
) {
287 boundary
= boundary
<<1;
298 for (int i
= 0; i
< numBlocks
; i
++){
299 blks
[i
].clearLoadLocks();