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