mshr.cc revision 4871
14202Sbinkertn@umich.edu/*
24202Sbinkertn@umich.edu * Copyright (c) 2002-2005 The Regents of The University of Michigan
34202Sbinkertn@umich.edu * All rights reserved.
44202Sbinkertn@umich.edu *
54202Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without
64202Sbinkertn@umich.edu * modification, are permitted provided that the following conditions are
74202Sbinkertn@umich.edu * met: redistributions of source code must retain the above copyright
84202Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer;
94202Sbinkertn@umich.edu * redistributions in binary form must reproduce the above copyright
104202Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the
114202Sbinkertn@umich.edu * documentation and/or other materials provided with the distribution;
124202Sbinkertn@umich.edu * neither the name of the copyright holders nor the names of its
134202Sbinkertn@umich.edu * contributors may be used to endorse or promote products derived from
144202Sbinkertn@umich.edu * this software without specific prior written permission.
154202Sbinkertn@umich.edu *
164202Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
174202Sbinkertn@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
184202Sbinkertn@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
194202Sbinkertn@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
204202Sbinkertn@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
214202Sbinkertn@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
224202Sbinkertn@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
234202Sbinkertn@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244202Sbinkertn@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254202Sbinkertn@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
264202Sbinkertn@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274202Sbinkertn@umich.edu *
284202Sbinkertn@umich.edu * Authors: Erik Hallnor
294202Sbinkertn@umich.edu *          Dave Greene
304202Sbinkertn@umich.edu */
314202Sbinkertn@umich.edu
324202Sbinkertn@umich.edu/**
335628Sgblack@eecs.umich.edu * @file
349157Sandreas.hansson@arm.com * Miss Status and Handling Register (MSHR) definitions.
354486Sbinkertn@umich.edu */
364776Sgblack@eecs.umich.edu
379793Sakash.bagdia@arm.com#include <assert.h>
389827Sakash.bagdia@arm.com#include <string>
394486Sbinkertn@umich.edu#include <vector>
408774Sgblack@eecs.umich.edu#include <algorithm>
414202Sbinkertn@umich.edu
424202Sbinkertn@umich.edu#include "mem/cache/miss/mshr.hh"
434202Sbinkertn@umich.edu#include "sim/core.hh" // for curTick
444202Sbinkertn@umich.edu#include "sim/host.hh"
455522Snate@binkert.org#include "base/misc.hh"
468233Snate@binkert.org#include "mem/cache/cache.hh"
474202Sbinkertn@umich.edu
484202Sbinkertn@umich.eduusing namespace std;
499342SAndreas.Sandberg@arm.com
504202Sbinkertn@umich.eduMSHR::MSHR()
514202Sbinkertn@umich.edu{
524202Sbinkertn@umich.edu    inService = false;
534202Sbinkertn@umich.edu    ntargets = 0;
548770Sgblack@eecs.umich.edu    threadNum = -1;
559793Sakash.bagdia@arm.com}
569827Sakash.bagdia@arm.com
577768SAli.Saidi@ARM.comvoid
587768SAli.Saidi@ARM.comMSHR::allocate(Addr _addr, int _size, PacketPtr target,
598766Sgblack@eecs.umich.edu               Tick whenReady, Counter _order)
607768SAli.Saidi@ARM.com{
617768SAli.Saidi@ARM.com    addr = _addr;
628766Sgblack@eecs.umich.edu    size = _size;
637768SAli.Saidi@ARM.com    readyTime = whenReady;
647768SAli.Saidi@ARM.com    order = _order;
654202Sbinkertn@umich.edu    assert(target);
668784Sgblack@eecs.umich.edu    isCacheFill = false;
675016Sgblack@eecs.umich.edu    needsExclusive = target->needsExclusive();
684486Sbinkertn@umich.edu    _isUncacheable = target->req->isUncacheable();
698335Snate@binkert.org    inService = false;
708335Snate@binkert.org    threadNum = 0;
719152Satgutier@umich.edu    ntargets = 1;
728335Snate@binkert.org    // Don't know of a case where we would allocate a new MSHR for a
738335Snate@binkert.org    // snoop (mem-side request), so set cpuSide to true here.
748335Snate@binkert.org    targets.push_back(Target(target, whenReady, _order, true));
758335Snate@binkert.org    assert(deferredTargets.empty());
768335Snate@binkert.org    deferredNeedsExclusive = false;
778335Snate@binkert.org    pendingInvalidate = false;
788335Snate@binkert.org    pendingShared = false;
799733Sandreas@sandberg.pp.se    data = NULL;
808335Snate@binkert.org}
818335Snate@binkert.org
828335Snate@binkert.orgvoid
838335Snate@binkert.orgMSHR::deallocate()
848335Snate@binkert.org{
858335Snate@binkert.org    assert(targets.empty());
868335Snate@binkert.org    assert(deferredTargets.empty());
878335Snate@binkert.org    assert(ntargets == 0);
889793Sakash.bagdia@arm.com    inService = false;
899827Sakash.bagdia@arm.com    //allocIter = NULL;
90    //readyIter = NULL;
91}
92
93/*
94 * Adds a target to an MSHR
95 */
96void
97MSHR::allocateTarget(PacketPtr target, Tick whenReady, Counter _order)
98{
99    if (inService) {
100        if (!deferredTargets.empty() || pendingInvalidate ||
101            (!needsExclusive && target->needsExclusive())) {
102            // need to put on deferred list
103            deferredTargets.push_back(Target(target, whenReady, _order, true));
104            if (target->needsExclusive()) {
105                deferredNeedsExclusive = true;
106            }
107        } else {
108            // still OK to append to outstanding request
109            targets.push_back(Target(target, whenReady, _order, true));
110        }
111    } else {
112        if (target->needsExclusive()) {
113            needsExclusive = true;
114        }
115
116        targets.push_back(Target(target, whenReady, _order, true));
117    }
118
119    ++ntargets;
120}
121
122void
123MSHR::allocateSnoopTarget(PacketPtr pkt, Tick whenReady, Counter _order)
124{
125    assert(inService); // don't bother to call otherwise
126
127    if (pendingInvalidate) {
128        // a prior snoop has already appended an invalidation, so
129        // logically we don't have the block anymore...
130        return;
131    }
132
133    DPRINTF(Cache, "deferred snoop on %x: %s %s\n", addr,
134            needsExclusive ? "needsExclusive" : "",
135            pkt->needsExclusive() ? "pkt->needsExclusive()" : "");
136
137    if (needsExclusive || pkt->needsExclusive()) {
138        // actual target device (typ. PhysicalMemory) will delete the
139        // packet on reception, so we need to save a copy here
140        targets.push_back(Target(new Packet(pkt), whenReady, _order, false));
141        ++ntargets;
142
143        if (needsExclusive) {
144            // We're awaiting an exclusive copy, so ownership is pending.
145            // It's up to us to respond once the data arrives.
146            pkt->assertMemInhibit();
147        }
148
149        if (pkt->needsExclusive()) {
150            // This transaction will take away our pending copy
151            pendingInvalidate = true;
152        }
153    } else {
154        // Read to a read: no conflict, so no need to record as
155        // target, but make sure neither reader thinks he's getting an
156        // exclusive copy
157        pendingShared = true;
158        pkt->assertShared();
159    }
160}
161
162
163bool
164MSHR::promoteDeferredTargets()
165{
166    if (deferredTargets.empty()) {
167        return false;
168    }
169
170    assert(targets.empty());
171    targets = deferredTargets;
172    deferredTargets.clear();
173    assert(targets.size() == ntargets);
174
175    needsExclusive = deferredNeedsExclusive;
176    pendingInvalidate = false;
177    pendingShared = false;
178    deferredNeedsExclusive = false;
179    order = targets.front().order;
180    readyTime = std::max(curTick, targets.front().readyTime);
181
182    return true;
183}
184
185
186void
187MSHR::handleFill(Packet *pkt, CacheBlk *blk)
188{
189    if (pendingShared) {
190        // we snooped another read while this read was in
191        // service... assert shared line on its behalf
192        pkt->assertShared();
193    }
194}
195
196
197void
198MSHR::dump()
199{
200    ccprintf(cerr,
201             "inService: %d thread: %d\n"
202             "Addr: %x ntargets %d\n"
203             "Targets:\n",
204             inService, threadNum, addr, ntargets);
205
206    TargetListIterator tar_it = targets.begin();
207    for (int i = 0; i < ntargets; i++) {
208        assert(tar_it != targets.end());
209
210        ccprintf(cerr, "\t%d: Addr: %x cmd: %s\n",
211                 i, tar_it->pkt->getAddr(), tar_it->pkt->cmdString());
212
213        tar_it++;
214    }
215    ccprintf(cerr, "\n");
216}
217
218MSHR::~MSHR()
219{
220}
221