xbar.hh revision 9092
111731Sjason@lowepower.com/* 211731Sjason@lowepower.com * Copyright (c) 2011-2012 ARM Limited 312137Sar4jc@virginia.edu * All rights reserved 412137Sar4jc@virginia.edu * 512137Sar4jc@virginia.edu * The license below extends only to copyright in the software and shall 612137Sar4jc@virginia.edu * not be construed as granting a license to any other intellectual 712137Sar4jc@virginia.edu * property including but not limited to intellectual property relating 812137Sar4jc@virginia.edu * to a hardware implementation of the functionality of the software 912137Sar4jc@virginia.edu * licensed hereunder. You may use the software subject to the license 1012137Sar4jc@virginia.edu * terms below provided that you ensure that this notice is replicated 1112137Sar4jc@virginia.edu * unmodified and in its entirety in all distributions of the software, 1212137Sar4jc@virginia.edu * modified or unmodified, in source code or in binary form. 1312137Sar4jc@virginia.edu * 1412137Sar4jc@virginia.edu * Copyright (c) 2002-2005 The Regents of The University of Michigan 1512137Sar4jc@virginia.edu * All rights reserved. 1612137Sar4jc@virginia.edu * 1712137Sar4jc@virginia.edu * Redistribution and use in source and binary forms, with or without 1812137Sar4jc@virginia.edu * modification, are permitted provided that the following conditions are 1912137Sar4jc@virginia.edu * met: redistributions of source code must retain the above copyright 2012137Sar4jc@virginia.edu * notice, this list of conditions and the following disclaimer; 2112137Sar4jc@virginia.edu * redistributions in binary form must reproduce the above copyright 2212137Sar4jc@virginia.edu * notice, this list of conditions and the following disclaimer in the 2312137Sar4jc@virginia.edu * documentation and/or other materials provided with the distribution; 2412137Sar4jc@virginia.edu * neither the name of the copyright holders nor the names of its 2512137Sar4jc@virginia.edu * contributors may be used to endorse or promote products derived from 2612137Sar4jc@virginia.edu * this software without specific prior written permission. 2712137Sar4jc@virginia.edu * 2812137Sar4jc@virginia.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2912137Sar4jc@virginia.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 3012137Sar4jc@virginia.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 3112137Sar4jc@virginia.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 3212137Sar4jc@virginia.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3312137Sar4jc@virginia.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3412137Sar4jc@virginia.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3512137Sar4jc@virginia.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3612137Sar4jc@virginia.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3712137Sar4jc@virginia.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3812137Sar4jc@virginia.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3912137Sar4jc@virginia.edu * 4012137Sar4jc@virginia.edu * Authors: Ron Dreslinski 4112137Sar4jc@virginia.edu * Ali Saidi 4212137Sar4jc@virginia.edu * Andreas Hansson 4312137Sar4jc@virginia.edu * William Wang 4412137Sar4jc@virginia.edu */ 4512137Sar4jc@virginia.edu 4612137Sar4jc@virginia.edu/** 4712137Sar4jc@virginia.edu * @file 4812137Sar4jc@virginia.edu * Declaration of an abstract bus base class. 4912137Sar4jc@virginia.edu */ 5012137Sar4jc@virginia.edu 5112137Sar4jc@virginia.edu#ifndef __MEM_BUS_HH__ 5212137Sar4jc@virginia.edu#define __MEM_BUS_HH__ 5312137Sar4jc@virginia.edu 5412137Sar4jc@virginia.edu#include <list> 5512137Sar4jc@virginia.edu#include <set> 5612137Sar4jc@virginia.edu 5712137Sar4jc@virginia.edu#include "base/range.hh" 5812137Sar4jc@virginia.edu#include "base/range_map.hh" 5912137Sar4jc@virginia.edu#include "base/types.hh" 6012137Sar4jc@virginia.edu#include "mem/mem_object.hh" 6112137Sar4jc@virginia.edu#include "params/BaseBus.hh" 6212137Sar4jc@virginia.edu 6312137Sar4jc@virginia.edu/** 6412137Sar4jc@virginia.edu * The base bus contains the common elements of the non-coherent and 6512137Sar4jc@virginia.edu * coherent bus. It is an abstract class that does not have any of the 6612137Sar4jc@virginia.edu * functionality relating to the actual reception and transmission of 6712137Sar4jc@virginia.edu * packets, as this is left for the subclasses. 6812137Sar4jc@virginia.edu * 6912137Sar4jc@virginia.edu * The BaseBus is responsible for the basic flow control (busy or 7012137Sar4jc@virginia.edu * not), the administration of retries, and the address decoding. 7112137Sar4jc@virginia.edu */ 7212137Sar4jc@virginia.educlass BaseBus : public MemObject 7312137Sar4jc@virginia.edu{ 7412137Sar4jc@virginia.edu 7512137Sar4jc@virginia.edu protected: 7612137Sar4jc@virginia.edu 7712137Sar4jc@virginia.edu /** 7812137Sar4jc@virginia.edu * A bus layer is an internal bus structure with its own flow 7912137Sar4jc@virginia.edu * control and arbitration. Hence, a single-layer bus mimics a 8012137Sar4jc@virginia.edu * traditional off-chip tri-state bus (like PCI), where only one 8112137Sar4jc@virginia.edu * set of wires are shared. For on-chip buses, a good starting 8212137Sar4jc@virginia.edu * point is to have three layers, for requests, responses, and 8312137Sar4jc@virginia.edu * snoop responses respectively (snoop requests are instantaneous 8412137Sar4jc@virginia.edu * and do not need any flow control or arbitration). This case is 8512137Sar4jc@virginia.edu * similar to AHB and some OCP configurations. As a further 8612137Sar4jc@virginia.edu * extensions beyond the three-layer bus, a future multi-layer bus 8712137Sar4jc@virginia.edu * has with one layer per connected slave port provides a full or 8812137Sar4jc@virginia.edu * partial crossbar, like AXI, OCP, PCIe etc. 8912137Sar4jc@virginia.edu */ 9012137Sar4jc@virginia.edu class Layer 9112137Sar4jc@virginia.edu { 9212137Sar4jc@virginia.edu 9312137Sar4jc@virginia.edu public: 9412137Sar4jc@virginia.edu 9512137Sar4jc@virginia.edu /** 9612137Sar4jc@virginia.edu * Create a bus layer and give it a name. The bus layer uses 9712137Sar4jc@virginia.edu * the bus an event manager. 9812137Sar4jc@virginia.edu * 9912137Sar4jc@virginia.edu * @param _bus the bus this layer belongs to 10012137Sar4jc@virginia.edu * @param _name the layer's name 10112137Sar4jc@virginia.edu * @param _clock clock period in ticks 10212137Sar4jc@virginia.edu */ 10312137Sar4jc@virginia.edu Layer(BaseBus& _bus, const std::string& _name, Tick _clock); 10412137Sar4jc@virginia.edu 10512137Sar4jc@virginia.edu /** 10612137Sar4jc@virginia.edu * Drain according to the normal semantics, so that the bus 10712137Sar4jc@virginia.edu * can tell the layer to drain, and pass an event to signal 10812137Sar4jc@virginia.edu * back when drained. 10912137Sar4jc@virginia.edu * 11012137Sar4jc@virginia.edu * @param de drain event to call once drained 11112137Sar4jc@virginia.edu * 11212137Sar4jc@virginia.edu * @return 1 if busy or waiting to retry, or 0 if idle 11312137Sar4jc@virginia.edu */ 11412137Sar4jc@virginia.edu unsigned int drain(Event *de); 11512137Sar4jc@virginia.edu 11612137Sar4jc@virginia.edu /** 11712137Sar4jc@virginia.edu * Get the bus layer's name 11812137Sar4jc@virginia.edu */ 11912137Sar4jc@virginia.edu const std::string name() const { return bus.name() + _name; } 12012137Sar4jc@virginia.edu 12112137Sar4jc@virginia.edu 12212137Sar4jc@virginia.edu /** 12312137Sar4jc@virginia.edu * Determine if the bus layer accepts a packet from a specific 12412137Sar4jc@virginia.edu * port. If not, the port in question is also added to the 12512137Sar4jc@virginia.edu * retry list. In either case the state of the layer is updated 12612137Sar4jc@virginia.edu * accordingly. 12712137Sar4jc@virginia.edu * 12812137Sar4jc@virginia.edu * @param port Source port resenting the packet 12912137Sar4jc@virginia.edu * 13012137Sar4jc@virginia.edu * @return True if the bus layer accepts the packet 13112137Sar4jc@virginia.edu */ 13212137Sar4jc@virginia.edu bool tryTiming(Port* port); 13312137Sar4jc@virginia.edu 13412137Sar4jc@virginia.edu /** 13512137Sar4jc@virginia.edu * Deal with a destination port accepting a packet by potentially 13612137Sar4jc@virginia.edu * removing the source port from the retry list (if retrying) and 13712137Sar4jc@virginia.edu * occupying the bus layer accordingly. 13812137Sar4jc@virginia.edu * 13912137Sar4jc@virginia.edu * @param busy_time Time to spend as a result of a successful send 14012137Sar4jc@virginia.edu */ 14112137Sar4jc@virginia.edu void succeededTiming(Tick busy_time); 14212137Sar4jc@virginia.edu 14312137Sar4jc@virginia.edu /** 14412137Sar4jc@virginia.edu * Deal with a destination port not accepting a packet by 14512137Sar4jc@virginia.edu * potentially adding the source port to the retry list (if 14612137Sar4jc@virginia.edu * not already at the front) and occupying the bus layer 14712137Sar4jc@virginia.edu * accordingly. 14812137Sar4jc@virginia.edu * 14912137Sar4jc@virginia.edu * @param busy_time Time to spend as a result of a failed send 15012137Sar4jc@virginia.edu */ 15112137Sar4jc@virginia.edu void failedTiming(SlavePort* port, Tick busy_time); 15212137Sar4jc@virginia.edu 15312137Sar4jc@virginia.edu /** Occupy the bus layer until until */ 15412137Sar4jc@virginia.edu void occupyLayer(Tick until); 15512137Sar4jc@virginia.edu 15612137Sar4jc@virginia.edu /** 15712137Sar4jc@virginia.edu * Send a retry to the port at the head of the retryList. The 15812137Sar4jc@virginia.edu * caller must ensure that the list is not empty. 15911731Sjason@lowepower.com */ 16011731Sjason@lowepower.com void retryWaiting(); 161 162 /** 163 * Handler a retry from a neighbouring module. Eventually this 164 * should be all encapsulated in the bus. This wraps 165 * retryWaiting by verifying that there are ports waiting 166 * before calling retryWaiting. 167 */ 168 void recvRetry(); 169 170 private: 171 172 /** The bus this layer is a part of. */ 173 BaseBus& bus; 174 175 /** A name for this layer. */ 176 std::string _name; 177 178 /** 179 * We declare an enum to track the state of the bus layer. The 180 * starting point is an idle state where the bus layer is 181 * waiting for a packet to arrive. Upon arrival, the bus layer 182 * transitions to the busy state, where it remains either 183 * until the packet transfer is done, or the header time is 184 * spent. Once the bus layer leaves the busy state, it can 185 * either go back to idle, if no packets have arrived while it 186 * was busy, or the bus layer goes on to retry the first port 187 * on the retryList. A similar transition takes place from 188 * idle to retry if the bus layer receives a retry from one of 189 * its connected ports. The retry state lasts until the port 190 * in questions calls sendTiming and returns control to the 191 * bus layer, or goes to a busy state if the port does not 192 * immediately react to the retry by calling sendTiming. 193 */ 194 enum State { IDLE, BUSY, RETRY }; 195 196 /** track the state of the bus layer */ 197 State state; 198 199 /** the clock speed for the bus layer */ 200 Tick clock; 201 202 /** event for signalling when drained */ 203 Event * drainEvent; 204 205 /** 206 * An array of pointers to ports that retry should be called 207 * on because the original send failed for whatever reason. 208 */ 209 std::list<Port*> retryList; 210 211 /** 212 * Release the bus layer after being occupied and return to an 213 * idle state where we proceed to send a retry to any 214 * potential waiting port, or drain if asked to do so. 215 */ 216 void releaseLayer(); 217 218 /** event used to schedule a release of the layer */ 219 EventWrapper<Layer, &Layer::releaseLayer> releaseEvent; 220 221 }; 222 223 /** the clock speed for the bus */ 224 Tick clock; 225 /** cycles of overhead per transaction */ 226 int headerCycles; 227 /** the width of the bus in bytes */ 228 int width; 229 230 typedef range_map<Addr, PortID>::iterator PortMapIter; 231 typedef range_map<Addr, PortID>::const_iterator PortMapConstIter; 232 range_map<Addr, PortID> portMap; 233 234 AddrRangeList defaultRange; 235 236 /** 237 * Function called by the port when the bus is recieving a range change. 238 * 239 * @param master_port_id id of the port that received the change 240 */ 241 void recvRangeChange(PortID master_port_id); 242 243 /** Find which port connected to this bus (if any) should be given a packet 244 * with this address. 245 * @param addr Address to find port for. 246 * @return id of port that the packet should be sent out of. 247 */ 248 PortID findPort(Addr addr); 249 250 // Cache for the findPort function storing recently used ports from portMap 251 struct PortCache { 252 bool valid; 253 PortID id; 254 Addr start; 255 Addr end; 256 }; 257 258 PortCache portCache[3]; 259 260 // Checks the cache and returns the id of the port that has the requested 261 // address within its range 262 inline PortID checkPortCache(Addr addr) { 263 if (portCache[0].valid && addr >= portCache[0].start && 264 addr < portCache[0].end) { 265 return portCache[0].id; 266 } 267 if (portCache[1].valid && addr >= portCache[1].start && 268 addr < portCache[1].end) { 269 return portCache[1].id; 270 } 271 if (portCache[2].valid && addr >= portCache[2].start && 272 addr < portCache[2].end) { 273 return portCache[2].id; 274 } 275 276 return InvalidPortID; 277 } 278 279 // Clears the earliest entry of the cache and inserts a new port entry 280 inline void updatePortCache(short id, Addr start, Addr end) { 281 portCache[2].valid = portCache[1].valid; 282 portCache[2].id = portCache[1].id; 283 portCache[2].start = portCache[1].start; 284 portCache[2].end = portCache[1].end; 285 286 portCache[1].valid = portCache[0].valid; 287 portCache[1].id = portCache[0].id; 288 portCache[1].start = portCache[0].start; 289 portCache[1].end = portCache[0].end; 290 291 portCache[0].valid = true; 292 portCache[0].id = id; 293 portCache[0].start = start; 294 portCache[0].end = end; 295 } 296 297 // Clears the cache. Needs to be called in constructor. 298 inline void clearPortCache() { 299 portCache[2].valid = false; 300 portCache[1].valid = false; 301 portCache[0].valid = false; 302 } 303 304 /** 305 * Return the address ranges the bus is responsible for. 306 * 307 * @return a list of non-overlapping address ranges 308 */ 309 AddrRangeList getAddrRanges() const; 310 311 /** Calculate the timing parameters for the packet. Updates the 312 * firstWordTime and finishTime fields of the packet object. 313 * Returns the tick at which the packet header is completed (which 314 * will be all that is sent if the target rejects the packet). 315 */ 316 Tick calcPacketTiming(PacketPtr pkt); 317 318 /** 319 * Ask everyone on the bus what their size is 320 * 321 * @return the max of all the sizes 322 */ 323 unsigned findBlockSize(); 324 325 std::set<PortID> inRecvRangeChange; 326 327 /** The master and slave ports of the bus */ 328 std::vector<SlavePort*> slavePorts; 329 std::vector<MasterPort*> masterPorts; 330 331 /** Convenience typedefs. */ 332 typedef std::vector<SlavePort*>::iterator SlavePortIter; 333 typedef std::vector<MasterPort*>::iterator MasterPortIter; 334 typedef std::vector<SlavePort*>::const_iterator SlavePortConstIter; 335 typedef std::vector<MasterPort*>::const_iterator MasterPortConstIter; 336 337 /** Port that handles requests that don't match any of the interfaces.*/ 338 PortID defaultPortID; 339 340 /** If true, use address range provided by default device. Any 341 address not handled by another port and not in default device's 342 range will cause a fatal error. If false, just send all 343 addresses not handled by another port to default device. */ 344 bool useDefaultRange; 345 346 unsigned defaultBlockSize; 347 unsigned cachedBlockSize; 348 bool cachedBlockSizeValid; 349 350 BaseBus(const BaseBusParams *p); 351 352 virtual ~BaseBus(); 353 354 public: 355 356 /** A function used to return the port associated with this bus object. */ 357 virtual MasterPort& getMasterPort(const std::string& if_name, int idx = -1); 358 virtual SlavePort& getSlavePort(const std::string& if_name, int idx = -1); 359 360 virtual unsigned int drain(Event *de) = 0; 361 362}; 363 364#endif //__MEM_BUS_HH__ 365