xbar.cc revision 9611
1/* 2 * Copyright (c) 2011-2013 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) 2006 The Regents of The University of Michigan 15 * All rights reserved. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions are 19 * met: redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer; 21 * redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution; 24 * neither the name of the copyright holders nor the names of its 25 * contributors may be used to endorse or promote products derived from 26 * this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39 * 40 * Authors: Ali Saidi 41 * Andreas Hansson 42 * William Wang 43 */ 44 45/** 46 * @file 47 * Definition of a bus object. 48 */ 49 50#include "base/misc.hh" 51#include "base/trace.hh" 52#include "debug/Bus.hh" 53#include "debug/BusAddrRanges.hh" 54#include "debug/Drain.hh" 55#include "mem/bus.hh" 56 57BaseBus::BaseBus(const BaseBusParams *p) 58 : MemObject(p), 59 headerCycles(p->header_cycles), width(p->width), 60 gotAddrRanges(p->port_default_connection_count + 61 p->port_master_connection_count, false), 62 gotAllAddrRanges(false), defaultPortID(InvalidPortID), 63 useDefaultRange(p->use_default_range), 64 blockSize(p->block_size) 65{} 66 67BaseBus::~BaseBus() 68{ 69 for (MasterPortIter m = masterPorts.begin(); m != masterPorts.end(); 70 ++m) { 71 delete *m; 72 } 73 74 for (SlavePortIter s = slavePorts.begin(); s != slavePorts.end(); 75 ++s) { 76 delete *s; 77 } 78} 79 80void 81BaseBus::init() 82{ 83 // determine the maximum peer block size, look at both the 84 // connected master and slave modules 85 uint32_t peer_block_size = 0; 86 87 for (MasterPortConstIter m = masterPorts.begin(); m != masterPorts.end(); 88 ++m) { 89 peer_block_size = std::max((*m)->peerBlockSize(), peer_block_size); 90 } 91 92 for (SlavePortConstIter s = slavePorts.begin(); s != slavePorts.end(); 93 ++s) { 94 peer_block_size = std::max((*s)->peerBlockSize(), peer_block_size); 95 } 96 97 // if the peers do not have a block size, use the default value 98 // set through the bus parameters 99 if (peer_block_size != 0) 100 blockSize = peer_block_size; 101 102 // check if the block size is a value known to work 103 if (!(blockSize == 16 || blockSize == 32 || blockSize == 64 || 104 blockSize == 128)) 105 warn_once("Block size is neither 16, 32, 64 or 128 bytes.\n"); 106} 107 108BaseMasterPort & 109BaseBus::getMasterPort(const std::string &if_name, PortID idx) 110{ 111 if (if_name == "master" && idx < masterPorts.size()) { 112 // the master port index translates directly to the vector position 113 return *masterPorts[idx]; 114 } else if (if_name == "default") { 115 return *masterPorts[defaultPortID]; 116 } else { 117 return MemObject::getMasterPort(if_name, idx); 118 } 119} 120 121BaseSlavePort & 122BaseBus::getSlavePort(const std::string &if_name, PortID idx) 123{ 124 if (if_name == "slave" && idx < slavePorts.size()) { 125 // the slave port index translates directly to the vector position 126 return *slavePorts[idx]; 127 } else { 128 return MemObject::getSlavePort(if_name, idx); 129 } 130} 131 132void 133BaseBus::calcPacketTiming(PacketPtr pkt) 134{ 135 // the bus will be called at a time that is not necessarily 136 // coinciding with its own clock, so start by determining how long 137 // until the next clock edge (could be zero) 138 Tick offset = nextCycle() - curTick(); 139 140 // determine how many cycles are needed to send the data 141 unsigned dataCycles = pkt->hasData() ? divCeil(pkt->getSize(), width) : 0; 142 143 // before setting the bus delay fields of the packet, ensure that 144 // the delay from any previous bus has been accounted for 145 if (pkt->busFirstWordDelay != 0 || pkt->busLastWordDelay != 0) 146 panic("Packet %s already has bus delay (%d, %d) that should be " 147 "accounted for.\n", pkt->cmdString(), pkt->busFirstWordDelay, 148 pkt->busLastWordDelay); 149 150 // The first word will be delivered on the cycle after the header. 151 pkt->busFirstWordDelay = (headerCycles + 1) * clockPeriod() + offset; 152 153 // Note that currently busLastWordDelay can be smaller than 154 // busFirstWordDelay if the packet has no data 155 pkt->busLastWordDelay = (headerCycles + dataCycles) * clockPeriod() + 156 offset; 157} 158 159template <typename PortClass> 160BaseBus::Layer<PortClass>::Layer(BaseBus& _bus, const std::string& _name) : 161 Drainable(), 162 bus(_bus), _name(_name), state(IDLE), drainManager(NULL), 163 retryingPort(NULL), releaseEvent(this) 164{ 165} 166 167template <typename PortClass> 168void BaseBus::Layer<PortClass>::occupyLayer(Tick until) 169{ 170 // ensure the state is busy or in retry and never idle at this 171 // point, as the bus should transition from idle as soon as it has 172 // decided to forward the packet to prevent any follow-on calls to 173 // sendTiming seeing an unoccupied bus 174 assert(state != IDLE); 175 176 // note that we do not change the bus state here, if we are going 177 // from idle to busy it is handled by tryTiming, and if we 178 // are in retry we should remain in retry such that 179 // succeededTiming still sees the accurate state 180 181 // until should never be 0 as express snoops never occupy the bus 182 assert(until != 0); 183 bus.schedule(releaseEvent, until); 184 185 DPRINTF(BaseBus, "The bus is now busy from tick %d to %d\n", 186 curTick(), until); 187} 188 189template <typename PortClass> 190bool 191BaseBus::Layer<PortClass>::tryTiming(PortClass* port) 192{ 193 // first we see if the bus is busy, next we check if we are in a 194 // retry with a port other than the current one 195 if (state == BUSY || (state == RETRY && port != retryingPort)) { 196 // put the port at the end of the retry list 197 retryList.push_back(port); 198 return false; 199 } 200 201 // @todo: here we should no longer consider this port retrying 202 // once we can differentiate retries due to a busy bus and a 203 // failed forwarding, for now keep it so we can stick it back at 204 // the front of the retry list if needed 205 206 // update the state which is shared for request, response and 207 // snoop responses, if we were idle we are now busy, if we are in 208 // a retry, then do not change 209 if (state == IDLE) 210 state = BUSY; 211 212 return true; 213} 214 215template <typename PortClass> 216void 217BaseBus::Layer<PortClass>::succeededTiming(Tick busy_time) 218{ 219 // if a retrying port succeeded, update the state and reset the 220 // retrying port 221 if (state == RETRY) { 222 DPRINTF(BaseBus, "Succeeded retry from %s\n", 223 retryingPort->name()); 224 state = BUSY; 225 retryingPort = NULL; 226 } 227 228 // we should either have gone from idle to busy in the 229 // tryTiming test, or just gone from a retry to busy 230 assert(state == BUSY); 231 232 // occupy the bus accordingly 233 occupyLayer(busy_time); 234} 235 236template <typename PortClass> 237void 238BaseBus::Layer<PortClass>::failedTiming(PortClass* port, Tick busy_time) 239{ 240 // if the current failing port is the retrying one, then for now stick it 241 // back at the front of the retry list to not change any regressions 242 if (state == RETRY) { 243 // we should never see a retry from any port but the current 244 // retry port at this point 245 assert(port == retryingPort); 246 retryList.push_front(port); 247 retryingPort = NULL; 248 } else { 249 // if we are not in a retry, i.e. busy (but never idle), then 250 // add the port at the end of the retry list 251 retryList.push_back(port); 252 } 253 254 // even if we retried the current one and did not succeed, 255 // we are no longer retrying but instead busy 256 state = BUSY; 257 258 // occupy the bus accordingly 259 occupyLayer(busy_time); 260} 261 262template <typename PortClass> 263void 264BaseBus::Layer<PortClass>::releaseLayer() 265{ 266 // releasing the bus means we should now be idle 267 assert(state == BUSY); 268 assert(!releaseEvent.scheduled()); 269 270 // update the state 271 state = IDLE; 272 273 // bus is now idle, so if someone is waiting we can retry 274 if (!retryList.empty()) { 275 // note that we block (return false on recvTiming) both 276 // because the bus is busy and because the destination is 277 // busy, and in the latter case the bus may be released before 278 // we see a retry from the destination 279 retryWaiting(); 280 } else if (drainManager) { 281 DPRINTF(Drain, "Bus done draining, signaling drain manager\n"); 282 //If we weren't able to drain before, do it now. 283 drainManager->signalDrainDone(); 284 // Clear the drain event once we're done with it. 285 drainManager = NULL; 286 } 287} 288 289template <typename PortClass> 290void 291BaseBus::Layer<PortClass>::retryWaiting() 292{ 293 // this should never be called with an empty retry list 294 assert(!retryList.empty()); 295 296 // we always go to retrying from idle 297 assert(state == IDLE); 298 299 // update the state 300 state = RETRY; 301 302 // set the retrying port to the front of the retry list and pop it 303 // off the list 304 assert(retryingPort == NULL); 305 retryingPort = retryList.front(); 306 retryList.pop_front(); 307 308 // note that we might have blocked on the receiving port being 309 // busy (rather than the bus itself) and now call retry before the 310 // destination called retry on the bus 311 retryingPort->sendRetry(); 312 313 // If the bus is still in the retry state, sendTiming wasn't 314 // called in zero time (e.g. the cache does this) 315 if (state == RETRY) { 316 //Burn a cycle for the missed grant. 317 318 // update the state to busy and reset the retrying port 319 state = BUSY; 320 retryingPort = NULL; 321 322 // occupy the bus layer until the next cycle ends 323 occupyLayer(bus.clockEdge(Cycles(1))); 324 } 325} 326 327template <typename PortClass> 328void 329BaseBus::Layer<PortClass>::recvRetry() 330{ 331 // we got a retry from a peer that we tried to send something to 332 // and failed, but we sent it on the account of someone else, and 333 // that source port should be on our retry list, however if the 334 // bus layer is released before this happens and the retry (from 335 // the bus point of view) is successful then this no longer holds 336 // and we could in fact have an empty retry list 337 if (retryList.empty()) 338 return; 339 340 // if the bus layer is idle 341 if (state == IDLE) { 342 // note that we do not care who told us to retry at the moment, we 343 // merely let the first one on the retry list go 344 retryWaiting(); 345 } 346} 347 348PortID 349BaseBus::findPort(Addr addr) 350{ 351 // we should never see any address lookups before we've got the 352 // ranges of all connected slave modules 353 assert(gotAllAddrRanges); 354 355 // Check the cache 356 PortID dest_id = checkPortCache(addr); 357 if (dest_id != InvalidPortID) 358 return dest_id; 359 360 // Check the address map interval tree 361 PortMapConstIter i = portMap.find(addr); 362 if (i != portMap.end()) { 363 dest_id = i->second; 364 updatePortCache(dest_id, i->first); 365 return dest_id; 366 } 367 368 // Check if this matches the default range 369 if (useDefaultRange) { 370 if (defaultRange.contains(addr)) { 371 DPRINTF(BusAddrRanges, " found addr %#llx on default\n", 372 addr); 373 return defaultPortID; 374 } 375 } else if (defaultPortID != InvalidPortID) { 376 DPRINTF(BusAddrRanges, "Unable to find destination for addr %#llx, " 377 "will use default port\n", addr); 378 return defaultPortID; 379 } 380 381 // we should use the range for the default port and it did not 382 // match, or the default port is not set 383 fatal("Unable to find destination for addr %#llx on bus %s\n", addr, 384 name()); 385} 386 387/** Function called by the port when the bus is receiving a range change.*/ 388void 389BaseBus::recvRangeChange(PortID master_port_id) 390{ 391 DPRINTF(BusAddrRanges, "Received range change from slave port %s\n", 392 masterPorts[master_port_id]->getSlavePort().name()); 393 394 // remember that we got a range from this master port and thus the 395 // connected slave module 396 gotAddrRanges[master_port_id] = true; 397 398 // update the global flag 399 if (!gotAllAddrRanges) { 400 // take a logical AND of all the ports and see if we got 401 // ranges from everyone 402 gotAllAddrRanges = true; 403 std::vector<bool>::const_iterator r = gotAddrRanges.begin(); 404 while (gotAllAddrRanges && r != gotAddrRanges.end()) { 405 gotAllAddrRanges &= *r++; 406 } 407 if (gotAllAddrRanges) 408 DPRINTF(BusAddrRanges, "Got address ranges from all slaves\n"); 409 } 410 411 // note that we could get the range from the default port at any 412 // point in time, and we cannot assume that the default range is 413 // set before the other ones are, so we do additional checks once 414 // all ranges are provided 415 if (master_port_id == defaultPortID) { 416 // only update if we are indeed checking ranges for the 417 // default port since the port might not have a valid range 418 // otherwise 419 if (useDefaultRange) { 420 AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges(); 421 422 if (ranges.size() != 1) 423 fatal("Bus %s may only have a single default range", 424 name()); 425 426 defaultRange = ranges.front(); 427 } 428 } else { 429 // the ports are allowed to update their address ranges 430 // dynamically, so remove any existing entries 431 if (gotAddrRanges[master_port_id]) { 432 for (PortMapIter p = portMap.begin(); p != portMap.end(); ) { 433 if (p->second == master_port_id) 434 // erasing invalidates the iterator, so advance it 435 // before the deletion takes place 436 portMap.erase(p++); 437 else 438 p++; 439 } 440 } 441 442 AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges(); 443 444 for (AddrRangeConstIter r = ranges.begin(); r != ranges.end(); ++r) { 445 DPRINTF(BusAddrRanges, "Adding range %s for id %d\n", 446 r->to_string(), master_port_id); 447 if (portMap.insert(*r, master_port_id) == portMap.end()) { 448 PortID conflict_id = portMap.find(*r)->second; 449 fatal("%s has two ports with same range:\n\t%s\n\t%s\n", 450 name(), 451 masterPorts[master_port_id]->getSlavePort().name(), 452 masterPorts[conflict_id]->getSlavePort().name()); 453 } 454 } 455 } 456 457 // if we have received ranges from all our neighbouring slave 458 // modules, go ahead and tell our connected master modules in 459 // turn, this effectively assumes a tree structure of the system 460 if (gotAllAddrRanges) { 461 DPRINTF(BusAddrRanges, "Aggregating bus ranges\n"); 462 busRanges.clear(); 463 464 // start out with the default range 465 if (useDefaultRange) { 466 if (!gotAddrRanges[defaultPortID]) 467 fatal("Bus %s uses default range, but none provided", 468 name()); 469 470 busRanges.push_back(defaultRange); 471 DPRINTF(BusAddrRanges, "-- Adding default %s\n", 472 defaultRange.to_string()); 473 } 474 475 // merge all interleaved ranges and add any range that is not 476 // a subset of the default range 477 std::vector<AddrRange> intlv_ranges; 478 for (AddrRangeMap<PortID>::const_iterator r = portMap.begin(); 479 r != portMap.end(); ++r) { 480 // if the range is interleaved then save it for now 481 if (r->first.interleaved()) { 482 // if we already got interleaved ranges that are not 483 // part of the same range, then first do a merge 484 // before we add the new one 485 if (!intlv_ranges.empty() && 486 !intlv_ranges.back().mergesWith(r->first)) { 487 DPRINTF(BusAddrRanges, "-- Merging range from %d ranges\n", 488 intlv_ranges.size()); 489 AddrRange merged_range(intlv_ranges); 490 // next decide if we keep the merged range or not 491 if (!(useDefaultRange && 492 merged_range.isSubset(defaultRange))) { 493 busRanges.push_back(merged_range); 494 DPRINTF(BusAddrRanges, "-- Adding merged range %s\n", 495 merged_range.to_string()); 496 } 497 intlv_ranges.clear(); 498 } 499 intlv_ranges.push_back(r->first); 500 } else { 501 // keep the current range if not a subset of the default 502 if (!(useDefaultRange && 503 r->first.isSubset(defaultRange))) { 504 busRanges.push_back(r->first); 505 DPRINTF(BusAddrRanges, "-- Adding range %s\n", 506 r->first.to_string()); 507 } 508 } 509 } 510 511 // if there is still interleaved ranges waiting to be merged, 512 // go ahead and do it 513 if (!intlv_ranges.empty()) { 514 DPRINTF(BusAddrRanges, "-- Merging range from %d ranges\n", 515 intlv_ranges.size()); 516 AddrRange merged_range(intlv_ranges); 517 if (!(useDefaultRange && merged_range.isSubset(defaultRange))) { 518 busRanges.push_back(merged_range); 519 DPRINTF(BusAddrRanges, "-- Adding merged range %s\n", 520 merged_range.to_string()); 521 } 522 } 523 524 // also check that no range partially overlaps with the 525 // default range, this has to be done after all ranges are set 526 // as there are no guarantees for when the default range is 527 // update with respect to the other ones 528 if (useDefaultRange) { 529 for (AddrRangeConstIter r = busRanges.begin(); 530 r != busRanges.end(); ++r) { 531 // see if the new range is partially 532 // overlapping the default range 533 if (r->intersects(defaultRange) && 534 !r->isSubset(defaultRange)) 535 fatal("Range %s intersects the " \ 536 "default range of %s but is not a " \ 537 "subset\n", r->to_string(), name()); 538 } 539 } 540 541 // tell all our neighbouring master ports that our address 542 // ranges have changed 543 for (SlavePortConstIter s = slavePorts.begin(); s != slavePorts.end(); 544 ++s) 545 (*s)->sendRangeChange(); 546 } 547 548 clearPortCache(); 549} 550 551AddrRangeList 552BaseBus::getAddrRanges() const 553{ 554 // we should never be asked without first having sent a range 555 // change, and the latter is only done once we have all the ranges 556 // of the connected devices 557 assert(gotAllAddrRanges); 558 559 // at the moment, this never happens, as there are no cycles in 560 // the range queries and no devices on the master side of a bus 561 // (CPU, cache, bridge etc) actually care about the ranges of the 562 // ports they are connected to 563 564 DPRINTF(BusAddrRanges, "Received address range request\n"); 565 566 return busRanges; 567} 568 569unsigned 570BaseBus::deviceBlockSize() const 571{ 572 return blockSize; 573} 574 575template <typename PortClass> 576unsigned int 577BaseBus::Layer<PortClass>::drain(DrainManager *dm) 578{ 579 //We should check that we're not "doing" anything, and that noone is 580 //waiting. We might be idle but have someone waiting if the device we 581 //contacted for a retry didn't actually retry. 582 if (!retryList.empty() || state != IDLE) { 583 DPRINTF(Drain, "Bus not drained\n"); 584 drainManager = dm; 585 return 1; 586 } 587 return 0; 588} 589 590/** 591 * Bus layer template instantiations. Could be removed with _impl.hh 592 * file, but since there are only two given options (MasterPort and 593 * SlavePort) it seems a bit excessive at this point. 594 */ 595template class BaseBus::Layer<SlavePort>; 596template class BaseBus::Layer<MasterPort>; 597