bridge.hh revision 8975
14776Sgblack@eecs.umich.edu/*
24776Sgblack@eecs.umich.edu * Copyright (c) 2011-2012 ARM Limited
34776Sgblack@eecs.umich.edu * All rights reserved
44776Sgblack@eecs.umich.edu *
54776Sgblack@eecs.umich.edu * The license below extends only to copyright in the software and shall
64776Sgblack@eecs.umich.edu * not be construed as granting a license to any other intellectual
74776Sgblack@eecs.umich.edu * property including but not limited to intellectual property relating
84776Sgblack@eecs.umich.edu * to a hardware implementation of the functionality of the software
94776Sgblack@eecs.umich.edu * licensed hereunder.  You may use the software subject to the license
104776Sgblack@eecs.umich.edu * terms below provided that you ensure that this notice is replicated
114776Sgblack@eecs.umich.edu * unmodified and in its entirety in all distributions of the software,
124776Sgblack@eecs.umich.edu * modified or unmodified, in source code or in binary form.
134776Sgblack@eecs.umich.edu *
144776Sgblack@eecs.umich.edu * Copyright (c) 2006 The Regents of The University of Michigan
154776Sgblack@eecs.umich.edu * All rights reserved.
164776Sgblack@eecs.umich.edu *
174776Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
184776Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
194776Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
204776Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
214776Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
224776Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
234776Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
244776Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
254776Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
264776Sgblack@eecs.umich.edu * this software without specific prior written permission.
274776Sgblack@eecs.umich.edu *
284776Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
294776Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
304776Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
314776Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
324776Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
334776Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
344776Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
354832Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
364776Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
374776Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
384776Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
394776Sgblack@eecs.umich.edu *
404776Sgblack@eecs.umich.edu * Authors: Ali Saidi
414776Sgblack@eecs.umich.edu *          Steve Reinhardt
424776Sgblack@eecs.umich.edu *          Andreas Hansson
434776Sgblack@eecs.umich.edu */
444776Sgblack@eecs.umich.edu
454776Sgblack@eecs.umich.edu/**
464776Sgblack@eecs.umich.edu * @file
474776Sgblack@eecs.umich.edu * Declaration of a memory-mapped bus bridge that connects a master
484776Sgblack@eecs.umich.edu * and a slave through a request and response queue.
494776Sgblack@eecs.umich.edu */
504776Sgblack@eecs.umich.edu
514776Sgblack@eecs.umich.edu#ifndef __MEM_BRIDGE_HH__
524776Sgblack@eecs.umich.edu#define __MEM_BRIDGE_HH__
534776Sgblack@eecs.umich.edu
544776Sgblack@eecs.umich.edu#include <list>
554776Sgblack@eecs.umich.edu#include <queue>
564776Sgblack@eecs.umich.edu#include <string>
574776Sgblack@eecs.umich.edu
584776Sgblack@eecs.umich.edu#include "base/fast_alloc.hh"
594776Sgblack@eecs.umich.edu#include "base/types.hh"
604776Sgblack@eecs.umich.edu#include "mem/mem_object.hh"
614776Sgblack@eecs.umich.edu#include "mem/packet.hh"
624776Sgblack@eecs.umich.edu#include "mem/port.hh"
634776Sgblack@eecs.umich.edu#include "params/Bridge.hh"
644776Sgblack@eecs.umich.edu#include "sim/eventq.hh"
654776Sgblack@eecs.umich.edu
664776Sgblack@eecs.umich.edu/**
674776Sgblack@eecs.umich.edu * A bridge is used to interface two different busses (or in general a
684776Sgblack@eecs.umich.edu * memory-mapped master and slave), with buffering for requests and
694776Sgblack@eecs.umich.edu * responses. The bridge has a fixed delay for packets passing through
704776Sgblack@eecs.umich.edu * it and responds to a fixed set of address ranges.
714776Sgblack@eecs.umich.edu *
724776Sgblack@eecs.umich.edu * The bridge comprises a slave port and a master port, that buffer
734776Sgblack@eecs.umich.edu * outgoing responses and requests respectively. Buffer space is
744776Sgblack@eecs.umich.edu * reserved when a request arrives, also reserving response space
754776Sgblack@eecs.umich.edu * before forwarding the request. An incoming request is always
764776Sgblack@eecs.umich.edu * accepted (recvTiming returns true), but is potentially NACKed if
774776Sgblack@eecs.umich.edu * there is no request space or response space.
784776Sgblack@eecs.umich.edu */
794776Sgblack@eecs.umich.educlass Bridge : public MemObject
804776Sgblack@eecs.umich.edu{
814776Sgblack@eecs.umich.edu  protected:
824776Sgblack@eecs.umich.edu
834776Sgblack@eecs.umich.edu    /**
844776Sgblack@eecs.umich.edu     * A packet buffer stores packets along with their sender state
854776Sgblack@eecs.umich.edu     * and scheduled time for transmission.
864776Sgblack@eecs.umich.edu     */
874776Sgblack@eecs.umich.edu    class PacketBuffer : public Packet::SenderState, public FastAlloc {
884776Sgblack@eecs.umich.edu
894776Sgblack@eecs.umich.edu      public:
904776Sgblack@eecs.umich.edu        Tick ready;
914776Sgblack@eecs.umich.edu        PacketPtr pkt;
924776Sgblack@eecs.umich.edu        bool nackedHere;
934776Sgblack@eecs.umich.edu        Packet::SenderState *origSenderState;
944776Sgblack@eecs.umich.edu        Packet::NodeID origSrc;
954776Sgblack@eecs.umich.edu        bool expectResponse;
964776Sgblack@eecs.umich.edu
974776Sgblack@eecs.umich.edu        PacketBuffer(PacketPtr _pkt, Tick t, bool nack = false)
984776Sgblack@eecs.umich.edu            : ready(t), pkt(_pkt), nackedHere(nack),
994776Sgblack@eecs.umich.edu              origSenderState(_pkt->senderState),
1004776Sgblack@eecs.umich.edu              origSrc(nack ? _pkt->getDest() : _pkt->getSrc() ),
1014776Sgblack@eecs.umich.edu              expectResponse(_pkt->needsResponse() && !nack)
1024776Sgblack@eecs.umich.edu
1034776Sgblack@eecs.umich.edu        {
1044776Sgblack@eecs.umich.edu            if (!pkt->isResponse() && !nack)
1054776Sgblack@eecs.umich.edu                pkt->senderState = this;
1064776Sgblack@eecs.umich.edu        }
1074776Sgblack@eecs.umich.edu
1084776Sgblack@eecs.umich.edu        void fixResponse(PacketPtr pkt)
1094776Sgblack@eecs.umich.edu        {
1104776Sgblack@eecs.umich.edu            assert(pkt->senderState == this);
1114776Sgblack@eecs.umich.edu            pkt->setDest(origSrc);
1124776Sgblack@eecs.umich.edu            pkt->senderState = origSenderState;
1134776Sgblack@eecs.umich.edu        }
1144776Sgblack@eecs.umich.edu    };
1154776Sgblack@eecs.umich.edu
1164776Sgblack@eecs.umich.edu    // Forward declaration to allow the slave port to have a pointer
1174776Sgblack@eecs.umich.edu    class BridgeMasterPort;
1184776Sgblack@eecs.umich.edu
1194776Sgblack@eecs.umich.edu    /**
1204776Sgblack@eecs.umich.edu     * The port on the side that receives requests and sends
1214776Sgblack@eecs.umich.edu     * responses. The slave port has a set of address ranges that it
1224776Sgblack@eecs.umich.edu     * is responsible for. The slave port also has a buffer for the
1234776Sgblack@eecs.umich.edu     * responses not yet sent.
1244776Sgblack@eecs.umich.edu     */
1254776Sgblack@eecs.umich.edu    class BridgeSlavePort : public SlavePort
1264776Sgblack@eecs.umich.edu    {
1274776Sgblack@eecs.umich.edu
1284776Sgblack@eecs.umich.edu      private:
1294776Sgblack@eecs.umich.edu
1304776Sgblack@eecs.umich.edu        /** A pointer to the bridge to which this port belongs. */
1314776Sgblack@eecs.umich.edu        Bridge *bridge;
1324776Sgblack@eecs.umich.edu
1334776Sgblack@eecs.umich.edu        /**
1344776Sgblack@eecs.umich.edu         * Master port on the other side of the bridge
1354776Sgblack@eecs.umich.edu         * (connected to the other bus).
1364776Sgblack@eecs.umich.edu         */
1374776Sgblack@eecs.umich.edu        BridgeMasterPort& masterPort;
1384776Sgblack@eecs.umich.edu
1394776Sgblack@eecs.umich.edu        /** Minimum request delay though this bridge. */
1404776Sgblack@eecs.umich.edu        Tick delay;
1414776Sgblack@eecs.umich.edu
1424776Sgblack@eecs.umich.edu        /** Min delay to respond with a nack. */
1434776Sgblack@eecs.umich.edu        Tick nackDelay;
1444776Sgblack@eecs.umich.edu
1454776Sgblack@eecs.umich.edu        /** Address ranges to pass through the bridge */
1464776Sgblack@eecs.umich.edu        AddrRangeList ranges;
1474776Sgblack@eecs.umich.edu
148        /**
149         * Response packet queue. Response packets are held in this
150         * queue for a specified delay to model the processing delay
151         * of the bridge.
152         */
153        std::list<PacketBuffer*> responseQueue;
154
155        /** Counter to track the outstanding responses. */
156        unsigned int outstandingResponses;
157
158        /** If we're waiting for a retry to happen. */
159        bool inRetry;
160
161        /** Max queue size for reserved responses. */
162        unsigned int respQueueLimit;
163
164        /**
165         * Is this side blocked from accepting new response packets.
166         *
167         * @return true if the reserved space has reached the set limit
168         */
169        bool respQueueFull();
170
171        /**
172         * Turn the request packet into a NACK response and put it in
173         * the response queue and schedule its transmission.
174         *
175         * @param pkt the request packet to NACK
176         */
177        void nackRequest(PacketPtr pkt);
178
179        /**
180         * Handle send event, scheduled when the packet at the head of
181         * the response queue is ready to transmit (for timing
182         * accesses only).
183         */
184        void trySend();
185
186        /**
187         * Private class for scheduling sending of responses from the
188         * response queue.
189         */
190        class SendEvent : public Event
191        {
192            BridgeSlavePort& port;
193
194          public:
195            SendEvent(BridgeSlavePort& p) : port(p) {}
196            virtual void process() { port.trySend(); }
197            virtual const char *description() const { return "bridge send"; }
198        };
199
200        /** Send event for the response queue. */
201        SendEvent sendEvent;
202
203      public:
204
205        /**
206         * Constructor for the BridgeSlavePort.
207         *
208         * @param _name the port name including the owner
209         * @param _bridge the structural owner
210         * @param _masterPort the master port on the other side of the bridge
211         * @param _delay the delay from seeing a response to sending it
212         * @param _nack_delay the delay from a NACK to sending the response
213         * @param _resp_limit the size of the response queue
214         * @param _ranges a number of address ranges to forward
215         */
216        BridgeSlavePort(const std::string &_name, Bridge *_bridge,
217                        BridgeMasterPort& _masterPort, int _delay,
218                        int _nack_delay, int _resp_limit,
219                        std::vector<Range<Addr> > _ranges);
220
221        /**
222         * Queue a response packet to be sent out later and also schedule
223         * a send if necessary.
224         *
225         * @param pkt a response to send out after a delay
226         */
227        void queueForSendTiming(PacketPtr pkt);
228
229      protected:
230
231        /** When receiving a timing request from the peer port,
232            pass it to the bridge. */
233        virtual bool recvTimingReq(PacketPtr pkt);
234
235        /** When receiving a retry request from the peer port,
236            pass it to the bridge. */
237        virtual void recvRetry();
238
239        /** When receiving a Atomic requestfrom the peer port,
240            pass it to the bridge. */
241        virtual Tick recvAtomic(PacketPtr pkt);
242
243        /** When receiving a Functional request from the peer port,
244            pass it to the bridge. */
245        virtual void recvFunctional(PacketPtr pkt);
246
247        /** When receiving a address range request the peer port,
248            pass it to the bridge. */
249        virtual AddrRangeList getAddrRanges();
250    };
251
252
253    /**
254     * Port on the side that forwards requests and receives
255     * responses. The master port has a buffer for the requests not
256     * yet sent.
257     */
258    class BridgeMasterPort : public MasterPort
259    {
260
261      private:
262
263        /** A pointer to the bridge to which this port belongs. */
264        Bridge* bridge;
265
266        /**
267         * Pointer to the slave port on the other side of the bridge
268         * (connected to the other bus).
269         */
270        BridgeSlavePort& slavePort;
271
272        /** Minimum delay though this bridge. */
273        Tick delay;
274
275        /**
276         * Request packet queue. Request packets are held in this
277         * queue for a specified delay to model the processing delay
278         * of the bridge.
279         */
280        std::list<PacketBuffer*> requestQueue;
281
282        /** If we're waiting for a retry to happen. */
283        bool inRetry;
284
285        /** Max queue size for request packets */
286        unsigned int reqQueueLimit;
287
288        /**
289         * Handle send event, scheduled when the packet at the head of
290         * the outbound queue is ready to transmit (for timing
291         * accesses only).
292         */
293        void trySend();
294
295        /**
296         * Private class for scheduling sending of requests from the
297         * request queue.
298         */
299        class SendEvent : public Event
300        {
301            BridgeMasterPort& port;
302
303          public:
304            SendEvent(BridgeMasterPort& p) : port(p) {}
305            virtual void process() { port.trySend(); }
306            virtual const char *description() const { return "bridge send"; }
307        };
308
309        /** Send event for the request queue. */
310        SendEvent sendEvent;
311
312      public:
313
314        /**
315         * Constructor for the BridgeMasterPort.
316         *
317         * @param _name the port name including the owner
318         * @param _bridge the structural owner
319         * @param _slavePort the slave port on the other side of the bridge
320         * @param _delay the delay from seeing a request to sending it
321         * @param _req_limit the size of the request queue
322         */
323        BridgeMasterPort(const std::string &_name, Bridge *_bridge,
324                         BridgeSlavePort& _slavePort, int _delay,
325                         int _req_limit);
326
327        /**
328         * Is this side blocked from accepting new request packets.
329         *
330         * @return true if the occupied space has reached the set limit
331         */
332        bool reqQueueFull();
333
334        /**
335         * Queue a request packet to be sent out later and also schedule
336         * a send if necessary.
337         *
338         * @param pkt a request to send out after a delay
339         */
340        void queueForSendTiming(PacketPtr pkt);
341
342        /**
343         * Check a functional request against the packets in our
344         * request queue.
345         *
346         * @param pkt packet to check against
347         *
348         * @return true if we find a match
349         */
350        bool checkFunctional(PacketPtr pkt);
351
352      protected:
353
354        /** When receiving a timing request from the peer port,
355            pass it to the bridge. */
356        virtual bool recvTimingResp(PacketPtr pkt);
357
358        /** When receiving a retry request from the peer port,
359            pass it to the bridge. */
360        virtual void recvRetry();
361    };
362
363    /** Slave port of the bridge. */
364    BridgeSlavePort slavePort;
365
366    /** Master port of the bridge. */
367    BridgeMasterPort masterPort;
368
369    /** If this bridge should acknowledge writes. */
370    bool ackWrites;
371
372  public:
373    typedef BridgeParams Params;
374
375  protected:
376    Params *_params;
377
378  public:
379    const Params *params() const { return _params; }
380
381    virtual MasterPort& getMasterPort(const std::string& if_name,
382                                      int idx = -1);
383    virtual SlavePort& getSlavePort(const std::string& if_name, int idx = -1);
384
385    virtual void init();
386
387    Bridge(Params *p);
388};
389
390#endif //__MEM_BUS_HH__
391