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 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
116 * List of command attributes.
120 IsRead, //!< Data flows from responder to requester
121 IsWrite, //!< Data flows from requester to responder
123 IsPrefetch, //!< Not a demand access
125 NeedsExclusive, //!< Requires exclusive copy to complete in-cache
126 IsRequest, //!< Issued by requester
127 IsResponse, //!< Issue by responder
128 NeedsResponse, //!< Requester needs response from target
131 IsLlsc, //!< Alpha/MIPS LL or SC access
132 HasData, //!< There is an associated payload
133 IsError, //!< Error response
134 IsPrint, //!< Print state matching address (for debugging)
135 IsFlush, //!< Flush the address from caches
136 NUM_COMMAND_ATTRIBUTES
140 * Structure that defines attributes and other data associated
145 /// Set of attribute flags.
146 const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
147 /// Corresponding response for requests; InvalidCmd if no
148 /// response is applicable.
149 const Command response;
150 /// String representation (for printing)
151 const std::string str;
154 /// Array to map Command enum to associated info.
155 static const CommandInfo commandInfo[];
162 testCmdAttrib(MemCmd::Attribute attrib) const
164 return commandInfo[cmd].attributes[attrib] != 0;
169 bool isRead() const { return testCmdAttrib(IsRead); }
170 bool isWrite() const { return testCmdAttrib(IsWrite); }
171 bool isUpgrade() const { return testCmdAttrib(IsUpgrade); }
172 bool isRequest() const { return testCmdAttrib(IsRequest); }
173 bool isResponse() const { return testCmdAttrib(IsResponse); }
174 bool needsExclusive() const { return testCmdAttrib(NeedsExclusive); }
175 bool needsResponse() const { return testCmdAttrib(NeedsResponse); }
176 bool isInvalidate() const { return testCmdAttrib(IsInvalidate); }
177 bool hasData() const { return testCmdAttrib(HasData); }
178 bool isReadWrite() const { return isRead() && isWrite(); }
179 bool isLLSC() const { return testCmdAttrib(IsLlsc); }
180 bool isError() const { return testCmdAttrib(IsError); }
181 bool isPrint() const { return testCmdAttrib(IsPrint); }
182 bool isFlush() const { return testCmdAttrib(IsFlush); }
185 responseCommand() const
187 return commandInfo[cmd].response;
190 /// Return the string to a cmd given by idx.
191 const std::string &toString() const { return commandInfo[cmd].str; }
192 int toInt() const { return (int)cmd; }
194 MemCmd(Command _cmd) : cmd(_cmd) { }
195 MemCmd(int _cmd) : cmd((Command)_cmd) { }
196 MemCmd() : cmd(InvalidCmd) { }
198 bool operator==(MemCmd c2) const { return (cmd == c2.cmd); }
199 bool operator!=(MemCmd c2) const { return (cmd != c2.cmd); }
203 * A Packet is used to encapsulate a transfer between two objects in
204 * the memory system (e.g., the L1 and L2 cache). (In contrast, a
205 * single Request travels all the way from the requester to the
206 * ultimate destination and back, possibly being conveyed by several
207 * different Packets along the way.)
209 class Packet : public FastAlloc, public Printable
212 typedef uint32_t FlagsType;
213 typedef ::Flags<FlagsType> Flags;
214 typedef short NodeID;
217 static const FlagsType PUBLIC_FLAGS = 0x00000000;
218 static const FlagsType PRIVATE_FLAGS = 0x00007F0F;
219 static const FlagsType COPY_FLAGS = 0x0000000F;
221 static const FlagsType SHARED = 0x00000001;
222 // Special control flags
223 /// Special timing-mode atomic snoop for multi-level coherence.
224 static const FlagsType EXPRESS_SNOOP = 0x00000002;
225 /// Does supplier have exclusive copy?
226 /// Useful for multi-level coherence.
227 static const FlagsType SUPPLY_EXCLUSIVE = 0x00000004;
228 // Snoop response flags
229 static const FlagsType MEM_INHIBIT = 0x00000008;
230 /// Are the 'addr' and 'size' fields valid?
231 static const FlagsType VALID_ADDR = 0x00000100;
232 static const FlagsType VALID_SIZE = 0x00000200;
233 /// Is the 'src' field valid?
234 static const FlagsType VALID_SRC = 0x00000400;
235 static const FlagsType VALID_DST = 0x00000800;
236 /// Is the data pointer set to a value that shouldn't be freed
237 /// when the packet is destroyed?
238 static const FlagsType STATIC_DATA = 0x00001000;
239 /// The data pointer points to a value that should be freed when
240 /// the packet is destroyed.
241 static const FlagsType DYNAMIC_DATA = 0x00002000;
242 /// the data pointer points to an array (thus delete []) needs to
243 /// be called on it rather than simply delete.
244 static const FlagsType ARRAY_DATA = 0x00004000;
245 /// suppress the error if this packet encounters a functional
247 static const FlagsType SUPPRESS_FUNC_ERROR = 0x00008000;
252 typedef MemCmd::Command Command;
254 /// The command field of the packet.
257 /// A pointer to the original request.
262 * A pointer to the data being transfered. It can be differnt
263 * sizes at each level of the heirarchy so it belongs in the
264 * packet, not request. This may or may not be populated when a
265 * responder recieves the packet. If not populated it memory should
270 /// The address of the request. This address could be virtual or
271 /// physical, depending on the system configuration.
274 /// The size of the request or transfer.
278 * Device address (e.g., bus ID) of the source of the
279 * transaction. The source is not responsible for setting this
280 * field; it is set implicitly by the interconnect when the packet
286 * Device address (e.g., bus ID) of the destination of the
287 * transaction. The special value Broadcast indicates that the
288 * packet should be routed based on its address. This field is
289 * initialized in the constructor and is thus always valid (unlike
290 * addr, size, and src).
295 * The original value of the command field. Only valid when the
296 * current command field is an error condition; in that case, the
297 * previous contents of the command field are copied here. This
298 * field is *not* set on non-error responses.
303 * These values specify the range of bytes found that satisfy a
306 uint16_t bytesValidStart;
307 uint16_t bytesValidEnd;
310 /// Used to calculate latencies for each packet.
313 /// The time at which the packet will be fully transmitted
316 /// The time at which the first chunk of the packet will be transmitted
319 /// The special destination address indicating that the packet
320 /// should be routed based on its address.
321 static const NodeID Broadcast = -1;
324 * A virtual base opaque structure used to hold state associated
325 * with the packet but specific to the sending device (e.g., an
326 * MSHR). A pointer to this state is returned in the packet's
327 * response so that the sender can quickly look up the state
328 * needed to process it. A specific subclass would be derived
329 * from this to carry state specific to a particular sending
334 virtual ~SenderState() {}
338 * Object used to maintain state of a PrintReq. The senderState
339 * field of a PrintReq should always be of this type.
341 class PrintReqState : public SenderState, public FastAlloc
345 * An entry in the label stack.
347 struct LabelStackEntry
349 const std::string label;
352 LabelStackEntry(const std::string &_label, std::string *_prefix);
355 typedef std::list<LabelStackEntry> LabelStack;
356 LabelStack labelStack;
358 std::string *curPrefixPtr;
364 PrintReqState(std::ostream &os, int verbosity = 0);
368 * Returns the current line prefix.
370 const std::string &curPrefix() { return *curPrefixPtr; }
373 * Push a label onto the label stack, and prepend the given
374 * prefix string onto the current prefix. Labels will only be
375 * printed if an object within the label's scope is printed.
377 void pushLabel(const std::string &lbl,
378 const std::string &prefix = " ");
381 * Pop a label off the label stack.
386 * Print all of the pending unprinted labels on the
387 * stack. Called by printObj(), so normally not called by
388 * users unless bypassing printObj().
393 * Print a Printable object to os, because it matched the
394 * address on a PrintReq.
396 void printObj(Printable *obj);
400 * This packet's sender state. Devices should use dynamic_cast<>
401 * to cast to the state appropriate to the sender. The intent of
402 * this variable is to allow a device to attach extra information
403 * to a request. A response packet must return the sender state
404 * that was attached to the original request (even if a new packet
407 SenderState *senderState;
409 /// Return the string name of the cmd field (for debugging and
411 const std::string &cmdString() const { return cmd.toString(); }
413 /// Return the index of this command.
414 inline int cmdToIndex() const { return cmd.toInt(); }
416 bool isRead() const { return cmd.isRead(); }
417 bool isWrite() const { return cmd.isWrite(); }
418 bool isUpgrade() const { return cmd.isUpgrade(); }
419 bool isRequest() const { return cmd.isRequest(); }
420 bool isResponse() const { return cmd.isResponse(); }
421 bool needsExclusive() const { return cmd.needsExclusive(); }
422 bool needsResponse() const { return cmd.needsResponse(); }
423 bool isInvalidate() const { return cmd.isInvalidate(); }
424 bool hasData() const { return cmd.hasData(); }
425 bool isReadWrite() const { return cmd.isReadWrite(); }
426 bool isLLSC() const { return cmd.isLLSC(); }
427 bool isError() const { return cmd.isError(); }
428 bool isPrint() const { return cmd.isPrint(); }
429 bool isFlush() const { return cmd.isFlush(); }
432 void assertMemInhibit() { flags.set(MEM_INHIBIT); }
433 bool memInhibitAsserted() { return flags.isSet(MEM_INHIBIT); }
434 void assertShared() { flags.set(SHARED); }
435 bool sharedAsserted() { return flags.isSet(SHARED); }
437 // Special control flags
438 void setExpressSnoop() { flags.set(EXPRESS_SNOOP); }
439 bool isExpressSnoop() { return flags.isSet(EXPRESS_SNOOP); }
440 void setSupplyExclusive() { flags.set(SUPPLY_EXCLUSIVE); }
441 void clearSupplyExclusive() { flags.clear(SUPPLY_EXCLUSIVE); }
442 bool isSupplyExclusive() { return flags.isSet(SUPPLY_EXCLUSIVE); }
443 void setSuppressFuncError() { flags.set(SUPPRESS_FUNC_ERROR); }
444 bool suppressFuncError() { return flags.isSet(SUPPRESS_FUNC_ERROR); }
446 // Network error conditions... encapsulate them as methods since
447 // their encoding keeps changing (from result field to command
452 assert(isResponse());
453 cmd = MemCmd::NetworkNackError;
459 assert(isResponse());
460 cmd = MemCmd::BadAddressError;
463 bool wasNacked() const { return cmd == MemCmd::NetworkNackError; }
464 bool hadBadAddress() const { return cmd == MemCmd::BadAddressError; }
465 void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
467 bool isSrcValid() { return flags.isSet(VALID_SRC); }
468 /// Accessor function to get the source index of the packet.
469 NodeID getSrc() const { assert(flags.isSet(VALID_SRC)); return src; }
470 /// Accessor function to set the source index of the packet.
471 void setSrc(NodeID _src) { src = _src; flags.set(VALID_SRC); }
472 /// Reset source field, e.g. to retransmit packet on different bus.
473 void clearSrc() { flags.clear(VALID_SRC); }
475 bool isDestValid() { return flags.isSet(VALID_DST); }
476 /// Accessor function for the destination index of the packet.
477 NodeID getDest() const { assert(flags.isSet(VALID_DST)); return dest; }
478 /// Accessor function to set the destination index of the packet.
479 void setDest(NodeID _dest) { dest = _dest; flags.set(VALID_DST); }
481 Addr getAddr() const { assert(flags.isSet(VALID_ADDR)); return addr; }
482 unsigned getSize() const { assert(flags.isSet(VALID_SIZE)); return size; }
483 Addr getOffset(int blkSize) const { return getAddr() & (Addr)(blkSize - 1); }
486 * It has been determined that the SC packet should successfully update
487 * memory. Therefore, convert this SC packet to a normal write.
494 cmd = MemCmd::WriteReq;
498 * When ruby is in use, Ruby will monitor the cache line and thus M5
499 * phys memory should treat LL ops as normal reads.
506 cmd = MemCmd::ReadReq;
510 * Constructor. Note that a Request object must be constructed
511 * first, but the Requests's physical address and size fields need
512 * not be valid. The command and destination addresses must be
515 Packet(Request *_req, MemCmd _cmd, NodeID _dest)
516 : flags(VALID_DST), cmd(_cmd), req(_req), data(NULL),
517 dest(_dest), bytesValidStart(0), bytesValidEnd(0),
518 time(curTick()), senderState(NULL)
520 if (req->hasPaddr()) {
521 addr = req->getPaddr();
522 flags.set(VALID_ADDR);
524 if (req->hasSize()) {
525 size = req->getSize();
526 flags.set(VALID_SIZE);
531 * Alternate constructor if you are trying to create a packet with
532 * a request that is for a whole block, not the address from the
533 * req. this allows for overriding the size/addr of the req.
535 Packet(Request *_req, MemCmd _cmd, NodeID _dest, int _blkSize)
536 : flags(VALID_DST), cmd(_cmd), req(_req), data(NULL),
537 dest(_dest), bytesValidStart(0), bytesValidEnd(0),
538 time(curTick()), senderState(NULL)
540 if (req->hasPaddr()) {
541 addr = req->getPaddr() & ~(_blkSize - 1);
542 flags.set(VALID_ADDR);
545 flags.set(VALID_SIZE);
549 * Alternate constructor for copying a packet. Copy all fields
550 * *except* if the original packet's data was dynamic, don't copy
551 * that, as we can't guarantee that the new packet's lifetime is
552 * less than that of the original packet. In this case the new
553 * packet should allocate its own data.
555 Packet(Packet *pkt, bool clearFlags = false)
556 : cmd(pkt->cmd), req(pkt->req),
557 data(pkt->flags.isSet(STATIC_DATA) ? pkt->data : NULL),
558 addr(pkt->addr), size(pkt->size), src(pkt->src), dest(pkt->dest),
559 bytesValidStart(pkt->bytesValidStart), bytesValidEnd(pkt->bytesValidEnd),
560 time(curTick()), senderState(pkt->senderState)
563 flags.set(pkt->flags & COPY_FLAGS);
565 flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE|VALID_SRC|VALID_DST));
566 flags.set(pkt->flags & STATIC_DATA);
571 * clean up packet variables
575 // If this is a request packet for which there's no response,
576 // delete the request object here, since the requester will
577 // never get the chance.
578 if (req && isRequest() && !needsResponse())
584 * Reinitialize packet address and size from the associated
585 * Request object, and reset other fields that may have been
586 * modified by a previous transaction. Typically called when a
587 * statically allocated Request/Packet pair is reused for multiple
593 assert(req->hasPaddr());
595 addr = req->getPaddr();
596 size = req->getSize();
599 flags.set(VALID_ADDR|VALID_SIZE);
604 * Take a request packet and modify it in place to be suitable for
605 * returning as a response to that request. The source and
606 * destination fields are *not* modified, as is appropriate for
612 assert(needsResponse());
615 cmd = cmd.responseCommand();
617 // responses are never express, even if the snoop that
618 // triggered them was
619 flags.clear(EXPRESS_SNOOP);
622 flags.set(VALID_DST, flags.isSet(VALID_SRC));
623 flags.clear(VALID_SRC);
639 setFunctionalResponseStatus(bool success)
643 cmd = MemCmd::FunctionalWriteError;
645 cmd = MemCmd::FunctionalReadError;
651 * Take a request packet that has been returned as NACKED and
652 * modify it so that it can be sent out again. Only packets that
653 * need a response can be NACKED, so verify that that is true.
660 assert(needsResponse());
665 setSize(unsigned size)
667 assert(!flags.isSet(VALID_SIZE));
670 flags.set(VALID_SIZE);
675 * Set the data pointer to the following value that should not be
678 template <typename T>
682 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
683 data = (PacketDataPtr)p;
684 flags.set(STATIC_DATA);
688 * Set the data pointer to a value that should have delete []
691 template <typename T>
693 dataDynamicArray(T *p)
695 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
696 data = (PacketDataPtr)p;
697 flags.set(DYNAMIC_DATA|ARRAY_DATA);
701 * set the data pointer to a value that should have delete called
704 template <typename T>
708 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
709 data = (PacketDataPtr)p;
710 flags.set(DYNAMIC_DATA);
714 * get a pointer to the data ptr.
716 template <typename T>
718 getPtr(bool null_ok = false)
720 assert(null_ok || flags.isSet(STATIC_DATA|DYNAMIC_DATA));
725 * return the value of what is pointed to in the packet.
727 template <typename T>
731 * set the value in the data pointer to v.
733 template <typename T>
737 * Copy data into the packet from the provided pointer.
742 if (p != getPtr<uint8_t>())
743 std::memcpy(getPtr<uint8_t>(), p, getSize());
747 * Copy data into the packet from the provided block pointer,
748 * which is aligned to the given block size.
751 setDataFromBlock(uint8_t *blk_data, int blkSize)
753 setData(blk_data + getOffset(blkSize));
757 * Copy data from the packet to the provided block pointer, which
758 * is aligned to the given block size.
761 writeData(uint8_t *p)
763 std::memcpy(p, getPtr<uint8_t>(), getSize());
767 * Copy data from the packet to the memory at the provided pointer.
770 writeDataToBlock(uint8_t *blk_data, int blkSize)
772 writeData(blk_data + getOffset(blkSize));
776 * delete the data pointed to in the data pointer. Ok to call to
777 * matter how data was allocted.
782 if (flags.isSet(ARRAY_DATA))
784 else if (flags.isSet(DYNAMIC_DATA))
787 flags.clear(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA);
791 /** If there isn't data in the packet, allocate some. */
796 assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
800 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
801 flags.set(DYNAMIC_DATA|ARRAY_DATA);
802 data = new uint8_t[getSize()];
806 * Check a functional request against a memory value represented
807 * by a base/size pair and an associated data array. If the
808 * functional request is a read, it may be satisfied by the memory
809 * value. If the functional request is a write, it may update the
812 bool checkFunctional(Printable *obj, Addr base, int size, uint8_t *data);
815 * Check a functional request against a memory value stored in
816 * another packet (i.e. an in-transit request or response).
819 checkFunctional(PacketPtr other)
821 uint8_t *data = other->hasData() ? other->getPtr<uint8_t>() : NULL;
822 return checkFunctional(other, other->getAddr(), other->getSize(),
827 * Push label for PrintReq (safe to call unconditionally).
830 pushLabel(const std::string &lbl)
833 safe_cast<PrintReqState*>(senderState)->pushLabel(lbl);
837 * Pop label for PrintReq (safe to call unconditionally).
843 safe_cast<PrintReqState*>(senderState)->popLabel();
846 void print(std::ostream &o, int verbosity = 0,
847 const std::string &prefix = "") const;
850 #endif //__MEM_PACKET_HH