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
33 * Declares a basic cache interface BaseCache.
36 #ifndef __BASE_CACHE_HH__
37 #define __BASE_CACHE_HH__
44 #include "base/statistics.hh"
45 #include "base/trace.hh"
46 #include "mem/mem_object.hh"
47 #include "mem/packet.hh"
48 #include "mem/port.hh"
49 #include "mem/request.hh"
50 #include "sim/eventq.hh"
53 * Reasons for Caches to be Blocked.
65 * Reasons for cache to request a bus.
75 * A basic cache interface. Implements some common functions for speed.
77 class BaseCache : public MemObject
79 class CachePort : public Port
84 CachePort(const std::string &_name, BaseCache *_cache, bool _isCpuSide);
87 virtual bool recvTiming(Packet *pkt);
89 virtual Tick recvAtomic(Packet *pkt);
91 virtual void recvFunctional(Packet *pkt);
93 virtual void recvStatusChange(Status status);
95 virtual void getDeviceAddressRanges(AddrRangeList &resp,
96 AddrRangeList &snoop);
98 virtual int deviceBlockSize();
110 struct CacheEvent : public Event
113 CachePort *cachePort;
115 CacheEvent(Packet *pkt, CachePort *cachePort);
117 const char *description();
121 CachePort *cpuSidePort;
122 CachePort *memSidePort;
125 virtual Port *getPort(const std::string &if_name);
128 //To be defined in cache_impl.hh not in base class
129 virtual bool doTimingAccess(Packet *pkt, CachePort *cachePort, bool isCpuSide);
130 virtual Tick doAtomicAccess(Packet *pkt, bool isCpuSide);
131 virtual void doFunctionalAccess(Packet *pkt, bool isCpuSide);
132 virtual void recvStatusChange(Port::Status status, bool isCpuSide);
135 * Bit vector of the blocking reasons for the access path.
141 * Bit vector for the blocking reasons for the snoop path.
144 uint8_t blockedSnoop;
147 * Bit vector for the outstanding requests for the master interface.
149 uint8_t masterRequests;
152 * Bit vector for the outstanding requests for the slave interface.
154 uint8_t slaveRequests;
158 /** True if this cache is connected to the CPU. */
161 /** Stores time the cache blocked for statistics. */
164 /** Block size of this cache */
167 /** The number of misses to trigger an exit event. */
173 * @addtogroup CacheStatistics
177 /** Number of hits per thread for each type of command. @sa Packet::Command */
178 Stats::Vector<> hits[NUM_MEM_CMDS];
179 /** Number of hits for demand accesses. */
180 Stats::Formula demandHits;
181 /** Number of hit for all accesses. */
182 Stats::Formula overallHits;
184 /** Number of misses per thread for each type of command. @sa Packet::Command */
185 Stats::Vector<> misses[NUM_MEM_CMDS];
186 /** Number of misses for demand accesses. */
187 Stats::Formula demandMisses;
188 /** Number of misses for all accesses. */
189 Stats::Formula overallMisses;
192 * Total number of cycles per thread/command spent waiting for a miss.
193 * Used to calculate the average miss latency.
195 Stats::Vector<> missLatency[NUM_MEM_CMDS];
196 /** Total number of cycles spent waiting for demand misses. */
197 Stats::Formula demandMissLatency;
198 /** Total number of cycles spent waiting for all misses. */
199 Stats::Formula overallMissLatency;
201 /** The number of accesses per command and thread. */
202 Stats::Formula accesses[NUM_MEM_CMDS];
203 /** The number of demand accesses. */
204 Stats::Formula demandAccesses;
205 /** The number of overall accesses. */
206 Stats::Formula overallAccesses;
208 /** The miss rate per command and thread. */
209 Stats::Formula missRate[NUM_MEM_CMDS];
210 /** The miss rate of all demand accesses. */
211 Stats::Formula demandMissRate;
212 /** The miss rate for all accesses. */
213 Stats::Formula overallMissRate;
215 /** The average miss latency per command and thread. */
216 Stats::Formula avgMissLatency[NUM_MEM_CMDS];
217 /** The average miss latency for demand misses. */
218 Stats::Formula demandAvgMissLatency;
219 /** The average miss latency for all misses. */
220 Stats::Formula overallAvgMissLatency;
222 /** The total number of cycles blocked for each blocked cause. */
223 Stats::Vector<> blocked_cycles;
224 /** The number of times this cache blocked for each blocked cause. */
225 Stats::Vector<> blocked_causes;
227 /** The average number of cycles blocked for each blocked cause. */
228 Stats::Formula avg_blocked;
230 /** The number of fast writes (WH64) performed. */
231 Stats::Scalar<> fastWrites;
233 /** The number of cache copies performed. */
234 Stats::Scalar<> cacheCopies;
241 * Register stats for this object.
243 virtual void regStats();
250 /** List of address ranges of this cache. */
251 std::vector<Range<Addr> > addrRange;
252 /** The hit latency for this cache. */
254 /** The block size of this cache. */
257 * The maximum number of misses this cache should handle before
258 * ending the simulation.
263 * Construct an instance of this parameter class.
265 Params(std::vector<Range<Addr> > addr_range,
266 int hit_latency, int _blkSize, Counter max_misses)
267 : addrRange(addr_range), hitLatency(hit_latency), blkSize(_blkSize),
268 maxMisses(max_misses)
274 * Create and initialize a basic cache object.
275 * @param name The name of this cache.
276 * @param hier_params Pointer to the HierParams object for this hierarchy
278 * @param params The parameter object for this BaseCache.
280 BaseCache(const std::string &name, Params ¶ms)
281 : MemObject(name), blocked(0), blockedSnoop(0), masterRequests(0),
282 slaveRequests(0), topLevelCache(false), blkSize(params.blkSize),
283 missCount(params.maxMisses)
285 //Start ports at null if more than one is created we should panic
291 * Query block size of a cache.
292 * @return The block size
294 int getBlockSize() const
300 * Returns true if this cache is connect to the CPU.
301 * @return True if this is a L1 cache.
305 return topLevelCache;
309 * Returns true if the cache is blocked for accesses.
317 * Returns true if the cache is blocked for snoops.
319 bool isBlockedForSnoop()
321 return blockedSnoop != 0;
325 * Marks the access path of the cache as blocked for the given cause. This
326 * also sets the blocked flag in the slave interface.
327 * @param cause The reason for the cache blocking.
329 void setBlocked(BlockedCause cause)
331 uint8_t flag = 1 << cause;
333 blocked_causes[cause]++;
334 blockedCycle = curTick;
337 DPRINTF(Cache,"Blocking for cause %s\n", cause);
338 cpuSidePort->setBlocked();
342 * Marks the snoop path of the cache as blocked for the given cause. This
343 * also sets the blocked flag in the master interface.
344 * @param cause The reason to block the snoop path.
346 void setBlockedForSnoop(BlockedCause cause)
348 uint8_t flag = 1 << cause;
349 blockedSnoop |= flag;
350 memSidePort->setBlocked();
354 * Marks the cache as unblocked for the given cause. This also clears the
355 * blocked flags in the appropriate interfaces.
356 * @param cause The newly unblocked cause.
357 * @warning Calling this function can cause a blocked request on the bus to
358 * access the cache. The cache must be in a state to handle that request.
360 void clearBlocked(BlockedCause cause)
362 uint8_t flag = 1 << cause;
364 blockedSnoop &= ~flag;
365 DPRINTF(Cache,"Unblocking for cause %s, causes left=%i\n",
368 blocked_cycles[cause] += curTick - blockedCycle;
369 DPRINTF(Cache,"Unblocking from all causes\n");
370 cpuSidePort->clearBlocked();
372 if (!isBlockedForSnoop()) {
373 memSidePort->clearBlocked();
379 * True if the master bus should be requested.
380 * @return True if there are outstanding requests for the master bus.
382 bool doMasterRequest()
384 return masterRequests != 0;
388 * Request the master bus for the given cause and time.
389 * @param cause The reason for the request.
390 * @param time The time to make the request.
392 void setMasterRequest(RequestCause cause, Tick time)
394 uint8_t flag = 1<<cause;
395 masterRequests |= flag;
396 assert("Implement\n" && 0);
397 // mi->pktuest(time);
401 * Clear the master bus request for the given cause.
402 * @param cause The request reason to clear.
404 void clearMasterRequest(RequestCause cause)
406 uint8_t flag = 1<<cause;
407 masterRequests &= ~flag;
411 * Return true if the slave bus should be requested.
412 * @return True if there are outstanding requests for the slave bus.
414 bool doSlaveRequest()
416 return slaveRequests != 0;
420 * Request the slave bus for the given reason and time.
421 * @param cause The reason for the request.
422 * @param time The time to make the request.
424 void setSlaveRequest(RequestCause cause, Tick time)
426 uint8_t flag = 1<<cause;
427 slaveRequests |= flag;
428 assert("Implement\n" && 0);
429 // si->pktuest(time);
433 * Clear the slave bus request for the given reason.
434 * @param cause The request reason to clear.
436 void clearSlaveRequest(RequestCause cause)
438 uint8_t flag = 1<<cause;
439 slaveRequests &= ~flag;
443 * Send a response to the slave interface.
444 * @param req The request being responded to.
445 * @param time The time the response is ready.
447 void respond(Packet *pkt, Tick time)
449 assert("Implement\n" && 0);
450 // si->respond(pkt,time);
454 * Send a reponse to the slave interface and calculate miss latency.
455 * @param req The request to respond to.
456 * @param time The time the response is ready.
458 void respondToMiss(Packet *pkt, Tick time)
460 if (!pkt->req->isUncacheable()) {
461 missLatency[pkt->cmdToIndex()][pkt->req->getThreadNum()] += time - pkt->time;
463 assert("Implement\n" && 0);
464 // si->respond(pkt,time);
468 * Suppliess the data if cache to cache transfers are enabled.
469 * @param req The bus transaction to fulfill.
471 void respondToSnoop(Packet *pkt)
473 assert("Implement\n" && 0);
474 // mi->respond(pkt,curTick + hitLatency);
478 * Notification from master interface that a address range changed. Nothing
481 void rangeChange() {}
483 void getAddressRanges(AddrRangeList &resp, AddrRangeList &snoop)
485 panic("Unimplimented\n");
489 #endif //__BASE_CACHE_HH__