coherent_xbar.cc revision 9612
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    // snoops should only happen if the system isn't bypassing caches
341    assert(!system->bypassCaches());
342
343    for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
344        SlavePort *p = *s;
345        // we could have gotten this request from a snooping master
346        // (corresponding to our own slave port that is also in
347        // snoopPorts) and should not send it back to where it came
348        // from
349        if (exclude_slave_port_id == InvalidPortID ||
350            p->getId() != exclude_slave_port_id) {
351            // cache is not allowed to refuse snoop
352            p->sendTimingSnoopReq(pkt);
353        }
354    }
355}
356
357void
358CoherentBus::recvRetry(PortID master_port_id)
359{
360    // responses and snoop responses never block on forwarding them,
361    // so the retry will always be coming from a port to which we
362    // tried to forward a request
363    reqLayer.recvRetry(master_port_id);
364}
365
366Tick
367CoherentBus::recvAtomic(PacketPtr pkt, PortID slave_port_id)
368{
369    DPRINTF(CoherentBus, "recvAtomic: packet src %s addr 0x%x cmd %s\n",
370            slavePorts[slave_port_id]->name(), pkt->getAddr(),
371            pkt->cmdString());
372
373    MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
374    Tick snoop_response_latency = 0;
375
376    // uncacheable requests need never be snooped
377    if (!pkt->req->isUncacheable() && !system->bypassCaches()) {
378        // forward to all snoopers but the source
379        std::pair<MemCmd, Tick> snoop_result =
380            forwardAtomic(pkt, slave_port_id);
381        snoop_response_cmd = snoop_result.first;
382        snoop_response_latency = snoop_result.second;
383    }
384
385    // even if we had a snoop response, we must continue and also
386    // perform the actual request at the destination
387    PortID dest_id = findPort(pkt->getAddr());
388
389    // forward the request to the appropriate destination
390    Tick response_latency = masterPorts[dest_id]->sendAtomic(pkt);
391
392    // if we got a response from a snooper, restore it here
393    if (snoop_response_cmd != MemCmd::InvalidCmd) {
394        // no one else should have responded
395        assert(!pkt->isResponse());
396        pkt->cmd = snoop_response_cmd;
397        response_latency = snoop_response_latency;
398    }
399
400    // @todo: Not setting first-word time
401    pkt->busLastWordDelay = response_latency;
402    return response_latency;
403}
404
405Tick
406CoherentBus::recvAtomicSnoop(PacketPtr pkt, PortID master_port_id)
407{
408    DPRINTF(CoherentBus, "recvAtomicSnoop: packet src %s addr 0x%x cmd %s\n",
409            masterPorts[master_port_id]->name(), pkt->getAddr(),
410            pkt->cmdString());
411
412    // forward to all snoopers
413    std::pair<MemCmd, Tick> snoop_result =
414        forwardAtomic(pkt, InvalidPortID);
415    MemCmd snoop_response_cmd = snoop_result.first;
416    Tick snoop_response_latency = snoop_result.second;
417
418    if (snoop_response_cmd != MemCmd::InvalidCmd)
419        pkt->cmd = snoop_response_cmd;
420
421    // @todo: Not setting first-word time
422    pkt->busLastWordDelay = snoop_response_latency;
423    return snoop_response_latency;
424}
425
426std::pair<MemCmd, Tick>
427CoherentBus::forwardAtomic(PacketPtr pkt, PortID exclude_slave_port_id)
428{
429    // the packet may be changed on snoops, record the original
430    // command to enable us to restore it between snoops so that
431    // additional snoops can take place properly
432    MemCmd orig_cmd = pkt->cmd;
433    MemCmd snoop_response_cmd = MemCmd::InvalidCmd;
434    Tick snoop_response_latency = 0;
435
436    // snoops should only happen if the system isn't bypassing caches
437    assert(!system->bypassCaches());
438
439    for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
440        SlavePort *p = *s;
441        // we could have gotten this request from a snooping master
442        // (corresponding to our own slave port that is also in
443        // snoopPorts) and should not send it back to where it came
444        // from
445        if (exclude_slave_port_id == InvalidPortID ||
446            p->getId() != exclude_slave_port_id) {
447            Tick latency = p->sendAtomicSnoop(pkt);
448            // in contrast to a functional access, we have to keep on
449            // going as all snoopers must be updated even if we get a
450            // response
451            if (pkt->isResponse()) {
452                // response from snoop agent
453                assert(pkt->cmd != orig_cmd);
454                assert(pkt->memInhibitAsserted());
455                // should only happen once
456                assert(snoop_response_cmd == MemCmd::InvalidCmd);
457                // save response state
458                snoop_response_cmd = pkt->cmd;
459                snoop_response_latency = latency;
460                // restore original packet state for remaining snoopers
461                pkt->cmd = orig_cmd;
462            }
463        }
464    }
465
466    // the packet is restored as part of the loop and any potential
467    // snoop response is part of the returned pair
468    return std::make_pair(snoop_response_cmd, snoop_response_latency);
469}
470
471void
472CoherentBus::recvFunctional(PacketPtr pkt, PortID slave_port_id)
473{
474    if (!pkt->isPrint()) {
475        // don't do DPRINTFs on PrintReq as it clutters up the output
476        DPRINTF(CoherentBus,
477                "recvFunctional: packet src %s addr 0x%x cmd %s\n",
478                slavePorts[slave_port_id]->name(), pkt->getAddr(),
479                pkt->cmdString());
480    }
481
482    // uncacheable requests need never be snooped
483    if (!pkt->req->isUncacheable() && !system->bypassCaches()) {
484        // forward to all snoopers but the source
485        forwardFunctional(pkt, slave_port_id);
486    }
487
488    // there is no need to continue if the snooping has found what we
489    // were looking for and the packet is already a response
490    if (!pkt->isResponse()) {
491        PortID dest_id = findPort(pkt->getAddr());
492
493        masterPorts[dest_id]->sendFunctional(pkt);
494    }
495}
496
497void
498CoherentBus::recvFunctionalSnoop(PacketPtr pkt, PortID master_port_id)
499{
500    if (!pkt->isPrint()) {
501        // don't do DPRINTFs on PrintReq as it clutters up the output
502        DPRINTF(CoherentBus,
503                "recvFunctionalSnoop: packet src %s addr 0x%x cmd %s\n",
504                masterPorts[master_port_id]->name(), pkt->getAddr(),
505                pkt->cmdString());
506    }
507
508    // forward to all snoopers
509    forwardFunctional(pkt, InvalidPortID);
510}
511
512void
513CoherentBus::forwardFunctional(PacketPtr pkt, PortID exclude_slave_port_id)
514{
515    // snoops should only happen if the system isn't bypassing caches
516    assert(!system->bypassCaches());
517
518    for (SlavePortIter s = snoopPorts.begin(); s != snoopPorts.end(); ++s) {
519        SlavePort *p = *s;
520        // we could have gotten this request from a snooping master
521        // (corresponding to our own slave port that is also in
522        // snoopPorts) and should not send it back to where it came
523        // from
524        if (exclude_slave_port_id == InvalidPortID ||
525            p->getId() != exclude_slave_port_id)
526            p->sendFunctionalSnoop(pkt);
527
528        // if we get a response we are done
529        if (pkt->isResponse()) {
530            break;
531        }
532    }
533}
534
535unsigned int
536CoherentBus::drain(DrainManager *dm)
537{
538    // sum up the individual layers
539    return reqLayer.drain(dm) + respLayer.drain(dm) + snoopRespLayer.drain(dm);
540}
541
542CoherentBus *
543CoherentBusParams::create()
544{
545    return new CoherentBus(this);
546}
547