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
<BusPort
*>::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 BusPort
*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
.front()->onRetryList(false);
114 retryList
.pop_front();
119 port
= findPort(pkt
->getAddr(), pkt
->getSrc());
121 //Snoop didn't succeed
122 DPRINTF(Bus
, "Snoop caused adding to RETRY list %i\n", pktPort
);
123 addToRetryList(pktPort
);
127 assert(dest
>= 0 && dest
< interfaces
.size());
128 assert(dest
!= pkt
->getSrc()); // catch infinite loops
129 port
= interfaces
[dest
];
132 //Bring tickNextIdle up to the present tick
133 //There is some potential ambiguity where a cycle starts, which might make
134 //a difference when devices are acting right around a cycle boundary. Using
135 //a < allows things which happen exactly on a cycle boundary to take up only
136 //the following cycle. Anthing that happens later will have to "wait" for
137 //the end of that cycle, and then start using the bus after that.
138 while (tickNextIdle
< curTick
)
139 tickNextIdle
+= clock
;
141 // The packet will be sent. Figure out how long it occupies the bus, and
142 // how much of that time is for the first "word", aka bus width.
144 // Requests need one cycle to send an address
145 if (pkt
->isRequest())
147 else if (pkt
->isResponse() || pkt
->hasData()) {
148 // If a packet has data, it needs ceil(size/width) cycles to send it
149 // We're using the "adding instead of dividing" trick again here
150 if (pkt
->hasData()) {
151 int dataSize
= pkt
->getSize();
152 for (int transmitted
= 0; transmitted
< dataSize
;
153 transmitted
+= width
) {
157 // If the packet didn't have data, it must have been a response.
158 // Those use the bus for one cycle to send their data.
163 // The first word will be delivered after the current tick, the delivery
164 // of the address if any, and one bus cycle to deliver the data
167 pkt
->isRequest() ? clock
: 0 +
170 //Advance it numCycles bus cycles.
171 //XXX Should this use the repeated addition trick as well?
172 tickNextIdle
+= (numCycles
* clock
);
173 if (!busIdle
.scheduled()) {
174 busIdle
.schedule(tickNextIdle
);
176 busIdle
.reschedule(tickNextIdle
);
178 DPRINTF(Bus
, "The bus is now occupied from tick %d to %d\n",
179 curTick
, tickNextIdle
);
181 // The bus will become idle once the current packet is delivered.
182 pkt
->finishTime
= tickNextIdle
;
184 if (port
->sendTiming(pkt
)) {
185 // Packet was successfully sent. Return true.
186 // Also take care of retries
188 DPRINTF(Bus
, "Remove retry from list %i\n", retryList
.front());
189 retryList
.front()->onRetryList(false);
190 retryList
.pop_front();
196 // Packet not successfully sent. Leave or put it on the retry list.
197 DPRINTF(Bus
, "Adding a retry to RETRY list %i\n", pktPort
);
198 addToRetryList(pktPort
);
203 Bus::recvRetry(int id
)
205 DPRINTF(Bus
, "Received a retry\n");
206 // If there's anything waiting...
207 if (retryList
.size()) {
208 //retryingPort = retryList.front();
210 DPRINTF(Bus
, "Sending a retry\n");
211 retryList
.front()->sendRetry();
212 // If inRetry is still true, sendTiming wasn't called
214 panic("Port %s didn't call sendTiming in it's recvRetry\n",\
215 retryList
.front()->getPeer()->name());
221 Bus::findPort(Addr addr
, int id
)
223 /* An interval tree would be a better way to do this. --ali. */
229 while (i
< portList
.size() && !found
)
231 if (portList
[i
].range
== addr
) {
232 dest_id
= portList
[i
].portId
;
234 DPRINTF(Bus
, " found addr %#llx on device %d\n", addr
, dest_id
);
239 // Check if this matches the default range
241 for (iter
= defaultRange
.begin(); iter
!= defaultRange
.end(); iter
++) {
243 DPRINTF(Bus
, " found addr %#llx on default\n", addr
);
247 panic("Unable to find destination for addr: %#llx", addr
);
251 // we shouldn't be sending this back to where it came from
252 assert(dest_id
!= id
);
254 return interfaces
[dest_id
];
258 Bus::findSnoopPorts(Addr addr
, int id
)
262 std::vector
<int> ports
;
264 while (i
< portSnoopList
.size())
266 if (portSnoopList
[i
].range
== addr
&& portSnoopList
[i
].portId
!= id
) {
267 //Careful to not overlap ranges
268 //or snoop will be called more than once on the port
269 ports
.push_back(portSnoopList
[i
].portId
);
270 // DPRINTF(Bus, " found snoop addr %#llx on device%d\n", addr,
271 // portSnoopList[i].portId);
279 Bus::atomicSnoop(Packet
*pkt
)
281 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
283 while (!ports
.empty())
285 interfaces
[ports
.back()]->sendAtomic(pkt
);
291 Bus::functionalSnoop(Packet
*pkt
)
293 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
295 while (!ports
.empty())
297 interfaces
[ports
.back()]->sendFunctional(pkt
);
303 Bus::timingSnoop(Packet
*pkt
)
305 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
308 while (!ports
.empty() && success
)
310 success
= interfaces
[ports
.back()]->sendTiming(pkt
);
318 /** Function called by the port when the bus is receiving a Atomic
321 Bus::recvAtomic(Packet
*pkt
)
323 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
324 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
325 assert(pkt
->getDest() == Packet::Broadcast
);
327 return findPort(pkt
->getAddr(), pkt
->getSrc())->sendAtomic(pkt
);
330 /** Function called by the port when the bus is receiving a Functional
333 Bus::recvFunctional(Packet
*pkt
)
335 DPRINTF(Bus
, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
336 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
337 assert(pkt
->getDest() == Packet::Broadcast
);
338 functionalSnoop(pkt
);
339 findPort(pkt
->getAddr(), pkt
->getSrc())->sendFunctional(pkt
);
342 /** Function called by the port when the bus is receiving a status change.*/
344 Bus::recvStatusChange(Port::Status status
, int id
)
346 AddrRangeList ranges
;
347 AddrRangeList snoops
;
351 assert(status
== Port::RangeChange
&&
352 "The other statuses need to be implemented.");
354 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n", id
);
356 if (id
== defaultId
) {
357 defaultRange
.clear();
358 defaultPort
->getPeerAddressRanges(ranges
, snoops
);
359 assert(snoops
.size() == 0);
360 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
361 defaultRange
.push_back(*iter
);
362 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
363 iter
->start
, iter
->end
);
367 assert((id
< interfaces
.size() && id
>= 0) || id
== -1);
368 Port
*port
= interfaces
[id
];
369 std::vector
<DevMap
>::iterator portIter
;
370 std::vector
<DevMap
>::iterator snoopIter
;
372 // Clean out any previously existent ids
373 for (portIter
= portList
.begin(); portIter
!= portList
.end(); ) {
374 if (portIter
->portId
== id
)
375 portIter
= portList
.erase(portIter
);
380 for (snoopIter
= portSnoopList
.begin(); snoopIter
!= portSnoopList
.end(); ) {
381 if (snoopIter
->portId
== id
)
382 snoopIter
= portSnoopList
.erase(snoopIter
);
387 port
->getPeerAddressRanges(ranges
, snoops
);
389 for(iter
= snoops
.begin(); iter
!= snoops
.end(); iter
++) {
394 DPRINTF(BusAddrRanges
, "Adding snoop range %#llx - %#llx for id %d\n",
395 dm
.range
.start
, dm
.range
.end
, id
);
396 portSnoopList
.push_back(dm
);
399 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
404 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
405 dm
.range
.start
, dm
.range
.end
, id
);
406 portList
.push_back(dm
);
409 DPRINTF(MMU
, "port list has %d entries\n", portList
.size());
411 // tell all our peers that our address range has changed.
412 // Don't tell the device that caused this change, it already knows
413 for (x
= 0; x
< interfaces
.size(); x
++)
415 interfaces
[x
]->sendStatusChange(Port::RangeChange
);
417 if (id
!= defaultId
&& defaultPort
)
418 defaultPort
->sendStatusChange(Port::RangeChange
);
422 Bus::addressRanges(AddrRangeList
&resp
, AddrRangeList
&snoop
, int id
)
424 std::vector
<DevMap
>::iterator portIter
;
425 AddrRangeIter dflt_iter
;
431 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
433 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
435 resp
.push_back(*dflt_iter
);
436 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",dflt_iter
->start
,
439 for (portIter
= portList
.begin(); portIter
!= portList
.end(); portIter
++) {
441 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
443 if ((portIter
->range
.start
< dflt_iter
->start
&&
444 portIter
->range
.end
>= dflt_iter
->start
) ||
445 (portIter
->range
.start
< dflt_iter
->end
&&
446 portIter
->range
.end
>= dflt_iter
->end
))
447 fatal("Devices can not set ranges that itersect the default set\
448 but are not a subset of the default set.\n");
449 if (portIter
->range
.start
>= dflt_iter
->start
&&
450 portIter
->range
.end
<= dflt_iter
->end
) {
452 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
453 portIter
->range
.start
, portIter
->range
.end
);
456 if (portIter
->portId
!= id
&& !subset
) {
457 resp
.push_back(portIter
->range
);
458 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
459 portIter
->range
.start
, portIter
->range
.end
);
464 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus
)
470 END_DECLARE_SIM_OBJECT_PARAMS(Bus
)
472 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus
)
473 INIT_PARAM(bus_id
, "a globally unique bus id"),
474 INIT_PARAM(clock
, "bus clock speed"),
475 INIT_PARAM(width
, "width of the bus (bits)")
476 END_INIT_SIM_OBJECT_PARAMS(Bus
)
478 CREATE_SIM_OBJECT(Bus
)
480 return new Bus(getInstanceName(), bus_id
, clock
, width
);
483 REGISTER_SIM_OBJECT("Bus", Bus
)