2 * Copyright (c) 2011-2013, 2015 ARM Limited
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.
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
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.
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.
47 * Implementation of a memory-mapped bridge that connects a master
48 * and a slave through a request and response queue.
51 #include "mem/bridge.hh"
53 #include "base/trace.hh"
54 #include "debug/Bridge.hh"
55 #include "params/Bridge.hh"
57 Bridge::BridgeSlavePort::BridgeSlavePort(const std::string
& _name
,
59 BridgeMasterPort
& _masterPort
,
60 Cycles _delay
, int _resp_limit
,
61 std::vector
<AddrRange
> _ranges
)
62 : SlavePort(_name
, &_bridge
), bridge(_bridge
), masterPort(_masterPort
),
63 delay(_delay
), ranges(_ranges
.begin(), _ranges
.end()),
64 outstandingResponses(0), retryReq(false), respQueueLimit(_resp_limit
),
65 sendEvent([this]{ trySendTiming(); }, _name
)
69 Bridge::BridgeMasterPort::BridgeMasterPort(const std::string
& _name
,
71 BridgeSlavePort
& _slavePort
,
72 Cycles _delay
, int _req_limit
)
73 : MasterPort(_name
, &_bridge
), bridge(_bridge
), slavePort(_slavePort
),
74 delay(_delay
), reqQueueLimit(_req_limit
),
75 sendEvent([this]{ trySendTiming(); }, _name
)
79 Bridge::Bridge(Params
*p
)
81 slavePort(p
->name
+ ".slave", *this, masterPort
,
82 ticksToCycles(p
->delay
), p
->resp_size
, p
->ranges
),
83 masterPort(p
->name
+ ".master", *this, slavePort
,
84 ticksToCycles(p
->delay
), p
->req_size
)
89 Bridge::getPort(const std::string
&if_name
, PortID idx
)
91 if (if_name
== "master")
93 else if (if_name
== "slave")
96 // pass it along to our super class
97 return MemObject::getPort(if_name
, idx
);
103 // make sure both sides are connected and have the same block size
104 if (!slavePort
.isConnected() || !masterPort
.isConnected())
105 fatal("Both ports of a bridge must be connected.\n");
107 // notify the master side of our address ranges
108 slavePort
.sendRangeChange();
112 Bridge::BridgeSlavePort::respQueueFull() const
114 return outstandingResponses
== respQueueLimit
;
118 Bridge::BridgeMasterPort::reqQueueFull() const
120 return transmitList
.size() == reqQueueLimit
;
124 Bridge::BridgeMasterPort::recvTimingResp(PacketPtr pkt
)
126 // all checks are done when the request is accepted on the slave
127 // side, so we are guaranteed to have space for the response
128 DPRINTF(Bridge
, "recvTimingResp: %s addr 0x%x\n",
129 pkt
->cmdString(), pkt
->getAddr());
131 DPRINTF(Bridge
, "Request queue size: %d\n", transmitList
.size());
133 // technically the packet only reaches us after the header delay,
134 // and typically we also need to deserialise any payload (unless
135 // the two sides of the bridge are synchronous)
136 Tick receive_delay
= pkt
->headerDelay
+ pkt
->payloadDelay
;
137 pkt
->headerDelay
= pkt
->payloadDelay
= 0;
139 slavePort
.schedTimingResp(pkt
, bridge
.clockEdge(delay
) +
146 Bridge::BridgeSlavePort::recvTimingReq(PacketPtr pkt
)
148 DPRINTF(Bridge
, "recvTimingReq: %s addr 0x%x\n",
149 pkt
->cmdString(), pkt
->getAddr());
151 panic_if(pkt
->cacheResponding(), "Should not see packets where cache "
154 // we should not get a new request after committing to retry the
155 // current one, but unfortunately the CPU violates this rule, so
156 // simply ignore it for now
160 DPRINTF(Bridge
, "Response queue size: %d outresp: %d\n",
161 transmitList
.size(), outstandingResponses
);
163 // if the request queue is full then there is no hope
164 if (masterPort
.reqQueueFull()) {
165 DPRINTF(Bridge
, "Request queue full\n");
168 // look at the response queue if we expect to see a response
169 bool expects_response
= pkt
->needsResponse();
170 if (expects_response
) {
171 if (respQueueFull()) {
172 DPRINTF(Bridge
, "Response queue full\n");
175 // ok to send the request with space for the response
176 DPRINTF(Bridge
, "Reserving space for response\n");
177 assert(outstandingResponses
!= respQueueLimit
);
178 ++outstandingResponses
;
180 // no need to set retryReq to false as this is already the
186 // technically the packet only reaches us after the header
187 // delay, and typically we also need to deserialise any
188 // payload (unless the two sides of the bridge are
190 Tick receive_delay
= pkt
->headerDelay
+ pkt
->payloadDelay
;
191 pkt
->headerDelay
= pkt
->payloadDelay
= 0;
193 masterPort
.schedTimingReq(pkt
, bridge
.clockEdge(delay
) +
198 // remember that we are now stalling a packet and that we have to
199 // tell the sending master to retry once space becomes available,
200 // we make no distinction whether the stalling is due to the
201 // request queue or response queue being full
206 Bridge::BridgeSlavePort::retryStalledReq()
209 DPRINTF(Bridge
, "Request waiting for retry, now retrying\n");
216 Bridge::BridgeMasterPort::schedTimingReq(PacketPtr pkt
, Tick when
)
218 // If we're about to put this packet at the head of the queue, we
219 // need to schedule an event to do the transmit. Otherwise there
220 // should already be an event scheduled for sending the head
222 if (transmitList
.empty()) {
223 bridge
.schedule(sendEvent
, when
);
226 assert(transmitList
.size() != reqQueueLimit
);
228 transmitList
.emplace_back(pkt
, when
);
233 Bridge::BridgeSlavePort::schedTimingResp(PacketPtr pkt
, Tick when
)
235 // If we're about to put this packet at the head of the queue, we
236 // need to schedule an event to do the transmit. Otherwise there
237 // should already be an event scheduled for sending the head
239 if (transmitList
.empty()) {
240 bridge
.schedule(sendEvent
, when
);
243 transmitList
.emplace_back(pkt
, when
);
247 Bridge::BridgeMasterPort::trySendTiming()
249 assert(!transmitList
.empty());
251 DeferredPacket req
= transmitList
.front();
253 assert(req
.tick
<= curTick());
255 PacketPtr pkt
= req
.pkt
;
257 DPRINTF(Bridge
, "trySend request addr 0x%x, queue size %d\n",
258 pkt
->getAddr(), transmitList
.size());
260 if (sendTimingReq(pkt
)) {
262 transmitList
.pop_front();
263 DPRINTF(Bridge
, "trySend request successful\n");
265 // If there are more packets to send, schedule event to try again.
266 if (!transmitList
.empty()) {
267 DeferredPacket next_req
= transmitList
.front();
268 DPRINTF(Bridge
, "Scheduling next send\n");
269 bridge
.schedule(sendEvent
, std::max(next_req
.tick
,
270 bridge
.clockEdge()));
273 // if we have stalled a request due to a full request queue,
274 // then send a retry at this point, also note that if the
275 // request we stalled was waiting for the response queue
276 // rather than the request queue we might stall it again
277 slavePort
.retryStalledReq();
280 // if the send failed, then we try again once we receive a retry,
281 // and therefore there is no need to take any action
285 Bridge::BridgeSlavePort::trySendTiming()
287 assert(!transmitList
.empty());
289 DeferredPacket resp
= transmitList
.front();
291 assert(resp
.tick
<= curTick());
293 PacketPtr pkt
= resp
.pkt
;
295 DPRINTF(Bridge
, "trySend response addr 0x%x, outstanding %d\n",
296 pkt
->getAddr(), outstandingResponses
);
298 if (sendTimingResp(pkt
)) {
300 transmitList
.pop_front();
301 DPRINTF(Bridge
, "trySend response successful\n");
303 assert(outstandingResponses
!= 0);
304 --outstandingResponses
;
306 // If there are more packets to send, schedule event to try again.
307 if (!transmitList
.empty()) {
308 DeferredPacket next_resp
= transmitList
.front();
309 DPRINTF(Bridge
, "Scheduling next send\n");
310 bridge
.schedule(sendEvent
, std::max(next_resp
.tick
,
311 bridge
.clockEdge()));
314 // if there is space in the request queue and we were stalling
315 // a request, it will definitely be possible to accept it now
316 // since there is guaranteed space in the response queue
317 if (!masterPort
.reqQueueFull() && retryReq
) {
318 DPRINTF(Bridge
, "Request waiting for retry, now retrying\n");
324 // if the send failed, then we try again once we receive a retry,
325 // and therefore there is no need to take any action
329 Bridge::BridgeMasterPort::recvReqRetry()
335 Bridge::BridgeSlavePort::recvRespRetry()
341 Bridge::BridgeSlavePort::recvAtomic(PacketPtr pkt
)
343 panic_if(pkt
->cacheResponding(), "Should not see packets where cache "
346 return delay
* bridge
.clockPeriod() + masterPort
.sendAtomic(pkt
);
350 Bridge::BridgeSlavePort::recvFunctional(PacketPtr pkt
)
352 pkt
->pushLabel(name());
354 // check the response queue
355 for (auto i
= transmitList
.begin(); i
!= transmitList
.end(); ++i
) {
356 if (pkt
->trySatisfyFunctional((*i
).pkt
)) {
362 // also check the master port's request queue
363 if (masterPort
.trySatisfyFunctional(pkt
)) {
369 // fall through if pkt still not satisfied
370 masterPort
.sendFunctional(pkt
);
374 Bridge::BridgeMasterPort::trySatisfyFunctional(PacketPtr pkt
)
377 auto i
= transmitList
.begin();
379 while (i
!= transmitList
.end() && !found
) {
380 if (pkt
->trySatisfyFunctional((*i
).pkt
)) {
391 Bridge::BridgeSlavePort::getAddrRanges() const
397 BridgeParams::create()
399 return new Bridge(this);