2 * Copyright (c) 2002-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
35 * Describes a cache based on template policies.
41 #include "base/compression/base.hh"
42 #include "base/misc.hh" // fatal, panic, and warn
43 #include "cpu/smt.hh" // SMT_MAX_THREADS
45 #include "mem/cache/base_cache.hh"
46 #include "mem/cache/cache_blk.hh"
47 #include "mem/cache/miss/miss_buffer.hh"
54 * A template-policy based cache. The behavior of the cache can be altered by
55 * supplying different template policies. TagStore handles all tag and data
56 * storage @sa TagStore. Buffering handles all misses and writes/writebacks
57 * @sa MissQueue. Coherence handles all coherence policy details @sa
58 * UniCoherence, SimpleMultiCoherence.
60 template <class TagStore, class Coherence>
61 class Cache : public BaseCache
64 /** Define the type of cache block to use. */
65 typedef typename TagStore::BlkType BlkType;
66 /** A typedef for a list of BlkType pointers. */
67 typedef typename TagStore::BlkList BlkList;
73 class CpuSidePort : public CachePort
76 CpuSidePort(const std::string &_name,
77 Cache<TagStore,Coherence> *_cache);
79 // BaseCache::CachePort just has a BaseCache *; this function
80 // lets us get back the type info we lost when we stored the
81 // cache pointer there.
82 Cache<TagStore,Coherence> *myCache() {
83 return static_cast<Cache<TagStore,Coherence> *>(cache);
86 virtual bool recvTiming(PacketPtr pkt);
88 virtual Tick recvAtomic(PacketPtr pkt);
90 virtual void recvFunctional(PacketPtr pkt);
93 class MemSidePort : public CachePort
96 MemSidePort(const std::string &_name,
97 Cache<TagStore,Coherence> *_cache);
99 // BaseCache::CachePort just has a BaseCache *; this function
100 // lets us get back the type info we lost when we stored the
101 // cache pointer there.
102 Cache<TagStore,Coherence> *myCache() {
103 return static_cast<Cache<TagStore,Coherence> *>(cache);
106 virtual bool recvTiming(PacketPtr pkt);
108 virtual Tick recvAtomic(PacketPtr pkt);
110 virtual void recvFunctional(PacketPtr pkt);
113 /** Tag and data Storage */
115 /** Miss and Writeback handler */
116 MissBuffer *missQueue;
117 /** Coherence protocol. */
118 Coherence *coherence;
121 BasePrefetcher *prefetcher;
124 * The clock ratio of the outgoing bus.
125 * Used for calculating critical word first.
130 * The bus width in bytes of the outgoing bus.
131 * Used for calculating critical word first.
136 * The latency of a hit in this device.
141 * A permanent mem req to always be used to cause invalidations.
142 * Used to append to target list, to cause an invalidation.
144 PacketPtr invalidatePkt;
145 Request *invalidateReq;
148 * Policy class for performing compression.
150 CompressionAlgorithm *compressionAlg;
153 * The block size of this cache. Set to value in the Tags object.
155 const int16_t blkSize;
158 * Can this cache should allocate a block on a line-sized write miss.
160 const bool doFastWrites;
162 const bool prefetchMiss;
165 * Can the data can be stored in a compressed form.
167 const bool storeCompressed;
170 * Do we need to compress blocks on writebacks (i.e. because
171 * writeback bus is compressed but storage is not)?
173 const bool compressOnWriteback;
176 * The latency of a compression operation.
178 const int16_t compLatency;
181 * Should we use an adaptive compression scheme.
183 const bool adaptiveCompression;
186 * Do writebacks need to be compressed (i.e. because writeback bus
187 * is compressed), whether or not they're already compressed for
190 const bool writebackCompressed;
193 * Compare the internal block data to the fast access block data.
194 * @param blk The cache block to check.
195 * @return True if the data is the same.
197 bool verifyData(BlkType *blk);
200 * Update the internal data of the block. The data to write is assumed to
201 * be in the fast access data.
202 * @param blk The block with the data to update.
203 * @param writebacks A list to store any generated writebacks.
204 * @param compress_block True if we should compress this block
206 void updateData(BlkType *blk, PacketList &writebacks, bool compress_block);
209 * Handle a replacement for the given request.
210 * @param blk A pointer to the block, usually NULL
211 * @param pkt The memory request to satisfy.
212 * @param new_state The new state of the block.
213 * @param writebacks A list to store any generated writebacks.
215 BlkType* doReplacement(BlkType *blk, PacketPtr &pkt,
216 CacheBlk::State new_state, PacketList &writebacks);
219 * Does all the processing necessary to perform the provided request.
220 * @param pkt The memory request to perform.
221 * @param lat The latency of the access.
222 * @param writebacks List for any writebacks that need to be performed.
223 * @param update True if the replacement data should be updated.
224 * @return Pointer to the cache block touched by the request. NULL if it
227 BlkType* handleAccess(PacketPtr &pkt, int & lat,
228 PacketList & writebacks, bool update = true);
232 *Handle doing the Compare and Swap function for SPARC.
234 void cmpAndSwap(BlkType *blk, PacketPtr &pkt);
237 * Populates a cache block and handles all outstanding requests for the
238 * satisfied fill request. This version takes an MSHR pointer and uses its
239 * request to fill the cache block, while repsonding to its targets.
240 * @param blk The cache block if it already exists.
241 * @param mshr The MSHR that contains the fill data and targets to satisfy.
242 * @param new_state The state of the new cache block.
243 * @param writebacks List for any writebacks that need to be performed.
244 * @return Pointer to the new cache block.
246 BlkType* handleFill(BlkType *blk, MSHR * mshr, CacheBlk::State new_state,
247 PacketList & writebacks, PacketPtr pkt);
250 * Populates a cache block and handles all outstanding requests for the
251 * satisfied fill request. This version takes two memory requests. One
252 * contains the fill data, the other is an optional target to satisfy.
253 * Used for Cache::probe.
254 * @param blk The cache block if it already exists.
255 * @param pkt The memory request with the fill data.
256 * @param new_state The state of the new cache block.
257 * @param writebacks List for any writebacks that need to be performed.
258 * @param target The memory request to perform after the fill.
259 * @return Pointer to the new cache block.
261 BlkType* handleFill(BlkType *blk, PacketPtr &pkt,
262 CacheBlk::State new_state,
263 PacketList & writebacks, PacketPtr target = NULL);
266 * Sets the blk to the new state and handles the given request.
267 * @param blk The cache block being snooped.
268 * @param new_state The new coherence state for the block.
269 * @param pkt The request to satisfy
271 void handleSnoop(BlkType *blk, CacheBlk::State new_state,
275 * Sets the blk to the new state.
276 * @param blk The cache block being snooped.
277 * @param new_state The new coherence state for the block.
279 void handleSnoop(BlkType *blk, CacheBlk::State new_state);
282 * Create a writeback request for the given block.
283 * @param blk The block to writeback.
284 * @return The writeback request for the block.
286 PacketPtr writebackBlk(BlkType *blk);
294 MissBuffer *missQueue;
295 Coherence *coherence;
296 BaseCache::Params baseParams;
297 BasePrefetcher*prefetcher;
300 CompressionAlgorithm *compressionAlg;
301 const int16_t blkSize;
302 const bool doFastWrites;
303 const bool prefetchMiss;
304 const bool storeCompressed;
305 const bool compressOnWriteback;
306 const int16_t compLatency;
307 const bool adaptiveCompression;
308 const bool writebackCompressed;
310 Params(TagStore *_tags, MissBuffer *mq, Coherence *coh,
311 BaseCache::Params params,
312 BasePrefetcher *_prefetcher,
313 bool prefetch_access, int hit_latency,
315 bool store_compressed, bool adaptive_compression,
316 bool writeback_compressed,
317 CompressionAlgorithm *_compressionAlg, int comp_latency,
319 : tags(_tags), missQueue(mq), coherence(coh),
321 prefetcher(_prefetcher), prefetchAccess(prefetch_access),
322 hitLatency(hit_latency),
323 compressionAlg(_compressionAlg),
324 blkSize(_tags->getBlockSize()),
325 doFastWrites(do_fast_writes),
326 prefetchMiss(prefetch_miss),
327 storeCompressed(store_compressed),
328 compressOnWriteback(!store_compressed && writeback_compressed),
329 compLatency(comp_latency),
330 adaptiveCompression(adaptive_compression),
331 writebackCompressed(writeback_compressed)
336 /** Instantiates a basic cache object. */
337 Cache(const std::string &_name, Params ¶ms);
339 virtual Port *getPort(const std::string &if_name, int idx = -1);
340 virtual void deletePortRefs(Port *p);
342 virtual void recvStatusChange(Port::Status status, bool isCpuSide);
347 * Performs the access specified by the request.
348 * @param pkt The request to perform.
349 * @return The result of the access.
351 bool access(PacketPtr &pkt);
354 * Selects a request to send on the bus.
355 * @return The memory request to service.
357 virtual PacketPtr getPacket();
360 * Was the request was sent successfully?
361 * @param pkt The request.
362 * @param success True if the request was sent successfully.
364 virtual void sendResult(PacketPtr &pkt, MSHR* mshr, bool success);
367 * Was the CSHR request was sent successfully?
368 * @param pkt The request.
369 * @param success True if the request was sent successfully.
371 virtual void sendCoherenceResult(PacketPtr &pkt, MSHR* cshr, bool success);
374 * Handles a response (cache line fill/write ack) from the bus.
375 * @param pkt The request being responded to.
377 void handleResponse(PacketPtr &pkt);
380 * Selects a coherence message to forward to lower levels of the hierarchy.
381 * @return The coherence message to forward.
383 virtual PacketPtr getCoherencePacket();
386 * Snoops bus transactions to maintain coherence.
387 * @param pkt The current bus transaction.
389 void snoop(PacketPtr &pkt);
391 void snoopResponse(PacketPtr &pkt);
394 * Squash all requests associated with specified thread.
395 * intended for use by I-cache.
396 * @param threadNum The thread to squash.
398 void squash(int threadNum)
400 missQueue->squash(threadNum);
404 * Return the number of outstanding misses in a Cache.
407 * @retval unsigned The number of missing still outstanding.
409 unsigned outstandingMisses() const
411 return missQueue->getMisses();
415 * Perform the access specified in the request and return the estimated
416 * time of completion. This function can either update the hierarchy state
417 * or just perform the access wherever the data is found depending on the
418 * state of the update flag.
419 * @param pkt The memory request to satisfy
420 * @param update If true, update the hierarchy, otherwise just perform the
422 * @return The estimated completion time.
424 Tick probe(PacketPtr &pkt, bool update, CachePort * otherSidePort);
427 * Snoop for the provided request in the cache and return the estimated
428 * time of completion.
429 * @todo Can a snoop probe not change state?
430 * @param pkt The memory request to satisfy
431 * @param update If true, update the hierarchy, otherwise just perform the
433 * @return The estimated completion time.
435 Tick snoopProbe(PacketPtr &pkt);
437 bool inCache(Addr addr) {
438 return (tags->findBlock(addr) != 0);
441 bool inMissQueue(Addr addr) {
442 return (missQueue->findMSHR(addr) != 0);
446 #endif // __CACHE_HH__