eventq: convert all usage of events to use the new API.
[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/range_ops.hh"
41 #include "base/trace.hh"
42 #include "mem/bridge.hh"
43 #include "params/Bridge.hh"
44
45 Bridge::BridgePort::BridgePort(const std::string &_name,
46 Bridge *_bridge, BridgePort *_otherPort,
47 int _delay, int _nack_delay, int _req_limit,
48 int _resp_limit,
49 std::vector<Range<Addr> > filter_ranges)
50 : Port(_name, _bridge), bridge(_bridge), otherPort(_otherPort),
51 delay(_delay), nackDelay(_nack_delay), filterRanges(filter_ranges),
52 outstandingResponses(0), queuedRequests(0), inRetry(false),
53 reqQueueLimit(_req_limit), respQueueLimit(_resp_limit), sendEvent(this)
54 {
55 }
56
57 Bridge::Bridge(Params *p)
58 : MemObject(p),
59 portA(p->name + "-portA", this, &portB, p->delay, p->nack_delay,
60 p->req_size_a, p->resp_size_a, p->filter_ranges_a),
61 portB(p->name + "-portB", this, &portA, p->delay, p->nack_delay,
62 p->req_size_b, p->resp_size_b, p->filter_ranges_b),
63 ackWrites(p->write_ack), _params(p)
64 {
65 if (ackWrites)
66 panic("No support for acknowledging writes\n");
67 }
68
69 Port *
70 Bridge::getPort(const std::string &if_name, int idx)
71 {
72 BridgePort *port;
73
74 if (if_name == "side_a")
75 port = &portA;
76 else if (if_name == "side_b")
77 port = &portB;
78 else
79 return NULL;
80
81 if (port->getPeer() != NULL && !port->getPeer()->isDefaultPort())
82 panic("bridge side %s already connected to %s.",
83 if_name, port->getPeer()->name());
84 return port;
85 }
86
87
88 void
89 Bridge::init()
90 {
91 // Make sure that both sides are connected to.
92 if (!portA.isConnected() || !portB.isConnected())
93 fatal("Both ports of bus bridge are not connected to a bus.\n");
94
95 if (portA.peerBlockSize() != portB.peerBlockSize())
96 fatal("Busses don't have the same block size... Not supported.\n");
97 }
98
99 bool
100 Bridge::BridgePort::respQueueFull()
101 {
102 assert(outstandingResponses >= 0 && outstandingResponses <= respQueueLimit);
103 return outstandingResponses >= respQueueLimit;
104 }
105
106 bool
107 Bridge::BridgePort::reqQueueFull()
108 {
109 assert(queuedRequests >= 0 && queuedRequests <= reqQueueLimit);
110 return queuedRequests >= reqQueueLimit;
111 }
112
113 /** Function called by the port when the bus is receiving a Timing
114 * transaction.*/
115 bool
116 Bridge::BridgePort::recvTiming(PacketPtr pkt)
117 {
118 DPRINTF(BusBridge, "recvTiming: src %d dest %d addr 0x%x\n",
119 pkt->getSrc(), pkt->getDest(), pkt->getAddr());
120
121 DPRINTF(BusBridge, "Local queue size: %d outreq: %d outresp: %d\n",
122 sendQueue.size(), queuedRequests, outstandingResponses);
123 DPRINTF(BusBridge, "Remote queue size: %d outreq: %d outresp: %d\n",
124 otherPort->sendQueue.size(), otherPort->queuedRequests,
125 otherPort->outstandingResponses);
126
127 if (pkt->isRequest() && otherPort->reqQueueFull()) {
128 DPRINTF(BusBridge, "Remote queue full, nacking\n");
129 nackRequest(pkt);
130 return true;
131 }
132
133 if (pkt->needsResponse()) {
134 if (respQueueFull()) {
135 DPRINTF(BusBridge, "Local queue full, no space for response, nacking\n");
136 DPRINTF(BusBridge, "queue size: %d outreq: %d outstanding resp: %d\n",
137 sendQueue.size(), queuedRequests, outstandingResponses);
138 nackRequest(pkt);
139 return true;
140 } else {
141 DPRINTF(BusBridge, "Request Needs response, reserving space\n");
142 ++outstandingResponses;
143 }
144 }
145
146 otherPort->queueForSendTiming(pkt);
147
148 return true;
149 }
150
151 void
152 Bridge::BridgePort::nackRequest(PacketPtr pkt)
153 {
154 // Nack the packet
155 pkt->makeTimingResponse();
156 pkt->setNacked();
157
158 //put it on the list to send
159 Tick readyTime = curTick + nackDelay;
160 PacketBuffer *buf = new PacketBuffer(pkt, readyTime, true);
161
162 // nothing on the list, add it and we're done
163 if (sendQueue.empty()) {
164 assert(!sendEvent.scheduled());
165 schedule(sendEvent, readyTime);
166 sendQueue.push_back(buf);
167 return;
168 }
169
170 assert(sendEvent.scheduled() || inRetry);
171
172 // does it go at the end?
173 if (readyTime >= sendQueue.back()->ready) {
174 sendQueue.push_back(buf);
175 return;
176 }
177
178 // ok, somewhere in the middle, fun
179 std::list<PacketBuffer*>::iterator i = sendQueue.begin();
180 std::list<PacketBuffer*>::iterator end = sendQueue.end();
181 std::list<PacketBuffer*>::iterator begin = sendQueue.begin();
182 bool done = false;
183
184 while (i != end && !done) {
185 if (readyTime < (*i)->ready) {
186 if (i == begin)
187 reschedule(sendEvent, readyTime);
188 sendQueue.insert(i,buf);
189 done = true;
190 }
191 i++;
192 }
193 assert(done);
194 }
195
196
197 void
198 Bridge::BridgePort::queueForSendTiming(PacketPtr pkt)
199 {
200 if (pkt->isResponse()) {
201 // This is a response for a request we forwarded earlier. The
202 // corresponding PacketBuffer should be stored in the packet's
203 // senderState field.
204
205 PacketBuffer *buf = dynamic_cast<PacketBuffer*>(pkt->senderState);
206 assert(buf != NULL);
207 // set up new packet dest & senderState based on values saved
208 // from original request
209 buf->fixResponse(pkt);
210
211 DPRINTF(BusBridge, "response, new dest %d\n", pkt->getDest());
212 delete buf;
213 }
214
215
216 if (pkt->isRequest()) {
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 schedule(sendEvent, 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 DPRINTF(BusBridge, "trySend: origSrc %d dest %d addr 0x%x\n",
247 buf->origSrc, pkt->getDest(), pkt->getAddr());
248
249 bool wasReq = pkt->isRequest();
250 bool was_nacked_here = buf->nackedHere;
251
252 // If the send was successful, make sure sender state was set to NULL
253 // otherwise we could get a NACK back of a packet that didn't expect a
254 // response and we would try to use freed memory.
255
256 Packet::SenderState *old_sender_state = pkt->senderState;
257 if (pkt->isRequest() && !buf->expectResponse)
258 pkt->senderState = NULL;
259
260 if (sendTiming(pkt)) {
261 // send successful
262 sendQueue.pop_front();
263 buf->pkt = NULL; // we no longer own packet, so it's not safe to look at it
264
265 if (buf->expectResponse) {
266 // Must wait for response
267 DPRINTF(BusBridge, " successful: awaiting response (%d)\n",
268 outstandingResponses);
269 } else {
270 // no response expected... deallocate packet buffer now.
271 DPRINTF(BusBridge, " successful: no response expected\n");
272 delete buf;
273 }
274
275 if (wasReq)
276 --queuedRequests;
277 else if (!was_nacked_here)
278 --outstandingResponses;
279
280 // If there are more packets to send, schedule event to try again.
281 if (!sendQueue.empty()) {
282 buf = sendQueue.front();
283 DPRINTF(BusBridge, "Scheduling next send\n");
284 schedule(sendEvent, std::max(buf->ready, curTick + 1));
285 }
286 } else {
287 DPRINTF(BusBridge, " unsuccessful\n");
288 pkt->senderState = old_sender_state;
289 inRetry = true;
290 }
291
292 DPRINTF(BusBridge, "trySend: queue size: %d outreq: %d outstanding resp: %d\n",
293 sendQueue.size(), queuedRequests, outstandingResponses);
294 }
295
296
297 void
298 Bridge::BridgePort::recvRetry()
299 {
300 inRetry = false;
301 Tick nextReady = sendQueue.front()->ready;
302 if (nextReady <= curTick)
303 trySend();
304 else
305 schedule(sendEvent, nextReady);
306 }
307
308 /** Function called by the port when the bus is receiving a Atomic
309 * transaction.*/
310 Tick
311 Bridge::BridgePort::recvAtomic(PacketPtr pkt)
312 {
313 return delay + otherPort->sendAtomic(pkt);
314 }
315
316 /** Function called by the port when the bus is receiving a Functional
317 * transaction.*/
318 void
319 Bridge::BridgePort::recvFunctional(PacketPtr pkt)
320 {
321 std::list<PacketBuffer*>::iterator i;
322
323 pkt->pushLabel(name());
324
325 for (i = sendQueue.begin(); i != sendQueue.end(); ++i) {
326 if (pkt->checkFunctional((*i)->pkt))
327 return;
328 }
329
330 pkt->popLabel();
331
332 // fall through if pkt still not satisfied
333 otherPort->sendFunctional(pkt);
334 }
335
336 /** Function called by the port when the bus is receiving a status change.*/
337 void
338 Bridge::BridgePort::recvStatusChange(Port::Status status)
339 {
340 otherPort->sendStatusChange(status);
341 }
342
343 void
344 Bridge::BridgePort::getDeviceAddressRanges(AddrRangeList &resp,
345 bool &snoop)
346 {
347 otherPort->getPeerAddressRanges(resp, snoop);
348 FilterRangeList(filterRanges, resp);
349 // we don't allow snooping across bridges
350 snoop = false;
351 }
352
353 Bridge *
354 BridgeParams::create()
355 {
356 return new Bridge(this);
357 }