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