packet.hh revision 10694
12381SN/A/*
212652Sandreas.sandberg@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 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
4512349Snikos.nikoleris@arm.com */
462381SN/A
472381SN/A/**
482381SN/A * @file
492381SN/A * Declaration of the Packet class.
502662Sstever@eecs.umich.edu */
512381SN/A
522381SN/A#ifndef __MEM_PACKET_HH__
532381SN/A#define __MEM_PACKET_HH__
542381SN/A
552381SN/A#include <bitset>
568229Snate@binkert.org#include <cassert>
573348Sbinkertn@umich.edu#include <list>
583348Sbinkertn@umich.edu
593348Sbinkertn@umich.edu#include "base/cast.hh"
605735Snate@binkert.org#include "base/compiler.hh"
614024Sbinkertn@umich.edu#include "base/flags.hh"
625735Snate@binkert.org#include "base/misc.hh"
6312334Sgabeblack@google.com#include "base/printable.hh"
645314Sstever@gmail.com#include "base/types.hh"
656216Snate@binkert.org#include "mem/request.hh"
6613347Sgabeblack@google.com#include "sim/core.hh"
672392SN/A
684167Sbinkertn@umich.educlass Packet;
692394SN/Atypedef Packet *PacketPtr;
708737Skoansin.tan@gmail.comtypedef uint8_t* PacketDataPtr;
713349Sbinkertn@umich.edutypedef std::list<PacketPtr> PacketList;
722394SN/A
732812Srdreslin@umich.educlass MemCmd
7412351Snikos.nikoleris@arm.com{
752812Srdreslin@umich.edu    friend class Packet;
764022Sstever@eecs.umich.edu
774022Sstever@eecs.umich.edu  public:
785735Snate@binkert.org    /**
795735Snate@binkert.org     * List of all commands associated with a packet.
804022Sstever@eecs.umich.edu     */
815735Snate@binkert.org    enum Command
825735Snate@binkert.org    {
835735Snate@binkert.org        InvalidCmd,
844022Sstever@eecs.umich.edu        ReadReq,
854022Sstever@eecs.umich.edu        ReadResp,
864022Sstever@eecs.umich.edu        ReadRespWithInvalidate,
874022Sstever@eecs.umich.edu        WriteReq,
884473Sstever@eecs.umich.edu        WriteResp,
895319Sstever@gmail.com        Writeback,
904022Sstever@eecs.umich.edu        SoftPFReq,
914022Sstever@eecs.umich.edu        HardPFReq,
9211199Sandreas.hansson@arm.com        SoftPFResp,
9311199Sandreas.hansson@arm.com        HardPFResp,
9412344Snikos.nikoleris@arm.com        WriteInvalidateReq,
9510883Sali.jafri@arm.com        WriteInvalidateResp,
964022Sstever@eecs.umich.edu        UpgradeReq,
974022Sstever@eecs.umich.edu        SCUpgradeReq,           // Special "weak" upgrade for StoreCond
984022Sstever@eecs.umich.edu        UpgradeResp,
994022Sstever@eecs.umich.edu        SCUpgradeFailReq,       // Failed SCUpgradeReq in MSHR (never sent)
10010886Sandreas.hansson@arm.com        UpgradeFailResp,        // Valid for SCUpgradeReq only
1014022Sstever@eecs.umich.edu        ReadExReq,
1027465Ssteve.reinhardt@amd.com        ReadExResp,
1034628Sstever@eecs.umich.edu        LoadLockedReq,
1047465Ssteve.reinhardt@amd.com        StoreCondReq,
1057465Ssteve.reinhardt@amd.com        StoreCondFailReq,       // Failed StoreCondReq in MSHR (never sent)
1064022Sstever@eecs.umich.edu        StoreCondResp,
1074022Sstever@eecs.umich.edu        SwapReq,
10810885Sandreas.hansson@arm.com        SwapResp,
10910885Sandreas.hansson@arm.com        MessageReq,
1104626Sstever@eecs.umich.edu        MessageResp,
1114626Sstever@eecs.umich.edu        // Error responses
1127669Ssteve.reinhardt@amd.com        // @TODO these should be classified as responses rather than
1134626Sstever@eecs.umich.edu        // requests; coding them as requests initially for backwards
1144040Ssaidi@eecs.umich.edu        // compatibility
1154040Ssaidi@eecs.umich.edu        InvalidDestError,  // packet dest field invalid
1165650Sgblack@eecs.umich.edu        BadAddressError,   // memory address invalid
1175650Sgblack@eecs.umich.edu        FunctionalReadError, // unable to fulfill functional read
11811256Santhony.gutierrez@amd.com        FunctionalWriteError, // unable to fulfill functional write
11911256Santhony.gutierrez@amd.com        // Fake simulator-only commands
12012347Snikos.nikoleris@arm.com        PrintReq,       // Print state matching address
12112347Snikos.nikoleris@arm.com        FlushReq,      //request for a cache flush
12212347Snikos.nikoleris@arm.com        InvalidationReq,   // request for address to be invalidated from lsq
12312347Snikos.nikoleris@arm.com        NUM_MEM_CMDS
1244870Sstever@eecs.umich.edu    };
1254870Sstever@eecs.umich.edu
1264870Sstever@eecs.umich.edu  private:
1274870Sstever@eecs.umich.edu    /**
1284870Sstever@eecs.umich.edu     * List of command attributes.
1294870Sstever@eecs.umich.edu     */
1308436SBrad.Beckmann@amd.com    enum Attribute
1318436SBrad.Beckmann@amd.com    {
1325314Sstever@gmail.com        IsRead,         //!< Data flows from responder to requester
1335314Sstever@gmail.com        IsWrite,        //!< Data flows from requester to responder
1348184Ssomayeh@cs.wisc.edu        IsUpgrade,
13510886Sandreas.hansson@arm.com        IsInvalidate,
13610886Sandreas.hansson@arm.com        NeedsExclusive, //!< Requires exclusive copy to complete in-cache
1374022Sstever@eecs.umich.edu        IsRequest,      //!< Issued by requester
1384022Sstever@eecs.umich.edu        IsResponse,     //!< Issue by responder
1394022Sstever@eecs.umich.edu        NeedsResponse,  //!< Requester needs response from target
1404022Sstever@eecs.umich.edu        IsSWPrefetch,
1415735Snate@binkert.org        IsHWPrefetch,
1425735Snate@binkert.org        IsLlsc,         //!< Alpha/MIPS LL or SC access
1435735Snate@binkert.org        HasData,        //!< There is an associated payload
1444022Sstever@eecs.umich.edu        IsError,        //!< Error response
1454022Sstever@eecs.umich.edu        IsPrint,        //!< Print state matching address (for debugging)
1464626Sstever@eecs.umich.edu        IsFlush,        //!< Flush the address from caches
1474626Sstever@eecs.umich.edu        NUM_COMMAND_ATTRIBUTES
1487465Ssteve.reinhardt@amd.com    };
1494022Sstever@eecs.umich.edu
15012347Snikos.nikoleris@arm.com    /**
15111284Sandreas.hansson@arm.com     * Structure that defines attributes and other data associated
1524626Sstever@eecs.umich.edu     * with a Command.
1534626Sstever@eecs.umich.edu     */
1544626Sstever@eecs.umich.edu    struct CommandInfo
15511199Sandreas.hansson@arm.com    {
1564022Sstever@eecs.umich.edu        /// Set of attribute flags.
1574022Sstever@eecs.umich.edu        const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
1586076Sgblack@eecs.umich.edu        /// Corresponding response for requests; InvalidCmd if no
1594626Sstever@eecs.umich.edu        /// response is applicable.
1604870Sstever@eecs.umich.edu        const Command response;
1615314Sstever@gmail.com        /// String representation (for printing)
1628184Ssomayeh@cs.wisc.edu        const std::string str;
16311600Sandreas.hansson@arm.com    };
1644022Sstever@eecs.umich.edu
1654022Sstever@eecs.umich.edu    /// Array to map Command enum to associated info.
1664022Sstever@eecs.umich.edu    static const CommandInfo commandInfo[];
1675735Snate@binkert.org
1685735Snate@binkert.org  private:
1695735Snate@binkert.org
1705735Snate@binkert.org    Command cmd;
1715735Snate@binkert.org
1725735Snate@binkert.org    bool
1735735Snate@binkert.org    testCmdAttrib(MemCmd::Attribute attrib) const
1744022Sstever@eecs.umich.edu    {
1755735Snate@binkert.org        return commandInfo[cmd].attributes[attrib] != 0;
1765735Snate@binkert.org    }
1774022Sstever@eecs.umich.edu
1785735Snate@binkert.org  public:
1794022Sstever@eecs.umich.edu
1804022Sstever@eecs.umich.edu    bool isRead() const            { return testCmdAttrib(IsRead); }
1814022Sstever@eecs.umich.edu    bool isWrite() const           { return testCmdAttrib(IsWrite); }
1825735Snate@binkert.org    bool isUpgrade() const         { return testCmdAttrib(IsUpgrade); }
1834022Sstever@eecs.umich.edu    bool isRequest() const         { return testCmdAttrib(IsRequest); }
1844022Sstever@eecs.umich.edu    bool isResponse() const        { return testCmdAttrib(IsResponse); }
1854022Sstever@eecs.umich.edu    bool needsExclusive() const    { return testCmdAttrib(NeedsExclusive); }
1864022Sstever@eecs.umich.edu    bool needsResponse() const     { return testCmdAttrib(NeedsResponse); }
1874022Sstever@eecs.umich.edu    bool isInvalidate() const      { return testCmdAttrib(IsInvalidate); }
1884022Sstever@eecs.umich.edu    bool isWriteInvalidate() const { return testCmdAttrib(IsWrite) &&
1895735Snate@binkert.org                                            testCmdAttrib(IsInvalidate); }
1905735Snate@binkert.org
1915735Snate@binkert.org    /**
1924022Sstever@eecs.umich.edu     * Check if this particular packet type carries payload data. Note
1934022Sstever@eecs.umich.edu     * that this does not reflect if the data pointer of the packet is
1944022Sstever@eecs.umich.edu     * valid or not.
1954022Sstever@eecs.umich.edu     */
1964022Sstever@eecs.umich.edu    bool hasData() const        { return testCmdAttrib(HasData); }
19710583SCurtis.Dunham@arm.com    bool isLLSC() const         { return testCmdAttrib(IsLlsc); }
19810583SCurtis.Dunham@arm.com    bool isSWPrefetch() const   { return testCmdAttrib(IsSWPrefetch); }
19910583SCurtis.Dunham@arm.com    bool isHWPrefetch() const   { return testCmdAttrib(IsHWPrefetch); }
20010583SCurtis.Dunham@arm.com    bool isPrefetch() const     { return testCmdAttrib(IsSWPrefetch) ||
20110583SCurtis.Dunham@arm.com                                         testCmdAttrib(IsHWPrefetch); }
20211284Sandreas.hansson@arm.com    bool isError() const        { return testCmdAttrib(IsError); }
20310583SCurtis.Dunham@arm.com    bool isPrint() const        { return testCmdAttrib(IsPrint); }
20410583SCurtis.Dunham@arm.com    bool isFlush() const        { return testCmdAttrib(IsFlush); }
20511199Sandreas.hansson@arm.com
20612347Snikos.nikoleris@arm.com    const Command
20711600Sandreas.hansson@arm.com    responseCommand() const
20811199Sandreas.hansson@arm.com    {
20911199Sandreas.hansson@arm.com        return commandInfo[cmd].response;
21011199Sandreas.hansson@arm.com    }
21111199Sandreas.hansson@arm.com
21211199Sandreas.hansson@arm.com    /// Return the string to a cmd given by idx.
21311199Sandreas.hansson@arm.com    const std::string &toString() const { return commandInfo[cmd].str; }
21410570Sandreas.hansson@arm.com    int toInt() const { return (int)cmd; }
21510570Sandreas.hansson@arm.com
21610570Sandreas.hansson@arm.com    MemCmd(Command _cmd) : cmd(_cmd) { }
21710570Sandreas.hansson@arm.com    MemCmd(int _cmd) : cmd((Command)_cmd) { }
21810570Sandreas.hansson@arm.com    MemCmd() : cmd(InvalidCmd) { }
21910570Sandreas.hansson@arm.com
2204022Sstever@eecs.umich.edu    bool operator==(MemCmd c2) const { return (cmd == c2.cmd); }
2216102Sgblack@eecs.umich.edu    bool operator!=(MemCmd c2) const { return (cmd != c2.cmd); }
22210343SCurtis.Dunham@arm.com};
22310343SCurtis.Dunham@arm.com
22410343SCurtis.Dunham@arm.com/**
22510343SCurtis.Dunham@arm.com * A Packet is used to encapsulate a transfer between two objects in
2264870Sstever@eecs.umich.edu * the memory system (e.g., the L1 and L2 cache).  (In contrast, a
2275314Sstever@gmail.com * single Request travels all the way from the requester to the
2288184Ssomayeh@cs.wisc.edu * ultimate destination and back, possibly being conveyed by several
2294022Sstever@eecs.umich.edu * different Packets along the way.)
23011294Sandreas.hansson@arm.com */
2315735Snate@binkert.orgclass Packet : public Printable
2325735Snate@binkert.org{
2334022Sstever@eecs.umich.edu  public:
2344022Sstever@eecs.umich.edu    typedef uint32_t FlagsType;
2354022Sstever@eecs.umich.edu    typedef ::Flags<FlagsType> Flags;
2365735Snate@binkert.org
2375735Snate@binkert.org  private:
2384022Sstever@eecs.umich.edu    static const FlagsType PUBLIC_FLAGS           = 0x00000000;
2394022Sstever@eecs.umich.edu    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;
2434022Sstever@eecs.umich.edu    // Special control flags
2445735Snate@binkert.org    /// Special timing-mode atomic snoop for multi-level coherence.
2455735Snate@binkert.org    static const FlagsType EXPRESS_SNOOP          = 0x00000002;
2464022Sstever@eecs.umich.edu    /// Does supplier have exclusive copy?
2474022Sstever@eecs.umich.edu    /// Useful for multi-level coherence.
2482381SN/A    static const FlagsType SUPPLY_EXCLUSIVE       = 0x00000004;
2492662Sstever@eecs.umich.edu    // Snoop response flags
2502662Sstever@eecs.umich.edu    static const FlagsType MEM_INHIBIT            = 0x00000008;
2512662Sstever@eecs.umich.edu    /// Are the 'addr' and 'size' fields valid?
2522662Sstever@eecs.umich.edu    static const FlagsType VALID_ADDR             = 0x00000100;
2532662Sstever@eecs.umich.edu    static const FlagsType VALID_SIZE             = 0x00000200;
2542381SN/A    /// Is the data pointer set to a value that shouldn't be freed
2559044SAli.Saidi@ARM.com    /// when the packet is destroyed?
2562381SN/A    static const FlagsType STATIC_DATA            = 0x00001000;
2572813Srdreslin@umich.edu    /// The data pointer points to a value that should be freed when
2585735Snate@binkert.org    /// the packet is destroyed. The pointer is assumed to be pointing
2595735Snate@binkert.org    /// to an array, and delete [] is consequently called
2604022Sstever@eecs.umich.edu    static const FlagsType DYNAMIC_DATA           = 0x00002000;
2615735Snate@binkert.org    /// suppress the error if this packet encounters a functional
2625735Snate@binkert.org    /// access failure.
26310938Sandreas.hansson@arm.com    static const FlagsType SUPPRESS_FUNC_ERROR    = 0x00008000;
26410938Sandreas.hansson@arm.com    // Signal prefetch squash through express snoop flag
26512349Snikos.nikoleris@arm.com    static const FlagsType PREFETCH_SNOOP_SQUASH  = 0x00010000;
26610938Sandreas.hansson@arm.com
26711284Sandreas.hansson@arm.com    Flags flags;
26811284Sandreas.hansson@arm.com
26911284Sandreas.hansson@arm.com  public:
27011284Sandreas.hansson@arm.com    typedef MemCmd::Command Command;
27110938Sandreas.hansson@arm.com
27210938Sandreas.hansson@arm.com    /// The command field of the packet.
27310938Sandreas.hansson@arm.com    MemCmd cmd;
27411284Sandreas.hansson@arm.com
27511284Sandreas.hansson@arm.com    /// A pointer to the original request.
27611284Sandreas.hansson@arm.com    const RequestPtr req;
27711284Sandreas.hansson@arm.com
27811284Sandreas.hansson@arm.com  private:
27911284Sandreas.hansson@arm.com   /**
28011284Sandreas.hansson@arm.com    * A pointer to the data being transfered.  It can be differnt
28111284Sandreas.hansson@arm.com    * sizes at each level of the heirarchy so it belongs in the
28211284Sandreas.hansson@arm.com    * packet, not request. This may or may not be populated when a
28310938Sandreas.hansson@arm.com    * responder recieves the packet. If not populated it memory should
28412346Snikos.nikoleris@arm.com    * be allocated.
28512346Snikos.nikoleris@arm.com    */
28612346Snikos.nikoleris@arm.com    PacketDataPtr data;
28712346Snikos.nikoleris@arm.com
28812349Snikos.nikoleris@arm.com    /// The address of the request.  This address could be virtual or
28912349Snikos.nikoleris@arm.com    /// physical, depending on the system configuration.
29012349Snikos.nikoleris@arm.com    Addr addr;
29112349Snikos.nikoleris@arm.com
29211057Sandreas.hansson@arm.com    /// True if the request targets the secure memory space.
29311057Sandreas.hansson@arm.com    bool _isSecure;
29411057Sandreas.hansson@arm.com
29511057Sandreas.hansson@arm.com    /// The size of the request or transfer.
29610938Sandreas.hansson@arm.com    unsigned size;
29710938Sandreas.hansson@arm.com
29810938Sandreas.hansson@arm.com    /**
29910938Sandreas.hansson@arm.com     * The original value of the command field.  Only valid when the
30010938Sandreas.hansson@arm.com     * current command field is an error condition; in that case, the
30110938Sandreas.hansson@arm.com     * previous contents of the command field are copied here.  This
30210938Sandreas.hansson@arm.com     * field is *not* set on non-error responses.
30310938Sandreas.hansson@arm.com     */
30410938Sandreas.hansson@arm.com    MemCmd origCmd;
30510938Sandreas.hansson@arm.com
30610938Sandreas.hansson@arm.com    /**
30710938Sandreas.hansson@arm.com     * These values specify the range of bytes found that satisfy a
30810938Sandreas.hansson@arm.com     * functional read.
30910938Sandreas.hansson@arm.com     */
31010938Sandreas.hansson@arm.com    uint16_t bytesValidStart;
31110938Sandreas.hansson@arm.com    uint16_t bytesValidEnd;
3125735Snate@binkert.org
3135735Snate@binkert.org  public:
3145735Snate@binkert.org
3155735Snate@binkert.org    /**
3164022Sstever@eecs.umich.edu     * The extra delay from seeing the packet until the header is
3174022Sstever@eecs.umich.edu     * transmitted. This delay is used to communicate the crossbar
3185735Snate@binkert.org     * forwarding latency to the neighbouring object (e.g. a cache)
3194870Sstever@eecs.umich.edu     * that actually makes the packet wait. As the delay is relative,
3204870Sstever@eecs.umich.edu     * a 32-bit unsigned should be sufficient.
32112351Snikos.nikoleris@arm.com     */
32212351Snikos.nikoleris@arm.com    uint32_t headerDelay;
3235735Snate@binkert.org
32412749Sgiacomo.travaglini@arm.com    /**
3254870Sstever@eecs.umich.edu     * The extra pipelining delay from seeing the packet until the end of
3262566SN/A     * payload is transmitted by the component that provided it (if
3275735Snate@binkert.org     * any). This includes the header delay. Similar to the header
32812633Sodanrc@yahoo.com.br     * delay, this is used to make up for the fact that the
32912633Sodanrc@yahoo.com.br     * crossbar does not make the packet wait. As the delay is
3305735Snate@binkert.org     * relative, a 32-bit unsigned should be sufficient.
33112633Sodanrc@yahoo.com.br     */
3325735Snate@binkert.org    uint32_t payloadDelay;
3332566SN/A
3342566SN/A    /**
3352566SN/A     * A virtual base opaque structure used to hold state associated
3365735Snate@binkert.org     * with the packet (e.g., an MSHR), specific to a MemObject that
3375735Snate@binkert.org     * sees the packet. A pointer to this state is returned in the
3382381SN/A     * packet's response so that the MemObject in question can quickly
3392381SN/A     * look up the state needed to process it. A specific subclass
34010028SGiacomo.Gabrielli@arm.com     * would be derived from this to carry state specific to a
34110028SGiacomo.Gabrielli@arm.com     * particular sending device.
34210028SGiacomo.Gabrielli@arm.com     *
3435735Snate@binkert.org     * As multiple MemObjects may add their SenderState throughout the
3446227Snate@binkert.org     * memory system, the SenderStates create a stack, where a
3452381SN/A     * MemObject can add a new Senderstate, as long as the
3465735Snate@binkert.org     * predecessing SenderState is restored when the response comes
34710723Sandreas.hansson@arm.com     * back. For this reason, the predecessor should always be
3488668Sgeoffrey.blake@arm.com     * populated with the current SenderState of a packet before
34910723Sandreas.hansson@arm.com     * modifying the senderState field in the request packet.
3508668Sgeoffrey.blake@arm.com     */
35112966SMatteo.Andreozzi@arm.com    struct SenderState
35212966SMatteo.Andreozzi@arm.com    {
35312966SMatteo.Andreozzi@arm.com        SenderState* predecessor;
3542641Sstever@eecs.umich.edu        SenderState() : predecessor(NULL) {}
3552811Srdreslin@umich.edu        virtual ~SenderState() {}
3569547Sandreas.hansson@arm.com    };
35710694SMarco.Balboni@ARM.com
35810405Sandreas.hansson@arm.com    /**
35910405Sandreas.hansson@arm.com     * Object used to maintain state of a PrintReq.  The senderState
36010405Sandreas.hansson@arm.com     * field of a PrintReq should always be of this type.
36110405Sandreas.hansson@arm.com     */
3629547Sandreas.hansson@arm.com    class PrintReqState : public SenderState
36310694SMarco.Balboni@ARM.com    {
3643218Sgblack@eecs.umich.edu      private:
3659547Sandreas.hansson@arm.com        /**
36611127Sandreas.hansson@arm.com         * An entry in the label stack.
36711127Sandreas.hansson@arm.com         */
36811127Sandreas.hansson@arm.com        struct LabelStackEntry
36911127Sandreas.hansson@arm.com        {
37011127Sandreas.hansson@arm.com            const std::string label;
37111127Sandreas.hansson@arm.com            std::string *prefix;
37211127Sandreas.hansson@arm.com            bool labelPrinted;
37311127Sandreas.hansson@arm.com            LabelStackEntry(const std::string &_label, std::string *_prefix);
37410694SMarco.Balboni@ARM.com        };
37510694SMarco.Balboni@ARM.com
37610694SMarco.Balboni@ARM.com        typedef std::list<LabelStackEntry> LabelStack;
37710694SMarco.Balboni@ARM.com        LabelStack labelStack;
37810405Sandreas.hansson@arm.com
37910405Sandreas.hansson@arm.com        std::string *curPrefixPtr;
3809547Sandreas.hansson@arm.com
38110694SMarco.Balboni@ARM.com      public:
3823218Sgblack@eecs.umich.edu        std::ostream &os;
3835735Snate@binkert.org        const int verbosity;
3845735Snate@binkert.org
3859542Sandreas.hansson@arm.com        PrintReqState(std::ostream &os, int verbosity = 0);
3869542Sandreas.hansson@arm.com        ~PrintReqState();
3879542Sandreas.hansson@arm.com
3889542Sandreas.hansson@arm.com        /**
3899542Sandreas.hansson@arm.com         * Returns the current line prefix.
3909542Sandreas.hansson@arm.com         */
3919542Sandreas.hansson@arm.com        const std::string &curPrefix() { return *curPrefixPtr; }
3929542Sandreas.hansson@arm.com
3939542Sandreas.hansson@arm.com        /**
3949542Sandreas.hansson@arm.com         * Push a label onto the label stack, and prepend the given
3959542Sandreas.hansson@arm.com         * prefix string onto the current prefix.  Labels will only be
3969542Sandreas.hansson@arm.com         * printed if an object within the label's scope is printed.
3979542Sandreas.hansson@arm.com         */
3989542Sandreas.hansson@arm.com        void pushLabel(const std::string &lbl,
3995735Snate@binkert.org                       const std::string &prefix = "  ");
4005735Snate@binkert.org
4015735Snate@binkert.org        /**
4029542Sandreas.hansson@arm.com         * Pop a label off the label stack.
4039542Sandreas.hansson@arm.com         */
4042641Sstever@eecs.umich.edu        void popLabel();
4052641Sstever@eecs.umich.edu
4062641Sstever@eecs.umich.edu        /**
4075315Sstever@gmail.com         * Print all of the pending unprinted labels on the
4085315Sstever@gmail.com         * stack. Called by printObj(), so normally not called by
4095315Sstever@gmail.com         * users unless bypassing printObj().
4105315Sstever@gmail.com         */
4119044SAli.Saidi@ARM.com        void printLabels();
4125735Snate@binkert.org
4135735Snate@binkert.org        /**
4145735Snate@binkert.org         * Print a Printable object to os, because it matched the
4155735Snate@binkert.org         * address on a PrintReq.
4165735Snate@binkert.org         */
4175735Snate@binkert.org        void printObj(Printable *obj);
4185735Snate@binkert.org    };
4195314Sstever@gmail.com
4205314Sstever@gmail.com    /**
4215314Sstever@gmail.com     * This packet's sender state.  Devices should use dynamic_cast<>
4225735Snate@binkert.org     * to cast to the state appropriate to the sender.  The intent of
4235314Sstever@gmail.com     * this variable is to allow a device to attach extra information
4245314Sstever@gmail.com     * to a request. A response packet must return the sender state
4255314Sstever@gmail.com     * that was attached to the original request (even if a new packet
4265314Sstever@gmail.com     * is created).
4275314Sstever@gmail.com     */
4285314Sstever@gmail.com    SenderState *senderState;
4295314Sstever@gmail.com
4305314Sstever@gmail.com    /**
4315314Sstever@gmail.com     * Push a new sender state to the packet and make the current
4325314Sstever@gmail.com     * sender state the predecessor of the new one. This should be
4335314Sstever@gmail.com     * prefered over direct manipulation of the senderState member
4345314Sstever@gmail.com     * variable.
4355314Sstever@gmail.com     *
4365314Sstever@gmail.com     * @param sender_state SenderState to push at the top of the stack
4375735Snate@binkert.org     */
4385735Snate@binkert.org    void pushSenderState(SenderState *sender_state);
4395735Snate@binkert.org
4405314Sstever@gmail.com    /**
4415315Sstever@gmail.com     * Pop the top of the state stack and return a pointer to it. This
4425735Snate@binkert.org     * assumes the current sender state is not NULL. This should be
4435735Snate@binkert.org     * preferred over direct manipulation of the senderState member
4445315Sstever@gmail.com     * variable.
4455735Snate@binkert.org     *
4465735Snate@binkert.org     * @return The current top of the stack
4475314Sstever@gmail.com     */
4485314Sstever@gmail.com    SenderState *popSenderState();
4495735Snate@binkert.org
4505735Snate@binkert.org    /**
4515735Snate@binkert.org     * Go through the sender state stack and return the first instance
4525735Snate@binkert.org     * that is of type T (as determined by a dynamic_cast). If there
4535314Sstever@gmail.com     * is no sender state of type T, NULL is returned.
4545735Snate@binkert.org     *
4555735Snate@binkert.org     * @return The topmost state of type T
4565735Snate@binkert.org     */
4575315Sstever@gmail.com    template <typename T>
4585735Snate@binkert.org    T * findNextSenderState() const
4595735Snate@binkert.org    {
4605314Sstever@gmail.com        T *t = NULL;
4615735Snate@binkert.org        SenderState* sender_state = senderState;
4625735Snate@binkert.org        while (t == NULL && sender_state != NULL) {
4635735Snate@binkert.org            t = dynamic_cast<T*>(sender_state);
4645735Snate@binkert.org            sender_state = sender_state->predecessor;
4655735Snate@binkert.org        }
4665314Sstever@gmail.com        return t;
4675314Sstever@gmail.com    }
4685314Sstever@gmail.com
4695735Snate@binkert.org    /// Return the string name of the cmd field (for debugging and
4705735Snate@binkert.org    /// tracing).
4715735Snate@binkert.org    const std::string &cmdString() const { return cmd.toString(); }
4725735Snate@binkert.org
4739542Sandreas.hansson@arm.com    /// Return the index of this command.
4745735Snate@binkert.org    inline int cmdToIndex() const { return cmd.toInt(); }
4755735Snate@binkert.org
4765735Snate@binkert.org    bool isRead() const              { return cmd.isRead(); }
4772662Sstever@eecs.umich.edu    bool isWrite() const             { return cmd.isWrite(); }
4782641Sstever@eecs.umich.edu    bool isUpgrade()  const          { return cmd.isUpgrade(); }
4799542Sandreas.hansson@arm.com    bool isRequest() const           { return cmd.isRequest(); }
4809542Sandreas.hansson@arm.com    bool isResponse() const          { return cmd.isResponse(); }
4819542Sandreas.hansson@arm.com    bool needsExclusive() const      { return cmd.needsExclusive(); }
4829542Sandreas.hansson@arm.com    bool needsResponse() const       { return cmd.needsResponse(); }
4839542Sandreas.hansson@arm.com    bool isInvalidate() const        { return cmd.isInvalidate(); }
4849542Sandreas.hansson@arm.com    bool isWriteInvalidate() const   { return cmd.isWriteInvalidate(); }
4859542Sandreas.hansson@arm.com    bool hasData() const             { return cmd.hasData(); }
4869542Sandreas.hansson@arm.com    bool isLLSC() const              { return cmd.isLLSC(); }
4879542Sandreas.hansson@arm.com    bool isError() const             { return cmd.isError(); }
4889542Sandreas.hansson@arm.com    bool isPrint() const             { return cmd.isPrint(); }
4899542Sandreas.hansson@arm.com    bool isFlush() const             { return cmd.isFlush(); }
4909542Sandreas.hansson@arm.com
4919542Sandreas.hansson@arm.com    // Snoop flags
4929542Sandreas.hansson@arm.com    void assertMemInhibit()
4939542Sandreas.hansson@arm.com    {
4949542Sandreas.hansson@arm.com        assert(isRequest());
4959542Sandreas.hansson@arm.com        assert(!flags.isSet(MEM_INHIBIT));
4969542Sandreas.hansson@arm.com        flags.set(MEM_INHIBIT);
4979542Sandreas.hansson@arm.com    }
4989542Sandreas.hansson@arm.com    bool memInhibitAsserted() const { return flags.isSet(MEM_INHIBIT); }
4999543Ssascha.bischoff@arm.com    void assertShared()             { flags.set(SHARED); }
5009543Ssascha.bischoff@arm.com    bool sharedAsserted() const     { return flags.isSet(SHARED); }
5019543Ssascha.bischoff@arm.com
5029543Ssascha.bischoff@arm.com    // Special control flags
5039543Ssascha.bischoff@arm.com    void setExpressSnoop()          { flags.set(EXPRESS_SNOOP); }
5049543Ssascha.bischoff@arm.com    bool isExpressSnoop() const     { return flags.isSet(EXPRESS_SNOOP); }
5059543Ssascha.bischoff@arm.com    void setSupplyExclusive()       { flags.set(SUPPLY_EXCLUSIVE); }
5069543Ssascha.bischoff@arm.com    void clearSupplyExclusive()     { flags.clear(SUPPLY_EXCLUSIVE); }
5079543Ssascha.bischoff@arm.com    bool isSupplyExclusive() const  { return flags.isSet(SUPPLY_EXCLUSIVE); }
5089543Ssascha.bischoff@arm.com    void setSuppressFuncError()     { flags.set(SUPPRESS_FUNC_ERROR); }
5099543Ssascha.bischoff@arm.com    bool suppressFuncError() const  { return flags.isSet(SUPPRESS_FUNC_ERROR); }
5109543Ssascha.bischoff@arm.com    void setPrefetchSquashed()      { flags.set(PREFETCH_SNOOP_SQUASH); }
5119543Ssascha.bischoff@arm.com    bool prefetchSquashed() const   { return flags.isSet(PREFETCH_SNOOP_SQUASH); }
5129543Ssascha.bischoff@arm.com
5139543Ssascha.bischoff@arm.com    // Network error conditions... encapsulate them as methods since
5149543Ssascha.bischoff@arm.com    // their encoding keeps changing (from result field to command
5159543Ssascha.bischoff@arm.com    // field, etc.)
5169543Ssascha.bischoff@arm.com    void
5179543Ssascha.bischoff@arm.com    setBadAddress()
5185735Snate@binkert.org    {
5195735Snate@binkert.org        assert(isResponse());
5204022Sstever@eecs.umich.edu        cmd = MemCmd::BadAddressError;
5212811Srdreslin@umich.edu    }
5225735Snate@binkert.org
5234022Sstever@eecs.umich.edu    bool hadBadAddress() const { return cmd == MemCmd::BadAddressError; }
5242811Srdreslin@umich.edu    void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
52510583SCurtis.Dunham@arm.com
52610583SCurtis.Dunham@arm.com    Addr getAddr() const { assert(flags.isSet(VALID_ADDR)); return addr; }
52710583SCurtis.Dunham@arm.com    /**
52810583SCurtis.Dunham@arm.com     * Update the address of this packet mid-transaction. This is used
52910583SCurtis.Dunham@arm.com     * by the address mapper to change an already set address to a new
53011287Sandreas.hansson@arm.com     * one based on the system configuration. It is intended to remap
53111287Sandreas.hansson@arm.com     * an existing address, so it asserts that the current address is
53211287Sandreas.hansson@arm.com     * valid.
53311287Sandreas.hansson@arm.com     */
53411287Sandreas.hansson@arm.com    void setAddr(Addr _addr) { assert(flags.isSet(VALID_ADDR)); addr = _addr; }
53511287Sandreas.hansson@arm.com
53611287Sandreas.hansson@arm.com    unsigned getSize() const  { assert(flags.isSet(VALID_SIZE)); return size; }
53711287Sandreas.hansson@arm.com    Addr getOffset(int blkSize) const { return getAddr() & (Addr)(blkSize - 1); }
53811287Sandreas.hansson@arm.com
53910583SCurtis.Dunham@arm.com    bool isSecure() const
54010583SCurtis.Dunham@arm.com    {
54111199Sandreas.hansson@arm.com        assert(flags.isSet(VALID_ADDR));
54212347Snikos.nikoleris@arm.com        return _isSecure;
54311600Sandreas.hansson@arm.com    }
54411199Sandreas.hansson@arm.com
54510583SCurtis.Dunham@arm.com    /**
54611286Sandreas.hansson@arm.com     * It has been determined that the SC packet should successfully update
54711286Sandreas.hansson@arm.com     * memory.  Therefore, convert this SC packet to a normal write.
54811286Sandreas.hansson@arm.com     */
54911286Sandreas.hansson@arm.com    void
55011286Sandreas.hansson@arm.com    convertScToWrite()
55110583SCurtis.Dunham@arm.com    {
55210583SCurtis.Dunham@arm.com        assert(isLLSC());
55310583SCurtis.Dunham@arm.com        assert(isWrite());
55410583SCurtis.Dunham@arm.com        cmd = MemCmd::WriteReq;
5552812Srdreslin@umich.edu    }
55613350Snikos.nikoleris@arm.com
55713350Snikos.nikoleris@arm.com    /**
55813350Snikos.nikoleris@arm.com     * When ruby is in use, Ruby will monitor the cache line and thus M5
55913350Snikos.nikoleris@arm.com     * phys memory should treat LL ops as normal reads.
56013350Snikos.nikoleris@arm.com     */
56113350Snikos.nikoleris@arm.com    void
56211284Sandreas.hansson@arm.com    convertLlToRead()
56311284Sandreas.hansson@arm.com    {
56411284Sandreas.hansson@arm.com        assert(isLLSC());
56511284Sandreas.hansson@arm.com        assert(isRead());
56611284Sandreas.hansson@arm.com        cmd = MemCmd::ReadReq;
56711284Sandreas.hansson@arm.com    }
56811284Sandreas.hansson@arm.com
56911284Sandreas.hansson@arm.com    /**
57011284Sandreas.hansson@arm.com     * Constructor.  Note that a Request object must be constructed
57111284Sandreas.hansson@arm.com     * first, but the Requests's physical address and size fields need
57211284Sandreas.hansson@arm.com     * not be valid. The command must be supplied.
57311284Sandreas.hansson@arm.com     */
57411284Sandreas.hansson@arm.com    Packet(const RequestPtr _req, MemCmd _cmd)
57511284Sandreas.hansson@arm.com        :  cmd(_cmd), req(_req), data(nullptr), addr(0), _isSecure(false),
57611284Sandreas.hansson@arm.com           size(0), bytesValidStart(0), bytesValidEnd(0),
57711284Sandreas.hansson@arm.com           headerDelay(0), payloadDelay(0),
57811284Sandreas.hansson@arm.com           senderState(NULL)
57911284Sandreas.hansson@arm.com    {
58011284Sandreas.hansson@arm.com        if (req->hasPaddr()) {
58111284Sandreas.hansson@arm.com            addr = req->getPaddr();
58211284Sandreas.hansson@arm.com            flags.set(VALID_ADDR);
58310567Sandreas.hansson@arm.com            _isSecure = req->isSecure();
58410567Sandreas.hansson@arm.com        }
58511284Sandreas.hansson@arm.com        if (req->hasSize()) {
58611284Sandreas.hansson@arm.com            size = req->getSize();
58710567Sandreas.hansson@arm.com            flags.set(VALID_SIZE);
58811284Sandreas.hansson@arm.com        }
58911284Sandreas.hansson@arm.com    }
59011284Sandreas.hansson@arm.com
59111284Sandreas.hansson@arm.com    /**
59211284Sandreas.hansson@arm.com     * Alternate constructor if you are trying to create a packet with
59311284Sandreas.hansson@arm.com     * a request that is for a whole block, not the address from the
59411284Sandreas.hansson@arm.com     * req.  this allows for overriding the size/addr of the req.
59511284Sandreas.hansson@arm.com     */
59611284Sandreas.hansson@arm.com    Packet(const RequestPtr _req, MemCmd _cmd, int _blkSize)
59711284Sandreas.hansson@arm.com        :  cmd(_cmd), req(_req), data(nullptr), addr(0), _isSecure(false),
59811284Sandreas.hansson@arm.com           bytesValidStart(0), bytesValidEnd(0),
59911284Sandreas.hansson@arm.com           headerDelay(0), payloadDelay(0),
60011284Sandreas.hansson@arm.com           senderState(NULL)
60111284Sandreas.hansson@arm.com    {
60211284Sandreas.hansson@arm.com        if (req->hasPaddr()) {
60311284Sandreas.hansson@arm.com            addr = req->getPaddr() & ~(_blkSize - 1);
60411284Sandreas.hansson@arm.com            flags.set(VALID_ADDR);
60511284Sandreas.hansson@arm.com            _isSecure = req->isSecure();
60611284Sandreas.hansson@arm.com        }
60711284Sandreas.hansson@arm.com        size = _blkSize;
60811284Sandreas.hansson@arm.com        flags.set(VALID_SIZE);
60911284Sandreas.hansson@arm.com    }
61011284Sandreas.hansson@arm.com
61111284Sandreas.hansson@arm.com    /**
61211284Sandreas.hansson@arm.com     * Alternate constructor for copying a packet.  Copy all fields
61311284Sandreas.hansson@arm.com     * *except* if the original packet's data was dynamic, don't copy
61411284Sandreas.hansson@arm.com     * that, as we can't guarantee that the new packet's lifetime is
61511284Sandreas.hansson@arm.com     * less than that of the original packet.  In this case the new
61611284Sandreas.hansson@arm.com     * packet should allocate its own data.
6174870Sstever@eecs.umich.edu     */
61811284Sandreas.hansson@arm.com    Packet(PacketPtr pkt, bool clear_flags, bool alloc_data)
61911284Sandreas.hansson@arm.com        :  cmd(pkt->cmd), req(pkt->req),
62011284Sandreas.hansson@arm.com           data(nullptr),
62111284Sandreas.hansson@arm.com           addr(pkt->addr), _isSecure(pkt->_isSecure), size(pkt->size),
62211284Sandreas.hansson@arm.com           bytesValidStart(pkt->bytesValidStart),
62311284Sandreas.hansson@arm.com           bytesValidEnd(pkt->bytesValidEnd),
62411284Sandreas.hansson@arm.com           headerDelay(pkt->headerDelay),
62511284Sandreas.hansson@arm.com           payloadDelay(pkt->payloadDelay),
62611284Sandreas.hansson@arm.com           senderState(pkt->senderState)
62711284Sandreas.hansson@arm.com    {
62811284Sandreas.hansson@arm.com        if (!clear_flags)
62911284Sandreas.hansson@arm.com            flags.set(pkt->flags & COPY_FLAGS);
63011284Sandreas.hansson@arm.com
63111284Sandreas.hansson@arm.com        flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE));
63211284Sandreas.hansson@arm.com
63311284Sandreas.hansson@arm.com        // should we allocate space for data, or not, the express
63411284Sandreas.hansson@arm.com        // snoops do not need to carry any data as they only serve to
63511284Sandreas.hansson@arm.com        // co-ordinate state changes
63611284Sandreas.hansson@arm.com        if (alloc_data) {
63711284Sandreas.hansson@arm.com            // even if asked to allocate data, if the original packet
63811284Sandreas.hansson@arm.com            // holds static data, then the sender will not be doing
63911284Sandreas.hansson@arm.com            // any memcpy on receiving the response, thus we simply
64011284Sandreas.hansson@arm.com            // carry the pointer forward
64111284Sandreas.hansson@arm.com            if (pkt->flags.isSet(STATIC_DATA)) {
64211284Sandreas.hansson@arm.com                data = pkt->data;
64311284Sandreas.hansson@arm.com                flags.set(STATIC_DATA);
64411284Sandreas.hansson@arm.com            } else {
64511746Snikos.nikoleris@arm.com                allocate();
64611284Sandreas.hansson@arm.com            }
64711284Sandreas.hansson@arm.com        }
64811284Sandreas.hansson@arm.com    }
64911284Sandreas.hansson@arm.com
65011284Sandreas.hansson@arm.com    /**
65112346Snikos.nikoleris@arm.com     * Change the packet type based on request type.
65212346Snikos.nikoleris@arm.com     */
65312346Snikos.nikoleris@arm.com    void
65412346Snikos.nikoleris@arm.com    refineCommand()
65512346Snikos.nikoleris@arm.com    {
65612346Snikos.nikoleris@arm.com        if (cmd == MemCmd::ReadReq) {
65712346Snikos.nikoleris@arm.com            if (req->isLLSC()) {
65812346Snikos.nikoleris@arm.com                cmd = MemCmd::LoadLockedReq;
65912346Snikos.nikoleris@arm.com            } else if (req->isPrefetch()) {
66012346Snikos.nikoleris@arm.com                cmd = MemCmd::SoftPFReq;
66112346Snikos.nikoleris@arm.com            }
66212346Snikos.nikoleris@arm.com        } else if (cmd == MemCmd::WriteReq) {
66312346Snikos.nikoleris@arm.com            if (req->isLLSC()) {
66412349Snikos.nikoleris@arm.com                cmd = MemCmd::StoreCondReq;
66512349Snikos.nikoleris@arm.com            } else if (req->isSwap()) {
66612349Snikos.nikoleris@arm.com                cmd = MemCmd::SwapReq;
66712349Snikos.nikoleris@arm.com            }
66812349Snikos.nikoleris@arm.com        }
66912349Snikos.nikoleris@arm.com    }
67012349Snikos.nikoleris@arm.com
67112349Snikos.nikoleris@arm.com    /**
67212349Snikos.nikoleris@arm.com     * Constructor-like methods that return Packets based on Request objects.
67312349Snikos.nikoleris@arm.com     * Will call refineCommand() to fine-tune the Packet type if it's not a
67412349Snikos.nikoleris@arm.com     * vanilla read or write.
67512349Snikos.nikoleris@arm.com     */
67612349Snikos.nikoleris@arm.com    static PacketPtr
6779951Sstephan.diestelhorst@arm.com    createRead(const RequestPtr req)
6789951Sstephan.diestelhorst@arm.com    {
67910763Sali.jafri@arm.com        PacketPtr pkt = new Packet(req, MemCmd::ReadReq);
68010763Sali.jafri@arm.com        pkt->refineCommand();
68110883Sali.jafri@arm.com        return pkt;
6824895Sstever@eecs.umich.edu    }
68312966SMatteo.Andreozzi@arm.com
68412966SMatteo.Andreozzi@arm.com    static PacketPtr
68512966SMatteo.Andreozzi@arm.com    createWrite(const RequestPtr req)
68612966SMatteo.Andreozzi@arm.com    {
68712966SMatteo.Andreozzi@arm.com        PacketPtr pkt = new Packet(req, MemCmd::WriteReq);
68812966SMatteo.Andreozzi@arm.com        pkt->refineCommand();
68912966SMatteo.Andreozzi@arm.com        return pkt;
69012966SMatteo.Andreozzi@arm.com    }
69112966SMatteo.Andreozzi@arm.com
69212966SMatteo.Andreozzi@arm.com    /**
69312966SMatteo.Andreozzi@arm.com     * clean up packet variables
69412966SMatteo.Andreozzi@arm.com     */
69512966SMatteo.Andreozzi@arm.com    ~Packet()
69612966SMatteo.Andreozzi@arm.com    {
69712966SMatteo.Andreozzi@arm.com        // If this is a request packet for which there's no response,
69812966SMatteo.Andreozzi@arm.com        // delete the request object here, since the requester will
69912966SMatteo.Andreozzi@arm.com        // never get the chance.
70012966SMatteo.Andreozzi@arm.com        if (req && isRequest() && !needsResponse())
70112966SMatteo.Andreozzi@arm.com            delete req;
7024870Sstever@eecs.umich.edu        deleteData();
7034870Sstever@eecs.umich.edu    }
7044870Sstever@eecs.umich.edu
7055735Snate@binkert.org    /**
7065735Snate@binkert.org     * Take a request packet and modify it in place to be suitable for
7075735Snate@binkert.org     * returning as a response to that request.
7085735Snate@binkert.org     */
7095735Snate@binkert.org    void
7105735Snate@binkert.org    makeResponse()
7115735Snate@binkert.org    {
7124986Ssaidi@eecs.umich.edu        assert(needsResponse());
7132814Srdreslin@umich.edu        assert(isRequest());
71411057Sandreas.hansson@arm.com        origCmd = cmd;
7159259SAli.Saidi@ARM.com        cmd = cmd.responseCommand();
7169259SAli.Saidi@ARM.com
7179259SAli.Saidi@ARM.com        // responses are never express, even if the snoop that
7189259SAli.Saidi@ARM.com        // triggered them was
7199259SAli.Saidi@ARM.com        flags.clear(EXPRESS_SNOOP);
7209259SAli.Saidi@ARM.com    }
7219259SAli.Saidi@ARM.com
72211057Sandreas.hansson@arm.com    void
7239259SAli.Saidi@ARM.com    makeAtomicResponse()
72411057Sandreas.hansson@arm.com    {
72510938Sandreas.hansson@arm.com        makeResponse();
72610938Sandreas.hansson@arm.com    }
72710938Sandreas.hansson@arm.com
72810938Sandreas.hansson@arm.com    void
72910938Sandreas.hansson@arm.com    makeTimingResponse()
73010938Sandreas.hansson@arm.com    {
73110938Sandreas.hansson@arm.com        makeResponse();
73210938Sandreas.hansson@arm.com    }
73310938Sandreas.hansson@arm.com
73410938Sandreas.hansson@arm.com    void
7352549SN/A    setFunctionalResponseStatus(bool success)
73611057Sandreas.hansson@arm.com    {
73711057Sandreas.hansson@arm.com        if (!success) {
73811057Sandreas.hansson@arm.com            if (isWrite()) {
73911057Sandreas.hansson@arm.com                cmd = MemCmd::FunctionalWriteError;
74011057Sandreas.hansson@arm.com            } else {
74110028SGiacomo.Gabrielli@arm.com                cmd = MemCmd::FunctionalReadError;
7425735Snate@binkert.org            }
74311306Santhony.gutierrez@amd.com        }
74411306Santhony.gutierrez@amd.com    }
74511306Santhony.gutierrez@amd.com
74611306Santhony.gutierrez@amd.com    void
74711306Santhony.gutierrez@amd.com    setSize(unsigned size)
74811306Santhony.gutierrez@amd.com    {
7497550SBrad.Beckmann@amd.com        assert(!flags.isSet(VALID_SIZE));
75010938Sandreas.hansson@arm.com
7517550SBrad.Beckmann@amd.com        this->size = size;
7527550SBrad.Beckmann@amd.com        flags.set(VALID_SIZE);
7537550SBrad.Beckmann@amd.com    }
7547550SBrad.Beckmann@amd.com
7557550SBrad.Beckmann@amd.com
7567550SBrad.Beckmann@amd.com    /**
7577550SBrad.Beckmann@amd.com     * Set the data pointer to the following value that should not be
7587550SBrad.Beckmann@amd.com     * freed. Static data allows us to do a single memcpy even if
7597550SBrad.Beckmann@amd.com     * multiple packets are required to get from source to destination
7607550SBrad.Beckmann@amd.com     * and back. In essence the pointer is set calling dataStatic on
76110938Sandreas.hansson@arm.com     * the original packet, and whenever this packet is copied and
76210938Sandreas.hansson@arm.com     * forwarded the same pointer is passed on. When a packet
7637550SBrad.Beckmann@amd.com     * eventually reaches the destination holding the data, it is
7647550SBrad.Beckmann@amd.com     * copied once into the location originally set. On the way back
7657550SBrad.Beckmann@amd.com     * to the source, no copies are necessary.
7667550SBrad.Beckmann@amd.com     */
7677550SBrad.Beckmann@amd.com    template <typename T>
7687550SBrad.Beckmann@amd.com    void
7697550SBrad.Beckmann@amd.com    dataStatic(T *p)
7707550SBrad.Beckmann@amd.com    {
7717550SBrad.Beckmann@amd.com        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
7727550SBrad.Beckmann@amd.com        data = (PacketDataPtr)p;
77310938Sandreas.hansson@arm.com        flags.set(STATIC_DATA);
77411057Sandreas.hansson@arm.com    }
77511057Sandreas.hansson@arm.com
7765735Snate@binkert.org    /**
77712749Sgiacomo.travaglini@arm.com     * Set the data pointer to the following value that should not be
77812966SMatteo.Andreozzi@arm.com     * freed. This version of the function allows the pointer passed
77912966SMatteo.Andreozzi@arm.com     * to us to be const. To avoid issues down the line we cast the
78012966SMatteo.Andreozzi@arm.com     * constness away, the alternative would be to keep both a const
78112351Snikos.nikoleris@arm.com     * and non-const data pointer and cleverly choose between
78211057Sandreas.hansson@arm.com     * them. Note that this is only allowed for static data.
78311057Sandreas.hansson@arm.com     */
78411057Sandreas.hansson@arm.com    template <typename T>
78511057Sandreas.hansson@arm.com    void
78611057Sandreas.hansson@arm.com    dataStaticConst(const T *p)
78711057Sandreas.hansson@arm.com    {
78811057Sandreas.hansson@arm.com        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
78911057Sandreas.hansson@arm.com        data = const_cast<PacketDataPtr>(p);
79011057Sandreas.hansson@arm.com        flags.set(STATIC_DATA);
79111057Sandreas.hansson@arm.com    }
79211057Sandreas.hansson@arm.com
79311057Sandreas.hansson@arm.com    /**
79411057Sandreas.hansson@arm.com     * Set the data pointer to a value that should have delete []
79511057Sandreas.hansson@arm.com     * called on it. Dynamic data is local to this packet, and as the
79611057Sandreas.hansson@arm.com     * packet travels from source to destination, forwarded packets
79711057Sandreas.hansson@arm.com     * will allocate their own data. When a packet reaches the final
79811057Sandreas.hansson@arm.com     * destination it will populate the dynamic data of that specific
79912749Sgiacomo.travaglini@arm.com     * packet, and on the way back towards the source, memcpy will be
80012749Sgiacomo.travaglini@arm.com     * invoked in every step where a new packet was created e.g. in
80112966SMatteo.Andreozzi@arm.com     * the caches. Ultimately when the response reaches the source a
80212966SMatteo.Andreozzi@arm.com     * final memcpy is needed to extract the data from the packet
80312749Sgiacomo.travaglini@arm.com     * before it is deallocated.
80411057Sandreas.hansson@arm.com     */
80511057Sandreas.hansson@arm.com    template <typename T>
80611057Sandreas.hansson@arm.com    void
80711057Sandreas.hansson@arm.com    dataDynamic(T *p)
80811057Sandreas.hansson@arm.com    {
80911057Sandreas.hansson@arm.com        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
81011057Sandreas.hansson@arm.com        data = (PacketDataPtr)p;
81111057Sandreas.hansson@arm.com        flags.set(DYNAMIC_DATA);
81211057Sandreas.hansson@arm.com    }
8134626Sstever@eecs.umich.edu
8145735Snate@binkert.org    /**
8155735Snate@binkert.org     * get a pointer to the data ptr.
8164887Sstever@eecs.umich.edu     */
8174887Sstever@eecs.umich.edu    template <typename T>
8184887Sstever@eecs.umich.edu    T*
8195735Snate@binkert.org    getPtr()
8205735Snate@binkert.org    {
82110896Snilay@cs.wisc.edu        assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
82212351Snikos.nikoleris@arm.com        return (T*)data;
82310571Sandreas.hansson@arm.com    }
82410028SGiacomo.Gabrielli@arm.com
82510723Sandreas.hansson@arm.com    template <typename T>
82612966SMatteo.Andreozzi@arm.com    const T*
82710694SMarco.Balboni@ARM.com    getConstPtr() const
82811127Sandreas.hansson@arm.com    {
82910694SMarco.Balboni@ARM.com        assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
8309546Sandreas.hansson@arm.com        return (const T*)data;
8314626Sstever@eecs.umich.edu    }
83210571Sandreas.hansson@arm.com
8335735Snate@binkert.org    /**
8345735Snate@binkert.org     * return the value of what is pointed to in the packet.
83511057Sandreas.hansson@arm.com     */
83611057Sandreas.hansson@arm.com    template <typename T>
83710571Sandreas.hansson@arm.com    T get() const;
83810571Sandreas.hansson@arm.com
83910571Sandreas.hansson@arm.com    /**
84010571Sandreas.hansson@arm.com     * set the value in the data pointer to v.
84110571Sandreas.hansson@arm.com     */
84210571Sandreas.hansson@arm.com    template <typename T>
84310571Sandreas.hansson@arm.com    void set(T v);
84410571Sandreas.hansson@arm.com
84510571Sandreas.hansson@arm.com    /**
84610571Sandreas.hansson@arm.com     * Copy data into the packet from the provided pointer.
84710571Sandreas.hansson@arm.com     */
84810571Sandreas.hansson@arm.com    void
84910571Sandreas.hansson@arm.com    setData(const uint8_t *p)
85010571Sandreas.hansson@arm.com    {
85110571Sandreas.hansson@arm.com        // we should never be copying data onto itself, which means we
85210342SCurtis.Dunham@arm.com        // must idenfity packets with static data, as they carry the
8538668Sgeoffrey.blake@arm.com        // same pointer from source to destination and back
85410342SCurtis.Dunham@arm.com        assert(p != getPtr<uint8_t>() || flags.isSet(STATIC_DATA));
85510739Ssteve.reinhardt@amd.com
85610342SCurtis.Dunham@arm.com        if (p != getPtr<uint8_t>())
85710739Ssteve.reinhardt@amd.com            // for packet with allocated dynamic data, we copy data from
85812749Sgiacomo.travaglini@arm.com            // one to the other, e.g. a forwarded response to a response
85910342SCurtis.Dunham@arm.com            std::memcpy(getPtr<uint8_t>(), p, getSize());
86010739Ssteve.reinhardt@amd.com    }
86110739Ssteve.reinhardt@amd.com
86210739Ssteve.reinhardt@amd.com    /**
86310739Ssteve.reinhardt@amd.com     * Copy data into the packet from the provided block pointer,
86410739Ssteve.reinhardt@amd.com     * which is aligned to the given block size.
86510739Ssteve.reinhardt@amd.com     */
86610739Ssteve.reinhardt@amd.com    void
86710739Ssteve.reinhardt@amd.com    setDataFromBlock(const uint8_t *blk_data, int blkSize)
86810739Ssteve.reinhardt@amd.com    {
86910739Ssteve.reinhardt@amd.com        setData(blk_data + getOffset(blkSize));
87010739Ssteve.reinhardt@amd.com    }
87110739Ssteve.reinhardt@amd.com
87212749Sgiacomo.travaglini@arm.com    /**
87310739Ssteve.reinhardt@amd.com     * Copy data from the packet to the provided block pointer, which
87410739Ssteve.reinhardt@amd.com     * is aligned to the given block size.
87510739Ssteve.reinhardt@amd.com     */
87612766Sqtt2@cornell.edu    void
87710739Ssteve.reinhardt@amd.com    writeData(uint8_t *p) const
87812347Snikos.nikoleris@arm.com    {
87912347Snikos.nikoleris@arm.com        std::memcpy(p, getConstPtr<uint8_t>(), getSize());
88012347Snikos.nikoleris@arm.com    }
88112347Snikos.nikoleris@arm.com
88212347Snikos.nikoleris@arm.com    /**
88312347Snikos.nikoleris@arm.com     * Copy data from the packet to the memory at the provided pointer.
88410739Ssteve.reinhardt@amd.com     */
88510342SCurtis.Dunham@arm.com    void
88610342SCurtis.Dunham@arm.com    writeDataToBlock(uint8_t *blk_data, int blkSize) const
88710342SCurtis.Dunham@arm.com    {
88810342SCurtis.Dunham@arm.com        writeData(blk_data + getOffset(blkSize));
88910739Ssteve.reinhardt@amd.com    }
89010342SCurtis.Dunham@arm.com
89110342SCurtis.Dunham@arm.com    /**
89212749Sgiacomo.travaglini@arm.com     * delete the data pointed to in the data pointer. Ok to call to
89310342SCurtis.Dunham@arm.com     * matter how data was allocted.
89410739Ssteve.reinhardt@amd.com     */
89510342SCurtis.Dunham@arm.com    void
89610342SCurtis.Dunham@arm.com    deleteData()
89710342SCurtis.Dunham@arm.com    {
89812749Sgiacomo.travaglini@arm.com        if (flags.isSet(DYNAMIC_DATA))
89910342SCurtis.Dunham@arm.com            delete [] data;
90010739Ssteve.reinhardt@amd.com
9012641Sstever@eecs.umich.edu        flags.clear(STATIC_DATA|DYNAMIC_DATA);
9022549SN/A        data = NULL;
9035735Snate@binkert.org    }
9045735Snate@binkert.org
9055735Snate@binkert.org    /** Allocate memory for the packet. */
9062566SN/A    void
9075387Sstever@gmail.com    allocate()
9085735Snate@binkert.org    {
9095387Sstever@gmail.com        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
9102566SN/A        flags.set(DYNAMIC_DATA);
9115735Snate@binkert.org        data = new uint8_t[getSize()];
9124626Sstever@eecs.umich.edu    }
91310660Sandreas.hansson@arm.com
9144626Sstever@eecs.umich.edu    /**
9155735Snate@binkert.org     * Check a functional request against a memory value stored in
9165735Snate@binkert.org     * another packet (i.e. an in-transit request or
9174626Sstever@eecs.umich.edu     * response). Returns true if the current packet is a read, and
9182662Sstever@eecs.umich.edu     * the other packet provides the data, which is then copied to the
9192855Srdreslin@umich.edu     * current packet. If the current packet is a write, and the other
9204022Sstever@eecs.umich.edu     * packet intersects this one, then we update the data
9215745Snate@binkert.org     * accordingly.
9227464Ssteve.reinhardt@amd.com     */
9237464Ssteve.reinhardt@amd.com    bool
9247464Ssteve.reinhardt@amd.com    checkFunctional(PacketPtr other)
9252641Sstever@eecs.umich.edu    {
9262641Sstever@eecs.umich.edu        // all packets that are carrying a payload should have a valid
9275735Snate@binkert.org        // data pointer
9285735Snate@binkert.org        return checkFunctional(other, other->getAddr(), other->isSecure(),
9294870Sstever@eecs.umich.edu                               other->getSize(),
9304870Sstever@eecs.umich.edu                               other->hasData() ?
9314870Sstever@eecs.umich.edu                               other->getPtr<uint8_t>() : NULL);
9324870Sstever@eecs.umich.edu    }
9335735Snate@binkert.org
9345735Snate@binkert.org    /**
9353348Sbinkertn@umich.edu     * Check a functional request against a memory value represented
9364870Sstever@eecs.umich.edu     * by a base/size pair and an associated data array. If the
9373135Srdreslin@umich.edu     * current packet is a read, it may be satisfied by the memory
9383135Srdreslin@umich.edu     * value. If the current packet is a write, it may update the
9398436SBrad.Beckmann@amd.com     * memory value.
9408436SBrad.Beckmann@amd.com     */
9418436SBrad.Beckmann@amd.com    bool
9428436SBrad.Beckmann@amd.com    checkFunctional(Printable *obj, Addr base, bool is_secure, int size,
9438436SBrad.Beckmann@amd.com                    uint8_t *_data);
9448436SBrad.Beckmann@amd.com
9458436SBrad.Beckmann@amd.com    /**
9468436SBrad.Beckmann@amd.com     * Push label for PrintReq (safe to call unconditionally).
9478436SBrad.Beckmann@amd.com     */
9488436SBrad.Beckmann@amd.com    void
9498436SBrad.Beckmann@amd.com    pushLabel(const std::string &lbl)
9508436SBrad.Beckmann@amd.com    {
95111057Sandreas.hansson@arm.com        if (isPrint())
95211057Sandreas.hansson@arm.com            safe_cast<PrintReqState*>(senderState)->pushLabel(lbl);
95311057Sandreas.hansson@arm.com    }
95411057Sandreas.hansson@arm.com
95511057Sandreas.hansson@arm.com    /**
95611057Sandreas.hansson@arm.com     * Pop label for PrintReq (safe to call unconditionally).
95711057Sandreas.hansson@arm.com     */
95811057Sandreas.hansson@arm.com    void
95911057Sandreas.hansson@arm.com    popLabel()
96011057Sandreas.hansson@arm.com    {
96111013Sandreas.sandberg@arm.com        if (isPrint())
96211013Sandreas.sandberg@arm.com            safe_cast<PrintReqState*>(senderState)->popLabel();
96311013Sandreas.sandberg@arm.com    }
96411013Sandreas.sandberg@arm.com
96511013Sandreas.sandberg@arm.com    void print(std::ostream &o, int verbosity = 0,
96611013Sandreas.sandberg@arm.com               const std::string &prefix = "") const;
9673348Sbinkertn@umich.edu
9683348Sbinkertn@umich.edu    /**
96910571Sandreas.hansson@arm.com     * A no-args wrapper of print(std::ostream...)
97010571Sandreas.hansson@arm.com     * meant to be invoked from DPRINTFs
97110571Sandreas.hansson@arm.com     * avoiding string overheads in fast mode
97210571Sandreas.hansson@arm.com     * @return string with the request's type and start<->end addresses
97310571Sandreas.hansson@arm.com     */
97410571Sandreas.hansson@arm.com    std::string print() const;
97510571Sandreas.hansson@arm.com};
97610571Sandreas.hansson@arm.com
9772566SN/A#endif //__MEM_PACKET_HH
9782566SN/A