bridge.cc revision 4435
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/trace.hh"
41#include "mem/bridge.hh"
42#include "sim/builder.hh"
43
44Bridge::BridgePort::BridgePort(const std::string &_name,
45                               Bridge *_bridge, BridgePort *_otherPort,
46                               int _delay, int _nack_delay, int _req_limit,
47                               int _resp_limit, bool fix_partial_write)
48    : Port(_name), bridge(_bridge), otherPort(_otherPort),
49      delay(_delay), nackDelay(_nack_delay), fixPartialWrite(fix_partial_write),
50      outstandingResponses(0), queuedRequests(0), inRetry(false),
51      reqQueueLimit(_req_limit), respQueueLimit(_resp_limit), sendEvent(this)
52{
53}
54
55Bridge::Bridge(Params *p)
56    : MemObject(p->name),
57      portA(p->name + "-portA", this, &portB, p->delay, p->nack_delay,
58              p->req_size_a, p->resp_size_a, p->fix_partial_write_a),
59      portB(p->name + "-portB", this, &portA, p->delay, p->nack_delay,
60              p->req_size_b, p->resp_size_b, p->fix_partial_write_b),
61      ackWrites(p->write_ack), _params(p)
62{
63    if (ackWrites)
64        panic("No support for acknowledging writes\n");
65}
66
67Port *
68Bridge::getPort(const std::string &if_name, int idx)
69{
70    BridgePort *port;
71
72    if (if_name == "side_a")
73        port = &portA;
74    else if (if_name == "side_b")
75        port = &portB;
76    else
77        return NULL;
78
79    if (port->getPeer() != NULL)
80        panic("bridge side %s already connected to.", if_name);
81    return port;
82}
83
84
85void
86Bridge::init()
87{
88    // Make sure that both sides are connected to.
89    if (portA.getPeer() == NULL || portB.getPeer() == NULL)
90        fatal("Both ports of bus bridge are not connected to a bus.\n");
91
92    if (portA.peerBlockSize() != portB.peerBlockSize())
93        fatal("Busses don't have the same block size... Not supported.\n");
94}
95
96bool
97Bridge::BridgePort::respQueueFull()
98{
99    assert(outstandingResponses >= 0 && outstandingResponses <= respQueueLimit);
100    return outstandingResponses >= respQueueLimit;
101}
102
103bool
104Bridge::BridgePort::reqQueueFull()
105{
106    assert(queuedRequests >= 0 && queuedRequests <= reqQueueLimit);
107    return queuedRequests >= reqQueueLimit;
108}
109
110/** Function called by the port when the bus is receiving a Timing
111 * transaction.*/
112bool
113Bridge::BridgePort::recvTiming(PacketPtr pkt)
114{
115    if (!(pkt->flags & SNOOP_COMMIT))
116        return true;
117
118
119    DPRINTF(BusBridge, "recvTiming: src %d dest %d addr 0x%x\n",
120                pkt->getSrc(), pkt->getDest(), pkt->getAddr());
121
122    if (pkt->isRequest() && otherPort->reqQueueFull()) {
123        DPRINTF(BusBridge, "Remote queue full, nacking\n");
124        nackRequest(pkt);
125        return true;
126    }
127
128    if (pkt->needsResponse() && pkt->result != Packet::Nacked)
129        if (respQueueFull()) {
130            DPRINTF(BusBridge, "Local queue full, no space for response, nacking\n");
131            DPRINTF(BusBridge, "queue size: %d outreq: %d outstanding resp: %d\n",
132                    sendQueue.size(), queuedRequests, outstandingResponses);
133            nackRequest(pkt);
134            return true;
135        } else {
136            DPRINTF(BusBridge, "Request Needs response, reserving space\n");
137            ++outstandingResponses;
138        }
139
140    otherPort->queueForSendTiming(pkt);
141
142    return true;
143}
144
145void
146Bridge::BridgePort::nackRequest(PacketPtr pkt)
147{
148    // Nack the packet
149    pkt->result = Packet::Nacked;
150    pkt->setDest(pkt->getSrc());
151
152    //put it on the list to send
153    Tick readyTime = curTick + nackDelay;
154    PacketBuffer *buf = new PacketBuffer(pkt, readyTime, true);
155
156    // nothing on the list, add it and we're done
157    if (sendQueue.empty()) {
158        assert(!sendEvent.scheduled());
159        sendEvent.schedule(readyTime);
160        sendQueue.push_back(buf);
161        return;
162    }
163
164    assert(sendEvent.scheduled() || inRetry);
165
166    // does it go at the end?
167    if (readyTime >= sendQueue.back()->ready) {
168        sendQueue.push_back(buf);
169        return;
170    }
171
172    // ok, somewhere in the middle, fun
173    std::list<PacketBuffer*>::iterator i = sendQueue.begin();
174    std::list<PacketBuffer*>::iterator end = sendQueue.end();
175    std::list<PacketBuffer*>::iterator begin = sendQueue.begin();
176    bool done = false;
177
178    while (i != end && !done) {
179        if (readyTime < (*i)->ready) {
180            if (i == begin)
181                sendEvent.reschedule(readyTime);
182            sendQueue.insert(i,buf);
183            done = true;
184        }
185        i++;
186    }
187    assert(done);
188}
189
190
191void
192Bridge::BridgePort::queueForSendTiming(PacketPtr pkt)
193{
194   if (pkt->isResponse() || pkt->result == Packet::Nacked) {
195        // This is a response for a request we forwarded earlier.  The
196        // corresponding PacketBuffer should be stored in the packet's
197        // senderState field.
198        PacketBuffer *buf = dynamic_cast<PacketBuffer*>(pkt->senderState);
199        assert(buf != NULL);
200        // set up new packet dest & senderState based on values saved
201        // from original request
202        buf->fixResponse(pkt);
203
204        // Check if this packet was expecting a response (this is either it or
205        // its a nacked packet and we won't be seeing that response)
206        if (buf->expectResponse)
207            --outstandingResponses;
208
209
210        DPRINTF(BusBridge, "restoring  sender state: %#X, from packet buffer: %#X\n",
211                        pkt->senderState, buf);
212        DPRINTF(BusBridge, "  is response, new dest %d\n", pkt->getDest());
213        delete buf;
214    }
215
216    Tick readyTime = curTick + delay;
217    PacketBuffer *buf = new PacketBuffer(pkt, readyTime);
218    DPRINTF(BusBridge, "old sender state: %#X, new sender state: %#X\n",
219            buf->origSenderState, buf);
220
221    // If we're about to put this packet at the head of the queue, we
222    // need to schedule an event to do the transmit.  Otherwise there
223    // should already be an event scheduled for sending the head
224    // packet.
225    if (sendQueue.empty()) {
226        sendEvent.schedule(readyTime);
227    }
228    ++queuedRequests;
229    sendQueue.push_back(buf);
230}
231
232void
233Bridge::BridgePort::trySend()
234{
235    assert(!sendQueue.empty());
236
237    int pbs = peerBlockSize();
238
239    PacketBuffer *buf = sendQueue.front();
240
241    assert(buf->ready <= curTick);
242
243    PacketPtr pkt = buf->pkt;
244
245    pkt->flags &= ~SNOOP_COMMIT; //CLear it if it was set
246
247    if (pkt->cmd == MemCmd::WriteInvalidateReq && fixPartialWrite &&
248            pkt->result != Packet::Nacked && pkt->getOffset(pbs) &&
249            pkt->getSize() != pbs) {
250        buf->partialWriteFix(this);
251        pkt = buf->pkt;
252    }
253
254    DPRINTF(BusBridge, "trySend: origSrc %d dest %d addr 0x%x\n",
255            buf->origSrc, pkt->getDest(), pkt->getAddr());
256
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 (!buf->nacked)
274                --queuedRequests;
275
276        // If there are more packets to send, schedule event to try again.
277        if (!sendQueue.empty()) {
278            buf = sendQueue.front();
279            DPRINTF(BusBridge, "Scheduling next send\n");
280            sendEvent.schedule(std::max(buf->ready, curTick + 1));
281        }
282    } else {
283        DPRINTF(BusBridge, "  unsuccessful\n");
284        buf->undoPartialWriteFix();
285        inRetry = true;
286    }
287    DPRINTF(BusBridge, "trySend: queue size: %d outreq: %d outstanding resp: %d\n",
288                    sendQueue.size(), queuedRequests, outstandingResponses);
289}
290
291
292void
293Bridge::BridgePort::recvRetry()
294{
295    inRetry = false;
296    Tick nextReady = sendQueue.front()->ready;
297    if (nextReady <= curTick)
298        trySend();
299    else
300        sendEvent.schedule(nextReady);
301}
302
303/** Function called by the port when the bus is receiving a Atomic
304 * transaction.*/
305Tick
306Bridge::BridgePort::recvAtomic(PacketPtr pkt)
307{
308    return otherPort->sendAtomic(pkt) + delay;
309}
310
311/** Function called by the port when the bus is receiving a Functional
312 * transaction.*/
313void
314Bridge::BridgePort::recvFunctional(PacketPtr pkt)
315{
316    std::list<PacketBuffer*>::iterator i;
317    bool pktContinue = true;
318
319    for (i = sendQueue.begin();  i != sendQueue.end(); ++i) {
320        if (pkt->intersect((*i)->pkt)) {
321            pktContinue &= fixPacket(pkt, (*i)->pkt);
322        }
323    }
324
325    if (pktContinue) {
326        otherPort->sendFunctional(pkt);
327    }
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                                           AddrRangeList &snoop)
340{
341    otherPort->getPeerAddressRanges(resp, snoop);
342}
343
344BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bridge)
345
346   Param<int> req_size_a;
347   Param<int> req_size_b;
348   Param<int> resp_size_a;
349   Param<int> resp_size_b;
350   Param<Tick> delay;
351   Param<Tick> nack_delay;
352   Param<bool> write_ack;
353   Param<bool> fix_partial_write_a;
354   Param<bool> fix_partial_write_b;
355
356END_DECLARE_SIM_OBJECT_PARAMS(Bridge)
357
358BEGIN_INIT_SIM_OBJECT_PARAMS(Bridge)
359
360    INIT_PARAM(req_size_a, "The size of the queue for requests coming into side a"),
361    INIT_PARAM(req_size_b, "The size of the queue for requests coming into side b"),
362    INIT_PARAM(resp_size_a, "The size of the queue for responses coming into side a"),
363    INIT_PARAM(resp_size_b, "The size of the queue for responses coming into side b"),
364    INIT_PARAM(delay, "The miminum delay to cross this bridge"),
365    INIT_PARAM(nack_delay, "The minimum delay to nack a packet"),
366    INIT_PARAM(write_ack, "Acknowledge any writes that are received."),
367    INIT_PARAM(fix_partial_write_a, "Fixup any partial block writes that are received"),
368    INIT_PARAM(fix_partial_write_b, "Fixup any partial block writes that are received")
369
370END_INIT_SIM_OBJECT_PARAMS(Bridge)
371
372CREATE_SIM_OBJECT(Bridge)
373{
374    Bridge::Params *p = new Bridge::Params;
375    p->name = getInstanceName();
376    p->req_size_a = req_size_a;
377    p->req_size_b = req_size_b;
378    p->resp_size_a = resp_size_a;
379    p->resp_size_b = resp_size_b;
380    p->delay = delay;
381    p->nack_delay = nack_delay;
382    p->write_ack = write_ack;
383    p->fix_partial_write_a = fix_partial_write_a;
384    p->fix_partial_write_b = fix_partial_write_b;
385    return new Bridge(p);
386}
387
388REGISTER_SIM_OBJECT("Bridge", Bridge)
389
390