xbar.hh revision 10888:85a001f2193b
1/*
2 * Copyright (c) 2011-2015 ARM Limited
3 * All rights reserved
4 *
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.
13 *
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
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.
27 *
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.
39 *
40 * Authors: Ron Dreslinski
41 *          Ali Saidi
42 *          Andreas Hansson
43 *          William Wang
44 */
45
46/**
47 * @file
48 * Declaration of an abstract crossbar base class.
49 */
50
51#ifndef __MEM_XBAR_HH__
52#define __MEM_XBAR_HH__
53
54#include <deque>
55
56#include "base/addr_range_map.hh"
57#include "base/hashmap.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"
63
64/**
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.
69 *
70 * The BaseXBar is responsible for the basic flow control (busy or
71 * not), the administration of retries, and the address decoding.
72 */
73class BaseXBar : public MemObject
74{
75
76  protected:
77
78    /**
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,
84     * PCIe, etc.
85     *
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.
91     */
92    template <typename SrcType, typename DstType>
93    class Layer : public Drainable
94    {
95
96      public:
97
98        /**
99         * Create a layer and give it a name. The layer uses
100         * the crossbar an event manager.
101         *
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
105         */
106        Layer(DstType& _port, BaseXBar& _xbar, const std::string& _name);
107
108        /**
109         * Drain according to the normal semantics, so that the crossbar
110         * can tell the layer to drain, and pass an event to signal
111         * back when drained.
112         *
113         * @param de drain event to call once drained
114         *
115         * @return 1 if busy or waiting to retry, or 0 if idle
116         */
117        unsigned int drain(DrainManager *dm);
118
119        /**
120         * Get the crossbar layer's name
121         */
122        const std::string name() const { return xbar.name() + _name; }
123
124
125        /**
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.
130         *
131         * @param port Source port presenting the packet
132         *
133         * @return True if the layer accepts the packet
134         */
135        bool tryTiming(SrcType* src_port);
136
137        /**
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.
141         *
142         * @param busy_time Time to spend as a result of a successful send
143         */
144        void succeededTiming(Tick busy_time);
145
146        /**
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
150         * accordingly.
151         *
152         * @param src_port Source port
153         * @param busy_time Time to spend as a result of a failed send
154         */
155        void failedTiming(SrcType* src_port, Tick busy_time);
156
157        /** Occupy the layer until until */
158        void occupyLayer(Tick until);
159
160        /**
161         * Send a retry to the port at the head of waitingForLayer. The
162         * caller must ensure that the list is not empty.
163         */
164        void retryWaiting();
165
166        /**
167         * Handle a retry from a neighbouring module. This wraps
168         * retryWaiting by verifying that there are ports waiting
169         * before calling retryWaiting.
170         */
171        void recvRetry();
172
173        /**
174         * Register stats for the layer
175         */
176        void regStats();
177
178      protected:
179
180        /**
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.
185         */
186        virtual void sendRetry(SrcType* retry_port) = 0;
187
188      private:
189
190        /** The destination port this layer converges at. */
191        DstType& port;
192
193        /** The crossbar this layer is a part of. */
194        BaseXBar& xbar;
195
196        /** A name for this layer. */
197        std::string _name;
198
199        /**
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.
214         */
215        enum State { IDLE, BUSY, RETRY };
216
217        /** track the state of the layer */
218        State state;
219
220        /** manager to signal when drained */
221        DrainManager *drainManager;
222
223        /**
224         * A deque of ports that retry should be called on because
225         * the original send was delayed due to a busy layer.
226         */
227        std::deque<SrcType*> waitingForLayer;
228
229        /**
230         * Track who is waiting for the retry when receiving it from a
231         * peer. If no port is waiting NULL is stored.
232         */
233        SrcType* waitingForPeer;
234
235        /**
236         * Release the layer after being occupied and return to an
237         * idle state where we proceed to send a retry to any
238         * potential waiting port, or drain if asked to do so.
239         */
240        void releaseLayer();
241
242        /** event used to schedule a release of the layer */
243        EventWrapper<Layer, &Layer::releaseLayer> releaseEvent;
244
245        /**
246         * Stats for occupancy and utilization. These stats capture
247         * the time the layer spends in the busy state and are thus only
248         * relevant when the memory system is in timing mode.
249         */
250        Stats::Scalar occupancy;
251        Stats::Formula utilization;
252
253    };
254
255    class ReqLayer : public Layer<SlavePort,MasterPort>
256    {
257      public:
258        /**
259         * Create a request layer and give it a name.
260         *
261         * @param _port destination port the layer converges at
262         * @param _xbar the crossbar this layer belongs to
263         * @param _name the layer's name
264         */
265        ReqLayer(MasterPort& _port, BaseXBar& _xbar, const std::string& _name) :
266            Layer(_port, _xbar, _name) {}
267
268      protected:
269
270        void sendRetry(SlavePort* retry_port)
271        { retry_port->sendRetryReq(); }
272    };
273
274    class RespLayer : public Layer<MasterPort,SlavePort>
275    {
276      public:
277        /**
278         * Create a response layer and give it a name.
279         *
280         * @param _port destination port the layer converges at
281         * @param _xbar the crossbar this layer belongs to
282         * @param _name the layer's name
283         */
284        RespLayer(SlavePort& _port, BaseXBar& _xbar, const std::string& _name) :
285            Layer(_port, _xbar, _name) {}
286
287      protected:
288
289        void sendRetry(MasterPort* retry_port)
290        { retry_port->sendRetryResp(); }
291    };
292
293    class SnoopRespLayer : public Layer<SlavePort,MasterPort>
294    {
295      public:
296        /**
297         * Create a snoop response layer and give it a name.
298         *
299         * @param _port destination port the layer converges at
300         * @param _xbar the crossbar this layer belongs to
301         * @param _name the layer's name
302         */
303        SnoopRespLayer(MasterPort& _port, BaseXBar& _xbar,
304                       const std::string& _name) :
305            Layer(_port, _xbar, _name) {}
306
307      protected:
308
309        void sendRetry(SlavePort* retry_port)
310        { retry_port->sendRetrySnoopResp(); }
311    };
312
313    /**
314     * Cycles of front-end pipeline including the delay to accept the request
315     * and to decode the address.
316     */
317    const Cycles frontendLatency;
318    /** Cycles of forward latency */
319    const Cycles forwardLatency;
320    /** Cycles of response latency */
321    const Cycles responseLatency;
322    /** the width of the xbar in bytes */
323    const uint32_t width;
324
325    AddrRangeMap<PortID> portMap;
326
327    /**
328     * Remember where request packets came from so that we can route
329     * responses to the appropriate port. This relies on the fact that
330     * the underlying Request pointer inside the Packet stays
331     * constant.
332     */
333    m5::unordered_map<RequestPtr, PortID> routeTo;
334
335    /** all contigous ranges seen by this crossbar */
336    AddrRangeList xbarRanges;
337
338    AddrRange defaultRange;
339
340    /**
341     * Function called by the port when the crossbar is recieving a
342     * range change.
343     *
344     * @param master_port_id id of the port that received the change
345     */
346    void recvRangeChange(PortID master_port_id);
347
348    /** Find which port connected to this crossbar (if any) should be
349     * given a packet with this address.
350     *
351     * @param addr Address to find port for.
352     * @return id of port that the packet should be sent out of.
353     */
354    PortID findPort(Addr addr);
355
356    // Cache for the findPort function storing recently used ports from portMap
357    struct PortCache {
358        bool valid;
359        PortID id;
360        AddrRange range;
361    };
362
363    PortCache portCache[3];
364
365    // Checks the cache and returns the id of the port that has the requested
366    // address within its range
367    inline PortID checkPortCache(Addr addr) const {
368        if (portCache[0].valid && portCache[0].range.contains(addr)) {
369            return portCache[0].id;
370        }
371        if (portCache[1].valid && portCache[1].range.contains(addr)) {
372            return portCache[1].id;
373        }
374        if (portCache[2].valid && portCache[2].range.contains(addr)) {
375            return portCache[2].id;
376        }
377
378        return InvalidPortID;
379    }
380
381    // Clears the earliest entry of the cache and inserts a new port entry
382    inline void updatePortCache(short id, const AddrRange& range) {
383        portCache[2].valid = portCache[1].valid;
384        portCache[2].id    = portCache[1].id;
385        portCache[2].range = portCache[1].range;
386
387        portCache[1].valid = portCache[0].valid;
388        portCache[1].id    = portCache[0].id;
389        portCache[1].range = portCache[0].range;
390
391        portCache[0].valid = true;
392        portCache[0].id    = id;
393        portCache[0].range = range;
394    }
395
396    // Clears the cache. Needs to be called in constructor.
397    inline void clearPortCache() {
398        portCache[2].valid = false;
399        portCache[1].valid = false;
400        portCache[0].valid = false;
401    }
402
403    /**
404     * Return the address ranges the crossbar is responsible for.
405     *
406     * @return a list of non-overlapping address ranges
407     */
408    AddrRangeList getAddrRanges() const;
409
410    /**
411     * Calculate the timing parameters for the packet. Updates the
412     * headerDelay and payloadDelay fields of the packet
413     * object with the relative number of ticks required to transmit
414     * the header and the payload, respectively.
415     *
416     * @param pkt Packet to populate with timings
417     * @param header_delay Header delay to be added
418     */
419    void calcPacketTiming(PacketPtr pkt, Tick header_delay);
420
421    /**
422     * Remember for each of the master ports of the crossbar if we got
423     * an address range from the connected slave. For convenience,
424     * also keep track of if we got ranges from all the slave modules
425     * or not.
426     */
427    std::vector<bool> gotAddrRanges;
428    bool gotAllAddrRanges;
429
430    /** The master and slave ports of the crossbar */
431    std::vector<QueuedSlavePort*> slavePorts;
432    std::vector<MasterPort*> masterPorts;
433
434    /** Port that handles requests that don't match any of the interfaces.*/
435    PortID defaultPortID;
436
437    /** If true, use address range provided by default device.  Any
438       address not handled by another port and not in default device's
439       range will cause a fatal error.  If false, just send all
440       addresses not handled by another port to default device. */
441    const bool useDefaultRange;
442
443    BaseXBar(const BaseXBarParams *p);
444
445    virtual ~BaseXBar();
446
447    /**
448     * Stats for transaction distribution and data passing through the
449     * crossbar. The transaction distribution is globally counting
450     * different types of commands. The packet count and total packet
451     * size are two-dimensional vectors that are indexed by the
452     * slave port and master port id (thus the neighbouring master and
453     * neighbouring slave), summing up both directions (request and
454     * response).
455     */
456    Stats::Vector transDist;
457    Stats::Vector2d pktCount;
458    Stats::Vector2d pktSize;
459
460  public:
461
462    virtual void init();
463
464    /** A function used to return the port associated with this object. */
465    BaseMasterPort& getMasterPort(const std::string& if_name,
466                                  PortID idx = InvalidPortID);
467    BaseSlavePort& getSlavePort(const std::string& if_name,
468                                PortID idx = InvalidPortID);
469
470    virtual unsigned int drain(DrainManager *dm) = 0;
471
472    virtual void regStats();
473
474};
475
476#endif //__MEM_XBAR_HH__
477