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