coherent_xbar.cc revision 9091
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        // 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 && !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            failedTiming(src_port, headerFinishTime);
180        } else {
181            // update the bus state and schedule an idle event
182            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 (!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    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 (!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    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
335Tick
336CoherentBus::recvAtomic(PacketPtr pkt, PortID slave_port_id)
337{
338    DPRINTF(CoherentBus, "recvAtomic: packet src %s addr 0x%x cmd %s\n",
339            slavePorts[slave_port_id]->name(), pkt->getAddr(),
340            pkt->cmdString());
341
342    MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
343    Tick snoop_response_latency = 0;
344
345    // uncacheable requests need never be snooped
346    if (!pkt->req->isUncacheable()) {
347        // forward to all snoopers but the source
348        std::pair<MemCmd, Tick> snoop_result =
349            forwardAtomic(pkt, slave_port_id);
350        snoop_response_cmd = snoop_result.first;
351        snoop_response_latency = snoop_result.second;
352    }
353
354    // even if we had a snoop response, we must continue and also
355    // perform the actual request at the destination
356    PortID dest_id = findPort(pkt->getAddr());
357
358    // forward the request to the appropriate destination
359    Tick response_latency = masterPorts[dest_id]->sendAtomic(pkt);
360
361    // if we got a response from a snooper, restore it here
362    if (snoop_response_cmd != MemCmd::InvalidCmd) {
363        // no one else should have responded
364        assert(!pkt->isResponse());
365        pkt->cmd = snoop_response_cmd;
366        response_latency = snoop_response_latency;
367    }
368
369    pkt->finishTime = curTick() + response_latency;
370    return response_latency;
371}
372
373Tick
374CoherentBus::recvAtomicSnoop(PacketPtr pkt, PortID master_port_id)
375{
376    DPRINTF(CoherentBus, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n",
377            masterPorts[master_port_id]->name(), pkt->getAddr(),
378            pkt->cmdString());
379
380    // forward to all snoopers
381    std::pair<MemCmd, Tick> snoop_result =
382        forwardAtomic(pkt, InvalidPortID);
383    MemCmd snoop_response_cmd = snoop_result.first;
384    Tick snoop_response_latency = snoop_result.second;
385
386    if (snoop_response_cmd != MemCmd::InvalidCmd)
387        pkt->cmd = snoop_response_cmd;
388
389    pkt->finishTime = curTick() + snoop_response_latency;
390    return snoop_response_latency;
391}
392
393std::pair<MemCmd, Tick>
394CoherentBus::forwardAtomic(PacketPtr pkt, PortID exclude_slave_port_id)
395{
396    // the packet may be changed on snoops, record the original
397    // command to enable us to restore it between snoops so that
398    // additional snoops can take place properly
399    MemCmd orig_cmd = pkt->cmd;
400    MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
401    Tick snoop_response_latency = 0;
402
403    for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
404        SlavePort *p = *s;
405        // we could have gotten this request from a snooping master
406        // (corresponding to our own slave port that is also in
407        // snoopPorts) and should not send it back to where it came
408        // from
409        if (exclude_slave_port_id == InvalidPortID ||
410            p->getId() != exclude_slave_port_id) {
411            Tick latency = p->sendAtomicSnoop(pkt);
412            // in contrast to a functional access, we have to keep on
413            // going as all snoopers must be updated even if we get a
414            // response
415            if (pkt->isResponse()) {
416                // response from snoop agent
417                assert(pkt->cmd != orig_cmd);
418                assert(pkt->memInhibitAsserted());
419                // should only happen once
420                assert(snoop_response_cmd == MemCmd::InvalidCmd);
421                // save response state
422                snoop_response_cmd = pkt->cmd;
423                snoop_response_latency = latency;
424                // restore original packet state for remaining snoopers
425                pkt->cmd = orig_cmd;
426            }
427        }
428    }
429
430    // the packet is restored as part of the loop and any potential
431    // snoop response is part of the returned pair
432    return std::make_pair(snoop_response_cmd, snoop_response_latency);
433}
434
435void
436CoherentBus::recvFunctional(PacketPtr pkt, PortID slave_port_id)
437{
438    if (!pkt->isPrint()) {
439        // don't do DPRINTFs on PrintReq as it clutters up the output
440        DPRINTF(CoherentBus,
441                "recvFunctional: packet src %s addr 0x%x cmd %s\n",
442                slavePorts[slave_port_id]->name(), pkt->getAddr(),
443                pkt->cmdString());
444    }
445
446    // uncacheable requests need never be snooped
447    if (!pkt->req->isUncacheable()) {
448        // forward to all snoopers but the source
449        forwardFunctional(pkt, slave_port_id);
450    }
451
452    // there is no need to continue if the snooping has found what we
453    // were looking for and the packet is already a response
454    if (!pkt->isResponse()) {
455        PortID dest_id = findPort(pkt->getAddr());
456
457        masterPorts[dest_id]->sendFunctional(pkt);
458    }
459}
460
461void
462CoherentBus::recvFunctionalSnoop(PacketPtr pkt, PortID master_port_id)
463{
464    if (!pkt->isPrint()) {
465        // don't do DPRINTFs on PrintReq as it clutters up the output
466        DPRINTF(CoherentBus,
467                "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n",
468                masterPorts[master_port_id]->name(), pkt->getAddr(),
469                pkt->cmdString());
470    }
471
472    // forward to all snoopers
473    forwardFunctional(pkt, InvalidPortID);
474}
475
476void
477CoherentBus::forwardFunctional(PacketPtr pkt, PortID exclude_slave_port_id)
478{
479    for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
480        SlavePort *p = *s;
481        // we could have gotten this request from a snooping master
482        // (corresponding to our own slave port that is also in
483        // snoopPorts) and should not send it back to where it came
484        // from
485        if (exclude_slave_port_id == InvalidPortID ||
486            p->getId() != exclude_slave_port_id)
487            p->sendFunctionalSnoop(pkt);
488
489        // if we get a response we are done
490        if (pkt->isResponse()) {
491            break;
492        }
493    }
494}
495
496CoherentBus *
497CoherentBusParams::create()
498{
499    return new CoherentBus(this);
500}
501