2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Erik Hallnor
34 * Simobject instatiation of caches.
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"
43 #include "mem/cache/base_cache.hh"
44 #include "mem/cache/cache.hh"
46 #include "mem/cache/coherence/coherence_protocol.hh"
47 #include "sim/builder.hh"
50 #if defined(USE_CACHE_LRU)
51 #include "mem/cache/tags/lru.hh"
54 #if defined(USE_CACHE_FALRU)
55 #include "mem/cache/tags/fa_lru.hh"
58 #if defined(USE_CACHE_IIC)
59 #include "mem/cache/tags/iic.hh"
62 #if defined(USE_CACHE_SPLIT)
63 #include "mem/cache/tags/split.hh"
66 #if defined(USE_CACHE_SPLIT_LIFO)
67 #include "mem/cache/tags/split_lifo.hh"
70 // Compression Templates
71 #include "base/compression/null_compression.hh"
72 #if defined(USE_LZSS_COMPRESSION)
73 #include "base/compression/lzss_compression.hh"
76 // CacheTags Templates
77 #include "mem/cache/tags/cache_tags.hh"
79 // MissQueue Templates
80 #include "mem/cache/miss/miss_queue.hh"
81 #include "mem/cache/miss/blocking_buffer.hh"
83 // Coherence Templates
84 #include "mem/cache/coherence/uni_coherence.hh"
85 #include "mem/cache/coherence/simple_coherence.hh"
89 #include "mem/cache/prefetch/ghb_prefetcher.hh"
91 #if defined(USE_TAGGED)
92 #include "mem/cache/prefetch/tagged_prefetcher.hh"
94 #if defined(USE_STRIDED)
95 #include "mem/cache/prefetch/stride_prefetcher.hh"
100 using namespace TheISA
;
102 #ifndef DOXYGEN_SHOULD_SKIP_THIS
104 BEGIN_DECLARE_SIM_OBJECT_PARAMS(BaseCache
)
108 Param
<int> block_size
;
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;
117 SimObjectParam
<CoherenceProtocol
*> protocol
;
118 Param
<Addr
> trace_addr
;
119 Param
<int> hash_delay
;
120 #if defined(USE_CACHE_IIC)
121 SimObjectParam
<Repl
*> repl
;
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;
133 Param
<int> split_size
;
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
;
149 END_DECLARE_SIM_OBJECT_PARAMS(BaseCache
)
152 BEGIN_INIT_SIM_OBJECT_PARAMS(BaseCache
)
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",
163 /* INIT_PARAM_DFLT(in_bus, "incoming bus object", NULL),
164 INIT_PARAM(out_bus, "outgoing bus object"),
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),
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
),
174 INIT_PARAM_DFLT(compressed_bus
,
175 "This cache connects to a compressed memory",
177 INIT_PARAM_DFLT(store_compressed
, "Store compressed data in the cache",
179 INIT_PARAM_DFLT(adaptive_compression
, "Use an adaptive compression scheme",
181 INIT_PARAM_DFLT(compression_latency
,
182 "Latency in cycles of compression algorithm",
184 INIT_PARAM_DFLT(subblock_size
,
185 "Size of subblock in IIC used for compression",
187 INIT_PARAM_DFLT(max_miss_count
,
188 "The number of misses to handle before calling exit",
190 /* INIT_PARAM_DFLT(hier,
191 "Hierarchy global variables",
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
)
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); \
221 else if (pf_policy == "stride") { \
222 BUILD_STRIDED_PREFETCHER(t, comp, b); \
224 else if (pf_policy == "ghb") { \
225 BUILD_GHB_PREFETCHER(t, comp, b); \
228 BUILD_NULL_PREFETCHER(t, comp, b); \
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,*/ \
237 /* if (in_bus == NULL) { \
238 retval->setSlaveInterface(new MemoryInterface<Cache<CacheTags<t, comp>, b, c> >(getInstanceName(), hier, retval, mem_trace)); \
240 retval->setSlaveInterface(new SlaveInterface<Cache<CacheTags<t, comp>, b, c>, Bus>(getInstanceName(), hier, retval, in_bus, mem_trace)); \
242 retval->setMasterInterface(new MasterInterface<Cache<CacheTags<t, comp>, b, c>, Bus>(getInstanceName(), hier, retval, out_bus)); \
243 out_bus->rangeChange(); \
248 #define BUILD_CACHE_PANIC(x) do { \
249 panic("%s not compiled into M5", x); \
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, \
261 BUILD_CACHE(TAGS, LZSSCompression, b, c); \
263 CacheTags<TAGS, NullCompression> *tagStore = \
264 new CacheTags<TAGS, NullCompression>(tags, \
265 compression_latency, \
266 true, store_compressed, \
267 adaptive_compression, \
269 BUILD_CACHE(TAGS, NullCompression, b, c); \
273 #define BUILD_COMPRESSED_CACHE(TAGS, tags, b, c) do { \
274 if (compressed_bus || store_compressed){ \
275 BUILD_CACHE_PANIC("compressed caches"); \
277 CacheTags<TAGS, NullCompression> *tagStore = \
278 new CacheTags<TAGS, NullCompression>(tags, \
279 compression_latency, \
280 true, store_compressed, \
281 adaptive_compression \
283 BUILD_CACHE(TAGS, NullCompression, b, c); \
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); \
294 #define BUILD_FALRU_CACHE(b, c) BUILD_CACHE_PANIC("falru cache")
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); \
303 #define BUILD_LRU_CACHE(b, c) BUILD_CACHE_PANIC("lru cache")
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); \
313 #define BUILD_SPLIT_CACHE(b, c) BUILD_CACHE_PANIC("split cache")
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); \
323 #define BUILD_SPLIT_LIFO_CACHE(b, c) BUILD_CACHE_PANIC("lifo cache")
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); \
332 #define BUILD_IIC_CACHE(b, c) BUILD_CACHE_PANIC("iic")
335 #define BUILD_CACHES(b, c) do { \
336 if (repl == NULL) { \
337 if (numSets == 1) { \
338 BUILD_FALRU_CACHE(b, c); \
340 if (split == true) { \
341 BUILD_SPLIT_CACHE(b, c); \
342 } else if (lifo == true) { \
343 BUILD_SPLIT_LIFO_CACHE(b, c); \
345 BUILD_LRU_CACHE(b, c); \
349 BUILD_IIC_CACHE(b, c); \
353 #define BUILD_COHERENCE(b) do { \
354 if (protocol == NULL) { \
355 UniCoherence *coh = new UniCoherence(); \
356 BUILD_CACHES(b, UniCoherence); \
358 SimpleCoherence *coh = new SimpleCoherence(protocol); \
359 BUILD_CACHES(b, SimpleCoherence); \
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, \
373 #define BUILD_TAGGED_PREFETCHER(t, comp, b) BUILD_CACHE_PANIC("Tagged Prefetcher")
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, \
387 #define BUILD_STRIDED_PREFETCHER(t, comp, b) BUILD_CACHE_PANIC("Stride Prefetcher")
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, \
401 #define BUILD_GHB_PREFETCHER(t, comp, b) BUILD_CACHE_PANIC("GHB Prefetcher")
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, \
414 #define BUILD_NULL_PREFETCHER(t, comp, b) BUILD_CACHE_PANIC("NULL Prefetcher (uses Tagged)")
417 CREATE_SIM_OBJECT(BaseCache
)
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
;
426 // Build BaseCache param object
427 BaseCache::Params
base_params(addr_range
, latency
,
428 block_size
, max_miss_count
);
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");
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");
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"
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
);
451 #if defined(USE_CACHE_IIC)
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
;
463 const void *repl
= NULL
;
466 if (mshrs
== 1 /*|| out_bus->doEvents() == false*/) {
467 BlockingBuffer
*mq
= new BlockingBuffer(true);
468 BUILD_COHERENCE(BlockingBuffer
);
470 MissQueue
*mq
= new MissQueue(mshrs
, tgts_per_mshr
, write_buffers
,
471 true, prefetch_miss
);
472 BUILD_COHERENCE(MissQueue
);
477 REGISTER_SIM_OBJECT("BaseCache", BaseCache
)
480 #endif //DOXYGEN_SHOULD_SKIP_THIS