packet.hh revision 13367
12381SN/A/*
212652Sandreas.sandberg@arm.com * Copyright (c) 2012-2018 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
4512349Snikos.nikoleris@arm.com *          Nikos Nikoleris
462381SN/A */
472381SN/A
482381SN/A/**
492381SN/A * @file
502662Sstever@eecs.umich.edu * Declaration of the Packet class.
512381SN/A */
522381SN/A
532381SN/A#ifndef __MEM_PACKET_HH__
542381SN/A#define __MEM_PACKET_HH__
552381SN/A
568229Snate@binkert.org#include <bitset>
573348Sbinkertn@umich.edu#include <cassert>
583348Sbinkertn@umich.edu#include <list>
593348Sbinkertn@umich.edu
605735Snate@binkert.org#include "base/cast.hh"
614024Sbinkertn@umich.edu#include "base/compiler.hh"
625735Snate@binkert.org#include "base/flags.hh"
6312334Sgabeblack@google.com#include "base/logging.hh"
645314Sstever@gmail.com#include "base/printable.hh"
656216Snate@binkert.org#include "base/types.hh"
6613347Sgabeblack@google.com#include "config/the_isa.hh"
672392SN/A#include "mem/request.hh"
684167Sbinkertn@umich.edu#include "sim/core.hh"
692394SN/A
708737Skoansin.tan@gmail.comclass Packet;
713349Sbinkertn@umich.edutypedef Packet *PacketPtr;
722394SN/Atypedef uint8_t* PacketDataPtr;
732812Srdreslin@umich.edutypedef std::list<PacketPtr> PacketList;
7412351Snikos.nikoleris@arm.comtypedef uint64_t PacketId;
752812Srdreslin@umich.edu
764022Sstever@eecs.umich.educlass MemCmd
774022Sstever@eecs.umich.edu{
785735Snate@binkert.org    friend class Packet;
795735Snate@binkert.org
804022Sstever@eecs.umich.edu  public:
815735Snate@binkert.org    /**
825735Snate@binkert.org     * List of all commands associated with a packet.
835735Snate@binkert.org     */
844022Sstever@eecs.umich.edu    enum Command
854022Sstever@eecs.umich.edu    {
864022Sstever@eecs.umich.edu        InvalidCmd,
874022Sstever@eecs.umich.edu        ReadReq,
884473Sstever@eecs.umich.edu        ReadResp,
895319Sstever@gmail.com        ReadRespWithInvalidate,
904022Sstever@eecs.umich.edu        WriteReq,
914022Sstever@eecs.umich.edu        WriteResp,
9211199Sandreas.hansson@arm.com        WritebackDirty,
9311199Sandreas.hansson@arm.com        WritebackClean,
9412344Snikos.nikoleris@arm.com        WriteClean,            // writes dirty data below without evicting
9510883Sali.jafri@arm.com        CleanEvict,
964022Sstever@eecs.umich.edu        SoftPFReq,
9713367Syuetsu.kodama@riken.jp        SoftPFExReq,
984022Sstever@eecs.umich.edu        HardPFReq,
994022Sstever@eecs.umich.edu        SoftPFResp,
1004022Sstever@eecs.umich.edu        HardPFResp,
10110886Sandreas.hansson@arm.com        WriteLineReq,
1024022Sstever@eecs.umich.edu        UpgradeReq,
1037465Ssteve.reinhardt@amd.com        SCUpgradeReq,           // Special "weak" upgrade for StoreCond
1044628Sstever@eecs.umich.edu        UpgradeResp,
1057465Ssteve.reinhardt@amd.com        SCUpgradeFailReq,       // Failed SCUpgradeReq in MSHR (never sent)
1067465Ssteve.reinhardt@amd.com        UpgradeFailResp,        // Valid for SCUpgradeReq only
1074022Sstever@eecs.umich.edu        ReadExReq,
1084022Sstever@eecs.umich.edu        ReadExResp,
10910885Sandreas.hansson@arm.com        ReadCleanReq,
11010885Sandreas.hansson@arm.com        ReadSharedReq,
1114626Sstever@eecs.umich.edu        LoadLockedReq,
1124626Sstever@eecs.umich.edu        StoreCondReq,
1137669Ssteve.reinhardt@amd.com        StoreCondFailReq,       // Failed StoreCondReq in MSHR (never sent)
1144626Sstever@eecs.umich.edu        StoreCondResp,
1154040Ssaidi@eecs.umich.edu        SwapReq,
1164040Ssaidi@eecs.umich.edu        SwapResp,
1175650Sgblack@eecs.umich.edu        MessageReq,
1185650Sgblack@eecs.umich.edu        MessageResp,
11911256Santhony.gutierrez@amd.com        MemFenceReq,
12011256Santhony.gutierrez@amd.com        MemFenceResp,
12112347Snikos.nikoleris@arm.com        CleanSharedReq,
12212347Snikos.nikoleris@arm.com        CleanSharedResp,
12312347Snikos.nikoleris@arm.com        CleanInvalidReq,
12412347Snikos.nikoleris@arm.com        CleanInvalidResp,
1254870Sstever@eecs.umich.edu        // Error responses
1264870Sstever@eecs.umich.edu        // @TODO these should be classified as responses rather than
1274870Sstever@eecs.umich.edu        // requests; coding them as requests initially for backwards
1284870Sstever@eecs.umich.edu        // compatibility
1294870Sstever@eecs.umich.edu        InvalidDestError,  // packet dest field invalid
1304870Sstever@eecs.umich.edu        BadAddressError,   // memory address invalid
1318436SBrad.Beckmann@amd.com        FunctionalReadError, // unable to fulfill functional read
1328436SBrad.Beckmann@amd.com        FunctionalWriteError, // unable to fulfill functional write
1335314Sstever@gmail.com        // Fake simulator-only commands
1345314Sstever@gmail.com        PrintReq,       // Print state matching address
1358184Ssomayeh@cs.wisc.edu        FlushReq,      //request for a cache flush
13610886Sandreas.hansson@arm.com        InvalidateReq,   // request for address to be invalidated
13710886Sandreas.hansson@arm.com        InvalidateResp,
1384022Sstever@eecs.umich.edu        NUM_MEM_CMDS
1394022Sstever@eecs.umich.edu    };
1404022Sstever@eecs.umich.edu
1414022Sstever@eecs.umich.edu  private:
1425735Snate@binkert.org    /**
1435735Snate@binkert.org     * List of command attributes.
1445735Snate@binkert.org     */
1454022Sstever@eecs.umich.edu    enum Attribute
1464022Sstever@eecs.umich.edu    {
1474626Sstever@eecs.umich.edu        IsRead,         //!< Data flows from responder to requester
1484626Sstever@eecs.umich.edu        IsWrite,        //!< Data flows from requester to responder
1497465Ssteve.reinhardt@amd.com        IsUpgrade,
1504022Sstever@eecs.umich.edu        IsInvalidate,
15112347Snikos.nikoleris@arm.com        IsClean,        //!< Cleans any existing dirty blocks
15211284Sandreas.hansson@arm.com        NeedsWritable,  //!< Requires writable copy to complete in-cache
1534626Sstever@eecs.umich.edu        IsRequest,      //!< Issued by requester
1544626Sstever@eecs.umich.edu        IsResponse,     //!< Issue by responder
1554626Sstever@eecs.umich.edu        NeedsResponse,  //!< Requester needs response from target
15611199Sandreas.hansson@arm.com        IsEviction,
1574022Sstever@eecs.umich.edu        IsSWPrefetch,
1584022Sstever@eecs.umich.edu        IsHWPrefetch,
1596076Sgblack@eecs.umich.edu        IsLlsc,         //!< Alpha/MIPS LL or SC access
1604626Sstever@eecs.umich.edu        HasData,        //!< There is an associated payload
1614870Sstever@eecs.umich.edu        IsError,        //!< Error response
1625314Sstever@gmail.com        IsPrint,        //!< Print state matching address (for debugging)
1638184Ssomayeh@cs.wisc.edu        IsFlush,        //!< Flush the address from caches
16411600Sandreas.hansson@arm.com        FromCache,      //!< Request originated from a caching agent
1654022Sstever@eecs.umich.edu        NUM_COMMAND_ATTRIBUTES
1664022Sstever@eecs.umich.edu    };
1674022Sstever@eecs.umich.edu
1685735Snate@binkert.org    /**
1695735Snate@binkert.org     * Structure that defines attributes and other data associated
1705735Snate@binkert.org     * with a Command.
1715735Snate@binkert.org     */
1725735Snate@binkert.org    struct CommandInfo
1735735Snate@binkert.org    {
1745735Snate@binkert.org        /// Set of attribute flags.
1754022Sstever@eecs.umich.edu        const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
1765735Snate@binkert.org        /// Corresponding response for requests; InvalidCmd if no
1775735Snate@binkert.org        /// response is applicable.
1784022Sstever@eecs.umich.edu        const Command response;
1795735Snate@binkert.org        /// String representation (for printing)
1804022Sstever@eecs.umich.edu        const std::string str;
1814022Sstever@eecs.umich.edu    };
1824022Sstever@eecs.umich.edu
1835735Snate@binkert.org    /// Array to map Command enum to associated info.
1844022Sstever@eecs.umich.edu    static const CommandInfo commandInfo[];
1854022Sstever@eecs.umich.edu
1864022Sstever@eecs.umich.edu  private:
1874022Sstever@eecs.umich.edu
1884022Sstever@eecs.umich.edu    Command cmd;
1894022Sstever@eecs.umich.edu
1905735Snate@binkert.org    bool
1915735Snate@binkert.org    testCmdAttrib(MemCmd::Attribute attrib) const
1925735Snate@binkert.org    {
1934022Sstever@eecs.umich.edu        return commandInfo[cmd].attributes[attrib] != 0;
1944022Sstever@eecs.umich.edu    }
1954022Sstever@eecs.umich.edu
1964022Sstever@eecs.umich.edu  public:
1974022Sstever@eecs.umich.edu
19810583SCurtis.Dunham@arm.com    bool isRead() const            { return testCmdAttrib(IsRead); }
19910583SCurtis.Dunham@arm.com    bool isWrite() const           { return testCmdAttrib(IsWrite); }
20010583SCurtis.Dunham@arm.com    bool isUpgrade() const         { return testCmdAttrib(IsUpgrade); }
20110583SCurtis.Dunham@arm.com    bool isRequest() const         { return testCmdAttrib(IsRequest); }
20210583SCurtis.Dunham@arm.com    bool isResponse() const        { return testCmdAttrib(IsResponse); }
20311284Sandreas.hansson@arm.com    bool needsWritable() const     { return testCmdAttrib(NeedsWritable); }
20410583SCurtis.Dunham@arm.com    bool needsResponse() const     { return testCmdAttrib(NeedsResponse); }
20510583SCurtis.Dunham@arm.com    bool isInvalidate() const      { return testCmdAttrib(IsInvalidate); }
20611199Sandreas.hansson@arm.com    bool isEviction() const        { return testCmdAttrib(IsEviction); }
20712347Snikos.nikoleris@arm.com    bool isClean() const           { return testCmdAttrib(IsClean); }
20811600Sandreas.hansson@arm.com    bool fromCache() const         { return testCmdAttrib(FromCache); }
20911199Sandreas.hansson@arm.com
21011199Sandreas.hansson@arm.com    /**
21111199Sandreas.hansson@arm.com     * A writeback is an eviction that carries data.
21211199Sandreas.hansson@arm.com     */
21311199Sandreas.hansson@arm.com    bool isWriteback() const       { return testCmdAttrib(IsEviction) &&
21411199Sandreas.hansson@arm.com                                            testCmdAttrib(HasData); }
21510570Sandreas.hansson@arm.com
21610570Sandreas.hansson@arm.com    /**
21710570Sandreas.hansson@arm.com     * Check if this particular packet type carries payload data. Note
21810570Sandreas.hansson@arm.com     * that this does not reflect if the data pointer of the packet is
21910570Sandreas.hansson@arm.com     * valid or not.
22010570Sandreas.hansson@arm.com     */
2214022Sstever@eecs.umich.edu    bool hasData() const        { return testCmdAttrib(HasData); }
2226102Sgblack@eecs.umich.edu    bool isLLSC() const         { return testCmdAttrib(IsLlsc); }
22310343SCurtis.Dunham@arm.com    bool isSWPrefetch() const   { return testCmdAttrib(IsSWPrefetch); }
22410343SCurtis.Dunham@arm.com    bool isHWPrefetch() const   { return testCmdAttrib(IsHWPrefetch); }
22510343SCurtis.Dunham@arm.com    bool isPrefetch() const     { return testCmdAttrib(IsSWPrefetch) ||
22610343SCurtis.Dunham@arm.com                                         testCmdAttrib(IsHWPrefetch); }
2274870Sstever@eecs.umich.edu    bool isError() const        { return testCmdAttrib(IsError); }
2285314Sstever@gmail.com    bool isPrint() const        { return testCmdAttrib(IsPrint); }
2298184Ssomayeh@cs.wisc.edu    bool isFlush() const        { return testCmdAttrib(IsFlush); }
2304022Sstever@eecs.umich.edu
23111294Sandreas.hansson@arm.com    Command
2325735Snate@binkert.org    responseCommand() const
2335735Snate@binkert.org    {
2344022Sstever@eecs.umich.edu        return commandInfo[cmd].response;
2354022Sstever@eecs.umich.edu    }
2364022Sstever@eecs.umich.edu
2375735Snate@binkert.org    /// Return the string to a cmd given by idx.
2385735Snate@binkert.org    const std::string &toString() const { return commandInfo[cmd].str; }
2394022Sstever@eecs.umich.edu    int toInt() const { return (int)cmd; }
2404022Sstever@eecs.umich.edu
2415735Snate@binkert.org    MemCmd(Command _cmd) : cmd(_cmd) { }
2425735Snate@binkert.org    MemCmd(int _cmd) : cmd((Command)_cmd) { }
2435735Snate@binkert.org    MemCmd() : cmd(InvalidCmd) { }
2444022Sstever@eecs.umich.edu
2455735Snate@binkert.org    bool operator==(MemCmd c2) const { return (cmd == c2.cmd); }
2465735Snate@binkert.org    bool operator!=(MemCmd c2) const { return (cmd != c2.cmd); }
2474022Sstever@eecs.umich.edu};
2484022Sstever@eecs.umich.edu
2492381SN/A/**
2502662Sstever@eecs.umich.edu * A Packet is used to encapsulate a transfer between two objects in
2512662Sstever@eecs.umich.edu * the memory system (e.g., the L1 and L2 cache).  (In contrast, a
2522662Sstever@eecs.umich.edu * single Request travels all the way from the requester to the
2532662Sstever@eecs.umich.edu * ultimate destination and back, possibly being conveyed by several
2542662Sstever@eecs.umich.edu * different Packets along the way.)
2552381SN/A */
2569044SAli.Saidi@ARM.comclass Packet : public Printable
2572381SN/A{
2582813Srdreslin@umich.edu  public:
2595735Snate@binkert.org    typedef uint32_t FlagsType;
2605735Snate@binkert.org    typedef ::Flags<FlagsType> Flags;
2614022Sstever@eecs.umich.edu
2625735Snate@binkert.org  private:
2635735Snate@binkert.org
26410938Sandreas.hansson@arm.com    enum : FlagsType {
26510938Sandreas.hansson@arm.com        // Flags to transfer across when copying a packet
26612349Snikos.nikoleris@arm.com        COPY_FLAGS             = 0x0000003F,
26710938Sandreas.hansson@arm.com
26811284Sandreas.hansson@arm.com        // Does this packet have sharers (which means it should not be
26911284Sandreas.hansson@arm.com        // considered writable) or not. See setHasSharers below.
27011284Sandreas.hansson@arm.com        HAS_SHARERS            = 0x00000001,
27111284Sandreas.hansson@arm.com
27210938Sandreas.hansson@arm.com        // Special control flags
27310938Sandreas.hansson@arm.com        /// Special timing-mode atomic snoop for multi-level coherence.
27410938Sandreas.hansson@arm.com        EXPRESS_SNOOP          = 0x00000002,
27511284Sandreas.hansson@arm.com
27611284Sandreas.hansson@arm.com        /// Allow a responding cache to inform the cache hierarchy
27711284Sandreas.hansson@arm.com        /// that it had a writable copy before responding. See
27811284Sandreas.hansson@arm.com        /// setResponderHadWritable below.
27911284Sandreas.hansson@arm.com        RESPONDER_HAD_WRITABLE = 0x00000004,
28011284Sandreas.hansson@arm.com
28111284Sandreas.hansson@arm.com        // Snoop co-ordination flag to indicate that a cache is
28211284Sandreas.hansson@arm.com        // responding to a snoop. See setCacheResponding below.
28311284Sandreas.hansson@arm.com        CACHE_RESPONDING       = 0x00000008,
28410938Sandreas.hansson@arm.com
28512346Snikos.nikoleris@arm.com        // The writeback/writeclean should be propagated further
28612346Snikos.nikoleris@arm.com        // downstream by the receiver
28712346Snikos.nikoleris@arm.com        WRITE_THROUGH          = 0x00000010,
28812346Snikos.nikoleris@arm.com
28912349Snikos.nikoleris@arm.com        // Response co-ordination flag for cache maintenance
29012349Snikos.nikoleris@arm.com        // operations
29112349Snikos.nikoleris@arm.com        SATISFIED              = 0x00000020,
29212349Snikos.nikoleris@arm.com
29311057Sandreas.hansson@arm.com        /// Are the 'addr' and 'size' fields valid?
29411057Sandreas.hansson@arm.com        VALID_ADDR             = 0x00000100,
29511057Sandreas.hansson@arm.com        VALID_SIZE             = 0x00000200,
29611057Sandreas.hansson@arm.com
29710938Sandreas.hansson@arm.com        /// Is the data pointer set to a value that shouldn't be freed
29810938Sandreas.hansson@arm.com        /// when the packet is destroyed?
29910938Sandreas.hansson@arm.com        STATIC_DATA            = 0x00001000,
30010938Sandreas.hansson@arm.com        /// The data pointer points to a value that should be freed when
30110938Sandreas.hansson@arm.com        /// the packet is destroyed. The pointer is assumed to be pointing
30210938Sandreas.hansson@arm.com        /// to an array, and delete [] is consequently called
30310938Sandreas.hansson@arm.com        DYNAMIC_DATA           = 0x00002000,
30410938Sandreas.hansson@arm.com
30510938Sandreas.hansson@arm.com        /// suppress the error if this packet encounters a functional
30610938Sandreas.hansson@arm.com        /// access failure.
30710938Sandreas.hansson@arm.com        SUPPRESS_FUNC_ERROR    = 0x00008000,
30810938Sandreas.hansson@arm.com
30910938Sandreas.hansson@arm.com        // Signal block present to squash prefetch and cache evict packets
31010938Sandreas.hansson@arm.com        // through express snoop flag
31110938Sandreas.hansson@arm.com        BLOCK_CACHED          = 0x00010000
31210938Sandreas.hansson@arm.com    };
3135735Snate@binkert.org
3145735Snate@binkert.org    Flags flags;
3155735Snate@binkert.org
3165735Snate@binkert.org  public:
3174022Sstever@eecs.umich.edu    typedef MemCmd::Command Command;
3184022Sstever@eecs.umich.edu
3195735Snate@binkert.org    /// The command field of the packet.
3204870Sstever@eecs.umich.edu    MemCmd cmd;
3214870Sstever@eecs.umich.edu
32212351Snikos.nikoleris@arm.com    const PacketId id;
32312351Snikos.nikoleris@arm.com
3245735Snate@binkert.org    /// A pointer to the original request.
32512749Sgiacomo.travaglini@arm.com    RequestPtr req;
3264870Sstever@eecs.umich.edu
3272566SN/A  private:
3285735Snate@binkert.org   /**
32912633Sodanrc@yahoo.com.br    * A pointer to the data being transferred. It can be different
33012633Sodanrc@yahoo.com.br    * sizes at each level of the hierarchy so it belongs to the
3315735Snate@binkert.org    * packet, not request. This may or may not be populated when a
33212633Sodanrc@yahoo.com.br    * responder receives the packet. If not populated memory should
3335735Snate@binkert.org    * be allocated.
3342566SN/A    */
3352566SN/A    PacketDataPtr data;
3362566SN/A
3375735Snate@binkert.org    /// The address of the request.  This address could be virtual or
3385735Snate@binkert.org    /// physical, depending on the system configuration.
3392381SN/A    Addr addr;
3402381SN/A
34110028SGiacomo.Gabrielli@arm.com    /// True if the request targets the secure memory space.
34210028SGiacomo.Gabrielli@arm.com    bool _isSecure;
34310028SGiacomo.Gabrielli@arm.com
3445735Snate@binkert.org    /// The size of the request or transfer.
3456227Snate@binkert.org    unsigned size;
3462381SN/A
3475735Snate@binkert.org    /**
34810723Sandreas.hansson@arm.com     * Track the bytes found that satisfy a functional read.
3498668Sgeoffrey.blake@arm.com     */
35010723Sandreas.hansson@arm.com    std::vector<bool> bytesValid;
3518668Sgeoffrey.blake@arm.com
35212966SMatteo.Andreozzi@arm.com    // Quality of Service priority value
35312966SMatteo.Andreozzi@arm.com    uint8_t _qosValue;
35412966SMatteo.Andreozzi@arm.com
3552641Sstever@eecs.umich.edu  public:
3562811Srdreslin@umich.edu
3579547Sandreas.hansson@arm.com    /**
35810694SMarco.Balboni@ARM.com     * The extra delay from seeing the packet until the header is
35910405Sandreas.hansson@arm.com     * transmitted. This delay is used to communicate the crossbar
36010405Sandreas.hansson@arm.com     * forwarding latency to the neighbouring object (e.g. a cache)
36110405Sandreas.hansson@arm.com     * that actually makes the packet wait. As the delay is relative,
36210405Sandreas.hansson@arm.com     * a 32-bit unsigned should be sufficient.
3639547Sandreas.hansson@arm.com     */
36410694SMarco.Balboni@ARM.com    uint32_t headerDelay;
3653218Sgblack@eecs.umich.edu
3669547Sandreas.hansson@arm.com    /**
36711127Sandreas.hansson@arm.com     * Keep track of the extra delay incurred by snooping upwards
36811127Sandreas.hansson@arm.com     * before sending a request down the memory system. This is used
36911127Sandreas.hansson@arm.com     * by the coherent crossbar to account for the additional request
37011127Sandreas.hansson@arm.com     * delay.
37111127Sandreas.hansson@arm.com     */
37211127Sandreas.hansson@arm.com    uint32_t snoopDelay;
37311127Sandreas.hansson@arm.com
37411127Sandreas.hansson@arm.com    /**
37510694SMarco.Balboni@ARM.com     * The extra pipelining delay from seeing the packet until the end of
37610694SMarco.Balboni@ARM.com     * payload is transmitted by the component that provided it (if
37710694SMarco.Balboni@ARM.com     * any). This includes the header delay. Similar to the header
37810694SMarco.Balboni@ARM.com     * delay, this is used to make up for the fact that the
37910405Sandreas.hansson@arm.com     * crossbar does not make the packet wait. As the delay is
38010405Sandreas.hansson@arm.com     * relative, a 32-bit unsigned should be sufficient.
3819547Sandreas.hansson@arm.com     */
38210694SMarco.Balboni@ARM.com    uint32_t payloadDelay;
3833218Sgblack@eecs.umich.edu
3845735Snate@binkert.org    /**
3855735Snate@binkert.org     * A virtual base opaque structure used to hold state associated
3869542Sandreas.hansson@arm.com     * with the packet (e.g., an MSHR), specific to a MemObject that
3879542Sandreas.hansson@arm.com     * sees the packet. A pointer to this state is returned in the
3889542Sandreas.hansson@arm.com     * packet's response so that the MemObject in question can quickly
3899542Sandreas.hansson@arm.com     * look up the state needed to process it. A specific subclass
3909542Sandreas.hansson@arm.com     * would be derived from this to carry state specific to a
3919542Sandreas.hansson@arm.com     * particular sending device.
3929542Sandreas.hansson@arm.com     *
3939542Sandreas.hansson@arm.com     * As multiple MemObjects may add their SenderState throughout the
3949542Sandreas.hansson@arm.com     * memory system, the SenderStates create a stack, where a
3959542Sandreas.hansson@arm.com     * MemObject can add a new Senderstate, as long as the
3969542Sandreas.hansson@arm.com     * predecessing SenderState is restored when the response comes
3979542Sandreas.hansson@arm.com     * back. For this reason, the predecessor should always be
3989542Sandreas.hansson@arm.com     * populated with the current SenderState of a packet before
3999542Sandreas.hansson@arm.com     * modifying the senderState field in the request packet.
4005735Snate@binkert.org     */
4015735Snate@binkert.org    struct SenderState
4025735Snate@binkert.org    {
4039542Sandreas.hansson@arm.com        SenderState* predecessor;
4049542Sandreas.hansson@arm.com        SenderState() : predecessor(NULL) {}
4052641Sstever@eecs.umich.edu        virtual ~SenderState() {}
4062641Sstever@eecs.umich.edu    };
4072641Sstever@eecs.umich.edu
4085315Sstever@gmail.com    /**
4095315Sstever@gmail.com     * Object used to maintain state of a PrintReq.  The senderState
4105315Sstever@gmail.com     * field of a PrintReq should always be of this type.
4115315Sstever@gmail.com     */
4129044SAli.Saidi@ARM.com    class PrintReqState : public SenderState
4135735Snate@binkert.org    {
4145735Snate@binkert.org      private:
4155735Snate@binkert.org        /**
4165735Snate@binkert.org         * An entry in the label stack.
4175735Snate@binkert.org         */
4185735Snate@binkert.org        struct LabelStackEntry
4195735Snate@binkert.org        {
4205314Sstever@gmail.com            const std::string label;
4215314Sstever@gmail.com            std::string *prefix;
4225314Sstever@gmail.com            bool labelPrinted;
4235735Snate@binkert.org            LabelStackEntry(const std::string &_label, std::string *_prefix);
4245314Sstever@gmail.com        };
4255314Sstever@gmail.com
4265314Sstever@gmail.com        typedef std::list<LabelStackEntry> LabelStack;
4275314Sstever@gmail.com        LabelStack labelStack;
4285314Sstever@gmail.com
4295314Sstever@gmail.com        std::string *curPrefixPtr;
4305314Sstever@gmail.com
4315314Sstever@gmail.com      public:
4325314Sstever@gmail.com        std::ostream &os;
4335314Sstever@gmail.com        const int verbosity;
4345314Sstever@gmail.com
4355314Sstever@gmail.com        PrintReqState(std::ostream &os, int verbosity = 0);
4365314Sstever@gmail.com        ~PrintReqState();
4375314Sstever@gmail.com
4385735Snate@binkert.org        /**
4395735Snate@binkert.org         * Returns the current line prefix.
4405735Snate@binkert.org         */
4415314Sstever@gmail.com        const std::string &curPrefix() { return *curPrefixPtr; }
4425315Sstever@gmail.com
4435735Snate@binkert.org        /**
4445735Snate@binkert.org         * Push a label onto the label stack, and prepend the given
4455315Sstever@gmail.com         * prefix string onto the current prefix.  Labels will only be
4465735Snate@binkert.org         * printed if an object within the label's scope is printed.
4475735Snate@binkert.org         */
4485314Sstever@gmail.com        void pushLabel(const std::string &lbl,
4495314Sstever@gmail.com                       const std::string &prefix = "  ");
4505735Snate@binkert.org
4515735Snate@binkert.org        /**
4525735Snate@binkert.org         * Pop a label off the label stack.
4535735Snate@binkert.org         */
4545314Sstever@gmail.com        void popLabel();
4555735Snate@binkert.org
4565735Snate@binkert.org        /**
4575735Snate@binkert.org         * Print all of the pending unprinted labels on the
4585315Sstever@gmail.com         * stack. Called by printObj(), so normally not called by
4595735Snate@binkert.org         * users unless bypassing printObj().
4605735Snate@binkert.org         */
4615314Sstever@gmail.com        void printLabels();
4625735Snate@binkert.org
4635735Snate@binkert.org        /**
4645735Snate@binkert.org         * Print a Printable object to os, because it matched the
4655735Snate@binkert.org         * address on a PrintReq.
4665735Snate@binkert.org         */
4675314Sstever@gmail.com        void printObj(Printable *obj);
4685314Sstever@gmail.com    };
4695314Sstever@gmail.com
4705735Snate@binkert.org    /**
4715735Snate@binkert.org     * This packet's sender state.  Devices should use dynamic_cast<>
4725735Snate@binkert.org     * to cast to the state appropriate to the sender.  The intent of
4735735Snate@binkert.org     * this variable is to allow a device to attach extra information
4749542Sandreas.hansson@arm.com     * to a request. A response packet must return the sender state
4755735Snate@binkert.org     * that was attached to the original request (even if a new packet
4765735Snate@binkert.org     * is created).
4775735Snate@binkert.org     */
4782662Sstever@eecs.umich.edu    SenderState *senderState;
4792641Sstever@eecs.umich.edu
4809542Sandreas.hansson@arm.com    /**
4819542Sandreas.hansson@arm.com     * Push a new sender state to the packet and make the current
4829542Sandreas.hansson@arm.com     * sender state the predecessor of the new one. This should be
4839542Sandreas.hansson@arm.com     * prefered over direct manipulation of the senderState member
4849542Sandreas.hansson@arm.com     * variable.
4859542Sandreas.hansson@arm.com     *
4869542Sandreas.hansson@arm.com     * @param sender_state SenderState to push at the top of the stack
4879542Sandreas.hansson@arm.com     */
4889542Sandreas.hansson@arm.com    void pushSenderState(SenderState *sender_state);
4899542Sandreas.hansson@arm.com
4909542Sandreas.hansson@arm.com    /**
4919542Sandreas.hansson@arm.com     * Pop the top of the state stack and return a pointer to it. This
4929542Sandreas.hansson@arm.com     * assumes the current sender state is not NULL. This should be
4939542Sandreas.hansson@arm.com     * preferred over direct manipulation of the senderState member
4949542Sandreas.hansson@arm.com     * variable.
4959542Sandreas.hansson@arm.com     *
4969542Sandreas.hansson@arm.com     * @return The current top of the stack
4979542Sandreas.hansson@arm.com     */
4989542Sandreas.hansson@arm.com    SenderState *popSenderState();
4999542Sandreas.hansson@arm.com
5009543Ssascha.bischoff@arm.com    /**
5019543Ssascha.bischoff@arm.com     * Go through the sender state stack and return the first instance
5029543Ssascha.bischoff@arm.com     * that is of type T (as determined by a dynamic_cast). If there
5039543Ssascha.bischoff@arm.com     * is no sender state of type T, NULL is returned.
5049543Ssascha.bischoff@arm.com     *
5059543Ssascha.bischoff@arm.com     * @return The topmost state of type T
5069543Ssascha.bischoff@arm.com     */
5079543Ssascha.bischoff@arm.com    template <typename T>
5089543Ssascha.bischoff@arm.com    T * findNextSenderState() const
5099543Ssascha.bischoff@arm.com    {
5109543Ssascha.bischoff@arm.com        T *t = NULL;
5119543Ssascha.bischoff@arm.com        SenderState* sender_state = senderState;
5129543Ssascha.bischoff@arm.com        while (t == NULL && sender_state != NULL) {
5139543Ssascha.bischoff@arm.com            t = dynamic_cast<T*>(sender_state);
5149543Ssascha.bischoff@arm.com            sender_state = sender_state->predecessor;
5159543Ssascha.bischoff@arm.com        }
5169543Ssascha.bischoff@arm.com        return t;
5179543Ssascha.bischoff@arm.com    }
5189543Ssascha.bischoff@arm.com
5195735Snate@binkert.org    /// Return the string name of the cmd field (for debugging and
5205735Snate@binkert.org    /// tracing).
5214022Sstever@eecs.umich.edu    const std::string &cmdString() const { return cmd.toString(); }
5222811Srdreslin@umich.edu
5235735Snate@binkert.org    /// Return the index of this command.
5244022Sstever@eecs.umich.edu    inline int cmdToIndex() const { return cmd.toInt(); }
5252811Srdreslin@umich.edu
52610583SCurtis.Dunham@arm.com    bool isRead() const              { return cmd.isRead(); }
52710583SCurtis.Dunham@arm.com    bool isWrite() const             { return cmd.isWrite(); }
52810583SCurtis.Dunham@arm.com    bool isUpgrade()  const          { return cmd.isUpgrade(); }
52910583SCurtis.Dunham@arm.com    bool isRequest() const           { return cmd.isRequest(); }
53010583SCurtis.Dunham@arm.com    bool isResponse() const          { return cmd.isResponse(); }
53111287Sandreas.hansson@arm.com    bool needsWritable() const
53211287Sandreas.hansson@arm.com    {
53311287Sandreas.hansson@arm.com        // we should never check if a response needsWritable, the
53411287Sandreas.hansson@arm.com        // request has this flag, and for a response we should rather
53511287Sandreas.hansson@arm.com        // look at the hasSharers flag (if not set, the response is to
53611287Sandreas.hansson@arm.com        // be considered writable)
53711287Sandreas.hansson@arm.com        assert(isRequest());
53811287Sandreas.hansson@arm.com        return cmd.needsWritable();
53911287Sandreas.hansson@arm.com    }
54010583SCurtis.Dunham@arm.com    bool needsResponse() const       { return cmd.needsResponse(); }
54110583SCurtis.Dunham@arm.com    bool isInvalidate() const        { return cmd.isInvalidate(); }
54211199Sandreas.hansson@arm.com    bool isEviction() const          { return cmd.isEviction(); }
54312347Snikos.nikoleris@arm.com    bool isClean() const             { return cmd.isClean(); }
54411600Sandreas.hansson@arm.com    bool fromCache() const           { return cmd.fromCache(); }
54511199Sandreas.hansson@arm.com    bool isWriteback() const         { return cmd.isWriteback(); }
54610583SCurtis.Dunham@arm.com    bool hasData() const             { return cmd.hasData(); }
54711286Sandreas.hansson@arm.com    bool hasRespData() const
54811286Sandreas.hansson@arm.com    {
54911286Sandreas.hansson@arm.com        MemCmd resp_cmd = cmd.responseCommand();
55011286Sandreas.hansson@arm.com        return resp_cmd.hasData();
55111286Sandreas.hansson@arm.com    }
55210583SCurtis.Dunham@arm.com    bool isLLSC() const              { return cmd.isLLSC(); }
55310583SCurtis.Dunham@arm.com    bool isError() const             { return cmd.isError(); }
55410583SCurtis.Dunham@arm.com    bool isPrint() const             { return cmd.isPrint(); }
55510583SCurtis.Dunham@arm.com    bool isFlush() const             { return cmd.isFlush(); }
5562812Srdreslin@umich.edu
55713350Snikos.nikoleris@arm.com    bool isWholeLineWrite(unsigned blk_size)
55813350Snikos.nikoleris@arm.com    {
55913350Snikos.nikoleris@arm.com        return (cmd == MemCmd::WriteReq || cmd == MemCmd::WriteLineReq) &&
56013350Snikos.nikoleris@arm.com            getOffset(blk_size) == 0 && getSize() == blk_size;
56113350Snikos.nikoleris@arm.com    }
56213350Snikos.nikoleris@arm.com
56311284Sandreas.hansson@arm.com    //@{
56411284Sandreas.hansson@arm.com    /// Snoop flags
56511284Sandreas.hansson@arm.com    /**
56611284Sandreas.hansson@arm.com     * Set the cacheResponding flag. This is used by the caches to
56711284Sandreas.hansson@arm.com     * signal another cache that they are responding to a request. A
56811284Sandreas.hansson@arm.com     * cache will only respond to snoops if it has the line in either
56911284Sandreas.hansson@arm.com     * Modified or Owned state. Note that on snoop hits we always pass
57011284Sandreas.hansson@arm.com     * the line as Modified and never Owned. In the case of an Owned
57111284Sandreas.hansson@arm.com     * line we proceed to invalidate all other copies.
57211284Sandreas.hansson@arm.com     *
57311284Sandreas.hansson@arm.com     * On a cache fill (see Cache::handleFill), we check hasSharers
57411284Sandreas.hansson@arm.com     * first, ignoring the cacheResponding flag if hasSharers is set.
57511284Sandreas.hansson@arm.com     * A line is consequently allocated as:
57611284Sandreas.hansson@arm.com     *
57711284Sandreas.hansson@arm.com     * hasSharers cacheResponding state
57811284Sandreas.hansson@arm.com     * true       false           Shared
57911284Sandreas.hansson@arm.com     * true       true            Shared
58011284Sandreas.hansson@arm.com     * false      false           Exclusive
58111284Sandreas.hansson@arm.com     * false      true            Modified
58211284Sandreas.hansson@arm.com     */
58311284Sandreas.hansson@arm.com    void setCacheResponding()
58410567Sandreas.hansson@arm.com    {
58510567Sandreas.hansson@arm.com        assert(isRequest());
58611284Sandreas.hansson@arm.com        assert(!flags.isSet(CACHE_RESPONDING));
58711284Sandreas.hansson@arm.com        flags.set(CACHE_RESPONDING);
58810567Sandreas.hansson@arm.com    }
58911284Sandreas.hansson@arm.com    bool cacheResponding() const { return flags.isSet(CACHE_RESPONDING); }
59011284Sandreas.hansson@arm.com    /**
59111284Sandreas.hansson@arm.com     * On fills, the hasSharers flag is used by the caches in
59211284Sandreas.hansson@arm.com     * combination with the cacheResponding flag, as clarified
59311284Sandreas.hansson@arm.com     * above. If the hasSharers flag is not set, the packet is passing
59411284Sandreas.hansson@arm.com     * writable. Thus, a response from a memory passes the line as
59511284Sandreas.hansson@arm.com     * writable by default.
59611284Sandreas.hansson@arm.com     *
59711284Sandreas.hansson@arm.com     * The hasSharers flag is also used by upstream caches to inform a
59811284Sandreas.hansson@arm.com     * downstream cache that they have the block (by calling
59911284Sandreas.hansson@arm.com     * setHasSharers on snoop request packets that hit in upstream
60011284Sandreas.hansson@arm.com     * cachs tags or MSHRs). If the snoop packet has sharers, a
60111284Sandreas.hansson@arm.com     * downstream cache is prevented from passing a dirty line upwards
60211284Sandreas.hansson@arm.com     * if it was not explicitly asked for a writable copy. See
60311284Sandreas.hansson@arm.com     * Cache::satisfyCpuSideRequest.
60411284Sandreas.hansson@arm.com     *
60511284Sandreas.hansson@arm.com     * The hasSharers flag is also used on writebacks, in
60611284Sandreas.hansson@arm.com     * combination with the WritbackClean or WritebackDirty commands,
60711284Sandreas.hansson@arm.com     * to allocate the block downstream either as:
60811284Sandreas.hansson@arm.com     *
60911284Sandreas.hansson@arm.com     * command        hasSharers state
61011284Sandreas.hansson@arm.com     * WritebackDirty false      Modified
61111284Sandreas.hansson@arm.com     * WritebackDirty true       Owned
61211284Sandreas.hansson@arm.com     * WritebackClean false      Exclusive
61311284Sandreas.hansson@arm.com     * WritebackClean true       Shared
61411284Sandreas.hansson@arm.com     */
61511284Sandreas.hansson@arm.com    void setHasSharers()    { flags.set(HAS_SHARERS); }
61611284Sandreas.hansson@arm.com    bool hasSharers() const { return flags.isSet(HAS_SHARERS); }
61711284Sandreas.hansson@arm.com    //@}
6184870Sstever@eecs.umich.edu
61911284Sandreas.hansson@arm.com    /**
62011284Sandreas.hansson@arm.com     * The express snoop flag is used for two purposes. Firstly, it is
62111284Sandreas.hansson@arm.com     * used to bypass flow control for normal (non-snoop) requests
62211284Sandreas.hansson@arm.com     * going downstream in the memory system. In cases where a cache
62311284Sandreas.hansson@arm.com     * is responding to a snoop from another cache (it had a dirty
62411284Sandreas.hansson@arm.com     * line), but the line is not writable (and there are possibly
62511284Sandreas.hansson@arm.com     * other copies), the express snoop flag is set by the downstream
62611284Sandreas.hansson@arm.com     * cache to invalidate all other copies in zero time. Secondly,
62711284Sandreas.hansson@arm.com     * the express snoop flag is also set to be able to distinguish
62811284Sandreas.hansson@arm.com     * snoop packets that came from a downstream cache, rather than
62911284Sandreas.hansson@arm.com     * snoop packets from neighbouring caches.
63011284Sandreas.hansson@arm.com     */
63111284Sandreas.hansson@arm.com    void setExpressSnoop()      { flags.set(EXPRESS_SNOOP); }
63211284Sandreas.hansson@arm.com    bool isExpressSnoop() const { return flags.isSet(EXPRESS_SNOOP); }
63311284Sandreas.hansson@arm.com
63411284Sandreas.hansson@arm.com    /**
63511284Sandreas.hansson@arm.com     * On responding to a snoop request (which only happens for
63611284Sandreas.hansson@arm.com     * Modified or Owned lines), make sure that we can transform an
63711284Sandreas.hansson@arm.com     * Owned response to a Modified one. If this flag is not set, the
63811284Sandreas.hansson@arm.com     * responding cache had the line in the Owned state, and there are
63911284Sandreas.hansson@arm.com     * possibly other Shared copies in the memory system. A downstream
64011284Sandreas.hansson@arm.com     * cache helps in orchestrating the invalidation of these copies
64111284Sandreas.hansson@arm.com     * by sending out the appropriate express snoops.
64211284Sandreas.hansson@arm.com     */
64311284Sandreas.hansson@arm.com    void setResponderHadWritable()
64411284Sandreas.hansson@arm.com    {
64511284Sandreas.hansson@arm.com        assert(cacheResponding());
64611746Snikos.nikoleris@arm.com        assert(!responderHadWritable());
64711284Sandreas.hansson@arm.com        flags.set(RESPONDER_HAD_WRITABLE);
64811284Sandreas.hansson@arm.com    }
64911284Sandreas.hansson@arm.com    bool responderHadWritable() const
65011284Sandreas.hansson@arm.com    { return flags.isSet(RESPONDER_HAD_WRITABLE); }
65111284Sandreas.hansson@arm.com
65212346Snikos.nikoleris@arm.com    /**
65312346Snikos.nikoleris@arm.com     * A writeback/writeclean cmd gets propagated further downstream
65412346Snikos.nikoleris@arm.com     * by the receiver when the flag is set.
65512346Snikos.nikoleris@arm.com     */
65612346Snikos.nikoleris@arm.com    void setWriteThrough()
65712346Snikos.nikoleris@arm.com    {
65812346Snikos.nikoleris@arm.com        assert(cmd.isWrite() &&
65912346Snikos.nikoleris@arm.com               (cmd.isEviction() || cmd == MemCmd::WriteClean));
66012346Snikos.nikoleris@arm.com        flags.set(WRITE_THROUGH);
66112346Snikos.nikoleris@arm.com    }
66212346Snikos.nikoleris@arm.com    void clearWriteThrough() { flags.clear(WRITE_THROUGH); }
66312346Snikos.nikoleris@arm.com    bool writeThrough() const { return flags.isSet(WRITE_THROUGH); }
66412346Snikos.nikoleris@arm.com
66512349Snikos.nikoleris@arm.com    /**
66612349Snikos.nikoleris@arm.com     * Set when a request hits in a cache and the cache is not going
66712349Snikos.nikoleris@arm.com     * to respond. This is used by the crossbar to coordinate
66812349Snikos.nikoleris@arm.com     * responses for cache maintenance operations.
66912349Snikos.nikoleris@arm.com     */
67012349Snikos.nikoleris@arm.com    void setSatisfied()
67112349Snikos.nikoleris@arm.com    {
67212349Snikos.nikoleris@arm.com        assert(cmd.isClean());
67312349Snikos.nikoleris@arm.com        assert(!flags.isSet(SATISFIED));
67412349Snikos.nikoleris@arm.com        flags.set(SATISFIED);
67512349Snikos.nikoleris@arm.com    }
67612349Snikos.nikoleris@arm.com    bool satisfied() const { return flags.isSet(SATISFIED); }
67712349Snikos.nikoleris@arm.com
6789951Sstephan.diestelhorst@arm.com    void setSuppressFuncError()     { flags.set(SUPPRESS_FUNC_ERROR); }
6799951Sstephan.diestelhorst@arm.com    bool suppressFuncError() const  { return flags.isSet(SUPPRESS_FUNC_ERROR); }
68010763Sali.jafri@arm.com    void setBlockCached()          { flags.set(BLOCK_CACHED); }
68110763Sali.jafri@arm.com    bool isBlockCached() const     { return flags.isSet(BLOCK_CACHED); }
68210883Sali.jafri@arm.com    void clearBlockCached()        { flags.clear(BLOCK_CACHED); }
6834895Sstever@eecs.umich.edu
68412966SMatteo.Andreozzi@arm.com    /**
68512966SMatteo.Andreozzi@arm.com     * QoS Value getter
68612966SMatteo.Andreozzi@arm.com     * Returns 0 if QoS value was never set (constructor default).
68712966SMatteo.Andreozzi@arm.com     *
68812966SMatteo.Andreozzi@arm.com     * @return QoS priority value of the packet
68912966SMatteo.Andreozzi@arm.com     */
69012966SMatteo.Andreozzi@arm.com    inline uint8_t qosValue() const { return _qosValue; }
69112966SMatteo.Andreozzi@arm.com
69212966SMatteo.Andreozzi@arm.com    /**
69312966SMatteo.Andreozzi@arm.com     * QoS Value setter
69412966SMatteo.Andreozzi@arm.com     * Interface for setting QoS priority value of the packet.
69512966SMatteo.Andreozzi@arm.com     *
69612966SMatteo.Andreozzi@arm.com     * @param qos_value QoS priority value
69712966SMatteo.Andreozzi@arm.com     */
69812966SMatteo.Andreozzi@arm.com    inline void qosValue(const uint8_t qos_value)
69912966SMatteo.Andreozzi@arm.com    { _qosValue = qos_value; }
70012966SMatteo.Andreozzi@arm.com
70112966SMatteo.Andreozzi@arm.com    inline MasterID masterId() const { return req->masterId(); }
70212966SMatteo.Andreozzi@arm.com
7034870Sstever@eecs.umich.edu    // Network error conditions... encapsulate them as methods since
7044870Sstever@eecs.umich.edu    // their encoding keeps changing (from result field to command
7054870Sstever@eecs.umich.edu    // field, etc.)
7065735Snate@binkert.org    void
7075735Snate@binkert.org    setBadAddress()
7085735Snate@binkert.org    {
7095735Snate@binkert.org        assert(isResponse());
7105735Snate@binkert.org        cmd = MemCmd::BadAddressError;
7115735Snate@binkert.org    }
7125735Snate@binkert.org
7134986Ssaidi@eecs.umich.edu    void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
7142814Srdreslin@umich.edu
71511057Sandreas.hansson@arm.com    Addr getAddr() const { assert(flags.isSet(VALID_ADDR)); return addr; }
7169259SAli.Saidi@ARM.com    /**
7179259SAli.Saidi@ARM.com     * Update the address of this packet mid-transaction. This is used
7189259SAli.Saidi@ARM.com     * by the address mapper to change an already set address to a new
7199259SAli.Saidi@ARM.com     * one based on the system configuration. It is intended to remap
7209259SAli.Saidi@ARM.com     * an existing address, so it asserts that the current address is
7219259SAli.Saidi@ARM.com     * valid.
7229259SAli.Saidi@ARM.com     */
72311057Sandreas.hansson@arm.com    void setAddr(Addr _addr) { assert(flags.isSet(VALID_ADDR)); addr = _addr; }
7249259SAli.Saidi@ARM.com
72511057Sandreas.hansson@arm.com    unsigned getSize() const  { assert(flags.isSet(VALID_SIZE)); return size; }
72610938Sandreas.hansson@arm.com
72710938Sandreas.hansson@arm.com    Addr getOffset(unsigned int blk_size) const
72810938Sandreas.hansson@arm.com    {
72910938Sandreas.hansson@arm.com        return getAddr() & Addr(blk_size - 1);
73010938Sandreas.hansson@arm.com    }
73110938Sandreas.hansson@arm.com
73210938Sandreas.hansson@arm.com    Addr getBlockAddr(unsigned int blk_size) const
73310938Sandreas.hansson@arm.com    {
73410938Sandreas.hansson@arm.com        return getAddr() & ~(Addr(blk_size - 1));
73510938Sandreas.hansson@arm.com    }
7362549SN/A
73711057Sandreas.hansson@arm.com    bool isSecure() const
73811057Sandreas.hansson@arm.com    {
73911057Sandreas.hansson@arm.com        assert(flags.isSet(VALID_ADDR));
74011057Sandreas.hansson@arm.com        return _isSecure;
74111057Sandreas.hansson@arm.com    }
74210028SGiacomo.Gabrielli@arm.com
7435735Snate@binkert.org    /**
74411306Santhony.gutierrez@amd.com     * Accessor function to atomic op.
74511306Santhony.gutierrez@amd.com     */
74611306Santhony.gutierrez@amd.com    AtomicOpFunctor *getAtomicOp() const { return req->getAtomicOpFunctor(); }
74711306Santhony.gutierrez@amd.com    bool isAtomicOp() const { return req->isAtomic(); }
74811306Santhony.gutierrez@amd.com
74911306Santhony.gutierrez@amd.com    /**
7507550SBrad.Beckmann@amd.com     * It has been determined that the SC packet should successfully update
75110938Sandreas.hansson@arm.com     * memory. Therefore, convert this SC packet to a normal write.
7527550SBrad.Beckmann@amd.com     */
7537550SBrad.Beckmann@amd.com    void
7547550SBrad.Beckmann@amd.com    convertScToWrite()
7557550SBrad.Beckmann@amd.com    {
7567550SBrad.Beckmann@amd.com        assert(isLLSC());
7577550SBrad.Beckmann@amd.com        assert(isWrite());
7587550SBrad.Beckmann@amd.com        cmd = MemCmd::WriteReq;
7597550SBrad.Beckmann@amd.com    }
7607550SBrad.Beckmann@amd.com
7617550SBrad.Beckmann@amd.com    /**
76210938Sandreas.hansson@arm.com     * When ruby is in use, Ruby will monitor the cache line and the
76310938Sandreas.hansson@arm.com     * phys memory should treat LL ops as normal reads.
7647550SBrad.Beckmann@amd.com     */
7657550SBrad.Beckmann@amd.com    void
7667550SBrad.Beckmann@amd.com    convertLlToRead()
7677550SBrad.Beckmann@amd.com    {
7687550SBrad.Beckmann@amd.com        assert(isLLSC());
7697550SBrad.Beckmann@amd.com        assert(isRead());
7707550SBrad.Beckmann@amd.com        cmd = MemCmd::ReadReq;
7717550SBrad.Beckmann@amd.com    }
7727550SBrad.Beckmann@amd.com
7737550SBrad.Beckmann@amd.com    /**
77410938Sandreas.hansson@arm.com     * Constructor. Note that a Request object must be constructed
77511057Sandreas.hansson@arm.com     * first, but the Requests's physical address and size fields need
77611057Sandreas.hansson@arm.com     * not be valid. The command must be supplied.
7775735Snate@binkert.org     */
77812749Sgiacomo.travaglini@arm.com    Packet(const RequestPtr &_req, MemCmd _cmd)
77912966SMatteo.Andreozzi@arm.com        :  cmd(_cmd), id((PacketId)_req.get()), req(_req),
78012966SMatteo.Andreozzi@arm.com           data(nullptr), addr(0), _isSecure(false), size(0),
78112966SMatteo.Andreozzi@arm.com           _qosValue(0), headerDelay(0), snoopDelay(0),
78212351Snikos.nikoleris@arm.com           payloadDelay(0), senderState(NULL)
78311057Sandreas.hansson@arm.com    {
78411057Sandreas.hansson@arm.com        if (req->hasPaddr()) {
78511057Sandreas.hansson@arm.com            addr = req->getPaddr();
78611057Sandreas.hansson@arm.com            flags.set(VALID_ADDR);
78711057Sandreas.hansson@arm.com            _isSecure = req->isSecure();
78811057Sandreas.hansson@arm.com        }
78911057Sandreas.hansson@arm.com        if (req->hasSize()) {
79011057Sandreas.hansson@arm.com            size = req->getSize();
79111057Sandreas.hansson@arm.com            flags.set(VALID_SIZE);
79211057Sandreas.hansson@arm.com        }
79311057Sandreas.hansson@arm.com    }
79411057Sandreas.hansson@arm.com
79511057Sandreas.hansson@arm.com    /**
79611057Sandreas.hansson@arm.com     * Alternate constructor if you are trying to create a packet with
79711057Sandreas.hansson@arm.com     * a request that is for a whole block, not the address from the
79811057Sandreas.hansson@arm.com     * req.  this allows for overriding the size/addr of the req.
79911057Sandreas.hansson@arm.com     */
80012749Sgiacomo.travaglini@arm.com    Packet(const RequestPtr &_req, MemCmd _cmd, int _blkSize, PacketId _id = 0)
80112749Sgiacomo.travaglini@arm.com        :  cmd(_cmd), id(_id ? _id : (PacketId)_req.get()), req(_req),
80212966SMatteo.Andreozzi@arm.com           data(nullptr), addr(0), _isSecure(false),
80312966SMatteo.Andreozzi@arm.com           _qosValue(0), headerDelay(0),
80412749Sgiacomo.travaglini@arm.com           snoopDelay(0), payloadDelay(0), senderState(NULL)
80511057Sandreas.hansson@arm.com    {
80611057Sandreas.hansson@arm.com        if (req->hasPaddr()) {
80711057Sandreas.hansson@arm.com            addr = req->getPaddr() & ~(_blkSize - 1);
80811057Sandreas.hansson@arm.com            flags.set(VALID_ADDR);
80911057Sandreas.hansson@arm.com            _isSecure = req->isSecure();
81011057Sandreas.hansson@arm.com        }
81111057Sandreas.hansson@arm.com        size = _blkSize;
81211057Sandreas.hansson@arm.com        flags.set(VALID_SIZE);
81311057Sandreas.hansson@arm.com    }
8144626Sstever@eecs.umich.edu
8155735Snate@binkert.org    /**
8165735Snate@binkert.org     * Alternate constructor for copying a packet.  Copy all fields
8174887Sstever@eecs.umich.edu     * *except* if the original packet's data was dynamic, don't copy
8184887Sstever@eecs.umich.edu     * that, as we can't guarantee that the new packet's lifetime is
8194887Sstever@eecs.umich.edu     * less than that of the original packet.  In this case the new
8205735Snate@binkert.org     * packet should allocate its own data.
8215735Snate@binkert.org     */
82210896Snilay@cs.wisc.edu    Packet(const PacketPtr pkt, bool clear_flags, bool alloc_data)
82312351Snikos.nikoleris@arm.com        :  cmd(pkt->cmd), id(pkt->id), req(pkt->req),
82410571Sandreas.hansson@arm.com           data(nullptr),
82510028SGiacomo.Gabrielli@arm.com           addr(pkt->addr), _isSecure(pkt->_isSecure), size(pkt->size),
82610723Sandreas.hansson@arm.com           bytesValid(pkt->bytesValid),
82712966SMatteo.Andreozzi@arm.com           _qosValue(pkt->qosValue()),
82810694SMarco.Balboni@ARM.com           headerDelay(pkt->headerDelay),
82911127Sandreas.hansson@arm.com           snoopDelay(0),
83010694SMarco.Balboni@ARM.com           payloadDelay(pkt->payloadDelay),
8319546Sandreas.hansson@arm.com           senderState(pkt->senderState)
8324626Sstever@eecs.umich.edu    {
83310571Sandreas.hansson@arm.com        if (!clear_flags)
8345735Snate@binkert.org            flags.set(pkt->flags & COPY_FLAGS);
8355735Snate@binkert.org
83611057Sandreas.hansson@arm.com        flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE));
83711057Sandreas.hansson@arm.com
83810571Sandreas.hansson@arm.com        // should we allocate space for data, or not, the express
83910571Sandreas.hansson@arm.com        // snoops do not need to carry any data as they only serve to
84010571Sandreas.hansson@arm.com        // co-ordinate state changes
84110571Sandreas.hansson@arm.com        if (alloc_data) {
84210571Sandreas.hansson@arm.com            // even if asked to allocate data, if the original packet
84310571Sandreas.hansson@arm.com            // holds static data, then the sender will not be doing
84410571Sandreas.hansson@arm.com            // any memcpy on receiving the response, thus we simply
84510571Sandreas.hansson@arm.com            // carry the pointer forward
84610571Sandreas.hansson@arm.com            if (pkt->flags.isSet(STATIC_DATA)) {
84710571Sandreas.hansson@arm.com                data = pkt->data;
84810571Sandreas.hansson@arm.com                flags.set(STATIC_DATA);
84910571Sandreas.hansson@arm.com            } else {
85010571Sandreas.hansson@arm.com                allocate();
85110571Sandreas.hansson@arm.com            }
85210571Sandreas.hansson@arm.com        }
85310342SCurtis.Dunham@arm.com    }
8548668Sgeoffrey.blake@arm.com
85510342SCurtis.Dunham@arm.com    /**
85610739Ssteve.reinhardt@amd.com     * Generate the appropriate read MemCmd based on the Request flags.
85710342SCurtis.Dunham@arm.com     */
85810739Ssteve.reinhardt@amd.com    static MemCmd
85912749Sgiacomo.travaglini@arm.com    makeReadCmd(const RequestPtr &req)
86010342SCurtis.Dunham@arm.com    {
86110739Ssteve.reinhardt@amd.com        if (req->isLLSC())
86210739Ssteve.reinhardt@amd.com            return MemCmd::LoadLockedReq;
86313367Syuetsu.kodama@riken.jp        else if (req->isPrefetchEx())
86413367Syuetsu.kodama@riken.jp            return MemCmd::SoftPFExReq;
86510739Ssteve.reinhardt@amd.com        else if (req->isPrefetch())
86610739Ssteve.reinhardt@amd.com            return MemCmd::SoftPFReq;
86710739Ssteve.reinhardt@amd.com        else
86810739Ssteve.reinhardt@amd.com            return MemCmd::ReadReq;
86910739Ssteve.reinhardt@amd.com    }
87010739Ssteve.reinhardt@amd.com
87110739Ssteve.reinhardt@amd.com    /**
87210739Ssteve.reinhardt@amd.com     * Generate the appropriate write MemCmd based on the Request flags.
87310739Ssteve.reinhardt@amd.com     */
87410739Ssteve.reinhardt@amd.com    static MemCmd
87512749Sgiacomo.travaglini@arm.com    makeWriteCmd(const RequestPtr &req)
87610739Ssteve.reinhardt@amd.com    {
87710739Ssteve.reinhardt@amd.com        if (req->isLLSC())
87810739Ssteve.reinhardt@amd.com            return MemCmd::StoreCondReq;
87912766Sqtt2@cornell.edu        else if (req->isSwap() || req->isAtomic())
88010739Ssteve.reinhardt@amd.com            return MemCmd::SwapReq;
88112347Snikos.nikoleris@arm.com        else if (req->isCacheInvalidate()) {
88212347Snikos.nikoleris@arm.com          return req->isCacheClean() ? MemCmd::CleanInvalidReq :
88312347Snikos.nikoleris@arm.com              MemCmd::InvalidateReq;
88412347Snikos.nikoleris@arm.com        } else if (req->isCacheClean()) {
88512347Snikos.nikoleris@arm.com            return MemCmd::CleanSharedReq;
88612347Snikos.nikoleris@arm.com        } else
88710739Ssteve.reinhardt@amd.com            return MemCmd::WriteReq;
88810342SCurtis.Dunham@arm.com    }
88910342SCurtis.Dunham@arm.com
89010342SCurtis.Dunham@arm.com    /**
89110342SCurtis.Dunham@arm.com     * Constructor-like methods that return Packets based on Request objects.
89210739Ssteve.reinhardt@amd.com     * Fine-tune the MemCmd type if it's not a vanilla read or write.
89310342SCurtis.Dunham@arm.com     */
89410342SCurtis.Dunham@arm.com    static PacketPtr
89512749Sgiacomo.travaglini@arm.com    createRead(const RequestPtr &req)
89610342SCurtis.Dunham@arm.com    {
89710739Ssteve.reinhardt@amd.com        return new Packet(req, makeReadCmd(req));
89810342SCurtis.Dunham@arm.com    }
89910342SCurtis.Dunham@arm.com
90010342SCurtis.Dunham@arm.com    static PacketPtr
90112749Sgiacomo.travaglini@arm.com    createWrite(const RequestPtr &req)
90210342SCurtis.Dunham@arm.com    {
90310739Ssteve.reinhardt@amd.com        return new Packet(req, makeWriteCmd(req));
9042641Sstever@eecs.umich.edu    }
9052549SN/A
9065735Snate@binkert.org    /**
9075735Snate@binkert.org     * clean up packet variables
9085735Snate@binkert.org     */
9092566SN/A    ~Packet()
9105387Sstever@gmail.com    {
9115735Snate@binkert.org        deleteData();
9125387Sstever@gmail.com    }
9132566SN/A
9145735Snate@binkert.org    /**
9154626Sstever@eecs.umich.edu     * Take a request packet and modify it in place to be suitable for
91610660Sandreas.hansson@arm.com     * returning as a response to that request.
9174626Sstever@eecs.umich.edu     */
9185735Snate@binkert.org    void
9195735Snate@binkert.org    makeResponse()
9204626Sstever@eecs.umich.edu    {
9212662Sstever@eecs.umich.edu        assert(needsResponse());
9222855Srdreslin@umich.edu        assert(isRequest());
9234022Sstever@eecs.umich.edu        cmd = cmd.responseCommand();
9245745Snate@binkert.org
9257464Ssteve.reinhardt@amd.com        // responses are never express, even if the snoop that
9267464Ssteve.reinhardt@amd.com        // triggered them was
9277464Ssteve.reinhardt@amd.com        flags.clear(EXPRESS_SNOOP);
9282641Sstever@eecs.umich.edu    }
9292641Sstever@eecs.umich.edu
9305735Snate@binkert.org    void
9315735Snate@binkert.org    makeAtomicResponse()
9324870Sstever@eecs.umich.edu    {
9334870Sstever@eecs.umich.edu        makeResponse();
9344870Sstever@eecs.umich.edu    }
9354870Sstever@eecs.umich.edu
9365735Snate@binkert.org    void
9375735Snate@binkert.org    makeTimingResponse()
9383348Sbinkertn@umich.edu    {
9394870Sstever@eecs.umich.edu        makeResponse();
9403135Srdreslin@umich.edu    }
9413135Srdreslin@umich.edu
9428436SBrad.Beckmann@amd.com    void
9438436SBrad.Beckmann@amd.com    setFunctionalResponseStatus(bool success)
9448436SBrad.Beckmann@amd.com    {
9458436SBrad.Beckmann@amd.com        if (!success) {
9468436SBrad.Beckmann@amd.com            if (isWrite()) {
9478436SBrad.Beckmann@amd.com                cmd = MemCmd::FunctionalWriteError;
9488436SBrad.Beckmann@amd.com            } else {
9498436SBrad.Beckmann@amd.com                cmd = MemCmd::FunctionalReadError;
9508436SBrad.Beckmann@amd.com            }
9518436SBrad.Beckmann@amd.com        }
9528436SBrad.Beckmann@amd.com    }
9538436SBrad.Beckmann@amd.com
95411057Sandreas.hansson@arm.com    void
95511057Sandreas.hansson@arm.com    setSize(unsigned size)
95611057Sandreas.hansson@arm.com    {
95711057Sandreas.hansson@arm.com        assert(!flags.isSet(VALID_SIZE));
95811057Sandreas.hansson@arm.com
95911057Sandreas.hansson@arm.com        this->size = size;
96011057Sandreas.hansson@arm.com        flags.set(VALID_SIZE);
96111057Sandreas.hansson@arm.com    }
96211057Sandreas.hansson@arm.com
96311057Sandreas.hansson@arm.com
96411013Sandreas.sandberg@arm.com  public:
96511013Sandreas.sandberg@arm.com    /**
96611013Sandreas.sandberg@arm.com     * @{
96711013Sandreas.sandberg@arm.com     * @name Data accessor mehtods
96811013Sandreas.sandberg@arm.com     */
96911013Sandreas.sandberg@arm.com
9703348Sbinkertn@umich.edu    /**
9713348Sbinkertn@umich.edu     * Set the data pointer to the following value that should not be
97210571Sandreas.hansson@arm.com     * freed. Static data allows us to do a single memcpy even if
97310571Sandreas.hansson@arm.com     * multiple packets are required to get from source to destination
97410571Sandreas.hansson@arm.com     * and back. In essence the pointer is set calling dataStatic on
97510571Sandreas.hansson@arm.com     * the original packet, and whenever this packet is copied and
97610571Sandreas.hansson@arm.com     * forwarded the same pointer is passed on. When a packet
97710571Sandreas.hansson@arm.com     * eventually reaches the destination holding the data, it is
97810571Sandreas.hansson@arm.com     * copied once into the location originally set. On the way back
97910571Sandreas.hansson@arm.com     * to the source, no copies are necessary.
9802566SN/A     */
9812566SN/A    template <typename T>
9823348Sbinkertn@umich.edu    void
9833348Sbinkertn@umich.edu    dataStatic(T *p)
9843348Sbinkertn@umich.edu    {
98510566Sandreas.hansson@arm.com        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
9863348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
9875735Snate@binkert.org        flags.set(STATIC_DATA);
9883348Sbinkertn@umich.edu    }
9892566SN/A
9903348Sbinkertn@umich.edu    /**
99110564Sandreas.hansson@arm.com     * Set the data pointer to the following value that should not be
99210564Sandreas.hansson@arm.com     * freed. This version of the function allows the pointer passed
99310564Sandreas.hansson@arm.com     * to us to be const. To avoid issues down the line we cast the
99410564Sandreas.hansson@arm.com     * constness away, the alternative would be to keep both a const
99510564Sandreas.hansson@arm.com     * and non-const data pointer and cleverly choose between
99610564Sandreas.hansson@arm.com     * them. Note that this is only allowed for static data.
99710564Sandreas.hansson@arm.com     */
99810564Sandreas.hansson@arm.com    template <typename T>
99910564Sandreas.hansson@arm.com    void
100010564Sandreas.hansson@arm.com    dataStaticConst(const T *p)
100110564Sandreas.hansson@arm.com    {
100210566Sandreas.hansson@arm.com        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
100310564Sandreas.hansson@arm.com        data = const_cast<PacketDataPtr>(p);
100410564Sandreas.hansson@arm.com        flags.set(STATIC_DATA);
100510564Sandreas.hansson@arm.com    }
100610564Sandreas.hansson@arm.com
100710564Sandreas.hansson@arm.com    /**
10083348Sbinkertn@umich.edu     * Set the data pointer to a value that should have delete []
100910571Sandreas.hansson@arm.com     * called on it. Dynamic data is local to this packet, and as the
101010571Sandreas.hansson@arm.com     * packet travels from source to destination, forwarded packets
101110571Sandreas.hansson@arm.com     * will allocate their own data. When a packet reaches the final
101210571Sandreas.hansson@arm.com     * destination it will populate the dynamic data of that specific
101310571Sandreas.hansson@arm.com     * packet, and on the way back towards the source, memcpy will be
101410571Sandreas.hansson@arm.com     * invoked in every step where a new packet was created e.g. in
101510571Sandreas.hansson@arm.com     * the caches. Ultimately when the response reaches the source a
101610571Sandreas.hansson@arm.com     * final memcpy is needed to extract the data from the packet
101710571Sandreas.hansson@arm.com     * before it is deallocated.
10183348Sbinkertn@umich.edu     */
10192566SN/A    template <typename T>
10203348Sbinkertn@umich.edu    void
10213348Sbinkertn@umich.edu    dataDynamic(T *p)
10223348Sbinkertn@umich.edu    {
102310566Sandreas.hansson@arm.com        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
10243348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
10255735Snate@binkert.org        flags.set(DYNAMIC_DATA);
10263348Sbinkertn@umich.edu    }
10273348Sbinkertn@umich.edu
10285735Snate@binkert.org    /**
10295735Snate@binkert.org     * get a pointer to the data ptr.
10305735Snate@binkert.org     */
10313348Sbinkertn@umich.edu    template <typename T>
10323348Sbinkertn@umich.edu    T*
103310562Sandreas.hansson@arm.com    getPtr()
10343348Sbinkertn@umich.edu    {
103510562Sandreas.hansson@arm.com        assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
10363348Sbinkertn@umich.edu        return (T*)data;
10373348Sbinkertn@umich.edu    }
10382566SN/A
103910563Sandreas.hansson@arm.com    template <typename T>
104010563Sandreas.hansson@arm.com    const T*
104110563Sandreas.hansson@arm.com    getConstPtr() const
104210563Sandreas.hansson@arm.com    {
104310563Sandreas.hansson@arm.com        assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
104410563Sandreas.hansson@arm.com        return (const T*)data;
104510563Sandreas.hansson@arm.com    }
104610563Sandreas.hansson@arm.com
10475735Snate@binkert.org    /**
104811013Sandreas.sandberg@arm.com     * Get the data in the packet byte swapped from big endian to
104911013Sandreas.sandberg@arm.com     * host endian.
105011013Sandreas.sandberg@arm.com     */
105111013Sandreas.sandberg@arm.com    template <typename T>
105211013Sandreas.sandberg@arm.com    T getBE() const;
105311013Sandreas.sandberg@arm.com
105411013Sandreas.sandberg@arm.com    /**
105511013Sandreas.sandberg@arm.com     * Get the data in the packet byte swapped from little endian to
105611013Sandreas.sandberg@arm.com     * host endian.
105711013Sandreas.sandberg@arm.com     */
105811013Sandreas.sandberg@arm.com    template <typename T>
105911013Sandreas.sandberg@arm.com    T getLE() const;
106011013Sandreas.sandberg@arm.com
106111013Sandreas.sandberg@arm.com    /**
106211013Sandreas.sandberg@arm.com     * Get the data in the packet byte swapped from the specified
106311013Sandreas.sandberg@arm.com     * endianness.
106411013Sandreas.sandberg@arm.com     */
106511013Sandreas.sandberg@arm.com    template <typename T>
106611013Sandreas.sandberg@arm.com    T get(ByteOrder endian) const;
106711013Sandreas.sandberg@arm.com
106813347Sgabeblack@google.com#if THE_ISA != NULL_ISA
106911013Sandreas.sandberg@arm.com    /**
107011013Sandreas.sandberg@arm.com     * Get the data in the packet byte swapped from guest to host
107111013Sandreas.sandberg@arm.com     * endian.
10725735Snate@binkert.org     */
10732566SN/A    template <typename T>
107413348Sgabeblack@google.com    T get() const
107513348Sgabeblack@google.com        M5_DEPRECATED_MSG("The memory system should be ISA independent.");
107613347Sgabeblack@google.com#endif
10772566SN/A
107811013Sandreas.sandberg@arm.com    /** Set the value in the data pointer to v as big endian. */
107911013Sandreas.sandberg@arm.com    template <typename T>
108011013Sandreas.sandberg@arm.com    void setBE(T v);
108111013Sandreas.sandberg@arm.com
108211013Sandreas.sandberg@arm.com    /** Set the value in the data pointer to v as little endian. */
108311013Sandreas.sandberg@arm.com    template <typename T>
108411013Sandreas.sandberg@arm.com    void setLE(T v);
108511013Sandreas.sandberg@arm.com
10865735Snate@binkert.org    /**
108711013Sandreas.sandberg@arm.com     * Set the value in the data pointer to v using the specified
108811013Sandreas.sandberg@arm.com     * endianness.
10895735Snate@binkert.org     */
10902566SN/A    template <typename T>
109111013Sandreas.sandberg@arm.com    void set(T v, ByteOrder endian);
109211013Sandreas.sandberg@arm.com
109313347Sgabeblack@google.com#if THE_ISA != NULL_ISA
109411013Sandreas.sandberg@arm.com    /** Set the value in the data pointer to v as guest endian. */
109511013Sandreas.sandberg@arm.com    template <typename T>
109613348Sgabeblack@google.com    void set(T v)
109713348Sgabeblack@google.com        M5_DEPRECATED_MSG("The memory system should be ISA independent.");
109813347Sgabeblack@google.com#endif
10992566SN/A
110012652Sandreas.sandberg@arm.com    /**
110112652Sandreas.sandberg@arm.com     * Get the data in the packet byte swapped from the specified
110212652Sandreas.sandberg@arm.com     * endianness and zero-extended to 64 bits.
110312652Sandreas.sandberg@arm.com     */
110412652Sandreas.sandberg@arm.com    uint64_t getUintX(ByteOrder endian) const;
110512652Sandreas.sandberg@arm.com
110612652Sandreas.sandberg@arm.com    /**
110712652Sandreas.sandberg@arm.com     * Set the value in the word w after truncating it to the length
110812652Sandreas.sandberg@arm.com     * of the packet and then byteswapping it to the desired
110912652Sandreas.sandberg@arm.com     * endianness.
111012652Sandreas.sandberg@arm.com     */
111112652Sandreas.sandberg@arm.com    void setUintX(uint64_t w, ByteOrder endian);
111212652Sandreas.sandberg@arm.com
11133348Sbinkertn@umich.edu    /**
11144626Sstever@eecs.umich.edu     * Copy data into the packet from the provided pointer.
11154626Sstever@eecs.umich.edu     */
11165735Snate@binkert.org    void
111710563Sandreas.hansson@arm.com    setData(const uint8_t *p)
11184626Sstever@eecs.umich.edu    {
111910571Sandreas.hansson@arm.com        // we should never be copying data onto itself, which means we
112010571Sandreas.hansson@arm.com        // must idenfity packets with static data, as they carry the
112110571Sandreas.hansson@arm.com        // same pointer from source to destination and back
112210571Sandreas.hansson@arm.com        assert(p != getPtr<uint8_t>() || flags.isSet(STATIC_DATA));
112310571Sandreas.hansson@arm.com
11247691SAli.Saidi@ARM.com        if (p != getPtr<uint8_t>())
112510571Sandreas.hansson@arm.com            // for packet with allocated dynamic data, we copy data from
112610571Sandreas.hansson@arm.com            // one to the other, e.g. a forwarded response to a response
11277691SAli.Saidi@ARM.com            std::memcpy(getPtr<uint8_t>(), p, getSize());
11284626Sstever@eecs.umich.edu    }
11294626Sstever@eecs.umich.edu
11304626Sstever@eecs.umich.edu    /**
11314626Sstever@eecs.umich.edu     * Copy data into the packet from the provided block pointer,
11324626Sstever@eecs.umich.edu     * which is aligned to the given block size.
11334626Sstever@eecs.umich.edu     */
11345735Snate@binkert.org    void
113510563Sandreas.hansson@arm.com    setDataFromBlock(const uint8_t *blk_data, int blkSize)
11364626Sstever@eecs.umich.edu    {
11374626Sstever@eecs.umich.edu        setData(blk_data + getOffset(blkSize));
11384626Sstever@eecs.umich.edu    }
11394626Sstever@eecs.umich.edu
11404626Sstever@eecs.umich.edu    /**
114112633Sodanrc@yahoo.com.br     * Copy data from the packet to the memory at the provided pointer.
114212633Sodanrc@yahoo.com.br     * @param p Pointer to which data will be copied.
11434626Sstever@eecs.umich.edu     */
11445735Snate@binkert.org    void
114510563Sandreas.hansson@arm.com    writeData(uint8_t *p) const
11464626Sstever@eecs.umich.edu    {
114710563Sandreas.hansson@arm.com        std::memcpy(p, getConstPtr<uint8_t>(), getSize());
11484626Sstever@eecs.umich.edu    }
11494626Sstever@eecs.umich.edu
11504626Sstever@eecs.umich.edu    /**
115112633Sodanrc@yahoo.com.br     * Copy data from the packet to the provided block pointer, which
115212633Sodanrc@yahoo.com.br     * is aligned to the given block size.
115312633Sodanrc@yahoo.com.br     * @param blk_data Pointer to block to which data will be copied.
115412633Sodanrc@yahoo.com.br     * @param blkSize Block size in bytes.
11554626Sstever@eecs.umich.edu     */
11565735Snate@binkert.org    void
115710563Sandreas.hansson@arm.com    writeDataToBlock(uint8_t *blk_data, int blkSize) const
11584626Sstever@eecs.umich.edu    {
11594626Sstever@eecs.umich.edu        writeData(blk_data + getOffset(blkSize));
11604626Sstever@eecs.umich.edu    }
11614626Sstever@eecs.umich.edu
11624626Sstever@eecs.umich.edu    /**
11633348Sbinkertn@umich.edu     * delete the data pointed to in the data pointer. Ok to call to
11643348Sbinkertn@umich.edu     * matter how data was allocted.
11653348Sbinkertn@umich.edu     */
11665735Snate@binkert.org    void
11675735Snate@binkert.org    deleteData()
11685735Snate@binkert.org    {
116910566Sandreas.hansson@arm.com        if (flags.isSet(DYNAMIC_DATA))
11705735Snate@binkert.org            delete [] data;
11715735Snate@binkert.org
117210566Sandreas.hansson@arm.com        flags.clear(STATIC_DATA|DYNAMIC_DATA);
11735735Snate@binkert.org        data = NULL;
11745735Snate@binkert.org    }
11752566SN/A
117610565Sandreas.hansson@arm.com    /** Allocate memory for the packet. */
11775735Snate@binkert.org    void
11785735Snate@binkert.org    allocate()
11795735Snate@binkert.org    {
118011286Sandreas.hansson@arm.com        // if either this command or the response command has a data
118111286Sandreas.hansson@arm.com        // payload, actually allocate space
118211286Sandreas.hansson@arm.com        if (hasData() || hasRespData()) {
118311286Sandreas.hansson@arm.com            assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
118411286Sandreas.hansson@arm.com            flags.set(DYNAMIC_DATA);
118511286Sandreas.hansson@arm.com            data = new uint8_t[getSize()];
118611286Sandreas.hansson@arm.com        }
11875735Snate@binkert.org    }
11885735Snate@binkert.org
118911013Sandreas.sandberg@arm.com    /** @} */
119011013Sandreas.sandberg@arm.com
119111013Sandreas.sandberg@arm.com    /** Get the data in the packet without byte swapping. */
119211013Sandreas.sandberg@arm.com    template <typename T>
119311013Sandreas.sandberg@arm.com    T getRaw() const;
119411013Sandreas.sandberg@arm.com
119511013Sandreas.sandberg@arm.com    /** Set the value in the data pointer to v without byte swapping. */
119611013Sandreas.sandberg@arm.com    template <typename T>
119711013Sandreas.sandberg@arm.com    void setRaw(T v);
119811013Sandreas.sandberg@arm.com
119911013Sandreas.sandberg@arm.com  public:
12004626Sstever@eecs.umich.edu    /**
120110570Sandreas.hansson@arm.com     * Check a functional request against a memory value stored in
120210570Sandreas.hansson@arm.com     * another packet (i.e. an in-transit request or
120310570Sandreas.hansson@arm.com     * response). Returns true if the current packet is a read, and
120410570Sandreas.hansson@arm.com     * the other packet provides the data, which is then copied to the
120510570Sandreas.hansson@arm.com     * current packet. If the current packet is a write, and the other
120610570Sandreas.hansson@arm.com     * packet intersects this one, then we update the data
120710570Sandreas.hansson@arm.com     * accordingly.
120810570Sandreas.hansson@arm.com     */
120910570Sandreas.hansson@arm.com    bool
121012823Srmk35@cl.cam.ac.uk    trySatisfyFunctional(PacketPtr other)
121110570Sandreas.hansson@arm.com    {
121210570Sandreas.hansson@arm.com        // all packets that are carrying a payload should have a valid
121310570Sandreas.hansson@arm.com        // data pointer
121412823Srmk35@cl.cam.ac.uk        return trySatisfyFunctional(other, other->getAddr(), other->isSecure(),
121512823Srmk35@cl.cam.ac.uk                                    other->getSize(),
121612823Srmk35@cl.cam.ac.uk                                    other->hasData() ?
121712823Srmk35@cl.cam.ac.uk                                    other->getPtr<uint8_t>() : NULL);
121810570Sandreas.hansson@arm.com    }
121910570Sandreas.hansson@arm.com
122010570Sandreas.hansson@arm.com    /**
122110883Sali.jafri@arm.com     * Does the request need to check for cached copies of the same block
122210883Sali.jafri@arm.com     * in the memory hierarchy above.
122310883Sali.jafri@arm.com     **/
122410883Sali.jafri@arm.com    bool
122510883Sali.jafri@arm.com    mustCheckAbove() const
122610883Sali.jafri@arm.com    {
122711199Sandreas.hansson@arm.com        return cmd == MemCmd::HardPFReq || isEviction();
122811199Sandreas.hansson@arm.com    }
122911199Sandreas.hansson@arm.com
123011199Sandreas.hansson@arm.com    /**
123111199Sandreas.hansson@arm.com     * Is this packet a clean eviction, including both actual clean
123211199Sandreas.hansson@arm.com     * evict packets, but also clean writebacks.
123311199Sandreas.hansson@arm.com     */
123411199Sandreas.hansson@arm.com    bool
123511199Sandreas.hansson@arm.com    isCleanEviction() const
123611199Sandreas.hansson@arm.com    {
123711199Sandreas.hansson@arm.com        return cmd == MemCmd::CleanEvict || cmd == MemCmd::WritebackClean;
123810883Sali.jafri@arm.com    }
123910883Sali.jafri@arm.com
124010883Sali.jafri@arm.com    /**
12414626Sstever@eecs.umich.edu     * Check a functional request against a memory value represented
124210570Sandreas.hansson@arm.com     * by a base/size pair and an associated data array. If the
124310570Sandreas.hansson@arm.com     * current packet is a read, it may be satisfied by the memory
124410570Sandreas.hansson@arm.com     * value. If the current packet is a write, it may update the
12454626Sstever@eecs.umich.edu     * memory value.
12464626Sstever@eecs.umich.edu     */
12475735Snate@binkert.org    bool
124812823Srmk35@cl.cam.ac.uk    trySatisfyFunctional(Printable *obj, Addr base, bool is_secure, int size,
124912823Srmk35@cl.cam.ac.uk                         uint8_t *_data);
12505314Sstever@gmail.com
12515315Sstever@gmail.com    /**
12525315Sstever@gmail.com     * Push label for PrintReq (safe to call unconditionally).
12535315Sstever@gmail.com     */
12545735Snate@binkert.org    void
12555735Snate@binkert.org    pushLabel(const std::string &lbl)
12565735Snate@binkert.org    {
12575735Snate@binkert.org        if (isPrint())
12585735Snate@binkert.org            safe_cast<PrintReqState*>(senderState)->pushLabel(lbl);
12595314Sstever@gmail.com    }
12605314Sstever@gmail.com
12615315Sstever@gmail.com    /**
12625315Sstever@gmail.com     * Pop label for PrintReq (safe to call unconditionally).
12635315Sstever@gmail.com     */
12645735Snate@binkert.org    void
12655735Snate@binkert.org    popLabel()
12665735Snate@binkert.org    {
12675735Snate@binkert.org        if (isPrint())
12685735Snate@binkert.org            safe_cast<PrintReqState*>(senderState)->popLabel();
12695314Sstever@gmail.com    }
12705314Sstever@gmail.com
12715314Sstever@gmail.com    void print(std::ostream &o, int verbosity = 0,
12725314Sstever@gmail.com               const std::string &prefix = "") const;
12739663Suri.wiener@arm.com
12749663Suri.wiener@arm.com    /**
12759663Suri.wiener@arm.com     * A no-args wrapper of print(std::ostream...)
12769663Suri.wiener@arm.com     * meant to be invoked from DPRINTFs
12779663Suri.wiener@arm.com     * avoiding string overheads in fast mode
12789663Suri.wiener@arm.com     * @return string with the request's type and start<->end addresses
12799663Suri.wiener@arm.com     */
12809663Suri.wiener@arm.com    std::string print() const;
12812381SN/A};
12822381SN/A
12832381SN/A#endif //__MEM_PACKET_HH
1284