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.
43 * Implementation of a memory-mapped bridge that connects a requestor
44 * and a responder through a request and response queue.
47 #include "mem/bridge.hh"
49 #include "base/trace.hh"
50 #include "debug/Bridge.hh"
51 #include "params/Bridge.hh"
53 Bridge::BridgeResponsePort::BridgeResponsePort(const std::string
& _name
,
55 BridgeRequestPort
& _memSidePort
,
56 Cycles _delay
, int _resp_limit
,
57 std::vector
<AddrRange
> _ranges
)
58 : ResponsePort(_name
, &_bridge
), bridge(_bridge
),
59 memSidePort(_memSidePort
), delay(_delay
),
60 ranges(_ranges
.begin(), _ranges
.end()),
61 outstandingResponses(0), retryReq(false), respQueueLimit(_resp_limit
),
62 sendEvent([this]{ trySendTiming(); }, _name
)
66 Bridge::BridgeRequestPort::BridgeRequestPort(const std::string
& _name
,
68 BridgeResponsePort
& _cpuSidePort
,
69 Cycles _delay
, int _req_limit
)
70 : RequestPort(_name
, &_bridge
), bridge(_bridge
),
71 cpuSidePort(_cpuSidePort
),
72 delay(_delay
), reqQueueLimit(_req_limit
),
73 sendEvent([this]{ trySendTiming(); }, _name
)
77 Bridge::Bridge(Params
*p
)
79 cpuSidePort(p
->name
+ ".cpu_side_port", *this, memSidePort
,
80 ticksToCycles(p
->delay
), p
->resp_size
, p
->ranges
),
81 memSidePort(p
->name
+ ".mem_side_port", *this, cpuSidePort
,
82 ticksToCycles(p
->delay
), p
->req_size
)
87 Bridge::getPort(const std::string
&if_name
, PortID idx
)
89 if (if_name
== "mem_side_port")
91 else if (if_name
== "cpu_side_port")
94 // pass it along to our super class
95 return ClockedObject::getPort(if_name
, idx
);
101 // make sure both sides are connected and have the same block size
102 if (!cpuSidePort
.isConnected() || !memSidePort
.isConnected())
103 fatal("Both ports of a bridge must be connected.\n");
105 // notify the request side of our address ranges
106 cpuSidePort
.sendRangeChange();
110 Bridge::BridgeResponsePort::respQueueFull() const
112 return outstandingResponses
== respQueueLimit
;
116 Bridge::BridgeRequestPort::reqQueueFull() const
118 return transmitList
.size() == reqQueueLimit
;
122 Bridge::BridgeRequestPort::recvTimingResp(PacketPtr pkt
)
124 // all checks are done when the request is accepted on the response
125 // side, so we are guaranteed to have space for the response
126 DPRINTF(Bridge
, "recvTimingResp: %s addr 0x%x\n",
127 pkt
->cmdString(), pkt
->getAddr());
129 DPRINTF(Bridge
, "Request queue size: %d\n", transmitList
.size());
131 // technically the packet only reaches us after the header delay,
132 // and typically we also need to deserialise any payload (unless
133 // the two sides of the bridge are synchronous)
134 Tick receive_delay
= pkt
->headerDelay
+ pkt
->payloadDelay
;
135 pkt
->headerDelay
= pkt
->payloadDelay
= 0;
137 cpuSidePort
.schedTimingResp(pkt
, bridge
.clockEdge(delay
) +
144 Bridge::BridgeResponsePort::recvTimingReq(PacketPtr pkt
)
146 DPRINTF(Bridge
, "recvTimingReq: %s addr 0x%x\n",
147 pkt
->cmdString(), pkt
->getAddr());
149 panic_if(pkt
->cacheResponding(), "Should not see packets where cache "
152 // we should not get a new request after committing to retry the
153 // current one, but unfortunately the CPU violates this rule, so
154 // simply ignore it for now
158 DPRINTF(Bridge
, "Response queue size: %d outresp: %d\n",
159 transmitList
.size(), outstandingResponses
);
161 // if the request queue is full then there is no hope
162 if (memSidePort
.reqQueueFull()) {
163 DPRINTF(Bridge
, "Request queue full\n");
166 // look at the response queue if we expect to see a response
167 bool expects_response
= pkt
->needsResponse();
168 if (expects_response
) {
169 if (respQueueFull()) {
170 DPRINTF(Bridge
, "Response queue full\n");
173 // ok to send the request with space for the response
174 DPRINTF(Bridge
, "Reserving space for response\n");
175 assert(outstandingResponses
!= respQueueLimit
);
176 ++outstandingResponses
;
178 // no need to set retryReq to false as this is already the
184 // technically the packet only reaches us after the header
185 // delay, and typically we also need to deserialise any
186 // payload (unless the two sides of the bridge are
188 Tick receive_delay
= pkt
->headerDelay
+ pkt
->payloadDelay
;
189 pkt
->headerDelay
= pkt
->payloadDelay
= 0;
191 memSidePort
.schedTimingReq(pkt
, bridge
.clockEdge(delay
) +
196 // remember that we are now stalling a packet and that we have to
197 // tell the sending requestor to retry once space becomes available,
198 // we make no distinction whether the stalling is due to the
199 // request queue or response queue being full
204 Bridge::BridgeResponsePort::retryStalledReq()
207 DPRINTF(Bridge
, "Request waiting for retry, now retrying\n");
214 Bridge::BridgeRequestPort::schedTimingReq(PacketPtr pkt
, Tick when
)
216 // If we're about to put this packet at the head of the queue, we
217 // need to schedule an event to do the transmit. Otherwise there
218 // should already be an event scheduled for sending the head
220 if (transmitList
.empty()) {
221 bridge
.schedule(sendEvent
, when
);
224 assert(transmitList
.size() != reqQueueLimit
);
226 transmitList
.emplace_back(pkt
, when
);
231 Bridge::BridgeResponsePort::schedTimingResp(PacketPtr pkt
, Tick when
)
233 // If we're about to put this packet at the head of the queue, we
234 // need to schedule an event to do the transmit. Otherwise there
235 // should already be an event scheduled for sending the head
237 if (transmitList
.empty()) {
238 bridge
.schedule(sendEvent
, when
);
241 transmitList
.emplace_back(pkt
, when
);
245 Bridge::BridgeRequestPort::trySendTiming()
247 assert(!transmitList
.empty());
249 DeferredPacket req
= transmitList
.front();
251 assert(req
.tick
<= curTick());
253 PacketPtr pkt
= req
.pkt
;
255 DPRINTF(Bridge
, "trySend request addr 0x%x, queue size %d\n",
256 pkt
->getAddr(), transmitList
.size());
258 if (sendTimingReq(pkt
)) {
260 transmitList
.pop_front();
261 DPRINTF(Bridge
, "trySend request successful\n");
263 // If there are more packets to send, schedule event to try again.
264 if (!transmitList
.empty()) {
265 DeferredPacket next_req
= transmitList
.front();
266 DPRINTF(Bridge
, "Scheduling next send\n");
267 bridge
.schedule(sendEvent
, std::max(next_req
.tick
,
268 bridge
.clockEdge()));
271 // if we have stalled a request due to a full request queue,
272 // then send a retry at this point, also note that if the
273 // request we stalled was waiting for the response queue
274 // rather than the request queue we might stall it again
275 cpuSidePort
.retryStalledReq();
278 // if the send failed, then we try again once we receive a retry,
279 // and therefore there is no need to take any action
283 Bridge::BridgeResponsePort::trySendTiming()
285 assert(!transmitList
.empty());
287 DeferredPacket resp
= transmitList
.front();
289 assert(resp
.tick
<= curTick());
291 PacketPtr pkt
= resp
.pkt
;
293 DPRINTF(Bridge
, "trySend response addr 0x%x, outstanding %d\n",
294 pkt
->getAddr(), outstandingResponses
);
296 if (sendTimingResp(pkt
)) {
298 transmitList
.pop_front();
299 DPRINTF(Bridge
, "trySend response successful\n");
301 assert(outstandingResponses
!= 0);
302 --outstandingResponses
;
304 // If there are more packets to send, schedule event to try again.
305 if (!transmitList
.empty()) {
306 DeferredPacket next_resp
= transmitList
.front();
307 DPRINTF(Bridge
, "Scheduling next send\n");
308 bridge
.schedule(sendEvent
, std::max(next_resp
.tick
,
309 bridge
.clockEdge()));
312 // if there is space in the request queue and we were stalling
313 // a request, it will definitely be possible to accept it now
314 // since there is guaranteed space in the response queue
315 if (!memSidePort
.reqQueueFull() && retryReq
) {
316 DPRINTF(Bridge
, "Request waiting for retry, now retrying\n");
322 // if the send failed, then we try again once we receive a retry,
323 // and therefore there is no need to take any action
327 Bridge::BridgeRequestPort::recvReqRetry()
333 Bridge::BridgeResponsePort::recvRespRetry()
339 Bridge::BridgeResponsePort::recvAtomic(PacketPtr pkt
)
341 panic_if(pkt
->cacheResponding(), "Should not see packets where cache "
344 return delay
* bridge
.clockPeriod() + memSidePort
.sendAtomic(pkt
);
348 Bridge::BridgeResponsePort::recvFunctional(PacketPtr pkt
)
350 pkt
->pushLabel(name());
352 // check the response queue
353 for (auto i
= transmitList
.begin(); i
!= transmitList
.end(); ++i
) {
354 if (pkt
->trySatisfyFunctional((*i
).pkt
)) {
360 // also check the request port's request queue
361 if (memSidePort
.trySatisfyFunctional(pkt
)) {
367 // fall through if pkt still not satisfied
368 memSidePort
.sendFunctional(pkt
);
372 Bridge::BridgeRequestPort::trySatisfyFunctional(PacketPtr pkt
)
375 auto i
= transmitList
.begin();
377 while (i
!= transmitList
.end() && !found
) {
378 if (pkt
->trySatisfyFunctional((*i
).pkt
)) {
389 Bridge::BridgeResponsePort::getAddrRanges() const
395 BridgeParams::create()
397 return new Bridge(this);