1/*
2 * Copyright (c) 2012,2015,2018 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#ifndef __MEM_PACKET_QUEUE_HH__
45#define __MEM_PACKET_QUEUE_HH__
46
47/**
48 * @file
49 * Declaration of a simple PacketQueue that is associated with
50 * a port on which it attempts to send packets according to the time
51 * stamp given to them at insertion. The packet queue is responsible
52 * for the flow control of the port.
53 */
54
55#include <list>
56
57#include "mem/port.hh"
58#include "sim/drain.hh"
59#include "sim/eventq_impl.hh"
60
61/**
62 * A packet queue is a class that holds deferred packets and later
63 * sends them using the associated slave port or master port.
64 */
65class PacketQueue : public Drainable
66{
67  private:
68    /** A deferred packet, buffered to transmit later. */
69    class DeferredPacket {
70      public:
71        Tick tick;      ///< The tick when the packet is ready to transmit
72        PacketPtr pkt;  ///< Pointer to the packet to transmit
73        DeferredPacket(Tick t, PacketPtr p)
74            : tick(t), pkt(p)
75        {}
76    };
77
78    typedef std::list<DeferredPacket> DeferredPacketList;
79
80    /** A list of outgoing packets. */
81    DeferredPacketList transmitList;
82
83    /** The manager which is used for the event queue */
84    EventManager& em;
85
86    /** Used to schedule sending of deferred packets. */
87    void processSendEvent();
88
89    /** Event used to call processSendEvent. */
90    EventFunctionWrapper sendEvent;
91
92     /*
93      * Optionally disable the sanity check
94      * on the size of the transmitList. The
95      * sanity check will be enabled by default.
96      */
97    bool _disableSanityCheck;
98
99    /**
100     * if true, inserted packets have to be unconditionally scheduled
101     * after the last packet in the queue that references the same
102     * address
103     */
104    bool forceOrder;
105
106  protected:
107
108    /** Label to use for print request packets label stack. */
109    const std::string label;
110
111    /** Remember whether we're awaiting a retry. */
112    bool waitingOnRetry;
113
114    /** Check whether we have a packet ready to go on the transmit list. */
115    bool deferredPacketReady() const
116    { return !transmitList.empty() && transmitList.front().tick <= curTick(); }
117
118    /**
119     * Attempt to send a packet. Note that a subclass of the
120     * PacketQueue can override this method and thus change the
121     * behaviour (as done by the cache for the request queue). The
122     * default implementation sends the head of the transmit list. The
123     * caller must guarantee that the list is non-empty and that the
124     * head packet is scheduled for curTick() (or earlier).
125     */
126    virtual void sendDeferredPacket();
127
128    /**
129     * Send a packet using the appropriate method for the specific
130     * subclass (reuest, response or snoop response).
131     */
132    virtual bool sendTiming(PacketPtr pkt) = 0;
133
134    /**
135     * Create a packet queue, linked to an event manager, and a label
136     * that will be used for functional print request packets.
137     *
138     * @param _em Event manager used for scheduling this queue
139     * @param _label Label to push on the label stack for print request packets
140     * @param force_order Force insertion order for packets with same address
141     * @param disable_sanity_check Flag used to disable the sanity check
142     *        on the size of the transmitList. The check is enabled by default.
143     */
144    PacketQueue(EventManager& _em, const std::string& _label,
145                const std::string& _sendEventName,
146                bool force_order = false,
147                bool disable_sanity_check = false);
148
149    /**
150     * Virtual desctructor since the class may be used as a base class.
151     */
152    virtual ~PacketQueue();
153
154  public:
155
156    /**
157     * Provide a name to simplify debugging.
158     *
159     * @return A complete name, appended to module and port
160     */
161    virtual const std::string name() const = 0;
162
163    /**
164     * Get the size of the queue.
165     */
166    size_t size() const { return transmitList.size(); }
167
168    /**
169     * Get the next packet ready time.
170     */
171    Tick deferredPacketReadyTime() const
172    { return transmitList.empty() ? MaxTick : transmitList.front().tick; }
173
174    /**
175     * Check if a packet corresponding to the same address exists in the
176     * queue.
177     *
178     * @param pkt The packet to compare against.
179     * @param blk_size Block size in bytes.
180     * @return Whether a corresponding packet is found.
181     */
182    bool checkConflict(const PacketPtr pkt, const int blk_size) const;
183
184    /** Check the list of buffered packets against the supplied
185     * functional request. */
186    bool trySatisfyFunctional(PacketPtr pkt);
187
188    /**
189     * Schedule a send event if we are not already waiting for a
190     * retry. If the requested time is before an already scheduled
191     * send event, the event will be rescheduled. If MaxTick is
192     * passed, no event is scheduled. Instead, if we are idle and
193     * asked to drain then check and signal drained.
194     *
195     * @param when time to schedule an event
196     */
197    void schedSendEvent(Tick when);
198
199    /**
200     * Add a packet to the transmit list, and schedule a send event.
201     *
202     * @param pkt Packet to send
203     * @param when Absolute time (in ticks) to send packet
204     */
205    void schedSendTiming(PacketPtr pkt, Tick when);
206
207    /**
208     * Retry sending a packet from the queue. Note that this is not
209     * necessarily the same packet if something has been added with an
210     * earlier time stamp.
211     */
212    void retry();
213
214    /**
215      * This allows a user to explicitly disable the sanity check
216      * on the size of the transmitList, which is enabled by default.
217      * Users must use this function to explicitly disable the sanity
218      * check.
219      */
220    void disableSanityCheck() { _disableSanityCheck = true; }
221
222    DrainState drain() override;
223};
224
225class ReqPacketQueue : public PacketQueue
226{
227
228  protected:
229
230    MasterPort& masterPort;
231
232    // Static definition so it can be called when constructing the parent
233    // without us being completely initialized.
234    static const std::string name(const MasterPort& masterPort,
235                                  const std::string& label)
236    { return masterPort.name() + "-" + label; }
237
238  public:
239
240    /**
241     * Create a request packet queue, linked to an event manager, a
242     * master port, and a label that will be used for functional print
243     * request packets.
244     *
245     * @param _em Event manager used for scheduling this queue
246     * @param _masterPort Master port used to send the packets
247     * @param _label Label to push on the label stack for print request packets
248     */
249    ReqPacketQueue(EventManager& _em, MasterPort& _masterPort,
250                   const std::string _label = "ReqPacketQueue");
251
252    virtual ~ReqPacketQueue() { }
253
254    const std::string name() const
255    { return name(masterPort, label); }
256
257    bool sendTiming(PacketPtr pkt);
258
259};
260
261class SnoopRespPacketQueue : public PacketQueue
262{
263
264  protected:
265
266    MasterPort& masterPort;
267
268    // Static definition so it can be called when constructing the parent
269    // without us being completely initialized.
270    static const std::string name(const MasterPort& masterPort,
271                                  const std::string& label)
272    { return masterPort.name() + "-" + label; }
273
274  public:
275
276    /**
277     * Create a snoop response packet queue, linked to an event
278     * manager, a master port, and a label that will be used for
279     * functional print request packets.
280     *
281     * @param _em Event manager used for scheduling this queue
282     * @param _masterPort Master port used to send the packets
283     * @param force_order Force insertion order for packets with same address
284     * @param _label Label to push on the label stack for print request packets
285     */
286    SnoopRespPacketQueue(EventManager& _em, MasterPort& _masterPort,
287                         bool force_order = false,
288                         const std::string _label = "SnoopRespPacketQueue");
289
290    virtual ~SnoopRespPacketQueue() { }
291
292    const std::string name() const
293    { return name(masterPort, label); }
294
295    bool sendTiming(PacketPtr pkt);
296
297};
298
299class RespPacketQueue : public PacketQueue
300{
301
302  protected:
303
304    SlavePort& slavePort;
305
306    // Static definition so it can be called when constructing the parent
307    // without us being completely initialized.
308    static const std::string name(const SlavePort& slavePort,
309                                  const std::string& label)
310    { return slavePort.name() + "-" + label; }
311
312  public:
313
314    /**
315     * Create a response packet queue, linked to an event manager, a
316     * slave port, and a label that will be used for functional print
317     * request packets.
318     *
319     * @param _em Event manager used for scheduling this queue
320     * @param _slavePort Slave port used to send the packets
321     * @param force_order Force insertion order for packets with same address
322     * @param _label Label to push on the label stack for print request packets
323     */
324    RespPacketQueue(EventManager& _em, SlavePort& _slavePort,
325                    bool force_order = false,
326                    const std::string _label = "RespPacketQueue");
327
328    virtual ~RespPacketQueue() { }
329
330    const std::string name() const
331    { return name(slavePort, label); }
332
333    bool sendTiming(PacketPtr pkt);
334
335};
336
337#endif // __MEM_PACKET_QUEUE_HH__
338