packet.hh revision 2663
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.
272381SN/A */
282381SN/A
292381SN/A/**
302381SN/A * @file
312662Sstever@eecs.umich.edu * Declaration of the Packet class.
322381SN/A */
332381SN/A
342381SN/A#ifndef __MEM_PACKET_HH__
352381SN/A#define __MEM_PACKET_HH__
362381SN/A
372392SN/A#include "mem/request.hh"
382423SN/A#include "arch/isa_traits.hh"
392394SN/A#include "sim/root.hh"
402394SN/A
412394SN/Astruct Packet;
422394SN/Atypedef Packet* PacketPtr;
432394SN/Atypedef uint8_t* PacketDataPtr;
442382SN/A
452381SN/A/**
462662Sstever@eecs.umich.edu * A Packet is used to encapsulate a transfer between two objects in
472662Sstever@eecs.umich.edu * the memory system (e.g., the L1 and L2 cache).  (In contrast, a
482662Sstever@eecs.umich.edu * single Request travels all the way from the requester to the
492662Sstever@eecs.umich.edu * ultimate destination and back, possibly being conveyed by several
502662Sstever@eecs.umich.edu * different Packets along the way.)
512381SN/A */
522641Sstever@eecs.umich.educlass Packet
532381SN/A{
542566SN/A  private:
552662Sstever@eecs.umich.edu   /** A pointer to the data being transfered.  It can be differnt
562662Sstever@eecs.umich.edu    *    sizes at each level of the heirarchy so it belongs in the
572662Sstever@eecs.umich.edu    *    packet, not request. This may or may not be populated when a
582662Sstever@eecs.umich.edu    *    responder recieves the packet. If not populated it memory
592662Sstever@eecs.umich.edu    *    should be allocated.
602566SN/A    */
612566SN/A    PacketDataPtr data;
622566SN/A
632662Sstever@eecs.umich.edu    /** Is the data pointer set to a value that shouldn't be freed
642662Sstever@eecs.umich.edu     *   when the packet is destroyed? */
652566SN/A    bool staticData;
662662Sstever@eecs.umich.edu    /** The data pointer points to a value that should be freed when
672662Sstever@eecs.umich.edu     *   the packet is destroyed. */
682566SN/A    bool dynamicData;
692662Sstever@eecs.umich.edu    /** the data pointer points to an array (thus delete [] ) needs to
702662Sstever@eecs.umich.edu     *   be called on it rather than simply delete.*/
712566SN/A    bool arrayData;
722566SN/A
732566SN/A
742662Sstever@eecs.umich.edu    /** The address of the request.  This address could be virtual or
752662Sstever@eecs.umich.edu     *   physical, depending on the system configuration. */
762381SN/A    Addr addr;
772381SN/A
782662Sstever@eecs.umich.edu     /** The size of the request or transfer. */
792381SN/A    int size;
802381SN/A
812662Sstever@eecs.umich.edu    /** Device address (e.g., bus ID) of the source of the
822662Sstever@eecs.umich.edu     *   transaction. The source is not responsible for setting this
832662Sstever@eecs.umich.edu     *   field; it is set implicitly by the interconnect when the
842662Sstever@eecs.umich.edu     *   packet * is first sent.  */
852381SN/A    short src;
862381SN/A
872662Sstever@eecs.umich.edu    /** Device address (e.g., bus ID) of the destination of the
882662Sstever@eecs.umich.edu     *   transaction. The special value Broadcast indicates that the
892662Sstever@eecs.umich.edu     *   packet should be routed based on its address. This field is
902662Sstever@eecs.umich.edu     *   initialized in the constructor and is thus always valid
912662Sstever@eecs.umich.edu     *   (unlike * addr, size, and src). */
922641Sstever@eecs.umich.edu    short dest;
932641Sstever@eecs.umich.edu
942663Sstever@eecs.umich.edu    /** Are the 'addr' and 'size' fields valid? */
952663Sstever@eecs.umich.edu    bool addrSizeValid;
962662Sstever@eecs.umich.edu    /** Is the 'src' field valid? */
972641Sstever@eecs.umich.edu    bool srcValid;
982641Sstever@eecs.umich.edu
992641Sstever@eecs.umich.edu  public:
1002641Sstever@eecs.umich.edu
1012662Sstever@eecs.umich.edu    /** The special destination address indicating that the packet
1022662Sstever@eecs.umich.edu     *   should be routed based on its address. */
1032623SN/A    static const short Broadcast = -1;
1042623SN/A
1052662Sstever@eecs.umich.edu    /** A pointer to the original request. */
1062641Sstever@eecs.umich.edu    RequestPtr req;
1072641Sstever@eecs.umich.edu
1082662Sstever@eecs.umich.edu    /** A virtual base opaque structure used to hold coherence-related
1092662Sstever@eecs.umich.edu     *    state.  A specific subclass would be derived from this to
1102662Sstever@eecs.umich.edu     *    carry state specific to a particular coherence protocol.  */
1112641Sstever@eecs.umich.edu    class CoherenceState {
1122641Sstever@eecs.umich.edu      public:
1132641Sstever@eecs.umich.edu        virtual ~CoherenceState() {}
1142641Sstever@eecs.umich.edu    };
1152641Sstever@eecs.umich.edu
1162662Sstever@eecs.umich.edu    /** This packet's coherence state.  Caches should use
1172662Sstever@eecs.umich.edu     *   dynamic_cast<> to cast to the state appropriate for the
1182662Sstever@eecs.umich.edu     *   system's coherence protocol.  */
1192662Sstever@eecs.umich.edu    CoherenceState *coherence;
1202641Sstever@eecs.umich.edu
1212662Sstever@eecs.umich.edu    /** A virtual base opaque structure used to hold state associated
1222662Sstever@eecs.umich.edu     *    with the packet but specific to the sending device (e.g., an
1232662Sstever@eecs.umich.edu     *    MSHR).  A pointer to this state is returned in the packet's
1242662Sstever@eecs.umich.edu     *    response so that the sender can quickly look up the state
1252662Sstever@eecs.umich.edu     *    needed to process it.  A specific subclass would be derived
1262662Sstever@eecs.umich.edu     *    from this to carry state specific to a particular sending
1272662Sstever@eecs.umich.edu     *    device.  */
1282641Sstever@eecs.umich.edu    class SenderState {
1292641Sstever@eecs.umich.edu      public:
1302641Sstever@eecs.umich.edu        virtual ~SenderState() {}
1312641Sstever@eecs.umich.edu    };
1322641Sstever@eecs.umich.edu
1332662Sstever@eecs.umich.edu    /** This packet's sender state.  Devices should use dynamic_cast<>
1342662Sstever@eecs.umich.edu     *   to cast to the state appropriate to the sender. */
1352662Sstever@eecs.umich.edu    SenderState *senderState;
1362641Sstever@eecs.umich.edu
1372641Sstever@eecs.umich.edu  private:
1382641Sstever@eecs.umich.edu    /** List of command attributes. */
1392641Sstever@eecs.umich.edu    enum CommandAttribute
1402641Sstever@eecs.umich.edu    {
1412641Sstever@eecs.umich.edu        IsRead		= 1 << 0,
1422641Sstever@eecs.umich.edu        IsWrite		= 1 << 1,
1432641Sstever@eecs.umich.edu        IsPrefetch	= 1 << 2,
1442641Sstever@eecs.umich.edu        IsInvalidate	= 1 << 3,
1452641Sstever@eecs.umich.edu        IsRequest	= 1 << 4,
1462641Sstever@eecs.umich.edu        IsResponse 	= 1 << 5,
1472641Sstever@eecs.umich.edu        NeedsResponse	= 1 << 6,
1482641Sstever@eecs.umich.edu    };
1492641Sstever@eecs.umich.edu
1502641Sstever@eecs.umich.edu  public:
1512641Sstever@eecs.umich.edu    /** List of all commands associated with a packet. */
1522641Sstever@eecs.umich.edu    enum Command
1532641Sstever@eecs.umich.edu    {
1542641Sstever@eecs.umich.edu        ReadReq		= IsRead  | IsRequest | NeedsResponse,
1552641Sstever@eecs.umich.edu        WriteReq	= IsWrite | IsRequest | NeedsResponse,
1562641Sstever@eecs.umich.edu        WriteReqNoAck	= IsWrite | IsRequest,
1572641Sstever@eecs.umich.edu        ReadResp	= IsRead  | IsResponse,
1582641Sstever@eecs.umich.edu        WriteResp	= IsWrite | IsResponse
1592641Sstever@eecs.umich.edu    };
1602641Sstever@eecs.umich.edu
1612662Sstever@eecs.umich.edu    /** Return the string name of the cmd field (for debugging and
1622662Sstever@eecs.umich.edu     *   tracing). */
1632641Sstever@eecs.umich.edu    const std::string &cmdString() const;
1642381SN/A
1652662Sstever@eecs.umich.edu    /** The command field of the packet. */
1662381SN/A    Command cmd;
1672381SN/A
1682641Sstever@eecs.umich.edu    bool isRead() 	 { return (cmd & IsRead)  != 0; }
1692641Sstever@eecs.umich.edu    bool isRequest()	 { return (cmd & IsRequest)  != 0; }
1702641Sstever@eecs.umich.edu    bool isResponse()	 { return (cmd & IsResponse) != 0; }
1712641Sstever@eecs.umich.edu    bool needsResponse() { return (cmd & NeedsResponse) != 0; }
1722641Sstever@eecs.umich.edu
1732662Sstever@eecs.umich.edu    /** Possible results of a packet's request. */
1742641Sstever@eecs.umich.edu    enum Result
1752641Sstever@eecs.umich.edu    {
1762641Sstever@eecs.umich.edu        Success,
1772641Sstever@eecs.umich.edu        BadAddress,
1782641Sstever@eecs.umich.edu        Unknown
1792641Sstever@eecs.umich.edu    };
1802641Sstever@eecs.umich.edu
1812662Sstever@eecs.umich.edu    /** The result of this packet's request. */
1822641Sstever@eecs.umich.edu    Result result;
1832381SN/A
1842381SN/A    /** Accessor function that returns the source index of the packet. */
1852641Sstever@eecs.umich.edu    short getSrc() const { assert(srcValid); return src; }
1862641Sstever@eecs.umich.edu    void setSrc(short _src) { src = _src; srcValid = true; }
1872381SN/A
1882381SN/A    /** Accessor function that returns the destination index of
1892381SN/A        the packet. */
1902381SN/A    short getDest() const { return dest; }
1912641Sstever@eecs.umich.edu    void setDest(short _dest) { dest = _dest; }
1922549SN/A
1932663Sstever@eecs.umich.edu    Addr getAddr() const { assert(addrSizeValid); return addr; }
1942663Sstever@eecs.umich.edu    int getSize() const { assert(addrSizeValid); return size; }
1952641Sstever@eecs.umich.edu
1962662Sstever@eecs.umich.edu    /** Constructor.  Note that a Request object must be constructed
1972662Sstever@eecs.umich.edu     *   first, but the Requests's physical address and size fields
1982662Sstever@eecs.umich.edu     *   need not be valid. The command and destination addresses
1992662Sstever@eecs.umich.edu     *   must be supplied.  */
2002641Sstever@eecs.umich.edu    Packet(Request *_req, Command _cmd, short _dest)
2012566SN/A        :  data(NULL), staticData(false), dynamicData(false), arrayData(false),
2022641Sstever@eecs.umich.edu           addr(_req->paddr), size(_req->size), dest(_dest),
2032663Sstever@eecs.umich.edu           addrSizeValid(_req->validPaddr),
2042641Sstever@eecs.umich.edu           srcValid(false),
2052641Sstever@eecs.umich.edu           req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
2062662Sstever@eecs.umich.edu           result(Unknown)
2072641Sstever@eecs.umich.edu    {
2082641Sstever@eecs.umich.edu    }
2092549SN/A
2102662Sstever@eecs.umich.edu    /** Destructor. */
2112566SN/A    ~Packet()
2122566SN/A    { deleteData(); }
2132566SN/A
2142662Sstever@eecs.umich.edu    /** Reinitialize packet address and size from the associated
2152662Sstever@eecs.umich.edu     *   Request object, and reset other fields that may have been
2162662Sstever@eecs.umich.edu     *   modified by a previous transaction.  Typically called when a
2172662Sstever@eecs.umich.edu     *   statically allocated Request/Packet pair is reused for
2182662Sstever@eecs.umich.edu     *   multiple transactions. */
2192662Sstever@eecs.umich.edu    void reinitFromRequest() {
2202662Sstever@eecs.umich.edu        assert(req->validPaddr);
2212663Sstever@eecs.umich.edu        addr = req->paddr;
2222663Sstever@eecs.umich.edu        size = req->size;
2232663Sstever@eecs.umich.edu        addrSizeValid = true;
2242662Sstever@eecs.umich.edu        result = Unknown;
2252662Sstever@eecs.umich.edu        if (dynamicData) {
2262662Sstever@eecs.umich.edu            deleteData();
2272662Sstever@eecs.umich.edu            dynamicData = false;
2282662Sstever@eecs.umich.edu            arrayData = false;
2292662Sstever@eecs.umich.edu        }
2302662Sstever@eecs.umich.edu    }
2312566SN/A
2322662Sstever@eecs.umich.edu    /** Take a request packet and modify it in place to be suitable
2332662Sstever@eecs.umich.edu     *   for returning as a response to that request.  Used for timing
2342662Sstever@eecs.umich.edu     *   accesses only.  For atomic and functional accesses, the
2352662Sstever@eecs.umich.edu     *   request packet is always implicitly passed back *without*
2362662Sstever@eecs.umich.edu     *   modifying the command or destination fields, so this function
2372662Sstever@eecs.umich.edu     *   should not be called. */
2382662Sstever@eecs.umich.edu    void makeTimingResponse() {
2392662Sstever@eecs.umich.edu        assert(needsResponse());
2402662Sstever@eecs.umich.edu        int icmd = (int)cmd;
2412662Sstever@eecs.umich.edu        icmd &= ~(IsRequest | NeedsResponse);
2422662Sstever@eecs.umich.edu        icmd |= IsResponse;
2432662Sstever@eecs.umich.edu        cmd = (Command)icmd;
2442662Sstever@eecs.umich.edu        dest = src;
2452662Sstever@eecs.umich.edu        srcValid = false;
2462641Sstever@eecs.umich.edu    }
2472641Sstever@eecs.umich.edu
2482566SN/A    /** Set the data pointer to the following value that should not be freed. */
2492566SN/A    template <typename T>
2502592SN/A    void dataStatic(T *p);
2512566SN/A
2522566SN/A    /** Set the data pointer to a value that should have delete [] called on it.
2532566SN/A     */
2542566SN/A    template <typename T>
2552592SN/A    void dataDynamicArray(T *p);
2562566SN/A
2572566SN/A    /** set the data pointer to a value that should have delete called on it. */
2582566SN/A    template <typename T>
2592592SN/A    void dataDynamic(T *p);
2602566SN/A
2612566SN/A    /** return the value of what is pointed to in the packet. */
2622566SN/A    template <typename T>
2632592SN/A    T get();
2642566SN/A
2652566SN/A    /** get a pointer to the data ptr. */
2662566SN/A    template <typename T>
2672592SN/A    T* getPtr();
2682566SN/A
2692566SN/A    /** set the value in the data pointer to v. */
2702566SN/A    template <typename T>
2712592SN/A    void set(T v);
2722566SN/A
2732566SN/A    /** delete the data pointed to in the data pointer. Ok to call to matter how
2742566SN/A     * data was allocted. */
2752592SN/A    void deleteData();
2762566SN/A
2772566SN/A    /** If there isn't data in the packet, allocate some. */
2782592SN/A    void allocate();
2792568SN/A
2802568SN/A    /** Do the packet modify the same addresses. */
2812592SN/A    bool intersect(Packet *p);
2822381SN/A};
2832381SN/A
2842630SN/Abool fixPacket(Packet *func, Packet *timing);
2852381SN/A#endif //__MEM_PACKET_HH
286