packet.hh revision 2811
12381SN/A/*
22592SN/A * Copyright (c) 2006 The Regents of The University of Michigan
32381SN/A * All rights reserved.
42381SN/A *
52381SN/A * Redistribution and use in source and binary forms, with or without
62381SN/A * modification, are permitted provided that the following conditions are
72381SN/A * met: redistributions of source code must retain the above copyright
82381SN/A * notice, this list of conditions and the following disclaimer;
92381SN/A * redistributions in binary form must reproduce the above copyright
102381SN/A * notice, this list of conditions and the following disclaimer in the
112381SN/A * documentation and/or other materials provided with the distribution;
122381SN/A * neither the name of the copyright holders nor the names of its
132381SN/A * contributors may be used to endorse or promote products derived from
142381SN/A * this software without specific prior written permission.
152381SN/A *
162381SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172381SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182381SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192381SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202381SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212381SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222381SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232381SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242381SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252381SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262381SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Ron Dreslinski
292665Ssaidi@eecs.umich.edu *          Steve Reinhardt
302665Ssaidi@eecs.umich.edu *          Ali Saidi
312381SN/A */
322381SN/A
332381SN/A/**
342381SN/A * @file
352662Sstever@eecs.umich.edu * Declaration of the Packet class.
362381SN/A */
372381SN/A
382381SN/A#ifndef __MEM_PACKET_HH__
392381SN/A#define __MEM_PACKET_HH__
402381SN/A
412392SN/A#include "mem/request.hh"
422423SN/A#include "arch/isa_traits.hh"
432394SN/A#include "sim/root.hh"
442394SN/A
452394SN/Astruct Packet;
462394SN/Atypedef Packet* PacketPtr;
472394SN/Atypedef uint8_t* PacketDataPtr;
482382SN/A
492811Srdreslin@umich.edu//For statistics we need max number of commands, hard code it at
502811Srdreslin@umich.edu//20 for now.  @todo fix later
512811Srdreslin@umich.edu#define NUM_MEM_CMDS 1 << 9
522811Srdreslin@umich.edu
532381SN/A/**
542662Sstever@eecs.umich.edu * A Packet is used to encapsulate a transfer between two objects in
552662Sstever@eecs.umich.edu * the memory system (e.g., the L1 and L2 cache).  (In contrast, a
562662Sstever@eecs.umich.edu * single Request travels all the way from the requester to the
572662Sstever@eecs.umich.edu * ultimate destination and back, possibly being conveyed by several
582662Sstever@eecs.umich.edu * different Packets along the way.)
592381SN/A */
602641Sstever@eecs.umich.educlass Packet
612381SN/A{
622566SN/A  private:
632662Sstever@eecs.umich.edu   /** A pointer to the data being transfered.  It can be differnt
642662Sstever@eecs.umich.edu    *    sizes at each level of the heirarchy so it belongs in the
652662Sstever@eecs.umich.edu    *    packet, not request. This may or may not be populated when a
662662Sstever@eecs.umich.edu    *    responder recieves the packet. If not populated it memory
672662Sstever@eecs.umich.edu    *    should be allocated.
682566SN/A    */
692566SN/A    PacketDataPtr data;
702566SN/A
712662Sstever@eecs.umich.edu    /** Is the data pointer set to a value that shouldn't be freed
722662Sstever@eecs.umich.edu     *   when the packet is destroyed? */
732566SN/A    bool staticData;
742662Sstever@eecs.umich.edu    /** The data pointer points to a value that should be freed when
752662Sstever@eecs.umich.edu     *   the packet is destroyed. */
762566SN/A    bool dynamicData;
772662Sstever@eecs.umich.edu    /** the data pointer points to an array (thus delete [] ) needs to
782662Sstever@eecs.umich.edu     *   be called on it rather than simply delete.*/
792566SN/A    bool arrayData;
802566SN/A
812566SN/A
822662Sstever@eecs.umich.edu    /** The address of the request.  This address could be virtual or
832662Sstever@eecs.umich.edu     *   physical, depending on the system configuration. */
842381SN/A    Addr addr;
852381SN/A
862662Sstever@eecs.umich.edu     /** The size of the request or transfer. */
872381SN/A    int size;
882381SN/A
892662Sstever@eecs.umich.edu    /** Device address (e.g., bus ID) of the source of the
902662Sstever@eecs.umich.edu     *   transaction. The source is not responsible for setting this
912662Sstever@eecs.umich.edu     *   field; it is set implicitly by the interconnect when the
922662Sstever@eecs.umich.edu     *   packet * is first sent.  */
932381SN/A    short src;
942381SN/A
952662Sstever@eecs.umich.edu    /** Device address (e.g., bus ID) of the destination of the
962662Sstever@eecs.umich.edu     *   transaction. The special value Broadcast indicates that the
972662Sstever@eecs.umich.edu     *   packet should be routed based on its address. This field is
982662Sstever@eecs.umich.edu     *   initialized in the constructor and is thus always valid
992662Sstever@eecs.umich.edu     *   (unlike * addr, size, and src). */
1002641Sstever@eecs.umich.edu    short dest;
1012641Sstever@eecs.umich.edu
1022663Sstever@eecs.umich.edu    /** Are the 'addr' and 'size' fields valid? */
1032663Sstever@eecs.umich.edu    bool addrSizeValid;
1042662Sstever@eecs.umich.edu    /** Is the 'src' field valid? */
1052641Sstever@eecs.umich.edu    bool srcValid;
1062641Sstever@eecs.umich.edu
1072641Sstever@eecs.umich.edu  public:
1082641Sstever@eecs.umich.edu
1092811Srdreslin@umich.edu    /** Used to calculate latencies for each packet.*/
1102811Srdreslin@umich.edu    Tick time;
1112811Srdreslin@umich.edu
1122662Sstever@eecs.umich.edu    /** The special destination address indicating that the packet
1132662Sstever@eecs.umich.edu     *   should be routed based on its address. */
1142623SN/A    static const short Broadcast = -1;
1152623SN/A
1162662Sstever@eecs.umich.edu    /** A pointer to the original request. */
1172641Sstever@eecs.umich.edu    RequestPtr req;
1182641Sstever@eecs.umich.edu
1192662Sstever@eecs.umich.edu    /** A virtual base opaque structure used to hold coherence-related
1202662Sstever@eecs.umich.edu     *    state.  A specific subclass would be derived from this to
1212662Sstever@eecs.umich.edu     *    carry state specific to a particular coherence protocol.  */
1222641Sstever@eecs.umich.edu    class CoherenceState {
1232641Sstever@eecs.umich.edu      public:
1242641Sstever@eecs.umich.edu        virtual ~CoherenceState() {}
1252641Sstever@eecs.umich.edu    };
1262641Sstever@eecs.umich.edu
1272662Sstever@eecs.umich.edu    /** This packet's coherence state.  Caches should use
1282662Sstever@eecs.umich.edu     *   dynamic_cast<> to cast to the state appropriate for the
1292662Sstever@eecs.umich.edu     *   system's coherence protocol.  */
1302662Sstever@eecs.umich.edu    CoherenceState *coherence;
1312641Sstever@eecs.umich.edu
1322662Sstever@eecs.umich.edu    /** A virtual base opaque structure used to hold state associated
1332662Sstever@eecs.umich.edu     *    with the packet but specific to the sending device (e.g., an
1342662Sstever@eecs.umich.edu     *    MSHR).  A pointer to this state is returned in the packet's
1352662Sstever@eecs.umich.edu     *    response so that the sender can quickly look up the state
1362662Sstever@eecs.umich.edu     *    needed to process it.  A specific subclass would be derived
1372662Sstever@eecs.umich.edu     *    from this to carry state specific to a particular sending
1382662Sstever@eecs.umich.edu     *    device.  */
1392641Sstever@eecs.umich.edu    class SenderState {
1402641Sstever@eecs.umich.edu      public:
1412641Sstever@eecs.umich.edu        virtual ~SenderState() {}
1422641Sstever@eecs.umich.edu    };
1432641Sstever@eecs.umich.edu
1442662Sstever@eecs.umich.edu    /** This packet's sender state.  Devices should use dynamic_cast<>
1452662Sstever@eecs.umich.edu     *   to cast to the state appropriate to the sender. */
1462662Sstever@eecs.umich.edu    SenderState *senderState;
1472641Sstever@eecs.umich.edu
1482641Sstever@eecs.umich.edu  private:
1492641Sstever@eecs.umich.edu    /** List of command attributes. */
1502641Sstever@eecs.umich.edu    enum CommandAttribute
1512641Sstever@eecs.umich.edu    {
1522641Sstever@eecs.umich.edu        IsRead		= 1 << 0,
1532641Sstever@eecs.umich.edu        IsWrite		= 1 << 1,
1542641Sstever@eecs.umich.edu        IsPrefetch	= 1 << 2,
1552641Sstever@eecs.umich.edu        IsInvalidate	= 1 << 3,
1562641Sstever@eecs.umich.edu        IsRequest	= 1 << 4,
1572641Sstever@eecs.umich.edu        IsResponse 	= 1 << 5,
1582641Sstever@eecs.umich.edu        NeedsResponse	= 1 << 6,
1592811Srdreslin@umich.edu        IsSWPrefetch    = 1 << 7,
1602811Srdreslin@umich.edu        IsHWPrefetch    = 1 << 8
1612641Sstever@eecs.umich.edu    };
1622641Sstever@eecs.umich.edu
1632641Sstever@eecs.umich.edu  public:
1642641Sstever@eecs.umich.edu    /** List of all commands associated with a packet. */
1652641Sstever@eecs.umich.edu    enum Command
1662641Sstever@eecs.umich.edu    {
1672641Sstever@eecs.umich.edu        ReadReq		= IsRead  | IsRequest | NeedsResponse,
1682641Sstever@eecs.umich.edu        WriteReq	= IsWrite | IsRequest | NeedsResponse,
1692641Sstever@eecs.umich.edu        WriteReqNoAck	= IsWrite | IsRequest,
1702641Sstever@eecs.umich.edu        ReadResp	= IsRead  | IsResponse,
1712811Srdreslin@umich.edu        WriteResp	= IsWrite | IsResponse,
1722811Srdreslin@umich.edu        Writeback       = IsWrite | IsRequest,
1732811Srdreslin@umich.edu        SoftPFReq       = IsRead  | IsRequest | IsSWPrefetch | NeedsResponse,
1742811Srdreslin@umich.edu        HardPFReq       = IsRead  | IsRequest | IsHWPrefetch | NeedsResponse,
1752811Srdreslin@umich.edu        SoftPFResp      = IsRead  | IsRequest | IsSWPrefetch | IsResponse,
1762811Srdreslin@umich.edu        HardPFResp      = IsRead  | IsRequest | IsHWPrefetch | IsResponse
1772641Sstever@eecs.umich.edu    };
1782641Sstever@eecs.umich.edu
1792662Sstever@eecs.umich.edu    /** Return the string name of the cmd field (for debugging and
1802662Sstever@eecs.umich.edu     *   tracing). */
1812641Sstever@eecs.umich.edu    const std::string &cmdString() const;
1822381SN/A
1832811Srdreslin@umich.edu    /** Reutrn the string to a cmd given by idx. */
1842811Srdreslin@umich.edu    const std::string &cmdIdxToString(Command idx);
1852811Srdreslin@umich.edu
1862811Srdreslin@umich.edu    /** Return the index of this command. */
1872811Srdreslin@umich.edu    inline int cmdToIndex() const { return (int) cmd; }
1882811Srdreslin@umich.edu
1892662Sstever@eecs.umich.edu    /** The command field of the packet. */
1902381SN/A    Command cmd;
1912381SN/A
1922641Sstever@eecs.umich.edu    bool isRead() 	 { return (cmd & IsRead)  != 0; }
1932641Sstever@eecs.umich.edu    bool isRequest()	 { return (cmd & IsRequest)  != 0; }
1942641Sstever@eecs.umich.edu    bool isResponse()	 { return (cmd & IsResponse) != 0; }
1952641Sstever@eecs.umich.edu    bool needsResponse() { return (cmd & NeedsResponse) != 0; }
1962641Sstever@eecs.umich.edu
1972662Sstever@eecs.umich.edu    /** Possible results of a packet's request. */
1982641Sstever@eecs.umich.edu    enum Result
1992641Sstever@eecs.umich.edu    {
2002641Sstever@eecs.umich.edu        Success,
2012641Sstever@eecs.umich.edu        BadAddress,
2022685Ssaidi@eecs.umich.edu        Nacked,
2032641Sstever@eecs.umich.edu        Unknown
2042641Sstever@eecs.umich.edu    };
2052641Sstever@eecs.umich.edu
2062662Sstever@eecs.umich.edu    /** The result of this packet's request. */
2072641Sstever@eecs.umich.edu    Result result;
2082381SN/A
2092381SN/A    /** Accessor function that returns the source index of the packet. */
2102641Sstever@eecs.umich.edu    short getSrc() const { assert(srcValid); return src; }
2112641Sstever@eecs.umich.edu    void setSrc(short _src) { src = _src; srcValid = true; }
2122381SN/A
2132381SN/A    /** Accessor function that returns the destination index of
2142381SN/A        the packet. */
2152381SN/A    short getDest() const { return dest; }
2162641Sstever@eecs.umich.edu    void setDest(short _dest) { dest = _dest; }
2172549SN/A
2182663Sstever@eecs.umich.edu    Addr getAddr() const { assert(addrSizeValid); return addr; }
2192663Sstever@eecs.umich.edu    int getSize() const { assert(addrSizeValid); return size; }
2202641Sstever@eecs.umich.edu
2212662Sstever@eecs.umich.edu    /** Constructor.  Note that a Request object must be constructed
2222662Sstever@eecs.umich.edu     *   first, but the Requests's physical address and size fields
2232662Sstever@eecs.umich.edu     *   need not be valid. The command and destination addresses
2242662Sstever@eecs.umich.edu     *   must be supplied.  */
2252641Sstever@eecs.umich.edu    Packet(Request *_req, Command _cmd, short _dest)
2262566SN/A        :  data(NULL), staticData(false), dynamicData(false), arrayData(false),
2272641Sstever@eecs.umich.edu           addr(_req->paddr), size(_req->size), dest(_dest),
2282663Sstever@eecs.umich.edu           addrSizeValid(_req->validPaddr),
2292641Sstever@eecs.umich.edu           srcValid(false),
2302641Sstever@eecs.umich.edu           req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
2312662Sstever@eecs.umich.edu           result(Unknown)
2322641Sstever@eecs.umich.edu    {
2332641Sstever@eecs.umich.edu    }
2342549SN/A
2352662Sstever@eecs.umich.edu    /** Destructor. */
2362566SN/A    ~Packet()
2372566SN/A    { deleteData(); }
2382566SN/A
2392662Sstever@eecs.umich.edu    /** Reinitialize packet address and size from the associated
2402662Sstever@eecs.umich.edu     *   Request object, and reset other fields that may have been
2412662Sstever@eecs.umich.edu     *   modified by a previous transaction.  Typically called when a
2422662Sstever@eecs.umich.edu     *   statically allocated Request/Packet pair is reused for
2432662Sstever@eecs.umich.edu     *   multiple transactions. */
2442662Sstever@eecs.umich.edu    void reinitFromRequest() {
2452662Sstever@eecs.umich.edu        assert(req->validPaddr);
2462663Sstever@eecs.umich.edu        addr = req->paddr;
2472663Sstever@eecs.umich.edu        size = req->size;
2482663Sstever@eecs.umich.edu        addrSizeValid = true;
2492662Sstever@eecs.umich.edu        result = Unknown;
2502662Sstever@eecs.umich.edu        if (dynamicData) {
2512662Sstever@eecs.umich.edu            deleteData();
2522662Sstever@eecs.umich.edu            dynamicData = false;
2532662Sstever@eecs.umich.edu            arrayData = false;
2542662Sstever@eecs.umich.edu        }
2552662Sstever@eecs.umich.edu    }
2562566SN/A
2572662Sstever@eecs.umich.edu    /** Take a request packet and modify it in place to be suitable
2582662Sstever@eecs.umich.edu     *   for returning as a response to that request.  Used for timing
2592662Sstever@eecs.umich.edu     *   accesses only.  For atomic and functional accesses, the
2602662Sstever@eecs.umich.edu     *   request packet is always implicitly passed back *without*
2612662Sstever@eecs.umich.edu     *   modifying the command or destination fields, so this function
2622662Sstever@eecs.umich.edu     *   should not be called. */
2632662Sstever@eecs.umich.edu    void makeTimingResponse() {
2642662Sstever@eecs.umich.edu        assert(needsResponse());
2652662Sstever@eecs.umich.edu        int icmd = (int)cmd;
2662662Sstever@eecs.umich.edu        icmd &= ~(IsRequest | NeedsResponse);
2672662Sstever@eecs.umich.edu        icmd |= IsResponse;
2682662Sstever@eecs.umich.edu        cmd = (Command)icmd;
2692662Sstever@eecs.umich.edu        dest = src;
2702662Sstever@eecs.umich.edu        srcValid = false;
2712641Sstever@eecs.umich.edu    }
2722641Sstever@eecs.umich.edu
2732685Ssaidi@eecs.umich.edu    /** Take a request packet that has been returned as NACKED and modify it so
2742685Ssaidi@eecs.umich.edu     * that it can be sent out again. Only packets that need a response can be
2752685Ssaidi@eecs.umich.edu     * NACKED, so verify that that is true. */
2762685Ssaidi@eecs.umich.edu    void reinitNacked() {
2772685Ssaidi@eecs.umich.edu        assert(needsResponse() && result == Nacked);
2782685Ssaidi@eecs.umich.edu        dest =  Broadcast;
2792685Ssaidi@eecs.umich.edu        result = Unknown;
2802685Ssaidi@eecs.umich.edu    }
2812685Ssaidi@eecs.umich.edu
2822685Ssaidi@eecs.umich.edu
2832566SN/A    /** Set the data pointer to the following value that should not be freed. */
2842566SN/A    template <typename T>
2852592SN/A    void dataStatic(T *p);
2862566SN/A
2872566SN/A    /** Set the data pointer to a value that should have delete [] called on it.
2882566SN/A     */
2892566SN/A    template <typename T>
2902592SN/A    void dataDynamicArray(T *p);
2912566SN/A
2922566SN/A    /** set the data pointer to a value that should have delete called on it. */
2932566SN/A    template <typename T>
2942592SN/A    void dataDynamic(T *p);
2952566SN/A
2962566SN/A    /** return the value of what is pointed to in the packet. */
2972566SN/A    template <typename T>
2982592SN/A    T get();
2992566SN/A
3002566SN/A    /** get a pointer to the data ptr. */
3012566SN/A    template <typename T>
3022592SN/A    T* getPtr();
3032566SN/A
3042566SN/A    /** set the value in the data pointer to v. */
3052566SN/A    template <typename T>
3062592SN/A    void set(T v);
3072566SN/A
3082566SN/A    /** delete the data pointed to in the data pointer. Ok to call to matter how
3092566SN/A     * data was allocted. */
3102592SN/A    void deleteData();
3112566SN/A
3122566SN/A    /** If there isn't data in the packet, allocate some. */
3132592SN/A    void allocate();
3142568SN/A
3152568SN/A    /** Do the packet modify the same addresses. */
3162592SN/A    bool intersect(Packet *p);
3172381SN/A};
3182381SN/A
3192630SN/Abool fixPacket(Packet *func, Packet *timing);
3202381SN/A#endif //__MEM_PACKET_HH
321