2 * Copyright (c) 2010 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * Copyright (c) 2010 Advanced Micro Devices, Inc.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * Authors: Erik Hallnor
53 #include "base/fast_alloc.hh"
54 #include "base/misc.hh"
55 #include "base/range.hh"
56 #include "base/types.hh"
57 #include "debug/Cache.hh"
58 #include "debug/CachePort.hh"
59 #include "mem/cache/prefetch/base.hh"
60 #include "mem/cache/blk.hh"
61 #include "mem/cache/cache.hh"
62 #include "mem/cache/mshr.hh"
63 #include "sim/sim_exit.hh"
65 template<class TagStore>
66 Cache<TagStore>::Cache(const Params *p, TagStore *tags, BasePrefetcher *pf)
71 prefetchOnAccess(p->prefetch_on_access)
73 tempBlock = new BlkType();
74 tempBlock->data = new uint8_t[blkSize];
76 cpuSidePort = new CpuSidePort(p->name + "-cpu_side_port", this,
78 memSidePort = new MemSidePort(p->name + "-mem_side_port", this,
80 cpuSidePort->setOtherPort(memSidePort);
81 memSidePort->setOtherPort(cpuSidePort);
85 prefetcher->setCache(this);
88 template<class TagStore>
90 Cache<TagStore>::regStats()
92 BaseCache::regStats();
93 tags->regStats(name());
95 prefetcher->regStats(name());
98 template<class TagStore>
100 Cache<TagStore>::getPort(const std::string &if_name, int idx)
102 if (if_name == "" || if_name == "cpu_side") {
104 } else if (if_name == "mem_side") {
106 } else if (if_name == "functional") {
107 CpuSidePort *funcPort =
108 new CpuSidePort(name() + "-cpu_side_funcport", this,
110 funcPort->setOtherPort(memSidePort);
113 panic("Port name %s unrecognized\n", if_name);
117 template<class TagStore>
119 Cache<TagStore>::deletePortRefs(Port *p)
121 if (cpuSidePort == p || memSidePort == p)
122 panic("Can only delete functional ports\n");
128 template<class TagStore>
130 Cache<TagStore>::cmpAndSwap(BlkType *blk, PacketPtr pkt)
132 uint64_t overwrite_val;
134 uint64_t condition_val64;
135 uint32_t condition_val32;
137 int offset = tags->extractBlkOffset(pkt->getAddr());
138 uint8_t *blk_data = blk->data + offset;
140 assert(sizeof(uint64_t) >= pkt->getSize());
142 overwrite_mem = true;
143 // keep a copy of our possible write value, and copy what is at the
144 // memory address into the packet
145 pkt->writeData((uint8_t *)&overwrite_val);
146 pkt->setData(blk_data);
148 if (pkt->req->isCondSwap()) {
149 if (pkt->getSize() == sizeof(uint64_t)) {
150 condition_val64 = pkt->req->getExtraData();
151 overwrite_mem = !std::memcmp(&condition_val64, blk_data,
153 } else if (pkt->getSize() == sizeof(uint32_t)) {
154 condition_val32 = (uint32_t)pkt->req->getExtraData();
155 overwrite_mem = !std::memcmp(&condition_val32, blk_data,
158 panic("Invalid size for conditional read/write\n");
162 std::memcpy(blk_data, &overwrite_val, pkt->getSize());
163 blk->status |= BlkDirty;
168 template<class TagStore>
170 Cache<TagStore>::satisfyCpuSideRequest(PacketPtr pkt, BlkType *blk,
171 bool deferred_response,
172 bool pending_downgrade)
174 assert(blk && blk->isValid());
175 // Occasionally this is not true... if we are a lower-level cache
176 // satisfying a string of Read and ReadEx requests from
177 // upper-level caches, a Read will mark the block as shared but we
178 // can satisfy a following ReadEx anyway since we can rely on the
179 // Read requester(s) to have buffered the ReadEx snoop and to
180 // invalidate their blocks after receiving them.
181 // assert(!pkt->needsExclusive() || blk->isWritable());
182 assert(pkt->getOffset(blkSize) + pkt->getSize() <= blkSize);
184 // Check RMW operations first since both isRead() and
185 // isWrite() will be true for them
186 if (pkt->cmd == MemCmd::SwapReq) {
187 cmpAndSwap(blk, pkt);
188 } else if (pkt->isWrite()) {
189 if (blk->checkWrite(pkt)) {
190 pkt->writeDataToBlock(blk->data, blkSize);
191 blk->status |= BlkDirty;
193 } else if (pkt->isRead()) {
195 blk->trackLoadLocked(pkt);
197 pkt->setDataFromBlock(blk->data, blkSize);
198 if (pkt->getSize() == blkSize) {
199 // special handling for coherent block requests from
200 // upper-level caches
201 if (pkt->needsExclusive()) {
202 // if we have a dirty copy, make sure the recipient
203 // keeps it marked dirty
204 if (blk->isDirty()) {
205 pkt->assertMemInhibit();
207 // on ReadExReq we give up our copy unconditionally
208 tags->invalidateBlk(blk);
209 } else if (blk->isWritable() && !pending_downgrade
210 && !pkt->sharedAsserted()) {
211 // we can give the requester an exclusive copy (by not
212 // asserting shared line) on a read request if:
213 // - we have an exclusive copy at this level (& below)
214 // - we don't have a pending snoop from below
215 // signaling another read request
216 // - no other cache above has a copy (otherwise it
217 // would have asseretd shared line on request)
219 if (blk->isDirty()) {
220 // special considerations if we're owner:
221 if (!deferred_response && !isTopLevel) {
222 // if we are responding immediately and can
223 // signal that we're transferring ownership
224 // along with exclusivity, do so
225 pkt->assertMemInhibit();
226 blk->status &= ~BlkDirty;
228 // if we're responding after our own miss,
229 // there's a window where the recipient didn't
230 // know it was getting ownership and may not
231 // have responded to snoops correctly, so we
232 // can't pass off ownership *or* exclusivity
237 // otherwise only respond with a shared copy
242 // Not a read or write... must be an upgrade. it's OK
243 // to just ack those as long as we have an exclusive
244 // copy at this level.
245 assert(pkt->isUpgrade());
246 tags->invalidateBlk(blk);
251 /////////////////////////////////////////////////////
253 // MSHR helper functions
255 /////////////////////////////////////////////////////
258 template<class TagStore>
260 Cache<TagStore>::markInService(MSHR *mshr, PacketPtr pkt)
262 markInServiceInternal(mshr, pkt);
264 if (mshr->originalCmd == MemCmd::HardPFReq) {
265 DPRINTF(HWPrefetch, "%s:Marking a HW_PF in service\n",
267 //Also clear pending if need be
268 if (!prefetcher->havePending())
270 deassertMemSideBusRequest(Request_PF);
277 template<class TagStore>
279 Cache<TagStore>::squash(int threadNum)
281 bool unblock = false;
282 BlockedCause cause = NUM_BLOCKED_CAUSES;
284 if (noTargetMSHR && noTargetMSHR->threadNum == threadNum) {
287 cause = Blocked_NoTargets;
289 if (mshrQueue.isFull()) {
291 cause = Blocked_NoMSHRs;
293 mshrQueue.squash(threadNum);
294 if (unblock && !mshrQueue.isFull()) {
299 /////////////////////////////////////////////////////
301 // Access path: requests coming in from the CPU side
303 /////////////////////////////////////////////////////
305 template<class TagStore>
307 Cache<TagStore>::access(PacketPtr pkt, BlkType *&blk,
308 int &lat, PacketList &writebacks)
310 if (pkt->req->isUncacheable()) {
311 if (pkt->req->isClearLL()) {
314 blk = tags->findBlock(pkt->getAddr());
316 tags->invalidateBlk(blk);
325 int id = pkt->req->hasContextId() ? pkt->req->contextId() : -1;
326 blk = tags->accessBlock(pkt->getAddr(), lat, id);
328 DPRINTF(Cache, "%s%s %x %s\n", pkt->cmdString(),
329 pkt->req->isInstFetch() ? " (ifetch)" : "",
330 pkt->getAddr(), (blk) ? "hit" : "miss");
334 if (pkt->needsExclusive() ? blk->isWritable() : blk->isReadable()) {
335 // OK to satisfy access
336 incHitCount(pkt, id);
337 satisfyCpuSideRequest(pkt, blk);
342 // Can't satisfy access normally... either no block (blk == NULL)
343 // or have block but need exclusive & only have shared.
345 // Writeback handling is special case. We can write the block
346 // into the cache without having a writeable copy (or any copy at
348 if (pkt->cmd == MemCmd::Writeback) {
349 assert(blkSize == pkt->getSize());
351 // need to do a replacement
352 blk = allocateBlock(pkt->getAddr(), writebacks);
354 // no replaceable block available, give up.
355 // writeback will be forwarded to next level.
356 incMissCount(pkt, id);
359 int id = pkt->req->hasContextId() ? pkt->req->contextId() : -1;
360 tags->insertBlock(pkt->getAddr(), blk, id);
361 blk->status = BlkValid | BlkReadable;
363 std::memcpy(blk->data, pkt->getPtr<uint8_t>(), blkSize);
364 blk->status |= BlkDirty;
365 if (pkt->isSupplyExclusive()) {
366 blk->status |= BlkWritable;
368 // nothing else to do; writeback doesn't expect response
369 assert(!pkt->needsResponse());
370 incHitCount(pkt, id);
374 incMissCount(pkt, id);
376 if (blk == NULL && pkt->isLLSC() && pkt->isWrite()) {
377 // complete miss on store conditional... just give up now
378 pkt->req->setExtraData(0);
386 class ForwardResponseRecord : public Packet::SenderState, public FastAlloc
388 Packet::SenderState *prevSenderState;
394 ForwardResponseRecord(Packet *pkt, BaseCache *_cache)
395 : prevSenderState(pkt->senderState), prevSrc(pkt->getSrc())
400 void restore(Packet *pkt, BaseCache *_cache)
402 assert(_cache == cache);
403 pkt->senderState = prevSenderState;
404 pkt->setDest(prevSrc);
409 template<class TagStore>
411 Cache<TagStore>::timingAccess(PacketPtr pkt)
413 //@todo Add back in MemDebug Calls
414 // MemDebug::cacheAccess(pkt);
416 // we charge hitLatency for doing just about anything here
417 Tick time = curTick() + hitLatency;
419 if (pkt->isResponse()) {
420 // must be cache-to-cache response from upper to lower level
421 ForwardResponseRecord *rec =
422 dynamic_cast<ForwardResponseRecord *>(pkt->senderState);
425 assert(pkt->cmd == MemCmd::HardPFResp);
426 // Check if it's a prefetch response and handle it. We shouldn't
427 // get any other kinds of responses without FRRs.
428 DPRINTF(Cache, "Got prefetch response from above for addr %#x\n",
434 rec->restore(pkt, this);
436 memSidePort->respond(pkt, time);
440 assert(pkt->isRequest());
442 if (pkt->memInhibitAsserted()) {
443 DPRINTF(Cache, "mem inhibited on 0x%x: not responding\n",
445 assert(!pkt->req->isUncacheable());
446 // Special tweak for multilevel coherence: snoop downward here
447 // on invalidates since there may be other caches below here
448 // that have shared copies. Not necessary if we know that
449 // supplier had exclusive copy to begin with.
450 if (pkt->needsExclusive() && !pkt->isSupplyExclusive()) {
451 Packet *snoopPkt = new Packet(pkt, true); // clear flags
452 snoopPkt->setExpressSnoop();
453 snoopPkt->assertMemInhibit();
454 memSidePort->sendTiming(snoopPkt);
455 // main memory will delete snoopPkt
457 // since we're the official target but we aren't responding,
458 // delete the packet now.
463 if (pkt->req->isUncacheable()) {
464 if (pkt->req->isClearLL()) {
467 BlkType *blk = tags->findBlock(pkt->getAddr());
469 tags->invalidateBlk(blk);
473 // writes go in write buffer, reads use MSHR
474 if (pkt->isWrite() && !pkt->isRead()) {
475 allocateWriteBuffer(pkt, time, true);
477 allocateUncachedReadBuffer(pkt, time, true);
479 assert(pkt->needsResponse()); // else we should delete it here??
483 int lat = hitLatency;
485 PacketList writebacks;
487 bool satisfied = access(pkt, blk, lat, writebacks);
490 /** @todo make the fast write alloc (wh64) work with coherence. */
492 // If this is a block size write/hint (WH64) allocate the block here
493 // if the coherence protocol allows it.
494 if (!blk && pkt->getSize() >= blkSize && coherence->allowFastWrites() &&
495 (pkt->cmd == MemCmd::WriteReq
496 || pkt->cmd == MemCmd::WriteInvalidateReq) ) {
497 // not outstanding misses, can do this
498 MSHR *outstanding_miss = mshrQueue.findMatch(pkt->getAddr());
499 if (pkt->cmd == MemCmd::WriteInvalidateReq || !outstanding_miss) {
500 if (outstanding_miss) {
501 warn("WriteInv doing a fastallocate"
502 "with an outstanding miss to the same address\n");
504 blk = handleFill(NULL, pkt, BlkValid | BlkWritable,
511 // track time of availability of next prefetch, if any
512 Tick next_pf_time = 0;
514 bool needsResponse = pkt->needsResponse();
517 if (prefetcher && (prefetchOnAccess || (blk && blk->wasPrefetched()))) {
519 blk->status &= ~BlkHWPrefetched;
520 next_pf_time = prefetcher->notify(pkt, time);
524 pkt->makeTimingResponse();
525 cpuSidePort->respond(pkt, curTick()+lat);
532 Addr blk_addr = blockAlign(pkt->getAddr());
533 MSHR *mshr = mshrQueue.findMatch(blk_addr);
537 //@todo remove hw_pf here
538 mshr_hits[pkt->cmdToIndex()][0/*pkt->req->threadId()*/]++;
539 if (mshr->threadNum != 0/*pkt->req->threadId()*/) {
540 mshr->threadNum = -1;
542 mshr->allocateTarget(pkt, time, order++);
543 if (mshr->getNumTargets() == numTarget) {
545 setBlocked(Blocked_NoTargets);
546 // need to be careful with this... if this mshr isn't
547 // ready yet (i.e. time > curTick()_, we don't want to
548 // move it ahead of mshrs that are ready
549 // mshrQueue.moveToFront(mshr);
553 mshr_misses[pkt->cmdToIndex()][0/*pkt->req->threadId()*/]++;
554 // always mark as cache fill for now... if we implement
555 // no-write-allocate or bypass accesses this will have to
557 if (pkt->cmd == MemCmd::Writeback) {
558 allocateWriteBuffer(pkt, time, true);
560 if (blk && blk->isValid()) {
561 // If we have a write miss to a valid block, we
562 // need to mark the block non-readable. Otherwise
563 // if we allow reads while there's an outstanding
564 // write miss, the read could return stale data
565 // out of the cache block... a more aggressive
566 // system could detect the overlap (if any) and
567 // forward data out of the MSHRs, but we don't do
568 // that yet. Note that we do need to leave the
569 // block valid so that it stays in the cache, in
570 // case we get an upgrade response (and hence no
571 // new data) when the write miss completes.
572 // As long as CPUs do proper store/load forwarding
573 // internally, and have a sufficiently weak memory
574 // model, this is probably unnecessary, but at some
575 // point it must have seemed like we needed it...
576 assert(pkt->needsExclusive() && !blk->isWritable());
577 blk->status &= ~BlkReadable;
580 allocateMissBuffer(pkt, time, true);
584 next_pf_time = prefetcher->notify(pkt, time);
589 if (next_pf_time != 0)
590 requestMemSideBus(Request_PF, std::max(time, next_pf_time));
592 // copy writebacks to write buffer
593 while (!writebacks.empty()) {
594 PacketPtr wbPkt = writebacks.front();
595 allocateWriteBuffer(wbPkt, time, true);
596 writebacks.pop_front();
603 // See comment in cache.hh.
604 template<class TagStore>
606 Cache<TagStore>::getBusPacket(PacketPtr cpu_pkt, BlkType *blk,
609 bool blkValid = blk && blk->isValid();
611 if (cpu_pkt->req->isUncacheable()) {
612 //assert(blk == NULL);
617 (cpu_pkt->cmd == MemCmd::Writeback || cpu_pkt->isUpgrade())) {
618 // Writebacks that weren't allocated in access() and upgrades
619 // from upper-level caches that missed completely just go
624 assert(cpu_pkt->needsResponse());
627 // @TODO make useUpgrades a parameter.
628 // Note that ownership protocols require upgrade, otherwise a
629 // write miss on a shared owned block will generate a ReadExcl,
630 // which will clobber the owned copy.
631 const bool useUpgrades = true;
632 if (blkValid && useUpgrades) {
633 // only reason to be here is that blk is shared
634 // (read-only) and we need exclusive
635 assert(needsExclusive && !blk->isWritable());
636 cmd = cpu_pkt->isLLSC() ? MemCmd::SCUpgradeReq : MemCmd::UpgradeReq;
639 cmd = needsExclusive ? MemCmd::ReadExReq : MemCmd::ReadReq;
641 PacketPtr pkt = new Packet(cpu_pkt->req, cmd, Packet::Broadcast, blkSize);
648 template<class TagStore>
650 Cache<TagStore>::atomicAccess(PacketPtr pkt)
652 int lat = hitLatency;
654 // @TODO: make this a parameter
655 bool last_level_cache = false;
657 if (pkt->memInhibitAsserted()) {
658 assert(!pkt->req->isUncacheable());
659 // have to invalidate ourselves and any lower caches even if
660 // upper cache will be responding
661 if (pkt->isInvalidate()) {
662 BlkType *blk = tags->findBlock(pkt->getAddr());
663 if (blk && blk->isValid()) {
664 tags->invalidateBlk(blk);
665 DPRINTF(Cache, "rcvd mem-inhibited %s on 0x%x: invalidating\n",
666 pkt->cmdString(), pkt->getAddr());
668 if (!last_level_cache) {
669 DPRINTF(Cache, "forwarding mem-inhibited %s on 0x%x\n",
670 pkt->cmdString(), pkt->getAddr());
671 lat += memSidePort->sendAtomic(pkt);
674 DPRINTF(Cache, "rcvd mem-inhibited %s on 0x%x: not responding\n",
675 pkt->cmdString(), pkt->getAddr());
681 // should assert here that there are no outstanding MSHRs or
682 // writebacks... that would mean that someone used an atomic
683 // access in timing mode
686 PacketList writebacks;
688 if (!access(pkt, blk, lat, writebacks)) {
690 PacketPtr bus_pkt = getBusPacket(pkt, blk, pkt->needsExclusive());
692 bool is_forward = (bus_pkt == NULL);
695 // just forwarding the same request to the next level
696 // no local cache operation involved
700 DPRINTF(Cache, "Sending an atomic %s for %x\n",
701 bus_pkt->cmdString(), bus_pkt->getAddr());
704 CacheBlk::State old_state = blk ? blk->status : 0;
707 lat += memSidePort->sendAtomic(bus_pkt);
709 DPRINTF(Cache, "Receive response: %s for addr %x in state %i\n",
710 bus_pkt->cmdString(), bus_pkt->getAddr(), old_state);
712 assert(!bus_pkt->wasNacked());
714 // If packet was a forward, the response (if any) is already
715 // in place in the bus_pkt == pkt structure, so we don't need
716 // to do anything. Otherwise, use the separate bus_pkt to
717 // generate response to pkt and then delete it.
719 if (pkt->needsResponse()) {
720 assert(bus_pkt->isResponse());
721 if (bus_pkt->isError()) {
722 pkt->makeAtomicResponse();
723 pkt->copyError(bus_pkt);
724 } else if (bus_pkt->isRead() ||
725 bus_pkt->cmd == MemCmd::UpgradeResp) {
726 // we're updating cache state to allow us to
727 // satisfy the upstream request from the cache
728 blk = handleFill(bus_pkt, blk, writebacks);
729 satisfyCpuSideRequest(pkt, blk);
731 // we're satisfying the upstream request without
732 // modifying cache state, e.g., a write-through
733 pkt->makeAtomicResponse();
740 // Note that we don't invoke the prefetcher at all in atomic mode.
741 // It's not clear how to do it properly, particularly for
742 // prefetchers that aggressively generate prefetch candidates and
743 // rely on bandwidth contention to throttle them; these will tend
744 // to pollute the cache in atomic mode since there is no bandwidth
745 // contention. If we ever do want to enable prefetching in atomic
746 // mode, though, this is the place to do it... see timingAccess()
747 // for an example (though we'd want to issue the prefetch(es)
748 // immediately rather than calling requestMemSideBus() as we do
751 // Handle writebacks if needed
752 while (!writebacks.empty()){
753 PacketPtr wbPkt = writebacks.front();
754 memSidePort->sendAtomic(wbPkt);
755 writebacks.pop_front();
759 // We now have the block one way or another (hit or completed miss)
761 if (pkt->needsResponse()) {
762 pkt->makeAtomicResponse();
769 template<class TagStore>
771 Cache<TagStore>::functionalAccess(PacketPtr pkt,
772 CachePort *incomingPort,
773 CachePort *otherSidePort)
775 Addr blk_addr = blockAlign(pkt->getAddr());
776 BlkType *blk = tags->findBlock(pkt->getAddr());
777 MSHR *mshr = mshrQueue.findMatch(blk_addr);
779 pkt->pushLabel(name());
781 CacheBlkPrintWrapper cbpw(blk);
783 // Note that just because an L2/L3 has valid data doesn't mean an
784 // L1 doesn't have a more up-to-date modified copy that still
785 // needs to be found. As a result we always update the request if
786 // we have it, but only declare it satisfied if we are the owner.
788 // see if we have data at all (owned or otherwise)
789 bool have_data = blk && blk->isValid()
790 && pkt->checkFunctional(&cbpw, blk_addr, blkSize, blk->data);
792 // data we have is dirty if marked as such or if valid & ownership
793 // pending due to outstanding UpgradeReq
795 have_data && (blk->isDirty() ||
796 (mshr && mshr->inService && mshr->isPendingDirty()));
798 bool done = have_dirty
799 || incomingPort->checkFunctional(pkt)
800 || mshrQueue.checkFunctional(pkt, blk_addr)
801 || writeBuffer.checkFunctional(pkt, blk_addr)
802 || otherSidePort->checkFunctional(pkt);
804 DPRINTF(Cache, "functional %s %x %s%s%s\n",
805 pkt->cmdString(), pkt->getAddr(),
806 (blk && blk->isValid()) ? "valid " : "",
807 have_data ? "data " : "", done ? "done " : "");
809 // We're leaving the cache, so pop cache->name() label
815 otherSidePort->sendFunctional(pkt);
820 /////////////////////////////////////////////////////
822 // Response handling: responses from the memory side
824 /////////////////////////////////////////////////////
827 template<class TagStore>
829 Cache<TagStore>::handleResponse(PacketPtr pkt)
831 Tick time = curTick() + hitLatency;
832 MSHR *mshr = dynamic_cast<MSHR*>(pkt->senderState);
833 bool is_error = pkt->isError();
837 if (pkt->wasNacked()) {
838 //pkt->reinitFromRequest();
839 warn("NACKs from devices not connected to the same bus "
840 "not implemented\n");
844 DPRINTF(Cache, "Cache received packet with error for address %x, "
845 "cmd: %s\n", pkt->getAddr(), pkt->cmdString());
848 DPRINTF(Cache, "Handling response to %x\n", pkt->getAddr());
850 MSHRQueue *mq = mshr->queue;
851 bool wasFull = mq->isFull();
853 if (mshr == noTargetMSHR) {
854 // we always clear at least one target
855 clearBlocked(Blocked_NoTargets);
859 // Initial target is used just for stats
860 MSHR::Target *initial_tgt = mshr->getTarget();
861 BlkType *blk = tags->findBlock(pkt->getAddr());
862 int stats_cmd_idx = initial_tgt->pkt->cmdToIndex();
863 Tick miss_latency = curTick() - initial_tgt->recvTime;
864 PacketList writebacks;
866 if (pkt->req->isUncacheable()) {
867 mshr_uncacheable_lat[stats_cmd_idx][0/*pkt->req->threadId()*/] +=
870 mshr_miss_latency[stats_cmd_idx][0/*pkt->req->threadId()*/] +=
874 bool is_fill = !mshr->isForward &&
875 (pkt->isRead() || pkt->cmd == MemCmd::UpgradeResp);
877 if (is_fill && !is_error) {
878 DPRINTF(Cache, "Block for addr %x being updated in Cache\n",
881 // give mshr a chance to do some dirty work
882 mshr->handleFill(pkt, blk);
884 blk = handleFill(pkt, blk, writebacks);
888 // First offset for critical word first calculations
889 int initial_offset = 0;
891 if (mshr->hasTargets()) {
892 initial_offset = mshr->getTarget()->pkt->getOffset(blkSize);
895 while (mshr->hasTargets()) {
896 MSHR::Target *target = mshr->getTarget();
898 switch (target->source) {
899 case MSHR::Target::FromCPU:
900 Tick completion_time;
902 satisfyCpuSideRequest(target->pkt, blk,
903 true, mshr->hasPostDowngrade());
904 // How many bytes past the first request is this one
905 int transfer_offset =
906 target->pkt->getOffset(blkSize) - initial_offset;
907 if (transfer_offset < 0) {
908 transfer_offset += blkSize;
911 // If critical word (no offset) return first word time
912 completion_time = tags->getHitLatency() +
913 (transfer_offset ? pkt->finishTime : pkt->firstWordTime);
915 assert(!target->pkt->req->isUncacheable());
916 missLatency[target->pkt->cmdToIndex()][0/*pkt->req->threadId()*/] +=
917 completion_time - target->recvTime;
918 } else if (pkt->cmd == MemCmd::UpgradeFailResp) {
919 // failed StoreCond upgrade
920 assert(target->pkt->cmd == MemCmd::StoreCondReq ||
921 target->pkt->cmd == MemCmd::StoreCondFailReq ||
922 target->pkt->cmd == MemCmd::SCUpgradeFailReq);
923 completion_time = tags->getHitLatency() + pkt->finishTime;
924 target->pkt->req->setExtraData(0);
926 // not a cache fill, just forwarding response
927 completion_time = tags->getHitLatency() + pkt->finishTime;
928 if (pkt->isRead() && !is_error) {
929 target->pkt->setData(pkt->getPtr<uint8_t>());
932 target->pkt->makeTimingResponse();
933 // if this packet is an error copy that to the new packet
935 target->pkt->copyError(pkt);
936 if (target->pkt->cmd == MemCmd::ReadResp &&
937 (pkt->isInvalidate() || mshr->hasPostInvalidate())) {
938 // If intermediate cache got ReadRespWithInvalidate,
939 // propagate that. Response should not have
940 // isInvalidate() set otherwise.
941 target->pkt->cmd = MemCmd::ReadRespWithInvalidate;
943 cpuSidePort->respond(target->pkt, completion_time);
946 case MSHR::Target::FromPrefetcher:
947 assert(target->pkt->cmd == MemCmd::HardPFReq);
949 blk->status |= BlkHWPrefetched;
950 delete target->pkt->req;
954 case MSHR::Target::FromSnoop:
955 // I don't believe that a snoop can be in an error state
957 // response to snoop request
958 DPRINTF(Cache, "processing deferred snoop...\n");
959 assert(!(pkt->isInvalidate() && !mshr->hasPostInvalidate()));
960 handleSnoop(target->pkt, blk, true, true,
961 mshr->hasPostInvalidate());
965 panic("Illegal target->source enum %d\n", target->source);
972 if (pkt->isInvalidate() || mshr->hasPostInvalidate()) {
973 tags->invalidateBlk(blk);
974 } else if (mshr->hasPostDowngrade()) {
975 blk->status &= ~BlkWritable;
979 if (mshr->promoteDeferredTargets()) {
980 // avoid later read getting stale data while write miss is
981 // outstanding.. see comment in timingAccess()
983 blk->status &= ~BlkReadable;
985 MSHRQueue *mq = mshr->queue;
986 mq->markPending(mshr);
987 requestMemSideBus((RequestCause)mq->index, pkt->finishTime);
989 mq->deallocate(mshr);
990 if (wasFull && !mq->isFull()) {
991 clearBlocked((BlockedCause)mq->index);
995 // copy writebacks to write buffer
996 while (!writebacks.empty()) {
997 PacketPtr wbPkt = writebacks.front();
998 allocateWriteBuffer(wbPkt, time, true);
999 writebacks.pop_front();
1001 // if we used temp block, clear it out
1002 if (blk == tempBlock) {
1003 if (blk->isDirty()) {
1004 allocateWriteBuffer(writebackBlk(blk), time, true);
1006 tags->invalidateBlk(blk);
1015 template<class TagStore>
1017 Cache<TagStore>::writebackBlk(BlkType *blk)
1019 assert(blk && blk->isValid() && blk->isDirty());
1021 writebacks[0/*pkt->req->threadId()*/]++;
1023 Request *writebackReq =
1024 new Request(tags->regenerateBlkAddr(blk->tag, blk->set), blkSize, 0);
1025 PacketPtr writeback = new Packet(writebackReq, MemCmd::Writeback, -1);
1026 if (blk->isWritable()) {
1027 writeback->setSupplyExclusive();
1029 writeback->allocate();
1030 std::memcpy(writeback->getPtr<uint8_t>(), blk->data, blkSize);
1032 blk->status &= ~BlkDirty;
1037 template<class TagStore>
1038 typename Cache<TagStore>::BlkType*
1039 Cache<TagStore>::allocateBlock(Addr addr, PacketList &writebacks)
1041 BlkType *blk = tags->findVictim(addr, writebacks);
1043 if (blk->isValid()) {
1044 Addr repl_addr = tags->regenerateBlkAddr(blk->tag, blk->set);
1045 MSHR *repl_mshr = mshrQueue.findMatch(repl_addr);
1047 // must be an outstanding upgrade request on block
1048 // we're about to replace...
1049 assert(!blk->isWritable());
1050 assert(repl_mshr->needsExclusive());
1051 // too hard to replace block with transient state
1052 // allocation failed, block not inserted
1055 DPRINTF(Cache, "replacement: replacing %x with %x: %s\n",
1057 blk->isDirty() ? "writeback" : "clean");
1059 if (blk->isDirty()) {
1060 // Save writeback packet for handling by caller
1061 writebacks.push_back(writebackBlk(blk));
1070 // Note that the reason we return a list of writebacks rather than
1071 // inserting them directly in the write buffer is that this function
1072 // is called by both atomic and timing-mode accesses, and in atomic
1073 // mode we don't mess with the write buffer (we just perform the
1074 // writebacks atomically once the original request is complete).
1075 template<class TagStore>
1076 typename Cache<TagStore>::BlkType*
1077 Cache<TagStore>::handleFill(PacketPtr pkt, BlkType *blk,
1078 PacketList &writebacks)
1080 Addr addr = pkt->getAddr();
1082 CacheBlk::State old_state = blk ? blk->status : 0;
1086 // better have read new data...
1087 assert(pkt->hasData());
1088 // need to do a replacement
1089 blk = allocateBlock(addr, writebacks);
1091 // No replaceable block... just use temporary storage to
1092 // complete the current request and then get rid of it
1093 assert(!tempBlock->isValid());
1095 tempBlock->set = tags->extractSet(addr);
1096 tempBlock->tag = tags->extractTag(addr);
1097 DPRINTF(Cache, "using temp block for %x\n", addr);
1099 int id = pkt->req->hasContextId() ? pkt->req->contextId() : -1;
1100 tags->insertBlock(pkt->getAddr(), blk, id);
1103 // starting from scratch with a new block
1106 // existing block... probably an upgrade
1107 assert(blk->tag == tags->extractTag(addr));
1108 // either we're getting new data or the block should already be valid
1109 assert(pkt->hasData() || blk->isValid());
1110 // don't clear block status... if block is already dirty we
1111 // don't want to lose that
1114 blk->status |= BlkValid | BlkReadable;
1116 if (!pkt->sharedAsserted()) {
1117 blk->status |= BlkWritable;
1118 // If we got this via cache-to-cache transfer (i.e., from a
1119 // cache that was an owner) and took away that owner's copy,
1120 // then we need to write it back. Normally this happens
1121 // anyway as a side effect of getting a copy to write it, but
1122 // there are cases (such as failed store conditionals or
1123 // compare-and-swaps) where we'll demand an exclusive copy but
1124 // end up not writing it.
1125 if (pkt->memInhibitAsserted())
1126 blk->status |= BlkDirty;
1129 DPRINTF(Cache, "Block addr %x moving from state %i to %i\n",
1130 addr, old_state, blk->status);
1132 // if we got new data, copy it in
1133 if (pkt->isRead()) {
1134 std::memcpy(blk->data, pkt->getPtr<uint8_t>(), blkSize);
1137 blk->whenReady = pkt->finishTime;
1143 /////////////////////////////////////////////////////
1145 // Snoop path: requests coming in from the memory side
1147 /////////////////////////////////////////////////////
1149 template<class TagStore>
1152 doTimingSupplyResponse(PacketPtr req_pkt, uint8_t *blk_data,
1153 bool already_copied, bool pending_inval)
1155 // timing-mode snoop responses require a new packet, unless we
1156 // already made a copy...
1157 PacketPtr pkt = already_copied ? req_pkt : new Packet(req_pkt);
1158 assert(req_pkt->isInvalidate() || pkt->sharedAsserted());
1160 pkt->makeTimingResponse();
1161 if (pkt->isRead()) {
1162 pkt->setDataFromBlock(blk_data, blkSize);
1164 if (pkt->cmd == MemCmd::ReadResp && pending_inval) {
1165 // Assume we defer a response to a read from a far-away cache
1166 // A, then later defer a ReadExcl from a cache B on the same
1167 // bus as us. We'll assert MemInhibit in both cases, but in
1168 // the latter case MemInhibit will keep the invalidation from
1169 // reaching cache A. This special response tells cache A that
1170 // it gets the block to satisfy its read, but must immediately
1172 pkt->cmd = MemCmd::ReadRespWithInvalidate;
1174 memSidePort->respond(pkt, curTick() + hitLatency);
1177 template<class TagStore>
1179 Cache<TagStore>::handleSnoop(PacketPtr pkt, BlkType *blk,
1180 bool is_timing, bool is_deferred,
1183 // deferred snoops can only happen in timing mode
1184 assert(!(is_deferred && !is_timing));
1185 // pending_inval only makes sense on deferred snoops
1186 assert(!(pending_inval && !is_deferred));
1187 assert(pkt->isRequest());
1189 // the packet may get modified if we or a forwarded snooper
1190 // responds in atomic mode, so remember a few things about the
1191 // original packet up front
1192 bool invalidate = pkt->isInvalidate();
1193 bool M5_VAR_USED needs_exclusive = pkt->needsExclusive();
1195 if (forwardSnoops) {
1196 // first propagate snoop upward to see if anyone above us wants to
1197 // handle it. save & restore packet src since it will get
1198 // rewritten to be relative to cpu-side bus (if any)
1199 bool alreadyResponded = pkt->memInhibitAsserted();
1201 Packet *snoopPkt = new Packet(pkt, true); // clear flags
1202 snoopPkt->setExpressSnoop();
1203 snoopPkt->senderState = new ForwardResponseRecord(pkt, this);
1204 cpuSidePort->sendTiming(snoopPkt);
1205 if (snoopPkt->memInhibitAsserted()) {
1206 // cache-to-cache response from some upper cache
1207 assert(!alreadyResponded);
1208 pkt->assertMemInhibit();
1210 delete snoopPkt->senderState;
1212 if (snoopPkt->sharedAsserted()) {
1213 pkt->assertShared();
1217 int origSrc = pkt->getSrc();
1218 cpuSidePort->sendAtomic(pkt);
1219 if (!alreadyResponded && pkt->memInhibitAsserted()) {
1220 // cache-to-cache response from some upper cache:
1221 // forward response to original requester
1222 assert(pkt->isResponse());
1224 pkt->setSrc(origSrc);
1228 if (!blk || !blk->isValid()) {
1232 // we may end up modifying both the block state and the packet (if
1233 // we respond in atomic mode), so just figure out what to do now
1234 // and then do it later
1235 bool respond = blk->isDirty() && pkt->needsResponse();
1236 bool have_exclusive = blk->isWritable();
1238 if (pkt->isRead() && !invalidate) {
1239 assert(!needs_exclusive);
1240 pkt->assertShared();
1241 int bits_to_clear = BlkWritable;
1242 const bool haveOwnershipState = true; // for now
1243 if (!haveOwnershipState) {
1244 // if we don't support pure ownership (dirty && !writable),
1245 // have to clear dirty bit here, assume memory snarfs data
1246 // on cache-to-cache xfer
1247 bits_to_clear |= BlkDirty;
1249 blk->status &= ~bits_to_clear;
1252 DPRINTF(Cache, "snooped a %s request for addr %x, %snew state is %i\n",
1253 pkt->cmdString(), blockAlign(pkt->getAddr()),
1254 respond ? "responding, " : "", invalidate ? 0 : blk->status);
1257 assert(!pkt->memInhibitAsserted());
1258 pkt->assertMemInhibit();
1259 if (have_exclusive) {
1260 pkt->setSupplyExclusive();
1263 doTimingSupplyResponse(pkt, blk->data, is_deferred, pending_inval);
1265 pkt->makeAtomicResponse();
1266 pkt->setDataFromBlock(blk->data, blkSize);
1268 } else if (is_timing && is_deferred) {
1269 // if it's a deferred timing snoop then we've made a copy of
1270 // the packet, and so if we're not using that copy to respond
1271 // then we need to delete it here.
1275 // Do this last in case it deallocates block data or something
1278 tags->invalidateBlk(blk);
1283 template<class TagStore>
1285 Cache<TagStore>::snoopTiming(PacketPtr pkt)
1287 // Note that some deferred snoops don't have requests, since the
1288 // original access may have already completed
1289 if ((pkt->req && pkt->req->isUncacheable()) ||
1290 pkt->cmd == MemCmd::Writeback) {
1291 //Can't get a hit on an uncacheable address
1292 //Revisit this for multi level coherence
1296 BlkType *blk = tags->findBlock(pkt->getAddr());
1298 Addr blk_addr = blockAlign(pkt->getAddr());
1299 MSHR *mshr = mshrQueue.findMatch(blk_addr);
1301 // Let the MSHR itself track the snoop and decide whether we want
1302 // to go ahead and do the regular cache snoop
1303 if (mshr && mshr->handleSnoop(pkt, order++)) {
1304 DPRINTF(Cache, "Deferring snoop on in-service MSHR to blk %x\n",
1306 if (mshr->getNumTargets() > numTarget)
1307 warn("allocating bonus target for snoop"); //handle later
1311 //We also need to check the writeback buffers and handle those
1312 std::vector<MSHR *> writebacks;
1313 if (writeBuffer.findMatches(blk_addr, writebacks)) {
1314 DPRINTF(Cache, "Snoop hit in writeback to addr: %x\n",
1317 //Look through writebacks for any non-uncachable writes, use that
1318 for (int i = 0; i < writebacks.size(); i++) {
1319 mshr = writebacks[i];
1320 assert(!mshr->isUncacheable());
1321 assert(mshr->getNumTargets() == 1);
1322 PacketPtr wb_pkt = mshr->getTarget()->pkt;
1323 assert(wb_pkt->cmd == MemCmd::Writeback);
1325 assert(!pkt->memInhibitAsserted());
1326 pkt->assertMemInhibit();
1327 if (!pkt->needsExclusive()) {
1328 pkt->assertShared();
1329 // the writeback is no longer the exclusive copy in the system
1330 wb_pkt->clearSupplyExclusive();
1332 // if we're not asserting the shared line, we need to
1333 // invalidate our copy. we'll do that below as long as
1334 // the packet's invalidate flag is set...
1335 assert(pkt->isInvalidate());
1337 doTimingSupplyResponse(pkt, wb_pkt->getPtr<uint8_t>(),
1340 if (pkt->isInvalidate()) {
1341 // Invalidation trumps our writeback... discard here
1342 markInService(mshr);
1346 // If this was a shared writeback, there may still be
1347 // other shared copies above that require invalidation.
1348 // We could be more selective and return here if the
1349 // request is non-exclusive or if the writeback is
1355 handleSnoop(pkt, blk, true, false, false);
1359 template<class TagStore>
1361 Cache<TagStore>::snoopAtomic(PacketPtr pkt)
1363 if (pkt->req->isUncacheable() || pkt->cmd == MemCmd::Writeback) {
1364 // Can't get a hit on an uncacheable address
1365 // Revisit this for multi level coherence
1369 BlkType *blk = tags->findBlock(pkt->getAddr());
1370 handleSnoop(pkt, blk, false, false, false);
1375 template<class TagStore>
1377 Cache<TagStore>::getNextMSHR()
1379 // Check both MSHR queue and write buffer for potential requests
1380 MSHR *miss_mshr = mshrQueue.getNextMSHR();
1381 MSHR *write_mshr = writeBuffer.getNextMSHR();
1383 // Now figure out which one to send... some cases are easy
1384 if (miss_mshr && !write_mshr) {
1387 if (write_mshr && !miss_mshr) {
1391 if (miss_mshr && write_mshr) {
1392 // We have one of each... normally we favor the miss request
1393 // unless the write buffer is full
1394 if (writeBuffer.isFull() && writeBuffer.inServiceEntries == 0) {
1395 // Write buffer is full, so we'd like to issue a write;
1396 // need to search MSHR queue for conflicting earlier miss.
1397 MSHR *conflict_mshr =
1398 mshrQueue.findPending(write_mshr->addr, write_mshr->size);
1400 if (conflict_mshr && conflict_mshr->order < write_mshr->order) {
1401 // Service misses in order until conflict is cleared.
1402 return conflict_mshr;
1405 // No conflicts; issue write
1409 // Write buffer isn't full, but need to check it for
1410 // conflicting earlier writeback
1411 MSHR *conflict_mshr =
1412 writeBuffer.findPending(miss_mshr->addr, miss_mshr->size);
1413 if (conflict_mshr) {
1414 // not sure why we don't check order here... it was in the
1415 // original code but commented out.
1417 // The only way this happens is if we are
1418 // doing a write and we didn't have permissions
1419 // then subsequently saw a writeback (owned got evicted)
1420 // We need to make sure to perform the writeback first
1421 // To preserve the dirty data, then we can issue the write
1423 // should we return write_mshr here instead? I.e. do we
1424 // have to flush writes in order? I don't think so... not
1425 // for Alpha anyway. Maybe for x86?
1426 return conflict_mshr;
1429 // No conflicts; issue read
1433 // fall through... no pending requests. Try a prefetch.
1434 assert(!miss_mshr && !write_mshr);
1435 if (prefetcher && !mshrQueue.isFull()) {
1436 // If we have a miss queue slot, we can try a prefetch
1437 PacketPtr pkt = prefetcher->getPacket();
1439 Addr pf_addr = blockAlign(pkt->getAddr());
1440 if (!tags->findBlock(pf_addr) && !mshrQueue.findMatch(pf_addr) &&
1441 !writeBuffer.findMatch(pf_addr)) {
1442 // Update statistic on number of prefetches issued
1443 // (hwpf_mshr_misses)
1444 mshr_misses[pkt->cmdToIndex()][0/*pkt->req->threadId()*/]++;
1445 // Don't request bus, since we already have it
1446 return allocateMissBuffer(pkt, curTick(), false);
1448 // free the request and packet
1459 template<class TagStore>
1461 Cache<TagStore>::getTimingPacket()
1463 MSHR *mshr = getNextMSHR();
1469 // use request from 1st target
1470 PacketPtr tgt_pkt = mshr->getTarget()->pkt;
1471 PacketPtr pkt = NULL;
1473 if (tgt_pkt->cmd == MemCmd::SCUpgradeFailReq ||
1474 tgt_pkt->cmd == MemCmd::StoreCondFailReq) {
1475 // SCUpgradeReq or StoreCondReq saw invalidation while queued
1476 // in MSHR, so now that we are getting around to processing
1477 // it, just treat it as if we got a failure response
1478 pkt = new Packet(tgt_pkt);
1479 pkt->cmd = MemCmd::UpgradeFailResp;
1480 pkt->senderState = mshr;
1481 pkt->firstWordTime = pkt->finishTime = curTick();
1482 handleResponse(pkt);
1484 } else if (mshr->isForwardNoResponse()) {
1485 // no response expected, just forward packet as it is
1486 assert(tags->findBlock(mshr->addr) == NULL);
1489 BlkType *blk = tags->findBlock(mshr->addr);
1491 if (tgt_pkt->cmd == MemCmd::HardPFReq) {
1492 // It might be possible for a writeback to arrive between
1493 // the time the prefetch is placed in the MSHRs and when
1494 // it's selected to send... if so, this assert will catch
1495 // that, and then we'll have to figure out what to do.
1496 assert(blk == NULL);
1498 // We need to check the caches above us to verify that they don't have
1499 // a copy of this block in the dirty state at the moment. Without this
1500 // check we could get a stale copy from memory that might get used
1501 // in place of the dirty one.
1502 PacketPtr snoop_pkt = new Packet(tgt_pkt, true);
1503 snoop_pkt->setExpressSnoop();
1504 snoop_pkt->senderState = mshr;
1505 cpuSidePort->sendTiming(snoop_pkt);
1507 if (snoop_pkt->memInhibitAsserted()) {
1508 markInService(mshr, snoop_pkt);
1509 DPRINTF(Cache, "Upward snoop of prefetch for addr %#x hit\n",
1510 tgt_pkt->getAddr());
1517 pkt = getBusPacket(tgt_pkt, blk, mshr->needsExclusive());
1519 mshr->isForward = (pkt == NULL);
1521 if (mshr->isForward) {
1522 // not a cache block request, but a response is expected
1523 // make copy of current packet to forward, keep current
1524 // copy for response handling
1525 pkt = new Packet(tgt_pkt);
1527 if (pkt->isWrite()) {
1528 pkt->setData(tgt_pkt->getPtr<uint8_t>());
1533 assert(pkt != NULL);
1534 pkt->senderState = mshr;
1539 template<class TagStore>
1541 Cache<TagStore>::nextMSHRReadyTime()
1543 Tick nextReady = std::min(mshrQueue.nextMSHRReadyTime(),
1544 writeBuffer.nextMSHRReadyTime());
1547 nextReady = std::min(nextReady,
1548 prefetcher->nextPrefetchReadyTime());
1561 template<class TagStore>
1563 Cache<TagStore>::CpuSidePort::
1564 getDeviceAddressRanges(AddrRangeList &resp, bool &snoop)
1566 // CPU side port doesn't snoop; it's a target only. It can
1567 // potentially respond to any address.
1569 resp.push_back(myCache()->getAddrRange());
1573 template<class TagStore>
1575 Cache<TagStore>::CpuSidePort::recvTiming(PacketPtr pkt)
1577 // illegal to block responses... can lead to deadlock
1578 if (pkt->isRequest() && !pkt->memInhibitAsserted() && blocked) {
1579 DPRINTF(Cache,"Scheduling a retry while blocked\n");
1580 mustSendRetry = true;
1584 myCache()->timingAccess(pkt);
1589 template<class TagStore>
1591 Cache<TagStore>::CpuSidePort::recvAtomic(PacketPtr pkt)
1593 return myCache()->atomicAccess(pkt);
1597 template<class TagStore>
1599 Cache<TagStore>::CpuSidePort::recvFunctional(PacketPtr pkt)
1601 myCache()->functionalAccess(pkt, this, otherPort);
1605 template<class TagStore>
1607 CpuSidePort::CpuSidePort(const std::string &_name, Cache<TagStore> *_cache,
1608 const std::string &_label)
1609 : BaseCache::CachePort(_name, _cache, _label)
1619 template<class TagStore>
1621 Cache<TagStore>::MemSidePort::
1622 getDeviceAddressRanges(AddrRangeList &resp, bool &snoop)
1624 // Memory-side port always snoops, but never passes requests
1625 // through to targets on the cpu side (so we don't add anything to
1626 // the address range list).
1631 template<class TagStore>
1633 Cache<TagStore>::MemSidePort::recvTiming(PacketPtr pkt)
1635 // this needs to be fixed so that the cache updates the mshr and sends the
1636 // packet back out on the link, but it probably won't happen so until this
1637 // gets fixed, just panic when it does
1638 if (pkt->wasNacked())
1639 panic("Need to implement cache resending nacked packets!\n");
1641 if (pkt->isRequest() && blocked) {
1642 DPRINTF(Cache,"Scheduling a retry while blocked\n");
1643 mustSendRetry = true;
1647 if (pkt->isResponse()) {
1648 myCache()->handleResponse(pkt);
1650 myCache()->snoopTiming(pkt);
1656 template<class TagStore>
1658 Cache<TagStore>::MemSidePort::recvAtomic(PacketPtr pkt)
1660 // in atomic mode, responses go back to the sender via the
1661 // function return from sendAtomic(), not via a separate
1662 // sendAtomic() from the responder. Thus we should never see a
1663 // response packet in recvAtomic() (anywhere, not just here).
1664 assert(!pkt->isResponse());
1665 return myCache()->snoopAtomic(pkt);
1669 template<class TagStore>
1671 Cache<TagStore>::MemSidePort::recvFunctional(PacketPtr pkt)
1673 myCache()->functionalAccess(pkt, this, otherPort);
1678 template<class TagStore>
1680 Cache<TagStore>::MemSidePort::sendPacket()
1682 // if we have responses that are ready, they take precedence
1683 if (deferredPacketReady()) {
1684 bool success = sendTiming(transmitList.front().pkt);
1687 //send successful, remove packet
1688 transmitList.pop_front();
1691 waitingOnRetry = !success;
1693 // check for non-response packets (requests & writebacks)
1694 PacketPtr pkt = myCache()->getTimingPacket();
1696 // can happen if e.g. we attempt a writeback and fail, but
1697 // before the retry, the writeback is eliminated because
1698 // we snoop another cache's ReadEx.
1699 waitingOnRetry = false;
1701 MSHR *mshr = dynamic_cast<MSHR*>(pkt->senderState);
1703 bool success = sendTiming(pkt);
1705 waitingOnRetry = !success;
1706 if (waitingOnRetry) {
1707 DPRINTF(CachePort, "now waiting on a retry\n");
1708 if (!mshr->isForwardNoResponse()) {
1712 myCache()->markInService(mshr, pkt);
1718 // tried to send packet... if it was successful (no retry), see if
1719 // we need to rerequest bus or not
1720 if (!waitingOnRetry) {
1721 Tick nextReady = std::min(deferredPacketReadyTime(),
1722 myCache()->nextMSHRReadyTime());
1723 // @TODO: need to facotr in prefetch requests here somehow
1724 if (nextReady != MaxTick) {
1725 DPRINTF(CachePort, "more packets to send @ %d\n", nextReady);
1726 schedule(sendEvent, std::max(nextReady, curTick() + 1));
1728 // no more to send right now: if we're draining, we may be done
1729 if (drainEvent && !sendEvent->scheduled()) {
1730 drainEvent->process();
1737 template<class TagStore>
1739 Cache<TagStore>::MemSidePort::recvRetry()
1741 assert(waitingOnRetry);
1746 template<class TagStore>
1748 Cache<TagStore>::MemSidePort::processSendEvent()
1750 assert(!waitingOnRetry);
1755 template<class TagStore>
1757 MemSidePort::MemSidePort(const std::string &_name, Cache<TagStore> *_cache,
1758 const std::string &_label)
1759 : BaseCache::CachePort(_name, _cache, _label)
1761 // override default send event from SimpleTimingPort
1763 sendEvent = new SendEvent(this);