0369745c9b9d593e2361b04923e29446b9edcc6a
[gem5.git] / src / mem / packet.hh
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 #include <list>
45
46 struct Packet;
47 typedef Packet* PacketPtr;
48 typedef uint8_t* PacketDataPtr;
49 typedef std::list<PacketPtr> PacketList;
50
51 //Coherence Flags
52 #define NACKED_LINE 1 << 0
53 #define SATISFIED 1 << 1
54 #define SHARED_LINE 1 << 2
55 #define CACHE_LINE_FILL 1 << 3
56 #define COMPRESSED 1 << 4
57 #define NO_ALLOCATE 1 << 5
58
59 //For statistics we need max number of commands, hard code it at
60 //20 for now. @todo fix later
61 #define NUM_MEM_CMDS 1 << 9
62
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 */
70 class 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
96 /** The address of the request. This address could be virtual or
97 * physical, depending on the system configuration. */
98 Addr addr;
99
100 /** The size of the request or transfer. */
101 int size;
102
103 /** The offset within the block that represents the data. */
104 int offset;
105
106 /** Device address (e.g., bus ID) of the source of the
107 * transaction. The source is not responsible for setting this
108 * field; it is set implicitly by the interconnect when the
109 * packet * is first sent. */
110 short src;
111
112 /** Device address (e.g., bus ID) of the destination of the
113 * transaction. The special value Broadcast indicates that the
114 * packet should be routed based on its address. This field is
115 * initialized in the constructor and is thus always valid
116 * (unlike * addr, size, and src). */
117 short dest;
118
119 /** Are the 'addr' and 'size' fields valid? */
120 bool addrSizeValid;
121 /** Is the 'src' field valid? */
122 bool srcValid;
123 /** Is the offset valid. */
124 bool offsetValid;
125
126
127 public:
128
129 /** Used to calculate latencies for each packet.*/
130 Tick time;
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 enum CommandAttribute
171 {
172 IsRead = 1 << 0,
173 IsWrite = 1 << 1,
174 IsPrefetch = 1 << 2,
175 IsInvalidate = 1 << 3,
176 IsRequest = 1 << 4,
177 IsResponse = 1 << 5,
178 NeedsResponse = 1 << 6,
179 IsSWPrefetch = 1 << 7,
180 IsHWPrefetch = 1 << 8
181 };
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,
190 WriteReqNoAck = IsWrite | IsRequest,
191 ReadResp = IsRead | IsResponse,
192 WriteResp = IsWrite | IsResponse,
193 Writeback = IsWrite | IsRequest,
194 SoftPFReq = IsRead | IsRequest | IsSWPrefetch | NeedsResponse,
195 HardPFReq = IsRead | IsRequest | IsHWPrefetch | NeedsResponse,
196 SoftPFResp = IsRead | IsRequest | IsSWPrefetch | IsResponse,
197 HardPFResp = IsRead | IsRequest | IsHWPrefetch | IsResponse,
198 InvalidateReq = IsInvalidate | IsRequest,
199 WriteInvalidateReq = IsWrite | IsInvalidate | IsRequest,
200 UpgradeReq = IsInvalidate | NeedsResponse,
201 UpgradeResp = IsInvalidate | IsResponse,
202 ReadExReq = IsRead | IsInvalidate | NeedsResponse,
203 ReadExResp = IsRead | IsInvalidate | IsResponse
204 };
205
206 /** Return the string name of the cmd field (for debugging and
207 * tracing). */
208 const std::string &cmdString() const;
209
210 /** Reutrn the string to a cmd given by idx. */
211 const std::string &cmdIdxToString(Command idx);
212
213 /** Return the index of this command. */
214 inline int cmdToIndex() const { return (int) cmd; }
215
216 /** The command field of the packet. */
217 Command cmd;
218
219 bool isRead() { return (cmd & IsRead) != 0; }
220 bool isWrite() { return (cmd & IsWrite) != 0; }
221 bool isRequest() { return (cmd & IsRequest) != 0; }
222 bool isResponse() { return (cmd & IsResponse) != 0; }
223 bool needsResponse() { return (cmd & NeedsResponse) != 0; }
224 bool isInvalidate() { return (cmd * IsInvalidate) != 0; }
225
226 bool isCacheFill() { return (flags & CACHE_LINE_FILL) != 0; }
227 bool isNoAllocate() { return (flags & NO_ALLOCATE) != 0; }
228
229 /** Possible results of a packet's request. */
230 enum Result
231 {
232 Success,
233 BadAddress,
234 Nacked,
235 Unknown
236 };
237
238 /** The result of this packet's request. */
239 Result result;
240
241 /** Accessor function that returns the source index of the packet. */
242 short getSrc() const { assert(srcValid); return src; }
243 void setSrc(short _src) { src = _src; srcValid = true; }
244
245 /** Accessor function that returns the destination index of
246 the packet. */
247 short getDest() const { return dest; }
248 void setDest(short _dest) { dest = _dest; }
249
250 Addr getAddr() const { assert(addrSizeValid); return addr; }
251 int getSize() const { assert(addrSizeValid); return size; }
252 int getOffset() const { assert(offsetValid); return offset; }
253
254 void addrOverride(Addr newAddr) { assert(addrSizeValid); addr = newAddr; }
255 void cmdOverride(Command newCmd) { cmd = newCmd; }
256
257 /** Constructor. Note that a Request object must be constructed
258 * first, but the Requests's physical address and size fields
259 * need not be valid. The command and destination addresses
260 * must be supplied. */
261 Packet(Request *_req, Command _cmd, short _dest)
262 : data(NULL), staticData(false), dynamicData(false), arrayData(false),
263 addr(_req->paddr), size(_req->size), dest(_dest),
264 addrSizeValid(_req->validPaddr),
265 srcValid(false), offsetValid(false),
266 req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
267 result(Unknown)
268 {
269 flags = 0;
270 }
271
272 /** Alternate constructor if you are trying to create a packet with
273 * a request that is for a whole block, not the address from the req.
274 * this allows for overriding the size/addr of the req.*/
275 Packet(Request *_req, Command _cmd, short _dest, int _blkSize)
276 : data(NULL), staticData(false), dynamicData(false), arrayData(false),
277 addr(_req->paddr & ~(_blkSize - 1)), size(_blkSize),
278 offset(_req->paddr & (_blkSize - 1)), dest(_dest),
279 addrSizeValid(_req->validPaddr), srcValid(false), offsetValid(true),
280 req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
281 result(Unknown)
282 {
283 flags = 0;
284 }
285
286 /** Destructor. */
287 ~Packet()
288 { deleteData(); }
289
290 /** Reinitialize packet address and size from the associated
291 * Request object, and reset other fields that may have been
292 * modified by a previous transaction. Typically called when a
293 * statically allocated Request/Packet pair is reused for
294 * multiple transactions. */
295 void reinitFromRequest() {
296 assert(req->validPaddr);
297 addr = req->paddr;
298 size = req->size;
299 addrSizeValid = true;
300 result = Unknown;
301 if (dynamicData) {
302 deleteData();
303 dynamicData = false;
304 arrayData = false;
305 }
306 }
307
308 /** Take a request packet and modify it in place to be suitable
309 * for returning as a response to that request. Used for timing
310 * accesses only. For atomic and functional accesses, the
311 * request packet is always implicitly passed back *without*
312 * modifying the command or destination fields, so this function
313 * should not be called. */
314 void makeTimingResponse() {
315 assert(needsResponse());
316 int icmd = (int)cmd;
317 icmd &= ~(IsRequest | NeedsResponse);
318 icmd |= IsResponse;
319 cmd = (Command)icmd;
320 dest = src;
321 srcValid = false;
322 }
323
324 /** Take a request packet that has been returned as NACKED and modify it so
325 * that it can be sent out again. Only packets that need a response can be
326 * NACKED, so verify that that is true. */
327 void reinitNacked() {
328 assert(needsResponse() && result == Nacked);
329 dest = Broadcast;
330 result = Unknown;
331 }
332
333
334 /** Set the data pointer to the following value that should not be freed. */
335 template <typename T>
336 void dataStatic(T *p);
337
338 /** Set the data pointer to a value that should have delete [] called on it.
339 */
340 template <typename T>
341 void dataDynamicArray(T *p);
342
343 /** set the data pointer to a value that should have delete called on it. */
344 template <typename T>
345 void dataDynamic(T *p);
346
347 /** return the value of what is pointed to in the packet. */
348 template <typename T>
349 T get();
350
351 /** get a pointer to the data ptr. */
352 template <typename T>
353 T* getPtr();
354
355 /** set the value in the data pointer to v. */
356 template <typename T>
357 void set(T v);
358
359 /** delete the data pointed to in the data pointer. Ok to call to matter how
360 * data was allocted. */
361 void deleteData();
362
363 /** If there isn't data in the packet, allocate some. */
364 void allocate();
365
366 /** Do the packet modify the same addresses. */
367 bool intersect(Packet *p);
368 };
369
370 bool fixPacket(Packet *func, Packet *timing);
371 #endif //__MEM_PACKET_HH