2 * Copyright (c) 2012 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 * Authors: Erik Hallnor
49 * Describes a cache based on template policies.
55 #include "base/misc.hh" // fatal, panic, and warn
56 #include "mem/cache/base.hh"
57 #include "mem/cache/blk.hh"
58 #include "mem/cache/mshr.hh"
59 #include "sim/eventq.hh"
65 * A template-policy based cache. The behavior of the cache can be altered by
66 * supplying different template policies. TagStore handles all tag and data
67 * storage @sa TagStore.
69 template <class TagStore>
70 class Cache : public BaseCache
73 /** Define the type of cache block to use. */
74 typedef typename TagStore::BlkType BlkType;
75 /** A typedef for a list of BlkType pointers. */
76 typedef typename TagStore::BlkList BlkList;
81 * The CPU-side port extends the base cache slave port with access
82 * functions for functional, atomic and timing requests.
84 class CpuSidePort : public CacheSlavePort
88 // a pointer to our specific cache implementation
89 Cache<TagStore> *cache;
93 virtual bool recvTiming(PacketPtr pkt);
95 virtual Tick recvAtomic(PacketPtr pkt);
97 virtual void recvFunctional(PacketPtr pkt);
99 virtual unsigned deviceBlockSize() const
100 { return cache->getBlockSize(); }
102 virtual AddrRangeList getAddrRanges();
106 CpuSidePort(const std::string &_name, Cache<TagStore> *_cache,
107 const std::string &_label);
112 * Override the default behaviour of sendDeferredPacket to enable
113 * the memory-side cache port to also send requests based on the
114 * current MSHR status. This queue has a pointer to our specific
115 * cache implementation and is used by the MemSidePort.
117 class MemSidePacketQueue : public PacketQueue
122 Cache<TagStore> &cache;
126 MemSidePacketQueue(Cache<TagStore> &cache, Port &port,
127 const std::string &label) :
128 PacketQueue(cache, port, label), cache(cache) { }
131 * Override the normal sendDeferredPacket and do not only
132 * consider the transmit list (used for responses), but also
135 virtual void sendDeferredPacket();
140 * The memory-side port extends the base cache master port with
141 * access functions for functional, atomic and timing snoops.
143 class MemSidePort : public CacheMasterPort
147 /** The cache-specific queue. */
148 MemSidePacketQueue _queue;
150 // a pointer to our specific cache implementation
151 Cache<TagStore> *cache;
155 virtual bool recvTiming(PacketPtr pkt);
157 virtual Tick recvAtomic(PacketPtr pkt);
159 virtual void recvFunctional(PacketPtr pkt);
161 virtual unsigned deviceBlockSize() const
162 { return cache->getBlockSize(); }
166 MemSidePort(const std::string &_name, Cache<TagStore> *_cache,
167 const std::string &_label);
170 /** Tag and data Storage */
174 BasePrefetcher *prefetcher;
176 /** Temporary cache block for occasional transitory use */
180 * This cache should allocate a block on a line-sized write miss.
182 const bool doFastWrites;
185 * Notify the prefetcher on every access, not just misses.
187 const bool prefetchOnAccess;
190 * Does all the processing necessary to perform the provided request.
191 * @param pkt The memory request to perform.
192 * @param lat The latency of the access.
193 * @param writebacks List for any writebacks that need to be performed.
194 * @param update True if the replacement data should be updated.
195 * @return Boolean indicating whether the request was satisfied.
197 bool access(PacketPtr pkt, BlkType *&blk,
198 int &lat, PacketList &writebacks);
201 *Handle doing the Compare and Swap function for SPARC.
203 void cmpAndSwap(BlkType *blk, PacketPtr pkt);
206 * Find a block frame for new block at address addr, assuming that
207 * the block is not currently in the cache. Append writebacks if
208 * any to provided packet list. Return free block frame. May
209 * return NULL if there are no replaceable blocks at the moment.
211 BlkType *allocateBlock(Addr addr, PacketList &writebacks);
214 * Populates a cache block and handles all outstanding requests for the
215 * satisfied fill request. This version takes two memory requests. One
216 * contains the fill data, the other is an optional target to satisfy.
217 * @param pkt The memory request with the fill data.
218 * @param blk The cache block if it already exists.
219 * @param writebacks List for any writebacks that need to be performed.
220 * @return Pointer to the new cache block.
222 BlkType *handleFill(PacketPtr pkt, BlkType *blk,
223 PacketList &writebacks);
225 void satisfyCpuSideRequest(PacketPtr pkt, BlkType *blk,
226 bool deferred_response = false,
227 bool pending_downgrade = false);
228 bool satisfyMSHR(MSHR *mshr, PacketPtr pkt, BlkType *blk);
230 void doTimingSupplyResponse(PacketPtr req_pkt, uint8_t *blk_data,
231 bool already_copied, bool pending_inval);
234 * Sets the blk to the new state.
235 * @param blk The cache block being snooped.
236 * @param new_state The new coherence state for the block.
238 void handleSnoop(PacketPtr ptk, BlkType *blk,
239 bool is_timing, bool is_deferred, bool pending_inval);
242 * Create a writeback request for the given block.
243 * @param blk The block to writeback.
244 * @return The writeback request for the block.
246 PacketPtr writebackBlk(BlkType *blk);
249 /** Instantiates a basic cache object. */
250 Cache(const Params *p, TagStore *tags);
252 virtual Port *getPort(const std::string &if_name, int idx = -1);
257 * Performs the access specified by the request.
258 * @param pkt The request to perform.
259 * @return The result of the access.
261 bool timingAccess(PacketPtr pkt);
264 * Performs the access specified by the request.
265 * @param pkt The request to perform.
266 * @return The result of the access.
268 Tick atomicAccess(PacketPtr pkt);
271 * Performs the access specified by the request.
272 * @param pkt The request to perform.
273 * @param fromCpuSide from the CPU side port or the memory side port
275 void functionalAccess(PacketPtr pkt, bool fromCpuSide);
278 * Handles a response (cache line fill/write ack) from the bus.
279 * @param pkt The request being responded to.
281 void handleResponse(PacketPtr pkt);
284 * Snoops bus transactions to maintain coherence.
285 * @param pkt The current bus transaction.
287 void snoopTiming(PacketPtr pkt);
290 * Snoop for the provided request in the cache and return the estimated
291 * time of completion.
292 * @param pkt The memory request to snoop
293 * @return The estimated completion time.
295 Tick snoopAtomic(PacketPtr pkt);
298 * Squash all requests associated with specified thread.
299 * intended for use by I-cache.
300 * @param threadNum The thread to squash.
302 void squash(int threadNum);
305 * Generate an appropriate downstream bus request packet for the
307 * @param cpu_pkt The upstream request that needs to be satisfied.
308 * @param blk The block currently in the cache corresponding to
309 * cpu_pkt (NULL if none).
310 * @param needsExclusive Indicates that an exclusive copy is required
311 * even if the request in cpu_pkt doesn't indicate that.
312 * @return A new Packet containing the request, or NULL if the
313 * current request in cpu_pkt should just be forwarded on.
315 PacketPtr getBusPacket(PacketPtr cpu_pkt, BlkType *blk,
316 bool needsExclusive);
319 * Return the next MSHR to service, either a pending miss from the
320 * mshrQueue, a buffered write from the write buffer, or something
321 * from the prefetcher. This function is responsible for
322 * prioritizing among those sources on the fly.
327 * Selects an outstanding request to service. Called when the
328 * cache gets granted the downstream bus in timing mode.
329 * @return The request to service, NULL if none found.
331 PacketPtr getTimingPacket();
334 * Marks a request as in service (sent on the bus). This can have side
335 * effect since storage for no response commands is deallocated once they
336 * are successfully sent.
337 * @param pkt The request that was sent on the bus.
339 void markInService(MSHR *mshr, PacketPtr pkt = 0);
342 * Perform the given writeback request.
343 * @param pkt The writeback request.
345 void doWriteback(PacketPtr pkt);
348 * Return whether there are any outstanding misses.
350 bool outstandingMisses() const
352 return mshrQueue.allocated != 0;
355 CacheBlk *findBlock(Addr addr) {
356 return tags->findBlock(addr);
359 bool inCache(Addr addr) {
360 return (tags->findBlock(addr) != 0);
363 bool inMissQueue(Addr addr) {
364 return (mshrQueue.findMatch(addr) != 0);
368 * Find next request ready time from among possible sources.
370 Tick nextMSHRReadyTime();
373 #endif // __CACHE_HH__