ce5748c24c78622b52135cd56d873f4708c3829c
[gem5.git] / src / mem / packet.hh
1 /*
2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * Copyright (c) 2010 Advanced Micro Devices, Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Ron Dreslinski
30 * Steve Reinhardt
31 * Ali Saidi
32 */
33
34 /**
35 * @file
36 * Declaration of the Packet class.
37 */
38
39 #ifndef __MEM_PACKET_HH__
40 #define __MEM_PACKET_HH__
41
42 #include <bitset>
43 #include <cassert>
44 #include <list>
45
46 #include "base/cast.hh"
47 #include "base/compiler.hh"
48 #include "base/fast_alloc.hh"
49 #include "base/flags.hh"
50 #include "base/misc.hh"
51 #include "base/printable.hh"
52 #include "base/types.hh"
53 #include "mem/request.hh"
54 #include "sim/core.hh"
55
56 class Packet;
57 typedef Packet *PacketPtr;
58 typedef uint8_t* PacketDataPtr;
59 typedef std::list<PacketPtr> PacketList;
60
61 class MemCmd
62 {
63 friend class Packet;
64
65 public:
66 /**
67 * List of all commands associated with a packet.
68 */
69 enum Command
70 {
71 InvalidCmd,
72 ReadReq,
73 ReadResp,
74 ReadRespWithInvalidate,
75 WriteReq,
76 WriteResp,
77 Writeback,
78 SoftPFReq,
79 HardPFReq,
80 SoftPFResp,
81 HardPFResp,
82 WriteInvalidateReq,
83 WriteInvalidateResp,
84 UpgradeReq,
85 SCUpgradeReq, // Special "weak" upgrade for StoreCond
86 UpgradeResp,
87 SCUpgradeFailReq, // Failed SCUpgradeReq in MSHR (never sent)
88 UpgradeFailResp, // Valid for SCUpgradeReq only
89 ReadExReq,
90 ReadExResp,
91 LoadLockedReq,
92 StoreCondReq,
93 StoreCondFailReq, // Failed StoreCondReq in MSHR (never sent)
94 StoreCondResp,
95 SwapReq,
96 SwapResp,
97 MessageReq,
98 MessageResp,
99 // Error responses
100 // @TODO these should be classified as responses rather than
101 // requests; coding them as requests initially for backwards
102 // compatibility
103 NetworkNackError, // nacked at network layer (not by protocol)
104 InvalidDestError, // packet dest field invalid
105 BadAddressError, // memory address invalid
106 FunctionalReadError, // unable to fulfill functional read
107 FunctionalWriteError, // unable to fulfill functional write
108 // Fake simulator-only commands
109 PrintReq, // Print state matching address
110 FlushReq, //request for a cache flush
111 InvalidationReq, // request for address to be invalidated from lsq
112 NUM_MEM_CMDS
113 };
114
115 private:
116 /**
117 * List of command attributes.
118 */
119 enum Attribute
120 {
121 IsRead, //!< Data flows from responder to requester
122 IsWrite, //!< Data flows from requester to responder
123 IsUpgrade,
124 IsPrefetch, //!< Not a demand access
125 IsInvalidate,
126 NeedsExclusive, //!< Requires exclusive copy to complete in-cache
127 IsRequest, //!< Issued by requester
128 IsResponse, //!< Issue by responder
129 NeedsResponse, //!< Requester needs response from target
130 IsSWPrefetch,
131 IsHWPrefetch,
132 IsLlsc, //!< Alpha/MIPS LL or SC access
133 HasData, //!< There is an associated payload
134 IsError, //!< Error response
135 IsPrint, //!< Print state matching address (for debugging)
136 IsFlush, //!< Flush the address from caches
137 NUM_COMMAND_ATTRIBUTES
138 };
139
140 /**
141 * Structure that defines attributes and other data associated
142 * with a Command.
143 */
144 struct CommandInfo
145 {
146 /// Set of attribute flags.
147 const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
148 /// Corresponding response for requests; InvalidCmd if no
149 /// response is applicable.
150 const Command response;
151 /// String representation (for printing)
152 const std::string str;
153 };
154
155 /// Array to map Command enum to associated info.
156 static const CommandInfo commandInfo[];
157
158 private:
159
160 Command cmd;
161
162 bool
163 testCmdAttrib(MemCmd::Attribute attrib) const
164 {
165 return commandInfo[cmd].attributes[attrib] != 0;
166 }
167
168 public:
169
170 bool isRead() const { return testCmdAttrib(IsRead); }
171 bool isWrite() const { return testCmdAttrib(IsWrite); }
172 bool isUpgrade() const { return testCmdAttrib(IsUpgrade); }
173 bool isRequest() const { return testCmdAttrib(IsRequest); }
174 bool isResponse() const { return testCmdAttrib(IsResponse); }
175 bool needsExclusive() const { return testCmdAttrib(NeedsExclusive); }
176 bool needsResponse() const { return testCmdAttrib(NeedsResponse); }
177 bool isInvalidate() const { return testCmdAttrib(IsInvalidate); }
178 bool hasData() const { return testCmdAttrib(HasData); }
179 bool isReadWrite() const { return isRead() && isWrite(); }
180 bool isLLSC() const { return testCmdAttrib(IsLlsc); }
181 bool isError() const { return testCmdAttrib(IsError); }
182 bool isPrint() const { return testCmdAttrib(IsPrint); }
183 bool isFlush() const { return testCmdAttrib(IsFlush); }
184
185 const Command
186 responseCommand() const
187 {
188 return commandInfo[cmd].response;
189 }
190
191 /// Return the string to a cmd given by idx.
192 const std::string &toString() const { return commandInfo[cmd].str; }
193 int toInt() const { return (int)cmd; }
194
195 MemCmd(Command _cmd) : cmd(_cmd) { }
196 MemCmd(int _cmd) : cmd((Command)_cmd) { }
197 MemCmd() : cmd(InvalidCmd) { }
198
199 bool operator==(MemCmd c2) const { return (cmd == c2.cmd); }
200 bool operator!=(MemCmd c2) const { return (cmd != c2.cmd); }
201 };
202
203 /**
204 * A Packet is used to encapsulate a transfer between two objects in
205 * the memory system (e.g., the L1 and L2 cache). (In contrast, a
206 * single Request travels all the way from the requester to the
207 * ultimate destination and back, possibly being conveyed by several
208 * different Packets along the way.)
209 */
210 class Packet : public FastAlloc, public Printable
211 {
212 public:
213 typedef uint32_t FlagsType;
214 typedef ::Flags<FlagsType> Flags;
215 typedef short NodeID;
216
217 private:
218 static const FlagsType PUBLIC_FLAGS = 0x00000000;
219 static const FlagsType PRIVATE_FLAGS = 0x00007F0F;
220 static const FlagsType COPY_FLAGS = 0x0000000F;
221
222 static const FlagsType SHARED = 0x00000001;
223 // Special control flags
224 /// Special timing-mode atomic snoop for multi-level coherence.
225 static const FlagsType EXPRESS_SNOOP = 0x00000002;
226 /// Does supplier have exclusive copy?
227 /// Useful for multi-level coherence.
228 static const FlagsType SUPPLY_EXCLUSIVE = 0x00000004;
229 // Snoop response flags
230 static const FlagsType MEM_INHIBIT = 0x00000008;
231 /// Are the 'addr' and 'size' fields valid?
232 static const FlagsType VALID_ADDR = 0x00000100;
233 static const FlagsType VALID_SIZE = 0x00000200;
234 /// Is the 'src' field valid?
235 static const FlagsType VALID_SRC = 0x00000400;
236 static const FlagsType VALID_DST = 0x00000800;
237 /// Is the data pointer set to a value that shouldn't be freed
238 /// when the packet is destroyed?
239 static const FlagsType STATIC_DATA = 0x00001000;
240 /// The data pointer points to a value that should be freed when
241 /// the packet is destroyed.
242 static const FlagsType DYNAMIC_DATA = 0x00002000;
243 /// the data pointer points to an array (thus delete []) needs to
244 /// be called on it rather than simply delete.
245 static const FlagsType ARRAY_DATA = 0x00004000;
246 /// suppress the error if this packet encounters a functional
247 /// access failure.
248 static const FlagsType SUPPRESS_FUNC_ERROR = 0x00008000;
249
250 Flags flags;
251
252 public:
253 typedef MemCmd::Command Command;
254
255 /// The command field of the packet.
256 MemCmd cmd;
257
258 /// A pointer to the original request.
259 RequestPtr req;
260
261 private:
262 /**
263 * A pointer to the data being transfered. It can be differnt
264 * sizes at each level of the heirarchy so it belongs in the
265 * packet, not request. This may or may not be populated when a
266 * responder recieves the packet. If not populated it memory should
267 * be allocated.
268 */
269 PacketDataPtr data;
270
271 /// The address of the request. This address could be virtual or
272 /// physical, depending on the system configuration.
273 Addr addr;
274
275 /// The size of the request or transfer.
276 unsigned size;
277
278 /**
279 * Device address (e.g., bus ID) of the source of the
280 * transaction. The source is not responsible for setting this
281 * field; it is set implicitly by the interconnect when the packet
282 * is first sent.
283 */
284 NodeID src;
285
286 /**
287 * Device address (e.g., bus ID) of the destination of the
288 * transaction. The special value Broadcast indicates that the
289 * packet should be routed based on its address. This field is
290 * initialized in the constructor and is thus always valid (unlike
291 * addr, size, and src).
292 */
293 NodeID dest;
294
295 /**
296 * The original value of the command field. Only valid when the
297 * current command field is an error condition; in that case, the
298 * previous contents of the command field are copied here. This
299 * field is *not* set on non-error responses.
300 */
301 MemCmd origCmd;
302
303 /**
304 * These values specify the range of bytes found that satisfy a
305 * functional read.
306 */
307 uint16_t bytesValidStart;
308 uint16_t bytesValidEnd;
309
310 public:
311 /// Used to calculate latencies for each packet.
312 Tick time;
313
314 /// The time at which the packet will be fully transmitted
315 Tick finishTime;
316
317 /// The time at which the first chunk of the packet will be transmitted
318 Tick firstWordTime;
319
320 /// The special destination address indicating that the packet
321 /// should be routed based on its address.
322 static const NodeID Broadcast = -1;
323
324 /**
325 * A virtual base opaque structure used to hold state associated
326 * with the packet but specific to the sending device (e.g., an
327 * MSHR). A pointer to this state is returned in the packet's
328 * response so that the sender can quickly look up the state
329 * needed to process it. A specific subclass would be derived
330 * from this to carry state specific to a particular sending
331 * device.
332 */
333 struct SenderState
334 {
335 virtual ~SenderState() {}
336 };
337
338 /**
339 * Object used to maintain state of a PrintReq. The senderState
340 * field of a PrintReq should always be of this type.
341 */
342 class PrintReqState : public SenderState, public FastAlloc
343 {
344 private:
345 /**
346 * An entry in the label stack.
347 */
348 struct LabelStackEntry
349 {
350 const std::string label;
351 std::string *prefix;
352 bool labelPrinted;
353 LabelStackEntry(const std::string &_label, std::string *_prefix);
354 };
355
356 typedef std::list<LabelStackEntry> LabelStack;
357 LabelStack labelStack;
358
359 std::string *curPrefixPtr;
360
361 public:
362 std::ostream &os;
363 const int verbosity;
364
365 PrintReqState(std::ostream &os, int verbosity = 0);
366 ~PrintReqState();
367
368 /**
369 * Returns the current line prefix.
370 */
371 const std::string &curPrefix() { return *curPrefixPtr; }
372
373 /**
374 * Push a label onto the label stack, and prepend the given
375 * prefix string onto the current prefix. Labels will only be
376 * printed if an object within the label's scope is printed.
377 */
378 void pushLabel(const std::string &lbl,
379 const std::string &prefix = " ");
380
381 /**
382 * Pop a label off the label stack.
383 */
384 void popLabel();
385
386 /**
387 * Print all of the pending unprinted labels on the
388 * stack. Called by printObj(), so normally not called by
389 * users unless bypassing printObj().
390 */
391 void printLabels();
392
393 /**
394 * Print a Printable object to os, because it matched the
395 * address on a PrintReq.
396 */
397 void printObj(Printable *obj);
398 };
399
400 /**
401 * This packet's sender state. Devices should use dynamic_cast<>
402 * to cast to the state appropriate to the sender. The intent of
403 * this variable is to allow a device to attach extra information
404 * to a request. A response packet must return the sender state
405 * that was attached to the original request (even if a new packet
406 * is created).
407 */
408 SenderState *senderState;
409
410 /// Return the string name of the cmd field (for debugging and
411 /// tracing).
412 const std::string &cmdString() const { return cmd.toString(); }
413
414 /// Return the index of this command.
415 inline int cmdToIndex() const { return cmd.toInt(); }
416
417 bool isRead() const { return cmd.isRead(); }
418 bool isWrite() const { return cmd.isWrite(); }
419 bool isUpgrade() const { return cmd.isUpgrade(); }
420 bool isRequest() const { return cmd.isRequest(); }
421 bool isResponse() const { return cmd.isResponse(); }
422 bool needsExclusive() const { return cmd.needsExclusive(); }
423 bool needsResponse() const { return cmd.needsResponse(); }
424 bool isInvalidate() const { return cmd.isInvalidate(); }
425 bool hasData() const { return cmd.hasData(); }
426 bool isReadWrite() const { return cmd.isReadWrite(); }
427 bool isLLSC() const { return cmd.isLLSC(); }
428 bool isError() const { return cmd.isError(); }
429 bool isPrint() const { return cmd.isPrint(); }
430 bool isFlush() const { return cmd.isFlush(); }
431
432 // Snoop flags
433 void assertMemInhibit() { flags.set(MEM_INHIBIT); }
434 bool memInhibitAsserted() { return flags.isSet(MEM_INHIBIT); }
435 void assertShared() { flags.set(SHARED); }
436 bool sharedAsserted() { return flags.isSet(SHARED); }
437
438 // Special control flags
439 void setExpressSnoop() { flags.set(EXPRESS_SNOOP); }
440 bool isExpressSnoop() { return flags.isSet(EXPRESS_SNOOP); }
441 void setSupplyExclusive() { flags.set(SUPPLY_EXCLUSIVE); }
442 void clearSupplyExclusive() { flags.clear(SUPPLY_EXCLUSIVE); }
443 bool isSupplyExclusive() { return flags.isSet(SUPPLY_EXCLUSIVE); }
444 void setSuppressFuncError() { flags.set(SUPPRESS_FUNC_ERROR); }
445 bool suppressFuncError() { return flags.isSet(SUPPRESS_FUNC_ERROR); }
446
447 // Network error conditions... encapsulate them as methods since
448 // their encoding keeps changing (from result field to command
449 // field, etc.)
450 void
451 setNacked()
452 {
453 assert(isResponse());
454 cmd = MemCmd::NetworkNackError;
455 }
456
457 void
458 setBadAddress()
459 {
460 assert(isResponse());
461 cmd = MemCmd::BadAddressError;
462 }
463
464 bool wasNacked() const { return cmd == MemCmd::NetworkNackError; }
465 bool hadBadAddress() const { return cmd == MemCmd::BadAddressError; }
466 void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
467
468 bool isSrcValid() { return flags.isSet(VALID_SRC); }
469 /// Accessor function to get the source index of the packet.
470 NodeID getSrc() const { assert(flags.isSet(VALID_SRC)); return src; }
471 /// Accessor function to set the source index of the packet.
472 void setSrc(NodeID _src) { src = _src; flags.set(VALID_SRC); }
473 /// Reset source field, e.g. to retransmit packet on different bus.
474 void clearSrc() { flags.clear(VALID_SRC); }
475
476 bool isDestValid() { return flags.isSet(VALID_DST); }
477 /// Accessor function for the destination index of the packet.
478 NodeID getDest() const { assert(flags.isSet(VALID_DST)); return dest; }
479 /// Accessor function to set the destination index of the packet.
480 void setDest(NodeID _dest) { dest = _dest; flags.set(VALID_DST); }
481
482 Addr getAddr() const { assert(flags.isSet(VALID_ADDR)); return addr; }
483 unsigned getSize() const { assert(flags.isSet(VALID_SIZE)); return size; }
484 Addr getOffset(int blkSize) const { return getAddr() & (Addr)(blkSize - 1); }
485
486 /**
487 * It has been determined that the SC packet should successfully update
488 * memory. Therefore, convert this SC packet to a normal write.
489 */
490 void
491 convertScToWrite()
492 {
493 assert(isLLSC());
494 assert(isWrite());
495 cmd = MemCmd::WriteReq;
496 }
497
498 /**
499 * When ruby is in use, Ruby will monitor the cache line and thus M5
500 * phys memory should treat LL ops as normal reads.
501 */
502 void
503 convertLlToRead()
504 {
505 assert(isLLSC());
506 assert(isRead());
507 cmd = MemCmd::ReadReq;
508 }
509
510 /**
511 * Constructor. Note that a Request object must be constructed
512 * first, but the Requests's physical address and size fields need
513 * not be valid. The command and destination addresses must be
514 * supplied.
515 */
516 Packet(Request *_req, MemCmd _cmd, NodeID _dest)
517 : flags(VALID_DST), cmd(_cmd), req(_req), data(NULL),
518 dest(_dest), bytesValidStart(0), bytesValidEnd(0),
519 time(curTick()), senderState(NULL)
520 {
521 if (req->hasPaddr()) {
522 addr = req->getPaddr();
523 flags.set(VALID_ADDR);
524 }
525 if (req->hasSize()) {
526 size = req->getSize();
527 flags.set(VALID_SIZE);
528 }
529 }
530
531 /**
532 * Alternate constructor if you are trying to create a packet with
533 * a request that is for a whole block, not the address from the
534 * req. this allows for overriding the size/addr of the req.
535 */
536 Packet(Request *_req, MemCmd _cmd, NodeID _dest, int _blkSize)
537 : flags(VALID_DST), cmd(_cmd), req(_req), data(NULL),
538 dest(_dest), bytesValidStart(0), bytesValidEnd(0),
539 time(curTick()), senderState(NULL)
540 {
541 if (req->hasPaddr()) {
542 addr = req->getPaddr() & ~(_blkSize - 1);
543 flags.set(VALID_ADDR);
544 }
545 size = _blkSize;
546 flags.set(VALID_SIZE);
547 }
548
549 /**
550 * Alternate constructor for copying a packet. Copy all fields
551 * *except* if the original packet's data was dynamic, don't copy
552 * that, as we can't guarantee that the new packet's lifetime is
553 * less than that of the original packet. In this case the new
554 * packet should allocate its own data.
555 */
556 Packet(Packet *pkt, bool clearFlags = false)
557 : cmd(pkt->cmd), req(pkt->req),
558 data(pkt->flags.isSet(STATIC_DATA) ? pkt->data : NULL),
559 addr(pkt->addr), size(pkt->size), src(pkt->src), dest(pkt->dest),
560 bytesValidStart(pkt->bytesValidStart), bytesValidEnd(pkt->bytesValidEnd),
561 time(curTick()), senderState(pkt->senderState)
562 {
563 if (!clearFlags)
564 flags.set(pkt->flags & COPY_FLAGS);
565
566 flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE|VALID_SRC|VALID_DST));
567 flags.set(pkt->flags & STATIC_DATA);
568
569 }
570
571 /**
572 * clean up packet variables
573 */
574 ~Packet()
575 {
576 // If this is a request packet for which there's no response,
577 // delete the request object here, since the requester will
578 // never get the chance.
579 if (req && isRequest() && !needsResponse())
580 delete req;
581 deleteData();
582 }
583
584 /**
585 * Reinitialize packet address and size from the associated
586 * Request object, and reset other fields that may have been
587 * modified by a previous transaction. Typically called when a
588 * statically allocated Request/Packet pair is reused for multiple
589 * transactions.
590 */
591 void
592 reinitFromRequest()
593 {
594 assert(req->hasPaddr());
595 flags = 0;
596 addr = req->getPaddr();
597 size = req->getSize();
598 time = req->time();
599
600 flags.set(VALID_ADDR|VALID_SIZE);
601 deleteData();
602 }
603
604 /**
605 * Take a request packet and modify it in place to be suitable for
606 * returning as a response to that request. The source and
607 * destination fields are *not* modified, as is appropriate for
608 * atomic accesses.
609 */
610 void
611 makeResponse()
612 {
613 assert(needsResponse());
614 assert(isRequest());
615 origCmd = cmd;
616 cmd = cmd.responseCommand();
617
618 // responses are never express, even if the snoop that
619 // triggered them was
620 flags.clear(EXPRESS_SNOOP);
621
622 dest = src;
623 flags.set(VALID_DST, flags.isSet(VALID_SRC));
624 flags.clear(VALID_SRC);
625 }
626
627 void
628 makeAtomicResponse()
629 {
630 makeResponse();
631 }
632
633 void
634 makeTimingResponse()
635 {
636 makeResponse();
637 }
638
639 void
640 setFunctionalResponseStatus(bool success)
641 {
642 if (!success) {
643 if (isWrite()) {
644 cmd = MemCmd::FunctionalWriteError;
645 } else {
646 cmd = MemCmd::FunctionalReadError;
647 }
648 }
649 }
650
651 /**
652 * Take a request packet that has been returned as NACKED and
653 * modify it so that it can be sent out again. Only packets that
654 * need a response can be NACKED, so verify that that is true.
655 */
656 void
657 reinitNacked()
658 {
659 assert(wasNacked());
660 cmd = origCmd;
661 assert(needsResponse());
662 setDest(Broadcast);
663 }
664
665 void
666 setSize(unsigned size)
667 {
668 assert(!flags.isSet(VALID_SIZE));
669
670 this->size = size;
671 flags.set(VALID_SIZE);
672 }
673
674
675 /**
676 * Set the data pointer to the following value that should not be
677 * freed.
678 */
679 template <typename T>
680 void
681 dataStatic(T *p)
682 {
683 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
684 data = (PacketDataPtr)p;
685 flags.set(STATIC_DATA);
686 }
687
688 /**
689 * Set the data pointer to a value that should have delete []
690 * called on it.
691 */
692 template <typename T>
693 void
694 dataDynamicArray(T *p)
695 {
696 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
697 data = (PacketDataPtr)p;
698 flags.set(DYNAMIC_DATA|ARRAY_DATA);
699 }
700
701 /**
702 * set the data pointer to a value that should have delete called
703 * on it.
704 */
705 template <typename T>
706 void
707 dataDynamic(T *p)
708 {
709 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
710 data = (PacketDataPtr)p;
711 flags.set(DYNAMIC_DATA);
712 }
713
714 /**
715 * get a pointer to the data ptr.
716 */
717 template <typename T>
718 T*
719 getPtr(bool null_ok = false)
720 {
721 assert(null_ok || flags.isSet(STATIC_DATA|DYNAMIC_DATA));
722 return (T*)data;
723 }
724
725 /**
726 * return the value of what is pointed to in the packet.
727 */
728 template <typename T>
729 T get();
730
731 /**
732 * set the value in the data pointer to v.
733 */
734 template <typename T>
735 void set(T v);
736
737 /**
738 * Copy data into the packet from the provided pointer.
739 */
740 void
741 setData(uint8_t *p)
742 {
743 if (p != getPtr<uint8_t>())
744 std::memcpy(getPtr<uint8_t>(), p, getSize());
745 }
746
747 /**
748 * Copy data into the packet from the provided block pointer,
749 * which is aligned to the given block size.
750 */
751 void
752 setDataFromBlock(uint8_t *blk_data, int blkSize)
753 {
754 setData(blk_data + getOffset(blkSize));
755 }
756
757 /**
758 * Copy data from the packet to the provided block pointer, which
759 * is aligned to the given block size.
760 */
761 void
762 writeData(uint8_t *p)
763 {
764 std::memcpy(p, getPtr<uint8_t>(), getSize());
765 }
766
767 /**
768 * Copy data from the packet to the memory at the provided pointer.
769 */
770 void
771 writeDataToBlock(uint8_t *blk_data, int blkSize)
772 {
773 writeData(blk_data + getOffset(blkSize));
774 }
775
776 /**
777 * delete the data pointed to in the data pointer. Ok to call to
778 * matter how data was allocted.
779 */
780 void
781 deleteData()
782 {
783 if (flags.isSet(ARRAY_DATA))
784 delete [] data;
785 else if (flags.isSet(DYNAMIC_DATA))
786 delete data;
787
788 flags.clear(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA);
789 data = NULL;
790 }
791
792 /** If there isn't data in the packet, allocate some. */
793 void
794 allocate()
795 {
796 if (data) {
797 assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
798 return;
799 }
800
801 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
802 flags.set(DYNAMIC_DATA|ARRAY_DATA);
803 data = new uint8_t[getSize()];
804 }
805
806 /**
807 * Check a functional request against a memory value represented
808 * by a base/size pair and an associated data array. If the
809 * functional request is a read, it may be satisfied by the memory
810 * value. If the functional request is a write, it may update the
811 * memory value.
812 */
813 bool checkFunctional(Printable *obj, Addr base, int size, uint8_t *data);
814
815 /**
816 * Check a functional request against a memory value stored in
817 * another packet (i.e. an in-transit request or response).
818 */
819 bool
820 checkFunctional(PacketPtr other)
821 {
822 uint8_t *data = other->hasData() ? other->getPtr<uint8_t>() : NULL;
823 return checkFunctional(other, other->getAddr(), other->getSize(),
824 data);
825 }
826
827 /**
828 * Push label for PrintReq (safe to call unconditionally).
829 */
830 void
831 pushLabel(const std::string &lbl)
832 {
833 if (isPrint())
834 safe_cast<PrintReqState*>(senderState)->pushLabel(lbl);
835 }
836
837 /**
838 * Pop label for PrintReq (safe to call unconditionally).
839 */
840 void
841 popLabel()
842 {
843 if (isPrint())
844 safe_cast<PrintReqState*>(senderState)->popLabel();
845 }
846
847 void print(std::ostream &o, int verbosity = 0,
848 const std::string &prefix = "") const;
849 };
850
851 #endif //__MEM_PACKET_HH