bridge.cc revision 8713
1/*
2 * Copyright (c) 2011 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Ali Saidi
41 *          Steve Reinhardt
42 *          Andreas Hansson
43 */
44
45/**
46 * @file
47 * Implementation of a memory-mapped bus bridge that connects a master
48 * and a slave through a request and response queue.
49 */
50
51#include "base/trace.hh"
52#include "debug/BusBridge.hh"
53#include "mem/bridge.hh"
54#include "params/Bridge.hh"
55
56Bridge::BridgeSlavePort::BridgeSlavePort(const std::string &_name,
57                                         Bridge* _bridge,
58                                         BridgeMasterPort* _masterPort,
59                                         int _delay, int _nack_delay,
60                                         int _resp_limit,
61                                         std::vector<Range<Addr> > _ranges)
62    : Port(_name, _bridge), bridge(_bridge), masterPort(_masterPort),
63      delay(_delay), nackDelay(_nack_delay),
64      ranges(_ranges.begin(), _ranges.end()),
65      outstandingResponses(0), inRetry(false),
66      respQueueLimit(_resp_limit), sendEvent(this)
67{
68}
69
70Bridge::BridgeMasterPort::BridgeMasterPort(const std::string &_name,
71                                           Bridge* _bridge,
72                                           BridgeSlavePort* _slavePort,
73                                           int _delay, int _req_limit)
74    : Port(_name, _bridge), bridge(_bridge), slavePort(_slavePort),
75      delay(_delay), inRetry(false), reqQueueLimit(_req_limit), sendEvent(this)
76{
77}
78
79Bridge::Bridge(Params *p)
80    : MemObject(p),
81      slavePort(p->name + "-slave", this, &masterPort, p->delay,
82                p->nack_delay, p->resp_size, p->ranges),
83      masterPort(p->name + "-master", this, &slavePort, p->delay, p->req_size),
84      ackWrites(p->write_ack), _params(p)
85{
86    if (ackWrites)
87        panic("No support for acknowledging writes\n");
88}
89
90Port*
91Bridge::getPort(const std::string &if_name, int idx)
92{
93    Port* port;
94
95    if (if_name == "slave")
96        port = &slavePort;
97    else if (if_name == "master")
98        port = &masterPort;
99    else
100        return NULL;
101
102    if (port->getPeer() != NULL)
103        panic("bridge side %s already connected to %s.",
104                if_name, port->getPeer()->name());
105    return port;
106}
107
108
109void
110Bridge::init()
111{
112    // make sure both sides are connected and have the same block size
113    if (!slavePort.isConnected() || !masterPort.isConnected())
114        fatal("Both ports of bus bridge are not connected to a bus.\n");
115
116    if (slavePort.peerBlockSize() != masterPort.peerBlockSize())
117        fatal("Slave port size %d, master port size %d \n " \
118              "Busses don't have the same block size... Not supported.\n",
119              slavePort.peerBlockSize(), masterPort.peerBlockSize());
120
121    // notify the master side  of our address ranges
122    slavePort.sendRangeChange();
123}
124
125bool
126Bridge::BridgeSlavePort::respQueueFull()
127{
128    return outstandingResponses == respQueueLimit;
129}
130
131bool
132Bridge::BridgeMasterPort::reqQueueFull()
133{
134    return requestQueue.size() == reqQueueLimit;
135}
136
137bool
138Bridge::BridgeMasterPort::recvTiming(PacketPtr pkt)
139{
140    // should only see responses on the master side
141    assert(pkt->isResponse());
142
143    // all checks are done when the request is accepted on the slave
144    // side, so we are guaranteed to have space for the response
145
146    DPRINTF(BusBridge, "recvTiming: src %d dest %d addr 0x%x\n",
147            pkt->getSrc(), pkt->getDest(), pkt->getAddr());
148
149    DPRINTF(BusBridge, "Request queue size: %d\n", requestQueue.size());
150
151    slavePort->queueForSendTiming(pkt);
152
153    return true;
154}
155
156bool
157Bridge::BridgeSlavePort::recvTiming(PacketPtr pkt)
158{
159    // should only see requests on the slave side
160    assert(pkt->isRequest());
161
162    DPRINTF(BusBridge, "recvTiming: src %d dest %d addr 0x%x\n",
163            pkt->getSrc(), pkt->getDest(), pkt->getAddr());
164
165    DPRINTF(BusBridge, "Response queue size: %d outresp: %d\n",
166            responseQueue.size(), outstandingResponses);
167
168    if (masterPort->reqQueueFull()) {
169        DPRINTF(BusBridge, "Request queue full, nacking\n");
170        nackRequest(pkt);
171        return true;
172    }
173
174    if (pkt->needsResponse()) {
175        if (respQueueFull()) {
176            DPRINTF(BusBridge,
177                    "Response queue full, no space for response, nacking\n");
178            DPRINTF(BusBridge,
179                    "queue size: %d outstanding resp: %d\n",
180                    responseQueue.size(), outstandingResponses);
181            nackRequest(pkt);
182            return true;
183        } else {
184            DPRINTF(BusBridge, "Request Needs response, reserving space\n");
185            assert(outstandingResponses != respQueueLimit);
186            ++outstandingResponses;
187        }
188    }
189
190    masterPort->queueForSendTiming(pkt);
191
192    return true;
193}
194
195void
196Bridge::BridgeSlavePort::nackRequest(PacketPtr pkt)
197{
198    // Nack the packet
199    pkt->makeTimingResponse();
200    pkt->setNacked();
201
202    // The Nack packets are stored in the response queue just like any
203    // other response, but they do not occupy any space as this is
204    // tracked by the outstandingResponses, this guarantees space for
205    // the Nack packets, but implicitly means we have an (unrealistic)
206    // unbounded Nack queue.
207
208    // put it on the list to send
209    Tick readyTime = curTick() + nackDelay;
210    PacketBuffer *buf = new PacketBuffer(pkt, readyTime, true);
211
212    // nothing on the list, add it and we're done
213    if (responseQueue.empty()) {
214        assert(!sendEvent.scheduled());
215        bridge->schedule(sendEvent, readyTime);
216        responseQueue.push_back(buf);
217        return;
218    }
219
220    assert(sendEvent.scheduled() || inRetry);
221
222    // does it go at the end?
223    if (readyTime >= responseQueue.back()->ready) {
224        responseQueue.push_back(buf);
225        return;
226    }
227
228    // ok, somewhere in the middle, fun
229    std::list<PacketBuffer*>::iterator i = responseQueue.begin();
230    std::list<PacketBuffer*>::iterator end = responseQueue.end();
231    std::list<PacketBuffer*>::iterator begin = responseQueue.begin();
232    bool done = false;
233
234    while (i != end && !done) {
235        if (readyTime < (*i)->ready) {
236            if (i == begin)
237                bridge->reschedule(sendEvent, readyTime);
238            responseQueue.insert(i,buf);
239            done = true;
240        }
241        i++;
242    }
243    assert(done);
244}
245
246void
247Bridge::BridgeMasterPort::queueForSendTiming(PacketPtr pkt)
248{
249    Tick readyTime = curTick() + delay;
250    PacketBuffer *buf = new PacketBuffer(pkt, readyTime);
251
252    // If we're about to put this packet at the head of the queue, we
253    // need to schedule an event to do the transmit.  Otherwise there
254    // should already be an event scheduled for sending the head
255    // packet.
256    if (requestQueue.empty()) {
257        bridge->schedule(sendEvent, readyTime);
258    }
259
260    assert(requestQueue.size() != reqQueueLimit);
261
262    requestQueue.push_back(buf);
263}
264
265
266void
267Bridge::BridgeSlavePort::queueForSendTiming(PacketPtr pkt)
268{
269    // This is a response for a request we forwarded earlier.  The
270    // corresponding PacketBuffer should be stored in the packet's
271    // senderState field.
272    PacketBuffer *buf = dynamic_cast<PacketBuffer*>(pkt->senderState);
273    assert(buf != NULL);
274    // set up new packet dest & senderState based on values saved
275    // from original request
276    buf->fixResponse(pkt);
277
278    DPRINTF(BusBridge, "response, new dest %d\n", pkt->getDest());
279    delete buf;
280
281    Tick readyTime = curTick() + delay;
282    buf = new PacketBuffer(pkt, readyTime);
283
284    // If we're about to put this packet at the head of the queue, we
285    // need to schedule an event to do the transmit.  Otherwise there
286    // should already be an event scheduled for sending the head
287    // packet.
288    if (responseQueue.empty()) {
289        bridge->schedule(sendEvent, readyTime);
290    }
291    responseQueue.push_back(buf);
292}
293
294void
295Bridge::BridgeMasterPort::trySend()
296{
297    assert(!requestQueue.empty());
298
299    PacketBuffer *buf = requestQueue.front();
300
301    assert(buf->ready <= curTick());
302
303    PacketPtr pkt = buf->pkt;
304
305    DPRINTF(BusBridge, "trySend: origSrc %d dest %d addr 0x%x\n",
306            buf->origSrc, pkt->getDest(), pkt->getAddr());
307
308    // If the send was successful, make sure sender state was set to NULL
309    // otherwise we could get a NACK back of a packet that didn't expect a
310    // response and we would try to use freed memory.
311
312    Packet::SenderState *old_sender_state = pkt->senderState;
313    if (!buf->expectResponse)
314        pkt->senderState = NULL;
315
316    if (sendTiming(pkt)) {
317        // send successful
318        requestQueue.pop_front();
319        // we no longer own packet, so it's not safe to look at it
320        buf->pkt = NULL;
321
322        if (!buf->expectResponse) {
323            // no response expected... deallocate packet buffer now.
324            DPRINTF(BusBridge, "  successful: no response expected\n");
325            delete buf;
326        }
327
328        // If there are more packets to send, schedule event to try again.
329        if (!requestQueue.empty()) {
330            buf = requestQueue.front();
331            DPRINTF(BusBridge, "Scheduling next send\n");
332            bridge->schedule(sendEvent, std::max(buf->ready, curTick() + 1));
333        }
334    } else {
335        DPRINTF(BusBridge, "  unsuccessful\n");
336        pkt->senderState = old_sender_state;
337        inRetry = true;
338    }
339
340    DPRINTF(BusBridge, "trySend: request queue size: %d\n",
341            requestQueue.size());
342}
343
344void
345Bridge::BridgeSlavePort::trySend()
346{
347    assert(!responseQueue.empty());
348
349    PacketBuffer *buf = responseQueue.front();
350
351    assert(buf->ready <= curTick());
352
353    PacketPtr pkt = buf->pkt;
354
355    DPRINTF(BusBridge, "trySend: origSrc %d dest %d addr 0x%x\n",
356            buf->origSrc, pkt->getDest(), pkt->getAddr());
357
358    bool was_nacked_here = buf->nackedHere;
359
360    // no need to worry about the sender state since we are not
361    // modifying it
362
363    if (sendTiming(pkt)) {
364        DPRINTF(BusBridge, "  successful\n");
365        // send successful
366        responseQueue.pop_front();
367        // this is a response... deallocate packet buffer now.
368        delete buf;
369
370        if (!was_nacked_here) {
371            assert(outstandingResponses != 0);
372            --outstandingResponses;
373        }
374
375        // If there are more packets to send, schedule event to try again.
376        if (!responseQueue.empty()) {
377            buf = responseQueue.front();
378            DPRINTF(BusBridge, "Scheduling next send\n");
379            bridge->schedule(sendEvent, std::max(buf->ready, curTick() + 1));
380        }
381    } else {
382        DPRINTF(BusBridge, "  unsuccessful\n");
383        inRetry = true;
384    }
385
386    DPRINTF(BusBridge, "trySend: queue size: %d outstanding resp: %d\n",
387            responseQueue.size(), outstandingResponses);
388}
389
390void
391Bridge::BridgeMasterPort::recvRetry()
392{
393    inRetry = false;
394    Tick nextReady = requestQueue.front()->ready;
395    if (nextReady <= curTick())
396        trySend();
397    else
398        bridge->schedule(sendEvent, nextReady);
399}
400
401void
402Bridge::BridgeSlavePort::recvRetry()
403{
404    inRetry = false;
405    Tick nextReady = responseQueue.front()->ready;
406    if (nextReady <= curTick())
407        trySend();
408    else
409        bridge->schedule(sendEvent, nextReady);
410}
411
412Tick
413Bridge::BridgeMasterPort::recvAtomic(PacketPtr pkt)
414{
415    // master port should never receive any atomic access (panic only
416    // works once the other side, i.e. the busses, respects this)
417    //
418    //panic("Master port on %s got a recvAtomic\n", bridge->name());
419    return 0;
420}
421
422Tick
423Bridge::BridgeSlavePort::recvAtomic(PacketPtr pkt)
424{
425    return delay + masterPort->sendAtomic(pkt);
426}
427
428void
429Bridge::BridgeMasterPort::recvFunctional(PacketPtr pkt)
430{
431    // master port should never receive any functional access (panic
432    // only works once the other side, i.e. the busses, respect this)
433
434    // panic("Master port on %s got a recvFunctional\n", bridge->name());
435}
436
437void
438Bridge::BridgeSlavePort::recvFunctional(PacketPtr pkt)
439{
440    std::list<PacketBuffer*>::iterator i;
441
442    pkt->pushLabel(name());
443
444    // check the response queue
445    for (i = responseQueue.begin();  i != responseQueue.end(); ++i) {
446        if (pkt->checkFunctional((*i)->pkt)) {
447            pkt->makeResponse();
448            return;
449        }
450    }
451
452    // also check the master port's request queue
453    if (masterPort->checkFunctional(pkt)) {
454        return;
455    }
456
457    pkt->popLabel();
458
459    // fall through if pkt still not satisfied
460    masterPort->sendFunctional(pkt);
461}
462
463bool
464Bridge::BridgeMasterPort::checkFunctional(PacketPtr pkt)
465{
466    bool found = false;
467    std::list<PacketBuffer*>::iterator i = requestQueue.begin();
468
469    while(i != requestQueue.end() && !found) {
470        if (pkt->checkFunctional((*i)->pkt)) {
471            pkt->makeResponse();
472            found = true;
473        }
474        ++i;
475    }
476
477    return found;
478}
479
480/** Function called by the port when the bridge is receiving a range change.*/
481void
482Bridge::BridgeMasterPort::recvRangeChange()
483{
484    // no need to forward as the bridge has a fixed set of ranges
485}
486
487void
488Bridge::BridgeSlavePort::recvRangeChange()
489{
490    // is a slave port so do nothing
491}
492
493AddrRangeList
494Bridge::BridgeSlavePort::getAddrRanges()
495{
496    return ranges;
497}
498
499Bridge *
500BridgeParams::create()
501{
502    return new Bridge(this);
503}
504