xbar.cc revision 9712
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 = clockEdge() - 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 uint16_t num_dest_ports) : 162 Drainable(), 163 bus(_bus), _name(_name), state(IDLE), drainManager(NULL), 164 retryingPort(NULL), waitingForPeer(num_dest_ports, NULL), 165 releaseEvent(this) 166{ 167} 168 169template <typename PortClass> 170void BaseBus::Layer<PortClass>::occupyLayer(Tick until) 171{ 172 // ensure the state is busy at this point, as the bus should 173 // transition from idle as soon as it has decided to forward the 174 // packet to prevent any follow-on calls to sendTiming seeing an 175 // unoccupied bus 176 assert(state == BUSY); 177 178 // until should never be 0 as express snoops never occupy the bus 179 assert(until != 0); 180 bus.schedule(releaseEvent, until); 181 182 // account for the occupied ticks 183 occupancy += until - curTick(); 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, PortID dest_port_id) 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, lastly we check 195 // if the destination port is already engaged in a transaction 196 // waiting for a retry from the peer 197 if (state == BUSY || (state == RETRY && port != retryingPort) || 198 (dest_port_id != InvalidPortID && 199 waitingForPeer[dest_port_id] != NULL)) { 200 // put the port at the end of the retry list waiting for the 201 // layer to be freed up (and in the case of a busy peer, for 202 // that transaction to go through, and then the bus to free 203 // up) 204 waitingForLayer.push_back(port); 205 return false; 206 } 207 208 // update the state to busy 209 state = BUSY; 210 211 // reset the retrying port 212 retryingPort = NULL; 213 214 return true; 215} 216 217template <typename PortClass> 218void 219BaseBus::Layer<PortClass>::succeededTiming(Tick busy_time) 220{ 221 // we should have gone from idle or retry to busy in the tryTiming 222 // test 223 assert(state == BUSY); 224 225 // occupy the bus accordingly 226 occupyLayer(busy_time); 227} 228 229template <typename PortClass> 230void 231BaseBus::Layer<PortClass>::failedTiming(PortClass* src_port, 232 PortID dest_port_id, Tick busy_time) 233{ 234 // ensure no one got in between and tried to send something to 235 // this port 236 assert(waitingForPeer[dest_port_id] == NULL); 237 238 // if the source port is the current retrying one or not, we have 239 // failed in forwarding and should track that we are now waiting 240 // for the peer to send a retry 241 waitingForPeer[dest_port_id] = src_port; 242 243 // we should have gone from idle or retry to busy in the tryTiming 244 // test 245 assert(state == BUSY); 246 247 // occupy the bus accordingly 248 occupyLayer(busy_time); 249} 250 251template <typename PortClass> 252void 253BaseBus::Layer<PortClass>::releaseLayer() 254{ 255 // releasing the bus means we should now be idle 256 assert(state == BUSY); 257 assert(!releaseEvent.scheduled()); 258 259 // update the state 260 state = IDLE; 261 262 // bus layer is now idle, so if someone is waiting we can retry 263 if (!waitingForLayer.empty()) { 264 retryWaiting(); 265 } else if (waitingForPeer == NULL && drainManager) { 266 DPRINTF(Drain, "Bus done draining, signaling drain manager\n"); 267 //If we weren't able to drain before, do it now. 268 drainManager->signalDrainDone(); 269 // Clear the drain event once we're done with it. 270 drainManager = NULL; 271 } 272} 273 274template <typename PortClass> 275void 276BaseBus::Layer<PortClass>::retryWaiting() 277{ 278 // this should never be called with no one waiting 279 assert(!waitingForLayer.empty()); 280 281 // we always go to retrying from idle 282 assert(state == IDLE); 283 284 // update the state 285 state = RETRY; 286 287 // set the retrying port to the front of the retry list and pop it 288 // off the list 289 assert(retryingPort == NULL); 290 retryingPort = waitingForLayer.front(); 291 waitingForLayer.pop_front(); 292 293 // tell the port to retry, which in some cases ends up calling the 294 // bus 295 retryingPort->sendRetry(); 296 297 // If the bus is still in the retry state, sendTiming wasn't 298 // called in zero time (e.g. the cache does this), burn a cycle 299 if (state == RETRY) { 300 // update the state to busy and reset the retrying port, we 301 // have done our bit and sent the retry 302 state = BUSY; 303 retryingPort = NULL; 304 305 // occupy the bus layer until the next cycle ends 306 occupyLayer(bus.clockEdge(Cycles(1))); 307 } 308} 309 310template <typename PortClass> 311void 312BaseBus::Layer<PortClass>::recvRetry(PortID port_id) 313{ 314 // we should never get a retry without having failed to forward 315 // something to this port 316 assert(waitingForPeer[port_id] != NULL); 317 318 // find the port where the failed packet originated and remove the 319 // item from the waiting list 320 PortClass* retry_port = waitingForPeer[port_id]; 321 waitingForPeer[port_id] = NULL; 322 323 // add this port at the front of the waiting ports for the layer, 324 // this allows us to call retry on the port immediately if the bus 325 // layer is idle 326 waitingForLayer.push_front(retry_port); 327 328 // if the bus layer is idle, retry this port straight away, if we 329 // are busy, then simply let the port wait for its turn 330 if (state == IDLE) { 331 retryWaiting(); 332 } else { 333 assert(state == BUSY); 334 } 335} 336 337PortID 338BaseBus::findPort(Addr addr) 339{ 340 // we should never see any address lookups before we've got the 341 // ranges of all connected slave modules 342 assert(gotAllAddrRanges); 343 344 // Check the cache 345 PortID dest_id = checkPortCache(addr); 346 if (dest_id != InvalidPortID) 347 return dest_id; 348 349 // Check the address map interval tree 350 PortMapConstIter i = portMap.find(addr); 351 if (i != portMap.end()) { 352 dest_id = i->second; 353 updatePortCache(dest_id, i->first); 354 return dest_id; 355 } 356 357 // Check if this matches the default range 358 if (useDefaultRange) { 359 if (defaultRange.contains(addr)) { 360 DPRINTF(BusAddrRanges, " found addr %#llx on default\n", 361 addr); 362 return defaultPortID; 363 } 364 } else if (defaultPortID != InvalidPortID) { 365 DPRINTF(BusAddrRanges, "Unable to find destination for addr %#llx, " 366 "will use default port\n", addr); 367 return defaultPortID; 368 } 369 370 // we should use the range for the default port and it did not 371 // match, or the default port is not set 372 fatal("Unable to find destination for addr %#llx on bus %s\n", addr, 373 name()); 374} 375 376/** Function called by the port when the bus is receiving a range change.*/ 377void 378BaseBus::recvRangeChange(PortID master_port_id) 379{ 380 DPRINTF(BusAddrRanges, "Received range change from slave port %s\n", 381 masterPorts[master_port_id]->getSlavePort().name()); 382 383 // remember that we got a range from this master port and thus the 384 // connected slave module 385 gotAddrRanges[master_port_id] = true; 386 387 // update the global flag 388 if (!gotAllAddrRanges) { 389 // take a logical AND of all the ports and see if we got 390 // ranges from everyone 391 gotAllAddrRanges = true; 392 std::vector<bool>::const_iterator r = gotAddrRanges.begin(); 393 while (gotAllAddrRanges && r != gotAddrRanges.end()) { 394 gotAllAddrRanges &= *r++; 395 } 396 if (gotAllAddrRanges) 397 DPRINTF(BusAddrRanges, "Got address ranges from all slaves\n"); 398 } 399 400 // note that we could get the range from the default port at any 401 // point in time, and we cannot assume that the default range is 402 // set before the other ones are, so we do additional checks once 403 // all ranges are provided 404 if (master_port_id == defaultPortID) { 405 // only update if we are indeed checking ranges for the 406 // default port since the port might not have a valid range 407 // otherwise 408 if (useDefaultRange) { 409 AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges(); 410 411 if (ranges.size() != 1) 412 fatal("Bus %s may only have a single default range", 413 name()); 414 415 defaultRange = ranges.front(); 416 } 417 } else { 418 // the ports are allowed to update their address ranges 419 // dynamically, so remove any existing entries 420 if (gotAddrRanges[master_port_id]) { 421 for (PortMapIter p = portMap.begin(); p != portMap.end(); ) { 422 if (p->second == master_port_id) 423 // erasing invalidates the iterator, so advance it 424 // before the deletion takes place 425 portMap.erase(p++); 426 else 427 p++; 428 } 429 } 430 431 AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges(); 432 433 for (AddrRangeConstIter r = ranges.begin(); r != ranges.end(); ++r) { 434 DPRINTF(BusAddrRanges, "Adding range %s for id %d\n", 435 r->to_string(), master_port_id); 436 if (portMap.insert(*r, master_port_id) == portMap.end()) { 437 PortID conflict_id = portMap.find(*r)->second; 438 fatal("%s has two ports with same range:\n\t%s\n\t%s\n", 439 name(), 440 masterPorts[master_port_id]->getSlavePort().name(), 441 masterPorts[conflict_id]->getSlavePort().name()); 442 } 443 } 444 } 445 446 // if we have received ranges from all our neighbouring slave 447 // modules, go ahead and tell our connected master modules in 448 // turn, this effectively assumes a tree structure of the system 449 if (gotAllAddrRanges) { 450 DPRINTF(BusAddrRanges, "Aggregating bus ranges\n"); 451 busRanges.clear(); 452 453 // start out with the default range 454 if (useDefaultRange) { 455 if (!gotAddrRanges[defaultPortID]) 456 fatal("Bus %s uses default range, but none provided", 457 name()); 458 459 busRanges.push_back(defaultRange); 460 DPRINTF(BusAddrRanges, "-- Adding default %s\n", 461 defaultRange.to_string()); 462 } 463 464 // merge all interleaved ranges and add any range that is not 465 // a subset of the default range 466 std::vector<AddrRange> intlv_ranges; 467 for (AddrRangeMap<PortID>::const_iterator r = portMap.begin(); 468 r != portMap.end(); ++r) { 469 // if the range is interleaved then save it for now 470 if (r->first.interleaved()) { 471 // if we already got interleaved ranges that are not 472 // part of the same range, then first do a merge 473 // before we add the new one 474 if (!intlv_ranges.empty() && 475 !intlv_ranges.back().mergesWith(r->first)) { 476 DPRINTF(BusAddrRanges, "-- Merging range from %d ranges\n", 477 intlv_ranges.size()); 478 AddrRange merged_range(intlv_ranges); 479 // next decide if we keep the merged range or not 480 if (!(useDefaultRange && 481 merged_range.isSubset(defaultRange))) { 482 busRanges.push_back(merged_range); 483 DPRINTF(BusAddrRanges, "-- Adding merged range %s\n", 484 merged_range.to_string()); 485 } 486 intlv_ranges.clear(); 487 } 488 intlv_ranges.push_back(r->first); 489 } else { 490 // keep the current range if not a subset of the default 491 if (!(useDefaultRange && 492 r->first.isSubset(defaultRange))) { 493 busRanges.push_back(r->first); 494 DPRINTF(BusAddrRanges, "-- Adding range %s\n", 495 r->first.to_string()); 496 } 497 } 498 } 499 500 // if there is still interleaved ranges waiting to be merged, 501 // go ahead and do it 502 if (!intlv_ranges.empty()) { 503 DPRINTF(BusAddrRanges, "-- Merging range from %d ranges\n", 504 intlv_ranges.size()); 505 AddrRange merged_range(intlv_ranges); 506 if (!(useDefaultRange && merged_range.isSubset(defaultRange))) { 507 busRanges.push_back(merged_range); 508 DPRINTF(BusAddrRanges, "-- Adding merged range %s\n", 509 merged_range.to_string()); 510 } 511 } 512 513 // also check that no range partially overlaps with the 514 // default range, this has to be done after all ranges are set 515 // as there are no guarantees for when the default range is 516 // update with respect to the other ones 517 if (useDefaultRange) { 518 for (AddrRangeConstIter r = busRanges.begin(); 519 r != busRanges.end(); ++r) { 520 // see if the new range is partially 521 // overlapping the default range 522 if (r->intersects(defaultRange) && 523 !r->isSubset(defaultRange)) 524 fatal("Range %s intersects the " \ 525 "default range of %s but is not a " \ 526 "subset\n", r->to_string(), name()); 527 } 528 } 529 530 // tell all our neighbouring master ports that our address 531 // ranges have changed 532 for (SlavePortConstIter s = slavePorts.begin(); s != slavePorts.end(); 533 ++s) 534 (*s)->sendRangeChange(); 535 } 536 537 clearPortCache(); 538} 539 540AddrRangeList 541BaseBus::getAddrRanges() const 542{ 543 // we should never be asked without first having sent a range 544 // change, and the latter is only done once we have all the ranges 545 // of the connected devices 546 assert(gotAllAddrRanges); 547 548 // at the moment, this never happens, as there are no cycles in 549 // the range queries and no devices on the master side of a bus 550 // (CPU, cache, bridge etc) actually care about the ranges of the 551 // ports they are connected to 552 553 DPRINTF(BusAddrRanges, "Received address range request\n"); 554 555 return busRanges; 556} 557 558unsigned 559BaseBus::deviceBlockSize() const 560{ 561 return blockSize; 562} 563 564void 565BaseBus::regStats() 566{ 567 using namespace Stats; 568 569 transDist 570 .init(MemCmd::NUM_MEM_CMDS) 571 .name(name() + ".trans_dist") 572 .desc("Transaction distribution") 573 .flags(nozero); 574 575 // get the string representation of the commands 576 for (int i = 0; i < MemCmd::NUM_MEM_CMDS; i++) { 577 MemCmd cmd(i); 578 const std::string &cstr = cmd.toString(); 579 transDist.subname(i, cstr); 580 } 581 582 pktCount 583 .init(slavePorts.size(), masterPorts.size()) 584 .name(name() + ".pkt_count") 585 .desc("Packet count per connected master and slave (bytes)") 586 .flags(total | nozero | nonan); 587 588 totPktSize 589 .init(slavePorts.size(), masterPorts.size()) 590 .name(name() + ".tot_pkt_size") 591 .desc("Cumulative packet size per connected master and slave (bytes)") 592 .flags(total | nozero | nonan); 593 594 // both the packet count and total size are two-dimensional 595 // vectors, indexed by slave port id and master port id, thus the 596 // neighbouring master and slave, they do not differentiate what 597 // came from the master and was forwarded to the slave (requests 598 // and snoop responses) and what came from the slave and was 599 // forwarded to the master (responses and snoop requests) 600 for (int i = 0; i < slavePorts.size(); i++) { 601 pktCount.subname(i, slavePorts[i]->getMasterPort().name()); 602 totPktSize.subname(i, slavePorts[i]->getMasterPort().name()); 603 for (int j = 0; j < masterPorts.size(); j++) { 604 pktCount.ysubname(j, masterPorts[j]->getSlavePort().name()); 605 totPktSize.ysubname(j, masterPorts[j]->getSlavePort().name()); 606 } 607 } 608} 609 610template <typename PortClass> 611unsigned int 612BaseBus::Layer<PortClass>::drain(DrainManager *dm) 613{ 614 //We should check that we're not "doing" anything, and that noone is 615 //waiting. We might be idle but have someone waiting if the device we 616 //contacted for a retry didn't actually retry. 617 if (state != IDLE) { 618 DPRINTF(Drain, "Bus not drained\n"); 619 drainManager = dm; 620 return 1; 621 } 622 return 0; 623} 624 625template <typename PortClass> 626void 627BaseBus::Layer<PortClass>::regStats() 628{ 629 using namespace Stats; 630 631 occupancy 632 .name(name() + ".occupancy") 633 .desc("Layer occupancy (ticks)") 634 .flags(nozero); 635 636 utilization 637 .name(name() + ".utilization") 638 .desc("Layer utilization (%)") 639 .precision(1) 640 .flags(nozero); 641 642 utilization = 100 * occupancy / simTicks; 643} 644 645/** 646 * Bus layer template instantiations. Could be removed with _impl.hh 647 * file, but since there are only two given options (MasterPort and 648 * SlavePort) it seems a bit excessive at this point. 649 */ 650template class BaseBus::Layer<SlavePort>; 651template class BaseBus::Layer<MasterPort>; 652