coherent_xbar.cc revision 9714
16691Stjones1@inf.ed.ac.uk/*
26691Stjones1@inf.ed.ac.uk * Copyright (c) 2011-2013 ARM Limited
36691Stjones1@inf.ed.ac.uk * All rights reserved
46691Stjones1@inf.ed.ac.uk *
56691Stjones1@inf.ed.ac.uk * The license below extends only to copyright in the software and shall
66691Stjones1@inf.ed.ac.uk * not be construed as granting a license to any other intellectual
76691Stjones1@inf.ed.ac.uk * property including but not limited to intellectual property relating
86691Stjones1@inf.ed.ac.uk * to a hardware implementation of the functionality of the software
96691Stjones1@inf.ed.ac.uk * licensed hereunder.  You may use the software subject to the license
106691Stjones1@inf.ed.ac.uk * terms below provided that you ensure that this notice is replicated
116691Stjones1@inf.ed.ac.uk * unmodified and in its entirety in all distributions of the software,
126691Stjones1@inf.ed.ac.uk * modified or unmodified, in source code or in binary form.
136691Stjones1@inf.ed.ac.uk *
146691Stjones1@inf.ed.ac.uk * Copyright (c) 2006 The Regents of The University of Michigan
156691Stjones1@inf.ed.ac.uk * All rights reserved.
166691Stjones1@inf.ed.ac.uk *
176691Stjones1@inf.ed.ac.uk * Redistribution and use in source and binary forms, with or without
186691Stjones1@inf.ed.ac.uk * modification, are permitted provided that the following conditions are
196691Stjones1@inf.ed.ac.uk * met: redistributions of source code must retain the above copyright
206691Stjones1@inf.ed.ac.uk * notice, this list of conditions and the following disclaimer;
216691Stjones1@inf.ed.ac.uk * redistributions in binary form must reproduce the above copyright
226691Stjones1@inf.ed.ac.uk * notice, this list of conditions and the following disclaimer in the
236691Stjones1@inf.ed.ac.uk * documentation and/or other materials provided with the distribution;
246691Stjones1@inf.ed.ac.uk * neither the name of the copyright holders nor the names of its
256691Stjones1@inf.ed.ac.uk * contributors may be used to endorse or promote products derived from
266691Stjones1@inf.ed.ac.uk * this software without specific prior written permission.
276691Stjones1@inf.ed.ac.uk *
286691Stjones1@inf.ed.ac.uk * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
296691Stjones1@inf.ed.ac.uk * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
306691Stjones1@inf.ed.ac.uk * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
316691Stjones1@inf.ed.ac.uk * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
326691Stjones1@inf.ed.ac.uk * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
336691Stjones1@inf.ed.ac.uk * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
346691Stjones1@inf.ed.ac.uk * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
356691Stjones1@inf.ed.ac.uk * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
366691Stjones1@inf.ed.ac.uk * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
376691Stjones1@inf.ed.ac.uk * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
386691Stjones1@inf.ed.ac.uk * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
396691Stjones1@inf.ed.ac.uk *
406691Stjones1@inf.ed.ac.uk * Authors: Ali Saidi
416691Stjones1@inf.ed.ac.uk *          Andreas Hansson
426691Stjones1@inf.ed.ac.uk *          William Wang
436691Stjones1@inf.ed.ac.uk */
446691Stjones1@inf.ed.ac.uk
456691Stjones1@inf.ed.ac.uk/**
466691Stjones1@inf.ed.ac.uk * @file
476691Stjones1@inf.ed.ac.uk * Definition of a bus object.
486691Stjones1@inf.ed.ac.uk */
497878Sgblack@eecs.umich.edu
506691Stjones1@inf.ed.ac.uk#include "base/misc.hh"
516691Stjones1@inf.ed.ac.uk#include "base/trace.hh"
526691Stjones1@inf.ed.ac.uk#include "debug/BusAddrRanges.hh"
536691Stjones1@inf.ed.ac.uk#include "debug/CoherentBus.hh"
546691Stjones1@inf.ed.ac.uk#include "mem/coherent_bus.hh"
556691Stjones1@inf.ed.ac.uk#include "sim/system.hh"
566691Stjones1@inf.ed.ac.uk
576691Stjones1@inf.ed.ac.ukCoherentBus::CoherentBus(const CoherentBusParams *p)
586691Stjones1@inf.ed.ac.uk    : BaseBus(p),
596691Stjones1@inf.ed.ac.uk      reqLayer(*this, ".reqLayer", p->port_master_connection_count +
606691Stjones1@inf.ed.ac.uk               p->port_default_connection_count),
616691Stjones1@inf.ed.ac.uk      respLayer(*this, ".respLayer", p->port_slave_connection_count),
626691Stjones1@inf.ed.ac.uk      snoopRespLayer(*this, ".snoopRespLayer",
636691Stjones1@inf.ed.ac.uk                     p->port_master_connection_count +
646691Stjones1@inf.ed.ac.uk                     p->port_default_connection_count),
656691Stjones1@inf.ed.ac.uk      system(p->system)
666691Stjones1@inf.ed.ac.uk{
676691Stjones1@inf.ed.ac.uk    // create the ports based on the size of the master and slave
686691Stjones1@inf.ed.ac.uk    // vector ports, and the presence of the default port, the ports
696691Stjones1@inf.ed.ac.uk    // are enumerated starting from zero
706691Stjones1@inf.ed.ac.uk    for (int i = 0; i < p->port_master_connection_count; ++i) {
716691Stjones1@inf.ed.ac.uk        std::string portName = csprintf("%s.master[%d]", name(), i);
726691Stjones1@inf.ed.ac.uk        MasterPort* bp = new CoherentBusMasterPort(portName, *this, i);
736691Stjones1@inf.ed.ac.uk        masterPorts.push_back(bp);
746691Stjones1@inf.ed.ac.uk    }
756691Stjones1@inf.ed.ac.uk
766691Stjones1@inf.ed.ac.uk    // see if we have a default slave device connected and if so add
776691Stjones1@inf.ed.ac.uk    // our corresponding master port
786691Stjones1@inf.ed.ac.uk    if (p->port_default_connection_count) {
796691Stjones1@inf.ed.ac.uk        defaultPortID = masterPorts.size();
806691Stjones1@inf.ed.ac.uk        std::string portName = name() + ".default";
816691Stjones1@inf.ed.ac.uk        MasterPort* bp = new CoherentBusMasterPort(portName, *this,
826691Stjones1@inf.ed.ac.uk                                                   defaultPortID);
836691Stjones1@inf.ed.ac.uk        masterPorts.push_back(bp);
846691Stjones1@inf.ed.ac.uk    }
856691Stjones1@inf.ed.ac.uk
866691Stjones1@inf.ed.ac.uk    // create the slave ports, once again starting at zero
876691Stjones1@inf.ed.ac.uk    for (int i = 0; i < p->port_slave_connection_count; ++i) {
886691Stjones1@inf.ed.ac.uk        std::string portName = csprintf("%s.slave[%d]", name(), i);
896691Stjones1@inf.ed.ac.uk        SlavePort* bp = new CoherentBusSlavePort(portName, *this, i);
906691Stjones1@inf.ed.ac.uk        slavePorts.push_back(bp);
916691Stjones1@inf.ed.ac.uk    }
926691Stjones1@inf.ed.ac.uk
936691Stjones1@inf.ed.ac.uk    clearPortCache();
946691Stjones1@inf.ed.ac.uk}
956691Stjones1@inf.ed.ac.uk
966691Stjones1@inf.ed.ac.ukvoid
976691Stjones1@inf.ed.ac.ukCoherentBus::init()
986691Stjones1@inf.ed.ac.uk{
996691Stjones1@inf.ed.ac.uk    // the base class is responsible for determining the block size
1006691Stjones1@inf.ed.ac.uk    BaseBus::init();
1016691Stjones1@inf.ed.ac.uk
1026691Stjones1@inf.ed.ac.uk    // iterate over our slave ports and determine which of our
1036691Stjones1@inf.ed.ac.uk    // neighbouring master ports are snooping and add them as snoopers
1046691Stjones1@inf.ed.ac.uk    for (SlavePortConstIter p = slavePorts.begin(); p != slavePorts.end();
1056691Stjones1@inf.ed.ac.uk         ++p) {
1066691Stjones1@inf.ed.ac.uk        // check if the connected master port is snooping
1076691Stjones1@inf.ed.ac.uk        if ((*p)->isSnooping()) {
1086691Stjones1@inf.ed.ac.uk            DPRINTF(BusAddrRanges, "Adding snooping master %s\n",
1096691Stjones1@inf.ed.ac.uk                    (*p)->getMasterPort().name());
1106691Stjones1@inf.ed.ac.uk            snoopPorts.push_back(*p);
1116691Stjones1@inf.ed.ac.uk        }
1126691Stjones1@inf.ed.ac.uk    }
1136691Stjones1@inf.ed.ac.uk
1146691Stjones1@inf.ed.ac.uk    if (snoopPorts.empty())
1156691Stjones1@inf.ed.ac.uk        warn("CoherentBus %s has no snooping ports attached!\n", name());
1166691Stjones1@inf.ed.ac.uk}
1176691Stjones1@inf.ed.ac.uk
1186691Stjones1@inf.ed.ac.ukbool
1196691Stjones1@inf.ed.ac.ukCoherentBus::recvTimingReq(PacketPtr pkt, PortID slave_port_id)
1206691Stjones1@inf.ed.ac.uk{
1216691Stjones1@inf.ed.ac.uk    // determine the source port based on the id
1226691Stjones1@inf.ed.ac.uk    SlavePort *src_port = slavePorts[slave_port_id];
1236691Stjones1@inf.ed.ac.uk
1246691Stjones1@inf.ed.ac.uk    // remember if the packet is an express snoop
1256691Stjones1@inf.ed.ac.uk    bool is_express_snoop = pkt->isExpressSnoop();
1266691Stjones1@inf.ed.ac.uk
1276691Stjones1@inf.ed.ac.uk    // determine the destination based on the address
1286691Stjones1@inf.ed.ac.uk    PortID master_port_id = findPort(pkt->getAddr());
1296691Stjones1@inf.ed.ac.uk
1306691Stjones1@inf.ed.ac.uk    // test if the bus should be considered occupied for the current
1316691Stjones1@inf.ed.ac.uk    // port, and exclude express snoops from the check
1326691Stjones1@inf.ed.ac.uk    if (!is_express_snoop && !reqLayer.tryTiming(src_port, master_port_id)) {
1336691Stjones1@inf.ed.ac.uk        DPRINTF(CoherentBus, "recvTimingReq: src %s %s 0x%x BUS BUSY\n",
1346691Stjones1@inf.ed.ac.uk                src_port->name(), pkt->cmdString(), pkt->getAddr());
1356691Stjones1@inf.ed.ac.uk        return false;
1366691Stjones1@inf.ed.ac.uk    }
1376691Stjones1@inf.ed.ac.uk
1386691Stjones1@inf.ed.ac.uk    DPRINTF(CoherentBus, "recvTimingReq: src %s %s expr %d 0x%x\n",
1396691Stjones1@inf.ed.ac.uk            src_port->name(), pkt->cmdString(), is_express_snoop,
1406691Stjones1@inf.ed.ac.uk            pkt->getAddr());
1416691Stjones1@inf.ed.ac.uk
1426691Stjones1@inf.ed.ac.uk    // store size and command as they might be modified when
1436691Stjones1@inf.ed.ac.uk    // forwarding the packet
1446691Stjones1@inf.ed.ac.uk    unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
1456691Stjones1@inf.ed.ac.uk    unsigned int pkt_cmd = pkt->cmdToIndex();
1466691Stjones1@inf.ed.ac.uk
1476691Stjones1@inf.ed.ac.uk    // set the source port for routing of the response
1486691Stjones1@inf.ed.ac.uk    pkt->setSrc(slave_port_id);
1496691Stjones1@inf.ed.ac.uk
1506691Stjones1@inf.ed.ac.uk    calcPacketTiming(pkt);
1516691Stjones1@inf.ed.ac.uk    Tick packetFinishTime = pkt->busLastWordDelay + curTick();
1526691Stjones1@inf.ed.ac.uk
1536691Stjones1@inf.ed.ac.uk    // uncacheable requests need never be snooped
1546691Stjones1@inf.ed.ac.uk    if (!pkt->req->isUncacheable() && !system->bypassCaches()) {
1556691Stjones1@inf.ed.ac.uk        // the packet is a memory-mapped request and should be
1566691Stjones1@inf.ed.ac.uk        // broadcasted to our snoopers but the source
1576691Stjones1@inf.ed.ac.uk        forwardTiming(pkt, slave_port_id);
1586972Stjones1@inf.ed.ac.uk    }
1596972Stjones1@inf.ed.ac.uk
1606691Stjones1@inf.ed.ac.uk    // remember if we add an outstanding req so we can undo it if
1616691Stjones1@inf.ed.ac.uk    // necessary, if the packet needs a response, we should add it
1626691Stjones1@inf.ed.ac.uk    // as outstanding and express snoops never fail so there is
1636691Stjones1@inf.ed.ac.uk    // not need to worry about them
1646691Stjones1@inf.ed.ac.uk    bool add_outstanding = !is_express_snoop && pkt->needsResponse();
1656691Stjones1@inf.ed.ac.uk
1666691Stjones1@inf.ed.ac.uk    // keep track that we have an outstanding request packet
1676691Stjones1@inf.ed.ac.uk    // matching this request, this is used by the coherency
1686691Stjones1@inf.ed.ac.uk    // mechanism in determining what to do with snoop responses
1696691Stjones1@inf.ed.ac.uk    // (in recvTimingSnoop)
1707811Ssteve.reinhardt@amd.com    if (add_outstanding) {
1716691Stjones1@inf.ed.ac.uk        // we should never have an exsiting request outstanding
1726691Stjones1@inf.ed.ac.uk        assert(outstandingReq.find(pkt->req) == outstandingReq.end());
173        outstandingReq.insert(pkt->req);
174    }
175
176    // since it is a normal request, attempt to send the packet
177    bool success = masterPorts[master_port_id]->sendTimingReq(pkt);
178
179    // if this is an express snoop, we are done at this point
180    if (is_express_snoop) {
181        assert(success);
182        snoopDataThroughBus += pkt_size;
183    } else {
184        // for normal requests, check if successful
185        if (!success)  {
186            // inhibited packets should never be forced to retry
187            assert(!pkt->memInhibitAsserted());
188
189            // if it was added as outstanding and the send failed, then
190            // erase it again
191            if (add_outstanding)
192                outstandingReq.erase(pkt->req);
193
194            // undo the calculation so we can check for 0 again
195            pkt->busFirstWordDelay = pkt->busLastWordDelay = 0;
196
197            DPRINTF(CoherentBus, "recvTimingReq: src %s %s 0x%x RETRY\n",
198                    src_port->name(), pkt->cmdString(), pkt->getAddr());
199
200            // update the bus state and schedule an idle event
201            reqLayer.failedTiming(src_port, master_port_id,
202                                  clockEdge(headerCycles));
203        } else {
204            // update the bus state and schedule an idle event
205            reqLayer.succeededTiming(packetFinishTime);
206            dataThroughBus += pkt_size;
207        }
208    }
209
210    // stats updates only consider packets that were successfully sent
211    if (success) {
212        pktCount[slave_port_id][master_port_id]++;
213        totPktSize[slave_port_id][master_port_id] += pkt_size;
214        transDist[pkt_cmd]++;
215    }
216
217    return success;
218}
219
220bool
221CoherentBus::recvTimingResp(PacketPtr pkt, PortID master_port_id)
222{
223    // determine the source port based on the id
224    MasterPort *src_port = masterPorts[master_port_id];
225
226    // determine the destination based on what is stored in the packet
227    PortID slave_port_id = pkt->getDest();
228
229    // test if the bus should be considered occupied for the current
230    // port
231    if (!respLayer.tryTiming(src_port, slave_port_id)) {
232        DPRINTF(CoherentBus, "recvTimingResp: src %s %s 0x%x BUSY\n",
233                src_port->name(), pkt->cmdString(), pkt->getAddr());
234        return false;
235    }
236
237    DPRINTF(CoherentBus, "recvTimingResp: src %s %s 0x%x\n",
238            src_port->name(), pkt->cmdString(), pkt->getAddr());
239
240    // store size and command as they might be modified when
241    // forwarding the packet
242    unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
243    unsigned int pkt_cmd = pkt->cmdToIndex();
244
245    calcPacketTiming(pkt);
246    Tick packetFinishTime = pkt->busLastWordDelay + curTick();
247
248    // the packet is a normal response to a request that we should
249    // have seen passing through the bus
250    assert(outstandingReq.find(pkt->req) != outstandingReq.end());
251
252    // remove it as outstanding
253    outstandingReq.erase(pkt->req);
254
255    // send the packet through the destination slave port
256    bool success M5_VAR_USED = slavePorts[slave_port_id]->sendTimingResp(pkt);
257
258    // currently it is illegal to block responses... can lead to
259    // deadlock
260    assert(success);
261
262    respLayer.succeededTiming(packetFinishTime);
263
264    // stats updates
265    dataThroughBus += pkt_size;
266    pktCount[slave_port_id][master_port_id]++;
267    totPktSize[slave_port_id][master_port_id] += pkt_size;
268    transDist[pkt_cmd]++;
269
270    return true;
271}
272
273void
274CoherentBus::recvTimingSnoopReq(PacketPtr pkt, PortID master_port_id)
275{
276    DPRINTF(CoherentBus, "recvTimingSnoopReq: src %s %s 0x%x\n",
277            masterPorts[master_port_id]->name(), pkt->cmdString(),
278            pkt->getAddr());
279
280    // update stats here as we know the forwarding will succeed
281    transDist[pkt->cmdToIndex()]++;
282    snoopDataThroughBus += pkt->hasData() ? pkt->getSize() : 0;
283
284    // we should only see express snoops from caches
285    assert(pkt->isExpressSnoop());
286
287    // set the source port for routing of the response
288    pkt->setSrc(master_port_id);
289
290    // forward to all snoopers
291    forwardTiming(pkt, InvalidPortID);
292
293    // a snoop request came from a connected slave device (one of
294    // our master ports), and if it is not coming from the slave
295    // device responsible for the address range something is
296    // wrong, hence there is nothing further to do as the packet
297    // would be going back to where it came from
298    assert(master_port_id == findPort(pkt->getAddr()));
299}
300
301bool
302CoherentBus::recvTimingSnoopResp(PacketPtr pkt, PortID slave_port_id)
303{
304    // determine the source port based on the id
305    SlavePort* src_port = slavePorts[slave_port_id];
306
307    // get the destination from the packet
308    PortID dest_port_id = pkt->getDest();
309
310    // determine if the response is from a snoop request we
311    // created as the result of a normal request (in which case it
312    // should be in the outstandingReq), or if we merely forwarded
313    // someone else's snoop request
314    bool forwardAsSnoop = outstandingReq.find(pkt->req) ==
315        outstandingReq.end();
316
317    // test if the bus should be considered occupied for the current
318    // port, note that the check is bypassed if the response is being
319    // passed on as a normal response since this is occupying the
320    // response layer rather than the snoop response layer
321    if (forwardAsSnoop && !snoopRespLayer.tryTiming(src_port, dest_port_id)) {
322        DPRINTF(CoherentBus, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n",
323                src_port->name(), pkt->cmdString(), pkt->getAddr());
324        return false;
325    }
326
327    DPRINTF(CoherentBus, "recvTimingSnoopResp: src %s %s 0x%x\n",
328            src_port->name(), pkt->cmdString(), pkt->getAddr());
329
330    // store size and command as they might be modified when
331    // forwarding the packet
332    unsigned int pkt_size = pkt->hasData() ? pkt->getSize() : 0;
333    unsigned int pkt_cmd = pkt->cmdToIndex();
334
335    // responses are never express snoops
336    assert(!pkt->isExpressSnoop());
337
338    calcPacketTiming(pkt);
339    Tick packetFinishTime = pkt->busLastWordDelay + curTick();
340
341    // forward it either as a snoop response or a normal response
342    if (forwardAsSnoop) {
343        // this is a snoop response to a snoop request we forwarded,
344        // e.g. coming from the L1 and going to the L2, and it should
345        // be forwarded as a snoop response
346        bool success M5_VAR_USED =
347            masterPorts[dest_port_id]->sendTimingSnoopResp(pkt);
348        pktCount[slave_port_id][dest_port_id]++;
349        totPktSize[slave_port_id][dest_port_id] += pkt_size;
350        assert(success);
351
352        snoopRespLayer.succeededTiming(packetFinishTime);
353    } else {
354        // we got a snoop response on one of our slave ports,
355        // i.e. from a coherent master connected to the bus, and
356        // since we created the snoop request as part of
357        // recvTiming, this should now be a normal response again
358        outstandingReq.erase(pkt->req);
359
360        // this is a snoop response from a coherent master, with a
361        // destination field set on its way through the bus as
362        // request, hence it should never go back to where the
363        // snoop response came from, but instead to where the
364        // original request came from
365        assert(slave_port_id != dest_port_id);
366
367        // as a normal response, it should go back to a master through
368        // one of our slave ports, at this point we are ignoring the
369        // fact that the response layer could be busy and do not touch
370        // its state
371        bool success M5_VAR_USED =
372            slavePorts[dest_port_id]->sendTimingResp(pkt);
373
374        // @todo Put the response in an internal FIFO and pass it on
375        // to the response layer from there
376
377        // currently it is illegal to block responses... can lead
378        // to deadlock
379        assert(success);
380    }
381
382    // stats updates
383    transDist[pkt_cmd]++;
384    snoopDataThroughBus += pkt_size;
385
386    return true;
387}
388
389
390void
391CoherentBus::forwardTiming(PacketPtr pkt, PortID exclude_slave_port_id)
392{
393    DPRINTF(CoherentBus, "%s for %s address %x size %d\n", __func__,
394            pkt->cmdString(), pkt->getAddr(), pkt->getSize());
395
396    // snoops should only happen if the system isn't bypassing caches
397    assert(!system->bypassCaches());
398
399    for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
400        SlavePort *p = *s;
401        // we could have gotten this request from a snooping master
402        // (corresponding to our own slave port that is also in
403        // snoopPorts) and should not send it back to where it came
404        // from
405        if (exclude_slave_port_id == InvalidPortID ||
406            p->getId() != exclude_slave_port_id) {
407            // cache is not allowed to refuse snoop
408            p->sendTimingSnoopReq(pkt);
409        }
410    }
411}
412
413void
414CoherentBus::recvRetry(PortID master_port_id)
415{
416    // responses and snoop responses never block on forwarding them,
417    // so the retry will always be coming from a port to which we
418    // tried to forward a request
419    reqLayer.recvRetry(master_port_id);
420}
421
422Tick
423CoherentBus::recvAtomic(PacketPtr pkt, PortID slave_port_id)
424{
425    DPRINTF(CoherentBus, "recvAtomic: packet src %s addr 0x%x cmd %s\n",
426            slavePorts[slave_port_id]->name(), pkt->getAddr(),
427            pkt->cmdString());
428
429    // add the request data
430    dataThroughBus += pkt->hasData() ? pkt->getSize() : 0;
431
432    MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
433    Tick snoop_response_latency = 0;
434
435    // uncacheable requests need never be snooped
436    if (!pkt->req->isUncacheable() && !system->bypassCaches()) {
437        // forward to all snoopers but the source
438        std::pair<MemCmd, Tick> snoop_result =
439            forwardAtomic(pkt, slave_port_id);
440        snoop_response_cmd = snoop_result.first;
441        snoop_response_latency = snoop_result.second;
442    }
443
444    // even if we had a snoop response, we must continue and also
445    // perform the actual request at the destination
446    PortID dest_id = findPort(pkt->getAddr());
447
448    // forward the request to the appropriate destination
449    Tick response_latency = masterPorts[dest_id]->sendAtomic(pkt);
450
451    // if we got a response from a snooper, restore it here
452    if (snoop_response_cmd != MemCmd::InvalidCmd) {
453        // no one else should have responded
454        assert(!pkt->isResponse());
455        pkt->cmd = snoop_response_cmd;
456        response_latency = snoop_response_latency;
457    }
458
459    // add the response data
460    if (pkt->isResponse())
461        dataThroughBus += pkt->hasData() ? pkt->getSize() : 0;
462
463    // @todo: Not setting first-word time
464    pkt->busLastWordDelay = response_latency;
465    return response_latency;
466}
467
468Tick
469CoherentBus::recvAtomicSnoop(PacketPtr pkt, PortID master_port_id)
470{
471    DPRINTF(CoherentBus, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n",
472            masterPorts[master_port_id]->name(), pkt->getAddr(),
473            pkt->cmdString());
474
475    // add the request snoop data
476    snoopDataThroughBus += pkt->hasData() ? pkt->getSize() : 0;
477
478    // forward to all snoopers
479    std::pair<MemCmd, Tick> snoop_result =
480        forwardAtomic(pkt, InvalidPortID);
481    MemCmd snoop_response_cmd = snoop_result.first;
482    Tick snoop_response_latency = snoop_result.second;
483
484    if (snoop_response_cmd != MemCmd::InvalidCmd)
485        pkt->cmd = snoop_response_cmd;
486
487    // add the response snoop data
488    if (pkt->isResponse())
489        snoopDataThroughBus += pkt->hasData() ? pkt->getSize() : 0;
490
491    // @todo: Not setting first-word time
492    pkt->busLastWordDelay = snoop_response_latency;
493    return snoop_response_latency;
494}
495
496std::pair<MemCmd, Tick>
497CoherentBus::forwardAtomic(PacketPtr pkt, PortID exclude_slave_port_id)
498{
499    // the packet may be changed on snoops, record the original
500    // command to enable us to restore it between snoops so that
501    // additional snoops can take place properly
502    MemCmd orig_cmd = pkt->cmd;
503    MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
504    Tick snoop_response_latency = 0;
505
506    // snoops should only happen if the system isn't bypassing caches
507    assert(!system->bypassCaches());
508
509    for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
510        SlavePort *p = *s;
511        // we could have gotten this request from a snooping master
512        // (corresponding to our own slave port that is also in
513        // snoopPorts) and should not send it back to where it came
514        // from
515        if (exclude_slave_port_id == InvalidPortID ||
516            p->getId() != exclude_slave_port_id) {
517            Tick latency = p->sendAtomicSnoop(pkt);
518            // in contrast to a functional access, we have to keep on
519            // going as all snoopers must be updated even if we get a
520            // response
521            if (pkt->isResponse()) {
522                // response from snoop agent
523                assert(pkt->cmd != orig_cmd);
524                assert(pkt->memInhibitAsserted());
525                // should only happen once
526                assert(snoop_response_cmd == MemCmd::InvalidCmd);
527                // save response state
528                snoop_response_cmd = pkt->cmd;
529                snoop_response_latency = latency;
530                // restore original packet state for remaining snoopers
531                pkt->cmd = orig_cmd;
532            }
533        }
534    }
535
536    // the packet is restored as part of the loop and any potential
537    // snoop response is part of the returned pair
538    return std::make_pair(snoop_response_cmd, snoop_response_latency);
539}
540
541void
542CoherentBus::recvFunctional(PacketPtr pkt, PortID slave_port_id)
543{
544    if (!pkt->isPrint()) {
545        // don't do DPRINTFs on PrintReq as it clutters up the output
546        DPRINTF(CoherentBus,
547                "recvFunctional: packet src %s addr 0x%x cmd %s\n",
548                slavePorts[slave_port_id]->name(), pkt->getAddr(),
549                pkt->cmdString());
550    }
551
552    // uncacheable requests need never be snooped
553    if (!pkt->req->isUncacheable() && !system->bypassCaches()) {
554        // forward to all snoopers but the source
555        forwardFunctional(pkt, slave_port_id);
556    }
557
558    // there is no need to continue if the snooping has found what we
559    // were looking for and the packet is already a response
560    if (!pkt->isResponse()) {
561        PortID dest_id = findPort(pkt->getAddr());
562
563        masterPorts[dest_id]->sendFunctional(pkt);
564    }
565}
566
567void
568CoherentBus::recvFunctionalSnoop(PacketPtr pkt, PortID master_port_id)
569{
570    if (!pkt->isPrint()) {
571        // don't do DPRINTFs on PrintReq as it clutters up the output
572        DPRINTF(CoherentBus,
573                "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n",
574                masterPorts[master_port_id]->name(), pkt->getAddr(),
575                pkt->cmdString());
576    }
577
578    // forward to all snoopers
579    forwardFunctional(pkt, InvalidPortID);
580}
581
582void
583CoherentBus::forwardFunctional(PacketPtr pkt, PortID exclude_slave_port_id)
584{
585    // snoops should only happen if the system isn't bypassing caches
586    assert(!system->bypassCaches());
587
588    for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
589        SlavePort *p = *s;
590        // we could have gotten this request from a snooping master
591        // (corresponding to our own slave port that is also in
592        // snoopPorts) and should not send it back to where it came
593        // from
594        if (exclude_slave_port_id == InvalidPortID ||
595            p->getId() != exclude_slave_port_id)
596            p->sendFunctionalSnoop(pkt);
597
598        // if we get a response we are done
599        if (pkt->isResponse()) {
600            break;
601        }
602    }
603}
604
605unsigned int
606CoherentBus::drain(DrainManager *dm)
607{
608    // sum up the individual layers
609    return reqLayer.drain(dm) + respLayer.drain(dm) + snoopRespLayer.drain(dm);
610}
611
612void
613CoherentBus::regStats()
614{
615    // register the stats of the base class and our three bus layers
616    BaseBus::regStats();
617    reqLayer.regStats();
618    respLayer.regStats();
619    snoopRespLayer.regStats();
620
621    dataThroughBus
622        .name(name() + ".data_through_bus")
623        .desc("Total data (bytes)")
624        ;
625
626    snoopDataThroughBus
627        .name(name() + ".snoop_data_through_bus")
628        .desc("Total snoop data (bytes)")
629    ;
630
631    throughput
632        .name(name() + ".throughput")
633        .desc("Throughput (bytes/s)")
634        .precision(0)
635        ;
636
637    throughput = (dataThroughBus + snoopDataThroughBus) / simSeconds;
638}
639
640CoherentBus *
641CoherentBusParams::create()
642{
643    return new CoherentBus(this);
644}
645