serial_link.cc revision 12084:5a3769ff3d55
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 * Authors: Ali Saidi
42 *          Steve Reinhardt
43 *          Andreas Hansson
44 *          Erfan Azarkhish
45 */
46
47/**
48 * @file
49 * Implementation of the SerialLink Class, modeling Hybrid-Memory-Cube's
50 * serial interface.
51 */
52
53#include "mem/serial_link.hh"
54
55#include "base/trace.hh"
56#include "debug/SerialLink.hh"
57#include "params/SerialLink.hh"
58
59SerialLink::SerialLinkSlavePort::SerialLinkSlavePort(const std::string& _name,
60                                         SerialLink& _serial_link,
61                                         SerialLinkMasterPort& _masterPort,
62                                         Cycles _delay, int _resp_limit,
63                                         const std::vector<AddrRange>&
64                                         _ranges)
65    : SlavePort(_name, &_serial_link), serial_link(_serial_link),
66      masterPort(_masterPort), delay(_delay),
67      ranges(_ranges.begin(), _ranges.end()),
68      outstandingResponses(0), retryReq(false),
69      respQueueLimit(_resp_limit),
70      sendEvent([this]{ trySendTiming(); }, _name)
71{
72}
73
74SerialLink::SerialLinkMasterPort::SerialLinkMasterPort(const std::string&
75                                           _name, SerialLink& _serial_link,
76                                           SerialLinkSlavePort& _slavePort,
77                                           Cycles _delay, int _req_limit)
78    : MasterPort(_name, &_serial_link), serial_link(_serial_link),
79      slavePort(_slavePort), delay(_delay), reqQueueLimit(_req_limit),
80      sendEvent([this]{ trySendTiming(); }, _name)
81{
82}
83
84SerialLink::SerialLink(SerialLinkParams *p)
85    : MemObject(p),
86      slavePort(p->name + ".slave", *this, masterPort,
87                ticksToCycles(p->delay), p->resp_size, p->ranges),
88      masterPort(p->name + ".master", *this, slavePort,
89                 ticksToCycles(p->delay), p->req_size),
90      num_lanes(p->num_lanes),
91      link_speed(p->link_speed)
92
93{
94}
95
96BaseMasterPort&
97SerialLink::getMasterPort(const std::string &if_name, PortID idx)
98{
99    if (if_name == "master")
100        return masterPort;
101    else
102        // pass it along to our super class
103        return MemObject::getMasterPort(if_name, idx);
104}
105
106BaseSlavePort&
107SerialLink::getSlavePort(const std::string &if_name, PortID idx)
108{
109    if (if_name == "slave")
110        return slavePort;
111    else
112        // pass it along to our super class
113        return MemObject::getSlavePort(if_name, idx);
114}
115
116void
117SerialLink::init()
118{
119    // make sure both sides are connected and have the same block size
120    if (!slavePort.isConnected() || !masterPort.isConnected())
121        fatal("Both ports of a serial_link must be connected.\n");
122
123    // notify the master side  of our address ranges
124    slavePort.sendRangeChange();
125}
126
127bool
128SerialLink::SerialLinkSlavePort::respQueueFull() const
129{
130    return outstandingResponses == respQueueLimit;
131}
132
133bool
134SerialLink::SerialLinkMasterPort::reqQueueFull() const
135{
136    return transmitList.size() == reqQueueLimit;
137}
138
139bool
140SerialLink::SerialLinkMasterPort::recvTimingResp(PacketPtr pkt)
141{
142    // all checks are done when the request is accepted on the slave
143    // side, so we are guaranteed to have space for the response
144    DPRINTF(SerialLink, "recvTimingResp: %s addr 0x%x\n",
145            pkt->cmdString(), pkt->getAddr());
146
147    DPRINTF(SerialLink, "Request queue size: %d\n", transmitList.size());
148
149    // @todo: We need to pay for this and not just zero it out
150    pkt->headerDelay = pkt->payloadDelay = 0;
151
152    // This is similar to what happens for the request packets:
153    // The serializer will start serialization as soon as it receives the
154    // first flit, but the deserializer (at the host side in this case), will
155    // have to wait to receive the whole packet. So we only account for the
156    // deserialization latency.
157    Cycles cycles = delay;
158    cycles += Cycles(divCeil(pkt->getSize() * 8, serial_link.num_lanes
159                * serial_link.link_speed));
160     Tick t = serial_link.clockEdge(cycles);
161
162    //@todo: If the processor sends two uncached requests towards HMC and the
163    // second one is smaller than the first one. It may happen that the second
164    // one crosses this link faster than the first one (because the packet
165    // waits in the link based on its size). This can reorder the received
166    // response.
167    slavePort.schedTimingResp(pkt, t);
168
169    return true;
170}
171
172bool
173SerialLink::SerialLinkSlavePort::recvTimingReq(PacketPtr pkt)
174{
175    DPRINTF(SerialLink, "recvTimingReq: %s addr 0x%x\n",
176            pkt->cmdString(), pkt->getAddr());
177
178    // we should not see a timing request if we are already in a retry
179    assert(!retryReq);
180
181    DPRINTF(SerialLink, "Response queue size: %d outresp: %d\n",
182            transmitList.size(), outstandingResponses);
183
184    // if the request queue is full then there is no hope
185    if (masterPort.reqQueueFull()) {
186        DPRINTF(SerialLink, "Request queue full\n");
187        retryReq = true;
188    } else if ( !retryReq ) {
189        // look at the response queue if we expect to see a response
190        bool expects_response = pkt->needsResponse() &&
191            !pkt->cacheResponding();
192        if (expects_response) {
193            if (respQueueFull()) {
194                DPRINTF(SerialLink, "Response queue full\n");
195                retryReq = true;
196            } else {
197                // ok to send the request with space for the response
198                DPRINTF(SerialLink, "Reserving space for response\n");
199                assert(outstandingResponses != respQueueLimit);
200                ++outstandingResponses;
201
202                // no need to set retryReq to false as this is already the
203                // case
204            }
205        }
206
207        if (!retryReq) {
208            // @todo: We need to pay for this and not just zero it out
209            pkt->headerDelay = pkt->payloadDelay = 0;
210
211            // We assume that the serializer component at the transmitter side
212            // does not need to receive the whole packet to start the
213            // serialization (this assumption is consistent with the HMC
214            // standard). But the deserializer waits for the complete packet
215            // to check its integrity first. So everytime a packet crosses a
216            // serial link, we should account for its deserialization latency
217            // only.
218            Cycles cycles = delay;
219            cycles += Cycles(divCeil(pkt->getSize() * 8,
220                    serial_link.num_lanes * serial_link.link_speed));
221            Tick t = serial_link.clockEdge(cycles);
222
223            //@todo: If the processor sends two uncached requests towards HMC
224            // and the second one is smaller than the first one. It may happen
225            // that the second one crosses this link faster than the first one
226            // (because the packet waits in the link based on its size).
227            // This can reorder the received response.
228            masterPort.schedTimingReq(pkt, t);
229        }
230    }
231
232    // remember that we are now stalling a packet and that we have to
233    // tell the sending master to retry once space becomes available,
234    // we make no distinction whether the stalling is due to the
235    // request queue or response queue being full
236    return !retryReq;
237}
238
239void
240SerialLink::SerialLinkSlavePort::retryStalledReq()
241{
242    if (retryReq) {
243        DPRINTF(SerialLink, "Request waiting for retry, now retrying\n");
244        retryReq = false;
245        sendRetryReq();
246    }
247}
248
249void
250SerialLink::SerialLinkMasterPort::schedTimingReq(PacketPtr pkt, Tick when)
251{
252    // If we're about to put this packet at the head of the queue, we
253    // need to schedule an event to do the transmit.  Otherwise there
254    // should already be an event scheduled for sending the head
255    // packet.
256    if (transmitList.empty()) {
257        serial_link.schedule(sendEvent, when);
258    }
259
260    assert(transmitList.size() != reqQueueLimit);
261
262    transmitList.emplace_back(DeferredPacket(pkt, when));
263}
264
265
266void
267SerialLink::SerialLinkSlavePort::schedTimingResp(PacketPtr pkt, Tick when)
268{
269    // If we're about to put this packet at the head of the queue, we
270    // need to schedule an event to do the transmit.  Otherwise there
271    // should already be an event scheduled for sending the head
272    // packet.
273    if (transmitList.empty()) {
274        serial_link.schedule(sendEvent, when);
275    }
276
277    transmitList.emplace_back(DeferredPacket(pkt, when));
278}
279
280void
281SerialLink::SerialLinkMasterPort::trySendTiming()
282{
283    assert(!transmitList.empty());
284
285    DeferredPacket req = transmitList.front();
286
287    assert(req.tick <= curTick());
288
289    PacketPtr pkt = req.pkt;
290
291    DPRINTF(SerialLink, "trySend request addr 0x%x, queue size %d\n",
292            pkt->getAddr(), transmitList.size());
293
294    if (sendTimingReq(pkt)) {
295        // send successful
296        transmitList.pop_front();
297
298        DPRINTF(SerialLink, "trySend request successful\n");
299
300        // If there are more packets to send, schedule event to try again.
301        if (!transmitList.empty()) {
302            DeferredPacket next_req = transmitList.front();
303            DPRINTF(SerialLink, "Scheduling next send\n");
304
305            // Make sure bandwidth limitation is met
306            Cycles cycles = Cycles(divCeil(pkt->getSize() * 8,
307                serial_link.num_lanes * serial_link.link_speed));
308            Tick t = serial_link.clockEdge(cycles);
309            serial_link.schedule(sendEvent, std::max(next_req.tick, t));
310        }
311
312        // if we have stalled a request due to a full request queue,
313        // then send a retry at this point, also note that if the
314        // request we stalled was waiting for the response queue
315        // rather than the request queue we might stall it again
316        slavePort.retryStalledReq();
317    }
318
319    // if the send failed, then we try again once we receive a retry,
320    // and therefore there is no need to take any action
321}
322
323void
324SerialLink::SerialLinkSlavePort::trySendTiming()
325{
326    assert(!transmitList.empty());
327
328    DeferredPacket resp = transmitList.front();
329
330    assert(resp.tick <= curTick());
331
332    PacketPtr pkt = resp.pkt;
333
334    DPRINTF(SerialLink, "trySend response addr 0x%x, outstanding %d\n",
335            pkt->getAddr(), outstandingResponses);
336
337    if (sendTimingResp(pkt)) {
338        // send successful
339        transmitList.pop_front();
340        DPRINTF(SerialLink, "trySend response successful\n");
341
342        assert(outstandingResponses != 0);
343        --outstandingResponses;
344
345        // If there are more packets to send, schedule event to try again.
346        if (!transmitList.empty()) {
347            DeferredPacket next_resp = transmitList.front();
348            DPRINTF(SerialLink, "Scheduling next send\n");
349
350            // Make sure bandwidth limitation is met
351            Cycles cycles = Cycles(divCeil(pkt->getSize() * 8,
352                serial_link.num_lanes * serial_link.link_speed));
353            Tick t = serial_link.clockEdge(cycles);
354            serial_link.schedule(sendEvent, std::max(next_resp.tick, t));
355        }
356
357        // if there is space in the request queue and we were stalling
358        // a request, it will definitely be possible to accept it now
359        // since there is guaranteed space in the response queue
360        if (!masterPort.reqQueueFull() && retryReq) {
361            DPRINTF(SerialLink, "Request waiting for retry, now retrying\n");
362            retryReq = false;
363            sendRetryReq();
364        }
365    }
366
367    // if the send failed, then we try again once we receive a retry,
368    // and therefore there is no need to take any action
369}
370
371void
372SerialLink::SerialLinkMasterPort::recvReqRetry()
373{
374    trySendTiming();
375}
376
377void
378SerialLink::SerialLinkSlavePort::recvRespRetry()
379{
380    trySendTiming();
381}
382
383Tick
384SerialLink::SerialLinkSlavePort::recvAtomic(PacketPtr pkt)
385{
386    return delay * serial_link.clockPeriod() + masterPort.sendAtomic(pkt);
387}
388
389void
390SerialLink::SerialLinkSlavePort::recvFunctional(PacketPtr pkt)
391{
392    pkt->pushLabel(name());
393
394    // check the response queue
395    for (auto i = transmitList.begin();  i != transmitList.end(); ++i) {
396        if (pkt->checkFunctional((*i).pkt)) {
397            pkt->makeResponse();
398            return;
399        }
400    }
401
402    // also check the master port's request queue
403    if (masterPort.checkFunctional(pkt)) {
404        return;
405    }
406
407    pkt->popLabel();
408
409    // fall through if pkt still not satisfied
410    masterPort.sendFunctional(pkt);
411}
412
413bool
414SerialLink::SerialLinkMasterPort::checkFunctional(PacketPtr pkt)
415{
416    bool found = false;
417    auto i = transmitList.begin();
418
419    while (i != transmitList.end() && !found) {
420        if (pkt->checkFunctional((*i).pkt)) {
421            pkt->makeResponse();
422            found = true;
423        }
424        ++i;
425    }
426
427    return found;
428}
429
430AddrRangeList
431SerialLink::SerialLinkSlavePort::getAddrRanges() const
432{
433    return ranges;
434}
435
436SerialLink *
437SerialLinkParams::create()
438{
439    return new SerialLink(this);
440}
441