cache.cc revision 12730:6c2ea88bf129
1/*
2 * Copyright (c) 2010-2018 ARM Limited
3 * All rights reserved.
4 *
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.
13 *
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * Copyright (c) 2010,2015 Advanced Micro Devices, Inc.
16 * All rights reserved.
17 *
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.
28 *
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.
40 *
41 * Authors: Erik Hallnor
42 *          Dave Greene
43 *          Nathan Binkert
44 *          Steve Reinhardt
45 *          Ron Dreslinski
46 *          Andreas Sandberg
47 *          Nikos Nikoleris
48 */
49
50/**
51 * @file
52 * Cache definitions.
53 */
54
55#include "mem/cache/cache.hh"
56
57#include <cassert>
58
59#include "base/compiler.hh"
60#include "base/logging.hh"
61#include "base/trace.hh"
62#include "base/types.hh"
63#include "debug/Cache.hh"
64#include "debug/CacheTags.hh"
65#include "debug/CacheVerbose.hh"
66#include "enums/Clusivity.hh"
67#include "mem/cache/blk.hh"
68#include "mem/cache/mshr.hh"
69#include "mem/cache/tags/base.hh"
70#include "mem/cache/write_queue_entry.hh"
71#include "mem/request.hh"
72#include "params/Cache.hh"
73
74Cache::Cache(const CacheParams *p)
75    : BaseCache(p, p->system->cacheLineSize()),
76      doFastWrites(true)
77{
78}
79
80void
81Cache::satisfyRequest(PacketPtr pkt, CacheBlk *blk,
82                      bool deferred_response, bool pending_downgrade)
83{
84    BaseCache::satisfyRequest(pkt, blk);
85
86    if (pkt->isRead()) {
87        // determine if this read is from a (coherent) cache or not
88        if (pkt->fromCache()) {
89            assert(pkt->getSize() == blkSize);
90            // special handling for coherent block requests from
91            // upper-level caches
92            if (pkt->needsWritable()) {
93                // sanity check
94                assert(pkt->cmd == MemCmd::ReadExReq ||
95                       pkt->cmd == MemCmd::SCUpgradeFailReq);
96                assert(!pkt->hasSharers());
97
98                // if we have a dirty copy, make sure the recipient
99                // keeps it marked dirty (in the modified state)
100                if (blk->isDirty()) {
101                    pkt->setCacheResponding();
102                    blk->status &= ~BlkDirty;
103                }
104            } else if (blk->isWritable() && !pending_downgrade &&
105                       !pkt->hasSharers() &&
106                       pkt->cmd != MemCmd::ReadCleanReq) {
107                // we can give the requester a writable copy on a read
108                // request if:
109                // - we have a writable copy at this level (& below)
110                // - we don't have a pending snoop from below
111                //   signaling another read request
112                // - no other cache above has a copy (otherwise it
113                //   would have set hasSharers flag when
114                //   snooping the packet)
115                // - the read has explicitly asked for a clean
116                //   copy of the line
117                if (blk->isDirty()) {
118                    // special considerations if we're owner:
119                    if (!deferred_response) {
120                        // respond with the line in Modified state
121                        // (cacheResponding set, hasSharers not set)
122                        pkt->setCacheResponding();
123
124                        // if this cache is mostly inclusive, we
125                        // keep the block in the Exclusive state,
126                        // and pass it upwards as Modified
127                        // (writable and dirty), hence we have
128                        // multiple caches, all on the same path
129                        // towards memory, all considering the
130                        // same block writable, but only one
131                        // considering it Modified
132
133                        // we get away with multiple caches (on
134                        // the same path to memory) considering
135                        // the block writeable as we always enter
136                        // the cache hierarchy through a cache,
137                        // and first snoop upwards in all other
138                        // branches
139                        blk->status &= ~BlkDirty;
140                    } else {
141                        // if we're responding after our own miss,
142                        // there's a window where the recipient didn't
143                        // know it was getting ownership and may not
144                        // have responded to snoops correctly, so we
145                        // have to respond with a shared line
146                        pkt->setHasSharers();
147                    }
148                }
149            } else {
150                // otherwise only respond with a shared copy
151                pkt->setHasSharers();
152            }
153        }
154    }
155}
156
157/////////////////////////////////////////////////////
158//
159// Access path: requests coming in from the CPU side
160//
161/////////////////////////////////////////////////////
162
163bool
164Cache::access(PacketPtr pkt, CacheBlk *&blk, Cycles &lat,
165              PacketList &writebacks)
166{
167
168    if (pkt->req->isUncacheable()) {
169        assert(pkt->isRequest());
170
171        chatty_assert(!(isReadOnly && pkt->isWrite()),
172                      "Should never see a write in a read-only cache %s\n",
173                      name());
174
175        DPRINTF(Cache, "%s for %s\n", __func__, pkt->print());
176
177        // flush and invalidate any existing block
178        CacheBlk *old_blk(tags->findBlock(pkt->getAddr(), pkt->isSecure()));
179        if (old_blk && old_blk->isValid()) {
180            evictBlock(old_blk, writebacks);
181        }
182
183        blk = nullptr;
184        // lookupLatency is the latency in case the request is uncacheable.
185        lat = lookupLatency;
186        return false;
187    }
188
189    return BaseCache::access(pkt, blk, lat, writebacks);
190}
191
192void
193Cache::doWritebacks(PacketList& writebacks, Tick forward_time)
194{
195    while (!writebacks.empty()) {
196        PacketPtr wbPkt = writebacks.front();
197        // We use forwardLatency here because we are copying writebacks to
198        // write buffer.
199
200        // Call isCachedAbove for Writebacks, CleanEvicts and
201        // WriteCleans to discover if the block is cached above.
202        if (isCachedAbove(wbPkt)) {
203            if (wbPkt->cmd == MemCmd::CleanEvict) {
204                // Delete CleanEvict because cached copies exist above. The
205                // packet destructor will delete the request object because
206                // this is a non-snoop request packet which does not require a
207                // response.
208                delete wbPkt;
209            } else if (wbPkt->cmd == MemCmd::WritebackClean) {
210                // clean writeback, do not send since the block is
211                // still cached above
212                assert(writebackClean);
213                delete wbPkt;
214            } else {
215                assert(wbPkt->cmd == MemCmd::WritebackDirty ||
216                       wbPkt->cmd == MemCmd::WriteClean);
217                // Set BLOCK_CACHED flag in Writeback and send below, so that
218                // the Writeback does not reset the bit corresponding to this
219                // address in the snoop filter below.
220                wbPkt->setBlockCached();
221                allocateWriteBuffer(wbPkt, forward_time);
222            }
223        } else {
224            // If the block is not cached above, send packet below. Both
225            // CleanEvict and Writeback with BLOCK_CACHED flag cleared will
226            // reset the bit corresponding to this address in the snoop filter
227            // below.
228            allocateWriteBuffer(wbPkt, forward_time);
229        }
230        writebacks.pop_front();
231    }
232}
233
234void
235Cache::doWritebacksAtomic(PacketList& writebacks)
236{
237    while (!writebacks.empty()) {
238        PacketPtr wbPkt = writebacks.front();
239        // Call isCachedAbove for both Writebacks and CleanEvicts. If
240        // isCachedAbove returns true we set BLOCK_CACHED flag in Writebacks
241        // and discard CleanEvicts.
242        if (isCachedAbove(wbPkt, false)) {
243            if (wbPkt->cmd == MemCmd::WritebackDirty ||
244                wbPkt->cmd == MemCmd::WriteClean) {
245                // Set BLOCK_CACHED flag in Writeback and send below,
246                // so that the Writeback does not reset the bit
247                // corresponding to this address in the snoop filter
248                // below. We can discard CleanEvicts because cached
249                // copies exist above. Atomic mode isCachedAbove
250                // modifies packet to set BLOCK_CACHED flag
251                memSidePort.sendAtomic(wbPkt);
252            }
253        } else {
254            // If the block is not cached above, send packet below. Both
255            // CleanEvict and Writeback with BLOCK_CACHED flag cleared will
256            // reset the bit corresponding to this address in the snoop filter
257            // below.
258            memSidePort.sendAtomic(wbPkt);
259        }
260        writebacks.pop_front();
261        // In case of CleanEvicts, the packet destructor will delete the
262        // request object because this is a non-snoop request packet which
263        // does not require a response.
264        delete wbPkt;
265    }
266}
267
268
269void
270Cache::recvTimingSnoopResp(PacketPtr pkt)
271{
272    DPRINTF(Cache, "%s for %s\n", __func__, pkt->print());
273
274    // determine if the response is from a snoop request we created
275    // (in which case it should be in the outstandingSnoop), or if we
276    // merely forwarded someone else's snoop request
277    const bool forwardAsSnoop = outstandingSnoop.find(pkt->req) ==
278        outstandingSnoop.end();
279
280    if (!forwardAsSnoop) {
281        // the packet came from this cache, so sink it here and do not
282        // forward it
283        assert(pkt->cmd == MemCmd::HardPFResp);
284
285        outstandingSnoop.erase(pkt->req);
286
287        DPRINTF(Cache, "Got prefetch response from above for addr "
288                "%#llx (%s)\n", pkt->getAddr(), pkt->isSecure() ? "s" : "ns");
289        recvTimingResp(pkt);
290        return;
291    }
292
293    // forwardLatency is set here because there is a response from an
294    // upper level cache.
295    // To pay the delay that occurs if the packet comes from the bus,
296    // we charge also headerDelay.
297    Tick snoop_resp_time = clockEdge(forwardLatency) + pkt->headerDelay;
298    // Reset the timing of the packet.
299    pkt->headerDelay = pkt->payloadDelay = 0;
300    memSidePort.schedTimingSnoopResp(pkt, snoop_resp_time);
301}
302
303void
304Cache::promoteWholeLineWrites(PacketPtr pkt)
305{
306    // Cache line clearing instructions
307    if (doFastWrites && (pkt->cmd == MemCmd::WriteReq) &&
308        (pkt->getSize() == blkSize) && (pkt->getOffset(blkSize) == 0)) {
309        pkt->cmd = MemCmd::WriteLineReq;
310        DPRINTF(Cache, "packet promoted from Write to WriteLineReq\n");
311    }
312}
313
314void
315Cache::handleTimingReqHit(PacketPtr pkt, CacheBlk *blk, Tick request_time)
316{
317    // should never be satisfying an uncacheable access as we
318    // flush and invalidate any existing block as part of the
319    // lookup
320    assert(!pkt->req->isUncacheable());
321
322    BaseCache::handleTimingReqHit(pkt, blk, request_time);
323}
324
325void
326Cache::handleTimingReqMiss(PacketPtr pkt, CacheBlk *blk, Tick forward_time,
327                           Tick request_time)
328{
329    if (pkt->req->isUncacheable()) {
330        // ignore any existing MSHR if we are dealing with an
331        // uncacheable request
332
333        // should have flushed and have no valid block
334        assert(!blk || !blk->isValid());
335
336        mshr_uncacheable[pkt->cmdToIndex()][pkt->req->masterId()]++;
337
338        if (pkt->isWrite()) {
339            allocateWriteBuffer(pkt, forward_time);
340        } else {
341            assert(pkt->isRead());
342
343            // uncacheable accesses always allocate a new MSHR
344
345            // Here we are using forward_time, modelling the latency of
346            // a miss (outbound) just as forwardLatency, neglecting the
347            // lookupLatency component.
348            allocateMissBuffer(pkt, forward_time);
349        }
350
351        return;
352    }
353
354    Addr blk_addr = pkt->getBlockAddr(blkSize);
355
356    MSHR *mshr = mshrQueue.findMatch(blk_addr, pkt->isSecure());
357
358    // Software prefetch handling:
359    // To keep the core from waiting on data it won't look at
360    // anyway, send back a response with dummy data. Miss handling
361    // will continue asynchronously. Unfortunately, the core will
362    // insist upon freeing original Packet/Request, so we have to
363    // create a new pair with a different lifecycle. Note that this
364    // processing happens before any MSHR munging on the behalf of
365    // this request because this new Request will be the one stored
366    // into the MSHRs, not the original.
367    if (pkt->cmd.isSWPrefetch()) {
368        assert(pkt->needsResponse());
369        assert(pkt->req->hasPaddr());
370        assert(!pkt->req->isUncacheable());
371
372        // There's no reason to add a prefetch as an additional target
373        // to an existing MSHR. If an outstanding request is already
374        // in progress, there is nothing for the prefetch to do.
375        // If this is the case, we don't even create a request at all.
376        PacketPtr pf = nullptr;
377
378        if (!mshr) {
379            // copy the request and create a new SoftPFReq packet
380            RequestPtr req = new Request(pkt->req->getPaddr(),
381                                         pkt->req->getSize(),
382                                         pkt->req->getFlags(),
383                                         pkt->req->masterId());
384            pf = new Packet(req, pkt->cmd);
385            pf->allocate();
386            assert(pf->getAddr() == pkt->getAddr());
387            assert(pf->getSize() == pkt->getSize());
388        }
389
390        pkt->makeTimingResponse();
391
392        // request_time is used here, taking into account lat and the delay
393        // charged if the packet comes from the xbar.
394        cpuSidePort.schedTimingResp(pkt, request_time, true);
395
396        // If an outstanding request is in progress (we found an
397        // MSHR) this is set to null
398        pkt = pf;
399    }
400
401    BaseCache::handleTimingReqMiss(pkt, mshr, blk, forward_time, request_time);
402}
403
404void
405Cache::recvTimingReq(PacketPtr pkt)
406{
407    DPRINTF(CacheTags, "%s tags:\n%s\n", __func__, tags->print());
408
409    promoteWholeLineWrites(pkt);
410
411    if (pkt->cacheResponding()) {
412        // a cache above us (but not where the packet came from) is
413        // responding to the request, in other words it has the line
414        // in Modified or Owned state
415        DPRINTF(Cache, "Cache above responding to %s: not responding\n",
416                pkt->print());
417
418        // if the packet needs the block to be writable, and the cache
419        // that has promised to respond (setting the cache responding
420        // flag) is not providing writable (it is in Owned rather than
421        // the Modified state), we know that there may be other Shared
422        // copies in the system; go out and invalidate them all
423        assert(pkt->needsWritable() && !pkt->responderHadWritable());
424
425        // an upstream cache that had the line in Owned state
426        // (dirty, but not writable), is responding and thus
427        // transferring the dirty line from one branch of the
428        // cache hierarchy to another
429
430        // send out an express snoop and invalidate all other
431        // copies (snooping a packet that needs writable is the
432        // same as an invalidation), thus turning the Owned line
433        // into a Modified line, note that we don't invalidate the
434        // block in the current cache or any other cache on the
435        // path to memory
436
437        // create a downstream express snoop with cleared packet
438        // flags, there is no need to allocate any data as the
439        // packet is merely used to co-ordinate state transitions
440        Packet *snoop_pkt = new Packet(pkt, true, false);
441
442        // also reset the bus time that the original packet has
443        // not yet paid for
444        snoop_pkt->headerDelay = snoop_pkt->payloadDelay = 0;
445
446        // make this an instantaneous express snoop, and let the
447        // other caches in the system know that the another cache
448        // is responding, because we have found the authorative
449        // copy (Modified or Owned) that will supply the right
450        // data
451        snoop_pkt->setExpressSnoop();
452        snoop_pkt->setCacheResponding();
453
454        // this express snoop travels towards the memory, and at
455        // every crossbar it is snooped upwards thus reaching
456        // every cache in the system
457        bool M5_VAR_USED success = memSidePort.sendTimingReq(snoop_pkt);
458        // express snoops always succeed
459        assert(success);
460
461        // main memory will delete the snoop packet
462
463        // queue for deletion, as opposed to immediate deletion, as
464        // the sending cache is still relying on the packet
465        pendingDelete.reset(pkt);
466
467        // no need to take any further action in this particular cache
468        // as an upstram cache has already committed to responding,
469        // and we have already sent out any express snoops in the
470        // section above to ensure all other copies in the system are
471        // invalidated
472        return;
473    }
474
475    BaseCache::recvTimingReq(pkt);
476}
477
478PacketPtr
479Cache::createMissPacket(PacketPtr cpu_pkt, CacheBlk *blk,
480                        bool needsWritable) const
481{
482    // should never see evictions here
483    assert(!cpu_pkt->isEviction());
484
485    bool blkValid = blk && blk->isValid();
486
487    if (cpu_pkt->req->isUncacheable() ||
488        (!blkValid && cpu_pkt->isUpgrade()) ||
489        cpu_pkt->cmd == MemCmd::InvalidateReq || cpu_pkt->isClean()) {
490        // uncacheable requests and upgrades from upper-level caches
491        // that missed completely just go through as is
492        return nullptr;
493    }
494
495    assert(cpu_pkt->needsResponse());
496
497    MemCmd cmd;
498    // @TODO make useUpgrades a parameter.
499    // Note that ownership protocols require upgrade, otherwise a
500    // write miss on a shared owned block will generate a ReadExcl,
501    // which will clobber the owned copy.
502    const bool useUpgrades = true;
503    if (cpu_pkt->cmd == MemCmd::WriteLineReq) {
504        assert(!blkValid || !blk->isWritable());
505        // forward as invalidate to all other caches, this gives us
506        // the line in Exclusive state, and invalidates all other
507        // copies
508        cmd = MemCmd::InvalidateReq;
509    } else if (blkValid && useUpgrades) {
510        // only reason to be here is that blk is read only and we need
511        // it to be writable
512        assert(needsWritable);
513        assert(!blk->isWritable());
514        cmd = cpu_pkt->isLLSC() ? MemCmd::SCUpgradeReq : MemCmd::UpgradeReq;
515    } else if (cpu_pkt->cmd == MemCmd::SCUpgradeFailReq ||
516               cpu_pkt->cmd == MemCmd::StoreCondFailReq) {
517        // Even though this SC will fail, we still need to send out the
518        // request and get the data to supply it to other snoopers in the case
519        // where the determination the StoreCond fails is delayed due to
520        // all caches not being on the same local bus.
521        cmd = MemCmd::SCUpgradeFailReq;
522    } else {
523        // block is invalid
524
525        // If the request does not need a writable there are two cases
526        // where we need to ensure the response will not fetch the
527        // block in dirty state:
528        // * this cache is read only and it does not perform
529        //   writebacks,
530        // * this cache is mostly exclusive and will not fill (since
531        //   it does not fill it will have to writeback the dirty data
532        //   immediately which generates uneccesary writebacks).
533        bool force_clean_rsp = isReadOnly || clusivity == Enums::mostly_excl;
534        cmd = needsWritable ? MemCmd::ReadExReq :
535            (force_clean_rsp ? MemCmd::ReadCleanReq : MemCmd::ReadSharedReq);
536    }
537    PacketPtr pkt = new Packet(cpu_pkt->req, cmd, blkSize);
538
539    // if there are upstream caches that have already marked the
540    // packet as having sharers (not passing writable), pass that info
541    // downstream
542    if (cpu_pkt->hasSharers() && !needsWritable) {
543        // note that cpu_pkt may have spent a considerable time in the
544        // MSHR queue and that the information could possibly be out
545        // of date, however, there is no harm in conservatively
546        // assuming the block has sharers
547        pkt->setHasSharers();
548        DPRINTF(Cache, "%s: passing hasSharers from %s to %s\n",
549                __func__, cpu_pkt->print(), pkt->print());
550    }
551
552    // the packet should be block aligned
553    assert(pkt->getAddr() == pkt->getBlockAddr(blkSize));
554
555    pkt->allocate();
556    DPRINTF(Cache, "%s: created %s from %s\n", __func__, pkt->print(),
557            cpu_pkt->print());
558    return pkt;
559}
560
561
562Cycles
563Cache::handleAtomicReqMiss(PacketPtr pkt, CacheBlk *blk,
564                           PacketList &writebacks)
565{
566    // deal with the packets that go through the write path of
567    // the cache, i.e. any evictions and writes
568    if (pkt->isEviction() || pkt->cmd == MemCmd::WriteClean ||
569        (pkt->req->isUncacheable() && pkt->isWrite())) {
570        Cycles latency = ticksToCycles(memSidePort.sendAtomic(pkt));
571
572        // at this point, if the request was an uncacheable write
573        // request, it has been satisfied by a memory below and the
574        // packet carries the response back
575        assert(!(pkt->req->isUncacheable() && pkt->isWrite()) ||
576               pkt->isResponse());
577
578        return latency;
579    }
580
581    // only misses left
582
583    PacketPtr bus_pkt = createMissPacket(pkt, blk, pkt->needsWritable());
584
585    bool is_forward = (bus_pkt == nullptr);
586
587    if (is_forward) {
588        // just forwarding the same request to the next level
589        // no local cache operation involved
590        bus_pkt = pkt;
591    }
592
593    DPRINTF(Cache, "%s: Sending an atomic %s\n", __func__,
594            bus_pkt->print());
595
596#if TRACING_ON
597    CacheBlk::State old_state = blk ? blk->status : 0;
598#endif
599
600    Cycles latency = ticksToCycles(memSidePort.sendAtomic(bus_pkt));
601
602    bool is_invalidate = bus_pkt->isInvalidate();
603
604    // We are now dealing with the response handling
605    DPRINTF(Cache, "%s: Receive response: %s in state %i\n", __func__,
606            bus_pkt->print(), old_state);
607
608    // If packet was a forward, the response (if any) is already
609    // in place in the bus_pkt == pkt structure, so we don't need
610    // to do anything.  Otherwise, use the separate bus_pkt to
611    // generate response to pkt and then delete it.
612    if (!is_forward) {
613        if (pkt->needsResponse()) {
614            assert(bus_pkt->isResponse());
615            if (bus_pkt->isError()) {
616                pkt->makeAtomicResponse();
617                pkt->copyError(bus_pkt);
618            } else if (pkt->cmd == MemCmd::WriteLineReq) {
619                // note the use of pkt, not bus_pkt here.
620
621                // write-line request to the cache that promoted
622                // the write to a whole line
623                blk = handleFill(pkt, blk, writebacks,
624                                 allocOnFill(pkt->cmd));
625                assert(blk != NULL);
626                is_invalidate = false;
627                satisfyRequest(pkt, blk);
628            } else if (bus_pkt->isRead() ||
629                       bus_pkt->cmd == MemCmd::UpgradeResp) {
630                // we're updating cache state to allow us to
631                // satisfy the upstream request from the cache
632                blk = handleFill(bus_pkt, blk, writebacks,
633                                 allocOnFill(pkt->cmd));
634                satisfyRequest(pkt, blk);
635                maintainClusivity(pkt->fromCache(), blk);
636            } else {
637                // we're satisfying the upstream request without
638                // modifying cache state, e.g., a write-through
639                pkt->makeAtomicResponse();
640            }
641        }
642        delete bus_pkt;
643    }
644
645    if (is_invalidate && blk && blk->isValid()) {
646        invalidateBlock(blk);
647    }
648
649    return latency;
650}
651
652Tick
653Cache::recvAtomic(PacketPtr pkt)
654{
655    promoteWholeLineWrites(pkt);
656
657    return BaseCache::recvAtomic(pkt);
658}
659
660
661/////////////////////////////////////////////////////
662//
663// Response handling: responses from the memory side
664//
665/////////////////////////////////////////////////////
666
667
668void
669Cache::serviceMSHRTargets(MSHR *mshr, const PacketPtr pkt, CacheBlk *blk,
670                          PacketList &writebacks)
671{
672    MSHR::Target *initial_tgt = mshr->getTarget();
673    // First offset for critical word first calculations
674    const int initial_offset = initial_tgt->pkt->getOffset(blkSize);
675
676    const bool is_error = pkt->isError();
677    bool is_fill = !mshr->isForward &&
678        (pkt->isRead() || pkt->cmd == MemCmd::UpgradeResp);
679    // allow invalidation responses originating from write-line
680    // requests to be discarded
681    bool is_invalidate = pkt->isInvalidate();
682
683    MSHR::TargetList targets = mshr->extractServiceableTargets(pkt);
684    for (auto &target: targets) {
685        Packet *tgt_pkt = target.pkt;
686        switch (target.source) {
687          case MSHR::Target::FromCPU:
688            Tick completion_time;
689            // Here we charge on completion_time the delay of the xbar if the
690            // packet comes from it, charged on headerDelay.
691            completion_time = pkt->headerDelay;
692
693            // Software prefetch handling for cache closest to core
694            if (tgt_pkt->cmd.isSWPrefetch()) {
695                // a software prefetch would have already been ack'd
696                // immediately with dummy data so the core would be able to
697                // retire it. This request completes right here, so we
698                // deallocate it.
699                delete tgt_pkt->req;
700                delete tgt_pkt;
701                break; // skip response
702            }
703
704            // unlike the other packet flows, where data is found in other
705            // caches or memory and brought back, write-line requests always
706            // have the data right away, so the above check for "is fill?"
707            // cannot actually be determined until examining the stored MSHR
708            // state. We "catch up" with that logic here, which is duplicated
709            // from above.
710            if (tgt_pkt->cmd == MemCmd::WriteLineReq) {
711                assert(!is_error);
712                // we got the block in a writable state, so promote
713                // any deferred targets if possible
714                mshr->promoteWritable();
715                // NB: we use the original packet here and not the response!
716                blk = handleFill(tgt_pkt, blk, writebacks,
717                                 targets.allocOnFill);
718                assert(blk);
719
720                // treat as a fill, and discard the invalidation
721                // response
722                is_fill = true;
723                is_invalidate = false;
724            }
725
726            if (is_fill) {
727                satisfyRequest(tgt_pkt, blk, true, mshr->hasPostDowngrade());
728
729                // How many bytes past the first request is this one
730                int transfer_offset =
731                    tgt_pkt->getOffset(blkSize) - initial_offset;
732                if (transfer_offset < 0) {
733                    transfer_offset += blkSize;
734                }
735
736                // If not critical word (offset) return payloadDelay.
737                // responseLatency is the latency of the return path
738                // from lower level caches/memory to an upper level cache or
739                // the core.
740                completion_time += clockEdge(responseLatency) +
741                    (transfer_offset ? pkt->payloadDelay : 0);
742
743                assert(!tgt_pkt->req->isUncacheable());
744
745                assert(tgt_pkt->req->masterId() < system->maxMasters());
746                missLatency[tgt_pkt->cmdToIndex()][tgt_pkt->req->masterId()] +=
747                    completion_time - target.recvTime;
748            } else if (pkt->cmd == MemCmd::UpgradeFailResp) {
749                // failed StoreCond upgrade
750                assert(tgt_pkt->cmd == MemCmd::StoreCondReq ||
751                       tgt_pkt->cmd == MemCmd::StoreCondFailReq ||
752                       tgt_pkt->cmd == MemCmd::SCUpgradeFailReq);
753                // responseLatency is the latency of the return path
754                // from lower level caches/memory to an upper level cache or
755                // the core.
756                completion_time += clockEdge(responseLatency) +
757                    pkt->payloadDelay;
758                tgt_pkt->req->setExtraData(0);
759            } else {
760                // We are about to send a response to a cache above
761                // that asked for an invalidation; we need to
762                // invalidate our copy immediately as the most
763                // up-to-date copy of the block will now be in the
764                // cache above. It will also prevent this cache from
765                // responding (if the block was previously dirty) to
766                // snoops as they should snoop the caches above where
767                // they will get the response from.
768                if (is_invalidate && blk && blk->isValid()) {
769                    invalidateBlock(blk);
770                }
771                // not a cache fill, just forwarding response
772                // responseLatency is the latency of the return path
773                // from lower level cahces/memory to the core.
774                completion_time += clockEdge(responseLatency) +
775                    pkt->payloadDelay;
776                if (pkt->isRead() && !is_error) {
777                    // sanity check
778                    assert(pkt->getAddr() == tgt_pkt->getAddr());
779                    assert(pkt->getSize() >= tgt_pkt->getSize());
780
781                    tgt_pkt->setData(pkt->getConstPtr<uint8_t>());
782                }
783            }
784            tgt_pkt->makeTimingResponse();
785            // if this packet is an error copy that to the new packet
786            if (is_error)
787                tgt_pkt->copyError(pkt);
788            if (tgt_pkt->cmd == MemCmd::ReadResp &&
789                (is_invalidate || mshr->hasPostInvalidate())) {
790                // If intermediate cache got ReadRespWithInvalidate,
791                // propagate that.  Response should not have
792                // isInvalidate() set otherwise.
793                tgt_pkt->cmd = MemCmd::ReadRespWithInvalidate;
794                DPRINTF(Cache, "%s: updated cmd to %s\n", __func__,
795                        tgt_pkt->print());
796            }
797            // Reset the bus additional time as it is now accounted for
798            tgt_pkt->headerDelay = tgt_pkt->payloadDelay = 0;
799            cpuSidePort.schedTimingResp(tgt_pkt, completion_time, true);
800            break;
801
802          case MSHR::Target::FromPrefetcher:
803            assert(tgt_pkt->cmd == MemCmd::HardPFReq);
804            if (blk)
805                blk->status |= BlkHWPrefetched;
806            delete tgt_pkt->req;
807            delete tgt_pkt;
808            break;
809
810          case MSHR::Target::FromSnoop:
811            // I don't believe that a snoop can be in an error state
812            assert(!is_error);
813            // response to snoop request
814            DPRINTF(Cache, "processing deferred snoop...\n");
815            // If the response is invalidating, a snooping target can
816            // be satisfied if it is also invalidating. If the reponse is, not
817            // only invalidating, but more specifically an InvalidateResp and
818            // the MSHR was created due to an InvalidateReq then a cache above
819            // is waiting to satisfy a WriteLineReq. In this case even an
820            // non-invalidating snoop is added as a target here since this is
821            // the ordering point. When the InvalidateResp reaches this cache,
822            // the snooping target will snoop further the cache above with the
823            // WriteLineReq.
824            assert(!is_invalidate || pkt->cmd == MemCmd::InvalidateResp ||
825                   pkt->req->isCacheMaintenance() ||
826                   mshr->hasPostInvalidate());
827            handleSnoop(tgt_pkt, blk, true, true, mshr->hasPostInvalidate());
828            break;
829
830          default:
831            panic("Illegal target->source enum %d\n", target.source);
832        }
833    }
834
835    maintainClusivity(targets.hasFromCache, blk);
836
837    if (blk && blk->isValid()) {
838        // an invalidate response stemming from a write line request
839        // should not invalidate the block, so check if the
840        // invalidation should be discarded
841        if (is_invalidate || mshr->hasPostInvalidate()) {
842            invalidateBlock(blk);
843        } else if (mshr->hasPostDowngrade()) {
844            blk->status &= ~BlkWritable;
845        }
846    }
847}
848
849PacketPtr
850Cache::evictBlock(CacheBlk *blk)
851{
852    PacketPtr pkt = (blk->isDirty() || writebackClean) ?
853        writebackBlk(blk) : cleanEvictBlk(blk);
854
855    invalidateBlock(blk);
856
857    return pkt;
858}
859
860void
861Cache::evictBlock(CacheBlk *blk, PacketList &writebacks)
862{
863    PacketPtr pkt = evictBlock(blk);
864    if (pkt) {
865        writebacks.push_back(pkt);
866    }
867}
868
869PacketPtr
870Cache::cleanEvictBlk(CacheBlk *blk)
871{
872    assert(!writebackClean);
873    assert(blk && blk->isValid() && !blk->isDirty());
874    // Creating a zero sized write, a message to the snoop filter
875    Request *req =
876        new Request(regenerateBlkAddr(blk), blkSize, 0,
877                    Request::wbMasterId);
878    if (blk->isSecure())
879        req->setFlags(Request::SECURE);
880
881    req->taskId(blk->task_id);
882
883    PacketPtr pkt = new Packet(req, MemCmd::CleanEvict);
884    pkt->allocate();
885    DPRINTF(Cache, "Create CleanEvict %s\n", pkt->print());
886
887    return pkt;
888}
889
890/////////////////////////////////////////////////////
891//
892// Snoop path: requests coming in from the memory side
893//
894/////////////////////////////////////////////////////
895
896void
897Cache::doTimingSupplyResponse(PacketPtr req_pkt, const uint8_t *blk_data,
898                              bool already_copied, bool pending_inval)
899{
900    // sanity check
901    assert(req_pkt->isRequest());
902    assert(req_pkt->needsResponse());
903
904    DPRINTF(Cache, "%s: for %s\n", __func__, req_pkt->print());
905    // timing-mode snoop responses require a new packet, unless we
906    // already made a copy...
907    PacketPtr pkt = req_pkt;
908    if (!already_copied)
909        // do not clear flags, and allocate space for data if the
910        // packet needs it (the only packets that carry data are read
911        // responses)
912        pkt = new Packet(req_pkt, false, req_pkt->isRead());
913
914    assert(req_pkt->req->isUncacheable() || req_pkt->isInvalidate() ||
915           pkt->hasSharers());
916    pkt->makeTimingResponse();
917    if (pkt->isRead()) {
918        pkt->setDataFromBlock(blk_data, blkSize);
919    }
920    if (pkt->cmd == MemCmd::ReadResp && pending_inval) {
921        // Assume we defer a response to a read from a far-away cache
922        // A, then later defer a ReadExcl from a cache B on the same
923        // bus as us. We'll assert cacheResponding in both cases, but
924        // in the latter case cacheResponding will keep the
925        // invalidation from reaching cache A. This special response
926        // tells cache A that it gets the block to satisfy its read,
927        // but must immediately invalidate it.
928        pkt->cmd = MemCmd::ReadRespWithInvalidate;
929    }
930    // Here we consider forward_time, paying for just forward latency and
931    // also charging the delay provided by the xbar.
932    // forward_time is used as send_time in next allocateWriteBuffer().
933    Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
934    // Here we reset the timing of the packet.
935    pkt->headerDelay = pkt->payloadDelay = 0;
936    DPRINTF(CacheVerbose, "%s: created response: %s tick: %lu\n", __func__,
937            pkt->print(), forward_time);
938    memSidePort.schedTimingSnoopResp(pkt, forward_time, true);
939}
940
941uint32_t
942Cache::handleSnoop(PacketPtr pkt, CacheBlk *blk, bool is_timing,
943                   bool is_deferred, bool pending_inval)
944{
945    DPRINTF(CacheVerbose, "%s: for %s\n", __func__, pkt->print());
946    // deferred snoops can only happen in timing mode
947    assert(!(is_deferred && !is_timing));
948    // pending_inval only makes sense on deferred snoops
949    assert(!(pending_inval && !is_deferred));
950    assert(pkt->isRequest());
951
952    // the packet may get modified if we or a forwarded snooper
953    // responds in atomic mode, so remember a few things about the
954    // original packet up front
955    bool invalidate = pkt->isInvalidate();
956    bool M5_VAR_USED needs_writable = pkt->needsWritable();
957
958    // at the moment we could get an uncacheable write which does not
959    // have the invalidate flag, and we need a suitable way of dealing
960    // with this case
961    panic_if(invalidate && pkt->req->isUncacheable(),
962             "%s got an invalidating uncacheable snoop request %s",
963             name(), pkt->print());
964
965    uint32_t snoop_delay = 0;
966
967    if (forwardSnoops) {
968        // first propagate snoop upward to see if anyone above us wants to
969        // handle it.  save & restore packet src since it will get
970        // rewritten to be relative to cpu-side bus (if any)
971        bool alreadyResponded = pkt->cacheResponding();
972        if (is_timing) {
973            // copy the packet so that we can clear any flags before
974            // forwarding it upwards, we also allocate data (passing
975            // the pointer along in case of static data), in case
976            // there is a snoop hit in upper levels
977            Packet snoopPkt(pkt, true, true);
978            snoopPkt.setExpressSnoop();
979            // the snoop packet does not need to wait any additional
980            // time
981            snoopPkt.headerDelay = snoopPkt.payloadDelay = 0;
982            cpuSidePort.sendTimingSnoopReq(&snoopPkt);
983
984            // add the header delay (including crossbar and snoop
985            // delays) of the upward snoop to the snoop delay for this
986            // cache
987            snoop_delay += snoopPkt.headerDelay;
988
989            if (snoopPkt.cacheResponding()) {
990                // cache-to-cache response from some upper cache
991                assert(!alreadyResponded);
992                pkt->setCacheResponding();
993            }
994            // upstream cache has the block, or has an outstanding
995            // MSHR, pass the flag on
996            if (snoopPkt.hasSharers()) {
997                pkt->setHasSharers();
998            }
999            // If this request is a prefetch or clean evict and an upper level
1000            // signals block present, make sure to propagate the block
1001            // presence to the requester.
1002            if (snoopPkt.isBlockCached()) {
1003                pkt->setBlockCached();
1004            }
1005            // If the request was satisfied by snooping the cache
1006            // above, mark the original packet as satisfied too.
1007            if (snoopPkt.satisfied()) {
1008                pkt->setSatisfied();
1009            }
1010        } else {
1011            cpuSidePort.sendAtomicSnoop(pkt);
1012            if (!alreadyResponded && pkt->cacheResponding()) {
1013                // cache-to-cache response from some upper cache:
1014                // forward response to original requester
1015                assert(pkt->isResponse());
1016            }
1017        }
1018    }
1019
1020    bool respond = false;
1021    bool blk_valid = blk && blk->isValid();
1022    if (pkt->isClean()) {
1023        if (blk_valid && blk->isDirty()) {
1024            DPRINTF(CacheVerbose, "%s: packet (snoop) %s found block: %s\n",
1025                    __func__, pkt->print(), blk->print());
1026            PacketPtr wb_pkt = writecleanBlk(blk, pkt->req->getDest(), pkt->id);
1027            PacketList writebacks;
1028            writebacks.push_back(wb_pkt);
1029
1030            if (is_timing) {
1031                // anything that is merely forwarded pays for the forward
1032                // latency and the delay provided by the crossbar
1033                Tick forward_time = clockEdge(forwardLatency) +
1034                    pkt->headerDelay;
1035                doWritebacks(writebacks, forward_time);
1036            } else {
1037                doWritebacksAtomic(writebacks);
1038            }
1039            pkt->setSatisfied();
1040        }
1041    } else if (!blk_valid) {
1042        DPRINTF(CacheVerbose, "%s: snoop miss for %s\n", __func__,
1043                pkt->print());
1044        if (is_deferred) {
1045            // we no longer have the block, and will not respond, but a
1046            // packet was allocated in MSHR::handleSnoop and we have
1047            // to delete it
1048            assert(pkt->needsResponse());
1049
1050            // we have passed the block to a cache upstream, that
1051            // cache should be responding
1052            assert(pkt->cacheResponding());
1053
1054            delete pkt;
1055        }
1056        return snoop_delay;
1057    } else {
1058        DPRINTF(Cache, "%s: snoop hit for %s, old state is %s\n", __func__,
1059                pkt->print(), blk->print());
1060
1061        // We may end up modifying both the block state and the packet (if
1062        // we respond in atomic mode), so just figure out what to do now
1063        // and then do it later. We respond to all snoops that need
1064        // responses provided we have the block in dirty state. The
1065        // invalidation itself is taken care of below. We don't respond to
1066        // cache maintenance operations as this is done by the destination
1067        // xbar.
1068        respond = blk->isDirty() && pkt->needsResponse();
1069
1070        chatty_assert(!(isReadOnly && blk->isDirty()), "Should never have "
1071                      "a dirty block in a read-only cache %s\n", name());
1072    }
1073
1074    // Invalidate any prefetch's from below that would strip write permissions
1075    // MemCmd::HardPFReq is only observed by upstream caches.  After missing
1076    // above and in it's own cache, a new MemCmd::ReadReq is created that
1077    // downstream caches observe.
1078    if (pkt->mustCheckAbove()) {
1079        DPRINTF(Cache, "Found addr %#llx in upper level cache for snoop %s "
1080                "from lower cache\n", pkt->getAddr(), pkt->print());
1081        pkt->setBlockCached();
1082        return snoop_delay;
1083    }
1084
1085    if (pkt->isRead() && !invalidate) {
1086        // reading without requiring the line in a writable state
1087        assert(!needs_writable);
1088        pkt->setHasSharers();
1089
1090        // if the requesting packet is uncacheable, retain the line in
1091        // the current state, otherwhise unset the writable flag,
1092        // which means we go from Modified to Owned (and will respond
1093        // below), remain in Owned (and will respond below), from
1094        // Exclusive to Shared, or remain in Shared
1095        if (!pkt->req->isUncacheable())
1096            blk->status &= ~BlkWritable;
1097        DPRINTF(Cache, "new state is %s\n", blk->print());
1098    }
1099
1100    if (respond) {
1101        // prevent anyone else from responding, cache as well as
1102        // memory, and also prevent any memory from even seeing the
1103        // request
1104        pkt->setCacheResponding();
1105        if (!pkt->isClean() && blk->isWritable()) {
1106            // inform the cache hierarchy that this cache had the line
1107            // in the Modified state so that we avoid unnecessary
1108            // invalidations (see Packet::setResponderHadWritable)
1109            pkt->setResponderHadWritable();
1110
1111            // in the case of an uncacheable request there is no point
1112            // in setting the responderHadWritable flag, but since the
1113            // recipient does not care there is no harm in doing so
1114        } else {
1115            // if the packet has needsWritable set we invalidate our
1116            // copy below and all other copies will be invalidates
1117            // through express snoops, and if needsWritable is not set
1118            // we already called setHasSharers above
1119        }
1120
1121        // if we are returning a writable and dirty (Modified) line,
1122        // we should be invalidating the line
1123        panic_if(!invalidate && !pkt->hasSharers(),
1124                 "%s is passing a Modified line through %s, "
1125                 "but keeping the block", name(), pkt->print());
1126
1127        if (is_timing) {
1128            doTimingSupplyResponse(pkt, blk->data, is_deferred, pending_inval);
1129        } else {
1130            pkt->makeAtomicResponse();
1131            // packets such as upgrades do not actually have any data
1132            // payload
1133            if (pkt->hasData())
1134                pkt->setDataFromBlock(blk->data, blkSize);
1135        }
1136    }
1137
1138    if (!respond && is_deferred) {
1139        assert(pkt->needsResponse());
1140
1141        // if we copied the deferred packet with the intention to
1142        // respond, but are not responding, then a cache above us must
1143        // be, and we can use this as the indication of whether this
1144        // is a packet where we created a copy of the request or not
1145        if (!pkt->cacheResponding()) {
1146            delete pkt->req;
1147        }
1148
1149        delete pkt;
1150    }
1151
1152    // Do this last in case it deallocates block data or something
1153    // like that
1154    if (blk_valid && invalidate) {
1155        invalidateBlock(blk);
1156        DPRINTF(Cache, "new state is %s\n", blk->print());
1157    }
1158
1159    return snoop_delay;
1160}
1161
1162
1163void
1164Cache::recvTimingSnoopReq(PacketPtr pkt)
1165{
1166    DPRINTF(CacheVerbose, "%s: for %s\n", __func__, pkt->print());
1167
1168    // no need to snoop requests that are not in range
1169    if (!inRange(pkt->getAddr())) {
1170        return;
1171    }
1172
1173    bool is_secure = pkt->isSecure();
1174    CacheBlk *blk = tags->findBlock(pkt->getAddr(), is_secure);
1175
1176    Addr blk_addr = pkt->getBlockAddr(blkSize);
1177    MSHR *mshr = mshrQueue.findMatch(blk_addr, is_secure);
1178
1179    // Update the latency cost of the snoop so that the crossbar can
1180    // account for it. Do not overwrite what other neighbouring caches
1181    // have already done, rather take the maximum. The update is
1182    // tentative, for cases where we return before an upward snoop
1183    // happens below.
1184    pkt->snoopDelay = std::max<uint32_t>(pkt->snoopDelay,
1185                                         lookupLatency * clockPeriod());
1186
1187    // Inform request(Prefetch, CleanEvict or Writeback) from below of
1188    // MSHR hit, set setBlockCached.
1189    if (mshr && pkt->mustCheckAbove()) {
1190        DPRINTF(Cache, "Setting block cached for %s from lower cache on "
1191                "mshr hit\n", pkt->print());
1192        pkt->setBlockCached();
1193        return;
1194    }
1195
1196    // Bypass any existing cache maintenance requests if the request
1197    // has been satisfied already (i.e., the dirty block has been
1198    // found).
1199    if (mshr && pkt->req->isCacheMaintenance() && pkt->satisfied()) {
1200        return;
1201    }
1202
1203    // Let the MSHR itself track the snoop and decide whether we want
1204    // to go ahead and do the regular cache snoop
1205    if (mshr && mshr->handleSnoop(pkt, order++)) {
1206        DPRINTF(Cache, "Deferring snoop on in-service MSHR to blk %#llx (%s)."
1207                "mshrs: %s\n", blk_addr, is_secure ? "s" : "ns",
1208                mshr->print());
1209
1210        if (mshr->getNumTargets() > numTarget)
1211            warn("allocating bonus target for snoop"); //handle later
1212        return;
1213    }
1214
1215    //We also need to check the writeback buffers and handle those
1216    WriteQueueEntry *wb_entry = writeBuffer.findMatch(blk_addr, is_secure);
1217    if (wb_entry) {
1218        DPRINTF(Cache, "Snoop hit in writeback to addr %#llx (%s)\n",
1219                pkt->getAddr(), is_secure ? "s" : "ns");
1220        // Expect to see only Writebacks and/or CleanEvicts here, both of
1221        // which should not be generated for uncacheable data.
1222        assert(!wb_entry->isUncacheable());
1223        // There should only be a single request responsible for generating
1224        // Writebacks/CleanEvicts.
1225        assert(wb_entry->getNumTargets() == 1);
1226        PacketPtr wb_pkt = wb_entry->getTarget()->pkt;
1227        assert(wb_pkt->isEviction() || wb_pkt->cmd == MemCmd::WriteClean);
1228
1229        if (pkt->isEviction()) {
1230            // if the block is found in the write queue, set the BLOCK_CACHED
1231            // flag for Writeback/CleanEvict snoop. On return the snoop will
1232            // propagate the BLOCK_CACHED flag in Writeback packets and prevent
1233            // any CleanEvicts from travelling down the memory hierarchy.
1234            pkt->setBlockCached();
1235            DPRINTF(Cache, "%s: Squashing %s from lower cache on writequeue "
1236                    "hit\n", __func__, pkt->print());
1237            return;
1238        }
1239
1240        // conceptually writebacks are no different to other blocks in
1241        // this cache, so the behaviour is modelled after handleSnoop,
1242        // the difference being that instead of querying the block
1243        // state to determine if it is dirty and writable, we use the
1244        // command and fields of the writeback packet
1245        bool respond = wb_pkt->cmd == MemCmd::WritebackDirty &&
1246            pkt->needsResponse();
1247        bool have_writable = !wb_pkt->hasSharers();
1248        bool invalidate = pkt->isInvalidate();
1249
1250        if (!pkt->req->isUncacheable() && pkt->isRead() && !invalidate) {
1251            assert(!pkt->needsWritable());
1252            pkt->setHasSharers();
1253            wb_pkt->setHasSharers();
1254        }
1255
1256        if (respond) {
1257            pkt->setCacheResponding();
1258
1259            if (have_writable) {
1260                pkt->setResponderHadWritable();
1261            }
1262
1263            doTimingSupplyResponse(pkt, wb_pkt->getConstPtr<uint8_t>(),
1264                                   false, false);
1265        }
1266
1267        if (invalidate && wb_pkt->cmd != MemCmd::WriteClean) {
1268            // Invalidation trumps our writeback... discard here
1269            // Note: markInService will remove entry from writeback buffer.
1270            markInService(wb_entry);
1271            delete wb_pkt;
1272        }
1273    }
1274
1275    // If this was a shared writeback, there may still be
1276    // other shared copies above that require invalidation.
1277    // We could be more selective and return here if the
1278    // request is non-exclusive or if the writeback is
1279    // exclusive.
1280    uint32_t snoop_delay = handleSnoop(pkt, blk, true, false, false);
1281
1282    // Override what we did when we first saw the snoop, as we now
1283    // also have the cost of the upwards snoops to account for
1284    pkt->snoopDelay = std::max<uint32_t>(pkt->snoopDelay, snoop_delay +
1285                                         lookupLatency * clockPeriod());
1286}
1287
1288Tick
1289Cache::recvAtomicSnoop(PacketPtr pkt)
1290{
1291    // no need to snoop requests that are not in range.
1292    if (!inRange(pkt->getAddr())) {
1293        return 0;
1294    }
1295
1296    CacheBlk *blk = tags->findBlock(pkt->getAddr(), pkt->isSecure());
1297    uint32_t snoop_delay = handleSnoop(pkt, blk, false, false, false);
1298    return snoop_delay + lookupLatency * clockPeriod();
1299}
1300
1301bool
1302Cache::isCachedAbove(PacketPtr pkt, bool is_timing)
1303{
1304    if (!forwardSnoops)
1305        return false;
1306    // Mirroring the flow of HardPFReqs, the cache sends CleanEvict and
1307    // Writeback snoops into upper level caches to check for copies of the
1308    // same block. Using the BLOCK_CACHED flag with the Writeback/CleanEvict
1309    // packet, the cache can inform the crossbar below of presence or absence
1310    // of the block.
1311    if (is_timing) {
1312        Packet snoop_pkt(pkt, true, false);
1313        snoop_pkt.setExpressSnoop();
1314        // Assert that packet is either Writeback or CleanEvict and not a
1315        // prefetch request because prefetch requests need an MSHR and may
1316        // generate a snoop response.
1317        assert(pkt->isEviction() || pkt->cmd == MemCmd::WriteClean);
1318        snoop_pkt.senderState = nullptr;
1319        cpuSidePort.sendTimingSnoopReq(&snoop_pkt);
1320        // Writeback/CleanEvict snoops do not generate a snoop response.
1321        assert(!(snoop_pkt.cacheResponding()));
1322        return snoop_pkt.isBlockCached();
1323    } else {
1324        cpuSidePort.sendAtomicSnoop(pkt);
1325        return pkt->isBlockCached();
1326    }
1327}
1328
1329bool
1330Cache::sendMSHRQueuePacket(MSHR* mshr)
1331{
1332    assert(mshr);
1333
1334    // use request from 1st target
1335    PacketPtr tgt_pkt = mshr->getTarget()->pkt;
1336
1337    if (tgt_pkt->cmd == MemCmd::HardPFReq && forwardSnoops) {
1338        DPRINTF(Cache, "%s: MSHR %s\n", __func__, tgt_pkt->print());
1339
1340        // we should never have hardware prefetches to allocated
1341        // blocks
1342        assert(!tags->findBlock(mshr->blkAddr, mshr->isSecure));
1343
1344        // We need to check the caches above us to verify that
1345        // they don't have a copy of this block in the dirty state
1346        // at the moment. Without this check we could get a stale
1347        // copy from memory that might get used in place of the
1348        // dirty one.
1349        Packet snoop_pkt(tgt_pkt, true, false);
1350        snoop_pkt.setExpressSnoop();
1351        // We are sending this packet upwards, but if it hits we will
1352        // get a snoop response that we end up treating just like a
1353        // normal response, hence it needs the MSHR as its sender
1354        // state
1355        snoop_pkt.senderState = mshr;
1356        cpuSidePort.sendTimingSnoopReq(&snoop_pkt);
1357
1358        // Check to see if the prefetch was squashed by an upper cache (to
1359        // prevent us from grabbing the line) or if a Check to see if a
1360        // writeback arrived between the time the prefetch was placed in
1361        // the MSHRs and when it was selected to be sent or if the
1362        // prefetch was squashed by an upper cache.
1363
1364        // It is important to check cacheResponding before
1365        // prefetchSquashed. If another cache has committed to
1366        // responding, it will be sending a dirty response which will
1367        // arrive at the MSHR allocated for this request. Checking the
1368        // prefetchSquash first may result in the MSHR being
1369        // prematurely deallocated.
1370        if (snoop_pkt.cacheResponding()) {
1371            auto M5_VAR_USED r = outstandingSnoop.insert(snoop_pkt.req);
1372            assert(r.second);
1373
1374            // if we are getting a snoop response with no sharers it
1375            // will be allocated as Modified
1376            bool pending_modified_resp = !snoop_pkt.hasSharers();
1377            markInService(mshr, pending_modified_resp);
1378
1379            DPRINTF(Cache, "Upward snoop of prefetch for addr"
1380                    " %#x (%s) hit\n",
1381                    tgt_pkt->getAddr(), tgt_pkt->isSecure()? "s": "ns");
1382            return false;
1383        }
1384
1385        if (snoop_pkt.isBlockCached()) {
1386            DPRINTF(Cache, "Block present, prefetch squashed by cache.  "
1387                    "Deallocating mshr target %#x.\n",
1388                    mshr->blkAddr);
1389
1390            // Deallocate the mshr target
1391            if (mshrQueue.forceDeallocateTarget(mshr)) {
1392                // Clear block if this deallocation resulted freed an
1393                // mshr when all had previously been utilized
1394                clearBlocked(Blocked_NoMSHRs);
1395            }
1396
1397            // given that no response is expected, delete Request and Packet
1398            delete tgt_pkt->req;
1399            delete tgt_pkt;
1400
1401            return false;
1402        }
1403    }
1404
1405    return BaseCache::sendMSHRQueuePacket(mshr);
1406}
1407
1408Cache*
1409CacheParams::create()
1410{
1411    assert(tags);
1412    assert(replacement_policy);
1413
1414    return new Cache(this);
1415}
1416