cache: pull CacheSet out of LRU so that other tags can use associative sets.
[gem5.git] / src / mem / cache / tags / lru.cc
1 /*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Erik Hallnor
29 */
30
31 /**
32 * @file
33 * Definitions of LRU tag store.
34 */
35
36 #include <string>
37
38 #include "mem/cache/base.hh"
39 #include "base/intmath.hh"
40 #include "mem/cache/tags/lru.hh"
41 #include "sim/core.hh"
42 #include "cacheset.hh"
43
44 using namespace std;
45
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)
51 {
52 // Check parameters
53 if (blkSize < 4 || !isPowerOf2(blkSize)) {
54 fatal("Block size must be at least 4 and a power of 2");
55 }
56 if (numSets <= 0 || !isPowerOf2(numSets)) {
57 fatal("# of sets must be non-zero and a power of 2");
58 }
59 if (assoc <= 0) {
60 fatal("associativity must be greater than zero");
61 }
62 if (hitLatency <= 0) {
63 fatal("access latency must be greater than zero");
64 }
65
66 blkMask = blkSize - 1;
67 setShift = floorLog2(blkSize);
68 setMask = numSets - 1;
69 tagShift = setShift + floorLog2(numSets);
70 warmedUp = false;
71 /** @todo Make warmup percentage a parameter. */
72 warmupBound = numSets * assoc;
73
74 sets = new CacheSet[numSets];
75 blks = new BlkType[numSets * assoc];
76 // allocate data storage in one big chunk
77 dataBlks = new uint8_t[numSets*assoc*blkSize];
78
79 unsigned blkIndex = 0; // index into blks array
80 for (unsigned i = 0; i < numSets; ++i) {
81 sets[i].assoc = assoc;
82
83 sets[i].blks = new BlkType*[assoc];
84
85 // link in the data blocks
86 for (unsigned j = 0; j < assoc; ++j) {
87 // locate next cache block
88 BlkType *blk = &blks[blkIndex];
89 blk->data = &dataBlks[blkSize*blkIndex];
90 ++blkIndex;
91
92 // invalidate new cache block
93 blk->status = 0;
94
95 //EGH Fix Me : do we need to initialize blk?
96
97 // Setting the tag to j is just to prevent long chains in the hash
98 // table; won't matter because the block is invalid
99 blk->tag = j;
100 blk->whenReady = 0;
101 blk->isTouched = false;
102 blk->size = blkSize;
103 sets[i].blks[j]=blk;
104 blk->set = i;
105 }
106 }
107 }
108
109 LRU::~LRU()
110 {
111 delete [] dataBlks;
112 delete [] blks;
113 delete [] sets;
114 }
115
116 LRU::BlkType*
117 LRU::accessBlock(Addr addr, int &lat, int context_src)
118 {
119 Addr tag = extractTag(addr);
120 unsigned set = extractSet(addr);
121 BlkType *blk = sets[set].findBlk(tag);
122 lat = hitLatency;
123 if (blk != NULL) {
124 // move this block to head of the MRU list
125 sets[set].moveToHead(blk);
126 DPRINTF(CacheRepl, "set %x: moving blk %x to MRU\n",
127 set, regenerateBlkAddr(tag, set));
128 if (blk->whenReady > curTick
129 && blk->whenReady - curTick > hitLatency) {
130 lat = blk->whenReady - curTick;
131 }
132 blk->refCount += 1;
133 }
134
135 return blk;
136 }
137
138
139 LRU::BlkType*
140 LRU::findBlock(Addr addr) const
141 {
142 Addr tag = extractTag(addr);
143 unsigned set = extractSet(addr);
144 BlkType *blk = sets[set].findBlk(tag);
145 return blk;
146 }
147
148 LRU::BlkType*
149 LRU::findVictim(Addr addr, PacketList &writebacks)
150 {
151 unsigned set = extractSet(addr);
152 // grab a replacement candidate
153 BlkType *blk = sets[set].blks[assoc-1];
154 if (blk->isValid()) {
155 replacements[0]++;
156 totalRefs += blk->refCount;
157 ++sampledRefs;
158 blk->refCount = 0;
159
160 DPRINTF(CacheRepl, "set %x: selecting blk %x for replacement\n",
161 set, regenerateBlkAddr(blk->tag, set));
162 }
163 return blk;
164 }
165
166 void
167 LRU::insertBlock(Addr addr, BlkType *blk, int context_src)
168 {
169 if (!blk->isTouched) {
170 tagsInUse++;
171 blk->isTouched = true;
172 if (!warmedUp && tagsInUse.value() >= warmupBound) {
173 warmedUp = true;
174 warmupCycle = curTick;
175 }
176 }
177
178 // Set tag for new block. Caller is responsible for setting status.
179 blk->tag = extractTag(addr);
180
181 unsigned set = extractSet(addr);
182 sets[set].moveToHead(blk);
183 }
184
185 void
186 LRU::invalidateBlk(BlkType *blk)
187 {
188 if (blk) {
189 blk->status = 0;
190 blk->isTouched = false;
191 blk->clearLoadLocks();
192 tagsInUse--;
193 }
194 }
195
196 void
197 LRU::cleanupRefs()
198 {
199 for (unsigned i = 0; i < numSets*assoc; ++i) {
200 if (blks[i].isValid()) {
201 totalRefs += blks[i].refCount;
202 ++sampledRefs;
203 }
204 }
205 }