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"
44 Bus::getPort(const std::string
&if_name
, int idx
)
46 if (if_name
== "default") {
47 if (defaultPort
== NULL
) {
48 defaultPort
= new BusPort(csprintf("%s-default",name()), this,
50 cachedBlockSizeValid
= false;
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
);
71 cachedBlockSizeValid
= false;
76 Bus::deletePortRefs(Port
*p
)
79 BusPort
*bp
= dynamic_cast<BusPort
*>(p
);
81 panic("Couldn't convert Port* to BusPort*\n");
82 // If this is our one functional port
85 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
)
105 Bus::BusFreeEvent::process()
111 Bus::BusFreeEvent::description() const
113 return "bus became available";
117 Bus::calcPacketTiming(PacketPtr pkt
)
119 // Bring tickNextIdle up to the present tick.
120 // There is some potential ambiguity where a cycle starts, which
121 // might make a difference when devices are acting right around a
122 // cycle boundary. Using a < allows things which happen exactly on
123 // a cycle boundary to take up only the following cycle. Anything
124 // that happens later will have to "wait" for the end of that
125 // cycle, and then start using the bus after that.
126 if (tickNextIdle
< curTick
) {
127 tickNextIdle
= curTick
;
128 if (tickNextIdle
% clock
!= 0)
129 tickNextIdle
= curTick
- (curTick
% clock
) + clock
;
132 Tick headerTime
= tickNextIdle
+ headerCycles
* clock
;
134 // The packet will be sent. Figure out how long it occupies the bus, and
135 // how much of that time is for the first "word", aka bus width.
137 if (pkt
->hasData()) {
138 // If a packet has data, it needs ceil(size/width) cycles to send it
139 int dataSize
= pkt
->getSize();
140 numCycles
+= dataSize
/width
;
141 if (dataSize
% width
)
145 // The first word will be delivered after the current tick, the delivery
146 // of the address if any, and one bus cycle to deliver the data
147 pkt
->firstWordTime
= headerTime
+ clock
;
149 pkt
->finishTime
= headerTime
+ numCycles
* clock
;
154 void Bus::occupyBus(Tick until
)
157 // shortcut for express snoop packets
161 tickNextIdle
= until
;
162 reschedule(busIdle
, tickNextIdle
, true);
164 DPRINTF(Bus
, "The bus is now occupied from tick %d to %d\n",
165 curTick
, tickNextIdle
);
168 /** Function called by the port when the bus is receiving a Timing
171 Bus::recvTiming(PacketPtr pkt
)
173 short src
= pkt
->getSrc();
176 if (src
== defaultId
)
177 src_port
= defaultPort
;
179 src_port
= checkBusCache(src
);
180 if (src_port
== NULL
) {
181 src_port
= interfaces
[src
];
182 updateBusCache(src
, src_port
);
186 // If the bus is busy, or other devices are in line ahead of the current
187 // one, put this device on the retry list.
188 if (!pkt
->isExpressSnoop() &&
189 (tickNextIdle
> curTick
||
190 (retryList
.size() && (!inRetry
|| src_port
!= retryList
.front()))))
192 addToRetryList(src_port
);
193 DPRINTF(Bus
, "recvTiming: src %d dst %d %s 0x%x BUSY\n",
194 src
, pkt
->getDest(), pkt
->cmdString(), pkt
->getAddr());
198 DPRINTF(Bus
, "recvTiming: src %d dst %d %s 0x%x\n",
199 src
, pkt
->getDest(), pkt
->cmdString(), pkt
->getAddr());
201 Tick headerFinishTime
= pkt
->isExpressSnoop() ? 0 : calcPacketTiming(pkt
);
202 Tick packetFinishTime
= pkt
->isExpressSnoop() ? 0 : pkt
->finishTime
;
204 short dest
= pkt
->getDest();
208 if (dest
== Packet::Broadcast
) {
209 dest_port_id
= findPort(pkt
->getAddr());
210 dest_port
= (dest_port_id
== defaultId
) ?
211 defaultPort
: interfaces
[dest_port_id
];
212 SnoopIter s_end
= snoopPorts
.end();
213 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= s_end
; s_iter
++) {
214 BusPort
*p
= *s_iter
;
215 if (p
!= dest_port
&& p
!= src_port
) {
216 // cache is not allowed to refuse snoop
217 bool success M5_VAR_USED
= p
->sendTiming(pkt
);
222 assert(dest
< maxId
);
223 assert(dest
!= src
); // catch infinite loops
225 if (dest_port_id
== defaultId
)
226 dest_port
= defaultPort
;
228 dest_port
= checkBusCache(dest
);
229 if (dest_port
== NULL
) {
230 dest_port
= interfaces
[dest_port_id
];
231 // updateBusCache(dest_port_id, dest_port);
234 dest_port
= (dest_port_id
== defaultId
) ?
235 defaultPort
: interfaces
[dest_port_id
];
238 if (dest_port_id
== src
) {
239 // Must be forwarded snoop up from below...
240 assert(dest
== Packet::Broadcast
);
242 // send to actual target
243 if (!dest_port
->sendTiming(pkt
)) {
244 // Packet not successfully sent. Leave or put it on the retry list.
245 // illegal to block responses... can lead to deadlock
246 assert(!pkt
->isResponse());
247 DPRINTF(Bus
, "recvTiming: src %d dst %d %s 0x%x TGT RETRY\n",
248 src
, pkt
->getDest(), pkt
->cmdString(), pkt
->getAddr());
249 addToRetryList(src_port
);
250 occupyBus(headerFinishTime
);
253 // send OK, fall through... pkt may have been deleted by
254 // target at this point, so it should *not* be referenced
255 // again. We'll set it to NULL here just to be safe.
259 occupyBus(packetFinishTime
);
261 // Packet was successfully sent.
262 // Also take care of retries
264 DPRINTF(Bus
, "Remove retry from list %d\n", src
);
265 retryList
.front()->onRetryList(false);
266 retryList
.pop_front();
273 Bus::recvRetry(int id
)
275 // If there's anything waiting, and the bus isn't busy...
276 if (retryList
.size() && curTick
>= tickNextIdle
) {
277 //retryingPort = retryList.front();
279 DPRINTF(Bus
, "Sending a retry to %s\n", retryList
.front()->getPeer()->name());
280 retryList
.front()->sendRetry();
281 // If inRetry is still true, sendTiming wasn't called
284 retryList
.front()->onRetryList(false);
285 retryList
.pop_front();
288 //Bring tickNextIdle up to the present
289 while (tickNextIdle
< curTick
)
290 tickNextIdle
+= clock
;
292 //Burn a cycle for the missed grant.
293 tickNextIdle
+= clock
;
295 reschedule(busIdle
, tickNextIdle
, true);
298 //If we weren't able to drain before, we might be able to now.
299 if (drainEvent
&& retryList
.size() == 0 && curTick
>= tickNextIdle
) {
300 drainEvent
->process();
301 // Clear the drain event once we're done with it.
307 Bus::findPort(Addr addr
)
309 /* An interval tree would be a better way to do this. --ali. */
312 dest_id
= checkPortCache(addr
);
314 PortIter i
= portMap
.find(RangeSize(addr
,1));
315 if (i
!= portMap
.end()) {
317 updatePortCache(dest_id
, i
->first
.start
, i
->first
.end
);
321 // Check if this matches the default range
323 AddrRangeIter a_end
= defaultRange
.end();
324 for (AddrRangeIter i
= defaultRange
.begin(); i
!= a_end
; i
++) {
326 DPRINTF(Bus
, " found addr %#llx on default\n", addr
);
332 panic("Unable to find destination for addr (user set default "
333 "responder): %#llx\n", addr
);
335 DPRINTF(Bus
, "Unable to find destination for addr: %#llx, will use "
336 "default port\n", addr
);
346 /** Function called by the port when the bus is receiving a Atomic
349 Bus::recvAtomic(PacketPtr pkt
)
351 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
352 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
353 assert(pkt
->getDest() == Packet::Broadcast
);
354 assert(pkt
->isRequest());
356 // Variables for recording original command and snoop response (if
357 // any)... if a snooper respondes, we will need to restore
358 // original command so that additional snoops can take place
360 MemCmd orig_cmd
= pkt
->cmd
;
361 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
362 Tick snoop_response_latency
= 0;
363 int orig_src
= pkt
->getSrc();
365 int target_port_id
= findPort(pkt
->getAddr());
366 BusPort
*target_port
;
367 if (target_port_id
== defaultId
)
368 target_port
= defaultPort
;
370 target_port
= checkBusCache(target_port_id
);
371 if (target_port
== NULL
) {
372 target_port
= interfaces
[target_port_id
];
373 updateBusCache(target_port_id
, target_port
);
377 SnoopIter s_end
= snoopPorts
.end();
378 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= s_end
; s_iter
++) {
379 BusPort
*p
= *s_iter
;
380 // same port should not have both target addresses and snooping
381 assert(p
!= target_port
);
382 if (p
->getId() != pkt
->getSrc()) {
383 Tick latency
= p
->sendAtomic(pkt
);
384 if (pkt
->isResponse()) {
385 // response from snoop agent
386 assert(pkt
->cmd
!= orig_cmd
);
387 assert(pkt
->memInhibitAsserted());
388 // should only happen once
389 assert(snoop_response_cmd
== MemCmd::InvalidCmd
);
390 // save response state
391 snoop_response_cmd
= pkt
->cmd
;
392 snoop_response_latency
= latency
;
393 // restore original packet state for remaining snoopers
395 pkt
->setSrc(orig_src
);
396 pkt
->setDest(Packet::Broadcast
);
401 Tick response_latency
= 0;
403 // we can get requests sent up from the memory side of the bus for
404 // snooping... don't send them back down!
405 if (target_port_id
!= pkt
->getSrc()) {
406 response_latency
= target_port
->sendAtomic(pkt
);
409 // if we got a response from a snooper, restore it here
410 if (snoop_response_cmd
!= MemCmd::InvalidCmd
) {
411 // no one else should have responded
412 assert(!pkt
->isResponse());
413 assert(pkt
->cmd
== orig_cmd
);
414 pkt
->cmd
= snoop_response_cmd
;
415 response_latency
= snoop_response_latency
;
418 // why do we have this packet field and the return value both???
419 pkt
->finishTime
= curTick
+ response_latency
;
420 return response_latency
;
423 /** Function called by the port when the bus is receiving a Functional
426 Bus::recvFunctional(PacketPtr pkt
)
428 if (!pkt
->isPrint()) {
429 // don't do DPRINTFs on PrintReq as it clutters up the output
431 "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
432 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(),
435 assert(pkt
->getDest() == Packet::Broadcast
);
437 int port_id
= findPort(pkt
->getAddr());
438 Port
*port
= (port_id
== defaultId
) ? defaultPort
: interfaces
[port_id
];
439 // The packet may be changed by another bus on snoops, restore the
441 int src_id
= pkt
->getSrc();
443 assert(pkt
->isRequest()); // hasn't already been satisfied
445 SnoopIter s_end
= snoopPorts
.end();
446 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= s_end
; s_iter
++) {
447 BusPort
*p
= *s_iter
;
448 if (p
!= port
&& p
->getId() != src_id
) {
449 p
->sendFunctional(pkt
);
451 if (pkt
->isResponse()) {
457 // If the snooping hasn't found what we were looking for, keep going.
458 if (!pkt
->isResponse() && port_id
!= pkt
->getSrc()) {
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
;
471 if (inRecvStatusChange
.count(id
))
473 inRecvStatusChange
.insert(id
);
475 assert(status
== Port::RangeChange
&&
476 "The other statuses need to be implemented.");
478 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n", id
);
481 if (id
== defaultId
) {
482 defaultRange
.clear();
483 // Only try to update these ranges if the user set a default responder.
485 defaultPort
->getPeerAddressRanges(ranges
, snoops
);
486 assert(snoops
== false);
487 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
488 defaultRange
.push_back(*iter
);
489 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
490 iter
->start
, iter
->end
);
495 assert((id
< maxId
&& id
>= 0) || id
== defaultId
);
496 BusPort
*port
= interfaces
[id
];
498 // Clean out any previously existent ids
499 for (PortIter portIter
= portMap
.begin();
500 portIter
!= portMap
.end(); ) {
501 if (portIter
->second
== id
)
502 portMap
.erase(portIter
++);
507 for (SnoopIter s_iter
= snoopPorts
.begin();
508 s_iter
!= snoopPorts
.end(); ) {
509 if ((*s_iter
)->getId() == id
)
510 s_iter
= snoopPorts
.erase(s_iter
);
515 port
->getPeerAddressRanges(ranges
, snoops
);
518 DPRINTF(BusAddrRanges
, "Adding id %d to snoop list\n", id
);
519 snoopPorts
.push_back(port
);
522 for (iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
523 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
524 iter
->start
, iter
->end
, id
);
525 if (portMap
.insert(*iter
, id
) == portMap
.end()) {
526 int conflict_id
= portMap
.find(*iter
)->second
;
527 fatal("%s has two ports with same range:\n\t%s\n\t%s\n",
528 name(), interfaces
[id
]->getPeer()->name(),
529 interfaces
[conflict_id
]->getPeer()->name());
533 DPRINTF(MMU
, "port list has %d entries\n", portMap
.size());
535 // tell all our peers that our address range has changed.
536 // Don't tell the device that caused this change, it already knows
537 m5::hash_map
<short,BusPort
*>::iterator intIter
;
539 for (intIter
= interfaces
.begin(); intIter
!= interfaces
.end(); intIter
++)
540 if (intIter
->first
!= id
&& intIter
->first
!= funcPortId
)
541 intIter
->second
->sendStatusChange(Port::RangeChange
);
543 if (id
!= defaultId
&& defaultPort
)
544 defaultPort
->sendStatusChange(Port::RangeChange
);
545 inRecvStatusChange
.erase(id
);
549 Bus::addressRanges(AddrRangeList
&resp
, bool &snoop
, int id
)
554 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
556 for (AddrRangeIter dflt_iter
= defaultRange
.begin();
557 dflt_iter
!= defaultRange
.end(); dflt_iter
++) {
558 resp
.push_back(*dflt_iter
);
559 DPRINTF(BusAddrRanges
, " -- Dflt: %#llx : %#llx\n",dflt_iter
->start
,
562 for (PortIter portIter
= portMap
.begin();
563 portIter
!= portMap
.end(); portIter
++) {
565 for (AddrRangeIter dflt_iter
= defaultRange
.begin();
566 dflt_iter
!= defaultRange
.end(); dflt_iter
++) {
567 if ((portIter
->first
.start
< dflt_iter
->start
&&
568 portIter
->first
.end
>= dflt_iter
->start
) ||
569 (portIter
->first
.start
< dflt_iter
->end
&&
570 portIter
->first
.end
>= dflt_iter
->end
))
571 fatal("Devices can not set ranges that itersect the default set\
572 but are not a subset of the default set.\n");
573 if (portIter
->first
.start
>= dflt_iter
->start
&&
574 portIter
->first
.end
<= dflt_iter
->end
) {
576 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
577 portIter
->first
.start
, portIter
->first
.end
);
580 if (portIter
->second
!= id
&& !subset
) {
581 resp
.push_back(portIter
->first
);
582 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
583 portIter
->first
.start
, portIter
->first
.end
);
587 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= snoopPorts
.end();
589 if ((*s_iter
)->getId() != id
) {
597 Bus::findBlockSize(int id
)
599 if (cachedBlockSizeValid
)
600 return cachedBlockSize
;
604 PortIter p_end
= portMap
.end();
605 for (PortIter p_iter
= portMap
.begin(); p_iter
!= p_end
; p_iter
++) {
606 int tmp_bs
= interfaces
[p_iter
->second
]->peerBlockSize();
610 SnoopIter s_end
= snoopPorts
.end();
611 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= s_end
; s_iter
++) {
612 int tmp_bs
= (*s_iter
)->peerBlockSize();
617 max_bs
= defaultBlockSize
;
620 warn_once("Blocksize found to not be 64... hmm... probably not.\n");
621 cachedBlockSize
= max_bs
;
622 cachedBlockSizeValid
= true;
628 Bus::drain(Event
* de
)
630 //We should check that we're not "doing" anything, and that noone is
631 //waiting. We might be idle but have someone waiting if the device we
632 //contacted for a retry didn't actually retry.
633 if (retryList
.size() || (curTick
< tickNextIdle
&& busIdle
.scheduled())) {
643 if (tickNextIdle
< curTick
)
644 tickNextIdle
= (curTick
/ clock
) * clock
+ clock
;
650 return new Bus(this);