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/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"
68 class Bus : public MemObject
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
74 /** A pointer to the bus to which this port belongs. */
77 /** A id to keep track of the intercafe ID this port is connected to. */
82 /** Constructor for the BusPort.*/
83 BusPort(const std::string &_name, Bus *_bus, int _id)
84 : Port(_name, _bus), bus(_bus), id(_id)
87 int getId() const { return id; }
90 * Determine if this port should be considered a snooper. This
91 * is determined by the bus.
93 * @return a boolean that is true if this port is snooping
95 virtual bool isSnooping()
96 { return bus->isSnooping(id); }
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); }
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); }
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); }
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); }
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); }
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
129 virtual AddrRangeList getAddrRanges()
130 { return bus->getAddrRanges(id); }
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); }
140 class BusFreeEvent : public Event
145 BusFreeEvent(Bus * _bus);
147 const char *description() const;
150 /** a globally unique id for this bus. */
152 /** the clock speed for the bus */
154 /** cycles of overhead per transaction */
156 /** the width of the bus in bytes */
158 /** the next tick at which the bus will be idle */
163 typedef range_map<Addr,int>::iterator PortIter;
164 range_map<Addr, int> portMap;
166 AddrRangeList defaultRange;
168 typedef std::vector<BusPort*>::iterator SnoopIter;
169 std::vector<BusPort*> snoopPorts;
171 /** Function called by the port when the bus is recieving a Timing
173 bool recvTiming(PacketPtr pkt);
175 /** Function called by the port when the bus is recieving a Atomic
177 Tick recvAtomic(PacketPtr pkt);
179 /** Function called by the port when the bus is recieving a Functional
181 void recvFunctional(PacketPtr pkt);
183 /** Timing function called by port when it is once again able to process
185 void recvRetry(int id);
187 /** Function called by the port when the bus is recieving a range change.*/
188 void recvRangeChange(int id);
190 /** Find which port connected to this bus (if any) should be given a packet
192 * @param addr Address to find port for.
193 * @return id of port that the packet should be sent out of.
195 int findPort(Addr addr);
197 // Cache for the findPort function storing recently used ports from portMap
205 PortCache portCache[3];
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;
214 if (portCache[1].valid && addr >= portCache[1].start &&
215 addr < portCache[1].end) {
216 return portCache[1].id;
218 if (portCache[2].valid && addr >= portCache[2].start &&
219 addr < portCache[2].end) {
220 return portCache[2].id;
223 return INVALID_PORT_ID;
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;
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;
238 portCache[0].valid = true;
239 portCache[0].id = id;
240 portCache[0].start = start;
241 portCache[0].end = end;
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;
252 * Return the address ranges this port is responsible for.
254 * @param id id of the bus port that made the request
256 * @return a list of non-overlapping address ranges
258 AddrRangeList getAddrRanges(int id);
261 * Determine if the bus port is snooping or not.
263 * @param id id of the bus port that made the request
265 * @return a boolean indicating if this port is snooping or not
267 bool isSnooping(int id);
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).
274 Tick calcPacketTiming(PacketPtr pkt);
276 /** Occupy the bus until until */
277 void occupyBus(Tick until);
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
283 unsigned findBlockSize(int id);
285 BusFreeEvent busIdle;
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__