bridge.cc revision 11193
18012Ssaidi@eecs.umich.edu/*
28029Snate@binkert.org * Copyright (c) 2011-2013, 2015 ARM Limited
38029Snate@binkert.org * All rights reserved
48013Sbinkertn@umich.edu *
58029Snate@binkert.org * The license below extends only to copyright in the software and shall
68029Snate@binkert.org * not be construed as granting a license to any other intellectual
78029Snate@binkert.org * property including but not limited to intellectual property relating
88029Snate@binkert.org * to a hardware implementation of the functionality of the software
98029Snate@binkert.org * licensed hereunder.  You may use the software subject to the license
108029Snate@binkert.org * terms below provided that you ensure that this notice is replicated
118029Snate@binkert.org * unmodified and in its entirety in all distributions of the software,
128029Snate@binkert.org * modified or unmodified, in source code or in binary form.
138029Snate@binkert.org *
148029Snate@binkert.org * Copyright (c) 2006 The Regents of The University of Michigan
158013Sbinkertn@umich.edu * All rights reserved.
168029Snate@binkert.org *
178029Snate@binkert.org * Redistribution and use in source and binary forms, with or without
188029Snate@binkert.org * modification, are permitted provided that the following conditions are
198029Snate@binkert.org * met: redistributions of source code must retain the above copyright
208029Snate@binkert.org * notice, this list of conditions and the following disclaimer;
218029Snate@binkert.org * redistributions in binary form must reproduce the above copyright
228029Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
238029Snate@binkert.org * documentation and/or other materials provided with the distribution;
248029Snate@binkert.org * neither the name of the copyright holders nor the names of its
258029Snate@binkert.org * contributors may be used to endorse or promote products derived from
268029Snate@binkert.org * this software without specific prior written permission.
278013Sbinkertn@umich.edu *
288012Ssaidi@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
297997Ssaidi@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
307997Ssaidi@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
317997Ssaidi@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
327997Ssaidi@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
337997Ssaidi@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
347997Ssaidi@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
357997Ssaidi@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
367997Ssaidi@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
377997Ssaidi@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
387997Ssaidi@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
397997Ssaidi@eecs.umich.edu *
407997Ssaidi@eecs.umich.edu * Authors: Ali Saidi
417997Ssaidi@eecs.umich.edu *          Steve Reinhardt
427997Ssaidi@eecs.umich.edu *          Andreas Hansson
437997Ssaidi@eecs.umich.edu */
447997Ssaidi@eecs.umich.edu
457997Ssaidi@eecs.umich.edu/**
467997Ssaidi@eecs.umich.edu * @file
477997Ssaidi@eecs.umich.edu * Implementation of a memory-mapped bridge that connects a master
487997Ssaidi@eecs.umich.edu * and a slave through a request and response queue.
497997Ssaidi@eecs.umich.edu */
507997Ssaidi@eecs.umich.edu
517997Ssaidi@eecs.umich.edu#include "base/trace.hh"
527997Ssaidi@eecs.umich.edu#include "debug/Bridge.hh"
537997Ssaidi@eecs.umich.edu#include "mem/bridge.hh"
547997Ssaidi@eecs.umich.edu#include "params/Bridge.hh"
557997Ssaidi@eecs.umich.edu
567997Ssaidi@eecs.umich.eduBridge::BridgeSlavePort::BridgeSlavePort(const std::string& _name,
577997Ssaidi@eecs.umich.edu                                         Bridge& _bridge,
587997Ssaidi@eecs.umich.edu                                         BridgeMasterPort& _masterPort,
597997Ssaidi@eecs.umich.edu                                         Cycles _delay, int _resp_limit,
607997Ssaidi@eecs.umich.edu                                         std::vector<AddrRange> _ranges)
617997Ssaidi@eecs.umich.edu    : SlavePort(_name, &_bridge), bridge(_bridge), masterPort(_masterPort),
627997Ssaidi@eecs.umich.edu      delay(_delay), ranges(_ranges.begin(), _ranges.end()),
637997Ssaidi@eecs.umich.edu      outstandingResponses(0), retryReq(false),
647997Ssaidi@eecs.umich.edu      respQueueLimit(_resp_limit), sendEvent(*this)
657997Ssaidi@eecs.umich.edu{
667997Ssaidi@eecs.umich.edu}
677997Ssaidi@eecs.umich.edu
687997Ssaidi@eecs.umich.eduBridge::BridgeMasterPort::BridgeMasterPort(const std::string& _name,
697997Ssaidi@eecs.umich.edu                                           Bridge& _bridge,
707997Ssaidi@eecs.umich.edu                                           BridgeSlavePort& _slavePort,
717997Ssaidi@eecs.umich.edu                                           Cycles _delay, int _req_limit)
727997Ssaidi@eecs.umich.edu    : MasterPort(_name, &_bridge), bridge(_bridge), slavePort(_slavePort),
737997Ssaidi@eecs.umich.edu      delay(_delay), reqQueueLimit(_req_limit), sendEvent(*this)
747997Ssaidi@eecs.umich.edu{
757997Ssaidi@eecs.umich.edu}
767997Ssaidi@eecs.umich.edu
777997Ssaidi@eecs.umich.eduBridge::Bridge(Params *p)
787997Ssaidi@eecs.umich.edu    : MemObject(p),
797997Ssaidi@eecs.umich.edu      slavePort(p->name + ".slave", *this, masterPort,
807997Ssaidi@eecs.umich.edu                ticksToCycles(p->delay), p->resp_size, p->ranges),
817997Ssaidi@eecs.umich.edu      masterPort(p->name + ".master", *this, slavePort,
827997Ssaidi@eecs.umich.edu                 ticksToCycles(p->delay), p->req_size)
837997Ssaidi@eecs.umich.edu{
847997Ssaidi@eecs.umich.edu}
857997Ssaidi@eecs.umich.edu
867997Ssaidi@eecs.umich.eduBaseMasterPort&
877997Ssaidi@eecs.umich.eduBridge::getMasterPort(const std::string &if_name, PortID idx)
887997Ssaidi@eecs.umich.edu{
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 a bridge must be connected.\n");
112
113    // notify the master side  of our address ranges
114    slavePort.sendRangeChange();
115}
116
117bool
118Bridge::BridgeSlavePort::respQueueFull() const
119{
120    return outstandingResponses == respQueueLimit;
121}
122
123bool
124Bridge::BridgeMasterPort::reqQueueFull() const
125{
126    return transmitList.size() == reqQueueLimit;
127}
128
129bool
130Bridge::BridgeMasterPort::recvTimingResp(PacketPtr pkt)
131{
132    // all checks are done when the request is accepted on the slave
133    // side, so we are guaranteed to have space for the response
134    DPRINTF(Bridge, "recvTimingResp: %s addr 0x%x\n",
135            pkt->cmdString(), pkt->getAddr());
136
137    DPRINTF(Bridge, "Request queue size: %d\n", transmitList.size());
138
139    // technically the packet only reaches us after the header delay,
140    // and typically we also need to deserialise any payload (unless
141    // the two sides of the bridge are synchronous)
142    Tick receive_delay = pkt->headerDelay + pkt->payloadDelay;
143    pkt->headerDelay = pkt->payloadDelay = 0;
144
145    slavePort.schedTimingResp(pkt, bridge.clockEdge(delay) +
146                              receive_delay);
147
148    return true;
149}
150
151bool
152Bridge::BridgeSlavePort::recvTimingReq(PacketPtr pkt)
153{
154    DPRINTF(Bridge, "recvTimingReq: %s addr 0x%x\n",
155            pkt->cmdString(), pkt->getAddr());
156
157    // sink inhibited packets without further action, also discard any
158    // packet that is not a read or a write
159    if (pkt->memInhibitAsserted() ||
160        !(pkt->isWrite() || pkt->isRead())) {
161        assert(!pkt->needsResponse());
162        pendingDelete.reset(pkt);
163        return true;
164    }
165
166    // we should not get a new request after committing to retry the
167    // current one, but unfortunately the CPU violates this rule, so
168    // simply ignore it for now
169    if (retryReq)
170        return false;
171
172    DPRINTF(Bridge, "Response queue size: %d outresp: %d\n",
173            transmitList.size(), outstandingResponses);
174
175    // if the request queue is full then there is no hope
176    if (masterPort.reqQueueFull()) {
177        DPRINTF(Bridge, "Request queue full\n");
178        retryReq = true;
179    } else {
180        // look at the response queue if we expect to see a response
181        bool expects_response = pkt->needsResponse();
182        if (expects_response) {
183            if (respQueueFull()) {
184                DPRINTF(Bridge, "Response queue full\n");
185                retryReq = true;
186            } else {
187                // ok to send the request with space for the response
188                DPRINTF(Bridge, "Reserving space for response\n");
189                assert(outstandingResponses != respQueueLimit);
190                ++outstandingResponses;
191
192                // no need to set retryReq to false as this is already the
193                // case
194            }
195        }
196
197        if (!retryReq) {
198            // technically the packet only reaches us after the header
199            // delay, and typically we also need to deserialise any
200            // payload (unless the two sides of the bridge are
201            // synchronous)
202            Tick receive_delay = pkt->headerDelay + pkt->payloadDelay;
203            pkt->headerDelay = pkt->payloadDelay = 0;
204
205            masterPort.schedTimingReq(pkt, bridge.clockEdge(delay) +
206                                      receive_delay);
207        }
208    }
209
210    // remember that we are now stalling a packet and that we have to
211    // tell the sending master to retry once space becomes available,
212    // we make no distinction whether the stalling is due to the
213    // request queue or response queue being full
214    return !retryReq;
215}
216
217void
218Bridge::BridgeSlavePort::retryStalledReq()
219{
220    if (retryReq) {
221        DPRINTF(Bridge, "Request waiting for retry, now retrying\n");
222        retryReq = false;
223        sendRetryReq();
224    }
225}
226
227void
228Bridge::BridgeMasterPort::schedTimingReq(PacketPtr pkt, Tick when)
229{
230    // If we're about to put this packet at the head of the queue, we
231    // need to schedule an event to do the transmit.  Otherwise there
232    // should already be an event scheduled for sending the head
233    // packet.
234    if (transmitList.empty()) {
235        bridge.schedule(sendEvent, when);
236    }
237
238    assert(transmitList.size() != reqQueueLimit);
239
240    transmitList.emplace_back(pkt, when);
241}
242
243
244void
245Bridge::BridgeSlavePort::schedTimingResp(PacketPtr pkt, Tick when)
246{
247    // If we're about to put this packet at the head of the queue, we
248    // need to schedule an event to do the transmit.  Otherwise there
249    // should already be an event scheduled for sending the head
250    // packet.
251    if (transmitList.empty()) {
252        bridge.schedule(sendEvent, when);
253    }
254
255    transmitList.emplace_back(pkt, when);
256}
257
258void
259Bridge::BridgeMasterPort::trySendTiming()
260{
261    assert(!transmitList.empty());
262
263    DeferredPacket req = transmitList.front();
264
265    assert(req.tick <= curTick());
266
267    PacketPtr pkt = req.pkt;
268
269    DPRINTF(Bridge, "trySend request addr 0x%x, queue size %d\n",
270            pkt->getAddr(), transmitList.size());
271
272    if (sendTimingReq(pkt)) {
273        // send successful
274        transmitList.pop_front();
275        DPRINTF(Bridge, "trySend request successful\n");
276
277        // If there are more packets to send, schedule event to try again.
278        if (!transmitList.empty()) {
279            DeferredPacket next_req = transmitList.front();
280            DPRINTF(Bridge, "Scheduling next send\n");
281            bridge.schedule(sendEvent, std::max(next_req.tick,
282                                                bridge.clockEdge()));
283        }
284
285        // if we have stalled a request due to a full request queue,
286        // then send a retry at this point, also note that if the
287        // request we stalled was waiting for the response queue
288        // rather than the request queue we might stall it again
289        slavePort.retryStalledReq();
290    }
291
292    // if the send failed, then we try again once we receive a retry,
293    // and therefore there is no need to take any action
294}
295
296void
297Bridge::BridgeSlavePort::trySendTiming()
298{
299    assert(!transmitList.empty());
300
301    DeferredPacket resp = transmitList.front();
302
303    assert(resp.tick <= curTick());
304
305    PacketPtr pkt = resp.pkt;
306
307    DPRINTF(Bridge, "trySend response addr 0x%x, outstanding %d\n",
308            pkt->getAddr(), outstandingResponses);
309
310    if (sendTimingResp(pkt)) {
311        // send successful
312        transmitList.pop_front();
313        DPRINTF(Bridge, "trySend response successful\n");
314
315        assert(outstandingResponses != 0);
316        --outstandingResponses;
317
318        // If there are more packets to send, schedule event to try again.
319        if (!transmitList.empty()) {
320            DeferredPacket next_resp = transmitList.front();
321            DPRINTF(Bridge, "Scheduling next send\n");
322            bridge.schedule(sendEvent, std::max(next_resp.tick,
323                                                bridge.clockEdge()));
324        }
325
326        // if there is space in the request queue and we were stalling
327        // a request, it will definitely be possible to accept it now
328        // since there is guaranteed space in the response queue
329        if (!masterPort.reqQueueFull() && retryReq) {
330            DPRINTF(Bridge, "Request waiting for retry, now retrying\n");
331            retryReq = false;
332            sendRetryReq();
333        }
334    }
335
336    // if the send failed, then we try again once we receive a retry,
337    // and therefore there is no need to take any action
338}
339
340void
341Bridge::BridgeMasterPort::recvReqRetry()
342{
343    trySendTiming();
344}
345
346void
347Bridge::BridgeSlavePort::recvRespRetry()
348{
349    trySendTiming();
350}
351
352Tick
353Bridge::BridgeSlavePort::recvAtomic(PacketPtr pkt)
354{
355    return delay * bridge.clockPeriod() + masterPort.sendAtomic(pkt);
356}
357
358void
359Bridge::BridgeSlavePort::recvFunctional(PacketPtr pkt)
360{
361    pkt->pushLabel(name());
362
363    // check the response queue
364    for (auto i = transmitList.begin();  i != transmitList.end(); ++i) {
365        if (pkt->checkFunctional((*i).pkt)) {
366            pkt->makeResponse();
367            return;
368        }
369    }
370
371    // also check the master port's request queue
372    if (masterPort.checkFunctional(pkt)) {
373        return;
374    }
375
376    pkt->popLabel();
377
378    // fall through if pkt still not satisfied
379    masterPort.sendFunctional(pkt);
380}
381
382bool
383Bridge::BridgeMasterPort::checkFunctional(PacketPtr pkt)
384{
385    bool found = false;
386    auto i = transmitList.begin();
387
388    while(i != transmitList.end() && !found) {
389        if (pkt->checkFunctional((*i).pkt)) {
390            pkt->makeResponse();
391            found = true;
392        }
393        ++i;
394    }
395
396    return found;
397}
398
399AddrRangeList
400Bridge::BridgeSlavePort::getAddrRanges() const
401{
402    return ranges;
403}
404
405Bridge *
406BridgeParams::create()
407{
408    return new Bridge(this);
409}
410