mshr.cc revision 12727
12810SN/A/*
212501Snikos.nikoleris@arm.com * Copyright (c) 2012-2013, 2015-2018 ARM Limited
39663Suri.wiener@arm.com * All rights reserved.
49663Suri.wiener@arm.com *
59663Suri.wiener@arm.com * The license below extends only to copyright in the software and shall
69663Suri.wiener@arm.com * not be construed as granting a license to any other intellectual
79663Suri.wiener@arm.com * property including but not limited to intellectual property relating
89663Suri.wiener@arm.com * to a hardware implementation of the functionality of the software
99663Suri.wiener@arm.com * licensed hereunder.  You may use the software subject to the license
109663Suri.wiener@arm.com * terms below provided that you ensure that this notice is replicated
119663Suri.wiener@arm.com * unmodified and in its entirety in all distributions of the software,
129663Suri.wiener@arm.com * modified or unmodified, in source code or in binary form.
139663Suri.wiener@arm.com *
142810SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
157636Ssteve.reinhardt@amd.com * Copyright (c) 2010 Advanced Micro Devices, Inc.
162810SN/A * All rights reserved.
172810SN/A *
182810SN/A * Redistribution and use in source and binary forms, with or without
192810SN/A * modification, are permitted provided that the following conditions are
202810SN/A * met: redistributions of source code must retain the above copyright
212810SN/A * notice, this list of conditions and the following disclaimer;
222810SN/A * redistributions in binary form must reproduce the above copyright
232810SN/A * notice, this list of conditions and the following disclaimer in the
242810SN/A * documentation and/or other materials provided with the distribution;
252810SN/A * neither the name of the copyright holders nor the names of its
262810SN/A * contributors may be used to endorse or promote products derived from
272810SN/A * this software without specific prior written permission.
282810SN/A *
292810SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
302810SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
312810SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
322810SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
332810SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
342810SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
352810SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
362810SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
372810SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
382810SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
392810SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
402810SN/A *
412810SN/A * Authors: Erik Hallnor
422810SN/A *          Dave Greene
432810SN/A */
442810SN/A
452810SN/A/**
462810SN/A * @file
472810SN/A * Miss Status and Handling Register (MSHR) definitions.
482810SN/A */
492810SN/A
5011486Snikos.nikoleris@arm.com#include "mem/cache/mshr.hh"
5111486Snikos.nikoleris@arm.com
526216Snate@binkert.org#include <cassert>
532810SN/A#include <string>
542810SN/A
5512334Sgabeblack@google.com#include "base/logging.hh"
5612727Snikos.nikoleris@arm.com#include "base/trace.hh"
576216Snate@binkert.org#include "base/types.hh"
588232Snate@binkert.org#include "debug/Cache.hh"
5912727Snikos.nikoleris@arm.com#include "mem/cache/base.hh"
6012727Snikos.nikoleris@arm.com#include "mem/request.hh"
616216Snate@binkert.org#include "sim/core.hh"
622810SN/A
6311375Sandreas.hansson@arm.comMSHR::MSHR() : downstreamPending(false),
6411284Sandreas.hansson@arm.com               pendingModified(false),
6510503SCurtis.Dunham@arm.com               postInvalidate(false), postDowngrade(false),
6611741Snikos.nikoleris@arm.com               isForward(false)
672810SN/A{
682810SN/A}
692810SN/A
704903SN/AMSHR::TargetList::TargetList()
7112715Snikos.nikoleris@arm.com    : needsWritable(false), hasUpgrade(false), allocOnFill(false),
7212715Snikos.nikoleris@arm.com      hasFromCache(false)
734903SN/A{}
744903SN/A
754903SN/A
7611740Snikos.nikoleris@arm.comvoid
7711741Snikos.nikoleris@arm.comMSHR::TargetList::updateFlags(PacketPtr pkt, Target::Source source,
7811741Snikos.nikoleris@arm.com                              bool alloc_on_fill)
794903SN/A{
805875Ssteve.reinhardt@amd.com    if (source != Target::FromSnoop) {
8111284Sandreas.hansson@arm.com        if (pkt->needsWritable()) {
8211284Sandreas.hansson@arm.com            needsWritable = true;
834903SN/A        }
844903SN/A
857669Ssteve.reinhardt@amd.com        // StoreCondReq is effectively an upgrade if it's in an MSHR
867669Ssteve.reinhardt@amd.com        // since it would have been failed already if we didn't have a
877669Ssteve.reinhardt@amd.com        // read-only copy
887669Ssteve.reinhardt@amd.com        if (pkt->isUpgrade() || pkt->cmd == MemCmd::StoreCondReq) {
894903SN/A            hasUpgrade = true;
904903SN/A        }
9111741Snikos.nikoleris@arm.com
9211741Snikos.nikoleris@arm.com        // potentially re-evaluate whether we should allocate on a fill or
9311741Snikos.nikoleris@arm.com        // not
9411741Snikos.nikoleris@arm.com        allocOnFill = allocOnFill || alloc_on_fill;
9512715Snikos.nikoleris@arm.com
9612715Snikos.nikoleris@arm.com        if (source != Target::FromPrefetcher) {
9712715Snikos.nikoleris@arm.com            hasFromCache = hasFromCache || pkt->fromCache();
9812715Snikos.nikoleris@arm.com        }
995318SN/A    }
10011740Snikos.nikoleris@arm.com}
1014908SN/A
10211740Snikos.nikoleris@arm.comvoid
10311740Snikos.nikoleris@arm.comMSHR::TargetList::populateFlags()
10411740Snikos.nikoleris@arm.com{
10511740Snikos.nikoleris@arm.com    resetFlags();
10611740Snikos.nikoleris@arm.com    for (auto& t: *this) {
10711741Snikos.nikoleris@arm.com        updateFlags(t.pkt, t.source, t.allocOnFill);
10811740Snikos.nikoleris@arm.com    }
10911740Snikos.nikoleris@arm.com}
11011740Snikos.nikoleris@arm.com
11111740Snikos.nikoleris@arm.cominline void
11211740Snikos.nikoleris@arm.comMSHR::TargetList::add(PacketPtr pkt, Tick readyTime,
11311741Snikos.nikoleris@arm.com                      Counter order, Target::Source source, bool markPending,
11411741Snikos.nikoleris@arm.com                      bool alloc_on_fill)
11511740Snikos.nikoleris@arm.com{
11611741Snikos.nikoleris@arm.com    updateFlags(pkt, source, alloc_on_fill);
1175318SN/A    if (markPending) {
1189543Ssascha.bischoff@arm.com        // Iterate over the SenderState stack and see if we find
1199543Ssascha.bischoff@arm.com        // an MSHR entry. If we do, set the downstreamPending
1209543Ssascha.bischoff@arm.com        // flag. Otherwise, do nothing.
1219543Ssascha.bischoff@arm.com        MSHR *mshr = pkt->findNextSenderState<MSHR>();
12211484Snikos.nikoleris@arm.com        if (mshr != nullptr) {
1234908SN/A            assert(!mshr->downstreamPending);
1244908SN/A            mshr->downstreamPending = true;
12511083Sandreas.hansson@arm.com        } else {
12611083Sandreas.hansson@arm.com            // No need to clear downstreamPending later
12711083Sandreas.hansson@arm.com            markPending = false;
1284908SN/A        }
1294903SN/A    }
1304903SN/A
13111741Snikos.nikoleris@arm.com    emplace_back(pkt, readyTime, order, source, markPending, alloc_on_fill);
1324903SN/A}
1334903SN/A
1344903SN/A
1357667Ssteve.reinhardt@amd.comstatic void
1367667Ssteve.reinhardt@amd.comreplaceUpgrade(PacketPtr pkt)
1377667Ssteve.reinhardt@amd.com{
13811286Sandreas.hansson@arm.com    // remember if the current packet has data allocated
13911286Sandreas.hansson@arm.com    bool has_data = pkt->hasData() || pkt->hasRespData();
14011286Sandreas.hansson@arm.com
1417667Ssteve.reinhardt@amd.com    if (pkt->cmd == MemCmd::UpgradeReq) {
1427667Ssteve.reinhardt@amd.com        pkt->cmd = MemCmd::ReadExReq;
1437667Ssteve.reinhardt@amd.com        DPRINTF(Cache, "Replacing UpgradeReq with ReadExReq\n");
1447667Ssteve.reinhardt@amd.com    } else if (pkt->cmd == MemCmd::SCUpgradeReq) {
1457667Ssteve.reinhardt@amd.com        pkt->cmd = MemCmd::SCUpgradeFailReq;
1467667Ssteve.reinhardt@amd.com        DPRINTF(Cache, "Replacing SCUpgradeReq with SCUpgradeFailReq\n");
1477669Ssteve.reinhardt@amd.com    } else if (pkt->cmd == MemCmd::StoreCondReq) {
1487669Ssteve.reinhardt@amd.com        pkt->cmd = MemCmd::StoreCondFailReq;
1497669Ssteve.reinhardt@amd.com        DPRINTF(Cache, "Replacing StoreCondReq with StoreCondFailReq\n");
1507667Ssteve.reinhardt@amd.com    }
15111286Sandreas.hansson@arm.com
15211286Sandreas.hansson@arm.com    if (!has_data) {
15311286Sandreas.hansson@arm.com        // there is no sensible way of setting the data field if the
15411286Sandreas.hansson@arm.com        // new command actually would carry data
15511286Sandreas.hansson@arm.com        assert(!pkt->hasData());
15611286Sandreas.hansson@arm.com
15711286Sandreas.hansson@arm.com        if (pkt->hasRespData()) {
15811286Sandreas.hansson@arm.com            // we went from a packet that had no data (neither request,
15911286Sandreas.hansson@arm.com            // nor response), to one that does, and therefore we need to
16011286Sandreas.hansson@arm.com            // actually allocate space for the data payload
16111286Sandreas.hansson@arm.com            pkt->allocate();
16211286Sandreas.hansson@arm.com        }
16311286Sandreas.hansson@arm.com    }
1647667Ssteve.reinhardt@amd.com}
1657667Ssteve.reinhardt@amd.com
1667667Ssteve.reinhardt@amd.com
1674903SN/Avoid
1684903SN/AMSHR::TargetList::replaceUpgrades()
1694903SN/A{
1704903SN/A    if (!hasUpgrade)
1714903SN/A        return;
1724903SN/A
17310766Sandreas.hansson@arm.com    for (auto& t : *this) {
17410766Sandreas.hansson@arm.com        replaceUpgrade(t.pkt);
1754903SN/A    }
1764903SN/A
1774903SN/A    hasUpgrade = false;
1784903SN/A}
1794903SN/A
1804903SN/A
1812810SN/Avoid
1824908SN/AMSHR::TargetList::clearDownstreamPending()
1834908SN/A{
18410766Sandreas.hansson@arm.com    for (auto& t : *this) {
18510766Sandreas.hansson@arm.com        if (t.markedPending) {
1869543Ssascha.bischoff@arm.com            // Iterate over the SenderState stack and see if we find
1879543Ssascha.bischoff@arm.com            // an MSHR entry. If we find one, clear the
1889543Ssascha.bischoff@arm.com            // downstreamPending flag by calling
1899543Ssascha.bischoff@arm.com            // clearDownstreamPending(). This recursively clears the
1909543Ssascha.bischoff@arm.com            // downstreamPending flag in all caches this packet has
1919543Ssascha.bischoff@arm.com            // passed through.
19210766Sandreas.hansson@arm.com            MSHR *mshr = t.pkt->findNextSenderState<MSHR>();
19311484Snikos.nikoleris@arm.com            if (mshr != nullptr) {
1945318SN/A                mshr->clearDownstreamPending();
1955318SN/A            }
19611742Snikos.nikoleris@arm.com            t.markedPending = false;
1974908SN/A        }
1984908SN/A    }
1994908SN/A}
2004908SN/A
2014908SN/A
2024920SN/Abool
2034920SN/AMSHR::TargetList::checkFunctional(PacketPtr pkt)
2044920SN/A{
20510766Sandreas.hansson@arm.com    for (auto& t : *this) {
20610766Sandreas.hansson@arm.com        if (pkt->checkFunctional(t.pkt)) {
2074920SN/A            return true;
2084920SN/A        }
2094920SN/A    }
2104920SN/A
2114920SN/A    return false;
2124920SN/A}
2134920SN/A
2144920SN/A
2154908SN/Avoid
21610766Sandreas.hansson@arm.comMSHR::TargetList::print(std::ostream &os, int verbosity,
21710766Sandreas.hansson@arm.com                        const std::string &prefix) const
2185314SN/A{
21910766Sandreas.hansson@arm.com    for (auto& t : *this) {
2205875Ssteve.reinhardt@amd.com        const char *s;
22110766Sandreas.hansson@arm.com        switch (t.source) {
2228988SAli.Saidi@ARM.com          case Target::FromCPU:
2238988SAli.Saidi@ARM.com            s = "FromCPU";
2248988SAli.Saidi@ARM.com            break;
2258988SAli.Saidi@ARM.com          case Target::FromSnoop:
2268988SAli.Saidi@ARM.com            s = "FromSnoop";
2278988SAli.Saidi@ARM.com            break;
2288988SAli.Saidi@ARM.com          case Target::FromPrefetcher:
2298988SAli.Saidi@ARM.com            s = "FromPrefetcher";
2308988SAli.Saidi@ARM.com            break;
2318988SAli.Saidi@ARM.com          default:
2328988SAli.Saidi@ARM.com            s = "";
2338988SAli.Saidi@ARM.com            break;
2345875Ssteve.reinhardt@amd.com        }
2355875Ssteve.reinhardt@amd.com        ccprintf(os, "%s%s: ", prefix, s);
23610766Sandreas.hansson@arm.com        t.pkt->print(os, verbosity, "");
23711744Snikos.nikoleris@arm.com        ccprintf(os, "\n");
2385314SN/A    }
2395314SN/A}
2405314SN/A
2415314SN/A
2425314SN/Avoid
24310764Sandreas.hansson@arm.comMSHR::allocate(Addr blk_addr, unsigned blk_size, PacketPtr target,
24411197Sandreas.hansson@arm.com               Tick when_ready, Counter _order, bool alloc_on_fill)
2452810SN/A{
24610764Sandreas.hansson@arm.com    blkAddr = blk_addr;
24710764Sandreas.hansson@arm.com    blkSize = blk_size;
24810028SGiacomo.Gabrielli@arm.com    isSecure = target->isSecure();
24910764Sandreas.hansson@arm.com    readyTime = when_ready;
2504666SN/A    order = _order;
2514626SN/A    assert(target);
2525730SSteve.Reinhardt@amd.com    isForward = false;
2534626SN/A    _isUncacheable = target->req->isUncacheable();
2544626SN/A    inService = false;
2554908SN/A    downstreamPending = false;
2569725Sandreas.hansson@arm.com    assert(targets.isReset());
2574626SN/A    // Don't know of a case where we would allocate a new MSHR for a
2585875Ssteve.reinhardt@amd.com    // snoop (mem-side request), so set source according to request here
2595875Ssteve.reinhardt@amd.com    Target::Source source = (target->cmd == MemCmd::HardPFReq) ?
2605875Ssteve.reinhardt@amd.com        Target::FromPrefetcher : Target::FromCPU;
26111741Snikos.nikoleris@arm.com    targets.add(target, when_ready, _order, source, true, alloc_on_fill);
2629725Sandreas.hansson@arm.com    assert(deferredTargets.isReset());
2632810SN/A}
2642810SN/A
2654908SN/A
2665318SN/Avoid
2675318SN/AMSHR::clearDownstreamPending()
2685318SN/A{
2695318SN/A    assert(downstreamPending);
2705318SN/A    downstreamPending = false;
2715318SN/A    // recursively clear flag on any MSHRs we will be forwarding
2725318SN/A    // responses to
2739725Sandreas.hansson@arm.com    targets.clearDownstreamPending();
2745318SN/A}
2755318SN/A
27611375Sandreas.hansson@arm.comvoid
27711284Sandreas.hansson@arm.comMSHR::markInService(bool pending_modified_resp)
2784908SN/A{
2794908SN/A    assert(!inService);
28010424Sandreas.hansson@arm.com
2814908SN/A    inService = true;
28211284Sandreas.hansson@arm.com    pendingModified = targets.needsWritable || pending_modified_resp;
2837667Ssteve.reinhardt@amd.com    postInvalidate = postDowngrade = false;
2847667Ssteve.reinhardt@amd.com
2854908SN/A    if (!downstreamPending) {
2864908SN/A        // let upstream caches know that the request has made it to a
2874908SN/A        // level where it's going to get a response
2889725Sandreas.hansson@arm.com        targets.clearDownstreamPending();
2894908SN/A    }
2904908SN/A}
2914908SN/A
2924908SN/A
2932810SN/Avoid
2942810SN/AMSHR::deallocate()
2952810SN/A{
2969725Sandreas.hansson@arm.com    assert(targets.empty());
2979725Sandreas.hansson@arm.com    targets.resetFlags();
2989725Sandreas.hansson@arm.com    assert(deferredTargets.isReset());
2992810SN/A    inService = false;
3002810SN/A}
3012810SN/A
3022810SN/A/*
3032810SN/A * Adds a target to an MSHR
3042810SN/A */
3052810SN/Avoid
30611197Sandreas.hansson@arm.comMSHR::allocateTarget(PacketPtr pkt, Tick whenReady, Counter _order,
30711197Sandreas.hansson@arm.com                     bool alloc_on_fill)
3082810SN/A{
30910768Sandreas.hansson@arm.com    // assume we'd never issue a prefetch when we've got an
31010768Sandreas.hansson@arm.com    // outstanding miss
31110768Sandreas.hansson@arm.com    assert(pkt->cmd != MemCmd::HardPFReq);
31210768Sandreas.hansson@arm.com
3134903SN/A    // if there's a request already in service for this MSHR, we will
3144903SN/A    // have to defer the new target until after the response if any of
3154903SN/A    // the following are true:
3164903SN/A    // - there are other targets already deferred
3174903SN/A    // - there's a pending invalidate to be applied after the response
3184903SN/A    //   comes back (but before this target is processed)
31912350Snikos.nikoleris@arm.com    // - the MSHR's first (and only) non-deferred target is a cache
32012350Snikos.nikoleris@arm.com    //   maintenance packet
32112350Snikos.nikoleris@arm.com    // - the new target is a cache maintenance packet (this is probably
32212350Snikos.nikoleris@arm.com    //   overly conservative but certainly safe)
32311284Sandreas.hansson@arm.com    // - this target requires a writable block and either we're not
32411284Sandreas.hansson@arm.com    //   getting a writable block back or we have already snooped
32511284Sandreas.hansson@arm.com    //   another read request that will downgrade our writable block
32611284Sandreas.hansson@arm.com    //   to non-writable (Shared or Owned)
32712350Snikos.nikoleris@arm.com    PacketPtr tgt_pkt = targets.front().pkt;
32812350Snikos.nikoleris@arm.com    if (pkt->req->isCacheMaintenance() ||
32912350Snikos.nikoleris@arm.com        tgt_pkt->req->isCacheMaintenance() ||
33012350Snikos.nikoleris@arm.com        !deferredTargets.empty() ||
33112350Snikos.nikoleris@arm.com        (inService &&
33212350Snikos.nikoleris@arm.com         (hasPostInvalidate() ||
33312350Snikos.nikoleris@arm.com          (pkt->needsWritable() &&
33412350Snikos.nikoleris@arm.com           (!isPendingModified() || hasPostDowngrade() || isForward))))) {
3354903SN/A        // need to put on deferred list
33612350Snikos.nikoleris@arm.com        if (inService && hasPostInvalidate())
3377667Ssteve.reinhardt@amd.com            replaceUpgrade(pkt);
33811741Snikos.nikoleris@arm.com        deferredTargets.add(pkt, whenReady, _order, Target::FromCPU, true,
33911741Snikos.nikoleris@arm.com                            alloc_on_fill);
3404665SN/A    } else {
3415318SN/A        // No request outstanding, or still OK to append to
3425318SN/A        // outstanding request: append to regular target list.  Only
3435318SN/A        // mark pending if current request hasn't been issued yet
3445318SN/A        // (isn't in service).
34511741Snikos.nikoleris@arm.com        targets.add(pkt, whenReady, _order, Target::FromCPU, !inService,
34611741Snikos.nikoleris@arm.com                    alloc_on_fill);
3472810SN/A    }
3484665SN/A}
3494665SN/A
3504902SN/Abool
3514902SN/AMSHR::handleSnoop(PacketPtr pkt, Counter _order)
3524665SN/A{
35311744Snikos.nikoleris@arm.com    DPRINTF(Cache, "%s for %s\n", __func__, pkt->print());
35411279Sandreas.hansson@arm.com
35511284Sandreas.hansson@arm.com    // when we snoop packets the needsWritable and isInvalidate flags
35611279Sandreas.hansson@arm.com    // should always be the same, however, this assumes that we never
35711279Sandreas.hansson@arm.com    // snoop writes as they are currently not marked as invalidations
35812350Snikos.nikoleris@arm.com    panic_if((pkt->needsWritable() != pkt->isInvalidate()) &&
35912350Snikos.nikoleris@arm.com             !pkt->req->isCacheMaintenance(),
36011744Snikos.nikoleris@arm.com             "%s got snoop %s where needsWritable, "
36111863Snikos.nikoleris@arm.com             "does not match isInvalidate", name(), pkt->print());
36211279Sandreas.hansson@arm.com
3634910SN/A    if (!inService || (pkt->isExpressSnoop() && downstreamPending)) {
3644903SN/A        // Request has not been issued yet, or it's been issued
3654903SN/A        // locally but is buffered unissued at some downstream cache
3664903SN/A        // which is forwarding us this snoop.  Either way, the packet
3674903SN/A        // we're snooping logically precedes this MSHR's request, so
3684903SN/A        // the snoop has no impact on the MSHR, but must be processed
3694903SN/A        // in the standard way by the cache.  The only exception is
3704903SN/A        // that if we're an L2+ cache buffering an UpgradeReq from a
3714903SN/A        // higher-level cache, and the snoop is invalidating, then our
3724903SN/A        // buffered upgrades must be converted to read exclusives,
3734903SN/A        // since the upper-level cache no longer has a valid copy.
3744903SN/A        // That is, even though the upper-level cache got out on its
3754903SN/A        // local bus first, some other invalidating transaction
3764903SN/A        // reached the global bus before the upgrade did.
37712350Snikos.nikoleris@arm.com        if (pkt->needsWritable() || pkt->req->isCacheInvalidate()) {
3789725Sandreas.hansson@arm.com            targets.replaceUpgrades();
3799725Sandreas.hansson@arm.com            deferredTargets.replaceUpgrades();
3804903SN/A        }
3814903SN/A
3824902SN/A        return false;
3834902SN/A    }
3844665SN/A
3854903SN/A    // From here on down, the request issued by this MSHR logically
3864903SN/A    // precedes the request we're snooping.
38712350Snikos.nikoleris@arm.com    if (pkt->needsWritable() || pkt->req->isCacheInvalidate()) {
3884903SN/A        // snooped request still precedes the re-request we'll have to
3894903SN/A        // issue for deferred targets, if any...
3909725Sandreas.hansson@arm.com        deferredTargets.replaceUpgrades();
3914903SN/A    }
3924903SN/A
39312350Snikos.nikoleris@arm.com    PacketPtr tgt_pkt = targets.front().pkt;
39412350Snikos.nikoleris@arm.com    if (hasPostInvalidate() || tgt_pkt->req->isCacheInvalidate()) {
39512350Snikos.nikoleris@arm.com        // a prior snoop has already appended an invalidation or a
39612350Snikos.nikoleris@arm.com        // cache invalidation operation is in progress, so logically
39712350Snikos.nikoleris@arm.com        // we don't have the block anymore; no need for further
39812350Snikos.nikoleris@arm.com        // snooping.
3994902SN/A        return true;
4004665SN/A    }
4014665SN/A
40211284Sandreas.hansson@arm.com    if (isPendingModified() || pkt->isInvalidate()) {
4037667Ssteve.reinhardt@amd.com        // We need to save and replay the packet in two cases:
40411284Sandreas.hansson@arm.com        // 1. We're awaiting a writable copy (Modified or Exclusive),
40511284Sandreas.hansson@arm.com        //    so this MSHR is the orgering point, and we need to respond
40611284Sandreas.hansson@arm.com        //    after we receive data.
4077667Ssteve.reinhardt@amd.com        // 2. It's an invalidation (e.g., UpgradeReq), and we need
4087667Ssteve.reinhardt@amd.com        //    to forward the snoop up the hierarchy after the current
4097667Ssteve.reinhardt@amd.com        //    transaction completes.
41010571Sandreas.hansson@arm.com
41111271Sandreas.hansson@arm.com        // Start by determining if we will eventually respond or not,
41211271Sandreas.hansson@arm.com        // matching the conditions checked in Cache::handleSnoop
41312350Snikos.nikoleris@arm.com        bool will_respond = isPendingModified() && pkt->needsResponse() &&
41412350Snikos.nikoleris@arm.com                      !pkt->isClean();
41511271Sandreas.hansson@arm.com
41611271Sandreas.hansson@arm.com        // The packet we are snooping may be deleted by the time we
41711271Sandreas.hansson@arm.com        // actually process the target, and we consequently need to
41811271Sandreas.hansson@arm.com        // save a copy here. Clear flags and also allocate new data as
41911271Sandreas.hansson@arm.com        // the original packet data storage may have been deleted by
42011271Sandreas.hansson@arm.com        // the time we get to process this packet. In the cases where
42111271Sandreas.hansson@arm.com        // we are not responding after handling the snoop we also need
42211271Sandreas.hansson@arm.com        // to create a copy of the request to be on the safe side. In
42311271Sandreas.hansson@arm.com        // the latter case the cache is responsible for deleting both
42411271Sandreas.hansson@arm.com        // the packet and the request as part of handling the deferred
42511271Sandreas.hansson@arm.com        // snoop.
42611271Sandreas.hansson@arm.com        PacketPtr cp_pkt = will_respond ? new Packet(pkt, true, true) :
42712749Sgiacomo.travaglini@arm.com            new Packet(new Request(*pkt->req), pkt->cmd, blkSize, pkt->id);
42812749Sgiacomo.travaglini@arm.com
4294670SN/A        if (will_respond) {
43011490Sandreas.hansson@arm.com            // we are the ordering point, and will consequently
43111284Sandreas.hansson@arm.com            // respond, and depending on whether the packet
43211284Sandreas.hansson@arm.com            // needsWritable or not we either pass a Shared line or a
43311284Sandreas.hansson@arm.com            // Modified line
43411284Sandreas.hansson@arm.com            pkt->setCacheResponding();
43511284Sandreas.hansson@arm.com
43611284Sandreas.hansson@arm.com            // inform the cache hierarchy that this cache had the line
43711284Sandreas.hansson@arm.com            // in the Modified state, even if the response is passed
43811284Sandreas.hansson@arm.com            // as Shared (and thus non-writable)
43911284Sandreas.hansson@arm.com            pkt->setResponderHadWritable();
44011284Sandreas.hansson@arm.com
44111284Sandreas.hansson@arm.com            // in the case of an uncacheable request there is no need
44210821Sandreas.hansson@arm.com            // to set the responderHadWritable flag, but since the
44311284Sandreas.hansson@arm.com            // recipient does not care there is no harm in doing so
44411284Sandreas.hansson@arm.com        }
4454670SN/A        targets.add(cp_pkt, curTick(), _order, Target::FromSnoop,
44610826Sstephan.diestelhorst@ARM.com                    downstreamPending && targets.needsWritable, false);
44711741Snikos.nikoleris@arm.com
4484670SN/A        if (pkt->needsWritable() || pkt->isInvalidate()) {
44912350Snikos.nikoleris@arm.com            // This transaction will take away our pending copy
4504670SN/A            postInvalidate = true;
4517667Ssteve.reinhardt@amd.com        }
4524670SN/A
45312350Snikos.nikoleris@arm.com        if (isPendingModified() && pkt->isClean()) {
45412501Snikos.nikoleris@arm.com            pkt->setSatisfied();
45512350Snikos.nikoleris@arm.com        }
45612350Snikos.nikoleris@arm.com    }
4577667Ssteve.reinhardt@amd.com
4587667Ssteve.reinhardt@amd.com    if (!pkt->needsWritable() && !pkt->req->isUncacheable()) {
45911284Sandreas.hansson@arm.com        // This transaction will get a read-shared copy, downgrading
4607667Ssteve.reinhardt@amd.com        // our copy if we had a writable one
46111284Sandreas.hansson@arm.com        postDowngrade = true;
4627667Ssteve.reinhardt@amd.com        // make sure that any downstream cache does not respond with a
46311284Sandreas.hansson@arm.com        // writable (and dirty) copy even if it has one, unless it was
46411284Sandreas.hansson@arm.com        // explicitly asked for one
46511284Sandreas.hansson@arm.com        pkt->setHasSharers();
46611284Sandreas.hansson@arm.com    }
4674667SN/A
4684902SN/A    return true;
4694902SN/A}
4704665SN/A
4714665SN/AMSHR::TargetList
47211742Snikos.nikoleris@arm.comMSHR::extractServiceableTargets(PacketPtr pkt)
47311742Snikos.nikoleris@arm.com{
47411742Snikos.nikoleris@arm.com    TargetList ready_targets;
47511742Snikos.nikoleris@arm.com    // If the downstream MSHR got an invalidation request then we only
47611742Snikos.nikoleris@arm.com    // service the first of the FromCPU targets and any other
47711742Snikos.nikoleris@arm.com    // non-FromCPU target. This way the remaining FromCPU targets
47811742Snikos.nikoleris@arm.com    // issue a new request and get a fresh copy of the block and we
47911742Snikos.nikoleris@arm.com    // avoid memory consistency violations.
48011742Snikos.nikoleris@arm.com    if (pkt->cmd == MemCmd::ReadRespWithInvalidate) {
48111742Snikos.nikoleris@arm.com        auto it = targets.begin();
48211742Snikos.nikoleris@arm.com        assert((it->source == Target::FromCPU) ||
48311866Ssascha.bischoff@arm.com               (it->source == Target::FromPrefetcher));
48411866Ssascha.bischoff@arm.com        ready_targets.push_back(*it);
48511742Snikos.nikoleris@arm.com        it = targets.erase(it);
48611742Snikos.nikoleris@arm.com        while (it != targets.end()) {
48711742Snikos.nikoleris@arm.com            if (it->source == Target::FromCPU) {
48811742Snikos.nikoleris@arm.com                it++;
48911742Snikos.nikoleris@arm.com            } else {
49011742Snikos.nikoleris@arm.com                assert(it->source == Target::FromSnoop);
49111742Snikos.nikoleris@arm.com                ready_targets.push_back(*it);
49211742Snikos.nikoleris@arm.com                it = targets.erase(it);
49311742Snikos.nikoleris@arm.com            }
49411742Snikos.nikoleris@arm.com        }
49511742Snikos.nikoleris@arm.com        ready_targets.populateFlags();
49611742Snikos.nikoleris@arm.com    } else {
49711742Snikos.nikoleris@arm.com        std::swap(ready_targets, targets);
49811742Snikos.nikoleris@arm.com    }
49911742Snikos.nikoleris@arm.com    targets.populateFlags();
50011742Snikos.nikoleris@arm.com
50111742Snikos.nikoleris@arm.com    return ready_targets;
50211742Snikos.nikoleris@arm.com}
50311742Snikos.nikoleris@arm.com
5044665SN/Abool
5054665SN/AMSHR::promoteDeferredTargets()
5064665SN/A{
5074665SN/A    if (targets.empty() && deferredTargets.empty()) {
50812350Snikos.nikoleris@arm.com        // nothing to promote
50912350Snikos.nikoleris@arm.com        return false;
51012350Snikos.nikoleris@arm.com    }
5114665SN/A
5124665SN/A    // the deferred targets can be generally promoted unless they
51312350Snikos.nikoleris@arm.com    // contain a cache maintenance request
51412350Snikos.nikoleris@arm.com
5154903SN/A    // find the first target that is a cache maintenance request
51612350Snikos.nikoleris@arm.com    auto it = std::find_if(deferredTargets.begin(), deferredTargets.end(),
51712350Snikos.nikoleris@arm.com                           [](MSHR::Target &t) {
51812350Snikos.nikoleris@arm.com                               return t.pkt->req->isCacheMaintenance();
51912350Snikos.nikoleris@arm.com                           });
52012350Snikos.nikoleris@arm.com    if (it == deferredTargets.begin()) {
52112350Snikos.nikoleris@arm.com        // if the first deferred target is a cache maintenance packet
52212350Snikos.nikoleris@arm.com        // then we can promote provided the targets list is empty and
52312350Snikos.nikoleris@arm.com        // we can service it on its own
52412350Snikos.nikoleris@arm.com        if (targets.empty()) {
52512350Snikos.nikoleris@arm.com            targets.splice(targets.end(), deferredTargets, it);
52612350Snikos.nikoleris@arm.com        }
52712350Snikos.nikoleris@arm.com    } else {
52812350Snikos.nikoleris@arm.com        // if a cache maintenance operation exists, we promote all the
52912350Snikos.nikoleris@arm.com        // deferred targets that precede it, or all deferred targets
53012350Snikos.nikoleris@arm.com        // otherwise
53112350Snikos.nikoleris@arm.com        targets.splice(targets.end(), deferredTargets,
53212350Snikos.nikoleris@arm.com                       deferredTargets.begin(), it);
53312350Snikos.nikoleris@arm.com    }
53412350Snikos.nikoleris@arm.com
53512350Snikos.nikoleris@arm.com    deferredTargets.populateFlags();
53612350Snikos.nikoleris@arm.com    targets.populateFlags();
53712350Snikos.nikoleris@arm.com    order = targets.front().order;
5389725Sandreas.hansson@arm.com    readyTime = std::max(curTick(), targets.front().readyTime);
5399725Sandreas.hansson@arm.com
5404665SN/A    return true;
5414665SN/A}
5422810SN/A
5432810SN/A
5442810SN/Avoid
5452810SN/AMSHR::promoteWritable()
54611284Sandreas.hansson@arm.com{
5474668SN/A    if (deferredTargets.needsWritable &&
54811284Sandreas.hansson@arm.com        !(hasPostInvalidate() || hasPostDowngrade())) {
54911177Sandreas.hansson@arm.com        // We got a writable response, but we have deferred targets
55011284Sandreas.hansson@arm.com        // which are waiting to request a writable copy (not because
55111284Sandreas.hansson@arm.com        // of a pending invalidate).  This can happen if the original
5525270SN/A        // request was for a read-only block, but we got a writable
55311284Sandreas.hansson@arm.com        // response anyway. Since we got the writable copy there's no
55411284Sandreas.hansson@arm.com        // need to defer the targets, so move them up to the regular
55511284Sandreas.hansson@arm.com        // target list.
55611284Sandreas.hansson@arm.com        assert(!targets.needsWritable);
55711284Sandreas.hansson@arm.com        targets.needsWritable = true;
55811284Sandreas.hansson@arm.com        // if any of the deferred targets were upper-level cache
5595318SN/A        // requests marked downstreamPending, need to clear that
5605318SN/A        assert(!downstreamPending);  // not pending here anymore
5615318SN/A        deferredTargets.clearDownstreamPending();
5629725Sandreas.hansson@arm.com        // this clears out deferredTargets too
5635270SN/A        targets.splice(targets.end(), deferredTargets);
5649725Sandreas.hansson@arm.com        deferredTargets.resetFlags();
5659725Sandreas.hansson@arm.com    }
5665270SN/A}
5674668SN/A
5684668SN/A
5694668SN/Abool
5705314SN/AMSHR::checkFunctional(PacketPtr pkt)
5715314SN/A{
5725314SN/A    // For printing, we treat the MSHR as a whole as single entity.
5735314SN/A    // For other requests, we iterate over the individual targets
5745314SN/A    // since that's where the actual data lies.
5755314SN/A    if (pkt->isPrint()) {
5765314SN/A        pkt->checkFunctional(this, blkAddr, isSecure, blkSize, nullptr);
57711484Snikos.nikoleris@arm.com        return false;
5785314SN/A    } else {
5795314SN/A        return (targets.checkFunctional(pkt) ||
5809725Sandreas.hansson@arm.com                deferredTargets.checkFunctional(pkt));
5819725Sandreas.hansson@arm.com    }
5825314SN/A}
5835314SN/A
5845314SN/Abool
58511375Sandreas.hansson@arm.comMSHR::sendPacket(BaseCache &cache)
58612724Snikos.nikoleris@arm.com{
58711375Sandreas.hansson@arm.com    return cache.sendMSHRQueuePacket(this);
58811375Sandreas.hansson@arm.com}
58911375Sandreas.hansson@arm.com
5905314SN/Avoid
5914668SN/AMSHR::print(std::ostream &os, int verbosity, const std::string &prefix) const
5925314SN/A{
5932810SN/A    ccprintf(os, "%s[%#llx:%#llx](%s) %s %s %s state: %s %s %s %s %s %s\n",
59412715Snikos.nikoleris@arm.com             prefix, blkAddr, blkAddr + blkSize - 1,
59510764Sandreas.hansson@arm.com             isSecure ? "s" : "ns",
59610028SGiacomo.Gabrielli@arm.com             isForward ? "Forward" : "",
5975730SSteve.Reinhardt@amd.com             allocOnFill() ? "AllocOnFill" : "",
59811741Snikos.nikoleris@arm.com             needsWritable() ? "Wrtbl" : "",
59911284Sandreas.hansson@arm.com             _isUncacheable ? "Unc" : "",
6005314SN/A             inService ? "InSvc" : "",
6015314SN/A             downstreamPending ? "DwnPend" : "",
6025314SN/A             postInvalidate ? "PostInv" : "",
60311610Snikos.nikoleris@arm.com             postDowngrade ? "PostDowngr" : "",
60412715Snikos.nikoleris@arm.com             hasFromCache() ? "HasFromCache" : "");
60512715Snikos.nikoleris@arm.com
6062810SN/A    if (!targets.empty()) {
60711610Snikos.nikoleris@arm.com        ccprintf(os, "%s  Targets:\n", prefix);
60811610Snikos.nikoleris@arm.com        targets.print(os, verbosity, prefix + "    ");
60911610Snikos.nikoleris@arm.com    }
61011610Snikos.nikoleris@arm.com    if (!deferredTargets.empty()) {
6119725Sandreas.hansson@arm.com        ccprintf(os, "%s  Deferred Targets:\n", prefix);
6125314SN/A        deferredTargets.print(os, verbosity, prefix + "      ");
6139725Sandreas.hansson@arm.com    }
6142810SN/A}
6152810SN/A
6162810SN/Astd::string
6179663Suri.wiener@arm.comMSHR::print() const
6189663Suri.wiener@arm.com{
6199663Suri.wiener@arm.com    std::ostringstream str;
62012637Sodanrc@yahoo.com.br    print(str);
6219663Suri.wiener@arm.com    return str.str();
6229663Suri.wiener@arm.com}
6239663Suri.wiener@arm.com