xbar.hh revision 4894
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Ron Dreslinski
29 *          Ali Saidi
30 */
31
32/**
33 * @file
34 * Declaration of a bus object.
35 */
36
37#ifndef __MEM_BUS_HH__
38#define __MEM_BUS_HH__
39
40#include <string>
41#include <list>
42#include <inttypes.h>
43
44#include "base/range.hh"
45#include "base/hashmap.hh"
46#include "base/range_map.hh"
47#include "mem/mem_object.hh"
48#include "mem/packet.hh"
49#include "mem/port.hh"
50#include "mem/request.hh"
51#include "sim/eventq.hh"
52
53class Bus : public MemObject
54{
55    /** Declaration of the buses port type, one will be instantiated for each
56        of the interfaces connecting to the bus. */
57    class BusPort : public Port
58    {
59        bool _onRetryList;
60
61        /** A pointer to the bus to which this port belongs. */
62        Bus *bus;
63
64        /** A id to keep track of the intercafe ID this port is connected to. */
65        int id;
66
67      public:
68
69        /** Constructor for the BusPort.*/
70        BusPort(const std::string &_name, Bus *_bus, int _id)
71            : Port(_name, _bus), _onRetryList(false), bus(_bus), id(_id)
72        { }
73
74        bool onRetryList()
75        { return _onRetryList; }
76
77        void onRetryList(bool newVal)
78        { _onRetryList = newVal; }
79
80        int getId() { return id; }
81
82      protected:
83
84        /** When reciving a timing request from the peer port (at id),
85            pass it to the bus. */
86        virtual bool recvTiming(PacketPtr pkt)
87        { pkt->setSrc(id); return bus->recvTiming(pkt); }
88
89        /** When reciving a Atomic requestfrom the peer port (at id),
90            pass it to the bus. */
91        virtual Tick recvAtomic(PacketPtr pkt)
92        { pkt->setSrc(id); return bus->recvAtomic(pkt); }
93
94        /** When reciving a Functional requestfrom the peer port (at id),
95            pass it to the bus. */
96        virtual void recvFunctional(PacketPtr pkt)
97        { pkt->setSrc(id); bus->recvFunctional(pkt); }
98
99        /** When reciving a status changefrom the peer port (at id),
100            pass it to the bus. */
101        virtual void recvStatusChange(Status status)
102        { bus->recvStatusChange(status, id); }
103
104        /** When reciving a retry from the peer port (at id),
105            pass it to the bus. */
106        virtual void recvRetry()
107        { bus->recvRetry(id); }
108
109        // This should return all the 'owned' addresses that are
110        // downstream from this bus, yes?  That is, the union of all
111        // the 'owned' address ranges of all the other interfaces on
112        // this bus...
113        virtual void getDeviceAddressRanges(AddrRangeList &resp,
114                                            bool &snoop)
115        { bus->addressRanges(resp, snoop, id); }
116
117        // Ask the bus to ask everyone on the bus what their block size is and
118        // take the max of it. This might need to be changed a bit if we ever
119        // support multiple block sizes.
120        virtual int deviceBlockSize()
121        { return bus->findBlockSize(id); }
122
123    };
124
125    class BusFreeEvent : public Event
126    {
127        Bus * bus;
128
129      public:
130        BusFreeEvent(Bus * _bus);
131        void process();
132        const char *description();
133    };
134
135    /** a globally unique id for this bus. */
136    int busId;
137    /** the clock speed for the bus */
138    int clock;
139    /** the width of the bus in bytes */
140    int width;
141    /** the next tick at which the bus will be idle */
142    Tick tickNextIdle;
143
144    Event * drainEvent;
145
146
147    static const int defaultId = -3; //Make it unique from Broadcast
148
149    typedef range_map<Addr,int>::iterator PortIter;
150    range_map<Addr, int> portMap;
151
152    AddrRangeList defaultRange;
153
154    typedef std::vector<BusPort*>::iterator SnoopIter;
155    std::vector<BusPort*> snoopPorts;
156
157    /** Function called by the port when the bus is recieving a Timing
158      transaction.*/
159    bool recvTiming(PacketPtr pkt);
160
161    /** Function called by the port when the bus is recieving a Atomic
162      transaction.*/
163    Tick recvAtomic(PacketPtr pkt);
164
165    /** Function called by the port when the bus is recieving a Functional
166        transaction.*/
167    void recvFunctional(PacketPtr pkt);
168
169    /** Timing function called by port when it is once again able to process
170     * requests. */
171    void recvRetry(int id);
172
173    /** Function called by the port when the bus is recieving a status change.*/
174    void recvStatusChange(Port::Status status, int id);
175
176    /** Find which port connected to this bus (if any) should be given a packet
177     * with this address.
178     * @param addr Address to find port for.
179     * @return id of port that the packet should be sent out of.
180     */
181    int findPort(Addr addr);
182
183    /** Snoop all relevant ports functionally. */
184    void functionalSnoop(PacketPtr pkt, Port *responder);
185
186    /** Call snoop on caches, be sure to set SNOOP_COMMIT bit if you want
187     * the snoop to happen
188     * @return True if succeds.
189     */
190    bool timingSnoop(PacketPtr pkt, Port *responder);
191
192    /** Process address range request.
193     * @param resp addresses that we can respond to
194     * @param snoop addresses that we would like to snoop
195     * @param id ide of the busport that made the request.
196     */
197    void addressRanges(AddrRangeList &resp, bool &snoop, int id);
198
199    /** Occupy the bus with transmitting the packet pkt */
200    void occupyBus(PacketPtr pkt);
201
202    /** Ask everyone on the bus what their size is
203     * @param id id of the busport that made the request
204     * @return the max of all the sizes
205     */
206    int findBlockSize(int id);
207
208    BusFreeEvent busIdle;
209
210    bool inRetry;
211
212    /** max number of bus ids we've handed out so far */
213    short maxId;
214
215    /** An array of pointers to the peer port interfaces
216        connected to this bus.*/
217    m5::hash_map<short,BusPort*> interfaces;
218
219    /** An array of pointers to ports that retry should be called on because the
220     * original send failed for whatever reason.*/
221    std::list<BusPort*> retryList;
222
223    void addToRetryList(BusPort * port)
224    {
225        if (!inRetry) {
226            // The device wasn't retrying a packet, or wasn't at an appropriate
227            // time.
228            assert(!port->onRetryList());
229            port->onRetryList(true);
230            retryList.push_back(port);
231        } else {
232            if (port->onRetryList()) {
233                // The device was retrying a packet. It didn't work, so we'll leave
234                // it at the head of the retry list.
235                assert(port == retryList.front());
236                inRetry = false;
237            }
238            else {
239                port->onRetryList(true);
240                retryList.push_back(port);
241            }
242        }
243    }
244
245    /** Port that handles requests that don't match any of the interfaces.*/
246    BusPort *defaultPort;
247
248    BusPort *funcPort;
249    int funcPortId;
250
251    /** Has the user specified their own default responder? */
252    bool responderSet;
253
254    int defaultBlockSize;
255    int cachedBlockSize;
256    bool cachedBlockSizeValid;
257
258  public:
259
260    /** A function used to return the port associated with this bus object. */
261    virtual Port *getPort(const std::string &if_name, int idx = -1);
262    virtual void deletePortRefs(Port *p);
263
264    virtual void init();
265    virtual void startup();
266
267    unsigned int drain(Event *de);
268
269    Bus(const std::string &n, int bus_id, int _clock, int _width,
270        bool responder_set, int dflt_blk_size)
271        : MemObject(n), busId(bus_id), clock(_clock), width(_width),
272          tickNextIdle(0), drainEvent(NULL), busIdle(this), inRetry(false),
273          maxId(0), defaultPort(NULL), funcPort(NULL), funcPortId(-4),
274          responderSet(responder_set), defaultBlockSize(dflt_blk_size),
275          cachedBlockSize(0), cachedBlockSizeValid(false)
276    {
277        //Both the width and clock period must be positive
278        if (width <= 0)
279            fatal("Bus width must be positive\n");
280        if (clock <= 0)
281            fatal("Bus clock period must be positive\n");
282    }
283
284};
285
286#endif //__MEM_BUS_HH__
287