bridge.cc revision 8851
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),
76      sendEvent(*this)
77{
78}
79
80Bridge::Bridge(Params *p)
81    : MemObject(p),
82      slavePort(p->name + "-slave", this, masterPort, p->delay,
83                p->nack_delay, p->resp_size, p->ranges),
84      masterPort(p->name + "-master", this, slavePort, p->delay, p->req_size),
85      ackWrites(p->write_ack), _params(p)
86{
87    if (ackWrites)
88        panic("No support for acknowledging writes\n");
89}
90
91Port*
92Bridge::getPort(const std::string &if_name, int idx)
93{
94    if (if_name == "slave")
95        return &slavePort;
96    else if (if_name == "master")
97        return &masterPort;
98    else {
99        panic("Bridge %s has no port named %s\n", name(), if_name);
100        return NULL;
101    }
102}
103
104
105void
106Bridge::init()
107{
108    // make sure both sides are connected and have the same block size
109    if (!slavePort.isConnected() || !masterPort.isConnected())
110        fatal("Both ports of bus bridge are not connected to a bus.\n");
111
112    if (slavePort.peerBlockSize() != masterPort.peerBlockSize())
113        fatal("Slave port size %d, master port size %d \n " \
114              "Busses don't have the same block size... Not supported.\n",
115              slavePort.peerBlockSize(), masterPort.peerBlockSize());
116
117    // notify the master side  of our address ranges
118    slavePort.sendRangeChange();
119}
120
121bool
122Bridge::BridgeSlavePort::respQueueFull()
123{
124    return outstandingResponses == respQueueLimit;
125}
126
127bool
128Bridge::BridgeMasterPort::reqQueueFull()
129{
130    return requestQueue.size() == reqQueueLimit;
131}
132
133bool
134Bridge::BridgeMasterPort::recvTiming(PacketPtr pkt)
135{
136    // should only see responses on the master side
137    assert(pkt->isResponse());
138
139    // all checks are done when the request is accepted on the slave
140    // side, so we are guaranteed to have space for the response
141
142    DPRINTF(BusBridge, "recvTiming: src %d dest %d addr 0x%x\n",
143            pkt->getSrc(), pkt->getDest(), pkt->getAddr());
144
145    DPRINTF(BusBridge, "Request queue size: %d\n", requestQueue.size());
146
147    slavePort.queueForSendTiming(pkt);
148
149    return true;
150}
151
152bool
153Bridge::BridgeSlavePort::recvTiming(PacketPtr pkt)
154{
155    // should only see requests on the slave side
156    assert(pkt->isRequest());
157
158    DPRINTF(BusBridge, "recvTiming: src %d dest %d addr 0x%x\n",
159            pkt->getSrc(), pkt->getDest(), pkt->getAddr());
160
161    DPRINTF(BusBridge, "Response queue size: %d outresp: %d\n",
162            responseQueue.size(), outstandingResponses);
163
164    if (masterPort.reqQueueFull()) {
165        DPRINTF(BusBridge, "Request queue full, nacking\n");
166        nackRequest(pkt);
167        return true;
168    }
169
170    if (pkt->needsResponse()) {
171        if (respQueueFull()) {
172            DPRINTF(BusBridge,
173                    "Response queue full, no space for response, nacking\n");
174            DPRINTF(BusBridge,
175                    "queue size: %d outstanding resp: %d\n",
176                    responseQueue.size(), outstandingResponses);
177            nackRequest(pkt);
178            return true;
179        } else {
180            DPRINTF(BusBridge, "Request Needs response, reserving space\n");
181            assert(outstandingResponses != respQueueLimit);
182            ++outstandingResponses;
183        }
184    }
185
186    masterPort.queueForSendTiming(pkt);
187
188    return true;
189}
190
191void
192Bridge::BridgeSlavePort::nackRequest(PacketPtr pkt)
193{
194    // Nack the packet
195    pkt->makeTimingResponse();
196    pkt->setNacked();
197
198    // The Nack packets are stored in the response queue just like any
199    // other response, but they do not occupy any space as this is
200    // tracked by the outstandingResponses, this guarantees space for
201    // the Nack packets, but implicitly means we have an (unrealistic)
202    // unbounded Nack queue.
203
204    // put it on the list to send
205    Tick readyTime = curTick() + nackDelay;
206    PacketBuffer *buf = new PacketBuffer(pkt, readyTime, true);
207
208    // nothing on the list, add it and we're done
209    if (responseQueue.empty()) {
210        assert(!sendEvent.scheduled());
211        bridge->schedule(sendEvent, readyTime);
212        responseQueue.push_back(buf);
213        return;
214    }
215
216    assert(sendEvent.scheduled() || inRetry);
217
218    // does it go at the end?
219    if (readyTime >= responseQueue.back()->ready) {
220        responseQueue.push_back(buf);
221        return;
222    }
223
224    // ok, somewhere in the middle, fun
225    std::list<PacketBuffer*>::iterator i = responseQueue.begin();
226    std::list<PacketBuffer*>::iterator end = responseQueue.end();
227    std::list<PacketBuffer*>::iterator begin = responseQueue.begin();
228    bool done = false;
229
230    while (i != end && !done) {
231        if (readyTime < (*i)->ready) {
232            if (i == begin)
233                bridge->reschedule(sendEvent, readyTime);
234            responseQueue.insert(i,buf);
235            done = true;
236        }
237        i++;
238    }
239    assert(done);
240}
241
242void
243Bridge::BridgeMasterPort::queueForSendTiming(PacketPtr pkt)
244{
245    Tick readyTime = curTick() + delay;
246    PacketBuffer *buf = new PacketBuffer(pkt, readyTime);
247
248    // If we're about to put this packet at the head of the queue, we
249    // need to schedule an event to do the transmit.  Otherwise there
250    // should already be an event scheduled for sending the head
251    // packet.
252    if (requestQueue.empty()) {
253        bridge->schedule(sendEvent, readyTime);
254    }
255
256    assert(requestQueue.size() != reqQueueLimit);
257
258    requestQueue.push_back(buf);
259}
260
261
262void
263Bridge::BridgeSlavePort::queueForSendTiming(PacketPtr pkt)
264{
265    // This is a response for a request we forwarded earlier.  The
266    // corresponding PacketBuffer should be stored in the packet's
267    // senderState field.
268    PacketBuffer *buf = dynamic_cast<PacketBuffer*>(pkt->senderState);
269    assert(buf != NULL);
270    // set up new packet dest & senderState based on values saved
271    // from original request
272    buf->fixResponse(pkt);
273
274    DPRINTF(BusBridge, "response, new dest %d\n", pkt->getDest());
275    delete buf;
276
277    Tick readyTime = curTick() + delay;
278    buf = new PacketBuffer(pkt, readyTime);
279
280    // If we're about to put this packet at the head of the queue, we
281    // need to schedule an event to do the transmit.  Otherwise there
282    // should already be an event scheduled for sending the head
283    // packet.
284    if (responseQueue.empty()) {
285        bridge->schedule(sendEvent, readyTime);
286    }
287    responseQueue.push_back(buf);
288}
289
290void
291Bridge::BridgeMasterPort::trySend()
292{
293    assert(!requestQueue.empty());
294
295    PacketBuffer *buf = requestQueue.front();
296
297    assert(buf->ready <= curTick());
298
299    PacketPtr pkt = buf->pkt;
300
301    DPRINTF(BusBridge, "trySend: origSrc %d dest %d addr 0x%x\n",
302            buf->origSrc, pkt->getDest(), pkt->getAddr());
303
304    // If the send was successful, make sure sender state was set to NULL
305    // otherwise we could get a NACK back of a packet that didn't expect a
306    // response and we would try to use freed memory.
307
308    Packet::SenderState *old_sender_state = pkt->senderState;
309    if (!buf->expectResponse)
310        pkt->senderState = NULL;
311
312    if (sendTiming(pkt)) {
313        // send successful
314        requestQueue.pop_front();
315        // we no longer own packet, so it's not safe to look at it
316        buf->pkt = NULL;
317
318        if (!buf->expectResponse) {
319            // no response expected... deallocate packet buffer now.
320            DPRINTF(BusBridge, "  successful: no response expected\n");
321            delete buf;
322        }
323
324        // If there are more packets to send, schedule event to try again.
325        if (!requestQueue.empty()) {
326            buf = requestQueue.front();
327            DPRINTF(BusBridge, "Scheduling next send\n");
328            bridge->schedule(sendEvent, std::max(buf->ready, curTick() + 1));
329        }
330    } else {
331        DPRINTF(BusBridge, "  unsuccessful\n");
332        pkt->senderState = old_sender_state;
333        inRetry = true;
334    }
335
336    DPRINTF(BusBridge, "trySend: request queue size: %d\n",
337            requestQueue.size());
338}
339
340void
341Bridge::BridgeSlavePort::trySend()
342{
343    assert(!responseQueue.empty());
344
345    PacketBuffer *buf = responseQueue.front();
346
347    assert(buf->ready <= curTick());
348
349    PacketPtr pkt = buf->pkt;
350
351    DPRINTF(BusBridge, "trySend: origSrc %d dest %d addr 0x%x\n",
352            buf->origSrc, pkt->getDest(), pkt->getAddr());
353
354    bool was_nacked_here = buf->nackedHere;
355
356    // no need to worry about the sender state since we are not
357    // modifying it
358
359    if (sendTiming(pkt)) {
360        DPRINTF(BusBridge, "  successful\n");
361        // send successful
362        responseQueue.pop_front();
363        // this is a response... deallocate packet buffer now.
364        delete buf;
365
366        if (!was_nacked_here) {
367            assert(outstandingResponses != 0);
368            --outstandingResponses;
369        }
370
371        // If there are more packets to send, schedule event to try again.
372        if (!responseQueue.empty()) {
373            buf = responseQueue.front();
374            DPRINTF(BusBridge, "Scheduling next send\n");
375            bridge->schedule(sendEvent, std::max(buf->ready, curTick() + 1));
376        }
377    } else {
378        DPRINTF(BusBridge, "  unsuccessful\n");
379        inRetry = true;
380    }
381
382    DPRINTF(BusBridge, "trySend: queue size: %d outstanding resp: %d\n",
383            responseQueue.size(), outstandingResponses);
384}
385
386void
387Bridge::BridgeMasterPort::recvRetry()
388{
389    inRetry = false;
390    Tick nextReady = requestQueue.front()->ready;
391    if (nextReady <= curTick())
392        trySend();
393    else
394        bridge->schedule(sendEvent, nextReady);
395}
396
397void
398Bridge::BridgeSlavePort::recvRetry()
399{
400    inRetry = false;
401    Tick nextReady = responseQueue.front()->ready;
402    if (nextReady <= curTick())
403        trySend();
404    else
405        bridge->schedule(sendEvent, nextReady);
406}
407
408Tick
409Bridge::BridgeMasterPort::recvAtomic(PacketPtr pkt)
410{
411    // master port should never receive any atomic access (panic only
412    // works once the other side, i.e. the busses, respects this)
413    //
414    //panic("Master port on %s got a recvAtomic\n", bridge->name());
415    return 0;
416}
417
418Tick
419Bridge::BridgeSlavePort::recvAtomic(PacketPtr pkt)
420{
421    return delay + masterPort.sendAtomic(pkt);
422}
423
424void
425Bridge::BridgeMasterPort::recvFunctional(PacketPtr pkt)
426{
427    // master port should never receive any functional access (panic
428    // only works once the other side, i.e. the busses, respect this)
429
430    // panic("Master port on %s got a recvFunctional\n", bridge->name());
431}
432
433void
434Bridge::BridgeSlavePort::recvFunctional(PacketPtr pkt)
435{
436    std::list<PacketBuffer*>::iterator i;
437
438    pkt->pushLabel(name());
439
440    // check the response queue
441    for (i = responseQueue.begin();  i != responseQueue.end(); ++i) {
442        if (pkt->checkFunctional((*i)->pkt)) {
443            pkt->makeResponse();
444            return;
445        }
446    }
447
448    // also check the master port's request queue
449    if (masterPort.checkFunctional(pkt)) {
450        return;
451    }
452
453    pkt->popLabel();
454
455    // fall through if pkt still not satisfied
456    masterPort.sendFunctional(pkt);
457}
458
459bool
460Bridge::BridgeMasterPort::checkFunctional(PacketPtr pkt)
461{
462    bool found = false;
463    std::list<PacketBuffer*>::iterator i = requestQueue.begin();
464
465    while(i != requestQueue.end() && !found) {
466        if (pkt->checkFunctional((*i)->pkt)) {
467            pkt->makeResponse();
468            found = true;
469        }
470        ++i;
471    }
472
473    return found;
474}
475
476/** Function called by the port when the bridge is receiving a range change.*/
477void
478Bridge::BridgeMasterPort::recvRangeChange()
479{
480    // no need to forward as the bridge has a fixed set of ranges
481}
482
483void
484Bridge::BridgeSlavePort::recvRangeChange()
485{
486    // is a slave port so do nothing
487}
488
489AddrRangeList
490Bridge::BridgeSlavePort::getAddrRanges()
491{
492    return ranges;
493}
494
495Bridge *
496BridgeParams::create()
497{
498    return new Bridge(this);
499}
500