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/misc.hh"
45 #include "base/statistics.hh"
46 #include "base/trace.hh"
47 #include "mem/mem_object.hh"
48 #include "mem/packet.hh"
49 #include "mem/port.hh"
50 #include "mem/request.hh"
51 #include "sim/eventq.hh"
54 * Reasons for Caches to be Blocked.
65 * Reasons for cache to request a bus.
76 * A basic cache interface. Implements some common functions for speed.
78 class BaseCache : public MemObject
80 class CachePort : public Port
86 CachePort(const std::string &_name, BaseCache *_cache, bool _isCpuSide);
87 virtual void recvStatusChange(Status status);
89 virtual void getDeviceAddressRanges(AddrRangeList &resp,
90 AddrRangeList &snoop);
92 virtual int deviceBlockSize();
94 virtual void recvRetry();
101 bool checkFunctional(PacketPtr pkt);
103 void checkAndSendFunctional(PacketPtr pkt);
105 bool canDrain() { return drainList.empty() && transmitList.empty(); }
115 std::list<PacketPtr> drainList;
117 std::list<std::pair<Tick,PacketPtr> > transmitList;
120 struct CacheEvent : public Event
122 CachePort *cachePort;
126 CacheEvent(CachePort *_cachePort, bool response);
128 const char *description();
131 public: //Made public so coherence can get at it.
132 CachePort *cpuSidePort;
133 CachePort *memSidePort;
135 CacheEvent *sendEvent;
136 CacheEvent *memSendEvent;
139 void recvStatusChange(Port::Status status, bool isCpuSide)
141 if (status == Port::RangeChange){
143 cpuSidePort->sendStatusChange(Port::RangeChange);
146 memSidePort->sendStatusChange(Port::RangeChange);
151 virtual PacketPtr getPacket() = 0;
153 virtual PacketPtr getCoherencePacket() = 0;
155 virtual void sendResult(PacketPtr &pkt, MSHR* mshr, bool success) = 0;
157 virtual void sendCoherenceResult(PacketPtr &pkt, MSHR* mshr, bool success) = 0;
160 * Bit vector of the blocking reasons for the access path.
166 * Bit vector for the blocking reasons for the snoop path.
169 uint8_t blockedSnoop;
172 * Bit vector for the outstanding requests for the master interface.
174 uint8_t masterRequests;
177 * Bit vector for the outstanding requests for the slave interface.
179 uint8_t slaveRequests;
183 /** Stores time the cache blocked for statistics. */
186 /** Block size of this cache */
189 /** The number of misses to trigger an exit event. */
192 /** The drain event. */
198 * @addtogroup CacheStatistics
202 /** Number of hits per thread for each type of command. @sa Packet::Command */
203 Stats::Vector<> hits[MemCmd::NUM_MEM_CMDS];
204 /** Number of hits for demand accesses. */
205 Stats::Formula demandHits;
206 /** Number of hit for all accesses. */
207 Stats::Formula overallHits;
209 /** Number of misses per thread for each type of command. @sa Packet::Command */
210 Stats::Vector<> misses[MemCmd::NUM_MEM_CMDS];
211 /** Number of misses for demand accesses. */
212 Stats::Formula demandMisses;
213 /** Number of misses for all accesses. */
214 Stats::Formula overallMisses;
217 * Total number of cycles per thread/command spent waiting for a miss.
218 * Used to calculate the average miss latency.
220 Stats::Vector<> missLatency[MemCmd::NUM_MEM_CMDS];
221 /** Total number of cycles spent waiting for demand misses. */
222 Stats::Formula demandMissLatency;
223 /** Total number of cycles spent waiting for all misses. */
224 Stats::Formula overallMissLatency;
226 /** The number of accesses per command and thread. */
227 Stats::Formula accesses[MemCmd::NUM_MEM_CMDS];
228 /** The number of demand accesses. */
229 Stats::Formula demandAccesses;
230 /** The number of overall accesses. */
231 Stats::Formula overallAccesses;
233 /** The miss rate per command and thread. */
234 Stats::Formula missRate[MemCmd::NUM_MEM_CMDS];
235 /** The miss rate of all demand accesses. */
236 Stats::Formula demandMissRate;
237 /** The miss rate for all accesses. */
238 Stats::Formula overallMissRate;
240 /** The average miss latency per command and thread. */
241 Stats::Formula avgMissLatency[MemCmd::NUM_MEM_CMDS];
242 /** The average miss latency for demand misses. */
243 Stats::Formula demandAvgMissLatency;
244 /** The average miss latency for all misses. */
245 Stats::Formula overallAvgMissLatency;
247 /** The total number of cycles blocked for each blocked cause. */
248 Stats::Vector<> blocked_cycles;
249 /** The number of times this cache blocked for each blocked cause. */
250 Stats::Vector<> blocked_causes;
252 /** The average number of cycles blocked for each blocked cause. */
253 Stats::Formula avg_blocked;
255 /** The number of fast writes (WH64) performed. */
256 Stats::Scalar<> fastWrites;
258 /** The number of cache copies performed. */
259 Stats::Scalar<> cacheCopies;
266 * Register stats for this object.
268 virtual void regStats();
275 /** List of address ranges of this cache. */
276 std::vector<Range<Addr> > addrRange;
277 /** The hit latency for this cache. */
279 /** The block size of this cache. */
282 * The maximum number of misses this cache should handle before
283 * ending the simulation.
288 * Construct an instance of this parameter class.
290 Params(std::vector<Range<Addr> > addr_range,
291 int hit_latency, int _blkSize, Counter max_misses)
292 : addrRange(addr_range), hitLatency(hit_latency), blkSize(_blkSize),
293 maxMisses(max_misses)
299 * Create and initialize a basic cache object.
300 * @param name The name of this cache.
301 * @param hier_params Pointer to the HierParams object for this hierarchy
303 * @param params The parameter object for this BaseCache.
305 BaseCache(const std::string &name, Params ¶ms)
306 : MemObject(name), blocked(0), blockedSnoop(0), masterRequests(0),
307 slaveRequests(0), blkSize(params.blkSize),
308 missCount(params.maxMisses), drainEvent(NULL)
310 //Start ports at null if more than one is created we should panic
324 * Query block size of a cache.
325 * @return The block size
327 int getBlockSize() const
333 * Returns true if the cache is blocked for accesses.
341 * Returns true if the cache is blocked for snoops.
343 bool isBlockedForSnoop()
345 return blockedSnoop != 0;
349 * Marks the access path of the cache as blocked for the given cause. This
350 * also sets the blocked flag in the slave interface.
351 * @param cause The reason for the cache blocking.
353 void setBlocked(BlockedCause cause)
355 uint8_t flag = 1 << cause;
357 blocked_causes[cause]++;
358 blockedCycle = curTick;
360 int old_state = blocked;
361 if (!(blocked & flag)) {
362 //Wasn't already blocked for this cause
364 DPRINTF(Cache,"Blocking for cause %s\n", cause);
366 cpuSidePort->setBlocked();
371 * Marks the snoop path of the cache as blocked for the given cause. This
372 * also sets the blocked flag in the master interface.
373 * @param cause The reason to block the snoop path.
375 void setBlockedForSnoop(BlockedCause cause)
377 uint8_t flag = 1 << cause;
378 uint8_t old_state = blockedSnoop;
379 if (!(blockedSnoop & flag)) {
380 //Wasn't already blocked for this cause
381 blockedSnoop |= flag;
383 memSidePort->setBlocked();
388 * Marks the cache as unblocked for the given cause. This also clears the
389 * blocked flags in the appropriate interfaces.
390 * @param cause The newly unblocked cause.
391 * @warning Calling this function can cause a blocked request on the bus to
392 * access the cache. The cache must be in a state to handle that request.
394 void clearBlocked(BlockedCause cause)
396 uint8_t flag = 1 << cause;
397 DPRINTF(Cache,"Unblocking for cause %s, causes left=%i\n",
403 blocked_cycles[cause] += curTick - blockedCycle;
404 DPRINTF(Cache,"Unblocking from all causes\n");
405 cpuSidePort->clearBlocked();
408 if (blockedSnoop & flag)
410 blockedSnoop &= ~flag;
411 if (!isBlockedForSnoop()) {
412 memSidePort->clearBlocked();
418 * True if the master bus should be requested.
419 * @return True if there are outstanding requests for the master bus.
421 bool doMasterRequest()
423 return masterRequests != 0;
427 * Request the master bus for the given cause and time.
428 * @param cause The reason for the request.
429 * @param time The time to make the request.
431 void setMasterRequest(RequestCause cause, Tick time)
433 if (!doMasterRequest() && !memSidePort->waitingOnRetry)
435 BaseCache::CacheEvent * reqCpu =
436 new BaseCache::CacheEvent(memSidePort, false);
437 reqCpu->schedule(time);
439 uint8_t flag = 1<<cause;
440 masterRequests |= flag;
444 * Clear the master bus request for the given cause.
445 * @param cause The request reason to clear.
447 void clearMasterRequest(RequestCause cause)
449 uint8_t flag = 1<<cause;
450 masterRequests &= ~flag;
455 * Return true if the slave bus should be requested.
456 * @return True if there are outstanding requests for the slave bus.
458 bool doSlaveRequest()
460 return slaveRequests != 0;
464 * Request the slave bus for the given reason and time.
465 * @param cause The reason for the request.
466 * @param time The time to make the request.
468 void setSlaveRequest(RequestCause cause, Tick time)
470 if (!doSlaveRequest() && !cpuSidePort->waitingOnRetry)
472 BaseCache::CacheEvent * reqCpu =
473 new BaseCache::CacheEvent(cpuSidePort, false);
474 reqCpu->schedule(time);
476 uint8_t flag = 1<<cause;
477 slaveRequests |= flag;
481 * Clear the slave bus request for the given reason.
482 * @param cause The request reason to clear.
484 void clearSlaveRequest(RequestCause cause)
486 uint8_t flag = 1<<cause;
487 slaveRequests &= ~flag;
492 * Send a response to the slave interface.
493 * @param pkt The request being responded to.
494 * @param time The time the response is ready.
496 void respond(PacketPtr pkt, Tick time)
498 assert(time >= curTick);
499 if (pkt->needsResponse()) {
500 /* CacheEvent *reqCpu = new CacheEvent(cpuSidePort, pkt);
501 reqCpu->schedule(time);
503 if (cpuSidePort->transmitList.empty()) {
504 assert(!sendEvent->scheduled());
505 sendEvent->schedule(time);
506 cpuSidePort->transmitList.push_back(std::pair<Tick,PacketPtr>
511 // something is on the list and this belongs at the end
512 if (time >= cpuSidePort->transmitList.back().first) {
513 cpuSidePort->transmitList.push_back(std::pair<Tick,PacketPtr>
517 // Something is on the list and this belongs somewhere else
518 std::list<std::pair<Tick,PacketPtr> >::iterator i =
519 cpuSidePort->transmitList.begin();
520 std::list<std::pair<Tick,PacketPtr> >::iterator end =
521 cpuSidePort->transmitList.end();
524 while (i != end && !done) {
525 if (time < i->first) {
526 if (i == cpuSidePort->transmitList.begin()) {
527 //Inserting at begining, reschedule
528 sendEvent->reschedule(time);
530 cpuSidePort->transmitList.insert(i,std::pair<Tick,PacketPtr>
538 if (pkt->cmd != MemCmd::UpgradeReq)
547 * Send a reponse to the slave interface and calculate miss latency.
548 * @param pkt The request to respond to.
549 * @param time The time the response is ready.
551 void respondToMiss(PacketPtr pkt, Tick time)
553 assert(time >= curTick);
554 if (!pkt->req->isUncacheable()) {
555 missLatency[pkt->cmdToIndex()][0/*pkt->req->getThreadNum()*/] +=
558 if (pkt->needsResponse()) {
559 /* CacheEvent *reqCpu = new CacheEvent(cpuSidePort, pkt);
560 reqCpu->schedule(time);
562 if (cpuSidePort->transmitList.empty()) {
563 assert(!sendEvent->scheduled());
564 sendEvent->schedule(time);
565 cpuSidePort->transmitList.push_back(std::pair<Tick,PacketPtr>
570 // something is on the list and this belongs at the end
571 if (time >= cpuSidePort->transmitList.back().first) {
572 cpuSidePort->transmitList.push_back(std::pair<Tick,PacketPtr>
576 // Something is on the list and this belongs somewhere else
577 std::list<std::pair<Tick,PacketPtr> >::iterator i =
578 cpuSidePort->transmitList.begin();
579 std::list<std::pair<Tick,PacketPtr> >::iterator end =
580 cpuSidePort->transmitList.end();
583 while (i != end && !done) {
584 if (time < i->first) {
585 if (i == cpuSidePort->transmitList.begin()) {
586 //Inserting at begining, reschedule
587 sendEvent->reschedule(time);
589 cpuSidePort->transmitList.insert(i,std::pair<Tick,PacketPtr>
597 if (pkt->cmd != MemCmd::UpgradeReq)
606 * Suppliess the data if cache to cache transfers are enabled.
607 * @param pkt The bus transaction to fulfill.
609 void respondToSnoop(PacketPtr pkt, Tick time)
611 assert(time >= curTick);
612 assert (pkt->needsResponse());
613 /* CacheEvent *reqMem = new CacheEvent(memSidePort, pkt);
614 reqMem->schedule(time);
616 if (memSidePort->transmitList.empty()) {
617 assert(!memSendEvent->scheduled());
618 memSendEvent->schedule(time);
619 memSidePort->transmitList.push_back(std::pair<Tick,PacketPtr>
624 // something is on the list and this belongs at the end
625 if (time >= memSidePort->transmitList.back().first) {
626 memSidePort->transmitList.push_back(std::pair<Tick,PacketPtr>
630 // Something is on the list and this belongs somewhere else
631 std::list<std::pair<Tick,PacketPtr> >::iterator i =
632 memSidePort->transmitList.begin();
633 std::list<std::pair<Tick,PacketPtr> >::iterator end =
634 memSidePort->transmitList.end();
637 while (i != end && !done) {
638 if (time < i->first) {
639 if (i == memSidePort->transmitList.begin()) {
640 //Inserting at begining, reschedule
641 memSendEvent->reschedule(time);
643 memSidePort->transmitList.insert(i,std::pair<Tick,PacketPtr>(time,pkt));
651 * Notification from master interface that a address range changed. Nothing
654 void rangeChange() {}
656 void getAddressRanges(AddrRangeList &resp, AddrRangeList &snoop, bool isCpuSide)
661 memSidePort->getPeerAddressRanges(resp, dummy);
665 //This is where snoops get updated
667 cpuSidePort->getPeerAddressRanges(dummy, snoop);
672 virtual unsigned int drain(Event *de);
676 if (drainEvent && canDrain()) {
677 drainEvent->process();
678 changeState(SimObject::Drained);
679 // Clear the drain event
686 if (doMasterRequest() || doSlaveRequest()) {
688 } else if (memSidePort && !memSidePort->canDrain()) {
690 } else if (cpuSidePort && !cpuSidePort->canDrain()) {
696 virtual bool inCache(Addr addr) = 0;
698 virtual bool inMissQueue(Addr addr) = 0;
701 #endif //__BASE_CACHE_HH__