2 * Copyright (c) 2011-2013 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.
47 * Definition of a bus object.
50 #include "base/misc.hh"
51 #include "base/trace.hh"
52 #include "debug/Bus.hh"
53 #include "debug/BusAddrRanges.hh"
54 #include "debug/Drain.hh"
57 BaseBus::BaseBus(const BaseBusParams
*p
)
59 headerCycles(p
->header_cycles
), width(p
->width
),
60 gotAddrRanges(p
->port_default_connection_count
+
61 p
->port_master_connection_count
, false),
62 gotAllAddrRanges(false), defaultPortID(InvalidPortID
),
63 useDefaultRange(p
->use_default_range
),
64 blockSize(p
->block_size
)
69 for (MasterPortIter m
= masterPorts
.begin(); m
!= masterPorts
.end();
74 for (SlavePortIter s
= slavePorts
.begin(); s
!= slavePorts
.end();
83 // determine the maximum peer block size, look at both the
84 // connected master and slave modules
85 uint32_t peer_block_size
= 0;
87 for (MasterPortConstIter m
= masterPorts
.begin(); m
!= masterPorts
.end();
89 peer_block_size
= std::max((*m
)->peerBlockSize(), peer_block_size
);
92 for (SlavePortConstIter s
= slavePorts
.begin(); s
!= slavePorts
.end();
94 peer_block_size
= std::max((*s
)->peerBlockSize(), peer_block_size
);
97 // if the peers do not have a block size, use the default value
98 // set through the bus parameters
99 if (peer_block_size
!= 0)
100 blockSize
= peer_block_size
;
102 // check if the block size is a value known to work
103 if (!(blockSize
== 16 || blockSize
== 32 || blockSize
== 64 ||
105 warn_once("Block size is neither 16, 32, 64 or 128 bytes.\n");
109 BaseBus::getMasterPort(const std::string
&if_name
, PortID idx
)
111 if (if_name
== "master" && idx
< masterPorts
.size()) {
112 // the master port index translates directly to the vector position
113 return *masterPorts
[idx
];
114 } else if (if_name
== "default") {
115 return *masterPorts
[defaultPortID
];
117 return MemObject::getMasterPort(if_name
, idx
);
122 BaseBus::getSlavePort(const std::string
&if_name
, PortID idx
)
124 if (if_name
== "slave" && idx
< slavePorts
.size()) {
125 // the slave port index translates directly to the vector position
126 return *slavePorts
[idx
];
128 return MemObject::getSlavePort(if_name
, idx
);
133 BaseBus::calcPacketTiming(PacketPtr pkt
)
135 // determine the header time rounded to the closest following
137 Tick headerTime
= clockEdge(headerCycles
);
139 // The packet will be sent. Figure out how long it occupies the bus, and
140 // how much of that time is for the first "word", aka bus width.
142 if (pkt
->hasData()) {
143 // If a packet has data, it needs ceil(size/width) cycles to send it
144 unsigned dataSize
= pkt
->getSize();
145 numCycles
= Cycles(divCeil(dataSize
, width
));
148 // The first word will be delivered on the cycle after the header.
149 pkt
->firstWordTime
= headerTime
+ clockPeriod();
151 // Note that currently finishTime can be smaller than
152 // firstWordTime if the packet has no data
153 pkt
->finishTime
= headerTime
+ numCycles
* clockPeriod();
158 template <typename PortClass
>
159 BaseBus::Layer
<PortClass
>::Layer(BaseBus
& _bus
, const std::string
& _name
) :
161 bus(_bus
), _name(_name
), state(IDLE
), drainManager(NULL
),
166 template <typename PortClass
>
167 void BaseBus::Layer
<PortClass
>::occupyLayer(Tick until
)
169 // ensure the state is busy or in retry and never idle at this
170 // point, as the bus should transition from idle as soon as it has
171 // decided to forward the packet to prevent any follow-on calls to
172 // sendTiming seeing an unoccupied bus
173 assert(state
!= IDLE
);
175 // note that we do not change the bus state here, if we are going
176 // from idle to busy it is handled by tryTiming, and if we
177 // are in retry we should remain in retry such that
178 // succeededTiming still sees the accurate state
180 // until should never be 0 as express snoops never occupy the bus
182 bus
.schedule(releaseEvent
, until
);
184 DPRINTF(BaseBus
, "The bus is now busy from tick %d to %d\n",
188 template <typename PortClass
>
190 BaseBus::Layer
<PortClass
>::tryTiming(PortClass
* port
)
192 // first we see if the bus is busy, next we check if we are in a
193 // retry with a port other than the current one
194 if (state
== BUSY
|| (state
== RETRY
&& port
!= retryList
.front())) {
195 // put the port at the end of the retry list
196 retryList
.push_back(port
);
200 // update the state which is shared for request, response and
201 // snoop responses, if we were idle we are now busy, if we are in
202 // a retry, then do not change
209 template <typename PortClass
>
211 BaseBus::Layer
<PortClass
>::succeededTiming(Tick busy_time
)
213 // if a retrying port succeeded, also take it off the retry list
214 if (state
== RETRY
) {
215 DPRINTF(BaseBus
, "Remove retry from list %s\n",
216 retryList
.front()->name());
217 retryList
.pop_front();
221 // we should either have gone from idle to busy in the
222 // tryTiming test, or just gone from a retry to busy
223 assert(state
== BUSY
);
225 // occupy the bus accordingly
226 occupyLayer(busy_time
);
229 template <typename PortClass
>
231 BaseBus::Layer
<PortClass
>::failedTiming(PortClass
* port
, Tick busy_time
)
233 // if we are not in a retry, i.e. busy (but never idle), or we are
234 // in a retry but not for the current port, then add the port at
235 // the end of the retry list
236 if (state
!= RETRY
|| port
!= retryList
.front()) {
237 retryList
.push_back(port
);
240 // even if we retried the current one and did not succeed,
241 // we are no longer retrying but instead busy
244 // occupy the bus accordingly
245 occupyLayer(busy_time
);
248 template <typename PortClass
>
250 BaseBus::Layer
<PortClass
>::releaseLayer()
252 // releasing the bus means we should now be idle
253 assert(state
== BUSY
);
254 assert(!releaseEvent
.scheduled());
259 // bus is now idle, so if someone is waiting we can retry
260 if (!retryList
.empty()) {
261 // note that we block (return false on recvTiming) both
262 // because the bus is busy and because the destination is
263 // busy, and in the latter case the bus may be released before
264 // we see a retry from the destination
266 } else if (drainManager
) {
267 DPRINTF(Drain
, "Bus done draining, signaling drain manager\n");
268 //If we weren't able to drain before, do it now.
269 drainManager
->signalDrainDone();
270 // Clear the drain event once we're done with it.
275 template <typename PortClass
>
277 BaseBus::Layer
<PortClass
>::retryWaiting()
279 // this should never be called with an empty retry list
280 assert(!retryList
.empty());
282 // we always go to retrying from idle
283 assert(state
== IDLE
);
285 // update the state which is shared for request, response and
289 // note that we might have blocked on the receiving port being
290 // busy (rather than the bus itself) and now call retry before the
291 // destination called retry on the bus
292 retryList
.front()->sendRetry();
294 // If the bus is still in the retry state, sendTiming wasn't
295 // called in zero time (e.g. the cache does this)
296 if (state
== RETRY
) {
297 retryList
.pop_front();
299 //Burn a cycle for the missed grant.
301 // update the state which is shared for request, response and
305 // occupy the bus layer until the next cycle ends
306 occupyLayer(bus
.clockEdge(Cycles(1)));
310 template <typename PortClass
>
312 BaseBus::Layer
<PortClass
>::recvRetry()
314 // we got a retry from a peer that we tried to send something to
315 // and failed, but we sent it on the account of someone else, and
316 // that source port should be on our retry list, however if the
317 // bus layer is released before this happens and the retry (from
318 // the bus point of view) is successful then this no longer holds
319 // and we could in fact have an empty retry list
320 if (retryList
.empty())
323 // if the bus layer is idle
325 // note that we do not care who told us to retry at the moment, we
326 // merely let the first one on the retry list go
332 BaseBus::findPort(Addr addr
)
334 // we should never see any address lookups before we've got the
335 // ranges of all connected slave modules
336 assert(gotAllAddrRanges
);
339 PortID dest_id
= checkPortCache(addr
);
340 if (dest_id
!= InvalidPortID
)
343 // Check the address map interval tree
344 PortMapConstIter i
= portMap
.find(addr
);
345 if (i
!= portMap
.end()) {
347 updatePortCache(dest_id
, i
->first
);
351 // Check if this matches the default range
352 if (useDefaultRange
) {
353 if (defaultRange
.contains(addr
)) {
354 DPRINTF(BusAddrRanges
, " found addr %#llx on default\n",
356 return defaultPortID
;
358 } else if (defaultPortID
!= InvalidPortID
) {
359 DPRINTF(BusAddrRanges
, "Unable to find destination for addr %#llx, "
360 "will use default port\n", addr
);
361 return defaultPortID
;
364 // we should use the range for the default port and it did not
365 // match, or the default port is not set
366 fatal("Unable to find destination for addr %#llx on bus %s\n", addr
,
370 /** Function called by the port when the bus is receiving a range change.*/
372 BaseBus::recvRangeChange(PortID master_port_id
)
374 DPRINTF(BusAddrRanges
, "Received range change from slave port %s\n",
375 masterPorts
[master_port_id
]->getSlavePort().name());
377 // remember that we got a range from this master port and thus the
378 // connected slave module
379 gotAddrRanges
[master_port_id
] = true;
381 // update the global flag
382 if (!gotAllAddrRanges
) {
383 // take a logical AND of all the ports and see if we got
384 // ranges from everyone
385 gotAllAddrRanges
= true;
386 std::vector
<bool>::const_iterator r
= gotAddrRanges
.begin();
387 while (gotAllAddrRanges
&& r
!= gotAddrRanges
.end()) {
388 gotAllAddrRanges
&= *r
++;
390 if (gotAllAddrRanges
)
391 DPRINTF(BusAddrRanges
, "Got address ranges from all slaves\n");
394 // note that we could get the range from the default port at any
395 // point in time, and we cannot assume that the default range is
396 // set before the other ones are, so we do additional checks once
397 // all ranges are provided
398 if (master_port_id
== defaultPortID
) {
399 // only update if we are indeed checking ranges for the
400 // default port since the port might not have a valid range
402 if (useDefaultRange
) {
403 AddrRangeList ranges
= masterPorts
[master_port_id
]->getAddrRanges();
405 if (ranges
.size() != 1)
406 fatal("Bus %s may only have a single default range",
409 defaultRange
= ranges
.front();
412 // the ports are allowed to update their address ranges
413 // dynamically, so remove any existing entries
414 if (gotAddrRanges
[master_port_id
]) {
415 for (PortMapIter p
= portMap
.begin(); p
!= portMap
.end(); ) {
416 if (p
->second
== master_port_id
)
417 // erasing invalidates the iterator, so advance it
418 // before the deletion takes place
425 AddrRangeList ranges
= masterPorts
[master_port_id
]->getAddrRanges();
427 for (AddrRangeConstIter r
= ranges
.begin(); r
!= ranges
.end(); ++r
) {
428 DPRINTF(BusAddrRanges
, "Adding range %s for id %d\n",
429 r
->to_string(), master_port_id
);
430 if (portMap
.insert(*r
, master_port_id
) == portMap
.end()) {
431 PortID conflict_id
= portMap
.find(*r
)->second
;
432 fatal("%s has two ports with same range:\n\t%s\n\t%s\n",
434 masterPorts
[master_port_id
]->getSlavePort().name(),
435 masterPorts
[conflict_id
]->getSlavePort().name());
440 // if we have received ranges from all our neighbouring slave
441 // modules, go ahead and tell our connected master modules in
442 // turn, this effectively assumes a tree structure of the system
443 if (gotAllAddrRanges
) {
444 // also check that no range partially overlaps with the
445 // default range, this has to be done after all ranges are set
446 // as there are no guarantees for when the default range is
447 // update with respect to the other ones
448 if (useDefaultRange
) {
449 for (PortID port_id
= 0; port_id
< masterPorts
.size(); ++port_id
) {
450 if (port_id
== defaultPortID
) {
451 if (!gotAddrRanges
[port_id
])
452 fatal("Bus %s uses default range, but none provided",
455 AddrRangeList ranges
=
456 masterPorts
[port_id
]->getAddrRanges();
458 for (AddrRangeConstIter r
= ranges
.begin();
459 r
!= ranges
.end(); ++r
) {
460 // see if the new range is partially
461 // overlapping the default range
462 if (r
->intersects(defaultRange
) &&
463 !r
->isSubset(defaultRange
))
464 fatal("Range %s intersects the " \
465 "default range of %s but is not a " \
466 "subset\n", r
->to_string(), name());
472 // tell all our neighbouring master ports that our address
473 // ranges have changed
474 for (SlavePortConstIter s
= slavePorts
.begin(); s
!= slavePorts
.end();
476 (*s
)->sendRangeChange();
483 BaseBus::getAddrRanges() const
485 // we should never be asked without first having sent a range
486 // change, and the latter is only done once we have all the ranges
487 // of the connected devices
488 assert(gotAllAddrRanges
);
490 // at the moment, this never happens, as there are no cycles in
491 // the range queries and no devices on the master side of a bus
492 // (CPU, cache, bridge etc) actually care about the ranges of the
493 // ports they are connected to
495 DPRINTF(BusAddrRanges
, "Received address range request, returning:\n");
497 // start out with the default range
498 AddrRangeList ranges
;
499 if (useDefaultRange
) {
500 ranges
.push_back(defaultRange
);
501 DPRINTF(BusAddrRanges
, " -- Default %s\n", defaultRange
.to_string());
504 // add any range that is not a subset of the default range
505 for (PortMapConstIter p
= portMap
.begin(); p
!= portMap
.end(); ++p
) {
506 if (useDefaultRange
&& p
->first
.isSubset(defaultRange
)) {
507 DPRINTF(BusAddrRanges
, " -- %s is a subset of default\n",
508 p
->first
.to_string());
510 ranges
.push_back(p
->first
);
511 DPRINTF(BusAddrRanges
, " -- %s\n", p
->first
.to_string());
519 BaseBus::deviceBlockSize() const
524 template <typename PortClass
>
526 BaseBus::Layer
<PortClass
>::drain(DrainManager
*dm
)
528 //We should check that we're not "doing" anything, and that noone is
529 //waiting. We might be idle but have someone waiting if the device we
530 //contacted for a retry didn't actually retry.
531 if (!retryList
.empty() || state
!= IDLE
) {
532 DPRINTF(Drain
, "Bus not drained\n");
540 * Bus layer template instantiations. Could be removed with _impl.hh
541 * file, but since there are only two given options (MasterPort and
542 * SlavePort) it seems a bit excessive at this point.
544 template class BaseBus::Layer
<SlavePort
>;
545 template class BaseBus::Layer
<MasterPort
>;