54977346f51c7ec60dc7d0bacb3ced3f5abb698b
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 "mem/cache/cache.hh"
46 #include "mem/cache/mshr.hh"
47 #include "sim/core.hh"
55 threadNum
= InvalidThreadID
;
56 targets
= new TargetList();
57 deferredTargets
= new TargetList();
61 MSHR::TargetList::TargetList()
62 : needsExclusive(false), hasUpgrade(false)
67 MSHR::TargetList::add(PacketPtr pkt
, Tick readyTime
,
68 Counter order
, Target::Source source
, bool markPending
)
70 if (source
!= Target::FromSnoop
) {
71 if (pkt
->needsExclusive()) {
72 needsExclusive
= true;
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
78 if (pkt
->isUpgrade() || pkt
->cmd
== MemCmd::StoreCondReq
) {
84 MSHR
*mshr
= dynamic_cast<MSHR
*>(pkt
->senderState
);
86 assert(!mshr
->downstreamPending
);
87 mshr
->downstreamPending
= true;
91 push_back(Target(pkt
, readyTime
, order
, source
, markPending
));
96 replaceUpgrade(PacketPtr pkt
)
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");
112 MSHR::TargetList::replaceUpgrades()
117 Iterator end_i
= end();
118 for (Iterator i
= begin(); i
!= end_i
; ++i
) {
119 replaceUpgrade(i
->pkt
);
127 MSHR::TargetList::clearDownstreamPending()
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
);
134 mshr
->clearDownstreamPending();
142 MSHR::TargetList::checkFunctional(PacketPtr pkt
)
144 Iterator end_i
= end();
145 for (Iterator i
= begin(); i
!= end_i
; ++i
) {
146 if (pkt
->checkFunctional(i
->pkt
)) {
157 print(std::ostream
&os
, int verbosity
, const std::string
&prefix
) const
159 ConstIterator end_i
= end();
160 for (ConstIterator i
= begin(); i
!= end_i
; ++i
) {
163 case Target::FromCPU
: s
= "FromCPU";
164 case Target::FromSnoop
: s
= "FromSnoop";
165 case Target::FromPrefetcher
: s
= "FromPrefetcher";
168 ccprintf(os
, "%s%s: ", prefix
, s
);
169 i
->pkt
->print(os
, verbosity
, "");
175 MSHR::allocate(Addr _addr
, int _size
, PacketPtr target
,
176 Tick whenReady
, Counter _order
)
180 readyTime
= whenReady
;
184 _isUncacheable
= target
->req
->isUncacheable();
186 downstreamPending
= false;
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());
201 MSHR::clearDownstreamPending()
203 assert(downstreamPending
);
204 downstreamPending
= false;
205 // recursively clear flag on any MSHRs we will be forwarding
207 targets
->clearDownstreamPending();
211 MSHR::markInService(PacketPtr pkt
)
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);
222 pendingDirty
= (targets
->needsExclusive
||
223 (!pkt
->sharedAsserted() && pkt
->memInhibitAsserted()));
224 postInvalidate
= postDowngrade
= false;
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();
238 assert(targets
->empty());
239 targets
->resetFlags();
240 assert(deferredTargets
->isReset());
241 assert(ntargets
== 0);
246 * Adds a target to an MSHR
249 MSHR::allocateTarget(PacketPtr pkt
, Tick whenReady
, Counter _order
)
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
262 // assume we'd never issue a prefetch when we've got an
264 assert(pkt
->cmd
!= MemCmd::HardPFReq
);
267 (!deferredTargets
->empty() || hasPostInvalidate() ||
268 (pkt
->needsExclusive() &&
269 (!isPendingDirty() || hasPostDowngrade() || isForward
)))) {
270 // need to put on deferred list
271 if (hasPostInvalidate())
273 deferredTargets
->add(pkt
, whenReady
, _order
, Target::FromCPU
, true);
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
);
286 MSHR::handleSnoop(PacketPtr pkt
, Counter _order
)
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();
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();
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
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.
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
);
340 if (isPendingDirty()) {
341 pkt
->assertMemInhibit();
342 pkt
->setSupplyExclusive();
345 if (pkt
->needsExclusive()) {
346 // This transaction will take away our pending copy
347 postInvalidate
= true;
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;
363 MSHR::promoteDeferredTargets()
365 assert(targets
->empty());
366 if (deferredTargets
->empty()) {
370 // swap targets & deferredTargets lists
371 TargetList
*tmp
= targets
;
372 targets
= deferredTargets
;
373 deferredTargets
= tmp
;
375 assert(targets
->size() == ntargets
);
377 // clear deferredTargets flags
378 deferredTargets
->resetFlags();
380 order
= targets
->front().order
;
381 readyTime
= std::max(curTick
, targets
->front().readyTime
);
388 MSHR::handleFill(Packet
*pkt
, CacheBlk
*blk
)
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();
415 MSHR::checkFunctional(PacketPtr pkt
)
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
);
424 return (targets
->checkFunctional(pkt
) ||
425 deferredTargets
->checkFunctional(pkt
));
431 MSHR::print(std::ostream
&os
, int verbosity
, const std::string
&prefix
) const
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" : "");
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
+ " ");