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"
41 #include "debug/Bus.hh"
42 #include "debug/BusAddrRanges.hh"
43 #include "debug/MMU.hh"
46 Bus::Bus(const BusParams
*p
)
47 : MemObject(p
), busId(p
->bus_id
), clock(p
->clock
),
48 headerCycles(p
->header_cycles
), width(p
->width
), tickNextIdle(0),
49 drainEvent(NULL
), busIdle(this), inRetry(false), maxId(0),
50 defaultPort(NULL
), funcPort(NULL
), funcPortId(-4),
51 useDefaultRange(p
->use_default_range
), defaultBlockSize(p
->block_size
),
52 cachedBlockSize(0), cachedBlockSizeValid(false)
54 //width, clock period, and header cycles must be positive
56 fatal("Bus width must be positive\n");
58 fatal("Bus clock period must be positive\n");
59 if (headerCycles
<= 0)
60 fatal("Number of header cycles must be positive\n");
66 Bus::getPort(const std::string
&if_name
, int idx
)
68 if (if_name
== "default") {
69 if (defaultPort
== NULL
) {
70 defaultPort
= new BusPort(csprintf("%s-default",name()), this,
72 cachedBlockSizeValid
= false;
75 fatal("Default port already set\n");
78 if (if_name
== "functional") {
81 funcPort
= new BusPort(csprintf("%s-p%d-func", name(), id
), this, id
);
83 interfaces
[id
] = funcPort
;
88 // if_name ignored? forced to be empty?
90 assert(maxId
< std::numeric_limits
<typeof(maxId
)>::max());
91 BusPort
*bp
= new BusPort(csprintf("%s-p%d", name(), id
), this, id
);
93 cachedBlockSizeValid
= false;
98 Bus::deletePortRefs(Port
*p
)
101 BusPort
*bp
= dynamic_cast<BusPort
*>(p
);
103 panic("Couldn't convert Port* to BusPort*\n");
104 // If this is our one functional port
107 interfaces
.erase(bp
->getId());
112 /** Get the ranges of anyone other buses that we are connected to. */
116 m5::hash_map
<short,BusPort
*>::iterator intIter
;
118 for (intIter
= interfaces
.begin(); intIter
!= interfaces
.end(); intIter
++)
119 intIter
->second
->sendStatusChange(Port::RangeChange
);
122 Bus::BusFreeEvent::BusFreeEvent(Bus
*_bus
)
127 Bus::BusFreeEvent::process()
133 Bus::BusFreeEvent::description() const
135 return "bus became available";
139 Bus::calcPacketTiming(PacketPtr pkt
)
141 // Bring tickNextIdle up to the present tick.
142 // There is some potential ambiguity where a cycle starts, which
143 // might make a difference when devices are acting right around a
144 // cycle boundary. Using a < allows things which happen exactly on
145 // a cycle boundary to take up only the following cycle. Anything
146 // that happens later will have to "wait" for the end of that
147 // cycle, and then start using the bus after that.
148 if (tickNextIdle
< curTick()) {
149 tickNextIdle
= curTick();
150 if (tickNextIdle
% clock
!= 0)
151 tickNextIdle
= curTick() - (curTick() % clock
) + clock
;
154 Tick headerTime
= tickNextIdle
+ headerCycles
* clock
;
156 // The packet will be sent. Figure out how long it occupies the bus, and
157 // how much of that time is for the first "word", aka bus width.
159 if (pkt
->hasData()) {
160 // If a packet has data, it needs ceil(size/width) cycles to send it
161 int dataSize
= pkt
->getSize();
162 numCycles
+= dataSize
/width
;
163 if (dataSize
% width
)
167 // The first word will be delivered after the current tick, the delivery
168 // of the address if any, and one bus cycle to deliver the data
169 pkt
->firstWordTime
= headerTime
+ clock
;
171 pkt
->finishTime
= headerTime
+ numCycles
* clock
;
176 void Bus::occupyBus(Tick until
)
179 // shortcut for express snoop packets
183 tickNextIdle
= until
;
184 reschedule(busIdle
, tickNextIdle
, true);
186 DPRINTF(Bus
, "The bus is now occupied from tick %d to %d\n",
187 curTick(), tickNextIdle
);
190 /** Function called by the port when the bus is receiving a Timing
193 Bus::recvTiming(PacketPtr pkt
)
195 short src
= pkt
->getSrc();
198 if (src
== defaultId
)
199 src_port
= defaultPort
;
201 src_port
= checkBusCache(src
);
202 if (src_port
== NULL
) {
203 src_port
= interfaces
[src
];
204 updateBusCache(src
, src_port
);
208 // If the bus is busy, or other devices are in line ahead of the current
209 // one, put this device on the retry list.
210 if (!pkt
->isExpressSnoop() &&
211 (tickNextIdle
> curTick() ||
212 (retryList
.size() && (!inRetry
|| src_port
!= retryList
.front()))))
214 addToRetryList(src_port
);
215 DPRINTF(Bus
, "recvTiming: src %d dst %d %s 0x%x BUSY\n",
216 src
, pkt
->getDest(), pkt
->cmdString(), pkt
->getAddr());
220 DPRINTF(Bus
, "recvTiming: src %d dst %d %s 0x%x\n",
221 src
, pkt
->getDest(), pkt
->cmdString(), pkt
->getAddr());
223 Tick headerFinishTime
= pkt
->isExpressSnoop() ? 0 : calcPacketTiming(pkt
);
224 Tick packetFinishTime
= pkt
->isExpressSnoop() ? 0 : pkt
->finishTime
;
226 short dest
= pkt
->getDest();
230 if (dest
== Packet::Broadcast
) {
231 dest_port_id
= findPort(pkt
->getAddr());
232 dest_port
= (dest_port_id
== defaultId
) ?
233 defaultPort
: interfaces
[dest_port_id
];
234 SnoopIter s_end
= snoopPorts
.end();
235 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= s_end
; s_iter
++) {
236 BusPort
*p
= *s_iter
;
237 if (p
!= dest_port
&& p
!= src_port
) {
238 // cache is not allowed to refuse snoop
239 bool success M5_VAR_USED
= p
->sendTiming(pkt
);
244 assert(dest
< maxId
);
245 assert(dest
!= src
); // catch infinite loops
247 if (dest_port_id
== defaultId
)
248 dest_port
= defaultPort
;
250 dest_port
= checkBusCache(dest
);
251 if (dest_port
== NULL
) {
252 dest_port
= interfaces
[dest_port_id
];
253 // updateBusCache(dest_port_id, dest_port);
256 dest_port
= (dest_port_id
== defaultId
) ?
257 defaultPort
: interfaces
[dest_port_id
];
260 if (dest_port_id
== src
) {
261 // Must be forwarded snoop up from below...
262 assert(dest
== Packet::Broadcast
);
264 // send to actual target
265 if (!dest_port
->sendTiming(pkt
)) {
266 // Packet not successfully sent. Leave or put it on the retry list.
267 // illegal to block responses... can lead to deadlock
268 assert(!pkt
->isResponse());
269 // It's also illegal to force a transaction to retry after
270 // someone else has committed to respond.
271 assert(!pkt
->memInhibitAsserted());
272 DPRINTF(Bus
, "recvTiming: src %d dst %d %s 0x%x TGT RETRY\n",
273 src
, pkt
->getDest(), pkt
->cmdString(), pkt
->getAddr());
274 addToRetryList(src_port
);
275 occupyBus(headerFinishTime
);
278 // send OK, fall through... pkt may have been deleted by
279 // target at this point, so it should *not* be referenced
280 // again. We'll set it to NULL here just to be safe.
284 occupyBus(packetFinishTime
);
286 // Packet was successfully sent.
287 // Also take care of retries
289 DPRINTF(Bus
, "Remove retry from list %d\n", src
);
290 retryList
.front()->onRetryList(false);
291 retryList
.pop_front();
298 Bus::recvRetry(int id
)
300 // If there's anything waiting, and the bus isn't busy...
301 if (retryList
.size() && curTick() >= tickNextIdle
) {
302 //retryingPort = retryList.front();
304 DPRINTF(Bus
, "Sending a retry to %s\n", retryList
.front()->getPeer()->name());
305 retryList
.front()->sendRetry();
306 // If inRetry is still true, sendTiming wasn't called
309 retryList
.front()->onRetryList(false);
310 retryList
.pop_front();
313 //Bring tickNextIdle up to the present
314 while (tickNextIdle
< curTick())
315 tickNextIdle
+= clock
;
317 //Burn a cycle for the missed grant.
318 tickNextIdle
+= clock
;
320 reschedule(busIdle
, tickNextIdle
, true);
323 //If we weren't able to drain before, we might be able to now.
324 if (drainEvent
&& retryList
.size() == 0 && curTick() >= tickNextIdle
) {
325 drainEvent
->process();
326 // Clear the drain event once we're done with it.
332 Bus::findPort(Addr addr
)
334 /* An interval tree would be a better way to do this. --ali. */
337 dest_id
= checkPortCache(addr
);
341 // Check normal port ranges
342 PortIter i
= portMap
.find(RangeSize(addr
,1));
343 if (i
!= portMap
.end()) {
345 updatePortCache(dest_id
, i
->first
.start
, i
->first
.end
);
349 // Check if this matches the default range
350 if (useDefaultRange
) {
351 AddrRangeIter a_end
= defaultRange
.end();
352 for (AddrRangeIter i
= defaultRange
.begin(); i
!= a_end
; i
++) {
354 DPRINTF(Bus
, " found addr %#llx on default\n", addr
);
359 panic("Unable to find destination for addr %#llx\n", addr
);
362 DPRINTF(Bus
, "Unable to find destination for addr %#llx, "
363 "will use default port\n", addr
);
368 /** Function called by the port when the bus is receiving a Atomic
371 Bus::recvAtomic(PacketPtr pkt
)
373 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
374 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
375 assert(pkt
->getDest() == Packet::Broadcast
);
376 assert(pkt
->isRequest());
378 // Variables for recording original command and snoop response (if
379 // any)... if a snooper respondes, we will need to restore
380 // original command so that additional snoops can take place
382 MemCmd orig_cmd
= pkt
->cmd
;
383 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
384 Tick snoop_response_latency
= 0;
385 int orig_src
= pkt
->getSrc();
387 int target_port_id
= findPort(pkt
->getAddr());
388 BusPort
*target_port
;
389 if (target_port_id
== defaultId
)
390 target_port
= defaultPort
;
392 target_port
= checkBusCache(target_port_id
);
393 if (target_port
== NULL
) {
394 target_port
= interfaces
[target_port_id
];
395 updateBusCache(target_port_id
, target_port
);
399 SnoopIter s_end
= snoopPorts
.end();
400 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= s_end
; s_iter
++) {
401 BusPort
*p
= *s_iter
;
402 // same port should not have both target addresses and snooping
403 assert(p
!= target_port
);
404 if (p
->getId() != pkt
->getSrc()) {
405 Tick latency
= p
->sendAtomic(pkt
);
406 if (pkt
->isResponse()) {
407 // response from snoop agent
408 assert(pkt
->cmd
!= orig_cmd
);
409 assert(pkt
->memInhibitAsserted());
410 // should only happen once
411 assert(snoop_response_cmd
== MemCmd::InvalidCmd
);
412 // save response state
413 snoop_response_cmd
= pkt
->cmd
;
414 snoop_response_latency
= latency
;
415 // restore original packet state for remaining snoopers
417 pkt
->setSrc(orig_src
);
418 pkt
->setDest(Packet::Broadcast
);
423 Tick response_latency
= 0;
425 // we can get requests sent up from the memory side of the bus for
426 // snooping... don't send them back down!
427 if (target_port_id
!= pkt
->getSrc()) {
428 response_latency
= target_port
->sendAtomic(pkt
);
431 // if we got a response from a snooper, restore it here
432 if (snoop_response_cmd
!= MemCmd::InvalidCmd
) {
433 // no one else should have responded
434 assert(!pkt
->isResponse());
435 assert(pkt
->cmd
== orig_cmd
);
436 pkt
->cmd
= snoop_response_cmd
;
437 response_latency
= snoop_response_latency
;
440 // why do we have this packet field and the return value both???
441 pkt
->finishTime
= curTick() + response_latency
;
442 return response_latency
;
445 /** Function called by the port when the bus is receiving a Functional
448 Bus::recvFunctional(PacketPtr pkt
)
450 assert(pkt
->getDest() == Packet::Broadcast
);
452 int port_id
= findPort(pkt
->getAddr());
453 Port
*port
= (port_id
== defaultId
) ? defaultPort
: interfaces
[port_id
];
454 // The packet may be changed by another bus on snoops, restore the
456 int src_id
= pkt
->getSrc();
458 if (!pkt
->isPrint()) {
459 // don't do DPRINTFs on PrintReq as it clutters up the output
461 "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
462 src_id
, port_id
, pkt
->getAddr(),
466 assert(pkt
->isRequest()); // hasn't already been satisfied
468 SnoopIter s_end
= snoopPorts
.end();
469 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= s_end
; s_iter
++) {
470 BusPort
*p
= *s_iter
;
471 if (p
!= port
&& p
->getId() != src_id
) {
472 p
->sendFunctional(pkt
);
474 if (pkt
->isResponse()) {
480 // If the snooping hasn't found what we were looking for, keep going.
481 if (!pkt
->isResponse() && port_id
!= pkt
->getSrc()) {
482 port
->sendFunctional(pkt
);
486 /** Function called by the port when the bus is receiving a status change.*/
488 Bus::recvStatusChange(Port::Status status
, int id
)
490 AddrRangeList ranges
;
494 if (inRecvStatusChange
.count(id
))
496 inRecvStatusChange
.insert(id
);
498 assert(status
== Port::RangeChange
&&
499 "The other statuses need to be implemented.");
501 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n", id
);
504 if (id
== defaultId
) {
505 defaultRange
.clear();
506 // Only try to update these ranges if the user set a default responder.
507 if (useDefaultRange
) {
508 defaultPort
->getPeerAddressRanges(ranges
, snoops
);
509 assert(snoops
== false);
510 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
511 defaultRange
.push_back(*iter
);
512 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
513 iter
->start
, iter
->end
);
518 assert((id
< maxId
&& id
>= 0) || id
== defaultId
);
519 BusPort
*port
= interfaces
[id
];
521 // Clean out any previously existent ids
522 for (PortIter portIter
= portMap
.begin();
523 portIter
!= portMap
.end(); ) {
524 if (portIter
->second
== id
)
525 portMap
.erase(portIter
++);
530 for (SnoopIter s_iter
= snoopPorts
.begin();
531 s_iter
!= snoopPorts
.end(); ) {
532 if ((*s_iter
)->getId() == id
)
533 s_iter
= snoopPorts
.erase(s_iter
);
538 port
->getPeerAddressRanges(ranges
, snoops
);
541 DPRINTF(BusAddrRanges
, "Adding id %d to snoop list\n", id
);
542 snoopPorts
.push_back(port
);
545 for (iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
546 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
547 iter
->start
, iter
->end
, id
);
548 if (portMap
.insert(*iter
, id
) == portMap
.end()) {
549 int conflict_id
= portMap
.find(*iter
)->second
;
550 fatal("%s has two ports with same range:\n\t%s\n\t%s\n",
551 name(), interfaces
[id
]->getPeer()->name(),
552 interfaces
[conflict_id
]->getPeer()->name());
556 DPRINTF(MMU
, "port list has %d entries\n", portMap
.size());
558 // tell all our peers that our address range has changed.
559 // Don't tell the device that caused this change, it already knows
560 m5::hash_map
<short,BusPort
*>::iterator intIter
;
562 for (intIter
= interfaces
.begin(); intIter
!= interfaces
.end(); intIter
++)
563 if (intIter
->first
!= id
&& intIter
->first
!= funcPortId
)
564 intIter
->second
->sendStatusChange(Port::RangeChange
);
566 if (id
!= defaultId
&& defaultPort
)
567 defaultPort
->sendStatusChange(Port::RangeChange
);
568 inRecvStatusChange
.erase(id
);
572 Bus::addressRanges(AddrRangeList
&resp
, bool &snoop
, int id
)
577 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
579 for (AddrRangeIter dflt_iter
= defaultRange
.begin();
580 dflt_iter
!= defaultRange
.end(); dflt_iter
++) {
581 resp
.push_back(*dflt_iter
);
582 DPRINTF(BusAddrRanges
, " -- Dflt: %#llx : %#llx\n",dflt_iter
->start
,
585 for (PortIter portIter
= portMap
.begin();
586 portIter
!= portMap
.end(); portIter
++) {
588 for (AddrRangeIter dflt_iter
= defaultRange
.begin();
589 dflt_iter
!= defaultRange
.end(); dflt_iter
++) {
590 if ((portIter
->first
.start
< dflt_iter
->start
&&
591 portIter
->first
.end
>= dflt_iter
->start
) ||
592 (portIter
->first
.start
< dflt_iter
->end
&&
593 portIter
->first
.end
>= dflt_iter
->end
))
594 fatal("Devices can not set ranges that itersect the default set\
595 but are not a subset of the default set.\n");
596 if (portIter
->first
.start
>= dflt_iter
->start
&&
597 portIter
->first
.end
<= dflt_iter
->end
) {
599 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
600 portIter
->first
.start
, portIter
->first
.end
);
603 if (portIter
->second
!= id
&& !subset
) {
604 resp
.push_back(portIter
->first
);
605 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
606 portIter
->first
.start
, portIter
->first
.end
);
610 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= snoopPorts
.end();
612 if ((*s_iter
)->getId() != id
) {
620 Bus::findBlockSize(int id
)
622 if (cachedBlockSizeValid
)
623 return cachedBlockSize
;
627 PortIter p_end
= portMap
.end();
628 for (PortIter p_iter
= portMap
.begin(); p_iter
!= p_end
; p_iter
++) {
629 unsigned tmp_bs
= interfaces
[p_iter
->second
]->peerBlockSize();
633 SnoopIter s_end
= snoopPorts
.end();
634 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= s_end
; s_iter
++) {
635 unsigned tmp_bs
= (*s_iter
)->peerBlockSize();
640 max_bs
= defaultBlockSize
;
643 warn_once("Blocksize found to not be 64... hmm... probably not.\n");
644 cachedBlockSize
= max_bs
;
645 cachedBlockSizeValid
= true;
651 Bus::drain(Event
* de
)
653 //We should check that we're not "doing" anything, and that noone is
654 //waiting. We might be idle but have someone waiting if the device we
655 //contacted for a retry didn't actually retry.
656 if (retryList
.size() || (curTick() < tickNextIdle
&& busIdle
.scheduled())) {
666 if (tickNextIdle
< curTick())
667 tickNextIdle
= (curTick() / clock
) * clock
+ clock
;
673 return new Bus(this);