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