coherent_xbar.cc revision 9713
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 // test if the bus should be considered occupied for the current 308 // port, do not use the destination port in the check as we do not 309 // know yet if it is to be passed on as a snoop response or normal 310 // response and we never block on either 311 if (!snoopRespLayer.tryTiming(src_port, InvalidPortID)) { 312 DPRINTF(CoherentBus, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n", 313 src_port->name(), pkt->cmdString(), pkt->getAddr()); 314 return false; 315 } 316 317 DPRINTF(CoherentBus, "recvTimingSnoop: src %s %s 0x%x\n", 318 src_port->name(), pkt->cmdString(), pkt->getAddr()); 319 320 // store size and command as they might be modified when 321 // forwarding the packet 322 unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0; 323 unsigned int pkt_cmd = pkt->cmdToIndex(); 324 325 // get the destination from the packet 326 PortID dest_port_id = pkt->getDest(); 327 328 // responses are never express snoops 329 assert(!pkt->isExpressSnoop()); 330 331 calcPacketTiming(pkt); 332 Tick packetFinishTime = pkt->busLastWordDelay + curTick(); 333 334 // determine if the response is from a snoop request we 335 // created as the result of a normal request (in which case it 336 // should be in the outstandingReq), or if we merely forwarded 337 // someone else's snoop request 338 if (outstandingReq.find(pkt->req) == outstandingReq.end()) { 339 // this is a snoop response to a snoop request we 340 // forwarded, e.g. coming from the L1 and going to the L2 341 // this should be forwarded as a snoop response 342 bool success M5_VAR_USED = 343 masterPorts[dest_port_id]->sendTimingSnoopResp(pkt); 344 pktCount[slave_port_id][dest_port_id]++; 345 totPktSize[slave_port_id][dest_port_id] += pkt_size; 346 assert(success); 347 } else { 348 // we got a snoop response on one of our slave ports, 349 // i.e. from a coherent master connected to the bus, and 350 // since we created the snoop request as part of 351 // recvTiming, this should now be a normal response again 352 outstandingReq.erase(pkt->req); 353 354 // this is a snoop response from a coherent master, with a 355 // destination field set on its way through the bus as 356 // request, hence it should never go back to where the 357 // snoop response came from, but instead to where the 358 // original request came from 359 assert(slave_port_id != dest_port_id); 360 361 // as a normal response, it should go back to a master 362 // through one of our slave ports 363 bool success M5_VAR_USED = 364 slavePorts[dest_port_id]->sendTimingResp(pkt); 365 366 // currently it is illegal to block responses... can lead 367 // to deadlock 368 assert(success); 369 } 370 371 snoopRespLayer.succeededTiming(packetFinishTime); 372 373 // stats updates 374 transDist[pkt_cmd]++; 375 snoopDataThroughBus += pkt_size; 376 377 return true; 378} 379 380 381void 382CoherentBus::forwardTiming(PacketPtr pkt, PortID exclude_slave_port_id) 383{ 384 DPRINTF(CoherentBus, "%s for %s address %x size %d\n", __func__, 385 pkt->cmdString(), pkt->getAddr(), pkt->getSize()); 386 387 // snoops should only happen if the system isn't bypassing caches 388 assert(!system->bypassCaches()); 389 390 for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) { 391 SlavePort *p = *s; 392 // we could have gotten this request from a snooping master 393 // (corresponding to our own slave port that is also in 394 // snoopPorts) and should not send it back to where it came 395 // from 396 if (exclude_slave_port_id == InvalidPortID || 397 p->getId() != exclude_slave_port_id) { 398 // cache is not allowed to refuse snoop 399 p->sendTimingSnoopReq(pkt); 400 } 401 } 402} 403 404void 405CoherentBus::recvRetry(PortID master_port_id) 406{ 407 // responses and snoop responses never block on forwarding them, 408 // so the retry will always be coming from a port to which we 409 // tried to forward a request 410 reqLayer.recvRetry(master_port_id); 411} 412 413Tick 414CoherentBus::recvAtomic(PacketPtr pkt, PortID slave_port_id) 415{ 416 DPRINTF(CoherentBus, "recvAtomic: packet src %s addr 0x%x cmd %s\n", 417 slavePorts[slave_port_id]->name(), pkt->getAddr(), 418 pkt->cmdString()); 419 420 // add the request data 421 dataThroughBus += pkt->hasData() ? pkt->getSize() : 0; 422 423 MemCmd snoop_response_cmd = MemCmd::InvalidCmd; 424 Tick snoop_response_latency = 0; 425 426 // uncacheable requests need never be snooped 427 if (!pkt->req->isUncacheable() && !system->bypassCaches()) { 428 // forward to all snoopers but the source 429 std::pair<MemCmd, Tick> snoop_result = 430 forwardAtomic(pkt, slave_port_id); 431 snoop_response_cmd = snoop_result.first; 432 snoop_response_latency = snoop_result.second; 433 } 434 435 // even if we had a snoop response, we must continue and also 436 // perform the actual request at the destination 437 PortID dest_id = findPort(pkt->getAddr()); 438 439 // forward the request to the appropriate destination 440 Tick response_latency = masterPorts[dest_id]->sendAtomic(pkt); 441 442 // if we got a response from a snooper, restore it here 443 if (snoop_response_cmd != MemCmd::InvalidCmd) { 444 // no one else should have responded 445 assert(!pkt->isResponse()); 446 pkt->cmd = snoop_response_cmd; 447 response_latency = snoop_response_latency; 448 } 449 450 // add the response data 451 if (pkt->isResponse()) 452 dataThroughBus += pkt->hasData() ? pkt->getSize() : 0; 453 454 // @todo: Not setting first-word time 455 pkt->busLastWordDelay = response_latency; 456 return response_latency; 457} 458 459Tick 460CoherentBus::recvAtomicSnoop(PacketPtr pkt, PortID master_port_id) 461{ 462 DPRINTF(CoherentBus, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n", 463 masterPorts[master_port_id]->name(), pkt->getAddr(), 464 pkt->cmdString()); 465 466 // add the request snoop data 467 snoopDataThroughBus += pkt->hasData() ? pkt->getSize() : 0; 468 469 // forward to all snoopers 470 std::pair<MemCmd, Tick> snoop_result = 471 forwardAtomic(pkt, InvalidPortID); 472 MemCmd snoop_response_cmd = snoop_result.first; 473 Tick snoop_response_latency = snoop_result.second; 474 475 if (snoop_response_cmd != MemCmd::InvalidCmd) 476 pkt->cmd = snoop_response_cmd; 477 478 // add the response snoop data 479 if (pkt->isResponse()) 480 snoopDataThroughBus += pkt->hasData() ? pkt->getSize() : 0; 481 482 // @todo: Not setting first-word time 483 pkt->busLastWordDelay = snoop_response_latency; 484 return snoop_response_latency; 485} 486 487std::pair<MemCmd, Tick> 488CoherentBus::forwardAtomic(PacketPtr pkt, PortID exclude_slave_port_id) 489{ 490 // the packet may be changed on snoops, record the original 491 // command to enable us to restore it between snoops so that 492 // additional snoops can take place properly 493 MemCmd orig_cmd = pkt->cmd; 494 MemCmd snoop_response_cmd = MemCmd::InvalidCmd; 495 Tick snoop_response_latency = 0; 496 497 // snoops should only happen if the system isn't bypassing caches 498 assert(!system->bypassCaches()); 499 500 for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) { 501 SlavePort *p = *s; 502 // we could have gotten this request from a snooping master 503 // (corresponding to our own slave port that is also in 504 // snoopPorts) and should not send it back to where it came 505 // from 506 if (exclude_slave_port_id == InvalidPortID || 507 p->getId() != exclude_slave_port_id) { 508 Tick latency = p->sendAtomicSnoop(pkt); 509 // in contrast to a functional access, we have to keep on 510 // going as all snoopers must be updated even if we get a 511 // response 512 if (pkt->isResponse()) { 513 // response from snoop agent 514 assert(pkt->cmd != orig_cmd); 515 assert(pkt->memInhibitAsserted()); 516 // should only happen once 517 assert(snoop_response_cmd == MemCmd::InvalidCmd); 518 // save response state 519 snoop_response_cmd = pkt->cmd; 520 snoop_response_latency = latency; 521 // restore original packet state for remaining snoopers 522 pkt->cmd = orig_cmd; 523 } 524 } 525 } 526 527 // the packet is restored as part of the loop and any potential 528 // snoop response is part of the returned pair 529 return std::make_pair(snoop_response_cmd, snoop_response_latency); 530} 531 532void 533CoherentBus::recvFunctional(PacketPtr pkt, PortID slave_port_id) 534{ 535 if (!pkt->isPrint()) { 536 // don't do DPRINTFs on PrintReq as it clutters up the output 537 DPRINTF(CoherentBus, 538 "recvFunctional: packet src %s addr 0x%x cmd %s\n", 539 slavePorts[slave_port_id]->name(), pkt->getAddr(), 540 pkt->cmdString()); 541 } 542 543 // uncacheable requests need never be snooped 544 if (!pkt->req->isUncacheable() && !system->bypassCaches()) { 545 // forward to all snoopers but the source 546 forwardFunctional(pkt, slave_port_id); 547 } 548 549 // there is no need to continue if the snooping has found what we 550 // were looking for and the packet is already a response 551 if (!pkt->isResponse()) { 552 PortID dest_id = findPort(pkt->getAddr()); 553 554 masterPorts[dest_id]->sendFunctional(pkt); 555 } 556} 557 558void 559CoherentBus::recvFunctionalSnoop(PacketPtr pkt, PortID master_port_id) 560{ 561 if (!pkt->isPrint()) { 562 // don't do DPRINTFs on PrintReq as it clutters up the output 563 DPRINTF(CoherentBus, 564 "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n", 565 masterPorts[master_port_id]->name(), pkt->getAddr(), 566 pkt->cmdString()); 567 } 568 569 // forward to all snoopers 570 forwardFunctional(pkt, InvalidPortID); 571} 572 573void 574CoherentBus::forwardFunctional(PacketPtr pkt, PortID exclude_slave_port_id) 575{ 576 // snoops should only happen if the system isn't bypassing caches 577 assert(!system->bypassCaches()); 578 579 for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) { 580 SlavePort *p = *s; 581 // we could have gotten this request from a snooping master 582 // (corresponding to our own slave port that is also in 583 // snoopPorts) and should not send it back to where it came 584 // from 585 if (exclude_slave_port_id == InvalidPortID || 586 p->getId() != exclude_slave_port_id) 587 p->sendFunctionalSnoop(pkt); 588 589 // if we get a response we are done 590 if (pkt->isResponse()) { 591 break; 592 } 593 } 594} 595 596unsigned int 597CoherentBus::drain(DrainManager *dm) 598{ 599 // sum up the individual layers 600 return reqLayer.drain(dm) + respLayer.drain(dm) + snoopRespLayer.drain(dm); 601} 602 603void 604CoherentBus::regStats() 605{ 606 // register the stats of the base class and our three bus layers 607 BaseBus::regStats(); 608 reqLayer.regStats(); 609 respLayer.regStats(); 610 snoopRespLayer.regStats(); 611 612 dataThroughBus 613 .name(name() + ".data_through_bus") 614 .desc("Total data (bytes)") 615 ; 616 617 snoopDataThroughBus 618 .name(name() + ".snoop_data_through_bus") 619 .desc("Total snoop data (bytes)") 620 ; 621 622 throughput 623 .name(name() + ".throughput") 624 .desc("Throughput (bytes/s)") 625 .precision(0) 626 ; 627 628 throughput = (dataThroughBus + snoopDataThroughBus) / simSeconds; 629} 630 631CoherentBus * 632CoherentBusParams::create() 633{ 634 return new CoherentBus(this); 635} 636