coherent_xbar.cc revision 9714
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/BusAddrRanges.hh" 53#include "debug/CoherentBus.hh" 54#include "mem/coherent_bus.hh" 55#include "sim/system.hh" 56 57CoherentBus::CoherentBus(const CoherentBusParams *p) 58 : BaseBus(p), 59 reqLayer(*this, ".reqLayer", p->port_master_connection_count + 60 p->port_default_connection_count), 61 respLayer(*this, ".respLayer", p->port_slave_connection_count), 62 snoopRespLayer(*this, ".snoopRespLayer", 63 p->port_master_connection_count + 64 p->port_default_connection_count), 65 system(p->system) 66{ 67 // create the ports based on the size of the master and slave 68 // vector ports, and the presence of the default port, the ports 69 // are enumerated starting from zero 70 for (int i = 0; i < p->port_master_connection_count; ++i) { 71 std::string portName = csprintf("%s.master[%d]", name(), i); 72 MasterPort* bp = new CoherentBusMasterPort(portName, *this, i); 73 masterPorts.push_back(bp); 74 } 75 76 // see if we have a default slave device connected and if so add 77 // our corresponding master port 78 if (p->port_default_connection_count) { 79 defaultPortID = masterPorts.size(); 80 std::string portName = name() + ".default"; 81 MasterPort* bp = new CoherentBusMasterPort(portName, *this, 82 defaultPortID); 83 masterPorts.push_back(bp); 84 } 85 86 // create the slave ports, once again starting at zero 87 for (int i = 0; i < p->port_slave_connection_count; ++i) { 88 std::string portName = csprintf("%s.slave[%d]", name(), i); 89 SlavePort* bp = new CoherentBusSlavePort(portName, *this, i); 90 slavePorts.push_back(bp); 91 } 92 93 clearPortCache(); 94} 95 96void 97CoherentBus::init() 98{ 99 // the base class is responsible for determining the block size 100 BaseBus::init(); 101 102 // iterate over our slave ports and determine which of our 103 // neighbouring master ports are snooping and add them as snoopers 104 for (SlavePortConstIter p = slavePorts.begin(); p != slavePorts.end(); 105 ++p) { 106 // check if the connected master port is snooping 107 if ((*p)->isSnooping()) { 108 DPRINTF(BusAddrRanges, "Adding snooping master %s\n", 109 (*p)->getMasterPort().name()); 110 snoopPorts.push_back(*p); 111 } 112 } 113 114 if (snoopPorts.empty()) 115 warn("CoherentBus %s has no snooping ports attached!\n", name()); 116} 117 118bool 119CoherentBus::recvTimingReq(PacketPtr pkt, PortID slave_port_id) 120{ 121 // determine the source port based on the id 122 SlavePort *src_port = slavePorts[slave_port_id]; 123 124 // remember if the packet is an express snoop 125 bool is_express_snoop = pkt->isExpressSnoop(); 126 127 // determine the destination based on the address 128 PortID master_port_id = findPort(pkt->getAddr()); 129 130 // test if the bus should be considered occupied for the current 131 // port, and exclude express snoops from the check 132 if (!is_express_snoop && !reqLayer.tryTiming(src_port, master_port_id)) { 133 DPRINTF(CoherentBus, "recvTimingReq: src %s %s 0x%x BUS BUSY\n", 134 src_port->name(), pkt->cmdString(), pkt->getAddr()); 135 return false; 136 } 137 138 DPRINTF(CoherentBus, "recvTimingReq: src %s %s expr %d 0x%x\n", 139 src_port->name(), pkt->cmdString(), is_express_snoop, 140 pkt->getAddr()); 141 142 // store size and command as they might be modified when 143 // forwarding the packet 144 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0; 145 unsigned int pkt_cmd = pkt->cmdToIndex(); 146 147 // set the source port for routing of the response 148 pkt->setSrc(slave_port_id); 149 150 calcPacketTiming(pkt); 151 Tick packetFinishTime = pkt->busLastWordDelay + curTick(); 152 153 // uncacheable requests need never be snooped 154 if (!pkt->req->isUncacheable() && !system->bypassCaches()) { 155 // the packet is a memory-mapped request and should be 156 // broadcasted to our snoopers but the source 157 forwardTiming(pkt, slave_port_id); 158 } 159 160 // remember if we add an outstanding req so we can undo it if 161 // necessary, if the packet needs a response, we should add it 162 // as outstanding and express snoops never fail so there is 163 // not need to worry about them 164 bool add_outstanding = !is_express_snoop && pkt->needsResponse(); 165 166 // keep track that we have an outstanding request packet 167 // matching this request, this is used by the coherency 168 // mechanism in determining what to do with snoop responses 169 // (in recvTimingSnoop) 170 if (add_outstanding) { 171 // we should never have an exsiting request outstanding 172 assert(outstandingReq.find(pkt->req) == outstandingReq.end()); 173 outstandingReq.insert(pkt->req); 174 } 175 176 // since it is a normal request, attempt to send the packet 177 bool success = masterPorts[master_port_id]->sendTimingReq(pkt); 178 179 // if this is an express snoop, we are done at this point 180 if (is_express_snoop) { 181 assert(success); 182 snoopDataThroughBus += pkt_size; 183 } else { 184 // for normal requests, check if successful 185 if (!success) { 186 // inhibited packets should never be forced to retry 187 assert(!pkt->memInhibitAsserted()); 188 189 // if it was added as outstanding and the send failed, then 190 // erase it again 191 if (add_outstanding) 192 outstandingReq.erase(pkt->req); 193 194 // undo the calculation so we can check for 0 again 195 pkt->busFirstWordDelay = pkt->busLastWordDelay = 0; 196 197 DPRINTF(CoherentBus, "recvTimingReq: src %s %s 0x%x RETRY\n", 198 src_port->name(), pkt->cmdString(), pkt->getAddr()); 199 200 // update the bus state and schedule an idle event 201 reqLayer.failedTiming(src_port, master_port_id, 202 clockEdge(headerCycles)); 203 } else { 204 // update the bus state and schedule an idle event 205 reqLayer.succeededTiming(packetFinishTime); 206 dataThroughBus += pkt_size; 207 } 208 } 209 210 // stats updates only consider packets that were successfully sent 211 if (success) { 212 pktCount[slave_port_id][master_port_id]++; 213 totPktSize[slave_port_id][master_port_id] += pkt_size; 214 transDist[pkt_cmd]++; 215 } 216 217 return success; 218} 219 220bool 221CoherentBus::recvTimingResp(PacketPtr pkt, PortID master_port_id) 222{ 223 // determine the source port based on the id 224 MasterPort *src_port = masterPorts[master_port_id]; 225 226 // determine the destination based on what is stored in the packet 227 PortID slave_port_id = pkt->getDest(); 228 229 // test if the bus should be considered occupied for the current 230 // port 231 if (!respLayer.tryTiming(src_port, slave_port_id)) { 232 DPRINTF(CoherentBus, "recvTimingResp: src %s %s 0x%x BUSY\n", 233 src_port->name(), pkt->cmdString(), pkt->getAddr()); 234 return false; 235 } 236 237 DPRINTF(CoherentBus, "recvTimingResp: src %s %s 0x%x\n", 238 src_port->name(), pkt->cmdString(), pkt->getAddr()); 239 240 // store size and command as they might be modified when 241 // forwarding the packet 242 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0; 243 unsigned int pkt_cmd = pkt->cmdToIndex(); 244 245 calcPacketTiming(pkt); 246 Tick packetFinishTime = pkt->busLastWordDelay + curTick(); 247 248 // the packet is a normal response to a request that we should 249 // have seen passing through the bus 250 assert(outstandingReq.find(pkt->req) != outstandingReq.end()); 251 252 // remove it as outstanding 253 outstandingReq.erase(pkt->req); 254 255 // send the packet through the destination slave port 256 bool success M5_VAR_USED = slavePorts[slave_port_id]->sendTimingResp(pkt); 257 258 // currently it is illegal to block responses... can lead to 259 // deadlock 260 assert(success); 261 262 respLayer.succeededTiming(packetFinishTime); 263 264 // stats updates 265 dataThroughBus += pkt_size; 266 pktCount[slave_port_id][master_port_id]++; 267 totPktSize[slave_port_id][master_port_id] += pkt_size; 268 transDist[pkt_cmd]++; 269 270 return true; 271} 272 273void 274CoherentBus::recvTimingSnoopReq(PacketPtr pkt, PortID master_port_id) 275{ 276 DPRINTF(CoherentBus, "recvTimingSnoopReq: src %s %s 0x%x\n", 277 masterPorts[master_port_id]->name(), pkt->cmdString(), 278 pkt->getAddr()); 279 280 // update stats here as we know the forwarding will succeed 281 transDist[pkt->cmdToIndex()]++; 282 snoopDataThroughBus += pkt->hasData() ? pkt->getSize() : 0; 283 284 // we should only see express snoops from caches 285 assert(pkt->isExpressSnoop()); 286 287 // set the source port for routing of the response 288 pkt->setSrc(master_port_id); 289 290 // forward to all snoopers 291 forwardTiming(pkt, InvalidPortID); 292 293 // a snoop request came from a connected slave device (one of 294 // our master ports), and if it is not coming from the slave 295 // device responsible for the address range something is 296 // wrong, hence there is nothing further to do as the packet 297 // would be going back to where it came from 298 assert(master_port_id == findPort(pkt->getAddr())); 299} 300 301bool 302CoherentBus::recvTimingSnoopResp(PacketPtr pkt, PortID slave_port_id) 303{ 304 // determine the source port based on the id 305 SlavePort* src_port = slavePorts[slave_port_id]; 306 307 // get the destination from the packet 308 PortID dest_port_id = pkt->getDest(); 309 310 // determine if the response is from a snoop request we 311 // created as the result of a normal request (in which case it 312 // should be in the outstandingReq), or if we merely forwarded 313 // someone else's snoop request 314 bool forwardAsSnoop = outstandingReq.find(pkt->req) == 315 outstandingReq.end(); 316 317 // test if the bus should be considered occupied for the current 318 // port, note that the check is bypassed if the response is being 319 // passed on as a normal response since this is occupying the 320 // response layer rather than the snoop response layer 321 if (forwardAsSnoop && !snoopRespLayer.tryTiming(src_port, dest_port_id)) { 322 DPRINTF(CoherentBus, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n", 323 src_port->name(), pkt->cmdString(), pkt->getAddr()); 324 return false; 325 } 326 327 DPRINTF(CoherentBus, "recvTimingSnoopResp: src %s %s 0x%x\n", 328 src_port->name(), pkt->cmdString(), pkt->getAddr()); 329 330 // store size and command as they might be modified when 331 // forwarding the packet 332 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0; 333 unsigned int pkt_cmd = pkt->cmdToIndex(); 334 335 // responses are never express snoops 336 assert(!pkt->isExpressSnoop()); 337 338 calcPacketTiming(pkt); 339 Tick packetFinishTime = pkt->busLastWordDelay + curTick(); 340 341 // forward it either as a snoop response or a normal response 342 if (forwardAsSnoop) { 343 // this is a snoop response to a snoop request we forwarded, 344 // e.g. coming from the L1 and going to the L2, and it should 345 // be forwarded as a snoop response 346 bool success M5_VAR_USED = 347 masterPorts[dest_port_id]->sendTimingSnoopResp(pkt); 348 pktCount[slave_port_id][dest_port_id]++; 349 totPktSize[slave_port_id][dest_port_id] += pkt_size; 350 assert(success); 351 352 snoopRespLayer.succeededTiming(packetFinishTime); 353 } else { 354 // we got a snoop response on one of our slave ports, 355 // i.e. from a coherent master connected to the bus, and 356 // since we created the snoop request as part of 357 // recvTiming, this should now be a normal response again 358 outstandingReq.erase(pkt->req); 359 360 // this is a snoop response from a coherent master, with a 361 // destination field set on its way through the bus as 362 // request, hence it should never go back to where the 363 // snoop response came from, but instead to where the 364 // original request came from 365 assert(slave_port_id != dest_port_id); 366 367 // as a normal response, it should go back to a master through 368 // one of our slave ports, at this point we are ignoring the 369 // fact that the response layer could be busy and do not touch 370 // its state 371 bool success M5_VAR_USED = 372 slavePorts[dest_port_id]->sendTimingResp(pkt); 373 374 // @todo Put the response in an internal FIFO and pass it on 375 // to the response layer from there 376 377 // currently it is illegal to block responses... can lead 378 // to deadlock 379 assert(success); 380 } 381 382 // stats updates 383 transDist[pkt_cmd]++; 384 snoopDataThroughBus += pkt_size; 385 386 return true; 387} 388 389 390void 391CoherentBus::forwardTiming(PacketPtr pkt, PortID exclude_slave_port_id) 392{ 393 DPRINTF(CoherentBus, "%s for %s address %x size %d\n", __func__, 394 pkt->cmdString(), pkt->getAddr(), pkt->getSize()); 395 396 // snoops should only happen if the system isn't bypassing caches 397 assert(!system->bypassCaches()); 398 399 for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) { 400 SlavePort *p = *s; 401 // we could have gotten this request from a snooping master 402 // (corresponding to our own slave port that is also in 403 // snoopPorts) and should not send it back to where it came 404 // from 405 if (exclude_slave_port_id == InvalidPortID || 406 p->getId() != exclude_slave_port_id) { 407 // cache is not allowed to refuse snoop 408 p->sendTimingSnoopReq(pkt); 409 } 410 } 411} 412 413void 414CoherentBus::recvRetry(PortID master_port_id) 415{ 416 // responses and snoop responses never block on forwarding them, 417 // so the retry will always be coming from a port to which we 418 // tried to forward a request 419 reqLayer.recvRetry(master_port_id); 420} 421 422Tick 423CoherentBus::recvAtomic(PacketPtr pkt, PortID slave_port_id) 424{ 425 DPRINTF(CoherentBus, "recvAtomic: packet src %s addr 0x%x cmd %s\n", 426 slavePorts[slave_port_id]->name(), pkt->getAddr(), 427 pkt->cmdString()); 428 429 // add the request data 430 dataThroughBus += pkt->hasData() ? pkt->getSize() : 0; 431 432 MemCmd snoop_response_cmd = MemCmd::InvalidCmd; 433 Tick snoop_response_latency = 0; 434 435 // uncacheable requests need never be snooped 436 if (!pkt->req->isUncacheable() && !system->bypassCaches()) { 437 // forward to all snoopers but the source 438 std::pair<MemCmd, Tick> snoop_result = 439 forwardAtomic(pkt, slave_port_id); 440 snoop_response_cmd = snoop_result.first; 441 snoop_response_latency = snoop_result.second; 442 } 443 444 // even if we had a snoop response, we must continue and also 445 // perform the actual request at the destination 446 PortID dest_id = findPort(pkt->getAddr()); 447 448 // forward the request to the appropriate destination 449 Tick response_latency = masterPorts[dest_id]->sendAtomic(pkt); 450 451 // if we got a response from a snooper, restore it here 452 if (snoop_response_cmd != MemCmd::InvalidCmd) { 453 // no one else should have responded 454 assert(!pkt->isResponse()); 455 pkt->cmd = snoop_response_cmd; 456 response_latency = snoop_response_latency; 457 } 458 459 // add the response data 460 if (pkt->isResponse()) 461 dataThroughBus += pkt->hasData() ? pkt->getSize() : 0; 462 463 // @todo: Not setting first-word time 464 pkt->busLastWordDelay = response_latency; 465 return response_latency; 466} 467 468Tick 469CoherentBus::recvAtomicSnoop(PacketPtr pkt, PortID master_port_id) 470{ 471 DPRINTF(CoherentBus, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n", 472 masterPorts[master_port_id]->name(), pkt->getAddr(), 473 pkt->cmdString()); 474 475 // add the request snoop data 476 snoopDataThroughBus += pkt->hasData() ? pkt->getSize() : 0; 477 478 // forward to all snoopers 479 std::pair<MemCmd, Tick> snoop_result = 480 forwardAtomic(pkt, InvalidPortID); 481 MemCmd snoop_response_cmd = snoop_result.first; 482 Tick snoop_response_latency = snoop_result.second; 483 484 if (snoop_response_cmd != MemCmd::InvalidCmd) 485 pkt->cmd = snoop_response_cmd; 486 487 // add the response snoop data 488 if (pkt->isResponse()) 489 snoopDataThroughBus += pkt->hasData() ? pkt->getSize() : 0; 490 491 // @todo: Not setting first-word time 492 pkt->busLastWordDelay = snoop_response_latency; 493 return snoop_response_latency; 494} 495 496std::pair<MemCmd, Tick> 497CoherentBus::forwardAtomic(PacketPtr pkt, PortID exclude_slave_port_id) 498{ 499 // the packet may be changed on snoops, record the original 500 // command to enable us to restore it between snoops so that 501 // additional snoops can take place properly 502 MemCmd orig_cmd = pkt->cmd; 503 MemCmd snoop_response_cmd = MemCmd::InvalidCmd; 504 Tick snoop_response_latency = 0; 505 506 // snoops should only happen if the system isn't bypassing caches 507 assert(!system->bypassCaches()); 508 509 for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) { 510 SlavePort *p = *s; 511 // we could have gotten this request from a snooping master 512 // (corresponding to our own slave port that is also in 513 // snoopPorts) and should not send it back to where it came 514 // from 515 if (exclude_slave_port_id == InvalidPortID || 516 p->getId() != exclude_slave_port_id) { 517 Tick latency = p->sendAtomicSnoop(pkt); 518 // in contrast to a functional access, we have to keep on 519 // going as all snoopers must be updated even if we get a 520 // response 521 if (pkt->isResponse()) { 522 // response from snoop agent 523 assert(pkt->cmd != orig_cmd); 524 assert(pkt->memInhibitAsserted()); 525 // should only happen once 526 assert(snoop_response_cmd == MemCmd::InvalidCmd); 527 // save response state 528 snoop_response_cmd = pkt->cmd; 529 snoop_response_latency = latency; 530 // restore original packet state for remaining snoopers 531 pkt->cmd = orig_cmd; 532 } 533 } 534 } 535 536 // the packet is restored as part of the loop and any potential 537 // snoop response is part of the returned pair 538 return std::make_pair(snoop_response_cmd, snoop_response_latency); 539} 540 541void 542CoherentBus::recvFunctional(PacketPtr pkt, PortID slave_port_id) 543{ 544 if (!pkt->isPrint()) { 545 // don't do DPRINTFs on PrintReq as it clutters up the output 546 DPRINTF(CoherentBus, 547 "recvFunctional: packet src %s addr 0x%x cmd %s\n", 548 slavePorts[slave_port_id]->name(), pkt->getAddr(), 549 pkt->cmdString()); 550 } 551 552 // uncacheable requests need never be snooped 553 if (!pkt->req->isUncacheable() && !system->bypassCaches()) { 554 // forward to all snoopers but the source 555 forwardFunctional(pkt, slave_port_id); 556 } 557 558 // there is no need to continue if the snooping has found what we 559 // were looking for and the packet is already a response 560 if (!pkt->isResponse()) { 561 PortID dest_id = findPort(pkt->getAddr()); 562 563 masterPorts[dest_id]->sendFunctional(pkt); 564 } 565} 566 567void 568CoherentBus::recvFunctionalSnoop(PacketPtr pkt, PortID master_port_id) 569{ 570 if (!pkt->isPrint()) { 571 // don't do DPRINTFs on PrintReq as it clutters up the output 572 DPRINTF(CoherentBus, 573 "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n", 574 masterPorts[master_port_id]->name(), pkt->getAddr(), 575 pkt->cmdString()); 576 } 577 578 // forward to all snoopers 579 forwardFunctional(pkt, InvalidPortID); 580} 581 582void 583CoherentBus::forwardFunctional(PacketPtr pkt, PortID exclude_slave_port_id) 584{ 585 // snoops should only happen if the system isn't bypassing caches 586 assert(!system->bypassCaches()); 587 588 for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) { 589 SlavePort *p = *s; 590 // we could have gotten this request from a snooping master 591 // (corresponding to our own slave port that is also in 592 // snoopPorts) and should not send it back to where it came 593 // from 594 if (exclude_slave_port_id == InvalidPortID || 595 p->getId() != exclude_slave_port_id) 596 p->sendFunctionalSnoop(pkt); 597 598 // if we get a response we are done 599 if (pkt->isResponse()) { 600 break; 601 } 602 } 603} 604 605unsigned int 606CoherentBus::drain(DrainManager *dm) 607{ 608 // sum up the individual layers 609 return reqLayer.drain(dm) + respLayer.drain(dm) + snoopRespLayer.drain(dm); 610} 611 612void 613CoherentBus::regStats() 614{ 615 // register the stats of the base class and our three bus layers 616 BaseBus::regStats(); 617 reqLayer.regStats(); 618 respLayer.regStats(); 619 snoopRespLayer.regStats(); 620 621 dataThroughBus 622 .name(name() + ".data_through_bus") 623 .desc("Total data (bytes)") 624 ; 625 626 snoopDataThroughBus 627 .name(name() + ".snoop_data_through_bus") 628 .desc("Total snoop data (bytes)") 629 ; 630 631 throughput 632 .name(name() + ".throughput") 633 .desc("Throughput (bytes/s)") 634 .precision(0) 635 ; 636 637 throughput = (dataThroughBus + snoopDataThroughBus) / simSeconds; 638} 639 640CoherentBus * 641CoherentBusParams::create() 642{ 643 return new CoherentBus(this); 644} 645