packet.hh revision 5319
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
454024Sbinkertn@umich.edu#include "base/compiler.hh"
464610Ssaidi@eecs.umich.edu#include "base/fast_alloc.hh"
473940Ssaidi@eecs.umich.edu#include "base/misc.hh"
485314Sstever@gmail.com#include "base/printable.hh"
492392SN/A#include "mem/request.hh"
502980Sgblack@eecs.umich.edu#include "sim/host.hh"
514167Sbinkertn@umich.edu#include "sim/core.hh"
522394SN/A
533940Ssaidi@eecs.umich.edu
542394SN/Astruct Packet;
553349Sbinkertn@umich.edutypedef Packet *PacketPtr;
562394SN/Atypedef uint8_t* PacketDataPtr;
572812Srdreslin@umich.edutypedef std::list<PacketPtr> PacketList;
582812Srdreslin@umich.edu
594022Sstever@eecs.umich.educlass MemCmd
604022Sstever@eecs.umich.edu{
614022Sstever@eecs.umich.edu  public:
624022Sstever@eecs.umich.edu
634022Sstever@eecs.umich.edu    /** List of all commands associated with a packet. */
644022Sstever@eecs.umich.edu    enum Command
654022Sstever@eecs.umich.edu    {
664022Sstever@eecs.umich.edu        InvalidCmd,
674022Sstever@eecs.umich.edu        ReadReq,
684473Sstever@eecs.umich.edu        ReadResp,
695319Sstever@gmail.com        ReadRespWithInvalidate,
704022Sstever@eecs.umich.edu        WriteReq,
714022Sstever@eecs.umich.edu        WriteResp,
724022Sstever@eecs.umich.edu        Writeback,
734022Sstever@eecs.umich.edu        SoftPFReq,
744022Sstever@eecs.umich.edu        HardPFReq,
754022Sstever@eecs.umich.edu        SoftPFResp,
764022Sstever@eecs.umich.edu        HardPFResp,
774022Sstever@eecs.umich.edu        WriteInvalidateReq,
784022Sstever@eecs.umich.edu        WriteInvalidateResp,
794022Sstever@eecs.umich.edu        UpgradeReq,
804628Sstever@eecs.umich.edu        UpgradeResp,
814022Sstever@eecs.umich.edu        ReadExReq,
824022Sstever@eecs.umich.edu        ReadExResp,
834626Sstever@eecs.umich.edu        LoadLockedReq,
844626Sstever@eecs.umich.edu        LoadLockedResp,
854626Sstever@eecs.umich.edu        StoreCondReq,
864626Sstever@eecs.umich.edu        StoreCondResp,
874040Ssaidi@eecs.umich.edu        SwapReq,
884040Ssaidi@eecs.umich.edu        SwapResp,
894870Sstever@eecs.umich.edu        // Error responses
904870Sstever@eecs.umich.edu        // @TODO these should be classified as responses rather than
914870Sstever@eecs.umich.edu        // requests; coding them as requests initially for backwards
924870Sstever@eecs.umich.edu        // compatibility
934870Sstever@eecs.umich.edu        NetworkNackError,  // nacked at network layer (not by protocol)
944870Sstever@eecs.umich.edu        InvalidDestError,  // packet dest field invalid
954870Sstever@eecs.umich.edu        BadAddressError,   // memory address invalid
965314Sstever@gmail.com        // Fake simulator-only commands
975314Sstever@gmail.com        PrintReq,       // Print state matching address
984022Sstever@eecs.umich.edu        NUM_MEM_CMDS
994022Sstever@eecs.umich.edu    };
1004022Sstever@eecs.umich.edu
1014022Sstever@eecs.umich.edu  private:
1024022Sstever@eecs.umich.edu    /** List of command attributes. */
1034022Sstever@eecs.umich.edu    enum Attribute
1044022Sstever@eecs.umich.edu    {
1054626Sstever@eecs.umich.edu        IsRead,         //!< Data flows from responder to requester
1064626Sstever@eecs.umich.edu        IsWrite,        //!< Data flows from requester to responder
1074626Sstever@eecs.umich.edu        IsPrefetch,     //!< Not a demand access
1084022Sstever@eecs.umich.edu        IsInvalidate,
1094626Sstever@eecs.umich.edu        NeedsExclusive, //!< Requires exclusive copy to complete in-cache
1104626Sstever@eecs.umich.edu        IsRequest,      //!< Issued by requester
1114626Sstever@eecs.umich.edu        IsResponse,     //!< Issue by responder
1124626Sstever@eecs.umich.edu        NeedsResponse,  //!< Requester needs response from target
1134022Sstever@eecs.umich.edu        IsSWPrefetch,
1144022Sstever@eecs.umich.edu        IsHWPrefetch,
1154626Sstever@eecs.umich.edu        IsLocked,       //!< Alpha/MIPS LL or SC access
1164626Sstever@eecs.umich.edu        HasData,        //!< There is an associated payload
1174870Sstever@eecs.umich.edu        IsError,        //!< Error response
1185314Sstever@gmail.com        IsPrint,        //!< Print state matching address (for debugging)
1194022Sstever@eecs.umich.edu        NUM_COMMAND_ATTRIBUTES
1204022Sstever@eecs.umich.edu    };
1214022Sstever@eecs.umich.edu
1224022Sstever@eecs.umich.edu    /** Structure that defines attributes and other data associated
1234022Sstever@eecs.umich.edu     * with a Command. */
1244022Sstever@eecs.umich.edu    struct CommandInfo {
1254022Sstever@eecs.umich.edu        /** Set of attribute flags. */
1264022Sstever@eecs.umich.edu        const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
1274022Sstever@eecs.umich.edu        /** Corresponding response for requests; InvalidCmd if no
1284022Sstever@eecs.umich.edu         * response is applicable. */
1294022Sstever@eecs.umich.edu        const Command response;
1304022Sstever@eecs.umich.edu        /** String representation (for printing) */
1314022Sstever@eecs.umich.edu        const std::string str;
1324022Sstever@eecs.umich.edu    };
1334022Sstever@eecs.umich.edu
1344022Sstever@eecs.umich.edu    /** Array to map Command enum to associated info. */
1354022Sstever@eecs.umich.edu    static const CommandInfo commandInfo[];
1364022Sstever@eecs.umich.edu
1374022Sstever@eecs.umich.edu  private:
1384022Sstever@eecs.umich.edu
1394022Sstever@eecs.umich.edu    Command cmd;
1404022Sstever@eecs.umich.edu
1414022Sstever@eecs.umich.edu    bool testCmdAttrib(MemCmd::Attribute attrib) const {
1424022Sstever@eecs.umich.edu        return commandInfo[cmd].attributes[attrib] != 0;
1434022Sstever@eecs.umich.edu    }
1444022Sstever@eecs.umich.edu
1454022Sstever@eecs.umich.edu  public:
1464022Sstever@eecs.umich.edu
1474022Sstever@eecs.umich.edu    bool isRead() const         { return testCmdAttrib(IsRead); }
1484022Sstever@eecs.umich.edu    bool isWrite()  const       { return testCmdAttrib(IsWrite); }
1494022Sstever@eecs.umich.edu    bool isRequest() const      { return testCmdAttrib(IsRequest); }
1504022Sstever@eecs.umich.edu    bool isResponse() const     { return testCmdAttrib(IsResponse); }
1514870Sstever@eecs.umich.edu    bool needsExclusive() const { return testCmdAttrib(NeedsExclusive); }
1524022Sstever@eecs.umich.edu    bool needsResponse() const  { return testCmdAttrib(NeedsResponse); }
1534022Sstever@eecs.umich.edu    bool isInvalidate() const   { return testCmdAttrib(IsInvalidate); }
1544022Sstever@eecs.umich.edu    bool hasData() const        { return testCmdAttrib(HasData); }
1554626Sstever@eecs.umich.edu    bool isReadWrite() const    { return isRead() && isWrite(); }
1564626Sstever@eecs.umich.edu    bool isLocked() const       { return testCmdAttrib(IsLocked); }
1574870Sstever@eecs.umich.edu    bool isError() const        { return testCmdAttrib(IsError); }
1585314Sstever@gmail.com    bool isPrint() const        { return testCmdAttrib(IsPrint); }
1594022Sstever@eecs.umich.edu
1604022Sstever@eecs.umich.edu    const Command responseCommand() const {
1614022Sstever@eecs.umich.edu        return commandInfo[cmd].response;
1624022Sstever@eecs.umich.edu    }
1634022Sstever@eecs.umich.edu
1644022Sstever@eecs.umich.edu    /** Return the string to a cmd given by idx. */
1654022Sstever@eecs.umich.edu    const std::string &toString() const {
1664022Sstever@eecs.umich.edu        return commandInfo[cmd].str;
1674022Sstever@eecs.umich.edu    }
1684022Sstever@eecs.umich.edu
1694022Sstever@eecs.umich.edu    int toInt() const { return (int)cmd; }
1704022Sstever@eecs.umich.edu
1714022Sstever@eecs.umich.edu    MemCmd(Command _cmd)
1724022Sstever@eecs.umich.edu        : cmd(_cmd)
1734022Sstever@eecs.umich.edu    { }
1744022Sstever@eecs.umich.edu
1754022Sstever@eecs.umich.edu    MemCmd(int _cmd)
1764022Sstever@eecs.umich.edu        : cmd((Command)_cmd)
1774022Sstever@eecs.umich.edu    { }
1784022Sstever@eecs.umich.edu
1794022Sstever@eecs.umich.edu    MemCmd()
1804022Sstever@eecs.umich.edu        : cmd(InvalidCmd)
1814022Sstever@eecs.umich.edu    { }
1824022Sstever@eecs.umich.edu
1834022Sstever@eecs.umich.edu    bool operator==(MemCmd c2) { return (cmd == c2.cmd); }
1844022Sstever@eecs.umich.edu    bool operator!=(MemCmd c2) { return (cmd != c2.cmd); }
1854022Sstever@eecs.umich.edu
1864022Sstever@eecs.umich.edu    friend class Packet;
1874022Sstever@eecs.umich.edu};
1884022Sstever@eecs.umich.edu
1892381SN/A/**
1902662Sstever@eecs.umich.edu * A Packet is used to encapsulate a transfer between two objects in
1912662Sstever@eecs.umich.edu * the memory system (e.g., the L1 and L2 cache).  (In contrast, a
1922662Sstever@eecs.umich.edu * single Request travels all the way from the requester to the
1932662Sstever@eecs.umich.edu * ultimate destination and back, possibly being conveyed by several
1942662Sstever@eecs.umich.edu * different Packets along the way.)
1952381SN/A */
1965314Sstever@gmail.comclass Packet : public FastAlloc, public Printable
1972381SN/A{
1982813Srdreslin@umich.edu  public:
1994022Sstever@eecs.umich.edu
2004022Sstever@eecs.umich.edu    typedef MemCmd::Command Command;
2014022Sstever@eecs.umich.edu
2024870Sstever@eecs.umich.edu    /** The command field of the packet. */
2034870Sstever@eecs.umich.edu    MemCmd cmd;
2044870Sstever@eecs.umich.edu
2054870Sstever@eecs.umich.edu    /** A pointer to the original request. */
2064870Sstever@eecs.umich.edu    RequestPtr req;
2074870Sstever@eecs.umich.edu
2082566SN/A  private:
2092662Sstever@eecs.umich.edu   /** A pointer to the data being transfered.  It can be differnt
2102662Sstever@eecs.umich.edu    *    sizes at each level of the heirarchy so it belongs in the
2112662Sstever@eecs.umich.edu    *    packet, not request. This may or may not be populated when a
2122662Sstever@eecs.umich.edu    *    responder recieves the packet. If not populated it memory
2132662Sstever@eecs.umich.edu    *    should be allocated.
2142566SN/A    */
2152566SN/A    PacketDataPtr data;
2162566SN/A
2172662Sstever@eecs.umich.edu    /** Is the data pointer set to a value that shouldn't be freed
2182662Sstever@eecs.umich.edu     *   when the packet is destroyed? */
2192566SN/A    bool staticData;
2202662Sstever@eecs.umich.edu    /** The data pointer points to a value that should be freed when
2212662Sstever@eecs.umich.edu     *   the packet is destroyed. */
2222566SN/A    bool dynamicData;
2232662Sstever@eecs.umich.edu    /** the data pointer points to an array (thus delete [] ) needs to
2242662Sstever@eecs.umich.edu     *   be called on it rather than simply delete.*/
2252566SN/A    bool arrayData;
2262566SN/A
2272662Sstever@eecs.umich.edu    /** The address of the request.  This address could be virtual or
2282662Sstever@eecs.umich.edu     *   physical, depending on the system configuration. */
2292381SN/A    Addr addr;
2302381SN/A
2312662Sstever@eecs.umich.edu     /** The size of the request or transfer. */
2322381SN/A    int size;
2332381SN/A
2342662Sstever@eecs.umich.edu    /** Device address (e.g., bus ID) of the source of the
2352662Sstever@eecs.umich.edu     *   transaction. The source is not responsible for setting this
2362662Sstever@eecs.umich.edu     *   field; it is set implicitly by the interconnect when the
2373349Sbinkertn@umich.edu     *   packet is first sent.  */
2382381SN/A    short src;
2392381SN/A
2402662Sstever@eecs.umich.edu    /** Device address (e.g., bus ID) of the destination of the
2412662Sstever@eecs.umich.edu     *   transaction. The special value Broadcast indicates that the
2422662Sstever@eecs.umich.edu     *   packet should be routed based on its address. This field is
2432662Sstever@eecs.umich.edu     *   initialized in the constructor and is thus always valid
2442662Sstever@eecs.umich.edu     *   (unlike * addr, size, and src). */
2452641Sstever@eecs.umich.edu    short dest;
2462641Sstever@eecs.umich.edu
2474870Sstever@eecs.umich.edu    /** The original value of the command field.  Only valid when the
2484870Sstever@eecs.umich.edu     * current command field is an error condition; in that case, the
2494870Sstever@eecs.umich.edu     * previous contents of the command field are copied here.  This
2504870Sstever@eecs.umich.edu     * field is *not* set on non-error responses.
2514870Sstever@eecs.umich.edu     */
2524870Sstever@eecs.umich.edu    MemCmd origCmd;
2534870Sstever@eecs.umich.edu
2542663Sstever@eecs.umich.edu    /** Are the 'addr' and 'size' fields valid? */
2552663Sstever@eecs.umich.edu    bool addrSizeValid;
2562662Sstever@eecs.umich.edu    /** Is the 'src' field valid? */
2572641Sstever@eecs.umich.edu    bool srcValid;
2584870Sstever@eecs.umich.edu    bool destValid;
2592813Srdreslin@umich.edu
2604870Sstever@eecs.umich.edu    enum Flag {
2614895Sstever@eecs.umich.edu        // Snoop response flags
2624626Sstever@eecs.umich.edu        MemInhibit,
2634626Sstever@eecs.umich.edu        Shared,
2644895Sstever@eecs.umich.edu        // Special control flags
2654916Sstever@eecs.umich.edu        /// Special timing-mode atomic snoop for multi-level coherence.
2664895Sstever@eecs.umich.edu        ExpressSnoop,
2674916Sstever@eecs.umich.edu        /// Does supplier have exclusive copy?
2684916Sstever@eecs.umich.edu        /// Useful for multi-level coherence.
2694916Sstever@eecs.umich.edu        SupplyExclusive,
2704870Sstever@eecs.umich.edu        NUM_PACKET_FLAGS
2714626Sstever@eecs.umich.edu    };
2724626Sstever@eecs.umich.edu
2734870Sstever@eecs.umich.edu    /** Status flags */
2744870Sstever@eecs.umich.edu    std::bitset<NUM_PACKET_FLAGS> flags;
2752641Sstever@eecs.umich.edu
2762641Sstever@eecs.umich.edu  public:
2772641Sstever@eecs.umich.edu
2782811Srdreslin@umich.edu    /** Used to calculate latencies for each packet.*/
2792811Srdreslin@umich.edu    Tick time;
2802811Srdreslin@umich.edu
2813218Sgblack@eecs.umich.edu    /** The time at which the packet will be fully transmitted */
2823218Sgblack@eecs.umich.edu    Tick finishTime;
2833218Sgblack@eecs.umich.edu
2843218Sgblack@eecs.umich.edu    /** The time at which the first chunk of the packet will be transmitted */
2853218Sgblack@eecs.umich.edu    Tick firstWordTime;
2863218Sgblack@eecs.umich.edu
2872662Sstever@eecs.umich.edu    /** The special destination address indicating that the packet
2882662Sstever@eecs.umich.edu     *   should be routed based on its address. */
2892623SN/A    static const short Broadcast = -1;
2902623SN/A
2912662Sstever@eecs.umich.edu    /** A virtual base opaque structure used to hold state associated
2922662Sstever@eecs.umich.edu     *    with the packet but specific to the sending device (e.g., an
2932662Sstever@eecs.umich.edu     *    MSHR).  A pointer to this state is returned in the packet's
2942662Sstever@eecs.umich.edu     *    response so that the sender can quickly look up the state
2952662Sstever@eecs.umich.edu     *    needed to process it.  A specific subclass would be derived
2962662Sstever@eecs.umich.edu     *    from this to carry state specific to a particular sending
2972662Sstever@eecs.umich.edu     *    device.  */
2984610Ssaidi@eecs.umich.edu    class SenderState : public FastAlloc {
2992641Sstever@eecs.umich.edu      public:
3002641Sstever@eecs.umich.edu        virtual ~SenderState() {}
3012641Sstever@eecs.umich.edu    };
3022641Sstever@eecs.umich.edu
3035315Sstever@gmail.com    /**
3045315Sstever@gmail.com     * Object used to maintain state of a PrintReq.  The senderState
3055315Sstever@gmail.com     * field of a PrintReq should always be of this type.
3065315Sstever@gmail.com     */
3075314Sstever@gmail.com    class PrintReqState : public SenderState {
3085315Sstever@gmail.com        /** An entry in the label stack. */
3095314Sstever@gmail.com        class LabelStackEntry {
3105314Sstever@gmail.com          public:
3115314Sstever@gmail.com            const std::string label;
3125314Sstever@gmail.com            std::string *prefix;
3135314Sstever@gmail.com            bool labelPrinted;
3145314Sstever@gmail.com            LabelStackEntry(const std::string &_label,
3155314Sstever@gmail.com                            std::string *_prefix);
3165314Sstever@gmail.com        };
3175314Sstever@gmail.com
3185314Sstever@gmail.com        typedef std::list<LabelStackEntry> LabelStack;
3195314Sstever@gmail.com        LabelStack labelStack;
3205314Sstever@gmail.com
3215314Sstever@gmail.com        std::string *curPrefixPtr;
3225314Sstever@gmail.com
3235314Sstever@gmail.com      public:
3245314Sstever@gmail.com        std::ostream &os;
3255314Sstever@gmail.com        const int verbosity;
3265314Sstever@gmail.com
3275314Sstever@gmail.com        PrintReqState(std::ostream &os, int verbosity = 0);
3285314Sstever@gmail.com        ~PrintReqState();
3295314Sstever@gmail.com
3305315Sstever@gmail.com        /** Returns the current line prefix. */
3315314Sstever@gmail.com        const std::string &curPrefix() { return *curPrefixPtr; }
3325315Sstever@gmail.com
3335315Sstever@gmail.com        /** Push a label onto the label stack, and prepend the given
3345315Sstever@gmail.com         * prefix string onto the current prefix.  Labels will only be
3355315Sstever@gmail.com         * printed if an object within the label's scope is
3365315Sstever@gmail.com         * printed. */
3375314Sstever@gmail.com        void pushLabel(const std::string &lbl,
3385314Sstever@gmail.com                       const std::string &prefix = "  ");
3395315Sstever@gmail.com        /** Pop a label off the label stack. */
3405314Sstever@gmail.com        void popLabel();
3415315Sstever@gmail.com        /** Print all of the pending unprinted labels on the
3425315Sstever@gmail.com         * stack. Called by printObj(), so normally not called by
3435315Sstever@gmail.com         * users unless bypassing printObj(). */
3445314Sstever@gmail.com        void printLabels();
3455315Sstever@gmail.com        /** Print a Printable object to os, because it matched the
3465315Sstever@gmail.com         * address on a PrintReq. */
3475314Sstever@gmail.com        void printObj(Printable *obj);
3485314Sstever@gmail.com    };
3495314Sstever@gmail.com
3502662Sstever@eecs.umich.edu    /** This packet's sender state.  Devices should use dynamic_cast<>
3512662Sstever@eecs.umich.edu     *   to cast to the state appropriate to the sender. */
3522662Sstever@eecs.umich.edu    SenderState *senderState;
3532641Sstever@eecs.umich.edu
3542662Sstever@eecs.umich.edu    /** Return the string name of the cmd field (for debugging and
3552662Sstever@eecs.umich.edu     *   tracing). */
3564022Sstever@eecs.umich.edu    const std::string &cmdString() const { return cmd.toString(); }
3572811Srdreslin@umich.edu
3582811Srdreslin@umich.edu    /** Return the index of this command. */
3594022Sstever@eecs.umich.edu    inline int cmdToIndex() const { return cmd.toInt(); }
3602811Srdreslin@umich.edu
3614022Sstever@eecs.umich.edu    bool isRead() const         { return cmd.isRead(); }
3624022Sstever@eecs.umich.edu    bool isWrite()  const       { return cmd.isWrite(); }
3634022Sstever@eecs.umich.edu    bool isRequest() const      { return cmd.isRequest(); }
3644022Sstever@eecs.umich.edu    bool isResponse() const     { return cmd.isResponse(); }
3654870Sstever@eecs.umich.edu    bool needsExclusive() const { return cmd.needsExclusive(); }
3664022Sstever@eecs.umich.edu    bool needsResponse() const  { return cmd.needsResponse(); }
3674022Sstever@eecs.umich.edu    bool isInvalidate() const   { return cmd.isInvalidate(); }
3684022Sstever@eecs.umich.edu    bool hasData() const        { return cmd.hasData(); }
3694040Ssaidi@eecs.umich.edu    bool isReadWrite() const    { return cmd.isReadWrite(); }
3704626Sstever@eecs.umich.edu    bool isLocked() const       { return cmd.isLocked(); }
3714870Sstever@eecs.umich.edu    bool isError() const        { return cmd.isError(); }
3725314Sstever@gmail.com    bool isPrint() const        { return cmd.isPrint(); }
3732812Srdreslin@umich.edu
3744870Sstever@eecs.umich.edu    // Snoop flags
3754870Sstever@eecs.umich.edu    void assertMemInhibit()     { flags[MemInhibit] = true; }
3764916Sstever@eecs.umich.edu    bool memInhibitAsserted()   { return flags[MemInhibit]; }
3774870Sstever@eecs.umich.edu    void assertShared()         { flags[Shared] = true; }
3784870Sstever@eecs.umich.edu    bool sharedAsserted()       { return flags[Shared]; }
3794870Sstever@eecs.umich.edu
3804895Sstever@eecs.umich.edu    // Special control flags
3814895Sstever@eecs.umich.edu    void setExpressSnoop()      { flags[ExpressSnoop] = true; }
3824895Sstever@eecs.umich.edu    bool isExpressSnoop()       { return flags[ExpressSnoop]; }
3834916Sstever@eecs.umich.edu    void setSupplyExclusive()   { flags[SupplyExclusive] = true; }
3844916Sstever@eecs.umich.edu    bool isSupplyExclusive()    { return flags[SupplyExclusive]; }
3854895Sstever@eecs.umich.edu
3864870Sstever@eecs.umich.edu    // Network error conditions... encapsulate them as methods since
3874870Sstever@eecs.umich.edu    // their encoding keeps changing (from result field to command
3884870Sstever@eecs.umich.edu    // field, etc.)
3894986Ssaidi@eecs.umich.edu    void setNacked()     { assert(isResponse()); cmd = MemCmd::NetworkNackError; }
3904986Ssaidi@eecs.umich.edu    void setBadAddress() { assert(isResponse()); cmd = MemCmd::BadAddressError; }
3914870Sstever@eecs.umich.edu    bool wasNacked()     { return cmd == MemCmd::NetworkNackError; }
3924870Sstever@eecs.umich.edu    bool hadBadAddress() { return cmd == MemCmd::BadAddressError; }
3934986Ssaidi@eecs.umich.edu    void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
3942814Srdreslin@umich.edu
3953940Ssaidi@eecs.umich.edu    bool nic_pkt() { panic("Unimplemented"); M5_DUMMY_RETURN }
3962641Sstever@eecs.umich.edu
3972381SN/A    /** Accessor function that returns the source index of the packet. */
3984870Sstever@eecs.umich.edu    short getSrc() const    { assert(srcValid); return src; }
3992641Sstever@eecs.umich.edu    void setSrc(short _src) { src = _src; srcValid = true; }
4004626Sstever@eecs.umich.edu    /** Reset source field, e.g. to retransmit packet on different bus. */
4014626Sstever@eecs.umich.edu    void clearSrc() { srcValid = false; }
4022381SN/A
4032381SN/A    /** Accessor function that returns the destination index of
4042381SN/A        the packet. */
4054870Sstever@eecs.umich.edu    short getDest() const     { assert(destValid); return dest; }
4064870Sstever@eecs.umich.edu    void setDest(short _dest) { dest = _dest; destValid = true; }
4072549SN/A
4082663Sstever@eecs.umich.edu    Addr getAddr() const { assert(addrSizeValid); return addr; }
4094870Sstever@eecs.umich.edu    int getSize() const  { assert(addrSizeValid); return size; }
4102883Srdreslin@umich.edu    Addr getOffset(int blkSize) const { return addr & (Addr)(blkSize - 1); }
4112813Srdreslin@umich.edu
4122662Sstever@eecs.umich.edu    /** Constructor.  Note that a Request object must be constructed
4132662Sstever@eecs.umich.edu     *   first, but the Requests's physical address and size fields
4142662Sstever@eecs.umich.edu     *   need not be valid. The command and destination addresses
4152662Sstever@eecs.umich.edu     *   must be supplied.  */
4164022Sstever@eecs.umich.edu    Packet(Request *_req, MemCmd _cmd, short _dest)
4174870Sstever@eecs.umich.edu        :  cmd(_cmd), req(_req),
4184870Sstever@eecs.umich.edu           data(NULL), staticData(false), dynamicData(false), arrayData(false),
4192641Sstever@eecs.umich.edu           addr(_req->paddr), size(_req->size), dest(_dest),
4204870Sstever@eecs.umich.edu           addrSizeValid(_req->validPaddr), srcValid(false), destValid(true),
4214870Sstever@eecs.umich.edu           flags(0), time(curTick), senderState(NULL)
4222641Sstever@eecs.umich.edu    {
4232813Srdreslin@umich.edu    }
4242813Srdreslin@umich.edu
4252813Srdreslin@umich.edu    /** Alternate constructor if you are trying to create a packet with
4262813Srdreslin@umich.edu     *  a request that is for a whole block, not the address from the req.
4272813Srdreslin@umich.edu     *  this allows for overriding the size/addr of the req.*/
4284022Sstever@eecs.umich.edu    Packet(Request *_req, MemCmd _cmd, short _dest, int _blkSize)
4294870Sstever@eecs.umich.edu        :  cmd(_cmd), req(_req),
4304870Sstever@eecs.umich.edu           data(NULL), staticData(false), dynamicData(false), arrayData(false),
4314626Sstever@eecs.umich.edu           addr(_req->paddr & ~(_blkSize - 1)), size(_blkSize), dest(_dest),
4324870Sstever@eecs.umich.edu           addrSizeValid(_req->validPaddr), srcValid(false), destValid(true),
4334870Sstever@eecs.umich.edu           flags(0), time(curTick), senderState(NULL)
4342813Srdreslin@umich.edu    {
4354626Sstever@eecs.umich.edu    }
4364626Sstever@eecs.umich.edu
4374626Sstever@eecs.umich.edu    /** Alternate constructor for copying a packet.  Copy all fields
4384887Sstever@eecs.umich.edu     * *except* if the original packet's data was dynamic, don't copy
4394887Sstever@eecs.umich.edu     * that, as we can't guarantee that the new packet's lifetime is
4404887Sstever@eecs.umich.edu     * less than that of the original packet.  In this case the new
4414887Sstever@eecs.umich.edu     * packet should allocate its own data. */
4424895Sstever@eecs.umich.edu    Packet(Packet *origPkt, bool clearFlags = false)
4434870Sstever@eecs.umich.edu        :  cmd(origPkt->cmd), req(origPkt->req),
4444887Sstever@eecs.umich.edu           data(origPkt->staticData ? origPkt->data : NULL),
4454887Sstever@eecs.umich.edu           staticData(origPkt->staticData),
4464887Sstever@eecs.umich.edu           dynamicData(false), arrayData(false),
4474626Sstever@eecs.umich.edu           addr(origPkt->addr), size(origPkt->size),
4484660Sstever@eecs.umich.edu           src(origPkt->src), dest(origPkt->dest),
4494870Sstever@eecs.umich.edu           addrSizeValid(origPkt->addrSizeValid),
4504870Sstever@eecs.umich.edu           srcValid(origPkt->srcValid), destValid(origPkt->destValid),
4514895Sstever@eecs.umich.edu           flags(clearFlags ? 0 : origPkt->flags),
4524870Sstever@eecs.umich.edu           time(curTick), senderState(origPkt->senderState)
4534626Sstever@eecs.umich.edu    {
4542641Sstever@eecs.umich.edu    }
4552549SN/A
4562662Sstever@eecs.umich.edu    /** Destructor. */
4572566SN/A    ~Packet()
4583665Srdreslin@umich.edu    { if (staticData || dynamicData) deleteData(); }
4592566SN/A
4602662Sstever@eecs.umich.edu    /** Reinitialize packet address and size from the associated
4612662Sstever@eecs.umich.edu     *   Request object, and reset other fields that may have been
4622662Sstever@eecs.umich.edu     *   modified by a previous transaction.  Typically called when a
4632662Sstever@eecs.umich.edu     *   statically allocated Request/Packet pair is reused for
4642662Sstever@eecs.umich.edu     *   multiple transactions. */
4652662Sstever@eecs.umich.edu    void reinitFromRequest() {
4662662Sstever@eecs.umich.edu        assert(req->validPaddr);
4674870Sstever@eecs.umich.edu        flags = 0;
4682663Sstever@eecs.umich.edu        addr = req->paddr;
4692663Sstever@eecs.umich.edu        size = req->size;
4703018Srdreslin@umich.edu        time = req->time;
4712663Sstever@eecs.umich.edu        addrSizeValid = true;
4722662Sstever@eecs.umich.edu        if (dynamicData) {
4732662Sstever@eecs.umich.edu            deleteData();
4742662Sstever@eecs.umich.edu            dynamicData = false;
4752662Sstever@eecs.umich.edu            arrayData = false;
4762662Sstever@eecs.umich.edu        }
4772662Sstever@eecs.umich.edu    }
4782566SN/A
4794626Sstever@eecs.umich.edu    /**
4804626Sstever@eecs.umich.edu     * Take a request packet and modify it in place to be suitable for
4814626Sstever@eecs.umich.edu     * returning as a response to that request.  The source and
4824626Sstever@eecs.umich.edu     * destination fields are *not* modified, as is appropriate for
4834626Sstever@eecs.umich.edu     * atomic accesses.
4844626Sstever@eecs.umich.edu     */
4854870Sstever@eecs.umich.edu    void makeResponse()
4864626Sstever@eecs.umich.edu    {
4872662Sstever@eecs.umich.edu        assert(needsResponse());
4882855Srdreslin@umich.edu        assert(isRequest());
4894986Ssaidi@eecs.umich.edu        origCmd = cmd;
4904022Sstever@eecs.umich.edu        cmd = cmd.responseCommand();
4914870Sstever@eecs.umich.edu        dest = src;
4924870Sstever@eecs.umich.edu        destValid = srcValid;
4932662Sstever@eecs.umich.edu        srcValid = false;
4942641Sstever@eecs.umich.edu    }
4952641Sstever@eecs.umich.edu
4964870Sstever@eecs.umich.edu    void makeAtomicResponse()
4974870Sstever@eecs.umich.edu    {
4984870Sstever@eecs.umich.edu        makeResponse();
4994870Sstever@eecs.umich.edu    }
5004870Sstever@eecs.umich.edu
5014626Sstever@eecs.umich.edu    void makeTimingResponse()
5023348Sbinkertn@umich.edu    {
5034870Sstever@eecs.umich.edu        makeResponse();
5043135Srdreslin@umich.edu    }
5053135Srdreslin@umich.edu
5063348Sbinkertn@umich.edu    /**
5073348Sbinkertn@umich.edu     * Take a request packet that has been returned as NACKED and
5083348Sbinkertn@umich.edu     * modify it so that it can be sent out again. Only packets that
5093348Sbinkertn@umich.edu     * need a response can be NACKED, so verify that that is true.
5103348Sbinkertn@umich.edu     */
5113348Sbinkertn@umich.edu    void
5123348Sbinkertn@umich.edu    reinitNacked()
5133348Sbinkertn@umich.edu    {
5144870Sstever@eecs.umich.edu        assert(wasNacked());
5154870Sstever@eecs.umich.edu        cmd = origCmd;
5164870Sstever@eecs.umich.edu        assert(needsResponse());
5174870Sstever@eecs.umich.edu        setDest(Broadcast);
5182685Ssaidi@eecs.umich.edu    }
5192685Ssaidi@eecs.umich.edu
5202685Ssaidi@eecs.umich.edu
5213348Sbinkertn@umich.edu    /**
5223348Sbinkertn@umich.edu     * Set the data pointer to the following value that should not be
5233348Sbinkertn@umich.edu     * freed.
5242566SN/A     */
5252566SN/A    template <typename T>
5263348Sbinkertn@umich.edu    void
5273348Sbinkertn@umich.edu    dataStatic(T *p)
5283348Sbinkertn@umich.edu    {
5293348Sbinkertn@umich.edu        if(dynamicData)
5303348Sbinkertn@umich.edu            dynamicData = false;
5313348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
5323348Sbinkertn@umich.edu        staticData = true;
5333348Sbinkertn@umich.edu    }
5342566SN/A
5353348Sbinkertn@umich.edu    /**
5363348Sbinkertn@umich.edu     * Set the data pointer to a value that should have delete []
5373348Sbinkertn@umich.edu     * called on it.
5383348Sbinkertn@umich.edu     */
5392566SN/A    template <typename T>
5403348Sbinkertn@umich.edu    void
5413348Sbinkertn@umich.edu    dataDynamicArray(T *p)
5423348Sbinkertn@umich.edu    {
5433348Sbinkertn@umich.edu        assert(!staticData && !dynamicData);
5443348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
5453348Sbinkertn@umich.edu        dynamicData = true;
5463348Sbinkertn@umich.edu        arrayData = true;
5473348Sbinkertn@umich.edu    }
5483348Sbinkertn@umich.edu
5493348Sbinkertn@umich.edu    /**
5503348Sbinkertn@umich.edu     * set the data pointer to a value that should have delete called
5513348Sbinkertn@umich.edu     * on it.
5523348Sbinkertn@umich.edu     */
5533348Sbinkertn@umich.edu    template <typename T>
5543348Sbinkertn@umich.edu    void
5553348Sbinkertn@umich.edu    dataDynamic(T *p)
5563348Sbinkertn@umich.edu    {
5573348Sbinkertn@umich.edu        assert(!staticData && !dynamicData);
5583348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
5593348Sbinkertn@umich.edu        dynamicData = true;
5603348Sbinkertn@umich.edu        arrayData = false;
5613348Sbinkertn@umich.edu    }
5623348Sbinkertn@umich.edu
5633348Sbinkertn@umich.edu    /** get a pointer to the data ptr. */
5643348Sbinkertn@umich.edu    template <typename T>
5653348Sbinkertn@umich.edu    T*
5663348Sbinkertn@umich.edu    getPtr()
5673348Sbinkertn@umich.edu    {
5683348Sbinkertn@umich.edu        assert(staticData || dynamicData);
5693348Sbinkertn@umich.edu        return (T*)data;
5703348Sbinkertn@umich.edu    }
5712566SN/A
5722566SN/A    /** return the value of what is pointed to in the packet. */
5732566SN/A    template <typename T>
5742592SN/A    T get();
5752566SN/A
5762566SN/A    /** set the value in the data pointer to v. */
5772566SN/A    template <typename T>
5782592SN/A    void set(T v);
5792566SN/A
5803348Sbinkertn@umich.edu    /**
5814626Sstever@eecs.umich.edu     * Copy data into the packet from the provided pointer.
5824626Sstever@eecs.umich.edu     */
5834626Sstever@eecs.umich.edu    void setData(uint8_t *p)
5844626Sstever@eecs.umich.edu    {
5854626Sstever@eecs.umich.edu        std::memcpy(getPtr<uint8_t>(), p, getSize());
5864626Sstever@eecs.umich.edu    }
5874626Sstever@eecs.umich.edu
5884626Sstever@eecs.umich.edu    /**
5894626Sstever@eecs.umich.edu     * Copy data into the packet from the provided block pointer,
5904626Sstever@eecs.umich.edu     * which is aligned to the given block size.
5914626Sstever@eecs.umich.edu     */
5924626Sstever@eecs.umich.edu    void setDataFromBlock(uint8_t *blk_data, int blkSize)
5934626Sstever@eecs.umich.edu    {
5944626Sstever@eecs.umich.edu        setData(blk_data + getOffset(blkSize));
5954626Sstever@eecs.umich.edu    }
5964626Sstever@eecs.umich.edu
5974626Sstever@eecs.umich.edu    /**
5984626Sstever@eecs.umich.edu     * Copy data from the packet to the provided block pointer, which
5994626Sstever@eecs.umich.edu     * is aligned to the given block size.
6004626Sstever@eecs.umich.edu     */
6014626Sstever@eecs.umich.edu    void writeData(uint8_t *p)
6024626Sstever@eecs.umich.edu    {
6034626Sstever@eecs.umich.edu        std::memcpy(p, getPtr<uint8_t>(), getSize());
6044626Sstever@eecs.umich.edu    }
6054626Sstever@eecs.umich.edu
6064626Sstever@eecs.umich.edu    /**
6074626Sstever@eecs.umich.edu     * Copy data from the packet to the memory at the provided pointer.
6084626Sstever@eecs.umich.edu     */
6094626Sstever@eecs.umich.edu    void writeDataToBlock(uint8_t *blk_data, int blkSize)
6104626Sstever@eecs.umich.edu    {
6114626Sstever@eecs.umich.edu        writeData(blk_data + getOffset(blkSize));
6124626Sstever@eecs.umich.edu    }
6134626Sstever@eecs.umich.edu
6144626Sstever@eecs.umich.edu    /**
6153348Sbinkertn@umich.edu     * delete the data pointed to in the data pointer. Ok to call to
6163348Sbinkertn@umich.edu     * matter how data was allocted.
6173348Sbinkertn@umich.edu     */
6182592SN/A    void deleteData();
6192566SN/A
6202566SN/A    /** If there isn't data in the packet, allocate some. */
6212592SN/A    void allocate();
6222568SN/A
6234626Sstever@eecs.umich.edu    /**
6244626Sstever@eecs.umich.edu     * Check a functional request against a memory value represented
6254626Sstever@eecs.umich.edu     * by a base/size pair and an associated data array.  If the
6264626Sstever@eecs.umich.edu     * functional request is a read, it may be satisfied by the memory
6274626Sstever@eecs.umich.edu     * value.  If the functional request is a write, it may update the
6284626Sstever@eecs.umich.edu     * memory value.
6294626Sstever@eecs.umich.edu     */
6305314Sstever@gmail.com    bool checkFunctional(Printable *obj, Addr base, int size, uint8_t *data);
6314626Sstever@eecs.umich.edu
6324626Sstever@eecs.umich.edu    /**
6334626Sstever@eecs.umich.edu     * Check a functional request against a memory value stored in
6345315Sstever@gmail.com     * another packet (i.e. an in-transit request or response).
6354626Sstever@eecs.umich.edu     */
6364626Sstever@eecs.umich.edu    bool checkFunctional(PacketPtr otherPkt) {
6375314Sstever@gmail.com        return checkFunctional(otherPkt,
6385314Sstever@gmail.com                               otherPkt->getAddr(), otherPkt->getSize(),
6395314Sstever@gmail.com                               otherPkt->hasData() ?
6405314Sstever@gmail.com                                   otherPkt->getPtr<uint8_t>() : NULL);
6414626Sstever@eecs.umich.edu    }
6425314Sstever@gmail.com
6435315Sstever@gmail.com    /**
6445315Sstever@gmail.com     * Push label for PrintReq (safe to call unconditionally).
6455315Sstever@gmail.com     */
6465314Sstever@gmail.com    void pushLabel(const std::string &lbl) {
6475314Sstever@gmail.com        if (isPrint()) {
6485314Sstever@gmail.com            dynamic_cast<PrintReqState*>(senderState)->pushLabel(lbl);
6495314Sstever@gmail.com        }
6505314Sstever@gmail.com    }
6515314Sstever@gmail.com
6525315Sstever@gmail.com    /**
6535315Sstever@gmail.com     * Pop label for PrintReq (safe to call unconditionally).
6545315Sstever@gmail.com     */
6555314Sstever@gmail.com    void popLabel() {
6565314Sstever@gmail.com        if (isPrint()) {
6575314Sstever@gmail.com            dynamic_cast<PrintReqState*>(senderState)->popLabel();
6585314Sstever@gmail.com        }
6595314Sstever@gmail.com    }
6605314Sstever@gmail.com
6615314Sstever@gmail.com    void print(std::ostream &o, int verbosity = 0,
6625314Sstever@gmail.com               const std::string &prefix = "") const;
6632381SN/A};
6642381SN/A
6652381SN/A#endif //__MEM_PACKET_HH
666