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 of LRU tag store.
38 #include "base/intmath.hh"
39 #include "mem/cache/base.hh"
40 #include "mem/cache/tags/cacheset.hh"
41 #include "mem/cache/tags/lru.hh"
42 #include "sim/core.hh"
46 // create and initialize a LRU/MRU cache structure
47 LRU::LRU(unsigned _numSets
, unsigned _blkSize
, unsigned _assoc
,
48 unsigned _hit_latency
)
49 : numSets(_numSets
), blkSize(_blkSize
), assoc(_assoc
),
50 hitLatency(_hit_latency
)
53 if (blkSize
< 4 || !isPowerOf2(blkSize
)) {
54 fatal("Block size must be at least 4 and a power of 2");
56 if (numSets
<= 0 || !isPowerOf2(numSets
)) {
57 fatal("# of sets must be non-zero and a power of 2");
60 fatal("associativity must be greater than zero");
62 if (hitLatency
<= 0) {
63 fatal("access latency must be greater than zero");
66 blkMask
= blkSize
- 1;
67 setShift
= floorLog2(blkSize
);
68 setMask
= numSets
- 1;
69 tagShift
= setShift
+ floorLog2(numSets
);
71 /** @todo Make warmup percentage a parameter. */
72 warmupBound
= numSets
* assoc
;
74 sets
= new CacheSet
[numSets
];
75 blks
= new BlkType
[numSets
* assoc
];
76 // allocate data storage in one big chunk
77 numBlocks
= numSets
* assoc
;
78 dataBlks
= new uint8_t[numBlocks
* blkSize
];
80 unsigned blkIndex
= 0; // index into blks array
81 for (unsigned i
= 0; i
< numSets
; ++i
) {
82 sets
[i
].assoc
= assoc
;
84 sets
[i
].blks
= new BlkType
*[assoc
];
86 // link in the data blocks
87 for (unsigned j
= 0; j
< assoc
; ++j
) {
88 // locate next cache block
89 BlkType
*blk
= &blks
[blkIndex
];
90 blk
->data
= &dataBlks
[blkSize
*blkIndex
];
93 // invalidate new cache block
96 //EGH Fix Me : do we need to initialize blk?
98 // Setting the tag to j is just to prevent long chains in the hash
99 // table; won't matter because the block is invalid
102 blk
->isTouched
= false;
118 LRU::accessBlock(Addr addr
, int &lat
, int context_src
)
120 Addr tag
= extractTag(addr
);
121 unsigned set
= extractSet(addr
);
122 BlkType
*blk
= sets
[set
].findBlk(tag
);
125 // move this block to head of the MRU list
126 sets
[set
].moveToHead(blk
);
127 DPRINTF(CacheRepl
, "set %x: moving blk %x to MRU\n",
128 set
, regenerateBlkAddr(tag
, set
));
129 if (blk
->whenReady
> curTick()
130 && blk
->whenReady
- curTick() > hitLatency
) {
131 lat
= blk
->whenReady
- curTick();
141 LRU::findBlock(Addr addr
) const
143 Addr tag
= extractTag(addr
);
144 unsigned set
= extractSet(addr
);
145 BlkType
*blk
= sets
[set
].findBlk(tag
);
150 LRU::findVictim(Addr addr
, PacketList
&writebacks
)
152 unsigned set
= extractSet(addr
);
153 // grab a replacement candidate
154 BlkType
*blk
= sets
[set
].blks
[assoc
-1];
155 if (blk
->isValid()) {
157 totalRefs
+= blk
->refCount
;
161 // deal with evicted block
162 if (blk
->contextSrc
!= -1) {
163 occupancies
[blk
->contextSrc
% cache
->numCpus()]--;
164 blk
->contextSrc
= -1;
166 occupancies
[cache
->numCpus()]--;
169 DPRINTF(CacheRepl
, "set %x: selecting blk %x for replacement\n",
170 set
, regenerateBlkAddr(blk
->tag
, set
));
176 LRU::insertBlock(Addr addr
, BlkType
*blk
, int context_src
)
178 if (!blk
->isTouched
) {
180 blk
->isTouched
= true;
181 if (!warmedUp
&& tagsInUse
.value() >= warmupBound
) {
183 warmupCycle
= curTick();
187 // Set tag for new block. Caller is responsible for setting status.
188 blk
->tag
= extractTag(addr
);
190 // deal with what we are bringing in
191 if (context_src
!= -1) {
192 occupancies
[context_src
% cache
->numCpus()]++;
194 occupancies
[cache
->numCpus()]++;
196 blk
->contextSrc
= context_src
;
198 unsigned set
= extractSet(addr
);
199 sets
[set
].moveToHead(blk
);
203 LRU::invalidateBlk(BlkType
*blk
)
207 blk
->isTouched
= false;
208 blk
->clearLoadLocks();
210 if (blk
->contextSrc
!= -1) {
211 occupancies
[blk
->contextSrc
% cache
->numCpus()]--;
212 blk
->contextSrc
= -1;
214 occupancies
[cache
->numCpus()]--;
222 for (int i
= 0; i
< numBlocks
; i
++){
223 blks
[i
].clearLoadLocks();
230 for (unsigned i
= 0; i
< numSets
*assoc
; ++i
) {
231 if (blks
[i
].isValid()) {
232 totalRefs
+= blks
[i
].refCount
;