Merge from head.
[gem5.git] / src / mem / bridge.cc
1
2 /*
3 * Copyright (c) 2006 The Regents of The University of Michigan
4 * All rights reserved.
5 *
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.
16 *
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.
28 *
29 * Authors: Ali Saidi
30 * Steve Reinhardt
31 */
32
33 /**
34 * @file
35 * Definition of a simple bus bridge without buffering.
36 */
37
38 #include <algorithm>
39
40 #include "base/trace.hh"
41 #include "mem/bridge.hh"
42 #include "params/Bridge.hh"
43
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)
52 {
53 }
54
55 Bridge::Bridge(Params *p)
56 : MemObject(p->name),
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)
62 {
63 if (ackWrites)
64 panic("No support for acknowledging writes\n");
65 }
66
67 Port *
68 Bridge::getPort(const std::string &if_name, int idx)
69 {
70 BridgePort *port;
71
72 if (if_name == "side_a")
73 port = &portA;
74 else if (if_name == "side_b")
75 port = &portB;
76 else
77 return NULL;
78
79 if (port->getPeer() != NULL)
80 panic("bridge side %s already connected to.", if_name);
81 return port;
82 }
83
84
85 void
86 Bridge::init()
87 {
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");
91
92 if (portA.peerBlockSize() != portB.peerBlockSize())
93 fatal("Busses don't have the same block size... Not supported.\n");
94 }
95
96 bool
97 Bridge::BridgePort::respQueueFull()
98 {
99 assert(outstandingResponses >= 0 && outstandingResponses <= respQueueLimit);
100 return outstandingResponses >= respQueueLimit;
101 }
102
103 bool
104 Bridge::BridgePort::reqQueueFull()
105 {
106 assert(queuedRequests >= 0 && queuedRequests <= reqQueueLimit);
107 return queuedRequests >= reqQueueLimit;
108 }
109
110 /** Function called by the port when the bus is receiving a Timing
111 * transaction.*/
112 bool
113 Bridge::BridgePort::recvTiming(PacketPtr pkt)
114 {
115 DPRINTF(BusBridge, "recvTiming: src %d dest %d addr 0x%x\n",
116 pkt->getSrc(), pkt->getDest(), pkt->getAddr());
117
118 DPRINTF(BusBridge, "Local queue size: %d outreq: %d outresp: %d\n",
119 sendQueue.size(), queuedRequests, outstandingResponses);
120 DPRINTF(BusBridge, "Remove queue size: %d outreq: %d outresp: %d\n",
121 otherPort->sendQueue.size(), otherPort->queuedRequests,
122 otherPort->outstandingResponses);
123
124 if (pkt->isRequest() && otherPort->reqQueueFull() && !pkt->wasNacked()) {
125 DPRINTF(BusBridge, "Remote queue full, nacking\n");
126 nackRequest(pkt);
127 return true;
128 }
129
130 if (pkt->needsResponse() && !pkt->wasNacked())
131 if (respQueueFull()) {
132 DPRINTF(BusBridge, "Local queue full, no space for response, nacking\n");
133 DPRINTF(BusBridge, "queue size: %d outreq: %d outstanding resp: %d\n",
134 sendQueue.size(), queuedRequests, outstandingResponses);
135 nackRequest(pkt);
136 return true;
137 } else {
138 DPRINTF(BusBridge, "Request Needs response, reserving space\n");
139 ++outstandingResponses;
140 }
141
142 otherPort->queueForSendTiming(pkt);
143
144 return true;
145 }
146
147 void
148 Bridge::BridgePort::nackRequest(PacketPtr pkt)
149 {
150 // Nack the packet
151 pkt->setNacked();
152 pkt->setDest(pkt->getSrc());
153
154 //put it on the list to send
155 Tick readyTime = curTick + nackDelay;
156 PacketBuffer *buf = new PacketBuffer(pkt, readyTime, true);
157
158 // nothing on the list, add it and we're done
159 if (sendQueue.empty()) {
160 assert(!sendEvent.scheduled());
161 sendEvent.schedule(readyTime);
162 sendQueue.push_back(buf);
163 return;
164 }
165
166 assert(sendEvent.scheduled() || inRetry);
167
168 // does it go at the end?
169 if (readyTime >= sendQueue.back()->ready) {
170 sendQueue.push_back(buf);
171 return;
172 }
173
174 // ok, somewhere in the middle, fun
175 std::list<PacketBuffer*>::iterator i = sendQueue.begin();
176 std::list<PacketBuffer*>::iterator end = sendQueue.end();
177 std::list<PacketBuffer*>::iterator begin = sendQueue.begin();
178 bool done = false;
179
180 while (i != end && !done) {
181 if (readyTime < (*i)->ready) {
182 if (i == begin)
183 sendEvent.reschedule(readyTime);
184 sendQueue.insert(i,buf);
185 done = true;
186 }
187 i++;
188 }
189 assert(done);
190 }
191
192
193 void
194 Bridge::BridgePort::queueForSendTiming(PacketPtr pkt)
195 {
196 if (pkt->isResponse() || pkt->wasNacked()) {
197 // This is a response for a request we forwarded earlier. The
198 // corresponding PacketBuffer should be stored in the packet's
199 // senderState field.
200 PacketBuffer *buf = dynamic_cast<PacketBuffer*>(pkt->senderState);
201 assert(buf != NULL);
202 // set up new packet dest & senderState based on values saved
203 // from original request
204 buf->fixResponse(pkt);
205
206 // Check if this packet was expecting a response and it's a nacked
207 // packet, in which case we will never being seeing it
208 if (buf->expectResponse && pkt->wasNacked())
209 --outstandingResponses;
210
211 DPRINTF(BusBridge, "response, new dest %d\n", pkt->getDest());
212 delete buf;
213 }
214
215
216 if (pkt->isRequest() && !pkt->wasNacked()) {
217 ++queuedRequests;
218 }
219
220
221
222 Tick readyTime = curTick + delay;
223 PacketBuffer *buf = new PacketBuffer(pkt, readyTime);
224
225 // If we're about to put this packet at the head of the queue, we
226 // need to schedule an event to do the transmit. Otherwise there
227 // should already be an event scheduled for sending the head
228 // packet.
229 if (sendQueue.empty()) {
230 sendEvent.schedule(readyTime);
231 }
232 sendQueue.push_back(buf);
233 }
234
235 void
236 Bridge::BridgePort::trySend()
237 {
238 assert(!sendQueue.empty());
239
240 PacketBuffer *buf = sendQueue.front();
241
242 assert(buf->ready <= curTick);
243
244 PacketPtr pkt = buf->pkt;
245
246 // Ugly! @todo When multilevel coherence works this will be removed
247 if (pkt->cmd == MemCmd::WriteInvalidateReq && fixPartialWrite &&
248 !pkt->wasNacked()) {
249 PacketPtr funcPkt = new Packet(pkt->req, MemCmd::WriteReq,
250 Packet::Broadcast);
251 funcPkt->dataStatic(pkt->getPtr<uint8_t>());
252 sendFunctional(funcPkt);
253 pkt->cmd = MemCmd::WriteReq;
254 delete funcPkt;
255 }
256
257 DPRINTF(BusBridge, "trySend: origSrc %d dest %d addr 0x%x\n",
258 buf->origSrc, pkt->getDest(), pkt->getAddr());
259
260 bool wasReq = pkt->isRequest();
261 bool wasNacked = pkt->wasNacked();
262
263 if (sendTiming(pkt)) {
264 // send successful
265 sendQueue.pop_front();
266 buf->pkt = NULL; // we no longer own packet, so it's not safe to look at it
267
268 if (buf->expectResponse) {
269 // Must wait for response
270 DPRINTF(BusBridge, " successful: awaiting response (%d)\n",
271 outstandingResponses);
272 } else {
273 // no response expected... deallocate packet buffer now.
274 DPRINTF(BusBridge, " successful: no response expected\n");
275 delete buf;
276 }
277
278 if (!wasNacked) {
279 if (wasReq)
280 --queuedRequests;
281 else
282 --outstandingResponses;
283 }
284
285 // If there are more packets to send, schedule event to try again.
286 if (!sendQueue.empty()) {
287 buf = sendQueue.front();
288 DPRINTF(BusBridge, "Scheduling next send\n");
289 sendEvent.schedule(std::max(buf->ready, curTick + 1));
290 }
291 } else {
292 DPRINTF(BusBridge, " unsuccessful\n");
293 inRetry = true;
294 }
295 DPRINTF(BusBridge, "trySend: queue size: %d outreq: %d outstanding resp: %d\n",
296 sendQueue.size(), queuedRequests, outstandingResponses);
297 }
298
299
300 void
301 Bridge::BridgePort::recvRetry()
302 {
303 inRetry = false;
304 Tick nextReady = sendQueue.front()->ready;
305 if (nextReady <= curTick)
306 trySend();
307 else
308 sendEvent.schedule(nextReady);
309 }
310
311 /** Function called by the port when the bus is receiving a Atomic
312 * transaction.*/
313 Tick
314 Bridge::BridgePort::recvAtomic(PacketPtr pkt)
315 {
316 // fix partial atomic writes... similar to the timing code that does the
317 // same... will be removed once our code gets this right
318 if (pkt->cmd == MemCmd::WriteInvalidateReq && fixPartialWrite) {
319
320 PacketPtr funcPkt = new Packet(pkt->req, MemCmd::WriteReq,
321 Packet::Broadcast);
322 funcPkt->dataStatic(pkt->getPtr<uint8_t>());
323 otherPort->sendFunctional(funcPkt);
324 delete funcPkt;
325 pkt->cmd = MemCmd::WriteReq;
326 }
327 return delay + otherPort->sendAtomic(pkt);
328 }
329
330 /** Function called by the port when the bus is receiving a Functional
331 * transaction.*/
332 void
333 Bridge::BridgePort::recvFunctional(PacketPtr pkt)
334 {
335 std::list<PacketBuffer*>::iterator i;
336
337 for (i = sendQueue.begin(); i != sendQueue.end(); ++i) {
338 if (pkt->checkFunctional((*i)->pkt))
339 return;
340 }
341
342 // fall through if pkt still not satisfied
343 otherPort->sendFunctional(pkt);
344 }
345
346 /** Function called by the port when the bus is receiving a status change.*/
347 void
348 Bridge::BridgePort::recvStatusChange(Port::Status status)
349 {
350 otherPort->sendStatusChange(status);
351 }
352
353 void
354 Bridge::BridgePort::getDeviceAddressRanges(AddrRangeList &resp,
355 bool &snoop)
356 {
357 otherPort->getPeerAddressRanges(resp, snoop);
358 // we don't allow snooping across bridges
359 snoop = false;
360 }
361
362 Bridge *
363 BridgeParams::create()
364 {
365 return new Bridge(this);
366 }