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