xbar.hh revision 10719:b4fc9ad648aa
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 "params/BaseXBar.hh"
61#include "sim/stats.hh"
62
63/**
64 * The base crossbar contains the common elements of the non-coherent
65 * and coherent crossbar. It is an abstract class that does not have
66 * any of the functionality relating to the actual reception and
67 * transmission of packets, as this is left for the subclasses.
68 *
69 * The BaseXBar is responsible for the basic flow control (busy or
70 * not), the administration of retries, and the address decoding.
71 */
72class BaseXBar : public MemObject
73{
74
75  protected:
76
77    /**
78     * A layer is an internal crossbar arbitration point with its own
79     * flow control. Each layer is a converging multiplexer tree. By
80     * instantiating one layer per destination port (and per packet
81     * type, i.e. request, response, snoop request and snoop
82     * response), we model full crossbar structures like AXI, ACE,
83     * PCIe, etc.
84     *
85     * The template parameter, PortClass, indicates the destination
86     * port type for the layer. The retry list holds either master
87     * ports or slave ports, depending on the direction of the
88     * layer. Thus, a request layer has a retry list containing slave
89     * ports, whereas a response layer holds master ports.
90     */
91    template <typename SrcType, typename DstType>
92    class Layer : public Drainable
93    {
94
95      public:
96
97        /**
98         * Create a layer and give it a name. The layer uses
99         * the crossbar an event manager.
100         *
101         * @param _port destination port the layer converges at
102         * @param _xbar the crossbar this layer belongs to
103         * @param _name the layer's name
104         */
105        Layer(DstType& _port, BaseXBar& _xbar, const std::string& _name);
106
107        /**
108         * Drain according to the normal semantics, so that the crossbar
109         * can tell the layer to drain, and pass an event to signal
110         * back when drained.
111         *
112         * @param de drain event to call once drained
113         *
114         * @return 1 if busy or waiting to retry, or 0 if idle
115         */
116        unsigned int drain(DrainManager *dm);
117
118        /**
119         * Get the crossbar layer's name
120         */
121        const std::string name() const { return xbar.name() + _name; }
122
123
124        /**
125         * Determine if the layer accepts a packet from a specific
126         * port. If not, the port in question is also added to the
127         * retry list. In either case the state of the layer is
128         * updated accordingly.
129         *
130         * @param port Source port presenting the packet
131         *
132         * @return True if the layer accepts the packet
133         */
134        bool tryTiming(SrcType* src_port);
135
136        /**
137         * Deal with a destination port accepting a packet by potentially
138         * removing the source port from the retry list (if retrying) and
139         * occupying the layer accordingly.
140         *
141         * @param busy_time Time to spend as a result of a successful send
142         */
143        void succeededTiming(Tick busy_time);
144
145        /**
146         * Deal with a destination port not accepting a packet by
147         * potentially adding the source port to the retry list (if
148         * not already at the front) and occupying the layer
149         * accordingly.
150         *
151         * @param src_port Source port
152         * @param busy_time Time to spend as a result of a failed send
153         */
154        void failedTiming(SrcType* src_port, Tick busy_time);
155
156        /** Occupy the layer until until */
157        void occupyLayer(Tick until);
158
159        /**
160         * Send a retry to the port at the head of waitingForLayer. The
161         * caller must ensure that the list is not empty.
162         */
163        void retryWaiting();
164
165        /**
166         * Handle a retry from a neighbouring module. This wraps
167         * retryWaiting by verifying that there are ports waiting
168         * before calling retryWaiting.
169         */
170        void recvRetry();
171
172        /**
173         * Register stats for the layer
174         */
175        void regStats();
176
177      protected:
178
179        /**
180         * Sending the actual retry, in a manner specific to the
181         * individual layers. Note that for a MasterPort, there is
182         * both a RequestLayer and a SnoopResponseLayer using the same
183         * port, but using different functions for the flow control.
184         */
185        virtual void sendRetry(SrcType* retry_port) = 0;
186
187      private:
188
189        /** The destination port this layer converges at. */
190        DstType& port;
191
192        /** The crossbar this layer is a part of. */
193        BaseXBar& xbar;
194
195        /** A name for this layer. */
196        std::string _name;
197
198        /**
199         * We declare an enum to track the state of the layer. The
200         * starting point is an idle state where the layer is waiting
201         * for a packet to arrive. Upon arrival, the layer
202         * transitions to the busy state, where it remains either
203         * until the packet transfer is done, or the header time is
204         * spent. Once the layer leaves the busy state, it can
205         * either go back to idle, if no packets have arrived while it
206         * was busy, or the layer goes on to retry the first port
207         * in waitingForLayer. A similar transition takes place from
208         * idle to retry if the layer receives a retry from one of
209         * its connected ports. The retry state lasts until the port
210         * in questions calls sendTiming and returns control to the
211         * layer, or goes to a busy state if the port does not
212         * immediately react to the retry by calling sendTiming.
213         */
214        enum State { IDLE, BUSY, RETRY };
215
216        /** track the state of the layer */
217        State state;
218
219        /** manager to signal when drained */
220        DrainManager *drainManager;
221
222        /**
223         * A deque of ports that retry should be called on because
224         * the original send was delayed due to a busy layer.
225         */
226        std::deque<SrcType*> waitingForLayer;
227
228        /**
229         * Track who is waiting for the retry when receiving it from a
230         * peer. If no port is waiting NULL is stored.
231         */
232        SrcType* waitingForPeer;
233
234        /**
235         * Release the layer after being occupied and return to an
236         * idle state where we proceed to send a retry to any
237         * potential waiting port, or drain if asked to do so.
238         */
239        void releaseLayer();
240
241        /** event used to schedule a release of the layer */
242        EventWrapper<Layer, &Layer::releaseLayer> releaseEvent;
243
244        /**
245         * Stats for occupancy and utilization. These stats capture
246         * the time the layer spends in the busy state and are thus only
247         * relevant when the memory system is in timing mode.
248         */
249        Stats::Scalar occupancy;
250        Stats::Formula utilization;
251
252    };
253
254    class ReqLayer : public Layer<SlavePort,MasterPort>
255    {
256      public:
257        /**
258         * Create a request layer and give it a name.
259         *
260         * @param _port destination port the layer converges at
261         * @param _xbar the crossbar this layer belongs to
262         * @param _name the layer's name
263         */
264        ReqLayer(MasterPort& _port, BaseXBar& _xbar, const std::string& _name) :
265            Layer(_port, _xbar, _name) {}
266
267      protected:
268
269        void sendRetry(SlavePort* retry_port)
270        { retry_port->sendRetryReq(); }
271    };
272
273    class RespLayer : public Layer<MasterPort,SlavePort>
274    {
275      public:
276        /**
277         * Create a response layer and give it a name.
278         *
279         * @param _port destination port the layer converges at
280         * @param _xbar the crossbar this layer belongs to
281         * @param _name the layer's name
282         */
283        RespLayer(SlavePort& _port, BaseXBar& _xbar, const std::string& _name) :
284            Layer(_port, _xbar, _name) {}
285
286      protected:
287
288        void sendRetry(MasterPort* retry_port)
289        { retry_port->sendRetryResp(); }
290    };
291
292    class SnoopRespLayer : public Layer<SlavePort,MasterPort>
293    {
294      public:
295        /**
296         * Create a snoop response layer and give it a name.
297         *
298         * @param _port destination port the layer converges at
299         * @param _xbar the crossbar this layer belongs to
300         * @param _name the layer's name
301         */
302        SnoopRespLayer(MasterPort& _port, BaseXBar& _xbar,
303                       const std::string& _name) :
304            Layer(_port, _xbar, _name) {}
305
306      protected:
307
308        void sendRetry(SlavePort* retry_port)
309        { retry_port->sendRetrySnoopResp(); }
310    };
311
312    /**
313     * Cycles of front-end pipeline including the delay to accept the request
314     * and to decode the address.
315     */
316    const Cycles frontendLatency;
317    /** Cycles of forward latency */
318    const Cycles forwardLatency;
319    /** Cycles of response latency */
320    const Cycles responseLatency;
321    /** the width of the xbar in bytes */
322    const uint32_t width;
323
324    AddrRangeMap<PortID> portMap;
325
326    /**
327     * Remember where request packets came from so that we can route
328     * responses to the appropriate port. This relies on the fact that
329     * the underlying Request pointer inside the Packet stays
330     * constant.
331     */
332    m5::unordered_map<RequestPtr, PortID> routeTo;
333
334    /** all contigous ranges seen by this crossbar */
335    AddrRangeList xbarRanges;
336
337    AddrRange defaultRange;
338
339    /**
340     * Function called by the port when the crossbar is recieving a
341     * range change.
342     *
343     * @param master_port_id id of the port that received the change
344     */
345    void recvRangeChange(PortID master_port_id);
346
347    /** Find which port connected to this crossbar (if any) should be
348     * given a packet with this address.
349     *
350     * @param addr Address to find port for.
351     * @return id of port that the packet should be sent out of.
352     */
353    PortID findPort(Addr addr);
354
355    // Cache for the findPort function storing recently used ports from portMap
356    struct PortCache {
357        bool valid;
358        PortID id;
359        AddrRange range;
360    };
361
362    PortCache portCache[3];
363
364    // Checks the cache and returns the id of the port that has the requested
365    // address within its range
366    inline PortID checkPortCache(Addr addr) const {
367        if (portCache[0].valid && portCache[0].range.contains(addr)) {
368            return portCache[0].id;
369        }
370        if (portCache[1].valid && portCache[1].range.contains(addr)) {
371            return portCache[1].id;
372        }
373        if (portCache[2].valid && portCache[2].range.contains(addr)) {
374            return portCache[2].id;
375        }
376
377        return InvalidPortID;
378    }
379
380    // Clears the earliest entry of the cache and inserts a new port entry
381    inline void updatePortCache(short id, const AddrRange& range) {
382        portCache[2].valid = portCache[1].valid;
383        portCache[2].id    = portCache[1].id;
384        portCache[2].range = portCache[1].range;
385
386        portCache[1].valid = portCache[0].valid;
387        portCache[1].id    = portCache[0].id;
388        portCache[1].range = portCache[0].range;
389
390        portCache[0].valid = true;
391        portCache[0].id    = id;
392        portCache[0].range = range;
393    }
394
395    // Clears the cache. Needs to be called in constructor.
396    inline void clearPortCache() {
397        portCache[2].valid = false;
398        portCache[1].valid = false;
399        portCache[0].valid = false;
400    }
401
402    /**
403     * Return the address ranges the crossbar is responsible for.
404     *
405     * @return a list of non-overlapping address ranges
406     */
407    AddrRangeList getAddrRanges() const;
408
409    /**
410     * Calculate the timing parameters for the packet. Updates the
411     * headerDelay and payloadDelay fields of the packet
412     * object with the relative number of ticks required to transmit
413     * the header and the payload, respectively.
414     *
415     * @param pkt Packet to populate with timings
416     * @param header_delay Header delay to be added
417     */
418    void calcPacketTiming(PacketPtr pkt, Tick header_delay);
419
420    /**
421     * Remember for each of the master ports of the crossbar if we got
422     * an address range from the connected slave. For convenience,
423     * also keep track of if we got ranges from all the slave modules
424     * or not.
425     */
426    std::vector<bool> gotAddrRanges;
427    bool gotAllAddrRanges;
428
429    /** The master and slave ports of the crossbar */
430    std::vector<SlavePort*> slavePorts;
431    std::vector<MasterPort*> masterPorts;
432
433    /** Port that handles requests that don't match any of the interfaces.*/
434    PortID defaultPortID;
435
436    /** If true, use address range provided by default device.  Any
437       address not handled by another port and not in default device's
438       range will cause a fatal error.  If false, just send all
439       addresses not handled by another port to default device. */
440    const bool useDefaultRange;
441
442    BaseXBar(const BaseXBarParams *p);
443
444    virtual ~BaseXBar();
445
446    /**
447     * Stats for transaction distribution and data passing through the
448     * crossbar. The transaction distribution is globally counting
449     * different types of commands. The packet count and total packet
450     * size are two-dimensional vectors that are indexed by the
451     * slave port and master port id (thus the neighbouring master and
452     * neighbouring slave), summing up both directions (request and
453     * response).
454     */
455    Stats::Vector transDist;
456    Stats::Vector2d pktCount;
457    Stats::Vector2d pktSize;
458
459  public:
460
461    virtual void init();
462
463    /** A function used to return the port associated with this object. */
464    BaseMasterPort& getMasterPort(const std::string& if_name,
465                                  PortID idx = InvalidPortID);
466    BaseSlavePort& getSlavePort(const std::string& if_name,
467                                PortID idx = InvalidPortID);
468
469    virtual unsigned int drain(DrainManager *dm) = 0;
470
471    virtual void regStats();
472
473};
474
475#endif //__MEM_XBAR_HH__
476