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