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