packet_queue.hh revision 8914
11689SN/A/*
27598Sminkyu.jeong@arm.com * Copyright (c) 2012 ARM Limited
37598Sminkyu.jeong@arm.com * All rights reserved.
47598Sminkyu.jeong@arm.com *
57598Sminkyu.jeong@arm.com * The license below extends only to copyright in the software and shall
67598Sminkyu.jeong@arm.com * not be construed as granting a license to any other intellectual
77598Sminkyu.jeong@arm.com * property including but not limited to intellectual property relating
87598Sminkyu.jeong@arm.com * to a hardware implementation of the functionality of the software
97598Sminkyu.jeong@arm.com * licensed hereunder.  You may use the software subject to the license
107598Sminkyu.jeong@arm.com * terms below provided that you ensure that this notice is replicated
117598Sminkyu.jeong@arm.com * unmodified and in its entirety in all distributions of the software,
127598Sminkyu.jeong@arm.com * modified or unmodified, in source code or in binary form.
137598Sminkyu.jeong@arm.com *
142326SN/A * Copyright (c) 2006 The Regents of The University of Michigan
151689SN/A * All rights reserved.
161689SN/A *
171689SN/A * Redistribution and use in source and binary forms, with or without
181689SN/A * modification, are permitted provided that the following conditions are
191689SN/A * met: redistributions of source code must retain the above copyright
201689SN/A * notice, this list of conditions and the following disclaimer;
211689SN/A * redistributions in binary form must reproduce the above copyright
221689SN/A * notice, this list of conditions and the following disclaimer in the
231689SN/A * documentation and/or other materials provided with the distribution;
241689SN/A * neither the name of the copyright holders nor the names of its
251689SN/A * contributors may be used to endorse or promote products derived from
261689SN/A * this software without specific prior written permission.
271689SN/A *
281689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
291689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
301689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
311689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
321689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
331689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
341689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
351689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
361689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
371689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
381689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392665Ssaidi@eecs.umich.edu *
402665Ssaidi@eecs.umich.edu * Authors: Ali Saidi
411689SN/A *          Andreas Hansson
421689SN/A */
431060SN/A
441060SN/A#ifndef __MEM_PACKET_QUEUE_HH__
451689SN/A#define __MEM_PACKET_QUEUE_HH__
461060SN/A
471060SN/A/**
481060SN/A * @file
497813Ssteve.reinhardt@amd.com * Declaration of a simple PacketQueue that is associated with
506658Snate@binkert.org * a port on which it attempts to send packets according to the time
512292SN/A * stamp given to them at insertion. The packet queue is responsible
521717SN/A * for the flow control of the port, but relies on the module
535529Snate@binkert.org * notifying the queue when a transfer ends.
541060SN/A */
556221Snate@binkert.org
566221Snate@binkert.org#include <list>
571681SN/A
585529Snate@binkert.org#include "mem/port.hh"
592873Sktlim@umich.edu#include "sim/eventq.hh"
604329Sktlim@umich.edu
614329Sktlim@umich.edu/**
624329Sktlim@umich.edu * A packet queue is a class that holds deferred packets and later
632292SN/A * sends them using the associated slave port or master port.
642292SN/A */
652292SN/Aclass PacketQueue
662292SN/A{
672820Sktlim@umich.edu  private:
682292SN/A    /** A deferred packet, buffered to transmit later. */
692820Sktlim@umich.edu    class DeferredPacket {
702820Sktlim@umich.edu      public:
715529Snate@binkert.org        Tick tick;      ///< The tick when the packet is ready to transmit
722307SN/A        PacketPtr pkt;  ///< Pointer to the packet to transmit
731060SN/A        DeferredPacket(Tick t, PacketPtr p)
742292SN/A            : tick(t), pkt(p)
752292SN/A        {}
762292SN/A    };
771060SN/A
781060SN/A    typedef std::list<DeferredPacket> DeferredPacketList;
791060SN/A    typedef std::list<DeferredPacket>::iterator DeferredPacketIterator;
801060SN/A
811060SN/A    /** A list of outgoing timing response packets that haven't been
821060SN/A     * serviced yet. */
831681SN/A    DeferredPacketList transmitList;
846221Snate@binkert.org
856221Snate@binkert.org    /** The manager which is used for the event queue */
866221Snate@binkert.org    EventManager& em;
876221Snate@binkert.org
882292SN/A    /** Label to use for print request packets label stack. */
892292SN/A    const std::string label;
902820Sktlim@umich.edu
912820Sktlim@umich.edu    /** This function attempts to send deferred packets.  Scheduled to
922292SN/A     * be called in the future via SendEvent. */
932292SN/A    void processSendEvent();
942820Sktlim@umich.edu
952820Sktlim@umich.edu    /**
962292SN/A     * Event used to call processSendEvent.
972292SN/A     **/
982292SN/A    EventWrapper<PacketQueue, &PacketQueue::processSendEvent> sendEvent;
992292SN/A
1002292SN/A    /** If we need to drain, keep the drain event around until we're done
1012292SN/A     * here.*/
1022292SN/A    Event *drainEvent;
1032292SN/A
1041060SN/A  protected:
1051060SN/A
1061681SN/A    /** The port used to send the packets. */
1071062SN/A    Port& port;
1082292SN/A
1091062SN/A    /** Remember whether we're awaiting a retry from the bus. */
1102301SN/A    bool waitingOnRetry;
1112301SN/A
1121062SN/A    /** Check whether we have a packet ready to go on the transmit list. */
1132727Sktlim@umich.edu    bool deferredPacketReady()
1141062SN/A    { return !transmitList.empty() && transmitList.front().tick <= curTick(); }
1151062SN/A
1161062SN/A    Tick deferredPacketReadyTime()
1171062SN/A    { return transmitList.empty() ? MaxTick : transmitList.front().tick; }
1181062SN/A
1191062SN/A    /**
1201062SN/A     * Attempt to send the packet at the head of the transmit
1211062SN/A     * list. Caller must guarantee that the list is non-empty and that
1221062SN/A     * the head packet is scheduled for curTick() (or earlier). Note
1231062SN/A     * that a subclass of the PacketQueue can override this method and
1241062SN/A     * thus change the behaviour (as done by the cache).
1251062SN/A     */
1261062SN/A    virtual void sendDeferredPacket();
1271062SN/A
1281062SN/A    /**
1291062SN/A     * Attempt to send the packet at the front of the transmit list,
1301062SN/A     * and set waitingOnRetry accordingly. The packet is temporarily
1311062SN/A     * taken off the list, but put back at the front if not
1321062SN/A     * successfully sent.
1331062SN/A     */
1341062SN/A    void trySendTiming();
1351062SN/A
1361062SN/A    /**
1371062SN/A     * Based on the transmit list, or the provided time, schedule a
1381062SN/A     * send event if there are packets to send. If we are idle and
1391062SN/A     * asked to drain then do so.
1401062SN/A     *
1411062SN/A     * @param time an alternative time for the next send event
1421062SN/A     */
1431062SN/A    void scheduleSend(Tick time = MaxTick);
1441062SN/A
1451062SN/A    /**
1461062SN/A     * Simple ports are generally used as slave ports (i.e. the
1471062SN/A     * respond to requests) and thus do not expect to receive any
1481062SN/A     * range changes (as the neighbouring port has a master role and
1491062SN/A     * do not have any address ranges. A subclass can override the
1501062SN/A     * default behaviuor if needed.
1511062SN/A     */
1521062SN/A    virtual void recvRangeChange() { }
1531062SN/A
1541062SN/A  public:
1552292SN/A
1562292SN/A    /**
1572292SN/A     * Create a packet queue, linked to an event manager, a port used
1582292SN/A     * to send the packets, and potentially give it a label that will
1591062SN/A     * be used for functional print request packets.
1601062SN/A     *
1611062SN/A     * @param _em Event manager used for scheduling this queue
1621062SN/A     * @param _port Port used to send the packets
1631062SN/A     * @param _label Label to push on the label stack for print request packets
1641062SN/A     */
1651062SN/A    PacketQueue(EventManager& _em, Port& _port,
1662292SN/A                const std::string _label = "PacketQueue");
1672292SN/A
1682292SN/A    /**
1692292SN/A     * Virtual desctructor since the class may be used as a base class.
1702292SN/A     */
1712292SN/A    virtual ~PacketQueue();
1722292SN/A
1732292SN/A    /**
1742292SN/A     * Provide a name to simplify debugging. Base it on the port.
1752292SN/A     *
1762301SN/A     * @return A complete name, appended to module and port
1772727Sktlim@umich.edu     */
1782353SN/A    const std::string name() const { return port.name() + "-queue"; }
1792727Sktlim@umich.edu
1802727Sktlim@umich.edu    /** Check the list of buffered packets against the supplied
1812727Sktlim@umich.edu     * functional request. */
1826221Snate@binkert.org    bool checkFunctional(PacketPtr pkt);
1832353SN/A
1842727Sktlim@umich.edu    /**
1852727Sktlim@umich.edu     * Schedule a send even if not already waiting for a retry. If the
1862727Sktlim@umich.edu     * requested time is before an already scheduled send event it
1872727Sktlim@umich.edu     * will be rescheduled.
1882353SN/A     *
1892727Sktlim@umich.edu     * @param when
1902727Sktlim@umich.edu     */
1912727Sktlim@umich.edu    void schedSendEvent(Tick when);
1926221Snate@binkert.org
1932301SN/A    /**
1942301SN/A     * Add a packet to the transmit list, and ensure that a
1952727Sktlim@umich.edu     * processSendEvent is called in the future.
1962301SN/A     *
1972727Sktlim@umich.edu     * @param pkt Packet to send
1986221Snate@binkert.org     * @param when Absolute time (in ticks) to send packet
1992301SN/A     */
2002301SN/A    void schedSendTiming(PacketPtr pkt, Tick when);
2012727Sktlim@umich.edu
2022301SN/A    /**
2032727Sktlim@umich.edu     * Used by a port to notify the queue that a retry was received
2046221Snate@binkert.org     * and that the queue can proceed and retry sending the packet
2052301SN/A     * that caused the wait.
2062301SN/A     */
2072727Sktlim@umich.edu    void retry();
2082301SN/A
2092727Sktlim@umich.edu    /**
2106221Snate@binkert.org     * Hook for draining the packet queue.
2112301SN/A     *
2122301SN/A     * @param de An event which is used to signal back to the caller
2132727Sktlim@umich.edu     * @return A number indicating how many times process will be called
2142301SN/A     */
2152301SN/A    unsigned int drain(Event *de);
2162301SN/A};
2172301SN/A
2182727Sktlim@umich.edu#endif // __MEM_TPORT_HH__
2192727Sktlim@umich.edu