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 "mem/request.hh"
49 #include "sim/host.hh"
50 #include "sim/core.hh"
54 typedef Packet *PacketPtr;
55 typedef uint8_t* PacketDataPtr;
56 typedef std::list<PacketPtr> PacketList;
59 #define NACKED_LINE (1 << 0)
60 #define SATISFIED (1 << 1)
61 #define SHARED_LINE (1 << 2)
62 #define CACHE_LINE_FILL (1 << 3)
63 #define COMPRESSED (1 << 4)
64 #define NO_ALLOCATE (1 << 5)
65 #define SNOOP_COMMIT (1 << 6)
72 /** List of all commands associated with a packet. */
98 /** List of command attributes. */
113 NUM_COMMAND_ATTRIBUTES
116 /** Structure that defines attributes and other data associated
119 /** Set of attribute flags. */
120 const std::bitset<NUM_COMMAND_ATTRIBUTES> attributes;
121 /** Corresponding response for requests; InvalidCmd if no
122 * response is applicable. */
123 const Command response;
124 /** String representation (for printing) */
125 const std::string str;
128 /** Array to map Command enum to associated info. */
129 static const CommandInfo commandInfo[];
135 bool testCmdAttrib(MemCmd::Attribute attrib) const {
136 return commandInfo[cmd].attributes[attrib] != 0;
141 bool isRead() const { return testCmdAttrib(IsRead); }
142 bool isWrite() const { return testCmdAttrib(IsWrite); }
143 bool isRequest() const { return testCmdAttrib(IsRequest); }
144 bool isResponse() const { return testCmdAttrib(IsResponse); }
145 bool needsResponse() const { return testCmdAttrib(NeedsResponse); }
146 bool isInvalidate() const { return testCmdAttrib(IsInvalidate); }
147 bool hasData() const { return testCmdAttrib(HasData); }
148 bool isReadWrite() const { return testCmdAttrib(IsReadWrite); }
150 const Command responseCommand() const {
151 return commandInfo[cmd].response;
154 /** Return the string to a cmd given by idx. */
155 const std::string &toString() const {
156 return commandInfo[cmd].str;
159 int toInt() const { return (int)cmd; }
173 bool operator==(MemCmd c2) { return (cmd == c2.cmd); }
174 bool operator!=(MemCmd c2) { return (cmd != c2.cmd); }
180 * A Packet is used to encapsulate a transfer between two objects in
181 * the memory system (e.g., the L1 and L2 cache). (In contrast, a
182 * single Request travels all the way from the requester to the
183 * ultimate destination and back, possibly being conveyed by several
184 * different Packets along the way.)
186 class Packet : public FastAlloc
190 typedef MemCmd::Command Command;
192 /** Temporary FLAGS field until cache gets working, this should be in coherence/sender state. */
196 /** A pointer to the data being transfered. It can be differnt
197 * sizes at each level of the heirarchy so it belongs in the
198 * packet, not request. This may or may not be populated when a
199 * responder recieves the packet. If not populated it memory
200 * should be allocated.
204 /** Is the data pointer set to a value that shouldn't be freed
205 * when the packet is destroyed? */
207 /** The data pointer points to a value that should be freed when
208 * the packet is destroyed. */
210 /** the data pointer points to an array (thus delete [] ) needs to
211 * be called on it rather than simply delete.*/
214 /** The address of the request. This address could be virtual or
215 * physical, depending on the system configuration. */
218 /** The size of the request or transfer. */
221 /** Device address (e.g., bus ID) of the source of the
222 * transaction. The source is not responsible for setting this
223 * field; it is set implicitly by the interconnect when the
224 * packet is first sent. */
227 /** Device address (e.g., bus ID) of the destination of the
228 * transaction. The special value Broadcast indicates that the
229 * packet should be routed based on its address. This field is
230 * initialized in the constructor and is thus always valid
231 * (unlike * addr, size, and src). */
234 /** Are the 'addr' and 'size' fields valid? */
236 /** Is the 'src' field valid? */
242 /** Used to calculate latencies for each packet.*/
245 /** The time at which the packet will be fully transmitted */
248 /** The time at which the first chunk of the packet will be transmitted */
251 /** The special destination address indicating that the packet
252 * should be routed based on its address. */
253 static const short Broadcast = -1;
255 /** A pointer to the original request. */
258 /** A virtual base opaque structure used to hold coherence-related
259 * state. A specific subclass would be derived from this to
260 * carry state specific to a particular coherence protocol. */
261 class CoherenceState : public FastAlloc {
263 virtual ~CoherenceState() {}
266 /** This packet's coherence state. Caches should use
267 * dynamic_cast<> to cast to the state appropriate for the
268 * system's coherence protocol. */
269 CoherenceState *coherence;
271 /** A virtual base opaque structure used to hold state associated
272 * with the packet but specific to the sending device (e.g., an
273 * MSHR). A pointer to this state is returned in the packet's
274 * response so that the sender can quickly look up the state
275 * needed to process it. A specific subclass would be derived
276 * from this to carry state specific to a particular sending
278 class SenderState : public FastAlloc {
280 virtual ~SenderState() {}
283 /** This packet's sender state. Devices should use dynamic_cast<>
284 * to cast to the state appropriate to the sender. */
285 SenderState *senderState;
289 /** The command field of the packet. */
292 /** Return the string name of the cmd field (for debugging and
294 const std::string &cmdString() const { return cmd.toString(); }
296 /** Return the index of this command. */
297 inline int cmdToIndex() const { return cmd.toInt(); }
301 bool isRead() const { return cmd.isRead(); }
302 bool isWrite() const { return cmd.isWrite(); }
303 bool isRequest() const { return cmd.isRequest(); }
304 bool isResponse() const { return cmd.isResponse(); }
305 bool needsResponse() const { return cmd.needsResponse(); }
306 bool isInvalidate() const { return cmd.isInvalidate(); }
307 bool hasData() const { return cmd.hasData(); }
308 bool isReadWrite() const { return cmd.isReadWrite(); }
310 bool isCacheFill() const { return (flags & CACHE_LINE_FILL) != 0; }
311 bool isNoAllocate() const { return (flags & NO_ALLOCATE) != 0; }
312 bool isCompressed() const { return (flags & COMPRESSED) != 0; }
314 bool nic_pkt() { panic("Unimplemented"); M5_DUMMY_RETURN }
316 /** Possible results of a packet's request. */
325 /** The result of this packet's request. */
328 /** Accessor function that returns the source index of the packet. */
329 short getSrc() const { assert(srcValid); return src; }
330 void setSrc(short _src) { src = _src; srcValid = true; }
332 /** Accessor function that returns the destination index of
334 short getDest() const { return dest; }
335 void setDest(short _dest) { dest = _dest; }
337 Addr getAddr() const { assert(addrSizeValid); return addr; }
338 int getSize() const { assert(addrSizeValid); return size; }
339 Addr getOffset(int blkSize) const { return addr & (Addr)(blkSize - 1); }
341 void addrOverride(Addr newAddr) { assert(addrSizeValid); addr = newAddr; }
342 void cmdOverride(MemCmd newCmd) { cmd = newCmd; }
344 /** Constructor. Note that a Request object must be constructed
345 * first, but the Requests's physical address and size fields
346 * need not be valid. The command and destination addresses
347 * must be supplied. */
348 Packet(Request *_req, MemCmd _cmd, short _dest)
349 : data(NULL), staticData(false), dynamicData(false), arrayData(false),
350 addr(_req->paddr), size(_req->size), dest(_dest),
351 addrSizeValid(_req->validPaddr),
353 req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
360 /** Alternate constructor if you are trying to create a packet with
361 * a request that is for a whole block, not the address from the req.
362 * this allows for overriding the size/addr of the req.*/
363 Packet(Request *_req, MemCmd _cmd, short _dest, int _blkSize)
364 : data(NULL), staticData(false), dynamicData(false), arrayData(false),
365 addr(_req->paddr & ~(_blkSize - 1)), size(_blkSize),
367 addrSizeValid(_req->validPaddr), srcValid(false),
368 req(_req), coherence(NULL), senderState(NULL), cmd(_cmd),
377 { if (staticData || dynamicData) deleteData(); }
379 /** Reinitialize packet address and size from the associated
380 * Request object, and reset other fields that may have been
381 * modified by a previous transaction. Typically called when a
382 * statically allocated Request/Packet pair is reused for
383 * multiple transactions. */
384 void reinitFromRequest() {
385 assert(req->validPaddr);
390 addrSizeValid = true;
399 /** Take a request packet and modify it in place to be suitable
400 * for returning as a response to that request. Used for timing
401 * accesses only. For atomic and functional accesses, the
402 * request packet is always implicitly passed back *without*
403 * modifying the destination fields, so this function
404 * should not be called. */
405 void makeTimingResponse() {
406 assert(needsResponse());
408 cmd = cmd.responseCommand();
414 * Take a request packet and modify it in place to be suitable for
415 * returning as a response to that request.
417 void makeAtomicResponse()
419 assert(needsResponse());
421 cmd = cmd.responseCommand();
425 * Take a request packet that has been returned as NACKED and
426 * modify it so that it can be sent out again. Only packets that
427 * need a response can be NACKED, so verify that that is true.
432 assert(needsResponse() && result == Nacked);
439 * Set the data pointer to the following value that should not be
442 template <typename T>
448 data = (PacketDataPtr)p;
453 * Set the data pointer to a value that should have delete []
456 template <typename T>
458 dataDynamicArray(T *p)
460 assert(!staticData && !dynamicData);
461 data = (PacketDataPtr)p;
467 * set the data pointer to a value that should have delete called
470 template <typename T>
474 assert(!staticData && !dynamicData);
475 data = (PacketDataPtr)p;
480 /** get a pointer to the data ptr. */
481 template <typename T>
485 assert(staticData || dynamicData);
489 /** return the value of what is pointed to in the packet. */
490 template <typename T>
493 /** set the value in the data pointer to v. */
494 template <typename T>
498 * delete the data pointed to in the data pointer. Ok to call to
499 * matter how data was allocted.
503 /** If there isn't data in the packet, allocate some. */
506 /** Do the packet modify the same addresses. */
507 bool intersect(PacketPtr p);
510 /** This function given a functional packet and a timing packet either
511 * satisfies the timing packet, or updates the timing packet to
512 * reflect the updated state in the timing packet. It returns if the
513 * functional packet should continue to traverse the memory hierarchy
516 bool fixPacket(PacketPtr func, PacketPtr timing);
518 /** This function is a wrapper for the fixPacket field that toggles
519 * the hasData bit it is used when a response is waiting in the
520 * caches, but hasn't been marked as a response yet (so the fixPacket
521 * needs to get the correct value for the hasData)
523 bool fixDelayedResponsePacket(PacketPtr func, PacketPtr timing);
525 std::ostream & operator<<(std::ostream &o, const Packet &p);
527 #endif //__MEM_PACKET_HH