2 * Copyright (c) 2006 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.
32 * @file Definition of a bus object.
36 #include "base/misc.hh"
37 #include "base/trace.hh"
39 #include "sim/builder.hh"
42 Bus::getPort(const std::string
&if_name
, int idx
)
44 if (if_name
== "default")
45 if (defaultPort
== NULL
) {
46 defaultPort
= new BusPort(csprintf("%s-default",name()), this,
50 fatal("Default port already set\n");
52 // if_name ignored? forced to be empty?
53 int id
= interfaces
.size();
54 BusPort
*bp
= new BusPort(csprintf("%s-p%d", name(), id
), this, id
);
55 interfaces
.push_back(bp
);
59 /** Get the ranges of anyone other buses that we are connected to. */
63 std::vector
<Port
*>::iterator intIter
;
65 for (intIter
= interfaces
.begin(); intIter
!= interfaces
.end(); intIter
++)
66 (*intIter
)->sendStatusChange(Port::RangeChange
);
70 /** Function called by the port when the bus is receiving a Timing
73 Bus::recvTiming(Packet
*pkt
)
76 DPRINTF(Bus
, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
77 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
79 short dest
= pkt
->getDest();
80 if (dest
== Packet::Broadcast
) {
81 port
= findPort(pkt
->getAddr(), pkt
->getSrc());
83 assert(dest
>= 0 && dest
< interfaces
.size());
84 assert(dest
!= pkt
->getSrc()); // catch infinite loops
85 port
= interfaces
[dest
];
87 if (port
->sendTiming(pkt
)) {
88 // packet was successfully sent, just return true.
92 // packet not successfully sent
93 retryList
.push_back(interfaces
[pkt
->getSrc()]);
98 Bus::recvRetry(int id
)
100 // Go through all the elements on the list calling sendRetry on each
101 // This is not very efficient at all but it works. Ultimately we should end
102 // up with something that is more intelligent.
103 int initialSize
= retryList
.size();
107 for (i
= 0; i
< initialSize
; i
++) {
108 assert(retryList
.size() > 0);
109 p
= retryList
.front();
110 retryList
.pop_front();
117 Bus::findPort(Addr addr
, int id
)
119 /* An interval tree would be a better way to do this. --ali. */
125 while (i
< portList
.size() && !found
)
127 if (portList
[i
].range
== addr
) {
128 dest_id
= portList
[i
].portId
;
130 DPRINTF(Bus
, " found addr 0x%llx on device %d\n", addr
, dest_id
);
135 // Check if this matches the default range
137 for (iter
= defaultRange
.begin(); iter
!= defaultRange
.end(); iter
++) {
139 DPRINTF(Bus
, " found addr 0x%llx on default\n", addr
);
143 panic("Unable to find destination for addr: %llx", addr
);
147 // we shouldn't be sending this back to where it came from
148 assert(dest_id
!= id
);
150 return interfaces
[dest_id
];
153 /** Function called by the port when the bus is receiving a Atomic
156 Bus::recvAtomic(Packet
*pkt
)
158 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
159 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
160 assert(pkt
->getDest() == Packet::Broadcast
);
161 return findPort(pkt
->getAddr(), pkt
->getSrc())->sendAtomic(pkt
);
164 /** Function called by the port when the bus is receiving a Functional
167 Bus::recvFunctional(Packet
*pkt
)
169 DPRINTF(Bus
, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
170 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
171 assert(pkt
->getDest() == Packet::Broadcast
);
172 findPort(pkt
->getAddr(), pkt
->getSrc())->sendFunctional(pkt
);
175 /** Function called by the port when the bus is receiving a status change.*/
177 Bus::recvStatusChange(Port::Status status
, int id
)
179 AddrRangeList ranges
;
180 AddrRangeList snoops
;
184 assert(status
== Port::RangeChange
&&
185 "The other statuses need to be implemented.");
187 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n", id
);
189 if (id
== defaultId
) {
190 defaultRange
.clear();
191 defaultPort
->getPeerAddressRanges(ranges
, snoops
);
192 assert(snoops
.size() == 0);
193 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
194 defaultRange
.push_back(*iter
);
195 DPRINTF(BusAddrRanges
, "Adding range %llx - %llx for default\n",
196 iter
->start
, iter
->end
);
200 assert((id
< interfaces
.size() && id
>= 0) || id
== -1);
201 Port
*port
= interfaces
[id
];
202 std::vector
<DevMap
>::iterator portIter
;
204 // Clean out any previously existent ids
205 for (portIter
= portList
.begin(); portIter
!= portList
.end(); ) {
206 if (portIter
->portId
== id
)
207 portIter
= portList
.erase(portIter
);
212 port
->getPeerAddressRanges(ranges
, snoops
);
214 // not dealing with snooping yet either
215 assert(snoops
.size() == 0);
216 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
221 DPRINTF(BusAddrRanges
, "Adding range %llx - %llx for id %d\n",
222 dm
.range
.start
, dm
.range
.end
, id
);
223 portList
.push_back(dm
);
226 DPRINTF(MMU
, "port list has %d entries\n", portList
.size());
228 // tell all our peers that our address range has changed.
229 // Don't tell the device that caused this change, it already knows
230 for (x
= 0; x
< interfaces
.size(); x
++)
232 interfaces
[x
]->sendStatusChange(Port::RangeChange
);
234 if (id
!= defaultId
&& defaultPort
)
235 defaultPort
->sendStatusChange(Port::RangeChange
);
239 Bus::addressRanges(AddrRangeList
&resp
, AddrRangeList
&snoop
, int id
)
241 std::vector
<DevMap
>::iterator portIter
;
242 AddrRangeIter dflt_iter
;
248 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
250 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
252 resp
.push_back(*dflt_iter
);
253 DPRINTF(BusAddrRanges
, " -- %#llX : %#llX\n",dflt_iter
->start
,
256 for (portIter
= portList
.begin(); portIter
!= portList
.end(); portIter
++) {
258 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
260 if ((portIter
->range
.start
< dflt_iter
->start
&&
261 portIter
->range
.end
>= dflt_iter
->start
) ||
262 (portIter
->range
.start
< dflt_iter
->end
&&
263 portIter
->range
.end
>= dflt_iter
->end
))
264 fatal("Devices can not set ranges that itersect the default set\
265 but are not a subset of the default set.\n");
266 if (portIter
->range
.start
>= dflt_iter
->start
&&
267 portIter
->range
.end
<= dflt_iter
->end
) {
269 DPRINTF(BusAddrRanges
, " -- %#llX : %#llX is a SUBSET\n",
270 portIter
->range
.start
, portIter
->range
.end
);
273 if (portIter
->portId
!= id
&& !subset
) {
274 resp
.push_back(portIter
->range
);
275 DPRINTF(BusAddrRanges
, " -- %#llX : %#llX\n",
276 portIter
->range
.start
, portIter
->range
.end
);
281 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus
)
285 END_DECLARE_SIM_OBJECT_PARAMS(Bus
)
287 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus
)
288 INIT_PARAM(bus_id
, "a globally unique bus id")
289 END_INIT_SIM_OBJECT_PARAMS(Bus
)
291 CREATE_SIM_OBJECT(Bus
)
293 return new Bus(getInstanceName(), bus_id
);
296 REGISTER_SIM_OBJECT("Bus", Bus
)