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.
33 * Definition of a bus object.
37 #include "base/misc.hh"
38 #include "base/trace.hh"
40 #include "sim/builder.hh"
43 Bus::getPort(const std::string
&if_name
, int idx
)
45 if (if_name
== "default")
46 if (defaultPort
== NULL
) {
47 defaultPort
= new BusPort(csprintf("%s-default",name()), this,
51 fatal("Default port already set\n");
53 // if_name ignored? forced to be empty?
54 int id
= interfaces
.size();
55 BusPort
*bp
= new BusPort(csprintf("%s-p%d", name(), id
), this, id
);
56 interfaces
.push_back(bp
);
60 /** Get the ranges of anyone other buses that we are connected to. */
64 std::vector
<Port
*>::iterator intIter
;
66 for (intIter
= interfaces
.begin(); intIter
!= interfaces
.end(); intIter
++)
67 (*intIter
)->sendStatusChange(Port::RangeChange
);
71 /** Function called by the port when the bus is receiving a Timing
74 Bus::recvTiming(Packet
*pkt
)
77 DPRINTF(Bus
, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
78 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
80 short dest
= pkt
->getDest();
81 if (dest
== Packet::Broadcast
) {
82 if ( timingSnoopPhase1(pkt
) )
84 timingSnoopPhase2(pkt
);
85 port
= findPort(pkt
->getAddr(), pkt
->getSrc());
89 //Snoop didn't succeed
90 retryList
.push_back(interfaces
[pkt
->getSrc()]);
94 assert(dest
>= 0 && dest
< interfaces
.size());
95 assert(dest
!= pkt
->getSrc()); // catch infinite loops
96 port
= interfaces
[dest
];
98 if (port
->sendTiming(pkt
)) {
99 // packet was successfully sent, just return true.
103 // packet not successfully sent
104 retryList
.push_back(interfaces
[pkt
->getSrc()]);
109 Bus::recvRetry(int id
)
111 // Go through all the elements on the list calling sendRetry on each
112 // This is not very efficient at all but it works. Ultimately we should end
113 // up with something that is more intelligent.
114 int initialSize
= retryList
.size();
118 for (i
= 0; i
< initialSize
; i
++) {
119 assert(retryList
.size() > 0);
120 p
= retryList
.front();
121 retryList
.pop_front();
128 Bus::findPort(Addr addr
, int id
)
130 /* An interval tree would be a better way to do this. --ali. */
136 while (i
< portList
.size() && !found
)
138 if (portList
[i
].range
== addr
) {
139 dest_id
= portList
[i
].portId
;
141 DPRINTF(Bus
, " found addr %#llx on device %d\n", addr
, dest_id
);
146 // Check if this matches the default range
148 for (iter
= defaultRange
.begin(); iter
!= defaultRange
.end(); iter
++) {
150 DPRINTF(Bus
, " found addr %#llx on default\n", addr
);
154 panic("Unable to find destination for addr: %#llx", addr
);
158 // we shouldn't be sending this back to where it came from
159 assert(dest_id
!= id
);
161 return interfaces
[dest_id
];
165 Bus::findSnoopPorts(Addr addr
, int id
)
169 std::vector
<int> ports
;
171 while (i
< portSnoopList
.size())
173 if (portSnoopList
[i
].range
== addr
&& portSnoopList
[i
].portId
!= id
) {
174 //Careful to not overlap ranges
175 //or snoop will be called more than once on the port
176 ports
.push_back(portSnoopList
[i
].portId
);
177 DPRINTF(Bus
, " found snoop addr %#llx on device%d\n", addr
,
178 portSnoopList
[i
].portId
);
186 Bus::atomicSnoop(Packet
*pkt
)
188 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
190 while (!ports
.empty())
192 interfaces
[ports
.back()]->sendAtomic(pkt
);
198 Bus::timingSnoopPhase1(Packet
*pkt
)
200 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
203 while (!ports
.empty() && success
)
205 snoopCallbacks
.push_back(ports
.back());
206 success
= interfaces
[ports
.back()]->sendTiming(pkt
);
211 while (!snoopCallbacks
.empty())
213 interfaces
[snoopCallbacks
.back()]->sendStatusChange(Port::SnoopSquash
);
214 snoopCallbacks
.pop_back();
222 Bus::timingSnoopPhase2(Packet
*pkt
)
225 pkt
->flags
|= SNOOP_COMMIT
;
226 while (!snoopCallbacks
.empty())
228 success
= interfaces
[snoopCallbacks
.back()]->sendTiming(pkt
);
229 //We should not fail on snoop callbacks
231 snoopCallbacks
.pop_back();
235 /** Function called by the port when the bus is receiving a Atomic
238 Bus::recvAtomic(Packet
*pkt
)
240 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
241 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
242 assert(pkt
->getDest() == Packet::Broadcast
);
244 return findPort(pkt
->getAddr(), pkt
->getSrc())->sendAtomic(pkt
);
247 /** Function called by the port when the bus is receiving a Functional
250 Bus::recvFunctional(Packet
*pkt
)
252 DPRINTF(Bus
, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
253 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
254 assert(pkt
->getDest() == Packet::Broadcast
);
256 findPort(pkt
->getAddr(), pkt
->getSrc())->sendFunctional(pkt
);
259 /** Function called by the port when the bus is receiving a status change.*/
261 Bus::recvStatusChange(Port::Status status
, int id
)
263 AddrRangeList ranges
;
264 AddrRangeList snoops
;
268 assert(status
== Port::RangeChange
&&
269 "The other statuses need to be implemented.");
271 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n", id
);
273 if (id
== defaultId
) {
274 defaultRange
.clear();
275 defaultPort
->getPeerAddressRanges(ranges
, snoops
);
276 assert(snoops
.size() == 0);
277 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
278 defaultRange
.push_back(*iter
);
279 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
280 iter
->start
, iter
->end
);
284 assert((id
< interfaces
.size() && id
>= 0) || id
== -1);
285 Port
*port
= interfaces
[id
];
286 std::vector
<DevMap
>::iterator portIter
;
287 std::vector
<DevMap
>::iterator snoopIter
;
289 // Clean out any previously existent ids
290 for (portIter
= portList
.begin(); portIter
!= portList
.end(); ) {
291 if (portIter
->portId
== id
)
292 portIter
= portList
.erase(portIter
);
297 for (snoopIter
= portSnoopList
.begin(); snoopIter
!= portSnoopList
.end(); ) {
298 if (snoopIter
->portId
== id
)
299 snoopIter
= portSnoopList
.erase(snoopIter
);
304 port
->getPeerAddressRanges(ranges
, snoops
);
306 for(iter
= snoops
.begin(); iter
!= snoops
.end(); iter
++) {
311 DPRINTF(BusAddrRanges
, "Adding snoop range %#llx - %#llx for id %d\n",
312 dm
.range
.start
, dm
.range
.end
, id
);
313 portSnoopList
.push_back(dm
);
316 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
321 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
322 dm
.range
.start
, dm
.range
.end
, id
);
323 portList
.push_back(dm
);
326 DPRINTF(MMU
, "port list has %d entries\n", portList
.size());
328 // tell all our peers that our address range has changed.
329 // Don't tell the device that caused this change, it already knows
330 for (x
= 0; x
< interfaces
.size(); x
++)
332 interfaces
[x
]->sendStatusChange(Port::RangeChange
);
334 if (id
!= defaultId
&& defaultPort
)
335 defaultPort
->sendStatusChange(Port::RangeChange
);
339 Bus::addressRanges(AddrRangeList
&resp
, AddrRangeList
&snoop
, int id
)
341 std::vector
<DevMap
>::iterator portIter
;
342 AddrRangeIter dflt_iter
;
348 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
350 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
352 resp
.push_back(*dflt_iter
);
353 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",dflt_iter
->start
,
356 for (portIter
= portList
.begin(); portIter
!= portList
.end(); portIter
++) {
358 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
360 if ((portIter
->range
.start
< dflt_iter
->start
&&
361 portIter
->range
.end
>= dflt_iter
->start
) ||
362 (portIter
->range
.start
< dflt_iter
->end
&&
363 portIter
->range
.end
>= dflt_iter
->end
))
364 fatal("Devices can not set ranges that itersect the default set\
365 but are not a subset of the default set.\n");
366 if (portIter
->range
.start
>= dflt_iter
->start
&&
367 portIter
->range
.end
<= dflt_iter
->end
) {
369 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
370 portIter
->range
.start
, portIter
->range
.end
);
373 if (portIter
->portId
!= id
&& !subset
) {
374 resp
.push_back(portIter
->range
);
375 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
376 portIter
->range
.start
, portIter
->range
.end
);
381 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus
)
385 END_DECLARE_SIM_OBJECT_PARAMS(Bus
)
387 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus
)
388 INIT_PARAM(bus_id
, "a globally unique bus id")
389 END_INIT_SIM_OBJECT_PARAMS(Bus
)
391 CREATE_SIM_OBJECT(Bus
)
393 return new Bus(getInstanceName(), bus_id
);
396 REGISTER_SIM_OBJECT("Bus", Bus
)