coherent_xbar.cc revision 9549
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            // undo the calculation so we can check for 0 again
183            pkt->busFirstWordDelay = pkt->busLastWordDelay = 0;
184
185            DPRINTF(CoherentBus, "recvTimingReq: src %s %s 0x%x RETRY\n",
186                    src_port->name(), pkt->cmdString(), pkt->getAddr());
187
188            // update the bus state and schedule an idle event
189            reqLayer.failedTiming(src_port, clockEdge(Cycles(headerCycles)));
190        } else {
191            // update the bus state and schedule an idle event
192            reqLayer.succeededTiming(packetFinishTime);
193        }
194    }
195
196    return success;
197}
198
199bool
200CoherentBus::recvTimingResp(PacketPtr pkt, PortID master_port_id)
201{
202    // determine the source port based on the id
203    MasterPort *src_port = masterPorts[master_port_id];
204
205    // test if the bus should be considered occupied for the current
206    // port
207    if (!respLayer.tryTiming(src_port)) {
208        DPRINTF(CoherentBus, "recvTimingResp: src %s %s 0x%x BUSY\n",
209                src_port->name(), pkt->cmdString(), pkt->getAddr());
210        return false;
211    }
212
213    DPRINTF(CoherentBus, "recvTimingResp: src %s %s 0x%x\n",
214            src_port->name(), pkt->cmdString(), pkt->getAddr());
215
216    calcPacketTiming(pkt);
217    Tick packetFinishTime = pkt->busLastWordDelay + curTick();
218
219    // the packet is a normal response to a request that we should
220    // have seen passing through the bus
221    assert(outstandingReq.find(pkt->req) != outstandingReq.end());
222
223    // remove it as outstanding
224    outstandingReq.erase(pkt->req);
225
226    // send the packet to the destination through one of our slave
227    // ports, as determined by the destination field
228    bool success M5_VAR_USED = slavePorts[pkt->getDest()]->sendTimingResp(pkt);
229
230    // currently it is illegal to block responses... can lead to
231    // deadlock
232    assert(success);
233
234    respLayer.succeededTiming(packetFinishTime);
235
236    return true;
237}
238
239void
240CoherentBus::recvTimingSnoopReq(PacketPtr pkt, PortID master_port_id)
241{
242    DPRINTF(CoherentBus, "recvTimingSnoopReq: src %s %s 0x%x\n",
243            masterPorts[master_port_id]->name(), pkt->cmdString(),
244            pkt->getAddr());
245
246    // we should only see express snoops from caches
247    assert(pkt->isExpressSnoop());
248
249    // set the source port for routing of the response
250    pkt->setSrc(master_port_id);
251
252    // forward to all snoopers
253    forwardTiming(pkt, InvalidPortID);
254
255    // a snoop request came from a connected slave device (one of
256    // our master ports), and if it is not coming from the slave
257    // device responsible for the address range something is
258    // wrong, hence there is nothing further to do as the packet
259    // would be going back to where it came from
260    assert(master_port_id == findPort(pkt->getAddr()));
261}
262
263bool
264CoherentBus::recvTimingSnoopResp(PacketPtr pkt, PortID slave_port_id)
265{
266    // determine the source port based on the id
267    SlavePort* src_port = slavePorts[slave_port_id];
268
269    // test if the bus should be considered occupied for the current
270    // port
271    if (!snoopRespLayer.tryTiming(src_port)) {
272        DPRINTF(CoherentBus, "recvTimingSnoopResp: src %s %s 0x%x BUSY\n",
273                src_port->name(), pkt->cmdString(), pkt->getAddr());
274        return false;
275    }
276
277    DPRINTF(CoherentBus, "recvTimingSnoop: src %s %s 0x%x\n",
278            src_port->name(), pkt->cmdString(), pkt->getAddr());
279
280    // get the destination from the packet
281    PortID dest = pkt->getDest();
282
283    // responses are never express snoops
284    assert(!pkt->isExpressSnoop());
285
286    calcPacketTiming(pkt);
287    Tick packetFinishTime = pkt->busLastWordDelay + curTick();
288
289    // determine if the response is from a snoop request we
290    // created as the result of a normal request (in which case it
291    // should be in the outstandingReq), or if we merely forwarded
292    // someone else's snoop request
293    if (outstandingReq.find(pkt->req) == outstandingReq.end()) {
294        // this is a snoop response to a snoop request we
295        // forwarded, e.g. coming from the L1 and going to the L2
296        // this should be forwarded as a snoop response
297        bool success M5_VAR_USED = masterPorts[dest]->sendTimingSnoopResp(pkt);
298        assert(success);
299    } else {
300        // we got a snoop response on one of our slave ports,
301        // i.e. from a coherent master connected to the bus, and
302        // since we created the snoop request as part of
303        // recvTiming, this should now be a normal response again
304        outstandingReq.erase(pkt->req);
305
306        // this is a snoop response from a coherent master, with a
307        // destination field set on its way through the bus as
308        // request, hence it should never go back to where the
309        // snoop response came from, but instead to where the
310        // original request came from
311        assert(slave_port_id != dest);
312
313        // as a normal response, it should go back to a master
314        // through one of our slave ports
315        bool success M5_VAR_USED = slavePorts[dest]->sendTimingResp(pkt);
316
317        // currently it is illegal to block responses... can lead
318        // to deadlock
319        assert(success);
320    }
321
322    snoopRespLayer.succeededTiming(packetFinishTime);
323
324    return true;
325}
326
327
328void
329CoherentBus::forwardTiming(PacketPtr pkt, PortID exclude_slave_port_id)
330{
331    // snoops should only happen if the system isn't bypassing caches
332    assert(!system->bypassCaches());
333
334    for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
335        SlavePort *p = *s;
336        // we could have gotten this request from a snooping master
337        // (corresponding to our own slave port that is also in
338        // snoopPorts) and should not send it back to where it came
339        // from
340        if (exclude_slave_port_id == InvalidPortID ||
341            p->getId() != exclude_slave_port_id) {
342            // cache is not allowed to refuse snoop
343            p->sendTimingSnoopReq(pkt);
344        }
345    }
346}
347
348void
349CoherentBus::recvRetry()
350{
351    // responses and snoop responses never block on forwarding them,
352    // so the retry will always be coming from a port to which we
353    // tried to forward a request
354    reqLayer.recvRetry();
355}
356
357Tick
358CoherentBus::recvAtomic(PacketPtr pkt, PortID slave_port_id)
359{
360    DPRINTF(CoherentBus, "recvAtomic: packet src %s addr 0x%x cmd %s\n",
361            slavePorts[slave_port_id]->name(), pkt->getAddr(),
362            pkt->cmdString());
363
364    MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
365    Tick snoop_response_latency = 0;
366
367    // uncacheable requests need never be snooped
368    if (!pkt->req->isUncacheable() && !system->bypassCaches()) {
369        // forward to all snoopers but the source
370        std::pair<MemCmd, Tick> snoop_result =
371            forwardAtomic(pkt, slave_port_id);
372        snoop_response_cmd = snoop_result.first;
373        snoop_response_latency = snoop_result.second;
374    }
375
376    // even if we had a snoop response, we must continue and also
377    // perform the actual request at the destination
378    PortID dest_id = findPort(pkt->getAddr());
379
380    // forward the request to the appropriate destination
381    Tick response_latency = masterPorts[dest_id]->sendAtomic(pkt);
382
383    // if we got a response from a snooper, restore it here
384    if (snoop_response_cmd != MemCmd::InvalidCmd) {
385        // no one else should have responded
386        assert(!pkt->isResponse());
387        pkt->cmd = snoop_response_cmd;
388        response_latency = snoop_response_latency;
389    }
390
391    // @todo: Not setting first-word time
392    pkt->busLastWordDelay = response_latency;
393    return response_latency;
394}
395
396Tick
397CoherentBus::recvAtomicSnoop(PacketPtr pkt, PortID master_port_id)
398{
399    DPRINTF(CoherentBus, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n",
400            masterPorts[master_port_id]->name(), pkt->getAddr(),
401            pkt->cmdString());
402
403    // forward to all snoopers
404    std::pair<MemCmd, Tick> snoop_result =
405        forwardAtomic(pkt, InvalidPortID);
406    MemCmd snoop_response_cmd = snoop_result.first;
407    Tick snoop_response_latency = snoop_result.second;
408
409    if (snoop_response_cmd != MemCmd::InvalidCmd)
410        pkt->cmd = snoop_response_cmd;
411
412    // @todo: Not setting first-word time
413    pkt->busLastWordDelay = snoop_response_latency;
414    return snoop_response_latency;
415}
416
417std::pair<MemCmd, Tick>
418CoherentBus::forwardAtomic(PacketPtr pkt, PortID exclude_slave_port_id)
419{
420    // the packet may be changed on snoops, record the original
421    // command to enable us to restore it between snoops so that
422    // additional snoops can take place properly
423    MemCmd orig_cmd = pkt->cmd;
424    MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
425    Tick snoop_response_latency = 0;
426
427    // snoops should only happen if the system isn't bypassing caches
428    assert(!system->bypassCaches());
429
430    for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
431        SlavePort *p = *s;
432        // we could have gotten this request from a snooping master
433        // (corresponding to our own slave port that is also in
434        // snoopPorts) and should not send it back to where it came
435        // from
436        if (exclude_slave_port_id == InvalidPortID ||
437            p->getId() != exclude_slave_port_id) {
438            Tick latency = p->sendAtomicSnoop(pkt);
439            // in contrast to a functional access, we have to keep on
440            // going as all snoopers must be updated even if we get a
441            // response
442            if (pkt->isResponse()) {
443                // response from snoop agent
444                assert(pkt->cmd != orig_cmd);
445                assert(pkt->memInhibitAsserted());
446                // should only happen once
447                assert(snoop_response_cmd == MemCmd::InvalidCmd);
448                // save response state
449                snoop_response_cmd = pkt->cmd;
450                snoop_response_latency = latency;
451                // restore original packet state for remaining snoopers
452                pkt->cmd = orig_cmd;
453            }
454        }
455    }
456
457    // the packet is restored as part of the loop and any potential
458    // snoop response is part of the returned pair
459    return std::make_pair(snoop_response_cmd, snoop_response_latency);
460}
461
462void
463CoherentBus::recvFunctional(PacketPtr pkt, PortID slave_port_id)
464{
465    if (!pkt->isPrint()) {
466        // don't do DPRINTFs on PrintReq as it clutters up the output
467        DPRINTF(CoherentBus,
468                "recvFunctional: packet src %s addr 0x%x cmd %s\n",
469                slavePorts[slave_port_id]->name(), pkt->getAddr(),
470                pkt->cmdString());
471    }
472
473    // uncacheable requests need never be snooped
474    if (!pkt->req->isUncacheable() && !system->bypassCaches()) {
475        // forward to all snoopers but the source
476        forwardFunctional(pkt, slave_port_id);
477    }
478
479    // there is no need to continue if the snooping has found what we
480    // were looking for and the packet is already a response
481    if (!pkt->isResponse()) {
482        PortID dest_id = findPort(pkt->getAddr());
483
484        masterPorts[dest_id]->sendFunctional(pkt);
485    }
486}
487
488void
489CoherentBus::recvFunctionalSnoop(PacketPtr pkt, PortID master_port_id)
490{
491    if (!pkt->isPrint()) {
492        // don't do DPRINTFs on PrintReq as it clutters up the output
493        DPRINTF(CoherentBus,
494                "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n",
495                masterPorts[master_port_id]->name(), pkt->getAddr(),
496                pkt->cmdString());
497    }
498
499    // forward to all snoopers
500    forwardFunctional(pkt, InvalidPortID);
501}
502
503void
504CoherentBus::forwardFunctional(PacketPtr pkt, PortID exclude_slave_port_id)
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            p->sendFunctionalSnoop(pkt);
518
519        // if we get a response we are done
520        if (pkt->isResponse()) {
521            break;
522        }
523    }
524}
525
526unsigned int
527CoherentBus::drain(DrainManager *dm)
528{
529    // sum up the individual layers
530    return reqLayer.drain(dm) + respLayer.drain(dm) + snoopRespLayer.drain(dm);
531}
532
533CoherentBus *
534CoherentBusParams::create()
535{
536    return new CoherentBus(this);
537}
538