mshr.cc revision 7823:dac01f14f20f
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * Copyright (c) 2010 Advanced Micro Devices, Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Erik Hallnor
30 *          Dave Greene
31 */
32
33/**
34 * @file
35 * Miss Status and Handling Register (MSHR) definitions.
36 */
37
38#include <algorithm>
39#include <cassert>
40#include <string>
41#include <vector>
42
43#include "base/misc.hh"
44#include "base/types.hh"
45#include "mem/cache/cache.hh"
46#include "mem/cache/mshr.hh"
47#include "sim/core.hh"
48
49using namespace std;
50
51MSHR::MSHR()
52{
53    inService = false;
54    ntargets = 0;
55    threadNum = InvalidThreadID;
56    targets = new TargetList();
57    deferredTargets = new TargetList();
58}
59
60
61MSHR::TargetList::TargetList()
62    : needsExclusive(false), hasUpgrade(false)
63{}
64
65
66inline void
67MSHR::TargetList::add(PacketPtr pkt, Tick readyTime,
68                      Counter order, Target::Source source, bool markPending)
69{
70    if (source != Target::FromSnoop) {
71        if (pkt->needsExclusive()) {
72            needsExclusive = true;
73        }
74
75        // StoreCondReq is effectively an upgrade if it's in an MSHR
76        // since it would have been failed already if we didn't have a
77        // read-only copy
78        if (pkt->isUpgrade() || pkt->cmd == MemCmd::StoreCondReq) {
79            hasUpgrade = true;
80        }
81    }
82
83    if (markPending) {
84        MSHR *mshr = dynamic_cast<MSHR*>(pkt->senderState);
85        if (mshr != NULL) {
86            assert(!mshr->downstreamPending);
87            mshr->downstreamPending = true;
88        }
89    }
90
91    push_back(Target(pkt, readyTime, order, source, markPending));
92}
93
94
95static void
96replaceUpgrade(PacketPtr pkt)
97{
98    if (pkt->cmd == MemCmd::UpgradeReq) {
99        pkt->cmd = MemCmd::ReadExReq;
100        DPRINTF(Cache, "Replacing UpgradeReq with ReadExReq\n");
101    } else if (pkt->cmd == MemCmd::SCUpgradeReq) {
102        pkt->cmd = MemCmd::SCUpgradeFailReq;
103        DPRINTF(Cache, "Replacing SCUpgradeReq with SCUpgradeFailReq\n");
104    } else if (pkt->cmd == MemCmd::StoreCondReq) {
105        pkt->cmd = MemCmd::StoreCondFailReq;
106        DPRINTF(Cache, "Replacing StoreCondReq with StoreCondFailReq\n");
107    }
108}
109
110
111void
112MSHR::TargetList::replaceUpgrades()
113{
114    if (!hasUpgrade)
115        return;
116
117    Iterator end_i = end();
118    for (Iterator i = begin(); i != end_i; ++i) {
119        replaceUpgrade(i->pkt);
120    }
121
122    hasUpgrade = false;
123}
124
125
126void
127MSHR::TargetList::clearDownstreamPending()
128{
129    Iterator end_i = end();
130    for (Iterator i = begin(); i != end_i; ++i) {
131        if (i->markedPending) {
132            MSHR *mshr = dynamic_cast<MSHR*>(i->pkt->senderState);
133            if (mshr != NULL) {
134                mshr->clearDownstreamPending();
135            }
136        }
137    }
138}
139
140
141bool
142MSHR::TargetList::checkFunctional(PacketPtr pkt)
143{
144    Iterator end_i = end();
145    for (Iterator i = begin(); i != end_i; ++i) {
146        if (pkt->checkFunctional(i->pkt)) {
147            return true;
148        }
149    }
150
151    return false;
152}
153
154
155void
156MSHR::TargetList::
157print(std::ostream &os, int verbosity, const std::string &prefix) const
158{
159    ConstIterator end_i = end();
160    for (ConstIterator i = begin(); i != end_i; ++i) {
161        const char *s;
162        switch (i->source) {
163          case Target::FromCPU: s = "FromCPU";
164          case Target::FromSnoop: s = "FromSnoop";
165          case Target::FromPrefetcher: s = "FromPrefetcher";
166          default: s = "";
167        }
168        ccprintf(os, "%s%s: ", prefix, s);
169        i->pkt->print(os, verbosity, "");
170    }
171}
172
173
174void
175MSHR::allocate(Addr _addr, int _size, PacketPtr target,
176               Tick whenReady, Counter _order)
177{
178    addr = _addr;
179    size = _size;
180    readyTime = whenReady;
181    order = _order;
182    assert(target);
183    isForward = false;
184    _isUncacheable = target->req->isUncacheable();
185    inService = false;
186    downstreamPending = false;
187    threadNum = 0;
188    ntargets = 1;
189    assert(targets->isReset());
190    // Don't know of a case where we would allocate a new MSHR for a
191    // snoop (mem-side request), so set source according to request here
192    Target::Source source = (target->cmd == MemCmd::HardPFReq) ?
193        Target::FromPrefetcher : Target::FromCPU;
194    targets->add(target, whenReady, _order, source, true);
195    assert(deferredTargets->isReset());
196    data = NULL;
197}
198
199
200void
201MSHR::clearDownstreamPending()
202{
203    assert(downstreamPending);
204    downstreamPending = false;
205    // recursively clear flag on any MSHRs we will be forwarding
206    // responses to
207    targets->clearDownstreamPending();
208}
209
210bool
211MSHR::markInService(PacketPtr pkt)
212{
213    assert(!inService);
214    if (isForwardNoResponse()) {
215        // we just forwarded the request packet & don't expect a
216        // response, so get rid of it
217        assert(getNumTargets() == 1);
218        popTarget();
219        return true;
220    }
221    inService = true;
222    pendingDirty = (targets->needsExclusive ||
223                    (!pkt->sharedAsserted() && pkt->memInhibitAsserted()));
224    postInvalidate = postDowngrade = false;
225
226    if (!downstreamPending) {
227        // let upstream caches know that the request has made it to a
228        // level where it's going to get a response
229        targets->clearDownstreamPending();
230    }
231    return false;
232}
233
234
235void
236MSHR::deallocate()
237{
238    assert(targets->empty());
239    targets->resetFlags();
240    assert(deferredTargets->isReset());
241    assert(ntargets == 0);
242    inService = false;
243}
244
245/*
246 * Adds a target to an MSHR
247 */
248void
249MSHR::allocateTarget(PacketPtr pkt, Tick whenReady, Counter _order)
250{
251    // if there's a request already in service for this MSHR, we will
252    // have to defer the new target until after the response if any of
253    // the following are true:
254    // - there are other targets already deferred
255    // - there's a pending invalidate to be applied after the response
256    //   comes back (but before this target is processed)
257    // - this target requires an exclusive block and either we're not
258    //   getting an exclusive block back or we have already snooped
259    //   another read request that will downgrade our exclusive block
260    //   to shared
261
262    // assume we'd never issue a prefetch when we've got an
263    // outstanding miss
264    assert(pkt->cmd != MemCmd::HardPFReq);
265
266    if (inService &&
267        (!deferredTargets->empty() || hasPostInvalidate() ||
268         (pkt->needsExclusive() &&
269          (!isPendingDirty() || hasPostDowngrade() || isForward)))) {
270        // need to put on deferred list
271        if (hasPostInvalidate())
272            replaceUpgrade(pkt);
273        deferredTargets->add(pkt, whenReady, _order, Target::FromCPU, true);
274    } else {
275        // No request outstanding, or still OK to append to
276        // outstanding request: append to regular target list.  Only
277        // mark pending if current request hasn't been issued yet
278        // (isn't in service).
279        targets->add(pkt, whenReady, _order, Target::FromCPU, !inService);
280    }
281
282    ++ntargets;
283}
284
285bool
286MSHR::handleSnoop(PacketPtr pkt, Counter _order)
287{
288    if (!inService || (pkt->isExpressSnoop() && downstreamPending)) {
289        // Request has not been issued yet, or it's been issued
290        // locally but is buffered unissued at some downstream cache
291        // which is forwarding us this snoop.  Either way, the packet
292        // we're snooping logically precedes this MSHR's request, so
293        // the snoop has no impact on the MSHR, but must be processed
294        // in the standard way by the cache.  The only exception is
295        // that if we're an L2+ cache buffering an UpgradeReq from a
296        // higher-level cache, and the snoop is invalidating, then our
297        // buffered upgrades must be converted to read exclusives,
298        // since the upper-level cache no longer has a valid copy.
299        // That is, even though the upper-level cache got out on its
300        // local bus first, some other invalidating transaction
301        // reached the global bus before the upgrade did.
302        if (pkt->needsExclusive()) {
303            targets->replaceUpgrades();
304            deferredTargets->replaceUpgrades();
305        }
306
307        return false;
308    }
309
310    // From here on down, the request issued by this MSHR logically
311    // precedes the request we're snooping.
312    if (pkt->needsExclusive()) {
313        // snooped request still precedes the re-request we'll have to
314        // issue for deferred targets, if any...
315        deferredTargets->replaceUpgrades();
316    }
317
318    if (hasPostInvalidate()) {
319        // a prior snoop has already appended an invalidation, so
320        // logically we don't have the block anymore; no need for
321        // further snooping.
322        return true;
323    }
324
325    if (isPendingDirty() || pkt->isInvalidate()) {
326        // We need to save and replay the packet in two cases:
327        // 1. We're awaiting an exclusive copy, so ownership is pending,
328        //    and we need to respond after we receive data.
329        // 2. It's an invalidation (e.g., UpgradeReq), and we need
330        //    to forward the snoop up the hierarchy after the current
331        //    transaction completes.
332
333        // Actual target device (typ. PhysicalMemory) will delete the
334        // packet on reception, so we need to save a copy here.
335        PacketPtr cp_pkt = new Packet(pkt, true);
336        targets->add(cp_pkt, curTick(), _order, Target::FromSnoop,
337                     downstreamPending && targets->needsExclusive);
338        ++ntargets;
339
340        if (isPendingDirty()) {
341            pkt->assertMemInhibit();
342            pkt->setSupplyExclusive();
343        }
344
345        if (pkt->needsExclusive()) {
346            // This transaction will take away our pending copy
347            postInvalidate = true;
348        }
349    }
350
351    if (!pkt->needsExclusive()) {
352        // This transaction will get a read-shared copy, downgrading
353        // our copy if we had an exclusive one
354        postDowngrade = true;
355        pkt->assertShared();
356    }
357
358    return true;
359}
360
361
362bool
363MSHR::promoteDeferredTargets()
364{
365    assert(targets->empty());
366    if (deferredTargets->empty()) {
367        return false;
368    }
369
370    // swap targets & deferredTargets lists
371    TargetList *tmp = targets;
372    targets = deferredTargets;
373    deferredTargets = tmp;
374
375    assert(targets->size() == ntargets);
376
377    // clear deferredTargets flags
378    deferredTargets->resetFlags();
379
380    order = targets->front().order;
381    readyTime = std::max(curTick(), targets->front().readyTime);
382
383    return true;
384}
385
386
387void
388MSHR::handleFill(Packet *pkt, CacheBlk *blk)
389{
390    if (!pkt->sharedAsserted()
391        && !(hasPostInvalidate() || hasPostDowngrade())
392        && deferredTargets->needsExclusive) {
393        // We got an exclusive response, but we have deferred targets
394        // which are waiting to request an exclusive copy (not because
395        // of a pending invalidate).  This can happen if the original
396        // request was for a read-only (non-exclusive) block, but we
397        // got an exclusive copy anyway because of the E part of the
398        // MOESI/MESI protocol.  Since we got the exclusive copy
399        // there's no need to defer the targets, so move them up to
400        // the regular target list.
401        assert(!targets->needsExclusive);
402        targets->needsExclusive = true;
403        // if any of the deferred targets were upper-level cache
404        // requests marked downstreamPending, need to clear that
405        assert(!downstreamPending);  // not pending here anymore
406        deferredTargets->clearDownstreamPending();
407        // this clears out deferredTargets too
408        targets->splice(targets->end(), *deferredTargets);
409        deferredTargets->resetFlags();
410    }
411}
412
413
414bool
415MSHR::checkFunctional(PacketPtr pkt)
416{
417    // For printing, we treat the MSHR as a whole as single entity.
418    // For other requests, we iterate over the individual targets
419    // since that's where the actual data lies.
420    if (pkt->isPrint()) {
421        pkt->checkFunctional(this, addr, size, NULL);
422        return false;
423    } else {
424        return (targets->checkFunctional(pkt) ||
425                deferredTargets->checkFunctional(pkt));
426    }
427}
428
429
430void
431MSHR::print(std::ostream &os, int verbosity, const std::string &prefix) const
432{
433    ccprintf(os, "%s[%x:%x] %s %s %s state: %s %s %s %s\n",
434             prefix, addr, addr+size-1,
435             isForward ? "Forward" : "",
436             isForwardNoResponse() ? "ForwNoResp" : "",
437             needsExclusive() ? "Excl" : "",
438             _isUncacheable ? "Unc" : "",
439             inService ? "InSvc" : "",
440             downstreamPending ? "DwnPend" : "",
441             hasPostInvalidate() ? "PostInv" : "",
442             hasPostDowngrade() ? "PostDowngr" : "");
443
444    ccprintf(os, "%s  Targets:\n", prefix);
445    targets->print(os, verbosity, prefix + "    ");
446    if (!deferredTargets->empty()) {
447        ccprintf(os, "%s  Deferred Targets:\n", prefix);
448        deferredTargets->print(os, verbosity, prefix + "      ");
449    }
450}
451
452MSHR::~MSHR()
453{
454}
455