mshr.cc revision 4671
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    pendingShared = 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 pkt, 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    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), when, _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    readyTick = std::max(curTick, targets.front().time);
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