* Definition of a bus object.
*/
-#include "base/intmath.hh"
#include "base/misc.hh"
#include "base/trace.hh"
#include "debug/Bus.hh"
#include "debug/BusAddrRanges.hh"
+#include "debug/Drain.hh"
#include "mem/bus.hh"
BaseBus::BaseBus(const BaseBusParams *p)
- : MemObject(p), clock(p->clock),
+ : MemObject(p),
headerCycles(p->header_cycles), width(p->width),
- defaultPortID(InvalidPortID),
+ gotAddrRanges(p->port_default_connection_count +
+ p->port_master_connection_count, false),
+ gotAllAddrRanges(false), defaultPortID(InvalidPortID),
useDefaultRange(p->use_default_range),
- defaultBlockSize(p->block_size),
- cachedBlockSize(0), cachedBlockSizeValid(false)
-{
- //width, clock period, and header cycles must be positive
- if (width <= 0)
- fatal("Bus width must be positive\n");
- if (clock <= 0)
- fatal("Bus clock period must be positive\n");
- if (headerCycles <= 0)
- fatal("Number of header cycles must be positive\n");
-}
+ blockSize(p->block_size)
+{}
BaseBus::~BaseBus()
{
}
}
-MasterPort &
-BaseBus::getMasterPort(const std::string &if_name, int idx)
+void
+BaseBus::init()
+{
+ // determine the maximum peer block size, look at both the
+ // connected master and slave modules
+ uint32_t peer_block_size = 0;
+
+ for (MasterPortConstIter m = masterPorts.begin(); m != masterPorts.end();
+ ++m) {
+ peer_block_size = std::max((*m)->peerBlockSize(), peer_block_size);
+ }
+
+ for (SlavePortConstIter s = slavePorts.begin(); s != slavePorts.end();
+ ++s) {
+ peer_block_size = std::max((*s)->peerBlockSize(), peer_block_size);
+ }
+
+ // if the peers do not have a block size, use the default value
+ // set through the bus parameters
+ if (peer_block_size != 0)
+ blockSize = peer_block_size;
+
+ // check if the block size is a value known to work
+ if (!(blockSize == 16 || blockSize == 32 || blockSize == 64 ||
+ blockSize == 128))
+ warn_once("Block size is neither 16, 32, 64 or 128 bytes.\n");
+}
+
+BaseMasterPort &
+BaseBus::getMasterPort(const std::string &if_name, PortID idx)
{
if (if_name == "master" && idx < masterPorts.size()) {
// the master port index translates directly to the vector position
}
}
-SlavePort &
-BaseBus::getSlavePort(const std::string &if_name, int idx)
+BaseSlavePort &
+BaseBus::getSlavePort(const std::string &if_name, PortID idx)
{
if (if_name == "slave" && idx < slavePorts.size()) {
// the slave port index translates directly to the vector position
{
// determine the current time rounded to the closest following
// clock edge
- Tick now = divCeil(curTick(), clock) * clock;
+ Tick now = nextCycle();
Tick headerTime = now + headerCycles * clock;
return headerTime;
}
-BaseBus::Layer::Layer(BaseBus& _bus, const std::string& _name, Tick _clock) :
- bus(_bus), _name(_name), state(IDLE), clock(_clock), drainEvent(NULL),
+template <typename PortClass>
+BaseBus::Layer<PortClass>::Layer(BaseBus& _bus, const std::string& _name,
+ Tick _clock) :
+ Drainable(),
+ bus(_bus), _name(_name), state(IDLE), clock(_clock), drainManager(NULL),
releaseEvent(this)
{
}
-void BaseBus::Layer::occupyLayer(Tick until)
+template <typename PortClass>
+void BaseBus::Layer<PortClass>::occupyLayer(Tick until)
{
// ensure the state is busy or in retry and never idle at this
// point, as the bus should transition from idle as soon as it has
curTick(), until);
}
+template <typename PortClass>
bool
-BaseBus::Layer::tryTiming(Port* port)
+BaseBus::Layer<PortClass>::tryTiming(PortClass* port)
{
// first we see if the bus is busy, next we check if we are in a
// retry with a port other than the current one
return true;
}
+template <typename PortClass>
void
-BaseBus::Layer::succeededTiming(Tick busy_time)
+BaseBus::Layer<PortClass>::succeededTiming(Tick busy_time)
{
// if a retrying port succeeded, also take it off the retry list
if (state == RETRY) {
occupyLayer(busy_time);
}
+template <typename PortClass>
void
-BaseBus::Layer::failedTiming(SlavePort* port, Tick busy_time)
+BaseBus::Layer<PortClass>::failedTiming(PortClass* port, Tick busy_time)
{
// if we are not in a retry, i.e. busy (but never idle), or we are
// in a retry but not for the current port, then add the port at
occupyLayer(busy_time);
}
+template <typename PortClass>
void
-BaseBus::Layer::releaseLayer()
+BaseBus::Layer<PortClass>::releaseLayer()
{
// releasing the bus means we should now be idle
assert(state == BUSY);
// busy, and in the latter case the bus may be released before
// we see a retry from the destination
retryWaiting();
- } else if (drainEvent) {
+ } else if (drainManager) {
+ DPRINTF(Drain, "Bus done draining, signaling drain manager\n");
//If we weren't able to drain before, do it now.
- drainEvent->process();
+ drainManager->signalDrainDone();
// Clear the drain event once we're done with it.
- drainEvent = NULL;
+ drainManager = NULL;
}
}
+template <typename PortClass>
void
-BaseBus::Layer::retryWaiting()
+BaseBus::Layer<PortClass>::retryWaiting()
{
// this should never be called with an empty retry list
assert(!retryList.empty());
// note that we might have blocked on the receiving port being
// busy (rather than the bus itself) and now call retry before the
// destination called retry on the bus
- if (dynamic_cast<SlavePort*>(retryList.front()) != NULL)
- (dynamic_cast<SlavePort*>(retryList.front()))->sendRetry();
- else
- (dynamic_cast<MasterPort*>(retryList.front()))->sendRetry();
+ retryList.front()->sendRetry();
// If the bus is still in the retry state, sendTiming wasn't
// called in zero time (e.g. the cache does this)
// determine the current time rounded to the closest following
// clock edge
- Tick now = divCeil(curTick(), clock) * clock;
+ Tick now = bus.nextCycle();
occupyLayer(now + clock);
}
}
+template <typename PortClass>
void
-BaseBus::Layer::recvRetry()
+BaseBus::Layer<PortClass>::recvRetry()
{
// we got a retry from a peer that we tried to send something to
// and failed, but we sent it on the account of someone else, and
PortID
BaseBus::findPort(Addr addr)
{
- /* An interval tree would be a better way to do this. --ali. */
+ // we should never see any address lookups before we've got the
+ // ranges of all connected slave modules
+ assert(gotAllAddrRanges);
+
+ // Check the cache
PortID dest_id = checkPortCache(addr);
if (dest_id != InvalidPortID)
return dest_id;
- // Check normal port ranges
- PortMapConstIter i = portMap.find(RangeSize(addr,1));
+ // Check the address map interval tree
+ PortMapConstIter i = portMap.find(addr);
if (i != portMap.end()) {
dest_id = i->second;
- updatePortCache(dest_id, i->first.start, i->first.end);
+ updatePortCache(dest_id, i->first);
return dest_id;
}
// Check if this matches the default range
if (useDefaultRange) {
- AddrRangeConstIter a_end = defaultRange.end();
- for (AddrRangeConstIter i = defaultRange.begin(); i != a_end; i++) {
- if (*i == addr) {
- DPRINTF(BusAddrRanges, " found addr %#llx on default\n",
- addr);
- return defaultPortID;
- }
+ if (defaultRange.contains(addr)) {
+ DPRINTF(BusAddrRanges, " found addr %#llx on default\n",
+ addr);
+ return defaultPortID;
}
} else if (defaultPortID != InvalidPortID) {
DPRINTF(BusAddrRanges, "Unable to find destination for addr %#llx, "
void
BaseBus::recvRangeChange(PortID master_port_id)
{
- AddrRangeList ranges;
- AddrRangeIter iter;
-
- if (inRecvRangeChange.count(master_port_id))
- return;
- inRecvRangeChange.insert(master_port_id);
-
- DPRINTF(BusAddrRanges, "received RangeChange from device id %d\n",
- master_port_id);
+ DPRINTF(BusAddrRanges, "Received range change from slave port %s\n",
+ masterPorts[master_port_id]->getSlavePort().name());
+
+ // remember that we got a range from this master port and thus the
+ // connected slave module
+ gotAddrRanges[master_port_id] = true;
+
+ // update the global flag
+ if (!gotAllAddrRanges) {
+ // take a logical AND of all the ports and see if we got
+ // ranges from everyone
+ gotAllAddrRanges = true;
+ std::vector<bool>::const_iterator r = gotAddrRanges.begin();
+ while (gotAllAddrRanges && r != gotAddrRanges.end()) {
+ gotAllAddrRanges &= *r++;
+ }
+ if (gotAllAddrRanges)
+ DPRINTF(BusAddrRanges, "Got address ranges from all slaves\n");
+ }
- clearPortCache();
+ // note that we could get the range from the default port at any
+ // point in time, and we cannot assume that the default range is
+ // set before the other ones are, so we do additional checks once
+ // all ranges are provided
if (master_port_id == defaultPortID) {
- defaultRange.clear();
- // Only try to update these ranges if the user set a default responder.
+ // only update if we are indeed checking ranges for the
+ // default port since the port might not have a valid range
+ // otherwise
if (useDefaultRange) {
- // get the address ranges of the connected slave port
- AddrRangeList ranges =
- masterPorts[master_port_id]->getAddrRanges();
- for(iter = ranges.begin(); iter != ranges.end(); iter++) {
- defaultRange.push_back(*iter);
- DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for default range\n",
- iter->start, iter->end);
- }
- }
- } else {
+ AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges();
- assert(master_port_id < masterPorts.size() && master_port_id >= 0);
- MasterPort *port = masterPorts[master_port_id];
+ if (ranges.size() != 1)
+ fatal("Bus %s may only have a single default range",
+ name());
- // Clean out any previously existent ids
- for (PortMapIter portIter = portMap.begin();
- portIter != portMap.end(); ) {
- if (portIter->second == master_port_id)
- portMap.erase(portIter++);
- else
- portIter++;
+ defaultRange = ranges.front();
+ }
+ } else {
+ // the ports are allowed to update their address ranges
+ // dynamically, so remove any existing entries
+ if (gotAddrRanges[master_port_id]) {
+ for (PortMapIter p = portMap.begin(); p != portMap.end(); ) {
+ if (p->second == master_port_id)
+ // erasing invalidates the iterator, so advance it
+ // before the deletion takes place
+ portMap.erase(p++);
+ else
+ p++;
+ }
}
- // get the address ranges of the connected slave port
- ranges = port->getAddrRanges();
+ AddrRangeList ranges = masterPorts[master_port_id]->getAddrRanges();
- for (iter = ranges.begin(); iter != ranges.end(); iter++) {
- DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for id %d\n",
- iter->start, iter->end, master_port_id);
- if (portMap.insert(*iter, master_port_id) == portMap.end()) {
- PortID conflict_id = portMap.find(*iter)->second;
+ for (AddrRangeConstIter r = ranges.begin(); r != ranges.end(); ++r) {
+ DPRINTF(BusAddrRanges, "Adding range %s for id %d\n",
+ r->to_string(), master_port_id);
+ if (portMap.insert(*r, master_port_id) == portMap.end()) {
+ PortID conflict_id = portMap.find(*r)->second;
fatal("%s has two ports with same range:\n\t%s\n\t%s\n",
name(),
masterPorts[master_port_id]->getSlavePort().name(),
}
}
}
- DPRINTF(BusAddrRanges, "port list has %d entries\n", portMap.size());
- // tell all our neighbouring master ports that our address range
- // has changed
- for (SlavePortConstIter p = slavePorts.begin(); p != slavePorts.end();
- ++p)
- (*p)->sendRangeChange();
+ // if we have received ranges from all our neighbouring slave
+ // modules, go ahead and tell our connected master modules in
+ // turn, this effectively assumes a tree structure of the system
+ if (gotAllAddrRanges) {
+ // also check that no range partially overlaps with the
+ // default range, this has to be done after all ranges are set
+ // as there are no guarantees for when the default range is
+ // update with respect to the other ones
+ if (useDefaultRange) {
+ for (PortID port_id = 0; port_id < masterPorts.size(); ++port_id) {
+ if (port_id == defaultPortID) {
+ if (!gotAddrRanges[port_id])
+ fatal("Bus %s uses default range, but none provided",
+ name());
+ } else {
+ AddrRangeList ranges =
+ masterPorts[port_id]->getAddrRanges();
+
+ for (AddrRangeConstIter r = ranges.begin();
+ r != ranges.end(); ++r) {
+ // see if the new range is partially
+ // overlapping the default range
+ if (r->intersects(defaultRange) &&
+ !r->isSubset(defaultRange))
+ fatal("Range %s intersects the " \
+ "default range of %s but is not a " \
+ "subset\n", r->to_string(), name());
+ }
+ }
+ }
+ }
+
+ // tell all our neighbouring master ports that our address
+ // ranges have changed
+ for (SlavePortConstIter s = slavePorts.begin(); s != slavePorts.end();
+ ++s)
+ (*s)->sendRangeChange();
+ }
- inRecvRangeChange.erase(master_port_id);
+ clearPortCache();
}
AddrRangeList
BaseBus::getAddrRanges() const
{
- AddrRangeList ranges;
+ // we should never be asked without first having sent a range
+ // change, and the latter is only done once we have all the ranges
+ // of the connected devices
+ assert(gotAllAddrRanges);
- DPRINTF(BusAddrRanges, "received address range request, returning:\n");
+ // at the moment, this never happens, as there are no cycles in
+ // the range queries and no devices on the master side of a bus
+ // (CPU, cache, bridge etc) actually care about the ranges of the
+ // ports they are connected to
- for (AddrRangeConstIter dflt_iter = defaultRange.begin();
- dflt_iter != defaultRange.end(); dflt_iter++) {
- ranges.push_back(*dflt_iter);
- DPRINTF(BusAddrRanges, " -- Dflt: %#llx : %#llx\n",dflt_iter->start,
- dflt_iter->end);
+ DPRINTF(BusAddrRanges, "Received address range request, returning:\n");
+
+ // start out with the default range
+ AddrRangeList ranges;
+ if (useDefaultRange) {
+ ranges.push_back(defaultRange);
+ DPRINTF(BusAddrRanges, " -- Default %s\n", defaultRange.to_string());
}
- for (PortMapConstIter portIter = portMap.begin();
- portIter != portMap.end(); portIter++) {
- bool subset = false;
- for (AddrRangeConstIter dflt_iter = defaultRange.begin();
- dflt_iter != defaultRange.end(); dflt_iter++) {
- if ((portIter->first.start < dflt_iter->start &&
- portIter->first.end >= dflt_iter->start) ||
- (portIter->first.start < dflt_iter->end &&
- portIter->first.end >= dflt_iter->end))
- fatal("Devices can not set ranges that itersect the default set\
- but are not a subset of the default set.\n");
- if (portIter->first.start >= dflt_iter->start &&
- portIter->first.end <= dflt_iter->end) {
- subset = true;
- DPRINTF(BusAddrRanges, " -- %#llx : %#llx is a SUBSET\n",
- portIter->first.start, portIter->first.end);
- }
- }
- if (!subset) {
- ranges.push_back(portIter->first);
- DPRINTF(BusAddrRanges, " -- %#llx : %#llx\n",
- portIter->first.start, portIter->first.end);
+
+ // add any range that is not a subset of the default range
+ for (PortMapConstIter p = portMap.begin(); p != portMap.end(); ++p) {
+ if (useDefaultRange && p->first.isSubset(defaultRange)) {
+ DPRINTF(BusAddrRanges, " -- %s is a subset of default\n",
+ p->first.to_string());
+ } else {
+ ranges.push_back(p->first);
+ DPRINTF(BusAddrRanges, " -- %s\n", p->first.to_string());
}
}
}
unsigned
-BaseBus::findBlockSize()
+BaseBus::deviceBlockSize() const
{
- if (cachedBlockSizeValid)
- return cachedBlockSize;
-
- unsigned max_bs = 0;
-
- for (MasterPortConstIter m = masterPorts.begin(); m != masterPorts.end();
- ++m) {
- unsigned tmp_bs = (*m)->peerBlockSize();
- if (tmp_bs > max_bs)
- max_bs = tmp_bs;
- }
-
- for (SlavePortConstIter s = slavePorts.begin(); s != slavePorts.end();
- ++s) {
- unsigned tmp_bs = (*s)->peerBlockSize();
- if (tmp_bs > max_bs)
- max_bs = tmp_bs;
- }
- if (max_bs == 0)
- max_bs = defaultBlockSize;
-
- if (max_bs != 64)
- warn_once("Blocksize found to not be 64... hmm... probably not.\n");
- cachedBlockSize = max_bs;
- cachedBlockSizeValid = true;
- return max_bs;
+ return blockSize;
}
-
+template <typename PortClass>
unsigned int
-BaseBus::Layer::drain(Event * de)
+BaseBus::Layer<PortClass>::drain(DrainManager *dm)
{
//We should check that we're not "doing" anything, and that noone is
//waiting. We might be idle but have someone waiting if the device we
//contacted for a retry didn't actually retry.
if (!retryList.empty() || state != IDLE) {
- drainEvent = de;
+ DPRINTF(Drain, "Bus not drained\n");
+ drainManager = dm;
return 1;
}
return 0;
}
+
+/**
+ * Bus layer template instantiations. Could be removed with _impl.hh
+ * file, but since there are only two given options (MasterPort and
+ * SlavePort) it seems a bit excessive at this point.
+ */
+template class BaseBus::Layer<SlavePort>;
+template class BaseBus::Layer<MasterPort>;