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