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 "mem/bus/bus.hh"
56 #include "mem/bus/slave_interface.hh"
57 #include "mem/memory_interface.hh"
58 #include "mem/bus/master_interface.hh"
60 #include "mem/mem_debug.hh"
62 #include "sim/sim_events.hh" // for SimExitEvent
66 template<class TagStore, class Buffering, class Coherence>
68 Cache<TagStore,Buffering,Coherence>::
69 doTimingAccess(Packet *pkt, MemoryPort *memoryPort, bool isCpuSide)
84 template<class TagStore, class Buffering, class Coherence>
86 Cache<TagStore,Buffering,Coherence>::
87 doAtomicAccess(Packet *pkt, MemoryPort *memoryPort, bool isCpuSide)
98 snoopProbe(pkt, true);
102 template<class TagStore, class Buffering, class Coherence>
104 Cache<TagStore,Buffering,Coherence>::
105 doFunctionalAccess(Packet *pkt, MemoryPort *memoryPort, bool isCpuSide)
113 if (pkt->isRespnse())
116 snoopProbe(pkt, false);
120 template<class TagStore, class Buffering, class Coherence>
122 Cache<TagStore,Buffering,Coherence>::
123 recvStatusChange(Port::Status status, bool isCpuSide)
129 template<class TagStore, class Buffering, class Coherence>
130 Cache<TagStore,Buffering,Coherence>::
131 Cache(const std::string &_name, HierParams *hier_params,
132 Cache<TagStore,Buffering,Coherence>::Params ¶ms)
133 : BaseCache(_name, hier_params, params.baseParams),
134 prefetchAccess(params.prefetchAccess),
135 tags(params.tags), missQueue(params.missQueue),
136 coherence(params.coherence), prefetcher(params.prefetcher),
137 doCopy(params.doCopy), blockOnCopy(params.blockOnCopy)
139 if (params.in == NULL) {
140 topLevelCache = true;
142 tags->setCache(this, params.out->width, params.out->clockRate);
143 tags->setPrefetcher(prefetcher);
144 missQueue->setCache(this);
145 missQueue->setPrefetcher(prefetcher);
146 coherence->setCache(this);
147 prefetcher->setCache(this);
148 prefetcher->setTags(tags);
149 prefetcher->setBuffer(missQueue);
150 invalidatePkt = new Packet;
151 invalidatePkt->cmd = Invalidate;
154 template<class TagStore, class Buffering, class Coherence>
156 Cache<TagStore,Buffering,Coherence>::regStats()
158 BaseCache::regStats();
159 tags->regStats(name());
160 missQueue->regStats(name());
161 coherence->regStats(name());
162 prefetcher->regStats(name());
165 template<class TagStore, class Buffering, class Coherence>
167 Cache<TagStore,Buffering,Coherence>::access(Packet &pkt)
169 MemDebug::cacheAccess(pkt);
171 PacketList* writebacks;
173 int lat = hitLatency;
174 if (prefetchAccess) {
175 //We are determining prefetches on access stream, call prefetcher
176 prefetcher->handleMiss(pkt, curTick);
178 if (!pkt->isUncacheable()) {
179 if (pkt->cmd.isInvalidate() && !pkt->cmd.isRead()
180 && !pkt->cmd.isWrite()) {
181 //Upgrade or Invalidate
182 //Look into what happens if two slave caches on bus
183 DPRINTF(Cache, "%s %d %x ? blk_addr: %x\n", pkt->cmd.toString(),
184 pkt->req->asid, pkt->paddr & (((ULL(1))<<48)-1),
185 pkt->paddr & ~((Addr)blkSize - 1));
187 //@todo Should this return latency have the hit latency in it?
188 // respond(pkt,curTick+lat);
189 pkt->flags |= SATISFIED;
192 blk = tags->handleAccess(pkt, lat, writebacks);
196 // If this is a block size write/hint (WH64) allocate the block here
197 // if the coherence protocol allows it.
198 /** @todo make the fast write alloc (wh64) work with coherence. */
199 /** @todo Do we want to do fast writes for writebacks as well? */
200 if (!blk && pkt->size >= blkSize && coherence->allowFastWrites() &&
201 (pkt->cmd == Write || pkt->cmd == WriteInvalidate) ) {
202 // not outstanding misses, can do this
203 MSHR* outstanding_miss = missQueue->findMSHR(pkt->paddr, pkt->req->asid);
204 if (pkt->cmd ==WriteInvalidate || !outstanding_miss) {
205 if (outstanding_miss) {
206 warn("WriteInv doing a fastallocate"
207 "with an outstanding miss to the same address\n");
209 blk = tags->handleFill(NULL, pkt, BlkValid | BlkWritable,
214 while (!writebacks.empty()) {
215 missQueue->doWriteback(writebacks.front());
216 writebacks.pop_front();
218 DPRINTF(Cache, "%s %d %x %s blk_addr: %x pc %x\n", pkt->cmd.toString(),
219 pkt->req->asid, pkt->paddr & (((ULL(1))<<48)-1), (blk) ? "hit" : "miss",
220 pkt->paddr & ~((Addr)blkSize - 1), pkt->pc);
223 hits[pkt->cmd.toIndex()][pkt->thread_num]++;
224 // clear dirty bit if write through
225 if (!pkt->cmd.isNoResponse())
226 respond(pkt, curTick+lat);
231 if (!pkt->isUncacheable()) {
232 misses[pkt->cmd.toIndex()][pkt->thread_num]++;
233 /** @todo Move miss count code into BaseCache */
237 new SimExitEvent("A cache reached the maximum miss count");
240 missQueue->handleMiss(pkt, size, curTick + hitLatency);
241 return MA_CACHE_MISS;
245 template<class TagStore, class Buffering, class Coherence>
247 Cache<TagStore,Buffering,Coherence>::getPacket()
249 Packet * pkt = missQueue->getPacket();
251 if (!pkt->isUncacheable()) {
252 if (pkt->cmd == Hard_Prefetch) misses[Hard_Prefetch][pkt->thread_num]++;
253 BlkType *blk = tags->findBlock(pkt);
254 Packet::Command cmd = coherence->getBusCmd(pkt->cmd,
255 (blk)? blk->status : 0);
256 missQueue->setBusCmd(pkt, cmd);
260 assert(!doMasterPktuest() || missQueue->havePending());
261 assert(!pkt || pkt->time <= curTick);
265 template<class TagStore, class Buffering, class Coherence>
267 Cache<TagStore,Buffering,Coherence>::sendResult(MemPktPtr &pkt, bool success)
270 missQueue->markInService(pkt);
271 //Temp Hack for UPGRADES
272 if (pkt->cmd == Upgrade) {
275 } else if (pkt && !pkt->isUncacheable()) {
276 missQueue->restoreOrigCmd(pkt);
280 template<class TagStore, class Buffering, class Coherence>
282 Cache<TagStore,Buffering,Coherence>::handleResponse(Packet * &pkt)
285 if (pkt->senderState) {
286 MemDebug::cacheResponse(pkt);
287 DPRINTF(Cache, "Handling reponse to %x, blk addr: %x\n",pkt->paddr,
288 pkt->paddr & (((ULL(1))<<48)-1));
290 if (pkt->isCacheFill() && !pkt->isNoAllocate()) {
291 blk = tags->findBlock(pkt);
292 CacheBlk::State old_state = (blk) ? blk->status : 0;
293 MemPktList writebacks;
294 blk = tags->handleFill(blk, pkt->senderState,
295 coherence->getNewState(pkt,old_state),
297 while (!writebacks.empty()) {
298 missQueue->doWriteback(writebacks.front());
301 missQueue->handleResponse(pkt, curTick + hitLatency);
305 template<class TagStore, class Buffering, class Coherence>
307 Cache<TagStore,Buffering,Coherence>::pseudoFill(Addr addr, int asid)
309 // Need to temporarily move this blk into MSHRs
310 MSHR *mshr = missQueue->allocateTargetList(addr, asid);
313 // Read the data into the mshr
314 BlkType *blk = tags->handleAccess(mshr->pkt, lat, dummy, false);
315 assert(dummy.empty());
316 assert(mshr->pkt->isSatisfied());
317 // can overload order since it isn't used on non pending blocks
318 mshr->order = blk->status;
319 // temporarily remove the block from the cache.
320 tags->invalidateBlk(addr, asid);
323 template<class TagStore, class Buffering, class Coherence>
325 Cache<TagStore,Buffering,Coherence>::pseudoFill(MSHR *mshr)
327 // Need to temporarily move this blk into MSHRs
328 assert(mshr->pkt->cmd == Read);
331 // Read the data into the mshr
332 BlkType *blk = tags->handleAccess(mshr->pkt, lat, dummy, false);
333 assert(dummy.empty());
334 assert(mshr->pkt->isSatisfied());
335 // can overload order since it isn't used on non pending blocks
336 mshr->order = blk->status;
337 // temporarily remove the block from the cache.
338 tags->invalidateBlk(mshr->pkt->paddr, mshr->pkt->req->asid);
342 template<class TagStore, class Buffering, class Coherence>
344 Cache<TagStore,Buffering,Coherence>::getCoherenceReq()
346 return coherence->getPacket();
350 template<class TagStore, class Buffering, class Coherence>
352 Cache<TagStore,Buffering,Coherence>::snoop(Packet * &pkt)
354 Addr blk_addr = pkt->paddr & ~(Addr(blkSize-1));
355 BlkType *blk = tags->findBlock(pkt);
356 MSHR *mshr = missQueue->findMSHR(blk_addr, pkt->req->asid);
357 if (isTopLevel() && coherence->hasProtocol()) { //@todo Move this into handle bus req
358 //If we find an mshr, and it is in service, we need to NACK or invalidate
360 if (mshr->inService) {
361 if ((mshr->pkt->cmd.isInvalidate() || !mshr->pkt->isCacheFill())
362 && (pkt->cmd != Invalidate && pkt->cmd != WriteInvalidate)) {
363 //If the outstanding request was an invalidate (upgrade,readex,..)
364 //Then we need to ACK the request until we get the data
365 //Also NACK if the outstanding request is not a cachefill (writeback)
366 pkt->flags |= NACKED_LINE;
370 //The supplier will be someone else, because we are waiting for
371 //the data. This should cause this cache to be forced to go to
372 //the shared state, not the exclusive even though the shared line
373 //won't be asserted. But for now we will just invlidate ourselves
374 //and allow the other cache to go into the exclusive state.
375 //@todo Make it so a read to a pending read doesn't invalidate.
376 //@todo Make it so that a read to a pending read can't be exclusive now.
378 //Set the address so find match works
379 invalidatePkt->paddr = pkt->paddr;
381 //Append the invalidate on
382 missQueue->addTarget(mshr,invalidatePkt);
383 DPRINTF(Cache, "Appending Invalidate to blk_addr: %x\n", pkt->paddr & (((ULL(1))<<48)-1));
388 //We also need to check the writeback buffers and handle those
389 std::vector<MSHR *> writebacks;
390 if (missQueue->findWrites(blk_addr, pkt->req->asid, writebacks)) {
391 DPRINTF(Cache, "Snoop hit in writeback to blk_addr: %x\n", pkt->paddr & (((ULL(1))<<48)-1));
393 //Look through writebacks for any non-uncachable writes, use that
394 for (int i=0; i<writebacks.size(); i++) {
395 mshr = writebacks[i];
397 if (!mshr->pkt->isUncacheable()) {
398 if (pkt->cmd.isRead()) {
399 //Only Upgrades don't get here
401 pkt->flags |= SATISFIED;
403 //If we are in an exclusive protocol, make it ask again
404 //to get write permissions (upgrade), signal shared
405 pkt->flags |= SHARED_LINE;
408 assert(pkt->cmd.isRead());
410 assert(pkt->offset < blkSize);
411 assert(pkt->size <= blkSize);
412 assert(pkt->offset + pkt->size <=blkSize);
413 memcpy(pkt->data, mshr->pkt->data + pkt->offset, pkt->size);
418 if (pkt->cmd.isInvalidate()) {
419 //This must be an upgrade or other cache will take ownership
420 missQueue->markInService(mshr->pkt);
427 CacheBlk::State new_state;
428 bool satisfy = coherence->handleBusRequest(pkt,blk,mshr, new_state);
430 tags->handleSnoop(blk, new_state, pkt);
434 tags->handleSnoop(blk, new_state);
437 template<class TagStore, class Buffering, class Coherence>
439 Cache<TagStore,Buffering,Coherence>::snoopResponse(Packet * &pkt)
441 //Need to handle the response, if NACKED
442 if (pkt->isNacked()) {
443 //Need to mark it as not in service, and retry for bus
444 assert(0); //Yeah, we saw a NACK come through
446 //For now this should never get called, we return false when we see a NACK
447 //instead, by doing this we allow the bus_blocked mechanism to handle the retry
448 //For now it retrys in just 2 cycles, need to figure out how to change that
449 //Eventually we will want to also have success come in as a parameter
450 //Need to make sure that we handle the functionality that happens on successufl
451 //return of the sendAddr function
455 template<class TagStore, class Buffering, class Coherence>
457 Cache<TagStore,Buffering,Coherence>::invalidateBlk(Addr addr, int asid)
459 tags->invalidateBlk(addr,asid);
464 * @todo Fix to not assume write allocate
466 template<class TagStore, class Buffering, class Coherence>
468 Cache<TagStore,Buffering,Coherence>::probe(Packet * &pkt, bool update)
470 MemDebug::cacheProbe(pkt);
472 if (!pkt->isUncacheable()) {
473 if (pkt->cmd.isInvalidate() && !pkt->cmd.isRead()
474 && !pkt->cmd.isWrite()) {
475 //Upgrade or Invalidate, satisfy it, don't forward
476 DPRINTF(Cache, "%s %d %x ? blk_addr: %x\n", pkt->cmd.toString(),
477 pkt->req->asid, pkt->paddr & (((ULL(1))<<48)-1),
478 pkt->paddr & ~((Addr)blkSize - 1));
479 pkt->flags |= SATISFIED;
484 if (!update && !doData()) {
485 // Nothing to do here
486 return mi->sendProbe(pkt,update);
489 PacketList* writebacks;
491 BlkType *blk = tags->handleAccess(pkt, lat, writebacks, update);
494 // Need to check for outstanding misses and writes
495 Addr blk_addr = pkt->paddr & ~(blkSize - 1);
497 // There can only be one matching outstanding miss.
498 MSHR* mshr = missQueue->findMSHR(blk_addr, pkt->req->asid);
500 // There can be many matching outstanding writes.
501 vector<MSHR*> writes;
502 missQueue->findWrites(blk_addr, pkt->req->asid, writes);
505 mi->sendProbe(pkt, update);
506 // Check for data in MSHR and writebuffer.
508 warn("Found outstanding miss on an non-update probe");
509 MSHR::TargetList *targets = mshr->getTargetList();
510 MSHR::TargetList::iterator i = targets->begin();
511 MSHR::TargetList::iterator end = targets->end();
512 for (; i != end; ++i) {
513 Packet * target = *i;
514 // If the target contains data, and it overlaps the
515 // probed request, need to update data
516 if (target->cmd.isWrite() && target->overlaps(pkt)) {
520 if (target->paddr < pkt->paddr) {
521 int offset = pkt->paddr - target->paddr;
522 pkt_data = pkt->data;
523 write_data = target->data + offset;
524 data_size = target->size - offset;
525 assert(data_size > 0);
526 if (data_size > pkt->size)
527 data_size = pkt->size;
529 int offset = target->paddr - pkt->paddr;
530 pkt_data = pkt->data + offset;
531 write_data = target->data;
532 data_size = pkt->size - offset;
533 assert(data_size > pkt->size);
534 if (data_size > target->size)
535 data_size = target->size;
538 if (pkt->cmd.isWrite()) {
539 memcpy(pkt_data, write_data, data_size);
541 memcpy(write_data, pkt_data, data_size);
546 for (int i = 0; i < writes.size(); ++i) {
547 Packet * write = writes[i]->pkt;
548 if (write->overlaps(pkt)) {
549 warn("Found outstanding write on an non-update probe");
553 if (write->paddr < pkt->paddr) {
554 int offset = pkt->paddr - write->paddr;
555 pkt_data = pkt->data;
556 write_data = write->data + offset;
557 data_size = write->size - offset;
558 assert(data_size > 0);
559 if (data_size > pkt->size)
560 data_size = pkt->size;
562 int offset = write->paddr - pkt->paddr;
563 pkt_data = pkt->data + offset;
564 write_data = write->data;
565 data_size = pkt->size - offset;
566 assert(data_size > pkt->size);
567 if (data_size > write->size)
568 data_size = write->size;
571 if (pkt->cmd.isWrite()) {
572 memcpy(pkt_data, write_data, data_size);
574 memcpy(write_data, pkt_data, data_size);
581 // update the cache state and statistics
582 if (mshr || !writes.empty()){
583 // Can't handle it, return pktuest unsatisfied.
586 if (!pkt->isUncacheable()) {
587 // Fetch the cache block to fill
588 Packet * busPkt = new MemPkt();
589 busPkt->paddr = blk_addr;
590 busPkt->size = blkSize;
591 busPkt->data = new uint8_t[blkSize];
593 BlkType *blk = tags->findBlock(pkt);
594 busPkt->cmd = coherence->getBusCmd(pkt->cmd,
595 (blk)? blk->status : 0);
597 busPkt->req->asid = pkt->req->asid;
598 busPkt->xc = pkt->xc;
599 busPkt->thread_num = pkt->thread_num;
600 busPkt->time = curTick;
602 lat = mi->sendProbe(busPkt, update);
604 if (!busPkt->isSatisfied()) {
605 // blocked at a higher level, just return
609 misses[pkt->cmd.toIndex()][pkt->thread_num]++;
611 CacheBlk::State old_state = (blk) ? blk->status : 0;
612 tags->handleFill(blk, busPkt,
613 coherence->getNewState(busPkt, old_state),
615 // Handle writebacks if needed
616 while (!writebacks.empty()){
617 mi->sendProbe(writebacks.front(), update);
618 writebacks.pop_front();
620 return lat + hitLatency;
622 return mi->sendProbe(pkt,update);
626 // There was a cache hit.
627 // Handle writebacks if needed
628 while (!writebacks.empty()){
629 mi->sendProbe(writebacks.front(), update);
630 writebacks.pop_front();
634 hits[pkt->cmd.toIndex()][pkt->thread_num]++;
635 } else if (pkt->cmd.isWrite()) {
636 // Still need to change data in all locations.
637 return mi->sendProbe(pkt, update);
639 return curTick + lat;
641 fatal("Probe not handled.\n");
645 template<class TagStore, class Buffering, class Coherence>
647 Cache<TagStore,Buffering,Coherence>::snoopProbe(MemPktPtr &pkt, bool update)
649 Addr blk_addr = pkt->paddr & ~(Addr(blkSize-1));
650 BlkType *blk = tags->findBlock(pkt);
651 MSHR *mshr = missQueue->findMSHR(blk_addr, pkt->req->asid);
652 CacheBlk::State new_state = 0;
653 bool satisfy = coherence->handleBusPktuest(pkt,blk,mshr, new_state);
655 tags->handleSnoop(blk, new_state, pkt);
658 tags->handleSnoop(blk, new_state);