base.cc revision 13352:75647326f19b
1/*
2 * Copyright (c) 2012-2013, 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) 2003-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Erik Hallnor
41 *          Nikos Nikoleris
42 */
43
44/**
45 * @file
46 * Definition of BaseCache functions.
47 */
48
49#include "mem/cache/base.hh"
50
51#include "base/compiler.hh"
52#include "base/logging.hh"
53#include "debug/Cache.hh"
54#include "debug/CachePort.hh"
55#include "debug/CacheRepl.hh"
56#include "debug/CacheVerbose.hh"
57#include "mem/cache/mshr.hh"
58#include "mem/cache/prefetch/base.hh"
59#include "mem/cache/queue_entry.hh"
60#include "params/BaseCache.hh"
61#include "params/WriteAllocator.hh"
62#include "sim/core.hh"
63
64class BaseMasterPort;
65class BaseSlavePort;
66
67using namespace std;
68
69BaseCache::CacheSlavePort::CacheSlavePort(const std::string &_name,
70                                          BaseCache *_cache,
71                                          const std::string &_label)
72    : QueuedSlavePort(_name, _cache, queue), queue(*_cache, *this, _label),
73      blocked(false), mustSendRetry(false),
74      sendRetryEvent([this]{ processSendRetry(); }, _name)
75{
76}
77
78BaseCache::BaseCache(const BaseCacheParams *p, unsigned blk_size)
79    : MemObject(p),
80      cpuSidePort (p->name + ".cpu_side", this, "CpuSidePort"),
81      memSidePort(p->name + ".mem_side", this, "MemSidePort"),
82      mshrQueue("MSHRs", p->mshrs, 0, p->demand_mshr_reserve), // see below
83      writeBuffer("write buffer", p->write_buffers, p->mshrs), // see below
84      tags(p->tags),
85      prefetcher(p->prefetcher),
86      prefetchOnAccess(p->prefetch_on_access),
87      writeAllocator(p->write_allocator),
88      writebackClean(p->writeback_clean),
89      tempBlockWriteback(nullptr),
90      writebackTempBlockAtomicEvent([this]{ writebackTempBlockAtomic(); },
91                                    name(), false,
92                                    EventBase::Delayed_Writeback_Pri),
93      blkSize(blk_size),
94      lookupLatency(p->tag_latency),
95      dataLatency(p->data_latency),
96      forwardLatency(p->tag_latency),
97      fillLatency(p->data_latency),
98      responseLatency(p->response_latency),
99      numTarget(p->tgts_per_mshr),
100      forwardSnoops(true),
101      clusivity(p->clusivity),
102      isReadOnly(p->is_read_only),
103      blocked(0),
104      order(0),
105      noTargetMSHR(nullptr),
106      missCount(p->max_miss_count),
107      addrRanges(p->addr_ranges.begin(), p->addr_ranges.end()),
108      system(p->system)
109{
110    // the MSHR queue has no reserve entries as we check the MSHR
111    // queue on every single allocation, whereas the write queue has
112    // as many reserve entries as we have MSHRs, since every MSHR may
113    // eventually require a writeback, and we do not check the write
114    // buffer before committing to an MSHR
115
116    // forward snoops is overridden in init() once we can query
117    // whether the connected master is actually snooping or not
118
119    tempBlock = new TempCacheBlk(blkSize);
120
121    tags->init(this);
122    if (prefetcher)
123        prefetcher->setCache(this);
124}
125
126BaseCache::~BaseCache()
127{
128    delete tempBlock;
129}
130
131void
132BaseCache::CacheSlavePort::setBlocked()
133{
134    assert(!blocked);
135    DPRINTF(CachePort, "Port is blocking new requests\n");
136    blocked = true;
137    // if we already scheduled a retry in this cycle, but it has not yet
138    // happened, cancel it
139    if (sendRetryEvent.scheduled()) {
140        owner.deschedule(sendRetryEvent);
141        DPRINTF(CachePort, "Port descheduled retry\n");
142        mustSendRetry = true;
143    }
144}
145
146void
147BaseCache::CacheSlavePort::clearBlocked()
148{
149    assert(blocked);
150    DPRINTF(CachePort, "Port is accepting new requests\n");
151    blocked = false;
152    if (mustSendRetry) {
153        // @TODO: need to find a better time (next cycle?)
154        owner.schedule(sendRetryEvent, curTick() + 1);
155    }
156}
157
158void
159BaseCache::CacheSlavePort::processSendRetry()
160{
161    DPRINTF(CachePort, "Port is sending retry\n");
162
163    // reset the flag and call retry
164    mustSendRetry = false;
165    sendRetryReq();
166}
167
168Addr
169BaseCache::regenerateBlkAddr(CacheBlk* blk)
170{
171    if (blk != tempBlock) {
172        return tags->regenerateBlkAddr(blk);
173    } else {
174        return tempBlock->getAddr();
175    }
176}
177
178void
179BaseCache::init()
180{
181    if (!cpuSidePort.isConnected() || !memSidePort.isConnected())
182        fatal("Cache ports on %s are not connected\n", name());
183    cpuSidePort.sendRangeChange();
184    forwardSnoops = cpuSidePort.isSnooping();
185}
186
187BaseMasterPort &
188BaseCache::getMasterPort(const std::string &if_name, PortID idx)
189{
190    if (if_name == "mem_side") {
191        return memSidePort;
192    }  else {
193        return MemObject::getMasterPort(if_name, idx);
194    }
195}
196
197BaseSlavePort &
198BaseCache::getSlavePort(const std::string &if_name, PortID idx)
199{
200    if (if_name == "cpu_side") {
201        return cpuSidePort;
202    } else {
203        return MemObject::getSlavePort(if_name, idx);
204    }
205}
206
207bool
208BaseCache::inRange(Addr addr) const
209{
210    for (const auto& r : addrRanges) {
211        if (r.contains(addr)) {
212            return true;
213       }
214    }
215    return false;
216}
217
218void
219BaseCache::handleTimingReqHit(PacketPtr pkt, CacheBlk *blk, Tick request_time)
220{
221    if (pkt->needsResponse()) {
222        pkt->makeTimingResponse();
223        // @todo: Make someone pay for this
224        pkt->headerDelay = pkt->payloadDelay = 0;
225
226        // In this case we are considering request_time that takes
227        // into account the delay of the xbar, if any, and just
228        // lat, neglecting responseLatency, modelling hit latency
229        // just as lookupLatency or or the value of lat overriden
230        // by access(), that calls accessBlock() function.
231        cpuSidePort.schedTimingResp(pkt, request_time, true);
232    } else {
233        DPRINTF(Cache, "%s satisfied %s, no response needed\n", __func__,
234                pkt->print());
235
236        // queue the packet for deletion, as the sending cache is
237        // still relying on it; if the block is found in access(),
238        // CleanEvict and Writeback messages will be deleted
239        // here as well
240        pendingDelete.reset(pkt);
241    }
242}
243
244void
245BaseCache::handleTimingReqMiss(PacketPtr pkt, MSHR *mshr, CacheBlk *blk,
246                               Tick forward_time, Tick request_time)
247{
248    if (writeAllocator &&
249        pkt && pkt->isWrite() && !pkt->req->isUncacheable()) {
250        writeAllocator->updateMode(pkt->getAddr(), pkt->getSize(),
251                                   pkt->getBlockAddr(blkSize));
252    }
253
254    if (mshr) {
255        /// MSHR hit
256        /// @note writebacks will be checked in getNextMSHR()
257        /// for any conflicting requests to the same block
258
259        //@todo remove hw_pf here
260
261        // Coalesce unless it was a software prefetch (see above).
262        if (pkt) {
263            assert(!pkt->isWriteback());
264            // CleanEvicts corresponding to blocks which have
265            // outstanding requests in MSHRs are simply sunk here
266            if (pkt->cmd == MemCmd::CleanEvict) {
267                pendingDelete.reset(pkt);
268            } else if (pkt->cmd == MemCmd::WriteClean) {
269                // A WriteClean should never coalesce with any
270                // outstanding cache maintenance requests.
271
272                // We use forward_time here because there is an
273                // uncached memory write, forwarded to WriteBuffer.
274                allocateWriteBuffer(pkt, forward_time);
275            } else {
276                DPRINTF(Cache, "%s coalescing MSHR for %s\n", __func__,
277                        pkt->print());
278
279                assert(pkt->req->masterId() < system->maxMasters());
280                mshr_hits[pkt->cmdToIndex()][pkt->req->masterId()]++;
281
282                // We use forward_time here because it is the same
283                // considering new targets. We have multiple
284                // requests for the same address here. It
285                // specifies the latency to allocate an internal
286                // buffer and to schedule an event to the queued
287                // port and also takes into account the additional
288                // delay of the xbar.
289                mshr->allocateTarget(pkt, forward_time, order++,
290                                     allocOnFill(pkt->cmd));
291                if (mshr->getNumTargets() == numTarget) {
292                    noTargetMSHR = mshr;
293                    setBlocked(Blocked_NoTargets);
294                    // need to be careful with this... if this mshr isn't
295                    // ready yet (i.e. time > curTick()), we don't want to
296                    // move it ahead of mshrs that are ready
297                    // mshrQueue.moveToFront(mshr);
298                }
299            }
300        }
301    } else {
302        // no MSHR
303        assert(pkt->req->masterId() < system->maxMasters());
304        mshr_misses[pkt->cmdToIndex()][pkt->req->masterId()]++;
305
306        if (pkt->isEviction() || pkt->cmd == MemCmd::WriteClean) {
307            // We use forward_time here because there is an
308            // writeback or writeclean, forwarded to WriteBuffer.
309            allocateWriteBuffer(pkt, forward_time);
310        } else {
311            if (blk && blk->isValid()) {
312                // If we have a write miss to a valid block, we
313                // need to mark the block non-readable.  Otherwise
314                // if we allow reads while there's an outstanding
315                // write miss, the read could return stale data
316                // out of the cache block... a more aggressive
317                // system could detect the overlap (if any) and
318                // forward data out of the MSHRs, but we don't do
319                // that yet.  Note that we do need to leave the
320                // block valid so that it stays in the cache, in
321                // case we get an upgrade response (and hence no
322                // new data) when the write miss completes.
323                // As long as CPUs do proper store/load forwarding
324                // internally, and have a sufficiently weak memory
325                // model, this is probably unnecessary, but at some
326                // point it must have seemed like we needed it...
327                assert((pkt->needsWritable() && !blk->isWritable()) ||
328                       pkt->req->isCacheMaintenance());
329                blk->status &= ~BlkReadable;
330            }
331            // Here we are using forward_time, modelling the latency of
332            // a miss (outbound) just as forwardLatency, neglecting the
333            // lookupLatency component.
334            allocateMissBuffer(pkt, forward_time);
335        }
336    }
337}
338
339void
340BaseCache::recvTimingReq(PacketPtr pkt)
341{
342    // anything that is merely forwarded pays for the forward latency and
343    // the delay provided by the crossbar
344    Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
345
346    // We use lookupLatency here because it is used to specify the latency
347    // to access.
348    Cycles lat = lookupLatency;
349    CacheBlk *blk = nullptr;
350    bool satisfied = false;
351    {
352        PacketList writebacks;
353        // Note that lat is passed by reference here. The function
354        // access() calls accessBlock() which can modify lat value.
355        satisfied = access(pkt, blk, lat, writebacks);
356
357        // copy writebacks to write buffer here to ensure they logically
358        // precede anything happening below
359        doWritebacks(writebacks, forward_time);
360    }
361
362    // Here we charge the headerDelay that takes into account the latencies
363    // of the bus, if the packet comes from it.
364    // The latency charged it is just lat that is the value of lookupLatency
365    // modified by access() function, or if not just lookupLatency.
366    // In case of a hit we are neglecting response latency.
367    // In case of a miss we are neglecting forward latency.
368    Tick request_time = clockEdge(lat) + pkt->headerDelay;
369    // Here we reset the timing of the packet.
370    pkt->headerDelay = pkt->payloadDelay = 0;
371    // track time of availability of next prefetch, if any
372    Tick next_pf_time = MaxTick;
373
374    if (satisfied) {
375        // if need to notify the prefetcher we have to do it before
376        // anything else as later handleTimingReqHit might turn the
377        // packet in a response
378        if (prefetcher &&
379            (prefetchOnAccess || (blk && blk->wasPrefetched()))) {
380            if (blk)
381                blk->status &= ~BlkHWPrefetched;
382
383            // Don't notify on SWPrefetch
384            if (!pkt->cmd.isSWPrefetch()) {
385                assert(!pkt->req->isCacheMaintenance());
386                next_pf_time = prefetcher->notify(pkt);
387            }
388        }
389
390        handleTimingReqHit(pkt, blk, request_time);
391    } else {
392        handleTimingReqMiss(pkt, blk, forward_time, request_time);
393
394        // We should call the prefetcher reguardless if the request is
395        // satisfied or not, reguardless if the request is in the MSHR
396        // or not. The request could be a ReadReq hit, but still not
397        // satisfied (potentially because of a prior write to the same
398        // cache line. So, even when not satisfied, there is an MSHR
399        // already allocated for this, we need to let the prefetcher
400        // know about the request
401
402        // Don't notify prefetcher on SWPrefetch, cache maintenance
403        // operations or for writes that we are coaslescing.
404        if (prefetcher && pkt &&
405            !pkt->cmd.isSWPrefetch() &&
406            !pkt->req->isCacheMaintenance() &&
407            !(writeAllocator && writeAllocator->coalesce() &&
408              pkt->isWrite())) {
409            next_pf_time = prefetcher->notify(pkt);
410        }
411    }
412
413    if (next_pf_time != MaxTick) {
414        schedMemSideSendEvent(next_pf_time);
415    }
416}
417
418void
419BaseCache::handleUncacheableWriteResp(PacketPtr pkt)
420{
421    Tick completion_time = clockEdge(responseLatency) +
422        pkt->headerDelay + pkt->payloadDelay;
423
424    // Reset the bus additional time as it is now accounted for
425    pkt->headerDelay = pkt->payloadDelay = 0;
426
427    cpuSidePort.schedTimingResp(pkt, completion_time, true);
428}
429
430void
431BaseCache::recvTimingResp(PacketPtr pkt)
432{
433    assert(pkt->isResponse());
434
435    // all header delay should be paid for by the crossbar, unless
436    // this is a prefetch response from above
437    panic_if(pkt->headerDelay != 0 && pkt->cmd != MemCmd::HardPFResp,
438             "%s saw a non-zero packet delay\n", name());
439
440    const bool is_error = pkt->isError();
441
442    if (is_error) {
443        DPRINTF(Cache, "%s: Cache received %s with error\n", __func__,
444                pkt->print());
445    }
446
447    DPRINTF(Cache, "%s: Handling response %s\n", __func__,
448            pkt->print());
449
450    // if this is a write, we should be looking at an uncacheable
451    // write
452    if (pkt->isWrite()) {
453        assert(pkt->req->isUncacheable());
454        handleUncacheableWriteResp(pkt);
455        return;
456    }
457
458    // we have dealt with any (uncacheable) writes above, from here on
459    // we know we are dealing with an MSHR due to a miss or a prefetch
460    MSHR *mshr = dynamic_cast<MSHR*>(pkt->popSenderState());
461    assert(mshr);
462
463    if (mshr == noTargetMSHR) {
464        // we always clear at least one target
465        clearBlocked(Blocked_NoTargets);
466        noTargetMSHR = nullptr;
467    }
468
469    // Initial target is used just for stats
470    MSHR::Target *initial_tgt = mshr->getTarget();
471    int stats_cmd_idx = initial_tgt->pkt->cmdToIndex();
472    Tick miss_latency = curTick() - initial_tgt->recvTime;
473
474    if (pkt->req->isUncacheable()) {
475        assert(pkt->req->masterId() < system->maxMasters());
476        mshr_uncacheable_lat[stats_cmd_idx][pkt->req->masterId()] +=
477            miss_latency;
478    } else {
479        assert(pkt->req->masterId() < system->maxMasters());
480        mshr_miss_latency[stats_cmd_idx][pkt->req->masterId()] +=
481            miss_latency;
482    }
483
484    PacketList writebacks;
485
486    bool is_fill = !mshr->isForward &&
487        (pkt->isRead() || pkt->cmd == MemCmd::UpgradeResp ||
488         mshr->wasWholeLineWrite);
489
490    // make sure that if the mshr was due to a whole line write then
491    // the response is an invalidation
492    assert(!mshr->wasWholeLineWrite || pkt->isInvalidate());
493
494    CacheBlk *blk = tags->findBlock(pkt->getAddr(), pkt->isSecure());
495
496    if (is_fill && !is_error) {
497        DPRINTF(Cache, "Block for addr %#llx being updated in Cache\n",
498                pkt->getAddr());
499
500        const bool allocate = (writeAllocator && mshr->wasWholeLineWrite) ?
501            writeAllocator->allocate() : mshr->allocOnFill();
502        blk = handleFill(pkt, blk, writebacks, allocate);
503        assert(blk != nullptr);
504    }
505
506    if (blk && blk->isValid() && pkt->isClean() && !pkt->isInvalidate()) {
507        // The block was marked not readable while there was a pending
508        // cache maintenance operation, restore its flag.
509        blk->status |= BlkReadable;
510
511        // This was a cache clean operation (without invalidate)
512        // and we have a copy of the block already. Since there
513        // is no invalidation, we can promote targets that don't
514        // require a writable copy
515        mshr->promoteReadable();
516    }
517
518    if (blk && blk->isWritable() && !pkt->req->isCacheInvalidate()) {
519        // If at this point the referenced block is writable and the
520        // response is not a cache invalidate, we promote targets that
521        // were deferred as we couldn't guarrantee a writable copy
522        mshr->promoteWritable();
523    }
524
525    serviceMSHRTargets(mshr, pkt, blk, writebacks);
526
527    if (mshr->promoteDeferredTargets()) {
528        // avoid later read getting stale data while write miss is
529        // outstanding.. see comment in timingAccess()
530        if (blk) {
531            blk->status &= ~BlkReadable;
532        }
533        mshrQueue.markPending(mshr);
534        schedMemSideSendEvent(clockEdge() + pkt->payloadDelay);
535    } else {
536        // while we deallocate an mshr from the queue we still have to
537        // check the isFull condition before and after as we might
538        // have been using the reserved entries already
539        const bool was_full = mshrQueue.isFull();
540        mshrQueue.deallocate(mshr);
541        if (was_full && !mshrQueue.isFull()) {
542            clearBlocked(Blocked_NoMSHRs);
543        }
544
545        // Request the bus for a prefetch if this deallocation freed enough
546        // MSHRs for a prefetch to take place
547        if (prefetcher && mshrQueue.canPrefetch()) {
548            Tick next_pf_time = std::max(prefetcher->nextPrefetchReadyTime(),
549                                         clockEdge());
550            if (next_pf_time != MaxTick)
551                schedMemSideSendEvent(next_pf_time);
552        }
553    }
554
555    // if we used temp block, check to see if its valid and then clear it out
556    if (blk == tempBlock && tempBlock->isValid()) {
557        evictBlock(blk, writebacks);
558    }
559
560    const Tick forward_time = clockEdge(forwardLatency) + pkt->headerDelay;
561    // copy writebacks to write buffer
562    doWritebacks(writebacks, forward_time);
563
564    DPRINTF(CacheVerbose, "%s: Leaving with %s\n", __func__, pkt->print());
565    delete pkt;
566}
567
568
569Tick
570BaseCache::recvAtomic(PacketPtr pkt)
571{
572    // We are in atomic mode so we pay just for lookupLatency here.
573    Cycles lat = lookupLatency;
574
575    // follow the same flow as in recvTimingReq, and check if a cache
576    // above us is responding
577    if (pkt->cacheResponding() && !pkt->isClean()) {
578        assert(!pkt->req->isCacheInvalidate());
579        DPRINTF(Cache, "Cache above responding to %s: not responding\n",
580                pkt->print());
581
582        // if a cache is responding, and it had the line in Owned
583        // rather than Modified state, we need to invalidate any
584        // copies that are not on the same path to memory
585        assert(pkt->needsWritable() && !pkt->responderHadWritable());
586        lat += ticksToCycles(memSidePort.sendAtomic(pkt));
587
588        return lat * clockPeriod();
589    }
590
591    // should assert here that there are no outstanding MSHRs or
592    // writebacks... that would mean that someone used an atomic
593    // access in timing mode
594
595    CacheBlk *blk = nullptr;
596    PacketList writebacks;
597    bool satisfied = access(pkt, blk, lat, writebacks);
598
599    if (pkt->isClean() && blk && blk->isDirty()) {
600        // A cache clean opearation is looking for a dirty
601        // block. If a dirty block is encountered a WriteClean
602        // will update any copies to the path to the memory
603        // until the point of reference.
604        DPRINTF(CacheVerbose, "%s: packet %s found block: %s\n",
605                __func__, pkt->print(), blk->print());
606        PacketPtr wb_pkt = writecleanBlk(blk, pkt->req->getDest(), pkt->id);
607        writebacks.push_back(wb_pkt);
608        pkt->setSatisfied();
609    }
610
611    // handle writebacks resulting from the access here to ensure they
612    // logically precede anything happening below
613    doWritebacksAtomic(writebacks);
614    assert(writebacks.empty());
615
616    if (!satisfied) {
617        lat += handleAtomicReqMiss(pkt, blk, writebacks);
618    }
619
620    // Note that we don't invoke the prefetcher at all in atomic mode.
621    // It's not clear how to do it properly, particularly for
622    // prefetchers that aggressively generate prefetch candidates and
623    // rely on bandwidth contention to throttle them; these will tend
624    // to pollute the cache in atomic mode since there is no bandwidth
625    // contention.  If we ever do want to enable prefetching in atomic
626    // mode, though, this is the place to do it... see timingAccess()
627    // for an example (though we'd want to issue the prefetch(es)
628    // immediately rather than calling requestMemSideBus() as we do
629    // there).
630
631    // do any writebacks resulting from the response handling
632    doWritebacksAtomic(writebacks);
633
634    // if we used temp block, check to see if its valid and if so
635    // clear it out, but only do so after the call to recvAtomic is
636    // finished so that any downstream observers (such as a snoop
637    // filter), first see the fill, and only then see the eviction
638    if (blk == tempBlock && tempBlock->isValid()) {
639        // the atomic CPU calls recvAtomic for fetch and load/store
640        // sequentuially, and we may already have a tempBlock
641        // writeback from the fetch that we have not yet sent
642        if (tempBlockWriteback) {
643            // if that is the case, write the prevoius one back, and
644            // do not schedule any new event
645            writebackTempBlockAtomic();
646        } else {
647            // the writeback/clean eviction happens after the call to
648            // recvAtomic has finished (but before any successive
649            // calls), so that the response handling from the fill is
650            // allowed to happen first
651            schedule(writebackTempBlockAtomicEvent, curTick());
652        }
653
654        tempBlockWriteback = evictBlock(blk);
655    }
656
657    if (pkt->needsResponse()) {
658        pkt->makeAtomicResponse();
659    }
660
661    return lat * clockPeriod();
662}
663
664void
665BaseCache::functionalAccess(PacketPtr pkt, bool from_cpu_side)
666{
667    Addr blk_addr = pkt->getBlockAddr(blkSize);
668    bool is_secure = pkt->isSecure();
669    CacheBlk *blk = tags->findBlock(pkt->getAddr(), is_secure);
670    MSHR *mshr = mshrQueue.findMatch(blk_addr, is_secure);
671
672    pkt->pushLabel(name());
673
674    CacheBlkPrintWrapper cbpw(blk);
675
676    // Note that just because an L2/L3 has valid data doesn't mean an
677    // L1 doesn't have a more up-to-date modified copy that still
678    // needs to be found.  As a result we always update the request if
679    // we have it, but only declare it satisfied if we are the owner.
680
681    // see if we have data at all (owned or otherwise)
682    bool have_data = blk && blk->isValid()
683        && pkt->trySatisfyFunctional(&cbpw, blk_addr, is_secure, blkSize,
684                                     blk->data);
685
686    // data we have is dirty if marked as such or if we have an
687    // in-service MSHR that is pending a modified line
688    bool have_dirty =
689        have_data && (blk->isDirty() ||
690                      (mshr && mshr->inService && mshr->isPendingModified()));
691
692    bool done = have_dirty ||
693        cpuSidePort.trySatisfyFunctional(pkt) ||
694        mshrQueue.trySatisfyFunctional(pkt, blk_addr) ||
695        writeBuffer.trySatisfyFunctional(pkt, blk_addr) ||
696        memSidePort.trySatisfyFunctional(pkt);
697
698    DPRINTF(CacheVerbose, "%s: %s %s%s%s\n", __func__,  pkt->print(),
699            (blk && blk->isValid()) ? "valid " : "",
700            have_data ? "data " : "", done ? "done " : "");
701
702    // We're leaving the cache, so pop cache->name() label
703    pkt->popLabel();
704
705    if (done) {
706        pkt->makeResponse();
707    } else {
708        // if it came as a request from the CPU side then make sure it
709        // continues towards the memory side
710        if (from_cpu_side) {
711            memSidePort.sendFunctional(pkt);
712        } else if (cpuSidePort.isSnooping()) {
713            // if it came from the memory side, it must be a snoop request
714            // and we should only forward it if we are forwarding snoops
715            cpuSidePort.sendFunctionalSnoop(pkt);
716        }
717    }
718}
719
720
721void
722BaseCache::cmpAndSwap(CacheBlk *blk, PacketPtr pkt)
723{
724    assert(pkt->isRequest());
725
726    uint64_t overwrite_val;
727    bool overwrite_mem;
728    uint64_t condition_val64;
729    uint32_t condition_val32;
730
731    int offset = pkt->getOffset(blkSize);
732    uint8_t *blk_data = blk->data + offset;
733
734    assert(sizeof(uint64_t) >= pkt->getSize());
735
736    overwrite_mem = true;
737    // keep a copy of our possible write value, and copy what is at the
738    // memory address into the packet
739    pkt->writeData((uint8_t *)&overwrite_val);
740    pkt->setData(blk_data);
741
742    if (pkt->req->isCondSwap()) {
743        if (pkt->getSize() == sizeof(uint64_t)) {
744            condition_val64 = pkt->req->getExtraData();
745            overwrite_mem = !std::memcmp(&condition_val64, blk_data,
746                                         sizeof(uint64_t));
747        } else if (pkt->getSize() == sizeof(uint32_t)) {
748            condition_val32 = (uint32_t)pkt->req->getExtraData();
749            overwrite_mem = !std::memcmp(&condition_val32, blk_data,
750                                         sizeof(uint32_t));
751        } else
752            panic("Invalid size for conditional read/write\n");
753    }
754
755    if (overwrite_mem) {
756        std::memcpy(blk_data, &overwrite_val, pkt->getSize());
757        blk->status |= BlkDirty;
758    }
759}
760
761QueueEntry*
762BaseCache::getNextQueueEntry()
763{
764    // Check both MSHR queue and write buffer for potential requests,
765    // note that null does not mean there is no request, it could
766    // simply be that it is not ready
767    MSHR *miss_mshr  = mshrQueue.getNext();
768    WriteQueueEntry *wq_entry = writeBuffer.getNext();
769
770    // If we got a write buffer request ready, first priority is a
771    // full write buffer, otherwise we favour the miss requests
772    if (wq_entry && (writeBuffer.isFull() || !miss_mshr)) {
773        // need to search MSHR queue for conflicting earlier miss.
774        MSHR *conflict_mshr =
775            mshrQueue.findPending(wq_entry->blkAddr,
776                                  wq_entry->isSecure);
777
778        if (conflict_mshr && conflict_mshr->order < wq_entry->order) {
779            // Service misses in order until conflict is cleared.
780            return conflict_mshr;
781
782            // @todo Note that we ignore the ready time of the conflict here
783        }
784
785        // No conflicts; issue write
786        return wq_entry;
787    } else if (miss_mshr) {
788        // need to check for conflicting earlier writeback
789        WriteQueueEntry *conflict_mshr =
790            writeBuffer.findPending(miss_mshr->blkAddr,
791                                    miss_mshr->isSecure);
792        if (conflict_mshr) {
793            // not sure why we don't check order here... it was in the
794            // original code but commented out.
795
796            // The only way this happens is if we are
797            // doing a write and we didn't have permissions
798            // then subsequently saw a writeback (owned got evicted)
799            // We need to make sure to perform the writeback first
800            // To preserve the dirty data, then we can issue the write
801
802            // should we return wq_entry here instead?  I.e. do we
803            // have to flush writes in order?  I don't think so... not
804            // for Alpha anyway.  Maybe for x86?
805            return conflict_mshr;
806
807            // @todo Note that we ignore the ready time of the conflict here
808        }
809
810        // No conflicts; issue read
811        return miss_mshr;
812    }
813
814    // fall through... no pending requests.  Try a prefetch.
815    assert(!miss_mshr && !wq_entry);
816    if (prefetcher && mshrQueue.canPrefetch()) {
817        // If we have a miss queue slot, we can try a prefetch
818        PacketPtr pkt = prefetcher->getPacket();
819        if (pkt) {
820            Addr pf_addr = pkt->getBlockAddr(blkSize);
821            if (!tags->findBlock(pf_addr, pkt->isSecure()) &&
822                !mshrQueue.findMatch(pf_addr, pkt->isSecure()) &&
823                !writeBuffer.findMatch(pf_addr, pkt->isSecure())) {
824                // Update statistic on number of prefetches issued
825                // (hwpf_mshr_misses)
826                assert(pkt->req->masterId() < system->maxMasters());
827                mshr_misses[pkt->cmdToIndex()][pkt->req->masterId()]++;
828
829                // allocate an MSHR and return it, note
830                // that we send the packet straight away, so do not
831                // schedule the send
832                return allocateMissBuffer(pkt, curTick(), false);
833            } else {
834                // free the request and packet
835                delete pkt;
836            }
837        }
838    }
839
840    return nullptr;
841}
842
843void
844BaseCache::satisfyRequest(PacketPtr pkt, CacheBlk *blk, bool, bool)
845{
846    assert(pkt->isRequest());
847
848    assert(blk && blk->isValid());
849    // Occasionally this is not true... if we are a lower-level cache
850    // satisfying a string of Read and ReadEx requests from
851    // upper-level caches, a Read will mark the block as shared but we
852    // can satisfy a following ReadEx anyway since we can rely on the
853    // Read requester(s) to have buffered the ReadEx snoop and to
854    // invalidate their blocks after receiving them.
855    // assert(!pkt->needsWritable() || blk->isWritable());
856    assert(pkt->getOffset(blkSize) + pkt->getSize() <= blkSize);
857
858    // Check RMW operations first since both isRead() and
859    // isWrite() will be true for them
860    if (pkt->cmd == MemCmd::SwapReq) {
861        if (pkt->isAtomicOp()) {
862            // extract data from cache and save it into the data field in
863            // the packet as a return value from this atomic op
864
865            int offset = tags->extractBlkOffset(pkt->getAddr());
866            uint8_t *blk_data = blk->data + offset;
867            std::memcpy(pkt->getPtr<uint8_t>(), blk_data, pkt->getSize());
868
869            // execute AMO operation
870            (*(pkt->getAtomicOp()))(blk_data);
871
872            // set block status to dirty
873            blk->status |= BlkDirty;
874        } else {
875            cmpAndSwap(blk, pkt);
876        }
877    } else if (pkt->isWrite()) {
878        // we have the block in a writable state and can go ahead,
879        // note that the line may be also be considered writable in
880        // downstream caches along the path to memory, but always
881        // Exclusive, and never Modified
882        assert(blk->isWritable());
883        // Write or WriteLine at the first cache with block in writable state
884        if (blk->checkWrite(pkt)) {
885            pkt->writeDataToBlock(blk->data, blkSize);
886        }
887        // Always mark the line as dirty (and thus transition to the
888        // Modified state) even if we are a failed StoreCond so we
889        // supply data to any snoops that have appended themselves to
890        // this cache before knowing the store will fail.
891        blk->status |= BlkDirty;
892        DPRINTF(CacheVerbose, "%s for %s (write)\n", __func__, pkt->print());
893    } else if (pkt->isRead()) {
894        if (pkt->isLLSC()) {
895            blk->trackLoadLocked(pkt);
896        }
897
898        // all read responses have a data payload
899        assert(pkt->hasRespData());
900        pkt->setDataFromBlock(blk->data, blkSize);
901    } else if (pkt->isUpgrade()) {
902        // sanity check
903        assert(!pkt->hasSharers());
904
905        if (blk->isDirty()) {
906            // we were in the Owned state, and a cache above us that
907            // has the line in Shared state needs to be made aware
908            // that the data it already has is in fact dirty
909            pkt->setCacheResponding();
910            blk->status &= ~BlkDirty;
911        }
912    } else if (pkt->isClean()) {
913        blk->status &= ~BlkDirty;
914    } else {
915        assert(pkt->isInvalidate());
916        invalidateBlock(blk);
917        DPRINTF(CacheVerbose, "%s for %s (invalidation)\n", __func__,
918                pkt->print());
919    }
920}
921
922/////////////////////////////////////////////////////
923//
924// Access path: requests coming in from the CPU side
925//
926/////////////////////////////////////////////////////
927
928bool
929BaseCache::access(PacketPtr pkt, CacheBlk *&blk, Cycles &lat,
930                  PacketList &writebacks)
931{
932    // sanity check
933    assert(pkt->isRequest());
934
935    chatty_assert(!(isReadOnly && pkt->isWrite()),
936                  "Should never see a write in a read-only cache %s\n",
937                  name());
938
939    // Here lat is the value passed as parameter to accessBlock() function
940    // that can modify its value.
941    blk = tags->accessBlock(pkt->getAddr(), pkt->isSecure(), lat);
942
943    DPRINTF(Cache, "%s for %s %s\n", __func__, pkt->print(),
944            blk ? "hit " + blk->print() : "miss");
945
946    if (pkt->req->isCacheMaintenance()) {
947        // A cache maintenance operation is always forwarded to the
948        // memory below even if the block is found in dirty state.
949
950        // We defer any changes to the state of the block until we
951        // create and mark as in service the mshr for the downstream
952        // packet.
953        return false;
954    }
955
956    if (pkt->isEviction()) {
957        // We check for presence of block in above caches before issuing
958        // Writeback or CleanEvict to write buffer. Therefore the only
959        // possible cases can be of a CleanEvict packet coming from above
960        // encountering a Writeback generated in this cache peer cache and
961        // waiting in the write buffer. Cases of upper level peer caches
962        // generating CleanEvict and Writeback or simply CleanEvict and
963        // CleanEvict almost simultaneously will be caught by snoops sent out
964        // by crossbar.
965        WriteQueueEntry *wb_entry = writeBuffer.findMatch(pkt->getAddr(),
966                                                          pkt->isSecure());
967        if (wb_entry) {
968            assert(wb_entry->getNumTargets() == 1);
969            PacketPtr wbPkt = wb_entry->getTarget()->pkt;
970            assert(wbPkt->isWriteback());
971
972            if (pkt->isCleanEviction()) {
973                // The CleanEvict and WritebackClean snoops into other
974                // peer caches of the same level while traversing the
975                // crossbar. If a copy of the block is found, the
976                // packet is deleted in the crossbar. Hence, none of
977                // the other upper level caches connected to this
978                // cache have the block, so we can clear the
979                // BLOCK_CACHED flag in the Writeback if set and
980                // discard the CleanEvict by returning true.
981                wbPkt->clearBlockCached();
982                return true;
983            } else {
984                assert(pkt->cmd == MemCmd::WritebackDirty);
985                // Dirty writeback from above trumps our clean
986                // writeback... discard here
987                // Note: markInService will remove entry from writeback buffer.
988                markInService(wb_entry);
989                delete wbPkt;
990            }
991        }
992    }
993
994    // Writeback handling is special case.  We can write the block into
995    // the cache without having a writeable copy (or any copy at all).
996    if (pkt->isWriteback()) {
997        assert(blkSize == pkt->getSize());
998
999        // we could get a clean writeback while we are having
1000        // outstanding accesses to a block, do the simple thing for
1001        // now and drop the clean writeback so that we do not upset
1002        // any ordering/decisions about ownership already taken
1003        if (pkt->cmd == MemCmd::WritebackClean &&
1004            mshrQueue.findMatch(pkt->getAddr(), pkt->isSecure())) {
1005            DPRINTF(Cache, "Clean writeback %#llx to block with MSHR, "
1006                    "dropping\n", pkt->getAddr());
1007            return true;
1008        }
1009
1010        if (!blk) {
1011            // need to do a replacement
1012            blk = allocateBlock(pkt, writebacks);
1013            if (!blk) {
1014                // no replaceable block available: give up, fwd to next level.
1015                incMissCount(pkt);
1016                return false;
1017            }
1018
1019            blk->status |= (BlkValid | BlkReadable);
1020        }
1021        // only mark the block dirty if we got a writeback command,
1022        // and leave it as is for a clean writeback
1023        if (pkt->cmd == MemCmd::WritebackDirty) {
1024            // TODO: the coherent cache can assert(!blk->isDirty());
1025            blk->status |= BlkDirty;
1026        }
1027        // if the packet does not have sharers, it is passing
1028        // writable, and we got the writeback in Modified or Exclusive
1029        // state, if not we are in the Owned or Shared state
1030        if (!pkt->hasSharers()) {
1031            blk->status |= BlkWritable;
1032        }
1033        // nothing else to do; writeback doesn't expect response
1034        assert(!pkt->needsResponse());
1035        pkt->writeDataToBlock(blk->data, blkSize);
1036        DPRINTF(Cache, "%s new state is %s\n", __func__, blk->print());
1037        incHitCount(pkt);
1038        // populate the time when the block will be ready to access.
1039        blk->whenReady = clockEdge(fillLatency) + pkt->headerDelay +
1040            pkt->payloadDelay;
1041        return true;
1042    } else if (pkt->cmd == MemCmd::CleanEvict) {
1043        if (blk) {
1044            // Found the block in the tags, need to stop CleanEvict from
1045            // propagating further down the hierarchy. Returning true will
1046            // treat the CleanEvict like a satisfied write request and delete
1047            // it.
1048            return true;
1049        }
1050        // We didn't find the block here, propagate the CleanEvict further
1051        // down the memory hierarchy. Returning false will treat the CleanEvict
1052        // like a Writeback which could not find a replaceable block so has to
1053        // go to next level.
1054        return false;
1055    } else if (pkt->cmd == MemCmd::WriteClean) {
1056        // WriteClean handling is a special case. We can allocate a
1057        // block directly if it doesn't exist and we can update the
1058        // block immediately. The WriteClean transfers the ownership
1059        // of the block as well.
1060        assert(blkSize == pkt->getSize());
1061
1062        if (!blk) {
1063            if (pkt->writeThrough()) {
1064                // if this is a write through packet, we don't try to
1065                // allocate if the block is not present
1066                return false;
1067            } else {
1068                // a writeback that misses needs to allocate a new block
1069                blk = allocateBlock(pkt, writebacks);
1070                if (!blk) {
1071                    // no replaceable block available: give up, fwd to
1072                    // next level.
1073                    incMissCount(pkt);
1074                    return false;
1075                }
1076
1077                blk->status |= (BlkValid | BlkReadable);
1078            }
1079        }
1080
1081        // at this point either this is a writeback or a write-through
1082        // write clean operation and the block is already in this
1083        // cache, we need to update the data and the block flags
1084        assert(blk);
1085        // TODO: the coherent cache can assert(!blk->isDirty());
1086        if (!pkt->writeThrough()) {
1087            blk->status |= BlkDirty;
1088        }
1089        // nothing else to do; writeback doesn't expect response
1090        assert(!pkt->needsResponse());
1091        pkt->writeDataToBlock(blk->data, blkSize);
1092        DPRINTF(Cache, "%s new state is %s\n", __func__, blk->print());
1093
1094        incHitCount(pkt);
1095        // populate the time when the block will be ready to access.
1096        blk->whenReady = clockEdge(fillLatency) + pkt->headerDelay +
1097            pkt->payloadDelay;
1098        // if this a write-through packet it will be sent to cache
1099        // below
1100        return !pkt->writeThrough();
1101    } else if (blk && (pkt->needsWritable() ? blk->isWritable() :
1102                       blk->isReadable())) {
1103        // OK to satisfy access
1104        incHitCount(pkt);
1105        satisfyRequest(pkt, blk);
1106        maintainClusivity(pkt->fromCache(), blk);
1107
1108        return true;
1109    }
1110
1111    // Can't satisfy access normally... either no block (blk == nullptr)
1112    // or have block but need writable
1113
1114    incMissCount(pkt);
1115
1116    if (!blk && pkt->isLLSC() && pkt->isWrite()) {
1117        // complete miss on store conditional... just give up now
1118        pkt->req->setExtraData(0);
1119        return true;
1120    }
1121
1122    return false;
1123}
1124
1125void
1126BaseCache::maintainClusivity(bool from_cache, CacheBlk *blk)
1127{
1128    if (from_cache && blk && blk->isValid() && !blk->isDirty() &&
1129        clusivity == Enums::mostly_excl) {
1130        // if we have responded to a cache, and our block is still
1131        // valid, but not dirty, and this cache is mostly exclusive
1132        // with respect to the cache above, drop the block
1133        invalidateBlock(blk);
1134    }
1135}
1136
1137CacheBlk*
1138BaseCache::handleFill(PacketPtr pkt, CacheBlk *blk, PacketList &writebacks,
1139                      bool allocate)
1140{
1141    assert(pkt->isResponse());
1142    Addr addr = pkt->getAddr();
1143    bool is_secure = pkt->isSecure();
1144#if TRACING_ON
1145    CacheBlk::State old_state = blk ? blk->status : 0;
1146#endif
1147
1148    // When handling a fill, we should have no writes to this line.
1149    assert(addr == pkt->getBlockAddr(blkSize));
1150    assert(!writeBuffer.findMatch(addr, is_secure));
1151
1152    if (!blk) {
1153        // better have read new data...
1154        assert(pkt->hasData() || pkt->cmd == MemCmd::InvalidateResp);
1155
1156        // need to do a replacement if allocating, otherwise we stick
1157        // with the temporary storage
1158        blk = allocate ? allocateBlock(pkt, writebacks) : nullptr;
1159
1160        if (!blk) {
1161            // No replaceable block or a mostly exclusive
1162            // cache... just use temporary storage to complete the
1163            // current request and then get rid of it
1164            assert(!tempBlock->isValid());
1165            blk = tempBlock;
1166            tempBlock->insert(addr, is_secure);
1167            DPRINTF(Cache, "using temp block for %#llx (%s)\n", addr,
1168                    is_secure ? "s" : "ns");
1169        }
1170
1171        // we should never be overwriting a valid block
1172        assert(!blk->isValid());
1173    } else {
1174        // existing block... probably an upgrade
1175        assert(regenerateBlkAddr(blk) == addr);
1176        assert(blk->isSecure() == is_secure);
1177        // either we're getting new data or the block should already be valid
1178        assert(pkt->hasData() || blk->isValid());
1179        // don't clear block status... if block is already dirty we
1180        // don't want to lose that
1181    }
1182
1183    blk->status |= BlkValid | BlkReadable;
1184
1185    // sanity check for whole-line writes, which should always be
1186    // marked as writable as part of the fill, and then later marked
1187    // dirty as part of satisfyRequest
1188    if (pkt->cmd == MemCmd::InvalidateResp) {
1189        assert(!pkt->hasSharers());
1190    }
1191
1192    // here we deal with setting the appropriate state of the line,
1193    // and we start by looking at the hasSharers flag, and ignore the
1194    // cacheResponding flag (normally signalling dirty data) if the
1195    // packet has sharers, thus the line is never allocated as Owned
1196    // (dirty but not writable), and always ends up being either
1197    // Shared, Exclusive or Modified, see Packet::setCacheResponding
1198    // for more details
1199    if (!pkt->hasSharers()) {
1200        // we could get a writable line from memory (rather than a
1201        // cache) even in a read-only cache, note that we set this bit
1202        // even for a read-only cache, possibly revisit this decision
1203        blk->status |= BlkWritable;
1204
1205        // check if we got this via cache-to-cache transfer (i.e., from a
1206        // cache that had the block in Modified or Owned state)
1207        if (pkt->cacheResponding()) {
1208            // we got the block in Modified state, and invalidated the
1209            // owners copy
1210            blk->status |= BlkDirty;
1211
1212            chatty_assert(!isReadOnly, "Should never see dirty snoop response "
1213                          "in read-only cache %s\n", name());
1214        }
1215    }
1216
1217    DPRINTF(Cache, "Block addr %#llx (%s) moving from state %x to %s\n",
1218            addr, is_secure ? "s" : "ns", old_state, blk->print());
1219
1220    // if we got new data, copy it in (checking for a read response
1221    // and a response that has data is the same in the end)
1222    if (pkt->isRead()) {
1223        // sanity checks
1224        assert(pkt->hasData());
1225        assert(pkt->getSize() == blkSize);
1226
1227        pkt->writeDataToBlock(blk->data, blkSize);
1228    }
1229    // We pay for fillLatency here.
1230    blk->whenReady = clockEdge() + fillLatency * clockPeriod() +
1231        pkt->payloadDelay;
1232
1233    return blk;
1234}
1235
1236CacheBlk*
1237BaseCache::allocateBlock(const PacketPtr pkt, PacketList &writebacks)
1238{
1239    // Get address
1240    const Addr addr = pkt->getAddr();
1241
1242    // Get secure bit
1243    const bool is_secure = pkt->isSecure();
1244
1245    // Find replacement victim
1246    std::vector<CacheBlk*> evict_blks;
1247    CacheBlk *victim = tags->findVictim(addr, is_secure, evict_blks);
1248
1249    // It is valid to return nullptr if there is no victim
1250    if (!victim)
1251        return nullptr;
1252
1253    // Print victim block's information
1254    DPRINTF(CacheRepl, "Replacement victim: %s\n", victim->print());
1255
1256    // Check for transient state allocations. If any of the entries listed
1257    // for eviction has a transient state, the allocation fails
1258    for (const auto& blk : evict_blks) {
1259        if (blk->isValid()) {
1260            Addr repl_addr = regenerateBlkAddr(blk);
1261            MSHR *repl_mshr = mshrQueue.findMatch(repl_addr, blk->isSecure());
1262            if (repl_mshr) {
1263                // must be an outstanding upgrade or clean request
1264                // on a block we're about to replace...
1265                assert((!blk->isWritable() && repl_mshr->needsWritable()) ||
1266                       repl_mshr->isCleaning());
1267
1268                // too hard to replace block with transient state
1269                // allocation failed, block not inserted
1270                return nullptr;
1271            }
1272        }
1273    }
1274
1275    // The victim will be replaced by a new entry, so increase the replacement
1276    // counter if a valid block is being replaced
1277    if (victim->isValid()) {
1278        DPRINTF(Cache, "replacement: replacing %#llx (%s) with %#llx "
1279                "(%s): %s\n", regenerateBlkAddr(victim),
1280                victim->isSecure() ? "s" : "ns",
1281                addr, is_secure ? "s" : "ns",
1282                victim->isDirty() ? "writeback" : "clean");
1283
1284        replacements++;
1285    }
1286
1287    // Evict valid blocks associated to this victim block
1288    for (const auto& blk : evict_blks) {
1289        if (blk->isValid()) {
1290            if (blk->wasPrefetched()) {
1291                unusedPrefetches++;
1292            }
1293
1294            evictBlock(blk, writebacks);
1295        }
1296    }
1297
1298    // Insert new block at victimized entry
1299    tags->insertBlock(addr, is_secure, pkt->req->masterId(),
1300                      pkt->req->taskId(), victim);
1301
1302    return victim;
1303}
1304
1305void
1306BaseCache::invalidateBlock(CacheBlk *blk)
1307{
1308    if (blk != tempBlock)
1309        tags->invalidate(blk);
1310    blk->invalidate();
1311}
1312
1313PacketPtr
1314BaseCache::writebackBlk(CacheBlk *blk)
1315{
1316    chatty_assert(!isReadOnly || writebackClean,
1317                  "Writeback from read-only cache");
1318    assert(blk && blk->isValid() && (blk->isDirty() || writebackClean));
1319
1320    writebacks[Request::wbMasterId]++;
1321
1322    RequestPtr req = std::make_shared<Request>(
1323        regenerateBlkAddr(blk), blkSize, 0, Request::wbMasterId);
1324
1325    if (blk->isSecure())
1326        req->setFlags(Request::SECURE);
1327
1328    req->taskId(blk->task_id);
1329
1330    PacketPtr pkt =
1331        new Packet(req, blk->isDirty() ?
1332                   MemCmd::WritebackDirty : MemCmd::WritebackClean);
1333
1334    DPRINTF(Cache, "Create Writeback %s writable: %d, dirty: %d\n",
1335            pkt->print(), blk->isWritable(), blk->isDirty());
1336
1337    if (blk->isWritable()) {
1338        // not asserting shared means we pass the block in modified
1339        // state, mark our own block non-writeable
1340        blk->status &= ~BlkWritable;
1341    } else {
1342        // we are in the Owned state, tell the receiver
1343        pkt->setHasSharers();
1344    }
1345
1346    // make sure the block is not marked dirty
1347    blk->status &= ~BlkDirty;
1348
1349    pkt->allocate();
1350    pkt->setDataFromBlock(blk->data, blkSize);
1351
1352    return pkt;
1353}
1354
1355PacketPtr
1356BaseCache::writecleanBlk(CacheBlk *blk, Request::Flags dest, PacketId id)
1357{
1358    RequestPtr req = std::make_shared<Request>(
1359        regenerateBlkAddr(blk), blkSize, 0, Request::wbMasterId);
1360
1361    if (blk->isSecure()) {
1362        req->setFlags(Request::SECURE);
1363    }
1364    req->taskId(blk->task_id);
1365
1366    PacketPtr pkt = new Packet(req, MemCmd::WriteClean, blkSize, id);
1367
1368    if (dest) {
1369        req->setFlags(dest);
1370        pkt->setWriteThrough();
1371    }
1372
1373    DPRINTF(Cache, "Create %s writable: %d, dirty: %d\n", pkt->print(),
1374            blk->isWritable(), blk->isDirty());
1375
1376    if (blk->isWritable()) {
1377        // not asserting shared means we pass the block in modified
1378        // state, mark our own block non-writeable
1379        blk->status &= ~BlkWritable;
1380    } else {
1381        // we are in the Owned state, tell the receiver
1382        pkt->setHasSharers();
1383    }
1384
1385    // make sure the block is not marked dirty
1386    blk->status &= ~BlkDirty;
1387
1388    pkt->allocate();
1389    pkt->setDataFromBlock(blk->data, blkSize);
1390
1391    return pkt;
1392}
1393
1394
1395void
1396BaseCache::memWriteback()
1397{
1398    tags->forEachBlk([this](CacheBlk &blk) { writebackVisitor(blk); });
1399}
1400
1401void
1402BaseCache::memInvalidate()
1403{
1404    tags->forEachBlk([this](CacheBlk &blk) { invalidateVisitor(blk); });
1405}
1406
1407bool
1408BaseCache::isDirty() const
1409{
1410    return tags->anyBlk([](CacheBlk &blk) { return blk.isDirty(); });
1411}
1412
1413void
1414BaseCache::writebackVisitor(CacheBlk &blk)
1415{
1416    if (blk.isDirty()) {
1417        assert(blk.isValid());
1418
1419        RequestPtr request = std::make_shared<Request>(
1420            regenerateBlkAddr(&blk), blkSize, 0, Request::funcMasterId);
1421
1422        request->taskId(blk.task_id);
1423        if (blk.isSecure()) {
1424            request->setFlags(Request::SECURE);
1425        }
1426
1427        Packet packet(request, MemCmd::WriteReq);
1428        packet.dataStatic(blk.data);
1429
1430        memSidePort.sendFunctional(&packet);
1431
1432        blk.status &= ~BlkDirty;
1433    }
1434}
1435
1436void
1437BaseCache::invalidateVisitor(CacheBlk &blk)
1438{
1439    if (blk.isDirty())
1440        warn_once("Invalidating dirty cache lines. " \
1441                  "Expect things to break.\n");
1442
1443    if (blk.isValid()) {
1444        assert(!blk.isDirty());
1445        invalidateBlock(&blk);
1446    }
1447}
1448
1449Tick
1450BaseCache::nextQueueReadyTime() const
1451{
1452    Tick nextReady = std::min(mshrQueue.nextReadyTime(),
1453                              writeBuffer.nextReadyTime());
1454
1455    // Don't signal prefetch ready time if no MSHRs available
1456    // Will signal once enoguh MSHRs are deallocated
1457    if (prefetcher && mshrQueue.canPrefetch()) {
1458        nextReady = std::min(nextReady,
1459                             prefetcher->nextPrefetchReadyTime());
1460    }
1461
1462    return nextReady;
1463}
1464
1465
1466bool
1467BaseCache::sendMSHRQueuePacket(MSHR* mshr)
1468{
1469    assert(mshr);
1470
1471    // use request from 1st target
1472    PacketPtr tgt_pkt = mshr->getTarget()->pkt;
1473
1474    DPRINTF(Cache, "%s: MSHR %s\n", __func__, tgt_pkt->print());
1475
1476    // if the cache is in write coalescing mode or (additionally) in
1477    // no allocation mode, and we have a write packet with an MSHR
1478    // that is not a whole-line write (due to incompatible flags etc),
1479    // then reset the write mode
1480    if (writeAllocator && writeAllocator->coalesce() && tgt_pkt->isWrite()) {
1481        if (!mshr->isWholeLineWrite()) {
1482            // if we are currently write coalescing, hold on the
1483            // MSHR as many cycles extra as we need to completely
1484            // write a cache line
1485            if (writeAllocator->delay(mshr->blkAddr)) {
1486                Tick delay = blkSize / tgt_pkt->getSize() * clockPeriod();
1487                DPRINTF(CacheVerbose, "Delaying pkt %s %llu ticks to allow "
1488                        "for write coalescing\n", tgt_pkt->print(), delay);
1489                mshrQueue.delay(mshr, delay);
1490                return false;
1491            } else {
1492                writeAllocator->reset();
1493            }
1494        } else {
1495            writeAllocator->resetDelay(mshr->blkAddr);
1496        }
1497    }
1498
1499    CacheBlk *blk = tags->findBlock(mshr->blkAddr, mshr->isSecure);
1500
1501    // either a prefetch that is not present upstream, or a normal
1502    // MSHR request, proceed to get the packet to send downstream
1503    PacketPtr pkt = createMissPacket(tgt_pkt, blk, mshr->needsWritable(),
1504                                     mshr->isWholeLineWrite());
1505
1506    mshr->isForward = (pkt == nullptr);
1507
1508    if (mshr->isForward) {
1509        // not a cache block request, but a response is expected
1510        // make copy of current packet to forward, keep current
1511        // copy for response handling
1512        pkt = new Packet(tgt_pkt, false, true);
1513        assert(!pkt->isWrite());
1514    }
1515
1516    // play it safe and append (rather than set) the sender state,
1517    // as forwarded packets may already have existing state
1518    pkt->pushSenderState(mshr);
1519
1520    if (pkt->isClean() && blk && blk->isDirty()) {
1521        // A cache clean opearation is looking for a dirty block. Mark
1522        // the packet so that the destination xbar can determine that
1523        // there will be a follow-up write packet as well.
1524        pkt->setSatisfied();
1525    }
1526
1527    if (!memSidePort.sendTimingReq(pkt)) {
1528        // we are awaiting a retry, but we
1529        // delete the packet and will be creating a new packet
1530        // when we get the opportunity
1531        delete pkt;
1532
1533        // note that we have now masked any requestBus and
1534        // schedSendEvent (we will wait for a retry before
1535        // doing anything), and this is so even if we do not
1536        // care about this packet and might override it before
1537        // it gets retried
1538        return true;
1539    } else {
1540        // As part of the call to sendTimingReq the packet is
1541        // forwarded to all neighbouring caches (and any caches
1542        // above them) as a snoop. Thus at this point we know if
1543        // any of the neighbouring caches are responding, and if
1544        // so, we know it is dirty, and we can determine if it is
1545        // being passed as Modified, making our MSHR the ordering
1546        // point
1547        bool pending_modified_resp = !pkt->hasSharers() &&
1548            pkt->cacheResponding();
1549        markInService(mshr, pending_modified_resp);
1550
1551        if (pkt->isClean() && blk && blk->isDirty()) {
1552            // A cache clean opearation is looking for a dirty
1553            // block. If a dirty block is encountered a WriteClean
1554            // will update any copies to the path to the memory
1555            // until the point of reference.
1556            DPRINTF(CacheVerbose, "%s: packet %s found block: %s\n",
1557                    __func__, pkt->print(), blk->print());
1558            PacketPtr wb_pkt = writecleanBlk(blk, pkt->req->getDest(),
1559                                             pkt->id);
1560            PacketList writebacks;
1561            writebacks.push_back(wb_pkt);
1562            doWritebacks(writebacks, 0);
1563        }
1564
1565        return false;
1566    }
1567}
1568
1569bool
1570BaseCache::sendWriteQueuePacket(WriteQueueEntry* wq_entry)
1571{
1572    assert(wq_entry);
1573
1574    // always a single target for write queue entries
1575    PacketPtr tgt_pkt = wq_entry->getTarget()->pkt;
1576
1577    DPRINTF(Cache, "%s: write %s\n", __func__, tgt_pkt->print());
1578
1579    // forward as is, both for evictions and uncacheable writes
1580    if (!memSidePort.sendTimingReq(tgt_pkt)) {
1581        // note that we have now masked any requestBus and
1582        // schedSendEvent (we will wait for a retry before
1583        // doing anything), and this is so even if we do not
1584        // care about this packet and might override it before
1585        // it gets retried
1586        return true;
1587    } else {
1588        markInService(wq_entry);
1589        return false;
1590    }
1591}
1592
1593void
1594BaseCache::serialize(CheckpointOut &cp) const
1595{
1596    bool dirty(isDirty());
1597
1598    if (dirty) {
1599        warn("*** The cache still contains dirty data. ***\n");
1600        warn("    Make sure to drain the system using the correct flags.\n");
1601        warn("    This checkpoint will not restore correctly " \
1602             "and dirty data in the cache will be lost!\n");
1603    }
1604
1605    // Since we don't checkpoint the data in the cache, any dirty data
1606    // will be lost when restoring from a checkpoint of a system that
1607    // wasn't drained properly. Flag the checkpoint as invalid if the
1608    // cache contains dirty data.
1609    bool bad_checkpoint(dirty);
1610    SERIALIZE_SCALAR(bad_checkpoint);
1611}
1612
1613void
1614BaseCache::unserialize(CheckpointIn &cp)
1615{
1616    bool bad_checkpoint;
1617    UNSERIALIZE_SCALAR(bad_checkpoint);
1618    if (bad_checkpoint) {
1619        fatal("Restoring from checkpoints with dirty caches is not "
1620              "supported in the classic memory system. Please remove any "
1621              "caches or drain them properly before taking checkpoints.\n");
1622    }
1623}
1624
1625void
1626BaseCache::regStats()
1627{
1628    MemObject::regStats();
1629
1630    using namespace Stats;
1631
1632    // Hit statistics
1633    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
1634        MemCmd cmd(access_idx);
1635        const string &cstr = cmd.toString();
1636
1637        hits[access_idx]
1638            .init(system->maxMasters())
1639            .name(name() + "." + cstr + "_hits")
1640            .desc("number of " + cstr + " hits")
1641            .flags(total | nozero | nonan)
1642            ;
1643        for (int i = 0; i < system->maxMasters(); i++) {
1644            hits[access_idx].subname(i, system->getMasterName(i));
1645        }
1646    }
1647
1648// These macros make it easier to sum the right subset of commands and
1649// to change the subset of commands that are considered "demand" vs
1650// "non-demand"
1651#define SUM_DEMAND(s) \
1652    (s[MemCmd::ReadReq] + s[MemCmd::WriteReq] + s[MemCmd::WriteLineReq] + \
1653     s[MemCmd::ReadExReq] + s[MemCmd::ReadCleanReq] + s[MemCmd::ReadSharedReq])
1654
1655// should writebacks be included here?  prior code was inconsistent...
1656#define SUM_NON_DEMAND(s) \
1657    (s[MemCmd::SoftPFReq] + s[MemCmd::HardPFReq])
1658
1659    demandHits
1660        .name(name() + ".demand_hits")
1661        .desc("number of demand (read+write) hits")
1662        .flags(total | nozero | nonan)
1663        ;
1664    demandHits = SUM_DEMAND(hits);
1665    for (int i = 0; i < system->maxMasters(); i++) {
1666        demandHits.subname(i, system->getMasterName(i));
1667    }
1668
1669    overallHits
1670        .name(name() + ".overall_hits")
1671        .desc("number of overall hits")
1672        .flags(total | nozero | nonan)
1673        ;
1674    overallHits = demandHits + SUM_NON_DEMAND(hits);
1675    for (int i = 0; i < system->maxMasters(); i++) {
1676        overallHits.subname(i, system->getMasterName(i));
1677    }
1678
1679    // Miss statistics
1680    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
1681        MemCmd cmd(access_idx);
1682        const string &cstr = cmd.toString();
1683
1684        misses[access_idx]
1685            .init(system->maxMasters())
1686            .name(name() + "." + cstr + "_misses")
1687            .desc("number of " + cstr + " misses")
1688            .flags(total | nozero | nonan)
1689            ;
1690        for (int i = 0; i < system->maxMasters(); i++) {
1691            misses[access_idx].subname(i, system->getMasterName(i));
1692        }
1693    }
1694
1695    demandMisses
1696        .name(name() + ".demand_misses")
1697        .desc("number of demand (read+write) misses")
1698        .flags(total | nozero | nonan)
1699        ;
1700    demandMisses = SUM_DEMAND(misses);
1701    for (int i = 0; i < system->maxMasters(); i++) {
1702        demandMisses.subname(i, system->getMasterName(i));
1703    }
1704
1705    overallMisses
1706        .name(name() + ".overall_misses")
1707        .desc("number of overall misses")
1708        .flags(total | nozero | nonan)
1709        ;
1710    overallMisses = demandMisses + SUM_NON_DEMAND(misses);
1711    for (int i = 0; i < system->maxMasters(); i++) {
1712        overallMisses.subname(i, system->getMasterName(i));
1713    }
1714
1715    // Miss latency statistics
1716    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
1717        MemCmd cmd(access_idx);
1718        const string &cstr = cmd.toString();
1719
1720        missLatency[access_idx]
1721            .init(system->maxMasters())
1722            .name(name() + "." + cstr + "_miss_latency")
1723            .desc("number of " + cstr + " miss cycles")
1724            .flags(total | nozero | nonan)
1725            ;
1726        for (int i = 0; i < system->maxMasters(); i++) {
1727            missLatency[access_idx].subname(i, system->getMasterName(i));
1728        }
1729    }
1730
1731    demandMissLatency
1732        .name(name() + ".demand_miss_latency")
1733        .desc("number of demand (read+write) miss cycles")
1734        .flags(total | nozero | nonan)
1735        ;
1736    demandMissLatency = SUM_DEMAND(missLatency);
1737    for (int i = 0; i < system->maxMasters(); i++) {
1738        demandMissLatency.subname(i, system->getMasterName(i));
1739    }
1740
1741    overallMissLatency
1742        .name(name() + ".overall_miss_latency")
1743        .desc("number of overall miss cycles")
1744        .flags(total | nozero | nonan)
1745        ;
1746    overallMissLatency = demandMissLatency + SUM_NON_DEMAND(missLatency);
1747    for (int i = 0; i < system->maxMasters(); i++) {
1748        overallMissLatency.subname(i, system->getMasterName(i));
1749    }
1750
1751    // access formulas
1752    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
1753        MemCmd cmd(access_idx);
1754        const string &cstr = cmd.toString();
1755
1756        accesses[access_idx]
1757            .name(name() + "." + cstr + "_accesses")
1758            .desc("number of " + cstr + " accesses(hits+misses)")
1759            .flags(total | nozero | nonan)
1760            ;
1761        accesses[access_idx] = hits[access_idx] + misses[access_idx];
1762
1763        for (int i = 0; i < system->maxMasters(); i++) {
1764            accesses[access_idx].subname(i, system->getMasterName(i));
1765        }
1766    }
1767
1768    demandAccesses
1769        .name(name() + ".demand_accesses")
1770        .desc("number of demand (read+write) accesses")
1771        .flags(total | nozero | nonan)
1772        ;
1773    demandAccesses = demandHits + demandMisses;
1774    for (int i = 0; i < system->maxMasters(); i++) {
1775        demandAccesses.subname(i, system->getMasterName(i));
1776    }
1777
1778    overallAccesses
1779        .name(name() + ".overall_accesses")
1780        .desc("number of overall (read+write) accesses")
1781        .flags(total | nozero | nonan)
1782        ;
1783    overallAccesses = overallHits + overallMisses;
1784    for (int i = 0; i < system->maxMasters(); i++) {
1785        overallAccesses.subname(i, system->getMasterName(i));
1786    }
1787
1788    // miss rate formulas
1789    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
1790        MemCmd cmd(access_idx);
1791        const string &cstr = cmd.toString();
1792
1793        missRate[access_idx]
1794            .name(name() + "." + cstr + "_miss_rate")
1795            .desc("miss rate for " + cstr + " accesses")
1796            .flags(total | nozero | nonan)
1797            ;
1798        missRate[access_idx] = misses[access_idx] / accesses[access_idx];
1799
1800        for (int i = 0; i < system->maxMasters(); i++) {
1801            missRate[access_idx].subname(i, system->getMasterName(i));
1802        }
1803    }
1804
1805    demandMissRate
1806        .name(name() + ".demand_miss_rate")
1807        .desc("miss rate for demand accesses")
1808        .flags(total | nozero | nonan)
1809        ;
1810    demandMissRate = demandMisses / demandAccesses;
1811    for (int i = 0; i < system->maxMasters(); i++) {
1812        demandMissRate.subname(i, system->getMasterName(i));
1813    }
1814
1815    overallMissRate
1816        .name(name() + ".overall_miss_rate")
1817        .desc("miss rate for overall accesses")
1818        .flags(total | nozero | nonan)
1819        ;
1820    overallMissRate = overallMisses / overallAccesses;
1821    for (int i = 0; i < system->maxMasters(); i++) {
1822        overallMissRate.subname(i, system->getMasterName(i));
1823    }
1824
1825    // miss latency formulas
1826    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
1827        MemCmd cmd(access_idx);
1828        const string &cstr = cmd.toString();
1829
1830        avgMissLatency[access_idx]
1831            .name(name() + "." + cstr + "_avg_miss_latency")
1832            .desc("average " + cstr + " miss latency")
1833            .flags(total | nozero | nonan)
1834            ;
1835        avgMissLatency[access_idx] =
1836            missLatency[access_idx] / misses[access_idx];
1837
1838        for (int i = 0; i < system->maxMasters(); i++) {
1839            avgMissLatency[access_idx].subname(i, system->getMasterName(i));
1840        }
1841    }
1842
1843    demandAvgMissLatency
1844        .name(name() + ".demand_avg_miss_latency")
1845        .desc("average overall miss latency")
1846        .flags(total | nozero | nonan)
1847        ;
1848    demandAvgMissLatency = demandMissLatency / demandMisses;
1849    for (int i = 0; i < system->maxMasters(); i++) {
1850        demandAvgMissLatency.subname(i, system->getMasterName(i));
1851    }
1852
1853    overallAvgMissLatency
1854        .name(name() + ".overall_avg_miss_latency")
1855        .desc("average overall miss latency")
1856        .flags(total | nozero | nonan)
1857        ;
1858    overallAvgMissLatency = overallMissLatency / overallMisses;
1859    for (int i = 0; i < system->maxMasters(); i++) {
1860        overallAvgMissLatency.subname(i, system->getMasterName(i));
1861    }
1862
1863    blocked_cycles.init(NUM_BLOCKED_CAUSES);
1864    blocked_cycles
1865        .name(name() + ".blocked_cycles")
1866        .desc("number of cycles access was blocked")
1867        .subname(Blocked_NoMSHRs, "no_mshrs")
1868        .subname(Blocked_NoTargets, "no_targets")
1869        ;
1870
1871
1872    blocked_causes.init(NUM_BLOCKED_CAUSES);
1873    blocked_causes
1874        .name(name() + ".blocked")
1875        .desc("number of cycles access was blocked")
1876        .subname(Blocked_NoMSHRs, "no_mshrs")
1877        .subname(Blocked_NoTargets, "no_targets")
1878        ;
1879
1880    avg_blocked
1881        .name(name() + ".avg_blocked_cycles")
1882        .desc("average number of cycles each access was blocked")
1883        .subname(Blocked_NoMSHRs, "no_mshrs")
1884        .subname(Blocked_NoTargets, "no_targets")
1885        ;
1886
1887    avg_blocked = blocked_cycles / blocked_causes;
1888
1889    unusedPrefetches
1890        .name(name() + ".unused_prefetches")
1891        .desc("number of HardPF blocks evicted w/o reference")
1892        .flags(nozero)
1893        ;
1894
1895    writebacks
1896        .init(system->maxMasters())
1897        .name(name() + ".writebacks")
1898        .desc("number of writebacks")
1899        .flags(total | nozero | nonan)
1900        ;
1901    for (int i = 0; i < system->maxMasters(); i++) {
1902        writebacks.subname(i, system->getMasterName(i));
1903    }
1904
1905    // MSHR statistics
1906    // MSHR hit statistics
1907    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
1908        MemCmd cmd(access_idx);
1909        const string &cstr = cmd.toString();
1910
1911        mshr_hits[access_idx]
1912            .init(system->maxMasters())
1913            .name(name() + "." + cstr + "_mshr_hits")
1914            .desc("number of " + cstr + " MSHR hits")
1915            .flags(total | nozero | nonan)
1916            ;
1917        for (int i = 0; i < system->maxMasters(); i++) {
1918            mshr_hits[access_idx].subname(i, system->getMasterName(i));
1919        }
1920    }
1921
1922    demandMshrHits
1923        .name(name() + ".demand_mshr_hits")
1924        .desc("number of demand (read+write) MSHR hits")
1925        .flags(total | nozero | nonan)
1926        ;
1927    demandMshrHits = SUM_DEMAND(mshr_hits);
1928    for (int i = 0; i < system->maxMasters(); i++) {
1929        demandMshrHits.subname(i, system->getMasterName(i));
1930    }
1931
1932    overallMshrHits
1933        .name(name() + ".overall_mshr_hits")
1934        .desc("number of overall MSHR hits")
1935        .flags(total | nozero | nonan)
1936        ;
1937    overallMshrHits = demandMshrHits + SUM_NON_DEMAND(mshr_hits);
1938    for (int i = 0; i < system->maxMasters(); i++) {
1939        overallMshrHits.subname(i, system->getMasterName(i));
1940    }
1941
1942    // MSHR miss statistics
1943    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
1944        MemCmd cmd(access_idx);
1945        const string &cstr = cmd.toString();
1946
1947        mshr_misses[access_idx]
1948            .init(system->maxMasters())
1949            .name(name() + "." + cstr + "_mshr_misses")
1950            .desc("number of " + cstr + " MSHR misses")
1951            .flags(total | nozero | nonan)
1952            ;
1953        for (int i = 0; i < system->maxMasters(); i++) {
1954            mshr_misses[access_idx].subname(i, system->getMasterName(i));
1955        }
1956    }
1957
1958    demandMshrMisses
1959        .name(name() + ".demand_mshr_misses")
1960        .desc("number of demand (read+write) MSHR misses")
1961        .flags(total | nozero | nonan)
1962        ;
1963    demandMshrMisses = SUM_DEMAND(mshr_misses);
1964    for (int i = 0; i < system->maxMasters(); i++) {
1965        demandMshrMisses.subname(i, system->getMasterName(i));
1966    }
1967
1968    overallMshrMisses
1969        .name(name() + ".overall_mshr_misses")
1970        .desc("number of overall MSHR misses")
1971        .flags(total | nozero | nonan)
1972        ;
1973    overallMshrMisses = demandMshrMisses + SUM_NON_DEMAND(mshr_misses);
1974    for (int i = 0; i < system->maxMasters(); i++) {
1975        overallMshrMisses.subname(i, system->getMasterName(i));
1976    }
1977
1978    // MSHR miss latency statistics
1979    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
1980        MemCmd cmd(access_idx);
1981        const string &cstr = cmd.toString();
1982
1983        mshr_miss_latency[access_idx]
1984            .init(system->maxMasters())
1985            .name(name() + "." + cstr + "_mshr_miss_latency")
1986            .desc("number of " + cstr + " MSHR miss cycles")
1987            .flags(total | nozero | nonan)
1988            ;
1989        for (int i = 0; i < system->maxMasters(); i++) {
1990            mshr_miss_latency[access_idx].subname(i, system->getMasterName(i));
1991        }
1992    }
1993
1994    demandMshrMissLatency
1995        .name(name() + ".demand_mshr_miss_latency")
1996        .desc("number of demand (read+write) MSHR miss cycles")
1997        .flags(total | nozero | nonan)
1998        ;
1999    demandMshrMissLatency = SUM_DEMAND(mshr_miss_latency);
2000    for (int i = 0; i < system->maxMasters(); i++) {
2001        demandMshrMissLatency.subname(i, system->getMasterName(i));
2002    }
2003
2004    overallMshrMissLatency
2005        .name(name() + ".overall_mshr_miss_latency")
2006        .desc("number of overall MSHR miss cycles")
2007        .flags(total | nozero | nonan)
2008        ;
2009    overallMshrMissLatency =
2010        demandMshrMissLatency + SUM_NON_DEMAND(mshr_miss_latency);
2011    for (int i = 0; i < system->maxMasters(); i++) {
2012        overallMshrMissLatency.subname(i, system->getMasterName(i));
2013    }
2014
2015    // MSHR uncacheable statistics
2016    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
2017        MemCmd cmd(access_idx);
2018        const string &cstr = cmd.toString();
2019
2020        mshr_uncacheable[access_idx]
2021            .init(system->maxMasters())
2022            .name(name() + "." + cstr + "_mshr_uncacheable")
2023            .desc("number of " + cstr + " MSHR uncacheable")
2024            .flags(total | nozero | nonan)
2025            ;
2026        for (int i = 0; i < system->maxMasters(); i++) {
2027            mshr_uncacheable[access_idx].subname(i, system->getMasterName(i));
2028        }
2029    }
2030
2031    overallMshrUncacheable
2032        .name(name() + ".overall_mshr_uncacheable_misses")
2033        .desc("number of overall MSHR uncacheable misses")
2034        .flags(total | nozero | nonan)
2035        ;
2036    overallMshrUncacheable =
2037        SUM_DEMAND(mshr_uncacheable) + SUM_NON_DEMAND(mshr_uncacheable);
2038    for (int i = 0; i < system->maxMasters(); i++) {
2039        overallMshrUncacheable.subname(i, system->getMasterName(i));
2040    }
2041
2042    // MSHR miss latency statistics
2043    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
2044        MemCmd cmd(access_idx);
2045        const string &cstr = cmd.toString();
2046
2047        mshr_uncacheable_lat[access_idx]
2048            .init(system->maxMasters())
2049            .name(name() + "." + cstr + "_mshr_uncacheable_latency")
2050            .desc("number of " + cstr + " MSHR uncacheable cycles")
2051            .flags(total | nozero | nonan)
2052            ;
2053        for (int i = 0; i < system->maxMasters(); i++) {
2054            mshr_uncacheable_lat[access_idx].subname(
2055                i, system->getMasterName(i));
2056        }
2057    }
2058
2059    overallMshrUncacheableLatency
2060        .name(name() + ".overall_mshr_uncacheable_latency")
2061        .desc("number of overall MSHR uncacheable cycles")
2062        .flags(total | nozero | nonan)
2063        ;
2064    overallMshrUncacheableLatency =
2065        SUM_DEMAND(mshr_uncacheable_lat) +
2066        SUM_NON_DEMAND(mshr_uncacheable_lat);
2067    for (int i = 0; i < system->maxMasters(); i++) {
2068        overallMshrUncacheableLatency.subname(i, system->getMasterName(i));
2069    }
2070
2071#if 0
2072    // MSHR access formulas
2073    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
2074        MemCmd cmd(access_idx);
2075        const string &cstr = cmd.toString();
2076
2077        mshrAccesses[access_idx]
2078            .name(name() + "." + cstr + "_mshr_accesses")
2079            .desc("number of " + cstr + " mshr accesses(hits+misses)")
2080            .flags(total | nozero | nonan)
2081            ;
2082        mshrAccesses[access_idx] =
2083            mshr_hits[access_idx] + mshr_misses[access_idx]
2084            + mshr_uncacheable[access_idx];
2085    }
2086
2087    demandMshrAccesses
2088        .name(name() + ".demand_mshr_accesses")
2089        .desc("number of demand (read+write) mshr accesses")
2090        .flags(total | nozero | nonan)
2091        ;
2092    demandMshrAccesses = demandMshrHits + demandMshrMisses;
2093
2094    overallMshrAccesses
2095        .name(name() + ".overall_mshr_accesses")
2096        .desc("number of overall (read+write) mshr accesses")
2097        .flags(total | nozero | nonan)
2098        ;
2099    overallMshrAccesses = overallMshrHits + overallMshrMisses
2100        + overallMshrUncacheable;
2101#endif
2102
2103    // MSHR miss rate formulas
2104    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
2105        MemCmd cmd(access_idx);
2106        const string &cstr = cmd.toString();
2107
2108        mshrMissRate[access_idx]
2109            .name(name() + "." + cstr + "_mshr_miss_rate")
2110            .desc("mshr miss rate for " + cstr + " accesses")
2111            .flags(total | nozero | nonan)
2112            ;
2113        mshrMissRate[access_idx] =
2114            mshr_misses[access_idx] / accesses[access_idx];
2115
2116        for (int i = 0; i < system->maxMasters(); i++) {
2117            mshrMissRate[access_idx].subname(i, system->getMasterName(i));
2118        }
2119    }
2120
2121    demandMshrMissRate
2122        .name(name() + ".demand_mshr_miss_rate")
2123        .desc("mshr miss rate for demand accesses")
2124        .flags(total | nozero | nonan)
2125        ;
2126    demandMshrMissRate = demandMshrMisses / demandAccesses;
2127    for (int i = 0; i < system->maxMasters(); i++) {
2128        demandMshrMissRate.subname(i, system->getMasterName(i));
2129    }
2130
2131    overallMshrMissRate
2132        .name(name() + ".overall_mshr_miss_rate")
2133        .desc("mshr miss rate for overall accesses")
2134        .flags(total | nozero | nonan)
2135        ;
2136    overallMshrMissRate = overallMshrMisses / overallAccesses;
2137    for (int i = 0; i < system->maxMasters(); i++) {
2138        overallMshrMissRate.subname(i, system->getMasterName(i));
2139    }
2140
2141    // mshrMiss latency formulas
2142    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
2143        MemCmd cmd(access_idx);
2144        const string &cstr = cmd.toString();
2145
2146        avgMshrMissLatency[access_idx]
2147            .name(name() + "." + cstr + "_avg_mshr_miss_latency")
2148            .desc("average " + cstr + " mshr miss latency")
2149            .flags(total | nozero | nonan)
2150            ;
2151        avgMshrMissLatency[access_idx] =
2152            mshr_miss_latency[access_idx] / mshr_misses[access_idx];
2153
2154        for (int i = 0; i < system->maxMasters(); i++) {
2155            avgMshrMissLatency[access_idx].subname(
2156                i, system->getMasterName(i));
2157        }
2158    }
2159
2160    demandAvgMshrMissLatency
2161        .name(name() + ".demand_avg_mshr_miss_latency")
2162        .desc("average overall mshr miss latency")
2163        .flags(total | nozero | nonan)
2164        ;
2165    demandAvgMshrMissLatency = demandMshrMissLatency / demandMshrMisses;
2166    for (int i = 0; i < system->maxMasters(); i++) {
2167        demandAvgMshrMissLatency.subname(i, system->getMasterName(i));
2168    }
2169
2170    overallAvgMshrMissLatency
2171        .name(name() + ".overall_avg_mshr_miss_latency")
2172        .desc("average overall mshr miss latency")
2173        .flags(total | nozero | nonan)
2174        ;
2175    overallAvgMshrMissLatency = overallMshrMissLatency / overallMshrMisses;
2176    for (int i = 0; i < system->maxMasters(); i++) {
2177        overallAvgMshrMissLatency.subname(i, system->getMasterName(i));
2178    }
2179
2180    // mshrUncacheable latency formulas
2181    for (int access_idx = 0; access_idx < MemCmd::NUM_MEM_CMDS; ++access_idx) {
2182        MemCmd cmd(access_idx);
2183        const string &cstr = cmd.toString();
2184
2185        avgMshrUncacheableLatency[access_idx]
2186            .name(name() + "." + cstr + "_avg_mshr_uncacheable_latency")
2187            .desc("average " + cstr + " mshr uncacheable latency")
2188            .flags(total | nozero | nonan)
2189            ;
2190        avgMshrUncacheableLatency[access_idx] =
2191            mshr_uncacheable_lat[access_idx] / mshr_uncacheable[access_idx];
2192
2193        for (int i = 0; i < system->maxMasters(); i++) {
2194            avgMshrUncacheableLatency[access_idx].subname(
2195                i, system->getMasterName(i));
2196        }
2197    }
2198
2199    overallAvgMshrUncacheableLatency
2200        .name(name() + ".overall_avg_mshr_uncacheable_latency")
2201        .desc("average overall mshr uncacheable latency")
2202        .flags(total | nozero | nonan)
2203        ;
2204    overallAvgMshrUncacheableLatency =
2205        overallMshrUncacheableLatency / overallMshrUncacheable;
2206    for (int i = 0; i < system->maxMasters(); i++) {
2207        overallAvgMshrUncacheableLatency.subname(i, system->getMasterName(i));
2208    }
2209
2210    replacements
2211        .name(name() + ".replacements")
2212        .desc("number of replacements")
2213        ;
2214}
2215
2216///////////////
2217//
2218// CpuSidePort
2219//
2220///////////////
2221bool
2222BaseCache::CpuSidePort::recvTimingSnoopResp(PacketPtr pkt)
2223{
2224    // Snoops shouldn't happen when bypassing caches
2225    assert(!cache->system->bypassCaches());
2226
2227    assert(pkt->isResponse());
2228
2229    // Express snoop responses from master to slave, e.g., from L1 to L2
2230    cache->recvTimingSnoopResp(pkt);
2231    return true;
2232}
2233
2234
2235bool
2236BaseCache::CpuSidePort::tryTiming(PacketPtr pkt)
2237{
2238    if (cache->system->bypassCaches() || pkt->isExpressSnoop()) {
2239        // always let express snoop packets through even if blocked
2240        return true;
2241    } else if (blocked || mustSendRetry) {
2242        // either already committed to send a retry, or blocked
2243        mustSendRetry = true;
2244        return false;
2245    }
2246    mustSendRetry = false;
2247    return true;
2248}
2249
2250bool
2251BaseCache::CpuSidePort::recvTimingReq(PacketPtr pkt)
2252{
2253    assert(pkt->isRequest());
2254
2255    if (cache->system->bypassCaches()) {
2256        // Just forward the packet if caches are disabled.
2257        // @todo This should really enqueue the packet rather
2258        bool M5_VAR_USED success = cache->memSidePort.sendTimingReq(pkt);
2259        assert(success);
2260        return true;
2261    } else if (tryTiming(pkt)) {
2262        cache->recvTimingReq(pkt);
2263        return true;
2264    }
2265    return false;
2266}
2267
2268Tick
2269BaseCache::CpuSidePort::recvAtomic(PacketPtr pkt)
2270{
2271    if (cache->system->bypassCaches()) {
2272        // Forward the request if the system is in cache bypass mode.
2273        return cache->memSidePort.sendAtomic(pkt);
2274    } else {
2275        return cache->recvAtomic(pkt);
2276    }
2277}
2278
2279void
2280BaseCache::CpuSidePort::recvFunctional(PacketPtr pkt)
2281{
2282    if (cache->system->bypassCaches()) {
2283        // The cache should be flushed if we are in cache bypass mode,
2284        // so we don't need to check if we need to update anything.
2285        cache->memSidePort.sendFunctional(pkt);
2286        return;
2287    }
2288
2289    // functional request
2290    cache->functionalAccess(pkt, true);
2291}
2292
2293AddrRangeList
2294BaseCache::CpuSidePort::getAddrRanges() const
2295{
2296    return cache->getAddrRanges();
2297}
2298
2299
2300BaseCache::
2301CpuSidePort::CpuSidePort(const std::string &_name, BaseCache *_cache,
2302                         const std::string &_label)
2303    : CacheSlavePort(_name, _cache, _label), cache(_cache)
2304{
2305}
2306
2307///////////////
2308//
2309// MemSidePort
2310//
2311///////////////
2312bool
2313BaseCache::MemSidePort::recvTimingResp(PacketPtr pkt)
2314{
2315    cache->recvTimingResp(pkt);
2316    return true;
2317}
2318
2319// Express snooping requests to memside port
2320void
2321BaseCache::MemSidePort::recvTimingSnoopReq(PacketPtr pkt)
2322{
2323    // Snoops shouldn't happen when bypassing caches
2324    assert(!cache->system->bypassCaches());
2325
2326    // handle snooping requests
2327    cache->recvTimingSnoopReq(pkt);
2328}
2329
2330Tick
2331BaseCache::MemSidePort::recvAtomicSnoop(PacketPtr pkt)
2332{
2333    // Snoops shouldn't happen when bypassing caches
2334    assert(!cache->system->bypassCaches());
2335
2336    return cache->recvAtomicSnoop(pkt);
2337}
2338
2339void
2340BaseCache::MemSidePort::recvFunctionalSnoop(PacketPtr pkt)
2341{
2342    // Snoops shouldn't happen when bypassing caches
2343    assert(!cache->system->bypassCaches());
2344
2345    // functional snoop (note that in contrast to atomic we don't have
2346    // a specific functionalSnoop method, as they have the same
2347    // behaviour regardless)
2348    cache->functionalAccess(pkt, false);
2349}
2350
2351void
2352BaseCache::CacheReqPacketQueue::sendDeferredPacket()
2353{
2354    // sanity check
2355    assert(!waitingOnRetry);
2356
2357    // there should never be any deferred request packets in the
2358    // queue, instead we resly on the cache to provide the packets
2359    // from the MSHR queue or write queue
2360    assert(deferredPacketReadyTime() == MaxTick);
2361
2362    // check for request packets (requests & writebacks)
2363    QueueEntry* entry = cache.getNextQueueEntry();
2364
2365    if (!entry) {
2366        // can happen if e.g. we attempt a writeback and fail, but
2367        // before the retry, the writeback is eliminated because
2368        // we snoop another cache's ReadEx.
2369    } else {
2370        // let our snoop responses go first if there are responses to
2371        // the same addresses
2372        if (checkConflictingSnoop(entry->blkAddr)) {
2373            return;
2374        }
2375        waitingOnRetry = entry->sendPacket(cache);
2376    }
2377
2378    // if we succeeded and are not waiting for a retry, schedule the
2379    // next send considering when the next queue is ready, note that
2380    // snoop responses have their own packet queue and thus schedule
2381    // their own events
2382    if (!waitingOnRetry) {
2383        schedSendEvent(cache.nextQueueReadyTime());
2384    }
2385}
2386
2387BaseCache::MemSidePort::MemSidePort(const std::string &_name,
2388                                    BaseCache *_cache,
2389                                    const std::string &_label)
2390    : CacheMasterPort(_name, _cache, _reqQueue, _snoopRespQueue),
2391      _reqQueue(*_cache, *this, _snoopRespQueue, _label),
2392      _snoopRespQueue(*_cache, *this, _label), cache(_cache)
2393{
2394}
2395
2396void
2397WriteAllocator::updateMode(Addr write_addr, unsigned write_size,
2398                           Addr blk_addr)
2399{
2400    // check if we are continuing where the last write ended
2401    if (nextAddr == write_addr) {
2402        delayCtr[blk_addr] = delayThreshold;
2403        // stop if we have already saturated
2404        if (mode != WriteMode::NO_ALLOCATE) {
2405            byteCount += write_size;
2406            // switch to streaming mode if we have passed the lower
2407            // threshold
2408            if (mode == WriteMode::ALLOCATE &&
2409                byteCount > coalesceLimit) {
2410                mode = WriteMode::COALESCE;
2411                DPRINTF(Cache, "Switched to write coalescing\n");
2412            } else if (mode == WriteMode::COALESCE &&
2413                       byteCount > noAllocateLimit) {
2414                // and continue and switch to non-allocating mode if we
2415                // pass the upper threshold
2416                mode = WriteMode::NO_ALLOCATE;
2417                DPRINTF(Cache, "Switched to write-no-allocate\n");
2418            }
2419        }
2420    } else {
2421        // we did not see a write matching the previous one, start
2422        // over again
2423        byteCount = write_size;
2424        mode = WriteMode::ALLOCATE;
2425        resetDelay(blk_addr);
2426    }
2427    nextAddr = write_addr + write_size;
2428}
2429
2430WriteAllocator*
2431WriteAllocatorParams::create()
2432{
2433    return new WriteAllocator(this);
2434}
2435