mshr.cc revision 8232
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 "debug/Cache.hh" 46#include "mem/cache/cache.hh" 47#include "mem/cache/mshr.hh" 48#include "sim/core.hh" 49 50using namespace std; 51 52MSHR::MSHR() 53{ 54 inService = false; 55 ntargets = 0; 56 threadNum = InvalidThreadID; 57 targets = new TargetList(); 58 deferredTargets = new TargetList(); 59} 60 61 62MSHR::TargetList::TargetList() 63 : needsExclusive(false), hasUpgrade(false) 64{} 65 66 67inline void 68MSHR::TargetList::add(PacketPtr pkt, Tick readyTime, 69 Counter order, Target::Source source, bool markPending) 70{ 71 if (source != Target::FromSnoop) { 72 if (pkt->needsExclusive()) { 73 needsExclusive = true; 74 } 75 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 78 // read-only copy 79 if (pkt->isUpgrade() || pkt->cmd == MemCmd::StoreCondReq) { 80 hasUpgrade = true; 81 } 82 } 83 84 if (markPending) { 85 MSHR *mshr = dynamic_cast<MSHR*>(pkt->senderState); 86 if (mshr != NULL) { 87 assert(!mshr->downstreamPending); 88 mshr->downstreamPending = true; 89 } 90 } 91 92 push_back(Target(pkt, readyTime, order, source, markPending)); 93} 94 95 96static void 97replaceUpgrade(PacketPtr pkt) 98{ 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"); 108 } 109} 110 111 112void 113MSHR::TargetList::replaceUpgrades() 114{ 115 if (!hasUpgrade) 116 return; 117 118 Iterator end_i = end(); 119 for (Iterator i = begin(); i != end_i; ++i) { 120 replaceUpgrade(i->pkt); 121 } 122 123 hasUpgrade = false; 124} 125 126 127void 128MSHR::TargetList::clearDownstreamPending() 129{ 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); 134 if (mshr != NULL) { 135 mshr->clearDownstreamPending(); 136 } 137 } 138 } 139} 140 141 142bool 143MSHR::TargetList::checkFunctional(PacketPtr pkt) 144{ 145 Iterator end_i = end(); 146 for (Iterator i = begin(); i != end_i; ++i) { 147 if (pkt->checkFunctional(i->pkt)) { 148 return true; 149 } 150 } 151 152 return false; 153} 154 155 156void 157MSHR::TargetList:: 158print(std::ostream &os, int verbosity, const std::string &prefix) const 159{ 160 ConstIterator end_i = end(); 161 for (ConstIterator i = begin(); i != end_i; ++i) { 162 const char *s; 163 switch (i->source) { 164 case Target::FromCPU: s = "FromCPU"; 165 case Target::FromSnoop: s = "FromSnoop"; 166 case Target::FromPrefetcher: s = "FromPrefetcher"; 167 default: s = ""; 168 } 169 ccprintf(os, "%s%s: ", prefix, s); 170 i->pkt->print(os, verbosity, ""); 171 } 172} 173 174 175void 176MSHR::allocate(Addr _addr, int _size, PacketPtr target, 177 Tick whenReady, Counter _order) 178{ 179 addr = _addr; 180 size = _size; 181 readyTime = whenReady; 182 order = _order; 183 assert(target); 184 isForward = false; 185 _isUncacheable = target->req->isUncacheable(); 186 inService = false; 187 downstreamPending = false; 188 threadNum = 0; 189 ntargets = 1; 190 assert(targets->isReset()); 191 // Don't know of a case where we would allocate a new MSHR for a 192 // snoop (mem-side request), so set source according to request here 193 Target::Source source = (target->cmd == MemCmd::HardPFReq) ? 194 Target::FromPrefetcher : Target::FromCPU; 195 targets->add(target, whenReady, _order, source, true); 196 assert(deferredTargets->isReset()); 197 data = NULL; 198} 199 200 201void 202MSHR::clearDownstreamPending() 203{ 204 assert(downstreamPending); 205 downstreamPending = false; 206 // recursively clear flag on any MSHRs we will be forwarding 207 // responses to 208 targets->clearDownstreamPending(); 209} 210 211bool 212MSHR::markInService(PacketPtr pkt) 213{ 214 assert(!inService); 215 if (isForwardNoResponse()) { 216 // we just forwarded the request packet & don't expect a 217 // response, so get rid of it 218 assert(getNumTargets() == 1); 219 popTarget(); 220 return true; 221 } 222 inService = true; 223 pendingDirty = (targets->needsExclusive || 224 (!pkt->sharedAsserted() && pkt->memInhibitAsserted())); 225 postInvalidate = postDowngrade = false; 226 227 if (!downstreamPending) { 228 // let upstream caches know that the request has made it to a 229 // level where it's going to get a response 230 targets->clearDownstreamPending(); 231 } 232 return false; 233} 234 235 236void 237MSHR::deallocate() 238{ 239 assert(targets->empty()); 240 targets->resetFlags(); 241 assert(deferredTargets->isReset()); 242 assert(ntargets == 0); 243 inService = false; 244} 245 246/* 247 * Adds a target to an MSHR 248 */ 249void 250MSHR::allocateTarget(PacketPtr pkt, Tick whenReady, Counter _order) 251{ 252 // if there's a request already in service for this MSHR, we will 253 // have to defer the new target until after the response if any of 254 // the following are true: 255 // - there are other targets already deferred 256 // - there's a pending invalidate to be applied after the response 257 // comes back (but before this target is processed) 258 // - this target requires an exclusive block and either we're not 259 // getting an exclusive block back or we have already snooped 260 // another read request that will downgrade our exclusive block 261 // to shared 262 263 // assume we'd never issue a prefetch when we've got an 264 // outstanding miss 265 assert(pkt->cmd != MemCmd::HardPFReq); 266 267 if (inService && 268 (!deferredTargets->empty() || hasPostInvalidate() || 269 (pkt->needsExclusive() && 270 (!isPendingDirty() || hasPostDowngrade() || isForward)))) { 271 // need to put on deferred list 272 if (hasPostInvalidate()) 273 replaceUpgrade(pkt); 274 deferredTargets->add(pkt, whenReady, _order, Target::FromCPU, true); 275 } else { 276 // No request outstanding, or still OK to append to 277 // outstanding request: append to regular target list. Only 278 // mark pending if current request hasn't been issued yet 279 // (isn't in service). 280 targets->add(pkt, whenReady, _order, Target::FromCPU, !inService); 281 } 282 283 ++ntargets; 284} 285 286bool 287MSHR::handleSnoop(PacketPtr pkt, Counter _order) 288{ 289 if (!inService || (pkt->isExpressSnoop() && downstreamPending)) { 290 // Request has not been issued yet, or it's been issued 291 // locally but is buffered unissued at some downstream cache 292 // which is forwarding us this snoop. Either way, the packet 293 // we're snooping logically precedes this MSHR's request, so 294 // the snoop has no impact on the MSHR, but must be processed 295 // in the standard way by the cache. The only exception is 296 // that if we're an L2+ cache buffering an UpgradeReq from a 297 // higher-level cache, and the snoop is invalidating, then our 298 // buffered upgrades must be converted to read exclusives, 299 // since the upper-level cache no longer has a valid copy. 300 // That is, even though the upper-level cache got out on its 301 // local bus first, some other invalidating transaction 302 // reached the global bus before the upgrade did. 303 if (pkt->needsExclusive()) { 304 targets->replaceUpgrades(); 305 deferredTargets->replaceUpgrades(); 306 } 307 308 return false; 309 } 310 311 // From here on down, the request issued by this MSHR logically 312 // precedes the request we're snooping. 313 if (pkt->needsExclusive()) { 314 // snooped request still precedes the re-request we'll have to 315 // issue for deferred targets, if any... 316 deferredTargets->replaceUpgrades(); 317 } 318 319 if (hasPostInvalidate()) { 320 // a prior snoop has already appended an invalidation, so 321 // logically we don't have the block anymore; no need for 322 // further snooping. 323 return true; 324 } 325 326 if (isPendingDirty() || pkt->isInvalidate()) { 327 // We need to save and replay the packet in two cases: 328 // 1. We're awaiting an exclusive copy, so ownership is pending, 329 // and we need to respond after we receive data. 330 // 2. It's an invalidation (e.g., UpgradeReq), and we need 331 // to forward the snoop up the hierarchy after the current 332 // transaction completes. 333 334 // Actual target device (typ. PhysicalMemory) will delete the 335 // packet on reception, so we need to save a copy here. 336 PacketPtr cp_pkt = new Packet(pkt, true); 337 targets->add(cp_pkt, curTick(), _order, Target::FromSnoop, 338 downstreamPending && targets->needsExclusive); 339 ++ntargets; 340 341 if (isPendingDirty()) { 342 pkt->assertMemInhibit(); 343 pkt->setSupplyExclusive(); 344 } 345 346 if (pkt->needsExclusive()) { 347 // This transaction will take away our pending copy 348 postInvalidate = true; 349 } 350 } 351 352 if (!pkt->needsExclusive()) { 353 // This transaction will get a read-shared copy, downgrading 354 // our copy if we had an exclusive one 355 postDowngrade = true; 356 pkt->assertShared(); 357 } 358 359 return true; 360} 361 362 363bool 364MSHR::promoteDeferredTargets() 365{ 366 assert(targets->empty()); 367 if (deferredTargets->empty()) { 368 return false; 369 } 370 371 // swap targets & deferredTargets lists 372 TargetList *tmp = targets; 373 targets = deferredTargets; 374 deferredTargets = tmp; 375 376 assert(targets->size() == ntargets); 377 378 // clear deferredTargets flags 379 deferredTargets->resetFlags(); 380 381 order = targets->front().order; 382 readyTime = std::max(curTick(), targets->front().readyTime); 383 384 return true; 385} 386 387 388void 389MSHR::handleFill(Packet *pkt, CacheBlk *blk) 390{ 391 if (!pkt->sharedAsserted() 392 && !(hasPostInvalidate() || hasPostDowngrade()) 393 && deferredTargets->needsExclusive) { 394 // We got an exclusive response, but we have deferred targets 395 // which are waiting to request an exclusive copy (not because 396 // of a pending invalidate). This can happen if the original 397 // request was for a read-only (non-exclusive) block, but we 398 // got an exclusive copy anyway because of the E part of the 399 // MOESI/MESI protocol. Since we got the exclusive copy 400 // there's no need to defer the targets, so move them up to 401 // the regular target list. 402 assert(!targets->needsExclusive); 403 targets->needsExclusive = true; 404 // if any of the deferred targets were upper-level cache 405 // requests marked downstreamPending, need to clear that 406 assert(!downstreamPending); // not pending here anymore 407 deferredTargets->clearDownstreamPending(); 408 // this clears out deferredTargets too 409 targets->splice(targets->end(), *deferredTargets); 410 deferredTargets->resetFlags(); 411 } 412} 413 414 415bool 416MSHR::checkFunctional(PacketPtr pkt) 417{ 418 // For printing, we treat the MSHR as a whole as single entity. 419 // For other requests, we iterate over the individual targets 420 // since that's where the actual data lies. 421 if (pkt->isPrint()) { 422 pkt->checkFunctional(this, addr, size, NULL); 423 return false; 424 } else { 425 return (targets->checkFunctional(pkt) || 426 deferredTargets->checkFunctional(pkt)); 427 } 428} 429 430 431void 432MSHR::print(std::ostream &os, int verbosity, const std::string &prefix) const 433{ 434 ccprintf(os, "%s[%x:%x] %s %s %s state: %s %s %s %s\n", 435 prefix, addr, addr+size-1, 436 isForward ? "Forward" : "", 437 isForwardNoResponse() ? "ForwNoResp" : "", 438 needsExclusive() ? "Excl" : "", 439 _isUncacheable ? "Unc" : "", 440 inService ? "InSvc" : "", 441 downstreamPending ? "DwnPend" : "", 442 hasPostInvalidate() ? "PostInv" : "", 443 hasPostDowngrade() ? "PostDowngr" : ""); 444 445 ccprintf(os, "%s Targets:\n", prefix); 446 targets->print(os, verbosity, prefix + " "); 447 if (!deferredTargets->empty()) { 448 ccprintf(os, "%s Deferred Targets:\n", prefix); 449 deferredTargets->print(os, verbosity, prefix + " "); 450 } 451} 452 453MSHR::~MSHR() 454{ 455} 456