mshr.cc revision 4668
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Erik Hallnor
29 *          Dave Greene
30 */
31
32/**
33 * @file
34 * Miss Status and Handling Register (MSHR) definitions.
35 */
36
37#include <assert.h>
38#include <string>
39#include <vector>
40#include <algorithm>
41
42#include "mem/cache/miss/mshr.hh"
43#include "sim/core.hh" // for curTick
44#include "sim/host.hh"
45#include "base/misc.hh"
46#include "mem/cache/cache.hh"
47
48using namespace std;
49
50MSHR::MSHR()
51{
52    inService = false;
53    ntargets = 0;
54    threadNum = -1;
55}
56
57void
58MSHR::allocate(Addr _addr, int _size, PacketPtr target,
59               Tick when, Counter _order)
60{
61    addr = _addr;
62    size = _size;
63    readyTick = when;
64    order = _order;
65    assert(target);
66    isCacheFill = false;
67    needsExclusive = target->needsExclusive();
68    _isUncacheable = target->req->isUncacheable();
69    inService = false;
70    threadNum = 0;
71    ntargets = 1;
72    // Don't know of a case where we would allocate a new MSHR for a
73    // snoop (mem-side request), so set cpuSide to true here.
74    targets.push_back(Target(target, when, _order, true));
75    assert(deferredTargets.empty());
76    deferredNeedsExclusive = false;
77    pendingInvalidate = false;
78    replacedPendingUpgrade = false;
79    data = NULL;
80}
81
82void
83MSHR::deallocate()
84{
85    assert(targets.empty());
86    assert(deferredTargets.empty());
87    assert(ntargets == 0);
88    inService = false;
89    //allocIter = NULL;
90    //readyIter = NULL;
91}
92
93/*
94 * Adds a target to an MSHR
95 */
96void
97MSHR::allocateTarget(PacketPtr target, Tick when, 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, when, _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, when, _order, true));
110        }
111    } else {
112        if (target->needsExclusive()) {
113            needsExclusive = true;
114        }
115
116        targets.push_back(Target(target, when, _order, true));
117    }
118
119    ++ntargets;
120}
121
122void
123MSHR::allocateSnoopTarget(PacketPtr target, Tick when, 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    if (needsExclusive) {
134        // We're awaiting an exclusive copy, so ownership is pending.
135        // It's up to us to respond once the data arrives.
136        target->assertMemInhibit();
137    }
138
139    if (target->needsExclusive()) {
140        // This transaction will take away our pending copy
141        pendingInvalidate = true;
142    } else {
143        // We'll keep our pending copy, but we can't let the other guy
144        // think he's getting it exclusive
145        target->assertShared();
146    }
147
148    targets.push_back(Target(target, when, _order, false));
149    ++ntargets;
150}
151
152
153bool
154MSHR::promoteDeferredTargets()
155{
156    if (deferredTargets.empty()) {
157        return false;
158    }
159
160    assert(targets.empty());
161    targets = deferredTargets;
162    deferredTargets.clear();
163    assert(targets.size() == ntargets);
164
165    needsExclusive = deferredNeedsExclusive;
166    pendingInvalidate = false;
167    deferredNeedsExclusive = false;
168    order = targets.front().order;
169    readyTick = std::max(curTick, targets.front().time);
170
171    return true;
172}
173
174
175void
176MSHR::handleReplacement(CacheBlk *blk, int blkSize)
177{
178    // must be an outstanding upgrade request on block we're about to
179    // replace...
180    assert(!blk->isWritable());
181    assert(needsExclusive);
182    replacedPendingUpgrade = true;
183
184    // if it's dirty, just remember what happened and allow the
185    // writeback to continue.  we'll reissue a ReadEx later whether
186    // the upgrade succeeds or not
187    if (blk->isDirty()) {
188        replacedPendingUpgradeDirty = true;
189        return;
190    }
191
192    // if not dirty, we need to save it off as it will be only valid
193    // copy in system if upgrade is successful (and may need to be
194    // written back then, as the current owner if any will be
195    // invalidating its block)
196    replacedPendingUpgradeDirty = false;
197    data = new uint8_t[blkSize];
198    std::memcpy(data, blk->data, blkSize);
199}
200
201
202bool
203MSHR::handleReplacedPendingUpgrade(Packet *pkt)
204{
205    // @TODO: if upgrade is nacked and replacedPendingUpgradeDirty is true, then we need to writeback the data (or rel
206    assert(pkt->cmd == MemCmd::UpgradeResp);
207    assert(replacedPendingUpgrade);
208    replacedPendingUpgrade = false; // reset
209    if (replacedPendingUpgradeDirty) {
210        // we wrote back the previous copy; just reissue as a ReadEx
211        return false;
212    }
213
214    // previous copy was not dirty, but we are now owner...  fake out
215    // cache by taking saved data and converting UpgradeResp to
216    // ReadExResp
217    assert(data);
218    pkt->cmd = MemCmd::ReadExResp;
219    pkt->setData(data);
220    delete [] data;
221    data = NULL;
222    return true;
223}
224
225
226void
227MSHR::dump()
228{
229    ccprintf(cerr,
230             "inService: %d thread: %d\n"
231             "Addr: %x ntargets %d\n"
232             "Targets:\n",
233             inService, threadNum, addr, ntargets);
234
235    TargetListIterator tar_it = targets.begin();
236    for (int i = 0; i < ntargets; i++) {
237        assert(tar_it != targets.end());
238
239        ccprintf(cerr, "\t%d: Addr: %x cmd: %s\n",
240                 i, tar_it->pkt->getAddr(), tar_it->pkt->cmdString());
241
242        tar_it++;
243    }
244    ccprintf(cerr, "\n");
245}
246
247MSHR::~MSHR()
248{
249}
250