eon is a tru64 regression, not a linux one
[gem5.git] / src / mem / bus.hh
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
53 class Bus : public MemObject
54 {
55 /** a globally unique id for this bus. */
56 int busId;
57 /** the clock speed for the bus */
58 int clock;
59 /** the width of the bus in bytes */
60 int width;
61 /** the next tick at which the bus will be idle */
62 Tick tickNextIdle;
63
64 Event * drainEvent;
65
66 static const int defaultId = -3; //Make it unique from Broadcast
67
68 struct DevMap {
69 int portId;
70 Range<Addr> range;
71 };
72 range_map<Addr, int> portMap;
73 AddrRangeList defaultRange;
74 std::vector<DevMap> portSnoopList;
75
76 /** Function called by the port when the bus is recieving a Timing
77 transaction.*/
78 bool recvTiming(PacketPtr pkt);
79
80 /** Function called by the port when the bus is recieving a Atomic
81 transaction.*/
82 Tick recvAtomic(PacketPtr pkt);
83
84 /** Function called by the port when the bus is recieving a Functional
85 transaction.*/
86 void recvFunctional(PacketPtr pkt);
87
88 /** Timing function called by port when it is once again able to process
89 * requests. */
90 void recvRetry(int id);
91
92 /** Function called by the port when the bus is recieving a status change.*/
93 void recvStatusChange(Port::Status status, int id);
94
95 /** Find which port connected to this bus (if any) should be given a packet
96 * with this address.
97 * @param addr Address to find port for.
98 * @param id Id of the port this packet was received from (to prevent
99 * loops)
100 * @return pointer to port that the packet should be sent out of.
101 */
102 Port *findPort(Addr addr, int id);
103
104 /** Find all ports with a matching snoop range, except src port. Keep in mind
105 * that the ranges shouldn't overlap or you will get a double snoop to the same
106 * interface.and the cache will assert out.
107 * @param addr Address to find snoop prts for.
108 * @param id Id of the src port of the request to avoid calling snoop on src
109 * @return vector of IDs to snoop on
110 */
111 std::vector<int> findSnoopPorts(Addr addr, int id);
112
113 /** Snoop all relevant ports atomicly. */
114 Tick atomicSnoop(PacketPtr pkt, Port* responder);
115
116 /** Snoop all relevant ports functionally. */
117 void functionalSnoop(PacketPtr pkt, Port *responder);
118
119 /** Call snoop on caches, be sure to set SNOOP_COMMIT bit if you want
120 * the snoop to happen
121 * @return True if succeds.
122 */
123 bool timingSnoop(PacketPtr pkt, Port *responder);
124
125 /** Process address range request.
126 * @param resp addresses that we can respond to
127 * @param snoop addresses that we would like to snoop
128 * @param id ide of the busport that made the request.
129 */
130 void addressRanges(AddrRangeList &resp, AddrRangeList &snoop, int id);
131
132 /** Occupy the bus with transmitting the packet pkt */
133 void occupyBus(PacketPtr pkt);
134
135 /** Declaration of the buses port type, one will be instantiated for each
136 of the interfaces connecting to the bus. */
137 class BusPort : public Port
138 {
139 bool _onRetryList;
140
141 /** A pointer to the bus to which this port belongs. */
142 Bus *bus;
143
144 /** A id to keep track of the intercafe ID this port is connected to. */
145 int id;
146
147 public:
148
149 /** Constructor for the BusPort.*/
150 BusPort(const std::string &_name, Bus *_bus, int _id)
151 : Port(_name, _bus), _onRetryList(false), bus(_bus), id(_id)
152 { }
153
154 bool onRetryList()
155 { return _onRetryList; }
156
157 void onRetryList(bool newVal)
158 { _onRetryList = newVal; }
159
160 int getId() { return id; }
161
162 protected:
163
164 /** When reciving a timing request from the peer port (at id),
165 pass it to the bus. */
166 virtual bool recvTiming(PacketPtr pkt)
167 { pkt->setSrc(id); return bus->recvTiming(pkt); }
168
169 /** When reciving a Atomic requestfrom the peer port (at id),
170 pass it to the bus. */
171 virtual Tick recvAtomic(PacketPtr pkt)
172 { pkt->setSrc(id); return bus->recvAtomic(pkt); }
173
174 /** When reciving a Functional requestfrom the peer port (at id),
175 pass it to the bus. */
176 virtual void recvFunctional(PacketPtr pkt)
177 { pkt->setSrc(id); bus->recvFunctional(pkt); }
178
179 /** When reciving a status changefrom the peer port (at id),
180 pass it to the bus. */
181 virtual void recvStatusChange(Status status)
182 { bus->recvStatusChange(status, id); }
183
184 /** When reciving a retry from the peer port (at id),
185 pass it to the bus. */
186 virtual void recvRetry()
187 { bus->recvRetry(id); }
188
189 // This should return all the 'owned' addresses that are
190 // downstream from this bus, yes? That is, the union of all
191 // the 'owned' address ranges of all the other interfaces on
192 // this bus...
193 virtual void getDeviceAddressRanges(AddrRangeList &resp,
194 AddrRangeList &snoop)
195 { bus->addressRanges(resp, snoop, id); }
196
197 // Hack to make translating port work without changes
198 virtual int deviceBlockSize() { return 32; }
199
200 };
201
202 class BusFreeEvent : public Event
203 {
204 Bus * bus;
205
206 public:
207 BusFreeEvent(Bus * _bus);
208 void process();
209 const char *description();
210 };
211
212 BusFreeEvent busIdle;
213
214 bool inRetry;
215
216 /** max number of bus ids we've handed out so far */
217 short maxId;
218
219 /** An array of pointers to the peer port interfaces
220 connected to this bus.*/
221 m5::hash_map<short,BusPort*> interfaces;
222
223 /** An array of pointers to ports that retry should be called on because the
224 * original send failed for whatever reason.*/
225 std::list<BusPort*> retryList;
226
227 void addToRetryList(BusPort * port)
228 {
229 if (!inRetry) {
230 // The device wasn't retrying a packet, or wasn't at an appropriate
231 // time.
232 assert(!port->onRetryList());
233 port->onRetryList(true);
234 retryList.push_back(port);
235 } else {
236 if (port->onRetryList()) {
237 // The device was retrying a packet. It didn't work, so we'll leave
238 // it at the head of the retry list.
239 assert(port == retryList.front());
240 inRetry = false;
241 }
242 else {
243 port->onRetryList(true);
244 retryList.push_back(port);
245 }
246 }
247 }
248
249 /** Port that handles requests that don't match any of the interfaces.*/
250 BusPort *defaultPort;
251
252 /** Has the user specified their own default responder? */
253 bool responderSet;
254
255 public:
256
257 /** A function used to return the port associated with this bus object. */
258 virtual Port *getPort(const std::string &if_name, int idx = -1);
259 virtual void deletePortRefs(Port *p);
260
261 virtual void init();
262
263 unsigned int drain(Event *de);
264
265 Bus(const std::string &n, int bus_id, int _clock, int _width,
266 bool responder_set)
267 : MemObject(n), busId(bus_id), clock(_clock), width(_width),
268 tickNextIdle(0), drainEvent(NULL), busIdle(this), inRetry(false),
269 maxId(0), defaultPort(NULL), responderSet(responder_set)
270 {
271 //Both the width and clock period must be positive
272 if (width <= 0)
273 fatal("Bus width must be positive\n");
274 if (clock <= 0)
275 fatal("Bus clock period must be positive\n");
276 }
277
278 };
279
280 #endif //__MEM_BUS_HH__