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";
83 /** Function called by the port when the bus is receiving a Timing
86 Bus::recvTiming(Packet
*pkt
)
89 DPRINTF(Bus
, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
90 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
92 Port
*pktPort
= interfaces
[pkt
->getSrc()];
94 // If the bus is busy, or other devices are in line ahead of the current
95 // one, put this device on the retry list.
96 if (tickNextIdle
> curTick
||
97 (retryList
.size() && (!inRetry
|| pktPort
!= retryList
.front()))) {
98 DPRINTF(Bus
, "Adding RETRY for %i\n", pktPort
);
99 addToRetryList(pktPort
);
103 short dest
= pkt
->getDest();
104 if (dest
== Packet::Broadcast
) {
105 if (timingSnoop(pkt
)) {
106 pkt
->flags
|= SNOOP_COMMIT
;
107 bool success
= timingSnoop(pkt
);
109 if (pkt
->flags
& SATISFIED
) {
110 //Cache-Cache transfer occuring
112 DPRINTF(Bus
, "Removing RETRY %i\n", retryList
.front());
113 retryList
.pop_front();
118 port
= findPort(pkt
->getAddr(), pkt
->getSrc());
120 //Snoop didn't succeed
121 DPRINTF(Bus
, "Snoop caused adding to RETRY list %i\n", pktPort
);
122 addToRetryList(pktPort
);
126 assert(dest
>= 0 && dest
< interfaces
.size());
127 assert(dest
!= pkt
->getSrc()); // catch infinite loops
128 port
= interfaces
[dest
];
131 //Bring tickNextIdle up to the present tick
132 //There is some potential ambiguity where a cycle starts, which might make
133 //a difference when devices are acting right around a cycle boundary. Using
134 //a < allows things which happen exactly on a cycle boundary to take up only
135 //the following cycle. Anthing that happens later will have to "wait" for
136 //the end of that cycle, and then start using the bus after that.
137 while (tickNextIdle
< curTick
)
138 tickNextIdle
+= clock
;
140 // The packet will be sent. Figure out how long it occupies the bus, and
141 // how much of that time is for the first "word", aka bus width.
143 // Requests need one cycle to send an address
144 if (pkt
->isRequest())
146 else if (pkt
->isResponse() || pkt
->hasData()) {
147 // If a packet has data, it needs ceil(size/width) cycles to send it
148 // We're using the "adding instead of dividing" trick again here
149 if (pkt
->hasData()) {
150 int dataSize
= pkt
->getSize();
151 for (int transmitted
= 0; transmitted
< dataSize
;
152 transmitted
+= width
) {
156 // If the packet didn't have data, it must have been a response.
157 // Those use the bus for one cycle to send their data.
162 // The first word will be delivered after the current tick, the delivery
163 // of the address if any, and one bus cycle to deliver the data
166 pkt
->isRequest() ? clock
: 0 +
169 //Advance it numCycles bus cycles.
170 //XXX Should this use the repeated addition trick as well?
171 tickNextIdle
+= (numCycles
* clock
);
172 if (!busIdle
.scheduled()) {
173 busIdle
.schedule(tickNextIdle
);
175 busIdle
.reschedule(tickNextIdle
);
177 DPRINTF(Bus
, "The bus is now occupied from tick %d to %d\n",
178 curTick
, tickNextIdle
);
180 // The bus will become idle once the current packet is delivered.
181 pkt
->finishTime
= tickNextIdle
;
183 if (port
->sendTiming(pkt
)) {
184 // Packet was successfully sent. Return true.
185 // Also take care of retries
187 DPRINTF(Bus
, "Remove retry from list %i\n", retryList
.front());
188 retryList
.pop_front();
194 // Packet not successfully sent. Leave or put it on the retry list.
195 DPRINTF(Bus
, "Adding a retry to RETRY list %i\n", pktPort
);
196 addToRetryList(pktPort
);
201 Bus::recvRetry(int id
)
203 DPRINTF(Bus
, "Received a retry\n");
204 // If there's anything waiting...
205 if (retryList
.size()) {
206 //retryingPort = retryList.front();
208 DPRINTF(Bus
, "Sending a retry\n");
209 retryList
.front()->sendRetry();
210 // If inRetry is still true, sendTiming wasn't called
212 panic("Port %s didn't call sendTiming in it's recvRetry\n",\
213 retryList
.front()->getPeer()->name());
219 Bus::findPort(Addr addr
, int id
)
221 /* An interval tree would be a better way to do this. --ali. */
227 while (i
< portList
.size() && !found
)
229 if (portList
[i
].range
== addr
) {
230 dest_id
= portList
[i
].portId
;
232 DPRINTF(Bus
, " found addr %#llx on device %d\n", addr
, dest_id
);
237 // Check if this matches the default range
239 for (iter
= defaultRange
.begin(); iter
!= defaultRange
.end(); iter
++) {
241 DPRINTF(Bus
, " found addr %#llx on default\n", addr
);
245 panic("Unable to find destination for addr: %#llx", addr
);
249 // we shouldn't be sending this back to where it came from
250 assert(dest_id
!= id
);
252 return interfaces
[dest_id
];
256 Bus::findSnoopPorts(Addr addr
, int id
)
260 std::vector
<int> ports
;
262 while (i
< portSnoopList
.size())
264 if (portSnoopList
[i
].range
== addr
&& portSnoopList
[i
].portId
!= id
) {
265 //Careful to not overlap ranges
266 //or snoop will be called more than once on the port
267 ports
.push_back(portSnoopList
[i
].portId
);
268 // DPRINTF(Bus, " found snoop addr %#llx on device%d\n", addr,
269 // portSnoopList[i].portId);
277 Bus::atomicSnoop(Packet
*pkt
)
279 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
281 while (!ports
.empty())
283 interfaces
[ports
.back()]->sendAtomic(pkt
);
289 Bus::functionalSnoop(Packet
*pkt
)
291 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
293 while (!ports
.empty())
295 interfaces
[ports
.back()]->sendFunctional(pkt
);
301 Bus::timingSnoop(Packet
*pkt
)
303 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
306 while (!ports
.empty() && success
)
308 success
= interfaces
[ports
.back()]->sendTiming(pkt
);
316 /** Function called by the port when the bus is receiving a Atomic
319 Bus::recvAtomic(Packet
*pkt
)
321 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
322 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
323 assert(pkt
->getDest() == Packet::Broadcast
);
325 return findPort(pkt
->getAddr(), pkt
->getSrc())->sendAtomic(pkt
);
328 /** Function called by the port when the bus is receiving a Functional
331 Bus::recvFunctional(Packet
*pkt
)
333 DPRINTF(Bus
, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
334 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
335 assert(pkt
->getDest() == Packet::Broadcast
);
336 functionalSnoop(pkt
);
337 findPort(pkt
->getAddr(), pkt
->getSrc())->sendFunctional(pkt
);
340 /** Function called by the port when the bus is receiving a status change.*/
342 Bus::recvStatusChange(Port::Status status
, int id
)
344 AddrRangeList ranges
;
345 AddrRangeList snoops
;
349 assert(status
== Port::RangeChange
&&
350 "The other statuses need to be implemented.");
352 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n", id
);
354 if (id
== defaultId
) {
355 defaultRange
.clear();
356 defaultPort
->getPeerAddressRanges(ranges
, snoops
);
357 assert(snoops
.size() == 0);
358 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
359 defaultRange
.push_back(*iter
);
360 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
361 iter
->start
, iter
->end
);
365 assert((id
< interfaces
.size() && id
>= 0) || id
== -1);
366 Port
*port
= interfaces
[id
];
367 std::vector
<DevMap
>::iterator portIter
;
368 std::vector
<DevMap
>::iterator snoopIter
;
370 // Clean out any previously existent ids
371 for (portIter
= portList
.begin(); portIter
!= portList
.end(); ) {
372 if (portIter
->portId
== id
)
373 portIter
= portList
.erase(portIter
);
378 for (snoopIter
= portSnoopList
.begin(); snoopIter
!= portSnoopList
.end(); ) {
379 if (snoopIter
->portId
== id
)
380 snoopIter
= portSnoopList
.erase(snoopIter
);
385 port
->getPeerAddressRanges(ranges
, snoops
);
387 for(iter
= snoops
.begin(); iter
!= snoops
.end(); iter
++) {
392 DPRINTF(BusAddrRanges
, "Adding snoop range %#llx - %#llx for id %d\n",
393 dm
.range
.start
, dm
.range
.end
, id
);
394 portSnoopList
.push_back(dm
);
397 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
402 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
403 dm
.range
.start
, dm
.range
.end
, id
);
404 portList
.push_back(dm
);
407 DPRINTF(MMU
, "port list has %d entries\n", portList
.size());
409 // tell all our peers that our address range has changed.
410 // Don't tell the device that caused this change, it already knows
411 for (x
= 0; x
< interfaces
.size(); x
++)
413 interfaces
[x
]->sendStatusChange(Port::RangeChange
);
415 if (id
!= defaultId
&& defaultPort
)
416 defaultPort
->sendStatusChange(Port::RangeChange
);
420 Bus::addressRanges(AddrRangeList
&resp
, AddrRangeList
&snoop
, int id
)
422 std::vector
<DevMap
>::iterator portIter
;
423 AddrRangeIter dflt_iter
;
429 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
431 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
433 resp
.push_back(*dflt_iter
);
434 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",dflt_iter
->start
,
437 for (portIter
= portList
.begin(); portIter
!= portList
.end(); portIter
++) {
439 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
441 if ((portIter
->range
.start
< dflt_iter
->start
&&
442 portIter
->range
.end
>= dflt_iter
->start
) ||
443 (portIter
->range
.start
< dflt_iter
->end
&&
444 portIter
->range
.end
>= dflt_iter
->end
))
445 fatal("Devices can not set ranges that itersect the default set\
446 but are not a subset of the default set.\n");
447 if (portIter
->range
.start
>= dflt_iter
->start
&&
448 portIter
->range
.end
<= dflt_iter
->end
) {
450 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
451 portIter
->range
.start
, portIter
->range
.end
);
454 if (portIter
->portId
!= id
&& !subset
) {
455 resp
.push_back(portIter
->range
);
456 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
457 portIter
->range
.start
, portIter
->range
.end
);
462 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus
)
468 END_DECLARE_SIM_OBJECT_PARAMS(Bus
)
470 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus
)
471 INIT_PARAM(bus_id
, "a globally unique bus id"),
472 INIT_PARAM(clock
, "bus clock speed"),
473 INIT_PARAM(width
, "width of the bus (bits)")
474 END_INIT_SIM_OBJECT_PARAMS(Bus
)
476 CREATE_SIM_OBJECT(Bus
)
478 return new Bus(getInstanceName(), bus_id
, clock
, width
);
481 REGISTER_SIM_OBJECT("Bus", Bus
)