xbar.cc revision 3218
1/* 2 * Copyright (c) 2006 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Ali Saidi 29 */ 30 31/** 32 * @file 33 * Definition of a bus object. 34 */ 35 36 37#include "base/misc.hh" 38#include "base/trace.hh" 39#include "mem/bus.hh" 40#include "sim/builder.hh" 41 42Port * 43Bus::getPort(const std::string &if_name, int idx) 44{ 45 if (if_name == "default") 46 if (defaultPort == NULL) { 47 defaultPort = new BusPort(csprintf("%s-default",name()), this, 48 defaultId); 49 return defaultPort; 50 } else 51 fatal("Default port already set\n"); 52 53 // if_name ignored? forced to be empty? 54 int id = interfaces.size(); 55 BusPort *bp = new BusPort(csprintf("%s-p%d", name(), id), this, id); 56 interfaces.push_back(bp); 57 return bp; 58} 59 60/** Get the ranges of anyone other buses that we are connected to. */ 61void 62Bus::init() 63{ 64 std::vector<Port*>::iterator intIter; 65 66 for (intIter = interfaces.begin(); intIter != interfaces.end(); intIter++) 67 (*intIter)->sendStatusChange(Port::RangeChange); 68} 69 70Bus::BusFreeEvent::BusFreeEvent(Bus *_bus) : Event(&mainEventQueue), bus(_bus) 71{ 72 assert(!scheduled()); 73} 74 75void Bus::BusFreeEvent::process() 76{ 77 bus->recvRetry(0); 78} 79 80const char * Bus::BusFreeEvent::description() 81{ 82 return "bus became available"; 83} 84 85/** Function called by the port when the bus is receiving a Timing 86 * transaction.*/ 87bool 88Bus::recvTiming(Packet *pkt) 89{ 90 Port *port; 91 DPRINTF(Bus, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n", 92 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString()); 93 94 Port *pktPort = interfaces[pkt->getSrc()]; 95 96 // If the bus is busy, or other devices are in line ahead of the current 97 // one, put this device on the retry list. 98 if (tickNextIdle > curTick || 99 (retryList.size() && pktPort != retryingPort)) { 100 addToRetryList(pktPort); 101 return false; 102 } 103 104 short dest = pkt->getDest(); 105 if (dest == Packet::Broadcast) { 106 if (timingSnoop(pkt)) { 107 pkt->flags |= SNOOP_COMMIT; 108 bool success = timingSnoop(pkt); 109 assert(success); 110 if (pkt->flags & SATISFIED) { 111 //Cache-Cache transfer occuring 112 if (retryingPort) { 113 retryList.pop_front(); 114 retryingPort = NULL; 115 } 116 return true; 117 } 118 port = findPort(pkt->getAddr(), pkt->getSrc()); 119 } else { 120 //Snoop didn't succeed 121 addToRetryList(pktPort); 122 return false; 123 } 124 } else { 125 assert(dest >= 0 && dest < interfaces.size()); 126 assert(dest != pkt->getSrc()); // catch infinite loops 127 port = interfaces[dest]; 128 } 129 130 //Bring tickNextIdle up to the present tick 131 //There is some potential ambiguity where a cycle starts, which might make 132 //a difference when devices are acting right around a cycle boundary. Using 133 //a < allows things which happen exactly on a cycle boundary to take up only 134 //the following cycle. Anthing that happens later will have to "wait" for 135 //the end of that cycle, and then start using the bus after that. 136 while (tickNextIdle < curTick) 137 tickNextIdle += clock; 138 139 // The packet will be sent. Figure out how long it occupies the bus, and 140 // how much of that time is for the first "word", aka bus width. 141 int numCycles = 0; 142 // Requests need one cycle to send an address 143 if (pkt->isRequest()) 144 numCycles++; 145 else if (pkt->isResponse() || pkt->hasData()) { 146 // If a packet has data, it needs ceil(size/width) cycles to send it 147 // We're using the "adding instead of dividing" trick again here 148 if (pkt->hasData()) { 149 int dataSize = pkt->getSize(); 150 for (int transmitted = 0; transmitted < dataSize; 151 transmitted += width) { 152 numCycles++; 153 } 154 } else { 155 // If the packet didn't have data, it must have been a response. 156 // Those use the bus for one cycle to send their data. 157 numCycles++; 158 } 159 } 160 161 // The first word will be delivered after the current tick, the delivery 162 // of the address if any, and one bus cycle to deliver the data 163 pkt->firstWordTime = 164 tickNextIdle + 165 pkt->isRequest() ? clock : 0 + 166 clock; 167 168 //Advance it numCycles bus cycles. 169 //XXX Should this use the repeated addition trick as well? 170 tickNextIdle += (numCycles * clock); 171 if (!busIdle.scheduled()) { 172 busIdle.schedule(tickNextIdle); 173 } else { 174 busIdle.reschedule(tickNextIdle); 175 } 176 DPRINTF(Bus, "The bus is now occupied from tick %d to %d\n", 177 curTick, tickNextIdle); 178 179 // The bus will become idle once the current packet is delivered. 180 pkt->finishTime = tickNextIdle; 181 182 if (port->sendTiming(pkt)) { 183 // Packet was successfully sent. Return true. 184 // Also take care of retries 185 if (retryingPort) { 186 retryList.pop_front(); 187 retryingPort = NULL; 188 } 189 return true; 190 } 191 192 // Packet not successfully sent. Leave or put it on the retry list. 193 addToRetryList(pktPort); 194 return false; 195} 196 197void 198Bus::recvRetry(int id) 199{ 200 // If there's anything waiting... 201 if (retryList.size()) { 202 retryingPort = retryList.front(); 203 retryingPort->sendRetry(); 204 // If the retryingPort pointer isn't null, sendTiming wasn't called 205 if (retryingPort) { 206 warn("sendRetry didn't call sendTiming\n"); 207 retryList.pop_front(); 208 retryingPort = NULL; 209 } 210 } 211} 212 213Port * 214Bus::findPort(Addr addr, int id) 215{ 216 /* An interval tree would be a better way to do this. --ali. */ 217 int dest_id = -1; 218 int i = 0; 219 bool found = false; 220 AddrRangeIter iter; 221 222 while (i < portList.size() && !found) 223 { 224 if (portList[i].range == addr) { 225 dest_id = portList[i].portId; 226 found = true; 227 DPRINTF(Bus, " found addr %#llx on device %d\n", addr, dest_id); 228 } 229 i++; 230 } 231 232 // Check if this matches the default range 233 if (dest_id == -1) { 234 for (iter = defaultRange.begin(); iter != defaultRange.end(); iter++) { 235 if (*iter == addr) { 236 DPRINTF(Bus, " found addr %#llx on default\n", addr); 237 return defaultPort; 238 } 239 } 240 panic("Unable to find destination for addr: %#llx", addr); 241 } 242 243 244 // we shouldn't be sending this back to where it came from 245 assert(dest_id != id); 246 247 return interfaces[dest_id]; 248} 249 250std::vector<int> 251Bus::findSnoopPorts(Addr addr, int id) 252{ 253 int i = 0; 254 AddrRangeIter iter; 255 std::vector<int> ports; 256 257 while (i < portSnoopList.size()) 258 { 259 if (portSnoopList[i].range == addr && portSnoopList[i].portId != id) { 260 //Careful to not overlap ranges 261 //or snoop will be called more than once on the port 262 ports.push_back(portSnoopList[i].portId); 263 DPRINTF(Bus, " found snoop addr %#llx on device%d\n", addr, 264 portSnoopList[i].portId); 265 } 266 i++; 267 } 268 return ports; 269} 270 271void 272Bus::atomicSnoop(Packet *pkt) 273{ 274 std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc()); 275 276 while (!ports.empty()) 277 { 278 interfaces[ports.back()]->sendAtomic(pkt); 279 ports.pop_back(); 280 } 281} 282 283void 284Bus::functionalSnoop(Packet *pkt) 285{ 286 std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc()); 287 288 while (!ports.empty()) 289 { 290 interfaces[ports.back()]->sendFunctional(pkt); 291 ports.pop_back(); 292 } 293} 294 295bool 296Bus::timingSnoop(Packet *pkt) 297{ 298 std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc()); 299 bool success = true; 300 301 while (!ports.empty() && success) 302 { 303 success = interfaces[ports.back()]->sendTiming(pkt); 304 ports.pop_back(); 305 } 306 307 return success; 308} 309 310 311/** Function called by the port when the bus is receiving a Atomic 312 * transaction.*/ 313Tick 314Bus::recvAtomic(Packet *pkt) 315{ 316 DPRINTF(Bus, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n", 317 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString()); 318 assert(pkt->getDest() == Packet::Broadcast); 319 atomicSnoop(pkt); 320 return findPort(pkt->getAddr(), pkt->getSrc())->sendAtomic(pkt); 321} 322 323/** Function called by the port when the bus is receiving a Functional 324 * transaction.*/ 325void 326Bus::recvFunctional(Packet *pkt) 327{ 328 DPRINTF(Bus, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n", 329 pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString()); 330 assert(pkt->getDest() == Packet::Broadcast); 331 functionalSnoop(pkt); 332 findPort(pkt->getAddr(), pkt->getSrc())->sendFunctional(pkt); 333} 334 335/** Function called by the port when the bus is receiving a status change.*/ 336void 337Bus::recvStatusChange(Port::Status status, int id) 338{ 339 AddrRangeList ranges; 340 AddrRangeList snoops; 341 int x; 342 AddrRangeIter iter; 343 344 assert(status == Port::RangeChange && 345 "The other statuses need to be implemented."); 346 347 DPRINTF(BusAddrRanges, "received RangeChange from device id %d\n", id); 348 349 if (id == defaultId) { 350 defaultRange.clear(); 351 defaultPort->getPeerAddressRanges(ranges, snoops); 352 assert(snoops.size() == 0); 353 for(iter = ranges.begin(); iter != ranges.end(); iter++) { 354 defaultRange.push_back(*iter); 355 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for default range\n", 356 iter->start, iter->end); 357 } 358 } else { 359 360 assert((id < interfaces.size() && id >= 0) || id == -1); 361 Port *port = interfaces[id]; 362 std::vector<DevMap>::iterator portIter; 363 std::vector<DevMap>::iterator snoopIter; 364 365 // Clean out any previously existent ids 366 for (portIter = portList.begin(); portIter != portList.end(); ) { 367 if (portIter->portId == id) 368 portIter = portList.erase(portIter); 369 else 370 portIter++; 371 } 372 373 for (snoopIter = portSnoopList.begin(); snoopIter != portSnoopList.end(); ) { 374 if (snoopIter->portId == id) 375 snoopIter = portSnoopList.erase(snoopIter); 376 else 377 snoopIter++; 378 } 379 380 port->getPeerAddressRanges(ranges, snoops); 381 382 for(iter = snoops.begin(); iter != snoops.end(); iter++) { 383 DevMap dm; 384 dm.portId = id; 385 dm.range = *iter; 386 387 DPRINTF(BusAddrRanges, "Adding snoop range %#llx - %#llx for id %d\n", 388 dm.range.start, dm.range.end, id); 389 portSnoopList.push_back(dm); 390 } 391 392 for(iter = ranges.begin(); iter != ranges.end(); iter++) { 393 DevMap dm; 394 dm.portId = id; 395 dm.range = *iter; 396 397 DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for id %d\n", 398 dm.range.start, dm.range.end, id); 399 portList.push_back(dm); 400 } 401 } 402 DPRINTF(MMU, "port list has %d entries\n", portList.size()); 403 404 // tell all our peers that our address range has changed. 405 // Don't tell the device that caused this change, it already knows 406 for (x = 0; x < interfaces.size(); x++) 407 if (x != id) 408 interfaces[x]->sendStatusChange(Port::RangeChange); 409 410 if (id != defaultId && defaultPort) 411 defaultPort->sendStatusChange(Port::RangeChange); 412} 413 414void 415Bus::addressRanges(AddrRangeList &resp, AddrRangeList &snoop, int id) 416{ 417 std::vector<DevMap>::iterator portIter; 418 AddrRangeIter dflt_iter; 419 bool subset; 420 421 resp.clear(); 422 snoop.clear(); 423 424 DPRINTF(BusAddrRanges, "received address range request, returning:\n"); 425 426 for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end(); 427 dflt_iter++) { 428 resp.push_back(*dflt_iter); 429 DPRINTF(BusAddrRanges, " -- %#llx : %#llx\n",dflt_iter->start, 430 dflt_iter->end); 431 } 432 for (portIter = portList.begin(); portIter != portList.end(); portIter++) { 433 subset = false; 434 for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end(); 435 dflt_iter++) { 436 if ((portIter->range.start < dflt_iter->start && 437 portIter->range.end >= dflt_iter->start) || 438 (portIter->range.start < dflt_iter->end && 439 portIter->range.end >= dflt_iter->end)) 440 fatal("Devices can not set ranges that itersect the default set\ 441 but are not a subset of the default set.\n"); 442 if (portIter->range.start >= dflt_iter->start && 443 portIter->range.end <= dflt_iter->end) { 444 subset = true; 445 DPRINTF(BusAddrRanges, " -- %#llx : %#llx is a SUBSET\n", 446 portIter->range.start, portIter->range.end); 447 } 448 } 449 if (portIter->portId != id && !subset) { 450 resp.push_back(portIter->range); 451 DPRINTF(BusAddrRanges, " -- %#llx : %#llx\n", 452 portIter->range.start, portIter->range.end); 453 } 454 } 455} 456 457BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus) 458 459 Param<int> bus_id; 460 Param<int> clock; 461 Param<int> width; 462 463END_DECLARE_SIM_OBJECT_PARAMS(Bus) 464 465BEGIN_INIT_SIM_OBJECT_PARAMS(Bus) 466 INIT_PARAM(bus_id, "a globally unique bus id"), 467 INIT_PARAM(clock, "bus clock speed"), 468 INIT_PARAM(width, "width of the bus (bits)") 469END_INIT_SIM_OBJECT_PARAMS(Bus) 470 471CREATE_SIM_OBJECT(Bus) 472{ 473 return new Bus(getInstanceName(), bus_id, clock, width); 474} 475 476REGISTER_SIM_OBJECT("Bus", Bus) 477