coherent_xbar.cc revision 9547
1/*
2 * Copyright (c) 2011-2012 ARM Limited
3 * All rights reserved
4 *
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.
13 *
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
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.
27 *
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.
39 *
40 * Authors: Ali Saidi
41 *          Andreas Hansson
42 *          William Wang
43 */
44
45/**
46 * @file
47 * Definition of a bus object.
48 */
49
50#include "base/misc.hh"
51#include "base/trace.hh"
52#include "debug/BusAddrRanges.hh"
53#include "debug/CoherentBus.hh"
54#include "mem/coherent_bus.hh"
55#include "sim/system.hh"
56
57CoherentBus::CoherentBus(const CoherentBusParams *p)
58    : BaseBus(p), reqLayer(*this, ".reqLayer"),
59      respLayer(*this, ".respLayer"),
60      snoopRespLayer(*this, ".snoopRespLayer"),
61      system(p->system)
62{
63    // create the ports based on the size of the master and slave
64    // vector ports, and the presence of the default port, the ports
65    // are enumerated starting from zero
66    for (int i = 0; i < p->port_master_connection_count; ++i) {
67        std::string portName = csprintf("%s.master[%d]", name(), i);
68        MasterPort* bp = new CoherentBusMasterPort(portName, *this, i);
69        masterPorts.push_back(bp);
70    }
71
72    // see if we have a default slave device connected and if so add
73    // our corresponding master port
74    if (p->port_default_connection_count) {
75        defaultPortID = masterPorts.size();
76        std::string portName = name() + ".default";
77        MasterPort* bp = new CoherentBusMasterPort(portName, *this,
78                                                   defaultPortID);
79        masterPorts.push_back(bp);
80    }
81
82    // create the slave ports, once again starting at zero
83    for (int i = 0; i < p->port_slave_connection_count; ++i) {
84        std::string portName = csprintf("%s.slave[%d]", name(), i);
85        SlavePort* bp = new CoherentBusSlavePort(portName, *this, i);
86        slavePorts.push_back(bp);
87    }
88
89    clearPortCache();
90}
91
92void
93CoherentBus::init()
94{
95    // the base class is responsible for determining the block size
96    BaseBus::init();
97
98    // iterate over our slave ports and determine which of our
99    // neighbouring master ports are snooping and add them as snoopers
100    for (SlavePortConstIter p = slavePorts.begin(); p != slavePorts.end();
101         ++p) {
102        // check if the connected master port is snooping
103        if ((*p)->isSnooping()) {
104            DPRINTF(BusAddrRanges, "Adding snooping master %s\n",
105                    (*p)->getMasterPort().name());
106            snoopPorts.push_back(*p);
107        }
108    }
109
110    if (snoopPorts.empty())
111        warn("CoherentBus %s has no snooping ports attached!\n", name());
112}
113
114bool
115CoherentBus::recvTimingReq(PacketPtr pkt, PortID slave_port_id)
116{
117    // determine the source port based on the id
118    SlavePort *src_port = slavePorts[slave_port_id];
119
120    // remember if the packet is an express snoop
121    bool is_express_snoop = pkt->isExpressSnoop();
122
123    // test if the bus should be considered occupied for the current
124    // port, and exclude express snoops from the check
125    if (!is_express_snoop && !reqLayer.tryTiming(src_port)) {
126        DPRINTF(CoherentBus, "recvTimingReq: src %s %s 0x%x BUSY\n",
127                src_port->name(), pkt->cmdString(), pkt->getAddr());
128        return false;
129    }
130
131    DPRINTF(CoherentBus, "recvTimingReq: src %s %s expr %d 0x%x\n",
132            src_port->name(), pkt->cmdString(), is_express_snoop,
133            pkt->getAddr());
134
135    // set the source port for routing of the response
136    pkt->setSrc(slave_port_id);
137
138    calcPacketTiming(pkt);
139    Tick packetFinishTime = pkt->busLastWordDelay + curTick();
140
141    // uncacheable requests need never be snooped
142    if (!pkt->req->isUncacheable() && !system->bypassCaches()) {
143        // the packet is a memory-mapped request and should be
144        // broadcasted to our snoopers but the source
145        forwardTiming(pkt, slave_port_id);
146    }
147
148    // remember if we add an outstanding req so we can undo it if
149    // necessary, if the packet needs a response, we should add it
150    // as outstanding and express snoops never fail so there is
151    // not need to worry about them
152    bool add_outstanding = !is_express_snoop && pkt->needsResponse();
153
154    // keep track that we have an outstanding request packet
155    // matching this request, this is used by the coherency
156    // mechanism in determining what to do with snoop responses
157    // (in recvTimingSnoop)
158    if (add_outstanding) {
159        // we should never have an exsiting request outstanding
160        assert(outstandingReq.find(pkt->req) == outstandingReq.end());
161        outstandingReq.insert(pkt->req);
162    }
163
164    // since it is a normal request, determine the destination
165    // based on the address and attempt to send the packet
166    bool success = masterPorts[findPort(pkt->getAddr())]->sendTimingReq(pkt);
167
168    // if this is an express snoop, we are done at this point
169    if (is_express_snoop) {
170        assert(success);
171    } else {
172        // for normal requests, check if successful
173        if (!success)  {
174            // inhibited packets should never be forced to retry
175            assert(!pkt->memInhibitAsserted());
176
177            // if it was added as outstanding and the send failed, then
178            // erase it again
179            if (add_outstanding)
180                outstandingReq.erase(pkt->req);
181
182            DPRINTF(CoherentBus, "recvTimingReq: src %s %s 0x%x RETRY\n",
183                    src_port->name(), pkt->cmdString(), pkt->getAddr());
184
185            // update the bus state and schedule an idle event
186            reqLayer.failedTiming(src_port, clockEdge(Cycles(headerCycles)));
187        } else {
188            // update the bus state and schedule an idle event
189            reqLayer.succeededTiming(packetFinishTime);
190        }
191    }
192
193    return success;
194}
195
196bool
197CoherentBus::recvTimingResp(PacketPtr pkt, PortID master_port_id)
198{
199    // determine the source port based on the id
200    MasterPort *src_port = masterPorts[master_port_id];
201
202    // test if the bus should be considered occupied for the current
203    // port
204    if (!respLayer.tryTiming(src_port)) {
205        DPRINTF(CoherentBus, "recvTimingResp: src %s %s 0x%x BUSY\n",
206                src_port->name(), pkt->cmdString(), pkt->getAddr());
207        return false;
208    }
209
210    DPRINTF(CoherentBus, "recvTimingResp: src %s %s 0x%x\n",
211            src_port->name(), pkt->cmdString(), pkt->getAddr());
212
213    calcPacketTiming(pkt);
214    Tick packetFinishTime = pkt->busLastWordDelay + curTick();
215
216    // the packet is a normal response to a request that we should
217    // have seen passing through the bus
218    assert(outstandingReq.find(pkt->req) != outstandingReq.end());
219
220    // remove it as outstanding
221    outstandingReq.erase(pkt->req);
222
223    // send the packet to the destination through one of our slave
224    // ports, as determined by the destination field
225    bool success M5_VAR_USED = slavePorts[pkt->getDest()]->sendTimingResp(pkt);
226
227    // currently it is illegal to block responses... can lead to
228    // deadlock
229    assert(success);
230
231    respLayer.succeededTiming(packetFinishTime);
232
233    return true;
234}
235
236void
237CoherentBus::recvTimingSnoopReq(PacketPtr pkt, PortID master_port_id)
238{
239    DPRINTF(CoherentBus, "recvTimingSnoopReq: src %s %s 0x%x\n",
240            masterPorts[master_port_id]->name(), pkt->cmdString(),
241            pkt->getAddr());
242
243    // we should only see express snoops from caches
244    assert(pkt->isExpressSnoop());
245
246    // set the source port for routing of the response
247    pkt->setSrc(master_port_id);
248
249    // forward to all snoopers
250    forwardTiming(pkt, InvalidPortID);
251
252    // a snoop request came from a connected slave device (one of
253    // our master ports), and if it is not coming from the slave
254    // device responsible for the address range something is
255    // wrong, hence there is nothing further to do as the packet
256    // would be going back to where it came from
257    assert(master_port_id == findPort(pkt->getAddr()));
258}
259
260bool
261CoherentBus::recvTimingSnoopResp(PacketPtr pkt, PortID slave_port_id)
262{
263    // determine the source port based on the id
264    SlavePort* src_port = slavePorts[slave_port_id];
265
266    // test if the bus should be considered occupied for the current
267    // port
268    if (!snoopRespLayer.tryTiming(src_port)) {
269        DPRINTF(CoherentBus, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n",
270                src_port->name(), pkt->cmdString(), pkt->getAddr());
271        return false;
272    }
273
274    DPRINTF(CoherentBus, "recvTimingSnoop: src %s %s 0x%x\n",
275            src_port->name(), pkt->cmdString(), pkt->getAddr());
276
277    // get the destination from the packet
278    PortID dest = pkt->getDest();
279
280    // responses are never express snoops
281    assert(!pkt->isExpressSnoop());
282
283    calcPacketTiming(pkt);
284    Tick packetFinishTime = pkt->busLastWordDelay + curTick();
285
286    // determine if the response is from a snoop request we
287    // created as the result of a normal request (in which case it
288    // should be in the outstandingReq), or if we merely forwarded
289    // someone else's snoop request
290    if (outstandingReq.find(pkt->req) == outstandingReq.end()) {
291        // this is a snoop response to a snoop request we
292        // forwarded, e.g. coming from the L1 and going to the L2
293        // this should be forwarded as a snoop response
294        bool success M5_VAR_USED = masterPorts[dest]->sendTimingSnoopResp(pkt);
295        assert(success);
296    } else {
297        // we got a snoop response on one of our slave ports,
298        // i.e. from a coherent master connected to the bus, and
299        // since we created the snoop request as part of
300        // recvTiming, this should now be a normal response again
301        outstandingReq.erase(pkt->req);
302
303        // this is a snoop response from a coherent master, with a
304        // destination field set on its way through the bus as
305        // request, hence it should never go back to where the
306        // snoop response came from, but instead to where the
307        // original request came from
308        assert(slave_port_id != dest);
309
310        // as a normal response, it should go back to a master
311        // through one of our slave ports
312        bool success M5_VAR_USED = slavePorts[dest]->sendTimingResp(pkt);
313
314        // currently it is illegal to block responses... can lead
315        // to deadlock
316        assert(success);
317    }
318
319    snoopRespLayer.succeededTiming(packetFinishTime);
320
321    return true;
322}
323
324
325void
326CoherentBus::forwardTiming(PacketPtr pkt, PortID exclude_slave_port_id)
327{
328    // snoops should only happen if the system isn't bypassing caches
329    assert(!system->bypassCaches());
330
331    for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
332        SlavePort *p = *s;
333        // we could have gotten this request from a snooping master
334        // (corresponding to our own slave port that is also in
335        // snoopPorts) and should not send it back to where it came
336        // from
337        if (exclude_slave_port_id == InvalidPortID ||
338            p->getId() != exclude_slave_port_id) {
339            // cache is not allowed to refuse snoop
340            p->sendTimingSnoopReq(pkt);
341        }
342    }
343}
344
345void
346CoherentBus::recvRetry()
347{
348    // responses and snoop responses never block on forwarding them,
349    // so the retry will always be coming from a port to which we
350    // tried to forward a request
351    reqLayer.recvRetry();
352}
353
354Tick
355CoherentBus::recvAtomic(PacketPtr pkt, PortID slave_port_id)
356{
357    DPRINTF(CoherentBus, "recvAtomic: packet src %s addr 0x%x cmd %s\n",
358            slavePorts[slave_port_id]->name(), pkt->getAddr(),
359            pkt->cmdString());
360
361    MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
362    Tick snoop_response_latency = 0;
363
364    // uncacheable requests need never be snooped
365    if (!pkt->req->isUncacheable() && !system->bypassCaches()) {
366        // forward to all snoopers but the source
367        std::pair<MemCmd, Tick> snoop_result =
368            forwardAtomic(pkt, slave_port_id);
369        snoop_response_cmd = snoop_result.first;
370        snoop_response_latency = snoop_result.second;
371    }
372
373    // even if we had a snoop response, we must continue and also
374    // perform the actual request at the destination
375    PortID dest_id = findPort(pkt->getAddr());
376
377    // forward the request to the appropriate destination
378    Tick response_latency = masterPorts[dest_id]->sendAtomic(pkt);
379
380    // if we got a response from a snooper, restore it here
381    if (snoop_response_cmd != MemCmd::InvalidCmd) {
382        // no one else should have responded
383        assert(!pkt->isResponse());
384        pkt->cmd = snoop_response_cmd;
385        response_latency = snoop_response_latency;
386    }
387
388    // @todo: Not setting first-word time
389    pkt->busLastWordDelay = response_latency;
390    return response_latency;
391}
392
393Tick
394CoherentBus::recvAtomicSnoop(PacketPtr pkt, PortID master_port_id)
395{
396    DPRINTF(CoherentBus, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n",
397            masterPorts[master_port_id]->name(), pkt->getAddr(),
398            pkt->cmdString());
399
400    // forward to all snoopers
401    std::pair<MemCmd, Tick> snoop_result =
402        forwardAtomic(pkt, InvalidPortID);
403    MemCmd snoop_response_cmd = snoop_result.first;
404    Tick snoop_response_latency = snoop_result.second;
405
406    if (snoop_response_cmd != MemCmd::InvalidCmd)
407        pkt->cmd = snoop_response_cmd;
408
409    // @todo: Not setting first-word time
410    pkt->busLastWordDelay = snoop_response_latency;
411    return snoop_response_latency;
412}
413
414std::pair<MemCmd, Tick>
415CoherentBus::forwardAtomic(PacketPtr pkt, PortID exclude_slave_port_id)
416{
417    // the packet may be changed on snoops, record the original
418    // command to enable us to restore it between snoops so that
419    // additional snoops can take place properly
420    MemCmd orig_cmd = pkt->cmd;
421    MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
422    Tick snoop_response_latency = 0;
423
424    // snoops should only happen if the system isn't bypassing caches
425    assert(!system->bypassCaches());
426
427    for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
428        SlavePort *p = *s;
429        // we could have gotten this request from a snooping master
430        // (corresponding to our own slave port that is also in
431        // snoopPorts) and should not send it back to where it came
432        // from
433        if (exclude_slave_port_id == InvalidPortID ||
434            p->getId() != exclude_slave_port_id) {
435            Tick latency = p->sendAtomicSnoop(pkt);
436            // in contrast to a functional access, we have to keep on
437            // going as all snoopers must be updated even if we get a
438            // response
439            if (pkt->isResponse()) {
440                // response from snoop agent
441                assert(pkt->cmd != orig_cmd);
442                assert(pkt->memInhibitAsserted());
443                // should only happen once
444                assert(snoop_response_cmd == MemCmd::InvalidCmd);
445                // save response state
446                snoop_response_cmd = pkt->cmd;
447                snoop_response_latency = latency;
448                // restore original packet state for remaining snoopers
449                pkt->cmd = orig_cmd;
450            }
451        }
452    }
453
454    // the packet is restored as part of the loop and any potential
455    // snoop response is part of the returned pair
456    return std::make_pair(snoop_response_cmd, snoop_response_latency);
457}
458
459void
460CoherentBus::recvFunctional(PacketPtr pkt, PortID slave_port_id)
461{
462    if (!pkt->isPrint()) {
463        // don't do DPRINTFs on PrintReq as it clutters up the output
464        DPRINTF(CoherentBus,
465                "recvFunctional: packet src %s addr 0x%x cmd %s\n",
466                slavePorts[slave_port_id]->name(), pkt->getAddr(),
467                pkt->cmdString());
468    }
469
470    // uncacheable requests need never be snooped
471    if (!pkt->req->isUncacheable() && !system->bypassCaches()) {
472        // forward to all snoopers but the source
473        forwardFunctional(pkt, slave_port_id);
474    }
475
476    // there is no need to continue if the snooping has found what we
477    // were looking for and the packet is already a response
478    if (!pkt->isResponse()) {
479        PortID dest_id = findPort(pkt->getAddr());
480
481        masterPorts[dest_id]->sendFunctional(pkt);
482    }
483}
484
485void
486CoherentBus::recvFunctionalSnoop(PacketPtr pkt, PortID master_port_id)
487{
488    if (!pkt->isPrint()) {
489        // don't do DPRINTFs on PrintReq as it clutters up the output
490        DPRINTF(CoherentBus,
491                "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n",
492                masterPorts[master_port_id]->name(), pkt->getAddr(),
493                pkt->cmdString());
494    }
495
496    // forward to all snoopers
497    forwardFunctional(pkt, InvalidPortID);
498}
499
500void
501CoherentBus::forwardFunctional(PacketPtr pkt, PortID exclude_slave_port_id)
502{
503    // snoops should only happen if the system isn't bypassing caches
504    assert(!system->bypassCaches());
505
506    for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
507        SlavePort *p = *s;
508        // we could have gotten this request from a snooping master
509        // (corresponding to our own slave port that is also in
510        // snoopPorts) and should not send it back to where it came
511        // from
512        if (exclude_slave_port_id == InvalidPortID ||
513            p->getId() != exclude_slave_port_id)
514            p->sendFunctionalSnoop(pkt);
515
516        // if we get a response we are done
517        if (pkt->isResponse()) {
518            break;
519        }
520    }
521}
522
523unsigned int
524CoherentBus::drain(DrainManager *dm)
525{
526    // sum up the individual layers
527    return reqLayer.drain(dm) + respLayer.drain(dm) + snoopRespLayer.drain(dm);
528}
529
530CoherentBus *
531CoherentBusParams::create()
532{
533    return new CoherentBus(this);
534}
535