2 * Copyright (c) 2012,2015,2018-2020 ARM Limited
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.
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
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.
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.
41 #include "mem/packet_queue.hh"
43 #include "base/trace.hh"
44 #include "debug/Drain.hh"
45 #include "debug/PacketQueue.hh"
47 PacketQueue::PacketQueue(EventManager
& _em
, const std::string
& _label
,
48 const std::string
& _sendEventName
,
50 bool disable_sanity_check
)
51 : em(_em
), sendEvent([this]{ processSendEvent(); }, _sendEventName
),
52 _disableSanityCheck(disable_sanity_check
),
53 forceOrder(force_order
),
54 label(_label
), waitingOnRetry(false)
58 PacketQueue::~PacketQueue()
65 DPRINTF(PacketQueue
, "Queue %s received retry\n", name());
66 assert(waitingOnRetry
);
67 waitingOnRetry
= false;
72 PacketQueue::checkConflict(const PacketPtr pkt
, const int blk_size
) const
74 // caller is responsible for ensuring that all packets have the
76 for (const auto& p
: transmitList
) {
77 if (p
.pkt
->matchBlockAddr(pkt
, blk_size
))
84 PacketQueue::trySatisfyFunctional(PacketPtr pkt
)
86 pkt
->pushLabel(label
);
88 auto i
= transmitList
.begin();
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
->trySatisfyFunctional(i
->pkt
);
104 PacketQueue::schedSendTiming(PacketPtr pkt
, Tick when
)
106 DPRINTF(PacketQueue
, "%s for %s address %x size %d when %lu ord: %i\n",
107 __func__
, pkt
->cmdString(), pkt
->getAddr(), pkt
->getSize(), when
,
110 // we can still send a packet before the end of this tick
111 assert(when
>= curTick());
113 // express snoops should never be queued
114 assert(!pkt
->isExpressSnoop());
116 // add a very basic sanity check on the port to ensure the
117 // invisible buffer is not growing beyond reasonable limits
118 if (!_disableSanityCheck
&& transmitList
.size() > 128) {
119 panic("Packet queue %s has grown beyond 128 packets\n",
123 // we should either have an outstanding retry, or a send event
124 // scheduled, but there is an unfortunate corner case where the
125 // x86 page-table walker and timing CPU send out a new request as
126 // part of the receiving of a response (called by
127 // PacketQueue::sendDeferredPacket), in which we end up calling
128 // ourselves again before we had a chance to update waitingOnRetry
129 // assert(waitingOnRetry || sendEvent.scheduled());
131 // this belongs in the middle somewhere, so search from the end to
132 // order by tick; however, if forceOrder is set, also make sure
133 // not to re-order in front of some existing packet with the same
135 auto it
= transmitList
.end();
136 while (it
!= transmitList
.begin()) {
138 if ((forceOrder
&& it
->pkt
->matchAddr(pkt
)) || it
->tick
<= when
) {
139 // emplace inserts the element before the position pointed to by
140 // the iterator, so advance it one step
141 transmitList
.emplace(++it
, when
, pkt
);
145 // either the packet list is empty or this has to be inserted
146 // before every other packet
147 transmitList
.emplace_front(when
, pkt
);
148 schedSendEvent(when
);
152 PacketQueue::schedSendEvent(Tick when
)
154 // if we are waiting on a retry just hold off
155 if (waitingOnRetry
) {
156 DPRINTF(PacketQueue
, "Not scheduling send as waiting for retry\n");
157 assert(!sendEvent
.scheduled());
161 if (when
!= MaxTick
) {
162 // we cannot go back in time, and to be consistent we stick to
163 // one tick in the future
164 when
= std::max(when
, curTick() + 1);
165 // @todo Revisit the +1
167 if (!sendEvent
.scheduled()) {
168 em
.schedule(&sendEvent
, when
);
169 } else if (when
< sendEvent
.when()) {
170 // if the new time is earlier than when the event
171 // currently is scheduled, move it forward
172 em
.reschedule(&sendEvent
, when
);
175 // we get a MaxTick when there is no more to send, so if we're
176 // draining, we may be done at this point
177 if (drainState() == DrainState::Draining
&&
178 transmitList
.empty() && !sendEvent
.scheduled()) {
180 DPRINTF(Drain
, "PacketQueue done draining,"
181 "processing drain event\n");
188 PacketQueue::sendDeferredPacket()
191 assert(!waitingOnRetry
);
192 assert(deferredPacketReady());
194 DeferredPacket dp
= transmitList
.front();
196 // take the packet of the list before sending it, as sending of
197 // the packet in some cases causes a new packet to be enqueued
198 // (most notaly when responding to the timing CPU, leading to a
199 // new request hitting in the L1 icache, leading to a new
201 transmitList
.pop_front();
203 // use the appropriate implementation of sendTiming based on the
205 waitingOnRetry
= !sendTiming(dp
.pkt
);
207 // if we succeeded and are not waiting for a retry, schedule the
209 if (!waitingOnRetry
) {
210 schedSendEvent(deferredPacketReadyTime());
212 // put the packet back at the front of the list
213 transmitList
.emplace_front(dp
);
218 PacketQueue::processSendEvent()
220 assert(!waitingOnRetry
);
221 sendDeferredPacket();
227 if (transmitList
.empty()) {
228 return DrainState::Drained
;
230 DPRINTF(Drain
, "PacketQueue not drained\n");
231 return DrainState::Draining
;
235 ReqPacketQueue::ReqPacketQueue(EventManager
& _em
, RequestPort
& _mem_side_port
,
236 const std::string _label
)
237 : PacketQueue(_em
, _label
, name(_mem_side_port
, _label
)),
238 memSidePort(_mem_side_port
)
243 ReqPacketQueue::sendTiming(PacketPtr pkt
)
245 return memSidePort
.sendTimingReq(pkt
);
248 SnoopRespPacketQueue::SnoopRespPacketQueue(EventManager
& _em
,
249 RequestPort
& _mem_side_port
,
251 const std::string _label
)
252 : PacketQueue(_em
, _label
, name(_mem_side_port
, _label
), force_order
),
253 memSidePort(_mem_side_port
)
258 SnoopRespPacketQueue::sendTiming(PacketPtr pkt
)
260 return memSidePort
.sendTimingSnoopResp(pkt
);
263 RespPacketQueue::RespPacketQueue(EventManager
& _em
,
264 ResponsePort
& _cpu_side_port
,
266 const std::string _label
)
267 : PacketQueue(_em
, _label
, name(_cpu_side_port
, _label
), force_order
),
268 cpuSidePort(_cpu_side_port
)
273 RespPacketQueue::sendTiming(PacketPtr pkt
)
275 return cpuSidePort
.sendTimingResp(pkt
);