ffd5e25a76ce9f9681dfd2cd5a8d83e188eb47da
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 result %d\n",
177 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString(),
181 if (pkt
->getSrc() == defaultId
)
182 pktPort
= defaultPort
;
183 else pktPort
= interfaces
[pkt
->getSrc()];
185 // If the bus is busy, or other devices are in line ahead of the current
186 // one, put this device on the retry list.
187 if (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 // Make sure to clear the snoop commit flag so it doesn't think an
198 // access has been handled twice.
199 if (dest
== Packet::Broadcast
) {
200 port
= findPort(pkt
->getAddr(), pkt
->getSrc());
201 timingSnoop(pkt
, port
? port
: interfaces
[pkt
->getSrc()]);
203 if (pkt
->memInhibitAsserted()) {
204 //Cache-Cache transfer occuring
206 retryList
.front()->onRetryList(false);
207 retryList
.pop_front();
211 DPRINTF(Bus
, "recvTiming: Packet sucessfully sent\n");
215 assert(dest
>= 0 && dest
< maxId
);
216 assert(dest
!= pkt
->getSrc()); // catch infinite loops
217 port
= interfaces
[dest
];
223 if (port
->sendTiming(pkt
)) {
224 // Packet was successfully sent. Return true.
225 // Also take care of retries
227 DPRINTF(Bus
, "Remove retry from list %d\n",
228 retryList
.front()->getId());
229 retryList
.front()->onRetryList(false);
230 retryList
.pop_front();
236 // Packet not successfully sent. Leave or put it on the retry list.
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 DPRINTF(Bus
, "Received a retry from %s\n", id
== -1 ? "self" : interfaces
[id
]->getPeer()->name());
253 // If there's anything waiting, and the bus isn't busy...
254 if (retryList
.size() && curTick
>= tickNextIdle
) {
255 //retryingPort = retryList.front();
257 DPRINTF(Bus
, "Sending a retry to %s\n", retryList
.front()->getPeer()->name());
258 retryList
.front()->sendRetry();
259 // If inRetry is still true, sendTiming wasn't called
262 retryList
.front()->onRetryList(false);
263 retryList
.pop_front();
266 //Bring tickNextIdle up to the present
267 while (tickNextIdle
< curTick
)
268 tickNextIdle
+= clock
;
270 //Burn a cycle for the missed grant.
271 tickNextIdle
+= clock
;
273 busIdle
.reschedule(tickNextIdle
, true);
276 //If we weren't able to drain before, we might be able to now.
277 if (drainEvent
&& retryList
.size() == 0 && curTick
>= tickNextIdle
) {
278 drainEvent
->process();
279 // Clear the drain event once we're done with it.
285 Bus::findPort(Addr addr
, int id
)
287 /* An interval tree would be a better way to do this. --ali. */
290 PortIter i
= portMap
.find(RangeSize(addr
,1));
291 if (i
!= portMap
.end())
294 // Check if this matches the default range
296 for (AddrRangeIter iter
= defaultRange
.begin();
297 iter
!= defaultRange
.end(); iter
++) {
299 DPRINTF(Bus
, " found addr %#llx on default\n", addr
);
305 panic("Unable to find destination for addr (user set default "
306 "responder): %#llx", addr
);
308 DPRINTF(Bus
, "Unable to find destination for addr: %#llx, will use "
309 "default port", addr
);
316 // we shouldn't be sending this back to where it came from
317 // do the snoop access and then we should terminate
318 // the cyclical call.
322 return interfaces
[dest_id
];
326 Bus::functionalSnoop(PacketPtr pkt
, Port
*responder
)
328 // The packet may be changed by another bus on snoops, restore the
330 int src_id
= pkt
->getSrc();
332 for (SnoopIter s_iter
= snoopPorts
.begin();
333 s_iter
!= snoopPorts
.end();
335 BusPort
*p
= *s_iter
;
336 if (p
!= responder
&& p
->getId() != src_id
) {
337 p
->sendFunctional(pkt
);
339 if (pkt
->result
== Packet::Success
) {
347 Bus::timingSnoop(PacketPtr pkt
, Port
* responder
)
349 for (SnoopIter s_iter
= snoopPorts
.begin();
350 s_iter
!= snoopPorts
.end();
352 BusPort
*p
= *s_iter
;
353 if (p
!= responder
&& p
->getId() != pkt
->getSrc()) {
354 bool success
= p
->sendTiming(pkt
);
364 /** Function called by the port when the bus is receiving a Atomic
367 Bus::recvAtomic(PacketPtr pkt
)
369 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
370 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
371 assert(pkt
->getDest() == Packet::Broadcast
);
373 // Variables for recording original command and snoop response (if
374 // any)... if a snooper respondes, we will need to restore
375 // original command so that additional snoops can take place
377 MemCmd orig_cmd
= pkt
->cmd
;
378 Packet::Result response_result
= Packet::Unknown
;
379 MemCmd response_cmd
= MemCmd::InvalidCmd
;
381 Port
*target_port
= findPort(pkt
->getAddr(), pkt
->getSrc());
383 SnoopIter s_end
= snoopPorts
.end();
384 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= s_end
; s_iter
++) {
385 BusPort
*p
= *s_iter
;
386 // same port should not have both target addresses and snooping
387 assert(p
!= target_port
);
388 if (p
->getId() != pkt
->getSrc()) {
390 if (pkt
->result
!= Packet::Unknown
) {
391 // response from snoop agent
392 assert(pkt
->cmd
!= orig_cmd
);
393 assert(pkt
->memInhibitAsserted());
394 assert(pkt
->isResponse());
395 // should only happen once
396 assert(response_result
== Packet::Unknown
);
397 assert(response_cmd
== MemCmd::InvalidCmd
);
398 // save response state
399 response_result
= pkt
->result
;
400 response_cmd
= pkt
->cmd
;
401 // restore original packet state for remaining snoopers
403 pkt
->result
= Packet::Unknown
;
408 Tick response_time
= target_port
->sendAtomic(pkt
);
410 // if we got a response from a snooper, restore it here
411 if (response_result
!= Packet::Unknown
) {
412 assert(response_cmd
!= MemCmd::InvalidCmd
);
413 // no one else should have responded
414 assert(pkt
->result
== Packet::Unknown
);
415 assert(pkt
->cmd
== orig_cmd
);
416 pkt
->cmd
= response_cmd
;
417 pkt
->result
= response_result
;
420 // why do we have this packet field and the return value both???
421 pkt
->finishTime
= std::max(response_time
, curTick
+ clock
);
422 return pkt
->finishTime
;
425 /** Function called by the port when the bus is receiving a Functional
428 Bus::recvFunctional(PacketPtr pkt
)
430 DPRINTF(Bus
, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
431 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
432 assert(pkt
->getDest() == Packet::Broadcast
);
434 Port
* port
= findPort(pkt
->getAddr(), pkt
->getSrc());
435 functionalSnoop(pkt
, port
? port
: interfaces
[pkt
->getSrc()]);
437 // If the snooping found what we were looking for, we're done.
438 if (pkt
->result
!= Packet::Success
&& port
) {
439 port
->sendFunctional(pkt
);
443 /** Function called by the port when the bus is receiving a status change.*/
445 Bus::recvStatusChange(Port::Status status
, int id
)
447 AddrRangeList ranges
;
451 assert(status
== Port::RangeChange
&&
452 "The other statuses need to be implemented.");
454 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n", id
);
456 if (id
== defaultId
) {
457 defaultRange
.clear();
458 // Only try to update these ranges if the user set a default responder.
460 defaultPort
->getPeerAddressRanges(ranges
, snoops
);
461 assert(snoops
== false);
462 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
463 defaultRange
.push_back(*iter
);
464 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
465 iter
->start
, iter
->end
);
470 assert((id
< maxId
&& id
>= 0) || id
== defaultId
);
471 BusPort
*port
= interfaces
[id
];
473 // Clean out any previously existent ids
474 for (PortIter portIter
= portMap
.begin();
475 portIter
!= portMap
.end(); ) {
476 if (portIter
->second
== id
)
477 portMap
.erase(portIter
++);
482 for (SnoopIter s_iter
= snoopPorts
.begin();
483 s_iter
!= snoopPorts
.end(); ) {
484 if ((*s_iter
)->getId() == id
)
485 s_iter
= snoopPorts
.erase(s_iter
);
490 port
->getPeerAddressRanges(ranges
, snoops
);
493 DPRINTF(BusAddrRanges
, "Adding id %d to snoop list\n", id
);
494 snoopPorts
.push_back(port
);
497 for (iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
498 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
499 iter
->start
, iter
->end
, id
);
500 if (portMap
.insert(*iter
, id
) == portMap
.end())
501 panic("Two devices with same range\n");
505 DPRINTF(MMU
, "port list has %d entries\n", portMap
.size());
507 // tell all our peers that our address range has changed.
508 // Don't tell the device that caused this change, it already knows
509 m5::hash_map
<short,BusPort
*>::iterator intIter
;
511 for (intIter
= interfaces
.begin(); intIter
!= interfaces
.end(); intIter
++)
512 if (intIter
->first
!= id
&& intIter
->first
!= funcPortId
)
513 intIter
->second
->sendStatusChange(Port::RangeChange
);
515 if (id
!= defaultId
&& defaultPort
)
516 defaultPort
->sendStatusChange(Port::RangeChange
);
520 Bus::addressRanges(AddrRangeList
&resp
, bool &snoop
, int id
)
525 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
527 for (AddrRangeIter dflt_iter
= defaultRange
.begin();
528 dflt_iter
!= defaultRange
.end(); dflt_iter
++) {
529 resp
.push_back(*dflt_iter
);
530 DPRINTF(BusAddrRanges
, " -- Dflt: %#llx : %#llx\n",dflt_iter
->start
,
533 for (PortIter portIter
= portMap
.begin();
534 portIter
!= portMap
.end(); portIter
++) {
536 for (AddrRangeIter dflt_iter
= defaultRange
.begin();
537 dflt_iter
!= defaultRange
.end(); dflt_iter
++) {
538 if ((portIter
->first
.start
< dflt_iter
->start
&&
539 portIter
->first
.end
>= dflt_iter
->start
) ||
540 (portIter
->first
.start
< dflt_iter
->end
&&
541 portIter
->first
.end
>= dflt_iter
->end
))
542 fatal("Devices can not set ranges that itersect the default set\
543 but are not a subset of the default set.\n");
544 if (portIter
->first
.start
>= dflt_iter
->start
&&
545 portIter
->first
.end
<= dflt_iter
->end
) {
547 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
548 portIter
->first
.start
, portIter
->first
.end
);
551 if (portIter
->second
!= id
&& !subset
) {
552 resp
.push_back(portIter
->first
);
553 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
554 portIter
->first
.start
, portIter
->first
.end
);
558 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= snoopPorts
.end();
560 if ((*s_iter
)->getId() != id
) {
568 Bus::findBlockSize(int id
)
570 if (cachedBlockSizeValid
)
571 return cachedBlockSize
;
575 for (PortIter portIter
= portMap
.begin();
576 portIter
!= portMap
.end(); portIter
++) {
577 int tmp_bs
= interfaces
[portIter
->second
]->peerBlockSize();
581 for (SnoopIter s_iter
= snoopPorts
.begin();
582 s_iter
!= snoopPorts
.end(); s_iter
++) {
583 int tmp_bs
= (*s_iter
)->peerBlockSize();
588 max_bs
= defaultBlockSize
;
591 warn_once("Blocksize found to not be 64... hmm... probably not.\n");
592 cachedBlockSize
= max_bs
;
593 cachedBlockSizeValid
= true;
599 Bus::drain(Event
* de
)
601 //We should check that we're not "doing" anything, and that noone is
602 //waiting. We might be idle but have someone waiting if the device we
603 //contacted for a retry didn't actually retry.
604 if (curTick
>= tickNextIdle
&& retryList
.size() == 0) {
615 if (tickNextIdle
< curTick
)
616 tickNextIdle
= (curTick
/ clock
) * clock
+ clock
;
619 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus
)
624 Param
<bool> responder_set
;
625 Param
<int> block_size
;
627 END_DECLARE_SIM_OBJECT_PARAMS(Bus
)
629 BEGIN_INIT_SIM_OBJECT_PARAMS(Bus
)
630 INIT_PARAM(bus_id
, "a globally unique bus id"),
631 INIT_PARAM(clock
, "bus clock speed"),
632 INIT_PARAM(width
, "width of the bus (bits)"),
633 INIT_PARAM(responder_set
, "Is a default responder set by the user"),
634 INIT_PARAM(block_size
, "Default blocksize if no device has one")
635 END_INIT_SIM_OBJECT_PARAMS(Bus
)
637 CREATE_SIM_OBJECT(Bus
)
639 return new Bus(getInstanceName(), bus_id
, clock
, width
, responder_set
,
643 REGISTER_SIM_OBJECT("Bus", Bus
)