xbar.cc revision 3489
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
54    // if_name ignored?  forced to be empty?
55    int id = interfaces.size();
56    BusPort *bp = new BusPort(csprintf("%s-p%d", name(), id), this, id);
57    interfaces.push_back(bp);
58    return bp;
59}
60
61/** Get the ranges of anyone other buses that we are connected to. */
62void
63Bus::init()
64{
65    std::vector<BusPort*>::iterator intIter;
66
67    for (intIter = interfaces.begin(); intIter != interfaces.end(); intIter++)
68        (*intIter)->sendStatusChange(Port::RangeChange);
69}
70
71Bus::BusFreeEvent::BusFreeEvent(Bus *_bus) : Event(&mainEventQueue), bus(_bus)
72{}
73
74void Bus::BusFreeEvent::process()
75{
76    bus->recvRetry(-1);
77}
78
79const char * Bus::BusFreeEvent::description()
80{
81    return "bus became available";
82}
83
84void Bus::occupyBus(PacketPtr pkt)
85{
86    //Bring tickNextIdle up to the present tick
87    //There is some potential ambiguity where a cycle starts, which might make
88    //a difference when devices are acting right around a cycle boundary. Using
89    //a < allows things which happen exactly on a cycle boundary to take up only
90    //the following cycle. Anthing that happens later will have to "wait" for
91    //the end of that cycle, and then start using the bus after that.
92    while (tickNextIdle < curTick)
93        tickNextIdle += clock;
94
95    // The packet will be sent. Figure out how long it occupies the bus, and
96    // how much of that time is for the first "word", aka bus width.
97    int numCycles = 0;
98    // Requests need one cycle to send an address
99    if (pkt->isRequest())
100        numCycles++;
101    else if (pkt->isResponse() || pkt->hasData()) {
102        // If a packet has data, it needs ceil(size/width) cycles to send it
103        // We're using the "adding instead of dividing" trick again here
104        if (pkt->hasData()) {
105            int dataSize = pkt->getSize();
106            for (int transmitted = 0; transmitted < dataSize;
107                    transmitted += width) {
108                numCycles++;
109            }
110        } else {
111            // If the packet didn't have data, it must have been a response.
112            // Those use the bus for one cycle to send their data.
113            numCycles++;
114        }
115    }
116
117    // The first word will be delivered after the current tick, the delivery
118    // of the address if any, and one bus cycle to deliver the data
119    pkt->firstWordTime =
120        tickNextIdle +
121        pkt->isRequest() ? clock : 0 +
122        clock;
123
124    //Advance it numCycles bus cycles.
125    //XXX Should this use the repeated addition trick as well?
126    tickNextIdle += (numCycles * clock);
127    if (!busIdle.scheduled()) {
128        busIdle.schedule(tickNextIdle);
129    } else {
130        busIdle.reschedule(tickNextIdle);
131    }
132    DPRINTF(Bus, "The bus is now occupied from tick %d to %d\n",
133            curTick, tickNextIdle);
134
135    // The bus will become idle once the current packet is delivered.
136    pkt->finishTime = tickNextIdle;
137}
138
139/** Function called by the port when the bus is receiving a Timing
140 * transaction.*/
141bool
142Bus::recvTiming(PacketPtr pkt)
143{
144    Port *port;
145    DPRINTF(Bus, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
146            pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
147
148    BusPort *pktPort;
149    if (pkt->getSrc() == defaultId)
150        pktPort = defaultPort;
151    else pktPort = interfaces[pkt->getSrc()];
152
153    // If the bus is busy, or other devices are in line ahead of the current
154    // one, put this device on the retry list.
155    if (tickNextIdle > curTick ||
156            (retryList.size() && (!inRetry || pktPort != retryList.front()))) {
157        addToRetryList(pktPort);
158        return false;
159    }
160
161    short dest = pkt->getDest();
162    if (dest == Packet::Broadcast) {
163        if (timingSnoop(pkt)) {
164            bool success;
165
166            pkt->flags |= SNOOP_COMMIT;
167            success = timingSnoop(pkt);
168            assert(success);
169
170            if (pkt->flags & SATISFIED) {
171                //Cache-Cache transfer occuring
172                if (inRetry) {
173                    retryList.front()->onRetryList(false);
174                    retryList.pop_front();
175                    inRetry = false;
176                }
177                occupyBus(pkt);
178                return true;
179            }
180            port = findPort(pkt->getAddr(), pkt->getSrc());
181        } else {
182            //Snoop didn't succeed
183            DPRINTF(Bus, "Adding a retry to RETRY list %i\n", pktPort);
184            addToRetryList(pktPort);
185            return false;
186        }
187    } else {
188        assert(dest >= 0 && dest < interfaces.size());
189        assert(dest != pkt->getSrc()); // catch infinite loops
190        port = interfaces[dest];
191    }
192
193    occupyBus(pkt);
194
195    if (port->sendTiming(pkt))  {
196        // Packet was successfully sent. Return true.
197        // Also take care of retries
198        if (inRetry) {
199            DPRINTF(Bus, "Remove retry from list %i\n", retryList.front());
200            retryList.front()->onRetryList(false);
201            retryList.pop_front();
202            inRetry = false;
203        }
204        return true;
205    }
206
207    // Packet not successfully sent. Leave or put it on the retry list.
208    DPRINTF(Bus, "Adding a retry to RETRY list %i\n", pktPort);
209    addToRetryList(pktPort);
210    return false;
211}
212
213void
214Bus::recvRetry(int id)
215{
216    DPRINTF(Bus, "Received a retry\n");
217    // If there's anything waiting, and the bus isn't busy...
218    if (retryList.size() && curTick >= tickNextIdle) {
219        //retryingPort = retryList.front();
220        inRetry = true;
221        DPRINTF(Bus, "Sending a retry\n");
222        retryList.front()->sendRetry();
223        // If inRetry is still true, sendTiming wasn't called
224        if (inRetry)
225        {
226            retryList.front()->onRetryList(false);
227            retryList.pop_front();
228            inRetry = false;
229
230            //Bring tickNextIdle up to the present
231            while (tickNextIdle < curTick)
232                tickNextIdle += clock;
233
234            //Burn a cycle for the missed grant.
235            tickNextIdle += clock;
236
237            if (!busIdle.scheduled()) {
238                busIdle.schedule(tickNextIdle);
239            } else {
240                busIdle.reschedule(tickNextIdle);
241            }
242        }
243    }
244}
245
246Port *
247Bus::findPort(Addr addr, int id)
248{
249    /* An interval tree would be a better way to do this. --ali. */
250    int dest_id = -1;
251    int i = 0;
252    bool found = false;
253    AddrRangeIter iter;
254
255    while (i < portList.size() && !found)
256    {
257        if (portList[i].range == addr) {
258            dest_id = portList[i].portId;
259            found = true;
260            DPRINTF(Bus, "  found addr %#llx on device %d\n", addr, dest_id);
261        }
262        i++;
263    }
264
265    // Check if this matches the default range
266    if (dest_id == -1) {
267        for (iter = defaultRange.begin(); iter != defaultRange.end(); iter++) {
268            if (*iter == addr) {
269                DPRINTF(Bus, "  found addr %#llx on default\n", addr);
270                return defaultPort;
271            }
272        }
273
274        if (responderSet) {
275            panic("Unable to find destination for addr (user set default "
276                  "responder): %#llx", addr);
277        } else {
278            DPRINTF(Bus, "Unable to find destination for addr: %#llx, will use "
279                    "default port", addr);
280
281            return defaultPort;
282        }
283    }
284
285
286    // we shouldn't be sending this back to where it came from
287    assert(dest_id != id);
288
289    return interfaces[dest_id];
290}
291
292std::vector<int>
293Bus::findSnoopPorts(Addr addr, int id)
294{
295    int i = 0;
296    AddrRangeIter iter;
297    std::vector<int> ports;
298
299    while (i < portSnoopList.size())
300    {
301        if (portSnoopList[i].range == addr && portSnoopList[i].portId != id) {
302            //Careful  to not overlap ranges
303            //or snoop will be called more than once on the port
304            ports.push_back(portSnoopList[i].portId);
305//            DPRINTF(Bus, "  found snoop addr %#llx on device%d\n", addr,
306//                    portSnoopList[i].portId);
307        }
308        i++;
309    }
310    return ports;
311}
312
313Tick
314Bus::atomicSnoop(PacketPtr pkt)
315{
316    std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
317    Tick response_time = 0;
318
319    while (!ports.empty())
320    {
321        Tick response = interfaces[ports.back()]->sendAtomic(pkt);
322        if (response) {
323            assert(!response_time);  //Multiple responders
324            response_time = response;
325        }
326        ports.pop_back();
327    }
328    return response_time;
329}
330
331void
332Bus::functionalSnoop(PacketPtr pkt)
333{
334    std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
335
336    while (!ports.empty() && pkt->result != Packet::Success)
337    {
338        interfaces[ports.back()]->sendFunctional(pkt);
339        ports.pop_back();
340    }
341}
342
343bool
344Bus::timingSnoop(PacketPtr pkt)
345{
346    std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
347    bool success = true;
348
349    while (!ports.empty() && success)
350    {
351        success = interfaces[ports.back()]->sendTiming(pkt);
352        ports.pop_back();
353    }
354
355    return success;
356}
357
358
359/** Function called by the port when the bus is receiving a Atomic
360 * transaction.*/
361Tick
362Bus::recvAtomic(PacketPtr pkt)
363{
364    DPRINTF(Bus, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
365            pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
366    assert(pkt->getDest() == Packet::Broadcast);
367    Tick snoopTime = atomicSnoop(pkt);
368    if (snoopTime)
369        return snoopTime;  //Snoop satisfies it
370    else
371        return findPort(pkt->getAddr(), pkt->getSrc())->sendAtomic(pkt);
372}
373
374/** Function called by the port when the bus is receiving a Functional
375 * transaction.*/
376void
377Bus::recvFunctional(PacketPtr pkt)
378{
379    DPRINTF(Bus, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
380            pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
381    assert(pkt->getDest() == Packet::Broadcast);
382    functionalSnoop(pkt);
383
384    // If the snooping found what we were looking for, we're done.
385    if (pkt->result != Packet::Success)
386        findPort(pkt->getAddr(), pkt->getSrc())->sendFunctional(pkt);
387}
388
389/** Function called by the port when the bus is receiving a status change.*/
390void
391Bus::recvStatusChange(Port::Status status, int id)
392{
393    AddrRangeList ranges;
394    AddrRangeList snoops;
395    int x;
396    AddrRangeIter iter;
397
398    assert(status == Port::RangeChange &&
399           "The other statuses need to be implemented.");
400
401    DPRINTF(BusAddrRanges, "received RangeChange from device id %d\n", id);
402
403    if (id == defaultId) {
404        defaultRange.clear();
405        // Only try to update these ranges if the user set a default responder.
406        if (responderSet) {
407            defaultPort->getPeerAddressRanges(ranges, snoops);
408            assert(snoops.size() == 0);
409            for(iter = ranges.begin(); iter != ranges.end(); iter++) {
410                defaultRange.push_back(*iter);
411                DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for default range\n",
412                        iter->start, iter->end);
413            }
414        }
415    } else {
416
417        assert((id < interfaces.size() && id >= 0) || id == defaultId);
418        Port *port = interfaces[id];
419        std::vector<DevMap>::iterator portIter;
420        std::vector<DevMap>::iterator snoopIter;
421
422        // Clean out any previously existent ids
423        for (portIter = portList.begin(); portIter != portList.end(); ) {
424            if (portIter->portId == id)
425                portIter = portList.erase(portIter);
426            else
427                portIter++;
428        }
429
430        for (snoopIter = portSnoopList.begin(); snoopIter != portSnoopList.end(); ) {
431            if (snoopIter->portId == id)
432                snoopIter = portSnoopList.erase(snoopIter);
433            else
434                snoopIter++;
435        }
436
437        port->getPeerAddressRanges(ranges, snoops);
438
439        for(iter = snoops.begin(); iter != snoops.end(); iter++) {
440            DevMap dm;
441            dm.portId = id;
442            dm.range = *iter;
443
444            DPRINTF(BusAddrRanges, "Adding snoop range %#llx - %#llx for id %d\n",
445                    dm.range.start, dm.range.end, id);
446            portSnoopList.push_back(dm);
447        }
448
449        for(iter = ranges.begin(); iter != ranges.end(); iter++) {
450            DevMap dm;
451            dm.portId = id;
452            dm.range = *iter;
453
454            DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for id %d\n",
455                    dm.range.start, dm.range.end, id);
456            portList.push_back(dm);
457        }
458    }
459    DPRINTF(MMU, "port list has %d entries\n", portList.size());
460
461    // tell all our peers that our address range has changed.
462    // Don't tell the device that caused this change, it already knows
463    for (x = 0; x < interfaces.size(); x++)
464        if (x != id)
465            interfaces[x]->sendStatusChange(Port::RangeChange);
466
467    if (id != defaultId && defaultPort)
468        defaultPort->sendStatusChange(Port::RangeChange);
469}
470
471void
472Bus::addressRanges(AddrRangeList &resp, AddrRangeList &snoop, int id)
473{
474    std::vector<DevMap>::iterator portIter;
475    AddrRangeIter dflt_iter;
476    bool subset;
477
478    resp.clear();
479    snoop.clear();
480
481    DPRINTF(BusAddrRanges, "received address range request, returning:\n");
482
483    for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
484            dflt_iter++) {
485        resp.push_back(*dflt_iter);
486        DPRINTF(BusAddrRanges, "  -- %#llx : %#llx\n",dflt_iter->start,
487                dflt_iter->end);
488    }
489    for (portIter = portList.begin(); portIter != portList.end(); portIter++) {
490        subset = false;
491        for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
492                dflt_iter++) {
493            if ((portIter->range.start < dflt_iter->start &&
494                portIter->range.end >= dflt_iter->start) ||
495               (portIter->range.start < dflt_iter->end &&
496                portIter->range.end >= dflt_iter->end))
497                fatal("Devices can not set ranges that itersect the default set\
498                        but are not a subset of the default set.\n");
499            if (portIter->range.start >= dflt_iter->start &&
500                portIter->range.end <= dflt_iter->end) {
501                subset = true;
502                DPRINTF(BusAddrRanges, "  -- %#llx : %#llx is a SUBSET\n",
503                    portIter->range.start, portIter->range.end);
504            }
505        }
506        if (portIter->portId != id && !subset) {
507            resp.push_back(portIter->range);
508            DPRINTF(BusAddrRanges, "  -- %#llx : %#llx\n",
509                    portIter->range.start, portIter->range.end);
510        }
511    }
512}
513
514BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus)
515
516    Param<int> bus_id;
517    Param<int> clock;
518    Param<int> width;
519    Param<bool> responder_set;
520
521END_DECLARE_SIM_OBJECT_PARAMS(Bus)
522
523BEGIN_INIT_SIM_OBJECT_PARAMS(Bus)
524    INIT_PARAM(bus_id, "a globally unique bus id"),
525    INIT_PARAM(clock, "bus clock speed"),
526    INIT_PARAM(width, "width of the bus (bits)"),
527    INIT_PARAM(responder_set, "Is a default responder set by the user")
528END_INIT_SIM_OBJECT_PARAMS(Bus)
529
530CREATE_SIM_OBJECT(Bus)
531{
532    return new Bus(getInstanceName(), bus_id, clock, width, responder_set);
533}
534
535REGISTER_SIM_OBJECT("Bus", Bus)
536