2 * Copyright (c) 2002-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
45 #include "sim/host.hh"
46 #include "base/misc.hh"
49 #include "mem/cache/cache.hh"
50 #include "mem/cache/cache_blk.hh"
51 #include "mem/cache/miss/mshr.hh"
52 #include "mem/cache/prefetch/prefetcher.hh"
54 #include "sim/sim_exit.hh" // for SimExitEvent
56 template<class TagStore, class Buffering, class Coherence>
58 Cache<TagStore,Buffering,Coherence>::
59 doTimingAccess(Packet *pkt, CachePort *cachePort, bool isCpuSide)
63 if (pkt->isWrite() && (pkt->req->isLocked())) {
64 pkt->req->setScResult(1);
71 if (pkt->isResponse())
74 //Check if we should do the snoop
75 if (pkt->flags & SNOOP_COMMIT)
82 template<class TagStore, class Buffering, class Coherence>
84 Cache<TagStore,Buffering,Coherence>::
85 doAtomicAccess(Packet *pkt, bool isCpuSide)
89 //Temporary solution to LL/SC
90 if (pkt->isWrite() && (pkt->req->isLocked())) {
91 pkt->req->setScResult(1);
94 probe(pkt, true, NULL);
95 //TEMP ALWAYS SUCCES FOR NOW
96 pkt->result = Packet::Success;
100 if (pkt->isResponse())
105 //Fix this timing info
109 template<class TagStore, class Buffering, class Coherence>
111 Cache<TagStore,Buffering,Coherence>::
112 doFunctionalAccess(Packet *pkt, bool isCpuSide)
116 //TEMP USE CPU?THREAD 0 0
117 pkt->req->setThreadContext(0,0);
119 //Temporary solution to LL/SC
120 if (pkt->isWrite() && (pkt->req->isLocked())) {
121 assert("Can't handle LL/SC on functional path\n");
124 probe(pkt, false, memSidePort);
125 //TEMP ALWAYS SUCCESFUL FOR NOW
126 pkt->result = Packet::Success;
130 probe(pkt, false, cpuSidePort);
134 template<class TagStore, class Buffering, class Coherence>
136 Cache<TagStore,Buffering,Coherence>::
137 recvStatusChange(Port::Status status, bool isCpuSide)
143 template<class TagStore, class Buffering, class Coherence>
144 Cache<TagStore,Buffering,Coherence>::
145 Cache(const std::string &_name,
146 Cache<TagStore,Buffering,Coherence>::Params ¶ms)
147 : BaseCache(_name, params.baseParams),
148 prefetchAccess(params.prefetchAccess),
149 tags(params.tags), missQueue(params.missQueue),
150 coherence(params.coherence), prefetcher(params.prefetcher),
151 doCopy(params.doCopy), blockOnCopy(params.blockOnCopy)
154 // if (params.in == NULL) {
155 topLevelCache = true;
157 //PLEASE FIX THIS, BUS SIZES NOT BEING USED
158 tags->setCache(this, blkSize, 1/*params.out->width, params.out->clockRate*/);
159 tags->setPrefetcher(prefetcher);
160 missQueue->setCache(this);
161 missQueue->setPrefetcher(prefetcher);
162 coherence->setCache(this);
163 prefetcher->setCache(this);
164 prefetcher->setTags(tags);
165 prefetcher->setBuffer(missQueue);
166 invalidateReq = new Request((Addr) NULL, blkSize, 0);
167 invalidatePkt = new Packet(invalidateReq, Packet::InvalidateReq, 0);
170 template<class TagStore, class Buffering, class Coherence>
172 Cache<TagStore,Buffering,Coherence>::regStats()
174 BaseCache::regStats();
175 tags->regStats(name());
176 missQueue->regStats(name());
177 coherence->regStats(name());
178 prefetcher->regStats(name());
181 template<class TagStore, class Buffering, class Coherence>
183 Cache<TagStore,Buffering,Coherence>::access(PacketPtr &pkt)
185 //@todo Add back in MemDebug Calls
186 // MemDebug::cacheAccess(pkt);
188 PacketList writebacks;
190 int lat = hitLatency;
191 if (prefetchAccess) {
192 //We are determining prefetches on access stream, call prefetcher
193 prefetcher->handleMiss(pkt, curTick);
195 if (!pkt->req->isUncacheable()) {
196 blk = tags->handleAccess(pkt, lat, writebacks);
198 size = pkt->getSize();
200 // If this is a block size write/hint (WH64) allocate the block here
201 // if the coherence protocol allows it.
202 /** @todo make the fast write alloc (wh64) work with coherence. */
203 /** @todo Do we want to do fast writes for writebacks as well? */
204 if (!blk && pkt->getSize() >= blkSize && coherence->allowFastWrites() &&
205 (pkt->cmd == Packet::WriteReq || pkt->cmd == Packet::WriteInvalidateReq) ) {
206 // not outstanding misses, can do this
207 MSHR* outstanding_miss = missQueue->findMSHR(pkt->getAddr());
208 if (pkt->cmd == Packet::WriteInvalidateReq || !outstanding_miss) {
209 if (outstanding_miss) {
210 warn("WriteInv doing a fastallocate"
211 "with an outstanding miss to the same address\n");
213 blk = tags->handleFill(NULL, pkt, BlkValid | BlkWritable,
218 while (!writebacks.empty()) {
219 missQueue->doWriteback(writebacks.front());
220 writebacks.pop_front();
222 DPRINTF(Cache, "%s %x %s blk_addr: %x\n", pkt->cmdString(),
223 pkt->getAddr() & (((ULL(1))<<48)-1), (blk) ? "hit" : "miss",
224 pkt->getAddr() & ~((Addr)blkSize - 1));
227 hits[pkt->cmdToIndex()][0/*pkt->req->getThreadNum()*/]++;
228 // clear dirty bit if write through
229 if (pkt->needsResponse())
230 respond(pkt, curTick+lat);
231 if (pkt->cmd == Packet::Writeback) {
232 //Signal that you can kill the pkt/req
233 pkt->flags |= SATISFIED;
239 if (!pkt->req->isUncacheable()) {
240 misses[pkt->cmdToIndex()][0/*pkt->req->getThreadNum()*/]++;
241 /** @todo Move miss count code into BaseCache */
245 exitSimLoop("A cache reached the maximum miss count");
248 missQueue->handleMiss(pkt, size, curTick + hitLatency);
249 // return MA_CACHE_MISS;
254 template<class TagStore, class Buffering, class Coherence>
256 Cache<TagStore,Buffering,Coherence>::getPacket()
258 assert(missQueue->havePending());
259 Packet * pkt = missQueue->getPacket();
261 if (!pkt->req->isUncacheable()) {
262 if (pkt->cmd == Packet::HardPFReq) misses[Packet::HardPFReq][0/*pkt->req->getThreadNum()*/]++;
263 BlkType *blk = tags->findBlock(pkt);
264 Packet::Command cmd = coherence->getBusCmd(pkt->cmd,
265 (blk)? blk->status : 0);
266 missQueue->setBusCmd(pkt, cmd);
270 assert(!doMasterRequest() || missQueue->havePending());
271 assert(!pkt || pkt->time <= curTick);
275 template<class TagStore, class Buffering, class Coherence>
277 Cache<TagStore,Buffering,Coherence>::sendResult(PacketPtr &pkt, MSHR* mshr, bool success)
279 if (success && !(pkt->flags & NACKED_LINE)) {
280 missQueue->markInService(pkt, mshr);
281 //Temp Hack for UPGRADES
282 if (pkt->cmd == Packet::UpgradeReq) {
283 pkt->flags &= ~CACHE_LINE_FILL;
284 BlkType *blk = tags->findBlock(pkt);
285 CacheBlk::State old_state = (blk) ? blk->status : 0;
286 CacheBlk::State new_state = coherence->getNewState(pkt,old_state);
287 DPRINTF(Cache, "Block for blk addr %x moving from state %i to %i\n",
288 pkt->getAddr() & (((ULL(1))<<48)-1), old_state, new_state);
289 //Set the state on the upgrade
290 memcpy(pkt->getPtr<uint8_t>(), blk->data, blkSize);
291 PacketList writebacks;
292 tags->handleFill(blk, mshr, new_state, writebacks, pkt);
293 assert(writebacks.empty());
294 missQueue->handleResponse(pkt, curTick + hitLatency);
296 } else if (pkt && !pkt->req->isUncacheable()) {
297 pkt->flags &= ~NACKED_LINE;
298 pkt->flags &= ~SATISFIED;
299 pkt->flags &= ~SNOOP_COMMIT;
300 missQueue->restoreOrigCmd(pkt);
304 template<class TagStore, class Buffering, class Coherence>
306 Cache<TagStore,Buffering,Coherence>::handleResponse(Packet * &pkt)
309 if (pkt->senderState) {
310 if (pkt->result == Packet::Nacked) {
311 //pkt->reinitFromRequest();
312 warn("NACKs from devices not connected to the same bus not implemented\n");
315 if (pkt->result == Packet::BadAddress) {
316 //Make the response a Bad address and send it
318 // MemDebug::cacheResponse(pkt);
319 DPRINTF(Cache, "Handling reponse to %x, blk addr: %x\n",pkt->getAddr(),
320 pkt->getAddr() & (((ULL(1))<<48)-1));
322 if (pkt->isCacheFill() && !pkt->isNoAllocate()) {
323 blk = tags->findBlock(pkt);
324 CacheBlk::State old_state = (blk) ? blk->status : 0;
325 PacketList writebacks;
326 CacheBlk::State new_state = coherence->getNewState(pkt,old_state);
327 DPRINTF(Cache, "Block for blk addr %x moving from state %i to %i\n",
328 pkt->getAddr() & (((ULL(1))<<48)-1), old_state, new_state);
329 blk = tags->handleFill(blk, (MSHR*)pkt->senderState,
330 new_state, writebacks, pkt);
331 while (!writebacks.empty()) {
332 missQueue->doWriteback(writebacks.front());
333 writebacks.pop_front();
336 missQueue->handleResponse(pkt, curTick + hitLatency);
340 template<class TagStore, class Buffering, class Coherence>
342 Cache<TagStore,Buffering,Coherence>::pseudoFill(Addr addr)
344 // Need to temporarily move this blk into MSHRs
345 MSHR *mshr = missQueue->allocateTargetList(addr);
348 // Read the data into the mshr
349 BlkType *blk = tags->handleAccess(mshr->pkt, lat, dummy, false);
350 assert(dummy.empty());
351 assert(mshr->pkt->flags & SATISFIED);
352 // can overload order since it isn't used on non pending blocks
353 mshr->order = blk->status;
354 // temporarily remove the block from the cache.
355 tags->invalidateBlk(addr);
358 template<class TagStore, class Buffering, class Coherence>
360 Cache<TagStore,Buffering,Coherence>::pseudoFill(MSHR *mshr)
362 // Need to temporarily move this blk into MSHRs
363 assert(mshr->pkt->cmd == Packet::ReadReq);
366 // Read the data into the mshr
367 BlkType *blk = tags->handleAccess(mshr->pkt, lat, dummy, false);
368 assert(dummy.empty());
369 assert(mshr->pkt->flags & SATISFIED);
370 // can overload order since it isn't used on non pending blocks
371 mshr->order = blk->status;
372 // temporarily remove the block from the cache.
373 tags->invalidateBlk(mshr->pkt->getAddr());
377 template<class TagStore, class Buffering, class Coherence>
379 Cache<TagStore,Buffering,Coherence>::getCoherencePacket()
381 return coherence->getPacket();
385 template<class TagStore, class Buffering, class Coherence>
387 Cache<TagStore,Buffering,Coherence>::snoop(Packet * &pkt)
389 Addr blk_addr = pkt->getAddr() & ~(Addr(blkSize-1));
390 BlkType *blk = tags->findBlock(pkt);
391 MSHR *mshr = missQueue->findMSHR(blk_addr);
392 if (isTopLevel() && coherence->hasProtocol()) { //@todo Move this into handle bus req
393 //If we find an mshr, and it is in service, we need to NACK or invalidate
395 if (mshr->inService) {
396 if ((mshr->pkt->isInvalidate() || !mshr->pkt->isCacheFill())
397 && (pkt->cmd != Packet::InvalidateReq && pkt->cmd != Packet::WriteInvalidateReq)) {
398 //If the outstanding request was an invalidate (upgrade,readex,..)
399 //Then we need to ACK the request until we get the data
400 //Also NACK if the outstanding request is not a cachefill (writeback)
401 assert(!(pkt->flags & SATISFIED));
402 pkt->flags |= SATISFIED;
403 pkt->flags |= NACKED_LINE;
404 ///@todo NACK's from other levels
405 //warn("NACKs from devices not connected to the same bus not implemented\n");
406 //respondToSnoop(pkt, curTick + hitLatency);
410 //The supplier will be someone else, because we are waiting for
411 //the data. This should cause this cache to be forced to go to
412 //the shared state, not the exclusive even though the shared line
413 //won't be asserted. But for now we will just invlidate ourselves
414 //and allow the other cache to go into the exclusive state.
415 //@todo Make it so a read to a pending read doesn't invalidate.
416 //@todo Make it so that a read to a pending read can't be exclusive now.
418 //Set the address so find match works
419 //panic("Don't have invalidates yet\n");
420 invalidatePkt->addrOverride(pkt->getAddr());
422 //Append the invalidate on
423 missQueue->addTarget(mshr,invalidatePkt);
424 DPRINTF(Cache, "Appending Invalidate to blk_addr: %x\n", pkt->getAddr() & (((ULL(1))<<48)-1));
429 //We also need to check the writeback buffers and handle those
430 std::vector<MSHR *> writebacks;
431 if (missQueue->findWrites(blk_addr, writebacks)) {
432 DPRINTF(Cache, "Snoop hit in writeback to blk_addr: %x\n", pkt->getAddr() & (((ULL(1))<<48)-1));
434 //Look through writebacks for any non-uncachable writes, use that
435 for (int i=0; i<writebacks.size(); i++) {
436 mshr = writebacks[i];
438 if (!mshr->pkt->req->isUncacheable()) {
440 //Only Upgrades don't get here
442 assert(!(pkt->flags & SATISFIED));
443 pkt->flags |= SATISFIED;
445 //If we are in an exclusive protocol, make it ask again
446 //to get write permissions (upgrade), signal shared
447 pkt->flags |= SHARED_LINE;
449 assert(pkt->isRead());
450 Addr offset = pkt->getAddr() & (blkSize - 1);
451 assert(offset < blkSize);
452 assert(pkt->getSize() <= blkSize);
453 assert(offset + pkt->getSize() <=blkSize);
454 memcpy(pkt->getPtr<uint8_t>(), mshr->pkt->getPtr<uint8_t>() + offset, pkt->getSize());
456 respondToSnoop(pkt, curTick + hitLatency);
459 if (pkt->isInvalidate()) {
460 //This must be an upgrade or other cache will take ownership
461 missQueue->markInService(mshr->pkt, mshr);
468 CacheBlk::State new_state;
469 bool satisfy = coherence->handleBusRequest(pkt,blk,mshr, new_state);
471 DPRINTF(Cache, "Cache snooped a %s request for addr %x and now supplying data,"
473 pkt->cmdString(), blk_addr, new_state);
475 tags->handleSnoop(blk, new_state, pkt);
476 respondToSnoop(pkt, curTick + hitLatency);
479 if (blk) DPRINTF(Cache, "Cache snooped a %s request for addr %x, new state is %i\n",
480 pkt->cmdString(), blk_addr, new_state);
481 tags->handleSnoop(blk, new_state);
484 template<class TagStore, class Buffering, class Coherence>
486 Cache<TagStore,Buffering,Coherence>::snoopResponse(Packet * &pkt)
488 //Need to handle the response, if NACKED
489 if (pkt->flags & NACKED_LINE) {
490 //Need to mark it as not in service, and retry for bus
491 assert(0); //Yeah, we saw a NACK come through
493 //For now this should never get called, we return false when we see a NACK
494 //instead, by doing this we allow the bus_blocked mechanism to handle the retry
495 //For now it retrys in just 2 cycles, need to figure out how to change that
496 //Eventually we will want to also have success come in as a parameter
497 //Need to make sure that we handle the functionality that happens on successufl
498 //return of the sendAddr function
502 template<class TagStore, class Buffering, class Coherence>
504 Cache<TagStore,Buffering,Coherence>::invalidateBlk(Addr addr)
506 tags->invalidateBlk(addr);
511 * @todo Fix to not assume write allocate
513 template<class TagStore, class Buffering, class Coherence>
515 Cache<TagStore,Buffering,Coherence>::probe(Packet * &pkt, bool update, CachePort* otherSidePort)
517 // MemDebug::cacheProbe(pkt);
518 if (!pkt->req->isUncacheable()) {
519 if (pkt->isInvalidate() && !pkt->isRead()
520 && !pkt->isWrite()) {
521 //Upgrade or Invalidate, satisfy it, don't forward
522 DPRINTF(Cache, "%s %x ? blk_addr: %x\n", pkt->cmdString(),
523 pkt->getAddr() & (((ULL(1))<<48)-1),
524 pkt->getAddr() & ~((Addr)blkSize - 1));
525 pkt->flags |= SATISFIED;
530 PacketList writebacks;
532 BlkType *blk = tags->handleAccess(pkt, lat, writebacks, update);
535 // Need to check for outstanding misses and writes
536 Addr blk_addr = pkt->getAddr() & ~(blkSize - 1);
538 // There can only be one matching outstanding miss.
539 MSHR* mshr = missQueue->findMSHR(blk_addr);
541 // There can be many matching outstanding writes.
542 std::vector<MSHR*> writes;
543 missQueue->findWrites(blk_addr, writes);
546 otherSidePort->sendFunctional(pkt);
548 // Check for data in MSHR and writebuffer.
550 warn("Found outstanding miss on an non-update probe");
551 MSHR::TargetList *targets = mshr->getTargetList();
552 MSHR::TargetList::iterator i = targets->begin();
553 MSHR::TargetList::iterator end = targets->end();
554 for (; i != end; ++i) {
555 Packet * target = *i;
556 // If the target contains data, and it overlaps the
557 // probed request, need to update data
558 if (target->isWrite() && target->intersect(pkt)) {
562 if (target->getAddr() < pkt->getAddr()) {
563 int offset = pkt->getAddr() - target->getAddr();
564 pkt_data = pkt->getPtr<uint8_t>();
565 write_data = target->getPtr<uint8_t>() + offset;
566 data_size = target->getSize() - offset;
567 assert(data_size > 0);
568 if (data_size > pkt->getSize())
569 data_size = pkt->getSize();
571 int offset = target->getAddr() - pkt->getAddr();
572 pkt_data = pkt->getPtr<uint8_t>() + offset;
573 write_data = target->getPtr<uint8_t>();
574 data_size = pkt->getSize() - offset;
575 assert(data_size > pkt->getSize());
576 if (data_size > target->getSize())
577 data_size = target->getSize();
580 if (pkt->isWrite()) {
581 memcpy(pkt_data, write_data, data_size);
583 memcpy(write_data, pkt_data, data_size);
588 for (int i = 0; i < writes.size(); ++i) {
589 Packet * write = writes[i]->pkt;
590 if (write->intersect(pkt)) {
591 warn("Found outstanding write on an non-update probe");
595 if (write->getAddr() < pkt->getAddr()) {
596 int offset = pkt->getAddr() - write->getAddr();
597 pkt_data = pkt->getPtr<uint8_t>();
598 write_data = write->getPtr<uint8_t>() + offset;
599 data_size = write->getSize() - offset;
600 assert(data_size > 0);
601 if (data_size > pkt->getSize())
602 data_size = pkt->getSize();
604 int offset = write->getAddr() - pkt->getAddr();
605 pkt_data = pkt->getPtr<uint8_t>() + offset;
606 write_data = write->getPtr<uint8_t>();
607 data_size = pkt->getSize() - offset;
608 assert(data_size > pkt->getSize());
609 if (data_size > write->getSize())
610 data_size = write->getSize();
613 if (pkt->isWrite()) {
614 memcpy(pkt_data, write_data, data_size);
616 memcpy(write_data, pkt_data, data_size);
623 // update the cache state and statistics
624 if (mshr || !writes.empty()){
625 // Can't handle it, return pktuest unsatisfied.
626 panic("Atomic access ran into outstanding MSHR's or WB's!");
628 if (!pkt->req->isUncacheable()) {
629 // Fetch the cache block to fill
630 BlkType *blk = tags->findBlock(pkt);
631 Packet::Command temp_cmd = coherence->getBusCmd(pkt->cmd,
632 (blk)? blk->status : 0);
634 Packet * busPkt = new Packet(pkt->req,temp_cmd, -1, blkSize);
638 busPkt->time = curTick;
640 lat = memSidePort->sendAtomic(busPkt);
642 //Be sure to flip the response to a request for coherence
643 if (busPkt->needsResponse()) {
644 busPkt->makeAtomicResponse();
647 /* if (!(busPkt->flags & SATISFIED)) {
648 // blocked at a higher level, just return
652 */ misses[pkt->cmdToIndex()][0/*pkt->req->getThreadNum()*/]++;
654 CacheBlk::State old_state = (blk) ? blk->status : 0;
655 tags->handleFill(blk, busPkt,
656 coherence->getNewState(busPkt, old_state),
658 // Handle writebacks if needed
659 while (!writebacks.empty()){
660 memSidePort->sendAtomic(writebacks.front());
661 writebacks.pop_front();
663 return lat + hitLatency;
665 return memSidePort->sendAtomic(pkt);
669 // There was a cache hit.
670 // Handle writebacks if needed
671 while (!writebacks.empty()){
672 memSidePort->sendAtomic(writebacks.front());
673 writebacks.pop_front();
677 hits[pkt->cmdToIndex()][0/*pkt->req->getThreadNum()*/]++;
678 } else if (pkt->isWrite()) {
679 // Still need to change data in all locations.
680 otherSidePort->sendFunctional(pkt);
682 return curTick + lat;
684 fatal("Probe not handled.\n");
688 template<class TagStore, class Buffering, class Coherence>
690 Cache<TagStore,Buffering,Coherence>::snoopProbe(PacketPtr &pkt)
692 Addr blk_addr = pkt->getAddr() & ~(Addr(blkSize-1));
693 BlkType *blk = tags->findBlock(pkt);
694 MSHR *mshr = missQueue->findMSHR(blk_addr);
695 CacheBlk::State new_state = 0;
696 bool satisfy = coherence->handleBusRequest(pkt,blk,mshr, new_state);
698 DPRINTF(Cache, "Cache snooped a %s request for addr %x and now supplying data,"
700 pkt->cmdString(), blk_addr, new_state);
702 tags->handleSnoop(blk, new_state, pkt);
705 if (blk) DPRINTF(Cache, "Cache snooped a %s request for addr %x, new state is %i\n",
706 pkt->cmdString(), blk_addr, new_state);
707 tags->handleSnoop(blk, new_state);