packet_queue.cc revision 13565:fe1169a7502d
1/*
2 * Copyright (c) 2012,2015,2018 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 *          Andreas Hansson
42 */
43
44#include "mem/packet_queue.hh"
45
46#include "base/trace.hh"
47#include "debug/Drain.hh"
48#include "debug/PacketQueue.hh"
49
50PacketQueue::PacketQueue(EventManager& _em, const std::string& _label,
51                         const std::string& _sendEventName,
52                         bool force_order,
53                         bool disable_sanity_check)
54    : em(_em), sendEvent([this]{ processSendEvent(); }, _sendEventName),
55      _disableSanityCheck(disable_sanity_check),
56      forceOrder(force_order),
57      label(_label), waitingOnRetry(false)
58{
59}
60
61PacketQueue::~PacketQueue()
62{
63}
64
65void
66PacketQueue::retry()
67{
68    DPRINTF(PacketQueue, "Queue %s received retry\n", name());
69    assert(waitingOnRetry);
70    waitingOnRetry = false;
71    sendDeferredPacket();
72}
73
74bool
75PacketQueue::hasAddr(Addr addr) const
76{
77    // caller is responsible for ensuring that all packets have the
78    // same alignment
79    for (const auto& p : transmitList) {
80        if (p.pkt->getAddr() == addr)
81            return true;
82    }
83    return false;
84}
85
86bool
87PacketQueue::trySatisfyFunctional(PacketPtr pkt)
88{
89    pkt->pushLabel(label);
90
91    auto i = transmitList.begin();
92    bool found = false;
93
94    while (!found && i != transmitList.end()) {
95        // If the buffered packet contains data, and it overlaps the
96        // current packet, then update data
97        found = pkt->trySatisfyFunctional(i->pkt);
98        ++i;
99    }
100
101    pkt->popLabel();
102
103    return found;
104}
105
106void
107PacketQueue::schedSendTiming(PacketPtr pkt, Tick when)
108{
109    DPRINTF(PacketQueue, "%s for %s address %x size %d when %lu ord: %i\n",
110            __func__, pkt->cmdString(), pkt->getAddr(), pkt->getSize(), when,
111            forceOrder);
112
113    // we can still send a packet before the end of this tick
114    assert(when >= curTick());
115
116    // express snoops should never be queued
117    assert(!pkt->isExpressSnoop());
118
119    // add a very basic sanity check on the port to ensure the
120    // invisible buffer is not growing beyond reasonable limits
121    if (!_disableSanityCheck && transmitList.size() > 100) {
122        panic("Packet queue %s has grown beyond 100 packets\n",
123              name());
124    }
125
126    // we should either have an outstanding retry, or a send event
127    // scheduled, but there is an unfortunate corner case where the
128    // x86 page-table walker and timing CPU send out a new request as
129    // part of the receiving of a response (called by
130    // PacketQueue::sendDeferredPacket), in which we end up calling
131    // ourselves again before we had a chance to update waitingOnRetry
132    // assert(waitingOnRetry || sendEvent.scheduled());
133
134    // this belongs in the middle somewhere, so search from the end to
135    // order by tick; however, if forceOrder is set, also make sure
136    // not to re-order in front of some existing packet with the same
137    // address
138    auto it = transmitList.end();
139    while (it != transmitList.begin()) {
140        --it;
141        if ((forceOrder && it->pkt->getAddr() == pkt->getAddr()) ||
142            it->tick <= when) {
143            // emplace inserts the element before the position pointed to by
144            // the iterator, so advance it one step
145            transmitList.emplace(++it, when, pkt);
146            return;
147        }
148    }
149    // either the packet list is empty or this has to be inserted
150    // before every other packet
151    transmitList.emplace_front(when, pkt);
152    schedSendEvent(when);
153}
154
155void
156PacketQueue::schedSendEvent(Tick when)
157{
158    // if we are waiting on a retry just hold off
159    if (waitingOnRetry) {
160        DPRINTF(PacketQueue, "Not scheduling send as waiting for retry\n");
161        assert(!sendEvent.scheduled());
162        return;
163    }
164
165    if (when != MaxTick) {
166        // we cannot go back in time, and to be consistent we stick to
167        // one tick in the future
168        when = std::max(when, curTick() + 1);
169        // @todo Revisit the +1
170
171        if (!sendEvent.scheduled()) {
172            em.schedule(&sendEvent, when);
173        } else if (when < sendEvent.when()) {
174            // if the new time is earlier than when the event
175            // currently is scheduled, move it forward
176            em.reschedule(&sendEvent, when);
177        }
178    } else {
179        // we get a MaxTick when there is no more to send, so if we're
180        // draining, we may be done at this point
181        if (drainState() == DrainState::Draining &&
182            transmitList.empty() && !sendEvent.scheduled()) {
183
184            DPRINTF(Drain, "PacketQueue done draining,"
185                    "processing drain event\n");
186            signalDrainDone();
187        }
188    }
189}
190
191void
192PacketQueue::sendDeferredPacket()
193{
194    // sanity checks
195    assert(!waitingOnRetry);
196    assert(deferredPacketReady());
197
198    DeferredPacket dp = transmitList.front();
199
200    // take the packet of the list before sending it, as sending of
201    // the packet in some cases causes a new packet to be enqueued
202    // (most notaly when responding to the timing CPU, leading to a
203    // new request hitting in the L1 icache, leading to a new
204    // response)
205    transmitList.pop_front();
206
207    // use the appropriate implementation of sendTiming based on the
208    // type of queue
209    waitingOnRetry = !sendTiming(dp.pkt);
210
211    // if we succeeded and are not waiting for a retry, schedule the
212    // next send
213    if (!waitingOnRetry) {
214        schedSendEvent(deferredPacketReadyTime());
215    } else {
216        // put the packet back at the front of the list
217        transmitList.emplace_front(dp);
218    }
219}
220
221void
222PacketQueue::processSendEvent()
223{
224    assert(!waitingOnRetry);
225    sendDeferredPacket();
226}
227
228DrainState
229PacketQueue::drain()
230{
231    if (transmitList.empty()) {
232        return DrainState::Drained;
233    } else {
234        DPRINTF(Drain, "PacketQueue not drained\n");
235        return DrainState::Draining;
236    }
237}
238
239ReqPacketQueue::ReqPacketQueue(EventManager& _em, MasterPort& _masterPort,
240                               const std::string _label)
241    : PacketQueue(_em, _label, name(_masterPort, _label)),
242      masterPort(_masterPort)
243{
244}
245
246bool
247ReqPacketQueue::sendTiming(PacketPtr pkt)
248{
249    return masterPort.sendTimingReq(pkt);
250}
251
252SnoopRespPacketQueue::SnoopRespPacketQueue(EventManager& _em,
253                                           MasterPort& _masterPort,
254                                           bool force_order,
255                                           const std::string _label)
256    : PacketQueue(_em, _label, name(_masterPort, _label), force_order),
257      masterPort(_masterPort)
258{
259}
260
261bool
262SnoopRespPacketQueue::sendTiming(PacketPtr pkt)
263{
264    return masterPort.sendTimingSnoopResp(pkt);
265}
266
267RespPacketQueue::RespPacketQueue(EventManager& _em, SlavePort& _slavePort,
268                                 bool force_order,
269                                 const std::string _label)
270    : PacketQueue(_em, _label, name(_slavePort, _label), force_order),
271      slavePort(_slavePort)
272{
273}
274
275bool
276RespPacketQueue::sendTiming(PacketPtr pkt)
277{
278    return slavePort.sendTimingResp(pkt);
279}
280