2 * Copyright (c) 2011 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 * Definition of a bus object.
49 #include "base/misc.hh"
50 #include "base/trace.hh"
51 #include "debug/Bus.hh"
52 #include "debug/BusAddrRanges.hh"
53 #include "debug/MMU.hh"
56 Bus::Bus(const BusParams
*p
)
57 : MemObject(p
), busId(p
->bus_id
), clock(p
->clock
),
58 headerCycles(p
->header_cycles
), width(p
->width
), tickNextIdle(0),
59 drainEvent(NULL
), busIdle(this), inRetry(false), defaultPortId(-1),
60 useDefaultRange(p
->use_default_range
), defaultBlockSize(p
->block_size
),
61 cachedBlockSize(0), cachedBlockSizeValid(false)
63 //width, clock period, and header cycles must be positive
65 fatal("Bus width must be positive\n");
67 fatal("Bus clock period must be positive\n");
68 if (headerCycles
<= 0)
69 fatal("Number of header cycles must be positive\n");
74 Bus::getPort(const std::string
&if_name
, int idx
)
77 int id
= interfaces
.size();
78 if (if_name
== "default") {
79 if (defaultPortId
== -1) {
81 portName
= csprintf("%s-default", name());
83 fatal("Default port already set on %s\n", name());
85 portName
= csprintf("%s-p%d", name(), id
);
87 BusPort
*bp
= new BusPort(portName
, this, id
);
88 interfaces
.push_back(bp
);
89 cachedBlockSizeValid
= false;
96 std::vector
<BusPort
*>::iterator intIter
;
98 // iterate over our interfaces and determine which of our neighbours
99 // are snooping and add them as snoopers
100 for (intIter
= interfaces
.begin(); intIter
!= interfaces
.end();
102 if ((*intIter
)->getPeer()->isSnooping()) {
103 DPRINTF(BusAddrRanges
, "Adding snooping neighbour %s\n",
104 (*intIter
)->getPeer()->name());
105 snoopPorts
.push_back(*intIter
);
110 Bus::BusFreeEvent::BusFreeEvent(Bus
*_bus
)
115 Bus::BusFreeEvent::process()
121 Bus::BusFreeEvent::description() const
123 return "bus became available";
127 Bus::calcPacketTiming(PacketPtr pkt
)
129 // Bring tickNextIdle up to the present tick.
130 // There is some potential ambiguity where a cycle starts, which
131 // might make a difference when devices are acting right around a
132 // cycle boundary. Using a < allows things which happen exactly on
133 // a cycle boundary to take up only the following cycle. Anything
134 // that happens later will have to "wait" for the end of that
135 // cycle, and then start using the bus after that.
136 if (tickNextIdle
< curTick()) {
137 tickNextIdle
= curTick();
138 if (tickNextIdle
% clock
!= 0)
139 tickNextIdle
= curTick() - (curTick() % clock
) + clock
;
142 Tick headerTime
= tickNextIdle
+ headerCycles
* clock
;
144 // The packet will be sent. Figure out how long it occupies the bus, and
145 // how much of that time is for the first "word", aka bus width.
147 if (pkt
->hasData()) {
148 // If a packet has data, it needs ceil(size/width) cycles to send it
149 int dataSize
= pkt
->getSize();
150 numCycles
+= dataSize
/width
;
151 if (dataSize
% width
)
155 // The first word will be delivered after the current tick, the delivery
156 // of the address if any, and one bus cycle to deliver the data
157 pkt
->firstWordTime
= headerTime
+ clock
;
159 pkt
->finishTime
= headerTime
+ numCycles
* clock
;
164 void Bus::occupyBus(Tick until
)
167 // shortcut for express snoop packets
171 tickNextIdle
= until
;
172 reschedule(busIdle
, tickNextIdle
, true);
174 DPRINTF(Bus
, "The bus is now occupied from tick %d to %d\n",
175 curTick(), tickNextIdle
);
178 /** Function called by the port when the bus is receiving a Timing
181 Bus::recvTiming(PacketPtr pkt
)
183 short src
= pkt
->getSrc();
185 BusPort
*src_port
= interfaces
[src
];
187 // If the bus is busy, or other devices are in line ahead of the current
188 // one, put this device on the retry list.
189 if (!pkt
->isExpressSnoop() &&
190 (tickNextIdle
> curTick() ||
191 (retryList
.size() && (!inRetry
|| src_port
!= retryList
.front()))))
193 addToRetryList(src_port
);
194 DPRINTF(Bus
, "recvTiming: src %d dst %d %s 0x%x BUSY\n",
195 src
, pkt
->getDest(), pkt
->cmdString(), pkt
->getAddr());
199 DPRINTF(Bus
, "recvTiming: src %d dst %d %s 0x%x\n",
200 src
, pkt
->getDest(), pkt
->cmdString(), pkt
->getAddr());
202 Tick headerFinishTime
= pkt
->isExpressSnoop() ? 0 : calcPacketTiming(pkt
);
203 Tick packetFinishTime
= pkt
->isExpressSnoop() ? 0 : pkt
->finishTime
;
205 short dest
= pkt
->getDest();
209 if (dest
== Packet::Broadcast
) {
210 dest_port_id
= findPort(pkt
->getAddr());
211 dest_port
= 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
< interfaces
.size());
223 assert(dest
!= src
); // catch infinite loops
225 dest_port
= interfaces
[dest_port_id
];
228 if (dest_port_id
== src
) {
229 // Must be forwarded snoop up from below...
230 assert(dest
== Packet::Broadcast
);
232 // send to actual target
233 if (!dest_port
->sendTiming(pkt
)) {
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 // It's also illegal to force a transaction to retry after
238 // someone else has committed to respond.
239 assert(!pkt
->memInhibitAsserted());
240 DPRINTF(Bus
, "recvTiming: src %d dst %d %s 0x%x TGT RETRY\n",
241 src
, pkt
->getDest(), pkt
->cmdString(), pkt
->getAddr());
242 addToRetryList(src_port
);
243 occupyBus(headerFinishTime
);
246 // send OK, fall through... pkt may have been deleted by
247 // target at this point, so it should *not* be referenced
248 // again. We'll set it to NULL here just to be safe.
252 occupyBus(packetFinishTime
);
254 // Packet was successfully sent.
255 // Also take care of retries
257 DPRINTF(Bus
, "Remove retry from list %d\n", src
);
258 retryList
.front()->onRetryList(false);
259 retryList
.pop_front();
266 Bus::recvRetry(int id
)
268 // If there's anything waiting, and the bus isn't busy...
269 if (retryList
.size() && curTick() >= tickNextIdle
) {
270 //retryingPort = retryList.front();
272 DPRINTF(Bus
, "Sending a retry to %s\n", retryList
.front()->getPeer()->name());
273 retryList
.front()->sendRetry();
274 // If inRetry is still true, sendTiming wasn't called
277 retryList
.front()->onRetryList(false);
278 retryList
.pop_front();
281 //Bring tickNextIdle up to the present
282 while (tickNextIdle
< curTick())
283 tickNextIdle
+= clock
;
285 //Burn a cycle for the missed grant.
286 tickNextIdle
+= clock
;
288 reschedule(busIdle
, tickNextIdle
, true);
291 //If we weren't able to drain before, we might be able to now.
292 if (drainEvent
&& retryList
.size() == 0 && curTick() >= tickNextIdle
) {
293 drainEvent
->process();
294 // Clear the drain event once we're done with it.
300 Bus::findPort(Addr addr
)
302 /* An interval tree would be a better way to do this. --ali. */
305 dest_id
= checkPortCache(addr
);
309 // Check normal port ranges
310 PortIter i
= portMap
.find(RangeSize(addr
,1));
311 if (i
!= portMap
.end()) {
313 updatePortCache(dest_id
, i
->first
.start
, i
->first
.end
);
317 // Check if this matches the default range
318 if (useDefaultRange
) {
319 AddrRangeIter a_end
= defaultRange
.end();
320 for (AddrRangeIter i
= defaultRange
.begin(); i
!= a_end
; i
++) {
322 DPRINTF(Bus
, " found addr %#llx on default\n", addr
);
323 return defaultPortId
;
327 panic("Unable to find destination for addr %#llx\n", addr
);
330 DPRINTF(Bus
, "Unable to find destination for addr %#llx, "
331 "will use default port\n", addr
);
332 return defaultPortId
;
336 /** Function called by the port when the bus is receiving a Atomic
339 Bus::recvAtomic(PacketPtr pkt
)
341 DPRINTF(Bus
, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
342 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr(), pkt
->cmdString());
343 assert(pkt
->getDest() == Packet::Broadcast
);
344 assert(pkt
->isRequest());
346 // Variables for recording original command and snoop response (if
347 // any)... if a snooper respondes, we will need to restore
348 // original command so that additional snoops can take place
350 MemCmd orig_cmd
= pkt
->cmd
;
351 MemCmd snoop_response_cmd
= MemCmd::InvalidCmd
;
352 Tick snoop_response_latency
= 0;
353 int orig_src
= pkt
->getSrc();
355 int target_port_id
= findPort(pkt
->getAddr());
356 BusPort
*target_port
= interfaces
[target_port_id
];
358 SnoopIter s_end
= snoopPorts
.end();
359 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= s_end
; s_iter
++) {
360 BusPort
*p
= *s_iter
;
361 // same port should not have both target addresses and snooping
362 assert(p
!= target_port
);
363 if (p
->getId() != pkt
->getSrc()) {
364 Tick latency
= p
->sendAtomic(pkt
);
365 if (pkt
->isResponse()) {
366 // response from snoop agent
367 assert(pkt
->cmd
!= orig_cmd
);
368 assert(pkt
->memInhibitAsserted());
369 // should only happen once
370 assert(snoop_response_cmd
== MemCmd::InvalidCmd
);
371 // save response state
372 snoop_response_cmd
= pkt
->cmd
;
373 snoop_response_latency
= latency
;
374 // restore original packet state for remaining snoopers
376 pkt
->setSrc(orig_src
);
377 pkt
->setDest(Packet::Broadcast
);
382 Tick response_latency
= 0;
384 // we can get requests sent up from the memory side of the bus for
385 // snooping... don't send them back down!
386 if (target_port_id
!= pkt
->getSrc()) {
387 response_latency
= target_port
->sendAtomic(pkt
);
390 // if we got a response from a snooper, restore it here
391 if (snoop_response_cmd
!= MemCmd::InvalidCmd
) {
392 // no one else should have responded
393 assert(!pkt
->isResponse());
394 assert(pkt
->cmd
== orig_cmd
);
395 pkt
->cmd
= snoop_response_cmd
;
396 response_latency
= snoop_response_latency
;
399 // why do we have this packet field and the return value both???
400 pkt
->finishTime
= curTick() + response_latency
;
401 return response_latency
;
404 /** Function called by the port when the bus is receiving a Functional
407 Bus::recvFunctional(PacketPtr pkt
)
409 assert(pkt
->getDest() == Packet::Broadcast
);
411 int port_id
= findPort(pkt
->getAddr());
412 Port
*port
= interfaces
[port_id
];
413 // The packet may be changed by another bus on snoops, restore the
415 int src_id
= pkt
->getSrc();
417 if (!pkt
->isPrint()) {
418 // don't do DPRINTFs on PrintReq as it clutters up the output
420 "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
421 src_id
, port_id
, pkt
->getAddr(),
425 assert(pkt
->isRequest()); // hasn't already been satisfied
427 SnoopIter s_end
= snoopPorts
.end();
428 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= s_end
; s_iter
++) {
429 BusPort
*p
= *s_iter
;
430 if (p
!= port
&& p
->getId() != src_id
) {
431 p
->sendFunctional(pkt
);
433 if (pkt
->isResponse()) {
439 // If the snooping hasn't found what we were looking for and it is not
440 // a forwarded snoop from below, keep going.
441 if (!pkt
->isResponse() && port_id
!= pkt
->getSrc()) {
442 port
->sendFunctional(pkt
);
446 /** Function called by the port when the bus is receiving a range change.*/
448 Bus::recvRangeChange(int id
)
450 AddrRangeList ranges
;
453 if (inRecvRangeChange
.count(id
))
455 inRecvRangeChange
.insert(id
);
457 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n", id
);
460 if (id
== defaultPortId
) {
461 defaultRange
.clear();
462 // Only try to update these ranges if the user set a default responder.
463 if (useDefaultRange
) {
464 AddrRangeList ranges
= interfaces
[id
]->getPeer()->getAddrRanges();
465 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
466 defaultRange
.push_back(*iter
);
467 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
468 iter
->start
, iter
->end
);
473 assert(id
< interfaces
.size() && id
>= 0);
474 BusPort
*port
= interfaces
[id
];
476 // Clean out any previously existent ids
477 for (PortIter portIter
= portMap
.begin();
478 portIter
!= portMap
.end(); ) {
479 if (portIter
->second
== id
)
480 portMap
.erase(portIter
++);
485 ranges
= port
->getPeer()->getAddrRanges();
487 for (iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
488 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
489 iter
->start
, iter
->end
, id
);
490 if (portMap
.insert(*iter
, id
) == portMap
.end()) {
491 int conflict_id
= portMap
.find(*iter
)->second
;
492 fatal("%s has two ports with same range:\n\t%s\n\t%s\n",
493 name(), interfaces
[id
]->getPeer()->name(),
494 interfaces
[conflict_id
]->getPeer()->name());
498 DPRINTF(MMU
, "port list has %d entries\n", portMap
.size());
500 // tell all our peers that our address range has changed.
501 // Don't tell the device that caused this change, it already knows
502 std::vector
<BusPort
*>::const_iterator intIter
;
504 for (intIter
= interfaces
.begin(); intIter
!= interfaces
.end(); intIter
++)
505 if ((*intIter
)->getId() != id
)
506 (*intIter
)->sendRangeChange();
508 inRecvRangeChange
.erase(id
);
512 Bus::getAddrRanges(int id
)
514 AddrRangeList ranges
;
516 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
518 for (AddrRangeIter dflt_iter
= defaultRange
.begin();
519 dflt_iter
!= defaultRange
.end(); dflt_iter
++) {
520 ranges
.push_back(*dflt_iter
);
521 DPRINTF(BusAddrRanges
, " -- Dflt: %#llx : %#llx\n",dflt_iter
->start
,
524 for (PortIter portIter
= portMap
.begin();
525 portIter
!= portMap
.end(); portIter
++) {
527 for (AddrRangeIter dflt_iter
= defaultRange
.begin();
528 dflt_iter
!= defaultRange
.end(); dflt_iter
++) {
529 if ((portIter
->first
.start
< dflt_iter
->start
&&
530 portIter
->first
.end
>= dflt_iter
->start
) ||
531 (portIter
->first
.start
< dflt_iter
->end
&&
532 portIter
->first
.end
>= dflt_iter
->end
))
533 fatal("Devices can not set ranges that itersect the default set\
534 but are not a subset of the default set.\n");
535 if (portIter
->first
.start
>= dflt_iter
->start
&&
536 portIter
->first
.end
<= dflt_iter
->end
) {
538 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
539 portIter
->first
.start
, portIter
->first
.end
);
542 if (portIter
->second
!= id
&& !subset
) {
543 ranges
.push_back(portIter
->first
);
544 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
545 portIter
->first
.start
, portIter
->first
.end
);
553 Bus::isSnooping(int id
)
555 // in essence, answer the question if there are other snooping
556 // ports rather than the port that is asking
558 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= snoopPorts
.end();
560 if ((*s_iter
)->getId() != id
) {
569 Bus::findBlockSize(int id
)
571 if (cachedBlockSizeValid
)
572 return cachedBlockSize
;
576 PortIter p_end
= portMap
.end();
577 for (PortIter p_iter
= portMap
.begin(); p_iter
!= p_end
; p_iter
++) {
578 unsigned tmp_bs
= interfaces
[p_iter
->second
]->peerBlockSize();
582 SnoopIter s_end
= snoopPorts
.end();
583 for (SnoopIter s_iter
= snoopPorts
.begin(); s_iter
!= s_end
; s_iter
++) {
584 unsigned tmp_bs
= (*s_iter
)->peerBlockSize();
589 max_bs
= defaultBlockSize
;
592 warn_once("Blocksize found to not be 64... hmm... probably not.\n");
593 cachedBlockSize
= max_bs
;
594 cachedBlockSizeValid
= true;
600 Bus::drain(Event
* de
)
602 //We should check that we're not "doing" anything, and that noone is
603 //waiting. We might be idle but have someone waiting if the device we
604 //contacted for a retry didn't actually retry.
605 if (retryList
.size() || (curTick() < tickNextIdle
&& busIdle
.scheduled())) {
615 if (tickNextIdle
< curTick())
616 tickNextIdle
= (curTick() / clock
) * clock
+ clock
;
622 return new Bus(this);