packet.hh revision 6216
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
413348Sbinkertn@umich.edu#include <cassert>
423348Sbinkertn@umich.edu#include <list>
434022Sstever@eecs.umich.edu#include <bitset>
443348Sbinkertn@umich.edu
455735Snate@binkert.org#include "base/cast.hh"
464024Sbinkertn@umich.edu#include "base/compiler.hh"
474610Ssaidi@eecs.umich.edu#include "base/fast_alloc.hh"
485735Snate@binkert.org#include "base/flags.hh"
493940Ssaidi@eecs.umich.edu#include "base/misc.hh"
505314Sstever@gmail.com#include "base/printable.hh"
516216Snate@binkert.org#include "base/types.hh"
522392SN/A#include "mem/request.hh"
534167Sbinkertn@umich.edu#include "sim/core.hh"
542394SN/A
552394SN/Astruct Packet;
563349Sbinkertn@umich.edutypedef Packet *PacketPtr;
572394SN/Atypedef uint8_t* PacketDataPtr;
582812Srdreslin@umich.edutypedef std::list<PacketPtr> PacketList;
592812Srdreslin@umich.edu
604022Sstever@eecs.umich.educlass MemCmd
614022Sstever@eecs.umich.edu{
625735Snate@binkert.org    friend class Packet;
635735Snate@binkert.org
644022Sstever@eecs.umich.edu  public:
655735Snate@binkert.org    /**
665735Snate@binkert.org     * List of all commands associated with a packet.
675735Snate@binkert.org     */
684022Sstever@eecs.umich.edu    enum Command
694022Sstever@eecs.umich.edu    {
704022Sstever@eecs.umich.edu        InvalidCmd,
714022Sstever@eecs.umich.edu        ReadReq,
724473Sstever@eecs.umich.edu        ReadResp,
735319Sstever@gmail.com        ReadRespWithInvalidate,
744022Sstever@eecs.umich.edu        WriteReq,
754022Sstever@eecs.umich.edu        WriteResp,
764022Sstever@eecs.umich.edu        Writeback,
774022Sstever@eecs.umich.edu        SoftPFReq,
784022Sstever@eecs.umich.edu        HardPFReq,
794022Sstever@eecs.umich.edu        SoftPFResp,
804022Sstever@eecs.umich.edu        HardPFResp,
814022Sstever@eecs.umich.edu        WriteInvalidateReq,
824022Sstever@eecs.umich.edu        WriteInvalidateResp,
834022Sstever@eecs.umich.edu        UpgradeReq,
844628Sstever@eecs.umich.edu        UpgradeResp,
854022Sstever@eecs.umich.edu        ReadExReq,
864022Sstever@eecs.umich.edu        ReadExResp,
874626Sstever@eecs.umich.edu        LoadLockedReq,
884626Sstever@eecs.umich.edu        StoreCondReq,
894626Sstever@eecs.umich.edu        StoreCondResp,
904040Ssaidi@eecs.umich.edu        SwapReq,
914040Ssaidi@eecs.umich.edu        SwapResp,
925650Sgblack@eecs.umich.edu        MessageReq,
935650Sgblack@eecs.umich.edu        MessageResp,
944870Sstever@eecs.umich.edu        // Error responses
954870Sstever@eecs.umich.edu        // @TODO these should be classified as responses rather than
964870Sstever@eecs.umich.edu        // requests; coding them as requests initially for backwards
974870Sstever@eecs.umich.edu        // compatibility
984870Sstever@eecs.umich.edu        NetworkNackError,  // nacked at network layer (not by protocol)
994870Sstever@eecs.umich.edu        InvalidDestError,  // packet dest field invalid
1004870Sstever@eecs.umich.edu        BadAddressError,   // memory address invalid
1015314Sstever@gmail.com        // Fake simulator-only commands
1025314Sstever@gmail.com        PrintReq,       // Print state matching address
1034022Sstever@eecs.umich.edu        NUM_MEM_CMDS
1044022Sstever@eecs.umich.edu    };
1054022Sstever@eecs.umich.edu
1064022Sstever@eecs.umich.edu  private:
1075735Snate@binkert.org    /**
1085735Snate@binkert.org     * List of command attributes.
1095735Snate@binkert.org     */
1104022Sstever@eecs.umich.edu    enum Attribute
1114022Sstever@eecs.umich.edu    {
1124626Sstever@eecs.umich.edu        IsRead,         //!< Data flows from responder to requester
1134626Sstever@eecs.umich.edu        IsWrite,        //!< Data flows from requester to responder
1144626Sstever@eecs.umich.edu        IsPrefetch,     //!< Not a demand access
1154022Sstever@eecs.umich.edu        IsInvalidate,
1164626Sstever@eecs.umich.edu        NeedsExclusive, //!< Requires exclusive copy to complete in-cache
1174626Sstever@eecs.umich.edu        IsRequest,      //!< Issued by requester
1184626Sstever@eecs.umich.edu        IsResponse,     //!< Issue by responder
1194626Sstever@eecs.umich.edu        NeedsResponse,  //!< Requester needs response from target
1204022Sstever@eecs.umich.edu        IsSWPrefetch,
1214022Sstever@eecs.umich.edu        IsHWPrefetch,
1226076Sgblack@eecs.umich.edu        IsLlsc,         //!< Alpha/MIPS LL or SC access
1234626Sstever@eecs.umich.edu        HasData,        //!< There is an associated payload
1244870Sstever@eecs.umich.edu        IsError,        //!< Error response
1255314Sstever@gmail.com        IsPrint,        //!< Print state matching address (for debugging)
1264022Sstever@eecs.umich.edu        NUM_COMMAND_ATTRIBUTES
1274022Sstever@eecs.umich.edu    };
1284022Sstever@eecs.umich.edu
1295735Snate@binkert.org    /**
1305735Snate@binkert.org     * Structure that defines attributes and other data associated
1315735Snate@binkert.org     * with a Command.
1325735Snate@binkert.org     */
1335735Snate@binkert.org    struct CommandInfo
1345735Snate@binkert.org    {
1355735Snate@binkert.org        /// Set of attribute flags.
1364022Sstever@eecs.umich.edu        const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
1375735Snate@binkert.org        /// Corresponding response for requests; InvalidCmd if no
1385735Snate@binkert.org        /// response is applicable.
1394022Sstever@eecs.umich.edu        const Command response;
1405735Snate@binkert.org        /// String representation (for printing)
1414022Sstever@eecs.umich.edu        const std::string str;
1424022Sstever@eecs.umich.edu    };
1434022Sstever@eecs.umich.edu
1445735Snate@binkert.org    /// Array to map Command enum to associated info.
1454022Sstever@eecs.umich.edu    static const CommandInfo commandInfo[];
1464022Sstever@eecs.umich.edu
1474022Sstever@eecs.umich.edu  private:
1484022Sstever@eecs.umich.edu
1494022Sstever@eecs.umich.edu    Command cmd;
1504022Sstever@eecs.umich.edu
1515735Snate@binkert.org    bool
1525735Snate@binkert.org    testCmdAttrib(MemCmd::Attribute attrib) const
1535735Snate@binkert.org    {
1544022Sstever@eecs.umich.edu        return commandInfo[cmd].attributes[attrib] != 0;
1554022Sstever@eecs.umich.edu    }
1564022Sstever@eecs.umich.edu
1574022Sstever@eecs.umich.edu  public:
1584022Sstever@eecs.umich.edu
1594022Sstever@eecs.umich.edu    bool isRead() const         { return testCmdAttrib(IsRead); }
1604022Sstever@eecs.umich.edu    bool isWrite()  const       { return testCmdAttrib(IsWrite); }
1614022Sstever@eecs.umich.edu    bool isRequest() const      { return testCmdAttrib(IsRequest); }
1624022Sstever@eecs.umich.edu    bool isResponse() const     { return testCmdAttrib(IsResponse); }
1634870Sstever@eecs.umich.edu    bool needsExclusive() const { return testCmdAttrib(NeedsExclusive); }
1644022Sstever@eecs.umich.edu    bool needsResponse() const  { return testCmdAttrib(NeedsResponse); }
1654022Sstever@eecs.umich.edu    bool isInvalidate() const   { return testCmdAttrib(IsInvalidate); }
1664022Sstever@eecs.umich.edu    bool hasData() const        { return testCmdAttrib(HasData); }
1674626Sstever@eecs.umich.edu    bool isReadWrite() const    { return isRead() && isWrite(); }
1686102Sgblack@eecs.umich.edu    bool isLLSC() const         { return testCmdAttrib(IsLlsc); }
1694870Sstever@eecs.umich.edu    bool isError() const        { return testCmdAttrib(IsError); }
1705314Sstever@gmail.com    bool isPrint() const        { return testCmdAttrib(IsPrint); }
1714022Sstever@eecs.umich.edu
1725735Snate@binkert.org    const Command
1735735Snate@binkert.org    responseCommand() const
1745735Snate@binkert.org    {
1754022Sstever@eecs.umich.edu        return commandInfo[cmd].response;
1764022Sstever@eecs.umich.edu    }
1774022Sstever@eecs.umich.edu
1785735Snate@binkert.org    /// Return the string to a cmd given by idx.
1795735Snate@binkert.org    const std::string &toString() const { return commandInfo[cmd].str; }
1804022Sstever@eecs.umich.edu    int toInt() const { return (int)cmd; }
1814022Sstever@eecs.umich.edu
1825735Snate@binkert.org    MemCmd(Command _cmd) : cmd(_cmd) { }
1835735Snate@binkert.org    MemCmd(int _cmd) : cmd((Command)_cmd) { }
1845735Snate@binkert.org    MemCmd() : cmd(InvalidCmd) { }
1854022Sstever@eecs.umich.edu
1865735Snate@binkert.org    bool operator==(MemCmd c2) const { return (cmd == c2.cmd); }
1875735Snate@binkert.org    bool operator!=(MemCmd c2) const { return (cmd != c2.cmd); }
1884022Sstever@eecs.umich.edu};
1894022Sstever@eecs.umich.edu
1902381SN/A/**
1912662Sstever@eecs.umich.edu * A Packet is used to encapsulate a transfer between two objects in
1922662Sstever@eecs.umich.edu * the memory system (e.g., the L1 and L2 cache).  (In contrast, a
1932662Sstever@eecs.umich.edu * single Request travels all the way from the requester to the
1942662Sstever@eecs.umich.edu * ultimate destination and back, possibly being conveyed by several
1952662Sstever@eecs.umich.edu * different Packets along the way.)
1962381SN/A */
1975314Sstever@gmail.comclass Packet : public FastAlloc, public Printable
1982381SN/A{
1992813Srdreslin@umich.edu  public:
2005735Snate@binkert.org    typedef uint32_t FlagsType;
2015735Snate@binkert.org    typedef ::Flags<FlagsType> Flags;
2025735Snate@binkert.org    typedef short NodeID;
2034022Sstever@eecs.umich.edu
2045735Snate@binkert.org  private:
2055735Snate@binkert.org    static const FlagsType PUBLIC_FLAGS           = 0x00000000;
2065735Snate@binkert.org    static const FlagsType PRIVATE_FLAGS          = 0x00007F0F;
2075735Snate@binkert.org    static const FlagsType COPY_FLAGS             = 0x0000000F;
2085735Snate@binkert.org
2095735Snate@binkert.org    static const FlagsType SHARED                 = 0x00000001;
2105735Snate@binkert.org    // Special control flags
2115735Snate@binkert.org    /// Special timing-mode atomic snoop for multi-level coherence.
2125735Snate@binkert.org    static const FlagsType EXPRESS_SNOOP          = 0x00000002;
2135735Snate@binkert.org    /// Does supplier have exclusive copy?
2145735Snate@binkert.org    /// Useful for multi-level coherence.
2155735Snate@binkert.org    static const FlagsType SUPPLY_EXCLUSIVE       = 0x00000004;
2165735Snate@binkert.org    // Snoop response flags
2175735Snate@binkert.org    static const FlagsType MEM_INHIBIT            = 0x00000008;
2185735Snate@binkert.org    /// Are the 'addr' and 'size' fields valid?
2195735Snate@binkert.org    static const FlagsType VALID_ADDR             = 0x00000100;
2205735Snate@binkert.org    static const FlagsType VALID_SIZE             = 0x00000200;
2215735Snate@binkert.org    /// Is the 'src' field valid?
2225735Snate@binkert.org    static const FlagsType VALID_SRC              = 0x00000400;
2235735Snate@binkert.org    static const FlagsType VALID_DST              = 0x00000800;
2245735Snate@binkert.org    /// Is the data pointer set to a value that shouldn't be freed
2255735Snate@binkert.org    /// when the packet is destroyed?
2265735Snate@binkert.org    static const FlagsType STATIC_DATA            = 0x00001000;
2275735Snate@binkert.org    /// The data pointer points to a value that should be freed when
2285735Snate@binkert.org    /// the packet is destroyed.
2295735Snate@binkert.org    static const FlagsType DYNAMIC_DATA           = 0x00002000;
2305735Snate@binkert.org    /// the data pointer points to an array (thus delete []) needs to
2315735Snate@binkert.org    /// be called on it rather than simply delete.
2325735Snate@binkert.org    static const FlagsType ARRAY_DATA             = 0x00004000;
2335735Snate@binkert.org
2345735Snate@binkert.org    Flags flags;
2355735Snate@binkert.org
2365735Snate@binkert.org  public:
2374022Sstever@eecs.umich.edu    typedef MemCmd::Command Command;
2384022Sstever@eecs.umich.edu
2395735Snate@binkert.org    /// The command field of the packet.
2404870Sstever@eecs.umich.edu    MemCmd cmd;
2414870Sstever@eecs.umich.edu
2425735Snate@binkert.org    /// A pointer to the original request.
2434870Sstever@eecs.umich.edu    RequestPtr req;
2444870Sstever@eecs.umich.edu
2452566SN/A  private:
2465735Snate@binkert.org   /**
2475735Snate@binkert.org    * A pointer to the data being transfered.  It can be differnt
2485735Snate@binkert.org    * sizes at each level of the heirarchy so it belongs in the
2495735Snate@binkert.org    * packet, not request. This may or may not be populated when a
2505735Snate@binkert.org    * responder recieves the packet. If not populated it memory should
2515735Snate@binkert.org    * be allocated.
2522566SN/A    */
2532566SN/A    PacketDataPtr data;
2542566SN/A
2555735Snate@binkert.org    /// The address of the request.  This address could be virtual or
2565735Snate@binkert.org    /// physical, depending on the system configuration.
2572381SN/A    Addr addr;
2582381SN/A
2595735Snate@binkert.org    /// The size of the request or transfer.
2602381SN/A    int size;
2612381SN/A
2625735Snate@binkert.org    /**
2635735Snate@binkert.org     * Device address (e.g., bus ID) of the source of the
2645735Snate@binkert.org     * transaction. The source is not responsible for setting this
2655735Snate@binkert.org     * field; it is set implicitly by the interconnect when the packet
2665735Snate@binkert.org     * is first sent.
2675735Snate@binkert.org     */
2685735Snate@binkert.org    NodeID src;
2692381SN/A
2705735Snate@binkert.org    /**
2715735Snate@binkert.org     * Device address (e.g., bus ID) of the destination of the
2725735Snate@binkert.org     * transaction. The special value Broadcast indicates that the
2735735Snate@binkert.org     * packet should be routed based on its address. This field is
2745735Snate@binkert.org     * initialized in the constructor and is thus always valid (unlike
2755735Snate@binkert.org     * addr, size, and src).
2765735Snate@binkert.org     */
2775735Snate@binkert.org    NodeID dest;
2782641Sstever@eecs.umich.edu
2795735Snate@binkert.org    /**
2805735Snate@binkert.org     * The original value of the command field.  Only valid when the
2814870Sstever@eecs.umich.edu     * current command field is an error condition; in that case, the
2824870Sstever@eecs.umich.edu     * previous contents of the command field are copied here.  This
2834870Sstever@eecs.umich.edu     * field is *not* set on non-error responses.
2844870Sstever@eecs.umich.edu     */
2854870Sstever@eecs.umich.edu    MemCmd origCmd;
2864870Sstever@eecs.umich.edu
2872641Sstever@eecs.umich.edu  public:
2885735Snate@binkert.org    /// Used to calculate latencies for each packet.
2892811Srdreslin@umich.edu    Tick time;
2902811Srdreslin@umich.edu
2915735Snate@binkert.org    /// The time at which the packet will be fully transmitted
2923218Sgblack@eecs.umich.edu    Tick finishTime;
2933218Sgblack@eecs.umich.edu
2945735Snate@binkert.org    /// The time at which the first chunk of the packet will be transmitted
2953218Sgblack@eecs.umich.edu    Tick firstWordTime;
2963218Sgblack@eecs.umich.edu
2975735Snate@binkert.org    /// The special destination address indicating that the packet
2985735Snate@binkert.org    /// should be routed based on its address.
2995735Snate@binkert.org    static const NodeID Broadcast = -1;
3002623SN/A
3015735Snate@binkert.org    /**
3025735Snate@binkert.org     * A virtual base opaque structure used to hold state associated
3035735Snate@binkert.org     * with the packet but specific to the sending device (e.g., an
3045735Snate@binkert.org     * MSHR).  A pointer to this state is returned in the packet's
3055735Snate@binkert.org     * response so that the sender can quickly look up the state
3065735Snate@binkert.org     * needed to process it.  A specific subclass would be derived
3075735Snate@binkert.org     * from this to carry state specific to a particular sending
3085735Snate@binkert.org     * device.
3095735Snate@binkert.org     */
3105735Snate@binkert.org    struct SenderState
3115735Snate@binkert.org    {
3122641Sstever@eecs.umich.edu        virtual ~SenderState() {}
3132641Sstever@eecs.umich.edu    };
3142641Sstever@eecs.umich.edu
3155315Sstever@gmail.com    /**
3165315Sstever@gmail.com     * Object used to maintain state of a PrintReq.  The senderState
3175315Sstever@gmail.com     * field of a PrintReq should always be of this type.
3185315Sstever@gmail.com     */
3195735Snate@binkert.org    class PrintReqState : public SenderState, public FastAlloc
3205735Snate@binkert.org    {
3215735Snate@binkert.org      private:
3225735Snate@binkert.org        /**
3235735Snate@binkert.org         * An entry in the label stack.
3245735Snate@binkert.org         */
3255735Snate@binkert.org        struct LabelStackEntry
3265735Snate@binkert.org        {
3275314Sstever@gmail.com            const std::string label;
3285314Sstever@gmail.com            std::string *prefix;
3295314Sstever@gmail.com            bool labelPrinted;
3305735Snate@binkert.org            LabelStackEntry(const std::string &_label, std::string *_prefix);
3315314Sstever@gmail.com        };
3325314Sstever@gmail.com
3335314Sstever@gmail.com        typedef std::list<LabelStackEntry> LabelStack;
3345314Sstever@gmail.com        LabelStack labelStack;
3355314Sstever@gmail.com
3365314Sstever@gmail.com        std::string *curPrefixPtr;
3375314Sstever@gmail.com
3385314Sstever@gmail.com      public:
3395314Sstever@gmail.com        std::ostream &os;
3405314Sstever@gmail.com        const int verbosity;
3415314Sstever@gmail.com
3425314Sstever@gmail.com        PrintReqState(std::ostream &os, int verbosity = 0);
3435314Sstever@gmail.com        ~PrintReqState();
3445314Sstever@gmail.com
3455735Snate@binkert.org        /**
3465735Snate@binkert.org         * Returns the current line prefix.
3475735Snate@binkert.org         */
3485314Sstever@gmail.com        const std::string &curPrefix() { return *curPrefixPtr; }
3495315Sstever@gmail.com
3505735Snate@binkert.org        /**
3515735Snate@binkert.org         * Push a label onto the label stack, and prepend the given
3525315Sstever@gmail.com         * prefix string onto the current prefix.  Labels will only be
3535735Snate@binkert.org         * printed if an object within the label's scope is printed.
3545735Snate@binkert.org         */
3555314Sstever@gmail.com        void pushLabel(const std::string &lbl,
3565314Sstever@gmail.com                       const std::string &prefix = "  ");
3575735Snate@binkert.org
3585735Snate@binkert.org        /**
3595735Snate@binkert.org         * Pop a label off the label stack.
3605735Snate@binkert.org         */
3615314Sstever@gmail.com        void popLabel();
3625735Snate@binkert.org
3635735Snate@binkert.org        /**
3645735Snate@binkert.org         * Print all of the pending unprinted labels on the
3655315Sstever@gmail.com         * stack. Called by printObj(), so normally not called by
3665735Snate@binkert.org         * users unless bypassing printObj().
3675735Snate@binkert.org         */
3685314Sstever@gmail.com        void printLabels();
3695735Snate@binkert.org
3705735Snate@binkert.org        /**
3715735Snate@binkert.org         * Print a Printable object to os, because it matched the
3725735Snate@binkert.org         * address on a PrintReq.
3735735Snate@binkert.org         */
3745314Sstever@gmail.com        void printObj(Printable *obj);
3755314Sstever@gmail.com    };
3765314Sstever@gmail.com
3775735Snate@binkert.org    /**
3785735Snate@binkert.org     * This packet's sender state.  Devices should use dynamic_cast<>
3795735Snate@binkert.org     * to cast to the state appropriate to the sender.  The intent of
3805735Snate@binkert.org     * this variable is to allow a device to attach extra information
3815735Snate@binkert.org     * to a request.  A response packet must return the sender state
3825735Snate@binkert.org     * that was attached to the original request (even if a new packet
3835735Snate@binkert.org     * is created).
3845735Snate@binkert.org     */
3852662Sstever@eecs.umich.edu    SenderState *senderState;
3862641Sstever@eecs.umich.edu
3875735Snate@binkert.org    /// Return the string name of the cmd field (for debugging and
3885735Snate@binkert.org    /// tracing).
3894022Sstever@eecs.umich.edu    const std::string &cmdString() const { return cmd.toString(); }
3902811Srdreslin@umich.edu
3915735Snate@binkert.org    /// Return the index of this command.
3924022Sstever@eecs.umich.edu    inline int cmdToIndex() const { return cmd.toInt(); }
3932811Srdreslin@umich.edu
3944022Sstever@eecs.umich.edu    bool isRead() const         { return cmd.isRead(); }
3954022Sstever@eecs.umich.edu    bool isWrite()  const       { return cmd.isWrite(); }
3964022Sstever@eecs.umich.edu    bool isRequest() const      { return cmd.isRequest(); }
3974022Sstever@eecs.umich.edu    bool isResponse() const     { return cmd.isResponse(); }
3984870Sstever@eecs.umich.edu    bool needsExclusive() const { return cmd.needsExclusive(); }
3994022Sstever@eecs.umich.edu    bool needsResponse() const  { return cmd.needsResponse(); }
4004022Sstever@eecs.umich.edu    bool isInvalidate() const   { return cmd.isInvalidate(); }
4014022Sstever@eecs.umich.edu    bool hasData() const        { return cmd.hasData(); }
4024040Ssaidi@eecs.umich.edu    bool isReadWrite() const    { return cmd.isReadWrite(); }
4036102Sgblack@eecs.umich.edu    bool isLLSC() const         { return cmd.isLLSC(); }
4044870Sstever@eecs.umich.edu    bool isError() const        { return cmd.isError(); }
4055314Sstever@gmail.com    bool isPrint() const        { return cmd.isPrint(); }
4062812Srdreslin@umich.edu
4074870Sstever@eecs.umich.edu    // Snoop flags
4085735Snate@binkert.org    void assertMemInhibit()     { flags.set(MEM_INHIBIT); }
4095764Snate@binkert.org    bool memInhibitAsserted()   { return flags.isSet(MEM_INHIBIT); }
4105735Snate@binkert.org    void assertShared()         { flags.set(SHARED); }
4115764Snate@binkert.org    bool sharedAsserted()       { return flags.isSet(SHARED); }
4124870Sstever@eecs.umich.edu
4134895Sstever@eecs.umich.edu    // Special control flags
4145735Snate@binkert.org    void setExpressSnoop()      { flags.set(EXPRESS_SNOOP); }
4155764Snate@binkert.org    bool isExpressSnoop()       { return flags.isSet(EXPRESS_SNOOP); }
4165735Snate@binkert.org    void setSupplyExclusive()   { flags.set(SUPPLY_EXCLUSIVE); }
4175764Snate@binkert.org    bool isSupplyExclusive()    { return flags.isSet(SUPPLY_EXCLUSIVE); }
4184895Sstever@eecs.umich.edu
4194870Sstever@eecs.umich.edu    // Network error conditions... encapsulate them as methods since
4204870Sstever@eecs.umich.edu    // their encoding keeps changing (from result field to command
4214870Sstever@eecs.umich.edu    // field, etc.)
4225735Snate@binkert.org    void
4235735Snate@binkert.org    setNacked()
4245735Snate@binkert.org    {
4255735Snate@binkert.org        assert(isResponse());
4265735Snate@binkert.org        cmd = MemCmd::NetworkNackError;
4275735Snate@binkert.org    }
4285735Snate@binkert.org
4295735Snate@binkert.org    void
4305735Snate@binkert.org    setBadAddress()
4315735Snate@binkert.org    {
4325735Snate@binkert.org        assert(isResponse());
4335735Snate@binkert.org        cmd = MemCmd::BadAddressError;
4345735Snate@binkert.org    }
4355735Snate@binkert.org
4365735Snate@binkert.org    bool wasNacked() const     { return cmd == MemCmd::NetworkNackError; }
4375735Snate@binkert.org    bool hadBadAddress() const { return cmd == MemCmd::BadAddressError; }
4384986Ssaidi@eecs.umich.edu    void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
4392814Srdreslin@umich.edu
4405735Snate@binkert.org    /// Accessor function to get the source index of the packet.
4415764Snate@binkert.org    NodeID getSrc() const    { assert(flags.isSet(VALID_SRC)); return src; }
4425735Snate@binkert.org    /// Accessor function to set the source index of the packet.
4435735Snate@binkert.org    void setSrc(NodeID _src) { src = _src; flags.set(VALID_SRC); }
4445735Snate@binkert.org    /// Reset source field, e.g. to retransmit packet on different bus.
4455735Snate@binkert.org    void clearSrc() { flags.clear(VALID_SRC); }
4462641Sstever@eecs.umich.edu
4475735Snate@binkert.org    /// Accessor function for the destination index of the packet.
4485764Snate@binkert.org    NodeID getDest() const     { assert(flags.isSet(VALID_DST)); return dest; }
4495735Snate@binkert.org    /// Accessor function to set the destination index of the packet.
4505735Snate@binkert.org    void setDest(NodeID _dest) { dest = _dest; flags.set(VALID_DST); }
4512381SN/A
4525764Snate@binkert.org    Addr getAddr() const { assert(flags.isSet(VALID_ADDR)); return addr; }
4535764Snate@binkert.org    int getSize() const  { assert(flags.isSet(VALID_SIZE)); return size; }
4545735Snate@binkert.org    Addr getOffset(int blkSize) const { return getAddr() & (Addr)(blkSize - 1); }
4552549SN/A
4565735Snate@binkert.org    /**
4575735Snate@binkert.org     * Constructor.  Note that a Request object must be constructed
4585735Snate@binkert.org     * first, but the Requests's physical address and size fields need
4595735Snate@binkert.org     * not be valid. The command and destination addresses must be
4605735Snate@binkert.org     * supplied.
4615735Snate@binkert.org     */
4625735Snate@binkert.org    Packet(Request *_req, MemCmd _cmd, NodeID _dest)
4635745Snate@binkert.org        :  flags(VALID_DST), cmd(_cmd), req(_req), data(NULL),
4646104Ssteve.reinhardt@amd.com           dest(_dest), time(curTick), senderState(NULL)
4652641Sstever@eecs.umich.edu    {
4666104Ssteve.reinhardt@amd.com        if (req->hasPaddr()) {
4676104Ssteve.reinhardt@amd.com            addr = req->getPaddr();
4686104Ssteve.reinhardt@amd.com            flags.set(VALID_ADDR);
4696104Ssteve.reinhardt@amd.com        }
4706104Ssteve.reinhardt@amd.com        if (req->hasSize()) {
4716104Ssteve.reinhardt@amd.com            size = req->getSize();
4726104Ssteve.reinhardt@amd.com            flags.set(VALID_SIZE);
4736104Ssteve.reinhardt@amd.com        }
4742813Srdreslin@umich.edu    }
4752813Srdreslin@umich.edu
4765735Snate@binkert.org    /**
4775735Snate@binkert.org     * Alternate constructor if you are trying to create a packet with
4785735Snate@binkert.org     * a request that is for a whole block, not the address from the
4795735Snate@binkert.org     * req.  this allows for overriding the size/addr of the req.
4805735Snate@binkert.org     */
4815735Snate@binkert.org    Packet(Request *_req, MemCmd _cmd, NodeID _dest, int _blkSize)
4825745Snate@binkert.org        :  flags(VALID_DST), cmd(_cmd), req(_req), data(NULL),
4836104Ssteve.reinhardt@amd.com           dest(_dest), time(curTick), senderState(NULL)
4842813Srdreslin@umich.edu    {
4856104Ssteve.reinhardt@amd.com        if (req->hasPaddr()) {
4866104Ssteve.reinhardt@amd.com            addr = req->getPaddr() & ~(_blkSize - 1);
4876104Ssteve.reinhardt@amd.com            flags.set(VALID_ADDR);
4886104Ssteve.reinhardt@amd.com        }
4896104Ssteve.reinhardt@amd.com        size = _blkSize;
4906104Ssteve.reinhardt@amd.com        flags.set(VALID_SIZE);
4914626Sstever@eecs.umich.edu    }
4924626Sstever@eecs.umich.edu
4935735Snate@binkert.org    /**
4945735Snate@binkert.org     * Alternate constructor for copying a packet.  Copy all fields
4954887Sstever@eecs.umich.edu     * *except* if the original packet's data was dynamic, don't copy
4964887Sstever@eecs.umich.edu     * that, as we can't guarantee that the new packet's lifetime is
4974887Sstever@eecs.umich.edu     * less than that of the original packet.  In this case the new
4985735Snate@binkert.org     * packet should allocate its own data.
4995735Snate@binkert.org     */
5005735Snate@binkert.org    Packet(Packet *pkt, bool clearFlags = false)
5015735Snate@binkert.org        :  cmd(pkt->cmd), req(pkt->req),
5025764Snate@binkert.org           data(pkt->flags.isSet(STATIC_DATA) ? pkt->data : NULL),
5035735Snate@binkert.org           addr(pkt->addr), size(pkt->size), src(pkt->src), dest(pkt->dest),
5045735Snate@binkert.org           time(curTick), senderState(pkt->senderState)
5054626Sstever@eecs.umich.edu    {
5065735Snate@binkert.org        if (!clearFlags)
5075735Snate@binkert.org            flags.set(pkt->flags & COPY_FLAGS);
5085735Snate@binkert.org
5095735Snate@binkert.org        flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE|VALID_SRC|VALID_DST));
5105735Snate@binkert.org        flags.set(pkt->flags & STATIC_DATA);
5112641Sstever@eecs.umich.edu    }
5122549SN/A
5135735Snate@binkert.org    /**
5145735Snate@binkert.org     * clean up packet variables
5155735Snate@binkert.org     */
5162566SN/A    ~Packet()
5175387Sstever@gmail.com    {
5185387Sstever@gmail.com        // If this is a request packet for which there's no response,
5195387Sstever@gmail.com        // delete the request object here, since the requester will
5205387Sstever@gmail.com        // never get the chance.
5215387Sstever@gmail.com        if (req && isRequest() && !needsResponse())
5225387Sstever@gmail.com            delete req;
5235735Snate@binkert.org        deleteData();
5245387Sstever@gmail.com    }
5252566SN/A
5265735Snate@binkert.org    /**
5275735Snate@binkert.org     * Reinitialize packet address and size from the associated
5285735Snate@binkert.org     * Request object, and reset other fields that may have been
5295735Snate@binkert.org     * modified by a previous transaction.  Typically called when a
5305735Snate@binkert.org     * statically allocated Request/Packet pair is reused for multiple
5315735Snate@binkert.org     * transactions.
5325735Snate@binkert.org     */
5335735Snate@binkert.org    void
5345735Snate@binkert.org    reinitFromRequest()
5355735Snate@binkert.org    {
5366104Ssteve.reinhardt@amd.com        assert(req->hasPaddr());
5374870Sstever@eecs.umich.edu        flags = 0;
5386104Ssteve.reinhardt@amd.com        addr = req->getPaddr();
5396104Ssteve.reinhardt@amd.com        size = req->getSize();
5406104Ssteve.reinhardt@amd.com        time = req->getTime();
5415735Snate@binkert.org
5425735Snate@binkert.org        flags.set(VALID_ADDR|VALID_SIZE);
5435735Snate@binkert.org        deleteData();
5442662Sstever@eecs.umich.edu    }
5452566SN/A
5464626Sstever@eecs.umich.edu    /**
5474626Sstever@eecs.umich.edu     * Take a request packet and modify it in place to be suitable for
5484626Sstever@eecs.umich.edu     * returning as a response to that request.  The source and
5494626Sstever@eecs.umich.edu     * destination fields are *not* modified, as is appropriate for
5504626Sstever@eecs.umich.edu     * atomic accesses.
5514626Sstever@eecs.umich.edu     */
5525735Snate@binkert.org    void
5535735Snate@binkert.org    makeResponse()
5544626Sstever@eecs.umich.edu    {
5552662Sstever@eecs.umich.edu        assert(needsResponse());
5562855Srdreslin@umich.edu        assert(isRequest());
5574986Ssaidi@eecs.umich.edu        origCmd = cmd;
5584022Sstever@eecs.umich.edu        cmd = cmd.responseCommand();
5595745Snate@binkert.org
5605745Snate@binkert.org        dest = src;
5615764Snate@binkert.org        flags.set(VALID_DST, flags.isSet(VALID_SRC));
5625745Snate@binkert.org        flags.clear(VALID_SRC);
5632641Sstever@eecs.umich.edu    }
5642641Sstever@eecs.umich.edu
5655735Snate@binkert.org    void
5665735Snate@binkert.org    makeAtomicResponse()
5674870Sstever@eecs.umich.edu    {
5684870Sstever@eecs.umich.edu        makeResponse();
5694870Sstever@eecs.umich.edu    }
5704870Sstever@eecs.umich.edu
5715735Snate@binkert.org    void
5725735Snate@binkert.org    makeTimingResponse()
5733348Sbinkertn@umich.edu    {
5744870Sstever@eecs.umich.edu        makeResponse();
5753135Srdreslin@umich.edu    }
5763135Srdreslin@umich.edu
5773348Sbinkertn@umich.edu    /**
5783348Sbinkertn@umich.edu     * Take a request packet that has been returned as NACKED and
5793348Sbinkertn@umich.edu     * modify it so that it can be sent out again. Only packets that
5803348Sbinkertn@umich.edu     * need a response can be NACKED, so verify that that is true.
5813348Sbinkertn@umich.edu     */
5823348Sbinkertn@umich.edu    void
5833348Sbinkertn@umich.edu    reinitNacked()
5843348Sbinkertn@umich.edu    {
5854870Sstever@eecs.umich.edu        assert(wasNacked());
5864870Sstever@eecs.umich.edu        cmd = origCmd;
5874870Sstever@eecs.umich.edu        assert(needsResponse());
5884870Sstever@eecs.umich.edu        setDest(Broadcast);
5892685Ssaidi@eecs.umich.edu    }
5902685Ssaidi@eecs.umich.edu
5912685Ssaidi@eecs.umich.edu
5923348Sbinkertn@umich.edu    /**
5933348Sbinkertn@umich.edu     * Set the data pointer to the following value that should not be
5943348Sbinkertn@umich.edu     * freed.
5952566SN/A     */
5962566SN/A    template <typename T>
5973348Sbinkertn@umich.edu    void
5983348Sbinkertn@umich.edu    dataStatic(T *p)
5993348Sbinkertn@umich.edu    {
6005764Snate@binkert.org        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
6013348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
6025735Snate@binkert.org        flags.set(STATIC_DATA);
6033348Sbinkertn@umich.edu    }
6042566SN/A
6053348Sbinkertn@umich.edu    /**
6063348Sbinkertn@umich.edu     * Set the data pointer to a value that should have delete []
6073348Sbinkertn@umich.edu     * called on it.
6083348Sbinkertn@umich.edu     */
6092566SN/A    template <typename T>
6103348Sbinkertn@umich.edu    void
6113348Sbinkertn@umich.edu    dataDynamicArray(T *p)
6123348Sbinkertn@umich.edu    {
6135764Snate@binkert.org        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
6143348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
6155735Snate@binkert.org        flags.set(DYNAMIC_DATA|ARRAY_DATA);
6163348Sbinkertn@umich.edu    }
6173348Sbinkertn@umich.edu
6183348Sbinkertn@umich.edu    /**
6193348Sbinkertn@umich.edu     * set the data pointer to a value that should have delete called
6203348Sbinkertn@umich.edu     * on it.
6213348Sbinkertn@umich.edu     */
6223348Sbinkertn@umich.edu    template <typename T>
6233348Sbinkertn@umich.edu    void
6243348Sbinkertn@umich.edu    dataDynamic(T *p)
6253348Sbinkertn@umich.edu    {
6265764Snate@binkert.org        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
6273348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
6285735Snate@binkert.org        flags.set(DYNAMIC_DATA);
6293348Sbinkertn@umich.edu    }
6303348Sbinkertn@umich.edu
6315735Snate@binkert.org    /**
6325735Snate@binkert.org     * get a pointer to the data ptr.
6335735Snate@binkert.org     */
6343348Sbinkertn@umich.edu    template <typename T>
6353348Sbinkertn@umich.edu    T*
6363348Sbinkertn@umich.edu    getPtr()
6373348Sbinkertn@umich.edu    {
6385764Snate@binkert.org        assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
6393348Sbinkertn@umich.edu        return (T*)data;
6403348Sbinkertn@umich.edu    }
6412566SN/A
6425735Snate@binkert.org    /**
6435735Snate@binkert.org     * return the value of what is pointed to in the packet.
6445735Snate@binkert.org     */
6452566SN/A    template <typename T>
6462592SN/A    T get();
6472566SN/A
6485735Snate@binkert.org    /**
6495735Snate@binkert.org     * set the value in the data pointer to v.
6505735Snate@binkert.org     */
6512566SN/A    template <typename T>
6522592SN/A    void set(T v);
6532566SN/A
6543348Sbinkertn@umich.edu    /**
6554626Sstever@eecs.umich.edu     * Copy data into the packet from the provided pointer.
6564626Sstever@eecs.umich.edu     */
6575735Snate@binkert.org    void
6585735Snate@binkert.org    setData(uint8_t *p)
6594626Sstever@eecs.umich.edu    {
6604626Sstever@eecs.umich.edu        std::memcpy(getPtr<uint8_t>(), p, getSize());
6614626Sstever@eecs.umich.edu    }
6624626Sstever@eecs.umich.edu
6634626Sstever@eecs.umich.edu    /**
6644626Sstever@eecs.umich.edu     * Copy data into the packet from the provided block pointer,
6654626Sstever@eecs.umich.edu     * which is aligned to the given block size.
6664626Sstever@eecs.umich.edu     */
6675735Snate@binkert.org    void
6685735Snate@binkert.org    setDataFromBlock(uint8_t *blk_data, int blkSize)
6694626Sstever@eecs.umich.edu    {
6704626Sstever@eecs.umich.edu        setData(blk_data + getOffset(blkSize));
6714626Sstever@eecs.umich.edu    }
6724626Sstever@eecs.umich.edu
6734626Sstever@eecs.umich.edu    /**
6744626Sstever@eecs.umich.edu     * Copy data from the packet to the provided block pointer, which
6754626Sstever@eecs.umich.edu     * is aligned to the given block size.
6764626Sstever@eecs.umich.edu     */
6775735Snate@binkert.org    void
6785735Snate@binkert.org    writeData(uint8_t *p)
6794626Sstever@eecs.umich.edu    {
6804626Sstever@eecs.umich.edu        std::memcpy(p, getPtr<uint8_t>(), getSize());
6814626Sstever@eecs.umich.edu    }
6824626Sstever@eecs.umich.edu
6834626Sstever@eecs.umich.edu    /**
6844626Sstever@eecs.umich.edu     * Copy data from the packet to the memory at the provided pointer.
6854626Sstever@eecs.umich.edu     */
6865735Snate@binkert.org    void
6875735Snate@binkert.org    writeDataToBlock(uint8_t *blk_data, int blkSize)
6884626Sstever@eecs.umich.edu    {
6894626Sstever@eecs.umich.edu        writeData(blk_data + getOffset(blkSize));
6904626Sstever@eecs.umich.edu    }
6914626Sstever@eecs.umich.edu
6924626Sstever@eecs.umich.edu    /**
6933348Sbinkertn@umich.edu     * delete the data pointed to in the data pointer. Ok to call to
6943348Sbinkertn@umich.edu     * matter how data was allocted.
6953348Sbinkertn@umich.edu     */
6965735Snate@binkert.org    void
6975735Snate@binkert.org    deleteData()
6985735Snate@binkert.org    {
6995764Snate@binkert.org        if (flags.isSet(ARRAY_DATA))
7005735Snate@binkert.org            delete [] data;
7015764Snate@binkert.org        else if (flags.isSet(DYNAMIC_DATA))
7025735Snate@binkert.org            delete data;
7035735Snate@binkert.org
7045735Snate@binkert.org        flags.clear(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA);
7055735Snate@binkert.org        data = NULL;
7065735Snate@binkert.org    }
7072566SN/A
7082566SN/A    /** If there isn't data in the packet, allocate some. */
7095735Snate@binkert.org    void
7105735Snate@binkert.org    allocate()
7115735Snate@binkert.org    {
7125735Snate@binkert.org        if (data) {
7135764Snate@binkert.org            assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
7145745Snate@binkert.org            return;
7155735Snate@binkert.org        }
7165745Snate@binkert.org
7175764Snate@binkert.org        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
7185745Snate@binkert.org        flags.set(DYNAMIC_DATA|ARRAY_DATA);
7195745Snate@binkert.org        data = new uint8_t[getSize()];
7205735Snate@binkert.org    }
7215735Snate@binkert.org
7222568SN/A
7234626Sstever@eecs.umich.edu    /**
7244626Sstever@eecs.umich.edu     * Check a functional request against a memory value represented
7254626Sstever@eecs.umich.edu     * by a base/size pair and an associated data array.  If the
7264626Sstever@eecs.umich.edu     * functional request is a read, it may be satisfied by the memory
7274626Sstever@eecs.umich.edu     * value.  If the functional request is a write, it may update the
7284626Sstever@eecs.umich.edu     * memory value.
7294626Sstever@eecs.umich.edu     */
7305314Sstever@gmail.com    bool checkFunctional(Printable *obj, Addr base, int size, uint8_t *data);
7314626Sstever@eecs.umich.edu
7324626Sstever@eecs.umich.edu    /**
7334626Sstever@eecs.umich.edu     * Check a functional request against a memory value stored in
7345315Sstever@gmail.com     * another packet (i.e. an in-transit request or response).
7354626Sstever@eecs.umich.edu     */
7365735Snate@binkert.org    bool
7375735Snate@binkert.org    checkFunctional(PacketPtr other)
7385735Snate@binkert.org    {
7395735Snate@binkert.org        uint8_t *data = other->hasData() ? other->getPtr<uint8_t>() : NULL;
7405735Snate@binkert.org        return checkFunctional(other, other->getAddr(), other->getSize(),
7415735Snate@binkert.org                               data);
7424626Sstever@eecs.umich.edu    }
7435314Sstever@gmail.com
7445315Sstever@gmail.com    /**
7455315Sstever@gmail.com     * Push label for PrintReq (safe to call unconditionally).
7465315Sstever@gmail.com     */
7475735Snate@binkert.org    void
7485735Snate@binkert.org    pushLabel(const std::string &lbl)
7495735Snate@binkert.org    {
7505735Snate@binkert.org        if (isPrint())
7515735Snate@binkert.org            safe_cast<PrintReqState*>(senderState)->pushLabel(lbl);
7525314Sstever@gmail.com    }
7535314Sstever@gmail.com
7545315Sstever@gmail.com    /**
7555315Sstever@gmail.com     * Pop label for PrintReq (safe to call unconditionally).
7565315Sstever@gmail.com     */
7575735Snate@binkert.org    void
7585735Snate@binkert.org    popLabel()
7595735Snate@binkert.org    {
7605735Snate@binkert.org        if (isPrint())
7615735Snate@binkert.org            safe_cast<PrintReqState*>(senderState)->popLabel();
7625314Sstever@gmail.com    }
7635314Sstever@gmail.com
7645314Sstever@gmail.com    void print(std::ostream &o, int verbosity = 0,
7655314Sstever@gmail.com               const std::string &prefix = "") const;
7662381SN/A};
7672381SN/A
7682381SN/A#endif //__MEM_PACKET_HH
769