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/intmath.hh"
51 #include "base/misc.hh"
52 #include "base/trace.hh"
53 #include "debug/Bus.hh"
54 #include "debug/BusAddrRanges.hh"
57 BaseBus::BaseBus(const BaseBusParams
*p
)
58 : MemObject(p
), clock(p
->clock
),
59 headerCycles(p
->header_cycles
), width(p
->width
),
60 defaultPortID(InvalidPortID
),
61 useDefaultRange(p
->use_default_range
),
62 defaultBlockSize(p
->block_size
),
63 cachedBlockSize(0), cachedBlockSizeValid(false)
65 //width, clock period, and header cycles must be positive
67 fatal("Bus width must be positive\n");
69 fatal("Bus clock period must be positive\n");
70 if (headerCycles
<= 0)
71 fatal("Number of header cycles must be positive\n");
76 for (MasterPortIter m
= masterPorts
.begin(); m
!= masterPorts
.end();
81 for (SlavePortIter s
= slavePorts
.begin(); s
!= slavePorts
.end();
88 BaseBus::getMasterPort(const std::string
&if_name
, int idx
)
90 if (if_name
== "master" && idx
< masterPorts
.size()) {
91 // the master port index translates directly to the vector position
92 return *masterPorts
[idx
];
93 } else if (if_name
== "default") {
94 return *masterPorts
[defaultPortID
];
96 return MemObject::getMasterPort(if_name
, idx
);
101 BaseBus::getSlavePort(const std::string
&if_name
, int idx
)
103 if (if_name
== "slave" && idx
< slavePorts
.size()) {
104 // the slave port index translates directly to the vector position
105 return *slavePorts
[idx
];
107 return MemObject::getSlavePort(if_name
, idx
);
112 BaseBus::calcPacketTiming(PacketPtr pkt
)
114 // determine the current time rounded to the closest following
116 Tick now
= divCeil(curTick(), clock
) * clock
;
118 Tick headerTime
= now
+ headerCycles
* clock
;
120 // The packet will be sent. Figure out how long it occupies the bus, and
121 // how much of that time is for the first "word", aka bus width.
123 if (pkt
->hasData()) {
124 // If a packet has data, it needs ceil(size/width) cycles to send it
125 int dataSize
= pkt
->getSize();
126 numCycles
+= dataSize
/width
;
127 if (dataSize
% width
)
131 // The first word will be delivered after the current tick, the delivery
132 // of the address if any, and one bus cycle to deliver the data
133 pkt
->firstWordTime
= headerTime
+ clock
;
135 pkt
->finishTime
= headerTime
+ numCycles
* clock
;
140 template <typename PortClass
>
141 BaseBus::Layer
<PortClass
>::Layer(BaseBus
& _bus
, const std::string
& _name
,
143 bus(_bus
), _name(_name
), state(IDLE
), clock(_clock
), drainEvent(NULL
),
148 template <typename PortClass
>
149 void BaseBus::Layer
<PortClass
>::occupyLayer(Tick until
)
151 // ensure the state is busy or in retry and never idle at this
152 // point, as the bus should transition from idle as soon as it has
153 // decided to forward the packet to prevent any follow-on calls to
154 // sendTiming seeing an unoccupied bus
155 assert(state
!= IDLE
);
157 // note that we do not change the bus state here, if we are going
158 // from idle to busy it is handled by tryTiming, and if we
159 // are in retry we should remain in retry such that
160 // succeededTiming still sees the accurate state
162 // until should never be 0 as express snoops never occupy the bus
164 bus
.schedule(releaseEvent
, until
);
166 DPRINTF(BaseBus
, "The bus is now busy from tick %d to %d\n",
170 template <typename PortClass
>
172 BaseBus::Layer
<PortClass
>::tryTiming(PortClass
* port
)
174 // first we see if the bus is busy, next we check if we are in a
175 // retry with a port other than the current one
176 if (state
== BUSY
|| (state
== RETRY
&& port
!= retryList
.front())) {
177 // put the port at the end of the retry list
178 retryList
.push_back(port
);
182 // update the state which is shared for request, response and
183 // snoop responses, if we were idle we are now busy, if we are in
184 // a retry, then do not change
191 template <typename PortClass
>
193 BaseBus::Layer
<PortClass
>::succeededTiming(Tick busy_time
)
195 // if a retrying port succeeded, also take it off the retry list
196 if (state
== RETRY
) {
197 DPRINTF(BaseBus
, "Remove retry from list %s\n",
198 retryList
.front()->name());
199 retryList
.pop_front();
203 // we should either have gone from idle to busy in the
204 // tryTiming test, or just gone from a retry to busy
205 assert(state
== BUSY
);
207 // occupy the bus accordingly
208 occupyLayer(busy_time
);
211 template <typename PortClass
>
213 BaseBus::Layer
<PortClass
>::failedTiming(PortClass
* port
, Tick busy_time
)
215 // if we are not in a retry, i.e. busy (but never idle), or we are
216 // in a retry but not for the current port, then add the port at
217 // the end of the retry list
218 if (state
!= RETRY
|| port
!= retryList
.front()) {
219 retryList
.push_back(port
);
222 // even if we retried the current one and did not succeed,
223 // we are no longer retrying but instead busy
226 // occupy the bus accordingly
227 occupyLayer(busy_time
);
230 template <typename PortClass
>
232 BaseBus::Layer
<PortClass
>::releaseLayer()
234 // releasing the bus means we should now be idle
235 assert(state
== BUSY
);
236 assert(!releaseEvent
.scheduled());
241 // bus is now idle, so if someone is waiting we can retry
242 if (!retryList
.empty()) {
243 // note that we block (return false on recvTiming) both
244 // because the bus is busy and because the destination is
245 // busy, and in the latter case the bus may be released before
246 // we see a retry from the destination
248 } else if (drainEvent
) {
249 //If we weren't able to drain before, do it now.
250 drainEvent
->process();
251 // Clear the drain event once we're done with it.
256 template <typename PortClass
>
258 BaseBus::Layer
<PortClass
>::retryWaiting()
260 // this should never be called with an empty retry list
261 assert(!retryList
.empty());
263 // we always go to retrying from idle
264 assert(state
== IDLE
);
266 // update the state which is shared for request, response and
270 // note that we might have blocked on the receiving port being
271 // busy (rather than the bus itself) and now call retry before the
272 // destination called retry on the bus
273 retryList
.front()->sendRetry();
275 // If the bus is still in the retry state, sendTiming wasn't
276 // called in zero time (e.g. the cache does this)
277 if (state
== RETRY
) {
278 retryList
.pop_front();
280 //Burn a cycle for the missed grant.
282 // update the state which is shared for request, response and
286 // determine the current time rounded to the closest following
288 Tick now
= divCeil(curTick(), clock
) * clock
;
290 occupyLayer(now
+ clock
);
294 template <typename PortClass
>
296 BaseBus::Layer
<PortClass
>::recvRetry()
298 // we got a retry from a peer that we tried to send something to
299 // and failed, but we sent it on the account of someone else, and
300 // that source port should be on our retry list, however if the
301 // bus layer is released before this happens and the retry (from
302 // the bus point of view) is successful then this no longer holds
303 // and we could in fact have an empty retry list
304 if (retryList
.empty())
307 // if the bus layer is idle
309 // note that we do not care who told us to retry at the moment, we
310 // merely let the first one on the retry list go
316 BaseBus::findPort(Addr addr
)
318 /* An interval tree would be a better way to do this. --ali. */
319 PortID dest_id
= checkPortCache(addr
);
320 if (dest_id
!= InvalidPortID
)
323 // Check normal port ranges
324 PortMapConstIter i
= portMap
.find(RangeSize(addr
,1));
325 if (i
!= portMap
.end()) {
327 updatePortCache(dest_id
, i
->first
.start
, i
->first
.end
);
331 // Check if this matches the default range
332 if (useDefaultRange
) {
333 AddrRangeConstIter a_end
= defaultRange
.end();
334 for (AddrRangeConstIter i
= defaultRange
.begin(); i
!= a_end
; i
++) {
336 DPRINTF(BusAddrRanges
, " found addr %#llx on default\n",
338 return defaultPortID
;
341 } else if (defaultPortID
!= InvalidPortID
) {
342 DPRINTF(BusAddrRanges
, "Unable to find destination for addr %#llx, "
343 "will use default port\n", addr
);
344 return defaultPortID
;
347 // we should use the range for the default port and it did not
348 // match, or the default port is not set
349 fatal("Unable to find destination for addr %#llx on bus %s\n", addr
,
353 /** Function called by the port when the bus is receiving a range change.*/
355 BaseBus::recvRangeChange(PortID master_port_id
)
357 AddrRangeList ranges
;
360 if (inRecvRangeChange
.count(master_port_id
))
362 inRecvRangeChange
.insert(master_port_id
);
364 DPRINTF(BusAddrRanges
, "received RangeChange from device id %d\n",
368 if (master_port_id
== defaultPortID
) {
369 defaultRange
.clear();
370 // Only try to update these ranges if the user set a default responder.
371 if (useDefaultRange
) {
372 // get the address ranges of the connected slave port
373 AddrRangeList ranges
=
374 masterPorts
[master_port_id
]->getAddrRanges();
375 for(iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
376 defaultRange
.push_back(*iter
);
377 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for default range\n",
378 iter
->start
, iter
->end
);
383 assert(master_port_id
< masterPorts
.size() && master_port_id
>= 0);
384 MasterPort
*port
= masterPorts
[master_port_id
];
386 // Clean out any previously existent ids
387 for (PortMapIter portIter
= portMap
.begin();
388 portIter
!= portMap
.end(); ) {
389 if (portIter
->second
== master_port_id
)
390 portMap
.erase(portIter
++);
395 // get the address ranges of the connected slave port
396 ranges
= port
->getAddrRanges();
398 for (iter
= ranges
.begin(); iter
!= ranges
.end(); iter
++) {
399 DPRINTF(BusAddrRanges
, "Adding range %#llx - %#llx for id %d\n",
400 iter
->start
, iter
->end
, master_port_id
);
401 if (portMap
.insert(*iter
, master_port_id
) == portMap
.end()) {
402 PortID conflict_id
= portMap
.find(*iter
)->second
;
403 fatal("%s has two ports with same range:\n\t%s\n\t%s\n",
405 masterPorts
[master_port_id
]->getSlavePort().name(),
406 masterPorts
[conflict_id
]->getSlavePort().name());
410 DPRINTF(BusAddrRanges
, "port list has %d entries\n", portMap
.size());
412 // tell all our neighbouring master ports that our address range
414 for (SlavePortConstIter p
= slavePorts
.begin(); p
!= slavePorts
.end();
416 (*p
)->sendRangeChange();
418 inRecvRangeChange
.erase(master_port_id
);
422 BaseBus::getAddrRanges() const
424 AddrRangeList ranges
;
426 DPRINTF(BusAddrRanges
, "received address range request, returning:\n");
428 for (AddrRangeConstIter dflt_iter
= defaultRange
.begin();
429 dflt_iter
!= defaultRange
.end(); dflt_iter
++) {
430 ranges
.push_back(*dflt_iter
);
431 DPRINTF(BusAddrRanges
, " -- Dflt: %#llx : %#llx\n",dflt_iter
->start
,
434 for (PortMapConstIter portIter
= portMap
.begin();
435 portIter
!= portMap
.end(); portIter
++) {
437 for (AddrRangeConstIter dflt_iter
= defaultRange
.begin();
438 dflt_iter
!= defaultRange
.end(); dflt_iter
++) {
439 if ((portIter
->first
.start
< dflt_iter
->start
&&
440 portIter
->first
.end
>= dflt_iter
->start
) ||
441 (portIter
->first
.start
< dflt_iter
->end
&&
442 portIter
->first
.end
>= dflt_iter
->end
))
443 fatal("Devices can not set ranges that itersect the default set\
444 but are not a subset of the default set.\n");
445 if (portIter
->first
.start
>= dflt_iter
->start
&&
446 portIter
->first
.end
<= dflt_iter
->end
) {
448 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx is a SUBSET\n",
449 portIter
->first
.start
, portIter
->first
.end
);
453 ranges
.push_back(portIter
->first
);
454 DPRINTF(BusAddrRanges
, " -- %#llx : %#llx\n",
455 portIter
->first
.start
, portIter
->first
.end
);
463 BaseBus::findBlockSize()
465 if (cachedBlockSizeValid
)
466 return cachedBlockSize
;
470 for (MasterPortConstIter m
= masterPorts
.begin(); m
!= masterPorts
.end();
472 unsigned tmp_bs
= (*m
)->peerBlockSize();
477 for (SlavePortConstIter s
= slavePorts
.begin(); s
!= slavePorts
.end();
479 unsigned tmp_bs
= (*s
)->peerBlockSize();
484 max_bs
= defaultBlockSize
;
487 warn_once("Blocksize found to not be 64... hmm... probably not.\n");
488 cachedBlockSize
= max_bs
;
489 cachedBlockSizeValid
= true;
493 template <typename PortClass
>
495 BaseBus::Layer
<PortClass
>::drain(Event
* de
)
497 //We should check that we're not "doing" anything, and that noone is
498 //waiting. We might be idle but have someone waiting if the device we
499 //contacted for a retry didn't actually retry.
500 if (!retryList
.empty() || state
!= IDLE
) {
508 * Bus layer template instantiations. Could be removed with _impl.hh
509 * file, but since there are only two given options (MasterPort and
510 * SlavePort) it seems a bit excessive at this point.
512 template class BaseBus::Layer
<SlavePort
>;
513 template class BaseBus::Layer
<MasterPort
>;