packet.hh revision 13860
12810SN/A/*
28856Sandreas.hansson@arm.com * Copyright (c) 2012-2019 ARM Limited
38856Sandreas.hansson@arm.com * All rights reserved
48856Sandreas.hansson@arm.com *
58856Sandreas.hansson@arm.com * The license below extends only to copyright in the software and shall
68856Sandreas.hansson@arm.com * not be construed as granting a license to any other intellectual
78856Sandreas.hansson@arm.com * property including but not limited to intellectual property relating
88856Sandreas.hansson@arm.com * to a hardware implementation of the functionality of the software
98856Sandreas.hansson@arm.com * licensed hereunder.  You may use the software subject to the license
108856Sandreas.hansson@arm.com * terms below provided that you ensure that this notice is replicated
118856Sandreas.hansson@arm.com * unmodified and in its entirety in all distributions of the software,
128856Sandreas.hansson@arm.com * modified or unmodified, in source code or in binary form.
138856Sandreas.hansson@arm.com *
142810SN/A * Copyright (c) 2006 The Regents of The University of Michigan
152810SN/A * Copyright (c) 2010,2015 Advanced Micro Devices, Inc.
162810SN/A * All rights reserved.
172810SN/A *
182810SN/A * Redistribution and use in source and binary forms, with or without
192810SN/A * modification, are permitted provided that the following conditions are
202810SN/A * met: redistributions of source code must retain the above copyright
212810SN/A * notice, this list of conditions and the following disclaimer;
222810SN/A * redistributions in binary form must reproduce the above copyright
232810SN/A * notice, this list of conditions and the following disclaimer in the
242810SN/A * documentation and/or other materials provided with the distribution;
252810SN/A * neither the name of the copyright holders nor the names of its
262810SN/A * contributors may be used to endorse or promote products derived from
272810SN/A * this software without specific prior written permission.
282810SN/A *
292810SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
302810SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
312810SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
322810SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
332810SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
342810SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
352810SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
362810SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
372810SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
382810SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
392810SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
402810SN/A *
414458SN/A * Authors: Ron Dreslinski
424458SN/A *          Steve Reinhardt
432810SN/A *          Ali Saidi
442810SN/A *          Andreas Hansson
452810SN/A *          Nikos Nikoleris
462810SN/A */
472810SN/A
482810SN/A/**
492810SN/A * @file
502810SN/A * Declaration of the Packet class.
512810SN/A */
522810SN/A
537676Snate@binkert.org#ifndef __MEM_PACKET_HH__
547676Snate@binkert.org#define __MEM_PACKET_HH__
557676Snate@binkert.org
562810SN/A#include <bitset>
572810SN/A#include <cassert>
582825SN/A#include <list>
592810SN/A
602810SN/A#include "base/addr_range.hh"
616215Snate@binkert.org#include "base/cast.hh"
628232Snate@binkert.org#include "base/compiler.hh"
638232Snate@binkert.org#include "base/flags.hh"
645338Sstever@gmail.com#include "base/logging.hh"
652810SN/A#include "base/printable.hh"
662810SN/A#include "base/types.hh"
678229Snate@binkert.org#include "config/the_isa.hh"
684626SN/A#include "mem/request.hh"
695034SN/A#include "sim/core.hh"
702811SN/A
718786Sgblack@eecs.umich.educlass Packet;
724626SN/Atypedef Packet *PacketPtr;
738833Sdam.sunwoo@arm.comtypedef uint8_t* PacketDataPtr;
742810SN/Atypedef std::list<PacketPtr> PacketList;
753194SN/Atypedef uint64_t PacketId;
762810SN/A
772810SN/Aclass MemCmd
782810SN/A{
792810SN/A    friend class Packet;
802810SN/A
814628SN/A  public:
824628SN/A    /**
834628SN/A     * List of all commands associated with a packet.
844628SN/A     */
854628SN/A    enum Command
864628SN/A    {
874628SN/A        InvalidCmd,
884628SN/A        ReadReq,
898737Skoansin.tan@gmail.com        ReadResp,
904628SN/A        ReadRespWithInvalidate,
914628SN/A        WriteReq,
924628SN/A        WriteResp,
934628SN/A        WritebackDirty,
944628SN/A        WritebackClean,
954628SN/A        WriteClean,            // writes dirty data below without evicting
964628SN/A        CleanEvict,
974628SN/A        SoftPFReq,
984628SN/A        SoftPFExReq,
994628SN/A        HardPFReq,
1004628SN/A        SoftPFResp,
1014628SN/A        HardPFResp,
1024628SN/A        WriteLineReq,
1034628SN/A        UpgradeReq,
1044628SN/A        SCUpgradeReq,           // Special "weak" upgrade for StoreCond
1054628SN/A        UpgradeResp,
1064628SN/A        SCUpgradeFailReq,       // Failed SCUpgradeReq in MSHR (never sent)
1074628SN/A        UpgradeFailResp,        // Valid for SCUpgradeReq only
1084628SN/A        ReadExReq,
1094628SN/A        ReadExResp,
1108737Skoansin.tan@gmail.com        ReadCleanReq,
1114628SN/A        ReadSharedReq,
1128856Sandreas.hansson@arm.com        LoadLockedReq,
1138856Sandreas.hansson@arm.com        StoreCondReq,
1148856Sandreas.hansson@arm.com        StoreCondFailReq,       // Failed StoreCondReq in MSHR (never sent)
1158856Sandreas.hansson@arm.com        StoreCondResp,
1168856Sandreas.hansson@arm.com        SwapReq,
1178856Sandreas.hansson@arm.com        SwapResp,
1188856Sandreas.hansson@arm.com        MessageReq,
1198856Sandreas.hansson@arm.com        MessageResp,
1208856Sandreas.hansson@arm.com        MemFenceReq,
1218856Sandreas.hansson@arm.com        MemFenceResp,
1222810SN/A        CleanSharedReq,
1238856Sandreas.hansson@arm.com        CleanSharedResp,
1242844SN/A        CleanInvalidReq,
1258856Sandreas.hansson@arm.com        CleanInvalidResp,
1268856Sandreas.hansson@arm.com        // Error responses
1278856Sandreas.hansson@arm.com        // @TODO these should be classified as responses rather than
1288856Sandreas.hansson@arm.com        // requests; coding them as requests initially for backwards
1298856Sandreas.hansson@arm.com        // compatibility
1308856Sandreas.hansson@arm.com        InvalidDestError,  // packet dest field invalid
1318856Sandreas.hansson@arm.com        BadAddressError,   // memory address invalid
1328856Sandreas.hansson@arm.com        FunctionalReadError, // unable to fulfill functional read
1338856Sandreas.hansson@arm.com        FunctionalWriteError, // unable to fulfill functional write
1348856Sandreas.hansson@arm.com        // Fake simulator-only commands
1358856Sandreas.hansson@arm.com        PrintReq,       // Print state matching address
1368856Sandreas.hansson@arm.com        FlushReq,      //request for a cache flush
1378856Sandreas.hansson@arm.com        InvalidateReq,   // request for address to be invalidated
1388856Sandreas.hansson@arm.com        InvalidateResp,
1398856Sandreas.hansson@arm.com        NUM_MEM_CMDS
1402810SN/A    };
1413738SN/A
1424458SN/A  private:
1438856Sandreas.hansson@arm.com    /**
1448856Sandreas.hansson@arm.com     * List of command attributes.
1452810SN/A     */
1468856Sandreas.hansson@arm.com    enum Attribute
1478856Sandreas.hansson@arm.com    {
1488856Sandreas.hansson@arm.com        IsRead,         //!< Data flows from responder to requester
1498856Sandreas.hansson@arm.com        IsWrite,        //!< Data flows from requester to responder
1508856Sandreas.hansson@arm.com        IsUpgrade,
1518856Sandreas.hansson@arm.com        IsInvalidate,
1528856Sandreas.hansson@arm.com        IsClean,        //!< Cleans any existing dirty blocks
1533013SN/A        NeedsWritable,  //!< Requires writable copy to complete in-cache
1548856Sandreas.hansson@arm.com        IsRequest,      //!< Issued by requester
1558856Sandreas.hansson@arm.com        IsResponse,     //!< Issue by responder
1568856Sandreas.hansson@arm.com        NeedsResponse,  //!< Requester needs response from target
1578856Sandreas.hansson@arm.com        IsEviction,
1588856Sandreas.hansson@arm.com        IsSWPrefetch,
1598856Sandreas.hansson@arm.com        IsHWPrefetch,
1608856Sandreas.hansson@arm.com        IsLlsc,         //!< Alpha/MIPS LL or SC access
1618856Sandreas.hansson@arm.com        HasData,        //!< There is an associated payload
1628856Sandreas.hansson@arm.com        IsError,        //!< Error response
1638856Sandreas.hansson@arm.com        IsPrint,        //!< Print state matching address (for debugging)
1645314SN/A        IsFlush,        //!< Flush the address from caches
1652811SN/A        FromCache,      //!< Request originated from a caching agent
1668856Sandreas.hansson@arm.com        NUM_COMMAND_ATTRIBUTES
1678856Sandreas.hansson@arm.com    };
1682810SN/A
1692810SN/A    /**
1708856Sandreas.hansson@arm.com     * Structure that defines attributes and other data associated
1712810SN/A     * with a Command.
1722810SN/A     */
1738856Sandreas.hansson@arm.com    struct CommandInfo
1748856Sandreas.hansson@arm.com    {
1758856Sandreas.hansson@arm.com        /// Set of attribute flags.
1768856Sandreas.hansson@arm.com        const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
1778856Sandreas.hansson@arm.com        /// Corresponding response for requests; InvalidCmd if no
1788856Sandreas.hansson@arm.com        /// response is applicable.
1798856Sandreas.hansson@arm.com        const Command response;
1808856Sandreas.hansson@arm.com        /// String representation (for printing)
1813606SN/A        const std::string str;
1822810SN/A    };
1832810SN/A
1842897SN/A    /// Array to map Command enum to associated info.
1852897SN/A    static const CommandInfo commandInfo[];
1868856Sandreas.hansson@arm.com
1874458SN/A  private:
1888856Sandreas.hansson@arm.com
1898856Sandreas.hansson@arm.com    Command cmd;
1902811SN/A
1912810SN/A    bool
1928856Sandreas.hansson@arm.com    testCmdAttrib(MemCmd::Attribute attrib) const
1938856Sandreas.hansson@arm.com    {
1943338SN/A        return commandInfo[cmd].attributes[attrib] != 0;
1954626SN/A    }
1964626SN/A
1974626SN/A  public:
1984626SN/A
1994626SN/A    bool isRead() const            { return testCmdAttrib(IsRead); }
2004626SN/A    bool isWrite() const           { return testCmdAttrib(IsWrite); }
2014626SN/A    bool isUpgrade() const         { return testCmdAttrib(IsUpgrade); }
2024626SN/A    bool isRequest() const         { return testCmdAttrib(IsRequest); }
2034628SN/A    bool isResponse() const        { return testCmdAttrib(IsResponse); }
2044628SN/A    bool needsWritable() const     { return testCmdAttrib(NeedsWritable); }
2054628SN/A    bool needsResponse() const     { return testCmdAttrib(NeedsResponse); }
2064666SN/A    bool isInvalidate() const      { return testCmdAttrib(IsInvalidate); }
2074628SN/A    bool isEviction() const        { return testCmdAttrib(IsEviction); }
2084628SN/A    bool isClean() const           { return testCmdAttrib(IsClean); }
2094628SN/A    bool fromCache() const         { return testCmdAttrib(FromCache); }
2104628SN/A
2114628SN/A    /**
2124628SN/A     * A writeback is an eviction that carries data.
2134628SN/A     */
2144628SN/A    bool isWriteback() const       { return testCmdAttrib(IsEviction) &&
2154628SN/A                                            testCmdAttrib(HasData); }
2164628SN/A
2174628SN/A    /**
2184628SN/A     * Check if this particular packet type carries payload data. Note
2197667Ssteve.reinhardt@amd.com     * that this does not reflect if the data pointer of the packet is
2204628SN/A     * valid or not.
2214628SN/A     */
2224628SN/A    bool hasData() const        { return testCmdAttrib(HasData); }
2237667Ssteve.reinhardt@amd.com    bool isLLSC() const         { return testCmdAttrib(IsLlsc); }
2244628SN/A    bool isSWPrefetch() const   { return testCmdAttrib(IsSWPrefetch); }
2254628SN/A    bool isHWPrefetch() const   { return testCmdAttrib(IsHWPrefetch); }
2264628SN/A    bool isPrefetch() const     { return testCmdAttrib(IsSWPrefetch) ||
2274628SN/A                                         testCmdAttrib(IsHWPrefetch); }
2284628SN/A    bool isError() const        { return testCmdAttrib(IsError); }
2294626SN/A    bool isPrint() const        { return testCmdAttrib(IsPrint); }
2306227Snate@binkert.org    bool isFlush() const        { return testCmdAttrib(IsFlush); }
2314626SN/A
2324630SN/A    Command
2334630SN/A    responseCommand() const
2344630SN/A    {
2354630SN/A        return commandInfo[cmd].response;
2364630SN/A    }
2374626SN/A
2384626SN/A    /// Return the string to a cmd given by idx.
2394626SN/A    const std::string &toString() const { return commandInfo[cmd].str; }
2406122SSteve.Reinhardt@amd.com    int toInt() const { return (int)cmd; }
2416122SSteve.Reinhardt@amd.com
2424626SN/A    MemCmd(Command _cmd) : cmd(_cmd) { }
2438134SAli.Saidi@ARM.com    MemCmd(int _cmd) : cmd((Command)_cmd) { }
2448134SAli.Saidi@ARM.com    MemCmd() : cmd(InvalidCmd) { }
2458134SAli.Saidi@ARM.com
2468134SAli.Saidi@ARM.com    bool operator==(MemCmd c2) const { return (cmd == c2.cmd); }
2478134SAli.Saidi@ARM.com    bool operator!=(MemCmd c2) const { return (cmd != c2.cmd); }
2482810SN/A};
2492810SN/A
2502810SN/A/**
2512810SN/A * A Packet is used to encapsulate a transfer between two objects in
2522810SN/A * the memory system (e.g., the L1 and L2 cache).  (In contrast, a
2532810SN/A * single Request travels all the way from the requester to the
2546122SSteve.Reinhardt@amd.com * ultimate destination and back, possibly being conveyed by several
2556122SSteve.Reinhardt@amd.com * different Packets along the way.)
2566122SSteve.Reinhardt@amd.com */
2572810SN/Aclass Packet : public Printable
2582810SN/A{
2592810SN/A  public:
2604626SN/A    typedef uint32_t FlagsType;
2614626SN/A    typedef ::Flags<FlagsType> Flags;
2622810SN/A
2632810SN/A  private:
2642810SN/A
2652810SN/A    enum : FlagsType {
2663503SN/A        // Flags to transfer across when copying a packet
2673503SN/A        COPY_FLAGS             = 0x0000003F,
2683503SN/A
2696122SSteve.Reinhardt@amd.com        // Flags that are used to create reponse packets
2706122SSteve.Reinhardt@amd.com        RESPONDER_FLAGS        = 0x00000009,
2716122SSteve.Reinhardt@amd.com
2728883SAli.Saidi@ARM.com        // Does this packet have sharers (which means it should not be
2736122SSteve.Reinhardt@amd.com        // considered writable) or not. See setHasSharers below.
2748833Sdam.sunwoo@arm.com        HAS_SHARERS            = 0x00000001,
2758833Sdam.sunwoo@arm.com
2768833Sdam.sunwoo@arm.com        // Special control flags
2776978SLisa.Hsu@amd.com        /// Special timing-mode atomic snoop for multi-level coherence.
2782810SN/A        EXPRESS_SNOOP          = 0x00000002,
2792810SN/A
2802810SN/A        /// Allow a responding cache to inform the cache hierarchy
2812810SN/A        /// that it had a writable copy before responding. See
2822810SN/A        /// setResponderHadWritable below.
2832810SN/A        RESPONDER_HAD_WRITABLE = 0x00000004,
2842810SN/A
2855999Snate@binkert.org        // Snoop co-ordination flag to indicate that a cache is
2862810SN/A        // responding to a snoop. See setCacheResponding below.
2872810SN/A        CACHE_RESPONDING       = 0x00000008,
2882810SN/A
2892810SN/A        // The writeback/writeclean should be propagated further
2902810SN/A        // downstream by the receiver
2912810SN/A        WRITE_THROUGH          = 0x00000010,
2925999Snate@binkert.org
2932810SN/A        // Response co-ordination flag for cache maintenance
2942810SN/A        // operations
2952810SN/A        SATISFIED              = 0x00000020,
2962810SN/A
2972810SN/A        /// Are the 'addr' and 'size' fields valid?
2982810SN/A        VALID_ADDR             = 0x00000100,
2992810SN/A        VALID_SIZE             = 0x00000200,
3002810SN/A
3012810SN/A        /// Is the data pointer set to a value that shouldn't be freed
3025999Snate@binkert.org        /// when the packet is destroyed?
3032810SN/A        STATIC_DATA            = 0x00001000,
3042810SN/A        /// The data pointer points to a value that should be freed when
3052810SN/A        /// the packet is destroyed. The pointer is assumed to be pointing
3062810SN/A        /// to an array, and delete [] is consequently called
3072810SN/A        DYNAMIC_DATA           = 0x00002000,
3082810SN/A
3094022SN/A        /// suppress the error if this packet encounters a functional
3102810SN/A        /// access failure.
3112810SN/A        SUPPRESS_FUNC_ERROR    = 0x00008000,
3122810SN/A
3132810SN/A        // Signal block present to squash prefetch and cache evict packets
3142810SN/A        // through express snoop flag
3152810SN/A        BLOCK_CACHED          = 0x00010000
3164022SN/A    };
3172810SN/A
3182810SN/A    Flags flags;
3192810SN/A
3202810SN/A  public:
3212810SN/A    typedef MemCmd::Command Command;
3222810SN/A
3234022SN/A    /// The command field of the packet.
3242810SN/A    MemCmd cmd;
3252810SN/A
3262810SN/A    const PacketId id;
3272810SN/A
3282810SN/A    /// A pointer to the original request.
3292810SN/A    RequestPtr req;
3305999Snate@binkert.org
3312810SN/A  private:
3325999Snate@binkert.org   /**
3332810SN/A    * A pointer to the data being transferred. It can be different
3342810SN/A    * sizes at each level of the hierarchy so it belongs to the
3352810SN/A    * packet, not request. This may or may not be populated when a
3362810SN/A    * responder receives the packet. If not populated memory should
3372810SN/A    * be allocated.
3385999Snate@binkert.org    */
3392810SN/A    PacketDataPtr data;
3402810SN/A
3415999Snate@binkert.org    /// The address of the request.  This address could be virtual or
3422810SN/A    /// physical, depending on the system configuration.
3434626SN/A    Addr addr;
3445999Snate@binkert.org
3454626SN/A    /// True if the request targets the secure memory space.
3464626SN/A    bool _isSecure;
3475999Snate@binkert.org
3484626SN/A    /// The size of the request or transfer.
3494626SN/A    unsigned size;
3504626SN/A
3514626SN/A    /**
3524626SN/A     * Track the bytes found that satisfy a functional read.
3534626SN/A     */
3545999Snate@binkert.org    std::vector<bool> bytesValid;
3554626SN/A
3564626SN/A    // Quality of Service priority value
3574626SN/A    uint8_t _qosValue;
3584626SN/A
3594626SN/A  public:
3604626SN/A
3615999Snate@binkert.org    /**
3624626SN/A     * The extra delay from seeing the packet until the header is
3634626SN/A     * transmitted. This delay is used to communicate the crossbar
3644626SN/A     * forwarding latency to the neighbouring object (e.g. a cache)
3654626SN/A     * that actually makes the packet wait. As the delay is relative,
3665999Snate@binkert.org     * a 32-bit unsigned should be sufficient.
3674626SN/A     */
3684626SN/A    uint32_t headerDelay;
3694626SN/A
3704626SN/A    /**
3714626SN/A     * Keep track of the extra delay incurred by snooping upwards
3724626SN/A     * before sending a request down the memory system. This is used
3735999Snate@binkert.org     * by the coherent crossbar to account for the additional request
3744626SN/A     * delay.
3754626SN/A     */
3764626SN/A    uint32_t snoopDelay;
3777461Snate@binkert.org
3784626SN/A    /**
3794626SN/A     * The extra pipelining delay from seeing the packet until the end of
3804626SN/A     * payload is transmitted by the component that provided it (if
3814626SN/A     * any). This includes the header delay. Similar to the header
3824626SN/A     * delay, this is used to make up for the fact that the
3834626SN/A     * crossbar does not make the packet wait. As the delay is
3847461Snate@binkert.org     * relative, a 32-bit unsigned should be sufficient.
3854626SN/A     */
3864626SN/A    uint32_t payloadDelay;
3874626SN/A
3884626SN/A    /**
3894626SN/A     * A virtual base opaque structure used to hold state associated
3904626SN/A     * with the packet (e.g., an MSHR), specific to a MemObject that
3914626SN/A     * sees the packet. A pointer to this state is returned in the
3924626SN/A     * packet's response so that the MemObject in question can quickly
3934626SN/A     * look up the state needed to process it. A specific subclass
3944626SN/A     * would be derived from this to carry state specific to a
3954626SN/A     * particular sending device.
3964626SN/A     *
3974626SN/A     * As multiple MemObjects may add their SenderState throughout the
3984626SN/A     * memory system, the SenderStates create a stack, where a
3994626SN/A     * MemObject can add a new Senderstate, as long as the
4004626SN/A     * predecessing SenderState is restored when the response comes
4014626SN/A     * back. For this reason, the predecessor should always be
4024626SN/A     * populated with the current SenderState of a packet before
4034626SN/A     * modifying the senderState field in the request packet.
4044626SN/A     */
4054626SN/A    struct SenderState
4065999Snate@binkert.org    {
4074626SN/A        SenderState* predecessor;
4085999Snate@binkert.org        SenderState() : predecessor(NULL) {}
4094626SN/A        virtual ~SenderState() {}
4105999Snate@binkert.org    };
4114626SN/A
4122810SN/A    /**
4132810SN/A     * Object used to maintain state of a PrintReq.  The senderState
4142810SN/A     * field of a PrintReq should always be of this type.
4152810SN/A     */
4162810SN/A    class PrintReqState : public SenderState
4172810SN/A    {
4182810SN/A      private:
4192810SN/A        /**
4202810SN/A         * An entry in the label stack.
4212810SN/A         */
4225034SN/A        struct LabelStackEntry
4235034SN/A        {
4245034SN/A            const std::string label;
4253606SN/A            std::string *prefix;
4262858SN/A            bool labelPrinted;
4272858SN/A            LabelStackEntry(const std::string &_label, std::string *_prefix);
4282810SN/A        };
4292810SN/A
4302810SN/A        typedef std::list<LabelStackEntry> LabelStack;
4312810SN/A        LabelStack labelStack;
4326227Snate@binkert.org
4336227Snate@binkert.org        std::string *curPrefixPtr;
4342810SN/A
4352810SN/A      public:
4362810SN/A        std::ostream &os;
4372810SN/A        const int verbosity;
4384626SN/A
4396666Ssteve.reinhardt@amd.com        PrintReqState(std::ostream &os, int verbosity = 0);
4404626SN/A        ~PrintReqState();
4414626SN/A
4428883SAli.Saidi@ARM.com        /**
4436122SSteve.Reinhardt@amd.com         * Returns the current line prefix.
4444628SN/A         */
4454628SN/A        const std::string &curPrefix() { return *curPrefixPtr; }
4464902SN/A
4474628SN/A        /**
4484628SN/A         * Push a label onto the label stack, and prepend the given
4494628SN/A         * prefix string onto the current prefix.  Labels will only be
4504628SN/A         * printed if an object within the label's scope is printed.
4514628SN/A         */
4524902SN/A        void pushLabel(const std::string &lbl,
4534628SN/A                       const std::string &prefix = "  ");
4544902SN/A
4554902SN/A        /**
4564902SN/A         * Pop a label off the label stack.
4574628SN/A         */
4584628SN/A        void popLabel();
4594628SN/A
4604902SN/A        /**
4614902SN/A         * Print all of the pending unprinted labels on the
4624902SN/A         * stack. Called by printObj(), so normally not called by
4634902SN/A         * users unless bypassing printObj().
4644902SN/A         */
4654902SN/A        void printLabels();
4664902SN/A
4674902SN/A        /**
4684628SN/A         * Print a Printable object to os, because it matched the
4692810SN/A         * address on a PrintReq.
4702810SN/A         */
4712810SN/A        void printObj(Printable *obj);
4722810SN/A    };
4732810SN/A
4742810SN/A    /**
4752810SN/A     * This packet's sender state.  Devices should use dynamic_cast<>
4762810SN/A     * to cast to the state appropriate to the sender.  The intent of
4772810SN/A     * this variable is to allow a device to attach extra information
4782810SN/A     * to a request. A response packet must return the sender state
4792810SN/A     * that was attached to the original request (even if a new packet
4802810SN/A     * is created).
4812810SN/A     */
4822810SN/A    SenderState *senderState;
4832810SN/A
4842810SN/A    /**
4852810SN/A     * Push a new sender state to the packet and make the current
4862810SN/A     * sender state the predecessor of the new one. This should be
4877823Ssteve.reinhardt@amd.com     * prefered over direct manipulation of the senderState member
4884630SN/A     * variable.
4892810SN/A     *
4904630SN/A     * @param sender_state SenderState to push at the top of the stack
4914630SN/A     */
4922810SN/A    void pushSenderState(SenderState *sender_state);
4932810SN/A
4942810SN/A    /**
4952810SN/A     * Pop the top of the state stack and return a pointer to it. This
4962810SN/A     * assumes the current sender state is not NULL. This should be
4972810SN/A     * preferred over direct manipulation of the senderState member
4982810SN/A     * variable.
4992810SN/A     *
5002810SN/A     * @return The current top of the stack
5012810SN/A     */
5022810SN/A    SenderState *popSenderState();
5032810SN/A
5044630SN/A    /**
5054630SN/A     * Go through the sender state stack and return the first instance
5064630SN/A     * that is of type T (as determined by a dynamic_cast). If there
5077823Ssteve.reinhardt@amd.com     * is no sender state of type T, NULL is returned.
5084630SN/A     *
5092810SN/A     * @return The topmost state of type T
5102810SN/A     */
5112810SN/A    template <typename T>
5122810SN/A    T * findNextSenderState() const
5132810SN/A    {
5142810SN/A        T *t = NULL;
5152810SN/A        SenderState* sender_state = senderState;
5162810SN/A        while (t == NULL && sender_state != NULL) {
5174458SN/A            t = dynamic_cast<T*>(sender_state);
5182810SN/A            sender_state = sender_state->predecessor;
5194458SN/A        }
5202810SN/A        return t;
5212810SN/A    }
5222810SN/A
5232810SN/A    /// Return the string name of the cmd field (for debugging and
5242810SN/A    /// tracing).
5252810SN/A    const std::string &cmdString() const { return cmd.toString(); }
5264458SN/A
5272810SN/A    /// Return the index of this command.
5285875Ssteve.reinhardt@amd.com    inline int cmdToIndex() const { return cmd.toInt(); }
5295875Ssteve.reinhardt@amd.com
5305875Ssteve.reinhardt@amd.com    bool isRead() const              { return cmd.isRead(); }
5315875Ssteve.reinhardt@amd.com    bool isWrite() const             { return cmd.isWrite(); }
5325875Ssteve.reinhardt@amd.com    bool isUpgrade()  const          { return cmd.isUpgrade(); }
5332811SN/A    bool isRequest() const           { return cmd.isRequest(); }
5343503SN/A    bool isResponse() const          { return cmd.isResponse(); }
5353503SN/A    bool needsWritable() const
5363503SN/A    {
5374626SN/A        // we should never check if a response needsWritable, the
5384626SN/A        // request has this flag, and for a response we should rather
5394626SN/A        // look at the hasSharers flag (if not set, the response is to
5404626SN/A        // be considered writable)
5418833Sdam.sunwoo@arm.com        assert(isRequest());
5423503SN/A        return cmd.needsWritable();
5438833Sdam.sunwoo@arm.com    }
5448833Sdam.sunwoo@arm.com    bool needsResponse() const       { return cmd.needsResponse(); }
5454626SN/A    bool isInvalidate() const        { return cmd.isInvalidate(); }
5464626SN/A    bool isEviction() const          { return cmd.isEviction(); }
5474626SN/A    bool isClean() const             { return cmd.isClean(); }
5484626SN/A    bool fromCache() const           { return cmd.fromCache(); }
5494626SN/A    bool isWriteback() const         { return cmd.isWriteback(); }
5503503SN/A    bool hasData() const             { return cmd.hasData(); }
5513503SN/A    bool hasRespData() const
5528833Sdam.sunwoo@arm.com    {
5536978SLisa.Hsu@amd.com        MemCmd resp_cmd = cmd.responseCommand();
5548833Sdam.sunwoo@arm.com        return resp_cmd.hasData();
5558833Sdam.sunwoo@arm.com    }
5566978SLisa.Hsu@amd.com    bool isLLSC() const              { return cmd.isLLSC(); }
5576978SLisa.Hsu@amd.com    bool isError() const             { return cmd.isError(); }
5583503SN/A    bool isPrint() const             { return cmd.isPrint(); }
5592810SN/A    bool isFlush() const             { return cmd.isFlush(); }
5602810SN/A
5612810SN/A    bool isWholeLineWrite(unsigned blk_size)
562    {
563        return (cmd == MemCmd::WriteReq || cmd == MemCmd::WriteLineReq) &&
564            getOffset(blk_size) == 0 && getSize() == blk_size;
565    }
566
567    //@{
568    /// Snoop flags
569    /**
570     * Set the cacheResponding flag. This is used by the caches to
571     * signal another cache that they are responding to a request. A
572     * cache will only respond to snoops if it has the line in either
573     * Modified or Owned state. Note that on snoop hits we always pass
574     * the line as Modified and never Owned. In the case of an Owned
575     * line we proceed to invalidate all other copies.
576     *
577     * On a cache fill (see Cache::handleFill), we check hasSharers
578     * first, ignoring the cacheResponding flag if hasSharers is set.
579     * A line is consequently allocated as:
580     *
581     * hasSharers cacheResponding state
582     * true       false           Shared
583     * true       true            Shared
584     * false      false           Exclusive
585     * false      true            Modified
586     */
587    void setCacheResponding()
588    {
589        assert(isRequest());
590        assert(!flags.isSet(CACHE_RESPONDING));
591        flags.set(CACHE_RESPONDING);
592    }
593    bool cacheResponding() const { return flags.isSet(CACHE_RESPONDING); }
594    /**
595     * On fills, the hasSharers flag is used by the caches in
596     * combination with the cacheResponding flag, as clarified
597     * above. If the hasSharers flag is not set, the packet is passing
598     * writable. Thus, a response from a memory passes the line as
599     * writable by default.
600     *
601     * The hasSharers flag is also used by upstream caches to inform a
602     * downstream cache that they have the block (by calling
603     * setHasSharers on snoop request packets that hit in upstream
604     * cachs tags or MSHRs). If the snoop packet has sharers, a
605     * downstream cache is prevented from passing a dirty line upwards
606     * if it was not explicitly asked for a writable copy. See
607     * Cache::satisfyCpuSideRequest.
608     *
609     * The hasSharers flag is also used on writebacks, in
610     * combination with the WritbackClean or WritebackDirty commands,
611     * to allocate the block downstream either as:
612     *
613     * command        hasSharers state
614     * WritebackDirty false      Modified
615     * WritebackDirty true       Owned
616     * WritebackClean false      Exclusive
617     * WritebackClean true       Shared
618     */
619    void setHasSharers()    { flags.set(HAS_SHARERS); }
620    bool hasSharers() const { return flags.isSet(HAS_SHARERS); }
621    //@}
622
623    /**
624     * The express snoop flag is used for two purposes. Firstly, it is
625     * used to bypass flow control for normal (non-snoop) requests
626     * going downstream in the memory system. In cases where a cache
627     * is responding to a snoop from another cache (it had a dirty
628     * line), but the line is not writable (and there are possibly
629     * other copies), the express snoop flag is set by the downstream
630     * cache to invalidate all other copies in zero time. Secondly,
631     * the express snoop flag is also set to be able to distinguish
632     * snoop packets that came from a downstream cache, rather than
633     * snoop packets from neighbouring caches.
634     */
635    void setExpressSnoop()      { flags.set(EXPRESS_SNOOP); }
636    bool isExpressSnoop() const { return flags.isSet(EXPRESS_SNOOP); }
637
638    /**
639     * On responding to a snoop request (which only happens for
640     * Modified or Owned lines), make sure that we can transform an
641     * Owned response to a Modified one. If this flag is not set, the
642     * responding cache had the line in the Owned state, and there are
643     * possibly other Shared copies in the memory system. A downstream
644     * cache helps in orchestrating the invalidation of these copies
645     * by sending out the appropriate express snoops.
646     */
647    void setResponderHadWritable()
648    {
649        assert(cacheResponding());
650        assert(!responderHadWritable());
651        flags.set(RESPONDER_HAD_WRITABLE);
652    }
653    bool responderHadWritable() const
654    { return flags.isSet(RESPONDER_HAD_WRITABLE); }
655
656    /**
657     * Copy the reponse flags from an input packet to this packet. The
658     * reponse flags determine whether a responder has been found and
659     * the state at which the block will be at the destination.
660     *
661     * @pkt The packet that we will copy flags from
662     */
663    void copyResponderFlags(const PacketPtr pkt);
664
665    /**
666     * A writeback/writeclean cmd gets propagated further downstream
667     * by the receiver when the flag is set.
668     */
669    void setWriteThrough()
670    {
671        assert(cmd.isWrite() &&
672               (cmd.isEviction() || cmd == MemCmd::WriteClean));
673        flags.set(WRITE_THROUGH);
674    }
675    void clearWriteThrough() { flags.clear(WRITE_THROUGH); }
676    bool writeThrough() const { return flags.isSet(WRITE_THROUGH); }
677
678    /**
679     * Set when a request hits in a cache and the cache is not going
680     * to respond. This is used by the crossbar to coordinate
681     * responses for cache maintenance operations.
682     */
683    void setSatisfied()
684    {
685        assert(cmd.isClean());
686        assert(!flags.isSet(SATISFIED));
687        flags.set(SATISFIED);
688    }
689    bool satisfied() const { return flags.isSet(SATISFIED); }
690
691    void setSuppressFuncError()     { flags.set(SUPPRESS_FUNC_ERROR); }
692    bool suppressFuncError() const  { return flags.isSet(SUPPRESS_FUNC_ERROR); }
693    void setBlockCached()          { flags.set(BLOCK_CACHED); }
694    bool isBlockCached() const     { return flags.isSet(BLOCK_CACHED); }
695    void clearBlockCached()        { flags.clear(BLOCK_CACHED); }
696
697    /**
698     * QoS Value getter
699     * Returns 0 if QoS value was never set (constructor default).
700     *
701     * @return QoS priority value of the packet
702     */
703    inline uint8_t qosValue() const { return _qosValue; }
704
705    /**
706     * QoS Value setter
707     * Interface for setting QoS priority value of the packet.
708     *
709     * @param qos_value QoS priority value
710     */
711    inline void qosValue(const uint8_t qos_value)
712    { _qosValue = qos_value; }
713
714    inline MasterID masterId() const { return req->masterId(); }
715
716    // Network error conditions... encapsulate them as methods since
717    // their encoding keeps changing (from result field to command
718    // field, etc.)
719    void
720    setBadAddress()
721    {
722        assert(isResponse());
723        cmd = MemCmd::BadAddressError;
724    }
725
726    void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
727
728    Addr getAddr() const { assert(flags.isSet(VALID_ADDR)); return addr; }
729    /**
730     * Update the address of this packet mid-transaction. This is used
731     * by the address mapper to change an already set address to a new
732     * one based on the system configuration. It is intended to remap
733     * an existing address, so it asserts that the current address is
734     * valid.
735     */
736    void setAddr(Addr _addr) { assert(flags.isSet(VALID_ADDR)); addr = _addr; }
737
738    unsigned getSize() const  { assert(flags.isSet(VALID_SIZE)); return size; }
739
740    /**
741     * Get address range to which this packet belongs.
742     *
743     * @return Address range of this packet.
744     */
745    AddrRange getAddrRange() const;
746
747    Addr getOffset(unsigned int blk_size) const
748    {
749        return getAddr() & Addr(blk_size - 1);
750    }
751
752    Addr getBlockAddr(unsigned int blk_size) const
753    {
754        return getAddr() & ~(Addr(blk_size - 1));
755    }
756
757    bool isSecure() const
758    {
759        assert(flags.isSet(VALID_ADDR));
760        return _isSecure;
761    }
762
763    /**
764     * Accessor function to atomic op.
765     */
766    AtomicOpFunctor *getAtomicOp() const { return req->getAtomicOpFunctor(); }
767    bool isAtomicOp() const { return req->isAtomic(); }
768
769    /**
770     * It has been determined that the SC packet should successfully update
771     * memory. Therefore, convert this SC packet to a normal write.
772     */
773    void
774    convertScToWrite()
775    {
776        assert(isLLSC());
777        assert(isWrite());
778        cmd = MemCmd::WriteReq;
779    }
780
781    /**
782     * When ruby is in use, Ruby will monitor the cache line and the
783     * phys memory should treat LL ops as normal reads.
784     */
785    void
786    convertLlToRead()
787    {
788        assert(isLLSC());
789        assert(isRead());
790        cmd = MemCmd::ReadReq;
791    }
792
793    /**
794     * Constructor. Note that a Request object must be constructed
795     * first, but the Requests's physical address and size fields need
796     * not be valid. The command must be supplied.
797     */
798    Packet(const RequestPtr &_req, MemCmd _cmd)
799        :  cmd(_cmd), id((PacketId)_req.get()), req(_req),
800           data(nullptr), addr(0), _isSecure(false), size(0),
801           _qosValue(0), headerDelay(0), snoopDelay(0),
802           payloadDelay(0), senderState(NULL)
803    {
804        if (req->hasPaddr()) {
805            addr = req->getPaddr();
806            flags.set(VALID_ADDR);
807            _isSecure = req->isSecure();
808        }
809        if (req->hasSize()) {
810            size = req->getSize();
811            flags.set(VALID_SIZE);
812        }
813    }
814
815    /**
816     * Alternate constructor if you are trying to create a packet with
817     * a request that is for a whole block, not the address from the
818     * req.  this allows for overriding the size/addr of the req.
819     */
820    Packet(const RequestPtr &_req, MemCmd _cmd, int _blkSize, PacketId _id = 0)
821        :  cmd(_cmd), id(_id ? _id : (PacketId)_req.get()), req(_req),
822           data(nullptr), addr(0), _isSecure(false),
823           _qosValue(0), headerDelay(0),
824           snoopDelay(0), payloadDelay(0), senderState(NULL)
825    {
826        if (req->hasPaddr()) {
827            addr = req->getPaddr() & ~(_blkSize - 1);
828            flags.set(VALID_ADDR);
829            _isSecure = req->isSecure();
830        }
831        size = _blkSize;
832        flags.set(VALID_SIZE);
833    }
834
835    /**
836     * Alternate constructor for copying a packet.  Copy all fields
837     * *except* if the original packet's data was dynamic, don't copy
838     * that, as we can't guarantee that the new packet's lifetime is
839     * less than that of the original packet.  In this case the new
840     * packet should allocate its own data.
841     */
842    Packet(const PacketPtr pkt, bool clear_flags, bool alloc_data)
843        :  cmd(pkt->cmd), id(pkt->id), req(pkt->req),
844           data(nullptr),
845           addr(pkt->addr), _isSecure(pkt->_isSecure), size(pkt->size),
846           bytesValid(pkt->bytesValid),
847           _qosValue(pkt->qosValue()),
848           headerDelay(pkt->headerDelay),
849           snoopDelay(0),
850           payloadDelay(pkt->payloadDelay),
851           senderState(pkt->senderState)
852    {
853        if (!clear_flags)
854            flags.set(pkt->flags & COPY_FLAGS);
855
856        flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE));
857
858        // should we allocate space for data, or not, the express
859        // snoops do not need to carry any data as they only serve to
860        // co-ordinate state changes
861        if (alloc_data) {
862            // even if asked to allocate data, if the original packet
863            // holds static data, then the sender will not be doing
864            // any memcpy on receiving the response, thus we simply
865            // carry the pointer forward
866            if (pkt->flags.isSet(STATIC_DATA)) {
867                data = pkt->data;
868                flags.set(STATIC_DATA);
869            } else {
870                allocate();
871            }
872        }
873    }
874
875    /**
876     * Generate the appropriate read MemCmd based on the Request flags.
877     */
878    static MemCmd
879    makeReadCmd(const RequestPtr &req)
880    {
881        if (req->isLLSC())
882            return MemCmd::LoadLockedReq;
883        else if (req->isPrefetchEx())
884            return MemCmd::SoftPFExReq;
885        else if (req->isPrefetch())
886            return MemCmd::SoftPFReq;
887        else
888            return MemCmd::ReadReq;
889    }
890
891    /**
892     * Generate the appropriate write MemCmd based on the Request flags.
893     */
894    static MemCmd
895    makeWriteCmd(const RequestPtr &req)
896    {
897        if (req->isLLSC())
898            return MemCmd::StoreCondReq;
899        else if (req->isSwap() || req->isAtomic())
900            return MemCmd::SwapReq;
901        else if (req->isCacheInvalidate()) {
902          return req->isCacheClean() ? MemCmd::CleanInvalidReq :
903              MemCmd::InvalidateReq;
904        } else if (req->isCacheClean()) {
905            return MemCmd::CleanSharedReq;
906        } else
907            return MemCmd::WriteReq;
908    }
909
910    /**
911     * Constructor-like methods that return Packets based on Request objects.
912     * Fine-tune the MemCmd type if it's not a vanilla read or write.
913     */
914    static PacketPtr
915    createRead(const RequestPtr &req)
916    {
917        return new Packet(req, makeReadCmd(req));
918    }
919
920    static PacketPtr
921    createWrite(const RequestPtr &req)
922    {
923        return new Packet(req, makeWriteCmd(req));
924    }
925
926    /**
927     * clean up packet variables
928     */
929    ~Packet()
930    {
931        deleteData();
932    }
933
934    /**
935     * Take a request packet and modify it in place to be suitable for
936     * returning as a response to that request.
937     */
938    void
939    makeResponse()
940    {
941        assert(needsResponse());
942        assert(isRequest());
943        cmd = cmd.responseCommand();
944
945        // responses are never express, even if the snoop that
946        // triggered them was
947        flags.clear(EXPRESS_SNOOP);
948    }
949
950    void
951    makeAtomicResponse()
952    {
953        makeResponse();
954    }
955
956    void
957    makeTimingResponse()
958    {
959        makeResponse();
960    }
961
962    void
963    setFunctionalResponseStatus(bool success)
964    {
965        if (!success) {
966            if (isWrite()) {
967                cmd = MemCmd::FunctionalWriteError;
968            } else {
969                cmd = MemCmd::FunctionalReadError;
970            }
971        }
972    }
973
974    void
975    setSize(unsigned size)
976    {
977        assert(!flags.isSet(VALID_SIZE));
978
979        this->size = size;
980        flags.set(VALID_SIZE);
981    }
982
983    /**
984     * Check if packet corresponds to a given block-aligned address and
985     * address space.
986     *
987     * @param addr The address to compare against.
988     * @param is_secure Whether addr belongs to the secure address space.
989     * @param blk_size Block size in bytes.
990     * @return Whether packet matches description.
991     */
992    bool matchBlockAddr(const Addr addr, const bool is_secure,
993                        const int blk_size) const;
994
995    /**
996     * Check if this packet refers to the same block-aligned address and
997     * address space as another packet.
998     *
999     * @param pkt The packet to compare against.
1000     * @param blk_size Block size in bytes.
1001     * @return Whether packet matches description.
1002     */
1003    bool matchBlockAddr(const PacketPtr pkt, const int blk_size) const;
1004
1005    /**
1006     * Check if packet corresponds to a given address and address space.
1007     *
1008     * @param addr The address to compare against.
1009     * @param is_secure Whether addr belongs to the secure address space.
1010     * @return Whether packet matches description.
1011     */
1012    bool matchAddr(const Addr addr, const bool is_secure) const;
1013
1014    /**
1015     * Check if this packet refers to the same address and address space as
1016     * another packet.
1017     *
1018     * @param pkt The packet to compare against.
1019     * @return Whether packet matches description.
1020     */
1021    bool matchAddr(const PacketPtr pkt) const;
1022
1023  public:
1024    /**
1025     * @{
1026     * @name Data accessor mehtods
1027     */
1028
1029    /**
1030     * Set the data pointer to the following value that should not be
1031     * freed. Static data allows us to do a single memcpy even if
1032     * multiple packets are required to get from source to destination
1033     * and back. In essence the pointer is set calling dataStatic on
1034     * the original packet, and whenever this packet is copied and
1035     * forwarded the same pointer is passed on. When a packet
1036     * eventually reaches the destination holding the data, it is
1037     * copied once into the location originally set. On the way back
1038     * to the source, no copies are necessary.
1039     */
1040    template <typename T>
1041    void
1042    dataStatic(T *p)
1043    {
1044        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
1045        data = (PacketDataPtr)p;
1046        flags.set(STATIC_DATA);
1047    }
1048
1049    /**
1050     * Set the data pointer to the following value that should not be
1051     * freed. This version of the function allows the pointer passed
1052     * to us to be const. To avoid issues down the line we cast the
1053     * constness away, the alternative would be to keep both a const
1054     * and non-const data pointer and cleverly choose between
1055     * them. Note that this is only allowed for static data.
1056     */
1057    template <typename T>
1058    void
1059    dataStaticConst(const T *p)
1060    {
1061        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
1062        data = const_cast<PacketDataPtr>(p);
1063        flags.set(STATIC_DATA);
1064    }
1065
1066    /**
1067     * Set the data pointer to a value that should have delete []
1068     * called on it. Dynamic data is local to this packet, and as the
1069     * packet travels from source to destination, forwarded packets
1070     * will allocate their own data. When a packet reaches the final
1071     * destination it will populate the dynamic data of that specific
1072     * packet, and on the way back towards the source, memcpy will be
1073     * invoked in every step where a new packet was created e.g. in
1074     * the caches. Ultimately when the response reaches the source a
1075     * final memcpy is needed to extract the data from the packet
1076     * before it is deallocated.
1077     */
1078    template <typename T>
1079    void
1080    dataDynamic(T *p)
1081    {
1082        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
1083        data = (PacketDataPtr)p;
1084        flags.set(DYNAMIC_DATA);
1085    }
1086
1087    /**
1088     * get a pointer to the data ptr.
1089     */
1090    template <typename T>
1091    T*
1092    getPtr()
1093    {
1094        assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
1095        return (T*)data;
1096    }
1097
1098    template <typename T>
1099    const T*
1100    getConstPtr() const
1101    {
1102        assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
1103        return (const T*)data;
1104    }
1105
1106    /**
1107     * Get the data in the packet byte swapped from big endian to
1108     * host endian.
1109     */
1110    template <typename T>
1111    T getBE() const;
1112
1113    /**
1114     * Get the data in the packet byte swapped from little endian to
1115     * host endian.
1116     */
1117    template <typename T>
1118    T getLE() const;
1119
1120    /**
1121     * Get the data in the packet byte swapped from the specified
1122     * endianness.
1123     */
1124    template <typename T>
1125    T get(ByteOrder endian) const;
1126
1127#if THE_ISA != NULL_ISA
1128    /**
1129     * Get the data in the packet byte swapped from guest to host
1130     * endian.
1131     */
1132    template <typename T>
1133    T get() const
1134        M5_DEPRECATED_MSG("The memory system should be ISA independent.");
1135#endif
1136
1137    /** Set the value in the data pointer to v as big endian. */
1138    template <typename T>
1139    void setBE(T v);
1140
1141    /** Set the value in the data pointer to v as little endian. */
1142    template <typename T>
1143    void setLE(T v);
1144
1145    /**
1146     * Set the value in the data pointer to v using the specified
1147     * endianness.
1148     */
1149    template <typename T>
1150    void set(T v, ByteOrder endian);
1151
1152#if THE_ISA != NULL_ISA
1153    /** Set the value in the data pointer to v as guest endian. */
1154    template <typename T>
1155    void set(T v)
1156        M5_DEPRECATED_MSG("The memory system should be ISA independent.");
1157#endif
1158
1159    /**
1160     * Get the data in the packet byte swapped from the specified
1161     * endianness and zero-extended to 64 bits.
1162     */
1163    uint64_t getUintX(ByteOrder endian) const;
1164
1165    /**
1166     * Set the value in the word w after truncating it to the length
1167     * of the packet and then byteswapping it to the desired
1168     * endianness.
1169     */
1170    void setUintX(uint64_t w, ByteOrder endian);
1171
1172    /**
1173     * Copy data into the packet from the provided pointer.
1174     */
1175    void
1176    setData(const uint8_t *p)
1177    {
1178        // we should never be copying data onto itself, which means we
1179        // must idenfity packets with static data, as they carry the
1180        // same pointer from source to destination and back
1181        assert(p != getPtr<uint8_t>() || flags.isSet(STATIC_DATA));
1182
1183        if (p != getPtr<uint8_t>())
1184            // for packet with allocated dynamic data, we copy data from
1185            // one to the other, e.g. a forwarded response to a response
1186            std::memcpy(getPtr<uint8_t>(), p, getSize());
1187    }
1188
1189    /**
1190     * Copy data into the packet from the provided block pointer,
1191     * which is aligned to the given block size.
1192     */
1193    void
1194    setDataFromBlock(const uint8_t *blk_data, int blkSize)
1195    {
1196        setData(blk_data + getOffset(blkSize));
1197    }
1198
1199    /**
1200     * Copy data from the packet to the memory at the provided pointer.
1201     * @param p Pointer to which data will be copied.
1202     */
1203    void
1204    writeData(uint8_t *p) const
1205    {
1206        std::memcpy(p, getConstPtr<uint8_t>(), getSize());
1207    }
1208
1209    /**
1210     * Copy data from the packet to the provided block pointer, which
1211     * is aligned to the given block size.
1212     * @param blk_data Pointer to block to which data will be copied.
1213     * @param blkSize Block size in bytes.
1214     */
1215    void
1216    writeDataToBlock(uint8_t *blk_data, int blkSize) const
1217    {
1218        writeData(blk_data + getOffset(blkSize));
1219    }
1220
1221    /**
1222     * delete the data pointed to in the data pointer. Ok to call to
1223     * matter how data was allocted.
1224     */
1225    void
1226    deleteData()
1227    {
1228        if (flags.isSet(DYNAMIC_DATA))
1229            delete [] data;
1230
1231        flags.clear(STATIC_DATA|DYNAMIC_DATA);
1232        data = NULL;
1233    }
1234
1235    /** Allocate memory for the packet. */
1236    void
1237    allocate()
1238    {
1239        // if either this command or the response command has a data
1240        // payload, actually allocate space
1241        if (hasData() || hasRespData()) {
1242            assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
1243            flags.set(DYNAMIC_DATA);
1244            data = new uint8_t[getSize()];
1245        }
1246    }
1247
1248    /** @} */
1249
1250    /** Get the data in the packet without byte swapping. */
1251    template <typename T>
1252    T getRaw() const;
1253
1254    /** Set the value in the data pointer to v without byte swapping. */
1255    template <typename T>
1256    void setRaw(T v);
1257
1258  public:
1259    /**
1260     * Check a functional request against a memory value stored in
1261     * another packet (i.e. an in-transit request or
1262     * response). Returns true if the current packet is a read, and
1263     * the other packet provides the data, which is then copied to the
1264     * current packet. If the current packet is a write, and the other
1265     * packet intersects this one, then we update the data
1266     * accordingly.
1267     */
1268    bool
1269    trySatisfyFunctional(PacketPtr other)
1270    {
1271        // all packets that are carrying a payload should have a valid
1272        // data pointer
1273        return trySatisfyFunctional(other, other->getAddr(), other->isSecure(),
1274                                    other->getSize(),
1275                                    other->hasData() ?
1276                                    other->getPtr<uint8_t>() : NULL);
1277    }
1278
1279    /**
1280     * Does the request need to check for cached copies of the same block
1281     * in the memory hierarchy above.
1282     **/
1283    bool
1284    mustCheckAbove() const
1285    {
1286        return cmd == MemCmd::HardPFReq || isEviction();
1287    }
1288
1289    /**
1290     * Is this packet a clean eviction, including both actual clean
1291     * evict packets, but also clean writebacks.
1292     */
1293    bool
1294    isCleanEviction() const
1295    {
1296        return cmd == MemCmd::CleanEvict || cmd == MemCmd::WritebackClean;
1297    }
1298
1299    /**
1300     * Check a functional request against a memory value represented
1301     * by a base/size pair and an associated data array. If the
1302     * current packet is a read, it may be satisfied by the memory
1303     * value. If the current packet is a write, it may update the
1304     * memory value.
1305     */
1306    bool
1307    trySatisfyFunctional(Printable *obj, Addr base, bool is_secure, int size,
1308                         uint8_t *_data);
1309
1310    /**
1311     * Push label for PrintReq (safe to call unconditionally).
1312     */
1313    void
1314    pushLabel(const std::string &lbl)
1315    {
1316        if (isPrint())
1317            safe_cast<PrintReqState*>(senderState)->pushLabel(lbl);
1318    }
1319
1320    /**
1321     * Pop label for PrintReq (safe to call unconditionally).
1322     */
1323    void
1324    popLabel()
1325    {
1326        if (isPrint())
1327            safe_cast<PrintReqState*>(senderState)->popLabel();
1328    }
1329
1330    void print(std::ostream &o, int verbosity = 0,
1331               const std::string &prefix = "") const;
1332
1333    /**
1334     * A no-args wrapper of print(std::ostream...)
1335     * meant to be invoked from DPRINTFs
1336     * avoiding string overheads in fast mode
1337     * @return string with the request's type and start<->end addresses
1338     */
1339    std::string print() const;
1340};
1341
1342#endif //__MEM_PACKET_HH
1343