bridge.cc revision 9029
14159Sgblack@eecs.umich.edu/*
210299Salexandru.dutu@amd.com * Copyright (c) 2011-2012 ARM Limited
34159Sgblack@eecs.umich.edu * All rights reserved
44159Sgblack@eecs.umich.edu *
54159Sgblack@eecs.umich.edu * The license below extends only to copyright in the software and shall
67087Snate@binkert.org * not be construed as granting a license to any other intellectual
77087Snate@binkert.org * property including but not limited to intellectual property relating
87087Snate@binkert.org * to a hardware implementation of the functionality of the software
97087Snate@binkert.org * licensed hereunder.  You may use the software subject to the license
107087Snate@binkert.org * terms below provided that you ensure that this notice is replicated
117087Snate@binkert.org * unmodified and in its entirety in all distributions of the software,
127087Snate@binkert.org * modified or unmodified, in source code or in binary form.
137087Snate@binkert.org *
144159Sgblack@eecs.umich.edu * Copyright (c) 2006 The Regents of The University of Michigan
157087Snate@binkert.org * All rights reserved.
167087Snate@binkert.org *
177087Snate@binkert.org * Redistribution and use in source and binary forms, with or without
187087Snate@binkert.org * modification, are permitted provided that the following conditions are
197087Snate@binkert.org * met: redistributions of source code must retain the above copyright
207087Snate@binkert.org * notice, this list of conditions and the following disclaimer;
217087Snate@binkert.org * redistributions in binary form must reproduce the above copyright
227087Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
234159Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
247087Snate@binkert.org * neither the name of the copyright holders nor the names of its
254159Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
264159Sgblack@eecs.umich.edu * this software without specific prior written permission.
274159Sgblack@eecs.umich.edu *
284159Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
294159Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
304159Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
314159Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
324159Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
334159Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
344159Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
354159Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
364159Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
374159Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
384159Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
394159Sgblack@eecs.umich.edu *
404159Sgblack@eecs.umich.edu * Authors: Ali Saidi
414159Sgblack@eecs.umich.edu *          Steve Reinhardt
424159Sgblack@eecs.umich.edu *          Andreas Hansson
434159Sgblack@eecs.umich.edu */
445124Sgblack@eecs.umich.edu
455124Sgblack@eecs.umich.edu/**
4610299Salexandru.dutu@amd.com * @file
475124Sgblack@eecs.umich.edu * Implementation of a memory-mapped bus bridge that connects a master
485237Sgblack@eecs.umich.edu * and a slave through a request and response queue.
496216Snate@binkert.org */
508953Sgblack@eecs.umich.edu
5110299Salexandru.dutu@amd.com#include "base/trace.hh"
5210299Salexandru.dutu@amd.com#include "debug/BusBridge.hh"
534159Sgblack@eecs.umich.edu#include "mem/bridge.hh"
545124Sgblack@eecs.umich.edu#include "params/Bridge.hh"
5511800Sbrandon.potter@amd.com
565124Sgblack@eecs.umich.eduBridge::BridgeSlavePort::BridgeSlavePort(const std::string &_name,
574159Sgblack@eecs.umich.edu                                         Bridge* _bridge,
584159Sgblack@eecs.umich.edu                                         BridgeMasterPort& _masterPort,
598953Sgblack@eecs.umich.edu                                         int _delay, int _nack_delay,
608953Sgblack@eecs.umich.edu                                         int _resp_limit,
618953Sgblack@eecs.umich.edu                                         std::vector<Range<Addr> > _ranges)
628953Sgblack@eecs.umich.edu    : SlavePort(_name, _bridge), bridge(_bridge), masterPort(_masterPort),
638953Sgblack@eecs.umich.edu      delay(_delay), nackDelay(_nack_delay),
648953Sgblack@eecs.umich.edu      ranges(_ranges.begin(), _ranges.end()),
658953Sgblack@eecs.umich.edu      outstandingResponses(0), inRetry(false),
6610905Sandreas.sandberg@arm.com      respQueueLimit(_resp_limit), sendEvent(*this)
674159Sgblack@eecs.umich.edu{
685124Sgblack@eecs.umich.edu}
695184Sgblack@eecs.umich.edu
705184Sgblack@eecs.umich.eduBridge::BridgeMasterPort::BridgeMasterPort(const std::string &_name,
715184Sgblack@eecs.umich.edu                                           Bridge* _bridge,
725184Sgblack@eecs.umich.edu                                           BridgeSlavePort& _slavePort,
738953Sgblack@eecs.umich.edu                                           int _delay, int _req_limit)
748953Sgblack@eecs.umich.edu    : MasterPort(_name, _bridge), bridge(_bridge), slavePort(_slavePort),
755184Sgblack@eecs.umich.edu      delay(_delay), inRetry(false), reqQueueLimit(_req_limit),
765124Sgblack@eecs.umich.edu      sendEvent(*this)
775124Sgblack@eecs.umich.edu{
785184Sgblack@eecs.umich.edu}
795124Sgblack@eecs.umich.edu
805124Sgblack@eecs.umich.eduBridge::Bridge(Params *p)
815124Sgblack@eecs.umich.edu    : MemObject(p),
825124Sgblack@eecs.umich.edu      slavePort(p->name + "-slave", this, masterPort, p->delay,
835124Sgblack@eecs.umich.edu                p->nack_delay, p->resp_size, p->ranges),
845124Sgblack@eecs.umich.edu      masterPort(p->name + "-master", this, slavePort, p->delay, p->req_size),
855124Sgblack@eecs.umich.edu      ackWrites(p->write_ack), _params(p)
865124Sgblack@eecs.umich.edu{
875124Sgblack@eecs.umich.edu    if (ackWrites)
885124Sgblack@eecs.umich.edu        panic("No support for acknowledging writes\n");
895124Sgblack@eecs.umich.edu}
905124Sgblack@eecs.umich.edu
915124Sgblack@eecs.umich.eduMasterPort&
928953Sgblack@eecs.umich.eduBridge::getMasterPort(const std::string &if_name, int idx)
938953Sgblack@eecs.umich.edu{
948953Sgblack@eecs.umich.edu    if (if_name == "master")
958953Sgblack@eecs.umich.edu        return masterPort;
965124Sgblack@eecs.umich.edu    else
9710558Salexandru.dutu@amd.com        // pass it along to our super class
9810558Salexandru.dutu@amd.com        return MemObject::getMasterPort(if_name, idx);
9910905Sandreas.sandberg@arm.com}
1005124Sgblack@eecs.umich.edu
1015877Shsul@eecs.umich.eduSlavePort&
1025877Shsul@eecs.umich.eduBridge::getSlavePort(const std::string &if_name, int idx)
1035877Shsul@eecs.umich.edu{
1045877Shsul@eecs.umich.edu    if (if_name == "slave")
1055877Shsul@eecs.umich.edu        return slavePort;
1065877Shsul@eecs.umich.edu    else
1075184Sgblack@eecs.umich.edu        // pass it along to our super class
1085184Sgblack@eecs.umich.edu        return MemObject::getSlavePort(if_name, idx);
1095184Sgblack@eecs.umich.edu}
1105184Sgblack@eecs.umich.edu
1115124Sgblack@eecs.umich.eduvoid
1129115SBrad.Beckmann@amd.comBridge::init()
1139115SBrad.Beckmann@amd.com{
1149115SBrad.Beckmann@amd.com    // make sure both sides are connected and have the same block size
1159115SBrad.Beckmann@amd.com    if (!slavePort.isConnected() || !masterPort.isConnected())
1169115SBrad.Beckmann@amd.com        fatal("Both ports of bus bridge are not connected to a bus.\n");
1179115SBrad.Beckmann@amd.com
11811168Sandreas.hansson@arm.com    if (slavePort.peerBlockSize() != masterPort.peerBlockSize())
11911168Sandreas.hansson@arm.com        fatal("Slave port size %d, master port size %d \n " \
1204159Sgblack@eecs.umich.edu              "Busses don't have the same block size... Not supported.\n",
12110299Salexandru.dutu@amd.com              slavePort.peerBlockSize(), masterPort.peerBlockSize());
12210299Salexandru.dutu@amd.com
12312460Sgabeblack@google.com    // notify the master side  of our address ranges
12412460Sgabeblack@google.com    slavePort.sendRangeChange();
12512460Sgabeblack@google.com}
12612460Sgabeblack@google.com
12712460Sgabeblack@google.combool
12810299Salexandru.dutu@amd.comBridge::BridgeSlavePort::respQueueFull()
12912460Sgabeblack@google.com{
13012460Sgabeblack@google.com    return outstandingResponses == respQueueLimit;
13112460Sgabeblack@google.com}
13212460Sgabeblack@google.com
13312460Sgabeblack@google.combool
13412460Sgabeblack@google.comBridge::BridgeMasterPort::reqQueueFull()
13512460Sgabeblack@google.com{
13612460Sgabeblack@google.com    return requestQueue.size() == reqQueueLimit;
13712460Sgabeblack@google.com}
13812460Sgabeblack@google.com
13912460Sgabeblack@google.combool
14012460Sgabeblack@google.comBridge::BridgeMasterPort::recvTimingResp(PacketPtr pkt)
14112460Sgabeblack@google.com{
14212460Sgabeblack@google.com    // all checks are done when the request is accepted on the slave
14312460Sgabeblack@google.com    // side, so we are guaranteed to have space for the response
14412460Sgabeblack@google.com    DPRINTF(BusBridge, "recvTiming: response %s addr 0x%x\n",
14512460Sgabeblack@google.com            pkt->cmdString(), pkt->getAddr());
14612460Sgabeblack@google.com
14712460Sgabeblack@google.com    DPRINTF(BusBridge, "Request queue size: %d\n", requestQueue.size());
14812460Sgabeblack@google.com
14912460Sgabeblack@google.com    slavePort.queueForSendTiming(pkt);
15012460Sgabeblack@google.com
15112460Sgabeblack@google.com    return true;
15212460Sgabeblack@google.com}
15312460Sgabeblack@google.com
15412460Sgabeblack@google.combool
15512460Sgabeblack@google.comBridge::BridgeSlavePort::recvTimingReq(PacketPtr pkt)
15612460Sgabeblack@google.com{
15710299Salexandru.dutu@amd.com    DPRINTF(BusBridge, "recvTiming: request %s addr 0x%x\n",
15810299Salexandru.dutu@amd.com            pkt->cmdString(), pkt->getAddr());
15912460Sgabeblack@google.com
16012460Sgabeblack@google.com    DPRINTF(BusBridge, "Response queue size: %d outresp: %d\n",
16112460Sgabeblack@google.com            responseQueue.size(), outstandingResponses);
16212460Sgabeblack@google.com
16312460Sgabeblack@google.com    if (masterPort.reqQueueFull()) {
16412460Sgabeblack@google.com        DPRINTF(BusBridge, "Request queue full, nacking\n");
16512460Sgabeblack@google.com        nackRequest(pkt);
16612460Sgabeblack@google.com        return true;
16712460Sgabeblack@google.com    }
16812460Sgabeblack@google.com
16912460Sgabeblack@google.com    if (pkt->needsResponse()) {
17012460Sgabeblack@google.com        if (respQueueFull()) {
17112460Sgabeblack@google.com            DPRINTF(BusBridge,
17212460Sgabeblack@google.com                    "Response queue full, no space for response, nacking\n");
17310299Salexandru.dutu@amd.com            DPRINTF(BusBridge,
17412460Sgabeblack@google.com                    "queue size: %d outstanding resp: %d\n",
17512460Sgabeblack@google.com                    responseQueue.size(), outstandingResponses);
17612460Sgabeblack@google.com            nackRequest(pkt);
17710299Salexandru.dutu@amd.com            return true;
17810299Salexandru.dutu@amd.com        } else {
17912460Sgabeblack@google.com            DPRINTF(BusBridge, "Request Needs response, reserving space\n");
18012460Sgabeblack@google.com            assert(outstandingResponses != respQueueLimit);
18112460Sgabeblack@google.com            ++outstandingResponses;
18210299Salexandru.dutu@amd.com        }
18312460Sgabeblack@google.com    }
18412460Sgabeblack@google.com
18512460Sgabeblack@google.com    masterPort.queueForSendTiming(pkt);
18612460Sgabeblack@google.com
18712460Sgabeblack@google.com    return true;
18812460Sgabeblack@google.com}
18912460Sgabeblack@google.com
19012460Sgabeblack@google.comvoid
19112460Sgabeblack@google.comBridge::BridgeSlavePort::nackRequest(PacketPtr pkt)
19212460Sgabeblack@google.com{
19312460Sgabeblack@google.com    // Nack the packet
19412460Sgabeblack@google.com    pkt->makeTimingResponse();
19512460Sgabeblack@google.com    pkt->setNacked();
19612460Sgabeblack@google.com
19710299Salexandru.dutu@amd.com    // The Nack packets are stored in the response queue just like any
19810299Salexandru.dutu@amd.com    // other response, but they do not occupy any space as this is
19912460Sgabeblack@google.com    // tracked by the outstandingResponses, this guarantees space for
20012460Sgabeblack@google.com    // the Nack packets, but implicitly means we have an (unrealistic)
20112460Sgabeblack@google.com    // unbounded Nack queue.
20210299Salexandru.dutu@amd.com
2034159Sgblack@eecs.umich.edu    // put it on the list to send
2044159Sgblack@eecs.umich.edu    Tick readyTime = curTick() + nackDelay;
2054159Sgblack@eecs.umich.edu    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