3 * Copyright (c) 2006 The Regents of The University of Michigan
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Definition of a simple bus bridge without buffering.
40 #include "base/trace.hh"
41 #include "mem/bridge.hh"
42 #include "sim/builder.hh"
44 Bridge::BridgePort::BridgePort(const std::string
&_name
,
45 Bridge
*_bridge
, BridgePort
*_otherPort
,
46 int _delay
, int _nack_delay
, int _req_limit
,
47 int _resp_limit
, bool fix_partial_write
)
48 : Port(_name
), bridge(_bridge
), otherPort(_otherPort
),
49 delay(_delay
), nackDelay(_nack_delay
), fixPartialWrite(fix_partial_write
),
50 outstandingResponses(0), queuedRequests(0), inRetry(false),
51 reqQueueLimit(_req_limit
), respQueueLimit(_resp_limit
), sendEvent(this)
55 Bridge::Bridge(Params
*p
)
57 portA(p
->name
+ "-portA", this, &portB
, p
->delay
, p
->nack_delay
,
58 p
->req_size_a
, p
->resp_size_a
, p
->fix_partial_write_a
),
59 portB(p
->name
+ "-portB", this, &portA
, p
->delay
, p
->nack_delay
,
60 p
->req_size_b
, p
->resp_size_b
, p
->fix_partial_write_b
),
61 ackWrites(p
->write_ack
), _params(p
)
64 panic("No support for acknowledging writes\n");
68 Bridge::getPort(const std::string
&if_name
, int idx
)
72 if (if_name
== "side_a")
74 else if (if_name
== "side_b")
79 if (port
->getPeer() != NULL
)
80 panic("bridge side %s already connected to.", if_name
);
88 // Make sure that both sides are connected to.
89 if (portA
.getPeer() == NULL
|| portB
.getPeer() == NULL
)
90 fatal("Both ports of bus bridge are not connected to a bus.\n");
92 if (portA
.peerBlockSize() != portB
.peerBlockSize())
93 fatal("Busses don't have the same block size... Not supported.\n");
97 Bridge::BridgePort::respQueueFull()
99 assert(outstandingResponses
>= 0 && outstandingResponses
<= respQueueLimit
);
100 return outstandingResponses
>= respQueueLimit
;
104 Bridge::BridgePort::reqQueueFull()
106 assert(queuedRequests
>= 0 && queuedRequests
<= reqQueueLimit
);
107 return queuedRequests
>= reqQueueLimit
;
110 /** Function called by the port when the bus is receiving a Timing
113 Bridge::BridgePort::recvTiming(PacketPtr pkt
)
115 if (!(pkt
->flags
& SNOOP_COMMIT
))
119 DPRINTF(BusBridge
, "recvTiming: src %d dest %d addr 0x%x\n",
120 pkt
->getSrc(), pkt
->getDest(), pkt
->getAddr());
122 DPRINTF(BusBridge
, "Local queue size: %d outreq: %d outresp: %d\n",
123 sendQueue
.size(), queuedRequests
, outstandingResponses
);
124 DPRINTF(BusBridge
, "Remove queue size: %d outreq: %d outresp: %d\n",
125 otherPort
->sendQueue
.size(), otherPort
->queuedRequests
,
126 otherPort
->outstandingResponses
);
128 if (pkt
->isRequest() && otherPort
->reqQueueFull() && pkt
->result
!=
130 DPRINTF(BusBridge
, "Remote queue full, nacking\n");
135 if (pkt
->needsResponse() && pkt
->result
!= Packet::Nacked
)
136 if (respQueueFull()) {
137 DPRINTF(BusBridge
, "Local queue full, no space for response, nacking\n");
138 DPRINTF(BusBridge
, "queue size: %d outreq: %d outstanding resp: %d\n",
139 sendQueue
.size(), queuedRequests
, outstandingResponses
);
143 DPRINTF(BusBridge
, "Request Needs response, reserving space\n");
144 ++outstandingResponses
;
147 otherPort
->queueForSendTiming(pkt
);
153 Bridge::BridgePort::nackRequest(PacketPtr pkt
)
156 pkt
->result
= Packet::Nacked
;
157 pkt
->setDest(pkt
->getSrc());
159 //put it on the list to send
160 Tick readyTime
= curTick
+ nackDelay
;
161 PacketBuffer
*buf
= new PacketBuffer(pkt
, readyTime
, true);
163 // nothing on the list, add it and we're done
164 if (sendQueue
.empty()) {
165 assert(!sendEvent
.scheduled());
166 sendEvent
.schedule(readyTime
);
167 sendQueue
.push_back(buf
);
171 assert(sendEvent
.scheduled() || inRetry
);
173 // does it go at the end?
174 if (readyTime
>= sendQueue
.back()->ready
) {
175 sendQueue
.push_back(buf
);
179 // ok, somewhere in the middle, fun
180 std::list
<PacketBuffer
*>::iterator i
= sendQueue
.begin();
181 std::list
<PacketBuffer
*>::iterator end
= sendQueue
.end();
182 std::list
<PacketBuffer
*>::iterator begin
= sendQueue
.begin();
185 while (i
!= end
&& !done
) {
186 if (readyTime
< (*i
)->ready
) {
188 sendEvent
.reschedule(readyTime
);
189 sendQueue
.insert(i
,buf
);
199 Bridge::BridgePort::queueForSendTiming(PacketPtr pkt
)
201 if (pkt
->isResponse() || pkt
->result
== Packet::Nacked
) {
202 // This is a response for a request we forwarded earlier. The
203 // corresponding PacketBuffer should be stored in the packet's
204 // senderState field.
205 PacketBuffer
*buf
= dynamic_cast<PacketBuffer
*>(pkt
->senderState
);
207 // set up new packet dest & senderState based on values saved
208 // from original request
209 buf
->fixResponse(pkt
);
211 // Check if this packet was expecting a response and it's a nacked
212 // packet, in which case we will never being seeing it
213 if (buf
->expectResponse
&& pkt
->result
== Packet::Nacked
)
214 --outstandingResponses
;
217 DPRINTF(BusBridge
, "restoring sender state: %#X, from packet buffer: %#X\n",
218 pkt
->senderState
, buf
);
219 DPRINTF(BusBridge
, " is response, new dest %d\n", pkt
->getDest());
224 if (pkt
->isRequest() && pkt
->result
!= Packet::Nacked
) {
230 Tick readyTime
= curTick
+ delay
;
231 PacketBuffer
*buf
= new PacketBuffer(pkt
, readyTime
);
232 DPRINTF(BusBridge
, "old sender state: %#X, new sender state: %#X\n",
233 buf
->origSenderState
, buf
);
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 (sendQueue
.empty()) {
240 sendEvent
.schedule(readyTime
);
242 sendQueue
.push_back(buf
);
246 Bridge::BridgePort::trySend()
248 assert(!sendQueue
.empty());
250 PacketBuffer
*buf
= sendQueue
.front();
252 assert(buf
->ready
<= curTick
);
254 PacketPtr pkt
= buf
->pkt
;
256 pkt
->flags
&= ~SNOOP_COMMIT
; //CLear it if it was set
258 // Ugly! @todo When multilevel coherence works this will be removed
259 if (pkt
->cmd
== MemCmd::WriteInvalidateReq
&& fixPartialWrite
&&
260 pkt
->result
!= Packet::Nacked
) {
261 PacketPtr funcPkt
= new Packet(pkt
->req
, MemCmd::WriteReq
,
263 funcPkt
->dataStatic(pkt
->getPtr
<uint8_t>());
264 sendFunctional(funcPkt
);
265 pkt
->cmd
= MemCmd::WriteReq
;
269 DPRINTF(BusBridge
, "trySend: origSrc %d dest %d addr 0x%x\n",
270 buf
->origSrc
, pkt
->getDest(), pkt
->getAddr());
272 bool wasReq
= pkt
->isRequest();
273 bool wasNacked
= pkt
->result
== Packet::Nacked
;
275 if (sendTiming(pkt
)) {
277 sendQueue
.pop_front();
278 buf
->pkt
= NULL
; // we no longer own packet, so it's not safe to look at it
280 if (buf
->expectResponse
) {
281 // Must wait for response
282 DPRINTF(BusBridge
, " successful: awaiting response (%d)\n",
283 outstandingResponses
);
285 // no response expected... deallocate packet buffer now.
286 DPRINTF(BusBridge
, " successful: no response expected\n");
294 --outstandingResponses
;
297 // If there are more packets to send, schedule event to try again.
298 if (!sendQueue
.empty()) {
299 buf
= sendQueue
.front();
300 DPRINTF(BusBridge
, "Scheduling next send\n");
301 sendEvent
.schedule(std::max(buf
->ready
, curTick
+ 1));
304 DPRINTF(BusBridge
, " unsuccessful\n");
307 DPRINTF(BusBridge
, "trySend: queue size: %d outreq: %d outstanding resp: %d\n",
308 sendQueue
.size(), queuedRequests
, outstandingResponses
);
313 Bridge::BridgePort::recvRetry()
316 Tick nextReady
= sendQueue
.front()->ready
;
317 if (nextReady
<= curTick
)
320 sendEvent
.schedule(nextReady
);
323 /** Function called by the port when the bus is receiving a Atomic
326 Bridge::BridgePort::recvAtomic(PacketPtr pkt
)
328 // fix partial atomic writes... similar to the timing code that does the
329 // same... will be removed once our code gets this right
330 if (pkt
->cmd
== MemCmd::WriteInvalidateReq
&& fixPartialWrite
) {
332 PacketPtr funcPkt
= new Packet(pkt
->req
, MemCmd::WriteReq
,
334 funcPkt
->dataStatic(pkt
->getPtr
<uint8_t>());
335 otherPort
->sendFunctional(funcPkt
);
337 pkt
->cmd
= MemCmd::WriteReq
;
339 return delay
+ otherPort
->sendAtomic(pkt
);
342 /** Function called by the port when the bus is receiving a Functional
345 Bridge::BridgePort::recvFunctional(PacketPtr pkt
)
347 std::list
<PacketBuffer
*>::iterator i
;
348 bool pktContinue
= true;
350 for (i
= sendQueue
.begin(); i
!= sendQueue
.end(); ++i
) {
351 if (pkt
->intersect((*i
)->pkt
)) {
352 pktContinue
&= fixPacket(pkt
, (*i
)->pkt
);
357 otherPort
->sendFunctional(pkt
);
361 /** Function called by the port when the bus is receiving a status change.*/
363 Bridge::BridgePort::recvStatusChange(Port::Status status
)
365 otherPort
->sendStatusChange(status
);
369 Bridge::BridgePort::getDeviceAddressRanges(AddrRangeList
&resp
,
370 AddrRangeList
&snoop
)
372 otherPort
->getPeerAddressRanges(resp
, snoop
);
375 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Bridge
)
377 Param
<int> req_size_a
;
378 Param
<int> req_size_b
;
379 Param
<int> resp_size_a
;
380 Param
<int> resp_size_b
;
382 Param
<Tick
> nack_delay
;
383 Param
<bool> write_ack
;
384 Param
<bool> fix_partial_write_a
;
385 Param
<bool> fix_partial_write_b
;
387 END_DECLARE_SIM_OBJECT_PARAMS(Bridge
)
389 BEGIN_INIT_SIM_OBJECT_PARAMS(Bridge
)
391 INIT_PARAM(req_size_a
, "The size of the queue for requests coming into side a"),
392 INIT_PARAM(req_size_b
, "The size of the queue for requests coming into side b"),
393 INIT_PARAM(resp_size_a
, "The size of the queue for responses coming into side a"),
394 INIT_PARAM(resp_size_b
, "The size of the queue for responses coming into side b"),
395 INIT_PARAM(delay
, "The miminum delay to cross this bridge"),
396 INIT_PARAM(nack_delay
, "The minimum delay to nack a packet"),
397 INIT_PARAM(write_ack
, "Acknowledge any writes that are received."),
398 INIT_PARAM(fix_partial_write_a
, "Fixup any partial block writes that are received"),
399 INIT_PARAM(fix_partial_write_b
, "Fixup any partial block writes that are received")
401 END_INIT_SIM_OBJECT_PARAMS(Bridge
)
403 CREATE_SIM_OBJECT(Bridge
)
405 Bridge::Params
*p
= new Bridge::Params
;
406 p
->name
= getInstanceName();
407 p
->req_size_a
= req_size_a
;
408 p
->req_size_b
= req_size_b
;
409 p
->resp_size_a
= resp_size_a
;
410 p
->resp_size_b
= resp_size_b
;
412 p
->nack_delay
= nack_delay
;
413 p
->write_ack
= write_ack
;
414 p
->fix_partial_write_a
= fix_partial_write_a
;
415 p
->fix_partial_write_b
= fix_partial_write_b
;
416 return new Bridge(p
);
419 REGISTER_SIM_OBJECT("Bridge", Bridge
)