packet.hh revision 6076
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"
512392SN/A#include "mem/request.hh"
522980Sgblack@eecs.umich.edu#include "sim/host.hh"
534167Sbinkertn@umich.edu#include "sim/core.hh"
542394SN/A
553940Ssaidi@eecs.umich.edu
562394SN/Astruct Packet;
573349Sbinkertn@umich.edutypedef Packet *PacketPtr;
582394SN/Atypedef uint8_t* PacketDataPtr;
592812Srdreslin@umich.edutypedef std::list<PacketPtr> PacketList;
602812Srdreslin@umich.edu
614022Sstever@eecs.umich.educlass MemCmd
624022Sstever@eecs.umich.edu{
635735Snate@binkert.org    friend class Packet;
645735Snate@binkert.org
654022Sstever@eecs.umich.edu  public:
665735Snate@binkert.org    /**
675735Snate@binkert.org     * List of all commands associated with a packet.
685735Snate@binkert.org     */
694022Sstever@eecs.umich.edu    enum Command
704022Sstever@eecs.umich.edu    {
714022Sstever@eecs.umich.edu        InvalidCmd,
724022Sstever@eecs.umich.edu        ReadReq,
734473Sstever@eecs.umich.edu        ReadResp,
745319Sstever@gmail.com        ReadRespWithInvalidate,
754022Sstever@eecs.umich.edu        WriteReq,
764022Sstever@eecs.umich.edu        WriteResp,
774022Sstever@eecs.umich.edu        Writeback,
784022Sstever@eecs.umich.edu        SoftPFReq,
794022Sstever@eecs.umich.edu        HardPFReq,
804022Sstever@eecs.umich.edu        SoftPFResp,
814022Sstever@eecs.umich.edu        HardPFResp,
824022Sstever@eecs.umich.edu        WriteInvalidateReq,
834022Sstever@eecs.umich.edu        WriteInvalidateResp,
844022Sstever@eecs.umich.edu        UpgradeReq,
854628Sstever@eecs.umich.edu        UpgradeResp,
864022Sstever@eecs.umich.edu        ReadExReq,
874022Sstever@eecs.umich.edu        ReadExResp,
884626Sstever@eecs.umich.edu        LoadLockedReq,
894626Sstever@eecs.umich.edu        StoreCondReq,
904626Sstever@eecs.umich.edu        StoreCondResp,
914040Ssaidi@eecs.umich.edu        SwapReq,
924040Ssaidi@eecs.umich.edu        SwapResp,
935650Sgblack@eecs.umich.edu        MessageReq,
945650Sgblack@eecs.umich.edu        MessageResp,
954870Sstever@eecs.umich.edu        // Error responses
964870Sstever@eecs.umich.edu        // @TODO these should be classified as responses rather than
974870Sstever@eecs.umich.edu        // requests; coding them as requests initially for backwards
984870Sstever@eecs.umich.edu        // compatibility
994870Sstever@eecs.umich.edu        NetworkNackError,  // nacked at network layer (not by protocol)
1004870Sstever@eecs.umich.edu        InvalidDestError,  // packet dest field invalid
1014870Sstever@eecs.umich.edu        BadAddressError,   // memory address invalid
1025314Sstever@gmail.com        // Fake simulator-only commands
1035314Sstever@gmail.com        PrintReq,       // Print state matching address
1044022Sstever@eecs.umich.edu        NUM_MEM_CMDS
1054022Sstever@eecs.umich.edu    };
1064022Sstever@eecs.umich.edu
1074022Sstever@eecs.umich.edu  private:
1085735Snate@binkert.org    /**
1095735Snate@binkert.org     * List of command attributes.
1105735Snate@binkert.org     */
1114022Sstever@eecs.umich.edu    enum Attribute
1124022Sstever@eecs.umich.edu    {
1134626Sstever@eecs.umich.edu        IsRead,         //!< Data flows from responder to requester
1144626Sstever@eecs.umich.edu        IsWrite,        //!< Data flows from requester to responder
1154626Sstever@eecs.umich.edu        IsPrefetch,     //!< Not a demand access
1164022Sstever@eecs.umich.edu        IsInvalidate,
1174626Sstever@eecs.umich.edu        NeedsExclusive, //!< Requires exclusive copy to complete in-cache
1184626Sstever@eecs.umich.edu        IsRequest,      //!< Issued by requester
1194626Sstever@eecs.umich.edu        IsResponse,     //!< Issue by responder
1204626Sstever@eecs.umich.edu        NeedsResponse,  //!< Requester needs response from target
1214022Sstever@eecs.umich.edu        IsSWPrefetch,
1224022Sstever@eecs.umich.edu        IsHWPrefetch,
1236076Sgblack@eecs.umich.edu        IsLlsc,         //!< Alpha/MIPS LL or SC access
1244626Sstever@eecs.umich.edu        HasData,        //!< There is an associated payload
1254870Sstever@eecs.umich.edu        IsError,        //!< Error response
1265314Sstever@gmail.com        IsPrint,        //!< Print state matching address (for debugging)
1274022Sstever@eecs.umich.edu        NUM_COMMAND_ATTRIBUTES
1284022Sstever@eecs.umich.edu    };
1294022Sstever@eecs.umich.edu
1305735Snate@binkert.org    /**
1315735Snate@binkert.org     * Structure that defines attributes and other data associated
1325735Snate@binkert.org     * with a Command.
1335735Snate@binkert.org     */
1345735Snate@binkert.org    struct CommandInfo
1355735Snate@binkert.org    {
1365735Snate@binkert.org        /// Set of attribute flags.
1374022Sstever@eecs.umich.edu        const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
1385735Snate@binkert.org        /// Corresponding response for requests; InvalidCmd if no
1395735Snate@binkert.org        /// response is applicable.
1404022Sstever@eecs.umich.edu        const Command response;
1415735Snate@binkert.org        /// String representation (for printing)
1424022Sstever@eecs.umich.edu        const std::string str;
1434022Sstever@eecs.umich.edu    };
1444022Sstever@eecs.umich.edu
1455735Snate@binkert.org    /// Array to map Command enum to associated info.
1464022Sstever@eecs.umich.edu    static const CommandInfo commandInfo[];
1474022Sstever@eecs.umich.edu
1484022Sstever@eecs.umich.edu  private:
1494022Sstever@eecs.umich.edu
1504022Sstever@eecs.umich.edu    Command cmd;
1514022Sstever@eecs.umich.edu
1525735Snate@binkert.org    bool
1535735Snate@binkert.org    testCmdAttrib(MemCmd::Attribute attrib) const
1545735Snate@binkert.org    {
1554022Sstever@eecs.umich.edu        return commandInfo[cmd].attributes[attrib] != 0;
1564022Sstever@eecs.umich.edu    }
1574022Sstever@eecs.umich.edu
1584022Sstever@eecs.umich.edu  public:
1594022Sstever@eecs.umich.edu
1604022Sstever@eecs.umich.edu    bool isRead() const         { return testCmdAttrib(IsRead); }
1614022Sstever@eecs.umich.edu    bool isWrite()  const       { return testCmdAttrib(IsWrite); }
1624022Sstever@eecs.umich.edu    bool isRequest() const      { return testCmdAttrib(IsRequest); }
1634022Sstever@eecs.umich.edu    bool isResponse() const     { return testCmdAttrib(IsResponse); }
1644870Sstever@eecs.umich.edu    bool needsExclusive() const { return testCmdAttrib(NeedsExclusive); }
1654022Sstever@eecs.umich.edu    bool needsResponse() const  { return testCmdAttrib(NeedsResponse); }
1664022Sstever@eecs.umich.edu    bool isInvalidate() const   { return testCmdAttrib(IsInvalidate); }
1674022Sstever@eecs.umich.edu    bool hasData() const        { return testCmdAttrib(HasData); }
1684626Sstever@eecs.umich.edu    bool isReadWrite() const    { return isRead() && isWrite(); }
1696076Sgblack@eecs.umich.edu    bool isLlsc() const         { return testCmdAttrib(IsLlsc); }
1704870Sstever@eecs.umich.edu    bool isError() const        { return testCmdAttrib(IsError); }
1715314Sstever@gmail.com    bool isPrint() const        { return testCmdAttrib(IsPrint); }
1724022Sstever@eecs.umich.edu
1735735Snate@binkert.org    const Command
1745735Snate@binkert.org    responseCommand() const
1755735Snate@binkert.org    {
1764022Sstever@eecs.umich.edu        return commandInfo[cmd].response;
1774022Sstever@eecs.umich.edu    }
1784022Sstever@eecs.umich.edu
1795735Snate@binkert.org    /// Return the string to a cmd given by idx.
1805735Snate@binkert.org    const std::string &toString() const { return commandInfo[cmd].str; }
1814022Sstever@eecs.umich.edu    int toInt() const { return (int)cmd; }
1824022Sstever@eecs.umich.edu
1835735Snate@binkert.org    MemCmd(Command _cmd) : cmd(_cmd) { }
1845735Snate@binkert.org    MemCmd(int _cmd) : cmd((Command)_cmd) { }
1855735Snate@binkert.org    MemCmd() : cmd(InvalidCmd) { }
1864022Sstever@eecs.umich.edu
1875735Snate@binkert.org    bool operator==(MemCmd c2) const { return (cmd == c2.cmd); }
1885735Snate@binkert.org    bool operator!=(MemCmd c2) const { return (cmd != c2.cmd); }
1894022Sstever@eecs.umich.edu};
1904022Sstever@eecs.umich.edu
1912381SN/A/**
1922662Sstever@eecs.umich.edu * A Packet is used to encapsulate a transfer between two objects in
1932662Sstever@eecs.umich.edu * the memory system (e.g., the L1 and L2 cache).  (In contrast, a
1942662Sstever@eecs.umich.edu * single Request travels all the way from the requester to the
1952662Sstever@eecs.umich.edu * ultimate destination and back, possibly being conveyed by several
1962662Sstever@eecs.umich.edu * different Packets along the way.)
1972381SN/A */
1985314Sstever@gmail.comclass Packet : public FastAlloc, public Printable
1992381SN/A{
2002813Srdreslin@umich.edu  public:
2015735Snate@binkert.org    typedef uint32_t FlagsType;
2025735Snate@binkert.org    typedef ::Flags<FlagsType> Flags;
2035735Snate@binkert.org    typedef short NodeID;
2044022Sstever@eecs.umich.edu
2055735Snate@binkert.org  private:
2065735Snate@binkert.org    static const FlagsType PUBLIC_FLAGS           = 0x00000000;
2075735Snate@binkert.org    static const FlagsType PRIVATE_FLAGS          = 0x00007F0F;
2085735Snate@binkert.org    static const FlagsType COPY_FLAGS             = 0x0000000F;
2095735Snate@binkert.org
2105735Snate@binkert.org    static const FlagsType SHARED                 = 0x00000001;
2115735Snate@binkert.org    // Special control flags
2125735Snate@binkert.org    /// Special timing-mode atomic snoop for multi-level coherence.
2135735Snate@binkert.org    static const FlagsType EXPRESS_SNOOP          = 0x00000002;
2145735Snate@binkert.org    /// Does supplier have exclusive copy?
2155735Snate@binkert.org    /// Useful for multi-level coherence.
2165735Snate@binkert.org    static const FlagsType SUPPLY_EXCLUSIVE       = 0x00000004;
2175735Snate@binkert.org    // Snoop response flags
2185735Snate@binkert.org    static const FlagsType MEM_INHIBIT            = 0x00000008;
2195735Snate@binkert.org    /// Are the 'addr' and 'size' fields valid?
2205735Snate@binkert.org    static const FlagsType VALID_ADDR             = 0x00000100;
2215735Snate@binkert.org    static const FlagsType VALID_SIZE             = 0x00000200;
2225735Snate@binkert.org    /// Is the 'src' field valid?
2235735Snate@binkert.org    static const FlagsType VALID_SRC              = 0x00000400;
2245735Snate@binkert.org    static const FlagsType VALID_DST              = 0x00000800;
2255735Snate@binkert.org    /// Is the data pointer set to a value that shouldn't be freed
2265735Snate@binkert.org    /// when the packet is destroyed?
2275735Snate@binkert.org    static const FlagsType STATIC_DATA            = 0x00001000;
2285735Snate@binkert.org    /// The data pointer points to a value that should be freed when
2295735Snate@binkert.org    /// the packet is destroyed.
2305735Snate@binkert.org    static const FlagsType DYNAMIC_DATA           = 0x00002000;
2315735Snate@binkert.org    /// the data pointer points to an array (thus delete []) needs to
2325735Snate@binkert.org    /// be called on it rather than simply delete.
2335735Snate@binkert.org    static const FlagsType ARRAY_DATA             = 0x00004000;
2345735Snate@binkert.org
2355735Snate@binkert.org    Flags flags;
2365735Snate@binkert.org
2375735Snate@binkert.org  public:
2384022Sstever@eecs.umich.edu    typedef MemCmd::Command Command;
2394022Sstever@eecs.umich.edu
2405735Snate@binkert.org    /// The command field of the packet.
2414870Sstever@eecs.umich.edu    MemCmd cmd;
2424870Sstever@eecs.umich.edu
2435735Snate@binkert.org    /// A pointer to the original request.
2444870Sstever@eecs.umich.edu    RequestPtr req;
2454870Sstever@eecs.umich.edu
2462566SN/A  private:
2475735Snate@binkert.org   /**
2485735Snate@binkert.org    * A pointer to the data being transfered.  It can be differnt
2495735Snate@binkert.org    * sizes at each level of the heirarchy so it belongs in the
2505735Snate@binkert.org    * packet, not request. This may or may not be populated when a
2515735Snate@binkert.org    * responder recieves the packet. If not populated it memory should
2525735Snate@binkert.org    * be allocated.
2532566SN/A    */
2542566SN/A    PacketDataPtr data;
2552566SN/A
2565735Snate@binkert.org    /// The address of the request.  This address could be virtual or
2575735Snate@binkert.org    /// physical, depending on the system configuration.
2582381SN/A    Addr addr;
2592381SN/A
2605735Snate@binkert.org    /// The size of the request or transfer.
2612381SN/A    int size;
2622381SN/A
2635735Snate@binkert.org    /**
2645735Snate@binkert.org     * Device address (e.g., bus ID) of the source of the
2655735Snate@binkert.org     * transaction. The source is not responsible for setting this
2665735Snate@binkert.org     * field; it is set implicitly by the interconnect when the packet
2675735Snate@binkert.org     * is first sent.
2685735Snate@binkert.org     */
2695735Snate@binkert.org    NodeID src;
2702381SN/A
2715735Snate@binkert.org    /**
2725735Snate@binkert.org     * Device address (e.g., bus ID) of the destination of the
2735735Snate@binkert.org     * transaction. The special value Broadcast indicates that the
2745735Snate@binkert.org     * packet should be routed based on its address. This field is
2755735Snate@binkert.org     * initialized in the constructor and is thus always valid (unlike
2765735Snate@binkert.org     * addr, size, and src).
2775735Snate@binkert.org     */
2785735Snate@binkert.org    NodeID dest;
2792641Sstever@eecs.umich.edu
2805735Snate@binkert.org    /**
2815735Snate@binkert.org     * The original value of the command field.  Only valid when the
2824870Sstever@eecs.umich.edu     * current command field is an error condition; in that case, the
2834870Sstever@eecs.umich.edu     * previous contents of the command field are copied here.  This
2844870Sstever@eecs.umich.edu     * field is *not* set on non-error responses.
2854870Sstever@eecs.umich.edu     */
2864870Sstever@eecs.umich.edu    MemCmd origCmd;
2874870Sstever@eecs.umich.edu
2882641Sstever@eecs.umich.edu  public:
2895735Snate@binkert.org    /// Used to calculate latencies for each packet.
2902811Srdreslin@umich.edu    Tick time;
2912811Srdreslin@umich.edu
2925735Snate@binkert.org    /// The time at which the packet will be fully transmitted
2933218Sgblack@eecs.umich.edu    Tick finishTime;
2943218Sgblack@eecs.umich.edu
2955735Snate@binkert.org    /// The time at which the first chunk of the packet will be transmitted
2963218Sgblack@eecs.umich.edu    Tick firstWordTime;
2973218Sgblack@eecs.umich.edu
2985735Snate@binkert.org    /// The special destination address indicating that the packet
2995735Snate@binkert.org    /// should be routed based on its address.
3005735Snate@binkert.org    static const NodeID Broadcast = -1;
3012623SN/A
3025735Snate@binkert.org    /**
3035735Snate@binkert.org     * A virtual base opaque structure used to hold state associated
3045735Snate@binkert.org     * with the packet but specific to the sending device (e.g., an
3055735Snate@binkert.org     * MSHR).  A pointer to this state is returned in the packet's
3065735Snate@binkert.org     * response so that the sender can quickly look up the state
3075735Snate@binkert.org     * needed to process it.  A specific subclass would be derived
3085735Snate@binkert.org     * from this to carry state specific to a particular sending
3095735Snate@binkert.org     * device.
3105735Snate@binkert.org     */
3115735Snate@binkert.org    struct SenderState
3125735Snate@binkert.org    {
3132641Sstever@eecs.umich.edu        virtual ~SenderState() {}
3142641Sstever@eecs.umich.edu    };
3152641Sstever@eecs.umich.edu
3165315Sstever@gmail.com    /**
3175315Sstever@gmail.com     * Object used to maintain state of a PrintReq.  The senderState
3185315Sstever@gmail.com     * field of a PrintReq should always be of this type.
3195315Sstever@gmail.com     */
3205735Snate@binkert.org    class PrintReqState : public SenderState, public FastAlloc
3215735Snate@binkert.org    {
3225735Snate@binkert.org      private:
3235735Snate@binkert.org        /**
3245735Snate@binkert.org         * An entry in the label stack.
3255735Snate@binkert.org         */
3265735Snate@binkert.org        struct LabelStackEntry
3275735Snate@binkert.org        {
3285314Sstever@gmail.com            const std::string label;
3295314Sstever@gmail.com            std::string *prefix;
3305314Sstever@gmail.com            bool labelPrinted;
3315735Snate@binkert.org            LabelStackEntry(const std::string &_label, std::string *_prefix);
3325314Sstever@gmail.com        };
3335314Sstever@gmail.com
3345314Sstever@gmail.com        typedef std::list<LabelStackEntry> LabelStack;
3355314Sstever@gmail.com        LabelStack labelStack;
3365314Sstever@gmail.com
3375314Sstever@gmail.com        std::string *curPrefixPtr;
3385314Sstever@gmail.com
3395314Sstever@gmail.com      public:
3405314Sstever@gmail.com        std::ostream &os;
3415314Sstever@gmail.com        const int verbosity;
3425314Sstever@gmail.com
3435314Sstever@gmail.com        PrintReqState(std::ostream &os, int verbosity = 0);
3445314Sstever@gmail.com        ~PrintReqState();
3455314Sstever@gmail.com
3465735Snate@binkert.org        /**
3475735Snate@binkert.org         * Returns the current line prefix.
3485735Snate@binkert.org         */
3495314Sstever@gmail.com        const std::string &curPrefix() { return *curPrefixPtr; }
3505315Sstever@gmail.com
3515735Snate@binkert.org        /**
3525735Snate@binkert.org         * Push a label onto the label stack, and prepend the given
3535315Sstever@gmail.com         * prefix string onto the current prefix.  Labels will only be
3545735Snate@binkert.org         * printed if an object within the label's scope is printed.
3555735Snate@binkert.org         */
3565314Sstever@gmail.com        void pushLabel(const std::string &lbl,
3575314Sstever@gmail.com                       const std::string &prefix = "  ");
3585735Snate@binkert.org
3595735Snate@binkert.org        /**
3605735Snate@binkert.org         * Pop a label off the label stack.
3615735Snate@binkert.org         */
3625314Sstever@gmail.com        void popLabel();
3635735Snate@binkert.org
3645735Snate@binkert.org        /**
3655735Snate@binkert.org         * Print all of the pending unprinted labels on the
3665315Sstever@gmail.com         * stack. Called by printObj(), so normally not called by
3675735Snate@binkert.org         * users unless bypassing printObj().
3685735Snate@binkert.org         */
3695314Sstever@gmail.com        void printLabels();
3705735Snate@binkert.org
3715735Snate@binkert.org        /**
3725735Snate@binkert.org         * Print a Printable object to os, because it matched the
3735735Snate@binkert.org         * address on a PrintReq.
3745735Snate@binkert.org         */
3755314Sstever@gmail.com        void printObj(Printable *obj);
3765314Sstever@gmail.com    };
3775314Sstever@gmail.com
3785735Snate@binkert.org    /**
3795735Snate@binkert.org     * This packet's sender state.  Devices should use dynamic_cast<>
3805735Snate@binkert.org     * to cast to the state appropriate to the sender.  The intent of
3815735Snate@binkert.org     * this variable is to allow a device to attach extra information
3825735Snate@binkert.org     * to a request.  A response packet must return the sender state
3835735Snate@binkert.org     * that was attached to the original request (even if a new packet
3845735Snate@binkert.org     * is created).
3855735Snate@binkert.org     */
3862662Sstever@eecs.umich.edu    SenderState *senderState;
3872641Sstever@eecs.umich.edu
3885735Snate@binkert.org    /// Return the string name of the cmd field (for debugging and
3895735Snate@binkert.org    /// tracing).
3904022Sstever@eecs.umich.edu    const std::string &cmdString() const { return cmd.toString(); }
3912811Srdreslin@umich.edu
3925735Snate@binkert.org    /// Return the index of this command.
3934022Sstever@eecs.umich.edu    inline int cmdToIndex() const { return cmd.toInt(); }
3942811Srdreslin@umich.edu
3954022Sstever@eecs.umich.edu    bool isRead() const         { return cmd.isRead(); }
3964022Sstever@eecs.umich.edu    bool isWrite()  const       { return cmd.isWrite(); }
3974022Sstever@eecs.umich.edu    bool isRequest() const      { return cmd.isRequest(); }
3984022Sstever@eecs.umich.edu    bool isResponse() const     { return cmd.isResponse(); }
3994870Sstever@eecs.umich.edu    bool needsExclusive() const { return cmd.needsExclusive(); }
4004022Sstever@eecs.umich.edu    bool needsResponse() const  { return cmd.needsResponse(); }
4014022Sstever@eecs.umich.edu    bool isInvalidate() const   { return cmd.isInvalidate(); }
4024022Sstever@eecs.umich.edu    bool hasData() const        { return cmd.hasData(); }
4034040Ssaidi@eecs.umich.edu    bool isReadWrite() const    { return cmd.isReadWrite(); }
4046076Sgblack@eecs.umich.edu    bool isLlsc() const         { return cmd.isLlsc(); }
4054870Sstever@eecs.umich.edu    bool isError() const        { return cmd.isError(); }
4065314Sstever@gmail.com    bool isPrint() const        { return cmd.isPrint(); }
4072812Srdreslin@umich.edu
4084870Sstever@eecs.umich.edu    // Snoop flags
4095735Snate@binkert.org    void assertMemInhibit()     { flags.set(MEM_INHIBIT); }
4105764Snate@binkert.org    bool memInhibitAsserted()   { return flags.isSet(MEM_INHIBIT); }
4115735Snate@binkert.org    void assertShared()         { flags.set(SHARED); }
4125764Snate@binkert.org    bool sharedAsserted()       { return flags.isSet(SHARED); }
4134870Sstever@eecs.umich.edu
4144895Sstever@eecs.umich.edu    // Special control flags
4155735Snate@binkert.org    void setExpressSnoop()      { flags.set(EXPRESS_SNOOP); }
4165764Snate@binkert.org    bool isExpressSnoop()       { return flags.isSet(EXPRESS_SNOOP); }
4175735Snate@binkert.org    void setSupplyExclusive()   { flags.set(SUPPLY_EXCLUSIVE); }
4185764Snate@binkert.org    bool isSupplyExclusive()    { return flags.isSet(SUPPLY_EXCLUSIVE); }
4194895Sstever@eecs.umich.edu
4204870Sstever@eecs.umich.edu    // Network error conditions... encapsulate them as methods since
4214870Sstever@eecs.umich.edu    // their encoding keeps changing (from result field to command
4224870Sstever@eecs.umich.edu    // field, etc.)
4235735Snate@binkert.org    void
4245735Snate@binkert.org    setNacked()
4255735Snate@binkert.org    {
4265735Snate@binkert.org        assert(isResponse());
4275735Snate@binkert.org        cmd = MemCmd::NetworkNackError;
4285735Snate@binkert.org    }
4295735Snate@binkert.org
4305735Snate@binkert.org    void
4315735Snate@binkert.org    setBadAddress()
4325735Snate@binkert.org    {
4335735Snate@binkert.org        assert(isResponse());
4345735Snate@binkert.org        cmd = MemCmd::BadAddressError;
4355735Snate@binkert.org    }
4365735Snate@binkert.org
4375735Snate@binkert.org    bool wasNacked() const     { return cmd == MemCmd::NetworkNackError; }
4385735Snate@binkert.org    bool hadBadAddress() const { return cmd == MemCmd::BadAddressError; }
4394986Ssaidi@eecs.umich.edu    void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
4402814Srdreslin@umich.edu
4415735Snate@binkert.org    /// Accessor function to get the source index of the packet.
4425764Snate@binkert.org    NodeID getSrc() const    { assert(flags.isSet(VALID_SRC)); return src; }
4435735Snate@binkert.org    /// Accessor function to set the source index of the packet.
4445735Snate@binkert.org    void setSrc(NodeID _src) { src = _src; flags.set(VALID_SRC); }
4455735Snate@binkert.org    /// Reset source field, e.g. to retransmit packet on different bus.
4465735Snate@binkert.org    void clearSrc() { flags.clear(VALID_SRC); }
4472641Sstever@eecs.umich.edu
4485735Snate@binkert.org    /// Accessor function for the destination index of the packet.
4495764Snate@binkert.org    NodeID getDest() const     { assert(flags.isSet(VALID_DST)); return dest; }
4505735Snate@binkert.org    /// Accessor function to set the destination index of the packet.
4515735Snate@binkert.org    void setDest(NodeID _dest) { dest = _dest; flags.set(VALID_DST); }
4522381SN/A
4535764Snate@binkert.org    Addr getAddr() const { assert(flags.isSet(VALID_ADDR)); return addr; }
4545764Snate@binkert.org    int getSize() const  { assert(flags.isSet(VALID_SIZE)); return size; }
4555735Snate@binkert.org    Addr getOffset(int blkSize) const { return getAddr() & (Addr)(blkSize - 1); }
4562549SN/A
4575735Snate@binkert.org    /**
4585735Snate@binkert.org     * Constructor.  Note that a Request object must be constructed
4595735Snate@binkert.org     * first, but the Requests's physical address and size fields need
4605735Snate@binkert.org     * not be valid. The command and destination addresses must be
4615735Snate@binkert.org     * supplied.
4625735Snate@binkert.org     */
4635735Snate@binkert.org    Packet(Request *_req, MemCmd _cmd, NodeID _dest)
4645745Snate@binkert.org        :  flags(VALID_DST), cmd(_cmd), req(_req), data(NULL),
4655745Snate@binkert.org           addr(_req->paddr), size(_req->size), dest(_dest), time(curTick),
4665745Snate@binkert.org           senderState(NULL)
4672641Sstever@eecs.umich.edu    {
4685764Snate@binkert.org        if (req->flags.isSet(Request::VALID_PADDR))
4695745Snate@binkert.org            flags.set(VALID_ADDR|VALID_SIZE);
4702813Srdreslin@umich.edu    }
4712813Srdreslin@umich.edu
4725735Snate@binkert.org    /**
4735735Snate@binkert.org     * Alternate constructor if you are trying to create a packet with
4745735Snate@binkert.org     * a request that is for a whole block, not the address from the
4755735Snate@binkert.org     * req.  this allows for overriding the size/addr of the req.
4765735Snate@binkert.org     */
4775735Snate@binkert.org    Packet(Request *_req, MemCmd _cmd, NodeID _dest, int _blkSize)
4785745Snate@binkert.org        :  flags(VALID_DST), cmd(_cmd), req(_req), data(NULL),
4794626Sstever@eecs.umich.edu           addr(_req->paddr & ~(_blkSize - 1)), size(_blkSize), dest(_dest),
4805735Snate@binkert.org           time(curTick), senderState(NULL)
4812813Srdreslin@umich.edu    {
4825764Snate@binkert.org        if (req->flags.isSet(Request::VALID_PADDR))
4835745Snate@binkert.org            flags.set(VALID_ADDR|VALID_SIZE);
4844626Sstever@eecs.umich.edu    }
4854626Sstever@eecs.umich.edu
4865735Snate@binkert.org    /**
4875735Snate@binkert.org     * Alternate constructor for copying a packet.  Copy all fields
4884887Sstever@eecs.umich.edu     * *except* if the original packet's data was dynamic, don't copy
4894887Sstever@eecs.umich.edu     * that, as we can't guarantee that the new packet's lifetime is
4904887Sstever@eecs.umich.edu     * less than that of the original packet.  In this case the new
4915735Snate@binkert.org     * packet should allocate its own data.
4925735Snate@binkert.org     */
4935735Snate@binkert.org    Packet(Packet *pkt, bool clearFlags = false)
4945735Snate@binkert.org        :  cmd(pkt->cmd), req(pkt->req),
4955764Snate@binkert.org           data(pkt->flags.isSet(STATIC_DATA) ? pkt->data : NULL),
4965735Snate@binkert.org           addr(pkt->addr), size(pkt->size), src(pkt->src), dest(pkt->dest),
4975735Snate@binkert.org           time(curTick), senderState(pkt->senderState)
4984626Sstever@eecs.umich.edu    {
4995735Snate@binkert.org        if (!clearFlags)
5005735Snate@binkert.org            flags.set(pkt->flags & COPY_FLAGS);
5015735Snate@binkert.org
5025735Snate@binkert.org        flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE|VALID_SRC|VALID_DST));
5035735Snate@binkert.org        flags.set(pkt->flags & STATIC_DATA);
5042641Sstever@eecs.umich.edu    }
5052549SN/A
5065735Snate@binkert.org    /**
5075735Snate@binkert.org     * clean up packet variables
5085735Snate@binkert.org     */
5092566SN/A    ~Packet()
5105387Sstever@gmail.com    {
5115387Sstever@gmail.com        // If this is a request packet for which there's no response,
5125387Sstever@gmail.com        // delete the request object here, since the requester will
5135387Sstever@gmail.com        // never get the chance.
5145387Sstever@gmail.com        if (req && isRequest() && !needsResponse())
5155387Sstever@gmail.com            delete req;
5165735Snate@binkert.org        deleteData();
5175387Sstever@gmail.com    }
5182566SN/A
5195735Snate@binkert.org    /**
5205735Snate@binkert.org     * Reinitialize packet address and size from the associated
5215735Snate@binkert.org     * Request object, and reset other fields that may have been
5225735Snate@binkert.org     * modified by a previous transaction.  Typically called when a
5235735Snate@binkert.org     * statically allocated Request/Packet pair is reused for multiple
5245735Snate@binkert.org     * transactions.
5255735Snate@binkert.org     */
5265735Snate@binkert.org    void
5275735Snate@binkert.org    reinitFromRequest()
5285735Snate@binkert.org    {
5295764Snate@binkert.org        assert(req->flags.isSet(Request::VALID_PADDR));
5304870Sstever@eecs.umich.edu        flags = 0;
5312663Sstever@eecs.umich.edu        addr = req->paddr;
5322663Sstever@eecs.umich.edu        size = req->size;
5333018Srdreslin@umich.edu        time = req->time;
5345735Snate@binkert.org
5355735Snate@binkert.org        flags.set(VALID_ADDR|VALID_SIZE);
5365735Snate@binkert.org        deleteData();
5372662Sstever@eecs.umich.edu    }
5382566SN/A
5394626Sstever@eecs.umich.edu    /**
5404626Sstever@eecs.umich.edu     * Take a request packet and modify it in place to be suitable for
5414626Sstever@eecs.umich.edu     * returning as a response to that request.  The source and
5424626Sstever@eecs.umich.edu     * destination fields are *not* modified, as is appropriate for
5434626Sstever@eecs.umich.edu     * atomic accesses.
5444626Sstever@eecs.umich.edu     */
5455735Snate@binkert.org    void
5465735Snate@binkert.org    makeResponse()
5474626Sstever@eecs.umich.edu    {
5482662Sstever@eecs.umich.edu        assert(needsResponse());
5492855Srdreslin@umich.edu        assert(isRequest());
5504986Ssaidi@eecs.umich.edu        origCmd = cmd;
5514022Sstever@eecs.umich.edu        cmd = cmd.responseCommand();
5525745Snate@binkert.org
5535745Snate@binkert.org        dest = src;
5545764Snate@binkert.org        flags.set(VALID_DST, flags.isSet(VALID_SRC));
5555745Snate@binkert.org        flags.clear(VALID_SRC);
5562641Sstever@eecs.umich.edu    }
5572641Sstever@eecs.umich.edu
5585735Snate@binkert.org    void
5595735Snate@binkert.org    makeAtomicResponse()
5604870Sstever@eecs.umich.edu    {
5614870Sstever@eecs.umich.edu        makeResponse();
5624870Sstever@eecs.umich.edu    }
5634870Sstever@eecs.umich.edu
5645735Snate@binkert.org    void
5655735Snate@binkert.org    makeTimingResponse()
5663348Sbinkertn@umich.edu    {
5674870Sstever@eecs.umich.edu        makeResponse();
5683135Srdreslin@umich.edu    }
5693135Srdreslin@umich.edu
5703348Sbinkertn@umich.edu    /**
5713348Sbinkertn@umich.edu     * Take a request packet that has been returned as NACKED and
5723348Sbinkertn@umich.edu     * modify it so that it can be sent out again. Only packets that
5733348Sbinkertn@umich.edu     * need a response can be NACKED, so verify that that is true.
5743348Sbinkertn@umich.edu     */
5753348Sbinkertn@umich.edu    void
5763348Sbinkertn@umich.edu    reinitNacked()
5773348Sbinkertn@umich.edu    {
5784870Sstever@eecs.umich.edu        assert(wasNacked());
5794870Sstever@eecs.umich.edu        cmd = origCmd;
5804870Sstever@eecs.umich.edu        assert(needsResponse());
5814870Sstever@eecs.umich.edu        setDest(Broadcast);
5822685Ssaidi@eecs.umich.edu    }
5832685Ssaidi@eecs.umich.edu
5842685Ssaidi@eecs.umich.edu
5853348Sbinkertn@umich.edu    /**
5863348Sbinkertn@umich.edu     * Set the data pointer to the following value that should not be
5873348Sbinkertn@umich.edu     * freed.
5882566SN/A     */
5892566SN/A    template <typename T>
5903348Sbinkertn@umich.edu    void
5913348Sbinkertn@umich.edu    dataStatic(T *p)
5923348Sbinkertn@umich.edu    {
5935764Snate@binkert.org        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
5943348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
5955735Snate@binkert.org        flags.set(STATIC_DATA);
5963348Sbinkertn@umich.edu    }
5972566SN/A
5983348Sbinkertn@umich.edu    /**
5993348Sbinkertn@umich.edu     * Set the data pointer to a value that should have delete []
6003348Sbinkertn@umich.edu     * called on it.
6013348Sbinkertn@umich.edu     */
6022566SN/A    template <typename T>
6033348Sbinkertn@umich.edu    void
6043348Sbinkertn@umich.edu    dataDynamicArray(T *p)
6053348Sbinkertn@umich.edu    {
6065764Snate@binkert.org        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
6073348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
6085735Snate@binkert.org        flags.set(DYNAMIC_DATA|ARRAY_DATA);
6093348Sbinkertn@umich.edu    }
6103348Sbinkertn@umich.edu
6113348Sbinkertn@umich.edu    /**
6123348Sbinkertn@umich.edu     * set the data pointer to a value that should have delete called
6133348Sbinkertn@umich.edu     * on it.
6143348Sbinkertn@umich.edu     */
6153348Sbinkertn@umich.edu    template <typename T>
6163348Sbinkertn@umich.edu    void
6173348Sbinkertn@umich.edu    dataDynamic(T *p)
6183348Sbinkertn@umich.edu    {
6195764Snate@binkert.org        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
6203348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
6215735Snate@binkert.org        flags.set(DYNAMIC_DATA);
6223348Sbinkertn@umich.edu    }
6233348Sbinkertn@umich.edu
6245735Snate@binkert.org    /**
6255735Snate@binkert.org     * get a pointer to the data ptr.
6265735Snate@binkert.org     */
6273348Sbinkertn@umich.edu    template <typename T>
6283348Sbinkertn@umich.edu    T*
6293348Sbinkertn@umich.edu    getPtr()
6303348Sbinkertn@umich.edu    {
6315764Snate@binkert.org        assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
6323348Sbinkertn@umich.edu        return (T*)data;
6333348Sbinkertn@umich.edu    }
6342566SN/A
6355735Snate@binkert.org    /**
6365735Snate@binkert.org     * return the value of what is pointed to in the packet.
6375735Snate@binkert.org     */
6382566SN/A    template <typename T>
6392592SN/A    T get();
6402566SN/A
6415735Snate@binkert.org    /**
6425735Snate@binkert.org     * set the value in the data pointer to v.
6435735Snate@binkert.org     */
6442566SN/A    template <typename T>
6452592SN/A    void set(T v);
6462566SN/A
6473348Sbinkertn@umich.edu    /**
6484626Sstever@eecs.umich.edu     * Copy data into the packet from the provided pointer.
6494626Sstever@eecs.umich.edu     */
6505735Snate@binkert.org    void
6515735Snate@binkert.org    setData(uint8_t *p)
6524626Sstever@eecs.umich.edu    {
6534626Sstever@eecs.umich.edu        std::memcpy(getPtr<uint8_t>(), p, getSize());
6544626Sstever@eecs.umich.edu    }
6554626Sstever@eecs.umich.edu
6564626Sstever@eecs.umich.edu    /**
6574626Sstever@eecs.umich.edu     * Copy data into the packet from the provided block pointer,
6584626Sstever@eecs.umich.edu     * which is aligned to the given block size.
6594626Sstever@eecs.umich.edu     */
6605735Snate@binkert.org    void
6615735Snate@binkert.org    setDataFromBlock(uint8_t *blk_data, int blkSize)
6624626Sstever@eecs.umich.edu    {
6634626Sstever@eecs.umich.edu        setData(blk_data + getOffset(blkSize));
6644626Sstever@eecs.umich.edu    }
6654626Sstever@eecs.umich.edu
6664626Sstever@eecs.umich.edu    /**
6674626Sstever@eecs.umich.edu     * Copy data from the packet to the provided block pointer, which
6684626Sstever@eecs.umich.edu     * is aligned to the given block size.
6694626Sstever@eecs.umich.edu     */
6705735Snate@binkert.org    void
6715735Snate@binkert.org    writeData(uint8_t *p)
6724626Sstever@eecs.umich.edu    {
6734626Sstever@eecs.umich.edu        std::memcpy(p, getPtr<uint8_t>(), getSize());
6744626Sstever@eecs.umich.edu    }
6754626Sstever@eecs.umich.edu
6764626Sstever@eecs.umich.edu    /**
6774626Sstever@eecs.umich.edu     * Copy data from the packet to the memory at the provided pointer.
6784626Sstever@eecs.umich.edu     */
6795735Snate@binkert.org    void
6805735Snate@binkert.org    writeDataToBlock(uint8_t *blk_data, int blkSize)
6814626Sstever@eecs.umich.edu    {
6824626Sstever@eecs.umich.edu        writeData(blk_data + getOffset(blkSize));
6834626Sstever@eecs.umich.edu    }
6844626Sstever@eecs.umich.edu
6854626Sstever@eecs.umich.edu    /**
6863348Sbinkertn@umich.edu     * delete the data pointed to in the data pointer. Ok to call to
6873348Sbinkertn@umich.edu     * matter how data was allocted.
6883348Sbinkertn@umich.edu     */
6895735Snate@binkert.org    void
6905735Snate@binkert.org    deleteData()
6915735Snate@binkert.org    {
6925764Snate@binkert.org        if (flags.isSet(ARRAY_DATA))
6935735Snate@binkert.org            delete [] data;
6945764Snate@binkert.org        else if (flags.isSet(DYNAMIC_DATA))
6955735Snate@binkert.org            delete data;
6965735Snate@binkert.org
6975735Snate@binkert.org        flags.clear(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA);
6985735Snate@binkert.org        data = NULL;
6995735Snate@binkert.org    }
7002566SN/A
7012566SN/A    /** If there isn't data in the packet, allocate some. */
7025735Snate@binkert.org    void
7035735Snate@binkert.org    allocate()
7045735Snate@binkert.org    {
7055735Snate@binkert.org        if (data) {
7065764Snate@binkert.org            assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
7075745Snate@binkert.org            return;
7085735Snate@binkert.org        }
7095745Snate@binkert.org
7105764Snate@binkert.org        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
7115745Snate@binkert.org        flags.set(DYNAMIC_DATA|ARRAY_DATA);
7125745Snate@binkert.org        data = new uint8_t[getSize()];
7135735Snate@binkert.org    }
7145735Snate@binkert.org
7152568SN/A
7164626Sstever@eecs.umich.edu    /**
7174626Sstever@eecs.umich.edu     * Check a functional request against a memory value represented
7184626Sstever@eecs.umich.edu     * by a base/size pair and an associated data array.  If the
7194626Sstever@eecs.umich.edu     * functional request is a read, it may be satisfied by the memory
7204626Sstever@eecs.umich.edu     * value.  If the functional request is a write, it may update the
7214626Sstever@eecs.umich.edu     * memory value.
7224626Sstever@eecs.umich.edu     */
7235314Sstever@gmail.com    bool checkFunctional(Printable *obj, Addr base, int size, uint8_t *data);
7244626Sstever@eecs.umich.edu
7254626Sstever@eecs.umich.edu    /**
7264626Sstever@eecs.umich.edu     * Check a functional request against a memory value stored in
7275315Sstever@gmail.com     * another packet (i.e. an in-transit request or response).
7284626Sstever@eecs.umich.edu     */
7295735Snate@binkert.org    bool
7305735Snate@binkert.org    checkFunctional(PacketPtr other)
7315735Snate@binkert.org    {
7325735Snate@binkert.org        uint8_t *data = other->hasData() ? other->getPtr<uint8_t>() : NULL;
7335735Snate@binkert.org        return checkFunctional(other, other->getAddr(), other->getSize(),
7345735Snate@binkert.org                               data);
7354626Sstever@eecs.umich.edu    }
7365314Sstever@gmail.com
7375315Sstever@gmail.com    /**
7385315Sstever@gmail.com     * Push label for PrintReq (safe to call unconditionally).
7395315Sstever@gmail.com     */
7405735Snate@binkert.org    void
7415735Snate@binkert.org    pushLabel(const std::string &lbl)
7425735Snate@binkert.org    {
7435735Snate@binkert.org        if (isPrint())
7445735Snate@binkert.org            safe_cast<PrintReqState*>(senderState)->pushLabel(lbl);
7455314Sstever@gmail.com    }
7465314Sstever@gmail.com
7475315Sstever@gmail.com    /**
7485315Sstever@gmail.com     * Pop label for PrintReq (safe to call unconditionally).
7495315Sstever@gmail.com     */
7505735Snate@binkert.org    void
7515735Snate@binkert.org    popLabel()
7525735Snate@binkert.org    {
7535735Snate@binkert.org        if (isPrint())
7545735Snate@binkert.org            safe_cast<PrintReqState*>(senderState)->popLabel();
7555314Sstever@gmail.com    }
7565314Sstever@gmail.com
7575314Sstever@gmail.com    void print(std::ostream &o, int verbosity = 0,
7585314Sstever@gmail.com               const std::string &prefix = "") const;
7592381SN/A};
7602381SN/A
7612381SN/A#endif //__MEM_PACKET_HH
762