mem: Consistently use ISO prefixes
[gem5.git] / src / mem / serial_link.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 * Copyright (c) 2015 The University of Bologna
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42 /**
43 * @file
44 * Implementation of the SerialLink Class, modeling Hybrid-Memory-Cube's
45 * serial interface.
46 */
47
48 #include "mem/serial_link.hh"
49
50 #include "base/trace.hh"
51 #include "debug/SerialLink.hh"
52 #include "params/SerialLink.hh"
53
54 SerialLink::SerialLinkResponsePort::
55 SerialLinkResponsePort(const std::string& _name,
56 SerialLink& _serial_link,
57 SerialLinkRequestPort& _mem_side_port,
58 Cycles _delay, int _resp_limit,
59 const std::vector<AddrRange>&
60 _ranges)
61 : ResponsePort(_name, &_serial_link), serial_link(_serial_link),
62 mem_side_port(_mem_side_port), delay(_delay),
63 ranges(_ranges.begin(), _ranges.end()),
64 outstandingResponses(0), retryReq(false),
65 respQueueLimit(_resp_limit),
66 sendEvent([this]{ trySendTiming(); }, _name)
67 {
68 }
69
70 SerialLink::SerialLinkRequestPort::SerialLinkRequestPort(const std::string&
71 _name, SerialLink& _serial_link,
72 SerialLinkResponsePort&
73 _cpu_side_port, Cycles _delay,
74 int _req_limit)
75 : RequestPort(_name, &_serial_link), serial_link(_serial_link),
76 cpu_side_port(_cpu_side_port), delay(_delay), reqQueueLimit(_req_limit),
77 sendEvent([this]{ trySendTiming(); }, _name)
78 {
79 }
80
81 SerialLink::SerialLink(const SerialLinkParams &p)
82 : ClockedObject(p),
83 cpu_side_port(p.name + ".cpu_side_port", *this, mem_side_port,
84 ticksToCycles(p.delay), p.resp_size, p.ranges),
85 mem_side_port(p.name + ".mem_side_port", *this, cpu_side_port,
86 ticksToCycles(p.delay), p.req_size),
87 num_lanes(p.num_lanes),
88 link_speed(p.link_speed)
89 {
90 }
91
92 Port&
93 SerialLink::getPort(const std::string &if_name, PortID idx)
94 {
95 if (if_name == "mem_side_port")
96 return mem_side_port;
97 else if (if_name == "cpu_side_port")
98 return cpu_side_port;
99 else
100 // pass it along to our super class
101 return ClockedObject::getPort(if_name, idx);
102 }
103
104 void
105 SerialLink::init()
106 {
107 // make sure both sides are connected and have the same block size
108 if (!cpu_side_port.isConnected() || !mem_side_port.isConnected())
109 fatal("Both ports of a serial_link must be connected.\n");
110
111 // notify the request side of our address ranges
112 cpu_side_port.sendRangeChange();
113 }
114
115 bool
116 SerialLink::SerialLinkResponsePort::respQueueFull() const
117 {
118 return outstandingResponses == respQueueLimit;
119 }
120
121 bool
122 SerialLink::SerialLinkRequestPort::reqQueueFull() const
123 {
124 return transmitList.size() == reqQueueLimit;
125 }
126
127 bool
128 SerialLink::SerialLinkRequestPort::recvTimingResp(PacketPtr pkt)
129 {
130 // all checks are done when the request is accepted on the response
131 // side, so we are guaranteed to have space for the response
132 DPRINTF(SerialLink, "recvTimingResp: %s addr 0x%x\n",
133 pkt->cmdString(), pkt->getAddr());
134
135 DPRINTF(SerialLink, "Request queue size: %d\n", transmitList.size());
136
137 // @todo: We need to pay for this and not just zero it out
138 pkt->headerDelay = pkt->payloadDelay = 0;
139
140 // This is similar to what happens for the request packets:
141 // The serializer will start serialization as soon as it receives the
142 // first flit, but the deserializer (at the host side in this case), will
143 // have to wait to receive the whole packet. So we only account for the
144 // deserialization latency.
145 Cycles cycles = delay;
146 cycles += Cycles(divCeil(pkt->getSize() * 8, serial_link.num_lanes
147 * serial_link.link_speed));
148 Tick t = serial_link.clockEdge(cycles);
149
150 //@todo: If the processor sends two uncached requests towards HMC and the
151 // second one is smaller than the first one. It may happen that the second
152 // one crosses this link faster than the first one (because the packet
153 // waits in the link based on its size). This can reorder the received
154 // response.
155 cpu_side_port.schedTimingResp(pkt, t);
156
157 return true;
158 }
159
160 bool
161 SerialLink::SerialLinkResponsePort::recvTimingReq(PacketPtr pkt)
162 {
163 DPRINTF(SerialLink, "recvTimingReq: %s addr 0x%x\n",
164 pkt->cmdString(), pkt->getAddr());
165
166 // we should not see a timing request if we are already in a retry
167 assert(!retryReq);
168
169 DPRINTF(SerialLink, "Response queue size: %d outresp: %d\n",
170 transmitList.size(), outstandingResponses);
171
172 // if the request queue is full then there is no hope
173 if (mem_side_port.reqQueueFull()) {
174 DPRINTF(SerialLink, "Request queue full\n");
175 retryReq = true;
176 } else if ( !retryReq ) {
177 // look at the response queue if we expect to see a response
178 bool expects_response = pkt->needsResponse() &&
179 !pkt->cacheResponding();
180 if (expects_response) {
181 if (respQueueFull()) {
182 DPRINTF(SerialLink, "Response queue full\n");
183 retryReq = true;
184 } else {
185 // ok to send the request with space for the response
186 DPRINTF(SerialLink, "Reserving space for response\n");
187 assert(outstandingResponses != respQueueLimit);
188 ++outstandingResponses;
189
190 // no need to set retryReq to false as this is already the
191 // case
192 }
193 }
194
195 if (!retryReq) {
196 // @todo: We need to pay for this and not just zero it out
197 pkt->headerDelay = pkt->payloadDelay = 0;
198
199 // We assume that the serializer component at the transmitter side
200 // does not need to receive the whole packet to start the
201 // serialization (this assumption is consistent with the HMC
202 // standard). But the deserializer waits for the complete packet
203 // to check its integrity first. So everytime a packet crosses a
204 // serial link, we should account for its deserialization latency
205 // only.
206 Cycles cycles = delay;
207 cycles += Cycles(divCeil(pkt->getSize() * 8,
208 serial_link.num_lanes * serial_link.link_speed));
209 Tick t = serial_link.clockEdge(cycles);
210
211 //@todo: If the processor sends two uncached requests towards HMC
212 // and the second one is smaller than the first one. It may happen
213 // that the second one crosses this link faster than the first one
214 // (because the packet waits in the link based on its size).
215 // This can reorder the received response.
216 mem_side_port.schedTimingReq(pkt, t);
217 }
218 }
219
220 // remember that we are now stalling a packet and that we have to
221 // tell the sending requestor to retry once space becomes available,
222 // we make no distinction whether the stalling is due to the
223 // request queue or response queue being full
224 return !retryReq;
225 }
226
227 void
228 SerialLink::SerialLinkResponsePort::retryStalledReq()
229 {
230 if (retryReq) {
231 DPRINTF(SerialLink, "Request waiting for retry, now retrying\n");
232 retryReq = false;
233 sendRetryReq();
234 }
235 }
236
237 void
238 SerialLink::SerialLinkRequestPort::schedTimingReq(PacketPtr pkt, Tick when)
239 {
240 // If we're about to put this packet at the head of the queue, we
241 // need to schedule an event to do the transmit. Otherwise there
242 // should already be an event scheduled for sending the head
243 // packet.
244 if (transmitList.empty()) {
245 serial_link.schedule(sendEvent, when);
246 }
247
248 assert(transmitList.size() != reqQueueLimit);
249
250 transmitList.emplace_back(DeferredPacket(pkt, when));
251 }
252
253
254 void
255 SerialLink::SerialLinkResponsePort::schedTimingResp(PacketPtr pkt, Tick when)
256 {
257 // If we're about to put this packet at the head of the queue, we
258 // need to schedule an event to do the transmit. Otherwise there
259 // should already be an event scheduled for sending the head
260 // packet.
261 if (transmitList.empty()) {
262 serial_link.schedule(sendEvent, when);
263 }
264
265 transmitList.emplace_back(DeferredPacket(pkt, when));
266 }
267
268 void
269 SerialLink::SerialLinkRequestPort::trySendTiming()
270 {
271 assert(!transmitList.empty());
272
273 DeferredPacket req = transmitList.front();
274
275 assert(req.tick <= curTick());
276
277 PacketPtr pkt = req.pkt;
278
279 DPRINTF(SerialLink, "trySend request addr 0x%x, queue size %d\n",
280 pkt->getAddr(), transmitList.size());
281
282 if (sendTimingReq(pkt)) {
283 // send successful
284 transmitList.pop_front();
285
286 DPRINTF(SerialLink, "trySend request successful\n");
287
288 // If there are more packets to send, schedule event to try again.
289 if (!transmitList.empty()) {
290 DeferredPacket next_req = transmitList.front();
291 DPRINTF(SerialLink, "Scheduling next send\n");
292
293 // Make sure bandwidth limitation is met
294 Cycles cycles = Cycles(divCeil(pkt->getSize() * 8,
295 serial_link.num_lanes * serial_link.link_speed));
296 Tick t = serial_link.clockEdge(cycles);
297 serial_link.schedule(sendEvent, std::max(next_req.tick, t));
298 }
299
300 // if we have stalled a request due to a full request queue,
301 // then send a retry at this point, also note that if the
302 // request we stalled was waiting for the response queue
303 // rather than the request queue we might stall it again
304 cpu_side_port.retryStalledReq();
305 }
306
307 // if the send failed, then we try again once we receive a retry,
308 // and therefore there is no need to take any action
309 }
310
311 void
312 SerialLink::SerialLinkResponsePort::trySendTiming()
313 {
314 assert(!transmitList.empty());
315
316 DeferredPacket resp = transmitList.front();
317
318 assert(resp.tick <= curTick());
319
320 PacketPtr pkt = resp.pkt;
321
322 DPRINTF(SerialLink, "trySend response addr 0x%x, outstanding %d\n",
323 pkt->getAddr(), outstandingResponses);
324
325 if (sendTimingResp(pkt)) {
326 // send successful
327 transmitList.pop_front();
328 DPRINTF(SerialLink, "trySend response successful\n");
329
330 assert(outstandingResponses != 0);
331 --outstandingResponses;
332
333 // If there are more packets to send, schedule event to try again.
334 if (!transmitList.empty()) {
335 DeferredPacket next_resp = transmitList.front();
336 DPRINTF(SerialLink, "Scheduling next send\n");
337
338 // Make sure bandwidth limitation is met
339 Cycles cycles = Cycles(divCeil(pkt->getSize() * 8,
340 serial_link.num_lanes * serial_link.link_speed));
341 Tick t = serial_link.clockEdge(cycles);
342 serial_link.schedule(sendEvent, std::max(next_resp.tick, t));
343 }
344
345 // if there is space in the request queue and we were stalling
346 // a request, it will definitely be possible to accept it now
347 // since there is guaranteed space in the response queue
348 if (!mem_side_port.reqQueueFull() && retryReq) {
349 DPRINTF(SerialLink, "Request waiting for retry, now retrying\n");
350 retryReq = false;
351 sendRetryReq();
352 }
353 }
354
355 // if the send failed, then we try again once we receive a retry,
356 // and therefore there is no need to take any action
357 }
358
359 void
360 SerialLink::SerialLinkRequestPort::recvReqRetry()
361 {
362 trySendTiming();
363 }
364
365 void
366 SerialLink::SerialLinkResponsePort::recvRespRetry()
367 {
368 trySendTiming();
369 }
370
371 Tick
372 SerialLink::SerialLinkResponsePort::recvAtomic(PacketPtr pkt)
373 {
374 return delay * serial_link.clockPeriod() + mem_side_port.sendAtomic(pkt);
375 }
376
377 void
378 SerialLink::SerialLinkResponsePort::recvFunctional(PacketPtr pkt)
379 {
380 pkt->pushLabel(name());
381
382 // check the response queue
383 for (auto i = transmitList.begin(); i != transmitList.end(); ++i) {
384 if (pkt->trySatisfyFunctional((*i).pkt)) {
385 pkt->makeResponse();
386 return;
387 }
388 }
389
390 // also check the memory-side port's request queue
391 if (mem_side_port.trySatisfyFunctional(pkt)) {
392 return;
393 }
394
395 pkt->popLabel();
396
397 // fall through if pkt still not satisfied
398 mem_side_port.sendFunctional(pkt);
399 }
400
401 bool
402 SerialLink::SerialLinkRequestPort::trySatisfyFunctional(PacketPtr pkt)
403 {
404 bool found = false;
405 auto i = transmitList.begin();
406
407 while (i != transmitList.end() && !found) {
408 if (pkt->trySatisfyFunctional((*i).pkt)) {
409 pkt->makeResponse();
410 found = true;
411 }
412 ++i;
413 }
414
415 return found;
416 }
417
418 AddrRangeList
419 SerialLink::SerialLinkResponsePort::getAddrRanges() const
420 {
421 return ranges;
422 }