mshr.cc revision 4908
1/* 2 * Copyright (c) 2002-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Erik Hallnor 29 * Dave Greene 30 */ 31 32/** 33 * @file 34 * Miss Status and Handling Register (MSHR) definitions. 35 */ 36 37#include <assert.h> 38#include <string> 39#include <vector> 40#include <algorithm> 41 42#include "mem/cache/miss/mshr.hh" 43#include "sim/core.hh" // for curTick 44#include "sim/host.hh" 45#include "base/misc.hh" 46#include "mem/cache/cache.hh" 47 48using namespace std; 49 50MSHR::MSHR() 51{ 52 inService = false; 53 ntargets = 0; 54 threadNum = -1; 55 targets = new TargetList(); 56 deferredTargets = new TargetList(); 57} 58 59 60MSHR::TargetList::TargetList() 61 : needsExclusive(false), hasUpgrade(false) 62{} 63 64 65inline void 66MSHR::TargetList::add(PacketPtr pkt, Tick readyTime, 67 Counter order, bool cpuSide) 68{ 69 if (cpuSide) { 70 if (pkt->needsExclusive()) { 71 needsExclusive = true; 72 } 73 74 if (pkt->cmd == MemCmd::UpgradeReq) { 75 hasUpgrade = true; 76 } 77 78 MSHR *mshr = dynamic_cast<MSHR*>(pkt->senderState); 79 if (mshr != NULL) { 80 assert(!mshr->downstreamPending); 81 mshr->downstreamPending = true; 82 } 83 } 84 85 push_back(Target(pkt, readyTime, order, cpuSide)); 86} 87 88 89void 90MSHR::TargetList::replaceUpgrades() 91{ 92 if (!hasUpgrade) 93 return; 94 95 Iterator end_i = end(); 96 for (Iterator i = begin(); i != end_i; ++i) { 97 if (i->pkt->cmd == MemCmd::UpgradeReq) { 98 i->pkt->cmd = MemCmd::ReadExReq; 99 DPRINTF(Cache, "Replacing UpgradeReq with ReadExReq\n"); 100 } 101 } 102 103 hasUpgrade = false; 104} 105 106 107void 108MSHR::TargetList::clearDownstreamPending() 109{ 110 Iterator end_i = end(); 111 for (Iterator i = begin(); i != end_i; ++i) { 112 MSHR *mshr = dynamic_cast<MSHR*>(i->pkt->senderState); 113 if (mshr != NULL) { 114 assert(mshr->downstreamPending); 115 mshr->downstreamPending = false; 116 } 117 } 118} 119 120 121void 122MSHR::allocate(Addr _addr, int _size, PacketPtr target, 123 Tick whenReady, Counter _order) 124{ 125 addr = _addr; 126 size = _size; 127 readyTime = whenReady; 128 order = _order; 129 assert(target); 130 isCacheFill = false; 131 _isUncacheable = target->req->isUncacheable(); 132 inService = false; 133 downstreamPending = false; 134 threadNum = 0; 135 ntargets = 1; 136 // Don't know of a case where we would allocate a new MSHR for a 137 // snoop (mem-side request), so set cpuSide to true here. 138 assert(targets->isReset()); 139 targets->add(target, whenReady, _order, true); 140 assert(deferredTargets->isReset()); 141 pendingInvalidate = false; 142 pendingShared = false; 143 data = NULL; 144} 145 146 147bool 148MSHR::markInService() 149{ 150 assert(!inService); 151 if (isSimpleForward()) { 152 // we just forwarded the request packet & don't expect a 153 // response, so get rid of it 154 assert(getNumTargets() == 1); 155 popTarget(); 156 return true; 157 } 158 inService = true; 159 if (!downstreamPending) { 160 // let upstream caches know that the request has made it to a 161 // level where it's going to get a response 162 targets->clearDownstreamPending(); 163 } 164 return false; 165} 166 167 168void 169MSHR::deallocate() 170{ 171 assert(targets->empty()); 172 targets->resetFlags(); 173 assert(deferredTargets->isReset()); 174 assert(ntargets == 0); 175 inService = false; 176 //allocIter = NULL; 177 //readyIter = NULL; 178} 179 180/* 181 * Adds a target to an MSHR 182 */ 183void 184MSHR::allocateTarget(PacketPtr pkt, Tick whenReady, Counter _order) 185{ 186 // if there's a request already in service for this MSHR, we will 187 // have to defer the new target until after the response if any of 188 // the following are true: 189 // - there are other targets already deferred 190 // - there's a pending invalidate to be applied after the response 191 // comes back (but before this target is processed) 192 // - the outstanding request is for a non-exclusive block and this 193 // target requires an exclusive block 194 if (inService && 195 (!deferredTargets->empty() || pendingInvalidate || 196 (!targets->needsExclusive && pkt->needsExclusive()))) { 197 // need to put on deferred list 198 deferredTargets->add(pkt, whenReady, _order, true); 199 } else { 200 // no request outstanding, or still OK to append to 201 // outstanding request 202 targets->add(pkt, whenReady, _order, true); 203 } 204 205 ++ntargets; 206} 207 208bool 209MSHR::handleSnoop(PacketPtr pkt, Counter _order) 210{ 211 if (!inService || downstreamPending) { 212 // Request has not been issued yet, or it's been issued 213 // locally but is buffered unissued at some downstream cache 214 // which is forwarding us this snoop. Either way, the packet 215 // we're snooping logically precedes this MSHR's request, so 216 // the snoop has no impact on the MSHR, but must be processed 217 // in the standard way by the cache. The only exception is 218 // that if we're an L2+ cache buffering an UpgradeReq from a 219 // higher-level cache, and the snoop is invalidating, then our 220 // buffered upgrades must be converted to read exclusives, 221 // since the upper-level cache no longer has a valid copy. 222 // That is, even though the upper-level cache got out on its 223 // local bus first, some other invalidating transaction 224 // reached the global bus before the upgrade did. 225 if (pkt->needsExclusive()) { 226 targets->replaceUpgrades(); 227 deferredTargets->replaceUpgrades(); 228 } 229 230 return false; 231 } 232 233 // From here on down, the request issued by this MSHR logically 234 // precedes the request we're snooping. 235 236 if (pkt->needsExclusive()) { 237 // snooped request still precedes the re-request we'll have to 238 // issue for deferred targets, if any... 239 deferredTargets->replaceUpgrades(); 240 } 241 242 if (pendingInvalidate) { 243 // a prior snoop has already appended an invalidation, so 244 // logically we don't have the block anymore; no need for 245 // further snooping. 246 return true; 247 } 248 249 if (targets->needsExclusive || pkt->needsExclusive()) { 250 // actual target device (typ. PhysicalMemory) will delete the 251 // packet on reception, so we need to save a copy here 252 targets->add(new Packet(pkt), curTick, _order, false); 253 ++ntargets; 254 255 if (targets->needsExclusive) { 256 // We're awaiting an exclusive copy, so ownership is pending. 257 // It's up to us to respond once the data arrives. 258 pkt->assertMemInhibit(); 259 } 260 261 if (pkt->needsExclusive()) { 262 // This transaction will take away our pending copy 263 pendingInvalidate = true; 264 } 265 } else { 266 // Read to a read: no conflict, so no need to record as 267 // target, but make sure neither reader thinks he's getting an 268 // exclusive copy 269 pendingShared = true; 270 pkt->assertShared(); 271 } 272 273 return true; 274} 275 276 277bool 278MSHR::promoteDeferredTargets() 279{ 280 assert(targets->empty()); 281 if (deferredTargets->empty()) { 282 return false; 283 } 284 285 // swap targets & deferredTargets lists 286 TargetList *tmp = targets; 287 targets = deferredTargets; 288 deferredTargets = tmp; 289 290 assert(targets->size() == ntargets); 291 292 // clear deferredTargets flags 293 deferredTargets->resetFlags(); 294 295 pendingInvalidate = false; 296 pendingShared = false; 297 order = targets->front().order; 298 readyTime = std::max(curTick, targets->front().readyTime); 299 300 return true; 301} 302 303 304void 305MSHR::handleFill(Packet *pkt, CacheBlk *blk) 306{ 307 if (pendingShared) { 308 // we snooped another read while this read was in 309 // service... assert shared line on its behalf 310 pkt->assertShared(); 311 } 312} 313 314 315void 316MSHR::dump() 317{ 318 ccprintf(cerr, 319 "inService: %d thread: %d\n" 320 "Addr: %x ntargets %d\n" 321 "Targets:\n", 322 inService, threadNum, addr, ntargets); 323#if 0 324 TargetListIterator tar_it = targets->begin(); 325 for (int i = 0; i < ntargets; i++) { 326 assert(tar_it != targets->end()); 327 328 ccprintf(cerr, "\t%d: Addr: %x cmd: %s\n", 329 i, tar_it->pkt->getAddr(), tar_it->pkt->cmdString()); 330 331 tar_it++; 332 } 333#endif 334 ccprintf(cerr, "\n"); 335} 336 337MSHR::~MSHR() 338{ 339} 340