xbar.cc revision 8948
14661Sksewell@umich.edu/* 25268Sksewell@umich.edu * Copyright (c) 2011-2012 ARM Limited 35268Sksewell@umich.edu * All rights reserved 44661Sksewell@umich.edu * 55268Sksewell@umich.edu * The license below extends only to copyright in the software and shall 65268Sksewell@umich.edu * not be construed as granting a license to any other intellectual 75268Sksewell@umich.edu * property including but not limited to intellectual property relating 85268Sksewell@umich.edu * to a hardware implementation of the functionality of the software 95268Sksewell@umich.edu * licensed hereunder. You may use the software subject to the license 105268Sksewell@umich.edu * terms below provided that you ensure that this notice is replicated 115268Sksewell@umich.edu * unmodified and in its entirety in all distributions of the software, 125268Sksewell@umich.edu * modified or unmodified, in source code or in binary form. 135268Sksewell@umich.edu * 145268Sksewell@umich.edu * Copyright (c) 2006 The Regents of The University of Michigan 154661Sksewell@umich.edu * All rights reserved. 165268Sksewell@umich.edu * 175268Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without 185268Sksewell@umich.edu * modification, are permitted provided that the following conditions are 195268Sksewell@umich.edu * met: redistributions of source code must retain the above copyright 205268Sksewell@umich.edu * notice, this list of conditions and the following disclaimer; 215268Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright 225268Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the 235268Sksewell@umich.edu * documentation and/or other materials provided with the distribution; 245268Sksewell@umich.edu * neither the name of the copyright holders nor the names of its 255268Sksewell@umich.edu * contributors may be used to endorse or promote products derived from 265268Sksewell@umich.edu * this software without specific prior written permission. 275222Sksewell@umich.edu * 285254Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 294661Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 304661Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 314661Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 324661Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 334661Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 344661Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 354661Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 364661Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 374661Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 384661Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 394661Sksewell@umich.edu * 404661Sksewell@umich.edu * Authors: Ali Saidi 414661Sksewell@umich.edu * Andreas Hansson 424661Sksewell@umich.edu * William Wang 434661Sksewell@umich.edu */ 444661Sksewell@umich.edu 454661Sksewell@umich.edu/** 464661Sksewell@umich.edu * @file 474661Sksewell@umich.edu * Definition of a bus object. 484661Sksewell@umich.edu */ 494661Sksewell@umich.edu 504661Sksewell@umich.edu#include "base/misc.hh" 514661Sksewell@umich.edu#include "base/trace.hh" 524661Sksewell@umich.edu#include "debug/Bus.hh" 534661Sksewell@umich.edu#include "debug/BusAddrRanges.hh" 544661Sksewell@umich.edu#include "mem/bus.hh" 554661Sksewell@umich.edu 564661Sksewell@umich.eduBus::Bus(const BusParams *p) 574661Sksewell@umich.edu : MemObject(p), clock(p->clock), 584661Sksewell@umich.edu headerCycles(p->header_cycles), width(p->width), tickNextIdle(0), 594661Sksewell@umich.edu drainEvent(NULL), busIdleEvent(this), inRetry(false), 604661Sksewell@umich.edu defaultPortId(INVALID_PORT_ID), useDefaultRange(p->use_default_range), 614661Sksewell@umich.edu defaultBlockSize(p->block_size), 624661Sksewell@umich.edu cachedBlockSize(0), cachedBlockSizeValid(false) 634661Sksewell@umich.edu{ 644661Sksewell@umich.edu //width, clock period, and header cycles must be positive 654661Sksewell@umich.edu if (width <= 0) 664661Sksewell@umich.edu fatal("Bus width must be positive\n"); 674661Sksewell@umich.edu if (clock <= 0) 684661Sksewell@umich.edu fatal("Bus clock period must be positive\n"); 694661Sksewell@umich.edu if (headerCycles <= 0) 704661Sksewell@umich.edu fatal("Number of header cycles must be positive\n"); 714661Sksewell@umich.edu 724661Sksewell@umich.edu // create the ports based on the size of the master and slave 734661Sksewell@umich.edu // vector ports, and the presence of the default port, the ports 744661Sksewell@umich.edu // are enumerated starting from zero 754661Sksewell@umich.edu for (int i = 0; i < p->port_master_connection_count; ++i) { 764661Sksewell@umich.edu std::string portName = csprintf("%s-p%d", name(), i); 774661Sksewell@umich.edu BusMasterPort* bp = new BusMasterPort(portName, this, i); 784661Sksewell@umich.edu masterPorts.push_back(bp); 794661Sksewell@umich.edu } 804661Sksewell@umich.edu 815715Shsul@eecs.umich.edu // see if we have a default slave device connected and if so add 824661Sksewell@umich.edu // our corresponding master port 834661Sksewell@umich.edu if (p->port_default_connection_count) { 844661Sksewell@umich.edu defaultPortId = masterPorts.size(); 854661Sksewell@umich.edu std::string portName = csprintf("%s-default", name()); 864661Sksewell@umich.edu BusMasterPort* bp = new BusMasterPort(portName, this, defaultPortId); 874661Sksewell@umich.edu masterPorts.push_back(bp); 884661Sksewell@umich.edu } 894661Sksewell@umich.edu 904661Sksewell@umich.edu // create the slave ports, once again starting at zero 914661Sksewell@umich.edu for (int i = 0; i < p->port_slave_connection_count; ++i) { 924661Sksewell@umich.edu std::string portName = csprintf("%s-p%d", name(), i); 934661Sksewell@umich.edu BusSlavePort* bp = new BusSlavePort(portName, this, i); 944661Sksewell@umich.edu slavePorts.push_back(bp); 954661Sksewell@umich.edu } 964661Sksewell@umich.edu 974661Sksewell@umich.edu clearPortCache(); 984661Sksewell@umich.edu} 994661Sksewell@umich.edu 1004661Sksewell@umich.eduMasterPort & 1015715Shsul@eecs.umich.eduBus::getMasterPort(const std::string &if_name, int idx) 1024661Sksewell@umich.edu{ 1034661Sksewell@umich.edu if (if_name == "master" && idx < masterPorts.size()) { 1044661Sksewell@umich.edu // the master port index translates directly to the vector position 1054661Sksewell@umich.edu return *masterPorts[idx]; 1064661Sksewell@umich.edu } else if (if_name == "default") { 1074661Sksewell@umich.edu return *masterPorts[defaultPortId]; 1084661Sksewell@umich.edu } else { 1094661Sksewell@umich.edu return MemObject::getMasterPort(if_name, idx); 1104661Sksewell@umich.edu } 1114661Sksewell@umich.edu} 1124661Sksewell@umich.edu 1134661Sksewell@umich.eduSlavePort & 1144661Sksewell@umich.eduBus::getSlavePort(const std::string &if_name, int idx) 1154661Sksewell@umich.edu{ 1164661Sksewell@umich.edu if (if_name == "slave" && idx < slavePorts.size()) { 1174661Sksewell@umich.edu // the slave port index translates directly to the vector position 1184661Sksewell@umich.edu return *slavePorts[idx]; 1194661Sksewell@umich.edu } else { 1204661Sksewell@umich.edu return MemObject::getSlavePort(if_name, idx); 1214661Sksewell@umich.edu } 1224661Sksewell@umich.edu} 1234661Sksewell@umich.edu 1244661Sksewell@umich.eduvoid 1254661Sksewell@umich.eduBus::init() 1264661Sksewell@umich.edu{ 1274661Sksewell@umich.edu std::vector<BusSlavePort*>::iterator p; 1284661Sksewell@umich.edu 1294661Sksewell@umich.edu // iterate over our slave ports and determine which of our 1304661Sksewell@umich.edu // neighbouring master ports are snooping and add them as snoopers 1314661Sksewell@umich.edu for (p = slavePorts.begin(); p != slavePorts.end(); ++p) { 1324661Sksewell@umich.edu if ((*p)->getMasterPort().isSnooping()) { 1334661Sksewell@umich.edu DPRINTF(BusAddrRanges, "Adding snooping neighbour %s\n", 1344661Sksewell@umich.edu (*p)->getMasterPort().name()); 1354661Sksewell@umich.edu snoopPorts.push_back(*p); 1364661Sksewell@umich.edu } 1374661Sksewell@umich.edu } 1384661Sksewell@umich.edu} 1394661Sksewell@umich.edu 1404661Sksewell@umich.eduTick 1414661Sksewell@umich.eduBus::calcPacketTiming(PacketPtr pkt) 1424661Sksewell@umich.edu{ 1434661Sksewell@umich.edu // determine the current time rounded to the closest following 1444661Sksewell@umich.edu // clock edge 1454661Sksewell@umich.edu Tick now = curTick(); 1464661Sksewell@umich.edu if (now % clock != 0) { 1474661Sksewell@umich.edu now = ((now / clock) + 1) * clock; 1484661Sksewell@umich.edu } 1494661Sksewell@umich.edu 1504661Sksewell@umich.edu Tick headerTime = now + headerCycles * clock; 1514661Sksewell@umich.edu 1524661Sksewell@umich.edu // The packet will be sent. Figure out how long it occupies the bus, and 1534661Sksewell@umich.edu // how much of that time is for the first "word", aka bus width. 1544661Sksewell@umich.edu int numCycles = 0; 1554661Sksewell@umich.edu if (pkt->hasData()) { 1564661Sksewell@umich.edu // If a packet has data, it needs ceil(size/width) cycles to send it 1574661Sksewell@umich.edu int dataSize = pkt->getSize(); 1584661Sksewell@umich.edu numCycles += dataSize/width; 1594661Sksewell@umich.edu if (dataSize % width) 1604661Sksewell@umich.edu numCycles++; 1614661Sksewell@umich.edu } 1624661Sksewell@umich.edu 1634661Sksewell@umich.edu // The first word will be delivered after the current tick, the delivery 1644661Sksewell@umich.edu // of the address if any, and one bus cycle to deliver the data 1654661Sksewell@umich.edu pkt->firstWordTime = headerTime + clock; 1664661Sksewell@umich.edu 1674661Sksewell@umich.edu pkt->finishTime = headerTime + numCycles * clock; 1684661Sksewell@umich.edu 1694661Sksewell@umich.edu return headerTime; 1704661Sksewell@umich.edu} 1714661Sksewell@umich.edu 1724661Sksewell@umich.eduvoid Bus::occupyBus(Tick until) 1734661Sksewell@umich.edu{ 1744661Sksewell@umich.edu if (until == 0) { 1754661Sksewell@umich.edu // shortcut for express snoop packets 1764661Sksewell@umich.edu return; 1774661Sksewell@umich.edu } 1784661Sksewell@umich.edu 1794661Sksewell@umich.edu tickNextIdle = until; 1804661Sksewell@umich.edu reschedule(busIdleEvent, tickNextIdle, true); 1814661Sksewell@umich.edu 1824661Sksewell@umich.edu DPRINTF(Bus, "The bus is now occupied from tick %d to %d\n", 1834661Sksewell@umich.edu curTick(), tickNextIdle); 1844661Sksewell@umich.edu} 1854661Sksewell@umich.edu 1864661Sksewell@umich.edubool 1874661Sksewell@umich.eduBus::isOccupied(PacketPtr pkt, Port* port) 1884661Sksewell@umich.edu{ 1894661Sksewell@umich.edu // first we see if the next idle tick is in the future, next the 1904661Sksewell@umich.edu // bus is considered occupied if there are ports on the retry list 1914661Sksewell@umich.edu // and we are not in a retry with the current port 1924661Sksewell@umich.edu if (tickNextIdle > curTick() || 1934661Sksewell@umich.edu (!retryList.empty() && !(inRetry && port == retryList.front()))) { 1944661Sksewell@umich.edu addToRetryList(port); 1954661Sksewell@umich.edu return true; 1964661Sksewell@umich.edu } 1974661Sksewell@umich.edu return false; 1984661Sksewell@umich.edu} 1994661Sksewell@umich.edu 2004661Sksewell@umich.edubool 2014661Sksewell@umich.eduBus::recvTiming(PacketPtr pkt) 2024661Sksewell@umich.edu{ 2034661Sksewell@umich.edu // get the source id 2044661Sksewell@umich.edu Packet::NodeID src_id = pkt->getSrc(); 2054661Sksewell@umich.edu 2064661Sksewell@umich.edu // determine the source port based on the id and direction 2074661Sksewell@umich.edu Port *src_port = NULL; 2084661Sksewell@umich.edu if (pkt->isRequest()) 2094661Sksewell@umich.edu src_port = slavePorts[src_id]; 2104661Sksewell@umich.edu else 2114661Sksewell@umich.edu src_port = masterPorts[src_id]; 2124661Sksewell@umich.edu 2134661Sksewell@umich.edu // test if the bus should be considered occupied for the current 2144661Sksewell@umich.edu // packet, and exclude express snoops from the check 2154661Sksewell@umich.edu if (!pkt->isExpressSnoop() && isOccupied(pkt, src_port)) { 2164661Sksewell@umich.edu DPRINTF(Bus, "recvTiming: src %d dst %d %s 0x%x BUSY\n", 2174661Sksewell@umich.edu src_id, pkt->getDest(), pkt->cmdString(), pkt->getAddr()); 2184661Sksewell@umich.edu return false; 2194661Sksewell@umich.edu } 2205715Shsul@eecs.umich.edu 2214661Sksewell@umich.edu DPRINTF(Bus, "recvTiming: src %d dst %d %s 0x%x\n", 2224661Sksewell@umich.edu src_id, pkt->getDest(), pkt->cmdString(), pkt->getAddr()); 2235561Snate@binkert.org 2244661Sksewell@umich.edu Tick headerFinishTime = pkt->isExpressSnoop() ? 0 : calcPacketTiming(pkt); 2254661Sksewell@umich.edu Tick packetFinishTime = pkt->isExpressSnoop() ? 0 : pkt->finishTime; 2264661Sksewell@umich.edu 2274661Sksewell@umich.edu // decide what to do based on the direction 2284661Sksewell@umich.edu if (pkt->isRequest()) { 2294661Sksewell@umich.edu // the packet is a memory-mapped request and should be broadcasted to 2304661Sksewell@umich.edu // our snoopers 2314661Sksewell@umich.edu assert(pkt->getDest() == Packet::Broadcast); 2324661Sksewell@umich.edu 2334661Sksewell@umich.edu // forward to all snoopers but the source 2344661Sksewell@umich.edu forwardTiming(pkt, src_id); 2354661Sksewell@umich.edu 2364661Sksewell@umich.edu // remember if we add an outstanding req so we can undo it if 2374661Sksewell@umich.edu // necessary, if the packet needs a response, we should add it 2384661Sksewell@umich.edu // as outstanding and express snoops never fail so there is 2394661Sksewell@umich.edu // not need to worry about them 2404661Sksewell@umich.edu bool add_outstanding = !pkt->isExpressSnoop() && pkt->needsResponse(); 2415715Shsul@eecs.umich.edu 2424661Sksewell@umich.edu // keep track that we have an outstanding request packet 2434661Sksewell@umich.edu // matching this request, this is used by the coherency 2444661Sksewell@umich.edu // mechanism in determining what to do with snoop responses 2454661Sksewell@umich.edu // (in recvTimingSnoop) 2464661Sksewell@umich.edu if (add_outstanding) { 2474661Sksewell@umich.edu // we should never have an exsiting request outstanding 2484661Sksewell@umich.edu assert(outstandingReq.find(pkt->req) == outstandingReq.end()); 2494661Sksewell@umich.edu outstandingReq.insert(pkt->req); 2504661Sksewell@umich.edu } 2514661Sksewell@umich.edu 2524661Sksewell@umich.edu // since it is a normal request, determine the destination 2534661Sksewell@umich.edu // based on the address and attempt to send the packet 2544661Sksewell@umich.edu bool success = masterPorts[findPort(pkt->getAddr())]->sendTiming(pkt); 2554661Sksewell@umich.edu 2564661Sksewell@umich.edu if (!success) { 2574661Sksewell@umich.edu // inhibited packets should never be forced to retry 2584661Sksewell@umich.edu assert(!pkt->memInhibitAsserted()); 2594661Sksewell@umich.edu 2604661Sksewell@umich.edu // if it was added as outstanding and the send failed, then 2614661Sksewell@umich.edu // erase it again 2624661Sksewell@umich.edu if (add_outstanding) 2634661Sksewell@umich.edu outstandingReq.erase(pkt->req); 2644661Sksewell@umich.edu 2654661Sksewell@umich.edu DPRINTF(Bus, "recvTiming: src %d dst %d %s 0x%x RETRY\n", 2664661Sksewell@umich.edu src_id, pkt->getDest(), pkt->cmdString(), pkt->getAddr()); 2674661Sksewell@umich.edu 2684661Sksewell@umich.edu addToRetryList(src_port); 2694661Sksewell@umich.edu occupyBus(headerFinishTime); 2704661Sksewell@umich.edu 2714661Sksewell@umich.edu return false; 2724661Sksewell@umich.edu } 2734661Sksewell@umich.edu } else { 2744661Sksewell@umich.edu // the packet is a normal response to a request that we should 2754661Sksewell@umich.edu // have seen passing through the bus 2764661Sksewell@umich.edu assert(outstandingReq.find(pkt->req) != outstandingReq.end()); 2774661Sksewell@umich.edu 2784661Sksewell@umich.edu // remove it as outstanding 2794661Sksewell@umich.edu outstandingReq.erase(pkt->req); 2804661Sksewell@umich.edu 2814661Sksewell@umich.edu // send the packet to the destination through one of our slave 2824661Sksewell@umich.edu // ports, as determined by the destination field 2834661Sksewell@umich.edu bool success M5_VAR_USED = slavePorts[pkt->getDest()]->sendTiming(pkt); 2844661Sksewell@umich.edu 2854661Sksewell@umich.edu // currently it is illegal to block responses... can lead to 2864661Sksewell@umich.edu // deadlock 2874661Sksewell@umich.edu assert(success); 2884661Sksewell@umich.edu } 2894661Sksewell@umich.edu 2904661Sksewell@umich.edu succeededTiming(packetFinishTime); 2914661Sksewell@umich.edu 2924661Sksewell@umich.edu return true; 2934661Sksewell@umich.edu} 2944661Sksewell@umich.edu 2954661Sksewell@umich.edubool 2964661Sksewell@umich.eduBus::recvTimingSnoop(PacketPtr pkt) 2974661Sksewell@umich.edu{ 2984661Sksewell@umich.edu // get the source id 2994661Sksewell@umich.edu Packet::NodeID src_id = pkt->getSrc(); 3004661Sksewell@umich.edu 3014661Sksewell@umich.edu if (pkt->isRequest()) { 3024661Sksewell@umich.edu DPRINTF(Bus, "recvTimingSnoop: src %d dst %d %s 0x%x\n", 303 src_id, pkt->getDest(), pkt->cmdString(), pkt->getAddr()); 304 305 // the packet is an express snoop request and should be 306 // broadcasted to our snoopers 307 assert(pkt->getDest() == Packet::Broadcast); 308 assert(pkt->isExpressSnoop()); 309 310 // forward to all snoopers 311 forwardTiming(pkt, INVALID_PORT_ID); 312 313 // a snoop request came from a connected slave device (one of 314 // our master ports), and if it is not coming from the slave 315 // device responsible for the address range something is 316 // wrong, hence there is nothing further to do as the packet 317 // would be going back to where it came from 318 assert(src_id == findPort(pkt->getAddr())); 319 320 // this is an express snoop and is never forced to retry 321 assert(!inRetry); 322 323 return true; 324 } else { 325 // determine the source port based on the id 326 SlavePort* src_port = slavePorts[src_id]; 327 328 if (isOccupied(pkt, src_port)) { 329 DPRINTF(Bus, "recvTimingSnoop: src %d dst %d %s 0x%x BUSY\n", 330 src_id, pkt->getDest(), pkt->cmdString(), pkt->getAddr()); 331 return false; 332 } 333 334 DPRINTF(Bus, "recvTimingSnoop: src %d dst %d %s 0x%x\n", 335 src_id, pkt->getDest(), pkt->cmdString(), pkt->getAddr()); 336 337 // get the destination from the packet 338 Packet::NodeID dest = pkt->getDest(); 339 340 // responses are never express snoops 341 assert(!pkt->isExpressSnoop()); 342 343 calcPacketTiming(pkt); 344 Tick packetFinishTime = pkt->finishTime; 345 346 // determine if the response is from a snoop request we 347 // created as the result of a normal request (in which case it 348 // should be in the outstandingReq), or if we merely forwarded 349 // someone else's snoop request 350 if (outstandingReq.find(pkt->req) == outstandingReq.end()) { 351 // this is a snoop response to a snoop request we 352 // forwarded, e.g. coming from the L1 and going to the L2 353 // this should be forwarded as a snoop response 354 bool success M5_VAR_USED = masterPorts[dest]->sendTimingSnoop(pkt); 355 assert(success); 356 } else { 357 // we got a snoop response on one of our slave ports, 358 // i.e. from a coherent master connected to the bus, and 359 // since we created the snoop request as part of 360 // recvTiming, this should now be a normal response again 361 outstandingReq.erase(pkt->req); 362 363 // this is a snoop response from a coherent master, with a 364 // destination field set on its way through the bus as 365 // request, hence it should never go back to where the 366 // snoop response came from, but instead to where the 367 // original request came from 368 assert(src_id != dest); 369 370 // as a normal response, it should go back to a master 371 // through one of our slave ports 372 bool success M5_VAR_USED = slavePorts[dest]->sendTiming(pkt); 373 374 // currently it is illegal to block responses... can lead 375 // to deadlock 376 assert(success); 377 } 378 379 succeededTiming(packetFinishTime); 380 381 return true; 382 } 383} 384 385void 386Bus::succeededTiming(Tick busy_time) 387{ 388 // occupy the bus accordingly 389 occupyBus(busy_time); 390 391 // if a retrying port succeeded, also take it off the retry list 392 if (inRetry) { 393 DPRINTF(Bus, "Remove retry from list %s\n", 394 retryList.front()->name()); 395 retryList.pop_front(); 396 inRetry = false; 397 } 398} 399 400void 401Bus::forwardTiming(PacketPtr pkt, int exclude_slave_port_id) 402{ 403 SnoopIter s_end = snoopPorts.end(); 404 for (SnoopIter s_iter = snoopPorts.begin(); s_iter != s_end; s_iter++) { 405 BusSlavePort *p = *s_iter; 406 // we could have gotten this request from a snooping master 407 // (corresponding to our own slave port that is also in 408 // snoopPorts) and should not send it back to where it came 409 // from 410 if (exclude_slave_port_id == INVALID_PORT_ID || 411 p->getId() != exclude_slave_port_id) { 412 // cache is not allowed to refuse snoop 413 bool success M5_VAR_USED = p->sendTimingSnoop(pkt); 414 assert(success); 415 } 416 } 417} 418 419void 420Bus::releaseBus() 421{ 422 // releasing the bus means we should now be idle 423 assert(curTick() >= tickNextIdle); 424 425 // bus is now idle, so if someone is waiting we can retry 426 if (!retryList.empty()) { 427 // note that we block (return false on recvTiming) both 428 // because the bus is busy and because the destination is 429 // busy, and in the latter case the bus may be released before 430 // we see a retry from the destination 431 retryWaiting(); 432 } 433 434 //If we weren't able to drain before, we might be able to now. 435 if (drainEvent && retryList.empty() && curTick() >= tickNextIdle) { 436 drainEvent->process(); 437 // Clear the drain event once we're done with it. 438 drainEvent = NULL; 439 } 440} 441 442void 443Bus::retryWaiting() 444{ 445 // this should never be called with an empty retry list 446 assert(!retryList.empty()); 447 448 // send a retry to the port at the head of the retry list 449 inRetry = true; 450 451 // note that we might have blocked on the receiving port being 452 // busy (rather than the bus itself) and now call retry before the 453 // destination called retry on the bus 454 retryList.front()->sendRetry(); 455 456 // If inRetry is still true, sendTiming wasn't called in zero time 457 // (e.g. the cache does this) 458 if (inRetry) { 459 retryList.pop_front(); 460 inRetry = false; 461 462 //Bring tickNextIdle up to the present 463 while (tickNextIdle < curTick()) 464 tickNextIdle += clock; 465 466 //Burn a cycle for the missed grant. 467 tickNextIdle += clock; 468 469 reschedule(busIdleEvent, tickNextIdle, true); 470 } 471} 472 473void 474Bus::recvRetry(int id) 475{ 476 // we got a retry from a peer that we tried to send something to 477 // and failed, but we sent it on the account of someone else, and 478 // that source port should be on our retry list, however if the 479 // bus is released before this happens and the retry (from the bus 480 // point of view) is successful then this no longer holds and we 481 // could in fact have an empty retry list 482 if (retryList.empty()) 483 return; 484 485 // if the bus isn't busy 486 if (curTick() >= tickNextIdle) { 487 // note that we do not care who told us to retry at the moment, we 488 // merely let the first one on the retry list go 489 retryWaiting(); 490 } 491} 492 493int 494Bus::findPort(Addr addr) 495{ 496 /* An interval tree would be a better way to do this. --ali. */ 497 int dest_id; 498 499 dest_id = checkPortCache(addr); 500 if (dest_id != INVALID_PORT_ID) 501 return dest_id; 502 503 // Check normal port ranges 504 PortIter i = portMap.find(RangeSize(addr,1)); 505 if (i != portMap.end()) { 506 dest_id = i->second; 507 updatePortCache(dest_id, i->first.start, i->first.end); 508 return dest_id; 509 } 510 511 // Check if this matches the default range 512 if (useDefaultRange) { 513 AddrRangeIter a_end = defaultRange.end(); 514 for (AddrRangeIter i = defaultRange.begin(); i != a_end; i++) { 515 if (*i == addr) { 516 DPRINTF(Bus, " found addr %#llx on default\n", addr); 517 return defaultPortId; 518 } 519 } 520 } else if (defaultPortId != INVALID_PORT_ID) { 521 DPRINTF(Bus, "Unable to find destination for addr %#llx, " 522 "will use default port\n", addr); 523 return defaultPortId; 524 } 525 526 // we should use the range for the default port and it did not 527 // match, or the default port is not set 528 fatal("Unable to find destination for addr %#llx on bus %s\n", addr, 529 name()); 530} 531 532Tick 533Bus::recvAtomic(PacketPtr pkt) 534{ 535 DPRINTF(Bus, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n", 536 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString()); 537 538 // we should always see a request routed based on the address 539 assert(pkt->getDest() == Packet::Broadcast); 540 assert(pkt->isRequest()); 541 542 // forward to all snoopers but the source 543 std::pair<MemCmd, Tick> snoop_result = forwardAtomic(pkt, pkt->getSrc()); 544 MemCmd snoop_response_cmd = snoop_result.first; 545 Tick snoop_response_latency = snoop_result.second; 546 547 // even if we had a snoop response, we must continue and also 548 // perform the actual request at the destination 549 int dest_id = findPort(pkt->getAddr()); 550 551 // forward the request to the appropriate destination 552 Tick response_latency = masterPorts[dest_id]->sendAtomic(pkt); 553 554 // if we got a response from a snooper, restore it here 555 if (snoop_response_cmd != MemCmd::InvalidCmd) { 556 // no one else should have responded 557 assert(!pkt->isResponse()); 558 pkt->cmd = snoop_response_cmd; 559 response_latency = snoop_response_latency; 560 } 561 562 pkt->finishTime = curTick() + response_latency; 563 return response_latency; 564} 565 566Tick 567Bus::recvAtomicSnoop(PacketPtr pkt) 568{ 569 DPRINTF(Bus, "recvAtomicSnoop: packet src %d dest %d addr 0x%x cmd %s\n", 570 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString()); 571 572 // we should always see a request routed based on the address 573 assert(pkt->getDest() == Packet::Broadcast); 574 assert(pkt->isRequest()); 575 576 // forward to all snoopers 577 std::pair<MemCmd, Tick> snoop_result = forwardAtomic(pkt, INVALID_PORT_ID); 578 MemCmd snoop_response_cmd = snoop_result.first; 579 Tick snoop_response_latency = snoop_result.second; 580 581 if (snoop_response_cmd != MemCmd::InvalidCmd) 582 pkt->cmd = snoop_response_cmd; 583 584 pkt->finishTime = curTick() + snoop_response_latency; 585 return snoop_response_latency; 586} 587 588std::pair<MemCmd, Tick> 589Bus::forwardAtomic(PacketPtr pkt, int exclude_slave_port_id) 590{ 591 // the packet may be changed on snoops, record the original source 592 // and command to enable us to restore it between snoops so that 593 // additional snoops can take place properly 594 Packet::NodeID orig_src_id = pkt->getSrc(); 595 MemCmd orig_cmd = pkt->cmd; 596 MemCmd snoop_response_cmd = MemCmd::InvalidCmd; 597 Tick snoop_response_latency = 0; 598 599 SnoopIter s_end = snoopPorts.end(); 600 for (SnoopIter s_iter = snoopPorts.begin(); s_iter != s_end; s_iter++) { 601 BusSlavePort *p = *s_iter; 602 // we could have gotten this request from a snooping master 603 // (corresponding to our own slave port that is also in 604 // snoopPorts) and should not send it back to where it came 605 // from 606 if (exclude_slave_port_id == INVALID_PORT_ID || 607 p->getId() != exclude_slave_port_id) { 608 Tick latency = p->sendAtomicSnoop(pkt); 609 // in contrast to a functional access, we have to keep on 610 // going as all snoopers must be updated even if we get a 611 // response 612 if (pkt->isResponse()) { 613 // response from snoop agent 614 assert(pkt->cmd != orig_cmd); 615 assert(pkt->memInhibitAsserted()); 616 // should only happen once 617 assert(snoop_response_cmd == MemCmd::InvalidCmd); 618 // save response state 619 snoop_response_cmd = pkt->cmd; 620 snoop_response_latency = latency; 621 // restore original packet state for remaining snoopers 622 pkt->cmd = orig_cmd; 623 pkt->setSrc(orig_src_id); 624 pkt->setDest(Packet::Broadcast); 625 } 626 } 627 } 628 629 // the packet is restored as part of the loop and any potential 630 // snoop response is part of the returned pair 631 return std::make_pair(snoop_response_cmd, snoop_response_latency); 632} 633 634void 635Bus::recvFunctional(PacketPtr pkt) 636{ 637 if (!pkt->isPrint()) { 638 // don't do DPRINTFs on PrintReq as it clutters up the output 639 DPRINTF(Bus, 640 "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n", 641 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), 642 pkt->cmdString()); 643 } 644 645 // we should always see a request routed based on the address 646 assert(pkt->getDest() == Packet::Broadcast); 647 assert(pkt->isRequest()); 648 649 // forward to all snoopers but the source 650 forwardFunctional(pkt, pkt->getSrc()); 651 652 // there is no need to continue if the snooping has found what we 653 // were looking for and the packet is already a response 654 if (!pkt->isResponse()) { 655 int dest_id = findPort(pkt->getAddr()); 656 657 masterPorts[dest_id]->sendFunctional(pkt); 658 } 659} 660 661void 662Bus::recvFunctionalSnoop(PacketPtr pkt) 663{ 664 if (!pkt->isPrint()) { 665 // don't do DPRINTFs on PrintReq as it clutters up the output 666 DPRINTF(Bus, 667 "recvFunctionalSnoop: packet src %d dest %d addr 0x%x cmd %s\n", 668 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), 669 pkt->cmdString()); 670 } 671 672 // we should always see a request routed based on the address 673 assert(pkt->getDest() == Packet::Broadcast); 674 assert(pkt->isRequest()); 675 676 // forward to all snoopers 677 forwardFunctional(pkt, INVALID_PORT_ID); 678} 679 680void 681Bus::forwardFunctional(PacketPtr pkt, int exclude_slave_port_id) 682{ 683 SnoopIter s_end = snoopPorts.end(); 684 for (SnoopIter s_iter = snoopPorts.begin(); s_iter != s_end; s_iter++) { 685 BusSlavePort *p = *s_iter; 686 // we could have gotten this request from a snooping master 687 // (corresponding to our own slave port that is also in 688 // snoopPorts) and should not send it back to where it came 689 // from 690 if (exclude_slave_port_id == INVALID_PORT_ID || 691 p->getId() != exclude_slave_port_id) 692 p->sendFunctionalSnoop(pkt); 693 694 // if we get a response we are done 695 if (pkt->isResponse()) { 696 break; 697 } 698 } 699} 700 701/** Function called by the port when the bus is receiving a range change.*/ 702void 703Bus::recvRangeChange(int id) 704{ 705 AddrRangeList ranges; 706 AddrRangeIter iter; 707 708 if (inRecvRangeChange.count(id)) 709 return; 710 inRecvRangeChange.insert(id); 711 712 DPRINTF(BusAddrRanges, "received RangeChange from device id %d\n", id); 713 714 clearPortCache(); 715 if (id == defaultPortId) { 716 defaultRange.clear(); 717 // Only try to update these ranges if the user set a default responder. 718 if (useDefaultRange) { 719 AddrRangeList ranges = 720 masterPorts[id]->getSlavePort().getAddrRanges(); 721 for(iter = ranges.begin(); iter != ranges.end(); iter++) { 722 defaultRange.push_back(*iter); 723 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for default range\n", 724 iter->start, iter->end); 725 } 726 } 727 } else { 728 729 assert(id < masterPorts.size() && id >= 0); 730 BusMasterPort *port = masterPorts[id]; 731 732 // Clean out any previously existent ids 733 for (PortIter portIter = portMap.begin(); 734 portIter != portMap.end(); ) { 735 if (portIter->second == id) 736 portMap.erase(portIter++); 737 else 738 portIter++; 739 } 740 741 ranges = port->getSlavePort().getAddrRanges(); 742 743 for (iter = ranges.begin(); iter != ranges.end(); iter++) { 744 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for id %d\n", 745 iter->start, iter->end, id); 746 if (portMap.insert(*iter, id) == portMap.end()) { 747 int conflict_id = portMap.find(*iter)->second; 748 fatal("%s has two ports with same range:\n\t%s\n\t%s\n", 749 name(), masterPorts[id]->getSlavePort().name(), 750 masterPorts[conflict_id]->getSlavePort().name()); 751 } 752 } 753 } 754 DPRINTF(BusAddrRanges, "port list has %d entries\n", portMap.size()); 755 756 // tell all our peers that our address range has changed. 757 // Don't tell the device that caused this change, it already knows 758 std::vector<BusSlavePort*>::const_iterator intIter; 759 760 for (intIter = slavePorts.begin(); intIter != slavePorts.end(); intIter++) 761 (*intIter)->sendRangeChange(); 762 763 inRecvRangeChange.erase(id); 764} 765 766AddrRangeList 767Bus::getAddrRanges(int id) 768{ 769 AddrRangeList ranges; 770 771 DPRINTF(BusAddrRanges, "received address range request, returning:\n"); 772 773 for (AddrRangeIter dflt_iter = defaultRange.begin(); 774 dflt_iter != defaultRange.end(); dflt_iter++) { 775 ranges.push_back(*dflt_iter); 776 DPRINTF(BusAddrRanges, " -- Dflt: %#llx : %#llx\n",dflt_iter->start, 777 dflt_iter->end); 778 } 779 for (PortIter portIter = portMap.begin(); 780 portIter != portMap.end(); portIter++) { 781 bool subset = false; 782 for (AddrRangeIter dflt_iter = defaultRange.begin(); 783 dflt_iter != defaultRange.end(); dflt_iter++) { 784 if ((portIter->first.start < dflt_iter->start && 785 portIter->first.end >= dflt_iter->start) || 786 (portIter->first.start < dflt_iter->end && 787 portIter->first.end >= dflt_iter->end)) 788 fatal("Devices can not set ranges that itersect the default set\ 789 but are not a subset of the default set.\n"); 790 if (portIter->first.start >= dflt_iter->start && 791 portIter->first.end <= dflt_iter->end) { 792 subset = true; 793 DPRINTF(BusAddrRanges, " -- %#llx : %#llx is a SUBSET\n", 794 portIter->first.start, portIter->first.end); 795 } 796 } 797 if (portIter->second != id && !subset) { 798 ranges.push_back(portIter->first); 799 DPRINTF(BusAddrRanges, " -- %#llx : %#llx\n", 800 portIter->first.start, portIter->first.end); 801 } 802 } 803 804 return ranges; 805} 806 807bool 808Bus::isSnooping(int id) const 809{ 810 // in essence, answer the question if there are snooping ports 811 return !snoopPorts.empty(); 812} 813 814unsigned 815Bus::findBlockSize(int id) 816{ 817 if (cachedBlockSizeValid) 818 return cachedBlockSize; 819 820 unsigned max_bs = 0; 821 822 PortIter p_end = portMap.end(); 823 for (PortIter p_iter = portMap.begin(); p_iter != p_end; p_iter++) { 824 unsigned tmp_bs = masterPorts[p_iter->second]->peerBlockSize(); 825 if (tmp_bs > max_bs) 826 max_bs = tmp_bs; 827 } 828 SnoopIter s_end = snoopPorts.end(); 829 for (SnoopIter s_iter = snoopPorts.begin(); s_iter != s_end; s_iter++) { 830 unsigned tmp_bs = (*s_iter)->peerBlockSize(); 831 if (tmp_bs > max_bs) 832 max_bs = tmp_bs; 833 } 834 if (max_bs == 0) 835 max_bs = defaultBlockSize; 836 837 if (max_bs != 64) 838 warn_once("Blocksize found to not be 64... hmm... probably not.\n"); 839 cachedBlockSize = max_bs; 840 cachedBlockSizeValid = true; 841 return max_bs; 842} 843 844 845unsigned int 846Bus::drain(Event * de) 847{ 848 //We should check that we're not "doing" anything, and that noone is 849 //waiting. We might be idle but have someone waiting if the device we 850 //contacted for a retry didn't actually retry. 851 if (!retryList.empty() || (curTick() < tickNextIdle && 852 busIdleEvent.scheduled())) { 853 drainEvent = de; 854 return 1; 855 } 856 return 0; 857} 858 859void 860Bus::startup() 861{ 862 if (tickNextIdle < curTick()) 863 tickNextIdle = (curTick() / clock) * clock + clock; 864} 865 866Bus * 867BusParams::create() 868{ 869 return new Bus(this); 870} 871