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_name ignored? forced to be empty?
58 assert(maxId
< std::numeric_limits
<typeof(maxId
)>::max());
59 BusPort
*bp
= new BusPort(csprintf("%s-p%d", name(), id
), this, id
);
65 Bus::deletePortRefs(Port
*p
)
67 BusPort
*bp
= dynamic_cast<BusPort
*>(p
);
69 panic("Couldn't convert Port* to BusPort*\n");
70 interfaces
.erase(bp
->getId());
73 /** Get the ranges of anyone other buses that we are connected to. */
77 m5::hash_map
<short,BusPort
*>::iterator intIter
;
79 for (intIter
= interfaces
.begin(); intIter
!= interfaces
.end(); intIter
++)
80 intIter
->second
->sendStatusChange(Port::RangeChange
);
83 Bus::BusFreeEvent::BusFreeEvent(Bus
*_bus
) : Event(&mainEventQueue
), bus(_bus
)
86 void Bus::BusFreeEvent::process()
91 const char * Bus::BusFreeEvent::description()
93 return "bus became available";
96 void Bus::occupyBus(PacketPtr pkt
)
98 //Bring tickNextIdle up to the present tick
99 //There is some potential ambiguity where a cycle starts, which might make
100 //a difference when devices are acting right around a cycle boundary. Using
101 //a < allows things which happen exactly on a cycle boundary to take up only
102 //the following cycle. Anthing that happens later will have to "wait" for
103 //the end of that cycle, and then start using the bus after that.
104 while (tickNextIdle
< curTick
)
105 tickNextIdle
+= clock
;
107 // The packet will be sent. Figure out how long it occupies the bus, and
108 // how much of that time is for the first "word", aka bus width.
110 // Requests need one cycle to send an address
111 if (pkt
->isRequest())
113 else if (pkt
->isResponse() || pkt
->hasData()) {
114 // If a packet has data, it needs ceil(size/width) cycles to send it
115 // We're using the "adding instead of dividing" trick again here
116 if (pkt
->hasData()) {
117 int dataSize
= pkt
->getSize();
118 for (int transmitted
= 0; transmitted
< dataSize
;
119 transmitted
+= width
) {
123 // If the packet didn't have data, it must have been a response.
124 // Those use the bus for one cycle to send their data.
129 // The first word will be delivered after the current tick, the delivery
130 // of the address if any, and one bus cycle to deliver the data
133 pkt
->isRequest() ? clock
: 0 +
136 //Advance it numCycles bus cycles.
137 //XXX Should this use the repeated addition trick as well?
138 tickNextIdle
+= (numCycles
* clock
);
139 if (!busIdle
.scheduled()) {
140 busIdle
.schedule(tickNextIdle
);
142 busIdle
.reschedule(tickNextIdle
);
144 DPRINTF(Bus
, "The bus is now occupied from tick %d to %d\n",
145 curTick
, tickNextIdle
);
147 // The bus will become idle once the current packet is delivered.
148 pkt
->finishTime
= tickNextIdle
;
151 /** Function called by the port when the bus is receiving a Timing
154 Bus::recvTiming(PacketPtr pkt
)
157 DPRINTF(Bus
, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
158 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
161 if (pkt
->getSrc() == defaultId
)
162 pktPort
= defaultPort
;
163 else pktPort
= interfaces
[pkt
->getSrc()];
165 // If the bus is busy, or other devices are in line ahead of the current
166 // one, put this device on the retry list.
167 if (tickNextIdle
> curTick
||
168 (retryList
.size() && (!inRetry
|| pktPort
!= retryList
.front()))) {
169 addToRetryList(pktPort
);
173 short dest
= pkt
->getDest();
175 // Make sure to clear the snoop commit flag so it doesn't think an
176 // access has been handled twice.
177 if (dest
== Packet::Broadcast
) {
178 port
= findPort(pkt
->getAddr(), pkt
->getSrc());
179 pkt
->flags
&= ~SNOOP_COMMIT
;
180 if (timingSnoop(pkt
, port
? port
: interfaces
[pkt
->getSrc()])) {
183 pkt
->flags
|= SNOOP_COMMIT
;
184 success
= timingSnoop(pkt
, port
? port
: interfaces
[pkt
->getSrc()]);
187 if (pkt
->flags
& SATISFIED
) {
188 //Cache-Cache transfer occuring
190 retryList
.front()->onRetryList(false);
191 retryList
.pop_front();
198 //Snoop didn't succeed
199 DPRINTF(Bus
, "Adding a retry to RETRY list %d\n",
201 addToRetryList(pktPort
);
205 assert(dest
>= 0 && dest
< maxId
);
206 assert(dest
!= pkt
->getSrc()); // catch infinite loops
207 port
= interfaces
[dest
];
213 if (port
->sendTiming(pkt
)) {
214 // Packet was successfully sent. Return true.
215 // Also take care of retries
217 DPRINTF(Bus
, "Remove retry from list %d\n",
218 retryList
.front()->getId());
219 retryList
.front()->onRetryList(false);
220 retryList
.pop_front();
226 // Packet not successfully sent. Leave or put it on the retry list.
227 DPRINTF(Bus
, "Adding a retry to RETRY list %d\n",
229 addToRetryList(pktPort
);
233 //Forwarding up from responder, just return true;
239 Bus::recvRetry(int id
)
241 DPRINTF(Bus
, "Received a retry\n");
242 // If there's anything waiting, and the bus isn't busy...
243 if (retryList
.size() && curTick
>= tickNextIdle
) {
244 //retryingPort = retryList.front();
246 DPRINTF(Bus
, "Sending a retry\n");
247 retryList
.front()->sendRetry();
248 // If inRetry is still true, sendTiming wasn't called
251 retryList
.front()->onRetryList(false);
252 retryList
.pop_front();
255 //Bring tickNextIdle up to the present
256 while (tickNextIdle
< curTick
)
257 tickNextIdle
+= clock
;
259 //Burn a cycle for the missed grant.
260 tickNextIdle
+= clock
;
262 if (!busIdle
.scheduled()) {
263 busIdle
.schedule(tickNextIdle
);
265 busIdle
.reschedule(tickNextIdle
);
269 //If we weren't able to drain before, we might be able to now.
270 if (drainEvent
&& retryList
.size() == 0 && curTick
>= tickNextIdle
) {
271 drainEvent
->process();
272 // Clear the drain event once we're done with it.
278 Bus::findPort(Addr addr
, int id
)
280 /* An interval tree would be a better way to do this. --ali. */
283 range_map
<Addr
,int>::iterator i
;
285 i
= portMap
.find(RangeSize(addr
,1));
286 if (i
!= portMap
.end())
289 // Check if this matches the default range
291 for (iter
= defaultRange
.begin(); iter
!= defaultRange
.end(); iter
++) {
293 DPRINTF(Bus
, " found addr %#llx on default\n", addr
);
299 panic("Unable to find destination for addr (user set default "
300 "responder): %#llx", addr
);
302 DPRINTF(Bus
, "Unable to find destination for addr: %#llx, will use "
303 "default port", addr
);
310 // we shouldn't be sending this back to where it came from
311 // do the snoop access and then we should terminate
312 // the cyclical call.
316 return interfaces
[dest_id
];
320 Bus::findSnoopPorts(Addr addr
, int id
)
324 std::vector
<int> ports
;
326 while (i
< portSnoopList
.size())
328 if (portSnoopList
[i
].range
== addr
&& portSnoopList
[i
].portId
!= id
) {
329 //Careful to not overlap ranges
330 //or snoop will be called more than once on the port
332 //@todo Fix this hack because ranges are overlapping
333 //need to make sure we dont't create overlapping ranges
334 bool hack_overlap
= false;
335 int size
= ports
.size();
336 for (int j
=0; j
< size
; j
++) {
337 if (ports
[j
] == portSnoopList
[i
].portId
)
342 ports
.push_back(portSnoopList
[i
].portId
);
343 // DPRINTF(Bus, " found snoop addr %#llx on device%d\n", addr,
344 // portSnoopList[i].portId);
352 Bus::atomicSnoop(PacketPtr pkt
, Port
*responder
)
354 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
355 Tick response_time
= 0;
357 while (!ports
.empty())
359 if (interfaces
[ports
.back()] != responder
) {
360 Tick response
= interfaces
[ports
.back()]->sendAtomic(pkt
);
362 assert(!response_time
); //Multiple responders
363 response_time
= response
;
368 return response_time
;
372 Bus::functionalSnoop(PacketPtr pkt
, Port
*responder
)
374 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
376 //The packet may be changed by another bus on snoops, restore the id after each
377 int id
= pkt
->getSrc();
378 while (!ports
.empty() && pkt
->result
!= Packet::Success
)
380 if (interfaces
[ports
.back()] != responder
)
381 interfaces
[ports
.back()]->sendFunctional(pkt
);
388 Bus::timingSnoop(PacketPtr pkt
, Port
* responder
)
390 std::vector
<int> ports
= findSnoopPorts(pkt
->getAddr(), pkt
->getSrc());
393 while (!ports
.empty() && success
)
395 if (interfaces
[ports
.back()] != responder
) //Don't call if responder also, once will do
396 success
= interfaces
[ports
.back()]->sendTiming(pkt
);
404 /** Function called by the port when the bus is receiving a Atomic
407 Bus::recvAtomic(PacketPtr pkt
)
409 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
410 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
411 assert(pkt
->getDest() == Packet::Broadcast
);
412 pkt
->flags
|= SNOOP_COMMIT
;
414 // Assume one bus cycle in order to get through. This may have
415 // some clock skew issues yet again...
416 pkt
->finishTime
= curTick
+ clock
;
418 Port
*port
= findPort(pkt
->getAddr(), pkt
->getSrc());
419 Tick snoopTime
= atomicSnoop(pkt
, port
? port
: interfaces
[pkt
->getSrc()]);
422 return snoopTime
; //Snoop satisfies it
424 return port
->sendAtomic(pkt
);
429 /** Function called by the port when the bus is receiving a Functional
432 Bus::recvFunctional(PacketPtr pkt
)
434 DPRINTF(Bus
, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
435 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
436 assert(pkt
->getDest() == Packet::Broadcast
);
437 pkt
->flags
|= SNOOP_COMMIT
;
439 Port
* port
= findPort(pkt
->getAddr(), pkt
->getSrc());
440 functionalSnoop(pkt
, port
? port
: interfaces
[pkt
->getSrc()]);
442 // If the snooping found what we were looking for, we're done.
443 if (pkt
->result
!= Packet::Success
&& port
) {
444 port
->sendFunctional(pkt
);
448 /** Function called by the port when the bus is receiving a status change.*/
450 Bus::recvStatusChange(Port::Status status
, int id
)
452 AddrRangeList ranges
;
453 AddrRangeList snoops
;
456 assert(status
== Port::RangeChange
&&
457 "The other statuses need to be implemented.");
459 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n", id
);
461 if (id
== defaultId
) {
462 defaultRange
.clear();
463 // Only try to update these ranges if the user set a default responder.
465 defaultPort
->getPeerAddressRanges(ranges
, snoops
);
466 assert(snoops
.size() == 0);
467 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
468 defaultRange
.push_back(*iter
);
469 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
470 iter
->start
, iter
->end
);
475 assert((id
< maxId
&& id
>= 0) || id
== defaultId
);
476 Port
*port
= interfaces
[id
];
477 range_map
<Addr
,int>::iterator portIter
;
478 std::vector
<DevMap
>::iterator snoopIter
;
480 // Clean out any previously existent ids
481 for (portIter
= portMap
.begin(); portIter
!= portMap
.end(); ) {
482 if (portIter
->second
== id
)
483 portMap
.erase(portIter
++);
488 for (snoopIter
= portSnoopList
.begin(); snoopIter
!= portSnoopList
.end(); ) {
489 if (snoopIter
->portId
== id
)
490 snoopIter
= portSnoopList
.erase(snoopIter
);
495 port
->getPeerAddressRanges(ranges
, snoops
);
497 for(iter
= snoops
.begin(); iter
!= snoops
.end(); iter
++) {
502 //@todo, make sure we don't overlap ranges
503 DPRINTF(BusAddrRanges
, "Adding snoop range %#llx - %#llx for id %d\n",
504 dm
.range
.start
, dm
.range
.end
, id
);
505 portSnoopList
.push_back(dm
);
508 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
509 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
510 iter
->start
, iter
->end
, id
);
511 if (portMap
.insert(*iter
, id
) == portMap
.end())
512 panic("Two devices with same range\n");
516 DPRINTF(MMU
, "port list has %d entries\n", portMap
.size());
518 // tell all our peers that our address range has changed.
519 // Don't tell the device that caused this change, it already knows
520 m5::hash_map
<short,BusPort
*>::iterator intIter
;
522 for (intIter
= interfaces
.begin(); intIter
!= interfaces
.end(); intIter
++)
523 if (intIter
->first
!= id
)
524 intIter
->second
->sendStatusChange(Port::RangeChange
);
526 if (id
!= defaultId
&& defaultPort
)
527 defaultPort
->sendStatusChange(Port::RangeChange
);
531 Bus::addressRanges(AddrRangeList
&resp
, AddrRangeList
&snoop
, int id
)
533 std::vector
<DevMap
>::iterator snoopIter
;
534 range_map
<Addr
,int>::iterator portIter
;
535 AddrRangeIter dflt_iter
;
541 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
543 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
545 resp
.push_back(*dflt_iter
);
546 DPRINTF(BusAddrRanges
, " -- Dflt: %#llx : %#llx\n",dflt_iter
->start
,
549 for (portIter
= portMap
.begin(); portIter
!= portMap
.end(); portIter
++) {
551 for (dflt_iter
= defaultRange
.begin(); dflt_iter
!= defaultRange
.end();
553 if ((portIter
->first
.start
< dflt_iter
->start
&&
554 portIter
->first
.end
>= dflt_iter
->start
) ||
555 (portIter
->first
.start
< dflt_iter
->end
&&
556 portIter
->first
.end
>= dflt_iter
->end
))
557 fatal("Devices can not set ranges that itersect the default set\
558 but are not a subset of the default set.\n");
559 if (portIter
->first
.start
>= dflt_iter
->start
&&
560 portIter
->first
.end
<= dflt_iter
->end
) {
562 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
563 portIter
->first
.start
, portIter
->first
.end
);
566 if (portIter
->second
!= id
&& !subset
) {
567 resp
.push_back(portIter
->first
);
568 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
569 portIter
->first
.start
, portIter
->first
.end
);
573 for (snoopIter
= portSnoopList
.begin();
574 snoopIter
!= portSnoopList
.end(); snoopIter
++)
576 if (snoopIter
->portId
!= id
) {
577 snoop
.push_back(snoopIter
->range
);
578 DPRINTF(BusAddrRanges
, " -- Snoop: %#llx : %#llx\n",
579 snoopIter
->range
.start
, snoopIter
->range
.end
);
580 //@todo We need to properly insert snoop ranges
581 //not overlapping the ranges (multiple)
587 Bus::drain(Event
* de
)
589 //We should check that we're not "doing" anything, and that noone is
590 //waiting. We might be idle but have someone waiting if the device we
591 //contacted for a retry didn't actually retry.
592 if (curTick
>= tickNextIdle
&& retryList
.size() == 0) {
600 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus
)
605 Param
<bool> responder_set
;
607 END_DECLARE_SIM_OBJECT_PARAMS(Bus
)
609 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus
)
610 INIT_PARAM(bus_id
, "a globally unique bus id"),
611 INIT_PARAM(clock
, "bus clock speed"),
612 INIT_PARAM(width
, "width of the bus (bits)"),
613 INIT_PARAM(responder_set
, "Is a default responder set by the user")
614 END_INIT_SIM_OBJECT_PARAMS(Bus
)
616 CREATE_SIM_OBJECT(Bus
)
618 return new Bus(getInstanceName(), bus_id
, clock
, width
, responder_set
);
621 REGISTER_SIM_OBJECT("Bus", Bus
)