coherent_xbar.hh revision 8948
1/*
2 * Copyright (c) 2011 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 a bus object.
49 */
50
51#ifndef __MEM_BUS_HH__
52#define __MEM_BUS_HH__
53
54#include <list>
55#include <set>
56#include <string>
57
58#include "base/range.hh"
59#include "base/range_map.hh"
60#include "base/types.hh"
61#include "mem/mem_object.hh"
62#include "mem/packet.hh"
63#include "mem/port.hh"
64#include "params/Bus.hh"
65#include "sim/eventq.hh"
66
67class Bus : public MemObject
68{
69    /**
70     * Declaration of the bus slave port type, one will be
71     * instantiated for each of the master interfaces connecting to
72     * the bus.
73     */
74    class BusSlavePort : public SlavePort
75    {
76      private:
77        /** A pointer to the bus to which this port belongs. */
78        Bus *bus;
79
80        /** A id to keep track of the interface ID of this port. */
81        int id;
82
83      public:
84
85        /** Constructor for the BusSlavePort.*/
86        BusSlavePort(const std::string &_name, Bus *_bus, int _id)
87            : SlavePort(_name, _bus), bus(_bus), id(_id)
88        { }
89
90        int getId() const { return id; }
91
92      protected:
93
94        /**
95         * When receiving a timing request, pass it to the bus.
96         */
97        virtual bool recvTiming(PacketPtr pkt)
98        { pkt->setSrc(id); return bus->recvTiming(pkt); }
99
100        /**
101         * When receiving a timing snoop response, pass it to the bus.
102         */
103        virtual bool recvTimingSnoop(PacketPtr pkt)
104        { pkt->setSrc(id); return bus->recvTimingSnoop(pkt); }
105
106        /**
107         * When receiving an atomic request, pass it to the bus.
108         */
109        virtual Tick recvAtomic(PacketPtr pkt)
110        { pkt->setSrc(id); return bus->recvAtomic(pkt); }
111
112        /**
113         * When receiving a functional request, pass it to the bus.
114         */
115        virtual void recvFunctional(PacketPtr pkt)
116        { pkt->setSrc(id); bus->recvFunctional(pkt); }
117
118        /**
119         * When receiving a retry, pass it to the bus.
120         */
121        virtual void recvRetry()
122        { panic("Bus slave ports always succeed and should never retry.\n"); }
123
124        // This should return all the 'owned' addresses that are
125        // downstream from this bus, yes?  That is, the union of all
126        // the 'owned' address ranges of all the other interfaces on
127        // this bus...
128        virtual AddrRangeList getAddrRanges()
129        { return bus->getAddrRanges(id); }
130
131        // Ask the bus to ask everyone on the bus what their block size is and
132        // take the max of it. This might need to be changed a bit if we ever
133        // support multiple block sizes.
134        virtual unsigned deviceBlockSize() const
135        { return bus->findBlockSize(id); }
136
137    };
138
139    /**
140     * Declaration of the bus master port type, one will be
141     * instantiated for each of the slave interfaces connecting to the
142     * bus.
143     */
144    class BusMasterPort : public MasterPort
145    {
146      private:
147        /** A pointer to the bus to which this port belongs. */
148        Bus *bus;
149
150        /** A id to keep track of the interface ID of this port. */
151        int id;
152
153      public:
154
155        /** Constructor for the BusMasterPort.*/
156        BusMasterPort(const std::string &_name, Bus *_bus, int _id)
157            : MasterPort(_name, _bus), bus(_bus), id(_id)
158        { }
159
160        int getId() const { return id; }
161
162        /**
163         * Determine if this port should be considered a snooper. This
164         * is determined by the bus.
165         *
166         * @return a boolean that is true if this port is snooping
167         */
168        virtual bool isSnooping() const
169        { return bus->isSnooping(id); }
170
171      protected:
172
173        /**
174         * When receiving a timing response, pass it to the bus.
175         */
176        virtual bool recvTiming(PacketPtr pkt)
177        { pkt->setSrc(id); return bus->recvTiming(pkt); }
178
179        /**
180         * When receiving a timing snoop request, pass it to the bus.
181         */
182        virtual bool recvTimingSnoop(PacketPtr pkt)
183        { pkt->setSrc(id); return bus->recvTimingSnoop(pkt); }
184
185        /**
186         * When receiving an atomic snoop request, pass it to the bus.
187         */
188        virtual Tick recvAtomicSnoop(PacketPtr pkt)
189        { pkt->setSrc(id); return bus->recvAtomicSnoop(pkt); }
190
191        /**
192         * When receiving a functional snoop request, pass it to the bus.
193         */
194        virtual void recvFunctionalSnoop(PacketPtr pkt)
195        { pkt->setSrc(id); bus->recvFunctionalSnoop(pkt); }
196
197        /** When reciving a range change from the peer port (at id),
198            pass it to the bus. */
199        virtual void recvRangeChange()
200        { bus->recvRangeChange(id); }
201
202        /** When reciving a retry from the peer port (at id),
203            pass it to the bus. */
204        virtual void recvRetry()
205        { bus->recvRetry(id); }
206
207        // Ask the bus to ask everyone on the bus what their block size is and
208        // take the max of it. This might need to be changed a bit if we ever
209        // support multiple block sizes.
210        virtual unsigned deviceBlockSize() const
211        { return bus->findBlockSize(id); }
212
213    };
214
215    /** the clock speed for the bus */
216    int clock;
217    /** cycles of overhead per transaction */
218    int headerCycles;
219    /** the width of the bus in bytes */
220    int width;
221    /** the next tick at which the bus will be idle */
222    Tick tickNextIdle;
223
224    Event * drainEvent;
225
226    typedef range_map<Addr,int>::iterator PortIter;
227    range_map<Addr, int> portMap;
228
229    AddrRangeList defaultRange;
230
231    typedef std::vector<BusSlavePort*>::iterator SnoopIter;
232    std::vector<BusSlavePort*> snoopPorts;
233
234    /**
235     * Store the outstanding requests so we can determine which ones
236     * we generated and which ones were merely forwarded. This is used
237     * in the coherent bus when coherency responses come back.
238     */
239    std::set<RequestPtr> outstandingReq;
240
241    /** Function called by the port when the bus is recieving a Timing
242      transaction.*/
243    bool recvTiming(PacketPtr pkt);
244
245    /** Function called by the port when the bus is recieving a timing
246        snoop transaction.*/
247    bool recvTimingSnoop(PacketPtr pkt);
248
249    /**
250     * Forward a timing packet to our snoopers, potentially excluding
251     * one of the connected coherent masters to avoid sending a packet
252     * back to where it came from.
253     *
254     * @param pkt Packet to forward
255     * @param exclude_slave_port_id Id of slave port to exclude
256     */
257    void forwardTiming(PacketPtr pkt, int exclude_slave_port_id);
258
259    /**
260     * Determine if the bus is to be considered occupied when being
261     * presented with a packet from a specific port. If so, the port
262     * in question is also added to the retry list.
263     *
264     * @param pkt Incoming packet
265     * @param port Source port on the bus presenting the packet
266     *
267     * @return True if the bus is to be considered occupied
268     */
269    bool isOccupied(PacketPtr pkt, Port* port);
270
271    /**
272     * Deal with a destination port accepting a packet by potentially
273     * removing the source port from the retry list (if retrying) and
274     * occupying the bus accordingly.
275     *
276     * @param busy_time Time to spend as a result of a successful send
277     */
278    void succeededTiming(Tick busy_time);
279
280    /** Function called by the port when the bus is recieving a Atomic
281      transaction.*/
282    Tick recvAtomic(PacketPtr pkt);
283
284    /** Function called by the port when the bus is recieving an
285        atomic snoop transaction.*/
286    Tick recvAtomicSnoop(PacketPtr pkt);
287
288    /**
289     * Forward an atomic packet to our snoopers, potentially excluding
290     * one of the connected coherent masters to avoid sending a packet
291     * back to where it came from.
292     *
293     * @param pkt Packet to forward
294     * @param exclude_slave_port_id Id of slave port to exclude
295     *
296     * @return a pair containing the snoop response and snoop latency
297     */
298    std::pair<MemCmd, Tick> forwardAtomic(PacketPtr pkt,
299                                          int exclude_slave_port_id);
300
301    /** Function called by the port when the bus is recieving a Functional
302        transaction.*/
303    void recvFunctional(PacketPtr pkt);
304
305    /** Function called by the port when the bus is recieving a functional
306        snoop transaction.*/
307    void recvFunctionalSnoop(PacketPtr pkt);
308
309    /**
310     * Forward a functional packet to our snoopers, potentially
311     * excluding one of the connected coherent masters to avoid
312     * sending a packet back to where it came from.
313     *
314     * @param pkt Packet to forward
315     * @param exclude_slave_port_id Id of slave port to exclude
316     */
317    void forwardFunctional(PacketPtr pkt, int exclude_slave_port_id);
318
319    /** Timing function called by port when it is once again able to process
320     * requests. */
321    void recvRetry(int id);
322
323    /** Function called by the port when the bus is recieving a range change.*/
324    void recvRangeChange(int id);
325
326    /** Find which port connected to this bus (if any) should be given a packet
327     * with this address.
328     * @param addr Address to find port for.
329     * @return id of port that the packet should be sent out of.
330     */
331    int findPort(Addr addr);
332
333    // Cache for the findPort function storing recently used ports from portMap
334    struct PortCache {
335        bool valid;
336        int  id;
337        Addr start;
338        Addr end;
339    };
340
341    PortCache portCache[3];
342
343    // Checks the cache and returns the id of the port that has the requested
344    // address within its range
345    inline int checkPortCache(Addr addr) {
346        if (portCache[0].valid && addr >= portCache[0].start &&
347            addr < portCache[0].end) {
348            return portCache[0].id;
349        }
350        if (portCache[1].valid && addr >= portCache[1].start &&
351                   addr < portCache[1].end) {
352            return portCache[1].id;
353        }
354        if (portCache[2].valid && addr >= portCache[2].start &&
355            addr < portCache[2].end) {
356            return portCache[2].id;
357        }
358
359        return INVALID_PORT_ID;
360    }
361
362    // Clears the earliest entry of the cache and inserts a new port entry
363    inline void updatePortCache(short id, Addr start, Addr end) {
364        portCache[2].valid = portCache[1].valid;
365        portCache[2].id    = portCache[1].id;
366        portCache[2].start = portCache[1].start;
367        portCache[2].end   = portCache[1].end;
368
369        portCache[1].valid = portCache[0].valid;
370        portCache[1].id    = portCache[0].id;
371        portCache[1].start = portCache[0].start;
372        portCache[1].end   = portCache[0].end;
373
374        portCache[0].valid = true;
375        portCache[0].id    = id;
376        portCache[0].start = start;
377        portCache[0].end   = end;
378    }
379
380    // Clears the cache. Needs to be called in constructor.
381    inline void clearPortCache() {
382        portCache[2].valid = false;
383        portCache[1].valid = false;
384        portCache[0].valid = false;
385    }
386
387    /**
388     * Return the address ranges this port is responsible for.
389     *
390     * @param id id of the bus port that made the request
391     *
392     * @return a list of non-overlapping address ranges
393     */
394    AddrRangeList getAddrRanges(int id);
395
396    /**
397     * Determine if the bus port is snooping or not.
398     *
399     * @param id id of the bus port that made the request
400     *
401     * @return a boolean indicating if this port is snooping or not
402     */
403    bool isSnooping(int id) const;
404
405    /** Calculate the timing parameters for the packet.  Updates the
406     * firstWordTime and finishTime fields of the packet object.
407     * Returns the tick at which the packet header is completed (which
408     * will be all that is sent if the target rejects the packet).
409     */
410    Tick calcPacketTiming(PacketPtr pkt);
411
412    /** Occupy the bus until until */
413    void occupyBus(Tick until);
414
415    /**
416     * Release the bus after being occupied and return to an idle
417     * state where we proceed to send a retry to any potential waiting
418     * port, or drain if asked to do so.
419     */
420    void releaseBus();
421
422    /**
423     * Send a retry to the port at the head of the retryList. The
424     * caller must ensure that the list is not empty.
425     */
426    void retryWaiting();
427
428    /** Ask everyone on the bus what their size is
429     * @param id id of the busport that made the request
430     * @return the max of all the sizes
431     */
432    unsigned findBlockSize(int id);
433
434    // event used to schedule a release of the bus
435    EventWrapper<Bus, &Bus::releaseBus> busIdleEvent;
436
437    bool inRetry;
438    std::set<int> inRecvRangeChange;
439
440    /** The master and slave ports of the bus */
441    std::vector<BusSlavePort*> slavePorts;
442    std::vector<BusMasterPort*> masterPorts;
443
444    /** An array of pointers to ports that retry should be called on because the
445     * original send failed for whatever reason.*/
446    std::list<Port*> retryList;
447
448    void addToRetryList(Port* port)
449    {
450        if (!inRetry) {
451            // The device wasn't retrying a packet, or wasn't at an
452            // appropriate time.
453            retryList.push_back(port);
454        } else {
455            if (!retryList.empty() && port == retryList.front()) {
456                // The device was retrying a packet. It didn't work,
457                // so we'll leave it at the head of the retry list.
458                inRetry = false;
459            } else {
460                // We are in retry, but not for this port, put it at
461                // the end.
462                retryList.push_back(port);
463            }
464        }
465    }
466
467    /** Port that handles requests that don't match any of the interfaces.*/
468    short defaultPortId;
469
470    /** A symbolic name for a port id that denotes no port. */
471    static const short INVALID_PORT_ID = -1;
472
473    /** If true, use address range provided by default device.  Any
474       address not handled by another port and not in default device's
475       range will cause a fatal error.  If false, just send all
476       addresses not handled by another port to default device. */
477    bool useDefaultRange;
478
479    unsigned defaultBlockSize;
480    unsigned cachedBlockSize;
481    bool cachedBlockSizeValid;
482
483  public:
484
485    /** A function used to return the port associated with this bus object. */
486    virtual MasterPort& getMasterPort(const std::string& if_name, int idx = -1);
487    virtual SlavePort& getSlavePort(const std::string& if_name, int idx = -1);
488
489    virtual void init();
490    virtual void startup();
491
492    unsigned int drain(Event *de);
493
494    Bus(const BusParams *p);
495};
496
497#endif //__MEM_BUS_HH__
498