2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * Copyright (c) 2010 Advanced Micro Devices, Inc.
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.
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.
29 * Authors: Erik Hallnor
35 * Miss Status and Handling Register (MSHR) definitions.
43 #include "base/misc.hh"
44 #include "base/types.hh"
45 #include "debug/Cache.hh"
46 #include "mem/cache/cache.hh"
47 #include "mem/cache/mshr.hh"
48 #include "sim/core.hh"
56 threadNum
= InvalidThreadID
;
57 targets
= new TargetList();
58 deferredTargets
= new TargetList();
62 MSHR::TargetList::TargetList()
63 : needsExclusive(false), hasUpgrade(false)
68 MSHR::TargetList::add(PacketPtr pkt
, Tick readyTime
,
69 Counter order
, Target::Source source
, bool markPending
)
71 if (source
!= Target::FromSnoop
) {
72 if (pkt
->needsExclusive()) {
73 needsExclusive
= true;
76 // StoreCondReq is effectively an upgrade if it's in an MSHR
77 // since it would have been failed already if we didn't have a
79 if (pkt
->isUpgrade() || pkt
->cmd
== MemCmd::StoreCondReq
) {
85 MSHR
*mshr
= dynamic_cast<MSHR
*>(pkt
->senderState
);
87 assert(!mshr
->downstreamPending
);
88 mshr
->downstreamPending
= true;
92 push_back(Target(pkt
, readyTime
, order
, source
, markPending
));
97 replaceUpgrade(PacketPtr pkt
)
99 if (pkt
->cmd
== MemCmd::UpgradeReq
) {
100 pkt
->cmd
= MemCmd::ReadExReq
;
101 DPRINTF(Cache
, "Replacing UpgradeReq with ReadExReq\n");
102 } else if (pkt
->cmd
== MemCmd::SCUpgradeReq
) {
103 pkt
->cmd
= MemCmd::SCUpgradeFailReq
;
104 DPRINTF(Cache
, "Replacing SCUpgradeReq with SCUpgradeFailReq\n");
105 } else if (pkt
->cmd
== MemCmd::StoreCondReq
) {
106 pkt
->cmd
= MemCmd::StoreCondFailReq
;
107 DPRINTF(Cache
, "Replacing StoreCondReq with StoreCondFailReq\n");
113 MSHR::TargetList::replaceUpgrades()
118 Iterator end_i
= end();
119 for (Iterator i
= begin(); i
!= end_i
; ++i
) {
120 replaceUpgrade(i
->pkt
);
128 MSHR::TargetList::clearDownstreamPending()
130 Iterator end_i
= end();
131 for (Iterator i
= begin(); i
!= end_i
; ++i
) {
132 if (i
->markedPending
) {
133 MSHR
*mshr
= dynamic_cast<MSHR
*>(i
->pkt
->senderState
);
135 mshr
->clearDownstreamPending();
143 MSHR::TargetList::checkFunctional(PacketPtr pkt
)
145 Iterator end_i
= end();
146 for (Iterator i
= begin(); i
!= end_i
; ++i
) {
147 if (pkt
->checkFunctional(i
->pkt
)) {
158 print(std::ostream
&os
, int verbosity
, const std::string
&prefix
) const
160 ConstIterator end_i
= end();
161 for (ConstIterator i
= begin(); i
!= end_i
; ++i
) {
164 case Target::FromCPU
:
167 case Target::FromSnoop
:
170 case Target::FromPrefetcher
:
171 s
= "FromPrefetcher";
177 ccprintf(os
, "%s%s: ", prefix
, s
);
178 i
->pkt
->print(os
, verbosity
, "");
184 MSHR::allocate(Addr _addr
, int _size
, PacketPtr target
,
185 Tick whenReady
, Counter _order
)
189 readyTime
= whenReady
;
193 _isUncacheable
= target
->req
->isUncacheable();
195 downstreamPending
= false;
198 assert(targets
->isReset());
199 // Don't know of a case where we would allocate a new MSHR for a
200 // snoop (mem-side request), so set source according to request here
201 Target::Source source
= (target
->cmd
== MemCmd::HardPFReq
) ?
202 Target::FromPrefetcher
: Target::FromCPU
;
203 targets
->add(target
, whenReady
, _order
, source
, true);
204 assert(deferredTargets
->isReset());
210 MSHR::clearDownstreamPending()
212 assert(downstreamPending
);
213 downstreamPending
= false;
214 // recursively clear flag on any MSHRs we will be forwarding
216 targets
->clearDownstreamPending();
220 MSHR::markInService(PacketPtr pkt
)
223 if (isForwardNoResponse()) {
224 // we just forwarded the request packet & don't expect a
225 // response, so get rid of it
226 assert(getNumTargets() == 1);
231 pendingDirty
= (targets
->needsExclusive
||
232 (!pkt
->sharedAsserted() && pkt
->memInhibitAsserted()));
233 postInvalidate
= postDowngrade
= false;
235 if (!downstreamPending
) {
236 // let upstream caches know that the request has made it to a
237 // level where it's going to get a response
238 targets
->clearDownstreamPending();
247 assert(targets
->empty());
248 targets
->resetFlags();
249 assert(deferredTargets
->isReset());
250 assert(ntargets
== 0);
255 * Adds a target to an MSHR
258 MSHR::allocateTarget(PacketPtr pkt
, Tick whenReady
, Counter _order
)
260 // if there's a request already in service for this MSHR, we will
261 // have to defer the new target until after the response if any of
262 // the following are true:
263 // - there are other targets already deferred
264 // - there's a pending invalidate to be applied after the response
265 // comes back (but before this target is processed)
266 // - this target requires an exclusive block and either we're not
267 // getting an exclusive block back or we have already snooped
268 // another read request that will downgrade our exclusive block
271 // assume we'd never issue a prefetch when we've got an
273 assert(pkt
->cmd
!= MemCmd::HardPFReq
);
276 (!deferredTargets
->empty() || hasPostInvalidate() ||
277 (pkt
->needsExclusive() &&
278 (!isPendingDirty() || hasPostDowngrade() || isForward
)))) {
279 // need to put on deferred list
280 if (hasPostInvalidate())
282 deferredTargets
->add(pkt
, whenReady
, _order
, Target::FromCPU
, true);
284 // No request outstanding, or still OK to append to
285 // outstanding request: append to regular target list. Only
286 // mark pending if current request hasn't been issued yet
287 // (isn't in service).
288 targets
->add(pkt
, whenReady
, _order
, Target::FromCPU
, !inService
);
295 MSHR::handleSnoop(PacketPtr pkt
, Counter _order
)
297 if (!inService
|| (pkt
->isExpressSnoop() && downstreamPending
)) {
298 // Request has not been issued yet, or it's been issued
299 // locally but is buffered unissued at some downstream cache
300 // which is forwarding us this snoop. Either way, the packet
301 // we're snooping logically precedes this MSHR's request, so
302 // the snoop has no impact on the MSHR, but must be processed
303 // in the standard way by the cache. The only exception is
304 // that if we're an L2+ cache buffering an UpgradeReq from a
305 // higher-level cache, and the snoop is invalidating, then our
306 // buffered upgrades must be converted to read exclusives,
307 // since the upper-level cache no longer has a valid copy.
308 // That is, even though the upper-level cache got out on its
309 // local bus first, some other invalidating transaction
310 // reached the global bus before the upgrade did.
311 if (pkt
->needsExclusive()) {
312 targets
->replaceUpgrades();
313 deferredTargets
->replaceUpgrades();
319 // From here on down, the request issued by this MSHR logically
320 // precedes the request we're snooping.
321 if (pkt
->needsExclusive()) {
322 // snooped request still precedes the re-request we'll have to
323 // issue for deferred targets, if any...
324 deferredTargets
->replaceUpgrades();
327 if (hasPostInvalidate()) {
328 // a prior snoop has already appended an invalidation, so
329 // logically we don't have the block anymore; no need for
334 if (isPendingDirty() || pkt
->isInvalidate()) {
335 // We need to save and replay the packet in two cases:
336 // 1. We're awaiting an exclusive copy, so ownership is pending,
337 // and we need to respond after we receive data.
338 // 2. It's an invalidation (e.g., UpgradeReq), and we need
339 // to forward the snoop up the hierarchy after the current
340 // transaction completes.
342 // Actual target device (typ. a memory) will delete the
343 // packet on reception, so we need to save a copy here.
344 PacketPtr cp_pkt
= new Packet(pkt
, true);
345 targets
->add(cp_pkt
, curTick(), _order
, Target::FromSnoop
,
346 downstreamPending
&& targets
->needsExclusive
);
349 if (isPendingDirty()) {
350 pkt
->assertMemInhibit();
351 pkt
->setSupplyExclusive();
354 if (pkt
->needsExclusive()) {
355 // This transaction will take away our pending copy
356 postInvalidate
= true;
360 if (!pkt
->needsExclusive()) {
361 // This transaction will get a read-shared copy, downgrading
362 // our copy if we had an exclusive one
363 postDowngrade
= true;
372 MSHR::promoteDeferredTargets()
374 assert(targets
->empty());
375 if (deferredTargets
->empty()) {
379 // swap targets & deferredTargets lists
380 TargetList
*tmp
= targets
;
381 targets
= deferredTargets
;
382 deferredTargets
= tmp
;
384 assert(targets
->size() == ntargets
);
386 // clear deferredTargets flags
387 deferredTargets
->resetFlags();
389 order
= targets
->front().order
;
390 readyTime
= std::max(curTick(), targets
->front().readyTime
);
397 MSHR::handleFill(Packet
*pkt
, CacheBlk
*blk
)
399 if (!pkt
->sharedAsserted()
400 && !(hasPostInvalidate() || hasPostDowngrade())
401 && deferredTargets
->needsExclusive
) {
402 // We got an exclusive response, but we have deferred targets
403 // which are waiting to request an exclusive copy (not because
404 // of a pending invalidate). This can happen if the original
405 // request was for a read-only (non-exclusive) block, but we
406 // got an exclusive copy anyway because of the E part of the
407 // MOESI/MESI protocol. Since we got the exclusive copy
408 // there's no need to defer the targets, so move them up to
409 // the regular target list.
410 assert(!targets
->needsExclusive
);
411 targets
->needsExclusive
= true;
412 // if any of the deferred targets were upper-level cache
413 // requests marked downstreamPending, need to clear that
414 assert(!downstreamPending
); // not pending here anymore
415 deferredTargets
->clearDownstreamPending();
416 // this clears out deferredTargets too
417 targets
->splice(targets
->end(), *deferredTargets
);
418 deferredTargets
->resetFlags();
424 MSHR::checkFunctional(PacketPtr pkt
)
426 // For printing, we treat the MSHR as a whole as single entity.
427 // For other requests, we iterate over the individual targets
428 // since that's where the actual data lies.
429 if (pkt
->isPrint()) {
430 pkt
->checkFunctional(this, addr
, size
, NULL
);
433 return (targets
->checkFunctional(pkt
) ||
434 deferredTargets
->checkFunctional(pkt
));
440 MSHR::print(std::ostream
&os
, int verbosity
, const std::string
&prefix
) const
442 ccprintf(os
, "%s[%x:%x] %s %s %s state: %s %s %s %s\n",
443 prefix
, addr
, addr
+size
-1,
444 isForward
? "Forward" : "",
445 isForwardNoResponse() ? "ForwNoResp" : "",
446 needsExclusive() ? "Excl" : "",
447 _isUncacheable
? "Unc" : "",
448 inService
? "InSvc" : "",
449 downstreamPending
? "DwnPend" : "",
450 hasPostInvalidate() ? "PostInv" : "",
451 hasPostDowngrade() ? "PostDowngr" : "");
453 ccprintf(os
, "%s Targets:\n", prefix
);
454 targets
->print(os
, verbosity
, prefix
+ " ");
455 if (!deferredTargets
->empty()) {
456 ccprintf(os
, "%s Deferred Targets:\n", prefix
);
457 deferredTargets
->print(os
, verbosity
, prefix
+ " ");
464 delete[] deferredTargets
;