bridge.cc revision 9542
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                                         Cycles _delay, int _resp_limit,
60                                         std::vector<AddrRange> _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                                           Cycles _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,
80                ticksToCycles(p->delay), p->resp_size, p->ranges),
81      masterPort(p->name + ".master", *this, slavePort,
82                 ticksToCycles(p->delay), p->req_size)
83{
84}
85
86BaseMasterPort&
87Bridge::getMasterPort(const std::string &if_name, PortID idx)
88{
89    if (if_name == "master")
90        return masterPort;
91    else
92        // pass it along to our super class
93        return MemObject::getMasterPort(if_name, idx);
94}
95
96BaseSlavePort&
97Bridge::getSlavePort(const std::string &if_name, PortID idx)
98{
99    if (if_name == "slave")
100        return slavePort;
101    else
102        // pass it along to our super class
103        return MemObject::getSlavePort(if_name, idx);
104}
105
106void
107Bridge::init()
108{
109    // make sure both sides are connected and have the same block size
110    if (!slavePort.isConnected() || !masterPort.isConnected())
111        fatal("Both ports of bus bridge are not connected to a bus.\n");
112
113    if (slavePort.peerBlockSize() != masterPort.peerBlockSize())
114        fatal("Slave port size %d, master port size %d \n " \
115              "Busses don't have the same block size... Not supported.\n",
116              slavePort.peerBlockSize(), masterPort.peerBlockSize());
117
118    // notify the master side  of our address ranges
119    slavePort.sendRangeChange();
120}
121
122bool
123Bridge::BridgeSlavePort::respQueueFull()
124{
125    return outstandingResponses == respQueueLimit;
126}
127
128bool
129Bridge::BridgeMasterPort::reqQueueFull()
130{
131    return transmitList.size() == reqQueueLimit;
132}
133
134bool
135Bridge::BridgeMasterPort::recvTimingResp(PacketPtr pkt)
136{
137    // all checks are done when the request is accepted on the slave
138    // side, so we are guaranteed to have space for the response
139    DPRINTF(Bridge, "recvTimingResp: %s addr 0x%x\n",
140            pkt->cmdString(), pkt->getAddr());
141
142    DPRINTF(Bridge, "Request queue size: %d\n", transmitList.size());
143
144    slavePort.schedTimingResp(pkt, bridge.clockEdge(delay));
145
146    return true;
147}
148
149bool
150Bridge::BridgeSlavePort::recvTimingReq(PacketPtr pkt)
151{
152    DPRINTF(Bridge, "recvTimingReq: %s addr 0x%x\n",
153            pkt->cmdString(), pkt->getAddr());
154
155    // ensure we do not have something waiting to retry
156    if(retryReq)
157        return false;
158
159    DPRINTF(Bridge, "Response queue size: %d outresp: %d\n",
160            transmitList.size(), outstandingResponses);
161
162    if (masterPort.reqQueueFull()) {
163        DPRINTF(Bridge, "Request queue full\n");
164        retryReq = true;
165    } else if (pkt->needsResponse()) {
166        if (respQueueFull()) {
167            DPRINTF(Bridge, "Response queue full\n");
168            retryReq = true;
169        } else {
170            DPRINTF(Bridge, "Reserving space for response\n");
171            assert(outstandingResponses != respQueueLimit);
172            ++outstandingResponses;
173            retryReq = false;
174            masterPort.schedTimingReq(pkt, bridge.clockEdge(delay));
175        }
176    }
177
178    // remember that we are now stalling a packet and that we have to
179    // tell the sending master to retry once space becomes available,
180    // we make no distinction whether the stalling is due to the
181    // request queue or response queue being full
182    return !retryReq;
183}
184
185void
186Bridge::BridgeSlavePort::retryStalledReq()
187{
188    if (retryReq) {
189        DPRINTF(Bridge, "Request waiting for retry, now retrying\n");
190        retryReq = false;
191        sendRetry();
192    }
193}
194
195void
196Bridge::BridgeMasterPort::schedTimingReq(PacketPtr pkt, Tick when)
197{
198    // If we expect to see a response, we need to restore the source
199    // and destination field that is potentially changed by a second
200    // bus
201    if (!pkt->memInhibitAsserted() && pkt->needsResponse()) {
202        // Update the sender state so we can deal with the response
203        // appropriately
204        pkt->pushSenderState(new RequestState(pkt->getSrc()));
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 =
228        dynamic_cast<RequestState*>(pkt->popSenderState());
229    assert(req_state != NULL);
230    pkt->setDest(req_state->origSrc);
231    delete req_state;
232
233    // the bridge assumes that at least one bus has set the
234    // destination field of the packet
235    assert(pkt->isDestValid());
236    DPRINTF(Bridge, "response, new dest %d\n", pkt->getDest());
237
238    // If we're about to put this packet at the head of the queue, we
239    // need to schedule an event to do the transmit.  Otherwise there
240    // should already be an event scheduled for sending the head
241    // packet.
242    if (transmitList.empty()) {
243        bridge.schedule(sendEvent, when);
244    }
245
246    transmitList.push_back(DeferredPacket(pkt, when));
247}
248
249void
250Bridge::BridgeMasterPort::trySendTiming()
251{
252    assert(!transmitList.empty());
253
254    DeferredPacket req = transmitList.front();
255
256    assert(req.tick <= curTick());
257
258    PacketPtr pkt = req.pkt;
259
260    DPRINTF(Bridge, "trySend request addr 0x%x, queue size %d\n",
261            pkt->getAddr(), transmitList.size());
262
263    if (sendTimingReq(pkt)) {
264        // send successful
265        transmitList.pop_front();
266        DPRINTF(Bridge, "trySend request successful\n");
267
268        // If there are more packets to send, schedule event to try again.
269        if (!transmitList.empty()) {
270            req = transmitList.front();
271            DPRINTF(Bridge, "Scheduling next send\n");
272            bridge.schedule(sendEvent, std::max(req.tick,
273                                                bridge.nextCycle()));
274        }
275
276        // if we have stalled a request due to a full request queue,
277        // then send a retry at this point, also note that if the
278        // request we stalled was waiting for the response queue
279        // rather than the request queue we might stall it again
280        slavePort.retryStalledReq();
281    }
282
283    // if the send failed, then we try again once we receive a retry,
284    // and therefore there is no need to take any action
285}
286
287void
288Bridge::BridgeSlavePort::trySendTiming()
289{
290    assert(!transmitList.empty());
291
292    DeferredPacket resp = transmitList.front();
293
294    assert(resp.tick <= curTick());
295
296    PacketPtr pkt = resp.pkt;
297
298    DPRINTF(Bridge, "trySend response addr 0x%x, outstanding %d\n",
299            pkt->getAddr(), outstandingResponses);
300
301    if (sendTimingResp(pkt)) {
302        // send successful
303        transmitList.pop_front();
304        DPRINTF(Bridge, "trySend response successful\n");
305
306        assert(outstandingResponses != 0);
307        --outstandingResponses;
308
309        // If there are more packets to send, schedule event to try again.
310        if (!transmitList.empty()) {
311            resp = transmitList.front();
312            DPRINTF(Bridge, "Scheduling next send\n");
313            bridge.schedule(sendEvent, std::max(resp.tick,
314                                                bridge.nextCycle()));
315        }
316
317        // if there is space in the request queue and we were stalling
318        // a request, it will definitely be possible to accept it now
319        // since there is guaranteed space in the response queue
320        if (!masterPort.reqQueueFull() && retryReq) {
321            DPRINTF(Bridge, "Request waiting for retry, now retrying\n");
322            retryReq = false;
323            sendRetry();
324        }
325    }
326
327    // if the send failed, then we try again once we receive a retry,
328    // and therefore there is no need to take any action
329}
330
331void
332Bridge::BridgeMasterPort::recvRetry()
333{
334    Tick nextReady = transmitList.front().tick;
335    if (nextReady <= curTick())
336        trySendTiming();
337    else
338        bridge.schedule(sendEvent, nextReady);
339}
340
341void
342Bridge::BridgeSlavePort::recvRetry()
343{
344    Tick nextReady = transmitList.front().tick;
345    if (nextReady <= curTick())
346        trySendTiming();
347    else
348        bridge.schedule(sendEvent, nextReady);
349}
350
351Tick
352Bridge::BridgeSlavePort::recvAtomic(PacketPtr pkt)
353{
354    return delay * bridge.clockPeriod() + masterPort.sendAtomic(pkt);
355}
356
357void
358Bridge::BridgeSlavePort::recvFunctional(PacketPtr pkt)
359{
360    std::list<DeferredPacket>::iterator i;
361
362    pkt->pushLabel(name());
363
364    // check the response queue
365    for (i = transmitList.begin();  i != transmitList.end(); ++i) {
366        if (pkt->checkFunctional((*i).pkt)) {
367            pkt->makeResponse();
368            return;
369        }
370    }
371
372    // also check the master port's request queue
373    if (masterPort.checkFunctional(pkt)) {
374        return;
375    }
376
377    pkt->popLabel();
378
379    // fall through if pkt still not satisfied
380    masterPort.sendFunctional(pkt);
381}
382
383bool
384Bridge::BridgeMasterPort::checkFunctional(PacketPtr pkt)
385{
386    bool found = false;
387    std::list<DeferredPacket>::iterator i = transmitList.begin();
388
389    while(i != transmitList.end() && !found) {
390        if (pkt->checkFunctional((*i).pkt)) {
391            pkt->makeResponse();
392            found = true;
393        }
394        ++i;
395    }
396
397    return found;
398}
399
400AddrRangeList
401Bridge::BridgeSlavePort::getAddrRanges() const
402{
403    return ranges;
404}
405
406Bridge *
407BridgeParams::create()
408{
409    return new Bridge(this);
410}
411