f7f69e08efbfc552a89414148fc26a591b3bcdbf
[gem5.git] / src / mem / bus.hh
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 */
44
45 /**
46 * @file
47 * Declaration of a bus object.
48 */
49
50 #ifndef __MEM_BUS_HH__
51 #define __MEM_BUS_HH__
52
53 #include <list>
54 #include <set>
55 #include <string>
56
57 #include "base/hashmap.hh"
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 "mem/request.hh"
65 #include "params/Bus.hh"
66 #include "sim/eventq.hh"
67
68 class Bus : public MemObject
69 {
70 /** Declaration of the buses port type, one will be instantiated for each
71 of the interfaces connecting to the bus. */
72 class BusPort : public Port
73 {
74 /** A pointer to the bus to which this port belongs. */
75 Bus *bus;
76
77 /** A id to keep track of the intercafe ID this port is connected to. */
78 int id;
79
80 public:
81
82 /** Constructor for the BusPort.*/
83 BusPort(const std::string &_name, Bus *_bus, int _id)
84 : Port(_name, _bus), bus(_bus), id(_id)
85 { }
86
87 int getId() const { return id; }
88
89 /**
90 * Determine if this port should be considered a snooper. This
91 * is determined by the bus.
92 *
93 * @return a boolean that is true if this port is snooping
94 */
95 virtual bool isSnooping()
96 { return bus->isSnooping(id); }
97
98 protected:
99
100 /** When reciving a timing request from the peer port (at id),
101 pass it to the bus. */
102 virtual bool recvTiming(PacketPtr pkt)
103 { pkt->setSrc(id); return bus->recvTiming(pkt); }
104
105 /** When reciving a Atomic requestfrom the peer port (at id),
106 pass it to the bus. */
107 virtual Tick recvAtomic(PacketPtr pkt)
108 { pkt->setSrc(id); return bus->recvAtomic(pkt); }
109
110 /** When reciving a Functional requestfrom the peer port (at id),
111 pass it to the bus. */
112 virtual void recvFunctional(PacketPtr pkt)
113 { pkt->setSrc(id); bus->recvFunctional(pkt); }
114
115 /** When reciving a range change from the peer port (at id),
116 pass it to the bus. */
117 virtual void recvRangeChange()
118 { bus->recvRangeChange(id); }
119
120 /** When reciving a retry from the peer port (at id),
121 pass it to the bus. */
122 virtual void recvRetry()
123 { bus->recvRetry(id); }
124
125 // This should return all the 'owned' addresses that are
126 // downstream from this bus, yes? That is, the union of all
127 // the 'owned' address ranges of all the other interfaces on
128 // this bus...
129 virtual AddrRangeList getAddrRanges()
130 { return bus->getAddrRanges(id); }
131
132 // Ask the bus to ask everyone on the bus what their block size is and
133 // take the max of it. This might need to be changed a bit if we ever
134 // support multiple block sizes.
135 virtual unsigned deviceBlockSize() const
136 { return bus->findBlockSize(id); }
137
138 };
139
140 class BusFreeEvent : public Event
141 {
142 Bus * bus;
143
144 public:
145 BusFreeEvent(Bus * _bus);
146 void process();
147 const char *description() const;
148 };
149
150 /** a globally unique id for this bus. */
151 int busId;
152 /** the clock speed for the bus */
153 int clock;
154 /** cycles of overhead per transaction */
155 int headerCycles;
156 /** the width of the bus in bytes */
157 int width;
158 /** the next tick at which the bus will be idle */
159 Tick tickNextIdle;
160
161 Event * drainEvent;
162
163 typedef range_map<Addr,int>::iterator PortIter;
164 range_map<Addr, int> portMap;
165
166 AddrRangeList defaultRange;
167
168 typedef std::vector<BusPort*>::iterator SnoopIter;
169 std::vector<BusPort*> snoopPorts;
170
171 /** Function called by the port when the bus is recieving a Timing
172 transaction.*/
173 bool recvTiming(PacketPtr pkt);
174
175 /** Function called by the port when the bus is recieving a Atomic
176 transaction.*/
177 Tick recvAtomic(PacketPtr pkt);
178
179 /** Function called by the port when the bus is recieving a Functional
180 transaction.*/
181 void recvFunctional(PacketPtr pkt);
182
183 /** Timing function called by port when it is once again able to process
184 * requests. */
185 void recvRetry(int id);
186
187 /** Function called by the port when the bus is recieving a range change.*/
188 void recvRangeChange(int id);
189
190 /** Find which port connected to this bus (if any) should be given a packet
191 * with this address.
192 * @param addr Address to find port for.
193 * @return id of port that the packet should be sent out of.
194 */
195 int findPort(Addr addr);
196
197 // Cache for the findPort function storing recently used ports from portMap
198 struct PortCache {
199 bool valid;
200 int id;
201 Addr start;
202 Addr end;
203 };
204
205 PortCache portCache[3];
206
207 // Checks the cache and returns the id of the port that has the requested
208 // address within its range
209 inline int checkPortCache(Addr addr) {
210 if (portCache[0].valid && addr >= portCache[0].start &&
211 addr < portCache[0].end) {
212 return portCache[0].id;
213 }
214 if (portCache[1].valid && addr >= portCache[1].start &&
215 addr < portCache[1].end) {
216 return portCache[1].id;
217 }
218 if (portCache[2].valid && addr >= portCache[2].start &&
219 addr < portCache[2].end) {
220 return portCache[2].id;
221 }
222
223 return INVALID_PORT_ID;
224 }
225
226 // Clears the earliest entry of the cache and inserts a new port entry
227 inline void updatePortCache(short id, Addr start, Addr end) {
228 portCache[2].valid = portCache[1].valid;
229 portCache[2].id = portCache[1].id;
230 portCache[2].start = portCache[1].start;
231 portCache[2].end = portCache[1].end;
232
233 portCache[1].valid = portCache[0].valid;
234 portCache[1].id = portCache[0].id;
235 portCache[1].start = portCache[0].start;
236 portCache[1].end = portCache[0].end;
237
238 portCache[0].valid = true;
239 portCache[0].id = id;
240 portCache[0].start = start;
241 portCache[0].end = end;
242 }
243
244 // Clears the cache. Needs to be called in constructor.
245 inline void clearPortCache() {
246 portCache[2].valid = false;
247 portCache[1].valid = false;
248 portCache[0].valid = false;
249 }
250
251 /**
252 * Return the address ranges this port is responsible for.
253 *
254 * @param id id of the bus port that made the request
255 *
256 * @return a list of non-overlapping address ranges
257 */
258 AddrRangeList getAddrRanges(int id);
259
260 /**
261 * Determine if the bus port is snooping or not.
262 *
263 * @param id id of the bus port that made the request
264 *
265 * @return a boolean indicating if this port is snooping or not
266 */
267 bool isSnooping(int id);
268
269 /** Calculate the timing parameters for the packet. Updates the
270 * firstWordTime and finishTime fields of the packet object.
271 * Returns the tick at which the packet header is completed (which
272 * will be all that is sent if the target rejects the packet).
273 */
274 Tick calcPacketTiming(PacketPtr pkt);
275
276 /** Occupy the bus until until */
277 void occupyBus(Tick until);
278
279 /** Ask everyone on the bus what their size is
280 * @param id id of the busport that made the request
281 * @return the max of all the sizes
282 */
283 unsigned findBlockSize(int id);
284
285 BusFreeEvent busIdle;
286
287 bool inRetry;
288 std::set<int> inRecvRangeChange;
289
290 /** An ordered vector of pointers to the peer port interfaces
291 connected to this bus.*/
292 std::vector<BusPort*> interfaces;
293
294 /** An array of pointers to ports that retry should be called on because the
295 * original send failed for whatever reason.*/
296 std::list<Port*> retryList;
297
298 void addToRetryList(Port* port)
299 {
300 if (!inRetry) {
301 // The device wasn't retrying a packet, or wasn't at an
302 // appropriate time.
303 retryList.push_back(port);
304 } else {
305 if (!retryList.empty() && port == retryList.front()) {
306 // The device was retrying a packet. It didn't work,
307 // so we'll leave it at the head of the retry list.
308 inRetry = false;
309 } else {
310 // We are in retry, but not for this port, put it at
311 // the end.
312 retryList.push_back(port);
313 }
314 }
315 }
316
317 /** Port that handles requests that don't match any of the interfaces.*/
318 short defaultPortId;
319
320 /** A symbolic name for a port id that denotes no port. */
321 static const short INVALID_PORT_ID = -1;
322
323 /** If true, use address range provided by default device. Any
324 address not handled by another port and not in default device's
325 range will cause a fatal error. If false, just send all
326 addresses not handled by another port to default device. */
327 bool useDefaultRange;
328
329 unsigned defaultBlockSize;
330 unsigned cachedBlockSize;
331 bool cachedBlockSizeValid;
332
333 public:
334
335 /** A function used to return the port associated with this bus object. */
336 virtual Port *getPort(const std::string &if_name, int idx = -1);
337
338 virtual void init();
339 virtual void startup();
340
341 unsigned int drain(Event *de);
342
343 Bus(const BusParams *p);
344 };
345
346 #endif //__MEM_BUS_HH__