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