First cut at LL/SC support in caches (atomic mode only).
[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 block containing the given address.
171 * @param asid The address space ID.
172 * @param addr The address to invalidate.
173 */
174 void invalidateBlk(Addr addr);
175
176 /**
177 * Finds the given address in the cache and update replacement data.
178 * Returns the access latency as a side effect.
179 * @param addr The address to find.
180 * @param asid The address space ID.
181 * @param lat The access latency.
182 * @return Pointer to the cache block if found.
183 */
184 SplitBlk* findBlock(Addr addr, int &lat);
185
186 /**
187 * Finds the given address in the cache and update replacement data.
188 * Returns the access latency as a side effect.
189 * @param pkt The req whose block to find.
190 * @param lat The access latency.
191 * @return Pointer to the cache block if found.
192 */
193 SplitBlk* findBlock(Packet * &pkt, int &lat);
194
195 /**
196 * Finds the given address in the cache, do not update replacement data.
197 * @param addr The address to find.
198 * @param asid The address space ID.
199 * @return Pointer to the cache block if found.
200 */
201 SplitBlk* findBlock(Addr addr) const;
202
203 /**
204 * Find a replacement block for the address provided.
205 * @param pkt The request to a find a replacement candidate for.
206 * @param writebacks List for any writebacks to be performed.
207 * @param compress_blocks List of blocks to compress, for adaptive comp.
208 * @return The block to place the replacement in.
209 */
210 SplitBlk* findReplacement(Packet * &pkt, PacketList &writebacks,
211 BlkList &compress_blocks);
212
213 /**
214 * Generate the tag from the given address.
215 * @param addr The address to get the tag from.
216 * @return The tag of the address.
217 */
218 Addr extractTag(Addr addr) const
219 {
220 return (addr >> tagShift);
221 }
222
223 /**
224 * Generate the tag from the given address.
225 * @param addr The address to get the tag from.
226 * @param blk Ignored.
227 * @return The tag of the address.
228 */
229 Addr extractTag(Addr addr, SplitBlk *blk) const
230 {
231 return (addr >> tagShift);
232 }
233
234 /**
235 * Calculate the set index from the address.
236 * @param addr The address to get the set from.
237 * @return The set index of the address.
238 */
239 int extractSet(Addr addr) const
240 {
241 return ((addr >> setShift) & setMask);
242 }
243
244 /**
245 * Get the block offset from an address.
246 * @param addr The address to get the offset of.
247 * @return The block offset.
248 */
249 int extractBlkOffset(Addr addr) const
250 {
251 return (addr & blkMask);
252 }
253
254 /**
255 * Align an address to the block size.
256 * @param addr the address to align.
257 * @return The block address.
258 */
259 Addr blkAlign(Addr addr) const
260 {
261 return (addr & ~(Addr)blkMask);
262 }
263
264 /**
265 * Regenerate the block address from the tag.
266 * @param tag The tag of the block.
267 * @param set The set of the block.
268 * @return The block address.
269 */
270 Addr regenerateBlkAddr(Addr tag, unsigned set) const
271 {
272 return ((tag << tagShift) | ((Addr)set << setShift));
273 }
274
275 /**
276 * Return the hit latency.
277 * @return the hit latency.
278 */
279 int getHitLatency() const
280 {
281 return hitLatency;
282 }
283
284 /**
285 * Read the data out of the internal storage of the given cache block.
286 * @param blk The cache block to read.
287 * @param data The buffer to read the data into.
288 * @return The cache block's data.
289 */
290 void readData(SplitBlk *blk, uint8_t *data)
291 {
292 memcpy(data, blk->data, blk->size);
293 }
294
295 /**
296 * Write data into the internal storage of the given cache block. Since in
297 * LRU does not store data differently this just needs to update the size.
298 * @param blk The cache block to write.
299 * @param data The data to write.
300 * @param size The number of bytes to write.
301 * @param writebacks A list for any writebacks to be performed. May be
302 * needed when writing to a compressed block.
303 */
304 void writeData(SplitBlk *blk, uint8_t *data, int size,
305 PacketList & writebacks)
306 {
307 assert(size <= blkSize);
308 blk->size = size;
309 }
310
311 /**
312 * Called at end of simulation to complete average block reference stats.
313 */
314 virtual void cleanupRefs();
315 };
316
317 #endif