packet.hh revision 3335:71bef174e59f
1/*
2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Ron Dreslinski
29 *          Steve Reinhardt
30 *          Ali Saidi
31 */
32
33/**
34 * @file
35 * Declaration of the Packet class.
36 */
37
38#ifndef __MEM_PACKET_HH__
39#define __MEM_PACKET_HH__
40
41#include "mem/request.hh"
42#include "sim/host.hh"
43#include "sim/root.hh"
44#include <list>
45#include <cassert>
46
47struct Packet;
48typedef Packet* PacketPtr;
49typedef uint8_t* PacketDataPtr;
50typedef std::list<PacketPtr> PacketList;
51
52//Coherence Flags
53#define NACKED_LINE 1 << 0
54#define SATISFIED 1 << 1
55#define SHARED_LINE 1 << 2
56#define CACHE_LINE_FILL 1 << 3
57#define COMPRESSED 1 << 4
58#define NO_ALLOCATE 1 << 5
59#define SNOOP_COMMIT 1 << 6
60
61//for now.  @todo fix later
62#define NUM_MEM_CMDS 1 << 11
63/**
64 * A Packet is used to encapsulate a transfer between two objects in
65 * the memory system (e.g., the L1 and L2 cache).  (In contrast, a
66 * single Request travels all the way from the requester to the
67 * ultimate destination and back, possibly being conveyed by several
68 * different Packets along the way.)
69 */
70class Packet
71{
72  public:
73    /** Temporary FLAGS field until cache gets working, this should be in coherence/sender state. */
74    uint64_t flags;
75
76  private:
77   /** A pointer to the data being transfered.  It can be differnt
78    *    sizes at each level of the heirarchy so it belongs in the
79    *    packet, not request. This may or may not be populated when a
80    *    responder recieves the packet. If not populated it memory
81    *    should be allocated.
82    */
83    PacketDataPtr data;
84
85    /** Is the data pointer set to a value that shouldn't be freed
86     *   when the packet is destroyed? */
87    bool staticData;
88    /** The data pointer points to a value that should be freed when
89     *   the packet is destroyed. */
90    bool dynamicData;
91    /** the data pointer points to an array (thus delete [] ) needs to
92     *   be called on it rather than simply delete.*/
93    bool arrayData;
94
95    /** The address of the request.  This address could be virtual or
96     *   physical, depending on the system configuration. */
97    Addr addr;
98
99     /** The size of the request or transfer. */
100    int size;
101
102    /** Device address (e.g., bus ID) of the source of the
103     *   transaction. The source is not responsible for setting this
104     *   field; it is set implicitly by the interconnect when the
105     *   packet * is first sent.  */
106    short src;
107
108    /** Device address (e.g., bus ID) of the destination of the
109     *   transaction. The special value Broadcast indicates that the
110     *   packet should be routed based on its address. This field is
111     *   initialized in the constructor and is thus always valid
112     *   (unlike * addr, size, and src). */
113    short dest;
114
115    /** Are the 'addr' and 'size' fields valid? */
116    bool addrSizeValid;
117    /** Is the 'src' field valid? */
118    bool srcValid;
119
120
121  public:
122
123    /** Used to calculate latencies for each packet.*/
124    Tick time;
125
126    /** The time at which the packet will be fully transmitted */
127    Tick finishTime;
128
129    /** The time at which the first chunk of the packet will be transmitted */
130    Tick firstWordTime;
131
132    /** The special destination address indicating that the packet
133     *   should be routed based on its address. */
134    static const short Broadcast = -1;
135
136    /** A pointer to the original request. */
137    RequestPtr req;
138
139    /** A virtual base opaque structure used to hold coherence-related
140     *    state.  A specific subclass would be derived from this to
141     *    carry state specific to a particular coherence protocol.  */
142    class CoherenceState {
143      public:
144        virtual ~CoherenceState() {}
145    };
146
147    /** This packet's coherence state.  Caches should use
148     *   dynamic_cast<> to cast to the state appropriate for the
149     *   system's coherence protocol.  */
150    CoherenceState *coherence;
151
152    /** A virtual base opaque structure used to hold state associated
153     *    with the packet but specific to the sending device (e.g., an
154     *    MSHR).  A pointer to this state is returned in the packet's
155     *    response so that the sender can quickly look up the state
156     *    needed to process it.  A specific subclass would be derived
157     *    from this to carry state specific to a particular sending
158     *    device.  */
159    class SenderState {
160      public:
161        virtual ~SenderState() {}
162    };
163
164    /** This packet's sender state.  Devices should use dynamic_cast<>
165     *   to cast to the state appropriate to the sender. */
166    SenderState *senderState;
167
168  private:
169    /** List of command attributes. */
170    // If you add a new CommandAttribute, make sure to increase NUM_MEM_CMDS
171    // as well.
172    enum CommandAttribute
173    {
174        IsRead                = 1 << 0,
175        IsWrite                = 1 << 1,
176        IsPrefetch        = 1 << 2,
177        IsInvalidate        = 1 << 3,
178        IsRequest        = 1 << 4,
179        IsResponse         = 1 << 5,
180        NeedsResponse        = 1 << 6,
181        IsSWPrefetch    = 1 << 7,
182        IsHWPrefetch    = 1 << 8,
183        IsUpgrade       = 1 << 9,
184        HasData                = 1 << 10
185    };
186
187  public:
188    /** List of all commands associated with a packet. */
189    enum Command
190    {
191        InvalidCmd      = 0,
192        ReadReq                = IsRead  | IsRequest | NeedsResponse,
193        WriteReq        = IsWrite | IsRequest | NeedsResponse | HasData,
194        WriteReqNoAck        = IsWrite | IsRequest | HasData,
195        ReadResp        = IsRead  | IsResponse | NeedsResponse | HasData,
196        WriteResp        = IsWrite | IsResponse | NeedsResponse,
197        Writeback       = IsWrite | IsRequest | HasData,
198        SoftPFReq       = IsRead  | IsRequest | IsSWPrefetch | NeedsResponse,
199        HardPFReq       = IsRead  | IsRequest | IsHWPrefetch | NeedsResponse,
200        SoftPFResp      = IsRead  | IsResponse | IsSWPrefetch
201                                | NeedsResponse | HasData,
202        HardPFResp      = IsRead  | IsResponse | IsHWPrefetch
203                                    | NeedsResponse | HasData,
204        InvalidateReq   = IsInvalidate | IsRequest,
205        WriteInvalidateReq = IsWrite | IsInvalidate | IsRequest
206                                   | HasData | NeedsResponse,
207        WriteInvalidateResp = IsWrite | IsInvalidate | IsRequest | NeedsResponse
208                                   | IsResponse,
209        UpgradeReq      = IsInvalidate | IsRequest | IsUpgrade,
210        ReadExReq       = IsRead | IsInvalidate | IsRequest | NeedsResponse,
211        ReadExResp      = IsRead | IsInvalidate | IsResponse
212                                | NeedsResponse | HasData
213    };
214
215    /** Return the string name of the cmd field (for debugging and
216     *   tracing). */
217    const std::string &cmdString() const;
218
219    /** Reutrn the string to a cmd given by idx. */
220    const std::string &cmdIdxToString(Command idx);
221
222    /** Return the index of this command. */
223    inline int cmdToIndex() const { return (int) cmd; }
224
225    /** The command field of the packet. */
226    Command cmd;
227
228    bool isRead() const         { return (cmd & IsRead)  != 0; }
229    bool isWrite()  const       { return (cmd & IsWrite) != 0; }
230    bool isRequest() const      { return (cmd & IsRequest)  != 0; }
231    bool isResponse() const     { return (cmd & IsResponse) != 0; }
232    bool needsResponse() const  { return (cmd & NeedsResponse) != 0; }
233    bool isInvalidate() const   { return (cmd & IsInvalidate) != 0; }
234    bool hasData() const        { return (cmd & HasData) != 0; }
235
236    bool isCacheFill() const    { return (flags & CACHE_LINE_FILL) != 0; }
237    bool isNoAllocate() const   { return (flags & NO_ALLOCATE) != 0; }
238    bool isCompressed() const   { return (flags & COMPRESSED) != 0; }
239
240    bool nic_pkt() { assert("Unimplemented\n" && 0); return false; }
241
242    /** Possible results of a packet's request. */
243    enum Result
244    {
245        Success,
246        BadAddress,
247        Nacked,
248        Unknown
249    };
250
251    /** The result of this packet's request. */
252    Result result;
253
254    /** Accessor function that returns the source index of the packet. */
255    short getSrc() const { assert(srcValid); return src; }
256    void setSrc(short _src) { src = _src; srcValid = true; }
257
258    /** Accessor function that returns the destination index of
259        the packet. */
260    short getDest() const { return dest; }
261    void setDest(short _dest) { dest = _dest; }
262
263    Addr getAddr() const { assert(addrSizeValid); return addr; }
264    int getSize() const { assert(addrSizeValid); return size; }
265    Addr getOffset(int blkSize) const { return addr & (Addr)(blkSize - 1); }
266
267    void addrOverride(Addr newAddr) { assert(addrSizeValid); addr = newAddr; }
268    void cmdOverride(Command newCmd) { cmd = newCmd; }
269
270    /** Constructor.  Note that a Request object must be constructed
271     *   first, but the Requests's physical address and size fields
272     *   need not be valid. The command and destination addresses
273     *   must be supplied.  */
274    Packet(Request *_req, Command _cmd, short _dest)
275        :  data(NULL), staticData(false), dynamicData(false), arrayData(false),
276           addr(_req->paddr), size(_req->size), dest(_dest),
277           addrSizeValid(_req->validPaddr),
278           srcValid(false),
279           req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
280           result(Unknown)
281    {
282        flags = 0;
283        time = curTick;
284    }
285
286    /** Alternate constructor if you are trying to create a packet with
287     *  a request that is for a whole block, not the address from the req.
288     *  this allows for overriding the size/addr of the req.*/
289    Packet(Request *_req, Command _cmd, short _dest, int _blkSize)
290        :  data(NULL), staticData(false), dynamicData(false), arrayData(false),
291           addr(_req->paddr & ~(_blkSize - 1)), size(_blkSize),
292           dest(_dest),
293           addrSizeValid(_req->validPaddr), srcValid(false),
294           req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
295           result(Unknown)
296    {
297        flags = 0;
298        time = curTick;
299    }
300
301    /** Destructor. */
302    ~Packet()
303    { deleteData(); }
304
305    /** Reinitialize packet address and size from the associated
306     *   Request object, and reset other fields that may have been
307     *   modified by a previous transaction.  Typically called when a
308     *   statically allocated Request/Packet pair is reused for
309     *   multiple transactions. */
310    void reinitFromRequest() {
311        assert(req->validPaddr);
312        addr = req->paddr;
313        size = req->size;
314        time = req->time;
315        addrSizeValid = true;
316        result = Unknown;
317        if (dynamicData) {
318            deleteData();
319            dynamicData = false;
320            arrayData = false;
321        }
322    }
323
324    /** Take a request packet and modify it in place to be suitable
325     *   for returning as a response to that request.  Used for timing
326     *   accesses only.  For atomic and functional accesses, the
327     *   request packet is always implicitly passed back *without*
328     *   modifying the destination fields, so this function
329     *   should not be called. */
330    void makeTimingResponse() {
331        assert(needsResponse());
332        assert(isRequest());
333        int icmd = (int)cmd;
334        icmd &= ~(IsRequest);
335        icmd |= IsResponse;
336        if (isRead())
337            icmd |= HasData;
338        if (isWrite())
339            icmd &= ~HasData;
340        cmd = (Command)icmd;
341        dest = src;
342        srcValid = false;
343    }
344
345    /** Take a request packet and modify it in place to be suitable
346     *   for returning as a response to that request.
347     */
348    void makeAtomicResponse() {
349        assert(needsResponse());
350        assert(isRequest());
351        int icmd = (int)cmd;
352        icmd &= ~(IsRequest);
353        icmd |= IsResponse;
354        if (isRead())
355            icmd |= HasData;
356        if (isWrite())
357            icmd &= ~HasData;
358        cmd = (Command)icmd;
359    }
360
361    /** Take a request packet that has been returned as NACKED and modify it so
362     * that it can be sent out again. Only packets that need a response can be
363     * NACKED, so verify that that is true. */
364    void reinitNacked() {
365        assert(needsResponse() && result == Nacked);
366        dest =  Broadcast;
367        result = Unknown;
368    }
369
370
371    /** Set the data pointer to the following value that should not be freed. */
372    template <typename T>
373    void dataStatic(T *p);
374
375    /** Set the data pointer to a value that should have delete [] called on it.
376     */
377    template <typename T>
378    void dataDynamicArray(T *p);
379
380    /** set the data pointer to a value that should have delete called on it. */
381    template <typename T>
382    void dataDynamic(T *p);
383
384    /** return the value of what is pointed to in the packet. */
385    template <typename T>
386    T get();
387
388    /** get a pointer to the data ptr. */
389    template <typename T>
390    T* getPtr();
391
392    /** set the value in the data pointer to v. */
393    template <typename T>
394    void set(T v);
395
396    /** delete the data pointed to in the data pointer. Ok to call to matter how
397     * data was allocted. */
398    void deleteData();
399
400    /** If there isn't data in the packet, allocate some. */
401    void allocate();
402
403    /** Do the packet modify the same addresses. */
404    bool intersect(Packet *p);
405};
406
407
408/** This function given a functional packet and a timing packet either satisfies
409 * the timing packet, or updates the timing packet to reflect the updated state
410 * in the timing packet. It returns if the functional packet should continue to
411 * traverse the memory hierarchy or not.
412 */
413bool fixPacket(Packet *func, Packet *timing);
414
415std::ostream & operator<<(std::ostream &o, const Packet &p);
416
417#endif //__MEM_PACKET_HH
418