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,
51 cachedBlockSizeValid
= false;
54 fatal("Default port already set\n");
57 if (if_name
== "functional") {
60 funcPort
= new BusPort(csprintf("%s-p%d-func", name(), id
), this, id
);
62 interfaces
[id
] = funcPort
;
67 // if_name ignored? forced to be empty?
69 assert(maxId
< std::numeric_limits
<typeof(maxId
)>::max());
70 BusPort
*bp
= new BusPort(csprintf("%s-p%d", name(), id
), this, id
);
72 cachedBlockSizeValid
= false;
77 Bus::deletePortRefs(Port
*p
)
80 BusPort
*bp
= dynamic_cast<BusPort
*>(p
);
82 panic("Couldn't convert Port* to BusPort*\n");
83 // If this is our one functional port
86 interfaces
.erase(bp
->getId());
90 /** Get the ranges of anyone other buses that we are connected to. */
94 m5::hash_map
<short,BusPort
*>::iterator intIter
;
96 for (intIter
= interfaces
.begin(); intIter
!= interfaces
.end(); intIter
++)
97 intIter
->second
->sendStatusChange(Port::RangeChange
);
100 Bus::BusFreeEvent::BusFreeEvent(Bus
*_bus
) : Event(&mainEventQueue
), bus(_bus
)
103 void Bus::BusFreeEvent::process()
108 const char * Bus::BusFreeEvent::description()
110 return "bus became available";
113 void Bus::occupyBus(PacketPtr pkt
)
115 //Bring tickNextIdle up to the present tick
116 //There is some potential ambiguity where a cycle starts, which might make
117 //a difference when devices are acting right around a cycle boundary. Using
118 //a < allows things which happen exactly on a cycle boundary to take up
119 //only the following cycle. Anything that happens later will have to "wait"
120 //for the end of that cycle, and then start using the bus after that.
121 if (tickNextIdle
< curTick
) {
122 tickNextIdle
= curTick
;
123 if (tickNextIdle
% clock
!= 0)
124 tickNextIdle
= curTick
- (curTick
% clock
) + clock
;
127 // The packet will be sent. Figure out how long it occupies the bus, and
128 // how much of that time is for the first "word", aka bus width.
130 // Requests need one cycle to send an address
131 if (pkt
->isRequest())
133 else if (pkt
->isResponse() || pkt
->hasData()) {
134 // If a packet has data, it needs ceil(size/width) cycles to send it
135 // We're using the "adding instead of dividing" trick again here
136 if (pkt
->hasData()) {
137 int dataSize
= pkt
->getSize();
138 numCycles
+= dataSize
/width
;
139 if (dataSize
% width
)
142 // If the packet didn't have data, it must have been a response.
143 // Those use the bus for one cycle to send their data.
148 // The first word will be delivered after the current tick, the delivery
149 // of the address if any, and one bus cycle to deliver the data
152 pkt
->isRequest() ? clock
: 0 +
155 //Advance it numCycles bus cycles.
156 //XXX Should this use the repeated addition trick as well?
157 tickNextIdle
+= (numCycles
* clock
);
158 if (!busIdle
.scheduled()) {
159 busIdle
.schedule(tickNextIdle
);
161 busIdle
.reschedule(tickNextIdle
);
163 DPRINTF(Bus
, "The bus is now occupied from tick %d to %d\n",
164 curTick
, tickNextIdle
);
166 // The bus will become idle once the current packet is delivered.
167 pkt
->finishTime
= tickNextIdle
;
170 /** Function called by the port when the bus is receiving a Timing
173 Bus::recvTiming(PacketPtr pkt
)
176 DPRINTF(Bus
, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
177 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
180 if (pkt
->getSrc() == defaultId
)
181 pktPort
= defaultPort
;
182 else pktPort
= interfaces
[pkt
->getSrc()];
184 // If the bus is busy, or other devices are in line ahead of the current
185 // one, put this device on the retry list.
186 if (!(pkt
->isResponse() || pkt
->isExpressSnoop()) &&
187 (tickNextIdle
> curTick
||
188 (retryList
.size() && (!inRetry
|| pktPort
!= retryList
.front()))))
190 addToRetryList(pktPort
);
191 DPRINTF(Bus
, "recvTiming: Bus is busy, returning false\n");
195 short dest
= pkt
->getDest();
197 if (dest
== Packet::Broadcast
) {
198 port_id
= findPort(pkt
->getAddr());
199 timingSnoop(pkt
, interfaces
[port_id
]);
201 if (pkt
->memInhibitAsserted()) {
202 //Cache-Cache transfer occuring
204 retryList
.front()->onRetryList(false);
205 retryList
.pop_front();
209 DPRINTF(Bus
, "recvTiming: Packet sucessfully sent\n");
213 assert(dest
>= 0 && dest
< maxId
);
214 assert(dest
!= pkt
->getSrc()); // catch infinite loops
220 if (port_id
!= pkt
->getSrc()) {
221 if (interfaces
[port_id
]->sendTiming(pkt
)) {
222 // Packet was successfully sent. Return true.
223 // Also take care of retries
225 DPRINTF(Bus
, "Remove retry from list %d\n",
226 retryList
.front()->getId());
227 retryList
.front()->onRetryList(false);
228 retryList
.pop_front();
234 // Packet not successfully sent. Leave or put it on the retry list.
235 // illegal to block responses... can lead to deadlock
236 assert(!pkt
->isResponse());
237 DPRINTF(Bus
, "Adding2 a retry to RETRY list %d\n",
239 addToRetryList(pktPort
);
243 //Forwarding up from responder, just return true;
244 DPRINTF(Bus
, "recvTiming: can we be here?\n");
250 Bus::recvRetry(int id
)
252 // If there's anything waiting, and the bus isn't busy...
253 if (retryList
.size() && curTick
>= tickNextIdle
) {
254 //retryingPort = retryList.front();
256 DPRINTF(Bus
, "Sending a retry to %s\n", retryList
.front()->getPeer()->name());
257 retryList
.front()->sendRetry();
258 // If inRetry is still true, sendTiming wasn't called
261 retryList
.front()->onRetryList(false);
262 retryList
.pop_front();
265 //Bring tickNextIdle up to the present
266 while (tickNextIdle
< curTick
)
267 tickNextIdle
+= clock
;
269 //Burn a cycle for the missed grant.
270 tickNextIdle
+= clock
;
272 busIdle
.reschedule(tickNextIdle
, true);
275 //If we weren't able to drain before, we might be able to now.
276 if (drainEvent
&& retryList
.size() == 0 && curTick
>= tickNextIdle
) {
277 drainEvent
->process();
278 // Clear the drain event once we're done with it.
284 Bus::findPort(Addr addr
)
286 /* An interval tree would be a better way to do this. --ali. */
289 PortIter i
= portMap
.find(RangeSize(addr
,1));
290 if (i
!= portMap
.end())
293 // Check if this matches the default range
295 for (AddrRangeIter iter
= defaultRange
.begin();
296 iter
!= defaultRange
.end(); iter
++) {
298 DPRINTF(Bus
, " found addr %#llx on default\n", addr
);
304 panic("Unable to find destination for addr (user set default "
305 "responder): %#llx", addr
);
307 DPRINTF(Bus
, "Unable to find destination for addr: %#llx, will use "
308 "default port", addr
);
318 Bus::functionalSnoop(PacketPtr pkt
, Port
*responder
)
320 // The packet may be changed by another bus on snoops, restore the
322 int src_id
= pkt
->getSrc();
324 assert(pkt
->isRequest()); // hasn't already been satisfied
326 for (SnoopIter s_iter
= snoopPorts
.begin();
327 s_iter
!= snoopPorts
.end();
329 BusPort
*p
= *s_iter
;
330 if (p
!= responder
&& p
->getId() != src_id
) {
331 p
->sendFunctional(pkt
);
333 if (pkt
->isResponse()) {
341 Bus::timingSnoop(PacketPtr pkt
, Port
* responder
)
343 for (SnoopIter s_iter
= snoopPorts
.begin();
344 s_iter
!= snoopPorts
.end();
346 BusPort
*p
= *s_iter
;
347 if (p
!= responder
&& p
->getId() != pkt
->getSrc()) {
348 bool success
= p
->sendTiming(pkt
);
358 /** Function called by the port when the bus is receiving a Atomic
361 Bus::recvAtomic(PacketPtr pkt
)
363 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
364 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
365 assert(pkt
->getDest() == Packet::Broadcast
);
366 assert(pkt
->isRequest());
368 // Variables for recording original command and snoop response (if
369 // any)... if a snooper respondes, we will need to restore
370 // original command so that additional snoops can take place
372 MemCmd orig_cmd
= pkt
->cmd
;
373 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
374 Tick snoop_response_latency
= 0;
375 int orig_src
= pkt
->getSrc();
377 int target_port_id
= findPort(pkt
->getAddr());
378 Port
*target_port
= interfaces
[target_port_id
];
380 SnoopIter s_end
= snoopPorts
.end();
381 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= s_end
; s_iter
++) {
382 BusPort
*p
= *s_iter
;
383 // same port should not have both target addresses and snooping
384 assert(p
!= target_port
);
385 if (p
->getId() != pkt
->getSrc()) {
386 Tick latency
= p
->sendAtomic(pkt
);
387 if (pkt
->isResponse()) {
388 // response from snoop agent
389 assert(pkt
->cmd
!= orig_cmd
);
390 assert(pkt
->memInhibitAsserted());
391 // should only happen once
392 assert(snoop_response_cmd
== MemCmd::InvalidCmd
);
393 // save response state
394 snoop_response_cmd
= pkt
->cmd
;
395 snoop_response_latency
= latency
;
396 // restore original packet state for remaining snoopers
398 pkt
->setSrc(orig_src
);
399 pkt
->setDest(Packet::Broadcast
);
404 Tick response_latency
= 0;
406 // we can get requests sent up from the memory side of the bus for
407 // snooping... don't send them back down!
408 if (target_port_id
!= pkt
->getSrc()) {
409 response_latency
= target_port
->sendAtomic(pkt
);
412 // if we got a response from a snooper, restore it here
413 if (snoop_response_cmd
!= MemCmd::InvalidCmd
) {
414 // no one else should have responded
415 assert(!pkt
->isResponse());
416 assert(pkt
->cmd
== orig_cmd
);
417 pkt
->cmd
= snoop_response_cmd
;
418 response_latency
= snoop_response_latency
;
421 // why do we have this packet field and the return value both???
422 pkt
->finishTime
= curTick
+ response_latency
;
423 return response_latency
;
426 /** Function called by the port when the bus is receiving a Functional
429 Bus::recvFunctional(PacketPtr pkt
)
431 DPRINTF(Bus
, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
432 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
433 assert(pkt
->getDest() == Packet::Broadcast
);
435 int port_id
= findPort(pkt
->getAddr());
436 Port
*port
= interfaces
[port_id
];
437 functionalSnoop(pkt
, port
);
439 // If the snooping hasn't found what we were looking for, keep going.
440 if (!pkt
->isResponse() && port_id
!= pkt
->getSrc()) {
441 port
->sendFunctional(pkt
);
445 /** Function called by the port when the bus is receiving a status change.*/
447 Bus::recvStatusChange(Port::Status status
, int id
)
449 AddrRangeList ranges
;
453 assert(status
== Port::RangeChange
&&
454 "The other statuses need to be implemented.");
456 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n", id
);
458 if (id
== defaultId
) {
459 defaultRange
.clear();
460 // Only try to update these ranges if the user set a default responder.
462 defaultPort
->getPeerAddressRanges(ranges
, snoops
);
463 assert(snoops
== false);
464 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
465 defaultRange
.push_back(*iter
);
466 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
467 iter
->start
, iter
->end
);
472 assert((id
< maxId
&& id
>= 0) || id
== defaultId
);
473 BusPort
*port
= interfaces
[id
];
475 // Clean out any previously existent ids
476 for (PortIter portIter
= portMap
.begin();
477 portIter
!= portMap
.end(); ) {
478 if (portIter
->second
== id
)
479 portMap
.erase(portIter
++);
484 for (SnoopIter s_iter
= snoopPorts
.begin();
485 s_iter
!= snoopPorts
.end(); ) {
486 if ((*s_iter
)->getId() == id
)
487 s_iter
= snoopPorts
.erase(s_iter
);
492 port
->getPeerAddressRanges(ranges
, snoops
);
495 DPRINTF(BusAddrRanges
, "Adding id %d to snoop list\n", id
);
496 snoopPorts
.push_back(port
);
499 for (iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
500 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
501 iter
->start
, iter
->end
, id
);
502 if (portMap
.insert(*iter
, id
) == portMap
.end())
503 panic("Two devices with same range\n");
507 DPRINTF(MMU
, "port list has %d entries\n", portMap
.size());
509 // tell all our peers that our address range has changed.
510 // Don't tell the device that caused this change, it already knows
511 m5::hash_map
<short,BusPort
*>::iterator intIter
;
513 for (intIter
= interfaces
.begin(); intIter
!= interfaces
.end(); intIter
++)
514 if (intIter
->first
!= id
&& intIter
->first
!= funcPortId
)
515 intIter
->second
->sendStatusChange(Port::RangeChange
);
517 if (id
!= defaultId
&& defaultPort
)
518 defaultPort
->sendStatusChange(Port::RangeChange
);
522 Bus::addressRanges(AddrRangeList
&resp
, bool &snoop
, int id
)
527 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
529 for (AddrRangeIter dflt_iter
= defaultRange
.begin();
530 dflt_iter
!= defaultRange
.end(); dflt_iter
++) {
531 resp
.push_back(*dflt_iter
);
532 DPRINTF(BusAddrRanges
, " -- Dflt: %#llx : %#llx\n",dflt_iter
->start
,
535 for (PortIter portIter
= portMap
.begin();
536 portIter
!= portMap
.end(); portIter
++) {
538 for (AddrRangeIter dflt_iter
= defaultRange
.begin();
539 dflt_iter
!= defaultRange
.end(); dflt_iter
++) {
540 if ((portIter
->first
.start
< dflt_iter
->start
&&
541 portIter
->first
.end
>= dflt_iter
->start
) ||
542 (portIter
->first
.start
< dflt_iter
->end
&&
543 portIter
->first
.end
>= dflt_iter
->end
))
544 fatal("Devices can not set ranges that itersect the default set\
545 but are not a subset of the default set.\n");
546 if (portIter
->first
.start
>= dflt_iter
->start
&&
547 portIter
->first
.end
<= dflt_iter
->end
) {
549 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
550 portIter
->first
.start
, portIter
->first
.end
);
553 if (portIter
->second
!= id
&& !subset
) {
554 resp
.push_back(portIter
->first
);
555 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
556 portIter
->first
.start
, portIter
->first
.end
);
560 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= snoopPorts
.end();
562 if ((*s_iter
)->getId() != id
) {
570 Bus::findBlockSize(int id
)
572 if (cachedBlockSizeValid
)
573 return cachedBlockSize
;
577 for (PortIter portIter
= portMap
.begin();
578 portIter
!= portMap
.end(); portIter
++) {
579 int tmp_bs
= interfaces
[portIter
->second
]->peerBlockSize();
583 for (SnoopIter s_iter
= snoopPorts
.begin();
584 s_iter
!= snoopPorts
.end(); s_iter
++) {
585 int tmp_bs
= (*s_iter
)->peerBlockSize();
590 max_bs
= defaultBlockSize
;
593 warn_once("Blocksize found to not be 64... hmm... probably not.\n");
594 cachedBlockSize
= max_bs
;
595 cachedBlockSizeValid
= true;
601 Bus::drain(Event
* de
)
603 //We should check that we're not "doing" anything, and that noone is
604 //waiting. We might be idle but have someone waiting if the device we
605 //contacted for a retry didn't actually retry.
606 if (curTick
>= tickNextIdle
&& retryList
.size() == 0) {
617 if (tickNextIdle
< curTick
)
618 tickNextIdle
= (curTick
/ clock
) * clock
+ clock
;
621 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus
)
626 Param
<bool> responder_set
;
627 Param
<int> block_size
;
629 END_DECLARE_SIM_OBJECT_PARAMS(Bus
)
631 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus
)
632 INIT_PARAM(bus_id
, "a globally unique bus id"),
633 INIT_PARAM(clock
, "bus clock speed"),
634 INIT_PARAM(width
, "width of the bus (bits)"),
635 INIT_PARAM(responder_set
, "Is a default responder set by the user"),
636 INIT_PARAM(block_size
, "Default blocksize if no device has one")
637 END_INIT_SIM_OBJECT_PARAMS(Bus
)
639 CREATE_SIM_OBJECT(Bus
)
641 return new Bus(getInstanceName(), bus_id
, clock
, width
, responder_set
,
645 REGISTER_SIM_OBJECT("Bus", Bus
)