packet.hh revision 7465
11689SN/A/*
27849SAli.Saidi@ARM.com * Copyright (c) 2006 The Regents of The University of Michigan
37849SAli.Saidi@ARM.com * Copyright (c) 2010 Advancec Micro Devices, Inc.
47849SAli.Saidi@ARM.com * All rights reserved.
57849SAli.Saidi@ARM.com *
67849SAli.Saidi@ARM.com * Redistribution and use in source and binary forms, with or without
77849SAli.Saidi@ARM.com * modification, are permitted provided that the following conditions are
87849SAli.Saidi@ARM.com * met: redistributions of source code must retain the above copyright
97849SAli.Saidi@ARM.com * notice, this list of conditions and the following disclaimer;
107849SAli.Saidi@ARM.com * redistributions in binary form must reproduce the above copyright
117849SAli.Saidi@ARM.com * notice, this list of conditions and the following disclaimer in the
127849SAli.Saidi@ARM.com * documentation and/or other materials provided with the distribution;
137849SAli.Saidi@ARM.com * neither the name of the copyright holders nor the names of its
142329SN/A * contributors may be used to endorse or promote products derived from
151689SN/A * this software without specific prior written permission.
161689SN/A *
171689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
181689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
191689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
201689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
211689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
221689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
231689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
241689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
271689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281689SN/A *
291689SN/A * Authors: Ron Dreslinski
301689SN/A *          Steve Reinhardt
311689SN/A *          Ali Saidi
321689SN/A */
331689SN/A
341689SN/A/**
351689SN/A * @file
361689SN/A * Declaration of the Packet class.
371689SN/A */
381689SN/A
392665Ssaidi@eecs.umich.edu#ifndef __MEM_PACKET_HH__
402665Ssaidi@eecs.umich.edu#define __MEM_PACKET_HH__
412756Sksewell@umich.edu
421689SN/A#include <cassert>
431689SN/A#include <list>
442292SN/A#include <bitset>
452292SN/A
461060SN/A#include "base/cast.hh"
472669Sktlim@umich.edu#include "base/compiler.hh"
484182Sgblack@eecs.umich.edu#include "base/fast_alloc.hh"
491461SN/A#include "base/flags.hh"
507813Ssteve.reinhardt@amd.com#include "base/misc.hh"
516658Snate@binkert.org#include "base/printable.hh"
521060SN/A#include "base/types.hh"
537849SAli.Saidi@ARM.com#include "mem/request.hh"
543348Sbinkertn@umich.edu#include "sim/core.hh"
552669Sktlim@umich.edu
561461SN/Astruct Packet;
571060SN/Atypedef Packet *PacketPtr;
585529Snate@binkert.orgtypedef uint8_t* PacketDataPtr;
595529Snate@binkert.orgtypedef std::list<PacketPtr> PacketList;
601060SN/A
612329SN/Aclass MemCmd
622329SN/A{
632329SN/A    friend class Packet;
642329SN/A
652348SN/A  public:
662329SN/A    /**
671060SN/A     * List of all commands associated with a packet.
681060SN/A     */
692292SN/A    enum Command
701060SN/A    {
711060SN/A        InvalidCmd,
721060SN/A        ReadReq,
731061SN/A        ReadResp,
741060SN/A        ReadRespWithInvalidate,
751061SN/A        WriteReq,
762733Sktlim@umich.edu        WriteResp,
771060SN/A        Writeback,
782292SN/A        SoftPFReq,
791061SN/A        HardPFReq,
801061SN/A        SoftPFResp,
811061SN/A        HardPFResp,
821060SN/A        WriteInvalidateReq,
831060SN/A        WriteInvalidateResp,
842107SN/A        UpgradeReq,
852292SN/A        SCUpgradeReq,           // Special "weak" upgrade for StoreCond
862632Sstever@eecs.umich.edu        UpgradeResp,
872698Sktlim@umich.edu        SCUpgradeFailReq,       // Failed SCUpgradeReq in MSHR (never sent)
882698Sktlim@umich.edu        UpgradeFailResp,        // Valid for SCUpgradeReq only
892698Sktlim@umich.edu        ReadExReq,
902669Sktlim@umich.edu        ReadExResp,
912669Sktlim@umich.edu        LoadLockedReq,
922669Sktlim@umich.edu        StoreCondReq,
932698Sktlim@umich.edu        StoreCondResp,
942669Sktlim@umich.edu        SwapReq,
952669Sktlim@umich.edu        SwapResp,
962669Sktlim@umich.edu        MessageReq,
972698Sktlim@umich.edu        MessageResp,
985494Sstever@gmail.com        // Error responses
995606Snate@binkert.org        // @TODO these should be classified as responses rather than
1002669Sktlim@umich.edu        // requests; coding them as requests initially for backwards
1012669Sktlim@umich.edu        // compatibility
1023647Srdreslin@umich.edu        NetworkNackError,  // nacked at network layer (not by protocol)
1033647Srdreslin@umich.edu        InvalidDestError,  // packet dest field invalid
1044302Sktlim@umich.edu        BadAddressError,   // memory address invalid
1054302Sktlim@umich.edu        // Fake simulator-only commands
1062669Sktlim@umich.edu        PrintReq,       // Print state matching address
1072698Sktlim@umich.edu        NUM_MEM_CMDS
1082669Sktlim@umich.edu    };
1092669Sktlim@umich.edu
1102698Sktlim@umich.edu  private:
1112669Sktlim@umich.edu    /**
1122669Sktlim@umich.edu     * List of command attributes.
1132698Sktlim@umich.edu     */
1142669Sktlim@umich.edu    enum Attribute
1152669Sktlim@umich.edu    {
1162698Sktlim@umich.edu        IsRead,         //!< Data flows from responder to requester
1172669Sktlim@umich.edu        IsWrite,        //!< Data flows from requester to responder
1184475Sstever@eecs.umich.edu        IsUpgrade,
1194475Sstever@eecs.umich.edu        IsPrefetch,     //!< Not a demand access
1202669Sktlim@umich.edu        IsInvalidate,
1212698Sktlim@umich.edu        NeedsExclusive, //!< Requires exclusive copy to complete in-cache
1222698Sktlim@umich.edu        IsRequest,      //!< Issued by requester
1232669Sktlim@umich.edu        IsResponse,     //!< Issue by responder
1242669Sktlim@umich.edu        NeedsResponse,  //!< Requester needs response from target
1252698Sktlim@umich.edu        IsSWPrefetch,
1262669Sktlim@umich.edu        IsHWPrefetch,
1272669Sktlim@umich.edu        IsLlsc,         //!< Alpha/MIPS LL or SC access
1281060SN/A        HasData,        //!< There is an associated payload
1297849SAli.Saidi@ARM.com        IsError,        //!< Error response
1307849SAli.Saidi@ARM.com        IsPrint,        //!< Print state matching address (for debugging)
1317849SAli.Saidi@ARM.com        NUM_COMMAND_ATTRIBUTES
1327849SAli.Saidi@ARM.com    };
1337849SAli.Saidi@ARM.com
1347849SAli.Saidi@ARM.com    /**
1357849SAli.Saidi@ARM.com     * Structure that defines attributes and other data associated
1367849SAli.Saidi@ARM.com     * with a Command.
1377849SAli.Saidi@ARM.com     */
1387849SAli.Saidi@ARM.com    struct CommandInfo
1397849SAli.Saidi@ARM.com    {
1407944SGiacomo.Gabrielli@arm.com        /// Set of attribute flags.
1417944SGiacomo.Gabrielli@arm.com        const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
1427944SGiacomo.Gabrielli@arm.com        /// Corresponding response for requests; InvalidCmd if no
1437944SGiacomo.Gabrielli@arm.com        /// response is applicable.
1447849SAli.Saidi@ARM.com        const Command response;
1457849SAli.Saidi@ARM.com        /// String representation (for printing)
1467849SAli.Saidi@ARM.com        const std::string str;
1477849SAli.Saidi@ARM.com    };
1487849SAli.Saidi@ARM.com
1497849SAli.Saidi@ARM.com    /// Array to map Command enum to associated info.
1507849SAli.Saidi@ARM.com    static const CommandInfo commandInfo[];
1517849SAli.Saidi@ARM.com
1522935Sksewell@umich.edu  private:
1531060SN/A
1542329SN/A    Command cmd;
1552329SN/A
1562292SN/A    bool
1572292SN/A    testCmdAttrib(MemCmd::Attribute attrib) const
1582292SN/A    {
1592292SN/A        return commandInfo[cmd].attributes[attrib] != 0;
1602292SN/A    }
1612292SN/A
1622292SN/A  public:
1632292SN/A
1641060SN/A    bool isRead() const         { return testCmdAttrib(IsRead); }
1651060SN/A    bool isWrite() const        { return testCmdAttrib(IsWrite); }
1661060SN/A    bool isUpgrade() const      { return testCmdAttrib(IsUpgrade); }
1671060SN/A    bool isRequest() const      { return testCmdAttrib(IsRequest); }
1682292SN/A    bool isResponse() const     { return testCmdAttrib(IsResponse); }
1692292SN/A    bool needsExclusive() const { return testCmdAttrib(NeedsExclusive); }
1702292SN/A    bool needsResponse() const  { return testCmdAttrib(NeedsResponse); }
1712307SN/A    bool isInvalidate() const   { return testCmdAttrib(IsInvalidate); }
1727849SAli.Saidi@ARM.com    bool hasData() const        { return testCmdAttrib(HasData); }
1732669Sktlim@umich.edu    bool isReadWrite() const    { return isRead() && isWrite(); }
1742696Sktlim@umich.edu    bool isLLSC() const         { return testCmdAttrib(IsLlsc); }
1752669Sktlim@umich.edu    bool isError() const        { return testCmdAttrib(IsError); }
1761060SN/A    bool isPrint() const        { return testCmdAttrib(IsPrint); }
1771060SN/A
1782292SN/A    const Command
1792292SN/A    responseCommand() const
1802292SN/A    {
1812292SN/A        return commandInfo[cmd].response;
1822292SN/A    }
1832292SN/A
1842292SN/A    /// Return the string to a cmd given by idx.
1852292SN/A    const std::string &toString() const { return commandInfo[cmd].str; }
1861060SN/A    int toInt() const { return (int)cmd; }
1872292SN/A
1882292SN/A    MemCmd(Command _cmd) : cmd(_cmd) { }
1892292SN/A    MemCmd(int _cmd) : cmd((Command)_cmd) { }
1902292SN/A    MemCmd() : cmd(InvalidCmd) { }
1912292SN/A
1922292SN/A    bool operator==(MemCmd c2) const { return (cmd == c2.cmd); }
1932292SN/A    bool operator!=(MemCmd c2) const { return (cmd != c2.cmd); }
1942292SN/A};
1952292SN/A
1962292SN/A/**
1972292SN/A * A Packet is used to encapsulate a transfer between two objects in
1986221Snate@binkert.org * the memory system (e.g., the L1 and L2 cache).  (In contrast, a
1991060SN/A * single Request travels all the way from the requester to the
2001060SN/A * ultimate destination and back, possibly being conveyed by several
2012292SN/A * different Packets along the way.)
2025529Snate@binkert.org */
2031684SN/Aclass Packet : public FastAlloc, public Printable
2042292SN/A{
2052292SN/A  public:
2061684SN/A    typedef uint32_t FlagsType;
2072292SN/A    typedef ::Flags<FlagsType> Flags;
2081062SN/A    typedef short NodeID;
2091062SN/A
2102871Sktlim@umich.edu  private:
2112871Sktlim@umich.edu    static const FlagsType PUBLIC_FLAGS           = 0x00000000;
2122871Sktlim@umich.edu    static const FlagsType PRIVATE_FLAGS          = 0x00007F0F;
2132292SN/A    static const FlagsType COPY_FLAGS             = 0x0000000F;
2141060SN/A
2151060SN/A    static const FlagsType SHARED                 = 0x00000001;
2162292SN/A    // Special control flags
2176221Snate@binkert.org    /// Special timing-mode atomic snoop for multi-level coherence.
2182292SN/A    static const FlagsType EXPRESS_SNOOP          = 0x00000002;
2192292SN/A    /// Does supplier have exclusive copy?
2201060SN/A    /// Useful for multi-level coherence.
2211060SN/A    static const FlagsType SUPPLY_EXCLUSIVE       = 0x00000004;
2222292SN/A    // Snoop response flags
2232292SN/A    static const FlagsType MEM_INHIBIT            = 0x00000008;
2242292SN/A    /// Are the 'addr' and 'size' fields valid?
2254302Sktlim@umich.edu    static const FlagsType VALID_ADDR             = 0x00000100;
2264302Sktlim@umich.edu    static const FlagsType VALID_SIZE             = 0x00000200;
2274302Sktlim@umich.edu    /// Is the 'src' field valid?
2282292SN/A    static const FlagsType VALID_SRC              = 0x00000400;
2292669Sktlim@umich.edu    static const FlagsType VALID_DST              = 0x00000800;
2302292SN/A    /// Is the data pointer set to a value that shouldn't be freed
2312843Sktlim@umich.edu    /// when the packet is destroyed?
2322863Sktlim@umich.edu    static const FlagsType STATIC_DATA            = 0x00001000;
2332843Sktlim@umich.edu    /// The data pointer points to a value that should be freed when
2342843Sktlim@umich.edu    /// the packet is destroyed.
2352843Sktlim@umich.edu    static const FlagsType DYNAMIC_DATA           = 0x00002000;
2362843Sktlim@umich.edu    /// the data pointer points to an array (thus delete []) needs to
2372843Sktlim@umich.edu    /// be called on it rather than simply delete.
2382307SN/A    static const FlagsType ARRAY_DATA             = 0x00004000;
2392307SN/A
2402348SN/A    Flags flags;
2412307SN/A
2422307SN/A  public:
2432348SN/A    typedef MemCmd::Command Command;
2442307SN/A
2452307SN/A    /// The command field of the packet.
2462348SN/A    MemCmd cmd;
2472292SN/A
2481060SN/A    /// A pointer to the original request.
2491061SN/A    RequestPtr req;
2502329SN/A
2512329SN/A  private:
2522292SN/A   /**
2532292SN/A    * A pointer to the data being transfered.  It can be differnt
2542292SN/A    * sizes at each level of the heirarchy so it belongs in the
2552329SN/A    * packet, not request. This may or may not be populated when a
2562329SN/A    * responder recieves the packet. If not populated it memory should
2572292SN/A    * be allocated.
2582292SN/A    */
2592292SN/A    PacketDataPtr data;
2601061SN/A
2611061SN/A    /// The address of the request.  This address could be virtual or
2621061SN/A    /// physical, depending on the system configuration.
2631763SN/A    Addr addr;
2641061SN/A
2651061SN/A    /// The size of the request or transfer.
2662935Sksewell@umich.edu    unsigned size;
2671061SN/A
2681061SN/A    /**
2697720Sgblack@eecs.umich.edu     * Device address (e.g., bus ID) of the source of the
2701062SN/A     * transaction. The source is not responsible for setting this
2711062SN/A     * field; it is set implicitly by the interconnect when the packet
2721062SN/A     * is first sent.
2731062SN/A     */
2741062SN/A    NodeID src;
2757764Sgblack@eecs.umich.edu
2762292SN/A    /**
2772292SN/A     * Device address (e.g., bus ID) of the destination of the
2782292SN/A     * transaction. The special value Broadcast indicates that the
2797764Sgblack@eecs.umich.edu     * packet should be routed based on its address. This field is
2801062SN/A     * initialized in the constructor and is thus always valid (unlike
2811062SN/A     * addr, size, and src).
2827849SAli.Saidi@ARM.com     */
2837849SAli.Saidi@ARM.com    NodeID dest;
2841062SN/A
2857847Sminkyu.jeong@arm.com    /**
2867847Sminkyu.jeong@arm.com     * The original value of the command field.  Only valid when the
2877847Sminkyu.jeong@arm.com     * current command field is an error condition; in that case, the
2887847Sminkyu.jeong@arm.com     * previous contents of the command field are copied here.  This
2897847Sminkyu.jeong@arm.com     * field is *not* set on non-error responses.
2907847Sminkyu.jeong@arm.com     */
2917847Sminkyu.jeong@arm.com    MemCmd origCmd;
2927847Sminkyu.jeong@arm.com
2937847Sminkyu.jeong@arm.com  public:
2942292SN/A    /// Used to calculate latencies for each packet.
2957720Sgblack@eecs.umich.edu    Tick time;
2961684SN/A
2972292SN/A    /// The time at which the packet will be fully transmitted
2982292SN/A    Tick finishTime;
2992292SN/A
3007720Sgblack@eecs.umich.edu    /// The time at which the first chunk of the packet will be transmitted
3016221Snate@binkert.org    Tick firstWordTime;
3022292SN/A
3032292SN/A    /// The special destination address indicating that the packet
3046221Snate@binkert.org    /// should be routed based on its address.
3052292SN/A    static const NodeID Broadcast = -1;
3062292SN/A
3072292SN/A    /**
3082292SN/A     * A virtual base opaque structure used to hold state associated
3091684SN/A     * with the packet but specific to the sending device (e.g., an
3101684SN/A     * MSHR).  A pointer to this state is returned in the packet's
3112292SN/A     * response so that the sender can quickly look up the state
3122292SN/A     * needed to process it.  A specific subclass would be derived
3132292SN/A     * from this to carry state specific to a particular sending
3142292SN/A     * device.
3157720Sgblack@eecs.umich.edu     */
3166221Snate@binkert.org    struct SenderState
3171684SN/A    {
3182292SN/A        virtual ~SenderState() {}
3192292SN/A    };
3202292SN/A
3211684SN/A    /**
3221684SN/A     * Object used to maintain state of a PrintReq.  The senderState
3232292SN/A     * field of a PrintReq should always be of this type.
3242292SN/A     */
3252292SN/A    class PrintReqState : public SenderState, public FastAlloc
3266221Snate@binkert.org    {
3271684SN/A      private:
3282292SN/A        /**
3292292SN/A         * An entry in the label stack.
3302292SN/A         */
3312292SN/A        struct LabelStackEntry
3322292SN/A        {
3332292SN/A            const std::string label;
3342292SN/A            std::string *prefix;
3352292SN/A            bool labelPrinted;
3361062SN/A            LabelStackEntry(const std::string &_label, std::string *_prefix);
3371062SN/A        };
3381062SN/A
3391062SN/A        typedef std::list<LabelStackEntry> LabelStack;
3401061SN/A        LabelStack labelStack;
3411060SN/A
3427764Sgblack@eecs.umich.edu        std::string *curPrefixPtr;
3437764Sgblack@eecs.umich.edu
3447764Sgblack@eecs.umich.edu      public:
3457764Sgblack@eecs.umich.edu        std::ostream &os;
3462698Sktlim@umich.edu        const int verbosity;
3472696Sktlim@umich.edu
3482696Sktlim@umich.edu        PrintReqState(std::ostream &os, int verbosity = 0);
3492292SN/A        ~PrintReqState();
3506221Snate@binkert.org
3512292SN/A        /**
3522292SN/A         * Returns the current line prefix.
3536221Snate@binkert.org         */
3542292SN/A        const std::string &curPrefix() { return *curPrefixPtr; }
3552292SN/A
3566221Snate@binkert.org        /**
3572292SN/A         * Push a label onto the label stack, and prepend the given
3582292SN/A         * prefix string onto the current prefix.  Labels will only be
3596221Snate@binkert.org         * printed if an object within the label's scope is printed.
3602292SN/A         */
3616221Snate@binkert.org        void pushLabel(const std::string &lbl,
3626221Snate@binkert.org                       const std::string &prefix = "  ");
3636221Snate@binkert.org
3642292SN/A        /**
3652292SN/A         * Pop a label off the label stack.
3662733Sktlim@umich.edu         */
3672733Sktlim@umich.edu        void popLabel();
3681060SN/A
3691060SN/A        /**
3701060SN/A         * Print all of the pending unprinted labels on the
3711060SN/A         * stack. Called by printObj(), so normally not called by
3721060SN/A         * users unless bypassing printObj().
3731060SN/A         */
3741060SN/A        void printLabels();
3751060SN/A
3761060SN/A        /**
3771060SN/A         * Print a Printable object to os, because it matched the
3781060SN/A         * address on a PrintReq.
3791060SN/A         */
3801060SN/A        void printObj(Printable *obj);
3811060SN/A    };
3821060SN/A
3831060SN/A    /**
3841060SN/A     * This packet's sender state.  Devices should use dynamic_cast<>
3851060SN/A     * to cast to the state appropriate to the sender.  The intent of
3861060SN/A     * this variable is to allow a device to attach extra information
3871060SN/A     * to a request.  A response packet must return the sender state
3881060SN/A     * that was attached to the original request (even if a new packet
3891060SN/A     * is created).
3901060SN/A     */
3911060SN/A    SenderState *senderState;
3922669Sktlim@umich.edu
3931060SN/A    /// Return the string name of the cmd field (for debugging and
3941061SN/A    /// tracing).
3951061SN/A    const std::string &cmdString() const { return cmd.toString(); }
3961061SN/A
3974182Sgblack@eecs.umich.edu    /// Return the index of this command.
3984182Sgblack@eecs.umich.edu    inline int cmdToIndex() const { return cmd.toInt(); }
3994182Sgblack@eecs.umich.edu
4007720Sgblack@eecs.umich.edu    bool isRead() const         { return cmd.isRead(); }
4012292SN/A    bool isWrite() const        { return cmd.isWrite(); }
4027764Sgblack@eecs.umich.edu    bool isUpgrade()  const     { return cmd.isUpgrade(); }
4037764Sgblack@eecs.umich.edu    bool isRequest() const      { return cmd.isRequest(); }
4047764Sgblack@eecs.umich.edu    bool isResponse() const     { return cmd.isResponse(); }
4057764Sgblack@eecs.umich.edu    bool needsExclusive() const { return cmd.needsExclusive(); }
4062678Sktlim@umich.edu    bool needsResponse() const  { return cmd.needsResponse(); }
4072678Sktlim@umich.edu    bool isInvalidate() const   { return cmd.isInvalidate(); }
4082292SN/A    bool hasData() const        { return cmd.hasData(); }
4092292SN/A    bool isReadWrite() const    { return cmd.isReadWrite(); }
4102292SN/A    bool isLLSC() const         { return cmd.isLLSC(); }
4112292SN/A    bool isError() const        { return cmd.isError(); }
4122292SN/A    bool isPrint() const        { return cmd.isPrint(); }
4132292SN/A
4142292SN/A    // Snoop flags
4152292SN/A    void assertMemInhibit()     { flags.set(MEM_INHIBIT); }
4162292SN/A    bool memInhibitAsserted()   { return flags.isSet(MEM_INHIBIT); }
4172292SN/A    void assertShared()         { flags.set(SHARED); }
4182292SN/A    bool sharedAsserted()       { return flags.isSet(SHARED); }
4192292SN/A
4202292SN/A    // Special control flags
4212292SN/A    void setExpressSnoop()      { flags.set(EXPRESS_SNOOP); }
4222292SN/A    bool isExpressSnoop()       { return flags.isSet(EXPRESS_SNOOP); }
4232292SN/A    void setSupplyExclusive()   { flags.set(SUPPLY_EXCLUSIVE); }
4242292SN/A    bool isSupplyExclusive()    { return flags.isSet(SUPPLY_EXCLUSIVE); }
4252292SN/A
4262292SN/A    // Network error conditions... encapsulate them as methods since
4271060SN/A    // their encoding keeps changing (from result field to command
4281060SN/A    // field, etc.)
4291060SN/A    void
4301060SN/A    setNacked()
4311060SN/A    {
4321060SN/A        assert(isResponse());
4331060SN/A        cmd = MemCmd::NetworkNackError;
4341060SN/A    }
4351060SN/A
4361060SN/A    void
4371060SN/A    setBadAddress()
4381060SN/A    {
4391060SN/A        assert(isResponse());
4401060SN/A        cmd = MemCmd::BadAddressError;
4411060SN/A    }
4421060SN/A
4432696Sktlim@umich.edu    bool wasNacked() const     { return cmd == MemCmd::NetworkNackError; }
4442696Sktlim@umich.edu    bool hadBadAddress() const { return cmd == MemCmd::BadAddressError; }
4452696Sktlim@umich.edu    void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
4462696Sktlim@umich.edu
4472696Sktlim@umich.edu    bool isSrcValid() { return flags.isSet(VALID_SRC); }
4482696Sktlim@umich.edu    /// Accessor function to get the source index of the packet.
4492696Sktlim@umich.edu    NodeID getSrc() const    { assert(flags.isSet(VALID_SRC)); return src; }
4506221Snate@binkert.org    /// Accessor function to set the source index of the packet.
4512696Sktlim@umich.edu    void setSrc(NodeID _src) { src = _src; flags.set(VALID_SRC); }
4521060SN/A    /// Reset source field, e.g. to retransmit packet on different bus.
4531062SN/A    void clearSrc() { flags.clear(VALID_SRC); }
4541060SN/A
4551060SN/A    bool isDestValid() { return flags.isSet(VALID_DST); }
4561062SN/A    /// Accessor function for the destination index of the packet.
4571060SN/A    NodeID getDest() const     { assert(flags.isSet(VALID_DST)); return dest; }
4581062SN/A    /// Accessor function to set the destination index of the packet.
4592292SN/A    void setDest(NodeID _dest) { dest = _dest; flags.set(VALID_DST); }
4601060SN/A
4612893Sktlim@umich.edu    Addr getAddr() const { assert(flags.isSet(VALID_ADDR)); return addr; }
4622893Sktlim@umich.edu    unsigned getSize() const  { assert(flags.isSet(VALID_SIZE)); return size; }
4632893Sktlim@umich.edu    Addr getOffset(int blkSize) const { return getAddr() & (Addr)(blkSize - 1); }
4642906Sktlim@umich.edu
4652906Sktlim@umich.edu    /**
4662906Sktlim@umich.edu     * Constructor.  Note that a Request object must be constructed
4671060SN/A     * first, but the Requests's physical address and size fields need
4681060SN/A     * not be valid. The command and destination addresses must be
4691060SN/A     * supplied.
4701060SN/A     */
4712292SN/A    Packet(Request *_req, MemCmd _cmd, NodeID _dest)
4721062SN/A        :  flags(VALID_DST), cmd(_cmd), req(_req), data(NULL),
4732292SN/A           dest(_dest), time(curTick), senderState(NULL)
4746221Snate@binkert.org    {
4752292SN/A        if (req->hasPaddr()) {
4762292SN/A            addr = req->getPaddr();
4776221Snate@binkert.org            flags.set(VALID_ADDR);
4782292SN/A        }
4792292SN/A        if (req->hasSize()) {
4806221Snate@binkert.org            size = req->getSize();
4812292SN/A            flags.set(VALID_SIZE);
4822292SN/A        }
4836221Snate@binkert.org    }
4842292SN/A
4852348SN/A    /**
4862348SN/A     * Alternate constructor if you are trying to create a packet with
4872348SN/A     * a request that is for a whole block, not the address from the
4882292SN/A     * req.  this allows for overriding the size/addr of the req.
4892292SN/A     */
4902843Sktlim@umich.edu    Packet(Request *_req, MemCmd _cmd, NodeID _dest, int _blkSize)
4912843Sktlim@umich.edu        :  flags(VALID_DST), cmd(_cmd), req(_req), data(NULL),
4922843Sktlim@umich.edu           dest(_dest), time(curTick), senderState(NULL)
4932348SN/A    {
4942307SN/A        if (req->hasPaddr()) {
4952307SN/A            addr = req->getPaddr() & ~(_blkSize - 1);
4962292SN/A            flags.set(VALID_ADDR);
4972292SN/A        }
4985999Snate@binkert.org        size = _blkSize;
4992292SN/A        flags.set(VALID_SIZE);
5005999Snate@binkert.org    }
5012727Sktlim@umich.edu
5025999Snate@binkert.org    /**
5032292SN/A     * Alternate constructor for copying a packet.  Copy all fields
5045999Snate@binkert.org     * *except* if the original packet's data was dynamic, don't copy
5052292SN/A     * that, as we can't guarantee that the new packet's lifetime is
5065999Snate@binkert.org     * less than that of the original packet.  In this case the new
5072292SN/A     * packet should allocate its own data.
5085999Snate@binkert.org     */
5097849SAli.Saidi@ARM.com    Packet(Packet *pkt, bool clearFlags = false)
5107849SAli.Saidi@ARM.com        :  cmd(pkt->cmd), req(pkt->req),
5112292SN/A           data(pkt->flags.isSet(STATIC_DATA) ? pkt->data : NULL),
5122292SN/A           addr(pkt->addr), size(pkt->size), src(pkt->src), dest(pkt->dest),
5132292SN/A           time(curTick), senderState(pkt->senderState)
5145999Snate@binkert.org    {
5152348SN/A        if (!clearFlags)
5165999Snate@binkert.org            flags.set(pkt->flags & COPY_FLAGS);
5172348SN/A
5185999Snate@binkert.org        flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE|VALID_SRC|VALID_DST));
5192292SN/A        flags.set(pkt->flags & STATIC_DATA);
5205999Snate@binkert.org    }
5212348SN/A
5222348SN/A    /**
5232348SN/A     * clean up packet variables
5245999Snate@binkert.org     */
5252292SN/A    ~Packet()
5265999Snate@binkert.org    {
5272348SN/A        // If this is a request packet for which there's no response,
5282292SN/A        // delete the request object here, since the requester will
5292348SN/A        // never get the chance.
5302292SN/A        if (req && isRequest() && !needsResponse())
5312348SN/A            delete req;
5322292SN/A        deleteData();
5331060SN/A    }
5341060SN/A
5352292SN/A    /**
536     * Reinitialize packet address and size from the associated
537     * Request object, and reset other fields that may have been
538     * modified by a previous transaction.  Typically called when a
539     * statically allocated Request/Packet pair is reused for multiple
540     * transactions.
541     */
542    void
543    reinitFromRequest()
544    {
545        assert(req->hasPaddr());
546        flags = 0;
547        addr = req->getPaddr();
548        size = req->getSize();
549        time = req->time();
550
551        flags.set(VALID_ADDR|VALID_SIZE);
552        deleteData();
553    }
554
555    /**
556     * Take a request packet and modify it in place to be suitable for
557     * returning as a response to that request.  The source and
558     * destination fields are *not* modified, as is appropriate for
559     * atomic accesses.
560     */
561    void
562    makeResponse()
563    {
564        assert(needsResponse());
565        assert(isRequest());
566        origCmd = cmd;
567        cmd = cmd.responseCommand();
568
569        // responses are never express, even if the snoop that
570        // triggered them was
571        flags.clear(EXPRESS_SNOOP);
572
573        dest = src;
574        flags.set(VALID_DST, flags.isSet(VALID_SRC));
575        flags.clear(VALID_SRC);
576    }
577
578    void
579    makeAtomicResponse()
580    {
581        makeResponse();
582    }
583
584    void
585    makeTimingResponse()
586    {
587        makeResponse();
588    }
589
590    /**
591     * Take a request packet that has been returned as NACKED and
592     * modify it so that it can be sent out again. Only packets that
593     * need a response can be NACKED, so verify that that is true.
594     */
595    void
596    reinitNacked()
597    {
598        assert(wasNacked());
599        cmd = origCmd;
600        assert(needsResponse());
601        setDest(Broadcast);
602    }
603
604    void
605    setSize(unsigned size)
606    {
607        assert(!flags.isSet(VALID_SIZE));
608
609        this->size = size;
610        flags.set(VALID_SIZE);
611    }
612
613
614    /**
615     * Set the data pointer to the following value that should not be
616     * freed.
617     */
618    template <typename T>
619    void
620    dataStatic(T *p)
621    {
622        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
623        data = (PacketDataPtr)p;
624        flags.set(STATIC_DATA);
625    }
626
627    /**
628     * Set the data pointer to a value that should have delete []
629     * called on it.
630     */
631    template <typename T>
632    void
633    dataDynamicArray(T *p)
634    {
635        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
636        data = (PacketDataPtr)p;
637        flags.set(DYNAMIC_DATA|ARRAY_DATA);
638    }
639
640    /**
641     * set the data pointer to a value that should have delete called
642     * on it.
643     */
644    template <typename T>
645    void
646    dataDynamic(T *p)
647    {
648        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
649        data = (PacketDataPtr)p;
650        flags.set(DYNAMIC_DATA);
651    }
652
653    /**
654     * get a pointer to the data ptr.
655     */
656    template <typename T>
657    T*
658    getPtr()
659    {
660        assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
661        return (T*)data;
662    }
663
664    /**
665     * return the value of what is pointed to in the packet.
666     */
667    template <typename T>
668    T get();
669
670    /**
671     * set the value in the data pointer to v.
672     */
673    template <typename T>
674    void set(T v);
675
676    /**
677     * Copy data into the packet from the provided pointer.
678     */
679    void
680    setData(uint8_t *p)
681    {
682        std::memcpy(getPtr<uint8_t>(), p, getSize());
683    }
684
685    /**
686     * Copy data into the packet from the provided block pointer,
687     * which is aligned to the given block size.
688     */
689    void
690    setDataFromBlock(uint8_t *blk_data, int blkSize)
691    {
692        setData(blk_data + getOffset(blkSize));
693    }
694
695    /**
696     * Copy data from the packet to the provided block pointer, which
697     * is aligned to the given block size.
698     */
699    void
700    writeData(uint8_t *p)
701    {
702        std::memcpy(p, getPtr<uint8_t>(), getSize());
703    }
704
705    /**
706     * Copy data from the packet to the memory at the provided pointer.
707     */
708    void
709    writeDataToBlock(uint8_t *blk_data, int blkSize)
710    {
711        writeData(blk_data + getOffset(blkSize));
712    }
713
714    /**
715     * delete the data pointed to in the data pointer. Ok to call to
716     * matter how data was allocted.
717     */
718    void
719    deleteData()
720    {
721        if (flags.isSet(ARRAY_DATA))
722            delete [] data;
723        else if (flags.isSet(DYNAMIC_DATA))
724            delete data;
725
726        flags.clear(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA);
727        data = NULL;
728    }
729
730    /** If there isn't data in the packet, allocate some. */
731    void
732    allocate()
733    {
734        if (data) {
735            assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
736            return;
737        }
738
739        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
740        flags.set(DYNAMIC_DATA|ARRAY_DATA);
741        data = new uint8_t[getSize()];
742    }
743
744
745    /**
746     * Check a functional request against a memory value represented
747     * by a base/size pair and an associated data array.  If the
748     * functional request is a read, it may be satisfied by the memory
749     * value.  If the functional request is a write, it may update the
750     * memory value.
751     */
752    bool checkFunctional(Printable *obj, Addr base, int size, uint8_t *data);
753
754    /**
755     * Check a functional request against a memory value stored in
756     * another packet (i.e. an in-transit request or response).
757     */
758    bool
759    checkFunctional(PacketPtr other)
760    {
761        uint8_t *data = other->hasData() ? other->getPtr<uint8_t>() : NULL;
762        return checkFunctional(other, other->getAddr(), other->getSize(),
763                               data);
764    }
765
766    /**
767     * Push label for PrintReq (safe to call unconditionally).
768     */
769    void
770    pushLabel(const std::string &lbl)
771    {
772        if (isPrint())
773            safe_cast<PrintReqState*>(senderState)->pushLabel(lbl);
774    }
775
776    /**
777     * Pop label for PrintReq (safe to call unconditionally).
778     */
779    void
780    popLabel()
781    {
782        if (isPrint())
783            safe_cast<PrintReqState*>(senderState)->popLabel();
784    }
785
786    void print(std::ostream &o, int verbosity = 0,
787               const std::string &prefix = "") const;
788};
789
790#endif //__MEM_PACKET_HH
791