packet.hh revision 11199
12381SN/A/*
210694SMarco.Balboni@ARM.com * Copyright (c) 2012-2015 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
1510975Sdavid.hashe@amd.com * Copyright (c) 2010,2015 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,
8911199Sandreas.hansson@arm.com        WritebackDirty,
9011199Sandreas.hansson@arm.com        WritebackClean,
9110883Sali.jafri@arm.com        CleanEvict,
924022Sstever@eecs.umich.edu        SoftPFReq,
934022Sstever@eecs.umich.edu        HardPFReq,
944022Sstever@eecs.umich.edu        SoftPFResp,
954022Sstever@eecs.umich.edu        HardPFResp,
9610886Sandreas.hansson@arm.com        WriteLineReq,
974022Sstever@eecs.umich.edu        UpgradeReq,
987465Ssteve.reinhardt@amd.com        SCUpgradeReq,           // Special "weak" upgrade for StoreCond
994628Sstever@eecs.umich.edu        UpgradeResp,
1007465Ssteve.reinhardt@amd.com        SCUpgradeFailReq,       // Failed SCUpgradeReq in MSHR (never sent)
1017465Ssteve.reinhardt@amd.com        UpgradeFailResp,        // Valid for SCUpgradeReq only
1024022Sstever@eecs.umich.edu        ReadExReq,
1034022Sstever@eecs.umich.edu        ReadExResp,
10410885Sandreas.hansson@arm.com        ReadCleanReq,
10510885Sandreas.hansson@arm.com        ReadSharedReq,
1064626Sstever@eecs.umich.edu        LoadLockedReq,
1074626Sstever@eecs.umich.edu        StoreCondReq,
1087669Ssteve.reinhardt@amd.com        StoreCondFailReq,       // Failed StoreCondReq in MSHR (never sent)
1094626Sstever@eecs.umich.edu        StoreCondResp,
1104040Ssaidi@eecs.umich.edu        SwapReq,
1114040Ssaidi@eecs.umich.edu        SwapResp,
1125650Sgblack@eecs.umich.edu        MessageReq,
1135650Sgblack@eecs.umich.edu        MessageResp,
11410975Sdavid.hashe@amd.com        ReleaseReq,
11510975Sdavid.hashe@amd.com        ReleaseResp,
11610975Sdavid.hashe@amd.com        AcquireReq,
11710975Sdavid.hashe@amd.com        AcquireResp,
1184870Sstever@eecs.umich.edu        // Error responses
1194870Sstever@eecs.umich.edu        // @TODO these should be classified as responses rather than
1204870Sstever@eecs.umich.edu        // requests; coding them as requests initially for backwards
1214870Sstever@eecs.umich.edu        // compatibility
1224870Sstever@eecs.umich.edu        InvalidDestError,  // packet dest field invalid
1234870Sstever@eecs.umich.edu        BadAddressError,   // memory address invalid
1248436SBrad.Beckmann@amd.com        FunctionalReadError, // unable to fulfill functional read
1258436SBrad.Beckmann@amd.com        FunctionalWriteError, // unable to fulfill functional write
1265314Sstever@gmail.com        // Fake simulator-only commands
1275314Sstever@gmail.com        PrintReq,       // Print state matching address
1288184Ssomayeh@cs.wisc.edu        FlushReq,      //request for a cache flush
12910886Sandreas.hansson@arm.com        InvalidateReq,   // request for address to be invalidated
13010886Sandreas.hansson@arm.com        InvalidateResp,
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
14811199Sandreas.hansson@arm.com        IsEviction,
1494022Sstever@eecs.umich.edu        IsSWPrefetch,
1504022Sstever@eecs.umich.edu        IsHWPrefetch,
1516076Sgblack@eecs.umich.edu        IsLlsc,         //!< Alpha/MIPS LL or SC access
1524626Sstever@eecs.umich.edu        HasData,        //!< There is an associated payload
1534870Sstever@eecs.umich.edu        IsError,        //!< Error response
1545314Sstever@gmail.com        IsPrint,        //!< Print state matching address (for debugging)
1558184Ssomayeh@cs.wisc.edu        IsFlush,        //!< Flush the address from caches
1564022Sstever@eecs.umich.edu        NUM_COMMAND_ATTRIBUTES
1574022Sstever@eecs.umich.edu    };
1584022Sstever@eecs.umich.edu
1595735Snate@binkert.org    /**
1605735Snate@binkert.org     * Structure that defines attributes and other data associated
1615735Snate@binkert.org     * with a Command.
1625735Snate@binkert.org     */
1635735Snate@binkert.org    struct CommandInfo
1645735Snate@binkert.org    {
1655735Snate@binkert.org        /// Set of attribute flags.
1664022Sstever@eecs.umich.edu        const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
1675735Snate@binkert.org        /// Corresponding response for requests; InvalidCmd if no
1685735Snate@binkert.org        /// response is applicable.
1694022Sstever@eecs.umich.edu        const Command response;
1705735Snate@binkert.org        /// String representation (for printing)
1714022Sstever@eecs.umich.edu        const std::string str;
1724022Sstever@eecs.umich.edu    };
1734022Sstever@eecs.umich.edu
1745735Snate@binkert.org    /// Array to map Command enum to associated info.
1754022Sstever@eecs.umich.edu    static const CommandInfo commandInfo[];
1764022Sstever@eecs.umich.edu
1774022Sstever@eecs.umich.edu  private:
1784022Sstever@eecs.umich.edu
1794022Sstever@eecs.umich.edu    Command cmd;
1804022Sstever@eecs.umich.edu
1815735Snate@binkert.org    bool
1825735Snate@binkert.org    testCmdAttrib(MemCmd::Attribute attrib) const
1835735Snate@binkert.org    {
1844022Sstever@eecs.umich.edu        return commandInfo[cmd].attributes[attrib] != 0;
1854022Sstever@eecs.umich.edu    }
1864022Sstever@eecs.umich.edu
1874022Sstever@eecs.umich.edu  public:
1884022Sstever@eecs.umich.edu
18910583SCurtis.Dunham@arm.com    bool isRead() const            { return testCmdAttrib(IsRead); }
19010583SCurtis.Dunham@arm.com    bool isWrite() const           { return testCmdAttrib(IsWrite); }
19110583SCurtis.Dunham@arm.com    bool isUpgrade() const         { return testCmdAttrib(IsUpgrade); }
19210583SCurtis.Dunham@arm.com    bool isRequest() const         { return testCmdAttrib(IsRequest); }
19310583SCurtis.Dunham@arm.com    bool isResponse() const        { return testCmdAttrib(IsResponse); }
19410583SCurtis.Dunham@arm.com    bool needsExclusive() const    { return testCmdAttrib(NeedsExclusive); }
19510583SCurtis.Dunham@arm.com    bool needsResponse() const     { return testCmdAttrib(NeedsResponse); }
19610583SCurtis.Dunham@arm.com    bool isInvalidate() const      { return testCmdAttrib(IsInvalidate); }
19711199Sandreas.hansson@arm.com    bool isEviction() const        { return testCmdAttrib(IsEviction); }
19811199Sandreas.hansson@arm.com
19911199Sandreas.hansson@arm.com    /**
20011199Sandreas.hansson@arm.com     * A writeback is an eviction that carries data.
20111199Sandreas.hansson@arm.com     */
20211199Sandreas.hansson@arm.com    bool isWriteback() const       { return testCmdAttrib(IsEviction) &&
20311199Sandreas.hansson@arm.com                                            testCmdAttrib(HasData); }
20410570Sandreas.hansson@arm.com
20510570Sandreas.hansson@arm.com    /**
20610570Sandreas.hansson@arm.com     * Check if this particular packet type carries payload data. Note
20710570Sandreas.hansson@arm.com     * that this does not reflect if the data pointer of the packet is
20810570Sandreas.hansson@arm.com     * valid or not.
20910570Sandreas.hansson@arm.com     */
2104022Sstever@eecs.umich.edu    bool hasData() const        { return testCmdAttrib(HasData); }
2116102Sgblack@eecs.umich.edu    bool isLLSC() const         { return testCmdAttrib(IsLlsc); }
21210343SCurtis.Dunham@arm.com    bool isSWPrefetch() const   { return testCmdAttrib(IsSWPrefetch); }
21310343SCurtis.Dunham@arm.com    bool isHWPrefetch() const   { return testCmdAttrib(IsHWPrefetch); }
21410343SCurtis.Dunham@arm.com    bool isPrefetch() const     { return testCmdAttrib(IsSWPrefetch) ||
21510343SCurtis.Dunham@arm.com                                         testCmdAttrib(IsHWPrefetch); }
2164870Sstever@eecs.umich.edu    bool isError() const        { return testCmdAttrib(IsError); }
2175314Sstever@gmail.com    bool isPrint() const        { return testCmdAttrib(IsPrint); }
2188184Ssomayeh@cs.wisc.edu    bool isFlush() const        { return testCmdAttrib(IsFlush); }
2194022Sstever@eecs.umich.edu
2205735Snate@binkert.org    const Command
2215735Snate@binkert.org    responseCommand() const
2225735Snate@binkert.org    {
2234022Sstever@eecs.umich.edu        return commandInfo[cmd].response;
2244022Sstever@eecs.umich.edu    }
2254022Sstever@eecs.umich.edu
2265735Snate@binkert.org    /// Return the string to a cmd given by idx.
2275735Snate@binkert.org    const std::string &toString() const { return commandInfo[cmd].str; }
2284022Sstever@eecs.umich.edu    int toInt() const { return (int)cmd; }
2294022Sstever@eecs.umich.edu
2305735Snate@binkert.org    MemCmd(Command _cmd) : cmd(_cmd) { }
2315735Snate@binkert.org    MemCmd(int _cmd) : cmd((Command)_cmd) { }
2325735Snate@binkert.org    MemCmd() : cmd(InvalidCmd) { }
2334022Sstever@eecs.umich.edu
2345735Snate@binkert.org    bool operator==(MemCmd c2) const { return (cmd == c2.cmd); }
2355735Snate@binkert.org    bool operator!=(MemCmd c2) const { return (cmd != c2.cmd); }
2364022Sstever@eecs.umich.edu};
2374022Sstever@eecs.umich.edu
2382381SN/A/**
2392662Sstever@eecs.umich.edu * A Packet is used to encapsulate a transfer between two objects in
2402662Sstever@eecs.umich.edu * the memory system (e.g., the L1 and L2 cache).  (In contrast, a
2412662Sstever@eecs.umich.edu * single Request travels all the way from the requester to the
2422662Sstever@eecs.umich.edu * ultimate destination and back, possibly being conveyed by several
2432662Sstever@eecs.umich.edu * different Packets along the way.)
2442381SN/A */
2459044SAli.Saidi@ARM.comclass Packet : public Printable
2462381SN/A{
2472813Srdreslin@umich.edu  public:
2485735Snate@binkert.org    typedef uint32_t FlagsType;
2495735Snate@binkert.org    typedef ::Flags<FlagsType> Flags;
2504022Sstever@eecs.umich.edu
2515735Snate@binkert.org  private:
2525735Snate@binkert.org
25310938Sandreas.hansson@arm.com    enum : FlagsType {
25410938Sandreas.hansson@arm.com        // Flags to transfer across when copying a packet
25510938Sandreas.hansson@arm.com        COPY_FLAGS             = 0x0000000F,
25610938Sandreas.hansson@arm.com
25710938Sandreas.hansson@arm.com        SHARED                 = 0x00000001,
25810938Sandreas.hansson@arm.com        // Special control flags
25910938Sandreas.hansson@arm.com        /// Special timing-mode atomic snoop for multi-level coherence.
26010938Sandreas.hansson@arm.com        EXPRESS_SNOOP          = 0x00000002,
26110938Sandreas.hansson@arm.com        /// Does supplier have exclusive copy?
26210938Sandreas.hansson@arm.com        /// Useful for multi-level coherence.
26310938Sandreas.hansson@arm.com        SUPPLY_EXCLUSIVE       = 0x00000004,
26410938Sandreas.hansson@arm.com        // Snoop response flags
26510938Sandreas.hansson@arm.com        MEM_INHIBIT            = 0x00000008,
26610938Sandreas.hansson@arm.com
26711057Sandreas.hansson@arm.com        /// Are the 'addr' and 'size' fields valid?
26811057Sandreas.hansson@arm.com        VALID_ADDR             = 0x00000100,
26911057Sandreas.hansson@arm.com        VALID_SIZE             = 0x00000200,
27011057Sandreas.hansson@arm.com
27110938Sandreas.hansson@arm.com        /// Is the data pointer set to a value that shouldn't be freed
27210938Sandreas.hansson@arm.com        /// when the packet is destroyed?
27310938Sandreas.hansson@arm.com        STATIC_DATA            = 0x00001000,
27410938Sandreas.hansson@arm.com        /// The data pointer points to a value that should be freed when
27510938Sandreas.hansson@arm.com        /// the packet is destroyed. The pointer is assumed to be pointing
27610938Sandreas.hansson@arm.com        /// to an array, and delete [] is consequently called
27710938Sandreas.hansson@arm.com        DYNAMIC_DATA           = 0x00002000,
27810938Sandreas.hansson@arm.com
27910938Sandreas.hansson@arm.com        /// suppress the error if this packet encounters a functional
28010938Sandreas.hansson@arm.com        /// access failure.
28110938Sandreas.hansson@arm.com        SUPPRESS_FUNC_ERROR    = 0x00008000,
28210938Sandreas.hansson@arm.com
28310938Sandreas.hansson@arm.com        // Signal block present to squash prefetch and cache evict packets
28410938Sandreas.hansson@arm.com        // through express snoop flag
28510938Sandreas.hansson@arm.com        BLOCK_CACHED          = 0x00010000
28610938Sandreas.hansson@arm.com    };
2875735Snate@binkert.org
2885735Snate@binkert.org    Flags flags;
2895735Snate@binkert.org
2905735Snate@binkert.org  public:
2914022Sstever@eecs.umich.edu    typedef MemCmd::Command Command;
2924022Sstever@eecs.umich.edu
2935735Snate@binkert.org    /// The command field of the packet.
2944870Sstever@eecs.umich.edu    MemCmd cmd;
2954870Sstever@eecs.umich.edu
2965735Snate@binkert.org    /// A pointer to the original request.
29710569Sandreas.hansson@arm.com    const RequestPtr req;
2984870Sstever@eecs.umich.edu
2992566SN/A  private:
3005735Snate@binkert.org   /**
3015735Snate@binkert.org    * A pointer to the data being transfered.  It can be differnt
3025735Snate@binkert.org    * sizes at each level of the heirarchy so it belongs in the
3035735Snate@binkert.org    * packet, not request. This may or may not be populated when a
3045735Snate@binkert.org    * responder recieves the packet. If not populated it memory should
3055735Snate@binkert.org    * be allocated.
3062566SN/A    */
3072566SN/A    PacketDataPtr data;
3082566SN/A
3095735Snate@binkert.org    /// The address of the request.  This address could be virtual or
3105735Snate@binkert.org    /// physical, depending on the system configuration.
3112381SN/A    Addr addr;
3122381SN/A
31310028SGiacomo.Gabrielli@arm.com    /// True if the request targets the secure memory space.
31410028SGiacomo.Gabrielli@arm.com    bool _isSecure;
31510028SGiacomo.Gabrielli@arm.com
3165735Snate@binkert.org    /// The size of the request or transfer.
3176227Snate@binkert.org    unsigned size;
3182381SN/A
3195735Snate@binkert.org    /**
32010723Sandreas.hansson@arm.com     * Track the bytes found that satisfy a functional read.
3218668Sgeoffrey.blake@arm.com     */
32210723Sandreas.hansson@arm.com    std::vector<bool> bytesValid;
3238668Sgeoffrey.blake@arm.com
3242641Sstever@eecs.umich.edu  public:
3252811Srdreslin@umich.edu
3269547Sandreas.hansson@arm.com    /**
32710694SMarco.Balboni@ARM.com     * The extra delay from seeing the packet until the header is
32810405Sandreas.hansson@arm.com     * transmitted. This delay is used to communicate the crossbar
32910405Sandreas.hansson@arm.com     * forwarding latency to the neighbouring object (e.g. a cache)
33010405Sandreas.hansson@arm.com     * that actually makes the packet wait. As the delay is relative,
33110405Sandreas.hansson@arm.com     * a 32-bit unsigned should be sufficient.
3329547Sandreas.hansson@arm.com     */
33310694SMarco.Balboni@ARM.com    uint32_t headerDelay;
3343218Sgblack@eecs.umich.edu
3359547Sandreas.hansson@arm.com    /**
33611127Sandreas.hansson@arm.com     * Keep track of the extra delay incurred by snooping upwards
33711127Sandreas.hansson@arm.com     * before sending a request down the memory system. This is used
33811127Sandreas.hansson@arm.com     * by the coherent crossbar to account for the additional request
33911127Sandreas.hansson@arm.com     * delay.
34011127Sandreas.hansson@arm.com     */
34111127Sandreas.hansson@arm.com    uint32_t snoopDelay;
34211127Sandreas.hansson@arm.com
34311127Sandreas.hansson@arm.com    /**
34410694SMarco.Balboni@ARM.com     * The extra pipelining delay from seeing the packet until the end of
34510694SMarco.Balboni@ARM.com     * payload is transmitted by the component that provided it (if
34610694SMarco.Balboni@ARM.com     * any). This includes the header delay. Similar to the header
34710694SMarco.Balboni@ARM.com     * delay, this is used to make up for the fact that the
34810405Sandreas.hansson@arm.com     * crossbar does not make the packet wait. As the delay is
34910405Sandreas.hansson@arm.com     * relative, a 32-bit unsigned should be sufficient.
3509547Sandreas.hansson@arm.com     */
35110694SMarco.Balboni@ARM.com    uint32_t payloadDelay;
3523218Sgblack@eecs.umich.edu
3535735Snate@binkert.org    /**
3545735Snate@binkert.org     * A virtual base opaque structure used to hold state associated
3559542Sandreas.hansson@arm.com     * with the packet (e.g., an MSHR), specific to a MemObject that
3569542Sandreas.hansson@arm.com     * sees the packet. A pointer to this state is returned in the
3579542Sandreas.hansson@arm.com     * packet's response so that the MemObject in question can quickly
3589542Sandreas.hansson@arm.com     * look up the state needed to process it. A specific subclass
3599542Sandreas.hansson@arm.com     * would be derived from this to carry state specific to a
3609542Sandreas.hansson@arm.com     * particular sending device.
3619542Sandreas.hansson@arm.com     *
3629542Sandreas.hansson@arm.com     * As multiple MemObjects may add their SenderState throughout the
3639542Sandreas.hansson@arm.com     * memory system, the SenderStates create a stack, where a
3649542Sandreas.hansson@arm.com     * MemObject can add a new Senderstate, as long as the
3659542Sandreas.hansson@arm.com     * predecessing SenderState is restored when the response comes
3669542Sandreas.hansson@arm.com     * back. For this reason, the predecessor should always be
3679542Sandreas.hansson@arm.com     * populated with the current SenderState of a packet before
3689542Sandreas.hansson@arm.com     * modifying the senderState field in the request packet.
3695735Snate@binkert.org     */
3705735Snate@binkert.org    struct SenderState
3715735Snate@binkert.org    {
3729542Sandreas.hansson@arm.com        SenderState* predecessor;
3739542Sandreas.hansson@arm.com        SenderState() : predecessor(NULL) {}
3742641Sstever@eecs.umich.edu        virtual ~SenderState() {}
3752641Sstever@eecs.umich.edu    };
3762641Sstever@eecs.umich.edu
3775315Sstever@gmail.com    /**
3785315Sstever@gmail.com     * Object used to maintain state of a PrintReq.  The senderState
3795315Sstever@gmail.com     * field of a PrintReq should always be of this type.
3805315Sstever@gmail.com     */
3819044SAli.Saidi@ARM.com    class PrintReqState : public SenderState
3825735Snate@binkert.org    {
3835735Snate@binkert.org      private:
3845735Snate@binkert.org        /**
3855735Snate@binkert.org         * An entry in the label stack.
3865735Snate@binkert.org         */
3875735Snate@binkert.org        struct LabelStackEntry
3885735Snate@binkert.org        {
3895314Sstever@gmail.com            const std::string label;
3905314Sstever@gmail.com            std::string *prefix;
3915314Sstever@gmail.com            bool labelPrinted;
3925735Snate@binkert.org            LabelStackEntry(const std::string &_label, std::string *_prefix);
3935314Sstever@gmail.com        };
3945314Sstever@gmail.com
3955314Sstever@gmail.com        typedef std::list<LabelStackEntry> LabelStack;
3965314Sstever@gmail.com        LabelStack labelStack;
3975314Sstever@gmail.com
3985314Sstever@gmail.com        std::string *curPrefixPtr;
3995314Sstever@gmail.com
4005314Sstever@gmail.com      public:
4015314Sstever@gmail.com        std::ostream &os;
4025314Sstever@gmail.com        const int verbosity;
4035314Sstever@gmail.com
4045314Sstever@gmail.com        PrintReqState(std::ostream &os, int verbosity = 0);
4055314Sstever@gmail.com        ~PrintReqState();
4065314Sstever@gmail.com
4075735Snate@binkert.org        /**
4085735Snate@binkert.org         * Returns the current line prefix.
4095735Snate@binkert.org         */
4105314Sstever@gmail.com        const std::string &curPrefix() { return *curPrefixPtr; }
4115315Sstever@gmail.com
4125735Snate@binkert.org        /**
4135735Snate@binkert.org         * Push a label onto the label stack, and prepend the given
4145315Sstever@gmail.com         * prefix string onto the current prefix.  Labels will only be
4155735Snate@binkert.org         * printed if an object within the label's scope is printed.
4165735Snate@binkert.org         */
4175314Sstever@gmail.com        void pushLabel(const std::string &lbl,
4185314Sstever@gmail.com                       const std::string &prefix = "  ");
4195735Snate@binkert.org
4205735Snate@binkert.org        /**
4215735Snate@binkert.org         * Pop a label off the label stack.
4225735Snate@binkert.org         */
4235314Sstever@gmail.com        void popLabel();
4245735Snate@binkert.org
4255735Snate@binkert.org        /**
4265735Snate@binkert.org         * Print all of the pending unprinted labels on the
4275315Sstever@gmail.com         * stack. Called by printObj(), so normally not called by
4285735Snate@binkert.org         * users unless bypassing printObj().
4295735Snate@binkert.org         */
4305314Sstever@gmail.com        void printLabels();
4315735Snate@binkert.org
4325735Snate@binkert.org        /**
4335735Snate@binkert.org         * Print a Printable object to os, because it matched the
4345735Snate@binkert.org         * address on a PrintReq.
4355735Snate@binkert.org         */
4365314Sstever@gmail.com        void printObj(Printable *obj);
4375314Sstever@gmail.com    };
4385314Sstever@gmail.com
4395735Snate@binkert.org    /**
4405735Snate@binkert.org     * This packet's sender state.  Devices should use dynamic_cast<>
4415735Snate@binkert.org     * to cast to the state appropriate to the sender.  The intent of
4425735Snate@binkert.org     * this variable is to allow a device to attach extra information
4439542Sandreas.hansson@arm.com     * to a request. A response packet must return the sender state
4445735Snate@binkert.org     * that was attached to the original request (even if a new packet
4455735Snate@binkert.org     * is created).
4465735Snate@binkert.org     */
4472662Sstever@eecs.umich.edu    SenderState *senderState;
4482641Sstever@eecs.umich.edu
4499542Sandreas.hansson@arm.com    /**
4509542Sandreas.hansson@arm.com     * Push a new sender state to the packet and make the current
4519542Sandreas.hansson@arm.com     * sender state the predecessor of the new one. This should be
4529542Sandreas.hansson@arm.com     * prefered over direct manipulation of the senderState member
4539542Sandreas.hansson@arm.com     * variable.
4549542Sandreas.hansson@arm.com     *
4559542Sandreas.hansson@arm.com     * @param sender_state SenderState to push at the top of the stack
4569542Sandreas.hansson@arm.com     */
4579542Sandreas.hansson@arm.com    void pushSenderState(SenderState *sender_state);
4589542Sandreas.hansson@arm.com
4599542Sandreas.hansson@arm.com    /**
4609542Sandreas.hansson@arm.com     * Pop the top of the state stack and return a pointer to it. This
4619542Sandreas.hansson@arm.com     * assumes the current sender state is not NULL. This should be
4629542Sandreas.hansson@arm.com     * preferred over direct manipulation of the senderState member
4639542Sandreas.hansson@arm.com     * variable.
4649542Sandreas.hansson@arm.com     *
4659542Sandreas.hansson@arm.com     * @return The current top of the stack
4669542Sandreas.hansson@arm.com     */
4679542Sandreas.hansson@arm.com    SenderState *popSenderState();
4689542Sandreas.hansson@arm.com
4699543Ssascha.bischoff@arm.com    /**
4709543Ssascha.bischoff@arm.com     * Go through the sender state stack and return the first instance
4719543Ssascha.bischoff@arm.com     * that is of type T (as determined by a dynamic_cast). If there
4729543Ssascha.bischoff@arm.com     * is no sender state of type T, NULL is returned.
4739543Ssascha.bischoff@arm.com     *
4749543Ssascha.bischoff@arm.com     * @return The topmost state of type T
4759543Ssascha.bischoff@arm.com     */
4769543Ssascha.bischoff@arm.com    template <typename T>
4779543Ssascha.bischoff@arm.com    T * findNextSenderState() const
4789543Ssascha.bischoff@arm.com    {
4799543Ssascha.bischoff@arm.com        T *t = NULL;
4809543Ssascha.bischoff@arm.com        SenderState* sender_state = senderState;
4819543Ssascha.bischoff@arm.com        while (t == NULL && sender_state != NULL) {
4829543Ssascha.bischoff@arm.com            t = dynamic_cast<T*>(sender_state);
4839543Ssascha.bischoff@arm.com            sender_state = sender_state->predecessor;
4849543Ssascha.bischoff@arm.com        }
4859543Ssascha.bischoff@arm.com        return t;
4869543Ssascha.bischoff@arm.com    }
4879543Ssascha.bischoff@arm.com
4885735Snate@binkert.org    /// Return the string name of the cmd field (for debugging and
4895735Snate@binkert.org    /// tracing).
4904022Sstever@eecs.umich.edu    const std::string &cmdString() const { return cmd.toString(); }
4912811Srdreslin@umich.edu
4925735Snate@binkert.org    /// Return the index of this command.
4934022Sstever@eecs.umich.edu    inline int cmdToIndex() const { return cmd.toInt(); }
4942811Srdreslin@umich.edu
49510583SCurtis.Dunham@arm.com    bool isRead() const              { return cmd.isRead(); }
49610583SCurtis.Dunham@arm.com    bool isWrite() const             { return cmd.isWrite(); }
49710583SCurtis.Dunham@arm.com    bool isUpgrade()  const          { return cmd.isUpgrade(); }
49810583SCurtis.Dunham@arm.com    bool isRequest() const           { return cmd.isRequest(); }
49910583SCurtis.Dunham@arm.com    bool isResponse() const          { return cmd.isResponse(); }
50010583SCurtis.Dunham@arm.com    bool needsExclusive() const      { return cmd.needsExclusive(); }
50110583SCurtis.Dunham@arm.com    bool needsResponse() const       { return cmd.needsResponse(); }
50210583SCurtis.Dunham@arm.com    bool isInvalidate() const        { return cmd.isInvalidate(); }
50311199Sandreas.hansson@arm.com    bool isEviction() const          { return cmd.isEviction(); }
50411199Sandreas.hansson@arm.com    bool isWriteback() const         { return cmd.isWriteback(); }
50510583SCurtis.Dunham@arm.com    bool hasData() const             { return cmd.hasData(); }
50610583SCurtis.Dunham@arm.com    bool isLLSC() const              { return cmd.isLLSC(); }
50710583SCurtis.Dunham@arm.com    bool isError() const             { return cmd.isError(); }
50810583SCurtis.Dunham@arm.com    bool isPrint() const             { return cmd.isPrint(); }
50910583SCurtis.Dunham@arm.com    bool isFlush() const             { return cmd.isFlush(); }
5102812Srdreslin@umich.edu
5114870Sstever@eecs.umich.edu    // Snoop flags
51210567Sandreas.hansson@arm.com    void assertMemInhibit()
51310567Sandreas.hansson@arm.com    {
51410567Sandreas.hansson@arm.com        assert(isRequest());
51510567Sandreas.hansson@arm.com        assert(!flags.isSet(MEM_INHIBIT));
51610567Sandreas.hansson@arm.com        flags.set(MEM_INHIBIT);
51710567Sandreas.hansson@arm.com    }
5189951Sstephan.diestelhorst@arm.com    bool memInhibitAsserted() const { return flags.isSet(MEM_INHIBIT); }
5199951Sstephan.diestelhorst@arm.com    void assertShared()             { flags.set(SHARED); }
5209951Sstephan.diestelhorst@arm.com    bool sharedAsserted() const     { return flags.isSet(SHARED); }
5214870Sstever@eecs.umich.edu
5224895Sstever@eecs.umich.edu    // Special control flags
5239951Sstephan.diestelhorst@arm.com    void setExpressSnoop()          { flags.set(EXPRESS_SNOOP); }
5249951Sstephan.diestelhorst@arm.com    bool isExpressSnoop() const     { return flags.isSet(EXPRESS_SNOOP); }
5259951Sstephan.diestelhorst@arm.com    void setSupplyExclusive()       { flags.set(SUPPLY_EXCLUSIVE); }
5269951Sstephan.diestelhorst@arm.com    bool isSupplyExclusive() const  { return flags.isSet(SUPPLY_EXCLUSIVE); }
5279951Sstephan.diestelhorst@arm.com    void setSuppressFuncError()     { flags.set(SUPPRESS_FUNC_ERROR); }
5289951Sstephan.diestelhorst@arm.com    bool suppressFuncError() const  { return flags.isSet(SUPPRESS_FUNC_ERROR); }
52910763Sali.jafri@arm.com    void setBlockCached()          { flags.set(BLOCK_CACHED); }
53010763Sali.jafri@arm.com    bool isBlockCached() const     { return flags.isSet(BLOCK_CACHED); }
53110883Sali.jafri@arm.com    void clearBlockCached()        { flags.clear(BLOCK_CACHED); }
5324895Sstever@eecs.umich.edu
5334870Sstever@eecs.umich.edu    // Network error conditions... encapsulate them as methods since
5344870Sstever@eecs.umich.edu    // their encoding keeps changing (from result field to command
5354870Sstever@eecs.umich.edu    // field, etc.)
5365735Snate@binkert.org    void
5375735Snate@binkert.org    setBadAddress()
5385735Snate@binkert.org    {
5395735Snate@binkert.org        assert(isResponse());
5405735Snate@binkert.org        cmd = MemCmd::BadAddressError;
5415735Snate@binkert.org    }
5425735Snate@binkert.org
5434986Ssaidi@eecs.umich.edu    void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
5442814Srdreslin@umich.edu
54511057Sandreas.hansson@arm.com    Addr getAddr() const { assert(flags.isSet(VALID_ADDR)); return addr; }
5469259SAli.Saidi@ARM.com    /**
5479259SAli.Saidi@ARM.com     * Update the address of this packet mid-transaction. This is used
5489259SAli.Saidi@ARM.com     * by the address mapper to change an already set address to a new
5499259SAli.Saidi@ARM.com     * one based on the system configuration. It is intended to remap
5509259SAli.Saidi@ARM.com     * an existing address, so it asserts that the current address is
5519259SAli.Saidi@ARM.com     * valid.
5529259SAli.Saidi@ARM.com     */
55311057Sandreas.hansson@arm.com    void setAddr(Addr _addr) { assert(flags.isSet(VALID_ADDR)); addr = _addr; }
5549259SAli.Saidi@ARM.com
55511057Sandreas.hansson@arm.com    unsigned getSize() const  { assert(flags.isSet(VALID_SIZE)); return size; }
55610938Sandreas.hansson@arm.com
55710938Sandreas.hansson@arm.com    Addr getOffset(unsigned int blk_size) const
55810938Sandreas.hansson@arm.com    {
55910938Sandreas.hansson@arm.com        return getAddr() & Addr(blk_size - 1);
56010938Sandreas.hansson@arm.com    }
56110938Sandreas.hansson@arm.com
56210938Sandreas.hansson@arm.com    Addr getBlockAddr(unsigned int blk_size) const
56310938Sandreas.hansson@arm.com    {
56410938Sandreas.hansson@arm.com        return getAddr() & ~(Addr(blk_size - 1));
56510938Sandreas.hansson@arm.com    }
5662549SN/A
56711057Sandreas.hansson@arm.com    bool isSecure() const
56811057Sandreas.hansson@arm.com    {
56911057Sandreas.hansson@arm.com        assert(flags.isSet(VALID_ADDR));
57011057Sandreas.hansson@arm.com        return _isSecure;
57111057Sandreas.hansson@arm.com    }
57210028SGiacomo.Gabrielli@arm.com
5735735Snate@binkert.org    /**
5747550SBrad.Beckmann@amd.com     * It has been determined that the SC packet should successfully update
57510938Sandreas.hansson@arm.com     * memory. Therefore, convert this SC packet to a normal write.
5767550SBrad.Beckmann@amd.com     */
5777550SBrad.Beckmann@amd.com    void
5787550SBrad.Beckmann@amd.com    convertScToWrite()
5797550SBrad.Beckmann@amd.com    {
5807550SBrad.Beckmann@amd.com        assert(isLLSC());
5817550SBrad.Beckmann@amd.com        assert(isWrite());
5827550SBrad.Beckmann@amd.com        cmd = MemCmd::WriteReq;
5837550SBrad.Beckmann@amd.com    }
5847550SBrad.Beckmann@amd.com
5857550SBrad.Beckmann@amd.com    /**
58610938Sandreas.hansson@arm.com     * When ruby is in use, Ruby will monitor the cache line and the
58710938Sandreas.hansson@arm.com     * phys memory should treat LL ops as normal reads.
5887550SBrad.Beckmann@amd.com     */
5897550SBrad.Beckmann@amd.com    void
5907550SBrad.Beckmann@amd.com    convertLlToRead()
5917550SBrad.Beckmann@amd.com    {
5927550SBrad.Beckmann@amd.com        assert(isLLSC());
5937550SBrad.Beckmann@amd.com        assert(isRead());
5947550SBrad.Beckmann@amd.com        cmd = MemCmd::ReadReq;
5957550SBrad.Beckmann@amd.com    }
5967550SBrad.Beckmann@amd.com
5977550SBrad.Beckmann@amd.com    /**
59810938Sandreas.hansson@arm.com     * Constructor. Note that a Request object must be constructed
59911057Sandreas.hansson@arm.com     * first, but the Requests's physical address and size fields need
60011057Sandreas.hansson@arm.com     * not be valid. The command must be supplied.
6015735Snate@binkert.org     */
60210569Sandreas.hansson@arm.com    Packet(const RequestPtr _req, MemCmd _cmd)
60311057Sandreas.hansson@arm.com        :  cmd(_cmd), req(_req), data(nullptr), addr(0), _isSecure(false),
60411127Sandreas.hansson@arm.com           size(0), headerDelay(0), snoopDelay(0), payloadDelay(0),
60511057Sandreas.hansson@arm.com           senderState(NULL)
60611057Sandreas.hansson@arm.com    {
60711057Sandreas.hansson@arm.com        if (req->hasPaddr()) {
60811057Sandreas.hansson@arm.com            addr = req->getPaddr();
60911057Sandreas.hansson@arm.com            flags.set(VALID_ADDR);
61011057Sandreas.hansson@arm.com            _isSecure = req->isSecure();
61111057Sandreas.hansson@arm.com        }
61211057Sandreas.hansson@arm.com        if (req->hasSize()) {
61311057Sandreas.hansson@arm.com            size = req->getSize();
61411057Sandreas.hansson@arm.com            flags.set(VALID_SIZE);
61511057Sandreas.hansson@arm.com        }
61611057Sandreas.hansson@arm.com    }
61711057Sandreas.hansson@arm.com
61811057Sandreas.hansson@arm.com    /**
61911057Sandreas.hansson@arm.com     * Alternate constructor if you are trying to create a packet with
62011057Sandreas.hansson@arm.com     * a request that is for a whole block, not the address from the
62111057Sandreas.hansson@arm.com     * req.  this allows for overriding the size/addr of the req.
62211057Sandreas.hansson@arm.com     */
62311057Sandreas.hansson@arm.com    Packet(const RequestPtr _req, MemCmd _cmd, int _blkSize)
62411057Sandreas.hansson@arm.com        :  cmd(_cmd), req(_req), data(nullptr), addr(0), _isSecure(false),
62511127Sandreas.hansson@arm.com           headerDelay(0), snoopDelay(0), payloadDelay(0),
6269546Sandreas.hansson@arm.com           senderState(NULL)
62711057Sandreas.hansson@arm.com    {
62811057Sandreas.hansson@arm.com        if (req->hasPaddr()) {
62911057Sandreas.hansson@arm.com            addr = req->getPaddr() & ~(_blkSize - 1);
63011057Sandreas.hansson@arm.com            flags.set(VALID_ADDR);
63111057Sandreas.hansson@arm.com            _isSecure = req->isSecure();
63211057Sandreas.hansson@arm.com        }
63311057Sandreas.hansson@arm.com        size = _blkSize;
63411057Sandreas.hansson@arm.com        flags.set(VALID_SIZE);
63511057Sandreas.hansson@arm.com    }
6364626Sstever@eecs.umich.edu
6375735Snate@binkert.org    /**
6385735Snate@binkert.org     * Alternate constructor for copying a packet.  Copy all fields
6394887Sstever@eecs.umich.edu     * *except* if the original packet's data was dynamic, don't copy
6404887Sstever@eecs.umich.edu     * that, as we can't guarantee that the new packet's lifetime is
6414887Sstever@eecs.umich.edu     * less than that of the original packet.  In this case the new
6425735Snate@binkert.org     * packet should allocate its own data.
6435735Snate@binkert.org     */
64410896Snilay@cs.wisc.edu    Packet(const PacketPtr pkt, bool clear_flags, bool alloc_data)
6455735Snate@binkert.org        :  cmd(pkt->cmd), req(pkt->req),
64610571Sandreas.hansson@arm.com           data(nullptr),
64710028SGiacomo.Gabrielli@arm.com           addr(pkt->addr), _isSecure(pkt->_isSecure), size(pkt->size),
64810723Sandreas.hansson@arm.com           bytesValid(pkt->bytesValid),
64910694SMarco.Balboni@ARM.com           headerDelay(pkt->headerDelay),
65011127Sandreas.hansson@arm.com           snoopDelay(0),
65110694SMarco.Balboni@ARM.com           payloadDelay(pkt->payloadDelay),
6529546Sandreas.hansson@arm.com           senderState(pkt->senderState)
6534626Sstever@eecs.umich.edu    {
65410571Sandreas.hansson@arm.com        if (!clear_flags)
6555735Snate@binkert.org            flags.set(pkt->flags & COPY_FLAGS);
6565735Snate@binkert.org
65711057Sandreas.hansson@arm.com        flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE));
65811057Sandreas.hansson@arm.com
65910571Sandreas.hansson@arm.com        // should we allocate space for data, or not, the express
66010571Sandreas.hansson@arm.com        // snoops do not need to carry any data as they only serve to
66110571Sandreas.hansson@arm.com        // co-ordinate state changes
66210571Sandreas.hansson@arm.com        if (alloc_data) {
66310571Sandreas.hansson@arm.com            // even if asked to allocate data, if the original packet
66410571Sandreas.hansson@arm.com            // holds static data, then the sender will not be doing
66510571Sandreas.hansson@arm.com            // any memcpy on receiving the response, thus we simply
66610571Sandreas.hansson@arm.com            // carry the pointer forward
66710571Sandreas.hansson@arm.com            if (pkt->flags.isSet(STATIC_DATA)) {
66810571Sandreas.hansson@arm.com                data = pkt->data;
66910571Sandreas.hansson@arm.com                flags.set(STATIC_DATA);
67010571Sandreas.hansson@arm.com            } else {
67110571Sandreas.hansson@arm.com                allocate();
67210571Sandreas.hansson@arm.com            }
67310571Sandreas.hansson@arm.com        }
67410342SCurtis.Dunham@arm.com    }
6758668Sgeoffrey.blake@arm.com
67610342SCurtis.Dunham@arm.com    /**
67710739Ssteve.reinhardt@amd.com     * Generate the appropriate read MemCmd based on the Request flags.
67810342SCurtis.Dunham@arm.com     */
67910739Ssteve.reinhardt@amd.com    static MemCmd
68010739Ssteve.reinhardt@amd.com    makeReadCmd(const RequestPtr req)
68110342SCurtis.Dunham@arm.com    {
68210739Ssteve.reinhardt@amd.com        if (req->isLLSC())
68310739Ssteve.reinhardt@amd.com            return MemCmd::LoadLockedReq;
68410739Ssteve.reinhardt@amd.com        else if (req->isPrefetch())
68510739Ssteve.reinhardt@amd.com            return MemCmd::SoftPFReq;
68610739Ssteve.reinhardt@amd.com        else
68710739Ssteve.reinhardt@amd.com            return MemCmd::ReadReq;
68810739Ssteve.reinhardt@amd.com    }
68910739Ssteve.reinhardt@amd.com
69010739Ssteve.reinhardt@amd.com    /**
69110739Ssteve.reinhardt@amd.com     * Generate the appropriate write MemCmd based on the Request flags.
69210739Ssteve.reinhardt@amd.com     */
69310739Ssteve.reinhardt@amd.com    static MemCmd
69410739Ssteve.reinhardt@amd.com    makeWriteCmd(const RequestPtr req)
69510739Ssteve.reinhardt@amd.com    {
69610739Ssteve.reinhardt@amd.com        if (req->isLLSC())
69710739Ssteve.reinhardt@amd.com            return MemCmd::StoreCondReq;
69810739Ssteve.reinhardt@amd.com        else if (req->isSwap())
69910739Ssteve.reinhardt@amd.com            return MemCmd::SwapReq;
70010739Ssteve.reinhardt@amd.com        else
70110739Ssteve.reinhardt@amd.com            return MemCmd::WriteReq;
70210342SCurtis.Dunham@arm.com    }
70310342SCurtis.Dunham@arm.com
70410342SCurtis.Dunham@arm.com    /**
70510342SCurtis.Dunham@arm.com     * Constructor-like methods that return Packets based on Request objects.
70610739Ssteve.reinhardt@amd.com     * Fine-tune the MemCmd type if it's not a vanilla read or write.
70710342SCurtis.Dunham@arm.com     */
70810342SCurtis.Dunham@arm.com    static PacketPtr
70910569Sandreas.hansson@arm.com    createRead(const RequestPtr req)
71010342SCurtis.Dunham@arm.com    {
71110739Ssteve.reinhardt@amd.com        return new Packet(req, makeReadCmd(req));
71210342SCurtis.Dunham@arm.com    }
71310342SCurtis.Dunham@arm.com
71410342SCurtis.Dunham@arm.com    static PacketPtr
71510569Sandreas.hansson@arm.com    createWrite(const RequestPtr req)
71610342SCurtis.Dunham@arm.com    {
71710739Ssteve.reinhardt@amd.com        return new Packet(req, makeWriteCmd(req));
7182641Sstever@eecs.umich.edu    }
7192549SN/A
7205735Snate@binkert.org    /**
7215735Snate@binkert.org     * clean up packet variables
7225735Snate@binkert.org     */
7232566SN/A    ~Packet()
7245387Sstever@gmail.com    {
72510872Sali.jafri@arm.com        // Delete the request object if this is a request packet which
72610872Sali.jafri@arm.com        // does not need a response, because the requester will not get
72710872Sali.jafri@arm.com        // a chance. If the request packet needs a response then the
72810872Sali.jafri@arm.com        // request will be deleted on receipt of the response
72910872Sali.jafri@arm.com        // packet. We also make sure to never delete the request for
73010872Sali.jafri@arm.com        // express snoops, even for cases when responses are not
73110872Sali.jafri@arm.com        // needed (CleanEvict and Writeback), since the snoop packet
73210872Sali.jafri@arm.com        // re-uses the same request.
73310872Sali.jafri@arm.com        if (req && isRequest() && !needsResponse() &&
73410872Sali.jafri@arm.com            !isExpressSnoop()) {
7355387Sstever@gmail.com            delete req;
73610872Sali.jafri@arm.com        }
7375735Snate@binkert.org        deleteData();
7385387Sstever@gmail.com    }
7392566SN/A
7405735Snate@binkert.org    /**
7414626Sstever@eecs.umich.edu     * Take a request packet and modify it in place to be suitable for
74210660Sandreas.hansson@arm.com     * returning as a response to that request.
7434626Sstever@eecs.umich.edu     */
7445735Snate@binkert.org    void
7455735Snate@binkert.org    makeResponse()
7464626Sstever@eecs.umich.edu    {
7472662Sstever@eecs.umich.edu        assert(needsResponse());
7482855Srdreslin@umich.edu        assert(isRequest());
7494022Sstever@eecs.umich.edu        cmd = cmd.responseCommand();
7505745Snate@binkert.org
7517464Ssteve.reinhardt@amd.com        // responses are never express, even if the snoop that
7527464Ssteve.reinhardt@amd.com        // triggered them was
7537464Ssteve.reinhardt@amd.com        flags.clear(EXPRESS_SNOOP);
7542641Sstever@eecs.umich.edu    }
7552641Sstever@eecs.umich.edu
7565735Snate@binkert.org    void
7575735Snate@binkert.org    makeAtomicResponse()
7584870Sstever@eecs.umich.edu    {
7594870Sstever@eecs.umich.edu        makeResponse();
7604870Sstever@eecs.umich.edu    }
7614870Sstever@eecs.umich.edu
7625735Snate@binkert.org    void
7635735Snate@binkert.org    makeTimingResponse()
7643348Sbinkertn@umich.edu    {
7654870Sstever@eecs.umich.edu        makeResponse();
7663135Srdreslin@umich.edu    }
7673135Srdreslin@umich.edu
7688436SBrad.Beckmann@amd.com    void
7698436SBrad.Beckmann@amd.com    setFunctionalResponseStatus(bool success)
7708436SBrad.Beckmann@amd.com    {
7718436SBrad.Beckmann@amd.com        if (!success) {
7728436SBrad.Beckmann@amd.com            if (isWrite()) {
7738436SBrad.Beckmann@amd.com                cmd = MemCmd::FunctionalWriteError;
7748436SBrad.Beckmann@amd.com            } else {
7758436SBrad.Beckmann@amd.com                cmd = MemCmd::FunctionalReadError;
7768436SBrad.Beckmann@amd.com            }
7778436SBrad.Beckmann@amd.com        }
7788436SBrad.Beckmann@amd.com    }
7798436SBrad.Beckmann@amd.com
78011057Sandreas.hansson@arm.com    void
78111057Sandreas.hansson@arm.com    setSize(unsigned size)
78211057Sandreas.hansson@arm.com    {
78311057Sandreas.hansson@arm.com        assert(!flags.isSet(VALID_SIZE));
78411057Sandreas.hansson@arm.com
78511057Sandreas.hansson@arm.com        this->size = size;
78611057Sandreas.hansson@arm.com        flags.set(VALID_SIZE);
78711057Sandreas.hansson@arm.com    }
78811057Sandreas.hansson@arm.com
78911057Sandreas.hansson@arm.com
79011013Sandreas.sandberg@arm.com  public:
79111013Sandreas.sandberg@arm.com    /**
79211013Sandreas.sandberg@arm.com     * @{
79311013Sandreas.sandberg@arm.com     * @name Data accessor mehtods
79411013Sandreas.sandberg@arm.com     */
79511013Sandreas.sandberg@arm.com
7963348Sbinkertn@umich.edu    /**
7973348Sbinkertn@umich.edu     * Set the data pointer to the following value that should not be
79810571Sandreas.hansson@arm.com     * freed. Static data allows us to do a single memcpy even if
79910571Sandreas.hansson@arm.com     * multiple packets are required to get from source to destination
80010571Sandreas.hansson@arm.com     * and back. In essence the pointer is set calling dataStatic on
80110571Sandreas.hansson@arm.com     * the original packet, and whenever this packet is copied and
80210571Sandreas.hansson@arm.com     * forwarded the same pointer is passed on. When a packet
80310571Sandreas.hansson@arm.com     * eventually reaches the destination holding the data, it is
80410571Sandreas.hansson@arm.com     * copied once into the location originally set. On the way back
80510571Sandreas.hansson@arm.com     * to the source, no copies are necessary.
8062566SN/A     */
8072566SN/A    template <typename T>
8083348Sbinkertn@umich.edu    void
8093348Sbinkertn@umich.edu    dataStatic(T *p)
8103348Sbinkertn@umich.edu    {
81110566Sandreas.hansson@arm.com        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
8123348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
8135735Snate@binkert.org        flags.set(STATIC_DATA);
8143348Sbinkertn@umich.edu    }
8152566SN/A
8163348Sbinkertn@umich.edu    /**
81710564Sandreas.hansson@arm.com     * Set the data pointer to the following value that should not be
81810564Sandreas.hansson@arm.com     * freed. This version of the function allows the pointer passed
81910564Sandreas.hansson@arm.com     * to us to be const. To avoid issues down the line we cast the
82010564Sandreas.hansson@arm.com     * constness away, the alternative would be to keep both a const
82110564Sandreas.hansson@arm.com     * and non-const data pointer and cleverly choose between
82210564Sandreas.hansson@arm.com     * them. Note that this is only allowed for static data.
82310564Sandreas.hansson@arm.com     */
82410564Sandreas.hansson@arm.com    template <typename T>
82510564Sandreas.hansson@arm.com    void
82610564Sandreas.hansson@arm.com    dataStaticConst(const T *p)
82710564Sandreas.hansson@arm.com    {
82810566Sandreas.hansson@arm.com        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
82910564Sandreas.hansson@arm.com        data = const_cast<PacketDataPtr>(p);
83010564Sandreas.hansson@arm.com        flags.set(STATIC_DATA);
83110564Sandreas.hansson@arm.com    }
83210564Sandreas.hansson@arm.com
83310564Sandreas.hansson@arm.com    /**
8343348Sbinkertn@umich.edu     * Set the data pointer to a value that should have delete []
83510571Sandreas.hansson@arm.com     * called on it. Dynamic data is local to this packet, and as the
83610571Sandreas.hansson@arm.com     * packet travels from source to destination, forwarded packets
83710571Sandreas.hansson@arm.com     * will allocate their own data. When a packet reaches the final
83810571Sandreas.hansson@arm.com     * destination it will populate the dynamic data of that specific
83910571Sandreas.hansson@arm.com     * packet, and on the way back towards the source, memcpy will be
84010571Sandreas.hansson@arm.com     * invoked in every step where a new packet was created e.g. in
84110571Sandreas.hansson@arm.com     * the caches. Ultimately when the response reaches the source a
84210571Sandreas.hansson@arm.com     * final memcpy is needed to extract the data from the packet
84310571Sandreas.hansson@arm.com     * before it is deallocated.
8443348Sbinkertn@umich.edu     */
8452566SN/A    template <typename T>
8463348Sbinkertn@umich.edu    void
8473348Sbinkertn@umich.edu    dataDynamic(T *p)
8483348Sbinkertn@umich.edu    {
84910566Sandreas.hansson@arm.com        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
8503348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
8515735Snate@binkert.org        flags.set(DYNAMIC_DATA);
8523348Sbinkertn@umich.edu    }
8533348Sbinkertn@umich.edu
8545735Snate@binkert.org    /**
8555735Snate@binkert.org     * get a pointer to the data ptr.
8565735Snate@binkert.org     */
8573348Sbinkertn@umich.edu    template <typename T>
8583348Sbinkertn@umich.edu    T*
85910562Sandreas.hansson@arm.com    getPtr()
8603348Sbinkertn@umich.edu    {
86110562Sandreas.hansson@arm.com        assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
8623348Sbinkertn@umich.edu        return (T*)data;
8633348Sbinkertn@umich.edu    }
8642566SN/A
86510563Sandreas.hansson@arm.com    template <typename T>
86610563Sandreas.hansson@arm.com    const T*
86710563Sandreas.hansson@arm.com    getConstPtr() const
86810563Sandreas.hansson@arm.com    {
86910563Sandreas.hansson@arm.com        assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
87010563Sandreas.hansson@arm.com        return (const T*)data;
87110563Sandreas.hansson@arm.com    }
87210563Sandreas.hansson@arm.com
8735735Snate@binkert.org    /**
87411013Sandreas.sandberg@arm.com     * Get the data in the packet byte swapped from big endian to
87511013Sandreas.sandberg@arm.com     * host endian.
87611013Sandreas.sandberg@arm.com     */
87711013Sandreas.sandberg@arm.com    template <typename T>
87811013Sandreas.sandberg@arm.com    T getBE() const;
87911013Sandreas.sandberg@arm.com
88011013Sandreas.sandberg@arm.com    /**
88111013Sandreas.sandberg@arm.com     * Get the data in the packet byte swapped from little endian to
88211013Sandreas.sandberg@arm.com     * host endian.
88311013Sandreas.sandberg@arm.com     */
88411013Sandreas.sandberg@arm.com    template <typename T>
88511013Sandreas.sandberg@arm.com    T getLE() const;
88611013Sandreas.sandberg@arm.com
88711013Sandreas.sandberg@arm.com    /**
88811013Sandreas.sandberg@arm.com     * Get the data in the packet byte swapped from the specified
88911013Sandreas.sandberg@arm.com     * endianness.
89011013Sandreas.sandberg@arm.com     */
89111013Sandreas.sandberg@arm.com    template <typename T>
89211013Sandreas.sandberg@arm.com    T get(ByteOrder endian) const;
89311013Sandreas.sandberg@arm.com
89411013Sandreas.sandberg@arm.com    /**
89511013Sandreas.sandberg@arm.com     * Get the data in the packet byte swapped from guest to host
89611013Sandreas.sandberg@arm.com     * endian.
8975735Snate@binkert.org     */
8982566SN/A    template <typename T>
89910563Sandreas.hansson@arm.com    T get() const;
9002566SN/A
90111013Sandreas.sandberg@arm.com    /** Set the value in the data pointer to v as big endian. */
90211013Sandreas.sandberg@arm.com    template <typename T>
90311013Sandreas.sandberg@arm.com    void setBE(T v);
90411013Sandreas.sandberg@arm.com
90511013Sandreas.sandberg@arm.com    /** Set the value in the data pointer to v as little endian. */
90611013Sandreas.sandberg@arm.com    template <typename T>
90711013Sandreas.sandberg@arm.com    void setLE(T v);
90811013Sandreas.sandberg@arm.com
9095735Snate@binkert.org    /**
91011013Sandreas.sandberg@arm.com     * Set the value in the data pointer to v using the specified
91111013Sandreas.sandberg@arm.com     * endianness.
9125735Snate@binkert.org     */
9132566SN/A    template <typename T>
91411013Sandreas.sandberg@arm.com    void set(T v, ByteOrder endian);
91511013Sandreas.sandberg@arm.com
91611013Sandreas.sandberg@arm.com    /** Set the value in the data pointer to v as guest endian. */
91711013Sandreas.sandberg@arm.com    template <typename T>
9182592SN/A    void set(T v);
9192566SN/A
9203348Sbinkertn@umich.edu    /**
9214626Sstever@eecs.umich.edu     * Copy data into the packet from the provided pointer.
9224626Sstever@eecs.umich.edu     */
9235735Snate@binkert.org    void
92410563Sandreas.hansson@arm.com    setData(const uint8_t *p)
9254626Sstever@eecs.umich.edu    {
92610571Sandreas.hansson@arm.com        // we should never be copying data onto itself, which means we
92710571Sandreas.hansson@arm.com        // must idenfity packets with static data, as they carry the
92810571Sandreas.hansson@arm.com        // same pointer from source to destination and back
92910571Sandreas.hansson@arm.com        assert(p != getPtr<uint8_t>() || flags.isSet(STATIC_DATA));
93010571Sandreas.hansson@arm.com
9317691SAli.Saidi@ARM.com        if (p != getPtr<uint8_t>())
93210571Sandreas.hansson@arm.com            // for packet with allocated dynamic data, we copy data from
93310571Sandreas.hansson@arm.com            // one to the other, e.g. a forwarded response to a response
9347691SAli.Saidi@ARM.com            std::memcpy(getPtr<uint8_t>(), p, getSize());
9354626Sstever@eecs.umich.edu    }
9364626Sstever@eecs.umich.edu
9374626Sstever@eecs.umich.edu    /**
9384626Sstever@eecs.umich.edu     * Copy data into the packet from the provided block pointer,
9394626Sstever@eecs.umich.edu     * which is aligned to the given block size.
9404626Sstever@eecs.umich.edu     */
9415735Snate@binkert.org    void
94210563Sandreas.hansson@arm.com    setDataFromBlock(const uint8_t *blk_data, int blkSize)
9434626Sstever@eecs.umich.edu    {
9444626Sstever@eecs.umich.edu        setData(blk_data + getOffset(blkSize));
9454626Sstever@eecs.umich.edu    }
9464626Sstever@eecs.umich.edu
9474626Sstever@eecs.umich.edu    /**
9484626Sstever@eecs.umich.edu     * Copy data from the packet to the provided block pointer, which
9494626Sstever@eecs.umich.edu     * is aligned to the given block size.
9504626Sstever@eecs.umich.edu     */
9515735Snate@binkert.org    void
95210563Sandreas.hansson@arm.com    writeData(uint8_t *p) const
9534626Sstever@eecs.umich.edu    {
95410563Sandreas.hansson@arm.com        std::memcpy(p, getConstPtr<uint8_t>(), getSize());
9554626Sstever@eecs.umich.edu    }
9564626Sstever@eecs.umich.edu
9574626Sstever@eecs.umich.edu    /**
9584626Sstever@eecs.umich.edu     * Copy data from the packet to the memory at the provided pointer.
9594626Sstever@eecs.umich.edu     */
9605735Snate@binkert.org    void
96110563Sandreas.hansson@arm.com    writeDataToBlock(uint8_t *blk_data, int blkSize) const
9624626Sstever@eecs.umich.edu    {
9634626Sstever@eecs.umich.edu        writeData(blk_data + getOffset(blkSize));
9644626Sstever@eecs.umich.edu    }
9654626Sstever@eecs.umich.edu
9664626Sstever@eecs.umich.edu    /**
9673348Sbinkertn@umich.edu     * delete the data pointed to in the data pointer. Ok to call to
9683348Sbinkertn@umich.edu     * matter how data was allocted.
9693348Sbinkertn@umich.edu     */
9705735Snate@binkert.org    void
9715735Snate@binkert.org    deleteData()
9725735Snate@binkert.org    {
97310566Sandreas.hansson@arm.com        if (flags.isSet(DYNAMIC_DATA))
9745735Snate@binkert.org            delete [] data;
9755735Snate@binkert.org
97610566Sandreas.hansson@arm.com        flags.clear(STATIC_DATA|DYNAMIC_DATA);
9775735Snate@binkert.org        data = NULL;
9785735Snate@binkert.org    }
9792566SN/A
98010565Sandreas.hansson@arm.com    /** Allocate memory for the packet. */
9815735Snate@binkert.org    void
9825735Snate@binkert.org    allocate()
9835735Snate@binkert.org    {
9845764Snate@binkert.org        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
98510566Sandreas.hansson@arm.com        flags.set(DYNAMIC_DATA);
9865745Snate@binkert.org        data = new uint8_t[getSize()];
9875735Snate@binkert.org    }
9885735Snate@binkert.org
98911013Sandreas.sandberg@arm.com    /** @} */
99011013Sandreas.sandberg@arm.com
99111013Sandreas.sandberg@arm.com  private: // Private data accessor methods
99211013Sandreas.sandberg@arm.com    /** Get the data in the packet without byte swapping. */
99311013Sandreas.sandberg@arm.com    template <typename T>
99411013Sandreas.sandberg@arm.com    T getRaw() const;
99511013Sandreas.sandberg@arm.com
99611013Sandreas.sandberg@arm.com    /** Set the value in the data pointer to v without byte swapping. */
99711013Sandreas.sandberg@arm.com    template <typename T>
99811013Sandreas.sandberg@arm.com    void setRaw(T v);
99911013Sandreas.sandberg@arm.com
100011013Sandreas.sandberg@arm.com  public:
10014626Sstever@eecs.umich.edu    /**
100210570Sandreas.hansson@arm.com     * Check a functional request against a memory value stored in
100310570Sandreas.hansson@arm.com     * another packet (i.e. an in-transit request or
100410570Sandreas.hansson@arm.com     * response). Returns true if the current packet is a read, and
100510570Sandreas.hansson@arm.com     * the other packet provides the data, which is then copied to the
100610570Sandreas.hansson@arm.com     * current packet. If the current packet is a write, and the other
100710570Sandreas.hansson@arm.com     * packet intersects this one, then we update the data
100810570Sandreas.hansson@arm.com     * accordingly.
100910570Sandreas.hansson@arm.com     */
101010570Sandreas.hansson@arm.com    bool
101110570Sandreas.hansson@arm.com    checkFunctional(PacketPtr other)
101210570Sandreas.hansson@arm.com    {
101310570Sandreas.hansson@arm.com        // all packets that are carrying a payload should have a valid
101410570Sandreas.hansson@arm.com        // data pointer
101510570Sandreas.hansson@arm.com        return checkFunctional(other, other->getAddr(), other->isSecure(),
101610570Sandreas.hansson@arm.com                               other->getSize(),
101710570Sandreas.hansson@arm.com                               other->hasData() ?
101810570Sandreas.hansson@arm.com                               other->getPtr<uint8_t>() : NULL);
101910570Sandreas.hansson@arm.com    }
102010570Sandreas.hansson@arm.com
102110570Sandreas.hansson@arm.com    /**
102210883Sali.jafri@arm.com     * Does the request need to check for cached copies of the same block
102310883Sali.jafri@arm.com     * in the memory hierarchy above.
102410883Sali.jafri@arm.com     **/
102510883Sali.jafri@arm.com    bool
102610883Sali.jafri@arm.com    mustCheckAbove() const
102710883Sali.jafri@arm.com    {
102811199Sandreas.hansson@arm.com        return cmd == MemCmd::HardPFReq || isEviction();
102911199Sandreas.hansson@arm.com    }
103011199Sandreas.hansson@arm.com
103111199Sandreas.hansson@arm.com    /**
103211199Sandreas.hansson@arm.com     * Is this packet a clean eviction, including both actual clean
103311199Sandreas.hansson@arm.com     * evict packets, but also clean writebacks.
103411199Sandreas.hansson@arm.com     */
103511199Sandreas.hansson@arm.com    bool
103611199Sandreas.hansson@arm.com    isCleanEviction() const
103711199Sandreas.hansson@arm.com    {
103811199Sandreas.hansson@arm.com        return cmd == MemCmd::CleanEvict || cmd == MemCmd::WritebackClean;
103910883Sali.jafri@arm.com    }
104010883Sali.jafri@arm.com
104110883Sali.jafri@arm.com    /**
10424626Sstever@eecs.umich.edu     * Check a functional request against a memory value represented
104310570Sandreas.hansson@arm.com     * by a base/size pair and an associated data array. If the
104410570Sandreas.hansson@arm.com     * current packet is a read, it may be satisfied by the memory
104510570Sandreas.hansson@arm.com     * value. If the current packet is a write, it may update the
10464626Sstever@eecs.umich.edu     * memory value.
10474626Sstever@eecs.umich.edu     */
10485735Snate@binkert.org    bool
104910570Sandreas.hansson@arm.com    checkFunctional(Printable *obj, Addr base, bool is_secure, int size,
105010570Sandreas.hansson@arm.com                    uint8_t *_data);
10515314Sstever@gmail.com
10525315Sstever@gmail.com    /**
10535315Sstever@gmail.com     * Push label for PrintReq (safe to call unconditionally).
10545315Sstever@gmail.com     */
10555735Snate@binkert.org    void
10565735Snate@binkert.org    pushLabel(const std::string &lbl)
10575735Snate@binkert.org    {
10585735Snate@binkert.org        if (isPrint())
10595735Snate@binkert.org            safe_cast<PrintReqState*>(senderState)->pushLabel(lbl);
10605314Sstever@gmail.com    }
10615314Sstever@gmail.com
10625315Sstever@gmail.com    /**
10635315Sstever@gmail.com     * Pop label for PrintReq (safe to call unconditionally).
10645315Sstever@gmail.com     */
10655735Snate@binkert.org    void
10665735Snate@binkert.org    popLabel()
10675735Snate@binkert.org    {
10685735Snate@binkert.org        if (isPrint())
10695735Snate@binkert.org            safe_cast<PrintReqState*>(senderState)->popLabel();
10705314Sstever@gmail.com    }
10715314Sstever@gmail.com
10725314Sstever@gmail.com    void print(std::ostream &o, int verbosity = 0,
10735314Sstever@gmail.com               const std::string &prefix = "") const;
10749663Suri.wiener@arm.com
10759663Suri.wiener@arm.com    /**
10769663Suri.wiener@arm.com     * A no-args wrapper of print(std::ostream...)
10779663Suri.wiener@arm.com     * meant to be invoked from DPRINTFs
10789663Suri.wiener@arm.com     * avoiding string overheads in fast mode
10799663Suri.wiener@arm.com     * @return string with the request's type and start<->end addresses
10809663Suri.wiener@arm.com     */
10819663Suri.wiener@arm.com    std::string print() const;
10822381SN/A};
10832381SN/A
10842381SN/A#endif //__MEM_PACKET_HH
1085