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 * Definition of BaseCache functions.
36 #include "mem/cache/base_cache.hh"
38 #include "cpu/base.hh"
39 #include "mem/cache/miss/mshr.hh"
43 BaseCache::CachePort::CachePort(const std::string
&_name
, BaseCache
*_cache
,
45 : Port(_name
), cache(_cache
), isCpuSide(_isCpuSide
)
48 waitingOnRetry
= false;
49 //Start ports at null if more than one is created we should panic
55 BaseCache::CachePort::recvStatusChange(Port::Status status
)
57 cache
->recvStatusChange(status
, isCpuSide
);
61 BaseCache::CachePort::getDeviceAddressRanges(AddrRangeList
&resp
,
64 cache
->getAddressRanges(resp
, snoop
, isCpuSide
);
68 BaseCache::CachePort::deviceBlockSize()
70 return cache
->getBlockSize();
74 BaseCache::CachePort::recvTiming(Packet
*pkt
)
77 && !pkt
->req
->isUncacheable()
78 && pkt
->isInvalidate()
79 && !pkt
->isRead() && !pkt
->isWrite()) {
80 //Upgrade or Invalidate
81 //Look into what happens if two slave caches on bus
82 DPRINTF(Cache
, "%s %x ? blk_addr: %x\n", pkt
->cmdString(),
83 pkt
->getAddr() & (((ULL(1))<<48)-1),
84 pkt
->getAddr() & ~((Addr
)cache
->blkSize
- 1));
86 assert(!(pkt
->flags
& SATISFIED
));
87 pkt
->flags
|= SATISFIED
;
88 //Invalidates/Upgrades need no response if they get the bus
92 if (pkt
->isRequest() && blocked
)
94 DPRINTF(Cache
,"Scheduling a retry while blocked\n");
98 return cache
->doTimingAccess(pkt
, this, isCpuSide
);
102 BaseCache::CachePort::recvAtomic(Packet
*pkt
)
104 return cache
->doAtomicAccess(pkt
, isCpuSide
);
108 BaseCache::CachePort::recvFunctional(Packet
*pkt
)
110 //Check storage here first
111 list
<Packet
*>::iterator i
= drainList
.begin();
112 list
<Packet
*>::iterator end
= drainList
.end();
113 for (; i
!= end
; ++i
) {
114 Packet
* target
= *i
;
115 // If the target contains data, and it overlaps the
116 // probed request, need to update data
117 if (target
->intersect(pkt
)) {
121 if (target
->getAddr() < pkt
->getAddr()) {
122 int offset
= pkt
->getAddr() - target
->getAddr();
123 pkt_data
= pkt
->getPtr
<uint8_t>();
124 write_data
= target
->getPtr
<uint8_t>() + offset
;
125 data_size
= target
->getSize() - offset
;
126 assert(data_size
> 0);
127 if (data_size
> pkt
->getSize())
128 data_size
= pkt
->getSize();
130 int offset
= target
->getAddr() - pkt
->getAddr();
131 pkt_data
= pkt
->getPtr
<uint8_t>() + offset
;
132 write_data
= target
->getPtr
<uint8_t>();
133 data_size
= pkt
->getSize() - offset
;
134 assert(data_size
> pkt
->getSize());
135 if (data_size
> target
->getSize())
136 data_size
= target
->getSize();
139 if (pkt
->isWrite()) {
140 memcpy(pkt_data
, write_data
, data_size
);
142 memcpy(write_data
, pkt_data
, data_size
);
146 cache
->doFunctionalAccess(pkt
, isCpuSide
);
150 BaseCache::CachePort::recvRetry()
153 assert(waitingOnRetry
);
154 if (!drainList
.empty()) {
155 DPRINTF(CachePort
, "%s attempting to send a retry for response\n", name());
156 //We have some responses to drain first
157 if (sendTiming(drainList
.front())) {
158 DPRINTF(CachePort
, "%s sucessful in sending a retry for response\n", name());
159 drainList
.pop_front();
160 if (!drainList
.empty() ||
161 !isCpuSide
&& cache
->doMasterRequest() ||
162 isCpuSide
&& cache
->doSlaveRequest()) {
164 DPRINTF(CachePort
, "%s has more responses/requests\n", name());
165 BaseCache::CacheEvent
* reqCpu
= new BaseCache::CacheEvent(this);
166 reqCpu
->schedule(curTick
+ 1);
168 waitingOnRetry
= false;
173 DPRINTF(CachePort
, "%s attempting to send a retry for MSHR\n", name());
174 if (!cache
->doMasterRequest()) {
175 //This can happen if I am the owner of a block and see an upgrade
176 //while the block was in my WB Buffers. I just remove the
177 //wb and de-assert the masterRequest
178 waitingOnRetry
= false;
181 pkt
= cache
->getPacket();
182 MSHR
* mshr
= (MSHR
*)pkt
->senderState
;
183 //Copy the packet, it may be modified/destroyed elsewhere
184 Packet
* copyPkt
= new Packet(*pkt
);
185 copyPkt
->dataStatic
<uint8_t>(pkt
->getPtr
<uint8_t>());
187 bool success
= sendTiming(pkt
);
188 DPRINTF(Cache
, "Address %x was %s in sending the timing request\n",
189 pkt
->getAddr(), success
? "succesful" : "unsuccesful");
190 cache
->sendResult(pkt
, mshr
, success
);
191 waitingOnRetry
= !success
;
192 if (success
&& cache
->doMasterRequest())
194 DPRINTF(CachePort
, "%s has more requests\n", name());
195 //Still more to issue, rerequest in 1 cycle
196 BaseCache::CacheEvent
* reqCpu
= new BaseCache::CacheEvent(this);
197 reqCpu
->schedule(curTick
+ 1);
202 assert(cache
->doSlaveRequest());
203 //pkt = cache->getCoherencePacket();
204 //We save the packet, no reordering on CSHRS
205 pkt
= cache
->getCoherencePacket();
206 MSHR
* cshr
= (MSHR
*)pkt
->senderState
;
207 bool success
= sendTiming(pkt
);
208 cache
->sendCoherenceResult(pkt
, cshr
, success
);
209 waitingOnRetry
= !success
;
210 if (success
&& cache
->doSlaveRequest())
212 DPRINTF(CachePort
, "%s has more requests\n", name());
213 //Still more to issue, rerequest in 1 cycle
214 BaseCache::CacheEvent
* reqCpu
= new BaseCache::CacheEvent(this);
215 reqCpu
->schedule(curTick
+ 1);
218 if (waitingOnRetry
) DPRINTF(CachePort
, "%s STILL Waiting on retry\n", name());
219 else DPRINTF(CachePort
, "%s no longer waiting on retry\n", name());
223 BaseCache::CachePort::setBlocked()
226 DPRINTF(Cache
, "Cache Blocking\n");
228 //Clear the retry flag
229 mustSendRetry
= false;
233 BaseCache::CachePort::clearBlocked()
236 DPRINTF(Cache
, "Cache Unblocking\n");
240 DPRINTF(Cache
, "Cache Sending Retry\n");
241 mustSendRetry
= false;
246 BaseCache::CacheEvent::CacheEvent(CachePort
*_cachePort
)
247 : Event(&mainEventQueue
, CPU_Tick_Pri
), cachePort(_cachePort
)
249 this->setFlags(AutoDelete
);
253 BaseCache::CacheEvent::CacheEvent(CachePort
*_cachePort
, Packet
*_pkt
)
254 : Event(&mainEventQueue
, CPU_Tick_Pri
), cachePort(_cachePort
), pkt(_pkt
)
256 this->setFlags(AutoDelete
);
260 BaseCache::CacheEvent::process()
264 if (cachePort
->waitingOnRetry
) return;
265 //We have some responses to drain first
266 if (!cachePort
->drainList
.empty()) {
267 DPRINTF(CachePort
, "%s trying to drain a response\n", cachePort
->name());
268 if (cachePort
->sendTiming(cachePort
->drainList
.front())) {
269 DPRINTF(CachePort
, "%s drains a response succesfully\n", cachePort
->name());
270 cachePort
->drainList
.pop_front();
271 if (!cachePort
->drainList
.empty() ||
272 !cachePort
->isCpuSide
&& cachePort
->cache
->doMasterRequest() ||
273 cachePort
->isCpuSide
&& cachePort
->cache
->doSlaveRequest()) {
275 DPRINTF(CachePort
, "%s still has outstanding bus reqs\n", cachePort
->name());
276 this->schedule(curTick
+ 1);
280 cachePort
->waitingOnRetry
= true;
281 DPRINTF(CachePort
, "%s now waiting on a retry\n", cachePort
->name());
284 else if (!cachePort
->isCpuSide
)
286 DPRINTF(CachePort
, "%s trying to send a MSHR request\n", cachePort
->name());
287 if (!cachePort
->cache
->doMasterRequest()) {
288 //This can happen if I am the owner of a block and see an upgrade
289 //while the block was in my WB Buffers. I just remove the
290 //wb and de-assert the masterRequest
294 pkt
= cachePort
->cache
->getPacket();
295 MSHR
* mshr
= (MSHR
*) pkt
->senderState
;
296 //Copy the packet, it may be modified/destroyed elsewhere
297 Packet
* copyPkt
= new Packet(*pkt
);
298 copyPkt
->dataStatic
<uint8_t>(pkt
->getPtr
<uint8_t>());
301 bool success
= cachePort
->sendTiming(pkt
);
302 DPRINTF(Cache
, "Address %x was %s in sending the timing request\n",
303 pkt
->getAddr(), success
? "succesful" : "unsuccesful");
304 cachePort
->cache
->sendResult(pkt
, mshr
, success
);
305 cachePort
->waitingOnRetry
= !success
;
306 if (cachePort
->waitingOnRetry
)
307 DPRINTF(CachePort
, "%s now waiting on a retry\n", cachePort
->name());
308 if (success
&& cachePort
->cache
->doMasterRequest())
310 DPRINTF(CachePort
, "%s still more MSHR requests to send\n",
312 //Still more to issue, rerequest in 1 cycle
314 this->schedule(curTick
+1);
320 assert(cachePort
->cache
->doSlaveRequest());
321 pkt
= cachePort
->cache
->getCoherencePacket();
322 MSHR
* cshr
= (MSHR
*) pkt
->senderState
;
323 bool success
= cachePort
->sendTiming(pkt
);
324 cachePort
->cache
->sendResult(pkt
, cshr
, success
);
325 cachePort
->waitingOnRetry
= !success
;
326 if (cachePort
->waitingOnRetry
)
327 DPRINTF(CachePort
, "%s now waiting on a retry\n", cachePort
->name());
328 if (success
&& cachePort
->cache
->doSlaveRequest())
330 DPRINTF(CachePort
, "%s still more CSHR requests to send\n",
332 //Still more to issue, rerequest in 1 cycle
334 this->schedule(curTick
+1);
340 //Know the packet to send
341 if (pkt
->flags
& NACKED_LINE
)
342 pkt
->result
= Packet::Nacked
;
344 pkt
->result
= Packet::Success
;
345 pkt
->makeTimingResponse();
346 DPRINTF(CachePort
, "%s attempting to send a response\n", cachePort
->name());
347 if (!cachePort
->drainList
.empty() || cachePort
->waitingOnRetry
) {
348 //Already have a list, just append
349 cachePort
->drainList
.push_back(pkt
);
350 DPRINTF(CachePort
, "%s appending response onto drain list\n", cachePort
->name());
352 else if (!cachePort
->sendTiming(pkt
)) {
353 //It failed, save it to list of drain events
354 DPRINTF(CachePort
, "%s now waiting for a retry\n", cachePort
->name());
355 cachePort
->drainList
.push_back(pkt
);
356 cachePort
->waitingOnRetry
= true;
361 BaseCache::CacheEvent::description()
363 return "timing event\n";
367 BaseCache::getPort(const std::string
&if_name
, int idx
)
371 if(cpuSidePort
== NULL
)
372 cpuSidePort
= new CachePort(name() + "-cpu_side_port", this, true);
375 else if (if_name
== "functional")
377 if(cpuSidePort
== NULL
)
378 cpuSidePort
= new CachePort(name() + "-cpu_side_port", this, true);
381 else if (if_name
== "cpu_side")
383 if(cpuSidePort
== NULL
)
384 cpuSidePort
= new CachePort(name() + "-cpu_side_port", this, true);
387 else if (if_name
== "mem_side")
389 if (memSidePort
!= NULL
)
390 panic("Already have a mem side for this cache\n");
391 memSidePort
= new CachePort(name() + "-mem_side_port", this, false);
394 else panic("Port name %s unrecognized\n", if_name
);
400 if (!cpuSidePort
|| !memSidePort
)
401 panic("Cache not hooked up on both sides\n");
402 cpuSidePort
->sendStatusChange(Port::RangeChange
);
406 BaseCache::regStats()
408 Request
temp_req((Addr
) NULL
, 4, 0);
409 Packet::Command temp_cmd
= Packet::ReadReq
;
410 Packet
temp_pkt(&temp_req
, temp_cmd
, 0); //@todo FIx command strings so this isn't neccessary
411 temp_pkt
.allocate(); //Temp allocate, all need data
413 using namespace Stats
;
416 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
417 Packet::Command cmd
= (Packet::Command
)access_idx
;
418 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
421 .init(maxThreadsPerCPU
)
422 .name(name() + "." + cstr
+ "_hits")
423 .desc("number of " + cstr
+ " hits")
424 .flags(total
| nozero
| nonan
)
429 .name(name() + ".demand_hits")
430 .desc("number of demand (read+write) hits")
433 demandHits
= hits
[Packet::ReadReq
] + hits
[Packet::WriteReq
];
436 .name(name() + ".overall_hits")
437 .desc("number of overall hits")
440 overallHits
= demandHits
+ hits
[Packet::SoftPFReq
] + hits
[Packet::HardPFReq
]
441 + hits
[Packet::Writeback
];
444 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
445 Packet::Command cmd
= (Packet::Command
)access_idx
;
446 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
449 .init(maxThreadsPerCPU
)
450 .name(name() + "." + cstr
+ "_misses")
451 .desc("number of " + cstr
+ " misses")
452 .flags(total
| nozero
| nonan
)
457 .name(name() + ".demand_misses")
458 .desc("number of demand (read+write) misses")
461 demandMisses
= misses
[Packet::ReadReq
] + misses
[Packet::WriteReq
];
464 .name(name() + ".overall_misses")
465 .desc("number of overall misses")
468 overallMisses
= demandMisses
+ misses
[Packet::SoftPFReq
] +
469 misses
[Packet::HardPFReq
] + misses
[Packet::Writeback
];
471 // Miss latency statistics
472 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
473 Packet::Command cmd
= (Packet::Command
)access_idx
;
474 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
476 missLatency
[access_idx
]
477 .init(maxThreadsPerCPU
)
478 .name(name() + "." + cstr
+ "_miss_latency")
479 .desc("number of " + cstr
+ " miss cycles")
480 .flags(total
| nozero
| nonan
)
485 .name(name() + ".demand_miss_latency")
486 .desc("number of demand (read+write) miss cycles")
489 demandMissLatency
= missLatency
[Packet::ReadReq
] + missLatency
[Packet::WriteReq
];
492 .name(name() + ".overall_miss_latency")
493 .desc("number of overall miss cycles")
496 overallMissLatency
= demandMissLatency
+ missLatency
[Packet::SoftPFReq
] +
497 missLatency
[Packet::HardPFReq
];
500 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
501 Packet::Command cmd
= (Packet::Command
)access_idx
;
502 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
505 .name(name() + "." + cstr
+ "_accesses")
506 .desc("number of " + cstr
+ " accesses(hits+misses)")
507 .flags(total
| nozero
| nonan
)
510 accesses
[access_idx
] = hits
[access_idx
] + misses
[access_idx
];
514 .name(name() + ".demand_accesses")
515 .desc("number of demand (read+write) accesses")
518 demandAccesses
= demandHits
+ demandMisses
;
521 .name(name() + ".overall_accesses")
522 .desc("number of overall (read+write) accesses")
525 overallAccesses
= overallHits
+ overallMisses
;
527 // miss rate formulas
528 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
529 Packet::Command cmd
= (Packet::Command
)access_idx
;
530 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
533 .name(name() + "." + cstr
+ "_miss_rate")
534 .desc("miss rate for " + cstr
+ " accesses")
535 .flags(total
| nozero
| nonan
)
538 missRate
[access_idx
] = misses
[access_idx
] / accesses
[access_idx
];
542 .name(name() + ".demand_miss_rate")
543 .desc("miss rate for demand accesses")
546 demandMissRate
= demandMisses
/ demandAccesses
;
549 .name(name() + ".overall_miss_rate")
550 .desc("miss rate for overall accesses")
553 overallMissRate
= overallMisses
/ overallAccesses
;
555 // miss latency formulas
556 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
557 Packet::Command cmd
= (Packet::Command
)access_idx
;
558 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
560 avgMissLatency
[access_idx
]
561 .name(name() + "." + cstr
+ "_avg_miss_latency")
562 .desc("average " + cstr
+ " miss latency")
563 .flags(total
| nozero
| nonan
)
566 avgMissLatency
[access_idx
] =
567 missLatency
[access_idx
] / misses
[access_idx
];
571 .name(name() + ".demand_avg_miss_latency")
572 .desc("average overall miss latency")
575 demandAvgMissLatency
= demandMissLatency
/ demandMisses
;
577 overallAvgMissLatency
578 .name(name() + ".overall_avg_miss_latency")
579 .desc("average overall miss latency")
582 overallAvgMissLatency
= overallMissLatency
/ overallMisses
;
584 blocked_cycles
.init(NUM_BLOCKED_CAUSES
);
586 .name(name() + ".blocked_cycles")
587 .desc("number of cycles access was blocked")
588 .subname(Blocked_NoMSHRs
, "no_mshrs")
589 .subname(Blocked_NoTargets
, "no_targets")
593 blocked_causes
.init(NUM_BLOCKED_CAUSES
);
595 .name(name() + ".blocked")
596 .desc("number of cycles access was blocked")
597 .subname(Blocked_NoMSHRs
, "no_mshrs")
598 .subname(Blocked_NoTargets
, "no_targets")
602 .name(name() + ".avg_blocked_cycles")
603 .desc("average number of cycles each access was blocked")
604 .subname(Blocked_NoMSHRs
, "no_mshrs")
605 .subname(Blocked_NoTargets
, "no_targets")
608 avg_blocked
= blocked_cycles
/ blocked_causes
;
611 .name(name() + ".fast_writes")
612 .desc("number of fast writes performed")
616 .name(name() + ".cache_copies")
617 .desc("number of cache copies performed")