packet_queue.hh revision 7823
14486Sbinkertn@umich.edu/*
24486Sbinkertn@umich.edu * Copyright (c) 2006 The Regents of The University of Michigan
34486Sbinkertn@umich.edu * All rights reserved.
44486Sbinkertn@umich.edu *
54486Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without
64486Sbinkertn@umich.edu * modification, are permitted provided that the following conditions are
74486Sbinkertn@umich.edu * met: redistributions of source code must retain the above copyright
84486Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer;
94486Sbinkertn@umich.edu * redistributions in binary form must reproduce the above copyright
104486Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the
114486Sbinkertn@umich.edu * documentation and/or other materials provided with the distribution;
124486Sbinkertn@umich.edu * neither the name of the copyright holders nor the names of its
134486Sbinkertn@umich.edu * contributors may be used to endorse or promote products derived from
144486Sbinkertn@umich.edu * this software without specific prior written permission.
154486Sbinkertn@umich.edu *
164486Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
174486Sbinkertn@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
184486Sbinkertn@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
194486Sbinkertn@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
204486Sbinkertn@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
214486Sbinkertn@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
224486Sbinkertn@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
234486Sbinkertn@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244486Sbinkertn@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254486Sbinkertn@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
264486Sbinkertn@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274486Sbinkertn@umich.edu *
284486Sbinkertn@umich.edu * Authors: Ali Saidi
293102SN/A */
303102SN/A
313102SN/A#ifndef __MEM_TPORT_HH__
322667SN/A#define __MEM_TPORT_HH__
332998SN/A
343584SN/A/**
352667SN/A * @file
364486Sbinkertn@umich.edu *
374486Sbinkertn@umich.edu * Declaration of SimpleTimingPort.
384486Sbinkertn@umich.edu */
394486Sbinkertn@umich.edu
404486Sbinkertn@umich.edu#include "mem/port.hh"
414486Sbinkertn@umich.edu#include "sim/eventq.hh"
424486Sbinkertn@umich.edu#include <list>
431692SN/A#include <string>
441366SN/A
451310SN/A/**
461310SN/A * A simple port for interfacing objects that basically have only
472901SN/A * functional memory behavior (e.g. I/O devices) to the memory system.
483170SN/A * Both timing and functional accesses are implemented in terms of
493170SN/A * atomic accesses.  A derived port class thus only needs to provide
501530SN/A * recvAtomic() to support all memory access modes.
513620SN/A *
523617SN/A * The tricky part is handling recvTiming(), where the response must
533617SN/A * be scheduled separately via a later call to sendTiming().  This
543617SN/A * feature is handled by scheduling an internal event that calls
553617SN/A * sendTiming() after a delay, and optionally rescheduling the
563617SN/A * response if it is nacked.
573584SN/A */
583584SN/Aclass SimpleTimingPort : public Port
593584SN/A{
603584SN/A  protected:
613584SN/A    /** A deferred packet, buffered to transmit later. */
623584SN/A    class DeferredPacket {
633584SN/A      public:
643584SN/A        Tick tick;      ///< The tick when the packet is ready to transmit
653584SN/A        PacketPtr pkt;  ///< Pointer to the packet to transmit
661445SN/A        DeferredPacket(Tick t, PacketPtr p)
671445SN/A            : tick(t), pkt(p)
681310SN/A        {}
691310SN/A    };
701310SN/A
711310SN/A    typedef std::list<DeferredPacket> DeferredPacketList;
721310SN/A    typedef std::list<DeferredPacket>::iterator DeferredPacketIterator;
731310SN/A
741310SN/A    /** A list of outgoing timing response packets that haven't been
751310SN/A     * serviced yet. */
761310SN/A    DeferredPacketList transmitList;
773878SN/A
783878SN/A    /** This function attempts to send deferred packets.  Scheduled to
791310SN/A     * be called in the future via SendEvent. */
801369SN/A    void processSendEvent();
811310SN/A
821634SN/A    /**
834167SN/A     * This class is used to implemented sendTiming() with a delay. When
844167SN/A     * a delay is requested a the event is scheduled if it isn't already.
852998SN/A     * When the event time expires it attempts to send the packet.
862998SN/A     * If it cannot, the packet sent when recvRetry() is called.
872998SN/A     **/
882998SN/A    Event *sendEvent;
892998SN/A
902998SN/A    /** If we need to drain, keep the drain event around until we're done
912998SN/A     * here.*/
922998SN/A    Event *drainEvent;
932998SN/A
942998SN/A    /** Remember whether we're awaiting a retry from the bus. */
952998SN/A    bool waitingOnRetry;
962998SN/A
972998SN/A    /** Check the list of buffered packets against the supplied
982998SN/A     * functional request. */
992998SN/A    bool checkFunctional(PacketPtr funcPkt);
1002998SN/A
1012998SN/A    /** Check whether we have a packet ready to go on the transmit list. */
1022998SN/A    bool deferredPacketReady()
1032998SN/A    { return !transmitList.empty() && transmitList.front().tick <= curTick(); }
1042998SN/A
1053017SN/A    Tick deferredPacketReadyTime()
1062998SN/A    { return transmitList.empty() ? MaxTick : transmitList.front().tick; }
107
108    void
109    schedSendEvent(Tick when)
110    {
111        if (waitingOnRetry) {
112            assert(!sendEvent->scheduled());
113            return;
114        }
115
116        if (!sendEvent->scheduled()) {
117            schedule(sendEvent, when);
118        } else if (sendEvent->when() > when) {
119            reschedule(sendEvent, when);
120        }
121    }
122
123
124    /** Schedule a sendTiming() event to be called in the future.
125     * @param pkt packet to send
126     * @param absolute time (in ticks) to send packet
127     */
128    void schedSendTiming(PacketPtr pkt, Tick when);
129
130    /** Attempt to send the packet at the head of the deferred packet
131     * list.  Caller must guarantee that the deferred packet list is
132     * non-empty and that the head packet is scheduled for curTick() (or
133     * earlier).
134     */
135    void sendDeferredPacket();
136
137    /** This function is notification that the device should attempt to send a
138     * packet again. */
139    virtual void recvRetry();
140
141    /** Implemented using recvAtomic(). */
142    void recvFunctional(PacketPtr pkt);
143
144    /** Implemented using recvAtomic(). */
145    bool recvTiming(PacketPtr pkt);
146
147    /**
148     * Simple ports generally don't care about any status
149     * changes... can always override this in cases where that's not
150     * true. */
151    virtual void recvStatusChange(Status status) { }
152
153
154  public:
155    SimpleTimingPort(std::string pname, MemObject *_owner);
156    ~SimpleTimingPort();
157
158    /** Hook for draining timing accesses from the system.  The
159     * associated SimObject's drain() functions should be implemented
160     * something like this when this class is used:
161     \code
162          PioDevice::drain(Event *de)
163          {
164              unsigned int count;
165              count = SimpleTimingPort->drain(de);
166              if (count)
167                  changeState(Draining);
168              else
169                  changeState(Drained);
170              return count;
171          }
172     \endcode
173    */
174    unsigned int drain(Event *de);
175};
176
177#endif // __MEM_TPORT_HH__
178