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");
54 // if_name ignored? forced to be empty?
55 int id
= interfaces
.size();
56 BusPort
*bp
= new BusPort(csprintf("%s-p%d", name(), id
), this, id
);
57 interfaces
.push_back(bp
);
61 /** Get the ranges of anyone other buses that we are connected to. */
65 std::vector
<BusPort
*>::iterator intIter
;
67 for (intIter
= interfaces
.begin(); intIter
!= interfaces
.end(); intIter
++)
68 (*intIter
)->sendStatusChange(Port::RangeChange
);
71 Bus::BusFreeEvent::BusFreeEvent(Bus
*_bus
) : Event(&mainEventQueue
), bus(_bus
)
74 void Bus::BusFreeEvent::process()
79 const char * Bus::BusFreeEvent::description()
81 return "bus became available";
84 void Bus::occupyBus(PacketPtr pkt
)
86 //Bring tickNextIdle up to the present tick
87 //There is some potential ambiguity where a cycle starts, which might make
88 //a difference when devices are acting right around a cycle boundary. Using
89 //a < allows things which happen exactly on a cycle boundary to take up only
90 //the following cycle. Anthing that happens later will have to "wait" for
91 //the end of that cycle, and then start using the bus after that.
92 while (tickNextIdle
< curTick
)
93 tickNextIdle
+= clock
;
95 // The packet will be sent. Figure out how long it occupies the bus, and
96 // how much of that time is for the first "word", aka bus width.
98 // Requests need one cycle to send an address
101 else if (pkt
->isResponse() || pkt
->hasData()) {
102 // If a packet has data, it needs ceil(size/width) cycles to send it
103 // We're using the "adding instead of dividing" trick again here
104 if (pkt
->hasData()) {
105 int dataSize
= pkt
->getSize();
106 for (int transmitted
= 0; transmitted
< dataSize
;
107 transmitted
+= width
) {
111 // If the packet didn't have data, it must have been a response.
112 // Those use the bus for one cycle to send their data.
117 // The first word will be delivered after the current tick, the delivery
118 // of the address if any, and one bus cycle to deliver the data
121 pkt
->isRequest() ? clock
: 0 +
124 //Advance it numCycles bus cycles.
125 //XXX Should this use the repeated addition trick as well?
126 tickNextIdle
+= (numCycles
* clock
);
127 if (!busIdle
.scheduled()) {
128 busIdle
.schedule(tickNextIdle
);
130 busIdle
.reschedule(tickNextIdle
);
132 DPRINTF(Bus
, "The bus is now occupied from tick %d to %d\n",
133 curTick
, tickNextIdle
);
135 // The bus will become idle once the current packet is delivered.
136 pkt
->finishTime
= tickNextIdle
;
139 /** Function called by the port when the bus is receiving a Timing
142 Bus::recvTiming(PacketPtr pkt
)
145 DPRINTF(Bus
, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
146 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
149 if (pkt
->getSrc() == defaultId
)
150 pktPort
= defaultPort
;
151 else pktPort
= interfaces
[pkt
->getSrc()];
153 // If the bus is busy, or other devices are in line ahead of the current
154 // one, put this device on the retry list.
155 if (tickNextIdle
> curTick
||
156 (retryList
.size() && (!inRetry
|| pktPort
!= retryList
.front()))) {
157 addToRetryList(pktPort
);
161 short dest
= pkt
->getDest();
162 if (dest
== Packet::Broadcast
) {
163 port
= findPort(pkt
->getAddr(), pkt
->getSrc());
164 if (timingSnoop(pkt
, port
? port
: interfaces
[pkt
->getSrc()])) {
167 pkt
->flags
|= SNOOP_COMMIT
;
168 success
= timingSnoop(pkt
, port
? port
: interfaces
[pkt
->getSrc()]);
171 if (pkt
->flags
& SATISFIED
) {
172 //Cache-Cache transfer occuring
174 retryList
.front()->onRetryList(false);
175 retryList
.pop_front();
182 //Snoop didn't succeed
183 DPRINTF(Bus
, "Adding a retry to RETRY list %d\n",
185 addToRetryList(pktPort
);
189 assert(dest
>= 0 && dest
< interfaces
.size());
190 assert(dest
!= pkt
->getSrc()); // catch infinite loops
191 port
= interfaces
[dest
];
197 if (port
->sendTiming(pkt
)) {
198 // Packet was successfully sent. Return true.
199 // Also take care of retries
201 DPRINTF(Bus
, "Remove retry from list %d\n",
202 retryList
.front()->getId());
203 retryList
.front()->onRetryList(false);
204 retryList
.pop_front();
210 // Packet not successfully sent. Leave or put it on the retry list.
211 DPRINTF(Bus
, "Adding a retry to RETRY list %d\n",
213 addToRetryList(pktPort
);
217 //Forwarding up from responder, just return true;
223 Bus::recvRetry(int id
)
225 DPRINTF(Bus
, "Received a retry\n");
226 // If there's anything waiting, and the bus isn't busy...
227 if (retryList
.size() && curTick
>= tickNextIdle
) {
228 //retryingPort = retryList.front();
230 DPRINTF(Bus
, "Sending a retry\n");
231 retryList
.front()->sendRetry();
232 // If inRetry is still true, sendTiming wasn't called
235 retryList
.front()->onRetryList(false);
236 retryList
.pop_front();
239 //Bring tickNextIdle up to the present
240 while (tickNextIdle
< curTick
)
241 tickNextIdle
+= clock
;
243 //Burn a cycle for the missed grant.
244 tickNextIdle
+= clock
;
246 if (!busIdle
.scheduled()) {
247 busIdle
.schedule(tickNextIdle
);
249 busIdle
.reschedule(tickNextIdle
);
253 //If we weren't able to drain before, we might be able to now.
254 if (drainEvent
&& retryList
.size() == 0 && curTick
>= tickNextIdle
) {
255 drainEvent
->process();
256 // Clear the drain event once we're done with it.
262 Bus::findPort(Addr addr
, int id
)
264 /* An interval tree would be a better way to do this. --ali. */
267 range_map
<Addr
,int>::iterator i
;
269 i
= portMap
.find(RangeSize(addr
,1));
270 if (i
!= portMap
.end())
273 // Check if this matches the default range
275 for (iter
= defaultRange
.begin(); iter
!= defaultRange
.end(); iter
++) {
277 DPRINTF(Bus
, " found addr %#llx on default\n", addr
);
283 panic("Unable to find destination for addr (user set default "
284 "responder): %#llx", addr
);
286 DPRINTF(Bus
, "Unable to find destination for addr: %#llx, will use "
287 "default port", addr
);
294 // we shouldn't be sending this back to where it came from
295 // do the snoop access and then we should terminate
296 // the cyclical call.
300 return interfaces
[dest_id
];
304 Bus::findSnoopPorts(Addr addr
, int id
)
308 std::vector
<int> ports
;
310 while (i
< portSnoopList
.size())
312 if (portSnoopList
[i
].range
== addr
&& portSnoopList
[i
].portId
!= id
) {
313 //Careful to not overlap ranges
314 //or snoop will be called more than once on the port
316 //@todo Fix this hack because ranges are overlapping
317 //need to make sure we dont't create overlapping ranges
318 bool hack_overlap
= false;
319 int size
= ports
.size();
320 for (int j
=0; j
< size
; j
++) {
321 if (ports
[j
] == portSnoopList
[i
].portId
)
326 ports
.push_back(portSnoopList
[i
].portId
);
327 // DPRINTF(Bus, " found snoop addr %#llx on device%d\n", addr,
328 // portSnoopList[i].portId);
336 Bus::atomicSnoop(PacketPtr pkt
, Port
*responder
)
338 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
339 Tick response_time
= 0;
341 while (!ports
.empty())
343 if (interfaces
[ports
.back()] != responder
) {
344 Tick response
= interfaces
[ports
.back()]->sendAtomic(pkt
);
346 assert(!response_time
); //Multiple responders
347 response_time
= response
;
352 return response_time
;
356 Bus::functionalSnoop(PacketPtr pkt
, Port
*responder
)
358 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
360 //The packet may be changed by another bus on snoops, restore the id after each
361 int id
= pkt
->getSrc();
362 while (!ports
.empty() && pkt
->result
!= Packet::Success
)
364 if (interfaces
[ports
.back()] != responder
)
365 interfaces
[ports
.back()]->sendFunctional(pkt
);
372 Bus::timingSnoop(PacketPtr pkt
, Port
* responder
)
374 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
377 while (!ports
.empty() && success
)
379 if (interfaces
[ports
.back()] != responder
) //Don't call if responder also, once will do
380 success
= interfaces
[ports
.back()]->sendTiming(pkt
);
388 /** Function called by the port when the bus is receiving a Atomic
391 Bus::recvAtomic(PacketPtr pkt
)
393 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
394 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
395 assert(pkt
->getDest() == Packet::Broadcast
);
396 pkt
->flags
|= SNOOP_COMMIT
;
398 // Assume one bus cycle in order to get through. This may have
399 // some clock skew issues yet again...
400 pkt
->finishTime
= curTick
+ clock
;
402 Port
*port
= findPort(pkt
->getAddr(), pkt
->getSrc());
403 Tick snoopTime
= atomicSnoop(pkt
, port
? port
: interfaces
[pkt
->getSrc()]);
406 return snoopTime
; //Snoop satisfies it
408 return port
->sendAtomic(pkt
);
413 /** Function called by the port when the bus is receiving a Functional
416 Bus::recvFunctional(PacketPtr pkt
)
418 DPRINTF(Bus
, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
419 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
420 assert(pkt
->getDest() == Packet::Broadcast
);
421 pkt
->flags
|= SNOOP_COMMIT
;
423 Port
* port
= findPort(pkt
->getAddr(), pkt
->getSrc());
424 functionalSnoop(pkt
, port
? port
: interfaces
[pkt
->getSrc()]);
426 // If the snooping found what we were looking for, we're done.
427 if (pkt
->result
!= Packet::Success
&& port
) {
428 port
->sendFunctional(pkt
);
432 /** Function called by the port when the bus is receiving a status change.*/
434 Bus::recvStatusChange(Port::Status status
, int id
)
436 AddrRangeList ranges
;
437 AddrRangeList snoops
;
441 assert(status
== Port::RangeChange
&&
442 "The other statuses need to be implemented.");
444 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n", id
);
446 if (id
== defaultId
) {
447 defaultRange
.clear();
448 // Only try to update these ranges if the user set a default responder.
450 defaultPort
->getPeerAddressRanges(ranges
, snoops
);
451 assert(snoops
.size() == 0);
452 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
453 defaultRange
.push_back(*iter
);
454 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
455 iter
->start
, iter
->end
);
460 assert((id
< interfaces
.size() && id
>= 0) || id
== defaultId
);
461 Port
*port
= interfaces
[id
];
462 range_map
<Addr
,int>::iterator portIter
;
463 std::vector
<DevMap
>::iterator snoopIter
;
465 // Clean out any previously existent ids
466 for (portIter
= portMap
.begin(); portIter
!= portMap
.end(); ) {
467 if (portIter
->second
== id
)
468 portMap
.erase(portIter
++);
473 for (snoopIter
= portSnoopList
.begin(); snoopIter
!= portSnoopList
.end(); ) {
474 if (snoopIter
->portId
== id
)
475 snoopIter
= portSnoopList
.erase(snoopIter
);
480 port
->getPeerAddressRanges(ranges
, snoops
);
482 for(iter
= snoops
.begin(); iter
!= snoops
.end(); iter
++) {
487 //@todo, make sure we don't overlap ranges
488 DPRINTF(BusAddrRanges
, "Adding snoop range %#llx - %#llx for id %d\n",
489 dm
.range
.start
, dm
.range
.end
, id
);
490 portSnoopList
.push_back(dm
);
493 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
494 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
495 iter
->start
, iter
->end
, id
);
496 if (portMap
.insert(*iter
, id
) == portMap
.end())
497 panic("Two devices with same range\n");
501 DPRINTF(MMU
, "port list has %d entries\n", portMap
.size());
503 // tell all our peers that our address range has changed.
504 // Don't tell the device that caused this change, it already knows
505 for (x
= 0; x
< interfaces
.size(); x
++)
507 interfaces
[x
]->sendStatusChange(Port::RangeChange
);
509 if (id
!= defaultId
&& defaultPort
)
510 defaultPort
->sendStatusChange(Port::RangeChange
);
514 Bus::addressRanges(AddrRangeList
&resp
, AddrRangeList
&snoop
, int id
)
516 std::vector
<DevMap
>::iterator snoopIter
;
517 range_map
<Addr
,int>::iterator portIter
;
518 AddrRangeIter dflt_iter
;
524 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
526 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
528 resp
.push_back(*dflt_iter
);
529 DPRINTF(BusAddrRanges
, " -- Dflt: %#llx : %#llx\n",dflt_iter
->start
,
532 for (portIter
= portMap
.begin(); portIter
!= portMap
.end(); portIter
++) {
534 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
536 if ((portIter
->first
.start
< dflt_iter
->start
&&
537 portIter
->first
.end
>= dflt_iter
->start
) ||
538 (portIter
->first
.start
< dflt_iter
->end
&&
539 portIter
->first
.end
>= dflt_iter
->end
))
540 fatal("Devices can not set ranges that itersect the default set\
541 but are not a subset of the default set.\n");
542 if (portIter
->first
.start
>= dflt_iter
->start
&&
543 portIter
->first
.end
<= dflt_iter
->end
) {
545 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
546 portIter
->first
.start
, portIter
->first
.end
);
549 if (portIter
->second
!= id
&& !subset
) {
550 resp
.push_back(portIter
->first
);
551 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
552 portIter
->first
.start
, portIter
->first
.end
);
556 for (snoopIter
= portSnoopList
.begin();
557 snoopIter
!= portSnoopList
.end(); snoopIter
++)
559 if (snoopIter
->portId
!= id
) {
560 snoop
.push_back(snoopIter
->range
);
561 DPRINTF(BusAddrRanges
, " -- Snoop: %#llx : %#llx\n",
562 snoopIter
->range
.start
, snoopIter
->range
.end
);
563 //@todo We need to properly insert snoop ranges
564 //not overlapping the ranges (multiple)
570 Bus::drain(Event
* de
)
572 //We should check that we're not "doing" anything, and that noone is
573 //waiting. We might be idle but have someone waiting if the device we
574 //contacted for a retry didn't actually retry.
575 if (curTick
>= tickNextIdle
&& retryList
.size() == 0) {
583 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus
)
588 Param
<bool> responder_set
;
590 END_DECLARE_SIM_OBJECT_PARAMS(Bus
)
592 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus
)
593 INIT_PARAM(bus_id
, "a globally unique bus id"),
594 INIT_PARAM(clock
, "bus clock speed"),
595 INIT_PARAM(width
, "width of the bus (bits)"),
596 INIT_PARAM(responder_set
, "Is a default responder set by the user")
597 END_INIT_SIM_OBJECT_PARAMS(Bus
)
599 CREATE_SIM_OBJECT(Bus
)
601 return new Bus(getInstanceName(), bus_id
, clock
, width
, responder_set
);
604 REGISTER_SIM_OBJECT("Bus", Bus
)