packet.hh revision 9546
12381SN/A/*
29542Sandreas.hansson@arm.com * Copyright (c) 2012-2013 ARM Limited
38949Sandreas.hansson@arm.com * All rights reserved
48949Sandreas.hansson@arm.com *
58949Sandreas.hansson@arm.com * The license below extends only to copyright in the software and shall
68949Sandreas.hansson@arm.com * not be construed as granting a license to any other intellectual
78949Sandreas.hansson@arm.com * property including but not limited to intellectual property relating
88949Sandreas.hansson@arm.com * to a hardware implementation of the functionality of the software
98949Sandreas.hansson@arm.com * licensed hereunder.  You may use the software subject to the license
108949Sandreas.hansson@arm.com * terms below provided that you ensure that this notice is replicated
118949Sandreas.hansson@arm.com * unmodified and in its entirety in all distributions of the software,
128949Sandreas.hansson@arm.com * modified or unmodified, in source code or in binary form.
138949Sandreas.hansson@arm.com *
142592SN/A * Copyright (c) 2006 The Regents of The University of Michigan
157636Ssteve.reinhardt@amd.com * Copyright (c) 2010 Advanced Micro Devices, Inc.
162381SN/A * All rights reserved.
172381SN/A *
182381SN/A * Redistribution and use in source and binary forms, with or without
192381SN/A * modification, are permitted provided that the following conditions are
202381SN/A * met: redistributions of source code must retain the above copyright
212381SN/A * notice, this list of conditions and the following disclaimer;
222381SN/A * redistributions in binary form must reproduce the above copyright
232381SN/A * notice, this list of conditions and the following disclaimer in the
242381SN/A * documentation and/or other materials provided with the distribution;
252381SN/A * neither the name of the copyright holders nor the names of its
262381SN/A * contributors may be used to endorse or promote products derived from
272381SN/A * this software without specific prior written permission.
282381SN/A *
292381SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
302381SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
312381SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
322381SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
332381SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
342381SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
352381SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
362381SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
372381SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
382381SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
392381SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
402665Ssaidi@eecs.umich.edu *
412665Ssaidi@eecs.umich.edu * Authors: Ron Dreslinski
422665Ssaidi@eecs.umich.edu *          Steve Reinhardt
432665Ssaidi@eecs.umich.edu *          Ali Saidi
449031Sandreas.hansson@arm.com *          Andreas Hansson
452381SN/A */
462381SN/A
472381SN/A/**
482381SN/A * @file
492662Sstever@eecs.umich.edu * Declaration of the Packet class.
502381SN/A */
512381SN/A
522381SN/A#ifndef __MEM_PACKET_HH__
532381SN/A#define __MEM_PACKET_HH__
542381SN/A
558229Snate@binkert.org#include <bitset>
563348Sbinkertn@umich.edu#include <cassert>
573348Sbinkertn@umich.edu#include <list>
583348Sbinkertn@umich.edu
595735Snate@binkert.org#include "base/cast.hh"
604024Sbinkertn@umich.edu#include "base/compiler.hh"
615735Snate@binkert.org#include "base/flags.hh"
623940Ssaidi@eecs.umich.edu#include "base/misc.hh"
635314Sstever@gmail.com#include "base/printable.hh"
646216Snate@binkert.org#include "base/types.hh"
652392SN/A#include "mem/request.hh"
664167Sbinkertn@umich.edu#include "sim/core.hh"
672394SN/A
688737Skoansin.tan@gmail.comclass Packet;
693349Sbinkertn@umich.edutypedef Packet *PacketPtr;
702394SN/Atypedef uint8_t* PacketDataPtr;
712812Srdreslin@umich.edutypedef std::list<PacketPtr> PacketList;
722812Srdreslin@umich.edu
734022Sstever@eecs.umich.educlass MemCmd
744022Sstever@eecs.umich.edu{
755735Snate@binkert.org    friend class Packet;
765735Snate@binkert.org
774022Sstever@eecs.umich.edu  public:
785735Snate@binkert.org    /**
795735Snate@binkert.org     * List of all commands associated with a packet.
805735Snate@binkert.org     */
814022Sstever@eecs.umich.edu    enum Command
824022Sstever@eecs.umich.edu    {
834022Sstever@eecs.umich.edu        InvalidCmd,
844022Sstever@eecs.umich.edu        ReadReq,
854473Sstever@eecs.umich.edu        ReadResp,
865319Sstever@gmail.com        ReadRespWithInvalidate,
874022Sstever@eecs.umich.edu        WriteReq,
884022Sstever@eecs.umich.edu        WriteResp,
894022Sstever@eecs.umich.edu        Writeback,
904022Sstever@eecs.umich.edu        SoftPFReq,
914022Sstever@eecs.umich.edu        HardPFReq,
924022Sstever@eecs.umich.edu        SoftPFResp,
934022Sstever@eecs.umich.edu        HardPFResp,
949018Sandreas.hansson@arm.com        // WriteInvalidateReq transactions used to be generated by the
959018Sandreas.hansson@arm.com        // DMA ports when writing full blocks to memory, however, it
969018Sandreas.hansson@arm.com        // is not used anymore since we put the I/O cache in place to
979018Sandreas.hansson@arm.com        // deal with partial block writes. Hence, WriteInvalidateReq
989018Sandreas.hansson@arm.com        // and WriteInvalidateResp are currently unused. The
999018Sandreas.hansson@arm.com        // implication is that the I/O cache does read-exclusive
1009018Sandreas.hansson@arm.com        // operations on every full-cache-block DMA, and ultimately
1019018Sandreas.hansson@arm.com        // this needs to be fixed.
1024022Sstever@eecs.umich.edu        WriteInvalidateReq,
1034022Sstever@eecs.umich.edu        WriteInvalidateResp,
1044022Sstever@eecs.umich.edu        UpgradeReq,
1057465Ssteve.reinhardt@amd.com        SCUpgradeReq,           // Special "weak" upgrade for StoreCond
1064628Sstever@eecs.umich.edu        UpgradeResp,
1077465Ssteve.reinhardt@amd.com        SCUpgradeFailReq,       // Failed SCUpgradeReq in MSHR (never sent)
1087465Ssteve.reinhardt@amd.com        UpgradeFailResp,        // Valid for SCUpgradeReq only
1094022Sstever@eecs.umich.edu        ReadExReq,
1104022Sstever@eecs.umich.edu        ReadExResp,
1114626Sstever@eecs.umich.edu        LoadLockedReq,
1124626Sstever@eecs.umich.edu        StoreCondReq,
1137669Ssteve.reinhardt@amd.com        StoreCondFailReq,       // Failed StoreCondReq in MSHR (never sent)
1144626Sstever@eecs.umich.edu        StoreCondResp,
1154040Ssaidi@eecs.umich.edu        SwapReq,
1164040Ssaidi@eecs.umich.edu        SwapResp,
1175650Sgblack@eecs.umich.edu        MessageReq,
1185650Sgblack@eecs.umich.edu        MessageResp,
1194870Sstever@eecs.umich.edu        // Error responses
1204870Sstever@eecs.umich.edu        // @TODO these should be classified as responses rather than
1214870Sstever@eecs.umich.edu        // requests; coding them as requests initially for backwards
1224870Sstever@eecs.umich.edu        // compatibility
1234870Sstever@eecs.umich.edu        InvalidDestError,  // packet dest field invalid
1244870Sstever@eecs.umich.edu        BadAddressError,   // memory address invalid
1258436SBrad.Beckmann@amd.com        FunctionalReadError, // unable to fulfill functional read
1268436SBrad.Beckmann@amd.com        FunctionalWriteError, // unable to fulfill functional write
1275314Sstever@gmail.com        // Fake simulator-only commands
1285314Sstever@gmail.com        PrintReq,       // Print state matching address
1298184Ssomayeh@cs.wisc.edu        FlushReq,      //request for a cache flush
1308716Snilay@cs.wisc.edu        InvalidationReq,   // request for address to be invalidated from lsq
1314022Sstever@eecs.umich.edu        NUM_MEM_CMDS
1324022Sstever@eecs.umich.edu    };
1334022Sstever@eecs.umich.edu
1344022Sstever@eecs.umich.edu  private:
1355735Snate@binkert.org    /**
1365735Snate@binkert.org     * List of command attributes.
1375735Snate@binkert.org     */
1384022Sstever@eecs.umich.edu    enum Attribute
1394022Sstever@eecs.umich.edu    {
1404626Sstever@eecs.umich.edu        IsRead,         //!< Data flows from responder to requester
1414626Sstever@eecs.umich.edu        IsWrite,        //!< Data flows from requester to responder
1427465Ssteve.reinhardt@amd.com        IsUpgrade,
1434022Sstever@eecs.umich.edu        IsInvalidate,
1444626Sstever@eecs.umich.edu        NeedsExclusive, //!< Requires exclusive copy to complete in-cache
1454626Sstever@eecs.umich.edu        IsRequest,      //!< Issued by requester
1464626Sstever@eecs.umich.edu        IsResponse,     //!< Issue by responder
1474626Sstever@eecs.umich.edu        NeedsResponse,  //!< Requester needs response from target
1484022Sstever@eecs.umich.edu        IsSWPrefetch,
1494022Sstever@eecs.umich.edu        IsHWPrefetch,
1506076Sgblack@eecs.umich.edu        IsLlsc,         //!< Alpha/MIPS LL or SC access
1514626Sstever@eecs.umich.edu        HasData,        //!< There is an associated payload
1524870Sstever@eecs.umich.edu        IsError,        //!< Error response
1535314Sstever@gmail.com        IsPrint,        //!< Print state matching address (for debugging)
1548184Ssomayeh@cs.wisc.edu        IsFlush,        //!< Flush the address from caches
1554022Sstever@eecs.umich.edu        NUM_COMMAND_ATTRIBUTES
1564022Sstever@eecs.umich.edu    };
1574022Sstever@eecs.umich.edu
1585735Snate@binkert.org    /**
1595735Snate@binkert.org     * Structure that defines attributes and other data associated
1605735Snate@binkert.org     * with a Command.
1615735Snate@binkert.org     */
1625735Snate@binkert.org    struct CommandInfo
1635735Snate@binkert.org    {
1645735Snate@binkert.org        /// Set of attribute flags.
1654022Sstever@eecs.umich.edu        const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
1665735Snate@binkert.org        /// Corresponding response for requests; InvalidCmd if no
1675735Snate@binkert.org        /// response is applicable.
1684022Sstever@eecs.umich.edu        const Command response;
1695735Snate@binkert.org        /// String representation (for printing)
1704022Sstever@eecs.umich.edu        const std::string str;
1714022Sstever@eecs.umich.edu    };
1724022Sstever@eecs.umich.edu
1735735Snate@binkert.org    /// Array to map Command enum to associated info.
1744022Sstever@eecs.umich.edu    static const CommandInfo commandInfo[];
1754022Sstever@eecs.umich.edu
1764022Sstever@eecs.umich.edu  private:
1774022Sstever@eecs.umich.edu
1784022Sstever@eecs.umich.edu    Command cmd;
1794022Sstever@eecs.umich.edu
1805735Snate@binkert.org    bool
1815735Snate@binkert.org    testCmdAttrib(MemCmd::Attribute attrib) const
1825735Snate@binkert.org    {
1834022Sstever@eecs.umich.edu        return commandInfo[cmd].attributes[attrib] != 0;
1844022Sstever@eecs.umich.edu    }
1854022Sstever@eecs.umich.edu
1864022Sstever@eecs.umich.edu  public:
1874022Sstever@eecs.umich.edu
1884022Sstever@eecs.umich.edu    bool isRead() const         { return testCmdAttrib(IsRead); }
1897465Ssteve.reinhardt@amd.com    bool isWrite() const        { return testCmdAttrib(IsWrite); }
1907465Ssteve.reinhardt@amd.com    bool isUpgrade() const      { return testCmdAttrib(IsUpgrade); }
1914022Sstever@eecs.umich.edu    bool isRequest() const      { return testCmdAttrib(IsRequest); }
1924022Sstever@eecs.umich.edu    bool isResponse() const     { return testCmdAttrib(IsResponse); }
1934870Sstever@eecs.umich.edu    bool needsExclusive() const { return testCmdAttrib(NeedsExclusive); }
1944022Sstever@eecs.umich.edu    bool needsResponse() const  { return testCmdAttrib(NeedsResponse); }
1954022Sstever@eecs.umich.edu    bool isInvalidate() const   { return testCmdAttrib(IsInvalidate); }
1964022Sstever@eecs.umich.edu    bool hasData() const        { return testCmdAttrib(HasData); }
1974626Sstever@eecs.umich.edu    bool isReadWrite() const    { return isRead() && isWrite(); }
1986102Sgblack@eecs.umich.edu    bool isLLSC() const         { return testCmdAttrib(IsLlsc); }
1994870Sstever@eecs.umich.edu    bool isError() const        { return testCmdAttrib(IsError); }
2005314Sstever@gmail.com    bool isPrint() const        { return testCmdAttrib(IsPrint); }
2018184Ssomayeh@cs.wisc.edu    bool isFlush() const        { return testCmdAttrib(IsFlush); }
2024022Sstever@eecs.umich.edu
2035735Snate@binkert.org    const Command
2045735Snate@binkert.org    responseCommand() const
2055735Snate@binkert.org    {
2064022Sstever@eecs.umich.edu        return commandInfo[cmd].response;
2074022Sstever@eecs.umich.edu    }
2084022Sstever@eecs.umich.edu
2095735Snate@binkert.org    /// Return the string to a cmd given by idx.
2105735Snate@binkert.org    const std::string &toString() const { return commandInfo[cmd].str; }
2114022Sstever@eecs.umich.edu    int toInt() const { return (int)cmd; }
2124022Sstever@eecs.umich.edu
2135735Snate@binkert.org    MemCmd(Command _cmd) : cmd(_cmd) { }
2145735Snate@binkert.org    MemCmd(int _cmd) : cmd((Command)_cmd) { }
2155735Snate@binkert.org    MemCmd() : cmd(InvalidCmd) { }
2164022Sstever@eecs.umich.edu
2175735Snate@binkert.org    bool operator==(MemCmd c2) const { return (cmd == c2.cmd); }
2185735Snate@binkert.org    bool operator!=(MemCmd c2) const { return (cmd != c2.cmd); }
2194022Sstever@eecs.umich.edu};
2204022Sstever@eecs.umich.edu
2212381SN/A/**
2222662Sstever@eecs.umich.edu * A Packet is used to encapsulate a transfer between two objects in
2232662Sstever@eecs.umich.edu * the memory system (e.g., the L1 and L2 cache).  (In contrast, a
2242662Sstever@eecs.umich.edu * single Request travels all the way from the requester to the
2252662Sstever@eecs.umich.edu * ultimate destination and back, possibly being conveyed by several
2262662Sstever@eecs.umich.edu * different Packets along the way.)
2272381SN/A */
2289044SAli.Saidi@ARM.comclass Packet : public Printable
2292381SN/A{
2302813Srdreslin@umich.edu  public:
2315735Snate@binkert.org    typedef uint32_t FlagsType;
2325735Snate@binkert.org    typedef ::Flags<FlagsType> Flags;
2334022Sstever@eecs.umich.edu
2345735Snate@binkert.org  private:
2355735Snate@binkert.org    static const FlagsType PUBLIC_FLAGS           = 0x00000000;
2365735Snate@binkert.org    static const FlagsType PRIVATE_FLAGS          = 0x00007F0F;
2375735Snate@binkert.org    static const FlagsType COPY_FLAGS             = 0x0000000F;
2385735Snate@binkert.org
2395735Snate@binkert.org    static const FlagsType SHARED                 = 0x00000001;
2405735Snate@binkert.org    // Special control flags
2415735Snate@binkert.org    /// Special timing-mode atomic snoop for multi-level coherence.
2425735Snate@binkert.org    static const FlagsType EXPRESS_SNOOP          = 0x00000002;
2435735Snate@binkert.org    /// Does supplier have exclusive copy?
2445735Snate@binkert.org    /// Useful for multi-level coherence.
2455735Snate@binkert.org    static const FlagsType SUPPLY_EXCLUSIVE       = 0x00000004;
2465735Snate@binkert.org    // Snoop response flags
2475735Snate@binkert.org    static const FlagsType MEM_INHIBIT            = 0x00000008;
2485735Snate@binkert.org    /// Are the 'addr' and 'size' fields valid?
2495735Snate@binkert.org    static const FlagsType VALID_ADDR             = 0x00000100;
2505735Snate@binkert.org    static const FlagsType VALID_SIZE             = 0x00000200;
2515735Snate@binkert.org    /// Is the data pointer set to a value that shouldn't be freed
2525735Snate@binkert.org    /// when the packet is destroyed?
2535735Snate@binkert.org    static const FlagsType STATIC_DATA            = 0x00001000;
2545735Snate@binkert.org    /// The data pointer points to a value that should be freed when
2555735Snate@binkert.org    /// the packet is destroyed.
2565735Snate@binkert.org    static const FlagsType DYNAMIC_DATA           = 0x00002000;
2575735Snate@binkert.org    /// the data pointer points to an array (thus delete []) needs to
2585735Snate@binkert.org    /// be called on it rather than simply delete.
2595735Snate@binkert.org    static const FlagsType ARRAY_DATA             = 0x00004000;
2608436SBrad.Beckmann@amd.com    /// suppress the error if this packet encounters a functional
2618436SBrad.Beckmann@amd.com    /// access failure.
2628436SBrad.Beckmann@amd.com    static const FlagsType SUPPRESS_FUNC_ERROR    = 0x00008000;
2635735Snate@binkert.org
2645735Snate@binkert.org    Flags flags;
2655735Snate@binkert.org
2665735Snate@binkert.org  public:
2674022Sstever@eecs.umich.edu    typedef MemCmd::Command Command;
2684022Sstever@eecs.umich.edu
2695735Snate@binkert.org    /// The command field of the packet.
2704870Sstever@eecs.umich.edu    MemCmd cmd;
2714870Sstever@eecs.umich.edu
2725735Snate@binkert.org    /// A pointer to the original request.
2734870Sstever@eecs.umich.edu    RequestPtr req;
2744870Sstever@eecs.umich.edu
2752566SN/A  private:
2765735Snate@binkert.org   /**
2775735Snate@binkert.org    * A pointer to the data being transfered.  It can be differnt
2785735Snate@binkert.org    * sizes at each level of the heirarchy so it belongs in the
2795735Snate@binkert.org    * packet, not request. This may or may not be populated when a
2805735Snate@binkert.org    * responder recieves the packet. If not populated it memory should
2815735Snate@binkert.org    * be allocated.
2822566SN/A    */
2832566SN/A    PacketDataPtr data;
2842566SN/A
2855735Snate@binkert.org    /// The address of the request.  This address could be virtual or
2865735Snate@binkert.org    /// physical, depending on the system configuration.
2872381SN/A    Addr addr;
2882381SN/A
2895735Snate@binkert.org    /// The size of the request or transfer.
2906227Snate@binkert.org    unsigned size;
2912381SN/A
2925735Snate@binkert.org    /**
2939030Sandreas.hansson@arm.com     * Source port identifier set on a request packet to enable
2949030Sandreas.hansson@arm.com     * appropriate routing of the responses. The source port
2959030Sandreas.hansson@arm.com     * identifier is set by any multiplexing component, e.g. a bus, as
2969030Sandreas.hansson@arm.com     * the timing responses need this information to be routed back to
2979030Sandreas.hansson@arm.com     * the appropriate port at a later point in time. The field can be
2989030Sandreas.hansson@arm.com     * updated (over-written) as the request packet passes through
2999030Sandreas.hansson@arm.com     * additional multiplexing components, and it is their
3009030Sandreas.hansson@arm.com     * responsibility to remember the original source port identifier,
3019030Sandreas.hansson@arm.com     * for example by using an appropriate sender state. The latter is
3029030Sandreas.hansson@arm.com     * done in the cache and bridge.
3035735Snate@binkert.org     */
3049031Sandreas.hansson@arm.com    PortID src;
3052381SN/A
3065735Snate@binkert.org    /**
3079030Sandreas.hansson@arm.com     * Destination port identifier that is present on all response
3089030Sandreas.hansson@arm.com     * packets that passed through a multiplexing component as a
3099030Sandreas.hansson@arm.com     * request packet. The source port identifier is turned into a
3109030Sandreas.hansson@arm.com     * destination port identifier when the packet is turned into a
3119030Sandreas.hansson@arm.com     * response, and the destination is used, e.g. by the bus, to
3129030Sandreas.hansson@arm.com     * select the appropriate path through the interconnect.
3135735Snate@binkert.org     */
3149031Sandreas.hansson@arm.com    PortID dest;
3152641Sstever@eecs.umich.edu
3165735Snate@binkert.org    /**
3175735Snate@binkert.org     * The original value of the command field.  Only valid when the
3184870Sstever@eecs.umich.edu     * current command field is an error condition; in that case, the
3194870Sstever@eecs.umich.edu     * previous contents of the command field are copied here.  This
3204870Sstever@eecs.umich.edu     * field is *not* set on non-error responses.
3214870Sstever@eecs.umich.edu     */
3224870Sstever@eecs.umich.edu    MemCmd origCmd;
3234870Sstever@eecs.umich.edu
3248668Sgeoffrey.blake@arm.com    /**
3258668Sgeoffrey.blake@arm.com     * These values specify the range of bytes found that satisfy a
3268668Sgeoffrey.blake@arm.com     * functional read.
3278668Sgeoffrey.blake@arm.com     */
3288668Sgeoffrey.blake@arm.com    uint16_t bytesValidStart;
3298668Sgeoffrey.blake@arm.com    uint16_t bytesValidEnd;
3308668Sgeoffrey.blake@arm.com
3312641Sstever@eecs.umich.edu  public:
3322811Srdreslin@umich.edu
3335735Snate@binkert.org    /// The time at which the packet will be fully transmitted
3343218Sgblack@eecs.umich.edu    Tick finishTime;
3353218Sgblack@eecs.umich.edu
3365735Snate@binkert.org    /// The time at which the first chunk of the packet will be transmitted
3373218Sgblack@eecs.umich.edu    Tick firstWordTime;
3383218Sgblack@eecs.umich.edu
3395735Snate@binkert.org    /**
3405735Snate@binkert.org     * A virtual base opaque structure used to hold state associated
3419542Sandreas.hansson@arm.com     * with the packet (e.g., an MSHR), specific to a MemObject that
3429542Sandreas.hansson@arm.com     * sees the packet. A pointer to this state is returned in the
3439542Sandreas.hansson@arm.com     * packet's response so that the MemObject in question can quickly
3449542Sandreas.hansson@arm.com     * look up the state needed to process it. A specific subclass
3459542Sandreas.hansson@arm.com     * would be derived from this to carry state specific to a
3469542Sandreas.hansson@arm.com     * particular sending device.
3479542Sandreas.hansson@arm.com     *
3489542Sandreas.hansson@arm.com     * As multiple MemObjects may add their SenderState throughout the
3499542Sandreas.hansson@arm.com     * memory system, the SenderStates create a stack, where a
3509542Sandreas.hansson@arm.com     * MemObject can add a new Senderstate, as long as the
3519542Sandreas.hansson@arm.com     * predecessing SenderState is restored when the response comes
3529542Sandreas.hansson@arm.com     * back. For this reason, the predecessor should always be
3539542Sandreas.hansson@arm.com     * populated with the current SenderState of a packet before
3549542Sandreas.hansson@arm.com     * modifying the senderState field in the request packet.
3555735Snate@binkert.org     */
3565735Snate@binkert.org    struct SenderState
3575735Snate@binkert.org    {
3589542Sandreas.hansson@arm.com        SenderState* predecessor;
3599542Sandreas.hansson@arm.com        SenderState() : predecessor(NULL) {}
3602641Sstever@eecs.umich.edu        virtual ~SenderState() {}
3612641Sstever@eecs.umich.edu    };
3622641Sstever@eecs.umich.edu
3635315Sstever@gmail.com    /**
3645315Sstever@gmail.com     * Object used to maintain state of a PrintReq.  The senderState
3655315Sstever@gmail.com     * field of a PrintReq should always be of this type.
3665315Sstever@gmail.com     */
3679044SAli.Saidi@ARM.com    class PrintReqState : public SenderState
3685735Snate@binkert.org    {
3695735Snate@binkert.org      private:
3705735Snate@binkert.org        /**
3715735Snate@binkert.org         * An entry in the label stack.
3725735Snate@binkert.org         */
3735735Snate@binkert.org        struct LabelStackEntry
3745735Snate@binkert.org        {
3755314Sstever@gmail.com            const std::string label;
3765314Sstever@gmail.com            std::string *prefix;
3775314Sstever@gmail.com            bool labelPrinted;
3785735Snate@binkert.org            LabelStackEntry(const std::string &_label, std::string *_prefix);
3795314Sstever@gmail.com        };
3805314Sstever@gmail.com
3815314Sstever@gmail.com        typedef std::list<LabelStackEntry> LabelStack;
3825314Sstever@gmail.com        LabelStack labelStack;
3835314Sstever@gmail.com
3845314Sstever@gmail.com        std::string *curPrefixPtr;
3855314Sstever@gmail.com
3865314Sstever@gmail.com      public:
3875314Sstever@gmail.com        std::ostream &os;
3885314Sstever@gmail.com        const int verbosity;
3895314Sstever@gmail.com
3905314Sstever@gmail.com        PrintReqState(std::ostream &os, int verbosity = 0);
3915314Sstever@gmail.com        ~PrintReqState();
3925314Sstever@gmail.com
3935735Snate@binkert.org        /**
3945735Snate@binkert.org         * Returns the current line prefix.
3955735Snate@binkert.org         */
3965314Sstever@gmail.com        const std::string &curPrefix() { return *curPrefixPtr; }
3975315Sstever@gmail.com
3985735Snate@binkert.org        /**
3995735Snate@binkert.org         * Push a label onto the label stack, and prepend the given
4005315Sstever@gmail.com         * prefix string onto the current prefix.  Labels will only be
4015735Snate@binkert.org         * printed if an object within the label's scope is printed.
4025735Snate@binkert.org         */
4035314Sstever@gmail.com        void pushLabel(const std::string &lbl,
4045314Sstever@gmail.com                       const std::string &prefix = "  ");
4055735Snate@binkert.org
4065735Snate@binkert.org        /**
4075735Snate@binkert.org         * Pop a label off the label stack.
4085735Snate@binkert.org         */
4095314Sstever@gmail.com        void popLabel();
4105735Snate@binkert.org
4115735Snate@binkert.org        /**
4125735Snate@binkert.org         * Print all of the pending unprinted labels on the
4135315Sstever@gmail.com         * stack. Called by printObj(), so normally not called by
4145735Snate@binkert.org         * users unless bypassing printObj().
4155735Snate@binkert.org         */
4165314Sstever@gmail.com        void printLabels();
4175735Snate@binkert.org
4185735Snate@binkert.org        /**
4195735Snate@binkert.org         * Print a Printable object to os, because it matched the
4205735Snate@binkert.org         * address on a PrintReq.
4215735Snate@binkert.org         */
4225314Sstever@gmail.com        void printObj(Printable *obj);
4235314Sstever@gmail.com    };
4245314Sstever@gmail.com
4255735Snate@binkert.org    /**
4265735Snate@binkert.org     * This packet's sender state.  Devices should use dynamic_cast<>
4275735Snate@binkert.org     * to cast to the state appropriate to the sender.  The intent of
4285735Snate@binkert.org     * this variable is to allow a device to attach extra information
4299542Sandreas.hansson@arm.com     * to a request. A response packet must return the sender state
4305735Snate@binkert.org     * that was attached to the original request (even if a new packet
4315735Snate@binkert.org     * is created).
4325735Snate@binkert.org     */
4332662Sstever@eecs.umich.edu    SenderState *senderState;
4342641Sstever@eecs.umich.edu
4359542Sandreas.hansson@arm.com    /**
4369542Sandreas.hansson@arm.com     * Push a new sender state to the packet and make the current
4379542Sandreas.hansson@arm.com     * sender state the predecessor of the new one. This should be
4389542Sandreas.hansson@arm.com     * prefered over direct manipulation of the senderState member
4399542Sandreas.hansson@arm.com     * variable.
4409542Sandreas.hansson@arm.com     *
4419542Sandreas.hansson@arm.com     * @param sender_state SenderState to push at the top of the stack
4429542Sandreas.hansson@arm.com     */
4439542Sandreas.hansson@arm.com    void pushSenderState(SenderState *sender_state);
4449542Sandreas.hansson@arm.com
4459542Sandreas.hansson@arm.com    /**
4469542Sandreas.hansson@arm.com     * Pop the top of the state stack and return a pointer to it. This
4479542Sandreas.hansson@arm.com     * assumes the current sender state is not NULL. This should be
4489542Sandreas.hansson@arm.com     * preferred over direct manipulation of the senderState member
4499542Sandreas.hansson@arm.com     * variable.
4509542Sandreas.hansson@arm.com     *
4519542Sandreas.hansson@arm.com     * @return The current top of the stack
4529542Sandreas.hansson@arm.com     */
4539542Sandreas.hansson@arm.com    SenderState *popSenderState();
4549542Sandreas.hansson@arm.com
4559543Ssascha.bischoff@arm.com    /**
4569543Ssascha.bischoff@arm.com     * Go through the sender state stack and return the first instance
4579543Ssascha.bischoff@arm.com     * that is of type T (as determined by a dynamic_cast). If there
4589543Ssascha.bischoff@arm.com     * is no sender state of type T, NULL is returned.
4599543Ssascha.bischoff@arm.com     *
4609543Ssascha.bischoff@arm.com     * @return The topmost state of type T
4619543Ssascha.bischoff@arm.com     */
4629543Ssascha.bischoff@arm.com    template <typename T>
4639543Ssascha.bischoff@arm.com    T * findNextSenderState() const
4649543Ssascha.bischoff@arm.com    {
4659543Ssascha.bischoff@arm.com        T *t = NULL;
4669543Ssascha.bischoff@arm.com        SenderState* sender_state = senderState;
4679543Ssascha.bischoff@arm.com        while (t == NULL && sender_state != NULL) {
4689543Ssascha.bischoff@arm.com            t = dynamic_cast<T*>(sender_state);
4699543Ssascha.bischoff@arm.com            sender_state = sender_state->predecessor;
4709543Ssascha.bischoff@arm.com        }
4719543Ssascha.bischoff@arm.com        return t;
4729543Ssascha.bischoff@arm.com    }
4739543Ssascha.bischoff@arm.com
4745735Snate@binkert.org    /// Return the string name of the cmd field (for debugging and
4755735Snate@binkert.org    /// tracing).
4764022Sstever@eecs.umich.edu    const std::string &cmdString() const { return cmd.toString(); }
4772811Srdreslin@umich.edu
4785735Snate@binkert.org    /// Return the index of this command.
4794022Sstever@eecs.umich.edu    inline int cmdToIndex() const { return cmd.toInt(); }
4802811Srdreslin@umich.edu
4814022Sstever@eecs.umich.edu    bool isRead() const         { return cmd.isRead(); }
4827465Ssteve.reinhardt@amd.com    bool isWrite() const        { return cmd.isWrite(); }
4837465Ssteve.reinhardt@amd.com    bool isUpgrade()  const     { return cmd.isUpgrade(); }
4844022Sstever@eecs.umich.edu    bool isRequest() const      { return cmd.isRequest(); }
4854022Sstever@eecs.umich.edu    bool isResponse() const     { return cmd.isResponse(); }
4864870Sstever@eecs.umich.edu    bool needsExclusive() const { return cmd.needsExclusive(); }
4874022Sstever@eecs.umich.edu    bool needsResponse() const  { return cmd.needsResponse(); }
4884022Sstever@eecs.umich.edu    bool isInvalidate() const   { return cmd.isInvalidate(); }
4894022Sstever@eecs.umich.edu    bool hasData() const        { return cmd.hasData(); }
4904040Ssaidi@eecs.umich.edu    bool isReadWrite() const    { return cmd.isReadWrite(); }
4916102Sgblack@eecs.umich.edu    bool isLLSC() const         { return cmd.isLLSC(); }
4924870Sstever@eecs.umich.edu    bool isError() const        { return cmd.isError(); }
4935314Sstever@gmail.com    bool isPrint() const        { return cmd.isPrint(); }
4948184Ssomayeh@cs.wisc.edu    bool isFlush() const        { return cmd.isFlush(); }
4952812Srdreslin@umich.edu
4964870Sstever@eecs.umich.edu    // Snoop flags
4975735Snate@binkert.org    void assertMemInhibit()     { flags.set(MEM_INHIBIT); }
4985764Snate@binkert.org    bool memInhibitAsserted()   { return flags.isSet(MEM_INHIBIT); }
4995735Snate@binkert.org    void assertShared()         { flags.set(SHARED); }
5005764Snate@binkert.org    bool sharedAsserted()       { return flags.isSet(SHARED); }
5014870Sstever@eecs.umich.edu
5024895Sstever@eecs.umich.edu    // Special control flags
5035735Snate@binkert.org    void setExpressSnoop()      { flags.set(EXPRESS_SNOOP); }
5045764Snate@binkert.org    bool isExpressSnoop()       { return flags.isSet(EXPRESS_SNOOP); }
5055735Snate@binkert.org    void setSupplyExclusive()   { flags.set(SUPPLY_EXCLUSIVE); }
5067687Ssteve.reinhardt@amd.com    void clearSupplyExclusive() { flags.clear(SUPPLY_EXCLUSIVE); }
5075764Snate@binkert.org    bool isSupplyExclusive()    { return flags.isSet(SUPPLY_EXCLUSIVE); }
5088436SBrad.Beckmann@amd.com    void setSuppressFuncError() { flags.set(SUPPRESS_FUNC_ERROR); }
5098436SBrad.Beckmann@amd.com    bool suppressFuncError()    { return flags.isSet(SUPPRESS_FUNC_ERROR); }
5104895Sstever@eecs.umich.edu
5114870Sstever@eecs.umich.edu    // Network error conditions... encapsulate them as methods since
5124870Sstever@eecs.umich.edu    // their encoding keeps changing (from result field to command
5134870Sstever@eecs.umich.edu    // field, etc.)
5145735Snate@binkert.org    void
5155735Snate@binkert.org    setBadAddress()
5165735Snate@binkert.org    {
5175735Snate@binkert.org        assert(isResponse());
5185735Snate@binkert.org        cmd = MemCmd::BadAddressError;
5195735Snate@binkert.org    }
5205735Snate@binkert.org
5215735Snate@binkert.org    bool hadBadAddress() const { return cmd == MemCmd::BadAddressError; }
5224986Ssaidi@eecs.umich.edu    void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
5232814Srdreslin@umich.edu
5249031Sandreas.hansson@arm.com    bool isSrcValid() const { return src != InvalidPortID; }
5255735Snate@binkert.org    /// Accessor function to get the source index of the packet.
5269031Sandreas.hansson@arm.com    PortID getSrc() const { assert(isSrcValid()); return src; }
5275735Snate@binkert.org    /// Accessor function to set the source index of the packet.
5289031Sandreas.hansson@arm.com    void setSrc(PortID _src) { src = _src; }
5295735Snate@binkert.org    /// Reset source field, e.g. to retransmit packet on different bus.
5309031Sandreas.hansson@arm.com    void clearSrc() { src = InvalidPortID; }
5312641Sstever@eecs.umich.edu
5329031Sandreas.hansson@arm.com    bool isDestValid() const { return dest != InvalidPortID; }
5335735Snate@binkert.org    /// Accessor function for the destination index of the packet.
5349031Sandreas.hansson@arm.com    PortID getDest() const { assert(isDestValid()); return dest; }
5355735Snate@binkert.org    /// Accessor function to set the destination index of the packet.
5369031Sandreas.hansson@arm.com    void setDest(PortID _dest) { dest = _dest; }
5378949Sandreas.hansson@arm.com    /// Reset destination field, e.g. to turn a response into a request again.
5389031Sandreas.hansson@arm.com    void clearDest() { dest = InvalidPortID; }
5392381SN/A
5405764Snate@binkert.org    Addr getAddr() const { assert(flags.isSet(VALID_ADDR)); return addr; }
5419259SAli.Saidi@ARM.com    /**
5429259SAli.Saidi@ARM.com     * Update the address of this packet mid-transaction. This is used
5439259SAli.Saidi@ARM.com     * by the address mapper to change an already set address to a new
5449259SAli.Saidi@ARM.com     * one based on the system configuration. It is intended to remap
5459259SAli.Saidi@ARM.com     * an existing address, so it asserts that the current address is
5469259SAli.Saidi@ARM.com     * valid.
5479259SAli.Saidi@ARM.com     */
5489259SAli.Saidi@ARM.com    void setAddr(Addr _addr) { assert(flags.isSet(VALID_ADDR)); addr = _addr; }
5499259SAli.Saidi@ARM.com
5506227Snate@binkert.org    unsigned getSize() const  { assert(flags.isSet(VALID_SIZE)); return size; }
5515735Snate@binkert.org    Addr getOffset(int blkSize) const { return getAddr() & (Addr)(blkSize - 1); }
5522549SN/A
5535735Snate@binkert.org    /**
5547550SBrad.Beckmann@amd.com     * It has been determined that the SC packet should successfully update
5557550SBrad.Beckmann@amd.com     * memory.  Therefore, convert this SC packet to a normal write.
5567550SBrad.Beckmann@amd.com     */
5577550SBrad.Beckmann@amd.com    void
5587550SBrad.Beckmann@amd.com    convertScToWrite()
5597550SBrad.Beckmann@amd.com    {
5607550SBrad.Beckmann@amd.com        assert(isLLSC());
5617550SBrad.Beckmann@amd.com        assert(isWrite());
5627550SBrad.Beckmann@amd.com        cmd = MemCmd::WriteReq;
5637550SBrad.Beckmann@amd.com    }
5647550SBrad.Beckmann@amd.com
5657550SBrad.Beckmann@amd.com    /**
5667550SBrad.Beckmann@amd.com     * When ruby is in use, Ruby will monitor the cache line and thus M5
5677550SBrad.Beckmann@amd.com     * phys memory should treat LL ops as normal reads.
5687550SBrad.Beckmann@amd.com     */
5697550SBrad.Beckmann@amd.com    void
5707550SBrad.Beckmann@amd.com    convertLlToRead()
5717550SBrad.Beckmann@amd.com    {
5727550SBrad.Beckmann@amd.com        assert(isLLSC());
5737550SBrad.Beckmann@amd.com        assert(isRead());
5747550SBrad.Beckmann@amd.com        cmd = MemCmd::ReadReq;
5757550SBrad.Beckmann@amd.com    }
5767550SBrad.Beckmann@amd.com
5777550SBrad.Beckmann@amd.com    /**
5785735Snate@binkert.org     * Constructor.  Note that a Request object must be constructed
5795735Snate@binkert.org     * first, but the Requests's physical address and size fields need
5809030Sandreas.hansson@arm.com     * not be valid. The command must be supplied.
5815735Snate@binkert.org     */
5828949Sandreas.hansson@arm.com    Packet(Request *_req, MemCmd _cmd)
5838949Sandreas.hansson@arm.com        :  cmd(_cmd), req(_req), data(NULL),
5849031Sandreas.hansson@arm.com           src(InvalidPortID), dest(InvalidPortID),
5858949Sandreas.hansson@arm.com           bytesValidStart(0), bytesValidEnd(0),
5869546Sandreas.hansson@arm.com           senderState(NULL)
5872641Sstever@eecs.umich.edu    {
5886104Ssteve.reinhardt@amd.com        if (req->hasPaddr()) {
5896104Ssteve.reinhardt@amd.com            addr = req->getPaddr();
5906104Ssteve.reinhardt@amd.com            flags.set(VALID_ADDR);
5916104Ssteve.reinhardt@amd.com        }
5926104Ssteve.reinhardt@amd.com        if (req->hasSize()) {
5936104Ssteve.reinhardt@amd.com            size = req->getSize();
5946104Ssteve.reinhardt@amd.com            flags.set(VALID_SIZE);
5956104Ssteve.reinhardt@amd.com        }
5962813Srdreslin@umich.edu    }
5972813Srdreslin@umich.edu
5985735Snate@binkert.org    /**
5995735Snate@binkert.org     * Alternate constructor if you are trying to create a packet with
6005735Snate@binkert.org     * a request that is for a whole block, not the address from the
6015735Snate@binkert.org     * req.  this allows for overriding the size/addr of the req.
6025735Snate@binkert.org     */
6038949Sandreas.hansson@arm.com    Packet(Request *_req, MemCmd _cmd, int _blkSize)
6048949Sandreas.hansson@arm.com        :  cmd(_cmd), req(_req), data(NULL),
6059031Sandreas.hansson@arm.com           src(InvalidPortID), dest(InvalidPortID),
6068949Sandreas.hansson@arm.com           bytesValidStart(0), bytesValidEnd(0),
6079546Sandreas.hansson@arm.com           senderState(NULL)
6082813Srdreslin@umich.edu    {
6096104Ssteve.reinhardt@amd.com        if (req->hasPaddr()) {
6106104Ssteve.reinhardt@amd.com            addr = req->getPaddr() & ~(_blkSize - 1);
6116104Ssteve.reinhardt@amd.com            flags.set(VALID_ADDR);
6126104Ssteve.reinhardt@amd.com        }
6136104Ssteve.reinhardt@amd.com        size = _blkSize;
6146104Ssteve.reinhardt@amd.com        flags.set(VALID_SIZE);
6154626Sstever@eecs.umich.edu    }
6164626Sstever@eecs.umich.edu
6175735Snate@binkert.org    /**
6185735Snate@binkert.org     * Alternate constructor for copying a packet.  Copy all fields
6194887Sstever@eecs.umich.edu     * *except* if the original packet's data was dynamic, don't copy
6204887Sstever@eecs.umich.edu     * that, as we can't guarantee that the new packet's lifetime is
6214887Sstever@eecs.umich.edu     * less than that of the original packet.  In this case the new
6225735Snate@binkert.org     * packet should allocate its own data.
6235735Snate@binkert.org     */
6245735Snate@binkert.org    Packet(Packet *pkt, bool clearFlags = false)
6255735Snate@binkert.org        :  cmd(pkt->cmd), req(pkt->req),
6265764Snate@binkert.org           data(pkt->flags.isSet(STATIC_DATA) ? pkt->data : NULL),
6275735Snate@binkert.org           addr(pkt->addr), size(pkt->size), src(pkt->src), dest(pkt->dest),
6288668Sgeoffrey.blake@arm.com           bytesValidStart(pkt->bytesValidStart), bytesValidEnd(pkt->bytesValidEnd),
6299546Sandreas.hansson@arm.com           senderState(pkt->senderState)
6304626Sstever@eecs.umich.edu    {
6315735Snate@binkert.org        if (!clearFlags)
6325735Snate@binkert.org            flags.set(pkt->flags & COPY_FLAGS);
6335735Snate@binkert.org
6349031Sandreas.hansson@arm.com        flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE));
6355735Snate@binkert.org        flags.set(pkt->flags & STATIC_DATA);
6368668Sgeoffrey.blake@arm.com
6372641Sstever@eecs.umich.edu    }
6382549SN/A
6395735Snate@binkert.org    /**
6405735Snate@binkert.org     * clean up packet variables
6415735Snate@binkert.org     */
6422566SN/A    ~Packet()
6435387Sstever@gmail.com    {
6445387Sstever@gmail.com        // If this is a request packet for which there's no response,
6455387Sstever@gmail.com        // delete the request object here, since the requester will
6465387Sstever@gmail.com        // never get the chance.
6475387Sstever@gmail.com        if (req && isRequest() && !needsResponse())
6485387Sstever@gmail.com            delete req;
6495735Snate@binkert.org        deleteData();
6505387Sstever@gmail.com    }
6512566SN/A
6525735Snate@binkert.org    /**
6535735Snate@binkert.org     * Reinitialize packet address and size from the associated
6545735Snate@binkert.org     * Request object, and reset other fields that may have been
6555735Snate@binkert.org     * modified by a previous transaction.  Typically called when a
6565735Snate@binkert.org     * statically allocated Request/Packet pair is reused for multiple
6575735Snate@binkert.org     * transactions.
6585735Snate@binkert.org     */
6595735Snate@binkert.org    void
6605735Snate@binkert.org    reinitFromRequest()
6615735Snate@binkert.org    {
6626104Ssteve.reinhardt@amd.com        assert(req->hasPaddr());
6634870Sstever@eecs.umich.edu        flags = 0;
6646104Ssteve.reinhardt@amd.com        addr = req->getPaddr();
6656104Ssteve.reinhardt@amd.com        size = req->getSize();
6665735Snate@binkert.org
6675735Snate@binkert.org        flags.set(VALID_ADDR|VALID_SIZE);
6685735Snate@binkert.org        deleteData();
6692662Sstever@eecs.umich.edu    }
6702566SN/A
6714626Sstever@eecs.umich.edu    /**
6724626Sstever@eecs.umich.edu     * Take a request packet and modify it in place to be suitable for
6739030Sandreas.hansson@arm.com     * returning as a response to that request. The source field is
6749030Sandreas.hansson@arm.com     * turned into the destination, and subsequently cleared. Note
6759030Sandreas.hansson@arm.com     * that the latter is not necessary for atomic requests, but
6769030Sandreas.hansson@arm.com     * causes no harm as neither field is valid.
6774626Sstever@eecs.umich.edu     */
6785735Snate@binkert.org    void
6795735Snate@binkert.org    makeResponse()
6804626Sstever@eecs.umich.edu    {
6812662Sstever@eecs.umich.edu        assert(needsResponse());
6822855Srdreslin@umich.edu        assert(isRequest());
6834986Ssaidi@eecs.umich.edu        origCmd = cmd;
6844022Sstever@eecs.umich.edu        cmd = cmd.responseCommand();
6855745Snate@binkert.org
6867464Ssteve.reinhardt@amd.com        // responses are never express, even if the snoop that
6877464Ssteve.reinhardt@amd.com        // triggered them was
6887464Ssteve.reinhardt@amd.com        flags.clear(EXPRESS_SNOOP);
6897464Ssteve.reinhardt@amd.com
6905745Snate@binkert.org        dest = src;
6919031Sandreas.hansson@arm.com        clearSrc();
6922641Sstever@eecs.umich.edu    }
6932641Sstever@eecs.umich.edu
6945735Snate@binkert.org    void
6955735Snate@binkert.org    makeAtomicResponse()
6964870Sstever@eecs.umich.edu    {
6974870Sstever@eecs.umich.edu        makeResponse();
6984870Sstever@eecs.umich.edu    }
6994870Sstever@eecs.umich.edu
7005735Snate@binkert.org    void
7015735Snate@binkert.org    makeTimingResponse()
7023348Sbinkertn@umich.edu    {
7034870Sstever@eecs.umich.edu        makeResponse();
7043135Srdreslin@umich.edu    }
7053135Srdreslin@umich.edu
7068436SBrad.Beckmann@amd.com    void
7078436SBrad.Beckmann@amd.com    setFunctionalResponseStatus(bool success)
7088436SBrad.Beckmann@amd.com    {
7098436SBrad.Beckmann@amd.com        if (!success) {
7108436SBrad.Beckmann@amd.com            if (isWrite()) {
7118436SBrad.Beckmann@amd.com                cmd = MemCmd::FunctionalWriteError;
7128436SBrad.Beckmann@amd.com            } else {
7138436SBrad.Beckmann@amd.com                cmd = MemCmd::FunctionalReadError;
7148436SBrad.Beckmann@amd.com            }
7158436SBrad.Beckmann@amd.com        }
7168436SBrad.Beckmann@amd.com    }
7178436SBrad.Beckmann@amd.com
7187006Snate@binkert.org    void
7197006Snate@binkert.org    setSize(unsigned size)
7207006Snate@binkert.org    {
7217006Snate@binkert.org        assert(!flags.isSet(VALID_SIZE));
7227006Snate@binkert.org
7237006Snate@binkert.org        this->size = size;
7247006Snate@binkert.org        flags.set(VALID_SIZE);
7257006Snate@binkert.org    }
7267006Snate@binkert.org
7272685Ssaidi@eecs.umich.edu
7283348Sbinkertn@umich.edu    /**
7293348Sbinkertn@umich.edu     * Set the data pointer to the following value that should not be
7303348Sbinkertn@umich.edu     * freed.
7312566SN/A     */
7322566SN/A    template <typename T>
7333348Sbinkertn@umich.edu    void
7343348Sbinkertn@umich.edu    dataStatic(T *p)
7353348Sbinkertn@umich.edu    {
7365764Snate@binkert.org        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
7373348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
7385735Snate@binkert.org        flags.set(STATIC_DATA);
7393348Sbinkertn@umich.edu    }
7402566SN/A
7413348Sbinkertn@umich.edu    /**
7423348Sbinkertn@umich.edu     * Set the data pointer to a value that should have delete []
7433348Sbinkertn@umich.edu     * called on it.
7443348Sbinkertn@umich.edu     */
7452566SN/A    template <typename T>
7463348Sbinkertn@umich.edu    void
7473348Sbinkertn@umich.edu    dataDynamicArray(T *p)
7483348Sbinkertn@umich.edu    {
7495764Snate@binkert.org        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
7503348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
7515735Snate@binkert.org        flags.set(DYNAMIC_DATA|ARRAY_DATA);
7523348Sbinkertn@umich.edu    }
7533348Sbinkertn@umich.edu
7543348Sbinkertn@umich.edu    /**
7553348Sbinkertn@umich.edu     * set the data pointer to a value that should have delete called
7563348Sbinkertn@umich.edu     * on it.
7573348Sbinkertn@umich.edu     */
7583348Sbinkertn@umich.edu    template <typename T>
7593348Sbinkertn@umich.edu    void
7603348Sbinkertn@umich.edu    dataDynamic(T *p)
7613348Sbinkertn@umich.edu    {
7625764Snate@binkert.org        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
7633348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
7645735Snate@binkert.org        flags.set(DYNAMIC_DATA);
7653348Sbinkertn@umich.edu    }
7663348Sbinkertn@umich.edu
7675735Snate@binkert.org    /**
7685735Snate@binkert.org     * get a pointer to the data ptr.
7695735Snate@binkert.org     */
7703348Sbinkertn@umich.edu    template <typename T>
7713348Sbinkertn@umich.edu    T*
7727915SBrad.Beckmann@amd.com    getPtr(bool null_ok = false)
7733348Sbinkertn@umich.edu    {
7747915SBrad.Beckmann@amd.com        assert(null_ok || flags.isSet(STATIC_DATA|DYNAMIC_DATA));
7753348Sbinkertn@umich.edu        return (T*)data;
7763348Sbinkertn@umich.edu    }
7772566SN/A
7785735Snate@binkert.org    /**
7795735Snate@binkert.org     * return the value of what is pointed to in the packet.
7805735Snate@binkert.org     */
7812566SN/A    template <typename T>
7822592SN/A    T get();
7832566SN/A
7845735Snate@binkert.org    /**
7855735Snate@binkert.org     * set the value in the data pointer to v.
7865735Snate@binkert.org     */
7872566SN/A    template <typename T>
7882592SN/A    void set(T v);
7892566SN/A
7903348Sbinkertn@umich.edu    /**
7914626Sstever@eecs.umich.edu     * Copy data into the packet from the provided pointer.
7924626Sstever@eecs.umich.edu     */
7935735Snate@binkert.org    void
7945735Snate@binkert.org    setData(uint8_t *p)
7954626Sstever@eecs.umich.edu    {
7967691SAli.Saidi@ARM.com        if (p != getPtr<uint8_t>())
7977691SAli.Saidi@ARM.com            std::memcpy(getPtr<uint8_t>(), p, getSize());
7984626Sstever@eecs.umich.edu    }
7994626Sstever@eecs.umich.edu
8004626Sstever@eecs.umich.edu    /**
8014626Sstever@eecs.umich.edu     * Copy data into the packet from the provided block pointer,
8024626Sstever@eecs.umich.edu     * which is aligned to the given block size.
8034626Sstever@eecs.umich.edu     */
8045735Snate@binkert.org    void
8055735Snate@binkert.org    setDataFromBlock(uint8_t *blk_data, int blkSize)
8064626Sstever@eecs.umich.edu    {
8074626Sstever@eecs.umich.edu        setData(blk_data + getOffset(blkSize));
8084626Sstever@eecs.umich.edu    }
8094626Sstever@eecs.umich.edu
8104626Sstever@eecs.umich.edu    /**
8114626Sstever@eecs.umich.edu     * Copy data from the packet to the provided block pointer, which
8124626Sstever@eecs.umich.edu     * is aligned to the given block size.
8134626Sstever@eecs.umich.edu     */
8145735Snate@binkert.org    void
8155735Snate@binkert.org    writeData(uint8_t *p)
8164626Sstever@eecs.umich.edu    {
8174626Sstever@eecs.umich.edu        std::memcpy(p, getPtr<uint8_t>(), getSize());
8184626Sstever@eecs.umich.edu    }
8194626Sstever@eecs.umich.edu
8204626Sstever@eecs.umich.edu    /**
8214626Sstever@eecs.umich.edu     * Copy data from the packet to the memory at the provided pointer.
8224626Sstever@eecs.umich.edu     */
8235735Snate@binkert.org    void
8245735Snate@binkert.org    writeDataToBlock(uint8_t *blk_data, int blkSize)
8254626Sstever@eecs.umich.edu    {
8264626Sstever@eecs.umich.edu        writeData(blk_data + getOffset(blkSize));
8274626Sstever@eecs.umich.edu    }
8284626Sstever@eecs.umich.edu
8294626Sstever@eecs.umich.edu    /**
8303348Sbinkertn@umich.edu     * delete the data pointed to in the data pointer. Ok to call to
8313348Sbinkertn@umich.edu     * matter how data was allocted.
8323348Sbinkertn@umich.edu     */
8335735Snate@binkert.org    void
8345735Snate@binkert.org    deleteData()
8355735Snate@binkert.org    {
8365764Snate@binkert.org        if (flags.isSet(ARRAY_DATA))
8375735Snate@binkert.org            delete [] data;
8385764Snate@binkert.org        else if (flags.isSet(DYNAMIC_DATA))
8395735Snate@binkert.org            delete data;
8405735Snate@binkert.org
8415735Snate@binkert.org        flags.clear(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA);
8425735Snate@binkert.org        data = NULL;
8435735Snate@binkert.org    }
8442566SN/A
8452566SN/A    /** If there isn't data in the packet, allocate some. */
8465735Snate@binkert.org    void
8475735Snate@binkert.org    allocate()
8485735Snate@binkert.org    {
8495735Snate@binkert.org        if (data) {
8505764Snate@binkert.org            assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
8515745Snate@binkert.org            return;
8525735Snate@binkert.org        }
8535745Snate@binkert.org
8545764Snate@binkert.org        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
8555745Snate@binkert.org        flags.set(DYNAMIC_DATA|ARRAY_DATA);
8565745Snate@binkert.org        data = new uint8_t[getSize()];
8575735Snate@binkert.org    }
8585735Snate@binkert.org
8594626Sstever@eecs.umich.edu    /**
8604626Sstever@eecs.umich.edu     * Check a functional request against a memory value represented
8614626Sstever@eecs.umich.edu     * by a base/size pair and an associated data array.  If the
8624626Sstever@eecs.umich.edu     * functional request is a read, it may be satisfied by the memory
8634626Sstever@eecs.umich.edu     * value.  If the functional request is a write, it may update the
8644626Sstever@eecs.umich.edu     * memory value.
8654626Sstever@eecs.umich.edu     */
8665314Sstever@gmail.com    bool checkFunctional(Printable *obj, Addr base, int size, uint8_t *data);
8674626Sstever@eecs.umich.edu
8684626Sstever@eecs.umich.edu    /**
8694626Sstever@eecs.umich.edu     * Check a functional request against a memory value stored in
8705315Sstever@gmail.com     * another packet (i.e. an in-transit request or response).
8714626Sstever@eecs.umich.edu     */
8725735Snate@binkert.org    bool
8735735Snate@binkert.org    checkFunctional(PacketPtr other)
8745735Snate@binkert.org    {
8755735Snate@binkert.org        uint8_t *data = other->hasData() ? other->getPtr<uint8_t>() : NULL;
8765735Snate@binkert.org        return checkFunctional(other, other->getAddr(), other->getSize(),
8775735Snate@binkert.org                               data);
8784626Sstever@eecs.umich.edu    }
8795314Sstever@gmail.com
8805315Sstever@gmail.com    /**
8815315Sstever@gmail.com     * Push label for PrintReq (safe to call unconditionally).
8825315Sstever@gmail.com     */
8835735Snate@binkert.org    void
8845735Snate@binkert.org    pushLabel(const std::string &lbl)
8855735Snate@binkert.org    {
8865735Snate@binkert.org        if (isPrint())
8875735Snate@binkert.org            safe_cast<PrintReqState*>(senderState)->pushLabel(lbl);
8885314Sstever@gmail.com    }
8895314Sstever@gmail.com
8905315Sstever@gmail.com    /**
8915315Sstever@gmail.com     * Pop label for PrintReq (safe to call unconditionally).
8925315Sstever@gmail.com     */
8935735Snate@binkert.org    void
8945735Snate@binkert.org    popLabel()
8955735Snate@binkert.org    {
8965735Snate@binkert.org        if (isPrint())
8975735Snate@binkert.org            safe_cast<PrintReqState*>(senderState)->popLabel();
8985314Sstever@gmail.com    }
8995314Sstever@gmail.com
9005314Sstever@gmail.com    void print(std::ostream &o, int verbosity = 0,
9015314Sstever@gmail.com               const std::string &prefix = "") const;
9022381SN/A};
9032381SN/A
9042381SN/A#endif //__MEM_PACKET_HH
905