xbar.hh revision 9714
16157Snate@binkert.org/* 26157Snate@binkert.org * Copyright (c) 2011-2013 ARM Limited 36157Snate@binkert.org * All rights reserved 46157Snate@binkert.org * 56157Snate@binkert.org * The license below extends only to copyright in the software and shall 66157Snate@binkert.org * not be construed as granting a license to any other intellectual 76157Snate@binkert.org * property including but not limited to intellectual property relating 86157Snate@binkert.org * to a hardware implementation of the functionality of the software 96157Snate@binkert.org * licensed hereunder. You may use the software subject to the license 106157Snate@binkert.org * terms below provided that you ensure that this notice is replicated 116157Snate@binkert.org * unmodified and in its entirety in all distributions of the software, 126157Snate@binkert.org * modified or unmodified, in source code or in binary form. 136157Snate@binkert.org * 146157Snate@binkert.org * Copyright (c) 2002-2005 The Regents of The University of Michigan 156157Snate@binkert.org * All rights reserved. 166157Snate@binkert.org * 176157Snate@binkert.org * Redistribution and use in source and binary forms, with or without 186157Snate@binkert.org * modification, are permitted provided that the following conditions are 196157Snate@binkert.org * met: redistributions of source code must retain the above copyright 206157Snate@binkert.org * notice, this list of conditions and the following disclaimer; 216157Snate@binkert.org * redistributions in binary form must reproduce the above copyright 226157Snate@binkert.org * notice, this list of conditions and the following disclaimer in the 236157Snate@binkert.org * documentation and/or other materials provided with the distribution; 246157Snate@binkert.org * neither the name of the copyright holders nor the names of its 256157Snate@binkert.org * contributors may be used to endorse or promote products derived from 266157Snate@binkert.org * this software without specific prior written permission. 276157Snate@binkert.org * 286157Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 296157Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 306157Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 316157Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 326157Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 336157Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 346157Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 356157Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 366157Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 376157Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 386157Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 396157Snate@binkert.org * 4010133Sandreas.hansson@arm.com * Authors: Ron Dreslinski 4110133Sandreas.hansson@arm.com * Ali Saidi 4210133Sandreas.hansson@arm.com * Andreas Hansson 4310133Sandreas.hansson@arm.com * William Wang 4410133Sandreas.hansson@arm.com */ 4510133Sandreas.hansson@arm.com 4610133Sandreas.hansson@arm.com/** 4710133Sandreas.hansson@arm.com * @file 4810133Sandreas.hansson@arm.com * Declaration of an abstract bus base class. 4910133Sandreas.hansson@arm.com */ 5010133Sandreas.hansson@arm.com 5110133Sandreas.hansson@arm.com#ifndef __MEM_BUS_HH__ 5210133Sandreas.hansson@arm.com#define __MEM_BUS_HH__ 5310133Sandreas.hansson@arm.com 5410133Sandreas.hansson@arm.com#include <deque> 5510133Sandreas.hansson@arm.com 5610133Sandreas.hansson@arm.com#include "base/addr_range_map.hh" 5710133Sandreas.hansson@arm.com#include "base/types.hh" 5810133Sandreas.hansson@arm.com#include "mem/mem_object.hh" 5910133Sandreas.hansson@arm.com#include "params/BaseBus.hh" 6010133Sandreas.hansson@arm.com#include "sim/stats.hh" 6110133Sandreas.hansson@arm.com 628492Snilay@cs.wisc.edu/** 636168Snate@binkert.org * The base bus contains the common elements of the non-coherent and 646168Snate@binkert.org * coherent bus. It is an abstract class that does not have any of the 656157Snate@binkert.org * functionality relating to the actual reception and transmission of 666157Snate@binkert.org * packets, as this is left for the subclasses. 676157Snate@binkert.org * 686157Snate@binkert.org * The BaseBus is responsible for the basic flow control (busy or 696157Snate@binkert.org * not), the administration of retries, and the address decoding. 706157Snate@binkert.org */ 716157Snate@binkert.orgclass BaseBus : public MemObject 726157Snate@binkert.org{ 736157Snate@binkert.org 746157Snate@binkert.org protected: 756157Snate@binkert.org 766157Snate@binkert.org /** 776157Snate@binkert.org * A bus layer is an internal bus structure with its own flow 786157Snate@binkert.org * control and arbitration. Hence, a single-layer bus mimics a 796157Snate@binkert.org * traditional off-chip tri-state bus (like PCI), where only one 806157Snate@binkert.org * set of wires are shared. For on-chip buses, a good starting 816157Snate@binkert.org * point is to have three layers, for requests, responses, and 826157Snate@binkert.org * snoop responses respectively (snoop requests are instantaneous 836157Snate@binkert.org * and do not need any flow control or arbitration). This case is 846157Snate@binkert.org * similar to AHB and some OCP configurations. 856157Snate@binkert.org * 866157Snate@binkert.org * As a further extensions beyond the three-layer bus, a future 876157Snate@binkert.org * multi-layer bus has with one layer per connected slave port 886157Snate@binkert.org * provides a full or partial crossbar, like AXI, OCP, PCIe etc. 896157Snate@binkert.org * 906157Snate@binkert.org * The template parameter, PortClass, indicates the destination 916157Snate@binkert.org * port type for the bus. The retry list holds either master ports 926157Snate@binkert.org * or slave ports, depending on the direction of the layer. Thus, 936157Snate@binkert.org * a request layer has a retry list containing slave ports, 946157Snate@binkert.org * whereas a response layer holds master ports. 956157Snate@binkert.org */ 966157Snate@binkert.org template <typename PortClass> 976157Snate@binkert.org class Layer : public Drainable 986157Snate@binkert.org { 996157Snate@binkert.org 1006157Snate@binkert.org public: 1016157Snate@binkert.org 1026157Snate@binkert.org /** 1036157Snate@binkert.org * Create a bus layer and give it a name. The bus layer uses 1046157Snate@binkert.org * the bus an event manager. 1056157Snate@binkert.org * 1066157Snate@binkert.org * @param _bus the bus this layer belongs to 1076157Snate@binkert.org * @param _name the layer's name 1088483Sgblack@eecs.umich.edu * @param num_dest_ports number of destination ports 1098483Sgblack@eecs.umich.edu */ 1106157Snate@binkert.org Layer(BaseBus& _bus, const std::string& _name, uint16_t num_dest_ports); 1116882SBrad.Beckmann@amd.com 1126286Snate@binkert.org /** 1136286Snate@binkert.org * Drain according to the normal semantics, so that the bus 1148092Snilay@cs.wisc.edu * can tell the layer to drain, and pass an event to signal 1156286Snate@binkert.org * back when drained. 1166286Snate@binkert.org * 1176157Snate@binkert.org * @param de drain event to call once drained 11811208Sjoseph.gross@amd.com * 1196157Snate@binkert.org * @return 1 if busy or waiting to retry, or 0 if idle 12011210SBrad.Beckmann@amd.com */ 12110301Snilay@cs.wisc.edu unsigned int drain(DrainManager *dm); 1226157Snate@binkert.org 1236157Snate@binkert.org /** 12411122Snilay@cs.wisc.edu * Get the bus layer's name 12510301Snilay@cs.wisc.edu */ 1269363Snilay@cs.wisc.edu const std::string name() const { return bus.name() + _name; } 12710301Snilay@cs.wisc.edu 1286286Snate@binkert.org 12910301Snilay@cs.wisc.edu /** 13010301Snilay@cs.wisc.edu * Determine if the bus layer accepts a packet from a specific 13110301Snilay@cs.wisc.edu * port. If not, the port in question is also added to the 13210301Snilay@cs.wisc.edu * retry list. In either case the state of the layer is 1336157Snate@binkert.org * updated accordingly. 13410301Snilay@cs.wisc.edu * 13510301Snilay@cs.wisc.edu * @param port Source port presenting the packet 136 * @param dest_port_id Destination port id 137 * 138 * @return True if the bus layer accepts the packet 139 */ 140 bool tryTiming(PortClass* port, PortID dest_port_id); 141 142 /** 143 * Deal with a destination port accepting a packet by potentially 144 * removing the source port from the retry list (if retrying) and 145 * occupying the bus layer accordingly. 146 * 147 * @param busy_time Time to spend as a result of a successful send 148 */ 149 void succeededTiming(Tick busy_time); 150 151 /** 152 * Deal with a destination port not accepting a packet by 153 * potentially adding the source port to the retry list (if 154 * not already at the front) and occupying the bus layer 155 * accordingly. 156 * 157 * @param src_port Source port 158 * @param dest_port_id Destination port id 159 * @param busy_time Time to spend as a result of a failed send 160 */ 161 void failedTiming(PortClass* src_port, PortID dest_port_id, 162 Tick busy_time); 163 164 /** Occupy the bus layer until until */ 165 void occupyLayer(Tick until); 166 167 /** 168 * Send a retry to the port at the head of waitingForLayer. The 169 * caller must ensure that the list is not empty. 170 */ 171 void retryWaiting(); 172 173 /** 174 * Handle a retry from a neighbouring module. This wraps 175 * retryWaiting by verifying that there are ports waiting 176 * before calling retryWaiting. 177 * 178 * @param port_id Id of the port that received the retry 179 */ 180 void recvRetry(PortID port_id); 181 182 /** 183 * Register stats for the layer 184 */ 185 void regStats(); 186 187 private: 188 189 /** The bus this layer is a part of. */ 190 BaseBus& bus; 191 192 /** A name for this layer. */ 193 std::string _name; 194 195 /** 196 * We declare an enum to track the state of the bus layer. The 197 * starting point is an idle state where the bus layer is 198 * waiting for a packet to arrive. Upon arrival, the bus layer 199 * transitions to the busy state, where it remains either 200 * until the packet transfer is done, or the header time is 201 * spent. Once the bus layer leaves the busy state, it can 202 * either go back to idle, if no packets have arrived while it 203 * was busy, or the bus layer goes on to retry the first port 204 * in waitingForLayer. A similar transition takes place from 205 * idle to retry if the bus layer receives a retry from one of 206 * its connected ports. The retry state lasts until the port 207 * in questions calls sendTiming and returns control to the 208 * bus layer, or goes to a busy state if the port does not 209 * immediately react to the retry by calling sendTiming. 210 */ 211 enum State { IDLE, BUSY, RETRY }; 212 213 /** track the state of the bus layer */ 214 State state; 215 216 /** manager to signal when drained */ 217 DrainManager *drainManager; 218 219 /** 220 * A deque of ports that retry should be called on because 221 * the original send was delayed due to a busy layer. 222 */ 223 std::deque<PortClass*> waitingForLayer; 224 225 /** 226 * Port that we are currently in the process of telling to 227 * retry a previously failed attempt to perform a timing 228 * transaction. This is a valid port when in the retry state, 229 * and NULL when in busy or idle. 230 */ 231 PortClass* retryingPort; 232 233 /** 234 * A vector that tracks who is waiting for the retry when 235 * receiving it from a peer. The vector indices are port ids 236 * of the outgoing ports for the specific layer. The values 237 * are the incoming ports that tried to forward something to 238 * the outgoing port, but was told to wait and is now waiting 239 * for a retry. If no port is waiting NULL is stored on the 240 * location in question. 241 */ 242 std::vector<PortClass*> waitingForPeer; 243 244 /** 245 * Release the bus layer after being occupied and return to an 246 * idle state where we proceed to send a retry to any 247 * potential waiting port, or drain if asked to do so. 248 */ 249 void releaseLayer(); 250 251 /** event used to schedule a release of the layer */ 252 EventWrapper<Layer, &Layer::releaseLayer> releaseEvent; 253 254 /** 255 * Stats for occupancy and utilization. These stats capture 256 * the time the bus spends in the busy state and are thus only 257 * relevant when the memory system is in timing mode. 258 */ 259 Stats::Scalar occupancy; 260 Stats::Formula utilization; 261 262 }; 263 264 /** cycles of overhead per transaction */ 265 const Cycles headerCycles; 266 /** the width of the bus in bytes */ 267 const uint32_t width; 268 269 typedef AddrRangeMap<PortID>::iterator PortMapIter; 270 typedef AddrRangeMap<PortID>::const_iterator PortMapConstIter; 271 AddrRangeMap<PortID> portMap; 272 273 /** all contigous ranges seen by this bus */ 274 AddrRangeList busRanges; 275 276 AddrRange defaultRange; 277 278 /** 279 * Function called by the port when the bus is recieving a range change. 280 * 281 * @param master_port_id id of the port that received the change 282 */ 283 void recvRangeChange(PortID master_port_id); 284 285 /** Find which port connected to this bus (if any) should be given a packet 286 * with this address. 287 * @param addr Address to find port for. 288 * @return id of port that the packet should be sent out of. 289 */ 290 PortID findPort(Addr addr); 291 292 // Cache for the findPort function storing recently used ports from portMap 293 struct PortCache { 294 bool valid; 295 PortID id; 296 AddrRange range; 297 }; 298 299 PortCache portCache[3]; 300 301 // Checks the cache and returns the id of the port that has the requested 302 // address within its range 303 inline PortID checkPortCache(Addr addr) const { 304 if (portCache[0].valid && portCache[0].range.contains(addr)) { 305 return portCache[0].id; 306 } 307 if (portCache[1].valid && portCache[1].range.contains(addr)) { 308 return portCache[1].id; 309 } 310 if (portCache[2].valid && portCache[2].range.contains(addr)) { 311 return portCache[2].id; 312 } 313 314 return InvalidPortID; 315 } 316 317 // Clears the earliest entry of the cache and inserts a new port entry 318 inline void updatePortCache(short id, const AddrRange& range) { 319 portCache[2].valid = portCache[1].valid; 320 portCache[2].id = portCache[1].id; 321 portCache[2].range = portCache[1].range; 322 323 portCache[1].valid = portCache[0].valid; 324 portCache[1].id = portCache[0].id; 325 portCache[1].range = portCache[0].range; 326 327 portCache[0].valid = true; 328 portCache[0].id = id; 329 portCache[0].range = range; 330 } 331 332 // Clears the cache. Needs to be called in constructor. 333 inline void clearPortCache() { 334 portCache[2].valid = false; 335 portCache[1].valid = false; 336 portCache[0].valid = false; 337 } 338 339 /** 340 * Return the address ranges the bus is responsible for. 341 * 342 * @return a list of non-overlapping address ranges 343 */ 344 AddrRangeList getAddrRanges() const; 345 346 /** 347 * Calculate the timing parameters for the packet. Updates the 348 * busFirstWordDelay and busLastWordDelay fields of the packet 349 * object with the relative number of ticks required to transmit 350 * the header and the first word, and the last word, respectively. 351 */ 352 void calcPacketTiming(PacketPtr pkt); 353 354 /** 355 * Ask everyone on the bus what their size is and determine the 356 * bus size as either the maximum, or if no device specifies a 357 * block size return the default. 358 * 359 * @return the max of all the sizes or the default if none is set 360 */ 361 unsigned deviceBlockSize() const; 362 363 /** 364 * Remember for each of the master ports of the bus if we got an 365 * address range from the connected slave. For convenience, also 366 * keep track of if we got ranges from all the slave modules or 367 * not. 368 */ 369 std::vector<bool> gotAddrRanges; 370 bool gotAllAddrRanges; 371 372 /** The master and slave ports of the bus */ 373 std::vector<SlavePort*> slavePorts; 374 std::vector<MasterPort*> masterPorts; 375 376 /** Convenience typedefs. */ 377 typedef std::vector<SlavePort*>::iterator SlavePortIter; 378 typedef std::vector<MasterPort*>::iterator MasterPortIter; 379 typedef std::vector<SlavePort*>::const_iterator SlavePortConstIter; 380 typedef std::vector<MasterPort*>::const_iterator MasterPortConstIter; 381 382 /** Port that handles requests that don't match any of the interfaces.*/ 383 PortID defaultPortID; 384 385 /** If true, use address range provided by default device. Any 386 address not handled by another port and not in default device's 387 range will cause a fatal error. If false, just send all 388 addresses not handled by another port to default device. */ 389 const bool useDefaultRange; 390 391 uint32_t blockSize; 392 393 BaseBus(const BaseBusParams *p); 394 395 virtual ~BaseBus(); 396 397 /** 398 * Stats for transaction distribution and data passing through the 399 * bus. The transaction distribution is globally counting 400 * different types of commands. The packet count and total packet 401 * size are two-dimensional vectors that are indexed by the bus 402 * slave port and master port id (thus the neighbouring master and 403 * neighbouring slave), summing up both directions (request and 404 * response). 405 */ 406 Stats::Formula throughput; 407 Stats::Vector transDist; 408 Stats::Vector2d pktCount; 409 Stats::Vector2d totPktSize; 410 411 public: 412 413 virtual void init(); 414 415 /** A function used to return the port associated with this bus object. */ 416 BaseMasterPort& getMasterPort(const std::string& if_name, 417 PortID idx = InvalidPortID); 418 BaseSlavePort& getSlavePort(const std::string& if_name, 419 PortID idx = InvalidPortID); 420 421 virtual unsigned int drain(DrainManager *dm) = 0; 422 423 virtual void regStats(); 424 425}; 426 427#endif //__MEM_BUS_HH__ 428