coherent_xbar.cc revision 3210
1/*
2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Ali Saidi
29 */
30
31/**
32 * @file
33 * Definition of a bus object.
34 */
35
36
37#include "base/misc.hh"
38#include "base/trace.hh"
39#include "mem/bus.hh"
40#include "sim/builder.hh"
41
42Port *
43Bus::getPort(const std::string &if_name, int idx)
44{
45    if (if_name == "default")
46        if (defaultPort == NULL) {
47            defaultPort = new BusPort(csprintf("%s-default",name()), this,
48                    defaultId);
49            return defaultPort;
50        } else
51            fatal("Default port already set\n");
52
53    // if_name ignored?  forced to be empty?
54    int id = interfaces.size();
55    BusPort *bp = new BusPort(csprintf("%s-p%d", name(), id), this, id);
56    interfaces.push_back(bp);
57    return bp;
58}
59
60/** Get the ranges of anyone other buses that we are connected to. */
61void
62Bus::init()
63{
64    std::vector<Port*>::iterator intIter;
65
66    for (intIter = interfaces.begin(); intIter != interfaces.end(); intIter++)
67        (*intIter)->sendStatusChange(Port::RangeChange);
68}
69
70Bus::BusFreeEvent::BusFreeEvent(Bus *_bus) : Event(&mainEventQueue), bus(_bus)
71{
72    assert(!scheduled());
73}
74
75void Bus::BusFreeEvent::process()
76{
77    bus->recvRetry(0);
78}
79
80const char * Bus::BusFreeEvent::description()
81{
82    return "bus became available";
83}
84
85void
86Bus::occupyBus(int numCycles)
87{
88    //Move up when the bus will next be free
89    //We avoid the use of divide by adding repeatedly
90    //This should be faster if the value is updated frequently, but should
91    //be may be slower otherwise.
92
93    //Bring tickNextIdle up to the present tick
94    //There is some potential ambiguity where a cycle starts, which might make
95    //a difference when devices are acting right around a cycle boundary. Using
96    //a < allows things which happen exactly on a cycle boundary to take up only
97    //the following cycle. Anthing that happens later will have to "wait" for the
98    //end of that cycle, and then start using the bus after that.
99    while (tickNextIdle < curTick)
100        tickNextIdle += clock;
101    //Advance it numCycles bus cycles.
102    //XXX Should this use the repeating add trick as well?
103    tickNextIdle += (numCycles * clock);
104    if (!busIdle.scheduled()) {
105        busIdle.schedule(tickNextIdle);
106    } else {
107        busIdle.reschedule(tickNextIdle);
108    }
109    DPRINTF(Bus, "The bus is now occupied from tick %d to %d\n", curTick, tickNextIdle);
110}
111
112/** Function called by the port when the bus is receiving a Timing
113 * transaction.*/
114bool
115Bus::recvTiming(Packet *pkt)
116{
117    Port *port;
118    DPRINTF(Bus, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
119            pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
120
121    Port *pktPort = interfaces[pkt->getSrc()];
122
123    short dest = pkt->getDest();
124    if (dest == Packet::Broadcast) {
125        if (timingSnoop(pkt)) {
126            pkt->flags |= SNOOP_COMMIT;
127            bool success = timingSnoop(pkt);
128            assert(success);
129            if (pkt->flags & SATISFIED) {
130                //Cache-Cache transfer occuring
131                if (retryingPort) {
132                    retryList.pop_front();
133                    retryingPort = NULL;
134                }
135                return true;
136            }
137            port = findPort(pkt->getAddr(), pkt->getSrc());
138        } else {
139            //Snoop didn't succeed
140            addToRetryList(pktPort);
141            return false;
142        }
143    } else {
144        assert(dest >= 0 && dest < interfaces.size());
145        assert(dest != pkt->getSrc()); // catch infinite loops
146        port = interfaces[dest];
147    }
148
149    // The packet will be sent. Figure out how long it occupies the bus.
150    int numCycles = 0;
151    // Requests need one cycle to send an address
152    if (pkt->isRequest())
153        numCycles++;
154    else if (pkt->isResponse() || pkt->hasData()) {
155        // If a packet has data, it needs ceil(size/width) cycles to send it
156        // We're using the "adding instead of dividing" trick again here
157        if (pkt->hasData()) {
158            int dataSize = pkt->getSize();
159            for (int transmitted = 0; transmitted < dataSize;
160                    transmitted += width) {
161                numCycles++;
162            }
163        } else {
164            // If the packet didn't have data, it must have been a response.
165            // Those use the bus for one cycle to send their data.
166            numCycles++;
167        }
168    }
169
170    occupyBus(numCycles);
171
172    if (port->sendTiming(pkt))  {
173        // Packet was successfully sent. Return true.
174        // Also take care of retries
175        if (retryingPort) {
176            retryList.pop_front();
177            retryingPort = NULL;
178        }
179        return true;
180    }
181
182    // Packet not successfully sent. Leave or put it on the retry list.
183    addToRetryList(pktPort);
184    return false;
185}
186
187void
188Bus::recvRetry(int id)
189{
190    // If there's anything waiting...
191    if (retryList.size()) {
192        retryingPort = retryList.front();
193        retryingPort->sendRetry();
194        // If the retryingPort pointer isn't null, sendTiming wasn't called
195        if (retryingPort) {
196            warn("sendRetry didn't call sendTiming\n");
197            retryList.pop_front();
198            retryingPort = NULL;
199        }
200    }
201}
202
203Port *
204Bus::findPort(Addr addr, int id)
205{
206    /* An interval tree would be a better way to do this. --ali. */
207    int dest_id = -1;
208    int i = 0;
209    bool found = false;
210    AddrRangeIter iter;
211
212    while (i < portList.size() && !found)
213    {
214        if (portList[i].range == addr) {
215            dest_id = portList[i].portId;
216            found = true;
217            DPRINTF(Bus, "  found addr %#llx on device %d\n", addr, dest_id);
218        }
219        i++;
220    }
221
222    // Check if this matches the default range
223    if (dest_id == -1) {
224        for (iter = defaultRange.begin(); iter != defaultRange.end(); iter++) {
225            if (*iter == addr) {
226                DPRINTF(Bus, "  found addr %#llx on default\n", addr);
227                return defaultPort;
228            }
229        }
230        panic("Unable to find destination for addr: %#llx", addr);
231    }
232
233
234    // we shouldn't be sending this back to where it came from
235    assert(dest_id != id);
236
237    return interfaces[dest_id];
238}
239
240std::vector<int>
241Bus::findSnoopPorts(Addr addr, int id)
242{
243    int i = 0;
244    AddrRangeIter iter;
245    std::vector<int> ports;
246
247    while (i < portSnoopList.size())
248    {
249        if (portSnoopList[i].range == addr && portSnoopList[i].portId != id) {
250            //Careful  to not overlap ranges
251            //or snoop will be called more than once on the port
252            ports.push_back(portSnoopList[i].portId);
253            DPRINTF(Bus, "  found snoop addr %#llx on device%d\n", addr,
254                    portSnoopList[i].portId);
255        }
256        i++;
257    }
258    return ports;
259}
260
261void
262Bus::atomicSnoop(Packet *pkt)
263{
264    std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
265
266    while (!ports.empty())
267    {
268        interfaces[ports.back()]->sendAtomic(pkt);
269        ports.pop_back();
270    }
271}
272
273bool
274Bus::timingSnoop(Packet *pkt)
275{
276    std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
277    bool success = true;
278
279    while (!ports.empty() && success)
280    {
281        success = interfaces[ports.back()]->sendTiming(pkt);
282        ports.pop_back();
283    }
284
285    return success;
286}
287
288
289/** Function called by the port when the bus is receiving a Atomic
290 * transaction.*/
291Tick
292Bus::recvAtomic(Packet *pkt)
293{
294    DPRINTF(Bus, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
295            pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
296    assert(pkt->getDest() == Packet::Broadcast);
297    atomicSnoop(pkt);
298    return findPort(pkt->getAddr(), pkt->getSrc())->sendAtomic(pkt);
299}
300
301/** Function called by the port when the bus is receiving a Functional
302 * transaction.*/
303void
304Bus::recvFunctional(Packet *pkt)
305{
306    DPRINTF(Bus, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
307            pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
308    assert(pkt->getDest() == Packet::Broadcast);
309    atomicSnoop(pkt);
310    findPort(pkt->getAddr(), pkt->getSrc())->sendFunctional(pkt);
311}
312
313/** Function called by the port when the bus is receiving a status change.*/
314void
315Bus::recvStatusChange(Port::Status status, int id)
316{
317    AddrRangeList ranges;
318    AddrRangeList snoops;
319    int x;
320    AddrRangeIter iter;
321
322    assert(status == Port::RangeChange &&
323           "The other statuses need to be implemented.");
324
325    DPRINTF(BusAddrRanges, "received RangeChange from device id %d\n", id);
326
327    if (id == defaultId) {
328        defaultRange.clear();
329        defaultPort->getPeerAddressRanges(ranges, snoops);
330        assert(snoops.size() == 0);
331        for(iter = ranges.begin(); iter != ranges.end(); iter++) {
332            defaultRange.push_back(*iter);
333            DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for default range\n",
334                    iter->start, iter->end);
335        }
336    } else {
337
338        assert((id < interfaces.size() && id >= 0) || id == -1);
339        Port *port = interfaces[id];
340        std::vector<DevMap>::iterator portIter;
341        std::vector<DevMap>::iterator snoopIter;
342
343        // Clean out any previously existent ids
344        for (portIter = portList.begin(); portIter != portList.end(); ) {
345            if (portIter->portId == id)
346                portIter = portList.erase(portIter);
347            else
348                portIter++;
349        }
350
351        for (snoopIter = portSnoopList.begin(); snoopIter != portSnoopList.end(); ) {
352            if (snoopIter->portId == id)
353                snoopIter = portSnoopList.erase(snoopIter);
354            else
355                snoopIter++;
356        }
357
358        port->getPeerAddressRanges(ranges, snoops);
359
360        for(iter = snoops.begin(); iter != snoops.end(); iter++) {
361            DevMap dm;
362            dm.portId = id;
363            dm.range = *iter;
364
365            DPRINTF(BusAddrRanges, "Adding snoop range %#llx - %#llx for id %d\n",
366                    dm.range.start, dm.range.end, id);
367            portSnoopList.push_back(dm);
368        }
369
370        for(iter = ranges.begin(); iter != ranges.end(); iter++) {
371            DevMap dm;
372            dm.portId = id;
373            dm.range = *iter;
374
375            DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for id %d\n",
376                    dm.range.start, dm.range.end, id);
377            portList.push_back(dm);
378        }
379    }
380    DPRINTF(MMU, "port list has %d entries\n", portList.size());
381
382    // tell all our peers that our address range has changed.
383    // Don't tell the device that caused this change, it already knows
384    for (x = 0; x < interfaces.size(); x++)
385        if (x != id)
386            interfaces[x]->sendStatusChange(Port::RangeChange);
387
388    if (id != defaultId && defaultPort)
389        defaultPort->sendStatusChange(Port::RangeChange);
390}
391
392void
393Bus::addressRanges(AddrRangeList &resp, AddrRangeList &snoop, int id)
394{
395    std::vector<DevMap>::iterator portIter;
396    AddrRangeIter dflt_iter;
397    bool subset;
398
399    resp.clear();
400    snoop.clear();
401
402    DPRINTF(BusAddrRanges, "received address range request, returning:\n");
403
404    for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
405            dflt_iter++) {
406        resp.push_back(*dflt_iter);
407        DPRINTF(BusAddrRanges, "  -- %#llx : %#llx\n",dflt_iter->start,
408                dflt_iter->end);
409    }
410    for (portIter = portList.begin(); portIter != portList.end(); portIter++) {
411        subset = false;
412        for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
413                dflt_iter++) {
414            if ((portIter->range.start < dflt_iter->start &&
415                portIter->range.end >= dflt_iter->start) ||
416               (portIter->range.start < dflt_iter->end &&
417                portIter->range.end >= dflt_iter->end))
418                fatal("Devices can not set ranges that itersect the default set\
419                        but are not a subset of the default set.\n");
420            if (portIter->range.start >= dflt_iter->start &&
421                portIter->range.end <= dflt_iter->end) {
422                subset = true;
423                DPRINTF(BusAddrRanges, "  -- %#llx : %#llx is a SUBSET\n",
424                    portIter->range.start, portIter->range.end);
425            }
426        }
427        if (portIter->portId != id && !subset) {
428            resp.push_back(portIter->range);
429            DPRINTF(BusAddrRanges, "  -- %#llx : %#llx\n",
430                    portIter->range.start, portIter->range.end);
431        }
432    }
433}
434
435BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus)
436
437    Param<int> bus_id;
438    Param<int> clock;
439    Param<int> width;
440
441END_DECLARE_SIM_OBJECT_PARAMS(Bus)
442
443BEGIN_INIT_SIM_OBJECT_PARAMS(Bus)
444    INIT_PARAM(bus_id, "a globally unique bus id"),
445    INIT_PARAM(clock, "bus clock speed"),
446    INIT_PARAM(width, "width of the bus (bits)")
447END_INIT_SIM_OBJECT_PARAMS(Bus)
448
449CREATE_SIM_OBJECT(Bus)
450{
451    return new Bus(getInstanceName(), bus_id, clock, width);
452}
453
454REGISTER_SIM_OBJECT("Bus", Bus)
455