mshr.cc revision 11741:72916416d2e2
1/* 2 * Copyright (c) 2012-2013, 2015-2016 ARM Limited 3 * All rights reserved. 4 * 5 * The license below extends only to copyright in the software and shall 6 * not be construed as granting a license to any other intellectual 7 * property including but not limited to intellectual property relating 8 * to a hardware implementation of the functionality of the software 9 * licensed hereunder. You may use the software subject to the license 10 * terms below provided that you ensure that this notice is replicated 11 * unmodified and in its entirety in all distributions of the software, 12 * modified or unmodified, in source code or in binary form. 13 * 14 * Copyright (c) 2002-2005 The Regents of The University of Michigan 15 * Copyright (c) 2010 Advanced Micro Devices, Inc. 16 * All rights reserved. 17 * 18 * Redistribution and use in source and binary forms, with or without 19 * modification, are permitted provided that the following conditions are 20 * met: redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer; 22 * redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution; 25 * neither the name of the copyright holders nor the names of its 26 * contributors may be used to endorse or promote products derived from 27 * this software without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Authors: Erik Hallnor 42 * Dave Greene 43 */ 44 45/** 46 * @file 47 * Miss Status and Handling Register (MSHR) definitions. 48 */ 49 50#include "mem/cache/mshr.hh" 51 52#include <algorithm> 53#include <cassert> 54#include <string> 55#include <vector> 56 57#include "base/misc.hh" 58#include "base/types.hh" 59#include "debug/Cache.hh" 60#include "mem/cache/cache.hh" 61#include "sim/core.hh" 62 63using namespace std; 64 65MSHR::MSHR() : downstreamPending(false), 66 pendingModified(false), 67 postInvalidate(false), postDowngrade(false), 68 isForward(false) 69{ 70} 71 72MSHR::TargetList::TargetList() 73 : needsWritable(false), hasUpgrade(false), allocOnFill(false) 74{} 75 76 77void 78MSHR::TargetList::updateFlags(PacketPtr pkt, Target::Source source, 79 bool alloc_on_fill) 80{ 81 if (source != Target::FromSnoop) { 82 if (pkt->needsWritable()) { 83 needsWritable = true; 84 } 85 86 // StoreCondReq is effectively an upgrade if it's in an MSHR 87 // since it would have been failed already if we didn't have a 88 // read-only copy 89 if (pkt->isUpgrade() || pkt->cmd == MemCmd::StoreCondReq) { 90 hasUpgrade = true; 91 } 92 93 // potentially re-evaluate whether we should allocate on a fill or 94 // not 95 allocOnFill = allocOnFill || alloc_on_fill; 96 } 97} 98 99void 100MSHR::TargetList::populateFlags() 101{ 102 resetFlags(); 103 for (auto& t: *this) { 104 updateFlags(t.pkt, t.source, t.allocOnFill); 105 } 106} 107 108inline void 109MSHR::TargetList::add(PacketPtr pkt, Tick readyTime, 110 Counter order, Target::Source source, bool markPending, 111 bool alloc_on_fill) 112{ 113 updateFlags(pkt, source, alloc_on_fill); 114 if (markPending) { 115 // Iterate over the SenderState stack and see if we find 116 // an MSHR entry. If we do, set the downstreamPending 117 // flag. Otherwise, do nothing. 118 MSHR *mshr = pkt->findNextSenderState<MSHR>(); 119 if (mshr != nullptr) { 120 assert(!mshr->downstreamPending); 121 mshr->downstreamPending = true; 122 } else { 123 // No need to clear downstreamPending later 124 markPending = false; 125 } 126 } 127 128 emplace_back(pkt, readyTime, order, source, markPending, alloc_on_fill); 129} 130 131 132static void 133replaceUpgrade(PacketPtr pkt) 134{ 135 // remember if the current packet has data allocated 136 bool has_data = pkt->hasData() || pkt->hasRespData(); 137 138 if (pkt->cmd == MemCmd::UpgradeReq) { 139 pkt->cmd = MemCmd::ReadExReq; 140 DPRINTF(Cache, "Replacing UpgradeReq with ReadExReq\n"); 141 } else if (pkt->cmd == MemCmd::SCUpgradeReq) { 142 pkt->cmd = MemCmd::SCUpgradeFailReq; 143 DPRINTF(Cache, "Replacing SCUpgradeReq with SCUpgradeFailReq\n"); 144 } else if (pkt->cmd == MemCmd::StoreCondReq) { 145 pkt->cmd = MemCmd::StoreCondFailReq; 146 DPRINTF(Cache, "Replacing StoreCondReq with StoreCondFailReq\n"); 147 } 148 149 if (!has_data) { 150 // there is no sensible way of setting the data field if the 151 // new command actually would carry data 152 assert(!pkt->hasData()); 153 154 if (pkt->hasRespData()) { 155 // we went from a packet that had no data (neither request, 156 // nor response), to one that does, and therefore we need to 157 // actually allocate space for the data payload 158 pkt->allocate(); 159 } 160 } 161} 162 163 164void 165MSHR::TargetList::replaceUpgrades() 166{ 167 if (!hasUpgrade) 168 return; 169 170 for (auto& t : *this) { 171 replaceUpgrade(t.pkt); 172 } 173 174 hasUpgrade = false; 175} 176 177 178void 179MSHR::TargetList::clearDownstreamPending() 180{ 181 for (auto& t : *this) { 182 if (t.markedPending) { 183 // Iterate over the SenderState stack and see if we find 184 // an MSHR entry. If we find one, clear the 185 // downstreamPending flag by calling 186 // clearDownstreamPending(). This recursively clears the 187 // downstreamPending flag in all caches this packet has 188 // passed through. 189 MSHR *mshr = t.pkt->findNextSenderState<MSHR>(); 190 if (mshr != nullptr) { 191 mshr->clearDownstreamPending(); 192 } 193 } 194 } 195} 196 197 198bool 199MSHR::TargetList::checkFunctional(PacketPtr pkt) 200{ 201 for (auto& t : *this) { 202 if (pkt->checkFunctional(t.pkt)) { 203 return true; 204 } 205 } 206 207 return false; 208} 209 210 211void 212MSHR::TargetList::print(std::ostream &os, int verbosity, 213 const std::string &prefix) const 214{ 215 for (auto& t : *this) { 216 const char *s; 217 switch (t.source) { 218 case Target::FromCPU: 219 s = "FromCPU"; 220 break; 221 case Target::FromSnoop: 222 s = "FromSnoop"; 223 break; 224 case Target::FromPrefetcher: 225 s = "FromPrefetcher"; 226 break; 227 default: 228 s = ""; 229 break; 230 } 231 ccprintf(os, "%s%s: ", prefix, s); 232 t.pkt->print(os, verbosity, ""); 233 } 234} 235 236 237void 238MSHR::allocate(Addr blk_addr, unsigned blk_size, PacketPtr target, 239 Tick when_ready, Counter _order, bool alloc_on_fill) 240{ 241 blkAddr = blk_addr; 242 blkSize = blk_size; 243 isSecure = target->isSecure(); 244 readyTime = when_ready; 245 order = _order; 246 assert(target); 247 isForward = false; 248 _isUncacheable = target->req->isUncacheable(); 249 inService = false; 250 downstreamPending = false; 251 assert(targets.isReset()); 252 // Don't know of a case where we would allocate a new MSHR for a 253 // snoop (mem-side request), so set source according to request here 254 Target::Source source = (target->cmd == MemCmd::HardPFReq) ? 255 Target::FromPrefetcher : Target::FromCPU; 256 targets.add(target, when_ready, _order, source, true, alloc_on_fill); 257 assert(deferredTargets.isReset()); 258} 259 260 261void 262MSHR::clearDownstreamPending() 263{ 264 assert(downstreamPending); 265 downstreamPending = false; 266 // recursively clear flag on any MSHRs we will be forwarding 267 // responses to 268 targets.clearDownstreamPending(); 269} 270 271void 272MSHR::markInService(bool pending_modified_resp) 273{ 274 assert(!inService); 275 276 inService = true; 277 pendingModified = targets.needsWritable || pending_modified_resp; 278 postInvalidate = postDowngrade = false; 279 280 if (!downstreamPending) { 281 // let upstream caches know that the request has made it to a 282 // level where it's going to get a response 283 targets.clearDownstreamPending(); 284 } 285} 286 287 288void 289MSHR::deallocate() 290{ 291 assert(targets.empty()); 292 targets.resetFlags(); 293 assert(deferredTargets.isReset()); 294 inService = false; 295} 296 297/* 298 * Adds a target to an MSHR 299 */ 300void 301MSHR::allocateTarget(PacketPtr pkt, Tick whenReady, Counter _order, 302 bool alloc_on_fill) 303{ 304 // assume we'd never issue a prefetch when we've got an 305 // outstanding miss 306 assert(pkt->cmd != MemCmd::HardPFReq); 307 308 // uncacheable accesses always allocate a new MSHR, and cacheable 309 // accesses ignore any uncacheable MSHRs, thus we should never 310 // have targets addded if originally allocated uncacheable 311 assert(!_isUncacheable); 312 313 // if there's a request already in service for this MSHR, we will 314 // have to defer the new target until after the response if any of 315 // the following are true: 316 // - there are other targets already deferred 317 // - there's a pending invalidate to be applied after the response 318 // comes back (but before this target is processed) 319 // - this target requires a writable block and either we're not 320 // getting a writable block back or we have already snooped 321 // another read request that will downgrade our writable block 322 // to non-writable (Shared or Owned) 323 if (inService && 324 (!deferredTargets.empty() || hasPostInvalidate() || 325 (pkt->needsWritable() && 326 (!isPendingModified() || hasPostDowngrade() || isForward)))) { 327 // need to put on deferred list 328 if (hasPostInvalidate()) 329 replaceUpgrade(pkt); 330 deferredTargets.add(pkt, whenReady, _order, Target::FromCPU, true, 331 alloc_on_fill); 332 } else { 333 // No request outstanding, or still OK to append to 334 // outstanding request: append to regular target list. Only 335 // mark pending if current request hasn't been issued yet 336 // (isn't in service). 337 targets.add(pkt, whenReady, _order, Target::FromCPU, !inService, 338 alloc_on_fill); 339 } 340} 341 342bool 343MSHR::handleSnoop(PacketPtr pkt, Counter _order) 344{ 345 DPRINTF(Cache, "%s for %s addr %#llx size %d\n", __func__, 346 pkt->cmdString(), pkt->getAddr(), pkt->getSize()); 347 348 // when we snoop packets the needsWritable and isInvalidate flags 349 // should always be the same, however, this assumes that we never 350 // snoop writes as they are currently not marked as invalidations 351 panic_if(pkt->needsWritable() != pkt->isInvalidate(), 352 "%s got snoop %s to addr %#llx where needsWritable, " 353 "does not match isInvalidate", name(), pkt->cmdString(), 354 pkt->getAddr()); 355 356 if (!inService || (pkt->isExpressSnoop() && downstreamPending)) { 357 // Request has not been issued yet, or it's been issued 358 // locally but is buffered unissued at some downstream cache 359 // which is forwarding us this snoop. Either way, the packet 360 // we're snooping logically precedes this MSHR's request, so 361 // the snoop has no impact on the MSHR, but must be processed 362 // in the standard way by the cache. The only exception is 363 // that if we're an L2+ cache buffering an UpgradeReq from a 364 // higher-level cache, and the snoop is invalidating, then our 365 // buffered upgrades must be converted to read exclusives, 366 // since the upper-level cache no longer has a valid copy. 367 // That is, even though the upper-level cache got out on its 368 // local bus first, some other invalidating transaction 369 // reached the global bus before the upgrade did. 370 if (pkt->needsWritable()) { 371 targets.replaceUpgrades(); 372 deferredTargets.replaceUpgrades(); 373 } 374 375 return false; 376 } 377 378 // From here on down, the request issued by this MSHR logically 379 // precedes the request we're snooping. 380 if (pkt->needsWritable()) { 381 // snooped request still precedes the re-request we'll have to 382 // issue for deferred targets, if any... 383 deferredTargets.replaceUpgrades(); 384 } 385 386 if (hasPostInvalidate()) { 387 // a prior snoop has already appended an invalidation, so 388 // logically we don't have the block anymore; no need for 389 // further snooping. 390 return true; 391 } 392 393 if (isPendingModified() || pkt->isInvalidate()) { 394 // We need to save and replay the packet in two cases: 395 // 1. We're awaiting a writable copy (Modified or Exclusive), 396 // so this MSHR is the orgering point, and we need to respond 397 // after we receive data. 398 // 2. It's an invalidation (e.g., UpgradeReq), and we need 399 // to forward the snoop up the hierarchy after the current 400 // transaction completes. 401 402 // Start by determining if we will eventually respond or not, 403 // matching the conditions checked in Cache::handleSnoop 404 bool will_respond = isPendingModified() && pkt->needsResponse() && 405 pkt->cmd != MemCmd::InvalidateReq; 406 407 // The packet we are snooping may be deleted by the time we 408 // actually process the target, and we consequently need to 409 // save a copy here. Clear flags and also allocate new data as 410 // the original packet data storage may have been deleted by 411 // the time we get to process this packet. In the cases where 412 // we are not responding after handling the snoop we also need 413 // to create a copy of the request to be on the safe side. In 414 // the latter case the cache is responsible for deleting both 415 // the packet and the request as part of handling the deferred 416 // snoop. 417 PacketPtr cp_pkt = will_respond ? new Packet(pkt, true, true) : 418 new Packet(new Request(*pkt->req), pkt->cmd); 419 420 if (will_respond) { 421 // we are the ordering point, and will consequently 422 // respond, and depending on whether the packet 423 // needsWritable or not we either pass a Shared line or a 424 // Modified line 425 pkt->setCacheResponding(); 426 427 // inform the cache hierarchy that this cache had the line 428 // in the Modified state, even if the response is passed 429 // as Shared (and thus non-writable) 430 pkt->setResponderHadWritable(); 431 432 // in the case of an uncacheable request there is no need 433 // to set the responderHadWritable flag, but since the 434 // recipient does not care there is no harm in doing so 435 } 436 targets.add(cp_pkt, curTick(), _order, Target::FromSnoop, 437 downstreamPending && targets.needsWritable, false); 438 439 if (pkt->needsWritable()) { 440 // This transaction will take away our pending copy 441 postInvalidate = true; 442 } 443 } 444 445 if (!pkt->needsWritable() && !pkt->req->isUncacheable()) { 446 // This transaction will get a read-shared copy, downgrading 447 // our copy if we had a writable one 448 postDowngrade = true; 449 // make sure that any downstream cache does not respond with a 450 // writable (and dirty) copy even if it has one, unless it was 451 // explicitly asked for one 452 pkt->setHasSharers(); 453 } 454 455 return true; 456} 457 458 459bool 460MSHR::promoteDeferredTargets() 461{ 462 assert(targets.empty()); 463 if (deferredTargets.empty()) { 464 return false; 465 } 466 467 // swap targets & deferredTargets lists 468 std::swap(targets, deferredTargets); 469 470 // clear deferredTargets flags 471 deferredTargets.resetFlags(); 472 473 order = targets.front().order; 474 readyTime = std::max(curTick(), targets.front().readyTime); 475 476 return true; 477} 478 479 480void 481MSHR::promoteWritable() 482{ 483 if (deferredTargets.needsWritable && 484 !(hasPostInvalidate() || hasPostDowngrade())) { 485 // We got a writable response, but we have deferred targets 486 // which are waiting to request a writable copy (not because 487 // of a pending invalidate). This can happen if the original 488 // request was for a read-only block, but we got a writable 489 // response anyway. Since we got the writable copy there's no 490 // need to defer the targets, so move them up to the regular 491 // target list. 492 assert(!targets.needsWritable); 493 targets.needsWritable = true; 494 // if any of the deferred targets were upper-level cache 495 // requests marked downstreamPending, need to clear that 496 assert(!downstreamPending); // not pending here anymore 497 deferredTargets.clearDownstreamPending(); 498 // this clears out deferredTargets too 499 targets.splice(targets.end(), deferredTargets); 500 deferredTargets.resetFlags(); 501 } 502} 503 504 505bool 506MSHR::checkFunctional(PacketPtr pkt) 507{ 508 // For printing, we treat the MSHR as a whole as single entity. 509 // For other requests, we iterate over the individual targets 510 // since that's where the actual data lies. 511 if (pkt->isPrint()) { 512 pkt->checkFunctional(this, blkAddr, isSecure, blkSize, nullptr); 513 return false; 514 } else { 515 return (targets.checkFunctional(pkt) || 516 deferredTargets.checkFunctional(pkt)); 517 } 518} 519 520bool 521MSHR::sendPacket(Cache &cache) 522{ 523 return cache.sendMSHRQueuePacket(this); 524} 525 526void 527MSHR::print(std::ostream &os, int verbosity, const std::string &prefix) const 528{ 529 ccprintf(os, "%s[%#llx:%#llx](%s) %s %s %s state: %s %s %s %s %s\n", 530 prefix, blkAddr, blkAddr + blkSize - 1, 531 isSecure ? "s" : "ns", 532 isForward ? "Forward" : "", 533 allocOnFill() ? "AllocOnFill" : "", 534 needsWritable() ? "Wrtbl" : "", 535 _isUncacheable ? "Unc" : "", 536 inService ? "InSvc" : "", 537 downstreamPending ? "DwnPend" : "", 538 postInvalidate ? "PostInv" : "", 539 postDowngrade ? "PostDowngr" : ""); 540 541 if (!targets.empty()) { 542 ccprintf(os, "%s Targets:\n", prefix); 543 targets.print(os, verbosity, prefix + " "); 544 } 545 if (!deferredTargets.empty()) { 546 ccprintf(os, "%s Deferred Targets:\n", prefix); 547 deferredTargets.print(os, verbosity, prefix + " "); 548 } 549} 550 551std::string 552MSHR::print() const 553{ 554 ostringstream str; 555 print(str); 556 return str.str(); 557} 558