xbar.cc revision 12778
1/* 2 * Copyright (c) 2011-2015, 2018 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 crossbar object. 48 */ 49 50#include "mem/xbar.hh" 51 52#include "base/logging.hh" 53#include "base/trace.hh" 54#include "debug/AddrRanges.hh" 55#include "debug/Drain.hh" 56#include "debug/XBar.hh" 57 58BaseXBar::BaseXBar(const BaseXBarParams *p) 59 : MemObject(p), 60 frontendLatency(p->frontend_latency), 61 forwardLatency(p->forward_latency), 62 responseLatency(p->response_latency), 63 width(p->width), 64 gotAddrRanges(p->port_default_connection_count + 65 p->port_master_connection_count, false), 66 gotAllAddrRanges(false), defaultPortID(InvalidPortID), 67 useDefaultRange(p->use_default_range) 68{} 69 70BaseXBar::~BaseXBar() 71{ 72 for (auto m: masterPorts) 73 delete m; 74 75 for (auto s: slavePorts) 76 delete s; 77} 78 79void 80BaseXBar::init() 81{ 82} 83 84BaseMasterPort & 85BaseXBar::getMasterPort(const std::string &if_name, PortID idx) 86{ 87 if (if_name == "master" && idx < masterPorts.size()) { 88 // the master port index translates directly to the vector position 89 return *masterPorts[idx]; 90 } else if (if_name == "default") { 91 return *masterPorts[defaultPortID]; 92 } else { 93 return MemObject::getMasterPort(if_name, idx); 94 } 95} 96 97BaseSlavePort & 98BaseXBar::getSlavePort(const std::string &if_name, PortID idx) 99{ 100 if (if_name == "slave" && idx < slavePorts.size()) { 101 // the slave port index translates directly to the vector position 102 return *slavePorts[idx]; 103 } else { 104 return MemObject::getSlavePort(if_name, idx); 105 } 106} 107 108void 109BaseXBar::calcPacketTiming(PacketPtr pkt, Tick header_delay) 110{ 111 // the crossbar will be called at a time that is not necessarily 112 // coinciding with its own clock, so start by determining how long 113 // until the next clock edge (could be zero) 114 Tick offset = clockEdge() - curTick(); 115 116 // the header delay depends on the path through the crossbar, and 117 // we therefore rely on the caller to provide the actual 118 // value 119 pkt->headerDelay += offset + header_delay; 120 121 // note that we add the header delay to the existing value, and 122 // align it to the crossbar clock 123 124 // do a quick sanity check to ensure the timings are not being 125 // ignored, note that this specific value may cause problems for 126 // slower interconnects 127 panic_if(pkt->headerDelay > SimClock::Int::us, 128 "Encountered header delay exceeding 1 us\n"); 129 130 if (pkt->hasData()) { 131 // the payloadDelay takes into account the relative time to 132 // deliver the payload of the packet, after the header delay, 133 // we take the maximum since the payload delay could already 134 // be longer than what this parcitular crossbar enforces. 135 pkt->payloadDelay = std::max<Tick>(pkt->payloadDelay, 136 divCeil(pkt->getSize(), width) * 137 clockPeriod()); 138 } 139 140 // the payload delay is not paying for the clock offset as that is 141 // already done using the header delay, and the payload delay is 142 // also used to determine how long the crossbar layer is busy and 143 // thus regulates throughput 144} 145 146template <typename SrcType, typename DstType> 147BaseXBar::Layer<SrcType,DstType>::Layer(DstType& _port, BaseXBar& _xbar, 148 const std::string& _name) : 149 port(_port), xbar(_xbar), _name(_name), state(IDLE), 150 waitingForPeer(NULL), releaseEvent([this]{ releaseLayer(); }, name()) 151{ 152} 153 154template <typename SrcType, typename DstType> 155void BaseXBar::Layer<SrcType,DstType>::occupyLayer(Tick until) 156{ 157 // ensure the state is busy at this point, as the layer should 158 // transition from idle as soon as it has decided to forward the 159 // packet to prevent any follow-on calls to sendTiming seeing an 160 // unoccupied layer 161 assert(state == BUSY); 162 163 // until should never be 0 as express snoops never occupy the layer 164 assert(until != 0); 165 xbar.schedule(releaseEvent, until); 166 167 // account for the occupied ticks 168 occupancy += until - curTick(); 169 170 DPRINTF(BaseXBar, "The crossbar layer is now busy from tick %d to %d\n", 171 curTick(), until); 172} 173 174template <typename SrcType, typename DstType> 175bool 176BaseXBar::Layer<SrcType,DstType>::tryTiming(SrcType* src_port) 177{ 178 // if we are in the retry state, we will not see anything but the 179 // retrying port (or in the case of the snoop ports the snoop 180 // response port that mirrors the actual slave port) as we leave 181 // this state again in zero time if the peer does not immediately 182 // call the layer when receiving the retry 183 184 // first we see if the layer is busy, next we check if the 185 // destination port is already engaged in a transaction waiting 186 // for a retry from the peer 187 if (state == BUSY || waitingForPeer != NULL) { 188 // the port should not be waiting already 189 assert(std::find(waitingForLayer.begin(), waitingForLayer.end(), 190 src_port) == waitingForLayer.end()); 191 192 // put the port at the end of the retry list waiting for the 193 // layer to be freed up (and in the case of a busy peer, for 194 // that transaction to go through, and then the layer to free 195 // up) 196 waitingForLayer.push_back(src_port); 197 return false; 198 } 199 200 state = BUSY; 201 202 return true; 203} 204 205template <typename SrcType, typename DstType> 206void 207BaseXBar::Layer<SrcType,DstType>::succeededTiming(Tick busy_time) 208{ 209 // we should have gone from idle or retry to busy in the tryTiming 210 // test 211 assert(state == BUSY); 212 213 // occupy the layer accordingly 214 occupyLayer(busy_time); 215} 216 217template <typename SrcType, typename DstType> 218void 219BaseXBar::Layer<SrcType,DstType>::failedTiming(SrcType* src_port, 220 Tick busy_time) 221{ 222 // ensure no one got in between and tried to send something to 223 // this port 224 assert(waitingForPeer == NULL); 225 226 // if the source port is the current retrying one or not, we have 227 // failed in forwarding and should track that we are now waiting 228 // for the peer to send a retry 229 waitingForPeer = src_port; 230 231 // we should have gone from idle or retry to busy in the tryTiming 232 // test 233 assert(state == BUSY); 234 235 // occupy the bus accordingly 236 occupyLayer(busy_time); 237} 238 239template <typename SrcType, typename DstType> 240void 241BaseXBar::Layer<SrcType,DstType>::releaseLayer() 242{ 243 // releasing the bus means we should now be idle 244 assert(state == BUSY); 245 assert(!releaseEvent.scheduled()); 246 247 // update the state 248 state = IDLE; 249 250 // bus layer is now idle, so if someone is waiting we can retry 251 if (!waitingForLayer.empty()) { 252 // there is no point in sending a retry if someone is still 253 // waiting for the peer 254 if (waitingForPeer == NULL) 255 retryWaiting(); 256 } else if (waitingForPeer == NULL && drainState() == DrainState::Draining) { 257 DPRINTF(Drain, "Crossbar done draining, signaling drain manager\n"); 258 //If we weren't able to drain before, do it now. 259 signalDrainDone(); 260 } 261} 262 263template <typename SrcType, typename DstType> 264void 265BaseXBar::Layer<SrcType,DstType>::retryWaiting() 266{ 267 // this should never be called with no one waiting 268 assert(!waitingForLayer.empty()); 269 270 // we always go to retrying from idle 271 assert(state == IDLE); 272 273 // update the state 274 state = RETRY; 275 276 // set the retrying port to the front of the retry list and pop it 277 // off the list 278 SrcType* retryingPort = waitingForLayer.front(); 279 waitingForLayer.pop_front(); 280 281 // tell the port to retry, which in some cases ends up calling the 282 // layer again 283 sendRetry(retryingPort); 284 285 // If the layer is still in the retry state, sendTiming wasn't 286 // called in zero time (e.g. the cache does this when a writeback 287 // is squashed) 288 if (state == RETRY) { 289 // update the state to busy and reset the retrying port, we 290 // have done our bit and sent the retry 291 state = BUSY; 292 293 // occupy the crossbar layer until the next clock edge 294 occupyLayer(xbar.clockEdge()); 295 } 296} 297 298template <typename SrcType, typename DstType> 299void 300BaseXBar::Layer<SrcType,DstType>::recvRetry() 301{ 302 // we should never get a retry without having failed to forward 303 // something to this port 304 assert(waitingForPeer != NULL); 305 306 // add the port where the failed packet originated to the front of 307 // the waiting ports for the layer, this allows us to call retry 308 // on the port immediately if the crossbar layer is idle 309 waitingForLayer.push_front(waitingForPeer); 310 311 // we are no longer waiting for the peer 312 waitingForPeer = NULL; 313 314 // if the layer is idle, retry this port straight away, if we 315 // are busy, then simply let the port wait for its turn 316 if (state == IDLE) { 317 retryWaiting(); 318 } else { 319 assert(state == BUSY); 320 } 321} 322 323PortID 324BaseXBar::findPort(Addr addr) 325{ 326 // we should never see any address lookups before we've got the 327 // ranges of all connected slave modules 328 assert(gotAllAddrRanges); 329 330 // Check the address map interval tree 331 auto i = portMap.contains(addr); 332 if (i != portMap.end()) { 333 return i->second; 334 } 335 336 // Check if this matches the default range 337 if (useDefaultRange) { 338 if (defaultRange.contains(addr)) { 339 DPRINTF(AddrRanges, " found addr %#llx on default\n", 340 addr); 341 return defaultPortID; 342 } 343 } else if (defaultPortID != InvalidPortID) { 344 DPRINTF(AddrRanges, "Unable to find destination for addr %#llx, " 345 "will use default port\n", addr); 346 return defaultPortID; 347 } 348 349 // we should use the range for the default port and it did not 350 // match, or the default port is not set 351 fatal("Unable to find destination for addr %#llx on %s\n", addr, 352 name()); 353} 354 355/** Function called by the port when the crossbar is receiving a range change.*/ 356void 357BaseXBar::recvRangeChange(PortID master_port_id) 358{ 359 DPRINTF(AddrRanges, "Received range change from slave port %s\n", 360 masterPorts[master_port_id]->getSlavePort().name()); 361 362 // remember that we got a range from this master port and thus the 363 // connected slave module 364 gotAddrRanges[master_port_id] = true; 365 366 // update the global flag 367 if (!gotAllAddrRanges) { 368 // take a logical AND of all the ports and see if we got 369 // ranges from everyone 370 gotAllAddrRanges = true; 371 std::vector<bool>::const_iterator r = gotAddrRanges.begin(); 372 while (gotAllAddrRanges && r != gotAddrRanges.end()) { 373 gotAllAddrRanges &= *r++; 374 } 375 if (gotAllAddrRanges) 376 DPRINTF(AddrRanges, "Got address ranges from all slaves\n"); 377 } 378 379 // note that we could get the range from the default port at any 380 // point in time, and we cannot assume that the default range is 381 // set before the other ones are, so we do additional checks once 382 // all ranges are provided 383 if (master_port_id == defaultPortID) { 384 // only update if we are indeed checking ranges for the 385 // default port since the port might not have a valid range 386 // otherwise 387 if (useDefaultRange) { 388 AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges(); 389 390 if (ranges.size() != 1) 391 fatal("Crossbar %s may only have a single default range", 392 name()); 393 394 defaultRange = ranges.front(); 395 } 396 } else { 397 // the ports are allowed to update their address ranges 398 // dynamically, so remove any existing entries 399 if (gotAddrRanges[master_port_id]) { 400 for (auto p = portMap.begin(); p != portMap.end(); ) { 401 if (p->second == master_port_id) 402 // erasing invalidates the iterator, so advance it 403 // before the deletion takes place 404 portMap.erase(p++); 405 else 406 p++; 407 } 408 } 409 410 AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges(); 411 412 for (const auto& r: ranges) { 413 DPRINTF(AddrRanges, "Adding range %s for id %d\n", 414 r.to_string(), master_port_id); 415 if (portMap.insert(r, master_port_id) == portMap.end()) { 416 PortID conflict_id = portMap.intersects(r)->second; 417 fatal("%s has two ports responding within range " 418 "%s:\n\t%s\n\t%s\n", 419 name(), 420 r.to_string(), 421 masterPorts[master_port_id]->getSlavePort().name(), 422 masterPorts[conflict_id]->getSlavePort().name()); 423 } 424 } 425 } 426 427 // if we have received ranges from all our neighbouring slave 428 // modules, go ahead and tell our connected master modules in 429 // turn, this effectively assumes a tree structure of the system 430 if (gotAllAddrRanges) { 431 DPRINTF(AddrRanges, "Aggregating address ranges\n"); 432 xbarRanges.clear(); 433 434 // start out with the default range 435 if (useDefaultRange) { 436 if (!gotAddrRanges[defaultPortID]) 437 fatal("Crossbar %s uses default range, but none provided", 438 name()); 439 440 xbarRanges.push_back(defaultRange); 441 DPRINTF(AddrRanges, "-- Adding default %s\n", 442 defaultRange.to_string()); 443 } 444 445 // merge all interleaved ranges and add any range that is not 446 // a subset of the default range 447 std::vector<AddrRange> intlv_ranges; 448 for (const auto& r: portMap) { 449 // if the range is interleaved then save it for now 450 if (r.first.interleaved()) { 451 // if we already got interleaved ranges that are not 452 // part of the same range, then first do a merge 453 // before we add the new one 454 if (!intlv_ranges.empty() && 455 !intlv_ranges.back().mergesWith(r.first)) { 456 DPRINTF(AddrRanges, "-- Merging range from %d ranges\n", 457 intlv_ranges.size()); 458 AddrRange merged_range(intlv_ranges); 459 // next decide if we keep the merged range or not 460 if (!(useDefaultRange && 461 merged_range.isSubset(defaultRange))) { 462 xbarRanges.push_back(merged_range); 463 DPRINTF(AddrRanges, "-- Adding merged range %s\n", 464 merged_range.to_string()); 465 } 466 intlv_ranges.clear(); 467 } 468 intlv_ranges.push_back(r.first); 469 } else { 470 // keep the current range if not a subset of the default 471 if (!(useDefaultRange && 472 r.first.isSubset(defaultRange))) { 473 xbarRanges.push_back(r.first); 474 DPRINTF(AddrRanges, "-- Adding range %s\n", 475 r.first.to_string()); 476 } 477 } 478 } 479 480 // if there is still interleaved ranges waiting to be merged, 481 // go ahead and do it 482 if (!intlv_ranges.empty()) { 483 DPRINTF(AddrRanges, "-- Merging range from %d ranges\n", 484 intlv_ranges.size()); 485 AddrRange merged_range(intlv_ranges); 486 if (!(useDefaultRange && merged_range.isSubset(defaultRange))) { 487 xbarRanges.push_back(merged_range); 488 DPRINTF(AddrRanges, "-- Adding merged range %s\n", 489 merged_range.to_string()); 490 } 491 } 492 493 // also check that no range partially intersects with the 494 // default range, this has to be done after all ranges are set 495 // as there are no guarantees for when the default range is 496 // update with respect to the other ones 497 if (useDefaultRange) { 498 for (const auto& r: xbarRanges) { 499 // see if the new range is partially 500 // overlapping the default range 501 if (r.intersects(defaultRange) && 502 !r.isSubset(defaultRange)) 503 fatal("Range %s intersects the " \ 504 "default range of %s but is not a " \ 505 "subset\n", r.to_string(), name()); 506 } 507 } 508 509 // tell all our neighbouring master ports that our address 510 // ranges have changed 511 for (const auto& s: slavePorts) 512 s->sendRangeChange(); 513 } 514} 515 516AddrRangeList 517BaseXBar::getAddrRanges() const 518{ 519 // we should never be asked without first having sent a range 520 // change, and the latter is only done once we have all the ranges 521 // of the connected devices 522 assert(gotAllAddrRanges); 523 524 // at the moment, this never happens, as there are no cycles in 525 // the range queries and no devices on the master side of a crossbar 526 // (CPU, cache, bridge etc) actually care about the ranges of the 527 // ports they are connected to 528 529 DPRINTF(AddrRanges, "Received address range request\n"); 530 531 return xbarRanges; 532} 533 534void 535BaseXBar::regStats() 536{ 537 ClockedObject::regStats(); 538 539 using namespace Stats; 540 541 transDist 542 .init(MemCmd::NUM_MEM_CMDS) 543 .name(name() + ".trans_dist") 544 .desc("Transaction distribution") 545 .flags(nozero); 546 547 // get the string representation of the commands 548 for (int i = 0; i < MemCmd::NUM_MEM_CMDS; i++) { 549 MemCmd cmd(i); 550 const std::string &cstr = cmd.toString(); 551 transDist.subname(i, cstr); 552 } 553 554 pktCount 555 .init(slavePorts.size(), masterPorts.size()) 556 .name(name() + ".pkt_count") 557 .desc("Packet count per connected master and slave (bytes)") 558 .flags(total | nozero | nonan); 559 560 pktSize 561 .init(slavePorts.size(), masterPorts.size()) 562 .name(name() + ".pkt_size") 563 .desc("Cumulative packet size per connected master and slave (bytes)") 564 .flags(total | nozero | nonan); 565 566 // both the packet count and total size are two-dimensional 567 // vectors, indexed by slave port id and master port id, thus the 568 // neighbouring master and slave, they do not differentiate what 569 // came from the master and was forwarded to the slave (requests 570 // and snoop responses) and what came from the slave and was 571 // forwarded to the master (responses and snoop requests) 572 for (int i = 0; i < slavePorts.size(); i++) { 573 pktCount.subname(i, slavePorts[i]->getMasterPort().name()); 574 pktSize.subname(i, slavePorts[i]->getMasterPort().name()); 575 for (int j = 0; j < masterPorts.size(); j++) { 576 pktCount.ysubname(j, masterPorts[j]->getSlavePort().name()); 577 pktSize.ysubname(j, masterPorts[j]->getSlavePort().name()); 578 } 579 } 580} 581 582template <typename SrcType, typename DstType> 583DrainState 584BaseXBar::Layer<SrcType,DstType>::drain() 585{ 586 //We should check that we're not "doing" anything, and that noone is 587 //waiting. We might be idle but have someone waiting if the device we 588 //contacted for a retry didn't actually retry. 589 if (state != IDLE) { 590 DPRINTF(Drain, "Crossbar not drained\n"); 591 return DrainState::Draining; 592 } else { 593 return DrainState::Drained; 594 } 595} 596 597template <typename SrcType, typename DstType> 598void 599BaseXBar::Layer<SrcType,DstType>::regStats() 600{ 601 using namespace Stats; 602 603 occupancy 604 .name(name() + ".occupancy") 605 .desc("Layer occupancy (ticks)") 606 .flags(nozero); 607 608 utilization 609 .name(name() + ".utilization") 610 .desc("Layer utilization (%)") 611 .precision(1) 612 .flags(nozero); 613 614 utilization = 100 * occupancy / simTicks; 615} 616 617/** 618 * Crossbar layer template instantiations. Could be removed with _impl.hh 619 * file, but since there are only two given options (MasterPort and 620 * SlavePort) it seems a bit excessive at this point. 621 */ 622template class BaseXBar::Layer<SlavePort,MasterPort>; 623template class BaseXBar::Layer<MasterPort,SlavePort>; 624