bridge.cc revision 7823:dac01f14f20f
1
2/*
3 * Copyright (c) 2006 The Regents of The University of Michigan
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Ali Saidi
30 *          Steve Reinhardt
31 */
32
33/**
34 * @file
35 * Definition of a simple bus bridge without buffering.
36 */
37
38#include <algorithm>
39
40#include "base/range_ops.hh"
41#include "base/trace.hh"
42#include "mem/bridge.hh"
43#include "params/Bridge.hh"
44
45Bridge::BridgePort::BridgePort(const std::string &_name,
46                               Bridge *_bridge, BridgePort *_otherPort,
47                               int _delay, int _nack_delay, int _req_limit,
48                               int _resp_limit,
49                               std::vector<Range<Addr> > filter_ranges)
50    : Port(_name, _bridge), bridge(_bridge), otherPort(_otherPort),
51      delay(_delay), nackDelay(_nack_delay), filterRanges(filter_ranges),
52      outstandingResponses(0), queuedRequests(0), inRetry(false),
53      reqQueueLimit(_req_limit), respQueueLimit(_resp_limit), sendEvent(this)
54{
55}
56
57Bridge::Bridge(Params *p)
58    : MemObject(p),
59      portA(p->name + "-portA", this, &portB, p->delay, p->nack_delay,
60              p->req_size_a, p->resp_size_a, p->filter_ranges_a),
61      portB(p->name + "-portB", this, &portA, p->delay, p->nack_delay,
62              p->req_size_b, p->resp_size_b, p->filter_ranges_b),
63      ackWrites(p->write_ack), _params(p)
64{
65    if (ackWrites)
66        panic("No support for acknowledging writes\n");
67}
68
69Port *
70Bridge::getPort(const std::string &if_name, int idx)
71{
72    BridgePort *port;
73
74    if (if_name == "side_a")
75        port = &portA;
76    else if (if_name == "side_b")
77        port = &portB;
78    else
79        return NULL;
80
81    if (port->getPeer() != NULL && !port->getPeer()->isDefaultPort())
82        panic("bridge side %s already connected to %s.",
83                if_name, port->getPeer()->name());
84    return port;
85}
86
87
88void
89Bridge::init()
90{
91    // Make sure that both sides are connected to.
92    if (!portA.isConnected() || !portB.isConnected())
93        fatal("Both ports of bus bridge are not connected to a bus.\n");
94
95    if (portA.peerBlockSize() != portB.peerBlockSize())
96        fatal("port A size %d, port B size %d \n " \
97              "Busses don't have the same block size... Not supported.\n",
98              portA.peerBlockSize(), portB.peerBlockSize());
99}
100
101bool
102Bridge::BridgePort::respQueueFull()
103{
104    assert(outstandingResponses >= 0 && outstandingResponses <= respQueueLimit);
105    return outstandingResponses >= respQueueLimit;
106}
107
108bool
109Bridge::BridgePort::reqQueueFull()
110{
111    assert(queuedRequests >= 0 && queuedRequests <= reqQueueLimit);
112    return queuedRequests >= reqQueueLimit;
113}
114
115/** Function called by the port when the bus is receiving a Timing
116 * transaction.*/
117bool
118Bridge::BridgePort::recvTiming(PacketPtr pkt)
119{
120    DPRINTF(BusBridge, "recvTiming: src %d dest %d addr 0x%x\n",
121                pkt->getSrc(), pkt->getDest(), pkt->getAddr());
122
123    DPRINTF(BusBridge, "Local queue size: %d outreq: %d outresp: %d\n",
124                    sendQueue.size(), queuedRequests, outstandingResponses);
125    DPRINTF(BusBridge, "Remote queue size: %d outreq: %d outresp: %d\n",
126                    otherPort->sendQueue.size(), otherPort->queuedRequests,
127                    otherPort->outstandingResponses);
128
129    if (pkt->isRequest() && otherPort->reqQueueFull()) {
130        DPRINTF(BusBridge, "Remote queue full, nacking\n");
131        nackRequest(pkt);
132        return true;
133    }
134
135    if (pkt->needsResponse()) {
136        if (respQueueFull()) {
137            DPRINTF(BusBridge, "Local queue full, no space for response, nacking\n");
138            DPRINTF(BusBridge, "queue size: %d outreq: %d outstanding resp: %d\n",
139                    sendQueue.size(), queuedRequests, outstandingResponses);
140            nackRequest(pkt);
141            return true;
142        } else {
143            DPRINTF(BusBridge, "Request Needs response, reserving space\n");
144            ++outstandingResponses;
145        }
146    }
147
148    otherPort->queueForSendTiming(pkt);
149
150    return true;
151}
152
153void
154Bridge::BridgePort::nackRequest(PacketPtr pkt)
155{
156    // Nack the packet
157    pkt->makeTimingResponse();
158    pkt->setNacked();
159
160    //put it on the list to send
161    Tick readyTime = curTick() + nackDelay;
162    PacketBuffer *buf = new PacketBuffer(pkt, readyTime, true);
163
164    // nothing on the list, add it and we're done
165    if (sendQueue.empty()) {
166        assert(!sendEvent.scheduled());
167        schedule(sendEvent, readyTime);
168        sendQueue.push_back(buf);
169        return;
170    }
171
172    assert(sendEvent.scheduled() || inRetry);
173
174    // does it go at the end?
175    if (readyTime >= sendQueue.back()->ready) {
176        sendQueue.push_back(buf);
177        return;
178    }
179
180    // ok, somewhere in the middle, fun
181    std::list<PacketBuffer*>::iterator i = sendQueue.begin();
182    std::list<PacketBuffer*>::iterator end = sendQueue.end();
183    std::list<PacketBuffer*>::iterator begin = sendQueue.begin();
184    bool done = false;
185
186    while (i != end && !done) {
187        if (readyTime < (*i)->ready) {
188            if (i == begin)
189                reschedule(sendEvent, readyTime);
190            sendQueue.insert(i,buf);
191            done = true;
192        }
193        i++;
194    }
195    assert(done);
196}
197
198
199void
200Bridge::BridgePort::queueForSendTiming(PacketPtr pkt)
201{
202    if (pkt->isResponse()) {
203        // This is a response for a request we forwarded earlier.  The
204        // corresponding PacketBuffer should be stored in the packet's
205        // senderState field.
206
207        PacketBuffer *buf = dynamic_cast<PacketBuffer*>(pkt->senderState);
208        assert(buf != NULL);
209        // set up new packet dest & senderState based on values saved
210        // from original request
211        buf->fixResponse(pkt);
212
213        DPRINTF(BusBridge, "response, new dest %d\n", pkt->getDest());
214        delete buf;
215    }
216
217
218    if (pkt->isRequest()) {
219        ++queuedRequests;
220    }
221
222
223
224    Tick readyTime = curTick() + delay;
225    PacketBuffer *buf = new PacketBuffer(pkt, readyTime);
226
227    // If we're about to put this packet at the head of the queue, we
228    // need to schedule an event to do the transmit.  Otherwise there
229    // should already be an event scheduled for sending the head
230    // packet.
231    if (sendQueue.empty()) {
232        schedule(sendEvent, readyTime);
233    }
234    sendQueue.push_back(buf);
235}
236
237void
238Bridge::BridgePort::trySend()
239{
240    assert(!sendQueue.empty());
241
242    PacketBuffer *buf = sendQueue.front();
243
244    assert(buf->ready <= curTick());
245
246    PacketPtr pkt = buf->pkt;
247
248    DPRINTF(BusBridge, "trySend: origSrc %d dest %d addr 0x%x\n",
249            buf->origSrc, pkt->getDest(), pkt->getAddr());
250
251    bool wasReq = pkt->isRequest();
252    bool was_nacked_here = buf->nackedHere;
253
254    // If the send was successful, make sure sender state was set to NULL
255    // otherwise we could get a NACK back of a packet that didn't expect a
256    // response and we would try to use freed memory.
257
258    Packet::SenderState *old_sender_state = pkt->senderState;
259    if (pkt->isRequest() && !buf->expectResponse)
260        pkt->senderState = NULL;
261
262    if (sendTiming(pkt)) {
263        // send successful
264        sendQueue.pop_front();
265        buf->pkt = NULL; // we no longer own packet, so it's not safe to look at it
266
267        if (buf->expectResponse) {
268            // Must wait for response
269            DPRINTF(BusBridge, "  successful: awaiting response (%d)\n",
270                    outstandingResponses);
271        } else {
272            // no response expected... deallocate packet buffer now.
273            DPRINTF(BusBridge, "  successful: no response expected\n");
274            delete buf;
275        }
276
277        if (wasReq)
278            --queuedRequests;
279        else if (!was_nacked_here)
280            --outstandingResponses;
281
282        // If there are more packets to send, schedule event to try again.
283        if (!sendQueue.empty()) {
284            buf = sendQueue.front();
285            DPRINTF(BusBridge, "Scheduling next send\n");
286            schedule(sendEvent, std::max(buf->ready, curTick() + 1));
287        }
288    } else {
289        DPRINTF(BusBridge, "  unsuccessful\n");
290        pkt->senderState = old_sender_state;
291        inRetry = true;
292    }
293
294    DPRINTF(BusBridge, "trySend: queue size: %d outreq: %d outstanding resp: %d\n",
295                    sendQueue.size(), queuedRequests, outstandingResponses);
296}
297
298
299void
300Bridge::BridgePort::recvRetry()
301{
302    inRetry = false;
303    Tick nextReady = sendQueue.front()->ready;
304    if (nextReady <= curTick())
305        trySend();
306    else
307        schedule(sendEvent, nextReady);
308}
309
310/** Function called by the port when the bus is receiving a Atomic
311 * transaction.*/
312Tick
313Bridge::BridgePort::recvAtomic(PacketPtr pkt)
314{
315    return delay + otherPort->sendAtomic(pkt);
316}
317
318/** Function called by the port when the bus is receiving a Functional
319 * transaction.*/
320void
321Bridge::BridgePort::recvFunctional(PacketPtr pkt)
322{
323    std::list<PacketBuffer*>::iterator i;
324
325    pkt->pushLabel(name());
326
327    for (i = sendQueue.begin();  i != sendQueue.end(); ++i) {
328        if (pkt->checkFunctional((*i)->pkt)) {
329            pkt->makeResponse();
330            return;
331        }
332    }
333
334    pkt->popLabel();
335
336    // fall through if pkt still not satisfied
337    otherPort->sendFunctional(pkt);
338}
339
340/** Function called by the port when the bus is receiving a status change.*/
341void
342Bridge::BridgePort::recvStatusChange(Port::Status status)
343{
344    otherPort->sendStatusChange(status);
345}
346
347void
348Bridge::BridgePort::getDeviceAddressRanges(AddrRangeList &resp,
349                                           bool &snoop)
350{
351    otherPort->getPeerAddressRanges(resp, snoop);
352    FilterRangeList(filterRanges, resp);
353    // we don't allow snooping across bridges
354    snoop = false;
355}
356
357Bridge *
358BridgeParams::create()
359{
360    return new Bridge(this);
361}
362