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
) {
84 pkt
->flags
|= SNOOP_COMMIT
;
85 bool success
= timingSnoop(pkt
);
87 if (pkt
->flags
& SATISFIED
) {
88 //Cache-Cache transfer occuring
91 port
= findPort(pkt
->getAddr(), pkt
->getSrc());
95 //Snoop didn't succeed
96 retryList
.push_back(interfaces
[pkt
->getSrc()]);
100 assert(dest
>= 0 && dest
< interfaces
.size());
101 assert(dest
!= pkt
->getSrc()); // catch infinite loops
102 port
= interfaces
[dest
];
104 if (port
->sendTiming(pkt
)) {
105 // packet was successfully sent, just return true.
109 // packet not successfully sent
110 retryList
.push_back(interfaces
[pkt
->getSrc()]);
115 Bus::recvRetry(int id
)
117 // Go through all the elements on the list calling sendRetry on each
118 // This is not very efficient at all but it works. Ultimately we should end
119 // up with something that is more intelligent.
120 int initialSize
= retryList
.size();
124 for (i
= 0; i
< initialSize
; i
++) {
125 assert(retryList
.size() > 0);
126 p
= retryList
.front();
127 retryList
.pop_front();
134 Bus::findPort(Addr addr
, int id
)
136 /* An interval tree would be a better way to do this. --ali. */
142 while (i
< portList
.size() && !found
)
144 if (portList
[i
].range
== addr
) {
145 dest_id
= portList
[i
].portId
;
147 DPRINTF(Bus
, " found addr %#llx on device %d\n", addr
, dest_id
);
152 // Check if this matches the default range
154 for (iter
= defaultRange
.begin(); iter
!= defaultRange
.end(); iter
++) {
156 DPRINTF(Bus
, " found addr %#llx on default\n", addr
);
160 panic("Unable to find destination for addr: %#llx", addr
);
164 // we shouldn't be sending this back to where it came from
165 assert(dest_id
!= id
);
167 return interfaces
[dest_id
];
171 Bus::findSnoopPorts(Addr addr
, int id
)
175 std::vector
<int> ports
;
177 while (i
< portSnoopList
.size())
179 if (portSnoopList
[i
].range
== addr
&& portSnoopList
[i
].portId
!= id
) {
180 //Careful to not overlap ranges
181 //or snoop will be called more than once on the port
182 ports
.push_back(portSnoopList
[i
].portId
);
183 DPRINTF(Bus
, " found snoop addr %#llx on device%d\n", addr
,
184 portSnoopList
[i
].portId
);
192 Bus::atomicSnoop(Packet
*pkt
)
194 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
196 while (!ports
.empty())
198 interfaces
[ports
.back()]->sendAtomic(pkt
);
204 Bus::functionalSnoop(Packet
*pkt
)
206 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
208 while (!ports
.empty())
210 interfaces
[ports
.back()]->sendFunctional(pkt
);
216 Bus::timingSnoop(Packet
*pkt
)
218 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
221 while (!ports
.empty() && success
)
223 success
= interfaces
[ports
.back()]->sendTiming(pkt
);
231 /** Function called by the port when the bus is receiving a Atomic
234 Bus::recvAtomic(Packet
*pkt
)
236 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
237 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
238 assert(pkt
->getDest() == Packet::Broadcast
);
240 return findPort(pkt
->getAddr(), pkt
->getSrc())->sendAtomic(pkt
);
243 /** Function called by the port when the bus is receiving a Functional
246 Bus::recvFunctional(Packet
*pkt
)
248 DPRINTF(Bus
, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
249 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
250 assert(pkt
->getDest() == Packet::Broadcast
);
251 functionalSnoop(pkt
);
252 findPort(pkt
->getAddr(), pkt
->getSrc())->sendFunctional(pkt
);
255 /** Function called by the port when the bus is receiving a status change.*/
257 Bus::recvStatusChange(Port::Status status
, int id
)
259 AddrRangeList ranges
;
260 AddrRangeList snoops
;
264 assert(status
== Port::RangeChange
&&
265 "The other statuses need to be implemented.");
267 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n", id
);
269 if (id
== defaultId
) {
270 defaultRange
.clear();
271 defaultPort
->getPeerAddressRanges(ranges
, snoops
);
272 assert(snoops
.size() == 0);
273 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
274 defaultRange
.push_back(*iter
);
275 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
276 iter
->start
, iter
->end
);
280 assert((id
< interfaces
.size() && id
>= 0) || id
== -1);
281 Port
*port
= interfaces
[id
];
282 std::vector
<DevMap
>::iterator portIter
;
283 std::vector
<DevMap
>::iterator snoopIter
;
285 // Clean out any previously existent ids
286 for (portIter
= portList
.begin(); portIter
!= portList
.end(); ) {
287 if (portIter
->portId
== id
)
288 portIter
= portList
.erase(portIter
);
293 for (snoopIter
= portSnoopList
.begin(); snoopIter
!= portSnoopList
.end(); ) {
294 if (snoopIter
->portId
== id
)
295 snoopIter
= portSnoopList
.erase(snoopIter
);
300 port
->getPeerAddressRanges(ranges
, snoops
);
302 for(iter
= snoops
.begin(); iter
!= snoops
.end(); iter
++) {
307 DPRINTF(BusAddrRanges
, "Adding snoop range %#llx - %#llx for id %d\n",
308 dm
.range
.start
, dm
.range
.end
, id
);
309 portSnoopList
.push_back(dm
);
312 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
317 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
318 dm
.range
.start
, dm
.range
.end
, id
);
319 portList
.push_back(dm
);
322 DPRINTF(MMU
, "port list has %d entries\n", portList
.size());
324 // tell all our peers that our address range has changed.
325 // Don't tell the device that caused this change, it already knows
326 for (x
= 0; x
< interfaces
.size(); x
++)
328 interfaces
[x
]->sendStatusChange(Port::RangeChange
);
330 if (id
!= defaultId
&& defaultPort
)
331 defaultPort
->sendStatusChange(Port::RangeChange
);
335 Bus::addressRanges(AddrRangeList
&resp
, AddrRangeList
&snoop
, int id
)
337 std::vector
<DevMap
>::iterator portIter
;
338 AddrRangeIter dflt_iter
;
344 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
346 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
348 resp
.push_back(*dflt_iter
);
349 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",dflt_iter
->start
,
352 for (portIter
= portList
.begin(); portIter
!= portList
.end(); portIter
++) {
354 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
356 if ((portIter
->range
.start
< dflt_iter
->start
&&
357 portIter
->range
.end
>= dflt_iter
->start
) ||
358 (portIter
->range
.start
< dflt_iter
->end
&&
359 portIter
->range
.end
>= dflt_iter
->end
))
360 fatal("Devices can not set ranges that itersect the default set\
361 but are not a subset of the default set.\n");
362 if (portIter
->range
.start
>= dflt_iter
->start
&&
363 portIter
->range
.end
<= dflt_iter
->end
) {
365 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
366 portIter
->range
.start
, portIter
->range
.end
);
369 if (portIter
->portId
!= id
&& !subset
) {
370 resp
.push_back(portIter
->range
);
371 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
372 portIter
->range
.start
, portIter
->range
.end
);
377 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus
)
381 END_DECLARE_SIM_OBJECT_PARAMS(Bus
)
383 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus
)
384 INIT_PARAM(bus_id
, "a globally unique bus id")
385 END_INIT_SIM_OBJECT_PARAMS(Bus
)
387 CREATE_SIM_OBJECT(Bus
)
389 return new Bus(getInstanceName(), bus_id
);
392 REGISTER_SIM_OBJECT("Bus", Bus
)