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 addToRetryList(pktPort
);
102 short dest
= pkt
->getDest();
103 if (dest
== Packet::Broadcast
) {
104 if (timingSnoop(pkt
)) {
105 pkt
->flags
|= SNOOP_COMMIT
;
106 bool success
= timingSnoop(pkt
);
108 if (pkt
->flags
& SATISFIED
) {
109 //Cache-Cache transfer occuring
111 retryList
.pop_front();
116 port
= findPort(pkt
->getAddr(), pkt
->getSrc());
118 //Snoop didn't succeed
119 addToRetryList(pktPort
);
123 assert(dest
>= 0 && dest
< interfaces
.size());
124 assert(dest
!= pkt
->getSrc()); // catch infinite loops
125 port
= interfaces
[dest
];
128 //Bring tickNextIdle up to the present tick
129 //There is some potential ambiguity where a cycle starts, which might make
130 //a difference when devices are acting right around a cycle boundary. Using
131 //a < allows things which happen exactly on a cycle boundary to take up only
132 //the following cycle. Anthing that happens later will have to "wait" for
133 //the end of that cycle, and then start using the bus after that.
134 while (tickNextIdle
< curTick
)
135 tickNextIdle
+= clock
;
137 // The packet will be sent. Figure out how long it occupies the bus, and
138 // how much of that time is for the first "word", aka bus width.
140 // Requests need one cycle to send an address
141 if (pkt
->isRequest())
143 else if (pkt
->isResponse() || pkt
->hasData()) {
144 // If a packet has data, it needs ceil(size/width) cycles to send it
145 // We're using the "adding instead of dividing" trick again here
146 if (pkt
->hasData()) {
147 int dataSize
= pkt
->getSize();
148 for (int transmitted
= 0; transmitted
< dataSize
;
149 transmitted
+= width
) {
153 // If the packet didn't have data, it must have been a response.
154 // Those use the bus for one cycle to send their data.
159 // The first word will be delivered after the current tick, the delivery
160 // of the address if any, and one bus cycle to deliver the data
163 pkt
->isRequest() ? clock
: 0 +
166 //Advance it numCycles bus cycles.
167 //XXX Should this use the repeated addition trick as well?
168 tickNextIdle
+= (numCycles
* clock
);
169 if (!busIdle
.scheduled()) {
170 busIdle
.schedule(tickNextIdle
);
172 busIdle
.reschedule(tickNextIdle
);
174 DPRINTF(Bus
, "The bus is now occupied from tick %d to %d\n",
175 curTick
, tickNextIdle
);
177 // The bus will become idle once the current packet is delivered.
178 pkt
->finishTime
= tickNextIdle
;
180 if (port
->sendTiming(pkt
)) {
181 // Packet was successfully sent. Return true.
182 // Also take care of retries
184 retryList
.pop_front();
190 // Packet not successfully sent. Leave or put it on the retry list.
191 addToRetryList(pktPort
);
196 Bus::recvRetry(int id
)
198 // If there's anything waiting...
199 if (retryList
.size()) {
200 //retryingPort = retryList.front();
202 retryList
.front()->sendRetry();
203 // If inRetry is still true, sendTiming wasn't called
205 panic("Port %s didn't call sendTiming in it's recvRetry\n",\
206 retryList
.front()->getPeer()->name());
212 Bus::findPort(Addr addr
, int id
)
214 /* An interval tree would be a better way to do this. --ali. */
220 while (i
< portList
.size() && !found
)
222 if (portList
[i
].range
== addr
) {
223 dest_id
= portList
[i
].portId
;
225 DPRINTF(Bus
, " found addr %#llx on device %d\n", addr
, dest_id
);
230 // Check if this matches the default range
232 for (iter
= defaultRange
.begin(); iter
!= defaultRange
.end(); iter
++) {
234 DPRINTF(Bus
, " found addr %#llx on default\n", addr
);
238 panic("Unable to find destination for addr: %#llx", addr
);
242 // we shouldn't be sending this back to where it came from
243 assert(dest_id
!= id
);
245 return interfaces
[dest_id
];
249 Bus::findSnoopPorts(Addr addr
, int id
)
253 std::vector
<int> ports
;
255 while (i
< portSnoopList
.size())
257 if (portSnoopList
[i
].range
== addr
&& portSnoopList
[i
].portId
!= id
) {
258 //Careful to not overlap ranges
259 //or snoop will be called more than once on the port
260 ports
.push_back(portSnoopList
[i
].portId
);
261 DPRINTF(Bus
, " found snoop addr %#llx on device%d\n", addr
,
262 portSnoopList
[i
].portId
);
270 Bus::atomicSnoop(Packet
*pkt
)
272 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
274 while (!ports
.empty())
276 interfaces
[ports
.back()]->sendAtomic(pkt
);
282 Bus::functionalSnoop(Packet
*pkt
)
284 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
286 while (!ports
.empty())
288 interfaces
[ports
.back()]->sendFunctional(pkt
);
294 Bus::timingSnoop(Packet
*pkt
)
296 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
299 while (!ports
.empty() && success
)
301 success
= interfaces
[ports
.back()]->sendTiming(pkt
);
309 /** Function called by the port when the bus is receiving a Atomic
312 Bus::recvAtomic(Packet
*pkt
)
314 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
315 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
316 assert(pkt
->getDest() == Packet::Broadcast
);
318 return findPort(pkt
->getAddr(), pkt
->getSrc())->sendAtomic(pkt
);
321 /** Function called by the port when the bus is receiving a Functional
324 Bus::recvFunctional(Packet
*pkt
)
326 DPRINTF(Bus
, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
327 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
328 assert(pkt
->getDest() == Packet::Broadcast
);
329 functionalSnoop(pkt
);
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
)