bridge.cc revision 9029
14040Ssaidi@eecs.umich.edu/*
23388Sgblack@eecs.umich.edu * Copyright (c) 2011-2012 ARM Limited
33388Sgblack@eecs.umich.edu * All rights reserved
43388Sgblack@eecs.umich.edu *
53388Sgblack@eecs.umich.edu * The license below extends only to copyright in the software and shall
63388Sgblack@eecs.umich.edu * not be construed as granting a license to any other intellectual
73388Sgblack@eecs.umich.edu * property including but not limited to intellectual property relating
83388Sgblack@eecs.umich.edu * to a hardware implementation of the functionality of the software
93388Sgblack@eecs.umich.edu * licensed hereunder.  You may use the software subject to the license
103388Sgblack@eecs.umich.edu * terms below provided that you ensure that this notice is replicated
113388Sgblack@eecs.umich.edu * unmodified and in its entirety in all distributions of the software,
123388Sgblack@eecs.umich.edu * modified or unmodified, in source code or in binary form.
133388Sgblack@eecs.umich.edu *
143388Sgblack@eecs.umich.edu * Copyright (c) 2006 The Regents of The University of Michigan
153388Sgblack@eecs.umich.edu * All rights reserved.
163388Sgblack@eecs.umich.edu *
173388Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
183388Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
193388Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
203388Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
213388Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
223388Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
233388Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
243388Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
253388Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
263388Sgblack@eecs.umich.edu * this software without specific prior written permission.
273388Sgblack@eecs.umich.edu *
283388Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
293388Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
302022SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
312022SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
323388Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
332022SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
342022SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
353388Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
363388Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372022SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
383388Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
393388Sgblack@eecs.umich.edu *
402022SN/A * Authors: Ali Saidi
413388Sgblack@eecs.umich.edu *          Steve Reinhardt
423388Sgblack@eecs.umich.edu *          Andreas Hansson
432516SN/A */
443439Sgblack@eecs.umich.edu
454040Ssaidi@eecs.umich.edu/**
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    : SlavePort(_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    : MasterPort(_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
91MasterPort&
92Bridge::getMasterPort(const std::string &if_name, int idx)
93{
94    if (if_name == "master")
95        return masterPort;
96    else
97        // pass it along to our super class
98        return MemObject::getMasterPort(if_name, idx);
99}
100
101SlavePort&
102Bridge::getSlavePort(const std::string &if_name, int idx)
103{
104    if (if_name == "slave")
105        return slavePort;
106    else
107        // pass it along to our super class
108        return MemObject::getSlavePort(if_name, idx);
109}
110
111void
112Bridge::init()
113{
114    // make sure both sides are connected and have the same block size
115    if (!slavePort.isConnected() || !masterPort.isConnected())
116        fatal("Both ports of bus bridge are not connected to a bus.\n");
117
118    if (slavePort.peerBlockSize() != masterPort.peerBlockSize())
119        fatal("Slave port size %d, master port size %d \n " \
120              "Busses don't have the same block size... Not supported.\n",
121              slavePort.peerBlockSize(), masterPort.peerBlockSize());
122
123    // notify the master side  of our address ranges
124    slavePort.sendRangeChange();
125}
126
127bool
128Bridge::BridgeSlavePort::respQueueFull()
129{
130    return outstandingResponses == respQueueLimit;
131}
132
133bool
134Bridge::BridgeMasterPort::reqQueueFull()
135{
136    return requestQueue.size() == reqQueueLimit;
137}
138
139bool
140Bridge::BridgeMasterPort::recvTimingResp(PacketPtr pkt)
141{
142    // all checks are done when the request is accepted on the slave
143    // side, so we are guaranteed to have space for the response
144    DPRINTF(BusBridge, "recvTiming: response %s addr 0x%x\n",
145            pkt->cmdString(), pkt->getAddr());
146
147    DPRINTF(BusBridge, "Request queue size: %d\n", requestQueue.size());
148
149    slavePort.queueForSendTiming(pkt);
150
151    return true;
152}
153
154bool
155Bridge::BridgeSlavePort::recvTimingReq(PacketPtr pkt)
156{
157    DPRINTF(BusBridge, "recvTiming: request %s addr 0x%x\n",
158            pkt->cmdString(), pkt->getAddr());
159
160    DPRINTF(BusBridge, "Response queue size: %d outresp: %d\n",
161            responseQueue.size(), outstandingResponses);
162
163    if (masterPort.reqQueueFull()) {
164        DPRINTF(BusBridge, "Request queue full, nacking\n");
165        nackRequest(pkt);
166        return true;
167    }
168
169    if (pkt->needsResponse()) {
170        if (respQueueFull()) {
171            DPRINTF(BusBridge,
172                    "Response queue full, no space for response, nacking\n");
173            DPRINTF(BusBridge,
174                    "queue size: %d outstanding resp: %d\n",
175                    responseQueue.size(), outstandingResponses);
176            nackRequest(pkt);
177            return true;
178        } else {
179            DPRINTF(BusBridge, "Request Needs response, reserving space\n");
180            assert(outstandingResponses != respQueueLimit);
181            ++outstandingResponses;
182        }
183    }
184
185    masterPort.queueForSendTiming(pkt);
186
187    return true;
188}
189
190void
191Bridge::BridgeSlavePort::nackRequest(PacketPtr pkt)
192{
193    // Nack the packet
194    pkt->makeTimingResponse();
195    pkt->setNacked();
196
197    // The Nack packets are stored in the response queue just like any
198    // other response, but they do not occupy any space as this is
199    // tracked by the outstandingResponses, this guarantees space for
200    // the Nack packets, but implicitly means we have an (unrealistic)
201    // unbounded Nack queue.
202
203    // put it on the list to send
204    Tick readyTime = curTick() + nackDelay;
205    DeferredResponse resp(pkt, readyTime, true);
206
207    // nothing on the list, add it and we're done
208    if (responseQueue.empty()) {
209        assert(!sendEvent.scheduled());
210        bridge->schedule(sendEvent, readyTime);
211        responseQueue.push_back(resp);
212        return;
213    }
214
215    assert(sendEvent.scheduled() || inRetry);
216
217    // does it go at the end?
218    if (readyTime >= responseQueue.back().ready) {
219        responseQueue.push_back(resp);
220        return;
221    }
222
223    // ok, somewhere in the middle, fun
224    std::list<DeferredResponse>::iterator i = responseQueue.begin();
225    std::list<DeferredResponse>::iterator end = responseQueue.end();
226    std::list<DeferredResponse>::iterator begin = responseQueue.begin();
227    bool done = false;
228
229    while (i != end && !done) {
230        if (readyTime < (*i).ready) {
231            if (i == begin)
232                bridge->reschedule(sendEvent, readyTime);
233            responseQueue.insert(i, resp);
234            done = true;
235        }
236        i++;
237    }
238    assert(done);
239}
240
241void
242Bridge::BridgeMasterPort::queueForSendTiming(PacketPtr pkt)
243{
244    Tick readyTime = curTick() + delay;
245
246    // If we expect to see a response, we need to restore the source
247    // and destination field that is potentially changed by a second
248    // bus
249    if (!pkt->memInhibitAsserted() && pkt->needsResponse()) {
250        // Update the sender state so we can deal with the response
251        // appropriately
252        RequestState *req_state = new RequestState(pkt);
253        pkt->senderState = req_state;
254    }
255
256    // If we're about to put this packet at the head of the queue, we
257    // need to schedule an event to do the transmit.  Otherwise there
258    // should already be an event scheduled for sending the head
259    // packet.
260    if (requestQueue.empty()) {
261        bridge->schedule(sendEvent, readyTime);
262    }
263
264    assert(requestQueue.size() != reqQueueLimit);
265
266    requestQueue.push_back(DeferredRequest(pkt, readyTime));
267}
268
269
270void
271Bridge::BridgeSlavePort::queueForSendTiming(PacketPtr pkt)
272{
273    // This is a response for a request we forwarded earlier.  The
274    // corresponding request state should be stored in the packet's
275    // senderState field.
276    RequestState *req_state = dynamic_cast<RequestState*>(pkt->senderState);
277    assert(req_state != NULL);
278    // set up new packet dest & senderState based on values saved
279    // from original request
280    req_state->fixResponse(pkt);
281
282    // the bridge assumes that at least one bus has set the
283    // destination field of the packet
284    assert(pkt->isDestValid());
285    DPRINTF(BusBridge, "response, new dest %d\n", pkt->getDest());
286    delete req_state;
287
288    Tick readyTime = curTick() + delay;
289
290    // If we're about to put this packet at the head of the queue, we
291    // need to schedule an event to do the transmit.  Otherwise there
292    // should already be an event scheduled for sending the head
293    // packet.
294    if (responseQueue.empty()) {
295        bridge->schedule(sendEvent, readyTime);
296    }
297    responseQueue.push_back(DeferredResponse(pkt, readyTime));
298}
299
300void
301Bridge::BridgeMasterPort::trySend()
302{
303    assert(!requestQueue.empty());
304
305    DeferredRequest req = requestQueue.front();
306
307    assert(req.ready <= curTick());
308
309    PacketPtr pkt = req.pkt;
310
311    DPRINTF(BusBridge, "trySend request: addr 0x%x\n", pkt->getAddr());
312
313    if (sendTimingReq(pkt)) {
314        // send successful
315        requestQueue.pop_front();
316
317        // If there are more packets to send, schedule event to try again.
318        if (!requestQueue.empty()) {
319            req = requestQueue.front();
320            DPRINTF(BusBridge, "Scheduling next send\n");
321            bridge->schedule(sendEvent,
322                             std::max(req.ready, curTick() + 1));
323        }
324    } else {
325        inRetry = true;
326    }
327
328    DPRINTF(BusBridge, "trySend: request queue size: %d\n",
329            requestQueue.size());
330}
331
332void
333Bridge::BridgeSlavePort::trySend()
334{
335    assert(!responseQueue.empty());
336
337    DeferredResponse resp = responseQueue.front();
338
339    assert(resp.ready <= curTick());
340
341    PacketPtr pkt = resp.pkt;
342
343    DPRINTF(BusBridge, "trySend response: dest %d addr 0x%x\n",
344            pkt->getDest(), pkt->getAddr());
345
346    bool was_nacked_here = resp.nackedHere;
347
348    if (sendTimingResp(pkt)) {
349        DPRINTF(BusBridge, "  successful\n");
350        // send successful
351        responseQueue.pop_front();
352
353        if (!was_nacked_here) {
354            assert(outstandingResponses != 0);
355            --outstandingResponses;
356        }
357
358        // If there are more packets to send, schedule event to try again.
359        if (!responseQueue.empty()) {
360            resp = responseQueue.front();
361            DPRINTF(BusBridge, "Scheduling next send\n");
362            bridge->schedule(sendEvent,
363                             std::max(resp.ready, curTick() + 1));
364        }
365    } else {
366        DPRINTF(BusBridge, "  unsuccessful\n");
367        inRetry = true;
368    }
369
370    DPRINTF(BusBridge, "trySend: queue size: %d outstanding resp: %d\n",
371            responseQueue.size(), outstandingResponses);
372}
373
374void
375Bridge::BridgeMasterPort::recvRetry()
376{
377    inRetry = false;
378    Tick nextReady = requestQueue.front().ready;
379    if (nextReady <= curTick())
380        trySend();
381    else
382        bridge->schedule(sendEvent, nextReady);
383}
384
385void
386Bridge::BridgeSlavePort::recvRetry()
387{
388    inRetry = false;
389    Tick nextReady = responseQueue.front().ready;
390    if (nextReady <= curTick())
391        trySend();
392    else
393        bridge->schedule(sendEvent, nextReady);
394}
395
396Tick
397Bridge::BridgeSlavePort::recvAtomic(PacketPtr pkt)
398{
399    return delay + masterPort.sendAtomic(pkt);
400}
401
402void
403Bridge::BridgeSlavePort::recvFunctional(PacketPtr pkt)
404{
405    std::list<DeferredResponse>::iterator i;
406
407    pkt->pushLabel(name());
408
409    // check the response queue
410    for (i = responseQueue.begin();  i != responseQueue.end(); ++i) {
411        if (pkt->checkFunctional((*i).pkt)) {
412            pkt->makeResponse();
413            return;
414        }
415    }
416
417    // also check the master port's request queue
418    if (masterPort.checkFunctional(pkt)) {
419        return;
420    }
421
422    pkt->popLabel();
423
424    // fall through if pkt still not satisfied
425    masterPort.sendFunctional(pkt);
426}
427
428bool
429Bridge::BridgeMasterPort::checkFunctional(PacketPtr pkt)
430{
431    bool found = false;
432    std::list<DeferredRequest>::iterator i = requestQueue.begin();
433
434    while(i != requestQueue.end() && !found) {
435        if (pkt->checkFunctional((*i).pkt)) {
436            pkt->makeResponse();
437            found = true;
438        }
439        ++i;
440    }
441
442    return found;
443}
444
445AddrRangeList
446Bridge::BridgeSlavePort::getAddrRanges()
447{
448    return ranges;
449}
450
451Bridge *
452BridgeParams::create()
453{
454    return new Bridge(this);
455}
456