coherent_xbar.cc revision 3244
12373SN/A/*
22373SN/A * Copyright (c) 2006 The Regents of The University of Michigan
32373SN/A * All rights reserved.
42373SN/A *
52373SN/A * Redistribution and use in source and binary forms, with or without
62373SN/A * modification, are permitted provided that the following conditions are
72373SN/A * met: redistributions of source code must retain the above copyright
82373SN/A * notice, this list of conditions and the following disclaimer;
92373SN/A * redistributions in binary form must reproduce the above copyright
102373SN/A * notice, this list of conditions and the following disclaimer in the
112373SN/A * documentation and/or other materials provided with the distribution;
122373SN/A * neither the name of the copyright holders nor the names of its
132373SN/A * contributors may be used to endorse or promote products derived from
142373SN/A * this software without specific prior written permission.
152373SN/A *
162373SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172373SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182373SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192373SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202373SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212373SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222373SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232373SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242373SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252373SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262373SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Ali Saidi
292373SN/A */
302373SN/A
312373SN/A/**
322373SN/A * @file
332373SN/A * Definition of a bus object.
342373SN/A */
352373SN/A
362373SN/A
372373SN/A#include "base/misc.hh"
382373SN/A#include "base/trace.hh"
392373SN/A#include "mem/bus.hh"
406227Snate@binkert.org#include "sim/builder.hh"
412373SN/A
426227Snate@binkert.orgPort *
432373SN/ABus::getPort(const std::string &if_name, int idx)
442373SN/A{
452373SN/A    if (if_name == "default")
462373SN/A        if (defaultPort == NULL) {
472373SN/A            defaultPort = new BusPort(csprintf("%s-default",name()), this,
482373SN/A                    defaultId);
492373SN/A            return defaultPort;
502373SN/A        } else
512373SN/A            fatal("Default port already set\n");
522373SN/A
532373SN/A    // if_name ignored?  forced to be empty?
542373SN/A    int id = interfaces.size();
552373SN/A    BusPort *bp = new BusPort(csprintf("%s-p%d", name(), id), this, id);
562373SN/A    interfaces.push_back(bp);
572373SN/A    return bp;
582373SN/A}
592373SN/A
602373SN/A/** Get the ranges of anyone other buses that we are connected to. */
612373SN/Avoid
622373SN/ABus::init()
632373SN/A{
642373SN/A    std::vector<BusPort*>::iterator intIter;
656227Snate@binkert.org
662373SN/A    for (intIter = interfaces.begin(); intIter != interfaces.end(); intIter++)
676227Snate@binkert.org        (*intIter)->sendStatusChange(Port::RangeChange);
682565SN/A}
692565SN/A
702373SN/ABus::BusFreeEvent::BusFreeEvent(Bus *_bus) : Event(&mainEventQueue), bus(_bus)
716227Snate@binkert.org{}
722373SN/A
732373SN/Avoid Bus::BusFreeEvent::process()
742373SN/A{
752373SN/A    bus->recvRetry(-1);
762982Sstever@eecs.umich.edu}
772373SN/A
782373SN/Aconst char * Bus::BusFreeEvent::description()
792373SN/A{
802373SN/A    return "bus became available";
816227Snate@binkert.org}
822565SN/A
832373SN/Avoid Bus::occupyBus(PacketPtr pkt)
842373SN/A{
852418SN/A    //Bring tickNextIdle up to the present tick
865731SSteve.Reinhardt@amd.com    //There is some potential ambiguity where a cycle starts, which might make
872373SN/A    //a difference when devices are acting right around a cycle boundary. Using
882373SN/A    //a < allows things which happen exactly on a cycle boundary to take up only
892373SN/A    //the following cycle. Anthing that happens later will have to "wait" for
902373SN/A    //the end of that cycle, and then start using the bus after that.
912415SN/A    while (tickNextIdle < curTick)
922415SN/A        tickNextIdle += clock;
932415SN/A
942415SN/A    // The packet will be sent. Figure out how long it occupies the bus, and
952415SN/A    // how much of that time is for the first "word", aka bus width.
962415SN/A    int numCycles = 0;
972415SN/A    // Requests need one cycle to send an address
982415SN/A    if (pkt->isRequest())
992415SN/A        numCycles++;
1002415SN/A    else if (pkt->isResponse() || pkt->hasData()) {
1012415SN/A        // If a packet has data, it needs ceil(size/width) cycles to send it
1022415SN/A        // We're using the "adding instead of dividing" trick again here
1032373SN/A        if (pkt->hasData()) {
1042373SN/A            int dataSize = pkt->getSize();
1052373SN/A            for (int transmitted = 0; transmitted < dataSize;
1066227Snate@binkert.org                    transmitted += width) {
1072373SN/A                numCycles++;
1082373SN/A            }
1092373SN/A        } else {
1102373SN/A            // If the packet didn't have data, it must have been a response.
1112373SN/A            // Those use the bus for one cycle to send their data.
1126227Snate@binkert.org            numCycles++;
1132373SN/A        }
1146227Snate@binkert.org    }
1152373SN/A
1162565SN/A    // The first word will be delivered after the current tick, the delivery
1176227Snate@binkert.org    // of the address if any, and one bus cycle to deliver the data
1186227Snate@binkert.org    pkt->firstWordTime =
1192373SN/A        tickNextIdle +
1202373SN/A        pkt->isRequest() ? clock : 0 +
1212373SN/A        clock;
1222373SN/A
1232373SN/A    //Advance it numCycles bus cycles.
1246227Snate@binkert.org    //XXX Should this use the repeated addition trick as well?
1252373SN/A    tickNextIdle += (numCycles * clock);
1262373SN/A    if (!busIdle.scheduled()) {
1272373SN/A        busIdle.schedule(tickNextIdle);
1282373SN/A    } else {
1292373SN/A        busIdle.reschedule(tickNextIdle);
1302373SN/A    }
1316227Snate@binkert.org    DPRINTF(Bus, "The bus is now occupied from tick %d to %d\n",
1326227Snate@binkert.org            curTick, tickNextIdle);
1332373SN/A
1342373SN/A    // The bus will become idle once the current packet is delivered.
1352373SN/A    pkt->finishTime = tickNextIdle;
1362373SN/A}
1372373SN/A
1382373SN/A/** Function called by the port when the bus is receiving a Timing
1392373SN/A * transaction.*/
1402373SN/Abool
1412373SN/ABus::recvTiming(Packet *pkt)
1422373SN/A{
1432373SN/A    Port *port;
1442373SN/A    DPRINTF(Bus, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
1452373SN/A            pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
1462373SN/A
1472373SN/A    BusPort *pktPort = interfaces[pkt->getSrc()];
148
149    // If the bus is busy, or other devices are in line ahead of the current
150    // one, put this device on the retry list.
151    if (tickNextIdle > curTick ||
152            (retryList.size() && (!inRetry || pktPort != retryList.front()))) {
153        addToRetryList(pktPort);
154        return false;
155    }
156
157    short dest = pkt->getDest();
158    if (dest == Packet::Broadcast) {
159        if (timingSnoop(pkt)) {
160            pkt->flags |= SNOOP_COMMIT;
161            bool success = timingSnoop(pkt);
162            assert(success);
163            if (pkt->flags & SATISFIED) {
164                //Cache-Cache transfer occuring
165                if (inRetry) {
166                    retryList.front()->onRetryList(false);
167                    retryList.pop_front();
168                    inRetry = false;
169                }
170                occupyBus(pkt);
171                return true;
172            }
173            port = findPort(pkt->getAddr(), pkt->getSrc());
174        } else {
175            //Snoop didn't succeed
176            addToRetryList(pktPort);
177            return false;
178        }
179    } else {
180        assert(dest >= 0 && dest < interfaces.size());
181        assert(dest != pkt->getSrc()); // catch infinite loops
182        port = interfaces[dest];
183    }
184
185    occupyBus(pkt);
186
187    if (port->sendTiming(pkt))  {
188        // Packet was successfully sent. Return true.
189        // Also take care of retries
190        if (inRetry) {
191            retryList.front()->onRetryList(false);
192            retryList.pop_front();
193            inRetry = false;
194        }
195        return true;
196    }
197
198    // Packet not successfully sent. Leave or put it on the retry list.
199    addToRetryList(pktPort);
200    return false;
201}
202
203void
204Bus::recvRetry(int id)
205{
206    // If there's anything waiting...
207    if (retryList.size()) {
208        //retryingPort = retryList.front();
209        inRetry = true;
210        retryList.front()->sendRetry();
211        // If inRetry is still true, sendTiming wasn't called
212        if (inRetry)
213            panic("Port %s didn't call sendTiming in it's recvRetry\n",\
214                    retryList.front()->getPeer()->name());
215        //assert(!inRetry);
216    }
217}
218
219Port *
220Bus::findPort(Addr addr, int id)
221{
222    /* An interval tree would be a better way to do this. --ali. */
223    int dest_id = -1;
224    int i = 0;
225    bool found = false;
226    AddrRangeIter iter;
227
228    while (i < portList.size() && !found)
229    {
230        if (portList[i].range == addr) {
231            dest_id = portList[i].portId;
232            found = true;
233            DPRINTF(Bus, "  found addr %#llx on device %d\n", addr, dest_id);
234        }
235        i++;
236    }
237
238    // Check if this matches the default range
239    if (dest_id == -1) {
240        for (iter = defaultRange.begin(); iter != defaultRange.end(); iter++) {
241            if (*iter == addr) {
242                DPRINTF(Bus, "  found addr %#llx on default\n", addr);
243                return defaultPort;
244            }
245        }
246        panic("Unable to find destination for addr: %#llx", addr);
247    }
248
249
250    // we shouldn't be sending this back to where it came from
251    assert(dest_id != id);
252
253    return interfaces[dest_id];
254}
255
256std::vector<int>
257Bus::findSnoopPorts(Addr addr, int id)
258{
259    int i = 0;
260    AddrRangeIter iter;
261    std::vector<int> ports;
262
263    while (i < portSnoopList.size())
264    {
265        if (portSnoopList[i].range == addr && portSnoopList[i].portId != id) {
266            //Careful  to not overlap ranges
267            //or snoop will be called more than once on the port
268            ports.push_back(portSnoopList[i].portId);
269            DPRINTF(Bus, "  found snoop addr %#llx on device%d\n", addr,
270                    portSnoopList[i].portId);
271        }
272        i++;
273    }
274    return ports;
275}
276
277void
278Bus::atomicSnoop(Packet *pkt)
279{
280    std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
281
282    while (!ports.empty())
283    {
284        interfaces[ports.back()]->sendAtomic(pkt);
285        ports.pop_back();
286    }
287}
288
289void
290Bus::functionalSnoop(Packet *pkt)
291{
292    std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
293
294    while (!ports.empty())
295    {
296        interfaces[ports.back()]->sendFunctional(pkt);
297        ports.pop_back();
298    }
299}
300
301bool
302Bus::timingSnoop(Packet *pkt)
303{
304    std::vector<int> ports = findSnoopPorts(pkt->getAddr(), pkt->getSrc());
305    bool success = true;
306
307    while (!ports.empty() && success)
308    {
309        success = interfaces[ports.back()]->sendTiming(pkt);
310        ports.pop_back();
311    }
312
313    return success;
314}
315
316
317/** Function called by the port when the bus is receiving a Atomic
318 * transaction.*/
319Tick
320Bus::recvAtomic(Packet *pkt)
321{
322    DPRINTF(Bus, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
323            pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
324    assert(pkt->getDest() == Packet::Broadcast);
325    atomicSnoop(pkt);
326    return findPort(pkt->getAddr(), pkt->getSrc())->sendAtomic(pkt);
327}
328
329/** Function called by the port when the bus is receiving a Functional
330 * transaction.*/
331void
332Bus::recvFunctional(Packet *pkt)
333{
334    DPRINTF(Bus, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
335            pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
336    assert(pkt->getDest() == Packet::Broadcast);
337    functionalSnoop(pkt);
338    findPort(pkt->getAddr(), pkt->getSrc())->sendFunctional(pkt);
339}
340
341/** Function called by the port when the bus is receiving a status change.*/
342void
343Bus::recvStatusChange(Port::Status status, int id)
344{
345    AddrRangeList ranges;
346    AddrRangeList snoops;
347    int x;
348    AddrRangeIter iter;
349
350    assert(status == Port::RangeChange &&
351           "The other statuses need to be implemented.");
352
353    DPRINTF(BusAddrRanges, "received RangeChange from device id %d\n", id);
354
355    if (id == defaultId) {
356        defaultRange.clear();
357        defaultPort->getPeerAddressRanges(ranges, snoops);
358        assert(snoops.size() == 0);
359        for(iter = ranges.begin(); iter != ranges.end(); iter++) {
360            defaultRange.push_back(*iter);
361            DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for default range\n",
362                    iter->start, iter->end);
363        }
364    } else {
365
366        assert((id < interfaces.size() && id >= 0) || id == -1);
367        Port *port = interfaces[id];
368        std::vector<DevMap>::iterator portIter;
369        std::vector<DevMap>::iterator snoopIter;
370
371        // Clean out any previously existent ids
372        for (portIter = portList.begin(); portIter != portList.end(); ) {
373            if (portIter->portId == id)
374                portIter = portList.erase(portIter);
375            else
376                portIter++;
377        }
378
379        for (snoopIter = portSnoopList.begin(); snoopIter != portSnoopList.end(); ) {
380            if (snoopIter->portId == id)
381                snoopIter = portSnoopList.erase(snoopIter);
382            else
383                snoopIter++;
384        }
385
386        port->getPeerAddressRanges(ranges, snoops);
387
388        for(iter = snoops.begin(); iter != snoops.end(); iter++) {
389            DevMap dm;
390            dm.portId = id;
391            dm.range = *iter;
392
393            DPRINTF(BusAddrRanges, "Adding snoop range %#llx - %#llx for id %d\n",
394                    dm.range.start, dm.range.end, id);
395            portSnoopList.push_back(dm);
396        }
397
398        for(iter = ranges.begin(); iter != ranges.end(); iter++) {
399            DevMap dm;
400            dm.portId = id;
401            dm.range = *iter;
402
403            DPRINTF(BusAddrRanges, "Adding range %#llx - %#llx for id %d\n",
404                    dm.range.start, dm.range.end, id);
405            portList.push_back(dm);
406        }
407    }
408    DPRINTF(MMU, "port list has %d entries\n", portList.size());
409
410    // tell all our peers that our address range has changed.
411    // Don't tell the device that caused this change, it already knows
412    for (x = 0; x < interfaces.size(); x++)
413        if (x != id)
414            interfaces[x]->sendStatusChange(Port::RangeChange);
415
416    if (id != defaultId && defaultPort)
417        defaultPort->sendStatusChange(Port::RangeChange);
418}
419
420void
421Bus::addressRanges(AddrRangeList &resp, AddrRangeList &snoop, int id)
422{
423    std::vector<DevMap>::iterator portIter;
424    AddrRangeIter dflt_iter;
425    bool subset;
426
427    resp.clear();
428    snoop.clear();
429
430    DPRINTF(BusAddrRanges, "received address range request, returning:\n");
431
432    for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
433            dflt_iter++) {
434        resp.push_back(*dflt_iter);
435        DPRINTF(BusAddrRanges, "  -- %#llx : %#llx\n",dflt_iter->start,
436                dflt_iter->end);
437    }
438    for (portIter = portList.begin(); portIter != portList.end(); portIter++) {
439        subset = false;
440        for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
441                dflt_iter++) {
442            if ((portIter->range.start < dflt_iter->start &&
443                portIter->range.end >= dflt_iter->start) ||
444               (portIter->range.start < dflt_iter->end &&
445                portIter->range.end >= dflt_iter->end))
446                fatal("Devices can not set ranges that itersect the default set\
447                        but are not a subset of the default set.\n");
448            if (portIter->range.start >= dflt_iter->start &&
449                portIter->range.end <= dflt_iter->end) {
450                subset = true;
451                DPRINTF(BusAddrRanges, "  -- %#llx : %#llx is a SUBSET\n",
452                    portIter->range.start, portIter->range.end);
453            }
454        }
455        if (portIter->portId != id && !subset) {
456            resp.push_back(portIter->range);
457            DPRINTF(BusAddrRanges, "  -- %#llx : %#llx\n",
458                    portIter->range.start, portIter->range.end);
459        }
460    }
461}
462
463BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus)
464
465    Param<int> bus_id;
466    Param<int> clock;
467    Param<int> width;
468
469END_DECLARE_SIM_OBJECT_PARAMS(Bus)
470
471BEGIN_INIT_SIM_OBJECT_PARAMS(Bus)
472    INIT_PARAM(bus_id, "a globally unique bus id"),
473    INIT_PARAM(clock, "bus clock speed"),
474    INIT_PARAM(width, "width of the bus (bits)")
475END_INIT_SIM_OBJECT_PARAMS(Bus)
476
477CREATE_SIM_OBJECT(Bus)
478{
479    return new Bus(getInstanceName(), bus_id, clock, width);
480}
481
482REGISTER_SIM_OBJECT("Bus", Bus)
483