packet.hh revision 3348
112837Sgabeblack@google.com/*
212837Sgabeblack@google.com * Copyright (c) 2006 The Regents of The University of Michigan
312837Sgabeblack@google.com * All rights reserved.
412837Sgabeblack@google.com *
512837Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
612837Sgabeblack@google.com * modification, are permitted provided that the following conditions are
712837Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
812837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
912837Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1012837Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1112837Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1212837Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1312837Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1412837Sgabeblack@google.com * this software without specific prior written permission.
1512837Sgabeblack@google.com *
1612837Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712837Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812837Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912837Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012837Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112837Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212837Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312837Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412837Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512837Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612837Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712837Sgabeblack@google.com *
2812837Sgabeblack@google.com * Authors: Ron Dreslinski
2912837Sgabeblack@google.com *          Steve Reinhardt
3012837Sgabeblack@google.com *          Ali Saidi
3112837Sgabeblack@google.com */
3212837Sgabeblack@google.com
3312837Sgabeblack@google.com/**
3412837Sgabeblack@google.com * @file
3512837Sgabeblack@google.com * Declaration of the Packet class.
3612837Sgabeblack@google.com */
3712837Sgabeblack@google.com
3812837Sgabeblack@google.com#ifndef __MEM_PACKET_HH__
3912837Sgabeblack@google.com#define __MEM_PACKET_HH__
4012837Sgabeblack@google.com
4112837Sgabeblack@google.com#include <cassert>
4212837Sgabeblack@google.com#include <list>
4312837Sgabeblack@google.com
4412837Sgabeblack@google.com#include "mem/request.hh"
4512837Sgabeblack@google.com#include "sim/host.hh"
4612837Sgabeblack@google.com#include "sim/root.hh"
4712837Sgabeblack@google.com
4812837Sgabeblack@google.comstruct Packet;
4912837Sgabeblack@google.comtypedef Packet* PacketPtr;
5012837Sgabeblack@google.comtypedef uint8_t* PacketDataPtr;
5112837Sgabeblack@google.comtypedef std::list<PacketPtr> PacketList;
5212837Sgabeblack@google.com
5312837Sgabeblack@google.com//Coherence Flags
5412837Sgabeblack@google.com#define NACKED_LINE 1 << 0
5512837Sgabeblack@google.com#define SATISFIED 1 << 1
5612837Sgabeblack@google.com#define SHARED_LINE 1 << 2
5712837Sgabeblack@google.com#define CACHE_LINE_FILL 1 << 3
5812837Sgabeblack@google.com#define COMPRESSED 1 << 4
5912837Sgabeblack@google.com#define NO_ALLOCATE 1 << 5
6012837Sgabeblack@google.com#define SNOOP_COMMIT 1 << 6
6112837Sgabeblack@google.com
6212837Sgabeblack@google.com//for now.  @todo fix later
6312837Sgabeblack@google.com#define NUM_MEM_CMDS 1 << 11
6412837Sgabeblack@google.com/**
6512837Sgabeblack@google.com * A Packet is used to encapsulate a transfer between two objects in
6612837Sgabeblack@google.com * the memory system (e.g., the L1 and L2 cache).  (In contrast, a
6712837Sgabeblack@google.com * single Request travels all the way from the requester to the
6812837Sgabeblack@google.com * ultimate destination and back, possibly being conveyed by several
6912837Sgabeblack@google.com * different Packets along the way.)
7012837Sgabeblack@google.com */
7112837Sgabeblack@google.comclass Packet
7212837Sgabeblack@google.com{
7312837Sgabeblack@google.com  public:
7412837Sgabeblack@google.com    /** Temporary FLAGS field until cache gets working, this should be in coherence/sender state. */
7512837Sgabeblack@google.com    uint64_t flags;
7612837Sgabeblack@google.com
7712837Sgabeblack@google.com  private:
7812837Sgabeblack@google.com   /** A pointer to the data being transfered.  It can be differnt
7912837Sgabeblack@google.com    *    sizes at each level of the heirarchy so it belongs in the
8012837Sgabeblack@google.com    *    packet, not request. This may or may not be populated when a
8112837Sgabeblack@google.com    *    responder recieves the packet. If not populated it memory
8212837Sgabeblack@google.com    *    should be allocated.
8312837Sgabeblack@google.com    */
8412837Sgabeblack@google.com    PacketDataPtr data;
8512837Sgabeblack@google.com
8612837Sgabeblack@google.com    /** Is the data pointer set to a value that shouldn't be freed
8712837Sgabeblack@google.com     *   when the packet is destroyed? */
8812837Sgabeblack@google.com    bool staticData;
8912837Sgabeblack@google.com    /** The data pointer points to a value that should be freed when
9012837Sgabeblack@google.com     *   the packet is destroyed. */
9112837Sgabeblack@google.com    bool dynamicData;
9212837Sgabeblack@google.com    /** the data pointer points to an array (thus delete [] ) needs to
9312837Sgabeblack@google.com     *   be called on it rather than simply delete.*/
9412837Sgabeblack@google.com    bool arrayData;
9512837Sgabeblack@google.com
9612837Sgabeblack@google.com    /** The address of the request.  This address could be virtual or
9712837Sgabeblack@google.com     *   physical, depending on the system configuration. */
9812837Sgabeblack@google.com    Addr addr;
9912837Sgabeblack@google.com
10012837Sgabeblack@google.com     /** The size of the request or transfer. */
10112837Sgabeblack@google.com    int size;
10212837Sgabeblack@google.com
10312837Sgabeblack@google.com    /** Device address (e.g., bus ID) of the source of the
10412837Sgabeblack@google.com     *   transaction. The source is not responsible for setting this
10512837Sgabeblack@google.com     *   field; it is set implicitly by the interconnect when the
10612837Sgabeblack@google.com     *   packet * is first sent.  */
10712837Sgabeblack@google.com    short src;
10812837Sgabeblack@google.com
10912837Sgabeblack@google.com    /** Device address (e.g., bus ID) of the destination of the
11012837Sgabeblack@google.com     *   transaction. The special value Broadcast indicates that the
11112837Sgabeblack@google.com     *   packet should be routed based on its address. This field is
11212837Sgabeblack@google.com     *   initialized in the constructor and is thus always valid
11312837Sgabeblack@google.com     *   (unlike * addr, size, and src). */
11412837Sgabeblack@google.com    short dest;
11512837Sgabeblack@google.com
11612837Sgabeblack@google.com    /** Are the 'addr' and 'size' fields valid? */
11712837Sgabeblack@google.com    bool addrSizeValid;
11812837Sgabeblack@google.com    /** Is the 'src' field valid? */
11912837Sgabeblack@google.com    bool srcValid;
12012837Sgabeblack@google.com
12112837Sgabeblack@google.com
12212837Sgabeblack@google.com  public:
12312837Sgabeblack@google.com
12412837Sgabeblack@google.com    /** Used to calculate latencies for each packet.*/
12512837Sgabeblack@google.com    Tick time;
12612837Sgabeblack@google.com
12712837Sgabeblack@google.com    /** The time at which the packet will be fully transmitted */
12812837Sgabeblack@google.com    Tick finishTime;
12912837Sgabeblack@google.com
13012837Sgabeblack@google.com    /** The time at which the first chunk of the packet will be transmitted */
13112837Sgabeblack@google.com    Tick firstWordTime;
13212837Sgabeblack@google.com
13312837Sgabeblack@google.com    /** The special destination address indicating that the packet
13412837Sgabeblack@google.com     *   should be routed based on its address. */
13512837Sgabeblack@google.com    static const short Broadcast = -1;
13612837Sgabeblack@google.com
13712837Sgabeblack@google.com    /** A pointer to the original request. */
13812837Sgabeblack@google.com    RequestPtr req;
13912837Sgabeblack@google.com
14012837Sgabeblack@google.com    /** A virtual base opaque structure used to hold coherence-related
14112837Sgabeblack@google.com     *    state.  A specific subclass would be derived from this to
14212837Sgabeblack@google.com     *    carry state specific to a particular coherence protocol.  */
14312837Sgabeblack@google.com    class CoherenceState {
14412864Sgabeblack@google.com      public:
14512864Sgabeblack@google.com        virtual ~CoherenceState() {}
14612864Sgabeblack@google.com    };
14712837Sgabeblack@google.com
14812837Sgabeblack@google.com    /** This packet's coherence state.  Caches should use
14912837Sgabeblack@google.com     *   dynamic_cast<> to cast to the state appropriate for the
15012837Sgabeblack@google.com     *   system's coherence protocol.  */
15112837Sgabeblack@google.com    CoherenceState *coherence;
15212837Sgabeblack@google.com
15312837Sgabeblack@google.com    /** A virtual base opaque structure used to hold state associated
15412837Sgabeblack@google.com     *    with the packet but specific to the sending device (e.g., an
15512837Sgabeblack@google.com     *    MSHR).  A pointer to this state is returned in the packet's
15612837Sgabeblack@google.com     *    response so that the sender can quickly look up the state
15712837Sgabeblack@google.com     *    needed to process it.  A specific subclass would be derived
15812837Sgabeblack@google.com     *    from this to carry state specific to a particular sending
15912837Sgabeblack@google.com     *    device.  */
16012837Sgabeblack@google.com    class SenderState {
16112837Sgabeblack@google.com      public:
16212837Sgabeblack@google.com        virtual ~SenderState() {}
16312837Sgabeblack@google.com    };
16412837Sgabeblack@google.com
16512837Sgabeblack@google.com    /** This packet's sender state.  Devices should use dynamic_cast<>
16612837Sgabeblack@google.com     *   to cast to the state appropriate to the sender. */
16712837Sgabeblack@google.com    SenderState *senderState;
16812837Sgabeblack@google.com
16912837Sgabeblack@google.com  private:
17012837Sgabeblack@google.com    /** List of command attributes. */
17112837Sgabeblack@google.com    // If you add a new CommandAttribute, make sure to increase NUM_MEM_CMDS
17212837Sgabeblack@google.com    // as well.
17312837Sgabeblack@google.com    enum CommandAttribute
17412837Sgabeblack@google.com    {
17512837Sgabeblack@google.com        IsRead                = 1 << 0,
17612837Sgabeblack@google.com        IsWrite                = 1 << 1,
17712837Sgabeblack@google.com        IsPrefetch        = 1 << 2,
17812837Sgabeblack@google.com        IsInvalidate        = 1 << 3,
17912837Sgabeblack@google.com        IsRequest        = 1 << 4,
18012837Sgabeblack@google.com        IsResponse         = 1 << 5,
18112837Sgabeblack@google.com        NeedsResponse        = 1 << 6,
18212837Sgabeblack@google.com        IsSWPrefetch    = 1 << 7,
18312837Sgabeblack@google.com        IsHWPrefetch    = 1 << 8,
18412837Sgabeblack@google.com        IsUpgrade       = 1 << 9,
18512837Sgabeblack@google.com        HasData                = 1 << 10
18612837Sgabeblack@google.com    };
18712837Sgabeblack@google.com
18812837Sgabeblack@google.com  public:
18912837Sgabeblack@google.com    /** List of all commands associated with a packet. */
19012837Sgabeblack@google.com    enum Command
19112837Sgabeblack@google.com    {
19212837Sgabeblack@google.com        InvalidCmd      = 0,
19312837Sgabeblack@google.com        ReadReq                = IsRead  | IsRequest | NeedsResponse,
19412837Sgabeblack@google.com        WriteReq        = IsWrite | IsRequest | NeedsResponse | HasData,
19512837Sgabeblack@google.com        WriteReqNoAck        = IsWrite | IsRequest | HasData,
19612837Sgabeblack@google.com        ReadResp        = IsRead  | IsResponse | NeedsResponse | HasData,
19712837Sgabeblack@google.com        WriteResp        = IsWrite | IsResponse | NeedsResponse,
19812837Sgabeblack@google.com        Writeback       = IsWrite | IsRequest | HasData,
19912837Sgabeblack@google.com        SoftPFReq       = IsRead  | IsRequest | IsSWPrefetch | NeedsResponse,
20012837Sgabeblack@google.com        HardPFReq       = IsRead  | IsRequest | IsHWPrefetch | NeedsResponse,
20112837Sgabeblack@google.com        SoftPFResp      = IsRead  | IsResponse | IsSWPrefetch
20212837Sgabeblack@google.com                                | NeedsResponse | HasData,
20312837Sgabeblack@google.com        HardPFResp      = IsRead  | IsResponse | IsHWPrefetch
20412837Sgabeblack@google.com                                    | NeedsResponse | HasData,
20512837Sgabeblack@google.com        InvalidateReq   = IsInvalidate | IsRequest,
20612837Sgabeblack@google.com        WriteInvalidateReq = IsWrite | IsInvalidate | IsRequest
20712837Sgabeblack@google.com                                   | HasData | NeedsResponse,
20812837Sgabeblack@google.com        WriteInvalidateResp = IsWrite | IsInvalidate | IsRequest | NeedsResponse
20912837Sgabeblack@google.com                                   | IsResponse,
21012837Sgabeblack@google.com        UpgradeReq      = IsInvalidate | IsRequest | IsUpgrade,
21112837Sgabeblack@google.com        ReadExReq       = IsRead | IsInvalidate | IsRequest | NeedsResponse,
21212837Sgabeblack@google.com        ReadExResp      = IsRead | IsInvalidate | IsResponse
21312837Sgabeblack@google.com                                | NeedsResponse | HasData
21412837Sgabeblack@google.com    };
21512837Sgabeblack@google.com
21612837Sgabeblack@google.com    /** Return the string name of the cmd field (for debugging and
21712837Sgabeblack@google.com     *   tracing). */
21812837Sgabeblack@google.com    const std::string &cmdString() const;
21912837Sgabeblack@google.com
22012837Sgabeblack@google.com    /** Reutrn the string to a cmd given by idx. */
22112837Sgabeblack@google.com    const std::string &cmdIdxToString(Command idx);
22212837Sgabeblack@google.com
22312837Sgabeblack@google.com    /** Return the index of this command. */
22412837Sgabeblack@google.com    inline int cmdToIndex() const { return (int) cmd; }
22512837Sgabeblack@google.com
22612837Sgabeblack@google.com    /** The command field of the packet. */
22712837Sgabeblack@google.com    Command cmd;
22812837Sgabeblack@google.com
22912837Sgabeblack@google.com    bool isRead() const         { return (cmd & IsRead)  != 0; }
23012837Sgabeblack@google.com    bool isWrite()  const       { return (cmd & IsWrite) != 0; }
23112837Sgabeblack@google.com    bool isRequest() const      { return (cmd & IsRequest)  != 0; }
23212837Sgabeblack@google.com    bool isResponse() const     { return (cmd & IsResponse) != 0; }
23312837Sgabeblack@google.com    bool needsResponse() const  { return (cmd & NeedsResponse) != 0; }
23412837Sgabeblack@google.com    bool isInvalidate() const   { return (cmd & IsInvalidate) != 0; }
23512837Sgabeblack@google.com    bool hasData() const        { return (cmd & HasData) != 0; }
23612837Sgabeblack@google.com
23712837Sgabeblack@google.com    bool isCacheFill() const    { return (flags & CACHE_LINE_FILL) != 0; }
23812837Sgabeblack@google.com    bool isNoAllocate() const   { return (flags & NO_ALLOCATE) != 0; }
23912837Sgabeblack@google.com    bool isCompressed() const   { return (flags & COMPRESSED) != 0; }
24012837Sgabeblack@google.com
24112837Sgabeblack@google.com    bool nic_pkt() { assert("Unimplemented\n" && 0); return false; }
24212837Sgabeblack@google.com
24312837Sgabeblack@google.com    /** Possible results of a packet's request. */
24412837Sgabeblack@google.com    enum Result
24512837Sgabeblack@google.com    {
24612837Sgabeblack@google.com        Success,
24712901Sgabeblack@google.com        BadAddress,
24812901Sgabeblack@google.com        Nacked,
24912901Sgabeblack@google.com        Unknown
25012901Sgabeblack@google.com    };
25112901Sgabeblack@google.com
25212901Sgabeblack@google.com    /** The result of this packet's request. */
25312837Sgabeblack@google.com    Result result;
25412837Sgabeblack@google.com
25512837Sgabeblack@google.com    /** Accessor function that returns the source index of the packet. */
256    short getSrc() const { assert(srcValid); return src; }
257    void setSrc(short _src) { src = _src; srcValid = true; }
258
259    /** Accessor function that returns the destination index of
260        the packet. */
261    short getDest() const { return dest; }
262    void setDest(short _dest) { dest = _dest; }
263
264    Addr getAddr() const { assert(addrSizeValid); return addr; }
265    int getSize() const { assert(addrSizeValid); return size; }
266    Addr getOffset(int blkSize) const { return addr & (Addr)(blkSize - 1); }
267
268    void addrOverride(Addr newAddr) { assert(addrSizeValid); addr = newAddr; }
269    void cmdOverride(Command newCmd) { cmd = newCmd; }
270
271    /** Constructor.  Note that a Request object must be constructed
272     *   first, but the Requests's physical address and size fields
273     *   need not be valid. The command and destination addresses
274     *   must be supplied.  */
275    Packet(Request *_req, Command _cmd, short _dest)
276        :  data(NULL), staticData(false), dynamicData(false), arrayData(false),
277           addr(_req->paddr), size(_req->size), dest(_dest),
278           addrSizeValid(_req->validPaddr),
279           srcValid(false),
280           req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
281           result(Unknown)
282    {
283        flags = 0;
284        time = curTick;
285    }
286
287    /** Alternate constructor if you are trying to create a packet with
288     *  a request that is for a whole block, not the address from the req.
289     *  this allows for overriding the size/addr of the req.*/
290    Packet(Request *_req, Command _cmd, short _dest, int _blkSize)
291        :  data(NULL), staticData(false), dynamicData(false), arrayData(false),
292           addr(_req->paddr & ~(_blkSize - 1)), size(_blkSize),
293           dest(_dest),
294           addrSizeValid(_req->validPaddr), srcValid(false),
295           req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
296           result(Unknown)
297    {
298        flags = 0;
299        time = curTick;
300    }
301
302    /** Destructor. */
303    ~Packet()
304    { deleteData(); }
305
306    /** Reinitialize packet address and size from the associated
307     *   Request object, and reset other fields that may have been
308     *   modified by a previous transaction.  Typically called when a
309     *   statically allocated Request/Packet pair is reused for
310     *   multiple transactions. */
311    void reinitFromRequest() {
312        assert(req->validPaddr);
313        addr = req->paddr;
314        size = req->size;
315        time = req->time;
316        addrSizeValid = true;
317        result = Unknown;
318        if (dynamicData) {
319            deleteData();
320            dynamicData = false;
321            arrayData = false;
322        }
323    }
324
325    /** Take a request packet and modify it in place to be suitable
326     *   for returning as a response to that request.  Used for timing
327     *   accesses only.  For atomic and functional accesses, the
328     *   request packet is always implicitly passed back *without*
329     *   modifying the destination fields, so this function
330     *   should not be called. */
331    void makeTimingResponse() {
332        assert(needsResponse());
333        assert(isRequest());
334        int icmd = (int)cmd;
335        icmd &= ~(IsRequest);
336        icmd |= IsResponse;
337        if (isRead())
338            icmd |= HasData;
339        if (isWrite())
340            icmd &= ~HasData;
341        cmd = (Command)icmd;
342        dest = src;
343        srcValid = false;
344    }
345
346    /**
347     * Take a request packet and modify it in place to be suitable for
348     * returning as a response to that request.
349     */
350    void makeAtomicResponse()
351    {
352        assert(needsResponse());
353        assert(isRequest());
354        int icmd = (int)cmd;
355        icmd &= ~(IsRequest);
356        icmd |= IsResponse;
357        if (isRead())
358            icmd |= HasData;
359        if (isWrite())
360            icmd &= ~HasData;
361        cmd = (Command)icmd;
362    }
363
364    /**
365     * Take a request packet that has been returned as NACKED and
366     * modify it so that it can be sent out again. Only packets that
367     * need a response can be NACKED, so verify that that is true.
368     */
369    void
370    reinitNacked()
371    {
372        assert(needsResponse() && result == Nacked);
373        dest =  Broadcast;
374        result = Unknown;
375    }
376
377
378    /**
379     * Set the data pointer to the following value that should not be
380     * freed.
381     */
382    template <typename T>
383    void
384    dataStatic(T *p)
385    {
386        if(dynamicData)
387            dynamicData = false;
388        data = (PacketDataPtr)p;
389        staticData = true;
390    }
391
392    /**
393     * Set the data pointer to a value that should have delete []
394     * called on it.
395     */
396    template <typename T>
397    void
398    dataDynamicArray(T *p)
399    {
400        assert(!staticData && !dynamicData);
401        data = (PacketDataPtr)p;
402        dynamicData = true;
403        arrayData = true;
404    }
405
406    /**
407     * set the data pointer to a value that should have delete called
408     * on it.
409     */
410    template <typename T>
411    void
412    dataDynamic(T *p)
413    {
414        assert(!staticData && !dynamicData);
415        data = (PacketDataPtr)p;
416        dynamicData = true;
417        arrayData = false;
418    }
419
420    /** get a pointer to the data ptr. */
421    template <typename T>
422    T*
423    getPtr()
424    {
425        assert(staticData || dynamicData);
426        return (T*)data;
427    }
428
429    /** return the value of what is pointed to in the packet. */
430    template <typename T>
431    T get();
432
433    /** set the value in the data pointer to v. */
434    template <typename T>
435    void set(T v);
436
437    /**
438     * delete the data pointed to in the data pointer. Ok to call to
439     * matter how data was allocted.
440     */
441    void deleteData();
442
443    /** If there isn't data in the packet, allocate some. */
444    void allocate();
445
446    /** Do the packet modify the same addresses. */
447    bool intersect(Packet *p);
448};
449
450
451/** This function given a functional packet and a timing packet either satisfies
452 * the timing packet, or updates the timing packet to reflect the updated state
453 * in the timing packet. It returns if the functional packet should continue to
454 * traverse the memory hierarchy or not.
455 */
456bool fixPacket(Packet *func, Packet *timing);
457
458std::ostream & operator<<(std::ostream &o, const Packet &p);
459
460#endif //__MEM_PACKET_HH
461