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