Fix problems with unCacheable addresses in timing-coherence
[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 block containing the given address.
188 * @param asid The address space ID.
189 * @param addr The address to invalidate.
190 */
191 void invalidateBlk(Addr addr);
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 and update replacement data.
205 * Returns the access latency as a side effect.
206 * @param pkt The req whose block to find
207 * @param lat The access latency.
208 * @return Pointer to the cache block if found.
209 */
210 SplitBlk* findBlock(Packet * &pkt, int &lat);
211
212 /**
213 * Finds the given address in the cache, do not update replacement data.
214 * @param addr The address to find.
215 * @param asid The address space ID.
216 * @return Pointer to the cache block if found.
217 */
218 SplitBlk* findBlock(Addr addr) const;
219
220 /**
221 * Find a replacement block for the address provided.
222 * @param pkt The request to a find a replacement candidate for.
223 * @param writebacks List for any writebacks to be performed.
224 * @param compress_blocks List of blocks to compress, for adaptive comp.
225 * @return The block to place the replacement in.
226 */
227 SplitBlk* findReplacement(Packet * &pkt, PacketList &writebacks,
228 BlkList &compress_blocks);
229
230 /**
231 * Generate the tag from the given address.
232 * @param addr The address to get the tag from.
233 * @return The tag of the address.
234 */
235 Addr extractTag(Addr addr) const
236 {
237 return (addr >> tagShift);
238 }
239
240 /**
241 * Generate the tag from the given address.
242 * @param addr The address to get the tag from.
243 * @param blk Ignored
244 * @return The tag of the address.
245 */
246 Addr extractTag(Addr addr, SplitBlk *blk) const
247 {
248 return (addr >> tagShift);
249 }
250
251 /**
252 * Calculate the set index from the address.
253 * @param addr The address to get the set from.
254 * @return The set index of the address.
255 */
256 int extractSet(Addr addr) const
257 {
258 return ((addr >> setShift) & setMask);
259 }
260
261 /**
262 * Get the block offset from an address.
263 * @param addr The address to get the offset of.
264 * @return The block offset.
265 */
266 int extractBlkOffset(Addr addr) const
267 {
268 return (addr & blkMask);
269 }
270
271 /**
272 * Align an address to the block size.
273 * @param addr the address to align.
274 * @return The block address.
275 */
276 Addr blkAlign(Addr addr) const
277 {
278 return (addr & ~(Addr)blkMask);
279 }
280
281 /**
282 * Regenerate the block address from the tag.
283 * @param tag The tag of the block.
284 * @param set The set of the block.
285 * @return The block address.
286 */
287 Addr regenerateBlkAddr(Addr tag, unsigned set) const
288 {
289 return ((tag << tagShift) | ((Addr)set << setShift));
290 }
291
292 /**
293 * Return the hit latency.
294 * @return the hit latency.
295 */
296 int getHitLatency() const
297 {
298 return hitLatency;
299 }
300
301 /**
302 * Read the data out of the internal storage of the given cache block.
303 * @param blk The cache block to read.
304 * @param data The buffer to read the data into.
305 * @return The cache block's data.
306 */
307 void readData(SplitBlk *blk, uint8_t *data)
308 {
309 memcpy(data, blk->data, blk->size);
310 }
311
312 /**
313 * Write data into the internal storage of the given cache block. Since in
314 * LIFO does not store data differently this just needs to update the size.
315 * @param blk The cache block to write.
316 * @param data The data to write.
317 * @param size The number of bytes to write.
318 * @param writebacks A list for any writebacks to be performed. May be
319 * needed when writing to a compressed block.
320 */
321 void writeData(SplitBlk *blk, uint8_t *data, int size,
322 PacketList & writebacks)
323 {
324 assert(size <= blkSize);
325 blk->size = size;
326 }
327
328 /**
329 * Perform a block aligned copy from the source address to the destination.
330 * @param source The block-aligned source address.
331 * @param dest The block-aligned destination address.
332 * @param asid The address space DI.
333 * @param writebacks List for any generated writeback pktuests.
334 */
335 void doCopy(Addr source, Addr dest, PacketList &writebacks);
336
337 /**
338 * No impl.
339 */
340 void fixCopy(Packet * &pkt, PacketList &writebacks)
341 {
342 }
343
344 /**
345 * Called at end of simulation to complete average block reference stats.
346 */
347 virtual void cleanupRefs();
348 };
349
350 #endif