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