First cut at LL/SC support in caches (atomic mode only).
[gem5.git] / src / mem / cache / tags / split.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 split/partitioned tag store.
34 */
35
36 #ifndef __SPLIT_HH__
37 #define __SPLIT_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 #include "base/hashmap.hh"
47
48 class BaseCache;
49 class SplitLRU;
50 class SplitLIFO;
51
52 /**
53 * A cache tag store.
54 */
55 class Split : public BaseTags
56 {
57 public:
58 /** Typedef the block type used in this tag store. */
59 typedef SplitBlk BlkType;
60 /** Typedef for a list of pointers to the local block class. */
61 typedef std::list<SplitBlk*> BlkList;
62 protected:
63 /** The number of sets in the cache. */
64 const int numSets;
65 /** The number of bytes in a block. */
66 const int blkSize;
67 /** Whether the 2nd partition (for the nic) is LIFO or not */
68 const bool lifo;
69 /** The hit latency. */
70 const int hitLatency;
71
72 Addr blkMask;
73
74 /** Number of NIC pktuests that hit in the NIC partition */
75 Stats::Scalar<> NR_NP_hits;
76 /** Number of NIC pktuests that hit in the CPU partition */
77 Stats::Scalar<> NR_CP_hits;
78 /** Number of CPU pktuests that hit in the NIC partition */
79 Stats::Scalar<> CR_NP_hits;
80 /** Number of CPU pktuests that hit in the CPU partition */
81 Stats::Scalar<> CR_CP_hits;
82 /** The number of nic replacements (i.e. misses) */
83 Stats::Scalar<> nic_repl;
84 /** The number of cpu replacements (i.e. misses) */
85 Stats::Scalar<> cpu_repl;
86
87 //For latency studies
88 /** the number of NIC blks that were used before evicted */
89 Stats::Scalar<> nicUsedWhenEvicted;
90 /** the total latency of used NIC blocks in the cache */
91 Stats::Scalar<> nicUsedTotLatency;
92 /** the total number of used NIC blocks evicted */
93 Stats::Scalar<> nicUsedTotEvicted;
94 /** the average number of cycles a used NIC blk is in the cache */
95 Stats::Formula nicUsedAvgLatency;
96 /** the Distribution of used NIC blk eviction times */
97 Stats::Distribution<> usedEvictDist;
98
99 /** the number of NIC blks that were unused before evicted */
100 Stats::Scalar<> nicUnusedWhenEvicted;
101 /** the total latency of unused NIC blks in the cache */
102 Stats::Scalar<> nicUnusedTotLatency;
103 /** the total number of unused NIC blocks evicted */
104 Stats::Scalar<> nicUnusedTotEvicted;
105 /** the average number of cycles an unused NIC blk is in the cache */
106 Stats::Formula nicUnusedAvgLatency;
107 /** the Distribution of unused NIC blk eviction times */
108 Stats::Distribution<> unusedEvictDist;
109
110 /** The total latency of NIC blocks to 1st usage time by CPU */
111 Stats::Scalar<> nicUseByCPUCycleTotal;
112 /** The total number of NIC blocks used */
113 Stats::Scalar<> nicBlksUsedByCPU;
114 /** the average number of cycles before a NIC blk that is used gets used by CPU */
115 Stats::Formula nicAvgUsageByCPULatency;
116 /** the Distribution of cycles time before a NIC blk is used by CPU*/
117 Stats::Distribution<> useByCPUCycleDist;
118
119 /** the number of CPU blks that were used before evicted */
120 Stats::Scalar<> cpuUsedBlks;
121 /** the number of CPU blks that were unused before evicted */
122 Stats::Scalar<> cpuUnusedBlks;
123
124 /** the avg number of cycles before a NIC blk is evicted */
125 Stats::Formula nicAvgLatency;
126
127 typedef m5::hash_map<Addr, int, m5::hash<Addr> > hash_t;
128 typedef hash_t::const_iterator memIter;
129 hash_t memHash;
130
131
132 private:
133 SplitLRU *lru;
134 SplitLRU *lru_net;
135 SplitLIFO *lifo_net;
136
137 public:
138 /**
139 * Construct and initialize this tag store.
140 * @param _numSets The number of sets in the cache.
141 * @param _blkSize The number of bytes in a block.
142 * @param _assoc The associativity of the cache.
143 * @param _hit_latency The latency in cycles for a hit.
144 */
145 Split(int _numSets, int _blkSize, int total_ways, int LRU1_assoc,
146 bool _lifo, bool _two_queue, int _hit_latency);
147
148 /**
149 * Destructor
150 */
151 virtual ~Split();
152
153 /**
154 * Register the stats for this object
155 * @param name The name to prepend to the stats name.
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 Split 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 memory request 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 /**
232 * Generate the tag from the given address.
233 * @param addr The address to get the tag from.
234 * @param blk The block to find the partition it's in
235 * @return The tag of the address.
236 */
237 Addr extractTag(Addr addr, SplitBlk *blk) const;
238
239 /**
240 * Calculate the set index from the address.
241 * @param addr The address to get the set from.
242 * @return The set index of the address.
243 */
244 int extractSet(Addr addr) const
245 {
246 panic("should never call this!\n");
247 }
248
249 /**
250 * Get the block offset from an address.
251 * @param addr The address to get the offset of.
252 * @return The block offset.
253 */
254 int extractBlkOffset(Addr addr) const
255 {
256 return (addr & blkMask);
257 }
258
259 /**
260 * Align an address to the block size.
261 * @param addr the address to align.
262 * @return The block address.
263 */
264 Addr blkAlign(Addr addr) const
265 {
266 return (addr & ~(Addr) (blkMask));
267 }
268
269 /**
270 * Regenerate the block address from the tag.
271 * @param tag The tag of the block.
272 * @param set The set of the block.
273 * @return The block address.
274 */
275 Addr regenerateBlkAddr(Addr tag, int set) const;
276
277 /**
278 * Return the hit latency.
279 * @return the hit latency.
280 */
281 int getHitLatency() const
282 {
283 return hitLatency;
284 }
285
286 /**
287 * Read the data out of the internal storage of the given cache block.
288 * @param blk The cache block to read.
289 * @param data The buffer to read the data into.
290 * @return The cache block's data.
291 */
292 void readData(SplitBlk *blk, uint8_t *data)
293 {
294 memcpy(data, blk->data, blk->size);
295 }
296
297 /**
298 * Write data into the internal storage of the given cache block. Since in
299 * Split does not store data differently this just needs to update the size.
300 * @param blk The cache block to write.
301 * @param data The data to write.
302 * @param size The number of bytes to write.
303 * @param writebacks A list for any writebacks to be performed. May be
304 * needed when writing to a compressed block.
305 */
306 void writeData(SplitBlk *blk, uint8_t *data, int size,
307 PacketList & writebacks)
308 {
309 assert(size <= blkSize);
310 blk->size = size;
311 }
312
313 /**
314 * Called at end of simulation to complete average block reference stats.
315 */
316 virtual void cleanupRefs();
317 };
318
319 #endif