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
);
70 Bus::BusFreeEvent::BusFreeEvent(Bus
*_bus
) : Event(&mainEventQueue
), bus(_bus
)
73 void Bus::BusFreeEvent::process()
78 const char * Bus::BusFreeEvent::description()
80 return "bus became available";
84 Bus::occupyBus(int numCycles
)
86 //Move up when the bus will next be free
87 //We avoid the use of divide by adding repeatedly
88 //This should be faster if the value is updated frequently, but should
89 //be may be slower otherwise.
91 //Bring tickNextIdle up to the present tick
92 //There is some potential ambiguity where a cycle starts, which might make
93 //a difference when devices are acting right around a cycle boundary. Using
94 //a < allows things which happen exactly on a cycle boundary to take up only
95 //the following cycle. Anthing that happens later will have to "wait" for the
96 //end of that cycle, and then start using the bus after that.
97 while (tickNextIdle
< curTick
)
98 tickNextIdle
+= clock
;
99 //Advance it numCycles bus cycles.
100 //XXX Should this use the repeating add trick as well?
101 tickNextIdle
+= (numCycles
* clock
);
102 if (!busIdle
.scheduled()) {
103 busIdle
.schedule(tickNextIdle
);
105 busIdle
.reschedule(tickNextIdle
);
109 /** Function called by the port when the bus is receiving a Timing
112 Bus::recvTiming(Packet
*pkt
)
115 DPRINTF(Bus
, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
116 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
118 Port
*pktPort
= interfaces
[pkt
->getSrc()];
120 // If the bus is busy, or other devices are in line ahead of the current one,
121 // put this device on the retry list.
122 if (tickNextIdle
> curTick
|| (retryList
.size() && pktPort
!= retryingPort
)) {
123 addToRetryList(pktPort
);
127 // If the bus is blocked, make the device wait.
128 if (!(port
= findDestPort(pkt
, pkt
->getSrc()))) {
129 addToRetryList(pktPort
);
133 // The packet will be sent. Figure out how long it occupies the bus.
135 // Requests need one cycle to send an address
136 if (pkt
->isRequest())
138 else if (pkt
->isResponse() || pkt
->hasData()) {
139 // If a packet has data, it needs ceil(size/width) cycles to send it
140 // We're using the "adding instead of dividing" trick again here
141 if (pkt
->hasData()) {
142 int dataSize
= pkt
->getSize();
143 for (int transmitted
= 0; transmitted
< dataSize
;
144 transmitted
+= width
) {
148 // If the packet didn't have data, it must have been a response.
149 // Those use the bus for one cycle to send their data.
154 occupyBus(numCycles
);
156 if (port
->sendTiming(pkt
)) {
157 // Packet was successfully sent. Return true.
161 // Packet not successfully sent. Leave or put it on the retry list.
162 addToRetryList(pktPort
);
167 Bus::recvRetry(int id
)
169 //If there's anything waiting...
170 if (retryList
.size()) {
171 retryingPort
= retryList
.front();
172 retryingPort
->sendRetry();
173 //If the retryingPort pointer isn't null, either sendTiming wasn't
174 //called, or it was and the packet was successfully sent.
176 retryList
.pop_front();
183 Bus::findDestPort(PacketPtr pkt
, int id
)
186 short dest
= pkt
->getDest();
188 if (dest
== Packet::Broadcast
) {
189 if (timingSnoopPhase1(pkt
)) {
190 timingSnoopPhase2(pkt
);
191 port
= findPort(pkt
->getAddr(), pkt
->getSrc());
193 //else, port stays NULL
195 assert(dest
>= 0 && dest
< interfaces
.size());
196 assert(dest
!= pkt
->getSrc()); // catch infinite loops
197 port
= interfaces
[dest
];
203 Bus::findPort(Addr addr
, int id
)
205 /* An interval tree would be a better way to do this. --ali. */
211 while (i
< portList
.size() && !found
)
213 if (portList
[i
].range
== addr
) {
214 dest_id
= portList
[i
].portId
;
216 DPRINTF(Bus
, " found addr %#llx on device %d\n", addr
, dest_id
);
221 // Check if this matches the default range
223 for (iter
= defaultRange
.begin(); iter
!= defaultRange
.end(); iter
++) {
225 DPRINTF(Bus
, " found addr %#llx on default\n", addr
);
229 panic("Unable to find destination for addr: %#llx", addr
);
233 // we shouldn't be sending this back to where it came from
234 assert(dest_id
!= id
);
236 return interfaces
[dest_id
];
240 Bus::findSnoopPorts(Addr addr
, int id
)
244 std::vector
<int> ports
;
246 while (i
< portSnoopList
.size())
248 if (portSnoopList
[i
].range
== addr
&& portSnoopList
[i
].portId
!= id
) {
249 //Careful to not overlap ranges
250 //or snoop will be called more than once on the port
251 ports
.push_back(portSnoopList
[i
].portId
);
252 DPRINTF(Bus
, " found snoop addr %#llx on device%d\n", addr
,
253 portSnoopList
[i
].portId
);
261 Bus::atomicSnoop(Packet
*pkt
)
263 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
265 while (!ports
.empty())
267 interfaces
[ports
.back()]->sendAtomic(pkt
);
273 Bus::timingSnoopPhase1(Packet
*pkt
)
275 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
278 while (!ports
.empty() && success
)
280 snoopCallbacks
.push_back(ports
.back());
281 success
= interfaces
[ports
.back()]->sendTiming(pkt
);
286 while (!snoopCallbacks
.empty())
288 interfaces
[snoopCallbacks
.back()]->sendStatusChange(Port::SnoopSquash
);
289 snoopCallbacks
.pop_back();
297 Bus::timingSnoopPhase2(Packet
*pkt
)
300 pkt
->flags
|= SNOOP_COMMIT
;
301 while (!snoopCallbacks
.empty())
303 success
= interfaces
[snoopCallbacks
.back()]->sendTiming(pkt
);
304 //We should not fail on snoop callbacks
306 snoopCallbacks
.pop_back();
310 /** Function called by the port when the bus is receiving a Atomic
313 Bus::recvAtomic(Packet
*pkt
)
315 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
316 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
317 assert(pkt
->getDest() == Packet::Broadcast
);
319 return findPort(pkt
->getAddr(), pkt
->getSrc())->sendAtomic(pkt
);
322 /** Function called by the port when the bus is receiving a Functional
325 Bus::recvFunctional(Packet
*pkt
)
327 DPRINTF(Bus
, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
328 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
329 assert(pkt
->getDest() == Packet::Broadcast
);
330 findPort(pkt
->getAddr(), pkt
->getSrc())->sendFunctional(pkt
);
333 /** Function called by the port when the bus is receiving a status change.*/
335 Bus::recvStatusChange(Port::Status status
, int id
)
337 AddrRangeList ranges
;
338 AddrRangeList snoops
;
342 assert(status
== Port::RangeChange
&&
343 "The other statuses need to be implemented.");
345 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n", id
);
347 if (id
== defaultId
) {
348 defaultRange
.clear();
349 defaultPort
->getPeerAddressRanges(ranges
, snoops
);
350 assert(snoops
.size() == 0);
351 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
352 defaultRange
.push_back(*iter
);
353 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
354 iter
->start
, iter
->end
);
358 assert((id
< interfaces
.size() && id
>= 0) || id
== -1);
359 Port
*port
= interfaces
[id
];
360 std::vector
<DevMap
>::iterator portIter
;
361 std::vector
<DevMap
>::iterator snoopIter
;
363 // Clean out any previously existent ids
364 for (portIter
= portList
.begin(); portIter
!= portList
.end(); ) {
365 if (portIter
->portId
== id
)
366 portIter
= portList
.erase(portIter
);
371 for (snoopIter
= portSnoopList
.begin(); snoopIter
!= portSnoopList
.end(); ) {
372 if (snoopIter
->portId
== id
)
373 snoopIter
= portSnoopList
.erase(snoopIter
);
378 port
->getPeerAddressRanges(ranges
, snoops
);
380 for(iter
= snoops
.begin(); iter
!= snoops
.end(); iter
++) {
385 DPRINTF(BusAddrRanges
, "Adding snoop range %#llx - %#llx for id %d\n",
386 dm
.range
.start
, dm
.range
.end
, id
);
387 portSnoopList
.push_back(dm
);
390 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
395 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
396 dm
.range
.start
, dm
.range
.end
, id
);
397 portList
.push_back(dm
);
400 DPRINTF(MMU
, "port list has %d entries\n", portList
.size());
402 // tell all our peers that our address range has changed.
403 // Don't tell the device that caused this change, it already knows
404 for (x
= 0; x
< interfaces
.size(); x
++)
406 interfaces
[x
]->sendStatusChange(Port::RangeChange
);
408 if (id
!= defaultId
&& defaultPort
)
409 defaultPort
->sendStatusChange(Port::RangeChange
);
413 Bus::addressRanges(AddrRangeList
&resp
, AddrRangeList
&snoop
, int id
)
415 std::vector
<DevMap
>::iterator portIter
;
416 AddrRangeIter dflt_iter
;
422 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
424 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
426 resp
.push_back(*dflt_iter
);
427 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",dflt_iter
->start
,
430 for (portIter
= portList
.begin(); portIter
!= portList
.end(); portIter
++) {
432 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
434 if ((portIter
->range
.start
< dflt_iter
->start
&&
435 portIter
->range
.end
>= dflt_iter
->start
) ||
436 (portIter
->range
.start
< dflt_iter
->end
&&
437 portIter
->range
.end
>= dflt_iter
->end
))
438 fatal("Devices can not set ranges that itersect the default set\
439 but are not a subset of the default set.\n");
440 if (portIter
->range
.start
>= dflt_iter
->start
&&
441 portIter
->range
.end
<= dflt_iter
->end
) {
443 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
444 portIter
->range
.start
, portIter
->range
.end
);
447 if (portIter
->portId
!= id
&& !subset
) {
448 resp
.push_back(portIter
->range
);
449 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
450 portIter
->range
.start
, portIter
->range
.end
);
455 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus
)
461 END_DECLARE_SIM_OBJECT_PARAMS(Bus
)
463 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus
)
464 INIT_PARAM(bus_id
, "a globally unique bus id"),
465 INIT_PARAM(clock
, "bus clock speed"),
466 INIT_PARAM(width
, "width of the bus (bits)")
467 END_INIT_SIM_OBJECT_PARAMS(Bus
)
469 CREATE_SIM_OBJECT(Bus
)
471 return new Bus(getInstanceName(), bus_id
, clock
, width
);
474 REGISTER_SIM_OBJECT("Bus", Bus
)