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
35 * Declares a basic cache interface BaseCache.
38 #ifndef __BASE_CACHE_HH__
39 #define __BASE_CACHE_HH__
46 #include "base/misc.hh"
47 #include "base/statistics.hh"
48 #include "base/trace.hh"
49 #include "base/types.hh"
50 #include "config/full_system.hh"
51 #include "debug/Cache.hh"
52 #include "debug/CachePort.hh"
53 #include "mem/cache/mshr_queue.hh"
54 #include "mem/mem_object.hh"
55 #include "mem/packet.hh"
56 #include "mem/request.hh"
57 #include "mem/tport.hh"
58 #include "params/BaseCache.hh"
59 #include "sim/eventq.hh"
60 #include "sim/sim_exit.hh"
64 * A basic cache interface. Implements some common functions for speed.
66 class BaseCache : public MemObject
69 * Indexes to enumerate the MSHR queues.
77 * Reasons for caches to be blocked.
80 Blocked_NoMSHRs = MSHRQueue_MSHRs,
81 Blocked_NoWBBuffers = MSHRQueue_WriteBuffer,
88 * Reasons for cache to request a bus.
91 Request_MSHR = MSHRQueue_MSHRs,
92 Request_WB = MSHRQueue_WriteBuffer,
99 class CachePort : public SimpleTimingPort
105 CachePort(const std::string &_name, BaseCache *_cache,
106 const std::string &_label);
108 virtual unsigned deviceBlockSize() const;
110 bool recvRetryCommon();
112 typedef EventWrapper<Port, &Port::sendRetry>
115 const std::string label;
122 bool checkFunctional(PacketPtr pkt);
128 void requestBus(RequestCause cause, Tick time)
130 DPRINTF(CachePort, "Asserting bus request for cause %d\n", cause);
131 if (!waitingOnRetry) {
132 schedSendEvent(time);
136 void respond(PacketPtr pkt, Tick time) {
137 schedSendTiming(pkt, time);
141 public: //Made public so coherence can get at it.
142 CachePort *cpuSidePort;
143 CachePort *memSidePort;
147 /** Miss status registers */
150 /** Write/writeback buffer */
151 MSHRQueue writeBuffer;
153 MSHR *allocateBufferInternal(MSHRQueue *mq, Addr addr, int size,
154 PacketPtr pkt, Tick time, bool requestBus)
156 MSHR *mshr = mq->allocate(addr, size, pkt, time, order++);
159 setBlocked((BlockedCause)mq->index);
163 requestMemSideBus((RequestCause)mq->index, time);
169 void markInServiceInternal(MSHR *mshr, PacketPtr pkt)
171 MSHRQueue *mq = mshr->queue;
172 bool wasFull = mq->isFull();
173 mq->markInService(mshr, pkt);
174 if (wasFull && !mq->isFull()) {
175 clearBlocked((BlockedCause)mq->index);
179 /** Block size of this cache */
180 const unsigned blkSize;
183 * The latency of a hit in this device.
187 /** The number of targets for each MSHR. */
190 /** Do we forward snoops from mem side port through to cpu side port? */
193 /** Is this cache a toplevel cache (e.g. L1, I/O cache). If so we should
194 * never try to forward ownership and similar optimizations to the cpu
199 * Bit vector of the blocking reasons for the access path.
204 /** Increasing order number assigned to each incoming request. */
207 /** Stores time the cache blocked for statistics. */
210 /** Pointer to the MSHR that has no targets. */
213 /** The number of misses to trigger an exit event. */
216 /** The drain event. */
220 * The address range to which the cache responds on the CPU side.
221 * Normally this is all possible memory addresses. */
222 Range<Addr> addrRange;
224 /** number of cpus sharing this cache - from config file */
228 int numCpus() { return _numCpus; }
231 * @addtogroup CacheStatistics
235 /** Number of hits per thread for each type of command. @sa Packet::Command */
236 Stats::Vector hits[MemCmd::NUM_MEM_CMDS];
237 /** Number of hits for demand accesses. */
238 Stats::Formula demandHits;
239 /** Number of hit for all accesses. */
240 Stats::Formula overallHits;
242 /** Number of misses per thread for each type of command. @sa Packet::Command */
243 Stats::Vector misses[MemCmd::NUM_MEM_CMDS];
244 /** Number of misses for demand accesses. */
245 Stats::Formula demandMisses;
246 /** Number of misses for all accesses. */
247 Stats::Formula overallMisses;
250 * Total number of cycles per thread/command spent waiting for a miss.
251 * Used to calculate the average miss latency.
253 Stats::Vector missLatency[MemCmd::NUM_MEM_CMDS];
254 /** Total number of cycles spent waiting for demand misses. */
255 Stats::Formula demandMissLatency;
256 /** Total number of cycles spent waiting for all misses. */
257 Stats::Formula overallMissLatency;
259 /** The number of accesses per command and thread. */
260 Stats::Formula accesses[MemCmd::NUM_MEM_CMDS];
261 /** The number of demand accesses. */
262 Stats::Formula demandAccesses;
263 /** The number of overall accesses. */
264 Stats::Formula overallAccesses;
266 /** The miss rate per command and thread. */
267 Stats::Formula missRate[MemCmd::NUM_MEM_CMDS];
268 /** The miss rate of all demand accesses. */
269 Stats::Formula demandMissRate;
270 /** The miss rate for all accesses. */
271 Stats::Formula overallMissRate;
273 /** The average miss latency per command and thread. */
274 Stats::Formula avgMissLatency[MemCmd::NUM_MEM_CMDS];
275 /** The average miss latency for demand misses. */
276 Stats::Formula demandAvgMissLatency;
277 /** The average miss latency for all misses. */
278 Stats::Formula overallAvgMissLatency;
280 /** The total number of cycles blocked for each blocked cause. */
281 Stats::Vector blocked_cycles;
282 /** The number of times this cache blocked for each blocked cause. */
283 Stats::Vector blocked_causes;
285 /** The average number of cycles blocked for each blocked cause. */
286 Stats::Formula avg_blocked;
288 /** The number of fast writes (WH64) performed. */
289 Stats::Scalar fastWrites;
291 /** The number of cache copies performed. */
292 Stats::Scalar cacheCopies;
294 /** Number of blocks written back per thread. */
295 Stats::Vector writebacks;
297 /** Number of misses that hit in the MSHRs per command and thread. */
298 Stats::Vector mshr_hits[MemCmd::NUM_MEM_CMDS];
299 /** Demand misses that hit in the MSHRs. */
300 Stats::Formula demandMshrHits;
301 /** Total number of misses that hit in the MSHRs. */
302 Stats::Formula overallMshrHits;
304 /** Number of misses that miss in the MSHRs, per command and thread. */
305 Stats::Vector mshr_misses[MemCmd::NUM_MEM_CMDS];
306 /** Demand misses that miss in the MSHRs. */
307 Stats::Formula demandMshrMisses;
308 /** Total number of misses that miss in the MSHRs. */
309 Stats::Formula overallMshrMisses;
311 /** Number of misses that miss in the MSHRs, per command and thread. */
312 Stats::Vector mshr_uncacheable[MemCmd::NUM_MEM_CMDS];
313 /** Total number of misses that miss in the MSHRs. */
314 Stats::Formula overallMshrUncacheable;
316 /** Total cycle latency of each MSHR miss, per command and thread. */
317 Stats::Vector mshr_miss_latency[MemCmd::NUM_MEM_CMDS];
318 /** Total cycle latency of demand MSHR misses. */
319 Stats::Formula demandMshrMissLatency;
320 /** Total cycle latency of overall MSHR misses. */
321 Stats::Formula overallMshrMissLatency;
323 /** Total cycle latency of each MSHR miss, per command and thread. */
324 Stats::Vector mshr_uncacheable_lat[MemCmd::NUM_MEM_CMDS];
325 /** Total cycle latency of overall MSHR misses. */
326 Stats::Formula overallMshrUncacheableLatency;
329 /** The total number of MSHR accesses per command and thread. */
330 Stats::Formula mshrAccesses[MemCmd::NUM_MEM_CMDS];
331 /** The total number of demand MSHR accesses. */
332 Stats::Formula demandMshrAccesses;
333 /** The total number of MSHR accesses. */
334 Stats::Formula overallMshrAccesses;
337 /** The miss rate in the MSHRs pre command and thread. */
338 Stats::Formula mshrMissRate[MemCmd::NUM_MEM_CMDS];
339 /** The demand miss rate in the MSHRs. */
340 Stats::Formula demandMshrMissRate;
341 /** The overall miss rate in the MSHRs. */
342 Stats::Formula overallMshrMissRate;
344 /** The average latency of an MSHR miss, per command and thread. */
345 Stats::Formula avgMshrMissLatency[MemCmd::NUM_MEM_CMDS];
346 /** The average latency of a demand MSHR miss. */
347 Stats::Formula demandAvgMshrMissLatency;
348 /** The average overall latency of an MSHR miss. */
349 Stats::Formula overallAvgMshrMissLatency;
351 /** The average latency of an MSHR miss, per command and thread. */
352 Stats::Formula avgMshrUncacheableLatency[MemCmd::NUM_MEM_CMDS];
353 /** The average overall latency of an MSHR miss. */
354 Stats::Formula overallAvgMshrUncacheableLatency;
356 /** The number of times a thread hit its MSHR cap. */
357 Stats::Vector mshr_cap_events;
358 /** The number of times software prefetches caused the MSHR to block. */
359 Stats::Vector soft_prefetch_mshr_full;
361 Stats::Scalar mshr_no_allocate_misses;
368 * Register stats for this object.
370 virtual void regStats();
373 typedef BaseCacheParams Params;
374 BaseCache(const Params *p);
380 * Query block size of a cache.
381 * @return The block size
390 Addr blockAlign(Addr addr) const { return (addr & ~(Addr(blkSize - 1))); }
393 const Range<Addr> &getAddrRange() const { return addrRange; }
395 MSHR *allocateMissBuffer(PacketPtr pkt, Tick time, bool requestBus)
397 assert(!pkt->req->isUncacheable());
398 return allocateBufferInternal(&mshrQueue,
399 blockAlign(pkt->getAddr()), blkSize,
400 pkt, time, requestBus);
403 MSHR *allocateWriteBuffer(PacketPtr pkt, Tick time, bool requestBus)
405 assert(pkt->isWrite() && !pkt->isRead());
406 return allocateBufferInternal(&writeBuffer,
407 pkt->getAddr(), pkt->getSize(),
408 pkt, time, requestBus);
411 MSHR *allocateUncachedReadBuffer(PacketPtr pkt, Tick time, bool requestBus)
413 assert(pkt->req->isUncacheable());
414 assert(pkt->isRead());
415 return allocateBufferInternal(&mshrQueue,
416 pkt->getAddr(), pkt->getSize(),
417 pkt, time, requestBus);
421 * Returns true if the cache is blocked for accesses.
429 * Marks the access path of the cache as blocked for the given cause. This
430 * also sets the blocked flag in the slave interface.
431 * @param cause The reason for the cache blocking.
433 void setBlocked(BlockedCause cause)
435 uint8_t flag = 1 << cause;
437 blocked_causes[cause]++;
438 blockedCycle = curTick();
439 cpuSidePort->setBlocked();
442 DPRINTF(Cache,"Blocking for cause %d, mask=%d\n", cause, blocked);
446 * Marks the cache as unblocked for the given cause. This also clears the
447 * blocked flags in the appropriate interfaces.
448 * @param cause The newly unblocked cause.
449 * @warning Calling this function can cause a blocked request on the bus to
450 * access the cache. The cache must be in a state to handle that request.
452 void clearBlocked(BlockedCause cause)
454 uint8_t flag = 1 << cause;
456 DPRINTF(Cache,"Unblocking for cause %d, mask=%d\n", cause, blocked);
458 blocked_cycles[cause] += curTick() - blockedCycle;
459 cpuSidePort->clearBlocked();
464 * Request the master bus for the given cause and time.
465 * @param cause The reason for the request.
466 * @param time The time to make the request.
468 void requestMemSideBus(RequestCause cause, Tick time)
470 memSidePort->requestBus(cause, time);
474 * Clear the master bus request for the given cause.
475 * @param cause The request reason to clear.
477 void deassertMemSideBusRequest(RequestCause cause)
479 // Obsolete... we no longer signal bus requests explicitly so
480 // we can't deassert them. Leaving this in as a no-op since
481 // the prefetcher calls it to indicate that it no longer wants
482 // to request a prefetch, and someday that might be
483 // interesting again.
486 virtual unsigned int drain(Event *de);
488 virtual bool inCache(Addr addr) = 0;
490 virtual bool inMissQueue(Addr addr) = 0;
492 void incMissCount(PacketPtr pkt, int id)
495 if (pkt->cmd == MemCmd::Writeback) {
497 misses[pkt->cmdToIndex()][0]++;
498 /* same thing for writeback hits as misses - no context id
499 * available, meanwhile writeback hit/miss stats are not used
500 * in any aggregate hit/miss calculations, so just lump them all
503 } else if (id == -1) {
504 // Device accesses have id -1
505 // lump device accesses into their own bucket
506 misses[pkt->cmdToIndex()][_numCpus]++;
509 misses[pkt->cmdToIndex()][id % _numCpus]++;
515 exitSimLoop("A cache reached the maximum miss count");
518 void incHitCount(PacketPtr pkt, int id)
521 /* Writeback requests don't have a context id associated with
522 * them, so attributing a hit to a -1 context id is obviously a
523 * problem. I've noticed in the stats that hits are split into
524 * demand and non-demand hits - neither of which include writeback
525 * hits, so here, I'll just put the writeback hits into bucket 0
526 * since it won't mess with any other stats -hsul */
527 if (pkt->cmd == MemCmd::Writeback) {
529 hits[pkt->cmdToIndex()][0]++;
531 } else if (id == -1) {
532 // Device accesses have id -1
533 // lump device accesses into their own bucket
534 hits[pkt->cmdToIndex()][_numCpus]++;
537 /* the % is necessary in case there are switch cpus */
538 hits[pkt->cmdToIndex()][id % _numCpus]++;
544 #endif //__BASE_CACHE_HH__