packet.hh revision 10583
12381SN/A/*
210342SCurtis.Dunham@arm.com * Copyright (c) 2012-2014 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,
944022Sstever@eecs.umich.edu        WriteInvalidateReq,
954022Sstever@eecs.umich.edu        WriteInvalidateResp,
964022Sstever@eecs.umich.edu        UpgradeReq,
977465Ssteve.reinhardt@amd.com        SCUpgradeReq,           // Special "weak" upgrade for StoreCond
984628Sstever@eecs.umich.edu        UpgradeResp,
997465Ssteve.reinhardt@amd.com        SCUpgradeFailReq,       // Failed SCUpgradeReq in MSHR (never sent)
1007465Ssteve.reinhardt@amd.com        UpgradeFailResp,        // Valid for SCUpgradeReq only
1014022Sstever@eecs.umich.edu        ReadExReq,
1024022Sstever@eecs.umich.edu        ReadExResp,
1034626Sstever@eecs.umich.edu        LoadLockedReq,
1044626Sstever@eecs.umich.edu        StoreCondReq,
1057669Ssteve.reinhardt@amd.com        StoreCondFailReq,       // Failed StoreCondReq in MSHR (never sent)
1064626Sstever@eecs.umich.edu        StoreCondResp,
1074040Ssaidi@eecs.umich.edu        SwapReq,
1084040Ssaidi@eecs.umich.edu        SwapResp,
1095650Sgblack@eecs.umich.edu        MessageReq,
1105650Sgblack@eecs.umich.edu        MessageResp,
1114870Sstever@eecs.umich.edu        // Error responses
1124870Sstever@eecs.umich.edu        // @TODO these should be classified as responses rather than
1134870Sstever@eecs.umich.edu        // requests; coding them as requests initially for backwards
1144870Sstever@eecs.umich.edu        // compatibility
1154870Sstever@eecs.umich.edu        InvalidDestError,  // packet dest field invalid
1164870Sstever@eecs.umich.edu        BadAddressError,   // memory address invalid
1178436SBrad.Beckmann@amd.com        FunctionalReadError, // unable to fulfill functional read
1188436SBrad.Beckmann@amd.com        FunctionalWriteError, // unable to fulfill functional write
1195314Sstever@gmail.com        // Fake simulator-only commands
1205314Sstever@gmail.com        PrintReq,       // Print state matching address
1218184Ssomayeh@cs.wisc.edu        FlushReq,      //request for a cache flush
1228716Snilay@cs.wisc.edu        InvalidationReq,   // request for address to be invalidated from lsq
1234022Sstever@eecs.umich.edu        NUM_MEM_CMDS
1244022Sstever@eecs.umich.edu    };
1254022Sstever@eecs.umich.edu
1264022Sstever@eecs.umich.edu  private:
1275735Snate@binkert.org    /**
1285735Snate@binkert.org     * List of command attributes.
1295735Snate@binkert.org     */
1304022Sstever@eecs.umich.edu    enum Attribute
1314022Sstever@eecs.umich.edu    {
1324626Sstever@eecs.umich.edu        IsRead,         //!< Data flows from responder to requester
1334626Sstever@eecs.umich.edu        IsWrite,        //!< Data flows from requester to responder
1347465Ssteve.reinhardt@amd.com        IsUpgrade,
1354022Sstever@eecs.umich.edu        IsInvalidate,
1364626Sstever@eecs.umich.edu        NeedsExclusive, //!< Requires exclusive copy to complete in-cache
1374626Sstever@eecs.umich.edu        IsRequest,      //!< Issued by requester
1384626Sstever@eecs.umich.edu        IsResponse,     //!< Issue by responder
1394626Sstever@eecs.umich.edu        NeedsResponse,  //!< Requester needs response from target
1404022Sstever@eecs.umich.edu        IsSWPrefetch,
1414022Sstever@eecs.umich.edu        IsHWPrefetch,
1426076Sgblack@eecs.umich.edu        IsLlsc,         //!< Alpha/MIPS LL or SC access
1434626Sstever@eecs.umich.edu        HasData,        //!< There is an associated payload
1444870Sstever@eecs.umich.edu        IsError,        //!< Error response
1455314Sstever@gmail.com        IsPrint,        //!< Print state matching address (for debugging)
1468184Ssomayeh@cs.wisc.edu        IsFlush,        //!< Flush the address from caches
1474022Sstever@eecs.umich.edu        NUM_COMMAND_ATTRIBUTES
1484022Sstever@eecs.umich.edu    };
1494022Sstever@eecs.umich.edu
1505735Snate@binkert.org    /**
1515735Snate@binkert.org     * Structure that defines attributes and other data associated
1525735Snate@binkert.org     * with a Command.
1535735Snate@binkert.org     */
1545735Snate@binkert.org    struct CommandInfo
1555735Snate@binkert.org    {
1565735Snate@binkert.org        /// Set of attribute flags.
1574022Sstever@eecs.umich.edu        const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
1585735Snate@binkert.org        /// Corresponding response for requests; InvalidCmd if no
1595735Snate@binkert.org        /// response is applicable.
1604022Sstever@eecs.umich.edu        const Command response;
1615735Snate@binkert.org        /// String representation (for printing)
1624022Sstever@eecs.umich.edu        const std::string str;
1634022Sstever@eecs.umich.edu    };
1644022Sstever@eecs.umich.edu
1655735Snate@binkert.org    /// Array to map Command enum to associated info.
1664022Sstever@eecs.umich.edu    static const CommandInfo commandInfo[];
1674022Sstever@eecs.umich.edu
1684022Sstever@eecs.umich.edu  private:
1694022Sstever@eecs.umich.edu
1704022Sstever@eecs.umich.edu    Command cmd;
1714022Sstever@eecs.umich.edu
1725735Snate@binkert.org    bool
1735735Snate@binkert.org    testCmdAttrib(MemCmd::Attribute attrib) const
1745735Snate@binkert.org    {
1754022Sstever@eecs.umich.edu        return commandInfo[cmd].attributes[attrib] != 0;
1764022Sstever@eecs.umich.edu    }
1774022Sstever@eecs.umich.edu
1784022Sstever@eecs.umich.edu  public:
1794022Sstever@eecs.umich.edu
18010583SCurtis.Dunham@arm.com    bool isRead() const            { return testCmdAttrib(IsRead); }
18110583SCurtis.Dunham@arm.com    bool isWrite() const           { return testCmdAttrib(IsWrite); }
18210583SCurtis.Dunham@arm.com    bool isUpgrade() const         { return testCmdAttrib(IsUpgrade); }
18310583SCurtis.Dunham@arm.com    bool isRequest() const         { return testCmdAttrib(IsRequest); }
18410583SCurtis.Dunham@arm.com    bool isResponse() const        { return testCmdAttrib(IsResponse); }
18510583SCurtis.Dunham@arm.com    bool needsExclusive() const    { return testCmdAttrib(NeedsExclusive); }
18610583SCurtis.Dunham@arm.com    bool needsResponse() const     { return testCmdAttrib(NeedsResponse); }
18710583SCurtis.Dunham@arm.com    bool isInvalidate() const      { return testCmdAttrib(IsInvalidate); }
18810583SCurtis.Dunham@arm.com    bool isWriteInvalidate() const { return testCmdAttrib(IsWrite) &&
18910583SCurtis.Dunham@arm.com                                            testCmdAttrib(IsInvalidate); }
19010570Sandreas.hansson@arm.com
19110570Sandreas.hansson@arm.com    /**
19210570Sandreas.hansson@arm.com     * Check if this particular packet type carries payload data. Note
19310570Sandreas.hansson@arm.com     * that this does not reflect if the data pointer of the packet is
19410570Sandreas.hansson@arm.com     * valid or not.
19510570Sandreas.hansson@arm.com     */
1964022Sstever@eecs.umich.edu    bool hasData() const        { return testCmdAttrib(HasData); }
1976102Sgblack@eecs.umich.edu    bool isLLSC() const         { return testCmdAttrib(IsLlsc); }
19810343SCurtis.Dunham@arm.com    bool isSWPrefetch() const   { return testCmdAttrib(IsSWPrefetch); }
19910343SCurtis.Dunham@arm.com    bool isHWPrefetch() const   { return testCmdAttrib(IsHWPrefetch); }
20010343SCurtis.Dunham@arm.com    bool isPrefetch() const     { return testCmdAttrib(IsSWPrefetch) ||
20110343SCurtis.Dunham@arm.com                                         testCmdAttrib(IsHWPrefetch); }
2024870Sstever@eecs.umich.edu    bool isError() const        { return testCmdAttrib(IsError); }
2035314Sstever@gmail.com    bool isPrint() const        { return testCmdAttrib(IsPrint); }
2048184Ssomayeh@cs.wisc.edu    bool isFlush() const        { return testCmdAttrib(IsFlush); }
2054022Sstever@eecs.umich.edu
2065735Snate@binkert.org    const Command
2075735Snate@binkert.org    responseCommand() const
2085735Snate@binkert.org    {
2094022Sstever@eecs.umich.edu        return commandInfo[cmd].response;
2104022Sstever@eecs.umich.edu    }
2114022Sstever@eecs.umich.edu
2125735Snate@binkert.org    /// Return the string to a cmd given by idx.
2135735Snate@binkert.org    const std::string &toString() const { return commandInfo[cmd].str; }
2144022Sstever@eecs.umich.edu    int toInt() const { return (int)cmd; }
2154022Sstever@eecs.umich.edu
2165735Snate@binkert.org    MemCmd(Command _cmd) : cmd(_cmd) { }
2175735Snate@binkert.org    MemCmd(int _cmd) : cmd((Command)_cmd) { }
2185735Snate@binkert.org    MemCmd() : cmd(InvalidCmd) { }
2194022Sstever@eecs.umich.edu
2205735Snate@binkert.org    bool operator==(MemCmd c2) const { return (cmd == c2.cmd); }
2215735Snate@binkert.org    bool operator!=(MemCmd c2) const { return (cmd != c2.cmd); }
2224022Sstever@eecs.umich.edu};
2234022Sstever@eecs.umich.edu
2242381SN/A/**
2252662Sstever@eecs.umich.edu * A Packet is used to encapsulate a transfer between two objects in
2262662Sstever@eecs.umich.edu * the memory system (e.g., the L1 and L2 cache).  (In contrast, a
2272662Sstever@eecs.umich.edu * single Request travels all the way from the requester to the
2282662Sstever@eecs.umich.edu * ultimate destination and back, possibly being conveyed by several
2292662Sstever@eecs.umich.edu * different Packets along the way.)
2302381SN/A */
2319044SAli.Saidi@ARM.comclass Packet : public Printable
2322381SN/A{
2332813Srdreslin@umich.edu  public:
2345735Snate@binkert.org    typedef uint32_t FlagsType;
2355735Snate@binkert.org    typedef ::Flags<FlagsType> Flags;
2364022Sstever@eecs.umich.edu
2375735Snate@binkert.org  private:
2385735Snate@binkert.org    static const FlagsType PUBLIC_FLAGS           = 0x00000000;
2395735Snate@binkert.org    static const FlagsType PRIVATE_FLAGS          = 0x00007F0F;
2405735Snate@binkert.org    static const FlagsType COPY_FLAGS             = 0x0000000F;
2415735Snate@binkert.org
2425735Snate@binkert.org    static const FlagsType SHARED                 = 0x00000001;
2435735Snate@binkert.org    // Special control flags
2445735Snate@binkert.org    /// Special timing-mode atomic snoop for multi-level coherence.
2455735Snate@binkert.org    static const FlagsType EXPRESS_SNOOP          = 0x00000002;
2465735Snate@binkert.org    /// Does supplier have exclusive copy?
2475735Snate@binkert.org    /// Useful for multi-level coherence.
2485735Snate@binkert.org    static const FlagsType SUPPLY_EXCLUSIVE       = 0x00000004;
2495735Snate@binkert.org    // Snoop response flags
2505735Snate@binkert.org    static const FlagsType MEM_INHIBIT            = 0x00000008;
2515735Snate@binkert.org    /// Are the 'addr' and 'size' fields valid?
2525735Snate@binkert.org    static const FlagsType VALID_ADDR             = 0x00000100;
2535735Snate@binkert.org    static const FlagsType VALID_SIZE             = 0x00000200;
2545735Snate@binkert.org    /// Is the data pointer set to a value that shouldn't be freed
2555735Snate@binkert.org    /// when the packet is destroyed?
2565735Snate@binkert.org    static const FlagsType STATIC_DATA            = 0x00001000;
2575735Snate@binkert.org    /// The data pointer points to a value that should be freed when
25810566Sandreas.hansson@arm.com    /// the packet is destroyed. The pointer is assumed to be pointing
25910566Sandreas.hansson@arm.com    /// to an array, and delete [] is consequently called
2605735Snate@binkert.org    static const FlagsType DYNAMIC_DATA           = 0x00002000;
2618436SBrad.Beckmann@amd.com    /// suppress the error if this packet encounters a functional
2628436SBrad.Beckmann@amd.com    /// access failure.
2638436SBrad.Beckmann@amd.com    static const FlagsType SUPPRESS_FUNC_ERROR    = 0x00008000;
26410192Smitch.hayenga@arm.com    // Signal prefetch squash through express snoop flag
26510192Smitch.hayenga@arm.com    static const FlagsType PREFETCH_SNOOP_SQUASH  = 0x00010000;
2665735Snate@binkert.org
2675735Snate@binkert.org    Flags flags;
2685735Snate@binkert.org
2695735Snate@binkert.org  public:
2704022Sstever@eecs.umich.edu    typedef MemCmd::Command Command;
2714022Sstever@eecs.umich.edu
2725735Snate@binkert.org    /// The command field of the packet.
2734870Sstever@eecs.umich.edu    MemCmd cmd;
2744870Sstever@eecs.umich.edu
2755735Snate@binkert.org    /// A pointer to the original request.
27610569Sandreas.hansson@arm.com    const RequestPtr req;
2774870Sstever@eecs.umich.edu
2782566SN/A  private:
2795735Snate@binkert.org   /**
2805735Snate@binkert.org    * A pointer to the data being transfered.  It can be differnt
2815735Snate@binkert.org    * sizes at each level of the heirarchy so it belongs in the
2825735Snate@binkert.org    * packet, not request. This may or may not be populated when a
2835735Snate@binkert.org    * responder recieves the packet. If not populated it memory should
2845735Snate@binkert.org    * be allocated.
2852566SN/A    */
2862566SN/A    PacketDataPtr data;
2872566SN/A
2885735Snate@binkert.org    /// The address of the request.  This address could be virtual or
2895735Snate@binkert.org    /// physical, depending on the system configuration.
2902381SN/A    Addr addr;
2912381SN/A
29210028SGiacomo.Gabrielli@arm.com    /// True if the request targets the secure memory space.
29310028SGiacomo.Gabrielli@arm.com    bool _isSecure;
29410028SGiacomo.Gabrielli@arm.com
2955735Snate@binkert.org    /// The size of the request or transfer.
2966227Snate@binkert.org    unsigned size;
2972381SN/A
2985735Snate@binkert.org    /**
2999030Sandreas.hansson@arm.com     * Source port identifier set on a request packet to enable
3009030Sandreas.hansson@arm.com     * appropriate routing of the responses. The source port
30110405Sandreas.hansson@arm.com     * identifier is set by any multiplexing component, e.g. a
30210405Sandreas.hansson@arm.com     * crossbar, as the timing responses need this information to be
30310405Sandreas.hansson@arm.com     * routed back to the appropriate port at a later point in
30410405Sandreas.hansson@arm.com     * time. The field can be updated (over-written) as the request
30510405Sandreas.hansson@arm.com     * packet passes through additional multiplexing components, and
30610405Sandreas.hansson@arm.com     * it is their responsibility to remember the original source port
30710405Sandreas.hansson@arm.com     * identifier, for example by using an appropriate sender
30810405Sandreas.hansson@arm.com     * state. The latter is done in the cache and bridge.
3095735Snate@binkert.org     */
3109031Sandreas.hansson@arm.com    PortID src;
3112381SN/A
3125735Snate@binkert.org    /**
3139030Sandreas.hansson@arm.com     * Destination port identifier that is present on all response
3149030Sandreas.hansson@arm.com     * packets that passed through a multiplexing component as a
3159030Sandreas.hansson@arm.com     * request packet. The source port identifier is turned into a
3169030Sandreas.hansson@arm.com     * destination port identifier when the packet is turned into a
31710405Sandreas.hansson@arm.com     * response, and the destination is used, e.g. by the crossbar, to
3189030Sandreas.hansson@arm.com     * select the appropriate path through the interconnect.
3195735Snate@binkert.org     */
3209031Sandreas.hansson@arm.com    PortID dest;
3212641Sstever@eecs.umich.edu
3225735Snate@binkert.org    /**
3235735Snate@binkert.org     * The original value of the command field.  Only valid when the
3244870Sstever@eecs.umich.edu     * current command field is an error condition; in that case, the
3254870Sstever@eecs.umich.edu     * previous contents of the command field are copied here.  This
3264870Sstever@eecs.umich.edu     * field is *not* set on non-error responses.
3274870Sstever@eecs.umich.edu     */
3284870Sstever@eecs.umich.edu    MemCmd origCmd;
3294870Sstever@eecs.umich.edu
3308668Sgeoffrey.blake@arm.com    /**
3318668Sgeoffrey.blake@arm.com     * These values specify the range of bytes found that satisfy a
3328668Sgeoffrey.blake@arm.com     * functional read.
3338668Sgeoffrey.blake@arm.com     */
3348668Sgeoffrey.blake@arm.com    uint16_t bytesValidStart;
3358668Sgeoffrey.blake@arm.com    uint16_t bytesValidEnd;
3368668Sgeoffrey.blake@arm.com
3372641Sstever@eecs.umich.edu  public:
3382811Srdreslin@umich.edu
3399547Sandreas.hansson@arm.com    /**
3409547Sandreas.hansson@arm.com     * The extra delay from seeing the packet until the first word is
34110405Sandreas.hansson@arm.com     * transmitted. This delay is used to communicate the crossbar
34210405Sandreas.hansson@arm.com     * forwarding latency to the neighbouring object (e.g. a cache)
34310405Sandreas.hansson@arm.com     * that actually makes the packet wait. As the delay is relative,
34410405Sandreas.hansson@arm.com     * a 32-bit unsigned should be sufficient.
3459547Sandreas.hansson@arm.com     */
34610405Sandreas.hansson@arm.com    uint32_t firstWordDelay;
3473218Sgblack@eecs.umich.edu
3489547Sandreas.hansson@arm.com    /**
34910405Sandreas.hansson@arm.com     * The extra pipelining delay from seeing the packet until the
35010405Sandreas.hansson@arm.com     * last word is transmitted by the component that provided it (if
35110405Sandreas.hansson@arm.com     * any). This includes the first word delay. Similar to the first
35210405Sandreas.hansson@arm.com     * word delay, this is used to make up for the fact that the
35310405Sandreas.hansson@arm.com     * crossbar does not make the packet wait. As the delay is
35410405Sandreas.hansson@arm.com     * relative, a 32-bit unsigned should be sufficient.
3559547Sandreas.hansson@arm.com     */
35610405Sandreas.hansson@arm.com    uint32_t lastWordDelay;
3573218Sgblack@eecs.umich.edu
3585735Snate@binkert.org    /**
3595735Snate@binkert.org     * A virtual base opaque structure used to hold state associated
3609542Sandreas.hansson@arm.com     * with the packet (e.g., an MSHR), specific to a MemObject that
3619542Sandreas.hansson@arm.com     * sees the packet. A pointer to this state is returned in the
3629542Sandreas.hansson@arm.com     * packet's response so that the MemObject in question can quickly
3639542Sandreas.hansson@arm.com     * look up the state needed to process it. A specific subclass
3649542Sandreas.hansson@arm.com     * would be derived from this to carry state specific to a
3659542Sandreas.hansson@arm.com     * particular sending device.
3669542Sandreas.hansson@arm.com     *
3679542Sandreas.hansson@arm.com     * As multiple MemObjects may add their SenderState throughout the
3689542Sandreas.hansson@arm.com     * memory system, the SenderStates create a stack, where a
3699542Sandreas.hansson@arm.com     * MemObject can add a new Senderstate, as long as the
3709542Sandreas.hansson@arm.com     * predecessing SenderState is restored when the response comes
3719542Sandreas.hansson@arm.com     * back. For this reason, the predecessor should always be
3729542Sandreas.hansson@arm.com     * populated with the current SenderState of a packet before
3739542Sandreas.hansson@arm.com     * modifying the senderState field in the request packet.
3745735Snate@binkert.org     */
3755735Snate@binkert.org    struct SenderState
3765735Snate@binkert.org    {
3779542Sandreas.hansson@arm.com        SenderState* predecessor;
3789542Sandreas.hansson@arm.com        SenderState() : predecessor(NULL) {}
3792641Sstever@eecs.umich.edu        virtual ~SenderState() {}
3802641Sstever@eecs.umich.edu    };
3812641Sstever@eecs.umich.edu
3825315Sstever@gmail.com    /**
3835315Sstever@gmail.com     * Object used to maintain state of a PrintReq.  The senderState
3845315Sstever@gmail.com     * field of a PrintReq should always be of this type.
3855315Sstever@gmail.com     */
3869044SAli.Saidi@ARM.com    class PrintReqState : public SenderState
3875735Snate@binkert.org    {
3885735Snate@binkert.org      private:
3895735Snate@binkert.org        /**
3905735Snate@binkert.org         * An entry in the label stack.
3915735Snate@binkert.org         */
3925735Snate@binkert.org        struct LabelStackEntry
3935735Snate@binkert.org        {
3945314Sstever@gmail.com            const std::string label;
3955314Sstever@gmail.com            std::string *prefix;
3965314Sstever@gmail.com            bool labelPrinted;
3975735Snate@binkert.org            LabelStackEntry(const std::string &_label, std::string *_prefix);
3985314Sstever@gmail.com        };
3995314Sstever@gmail.com
4005314Sstever@gmail.com        typedef std::list<LabelStackEntry> LabelStack;
4015314Sstever@gmail.com        LabelStack labelStack;
4025314Sstever@gmail.com
4035314Sstever@gmail.com        std::string *curPrefixPtr;
4045314Sstever@gmail.com
4055314Sstever@gmail.com      public:
4065314Sstever@gmail.com        std::ostream &os;
4075314Sstever@gmail.com        const int verbosity;
4085314Sstever@gmail.com
4095314Sstever@gmail.com        PrintReqState(std::ostream &os, int verbosity = 0);
4105314Sstever@gmail.com        ~PrintReqState();
4115314Sstever@gmail.com
4125735Snate@binkert.org        /**
4135735Snate@binkert.org         * Returns the current line prefix.
4145735Snate@binkert.org         */
4155314Sstever@gmail.com        const std::string &curPrefix() { return *curPrefixPtr; }
4165315Sstever@gmail.com
4175735Snate@binkert.org        /**
4185735Snate@binkert.org         * Push a label onto the label stack, and prepend the given
4195315Sstever@gmail.com         * prefix string onto the current prefix.  Labels will only be
4205735Snate@binkert.org         * printed if an object within the label's scope is printed.
4215735Snate@binkert.org         */
4225314Sstever@gmail.com        void pushLabel(const std::string &lbl,
4235314Sstever@gmail.com                       const std::string &prefix = "  ");
4245735Snate@binkert.org
4255735Snate@binkert.org        /**
4265735Snate@binkert.org         * Pop a label off the label stack.
4275735Snate@binkert.org         */
4285314Sstever@gmail.com        void popLabel();
4295735Snate@binkert.org
4305735Snate@binkert.org        /**
4315735Snate@binkert.org         * Print all of the pending unprinted labels on the
4325315Sstever@gmail.com         * stack. Called by printObj(), so normally not called by
4335735Snate@binkert.org         * users unless bypassing printObj().
4345735Snate@binkert.org         */
4355314Sstever@gmail.com        void printLabels();
4365735Snate@binkert.org
4375735Snate@binkert.org        /**
4385735Snate@binkert.org         * Print a Printable object to os, because it matched the
4395735Snate@binkert.org         * address on a PrintReq.
4405735Snate@binkert.org         */
4415314Sstever@gmail.com        void printObj(Printable *obj);
4425314Sstever@gmail.com    };
4435314Sstever@gmail.com
4445735Snate@binkert.org    /**
4455735Snate@binkert.org     * This packet's sender state.  Devices should use dynamic_cast<>
4465735Snate@binkert.org     * to cast to the state appropriate to the sender.  The intent of
4475735Snate@binkert.org     * this variable is to allow a device to attach extra information
4489542Sandreas.hansson@arm.com     * to a request. A response packet must return the sender state
4495735Snate@binkert.org     * that was attached to the original request (even if a new packet
4505735Snate@binkert.org     * is created).
4515735Snate@binkert.org     */
4522662Sstever@eecs.umich.edu    SenderState *senderState;
4532641Sstever@eecs.umich.edu
4549542Sandreas.hansson@arm.com    /**
4559542Sandreas.hansson@arm.com     * Push a new sender state to the packet and make the current
4569542Sandreas.hansson@arm.com     * sender state the predecessor of the new one. This should be
4579542Sandreas.hansson@arm.com     * prefered over direct manipulation of the senderState member
4589542Sandreas.hansson@arm.com     * variable.
4599542Sandreas.hansson@arm.com     *
4609542Sandreas.hansson@arm.com     * @param sender_state SenderState to push at the top of the stack
4619542Sandreas.hansson@arm.com     */
4629542Sandreas.hansson@arm.com    void pushSenderState(SenderState *sender_state);
4639542Sandreas.hansson@arm.com
4649542Sandreas.hansson@arm.com    /**
4659542Sandreas.hansson@arm.com     * Pop the top of the state stack and return a pointer to it. This
4669542Sandreas.hansson@arm.com     * assumes the current sender state is not NULL. This should be
4679542Sandreas.hansson@arm.com     * preferred over direct manipulation of the senderState member
4689542Sandreas.hansson@arm.com     * variable.
4699542Sandreas.hansson@arm.com     *
4709542Sandreas.hansson@arm.com     * @return The current top of the stack
4719542Sandreas.hansson@arm.com     */
4729542Sandreas.hansson@arm.com    SenderState *popSenderState();
4739542Sandreas.hansson@arm.com
4749543Ssascha.bischoff@arm.com    /**
4759543Ssascha.bischoff@arm.com     * Go through the sender state stack and return the first instance
4769543Ssascha.bischoff@arm.com     * that is of type T (as determined by a dynamic_cast). If there
4779543Ssascha.bischoff@arm.com     * is no sender state of type T, NULL is returned.
4789543Ssascha.bischoff@arm.com     *
4799543Ssascha.bischoff@arm.com     * @return The topmost state of type T
4809543Ssascha.bischoff@arm.com     */
4819543Ssascha.bischoff@arm.com    template <typename T>
4829543Ssascha.bischoff@arm.com    T * findNextSenderState() const
4839543Ssascha.bischoff@arm.com    {
4849543Ssascha.bischoff@arm.com        T *t = NULL;
4859543Ssascha.bischoff@arm.com        SenderState* sender_state = senderState;
4869543Ssascha.bischoff@arm.com        while (t == NULL && sender_state != NULL) {
4879543Ssascha.bischoff@arm.com            t = dynamic_cast<T*>(sender_state);
4889543Ssascha.bischoff@arm.com            sender_state = sender_state->predecessor;
4899543Ssascha.bischoff@arm.com        }
4909543Ssascha.bischoff@arm.com        return t;
4919543Ssascha.bischoff@arm.com    }
4929543Ssascha.bischoff@arm.com
4935735Snate@binkert.org    /// Return the string name of the cmd field (for debugging and
4945735Snate@binkert.org    /// tracing).
4954022Sstever@eecs.umich.edu    const std::string &cmdString() const { return cmd.toString(); }
4962811Srdreslin@umich.edu
4975735Snate@binkert.org    /// Return the index of this command.
4984022Sstever@eecs.umich.edu    inline int cmdToIndex() const { return cmd.toInt(); }
4992811Srdreslin@umich.edu
50010583SCurtis.Dunham@arm.com    bool isRead() const              { return cmd.isRead(); }
50110583SCurtis.Dunham@arm.com    bool isWrite() const             { return cmd.isWrite(); }
50210583SCurtis.Dunham@arm.com    bool isUpgrade()  const          { return cmd.isUpgrade(); }
50310583SCurtis.Dunham@arm.com    bool isRequest() const           { return cmd.isRequest(); }
50410583SCurtis.Dunham@arm.com    bool isResponse() const          { return cmd.isResponse(); }
50510583SCurtis.Dunham@arm.com    bool needsExclusive() const      { return cmd.needsExclusive(); }
50610583SCurtis.Dunham@arm.com    bool needsResponse() const       { return cmd.needsResponse(); }
50710583SCurtis.Dunham@arm.com    bool isInvalidate() const        { return cmd.isInvalidate(); }
50810583SCurtis.Dunham@arm.com    bool isWriteInvalidate() const   { return cmd.isWriteInvalidate(); }
50910583SCurtis.Dunham@arm.com    bool hasData() const             { return cmd.hasData(); }
51010583SCurtis.Dunham@arm.com    bool isLLSC() const              { return cmd.isLLSC(); }
51110583SCurtis.Dunham@arm.com    bool isError() const             { return cmd.isError(); }
51210583SCurtis.Dunham@arm.com    bool isPrint() const             { return cmd.isPrint(); }
51310583SCurtis.Dunham@arm.com    bool isFlush() const             { return cmd.isFlush(); }
5142812Srdreslin@umich.edu
5154870Sstever@eecs.umich.edu    // Snoop flags
51610567Sandreas.hansson@arm.com    void assertMemInhibit()
51710567Sandreas.hansson@arm.com    {
51810567Sandreas.hansson@arm.com        assert(isRequest());
51910567Sandreas.hansson@arm.com        assert(!flags.isSet(MEM_INHIBIT));
52010567Sandreas.hansson@arm.com        flags.set(MEM_INHIBIT);
52110567Sandreas.hansson@arm.com    }
5229951Sstephan.diestelhorst@arm.com    bool memInhibitAsserted() const { return flags.isSet(MEM_INHIBIT); }
5239951Sstephan.diestelhorst@arm.com    void assertShared()             { flags.set(SHARED); }
5249951Sstephan.diestelhorst@arm.com    bool sharedAsserted() const     { return flags.isSet(SHARED); }
5254870Sstever@eecs.umich.edu
5264895Sstever@eecs.umich.edu    // Special control flags
5279951Sstephan.diestelhorst@arm.com    void setExpressSnoop()          { flags.set(EXPRESS_SNOOP); }
5289951Sstephan.diestelhorst@arm.com    bool isExpressSnoop() const     { return flags.isSet(EXPRESS_SNOOP); }
5299951Sstephan.diestelhorst@arm.com    void setSupplyExclusive()       { flags.set(SUPPLY_EXCLUSIVE); }
5309951Sstephan.diestelhorst@arm.com    void clearSupplyExclusive()     { flags.clear(SUPPLY_EXCLUSIVE); }
5319951Sstephan.diestelhorst@arm.com    bool isSupplyExclusive() const  { return flags.isSet(SUPPLY_EXCLUSIVE); }
5329951Sstephan.diestelhorst@arm.com    void setSuppressFuncError()     { flags.set(SUPPRESS_FUNC_ERROR); }
5339951Sstephan.diestelhorst@arm.com    bool suppressFuncError() const  { return flags.isSet(SUPPRESS_FUNC_ERROR); }
53410192Smitch.hayenga@arm.com    void setPrefetchSquashed()      { flags.set(PREFETCH_SNOOP_SQUASH); }
53510192Smitch.hayenga@arm.com    bool prefetchSquashed() const   { return flags.isSet(PREFETCH_SNOOP_SQUASH); }
5364895Sstever@eecs.umich.edu
5374870Sstever@eecs.umich.edu    // Network error conditions... encapsulate them as methods since
5384870Sstever@eecs.umich.edu    // their encoding keeps changing (from result field to command
5394870Sstever@eecs.umich.edu    // field, etc.)
5405735Snate@binkert.org    void
5415735Snate@binkert.org    setBadAddress()
5425735Snate@binkert.org    {
5435735Snate@binkert.org        assert(isResponse());
5445735Snate@binkert.org        cmd = MemCmd::BadAddressError;
5455735Snate@binkert.org    }
5465735Snate@binkert.org
5475735Snate@binkert.org    bool hadBadAddress() const { return cmd == MemCmd::BadAddressError; }
5484986Ssaidi@eecs.umich.edu    void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
5492814Srdreslin@umich.edu
5505735Snate@binkert.org    /// Accessor function to get the source index of the packet.
55110572Sandreas.hansson@arm.com    PortID getSrc() const { return src; }
5525735Snate@binkert.org    /// Accessor function to set the source index of the packet.
5539031Sandreas.hansson@arm.com    void setSrc(PortID _src) { src = _src; }
5542641Sstever@eecs.umich.edu
5555735Snate@binkert.org    /// Accessor function for the destination index of the packet.
55610572Sandreas.hansson@arm.com    PortID getDest() const { return dest; }
5575735Snate@binkert.org    /// Accessor function to set the destination index of the packet.
5589031Sandreas.hansson@arm.com    void setDest(PortID _dest) { dest = _dest; }
5598949Sandreas.hansson@arm.com    /// Reset destination field, e.g. to turn a response into a request again.
5609031Sandreas.hansson@arm.com    void clearDest() { dest = InvalidPortID; }
5612381SN/A
5625764Snate@binkert.org    Addr getAddr() const { assert(flags.isSet(VALID_ADDR)); return addr; }
5639259SAli.Saidi@ARM.com    /**
5649259SAli.Saidi@ARM.com     * Update the address of this packet mid-transaction. This is used
5659259SAli.Saidi@ARM.com     * by the address mapper to change an already set address to a new
5669259SAli.Saidi@ARM.com     * one based on the system configuration. It is intended to remap
5679259SAli.Saidi@ARM.com     * an existing address, so it asserts that the current address is
5689259SAli.Saidi@ARM.com     * valid.
5699259SAli.Saidi@ARM.com     */
5709259SAli.Saidi@ARM.com    void setAddr(Addr _addr) { assert(flags.isSet(VALID_ADDR)); addr = _addr; }
5719259SAli.Saidi@ARM.com
5726227Snate@binkert.org    unsigned getSize() const  { assert(flags.isSet(VALID_SIZE)); return size; }
5735735Snate@binkert.org    Addr getOffset(int blkSize) const { return getAddr() & (Addr)(blkSize - 1); }
5742549SN/A
57510028SGiacomo.Gabrielli@arm.com    bool isSecure() const
57610028SGiacomo.Gabrielli@arm.com    {
57710028SGiacomo.Gabrielli@arm.com        assert(flags.isSet(VALID_ADDR));
57810028SGiacomo.Gabrielli@arm.com        return _isSecure;
57910028SGiacomo.Gabrielli@arm.com    }
58010028SGiacomo.Gabrielli@arm.com
5815735Snate@binkert.org    /**
5827550SBrad.Beckmann@amd.com     * It has been determined that the SC packet should successfully update
5837550SBrad.Beckmann@amd.com     * memory.  Therefore, convert this SC packet to a normal write.
5847550SBrad.Beckmann@amd.com     */
5857550SBrad.Beckmann@amd.com    void
5867550SBrad.Beckmann@amd.com    convertScToWrite()
5877550SBrad.Beckmann@amd.com    {
5887550SBrad.Beckmann@amd.com        assert(isLLSC());
5897550SBrad.Beckmann@amd.com        assert(isWrite());
5907550SBrad.Beckmann@amd.com        cmd = MemCmd::WriteReq;
5917550SBrad.Beckmann@amd.com    }
5927550SBrad.Beckmann@amd.com
5937550SBrad.Beckmann@amd.com    /**
5947550SBrad.Beckmann@amd.com     * When ruby is in use, Ruby will monitor the cache line and thus M5
5957550SBrad.Beckmann@amd.com     * phys memory should treat LL ops as normal reads.
5967550SBrad.Beckmann@amd.com     */
5977550SBrad.Beckmann@amd.com    void
5987550SBrad.Beckmann@amd.com    convertLlToRead()
5997550SBrad.Beckmann@amd.com    {
6007550SBrad.Beckmann@amd.com        assert(isLLSC());
6017550SBrad.Beckmann@amd.com        assert(isRead());
6027550SBrad.Beckmann@amd.com        cmd = MemCmd::ReadReq;
6037550SBrad.Beckmann@amd.com    }
6047550SBrad.Beckmann@amd.com
6057550SBrad.Beckmann@amd.com    /**
6065735Snate@binkert.org     * Constructor.  Note that a Request object must be constructed
6075735Snate@binkert.org     * first, but the Requests's physical address and size fields need
6089030Sandreas.hansson@arm.com     * not be valid. The command must be supplied.
6095735Snate@binkert.org     */
61010569Sandreas.hansson@arm.com    Packet(const RequestPtr _req, MemCmd _cmd)
61110360Sandreas.hansson@arm.com        :  cmd(_cmd), req(_req), data(nullptr), addr(0), _isSecure(false),
61210412Sandreas.hansson@arm.com           size(0), src(InvalidPortID), dest(InvalidPortID),
6138949Sandreas.hansson@arm.com           bytesValidStart(0), bytesValidEnd(0),
61410405Sandreas.hansson@arm.com           firstWordDelay(0), lastWordDelay(0),
6159546Sandreas.hansson@arm.com           senderState(NULL)
6162641Sstever@eecs.umich.edu    {
6176104Ssteve.reinhardt@amd.com        if (req->hasPaddr()) {
6186104Ssteve.reinhardt@amd.com            addr = req->getPaddr();
6196104Ssteve.reinhardt@amd.com            flags.set(VALID_ADDR);
62010028SGiacomo.Gabrielli@arm.com            _isSecure = req->isSecure();
6216104Ssteve.reinhardt@amd.com        }
6226104Ssteve.reinhardt@amd.com        if (req->hasSize()) {
6236104Ssteve.reinhardt@amd.com            size = req->getSize();
6246104Ssteve.reinhardt@amd.com            flags.set(VALID_SIZE);
6256104Ssteve.reinhardt@amd.com        }
6262813Srdreslin@umich.edu    }
6272813Srdreslin@umich.edu
6285735Snate@binkert.org    /**
6295735Snate@binkert.org     * Alternate constructor if you are trying to create a packet with
6305735Snate@binkert.org     * a request that is for a whole block, not the address from the
6315735Snate@binkert.org     * req.  this allows for overriding the size/addr of the req.
6325735Snate@binkert.org     */
63310569Sandreas.hansson@arm.com    Packet(const RequestPtr _req, MemCmd _cmd, int _blkSize)
63410360Sandreas.hansson@arm.com        :  cmd(_cmd), req(_req), data(nullptr), addr(0), _isSecure(false),
6359031Sandreas.hansson@arm.com           src(InvalidPortID), dest(InvalidPortID),
6368949Sandreas.hansson@arm.com           bytesValidStart(0), bytesValidEnd(0),
63710405Sandreas.hansson@arm.com           firstWordDelay(0), lastWordDelay(0),
6389546Sandreas.hansson@arm.com           senderState(NULL)
6392813Srdreslin@umich.edu    {
6406104Ssteve.reinhardt@amd.com        if (req->hasPaddr()) {
6416104Ssteve.reinhardt@amd.com            addr = req->getPaddr() & ~(_blkSize - 1);
6426104Ssteve.reinhardt@amd.com            flags.set(VALID_ADDR);
64310028SGiacomo.Gabrielli@arm.com            _isSecure = req->isSecure();
6446104Ssteve.reinhardt@amd.com        }
6456104Ssteve.reinhardt@amd.com        size = _blkSize;
6466104Ssteve.reinhardt@amd.com        flags.set(VALID_SIZE);
6474626Sstever@eecs.umich.edu    }
6484626Sstever@eecs.umich.edu
6495735Snate@binkert.org    /**
6505735Snate@binkert.org     * Alternate constructor for copying a packet.  Copy all fields
6514887Sstever@eecs.umich.edu     * *except* if the original packet's data was dynamic, don't copy
6524887Sstever@eecs.umich.edu     * that, as we can't guarantee that the new packet's lifetime is
6534887Sstever@eecs.umich.edu     * less than that of the original packet.  In this case the new
6545735Snate@binkert.org     * packet should allocate its own data.
6555735Snate@binkert.org     */
65610571Sandreas.hansson@arm.com    Packet(PacketPtr pkt, bool clear_flags, bool alloc_data)
6575735Snate@binkert.org        :  cmd(pkt->cmd), req(pkt->req),
65810571Sandreas.hansson@arm.com           data(nullptr),
65910028SGiacomo.Gabrielli@arm.com           addr(pkt->addr), _isSecure(pkt->_isSecure), size(pkt->size),
66010028SGiacomo.Gabrielli@arm.com           src(pkt->src), dest(pkt->dest),
6619547Sandreas.hansson@arm.com           bytesValidStart(pkt->bytesValidStart),
6629547Sandreas.hansson@arm.com           bytesValidEnd(pkt->bytesValidEnd),
66310405Sandreas.hansson@arm.com           firstWordDelay(pkt->firstWordDelay),
66410405Sandreas.hansson@arm.com           lastWordDelay(pkt->lastWordDelay),
6659546Sandreas.hansson@arm.com           senderState(pkt->senderState)
6664626Sstever@eecs.umich.edu    {
66710571Sandreas.hansson@arm.com        if (!clear_flags)
6685735Snate@binkert.org            flags.set(pkt->flags & COPY_FLAGS);
6695735Snate@binkert.org
6709031Sandreas.hansson@arm.com        flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE));
67110565Sandreas.hansson@arm.com
67210571Sandreas.hansson@arm.com        // should we allocate space for data, or not, the express
67310571Sandreas.hansson@arm.com        // snoops do not need to carry any data as they only serve to
67410571Sandreas.hansson@arm.com        // co-ordinate state changes
67510571Sandreas.hansson@arm.com        if (alloc_data) {
67610571Sandreas.hansson@arm.com            // even if asked to allocate data, if the original packet
67710571Sandreas.hansson@arm.com            // holds static data, then the sender will not be doing
67810571Sandreas.hansson@arm.com            // any memcpy on receiving the response, thus we simply
67910571Sandreas.hansson@arm.com            // carry the pointer forward
68010571Sandreas.hansson@arm.com            if (pkt->flags.isSet(STATIC_DATA)) {
68110571Sandreas.hansson@arm.com                data = pkt->data;
68210571Sandreas.hansson@arm.com                flags.set(STATIC_DATA);
68310571Sandreas.hansson@arm.com            } else {
68410571Sandreas.hansson@arm.com                allocate();
68510571Sandreas.hansson@arm.com            }
68610571Sandreas.hansson@arm.com        }
68710342SCurtis.Dunham@arm.com    }
6888668Sgeoffrey.blake@arm.com
68910342SCurtis.Dunham@arm.com    /**
69010342SCurtis.Dunham@arm.com     * Change the packet type based on request type.
69110342SCurtis.Dunham@arm.com     */
69210342SCurtis.Dunham@arm.com    void
69310342SCurtis.Dunham@arm.com    refineCommand()
69410342SCurtis.Dunham@arm.com    {
69510342SCurtis.Dunham@arm.com        if (cmd == MemCmd::ReadReq) {
69610342SCurtis.Dunham@arm.com            if (req->isLLSC()) {
69710342SCurtis.Dunham@arm.com                cmd = MemCmd::LoadLockedReq;
69810343SCurtis.Dunham@arm.com            } else if (req->isPrefetch()) {
69910343SCurtis.Dunham@arm.com                cmd = MemCmd::SoftPFReq;
70010342SCurtis.Dunham@arm.com            }
70110342SCurtis.Dunham@arm.com        } else if (cmd == MemCmd::WriteReq) {
70210342SCurtis.Dunham@arm.com            if (req->isLLSC()) {
70310342SCurtis.Dunham@arm.com                cmd = MemCmd::StoreCondReq;
70410342SCurtis.Dunham@arm.com            } else if (req->isSwap()) {
70510342SCurtis.Dunham@arm.com                cmd = MemCmd::SwapReq;
70610342SCurtis.Dunham@arm.com            }
70710342SCurtis.Dunham@arm.com        }
70810342SCurtis.Dunham@arm.com    }
70910342SCurtis.Dunham@arm.com
71010342SCurtis.Dunham@arm.com    /**
71110342SCurtis.Dunham@arm.com     * Constructor-like methods that return Packets based on Request objects.
71210342SCurtis.Dunham@arm.com     * Will call refineCommand() to fine-tune the Packet type if it's not a
71310342SCurtis.Dunham@arm.com     * vanilla read or write.
71410342SCurtis.Dunham@arm.com     */
71510342SCurtis.Dunham@arm.com    static PacketPtr
71610569Sandreas.hansson@arm.com    createRead(const RequestPtr req)
71710342SCurtis.Dunham@arm.com    {
71810342SCurtis.Dunham@arm.com        PacketPtr pkt = new Packet(req, MemCmd::ReadReq);
71910342SCurtis.Dunham@arm.com        pkt->refineCommand();
72010342SCurtis.Dunham@arm.com        return pkt;
72110342SCurtis.Dunham@arm.com    }
72210342SCurtis.Dunham@arm.com
72310342SCurtis.Dunham@arm.com    static PacketPtr
72410569Sandreas.hansson@arm.com    createWrite(const RequestPtr req)
72510342SCurtis.Dunham@arm.com    {
72610342SCurtis.Dunham@arm.com        PacketPtr pkt = new Packet(req, MemCmd::WriteReq);
72710342SCurtis.Dunham@arm.com        pkt->refineCommand();
72810342SCurtis.Dunham@arm.com        return pkt;
7292641Sstever@eecs.umich.edu    }
7302549SN/A
7315735Snate@binkert.org    /**
7325735Snate@binkert.org     * clean up packet variables
7335735Snate@binkert.org     */
7342566SN/A    ~Packet()
7355387Sstever@gmail.com    {
7365387Sstever@gmail.com        // If this is a request packet for which there's no response,
7375387Sstever@gmail.com        // delete the request object here, since the requester will
7385387Sstever@gmail.com        // never get the chance.
7395387Sstever@gmail.com        if (req && isRequest() && !needsResponse())
7405387Sstever@gmail.com            delete req;
7415735Snate@binkert.org        deleteData();
7425387Sstever@gmail.com    }
7432566SN/A
7445735Snate@binkert.org    /**
7454626Sstever@eecs.umich.edu     * Take a request packet and modify it in place to be suitable for
7469030Sandreas.hansson@arm.com     * returning as a response to that request. The source field is
7479030Sandreas.hansson@arm.com     * turned into the destination, and subsequently cleared. Note
7489030Sandreas.hansson@arm.com     * that the latter is not necessary for atomic requests, but
7499030Sandreas.hansson@arm.com     * causes no harm as neither field is valid.
7504626Sstever@eecs.umich.edu     */
7515735Snate@binkert.org    void
7525735Snate@binkert.org    makeResponse()
7534626Sstever@eecs.umich.edu    {
7542662Sstever@eecs.umich.edu        assert(needsResponse());
7552855Srdreslin@umich.edu        assert(isRequest());
7564986Ssaidi@eecs.umich.edu        origCmd = cmd;
7574022Sstever@eecs.umich.edu        cmd = cmd.responseCommand();
7585745Snate@binkert.org
7597464Ssteve.reinhardt@amd.com        // responses are never express, even if the snoop that
7607464Ssteve.reinhardt@amd.com        // triggered them was
7617464Ssteve.reinhardt@amd.com        flags.clear(EXPRESS_SNOOP);
7627464Ssteve.reinhardt@amd.com
7635745Snate@binkert.org        dest = src;
76410405Sandreas.hansson@arm.com        src = InvalidPortID;
7652641Sstever@eecs.umich.edu    }
7662641Sstever@eecs.umich.edu
7675735Snate@binkert.org    void
7685735Snate@binkert.org    makeAtomicResponse()
7694870Sstever@eecs.umich.edu    {
7704870Sstever@eecs.umich.edu        makeResponse();
7714870Sstever@eecs.umich.edu    }
7724870Sstever@eecs.umich.edu
7735735Snate@binkert.org    void
7745735Snate@binkert.org    makeTimingResponse()
7753348Sbinkertn@umich.edu    {
7764870Sstever@eecs.umich.edu        makeResponse();
7773135Srdreslin@umich.edu    }
7783135Srdreslin@umich.edu
7798436SBrad.Beckmann@amd.com    void
7808436SBrad.Beckmann@amd.com    setFunctionalResponseStatus(bool success)
7818436SBrad.Beckmann@amd.com    {
7828436SBrad.Beckmann@amd.com        if (!success) {
7838436SBrad.Beckmann@amd.com            if (isWrite()) {
7848436SBrad.Beckmann@amd.com                cmd = MemCmd::FunctionalWriteError;
7858436SBrad.Beckmann@amd.com            } else {
7868436SBrad.Beckmann@amd.com                cmd = MemCmd::FunctionalReadError;
7878436SBrad.Beckmann@amd.com            }
7888436SBrad.Beckmann@amd.com        }
7898436SBrad.Beckmann@amd.com    }
7908436SBrad.Beckmann@amd.com
7917006Snate@binkert.org    void
7927006Snate@binkert.org    setSize(unsigned size)
7937006Snate@binkert.org    {
7947006Snate@binkert.org        assert(!flags.isSet(VALID_SIZE));
7957006Snate@binkert.org
7967006Snate@binkert.org        this->size = size;
7977006Snate@binkert.org        flags.set(VALID_SIZE);
7987006Snate@binkert.org    }
7997006Snate@binkert.org
8002685Ssaidi@eecs.umich.edu
8013348Sbinkertn@umich.edu    /**
8023348Sbinkertn@umich.edu     * Set the data pointer to the following value that should not be
80310571Sandreas.hansson@arm.com     * freed. Static data allows us to do a single memcpy even if
80410571Sandreas.hansson@arm.com     * multiple packets are required to get from source to destination
80510571Sandreas.hansson@arm.com     * and back. In essence the pointer is set calling dataStatic on
80610571Sandreas.hansson@arm.com     * the original packet, and whenever this packet is copied and
80710571Sandreas.hansson@arm.com     * forwarded the same pointer is passed on. When a packet
80810571Sandreas.hansson@arm.com     * eventually reaches the destination holding the data, it is
80910571Sandreas.hansson@arm.com     * copied once into the location originally set. On the way back
81010571Sandreas.hansson@arm.com     * to the source, no copies are necessary.
8112566SN/A     */
8122566SN/A    template <typename T>
8133348Sbinkertn@umich.edu    void
8143348Sbinkertn@umich.edu    dataStatic(T *p)
8153348Sbinkertn@umich.edu    {
81610566Sandreas.hansson@arm.com        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
8173348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
8185735Snate@binkert.org        flags.set(STATIC_DATA);
8193348Sbinkertn@umich.edu    }
8202566SN/A
8213348Sbinkertn@umich.edu    /**
82210564Sandreas.hansson@arm.com     * Set the data pointer to the following value that should not be
82310564Sandreas.hansson@arm.com     * freed. This version of the function allows the pointer passed
82410564Sandreas.hansson@arm.com     * to us to be const. To avoid issues down the line we cast the
82510564Sandreas.hansson@arm.com     * constness away, the alternative would be to keep both a const
82610564Sandreas.hansson@arm.com     * and non-const data pointer and cleverly choose between
82710564Sandreas.hansson@arm.com     * them. Note that this is only allowed for static data.
82810564Sandreas.hansson@arm.com     */
82910564Sandreas.hansson@arm.com    template <typename T>
83010564Sandreas.hansson@arm.com    void
83110564Sandreas.hansson@arm.com    dataStaticConst(const T *p)
83210564Sandreas.hansson@arm.com    {
83310566Sandreas.hansson@arm.com        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
83410564Sandreas.hansson@arm.com        data = const_cast<PacketDataPtr>(p);
83510564Sandreas.hansson@arm.com        flags.set(STATIC_DATA);
83610564Sandreas.hansson@arm.com    }
83710564Sandreas.hansson@arm.com
83810564Sandreas.hansson@arm.com    /**
8393348Sbinkertn@umich.edu     * Set the data pointer to a value that should have delete []
84010571Sandreas.hansson@arm.com     * called on it. Dynamic data is local to this packet, and as the
84110571Sandreas.hansson@arm.com     * packet travels from source to destination, forwarded packets
84210571Sandreas.hansson@arm.com     * will allocate their own data. When a packet reaches the final
84310571Sandreas.hansson@arm.com     * destination it will populate the dynamic data of that specific
84410571Sandreas.hansson@arm.com     * packet, and on the way back towards the source, memcpy will be
84510571Sandreas.hansson@arm.com     * invoked in every step where a new packet was created e.g. in
84610571Sandreas.hansson@arm.com     * the caches. Ultimately when the response reaches the source a
84710571Sandreas.hansson@arm.com     * final memcpy is needed to extract the data from the packet
84810571Sandreas.hansson@arm.com     * before it is deallocated.
8493348Sbinkertn@umich.edu     */
8502566SN/A    template <typename T>
8513348Sbinkertn@umich.edu    void
8523348Sbinkertn@umich.edu    dataDynamic(T *p)
8533348Sbinkertn@umich.edu    {
85410566Sandreas.hansson@arm.com        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
8553348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
8565735Snate@binkert.org        flags.set(DYNAMIC_DATA);
8573348Sbinkertn@umich.edu    }
8583348Sbinkertn@umich.edu
8595735Snate@binkert.org    /**
8605735Snate@binkert.org     * get a pointer to the data ptr.
8615735Snate@binkert.org     */
8623348Sbinkertn@umich.edu    template <typename T>
8633348Sbinkertn@umich.edu    T*
86410562Sandreas.hansson@arm.com    getPtr()
8653348Sbinkertn@umich.edu    {
86610562Sandreas.hansson@arm.com        assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
8673348Sbinkertn@umich.edu        return (T*)data;
8683348Sbinkertn@umich.edu    }
8692566SN/A
87010563Sandreas.hansson@arm.com    template <typename T>
87110563Sandreas.hansson@arm.com    const T*
87210563Sandreas.hansson@arm.com    getConstPtr() const
87310563Sandreas.hansson@arm.com    {
87410563Sandreas.hansson@arm.com        assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
87510563Sandreas.hansson@arm.com        return (const T*)data;
87610563Sandreas.hansson@arm.com    }
87710563Sandreas.hansson@arm.com
8785735Snate@binkert.org    /**
8795735Snate@binkert.org     * return the value of what is pointed to in the packet.
8805735Snate@binkert.org     */
8812566SN/A    template <typename T>
88210563Sandreas.hansson@arm.com    T get() const;
8832566SN/A
8845735Snate@binkert.org    /**
8855735Snate@binkert.org     * set the value in the data pointer to v.
8865735Snate@binkert.org     */
8872566SN/A    template <typename T>
8882592SN/A    void set(T v);
8892566SN/A
8903348Sbinkertn@umich.edu    /**
8914626Sstever@eecs.umich.edu     * Copy data into the packet from the provided pointer.
8924626Sstever@eecs.umich.edu     */
8935735Snate@binkert.org    void
89410563Sandreas.hansson@arm.com    setData(const uint8_t *p)
8954626Sstever@eecs.umich.edu    {
89610571Sandreas.hansson@arm.com        // we should never be copying data onto itself, which means we
89710571Sandreas.hansson@arm.com        // must idenfity packets with static data, as they carry the
89810571Sandreas.hansson@arm.com        // same pointer from source to destination and back
89910571Sandreas.hansson@arm.com        assert(p != getPtr<uint8_t>() || flags.isSet(STATIC_DATA));
90010571Sandreas.hansson@arm.com
9017691SAli.Saidi@ARM.com        if (p != getPtr<uint8_t>())
90210571Sandreas.hansson@arm.com            // for packet with allocated dynamic data, we copy data from
90310571Sandreas.hansson@arm.com            // one to the other, e.g. a forwarded response to a response
9047691SAli.Saidi@ARM.com            std::memcpy(getPtr<uint8_t>(), p, getSize());
9054626Sstever@eecs.umich.edu    }
9064626Sstever@eecs.umich.edu
9074626Sstever@eecs.umich.edu    /**
9084626Sstever@eecs.umich.edu     * Copy data into the packet from the provided block pointer,
9094626Sstever@eecs.umich.edu     * which is aligned to the given block size.
9104626Sstever@eecs.umich.edu     */
9115735Snate@binkert.org    void
91210563Sandreas.hansson@arm.com    setDataFromBlock(const uint8_t *blk_data, int blkSize)
9134626Sstever@eecs.umich.edu    {
9144626Sstever@eecs.umich.edu        setData(blk_data + getOffset(blkSize));
9154626Sstever@eecs.umich.edu    }
9164626Sstever@eecs.umich.edu
9174626Sstever@eecs.umich.edu    /**
9184626Sstever@eecs.umich.edu     * Copy data from the packet to the provided block pointer, which
9194626Sstever@eecs.umich.edu     * is aligned to the given block size.
9204626Sstever@eecs.umich.edu     */
9215735Snate@binkert.org    void
92210563Sandreas.hansson@arm.com    writeData(uint8_t *p) const
9234626Sstever@eecs.umich.edu    {
92410563Sandreas.hansson@arm.com        std::memcpy(p, getConstPtr<uint8_t>(), getSize());
9254626Sstever@eecs.umich.edu    }
9264626Sstever@eecs.umich.edu
9274626Sstever@eecs.umich.edu    /**
9284626Sstever@eecs.umich.edu     * Copy data from the packet to the memory at the provided pointer.
9294626Sstever@eecs.umich.edu     */
9305735Snate@binkert.org    void
93110563Sandreas.hansson@arm.com    writeDataToBlock(uint8_t *blk_data, int blkSize) const
9324626Sstever@eecs.umich.edu    {
9334626Sstever@eecs.umich.edu        writeData(blk_data + getOffset(blkSize));
9344626Sstever@eecs.umich.edu    }
9354626Sstever@eecs.umich.edu
9364626Sstever@eecs.umich.edu    /**
9373348Sbinkertn@umich.edu     * delete the data pointed to in the data pointer. Ok to call to
9383348Sbinkertn@umich.edu     * matter how data was allocted.
9393348Sbinkertn@umich.edu     */
9405735Snate@binkert.org    void
9415735Snate@binkert.org    deleteData()
9425735Snate@binkert.org    {
94310566Sandreas.hansson@arm.com        if (flags.isSet(DYNAMIC_DATA))
9445735Snate@binkert.org            delete [] data;
9455735Snate@binkert.org
94610566Sandreas.hansson@arm.com        flags.clear(STATIC_DATA|DYNAMIC_DATA);
9475735Snate@binkert.org        data = NULL;
9485735Snate@binkert.org    }
9492566SN/A
95010565Sandreas.hansson@arm.com    /** Allocate memory for the packet. */
9515735Snate@binkert.org    void
9525735Snate@binkert.org    allocate()
9535735Snate@binkert.org    {
9545764Snate@binkert.org        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
95510566Sandreas.hansson@arm.com        flags.set(DYNAMIC_DATA);
9565745Snate@binkert.org        data = new uint8_t[getSize()];
9575735Snate@binkert.org    }
9585735Snate@binkert.org
9594626Sstever@eecs.umich.edu    /**
96010570Sandreas.hansson@arm.com     * Check a functional request against a memory value stored in
96110570Sandreas.hansson@arm.com     * another packet (i.e. an in-transit request or
96210570Sandreas.hansson@arm.com     * response). Returns true if the current packet is a read, and
96310570Sandreas.hansson@arm.com     * the other packet provides the data, which is then copied to the
96410570Sandreas.hansson@arm.com     * current packet. If the current packet is a write, and the other
96510570Sandreas.hansson@arm.com     * packet intersects this one, then we update the data
96610570Sandreas.hansson@arm.com     * accordingly.
96710570Sandreas.hansson@arm.com     */
96810570Sandreas.hansson@arm.com    bool
96910570Sandreas.hansson@arm.com    checkFunctional(PacketPtr other)
97010570Sandreas.hansson@arm.com    {
97110570Sandreas.hansson@arm.com        // all packets that are carrying a payload should have a valid
97210570Sandreas.hansson@arm.com        // data pointer
97310570Sandreas.hansson@arm.com        return checkFunctional(other, other->getAddr(), other->isSecure(),
97410570Sandreas.hansson@arm.com                               other->getSize(),
97510570Sandreas.hansson@arm.com                               other->hasData() ?
97610570Sandreas.hansson@arm.com                               other->getPtr<uint8_t>() : NULL);
97710570Sandreas.hansson@arm.com    }
97810570Sandreas.hansson@arm.com
97910570Sandreas.hansson@arm.com    /**
9804626Sstever@eecs.umich.edu     * Check a functional request against a memory value represented
98110570Sandreas.hansson@arm.com     * by a base/size pair and an associated data array. If the
98210570Sandreas.hansson@arm.com     * current packet is a read, it may be satisfied by the memory
98310570Sandreas.hansson@arm.com     * value. If the current packet is a write, it may update the
9844626Sstever@eecs.umich.edu     * memory value.
9854626Sstever@eecs.umich.edu     */
9865735Snate@binkert.org    bool
98710570Sandreas.hansson@arm.com    checkFunctional(Printable *obj, Addr base, bool is_secure, int size,
98810570Sandreas.hansson@arm.com                    uint8_t *_data);
9895314Sstever@gmail.com
9905315Sstever@gmail.com    /**
9915315Sstever@gmail.com     * Push label for PrintReq (safe to call unconditionally).
9925315Sstever@gmail.com     */
9935735Snate@binkert.org    void
9945735Snate@binkert.org    pushLabel(const std::string &lbl)
9955735Snate@binkert.org    {
9965735Snate@binkert.org        if (isPrint())
9975735Snate@binkert.org            safe_cast<PrintReqState*>(senderState)->pushLabel(lbl);
9985314Sstever@gmail.com    }
9995314Sstever@gmail.com
10005315Sstever@gmail.com    /**
10015315Sstever@gmail.com     * Pop label for PrintReq (safe to call unconditionally).
10025315Sstever@gmail.com     */
10035735Snate@binkert.org    void
10045735Snate@binkert.org    popLabel()
10055735Snate@binkert.org    {
10065735Snate@binkert.org        if (isPrint())
10075735Snate@binkert.org            safe_cast<PrintReqState*>(senderState)->popLabel();
10085314Sstever@gmail.com    }
10095314Sstever@gmail.com
10105314Sstever@gmail.com    void print(std::ostream &o, int verbosity = 0,
10115314Sstever@gmail.com               const std::string &prefix = "") const;
10129663Suri.wiener@arm.com
10139663Suri.wiener@arm.com    /**
10149663Suri.wiener@arm.com     * A no-args wrapper of print(std::ostream...)
10159663Suri.wiener@arm.com     * meant to be invoked from DPRINTFs
10169663Suri.wiener@arm.com     * avoiding string overheads in fast mode
10179663Suri.wiener@arm.com     * @return string with the request's type and start<->end addresses
10189663Suri.wiener@arm.com     */
10199663Suri.wiener@arm.com    std::string print() const;
10202381SN/A};
10212381SN/A
10222381SN/A#endif //__MEM_PACKET_HH
1023