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 void Bus::occupyBus(PacketPtr pkt
)
85 //Bring tickNextIdle up to the present tick
86 //There is some potential ambiguity where a cycle starts, which might make
87 //a difference when devices are acting right around a cycle boundary. Using
88 //a < allows things which happen exactly on a cycle boundary to take up only
89 //the following cycle. Anthing that happens later will have to "wait" for
90 //the end of that cycle, and then start using the bus after that.
91 while (tickNextIdle
< curTick
)
92 tickNextIdle
+= clock
;
94 // The packet will be sent. Figure out how long it occupies the bus, and
95 // how much of that time is for the first "word", aka bus width.
97 // Requests need one cycle to send an address
100 else if (pkt
->isResponse() || pkt
->hasData()) {
101 // If a packet has data, it needs ceil(size/width) cycles to send it
102 // We're using the "adding instead of dividing" trick again here
103 if (pkt
->hasData()) {
104 int dataSize
= pkt
->getSize();
105 for (int transmitted
= 0; transmitted
< dataSize
;
106 transmitted
+= width
) {
110 // If the packet didn't have data, it must have been a response.
111 // Those use the bus for one cycle to send their data.
116 // The first word will be delivered after the current tick, the delivery
117 // of the address if any, and one bus cycle to deliver the data
120 pkt
->isRequest() ? clock
: 0 +
123 //Advance it numCycles bus cycles.
124 //XXX Should this use the repeated addition trick as well?
125 tickNextIdle
+= (numCycles
* clock
);
126 if (!busIdle
.scheduled()) {
127 busIdle
.schedule(tickNextIdle
);
129 busIdle
.reschedule(tickNextIdle
);
131 DPRINTF(Bus
, "The bus is now occupied from tick %d to %d\n",
132 curTick
, tickNextIdle
);
134 // The bus will become idle once the current packet is delivered.
135 pkt
->finishTime
= tickNextIdle
;
138 /** Function called by the port when the bus is receiving a Timing
141 Bus::recvTiming(Packet
*pkt
)
144 DPRINTF(Bus
, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
145 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
147 BusPort
*pktPort
= interfaces
[pkt
->getSrc()];
149 // If the bus is busy, or other devices are in line ahead of the current
150 // one, put this device on the retry list.
151 if (tickNextIdle
> curTick
||
152 (retryList
.size() && (!inRetry
|| pktPort
!= retryList
.front()))) {
153 addToRetryList(pktPort
);
157 short dest
= pkt
->getDest();
158 if (dest
== Packet::Broadcast
) {
159 if (timingSnoop(pkt
)) {
160 pkt
->flags
|= SNOOP_COMMIT
;
161 bool success
= timingSnoop(pkt
);
163 if (pkt
->flags
& SATISFIED
) {
164 //Cache-Cache transfer occuring
166 retryList
.front()->onRetryList(false);
167 retryList
.pop_front();
173 port
= findPort(pkt
->getAddr(), pkt
->getSrc());
175 //Snoop didn't succeed
176 addToRetryList(pktPort
);
180 assert(dest
>= 0 && dest
< interfaces
.size());
181 assert(dest
!= pkt
->getSrc()); // catch infinite loops
182 port
= interfaces
[dest
];
187 if (port
->sendTiming(pkt
)) {
188 // Packet was successfully sent. Return true.
189 // Also take care of retries
191 DPRINTF(Bus
, "Remove retry from list %i\n", retryList
.front());
192 retryList
.front()->onRetryList(false);
193 retryList
.pop_front();
199 // Packet not successfully sent. Leave or put it on the retry list.
200 DPRINTF(Bus
, "Adding a retry to RETRY list %i\n", pktPort
);
201 addToRetryList(pktPort
);
206 Bus::recvRetry(int id
)
208 DPRINTF(Bus
, "Received a retry\n");
209 // If there's anything waiting...
210 if (retryList
.size()) {
211 //retryingPort = retryList.front();
213 DPRINTF(Bus
, "Sending a retry\n");
214 retryList
.front()->sendRetry();
215 // If inRetry is still true, sendTiming wasn't called
218 retryList
.front()->onRetryList(false);
219 retryList
.pop_front();
222 //Bring tickNextIdle up to the present
223 while (tickNextIdle
< curTick
)
224 tickNextIdle
+= clock
;
226 //Burn a cycle for the missed grant.
227 tickNextIdle
+= clock
;
229 if (!busIdle
.scheduled()) {
230 busIdle
.schedule(tickNextIdle
);
232 busIdle
.reschedule(tickNextIdle
);
239 Bus::findPort(Addr addr
, int id
)
241 /* An interval tree would be a better way to do this. --ali. */
247 while (i
< portList
.size() && !found
)
249 if (portList
[i
].range
== addr
) {
250 dest_id
= portList
[i
].portId
;
252 DPRINTF(Bus
, " found addr %#llx on device %d\n", addr
, dest_id
);
257 // Check if this matches the default range
259 for (iter
= defaultRange
.begin(); iter
!= defaultRange
.end(); iter
++) {
261 DPRINTF(Bus
, " found addr %#llx on default\n", addr
);
265 panic("Unable to find destination for addr: %#llx", addr
);
269 // we shouldn't be sending this back to where it came from
270 assert(dest_id
!= id
);
272 return interfaces
[dest_id
];
276 Bus::findSnoopPorts(Addr addr
, int id
)
280 std::vector
<int> ports
;
282 while (i
< portSnoopList
.size())
284 if (portSnoopList
[i
].range
== addr
&& portSnoopList
[i
].portId
!= id
) {
285 //Careful to not overlap ranges
286 //or snoop will be called more than once on the port
287 ports
.push_back(portSnoopList
[i
].portId
);
288 // DPRINTF(Bus, " found snoop addr %#llx on device%d\n", addr,
289 // portSnoopList[i].portId);
297 Bus::atomicSnoop(Packet
*pkt
)
299 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
301 while (!ports
.empty())
303 interfaces
[ports
.back()]->sendAtomic(pkt
);
309 Bus::functionalSnoop(Packet
*pkt
)
311 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
313 while (!ports
.empty())
315 interfaces
[ports
.back()]->sendFunctional(pkt
);
321 Bus::timingSnoop(Packet
*pkt
)
323 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
326 while (!ports
.empty() && success
)
328 success
= interfaces
[ports
.back()]->sendTiming(pkt
);
336 /** Function called by the port when the bus is receiving a Atomic
339 Bus::recvAtomic(Packet
*pkt
)
341 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
342 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
343 assert(pkt
->getDest() == Packet::Broadcast
);
345 return findPort(pkt
->getAddr(), pkt
->getSrc())->sendAtomic(pkt
);
348 /** Function called by the port when the bus is receiving a Functional
351 Bus::recvFunctional(Packet
*pkt
)
353 DPRINTF(Bus
, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
354 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
355 assert(pkt
->getDest() == Packet::Broadcast
);
356 functionalSnoop(pkt
);
357 findPort(pkt
->getAddr(), pkt
->getSrc())->sendFunctional(pkt
);
360 /** Function called by the port when the bus is receiving a status change.*/
362 Bus::recvStatusChange(Port::Status status
, int id
)
364 AddrRangeList ranges
;
365 AddrRangeList snoops
;
369 assert(status
== Port::RangeChange
&&
370 "The other statuses need to be implemented.");
372 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n", id
);
374 if (id
== defaultId
) {
375 defaultRange
.clear();
376 defaultPort
->getPeerAddressRanges(ranges
, snoops
);
377 assert(snoops
.size() == 0);
378 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
379 defaultRange
.push_back(*iter
);
380 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
381 iter
->start
, iter
->end
);
385 assert((id
< interfaces
.size() && id
>= 0) || id
== -1);
386 Port
*port
= interfaces
[id
];
387 std::vector
<DevMap
>::iterator portIter
;
388 std::vector
<DevMap
>::iterator snoopIter
;
390 // Clean out any previously existent ids
391 for (portIter
= portList
.begin(); portIter
!= portList
.end(); ) {
392 if (portIter
->portId
== id
)
393 portIter
= portList
.erase(portIter
);
398 for (snoopIter
= portSnoopList
.begin(); snoopIter
!= portSnoopList
.end(); ) {
399 if (snoopIter
->portId
== id
)
400 snoopIter
= portSnoopList
.erase(snoopIter
);
405 port
->getPeerAddressRanges(ranges
, snoops
);
407 for(iter
= snoops
.begin(); iter
!= snoops
.end(); iter
++) {
412 DPRINTF(BusAddrRanges
, "Adding snoop range %#llx - %#llx for id %d\n",
413 dm
.range
.start
, dm
.range
.end
, id
);
414 portSnoopList
.push_back(dm
);
417 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
422 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
423 dm
.range
.start
, dm
.range
.end
, id
);
424 portList
.push_back(dm
);
427 DPRINTF(MMU
, "port list has %d entries\n", portList
.size());
429 // tell all our peers that our address range has changed.
430 // Don't tell the device that caused this change, it already knows
431 for (x
= 0; x
< interfaces
.size(); x
++)
433 interfaces
[x
]->sendStatusChange(Port::RangeChange
);
435 if (id
!= defaultId
&& defaultPort
)
436 defaultPort
->sendStatusChange(Port::RangeChange
);
440 Bus::addressRanges(AddrRangeList
&resp
, AddrRangeList
&snoop
, int id
)
442 std::vector
<DevMap
>::iterator portIter
;
443 AddrRangeIter dflt_iter
;
449 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
451 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
453 resp
.push_back(*dflt_iter
);
454 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",dflt_iter
->start
,
457 for (portIter
= portList
.begin(); portIter
!= portList
.end(); portIter
++) {
459 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
461 if ((portIter
->range
.start
< dflt_iter
->start
&&
462 portIter
->range
.end
>= dflt_iter
->start
) ||
463 (portIter
->range
.start
< dflt_iter
->end
&&
464 portIter
->range
.end
>= dflt_iter
->end
))
465 fatal("Devices can not set ranges that itersect the default set\
466 but are not a subset of the default set.\n");
467 if (portIter
->range
.start
>= dflt_iter
->start
&&
468 portIter
->range
.end
<= dflt_iter
->end
) {
470 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
471 portIter
->range
.start
, portIter
->range
.end
);
474 if (portIter
->portId
!= id
&& !subset
) {
475 resp
.push_back(portIter
->range
);
476 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
477 portIter
->range
.start
, portIter
->range
.end
);
482 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus
)
488 END_DECLARE_SIM_OBJECT_PARAMS(Bus
)
490 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus
)
491 INIT_PARAM(bus_id
, "a globally unique bus id"),
492 INIT_PARAM(clock
, "bus clock speed"),
493 INIT_PARAM(width
, "width of the bus (bits)")
494 END_INIT_SIM_OBJECT_PARAMS(Bus
)
496 CREATE_SIM_OBJECT(Bus
)
498 return new Bus(getInstanceName(), bus_id
, clock
, width
);
501 REGISTER_SIM_OBJECT("Bus", Bus
)