2 * Copyright (c) 2011 ARM Limited
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.
14 * Copyright (c) 2002-2005 The Regents of The University of Michigan
15 * All rights reserved.
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.
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.
40 * Authors: Ron Dreslinski
47 * Declaration of a bus object.
50 #ifndef __MEM_BUS_HH__
51 #define __MEM_BUS_HH__
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"
66 class Bus : public MemObject
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
72 /** A pointer to the bus to which this port belongs. */
75 /** A id to keep track of the intercafe ID this port is connected to. */
80 /** Constructor for the BusPort.*/
81 BusPort(const std::string &_name, Bus *_bus, int _id)
82 : Port(_name, _bus), bus(_bus), id(_id)
85 int getId() const { return id; }
88 * Determine if this port should be considered a snooper. This
89 * is determined by the bus.
91 * @return a boolean that is true if this port is snooping
93 virtual bool isSnooping()
94 { return bus->isSnooping(id); }
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); }
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); }
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); }
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); }
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); }
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
127 virtual AddrRangeList getAddrRanges()
128 { return bus->getAddrRanges(id); }
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); }
138 /** the clock speed for the bus */
140 /** cycles of overhead per transaction */
142 /** the width of the bus in bytes */
144 /** the next tick at which the bus will be idle */
149 typedef range_map<Addr,int>::iterator PortIter;
150 range_map<Addr, int> portMap;
152 AddrRangeList defaultRange;
154 typedef std::vector<BusPort*>::iterator SnoopIter;
155 std::vector<BusPort*> snoopPorts;
157 /** Function called by the port when the bus is recieving a Timing
159 bool recvTiming(PacketPtr pkt);
161 /** Function called by the port when the bus is recieving a Atomic
163 Tick recvAtomic(PacketPtr pkt);
165 /** Function called by the port when the bus is recieving a Functional
167 void recvFunctional(PacketPtr pkt);
169 /** Timing function called by port when it is once again able to process
171 void recvRetry(int id);
173 /** Function called by the port when the bus is recieving a range change.*/
174 void recvRangeChange(int id);
176 /** Find which port connected to this bus (if any) should be given a packet
178 * @param addr Address to find port for.
179 * @return id of port that the packet should be sent out of.
181 int findPort(Addr addr);
183 // Cache for the findPort function storing recently used ports from portMap
191 PortCache portCache[3];
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;
200 if (portCache[1].valid && addr >= portCache[1].start &&
201 addr < portCache[1].end) {
202 return portCache[1].id;
204 if (portCache[2].valid && addr >= portCache[2].start &&
205 addr < portCache[2].end) {
206 return portCache[2].id;
209 return INVALID_PORT_ID;
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;
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;
224 portCache[0].valid = true;
225 portCache[0].id = id;
226 portCache[0].start = start;
227 portCache[0].end = end;
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;
238 * Return the address ranges this port is responsible for.
240 * @param id id of the bus port that made the request
242 * @return a list of non-overlapping address ranges
244 AddrRangeList getAddrRanges(int id);
247 * Determine if the bus port is snooping or not.
249 * @param id id of the bus port that made the request
251 * @return a boolean indicating if this port is snooping or not
253 bool isSnooping(int id);
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).
260 Tick calcPacketTiming(PacketPtr pkt);
262 /** Occupy the bus until until */
263 void occupyBus(Tick until);
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.
273 * Send a retry to the port at the head of the retryList. The
274 * caller must ensure that the list is not empty.
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
282 unsigned findBlockSize(int id);
284 // event used to schedule a release of the bus
285 EventWrapper<Bus, &Bus::releaseBus> busIdleEvent;
288 std::set<int> inRecvRangeChange;
290 // keep track of the number of master ports (not counting the
291 // default master) since we need this as an offset into the
293 unsigned int nbrMasterPorts;
295 /** An ordered vector of pointers to the peer port interfaces
296 connected to this bus.*/
297 std::vector<BusPort*> interfaces;
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;
303 void addToRetryList(Port* port)
306 // The device wasn't retrying a packet, or wasn't at an
308 retryList.push_back(port);
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.
315 // We are in retry, but not for this port, put it at
317 retryList.push_back(port);
322 /** Port that handles requests that don't match any of the interfaces.*/
325 /** A symbolic name for a port id that denotes no port. */
326 static const short INVALID_PORT_ID = -1;
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;
334 unsigned defaultBlockSize;
335 unsigned cachedBlockSize;
336 bool cachedBlockSizeValid;
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);
344 virtual void startup();
346 unsigned int drain(Event *de);
348 Bus(const BusParams *p);
351 #endif //__MEM_BUS_HH__