2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Ron Dreslinski
34 * Declaration of a bus object.
37 #ifndef __MEM_BUS_HH__
38 #define __MEM_BUS_HH__
45 #include "base/range.hh"
46 #include "base/hashmap.hh"
47 #include "base/range_map.hh"
48 #include "mem/mem_object.hh"
49 #include "mem/packet.hh"
50 #include "mem/port.hh"
51 #include "mem/request.hh"
52 #include "sim/eventq.hh"
53 #include "params/Bus.hh"
55 class Bus : public MemObject
57 /** Declaration of the buses port type, one will be instantiated for each
58 of the interfaces connecting to the bus. */
59 class BusPort : public Port
63 /** A pointer to the bus to which this port belongs. */
66 /** A id to keep track of the intercafe ID this port is connected to. */
71 /** Constructor for the BusPort.*/
72 BusPort(const std::string &_name, Bus *_bus, int _id)
73 : Port(_name, _bus), _onRetryList(false), bus(_bus), id(_id)
77 { return _onRetryList; }
79 void onRetryList(bool newVal)
80 { _onRetryList = newVal; }
82 int getId() { return id; }
86 /** When reciving a timing request from the peer port (at id),
87 pass it to the bus. */
88 virtual bool recvTiming(PacketPtr pkt)
89 { pkt->setSrc(id); return bus->recvTiming(pkt); }
91 /** When reciving a Atomic requestfrom the peer port (at id),
92 pass it to the bus. */
93 virtual Tick recvAtomic(PacketPtr pkt)
94 { pkt->setSrc(id); return bus->recvAtomic(pkt); }
96 /** When reciving a Functional requestfrom the peer port (at id),
97 pass it to the bus. */
98 virtual void recvFunctional(PacketPtr pkt)
99 { pkt->setSrc(id); bus->recvFunctional(pkt); }
101 /** When reciving a status changefrom the peer port (at id),
102 pass it to the bus. */
103 virtual void recvStatusChange(Status status)
104 { bus->recvStatusChange(status, id); }
106 /** When reciving a retry from the peer port (at id),
107 pass it to the bus. */
108 virtual void recvRetry()
109 { bus->recvRetry(id); }
111 // This should return all the 'owned' addresses that are
112 // downstream from this bus, yes? That is, the union of all
113 // the 'owned' address ranges of all the other interfaces on
115 virtual void getDeviceAddressRanges(AddrRangeList &resp,
117 { bus->addressRanges(resp, snoop, id); }
119 // Ask the bus to ask everyone on the bus what their block size is and
120 // take the max of it. This might need to be changed a bit if we ever
121 // support multiple block sizes.
122 virtual int deviceBlockSize()
123 { return bus->findBlockSize(id); }
127 class BusFreeEvent : public Event
132 BusFreeEvent(Bus * _bus);
134 const char *description() const;
137 /** a globally unique id for this bus. */
139 /** the clock speed for the bus */
141 /** cycles of overhead per transaction */
143 /** the width of the bus in bytes */
145 /** the next tick at which the bus will be idle */
151 static const int defaultId = -3; //Make it unique from Broadcast
153 typedef range_map<Addr,int>::iterator PortIter;
154 range_map<Addr, int> portMap;
156 AddrRangeList defaultRange;
158 typedef std::vector<BusPort*>::iterator SnoopIter;
159 std::vector<BusPort*> snoopPorts;
161 /** Function called by the port when the bus is recieving a Timing
163 bool recvTiming(PacketPtr pkt);
165 /** Function called by the port when the bus is recieving a Atomic
167 Tick recvAtomic(PacketPtr pkt);
169 /** Function called by the port when the bus is recieving a Functional
171 void recvFunctional(PacketPtr pkt);
173 /** Timing function called by port when it is once again able to process
175 void recvRetry(int id);
177 /** Function called by the port when the bus is recieving a status change.*/
178 void recvStatusChange(Port::Status status, int id);
180 /** Find which port connected to this bus (if any) should be given a packet
182 * @param addr Address to find port for.
183 * @return id of port that the packet should be sent out of.
185 int findPort(Addr addr);
187 // Cache for the findPort function storing recently used ports from portMap
195 PortCache portCache[3];
197 // Checks the cache and returns the id of the port that has the requested
198 // address within its range
199 inline int checkPortCache(Addr addr) {
200 if (portCache[0].valid && addr >= portCache[0].start &&
201 addr < portCache[0].end) {
202 return portCache[0].id;
204 if (portCache[1].valid && addr >= portCache[1].start &&
205 addr < portCache[1].end) {
206 return portCache[1].id;
208 if (portCache[2].valid && addr >= portCache[2].start &&
209 addr < portCache[2].end) {
210 return portCache[2].id;
216 // Clears the earliest entry of the cache and inserts a new port entry
217 inline void updatePortCache(short id, Addr start, Addr end) {
218 portCache[2].valid = portCache[1].valid;
219 portCache[2].id = portCache[1].id;
220 portCache[2].start = portCache[1].start;
221 portCache[2].end = portCache[1].end;
223 portCache[1].valid = portCache[0].valid;
224 portCache[1].id = portCache[0].id;
225 portCache[1].start = portCache[0].start;
226 portCache[1].end = portCache[0].end;
228 portCache[0].valid = true;
229 portCache[0].id = id;
230 portCache[0].start = start;
231 portCache[0].end = end;
234 // Clears the cache. Needs to be called in constructor.
235 inline void clearPortCache() {
236 portCache[2].valid = false;
237 portCache[1].valid = false;
238 portCache[0].valid = false;
241 /** Process address range request.
242 * @param resp addresses that we can respond to
243 * @param snoop addresses that we would like to snoop
244 * @param id ide of the busport that made the request.
246 void addressRanges(AddrRangeList &resp, bool &snoop, int id);
248 /** Calculate the timing parameters for the packet. Updates the
249 * firstWordTime and finishTime fields of the packet object.
250 * Returns the tick at which the packet header is completed (which
251 * will be all that is sent if the target rejects the packet).
253 Tick calcPacketTiming(PacketPtr pkt);
255 /** Occupy the bus until until */
256 void occupyBus(Tick until);
258 /** Ask everyone on the bus what their size is
259 * @param id id of the busport that made the request
260 * @return the max of all the sizes
262 int findBlockSize(int id);
264 BusFreeEvent busIdle;
267 std::set<int> inRecvStatusChange;
269 /** max number of bus ids we've handed out so far */
272 /** An array of pointers to the peer port interfaces
273 connected to this bus.*/
274 m5::hash_map<short,BusPort*> interfaces;
276 /** An array of pointers to ports that retry should be called on because the
277 * original send failed for whatever reason.*/
278 std::list<BusPort*> retryList;
280 void addToRetryList(BusPort * port)
283 // The device wasn't retrying a packet, or wasn't at an appropriate
285 assert(!port->onRetryList());
286 port->onRetryList(true);
287 retryList.push_back(port);
289 if (port->onRetryList()) {
290 // The device was retrying a packet. It didn't work, so we'll leave
291 // it at the head of the retry list.
292 assert(port == retryList.front());
296 port->onRetryList(true);
297 retryList.push_back(port);
302 /** Port that handles requests that don't match any of the interfaces.*/
303 BusPort *defaultPort;
308 /** Has the user specified their own default responder? */
311 int defaultBlockSize;
313 bool cachedBlockSizeValid;
315 // Cache for the peer port interfaces
322 BusCache busCache[3];
324 // Checks the peer port interfaces cache for the port id and returns
325 // a pointer to the matching port
326 inline BusPort* checkBusCache(short id) {
327 if (busCache[0].valid && id == busCache[0].id) {
328 return busCache[0].port;
330 if (busCache[1].valid && id == busCache[1].id) {
331 return busCache[1].port;
333 if (busCache[2].valid && id == busCache[2].id) {
334 return busCache[2].port;
340 // Replaces the earliest entry in the cache with a new entry
341 inline void updateBusCache(short id, BusPort *port) {
342 busCache[2].valid = busCache[1].valid;
343 busCache[2].id = busCache[1].id;
344 busCache[2].port = busCache[1].port;
346 busCache[1].valid = busCache[0].valid;
347 busCache[1].id = busCache[0].id;
348 busCache[1].port = busCache[0].port;
350 busCache[0].valid = true;
352 busCache[0].port = port;
355 // Invalidates the cache. Needs to be called in constructor.
356 inline void clearBusCache() {
357 busCache[2].valid = false;
358 busCache[1].valid = false;
359 busCache[0].valid = false;
365 /** A function used to return the port associated with this bus object. */
366 virtual Port *getPort(const std::string &if_name, int idx = -1);
367 virtual void deletePortRefs(Port *p);
370 virtual void startup();
372 unsigned int drain(Event *de);
374 Bus(const BusParams *p)
375 : MemObject(p), busId(p->bus_id), clock(p->clock),
376 headerCycles(p->header_cycles), width(p->width), tickNextIdle(0),
377 drainEvent(NULL), busIdle(this), inRetry(false), maxId(0),
378 defaultPort(NULL), funcPort(NULL), funcPortId(-4),
379 responderSet(p->responder_set), defaultBlockSize(p->block_size),
380 cachedBlockSize(0), cachedBlockSizeValid(false)
382 //width, clock period, and header cycles must be positive
384 fatal("Bus width must be positive\n");
386 fatal("Bus clock period must be positive\n");
387 if (headerCycles <= 0)
388 fatal("Number of header cycles must be positive\n");
395 #endif //__MEM_BUS_HH__