bridge.cc revision 9164
1/*
2 * Copyright (c) 2011-2012 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/Bridge.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 _resp_limit,
60                                         std::vector<Range<Addr> > _ranges)
61    : SlavePort(_name, &_bridge), bridge(_bridge), masterPort(_masterPort),
62      delay(_delay), ranges(_ranges.begin(), _ranges.end()),
63      outstandingResponses(0), retryReq(false),
64      respQueueLimit(_resp_limit), sendEvent(*this)
65{
66}
67
68Bridge::BridgeMasterPort::BridgeMasterPort(const std::string& _name,
69                                           Bridge& _bridge,
70                                           BridgeSlavePort& _slavePort,
71                                           int _delay, int _req_limit)
72    : MasterPort(_name, &_bridge), bridge(_bridge), slavePort(_slavePort),
73      delay(_delay), reqQueueLimit(_req_limit), sendEvent(*this)
74{
75}
76
77Bridge::Bridge(Params *p)
78    : MemObject(p),
79      slavePort(p->name + ".slave", *this, masterPort, p->delay, p->resp_size,
80                p->ranges),
81      masterPort(p->name + ".master", *this, slavePort, p->delay, p->req_size)
82{
83}
84
85MasterPort&
86Bridge::getMasterPort(const std::string &if_name, int idx)
87{
88    if (if_name == "master")
89        return masterPort;
90    else
91        // pass it along to our super class
92        return MemObject::getMasterPort(if_name, idx);
93}
94
95SlavePort&
96Bridge::getSlavePort(const std::string &if_name, int idx)
97{
98    if (if_name == "slave")
99        return slavePort;
100    else
101        // pass it along to our super class
102        return MemObject::getSlavePort(if_name, idx);
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 transmitList.size() == reqQueueLimit;
131}
132
133bool
134Bridge::BridgeMasterPort::recvTimingResp(PacketPtr pkt)
135{
136    // all checks are done when the request is accepted on the slave
137    // side, so we are guaranteed to have space for the response
138    DPRINTF(Bridge, "recvTimingResp: %s addr 0x%x\n",
139            pkt->cmdString(), pkt->getAddr());
140
141    DPRINTF(Bridge, "Request queue size: %d\n", transmitList.size());
142
143    slavePort.schedTimingResp(pkt, curTick() + delay);
144
145    return true;
146}
147
148bool
149Bridge::BridgeSlavePort::recvTimingReq(PacketPtr pkt)
150{
151    DPRINTF(Bridge, "recvTimingReq: %s addr 0x%x\n",
152            pkt->cmdString(), pkt->getAddr());
153
154    // ensure we do not have something waiting to retry
155    if(retryReq)
156        return false;
157
158    DPRINTF(Bridge, "Response queue size: %d outresp: %d\n",
159            transmitList.size(), outstandingResponses);
160
161    if (masterPort.reqQueueFull()) {
162        DPRINTF(Bridge, "Request queue full\n");
163        retryReq = true;
164    } else if (pkt->needsResponse()) {
165        if (respQueueFull()) {
166            DPRINTF(Bridge, "Response queue full\n");
167            retryReq = true;
168        } else {
169            DPRINTF(Bridge, "Reserving space for response\n");
170            assert(outstandingResponses != respQueueLimit);
171            ++outstandingResponses;
172            retryReq = false;
173            masterPort.schedTimingReq(pkt, curTick() + delay);
174        }
175    }
176
177    // remember that we are now stalling a packet and that we have to
178    // tell the sending master to retry once space becomes available,
179    // we make no distinction whether the stalling is due to the
180    // request queue or response queue being full
181    return !retryReq;
182}
183
184void
185Bridge::BridgeSlavePort::retryStalledReq()
186{
187    if (retryReq) {
188        DPRINTF(Bridge, "Request waiting for retry, now retrying\n");
189        retryReq = false;
190        sendRetry();
191    }
192}
193
194void
195Bridge::BridgeMasterPort::schedTimingReq(PacketPtr pkt, Tick when)
196{
197    // If we expect to see a response, we need to restore the source
198    // and destination field that is potentially changed by a second
199    // bus
200    if (!pkt->memInhibitAsserted() && pkt->needsResponse()) {
201        // Update the sender state so we can deal with the response
202        // appropriately
203        RequestState *req_state = new RequestState(pkt);
204        pkt->senderState = req_state;
205    }
206
207    // If we're about to put this packet at the head of the queue, we
208    // need to schedule an event to do the transmit.  Otherwise there
209    // should already be an event scheduled for sending the head
210    // packet.
211    if (transmitList.empty()) {
212        bridge.schedule(sendEvent, when);
213    }
214
215    assert(transmitList.size() != reqQueueLimit);
216
217    transmitList.push_back(DeferredPacket(pkt, when));
218}
219
220
221void
222Bridge::BridgeSlavePort::schedTimingResp(PacketPtr pkt, Tick when)
223{
224    // This is a response for a request we forwarded earlier.  The
225    // corresponding request state should be stored in the packet's
226    // senderState field.
227    RequestState *req_state = dynamic_cast<RequestState*>(pkt->senderState);
228    assert(req_state != NULL);
229    // set up new packet dest & senderState based on values saved
230    // from original request
231    req_state->fixResponse(pkt);
232    delete req_state;
233
234    // the bridge assumes that at least one bus has set the
235    // destination field of the packet
236    assert(pkt->isDestValid());
237    DPRINTF(Bridge, "response, new dest %d\n", pkt->getDest());
238
239    // If we're about to put this packet at the head of the queue, we
240    // need to schedule an event to do the transmit.  Otherwise there
241    // should already be an event scheduled for sending the head
242    // packet.
243    if (transmitList.empty()) {
244        bridge.schedule(sendEvent, when);
245    }
246
247    transmitList.push_back(DeferredPacket(pkt, when));
248}
249
250void
251Bridge::BridgeMasterPort::trySendTiming()
252{
253    assert(!transmitList.empty());
254
255    DeferredPacket req = transmitList.front();
256
257    assert(req.tick <= curTick());
258
259    PacketPtr pkt = req.pkt;
260
261    DPRINTF(Bridge, "trySend request addr 0x%x, queue size %d\n",
262            pkt->getAddr(), transmitList.size());
263
264    if (sendTimingReq(pkt)) {
265        // send successful
266        transmitList.pop_front();
267        DPRINTF(Bridge, "trySend request successful\n");
268
269        // If there are more packets to send, schedule event to try again.
270        if (!transmitList.empty()) {
271            req = transmitList.front();
272            DPRINTF(Bridge, "Scheduling next send\n");
273            bridge.schedule(sendEvent, std::max(req.tick,
274                                                bridge.nextCycle()));
275        }
276
277        // if we have stalled a request due to a full request queue,
278        // then send a retry at this point, also note that if the
279        // request we stalled was waiting for the response queue
280        // rather than the request queue we might stall it again
281        slavePort.retryStalledReq();
282    }
283
284    // if the send failed, then we try again once we receive a retry,
285    // and therefore there is no need to take any action
286}
287
288void
289Bridge::BridgeSlavePort::trySendTiming()
290{
291    assert(!transmitList.empty());
292
293    DeferredPacket resp = transmitList.front();
294
295    assert(resp.tick <= curTick());
296
297    PacketPtr pkt = resp.pkt;
298
299    DPRINTF(Bridge, "trySend response addr 0x%x, outstanding %d\n",
300            pkt->getAddr(), outstandingResponses);
301
302    if (sendTimingResp(pkt)) {
303        // send successful
304        transmitList.pop_front();
305        DPRINTF(Bridge, "trySend response successful\n");
306
307        assert(outstandingResponses != 0);
308        --outstandingResponses;
309
310        // If there are more packets to send, schedule event to try again.
311        if (!transmitList.empty()) {
312            resp = transmitList.front();
313            DPRINTF(Bridge, "Scheduling next send\n");
314            bridge.schedule(sendEvent, std::max(resp.tick,
315                                                bridge.nextCycle()));
316        }
317
318        // if there is space in the request queue and we were stalling
319        // a request, it will definitely be possible to accept it now
320        // since there is guaranteed space in the response queue
321        if (!masterPort.reqQueueFull() && retryReq) {
322            DPRINTF(Bridge, "Request waiting for retry, now retrying\n");
323            retryReq = false;
324            sendRetry();
325        }
326    }
327
328    // if the send failed, then we try again once we receive a retry,
329    // and therefore there is no need to take any action
330}
331
332void
333Bridge::BridgeMasterPort::recvRetry()
334{
335    Tick nextReady = transmitList.front().tick;
336    if (nextReady <= curTick())
337        trySendTiming();
338    else
339        bridge.schedule(sendEvent, nextReady);
340}
341
342void
343Bridge::BridgeSlavePort::recvRetry()
344{
345    Tick nextReady = transmitList.front().tick;
346    if (nextReady <= curTick())
347        trySendTiming();
348    else
349        bridge.schedule(sendEvent, nextReady);
350}
351
352Tick
353Bridge::BridgeSlavePort::recvAtomic(PacketPtr pkt)
354{
355    return delay + masterPort.sendAtomic(pkt);
356}
357
358void
359Bridge::BridgeSlavePort::recvFunctional(PacketPtr pkt)
360{
361    std::list<DeferredPacket>::iterator i;
362
363    pkt->pushLabel(name());
364
365    // check the response queue
366    for (i = transmitList.begin();  i != transmitList.end(); ++i) {
367        if (pkt->checkFunctional((*i).pkt)) {
368            pkt->makeResponse();
369            return;
370        }
371    }
372
373    // also check the master port's request queue
374    if (masterPort.checkFunctional(pkt)) {
375        return;
376    }
377
378    pkt->popLabel();
379
380    // fall through if pkt still not satisfied
381    masterPort.sendFunctional(pkt);
382}
383
384bool
385Bridge::BridgeMasterPort::checkFunctional(PacketPtr pkt)
386{
387    bool found = false;
388    std::list<DeferredPacket>::iterator i = transmitList.begin();
389
390    while(i != transmitList.end() && !found) {
391        if (pkt->checkFunctional((*i).pkt)) {
392            pkt->makeResponse();
393            found = true;
394        }
395        ++i;
396    }
397
398    return found;
399}
400
401AddrRangeList
402Bridge::BridgeSlavePort::getAddrRanges() const
403{
404    return ranges;
405}
406
407Bridge *
408BridgeParams::create()
409{
410    return new Bridge(this);
411}
412