xbar.cc revision 9278
16019Shines@cs.fsu.edu/*
27399SAli.Saidi@ARM.com * Copyright (c) 2011-2012 ARM Limited
37399SAli.Saidi@ARM.com * All rights reserved
47399SAli.Saidi@ARM.com *
57399SAli.Saidi@ARM.com * The license below extends only to copyright in the software and shall
67399SAli.Saidi@ARM.com * not be construed as granting a license to any other intellectual
77399SAli.Saidi@ARM.com * property including but not limited to intellectual property relating
87399SAli.Saidi@ARM.com * to a hardware implementation of the functionality of the software
97399SAli.Saidi@ARM.com * licensed hereunder.  You may use the software subject to the license
107399SAli.Saidi@ARM.com * terms below provided that you ensure that this notice is replicated
117399SAli.Saidi@ARM.com * unmodified and in its entirety in all distributions of the software,
127399SAli.Saidi@ARM.com * modified or unmodified, in source code or in binary form.
137399SAli.Saidi@ARM.com *
146019Shines@cs.fsu.edu * Copyright (c) 2006 The Regents of The University of Michigan
156019Shines@cs.fsu.edu * All rights reserved.
166019Shines@cs.fsu.edu *
176019Shines@cs.fsu.edu * Redistribution and use in source and binary forms, with or without
186019Shines@cs.fsu.edu * modification, are permitted provided that the following conditions are
196019Shines@cs.fsu.edu * met: redistributions of source code must retain the above copyright
206019Shines@cs.fsu.edu * notice, this list of conditions and the following disclaimer;
216019Shines@cs.fsu.edu * redistributions in binary form must reproduce the above copyright
226019Shines@cs.fsu.edu * notice, this list of conditions and the following disclaimer in the
236019Shines@cs.fsu.edu * documentation and/or other materials provided with the distribution;
246019Shines@cs.fsu.edu * neither the name of the copyright holders nor the names of its
256019Shines@cs.fsu.edu * contributors may be used to endorse or promote products derived from
266019Shines@cs.fsu.edu * this software without specific prior written permission.
276019Shines@cs.fsu.edu *
286019Shines@cs.fsu.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
296019Shines@cs.fsu.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
306019Shines@cs.fsu.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
316019Shines@cs.fsu.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
326019Shines@cs.fsu.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
336019Shines@cs.fsu.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
346019Shines@cs.fsu.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
356019Shines@cs.fsu.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
366019Shines@cs.fsu.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
376019Shines@cs.fsu.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
386019Shines@cs.fsu.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
396019Shines@cs.fsu.edu *
407399SAli.Saidi@ARM.com * Authors: Ali Saidi
416019Shines@cs.fsu.edu *          Andreas Hansson
426019Shines@cs.fsu.edu *          William Wang
436019Shines@cs.fsu.edu */
446019Shines@cs.fsu.edu
456019Shines@cs.fsu.edu/**
466019Shines@cs.fsu.edu * @file
476019Shines@cs.fsu.edu * Definition of a bus object.
486019Shines@cs.fsu.edu */
496019Shines@cs.fsu.edu
506019Shines@cs.fsu.edu#include "base/misc.hh"
516019Shines@cs.fsu.edu#include "base/trace.hh"
526019Shines@cs.fsu.edu#include "debug/Bus.hh"
536019Shines@cs.fsu.edu#include "debug/BusAddrRanges.hh"
546116Snate@binkert.org#include "debug/Drain.hh"
556019Shines@cs.fsu.edu#include "mem/bus.hh"
566019Shines@cs.fsu.edu
576019Shines@cs.fsu.eduBaseBus::BaseBus(const BaseBusParams *p)
586019Shines@cs.fsu.edu    : MemObject(p),
596019Shines@cs.fsu.edu      headerCycles(p->header_cycles), width(p->width),
606019Shines@cs.fsu.edu      defaultPortID(InvalidPortID),
616019Shines@cs.fsu.edu      useDefaultRange(p->use_default_range),
627404SAli.Saidi@ARM.com      blockSize(p->block_size)
637404SAli.Saidi@ARM.com{}
646019Shines@cs.fsu.edu
656019Shines@cs.fsu.eduBaseBus::~BaseBus()
667294Sgblack@eecs.umich.edu{
677294Sgblack@eecs.umich.edu    for (MasterPortIter m = masterPorts.begin(); m != masterPorts.end();
687639Sgblack@eecs.umich.edu         ++m) {
697294Sgblack@eecs.umich.edu        delete *m;
707294Sgblack@eecs.umich.edu    }
717294Sgblack@eecs.umich.edu
727294Sgblack@eecs.umich.edu    for (SlavePortIter s = slavePorts.begin(); s != slavePorts.end();
737294Sgblack@eecs.umich.edu         ++s) {
747639Sgblack@eecs.umich.edu        delete *s;
757639Sgblack@eecs.umich.edu    }
767294Sgblack@eecs.umich.edu}
777639Sgblack@eecs.umich.edu
787404SAli.Saidi@ARM.comvoid
797639Sgblack@eecs.umich.eduBaseBus::init()
807294Sgblack@eecs.umich.edu{
817294Sgblack@eecs.umich.edu    // determine the maximum peer block size, look at both the
827294Sgblack@eecs.umich.edu    // connected master and slave modules
837639Sgblack@eecs.umich.edu    uint32_t peer_block_size = 0;
847294Sgblack@eecs.umich.edu
856019Shines@cs.fsu.edu    for (MasterPortConstIter m = masterPorts.begin(); m != masterPorts.end();
866019Shines@cs.fsu.edu         ++m) {
876019Shines@cs.fsu.edu        peer_block_size = std::max((*m)->peerBlockSize(), peer_block_size);
886019Shines@cs.fsu.edu    }
897404SAli.Saidi@ARM.com
906019Shines@cs.fsu.edu    for (SlavePortConstIter s = slavePorts.begin(); s != slavePorts.end();
916019Shines@cs.fsu.edu         ++s) {
927406SAli.Saidi@ARM.com        peer_block_size = std::max((*s)->peerBlockSize(), peer_block_size);
937436Sdam.sunwoo@arm.com    }
947436Sdam.sunwoo@arm.com
957406SAli.Saidi@ARM.com    // if the peers do not have a block size, use the default value
967404SAli.Saidi@ARM.com    // set through the bus parameters
977406SAli.Saidi@ARM.com    if (peer_block_size != 0)
986019Shines@cs.fsu.edu        blockSize = peer_block_size;
996019Shines@cs.fsu.edu
1007404SAli.Saidi@ARM.com    // check if the block size is a value known to work
1016019Shines@cs.fsu.edu    if (blockSize != 16 || blockSize != 32 || blockSize != 64 ||
1027399SAli.Saidi@ARM.com        blockSize != 128)
1036020Sgblack@eecs.umich.edu        warn_once("Block size is neither 16, 32, 64 or 128 bytes.\n");
1046020Sgblack@eecs.umich.edu}
1056020Sgblack@eecs.umich.edu
1066020Sgblack@eecs.umich.eduMasterPort &
1076020Sgblack@eecs.umich.eduBaseBus::getMasterPort(const std::string &if_name, int idx)
1086020Sgblack@eecs.umich.edu{
1096020Sgblack@eecs.umich.edu    if (if_name == "master" && idx < masterPorts.size()) {
1106020Sgblack@eecs.umich.edu        // the master port index translates directly to the vector position
1116019Shines@cs.fsu.edu        return *masterPorts[idx];
1126019Shines@cs.fsu.edu    } else  if (if_name == "default") {
1136019Shines@cs.fsu.edu        return *masterPorts[defaultPortID];
1146019Shines@cs.fsu.edu    } else {
1157404SAli.Saidi@ARM.com        return MemObject::getMasterPort(if_name, idx);
1166019Shines@cs.fsu.edu    }
1176019Shines@cs.fsu.edu}
1186019Shines@cs.fsu.edu
1196019Shines@cs.fsu.eduSlavePort &
1206019Shines@cs.fsu.eduBaseBus::getSlavePort(const std::string &if_name, int idx)
1216019Shines@cs.fsu.edu{
1226019Shines@cs.fsu.edu    if (if_name == "slave" && idx < slavePorts.size()) {
1237404SAli.Saidi@ARM.com        // the slave port index translates directly to the vector position
1247404SAli.Saidi@ARM.com        return *slavePorts[idx];
1257404SAli.Saidi@ARM.com    } else {
1266019Shines@cs.fsu.edu        return MemObject::getSlavePort(if_name, idx);
1277404SAli.Saidi@ARM.com    }
1287404SAli.Saidi@ARM.com}
1297404SAli.Saidi@ARM.com
1307404SAli.Saidi@ARM.comTick
1317404SAli.Saidi@ARM.comBaseBus::calcPacketTiming(PacketPtr pkt)
1327404SAli.Saidi@ARM.com{
1337404SAli.Saidi@ARM.com    // determine the current time rounded to the closest following
1347404SAli.Saidi@ARM.com    // clock edge
1357404SAli.Saidi@ARM.com    Tick now = nextCycle();
1367404SAli.Saidi@ARM.com
1377404SAli.Saidi@ARM.com    Tick headerTime = now + headerCycles * clock;
1387404SAli.Saidi@ARM.com
1397404SAli.Saidi@ARM.com    // The packet will be sent. Figure out how long it occupies the bus, and
1407404SAli.Saidi@ARM.com    // how much of that time is for the first "word", aka bus width.
1417404SAli.Saidi@ARM.com    int numCycles = 0;
1427404SAli.Saidi@ARM.com    if (pkt->hasData()) {
1437404SAli.Saidi@ARM.com        // If a packet has data, it needs ceil(size/width) cycles to send it
1447404SAli.Saidi@ARM.com        int dataSize = pkt->getSize();
1457406SAli.Saidi@ARM.com        numCycles += dataSize/width;
1467406SAli.Saidi@ARM.com        if (dataSize % width)
1477404SAli.Saidi@ARM.com            numCycles++;
1487404SAli.Saidi@ARM.com    }
1497404SAli.Saidi@ARM.com
1506019Shines@cs.fsu.edu    // The first word will be delivered after the current tick, the delivery
1516019Shines@cs.fsu.edu    // of the address if any, and one bus cycle to deliver the data
1527404SAli.Saidi@ARM.com    pkt->firstWordTime = headerTime + clock;
1536019Shines@cs.fsu.edu
1546019Shines@cs.fsu.edu    pkt->finishTime = headerTime + numCycles * clock;
1556019Shines@cs.fsu.edu
1566019Shines@cs.fsu.edu    return headerTime;
1577436Sdam.sunwoo@arm.com}
1587436Sdam.sunwoo@arm.com
1597436Sdam.sunwoo@arm.comtemplate <typename PortClass>
1607436Sdam.sunwoo@arm.comBaseBus::Layer<PortClass>::Layer(BaseBus& _bus, const std::string& _name,
1617436Sdam.sunwoo@arm.com                                 Tick _clock) :
1627436Sdam.sunwoo@arm.com    bus(_bus), _name(_name), state(IDLE), clock(_clock), drainEvent(NULL),
1637436Sdam.sunwoo@arm.com    releaseEvent(this)
1647436Sdam.sunwoo@arm.com{
1657436Sdam.sunwoo@arm.com}
1667436Sdam.sunwoo@arm.com
1677436Sdam.sunwoo@arm.comtemplate <typename PortClass>
1687436Sdam.sunwoo@arm.comvoid BaseBus::Layer<PortClass>::occupyLayer(Tick until)
1697436Sdam.sunwoo@arm.com{
1707404SAli.Saidi@ARM.com    // ensure the state is busy or in retry and never idle at this
1717404SAli.Saidi@ARM.com    // point, as the bus should transition from idle as soon as it has
1727404SAli.Saidi@ARM.com    // decided to forward the packet to prevent any follow-on calls to
1737404SAli.Saidi@ARM.com    // sendTiming seeing an unoccupied bus
1747404SAli.Saidi@ARM.com    assert(state != IDLE);
1757404SAli.Saidi@ARM.com
1767404SAli.Saidi@ARM.com    // note that we do not change the bus state here, if we are going
1776116Snate@binkert.org    // from idle to busy it is handled by tryTiming, and if we
1787404SAli.Saidi@ARM.com    // are in retry we should remain in retry such that
1796116Snate@binkert.org    // succeededTiming still sees the accurate state
1806116Snate@binkert.org
1816019Shines@cs.fsu.edu    // until should never be 0 as express snoops never occupy the bus
1826019Shines@cs.fsu.edu    assert(until != 0);
1836019Shines@cs.fsu.edu    bus.schedule(releaseEvent, until);
1846019Shines@cs.fsu.edu
1856019Shines@cs.fsu.edu    DPRINTF(BaseBus, "The bus is now busy from tick %d to %d\n",
1866019Shines@cs.fsu.edu            curTick(), until);
1876019Shines@cs.fsu.edu}
1886116Snate@binkert.org
1896019Shines@cs.fsu.edutemplate <typename PortClass>
1906019Shines@cs.fsu.edubool
191BaseBus::Layer<PortClass>::tryTiming(PortClass* port)
192{
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);
198        return false;
199    }
200
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
204    if (state == IDLE)
205        state = BUSY;
206
207    return true;
208}
209
210template <typename PortClass>
211void
212BaseBus::Layer<PortClass>::succeededTiming(Tick busy_time)
213{
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();
219        state = BUSY;
220    }
221
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);
225
226    // occupy the bus accordingly
227    occupyLayer(busy_time);
228}
229
230template <typename PortClass>
231void
232BaseBus::Layer<PortClass>::failedTiming(PortClass* port, Tick busy_time)
233{
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);
239    }
240
241    // even if we retried the current one and did not succeed,
242    // we are no longer retrying but instead busy
243    state = BUSY;
244
245    // occupy the bus accordingly
246    occupyLayer(busy_time);
247}
248
249template <typename PortClass>
250void
251BaseBus::Layer<PortClass>::releaseLayer()
252{
253    // releasing the bus means we should now be idle
254    assert(state == BUSY);
255    assert(!releaseEvent.scheduled());
256
257    // update the state
258    state = IDLE;
259
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
266        retryWaiting();
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.
272        drainEvent = NULL;
273    }
274}
275
276template <typename PortClass>
277void
278BaseBus::Layer<PortClass>::retryWaiting()
279{
280    // this should never be called with an empty retry list
281    assert(!retryList.empty());
282
283    // we always go to retrying from idle
284    assert(state == IDLE);
285
286    // update the state which is shared for request, response and
287    // snoop responses
288    state = RETRY;
289
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();
294
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();
299
300        //Burn a cycle for the missed grant.
301
302        // update the state which is shared for request, response and
303        // snoop responses
304        state = BUSY;
305
306        // determine the current time rounded to the closest following
307        // clock edge
308        Tick now = bus.nextCycle();
309
310        occupyLayer(now + clock);
311    }
312}
313
314template <typename PortClass>
315void
316BaseBus::Layer<PortClass>::recvRetry()
317{
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())
325        return;
326
327    // if the bus layer is idle
328    if (state == 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
331        retryWaiting();
332    }
333}
334
335PortID
336BaseBus::findPort(Addr addr)
337{
338    /* An interval tree would be a better way to do this. --ali. */
339    PortID dest_id = checkPortCache(addr);
340    if (dest_id != InvalidPortID)
341        return dest_id;
342
343    // Check normal port ranges
344    PortMapConstIter i = portMap.find(RangeSize(addr,1));
345    if (i != portMap.end()) {
346        dest_id = i->second;
347        updatePortCache(dest_id, i->first.start, i->first.end);
348        return dest_id;
349    }
350
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++) {
355            if (*i == addr) {
356                DPRINTF(BusAddrRanges, "  found addr %#llx on default\n",
357                        addr);
358                return defaultPortID;
359            }
360        }
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;
365    }
366
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,
370          name());
371}
372
373/** Function called by the port when the bus is receiving a range change.*/
374void
375BaseBus::recvRangeChange(PortID master_port_id)
376{
377    AddrRangeIter iter;
378
379    if (inRecvRangeChange.count(master_port_id))
380        return;
381    inRecvRangeChange.insert(master_port_id);
382
383    DPRINTF(BusAddrRanges, "received RangeChange from device id %d\n",
384            master_port_id);
385
386    clearPortCache();
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);
398            }
399        }
400    } else {
401
402        assert(master_port_id < masterPorts.size() && master_port_id >= 0);
403        MasterPort *port = masterPorts[master_port_id];
404
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++);
410            else
411                portIter++;
412        }
413
414        // get the address ranges of the connected slave port
415        AddrRangeList ranges = port->getAddrRanges();
416
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",
423                      name(),
424                      masterPorts[master_port_id]->getSlavePort().name(),
425                      masterPorts[conflict_id]->getSlavePort().name());
426            }
427        }
428    }
429    DPRINTF(BusAddrRanges, "port list has %d entries\n", portMap.size());
430
431    // tell all our neighbouring master ports that our address range
432    // has changed
433    for (SlavePortConstIter p = slavePorts.begin(); p != slavePorts.end();
434         ++p)
435        (*p)->sendRangeChange();
436
437    inRecvRangeChange.erase(master_port_id);
438}
439
440AddrRangeList
441BaseBus::getAddrRanges() const
442{
443    AddrRangeList ranges;
444
445    DPRINTF(BusAddrRanges, "received address range request, returning:\n");
446
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,
451                dflt_iter->end);
452    }
453    for (PortMapConstIter portIter = portMap.begin();
454         portIter != portMap.end(); portIter++) {
455        bool subset = false;
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) {
466                subset = true;
467                DPRINTF(BusAddrRanges, "  -- %#llx : %#llx is a SUBSET\n",
468                    portIter->first.start, portIter->first.end);
469            }
470        }
471        if (!subset) {
472            ranges.push_back(portIter->first);
473            DPRINTF(BusAddrRanges, "  -- %#llx : %#llx\n",
474                    portIter->first.start, portIter->first.end);
475        }
476    }
477
478    return ranges;
479}
480
481unsigned
482BaseBus::deviceBlockSize() const
483{
484    return blockSize;
485}
486
487template <typename PortClass>
488unsigned int
489BaseBus::Layer<PortClass>::drain(Event * de)
490{
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");
496        drainEvent = de;
497        return 1;
498    }
499    return 0;
500}
501
502/**
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.
506 */
507template class BaseBus::Layer<SlavePort>;
508template class BaseBus::Layer<MasterPort>;
509