coherent_xbar.cc revision 2982
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
70
71/** Function called by the port when the bus is receiving a Timing
72 * transaction.*/
73bool
74Bus::recvTiming(Packet *pkt)
75{
76    Port *port;
77    DPRINTF(Bus, "recvTiming: packet src %d dest %d addr 0x%x cmd %s\n",
78            pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
79
80    short dest = pkt->getDest();
81    if (dest == Packet::Broadcast) {
82        port = findPort(pkt->getAddr(), pkt->getSrc());
83    } else {
84        assert(dest >= 0 && dest < interfaces.size());
85        assert(dest != pkt->getSrc()); // catch infinite loops
86        port = interfaces[dest];
87    }
88    if (port->sendTiming(pkt))  {
89        // packet was successfully sent, just return true.
90        return true;
91    }
92
93    // packet not successfully sent
94    retryList.push_back(interfaces[pkt->getSrc()]);
95    return false;
96}
97
98void
99Bus::recvRetry(int id)
100{
101    // Go through all the elements on the list calling sendRetry on each
102    // This is not very efficient at all but it works. Ultimately we should end
103    // up with something that is more intelligent.
104    int initialSize = retryList.size();
105    int i;
106    Port *p;
107
108    for (i = 0; i < initialSize; i++) {
109        assert(retryList.size() > 0);
110        p = retryList.front();
111        retryList.pop_front();
112        p->sendRetry();
113    }
114}
115
116
117Port *
118Bus::findPort(Addr addr, int id)
119{
120    /* An interval tree would be a better way to do this. --ali. */
121    int dest_id = -1;
122    int i = 0;
123    bool found = false;
124    AddrRangeIter iter;
125
126    while (i < portList.size() && !found)
127    {
128        if (portList[i].range == addr) {
129            dest_id = portList[i].portId;
130            found = true;
131            DPRINTF(Bus, "  found addr 0x%llx on device %d\n", addr, dest_id);
132        }
133        i++;
134    }
135
136    // Check if this matches the default range
137    if (dest_id == -1) {
138        for (iter = defaultRange.begin(); iter != defaultRange.end(); iter++) {
139            if (*iter == addr) {
140                DPRINTF(Bus, "  found addr 0x%llx on default\n", addr);
141                return defaultPort;
142            }
143        }
144        panic("Unable to find destination for addr: %llx", addr);
145    }
146
147
148    // we shouldn't be sending this back to where it came from
149    assert(dest_id != id);
150
151    return interfaces[dest_id];
152}
153
154/** Function called by the port when the bus is receiving a Atomic
155 * transaction.*/
156Tick
157Bus::recvAtomic(Packet *pkt)
158{
159    DPRINTF(Bus, "recvAtomic: packet src %d dest %d addr 0x%x cmd %s\n",
160            pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
161    assert(pkt->getDest() == Packet::Broadcast);
162    return findPort(pkt->getAddr(), pkt->getSrc())->sendAtomic(pkt);
163}
164
165/** Function called by the port when the bus is receiving a Functional
166 * transaction.*/
167void
168Bus::recvFunctional(Packet *pkt)
169{
170    DPRINTF(Bus, "recvFunctional: packet src %d dest %d addr 0x%x cmd %s\n",
171            pkt->getSrc(), pkt->getDest(), pkt->getAddr(), pkt->cmdString());
172    assert(pkt->getDest() == Packet::Broadcast);
173    findPort(pkt->getAddr(), pkt->getSrc())->sendFunctional(pkt);
174}
175
176/** Function called by the port when the bus is receiving a status change.*/
177void
178Bus::recvStatusChange(Port::Status status, int id)
179{
180    AddrRangeList ranges;
181    AddrRangeList snoops;
182    int x;
183    AddrRangeIter iter;
184
185    assert(status == Port::RangeChange &&
186           "The other statuses need to be implemented.");
187
188    DPRINTF(BusAddrRanges, "received RangeChange from device id %d\n", id);
189
190    if (id == defaultId) {
191        defaultRange.clear();
192        defaultPort->getPeerAddressRanges(ranges, snoops);
193        assert(snoops.size() == 0);
194        for(iter = ranges.begin(); iter != ranges.end(); iter++) {
195            defaultRange.push_back(*iter);
196            DPRINTF(BusAddrRanges, "Adding range %llx - %llx for default\n",
197                    iter->start, iter->end);
198        }
199    } else {
200
201        assert((id < interfaces.size() && id >= 0) || id == -1);
202        Port *port = interfaces[id];
203        std::vector<DevMap>::iterator portIter;
204
205        // Clean out any previously existent ids
206        for (portIter = portList.begin(); portIter != portList.end(); ) {
207            if (portIter->portId == id)
208                portIter = portList.erase(portIter);
209            else
210                portIter++;
211        }
212
213        port->getPeerAddressRanges(ranges, snoops);
214
215        // not dealing with snooping yet either
216        assert(snoops.size() == 0);
217        for(iter = ranges.begin(); iter != ranges.end(); iter++) {
218            DevMap dm;
219            dm.portId = id;
220            dm.range = *iter;
221
222            DPRINTF(BusAddrRanges, "Adding range %llx - %llx for id %d\n",
223                    dm.range.start, dm.range.end, id);
224            portList.push_back(dm);
225        }
226    }
227    DPRINTF(MMU, "port list has %d entries\n", portList.size());
228
229    // tell all our peers that our address range has changed.
230    // Don't tell the device that caused this change, it already knows
231    for (x = 0; x < interfaces.size(); x++)
232        if (x != id)
233            interfaces[x]->sendStatusChange(Port::RangeChange);
234
235    if (id != defaultId && defaultPort)
236        defaultPort->sendStatusChange(Port::RangeChange);
237}
238
239void
240Bus::addressRanges(AddrRangeList &resp, AddrRangeList &snoop, int id)
241{
242    std::vector<DevMap>::iterator portIter;
243    AddrRangeIter dflt_iter;
244    bool subset;
245
246    resp.clear();
247    snoop.clear();
248
249    DPRINTF(BusAddrRanges, "received address range request, returning:\n");
250
251    for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
252            dflt_iter++) {
253        resp.push_back(*dflt_iter);
254        DPRINTF(BusAddrRanges, "  -- %#llX : %#llX\n",dflt_iter->start,
255                dflt_iter->end);
256    }
257    for (portIter = portList.begin(); portIter != portList.end(); portIter++) {
258        subset = false;
259        for (dflt_iter = defaultRange.begin(); dflt_iter != defaultRange.end();
260                dflt_iter++) {
261            if ((portIter->range.start < dflt_iter->start &&
262                portIter->range.end >= dflt_iter->start) ||
263               (portIter->range.start < dflt_iter->end &&
264                portIter->range.end >= dflt_iter->end))
265                fatal("Devices can not set ranges that itersect the default set\
266                        but are not a subset of the default set.\n");
267            if (portIter->range.start >= dflt_iter->start &&
268                portIter->range.end <= dflt_iter->end) {
269                subset = true;
270                DPRINTF(BusAddrRanges, "  -- %#llX : %#llX is a SUBSET\n",
271                    portIter->range.start, portIter->range.end);
272            }
273        }
274        if (portIter->portId != id && !subset) {
275            resp.push_back(portIter->range);
276            DPRINTF(BusAddrRanges, "  -- %#llX : %#llX\n",
277                    portIter->range.start, portIter->range.end);
278        }
279    }
280}
281
282BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bus)
283
284    Param<int> bus_id;
285
286END_DECLARE_SIM_OBJECT_PARAMS(Bus)
287
288BEGIN_INIT_SIM_OBJECT_PARAMS(Bus)
289    INIT_PARAM(bus_id, "a globally unique bus id")
290END_INIT_SIM_OBJECT_PARAMS(Bus)
291
292CREATE_SIM_OBJECT(Bus)
293{
294    return new Bus(getInstanceName(), bus_id);
295}
296
297REGISTER_SIM_OBJECT("Bus", Bus)
298