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
)
75 void Bus::BusFreeEvent::process()
80 const char * Bus::BusFreeEvent::description()
82 return "bus became available";
85 /** Function called by the port when the bus is receiving a Timing
88 Bus::recvTiming(Packet
*pkt
)
91 DPRINTF(Bus
, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
92 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
94 Port
*pktPort
= interfaces
[pkt
->getSrc()];
96 // If the bus is busy, or other devices are in line ahead of the current
97 // one, put this device on the retry list.
98 if (tickNextIdle
> curTick
||
99 (retryList
.size() && pktPort
!= retryingPort
)) {
100 addToRetryList(pktPort
);
104 short dest
= pkt
->getDest();
105 if (dest
== Packet::Broadcast
) {
106 if (timingSnoop(pkt
)) {
107 pkt
->flags
|= SNOOP_COMMIT
;
108 bool success
= timingSnoop(pkt
);
110 if (pkt
->flags
& SATISFIED
) {
111 //Cache-Cache transfer occuring
113 retryList
.pop_front();
118 port
= findPort(pkt
->getAddr(), pkt
->getSrc());
120 //Snoop didn't succeed
121 addToRetryList(pktPort
);
125 assert(dest
>= 0 && dest
< interfaces
.size());
126 assert(dest
!= pkt
->getSrc()); // catch infinite loops
127 port
= interfaces
[dest
];
130 //Bring tickNextIdle up to the present tick
131 //There is some potential ambiguity where a cycle starts, which might make
132 //a difference when devices are acting right around a cycle boundary. Using
133 //a < allows things which happen exactly on a cycle boundary to take up only
134 //the following cycle. Anthing that happens later will have to "wait" for
135 //the end of that cycle, and then start using the bus after that.
136 while (tickNextIdle
< curTick
)
137 tickNextIdle
+= clock
;
139 // The packet will be sent. Figure out how long it occupies the bus, and
140 // how much of that time is for the first "word", aka bus width.
142 // Requests need one cycle to send an address
143 if (pkt
->isRequest())
145 else if (pkt
->isResponse() || pkt
->hasData()) {
146 // If a packet has data, it needs ceil(size/width) cycles to send it
147 // We're using the "adding instead of dividing" trick again here
148 if (pkt
->hasData()) {
149 int dataSize
= pkt
->getSize();
150 for (int transmitted
= 0; transmitted
< dataSize
;
151 transmitted
+= width
) {
155 // If the packet didn't have data, it must have been a response.
156 // Those use the bus for one cycle to send their data.
161 // The first word will be delivered after the current tick, the delivery
162 // of the address if any, and one bus cycle to deliver the data
165 pkt
->isRequest() ? clock
: 0 +
168 //Advance it numCycles bus cycles.
169 //XXX Should this use the repeated addition trick as well?
170 tickNextIdle
+= (numCycles
* clock
);
171 if (!busIdle
.scheduled()) {
172 busIdle
.schedule(tickNextIdle
);
174 busIdle
.reschedule(tickNextIdle
);
176 DPRINTF(Bus
, "The bus is now occupied from tick %d to %d\n",
177 curTick
, tickNextIdle
);
179 // The bus will become idle once the current packet is delivered.
180 pkt
->finishTime
= tickNextIdle
;
182 if (port
->sendTiming(pkt
)) {
183 // Packet was successfully sent. Return true.
184 // Also take care of retries
186 retryList
.pop_front();
192 // Packet not successfully sent. Leave or put it on the retry list.
193 addToRetryList(pktPort
);
198 Bus::recvRetry(int id
)
200 // If there's anything waiting...
201 if (retryList
.size()) {
202 retryingPort
= retryList
.front();
203 retryingPort
->sendRetry();
204 // If the retryingPort pointer isn't null, sendTiming wasn't called
206 warn("sendRetry didn't call sendTiming\n");
207 retryList
.pop_front();
214 Bus::findPort(Addr addr
, int id
)
216 /* An interval tree would be a better way to do this. --ali. */
222 while (i
< portList
.size() && !found
)
224 if (portList
[i
].range
== addr
) {
225 dest_id
= portList
[i
].portId
;
227 DPRINTF(Bus
, " found addr %#llx on device %d\n", addr
, dest_id
);
232 // Check if this matches the default range
234 for (iter
= defaultRange
.begin(); iter
!= defaultRange
.end(); iter
++) {
236 DPRINTF(Bus
, " found addr %#llx on default\n", addr
);
240 panic("Unable to find destination for addr: %#llx", addr
);
244 // we shouldn't be sending this back to where it came from
245 assert(dest_id
!= id
);
247 return interfaces
[dest_id
];
251 Bus::findSnoopPorts(Addr addr
, int id
)
255 std::vector
<int> ports
;
257 while (i
< portSnoopList
.size())
259 if (portSnoopList
[i
].range
== addr
&& portSnoopList
[i
].portId
!= id
) {
260 //Careful to not overlap ranges
261 //or snoop will be called more than once on the port
262 ports
.push_back(portSnoopList
[i
].portId
);
263 DPRINTF(Bus
, " found snoop addr %#llx on device%d\n", addr
,
264 portSnoopList
[i
].portId
);
272 Bus::atomicSnoop(Packet
*pkt
)
274 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
276 while (!ports
.empty())
278 interfaces
[ports
.back()]->sendAtomic(pkt
);
284 Bus::functionalSnoop(Packet
*pkt
)
286 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
288 while (!ports
.empty())
290 interfaces
[ports
.back()]->sendFunctional(pkt
);
296 Bus::timingSnoop(Packet
*pkt
)
298 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
301 while (!ports
.empty() && success
)
303 success
= interfaces
[ports
.back()]->sendTiming(pkt
);
311 /** Function called by the port when the bus is receiving a Atomic
314 Bus::recvAtomic(Packet
*pkt
)
316 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
317 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
318 assert(pkt
->getDest() == Packet::Broadcast
);
320 return findPort(pkt
->getAddr(), pkt
->getSrc())->sendAtomic(pkt
);
323 /** Function called by the port when the bus is receiving a Functional
326 Bus::recvFunctional(Packet
*pkt
)
328 DPRINTF(Bus
, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
329 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
330 assert(pkt
->getDest() == Packet::Broadcast
);
331 functionalSnoop(pkt
);
332 findPort(pkt
->getAddr(), pkt
->getSrc())->sendFunctional(pkt
);
335 /** Function called by the port when the bus is receiving a status change.*/
337 Bus::recvStatusChange(Port::Status status
, int id
)
339 AddrRangeList ranges
;
340 AddrRangeList snoops
;
344 assert(status
== Port::RangeChange
&&
345 "The other statuses need to be implemented.");
347 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n", id
);
349 if (id
== defaultId
) {
350 defaultRange
.clear();
351 defaultPort
->getPeerAddressRanges(ranges
, snoops
);
352 assert(snoops
.size() == 0);
353 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
354 defaultRange
.push_back(*iter
);
355 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
356 iter
->start
, iter
->end
);
360 assert((id
< interfaces
.size() && id
>= 0) || id
== -1);
361 Port
*port
= interfaces
[id
];
362 std::vector
<DevMap
>::iterator portIter
;
363 std::vector
<DevMap
>::iterator snoopIter
;
365 // Clean out any previously existent ids
366 for (portIter
= portList
.begin(); portIter
!= portList
.end(); ) {
367 if (portIter
->portId
== id
)
368 portIter
= portList
.erase(portIter
);
373 for (snoopIter
= portSnoopList
.begin(); snoopIter
!= portSnoopList
.end(); ) {
374 if (snoopIter
->portId
== id
)
375 snoopIter
= portSnoopList
.erase(snoopIter
);
380 port
->getPeerAddressRanges(ranges
, snoops
);
382 for(iter
= snoops
.begin(); iter
!= snoops
.end(); iter
++) {
387 DPRINTF(BusAddrRanges
, "Adding snoop range %#llx - %#llx for id %d\n",
388 dm
.range
.start
, dm
.range
.end
, id
);
389 portSnoopList
.push_back(dm
);
392 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
397 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
398 dm
.range
.start
, dm
.range
.end
, id
);
399 portList
.push_back(dm
);
402 DPRINTF(MMU
, "port list has %d entries\n", portList
.size());
404 // tell all our peers that our address range has changed.
405 // Don't tell the device that caused this change, it already knows
406 for (x
= 0; x
< interfaces
.size(); x
++)
408 interfaces
[x
]->sendStatusChange(Port::RangeChange
);
410 if (id
!= defaultId
&& defaultPort
)
411 defaultPort
->sendStatusChange(Port::RangeChange
);
415 Bus::addressRanges(AddrRangeList
&resp
, AddrRangeList
&snoop
, int id
)
417 std::vector
<DevMap
>::iterator portIter
;
418 AddrRangeIter dflt_iter
;
424 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
426 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
428 resp
.push_back(*dflt_iter
);
429 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",dflt_iter
->start
,
432 for (portIter
= portList
.begin(); portIter
!= portList
.end(); portIter
++) {
434 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
436 if ((portIter
->range
.start
< dflt_iter
->start
&&
437 portIter
->range
.end
>= dflt_iter
->start
) ||
438 (portIter
->range
.start
< dflt_iter
->end
&&
439 portIter
->range
.end
>= dflt_iter
->end
))
440 fatal("Devices can not set ranges that itersect the default set\
441 but are not a subset of the default set.\n");
442 if (portIter
->range
.start
>= dflt_iter
->start
&&
443 portIter
->range
.end
<= dflt_iter
->end
) {
445 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
446 portIter
->range
.start
, portIter
->range
.end
);
449 if (portIter
->portId
!= id
&& !subset
) {
450 resp
.push_back(portIter
->range
);
451 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
452 portIter
->range
.start
, portIter
->range
.end
);
457 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus
)
463 END_DECLARE_SIM_OBJECT_PARAMS(Bus
)
465 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus
)
466 INIT_PARAM(bus_id
, "a globally unique bus id"),
467 INIT_PARAM(clock
, "bus clock speed"),
468 INIT_PARAM(width
, "width of the bus (bits)")
469 END_INIT_SIM_OBJECT_PARAMS(Bus
)
471 CREATE_SIM_OBJECT(Bus
)
473 return new Bus(getInstanceName(), bus_id
, clock
, width
);
476 REGISTER_SIM_OBJECT("Bus", Bus
)