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"
45 #include "mem/bus/bus.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"
88 #include "mem/bus/slave_interface.hh"
89 #include "mem/bus/master_interface.hh"
90 #include "mem/memory_interface.hh"
92 #include "mem/trace/mem_trace_writer.hh"
96 #include "mem/cache/prefetch/ghb_prefetcher.hh"
98 #if defined(USE_TAGGED)
99 #include "mem/cache/prefetch/tagged_prefetcher.hh"
101 #if defined(USE_STRIDED)
102 #include "mem/cache/prefetch/stride_prefetcher.hh"
107 using namespace TheISA
;
109 #ifndef DOXYGEN_SHOULD_SKIP_THIS
111 BEGIN_DECLARE_SIM_OBJECT_PARAMS(BaseCache
)
115 Param
<int> block_size
;
118 Param
<int> tgts_per_mshr
;
119 Param
<int> write_buffers
;
120 Param
<bool> prioritizeRequests
;
121 SimObjectParam
<Bus
*> in_bus
;
122 SimObjectParam
<Bus
*> out_bus
;
124 SimObjectParam
<CoherenceProtocol
*> protocol
;
125 Param
<Addr
> trace_addr
;
126 Param
<int> hash_delay
;
127 #if defined(USE_CACHE_IIC)
128 SimObjectParam
<Repl
*> repl
;
130 Param
<bool> compressed_bus
;
131 Param
<bool> store_compressed
;
132 Param
<bool> adaptive_compression
;
133 Param
<int> compression_latency
;
134 Param
<int> subblock_size
;
135 Param
<Counter
> max_miss_count
;
136 SimObjectParam
<HierParams
*> hier
;
137 VectorParam
<Range
<Addr
> > addr_range
;
138 SimObjectParam
<MemTraceWriter
*> mem_trace
;
140 Param
<int> split_size
;
142 Param
<bool> two_queue
;
143 Param
<bool> prefetch_miss
;
144 Param
<bool> prefetch_access
;
145 Param
<int> prefetcher_size
;
146 Param
<bool> prefetch_past_page
;
147 Param
<bool> prefetch_serial_squash
;
148 Param
<Tick
> prefetch_latency
;
149 Param
<int> prefetch_degree
;
150 Param
<string
> prefetch_policy
;
151 Param
<bool> prefetch_cache_check_push
;
152 Param
<bool> prefetch_use_cpu_id
;
153 Param
<bool> prefetch_data_accesses_only
;
155 END_DECLARE_SIM_OBJECT_PARAMS(BaseCache
)
158 BEGIN_INIT_SIM_OBJECT_PARAMS(BaseCache
)
160 INIT_PARAM(size
, "capacity in bytes"),
161 INIT_PARAM(assoc
, "associativity"),
162 INIT_PARAM(block_size
, "block size in bytes"),
163 INIT_PARAM(latency
, "hit latency in CPU cycles"),
164 INIT_PARAM(mshrs
, "number of MSHRs (max outstanding requests)"),
165 INIT_PARAM(tgts_per_mshr
, "max number of accesses per MSHR"),
166 INIT_PARAM_DFLT(write_buffers
, "number of write buffers", 8),
167 INIT_PARAM_DFLT(prioritizeRequests
, "always service demand misses first",
169 INIT_PARAM_DFLT(in_bus
, "incoming bus object", NULL
),
170 INIT_PARAM(out_bus
, "outgoing bus object"),
171 INIT_PARAM_DFLT(do_copy
, "perform fast copies in the cache", false),
172 INIT_PARAM_DFLT(protocol
, "coherence protocol to use in the cache", NULL
),
173 INIT_PARAM_DFLT(trace_addr
, "address to trace", 0),
175 INIT_PARAM_DFLT(hash_delay
, "time in cycles of hash access",1),
176 #if defined(USE_CACHE_IIC)
177 INIT_PARAM_DFLT(repl
, "replacement policy",NULL
),
179 INIT_PARAM_DFLT(compressed_bus
,
180 "This cache connects to a compressed memory",
182 INIT_PARAM_DFLT(store_compressed
, "Store compressed data in the cache",
184 INIT_PARAM_DFLT(adaptive_compression
, "Use an adaptive compression scheme",
186 INIT_PARAM_DFLT(compression_latency
,
187 "Latency in cycles of compression algorithm",
189 INIT_PARAM_DFLT(subblock_size
,
190 "Size of subblock in IIC used for compression",
192 INIT_PARAM_DFLT(max_miss_count
,
193 "The number of misses to handle before calling exit",
195 INIT_PARAM_DFLT(hier
,
196 "Hierarchy global variables",
198 INIT_PARAM_DFLT(addr_range
, "The address range in bytes",
199 vector
<Range
<Addr
> >(1,RangeIn((Addr
)0, MaxAddr
))),
200 INIT_PARAM_DFLT(mem_trace
, "Memory trace to write accesses to", NULL
),
201 INIT_PARAM_DFLT(split
, "Whether this is a partitioned cache", false),
202 INIT_PARAM_DFLT(split_size
, "the number of \"ways\" belonging to the LRU partition", 0),
203 INIT_PARAM_DFLT(lifo
, "whether you are using a LIFO repl. policy", false),
204 INIT_PARAM_DFLT(two_queue
, "whether the lifo should have two queue replacement", false),
205 INIT_PARAM_DFLT(prefetch_miss
, "wheter you are using the hardware prefetcher from Miss stream", false),
206 INIT_PARAM_DFLT(prefetch_access
, "wheter you are using the hardware prefetcher from Access stream", false),
207 INIT_PARAM_DFLT(prefetcher_size
, "Number of entries in the harware prefetch queue", 100),
208 INIT_PARAM_DFLT(prefetch_past_page
, "Allow prefetches to cross virtual page boundaries", false),
209 INIT_PARAM_DFLT(prefetch_serial_squash
, "Squash prefetches with a later time on a subsequent miss", false),
210 INIT_PARAM_DFLT(prefetch_latency
, "Latency of the prefetcher", 10),
211 INIT_PARAM_DFLT(prefetch_degree
, "Degree of the prefetch depth", 1),
212 INIT_PARAM_DFLT(prefetch_policy
, "Type of prefetcher to use", "none"),
213 INIT_PARAM_DFLT(prefetch_cache_check_push
, "Check if in cash on push or pop of prefetch queue", true),
214 INIT_PARAM_DFLT(prefetch_use_cpu_id
, "Use the CPU ID to seperate calculations of prefetches", true),
215 INIT_PARAM_DFLT(prefetch_data_accesses_only
, "Only prefetch on data not on instruction accesses", false)
216 END_INIT_SIM_OBJECT_PARAMS(BaseCache
)
219 #define BUILD_CACHE(t, comp, b, c) do { \
220 Prefetcher<CacheTags<t, comp>, b> *pf; \
221 if (pf_policy == "tagged") { \
222 BUILD_TAGGED_PREFETCHER(t, comp, b); \
224 else if (pf_policy == "stride") { \
225 BUILD_STRIDED_PREFETCHER(t, comp, b); \
227 else if (pf_policy == "ghb") { \
228 BUILD_GHB_PREFETCHER(t, comp, b); \
231 BUILD_NULL_PREFETCHER(t, comp, b); \
233 Cache<CacheTags<t, comp>, b, c>::Params params(tagStore, mq, coh, \
234 do_copy, base_params, \
235 in_bus, out_bus, pf, \
237 Cache<CacheTags<t, comp>, b, c> *retval = \
238 new Cache<CacheTags<t, comp>, b, c>(getInstanceName(), hier, \
240 if (in_bus == NULL) { \
241 retval->setSlaveInterface(new MemoryInterface<Cache<CacheTags<t, comp>, b, c> >(getInstanceName(), hier, retval, mem_trace)); \
243 retval->setSlaveInterface(new SlaveInterface<Cache<CacheTags<t, comp>, b, c>, Bus>(getInstanceName(), hier, retval, in_bus, mem_trace)); \
245 retval->setMasterInterface(new MasterInterface<Cache<CacheTags<t, comp>, b, c>, Bus>(getInstanceName(), hier, retval, out_bus)); \
246 out_bus->rangeChange(); \
250 #define BUILD_CACHE_PANIC(x) do { \
251 panic("%s not compiled into M5", x); \
254 #if defined(USE_LZSS_COMPRESSION)
255 #define BUILD_COMPRESSED_CACHE(TAGS, tags, b, c) do { \
256 if (compressed_bus || store_compressed){ \
257 CacheTags<TAGS, LZSSCompression> *tagStore = \
258 new CacheTags<TAGS, LZSSCompression>(tags, \
259 compression_latency, \
260 true, store_compressed, \
261 adaptive_compression, \
263 BUILD_CACHE(TAGS, LZSSCompression, b, c); \
265 CacheTags<TAGS, NullCompression> *tagStore = \
266 new CacheTags<TAGS, NullCompression>(tags, \
267 compression_latency, \
268 true, store_compressed, \
269 adaptive_compression, \
271 BUILD_CACHE(TAGS, NullCompression, b, c); \
275 #define BUILD_COMPRESSED_CACHE(TAGS, tags, b, c) do { \
276 if (compressed_bus || store_compressed){ \
277 BUILD_CACHE_PANIC("compressed caches"); \
279 CacheTags<TAGS, NullCompression> *tagStore = \
280 new CacheTags<TAGS, NullCompression>(tags, \
281 compression_latency, \
282 true, store_compressed, \
283 adaptive_compression \
285 BUILD_CACHE(TAGS, NullCompression, b, c); \
290 #if defined(USE_CACHE_FALRU)
291 #define BUILD_FALRU_CACHE(b,c) do { \
292 FALRU *tags = new FALRU(block_size, size, latency); \
293 BUILD_COMPRESSED_CACHE(FALRU, tags, b, c); \
296 #define BUILD_FALRU_CACHE(b, c) BUILD_CACHE_PANIC("falru cache")
299 #if defined(USE_CACHE_LRU)
300 #define BUILD_LRU_CACHE(b, c) do { \
301 LRU *tags = new LRU(numSets, block_size, assoc, latency); \
302 BUILD_COMPRESSED_CACHE(LRU, tags, b, c); \
305 #define BUILD_LRU_CACHE(b, c) BUILD_CACHE_PANIC("lru cache")
308 #if defined(USE_CACHE_SPLIT)
309 #define BUILD_SPLIT_CACHE(b, c) do { \
310 Split *tags = new Split(numSets, block_size, assoc, split_size, lifo, \
311 two_queue, latency); \
312 BUILD_COMPRESSED_CACHE(Split, tags, b, c); \
315 #define BUILD_SPLIT_CACHE(b, c) BUILD_CACHE_PANIC("split cache")
318 #if defined(USE_CACHE_SPLIT_LIFO)
319 #define BUILD_SPLIT_LIFO_CACHE(b, c) do { \
320 SplitLIFO *tags = new SplitLIFO(block_size, size, assoc, \
321 latency, two_queue, -1); \
322 BUILD_COMPRESSED_CACHE(SplitLIFO, tags, b, c); \
325 #define BUILD_SPLIT_LIFO_CACHE(b, c) BUILD_CACHE_PANIC("lifo cache")
328 #if defined(USE_CACHE_IIC)
329 #define BUILD_IIC_CACHE(b ,c) do { \
330 IIC *tags = new IIC(iic_params); \
331 BUILD_COMPRESSED_CACHE(IIC, tags, b, c); \
334 #define BUILD_IIC_CACHE(b, c) BUILD_CACHE_PANIC("iic")
337 #define BUILD_CACHES(b, c) do { \
338 if (repl == NULL) { \
339 if (numSets == 1) { \
340 BUILD_FALRU_CACHE(b, c); \
342 if (split == true) { \
343 BUILD_SPLIT_CACHE(b, c); \
344 } else if (lifo == true) { \
345 BUILD_SPLIT_LIFO_CACHE(b, c); \
347 BUILD_LRU_CACHE(b, c); \
351 BUILD_IIC_CACHE(b, c); \
355 #define BUILD_COHERENCE(b) do { \
356 if (protocol == NULL) { \
357 UniCoherence *coh = new UniCoherence(); \
358 BUILD_CACHES(b, UniCoherence); \
360 SimpleCoherence *coh = new SimpleCoherence(protocol); \
361 BUILD_CACHES(b, SimpleCoherence); \
365 #if defined(USE_TAGGED)
366 #define BUILD_TAGGED_PREFETCHER(t, comp, b) pf = new \
367 TaggedPrefetcher<CacheTags<t, comp>, b>(prefetcher_size, \
368 !prefetch_past_page, \
369 prefetch_serial_squash, \
370 prefetch_cache_check_push, \
371 prefetch_data_accesses_only, \
375 #define BUILD_TAGGED_PREFETCHER(t, comp, b) BUILD_CACHE_PANIC("Tagged Prefetcher")
378 #if defined(USE_STRIDED)
379 #define BUILD_STRIDED_PREFETCHER(t, comp, b) pf = new \
380 StridePrefetcher<CacheTags<t, comp>, b>(prefetcher_size, \
381 !prefetch_past_page, \
382 prefetch_serial_squash, \
383 prefetch_cache_check_push, \
384 prefetch_data_accesses_only, \
389 #define BUILD_STRIDED_PREFETCHER(t, comp, b) BUILD_CACHE_PANIC("Stride Prefetcher")
393 #define BUILD_GHB_PREFETCHER(t, comp, b) pf = new \
394 GHBPrefetcher<CacheTags<t, comp>, b>(prefetcher_size, \
395 !prefetch_past_page, \
396 prefetch_serial_squash, \
397 prefetch_cache_check_push, \
398 prefetch_data_accesses_only, \
403 #define BUILD_GHB_PREFETCHER(t, comp, b) BUILD_CACHE_PANIC("GHB Prefetcher")
406 #if defined(USE_TAGGED)
407 #define BUILD_NULL_PREFETCHER(t, comp, b) pf = new \
408 TaggedPrefetcher<CacheTags<t, comp>, b>(prefetcher_size, \
409 !prefetch_past_page, \
410 prefetch_serial_squash, \
411 prefetch_cache_check_push, \
412 prefetch_data_accesses_only, \
416 #define BUILD_NULL_PREFETCHER(t, comp, b) BUILD_CACHE_PANIC("NULL Prefetcher (uses Tagged)")
419 CREATE_SIM_OBJECT(BaseCache
)
421 string name
= getInstanceName();
422 int numSets
= size
/ (assoc
* block_size
);
423 string pf_policy
= prefetch_policy
;
424 if (subblock_size
== 0) {
425 subblock_size
= block_size
;
428 // Build BaseCache param object
429 BaseCache::Params
base_params(addr_range
, latency
,
430 block_size
, max_miss_count
);
432 //Warnings about prefetcher policy
433 if (pf_policy
== "none" && (prefetch_miss
|| prefetch_access
)) {
434 panic("With no prefetcher, you shouldn't prefetch from"
435 " either miss or access stream\n");
437 if ((pf_policy
== "tagged" || pf_policy
== "stride" ||
438 pf_policy
== "ghb") && !(prefetch_miss
|| prefetch_access
)) {
439 warn("With this prefetcher you should chose a prefetch"
440 " stream (miss or access)\nNo Prefetching will occur\n");
442 if ((pf_policy
== "tagged" || pf_policy
== "stride" ||
443 pf_policy
== "ghb") && prefetch_miss
&& prefetch_access
) {
444 panic("Can't do prefetches from both miss and access"
447 if (pf_policy
!= "tagged" && pf_policy
!= "stride" &&
448 pf_policy
!= "ghb" && pf_policy
!= "none") {
449 panic("Unrecognized form of a prefetcher: %s, try using"
450 "['none','stride','tagged','ghb']\n", pf_policy
);
453 #if defined(USE_CACHE_IIC)
455 IIC::Params iic_params
;
456 iic_params
.size
= size
;
457 iic_params
.numSets
= numSets
;
458 iic_params
.blkSize
= block_size
;
459 iic_params
.assoc
= assoc
;
460 iic_params
.hashDelay
= hash_delay
;
461 iic_params
.hitLatency
= latency
;
462 iic_params
.rp
= repl
;
463 iic_params
.subblockSize
= subblock_size
;
465 const void *repl
= NULL
;
468 if (mshrs
== 1 || out_bus
->doEvents() == false) {
469 BlockingBuffer
*mq
= new BlockingBuffer(true);
470 BUILD_COHERENCE(BlockingBuffer
);
472 MissQueue
*mq
= new MissQueue(mshrs
, tgts_per_mshr
, write_buffers
,
473 true, prefetch_miss
);
474 BUILD_COHERENCE(MissQueue
);
479 REGISTER_SIM_OBJECT("BaseCache", BaseCache
)
482 #endif //DOXYGEN_SHOULD_SKIP_THIS