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 defaultPortID(InvalidPortID
),
61 useDefaultRange(p
->use_default_range
),
62 blockSize(p
->block_size
)
67 for (MasterPortIter m
= masterPorts
.begin(); m
!= masterPorts
.end();
72 for (SlavePortIter s
= slavePorts
.begin(); s
!= slavePorts
.end();
81 // determine the maximum peer block size, look at both the
82 // connected master and slave modules
83 uint32_t peer_block_size
= 0;
85 for (MasterPortConstIter m
= masterPorts
.begin(); m
!= masterPorts
.end();
87 peer_block_size
= std::max((*m
)->peerBlockSize(), peer_block_size
);
90 for (SlavePortConstIter s
= slavePorts
.begin(); s
!= slavePorts
.end();
92 peer_block_size
= std::max((*s
)->peerBlockSize(), peer_block_size
);
95 // if the peers do not have a block size, use the default value
96 // set through the bus parameters
97 if (peer_block_size
!= 0)
98 blockSize
= peer_block_size
;
100 // check if the block size is a value known to work
101 if (blockSize
!= 16 || blockSize
!= 32 || blockSize
!= 64 ||
103 warn_once("Block size is neither 16, 32, 64 or 128 bytes.\n");
107 BaseBus::getMasterPort(const std::string
&if_name
, int idx
)
109 if (if_name
== "master" && idx
< masterPorts
.size()) {
110 // the master port index translates directly to the vector position
111 return *masterPorts
[idx
];
112 } else if (if_name
== "default") {
113 return *masterPorts
[defaultPortID
];
115 return MemObject::getMasterPort(if_name
, idx
);
120 BaseBus::getSlavePort(const std::string
&if_name
, int idx
)
122 if (if_name
== "slave" && idx
< slavePorts
.size()) {
123 // the slave port index translates directly to the vector position
124 return *slavePorts
[idx
];
126 return MemObject::getSlavePort(if_name
, idx
);
131 BaseBus::calcPacketTiming(PacketPtr pkt
)
133 // determine the current time rounded to the closest following
135 Tick now
= nextCycle();
137 Tick headerTime
= now
+ headerCycles
* clock
;
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 int dataSize
= pkt
->getSize();
145 numCycles
+= dataSize
/width
;
146 if (dataSize
% width
)
150 // The first word will be delivered after the current tick, the delivery
151 // of the address if any, and one bus cycle to deliver the data
152 pkt
->firstWordTime
= headerTime
+ clock
;
154 pkt
->finishTime
= headerTime
+ numCycles
* clock
;
159 template <typename PortClass
>
160 BaseBus::Layer
<PortClass
>::Layer(BaseBus
& _bus
, const std::string
& _name
,
162 bus(_bus
), _name(_name
), state(IDLE
), clock(_clock
), drainEvent(NULL
),
167 template <typename PortClass
>
168 void BaseBus::Layer
<PortClass
>::occupyLayer(Tick until
)
170 // ensure the state is busy or in retry and never idle at this
171 // point, as the bus should transition from idle as soon as it has
172 // decided to forward the packet to prevent any follow-on calls to
173 // sendTiming seeing an unoccupied bus
174 assert(state
!= IDLE
);
176 // note that we do not change the bus state here, if we are going
177 // from idle to busy it is handled by tryTiming, and if we
178 // are in retry we should remain in retry such that
179 // succeededTiming still sees the accurate state
181 // until should never be 0 as express snoops never occupy the bus
183 bus
.schedule(releaseEvent
, until
);
185 DPRINTF(BaseBus
, "The bus is now busy from tick %d to %d\n",
189 template <typename PortClass
>
191 BaseBus::Layer
<PortClass
>::tryTiming(PortClass
* port
)
193 // first we see if the bus is busy, next we check if we are in a
194 // retry with a port other than the current one
195 if (state
== BUSY
|| (state
== RETRY
&& port
!= retryList
.front())) {
196 // put the port at the end of the retry list
197 retryList
.push_back(port
);
201 // update the state which is shared for request, response and
202 // snoop responses, if we were idle we are now busy, if we are in
203 // a retry, then do not change
210 template <typename PortClass
>
212 BaseBus::Layer
<PortClass
>::succeededTiming(Tick busy_time
)
214 // if a retrying port succeeded, also take it off the retry list
215 if (state
== RETRY
) {
216 DPRINTF(BaseBus
, "Remove retry from list %s\n",
217 retryList
.front()->name());
218 retryList
.pop_front();
222 // we should either have gone from idle to busy in the
223 // tryTiming test, or just gone from a retry to busy
224 assert(state
== BUSY
);
226 // occupy the bus accordingly
227 occupyLayer(busy_time
);
230 template <typename PortClass
>
232 BaseBus::Layer
<PortClass
>::failedTiming(PortClass
* port
, Tick busy_time
)
234 // if we are not in a retry, i.e. busy (but never idle), or we are
235 // in a retry but not for the current port, then add the port at
236 // the end of the retry list
237 if (state
!= RETRY
|| port
!= retryList
.front()) {
238 retryList
.push_back(port
);
241 // even if we retried the current one and did not succeed,
242 // we are no longer retrying but instead busy
245 // occupy the bus accordingly
246 occupyLayer(busy_time
);
249 template <typename PortClass
>
251 BaseBus::Layer
<PortClass
>::releaseLayer()
253 // releasing the bus means we should now be idle
254 assert(state
== BUSY
);
255 assert(!releaseEvent
.scheduled());
260 // bus is now idle, so if someone is waiting we can retry
261 if (!retryList
.empty()) {
262 // note that we block (return false on recvTiming) both
263 // because the bus is busy and because the destination is
264 // busy, and in the latter case the bus may be released before
265 // we see a retry from the destination
267 } else if (drainEvent
) {
268 DPRINTF(Drain
, "Bus done draining, processing drain event\n");
269 //If we weren't able to drain before, do it now.
270 drainEvent
->process();
271 // Clear the drain event once we're done with it.
276 template <typename PortClass
>
278 BaseBus::Layer
<PortClass
>::retryWaiting()
280 // this should never be called with an empty retry list
281 assert(!retryList
.empty());
283 // we always go to retrying from idle
284 assert(state
== IDLE
);
286 // update the state which is shared for request, response and
290 // note that we might have blocked on the receiving port being
291 // busy (rather than the bus itself) and now call retry before the
292 // destination called retry on the bus
293 retryList
.front()->sendRetry();
295 // If the bus is still in the retry state, sendTiming wasn't
296 // called in zero time (e.g. the cache does this)
297 if (state
== RETRY
) {
298 retryList
.pop_front();
300 //Burn a cycle for the missed grant.
302 // update the state which is shared for request, response and
306 // determine the current time rounded to the closest following
308 Tick now
= bus
.nextCycle();
310 occupyLayer(now
+ clock
);
314 template <typename PortClass
>
316 BaseBus::Layer
<PortClass
>::recvRetry()
318 // we got a retry from a peer that we tried to send something to
319 // and failed, but we sent it on the account of someone else, and
320 // that source port should be on our retry list, however if the
321 // bus layer is released before this happens and the retry (from
322 // the bus point of view) is successful then this no longer holds
323 // and we could in fact have an empty retry list
324 if (retryList
.empty())
327 // if the bus layer is idle
329 // note that we do not care who told us to retry at the moment, we
330 // merely let the first one on the retry list go
336 BaseBus::findPort(Addr addr
)
338 /* An interval tree would be a better way to do this. --ali. */
339 PortID dest_id
= checkPortCache(addr
);
340 if (dest_id
!= InvalidPortID
)
343 // Check normal port ranges
344 PortMapConstIter i
= portMap
.find(RangeSize(addr
,1));
345 if (i
!= portMap
.end()) {
347 updatePortCache(dest_id
, i
->first
.start
, i
->first
.end
);
351 // Check if this matches the default range
352 if (useDefaultRange
) {
353 AddrRangeConstIter a_end
= defaultRange
.end();
354 for (AddrRangeConstIter i
= defaultRange
.begin(); i
!= a_end
; i
++) {
356 DPRINTF(BusAddrRanges
, " found addr %#llx on default\n",
358 return defaultPortID
;
361 } else if (defaultPortID
!= InvalidPortID
) {
362 DPRINTF(BusAddrRanges
, "Unable to find destination for addr %#llx, "
363 "will use default port\n", addr
);
364 return defaultPortID
;
367 // we should use the range for the default port and it did not
368 // match, or the default port is not set
369 fatal("Unable to find destination for addr %#llx on bus %s\n", addr
,
373 /** Function called by the port when the bus is receiving a range change.*/
375 BaseBus::recvRangeChange(PortID master_port_id
)
379 if (inRecvRangeChange
.count(master_port_id
))
381 inRecvRangeChange
.insert(master_port_id
);
383 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n",
387 if (master_port_id
== defaultPortID
) {
388 defaultRange
.clear();
389 // Only try to update these ranges if the user set a default responder.
390 if (useDefaultRange
) {
391 // get the address ranges of the connected slave port
392 AddrRangeList ranges
=
393 masterPorts
[master_port_id
]->getAddrRanges();
394 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
395 defaultRange
.push_back(*iter
);
396 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
397 iter
->start
, iter
->end
);
402 assert(master_port_id
< masterPorts
.size() && master_port_id
>= 0);
403 MasterPort
*port
= masterPorts
[master_port_id
];
405 // Clean out any previously existent ids
406 for (PortMapIter portIter
= portMap
.begin();
407 portIter
!= portMap
.end(); ) {
408 if (portIter
->second
== master_port_id
)
409 portMap
.erase(portIter
++);
414 // get the address ranges of the connected slave port
415 AddrRangeList ranges
= port
->getAddrRanges();
417 for (iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
418 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
419 iter
->start
, iter
->end
, master_port_id
);
420 if (portMap
.insert(*iter
, master_port_id
) == portMap
.end()) {
421 PortID conflict_id
= portMap
.find(*iter
)->second
;
422 fatal("%s has two ports with same range:\n\t%s\n\t%s\n",
424 masterPorts
[master_port_id
]->getSlavePort().name(),
425 masterPorts
[conflict_id
]->getSlavePort().name());
429 DPRINTF(BusAddrRanges
, "port list has %d entries\n", portMap
.size());
431 // tell all our neighbouring master ports that our address range
433 for (SlavePortConstIter p
= slavePorts
.begin(); p
!= slavePorts
.end();
435 (*p
)->sendRangeChange();
437 inRecvRangeChange
.erase(master_port_id
);
441 BaseBus::getAddrRanges() const
443 AddrRangeList ranges
;
445 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
447 for (AddrRangeConstIter dflt_iter
= defaultRange
.begin();
448 dflt_iter
!= defaultRange
.end(); dflt_iter
++) {
449 ranges
.push_back(*dflt_iter
);
450 DPRINTF(BusAddrRanges
, " -- Dflt: %#llx : %#llx\n",dflt_iter
->start
,
453 for (PortMapConstIter portIter
= portMap
.begin();
454 portIter
!= portMap
.end(); portIter
++) {
456 for (AddrRangeConstIter dflt_iter
= defaultRange
.begin();
457 dflt_iter
!= defaultRange
.end(); dflt_iter
++) {
458 if ((portIter
->first
.start
< dflt_iter
->start
&&
459 portIter
->first
.end
>= dflt_iter
->start
) ||
460 (portIter
->first
.start
< dflt_iter
->end
&&
461 portIter
->first
.end
>= dflt_iter
->end
))
462 fatal("Devices can not set ranges that itersect the default set\
463 but are not a subset of the default set.\n");
464 if (portIter
->first
.start
>= dflt_iter
->start
&&
465 portIter
->first
.end
<= dflt_iter
->end
) {
467 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
468 portIter
->first
.start
, portIter
->first
.end
);
472 ranges
.push_back(portIter
->first
);
473 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
474 portIter
->first
.start
, portIter
->first
.end
);
482 BaseBus::deviceBlockSize() const
487 template <typename PortClass
>
489 BaseBus::Layer
<PortClass
>::drain(Event
* de
)
491 //We should check that we're not "doing" anything, and that noone is
492 //waiting. We might be idle but have someone waiting if the device we
493 //contacted for a retry didn't actually retry.
494 if (!retryList
.empty() || state
!= IDLE
) {
495 DPRINTF(Drain
, "Bus not drained\n");
503 * Bus layer template instantiations. Could be removed with _impl.hh
504 * file, but since there are only two given options (MasterPort and
505 * SlavePort) it seems a bit excessive at this point.
507 template class BaseBus::Layer
<SlavePort
>;
508 template class BaseBus::Layer
<MasterPort
>;