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.
39 #include "base/misc.hh"
40 #include "base/trace.hh"
42 #include "sim/builder.hh"
45 Bus::getPort(const std::string
&if_name
, int idx
)
47 if (if_name
== "default") {
48 if (defaultPort
== NULL
) {
49 defaultPort
= new BusPort(csprintf("%s-default",name()), this,
53 fatal("Default port already set\n");
56 if (if_name
== "functional") {
59 funcPort
= new BusPort(csprintf("%s-p%d-func", name(), id
), this, id
);
61 interfaces
[id
] = funcPort
;
66 // if_name ignored? forced to be empty?
68 assert(maxId
< std::numeric_limits
<typeof(maxId
)>::max());
69 BusPort
*bp
= new BusPort(csprintf("%s-p%d", name(), id
), this, id
);
75 Bus::deletePortRefs(Port
*p
)
78 BusPort
*bp
= dynamic_cast<BusPort
*>(p
);
80 panic("Couldn't convert Port* to BusPort*\n");
81 // If this is our one functional port
84 interfaces
.erase(bp
->getId());
88 /** Get the ranges of anyone other buses that we are connected to. */
92 m5::hash_map
<short,BusPort
*>::iterator intIter
;
94 for (intIter
= interfaces
.begin(); intIter
!= interfaces
.end(); intIter
++)
95 intIter
->second
->sendStatusChange(Port::RangeChange
);
98 Bus::BusFreeEvent::BusFreeEvent(Bus
*_bus
) : Event(&mainEventQueue
), bus(_bus
)
101 void Bus::BusFreeEvent::process()
106 const char * Bus::BusFreeEvent::description()
108 return "bus became available";
111 void Bus::occupyBus(PacketPtr pkt
)
113 //Bring tickNextIdle up to the present tick
114 //There is some potential ambiguity where a cycle starts, which might make
115 //a difference when devices are acting right around a cycle boundary. Using
116 //a < allows things which happen exactly on a cycle boundary to take up only
117 //the following cycle. Anthing that happens later will have to "wait" for
118 //the end of that cycle, and then start using the bus after that.
119 while (tickNextIdle
< curTick
)
120 tickNextIdle
+= clock
;
122 // The packet will be sent. Figure out how long it occupies the bus, and
123 // how much of that time is for the first "word", aka bus width.
125 // Requests need one cycle to send an address
126 if (pkt
->isRequest())
128 else if (pkt
->isResponse() || pkt
->hasData()) {
129 // If a packet has data, it needs ceil(size/width) cycles to send it
130 // We're using the "adding instead of dividing" trick again here
131 if (pkt
->hasData()) {
132 int dataSize
= pkt
->getSize();
133 for (int transmitted
= 0; transmitted
< dataSize
;
134 transmitted
+= width
) {
138 // If the packet didn't have data, it must have been a response.
139 // Those use the bus for one cycle to send their data.
144 // The first word will be delivered after the current tick, the delivery
145 // of the address if any, and one bus cycle to deliver the data
148 pkt
->isRequest() ? clock
: 0 +
151 //Advance it numCycles bus cycles.
152 //XXX Should this use the repeated addition trick as well?
153 tickNextIdle
+= (numCycles
* clock
);
154 if (!busIdle
.scheduled()) {
155 busIdle
.schedule(tickNextIdle
);
157 busIdle
.reschedule(tickNextIdle
);
159 DPRINTF(Bus
, "The bus is now occupied from tick %d to %d\n",
160 curTick
, tickNextIdle
);
162 // The bus will become idle once the current packet is delivered.
163 pkt
->finishTime
= tickNextIdle
;
166 /** Function called by the port when the bus is receiving a Timing
169 Bus::recvTiming(PacketPtr pkt
)
172 DPRINTF(Bus
, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
173 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
176 if (pkt
->getSrc() == defaultId
)
177 pktPort
= defaultPort
;
178 else pktPort
= interfaces
[pkt
->getSrc()];
180 // If the bus is busy, or other devices are in line ahead of the current
181 // one, put this device on the retry list.
182 if (tickNextIdle
> curTick
||
183 (retryList
.size() && (!inRetry
|| pktPort
!= retryList
.front()))) {
184 addToRetryList(pktPort
);
188 short dest
= pkt
->getDest();
190 // Make sure to clear the snoop commit flag so it doesn't think an
191 // access has been handled twice.
192 if (dest
== Packet::Broadcast
) {
193 port
= findPort(pkt
->getAddr(), pkt
->getSrc());
194 pkt
->flags
&= ~SNOOP_COMMIT
;
195 if (timingSnoop(pkt
, port
? port
: interfaces
[pkt
->getSrc()])) {
198 pkt
->flags
|= SNOOP_COMMIT
;
199 success
= timingSnoop(pkt
, port
? port
: interfaces
[pkt
->getSrc()]);
202 if (pkt
->flags
& SATISFIED
) {
203 //Cache-Cache transfer occuring
205 retryList
.front()->onRetryList(false);
206 retryList
.pop_front();
213 //Snoop didn't succeed
214 DPRINTF(Bus
, "Adding a retry to RETRY list %d\n",
216 addToRetryList(pktPort
);
220 assert(dest
>= 0 && dest
< maxId
);
221 assert(dest
!= pkt
->getSrc()); // catch infinite loops
222 port
= interfaces
[dest
];
228 if (port
->sendTiming(pkt
)) {
229 // Packet was successfully sent. Return true.
230 // Also take care of retries
232 DPRINTF(Bus
, "Remove retry from list %d\n",
233 retryList
.front()->getId());
234 retryList
.front()->onRetryList(false);
235 retryList
.pop_front();
241 // Packet not successfully sent. Leave or put it on the retry list.
242 DPRINTF(Bus
, "Adding a retry to RETRY list %d\n",
244 addToRetryList(pktPort
);
248 //Forwarding up from responder, just return true;
254 Bus::recvRetry(int id
)
256 DPRINTF(Bus
, "Received a retry\n");
257 // If there's anything waiting, and the bus isn't busy...
258 if (retryList
.size() && curTick
>= tickNextIdle
) {
259 //retryingPort = retryList.front();
261 DPRINTF(Bus
, "Sending a retry\n");
262 retryList
.front()->sendRetry();
263 // If inRetry is still true, sendTiming wasn't called
266 retryList
.front()->onRetryList(false);
267 retryList
.pop_front();
270 //Bring tickNextIdle up to the present
271 while (tickNextIdle
< curTick
)
272 tickNextIdle
+= clock
;
274 //Burn a cycle for the missed grant.
275 tickNextIdle
+= clock
;
277 if (!busIdle
.scheduled()) {
278 busIdle
.schedule(tickNextIdle
);
280 busIdle
.reschedule(tickNextIdle
);
284 //If we weren't able to drain before, we might be able to now.
285 if (drainEvent
&& retryList
.size() == 0 && curTick
>= tickNextIdle
) {
286 drainEvent
->process();
287 // Clear the drain event once we're done with it.
293 Bus::findPort(Addr addr
, int id
)
295 /* An interval tree would be a better way to do this. --ali. */
298 range_map
<Addr
,int>::iterator i
;
300 i
= portMap
.find(RangeSize(addr
,1));
301 if (i
!= portMap
.end())
304 // Check if this matches the default range
306 for (iter
= defaultRange
.begin(); iter
!= defaultRange
.end(); iter
++) {
308 DPRINTF(Bus
, " found addr %#llx on default\n", addr
);
314 panic("Unable to find destination for addr (user set default "
315 "responder): %#llx", addr
);
317 DPRINTF(Bus
, "Unable to find destination for addr: %#llx, will use "
318 "default port", addr
);
325 // we shouldn't be sending this back to where it came from
326 // do the snoop access and then we should terminate
327 // the cyclical call.
331 return interfaces
[dest_id
];
335 Bus::findSnoopPorts(Addr addr
, int id
)
339 std::vector
<int> ports
;
341 while (i
< portSnoopList
.size())
343 if (portSnoopList
[i
].range
== addr
&& portSnoopList
[i
].portId
!= id
) {
344 //Careful to not overlap ranges
345 //or snoop will be called more than once on the port
347 //@todo Fix this hack because ranges are overlapping
348 //need to make sure we dont't create overlapping ranges
349 bool hack_overlap
= false;
350 int size
= ports
.size();
351 for (int j
=0; j
< size
; j
++) {
352 if (ports
[j
] == portSnoopList
[i
].portId
)
357 ports
.push_back(portSnoopList
[i
].portId
);
358 // DPRINTF(Bus, " found snoop addr %#llx on device%d\n", addr,
359 // portSnoopList[i].portId);
367 Bus::atomicSnoop(PacketPtr pkt
, Port
*responder
)
369 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
370 Tick response_time
= 0;
372 while (!ports
.empty())
374 if (interfaces
[ports
.back()] != responder
) {
375 Tick response
= interfaces
[ports
.back()]->sendAtomic(pkt
);
377 assert(!response_time
); //Multiple responders
378 response_time
= response
;
383 return response_time
;
387 Bus::functionalSnoop(PacketPtr pkt
, Port
*responder
)
389 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
391 //The packet may be changed by another bus on snoops, restore the id after each
392 int id
= pkt
->getSrc();
393 while (!ports
.empty() && pkt
->result
!= Packet::Success
)
395 if (interfaces
[ports
.back()] != responder
)
396 interfaces
[ports
.back()]->sendFunctional(pkt
);
403 Bus::timingSnoop(PacketPtr pkt
, Port
* responder
)
405 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
408 while (!ports
.empty() && success
)
410 if (interfaces
[ports
.back()] != responder
) //Don't call if responder also, once will do
411 success
= interfaces
[ports
.back()]->sendTiming(pkt
);
419 /** Function called by the port when the bus is receiving a Atomic
422 Bus::recvAtomic(PacketPtr pkt
)
424 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
425 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
426 assert(pkt
->getDest() == Packet::Broadcast
);
427 pkt
->flags
|= SNOOP_COMMIT
;
429 // Assume one bus cycle in order to get through. This may have
430 // some clock skew issues yet again...
431 pkt
->finishTime
= curTick
+ clock
;
433 Port
*port
= findPort(pkt
->getAddr(), pkt
->getSrc());
434 Tick snoopTime
= atomicSnoop(pkt
, port
? port
: interfaces
[pkt
->getSrc()]);
437 return snoopTime
; //Snoop satisfies it
439 return port
->sendAtomic(pkt
);
444 /** Function called by the port when the bus is receiving a Functional
447 Bus::recvFunctional(PacketPtr pkt
)
449 DPRINTF(Bus
, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
450 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
451 assert(pkt
->getDest() == Packet::Broadcast
);
452 pkt
->flags
|= SNOOP_COMMIT
;
454 Port
* port
= findPort(pkt
->getAddr(), pkt
->getSrc());
455 functionalSnoop(pkt
, port
? port
: interfaces
[pkt
->getSrc()]);
457 // If the snooping found what we were looking for, we're done.
458 if (pkt
->result
!= Packet::Success
&& port
) {
459 port
->sendFunctional(pkt
);
463 /** Function called by the port when the bus is receiving a status change.*/
465 Bus::recvStatusChange(Port::Status status
, int id
)
467 AddrRangeList ranges
;
468 AddrRangeList snoops
;
471 assert(status
== Port::RangeChange
&&
472 "The other statuses need to be implemented.");
474 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n", id
);
476 if (id
== defaultId
) {
477 defaultRange
.clear();
478 // Only try to update these ranges if the user set a default responder.
480 defaultPort
->getPeerAddressRanges(ranges
, snoops
);
481 assert(snoops
.size() == 0);
482 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
483 defaultRange
.push_back(*iter
);
484 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
485 iter
->start
, iter
->end
);
490 assert((id
< maxId
&& id
>= 0) || id
== defaultId
);
491 Port
*port
= interfaces
[id
];
492 range_map
<Addr
,int>::iterator portIter
;
493 std::vector
<DevMap
>::iterator snoopIter
;
495 // Clean out any previously existent ids
496 for (portIter
= portMap
.begin(); portIter
!= portMap
.end(); ) {
497 if (portIter
->second
== id
)
498 portMap
.erase(portIter
++);
503 for (snoopIter
= portSnoopList
.begin(); snoopIter
!= portSnoopList
.end(); ) {
504 if (snoopIter
->portId
== id
)
505 snoopIter
= portSnoopList
.erase(snoopIter
);
510 port
->getPeerAddressRanges(ranges
, snoops
);
512 for(iter
= snoops
.begin(); iter
!= snoops
.end(); iter
++) {
517 //@todo, make sure we don't overlap ranges
518 DPRINTF(BusAddrRanges
, "Adding snoop range %#llx - %#llx for id %d\n",
519 dm
.range
.start
, dm
.range
.end
, id
);
520 portSnoopList
.push_back(dm
);
523 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
524 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
525 iter
->start
, iter
->end
, id
);
526 if (portMap
.insert(*iter
, id
) == portMap
.end())
527 panic("Two devices with same range\n");
531 DPRINTF(MMU
, "port list has %d entries\n", portMap
.size());
533 // tell all our peers that our address range has changed.
534 // Don't tell the device that caused this change, it already knows
535 m5::hash_map
<short,BusPort
*>::iterator intIter
;
537 for (intIter
= interfaces
.begin(); intIter
!= interfaces
.end(); intIter
++)
538 if (intIter
->first
!= id
&& intIter
->first
!= funcPortId
)
539 intIter
->second
->sendStatusChange(Port::RangeChange
);
541 if (id
!= defaultId
&& defaultPort
)
542 defaultPort
->sendStatusChange(Port::RangeChange
);
546 Bus::addressRanges(AddrRangeList
&resp
, AddrRangeList
&snoop
, int id
)
548 std::vector
<DevMap
>::iterator snoopIter
;
549 range_map
<Addr
,int>::iterator portIter
;
550 AddrRangeIter dflt_iter
;
556 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
558 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
560 resp
.push_back(*dflt_iter
);
561 DPRINTF(BusAddrRanges
, " -- Dflt: %#llx : %#llx\n",dflt_iter
->start
,
564 for (portIter
= portMap
.begin(); portIter
!= portMap
.end(); portIter
++) {
566 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
568 if ((portIter
->first
.start
< dflt_iter
->start
&&
569 portIter
->first
.end
>= dflt_iter
->start
) ||
570 (portIter
->first
.start
< dflt_iter
->end
&&
571 portIter
->first
.end
>= dflt_iter
->end
))
572 fatal("Devices can not set ranges that itersect the default set\
573 but are not a subset of the default set.\n");
574 if (portIter
->first
.start
>= dflt_iter
->start
&&
575 portIter
->first
.end
<= dflt_iter
->end
) {
577 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
578 portIter
->first
.start
, portIter
->first
.end
);
581 if (portIter
->second
!= id
&& !subset
) {
582 resp
.push_back(portIter
->first
);
583 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
584 portIter
->first
.start
, portIter
->first
.end
);
588 for (snoopIter
= portSnoopList
.begin();
589 snoopIter
!= portSnoopList
.end(); snoopIter
++)
591 if (snoopIter
->portId
!= id
) {
592 snoop
.push_back(snoopIter
->range
);
593 DPRINTF(BusAddrRanges
, " -- Snoop: %#llx : %#llx\n",
594 snoopIter
->range
.start
, snoopIter
->range
.end
);
595 //@todo We need to properly insert snoop ranges
596 //not overlapping the ranges (multiple)
602 Bus::drain(Event
* de
)
604 //We should check that we're not "doing" anything, and that noone is
605 //waiting. We might be idle but have someone waiting if the device we
606 //contacted for a retry didn't actually retry.
607 if (curTick
>= tickNextIdle
&& retryList
.size() == 0) {
615 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus
)
620 Param
<bool> responder_set
;
622 END_DECLARE_SIM_OBJECT_PARAMS(Bus
)
624 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus
)
625 INIT_PARAM(bus_id
, "a globally unique bus id"),
626 INIT_PARAM(clock
, "bus clock speed"),
627 INIT_PARAM(width
, "width of the bus (bits)"),
628 INIT_PARAM(responder_set
, "Is a default responder set by the user")
629 END_INIT_SIM_OBJECT_PARAMS(Bus
)
631 CREATE_SIM_OBJECT(Bus
)
633 return new Bus(getInstanceName(), bus_id
, clock
, width
, responder_set
);
636 REGISTER_SIM_OBJECT("Bus", Bus
)