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