2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * Copyright (c) 2010 Advanced Micro Devices, Inc.
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.
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.
29 * Authors: Ron Dreslinski
36 * Declaration of the Packet class.
39 #ifndef __MEM_PACKET_HH__
40 #define __MEM_PACKET_HH__
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"
57 typedef Packet *PacketPtr;
58 typedef uint8_t* PacketDataPtr;
59 typedef std::list<PacketPtr> PacketList;
67 * List of all commands associated with a packet.
74 ReadRespWithInvalidate,
85 SCUpgradeReq, // Special "weak" upgrade for StoreCond
87 SCUpgradeFailReq, // Failed SCUpgradeReq in MSHR (never sent)
88 UpgradeFailResp, // Valid for SCUpgradeReq only
93 StoreCondFailReq, // Failed StoreCondReq in MSHR (never sent)
100 // @TODO these should be classified as responses rather than
101 // requests; coding them as requests initially for backwards
103 NetworkNackError, // nacked at network layer (not by protocol)
104 InvalidDestError, // packet dest field invalid
105 BadAddressError, // memory address invalid
106 // Fake simulator-only commands
107 PrintReq, // Print state matching address
108 FlushReq, //request for a cache flush
114 * List of command attributes.
118 IsRead, //!< Data flows from responder to requester
119 IsWrite, //!< Data flows from requester to responder
121 IsPrefetch, //!< Not a demand access
123 NeedsExclusive, //!< Requires exclusive copy to complete in-cache
124 IsRequest, //!< Issued by requester
125 IsResponse, //!< Issue by responder
126 NeedsResponse, //!< Requester needs response from target
129 IsLlsc, //!< Alpha/MIPS LL or SC access
130 HasData, //!< There is an associated payload
131 IsError, //!< Error response
132 IsPrint, //!< Print state matching address (for debugging)
133 IsFlush, //!< Flush the address from caches
134 NUM_COMMAND_ATTRIBUTES
138 * Structure that defines attributes and other data associated
143 /// Set of attribute flags.
144 const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
145 /// Corresponding response for requests; InvalidCmd if no
146 /// response is applicable.
147 const Command response;
148 /// String representation (for printing)
149 const std::string str;
152 /// Array to map Command enum to associated info.
153 static const CommandInfo commandInfo[];
160 testCmdAttrib(MemCmd::Attribute attrib) const
162 return commandInfo[cmd].attributes[attrib] != 0;
167 bool isRead() const { return testCmdAttrib(IsRead); }
168 bool isWrite() const { return testCmdAttrib(IsWrite); }
169 bool isUpgrade() const { return testCmdAttrib(IsUpgrade); }
170 bool isRequest() const { return testCmdAttrib(IsRequest); }
171 bool isResponse() const { return testCmdAttrib(IsResponse); }
172 bool needsExclusive() const { return testCmdAttrib(NeedsExclusive); }
173 bool needsResponse() const { return testCmdAttrib(NeedsResponse); }
174 bool isInvalidate() const { return testCmdAttrib(IsInvalidate); }
175 bool hasData() const { return testCmdAttrib(HasData); }
176 bool isReadWrite() const { return isRead() && isWrite(); }
177 bool isLLSC() const { return testCmdAttrib(IsLlsc); }
178 bool isError() const { return testCmdAttrib(IsError); }
179 bool isPrint() const { return testCmdAttrib(IsPrint); }
180 bool isFlush() const { return testCmdAttrib(IsFlush); }
183 responseCommand() const
185 return commandInfo[cmd].response;
188 /// Return the string to a cmd given by idx.
189 const std::string &toString() const { return commandInfo[cmd].str; }
190 int toInt() const { return (int)cmd; }
192 MemCmd(Command _cmd) : cmd(_cmd) { }
193 MemCmd(int _cmd) : cmd((Command)_cmd) { }
194 MemCmd() : cmd(InvalidCmd) { }
196 bool operator==(MemCmd c2) const { return (cmd == c2.cmd); }
197 bool operator!=(MemCmd c2) const { return (cmd != c2.cmd); }
201 * A Packet is used to encapsulate a transfer between two objects in
202 * the memory system (e.g., the L1 and L2 cache). (In contrast, a
203 * single Request travels all the way from the requester to the
204 * ultimate destination and back, possibly being conveyed by several
205 * different Packets along the way.)
207 class Packet : public FastAlloc, public Printable
210 typedef uint32_t FlagsType;
211 typedef ::Flags<FlagsType> Flags;
212 typedef short NodeID;
215 static const FlagsType PUBLIC_FLAGS = 0x00000000;
216 static const FlagsType PRIVATE_FLAGS = 0x00007F0F;
217 static const FlagsType COPY_FLAGS = 0x0000000F;
219 static const FlagsType SHARED = 0x00000001;
220 // Special control flags
221 /// Special timing-mode atomic snoop for multi-level coherence.
222 static const FlagsType EXPRESS_SNOOP = 0x00000002;
223 /// Does supplier have exclusive copy?
224 /// Useful for multi-level coherence.
225 static const FlagsType SUPPLY_EXCLUSIVE = 0x00000004;
226 // Snoop response flags
227 static const FlagsType MEM_INHIBIT = 0x00000008;
228 /// Are the 'addr' and 'size' fields valid?
229 static const FlagsType VALID_ADDR = 0x00000100;
230 static const FlagsType VALID_SIZE = 0x00000200;
231 /// Is the 'src' field valid?
232 static const FlagsType VALID_SRC = 0x00000400;
233 static const FlagsType VALID_DST = 0x00000800;
234 /// Is the data pointer set to a value that shouldn't be freed
235 /// when the packet is destroyed?
236 static const FlagsType STATIC_DATA = 0x00001000;
237 /// The data pointer points to a value that should be freed when
238 /// the packet is destroyed.
239 static const FlagsType DYNAMIC_DATA = 0x00002000;
240 /// the data pointer points to an array (thus delete []) needs to
241 /// be called on it rather than simply delete.
242 static const FlagsType ARRAY_DATA = 0x00004000;
247 typedef MemCmd::Command Command;
249 /// The command field of the packet.
252 /// A pointer to the original request.
257 * A pointer to the data being transfered. It can be differnt
258 * sizes at each level of the heirarchy so it belongs in the
259 * packet, not request. This may or may not be populated when a
260 * responder recieves the packet. If not populated it memory should
265 /// The address of the request. This address could be virtual or
266 /// physical, depending on the system configuration.
269 /// The size of the request or transfer.
273 * Device address (e.g., bus ID) of the source of the
274 * transaction. The source is not responsible for setting this
275 * field; it is set implicitly by the interconnect when the packet
281 * Device address (e.g., bus ID) of the destination of the
282 * transaction. The special value Broadcast indicates that the
283 * packet should be routed based on its address. This field is
284 * initialized in the constructor and is thus always valid (unlike
285 * addr, size, and src).
290 * The original value of the command field. Only valid when the
291 * current command field is an error condition; in that case, the
292 * previous contents of the command field are copied here. This
293 * field is *not* set on non-error responses.
298 /// Used to calculate latencies for each packet.
301 /// The time at which the packet will be fully transmitted
304 /// The time at which the first chunk of the packet will be transmitted
307 /// The special destination address indicating that the packet
308 /// should be routed based on its address.
309 static const NodeID Broadcast = -1;
312 * A virtual base opaque structure used to hold state associated
313 * with the packet but specific to the sending device (e.g., an
314 * MSHR). A pointer to this state is returned in the packet's
315 * response so that the sender can quickly look up the state
316 * needed to process it. A specific subclass would be derived
317 * from this to carry state specific to a particular sending
322 virtual ~SenderState() {}
326 * Object used to maintain state of a PrintReq. The senderState
327 * field of a PrintReq should always be of this type.
329 class PrintReqState : public SenderState, public FastAlloc
333 * An entry in the label stack.
335 struct LabelStackEntry
337 const std::string label;
340 LabelStackEntry(const std::string &_label, std::string *_prefix);
343 typedef std::list<LabelStackEntry> LabelStack;
344 LabelStack labelStack;
346 std::string *curPrefixPtr;
352 PrintReqState(std::ostream &os, int verbosity = 0);
356 * Returns the current line prefix.
358 const std::string &curPrefix() { return *curPrefixPtr; }
361 * Push a label onto the label stack, and prepend the given
362 * prefix string onto the current prefix. Labels will only be
363 * printed if an object within the label's scope is printed.
365 void pushLabel(const std::string &lbl,
366 const std::string &prefix = " ");
369 * Pop a label off the label stack.
374 * Print all of the pending unprinted labels on the
375 * stack. Called by printObj(), so normally not called by
376 * users unless bypassing printObj().
381 * Print a Printable object to os, because it matched the
382 * address on a PrintReq.
384 void printObj(Printable *obj);
388 * This packet's sender state. Devices should use dynamic_cast<>
389 * to cast to the state appropriate to the sender. The intent of
390 * this variable is to allow a device to attach extra information
391 * to a request. A response packet must return the sender state
392 * that was attached to the original request (even if a new packet
395 SenderState *senderState;
397 /// Return the string name of the cmd field (for debugging and
399 const std::string &cmdString() const { return cmd.toString(); }
401 /// Return the index of this command.
402 inline int cmdToIndex() const { return cmd.toInt(); }
404 bool isRead() const { return cmd.isRead(); }
405 bool isWrite() const { return cmd.isWrite(); }
406 bool isUpgrade() const { return cmd.isUpgrade(); }
407 bool isRequest() const { return cmd.isRequest(); }
408 bool isResponse() const { return cmd.isResponse(); }
409 bool needsExclusive() const { return cmd.needsExclusive(); }
410 bool needsResponse() const { return cmd.needsResponse(); }
411 bool isInvalidate() const { return cmd.isInvalidate(); }
412 bool hasData() const { return cmd.hasData(); }
413 bool isReadWrite() const { return cmd.isReadWrite(); }
414 bool isLLSC() const { return cmd.isLLSC(); }
415 bool isError() const { return cmd.isError(); }
416 bool isPrint() const { return cmd.isPrint(); }
417 bool isFlush() const { return cmd.isFlush(); }
420 void assertMemInhibit() { flags.set(MEM_INHIBIT); }
421 bool memInhibitAsserted() { return flags.isSet(MEM_INHIBIT); }
422 void assertShared() { flags.set(SHARED); }
423 bool sharedAsserted() { return flags.isSet(SHARED); }
425 // Special control flags
426 void setExpressSnoop() { flags.set(EXPRESS_SNOOP); }
427 bool isExpressSnoop() { return flags.isSet(EXPRESS_SNOOP); }
428 void setSupplyExclusive() { flags.set(SUPPLY_EXCLUSIVE); }
429 void clearSupplyExclusive() { flags.clear(SUPPLY_EXCLUSIVE); }
430 bool isSupplyExclusive() { return flags.isSet(SUPPLY_EXCLUSIVE); }
432 // Network error conditions... encapsulate them as methods since
433 // their encoding keeps changing (from result field to command
438 assert(isResponse());
439 cmd = MemCmd::NetworkNackError;
445 assert(isResponse());
446 cmd = MemCmd::BadAddressError;
449 bool wasNacked() const { return cmd == MemCmd::NetworkNackError; }
450 bool hadBadAddress() const { return cmd == MemCmd::BadAddressError; }
451 void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
453 bool isSrcValid() { return flags.isSet(VALID_SRC); }
454 /// Accessor function to get the source index of the packet.
455 NodeID getSrc() const { assert(flags.isSet(VALID_SRC)); return src; }
456 /// Accessor function to set the source index of the packet.
457 void setSrc(NodeID _src) { src = _src; flags.set(VALID_SRC); }
458 /// Reset source field, e.g. to retransmit packet on different bus.
459 void clearSrc() { flags.clear(VALID_SRC); }
461 bool isDestValid() { return flags.isSet(VALID_DST); }
462 /// Accessor function for the destination index of the packet.
463 NodeID getDest() const { assert(flags.isSet(VALID_DST)); return dest; }
464 /// Accessor function to set the destination index of the packet.
465 void setDest(NodeID _dest) { dest = _dest; flags.set(VALID_DST); }
467 Addr getAddr() const { assert(flags.isSet(VALID_ADDR)); return addr; }
468 unsigned getSize() const { assert(flags.isSet(VALID_SIZE)); return size; }
469 Addr getOffset(int blkSize) const { return getAddr() & (Addr)(blkSize - 1); }
472 * It has been determined that the SC packet should successfully update
473 * memory. Therefore, convert this SC packet to a normal write.
480 cmd = MemCmd::WriteReq;
484 * When ruby is in use, Ruby will monitor the cache line and thus M5
485 * phys memory should treat LL ops as normal reads.
492 cmd = MemCmd::ReadReq;
496 * Constructor. Note that a Request object must be constructed
497 * first, but the Requests's physical address and size fields need
498 * not be valid. The command and destination addresses must be
501 Packet(Request *_req, MemCmd _cmd, NodeID _dest)
502 : flags(VALID_DST), cmd(_cmd), req(_req), data(NULL),
503 dest(_dest), time(curTick()), senderState(NULL)
505 if (req->hasPaddr()) {
506 addr = req->getPaddr();
507 flags.set(VALID_ADDR);
509 if (req->hasSize()) {
510 size = req->getSize();
511 flags.set(VALID_SIZE);
516 * Alternate constructor if you are trying to create a packet with
517 * a request that is for a whole block, not the address from the
518 * req. this allows for overriding the size/addr of the req.
520 Packet(Request *_req, MemCmd _cmd, NodeID _dest, int _blkSize)
521 : flags(VALID_DST), cmd(_cmd), req(_req), data(NULL),
522 dest(_dest), time(curTick()), senderState(NULL)
524 if (req->hasPaddr()) {
525 addr = req->getPaddr() & ~(_blkSize - 1);
526 flags.set(VALID_ADDR);
529 flags.set(VALID_SIZE);
533 * Alternate constructor for copying a packet. Copy all fields
534 * *except* if the original packet's data was dynamic, don't copy
535 * that, as we can't guarantee that the new packet's lifetime is
536 * less than that of the original packet. In this case the new
537 * packet should allocate its own data.
539 Packet(Packet *pkt, bool clearFlags = false)
540 : cmd(pkt->cmd), req(pkt->req),
541 data(pkt->flags.isSet(STATIC_DATA) ? pkt->data : NULL),
542 addr(pkt->addr), size(pkt->size), src(pkt->src), dest(pkt->dest),
543 time(curTick()), senderState(pkt->senderState)
546 flags.set(pkt->flags & COPY_FLAGS);
548 flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE|VALID_SRC|VALID_DST));
549 flags.set(pkt->flags & STATIC_DATA);
553 * clean up packet variables
557 // If this is a request packet for which there's no response,
558 // delete the request object here, since the requester will
559 // never get the chance.
560 if (req && isRequest() && !needsResponse())
566 * Reinitialize packet address and size from the associated
567 * Request object, and reset other fields that may have been
568 * modified by a previous transaction. Typically called when a
569 * statically allocated Request/Packet pair is reused for multiple
575 assert(req->hasPaddr());
577 addr = req->getPaddr();
578 size = req->getSize();
581 flags.set(VALID_ADDR|VALID_SIZE);
586 * Take a request packet and modify it in place to be suitable for
587 * returning as a response to that request. The source and
588 * destination fields are *not* modified, as is appropriate for
594 assert(needsResponse());
597 cmd = cmd.responseCommand();
599 // responses are never express, even if the snoop that
600 // triggered them was
601 flags.clear(EXPRESS_SNOOP);
604 flags.set(VALID_DST, flags.isSet(VALID_SRC));
605 flags.clear(VALID_SRC);
621 * Take a request packet that has been returned as NACKED and
622 * modify it so that it can be sent out again. Only packets that
623 * need a response can be NACKED, so verify that that is true.
630 assert(needsResponse());
635 setSize(unsigned size)
637 assert(!flags.isSet(VALID_SIZE));
640 flags.set(VALID_SIZE);
645 * Set the data pointer to the following value that should not be
648 template <typename T>
652 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
653 data = (PacketDataPtr)p;
654 flags.set(STATIC_DATA);
658 * Set the data pointer to a value that should have delete []
661 template <typename T>
663 dataDynamicArray(T *p)
665 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
666 data = (PacketDataPtr)p;
667 flags.set(DYNAMIC_DATA|ARRAY_DATA);
671 * set the data pointer to a value that should have delete called
674 template <typename T>
678 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
679 data = (PacketDataPtr)p;
680 flags.set(DYNAMIC_DATA);
684 * get a pointer to the data ptr.
686 template <typename T>
688 getPtr(bool null_ok = false)
690 assert(null_ok || flags.isSet(STATIC_DATA|DYNAMIC_DATA));
695 * return the value of what is pointed to in the packet.
697 template <typename T>
701 * set the value in the data pointer to v.
703 template <typename T>
707 * Copy data into the packet from the provided pointer.
712 if (p != getPtr<uint8_t>())
713 std::memcpy(getPtr<uint8_t>(), p, getSize());
717 * Copy data into the packet from the provided block pointer,
718 * which is aligned to the given block size.
721 setDataFromBlock(uint8_t *blk_data, int blkSize)
723 setData(blk_data + getOffset(blkSize));
727 * Copy data from the packet to the provided block pointer, which
728 * is aligned to the given block size.
731 writeData(uint8_t *p)
733 std::memcpy(p, getPtr<uint8_t>(), getSize());
737 * Copy data from the packet to the memory at the provided pointer.
740 writeDataToBlock(uint8_t *blk_data, int blkSize)
742 writeData(blk_data + getOffset(blkSize));
746 * delete the data pointed to in the data pointer. Ok to call to
747 * matter how data was allocted.
752 if (flags.isSet(ARRAY_DATA))
754 else if (flags.isSet(DYNAMIC_DATA))
757 flags.clear(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA);
761 /** If there isn't data in the packet, allocate some. */
766 assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
770 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
771 flags.set(DYNAMIC_DATA|ARRAY_DATA);
772 data = new uint8_t[getSize()];
776 * Check a functional request against a memory value represented
777 * by a base/size pair and an associated data array. If the
778 * functional request is a read, it may be satisfied by the memory
779 * value. If the functional request is a write, it may update the
782 bool checkFunctional(Printable *obj, Addr base, int size, uint8_t *data);
785 * Check a functional request against a memory value stored in
786 * another packet (i.e. an in-transit request or response).
789 checkFunctional(PacketPtr other)
791 uint8_t *data = other->hasData() ? other->getPtr<uint8_t>() : NULL;
792 return checkFunctional(other, other->getAddr(), other->getSize(),
797 * Push label for PrintReq (safe to call unconditionally).
800 pushLabel(const std::string &lbl)
803 safe_cast<PrintReqState*>(senderState)->pushLabel(lbl);
807 * Pop label for PrintReq (safe to call unconditionally).
813 safe_cast<PrintReqState*>(senderState)->popLabel();
816 void print(std::ostream &o, int verbosity = 0,
817 const std::string &prefix = "") const;
820 #endif //__MEM_PACKET_HH