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