bridge.cc revision 4986:b7c82ad6b3ef
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), 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->name),
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)
82        panic("bridge side %s already connected to.", if_name);
83    return port;
84}
85
86
87void
88Bridge::init()
89{
90    // Make sure that both sides are connected to.
91    if (portA.getPeer() == NULL || portB.getPeer() == NULL)
92        fatal("Both ports of bus bridge are not connected to a bus.\n");
93
94    if (portA.peerBlockSize() != portB.peerBlockSize())
95        fatal("Busses don't have the same block size... Not supported.\n");
96}
97
98bool
99Bridge::BridgePort::respQueueFull()
100{
101    assert(outstandingResponses >= 0 && outstandingResponses <= respQueueLimit);
102    return outstandingResponses >= respQueueLimit;
103}
104
105bool
106Bridge::BridgePort::reqQueueFull()
107{
108    assert(queuedRequests >= 0 && queuedRequests <= reqQueueLimit);
109    return queuedRequests >= reqQueueLimit;
110}
111
112/** Function called by the port when the bus is receiving a Timing
113 * transaction.*/
114bool
115Bridge::BridgePort::recvTiming(PacketPtr pkt)
116{
117    DPRINTF(BusBridge, "recvTiming: src %d dest %d addr 0x%x\n",
118                pkt->getSrc(), pkt->getDest(), pkt->getAddr());
119
120    DPRINTF(BusBridge, "Local queue size: %d outreq: %d outresp: %d\n",
121                    sendQueue.size(), queuedRequests, outstandingResponses);
122    DPRINTF(BusBridge, "Remote queue size: %d outreq: %d outresp: %d\n",
123                    otherPort->sendQueue.size(), otherPort->queuedRequests,
124                    otherPort->outstandingResponses);
125
126    if (pkt->isRequest() && otherPort->reqQueueFull()) {
127        DPRINTF(BusBridge, "Remote queue full, nacking\n");
128        nackRequest(pkt);
129        return true;
130    }
131
132    if (pkt->needsResponse())
133        if (respQueueFull()) {
134            DPRINTF(BusBridge, "Local queue full, no space for response, nacking\n");
135            DPRINTF(BusBridge, "queue size: %d outreq: %d outstanding resp: %d\n",
136                    sendQueue.size(), queuedRequests, outstandingResponses);
137            nackRequest(pkt);
138            return true;
139        } else {
140            DPRINTF(BusBridge, "Request Needs response, reserving space\n");
141            ++outstandingResponses;
142        }
143
144    otherPort->queueForSendTiming(pkt);
145
146    return true;
147}
148
149void
150Bridge::BridgePort::nackRequest(PacketPtr pkt)
151{
152    // Nack the packet
153    pkt->makeTimingResponse();
154    pkt->setNacked();
155
156    //put it on the list to send
157    Tick readyTime = curTick + nackDelay;
158    PacketBuffer *buf = new PacketBuffer(pkt, readyTime, true);
159
160    // nothing on the list, add it and we're done
161    if (sendQueue.empty()) {
162        assert(!sendEvent.scheduled());
163        sendEvent.schedule(readyTime);
164        sendQueue.push_back(buf);
165        return;
166    }
167
168    assert(sendEvent.scheduled() || inRetry);
169
170    // does it go at the end?
171    if (readyTime >= sendQueue.back()->ready) {
172        sendQueue.push_back(buf);
173        return;
174    }
175
176    // ok, somewhere in the middle, fun
177    std::list<PacketBuffer*>::iterator i = sendQueue.begin();
178    std::list<PacketBuffer*>::iterator end = sendQueue.end();
179    std::list<PacketBuffer*>::iterator begin = sendQueue.begin();
180    bool done = false;
181
182    while (i != end && !done) {
183        if (readyTime < (*i)->ready) {
184            if (i == begin)
185                sendEvent.reschedule(readyTime);
186            sendQueue.insert(i,buf);
187            done = true;
188        }
189        i++;
190    }
191    assert(done);
192}
193
194
195void
196Bridge::BridgePort::queueForSendTiming(PacketPtr pkt)
197{
198    if (pkt->isResponse()) {
199        // This is a response for a request we forwarded earlier.  The
200        // corresponding PacketBuffer should be stored in the packet's
201        // senderState field.
202
203        PacketBuffer *buf = dynamic_cast<PacketBuffer*>(pkt->senderState);
204        assert(buf != NULL);
205        // set up new packet dest & senderState based on values saved
206        // from original request
207        buf->fixResponse(pkt);
208
209        DPRINTF(BusBridge, "response, new dest %d\n", pkt->getDest());
210        delete buf;
211    }
212
213
214    if (pkt->isRequest()) {
215        ++queuedRequests;
216    }
217
218
219
220    Tick readyTime = curTick + delay;
221    PacketBuffer *buf = new PacketBuffer(pkt, readyTime);
222
223    // If we're about to put this packet at the head of the queue, we
224    // need to schedule an event to do the transmit.  Otherwise there
225    // should already be an event scheduled for sending the head
226    // packet.
227    if (sendQueue.empty()) {
228        sendEvent.schedule(readyTime);
229    }
230    sendQueue.push_back(buf);
231}
232
233void
234Bridge::BridgePort::trySend()
235{
236    assert(!sendQueue.empty());
237
238    PacketBuffer *buf = sendQueue.front();
239
240    assert(buf->ready <= curTick);
241
242    PacketPtr pkt = buf->pkt;
243
244    DPRINTF(BusBridge, "trySend: origSrc %d dest %d addr 0x%x\n",
245            buf->origSrc, pkt->getDest(), pkt->getAddr());
246
247    bool wasReq = pkt->isRequest();
248    bool was_nacked_here = buf->nackedHere;
249
250    // If the send was successful, make sure sender state was set to NULL
251    // otherwise we could get a NACK back of a packet that didn't expect a
252    // response and we would try to use freed memory.
253
254    Packet::SenderState *old_sender_state = pkt->senderState;
255    if (pkt->isRequest() && !buf->expectResponse)
256        pkt->senderState = NULL;
257
258    if (sendTiming(pkt)) {
259        // send successful
260        sendQueue.pop_front();
261        buf->pkt = NULL; // we no longer own packet, so it's not safe to look at it
262
263        if (buf->expectResponse) {
264            // Must wait for response
265            DPRINTF(BusBridge, "  successful: awaiting response (%d)\n",
266                    outstandingResponses);
267        } else {
268            // no response expected... deallocate packet buffer now.
269            DPRINTF(BusBridge, "  successful: no response expected\n");
270            delete buf;
271        }
272
273        if (wasReq)
274            --queuedRequests;
275        else if (!was_nacked_here)
276            --outstandingResponses;
277
278        // If there are more packets to send, schedule event to try again.
279        if (!sendQueue.empty()) {
280            buf = sendQueue.front();
281            DPRINTF(BusBridge, "Scheduling next send\n");
282            sendEvent.schedule(std::max(buf->ready, curTick + 1));
283        }
284    } else {
285        DPRINTF(BusBridge, "  unsuccessful\n");
286        pkt->senderState = old_sender_state;
287        inRetry = true;
288    }
289
290    DPRINTF(BusBridge, "trySend: queue size: %d outreq: %d outstanding resp: %d\n",
291                    sendQueue.size(), queuedRequests, outstandingResponses);
292}
293
294
295void
296Bridge::BridgePort::recvRetry()
297{
298    inRetry = false;
299    Tick nextReady = sendQueue.front()->ready;
300    if (nextReady <= curTick)
301        trySend();
302    else
303        sendEvent.schedule(nextReady);
304}
305
306/** Function called by the port when the bus is receiving a Atomic
307 * transaction.*/
308Tick
309Bridge::BridgePort::recvAtomic(PacketPtr pkt)
310{
311    return delay + otherPort->sendAtomic(pkt);
312}
313
314/** Function called by the port when the bus is receiving a Functional
315 * transaction.*/
316void
317Bridge::BridgePort::recvFunctional(PacketPtr pkt)
318{
319    std::list<PacketBuffer*>::iterator i;
320
321    for (i = sendQueue.begin();  i != sendQueue.end(); ++i) {
322        if (pkt->checkFunctional((*i)->pkt))
323            return;
324    }
325
326    // fall through if pkt still not satisfied
327    otherPort->sendFunctional(pkt);
328}
329
330/** Function called by the port when the bus is receiving a status change.*/
331void
332Bridge::BridgePort::recvStatusChange(Port::Status status)
333{
334    otherPort->sendStatusChange(status);
335}
336
337void
338Bridge::BridgePort::getDeviceAddressRanges(AddrRangeList &resp,
339                                           bool &snoop)
340{
341    otherPort->getPeerAddressRanges(resp, snoop);
342    FilterRangeList(filterRanges, resp);
343    // we don't allow snooping across bridges
344    snoop = false;
345}
346
347Bridge *
348BridgeParams::create()
349{
350    return new Bridge(this);
351}
352