packet.hh revision 5315
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,
694022Sstever@eecs.umich.edu        WriteReq,
704022Sstever@eecs.umich.edu        WriteResp,
714022Sstever@eecs.umich.edu        Writeback,
724022Sstever@eecs.umich.edu        SoftPFReq,
734022Sstever@eecs.umich.edu        HardPFReq,
744022Sstever@eecs.umich.edu        SoftPFResp,
754022Sstever@eecs.umich.edu        HardPFResp,
764022Sstever@eecs.umich.edu        WriteInvalidateReq,
774022Sstever@eecs.umich.edu        WriteInvalidateResp,
784022Sstever@eecs.umich.edu        UpgradeReq,
794628Sstever@eecs.umich.edu        UpgradeResp,
804022Sstever@eecs.umich.edu        ReadExReq,
814022Sstever@eecs.umich.edu        ReadExResp,
824626Sstever@eecs.umich.edu        LoadLockedReq,
834626Sstever@eecs.umich.edu        LoadLockedResp,
844626Sstever@eecs.umich.edu        StoreCondReq,
854626Sstever@eecs.umich.edu        StoreCondResp,
864040Ssaidi@eecs.umich.edu        SwapReq,
874040Ssaidi@eecs.umich.edu        SwapResp,
884870Sstever@eecs.umich.edu        // Error responses
894870Sstever@eecs.umich.edu        // @TODO these should be classified as responses rather than
904870Sstever@eecs.umich.edu        // requests; coding them as requests initially for backwards
914870Sstever@eecs.umich.edu        // compatibility
924870Sstever@eecs.umich.edu        NetworkNackError,  // nacked at network layer (not by protocol)
934870Sstever@eecs.umich.edu        InvalidDestError,  // packet dest field invalid
944870Sstever@eecs.umich.edu        BadAddressError,   // memory address invalid
955314Sstever@gmail.com        // Fake simulator-only commands
965314Sstever@gmail.com        PrintReq,       // Print state matching address
974022Sstever@eecs.umich.edu        NUM_MEM_CMDS
984022Sstever@eecs.umich.edu    };
994022Sstever@eecs.umich.edu
1004022Sstever@eecs.umich.edu  private:
1014022Sstever@eecs.umich.edu    /** List of command attributes. */
1024022Sstever@eecs.umich.edu    enum Attribute
1034022Sstever@eecs.umich.edu    {
1044626Sstever@eecs.umich.edu        IsRead,         //!< Data flows from responder to requester
1054626Sstever@eecs.umich.edu        IsWrite,        //!< Data flows from requester to responder
1064626Sstever@eecs.umich.edu        IsPrefetch,     //!< Not a demand access
1074022Sstever@eecs.umich.edu        IsInvalidate,
1084626Sstever@eecs.umich.edu        NeedsExclusive, //!< Requires exclusive copy to complete in-cache
1094626Sstever@eecs.umich.edu        IsRequest,      //!< Issued by requester
1104626Sstever@eecs.umich.edu        IsResponse,     //!< Issue by responder
1114626Sstever@eecs.umich.edu        NeedsResponse,  //!< Requester needs response from target
1124022Sstever@eecs.umich.edu        IsSWPrefetch,
1134022Sstever@eecs.umich.edu        IsHWPrefetch,
1144626Sstever@eecs.umich.edu        IsLocked,       //!< Alpha/MIPS LL or SC access
1154626Sstever@eecs.umich.edu        HasData,        //!< There is an associated payload
1164870Sstever@eecs.umich.edu        IsError,        //!< Error response
1175314Sstever@gmail.com        IsPrint,        //!< Print state matching address (for debugging)
1184022Sstever@eecs.umich.edu        NUM_COMMAND_ATTRIBUTES
1194022Sstever@eecs.umich.edu    };
1204022Sstever@eecs.umich.edu
1214022Sstever@eecs.umich.edu    /** Structure that defines attributes and other data associated
1224022Sstever@eecs.umich.edu     * with a Command. */
1234022Sstever@eecs.umich.edu    struct CommandInfo {
1244022Sstever@eecs.umich.edu        /** Set of attribute flags. */
1254022Sstever@eecs.umich.edu        const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
1264022Sstever@eecs.umich.edu        /** Corresponding response for requests; InvalidCmd if no
1274022Sstever@eecs.umich.edu         * response is applicable. */
1284022Sstever@eecs.umich.edu        const Command response;
1294022Sstever@eecs.umich.edu        /** String representation (for printing) */
1304022Sstever@eecs.umich.edu        const std::string str;
1314022Sstever@eecs.umich.edu    };
1324022Sstever@eecs.umich.edu
1334022Sstever@eecs.umich.edu    /** Array to map Command enum to associated info. */
1344022Sstever@eecs.umich.edu    static const CommandInfo commandInfo[];
1354022Sstever@eecs.umich.edu
1364022Sstever@eecs.umich.edu  private:
1374022Sstever@eecs.umich.edu
1384022Sstever@eecs.umich.edu    Command cmd;
1394022Sstever@eecs.umich.edu
1404022Sstever@eecs.umich.edu    bool testCmdAttrib(MemCmd::Attribute attrib) const {
1414022Sstever@eecs.umich.edu        return commandInfo[cmd].attributes[attrib] != 0;
1424022Sstever@eecs.umich.edu    }
1434022Sstever@eecs.umich.edu
1444022Sstever@eecs.umich.edu  public:
1454022Sstever@eecs.umich.edu
1464022Sstever@eecs.umich.edu    bool isRead() const         { return testCmdAttrib(IsRead); }
1474022Sstever@eecs.umich.edu    bool isWrite()  const       { return testCmdAttrib(IsWrite); }
1484022Sstever@eecs.umich.edu    bool isRequest() const      { return testCmdAttrib(IsRequest); }
1494022Sstever@eecs.umich.edu    bool isResponse() const     { return testCmdAttrib(IsResponse); }
1504870Sstever@eecs.umich.edu    bool needsExclusive() const { return testCmdAttrib(NeedsExclusive); }
1514022Sstever@eecs.umich.edu    bool needsResponse() const  { return testCmdAttrib(NeedsResponse); }
1524022Sstever@eecs.umich.edu    bool isInvalidate() const   { return testCmdAttrib(IsInvalidate); }
1534022Sstever@eecs.umich.edu    bool hasData() const        { return testCmdAttrib(HasData); }
1544626Sstever@eecs.umich.edu    bool isReadWrite() const    { return isRead() && isWrite(); }
1554626Sstever@eecs.umich.edu    bool isLocked() const       { return testCmdAttrib(IsLocked); }
1564870Sstever@eecs.umich.edu    bool isError() const        { return testCmdAttrib(IsError); }
1575314Sstever@gmail.com    bool isPrint() const        { return testCmdAttrib(IsPrint); }
1584022Sstever@eecs.umich.edu
1594022Sstever@eecs.umich.edu    const Command responseCommand() const {
1604022Sstever@eecs.umich.edu        return commandInfo[cmd].response;
1614022Sstever@eecs.umich.edu    }
1624022Sstever@eecs.umich.edu
1634022Sstever@eecs.umich.edu    /** Return the string to a cmd given by idx. */
1644022Sstever@eecs.umich.edu    const std::string &toString() const {
1654022Sstever@eecs.umich.edu        return commandInfo[cmd].str;
1664022Sstever@eecs.umich.edu    }
1674022Sstever@eecs.umich.edu
1684022Sstever@eecs.umich.edu    int toInt() const { return (int)cmd; }
1694022Sstever@eecs.umich.edu
1704022Sstever@eecs.umich.edu    MemCmd(Command _cmd)
1714022Sstever@eecs.umich.edu        : cmd(_cmd)
1724022Sstever@eecs.umich.edu    { }
1734022Sstever@eecs.umich.edu
1744022Sstever@eecs.umich.edu    MemCmd(int _cmd)
1754022Sstever@eecs.umich.edu        : cmd((Command)_cmd)
1764022Sstever@eecs.umich.edu    { }
1774022Sstever@eecs.umich.edu
1784022Sstever@eecs.umich.edu    MemCmd()
1794022Sstever@eecs.umich.edu        : cmd(InvalidCmd)
1804022Sstever@eecs.umich.edu    { }
1814022Sstever@eecs.umich.edu
1824022Sstever@eecs.umich.edu    bool operator==(MemCmd c2) { return (cmd == c2.cmd); }
1834022Sstever@eecs.umich.edu    bool operator!=(MemCmd c2) { return (cmd != c2.cmd); }
1844022Sstever@eecs.umich.edu
1854022Sstever@eecs.umich.edu    friend class Packet;
1864022Sstever@eecs.umich.edu};
1874022Sstever@eecs.umich.edu
1882381SN/A/**
1892662Sstever@eecs.umich.edu * A Packet is used to encapsulate a transfer between two objects in
1902662Sstever@eecs.umich.edu * the memory system (e.g., the L1 and L2 cache).  (In contrast, a
1912662Sstever@eecs.umich.edu * single Request travels all the way from the requester to the
1922662Sstever@eecs.umich.edu * ultimate destination and back, possibly being conveyed by several
1932662Sstever@eecs.umich.edu * different Packets along the way.)
1942381SN/A */
1955314Sstever@gmail.comclass Packet : public FastAlloc, public Printable
1962381SN/A{
1972813Srdreslin@umich.edu  public:
1984022Sstever@eecs.umich.edu
1994022Sstever@eecs.umich.edu    typedef MemCmd::Command Command;
2004022Sstever@eecs.umich.edu
2014870Sstever@eecs.umich.edu    /** The command field of the packet. */
2024870Sstever@eecs.umich.edu    MemCmd cmd;
2034870Sstever@eecs.umich.edu
2044870Sstever@eecs.umich.edu    /** A pointer to the original request. */
2054870Sstever@eecs.umich.edu    RequestPtr req;
2064870Sstever@eecs.umich.edu
2072566SN/A  private:
2082662Sstever@eecs.umich.edu   /** A pointer to the data being transfered.  It can be differnt
2092662Sstever@eecs.umich.edu    *    sizes at each level of the heirarchy so it belongs in the
2102662Sstever@eecs.umich.edu    *    packet, not request. This may or may not be populated when a
2112662Sstever@eecs.umich.edu    *    responder recieves the packet. If not populated it memory
2122662Sstever@eecs.umich.edu    *    should be allocated.
2132566SN/A    */
2142566SN/A    PacketDataPtr data;
2152566SN/A
2162662Sstever@eecs.umich.edu    /** Is the data pointer set to a value that shouldn't be freed
2172662Sstever@eecs.umich.edu     *   when the packet is destroyed? */
2182566SN/A    bool staticData;
2192662Sstever@eecs.umich.edu    /** The data pointer points to a value that should be freed when
2202662Sstever@eecs.umich.edu     *   the packet is destroyed. */
2212566SN/A    bool dynamicData;
2222662Sstever@eecs.umich.edu    /** the data pointer points to an array (thus delete [] ) needs to
2232662Sstever@eecs.umich.edu     *   be called on it rather than simply delete.*/
2242566SN/A    bool arrayData;
2252566SN/A
2262662Sstever@eecs.umich.edu    /** The address of the request.  This address could be virtual or
2272662Sstever@eecs.umich.edu     *   physical, depending on the system configuration. */
2282381SN/A    Addr addr;
2292381SN/A
2302662Sstever@eecs.umich.edu     /** The size of the request or transfer. */
2312381SN/A    int size;
2322381SN/A
2332662Sstever@eecs.umich.edu    /** Device address (e.g., bus ID) of the source of the
2342662Sstever@eecs.umich.edu     *   transaction. The source is not responsible for setting this
2352662Sstever@eecs.umich.edu     *   field; it is set implicitly by the interconnect when the
2363349Sbinkertn@umich.edu     *   packet is first sent.  */
2372381SN/A    short src;
2382381SN/A
2392662Sstever@eecs.umich.edu    /** Device address (e.g., bus ID) of the destination of the
2402662Sstever@eecs.umich.edu     *   transaction. The special value Broadcast indicates that the
2412662Sstever@eecs.umich.edu     *   packet should be routed based on its address. This field is
2422662Sstever@eecs.umich.edu     *   initialized in the constructor and is thus always valid
2432662Sstever@eecs.umich.edu     *   (unlike * addr, size, and src). */
2442641Sstever@eecs.umich.edu    short dest;
2452641Sstever@eecs.umich.edu
2464870Sstever@eecs.umich.edu    /** The original value of the command field.  Only valid when the
2474870Sstever@eecs.umich.edu     * current command field is an error condition; in that case, the
2484870Sstever@eecs.umich.edu     * previous contents of the command field are copied here.  This
2494870Sstever@eecs.umich.edu     * field is *not* set on non-error responses.
2504870Sstever@eecs.umich.edu     */
2514870Sstever@eecs.umich.edu    MemCmd origCmd;
2524870Sstever@eecs.umich.edu
2532663Sstever@eecs.umich.edu    /** Are the 'addr' and 'size' fields valid? */
2542663Sstever@eecs.umich.edu    bool addrSizeValid;
2552662Sstever@eecs.umich.edu    /** Is the 'src' field valid? */
2562641Sstever@eecs.umich.edu    bool srcValid;
2574870Sstever@eecs.umich.edu    bool destValid;
2582813Srdreslin@umich.edu
2594870Sstever@eecs.umich.edu    enum Flag {
2604895Sstever@eecs.umich.edu        // Snoop response flags
2614626Sstever@eecs.umich.edu        MemInhibit,
2624626Sstever@eecs.umich.edu        Shared,
2634895Sstever@eecs.umich.edu        // Special control flags
2644916Sstever@eecs.umich.edu        /// Special timing-mode atomic snoop for multi-level coherence.
2654895Sstever@eecs.umich.edu        ExpressSnoop,
2664916Sstever@eecs.umich.edu        /// Does supplier have exclusive copy?
2674916Sstever@eecs.umich.edu        /// Useful for multi-level coherence.
2684916Sstever@eecs.umich.edu        SupplyExclusive,
2694870Sstever@eecs.umich.edu        NUM_PACKET_FLAGS
2704626Sstever@eecs.umich.edu    };
2714626Sstever@eecs.umich.edu
2724870Sstever@eecs.umich.edu    /** Status flags */
2734870Sstever@eecs.umich.edu    std::bitset<NUM_PACKET_FLAGS> flags;
2742641Sstever@eecs.umich.edu
2752641Sstever@eecs.umich.edu  public:
2762641Sstever@eecs.umich.edu
2772811Srdreslin@umich.edu    /** Used to calculate latencies for each packet.*/
2782811Srdreslin@umich.edu    Tick time;
2792811Srdreslin@umich.edu
2803218Sgblack@eecs.umich.edu    /** The time at which the packet will be fully transmitted */
2813218Sgblack@eecs.umich.edu    Tick finishTime;
2823218Sgblack@eecs.umich.edu
2833218Sgblack@eecs.umich.edu    /** The time at which the first chunk of the packet will be transmitted */
2843218Sgblack@eecs.umich.edu    Tick firstWordTime;
2853218Sgblack@eecs.umich.edu
2862662Sstever@eecs.umich.edu    /** The special destination address indicating that the packet
2872662Sstever@eecs.umich.edu     *   should be routed based on its address. */
2882623SN/A    static const short Broadcast = -1;
2892623SN/A
2902662Sstever@eecs.umich.edu    /** A virtual base opaque structure used to hold state associated
2912662Sstever@eecs.umich.edu     *    with the packet but specific to the sending device (e.g., an
2922662Sstever@eecs.umich.edu     *    MSHR).  A pointer to this state is returned in the packet's
2932662Sstever@eecs.umich.edu     *    response so that the sender can quickly look up the state
2942662Sstever@eecs.umich.edu     *    needed to process it.  A specific subclass would be derived
2952662Sstever@eecs.umich.edu     *    from this to carry state specific to a particular sending
2962662Sstever@eecs.umich.edu     *    device.  */
2974610Ssaidi@eecs.umich.edu    class SenderState : public FastAlloc {
2982641Sstever@eecs.umich.edu      public:
2992641Sstever@eecs.umich.edu        virtual ~SenderState() {}
3002641Sstever@eecs.umich.edu    };
3012641Sstever@eecs.umich.edu
3025315Sstever@gmail.com    /**
3035315Sstever@gmail.com     * Object used to maintain state of a PrintReq.  The senderState
3045315Sstever@gmail.com     * field of a PrintReq should always be of this type.
3055315Sstever@gmail.com     */
3065314Sstever@gmail.com    class PrintReqState : public SenderState {
3075315Sstever@gmail.com        /** An entry in the label stack. */
3085314Sstever@gmail.com        class LabelStackEntry {
3095314Sstever@gmail.com          public:
3105314Sstever@gmail.com            const std::string label;
3115314Sstever@gmail.com            std::string *prefix;
3125314Sstever@gmail.com            bool labelPrinted;
3135314Sstever@gmail.com            LabelStackEntry(const std::string &_label,
3145314Sstever@gmail.com                            std::string *_prefix);
3155314Sstever@gmail.com        };
3165314Sstever@gmail.com
3175314Sstever@gmail.com        typedef std::list<LabelStackEntry> LabelStack;
3185314Sstever@gmail.com        LabelStack labelStack;
3195314Sstever@gmail.com
3205314Sstever@gmail.com        std::string *curPrefixPtr;
3215314Sstever@gmail.com
3225314Sstever@gmail.com      public:
3235314Sstever@gmail.com        std::ostream &os;
3245314Sstever@gmail.com        const int verbosity;
3255314Sstever@gmail.com
3265314Sstever@gmail.com        PrintReqState(std::ostream &os, int verbosity = 0);
3275314Sstever@gmail.com        ~PrintReqState();
3285314Sstever@gmail.com
3295315Sstever@gmail.com        /** Returns the current line prefix. */
3305314Sstever@gmail.com        const std::string &curPrefix() { return *curPrefixPtr; }
3315315Sstever@gmail.com
3325315Sstever@gmail.com        /** Push a label onto the label stack, and prepend the given
3335315Sstever@gmail.com         * prefix string onto the current prefix.  Labels will only be
3345315Sstever@gmail.com         * printed if an object within the label's scope is
3355315Sstever@gmail.com         * printed. */
3365314Sstever@gmail.com        void pushLabel(const std::string &lbl,
3375314Sstever@gmail.com                       const std::string &prefix = "  ");
3385315Sstever@gmail.com        /** Pop a label off the label stack. */
3395314Sstever@gmail.com        void popLabel();
3405315Sstever@gmail.com        /** Print all of the pending unprinted labels on the
3415315Sstever@gmail.com         * stack. Called by printObj(), so normally not called by
3425315Sstever@gmail.com         * users unless bypassing printObj(). */
3435314Sstever@gmail.com        void printLabels();
3445315Sstever@gmail.com        /** Print a Printable object to os, because it matched the
3455315Sstever@gmail.com         * address on a PrintReq. */
3465314Sstever@gmail.com        void printObj(Printable *obj);
3475314Sstever@gmail.com    };
3485314Sstever@gmail.com
3492662Sstever@eecs.umich.edu    /** This packet's sender state.  Devices should use dynamic_cast<>
3502662Sstever@eecs.umich.edu     *   to cast to the state appropriate to the sender. */
3512662Sstever@eecs.umich.edu    SenderState *senderState;
3522641Sstever@eecs.umich.edu
3532662Sstever@eecs.umich.edu    /** Return the string name of the cmd field (for debugging and
3542662Sstever@eecs.umich.edu     *   tracing). */
3554022Sstever@eecs.umich.edu    const std::string &cmdString() const { return cmd.toString(); }
3562811Srdreslin@umich.edu
3572811Srdreslin@umich.edu    /** Return the index of this command. */
3584022Sstever@eecs.umich.edu    inline int cmdToIndex() const { return cmd.toInt(); }
3592811Srdreslin@umich.edu
3604022Sstever@eecs.umich.edu    bool isRead() const         { return cmd.isRead(); }
3614022Sstever@eecs.umich.edu    bool isWrite()  const       { return cmd.isWrite(); }
3624022Sstever@eecs.umich.edu    bool isRequest() const      { return cmd.isRequest(); }
3634022Sstever@eecs.umich.edu    bool isResponse() const     { return cmd.isResponse(); }
3644870Sstever@eecs.umich.edu    bool needsExclusive() const { return cmd.needsExclusive(); }
3654022Sstever@eecs.umich.edu    bool needsResponse() const  { return cmd.needsResponse(); }
3664022Sstever@eecs.umich.edu    bool isInvalidate() const   { return cmd.isInvalidate(); }
3674022Sstever@eecs.umich.edu    bool hasData() const        { return cmd.hasData(); }
3684040Ssaidi@eecs.umich.edu    bool isReadWrite() const    { return cmd.isReadWrite(); }
3694626Sstever@eecs.umich.edu    bool isLocked() const       { return cmd.isLocked(); }
3704870Sstever@eecs.umich.edu    bool isError() const        { return cmd.isError(); }
3715314Sstever@gmail.com    bool isPrint() const        { return cmd.isPrint(); }
3722812Srdreslin@umich.edu
3734870Sstever@eecs.umich.edu    // Snoop flags
3744870Sstever@eecs.umich.edu    void assertMemInhibit()     { flags[MemInhibit] = true; }
3754916Sstever@eecs.umich.edu    bool memInhibitAsserted()   { return flags[MemInhibit]; }
3764870Sstever@eecs.umich.edu    void assertShared()         { flags[Shared] = true; }
3774870Sstever@eecs.umich.edu    bool sharedAsserted()       { return flags[Shared]; }
3784870Sstever@eecs.umich.edu
3794895Sstever@eecs.umich.edu    // Special control flags
3804895Sstever@eecs.umich.edu    void setExpressSnoop()      { flags[ExpressSnoop] = true; }
3814895Sstever@eecs.umich.edu    bool isExpressSnoop()       { return flags[ExpressSnoop]; }
3824916Sstever@eecs.umich.edu    void setSupplyExclusive()   { flags[SupplyExclusive] = true; }
3834916Sstever@eecs.umich.edu    bool isSupplyExclusive()    { return flags[SupplyExclusive]; }
3844895Sstever@eecs.umich.edu
3854870Sstever@eecs.umich.edu    // Network error conditions... encapsulate them as methods since
3864870Sstever@eecs.umich.edu    // their encoding keeps changing (from result field to command
3874870Sstever@eecs.umich.edu    // field, etc.)
3884986Ssaidi@eecs.umich.edu    void setNacked()     { assert(isResponse()); cmd = MemCmd::NetworkNackError; }
3894986Ssaidi@eecs.umich.edu    void setBadAddress() { assert(isResponse()); cmd = MemCmd::BadAddressError; }
3904870Sstever@eecs.umich.edu    bool wasNacked()     { return cmd == MemCmd::NetworkNackError; }
3914870Sstever@eecs.umich.edu    bool hadBadAddress() { return cmd == MemCmd::BadAddressError; }
3924986Ssaidi@eecs.umich.edu    void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
3932814Srdreslin@umich.edu
3943940Ssaidi@eecs.umich.edu    bool nic_pkt() { panic("Unimplemented"); M5_DUMMY_RETURN }
3952641Sstever@eecs.umich.edu
3962381SN/A    /** Accessor function that returns the source index of the packet. */
3974870Sstever@eecs.umich.edu    short getSrc() const    { assert(srcValid); return src; }
3982641Sstever@eecs.umich.edu    void setSrc(short _src) { src = _src; srcValid = true; }
3994626Sstever@eecs.umich.edu    /** Reset source field, e.g. to retransmit packet on different bus. */
4004626Sstever@eecs.umich.edu    void clearSrc() { srcValid = false; }
4012381SN/A
4022381SN/A    /** Accessor function that returns the destination index of
4032381SN/A        the packet. */
4044870Sstever@eecs.umich.edu    short getDest() const     { assert(destValid); return dest; }
4054870Sstever@eecs.umich.edu    void setDest(short _dest) { dest = _dest; destValid = true; }
4062549SN/A
4072663Sstever@eecs.umich.edu    Addr getAddr() const { assert(addrSizeValid); return addr; }
4084870Sstever@eecs.umich.edu    int getSize() const  { assert(addrSizeValid); return size; }
4092883Srdreslin@umich.edu    Addr getOffset(int blkSize) const { return addr & (Addr)(blkSize - 1); }
4102813Srdreslin@umich.edu
4112662Sstever@eecs.umich.edu    /** Constructor.  Note that a Request object must be constructed
4122662Sstever@eecs.umich.edu     *   first, but the Requests's physical address and size fields
4132662Sstever@eecs.umich.edu     *   need not be valid. The command and destination addresses
4142662Sstever@eecs.umich.edu     *   must be supplied.  */
4154022Sstever@eecs.umich.edu    Packet(Request *_req, MemCmd _cmd, short _dest)
4164870Sstever@eecs.umich.edu        :  cmd(_cmd), req(_req),
4174870Sstever@eecs.umich.edu           data(NULL), staticData(false), dynamicData(false), arrayData(false),
4182641Sstever@eecs.umich.edu           addr(_req->paddr), size(_req->size), dest(_dest),
4194870Sstever@eecs.umich.edu           addrSizeValid(_req->validPaddr), srcValid(false), destValid(true),
4204870Sstever@eecs.umich.edu           flags(0), time(curTick), senderState(NULL)
4212641Sstever@eecs.umich.edu    {
4222813Srdreslin@umich.edu    }
4232813Srdreslin@umich.edu
4242813Srdreslin@umich.edu    /** Alternate constructor if you are trying to create a packet with
4252813Srdreslin@umich.edu     *  a request that is for a whole block, not the address from the req.
4262813Srdreslin@umich.edu     *  this allows for overriding the size/addr of the req.*/
4274022Sstever@eecs.umich.edu    Packet(Request *_req, MemCmd _cmd, short _dest, int _blkSize)
4284870Sstever@eecs.umich.edu        :  cmd(_cmd), req(_req),
4294870Sstever@eecs.umich.edu           data(NULL), staticData(false), dynamicData(false), arrayData(false),
4304626Sstever@eecs.umich.edu           addr(_req->paddr & ~(_blkSize - 1)), size(_blkSize), dest(_dest),
4314870Sstever@eecs.umich.edu           addrSizeValid(_req->validPaddr), srcValid(false), destValid(true),
4324870Sstever@eecs.umich.edu           flags(0), time(curTick), senderState(NULL)
4332813Srdreslin@umich.edu    {
4344626Sstever@eecs.umich.edu    }
4354626Sstever@eecs.umich.edu
4364626Sstever@eecs.umich.edu    /** Alternate constructor for copying a packet.  Copy all fields
4374887Sstever@eecs.umich.edu     * *except* if the original packet's data was dynamic, don't copy
4384887Sstever@eecs.umich.edu     * that, as we can't guarantee that the new packet's lifetime is
4394887Sstever@eecs.umich.edu     * less than that of the original packet.  In this case the new
4404887Sstever@eecs.umich.edu     * packet should allocate its own data. */
4414895Sstever@eecs.umich.edu    Packet(Packet *origPkt, bool clearFlags = false)
4424870Sstever@eecs.umich.edu        :  cmd(origPkt->cmd), req(origPkt->req),
4434887Sstever@eecs.umich.edu           data(origPkt->staticData ? origPkt->data : NULL),
4444887Sstever@eecs.umich.edu           staticData(origPkt->staticData),
4454887Sstever@eecs.umich.edu           dynamicData(false), arrayData(false),
4464626Sstever@eecs.umich.edu           addr(origPkt->addr), size(origPkt->size),
4474660Sstever@eecs.umich.edu           src(origPkt->src), dest(origPkt->dest),
4484870Sstever@eecs.umich.edu           addrSizeValid(origPkt->addrSizeValid),
4494870Sstever@eecs.umich.edu           srcValid(origPkt->srcValid), destValid(origPkt->destValid),
4504895Sstever@eecs.umich.edu           flags(clearFlags ? 0 : origPkt->flags),
4514870Sstever@eecs.umich.edu           time(curTick), senderState(origPkt->senderState)
4524626Sstever@eecs.umich.edu    {
4532641Sstever@eecs.umich.edu    }
4542549SN/A
4552662Sstever@eecs.umich.edu    /** Destructor. */
4562566SN/A    ~Packet()
4573665Srdreslin@umich.edu    { if (staticData || dynamicData) deleteData(); }
4582566SN/A
4592662Sstever@eecs.umich.edu    /** Reinitialize packet address and size from the associated
4602662Sstever@eecs.umich.edu     *   Request object, and reset other fields that may have been
4612662Sstever@eecs.umich.edu     *   modified by a previous transaction.  Typically called when a
4622662Sstever@eecs.umich.edu     *   statically allocated Request/Packet pair is reused for
4632662Sstever@eecs.umich.edu     *   multiple transactions. */
4642662Sstever@eecs.umich.edu    void reinitFromRequest() {
4652662Sstever@eecs.umich.edu        assert(req->validPaddr);
4664870Sstever@eecs.umich.edu        flags = 0;
4672663Sstever@eecs.umich.edu        addr = req->paddr;
4682663Sstever@eecs.umich.edu        size = req->size;
4693018Srdreslin@umich.edu        time = req->time;
4702663Sstever@eecs.umich.edu        addrSizeValid = true;
4712662Sstever@eecs.umich.edu        if (dynamicData) {
4722662Sstever@eecs.umich.edu            deleteData();
4732662Sstever@eecs.umich.edu            dynamicData = false;
4742662Sstever@eecs.umich.edu            arrayData = false;
4752662Sstever@eecs.umich.edu        }
4762662Sstever@eecs.umich.edu    }
4772566SN/A
4784626Sstever@eecs.umich.edu    /**
4794626Sstever@eecs.umich.edu     * Take a request packet and modify it in place to be suitable for
4804626Sstever@eecs.umich.edu     * returning as a response to that request.  The source and
4814626Sstever@eecs.umich.edu     * destination fields are *not* modified, as is appropriate for
4824626Sstever@eecs.umich.edu     * atomic accesses.
4834626Sstever@eecs.umich.edu     */
4844870Sstever@eecs.umich.edu    void makeResponse()
4854626Sstever@eecs.umich.edu    {
4862662Sstever@eecs.umich.edu        assert(needsResponse());
4872855Srdreslin@umich.edu        assert(isRequest());
4884986Ssaidi@eecs.umich.edu        origCmd = cmd;
4894022Sstever@eecs.umich.edu        cmd = cmd.responseCommand();
4904870Sstever@eecs.umich.edu        dest = src;
4914870Sstever@eecs.umich.edu        destValid = srcValid;
4922662Sstever@eecs.umich.edu        srcValid = false;
4932641Sstever@eecs.umich.edu    }
4942641Sstever@eecs.umich.edu
4954870Sstever@eecs.umich.edu    void makeAtomicResponse()
4964870Sstever@eecs.umich.edu    {
4974870Sstever@eecs.umich.edu        makeResponse();
4984870Sstever@eecs.umich.edu    }
4994870Sstever@eecs.umich.edu
5004626Sstever@eecs.umich.edu    void makeTimingResponse()
5013348Sbinkertn@umich.edu    {
5024870Sstever@eecs.umich.edu        makeResponse();
5033135Srdreslin@umich.edu    }
5043135Srdreslin@umich.edu
5053348Sbinkertn@umich.edu    /**
5063348Sbinkertn@umich.edu     * Take a request packet that has been returned as NACKED and
5073348Sbinkertn@umich.edu     * modify it so that it can be sent out again. Only packets that
5083348Sbinkertn@umich.edu     * need a response can be NACKED, so verify that that is true.
5093348Sbinkertn@umich.edu     */
5103348Sbinkertn@umich.edu    void
5113348Sbinkertn@umich.edu    reinitNacked()
5123348Sbinkertn@umich.edu    {
5134870Sstever@eecs.umich.edu        assert(wasNacked());
5144870Sstever@eecs.umich.edu        cmd = origCmd;
5154870Sstever@eecs.umich.edu        assert(needsResponse());
5164870Sstever@eecs.umich.edu        setDest(Broadcast);
5172685Ssaidi@eecs.umich.edu    }
5182685Ssaidi@eecs.umich.edu
5192685Ssaidi@eecs.umich.edu
5203348Sbinkertn@umich.edu    /**
5213348Sbinkertn@umich.edu     * Set the data pointer to the following value that should not be
5223348Sbinkertn@umich.edu     * freed.
5232566SN/A     */
5242566SN/A    template <typename T>
5253348Sbinkertn@umich.edu    void
5263348Sbinkertn@umich.edu    dataStatic(T *p)
5273348Sbinkertn@umich.edu    {
5283348Sbinkertn@umich.edu        if(dynamicData)
5293348Sbinkertn@umich.edu            dynamicData = false;
5303348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
5313348Sbinkertn@umich.edu        staticData = true;
5323348Sbinkertn@umich.edu    }
5332566SN/A
5343348Sbinkertn@umich.edu    /**
5353348Sbinkertn@umich.edu     * Set the data pointer to a value that should have delete []
5363348Sbinkertn@umich.edu     * called on it.
5373348Sbinkertn@umich.edu     */
5382566SN/A    template <typename T>
5393348Sbinkertn@umich.edu    void
5403348Sbinkertn@umich.edu    dataDynamicArray(T *p)
5413348Sbinkertn@umich.edu    {
5423348Sbinkertn@umich.edu        assert(!staticData && !dynamicData);
5433348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
5443348Sbinkertn@umich.edu        dynamicData = true;
5453348Sbinkertn@umich.edu        arrayData = true;
5463348Sbinkertn@umich.edu    }
5473348Sbinkertn@umich.edu
5483348Sbinkertn@umich.edu    /**
5493348Sbinkertn@umich.edu     * set the data pointer to a value that should have delete called
5503348Sbinkertn@umich.edu     * on it.
5513348Sbinkertn@umich.edu     */
5523348Sbinkertn@umich.edu    template <typename T>
5533348Sbinkertn@umich.edu    void
5543348Sbinkertn@umich.edu    dataDynamic(T *p)
5553348Sbinkertn@umich.edu    {
5563348Sbinkertn@umich.edu        assert(!staticData && !dynamicData);
5573348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
5583348Sbinkertn@umich.edu        dynamicData = true;
5593348Sbinkertn@umich.edu        arrayData = false;
5603348Sbinkertn@umich.edu    }
5613348Sbinkertn@umich.edu
5623348Sbinkertn@umich.edu    /** get a pointer to the data ptr. */
5633348Sbinkertn@umich.edu    template <typename T>
5643348Sbinkertn@umich.edu    T*
5653348Sbinkertn@umich.edu    getPtr()
5663348Sbinkertn@umich.edu    {
5673348Sbinkertn@umich.edu        assert(staticData || dynamicData);
5683348Sbinkertn@umich.edu        return (T*)data;
5693348Sbinkertn@umich.edu    }
5702566SN/A
5712566SN/A    /** return the value of what is pointed to in the packet. */
5722566SN/A    template <typename T>
5732592SN/A    T get();
5742566SN/A
5752566SN/A    /** set the value in the data pointer to v. */
5762566SN/A    template <typename T>
5772592SN/A    void set(T v);
5782566SN/A
5793348Sbinkertn@umich.edu    /**
5804626Sstever@eecs.umich.edu     * Copy data into the packet from the provided pointer.
5814626Sstever@eecs.umich.edu     */
5824626Sstever@eecs.umich.edu    void setData(uint8_t *p)
5834626Sstever@eecs.umich.edu    {
5844626Sstever@eecs.umich.edu        std::memcpy(getPtr<uint8_t>(), p, getSize());
5854626Sstever@eecs.umich.edu    }
5864626Sstever@eecs.umich.edu
5874626Sstever@eecs.umich.edu    /**
5884626Sstever@eecs.umich.edu     * Copy data into the packet from the provided block pointer,
5894626Sstever@eecs.umich.edu     * which is aligned to the given block size.
5904626Sstever@eecs.umich.edu     */
5914626Sstever@eecs.umich.edu    void setDataFromBlock(uint8_t *blk_data, int blkSize)
5924626Sstever@eecs.umich.edu    {
5934626Sstever@eecs.umich.edu        setData(blk_data + getOffset(blkSize));
5944626Sstever@eecs.umich.edu    }
5954626Sstever@eecs.umich.edu
5964626Sstever@eecs.umich.edu    /**
5974626Sstever@eecs.umich.edu     * Copy data from the packet to the provided block pointer, which
5984626Sstever@eecs.umich.edu     * is aligned to the given block size.
5994626Sstever@eecs.umich.edu     */
6004626Sstever@eecs.umich.edu    void writeData(uint8_t *p)
6014626Sstever@eecs.umich.edu    {
6024626Sstever@eecs.umich.edu        std::memcpy(p, getPtr<uint8_t>(), getSize());
6034626Sstever@eecs.umich.edu    }
6044626Sstever@eecs.umich.edu
6054626Sstever@eecs.umich.edu    /**
6064626Sstever@eecs.umich.edu     * Copy data from the packet to the memory at the provided pointer.
6074626Sstever@eecs.umich.edu     */
6084626Sstever@eecs.umich.edu    void writeDataToBlock(uint8_t *blk_data, int blkSize)
6094626Sstever@eecs.umich.edu    {
6104626Sstever@eecs.umich.edu        writeData(blk_data + getOffset(blkSize));
6114626Sstever@eecs.umich.edu    }
6124626Sstever@eecs.umich.edu
6134626Sstever@eecs.umich.edu    /**
6143348Sbinkertn@umich.edu     * delete the data pointed to in the data pointer. Ok to call to
6153348Sbinkertn@umich.edu     * matter how data was allocted.
6163348Sbinkertn@umich.edu     */
6172592SN/A    void deleteData();
6182566SN/A
6192566SN/A    /** If there isn't data in the packet, allocate some. */
6202592SN/A    void allocate();
6212568SN/A
6224626Sstever@eecs.umich.edu    /**
6234626Sstever@eecs.umich.edu     * Check a functional request against a memory value represented
6244626Sstever@eecs.umich.edu     * by a base/size pair and an associated data array.  If the
6254626Sstever@eecs.umich.edu     * functional request is a read, it may be satisfied by the memory
6264626Sstever@eecs.umich.edu     * value.  If the functional request is a write, it may update the
6274626Sstever@eecs.umich.edu     * memory value.
6284626Sstever@eecs.umich.edu     */
6295314Sstever@gmail.com    bool checkFunctional(Printable *obj, Addr base, int size, uint8_t *data);
6304626Sstever@eecs.umich.edu
6314626Sstever@eecs.umich.edu    /**
6324626Sstever@eecs.umich.edu     * Check a functional request against a memory value stored in
6335315Sstever@gmail.com     * another packet (i.e. an in-transit request or response).
6344626Sstever@eecs.umich.edu     */
6354626Sstever@eecs.umich.edu    bool checkFunctional(PacketPtr otherPkt) {
6365314Sstever@gmail.com        return checkFunctional(otherPkt,
6375314Sstever@gmail.com                               otherPkt->getAddr(), otherPkt->getSize(),
6385314Sstever@gmail.com                               otherPkt->hasData() ?
6395314Sstever@gmail.com                                   otherPkt->getPtr<uint8_t>() : NULL);
6404626Sstever@eecs.umich.edu    }
6415314Sstever@gmail.com
6425315Sstever@gmail.com    /**
6435315Sstever@gmail.com     * Push label for PrintReq (safe to call unconditionally).
6445315Sstever@gmail.com     */
6455314Sstever@gmail.com    void pushLabel(const std::string &lbl) {
6465314Sstever@gmail.com        if (isPrint()) {
6475314Sstever@gmail.com            dynamic_cast<PrintReqState*>(senderState)->pushLabel(lbl);
6485314Sstever@gmail.com        }
6495314Sstever@gmail.com    }
6505314Sstever@gmail.com
6515315Sstever@gmail.com    /**
6525315Sstever@gmail.com     * Pop label for PrintReq (safe to call unconditionally).
6535315Sstever@gmail.com     */
6545314Sstever@gmail.com    void popLabel() {
6555314Sstever@gmail.com        if (isPrint()) {
6565314Sstever@gmail.com            dynamic_cast<PrintReqState*>(senderState)->popLabel();
6575314Sstever@gmail.com        }
6585314Sstever@gmail.com    }
6595314Sstever@gmail.com
6605314Sstever@gmail.com    void print(std::ostream &o, int verbosity = 0,
6615314Sstever@gmail.com               const std::string &prefix = "") const;
6622381SN/A};
6632381SN/A
6642381SN/A#endif //__MEM_PACKET_HH
665