mem: Fix (ab)use of emplace to avoid temporary object creation
[gem5.git] / src / mem / bridge.cc
1 /*
2 * Copyright (c) 2011-2013 ARM Limited
3 * All rights reserved
4 *
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.
13 *
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
16 *
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.
27 *
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.
39 *
40 * Authors: Ali Saidi
41 * Steve Reinhardt
42 * Andreas Hansson
43 */
44
45 /**
46 * @file
47 * Implementation of a memory-mapped bridge that connects a master
48 * and a slave through a request and response queue.
49 */
50
51 #include "base/trace.hh"
52 #include "debug/Bridge.hh"
53 #include "mem/bridge.hh"
54 #include "params/Bridge.hh"
55
56 Bridge::BridgeSlavePort::BridgeSlavePort(const std::string& _name,
57 Bridge& _bridge,
58 BridgeMasterPort& _masterPort,
59 Cycles _delay, int _resp_limit,
60 std::vector<AddrRange> _ranges)
61 : SlavePort(_name, &_bridge), bridge(_bridge), masterPort(_masterPort),
62 delay(_delay), ranges(_ranges.begin(), _ranges.end()),
63 outstandingResponses(0), retryReq(false),
64 respQueueLimit(_resp_limit), sendEvent(*this)
65 {
66 }
67
68 Bridge::BridgeMasterPort::BridgeMasterPort(const std::string& _name,
69 Bridge& _bridge,
70 BridgeSlavePort& _slavePort,
71 Cycles _delay, int _req_limit)
72 : MasterPort(_name, &_bridge), bridge(_bridge), slavePort(_slavePort),
73 delay(_delay), reqQueueLimit(_req_limit), sendEvent(*this)
74 {
75 }
76
77 Bridge::Bridge(Params *p)
78 : MemObject(p),
79 slavePort(p->name + ".slave", *this, masterPort,
80 ticksToCycles(p->delay), p->resp_size, p->ranges),
81 masterPort(p->name + ".master", *this, slavePort,
82 ticksToCycles(p->delay), p->req_size)
83 {
84 }
85
86 BaseMasterPort&
87 Bridge::getMasterPort(const std::string &if_name, PortID idx)
88 {
89 if (if_name == "master")
90 return masterPort;
91 else
92 // pass it along to our super class
93 return MemObject::getMasterPort(if_name, idx);
94 }
95
96 BaseSlavePort&
97 Bridge::getSlavePort(const std::string &if_name, PortID idx)
98 {
99 if (if_name == "slave")
100 return slavePort;
101 else
102 // pass it along to our super class
103 return MemObject::getSlavePort(if_name, idx);
104 }
105
106 void
107 Bridge::init()
108 {
109 // make sure both sides are connected and have the same block size
110 if (!slavePort.isConnected() || !masterPort.isConnected())
111 fatal("Both ports of a bridge must be connected.\n");
112
113 // notify the master side of our address ranges
114 slavePort.sendRangeChange();
115 }
116
117 bool
118 Bridge::BridgeSlavePort::respQueueFull() const
119 {
120 return outstandingResponses == respQueueLimit;
121 }
122
123 bool
124 Bridge::BridgeMasterPort::reqQueueFull() const
125 {
126 return transmitList.size() == reqQueueLimit;
127 }
128
129 bool
130 Bridge::BridgeMasterPort::recvTimingResp(PacketPtr pkt)
131 {
132 // all checks are done when the request is accepted on the slave
133 // side, so we are guaranteed to have space for the response
134 DPRINTF(Bridge, "recvTimingResp: %s addr 0x%x\n",
135 pkt->cmdString(), pkt->getAddr());
136
137 DPRINTF(Bridge, "Request queue size: %d\n", transmitList.size());
138
139 // @todo: We need to pay for this and not just zero it out
140 pkt->headerDelay = pkt->payloadDelay = 0;
141
142 slavePort.schedTimingResp(pkt, bridge.clockEdge(delay));
143
144 return true;
145 }
146
147 bool
148 Bridge::BridgeSlavePort::recvTimingReq(PacketPtr pkt)
149 {
150 DPRINTF(Bridge, "recvTimingReq: %s addr 0x%x\n",
151 pkt->cmdString(), pkt->getAddr());
152
153 // we should not see a timing request if we are already in a retry
154 assert(!retryReq);
155
156 DPRINTF(Bridge, "Response queue size: %d outresp: %d\n",
157 transmitList.size(), outstandingResponses);
158
159 // if the request queue is full then there is no hope
160 if (masterPort.reqQueueFull()) {
161 DPRINTF(Bridge, "Request queue full\n");
162 retryReq = true;
163 } else {
164 // look at the response queue if we expect to see a response
165 bool expects_response = pkt->needsResponse() &&
166 !pkt->memInhibitAsserted();
167 if (expects_response) {
168 if (respQueueFull()) {
169 DPRINTF(Bridge, "Response queue full\n");
170 retryReq = true;
171 } else {
172 // ok to send the request with space for the response
173 DPRINTF(Bridge, "Reserving space for response\n");
174 assert(outstandingResponses != respQueueLimit);
175 ++outstandingResponses;
176
177 // no need to set retryReq to false as this is already the
178 // case
179 }
180 }
181
182 if (!retryReq) {
183 // @todo: We need to pay for this and not just zero it out
184 pkt->headerDelay = pkt->payloadDelay = 0;
185
186 masterPort.schedTimingReq(pkt, bridge.clockEdge(delay));
187 }
188 }
189
190 // remember that we are now stalling a packet and that we have to
191 // tell the sending master to retry once space becomes available,
192 // we make no distinction whether the stalling is due to the
193 // request queue or response queue being full
194 return !retryReq;
195 }
196
197 void
198 Bridge::BridgeSlavePort::retryStalledReq()
199 {
200 if (retryReq) {
201 DPRINTF(Bridge, "Request waiting for retry, now retrying\n");
202 retryReq = false;
203 sendRetryReq();
204 }
205 }
206
207 void
208 Bridge::BridgeMasterPort::schedTimingReq(PacketPtr pkt, Tick when)
209 {
210 // If we're about to put this packet at the head of the queue, we
211 // need to schedule an event to do the transmit. Otherwise there
212 // should already be an event scheduled for sending the head
213 // packet.
214 if (transmitList.empty()) {
215 bridge.schedule(sendEvent, when);
216 }
217
218 assert(transmitList.size() != reqQueueLimit);
219
220 transmitList.emplace_back(pkt, when);
221 }
222
223
224 void
225 Bridge::BridgeSlavePort::schedTimingResp(PacketPtr pkt, Tick when)
226 {
227 // If we're about to put this packet at the head of the queue, we
228 // need to schedule an event to do the transmit. Otherwise there
229 // should already be an event scheduled for sending the head
230 // packet.
231 if (transmitList.empty()) {
232 bridge.schedule(sendEvent, when);
233 }
234
235 transmitList.emplace_back(pkt, when);
236 }
237
238 void
239 Bridge::BridgeMasterPort::trySendTiming()
240 {
241 assert(!transmitList.empty());
242
243 DeferredPacket req = transmitList.front();
244
245 assert(req.tick <= curTick());
246
247 PacketPtr pkt = req.pkt;
248
249 DPRINTF(Bridge, "trySend request addr 0x%x, queue size %d\n",
250 pkt->getAddr(), transmitList.size());
251
252 if (sendTimingReq(pkt)) {
253 // send successful
254 transmitList.pop_front();
255 DPRINTF(Bridge, "trySend request successful\n");
256
257 // If there are more packets to send, schedule event to try again.
258 if (!transmitList.empty()) {
259 DeferredPacket next_req = transmitList.front();
260 DPRINTF(Bridge, "Scheduling next send\n");
261 bridge.schedule(sendEvent, std::max(next_req.tick,
262 bridge.clockEdge()));
263 }
264
265 // if we have stalled a request due to a full request queue,
266 // then send a retry at this point, also note that if the
267 // request we stalled was waiting for the response queue
268 // rather than the request queue we might stall it again
269 slavePort.retryStalledReq();
270 }
271
272 // if the send failed, then we try again once we receive a retry,
273 // and therefore there is no need to take any action
274 }
275
276 void
277 Bridge::BridgeSlavePort::trySendTiming()
278 {
279 assert(!transmitList.empty());
280
281 DeferredPacket resp = transmitList.front();
282
283 assert(resp.tick <= curTick());
284
285 PacketPtr pkt = resp.pkt;
286
287 DPRINTF(Bridge, "trySend response addr 0x%x, outstanding %d\n",
288 pkt->getAddr(), outstandingResponses);
289
290 if (sendTimingResp(pkt)) {
291 // send successful
292 transmitList.pop_front();
293 DPRINTF(Bridge, "trySend response successful\n");
294
295 assert(outstandingResponses != 0);
296 --outstandingResponses;
297
298 // If there are more packets to send, schedule event to try again.
299 if (!transmitList.empty()) {
300 DeferredPacket next_resp = transmitList.front();
301 DPRINTF(Bridge, "Scheduling next send\n");
302 bridge.schedule(sendEvent, std::max(next_resp.tick,
303 bridge.clockEdge()));
304 }
305
306 // if there is space in the request queue and we were stalling
307 // a request, it will definitely be possible to accept it now
308 // since there is guaranteed space in the response queue
309 if (!masterPort.reqQueueFull() && retryReq) {
310 DPRINTF(Bridge, "Request waiting for retry, now retrying\n");
311 retryReq = false;
312 sendRetryReq();
313 }
314 }
315
316 // if the send failed, then we try again once we receive a retry,
317 // and therefore there is no need to take any action
318 }
319
320 void
321 Bridge::BridgeMasterPort::recvReqRetry()
322 {
323 trySendTiming();
324 }
325
326 void
327 Bridge::BridgeSlavePort::recvRespRetry()
328 {
329 trySendTiming();
330 }
331
332 Tick
333 Bridge::BridgeSlavePort::recvAtomic(PacketPtr pkt)
334 {
335 return delay * bridge.clockPeriod() + masterPort.sendAtomic(pkt);
336 }
337
338 void
339 Bridge::BridgeSlavePort::recvFunctional(PacketPtr pkt)
340 {
341 pkt->pushLabel(name());
342
343 // check the response queue
344 for (auto i = transmitList.begin(); i != transmitList.end(); ++i) {
345 if (pkt->checkFunctional((*i).pkt)) {
346 pkt->makeResponse();
347 return;
348 }
349 }
350
351 // also check the master port's request queue
352 if (masterPort.checkFunctional(pkt)) {
353 return;
354 }
355
356 pkt->popLabel();
357
358 // fall through if pkt still not satisfied
359 masterPort.sendFunctional(pkt);
360 }
361
362 bool
363 Bridge::BridgeMasterPort::checkFunctional(PacketPtr pkt)
364 {
365 bool found = false;
366 auto i = transmitList.begin();
367
368 while(i != transmitList.end() && !found) {
369 if (pkt->checkFunctional((*i).pkt)) {
370 pkt->makeResponse();
371 found = true;
372 }
373 ++i;
374 }
375
376 return found;
377 }
378
379 AddrRangeList
380 Bridge::BridgeSlavePort::getAddrRanges() const
381 {
382 return ranges;
383 }
384
385 Bridge *
386 BridgeParams::create()
387 {
388 return new Bridge(this);
389 }