mshr.cc revision 4920
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    targets = new TargetList();
56    deferredTargets = new TargetList();
57}
58
59
60MSHR::TargetList::TargetList()
61    : needsExclusive(false), hasUpgrade(false)
62{}
63
64
65inline void
66MSHR::TargetList::add(PacketPtr pkt, Tick readyTime,
67                      Counter order, bool cpuSide)
68{
69    if (cpuSide) {
70        if (pkt->needsExclusive()) {
71            needsExclusive = true;
72        }
73
74        if (pkt->cmd == MemCmd::UpgradeReq) {
75            hasUpgrade = true;
76        }
77
78        MSHR *mshr = dynamic_cast<MSHR*>(pkt->senderState);
79        if (mshr != NULL) {
80            assert(!mshr->downstreamPending);
81            mshr->downstreamPending = true;
82        }
83    }
84
85    push_back(Target(pkt, readyTime, order, cpuSide));
86}
87
88
89void
90MSHR::TargetList::replaceUpgrades()
91{
92    if (!hasUpgrade)
93        return;
94
95    Iterator end_i = end();
96    for (Iterator i = begin(); i != end_i; ++i) {
97        if (i->pkt->cmd == MemCmd::UpgradeReq) {
98            i->pkt->cmd = MemCmd::ReadExReq;
99            DPRINTF(Cache, "Replacing UpgradeReq with ReadExReq\n");
100        }
101    }
102
103    hasUpgrade = false;
104}
105
106
107void
108MSHR::TargetList::clearDownstreamPending()
109{
110    Iterator end_i = end();
111    for (Iterator i = begin(); i != end_i; ++i) {
112        MSHR *mshr = dynamic_cast<MSHR*>(i->pkt->senderState);
113        if (mshr != NULL) {
114            assert(mshr->downstreamPending);
115            mshr->downstreamPending = false;
116        }
117    }
118}
119
120
121bool
122MSHR::TargetList::checkFunctional(PacketPtr pkt)
123{
124    Iterator end_i = end();
125    for (Iterator i = begin(); i != end_i; ++i) {
126        if (pkt->checkFunctional(i->pkt)) {
127            return true;
128        }
129    }
130
131    return false;
132}
133
134
135void
136MSHR::allocate(Addr _addr, int _size, PacketPtr target,
137               Tick whenReady, Counter _order)
138{
139    addr = _addr;
140    size = _size;
141    readyTime = whenReady;
142    order = _order;
143    assert(target);
144    isCacheFill = false;
145    _isUncacheable = target->req->isUncacheable();
146    inService = false;
147    downstreamPending = false;
148    threadNum = 0;
149    ntargets = 1;
150    // Don't know of a case where we would allocate a new MSHR for a
151    // snoop (mem-side request), so set cpuSide to true here.
152    assert(targets->isReset());
153    targets->add(target, whenReady, _order, true);
154    assert(deferredTargets->isReset());
155    pendingInvalidate = false;
156    pendingShared = false;
157    data = NULL;
158}
159
160
161bool
162MSHR::markInService()
163{
164    assert(!inService);
165    if (isSimpleForward()) {
166        // we just forwarded the request packet & don't expect a
167        // response, so get rid of it
168        assert(getNumTargets() == 1);
169        popTarget();
170        return true;
171    }
172    inService = true;
173    if (!downstreamPending) {
174        // let upstream caches know that the request has made it to a
175        // level where it's going to get a response
176        targets->clearDownstreamPending();
177    }
178    return false;
179}
180
181
182void
183MSHR::deallocate()
184{
185    assert(targets->empty());
186    targets->resetFlags();
187    assert(deferredTargets->isReset());
188    assert(ntargets == 0);
189    inService = false;
190    //allocIter = NULL;
191    //readyIter = NULL;
192}
193
194/*
195 * Adds a target to an MSHR
196 */
197void
198MSHR::allocateTarget(PacketPtr pkt, Tick whenReady, Counter _order)
199{
200    // if there's a request already in service for this MSHR, we will
201    // have to defer the new target until after the response if any of
202    // the following are true:
203    // - there are other targets already deferred
204    // - there's a pending invalidate to be applied after the response
205    //   comes back (but before this target is processed)
206    // - the outstanding request is for a non-exclusive block and this
207    //   target requires an exclusive block
208    if (inService &&
209        (!deferredTargets->empty() || pendingInvalidate ||
210         (!targets->needsExclusive && pkt->needsExclusive()))) {
211        // need to put on deferred list
212        deferredTargets->add(pkt, whenReady, _order, true);
213    } else {
214        // no request outstanding, or still OK to append to
215        // outstanding request
216        targets->add(pkt, whenReady, _order, true);
217    }
218
219    ++ntargets;
220}
221
222bool
223MSHR::handleSnoop(PacketPtr pkt, Counter _order)
224{
225    if (!inService || (pkt->isExpressSnoop() && downstreamPending)) {
226        // Request has not been issued yet, or it's been issued
227        // locally but is buffered unissued at some downstream cache
228        // which is forwarding us this snoop.  Either way, the packet
229        // we're snooping logically precedes this MSHR's request, so
230        // the snoop has no impact on the MSHR, but must be processed
231        // in the standard way by the cache.  The only exception is
232        // that if we're an L2+ cache buffering an UpgradeReq from a
233        // higher-level cache, and the snoop is invalidating, then our
234        // buffered upgrades must be converted to read exclusives,
235        // since the upper-level cache no longer has a valid copy.
236        // That is, even though the upper-level cache got out on its
237        // local bus first, some other invalidating transaction
238        // reached the global bus before the upgrade did.
239        if (pkt->needsExclusive()) {
240            targets->replaceUpgrades();
241            deferredTargets->replaceUpgrades();
242        }
243
244        return false;
245    }
246
247    // From here on down, the request issued by this MSHR logically
248    // precedes the request we're snooping.
249
250    if (pkt->needsExclusive()) {
251        // snooped request still precedes the re-request we'll have to
252        // issue for deferred targets, if any...
253        deferredTargets->replaceUpgrades();
254    }
255
256    if (pendingInvalidate) {
257        // a prior snoop has already appended an invalidation, so
258        // logically we don't have the block anymore; no need for
259        // further snooping.
260        return true;
261    }
262
263    if (targets->needsExclusive || pkt->needsExclusive()) {
264        // actual target device (typ. PhysicalMemory) will delete the
265        // packet on reception, so we need to save a copy here
266        PacketPtr cp_pkt = new Packet(pkt);
267        targets->add(cp_pkt, curTick, _order, false);
268        ++ntargets;
269
270        if (targets->needsExclusive) {
271            // We're awaiting an exclusive copy, so ownership is pending.
272            // It's up to us to respond once the data arrives.
273            pkt->assertMemInhibit();
274            pkt->setSupplyExclusive();
275        } else {
276            // Someone else may respond before we get around to
277            // processing this snoop, which means the copied request
278            // pointer will no longer be valid
279            cp_pkt->req = NULL;
280        }
281
282        if (pkt->needsExclusive()) {
283            // This transaction will take away our pending copy
284            pendingInvalidate = true;
285        }
286    } else {
287        // Read to a read: no conflict, so no need to record as
288        // target, but make sure neither reader thinks he's getting an
289        // exclusive copy
290        pendingShared = true;
291        pkt->assertShared();
292    }
293
294    return true;
295}
296
297
298bool
299MSHR::promoteDeferredTargets()
300{
301    assert(targets->empty());
302    if (deferredTargets->empty()) {
303        return false;
304    }
305
306    // swap targets & deferredTargets lists
307    TargetList *tmp = targets;
308    targets = deferredTargets;
309    deferredTargets = tmp;
310
311    assert(targets->size() == ntargets);
312
313    // clear deferredTargets flags
314    deferredTargets->resetFlags();
315
316    pendingInvalidate = false;
317    pendingShared = false;
318    order = targets->front().order;
319    readyTime = std::max(curTick, targets->front().readyTime);
320
321    return true;
322}
323
324
325void
326MSHR::handleFill(Packet *pkt, CacheBlk *blk)
327{
328    if (pendingShared) {
329        // we snooped another read while this read was in
330        // service... assert shared line on its behalf
331        pkt->assertShared();
332    }
333}
334
335
336void
337MSHR::dump()
338{
339    ccprintf(cerr,
340             "inService: %d thread: %d\n"
341             "Addr: %x ntargets %d\n"
342             "Targets:\n",
343             inService, threadNum, addr, ntargets);
344#if 0
345    TargetListIterator tar_it = targets->begin();
346    for (int i = 0; i < ntargets; i++) {
347        assert(tar_it != targets->end());
348
349        ccprintf(cerr, "\t%d: Addr: %x cmd: %s\n",
350                 i, tar_it->pkt->getAddr(), tar_it->pkt->cmdString());
351
352        tar_it++;
353    }
354#endif
355    ccprintf(cerr, "\n");
356}
357
358MSHR::~MSHR()
359{
360}
361