Merge zizzer:/bk/newmem
[gem5.git] / src / mem / cache / tags / split_lru.hh
1 /*
2 * Copyright (c) 2004-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: Lisa Hsu
29 */
30
31 /**
32 * @file
33 * Declaration of a LRU tag store for a partitioned cache.
34 */
35
36 #ifndef __SPLIT_LRU_HH__
37 #define __SPLIT_LRU_HH__
38
39 #include <list>
40
41 #include "mem/cache/cache_blk.hh" // base class
42 #include "mem/cache/tags/split_blk.hh"
43 #include "mem/packet.hh" // for inlined functions
44 #include <assert.h>
45 #include "mem/cache/tags/base_tags.hh"
46
47 class BaseCache;
48
49 /**
50 * An associative set of cache blocks.
51 */
52
53 class SplitCacheSet
54 {
55 public:
56 /** The associativity of this set. */
57 int assoc;
58
59 /** Cache blocks in this set, maintained in LRU order 0 = MRU. */
60 SplitBlk **blks;
61
62 /**
63 * Find a block matching the tag in this set.
64 * @param asid The address space ID.
65 * @param tag The Tag to find.
66 * @return Pointer to the block if found.
67 */
68 SplitBlk* findBlk(Addr tag) const;
69
70 /**
71 * Move the given block to the head of the list.
72 * @param blk The block to move.
73 */
74 void moveToHead(SplitBlk *blk);
75 };
76
77 /**
78 * A LRU cache tag store.
79 */
80 class SplitLRU : public BaseTags
81 {
82 public:
83 /** Typedef the block type used in this tag store. */
84 typedef SplitBlk BlkType;
85 /** Typedef for a list of pointers to the local block class. */
86 typedef std::list<SplitBlk*> BlkList;
87 protected:
88 /** The number of sets in the cache. */
89 const int numSets;
90 /** The number of bytes in a block. */
91 const int blkSize;
92 /** The associativity of the cache. */
93 const int assoc;
94 /** The hit latency. */
95 const int hitLatency;
96 /** indicator for which partition this is */
97 const int part;
98
99 /** The cache sets. */
100 SplitCacheSet *sets;
101
102 /** The cache blocks. */
103 SplitBlk *blks;
104 /** The data blocks, 1 per cache block. */
105 uint8_t *dataBlks;
106
107 /** The amount to shift the address to get the set. */
108 int setShift;
109 /** The amount to shift the address to get the tag. */
110 int tagShift;
111 /** Mask out all bits that aren't part of the set index. */
112 unsigned setMask;
113 /** Mask out all bits that aren't part of the block offset. */
114 unsigned blkMask;
115
116 /** number of hits in this partition */
117 Stats::Scalar<> hits;
118 /** number of blocks brought into this partition (i.e. misses) */
119 Stats::Scalar<> misses;
120
121 public:
122 /**
123 * Construct and initialize this tag store.
124 * @param _numSets The number of sets in the cache.
125 * @param _blkSize The number of bytes in a block.
126 * @param _assoc The associativity of the cache.
127 * @param _hit_latency The latency in cycles for a hit.
128 */
129 SplitLRU(int _numSets, int _blkSize, int _assoc, int _hit_latency, int _part);
130
131 /**
132 * Destructor
133 */
134 virtual ~SplitLRU();
135
136 /**
137 * Register the statistics for this object
138 * @param name The name to precede the stat
139 */
140 void regStats(const std::string &name);
141
142 /**
143 * Return the block size.
144 * @return the block size.
145 */
146 int getBlockSize()
147 {
148 return blkSize;
149 }
150
151 /**
152 * Return the subblock size. In the case of LRU it is always the block
153 * size.
154 * @return The block size.
155 */
156 int getSubBlockSize()
157 {
158 return blkSize;
159 }
160
161 /**
162 * Search for the address in the cache.
163 * @param asid The address space ID.
164 * @param addr The address to find.
165 * @return True if the address is in the cache.
166 */
167 bool probe(Addr addr) const;
168
169 /**
170 * Invalidate the given block.
171 * @param blk The block to invalidate.
172 */
173 void invalidateBlk(BlkType *blk);
174
175 /**
176 * Finds the given address in the cache and update replacement data.
177 * Returns the access latency as a side effect.
178 * @param addr The address to find.
179 * @param asid The address space ID.
180 * @param lat The access latency.
181 * @return Pointer to the cache block if found.
182 */
183 SplitBlk* findBlock(Addr addr, int &lat);
184
185 /**
186 * Finds the given address in the cache, do not update replacement data.
187 * @param addr The address to find.
188 * @param asid The address space ID.
189 * @return Pointer to the cache block if found.
190 */
191 SplitBlk* findBlock(Addr addr) const;
192
193 /**
194 * Find a replacement block for the address provided.
195 * @param pkt The request to a find a replacement candidate for.
196 * @param writebacks List for any writebacks to be performed.
197 * @param compress_blocks List of blocks to compress, for adaptive comp.
198 * @return The block to place the replacement in.
199 */
200 SplitBlk* findReplacement(PacketPtr &pkt, PacketList &writebacks,
201 BlkList &compress_blocks);
202
203 /**
204 * Generate the tag from the given address.
205 * @param addr The address to get the tag from.
206 * @return The tag of the address.
207 */
208 Addr extractTag(Addr addr) const
209 {
210 return (addr >> tagShift);
211 }
212
213 /**
214 * Generate the tag from the given address.
215 * @param addr The address to get the tag from.
216 * @param blk Ignored.
217 * @return The tag of the address.
218 */
219 Addr extractTag(Addr addr, SplitBlk *blk) const
220 {
221 return (addr >> tagShift);
222 }
223
224 /**
225 * Calculate the set index from the address.
226 * @param addr The address to get the set from.
227 * @return The set index of the address.
228 */
229 int extractSet(Addr addr) const
230 {
231 return ((addr >> setShift) & setMask);
232 }
233
234 /**
235 * Get the block offset from an address.
236 * @param addr The address to get the offset of.
237 * @return The block offset.
238 */
239 int extractBlkOffset(Addr addr) const
240 {
241 return (addr & blkMask);
242 }
243
244 /**
245 * Align an address to the block size.
246 * @param addr the address to align.
247 * @return The block address.
248 */
249 Addr blkAlign(Addr addr) const
250 {
251 return (addr & ~(Addr)blkMask);
252 }
253
254 /**
255 * Regenerate the block address from the tag.
256 * @param tag The tag of the block.
257 * @param set The set of the block.
258 * @return The block address.
259 */
260 Addr regenerateBlkAddr(Addr tag, unsigned set) const
261 {
262 return ((tag << tagShift) | ((Addr)set << setShift));
263 }
264
265 /**
266 * Return the hit latency.
267 * @return the hit latency.
268 */
269 int getHitLatency() const
270 {
271 return hitLatency;
272 }
273
274 /**
275 * Read the data out of the internal storage of the given cache block.
276 * @param blk The cache block to read.
277 * @param data The buffer to read the data into.
278 * @return The cache block's data.
279 */
280 void readData(SplitBlk *blk, uint8_t *data)
281 {
282 memcpy(data, blk->data, blk->size);
283 }
284
285 /**
286 * Write data into the internal storage of the given cache block. Since in
287 * LRU does not store data differently this just needs to update the size.
288 * @param blk The cache block to write.
289 * @param data The data to write.
290 * @param size The number of bytes to write.
291 * @param writebacks A list for any writebacks to be performed. May be
292 * needed when writing to a compressed block.
293 */
294 void writeData(SplitBlk *blk, uint8_t *data, int size,
295 PacketList & writebacks)
296 {
297 assert(size <= blkSize);
298 blk->size = size;
299 }
300
301 /**
302 * Called at end of simulation to complete average block reference stats.
303 */
304 virtual void cleanupRefs();
305 };
306
307 #endif