packet.hh revision 3940:b87f85bb4275
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 <cassert>
42#include <list>
43
44#include "base/misc.hh"
45#include "mem/request.hh"
46#include "sim/host.hh"
47#include "sim/root.hh"
48
49
50struct Packet;
51typedef Packet *PacketPtr;
52typedef uint8_t* PacketDataPtr;
53typedef std::list<PacketPtr> PacketList;
54
55//Coherence Flags
56#define NACKED_LINE     (1 << 0)
57#define SATISFIED       (1 << 1)
58#define SHARED_LINE     (1 << 2)
59#define CACHE_LINE_FILL (1 << 3)
60#define COMPRESSED      (1 << 4)
61#define NO_ALLOCATE     (1 << 5)
62#define SNOOP_COMMIT    (1 << 6)
63
64//for now.  @todo fix later
65#define NUM_MEM_CMDS    (1 << 11)
66/**
67 * A Packet is used to encapsulate a transfer between two objects in
68 * the memory system (e.g., the L1 and L2 cache).  (In contrast, a
69 * single Request travels all the way from the requester to the
70 * ultimate destination and back, possibly being conveyed by several
71 * different Packets along the way.)
72 */
73class Packet
74{
75  public:
76    /** Temporary FLAGS field until cache gets working, this should be in coherence/sender state. */
77    uint64_t flags;
78
79  private:
80   /** A pointer to the data being transfered.  It can be differnt
81    *    sizes at each level of the heirarchy so it belongs in the
82    *    packet, not request. This may or may not be populated when a
83    *    responder recieves the packet. If not populated it memory
84    *    should be allocated.
85    */
86    PacketDataPtr data;
87
88    /** Is the data pointer set to a value that shouldn't be freed
89     *   when the packet is destroyed? */
90    bool staticData;
91    /** The data pointer points to a value that should be freed when
92     *   the packet is destroyed. */
93    bool dynamicData;
94    /** the data pointer points to an array (thus delete [] ) needs to
95     *   be called on it rather than simply delete.*/
96    bool arrayData;
97
98    /** The address of the request.  This address could be virtual or
99     *   physical, depending on the system configuration. */
100    Addr addr;
101
102     /** The size of the request or transfer. */
103    int size;
104
105    /** Device address (e.g., bus ID) of the source of the
106     *   transaction. The source is not responsible for setting this
107     *   field; it is set implicitly by the interconnect when the
108     *   packet is first sent.  */
109    short src;
110
111    /** Device address (e.g., bus ID) of the destination of the
112     *   transaction. The special value Broadcast indicates that the
113     *   packet should be routed based on its address. This field is
114     *   initialized in the constructor and is thus always valid
115     *   (unlike * addr, size, and src). */
116    short dest;
117
118    /** Are the 'addr' and 'size' fields valid? */
119    bool addrSizeValid;
120    /** Is the 'src' field valid? */
121    bool srcValid;
122
123
124  public:
125
126    /** Used to calculate latencies for each packet.*/
127    Tick time;
128
129    /** The time at which the packet will be fully transmitted */
130    Tick finishTime;
131
132    /** The time at which the first chunk of the packet will be transmitted */
133    Tick firstWordTime;
134
135    /** The special destination address indicating that the packet
136     *   should be routed based on its address. */
137    static const short Broadcast = -1;
138
139    /** A pointer to the original request. */
140    RequestPtr req;
141
142    /** A virtual base opaque structure used to hold coherence-related
143     *    state.  A specific subclass would be derived from this to
144     *    carry state specific to a particular coherence protocol.  */
145    class CoherenceState {
146      public:
147        virtual ~CoherenceState() {}
148    };
149
150    /** This packet's coherence state.  Caches should use
151     *   dynamic_cast<> to cast to the state appropriate for the
152     *   system's coherence protocol.  */
153    CoherenceState *coherence;
154
155    /** A virtual base opaque structure used to hold state associated
156     *    with the packet but specific to the sending device (e.g., an
157     *    MSHR).  A pointer to this state is returned in the packet's
158     *    response so that the sender can quickly look up the state
159     *    needed to process it.  A specific subclass would be derived
160     *    from this to carry state specific to a particular sending
161     *    device.  */
162    class SenderState {
163      public:
164        virtual ~SenderState() {}
165    };
166
167    /** This packet's sender state.  Devices should use dynamic_cast<>
168     *   to cast to the state appropriate to the sender. */
169    SenderState *senderState;
170
171  private:
172    /** List of command attributes. */
173    // If you add a new CommandAttribute, make sure to increase NUM_MEM_CMDS
174    // as well.
175    enum CommandAttribute
176    {
177        IsRead          = 1 << 0,
178        IsWrite         = 1 << 1,
179        IsPrefetch      = 1 << 2,
180        IsInvalidate    = 1 << 3,
181        IsRequest       = 1 << 4,
182        IsResponse      = 1 << 5,
183        NeedsResponse   = 1 << 6,
184        IsSWPrefetch    = 1 << 7,
185        IsHWPrefetch    = 1 << 8,
186        IsUpgrade       = 1 << 9,
187        HasData         = 1 << 10
188    };
189
190  public:
191    /** List of all commands associated with a packet. */
192    enum Command
193    {
194        InvalidCmd      = 0,
195        ReadReq         = IsRead  | IsRequest | NeedsResponse,
196        WriteReq        = IsWrite | IsRequest | NeedsResponse | HasData,
197        WriteReqNoAck   = IsWrite | IsRequest | HasData,
198        ReadResp        = IsRead  | IsResponse | NeedsResponse | HasData,
199        WriteResp       = IsWrite | IsResponse | NeedsResponse,
200        Writeback       = IsWrite | IsRequest | HasData,
201        SoftPFReq       = IsRead  | IsRequest | IsSWPrefetch | NeedsResponse,
202        HardPFReq       = IsRead  | IsRequest | IsHWPrefetch | NeedsResponse,
203        SoftPFResp      = IsRead  | IsResponse | IsSWPrefetch
204                                  | NeedsResponse | HasData,
205        HardPFResp      = IsRead  | IsResponse | IsHWPrefetch
206                                  | NeedsResponse | HasData,
207        InvalidateReq   = IsInvalidate | IsRequest,
208        WriteInvalidateReq  = IsWrite | IsInvalidate | IsRequest
209                                      | HasData | NeedsResponse,
210        WriteInvalidateResp = IsWrite | IsInvalidate | IsRequest
211                                      | NeedsResponse | IsResponse,
212        UpgradeReq      = IsInvalidate | IsRequest | IsUpgrade,
213        ReadExReq       = IsRead | IsInvalidate | IsRequest | NeedsResponse,
214        ReadExResp      = IsRead | IsInvalidate | IsResponse
215                                 | NeedsResponse | HasData
216    };
217
218    /** Return the string name of the cmd field (for debugging and
219     *   tracing). */
220    const std::string &cmdString() const;
221
222    /** Reutrn the string to a cmd given by idx. */
223    const std::string &cmdIdxToString(Command idx);
224
225    /** Return the index of this command. */
226    inline int cmdToIndex() const { return (int) cmd; }
227
228    /** The command field of the packet. */
229    Command cmd;
230
231    bool isRead() const         { return (cmd & IsRead)  != 0; }
232    bool isWrite()  const       { return (cmd & IsWrite) != 0; }
233    bool isRequest() const      { return (cmd & IsRequest)  != 0; }
234    bool isResponse() const     { return (cmd & IsResponse) != 0; }
235    bool needsResponse() const  { return (cmd & NeedsResponse) != 0; }
236    bool isInvalidate() const   { return (cmd & IsInvalidate) != 0; }
237    bool hasData() const        { return (cmd & HasData) != 0; }
238
239    bool isCacheFill() const    { return (flags & CACHE_LINE_FILL) != 0; }
240    bool isNoAllocate() const   { return (flags & NO_ALLOCATE) != 0; }
241    bool isCompressed() const   { return (flags & COMPRESSED) != 0; }
242
243    bool nic_pkt() { panic("Unimplemented"); M5_DUMMY_RETURN }
244
245    /** Possible results of a packet's request. */
246    enum Result
247    {
248        Success,
249        BadAddress,
250        Nacked,
251        Unknown
252    };
253
254    /** The result of this packet's request. */
255    Result result;
256
257    /** Accessor function that returns the source index of the packet. */
258    short getSrc() const { assert(srcValid); return src; }
259    void setSrc(short _src) { src = _src; srcValid = true; }
260
261    /** Accessor function that returns the destination index of
262        the packet. */
263    short getDest() const { return dest; }
264    void setDest(short _dest) { dest = _dest; }
265
266    Addr getAddr() const { assert(addrSizeValid); return addr; }
267    int getSize() const { assert(addrSizeValid); return size; }
268    Addr getOffset(int blkSize) const { return addr & (Addr)(blkSize - 1); }
269
270    void addrOverride(Addr newAddr) { assert(addrSizeValid); addr = newAddr; }
271    void cmdOverride(Command newCmd) { cmd = newCmd; }
272
273    /** Constructor.  Note that a Request object must be constructed
274     *   first, but the Requests's physical address and size fields
275     *   need not be valid. The command and destination addresses
276     *   must be supplied.  */
277    Packet(Request *_req, Command _cmd, short _dest)
278        :  data(NULL), staticData(false), dynamicData(false), arrayData(false),
279           addr(_req->paddr), size(_req->size), dest(_dest),
280           addrSizeValid(_req->validPaddr),
281           srcValid(false),
282           req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
283           result(Unknown)
284    {
285        flags = 0;
286        time = curTick;
287    }
288
289    /** Alternate constructor if you are trying to create a packet with
290     *  a request that is for a whole block, not the address from the req.
291     *  this allows for overriding the size/addr of the req.*/
292    Packet(Request *_req, Command _cmd, short _dest, int _blkSize)
293        :  data(NULL), staticData(false), dynamicData(false), arrayData(false),
294           addr(_req->paddr & ~(_blkSize - 1)), size(_blkSize),
295           dest(_dest),
296           addrSizeValid(_req->validPaddr), srcValid(false),
297           req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
298           result(Unknown)
299    {
300        flags = 0;
301        time = curTick;
302    }
303
304    /** Destructor. */
305    ~Packet()
306    { if (staticData || dynamicData) deleteData(); }
307
308    /** Reinitialize packet address and size from the associated
309     *   Request object, and reset other fields that may have been
310     *   modified by a previous transaction.  Typically called when a
311     *   statically allocated Request/Packet pair is reused for
312     *   multiple transactions. */
313    void reinitFromRequest() {
314        assert(req->validPaddr);
315        flags = 0;
316        addr = req->paddr;
317        size = req->size;
318        time = req->time;
319        addrSizeValid = true;
320        result = Unknown;
321        if (dynamicData) {
322            deleteData();
323            dynamicData = false;
324            arrayData = false;
325        }
326    }
327
328    /** Take a request packet and modify it in place to be suitable
329     *   for returning as a response to that request.  Used for timing
330     *   accesses only.  For atomic and functional accesses, the
331     *   request packet is always implicitly passed back *without*
332     *   modifying the destination fields, so this function
333     *   should not be called. */
334    void makeTimingResponse() {
335        assert(needsResponse());
336        assert(isRequest());
337        int icmd = (int)cmd;
338        icmd &= ~(IsRequest);
339        icmd |= IsResponse;
340        if (isRead())
341            icmd |= HasData;
342        if (isWrite())
343            icmd &= ~HasData;
344        cmd = (Command)icmd;
345        dest = src;
346        srcValid = false;
347    }
348
349
350    void toggleData() {
351        int icmd = (int)cmd;
352        icmd ^= HasData;
353        cmd = (Command)icmd;
354    }
355
356    /**
357     * Take a request packet and modify it in place to be suitable for
358     * returning as a response to that request.
359     */
360    void makeAtomicResponse()
361    {
362        assert(needsResponse());
363        assert(isRequest());
364        int icmd = (int)cmd;
365        icmd &= ~(IsRequest);
366        icmd |= IsResponse;
367        if (isRead())
368            icmd |= HasData;
369        if (isWrite())
370            icmd &= ~HasData;
371        cmd = (Command)icmd;
372    }
373
374    /**
375     * Take a request packet that has been returned as NACKED and
376     * modify it so that it can be sent out again. Only packets that
377     * need a response can be NACKED, so verify that that is true.
378     */
379    void
380    reinitNacked()
381    {
382        assert(needsResponse() && result == Nacked);
383        dest =  Broadcast;
384        result = Unknown;
385    }
386
387
388    /**
389     * Set the data pointer to the following value that should not be
390     * freed.
391     */
392    template <typename T>
393    void
394    dataStatic(T *p)
395    {
396        if(dynamicData)
397            dynamicData = false;
398        data = (PacketDataPtr)p;
399        staticData = true;
400    }
401
402    /**
403     * Set the data pointer to a value that should have delete []
404     * called on it.
405     */
406    template <typename T>
407    void
408    dataDynamicArray(T *p)
409    {
410        assert(!staticData && !dynamicData);
411        data = (PacketDataPtr)p;
412        dynamicData = true;
413        arrayData = true;
414    }
415
416    /**
417     * set the data pointer to a value that should have delete called
418     * on it.
419     */
420    template <typename T>
421    void
422    dataDynamic(T *p)
423    {
424        assert(!staticData && !dynamicData);
425        data = (PacketDataPtr)p;
426        dynamicData = true;
427        arrayData = false;
428    }
429
430    /** get a pointer to the data ptr. */
431    template <typename T>
432    T*
433    getPtr()
434    {
435        assert(staticData || dynamicData);
436        return (T*)data;
437    }
438
439    /** return the value of what is pointed to in the packet. */
440    template <typename T>
441    T get();
442
443    /** set the value in the data pointer to v. */
444    template <typename T>
445    void set(T v);
446
447    /**
448     * delete the data pointed to in the data pointer. Ok to call to
449     * matter how data was allocted.
450     */
451    void deleteData();
452
453    /** If there isn't data in the packet, allocate some. */
454    void allocate();
455
456    /** Do the packet modify the same addresses. */
457    bool intersect(PacketPtr p);
458};
459
460/** This function given a functional packet and a timing packet either satisfies
461 * the timing packet, or updates the timing packet to reflect the updated state
462 * in the timing packet. It returns if the functional packet should continue to
463 * traverse the memory hierarchy or not.
464 */
465bool fixPacket(PacketPtr func, PacketPtr timing);
466
467/** This function is a wrapper for the fixPacket field that toggles the hasData bit
468 * it is used when a response is waiting in the caches, but hasn't been marked as a
469 * response yet (so the fixPacket needs to get the correct value for the hasData)
470 */
471bool fixDelayedResponsePacket(PacketPtr func, PacketPtr timing);
472
473std::ostream & operator<<(std::ostream &o, const Packet &p);
474
475#endif //__MEM_PACKET_HH
476