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