2 * Copyright (c) 2006 The Regents of The University of Michigan
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;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Ron Dreslinski
35 * Declaration of the Packet class.
38 #ifndef __MEM_PACKET_HH__
39 #define __MEM_PACKET_HH__
45 #include "base/compiler.hh"
46 #include "base/fast_alloc.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"
55 typedef Packet *PacketPtr;
56 typedef uint8_t* PacketDataPtr;
57 typedef std::list<PacketPtr> PacketList;
63 /** List of all commands associated with a packet. */
69 ReadRespWithInvalidate,
90 // @TODO these should be classified as responses rather than
91 // requests; coding them as requests initially for backwards
93 NetworkNackError, // nacked at network layer (not by protocol)
94 InvalidDestError, // packet dest field invalid
95 BadAddressError, // memory address invalid
96 // Fake simulator-only commands
97 PrintReq, // Print state matching address
102 /** List of command attributes. */
105 IsRead, //!< Data flows from responder to requester
106 IsWrite, //!< Data flows from requester to responder
107 IsPrefetch, //!< Not a demand access
109 NeedsExclusive, //!< Requires exclusive copy to complete in-cache
110 IsRequest, //!< Issued by requester
111 IsResponse, //!< Issue by responder
112 NeedsResponse, //!< Requester needs response from target
115 IsLocked, //!< Alpha/MIPS LL or SC access
116 HasData, //!< There is an associated payload
117 IsError, //!< Error response
118 IsPrint, //!< Print state matching address (for debugging)
119 NUM_COMMAND_ATTRIBUTES
122 /** Structure that defines attributes and other data associated
125 /** Set of attribute flags. */
126 const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
127 /** Corresponding response for requests; InvalidCmd if no
128 * response is applicable. */
129 const Command response;
130 /** String representation (for printing) */
131 const std::string str;
134 /** Array to map Command enum to associated info. */
135 static const CommandInfo commandInfo[];
141 bool testCmdAttrib(MemCmd::Attribute attrib) const {
142 return commandInfo[cmd].attributes[attrib] != 0;
147 bool isRead() const { return testCmdAttrib(IsRead); }
148 bool isWrite() const { return testCmdAttrib(IsWrite); }
149 bool isRequest() const { return testCmdAttrib(IsRequest); }
150 bool isResponse() const { return testCmdAttrib(IsResponse); }
151 bool needsExclusive() const { return testCmdAttrib(NeedsExclusive); }
152 bool needsResponse() const { return testCmdAttrib(NeedsResponse); }
153 bool isInvalidate() const { return testCmdAttrib(IsInvalidate); }
154 bool hasData() const { return testCmdAttrib(HasData); }
155 bool isReadWrite() const { return isRead() && isWrite(); }
156 bool isLocked() const { return testCmdAttrib(IsLocked); }
157 bool isError() const { return testCmdAttrib(IsError); }
158 bool isPrint() const { return testCmdAttrib(IsPrint); }
160 const Command responseCommand() const {
161 return commandInfo[cmd].response;
164 /** Return the string to a cmd given by idx. */
165 const std::string &toString() const {
166 return commandInfo[cmd].str;
169 int toInt() const { return (int)cmd; }
183 bool operator==(MemCmd c2) { return (cmd == c2.cmd); }
184 bool operator!=(MemCmd c2) { return (cmd != c2.cmd); }
190 * A Packet is used to encapsulate a transfer between two objects in
191 * the memory system (e.g., the L1 and L2 cache). (In contrast, a
192 * single Request travels all the way from the requester to the
193 * ultimate destination and back, possibly being conveyed by several
194 * different Packets along the way.)
196 class Packet : public FastAlloc, public Printable
200 typedef MemCmd::Command Command;
202 /** The command field of the packet. */
205 /** A pointer to the original request. */
209 /** A pointer to the data being transfered. It can be differnt
210 * sizes at each level of the heirarchy so it belongs in the
211 * packet, not request. This may or may not be populated when a
212 * responder recieves the packet. If not populated it memory
213 * should be allocated.
217 /** Is the data pointer set to a value that shouldn't be freed
218 * when the packet is destroyed? */
220 /** The data pointer points to a value that should be freed when
221 * the packet is destroyed. */
223 /** the data pointer points to an array (thus delete [] ) needs to
224 * be called on it rather than simply delete.*/
227 /** The address of the request. This address could be virtual or
228 * physical, depending on the system configuration. */
231 /** The size of the request or transfer. */
234 /** Device address (e.g., bus ID) of the source of the
235 * transaction. The source is not responsible for setting this
236 * field; it is set implicitly by the interconnect when the
237 * packet is first sent. */
240 /** Device address (e.g., bus ID) of the destination of the
241 * transaction. The special value Broadcast indicates that the
242 * packet should be routed based on its address. This field is
243 * initialized in the constructor and is thus always valid
244 * (unlike * addr, size, and src). */
247 /** The original value of the command field. Only valid when the
248 * current command field is an error condition; in that case, the
249 * previous contents of the command field are copied here. This
250 * field is *not* set on non-error responses.
254 /** Are the 'addr' and 'size' fields valid? */
256 /** Is the 'src' field valid? */
261 // Snoop response flags
264 // Special control flags
265 /// Special timing-mode atomic snoop for multi-level coherence.
267 /// Does supplier have exclusive copy?
268 /// Useful for multi-level coherence.
274 std::bitset<NUM_PACKET_FLAGS> flags;
278 /** Used to calculate latencies for each packet.*/
281 /** The time at which the packet will be fully transmitted */
284 /** The time at which the first chunk of the packet will be transmitted */
287 /** The special destination address indicating that the packet
288 * should be routed based on its address. */
289 static const short Broadcast = -1;
291 /** A virtual base opaque structure used to hold state associated
292 * with the packet but specific to the sending device (e.g., an
293 * MSHR). A pointer to this state is returned in the packet's
294 * response so that the sender can quickly look up the state
295 * needed to process it. A specific subclass would be derived
296 * from this to carry state specific to a particular sending
298 class SenderState : public FastAlloc {
300 virtual ~SenderState() {}
304 * Object used to maintain state of a PrintReq. The senderState
305 * field of a PrintReq should always be of this type.
307 class PrintReqState : public SenderState {
308 /** An entry in the label stack. */
309 class LabelStackEntry {
311 const std::string label;
314 LabelStackEntry(const std::string &_label,
315 std::string *_prefix);
318 typedef std::list<LabelStackEntry> LabelStack;
319 LabelStack labelStack;
321 std::string *curPrefixPtr;
327 PrintReqState(std::ostream &os, int verbosity = 0);
330 /** Returns the current line prefix. */
331 const std::string &curPrefix() { return *curPrefixPtr; }
333 /** Push a label onto the label stack, and prepend the given
334 * prefix string onto the current prefix. Labels will only be
335 * printed if an object within the label's scope is
337 void pushLabel(const std::string &lbl,
338 const std::string &prefix = " ");
339 /** Pop a label off the label stack. */
341 /** Print all of the pending unprinted labels on the
342 * stack. Called by printObj(), so normally not called by
343 * users unless bypassing printObj(). */
345 /** Print a Printable object to os, because it matched the
346 * address on a PrintReq. */
347 void printObj(Printable *obj);
350 /** This packet's sender state. Devices should use dynamic_cast<>
351 * to cast to the state appropriate to the sender. */
352 SenderState *senderState;
354 /** Return the string name of the cmd field (for debugging and
356 const std::string &cmdString() const { return cmd.toString(); }
358 /** Return the index of this command. */
359 inline int cmdToIndex() const { return cmd.toInt(); }
361 bool isRead() const { return cmd.isRead(); }
362 bool isWrite() const { return cmd.isWrite(); }
363 bool isRequest() const { return cmd.isRequest(); }
364 bool isResponse() const { return cmd.isResponse(); }
365 bool needsExclusive() const { return cmd.needsExclusive(); }
366 bool needsResponse() const { return cmd.needsResponse(); }
367 bool isInvalidate() const { return cmd.isInvalidate(); }
368 bool hasData() const { return cmd.hasData(); }
369 bool isReadWrite() const { return cmd.isReadWrite(); }
370 bool isLocked() const { return cmd.isLocked(); }
371 bool isError() const { return cmd.isError(); }
372 bool isPrint() const { return cmd.isPrint(); }
375 void assertMemInhibit() { flags[MemInhibit] = true; }
376 bool memInhibitAsserted() { return flags[MemInhibit]; }
377 void assertShared() { flags[Shared] = true; }
378 bool sharedAsserted() { return flags[Shared]; }
380 // Special control flags
381 void setExpressSnoop() { flags[ExpressSnoop] = true; }
382 bool isExpressSnoop() { return flags[ExpressSnoop]; }
383 void setSupplyExclusive() { flags[SupplyExclusive] = true; }
384 bool isSupplyExclusive() { return flags[SupplyExclusive]; }
386 // Network error conditions... encapsulate them as methods since
387 // their encoding keeps changing (from result field to command
389 void setNacked() { assert(isResponse()); cmd = MemCmd::NetworkNackError; }
390 void setBadAddress() { assert(isResponse()); cmd = MemCmd::BadAddressError; }
391 bool wasNacked() { return cmd == MemCmd::NetworkNackError; }
392 bool hadBadAddress() { return cmd == MemCmd::BadAddressError; }
393 void copyError(Packet *pkt) { assert(pkt->isError()); cmd = pkt->cmd; }
395 bool nic_pkt() { panic("Unimplemented"); M5_DUMMY_RETURN }
397 /** Accessor function that returns the source index of the packet. */
398 short getSrc() const { assert(srcValid); return src; }
399 void setSrc(short _src) { src = _src; srcValid = true; }
400 /** Reset source field, e.g. to retransmit packet on different bus. */
401 void clearSrc() { srcValid = false; }
403 /** Accessor function that returns the destination index of
405 short getDest() const { assert(destValid); return dest; }
406 void setDest(short _dest) { dest = _dest; destValid = true; }
408 Addr getAddr() const { assert(addrSizeValid); return addr; }
409 int getSize() const { assert(addrSizeValid); return size; }
410 Addr getOffset(int blkSize) const { return addr & (Addr)(blkSize - 1); }
412 /** Constructor. Note that a Request object must be constructed
413 * first, but the Requests's physical address and size fields
414 * need not be valid. The command and destination addresses
415 * must be supplied. */
416 Packet(Request *_req, MemCmd _cmd, short _dest)
417 : cmd(_cmd), req(_req),
418 data(NULL), staticData(false), dynamicData(false), arrayData(false),
419 addr(_req->paddr), size(_req->size), dest(_dest),
420 addrSizeValid(_req->validPaddr), srcValid(false), destValid(true),
421 flags(0), time(curTick), senderState(NULL)
425 /** Alternate constructor if you are trying to create a packet with
426 * a request that is for a whole block, not the address from the req.
427 * this allows for overriding the size/addr of the req.*/
428 Packet(Request *_req, MemCmd _cmd, short _dest, int _blkSize)
429 : cmd(_cmd), req(_req),
430 data(NULL), staticData(false), dynamicData(false), arrayData(false),
431 addr(_req->paddr & ~(_blkSize - 1)), size(_blkSize), dest(_dest),
432 addrSizeValid(_req->validPaddr), srcValid(false), destValid(true),
433 flags(0), time(curTick), senderState(NULL)
437 /** Alternate constructor for copying a packet. Copy all fields
438 * *except* if the original packet's data was dynamic, don't copy
439 * that, as we can't guarantee that the new packet's lifetime is
440 * less than that of the original packet. In this case the new
441 * packet should allocate its own data. */
442 Packet(Packet *origPkt, bool clearFlags = false)
443 : cmd(origPkt->cmd), req(origPkt->req),
444 data(origPkt->staticData ? origPkt->data : NULL),
445 staticData(origPkt->staticData),
446 dynamicData(false), arrayData(false),
447 addr(origPkt->addr), size(origPkt->size),
448 src(origPkt->src), dest(origPkt->dest),
449 addrSizeValid(origPkt->addrSizeValid),
450 srcValid(origPkt->srcValid), destValid(origPkt->destValid),
451 flags(clearFlags ? 0 : origPkt->flags),
452 time(curTick), senderState(origPkt->senderState)
458 { if (staticData || dynamicData) deleteData(); }
460 /** Reinitialize packet address and size from the associated
461 * Request object, and reset other fields that may have been
462 * modified by a previous transaction. Typically called when a
463 * statically allocated Request/Packet pair is reused for
464 * multiple transactions. */
465 void reinitFromRequest() {
466 assert(req->validPaddr);
471 addrSizeValid = true;
480 * Take a request packet and modify it in place to be suitable for
481 * returning as a response to that request. The source and
482 * destination fields are *not* modified, as is appropriate for
487 assert(needsResponse());
490 cmd = cmd.responseCommand();
492 destValid = srcValid;
496 void makeAtomicResponse()
501 void makeTimingResponse()
507 * Take a request packet that has been returned as NACKED and
508 * modify it so that it can be sent out again. Only packets that
509 * need a response can be NACKED, so verify that that is true.
516 assert(needsResponse());
522 * Set the data pointer to the following value that should not be
525 template <typename T>
531 data = (PacketDataPtr)p;
536 * Set the data pointer to a value that should have delete []
539 template <typename T>
541 dataDynamicArray(T *p)
543 assert(!staticData && !dynamicData);
544 data = (PacketDataPtr)p;
550 * set the data pointer to a value that should have delete called
553 template <typename T>
557 assert(!staticData && !dynamicData);
558 data = (PacketDataPtr)p;
563 /** get a pointer to the data ptr. */
564 template <typename T>
568 assert(staticData || dynamicData);
572 /** return the value of what is pointed to in the packet. */
573 template <typename T>
576 /** set the value in the data pointer to v. */
577 template <typename T>
581 * Copy data into the packet from the provided pointer.
583 void setData(uint8_t *p)
585 std::memcpy(getPtr<uint8_t>(), p, getSize());
589 * Copy data into the packet from the provided block pointer,
590 * which is aligned to the given block size.
592 void setDataFromBlock(uint8_t *blk_data, int blkSize)
594 setData(blk_data + getOffset(blkSize));
598 * Copy data from the packet to the provided block pointer, which
599 * is aligned to the given block size.
601 void writeData(uint8_t *p)
603 std::memcpy(p, getPtr<uint8_t>(), getSize());
607 * Copy data from the packet to the memory at the provided pointer.
609 void writeDataToBlock(uint8_t *blk_data, int blkSize)
611 writeData(blk_data + getOffset(blkSize));
615 * delete the data pointed to in the data pointer. Ok to call to
616 * matter how data was allocted.
620 /** If there isn't data in the packet, allocate some. */
624 * Check a functional request against a memory value represented
625 * by a base/size pair and an associated data array. If the
626 * functional request is a read, it may be satisfied by the memory
627 * value. If the functional request is a write, it may update the
630 bool checkFunctional(Printable *obj, Addr base, int size, uint8_t *data);
633 * Check a functional request against a memory value stored in
634 * another packet (i.e. an in-transit request or response).
636 bool checkFunctional(PacketPtr otherPkt) {
637 return checkFunctional(otherPkt,
638 otherPkt->getAddr(), otherPkt->getSize(),
639 otherPkt->hasData() ?
640 otherPkt->getPtr<uint8_t>() : NULL);
644 * Push label for PrintReq (safe to call unconditionally).
646 void pushLabel(const std::string &lbl) {
648 dynamic_cast<PrintReqState*>(senderState)->pushLabel(lbl);
653 * Pop label for PrintReq (safe to call unconditionally).
657 dynamic_cast<PrintReqState*>(senderState)->popLabel();
661 void print(std::ostream &o, int verbosity = 0,
662 const std::string &prefix = "") const;
665 #endif //__MEM_PACKET_HH