2 * Copyright (c) 2011-2012 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 bus bridge that connects a master
48 * and a slave through a request and response queue.
51 #include "base/trace.hh"
52 #include "debug/BusBridge.hh"
53 #include "mem/bridge.hh"
54 #include "params/Bridge.hh"
56 Bridge::BridgeSlavePort::BridgeSlavePort(const std::string
&_name
,
58 BridgeMasterPort
& _masterPort
,
59 int _delay
, int _nack_delay
,
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)
70 Bridge::BridgeMasterPort::BridgeMasterPort(const std::string
&_name
,
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
),
80 Bridge::Bridge(Params
*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
)
88 panic("No support for acknowledging writes\n");
92 Bridge::getMasterPort(const std::string
&if_name
, int idx
)
94 if (if_name
== "master")
97 // pass it along to our super class
98 return MemObject::getMasterPort(if_name
, idx
);
102 Bridge::getSlavePort(const std::string
&if_name
, int idx
)
104 if (if_name
== "slave")
107 // pass it along to our super class
108 return MemObject::getSlavePort(if_name
, idx
);
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");
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());
123 // notify the master side of our address ranges
124 slavePort
.sendRangeChange();
128 Bridge::BridgeSlavePort::respQueueFull()
130 return outstandingResponses
== respQueueLimit
;
134 Bridge::BridgeMasterPort::reqQueueFull()
136 return requestQueue
.size() == reqQueueLimit
;
140 Bridge::BridgeMasterPort::recvTiming(PacketPtr pkt
)
142 // should only see responses on the master side
143 assert(pkt
->isResponse());
145 // all checks are done when the request is accepted on the slave
146 // side, so we are guaranteed to have space for the response
147 DPRINTF(BusBridge
, "recvTiming: response %s addr 0x%x\n",
148 pkt
->cmdString(), pkt
->getAddr());
150 DPRINTF(BusBridge
, "Request queue size: %d\n", requestQueue
.size());
152 slavePort
.queueForSendTiming(pkt
);
158 Bridge::BridgeSlavePort::recvTiming(PacketPtr pkt
)
160 // should only see requests on the slave side
161 assert(pkt
->isRequest());
164 DPRINTF(BusBridge
, "recvTiming: request %s addr 0x%x\n",
165 pkt
->cmdString(), pkt
->getAddr());
167 DPRINTF(BusBridge
, "Response queue size: %d outresp: %d\n",
168 responseQueue
.size(), outstandingResponses
);
170 if (masterPort
.reqQueueFull()) {
171 DPRINTF(BusBridge
, "Request queue full, nacking\n");
176 if (pkt
->needsResponse()) {
177 if (respQueueFull()) {
179 "Response queue full, no space for response, nacking\n");
181 "queue size: %d outstanding resp: %d\n",
182 responseQueue
.size(), outstandingResponses
);
186 DPRINTF(BusBridge
, "Request Needs response, reserving space\n");
187 assert(outstandingResponses
!= respQueueLimit
);
188 ++outstandingResponses
;
192 masterPort
.queueForSendTiming(pkt
);
198 Bridge::BridgeSlavePort::nackRequest(PacketPtr pkt
)
201 pkt
->makeTimingResponse();
204 // The Nack packets are stored in the response queue just like any
205 // other response, but they do not occupy any space as this is
206 // tracked by the outstandingResponses, this guarantees space for
207 // the Nack packets, but implicitly means we have an (unrealistic)
208 // unbounded Nack queue.
210 // put it on the list to send
211 Tick readyTime
= curTick() + nackDelay
;
212 PacketBuffer
*buf
= new PacketBuffer(pkt
, readyTime
, true);
214 // nothing on the list, add it and we're done
215 if (responseQueue
.empty()) {
216 assert(!sendEvent
.scheduled());
217 bridge
->schedule(sendEvent
, readyTime
);
218 responseQueue
.push_back(buf
);
222 assert(sendEvent
.scheduled() || inRetry
);
224 // does it go at the end?
225 if (readyTime
>= responseQueue
.back()->ready
) {
226 responseQueue
.push_back(buf
);
230 // ok, somewhere in the middle, fun
231 std::list
<PacketBuffer
*>::iterator i
= responseQueue
.begin();
232 std::list
<PacketBuffer
*>::iterator end
= responseQueue
.end();
233 std::list
<PacketBuffer
*>::iterator begin
= responseQueue
.begin();
236 while (i
!= end
&& !done
) {
237 if (readyTime
< (*i
)->ready
) {
239 bridge
->reschedule(sendEvent
, readyTime
);
240 responseQueue
.insert(i
,buf
);
249 Bridge::BridgeMasterPort::queueForSendTiming(PacketPtr pkt
)
251 Tick readyTime
= curTick() + delay
;
252 PacketBuffer
*buf
= new PacketBuffer(pkt
, readyTime
);
254 // If we're about to put this packet at the head of the queue, we
255 // need to schedule an event to do the transmit. Otherwise there
256 // should already be an event scheduled for sending the head
258 if (requestQueue
.empty()) {
259 bridge
->schedule(sendEvent
, readyTime
);
262 assert(requestQueue
.size() != reqQueueLimit
);
264 requestQueue
.push_back(buf
);
269 Bridge::BridgeSlavePort::queueForSendTiming(PacketPtr pkt
)
271 // This is a response for a request we forwarded earlier. The
272 // corresponding PacketBuffer should be stored in the packet's
273 // senderState field.
274 PacketBuffer
*buf
= dynamic_cast<PacketBuffer
*>(pkt
->senderState
);
276 // set up new packet dest & senderState based on values saved
277 // from original request
278 buf
->fixResponse(pkt
);
280 // the bridge assumes that at least one bus has set the
281 // destination field of the packet
282 assert(pkt
->isDestValid());
283 DPRINTF(BusBridge
, "response, new dest %d\n", pkt
->getDest());
286 Tick readyTime
= curTick() + delay
;
287 buf
= new PacketBuffer(pkt
, readyTime
);
289 // If we're about to put this packet at the head of the queue, we
290 // need to schedule an event to do the transmit. Otherwise there
291 // should already be an event scheduled for sending the head
293 if (responseQueue
.empty()) {
294 bridge
->schedule(sendEvent
, readyTime
);
296 responseQueue
.push_back(buf
);
300 Bridge::BridgeMasterPort::trySend()
302 assert(!requestQueue
.empty());
304 PacketBuffer
*buf
= requestQueue
.front();
306 assert(buf
->ready
<= curTick());
308 PacketPtr pkt
= buf
->pkt
;
310 DPRINTF(BusBridge
, "trySend: origSrc %d addr 0x%x\n",
311 buf
->origSrc
, pkt
->getAddr());
313 // If the send was successful, make sure sender state was set to NULL
314 // otherwise we could get a NACK back of a packet that didn't expect a
315 // response and we would try to use freed memory.
317 Packet::SenderState
*old_sender_state
= pkt
->senderState
;
318 if (!buf
->expectResponse
)
319 pkt
->senderState
= NULL
;
321 if (sendTiming(pkt
)) {
323 requestQueue
.pop_front();
324 // we no longer own packet, so it's not safe to look at it
327 if (!buf
->expectResponse
) {
328 // no response expected... deallocate packet buffer now.
329 DPRINTF(BusBridge
, " successful: no response expected\n");
333 // If there are more packets to send, schedule event to try again.
334 if (!requestQueue
.empty()) {
335 buf
= requestQueue
.front();
336 DPRINTF(BusBridge
, "Scheduling next send\n");
337 bridge
->schedule(sendEvent
, std::max(buf
->ready
, curTick() + 1));
340 DPRINTF(BusBridge
, " unsuccessful\n");
341 pkt
->senderState
= old_sender_state
;
345 DPRINTF(BusBridge
, "trySend: request queue size: %d\n",
346 requestQueue
.size());
350 Bridge::BridgeSlavePort::trySend()
352 assert(!responseQueue
.empty());
354 PacketBuffer
*buf
= responseQueue
.front();
356 assert(buf
->ready
<= curTick());
358 PacketPtr pkt
= buf
->pkt
;
360 DPRINTF(BusBridge
, "trySend: origSrc %d dest %d addr 0x%x\n",
361 buf
->origSrc
, pkt
->getDest(), pkt
->getAddr());
363 bool was_nacked_here
= buf
->nackedHere
;
365 // no need to worry about the sender state since we are not
368 if (sendTiming(pkt
)) {
369 DPRINTF(BusBridge
, " successful\n");
371 responseQueue
.pop_front();
372 // this is a response... deallocate packet buffer now.
375 if (!was_nacked_here
) {
376 assert(outstandingResponses
!= 0);
377 --outstandingResponses
;
380 // If there are more packets to send, schedule event to try again.
381 if (!responseQueue
.empty()) {
382 buf
= responseQueue
.front();
383 DPRINTF(BusBridge
, "Scheduling next send\n");
384 bridge
->schedule(sendEvent
, std::max(buf
->ready
, curTick() + 1));
387 DPRINTF(BusBridge
, " unsuccessful\n");
391 DPRINTF(BusBridge
, "trySend: queue size: %d outstanding resp: %d\n",
392 responseQueue
.size(), outstandingResponses
);
396 Bridge::BridgeMasterPort::recvRetry()
399 Tick nextReady
= requestQueue
.front()->ready
;
400 if (nextReady
<= curTick())
403 bridge
->schedule(sendEvent
, nextReady
);
407 Bridge::BridgeSlavePort::recvRetry()
410 Tick nextReady
= responseQueue
.front()->ready
;
411 if (nextReady
<= curTick())
414 bridge
->schedule(sendEvent
, nextReady
);
418 Bridge::BridgeSlavePort::recvAtomic(PacketPtr pkt
)
420 return delay
+ masterPort
.sendAtomic(pkt
);
424 Bridge::BridgeSlavePort::recvFunctional(PacketPtr pkt
)
426 std::list
<PacketBuffer
*>::iterator i
;
428 pkt
->pushLabel(name());
430 // check the response queue
431 for (i
= responseQueue
.begin(); i
!= responseQueue
.end(); ++i
) {
432 if (pkt
->checkFunctional((*i
)->pkt
)) {
438 // also check the master port's request queue
439 if (masterPort
.checkFunctional(pkt
)) {
445 // fall through if pkt still not satisfied
446 masterPort
.sendFunctional(pkt
);
450 Bridge::BridgeMasterPort::checkFunctional(PacketPtr pkt
)
453 std::list
<PacketBuffer
*>::iterator i
= requestQueue
.begin();
455 while(i
!= requestQueue
.end() && !found
) {
456 if (pkt
->checkFunctional((*i
)->pkt
)) {
467 Bridge::BridgeSlavePort::getAddrRanges()
473 BridgeParams::create()
475 return new Bridge(this);