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
99 // @TODO these should be classified as responses rather than
100 // requests; coding them as requests initially for backwards
102 NetworkNackError, // nacked at network layer (not by protocol)
103 InvalidDestError, // packet dest field invalid
104 BadAddressError, // memory address invalid
105 // Fake simulator-only commands
106 PrintReq, // Print state matching address
112 * List of command attributes.
116 IsRead, //!< Data flows from responder to requester
117 IsWrite, //!< Data flows from requester to responder
119 IsPrefetch, //!< Not a demand access
121 NeedsExclusive, //!< Requires exclusive copy to complete in-cache
122 IsRequest, //!< Issued by requester
123 IsResponse, //!< Issue by responder
124 NeedsResponse, //!< Requester needs response from target
127 IsLlsc, //!< Alpha/MIPS LL or SC access
128 HasData, //!< There is an associated payload
129 IsError, //!< Error response
130 IsPrint, //!< Print state matching address (for debugging)
131 NUM_COMMAND_ATTRIBUTES
135 * Structure that defines attributes and other data associated
140 /// Set of attribute flags.
141 const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
142 /// Corresponding response for requests; InvalidCmd if no
143 /// response is applicable.
144 const Command response;
145 /// String representation (for printing)
146 const std::string str;
149 /// Array to map Command enum to associated info.
150 static const CommandInfo commandInfo[];
157 testCmdAttrib(MemCmd::Attribute attrib) const
159 return commandInfo[cmd].attributes[attrib] != 0;
164 bool isRead() const { return testCmdAttrib(IsRead); }
165 bool isWrite() const { return testCmdAttrib(IsWrite); }
166 bool isUpgrade() const { return testCmdAttrib(IsUpgrade); }
167 bool isRequest() const { return testCmdAttrib(IsRequest); }
168 bool isResponse() const { return testCmdAttrib(IsResponse); }
169 bool needsExclusive() const { return testCmdAttrib(NeedsExclusive); }
170 bool needsResponse() const { return testCmdAttrib(NeedsResponse); }
171 bool isInvalidate() const { return testCmdAttrib(IsInvalidate); }
172 bool hasData() const { return testCmdAttrib(HasData); }
173 bool isReadWrite() const { return isRead() && isWrite(); }
174 bool isLLSC() const { return testCmdAttrib(IsLlsc); }
175 bool isError() const { return testCmdAttrib(IsError); }
176 bool isPrint() const { return testCmdAttrib(IsPrint); }
179 responseCommand() const
181 return commandInfo[cmd].response;
184 /// Return the string to a cmd given by idx.
185 const std::string &toString() const { return commandInfo[cmd].str; }
186 int toInt() const { return (int)cmd; }
188 MemCmd(Command _cmd) : cmd(_cmd) { }
189 MemCmd(int _cmd) : cmd((Command)_cmd) { }
190 MemCmd() : cmd(InvalidCmd) { }
192 bool operator==(MemCmd c2) const { return (cmd == c2.cmd); }
193 bool operator!=(MemCmd c2) const { return (cmd != c2.cmd); }
197 * A Packet is used to encapsulate a transfer between two objects in
198 * the memory system (e.g., the L1 and L2 cache). (In contrast, a
199 * single Request travels all the way from the requester to the
200 * ultimate destination and back, possibly being conveyed by several
201 * different Packets along the way.)
203 class Packet : public FastAlloc, public Printable
206 typedef uint32_t FlagsType;
207 typedef ::Flags<FlagsType> Flags;
208 typedef short NodeID;
211 static const FlagsType PUBLIC_FLAGS = 0x00000000;
212 static const FlagsType PRIVATE_FLAGS = 0x00007F0F;
213 static const FlagsType COPY_FLAGS = 0x0000000F;
215 static const FlagsType SHARED = 0x00000001;
216 // Special control flags
217 /// Special timing-mode atomic snoop for multi-level coherence.
218 static const FlagsType EXPRESS_SNOOP = 0x00000002;
219 /// Does supplier have exclusive copy?
220 /// Useful for multi-level coherence.
221 static const FlagsType SUPPLY_EXCLUSIVE = 0x00000004;
222 // Snoop response flags
223 static const FlagsType MEM_INHIBIT = 0x00000008;
224 /// Are the 'addr' and 'size' fields valid?
225 static const FlagsType VALID_ADDR = 0x00000100;
226 static const FlagsType VALID_SIZE = 0x00000200;
227 /// Is the 'src' field valid?
228 static const FlagsType VALID_SRC = 0x00000400;
229 static const FlagsType VALID_DST = 0x00000800;
230 /// Is the data pointer set to a value that shouldn't be freed
231 /// when the packet is destroyed?
232 static const FlagsType STATIC_DATA = 0x00001000;
233 /// The data pointer points to a value that should be freed when
234 /// the packet is destroyed.
235 static const FlagsType DYNAMIC_DATA = 0x00002000;
236 /// the data pointer points to an array (thus delete []) needs to
237 /// be called on it rather than simply delete.
238 static const FlagsType ARRAY_DATA = 0x00004000;
243 typedef MemCmd::Command Command;
245 /// The command field of the packet.
248 /// A pointer to the original request.
253 * A pointer to the data being transfered. It can be differnt
254 * sizes at each level of the heirarchy so it belongs in the
255 * packet, not request. This may or may not be populated when a
256 * responder recieves the packet. If not populated it memory should
261 /// The address of the request. This address could be virtual or
262 /// physical, depending on the system configuration.
265 /// The size of the request or transfer.
269 * Device address (e.g., bus ID) of the source of the
270 * transaction. The source is not responsible for setting this
271 * field; it is set implicitly by the interconnect when the packet
277 * Device address (e.g., bus ID) of the destination of the
278 * transaction. The special value Broadcast indicates that the
279 * packet should be routed based on its address. This field is
280 * initialized in the constructor and is thus always valid (unlike
281 * addr, size, and src).
286 * The original value of the command field. Only valid when the
287 * current command field is an error condition; in that case, the
288 * previous contents of the command field are copied here. This
289 * field is *not* set on non-error responses.
294 /// Used to calculate latencies for each packet.
297 /// The time at which the packet will be fully transmitted
300 /// The time at which the first chunk of the packet will be transmitted
303 /// The special destination address indicating that the packet
304 /// should be routed based on its address.
305 static const NodeID Broadcast = -1;
308 * A virtual base opaque structure used to hold state associated
309 * with the packet but specific to the sending device (e.g., an
310 * MSHR). A pointer to this state is returned in the packet's
311 * response so that the sender can quickly look up the state
312 * needed to process it. A specific subclass would be derived
313 * from this to carry state specific to a particular sending
318 virtual ~SenderState() {}
322 * Object used to maintain state of a PrintReq. The senderState
323 * field of a PrintReq should always be of this type.
325 class PrintReqState : public SenderState, public FastAlloc
329 * An entry in the label stack.
331 struct LabelStackEntry
333 const std::string label;
336 LabelStackEntry(const std::string &_label, std::string *_prefix);
339 typedef std::list<LabelStackEntry> LabelStack;
340 LabelStack labelStack;
342 std::string *curPrefixPtr;
348 PrintReqState(std::ostream &os, int verbosity = 0);
352 * Returns the current line prefix.
354 const std::string &curPrefix() { return *curPrefixPtr; }
357 * Push a label onto the label stack, and prepend the given
358 * prefix string onto the current prefix. Labels will only be
359 * printed if an object within the label's scope is printed.
361 void pushLabel(const std::string &lbl,
362 const std::string &prefix = " ");
365 * Pop a label off the label stack.
370 * Print all of the pending unprinted labels on the
371 * stack. Called by printObj(), so normally not called by
372 * users unless bypassing printObj().
377 * Print a Printable object to os, because it matched the
378 * address on a PrintReq.
380 void printObj(Printable *obj);
384 * This packet's sender state. Devices should use dynamic_cast<>
385 * to cast to the state appropriate to the sender. The intent of
386 * this variable is to allow a device to attach extra information
387 * to a request. A response packet must return the sender state
388 * that was attached to the original request (even if a new packet
391 SenderState *senderState;
393 /// Return the string name of the cmd field (for debugging and
395 const std::string &cmdString() const { return cmd.toString(); }
397 /// Return the index of this command.
398 inline int cmdToIndex() const { return cmd.toInt(); }
400 bool isRead() const { return cmd.isRead(); }
401 bool isWrite() const { return cmd.isWrite(); }
402 bool isUpgrade() const { return cmd.isUpgrade(); }
403 bool isRequest() const { return cmd.isRequest(); }
404 bool isResponse() const { return cmd.isResponse(); }
405 bool needsExclusive() const { return cmd.needsExclusive(); }
406 bool needsResponse() const { return cmd.needsResponse(); }
407 bool isInvalidate() const { return cmd.isInvalidate(); }
408 bool hasData() const { return cmd.hasData(); }
409 bool isReadWrite() const { return cmd.isReadWrite(); }
410 bool isLLSC() const { return cmd.isLLSC(); }
411 bool isError() const { return cmd.isError(); }
412 bool isPrint() const { return cmd.isPrint(); }
415 void assertMemInhibit() { flags.set(MEM_INHIBIT); }
416 bool memInhibitAsserted() { return flags.isSet(MEM_INHIBIT); }
417 void assertShared() { flags.set(SHARED); }
418 bool sharedAsserted() { return flags.isSet(SHARED); }
420 // Special control flags
421 void setExpressSnoop() { flags.set(EXPRESS_SNOOP); }
422 bool isExpressSnoop() { return flags.isSet(EXPRESS_SNOOP); }
423 void setSupplyExclusive() { flags.set(SUPPLY_EXCLUSIVE); }
424 bool isSupplyExclusive() { return flags.isSet(SUPPLY_EXCLUSIVE); }
426 // Network error conditions... encapsulate them as methods since
427 // their encoding keeps changing (from result field to command
432 assert(isResponse());
433 cmd = MemCmd::NetworkNackError;
439 assert(isResponse());
440 cmd = MemCmd::BadAddressError;
443 bool wasNacked() const { return cmd == MemCmd::NetworkNackError; }
444 bool hadBadAddress() const { return cmd == MemCmd::BadAddressError; }
445 void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
447 bool isSrcValid() { return flags.isSet(VALID_SRC); }
448 /// Accessor function to get the source index of the packet.
449 NodeID getSrc() const { assert(flags.isSet(VALID_SRC)); return src; }
450 /// Accessor function to set the source index of the packet.
451 void setSrc(NodeID _src) { src = _src; flags.set(VALID_SRC); }
452 /// Reset source field, e.g. to retransmit packet on different bus.
453 void clearSrc() { flags.clear(VALID_SRC); }
455 bool isDestValid() { return flags.isSet(VALID_DST); }
456 /// Accessor function for the destination index of the packet.
457 NodeID getDest() const { assert(flags.isSet(VALID_DST)); return dest; }
458 /// Accessor function to set the destination index of the packet.
459 void setDest(NodeID _dest) { dest = _dest; flags.set(VALID_DST); }
461 Addr getAddr() const { assert(flags.isSet(VALID_ADDR)); return addr; }
462 unsigned getSize() const { assert(flags.isSet(VALID_SIZE)); return size; }
463 Addr getOffset(int blkSize) const { return getAddr() & (Addr)(blkSize - 1); }
466 * It has been determined that the SC packet should successfully update
467 * memory. Therefore, convert this SC packet to a normal write.
474 cmd = MemCmd::WriteReq;
478 * When ruby is in use, Ruby will monitor the cache line and thus M5
479 * phys memory should treat LL ops as normal reads.
486 cmd = MemCmd::ReadReq;
490 * Constructor. Note that a Request object must be constructed
491 * first, but the Requests's physical address and size fields need
492 * not be valid. The command and destination addresses must be
495 Packet(Request *_req, MemCmd _cmd, NodeID _dest)
496 : flags(VALID_DST), cmd(_cmd), req(_req), data(NULL),
497 dest(_dest), time(curTick), senderState(NULL)
499 if (req->hasPaddr()) {
500 addr = req->getPaddr();
501 flags.set(VALID_ADDR);
503 if (req->hasSize()) {
504 size = req->getSize();
505 flags.set(VALID_SIZE);
510 * Alternate constructor if you are trying to create a packet with
511 * a request that is for a whole block, not the address from the
512 * req. this allows for overriding the size/addr of the req.
514 Packet(Request *_req, MemCmd _cmd, NodeID _dest, int _blkSize)
515 : flags(VALID_DST), cmd(_cmd), req(_req), data(NULL),
516 dest(_dest), time(curTick), senderState(NULL)
518 if (req->hasPaddr()) {
519 addr = req->getPaddr() & ~(_blkSize - 1);
520 flags.set(VALID_ADDR);
523 flags.set(VALID_SIZE);
527 * Alternate constructor for copying a packet. Copy all fields
528 * *except* if the original packet's data was dynamic, don't copy
529 * that, as we can't guarantee that the new packet's lifetime is
530 * less than that of the original packet. In this case the new
531 * packet should allocate its own data.
533 Packet(Packet *pkt, bool clearFlags = false)
534 : cmd(pkt->cmd), req(pkt->req),
535 data(pkt->flags.isSet(STATIC_DATA) ? pkt->data : NULL),
536 addr(pkt->addr), size(pkt->size), src(pkt->src), dest(pkt->dest),
537 time(curTick), senderState(pkt->senderState)
540 flags.set(pkt->flags & COPY_FLAGS);
542 flags.set(pkt->flags & (VALID_ADDR|VALID_SIZE|VALID_SRC|VALID_DST));
543 flags.set(pkt->flags & STATIC_DATA);
547 * clean up packet variables
551 // If this is a request packet for which there's no response,
552 // delete the request object here, since the requester will
553 // never get the chance.
554 if (req && isRequest() && !needsResponse())
560 * Reinitialize packet address and size from the associated
561 * Request object, and reset other fields that may have been
562 * modified by a previous transaction. Typically called when a
563 * statically allocated Request/Packet pair is reused for multiple
569 assert(req->hasPaddr());
571 addr = req->getPaddr();
572 size = req->getSize();
575 flags.set(VALID_ADDR|VALID_SIZE);
580 * Take a request packet and modify it in place to be suitable for
581 * returning as a response to that request. The source and
582 * destination fields are *not* modified, as is appropriate for
588 assert(needsResponse());
591 cmd = cmd.responseCommand();
593 // responses are never express, even if the snoop that
594 // triggered them was
595 flags.clear(EXPRESS_SNOOP);
598 flags.set(VALID_DST, flags.isSet(VALID_SRC));
599 flags.clear(VALID_SRC);
615 * Take a request packet that has been returned as NACKED and
616 * modify it so that it can be sent out again. Only packets that
617 * need a response can be NACKED, so verify that that is true.
624 assert(needsResponse());
629 setSize(unsigned size)
631 assert(!flags.isSet(VALID_SIZE));
634 flags.set(VALID_SIZE);
639 * Set the data pointer to the following value that should not be
642 template <typename T>
646 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
647 data = (PacketDataPtr)p;
648 flags.set(STATIC_DATA);
652 * Set the data pointer to a value that should have delete []
655 template <typename T>
657 dataDynamicArray(T *p)
659 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
660 data = (PacketDataPtr)p;
661 flags.set(DYNAMIC_DATA|ARRAY_DATA);
665 * set the data pointer to a value that should have delete called
668 template <typename T>
672 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA));
673 data = (PacketDataPtr)p;
674 flags.set(DYNAMIC_DATA);
678 * get a pointer to the data ptr.
680 template <typename T>
684 assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
689 * return the value of what is pointed to in the packet.
691 template <typename T>
695 * set the value in the data pointer to v.
697 template <typename T>
701 * Copy data into the packet from the provided pointer.
706 std::memcpy(getPtr<uint8_t>(), p, getSize());
710 * Copy data into the packet from the provided block pointer,
711 * which is aligned to the given block size.
714 setDataFromBlock(uint8_t *blk_data, int blkSize)
716 setData(blk_data + getOffset(blkSize));
720 * Copy data from the packet to the provided block pointer, which
721 * is aligned to the given block size.
724 writeData(uint8_t *p)
726 std::memcpy(p, getPtr<uint8_t>(), getSize());
730 * Copy data from the packet to the memory at the provided pointer.
733 writeDataToBlock(uint8_t *blk_data, int blkSize)
735 writeData(blk_data + getOffset(blkSize));
739 * delete the data pointed to in the data pointer. Ok to call to
740 * matter how data was allocted.
745 if (flags.isSet(ARRAY_DATA))
747 else if (flags.isSet(DYNAMIC_DATA))
750 flags.clear(STATIC_DATA|DYNAMIC_DATA|ARRAY_DATA);
754 /** If there isn't data in the packet, allocate some. */
759 assert(flags.isSet(STATIC_DATA|DYNAMIC_DATA));
763 assert(flags.noneSet(STATIC_DATA|DYNAMIC_DATA));
764 flags.set(DYNAMIC_DATA|ARRAY_DATA);
765 data = new uint8_t[getSize()];
770 * Check a functional request against a memory value represented
771 * by a base/size pair and an associated data array. If the
772 * functional request is a read, it may be satisfied by the memory
773 * value. If the functional request is a write, it may update the
776 bool checkFunctional(Printable *obj, Addr base, int size, uint8_t *data);
779 * Check a functional request against a memory value stored in
780 * another packet (i.e. an in-transit request or response).
783 checkFunctional(PacketPtr other)
785 uint8_t *data = other->hasData() ? other->getPtr<uint8_t>() : NULL;
786 return checkFunctional(other, other->getAddr(), other->getSize(),
791 * Push label for PrintReq (safe to call unconditionally).
794 pushLabel(const std::string &lbl)
797 safe_cast<PrintReqState*>(senderState)->pushLabel(lbl);
801 * Pop label for PrintReq (safe to call unconditionally).
807 safe_cast<PrintReqState*>(senderState)->popLabel();
810 void print(std::ostream &o, int verbosity = 0,
811 const std::string &prefix = "") const;
814 #endif //__MEM_PACKET_HH