packet.hh (5650:d2782c951841) packet.hh (5735:a88e8e7dec75)
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;

--- 28 unchanged lines hidden (view full) ---

37
38#ifndef __MEM_PACKET_HH__
39#define __MEM_PACKET_HH__
40
41#include <cassert>
42#include <list>
43#include <bitset>
44
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;

--- 28 unchanged lines hidden (view full) ---

37
38#ifndef __MEM_PACKET_HH__
39#define __MEM_PACKET_HH__
40
41#include <cassert>
42#include <list>
43#include <bitset>
44
45#include "base/cast.hh"
45#include "base/compiler.hh"
46#include "base/fast_alloc.hh"
46#include "base/compiler.hh"
47#include "base/fast_alloc.hh"
48#include "base/flags.hh"
47#include "base/misc.hh"
48#include "base/printable.hh"
49#include "mem/request.hh"
50#include "sim/host.hh"
51#include "sim/core.hh"
52
53
54struct Packet;
55typedef Packet *PacketPtr;
56typedef uint8_t* PacketDataPtr;
57typedef std::list<PacketPtr> PacketList;
58
59class MemCmd
60{
49#include "base/misc.hh"
50#include "base/printable.hh"
51#include "mem/request.hh"
52#include "sim/host.hh"
53#include "sim/core.hh"
54
55
56struct Packet;
57typedef Packet *PacketPtr;
58typedef uint8_t* PacketDataPtr;
59typedef std::list<PacketPtr> PacketList;
60
61class MemCmd
62{
61 public:
63 friend class Packet;
62
64
63 /** List of all commands associated with a packet. */
65 public:
66 /**
67 * List of all commands associated with a packet.
68 */
64 enum Command
65 {
66 InvalidCmd,
67 ReadReq,
68 ReadResp,
69 ReadRespWithInvalidate,
70 WriteReq,
71 WriteResp,

--- 23 unchanged lines hidden (view full) ---

95 InvalidDestError, // packet dest field invalid
96 BadAddressError, // memory address invalid
97 // Fake simulator-only commands
98 PrintReq, // Print state matching address
99 NUM_MEM_CMDS
100 };
101
102 private:
69 enum Command
70 {
71 InvalidCmd,
72 ReadReq,
73 ReadResp,
74 ReadRespWithInvalidate,
75 WriteReq,
76 WriteResp,

--- 23 unchanged lines hidden (view full) ---

100 InvalidDestError, // packet dest field invalid
101 BadAddressError, // memory address invalid
102 // Fake simulator-only commands
103 PrintReq, // Print state matching address
104 NUM_MEM_CMDS
105 };
106
107 private:
103 /** List of command attributes. */
108 /**
109 * List of command attributes.
110 */
104 enum Attribute
105 {
106 IsRead, //!< Data flows from responder to requester
107 IsWrite, //!< Data flows from requester to responder
108 IsPrefetch, //!< Not a demand access
109 IsInvalidate,
110 NeedsExclusive, //!< Requires exclusive copy to complete in-cache
111 IsRequest, //!< Issued by requester
112 IsResponse, //!< Issue by responder
113 NeedsResponse, //!< Requester needs response from target
114 IsSWPrefetch,
115 IsHWPrefetch,
116 IsLocked, //!< Alpha/MIPS LL or SC access
117 HasData, //!< There is an associated payload
118 IsError, //!< Error response
119 IsPrint, //!< Print state matching address (for debugging)
120 NUM_COMMAND_ATTRIBUTES
121 };
122
111 enum Attribute
112 {
113 IsRead, //!< Data flows from responder to requester
114 IsWrite, //!< Data flows from requester to responder
115 IsPrefetch, //!< Not a demand access
116 IsInvalidate,
117 NeedsExclusive, //!< Requires exclusive copy to complete in-cache
118 IsRequest, //!< Issued by requester
119 IsResponse, //!< Issue by responder
120 NeedsResponse, //!< Requester needs response from target
121 IsSWPrefetch,
122 IsHWPrefetch,
123 IsLocked, //!< Alpha/MIPS LL or SC access
124 HasData, //!< There is an associated payload
125 IsError, //!< Error response
126 IsPrint, //!< Print state matching address (for debugging)
127 NUM_COMMAND_ATTRIBUTES
128 };
129
123 /** Structure that defines attributes and other data associated
124 * with a Command. */
125 struct CommandInfo {
126 /** Set of attribute flags. */
130 /**
131 * Structure that defines attributes and other data associated
132 * with a Command.
133 */
134 struct CommandInfo
135 {
136 /// Set of attribute flags.
127 const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
137 const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
128 /** Corresponding response for requests; InvalidCmd if no
129 * response is applicable. */
138 /// Corresponding response for requests; InvalidCmd if no
139 /// response is applicable.
130 const Command response;
140 const Command response;
131 /** String representation (for printing) */
141 /// String representation (for printing)
132 const std::string str;
133 };
134
142 const std::string str;
143 };
144
135 /** Array to map Command enum to associated info. */
145 /// Array to map Command enum to associated info.
136 static const CommandInfo commandInfo[];
137
138 private:
139
140 Command cmd;
141
146 static const CommandInfo commandInfo[];
147
148 private:
149
150 Command cmd;
151
142 bool testCmdAttrib(MemCmd::Attribute attrib) const {
152 bool
153 testCmdAttrib(MemCmd::Attribute attrib) const
154 {
143 return commandInfo[cmd].attributes[attrib] != 0;
144 }
145
146 public:
147
148 bool isRead() const { return testCmdAttrib(IsRead); }
149 bool isWrite() const { return testCmdAttrib(IsWrite); }
150 bool isRequest() const { return testCmdAttrib(IsRequest); }
151 bool isResponse() const { return testCmdAttrib(IsResponse); }
152 bool needsExclusive() const { return testCmdAttrib(NeedsExclusive); }
153 bool needsResponse() const { return testCmdAttrib(NeedsResponse); }
154 bool isInvalidate() const { return testCmdAttrib(IsInvalidate); }
155 bool hasData() const { return testCmdAttrib(HasData); }
156 bool isReadWrite() const { return isRead() && isWrite(); }
157 bool isLocked() const { return testCmdAttrib(IsLocked); }
158 bool isError() const { return testCmdAttrib(IsError); }
159 bool isPrint() const { return testCmdAttrib(IsPrint); }
160
155 return commandInfo[cmd].attributes[attrib] != 0;
156 }
157
158 public:
159
160 bool isRead() const { return testCmdAttrib(IsRead); }
161 bool isWrite() const { return testCmdAttrib(IsWrite); }
162 bool isRequest() const { return testCmdAttrib(IsRequest); }
163 bool isResponse() const { return testCmdAttrib(IsResponse); }
164 bool needsExclusive() const { return testCmdAttrib(NeedsExclusive); }
165 bool needsResponse() const { return testCmdAttrib(NeedsResponse); }
166 bool isInvalidate() const { return testCmdAttrib(IsInvalidate); }
167 bool hasData() const { return testCmdAttrib(HasData); }
168 bool isReadWrite() const { return isRead() && isWrite(); }
169 bool isLocked() const { return testCmdAttrib(IsLocked); }
170 bool isError() const { return testCmdAttrib(IsError); }
171 bool isPrint() const { return testCmdAttrib(IsPrint); }
172
161 const Command responseCommand() const {
173 const Command
174 responseCommand() const
175 {
162 return commandInfo[cmd].response;
163 }
164
176 return commandInfo[cmd].response;
177 }
178
165 /** Return the string to a cmd given by idx. */
166 const std::string &toString() const {
167 return commandInfo[cmd].str;
168 }
169
179 /// Return the string to a cmd given by idx.
180 const std::string &toString() const { return commandInfo[cmd].str; }
170 int toInt() const { return (int)cmd; }
171
181 int toInt() const { return (int)cmd; }
182
172 MemCmd(Command _cmd)
173 : cmd(_cmd)
174 { }
183 MemCmd(Command _cmd) : cmd(_cmd) { }
184 MemCmd(int _cmd) : cmd((Command)_cmd) { }
185 MemCmd() : cmd(InvalidCmd) { }
175
186
176 MemCmd(int _cmd)
177 : cmd((Command)_cmd)
178 { }
179
180 MemCmd()
181 : cmd(InvalidCmd)
182 { }
183
184 bool operator==(MemCmd c2) { return (cmd == c2.cmd); }
185 bool operator!=(MemCmd c2) { return (cmd != c2.cmd); }
186
187 friend class Packet;
187 bool operator==(MemCmd c2) const { return (cmd == c2.cmd); }
188 bool operator!=(MemCmd c2) const { return (cmd != c2.cmd); }
188};
189
190/**
191 * A Packet is used to encapsulate a transfer between two objects in
192 * the memory system (e.g., the L1 and L2 cache). (In contrast, a
193 * single Request travels all the way from the requester to the
194 * ultimate destination and back, possibly being conveyed by several
195 * different Packets along the way.)
196 */
197class Packet : public FastAlloc, public Printable
198{
199 public:
189};
190
191/**
192 * A Packet is used to encapsulate a transfer between two objects in
193 * the memory system (e.g., the L1 and L2 cache). (In contrast, a
194 * single Request travels all the way from the requester to the
195 * ultimate destination and back, possibly being conveyed by several
196 * different Packets along the way.)
197 */
198class Packet : public FastAlloc, public Printable
199{
200 public:
201 typedef uint32_t FlagsType;
202 typedef ::Flags<FlagsType> Flags;
203 typedef short NodeID;
200
204
205 private:
206 static const FlagsType PUBLIC_FLAGS = 0x00000000;
207 static const FlagsType PRIVATE_FLAGS = 0x00007F0F;
208 static const FlagsType COPY_FLAGS = 0x0000000F;
209
210 static const FlagsType SHARED = 0x00000001;
211 // Special control flags
212 /// Special timing-mode atomic snoop for multi-level coherence.
213 static const FlagsType EXPRESS_SNOOP = 0x00000002;
214 /// Does supplier have exclusive copy?
215 /// Useful for multi-level coherence.
216 static const FlagsType SUPPLY_EXCLUSIVE = 0x00000004;
217 // Snoop response flags
218 static const FlagsType MEM_INHIBIT = 0x00000008;
219 /// Are the 'addr' and 'size' fields valid?
220 static const FlagsType VALID_ADDR = 0x00000100;
221 static const FlagsType VALID_SIZE = 0x00000200;
222 /// Is the 'src' field valid?
223 static const FlagsType VALID_SRC = 0x00000400;
224 static const FlagsType VALID_DST = 0x00000800;
225 /// Is the data pointer set to a value that shouldn't be freed
226 /// when the packet is destroyed?
227 static const FlagsType STATIC_DATA = 0x00001000;
228 /// The data pointer points to a value that should be freed when
229 /// the packet is destroyed.
230 static const FlagsType DYNAMIC_DATA = 0x00002000;
231 /// the data pointer points to an array (thus delete []) needs to
232 /// be called on it rather than simply delete.
233 static const FlagsType ARRAY_DATA = 0x00004000;
234
235 Flags flags;
236
237 public:
201 typedef MemCmd::Command Command;
202
238 typedef MemCmd::Command Command;
239
203 /** The command field of the packet. */
240 /// The command field of the packet.
204 MemCmd cmd;
205
241 MemCmd cmd;
242
206 /** A pointer to the original request. */
243 /// A pointer to the original request.
207 RequestPtr req;
208
209 private:
244 RequestPtr req;
245
246 private:
210 /** A pointer to the data being transfered. It can be differnt
211 * sizes at each level of the heirarchy so it belongs in the
212 * packet, not request. This may or may not be populated when a
213 * responder recieves the packet. If not populated it memory
214 * should be allocated.
247 /**
248 * A pointer to the data being transfered. It can be differnt
249 * sizes at each level of the heirarchy so it belongs in the
250 * packet, not request. This may or may not be populated when a
251 * responder recieves the packet. If not populated it memory should
252 * be allocated.
215 */
216 PacketDataPtr data;
217
253 */
254 PacketDataPtr data;
255
218 /** Is the data pointer set to a value that shouldn't be freed
219 * when the packet is destroyed? */
220 bool staticData;
221 /** The data pointer points to a value that should be freed when
222 * the packet is destroyed. */
223 bool dynamicData;
224 /** the data pointer points to an array (thus delete [] ) needs to
225 * be called on it rather than simply delete.*/
226 bool arrayData;
227
228 /** The address of the request. This address could be virtual or
229 * physical, depending on the system configuration. */
256 /// The address of the request. This address could be virtual or
257 /// physical, depending on the system configuration.
230 Addr addr;
231
258 Addr addr;
259
232 /** The size of the request or transfer. */
260 /// The size of the request or transfer.
233 int size;
234
261 int size;
262
235 /** Device address (e.g., bus ID) of the source of the
236 * transaction. The source is not responsible for setting this
237 * field; it is set implicitly by the interconnect when the
238 * packet is first sent. */
239 short src;
263 /**
264 * Device address (e.g., bus ID) of the source of the
265 * transaction. The source is not responsible for setting this
266 * field; it is set implicitly by the interconnect when the packet
267 * is first sent.
268 */
269 NodeID src;
240
270
241 /** Device address (e.g., bus ID) of the destination of the
242 * transaction. The special value Broadcast indicates that the
243 * packet should be routed based on its address. This field is
244 * initialized in the constructor and is thus always valid
245 * (unlike * addr, size, and src). */
246 short dest;
271 /**
272 * Device address (e.g., bus ID) of the destination of the
273 * transaction. The special value Broadcast indicates that the
274 * packet should be routed based on its address. This field is
275 * initialized in the constructor and is thus always valid (unlike
276 * addr, size, and src).
277 */
278 NodeID dest;
247
279
248 /** The original value of the command field. Only valid when the
280 /**
281 * The original value of the command field. Only valid when the
249 * current command field is an error condition; in that case, the
250 * previous contents of the command field are copied here. This
251 * field is *not* set on non-error responses.
252 */
253 MemCmd origCmd;
254
282 * current command field is an error condition; in that case, the
283 * previous contents of the command field are copied here. This
284 * field is *not* set on non-error responses.
285 */
286 MemCmd origCmd;
287
255 /** Are the 'addr' and 'size' fields valid? */
256 bool addrSizeValid;
257 /** Is the 'src' field valid? */
258 bool srcValid;
259 bool destValid;
260
261 enum Flag {
262 // Snoop response flags
263 MemInhibit,
264 Shared,
265 // Special control flags
266 /// Special timing-mode atomic snoop for multi-level coherence.
267 ExpressSnoop,
268 /// Does supplier have exclusive copy?
269 /// Useful for multi-level coherence.
270 SupplyExclusive,
271 NUM_PACKET_FLAGS
272 };
273
274 /** Status flags */
275 std::bitset<NUM_PACKET_FLAGS> flags;
276
277 public:
288 public:
278
279 /** Used to calculate latencies for each packet.*/
289 /// Used to calculate latencies for each packet.
280 Tick time;
281
290 Tick time;
291
282 /** The time at which the packet will be fully transmitted */
292 /// The time at which the packet will be fully transmitted
283 Tick finishTime;
284
293 Tick finishTime;
294
285 /** The time at which the first chunk of the packet will be transmitted */
295 /// The time at which the first chunk of the packet will be transmitted
286 Tick firstWordTime;
287
296 Tick firstWordTime;
297
288 /** The special destination address indicating that the packet
289 * should be routed based on its address. */
290 static const short Broadcast = -1;
298 /// The special destination address indicating that the packet
299 /// should be routed based on its address.
300 static const NodeID Broadcast = -1;
291
301
292 /** A virtual base opaque structure used to hold state associated
293 * with the packet but specific to the sending device (e.g., an
294 * MSHR). A pointer to this state is returned in the packet's
295 * response so that the sender can quickly look up the state
296 * needed to process it. A specific subclass would be derived
297 * from this to carry state specific to a particular sending
298 * device. */
299 class SenderState {
300 public:
302 /**
303 * A virtual base opaque structure used to hold state associated
304 * with the packet but specific to the sending device (e.g., an
305 * MSHR). A pointer to this state is returned in the packet's
306 * response so that the sender can quickly look up the state
307 * needed to process it. A specific subclass would be derived
308 * from this to carry state specific to a particular sending
309 * device.
310 */
311 struct SenderState
312 {
301 virtual ~SenderState() {}
302 };
303
304 /**
305 * Object used to maintain state of a PrintReq. The senderState
306 * field of a PrintReq should always be of this type.
307 */
313 virtual ~SenderState() {}
314 };
315
316 /**
317 * Object used to maintain state of a PrintReq. The senderState
318 * field of a PrintReq should always be of this type.
319 */
308 class PrintReqState : public SenderState, public FastAlloc {
309 /** An entry in the label stack. */
310 class LabelStackEntry {
311 public:
320 class PrintReqState : public SenderState, public FastAlloc
321 {
322 private:
323 /**
324 * An entry in the label stack.
325 */
326 struct LabelStackEntry
327 {
312 const std::string label;
313 std::string *prefix;
314 bool labelPrinted;
328 const std::string label;
329 std::string *prefix;
330 bool labelPrinted;
315 LabelStackEntry(const std::string &_label,
316 std::string *_prefix);
331 LabelStackEntry(const std::string &_label, std::string *_prefix);
317 };
318
319 typedef std::list<LabelStackEntry> LabelStack;
320 LabelStack labelStack;
321
322 std::string *curPrefixPtr;
323
324 public:
325 std::ostream &os;
326 const int verbosity;
327
328 PrintReqState(std::ostream &os, int verbosity = 0);
329 ~PrintReqState();
330
332 };
333
334 typedef std::list<LabelStackEntry> LabelStack;
335 LabelStack labelStack;
336
337 std::string *curPrefixPtr;
338
339 public:
340 std::ostream &os;
341 const int verbosity;
342
343 PrintReqState(std::ostream &os, int verbosity = 0);
344 ~PrintReqState();
345
331 /** Returns the current line prefix. */
346 /**
347 * Returns the current line prefix.
348 */
332 const std::string &curPrefix() { return *curPrefixPtr; }
333
349 const std::string &curPrefix() { return *curPrefixPtr; }
350
334 /** Push a label onto the label stack, and prepend the given
351 /**
352 * Push a label onto the label stack, and prepend the given
335 * prefix string onto the current prefix. Labels will only be
353 * prefix string onto the current prefix. Labels will only be
336 * printed if an object within the label's scope is
337 * printed. */
354 * printed if an object within the label's scope is printed.
355 */
338 void pushLabel(const std::string &lbl,
339 const std::string &prefix = " ");
356 void pushLabel(const std::string &lbl,
357 const std::string &prefix = " ");
340 /** Pop a label off the label stack. */
358
359 /**
360 * Pop a label off the label stack.
361 */
341 void popLabel();
362 void popLabel();
342 /** Print all of the pending unprinted labels on the
363
364 /**
365 * Print all of the pending unprinted labels on the
343 * stack. Called by printObj(), so normally not called by
366 * stack. Called by printObj(), so normally not called by
344 * users unless bypassing printObj(). */
367 * users unless bypassing printObj().
368 */
345 void printLabels();
369 void printLabels();
346 /** Print a Printable object to os, because it matched the
347 * address on a PrintReq. */
370
371 /**
372 * Print a Printable object to os, because it matched the
373 * address on a PrintReq.
374 */
348 void printObj(Printable *obj);
349 };
350
375 void printObj(Printable *obj);
376 };
377
351 /** This packet's sender state. Devices should use dynamic_cast<>
352 * to cast to the state appropriate to the sender. */
378 /**
379 * This packet's sender state. Devices should use dynamic_cast<>
380 * to cast to the state appropriate to the sender. The intent of
381 * this variable is to allow a device to attach extra information
382 * to a request. A response packet must return the sender state
383 * that was attached to the original request (even if a new packet
384 * is created).
385 */
353 SenderState *senderState;
354
386 SenderState *senderState;
387
355 /** Return the string name of the cmd field (for debugging and
356 * tracing). */
388 /// Return the string name of the cmd field (for debugging and
389 /// tracing).
357 const std::string &cmdString() const { return cmd.toString(); }
358
390 const std::string &cmdString() const { return cmd.toString(); }
391
359 /** Return the index of this command. */
392 /// Return the index of this command.
360 inline int cmdToIndex() const { return cmd.toInt(); }
361
362 bool isRead() const { return cmd.isRead(); }
363 bool isWrite() const { return cmd.isWrite(); }
364 bool isRequest() const { return cmd.isRequest(); }
365 bool isResponse() const { return cmd.isResponse(); }
366 bool needsExclusive() const { return cmd.needsExclusive(); }
367 bool needsResponse() const { return cmd.needsResponse(); }
368 bool isInvalidate() const { return cmd.isInvalidate(); }
369 bool hasData() const { return cmd.hasData(); }
370 bool isReadWrite() const { return cmd.isReadWrite(); }
371 bool isLocked() const { return cmd.isLocked(); }
372 bool isError() const { return cmd.isError(); }
373 bool isPrint() const { return cmd.isPrint(); }
374
375 // Snoop flags
393 inline int cmdToIndex() const { return cmd.toInt(); }
394
395 bool isRead() const { return cmd.isRead(); }
396 bool isWrite() const { return cmd.isWrite(); }
397 bool isRequest() const { return cmd.isRequest(); }
398 bool isResponse() const { return cmd.isResponse(); }
399 bool needsExclusive() const { return cmd.needsExclusive(); }
400 bool needsResponse() const { return cmd.needsResponse(); }
401 bool isInvalidate() const { return cmd.isInvalidate(); }
402 bool hasData() const { return cmd.hasData(); }
403 bool isReadWrite() const { return cmd.isReadWrite(); }
404 bool isLocked() const { return cmd.isLocked(); }
405 bool isError() const { return cmd.isError(); }
406 bool isPrint() const { return cmd.isPrint(); }
407
408 // Snoop flags
376 void assertMemInhibit() { flags[MemInhibit] = true; }
377 bool memInhibitAsserted() { return flags[MemInhibit]; }
378 void assertShared() { flags[Shared] = true; }
379 bool sharedAsserted() { return flags[Shared]; }
409 void assertMemInhibit() { flags.set(MEM_INHIBIT); }
410 bool memInhibitAsserted() { return flags.any(MEM_INHIBIT); }
411 void assertShared() { flags.set(SHARED); }
412 bool sharedAsserted() { return flags.any(SHARED); }
380
381 // Special control flags
413
414 // Special control flags
382 void setExpressSnoop() { flags[ExpressSnoop] = true; }
383 bool isExpressSnoop() { return flags[ExpressSnoop]; }
384 void setSupplyExclusive() { flags[SupplyExclusive] = true; }
385 bool isSupplyExclusive() { return flags[SupplyExclusive]; }
415 void setExpressSnoop() { flags.set(EXPRESS_SNOOP); }
416 bool isExpressSnoop() { return flags.any(EXPRESS_SNOOP); }
417 void setSupplyExclusive() { flags.set(SUPPLY_EXCLUSIVE); }
418 bool isSupplyExclusive() { return flags.any(SUPPLY_EXCLUSIVE); }
386
387 // Network error conditions... encapsulate them as methods since
388 // their encoding keeps changing (from result field to command
389 // field, etc.)
419
420 // Network error conditions... encapsulate them as methods since
421 // their encoding keeps changing (from result field to command
422 // field, etc.)
390 void setNacked() { assert(isResponse()); cmd = MemCmd::NetworkNackError; }
391 void setBadAddress() { assert(isResponse()); cmd = MemCmd::BadAddressError; }
392 bool wasNacked() { return cmd == MemCmd::NetworkNackError; }
393 bool hadBadAddress() { return cmd == MemCmd::BadAddressError; }
394 void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
423 void
424 setNacked()
425 {
426 assert(isResponse());
427 cmd = MemCmd::NetworkNackError;
428 }
395
429
396 bool nic_pkt() { panic("Unimplemented"); M5_DUMMY_RETURN }
430 void
431 setBadAddress()
432 {
433 assert(isResponse());
434 cmd = MemCmd::BadAddressError;
435 }
397
436
398 /** Accessor function that returns the source index of the packet. */
399 short getSrc() const { assert(srcValid); return src; }
400 void setSrc(short _src) { src = _src; srcValid = true; }
401 /** Reset source field, e.g. to retransmit packet on different bus. */
402 void clearSrc() { srcValid = false; }
437 bool wasNacked() const { return cmd == MemCmd::NetworkNackError; }
438 bool hadBadAddress() const { return cmd == MemCmd::BadAddressError; }
439 void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
403
440
404 /** Accessor function that returns the destination index of
405 the packet. */
406 short getDest() const { assert(destValid); return dest; }
407 void setDest(short _dest) { dest = _dest; destValid = true; }
441 /// Accessor function to get the source index of the packet.
442 NodeID getSrc() const { assert(flags.any(VALID_SRC)); return src; }
443 /// Accessor function to set the source index of the packet.
444 void setSrc(NodeID _src) { src = _src; flags.set(VALID_SRC); }
445 /// Reset source field, e.g. to retransmit packet on different bus.
446 void clearSrc() { flags.clear(VALID_SRC); }
408
447
409 Addr getAddr() const { assert(addrSizeValid); return addr; }
410 int getSize() const { assert(addrSizeValid); return size; }
411 Addr getOffset(int blkSize) const { return addr & (Addr)(blkSize - 1); }
448 /// Accessor function for the destination index of the packet.
449 NodeID getDest() const { assert(flags.any(VALID_DST)); return dest; }
450 /// Accessor function to set the destination index of the packet.
451 void setDest(NodeID _dest) { dest = _dest; flags.set(VALID_DST); }
412
452
413 /** Constructor. Note that a Request object must be constructed
414 * first, but the Requests's physical address and size fields
415 * need not be valid. The command and destination addresses
416 * must be supplied. */
417 Packet(Request *_req, MemCmd _cmd, short _dest)
418 : cmd(_cmd), req(_req),
419 data(NULL), staticData(false), dynamicData(false), arrayData(false),
420 addr(_req->paddr), size(_req->size), dest(_dest),
421 addrSizeValid(_req->validPaddr), srcValid(false), destValid(true),
422 flags(0), time(curTick), senderState(NULL)
453 Addr getAddr() const { assert(flags.all(VALID_ADDR)); return addr; }
454 int getSize() const { assert(flags.all(VALID_SIZE)); return size; }
455 Addr getOffset(int blkSize) const { return getAddr() & (Addr)(blkSize - 1); }
456
457 /**
458 * Constructor. Note that a Request object must be constructed
459 * first, but the Requests's physical address and size fields need
460 * not be valid. The command and destination addresses must be
461 * supplied.
462 */
463 Packet(Request *_req, MemCmd _cmd, NodeID _dest)
464 : cmd(_cmd), req(_req), data(NULL), addr(_req->paddr),
465 size(_req->size), dest(_dest), time(curTick), senderState(NULL)
423 {
424 }
425
466 {
467 }
468
426 /** Alternate constructor if you are trying to create a packet with
427 * a request that is for a whole block, not the address from the req.
428 * this allows for overriding the size/addr of the req.*/
429 Packet(Request *_req, MemCmd _cmd, short _dest, int _blkSize)
430 : cmd(_cmd), req(_req),
431 data(NULL), staticData(false), dynamicData(false), arrayData(false),
469 /**
470 * Alternate constructor if you are trying to create a packet with
471 * a request that is for a whole block, not the address from the
472 * req. this allows for overriding the size/addr of the req.
473 */
474 Packet(Request *_req, MemCmd _cmd, NodeID _dest, int _blkSize)
475 : cmd(_cmd), req(_req), data(NULL),
432 addr(_req->paddr & ~(_blkSize - 1)), size(_blkSize), dest(_dest),
476 addr(_req->paddr & ~(_blkSize - 1)), size(_blkSize), dest(_dest),
433 addrSizeValid(_req->validPaddr), srcValid(false), destValid(true),
434 flags(0), time(curTick), senderState(NULL)
477 time(curTick), senderState(NULL)
435 {
436 }
437
478 {
479 }
480
438 /** Alternate constructor for copying a packet. Copy all fields
481 /**
482 * Alternate constructor for copying a packet. Copy all fields
439 * *except* if the original packet's data was dynamic, don't copy
440 * that, as we can't guarantee that the new packet's lifetime is
441 * less than that of the original packet. In this case the new
483 * *except* if the original packet's data was dynamic, don't copy
484 * that, as we can't guarantee that the new packet's lifetime is
485 * less than that of the original packet. In this case the new
442 * packet should allocate its own data. */
443 Packet(Packet *origPkt, bool clearFlags = false)
444 : cmd(origPkt->cmd), req(origPkt->req),
445 data(origPkt->staticData ? origPkt->data : NULL),
446 staticData(origPkt->staticData),
447 dynamicData(false), arrayData(false),
448 addr(origPkt->addr), size(origPkt->size),
449 src(origPkt->src), dest(origPkt->dest),
450 addrSizeValid(origPkt->addrSizeValid),
451 srcValid(origPkt->srcValid), destValid(origPkt->destValid),
452 flags(clearFlags ? 0 : origPkt->flags),
453 time(curTick), senderState(origPkt->senderState)
486 * packet should allocate its own data.
487 */
488 Packet(Packet *pkt, bool clearFlags = false)
489 : cmd(pkt->cmd), req(pkt->req),
490 data(pkt->flags.any(STATIC_DATA) ? pkt->data : NULL),
491 addr(pkt->addr), size(pkt->size), src(pkt->src), dest(pkt->dest),
492 time(curTick), senderState(pkt->senderState)
454 {
493 {
494 if (!clearFlags)
495 flags.set(pkt->flags & COPY_FLAGS);
496
497 flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE|VALID_SRC|VALID_DST));
498 flags.set(pkt->flags & STATIC_DATA);
455 }
456
499 }
500
457 /** Destructor. */
501 /**
502 * clean up packet variables
503 */
458 ~Packet()
459 {
460 // If this is a request packet for which there's no response,
461 // delete the request object here, since the requester will
462 // never get the chance.
463 if (req && isRequest() && !needsResponse())
464 delete req;
504 ~Packet()
505 {
506 // If this is a request packet for which there's no response,
507 // delete the request object here, since the requester will
508 // never get the chance.
509 if (req && isRequest() && !needsResponse())
510 delete req;
465 if (staticData || dynamicData)
466 deleteData();
511 deleteData();
467 }
468
512 }
513
469 /** Reinitialize packet address and size from the associated
470 * Request object, and reset other fields that may have been
471 * modified by a previous transaction. Typically called when a
472 * statically allocated Request/Packet pair is reused for
473 * multiple transactions. */
474 void reinitFromRequest() {
475 assert(req->validPaddr);
514 /**
515 * Reinitialize packet address and size from the associated
516 * Request object, and reset other fields that may have been
517 * modified by a previous transaction. Typically called when a
518 * statically allocated Request/Packet pair is reused for multiple
519 * transactions.
520 */
521 void
522 reinitFromRequest()
523 {
524 assert(req->flags.any(Request::VALID_PADDR));
476 flags = 0;
477 addr = req->paddr;
478 size = req->size;
479 time = req->time;
525 flags = 0;
526 addr = req->paddr;
527 size = req->size;
528 time = req->time;
480 addrSizeValid = true;
481 if (dynamicData) {
482 deleteData();
483 dynamicData = false;
484 arrayData = false;
485 }
529
530 flags.set(VALID_ADDR|VALID_SIZE);
531 deleteData();
486 }
487
488 /**
489 * Take a request packet and modify it in place to be suitable for
490 * returning as a response to that request. The source and
491 * destination fields are *not* modified, as is appropriate for
492 * atomic accesses.
493 */
532 }
533
534 /**
535 * Take a request packet and modify it in place to be suitable for
536 * returning as a response to that request. The source and
537 * destination fields are *not* modified, as is appropriate for
538 * atomic accesses.
539 */
494 void makeResponse()
540 void
541 makeResponse()
495 {
496 assert(needsResponse());
497 assert(isRequest());
498 origCmd = cmd;
499 cmd = cmd.responseCommand();
542 {
543 assert(needsResponse());
544 assert(isRequest());
545 origCmd = cmd;
546 cmd = cmd.responseCommand();
500 dest = src;
501 destValid = srcValid;
502 srcValid = false;
547 if (flags.any(VALID_SRC)) {
548 dest = src;
549 flags.set(VALID_DST);
550 flags.clear(VALID_SRC);
551 } else {
552 flags.clear(VALID_DST);
553 }
503 }
504
554 }
555
505 void makeAtomicResponse()
556 void
557 makeAtomicResponse()
506 {
507 makeResponse();
508 }
509
558 {
559 makeResponse();
560 }
561
510 void makeTimingResponse()
562 void
563 makeTimingResponse()
511 {
512 makeResponse();
513 }
514
515 /**
516 * Take a request packet that has been returned as NACKED and
517 * modify it so that it can be sent out again. Only packets that
518 * need a response can be NACKED, so verify that that is true.

--- 11 unchanged lines hidden (view full) ---

530 /**
531 * Set the data pointer to the following value that should not be
532 * freed.
533 */
534 template <typename T>
535 void
536 dataStatic(T *p)
537 {
564 {
565 makeResponse();
566 }
567
568 /**
569 * Take a request packet that has been returned as NACKED and
570 * modify it so that it can be sent out again. Only packets that
571 * need a response can be NACKED, so verify that that is true.

--- 11 unchanged lines hidden (view full) ---

583 /**
584 * Set the data pointer to the following value that should not be
585 * freed.
586 */
587 template <typename T>
588 void
589 dataStatic(T *p)
590 {
538 if(dynamicData)
539 dynamicData = false;
591 assert(flags.none(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
540 data = (PacketDataPtr)p;
592 data = (PacketDataPtr)p;
541 staticData = true;
593 flags.set(STATIC_DATA);
542 }
543
544 /**
545 * Set the data pointer to a value that should have delete []
546 * called on it.
547 */
548 template <typename T>
549 void
550 dataDynamicArray(T *p)
551 {
594 }
595
596 /**
597 * Set the data pointer to a value that should have delete []
598 * called on it.
599 */
600 template <typename T>
601 void
602 dataDynamicArray(T *p)
603 {
552 assert(!staticData && !dynamicData);
604 assert(flags.none(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
553 data = (PacketDataPtr)p;
605 data = (PacketDataPtr)p;
554 dynamicData = true;
555 arrayData = true;
606 flags.set(DYNAMIC_DATA|ARRAY_DATA);
556 }
557
558 /**
559 * set the data pointer to a value that should have delete called
560 * on it.
561 */
562 template <typename T>
563 void
564 dataDynamic(T *p)
565 {
607 }
608
609 /**
610 * set the data pointer to a value that should have delete called
611 * on it.
612 */
613 template <typename T>
614 void
615 dataDynamic(T *p)
616 {
566 assert(!staticData && !dynamicData);
617 assert(flags.none(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
567 data = (PacketDataPtr)p;
618 data = (PacketDataPtr)p;
568 dynamicData = true;
569 arrayData = false;
619 flags.set(DYNAMIC_DATA);
570 }
571
620 }
621
572 /** get a pointer to the data ptr. */
622 /**
623 * get a pointer to the data ptr.
624 */
573 template <typename T>
574 T*
575 getPtr()
576 {
625 template <typename T>
626 T*
627 getPtr()
628 {
577 assert(staticData || dynamicData);
629 assert(flags.any(STATIC_DATA|DYNAMIC_DATA));
578 return (T*)data;
579 }
580
630 return (T*)data;
631 }
632
581 /** return the value of what is pointed to in the packet. */
633 /**
634 * return the value of what is pointed to in the packet.
635 */
582 template <typename T>
583 T get();
584
636 template <typename T>
637 T get();
638
585 /** set the value in the data pointer to v. */
639 /**
640 * set the value in the data pointer to v.
641 */
586 template <typename T>
587 void set(T v);
588
589 /**
590 * Copy data into the packet from the provided pointer.
591 */
642 template <typename T>
643 void set(T v);
644
645 /**
646 * Copy data into the packet from the provided pointer.
647 */
592 void setData(uint8_t *p)
648 void
649 setData(uint8_t *p)
593 {
594 std::memcpy(getPtr<uint8_t>(), p, getSize());
595 }
596
597 /**
598 * Copy data into the packet from the provided block pointer,
599 * which is aligned to the given block size.
600 */
650 {
651 std::memcpy(getPtr<uint8_t>(), p, getSize());
652 }
653
654 /**
655 * Copy data into the packet from the provided block pointer,
656 * which is aligned to the given block size.
657 */
601 void setDataFromBlock(uint8_t *blk_data, int blkSize)
658 void
659 setDataFromBlock(uint8_t *blk_data, int blkSize)
602 {
603 setData(blk_data + getOffset(blkSize));
604 }
605
606 /**
607 * Copy data from the packet to the provided block pointer, which
608 * is aligned to the given block size.
609 */
660 {
661 setData(blk_data + getOffset(blkSize));
662 }
663
664 /**
665 * Copy data from the packet to the provided block pointer, which
666 * is aligned to the given block size.
667 */
610 void writeData(uint8_t *p)
668 void
669 writeData(uint8_t *p)
611 {
612 std::memcpy(p, getPtr<uint8_t>(), getSize());
613 }
614
615 /**
616 * Copy data from the packet to the memory at the provided pointer.
617 */
670 {
671 std::memcpy(p, getPtr<uint8_t>(), getSize());
672 }
673
674 /**
675 * Copy data from the packet to the memory at the provided pointer.
676 */
618 void writeDataToBlock(uint8_t *blk_data, int blkSize)
677 void
678 writeDataToBlock(uint8_t *blk_data, int blkSize)
619 {
620 writeData(blk_data + getOffset(blkSize));
621 }
622
623 /**
624 * delete the data pointed to in the data pointer. Ok to call to
625 * matter how data was allocted.
626 */
679 {
680 writeData(blk_data + getOffset(blkSize));
681 }
682
683 /**
684 * delete the data pointed to in the data pointer. Ok to call to
685 * matter how data was allocted.
686 */
627 void deleteData();
687 void
688 deleteData()
689 {
690 if (flags.any(ARRAY_DATA))
691 delete [] data;
692 else if (flags.any(DYNAMIC_DATA))
628
693
694 delete data;
695
696 flags.clear(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA);
697 data = NULL;
698 }
699
629 /** If there isn't data in the packet, allocate some. */
700 /** If there isn't data in the packet, allocate some. */
630 void allocate();
701 void
702 allocate()
703 {
704 if (data) {
705 assert(flags.none(STATIC_DATA|DYNAMIC_DATA));
706 } else {
707 flags.set(DYNAMIC_DATA|ARRAY_DATA);
708 data = new uint8_t[getSize()];
709 }
710 }
631
711
712
632 /**
633 * Check a functional request against a memory value represented
634 * by a base/size pair and an associated data array. If the
635 * functional request is a read, it may be satisfied by the memory
636 * value. If the functional request is a write, it may update the
637 * memory value.
638 */
639 bool checkFunctional(Printable *obj, Addr base, int size, uint8_t *data);
640
641 /**
642 * Check a functional request against a memory value stored in
643 * another packet (i.e. an in-transit request or response).
644 */
713 /**
714 * Check a functional request against a memory value represented
715 * by a base/size pair and an associated data array. If the
716 * functional request is a read, it may be satisfied by the memory
717 * value. If the functional request is a write, it may update the
718 * memory value.
719 */
720 bool checkFunctional(Printable *obj, Addr base, int size, uint8_t *data);
721
722 /**
723 * Check a functional request against a memory value stored in
724 * another packet (i.e. an in-transit request or response).
725 */
645 bool checkFunctional(PacketPtr otherPkt) {
646 return checkFunctional(otherPkt,
647 otherPkt->getAddr(), otherPkt->getSize(),
648 otherPkt->hasData() ?
649 otherPkt->getPtr<uint8_t>() : NULL);
726 bool
727 checkFunctional(PacketPtr other)
728 {
729 uint8_t *data = other->hasData() ? other->getPtr<uint8_t>() : NULL;
730 return checkFunctional(other, other->getAddr(), other->getSize(),
731 data);
650 }
651
652 /**
653 * Push label for PrintReq (safe to call unconditionally).
654 */
732 }
733
734 /**
735 * Push label for PrintReq (safe to call unconditionally).
736 */
655 void pushLabel(const std::string &lbl) {
656 if (isPrint()) {
657 dynamic_cast<PrintReqState*>(senderState)->pushLabel(lbl);
658 }
737 void
738 pushLabel(const std::string &lbl)
739 {
740 if (isPrint())
741 safe_cast<PrintReqState*>(senderState)->pushLabel(lbl);
659 }
660
661 /**
662 * Pop label for PrintReq (safe to call unconditionally).
663 */
742 }
743
744 /**
745 * Pop label for PrintReq (safe to call unconditionally).
746 */
664 void popLabel() {
665 if (isPrint()) {
666 dynamic_cast<PrintReqState*>(senderState)->popLabel();
667 }
747 void
748 popLabel()
749 {
750 if (isPrint())
751 safe_cast<PrintReqState*>(senderState)->popLabel();
668 }
669
670 void print(std::ostream &o, int verbosity = 0,
671 const std::string &prefix = "") const;
672};
673
674#endif //__MEM_PACKET_HH
752 }
753
754 void print(std::ostream &o, int verbosity = 0,
755 const std::string &prefix = "") const;
756};
757
758#endif //__MEM_PACKET_HH