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