coherent_xbar.cc revision 9524
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", p->clock),
59      respLayer(*this, ".respLayer", p->clock),
60      snoopRespLayer(*this, ".snoopRespLayer", p->clock),
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    Tick headerFinishTime = is_express_snoop ? 0 : calcPacketTiming(pkt);
139    Tick packetFinishTime = is_express_snoop ? 0 : pkt->finishTime;
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, headerFinishTime);
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->finishTime;
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->finishTime;
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    pkt->finishTime = curTick() + response_latency;
389    return response_latency;
390}
391
392Tick
393CoherentBus::recvAtomicSnoop(PacketPtr pkt, PortID master_port_id)
394{
395    DPRINTF(CoherentBus, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n",
396            masterPorts[master_port_id]->name(), pkt->getAddr(),
397            pkt->cmdString());
398
399    // forward to all snoopers
400    std::pair<MemCmd, Tick> snoop_result =
401        forwardAtomic(pkt, InvalidPortID);
402    MemCmd snoop_response_cmd = snoop_result.first;
403    Tick snoop_response_latency = snoop_result.second;
404
405    if (snoop_response_cmd != MemCmd::InvalidCmd)
406        pkt->cmd = snoop_response_cmd;
407
408    pkt->finishTime = curTick() + snoop_response_latency;
409    return snoop_response_latency;
410}
411
412std::pair<MemCmd, Tick>
413CoherentBus::forwardAtomic(PacketPtr pkt, PortID exclude_slave_port_id)
414{
415    // the packet may be changed on snoops, record the original
416    // command to enable us to restore it between snoops so that
417    // additional snoops can take place properly
418    MemCmd orig_cmd = pkt->cmd;
419    MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
420    Tick snoop_response_latency = 0;
421
422    // snoops should only happen if the system isn't bypassing caches
423    assert(!system->bypassCaches());
424
425    for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
426        SlavePort *p = *s;
427        // we could have gotten this request from a snooping master
428        // (corresponding to our own slave port that is also in
429        // snoopPorts) and should not send it back to where it came
430        // from
431        if (exclude_slave_port_id == InvalidPortID ||
432            p->getId() != exclude_slave_port_id) {
433            Tick latency = p->sendAtomicSnoop(pkt);
434            // in contrast to a functional access, we have to keep on
435            // going as all snoopers must be updated even if we get a
436            // response
437            if (pkt->isResponse()) {
438                // response from snoop agent
439                assert(pkt->cmd != orig_cmd);
440                assert(pkt->memInhibitAsserted());
441                // should only happen once
442                assert(snoop_response_cmd == MemCmd::InvalidCmd);
443                // save response state
444                snoop_response_cmd = pkt->cmd;
445                snoop_response_latency = latency;
446                // restore original packet state for remaining snoopers
447                pkt->cmd = orig_cmd;
448            }
449        }
450    }
451
452    // the packet is restored as part of the loop and any potential
453    // snoop response is part of the returned pair
454    return std::make_pair(snoop_response_cmd, snoop_response_latency);
455}
456
457void
458CoherentBus::recvFunctional(PacketPtr pkt, PortID slave_port_id)
459{
460    if (!pkt->isPrint()) {
461        // don't do DPRINTFs on PrintReq as it clutters up the output
462        DPRINTF(CoherentBus,
463                "recvFunctional: packet src %s addr 0x%x cmd %s\n",
464                slavePorts[slave_port_id]->name(), pkt->getAddr(),
465                pkt->cmdString());
466    }
467
468    // uncacheable requests need never be snooped
469    if (!pkt->req->isUncacheable() && !system->bypassCaches()) {
470        // forward to all snoopers but the source
471        forwardFunctional(pkt, slave_port_id);
472    }
473
474    // there is no need to continue if the snooping has found what we
475    // were looking for and the packet is already a response
476    if (!pkt->isResponse()) {
477        PortID dest_id = findPort(pkt->getAddr());
478
479        masterPorts[dest_id]->sendFunctional(pkt);
480    }
481}
482
483void
484CoherentBus::recvFunctionalSnoop(PacketPtr pkt, PortID master_port_id)
485{
486    if (!pkt->isPrint()) {
487        // don't do DPRINTFs on PrintReq as it clutters up the output
488        DPRINTF(CoherentBus,
489                "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n",
490                masterPorts[master_port_id]->name(), pkt->getAddr(),
491                pkt->cmdString());
492    }
493
494    // forward to all snoopers
495    forwardFunctional(pkt, InvalidPortID);
496}
497
498void
499CoherentBus::forwardFunctional(PacketPtr pkt, PortID exclude_slave_port_id)
500{
501    // snoops should only happen if the system isn't bypassing caches
502    assert(!system->bypassCaches());
503
504    for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
505        SlavePort *p = *s;
506        // we could have gotten this request from a snooping master
507        // (corresponding to our own slave port that is also in
508        // snoopPorts) and should not send it back to where it came
509        // from
510        if (exclude_slave_port_id == InvalidPortID ||
511            p->getId() != exclude_slave_port_id)
512            p->sendFunctionalSnoop(pkt);
513
514        // if we get a response we are done
515        if (pkt->isResponse()) {
516            break;
517        }
518    }
519}
520
521unsigned int
522CoherentBus::drain(DrainManager *dm)
523{
524    // sum up the individual layers
525    return reqLayer.drain(dm) + respLayer.drain(dm) + snoopRespLayer.drain(dm);
526}
527
528CoherentBus *
529CoherentBusParams::create()
530{
531    return new CoherentBus(this);
532}
533