packet.hh revision 5314
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
3025314Sstever@gmail.com    class PrintReqState : public SenderState {
3035314Sstever@gmail.com        class LabelStackEntry {
3045314Sstever@gmail.com          public:
3055314Sstever@gmail.com            const std::string label;
3065314Sstever@gmail.com            std::string *prefix;
3075314Sstever@gmail.com            bool labelPrinted;
3085314Sstever@gmail.com            LabelStackEntry(const std::string &_label,
3095314Sstever@gmail.com                            std::string *_prefix);
3105314Sstever@gmail.com        };
3115314Sstever@gmail.com
3125314Sstever@gmail.com        typedef std::list<LabelStackEntry> LabelStack;
3135314Sstever@gmail.com        LabelStack labelStack;
3145314Sstever@gmail.com
3155314Sstever@gmail.com        std::string *curPrefixPtr;
3165314Sstever@gmail.com
3175314Sstever@gmail.com      public:
3185314Sstever@gmail.com        std::ostream &os;
3195314Sstever@gmail.com        const int verbosity;
3205314Sstever@gmail.com
3215314Sstever@gmail.com        PrintReqState(std::ostream &os, int verbosity = 0);
3225314Sstever@gmail.com        ~PrintReqState();
3235314Sstever@gmail.com
3245314Sstever@gmail.com        const std::string &curPrefix() { return *curPrefixPtr; }
3255314Sstever@gmail.com        void pushLabel(const std::string &lbl,
3265314Sstever@gmail.com                       const std::string &prefix = "  ");
3275314Sstever@gmail.com        void popLabel();
3285314Sstever@gmail.com        void printLabels();
3295314Sstever@gmail.com        void printObj(Printable *obj);
3305314Sstever@gmail.com    };
3315314Sstever@gmail.com
3322662Sstever@eecs.umich.edu    /** This packet's sender state.  Devices should use dynamic_cast<>
3332662Sstever@eecs.umich.edu     *   to cast to the state appropriate to the sender. */
3342662Sstever@eecs.umich.edu    SenderState *senderState;
3352641Sstever@eecs.umich.edu
3362662Sstever@eecs.umich.edu    /** Return the string name of the cmd field (for debugging and
3372662Sstever@eecs.umich.edu     *   tracing). */
3384022Sstever@eecs.umich.edu    const std::string &cmdString() const { return cmd.toString(); }
3392811Srdreslin@umich.edu
3402811Srdreslin@umich.edu    /** Return the index of this command. */
3414022Sstever@eecs.umich.edu    inline int cmdToIndex() const { return cmd.toInt(); }
3422811Srdreslin@umich.edu
3434022Sstever@eecs.umich.edu    bool isRead() const         { return cmd.isRead(); }
3444022Sstever@eecs.umich.edu    bool isWrite()  const       { return cmd.isWrite(); }
3454022Sstever@eecs.umich.edu    bool isRequest() const      { return cmd.isRequest(); }
3464022Sstever@eecs.umich.edu    bool isResponse() const     { return cmd.isResponse(); }
3474870Sstever@eecs.umich.edu    bool needsExclusive() const { return cmd.needsExclusive(); }
3484022Sstever@eecs.umich.edu    bool needsResponse() const  { return cmd.needsResponse(); }
3494022Sstever@eecs.umich.edu    bool isInvalidate() const   { return cmd.isInvalidate(); }
3504022Sstever@eecs.umich.edu    bool hasData() const        { return cmd.hasData(); }
3514040Ssaidi@eecs.umich.edu    bool isReadWrite() const    { return cmd.isReadWrite(); }
3524626Sstever@eecs.umich.edu    bool isLocked() const       { return cmd.isLocked(); }
3534870Sstever@eecs.umich.edu    bool isError() const        { return cmd.isError(); }
3545314Sstever@gmail.com    bool isPrint() const        { return cmd.isPrint(); }
3552812Srdreslin@umich.edu
3564870Sstever@eecs.umich.edu    // Snoop flags
3574870Sstever@eecs.umich.edu    void assertMemInhibit()     { flags[MemInhibit] = true; }
3584916Sstever@eecs.umich.edu    bool memInhibitAsserted()   { return flags[MemInhibit]; }
3594870Sstever@eecs.umich.edu    void assertShared()         { flags[Shared] = true; }
3604870Sstever@eecs.umich.edu    bool sharedAsserted()       { return flags[Shared]; }
3614870Sstever@eecs.umich.edu
3624895Sstever@eecs.umich.edu    // Special control flags
3634895Sstever@eecs.umich.edu    void setExpressSnoop()      { flags[ExpressSnoop] = true; }
3644895Sstever@eecs.umich.edu    bool isExpressSnoop()       { return flags[ExpressSnoop]; }
3654916Sstever@eecs.umich.edu    void setSupplyExclusive()   { flags[SupplyExclusive] = true; }
3664916Sstever@eecs.umich.edu    bool isSupplyExclusive()    { return flags[SupplyExclusive]; }
3674895Sstever@eecs.umich.edu
3684870Sstever@eecs.umich.edu    // Network error conditions... encapsulate them as methods since
3694870Sstever@eecs.umich.edu    // their encoding keeps changing (from result field to command
3704870Sstever@eecs.umich.edu    // field, etc.)
3714986Ssaidi@eecs.umich.edu    void setNacked()     { assert(isResponse()); cmd = MemCmd::NetworkNackError; }
3724986Ssaidi@eecs.umich.edu    void setBadAddress() { assert(isResponse()); cmd = MemCmd::BadAddressError; }
3734870Sstever@eecs.umich.edu    bool wasNacked()     { return cmd == MemCmd::NetworkNackError; }
3744870Sstever@eecs.umich.edu    bool hadBadAddress() { return cmd == MemCmd::BadAddressError; }
3754986Ssaidi@eecs.umich.edu    void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
3762814Srdreslin@umich.edu
3773940Ssaidi@eecs.umich.edu    bool nic_pkt() { panic("Unimplemented"); M5_DUMMY_RETURN }
3782641Sstever@eecs.umich.edu
3792381SN/A    /** Accessor function that returns the source index of the packet. */
3804870Sstever@eecs.umich.edu    short getSrc() const    { assert(srcValid); return src; }
3812641Sstever@eecs.umich.edu    void setSrc(short _src) { src = _src; srcValid = true; }
3824626Sstever@eecs.umich.edu    /** Reset source field, e.g. to retransmit packet on different bus. */
3834626Sstever@eecs.umich.edu    void clearSrc() { srcValid = false; }
3842381SN/A
3852381SN/A    /** Accessor function that returns the destination index of
3862381SN/A        the packet. */
3874870Sstever@eecs.umich.edu    short getDest() const     { assert(destValid); return dest; }
3884870Sstever@eecs.umich.edu    void setDest(short _dest) { dest = _dest; destValid = true; }
3892549SN/A
3902663Sstever@eecs.umich.edu    Addr getAddr() const { assert(addrSizeValid); return addr; }
3914870Sstever@eecs.umich.edu    int getSize() const  { assert(addrSizeValid); return size; }
3922883Srdreslin@umich.edu    Addr getOffset(int blkSize) const { return addr & (Addr)(blkSize - 1); }
3932813Srdreslin@umich.edu
3942662Sstever@eecs.umich.edu    /** Constructor.  Note that a Request object must be constructed
3952662Sstever@eecs.umich.edu     *   first, but the Requests's physical address and size fields
3962662Sstever@eecs.umich.edu     *   need not be valid. The command and destination addresses
3972662Sstever@eecs.umich.edu     *   must be supplied.  */
3984022Sstever@eecs.umich.edu    Packet(Request *_req, MemCmd _cmd, short _dest)
3994870Sstever@eecs.umich.edu        :  cmd(_cmd), req(_req),
4004870Sstever@eecs.umich.edu           data(NULL), staticData(false), dynamicData(false), arrayData(false),
4012641Sstever@eecs.umich.edu           addr(_req->paddr), size(_req->size), dest(_dest),
4024870Sstever@eecs.umich.edu           addrSizeValid(_req->validPaddr), srcValid(false), destValid(true),
4034870Sstever@eecs.umich.edu           flags(0), time(curTick), senderState(NULL)
4042641Sstever@eecs.umich.edu    {
4052813Srdreslin@umich.edu    }
4062813Srdreslin@umich.edu
4072813Srdreslin@umich.edu    /** Alternate constructor if you are trying to create a packet with
4082813Srdreslin@umich.edu     *  a request that is for a whole block, not the address from the req.
4092813Srdreslin@umich.edu     *  this allows for overriding the size/addr of the req.*/
4104022Sstever@eecs.umich.edu    Packet(Request *_req, MemCmd _cmd, short _dest, int _blkSize)
4114870Sstever@eecs.umich.edu        :  cmd(_cmd), req(_req),
4124870Sstever@eecs.umich.edu           data(NULL), staticData(false), dynamicData(false), arrayData(false),
4134626Sstever@eecs.umich.edu           addr(_req->paddr & ~(_blkSize - 1)), size(_blkSize), dest(_dest),
4144870Sstever@eecs.umich.edu           addrSizeValid(_req->validPaddr), srcValid(false), destValid(true),
4154870Sstever@eecs.umich.edu           flags(0), time(curTick), senderState(NULL)
4162813Srdreslin@umich.edu    {
4174626Sstever@eecs.umich.edu    }
4184626Sstever@eecs.umich.edu
4194626Sstever@eecs.umich.edu    /** Alternate constructor for copying a packet.  Copy all fields
4204887Sstever@eecs.umich.edu     * *except* if the original packet's data was dynamic, don't copy
4214887Sstever@eecs.umich.edu     * that, as we can't guarantee that the new packet's lifetime is
4224887Sstever@eecs.umich.edu     * less than that of the original packet.  In this case the new
4234887Sstever@eecs.umich.edu     * packet should allocate its own data. */
4244895Sstever@eecs.umich.edu    Packet(Packet *origPkt, bool clearFlags = false)
4254870Sstever@eecs.umich.edu        :  cmd(origPkt->cmd), req(origPkt->req),
4264887Sstever@eecs.umich.edu           data(origPkt->staticData ? origPkt->data : NULL),
4274887Sstever@eecs.umich.edu           staticData(origPkt->staticData),
4284887Sstever@eecs.umich.edu           dynamicData(false), arrayData(false),
4294626Sstever@eecs.umich.edu           addr(origPkt->addr), size(origPkt->size),
4304660Sstever@eecs.umich.edu           src(origPkt->src), dest(origPkt->dest),
4314870Sstever@eecs.umich.edu           addrSizeValid(origPkt->addrSizeValid),
4324870Sstever@eecs.umich.edu           srcValid(origPkt->srcValid), destValid(origPkt->destValid),
4334895Sstever@eecs.umich.edu           flags(clearFlags ? 0 : origPkt->flags),
4344870Sstever@eecs.umich.edu           time(curTick), senderState(origPkt->senderState)
4354626Sstever@eecs.umich.edu    {
4362641Sstever@eecs.umich.edu    }
4372549SN/A
4382662Sstever@eecs.umich.edu    /** Destructor. */
4392566SN/A    ~Packet()
4403665Srdreslin@umich.edu    { if (staticData || dynamicData) deleteData(); }
4412566SN/A
4422662Sstever@eecs.umich.edu    /** Reinitialize packet address and size from the associated
4432662Sstever@eecs.umich.edu     *   Request object, and reset other fields that may have been
4442662Sstever@eecs.umich.edu     *   modified by a previous transaction.  Typically called when a
4452662Sstever@eecs.umich.edu     *   statically allocated Request/Packet pair is reused for
4462662Sstever@eecs.umich.edu     *   multiple transactions. */
4472662Sstever@eecs.umich.edu    void reinitFromRequest() {
4482662Sstever@eecs.umich.edu        assert(req->validPaddr);
4494870Sstever@eecs.umich.edu        flags = 0;
4502663Sstever@eecs.umich.edu        addr = req->paddr;
4512663Sstever@eecs.umich.edu        size = req->size;
4523018Srdreslin@umich.edu        time = req->time;
4532663Sstever@eecs.umich.edu        addrSizeValid = true;
4542662Sstever@eecs.umich.edu        if (dynamicData) {
4552662Sstever@eecs.umich.edu            deleteData();
4562662Sstever@eecs.umich.edu            dynamicData = false;
4572662Sstever@eecs.umich.edu            arrayData = false;
4582662Sstever@eecs.umich.edu        }
4592662Sstever@eecs.umich.edu    }
4602566SN/A
4614626Sstever@eecs.umich.edu    /**
4624626Sstever@eecs.umich.edu     * Take a request packet and modify it in place to be suitable for
4634626Sstever@eecs.umich.edu     * returning as a response to that request.  The source and
4644626Sstever@eecs.umich.edu     * destination fields are *not* modified, as is appropriate for
4654626Sstever@eecs.umich.edu     * atomic accesses.
4664626Sstever@eecs.umich.edu     */
4674870Sstever@eecs.umich.edu    void makeResponse()
4684626Sstever@eecs.umich.edu    {
4692662Sstever@eecs.umich.edu        assert(needsResponse());
4702855Srdreslin@umich.edu        assert(isRequest());
4714986Ssaidi@eecs.umich.edu        origCmd = cmd;
4724022Sstever@eecs.umich.edu        cmd = cmd.responseCommand();
4734870Sstever@eecs.umich.edu        dest = src;
4744870Sstever@eecs.umich.edu        destValid = srcValid;
4752662Sstever@eecs.umich.edu        srcValid = false;
4762641Sstever@eecs.umich.edu    }
4772641Sstever@eecs.umich.edu
4784870Sstever@eecs.umich.edu    void makeAtomicResponse()
4794870Sstever@eecs.umich.edu    {
4804870Sstever@eecs.umich.edu        makeResponse();
4814870Sstever@eecs.umich.edu    }
4824870Sstever@eecs.umich.edu
4834626Sstever@eecs.umich.edu    void makeTimingResponse()
4843348Sbinkertn@umich.edu    {
4854870Sstever@eecs.umich.edu        makeResponse();
4863135Srdreslin@umich.edu    }
4873135Srdreslin@umich.edu
4883348Sbinkertn@umich.edu    /**
4893348Sbinkertn@umich.edu     * Take a request packet that has been returned as NACKED and
4903348Sbinkertn@umich.edu     * modify it so that it can be sent out again. Only packets that
4913348Sbinkertn@umich.edu     * need a response can be NACKED, so verify that that is true.
4923348Sbinkertn@umich.edu     */
4933348Sbinkertn@umich.edu    void
4943348Sbinkertn@umich.edu    reinitNacked()
4953348Sbinkertn@umich.edu    {
4964870Sstever@eecs.umich.edu        assert(wasNacked());
4974870Sstever@eecs.umich.edu        cmd = origCmd;
4984870Sstever@eecs.umich.edu        assert(needsResponse());
4994870Sstever@eecs.umich.edu        setDest(Broadcast);
5002685Ssaidi@eecs.umich.edu    }
5012685Ssaidi@eecs.umich.edu
5022685Ssaidi@eecs.umich.edu
5033348Sbinkertn@umich.edu    /**
5043348Sbinkertn@umich.edu     * Set the data pointer to the following value that should not be
5053348Sbinkertn@umich.edu     * freed.
5062566SN/A     */
5072566SN/A    template <typename T>
5083348Sbinkertn@umich.edu    void
5093348Sbinkertn@umich.edu    dataStatic(T *p)
5103348Sbinkertn@umich.edu    {
5113348Sbinkertn@umich.edu        if(dynamicData)
5123348Sbinkertn@umich.edu            dynamicData = false;
5133348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
5143348Sbinkertn@umich.edu        staticData = true;
5153348Sbinkertn@umich.edu    }
5162566SN/A
5173348Sbinkertn@umich.edu    /**
5183348Sbinkertn@umich.edu     * Set the data pointer to a value that should have delete []
5193348Sbinkertn@umich.edu     * called on it.
5203348Sbinkertn@umich.edu     */
5212566SN/A    template <typename T>
5223348Sbinkertn@umich.edu    void
5233348Sbinkertn@umich.edu    dataDynamicArray(T *p)
5243348Sbinkertn@umich.edu    {
5253348Sbinkertn@umich.edu        assert(!staticData && !dynamicData);
5263348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
5273348Sbinkertn@umich.edu        dynamicData = true;
5283348Sbinkertn@umich.edu        arrayData = true;
5293348Sbinkertn@umich.edu    }
5303348Sbinkertn@umich.edu
5313348Sbinkertn@umich.edu    /**
5323348Sbinkertn@umich.edu     * set the data pointer to a value that should have delete called
5333348Sbinkertn@umich.edu     * on it.
5343348Sbinkertn@umich.edu     */
5353348Sbinkertn@umich.edu    template <typename T>
5363348Sbinkertn@umich.edu    void
5373348Sbinkertn@umich.edu    dataDynamic(T *p)
5383348Sbinkertn@umich.edu    {
5393348Sbinkertn@umich.edu        assert(!staticData && !dynamicData);
5403348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
5413348Sbinkertn@umich.edu        dynamicData = true;
5423348Sbinkertn@umich.edu        arrayData = false;
5433348Sbinkertn@umich.edu    }
5443348Sbinkertn@umich.edu
5453348Sbinkertn@umich.edu    /** get a pointer to the data ptr. */
5463348Sbinkertn@umich.edu    template <typename T>
5473348Sbinkertn@umich.edu    T*
5483348Sbinkertn@umich.edu    getPtr()
5493348Sbinkertn@umich.edu    {
5503348Sbinkertn@umich.edu        assert(staticData || dynamicData);
5513348Sbinkertn@umich.edu        return (T*)data;
5523348Sbinkertn@umich.edu    }
5532566SN/A
5542566SN/A    /** return the value of what is pointed to in the packet. */
5552566SN/A    template <typename T>
5562592SN/A    T get();
5572566SN/A
5582566SN/A    /** set the value in the data pointer to v. */
5592566SN/A    template <typename T>
5602592SN/A    void set(T v);
5612566SN/A
5623348Sbinkertn@umich.edu    /**
5634626Sstever@eecs.umich.edu     * Copy data into the packet from the provided pointer.
5644626Sstever@eecs.umich.edu     */
5654626Sstever@eecs.umich.edu    void setData(uint8_t *p)
5664626Sstever@eecs.umich.edu    {
5674626Sstever@eecs.umich.edu        std::memcpy(getPtr<uint8_t>(), p, getSize());
5684626Sstever@eecs.umich.edu    }
5694626Sstever@eecs.umich.edu
5704626Sstever@eecs.umich.edu    /**
5714626Sstever@eecs.umich.edu     * Copy data into the packet from the provided block pointer,
5724626Sstever@eecs.umich.edu     * which is aligned to the given block size.
5734626Sstever@eecs.umich.edu     */
5744626Sstever@eecs.umich.edu    void setDataFromBlock(uint8_t *blk_data, int blkSize)
5754626Sstever@eecs.umich.edu    {
5764626Sstever@eecs.umich.edu        setData(blk_data + getOffset(blkSize));
5774626Sstever@eecs.umich.edu    }
5784626Sstever@eecs.umich.edu
5794626Sstever@eecs.umich.edu    /**
5804626Sstever@eecs.umich.edu     * Copy data from the packet to the provided block pointer, which
5814626Sstever@eecs.umich.edu     * is aligned to the given block size.
5824626Sstever@eecs.umich.edu     */
5834626Sstever@eecs.umich.edu    void writeData(uint8_t *p)
5844626Sstever@eecs.umich.edu    {
5854626Sstever@eecs.umich.edu        std::memcpy(p, getPtr<uint8_t>(), getSize());
5864626Sstever@eecs.umich.edu    }
5874626Sstever@eecs.umich.edu
5884626Sstever@eecs.umich.edu    /**
5894626Sstever@eecs.umich.edu     * Copy data from the packet to the memory at the provided pointer.
5904626Sstever@eecs.umich.edu     */
5914626Sstever@eecs.umich.edu    void writeDataToBlock(uint8_t *blk_data, int blkSize)
5924626Sstever@eecs.umich.edu    {
5934626Sstever@eecs.umich.edu        writeData(blk_data + getOffset(blkSize));
5944626Sstever@eecs.umich.edu    }
5954626Sstever@eecs.umich.edu
5964626Sstever@eecs.umich.edu    /**
5973348Sbinkertn@umich.edu     * delete the data pointed to in the data pointer. Ok to call to
5983348Sbinkertn@umich.edu     * matter how data was allocted.
5993348Sbinkertn@umich.edu     */
6002592SN/A    void deleteData();
6012566SN/A
6022566SN/A    /** If there isn't data in the packet, allocate some. */
6032592SN/A    void allocate();
6042568SN/A
6054626Sstever@eecs.umich.edu    /**
6064626Sstever@eecs.umich.edu     * Check a functional request against a memory value represented
6074626Sstever@eecs.umich.edu     * by a base/size pair and an associated data array.  If the
6084626Sstever@eecs.umich.edu     * functional request is a read, it may be satisfied by the memory
6094626Sstever@eecs.umich.edu     * value.  If the functional request is a write, it may update the
6104626Sstever@eecs.umich.edu     * memory value.
6114626Sstever@eecs.umich.edu     */
6125314Sstever@gmail.com    bool checkFunctional(Printable *obj, Addr base, int size, uint8_t *data);
6134626Sstever@eecs.umich.edu
6144626Sstever@eecs.umich.edu    /**
6154626Sstever@eecs.umich.edu     * Check a functional request against a memory value stored in
6165314Sstever@gmail.com     * another packet (i.e. an in-transit request or response).  If
6175314Sstever@gmail.com     * possible, the request will be satisfied and transformed
6185314Sstever@gmail.com     * in-place into a response (at which point no further checking
6195314Sstever@gmail.com     * need be done).
6205314Sstever@gmail.com     *
6215314Sstever@gmail.com     * @return True if the memory location addressed by the request
6225314Sstever@gmail.com     * overlaps with the location addressed by otherPkt.
6234626Sstever@eecs.umich.edu     */
6244626Sstever@eecs.umich.edu    bool checkFunctional(PacketPtr otherPkt) {
6255314Sstever@gmail.com        return checkFunctional(otherPkt,
6265314Sstever@gmail.com                               otherPkt->getAddr(), otherPkt->getSize(),
6275314Sstever@gmail.com                               otherPkt->hasData() ?
6285314Sstever@gmail.com                                   otherPkt->getPtr<uint8_t>() : NULL);
6294626Sstever@eecs.umich.edu    }
6305314Sstever@gmail.com
6315314Sstever@gmail.com    void pushLabel(const std::string &lbl) {
6325314Sstever@gmail.com        if (isPrint()) {
6335314Sstever@gmail.com            dynamic_cast<PrintReqState*>(senderState)->pushLabel(lbl);
6345314Sstever@gmail.com        }
6355314Sstever@gmail.com    }
6365314Sstever@gmail.com
6375314Sstever@gmail.com    void popLabel() {
6385314Sstever@gmail.com        if (isPrint()) {
6395314Sstever@gmail.com            dynamic_cast<PrintReqState*>(senderState)->popLabel();
6405314Sstever@gmail.com        }
6415314Sstever@gmail.com    }
6425314Sstever@gmail.com
6435314Sstever@gmail.com    void print(std::ostream &o, int verbosity = 0,
6445314Sstever@gmail.com               const std::string &prefix = "") const;
6452381SN/A};
6462381SN/A
6472381SN/A#endif //__MEM_PACKET_HH
648