2 * Copyright (c) 2011-2015, 2018 ARM Limited
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.
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * All rights reserved.
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.
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.
40 * Authors: Ron Dreslinski
48 * Declaration of an abstract crossbar base class.
51 #ifndef __MEM_XBAR_HH__
52 #define __MEM_XBAR_HH__
55 #include <unordered_map>
57 #include "base/addr_range_map.hh"
58 #include "base/types.hh"
59 #include "mem/mem_object.hh"
60 #include "mem/qport.hh"
61 #include "params/BaseXBar.hh"
62 #include "sim/stats.hh"
65 * The base crossbar contains the common elements of the non-coherent
66 * and coherent crossbar. It is an abstract class that does not have
67 * any of the functionality relating to the actual reception and
68 * transmission of packets, as this is left for the subclasses.
70 * The BaseXBar is responsible for the basic flow control (busy or
71 * not), the administration of retries, and the address decoding.
73 class BaseXBar : public MemObject
79 * A layer is an internal crossbar arbitration point with its own
80 * flow control. Each layer is a converging multiplexer tree. By
81 * instantiating one layer per destination port (and per packet
82 * type, i.e. request, response, snoop request and snoop
83 * response), we model full crossbar structures like AXI, ACE,
86 * The template parameter, PortClass, indicates the destination
87 * port type for the layer. The retry list holds either master
88 * ports or slave ports, depending on the direction of the
89 * layer. Thus, a request layer has a retry list containing slave
90 * ports, whereas a response layer holds master ports.
92 template <typename SrcType, typename DstType>
93 class Layer : public Drainable
99 * Create a layer and give it a name. The layer uses
100 * the crossbar an event manager.
102 * @param _port destination port the layer converges at
103 * @param _xbar the crossbar this layer belongs to
104 * @param _name the layer's name
106 Layer(DstType& _port, BaseXBar& _xbar, const std::string& _name);
109 * Drain according to the normal semantics, so that the crossbar
110 * can tell the layer to drain, and pass an event to signal
113 * @param de drain event to call once drained
115 * @return 1 if busy or waiting to retry, or 0 if idle
117 DrainState drain() override;
120 * Get the crossbar layer's name
122 const std::string name() const { return xbar.name() + _name; }
126 * Determine if the layer accepts a packet from a specific
127 * port. If not, the port in question is also added to the
128 * retry list. In either case the state of the layer is
129 * updated accordingly.
131 * @param port Source port presenting the packet
133 * @return True if the layer accepts the packet
135 bool tryTiming(SrcType* src_port);
138 * Deal with a destination port accepting a packet by potentially
139 * removing the source port from the retry list (if retrying) and
140 * occupying the layer accordingly.
142 * @param busy_time Time to spend as a result of a successful send
144 void succeededTiming(Tick busy_time);
147 * Deal with a destination port not accepting a packet by
148 * potentially adding the source port to the retry list (if
149 * not already at the front) and occupying the layer
152 * @param src_port Source port
153 * @param busy_time Time to spend as a result of a failed send
155 void failedTiming(SrcType* src_port, Tick busy_time);
157 /** Occupy the layer until until */
158 void occupyLayer(Tick until);
161 * Send a retry to the port at the head of waitingForLayer. The
162 * caller must ensure that the list is not empty.
167 * Handle a retry from a neighbouring module. This wraps
168 * retryWaiting by verifying that there are ports waiting
169 * before calling retryWaiting.
174 * Register stats for the layer
181 * Sending the actual retry, in a manner specific to the
182 * individual layers. Note that for a MasterPort, there is
183 * both a RequestLayer and a SnoopResponseLayer using the same
184 * port, but using different functions for the flow control.
186 virtual void sendRetry(SrcType* retry_port) = 0;
190 /** The destination port this layer converges at. */
193 /** The crossbar this layer is a part of. */
196 /** A name for this layer. */
200 * We declare an enum to track the state of the layer. The
201 * starting point is an idle state where the layer is waiting
202 * for a packet to arrive. Upon arrival, the layer
203 * transitions to the busy state, where it remains either
204 * until the packet transfer is done, or the header time is
205 * spent. Once the layer leaves the busy state, it can
206 * either go back to idle, if no packets have arrived while it
207 * was busy, or the layer goes on to retry the first port
208 * in waitingForLayer. A similar transition takes place from
209 * idle to retry if the layer receives a retry from one of
210 * its connected ports. The retry state lasts until the port
211 * in questions calls sendTiming and returns control to the
212 * layer, or goes to a busy state if the port does not
213 * immediately react to the retry by calling sendTiming.
215 enum State { IDLE, BUSY, RETRY };
217 /** track the state of the layer */
221 * A deque of ports that retry should be called on because
222 * the original send was delayed due to a busy layer.
224 std::deque<SrcType*> waitingForLayer;
227 * Track who is waiting for the retry when receiving it from a
228 * peer. If no port is waiting NULL is stored.
230 SrcType* waitingForPeer;
233 * Release the layer after being occupied and return to an
234 * idle state where we proceed to send a retry to any
235 * potential waiting port, or drain if asked to do so.
239 /** event used to schedule a release of the layer */
240 EventFunctionWrapper releaseEvent;
243 * Stats for occupancy and utilization. These stats capture
244 * the time the layer spends in the busy state and are thus only
245 * relevant when the memory system is in timing mode.
247 Stats::Scalar occupancy;
248 Stats::Formula utilization;
252 class ReqLayer : public Layer<SlavePort,MasterPort>
256 * Create a request layer and give it a name.
258 * @param _port destination port the layer converges at
259 * @param _xbar the crossbar this layer belongs to
260 * @param _name the layer's name
262 ReqLayer(MasterPort& _port, BaseXBar& _xbar, const std::string& _name) :
263 Layer(_port, _xbar, _name) {}
267 void sendRetry(SlavePort* retry_port)
268 { retry_port->sendRetryReq(); }
271 class RespLayer : public Layer<MasterPort,SlavePort>
275 * Create a response layer and give it a name.
277 * @param _port destination port the layer converges at
278 * @param _xbar the crossbar this layer belongs to
279 * @param _name the layer's name
281 RespLayer(SlavePort& _port, BaseXBar& _xbar, const std::string& _name) :
282 Layer(_port, _xbar, _name) {}
286 void sendRetry(MasterPort* retry_port)
287 { retry_port->sendRetryResp(); }
290 class SnoopRespLayer : public Layer<SlavePort,MasterPort>
294 * Create a snoop response layer and give it a name.
296 * @param _port destination port the layer converges at
297 * @param _xbar the crossbar this layer belongs to
298 * @param _name the layer's name
300 SnoopRespLayer(MasterPort& _port, BaseXBar& _xbar,
301 const std::string& _name) :
302 Layer(_port, _xbar, _name) {}
306 void sendRetry(SlavePort* retry_port)
307 { retry_port->sendRetrySnoopResp(); }
311 * Cycles of front-end pipeline including the delay to accept the request
312 * and to decode the address.
314 const Cycles frontendLatency;
315 /** Cycles of forward latency */
316 const Cycles forwardLatency;
317 /** Cycles of response latency */
318 const Cycles responseLatency;
319 /** the width of the xbar in bytes */
320 const uint32_t width;
322 AddrRangeMap<PortID, 3> portMap;
325 * Remember where request packets came from so that we can route
326 * responses to the appropriate port. This relies on the fact that
327 * the underlying Request pointer inside the Packet stays
330 std::unordered_map<RequestPtr, PortID> routeTo;
332 /** all contigous ranges seen by this crossbar */
333 AddrRangeList xbarRanges;
335 AddrRange defaultRange;
338 * Function called by the port when the crossbar is recieving a
341 * @param master_port_id id of the port that received the change
343 virtual void recvRangeChange(PortID master_port_id);
346 * Find which port connected to this crossbar (if any) should be
347 * given a packet with this address range.
349 * @param addr_range Address range to find port for.
350 * @return id of port that the packet should be sent out of.
352 PortID findPort(AddrRange addr_range);
355 * Return the address ranges the crossbar is responsible for.
357 * @return a list of non-overlapping address ranges
359 AddrRangeList getAddrRanges() const;
362 * Calculate the timing parameters for the packet. Updates the
363 * headerDelay and payloadDelay fields of the packet
364 * object with the relative number of ticks required to transmit
365 * the header and the payload, respectively.
367 * @param pkt Packet to populate with timings
368 * @param header_delay Header delay to be added
370 void calcPacketTiming(PacketPtr pkt, Tick header_delay);
373 * Remember for each of the master ports of the crossbar if we got
374 * an address range from the connected slave. For convenience,
375 * also keep track of if we got ranges from all the slave modules
378 std::vector<bool> gotAddrRanges;
379 bool gotAllAddrRanges;
381 /** The master and slave ports of the crossbar */
382 std::vector<QueuedSlavePort*> slavePorts;
383 std::vector<MasterPort*> masterPorts;
385 /** Port that handles requests that don't match any of the interfaces.*/
386 PortID defaultPortID;
388 /** If true, use address range provided by default device. Any
389 address not handled by another port and not in default device's
390 range will cause a fatal error. If false, just send all
391 addresses not handled by another port to default device. */
392 const bool useDefaultRange;
394 BaseXBar(const BaseXBarParams *p);
397 * Stats for transaction distribution and data passing through the
398 * crossbar. The transaction distribution is globally counting
399 * different types of commands. The packet count and total packet
400 * size are two-dimensional vectors that are indexed by the
401 * slave port and master port id (thus the neighbouring master and
402 * neighbouring slave), summing up both directions (request and
405 Stats::Vector transDist;
406 Stats::Vector2d pktCount;
407 Stats::Vector2d pktSize;
413 void init() override;
415 /** A function used to return the port associated with this object. */
416 Port &getPort(const std::string &if_name,
417 PortID idx=InvalidPortID) override;
419 void regStats() override;
423 #endif //__MEM_XBAR_HH__