2 * Copyright (c) 2011-2012 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
, int 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
, int 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 current time rounded to the closest following
137 Tick now
= nextCycle();
139 Tick headerTime
= now
+ headerCycles
* clock
;
141 // The packet will be sent. Figure out how long it occupies the bus, and
142 // how much of that time is for the first "word", aka bus width.
144 if (pkt
->hasData()) {
145 // If a packet has data, it needs ceil(size/width) cycles to send it
146 int dataSize
= pkt
->getSize();
147 numCycles
+= dataSize
/width
;
148 if (dataSize
% width
)
152 // The first word will be delivered after the current tick, the delivery
153 // of the address if any, and one bus cycle to deliver the data
154 pkt
->firstWordTime
= headerTime
+ clock
;
156 pkt
->finishTime
= headerTime
+ numCycles
* clock
;
161 template <typename PortClass
>
162 BaseBus::Layer
<PortClass
>::Layer(BaseBus
& _bus
, const std::string
& _name
,
164 bus(_bus
), _name(_name
), state(IDLE
), clock(_clock
), drainEvent(NULL
),
169 template <typename PortClass
>
170 void BaseBus::Layer
<PortClass
>::occupyLayer(Tick until
)
172 // ensure the state is busy or in retry and never idle at this
173 // point, as the bus should transition from idle as soon as it has
174 // decided to forward the packet to prevent any follow-on calls to
175 // sendTiming seeing an unoccupied bus
176 assert(state
!= IDLE
);
178 // note that we do not change the bus state here, if we are going
179 // from idle to busy it is handled by tryTiming, and if we
180 // are in retry we should remain in retry such that
181 // succeededTiming still sees the accurate state
183 // until should never be 0 as express snoops never occupy the bus
185 bus
.schedule(releaseEvent
, until
);
187 DPRINTF(BaseBus
, "The bus is now busy from tick %d to %d\n",
191 template <typename PortClass
>
193 BaseBus::Layer
<PortClass
>::tryTiming(PortClass
* port
)
195 // first we see if the bus is busy, next we check if we are in a
196 // retry with a port other than the current one
197 if (state
== BUSY
|| (state
== RETRY
&& port
!= retryList
.front())) {
198 // put the port at the end of the retry list
199 retryList
.push_back(port
);
203 // update the state which is shared for request, response and
204 // snoop responses, if we were idle we are now busy, if we are in
205 // a retry, then do not change
212 template <typename PortClass
>
214 BaseBus::Layer
<PortClass
>::succeededTiming(Tick busy_time
)
216 // if a retrying port succeeded, also take it off the retry list
217 if (state
== RETRY
) {
218 DPRINTF(BaseBus
, "Remove retry from list %s\n",
219 retryList
.front()->name());
220 retryList
.pop_front();
224 // we should either have gone from idle to busy in the
225 // tryTiming test, or just gone from a retry to busy
226 assert(state
== BUSY
);
228 // occupy the bus accordingly
229 occupyLayer(busy_time
);
232 template <typename PortClass
>
234 BaseBus::Layer
<PortClass
>::failedTiming(PortClass
* port
, Tick busy_time
)
236 // if we are not in a retry, i.e. busy (but never idle), or we are
237 // in a retry but not for the current port, then add the port at
238 // the end of the retry list
239 if (state
!= RETRY
|| port
!= retryList
.front()) {
240 retryList
.push_back(port
);
243 // even if we retried the current one and did not succeed,
244 // we are no longer retrying but instead busy
247 // occupy the bus accordingly
248 occupyLayer(busy_time
);
251 template <typename PortClass
>
253 BaseBus::Layer
<PortClass
>::releaseLayer()
255 // releasing the bus means we should now be idle
256 assert(state
== BUSY
);
257 assert(!releaseEvent
.scheduled());
262 // bus is now idle, so if someone is waiting we can retry
263 if (!retryList
.empty()) {
264 // note that we block (return false on recvTiming) both
265 // because the bus is busy and because the destination is
266 // busy, and in the latter case the bus may be released before
267 // we see a retry from the destination
269 } else if (drainEvent
) {
270 DPRINTF(Drain
, "Bus done draining, processing drain event\n");
271 //If we weren't able to drain before, do it now.
272 drainEvent
->process();
273 // Clear the drain event once we're done with it.
278 template <typename PortClass
>
280 BaseBus::Layer
<PortClass
>::retryWaiting()
282 // this should never be called with an empty retry list
283 assert(!retryList
.empty());
285 // we always go to retrying from idle
286 assert(state
== IDLE
);
288 // update the state which is shared for request, response and
292 // note that we might have blocked on the receiving port being
293 // busy (rather than the bus itself) and now call retry before the
294 // destination called retry on the bus
295 retryList
.front()->sendRetry();
297 // If the bus is still in the retry state, sendTiming wasn't
298 // called in zero time (e.g. the cache does this)
299 if (state
== RETRY
) {
300 retryList
.pop_front();
302 //Burn a cycle for the missed grant.
304 // update the state which is shared for request, response and
308 // determine the current time rounded to the closest following
310 Tick now
= bus
.nextCycle();
312 occupyLayer(now
+ clock
);
316 template <typename PortClass
>
318 BaseBus::Layer
<PortClass
>::recvRetry()
320 // we got a retry from a peer that we tried to send something to
321 // and failed, but we sent it on the account of someone else, and
322 // that source port should be on our retry list, however if the
323 // bus layer is released before this happens and the retry (from
324 // the bus point of view) is successful then this no longer holds
325 // and we could in fact have an empty retry list
326 if (retryList
.empty())
329 // if the bus layer is idle
331 // note that we do not care who told us to retry at the moment, we
332 // merely let the first one on the retry list go
338 BaseBus::findPort(Addr addr
)
340 // we should never see any address lookups before we've got the
341 // ranges of all connected slave modules
342 assert(gotAllAddrRanges
);
345 PortID dest_id
= checkPortCache(addr
);
346 if (dest_id
!= InvalidPortID
)
349 // Check the address map interval tree
350 PortMapConstIter i
= portMap
.find(addr
);
351 if (i
!= portMap
.end()) {
353 updatePortCache(dest_id
, i
->first
);
357 // Check if this matches the default range
358 if (useDefaultRange
) {
359 if (defaultRange
== addr
) {
360 DPRINTF(BusAddrRanges
, " found addr %#llx on default\n",
362 return defaultPortID
;
364 } else if (defaultPortID
!= InvalidPortID
) {
365 DPRINTF(BusAddrRanges
, "Unable to find destination for addr %#llx, "
366 "will use default port\n", addr
);
367 return defaultPortID
;
370 // we should use the range for the default port and it did not
371 // match, or the default port is not set
372 fatal("Unable to find destination for addr %#llx on bus %s\n", addr
,
376 /** Function called by the port when the bus is receiving a range change.*/
378 BaseBus::recvRangeChange(PortID master_port_id
)
380 // remember that we got a range from this master port and thus the
381 // connected slave module
382 gotAddrRanges
[master_port_id
] = true;
384 // update the global flag
385 if (!gotAllAddrRanges
) {
386 // take a logical AND of all the ports and see if we got
387 // ranges from everyone
388 gotAllAddrRanges
= true;
389 std::vector
<bool>::const_iterator r
= gotAddrRanges
.begin();
390 while (gotAllAddrRanges
&& r
!= gotAddrRanges
.end()) {
391 gotAllAddrRanges
&= *r
++;
395 // note that we could get the range from the default port at any
396 // point in time, and we cannot assume that the default range is
397 // set before the other ones are, so we do additional checks once
398 // all ranges are provided
399 DPRINTF(BusAddrRanges
, "received RangeChange from slave port %s\n",
400 masterPorts
[master_port_id
]->getSlavePort().name());
402 if (master_port_id
== defaultPortID
) {
403 // only update if we are indeed checking ranges for the
404 // default port since the port might not have a valid range
406 if (useDefaultRange
) {
407 AddrRangeList ranges
= masterPorts
[master_port_id
]->getAddrRanges();
409 if (ranges
.size() != 1)
410 fatal("Bus %s may only have a single default range",
413 defaultRange
= ranges
.front();
416 // the ports are allowed to update their address ranges
417 // dynamically, so remove any existing entries
418 if (gotAddrRanges
[master_port_id
]) {
419 for (PortMapIter p
= portMap
.begin(); p
!= portMap
.end(); ) {
420 if (p
->second
== master_port_id
)
421 // erasing invalidates the iterator, so advance it
422 // before the deletion takes place
429 AddrRangeList ranges
= masterPorts
[master_port_id
]->getAddrRanges();
431 for (AddrRangeConstIter r
= ranges
.begin(); r
!= ranges
.end(); ++r
) {
432 DPRINTF(BusAddrRanges
, "Adding range %#llx : %#llx for id %d\n",
433 r
->start
, r
->end
, master_port_id
);
434 if (portMap
.insert(*r
, master_port_id
) == portMap
.end()) {
435 PortID conflict_id
= portMap
.find(*r
)->second
;
436 fatal("%s has two ports with same range:\n\t%s\n\t%s\n",
438 masterPorts
[master_port_id
]->getSlavePort().name(),
439 masterPorts
[conflict_id
]->getSlavePort().name());
444 // if we have received ranges from all our neighbouring slave
445 // modules, go ahead and tell our connected master modules in
446 // turn, this effectively assumes a tree structure of the system
447 if (gotAllAddrRanges
) {
448 // also check that no range partially overlaps with the
449 // default range, this has to be done after all ranges are set
450 // as there are no guarantees for when the default range is
451 // update with respect to the other ones
452 if (useDefaultRange
) {
453 for (PortID port_id
= 0; port_id
< masterPorts
.size(); ++port_id
) {
454 if (port_id
== defaultPortID
) {
455 if (!gotAddrRanges
[port_id
])
456 fatal("Bus %s uses default range, but none provided",
459 AddrRangeList ranges
=
460 masterPorts
[port_id
]->getAddrRanges();
462 for (AddrRangeConstIter r
= ranges
.begin();
463 r
!= ranges
.end(); ++r
) {
464 // see if the new range is partially
465 // overlapping the default range
466 if (r
->intersects(defaultRange
) &&
467 !r
->isSubset(defaultRange
))
468 fatal("Range %#llx : %#llx intersects the " \
469 "default range of %s but is not a " \
470 "subset\n", r
->start
, r
->end
, name());
476 // tell all our neighbouring master ports that our address
477 // ranges have changed
478 for (SlavePortConstIter s
= slavePorts
.begin(); s
!= slavePorts
.end();
480 (*s
)->sendRangeChange();
487 BaseBus::getAddrRanges() const
489 // we should never be asked without first having sent a range
490 // change, and the latter is only done once we have all the ranges
491 // of the connected devices
492 assert(gotAllAddrRanges
);
494 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
496 // start out with the default range
497 AddrRangeList ranges
;
498 ranges
.push_back(defaultRange
);
499 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx DEFAULT\n",
500 defaultRange
.start
, defaultRange
.end
);
502 // add any range that is not a subset of the default range
503 for (PortMapConstIter p
= portMap
.begin(); p
!= portMap
.end(); ++p
) {
504 if (useDefaultRange
&& p
->first
.isSubset(defaultRange
)) {
505 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
506 p
->first
.start
, p
->first
.end
);
508 ranges
.push_back(p
->first
);
509 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
510 p
->first
.start
, p
->first
.end
);
518 BaseBus::deviceBlockSize() const
523 template <typename PortClass
>
525 BaseBus::Layer
<PortClass
>::drain(Event
* de
)
527 //We should check that we're not "doing" anything, and that noone is
528 //waiting. We might be idle but have someone waiting if the device we
529 //contacted for a retry didn't actually retry.
530 if (!retryList
.empty() || state
!= IDLE
) {
531 DPRINTF(Drain
, "Bus not drained\n");
539 * Bus layer template instantiations. Could be removed with _impl.hh
540 * file, but since there are only two given options (MasterPort and
541 * SlavePort) it seems a bit excessive at this point.
543 template class BaseBus::Layer
<SlavePort
>;
544 template class BaseBus::Layer
<MasterPort
>;