12381SN/A/*
213732Snikos.nikoleris@arm.com * Copyright (c) 2012-2019 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
6013856Sodanrc@yahoo.com.br#include "base/addr_range.hh"
615735Snate@binkert.org#include "base/cast.hh"
624024Sbinkertn@umich.edu#include "base/compiler.hh"
635735Snate@binkert.org#include "base/flags.hh"
6412334Sgabeblack@google.com#include "base/logging.hh"
655314Sstever@gmail.com#include "base/printable.hh"
666216Snate@binkert.org#include "base/types.hh"
6713347Sgabeblack@google.com#include "config/the_isa.hh"
682392SN/A#include "mem/request.hh"
694167Sbinkertn@umich.edu#include "sim/core.hh"
702394SN/A
718737Skoansin.tan@gmail.comclass Packet;
723349Sbinkertn@umich.edutypedef Packet *PacketPtr;
732394SN/Atypedef uint8_t* PacketDataPtr;
742812Srdreslin@umich.edutypedef std::list<PacketPtr> PacketList;
7512351Snikos.nikoleris@arm.comtypedef uint64_t PacketId;
762812Srdreslin@umich.edu
774022Sstever@eecs.umich.educlass MemCmd
784022Sstever@eecs.umich.edu{
795735Snate@binkert.org    friend class Packet;
805735Snate@binkert.org
814022Sstever@eecs.umich.edu  public:
825735Snate@binkert.org    /**
835735Snate@binkert.org     * List of all commands associated with a packet.
845735Snate@binkert.org     */
854022Sstever@eecs.umich.edu    enum Command
864022Sstever@eecs.umich.edu    {
874022Sstever@eecs.umich.edu        InvalidCmd,
884022Sstever@eecs.umich.edu        ReadReq,
894473Sstever@eecs.umich.edu        ReadResp,
905319Sstever@gmail.com        ReadRespWithInvalidate,
914022Sstever@eecs.umich.edu        WriteReq,
924022Sstever@eecs.umich.edu        WriteResp,
9311199Sandreas.hansson@arm.com        WritebackDirty,
9411199Sandreas.hansson@arm.com        WritebackClean,
9512344Snikos.nikoleris@arm.com        WriteClean,            // writes dirty data below without evicting
9610883Sali.jafri@arm.com        CleanEvict,
974022Sstever@eecs.umich.edu        SoftPFReq,
9813367Syuetsu.kodama@riken.jp        SoftPFExReq,
994022Sstever@eecs.umich.edu        HardPFReq,
1004022Sstever@eecs.umich.edu        SoftPFResp,
1014022Sstever@eecs.umich.edu        HardPFResp,
10210886Sandreas.hansson@arm.com        WriteLineReq,
1034022Sstever@eecs.umich.edu        UpgradeReq,
1047465Ssteve.reinhardt@amd.com        SCUpgradeReq,           // Special "weak" upgrade for StoreCond
1054628Sstever@eecs.umich.edu        UpgradeResp,
1067465Ssteve.reinhardt@amd.com        SCUpgradeFailReq,       // Failed SCUpgradeReq in MSHR (never sent)
1077465Ssteve.reinhardt@amd.com        UpgradeFailResp,        // Valid for SCUpgradeReq only
1084022Sstever@eecs.umich.edu        ReadExReq,
1094022Sstever@eecs.umich.edu        ReadExResp,
11010885Sandreas.hansson@arm.com        ReadCleanReq,
11110885Sandreas.hansson@arm.com        ReadSharedReq,
1124626Sstever@eecs.umich.edu        LoadLockedReq,
1134626Sstever@eecs.umich.edu        StoreCondReq,
1147669Ssteve.reinhardt@amd.com        StoreCondFailReq,       // Failed StoreCondReq in MSHR (never sent)
1154626Sstever@eecs.umich.edu        StoreCondResp,
1164040Ssaidi@eecs.umich.edu        SwapReq,
1174040Ssaidi@eecs.umich.edu        SwapResp,
1185650Sgblack@eecs.umich.edu        MessageReq,
1195650Sgblack@eecs.umich.edu        MessageResp,
12011256Santhony.gutierrez@amd.com        MemFenceReq,
12111256Santhony.gutierrez@amd.com        MemFenceResp,
12212347Snikos.nikoleris@arm.com        CleanSharedReq,
12312347Snikos.nikoleris@arm.com        CleanSharedResp,
12412347Snikos.nikoleris@arm.com        CleanInvalidReq,
12512347Snikos.nikoleris@arm.com        CleanInvalidResp,
1264870Sstever@eecs.umich.edu        // Error responses
1274870Sstever@eecs.umich.edu        // @TODO these should be classified as responses rather than
1284870Sstever@eecs.umich.edu        // requests; coding them as requests initially for backwards
1294870Sstever@eecs.umich.edu        // compatibility
1304870Sstever@eecs.umich.edu        InvalidDestError,  // packet dest field invalid
1314870Sstever@eecs.umich.edu        BadAddressError,   // memory address invalid
1328436SBrad.Beckmann@amd.com        FunctionalReadError, // unable to fulfill functional read
1338436SBrad.Beckmann@amd.com        FunctionalWriteError, // unable to fulfill functional write
1345314Sstever@gmail.com        // Fake simulator-only commands
1355314Sstever@gmail.com        PrintReq,       // Print state matching address
1368184Ssomayeh@cs.wisc.edu        FlushReq,      //request for a cache flush
13710886Sandreas.hansson@arm.com        InvalidateReq,   // request for address to be invalidated
13810886Sandreas.hansson@arm.com        InvalidateResp,
1394022Sstever@eecs.umich.edu        NUM_MEM_CMDS
1404022Sstever@eecs.umich.edu    };
1414022Sstever@eecs.umich.edu
1424022Sstever@eecs.umich.edu  private:
1435735Snate@binkert.org    /**
1445735Snate@binkert.org     * List of command attributes.
1455735Snate@binkert.org     */
1464022Sstever@eecs.umich.edu    enum Attribute
1474022Sstever@eecs.umich.edu    {
1484626Sstever@eecs.umich.edu        IsRead,         //!< Data flows from responder to requester
1494626Sstever@eecs.umich.edu        IsWrite,        //!< Data flows from requester to responder
1507465Ssteve.reinhardt@amd.com        IsUpgrade,
1514022Sstever@eecs.umich.edu        IsInvalidate,
15212347Snikos.nikoleris@arm.com        IsClean,        //!< Cleans any existing dirty blocks
15311284Sandreas.hansson@arm.com        NeedsWritable,  //!< Requires writable copy to complete in-cache
1544626Sstever@eecs.umich.edu        IsRequest,      //!< Issued by requester
1554626Sstever@eecs.umich.edu        IsResponse,     //!< Issue by responder
1564626Sstever@eecs.umich.edu        NeedsResponse,  //!< Requester needs response from target
15711199Sandreas.hansson@arm.com        IsEviction,
1584022Sstever@eecs.umich.edu        IsSWPrefetch,
1594022Sstever@eecs.umich.edu        IsHWPrefetch,
1606076Sgblack@eecs.umich.edu        IsLlsc,         //!< Alpha/MIPS LL or SC access
1614626Sstever@eecs.umich.edu        HasData,        //!< There is an associated payload
1624870Sstever@eecs.umich.edu        IsError,        //!< Error response
1635314Sstever@gmail.com        IsPrint,        //!< Print state matching address (for debugging)
1648184Ssomayeh@cs.wisc.edu        IsFlush,        //!< Flush the address from caches
16511600Sandreas.hansson@arm.com        FromCache,      //!< Request originated from a caching agent
1664022Sstever@eecs.umich.edu        NUM_COMMAND_ATTRIBUTES
1674022Sstever@eecs.umich.edu    };
1684022Sstever@eecs.umich.edu
1695735Snate@binkert.org    /**
1705735Snate@binkert.org     * Structure that defines attributes and other data associated
1715735Snate@binkert.org     * with a Command.
1725735Snate@binkert.org     */
1735735Snate@binkert.org    struct CommandInfo
1745735Snate@binkert.org    {
1755735Snate@binkert.org        /// Set of attribute flags.
1764022Sstever@eecs.umich.edu        const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
1775735Snate@binkert.org        /// Corresponding response for requests; InvalidCmd if no
1785735Snate@binkert.org        /// response is applicable.
1794022Sstever@eecs.umich.edu        const Command response;
1805735Snate@binkert.org        /// String representation (for printing)
1814022Sstever@eecs.umich.edu        const std::string str;
1824022Sstever@eecs.umich.edu    };
1834022Sstever@eecs.umich.edu
1845735Snate@binkert.org    /// Array to map Command enum to associated info.
1854022Sstever@eecs.umich.edu    static const CommandInfo commandInfo[];
1864022Sstever@eecs.umich.edu
1874022Sstever@eecs.umich.edu  private:
1884022Sstever@eecs.umich.edu
1894022Sstever@eecs.umich.edu    Command cmd;
1904022Sstever@eecs.umich.edu
1915735Snate@binkert.org    bool
1925735Snate@binkert.org    testCmdAttrib(MemCmd::Attribute attrib) const
1935735Snate@binkert.org    {
1944022Sstever@eecs.umich.edu        return commandInfo[cmd].attributes[attrib] != 0;
1954022Sstever@eecs.umich.edu    }
1964022Sstever@eecs.umich.edu
1974022Sstever@eecs.umich.edu  public:
1984022Sstever@eecs.umich.edu
19910583SCurtis.Dunham@arm.com    bool isRead() const            { return testCmdAttrib(IsRead); }
20010583SCurtis.Dunham@arm.com    bool isWrite() const           { return testCmdAttrib(IsWrite); }
20110583SCurtis.Dunham@arm.com    bool isUpgrade() const         { return testCmdAttrib(IsUpgrade); }
20210583SCurtis.Dunham@arm.com    bool isRequest() const         { return testCmdAttrib(IsRequest); }
20310583SCurtis.Dunham@arm.com    bool isResponse() const        { return testCmdAttrib(IsResponse); }
20411284Sandreas.hansson@arm.com    bool needsWritable() const     { return testCmdAttrib(NeedsWritable); }
20510583SCurtis.Dunham@arm.com    bool needsResponse() const     { return testCmdAttrib(NeedsResponse); }
20610583SCurtis.Dunham@arm.com    bool isInvalidate() const      { return testCmdAttrib(IsInvalidate); }
20711199Sandreas.hansson@arm.com    bool isEviction() const        { return testCmdAttrib(IsEviction); }
20812347Snikos.nikoleris@arm.com    bool isClean() const           { return testCmdAttrib(IsClean); }
20911600Sandreas.hansson@arm.com    bool fromCache() const         { return testCmdAttrib(FromCache); }
21011199Sandreas.hansson@arm.com
21111199Sandreas.hansson@arm.com    /**
21211199Sandreas.hansson@arm.com     * A writeback is an eviction that carries data.
21311199Sandreas.hansson@arm.com     */
21411199Sandreas.hansson@arm.com    bool isWriteback() const       { return testCmdAttrib(IsEviction) &&
21511199Sandreas.hansson@arm.com                                            testCmdAttrib(HasData); }
21610570Sandreas.hansson@arm.com
21710570Sandreas.hansson@arm.com    /**
21810570Sandreas.hansson@arm.com     * Check if this particular packet type carries payload data. Note
21910570Sandreas.hansson@arm.com     * that this does not reflect if the data pointer of the packet is
22010570Sandreas.hansson@arm.com     * valid or not.
22110570Sandreas.hansson@arm.com     */
2224022Sstever@eecs.umich.edu    bool hasData() const        { return testCmdAttrib(HasData); }
2236102Sgblack@eecs.umich.edu    bool isLLSC() const         { return testCmdAttrib(IsLlsc); }
22410343SCurtis.Dunham@arm.com    bool isSWPrefetch() const   { return testCmdAttrib(IsSWPrefetch); }
22510343SCurtis.Dunham@arm.com    bool isHWPrefetch() const   { return testCmdAttrib(IsHWPrefetch); }
22610343SCurtis.Dunham@arm.com    bool isPrefetch() const     { return testCmdAttrib(IsSWPrefetch) ||
22710343SCurtis.Dunham@arm.com                                         testCmdAttrib(IsHWPrefetch); }
2284870Sstever@eecs.umich.edu    bool isError() const        { return testCmdAttrib(IsError); }
2295314Sstever@gmail.com    bool isPrint() const        { return testCmdAttrib(IsPrint); }
2308184Ssomayeh@cs.wisc.edu    bool isFlush() const        { return testCmdAttrib(IsFlush); }
2314022Sstever@eecs.umich.edu
23211294Sandreas.hansson@arm.com    Command
2335735Snate@binkert.org    responseCommand() const
2345735Snate@binkert.org    {
2354022Sstever@eecs.umich.edu        return commandInfo[cmd].response;
2364022Sstever@eecs.umich.edu    }
2374022Sstever@eecs.umich.edu
2385735Snate@binkert.org    /// Return the string to a cmd given by idx.
2395735Snate@binkert.org    const std::string &toString() const { return commandInfo[cmd].str; }
2404022Sstever@eecs.umich.edu    int toInt() const { return (int)cmd; }
2414022Sstever@eecs.umich.edu
2425735Snate@binkert.org    MemCmd(Command _cmd) : cmd(_cmd) { }
2435735Snate@binkert.org    MemCmd(int _cmd) : cmd((Command)_cmd) { }
2445735Snate@binkert.org    MemCmd() : cmd(InvalidCmd) { }
2454022Sstever@eecs.umich.edu
2465735Snate@binkert.org    bool operator==(MemCmd c2) const { return (cmd == c2.cmd); }
2475735Snate@binkert.org    bool operator!=(MemCmd c2) const { return (cmd != c2.cmd); }
2484022Sstever@eecs.umich.edu};
2494022Sstever@eecs.umich.edu
2502381SN/A/**
2512662Sstever@eecs.umich.edu * A Packet is used to encapsulate a transfer between two objects in
2522662Sstever@eecs.umich.edu * the memory system (e.g., the L1 and L2 cache).  (In contrast, a
2532662Sstever@eecs.umich.edu * single Request travels all the way from the requester to the
2542662Sstever@eecs.umich.edu * ultimate destination and back, possibly being conveyed by several
2552662Sstever@eecs.umich.edu * different Packets along the way.)
2562381SN/A */
2579044SAli.Saidi@ARM.comclass Packet : public Printable
2582381SN/A{
2592813Srdreslin@umich.edu  public:
2605735Snate@binkert.org    typedef uint32_t FlagsType;
2615735Snate@binkert.org    typedef ::Flags<FlagsType> Flags;
2624022Sstever@eecs.umich.edu
2635735Snate@binkert.org  private:
2645735Snate@binkert.org
26510938Sandreas.hansson@arm.com    enum : FlagsType {
26610938Sandreas.hansson@arm.com        // Flags to transfer across when copying a packet
26712349Snikos.nikoleris@arm.com        COPY_FLAGS             = 0x0000003F,
26810938Sandreas.hansson@arm.com
26913732Snikos.nikoleris@arm.com        // Flags that are used to create reponse packets
27013732Snikos.nikoleris@arm.com        RESPONDER_FLAGS        = 0x00000009,
27113732Snikos.nikoleris@arm.com
27211284Sandreas.hansson@arm.com        // Does this packet have sharers (which means it should not be
27311284Sandreas.hansson@arm.com        // considered writable) or not. See setHasSharers below.
27411284Sandreas.hansson@arm.com        HAS_SHARERS            = 0x00000001,
27511284Sandreas.hansson@arm.com
27610938Sandreas.hansson@arm.com        // Special control flags
27710938Sandreas.hansson@arm.com        /// Special timing-mode atomic snoop for multi-level coherence.
27810938Sandreas.hansson@arm.com        EXPRESS_SNOOP          = 0x00000002,
27911284Sandreas.hansson@arm.com
28011284Sandreas.hansson@arm.com        /// Allow a responding cache to inform the cache hierarchy
28111284Sandreas.hansson@arm.com        /// that it had a writable copy before responding. See
28211284Sandreas.hansson@arm.com        /// setResponderHadWritable below.
28311284Sandreas.hansson@arm.com        RESPONDER_HAD_WRITABLE = 0x00000004,
28411284Sandreas.hansson@arm.com
28511284Sandreas.hansson@arm.com        // Snoop co-ordination flag to indicate that a cache is
28611284Sandreas.hansson@arm.com        // responding to a snoop. See setCacheResponding below.
28711284Sandreas.hansson@arm.com        CACHE_RESPONDING       = 0x00000008,
28810938Sandreas.hansson@arm.com
28912346Snikos.nikoleris@arm.com        // The writeback/writeclean should be propagated further
29012346Snikos.nikoleris@arm.com        // downstream by the receiver
29112346Snikos.nikoleris@arm.com        WRITE_THROUGH          = 0x00000010,
29212346Snikos.nikoleris@arm.com
29312349Snikos.nikoleris@arm.com        // Response co-ordination flag for cache maintenance
29412349Snikos.nikoleris@arm.com        // operations
29512349Snikos.nikoleris@arm.com        SATISFIED              = 0x00000020,
29612349Snikos.nikoleris@arm.com
29711057Sandreas.hansson@arm.com        /// Are the 'addr' and 'size' fields valid?
29811057Sandreas.hansson@arm.com        VALID_ADDR             = 0x00000100,
29911057Sandreas.hansson@arm.com        VALID_SIZE             = 0x00000200,
30011057Sandreas.hansson@arm.com
30110938Sandreas.hansson@arm.com        /// Is the data pointer set to a value that shouldn't be freed
30210938Sandreas.hansson@arm.com        /// when the packet is destroyed?
30310938Sandreas.hansson@arm.com        STATIC_DATA            = 0x00001000,
30410938Sandreas.hansson@arm.com        /// The data pointer points to a value that should be freed when
30510938Sandreas.hansson@arm.com        /// the packet is destroyed. The pointer is assumed to be pointing
30610938Sandreas.hansson@arm.com        /// to an array, and delete [] is consequently called
30710938Sandreas.hansson@arm.com        DYNAMIC_DATA           = 0x00002000,
30810938Sandreas.hansson@arm.com
30910938Sandreas.hansson@arm.com        /// suppress the error if this packet encounters a functional
31010938Sandreas.hansson@arm.com        /// access failure.
31110938Sandreas.hansson@arm.com        SUPPRESS_FUNC_ERROR    = 0x00008000,
31210938Sandreas.hansson@arm.com
31310938Sandreas.hansson@arm.com        // Signal block present to squash prefetch and cache evict packets
31410938Sandreas.hansson@arm.com        // through express snoop flag
31510938Sandreas.hansson@arm.com        BLOCK_CACHED          = 0x00010000
31610938Sandreas.hansson@arm.com    };
3175735Snate@binkert.org
3185735Snate@binkert.org    Flags flags;
3195735Snate@binkert.org
3205735Snate@binkert.org  public:
3214022Sstever@eecs.umich.edu    typedef MemCmd::Command Command;
3224022Sstever@eecs.umich.edu
3235735Snate@binkert.org    /// The command field of the packet.
3244870Sstever@eecs.umich.edu    MemCmd cmd;
3254870Sstever@eecs.umich.edu
32612351Snikos.nikoleris@arm.com    const PacketId id;
32712351Snikos.nikoleris@arm.com
3285735Snate@binkert.org    /// A pointer to the original request.
32912749Sgiacomo.travaglini@arm.com    RequestPtr req;
3304870Sstever@eecs.umich.edu
3312566SN/A  private:
3325735Snate@binkert.org   /**
33312633Sodanrc@yahoo.com.br    * A pointer to the data being transferred. It can be different
33412633Sodanrc@yahoo.com.br    * sizes at each level of the hierarchy so it belongs to the
3355735Snate@binkert.org    * packet, not request. This may or may not be populated when a
33612633Sodanrc@yahoo.com.br    * responder receives the packet. If not populated memory should
3375735Snate@binkert.org    * be allocated.
3382566SN/A    */
3392566SN/A    PacketDataPtr data;
3402566SN/A
3415735Snate@binkert.org    /// The address of the request.  This address could be virtual or
3425735Snate@binkert.org    /// physical, depending on the system configuration.
3432381SN/A    Addr addr;
3442381SN/A
34510028SGiacomo.Gabrielli@arm.com    /// True if the request targets the secure memory space.
34610028SGiacomo.Gabrielli@arm.com    bool _isSecure;
34710028SGiacomo.Gabrielli@arm.com
3485735Snate@binkert.org    /// The size of the request or transfer.
3496227Snate@binkert.org    unsigned size;
3502381SN/A
3515735Snate@binkert.org    /**
35210723Sandreas.hansson@arm.com     * Track the bytes found that satisfy a functional read.
3538668Sgeoffrey.blake@arm.com     */
35410723Sandreas.hansson@arm.com    std::vector<bool> bytesValid;
3558668Sgeoffrey.blake@arm.com
35612966SMatteo.Andreozzi@arm.com    // Quality of Service priority value
35712966SMatteo.Andreozzi@arm.com    uint8_t _qosValue;
35812966SMatteo.Andreozzi@arm.com
3592641Sstever@eecs.umich.edu  public:
3602811Srdreslin@umich.edu
3619547Sandreas.hansson@arm.com    /**
36210694SMarco.Balboni@ARM.com     * The extra delay from seeing the packet until the header is
36310405Sandreas.hansson@arm.com     * transmitted. This delay is used to communicate the crossbar
36410405Sandreas.hansson@arm.com     * forwarding latency to the neighbouring object (e.g. a cache)
36510405Sandreas.hansson@arm.com     * that actually makes the packet wait. As the delay is relative,
36610405Sandreas.hansson@arm.com     * a 32-bit unsigned should be sufficient.
3679547Sandreas.hansson@arm.com     */
36810694SMarco.Balboni@ARM.com    uint32_t headerDelay;
3693218Sgblack@eecs.umich.edu
3709547Sandreas.hansson@arm.com    /**
37111127Sandreas.hansson@arm.com     * Keep track of the extra delay incurred by snooping upwards
37211127Sandreas.hansson@arm.com     * before sending a request down the memory system. This is used
37311127Sandreas.hansson@arm.com     * by the coherent crossbar to account for the additional request
37411127Sandreas.hansson@arm.com     * delay.
37511127Sandreas.hansson@arm.com     */
37611127Sandreas.hansson@arm.com    uint32_t snoopDelay;
37711127Sandreas.hansson@arm.com
37811127Sandreas.hansson@arm.com    /**
37910694SMarco.Balboni@ARM.com     * The extra pipelining delay from seeing the packet until the end of
38010694SMarco.Balboni@ARM.com     * payload is transmitted by the component that provided it (if
38110694SMarco.Balboni@ARM.com     * any). This includes the header delay. Similar to the header
38210694SMarco.Balboni@ARM.com     * delay, this is used to make up for the fact that the
38310405Sandreas.hansson@arm.com     * crossbar does not make the packet wait. As the delay is
38410405Sandreas.hansson@arm.com     * relative, a 32-bit unsigned should be sufficient.
3859547Sandreas.hansson@arm.com     */
38610694SMarco.Balboni@ARM.com    uint32_t payloadDelay;
3873218Sgblack@eecs.umich.edu
3885735Snate@binkert.org    /**
3895735Snate@binkert.org     * A virtual base opaque structure used to hold state associated
39013892Sgabeblack@google.com     * with the packet (e.g., an MSHR), specific to a SimObject that
3919542Sandreas.hansson@arm.com     * sees the packet. A pointer to this state is returned in the
39213892Sgabeblack@google.com     * packet's response so that the SimObject in question can quickly
3939542Sandreas.hansson@arm.com     * look up the state needed to process it. A specific subclass
3949542Sandreas.hansson@arm.com     * would be derived from this to carry state specific to a
3959542Sandreas.hansson@arm.com     * particular sending device.
3969542Sandreas.hansson@arm.com     *
39713892Sgabeblack@google.com     * As multiple SimObjects may add their SenderState throughout the
3989542Sandreas.hansson@arm.com     * memory system, the SenderStates create a stack, where a
39913892Sgabeblack@google.com     * SimObject can add a new Senderstate, as long as the
4009542Sandreas.hansson@arm.com     * predecessing SenderState is restored when the response comes
4019542Sandreas.hansson@arm.com     * back. For this reason, the predecessor should always be
4029542Sandreas.hansson@arm.com     * populated with the current SenderState of a packet before
4039542Sandreas.hansson@arm.com     * modifying the senderState field in the request packet.
4045735Snate@binkert.org     */
4055735Snate@binkert.org    struct SenderState
4065735Snate@binkert.org    {
4079542Sandreas.hansson@arm.com        SenderState* predecessor;
4089542Sandreas.hansson@arm.com        SenderState() : predecessor(NULL) {}
4092641Sstever@eecs.umich.edu        virtual ~SenderState() {}
4102641Sstever@eecs.umich.edu    };
4112641Sstever@eecs.umich.edu
4125315Sstever@gmail.com    /**
4135315Sstever@gmail.com     * Object used to maintain state of a PrintReq.  The senderState
4145315Sstever@gmail.com     * field of a PrintReq should always be of this type.
4155315Sstever@gmail.com     */
4169044SAli.Saidi@ARM.com    class PrintReqState : public SenderState
4175735Snate@binkert.org    {
4185735Snate@binkert.org      private:
4195735Snate@binkert.org        /**
4205735Snate@binkert.org         * An entry in the label stack.
4215735Snate@binkert.org         */
4225735Snate@binkert.org        struct LabelStackEntry
4235735Snate@binkert.org        {
4245314Sstever@gmail.com            const std::string label;
4255314Sstever@gmail.com            std::string *prefix;
4265314Sstever@gmail.com            bool labelPrinted;
4275735Snate@binkert.org            LabelStackEntry(const std::string &_label, std::string *_prefix);
4285314Sstever@gmail.com        };
4295314Sstever@gmail.com
4305314Sstever@gmail.com        typedef std::list<LabelStackEntry> LabelStack;
4315314Sstever@gmail.com        LabelStack labelStack;
4325314Sstever@gmail.com
4335314Sstever@gmail.com        std::string *curPrefixPtr;
4345314Sstever@gmail.com
4355314Sstever@gmail.com      public:
4365314Sstever@gmail.com        std::ostream &os;
4375314Sstever@gmail.com        const int verbosity;
4385314Sstever@gmail.com
4395314Sstever@gmail.com        PrintReqState(std::ostream &os, int verbosity = 0);
4405314Sstever@gmail.com        ~PrintReqState();
4415314Sstever@gmail.com
4425735Snate@binkert.org        /**
4435735Snate@binkert.org         * Returns the current line prefix.
4445735Snate@binkert.org         */
4455314Sstever@gmail.com        const std::string &curPrefix() { return *curPrefixPtr; }
4465315Sstever@gmail.com
4475735Snate@binkert.org        /**
4485735Snate@binkert.org         * Push a label onto the label stack, and prepend the given
4495315Sstever@gmail.com         * prefix string onto the current prefix.  Labels will only be
4505735Snate@binkert.org         * printed if an object within the label's scope is printed.
4515735Snate@binkert.org         */
4525314Sstever@gmail.com        void pushLabel(const std::string &lbl,
4535314Sstever@gmail.com                       const std::string &prefix = "  ");
4545735Snate@binkert.org
4555735Snate@binkert.org        /**
4565735Snate@binkert.org         * Pop a label off the label stack.
4575735Snate@binkert.org         */
4585314Sstever@gmail.com        void popLabel();
4595735Snate@binkert.org
4605735Snate@binkert.org        /**
4615735Snate@binkert.org         * Print all of the pending unprinted labels on the
4625315Sstever@gmail.com         * stack. Called by printObj(), so normally not called by
4635735Snate@binkert.org         * users unless bypassing printObj().
4645735Snate@binkert.org         */
4655314Sstever@gmail.com        void printLabels();
4665735Snate@binkert.org
4675735Snate@binkert.org        /**
4685735Snate@binkert.org         * Print a Printable object to os, because it matched the
4695735Snate@binkert.org         * address on a PrintReq.
4705735Snate@binkert.org         */
4715314Sstever@gmail.com        void printObj(Printable *obj);
4725314Sstever@gmail.com    };
4735314Sstever@gmail.com
4745735Snate@binkert.org    /**
4755735Snate@binkert.org     * This packet's sender state.  Devices should use dynamic_cast<>
4765735Snate@binkert.org     * to cast to the state appropriate to the sender.  The intent of
4775735Snate@binkert.org     * this variable is to allow a device to attach extra information
4789542Sandreas.hansson@arm.com     * to a request. A response packet must return the sender state
4795735Snate@binkert.org     * that was attached to the original request (even if a new packet
4805735Snate@binkert.org     * is created).
4815735Snate@binkert.org     */
4822662Sstever@eecs.umich.edu    SenderState *senderState;
4832641Sstever@eecs.umich.edu
4849542Sandreas.hansson@arm.com    /**
4859542Sandreas.hansson@arm.com     * Push a new sender state to the packet and make the current
4869542Sandreas.hansson@arm.com     * sender state the predecessor of the new one. This should be
4879542Sandreas.hansson@arm.com     * prefered over direct manipulation of the senderState member
4889542Sandreas.hansson@arm.com     * variable.
4899542Sandreas.hansson@arm.com     *
4909542Sandreas.hansson@arm.com     * @param sender_state SenderState to push at the top of the stack
4919542Sandreas.hansson@arm.com     */
4929542Sandreas.hansson@arm.com    void pushSenderState(SenderState *sender_state);
4939542Sandreas.hansson@arm.com
4949542Sandreas.hansson@arm.com    /**
4959542Sandreas.hansson@arm.com     * Pop the top of the state stack and return a pointer to it. This
4969542Sandreas.hansson@arm.com     * assumes the current sender state is not NULL. This should be
4979542Sandreas.hansson@arm.com     * preferred over direct manipulation of the senderState member
4989542Sandreas.hansson@arm.com     * variable.
4999542Sandreas.hansson@arm.com     *
5009542Sandreas.hansson@arm.com     * @return The current top of the stack
5019542Sandreas.hansson@arm.com     */
5029542Sandreas.hansson@arm.com    SenderState *popSenderState();
5039542Sandreas.hansson@arm.com
5049543Ssascha.bischoff@arm.com    /**
5059543Ssascha.bischoff@arm.com     * Go through the sender state stack and return the first instance
5069543Ssascha.bischoff@arm.com     * that is of type T (as determined by a dynamic_cast). If there
5079543Ssascha.bischoff@arm.com     * is no sender state of type T, NULL is returned.
5089543Ssascha.bischoff@arm.com     *
5099543Ssascha.bischoff@arm.com     * @return The topmost state of type T
5109543Ssascha.bischoff@arm.com     */
5119543Ssascha.bischoff@arm.com    template <typename T>
5129543Ssascha.bischoff@arm.com    T * findNextSenderState() const
5139543Ssascha.bischoff@arm.com    {
5149543Ssascha.bischoff@arm.com        T *t = NULL;
5159543Ssascha.bischoff@arm.com        SenderState* sender_state = senderState;
5169543Ssascha.bischoff@arm.com        while (t == NULL && sender_state != NULL) {
5179543Ssascha.bischoff@arm.com            t = dynamic_cast<T*>(sender_state);
5189543Ssascha.bischoff@arm.com            sender_state = sender_state->predecessor;
5199543Ssascha.bischoff@arm.com        }
5209543Ssascha.bischoff@arm.com        return t;
5219543Ssascha.bischoff@arm.com    }
5229543Ssascha.bischoff@arm.com
5235735Snate@binkert.org    /// Return the string name of the cmd field (for debugging and
5245735Snate@binkert.org    /// tracing).
5254022Sstever@eecs.umich.edu    const std::string &cmdString() const { return cmd.toString(); }
5262811Srdreslin@umich.edu
5275735Snate@binkert.org    /// Return the index of this command.
5284022Sstever@eecs.umich.edu    inline int cmdToIndex() const { return cmd.toInt(); }
5292811Srdreslin@umich.edu
53010583SCurtis.Dunham@arm.com    bool isRead() const              { return cmd.isRead(); }
53110583SCurtis.Dunham@arm.com    bool isWrite() const             { return cmd.isWrite(); }
53210583SCurtis.Dunham@arm.com    bool isUpgrade()  const          { return cmd.isUpgrade(); }
53310583SCurtis.Dunham@arm.com    bool isRequest() const           { return cmd.isRequest(); }
53410583SCurtis.Dunham@arm.com    bool isResponse() const          { return cmd.isResponse(); }
53511287Sandreas.hansson@arm.com    bool needsWritable() const
53611287Sandreas.hansson@arm.com    {
53711287Sandreas.hansson@arm.com        // we should never check if a response needsWritable, the
53811287Sandreas.hansson@arm.com        // request has this flag, and for a response we should rather
53911287Sandreas.hansson@arm.com        // look at the hasSharers flag (if not set, the response is to
54011287Sandreas.hansson@arm.com        // be considered writable)
54111287Sandreas.hansson@arm.com        assert(isRequest());
54211287Sandreas.hansson@arm.com        return cmd.needsWritable();
54311287Sandreas.hansson@arm.com    }
54410583SCurtis.Dunham@arm.com    bool needsResponse() const       { return cmd.needsResponse(); }
54510583SCurtis.Dunham@arm.com    bool isInvalidate() const        { return cmd.isInvalidate(); }
54611199Sandreas.hansson@arm.com    bool isEviction() const          { return cmd.isEviction(); }
54712347Snikos.nikoleris@arm.com    bool isClean() const             { return cmd.isClean(); }
54811600Sandreas.hansson@arm.com    bool fromCache() const           { return cmd.fromCache(); }
54911199Sandreas.hansson@arm.com    bool isWriteback() const         { return cmd.isWriteback(); }
55010583SCurtis.Dunham@arm.com    bool hasData() const             { return cmd.hasData(); }
55111286Sandreas.hansson@arm.com    bool hasRespData() const
55211286Sandreas.hansson@arm.com    {
55311286Sandreas.hansson@arm.com        MemCmd resp_cmd = cmd.responseCommand();
55411286Sandreas.hansson@arm.com        return resp_cmd.hasData();
55511286Sandreas.hansson@arm.com    }
55610583SCurtis.Dunham@arm.com    bool isLLSC() const              { return cmd.isLLSC(); }
55710583SCurtis.Dunham@arm.com    bool isError() const             { return cmd.isError(); }
55810583SCurtis.Dunham@arm.com    bool isPrint() const             { return cmd.isPrint(); }
55910583SCurtis.Dunham@arm.com    bool isFlush() const             { return cmd.isFlush(); }
5602812Srdreslin@umich.edu
56113350Snikos.nikoleris@arm.com    bool isWholeLineWrite(unsigned blk_size)
56213350Snikos.nikoleris@arm.com    {
56313350Snikos.nikoleris@arm.com        return (cmd == MemCmd::WriteReq || cmd == MemCmd::WriteLineReq) &&
56413350Snikos.nikoleris@arm.com            getOffset(blk_size) == 0 && getSize() == blk_size;
56513350Snikos.nikoleris@arm.com    }
56613350Snikos.nikoleris@arm.com
56711284Sandreas.hansson@arm.com    //@{
56811284Sandreas.hansson@arm.com    /// Snoop flags
56911284Sandreas.hansson@arm.com    /**
57011284Sandreas.hansson@arm.com     * Set the cacheResponding flag. This is used by the caches to
57111284Sandreas.hansson@arm.com     * signal another cache that they are responding to a request. A
57211284Sandreas.hansson@arm.com     * cache will only respond to snoops if it has the line in either
57311284Sandreas.hansson@arm.com     * Modified or Owned state. Note that on snoop hits we always pass
57411284Sandreas.hansson@arm.com     * the line as Modified and never Owned. In the case of an Owned
57511284Sandreas.hansson@arm.com     * line we proceed to invalidate all other copies.
57611284Sandreas.hansson@arm.com     *
57711284Sandreas.hansson@arm.com     * On a cache fill (see Cache::handleFill), we check hasSharers
57811284Sandreas.hansson@arm.com     * first, ignoring the cacheResponding flag if hasSharers is set.
57911284Sandreas.hansson@arm.com     * A line is consequently allocated as:
58011284Sandreas.hansson@arm.com     *
58111284Sandreas.hansson@arm.com     * hasSharers cacheResponding state
58211284Sandreas.hansson@arm.com     * true       false           Shared
58311284Sandreas.hansson@arm.com     * true       true            Shared
58411284Sandreas.hansson@arm.com     * false      false           Exclusive
58511284Sandreas.hansson@arm.com     * false      true            Modified
58611284Sandreas.hansson@arm.com     */
58711284Sandreas.hansson@arm.com    void setCacheResponding()
58810567Sandreas.hansson@arm.com    {
58910567Sandreas.hansson@arm.com        assert(isRequest());
59011284Sandreas.hansson@arm.com        assert(!flags.isSet(CACHE_RESPONDING));
59111284Sandreas.hansson@arm.com        flags.set(CACHE_RESPONDING);
59210567Sandreas.hansson@arm.com    }
59311284Sandreas.hansson@arm.com    bool cacheResponding() const { return flags.isSet(CACHE_RESPONDING); }
59411284Sandreas.hansson@arm.com    /**
59511284Sandreas.hansson@arm.com     * On fills, the hasSharers flag is used by the caches in
59611284Sandreas.hansson@arm.com     * combination with the cacheResponding flag, as clarified
59711284Sandreas.hansson@arm.com     * above. If the hasSharers flag is not set, the packet is passing
59811284Sandreas.hansson@arm.com     * writable. Thus, a response from a memory passes the line as
59911284Sandreas.hansson@arm.com     * writable by default.
60011284Sandreas.hansson@arm.com     *
60111284Sandreas.hansson@arm.com     * The hasSharers flag is also used by upstream caches to inform a
60211284Sandreas.hansson@arm.com     * downstream cache that they have the block (by calling
60311284Sandreas.hansson@arm.com     * setHasSharers on snoop request packets that hit in upstream
60411284Sandreas.hansson@arm.com     * cachs tags or MSHRs). If the snoop packet has sharers, a
60511284Sandreas.hansson@arm.com     * downstream cache is prevented from passing a dirty line upwards
60611284Sandreas.hansson@arm.com     * if it was not explicitly asked for a writable copy. See
60711284Sandreas.hansson@arm.com     * Cache::satisfyCpuSideRequest.
60811284Sandreas.hansson@arm.com     *
60911284Sandreas.hansson@arm.com     * The hasSharers flag is also used on writebacks, in
61011284Sandreas.hansson@arm.com     * combination with the WritbackClean or WritebackDirty commands,
61111284Sandreas.hansson@arm.com     * to allocate the block downstream either as:
61211284Sandreas.hansson@arm.com     *
61311284Sandreas.hansson@arm.com     * command        hasSharers state
61411284Sandreas.hansson@arm.com     * WritebackDirty false      Modified
61511284Sandreas.hansson@arm.com     * WritebackDirty true       Owned
61611284Sandreas.hansson@arm.com     * WritebackClean false      Exclusive
61711284Sandreas.hansson@arm.com     * WritebackClean true       Shared
61811284Sandreas.hansson@arm.com     */
61911284Sandreas.hansson@arm.com    void setHasSharers()    { flags.set(HAS_SHARERS); }
62011284Sandreas.hansson@arm.com    bool hasSharers() const { return flags.isSet(HAS_SHARERS); }
62111284Sandreas.hansson@arm.com    //@}
6224870Sstever@eecs.umich.edu
62311284Sandreas.hansson@arm.com    /**
62411284Sandreas.hansson@arm.com     * The express snoop flag is used for two purposes. Firstly, it is
62511284Sandreas.hansson@arm.com     * used to bypass flow control for normal (non-snoop) requests
62611284Sandreas.hansson@arm.com     * going downstream in the memory system. In cases where a cache
62711284Sandreas.hansson@arm.com     * is responding to a snoop from another cache (it had a dirty
62811284Sandreas.hansson@arm.com     * line), but the line is not writable (and there are possibly
62911284Sandreas.hansson@arm.com     * other copies), the express snoop flag is set by the downstream
63011284Sandreas.hansson@arm.com     * cache to invalidate all other copies in zero time. Secondly,
63111284Sandreas.hansson@arm.com     * the express snoop flag is also set to be able to distinguish
63211284Sandreas.hansson@arm.com     * snoop packets that came from a downstream cache, rather than
63311284Sandreas.hansson@arm.com     * snoop packets from neighbouring caches.
63411284Sandreas.hansson@arm.com     */
63511284Sandreas.hansson@arm.com    void setExpressSnoop()      { flags.set(EXPRESS_SNOOP); }
63611284Sandreas.hansson@arm.com    bool isExpressSnoop() const { return flags.isSet(EXPRESS_SNOOP); }
63711284Sandreas.hansson@arm.com
63811284Sandreas.hansson@arm.com    /**
63911284Sandreas.hansson@arm.com     * On responding to a snoop request (which only happens for
64011284Sandreas.hansson@arm.com     * Modified or Owned lines), make sure that we can transform an
64111284Sandreas.hansson@arm.com     * Owned response to a Modified one. If this flag is not set, the
64211284Sandreas.hansson@arm.com     * responding cache had the line in the Owned state, and there are
64311284Sandreas.hansson@arm.com     * possibly other Shared copies in the memory system. A downstream
64411284Sandreas.hansson@arm.com     * cache helps in orchestrating the invalidation of these copies
64511284Sandreas.hansson@arm.com     * by sending out the appropriate express snoops.
64611284Sandreas.hansson@arm.com     */
64711284Sandreas.hansson@arm.com    void setResponderHadWritable()
64811284Sandreas.hansson@arm.com    {
64911284Sandreas.hansson@arm.com        assert(cacheResponding());
65011746Snikos.nikoleris@arm.com        assert(!responderHadWritable());
65111284Sandreas.hansson@arm.com        flags.set(RESPONDER_HAD_WRITABLE);
65211284Sandreas.hansson@arm.com    }
65311284Sandreas.hansson@arm.com    bool responderHadWritable() const
65411284Sandreas.hansson@arm.com    { return flags.isSet(RESPONDER_HAD_WRITABLE); }
65511284Sandreas.hansson@arm.com
65612346Snikos.nikoleris@arm.com    /**
65713732Snikos.nikoleris@arm.com     * Copy the reponse flags from an input packet to this packet. The
65813732Snikos.nikoleris@arm.com     * reponse flags determine whether a responder has been found and
65913732Snikos.nikoleris@arm.com     * the state at which the block will be at the destination.
66013732Snikos.nikoleris@arm.com     *
66113732Snikos.nikoleris@arm.com     * @pkt The packet that we will copy flags from
66213732Snikos.nikoleris@arm.com     */
66313732Snikos.nikoleris@arm.com    void copyResponderFlags(const PacketPtr pkt);
66413732Snikos.nikoleris@arm.com
66513732Snikos.nikoleris@arm.com    /**
66612346Snikos.nikoleris@arm.com     * A writeback/writeclean cmd gets propagated further downstream
66712346Snikos.nikoleris@arm.com     * by the receiver when the flag is set.
66812346Snikos.nikoleris@arm.com     */
66912346Snikos.nikoleris@arm.com    void setWriteThrough()
67012346Snikos.nikoleris@arm.com    {
67112346Snikos.nikoleris@arm.com        assert(cmd.isWrite() &&
67212346Snikos.nikoleris@arm.com               (cmd.isEviction() || cmd == MemCmd::WriteClean));
67312346Snikos.nikoleris@arm.com        flags.set(WRITE_THROUGH);
67412346Snikos.nikoleris@arm.com    }
67512346Snikos.nikoleris@arm.com    void clearWriteThrough() { flags.clear(WRITE_THROUGH); }
67612346Snikos.nikoleris@arm.com    bool writeThrough() const { return flags.isSet(WRITE_THROUGH); }
67712346Snikos.nikoleris@arm.com
67812349Snikos.nikoleris@arm.com    /**
67912349Snikos.nikoleris@arm.com     * Set when a request hits in a cache and the cache is not going
68012349Snikos.nikoleris@arm.com     * to respond. This is used by the crossbar to coordinate
68112349Snikos.nikoleris@arm.com     * responses for cache maintenance operations.
68212349Snikos.nikoleris@arm.com     */
68312349Snikos.nikoleris@arm.com    void setSatisfied()
68412349Snikos.nikoleris@arm.com    {
68512349Snikos.nikoleris@arm.com        assert(cmd.isClean());
68612349Snikos.nikoleris@arm.com        assert(!flags.isSet(SATISFIED));
68712349Snikos.nikoleris@arm.com        flags.set(SATISFIED);
68812349Snikos.nikoleris@arm.com    }
68912349Snikos.nikoleris@arm.com    bool satisfied() const { return flags.isSet(SATISFIED); }
69012349Snikos.nikoleris@arm.com
6919951Sstephan.diestelhorst@arm.com    void setSuppressFuncError()     { flags.set(SUPPRESS_FUNC_ERROR); }
6929951Sstephan.diestelhorst@arm.com    bool suppressFuncError() const  { return flags.isSet(SUPPRESS_FUNC_ERROR); }
69310763Sali.jafri@arm.com    void setBlockCached()          { flags.set(BLOCK_CACHED); }
69410763Sali.jafri@arm.com    bool isBlockCached() const     { return flags.isSet(BLOCK_CACHED); }
69510883Sali.jafri@arm.com    void clearBlockCached()        { flags.clear(BLOCK_CACHED); }
6964895Sstever@eecs.umich.edu
69712966SMatteo.Andreozzi@arm.com    /**
69812966SMatteo.Andreozzi@arm.com     * QoS Value getter
69912966SMatteo.Andreozzi@arm.com     * Returns 0 if QoS value was never set (constructor default).
70012966SMatteo.Andreozzi@arm.com     *
70112966SMatteo.Andreozzi@arm.com     * @return QoS priority value of the packet
70212966SMatteo.Andreozzi@arm.com     */
70312966SMatteo.Andreozzi@arm.com    inline uint8_t qosValue() const { return _qosValue; }
70412966SMatteo.Andreozzi@arm.com
70512966SMatteo.Andreozzi@arm.com    /**
70612966SMatteo.Andreozzi@arm.com     * QoS Value setter
70712966SMatteo.Andreozzi@arm.com     * Interface for setting QoS priority value of the packet.
70812966SMatteo.Andreozzi@arm.com     *
70912966SMatteo.Andreozzi@arm.com     * @param qos_value QoS priority value
71012966SMatteo.Andreozzi@arm.com     */
71112966SMatteo.Andreozzi@arm.com    inline void qosValue(const uint8_t qos_value)
71212966SMatteo.Andreozzi@arm.com    { _qosValue = qos_value; }
71312966SMatteo.Andreozzi@arm.com
71412966SMatteo.Andreozzi@arm.com    inline MasterID masterId() const { return req->masterId(); }
71512966SMatteo.Andreozzi@arm.com
7164870Sstever@eecs.umich.edu    // Network error conditions... encapsulate them as methods since
7174870Sstever@eecs.umich.edu    // their encoding keeps changing (from result field to command
7184870Sstever@eecs.umich.edu    // field, etc.)
7195735Snate@binkert.org    void
7205735Snate@binkert.org    setBadAddress()
7215735Snate@binkert.org    {
7225735Snate@binkert.org        assert(isResponse());
7235735Snate@binkert.org        cmd = MemCmd::BadAddressError;
7245735Snate@binkert.org    }
7255735Snate@binkert.org
7264986Ssaidi@eecs.umich.edu    void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
7272814Srdreslin@umich.edu
72811057Sandreas.hansson@arm.com    Addr getAddr() const { assert(flags.isSet(VALID_ADDR)); return addr; }
7299259SAli.Saidi@ARM.com    /**
7309259SAli.Saidi@ARM.com     * Update the address of this packet mid-transaction. This is used
7319259SAli.Saidi@ARM.com     * by the address mapper to change an already set address to a new
7329259SAli.Saidi@ARM.com     * one based on the system configuration. It is intended to remap
7339259SAli.Saidi@ARM.com     * an existing address, so it asserts that the current address is
7349259SAli.Saidi@ARM.com     * valid.
7359259SAli.Saidi@ARM.com     */
73611057Sandreas.hansson@arm.com    void setAddr(Addr _addr) { assert(flags.isSet(VALID_ADDR)); addr = _addr; }
7379259SAli.Saidi@ARM.com
73811057Sandreas.hansson@arm.com    unsigned getSize() const  { assert(flags.isSet(VALID_SIZE)); return size; }
73910938Sandreas.hansson@arm.com
74013856Sodanrc@yahoo.com.br    /**
74113856Sodanrc@yahoo.com.br     * Get address range to which this packet belongs.
74213856Sodanrc@yahoo.com.br     *
74313856Sodanrc@yahoo.com.br     * @return Address range of this packet.
74413856Sodanrc@yahoo.com.br     */
74513856Sodanrc@yahoo.com.br    AddrRange getAddrRange() const;
74613856Sodanrc@yahoo.com.br
74710938Sandreas.hansson@arm.com    Addr getOffset(unsigned int blk_size) const
74810938Sandreas.hansson@arm.com    {
74910938Sandreas.hansson@arm.com        return getAddr() & Addr(blk_size - 1);
75010938Sandreas.hansson@arm.com    }
75110938Sandreas.hansson@arm.com
75210938Sandreas.hansson@arm.com    Addr getBlockAddr(unsigned int blk_size) const
75310938Sandreas.hansson@arm.com    {
75410938Sandreas.hansson@arm.com        return getAddr() & ~(Addr(blk_size - 1));
75510938Sandreas.hansson@arm.com    }
7562549SN/A
75711057Sandreas.hansson@arm.com    bool isSecure() const
75811057Sandreas.hansson@arm.com    {
75911057Sandreas.hansson@arm.com        assert(flags.isSet(VALID_ADDR));
76011057Sandreas.hansson@arm.com        return _isSecure;
76111057Sandreas.hansson@arm.com    }
76210028SGiacomo.Gabrielli@arm.com
7635735Snate@binkert.org    /**
76411306Santhony.gutierrez@amd.com     * Accessor function to atomic op.
76511306Santhony.gutierrez@amd.com     */
76611306Santhony.gutierrez@amd.com    AtomicOpFunctor *getAtomicOp() const { return req->getAtomicOpFunctor(); }
76711306Santhony.gutierrez@amd.com    bool isAtomicOp() const { return req->isAtomic(); }
76811306Santhony.gutierrez@amd.com
76911306Santhony.gutierrez@amd.com    /**
7707550SBrad.Beckmann@amd.com     * It has been determined that the SC packet should successfully update
77110938Sandreas.hansson@arm.com     * memory. Therefore, convert this SC packet to a normal write.
7727550SBrad.Beckmann@amd.com     */
7737550SBrad.Beckmann@amd.com    void
7747550SBrad.Beckmann@amd.com    convertScToWrite()
7757550SBrad.Beckmann@amd.com    {
7767550SBrad.Beckmann@amd.com        assert(isLLSC());
7777550SBrad.Beckmann@amd.com        assert(isWrite());
7787550SBrad.Beckmann@amd.com        cmd = MemCmd::WriteReq;
7797550SBrad.Beckmann@amd.com    }
7807550SBrad.Beckmann@amd.com
7817550SBrad.Beckmann@amd.com    /**
78210938Sandreas.hansson@arm.com     * When ruby is in use, Ruby will monitor the cache line and the
78310938Sandreas.hansson@arm.com     * phys memory should treat LL ops as normal reads.
7847550SBrad.Beckmann@amd.com     */
7857550SBrad.Beckmann@amd.com    void
7867550SBrad.Beckmann@amd.com    convertLlToRead()
7877550SBrad.Beckmann@amd.com    {
7887550SBrad.Beckmann@amd.com        assert(isLLSC());
7897550SBrad.Beckmann@amd.com        assert(isRead());
7907550SBrad.Beckmann@amd.com        cmd = MemCmd::ReadReq;
7917550SBrad.Beckmann@amd.com    }
7927550SBrad.Beckmann@amd.com
7937550SBrad.Beckmann@amd.com    /**
79410938Sandreas.hansson@arm.com     * Constructor. Note that a Request object must be constructed
79511057Sandreas.hansson@arm.com     * first, but the Requests's physical address and size fields need
79611057Sandreas.hansson@arm.com     * not be valid. The command must be supplied.
7975735Snate@binkert.org     */
79812749Sgiacomo.travaglini@arm.com    Packet(const RequestPtr &_req, MemCmd _cmd)
79912966SMatteo.Andreozzi@arm.com        :  cmd(_cmd), id((PacketId)_req.get()), req(_req),
80012966SMatteo.Andreozzi@arm.com           data(nullptr), addr(0), _isSecure(false), size(0),
80112966SMatteo.Andreozzi@arm.com           _qosValue(0), headerDelay(0), snoopDelay(0),
80212351Snikos.nikoleris@arm.com           payloadDelay(0), senderState(NULL)
80311057Sandreas.hansson@arm.com    {
80411057Sandreas.hansson@arm.com        if (req->hasPaddr()) {
80511057Sandreas.hansson@arm.com            addr = req->getPaddr();
80611057Sandreas.hansson@arm.com            flags.set(VALID_ADDR);
80711057Sandreas.hansson@arm.com            _isSecure = req->isSecure();
80811057Sandreas.hansson@arm.com        }
80911057Sandreas.hansson@arm.com        if (req->hasSize()) {
81011057Sandreas.hansson@arm.com            size = req->getSize();
81111057Sandreas.hansson@arm.com            flags.set(VALID_SIZE);
81211057Sandreas.hansson@arm.com        }
81311057Sandreas.hansson@arm.com    }
81411057Sandreas.hansson@arm.com
81511057Sandreas.hansson@arm.com    /**
81611057Sandreas.hansson@arm.com     * Alternate constructor if you are trying to create a packet with
81711057Sandreas.hansson@arm.com     * a request that is for a whole block, not the address from the
81811057Sandreas.hansson@arm.com     * req.  this allows for overriding the size/addr of the req.
81911057Sandreas.hansson@arm.com     */
82012749Sgiacomo.travaglini@arm.com    Packet(const RequestPtr &_req, MemCmd _cmd, int _blkSize, PacketId _id = 0)
82112749Sgiacomo.travaglini@arm.com        :  cmd(_cmd), id(_id ? _id : (PacketId)_req.get()), req(_req),
82212966SMatteo.Andreozzi@arm.com           data(nullptr), addr(0), _isSecure(false),
82312966SMatteo.Andreozzi@arm.com           _qosValue(0), headerDelay(0),
82412749Sgiacomo.travaglini@arm.com           snoopDelay(0), payloadDelay(0), senderState(NULL)
82511057Sandreas.hansson@arm.com    {
82611057Sandreas.hansson@arm.com        if (req->hasPaddr()) {
82711057Sandreas.hansson@arm.com            addr = req->getPaddr() & ~(_blkSize - 1);
82811057Sandreas.hansson@arm.com            flags.set(VALID_ADDR);
82911057Sandreas.hansson@arm.com            _isSecure = req->isSecure();
83011057Sandreas.hansson@arm.com        }
83111057Sandreas.hansson@arm.com        size = _blkSize;
83211057Sandreas.hansson@arm.com        flags.set(VALID_SIZE);
83311057Sandreas.hansson@arm.com    }
8344626Sstever@eecs.umich.edu
8355735Snate@binkert.org    /**
8365735Snate@binkert.org     * Alternate constructor for copying a packet.  Copy all fields
8374887Sstever@eecs.umich.edu     * *except* if the original packet's data was dynamic, don't copy
8384887Sstever@eecs.umich.edu     * that, as we can't guarantee that the new packet's lifetime is
8394887Sstever@eecs.umich.edu     * less than that of the original packet.  In this case the new
8405735Snate@binkert.org     * packet should allocate its own data.
8415735Snate@binkert.org     */
84210896Snilay@cs.wisc.edu    Packet(const PacketPtr pkt, bool clear_flags, bool alloc_data)
84312351Snikos.nikoleris@arm.com        :  cmd(pkt->cmd), id(pkt->id), req(pkt->req),
84410571Sandreas.hansson@arm.com           data(nullptr),
84510028SGiacomo.Gabrielli@arm.com           addr(pkt->addr), _isSecure(pkt->_isSecure), size(pkt->size),
84610723Sandreas.hansson@arm.com           bytesValid(pkt->bytesValid),
84712966SMatteo.Andreozzi@arm.com           _qosValue(pkt->qosValue()),
84810694SMarco.Balboni@ARM.com           headerDelay(pkt->headerDelay),
84911127Sandreas.hansson@arm.com           snoopDelay(0),
85010694SMarco.Balboni@ARM.com           payloadDelay(pkt->payloadDelay),
8519546Sandreas.hansson@arm.com           senderState(pkt->senderState)
8524626Sstever@eecs.umich.edu    {
85310571Sandreas.hansson@arm.com        if (!clear_flags)
8545735Snate@binkert.org            flags.set(pkt->flags & COPY_FLAGS);
8555735Snate@binkert.org
85611057Sandreas.hansson@arm.com        flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE));
85711057Sandreas.hansson@arm.com
85810571Sandreas.hansson@arm.com        // should we allocate space for data, or not, the express
85910571Sandreas.hansson@arm.com        // snoops do not need to carry any data as they only serve to
86010571Sandreas.hansson@arm.com        // co-ordinate state changes
86110571Sandreas.hansson@arm.com        if (alloc_data) {
86210571Sandreas.hansson@arm.com            // even if asked to allocate data, if the original packet
86310571Sandreas.hansson@arm.com            // holds static data, then the sender will not be doing
86410571Sandreas.hansson@arm.com            // any memcpy on receiving the response, thus we simply
86510571Sandreas.hansson@arm.com            // carry the pointer forward
86610571Sandreas.hansson@arm.com            if (pkt->flags.isSet(STATIC_DATA)) {
86710571Sandreas.hansson@arm.com                data = pkt->data;
86810571Sandreas.hansson@arm.com                flags.set(STATIC_DATA);
86910571Sandreas.hansson@arm.com            } else {
87010571Sandreas.hansson@arm.com                allocate();
87110571Sandreas.hansson@arm.com            }
87210571Sandreas.hansson@arm.com        }
87310342SCurtis.Dunham@arm.com    }
8748668Sgeoffrey.blake@arm.com
87510342SCurtis.Dunham@arm.com    /**
87610739Ssteve.reinhardt@amd.com     * Generate the appropriate read MemCmd based on the Request flags.
87710342SCurtis.Dunham@arm.com     */
87810739Ssteve.reinhardt@amd.com    static MemCmd
87912749Sgiacomo.travaglini@arm.com    makeReadCmd(const RequestPtr &req)
88010342SCurtis.Dunham@arm.com    {
88110739Ssteve.reinhardt@amd.com        if (req->isLLSC())
88210739Ssteve.reinhardt@amd.com            return MemCmd::LoadLockedReq;
88313367Syuetsu.kodama@riken.jp        else if (req->isPrefetchEx())
88413367Syuetsu.kodama@riken.jp            return MemCmd::SoftPFExReq;
88510739Ssteve.reinhardt@amd.com        else if (req->isPrefetch())
88610739Ssteve.reinhardt@amd.com            return MemCmd::SoftPFReq;
88710739Ssteve.reinhardt@amd.com        else
88810739Ssteve.reinhardt@amd.com            return MemCmd::ReadReq;
88910739Ssteve.reinhardt@amd.com    }
89010739Ssteve.reinhardt@amd.com
89110739Ssteve.reinhardt@amd.com    /**
89210739Ssteve.reinhardt@amd.com     * Generate the appropriate write MemCmd based on the Request flags.
89310739Ssteve.reinhardt@amd.com     */
89410739Ssteve.reinhardt@amd.com    static MemCmd
89512749Sgiacomo.travaglini@arm.com    makeWriteCmd(const RequestPtr &req)
89610739Ssteve.reinhardt@amd.com    {
89710739Ssteve.reinhardt@amd.com        if (req->isLLSC())
89810739Ssteve.reinhardt@amd.com            return MemCmd::StoreCondReq;
89912766Sqtt2@cornell.edu        else if (req->isSwap() || req->isAtomic())
90010739Ssteve.reinhardt@amd.com            return MemCmd::SwapReq;
90112347Snikos.nikoleris@arm.com        else if (req->isCacheInvalidate()) {
90212347Snikos.nikoleris@arm.com          return req->isCacheClean() ? MemCmd::CleanInvalidReq :
90312347Snikos.nikoleris@arm.com              MemCmd::InvalidateReq;
90412347Snikos.nikoleris@arm.com        } else if (req->isCacheClean()) {
90512347Snikos.nikoleris@arm.com            return MemCmd::CleanSharedReq;
90612347Snikos.nikoleris@arm.com        } else
90710739Ssteve.reinhardt@amd.com            return MemCmd::WriteReq;
90810342SCurtis.Dunham@arm.com    }
90910342SCurtis.Dunham@arm.com
91010342SCurtis.Dunham@arm.com    /**
91110342SCurtis.Dunham@arm.com     * Constructor-like methods that return Packets based on Request objects.
91210739Ssteve.reinhardt@amd.com     * Fine-tune the MemCmd type if it's not a vanilla read or write.
91310342SCurtis.Dunham@arm.com     */
91410342SCurtis.Dunham@arm.com    static PacketPtr
91512749Sgiacomo.travaglini@arm.com    createRead(const RequestPtr &req)
91610342SCurtis.Dunham@arm.com    {
91710739Ssteve.reinhardt@amd.com        return new Packet(req, makeReadCmd(req));
91810342SCurtis.Dunham@arm.com    }
91910342SCurtis.Dunham@arm.com
92010342SCurtis.Dunham@arm.com    static PacketPtr
92112749Sgiacomo.travaglini@arm.com    createWrite(const RequestPtr &req)
92210342SCurtis.Dunham@arm.com    {
92310739Ssteve.reinhardt@amd.com        return new Packet(req, makeWriteCmd(req));
9242641Sstever@eecs.umich.edu    }
9252549SN/A
9265735Snate@binkert.org    /**
9275735Snate@binkert.org     * clean up packet variables
9285735Snate@binkert.org     */
9292566SN/A    ~Packet()
9305387Sstever@gmail.com    {
9315735Snate@binkert.org        deleteData();
9325387Sstever@gmail.com    }
9332566SN/A
9345735Snate@binkert.org    /**
9354626Sstever@eecs.umich.edu     * Take a request packet and modify it in place to be suitable for
93610660Sandreas.hansson@arm.com     * returning as a response to that request.
9374626Sstever@eecs.umich.edu     */
9385735Snate@binkert.org    void
9395735Snate@binkert.org    makeResponse()
9404626Sstever@eecs.umich.edu    {
9412662Sstever@eecs.umich.edu        assert(needsResponse());
9422855Srdreslin@umich.edu        assert(isRequest());
9434022Sstever@eecs.umich.edu        cmd = cmd.responseCommand();
9445745Snate@binkert.org
9457464Ssteve.reinhardt@amd.com        // responses are never express, even if the snoop that
9467464Ssteve.reinhardt@amd.com        // triggered them was
9477464Ssteve.reinhardt@amd.com        flags.clear(EXPRESS_SNOOP);
9482641Sstever@eecs.umich.edu    }
9492641Sstever@eecs.umich.edu
9505735Snate@binkert.org    void
9515735Snate@binkert.org    makeAtomicResponse()
9524870Sstever@eecs.umich.edu    {
9534870Sstever@eecs.umich.edu        makeResponse();
9544870Sstever@eecs.umich.edu    }
9554870Sstever@eecs.umich.edu
9565735Snate@binkert.org    void
9575735Snate@binkert.org    makeTimingResponse()
9583348Sbinkertn@umich.edu    {
9594870Sstever@eecs.umich.edu        makeResponse();
9603135Srdreslin@umich.edu    }
9613135Srdreslin@umich.edu
9628436SBrad.Beckmann@amd.com    void
9638436SBrad.Beckmann@amd.com    setFunctionalResponseStatus(bool success)
9648436SBrad.Beckmann@amd.com    {
9658436SBrad.Beckmann@amd.com        if (!success) {
9668436SBrad.Beckmann@amd.com            if (isWrite()) {
9678436SBrad.Beckmann@amd.com                cmd = MemCmd::FunctionalWriteError;
9688436SBrad.Beckmann@amd.com            } else {
9698436SBrad.Beckmann@amd.com                cmd = MemCmd::FunctionalReadError;
9708436SBrad.Beckmann@amd.com            }
9718436SBrad.Beckmann@amd.com        }
9728436SBrad.Beckmann@amd.com    }
9738436SBrad.Beckmann@amd.com
97411057Sandreas.hansson@arm.com    void
97511057Sandreas.hansson@arm.com    setSize(unsigned size)
97611057Sandreas.hansson@arm.com    {
97711057Sandreas.hansson@arm.com        assert(!flags.isSet(VALID_SIZE));
97811057Sandreas.hansson@arm.com
97911057Sandreas.hansson@arm.com        this->size = size;
98011057Sandreas.hansson@arm.com        flags.set(VALID_SIZE);
98111057Sandreas.hansson@arm.com    }
98211057Sandreas.hansson@arm.com
98313860Sodanrc@yahoo.com.br    /**
98413860Sodanrc@yahoo.com.br     * Check if packet corresponds to a given block-aligned address and
98513860Sodanrc@yahoo.com.br     * address space.
98613860Sodanrc@yahoo.com.br     *
98713860Sodanrc@yahoo.com.br     * @param addr The address to compare against.
98813860Sodanrc@yahoo.com.br     * @param is_secure Whether addr belongs to the secure address space.
98913860Sodanrc@yahoo.com.br     * @param blk_size Block size in bytes.
99013860Sodanrc@yahoo.com.br     * @return Whether packet matches description.
99113860Sodanrc@yahoo.com.br     */
99213860Sodanrc@yahoo.com.br    bool matchBlockAddr(const Addr addr, const bool is_secure,
99313860Sodanrc@yahoo.com.br                        const int blk_size) const;
99413860Sodanrc@yahoo.com.br
99513860Sodanrc@yahoo.com.br    /**
99613860Sodanrc@yahoo.com.br     * Check if this packet refers to the same block-aligned address and
99713860Sodanrc@yahoo.com.br     * address space as another packet.
99813860Sodanrc@yahoo.com.br     *
99913860Sodanrc@yahoo.com.br     * @param pkt The packet to compare against.
100013860Sodanrc@yahoo.com.br     * @param blk_size Block size in bytes.
100113860Sodanrc@yahoo.com.br     * @return Whether packet matches description.
100213860Sodanrc@yahoo.com.br     */
100313860Sodanrc@yahoo.com.br    bool matchBlockAddr(const PacketPtr pkt, const int blk_size) const;
100413860Sodanrc@yahoo.com.br
100513860Sodanrc@yahoo.com.br    /**
100613860Sodanrc@yahoo.com.br     * Check if packet corresponds to a given address and address space.
100713860Sodanrc@yahoo.com.br     *
100813860Sodanrc@yahoo.com.br     * @param addr The address to compare against.
100913860Sodanrc@yahoo.com.br     * @param is_secure Whether addr belongs to the secure address space.
101013860Sodanrc@yahoo.com.br     * @return Whether packet matches description.
101113860Sodanrc@yahoo.com.br     */
101213860Sodanrc@yahoo.com.br    bool matchAddr(const Addr addr, const bool is_secure) const;
101313860Sodanrc@yahoo.com.br
101413860Sodanrc@yahoo.com.br    /**
101513860Sodanrc@yahoo.com.br     * Check if this packet refers to the same address and address space as
101613860Sodanrc@yahoo.com.br     * another packet.
101713860Sodanrc@yahoo.com.br     *
101813860Sodanrc@yahoo.com.br     * @param pkt The packet to compare against.
101913860Sodanrc@yahoo.com.br     * @return Whether packet matches description.
102013860Sodanrc@yahoo.com.br     */
102113860Sodanrc@yahoo.com.br    bool matchAddr(const PacketPtr pkt) const;
102211057Sandreas.hansson@arm.com
102311013Sandreas.sandberg@arm.com  public:
102411013Sandreas.sandberg@arm.com    /**
102511013Sandreas.sandberg@arm.com     * @{
102611013Sandreas.sandberg@arm.com     * @name Data accessor mehtods
102711013Sandreas.sandberg@arm.com     */
102811013Sandreas.sandberg@arm.com
10293348Sbinkertn@umich.edu    /**
10303348Sbinkertn@umich.edu     * Set the data pointer to the following value that should not be
103110571Sandreas.hansson@arm.com     * freed. Static data allows us to do a single memcpy even if
103210571Sandreas.hansson@arm.com     * multiple packets are required to get from source to destination
103310571Sandreas.hansson@arm.com     * and back. In essence the pointer is set calling dataStatic on
103410571Sandreas.hansson@arm.com     * the original packet, and whenever this packet is copied and
103510571Sandreas.hansson@arm.com     * forwarded the same pointer is passed on. When a packet
103610571Sandreas.hansson@arm.com     * eventually reaches the destination holding the data, it is
103710571Sandreas.hansson@arm.com     * copied once into the location originally set. On the way back
103810571Sandreas.hansson@arm.com     * to the source, no copies are necessary.
10392566SN/A     */
10402566SN/A    template <typename T>
10413348Sbinkertn@umich.edu    void
10423348Sbinkertn@umich.edu    dataStatic(T *p)
10433348Sbinkertn@umich.edu    {
104410566Sandreas.hansson@arm.com        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
10453348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
10465735Snate@binkert.org        flags.set(STATIC_DATA);
10473348Sbinkertn@umich.edu    }
10482566SN/A
10493348Sbinkertn@umich.edu    /**
105010564Sandreas.hansson@arm.com     * Set the data pointer to the following value that should not be
105110564Sandreas.hansson@arm.com     * freed. This version of the function allows the pointer passed
105210564Sandreas.hansson@arm.com     * to us to be const. To avoid issues down the line we cast the
105310564Sandreas.hansson@arm.com     * constness away, the alternative would be to keep both a const
105410564Sandreas.hansson@arm.com     * and non-const data pointer and cleverly choose between
105510564Sandreas.hansson@arm.com     * them. Note that this is only allowed for static data.
105610564Sandreas.hansson@arm.com     */
105710564Sandreas.hansson@arm.com    template <typename T>
105810564Sandreas.hansson@arm.com    void
105910564Sandreas.hansson@arm.com    dataStaticConst(const T *p)
106010564Sandreas.hansson@arm.com    {
106110566Sandreas.hansson@arm.com        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
106210564Sandreas.hansson@arm.com        data = const_cast<PacketDataPtr>(p);
106310564Sandreas.hansson@arm.com        flags.set(STATIC_DATA);
106410564Sandreas.hansson@arm.com    }
106510564Sandreas.hansson@arm.com
106610564Sandreas.hansson@arm.com    /**
10673348Sbinkertn@umich.edu     * Set the data pointer to a value that should have delete []
106810571Sandreas.hansson@arm.com     * called on it. Dynamic data is local to this packet, and as the
106910571Sandreas.hansson@arm.com     * packet travels from source to destination, forwarded packets
107010571Sandreas.hansson@arm.com     * will allocate their own data. When a packet reaches the final
107110571Sandreas.hansson@arm.com     * destination it will populate the dynamic data of that specific
107210571Sandreas.hansson@arm.com     * packet, and on the way back towards the source, memcpy will be
107310571Sandreas.hansson@arm.com     * invoked in every step where a new packet was created e.g. in
107410571Sandreas.hansson@arm.com     * the caches. Ultimately when the response reaches the source a
107510571Sandreas.hansson@arm.com     * final memcpy is needed to extract the data from the packet
107610571Sandreas.hansson@arm.com     * before it is deallocated.
10773348Sbinkertn@umich.edu     */
10782566SN/A    template <typename T>
10793348Sbinkertn@umich.edu    void
10803348Sbinkertn@umich.edu    dataDynamic(T *p)
10813348Sbinkertn@umich.edu    {
108210566Sandreas.hansson@arm.com        assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
10833348Sbinkertn@umich.edu        data = (PacketDataPtr)p;
10845735Snate@binkert.org        flags.set(DYNAMIC_DATA);
10853348Sbinkertn@umich.edu    }
10863348Sbinkertn@umich.edu
10875735Snate@binkert.org    /**
10885735Snate@binkert.org     * get a pointer to the data ptr.
10895735Snate@binkert.org     */
10903348Sbinkertn@umich.edu    template <typename T>
10913348Sbinkertn@umich.edu    T*
109210562Sandreas.hansson@arm.com    getPtr()
10933348Sbinkertn@umich.edu    {
109410562Sandreas.hansson@arm.com        assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
109513954Sgiacomo.gabrielli@arm.com        assert(!isMaskedWrite());
10963348Sbinkertn@umich.edu        return (T*)data;
10973348Sbinkertn@umich.edu    }
10982566SN/A
109910563Sandreas.hansson@arm.com    template <typename T>
110010563Sandreas.hansson@arm.com    const T*
110110563Sandreas.hansson@arm.com    getConstPtr() const
110210563Sandreas.hansson@arm.com    {
110310563Sandreas.hansson@arm.com        assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
110410563Sandreas.hansson@arm.com        return (const T*)data;
110510563Sandreas.hansson@arm.com    }
110610563Sandreas.hansson@arm.com
11075735Snate@binkert.org    /**
110811013Sandreas.sandberg@arm.com     * Get the data in the packet byte swapped from big endian to
110911013Sandreas.sandberg@arm.com     * host endian.
111011013Sandreas.sandberg@arm.com     */
111111013Sandreas.sandberg@arm.com    template <typename T>
111211013Sandreas.sandberg@arm.com    T getBE() const;
111311013Sandreas.sandberg@arm.com
111411013Sandreas.sandberg@arm.com    /**
111511013Sandreas.sandberg@arm.com     * Get the data in the packet byte swapped from little endian to
111611013Sandreas.sandberg@arm.com     * host endian.
111711013Sandreas.sandberg@arm.com     */
111811013Sandreas.sandberg@arm.com    template <typename T>
111911013Sandreas.sandberg@arm.com    T getLE() const;
112011013Sandreas.sandberg@arm.com
112111013Sandreas.sandberg@arm.com    /**
112211013Sandreas.sandberg@arm.com     * Get the data in the packet byte swapped from the specified
112311013Sandreas.sandberg@arm.com     * endianness.
112411013Sandreas.sandberg@arm.com     */
112511013Sandreas.sandberg@arm.com    template <typename T>
112611013Sandreas.sandberg@arm.com    T get(ByteOrder endian) const;
112711013Sandreas.sandberg@arm.com
112813347Sgabeblack@google.com#if THE_ISA != NULL_ISA
112911013Sandreas.sandberg@arm.com    /**
113011013Sandreas.sandberg@arm.com     * Get the data in the packet byte swapped from guest to host
113111013Sandreas.sandberg@arm.com     * endian.
11325735Snate@binkert.org     */
11332566SN/A    template <typename T>
113413348Sgabeblack@google.com    T get() const
113513348Sgabeblack@google.com        M5_DEPRECATED_MSG("The memory system should be ISA independent.");
113613347Sgabeblack@google.com#endif
11372566SN/A
113811013Sandreas.sandberg@arm.com    /** Set the value in the data pointer to v as big endian. */
113911013Sandreas.sandberg@arm.com    template <typename T>
114011013Sandreas.sandberg@arm.com    void setBE(T v);
114111013Sandreas.sandberg@arm.com
114211013Sandreas.sandberg@arm.com    /** Set the value in the data pointer to v as little endian. */
114311013Sandreas.sandberg@arm.com    template <typename T>
114411013Sandreas.sandberg@arm.com    void setLE(T v);
114511013Sandreas.sandberg@arm.com
11465735Snate@binkert.org    /**
114711013Sandreas.sandberg@arm.com     * Set the value in the data pointer to v using the specified
114811013Sandreas.sandberg@arm.com     * endianness.
11495735Snate@binkert.org     */
11502566SN/A    template <typename T>
115111013Sandreas.sandberg@arm.com    void set(T v, ByteOrder endian);
115211013Sandreas.sandberg@arm.com
115313347Sgabeblack@google.com#if THE_ISA != NULL_ISA
115411013Sandreas.sandberg@arm.com    /** Set the value in the data pointer to v as guest endian. */
115511013Sandreas.sandberg@arm.com    template <typename T>
115613348Sgabeblack@google.com    void set(T v)
115713348Sgabeblack@google.com        M5_DEPRECATED_MSG("The memory system should be ISA independent.");
115813347Sgabeblack@google.com#endif
11592566SN/A
116012652Sandreas.sandberg@arm.com    /**
116112652Sandreas.sandberg@arm.com     * Get the data in the packet byte swapped from the specified
116212652Sandreas.sandberg@arm.com     * endianness and zero-extended to 64 bits.
116312652Sandreas.sandberg@arm.com     */
116412652Sandreas.sandberg@arm.com    uint64_t getUintX(ByteOrder endian) const;
116512652Sandreas.sandberg@arm.com
116612652Sandreas.sandberg@arm.com    /**
116712652Sandreas.sandberg@arm.com     * Set the value in the word w after truncating it to the length
116812652Sandreas.sandberg@arm.com     * of the packet and then byteswapping it to the desired
116912652Sandreas.sandberg@arm.com     * endianness.
117012652Sandreas.sandberg@arm.com     */
117112652Sandreas.sandberg@arm.com    void setUintX(uint64_t w, ByteOrder endian);
117212652Sandreas.sandberg@arm.com
11733348Sbinkertn@umich.edu    /**
11744626Sstever@eecs.umich.edu     * Copy data into the packet from the provided pointer.
11754626Sstever@eecs.umich.edu     */
11765735Snate@binkert.org    void
117710563Sandreas.hansson@arm.com    setData(const uint8_t *p)
11784626Sstever@eecs.umich.edu    {
117910571Sandreas.hansson@arm.com        // we should never be copying data onto itself, which means we
118010571Sandreas.hansson@arm.com        // must idenfity packets with static data, as they carry the
118110571Sandreas.hansson@arm.com        // same pointer from source to destination and back
118210571Sandreas.hansson@arm.com        assert(p != getPtr<uint8_t>() || flags.isSet(STATIC_DATA));
118310571Sandreas.hansson@arm.com
118413954Sgiacomo.gabrielli@arm.com        if (p != getPtr<uint8_t>()) {
118510571Sandreas.hansson@arm.com            // for packet with allocated dynamic data, we copy data from
118610571Sandreas.hansson@arm.com            // one to the other, e.g. a forwarded response to a response
11877691SAli.Saidi@ARM.com            std::memcpy(getPtr<uint8_t>(), p, getSize());
118813954Sgiacomo.gabrielli@arm.com        }
11894626Sstever@eecs.umich.edu    }
11904626Sstever@eecs.umich.edu
11914626Sstever@eecs.umich.edu    /**
11924626Sstever@eecs.umich.edu     * Copy data into the packet from the provided block pointer,
11934626Sstever@eecs.umich.edu     * which is aligned to the given block size.
11944626Sstever@eecs.umich.edu     */
11955735Snate@binkert.org    void
119610563Sandreas.hansson@arm.com    setDataFromBlock(const uint8_t *blk_data, int blkSize)
11974626Sstever@eecs.umich.edu    {
11984626Sstever@eecs.umich.edu        setData(blk_data + getOffset(blkSize));
11994626Sstever@eecs.umich.edu    }
12004626Sstever@eecs.umich.edu
12014626Sstever@eecs.umich.edu    /**
120212633Sodanrc@yahoo.com.br     * Copy data from the packet to the memory at the provided pointer.
120312633Sodanrc@yahoo.com.br     * @param p Pointer to which data will be copied.
12044626Sstever@eecs.umich.edu     */
12055735Snate@binkert.org    void
120610563Sandreas.hansson@arm.com    writeData(uint8_t *p) const
12074626Sstever@eecs.umich.edu    {
120813954Sgiacomo.gabrielli@arm.com        if (!isMaskedWrite()) {
120913954Sgiacomo.gabrielli@arm.com            std::memcpy(p, getConstPtr<uint8_t>(), getSize());
121013954Sgiacomo.gabrielli@arm.com        } else {
121113954Sgiacomo.gabrielli@arm.com            assert(req->getByteEnable().size() == getSize());
121213954Sgiacomo.gabrielli@arm.com            // Write only the enabled bytes
121313954Sgiacomo.gabrielli@arm.com            const uint8_t *base = getConstPtr<uint8_t>();
121413954Sgiacomo.gabrielli@arm.com            for (int i = 0; i < getSize(); i++) {
121513954Sgiacomo.gabrielli@arm.com                if (req->getByteEnable()[i]) {
121613954Sgiacomo.gabrielli@arm.com                    p[i] = *(base + i);
121713954Sgiacomo.gabrielli@arm.com                }
121813954Sgiacomo.gabrielli@arm.com                // Disabled bytes stay untouched
121913954Sgiacomo.gabrielli@arm.com            }
122013954Sgiacomo.gabrielli@arm.com        }
12214626Sstever@eecs.umich.edu    }
12224626Sstever@eecs.umich.edu
12234626Sstever@eecs.umich.edu    /**
122412633Sodanrc@yahoo.com.br     * Copy data from the packet to the provided block pointer, which
122512633Sodanrc@yahoo.com.br     * is aligned to the given block size.
122612633Sodanrc@yahoo.com.br     * @param blk_data Pointer to block to which data will be copied.
122712633Sodanrc@yahoo.com.br     * @param blkSize Block size in bytes.
12284626Sstever@eecs.umich.edu     */
12295735Snate@binkert.org    void
123010563Sandreas.hansson@arm.com    writeDataToBlock(uint8_t *blk_data, int blkSize) const
12314626Sstever@eecs.umich.edu    {
12324626Sstever@eecs.umich.edu        writeData(blk_data + getOffset(blkSize));
12334626Sstever@eecs.umich.edu    }
12344626Sstever@eecs.umich.edu
12354626Sstever@eecs.umich.edu    /**
12363348Sbinkertn@umich.edu     * delete the data pointed to in the data pointer. Ok to call to
12373348Sbinkertn@umich.edu     * matter how data was allocted.
12383348Sbinkertn@umich.edu     */
12395735Snate@binkert.org    void
12405735Snate@binkert.org    deleteData()
12415735Snate@binkert.org    {
124210566Sandreas.hansson@arm.com        if (flags.isSet(DYNAMIC_DATA))
12435735Snate@binkert.org            delete [] data;
12445735Snate@binkert.org
124510566Sandreas.hansson@arm.com        flags.clear(STATIC_DATA|DYNAMIC_DATA);
12465735Snate@binkert.org        data = NULL;
12475735Snate@binkert.org    }
12482566SN/A
124910565Sandreas.hansson@arm.com    /** Allocate memory for the packet. */
12505735Snate@binkert.org    void
12515735Snate@binkert.org    allocate()
12525735Snate@binkert.org    {
125311286Sandreas.hansson@arm.com        // if either this command or the response command has a data
125411286Sandreas.hansson@arm.com        // payload, actually allocate space
125511286Sandreas.hansson@arm.com        if (hasData() || hasRespData()) {
125611286Sandreas.hansson@arm.com            assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
125711286Sandreas.hansson@arm.com            flags.set(DYNAMIC_DATA);
125811286Sandreas.hansson@arm.com            data = new uint8_t[getSize()];
125911286Sandreas.hansson@arm.com        }
12605735Snate@binkert.org    }
12615735Snate@binkert.org
126211013Sandreas.sandberg@arm.com    /** @} */
126311013Sandreas.sandberg@arm.com
126411013Sandreas.sandberg@arm.com    /** Get the data in the packet without byte swapping. */
126511013Sandreas.sandberg@arm.com    template <typename T>
126611013Sandreas.sandberg@arm.com    T getRaw() const;
126711013Sandreas.sandberg@arm.com
126811013Sandreas.sandberg@arm.com    /** Set the value in the data pointer to v without byte swapping. */
126911013Sandreas.sandberg@arm.com    template <typename T>
127011013Sandreas.sandberg@arm.com    void setRaw(T v);
127111013Sandreas.sandberg@arm.com
127211013Sandreas.sandberg@arm.com  public:
12734626Sstever@eecs.umich.edu    /**
127410570Sandreas.hansson@arm.com     * Check a functional request against a memory value stored in
127510570Sandreas.hansson@arm.com     * another packet (i.e. an in-transit request or
127610570Sandreas.hansson@arm.com     * response). Returns true if the current packet is a read, and
127710570Sandreas.hansson@arm.com     * the other packet provides the data, which is then copied to the
127810570Sandreas.hansson@arm.com     * current packet. If the current packet is a write, and the other
127910570Sandreas.hansson@arm.com     * packet intersects this one, then we update the data
128010570Sandreas.hansson@arm.com     * accordingly.
128110570Sandreas.hansson@arm.com     */
128210570Sandreas.hansson@arm.com    bool
128312823Srmk35@cl.cam.ac.uk    trySatisfyFunctional(PacketPtr other)
128410570Sandreas.hansson@arm.com    {
128513954Sgiacomo.gabrielli@arm.com        if (other->isMaskedWrite()) {
128613954Sgiacomo.gabrielli@arm.com            // Do not forward data if overlapping with a masked write
128713954Sgiacomo.gabrielli@arm.com            if (_isSecure == other->isSecure() &&
128813954Sgiacomo.gabrielli@arm.com                getAddr() <= (other->getAddr() + other->getSize() - 1) &&
128913954Sgiacomo.gabrielli@arm.com                other->getAddr() <= (getAddr() + getSize() - 1)) {
129013954Sgiacomo.gabrielli@arm.com                warn("Trying to check against a masked write, skipping."
129113954Sgiacomo.gabrielli@arm.com                     " (addr: 0x%x, other addr: 0x%x)", getAddr(),
129213954Sgiacomo.gabrielli@arm.com                     other->getAddr());
129313954Sgiacomo.gabrielli@arm.com            }
129413954Sgiacomo.gabrielli@arm.com            return false;
129513954Sgiacomo.gabrielli@arm.com        }
129610570Sandreas.hansson@arm.com        // all packets that are carrying a payload should have a valid
129710570Sandreas.hansson@arm.com        // data pointer
129812823Srmk35@cl.cam.ac.uk        return trySatisfyFunctional(other, other->getAddr(), other->isSecure(),
129912823Srmk35@cl.cam.ac.uk                                    other->getSize(),
130012823Srmk35@cl.cam.ac.uk                                    other->hasData() ?
130112823Srmk35@cl.cam.ac.uk                                    other->getPtr<uint8_t>() : NULL);
130210570Sandreas.hansson@arm.com    }
130310570Sandreas.hansson@arm.com
130410570Sandreas.hansson@arm.com    /**
130510883Sali.jafri@arm.com     * Does the request need to check for cached copies of the same block
130610883Sali.jafri@arm.com     * in the memory hierarchy above.
130710883Sali.jafri@arm.com     **/
130810883Sali.jafri@arm.com    bool
130910883Sali.jafri@arm.com    mustCheckAbove() const
131010883Sali.jafri@arm.com    {
131111199Sandreas.hansson@arm.com        return cmd == MemCmd::HardPFReq || isEviction();
131211199Sandreas.hansson@arm.com    }
131311199Sandreas.hansson@arm.com
131411199Sandreas.hansson@arm.com    /**
131511199Sandreas.hansson@arm.com     * Is this packet a clean eviction, including both actual clean
131611199Sandreas.hansson@arm.com     * evict packets, but also clean writebacks.
131711199Sandreas.hansson@arm.com     */
131811199Sandreas.hansson@arm.com    bool
131911199Sandreas.hansson@arm.com    isCleanEviction() const
132011199Sandreas.hansson@arm.com    {
132111199Sandreas.hansson@arm.com        return cmd == MemCmd::CleanEvict || cmd == MemCmd::WritebackClean;
132210883Sali.jafri@arm.com    }
132310883Sali.jafri@arm.com
132413954Sgiacomo.gabrielli@arm.com    bool
132513954Sgiacomo.gabrielli@arm.com    isMaskedWrite() const
132613954Sgiacomo.gabrielli@arm.com    {
132713954Sgiacomo.gabrielli@arm.com        return (cmd == MemCmd::WriteReq && !req->getByteEnable().empty());
132813954Sgiacomo.gabrielli@arm.com    }
132913954Sgiacomo.gabrielli@arm.com
133010883Sali.jafri@arm.com    /**
13314626Sstever@eecs.umich.edu     * Check a functional request against a memory value represented
133210570Sandreas.hansson@arm.com     * by a base/size pair and an associated data array. If the
133310570Sandreas.hansson@arm.com     * current packet is a read, it may be satisfied by the memory
133410570Sandreas.hansson@arm.com     * value. If the current packet is a write, it may update the
13354626Sstever@eecs.umich.edu     * memory value.
13364626Sstever@eecs.umich.edu     */
13375735Snate@binkert.org    bool
133812823Srmk35@cl.cam.ac.uk    trySatisfyFunctional(Printable *obj, Addr base, bool is_secure, int size,
133912823Srmk35@cl.cam.ac.uk                         uint8_t *_data);
13405314Sstever@gmail.com
13415315Sstever@gmail.com    /**
13425315Sstever@gmail.com     * Push label for PrintReq (safe to call unconditionally).
13435315Sstever@gmail.com     */
13445735Snate@binkert.org    void
13455735Snate@binkert.org    pushLabel(const std::string &lbl)
13465735Snate@binkert.org    {
13475735Snate@binkert.org        if (isPrint())
13485735Snate@binkert.org            safe_cast<PrintReqState*>(senderState)->pushLabel(lbl);
13495314Sstever@gmail.com    }
13505314Sstever@gmail.com
13515315Sstever@gmail.com    /**
13525315Sstever@gmail.com     * Pop label for PrintReq (safe to call unconditionally).
13535315Sstever@gmail.com     */
13545735Snate@binkert.org    void
13555735Snate@binkert.org    popLabel()
13565735Snate@binkert.org    {
13575735Snate@binkert.org        if (isPrint())
13585735Snate@binkert.org            safe_cast<PrintReqState*>(senderState)->popLabel();
13595314Sstever@gmail.com    }
13605314Sstever@gmail.com
13615314Sstever@gmail.com    void print(std::ostream &o, int verbosity = 0,
13625314Sstever@gmail.com               const std::string &prefix = "") const;
13639663Suri.wiener@arm.com
13649663Suri.wiener@arm.com    /**
13659663Suri.wiener@arm.com     * A no-args wrapper of print(std::ostream...)
13669663Suri.wiener@arm.com     * meant to be invoked from DPRINTFs
13679663Suri.wiener@arm.com     * avoiding string overheads in fast mode
13689663Suri.wiener@arm.com     * @return string with the request's type and start<->end addresses
13699663Suri.wiener@arm.com     */
13709663Suri.wiener@arm.com    std::string print() const;
13712381SN/A};
13722381SN/A
13732381SN/A#endif //__MEM_PACKET_HH
1374