mem: Allow packet queue to move next send event forward
[gem5.git] / src / mem / packet_queue.cc
1 /*
2 * Copyright (c) 2012 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
49 using namespace std;
50
51 PacketQueue::PacketQueue(EventManager& _em, const std::string& _label)
52 : em(_em), sendEvent(this), drainManager(NULL), label(_label),
53 waitingOnRetry(false)
54 {
55 }
56
57 PacketQueue::~PacketQueue()
58 {
59 }
60
61 void
62 PacketQueue::retry()
63 {
64 DPRINTF(PacketQueue, "Queue %s received retry\n", name());
65 assert(waitingOnRetry);
66 sendDeferredPacket();
67 }
68
69 bool
70 PacketQueue::checkFunctional(PacketPtr pkt)
71 {
72 pkt->pushLabel(label);
73
74 auto i = transmitList.begin();
75 bool found = false;
76
77 while (!found && i != transmitList.end()) {
78 // If the buffered packet contains data, and it overlaps the
79 // current packet, then update data
80 found = pkt->checkFunctional(i->pkt);
81 ++i;
82 }
83
84 pkt->popLabel();
85
86 return found;
87 }
88
89 void
90 PacketQueue::schedSendEvent(Tick when)
91 {
92 // if we are waiting on a retry, do not schedule a send event, and
93 // instead rely on retry being called
94 if (waitingOnRetry) {
95 assert(!sendEvent.scheduled());
96 return;
97 }
98
99 if (!sendEvent.scheduled()) {
100 em.schedule(&sendEvent, when);
101 } else if (sendEvent.when() > when) {
102 em.reschedule(&sendEvent, when);
103 }
104 }
105
106 void
107 PacketQueue::schedSendTiming(PacketPtr pkt, Tick when, bool send_as_snoop)
108 {
109 DPRINTF(PacketQueue, "%s for %s address %x size %d\n", __func__,
110 pkt->cmdString(), pkt->getAddr(), pkt->getSize());
111 // we can still send a packet before the end of this tick
112 assert(when >= curTick());
113
114 // express snoops should never be queued
115 assert(!pkt->isExpressSnoop());
116
117 // add a very basic sanity check on the port to ensure the
118 // invisible buffer is not growing beyond reasonable limits
119 if (transmitList.size() > 100) {
120 panic("Packet queue %s has grown beyond 100 packets\n",
121 name());
122 }
123
124 // nothing on the list, or earlier than current front element,
125 // schedule an event
126 if (transmitList.empty() || when < transmitList.front().tick) {
127 // note that currently we ignore a potentially outstanding retry
128 // and could in theory put a new packet at the head of the
129 // transmit list before retrying the existing packet
130 transmitList.push_front(DeferredPacket(when, pkt, send_as_snoop));
131 schedSendEvent(when);
132 return;
133 }
134
135 // list is non-empty and this belongs at the end
136 if (when >= transmitList.back().tick) {
137 transmitList.push_back(DeferredPacket(when, pkt, send_as_snoop));
138 return;
139 }
140
141 // this belongs in the middle somewhere, insertion sort
142 auto i = transmitList.begin();
143 ++i; // already checked for insertion at front
144 while (i != transmitList.end() && when >= i->tick)
145 ++i;
146 transmitList.insert(i, DeferredPacket(when, pkt, send_as_snoop));
147 }
148
149 void PacketQueue::trySendTiming()
150 {
151 assert(deferredPacketReady());
152
153 DeferredPacket dp = transmitList.front();
154
155 // use the appropriate implementation of sendTiming based on the
156 // type of port associated with the queue, and whether the packet
157 // is to be sent as a snoop or not
158 waitingOnRetry = !sendTiming(dp.pkt, dp.sendAsSnoop);
159
160 if (!waitingOnRetry) {
161 // take the packet off the list
162 transmitList.pop_front();
163 }
164 }
165
166 void
167 PacketQueue::scheduleSend(Tick time)
168 {
169 // the next ready time is either determined by the next deferred packet,
170 // or in the cache through the MSHR ready time
171 Tick nextReady = std::max(std::min(deferredPacketReadyTime(), time),
172 curTick() + 1);
173
174 if (nextReady != MaxTick) {
175 // if the sendTiming caused someone else to call our
176 // recvTiming we could already have an event scheduled, check
177 if (!sendEvent.scheduled()) {
178 em.schedule(&sendEvent, nextReady);
179 } else if (nextReady < sendEvent.when()) {
180 // if the new time is earlier than when the event
181 // currently is scheduled, move it forward
182 em.reschedule(&sendEvent, nextReady);
183 }
184 } else {
185 // no more to send, so if we're draining, we may be done
186 if (drainManager && transmitList.empty() && !sendEvent.scheduled()) {
187 DPRINTF(Drain, "PacketQueue done draining,"
188 "processing drain event\n");
189 drainManager->signalDrainDone();
190 drainManager = NULL;
191 }
192 }
193 }
194
195 void
196 PacketQueue::sendDeferredPacket()
197 {
198 // try to send what is on the list, this will set waitingOnRetry
199 // accordingly
200 trySendTiming();
201
202 // if we succeeded and are not waiting for a retry, schedule the
203 // next send
204 if (!waitingOnRetry) {
205 scheduleSend();
206 }
207 }
208
209 void
210 PacketQueue::processSendEvent()
211 {
212 assert(!waitingOnRetry);
213 sendDeferredPacket();
214 }
215
216 unsigned int
217 PacketQueue::drain(DrainManager *dm)
218 {
219 if (transmitList.empty())
220 return 0;
221 DPRINTF(Drain, "PacketQueue not drained\n");
222 drainManager = dm;
223 return 1;
224 }
225
226 MasterPacketQueue::MasterPacketQueue(EventManager& _em, MasterPort& _masterPort,
227 const std::string _label)
228 : PacketQueue(_em, _label), masterPort(_masterPort)
229 {
230 }
231
232 bool
233 MasterPacketQueue::sendTiming(PacketPtr pkt, bool send_as_snoop)
234 {
235 // attempt to send the packet and return according to the outcome
236 if (!send_as_snoop)
237 return masterPort.sendTimingReq(pkt);
238 else
239 return masterPort.sendTimingSnoopResp(pkt);
240 }
241
242 SlavePacketQueue::SlavePacketQueue(EventManager& _em, SlavePort& _slavePort,
243 const std::string _label)
244 : PacketQueue(_em, _label), slavePort(_slavePort)
245 {
246 }
247
248 bool
249 SlavePacketQueue::sendTiming(PacketPtr pkt, bool send_as_snoop)
250 {
251 // we should never have queued snoop requests
252 assert(!send_as_snoop);
253 return slavePort.sendTimingResp(pkt);
254 }