Fix problems with unCacheable addresses in timing-coherence
[gem5.git] / src / mem / cache / cache_builder.cc
1 /*
2 * Copyright (c) 2003-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: Erik Hallnor
29 * Nathan Binkert
30 */
31
32 /**
33 * @file
34 * Simobject instatiation of caches.
35 */
36 #include <vector>
37
38 // Must be included first to determine which caches we want
39 #include "mem/config/cache.hh"
40 #include "mem/config/compression.hh"
41 #include "mem/config/prefetch.hh"
42
43 #include "mem/cache/base_cache.hh"
44 #include "mem/cache/cache.hh"
45 #include "mem/bus.hh"
46 #include "mem/cache/coherence/coherence_protocol.hh"
47 #include "sim/builder.hh"
48
49 // Tag Templates
50 #if defined(USE_CACHE_LRU)
51 #include "mem/cache/tags/lru.hh"
52 #endif
53
54 #if defined(USE_CACHE_FALRU)
55 #include "mem/cache/tags/fa_lru.hh"
56 #endif
57
58 #if defined(USE_CACHE_IIC)
59 #include "mem/cache/tags/iic.hh"
60 #endif
61
62 #if defined(USE_CACHE_SPLIT)
63 #include "mem/cache/tags/split.hh"
64 #endif
65
66 #if defined(USE_CACHE_SPLIT_LIFO)
67 #include "mem/cache/tags/split_lifo.hh"
68 #endif
69
70 // Compression Templates
71 #include "base/compression/null_compression.hh"
72 #if defined(USE_LZSS_COMPRESSION)
73 #include "base/compression/lzss_compression.hh"
74 #endif
75
76 // CacheTags Templates
77 #include "mem/cache/tags/cache_tags.hh"
78
79 // MissQueue Templates
80 #include "mem/cache/miss/miss_queue.hh"
81 #include "mem/cache/miss/blocking_buffer.hh"
82
83 // Coherence Templates
84 #include "mem/cache/coherence/uni_coherence.hh"
85 #include "mem/cache/coherence/simple_coherence.hh"
86
87 //Prefetcher Headers
88 #if defined(USE_GHB)
89 #include "mem/cache/prefetch/ghb_prefetcher.hh"
90 #endif
91 #if defined(USE_TAGGED)
92 #include "mem/cache/prefetch/tagged_prefetcher.hh"
93 #endif
94 #if defined(USE_STRIDED)
95 #include "mem/cache/prefetch/stride_prefetcher.hh"
96 #endif
97
98
99 using namespace std;
100 using namespace TheISA;
101
102 #ifndef DOXYGEN_SHOULD_SKIP_THIS
103
104 BEGIN_DECLARE_SIM_OBJECT_PARAMS(BaseCache)
105
106 Param<int> size;
107 Param<int> assoc;
108 Param<int> block_size;
109 Param<int> latency;
110 Param<int> mshrs;
111 Param<int> tgts_per_mshr;
112 Param<int> write_buffers;
113 Param<bool> prioritizeRequests;
114 // SimObjectParam<Bus *> in_bus;
115 // SimObjectParam<Bus *> out_bus;
116 Param<bool> do_copy;
117 SimObjectParam<CoherenceProtocol *> protocol;
118 Param<Addr> trace_addr;
119 Param<int> hash_delay;
120 #if defined(USE_CACHE_IIC)
121 SimObjectParam<Repl *> repl;
122 #endif
123 Param<bool> compressed_bus;
124 Param<bool> store_compressed;
125 Param<bool> adaptive_compression;
126 Param<int> compression_latency;
127 Param<int> subblock_size;
128 Param<Counter> max_miss_count;
129 // SimObjectParam<HierParams *> hier;
130 VectorParam<Range<Addr> > addr_range;
131 // SimObjectParam<MemTraceWriter *> mem_trace;
132 Param<bool> split;
133 Param<int> split_size;
134 Param<bool> lifo;
135 Param<bool> two_queue;
136 Param<bool> prefetch_miss;
137 Param<bool> prefetch_access;
138 Param<int> prefetcher_size;
139 Param<bool> prefetch_past_page;
140 Param<bool> prefetch_serial_squash;
141 Param<Tick> prefetch_latency;
142 Param<int> prefetch_degree;
143 Param<string> prefetch_policy;
144 Param<bool> prefetch_cache_check_push;
145 Param<bool> prefetch_use_cpu_id;
146 Param<bool> prefetch_data_accesses_only;
147 Param<int> hit_latency;
148
149 END_DECLARE_SIM_OBJECT_PARAMS(BaseCache)
150
151
152 BEGIN_INIT_SIM_OBJECT_PARAMS(BaseCache)
153
154 INIT_PARAM(size, "capacity in bytes"),
155 INIT_PARAM(assoc, "associativity"),
156 INIT_PARAM(block_size, "block size in bytes"),
157 INIT_PARAM(latency, "hit latency in CPU cycles"),
158 INIT_PARAM(mshrs, "number of MSHRs (max outstanding requests)"),
159 INIT_PARAM(tgts_per_mshr, "max number of accesses per MSHR"),
160 INIT_PARAM_DFLT(write_buffers, "number of write buffers", 8),
161 INIT_PARAM_DFLT(prioritizeRequests, "always service demand misses first",
162 false),
163 /* INIT_PARAM_DFLT(in_bus, "incoming bus object", NULL),
164 INIT_PARAM(out_bus, "outgoing bus object"),
165 */
166 INIT_PARAM_DFLT(do_copy, "perform fast copies in the cache", false),
167 INIT_PARAM_DFLT(protocol, "coherence protocol to use in the cache", NULL),
168 INIT_PARAM_DFLT(trace_addr, "address to trace", 0),
169
170 INIT_PARAM_DFLT(hash_delay, "time in cycles of hash access",1),
171 #if defined(USE_CACHE_IIC)
172 INIT_PARAM_DFLT(repl, "replacement policy",NULL),
173 #endif
174 INIT_PARAM_DFLT(compressed_bus,
175 "This cache connects to a compressed memory",
176 false),
177 INIT_PARAM_DFLT(store_compressed, "Store compressed data in the cache",
178 false),
179 INIT_PARAM_DFLT(adaptive_compression, "Use an adaptive compression scheme",
180 false),
181 INIT_PARAM_DFLT(compression_latency,
182 "Latency in cycles of compression algorithm",
183 0),
184 INIT_PARAM_DFLT(subblock_size,
185 "Size of subblock in IIC used for compression",
186 0),
187 INIT_PARAM_DFLT(max_miss_count,
188 "The number of misses to handle before calling exit",
189 0),
190 /* INIT_PARAM_DFLT(hier,
191 "Hierarchy global variables",
192 &defaultHierParams),
193 */
194 INIT_PARAM_DFLT(addr_range, "The address range in bytes",
195 vector<Range<Addr> >(1,RangeIn((Addr)0, MaxAddr))),
196 // INIT_PARAM_DFLT(mem_trace, "Memory trace to write accesses to", NULL),
197 INIT_PARAM_DFLT(split, "Whether this is a partitioned cache", false),
198 INIT_PARAM_DFLT(split_size, "the number of \"ways\" belonging to the LRU partition", 0),
199 INIT_PARAM_DFLT(lifo, "whether you are using a LIFO repl. policy", false),
200 INIT_PARAM_DFLT(two_queue, "whether the lifo should have two queue replacement", false),
201 INIT_PARAM_DFLT(prefetch_miss, "wheter you are using the hardware prefetcher from Miss stream", false),
202 INIT_PARAM_DFLT(prefetch_access, "wheter you are using the hardware prefetcher from Access stream", false),
203 INIT_PARAM_DFLT(prefetcher_size, "Number of entries in the harware prefetch queue", 100),
204 INIT_PARAM_DFLT(prefetch_past_page, "Allow prefetches to cross virtual page boundaries", false),
205 INIT_PARAM_DFLT(prefetch_serial_squash, "Squash prefetches with a later time on a subsequent miss", false),
206 INIT_PARAM_DFLT(prefetch_latency, "Latency of the prefetcher", 10),
207 INIT_PARAM_DFLT(prefetch_degree, "Degree of the prefetch depth", 1),
208 INIT_PARAM_DFLT(prefetch_policy, "Type of prefetcher to use", "none"),
209 INIT_PARAM_DFLT(prefetch_cache_check_push, "Check if in cash on push or pop of prefetch queue", true),
210 INIT_PARAM_DFLT(prefetch_use_cpu_id, "Use the CPU ID to seperate calculations of prefetches", true),
211 INIT_PARAM_DFLT(prefetch_data_accesses_only, "Only prefetch on data not on instruction accesses", false),
212 INIT_PARAM_DFLT(hit_latency, "Hit Latecny for a succesful access", 1)
213 END_INIT_SIM_OBJECT_PARAMS(BaseCache)
214
215
216 #define BUILD_CACHE(t, comp, b, c) do { \
217 Prefetcher<CacheTags<t, comp>, b> *pf; \
218 if (pf_policy == "tagged") { \
219 BUILD_TAGGED_PREFETCHER(t, comp, b); \
220 } \
221 else if (pf_policy == "stride") { \
222 BUILD_STRIDED_PREFETCHER(t, comp, b); \
223 } \
224 else if (pf_policy == "ghb") { \
225 BUILD_GHB_PREFETCHER(t, comp, b); \
226 } \
227 else { \
228 BUILD_NULL_PREFETCHER(t, comp, b); \
229 } \
230 Cache<CacheTags<t, comp>, b, c>::Params params(tagStore, mq, coh, \
231 do_copy, base_params, \
232 /*in_bus, out_bus,*/ pf, \
233 prefetch_access, hit_latency); \
234 Cache<CacheTags<t, comp>, b, c> *retval = \
235 new Cache<CacheTags<t, comp>, b, c>(getInstanceName(), /*hier,*/ \
236 params); \
237 /* if (in_bus == NULL) { \
238 retval->setSlaveInterface(new MemoryInterface<Cache<CacheTags<t, comp>, b, c> >(getInstanceName(), hier, retval, mem_trace)); \
239 } else { \
240 retval->setSlaveInterface(new SlaveInterface<Cache<CacheTags<t, comp>, b, c>, Bus>(getInstanceName(), hier, retval, in_bus, mem_trace)); \
241 } \
242 retval->setMasterInterface(new MasterInterface<Cache<CacheTags<t, comp>, b, c>, Bus>(getInstanceName(), hier, retval, out_bus)); \
243 out_bus->rangeChange(); \
244 return retval; \
245 */return retval; \
246 } while (0)
247
248 #define BUILD_CACHE_PANIC(x) do { \
249 panic("%s not compiled into M5", x); \
250 } while (0)
251
252 #if defined(USE_LZSS_COMPRESSION)
253 #define BUILD_COMPRESSED_CACHE(TAGS, tags, b, c) do { \
254 if (compressed_bus || store_compressed){ \
255 CacheTags<TAGS, LZSSCompression> *tagStore = \
256 new CacheTags<TAGS, LZSSCompression>(tags, \
257 compression_latency, \
258 true, store_compressed, \
259 adaptive_compression, \
260 prefetch_miss); \
261 BUILD_CACHE(TAGS, LZSSCompression, b, c); \
262 } else { \
263 CacheTags<TAGS, NullCompression> *tagStore = \
264 new CacheTags<TAGS, NullCompression>(tags, \
265 compression_latency, \
266 true, store_compressed, \
267 adaptive_compression, \
268 prefetch_miss); \
269 BUILD_CACHE(TAGS, NullCompression, b, c); \
270 } \
271 } while (0)
272 #else
273 #define BUILD_COMPRESSED_CACHE(TAGS, tags, b, c) do { \
274 if (compressed_bus || store_compressed){ \
275 BUILD_CACHE_PANIC("compressed caches"); \
276 } else { \
277 CacheTags<TAGS, NullCompression> *tagStore = \
278 new CacheTags<TAGS, NullCompression>(tags, \
279 compression_latency, \
280 true, store_compressed, \
281 adaptive_compression \
282 prefetch_miss); \
283 BUILD_CACHE(TAGS, NullCompression, b, c); \
284 } \
285 } while (0)
286 #endif
287
288 #if defined(USE_CACHE_FALRU)
289 #define BUILD_FALRU_CACHE(b,c) do { \
290 FALRU *tags = new FALRU(block_size, size, latency); \
291 BUILD_COMPRESSED_CACHE(FALRU, tags, b, c); \
292 } while (0)
293 #else
294 #define BUILD_FALRU_CACHE(b, c) BUILD_CACHE_PANIC("falru cache")
295 #endif
296
297 #if defined(USE_CACHE_LRU)
298 #define BUILD_LRU_CACHE(b, c) do { \
299 LRU *tags = new LRU(numSets, block_size, assoc, latency); \
300 BUILD_COMPRESSED_CACHE(LRU, tags, b, c); \
301 } while (0)
302 #else
303 #define BUILD_LRU_CACHE(b, c) BUILD_CACHE_PANIC("lru cache")
304 #endif
305
306 #if defined(USE_CACHE_SPLIT)
307 #define BUILD_SPLIT_CACHE(b, c) do { \
308 Split *tags = new Split(numSets, block_size, assoc, split_size, lifo, \
309 two_queue, latency); \
310 BUILD_COMPRESSED_CACHE(Split, tags, b, c); \
311 } while (0)
312 #else
313 #define BUILD_SPLIT_CACHE(b, c) BUILD_CACHE_PANIC("split cache")
314 #endif
315
316 #if defined(USE_CACHE_SPLIT_LIFO)
317 #define BUILD_SPLIT_LIFO_CACHE(b, c) do { \
318 SplitLIFO *tags = new SplitLIFO(block_size, size, assoc, \
319 latency, two_queue, -1); \
320 BUILD_COMPRESSED_CACHE(SplitLIFO, tags, b, c); \
321 } while (0)
322 #else
323 #define BUILD_SPLIT_LIFO_CACHE(b, c) BUILD_CACHE_PANIC("lifo cache")
324 #endif
325
326 #if defined(USE_CACHE_IIC)
327 #define BUILD_IIC_CACHE(b ,c) do { \
328 IIC *tags = new IIC(iic_params); \
329 BUILD_COMPRESSED_CACHE(IIC, tags, b, c); \
330 } while (0)
331 #else
332 #define BUILD_IIC_CACHE(b, c) BUILD_CACHE_PANIC("iic")
333 #endif
334
335 #define BUILD_CACHES(b, c) do { \
336 if (repl == NULL) { \
337 if (numSets == 1) { \
338 BUILD_FALRU_CACHE(b, c); \
339 } else { \
340 if (split == true) { \
341 BUILD_SPLIT_CACHE(b, c); \
342 } else if (lifo == true) { \
343 BUILD_SPLIT_LIFO_CACHE(b, c); \
344 } else { \
345 BUILD_LRU_CACHE(b, c); \
346 } \
347 } \
348 } else { \
349 BUILD_IIC_CACHE(b, c); \
350 } \
351 } while (0)
352
353 #define BUILD_COHERENCE(b) do { \
354 if (protocol == NULL) { \
355 UniCoherence *coh = new UniCoherence(); \
356 BUILD_CACHES(b, UniCoherence); \
357 } else { \
358 SimpleCoherence *coh = new SimpleCoherence(protocol); \
359 BUILD_CACHES(b, SimpleCoherence); \
360 } \
361 } while (0)
362
363 #if defined(USE_TAGGED)
364 #define BUILD_TAGGED_PREFETCHER(t, comp, b) pf = new \
365 TaggedPrefetcher<CacheTags<t, comp>, b>(prefetcher_size, \
366 !prefetch_past_page, \
367 prefetch_serial_squash, \
368 prefetch_cache_check_push, \
369 prefetch_data_accesses_only, \
370 prefetch_latency, \
371 prefetch_degree)
372 #else
373 #define BUILD_TAGGED_PREFETCHER(t, comp, b) BUILD_CACHE_PANIC("Tagged Prefetcher")
374 #endif
375
376 #if defined(USE_STRIDED)
377 #define BUILD_STRIDED_PREFETCHER(t, comp, b) pf = new \
378 StridePrefetcher<CacheTags<t, comp>, b>(prefetcher_size, \
379 !prefetch_past_page, \
380 prefetch_serial_squash, \
381 prefetch_cache_check_push, \
382 prefetch_data_accesses_only, \
383 prefetch_latency, \
384 prefetch_degree, \
385 prefetch_use_cpu_id)
386 #else
387 #define BUILD_STRIDED_PREFETCHER(t, comp, b) BUILD_CACHE_PANIC("Stride Prefetcher")
388 #endif
389
390 #if defined(USE_GHB)
391 #define BUILD_GHB_PREFETCHER(t, comp, b) pf = new \
392 GHBPrefetcher<CacheTags<t, comp>, b>(prefetcher_size, \
393 !prefetch_past_page, \
394 prefetch_serial_squash, \
395 prefetch_cache_check_push, \
396 prefetch_data_accesses_only, \
397 prefetch_latency, \
398 prefetch_degree, \
399 prefetch_use_cpu_id)
400 #else
401 #define BUILD_GHB_PREFETCHER(t, comp, b) BUILD_CACHE_PANIC("GHB Prefetcher")
402 #endif
403
404 #if defined(USE_TAGGED)
405 #define BUILD_NULL_PREFETCHER(t, comp, b) pf = new \
406 TaggedPrefetcher<CacheTags<t, comp>, b>(prefetcher_size, \
407 !prefetch_past_page, \
408 prefetch_serial_squash, \
409 prefetch_cache_check_push, \
410 prefetch_data_accesses_only, \
411 prefetch_latency, \
412 prefetch_degree)
413 #else
414 #define BUILD_NULL_PREFETCHER(t, comp, b) BUILD_CACHE_PANIC("NULL Prefetcher (uses Tagged)")
415 #endif
416
417 CREATE_SIM_OBJECT(BaseCache)
418 {
419 string name = getInstanceName();
420 int numSets = size / (assoc * block_size);
421 string pf_policy = prefetch_policy;
422 if (subblock_size == 0) {
423 subblock_size = block_size;
424 }
425
426 // Build BaseCache param object
427 BaseCache::Params base_params(addr_range, latency,
428 block_size, max_miss_count);
429
430 //Warnings about prefetcher policy
431 if (pf_policy == "none" && (prefetch_miss || prefetch_access)) {
432 panic("With no prefetcher, you shouldn't prefetch from"
433 " either miss or access stream\n");
434 }
435 if ((pf_policy == "tagged" || pf_policy == "stride" ||
436 pf_policy == "ghb") && !(prefetch_miss || prefetch_access)) {
437 warn("With this prefetcher you should chose a prefetch"
438 " stream (miss or access)\nNo Prefetching will occur\n");
439 }
440 if ((pf_policy == "tagged" || pf_policy == "stride" ||
441 pf_policy == "ghb") && prefetch_miss && prefetch_access) {
442 panic("Can't do prefetches from both miss and access"
443 " stream\n");
444 }
445 if (pf_policy != "tagged" && pf_policy != "stride" &&
446 pf_policy != "ghb" && pf_policy != "none") {
447 panic("Unrecognized form of a prefetcher: %s, try using"
448 "['none','stride','tagged','ghb']\n", pf_policy);
449 }
450
451 #if defined(USE_CACHE_IIC)
452 // Build IIC params
453 IIC::Params iic_params;
454 iic_params.size = size;
455 iic_params.numSets = numSets;
456 iic_params.blkSize = block_size;
457 iic_params.assoc = assoc;
458 iic_params.hashDelay = hash_delay;
459 iic_params.hitLatency = latency;
460 iic_params.rp = repl;
461 iic_params.subblockSize = subblock_size;
462 #else
463 const void *repl = NULL;
464 #endif
465
466 if (mshrs == 1 /*|| out_bus->doEvents() == false*/) {
467 BlockingBuffer *mq = new BlockingBuffer(true);
468 BUILD_COHERENCE(BlockingBuffer);
469 } else {
470 MissQueue *mq = new MissQueue(mshrs, tgts_per_mshr, write_buffers,
471 true, prefetch_miss);
472 BUILD_COHERENCE(MissQueue);
473 }
474 return NULL;
475 }
476
477 REGISTER_SIM_OBJECT("BaseCache", BaseCache)
478
479
480 #endif //DOXYGEN_SHOULD_SKIP_THIS